From nobody Thu Nov 21 15:02:08 2024 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=reject dis=none) header.from=cloud.com ARC-Seal: i=1; a=rsa-sha256; t=1725877895; cv=none; d=zohomail.com; s=zohoarc; b=mnnUdI1tsWZHGTfMyI5SwYGjEVA+fQM//zEDi1SAjrEqNkEjiBvjoEh6eHZDs0bZa9yHUSoSxEJPIPp6tBDBezng1h07fMBi8nA0csAbSbzZaZevf/jEe2ovWpFYV5+dWm/5eqCypY15kBjACm2GmvJA90NPjV8hC/SkJr02sqo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1725877895; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=bwRJoM0PMJv/f/AbKck9Y9EWTznz04JiqAuIcav9b18=; b=TtzdhOibHRkfQ2JRgk1Hxfc+Tu/O/j3s9aFo9Jz8mD+7x+CFv7gjj/lRjri3NMnVBovjmZIUZNOIDHwP8sATNpHnA1TbViyKcZ4drDna4dszfYeu+Nw1VG4Qi8bo2fePAo/LWgEocYzwyfh50H64JU3DOc6qixIYkF6TsTE18og= 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=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1725877895208109.67589824092306; Mon, 9 Sep 2024 03:31:35 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.794224.1203003 (Exim 4.92) (envelope-from ) id 1snbfz-0004HN-FD; Mon, 09 Sep 2024 10:31:19 +0000 Received: by outflank-mailman (output) from mailman id 794224.1203003; Mon, 09 Sep 2024 10:31: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 1snbfz-0004HG-CE; Mon, 09 Sep 2024 10:31:19 +0000 Received: by outflank-mailman (input) for mailman id 794224; Mon, 09 Sep 2024 10:31:17 +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 1snbfx-00046N-Op for xen-devel@lists.xenproject.org; Mon, 09 Sep 2024 10:31:17 +0000 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [2a00:1450:4864:20::52e]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id a4b2c9a2-6e96-11ef-99a1-01e77a169b0f; Mon, 09 Sep 2024 12:31:16 +0200 (CEST) Received: by mail-ed1-x52e.google.com with SMTP id 4fb4d7f45d1cf-5becd359800so3719959a12.0 for ; Mon, 09 Sep 2024 03:31:16 -0700 (PDT) Received: from fziglio-desktop.. ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a8d258318aesm319413366b.45.2024.09.09.03.31.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Sep 2024 03:31:14 -0700 (PDT) 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: a4b2c9a2-6e96-11ef-99a1-01e77a169b0f DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1725877875; x=1726482675; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=bwRJoM0PMJv/f/AbKck9Y9EWTznz04JiqAuIcav9b18=; b=D9Femfrjb4+NYmWf93mcHw4xB6ufQfksUYjOAsQoyowX54oYBJfBxJbjggI7hWL0MT bU+J2143ZyKL2pE2iVEs6cDdf+pleML+IIZj2TEWQQRau6HIn0+rEcACZ1mV6lmG/eBl 7FRutsKykbJ4/rv60XXgFAcKPgFSS5C2x5DtI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725877875; x=1726482675; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=bwRJoM0PMJv/f/AbKck9Y9EWTznz04JiqAuIcav9b18=; b=iVUzK+19/06mftegIvoNVXIb1xFkvHDjsuL82nTNXdijhRt3yNLu3MFTNWSFwYOf4a e1Uj+sYp2CiF7TYt+CqEqBOTccHRXADm/9pn24LvAPR/K2kfI1fSKD4TyAzDVc/dc2x5 YT8KhuugL9XrHXdeUP7ffFxpn60/hfx86Q8l7dChSkSVI9v5kOJvLMweWFdPYABgiPn1 Ho5URISjliZodssavOW3I+DvmPEJMQMK/CEtR5THoFSTU7x1M4gtGUVNfGlxQuYfhH2i a7gM7UCHuDW7zz2yEUtG57ai4CAzvPxS5yGwfEEhL07VJUK3ja/NZZBkKlO9OSmjjESR d3dQ== X-Gm-Message-State: AOJu0Yy9/z/ohmGyUN84pV5rAI/e+Hv4HyjXrUhOW1ayMvUjhaqUx7gR CLYn7i5yiqqEvPUIVsWOtpR37u9xrH0tetl+fEkQlOImg5wmzXShSxIA6FXLi+5uXbi22Sj+ful E X-Google-Smtp-Source: AGHT+IHtf1/81NLAPV9JzY8NybR80v9l89xpkG6kcr4yLMkGfgpLEMEtgHYuhz6x7N6jq7lLhMzzeQ== X-Received: by 2002:a17:907:7f03:b0:a8d:5f69:c854 with SMTP id a640c23a62f3a-a8d5f69cb55mr128417966b.24.1725877874683; Mon, 09 Sep 2024 03:31:14 -0700 (PDT) From: Frediano Ziglio To: xen-devel@lists.xenproject.org Cc: Frediano Ziglio , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Subject: [PATCH v3] x86/boot: Avoid usage of global in reloc.c Date: Mon, 9 Sep 2024 11:30:59 +0100 Message-Id: <20240909103059.54577-1-frediano.ziglio@cloud.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @cloud.com) X-ZM-MESSAGEID: 1725877896208116600 Content-Type: text/plain; charset="utf-8" All code and dat from this file will go into a text section which we want to not be writeable. Signed-off-by: Frediano Ziglio Reviewed-by: Andrew Cooper --- xen/arch/x86/boot/reloc.c | 67 +++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 30 deletions(-) --- Changes since v1: - add comment; - change "alloc" name to "ptr". Changes since v2: - fixed comment style. diff --git a/xen/arch/x86/boot/reloc.c b/xen/arch/x86/boot/reloc.c index 201e38d544..8c58affcd9 100644 --- a/xen/arch/x86/boot/reloc.c +++ b/xen/arch/x86/boot/reloc.c @@ -72,11 +72,18 @@ struct vesa_mode_info { #define get_mb2_data(tag, type, member) (((const multiboot2_tag_##type##= _t *)(tag))->member) #define get_mb2_string(tag, type, member) ((uint32_t)get_mb2_data(tag, typ= e, member)) =20 -static uint32_t alloc; +typedef struct memctx { + /* + * Simple bump allocator. + * + * It starts from the base of the trampoline and allocates downwards. + */ + uint32_t ptr; +} memctx; =20 -static uint32_t alloc_mem(uint32_t bytes) +static uint32_t alloc_mem(uint32_t bytes, memctx *ctx) { - return alloc -=3D ROUNDUP(bytes, 16); + return ctx->ptr -=3D ROUNDUP(bytes, 16); } =20 static void zero_mem(uint32_t s, uint32_t bytes) @@ -85,11 +92,11 @@ static void zero_mem(uint32_t s, uint32_t bytes) *(char *)s++ =3D 0; } =20 -static uint32_t copy_mem(uint32_t src, uint32_t bytes) +static uint32_t copy_mem(uint32_t src, uint32_t bytes, memctx *ctx) { uint32_t dst, dst_ret; =20 - dst =3D alloc_mem(bytes); + dst =3D alloc_mem(bytes, ctx); dst_ret =3D dst; =20 while ( bytes-- ) @@ -98,7 +105,7 @@ static uint32_t copy_mem(uint32_t src, uint32_t bytes) return dst_ret; } =20 -static uint32_t copy_string(uint32_t src) +static uint32_t copy_string(uint32_t src, memctx *ctx) { uint32_t p; =20 @@ -108,17 +115,17 @@ static uint32_t copy_string(uint32_t src) for ( p =3D src; *(char *)p !=3D '\0'; p++ ) continue; =20 - return copy_mem(src, p - src + 1); + return copy_mem(src, p - src + 1, ctx); } =20 -static struct hvm_start_info *pvh_info_reloc(uint32_t in) +static struct hvm_start_info *pvh_info_reloc(uint32_t in, memctx *ctx) { struct hvm_start_info *out; =20 - out =3D _p(copy_mem(in, sizeof(*out))); + out =3D _p(copy_mem(in, sizeof(*out), ctx)); =20 if ( out->cmdline_paddr ) - out->cmdline_paddr =3D copy_string(out->cmdline_paddr); + out->cmdline_paddr =3D copy_string(out->cmdline_paddr, ctx); =20 if ( out->nr_modules ) { @@ -127,51 +134,51 @@ static struct hvm_start_info *pvh_info_reloc(uint32_t= in) =20 out->modlist_paddr =3D copy_mem(out->modlist_paddr, - out->nr_modules * sizeof(struct hvm_modlist_entry)); + out->nr_modules * sizeof(struct hvm_modlist_entry), c= tx); =20 mods =3D _p(out->modlist_paddr); =20 for ( i =3D 0; i < out->nr_modules; i++ ) { if ( mods[i].cmdline_paddr ) - mods[i].cmdline_paddr =3D copy_string(mods[i].cmdline_padd= r); + mods[i].cmdline_paddr =3D copy_string(mods[i].cmdline_padd= r, ctx); } } =20 return out; } =20 -static multiboot_info_t *mbi_reloc(uint32_t mbi_in) +static multiboot_info_t *mbi_reloc(uint32_t mbi_in, memctx *ctx) { int i; multiboot_info_t *mbi_out; =20 - mbi_out =3D _p(copy_mem(mbi_in, sizeof(*mbi_out))); + mbi_out =3D _p(copy_mem(mbi_in, sizeof(*mbi_out), ctx)); =20 if ( mbi_out->flags & MBI_CMDLINE ) - mbi_out->cmdline =3D copy_string(mbi_out->cmdline); + mbi_out->cmdline =3D copy_string(mbi_out->cmdline, ctx); =20 if ( mbi_out->flags & MBI_MODULES ) { module_t *mods; =20 mbi_out->mods_addr =3D copy_mem(mbi_out->mods_addr, - mbi_out->mods_count * sizeof(module_= t)); + mbi_out->mods_count * sizeof(module_= t), ctx); =20 mods =3D _p(mbi_out->mods_addr); =20 for ( i =3D 0; i < mbi_out->mods_count; i++ ) { if ( mods[i].string ) - mods[i].string =3D copy_string(mods[i].string); + mods[i].string =3D copy_string(mods[i].string, ctx); } } =20 if ( mbi_out->flags & MBI_MEMMAP ) - mbi_out->mmap_addr =3D copy_mem(mbi_out->mmap_addr, mbi_out->mmap_= length); + mbi_out->mmap_addr =3D copy_mem(mbi_out->mmap_addr, mbi_out->mmap_= length, ctx); =20 if ( mbi_out->flags & MBI_LOADERNAME ) - mbi_out->boot_loader_name =3D copy_string(mbi_out->boot_loader_nam= e); + mbi_out->boot_loader_name =3D copy_string(mbi_out->boot_loader_nam= e, ctx); =20 /* Mask features we don't understand or don't relocate. */ mbi_out->flags &=3D (MBI_MEMLIMITS | @@ -183,7 +190,7 @@ static multiboot_info_t *mbi_reloc(uint32_t mbi_in) return mbi_out; } =20 -static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out) +static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out, m= emctx *ctx) { const multiboot2_fixed_t *mbi_fix =3D _p(mbi_in); const multiboot2_memory_map_t *mmap_src; @@ -197,7 +204,7 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, ui= nt32_t video_out) uint32_t ptr; unsigned int i, mod_idx =3D 0; =20 - ptr =3D alloc_mem(sizeof(*mbi_out)); + ptr =3D alloc_mem(sizeof(*mbi_out), ctx); mbi_out =3D _p(ptr); zero_mem(ptr, sizeof(*mbi_out)); =20 @@ -222,7 +229,7 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, ui= nt32_t video_out) * __start_xen() may put Xen image placement into it. */ mbi_out->mods_addr =3D alloc_mem((mbi_out->mods_count + 1) * - sizeof(*mbi_out_mods)); + sizeof(*mbi_out_mods), ctx); mbi_out_mods =3D _p(mbi_out->mods_addr); } =20 @@ -238,13 +245,13 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, = uint32_t video_out) case MULTIBOOT2_TAG_TYPE_BOOT_LOADER_NAME: mbi_out->flags |=3D MBI_LOADERNAME; ptr =3D get_mb2_string(tag, string, string); - mbi_out->boot_loader_name =3D copy_string(ptr); + mbi_out->boot_loader_name =3D copy_string(ptr, ctx); break; =20 case MULTIBOOT2_TAG_TYPE_CMDLINE: mbi_out->flags |=3D MBI_CMDLINE; ptr =3D get_mb2_string(tag, string, string); - mbi_out->cmdline =3D copy_string(ptr); + mbi_out->cmdline =3D copy_string(ptr, ctx); break; =20 case MULTIBOOT2_TAG_TYPE_BASIC_MEMINFO: @@ -263,7 +270,7 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, ui= nt32_t video_out) mbi_out->mmap_length /=3D get_mb2_data(tag, mmap, entry_size); mbi_out->mmap_length *=3D sizeof(*mmap_dst); =20 - mbi_out->mmap_addr =3D alloc_mem(mbi_out->mmap_length); + mbi_out->mmap_addr =3D alloc_mem(mbi_out->mmap_length, ctx); =20 mmap_src =3D get_mb2_data(tag, mmap, entries); mmap_dst =3D _p(mbi_out->mmap_addr); @@ -290,7 +297,7 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, ui= nt32_t video_out) mbi_out_mods[mod_idx].mod_start =3D get_mb2_data(tag, module, = mod_start); mbi_out_mods[mod_idx].mod_end =3D get_mb2_data(tag, module, mo= d_end); ptr =3D get_mb2_string(tag, module, cmdline); - mbi_out_mods[mod_idx].string =3D copy_string(ptr); + mbi_out_mods[mod_idx].string =3D copy_string(ptr, ctx); mbi_out_mods[mod_idx].reserved =3D 0; ++mod_idx; break; @@ -356,19 +363,19 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, = uint32_t video_out) void *reloc(uint32_t magic, uint32_t in, uint32_t trampoline, uint32_t video_info) { - alloc =3D trampoline; + memctx ctx =3D { trampoline }; =20 switch ( magic ) { case MULTIBOOT_BOOTLOADER_MAGIC: - return mbi_reloc(in); + return mbi_reloc(in, &ctx); =20 case MULTIBOOT2_BOOTLOADER_MAGIC: - return mbi2_reloc(in, video_info); + return mbi2_reloc(in, video_info, &ctx); =20 case XEN_HVM_START_MAGIC_VALUE: if ( IS_ENABLED(CONFIG_PVH_GUEST) ) - return pvh_info_reloc(in); + return pvh_info_reloc(in, &ctx); /* Fallthrough */ =20 default: --=20 2.34.1