From nobody Wed Dec 4 09:31:16 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=1725461857; cv=none; d=zohomail.com; s=zohoarc; b=jBb/OSB6o40pLH/Z4PpljngEUkRyLiHZCnSSUFjUMlkGCOhc8P3s6G3MdD+Q2wRoI1SuXK+TQvwyxMcK0TAaKnRKI4Zp0mw3DFUNOMxx+6lKzg1D8okRo1QACAi9OxUjff7GytqD9nMk5Lj/D60sElh0WXn/LY6qQ1QNOR08hIM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1725461857; 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=H2XLzta5aUPYVTAfd1K28dnU5rjnLpIfui7Cf5/r5KQ=; b=NH2pFETcGQBylCxq9nNuoCIumUvLAJuW/9+ceinwnzm8qrWwNeWPigdk7j3+ZQh1YOBZV5kM8jHPmAInhNSaeAmr8TeUdZ1NQpVCdQ2BwaKj4FUVNqLsLf7iz2osAbURtj6hqYlQGmvpSSZpvPrrJbACKUm6dqjnVwEewu7RusM= 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 1725461857446920.2179496382909; Wed, 4 Sep 2024 07:57:37 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.790436.1200183 (Exim 4.92) (envelope-from ) id 1slrRR-0001CA-Pg; Wed, 04 Sep 2024 14:57:05 +0000 Received: by outflank-mailman (output) from mailman id 790436.1200183; Wed, 04 Sep 2024 14:57:05 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1slrRR-0001C3-Mv; Wed, 04 Sep 2024 14:57:05 +0000 Received: by outflank-mailman (input) for mailman id 790436; Wed, 04 Sep 2024 14:57:03 +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 1slrRP-0001Be-QU for xen-devel@lists.xenproject.org; Wed, 04 Sep 2024 14:57:03 +0000 Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [2a00:1450:4864:20::531]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id f1a4bf85-6acd-11ef-a0b3-8be0dac302b0; Wed, 04 Sep 2024 16:57:02 +0200 (CEST) Received: by mail-ed1-x531.google.com with SMTP id 4fb4d7f45d1cf-5bef295a429so1070863a12.2 for ; Wed, 04 Sep 2024 07:57:02 -0700 (PDT) Received: from fziglio-xenia-fedora.eng.citrite.net ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5c3cc56c501sm18862a12.52.2024.09.04.07.57.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 07:57:01 -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: f1a4bf85-6acd-11ef-a0b3-8be0dac302b0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1725461822; x=1726066622; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=H2XLzta5aUPYVTAfd1K28dnU5rjnLpIfui7Cf5/r5KQ=; b=jP8ZevKMqmmu/mAMD0kGn4lr4Zo+qcxrEJfw4TaVrxEc7QMWGsXSEiIbricKrCogXi if09DwlKKuhBI6exLFSecbJE87cOOqUH0sAnC2w2kZxNx5CV/SpkzI5ZXRS+2v7i0dPY 3m/MW089iQXAYMdL6vtakwHMCe3HZJzIdZt/c= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725461822; x=1726066622; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=H2XLzta5aUPYVTAfd1K28dnU5rjnLpIfui7Cf5/r5KQ=; b=lBlNYQunj0Ky/eB6e+NQD4CZPUR69QhW6RfXAqvhejr5PDLc11mp8BMzUKRsdcQfS9 RQCiVoXy+l4KnUVV1BrSPO3QoIeTnzGqgOhyG5iuBo8thtrzuMA6woVViJ9+fp08aSzg 8zmO6cezKzzHx6BB6CwmjVsULPR5SoW8bBDAKZab6DWkv5MulVhMVF/cS1RAfvkAL/ZJ kN62Z1YOuLkLOe+XmTgxHQJ+pjdBejPzanjo8AR2/aC2nAvBun0ZRPoqOH9myKriR7cH rIQmIJMFPCmpZW/JIJFHdehodDyDAwkFSwH3106dOXIXqp0qNfYfXiZ/sHpR4zaNq5jw A0Sw== X-Gm-Message-State: AOJu0YxYq5mrK7jFNFh+2GnfTU4li1aIourUIyx6fdbl3c+XQESvwDmL Ltn7dmGOCKg6Jlp4k+FF6uw2/G8FcOBwDzhpvsu+NBtowOdobuvwo0qrjOOoQV018CsoZQGE0OW 4 X-Google-Smtp-Source: AGHT+IGqgeZCHMlbOlVzNCPm9aIs6RyyjIS1QCMGTLMuwlRgWzHc7PSzqeWrVWP56rm7TMUE30KSBw== X-Received: by 2002:a05:6402:210b:b0:5c2:5ca0:807a with SMTP id 4fb4d7f45d1cf-5c2caf22e50mr2018608a12.22.1725461821752; Wed, 04 Sep 2024 07:57:01 -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: [RFC 1/5] Avoid usage of global in reloc.c Date: Wed, 4 Sep 2024 15:56:44 +0100 Message-ID: <20240904145648.33707-2-frediano.ziglio@cloud.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240904145648.33707-1-frediano.ziglio@cloud.com> References: <20240904145648.33707-1-frediano.ziglio@cloud.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @cloud.com) X-ZM-MESSAGEID: 1725461858828116600 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 --- xen/arch/x86/boot/reloc.c | 62 ++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/xen/arch/x86/boot/reloc.c b/xen/arch/x86/boot/reloc.c index 201e38d544..de8487483a 100644 --- a/xen/arch/x86/boot/reloc.c +++ b/xen/arch/x86/boot/reloc.c @@ -72,11 +72,13 @@ 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 { + uint32_t alloc; +} 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->alloc -=3D ROUNDUP(bytes, 16); } =20 static void zero_mem(uint32_t s, uint32_t bytes) @@ -85,11 +87,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 +100,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 +110,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 +129,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 +185,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 +199,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 +224,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 +240,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 +265,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 +292,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 +358,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.46.0