From nobody Fri Oct 31 03:42:49 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1750063615; cv=none; d=zohomail.com; s=zohoarc; b=D+CCABDMwCPFVSi7O32k/IpxjiFyyr2M6G3zHqDPJipqPJ5GNqD3mYiaCvYePf49DfyC86UwCdf1QZizDJTVlTx2eYcCrGy0Yh9ZnOW6ZRNdeJKfEU+qS7Rezyk1Mxrv9om7vUwOyaB4lfUfiIXnXavHo88/MByyD4jtnhjQcWE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750063615; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=eIbkCenoZvqJqr3MP5CvMO1uqc+kjXWDfidavHJfSOg=; b=M+fzv8aumfUC0fbDhQa6Ptp9KjJn5G/alffe7EVATujUCcvulu1WWs3sSrJj/ta7ctQ7KraEb+VTlACrn/XQ4jO6I5R7siCI0ZTX85v2NoiK7J1jZtvwKmtFRXkAFdSoo0GtIPtnjCuh42s7tlGJA02odpKbU4p/NUuBuEY5zWw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1750063615131168.61116769379248; Mon, 16 Jun 2025 01:46:55 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1016883.1393865 (Exim 4.92) (envelope-from ) id 1uR5UG-00084S-3v; Mon, 16 Jun 2025 08:46:40 +0000 Received: by outflank-mailman (output) from mailman id 1016883.1393865; Mon, 16 Jun 2025 08:46:40 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5UG-00084L-0f; Mon, 16 Jun 2025 08:46:40 +0000 Received: by outflank-mailman (input) for mailman id 1016883; Mon, 16 Jun 2025 08:46:38 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5UD-0007hV-Vy for xen-devel@lists.xenproject.org; Mon, 16 Jun 2025 08:46:37 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 695bf66e-4a8e-11f0-b894-0df219b8e170; Mon, 16 Jun 2025 10:46:36 +0200 (CEST) Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (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 smtp-out1.suse.de (Postfix) with ESMTPS id 2F11E21181; Mon, 16 Jun 2025 08:46:27 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id F3F4A139E2; Mon, 16 Jun 2025 08:46:26 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id b8obOuLZT2jgPwAAD6G6ig (envelope-from ); Mon, 16 Jun 2025 08:46:26 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 695bf66e-4a8e-11f0-b894-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063587; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eIbkCenoZvqJqr3MP5CvMO1uqc+kjXWDfidavHJfSOg=; b=E4B/oENMbf4MVYNW+Xi+9rgx/R78CrdQ/RHbovg0TUQ9U/kZqBKiOQftJ/bJnKg/BGnGdG ZJn/UqW3DfyzvC4yEJLjrBTJKcJRjBUbp9qc0F6nJGRRhli9N20MoDbbEEU75ow3HJ34px BTKmSsmkCr++8XAiw79XqPCM3ydFqjk= Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063587; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eIbkCenoZvqJqr3MP5CvMO1uqc+kjXWDfidavHJfSOg=; b=E4B/oENMbf4MVYNW+Xi+9rgx/R78CrdQ/RHbovg0TUQ9U/kZqBKiOQftJ/bJnKg/BGnGdG ZJn/UqW3DfyzvC4yEJLjrBTJKcJRjBUbp9qc0F6nJGRRhli9N20MoDbbEEU75ow3HJ34px BTKmSsmkCr++8XAiw79XqPCM3ydFqjk= From: Juergen Gross To: minios-devel@lists.xenproject.org, xen-devel@lists.xenproject.org Cc: samuel.thibault@ens-lyon.org, Juergen Gross , Jason Andryuk Subject: [MINI-OS PATCH v2 01/12] add kexec framework Date: Mon, 16 Jun 2025 10:46:08 +0200 Message-ID: <20250616084619.11112-2-jgross@suse.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250616084619.11112-1-jgross@suse.com> References: <20250616084619.11112-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-0.999]; MIME_GOOD(-0.10)[text/plain]; MIME_TRACE(0.00)[0:+]; TO_DN_SOME(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; ARC_NA(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_FIVE(0.00)[5]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,suse.com:email,suse.com:mid,amd.com:email]; RCVD_TLS_ALL(0.00)[] X-Spam-Level: X-Spam-Flag: NO X-Spam-Score: -2.80 X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1750063616756116600 Content-Type: text/plain; charset="utf-8" Add a new config option CONFIG_KEXEC for support of kexec-ing into a new mini-os kernel. Add a related kexec.c source and a kexec.h header. For now allow CONFIG_KEXEC to be set only for PVH variant of mini-os. Signed-off-by: Juergen Gross Reviewed-by: Jason Andryuk --- V2: - reword some comments (Jason Andryuk) --- Config.mk | 1 + Makefile | 1 + arch/x86/testbuild/all-no | 1 + arch/x86/testbuild/all-yes | 2 ++ arch/x86/testbuild/kexec | 4 +++ include/kexec.h | 7 +++++ kexec.c | 60 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 76 insertions(+) create mode 100644 arch/x86/testbuild/kexec create mode 100644 include/kexec.h create mode 100644 kexec.c diff --git a/Config.mk b/Config.mk index e493533a..e2afb1b4 100644 --- a/Config.mk +++ b/Config.mk @@ -204,6 +204,7 @@ CONFIG-n +=3D CONFIG_LIBXENGUEST CONFIG-n +=3D CONFIG_LIBXENTOOLCORE CONFIG-n +=3D CONFIG_LIBXENTOOLLOG CONFIG-n +=3D CONFIG_LIBXENMANAGE +CONFIG-n +=3D CONFIG_KEXEC # Setting CONFIG_USE_XEN_CONSOLE copies all print output to the Xen emerge= ncy # console apart of standard dom0 handled console. CONFIG-n +=3D CONFIG_USE_XEN_CONSOLE diff --git a/Makefile b/Makefile index d094858a..a64913ad 100644 --- a/Makefile +++ b/Makefile @@ -51,6 +51,7 @@ src-y +=3D gntmap.c src-y +=3D gnttab.c src-y +=3D hypervisor.c src-y +=3D kernel.c +src-$(CONFIG_KEXEC) +=3D kexec.c src-y +=3D lock.c src-y +=3D main.c src-y +=3D mm.c diff --git a/arch/x86/testbuild/all-no b/arch/x86/testbuild/all-no index 5b3e99ed..b2ee5ce8 100644 --- a/arch/x86/testbuild/all-no +++ b/arch/x86/testbuild/all-no @@ -18,3 +18,4 @@ CONFIG_LIBXS =3D n CONFIG_LWIP =3D n CONFIG_BALLOON =3D n CONFIG_USE_XEN_CONSOLE =3D n +CONFIG_KEXEC =3D n diff --git a/arch/x86/testbuild/all-yes b/arch/x86/testbuild/all-yes index 8ae489a4..c9a990f9 100644 --- a/arch/x86/testbuild/all-yes +++ b/arch/x86/testbuild/all-yes @@ -19,3 +19,5 @@ CONFIG_BALLOON =3D y CONFIG_USE_XEN_CONSOLE =3D y # The following are special: they need support from outside CONFIG_LWIP =3D n +# KEXEC not implemented for PARAVIRT +CONFIG_KEXEC =3D n diff --git a/arch/x86/testbuild/kexec b/arch/x86/testbuild/kexec new file mode 100644 index 00000000..ea17b4d9 --- /dev/null +++ b/arch/x86/testbuild/kexec @@ -0,0 +1,4 @@ +CONFIG_PARAVIRT =3D n +CONFIG_BALLOON =3D y +CONFIG_USE_XEN_CONSOLE =3D y +CONFIG_KEXEC =3D y diff --git a/include/kexec.h b/include/kexec.h new file mode 100644 index 00000000..6fd96774 --- /dev/null +++ b/include/kexec.h @@ -0,0 +1,7 @@ +#ifndef _KEXEC_H +#define _KEXEC_H + +int kexec(void *kernel, unsigned long kernel_size, + const char *cmdline); + +#endif /* _KEXEC_H */ diff --git a/kexec.c b/kexec.c new file mode 100644 index 00000000..7fcc5c6b --- /dev/null +++ b/kexec.c @@ -0,0 +1,60 @@ +/*************************************************************************= ***** + * kexec.c + * + * Support of kexec (reboot locally into new mini-os kernel). + * + * Copyright (c) 2024, Juergen Gross, SUSE Linux GmbH + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation = the + * rights to use, copy, modify, merge, publish, distribute, sublicense, an= d/or + * sell copies of the Software, and to permit persons to whom the Software= is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL= THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#ifdef CONFIG_PARAVIRT +#error "kexec support not implemented in PV variant" +#endif + +#include +#include +#include +#include + +/* + * General approach for kexec support (PVH only) is as follows: + * + * - New kernel needs to be in memory in form of a ELF binary in a virtual + * memory region. + * - A new start_info structure is constructed in memory with the final + * memory locations included. + * - Page tables and memory pages of the new kernel binary conflicting wit= h the + * final memory layout are moved to non-conflicting locations. + * - All memory areas needed for kexec execution are being finalized. + * - The final kexec execution stage is copied to a memory area below 4G w= hich + * doesn't conflict with the target areas of kernel etc. + * - From here on a graceful failure is no longer possible. + * - Grants and event channels are torn down. + * - Execution continues in the final execution stage. + * - All data is copied to its final addresses. + * - CPU is switched to 32-bit mode with paging disabled. + * - The new kernel is activated. + */ + +int kexec(void *kernel, unsigned long kernel_size, const char *cmdline) +{ + return ENOSYS; +} +EXPORT_SYMBOL(kexec); --=20 2.43.0 From nobody Fri Oct 31 03:42:49 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1750063621; cv=none; d=zohomail.com; s=zohoarc; b=O+FC0nqJCaQR3sAet2P1X9IW8HSVXM96N7SeMsoI8cIhL0OiJpts7pswT184cG48mQnyz88uFiJQZidrxVUx+VwdkSTo+DQn66JecUz12lpF0tJLMo5CJlorIdz+QnILwZIbYjDIY4PZAeCbLRzJopeccTxfD/SPa4vsvS9sOZM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750063621; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=2H0qWHO5jB15jbB7hAMsbxc9Fi6ipKfWjIbEbuSKCeQ=; b=GheBFOQ9UztQwQsmyOMFTX8qhUlR8LVcYZjxOGwDw2glFrCAsQXXDImXR7p+rVDWDUan+FRK41DVBM2w+9wGzbC0JosM4iYB1TE/6vLqgA+2i8I37ISEfDRpEshnAZQEhTC3Xo0WtXlbuN5T4zhOe3P0e1WogNMbgUi+qTkkzAE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1750063621227604.5994256097678; Mon, 16 Jun 2025 01:47:01 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1016888.1393889 (Exim 4.92) (envelope-from ) id 1uR5UJ-0000B5-PU; Mon, 16 Jun 2025 08:46:43 +0000 Received: by outflank-mailman (output) from mailman id 1016888.1393889; Mon, 16 Jun 2025 08:46:43 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5UJ-0000As-MT; Mon, 16 Jun 2025 08:46:43 +0000 Received: by outflank-mailman (input) for mailman id 1016888; Mon, 16 Jun 2025 08:46:42 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5UI-0007hV-U9 for xen-devel@lists.xenproject.org; Mon, 16 Jun 2025 08:46:42 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 6c289f1f-4a8e-11f0-b894-0df219b8e170; Mon, 16 Jun 2025 10:46:41 +0200 (CEST) Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (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 smtp-out1.suse.de (Postfix) with ESMTPS id C6B0A2118C; Mon, 16 Jun 2025 08:46:32 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 90F76139E2; Mon, 16 Jun 2025 08:46:32 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id EfPvIejZT2jpPwAAD6G6ig (envelope-from ); Mon, 16 Jun 2025 08:46:32 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 6c289f1f-4a8e-11f0-b894-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063592; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2H0qWHO5jB15jbB7hAMsbxc9Fi6ipKfWjIbEbuSKCeQ=; b=NeAmfA7S9fIDRKkgzKVn9X+cNEBpZmm/+MEtqjZMNR0ycHN2YxdYXQXV/9y+Lfv54S0xAE j5zIC63cOCV0aSlTw/ITmkUpJXD7a6Cyuw55QtKa/TqWxoXs/JGgEFbKYxIR4zi42ZWZJQ foKRyKDHZL2eWOGt9ZtvtWu5PkQbzBE= Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063592; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2H0qWHO5jB15jbB7hAMsbxc9Fi6ipKfWjIbEbuSKCeQ=; b=NeAmfA7S9fIDRKkgzKVn9X+cNEBpZmm/+MEtqjZMNR0ycHN2YxdYXQXV/9y+Lfv54S0xAE j5zIC63cOCV0aSlTw/ITmkUpJXD7a6Cyuw55QtKa/TqWxoXs/JGgEFbKYxIR4zi42ZWZJQ foKRyKDHZL2eWOGt9ZtvtWu5PkQbzBE= From: Juergen Gross To: minios-devel@lists.xenproject.org, xen-devel@lists.xenproject.org Cc: samuel.thibault@ens-lyon.org, Juergen Gross Subject: [MINI-OS PATCH v2 02/12] Mini-OS: add final kexec stage Date: Mon, 16 Jun 2025 10:46:09 +0200 Message-ID: <20250616084619.11112-3-jgross@suse.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250616084619.11112-1-jgross@suse.com> References: <20250616084619.11112-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-0.999]; MIME_GOOD(-0.10)[text/plain]; MIME_TRACE(0.00)[0:+]; TO_DN_SOME(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; ARC_NA(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[4]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:email,suse.com:mid,imap1.dmz-prg2.suse.org:helo]; RCVD_TLS_ALL(0.00)[] X-Spam-Level: X-Spam-Flag: NO X-Spam-Score: -2.80 X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1750063622915116600 Content-Type: text/plain; charset="utf-8" Add the code and data definitions of the final kexec stage. Put the code and related data into a dedicated section in order to be able to copy it to another location. For this reason there must be no absolute relocations being used in the code or data. Being functionally related, add a function for adding a final kexec action. Signed-off-by: Juergen Gross Reviewed-by: Jason Andryuk --- V2: - add ASSERT() to linker script (Jason Andryuk) - fix asm() input/output parameter bug (Jason Andryuk) --- arch/x86/kexec.c | 107 ++++++++++++++++++++++++++++++++++++++ arch/x86/minios-x86.lds.S | 9 ++++ arch/x86/x86_hvm.S | 46 ++++++++++++++++ include/kexec.h | 27 ++++++++++ include/x86/os.h | 5 ++ kexec.c | 22 ++++++++ 6 files changed, 216 insertions(+) create mode 100644 arch/x86/kexec.c diff --git a/arch/x86/kexec.c b/arch/x86/kexec.c new file mode 100644 index 00000000..a8a2fa97 --- /dev/null +++ b/arch/x86/kexec.c @@ -0,0 +1,107 @@ +/*************************************************************************= ***** + * kexec.c + * + * Support of kexec (reboot locally into new mini-os kernel). + * + * Copyright (c) 2024, Juergen Gross, SUSE Linux GmbH + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation = the + * rights to use, copy, modify, merge, publish, distribute, sublicense, an= d/or + * sell copies of the Software, and to permit persons to whom the Software= is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL= THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#ifdef CONFIG_KEXEC + +#include +#include +#include + +/* + * Final stage of kexec. Copies all data to the final destinations, zeroes + * .bss and activates new kernel. + * Must be called with interrupts off. Stack, code and data must be + * accessible via identity mapped virtual addresses (virt =3D=3D phys). Co= pying + * and zeroing is done using virtual addresses. + * No relocations inside the function are allowed, as it is copied to an + * allocated page before being executed. + */ +static void __attribute__((__section__(".text.kexec"))) + kexec_final(struct kexec_action *actions, unsigned long real) +{ + char *src, *dest; + unsigned int a, cnt; + + for ( a =3D 0; ; a++ ) + { + switch ( actions[a].action ) + { + case KEXEC_COPY: + dest =3D actions[a].dest; + src =3D actions[a].src; + for ( cnt =3D 0; cnt < actions[a].len; cnt++ ) + *dest++ =3D *src++; + break; + + case KEXEC_ZERO: + dest =3D actions[a].dest; + for ( cnt =3D 0; cnt < actions[a].len; cnt++ ) + *dest++ =3D 0; + break; + + case KEXEC_CALL: + asm("movl %0, %%ebx\n\t" + "movl %1, %%edi\n\t" + "jmp *%2" + : :"m" (actions[a].src), "m" (actions[a].dest), "m" (real)= ); + break; + } + } +} + +#define KEXEC_STACK_LONGS 8 +static unsigned long __attribute__((__section__(".data.kexec"))) + kexec_stack[KEXEC_STACK_LONGS]; + +static unsigned long get_kexec_addr(void *kexec_page, void *addr) +{ + unsigned long off =3D (unsigned long)addr - (unsigned long)_kexec_star= t; + + return (unsigned long)kexec_page + off; +} + +void do_kexec(void *kexec_page) +{ + unsigned long actions; + unsigned long stack; + unsigned long final; + unsigned long phys; + + actions =3D get_kexec_addr(kexec_page, kexec_actions); + stack =3D get_kexec_addr(kexec_page, kexec_stack + KEXEC_STACK_LONGS); + final =3D get_kexec_addr(kexec_page, kexec_final); + phys =3D get_kexec_addr(kexec_page, kexec_phys); + + memcpy(kexec_page, _kexec_start, KEXEC_SECSIZE); + asm("cli\n\t" + "mov %0, %%"ASM_SP"\n\t" + "mov %1, %%"ASM_ARG1"\n\t" + "mov %2, %%"ASM_ARG2"\n\t" + "jmp *%3" + : :"m" (stack), "m" (actions), "m" (phys), "m" (final)); +} + +#endif /* CONFIG_KEXEC */ diff --git a/arch/x86/minios-x86.lds.S b/arch/x86/minios-x86.lds.S index 8aae2fd6..8a1db7d4 100644 --- a/arch/x86/minios-x86.lds.S +++ b/arch/x86/minios-x86.lds.S @@ -87,6 +87,15 @@ SECTIONS =20 _edata =3D .; /* End of data section */ =20 + . =3D ALIGN(8); + _kexec_start =3D .; /* Kexec relocatable code/data */ + .kexec : { + *(.text.kexec) + *(.data.kexec) + } + _kexec_end =3D .; + ASSERT(_kexec_end - _kexec_start <=3D 4096, "kexec code/data > 1 p= age") + __bss_start =3D .; /* BSS */ .bss : { *(.bss) diff --git a/arch/x86/x86_hvm.S b/arch/x86/x86_hvm.S index 42a5f02e..e2f82e96 100644 --- a/arch/x86/x86_hvm.S +++ b/arch/x86/x86_hvm.S @@ -85,4 +85,50 @@ page_table_l2: #endif .align __PAGE_SIZE, 0 =20 +#ifdef CONFIG_KEXEC +.section .text.kexec, "ax", @progbits + +/* + * Switch off paging and call new OS for kexec. + * %ebx holds the physical address of the start_info structure + * %edi holds the physical address of the entry point to call + */ +.globl kexec_phys +kexec_phys: + /* Set DS, ES, SS to 0...ffffffff. */ + mov $(GDTE_DS32_DPL0 * 8), %eax + mov %eax, %ds + mov %eax, %es + mov %eax, %ss + +#ifdef __x86_64__ + /* Switch to 32-bit mode. */ + pushq $(GDTE_CS32_DPL0 * 8) + lea cs32_switch(%rip),%edx + push %rdx + lretq + + .code32 +cs32_switch: +#endif + /* Set %cr0 and %cr4 (disables paging). */ + mov $X86_CR0_PE, %eax + mov %eax, %cr0 + mov $0, %eax + mov %eax, %cr4 +#ifdef __x86_64__ + /* Disable 64-bit mode. */ + mov $MSR_EFER, %ecx + rdmsr + btr $_EFER_LME, %eax + wrmsr +#endif + + jmp *%edi + +#ifdef __x86_64__ + .code64 +#endif +#endif /* CONFIG_KEXEC */ + .text diff --git a/include/kexec.h b/include/kexec.h index 6fd96774..722be456 100644 --- a/include/kexec.h +++ b/include/kexec.h @@ -1,7 +1,34 @@ #ifndef _KEXEC_H #define _KEXEC_H =20 +/* One element of kexec actions (last element must have action KEXEC_CALL)= : */ +struct kexec_action { + enum { + KEXEC_COPY, /* Copy len bytes from src to dest. */ + KEXEC_ZERO, /* Zero len bytes at dest. */ + KEXEC_CALL /* Call dest with paging turned off, param is src. */ + } action; + unsigned int len; + void *dest; + void *src; +}; + +#define KEXEC_MAX_ACTIONS 16 + +extern char _kexec_start[], _kexec_end[]; +extern struct kexec_action kexec_actions[KEXEC_MAX_ACTIONS]; + +int kexec_add_action(int action, void *dest, void *src, unsigned int len); + +#define KEXEC_SECSIZE ((unsigned long)_kexec_end - (unsigned long)_kexec_s= tart) + int kexec(void *kernel, unsigned long kernel_size, const char *cmdline); =20 +/* Initiate final kexec stage. */ +void do_kexec(void *kexec_page); + +/* Assembler code for switching off paging and passing execution to new OS= . */ +void kexec_phys(void); + #endif /* _KEXEC_H */ diff --git a/include/x86/os.h b/include/x86/os.h index 0095be13..8a057d81 100644 --- a/include/x86/os.h +++ b/include/x86/os.h @@ -27,6 +27,7 @@ #define MSR_EFER 0xc0000080 #define _EFER_LME 8 /* Long mode enable */ =20 +#define X86_CR0_PE 0x00000001 /* Protected mode enable */ #define X86_CR0_WP 0x00010000 /* Write protect */ #define X86_CR0_PG 0x80000000 /* Paging */ #define X86_CR4_PAE 0x00000020 /* enable physical address extensi= ons */ @@ -64,9 +65,13 @@ #if defined(__i386__) #define __SZ "l" #define __REG "e" +#define ASM_ARG1 "eax" +#define ASM_ARG2 "edx" #else #define __SZ "q" #define __REG "r" +#define ASM_ARG1 "rdi" +#define ASM_ARG2 "rsi" #endif =20 #define ASM_SP __REG"sp" diff --git a/kexec.c b/kexec.c index 7fcc5c6b..b69e145b 100644 --- a/kexec.c +++ b/kexec.c @@ -58,3 +58,25 @@ int kexec(void *kernel, unsigned long kernel_size, const= char *cmdline) return ENOSYS; } EXPORT_SYMBOL(kexec); + +struct kexec_action __attribute__((__section__(".data.kexec"))) + kexec_actions[KEXEC_MAX_ACTIONS]; +static unsigned int act_idx; + +int kexec_add_action(int action, void *dest, void *src, unsigned int len) +{ + struct kexec_action *act; + + if ( act_idx =3D=3D KEXEC_MAX_ACTIONS ) + return -ENOSPC; + + act =3D kexec_actions + act_idx; + act_idx++; + + act->action =3D action; + act->len =3D len; + act->dest =3D dest; + act->src =3D src; + + return 0; +} --=20 2.43.0 From nobody Fri Oct 31 03:42:49 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1750063619; cv=none; d=zohomail.com; s=zohoarc; b=ItWMpsbawiJi2FWJChIfWycCGcN1vTm8axkxICWTBZtaG3Lf9Sl4Z80gpa+aSNqtTeya5YdbJUuYk8w6gjcQJiXdaYl2Jxpv/0F4qLmfr0Ha/U1NQSbgStLOo2eDRXiXTABclEu1H6ekWMPYb6TvA93JdnA2VmUUxFcidA6krls= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750063619; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=EYprPgKlu2rqJCyxTaoQnR7Oz+a9tTVs4vX0zP9GR4Y=; b=Zy4rqiSsDoUSnY1M/11L4BGV4LEwQ+up0W74E9Oq8JNg+CpWyRiA1MM/y+Edwj9FbF6CUVVm8TqnUbV9hwggPDLoCcgA8LLesM3nxvzpVOJZPFXIaeo32iSkdSt9XhuyukGcxEUrAgsn62PHUKoBRDrjHhf3ygdNA12rHEEvsvw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1750063619116129.5579129249345; Mon, 16 Jun 2025 01:46:59 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1016887.1393878 (Exim 4.92) (envelope-from ) id 1uR5UI-0008MC-EH; Mon, 16 Jun 2025 08:46:42 +0000 Received: by outflank-mailman (output) from mailman id 1016887.1393878; Mon, 16 Jun 2025 08:46:42 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5UI-0008M3-At; Mon, 16 Jun 2025 08:46:42 +0000 Received: by outflank-mailman (input) for mailman id 1016887; Mon, 16 Jun 2025 08:46:41 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5UH-0007jR-KB for xen-devel@lists.xenproject.org; Mon, 16 Jun 2025 08:46:41 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [2a07:de40:b251:101:10:150:64:2]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 6ad61262-4a8e-11f0-a309-13f23c93f187; Mon, 16 Jun 2025 10:46:38 +0200 (CEST) Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (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 smtp-out2.suse.de (Postfix) with ESMTPS id 5B3F01F38F; Mon, 16 Jun 2025 08:46:38 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 29430139E2; Mon, 16 Jun 2025 08:46:38 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id czqkCO7ZT2j+PwAAD6G6ig (envelope-from ); Mon, 16 Jun 2025 08:46:38 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 6ad61262-4a8e-11f0-a309-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063598; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EYprPgKlu2rqJCyxTaoQnR7Oz+a9tTVs4vX0zP9GR4Y=; b=oUHhG+ibSeYEf6fZOIQnq/6IUeGc81v46tTZNBQfSvXGJzLhVWckDEYukjyNmYPomyijUa pS6uHk1GEsum385eSw10TpkHECdNhatrGstiPgfXusxNsn17gp3N6igrKoZ6+ZxOmzYxOG LZO3dypVFQovAW5dB7E24Hd3A9uXpWw= Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=oUHhG+ib DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063598; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EYprPgKlu2rqJCyxTaoQnR7Oz+a9tTVs4vX0zP9GR4Y=; b=oUHhG+ibSeYEf6fZOIQnq/6IUeGc81v46tTZNBQfSvXGJzLhVWckDEYukjyNmYPomyijUa pS6uHk1GEsum385eSw10TpkHECdNhatrGstiPgfXusxNsn17gp3N6igrKoZ6+ZxOmzYxOG LZO3dypVFQovAW5dB7E24Hd3A9uXpWw= From: Juergen Gross To: minios-devel@lists.xenproject.org, xen-devel@lists.xenproject.org Cc: samuel.thibault@ens-lyon.org, Juergen Gross , Jan Beulich Subject: [MINI-OS PATCH v2 03/12] mini-os: add elf.h Date: Mon, 16 Jun 2025 10:46:10 +0200 Message-ID: <20250616084619.11112-4-jgross@suse.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250616084619.11112-1-jgross@suse.com> References: <20250616084619.11112-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; ARC_NA(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; MIME_TRACE(0.00)[0:+]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; TO_DN_SOME(0.00)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns,suse.com:dkim,suse.com:mid,suse.com:email]; RCVD_TLS_ALL(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCPT_COUNT_FIVE(0.00)[5]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; DKIM_TRACE(0.00)[suse.com:+] X-Spam-Level: X-Spam-Flag: NO X-Rspamd-Queue-Id: 5B3F01F38F X-Rspamd-Action: no action X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Score: -3.01 X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1750063626795116600 Content-Type: text/plain; charset="utf-8" Add some definitions for accessing an ELF file. Only the file header and the program header are needed. The main source for those are elfstructs.h and libelf.h from the Xen tree. The license boiler plate of those files is being kept in the resulting header file. Signed-off-by: Juergen Gross Reviewed-by: Jan Beulich --- V2: - remove some unneeded defines (Jan Beulich) - remove not needed cast (Jan Beulich) --- include/elf.h | 300 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 300 insertions(+) create mode 100644 include/elf.h diff --git a/include/elf.h b/include/elf.h new file mode 100644 index 00000000..fb3cae08 --- /dev/null +++ b/include/elf.h @@ -0,0 +1,300 @@ +#ifndef __ELF_H__ +#define __ELF_H__ +/* + * Copyright (c) 1995, 1996 Erik Theisen. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTI= ES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF US= E, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include + +typedef uint32_t Elf32_Addr; /* Unsigned program address */ +typedef uint32_t Elf32_Off; /* Unsigned file offset */ +typedef uint16_t Elf32_Half; /* Unsigned medium integer */ +typedef uint32_t Elf32_Word; /* Unsigned large integer */ + +typedef uint64_t Elf64_Addr; +typedef uint64_t Elf64_Off; +typedef uint16_t Elf64_Half; +typedef uint32_t Elf64_Word; +typedef uint64_t Elf64_Xword; + +/* Unique build id string format when using --build-id. */ +#define NT_GNU_BUILD_ID 3 + +/* + * e_ident[] identification indexes + * See http://www.caldera.com/developers/gabi/2000-07-17/ch4.eheader.html + */ +#define EI_MAG0 0 /* file ID */ +#define EI_MAG1 1 /* file ID */ +#define EI_MAG2 2 /* file ID */ +#define EI_MAG3 3 /* file ID */ +#define EI_CLASS 4 /* file class */ +#define EI_DATA 5 /* data encoding */ +#define EI_VERSION 6 /* ELF header version */ +#define EI_OSABI 7 /* OS/ABI ID */ +#define EI_ABIVERSION 8 /* ABI version */ +#define EI_PAD 9 /* start of pad bytes */ +#define EI_NIDENT 16 /* Size of e_ident[] */ + +/* e_ident[] magic number */ +#define ELFMAG0 0x7f /* e_ident[EI_MAG0] */ +#define ELFMAG1 'E' /* e_ident[EI_MAG1] */ +#define ELFMAG2 'L' /* e_ident[EI_MAG2] */ +#define ELFMAG3 'F' /* e_ident[EI_MAG3] */ +#define ELFMAG "\177ELF" /* magic */ +#define SELFMAG 4 /* size of magic */ + +/* e_ident[] file class */ +#define ELFCLASSNONE 0 /* invalid */ +#define ELFCLASS32 1 /* 32-bit objs */ +#define ELFCLASS64 2 /* 64-bit objs */ +#define ELFCLASSNUM 3 /* number of classes */ + +/* e_ident[] data encoding */ +#define ELFDATANONE 0 /* invalid */ +#define ELFDATA2LSB 1 /* Little-Endian */ +#define ELFDATA2MSB 2 /* Big-Endian */ +#define ELFDATANUM 3 /* number of data encode defines */ + +/* e_ident */ +#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] =3D=3D ELFMAG0 && \ + (ehdr).e_ident[EI_MAG1] =3D=3D ELFMAG1 && \ + (ehdr).e_ident[EI_MAG2] =3D=3D ELFMAG2 && \ + (ehdr).e_ident[EI_MAG3] =3D=3D ELFMAG3) + +/* e_flags */ +#define EF_ARM_EABI_MASK 0xff000000 +#define EF_ARM_EABI_UNKNOWN 0x00000000 +#define EF_ARM_EABI_VER1 0x01000000 +#define EF_ARM_EABI_VER2 0x02000000 +#define EF_ARM_EABI_VER3 0x03000000 +#define EF_ARM_EABI_VER4 0x04000000 +#define EF_ARM_EABI_VER5 0x05000000 + +/* ELF Header */ +typedef struct { + unsigned char e_ident[EI_NIDENT]; /* ELF Identification */ + Elf32_Half e_type; /* object file type */ + Elf32_Half e_machine; /* machine */ + Elf32_Word e_version; /* object file version */ + Elf32_Addr e_entry; /* virtual entry point */ + Elf32_Off e_phoff; /* program header table offset */ + Elf32_Off e_shoff; /* section header table offset */ + Elf32_Word e_flags; /* processor-specific flags */ + Elf32_Half e_ehsize; /* ELF header size */ + Elf32_Half e_phentsize; /* program header entry size */ + Elf32_Half e_phnum; /* number of program header entries */ + Elf32_Half e_shentsize; /* section header entry size */ + Elf32_Half e_shnum; /* number of section header entries */ + Elf32_Half e_shstrndx; /* section header table's "section + header string table" entry offset */ +} Elf32_Ehdr; + +typedef struct { + unsigned char e_ident[EI_NIDENT]; /* Id bytes */ + Elf64_Half e_type; /* file type */ + Elf64_Half e_machine; /* machine type */ + Elf64_Word e_version; /* version number */ + Elf64_Addr e_entry; /* entry point */ + Elf64_Off e_phoff; /* Program hdr offset */ + Elf64_Off e_shoff; /* Section hdr offset */ + Elf64_Word e_flags; /* Processor flags */ + Elf64_Half e_ehsize; /* sizeof ehdr */ + Elf64_Half e_phentsize; /* Program header entry size */ + Elf64_Half e_phnum; /* Number of program headers */ + Elf64_Half e_shentsize; /* Section header entry size */ + Elf64_Half e_shnum; /* Number of section headers */ + Elf64_Half e_shstrndx; /* String table index */ +} Elf64_Ehdr; + +/* e_type */ +#define ET_NONE 0 /* No file type */ +#define ET_REL 1 /* relocatable file */ +#define ET_EXEC 2 /* executable file */ +#define ET_DYN 3 /* shared object file */ +#define ET_CORE 4 /* core file */ +#define ET_NUM 5 /* number of types */ +#define ET_LOPROC 0xff00 /* reserved range for processor */ +#define ET_HIPROC 0xffff /* specific e_type */ + +/* e_machine */ +#define EM_NONE 0 /* No Machine */ +#define EM_386 3 /* Intel 80386 */ +#define EM_PPC64 21 /* PowerPC 64-bit */ +#define EM_ARM 40 /* Advanced RISC Machines ARM */ +#define EM_X86_64 62 /* AMD x86-64 architecture */ +#define EM_AARCH64 183 /* ARM 64-bit */ + +/* Version */ +#define EV_NONE 0 /* Invalid */ +#define EV_CURRENT 1 /* Current */ +#define EV_NUM 2 /* number of versions */ + +/* Program Header */ +typedef struct { + Elf32_Word p_type; /* segment type */ + Elf32_Off p_offset; /* segment offset */ + Elf32_Addr p_vaddr; /* virtual address of segment */ + Elf32_Addr p_paddr; /* physical address - ignored? */ + Elf32_Word p_filesz; /* number of bytes in file for seg. */ + Elf32_Word p_memsz; /* number of bytes in mem. for seg. */ + Elf32_Word p_flags; /* flags */ + Elf32_Word p_align; /* memory alignment */ +} Elf32_Phdr; + +typedef struct { + Elf64_Word p_type; /* entry type */ + Elf64_Word p_flags; /* flags */ + Elf64_Off p_offset; /* offset */ + Elf64_Addr p_vaddr; /* virtual address */ + Elf64_Addr p_paddr; /* physical address */ + Elf64_Xword p_filesz; /* file size */ + Elf64_Xword p_memsz; /* memory size */ + Elf64_Xword p_align; /* memory & file alignment */ +} Elf64_Phdr; + +/* Segment types - p_type */ +#define PT_NULL 0 /* unused */ +#define PT_LOAD 1 /* loadable segment */ +#define PT_DYNAMIC 2 /* dynamic linking section */ +#define PT_INTERP 3 /* the RTLD */ +#define PT_NOTE 4 /* auxiliary information */ +#define PT_SHLIB 5 /* reserved - purpose undefined */ +#define PT_PHDR 6 /* program header */ +#define PT_NUM 7 /* Number of segment types */ +#define PT_LOPROC 0x70000000 /* reserved range for processor */ +#define PT_HIPROC 0x7fffffff /* specific segment types */ + +/* Segment flags - p_flags */ +#define PF_X 0x1 /* Executable */ +#define PF_W 0x2 /* Writable */ +#define PF_R 0x4 /* Readable */ +#define PF_MASKPROC 0xf0000000 /* reserved bits for processor */ + /* specific segment flags */ + +/* Section Header */ +typedef struct { + Elf32_Word sh_name; /* name - index into section header + string table section */ + Elf32_Word sh_type; /* type */ + Elf32_Word sh_flags; /* flags */ + Elf32_Addr sh_addr; /* address */ + Elf32_Off sh_offset; /* file offset */ + Elf32_Word sh_size; /* section size */ + Elf32_Word sh_link; /* section header table index link */ + Elf32_Word sh_info; /* extra information */ + Elf32_Word sh_addralign; /* address alignment */ + Elf32_Word sh_entsize; /* section entry size */ +} Elf32_Shdr; + +typedef struct { + Elf64_Word sh_name; /* section name */ + Elf64_Word sh_type; /* section type */ + Elf64_Xword sh_flags; /* section flags */ + Elf64_Addr sh_addr; /* virtual address */ + Elf64_Off sh_offset; /* file offset */ + Elf64_Xword sh_size; /* section size */ + Elf64_Word sh_link; /* link to another */ + Elf64_Word sh_info; /* misc info */ + Elf64_Xword sh_addralign; /* memory alignment */ + Elf64_Xword sh_entsize; /* table entry size */ +} Elf64_Shdr; + +/* sh_type */ +#define SHT_NULL 0 /* inactive */ +#define SHT_PROGBITS 1 /* program defined information */ +#define SHT_SYMTAB 2 /* symbol table section */ +#define SHT_STRTAB 3 /* string table section */ +#define SHT_RELA 4 /* relocation section with addends*/ +#define SHT_HASH 5 /* symbol hash table section */ +#define SHT_DYNAMIC 6 /* dynamic section */ +#define SHT_NOTE 7 /* note section */ +#define SHT_NOBITS 8 /* no space section */ +#define SHT_REL 9 /* relation section without addends */ +#define SHT_SHLIB 10 /* reserved - purpose unknown */ +#define SHT_DYNSYM 11 /* dynamic symbol table section */ +#define SHT_NUM 12 /* number of section types */ + +/* Note definitions */ +typedef struct { + Elf32_Word namesz; + Elf32_Word descsz; + Elf32_Word type; + char data[]; +} Elf32_Note; + +typedef struct { + Elf64_Word namesz; + Elf64_Word descsz; + Elf64_Word type; + char data[]; +} Elf64_Note; + +/* Abstraction layer for handling 32- and 64-bit ELF files. */ + +typedef union { + Elf32_Ehdr e32; + Elf64_Ehdr e64; +} elf_ehdr; + +static inline bool elf_is_32bit(elf_ehdr *ehdr) +{ + return ehdr->e32.e_ident[EI_CLASS] =3D=3D ELFCLASS32; +} + +static inline bool elf_is_64bit(elf_ehdr *ehdr) +{ + return ehdr->e32.e_ident[EI_CLASS] =3D=3D ELFCLASS64; +} + +#define ehdr_val(ehdr, elem) (elf_is_32bit(ehdr) ? (ehdr)->e32.elem : (ehd= r)->e64.elem) + +typedef union { + Elf32_Phdr e32; + Elf64_Phdr e64; +} elf_phdr; + +#define phdr_val(ehdr, phdr, elem) (elf_is_32bit(ehdr) ? (phdr)->e32.elem = : (phdr)->e64.elem) + +typedef union { + Elf32_Shdr e32; + Elf64_Shdr e64; +} elf_shdr; + +#define shdr_val(ehdr, shdr, elem) (elf_is_32bit(ehdr) ? (shdr)->e32.elem = : (shdr)->e64.elem) + +typedef union { + Elf32_Note e32; + Elf64_Note e64; +} elf_note; + +#define note_val(ehdr, note, elem) (elf_is_32bit(ehdr) ? (note)->e32.elem = : (note)->e64.elem) + +static inline void *elf_ptr_add(void *ptr, unsigned long add) +{ + return ptr + add; +} +#endif /* __ELF_H__ */ --=20 2.43.0 From nobody Fri Oct 31 03:42:49 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1750063620; cv=none; d=zohomail.com; s=zohoarc; b=Df2ZAezwkKFmVYxtSqZqel9zvNXmsYws7/6KKUMw+0RkTRLQ47hCp6dZwgUKlXNaH6b7PeDRULGz8wQuFEFr9i+JmocmNlqdrmUFkXmFwErOySgkxTTEfLxkPgjxWDCi5hvY9I8zucl/mKNqA0FSDqpvyiGlKdu/uG3o7PtfGl8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750063620; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=ErW9+9DRpMiP5o75sVek1KZHEMoGAiJ0VRopG8PxS3M=; b=T92/6oMKq+C7dLeclDPuTbHlUdc99QJg7/uJEe40CK7v4YhwoBBHCxEgsq6H2E6IuGyOE/BDMdrvrQY4s1CuqVfx+Bx3wJEs1NwNZNTObjVfd1nXD1l59slwU7ANd2jLxVHfmidiFR0cSe2oTwh1Cct+RutR7zGVrmByXrx74gQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1750063619995475.5177797256049; Mon, 16 Jun 2025 01:46:59 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1016892.1393899 (Exim 4.92) (envelope-from ) id 1uR5UM-0000Tg-1t; Mon, 16 Jun 2025 08:46:46 +0000 Received: by outflank-mailman (output) from mailman id 1016892.1393899; Mon, 16 Jun 2025 08:46:46 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5UL-0000TX-UO; Mon, 16 Jun 2025 08:46:45 +0000 Received: by outflank-mailman (input) for mailman id 1016892; Mon, 16 Jun 2025 08:46:44 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5UK-0007jR-Rb for xen-devel@lists.xenproject.org; Mon, 16 Jun 2025 08:46:44 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 6e1ca925-4a8e-11f0-a309-13f23c93f187; Mon, 16 Jun 2025 10:46:44 +0200 (CEST) Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (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 smtp-out2.suse.de (Postfix) with ESMTPS id F08491F387; Mon, 16 Jun 2025 08:46:43 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id BED63139E2; Mon, 16 Jun 2025 08:46:43 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id oTULLfPZT2gFQAAAD6G6ig (envelope-from ); Mon, 16 Jun 2025 08:46:43 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 6e1ca925-4a8e-11f0-a309-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063604; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ErW9+9DRpMiP5o75sVek1KZHEMoGAiJ0VRopG8PxS3M=; b=SWoQN4zlxWLi8ZKq2jWhfuB4yjsy5mSYgvK3YyZ+h+/nexswTQbjMgSPbXt4/KWLwkDezK jvqstgion871pR+Jm9cej3VnHV9VRBGVNo0DSteKAOKFcYurLAVWL8B15KLL5oAq/0IIYD LM+qRQ2xjl51xB4xBIoReu/zkvmuNYw= Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063603; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ErW9+9DRpMiP5o75sVek1KZHEMoGAiJ0VRopG8PxS3M=; b=L/kjDblt1jRnE052nkLszKRxliRvJ4gQPpfqz0FQLZXZfJhtE40x+6oOtiAm+V6YsybFOp A+ls5LBf9wwwhM/33nz6IIfMONohLY5zY4/O6UPK1/+ti9+n+DQWaSPTcXb/Pz+kaYYLUZ fbCK8t/iepaP61IcUCpvwV3LV+aTuv8= From: Juergen Gross To: minios-devel@lists.xenproject.org, xen-devel@lists.xenproject.org Cc: samuel.thibault@ens-lyon.org, Juergen Gross , Jason Andryuk Subject: [MINI-OS PATCH v2 04/12] mini-os: analyze new kernel for kexec Date: Mon, 16 Jun 2025 10:46:11 +0200 Message-ID: <20250616084619.11112-5-jgross@suse.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250616084619.11112-1-jgross@suse.com> References: <20250616084619.11112-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-0.999]; MIME_GOOD(-0.10)[text/plain]; ARC_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; TO_DN_SOME(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; FUZZY_BLOCKED(0.00)[rspamd.com]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_HAS_DN(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCVD_TLS_ALL(0.00)[]; RCPT_COUNT_FIVE(0.00)[5]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:email,suse.com:mid,imap1.dmz-prg2.suse.org:helo,amd.com:email] X-Spam-Level: X-Spam-Flag: NO X-Spam-Score: -2.80 X-ZohoMail-DKIM: pass (identity @suse.com) (identity @suse.com) X-ZM-MESSAGEID: 1750063620902116600 Content-Type: text/plain; charset="utf-8" Analyze the properties of the new kernel to be loaded by kexec. The data needed is: - upper boundary in final location - copy and memory clear operations - entry point and entry parameter Signed-off-by: Juergen Gross Reviewed-by: Jason Andryuk --- V2: - rename check_notes_entry() (Jason Andryuk) - rename kernel_entry to kernel_phys_entry (Jason Andryuk) --- arch/x86/kexec.c | 92 +++++++++++++++++++++++++++++++++++++ include/kexec.h | 11 +++++ kexec.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 220 insertions(+) diff --git a/arch/x86/kexec.c b/arch/x86/kexec.c index a8a2fa97..736ca1bf 100644 --- a/arch/x86/kexec.c +++ b/arch/x86/kexec.c @@ -28,8 +28,15 @@ =20 #include #include +#include +#include #include =20 +#include +#include + +static unsigned long kernel_phys_entry =3D ~0UL; + /* * Final stage of kexec. Copies all data to the final destinations, zeroes * .bss and activates new kernel. @@ -104,4 +111,89 @@ void do_kexec(void *kexec_page) : :"m" (stack), "m" (actions), "m" (phys), "m" (final)); } =20 +bool kexec_chk_arch(elf_ehdr *ehdr) +{ + return ehdr->e32.e_machine =3D=3D EM_386 || ehdr->e32.e_machine =3D=3D= EM_X86_64; +} + +static unsigned int note_data_sz(unsigned int sz) +{ + return (sz + 3) & ~3; +} + +static void read_note_entry(elf_ehdr *ehdr, void *start, unsigned int len) +{ + elf_note *note =3D start; + unsigned int off, note_len, namesz, descsz; + char *val; + + for ( off =3D 0; off < len; off +=3D note_len ) + { + namesz =3D note_data_sz(note_val(ehdr, note, namesz)); + descsz =3D note_data_sz(note_val(ehdr, note, descsz)); + val =3D note_val(ehdr, note, data); + note_len =3D val - (char *)note + namesz + descsz; + + if ( !strncmp(val, "Xen", namesz) && + note_val(ehdr, note, type) =3D=3D XEN_ELFNOTE_PHYS32_ENTRY ) + { + val +=3D namesz; + switch ( note_val(ehdr, note, descsz) ) + { + case 1: + kernel_phys_entry =3D *(uint8_t *)val; + return; + case 2: + kernel_phys_entry =3D *(uint16_t *)val; + return; + case 4: + kernel_phys_entry =3D *(uint32_t *)val; + return; + case 8: + kernel_phys_entry =3D *(uint64_t *)val; + return; + default: + break; + } + } + + note =3D elf_ptr_add(note, note_len); + } +} + +int kexec_arch_analyze_phdr(elf_ehdr *ehdr, elf_phdr *phdr) +{ + void *notes_start; + unsigned int notes_len; + + if ( phdr_val(ehdr, phdr, p_type) !=3D PT_NOTE || kernel_phys_entry != =3D ~0UL ) + return 0; + + notes_start =3D elf_ptr_add(ehdr, phdr_val(ehdr, phdr, p_offset)); + notes_len =3D phdr_val(ehdr, phdr, p_filesz); + read_note_entry(ehdr, notes_start, notes_len); + + return 0; +} + +int kexec_arch_analyze_shdr(elf_ehdr *ehdr, elf_shdr *shdr) +{ + void *notes_start; + unsigned int notes_len; + + if ( shdr_val(ehdr, shdr, sh_type) !=3D SHT_NOTE || + kernel_phys_entry !=3D ~0UL ) + return 0; + + notes_start =3D elf_ptr_add(ehdr, shdr_val(ehdr, shdr, sh_offset)); + notes_len =3D shdr_val(ehdr, shdr, sh_size); + read_note_entry(ehdr, notes_start, notes_len); + + return 0; +} + +bool kexec_arch_need_analyze_shdrs(void) +{ + return kernel_phys_entry =3D=3D ~0UL; +} #endif /* CONFIG_KEXEC */ diff --git a/include/kexec.h b/include/kexec.h index 722be456..f54cbb90 100644 --- a/include/kexec.h +++ b/include/kexec.h @@ -1,5 +1,6 @@ #ifndef _KEXEC_H #define _KEXEC_H +#include =20 /* One element of kexec actions (last element must have action KEXEC_CALL)= : */ struct kexec_action { @@ -18,6 +19,8 @@ struct kexec_action { extern char _kexec_start[], _kexec_end[]; extern struct kexec_action kexec_actions[KEXEC_MAX_ACTIONS]; =20 +extern unsigned long kexec_last_addr; + int kexec_add_action(int action, void *dest, void *src, unsigned int len); =20 #define KEXEC_SECSIZE ((unsigned long)_kexec_end - (unsigned long)_kexec_s= tart) @@ -31,4 +34,12 @@ void do_kexec(void *kexec_page); /* Assembler code for switching off paging and passing execution to new OS= . */ void kexec_phys(void); =20 +/* Check kernel to match current architecture. */ +bool kexec_chk_arch(elf_ehdr *ehdr); + +/* Architecture specific ELF handling functions. */ +int kexec_arch_analyze_phdr(elf_ehdr *ehdr, elf_phdr *phdr); +int kexec_arch_analyze_shdr(elf_ehdr *ehdr, elf_shdr *shdr); +bool kexec_arch_need_analyze_shdrs(void); + #endif /* _KEXEC_H */ diff --git a/kexec.c b/kexec.c index b69e145b..7c0e6377 100644 --- a/kexec.c +++ b/kexec.c @@ -31,6 +31,9 @@ #include #include #include +#include +#include +#include #include =20 /* @@ -53,8 +56,122 @@ * - The new kernel is activated. */ =20 +unsigned long kexec_last_addr; + +static int analyze_phdrs(elf_ehdr *ehdr) +{ + elf_phdr *phdr; + unsigned int n_hdr, i; + unsigned long paddr, offset, filesz, memsz; + int ret; + + phdr =3D elf_ptr_add(ehdr, ehdr_val(ehdr, e_phoff)); + n_hdr =3D ehdr_val(ehdr, e_phnum); + for ( i =3D 0; i < n_hdr; i++ ) + { + ret =3D kexec_arch_analyze_phdr(ehdr, phdr); + if ( ret ) + return ret; + + if ( phdr_val(ehdr, phdr, p_type) =3D=3D PT_LOAD && + (phdr_val(ehdr, phdr, p_flags) & (PF_X | PF_W | PF_R)) ) + { + paddr =3D phdr_val(ehdr, phdr, p_paddr); + offset =3D phdr_val(ehdr, phdr, p_offset); + filesz =3D phdr_val(ehdr, phdr, p_filesz); + memsz =3D phdr_val(ehdr, phdr, p_memsz); + if ( filesz > 0 ) + { + ret =3D kexec_add_action(KEXEC_COPY, to_virt(paddr), + (char *)ehdr + offset, filesz); + if ( ret ) + return ret; + } + if ( memsz > filesz ) + { + ret =3D kexec_add_action(KEXEC_ZERO, to_virt(paddr + files= z), + NULL, memsz - filesz); + if ( ret ) + return ret; + } + if ( paddr + memsz > kexec_last_addr ) + kexec_last_addr =3D paddr + memsz; + } + + phdr =3D elf_ptr_add(phdr, ehdr_val(ehdr, e_phentsize)); + } + + return 0; +} + +static int analyze_shdrs(elf_ehdr *ehdr) +{ + elf_shdr *shdr; + unsigned int n_hdr, i; + int ret; + + if ( !kexec_arch_need_analyze_shdrs() ) + return 0; + + shdr =3D elf_ptr_add(ehdr, ehdr_val(ehdr, e_shoff)); + n_hdr =3D ehdr_val(ehdr, e_shnum); + for ( i =3D 0; i < n_hdr; i++ ) + { + ret =3D kexec_arch_analyze_shdr(ehdr, shdr); + if ( ret ) + return ret; + + shdr =3D elf_ptr_add(shdr, ehdr_val(ehdr, e_shentsize)); + } + + return 0; +} + +static int analyze_kernel(void *kernel, unsigned long size) +{ + elf_ehdr *ehdr =3D kernel; + int ret; + + if ( !IS_ELF(ehdr->e32) ) + { + printk("kexec: new kernel not an ELF file\n"); + return ENOEXEC; + } + if ( ehdr->e32.e_ident[EI_DATA] !=3D ELFDATA2LSB ) + { + printk("kexec: ELF file of new kernel is big endian\n"); + return ENOEXEC; + } + if ( !elf_is_32bit(ehdr) && !elf_is_64bit(ehdr) ) + { + printk("kexec: ELF file of new kernel is neither 32 nor 64 bit\n"); + return ENOEXEC; + } + if ( !kexec_chk_arch(ehdr) ) + { + printk("kexec: ELF file of new kernel is not compatible with arch\= n"); + return ENOEXEC; + } + + ret =3D analyze_phdrs(ehdr); + if ( ret ) + return ret; + + ret =3D analyze_shdrs(ehdr); + if ( ret ) + return ret; + + return 0; +} + int kexec(void *kernel, unsigned long kernel_size, const char *cmdline) { + int ret; + + ret =3D analyze_kernel(kernel, kernel_size); + if ( ret ) + return ret; + return ENOSYS; } EXPORT_SYMBOL(kexec); --=20 2.43.0 From nobody Fri Oct 31 03:42:49 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1750063630; cv=none; d=zohomail.com; s=zohoarc; b=UPJaaEap9bhpEF2ICdtl3Q0byf08tT3jxccKjAHl3dNmz6eM2wt0KVPqrEPkcuUQiQXmkUqvF4H86SMjqu9aB7mV31TDDtObqz+gJNz8/zimGVTpYx4rkNwfLqODFvqShKb3KhSR0MObkgAlwe6ENSiieWQrOh4OTwMxV5mUogo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750063630; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Har4IwtPYyYJZu4D6gPZgpdZMuGaRz2PoPDisbs0FwU=; b=h8uqryTXQ3Ri7Gsbz2knXDqXkxcSb838B4b2OxE5r6TnZzYvUM4KL3kYT3RdCUrgis4YeGL1xGXH+9bSvt7fCax8T2Xfik7AKkR3QZlxwlrZkRRRfyaOD4v/t29Lhz5R3Ed2enGFLzAVXKUZ/J2GkN8rjo5ZnRz1YqXZxkIwwMA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1750063630594121.99893165119488; Mon, 16 Jun 2025 01:47:10 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1016904.1393917 (Exim 4.92) (envelope-from ) id 1uR5UU-00019m-Fq; Mon, 16 Jun 2025 08:46:54 +0000 Received: by outflank-mailman (output) from mailman id 1016904.1393917; Mon, 16 Jun 2025 08:46:54 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5UU-00019c-Ae; Mon, 16 Jun 2025 08:46:54 +0000 Received: by outflank-mailman (input) for mailman id 1016904; Mon, 16 Jun 2025 08:46:52 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5US-0007hV-FO for xen-devel@lists.xenproject.org; Mon, 16 Jun 2025 08:46:52 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [2a07:de40:b251:101:10:150:64:1]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 717beb00-4a8e-11f0-b894-0df219b8e170; Mon, 16 Jun 2025 10:46:50 +0200 (CEST) Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (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 smtp-out1.suse.de (Postfix) with ESMTPS id A853821203; Mon, 16 Jun 2025 08:46:49 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 6FD76139E2; Mon, 16 Jun 2025 08:46:49 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id Q1epGfnZT2gPQAAAD6G6ig (envelope-from ); Mon, 16 Jun 2025 08:46:49 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 717beb00-4a8e-11f0-b894-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063609; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Har4IwtPYyYJZu4D6gPZgpdZMuGaRz2PoPDisbs0FwU=; b=R83igbw1tj3VZ1xHwp86+zoOf0BMddI5D3JZHdZeQTL9irr1tyP2PmLv+pjUfRSF/FhnSE U1w7DJTaaiZJCk1CKBQYvYrzGDVxz/Ij32KRmJWHwvGtDa7lxZVHloBAatywys8KVMCzES hRF8oHhdtnxhXPX64tfCoFA/+9Ul5n0= Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063609; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Har4IwtPYyYJZu4D6gPZgpdZMuGaRz2PoPDisbs0FwU=; b=R83igbw1tj3VZ1xHwp86+zoOf0BMddI5D3JZHdZeQTL9irr1tyP2PmLv+pjUfRSF/FhnSE U1w7DJTaaiZJCk1CKBQYvYrzGDVxz/Ij32KRmJWHwvGtDa7lxZVHloBAatywys8KVMCzES hRF8oHhdtnxhXPX64tfCoFA/+9Ul5n0= From: Juergen Gross To: minios-devel@lists.xenproject.org, xen-devel@lists.xenproject.org Cc: samuel.thibault@ens-lyon.org, Juergen Gross , Jason Andryuk Subject: [MINI-OS PATCH v2 05/12] mini-os: kexec: finalize parameter location and size Date: Mon, 16 Jun 2025 10:46:12 +0200 Message-ID: <20250616084619.11112-6-jgross@suse.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250616084619.11112-1-jgross@suse.com> References: <20250616084619.11112-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-0.999]; MIME_GOOD(-0.10)[text/plain]; MIME_TRACE(0.00)[0:+]; TO_DN_SOME(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; ARC_NA(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_FIVE(0.00)[5]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[amd.com:email,imap1.dmz-prg2.suse.org:helo,suse.com:email,suse.com:mid]; RCVD_TLS_ALL(0.00)[] X-Spam-Level: X-Spam-Flag: NO X-Spam-Score: -2.80 X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1750063632655116600 Content-Type: text/plain; charset="utf-8" Finalize the location and the size of the parameters for the new kernel. This is needed in order to avoid allocating new memory in the area occupied by the new kernel and parameters. Signed-off-by: Juergen Gross Reviewed-by: Jason Andryuk --- arch/x86/kexec.c | 15 +++++++++++++++ include/kexec.h | 3 +++ kexec.c | 2 ++ 3 files changed, 20 insertions(+) diff --git a/arch/x86/kexec.c b/arch/x86/kexec.c index 736ca1bf..47830905 100644 --- a/arch/x86/kexec.c +++ b/arch/x86/kexec.c @@ -196,4 +196,19 @@ bool kexec_arch_need_analyze_shdrs(void) { return kernel_phys_entry =3D=3D ~0UL; } + +static unsigned long kexec_param_loc; +static unsigned int kexec_param_size; + +void kexec_set_param_loc(const char *cmdline) +{ + kexec_param_size =3D sizeof(struct hvm_start_info); + kexec_param_size +=3D e820_entries * sizeof(struct hvm_memmap_table_en= try); + kexec_param_size +=3D strlen(cmdline) + 1; + + kexec_last_addr =3D (kexec_last_addr + 7) & ~7UL; + kexec_param_loc =3D kexec_last_addr; + kexec_last_addr +=3D kexec_param_size; + kexec_last_addr =3D round_pgup(kexec_last_addr); +} #endif /* CONFIG_KEXEC */ diff --git a/include/kexec.h b/include/kexec.h index f54cbb90..8a2b552f 100644 --- a/include/kexec.h +++ b/include/kexec.h @@ -42,4 +42,7 @@ int kexec_arch_analyze_phdr(elf_ehdr *ehdr, elf_phdr *phd= r); int kexec_arch_analyze_shdr(elf_ehdr *ehdr, elf_shdr *shdr); bool kexec_arch_need_analyze_shdrs(void); =20 +/* Finalize parameter location and size. */ +void kexec_set_param_loc(const char *cmdline); + #endif /* _KEXEC_H */ diff --git a/kexec.c b/kexec.c index 7c0e6377..31839382 100644 --- a/kexec.c +++ b/kexec.c @@ -172,6 +172,8 @@ int kexec(void *kernel, unsigned long kernel_size, cons= t char *cmdline) if ( ret ) return ret; =20 + kexec_set_param_loc(cmdline); + return ENOSYS; } EXPORT_SYMBOL(kexec); --=20 2.43.0 From nobody Fri Oct 31 03:42:49 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1750063866; cv=none; d=zohomail.com; s=zohoarc; b=a/gO5k6OebmYIHtIdtnZ+uzyyrZMVY+UEKjs5lMKyLTqPgejEFKA+5rVyLJ4gC+M0Hk6cOroYqLyxoCyacq30shbvXuumwjzZI24Myv7yzFdCtrAWOmPvL9ecXHgPkG1IHycAeHKwC/AqYI4Kc/7VG0rVitTfE+N6BMPIFaZ1fs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750063866; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=t6vErAErnWD/FpLTzRhfE8JqNkLUXRMZRtPqHFEP1P8=; b=ZzdkOedZoEra/PE5eq244ydlFHUlrE5Z/i+Yv5X9Kf7b3iRr1221Onubw4AFceHhlVIEA6lNRQbF7aRgKJeOwu5fQUoUDNdT5btb3vTfJWUajNYTGTETe09LUzdc5DR0t/fCDpQCp8hh3I65g5VAbub666t29T0L06s9GO8Bg6U= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1750063866975661.9227656020098; Mon, 16 Jun 2025 01:51:06 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1016954.1393965 (Exim 4.92) (envelope-from ) id 1uR5YF-0004jt-67; Mon, 16 Jun 2025 08:50:47 +0000 Received: by outflank-mailman (output) from mailman id 1016954.1393965; Mon, 16 Jun 2025 08:50:47 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5YF-0004jU-0w; Mon, 16 Jun 2025 08:50:47 +0000 Received: by outflank-mailman (input) for mailman id 1016954; Mon, 16 Jun 2025 08:50:45 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5UX-0007jR-8c for xen-devel@lists.xenproject.org; Mon, 16 Jun 2025 08:46:57 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [2a07:de40:b251:101:10:150:64:2]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 74d926bb-4a8e-11f0-a309-13f23c93f187; Mon, 16 Jun 2025 10:46:55 +0200 (CEST) Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (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 smtp-out2.suse.de (Postfix) with ESMTPS id 44EB41F387; Mon, 16 Jun 2025 08:46:55 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 16AD3139E2; Mon, 16 Jun 2025 08:46:55 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id yj0oBP/ZT2gXQAAAD6G6ig (envelope-from ); Mon, 16 Jun 2025 08:46:55 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 74d926bb-4a8e-11f0-a309-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063615; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=t6vErAErnWD/FpLTzRhfE8JqNkLUXRMZRtPqHFEP1P8=; b=PHX2IwrcZ/YS5hTfN4yhU5/SXV3c3swS8eEhIhSMQZqnhRtqk9mZy8zfw2KirRigVGNjG+ 8wFtDb/QDlCghYPtOOd9KgwCx1CoXsPR214SgBevfEnowA9fHu+tVL2of+br5uMXGjlWTa H3aP2zAp1uGsrnSpfq0FFBfNApMf4Fo= Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063615; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=t6vErAErnWD/FpLTzRhfE8JqNkLUXRMZRtPqHFEP1P8=; b=PHX2IwrcZ/YS5hTfN4yhU5/SXV3c3swS8eEhIhSMQZqnhRtqk9mZy8zfw2KirRigVGNjG+ 8wFtDb/QDlCghYPtOOd9KgwCx1CoXsPR214SgBevfEnowA9fHu+tVL2of+br5uMXGjlWTa H3aP2zAp1uGsrnSpfq0FFBfNApMf4Fo= From: Juergen Gross To: minios-devel@lists.xenproject.org, xen-devel@lists.xenproject.org Cc: samuel.thibault@ens-lyon.org, Juergen Gross , Jason Andryuk Subject: [MINI-OS PATCH v2 06/12] mini-os: reserve memory below boundary Date: Mon, 16 Jun 2025 10:46:13 +0200 Message-ID: <20250616084619.11112-7-jgross@suse.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250616084619.11112-1-jgross@suse.com> References: <20250616084619.11112-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-0.999]; MIME_GOOD(-0.10)[text/plain]; ARC_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; TO_DN_SOME(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; FUZZY_BLOCKED(0.00)[rspamd.com]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_HAS_DN(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCVD_TLS_ALL(0.00)[]; RCPT_COUNT_FIVE(0.00)[5]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:email,suse.com:mid,amd.com:email,imap1.dmz-prg2.suse.org:helo] X-Spam-Level: X-Spam-Flag: NO X-Spam-Score: -2.80 X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1750063867873116600 Content-Type: text/plain; charset="utf-8" In order to support kexec any memory used during copying the new kernel to its final destination must not overlap with the destination area. In order to achieve that add a new interface allowing to mark all allocatable memory below a specific physical address as not available. This is done by walking through all chunks of the buddy allocator and removing the chunks (or chunk parts) below the boundary. The removed chunks are put into a list in order to be able to undo the operation in case kexec is failing before doing any unrecoverable system modifications. Any pages freed located below the boundary need to go directly into the list of reserved pages instead of the free pool. Call the new function from kexec code. Signed-off-by: Juergen Gross Reviewed-by: Jason Andryuk --- include/mm.h | 5 +++ kexec.c | 5 +++ mm.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 98 insertions(+), 1 deletion(-) diff --git a/include/mm.h b/include/mm.h index 4fc364ff..5775c3e1 100644 --- a/include/mm.h +++ b/include/mm.h @@ -57,6 +57,11 @@ unsigned long alloc_pages(int order); void free_pages(void *pointer, int order); #define free_page(p) free_pages(p, 0) =20 +#ifdef CONFIG_KEXEC +void reserve_memory_below(unsigned long boundary); +void unreserve_memory_below(void); +#endif + static __inline__ int get_order(unsigned long size) { int order; diff --git a/kexec.c b/kexec.c index 31839382..1a90d8ab 100644 --- a/kexec.c +++ b/kexec.c @@ -174,6 +174,11 @@ int kexec(void *kernel, unsigned long kernel_size, con= st char *cmdline) =20 kexec_set_param_loc(cmdline); =20 + reserve_memory_below(kexec_last_addr); + + /* Error exit. */ + unreserve_memory_below(); + return ENOSYS; } EXPORT_SYMBOL(kexec); diff --git a/mm.c b/mm.c index a5d3f5e5..9236db58 100644 --- a/mm.c +++ b/mm.c @@ -230,6 +230,84 @@ static void init_page_allocator(unsigned long min, uns= igned long max) mm_alloc_bitmap_remap(); } =20 +#ifdef CONFIG_KEXEC +static chunk_head_t *reserved_chunks; +static unsigned long boundary_pfn; + +static void free_pages_below(void *pointer, unsigned int order) +{ + unsigned long pfn_s, pfn; + chunk_head_t *ch =3D pointer; + + pfn_s =3D virt_to_pfn(ch); + + if ( pfn_s + (1UL << order) <=3D boundary_pfn ) + { + /* Put complete chunk into reserved list. */ + ch->level =3D order; + ch->next =3D reserved_chunks; + reserved_chunks =3D ch; + } + else + { + /* Put pages below boundary into reserved list. */ + for ( pfn =3D pfn_s; pfn < boundary_pfn; pfn++ ) + { + chunk_head_t *ch_res =3D pfn_to_virt(pfn); + + ch_res->level =3D 0; + ch_res->next =3D reserved_chunks; + reserved_chunks =3D ch_res; + } + + /* Return pages above boundary to free pool again. */ + for ( ; pfn < pfn_s + (1UL << order); pfn++ ) + free_pages(pfn_to_virt(pfn), 0); + } +} + +void reserve_memory_below(unsigned long boundary) +{ + unsigned long pfn; + unsigned int order; + chunk_head_t *ch; + + ASSERT(!boundary_pfn); + boundary_pfn =3D PHYS_PFN(boundary); + + for ( order =3D 0; order < FREELIST_SIZE; order++ ) + { + for ( ch =3D free_list[order].next; !FREELIST_EMPTY(ch); ch =3D ch= ->next ) + { + pfn =3D virt_to_pfn(ch); + if ( pfn >=3D boundary_pfn ) + continue; + + /* Dequeue from this level, at least parts will be reserved. */ + dequeue_elem(ch); + /* Mark all as allocated, pieces above boundary will be return= ed. */ + map_alloc(pfn, 1UL << ch->level); + + free_pages_below(ch, ch->level); + } + } +} + +void unreserve_memory_below(void) +{ + chunk_head_t *ch; + + boundary_pfn =3D 0; + + while ( reserved_chunks ) + { + ch =3D reserved_chunks; + reserved_chunks =3D ch->next; + free_pages(ch, ch->level); + } +} +#endif /* CONFIG_KEXEC */ + /* Allocate 2^@order contiguous pages. Returns a VIRTUAL address. */ unsigned long alloc_pages(int order) { @@ -279,10 +357,19 @@ EXPORT_SYMBOL(alloc_pages); void free_pages(void *pointer, int order) { chunk_head_t *freed_ch, *to_merge_ch; + unsigned long pfn =3D virt_to_pfn(pointer); unsigned long mask; =20 +#ifdef CONFIG_KEXEC + if ( pfn < boundary_pfn ) + { + free_pages_below(pointer, order); + return; + } +#endif + /* First free the chunk */ - map_free(virt_to_pfn(pointer), 1UL << order); + map_free(pfn, 1UL << order); =20 /* Create free chunk */ freed_ch =3D (chunk_head_t *)pointer; --=20 2.43.0 From nobody Fri Oct 31 03:42:49 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1750063867; cv=none; d=zohomail.com; s=zohoarc; b=H00IJOU5ZI36kY12SFDDrhncPYWLBNLmLZ0UYRe2mHnkGWrsbueGtqot8aPN1KQihOyryTRa+KVxeeIXFbtzQzAetVsy6LMHRJph1wQtFf695YzRB7+jxBiIZojt/lttrsioF0ssVAdNKEWG/DQYBZ0vVZO2KGM4RafxCLczDOU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750063867; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=fyowQBL0+KxczJ3QgQBuNM66VYPd6WBeOp+F4jmgKk4=; b=TiIMhaa18/xJPPQx8JJyTieNqJ9pX/qEEOQ6xyJpSUKLZv0VBzL/heJQwVeLWN0lKfbnfaCst50wOHJfCFcX3OU3s5aPTxwvhMIfxsJbfKMqmROp6ITxSORhix+4JQXpGJTNvW7AQybeTxwo8+vEJLq9hHg+IQL8ORKq+rGUIRU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 175006386721461.12244875974238; Mon, 16 Jun 2025 01:51:07 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1016973.1393991 (Exim 4.92) (envelope-from ) id 1uR5YK-0005QK-5H; Mon, 16 Jun 2025 08:50:52 +0000 Received: by outflank-mailman (output) from mailman id 1016973.1393991; Mon, 16 Jun 2025 08:50:52 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5YJ-0005P1-W7; Mon, 16 Jun 2025 08:50:51 +0000 Received: by outflank-mailman (input) for mailman id 1016973; Mon, 16 Jun 2025 08:50:50 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5Ub-0007jR-Qo for xen-devel@lists.xenproject.org; Mon, 16 Jun 2025 08:47:01 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [2a07:de40:b251:101:10:150:64:1]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 78390ead-4a8e-11f0-a309-13f23c93f187; Mon, 16 Jun 2025 10:47:01 +0200 (CEST) Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (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 smtp-out1.suse.de (Postfix) with ESMTPS id E98762118C; Mon, 16 Jun 2025 08:47:00 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id AFA83139E2; Mon, 16 Jun 2025 08:47:00 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id sb9uKQTaT2ghQAAAD6G6ig (envelope-from ); Mon, 16 Jun 2025 08:47:00 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 78390ead-4a8e-11f0-a309-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063621; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fyowQBL0+KxczJ3QgQBuNM66VYPd6WBeOp+F4jmgKk4=; b=cNJEHyikb9mt/7zsAQCnic9WNs6aNvWlVAo7gh5Foz8ylwwofNgGRJQdVwjOub6o+3afV2 cfGHzesavDEPiFkJ+yu4RECAegYAu34Qavlgc0B6rpoWQ8EO/SgPi2t/NY8pGVUDmacdaF H2v2SEOHo8FR17XTiSMsAvqz0hUIzDQ= Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063620; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fyowQBL0+KxczJ3QgQBuNM66VYPd6WBeOp+F4jmgKk4=; b=b8TE957bRIDX2dKj2D4qCqXjot6zF2lhHmjCjRiwyB9XpopFzubXSiXK8jpJthg5yFsgrA yHw/6RvRm5L75cgVyPPBpVKIpW/tNqKH6H/6o1Ct0d25D+ny7ImtKuSfGrDwA+OnZ6JyvK aJvjIyol4usendyS26lfrJa4TZf3ii0= From: Juergen Gross To: minios-devel@lists.xenproject.org, xen-devel@lists.xenproject.org Cc: samuel.thibault@ens-lyon.org, Juergen Gross , Jason Andryuk Subject: [MINI-OS PATCH v2 07/12] mini-os: kexec: build parameters for new kernel Date: Mon, 16 Jun 2025 10:46:14 +0200 Message-ID: <20250616084619.11112-8-jgross@suse.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250616084619.11112-1-jgross@suse.com> References: <20250616084619.11112-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-0.999]; MIME_GOOD(-0.10)[text/plain]; ARC_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; TO_DN_SOME(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; FUZZY_BLOCKED(0.00)[rspamd.com]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_HAS_DN(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCVD_TLS_ALL(0.00)[]; RCPT_COUNT_FIVE(0.00)[5]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:email,suse.com:mid,imap1.dmz-prg2.suse.org:helo,amd.com:email] X-Spam-Level: X-Spam-Flag: NO X-Spam-Score: -2.80 X-ZohoMail-DKIM: pass (identity @suse.com) (identity @suse.com) X-ZM-MESSAGEID: 1750063867811116600 Content-Type: text/plain; charset="utf-8" Build the parameters for the new kernel, consisting of the hvm_start_info struct, the memory map and the command line. Signed-off-by: Juergen Gross Reviewed-by: Jason Andryuk --- arch/x86/kexec.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ include/kexec.h | 4 ++++ kexec.c | 13 ++++++++++- 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/arch/x86/kexec.c b/arch/x86/kexec.c index 47830905..fc58473e 100644 --- a/arch/x86/kexec.c +++ b/arch/x86/kexec.c @@ -199,6 +199,7 @@ bool kexec_arch_need_analyze_shdrs(void) =20 static unsigned long kexec_param_loc; static unsigned int kexec_param_size; +static unsigned long kexec_param_mem; =20 void kexec_set_param_loc(const char *cmdline) { @@ -211,4 +212,61 @@ void kexec_set_param_loc(const char *cmdline) kexec_last_addr +=3D kexec_param_size; kexec_last_addr =3D round_pgup(kexec_last_addr); } + +int kexec_get_entry(const char *cmdline) +{ + struct hvm_start_info *info; + struct hvm_memmap_table_entry *mmap; + unsigned int order; + unsigned int i; + + if ( kernel_phys_entry =3D=3D ~0UL ) + return ENOEXEC; + + order =3D get_order(kexec_param_size); + + kexec_param_mem =3D alloc_pages(order); + if ( !kexec_param_mem ) + return ENOMEM; + + info =3D (struct hvm_start_info *)kexec_param_mem; + memset(info, 0, sizeof(*info)); + info->magic =3D XEN_HVM_START_MAGIC_VALUE; + info->version =3D 1; + info->cmdline_paddr =3D kexec_param_mem + sizeof(*info) + + e820_entries * sizeof(struct hvm_memmap_table_en= try); + info->memmap_paddr =3D kexec_param_mem + sizeof(*info); + info->memmap_entries =3D e820_entries; + + mmap =3D (struct hvm_memmap_table_entry *)(info + 1); + for ( i =3D 0; i < e820_entries; i++ ) + { + mmap->addr =3D e820_map[i].addr; + mmap->size =3D e820_map[i].size; + mmap->type =3D e820_map[i].type; + mmap++; + } + + strcpy((char *)mmap, cmdline); + + if ( kexec_add_action(KEXEC_COPY, to_virt(kexec_param_loc), info, + kexec_param_size) ) + return ENOSPC; + + /* The call of the new kernel happens via the physical address! */ + if ( kexec_add_action(KEXEC_CALL, (void *)kernel_phys_entry, + (void *)kexec_param_loc, 0) ) + return ENOSPC; + + return 0; +} + +void kexec_get_entry_undo(void) +{ + if ( kexec_param_mem ) + { + free_pages((void *)kexec_param_mem, get_order(kexec_param_size)); + kexec_param_mem =3D 0; + } +} #endif /* CONFIG_KEXEC */ diff --git a/include/kexec.h b/include/kexec.h index 8a2b552f..7b103dea 100644 --- a/include/kexec.h +++ b/include/kexec.h @@ -45,4 +45,8 @@ bool kexec_arch_need_analyze_shdrs(void); /* Finalize parameter location and size. */ void kexec_set_param_loc(const char *cmdline); =20 +/* Get entry point and parameter of new kernel. */ +int kexec_get_entry(const char *cmdline); +void kexec_get_entry_undo(void); + #endif /* _KEXEC_H */ diff --git a/kexec.c b/kexec.c index 1a90d8ab..e762fbeb 100644 --- a/kexec.c +++ b/kexec.c @@ -176,10 +176,21 @@ int kexec(void *kernel, unsigned long kernel_size, co= nst char *cmdline) =20 reserve_memory_below(kexec_last_addr); =20 + ret =3D kexec_get_entry(cmdline); + if ( ret ) + { + printk("kexec: ELF file of new kernel has no valid entry point\n"); + goto err; + } + /* Error exit. */ + ret =3D ENOSYS; + + err: unreserve_memory_below(); + kexec_get_entry_undo(); =20 - return ENOSYS; + return ret; } EXPORT_SYMBOL(kexec); =20 --=20 2.43.0 From nobody Fri Oct 31 03:42:49 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1750063870; cv=none; d=zohomail.com; s=zohoarc; b=TBRypr8etUavzALUaHlD9qM4v/dORkuYxm1x9lzaiMM7NcfJqW+sWmEToHGa1taLN20zYN+SCOCKV25O2Od9AFssgUd2oBAcvE7DzbZQ/rvyoPAjYIdIFeYE9krnDndlTcEqqmAwALZ9UscM/n38dRXNhZ3JPtfhiVXmIr9sZos= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750063870; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=gNbWUmZdTVJqcXlU2fjp4XsMXe38emePLWQ2ppavBkA=; b=NcVQ1cLxRk0CTevDIDUCmbZ1d2tGbzWA2sf7ZnmV5wX0gC8914zUju/4fOEAJZQ0fc1j157mvTVM78MroKxgTHjPRw8wYC0jT74aSgMxMMFju4nsCCaamRAq8AX6zs/if/D/PCjLRwKPBelAYwgS3sfldSse/OfG6uivqTSWL1c= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1750063870852836.1324667821747; Mon, 16 Jun 2025 01:51:10 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1016988.1394015 (Exim 4.92) (envelope-from ) id 1uR5YP-0006Fu-01; Mon, 16 Jun 2025 08:50:57 +0000 Received: by outflank-mailman (output) from mailman id 1016988.1394015; Mon, 16 Jun 2025 08:50:56 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5YO-0006Fb-QX; Mon, 16 Jun 2025 08:50:56 +0000 Received: by outflank-mailman (input) for mailman id 1016988; Mon, 16 Jun 2025 08:50:55 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5Uh-0007jR-Ep for xen-devel@lists.xenproject.org; Mon, 16 Jun 2025 08:47:07 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 7b999b87-4a8e-11f0-a309-13f23c93f187; Mon, 16 Jun 2025 10:47:06 +0200 (CEST) Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (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 smtp-out1.suse.de (Postfix) with ESMTPS id 861F32118C; Mon, 16 Jun 2025 08:47:06 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 4B8F4139E2; Mon, 16 Jun 2025 08:47:06 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id khXvEAraT2gsQAAAD6G6ig (envelope-from ); Mon, 16 Jun 2025 08:47:06 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 7b999b87-4a8e-11f0-a309-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063626; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gNbWUmZdTVJqcXlU2fjp4XsMXe38emePLWQ2ppavBkA=; b=tls3nwVCrtCHQR59uMJcXlRMhSzYdIzt6nLDRUrn5+RcFk84twmAK068pfugW7FkGASKel omvkvRx7Ez7ZPUfB9awNnuzVEX00E9WvgLBp1SaJgt9kdeBwIr8QLdStc5veO8tRuo3SzN HJza56kLAE3SyO3bGwPWHlve/xxoPXc= Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063626; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gNbWUmZdTVJqcXlU2fjp4XsMXe38emePLWQ2ppavBkA=; b=tls3nwVCrtCHQR59uMJcXlRMhSzYdIzt6nLDRUrn5+RcFk84twmAK068pfugW7FkGASKel omvkvRx7Ez7ZPUfB9awNnuzVEX00E9WvgLBp1SaJgt9kdeBwIr8QLdStc5veO8tRuo3SzN HJza56kLAE3SyO3bGwPWHlve/xxoPXc= From: Juergen Gross To: minios-devel@lists.xenproject.org, xen-devel@lists.xenproject.org Cc: samuel.thibault@ens-lyon.org, Juergen Gross , Jason Andryuk Subject: [MINI-OS PATCH v2 08/12] mini-os: kexec: move used pages away for new kernel Date: Mon, 16 Jun 2025 10:46:15 +0200 Message-ID: <20250616084619.11112-9-jgross@suse.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250616084619.11112-1-jgross@suse.com> References: <20250616084619.11112-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-0.999]; MIME_GOOD(-0.10)[text/plain]; ARC_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; TO_DN_SOME(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; FUZZY_BLOCKED(0.00)[rspamd.com]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_HAS_DN(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCVD_TLS_ALL(0.00)[]; RCPT_COUNT_FIVE(0.00)[5]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:email,suse.com:mid,imap1.dmz-prg2.suse.org:helo,amd.com:email] X-Spam-Level: X-Spam-Flag: NO X-Spam-Score: -2.80 X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1750063871804116600 Content-Type: text/plain; charset="utf-8" Copying the new kexec kernel must not overwrite any pages still needed during this process. Those are especially the GDT, IDT and page tables. Move those to new allocated pages and update any related pointers. In case the kexec process is cancelled later, don't undo any page table moves, as the system can just be used with the new layout. By freeing the original pages there is no memory leaking. GDT and IDT should be reverted to their original locations, as their original memory can't be freed due to not being whole pages. Signed-off-by: Juergen Gross Reviewed-by: Jason Andryuk --- arch/x86/mm.c | 126 ++++++++++++++++++++++++++++++++++++++++++++++++ include/kexec.h | 5 ++ kexec.c | 6 +++ 3 files changed, 137 insertions(+) diff --git a/arch/x86/mm.c b/arch/x86/mm.c index 3ba6d917..a71eb192 100644 --- a/arch/x86/mm.c +++ b/arch/x86/mm.c @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -923,3 +924,128 @@ unsigned long map_frame_virt(unsigned long mfn) return addr; } EXPORT_SYMBOL(map_frame_virt); + +#ifdef CONFIG_KEXEC +static unsigned long kexec_gdt; +static unsigned long kexec_idt; + +static int move_pt(unsigned long va, unsigned int lvl, bool is_leaf, + pgentry_t *pte, void *par) +{ + unsigned long boundary_pfn =3D *(unsigned long *)par; + unsigned long pfn; + void *old_pg, *new_pg; + + if ( is_leaf ) + return 0; + + pfn =3D (lvl =3D=3D PAGETABLE_LEVELS + 1) ? PHYS_PFN(*(unsigned long *= )pte) + : pte_to_mfn(*pte); + if ( pfn >=3D boundary_pfn ) + return 0; + + new_pg =3D (void *)alloc_page(); + if ( !new_pg ) + return ENOMEM; + old_pg =3D pfn_to_virt(pfn); + memcpy(new_pg, old_pg, PAGE_SIZE); + if ( lvl =3D=3D PAGETABLE_LEVELS + 1 ) + *(pgentry_t **)pte =3D new_pg; + else + *pte =3D ((unsigned long)new_pg & PAGE_MASK) | ptdata[lvl].prot; + + tlb_flush(); + + free_page(old_pg); + + return 0; +} + +static int move_leaf(unsigned long va, unsigned int lvl, bool is_leaf, + pgentry_t *pte, void *par) +{ + unsigned long boundary_pfn =3D *(unsigned long *)par; + unsigned long pfn; + void *old_pg, *new_pg; + + if ( !is_leaf ) + return 0; + + /* No large page support, all pages must be valid. */ + if ( (*pte & _PAGE_PSE) || !(*pte & _PAGE_PRESENT) ) + return EINVAL; + + pfn =3D pte_to_mfn(*pte); + if ( pfn >=3D boundary_pfn ) + return 0; + + new_pg =3D (void *)alloc_page(); + if ( !new_pg ) + return ENOMEM; + old_pg =3D pfn_to_virt(pfn); + memcpy(new_pg, old_pg, PAGE_SIZE); + *pte =3D ((unsigned long)new_pg & PAGE_MASK) | ptdata[lvl].prot; + + invlpg(va); + + free_page(old_pg); + + return 0; +} + +int kexec_move_used_pages(unsigned long boundary, unsigned long kernel, + unsigned long kernel_size) +{ + int ret; + unsigned long boundary_pfn =3D PHYS_PFN(boundary); + + kexec_gdt =3D alloc_page(); + if ( !kexec_gdt ) + return ENOMEM; + memcpy((char *)kexec_gdt, &gdt, sizeof(gdt)); + gdt_ptr.base =3D kexec_gdt; + asm volatile("lgdt %0" : : "m" (gdt_ptr)); + + kexec_idt =3D alloc_page(); + if ( !kexec_idt ) + return ENOMEM; + memcpy((char *)kexec_idt, &idt, sizeof(idt)); + idt_ptr.base =3D kexec_idt; + asm volatile("lidt %0" : : "m" (idt_ptr)); + + /* Top level page table needs special handling. */ + ret =3D move_pt(0, PAGETABLE_LEVELS + 1, false, (pgentry_t *)(&pt_base= ), + &boundary_pfn); + if ( ret ) + return ret; + ret =3D walk_pt(0, ~0UL, move_pt, &boundary_pfn); + if ( ret ) + return ret; + + /* Move new kernel image pages. */ + ret =3D walk_pt(kernel, kernel + kernel_size - 1, move_leaf, &boundary= _pfn); + if ( ret ) + return ret; + + return 0; +} + +void kexec_move_used_pages_undo(void) +{ + if ( kexec_gdt ) + { + gdt_ptr.base =3D (unsigned long)&gdt; + asm volatile("lgdt %0" : : "m" (gdt_ptr)); + free_page((void *)kexec_gdt); + kexec_gdt =3D 0; + } + + if ( kexec_idt ) + { + idt_ptr.base =3D (unsigned long)&idt; + asm volatile("lidt %0" : : "m" (idt_ptr)); + free_page((void *)kexec_idt); + kexec_idt =3D 0; + } +} +#endif diff --git a/include/kexec.h b/include/kexec.h index 7b103dea..411fa013 100644 --- a/include/kexec.h +++ b/include/kexec.h @@ -49,4 +49,9 @@ void kexec_set_param_loc(const char *cmdline); int kexec_get_entry(const char *cmdline); void kexec_get_entry_undo(void); =20 +/* Move used pages away from new kernel area. */ +int kexec_move_used_pages(unsigned long boundary, unsigned long kernel, + unsigned long kernel_size); +void kexec_move_used_pages_undo(void); + #endif /* _KEXEC_H */ diff --git a/kexec.c b/kexec.c index e762fbeb..3b11a69c 100644 --- a/kexec.c +++ b/kexec.c @@ -183,11 +183,17 @@ int kexec(void *kernel, unsigned long kernel_size, co= nst char *cmdline) goto err; } =20 + ret =3D kexec_move_used_pages(kexec_last_addr, (unsigned long)kernel, + kernel_size); + if ( ret ) + goto err; + /* Error exit. */ ret =3D ENOSYS; =20 err: unreserve_memory_below(); + kexec_move_used_pages_undo(); kexec_get_entry_undo(); =20 return ret; --=20 2.43.0 From nobody Fri Oct 31 03:42:49 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1750063861; cv=none; d=zohomail.com; s=zohoarc; b=Ct7WcPj7XWlw+U7fUJs0IfdkXzKGeIAgLaaTIf5YKETaAHcY9rZuFCoO9Jh6UBZsvR7A1gVUQTYJvoENA8QQ3mfaNRdCIYm6gzr+P4ie1CfETkDSrTd0X4hw7s0PlumQlZAZKNVJW/OfkdHrNzWJAfsWIgB1nNq+qsd4c316ANY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750063861; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=PH+wEmmUZQaMniHgWmRo2+UM2z0RSGbsNqftNFMeuQo=; b=DUWBTq20yLTmfJrD/GKOmUigHTlqz1jstEOqAUrLfILJKBaMDyrqOZwrBBmp5p0nUqvlO6lc2m0vAIceA8KYqZC6/qc7fMbz+0/nh3/5UGDNETzHdypzAyXIefIt0UlUegh4xzti6ZvxllL3y4PDB8b9YLSOQibwz038cmAymVw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1750063861866227.43055592186101; Mon, 16 Jun 2025 01:51:01 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1016942.1393936 (Exim 4.92) (envelope-from ) id 1uR5YB-000450-Ey; Mon, 16 Jun 2025 08:50:43 +0000 Received: by outflank-mailman (output) from mailman id 1016942.1393936; Mon, 16 Jun 2025 08:50:43 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5YB-00044t-C1; Mon, 16 Jun 2025 08:50:43 +0000 Received: by outflank-mailman (input) for mailman id 1016942; Mon, 16 Jun 2025 08:50:42 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5Uo-0007jR-GY for xen-devel@lists.xenproject.org; Mon, 16 Jun 2025 08:47:14 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 7eedf500-4a8e-11f0-a309-13f23c93f187; Mon, 16 Jun 2025 10:47:12 +0200 (CEST) Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (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 smtp-out2.suse.de (Postfix) with ESMTPS id 2B3FB1F387; Mon, 16 Jun 2025 08:47:12 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id E1D1D139E2; Mon, 16 Jun 2025 08:47:11 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id c91kNQ/aT2gzQAAAD6G6ig (envelope-from ); Mon, 16 Jun 2025 08:47:11 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 7eedf500-4a8e-11f0-a309-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063632; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PH+wEmmUZQaMniHgWmRo2+UM2z0RSGbsNqftNFMeuQo=; b=F86He+TTVrGS+5vuLSTsGyJlyDfbSDa2RjExEiUHJJVgeHR/0gNLAlWNdO94q/vQdZu95C YUUKnoK5lVD5tj9tj5TztJtAVntuujCirxRsmCiRo/3mmgP66lVjufKJe+y0i7/Q9Tp9gW keaScFbi1Mvlij0y3jhG7E+beTdJjv0= Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=F86He+TT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063632; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PH+wEmmUZQaMniHgWmRo2+UM2z0RSGbsNqftNFMeuQo=; b=F86He+TTVrGS+5vuLSTsGyJlyDfbSDa2RjExEiUHJJVgeHR/0gNLAlWNdO94q/vQdZu95C YUUKnoK5lVD5tj9tj5TztJtAVntuujCirxRsmCiRo/3mmgP66lVjufKJe+y0i7/Q9Tp9gW keaScFbi1Mvlij0y3jhG7E+beTdJjv0= From: Juergen Gross To: minios-devel@lists.xenproject.org, xen-devel@lists.xenproject.org Cc: samuel.thibault@ens-lyon.org, Juergen Gross , Jason Andryuk Subject: [MINI-OS PATCH v2 09/12] Mini-OS: mm: change set_readonly() to change_readonly() Date: Mon, 16 Jun 2025 10:46:16 +0200 Message-ID: <20250616084619.11112-10-jgross@suse.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250616084619.11112-1-jgross@suse.com> References: <20250616084619.11112-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; ARC_NA(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; MIME_TRACE(0.00)[0:+]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; TO_DN_SOME(0.00)[]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:dkim,suse.com:mid,suse.com:email,imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns,amd.com:email]; RCVD_TLS_ALL(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCPT_COUNT_FIVE(0.00)[5]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; DKIM_TRACE(0.00)[suse.com:+] X-Spam-Level: X-Spam-Flag: NO X-Rspamd-Queue-Id: 2B3FB1F387 X-Rspamd-Action: no action X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Score: -3.01 X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1750063863948116600 Content-Type: text/plain; charset="utf-8" Rename set_readonly() to change_readonly() and add a parameter specifying whether it should set the kernel to readonly or to writable. At the same time move the boundary setting from the only caller into the function itself, avoiding the need to use the same boundaries in future, when it will be called to set the kernel to writable again. Make the function globally visible in order to allow calling it from kexec coding later. Merge clear_bootstrap() into change_readonly() and undo its setting of page 0 to invalid when setting the kernel writable. Signed-off-by: Juergen Gross Reviewed-by: Jason Andryuk --- arch/x86/mm.c | 111 +++++++++++++++++++++++++++----------------------- include/mm.h | 3 ++ 2 files changed, 64 insertions(+), 50 deletions(-) diff --git a/arch/x86/mm.c b/arch/x86/mm.c index a71eb192..f4419d95 100644 --- a/arch/x86/mm.c +++ b/arch/x86/mm.c @@ -405,17 +405,19 @@ static void build_pagetable(unsigned long *start_pfn,= unsigned long *max_pfn) */ extern struct shared_info shared_info; =20 -struct set_readonly_par { +struct change_readonly_par { unsigned long etext; #ifdef CONFIG_PARAVIRT unsigned int count; #endif + bool readonly; }; =20 -static int set_readonly_func(unsigned long va, unsigned int lvl, bool is_l= eaf, - pgentry_t *pte, void *par) +static int change_readonly_func(unsigned long va, unsigned int lvl, + bool is_leaf, pgentry_t *pte, void *par) { - struct set_readonly_par *ro =3D par; + struct change_readonly_par *ro =3D par; + pgentry_t newval; =20 if ( !is_leaf ) return 0; @@ -429,9 +431,11 @@ static int set_readonly_func(unsigned long va, unsigne= d int lvl, bool is_leaf, return 0; } =20 + newval =3D ro->readonly ? (*pte & ~_PAGE_RW) : (*pte | _PAGE_RW); + #ifdef CONFIG_PARAVIRT mmu_updates[ro->count].ptr =3D virt_to_mach(pte); - mmu_updates[ro->count].val =3D *pte & ~_PAGE_RW; + mmu_updates[ro->count].val =3D newval; ro->count++; =20 if ( ro->count =3D=3D L1_PAGETABLE_ENTRIES ) @@ -442,7 +446,7 @@ static int set_readonly_func(unsigned long va, unsigned= int lvl, bool is_leaf, ro->count =3D 0; } #else - *pte &=3D ~_PAGE_RW; + *pte =3D newval; #endif =20 return 0; @@ -463,23 +467,6 @@ static void tlb_flush(void) } #endif =20 -static void set_readonly(void *text, void *etext) -{ - struct set_readonly_par setro =3D { .etext =3D (unsigned long)etext }; - unsigned long start_address =3D PAGE_ALIGN((unsigned long)text); - - printk("setting %p-%p readonly\n", text, etext); - walk_pt(start_address, setro.etext, set_readonly_func, &setro); - -#ifdef CONFIG_PARAVIRT - if ( setro.count && - HYPERVISOR_mmu_update(mmu_updates, setro.count, NULL, DOMID_SELF)= < 0) - BUG(); -#endif - - tlb_flush(); -} - /* * get the PTE for virtual address va if it exists. Otherwise NULL. */ @@ -508,6 +495,51 @@ static pgentry_t *get_pgt(unsigned long va) return tab; } =20 +void change_readonly(bool readonly) +{ + struct change_readonly_par ro =3D { + .etext =3D (unsigned long)&_erodata, + .readonly =3D readonly, + }; + unsigned long start_address =3D PAGE_ALIGN((unsigned long)&_text); +#ifdef CONFIG_PARAVIRT + pte_t nullpte =3D { }; + int rc; +#else + pgentry_t *pgt =3D get_pgt((unsigned long)&_text); +#endif + + if ( readonly ) + { +#ifdef CONFIG_PARAVIRT + if ( (rc =3D HYPERVISOR_update_va_mapping(0, nullpte, UVMF_INVLPG)= ) ) + printk("Unable to unmap NULL page. rc=3D%d\n", rc); +#else + *pgt =3D 0; + invlpg((unsigned long)&_text); +#endif + } + else + { +#ifdef CONFIG_PARAVIRT + /* No kexec support with PARAVIRT. */ + BUG(); +#else + *pgt =3D L1_PROT; +#endif + } + + printk("setting %p-%p readonly\n", &_text, &_erodata); + walk_pt(start_address, ro.etext, change_readonly_func, &ro); + +#ifdef CONFIG_PARAVIRT + if ( ro.count && + HYPERVISOR_mmu_update(mmu_updates, ro.count, NULL, DOMID_SELF) < = 0) + BUG(); +#endif + + tlb_flush(); +} =20 /* * return a valid PTE for a given virtual address. If PTE does not exist, @@ -789,31 +821,6 @@ int unmap_frames(unsigned long va, unsigned long num_f= rames) } EXPORT_SYMBOL(unmap_frames); =20 -/* - * Clear some of the bootstrap memory - */ -static void clear_bootstrap(void) -{ -#ifdef CONFIG_PARAVIRT - pte_t nullpte =3D { }; - int rc; -#else - pgentry_t *pgt; -#endif - - /* Use first page as the CoW zero page */ - memset(&_text, 0, PAGE_SIZE); - mfn_zero =3D virt_to_mfn((unsigned long) &_text); -#ifdef CONFIG_PARAVIRT - if ( (rc =3D HYPERVISOR_update_va_mapping(0, nullpte, UVMF_INVLPG)) ) - printk("Unable to unmap NULL page. rc=3D%d\n", rc); -#else - pgt =3D get_pgt((unsigned long)&_text); - *pgt =3D 0; - invlpg((unsigned long)&_text); -#endif -} - #ifdef CONFIG_PARAVIRT void p2m_chk_pfn(unsigned long pfn) { @@ -884,8 +891,12 @@ void arch_init_mm(unsigned long* start_pfn_p, unsigned= long* max_pfn_p) printk(" max_pfn: %lx\n", max_pfn); =20 build_pagetable(&start_pfn, &max_pfn); - clear_bootstrap(); - set_readonly(&_text, &_erodata); + + /* Prepare page 0 as CoW page. */ + memset(&_text, 0, PAGE_SIZE); + mfn_zero =3D virt_to_mfn((unsigned long)&_text); + + change_readonly(true); =20 *start_pfn_p =3D start_pfn; *max_pfn_p =3D max_pfn; diff --git a/include/mm.h b/include/mm.h index 5775c3e1..0a16d56c 100644 --- a/include/mm.h +++ b/include/mm.h @@ -25,6 +25,7 @@ #ifndef _MM_H_ #define _MM_H_ =20 +#include #if defined(__i386__) #include #elif defined(__x86_64__) @@ -92,4 +93,6 @@ extern unsigned long heap, brk, heap_mapped, heap_end; int free_physical_pages(xen_pfn_t *mfns, int n); void fini_mm(void); =20 +void change_readonly(bool readonly); + #endif /* _MM_H_ */ --=20 2.43.0 From nobody Fri Oct 31 03:42:49 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1750063862; cv=none; d=zohomail.com; s=zohoarc; b=VUrC73qV0K5jt8uNHTLInRIhXzQv2Bx0eOCxZ17U8IwLF878DMp/Jx2oXSwIhe5NMeCe4xFfhpjQTqHrIEGL4prXq2aQFcG10rlTRvkFZ2F1umLGVJmvhsSII1+GTE3kYrTaqogZdH5zXhbexniU2SX5NaSqjmFGFYwq7gKg0k4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750063862; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=9oEFTpU9aU3slJF04hBQYd4aVGaCv7knMZrTTup/bd0=; b=kXmuXgDFE6Qxa4p5kYlPJm0dKWMUXzQEGv0Zdf9pHkCJGITkcNUPo+OMpWKvnI/MWCqrn/Xpq6pceml9vxEz+m9vAkRW2OWLR036i6LkRRitJ3MC2CW2pAT4pt7aHSJM7EmAbHiwJg2P57zJbJhpuocl9C4eVZZnvhYtWhYrAw8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1750063862556153.23150217086618; Mon, 16 Jun 2025 01:51:02 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1016939.1393926 (Exim 4.92) (envelope-from ) id 1uR5YA-0003qk-88; Mon, 16 Jun 2025 08:50:42 +0000 Received: by outflank-mailman (output) from mailman id 1016939.1393926; Mon, 16 Jun 2025 08:50:42 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5YA-0003qb-5O; Mon, 16 Jun 2025 08:50:42 +0000 Received: by outflank-mailman (input) for mailman id 1016939; Mon, 16 Jun 2025 08:50:41 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5Ut-0007jR-Ts for xen-devel@lists.xenproject.org; Mon, 16 Jun 2025 08:47:19 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [2a07:de40:b251:101:10:150:64:2]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 8307937a-4a8e-11f0-a309-13f23c93f187; Mon, 16 Jun 2025 10:47:19 +0200 (CEST) Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (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 smtp-out2.suse.de (Postfix) with ESMTPS id B273A1F38F; Mon, 16 Jun 2025 08:47:17 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 80FDD139E2; Mon, 16 Jun 2025 08:47:17 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id sunAHRXaT2g6QAAAD6G6ig (envelope-from ); Mon, 16 Jun 2025 08:47:17 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 8307937a-4a8e-11f0-a309-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063639; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9oEFTpU9aU3slJF04hBQYd4aVGaCv7knMZrTTup/bd0=; b=sheYdGj3pN/vC/Rmbq1lGWAmA2IZKgGLUu21+LihgsnyaOqBqkqlcef+YPpQOyq9qBTq7L NrPP5vKsYlPjyqVBpOYgFHhD2Up59/C+xiOJuB+8KsrQVU82c6aoS6iKL+MssAIV3z9uty iyautI2uJP/Yk0cXjsJbJSspA9XiWsw= Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=oY+0NaNR DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063637; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9oEFTpU9aU3slJF04hBQYd4aVGaCv7knMZrTTup/bd0=; b=oY+0NaNR88YKEezyZT76ajQXIDV6E/w9gQmjqDAGaSEp+OI27LwPkMzRPFnCt2NOmklteT LNdGLs2sHtlW2IduhOupmr47jHWggPSzL1iN0NOYKkuFQXzU7euxBNVgUa35m9b63vV3cx Qp3e2ZqoEmOBFzN0sH79WGnM9zETq3s= From: Juergen Gross To: minios-devel@lists.xenproject.org, xen-devel@lists.xenproject.org Cc: samuel.thibault@ens-lyon.org, Juergen Gross , Jason Andryuk Subject: [MINI-OS PATCH v2 10/12] Mini-OS: kexec: switch read-only area to be writable again Date: Mon, 16 Jun 2025 10:46:17 +0200 Message-ID: <20250616084619.11112-11-jgross@suse.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250616084619.11112-1-jgross@suse.com> References: <20250616084619.11112-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Rspamd-Queue-Id: B273A1F38F X-Rspamd-Action: no action X-Spam-Flag: NO X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[99.99%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; MIME_TRACE(0.00)[0:+]; FUZZY_BLOCKED(0.00)[rspamd.com]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; TO_DN_SOME(0.00)[]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; ARC_NA(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_HAS_DN(0.00)[]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; DNSWL_BLOCKED(0.00)[2a07:de40:b281:104:10:150:64:97:from,2a07:de40:b281:106:10:150:64:167:received]; FROM_EQ_ENVFROM(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCVD_TLS_ALL(0.00)[]; DKIM_TRACE(0.00)[suse.com:+]; RCPT_COUNT_FIVE(0.00)[5]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:rdns,imap1.dmz-prg2.suse.org:helo,amd.com:email,suse.com:mid,suse.com:dkim,suse.com:email] X-Spam-Score: -3.01 X-Spam-Level: X-ZohoMail-DKIM: pass (identity @suse.com) (identity @suse.com) X-ZM-MESSAGEID: 1750063863787116600 Content-Type: text/plain; charset="utf-8" In order to allow writing the new kernel, make the readonly area covering current kernel text writable again. Signed-off-by: Juergen Gross Reviewed-by: Jason Andryuk --- V2: - modify printk() statement (Jason Andryuk) --- arch/x86/mm.c | 3 ++- kexec.c | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/arch/x86/mm.c b/arch/x86/mm.c index f4419d95..26ede6f4 100644 --- a/arch/x86/mm.c +++ b/arch/x86/mm.c @@ -529,7 +529,8 @@ void change_readonly(bool readonly) #endif } =20 - printk("setting %p-%p readonly\n", &_text, &_erodata); + printk("setting %p-%p %s\n", &_text, &_erodata, + readonly ? "readonly" : "writable"); walk_pt(start_address, ro.etext, change_readonly_func, &ro); =20 #ifdef CONFIG_PARAVIRT diff --git a/kexec.c b/kexec.c index 3b11a69c..1ef7ce84 100644 --- a/kexec.c +++ b/kexec.c @@ -183,6 +183,8 @@ int kexec(void *kernel, unsigned long kernel_size, cons= t char *cmdline) goto err; } =20 + change_readonly(false); + ret =3D kexec_move_used_pages(kexec_last_addr, (unsigned long)kernel, kernel_size); if ( ret ) @@ -192,6 +194,7 @@ int kexec(void *kernel, unsigned long kernel_size, cons= t char *cmdline) ret =3D ENOSYS; =20 err: + change_readonly(true); unreserve_memory_below(); kexec_move_used_pages_undo(); kexec_get_entry_undo(); --=20 2.43.0 From nobody Fri Oct 31 03:42:49 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1750063870; cv=none; d=zohomail.com; s=zohoarc; b=h4RzZ3tAbxm2UBAkTU9XNPunY96nR0k90GONrLCwMzIz3e3/IeNAUTYBKI8xAIrXAcvGTE68pumTDx+OQ+1UNR52JcMPVZCQ6ZnSPWotydWQTt1lXIOriTANvz/LZfT9PyCbTa9egKgQf/DiE3AafGYXiEQ0ryirLBuWSaFu6bs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750063870; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=k/MeYSsGBJ27+vnobz1RvxbDTqVako7BAJOkWTQLl8E=; b=HxUL7VswUSyE6/oWL08itEv+FYSX4jM8YfVt1kUvFdozNHw6UM8rQzOUSM8F3IK0HYE8x5g0dcrTA0A/QNc6auVITyRbOWJLrbWCzoj1QzazAlBVXCoOTM/svlVfWrgW42T7ZY/z2QHiawFfp2iXz3aCOORsdQnAB5vRnPD/p78= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1750063870851129.66169348182768; Mon, 16 Jun 2025 01:51:10 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1016981.1394003 (Exim 4.92) (envelope-from ) id 1uR5YL-0005jV-Kt; Mon, 16 Jun 2025 08:50:53 +0000 Received: by outflank-mailman (output) from mailman id 1016981.1394003; Mon, 16 Jun 2025 08:50:53 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5YL-0005jB-HB; Mon, 16 Jun 2025 08:50:53 +0000 Received: by outflank-mailman (input) for mailman id 1016981; Mon, 16 Jun 2025 08:50:53 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5Uy-0007jR-7j for xen-devel@lists.xenproject.org; Mon, 16 Jun 2025 08:47:24 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 85a4ee03-4a8e-11f0-a309-13f23c93f187; Mon, 16 Jun 2025 10:47:23 +0200 (CEST) Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (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 smtp-out1.suse.de (Postfix) with ESMTPS id 53FAB21203; Mon, 16 Jun 2025 08:47:23 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 1BCF7139E2; Mon, 16 Jun 2025 08:47:23 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id +Cs4BRvaT2hAQAAAD6G6ig (envelope-from ); Mon, 16 Jun 2025 08:47:23 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 85a4ee03-4a8e-11f0-a309-13f23c93f187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063643; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=k/MeYSsGBJ27+vnobz1RvxbDTqVako7BAJOkWTQLl8E=; b=GXDjXMGo+0jyW1vB1crqi8s5c4HCfFeRis9b1zeJ45ctD3I/jEBpSePWxd+6dr05WO8bUM VEC11DAu0zCijo+VAjliNqxmux25hCpMVsyRzX9BKg1AZCzDfzQo0X5u7N+MYSqwgsPhF1 kfjYWesKAEyZNk9NLbamW9ewCBDyygM= Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=GXDjXMGo DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063643; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=k/MeYSsGBJ27+vnobz1RvxbDTqVako7BAJOkWTQLl8E=; b=GXDjXMGo+0jyW1vB1crqi8s5c4HCfFeRis9b1zeJ45ctD3I/jEBpSePWxd+6dr05WO8bUM VEC11DAu0zCijo+VAjliNqxmux25hCpMVsyRzX9BKg1AZCzDfzQo0X5u7N+MYSqwgsPhF1 kfjYWesKAEyZNk9NLbamW9ewCBDyygM= From: Juergen Gross To: minios-devel@lists.xenproject.org, xen-devel@lists.xenproject.org Cc: samuel.thibault@ens-lyon.org, Juergen Gross , Jason Andryuk Subject: [MINI-OS PATCH v2 11/12] mini-os: kexec: add kexec callback functionality Date: Mon, 16 Jun 2025 10:46:18 +0200 Message-ID: <20250616084619.11112-12-jgross@suse.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250616084619.11112-1-jgross@suse.com> References: <20250616084619.11112-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Rspamd-Queue-Id: 53FAB21203 X-Rspamd-Action: no action X-Spam-Flag: NO X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; MIME_TRACE(0.00)[0:+]; FUZZY_BLOCKED(0.00)[rspamd.com]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; TO_DN_SOME(0.00)[]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; ARC_NA(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_HAS_DN(0.00)[]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; DNSWL_BLOCKED(0.00)[2a07:de40:b281:106:10:150:64:167:received,2a07:de40:b281:104:10:150:64:97:from]; FROM_EQ_ENVFROM(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCVD_TLS_ALL(0.00)[]; DKIM_TRACE(0.00)[suse.com:+]; RCPT_COUNT_FIVE(0.00)[5]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:mid,suse.com:dkim,suse.com:email,imap1.dmz-prg2.suse.org:rdns,imap1.dmz-prg2.suse.org:helo,amd.com:email] X-Spam-Score: -3.01 X-Spam-Level: X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1750063871707116600 Content-Type: text/plain; charset="utf-8" Add a kexec_call() macro which will provide the capability to register a function for being called when doing a kexec() call. The called functions will be called with a boolean parameter "undo" indicating whether a previous call needs to be undone due to a failure during kexec(). The related loop to call all callbacks is added to kexec(). Signed-off-by: Juergen Gross Reviewed-by: Jason Andryuk --- arch/x86/minios-x86.lds.S | 8 ++++++++ include/kexec.h | 6 ++++++ kexec.c | 13 +++++++++++++ 3 files changed, 27 insertions(+) diff --git a/arch/x86/minios-x86.lds.S b/arch/x86/minios-x86.lds.S index 8a1db7d4..8c6e0a60 100644 --- a/arch/x86/minios-x86.lds.S +++ b/arch/x86/minios-x86.lds.S @@ -58,6 +58,14 @@ SECTIONS } PROVIDE (__fini_array_end =3D .); =20 +#if defined(CONFIG_KEXEC) + PROVIDE (__kexec_array_start =3D .); + .kexec_array : { + *(.kexec_array) + } + PROVIDE (__kexec_array_end =3D .); +#endif + .ctors : { __CTOR_LIST__ =3D .; *(.ctors) diff --git a/include/kexec.h b/include/kexec.h index 411fa013..b89c3000 100644 --- a/include/kexec.h +++ b/include/kexec.h @@ -18,6 +18,12 @@ struct kexec_action { =20 extern char _kexec_start[], _kexec_end[]; extern struct kexec_action kexec_actions[KEXEC_MAX_ACTIONS]; +extern unsigned long __kexec_array_start[], __kexec_array_end[]; + +typedef int(*kexeccall_t)(bool undo); +#define kexec_call(func) = \ + static kexeccall_t __kexeccall_##func __attribute__((__used__)) = \ + __attribute__((__section__(".kexec_array"))) =3D fu= nc =20 extern unsigned long kexec_last_addr; =20 diff --git a/kexec.c b/kexec.c index 1ef7ce84..f42aa8ad 100644 --- a/kexec.c +++ b/kexec.c @@ -167,6 +167,7 @@ static int analyze_kernel(void *kernel, unsigned long s= ize) int kexec(void *kernel, unsigned long kernel_size, const char *cmdline) { int ret; + unsigned long *func; =20 ret =3D analyze_kernel(kernel, kernel_size); if ( ret ) @@ -190,6 +191,18 @@ int kexec(void *kernel, unsigned long kernel_size, con= st char *cmdline) if ( ret ) goto err; =20 + for ( func =3D __kexec_array_start; func < __kexec_array_end; func++ ) + { + ret =3D ((kexeccall_t)(*func))(false); + if ( ret ) + { + for ( func--; func >=3D __kexec_array_start; func-- ) + ((kexeccall_t)(*func))(true); + + goto err; + } + } + /* Error exit. */ ret =3D ENOSYS; =20 --=20 2.43.0 From nobody Fri Oct 31 03:42:49 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1750063859; cv=none; d=zohomail.com; s=zohoarc; b=fDur3viRVAFGq7wssKhk1HZMjJSi8/gat7BckW1JPaV8UYHEzp8W5/7yoVHICTCmSMcA6A3yoF5FLt+AibZ5aJLEbkC94zxDTu9Rv39uN3W5jB+aPwyDqwWhb3OgnkUZrbwILZbntVJEeC14ThUxzTRPPAcz/MoN0epb1OHjX3Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750063859; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=IMg43rEyz8CpFoVtq3k2oUri4d2hHuQbI9tlZdtJd1M=; b=GLa0ofN19pUVqycCJfayjE+04DhBgEQn1aQzz6GmOVHb9qGgx5UoHwXuqRvWj6PDV2/Qq7UTmGjdXe5JnoeD4A9RwT1tZwuwAF+bpBI5QEowOLw/ZBPBDUNBuHPIUeWNbgYA6AlZhhlBVY03HQ6qkheiLt21xKeLRbAPkeiFC+k= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1750063859216270.8250282766619; Mon, 16 Jun 2025 01:50:59 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1016944.1393943 (Exim 4.92) (envelope-from ) id 1uR5YB-00048A-Oo; Mon, 16 Jun 2025 08:50:43 +0000 Received: by outflank-mailman (output) from mailman id 1016944.1393943; Mon, 16 Jun 2025 08:50:43 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5YB-00047I-JT; Mon, 16 Jun 2025 08:50:43 +0000 Received: by outflank-mailman (input) for mailman id 1016944; Mon, 16 Jun 2025 08:50:42 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uR5V4-0007hV-SQ for xen-devel@lists.xenproject.org; Mon, 16 Jun 2025 08:47:30 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 88f20ac5-4a8e-11f0-b894-0df219b8e170; Mon, 16 Jun 2025 10:47:29 +0200 (CEST) Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (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 smtp-out1.suse.de (Postfix) with ESMTPS id DD17221203; Mon, 16 Jun 2025 08:47:28 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id A87E4139E2; Mon, 16 Jun 2025 08:47:28 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id oGyWJyDaT2hFQAAAD6G6ig (envelope-from ); Mon, 16 Jun 2025 08:47:28 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 88f20ac5-4a8e-11f0-b894-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063649; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IMg43rEyz8CpFoVtq3k2oUri4d2hHuQbI9tlZdtJd1M=; b=RBycZqlgyhvjGURyBTEl+DgB6nQGhh31WKaVV+yTp1sAXttmg+yIRZ9ukSzGsZKHBoj0ij REk5qSbSMn7B/V/WkDfZJFZwpnGhvZxP0e6Vbm47m2+M3bB7WOMiJsEb42h7Vi8uUtIAfe XGe7W1XKAgxDD1/uhmOBMfx6dd+ohfU= Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=dYaRylJK DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1750063648; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IMg43rEyz8CpFoVtq3k2oUri4d2hHuQbI9tlZdtJd1M=; b=dYaRylJK0V/t4bUa7GTifIuxYGsBu9t85xuZtG1CaUoZoxuvd5an6xk4mHpO7Mnz3VLPp3 lyGpE1QHPLGkpJK7gIO0bQMrQee+GDMqR2JZL5rY7NJNs4DjFes4i3UVl51VExF0k8KaBk b9hndnRcm56g6Zm+Zo6APRP/RHPKP4k= From: Juergen Gross To: minios-devel@lists.xenproject.org, xen-devel@lists.xenproject.org Cc: samuel.thibault@ens-lyon.org, Juergen Gross Subject: [MINI-OS PATCH v2 12/12] mini-os: kexec: do the final kexec step Date: Mon, 16 Jun 2025 10:46:19 +0200 Message-ID: <20250616084619.11112-13-jgross@suse.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250616084619.11112-1-jgross@suse.com> References: <20250616084619.11112-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; ARC_NA(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; MIME_TRACE(0.00)[0:+]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; TO_DN_SOME(0.00)[]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:dkim,suse.com:mid,suse.com:email,imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns]; RCVD_TLS_ALL(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCPT_COUNT_THREE(0.00)[4]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; DKIM_TRACE(0.00)[suse.com:+] X-Spam-Level: X-Spam-Flag: NO X-Rspamd-Queue-Id: DD17221203 X-Rspamd-Action: no action X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Score: -3.01 X-ZohoMail-DKIM: pass (identity @suse.com) (identity @suse.com) X-ZM-MESSAGEID: 1750063859575116600 Content-Type: text/plain; charset="utf-8" With all kexec preparations done, activate the new kernel. Signed-off-by: Juergen Gross Reviewed-by: Jason Andryuk --- kexec.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/kexec.c b/kexec.c index f42aa8ad..2607c819 100644 --- a/kexec.c +++ b/kexec.c @@ -168,6 +168,7 @@ int kexec(void *kernel, unsigned long kernel_size, cons= t char *cmdline) { int ret; unsigned long *func; + void *kexec_page; =20 ret =3D analyze_kernel(kernel, kernel_size); if ( ret ) @@ -191,6 +192,13 @@ int kexec(void *kernel, unsigned long kernel_size, con= st char *cmdline) if ( ret ) goto err; =20 + kexec_page =3D (void *)alloc_page(); + if ( !kexec_page ) + { + ret =3D ENOMEM; + goto err; + } + for ( func =3D __kexec_array_start; func < __kexec_array_end; func++ ) { ret =3D ((kexeccall_t)(*func))(false); @@ -203,10 +211,15 @@ int kexec(void *kernel, unsigned long kernel_size, co= nst char *cmdline) } } =20 - /* Error exit. */ - ret =3D ENOSYS; + /* Activate the new kernel. */ + do_kexec(kexec_page); + + /* do_kexec() shouldn't return, crash. */ + BUG(); =20 err: + if ( kexec_page ) + free_page(kexec_page); change_readonly(true); unreserve_memory_below(); kexec_move_used_pages_undo(); --=20 2.43.0