From patchwork Wed Sep 18 09:22:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frediano Ziglio X-Patchwork-Id: 13806732 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 56CECCCD198 for ; Wed, 18 Sep 2024 09:23:23 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.800211.1210121 (Exim 4.92) (envelope-from ) id 1sqqtx-0001jF-VD; Wed, 18 Sep 2024 09:23:09 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 800211.1210121; Wed, 18 Sep 2024 09:23:09 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sqqtx-0001j8-Rf; Wed, 18 Sep 2024 09:23:09 +0000 Received: by outflank-mailman (input) for mailman id 800211; Wed, 18 Sep 2024 09:23:07 +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 1sqqtv-0001Ui-UP for xen-devel@lists.xenproject.org; Wed, 18 Sep 2024 09:23:07 +0000 Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [2a00:1450:4864:20::533]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 9cfd1f7d-759f-11ef-a0b8-8be0dac302b0; Wed, 18 Sep 2024 11:23:06 +0200 (CEST) Received: by mail-ed1-x533.google.com with SMTP id 4fb4d7f45d1cf-5c24c92f699so7028651a12.2 for ; Wed, 18 Sep 2024 02:23:06 -0700 (PDT) Received: from fziglio-desktop.. ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a906109718bsm559125666b.34.2024.09.18.02.23.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Sep 2024 02:23:05 -0700 (PDT) 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: 9cfd1f7d-759f-11ef-a0b8-8be0dac302b0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1726651386; x=1727256186; 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=GiqJyrFMaC770o/mcU/mGV/x9SPshXQY9Acm8HRzZDM=; b=HtolMe8kph3J9rBNu6i6X62frhe9f/ROs8cXEST2k+tjYvt9UG92PzNqapB/CWKwnz H2lWOt6gRNDF8Z/7U2JvLJus2GnjSS2O1SWCmw52r32d7cW4UFz+bF3EflZhW9fZ9M+H xDtyv/3vfins0DM47ddJW107IsiExMRQ8qzuQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726651386; x=1727256186; 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=GiqJyrFMaC770o/mcU/mGV/x9SPshXQY9Acm8HRzZDM=; b=OuzQghXVAbuxM5CLTqQKQWHrF5a8EwtrwL+wPpq/prBgxh75fybtRPCgvDWsqr1Cvf 6qUX8vTvZqXsnGMcsUy0USIDfd0W5SsKdXx0JAOrxhOC7Dn0aH2d+usP7VK4hxBUS/2b 3rsw55lWikm5NVVsLzi3yie/bAXY4xPKvLi8iiGXCybdlFjgGOusVhmPaM7gOz9xcDbs gfycjtpCCYLpuSPLjw25yaokL2seUDEnIFHZbLYoLLxdNJWK05yAKD58aLHpojNDMAMw E5wt50E7f6WQ4SM4noOqy9UQ7Qo3R76dwHe0ZaxlrQJ/r/7wyoZK122BZbuX9QemaXQI C8dw== X-Gm-Message-State: AOJu0YxaNgAATlUYUTgAyOYLDy6G7iluVV16y1AwEk/VnkLjuy7Kf8xg 3/qtL3SQuMLoj5YPL+NsvlnZTch3GXM+Royo5csaMfZ3zwk2cyPuRqo/jJM9vBlmudtvIIbAGyG 4 X-Google-Smtp-Source: AGHT+IGc5vre66vZUMWNspvmHfkOcMk9rCWuOrlth9OUNq0QoNaOKn1bvXqjbsoBeHYO28/9MPkxWg== X-Received: by 2002:a17:907:f14a:b0:a86:9ba1:639e with SMTP id a640c23a62f3a-a9029486e83mr2050128266b.26.1726651385486; Wed, 18 Sep 2024 02:23:05 -0700 (PDT) From: Frediano Ziglio To: xen-devel@lists.xenproject.org Cc: Frediano Ziglio , Jan Beulich , Andrew Cooper , =?utf-8?q?Roger_Pau_Monn=C3=A9?= Subject: [PATCH v2 1/4] x86/boot: Initialise BSS sooner Date: Wed, 18 Sep 2024 10:22:52 +0100 Message-Id: <20240918092255.13527-2-frediano.ziglio@cloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240918092255.13527-1-frediano.ziglio@cloud.com> References: <20240918092255.13527-1-frediano.ziglio@cloud.com> MIME-Version: 1.0 Allows to call C code earlier. In order to safely call C code we need to setup stack, selectors and BSS. Signed-off-by: Frediano Ziglio --- Changes since v1: - improve commit message; - improve some comments; - fix some code style (spacing); - set trampoline_phys as 32 bit value; - use PAGE_SIZE mnemonic instead of 0x1000; - use local label. --- xen/arch/x86/boot/head.S | 86 ++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 42 deletions(-) diff --git a/xen/arch/x86/boot/head.S b/xen/arch/x86/boot/head.S index 12bbb97f33..182b74e53f 100644 --- a/xen/arch/x86/boot/head.S +++ b/xen/arch/x86/boot/head.S @@ -231,6 +231,27 @@ __efi64_mb2_start: /* VGA is not available on EFI platforms. */ movl $0,vga_text_buffer(%rip) + /* + * Align the stack as UEFI spec requires. Keep it aligned + * before efi_multiboot2() call by pushing/popping even + * numbers of items on it. + */ + and $~15, %rsp + + /* + * Initialize BSS (no nasty surprises!). + * It must be done earlier than in BIOS case + * because efi_multiboot2() touches it. + */ + mov %eax, %edx + lea __bss_start(%rip), %edi + lea __bss_end(%rip), %ecx + sub %edi, %ecx + shr $3, %ecx + xor %eax, %eax + rep stosq + mov %edx, %eax + /* Check for Multiboot2 bootloader. */ cmp $MULTIBOOT2_BOOTLOADER_MAGIC,%eax je .Lefi_multiboot2_proto @@ -321,34 +342,12 @@ __efi64_mb2_start: lea .Lmb2_no_ih(%rip),%r15 jz x86_32_switch - /* - * Align the stack as UEFI spec requires. Keep it aligned - * before efi_multiboot2() call by pushing/popping even - * numbers of items on it. - */ - and $~15,%rsp - /* Save Multiboot2 magic on the stack. */ push %rax /* Save EFI ImageHandle on the stack. */ push %rdi - /* - * Initialize BSS (no nasty surprises!). - * It must be done earlier than in BIOS case - * because efi_multiboot2() touches it. - */ - lea __bss_start(%rip),%edi - lea __bss_end(%rip),%ecx - sub %edi,%ecx - shr $3,%ecx - xor %eax,%eax - rep stosq - - /* Keep the stack aligned. Do not pop a single item off it. */ - mov (%rsp),%rdi - /* * efi_multiboot2() is called according to System V AMD64 ABI: * - IN: %rdi - EFI ImageHandle, %rsi - EFI SystemTable, @@ -434,6 +433,8 @@ __pvh_start: /* Set up stack. */ lea STACK_SIZE - CPUINFO_sizeof + sym_esi(cpu0_stack), %esp + call .Linitialise_bss + mov %ebx, sym_esi(pvh_start_info_pa) /* Force xen console. Will revert to user choice in init code. */ @@ -459,6 +460,25 @@ __pvh_start: #endif /* CONFIG_PVH_GUEST */ +.Linitialise_bss: + /* + * Initialise the BSS. + * + * !!! WARNING - also zeroes the current stack !!! + */ + pop %ebp + mov %eax, %edx + + lea sym_esi(__bss_start), %edi + lea sym_esi(__bss_end), %ecx + sub %edi, %ecx + xor %eax, %eax + shr $2, %ecx + rep stosl + + mov %edx, %eax + jmp *%ebp + __start: cld cli @@ -489,6 +509,8 @@ __start: /* Set up stack. */ lea STACK_SIZE - CPUINFO_sizeof + sym_esi(cpu0_stack), %esp + call .Linitialise_bss + /* Bootloaders may set multiboot{1,2}.mem_lower to a nonzero value. */ xor %edx,%edx @@ -645,26 +667,6 @@ trampoline_setup: * reserved for trampoline code and data. */ - /* - * Do not zero BSS on EFI platform here. - * It was initialized earlier. - */ - cmpb $0, sym_esi(efi_platform) - jnz 1f - - /* - * Initialise the BSS. - * - * !!! WARNING - also zeroes the current stack !!! - */ - lea sym_esi(__bss_start), %edi - lea sym_esi(__bss_end), %ecx - sub %edi,%ecx - xor %eax,%eax - shr $2,%ecx - rep stosl - -1: /* Interrogate CPU extended features via CPUID. */ mov $1, %eax cpuid From patchwork Wed Sep 18 09:22:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frediano Ziglio X-Patchwork-Id: 13806731 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5593BCCD195 for ; Wed, 18 Sep 2024 09:23:22 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.800212.1210127 (Exim 4.92) (envelope-from ) id 1sqqty-0001mN-Ak; Wed, 18 Sep 2024 09:23:10 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 800212.1210127; Wed, 18 Sep 2024 09:23:10 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sqqty-0001l9-2C; Wed, 18 Sep 2024 09:23:10 +0000 Received: by outflank-mailman (input) for mailman id 800212; Wed, 18 Sep 2024 09:23:08 +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 1sqqtw-0001Ui-Ch for xen-devel@lists.xenproject.org; Wed, 18 Sep 2024 09:23:08 +0000 Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [2a00:1450:4864:20::533]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 9d442966-759f-11ef-a0b8-8be0dac302b0; Wed, 18 Sep 2024 11:23:07 +0200 (CEST) Received: by mail-ed1-x533.google.com with SMTP id 4fb4d7f45d1cf-5c25f01879fso7987047a12.1 for ; Wed, 18 Sep 2024 02:23:07 -0700 (PDT) Received: from fziglio-desktop.. ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a906109718bsm559125666b.34.2024.09.18.02.23.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Sep 2024 02:23:05 -0700 (PDT) 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: 9d442966-759f-11ef-a0b8-8be0dac302b0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1726651386; x=1727256186; 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=r01uCFOVhRmbJDw3bVCuq7NRFDQWjnUurZDGXfwV2lk=; b=ajVzej22duliM6qC9zye6SSulkWhsJrSDFKyP1wFpDifd1idF/qAheruDsYgy5VK7J 6G1HJzRe+kXaDbStn+624K9VVIEIrn9AIMl3x/1l8LQyKYY6DndxC2uewYUokY2TF6tK A4alOAoqfOyh9Zy3PvLXesRBmtfRifvuivOdY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726651386; x=1727256186; 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=r01uCFOVhRmbJDw3bVCuq7NRFDQWjnUurZDGXfwV2lk=; b=DqOV//NeSwEhD7+YMH91l7CjfD1Kp7ZgSGbkisO0vDrINOmhhMEwX6eQPgq/sJkkiP gmyRNOQlllRXwxr6XQIgNI+fOBVmvFEAWa4hPqHCGQvxP1PcZjJqZYxipwrHp59wgOTK 3uaB03ymxLfTrElLtVX+Gpm4T2rCetYmAjXkBwoidBr761HpRq5GVp4ms76QuGb5HcoX xNd70UVrg5Nyh4lCITGeyxCp+WBl5pKoqa5aQNzZewnPS3ydhqubjwbmf0mEIpCmh96W GCGCk0maIC8DLCXhihHwsDf37WT+oFK/PFIeVU5khpGX0lFVohnShHzyyZoaYk8S5hSQ iC5g== X-Gm-Message-State: AOJu0YwRgGBqFpUk4fiS0qqLrlmPOpUp9y0RX3r1BAxLBmKuReo3fkMw CmMCk5i+t43yHuHdfbJ/7V4MMJWB3iEk1IWK9pQpGKoBIWEsfiOKetwOhJFGLyI3t9SYsWqDQvJ R X-Google-Smtp-Source: AGHT+IHPinPyYO6ZCTy5tvfrTnyPhdSSEqD+0CygaRoCx04yxCB7QsROO+h2sp0aKDEsU3DFy7IXDA== X-Received: by 2002:a17:907:868e:b0:a86:743a:a716 with SMTP id a640c23a62f3a-a902966bc54mr2122605366b.53.1726651386194; Wed, 18 Sep 2024 02:23:06 -0700 (PDT) From: Frediano Ziglio To: xen-devel@lists.xenproject.org Cc: Frediano Ziglio , Jan Beulich , Andrew Cooper , =?utf-8?q?Roger_Pau_Monn=C3=A9?= Subject: [PATCH v2 2/4] x86/boot: Refactor BIOS/PVH start Date: Wed, 18 Sep 2024 10:22:53 +0100 Message-Id: <20240918092255.13527-3-frediano.ziglio@cloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240918092255.13527-1-frediano.ziglio@cloud.com> References: <20240918092255.13527-1-frediano.ziglio@cloud.com> MIME-Version: 1.0 The 2 code paths were sharing quite some common code, reuse it instead of having duplications. Use %dl register to store boot type before running common code. Using a 8 bit register reduces code size. Signed-off-by: Frediano Ziglio --- Changes since v1: - use %dl instead of %ebp to reduce code size; - fold cli instruction; - update some comments. --- xen/arch/x86/boot/head.S | 118 +++++++++++++++------------------------ 1 file changed, 45 insertions(+), 73 deletions(-) diff --git a/xen/arch/x86/boot/head.S b/xen/arch/x86/boot/head.S index 182b74e53f..f18c15c666 100644 --- a/xen/arch/x86/boot/head.S +++ b/xen/arch/x86/boot/head.S @@ -25,6 +25,9 @@ #define MB2_HT(name) (MULTIBOOT2_HEADER_TAG_##name) #define MB2_TT(name) (MULTIBOOT2_TAG_TYPE_##name) +#define BOOT_TYPE_BIOS 1 +#define BOOT_TYPE_PVH 2 + .macro mb2ht_args arg:req, args:vararg .long \arg .ifnb \args @@ -409,17 +412,31 @@ cs32_switch: ELFNOTE(Xen, XEN_ELFNOTE_PHYS32_ENTRY, .long sym_offs(__pvh_start)) __pvh_start: - cld + mov $BOOT_TYPE_PVH, %dl + jmp .Lcommon_bios_pvh +#endif /* CONFIG_PVH_GUEST */ + +__start: + mov $BOOT_TYPE_BIOS, %dl + +.Lcommon_bios_pvh: cli + cld /* - * We need one call (i.e. push) to determine the load address. See - * __start for a discussion on how to do this safely using the PVH - * info structure. + * Multiboot (both 1 and 2) and PVH specify the stack pointer as + * undefined. This is unhelpful for relocatable images, where one + * call (i.e. push) is required to calculate the image's load address. + * + * Durig BIOS boot, there is one area of memory we know about with + * reasonable confidence that it isn't overlapped by Xen, and that's + * the Multiboot info structure in %ebx. Use it as a temporary stack. + * + * During PVH boot use info structure in %ebx. */ /* Preserve the field we're about to clobber. */ - mov (%ebx), %edx + mov (%ebx), %ecx lea 4(%ebx), %esp /* Calculate the load base address. */ @@ -428,19 +445,12 @@ __pvh_start: sub $sym_offs(1b), %esi /* Restore the clobbered field. */ - mov %edx, (%ebx) + mov %ecx, (%ebx) /* Set up stack. */ lea STACK_SIZE - CPUINFO_sizeof + sym_esi(cpu0_stack), %esp - call .Linitialise_bss - - mov %ebx, sym_esi(pvh_start_info_pa) - - /* Force xen console. Will revert to user choice in init code. */ - movb $-1, sym_esi(opt_console_xen) - - /* Prepare gdt and segments */ + /* Initialize GDTR and basic data segments. */ add %esi, sym_esi(gdt_boot_base) lgdt sym_esi(gdt_boot_descr) @@ -449,67 +459,44 @@ __pvh_start: mov %ecx, %es mov %ecx, %ss - /* Skip bootloader setup and bios setup, go straight to trampoline */ - movb $1, sym_esi(pvh_boot) - movb $1, sym_esi(skip_realmode) - - /* Set trampoline_phys to use mfn 1 to avoid having a mapping at VA 0 */ - movw $0x1000, sym_esi(trampoline_phys) - mov (%ebx), %eax /* mov $XEN_HVM_START_MAGIC_VALUE, %eax */ - jmp trampoline_setup - -#endif /* CONFIG_PVH_GUEST */ + /* Load null selector to unused segment registers. */ + xor %ecx, %ecx + mov %ecx, %fs + mov %ecx, %gs -.Linitialise_bss: /* * Initialise the BSS. * * !!! WARNING - also zeroes the current stack !!! */ - pop %ebp - mov %eax, %edx - + mov %eax, %ebp lea sym_esi(__bss_start), %edi lea sym_esi(__bss_end), %ecx sub %edi, %ecx xor %eax, %eax shr $2, %ecx rep stosl + mov %ebp, %eax - mov %edx, %eax - jmp *%ebp - -__start: - cld - cli - - /* - * Multiboot (both 1 and 2) specify the stack pointer as undefined - * when entering in BIOS circumstances. This is unhelpful for - * relocatable images, where one call (i.e. push) is required to - * calculate the image's load address. - * - * This early in boot, there is one area of memory we know about with - * reasonable confidence that it isn't overlapped by Xen, and that's - * the Multiboot info structure in %ebx. Use it as a temporary stack. - */ - - /* Preserve the field we're about to clobber. */ - mov (%ebx), %edx - lea 4(%ebx), %esp +#ifdef CONFIG_PVH_GUEST + cmp $BOOT_TYPE_PVH, %dl + jne 1f - /* Calculate the load base address. */ - call 1f -1: pop %esi - sub $sym_offs(1b), %esi + mov %ebx, sym_esi(pvh_start_info_pa) - /* Restore the clobbered field. */ - mov %edx, (%ebx) + /* Force xen console. Will revert to user choice in init code. */ + movb $-1, sym_esi(opt_console_xen) - /* Set up stack. */ - lea STACK_SIZE - CPUINFO_sizeof + sym_esi(cpu0_stack), %esp + /* Skip bootloader setup and bios setup, go straight to trampoline */ + movb $1, sym_esi(pvh_boot) + movb $1, sym_esi(skip_realmode) - call .Linitialise_bss + /* Set trampoline_phys to use mfn 1 to avoid having a mapping at VA 0 */ + movl $PAGE_SIZE, sym_esi(trampoline_phys) + mov (%ebx), %eax /* mov $XEN_HVM_START_MAGIC_VALUE, %eax */ + jmp trampoline_setup +1: +#endif /* CONFIG_PVH_GUEST */ /* Bootloaders may set multiboot{1,2}.mem_lower to a nonzero value. */ xor %edx,%edx @@ -568,22 +555,7 @@ __start: trampoline_bios_setup: /* * Called on legacy BIOS platforms only. - * - * Initialize GDTR and basic data segments. */ - add %esi,sym_esi(gdt_boot_base) - lgdt sym_esi(gdt_boot_descr) - - mov $BOOT_DS,%ecx - mov %ecx,%ds - mov %ecx,%es - mov %ecx,%ss - /* %esp is initialized later. */ - - /* Load null descriptor to unused segment registers. */ - xor %ecx,%ecx - mov %ecx,%fs - mov %ecx,%gs /* Set up trampoline segment 64k below EBDA */ movzwl 0x40e,%ecx /* EBDA segment */ From patchwork Wed Sep 18 09:22:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frediano Ziglio X-Patchwork-Id: 13806733 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CDB7FCCD194 for ; Wed, 18 Sep 2024 09:23:22 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.800213.1210133 (Exim 4.92) (envelope-from ) id 1sqqty-0001wN-QT; Wed, 18 Sep 2024 09:23:10 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 800213.1210133; Wed, 18 Sep 2024 09:23:10 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sqqty-0001uO-KW; Wed, 18 Sep 2024 09:23:10 +0000 Received: by outflank-mailman (input) for mailman id 800213; Wed, 18 Sep 2024 09:23:09 +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 1sqqtx-0001Ui-Cz for xen-devel@lists.xenproject.org; Wed, 18 Sep 2024 09:23:09 +0000 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [2a00:1450:4864:20::631]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 9df06ad2-759f-11ef-a0b8-8be0dac302b0; Wed, 18 Sep 2024 11:23:08 +0200 (CEST) Received: by mail-ej1-x631.google.com with SMTP id a640c23a62f3a-a8ce5db8668so59136566b.1 for ; Wed, 18 Sep 2024 02:23:08 -0700 (PDT) Received: from fziglio-desktop.. ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a906109718bsm559125666b.34.2024.09.18.02.23.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Sep 2024 02:23:06 -0700 (PDT) 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: 9df06ad2-759f-11ef-a0b8-8be0dac302b0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1726651387; x=1727256187; 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=1ui1cggI59NaiSofWslRY4D04lkLcdTxHVYAhI74Y5M=; b=AAjhFr/7z24io8HN/VBclSWwcEgvVpKbXbiMMRnxUziAT+GgVgZBrjeaHx8KSvqfEk 4uolLd/XhFhDCpvlP9eI0Q78Q83eZcwg8NZvIElzAj/W2qkf5fCsBIsrNv7J1liavSMg BFysuI6wgczM4XK2ZlqFDd7rG7a0/x62/gQGI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726651387; x=1727256187; 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=1ui1cggI59NaiSofWslRY4D04lkLcdTxHVYAhI74Y5M=; b=GmgpHjKTwNtslQ617vIBtjS8weekecxKBriz54lQDx0GMma3o0riKHSmGpk9T702kK P2hvAqRlBowCHrVQWF6MaKtXtIZ7v0CIWM/LVNK3spchW0wAex0Cfb1PaPQvcxS/0K78 5kfTderxn/fSzmiAlmamWXa6Ddl8jq3EFYf3bonpNRxq/rtL19zrbJqZ2NQvQBPWpH0w gFJroC1y2hNGY2ljKAJRTihYRJJN3nEN8xuLJK6LRT+FaiKD9uIjwR7rY5qXzvKN9ytJ KBru/db/7DYhSYMbH0hmCpRJ+gwwU08kH7Jb3m9Tt+h26JR3y1iu3DVUJWCpTmMy+qiQ D8qg== X-Gm-Message-State: AOJu0YxgyPvJhn/Sni5T/A+pSc1gKwjJIRgtFFGSt8Jll1FP403CoeM/ jxwhKb7P3N+5mnEXFpZqK/GrwQohUd1PSqpXW5AqSoFMTUKFaAs7UVGWaLP/Rbu9W0XVdujvNNq J X-Google-Smtp-Source: AGHT+IGjZx4zoQ/6n68ITL6lJjNTaqv4+mbRP+e4NqqKtrUd9mXaP9LaapcYW4PWWz0Mb3t5sBF/tA== X-Received: by 2002:a17:907:f72a:b0:a80:7193:bd93 with SMTP id a640c23a62f3a-a9029447148mr2251621066b.25.1726651387097; Wed, 18 Sep 2024 02:23:07 -0700 (PDT) From: Frediano Ziglio To: xen-devel@lists.xenproject.org Cc: Frediano Ziglio , Jan Beulich , Andrew Cooper , =?utf-8?q?Roger_Pau_Monn=C3=A9?= , "Daniel P. Smith" , =?utf-8?q?Marek_Marczykow?= =?utf-8?q?ski-G=C3=B3recki?= Subject: [PATCH v2 3/4] x86/boot: Rewrite EFI/MBI2 code partly in C Date: Wed, 18 Sep 2024 10:22:54 +0100 Message-Id: <20240918092255.13527-4-frediano.ziglio@cloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240918092255.13527-1-frediano.ziglio@cloud.com> References: <20240918092255.13527-1-frediano.ziglio@cloud.com> MIME-Version: 1.0 No need to have it coded in assembly. Signed-off-by: Frediano Ziglio --- Changes since v1: - update some comments; - explain why %ebx is saved before calling efi_parse_mbi2; - move lea before test instruction; - removed asmlinkage from efi_multiboot2 and add to efi_parse_mbi2; - fix line length; - update an error message specifying "Multiboot2" instead of "Multiboot"; - use obj-bin-X instead of obj-X in Makefile; - avoid restoring %eax (MBI magic). --- xen/arch/x86/boot/head.S | 136 +++++++--------------------------- xen/arch/x86/efi/Makefile | 1 + xen/arch/x86/efi/efi-boot.h | 6 +- xen/arch/x86/efi/parse-mbi2.c | 58 +++++++++++++++ xen/arch/x86/efi/stub.c | 3 +- 5 files changed, 89 insertions(+), 115 deletions(-) create mode 100644 xen/arch/x86/efi/parse-mbi2.c diff --git a/xen/arch/x86/boot/head.S b/xen/arch/x86/boot/head.S index f18c15c666..30fbacf709 100644 --- a/xen/arch/x86/boot/head.S +++ b/xen/arch/x86/boot/head.S @@ -122,8 +122,6 @@ multiboot2_header: .Lbad_cpu_msg: .asciz "ERR: Not a 64-bit CPU!" .Lbad_ldr_msg: .asciz "ERR: Not a Multiboot bootloader!" .Lbad_ldr_nbs: .asciz "ERR: Bootloader shutdown EFI x64 boot services!" -.Lbad_ldr_nst: .asciz "ERR: EFI SystemTable is not provided by bootloader!" -.Lbad_ldr_nih: .asciz "ERR: EFI ImageHandle is not provided by bootloader!" .Lbad_efi_msg: .asciz "ERR: EFI IA-32 platforms are not supported!" .Lbag_alg_msg: .asciz "ERR: Xen must be loaded at a 2Mb boundary!" .Lno_nx_msg: .asciz "ERR: Not an NX-capable CPU!" @@ -162,17 +160,6 @@ early_error: /* Here to improve the disassembly. */ mov $sym_offs(.Lno_nx_msg), %ecx jmp .Lget_vtb #endif -.Lmb2_no_st: - /* - * Here we are on EFI platform. vga_text_buffer was zapped earlier - * because there is pretty good chance that VGA is unavailable. - */ - mov $sym_offs(.Lbad_ldr_nst), %ecx - jmp .Lget_vtb -.Lmb2_no_ih: - /* Ditto. */ - mov $sym_offs(.Lbad_ldr_nih), %ecx - jmp .Lget_vtb .Lmb2_no_bs: /* * Ditto. Additionally, here there is a chance that Xen was started @@ -190,6 +177,10 @@ early_error: /* Here to improve the disassembly. */ mov $sym_offs(.Lbad_efi_msg), %ecx xor %edi,%edi # No VGA text buffer jmp .Lprint_err +.Ldirect_error: + mov sym_esi(vga_text_buffer), %edi + mov %eax, %esi + jmp 1f .Lget_vtb: mov sym_esi(vga_text_buffer), %edi .Lprint_err: @@ -236,7 +227,7 @@ __efi64_mb2_start: /* * Align the stack as UEFI spec requires. Keep it aligned - * before efi_multiboot2() call by pushing/popping even + * before efi_parse_mbi2() call by pushing/popping even * numbers of items on it. */ and $~15, %rsp @@ -244,7 +235,7 @@ __efi64_mb2_start: /* * Initialize BSS (no nasty surprises!). * It must be done earlier than in BIOS case - * because efi_multiboot2() touches it. + * because efi_parse_mbi2() touches it. */ mov %eax, %edx lea __bss_start(%rip), %edi @@ -253,36 +244,30 @@ __efi64_mb2_start: shr $3, %ecx xor %eax, %eax rep stosq - mov %edx, %eax - /* Check for Multiboot2 bootloader. */ - cmp $MULTIBOOT2_BOOTLOADER_MAGIC,%eax - je .Lefi_multiboot2_proto - - /* Jump to .Lnot_multiboot after switching CPU to x86_32 mode. */ - lea .Lnot_multiboot(%rip), %r15 - jmp x86_32_switch + /* Save Multiboot2 magic on the stack. */ + push %rdx -.Lefi_multiboot2_proto: - /* Zero EFI SystemTable, EFI ImageHandle addresses and cmdline. */ - xor %esi,%esi - xor %edi,%edi - xor %edx,%edx + /* Save Multiboot2 pointer on the stack, keep the stack aligned. */ + push %rbx - /* Skip Multiboot2 information fixed part. */ - lea (MB2_fixed_sizeof+MULTIBOOT2_TAG_ALIGN-1)(%rbx),%ecx - and $~(MULTIBOOT2_TAG_ALIGN-1),%ecx + /* + * efi_parse_mbi2() is called according to System V AMD64 ABI: + * - IN: %edi - Multiboot2 magic, %rsi - Multiboot2 pointer. + * - OUT: %rax - error string. + */ + mov %edx, %edi + mov %rbx, %rsi + call efi_parse_mbi2 + lea .Ldirect_error(%rip), %r15 + test %rax, %rax + jnz x86_32_switch -.Lefi_mb2_tsize: - /* Check Multiboot2 information total size. */ - mov %ecx,%r8d - sub %ebx,%r8d - cmp %r8d,MB2_fixed_total_size(%rbx) - jbe .Lrun_bs + /* Restore Multiboot2 pointer. */ + pop %rbx - /* Are EFI boot services available? */ - cmpl $MULTIBOOT2_TAG_TYPE_EFI_BS,MB2_tag_type(%rcx) - jne .Lefi_mb2_st + /* Restore Multiboot2 magic. */ + pop %rax /* We are on EFI platform and EFI boot services are available. */ incb efi_platform(%rip) @@ -292,77 +277,6 @@ __efi64_mb2_start: * be run on EFI platforms. */ incb skip_realmode(%rip) - jmp .Lefi_mb2_next_tag - -.Lefi_mb2_st: - /* Get EFI SystemTable address from Multiboot2 information. */ - cmpl $MULTIBOOT2_TAG_TYPE_EFI64,MB2_tag_type(%rcx) - cmove MB2_efi64_st(%rcx),%rsi - je .Lefi_mb2_next_tag - - /* Get EFI ImageHandle address from Multiboot2 information. */ - cmpl $MULTIBOOT2_TAG_TYPE_EFI64_IH,MB2_tag_type(%rcx) - cmove MB2_efi64_ih(%rcx),%rdi - je .Lefi_mb2_next_tag - - /* Get command line from Multiboot2 information. */ - cmpl $MULTIBOOT2_TAG_TYPE_CMDLINE, MB2_tag_type(%rcx) - jne .Lno_cmdline - lea MB2_tag_string(%rcx), %rdx - jmp .Lefi_mb2_next_tag -.Lno_cmdline: - - /* Is it the end of Multiboot2 information? */ - cmpl $MULTIBOOT2_TAG_TYPE_END,MB2_tag_type(%rcx) - je .Lrun_bs - -.Lefi_mb2_next_tag: - /* Go to next Multiboot2 information tag. */ - add MB2_tag_size(%rcx),%ecx - add $(MULTIBOOT2_TAG_ALIGN-1),%ecx - and $~(MULTIBOOT2_TAG_ALIGN-1),%ecx - jmp .Lefi_mb2_tsize - -.Lrun_bs: - /* Are EFI boot services available? */ - cmpb $0,efi_platform(%rip) - - /* Jump to .Lmb2_no_bs after switching CPU to x86_32 mode. */ - lea .Lmb2_no_bs(%rip),%r15 - jz x86_32_switch - - /* Is EFI SystemTable address provided by boot loader? */ - test %rsi,%rsi - - /* Jump to .Lmb2_no_st after switching CPU to x86_32 mode. */ - lea .Lmb2_no_st(%rip),%r15 - jz x86_32_switch - - /* Is EFI ImageHandle address provided by boot loader? */ - test %rdi,%rdi - - /* Jump to .Lmb2_no_ih after switching CPU to x86_32 mode. */ - lea .Lmb2_no_ih(%rip),%r15 - jz x86_32_switch - - /* Save Multiboot2 magic on the stack. */ - push %rax - - /* Save EFI ImageHandle on the stack. */ - push %rdi - - /* - * efi_multiboot2() is called according to System V AMD64 ABI: - * - IN: %rdi - EFI ImageHandle, %rsi - EFI SystemTable, - * %rdx - MB2 cmdline - */ - call efi_multiboot2 - - /* Just pop an item from the stack. */ - pop %rax - - /* Restore Multiboot2 magic. */ - pop %rax /* Jump to trampoline_setup after switching CPU to x86_32 mode. */ lea trampoline_setup(%rip),%r15 diff --git a/xen/arch/x86/efi/Makefile b/xen/arch/x86/efi/Makefile index 24dfecfad1..51140061fc 100644 --- a/xen/arch/x86/efi/Makefile +++ b/xen/arch/x86/efi/Makefile @@ -14,5 +14,6 @@ $(addprefix $(obj)/,$(EFIOBJ-y)): CFLAGS_stack_boundary := $(cflags-stack-bounda obj-y := common-stub.o stub.o obj-$(XEN_BUILD_EFI) := $(filter-out %.init.o,$(EFIOBJ-y)) obj-bin-$(XEN_BUILD_EFI) := $(filter %.init.o,$(EFIOBJ-y)) +obj-bin-y += parse-mbi2.o extra-$(XEN_BUILD_EFI) += buildid.o relocs-dummy.o nocov-$(XEN_BUILD_EFI) += stub.o diff --git a/xen/arch/x86/efi/efi-boot.h b/xen/arch/x86/efi/efi-boot.h index 7aa55e7aaf..859c01c13f 100644 --- a/xen/arch/x86/efi/efi-boot.h +++ b/xen/arch/x86/efi/efi-boot.h @@ -816,9 +816,9 @@ static const char *__init get_option(const char *cmd, const char *opt) return o; } -void asmlinkage __init efi_multiboot2(EFI_HANDLE ImageHandle, - EFI_SYSTEM_TABLE *SystemTable, - const char *cmdline) +void __init efi_multiboot2(EFI_HANDLE ImageHandle, + EFI_SYSTEM_TABLE *SystemTable, + const char *cmdline) { EFI_GRAPHICS_OUTPUT_PROTOCOL *gop; EFI_HANDLE gop_handle; diff --git a/xen/arch/x86/efi/parse-mbi2.c b/xen/arch/x86/efi/parse-mbi2.c new file mode 100644 index 0000000000..6038f35b16 --- /dev/null +++ b/xen/arch/x86/efi/parse-mbi2.c @@ -0,0 +1,58 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void __init efi_multiboot2(EFI_HANDLE ImageHandle, + EFI_SYSTEM_TABLE *SystemTable, + const char *cmdline); + +const char * asmlinkage __init +efi_parse_mbi2(uint32_t magic, const multiboot2_fixed_t *mbi) +{ + const multiboot2_tag_t *tag; + EFI_HANDLE ImageHandle = NULL; + EFI_SYSTEM_TABLE *SystemTable = NULL; + const char *cmdline = NULL; + bool have_bs = false; + + if ( magic != MULTIBOOT2_BOOTLOADER_MAGIC ) + return "ERR: Not a Multiboot2 bootloader!"; + + /* Skip Multiboot2 information fixed part. */ + tag = _p(ROUNDUP((unsigned long)(mbi + 1), MULTIBOOT2_TAG_ALIGN)); + + for ( ; (const void *)tag - (const void *)mbi < mbi->total_size + && tag->type != MULTIBOOT2_TAG_TYPE_END; + tag = _p(ROUNDUP((unsigned long)((const void *)tag + tag->size), + MULTIBOOT2_TAG_ALIGN)) ) + { + if ( tag->type == MULTIBOOT2_TAG_TYPE_EFI_BS ) + have_bs = true; + else if ( tag->type == MULTIBOOT2_TAG_TYPE_EFI64 ) + SystemTable = _p(((const multiboot2_tag_efi64_t *)tag)->pointer); + else if ( tag->type == MULTIBOOT2_TAG_TYPE_EFI64_IH ) + ImageHandle = _p(((const multiboot2_tag_efi64_ih_t *)tag)->pointer); + else if ( tag->type == MULTIBOOT2_TAG_TYPE_CMDLINE ) + cmdline = ((const multiboot2_tag_string_t *)tag)->string; + } + + if ( !have_bs ) + return "ERR: Bootloader shutdown EFI x64 boot services!"; + if ( !SystemTable ) + return "ERR: EFI SystemTable is not provided by bootloader!"; + if ( !ImageHandle ) + return "ERR: EFI ImageHandle is not provided by bootloader!"; + + efi_multiboot2(ImageHandle, SystemTable, cmdline); + + return NULL; +} diff --git a/xen/arch/x86/efi/stub.c b/xen/arch/x86/efi/stub.c index 2cd5c8d4dc..27d40964d5 100644 --- a/xen/arch/x86/efi/stub.c +++ b/xen/arch/x86/efi/stub.c @@ -17,7 +17,8 @@ */ void __init noreturn efi_multiboot2(EFI_HANDLE ImageHandle, - EFI_SYSTEM_TABLE *SystemTable) + EFI_SYSTEM_TABLE *SystemTable, + const char *cmdline) { static const CHAR16 __initconst err[] = L"Xen does not have EFI code build in!\r\nSystem halted!\r\n"; From patchwork Wed Sep 18 09:22:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frediano Ziglio X-Patchwork-Id: 13806730 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0EE90CCD196 for ; Wed, 18 Sep 2024 09:23:23 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.800214.1210151 (Exim 4.92) (envelope-from ) id 1sqqu0-0002QV-1r; Wed, 18 Sep 2024 09:23:12 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 800214.1210151; Wed, 18 Sep 2024 09:23:11 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sqqtz-0002QC-U5; Wed, 18 Sep 2024 09:23:11 +0000 Received: by outflank-mailman (input) for mailman id 800214; Wed, 18 Sep 2024 09:23:10 +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 1sqqty-0001Ui-D4 for xen-devel@lists.xenproject.org; Wed, 18 Sep 2024 09:23:10 +0000 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [2a00:1450:4864:20::529]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 9e41cb69-759f-11ef-a0b8-8be0dac302b0; Wed, 18 Sep 2024 11:23:08 +0200 (CEST) Received: by mail-ed1-x529.google.com with SMTP id 4fb4d7f45d1cf-5c413cf5de5so9355136a12.0 for ; Wed, 18 Sep 2024 02:23:08 -0700 (PDT) Received: from fziglio-desktop.. ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a906109718bsm559125666b.34.2024.09.18.02.23.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Sep 2024 02:23:07 -0700 (PDT) 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: 9e41cb69-759f-11ef-a0b8-8be0dac302b0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1726651388; x=1727256188; 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=76bJmVRgoyuRftic5KQh8KmD08jovx2iL9DhRm4U/lo=; b=F2nRc1UjgFTmESZydi8vAqfSPyuCBK8F1rBh7FuuzgtWdH3LPGjPSB4uWEWhToYllt daBaHEEZ1qJgbgDUTebOg0ZTumsVAl7efij3yGCv9H9Tcgqax97sv19uH0zS+acvTGZ0 1jNyVBT0JcFQHIT3ASDz86m50BZ84jKxUQiJc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726651388; x=1727256188; 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=76bJmVRgoyuRftic5KQh8KmD08jovx2iL9DhRm4U/lo=; b=Zqw5xKVNB6500XIuyXtt/wf40APVF4wbvBoAH6icJkXvMXe1ib4Z5v1RqUFRtn2DfY ZmNcrS4NIx5pyxhndj/gwxCXto6wb9YNKTpimsUPHjH+ALaqw4O9c6wFw8QLfnYBO7K2 4fjQHZXjpF5gAGMZnP7zFyFanFLaOIr2wWi836qrrSyj+sI5oYkeFnUswLzcfpMuosET CEJn9eu847h/PfuvO5QcivBYU3JPw28BR+VdEXSMOJdTkZ/zX8lVhyN1GbOKPkMAbcO6 yzAY2WuCiO/zsuy54gDpIbk3cnaa0mcCYG05jUHMGEmaYfINq0YVS7by+NZ+50sMolpN AV4w== X-Gm-Message-State: AOJu0YyukCVI5SNfgxCv+V0NB9kS2vX2+zAdkzvaqCtIc6CwHp/PF/5X sSLh3li0qKAWR4GGtVlE0KuPaMjZbv96h5TLlmTKdXpD2pqy8J1Q0x0pYnp9Z777lasmtSWewzj v X-Google-Smtp-Source: AGHT+IFEp/u+VUPtxAjeVGb1u/u7pp2Nj5RBUYuw+I7Zrj1Fea0oc1XRWYT5UzIgnFUvu1BdOycgaw== X-Received: by 2002:a17:907:f703:b0:a8a:7f08:97a6 with SMTP id a640c23a62f3a-a902943932amr2051093266b.24.1726651387902; Wed, 18 Sep 2024 02:23:07 -0700 (PDT) From: Frediano Ziglio To: xen-devel@lists.xenproject.org Cc: Frediano Ziglio , "Daniel P. Smith" , =?utf-8?q?Marek_Marczykow?= =?utf-8?q?ski-G=C3=B3recki?= , Jan Beulich , Andrew Cooper , =?utf-8?q?Roger_Pau_Monn=C3=A9?= Subject: [PATCH v2 4/4] x86/boot: Improve MBI2 structure check Date: Wed, 18 Sep 2024 10:22:55 +0100 Message-Id: <20240918092255.13527-5-frediano.ziglio@cloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240918092255.13527-1-frediano.ziglio@cloud.com> References: <20240918092255.13527-1-frediano.ziglio@cloud.com> MIME-Version: 1.0 Tag structure should contain at least the tag header. Entire tag structure must be contained inside MBI2 data. Signed-off-by: Frediano Ziglio --- xen/arch/x86/efi/parse-mbi2.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/xen/arch/x86/efi/parse-mbi2.c b/xen/arch/x86/efi/parse-mbi2.c index 6038f35b16..7efda8fab2 100644 --- a/xen/arch/x86/efi/parse-mbi2.c +++ b/xen/arch/x86/efi/parse-mbi2.c @@ -22,6 +22,7 @@ efi_parse_mbi2(uint32_t magic, const multiboot2_fixed_t *mbi) EFI_HANDLE ImageHandle = NULL; EFI_SYSTEM_TABLE *SystemTable = NULL; const char *cmdline = NULL; + const void *const mbi_end = (const void *)mbi + mbi->total_size; bool have_bs = false; if ( magic != MULTIBOOT2_BOOTLOADER_MAGIC ) @@ -30,7 +31,9 @@ efi_parse_mbi2(uint32_t magic, const multiboot2_fixed_t *mbi) /* Skip Multiboot2 information fixed part. */ tag = _p(ROUNDUP((unsigned long)(mbi + 1), MULTIBOOT2_TAG_ALIGN)); - for ( ; (const void *)tag - (const void *)mbi < mbi->total_size + for ( ; (const void *)(tag + 1) <= mbi_end + && tag->size >= sizeof(*tag) + && (const void *)tag + tag->size <= mbi_end && tag->type != MULTIBOOT2_TAG_TYPE_END; tag = _p(ROUNDUP((unsigned long)((const void *)tag + tag->size), MULTIBOOT2_TAG_ALIGN)) )