From nobody Fri Oct 31 04:37:05 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=1751444505; cv=none; d=zohomail.com; s=zohoarc; b=nIdmWMl0UUeYDE84lumtNyQVuOqyKV35lV4QyNlB7QM+L420DiaN719oQ6KRSw7YaT/s+VaT/qsh7LWH8lyLNXjBHNGbsoctWEg868d9jRWSHAKybW+kGQ/hSL8KIuiH9pP4EE/c/XTHiXLj9LU+hwZ8MxSC87J6IiZXDWJIvNo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1751444505; 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=NPQ4KdPqGaFvUqDWb/++eBiyJkIUFURqS3mOYAtyiPM=; b=YlgS6WbNopL5LeujYrBx4VMW/6JRCPd1kBhQQ8+0sbI/IwJpvvi47CMGfkE+UbjO8BDmZa8Xe5x4SQy+ezHalAKIKelleYwtS94alOx8bd1tVAoqMN5+hnjhiuvuT/L7U6qlw0Bm5L7IjzNbwiyxTAgRqbFWgfr3wapwW3XPX4Q= 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 175144450553480.47403907842056; Wed, 2 Jul 2025 01:21:45 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1030561.1404304 (Exim 4.92) (envelope-from ) id 1uWsij-0003mu-I3; Wed, 02 Jul 2025 08:21:33 +0000 Received: by outflank-mailman (output) from mailman id 1030561.1404304; Wed, 02 Jul 2025 08:21:33 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uWsij-0003m6-4B; Wed, 02 Jul 2025 08:21:33 +0000 Received: by outflank-mailman (input) for mailman id 1030561; Wed, 02 Jul 2025 08:21:32 +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 1uWsbb-00082X-Vw for xen-devel@lists.xenproject.org; Wed, 02 Jul 2025 08:14:11 +0000 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 882321f2-571c-11f0-b894-0df219b8e170; Wed, 02 Jul 2025 10:14:10 +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 24EAB1F452; Wed, 2 Jul 2025 08:14:10 +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 EF13C13A24; Wed, 2 Jul 2025 08:14:09 +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 K/rrOFHqZGhoSQAAD6G6ig (envelope-from ); Wed, 02 Jul 2025 08:14:09 +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: 882321f2-571c-11f0-b894-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1751444050; 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=NPQ4KdPqGaFvUqDWb/++eBiyJkIUFURqS3mOYAtyiPM=; b=hXncPUa3E1HRPhmofLuq/RAN/uukbVkUMGFk8Gpex8fFSEypkOMdLbhxwQGrMYIqS911de 2HQgufJUD+6cLcHrDeJ6ckBKphxYI8auBN4aFbGSJJTQstRTjnSsGjXW/B/9JsEKuxe5LR igDgaQpAZMV6x2c6QO+xHzPccp0ZNJc= Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=hXncPUa3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1751444050; 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=NPQ4KdPqGaFvUqDWb/++eBiyJkIUFURqS3mOYAtyiPM=; b=hXncPUa3E1HRPhmofLuq/RAN/uukbVkUMGFk8Gpex8fFSEypkOMdLbhxwQGrMYIqS911de 2HQgufJUD+6cLcHrDeJ6ckBKphxYI8auBN4aFbGSJJTQstRTjnSsGjXW/B/9JsEKuxe5LR igDgaQpAZMV6x2c6QO+xHzPccp0ZNJc= 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 13/19] kexec: add support for allocating pages from kexec module memory Date: Wed, 2 Jul 2025 10:12:48 +0200 Message-ID: <20250702081254.14383-14-jgross@suse.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250702081254.14383-1-jgross@suse.com> References: <20250702081254.14383-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)[]; TO_DN_SOME(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; MIME_TRACE(0.00)[0:+]; ARC_NA(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]; RCVD_COUNT_TWO(0.00)[2]; FUZZY_BLOCKED(0.00)[rspamd.com]; 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)[]; DNSWL_BLOCKED(0.00)[2a07:de40:b281:106:10:150:64:167:received,2a07:de40:b281:104:10:150:64:97:from]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; RCPT_COUNT_THREE(0.00)[4]; DKIM_TRACE(0.00)[suse.com:+] X-Spam-Level: X-Spam-Flag: NO X-Rspamd-Queue-Id: 24EAB1F452 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: 1751444507421116600 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 7fb98473..8b3e841a 100644 --- a/arch/x86/kexec.c +++ b/arch/x86/kexec.c @@ -238,6 +238,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 ) @@ -255,11 +258,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) + CONFIG_KEXEC_MODULE_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 0200005f..1e753ee1 100644 --- a/include/kexec.h +++ b/include/kexec.h @@ -43,6 +43,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 { @@ -56,6 +59,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[]; @@ -105,4 +119,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