From nobody Tue Jan 24 22:12:21 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4P1h6F5M6Pz3bfNk; Tue, 24 Jan 2023 22:12:21 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4P1h6F399Kz3CfT; Tue, 24 Jan 2023 22:12:21 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1674598341; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=XU3Y6S6Spa9pdPcULux8iWU/K+EDGsTX6BXZCxtYPnY=; b=Z20BoyaLWRDlQ/EGnsK72lLKi6QWBtLEsUBiYYpHuy3YLJ72sMEOlqUhKPJAGs4anHKUwE wTDqSJid7izsU2dBEyccRwtalGAowoN8pzr8JVezyVY0QYJQ4CFFcHyRK7bF98GvKlC9u1 VNUV7RaFBpfbb30zjFc+/BAzByDHs2mQA1KnXHbPlHlgwVyw/P6dx2+TYZqMynZSLKJcs+ FlvAPvaAxy4nTTS6+2yvNINVdrk1/S7358xRVPDptZGL2gfxwlBRWBhtocJCtFPWY4xAZ0 8On8MzXxUmHX2IdO5hNsWdxS1KuX40Du8+UwHgsIknWV2tX1QfoW1CwS7ueImw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1674598341; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=XU3Y6S6Spa9pdPcULux8iWU/K+EDGsTX6BXZCxtYPnY=; b=iBpkO4AcITUSsZRmgcGReIn/hZlKj3UKELzoixdgkfgRB8avvEWtcC309QIwKgy/WGTKoP qvMNCPVuwVLaXXKn5Y9xiSgXb2/PUKtE50eTIeICpfPHlzvhOLwvgWhsz8m7f+h1lma/cs qU7jdVBOXVy48rSB4HjA1lVbJrD6F6W1N9bLhEEZv3aY1RL/pfxtZJ65EM7D9Ej8U5thQx tcwCgC7GnG5h//Lwt6FE72Yc9U1LdWZQl/W6D7aVW3ZnOAUtfQcedPr10XrcnNdGcW5UB2 VnrGgp2CqhsRuvX44gG47tIbB3U52hdVvM6i6fAQK1b3ygvm6EYNEjMLvaaTfQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1674598341; a=rsa-sha256; cv=none; b=a93/zGoAqQ++aFYfUX0NSSzSDZH1IqMab6K2jIlQeXOe+04DzVBUiTUvbxfHR1yt7wYzQQ mY0b+Rtr0QfsKzpYNMTETuIRNq4o7CtbNSloGBJkA1roi6i8TEHS8MVpwIpZRt6j8G9yT0 nWxkD2vULLU+rDPRNpu++Xx+/D+qJyOCy24L0CiFgIMIiHh6lZcVnsUPUzawOhRa1ZOIb6 xEFG4qO8sc74xlkVTwc57372j/eiazw8EqDU1ayGh4UHbRBtoG3yhR551TdrOP/uA95k9Q s1zz180Rqg+7PlCIGXspp+5zaek9JcZ7KSTPtJXb1YhNW1PvTem8KboM2i/llg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4P1h6F2D2dzQY3; Tue, 24 Jan 2023 22:12:21 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 30OMCLwS086910; Tue, 24 Jan 2023 22:12:21 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 30OMCL74086909; Tue, 24 Jan 2023 22:12:21 GMT (envelope-from git) Date: Tue, 24 Jan 2023 22:12:21 GMT Message-Id: <202301242212.30OMCL74086909@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Warner Losh Subject: git: 77fd18a8271e - stable/13 - kboot: add minmalist init functionality List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 77fd18a8271ea32db8acefeab6e3fbeb181bbfec Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=77fd18a8271ea32db8acefeab6e3fbeb181bbfec commit 77fd18a8271ea32db8acefeab6e3fbeb181bbfec Author: Warner Losh AuthorDate: 2022-09-01 17:06:43 +0000 Commit: Warner Losh CommitDate: 2023-01-24 21:49:33 +0000 kboot: add minmalist init functionality It is desirable to run kboot as the first program in some LinuxBoot environments. This is the traditional "pid 1" or "init" program. When running as pid 1. rovide a minimal environment based on what sysvinit, u-root, initramfs-tools and other like projects do. We mount /dev, /sys, /proc, make symlinks from /dev/fd to /dev/proc, and create /tmp, /run, and /var. We also setup stdin/out/err to the console, set the tty characteristics of same and block the appropriate signals. This is indended as an environment that never does a fork/exec. If that's required, the process groups, session leaders and all things POSIX terminal handlers will need to be added. Unlike the general purpose linux projects in this area, no attempt is made to support very old kernels. When not pid 1, we skip all of the above. Sponsored by: Netflix Differential Revision: https://reviews.freebsd.org/D36368 (cherry picked from commit 57f90cf81338ca9d1e403fdfeac16f21def72763) --- stand/kboot/Makefile | 1 + stand/kboot/init.c | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++ stand/kboot/kboot.h | 12 +++++ stand/kboot/main.c | 7 ++- 4 files changed, 147 insertions(+), 2 deletions(-) diff --git a/stand/kboot/Makefile b/stand/kboot/Makefile index 986e636a8c35..b8e71fd70dc7 100644 --- a/stand/kboot/Makefile +++ b/stand/kboot/Makefile @@ -19,6 +19,7 @@ INSTALLFLAGS= -b # Architecture-specific loader code SRCS= crt1.c conf.c vers.c main.c host_syscalls.c hostcons.c hostdisk.c kbootfdt.c gfx_fb_stub.c +SRCS+= init.c SRCS+= termios.c CFLAGS.gfx_fb_stub.c += -I${SRCTOP}/contrib/pnglite -I${SRCTOP}/sys/teken diff --git a/stand/kboot/init.c b/stand/kboot/init.c new file mode 100644 index 000000000000..e707f65096d7 --- /dev/null +++ b/stand/kboot/init.c @@ -0,0 +1,129 @@ +/*- + * Copyright (c) 2022, Netflix, Inc. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +/* + * Mini-init(8) so we can run as init/pid 1 in a LinuxBoot environment. + */ + +#include "stand.h" +#include "host_syscall.h" +#include "kboot.h" + +/* + * Create a 'standard' early boot environment. Cribbed from the things that + * sysvinit, u-root, and initramfs-tools do. This is a minimal environment + * for modern Linux systems, though the /tmp, /run and /var stuff can likely + * be done inside the initrd image itself (as can creating the mount points + * for /proc, /dev and /sys). + * + * Note: We ignore errors here. There's no stderr to report them to yet. These + * operations generally can't fail, but if they do, we may not have the ability + * to report them later. + */ +static void +init_fs_env(void) +{ + /* + * Create directories for mandatory filesystems and mount them. + */ + host_mkdir("/proc", 0555); + host_mount("proc", "/proc", "proc", MS_RELATIME, ""); + host_mkdir("/sys", 0555); + host_mount("sysfs", "/sys", "sysfs", MS_RELATIME, ""); + host_mkdir("/dev", 0755); + host_mount("devtmpfs", "/dev", "devtmpfs", MS_RELATIME, + "mode=0755,nr_inodes=0"); + + /* + * Create compat links: /dev/fd lives in /proc, and needs some help to + * get setup. + */ + host_symlink("/proc/self/fd", "/dev/fd"); + host_symlink("fd/0", "/dev/stdin"); + host_symlink("fd/1", "/dev/stdout"); + host_symlink("fd/2", "/dev/stderr"); + + + /* + * Unsure if we need this, but create a sane /tmp just in case that's useful. + * and point /run over to it. + */ + host_mkdir("/tmp", 01777); + host_mount("tmpfs", "/tmp", "tmpfs", MS_RELATIME, "size=10%,mode=1777"); + host_symlink("/tmp", "/run"); + + /* + * Unsure the loader needs /var and /var/log, but they are easy to + * create. + */ + host_mkdir("/var", 0555); + host_mkdir("/var/lock", 0555); + host_symlink("/tmp", "/var/tmp"); +} + +static void +init_tty(void) +{ + int fd; + + /* + * sysvinit asks the linux kernel to convert the CTRL-ALT-DEL to a SIGINT, + * but we skip that. + */ + + /* + * Setup /dev/console as stdin/out/err + */ + host_close(0); + host_close(1); + host_close(2); + fd = host_open("/dev/console", HOST_O_RDWR | HOST_O_NOCTTY, 0); + host_dup(fd); + host_dup(fd); +#if 0 + /* + * I think we may need to put it in 'raw' mode, but maybe not. Linux + * sysvinit sets it into 'sane' mode with several tweaks. Not enabled at + * the moment since host console initialization seems sufficient. + */ + struct host_termios tty; + + host_cfmakeraw(&tty); + host_tcsetattr(fd, HOST_TCANOW, &tty); + host_tcflush(fd, HOST_TCIOFLUSH) +#endif +} + +static void +init_sig(void) +{ + /* + * since we're running as init, we need to catch some signals + */ + + /* + * setup signals here + * + * sysvinit catches a lot of signals, but the boot loader needn't catch + * so many since we don't do as much as it does. If we need to, put the + * signal catching / ignoring code here. If we implement a 'shell' + * function to spawn a sub-shell, we'll likely need to do a lot more. + */ +} + +void +do_init(void) +{ + /* + * Only pid 1 is init + */ + if (host_getpid() != 1) + return; + + init_fs_env(); + init_tty(); + init_sig(); +} diff --git a/stand/kboot/kboot.h b/stand/kboot/kboot.h new file mode 100644 index 000000000000..01de346234f3 --- /dev/null +++ b/stand/kboot/kboot.h @@ -0,0 +1,12 @@ +/*- + * Copyright (c) 2022, Netflix, Inc. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#ifndef KBOOT_H +#define KBOOT_H + +void do_init(void); + +#endif /* KBOOT_H */ diff --git a/stand/kboot/main.c b/stand/kboot/main.c index 5d40e2c3b582..be08528049e6 100644 --- a/stand/kboot/main.c +++ b/stand/kboot/main.c @@ -34,7 +34,7 @@ __FBSDID("$FreeBSD$"); #include #include #include "host_syscall.h" - +#include "kboot.h" struct arch_switch archsw; extern void *_end; @@ -261,8 +261,11 @@ main(int argc, const char **argv) const size_t heapsize = 15*1024*1024; const char *bootdev; + /* Give us a sane world if we're running as init */ + do_init(); + /* - * Set the heap to one page after the end of the loader. + * Setup the heap 15MB should be plenty */ heapbase = host_getmem(heapsize); setheap(heapbase, heapbase + heapsize);