From patchwork Wed Jan 20 02:27:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hajime Tazaki X-Patchwork-Id: 1428982 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2001:8b0:10b:1231::1; helo=merlin.infradead.org; envelope-from=linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=merlin.20170209 header.b=jaSpOLwO; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=C0e6cnyg; dkim-atps=neutral Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1231::1]) (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) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4DL8c60lj9z9sB4 for ; Wed, 20 Jan 2021 13:29:26 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=+OMHzUBi2wcvIy5ay4UEEb2gv7i8USLcd9xGd3+dsx0=; b=jaSpOLwO8S8kDYb21TZTuc8GH ZD8M6CleR6jqQ3sH+cRJ0E8MklI2P7r3ZWqw4T1DOZJFJX8cVz9/Rp7/svGcCri2iu7+UM0kofNAQ 7SpCRMu5vGkQw1BnsZMKmzr6ImuolUP5QE+AQoBirq0z6Qiw5IDBMZoFoJsSVkEnRP5p6m183wLFD PgoObAB4ofvHq1M29caUOX1y6LnCkkDQ5hInEj0SmLc4/oQxfBviX/kshfIkK+4WXOXPrKENzAhjh vhNxxilkQ8pPj+R20kQjwdtQWJzo4G+ppmbICyk168FHe2A2zwQk37PEn1U2Yr4RjvBLblCFLPVLX lpkCyrq3g==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1l23FF-0002Kw-Sk; Wed, 20 Jan 2021 02:29:17 +0000 Received: from mail-pj1-x102d.google.com ([2607:f8b0:4864:20::102d]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1l23FC-0002KQ-M9 for linux-um@lists.infradead.org; Wed, 20 Jan 2021 02:29:15 +0000 Received: by mail-pj1-x102d.google.com with SMTP id my11so2365831pjb.1 for ; Tue, 19 Jan 2021 18:29:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Ypgiu6q1Iz0Yn4IRUnjyh+oh2jFCd4nbbIEvAdriWkc=; b=C0e6cnygwKk6Y9XF6vxQ0vcFiifcdTbW181s5RjoVPl40L+dcFnwKQIC9limi/xs/U sNanCYEPYLBBbLw8ltoZ3WwQ0rsCklPI78VpBZ8FfsUwiqSeQrzzYlWDHigtbQsrDkFY FdFo38RvEBbZ+7AR9pS4WfG392jtmEA2KRXM1XYWdcLf3FjkiyB2ANofLYQFQ3Rhe5tu L0jIqqaspaCJ7EwhLawyrYLO4/sXXJHu7PFzX36WUh9+pLIXB+IBU+JKaL6kXYGq/WNH 34Q2jdhKlPxBCGbKR6mTzd5zEgL/wouzZs/RyMakq6Su/gUHE0lwLjexiHse+PnFHY8x 1BkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Ypgiu6q1Iz0Yn4IRUnjyh+oh2jFCd4nbbIEvAdriWkc=; b=FBCpjw9x2Rjx3xGxri+W42CASVcClgRsArFIsQBbJylMy1911w3DcEnM7L3VJUr9sm xZ6enTaq5gMSJPDnrl9+3Tv7BiNTGBH8iWPS/XFFYL4DAzYUJ7nzWw5flhINZlXoylZ1 7G2QRZlkuTccxDjwNla8bqrhkbn/VYTta8PH0pK59b0hEJS833A11oFNq6HV43bkn07u 7CICJ0D0+piasvgePWgnswLt+Wg2ick2FvAIQ4pWMfwCQKDob9ptCXPC9f0kjHGqInCB CZC8jSMOJo/oOQFRoORPwENfSEErN4GsBf2VezCylRoIJBcg/ozGDR92VuhY79EgAI/k 1W/w== X-Gm-Message-State: AOAM532+7xc2flfPIteyHijK/d75eg0RLrRPZFBd8t0nrhDlTZ8EnSb2 tsreBYKvvJHk+44cc+Od2ZA= X-Google-Smtp-Source: ABdhPJzqa8uR2DbbFBTVcxjURcmO3NvsV+8B+mYGJjsVv8miCKayaFHrS0wExD8jC00JTzpvWbJzDw== X-Received: by 2002:a17:90a:e396:: with SMTP id b22mr2935724pjz.155.1611109752672; Tue, 19 Jan 2021 18:29:12 -0800 (PST) Received: from earth-mac.local (219x123x138x129.ap219.ftth.ucom.ne.jp. [219.123.138.129]) by smtp.gmail.com with ESMTPSA id 6sm389592pfz.34.2021.01.19.18.29.10 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 19 Jan 2021 18:29:11 -0800 (PST) Received: by earth-mac.local (Postfix, from userid 501) id 8E67F20442D38F; Wed, 20 Jan 2021 11:29:08 +0900 (JST) From: Hajime Tazaki To: linux-um@lists.infradead.org, jdike@addtoit.com, richard@nod.at, anton.ivanov@cambridgegreys.com Subject: [RFC v8 12/20] um: lkl: initialization and cleanup Date: Wed, 20 Jan 2021 11:27:17 +0900 Message-Id: <031847ceca73023566fba8e84433a615eac6a2f3.1611103406.git.thehajime@gmail.com> X-Mailer: git-send-email 2.20.1 (Apple Git-117) In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210119_212914_726549_D2B1ECD0 X-CRM114-Status: GOOD ( 24.01 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:102d listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [thehajime[at]gmail.com] -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain X-BeenThere: linux-um@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: tavi.purdila@gmail.com, linux-kernel-library@freelists.org, linux-arch@vger.kernel.org, thehajime@gmail.com, retrage01@gmail.com Sender: "linux-um" Errors-To: linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Add the lkl_start_kernel and lkl_sys_halt APIs that start and respectively stops the Linux kernel. lkl_start_kernel creates a separate threads that will run the initial and idle kernel thread. It waits for the kernel to complete initialization before returning, to avoid races with system calls issues by the host application. During the setup phase, we create "/init" in initial ramfs root filesystem to avoid mounting the "real" rootfs since ramfs is good enough for now. lkl_sys_halt will shutdown the kernel, terminate all threads and free all host resources used by the kernel before returning. Signed-off-by: Hajime Tazaki Signed-off-by: Octavian Purdila --- arch/um/lkl/include/uapi/asm/host_ops.h | 20 +++ arch/um/lkl/um/setup.c | 162 ++++++++++++++++++++++++ tools/um/uml/process.c | 2 + 3 files changed, 184 insertions(+) create mode 100644 arch/um/lkl/um/setup.c diff --git a/arch/um/lkl/include/uapi/asm/host_ops.h b/arch/um/lkl/include/uapi/asm/host_ops.h index 976fc4301b3d..85d7d4790602 100644 --- a/arch/um/lkl/include/uapi/asm/host_ops.h +++ b/arch/um/lkl/include/uapi/asm/host_ops.h @@ -252,4 +252,24 @@ void *lkl_tls_get(struct lkl_tls_key *key); */ void lkl_print(const char *str, int len); +/** + * lkl_panic - called during a kernel panic + * + */ +void lkl_panic(void); + +/** + * lkl_start_kernel() - registers the host operations and starts the kernel + * + * @ops: pointer to host operations + * @fmt: format for command line string that is going to be used to + * generate the Linux kernel command line + * + * Return: 0 if there is no error; otherwise non-zero value returns. + * + * The function returns only after the kernel is shutdown with lkl_sys_halt. + */ +int lkl_start_kernel(struct lkl_host_operations *ops, + const char *fmt, ...); + #endif diff --git a/arch/um/lkl/um/setup.c b/arch/um/lkl/um/setup.c new file mode 100644 index 000000000000..ba8338d4fc23 --- /dev/null +++ b/arch/um/lkl/um/setup.c @@ -0,0 +1,162 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +static void *init_sem; +static int is_running; + + +struct lkl_host_operations *lkl_ops; + +long lkl_panic_blink(int state) +{ + lkl_panic(); + return 0; +} + +static void __init *lkl_run_kernel(void *arg) +{ + + panic_blink = lkl_panic_blink; + + threads_init(); + lkl_cpu_get(); + start_kernel(); + + return NULL; +} + +int __init lkl_start_kernel(struct lkl_host_operations *ops, + const char *fmt, ...) +{ + int ret; + + lkl_ops = ops; + + init_sem = lkl_sem_alloc(0); + if (!init_sem) + return -ENOMEM; + + ret = lkl_cpu_init(); + if (ret) + goto out_free_init_sem; + + ret = lkl_thread_create(lkl_run_kernel, NULL); + if (!ret) { + ret = -ENOMEM; + goto out_free_init_sem; + } + + lkl_sem_down(init_sem); + lkl_sem_free(init_sem); + current_thread_info()->task->thread.arch.tid = lkl_thread_self(); + lkl_cpu_change_owner(current_thread_info()->task->thread.arch.tid); + + lkl_cpu_put(); + is_running = 1; + + return 0; + +out_free_init_sem: + lkl_sem_free(init_sem); + + return ret; +} + +int lkl_is_running(void) +{ + return is_running; +} + + +long lkl_sys_halt(void) +{ + long err; + long params[6] = {LINUX_REBOOT_MAGIC1, + LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART, }; + + err = lkl_syscall(__NR_reboot, params); + if (err < 0) + return err; + + is_running = false; + + lkl_cpu_wait_shutdown(); + + syscalls_cleanup(); + threads_cleanup(); + /* Shutdown the clockevents source. */ + tick_suspend_local(); + free_mem(); + lkl_thread_join(current_thread_info()->task->thread.arch.tid); + + return 0; +} + + +static int lkl_run_init(struct linux_binprm *bprm); + +static struct linux_binfmt lkl_run_init_binfmt = { + .module = THIS_MODULE, + .load_binary = lkl_run_init, +}; + +static int lkl_run_init(struct linux_binprm *bprm) +{ + int ret; + + if (strcmp("/init", bprm->filename) != 0) + return -EINVAL; + + ret = begin_new_exec(bprm); + if (ret) + return ret; + set_personality(PER_LINUX); + setup_new_exec(bprm); + + set_binfmt(&lkl_run_init_binfmt); + + init_pid_ns.child_reaper = 0; + + syscalls_init(); + + lkl_sem_up(init_sem); + lkl_thread_exit(); + + return 0; +} + + +/* skip mounting the "real" rootfs. ramfs is good enough. */ +static int __init fs_setup(void) +{ + int fd, flags = 0; + + if (force_o_largefile()) + flags |= O_LARGEFILE; + fd = do_sys_open(AT_FDCWD, "/init", O_CREAT | flags, 0700); + WARN_ON(fd < 0); + close_fd(fd); + + register_binfmt(&lkl_run_init_binfmt); + + return 0; +} +late_initcall(fs_setup); diff --git a/tools/um/uml/process.c b/tools/um/uml/process.c index e52dd37ddadc..c39d914e80ac 100644 --- a/tools/um/uml/process.c +++ b/tools/um/uml/process.c @@ -114,6 +114,8 @@ void os_kill_process(int pid, int reap_child) void os_kill_ptraced_process(int pid, int reap_child) { + if (pid == 0) + return; kill(pid, SIGKILL); ptrace(PTRACE_KILL, pid); ptrace(PTRACE_CONT, pid);