From nobody Fri Oct 31 04:01:20 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=1752586352; cv=none; d=zohomail.com; s=zohoarc; b=m7FptZaiske6k1t29tC4ThA3w5vZc4i7BssF7WmIsjiw2cXFdRJ7TAC4qomn2rvAfwss7ycxpkN+icu7F+iXbO2WvZya1NzlIRmeiAmcB0AoOcFue+RqEucfFoK36KTB/YxYVOmOvqq0CuFKhuHl4ItZOvna7PmhAy3LsjEmI5g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1752586352; 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=dQG0ZTeiRyc26Tqw3qcZ1Dj3k6IjcIiutErlineQVn4=; b=DyBT70jSfkpq3p1gKxSvZAbfGqkrdEUv/SXHhPGJxz06ZjmnW9SWXMWY4EGYpH6eaPb0XqkaBydcgYWgKm+1zR957JOZS1L1huL3bChhWpV6Hplz6PdGWW+JAHpcnt7VAnIiwRl/59pchZwpKcmz+b7dehbFucLBTihaXPhUGN0= 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 1752586352192602.4482288385137; Tue, 15 Jul 2025 06:32:32 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1044184.1414296 (Exim 4.92) (envelope-from ) id 1ubflb-0006F9-SX; Tue, 15 Jul 2025 13:32:19 +0000 Received: by outflank-mailman (output) from mailman id 1044184.1414296; Tue, 15 Jul 2025 13:32:19 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ubflb-0006F2-Pr; Tue, 15 Jul 2025 13:32:19 +0000 Received: by outflank-mailman (input) for mailman id 1044184; Tue, 15 Jul 2025 13:32:19 +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 1ubfjr-0000p6-B8 for xen-devel@lists.xenproject.org; Tue, 15 Jul 2025 13:30:31 +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 deddae55-617f-11f0-b894-0df219b8e170; Tue, 15 Jul 2025 15:30:27 +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 7753621204; Tue, 15 Jul 2025 13:30: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 3E4B413A68; Tue, 15 Jul 2025 13:30:27 +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 CqNVDfNXdmhuNwAAD6G6ig (envelope-from ); Tue, 15 Jul 2025 13:30:27 +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: deddae55-617f-11f0-b894-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1752586227; 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=dQG0ZTeiRyc26Tqw3qcZ1Dj3k6IjcIiutErlineQVn4=; b=NZ2ORD1YgOxFUDEQktZudNsTB9rkqxzOvgAcbLZzEyNGLXQu53e+b6Je6NJdmPTVcCK1Fg xEcWFNL09prPaYlxoLmIDRUjOZxPZIJtYlSH332HGKkHrKg2eMatccnZku4uU/3i8sMEtA ZZ9CqEWjqDzOpjrpRhZAZtrKkxBO/l4= Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1752586227; 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=dQG0ZTeiRyc26Tqw3qcZ1Dj3k6IjcIiutErlineQVn4=; b=NZ2ORD1YgOxFUDEQktZudNsTB9rkqxzOvgAcbLZzEyNGLXQu53e+b6Je6NJdmPTVcCK1Fg xEcWFNL09prPaYlxoLmIDRUjOZxPZIJtYlSH332HGKkHrKg2eMatccnZku4uU/3i8sMEtA ZZ9CqEWjqDzOpjrpRhZAZtrKkxBO/l4= 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/14] kexec: add support for allocating pages from kexec module memory Date: Tue, 15 Jul 2025 15:29:30 +0200 Message-ID: <20250715132936.2798-9-jgross@suse.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250715132936.2798-1-jgross@suse.com> References: <20250715132936.2798-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spam-Level: 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]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_HAS_DN(0.00)[]; ARC_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; TO_DN_SOME(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,suse.com:email,suse.com:mid,amd.com:email]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; FUZZY_RATELIMITED(0.00)[rspamd.com]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; RCPT_COUNT_FIVE(0.00)[5]; RCVD_TLS_ALL(0.00)[] X-Spam-Flag: NO X-Spam-Score: -2.80 X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1752586354528116600 Content-Type: text/plain; charset="utf-8" Add the needed functions for allocating and freeing memory pages of the kexec module. As the pages are always related to a kexec module record, add the related utility functions, too. For now only support adding records and retrieving them. Signed-off-by: Juergen Gross Reviewed-by: Jason Andryuk --- arch/x86/kexec.c | 16 +++++++ include/kexec.h | 52 +++++++++++++++++++++ kexec.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 183 insertions(+) diff --git a/arch/x86/kexec.c b/arch/x86/kexec.c index 8c9f8a3c..e48df3f5 100644 --- a/arch/x86/kexec.c +++ b/arch/x86/kexec.c @@ -240,6 +240,9 @@ static void get_mod_addr(unsigned long from, unsigned l= ong to) #define min(a, b) ((a) < (b) ? (a) : (b)) void kexec_module(unsigned long start_pfn, unsigned long max_pfn) { + unsigned int i; + char *rec_end; + /* Reuse already existing kexec module. */ mod_ptr =3D kexec_check_module(); if ( !mod_ptr && CONFIG_KEXEC_MODULE_PAGES ) @@ -257,11 +260,24 @@ void kexec_module(unsigned long start_pfn, unsigned l= ong max_pfn) sizeof(mod_ptr->eye_catcher)); mod_ptr->n_pages =3D CONFIG_KEXEC_MODULE_PAGES - 1; memset(mod_ptr->pg2rec, KEXECMOD_PG_FREE, mod_ptr->n_pages); + mod_ptr->n_records =3D 16; mod_ptr->recs_off =3D sizeof(struct kexec_module) + mod_ptr->n_pages + (mod_ptr->n_pages & 1); =20 set_reserved_range(kexec_mod_start, (unsigned long)mod_ptr + PAGE_= SIZE); } + + mod_recs =3D (void *)((unsigned long)mod_ptr + mod_ptr->recs_off); + mod_rec_start =3D (char *)(mod_recs + mod_ptr->n_records); + mod_rec_end =3D mod_rec_start; + for ( i =3D 0; i < mod_ptr->n_records; i++ ) + { + if ( mod_recs[i].type =3D=3D KEXECMOD_REC_NONE ) + continue; + rec_end =3D (char *)mod_ptr + mod_recs[i].offset + mod_recs[i].siz= e; + if ( mod_rec_end < rec_end ) + mod_rec_end =3D rec_end; + } } =20 void kexec_set_param_loc(const char *cmdline) diff --git a/include/kexec.h b/include/kexec.h index b38a9f54..0e0b1a4d 100644 --- a/include/kexec.h +++ b/include/kexec.h @@ -67,6 +67,9 @@ struct kexec_module_rec { =20 extern unsigned long kexec_mod_start; extern struct kexec_module *mod_ptr; +extern struct kexec_module_rec *mod_recs; +extern char *mod_rec_start; +extern char *mod_rec_end; =20 /* One element of kexec actions (last element must have action KEXEC_CALL)= : */ struct kexec_action { @@ -80,6 +83,17 @@ struct kexec_action { void *src; }; =20 +#ifdef CONFIG_KEXEC +unsigned long kexec_alloc_mod_pages(unsigned int recid, unsigned int n); +void kexec_free_mod_pages(unsigned int recid, unsigned long addr, + unsigned int n); +int kexec_find_mod_record(unsigned int start_idx, unsigned int type, + unsigned int *size); +int kexec_add_mod_record(unsigned int type, void *addr, unsigned int size); +int kexec_upd_mod_record(unsigned int idx, unsigned int type, + void *addr, unsigned int size); +int kexec_read_mod_record(unsigned int idx, void *addr, unsigned int size); + #define KEXEC_MAX_ACTIONS 16 =20 extern char _kexec_start[], _kexec_end[]; @@ -129,4 +143,42 @@ void kexec_move_used_pages_undo(void); /* Check for kexec module and create kexec memory if needed. */ void kexec_module(unsigned long start_pfn, unsigned long max_pfn); =20 +#else /* CONFIG_KEXEC */ +static inline unsigned long kexec_alloc_mod_pages(unsigned int recid, + unsigned int n) +{ + return 0; +} + +static inline void kexec_free_mod_pages(unsigned int recid, unsigned long = addr, + unsigned int n) +{ +} + +static inline int kexec_find_mod_record(unsigned int start_idx, + unsigned int type, unsigned int *s= ize) +{ + return 0; +} + +static inline int kexec_add_mod_record(unsigned int type, void *addr, + unsigned int size) +{ + return 0; +} + +static inline int kexec_upd_mod_record(unsigned int idx, unsigned int type, + void *addr, unsigned int size) +{ + return 0; +} + +static inline int kexec_read_mod_record(unsigned int idx, void *addr, + unsigned int size) +{ + return 0; +} + +#endif + #endif /* _KEXEC_H */ diff --git a/kexec.c b/kexec.c index ded29882..f17ed13d 100644 --- a/kexec.c +++ b/kexec.c @@ -253,3 +253,118 @@ int kexec_add_action(int action, void *dest, void *sr= c, unsigned int len) =20 unsigned long kexec_mod_start; struct kexec_module *mod_ptr; +struct kexec_module_rec *mod_recs; +char *mod_rec_start; +char *mod_rec_end; + +unsigned long kexec_alloc_mod_pages(unsigned int recid, unsigned int n) +{ + unsigned int first =3D 0, i; + + for ( i =3D 0; i < mod_ptr->n_pages; i++ ) + { + if ( i - first =3D=3D n - 1 ) + { + for ( i =3D 0; i < n; i++ ) + mod_ptr->pg2rec[first + i] =3D recid; + + return kexec_mod_start + PFN_PHYS(first); + } + + if ( mod_ptr->pg2rec[i] !=3D KEXECMOD_PG_FREE ) + first =3D i + 1; + } + + printk("Kexec module out of memory\n"); + BUG(); +} + +void kexec_free_mod_pages(unsigned int recid, unsigned long addr, + unsigned int n) +{ + unsigned int s =3D PHYS_PFN(addr - kexec_mod_start); + unsigned int i; + + BUG_ON(addr < kexec_mod_start || + addr + PFN_PHYS(n) > (unsigned long)mod_ptr); + + for ( i =3D 0; i < n; i++ ) + { + BUG_ON(mod_ptr->pg2rec[s + i] !=3D recid); + mod_ptr->pg2rec[s + i] =3D KEXECMOD_PG_FREE; + } +} + +int kexec_find_mod_record(unsigned int start_idx, unsigned int type, + unsigned int *size) +{ + unsigned int i; + + for ( i =3D start_idx; i < mod_ptr->n_records; i++ ) + { + if ( mod_recs[i].type =3D=3D type ) + { + *size =3D mod_recs[i].size; + return i; + } + } + + return -ENOENT; +} + +int kexec_add_mod_record(unsigned int type, void *addr, unsigned int size) +{ + unsigned int i; + + if ( mod_rec_end + size > (char *)mod_ptr + PAGE_SIZE ) + { + /* TODO: support compressing record space. */ + printk("Kexec module record space exhausted\n"); + BUG(); + } + + for ( i =3D 0; i < mod_ptr->n_records; i++ ) + { + if ( mod_recs[i].type =3D=3D KEXECMOD_REC_NONE ) + { + mod_recs[i].offset =3D mod_rec_end - (char *)mod_ptr; + mod_recs[i].type =3D type; + mod_recs[i].size =3D size; + memcpy(mod_rec_end, addr, size); + mod_rec_end =3D mod_rec_end + size; + + return i; + } + } + + /* TODO: support extending the mod_recs[] table. */ + printk("Kexec module record table exhausted\n"); + BUG(); +} + +int kexec_upd_mod_record(unsigned int idx, unsigned int type, + void *addr, unsigned int size) +{ + if ( idx >=3D mod_ptr->n_records ) + return -ENOENT; + + if ( mod_recs[idx].type !=3D type || mod_recs[idx].size !=3D size ) + return -EINVAL; + + memcpy((char *)mod_ptr + mod_recs[idx].offset, addr, size); + + return 0; +} + +int kexec_read_mod_record(unsigned int idx, void *addr, unsigned int size) +{ + if ( idx >=3D mod_ptr->n_records ) + return -ENOENT; + + if ( mod_recs[idx].size !=3D size ) + return -EINVAL; + + memcpy(addr, (char *)mod_ptr + mod_recs[idx].offset, size); + + return 0; +} --=20 2.43.0