Skip to contentSkip to conversation

x86: Use low memory size directly from Multiboot

PATCH Superseded [email protected] 3 participants 9 messages Feb 9, 2022

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: Use low memory size directly from Multiboot

Previously, Xen used information from the BDA to detect the amount of available low memory. This does not work on some scenarios such as Coreboot, or when booting from Kexec on a UEFI system without CSM.

Use the information directly supplied by Multiboot boot information instead.

---
 xen/arch/x86/boot/head.S | 30 ++++++------------------------
 1 file changed, 6 insertions(+), 24 deletions(-)
xen/arch/x86/boot/head.S +3 -19
index dd1bea0d10..31a0663e6d 100644
@@ -524,33 +524,15 @@ trampoline_bios_setup:
         mov     %ecx,%fs
         mov     %ecx,%gs
 
-        /* Set up trampoline segment 64k below EBDA */
-        movzwl  0x40e,%ecx          /* EBDA segment */
-        cmp     $0xa000,%ecx        /* sanity check (high) */
-        jae     0f
-        cmp     $0x4000,%ecx        /* sanity check (low) */
-        jae     1f
-0:
-        movzwl  0x413,%ecx          /* use base memory size on failure */
-        shl     $10-4,%ecx
-1:
-        /*
-         * Compare the value in the BDA with the information from the
-         * multiboot structure (if available) and use the smallest.
-         */
-        cmp     $0x100,%edx         /* is the multiboot value too small? */
-        jb      2f                  /* if so, do not use it */
-        shl     $10-4,%edx
-        cmp     %ecx,%edx           /* compare with BDA value */
-        cmovb   %edx,%ecx           /* and use the smaller */
+        /* Use lower memory size directly from Multiboot */
+        mov     %edx,%ecx
+        /* From arch/x86/smpboot.c: start_eip had better be page-aligned!

*/
+ shr $2,%ecx

-2:

         /* Reserve memory for the trampoline and the low-memory stack. */
-        sub     $((TRAMPOLINE_SPACE+TRAMPOLINE_STACK_SPACE)>>4),%ecx
+        sub     $((TRAMPOLINE_SPACE+TRAMPOLINE_STACK_SPACE)>>12),%ecx

- /* From arch/x86/smpboot.c: start_eip had better be page-aligned!
*/

-        xor     %cl, %cl
-        shl     $4, %ecx
+        shl     $12,%ecx
         mov     %ecx,sym_esi(trampoline_phys)

trampoline_setup:

Signature · 1 line
2.25.1
#1 lore raw
Quoted 24 lines · On 09.02.2022 12:00, [email protected] wrote:

Previously, Xen used information from the BDA to detect the amount of available low memory. This does not work on some scenarios such as Coreboot, or when booting from Kexec on a UEFI system without CSM.

Use the information directly supplied by Multiboot boot information instead.

---
 xen/arch/x86/boot/head.S | 30 ++++++------------------------
 1 file changed, 6 insertions(+), 24 deletions(-)
xen/arch/x86/boot/head.S +0 -6
index dd1bea0d10..31a0663e6d 100644
@@ -524,33 +524,15 @@ trampoline_bios_setup:
         mov     %ecx,%fs
         mov     %ecx,%gs
 
-        /* Set up trampoline segment 64k below EBDA */
-        movzwl  0x40e,%ecx          /* EBDA segment */
-        cmp     $0xa000,%ecx        /* sanity check (high) */
-        jae     0f
-        cmp     $0x4000,%ecx        /* sanity check (low) */
-        jae     1f

xen/arch/x86/boot/head.S trampoline_bios_setup: · line 542

Quoted 10 lines
xen/arch/x86/boot/head.S +0 -10
-0:
-        movzwl  0x413,%ecx          /* use base memory size on failure */
-        shl     $10-4,%ecx
-1:
-        /*
-         * Compare the value in the BDA with the information from the
-         * multiboot structure (if available) and use the smallest.
-         */
-        cmp     $0x100,%edx         /* is the multiboot value too small? */
-        jb      2f                  /* if so, do not use it */

The comment here is a pretty clear indication that bad values may have been observed, even if this was only in the distant past. But we have to not regress even on very old boot loaders.

Is the kexec case recognizable by any means (including to distinguish kexec properly communicating the value vs it not doing so, as iirc it was said on irc that this didn't always work correctly there), such that we could skip using the BDA value in that case?

Jan

Quoted 22 lines
xen/arch/x86/boot/head.S +6 -8
-        shl     $10-4,%edx
-        cmp     %ecx,%edx           /* compare with BDA value */
-        cmovb   %edx,%ecx           /* and use the smaller */
+        /* Use lower memory size directly from Multiboot */
+        mov     %edx,%ecx
+        /* From arch/x86/smpboot.c: start_eip had better be page-aligned!
 */
+        shr     $2,%ecx
 
-2:
         /* Reserve memory for the trampoline and the low-memory stack. */
-        sub     $((TRAMPOLINE_SPACE+TRAMPOLINE_STACK_SPACE)>>4),%ecx
+        sub     $((TRAMPOLINE_SPACE+TRAMPOLINE_STACK_SPACE)>>12),%ecx
 
-        /* From arch/x86/smpboot.c: start_eip had better be page-aligned!
 */
-        xor     %cl, %cl
-        shl     $4, %ecx
+        shl     $12,%ecx
         mov     %ecx,sym_esi(trampoline_phys)

trampoline_setup:

#2 lore raw
Quoted 8 lines

The comment here is a pretty clear indication that bad values may have been observed, even if this was only in the distant past. But we have to not regress even on very old boot loaders.

Is the kexec case recognizable by any means (including to distinguish kexec properly communicating the value vs it not doing so, as iirc it was said on irc that this didn't always work correctly there), such that we could skip using the BDA value in that case?

Old Kexec used to report the value in bytes instead of kilobytes like it's supposed to. I can make a special case for that so that we won't have to rely on the BDA.

#3 lore raw

PATCH v2 1/1 x86: Use low memory size directly from Multiboot

In reply to Jan Beulich

Previously, Xen used information from the BDA to detect the amount of available low memory. This does not work on some scenarios such as Coreboot, or when booting from Kexec on a UEFI system without CSM.

Use the information directly supplied by Multiboot boot information instead.

---
 xen/arch/x86/boot/head.S | 34 ++++++++++++----------------------
 1 file changed, 12 insertions(+), 22 deletions(-)
xen/arch/x86/boot/head.S +12 -22
index dd1bea0d10..62fe3fe55b 100644
@@ -524,33 +524,23 @@ trampoline_bios_setup:
         mov     %ecx,%fs
         mov     %ecx,%gs
 
-        /* Set up trampoline segment 64k below EBDA */
-        movzwl  0x40e,%ecx          /* EBDA segment */
-        cmp     $0xa000,%ecx        /* sanity check (high) */
-        jae     0f
-        cmp     $0x4000,%ecx        /* sanity check (low) */
-        jae     1f
-0:
-        movzwl  0x413,%ecx          /* use base memory size on failure */
-        shl     $10-4,%ecx
-1:
+        /* Use lower memory size directly from Multiboot */
+        mov     %edx,%ecx
         /*
-         * Compare the value in the BDA with the information from the
-         * multiboot structure (if available) and use the smallest.
+         * Old Kexec used to report the value in bytes instead of kilobytes
+         * like it's supposed to, so fix that if detected.
          */
-        cmp     $0x100,%edx         /* is the multiboot value too small? */
-        jb      2f                  /* if so, do not use it */
-        shl     $10-4,%edx
-        cmp     %ecx,%edx           /* compare with BDA value */
-        cmovb   %edx,%ecx           /* and use the smaller */
+        cmpl    $640,%ecx
+        jbe     1f
+        shr     $10,%ecx
+1:
+        /* From arch/x86/smpboot.c: start_eip had better be page-aligned! */
+        shr     $2,%ecx
 
-2:
         /* Reserve memory for the trampoline and the low-memory stack. */
-        sub     $((TRAMPOLINE_SPACE+TRAMPOLINE_STACK_SPACE)>>4),%ecx
+        sub     $((TRAMPOLINE_SPACE+TRAMPOLINE_STACK_SPACE)>>12),%ecx
 
-        /* From arch/x86/smpboot.c: start_eip had better be page-aligned! */
-        xor     %cl, %cl
-        shl     $4, %ecx
+        shl     $12,%ecx
         mov     %ecx,sym_esi(trampoline_phys)
 
 trampoline_setup:
Signature · 1 line
2.25.1
#4 lore raw
Quoted 7 lines · Tu Dinh Ngoc

Previously, Xen used information from the BDA to detect the amount of available low memory. This does not work on some scenarios such as Coreboot, or when booting from Kexec on a UEFI system without CSM.

Use the information directly supplied by Multiboot boot information instead.
---

Btw - please summarize here briefly what has changed from the earlier version. As it stands your adjustment looks to take care of one third of what I did say in reply to your v1. That's not enough for a v2, or else you should have taken care of the remaining aspects verbally.

Jan

Quoted 48 lines
xen/arch/x86/boot/head.S +12 -22
@@ -524,33 +524,23 @@ trampoline_bios_setup:
         mov     %ecx,%fs
         mov     %ecx,%gs
 
-        /* Set up trampoline segment 64k below EBDA */
-        movzwl  0x40e,%ecx          /* EBDA segment */
-        cmp     $0xa000,%ecx        /* sanity check (high) */
-        jae     0f
-        cmp     $0x4000,%ecx        /* sanity check (low) */
-        jae     1f
-0:
-        movzwl  0x413,%ecx          /* use base memory size on failure */
-        shl     $10-4,%ecx
-1:
+        /* Use lower memory size directly from Multiboot */
+        mov     %edx,%ecx
         /*
-         * Compare the value in the BDA with the information from the
-         * multiboot structure (if available) and use the smallest.
+         * Old Kexec used to report the value in bytes instead of kilobytes
+         * like it's supposed to, so fix that if detected.
          */
-        cmp     $0x100,%edx         /* is the multiboot value too small? */
-        jb      2f                  /* if so, do not use it */
-        shl     $10-4,%edx
-        cmp     %ecx,%edx           /* compare with BDA value */
-        cmovb   %edx,%ecx           /* and use the smaller */
+        cmpl    $640,%ecx
+        jbe     1f
+        shr     $10,%ecx
+1:
+        /* From arch/x86/smpboot.c: start_eip had better be page-aligned! */
+        shr     $2,%ecx
 
-2:
         /* Reserve memory for the trampoline and the low-memory stack. */
-        sub     $((TRAMPOLINE_SPACE+TRAMPOLINE_STACK_SPACE)>>4),%ecx
+        sub     $((TRAMPOLINE_SPACE+TRAMPOLINE_STACK_SPACE)>>12),%ecx
 
-        /* From arch/x86/smpboot.c: start_eip had better be page-aligned! */
-        xor     %cl, %cl
-        shl     $4, %ecx
+        shl     $12,%ecx
         mov     %ecx,sym_esi(trampoline_phys)
 
 trampoline_setup:
#5 lore raw
Quoted 16 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Wednesday, 9 February 2022 15:26
To: Tu Dinh Ngoc <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH v2] x86: Use low memory size directly from Multiboot

Quoted 7 lines · Tu Dinh Ngoc

Previously, Xen used information from the BDA to detect the amount of available low memory. This does not work on some scenarios such as Coreboot, or when booting from Kexec on a UEFI system without CSM.

Use the information directly supplied by Multiboot boot information instead.
---

Quoted 6 lines

Btw - please summarize here briefly what has changed from the earlier version. As it stands your adjustment looks to take care of one third of what I did say in reply to your v1. That's not enough for a v2, or else you should have taken care of the remaining aspects verbally.

Jan

Hi,

Quoted 3 lines

The comment here is a pretty clear indication that bad values may have been observed, even if this was only in the distant past. But we have to not regress even on very old boot loaders.

Is the kexec case recognizable by any means (including to distinguish kexec properly communicating the value vs it not doing so, as iirc it was said on irc that this didn't always work correctly there), such that we could skip using the BDA value in that case?

As written in the comments, old versions of kexec (before 2.0.23) presented the amount of lower and upper memory in the BASIC_MEMINFO Multiboot2 tag in bytes instead of kilobytes. The v2 patch tries to detect this condition by checking if there's more than 640 KB of low memory and corrects the low memory size in that case.

This change should only affect the particular case of booting with Multiboot2 without EFI (e.g. legacy BIOS or Kexec). Other cases like Multiboot 0.x, EFI booting (with or without MB2), or bootloaders that generate the BASIC_MEMINFO tag correctly shouldn't be affected.

#6 lore raw
Quoted 1 line · On 09.02.2022 16:20, [email protected] wrote:

This change should only affect the particular case of booting with Multiboot2 without EFI (e.g. legacy BIOS or Kexec). Other cases like Multiboot 0.x, EFI booting (with or without MB2), or bootloaders that generate the BASIC_MEMINFO tag correctly shouldn't be affected.

How that? You're taking out the reading of the BDA value altogether, aren't you? This is certainly a change affecting other environments as well.

Jan

#7 lore raw
Quoted 9 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: Wednesday, 9 February 2022 16:23
To: [email protected]
Cc: [email protected]
Subject: Re: [PATCH v2] x86: Use low memory size directly from Multiboot

Quoted 1 line · On 09.02.2022 16:20, [email protected] wrote:

This change should only affect the particular case of booting with

Quoted 8 lines

Multiboot2 without EFI (e.g. legacy BIOS or Kexec). Other cases like Multiboot 0.x, EFI booting (with or without MB2), or bootloaders that generate the BASIC_MEMINFO tag correctly shouldn't be affected.

How that? You're taking out the reading of the BDA value altogether, aren't you? This is certainly a change affecting other environments as well.

Jan

I missed that the BDA was used when booting on Multiboot 0.x as well. I can add a fallback to use the BDA when the bootloader doesn't provide low memory size in the MBI.

#8 lore raw
In reply to Tu Dinh Ngoc
Quoted 6 lines · Tu Dinh Ngoc

Previously, Xen used information from the BDA to detect the amount of available low memory. This does not work on some scenarios such as Coreboot, or when booting from Kexec on a UEFI system without CSM.

Use the information directly supplied by Multiboot boot information instead.

This is missing a SoB, see:

https://wiki.xenproject.org/wiki/Submitting_Xen_Project_Patches#Signed-off-by

Thanks, Roger.