From patchwork Thu Sep 24 07:12:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hajime Tazaki X-Patchwork-Id: 1370378 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; dmarc=fail (p=none dis=none) header.from=gmail.com 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=XopINfu5; 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=f38weITE; 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 4BxmY159Hmz9sTH for ; Thu, 24 Sep 2020 17:15:49 +1000 (AEST) 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=SdvmCbudIaKEldkOZhTAuL+PTlCIJJf4cSkstnypYBk=; b=XopINfu5KTB4mznijN1jUCkhw R/kZNWam9FhjSEcyPeQx2575MG16Ca786JIFjbaKcWcox5B7IvfsAq5jd6iA1IP/6nA7IzAv39XvY Hix8lTanq2GGX6bIElwBtjr2rQRhXJdFdmxnz4e8oNXwVQNx7CLIwcVOKXNbXCr4ljTlFK2+gfzsx aogaUNyvu9hrFu+F0SAK+2rfA8lX1i9dLTUU/SfMqt8TnS6odLk8TAyI3jkRurgNWpnGk/a9y7T06 /xGN7zlFrCrVQqYdAuA4mmb/vteNsOKdM/J0afJXtONRW9RaSiLM/lGxWF2yvJhWxq8zPlccXtbAb I+J6Hf3pw==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kLLTm-0003dT-0D; Thu, 24 Sep 2020 07:15:46 +0000 Received: from mail-pl1-x643.google.com ([2607:f8b0:4864:20::643]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kLLTi-0003c0-RC for linux-um@lists.infradead.org; Thu, 24 Sep 2020 07:15:44 +0000 Received: by mail-pl1-x643.google.com with SMTP id y6so1182880plt.9 for ; Thu, 24 Sep 2020 00:15:42 -0700 (PDT) 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=jYKWSZpS/C8VsskkJfHhWWpBDQWCJdoW8/7KByaLGe0=; b=f38weITEX6kqbHCteojypv/g238Ry4uszmYgf5LI0oPwoANsVqQ9D4IAAgFd3GZoQa X3l5yBNP/Xwt6IqzIdReEmqMhKUYR6FaXxbLeehcztCHzxTjGuFv+BY8c9ANNL0eE19x JSN7TALRcnTkpEEYMrWA1cxJ6v/Dal3CrbvbyUaei4a4UoXovSS6CaFkTdcLEyrBoYOg p1nC7JNE7tKHqwqxj9cYrf+9z/AxWhV6yUMRzYtIFUJzlfUHyAhEByywxN8q2jlVpe2c N+WJwVYk8IGij1pbSARSY0Tr+6P5yCMPPv+v3DBrgtQbXCryroOitqMberZFW43aMTDD p+Uw== 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=jYKWSZpS/C8VsskkJfHhWWpBDQWCJdoW8/7KByaLGe0=; b=t0MxqirbYNaIphizVwkWWtEmuuc57Xths5Bi5NPZXTKGYXwsaoTVUTknxoMBgIOH9/ /7NB2N37IrhI4KrKbD3rhkkkTr1xS58olejDVf61Ccad9zEYeaqfxZYLXYK2YOz+US4h H/uC0kpZx58nkqSqq9rra/UXL+YASuBDPM3pXs5n3dFcT0rmO7TgqtnQ9kfSi2VD+Lw3 Jq8/CjH5W1nNbme6OMF4O0q2WfiGqf2jueqXLJNOtWeB9t3OoZJDBqcE9j9uluIRegpO VcZrfOgi2QjyROE3vUYHzIDQ6TTcyc7bw/lmZptKVIo95eBo6bZ8LpI1owYkGKsw66tZ NsrA== X-Gm-Message-State: AOAM5335/bywnVPVKtZglUgZMlVnqdqDg3+zNZYGIC2qCF30rSl/O0ii ZCJPfjWI+g3LsQIKU2eCQVc= X-Google-Smtp-Source: ABdhPJxdkd66wudGGPo0TVb1Xyuk0MImYs87Nzu4aJK2YbOnApLjyX2mf6T7t9fOUWKOetsiFc0UjQ== X-Received: by 2002:a17:902:b117:b029:d1:e5e7:bdf5 with SMTP id q23-20020a170902b117b02900d1e5e7bdf5mr3116803plr.85.1600931740912; Thu, 24 Sep 2020 00:15:40 -0700 (PDT) Received: from earth-mac.local (219x123x138x129.ap219.ftth.ucom.ne.jp. [219.123.138.129]) by smtp.gmail.com with ESMTPSA id 75sm1879070pgg.22.2020.09.24.00.15.39 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 24 Sep 2020 00:15:40 -0700 (PDT) Received: by earth-mac.local (Postfix, from userid 501) id 5C7D12037C20B1; Thu, 24 Sep 2020 16:15:34 +0900 (JST) From: Hajime Tazaki To: linux-um@lists.infradead.org, jdike@addtoit.com, richard@nod.at, anton.ivanov@cambridgegreys.com Subject: [RFC v6 14/21] um: nommu: initialization and cleanup Date: Thu, 24 Sep 2020 16:12:54 +0900 Message-Id: <12f3ba8d5a73a068c5bb116cadbb0a2e55334b06.1600922529.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-20200924_031542_967219_4FCE5AAF X-CRM114-Status: GOOD ( 25.46 ) 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:643 listed in] [list.dnswl.org] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [thehajime[at]gmail.com] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -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 -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 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, Hajime Tazaki , 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/nommu/include/uapi/asm/host_ops.h | 14 ++ arch/um/nommu/um/setup.c | 162 ++++++++++++++++++++++ tools/um/uml/process.c | 2 + 3 files changed, 178 insertions(+) create mode 100644 arch/um/nommu/um/setup.c diff --git a/arch/um/nommu/include/uapi/asm/host_ops.h b/arch/um/nommu/include/uapi/asm/host_ops.h index 133877c857a1..0811494c080c 100644 --- a/arch/um/nommu/include/uapi/asm/host_ops.h +++ b/arch/um/nommu/include/uapi/asm/host_ops.h @@ -16,6 +16,7 @@ struct lkl_jmp_buf { * These operations must be provided by a host library or by the application * itself. * @print - optional operation that receives console messages + * @panic - called during a kernel panic * * @sem_alloc - allocate a host semaphore an initialize it to count * @sem_free - free a host semaphore @@ -65,6 +66,7 @@ struct lkl_jmp_buf { */ struct lkl_host_operations { void (*print)(const char *str, int len); + void (*panic)(void); struct lkl_sem *(*sem_alloc)(int count); void (*sem_free)(struct lkl_sem *sem); @@ -96,6 +98,18 @@ struct lkl_host_operations { void (*mem_free)(void *mem); }; +/** + * lkl_start_kernel - registers the host operations and starts the kernel + * + * The function returns only after the kernel is shutdown with lkl_sys_halt. + * + * @lkl_ops - pointer to host operations + * @cmd_line - format for command line string that is going to be used to + * generate the Linux kernel command line + */ +int lkl_start_kernel(struct lkl_host_operations *ops, + const char *fmt, ...); + void lkl_bug(const char *fmt, ...); #endif diff --git a/arch/um/nommu/um/setup.c b/arch/um/nommu/um/setup.c new file mode 100644 index 000000000000..eaa74d771f73 --- /dev/null +++ b/arch/um/nommu/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_ops->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_ops->sem_alloc(0); + if (!init_sem) + return -ENOMEM; + + ret = lkl_cpu_init(); + if (ret) + goto out_free_init_sem; + + ret = lkl_ops->thread_create(lkl_run_kernel, NULL); + if (!ret) { + ret = -ENOMEM; + goto out_free_init_sem; + } + + lkl_ops->sem_down(init_sem); + lkl_ops->sem_free(init_sem); + current_thread_info()->task->thread.arch.tid = lkl_ops->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_ops->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_ops->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_ops->sem_up(init_sem); + lkl_ops->thread_exit(); + + return 0; +} + + +/* skip mounting the "real" rootfs. ramfs is good enough. */ +static int __init fs_setup(void) +{ + int fd, flags; + + if (force_o_largefile()) + flags |= O_LARGEFILE; + fd = do_sys_open(AT_FDCWD, "/init", O_CREAT | flags, 0700); + WARN_ON(fd < 0); + ksys_close(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);