From patchwork Tue Sep 10 16:16:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frediano Ziglio X-Patchwork-Id: 13798864 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 8EF94EDE9A2 for ; Tue, 10 Sep 2024 16:16:40 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.795864.1205340 (Exim 4.92) (envelope-from ) id 1so3XX-0007JT-Kz; Tue, 10 Sep 2024 16:16:27 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 795864.1205340; Tue, 10 Sep 2024 16:16:27 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1so3XX-0007JM-Hg; Tue, 10 Sep 2024 16:16:27 +0000 Received: by outflank-mailman (input) for mailman id 795864; Tue, 10 Sep 2024 16:16:26 +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 1so3XW-000753-2g for xen-devel@lists.xenproject.org; Tue, 10 Sep 2024 16:16:26 +0000 Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [2a00:1450:4864:20::52c]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 063e9062-6f90-11ef-a0b5-8be0dac302b0; Tue, 10 Sep 2024 18:16:24 +0200 (CEST) Received: by mail-ed1-x52c.google.com with SMTP id 4fb4d7f45d1cf-5c3ca32974fso6509442a12.3 for ; Tue, 10 Sep 2024 09:16:24 -0700 (PDT) Received: from fziglio-desktop.. ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5c3ebd468a1sm4397405a12.26.2024.09.10.09.16.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Sep 2024 09:16:22 -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: 063e9062-6f90-11ef-a0b5-8be0dac302b0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1725984983; x=1726589783; 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=batcHmj1wvcKkQ68lq1V9VDYqmGzG5ewJQtSr+95acw=; b=DR+MYK+XrRmLtVjfIAekFKdnJgf0oQ/Ev7hpdkMIlNv37PHQJXKiTYqM6SHH6xFbIz /js8oqEMVvUO0LkpThC0QpVo0Gw+Sykbgl6cKPXo19Wmqn03APQj2VvAIV1iGfeggFJs PeGNynjmXfuhTXICKaiG6IVhHu7fnSsBtr6XY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725984983; x=1726589783; 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=batcHmj1wvcKkQ68lq1V9VDYqmGzG5ewJQtSr+95acw=; b=QcThqueovCDdJHb7Mj+Q8LnKlSpM22zuORULNEG7bKtogrt5bWV6gq4OaxhS7zmiQF 6P1EMX5M0hRApSDfZMH983LXr/ZIQXaUfgiouGjerEO/JSwQlji6Xrqjx5OrEZ9ryLnv e8XfEsJmo7JdPya2/Affsr4W4DwVBfCZ0JakdyjYzqUdyp+b0r/YRytroqKZKgFn+VlV MrYpUPrAoTbb+PpNvSqz9qlL9WK1M73e23CB3h71pJ58tyq4skx3V2mZr67v4EV7U8eh 2DTplL4X9XVyciNlGPu6/yaGgJeN4gXTeVNzzivtTcAJxnaoVaDdCrH/moZvJOQi842T MdVg== X-Gm-Message-State: AOJu0Yz8iiQvkOETw/ig8bBDbyfH65dtYD+xGjTfDqkBizIYB7wJSEIj rFDxRw7uZZgyDowstvfIhYY2YOZ252PSdLEF6+vQX4zTFkz8oqFQSuC4nxt0Cm/lFu80mFYBYqI t X-Google-Smtp-Source: AGHT+IEueQV9OX917xYDvnV0h8yUCQls0OMJUKdHXADMz84yuF3Ja+eIi+9A++nbtuFbN0V27ThAtA== X-Received: by 2002:a05:6402:3884:b0:5c2:6343:8ddf with SMTP id 4fb4d7f45d1cf-5c3dc7c4eedmr9685586a12.33.1725984983385; Tue, 10 Sep 2024 09:16:23 -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 1/3] x86/boot: Initialise BSS as soon as possible Date: Tue, 10 Sep 2024 17:16:10 +0100 Message-Id: <20240910161612.242702-2-frediano.ziglio@cloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240910161612.242702-1-frediano.ziglio@cloud.com> References: <20240910161612.242702-1-frediano.ziglio@cloud.com> MIME-Version: 1.0 Allows to call C code earlier. Signed-off-by: Frediano Ziglio --- 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..de118d72f2 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 initialise_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 */ +initialise_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 initialise_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 Tue Sep 10 16:16:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frediano Ziglio X-Patchwork-Id: 13798866 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 33013EDE9A2 for ; Tue, 10 Sep 2024 16:16:43 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.795865.1205347 (Exim 4.92) (envelope-from ) id 1so3XY-0007R6-3U; Tue, 10 Sep 2024 16:16:28 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 795865.1205347; Tue, 10 Sep 2024 16:16:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1so3XX-0007PK-Ve; Tue, 10 Sep 2024 16:16:27 +0000 Received: by outflank-mailman (input) for mailman id 795865; Tue, 10 Sep 2024 16:16:26 +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 1so3XW-00073b-Tw for xen-devel@lists.xenproject.org; Tue, 10 Sep 2024 16:16:26 +0000 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [2a00:1450:4864:20::532]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 06ac1364-6f90-11ef-99a1-01e77a169b0f; Tue, 10 Sep 2024 18:16:25 +0200 (CEST) Received: by mail-ed1-x532.google.com with SMTP id 4fb4d7f45d1cf-5c26311c6f0so7151690a12.3 for ; Tue, 10 Sep 2024 09:16:25 -0700 (PDT) Received: from fziglio-desktop.. ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5c3ebd468a1sm4397405a12.26.2024.09.10.09.16.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Sep 2024 09:16:23 -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: 06ac1364-6f90-11ef-99a1-01e77a169b0f DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1725984984; x=1726589784; 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=733LuTti2vz+EFR0bf9WFVrkc1aXhrThkAewtQ4pEBM=; b=fFND9lh1eonI7MVJk7KSL+jucQB/1024/f0poR1BVZ1mR3MYpe8xkSiGe6ShJraipO GV7pineQyB6i1B6HKh+vd/MrdFjRCwjSzN2MR72tWSfVbybYAfO2XYh34A1OX4jHLEKc H0uj2dNfkI/NhSkuUk2xaJgMqDfExfAZaqODo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725984984; x=1726589784; 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=733LuTti2vz+EFR0bf9WFVrkc1aXhrThkAewtQ4pEBM=; b=lU8e03ZW0pbjfbVNJ68JD/7JiSCw8qaEdaFHqsTjvq8vjRmOKaCIwLCp9FqpEpHPRK b9YFOnmOTaca9++3Q57QHksqNilliYV6WBdM3YwE1jZrZsAhIWkbfprRp5oPc0rGXP5w Z364ds4zsOvkTsfsM1cN0wi3IJ10yq8DWWwoHIaE/jRVvNZUfwd7Z7KYoOBFxkYMYXsF tz/ECHHUrEmcXucvzhZkIRB+JjuwkP86vbei1b7idpxPbRLckvfdiJMJI2iqNWM8W6dz P3JejkhLhLQetHWAEHleTew9P0RzwTkwOWyLs8it5gfciBIt85KSK20zeMXnEH+Zbk14 NnlQ== X-Gm-Message-State: AOJu0Yy7k1A49St+sM+dQW2wP9ja0wsbKtLKgNDbjO/sLe0ymwf6l+eD +jQWARoS+sq7ftjQXAAXvdL+Thm0U3wSipUONdVmk1MzyEmHChDUqdp/iNoaHdaT7sUOZd8DzS9 D X-Google-Smtp-Source: AGHT+IH161caz/hrEEDmu9rbaORyuKaIBN8RaZW2Jp2SQC5GDgiJwwYUecWOIrC97hdu248Z+GXJJQ== X-Received: by 2002:a05:6402:5254:b0:5c2:caf6:64d4 with SMTP id 4fb4d7f45d1cf-5c3dc78dbaamr10636424a12.12.1725984984320; Tue, 10 Sep 2024 09:16:24 -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 2/3] x86/boot: Refactor BIOS/PVH start Date: Tue, 10 Sep 2024 17:16:11 +0100 Message-Id: <20240910161612.242702-3-frediano.ziglio@cloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240910161612.242702-1-frediano.ziglio@cloud.com> References: <20240910161612.242702-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 %ebp register to store boot type before running common code. Signed-off-by: Frediano Ziglio --- xen/arch/x86/boot/head.S | 113 +++++++++++++++------------------------ 1 file changed, 44 insertions(+), 69 deletions(-) diff --git a/xen/arch/x86/boot/head.S b/xen/arch/x86/boot/head.S index de118d72f2..a91413184c 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,13 +412,28 @@ cs32_switch: ELFNOTE(Xen, XEN_ELFNOTE_PHYS32_ENTRY, .long sym_offs(__pvh_start)) __pvh_start: - cld cli + mov $BOOT_TYPE_PVH, %ebp + jmp common_bios_pvh +#endif /* CONFIG_PVH_GUEST */ + +__start: + cli + mov $BOOT_TYPE_BIOS, %ebp + +common_bios_pvh: + 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. */ @@ -433,14 +451,9 @@ __pvh_start: /* Set up stack. */ lea STACK_SIZE - CPUINFO_sizeof + sym_esi(cpu0_stack), %esp - call initialise_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 +462,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 descriptor to unused segment registers. */ + xor %ecx, %ecx + mov %ecx, %fs + mov %ecx, %gs -initialise_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 - /* - * 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, %ebp + 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 initialise_bss + /* 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 +1: +#endif /* CONFIG_PVH_GUEST */ /* Bootloaders may set multiboot{1,2}.mem_lower to a nonzero value. */ xor %edx,%edx @@ -568,22 +558,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 Tue Sep 10 16:16:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frediano Ziglio X-Patchwork-Id: 13798865 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 729D4EDE99A for ; Tue, 10 Sep 2024 16:16:40 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.795866.1205354 (Exim 4.92) (envelope-from ) id 1so3XY-0007Vl-Fz; Tue, 10 Sep 2024 16:16:28 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 795866.1205354; Tue, 10 Sep 2024 16:16:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1so3XY-0007Uw-7j; Tue, 10 Sep 2024 16:16:28 +0000 Received: by outflank-mailman (input) for mailman id 795866; Tue, 10 Sep 2024 16:16:27 +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 1so3XX-000753-4P for xen-devel@lists.xenproject.org; Tue, 10 Sep 2024 16:16:27 +0000 Received: from mail-lj1-x229.google.com (mail-lj1-x229.google.com [2a00:1450:4864:20::229]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 079b582a-6f90-11ef-a0b5-8be0dac302b0; Tue, 10 Sep 2024 18:16:26 +0200 (CEST) Received: by mail-lj1-x229.google.com with SMTP id 38308e7fff4ca-2f75aaaade6so37623781fa.1 for ; Tue, 10 Sep 2024 09:16:26 -0700 (PDT) Received: from fziglio-desktop.. ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5c3ebd468a1sm4397405a12.26.2024.09.10.09.16.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Sep 2024 09:16:24 -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: 079b582a-6f90-11ef-a0b5-8be0dac302b0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1725984986; x=1726589786; 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=GgQOCHdSoX47Ypi7lpWXHIrSV2P92tiUaQrbWBnKQ1o=; b=RBArRvcZqCNnlWjYLY15SNDmrVVujoeaFzpIJUegcmDouh431q2QirYFW+4eOxGcek SGPuPMvvlR2qdMEzKIvUtNpJovoL8v++bGDm8junaNAgj5DgkM11cnPJgUzYEqjcaGWJ YjGdkVvBEwqqUEHT3B2CuQdBFYIqDA2GPABqg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725984986; x=1726589786; 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=GgQOCHdSoX47Ypi7lpWXHIrSV2P92tiUaQrbWBnKQ1o=; b=hEO0rwLwOV+mY/bCt9cCbcz097wm4ZMcqikplzVxeyk+7z+gi7dyyEwZ/3letQil4E gFpyKB3MSq/5NEdfu2TxBZqzEYGPaxVqhS4eF46IbB30IKOWS39kz3Jx2NakrCqa9sGc diDiFev43e7SzvwMv1eUlaWhVFyAzTat7EXoOsk9BGk2ZeYKRanD+YxIUFSwgw42FXQ3 tJnJ74YxXPxauHRj4lw+balGzr/93avaniWfXW1n2uFA7leMmTwmJ0Xx1of2EFMD9Oad o5MbagHW+CQzcjR8MW28K1QNURR0cTM2z/Cq7kCP3LNtKWRgLI5N4A2c09X6VMWZiCXE ealQ== X-Gm-Message-State: AOJu0Ywk35EaRYpsjxMt0BfkNL0eHHMR8WsH963shCjX8Hc1FiRHSsS1 nU+n5dhKaTXqFuJnQLTCCJsyyALuoAS12yzND/RP/7n4W7Fz+CgKneCWrQGa7Ep9IgHoVvFVJ5P D X-Google-Smtp-Source: AGHT+IG2zm0KGDoqTA19TMPtgJHt/51E1qM8dOhsMJpNzhN3JTkcBJeSrq5plY45m8ccf2spruZBQg== X-Received: by 2002:a2e:812:0:b0:2f6:5f7b:e5cf with SMTP id 38308e7fff4ca-2f751edfebbmr64251151fa.14.1725984985324; Tue, 10 Sep 2024 09:16:25 -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 3/3] x86/boot: Rewrite EFI start part in C Date: Tue, 10 Sep 2024 17:16:12 +0100 Message-Id: <20240910161612.242702-4-frediano.ziglio@cloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240910161612.242702-1-frediano.ziglio@cloud.com> References: <20240910161612.242702-1-frediano.ziglio@cloud.com> MIME-Version: 1.0 No need to have it coded in assembly. Signed-off-by: Frediano Ziglio --- xen/arch/x86/boot/head.S | 131 ++++++---------------------------- xen/arch/x86/efi/Makefile | 1 + xen/arch/x86/efi/parse-mbi2.c | 54 ++++++++++++++ xen/arch/x86/efi/stub.c | 3 +- 4 files changed, 80 insertions(+), 109 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 a91413184c..4b05b564a0 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: @@ -255,34 +246,29 @@ __efi64_mb2_start: 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 %rax -.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. */ + 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: %eax - error string. + */ + mov %eax, %edi + mov %ebx, %esi + call efi_parse_mbi2 + test %rax, %rax + lea .Ldirect_error(%rip), %r15 + 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 +278,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..0bbb29dc0e 100644 --- a/xen/arch/x86/efi/Makefile +++ b/xen/arch/x86/efi/Makefile @@ -13,6 +13,7 @@ $(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-y += parse-mbi2.o obj-bin-$(XEN_BUILD_EFI) := $(filter %.init.o,$(EFIOBJ-y)) extra-$(XEN_BUILD_EFI) += buildid.o relocs-dummy.o nocov-$(XEN_BUILD_EFI) += stub.o diff --git a/xen/arch/x86/efi/parse-mbi2.c b/xen/arch/x86/efi/parse-mbi2.c new file mode 100644 index 0000000000..4dfcac326e --- /dev/null +++ b/xen/arch/x86/efi/parse-mbi2.c @@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void asmlinkage __init efi_multiboot2(EFI_HANDLE ImageHandle, + EFI_SYSTEM_TABLE *SystemTable, + const char *cmdline); + +const char *__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 Multiboot bootloader!"; + + /* Skip Multiboot2 information fixed part. */ + for ( tag = _p(ROUNDUP((unsigned long)(mbi + 1), MULTIBOOT2_TAG_ALIGN)); + (void *)tag - (void *)mbi < mbi->total_size && tag->type != MULTIBOOT2_TAG_TYPE_END; + tag = _p(ROUNDUP((unsigned long)((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";