Skip to contentSkip to conversation

x86/boot: Fix the boot time relocation calculations

PATCH Superseded Andrew Cooper 2 participants 4 messages Jun 2, 2017 – Jun 3, 2017

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/boot: Fix the boot time relocation calculations

c/s b28044226e1 "x86: make Xen early boot code relocatable" introduces

    mov $sym_offs(__image_base__),%esi

to the legacy boot path. However, this is by definition 0, which means the boot code only functions correctly when Xen is loaded at its preferred physical address (2M at the time of writing).

Xen does cope if loaded at an alternative physical address, if the MULTIBOOT2_TAG_TYPE_LOAD_BASE_ADDR tag is filled in properly. While recent versions of Grub do fill this in appropriately, tboot does not. (In fact, tboot loads Xen at the preferred address, but claims a load address of 8M.)

Both multiboot 1 and 2 specify the execution environment as being flat. As a result, Xen needs no help calculating the proper load address.

Calculate the load address from %eip alone, and ignore MULTIBOOT2_TAG_TYPE_LOAD_BASE_ADDR entirely. This fixes legacy boot under various versions of tboot.

---
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Daniel Kiper <[email protected]>
CC: Doug Goldstein <[email protected]>
CC: Sergey Dyasli <[email protected]>

This is a regression introduced in Xen 4.9, and should therefore be fixed.

---
 xen/arch/x86/boot/head.S | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)
xen/arch/x86/boot/head.S +4 -11
index 5e84e42..df28b09 100644
@@ -377,8 +377,10 @@ __start:
         cld
         cli
 
-        /* Load default Xen image load base address. */
-        mov     $sym_offs(__image_base__),%esi
+        /* Calculate the load base address. */
+        call    1f
+1:      pop     %esi
+        sub     $sym_offs(1b), %esi
 
         /* Bootloaders may set multiboot{1,2}.mem_lower to a nonzero value. */
         xor     %edx,%edx
@@ -410,15 +412,6 @@ __start:
         cmp     %edi,MB2_fixed_total_size(%ebx)
         jbe     trampoline_bios_setup
 
-        /* Get Xen image load base address from Multiboot2 information. */
-        cmpl    $MULTIBOOT2_TAG_TYPE_LOAD_BASE_ADDR,MB2_tag_type(%ecx)
-        jne     .Lmb2_mem_lower
-
-        mov     MB2_load_base_addr(%ecx),%esi
-        sub     $XEN_IMG_OFFSET,%esi
-        jmp     .Lmb2_next_tag
-
-.Lmb2_mem_lower:
         /* Get mem_lower from Multiboot2 information. */
         cmpl    $MULTIBOOT2_TAG_TYPE_BASIC_MEMINFO,MB2_tag_type(%ecx)
         cmove   MB2_mem_lower(%ecx),%edx
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw
Quoted 4 lines · Andrew Cooper

c/s b28044226e1 "x86: make Xen early boot code relocatable" introduces

    mov $sym_offs(__image_base__),%esi
Quoted 8 lines

to the legacy boot path. However, this is by definition 0, which means the boot code only functions correctly when Xen is loaded at its preferred physical address (2M at the time of writing).

Xen does cope if loaded at an alternative physical address, if the MULTIBOOT2_TAG_TYPE_LOAD_BASE_ADDR tag is filled in properly. While recent versions of Grub do fill this in appropriately, tboot does not. (In fact, tboot loads Xen at the preferred address, but claims a load address of 8M.)

Does it use Multiboot or Multiboot2 protocol? Anyway, I think that this should be fixed in tboot.

Quoted 26 lines

Both multiboot 1 and 2 specify the execution environment as being flat. As a result, Xen needs no help calculating the proper load address.

Calculate the load address from %eip alone, and ignore MULTIBOOT2_TAG_TYPE_LOAD_BASE_ADDR entirely. This fixes legacy boot under various versions of tboot.

---
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Daniel Kiper <[email protected]>
CC: Doug Goldstein <[email protected]>
CC: Sergey Dyasli <[email protected]>

This is a regression introduced in Xen 4.9, and should therefore be fixed.

---
 xen/arch/x86/boot/head.S | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)
xen/arch/x86/boot/head.S +0 -0
index 5e84e42..df28b09 100644

xen/arch/x86/boot/head.S __start: · line 383

Quoted 9 lines
xen/arch/x86/boot/head.S +4 -2
@@ -377,8 +377,10 @@ __start:
         cld
         cli
 
-        /* Load default Xen image load base address. */
-        mov     $sym_offs(__image_base__),%esi
+        /* Calculate the load base address. */
+        call    1f
+1:      pop     %esi
+        sub     $sym_offs(1b), %esi

I have a problem with this. I know that this should be done in that way. Even I considered that during my work on Multiboot2 stuff. However Multiboot and Multiboot2 specs clearly say: The OS image must create its own stack as soon as it needs one. So, we cannot assume here that %ss:%esp points to valid stack and do such things. Even if it works with this and that right now. So, I would prefer to fix it in more reliable way right now even if it means more code here and there.

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#2 lore raw
Quoted 9 lines · Daniel Kiper
Quoted 8 lines · Andrew Cooper

c/s b28044226e1 "x86: make Xen early boot code relocatable" introduces

    mov $sym_offs(__image_base__),%esi

to the legacy boot path. However, this is by definition 0, which means the boot code only functions correctly when Xen is loaded at its preferred physical address (2M at the time of writing).

Quoted 6 lines
Quoted 4 lines

Xen does cope if loaded at an alternative physical address, if the MULTIBOOT2_TAG_TYPE_LOAD_BASE_ADDR tag is filled in properly. While recent versions of Grub do fill this in appropriately, tboot does not. (In fact, tboot loads Xen at the preferred address, but claims a load address of 8M.)

Does it use Multiboot or Multiboot2 protocol? Anyway, I think that this should be fixed in tboot.

In this case, MB2. Tboot should indeed be fixed, but that doesn't invalidate this fix.

Quoted 30 lines
Quoted 30 lines

Both multiboot 1 and 2 specify the execution environment as being flat. As a result, Xen needs no help calculating the proper load address.

Calculate the load address from %eip alone, and ignore MULTIBOOT2_TAG_TYPE_LOAD_BASE_ADDR entirely. This fixes legacy boot under various versions of tboot.

---
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Daniel Kiper <[email protected]>
CC: Doug Goldstein <[email protected]>
CC: Sergey Dyasli <[email protected]>

This is a regression introduced in Xen 4.9, and should therefore be fixed.

---
 xen/arch/x86/boot/head.S | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)
xen/arch/x86/boot/head.S +0 -0
index 5e84e42..df28b09 100644
@@ -377,8 +377,10 @@ __start:
         cld
         cli
 
Quoted 11 lines

xen/arch/x86/boot/head.S __start: · line 383

Quoted 6 lines
xen/arch/x86/boot/head.S +4 -2
-        /* Load default Xen image load base address. */
-        mov     $sym_offs(__image_base__),%esi
+        /* Calculate the load base address. */
+        call    1f
+1:      pop     %esi
+        sub     $sym_offs(1b), %esi

I have a problem with this. I know that this should be done in that way. Even I considered that during my work on Multiboot2 stuff. However Multiboot and Multiboot2 specs clearly say: The OS image must create its own stack as soon as it needs one. So, we cannot assume here that %ss:%esp points to valid stack and do such things.

There is no possible way that %ss is bad. It must be a writeable segment (to be loaded in the first place) with 0 base and 4G limit (to fulfil the flat requirement).

I see MB1 says that %esp is undefined. Where does the MB2 spec live these days (google doesn't seem to know)?

Quoted 3 lines

Even if it works with this and that right now. So, I would
prefer to fix it in more reliable way right now even if it means more code here and there.

Does the EFI entry guarantee a good %rsp? That codepath doesn't sanitise %rsp before use.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#3 lore raw
Quoted 14 lines · Andrew Cooper
Quoted 13 lines · Daniel Kiper
Quoted 12 lines · Andrew Cooper

c/s b28044226e1 "x86: make Xen early boot code relocatable" introduces

    mov $sym_offs(__image_base__),%esi

to the legacy boot path. However, this is by definition 0, which means the boot code only functions correctly when Xen is loaded at its preferred physical address (2M at the time of writing).

Xen does cope if loaded at an alternative physical address, if the MULTIBOOT2_TAG_TYPE_LOAD_BASE_ADDR tag is filled in properly. While recent versions of Grub do fill this in appropriately, tboot does not. (In fact, tboot loads Xen at the preferred address, but claims a load address of 8M.)

Quoted 5 lines
Quoted 2 lines

Does it use Multiboot or Multiboot2 protocol? Anyway, I think that this should be fixed in tboot.

In this case, MB2. Tboot should indeed be fixed, but that doesn't invalidate this fix.

Sure thing.

Quoted 41 lines
Quoted 29 lines
Quoted 29 lines

Both multiboot 1 and 2 specify the execution environment as being flat. As a result, Xen needs no help calculating the proper load address.

Calculate the load address from %eip alone, and ignore MULTIBOOT2_TAG_TYPE_LOAD_BASE_ADDR entirely. This fixes legacy boot under various versions of tboot.

---
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Daniel Kiper <[email protected]>
CC: Doug Goldstein <[email protected]>
CC: Sergey Dyasli <[email protected]>

This is a regression introduced in Xen 4.9, and should therefore be fixed.

---
 xen/arch/x86/boot/head.S | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)
xen/arch/x86/boot/head.S +0 -0
index 5e84e42..df28b09 100644
@@ -377,8 +377,10 @@ __start:
         cld
         cli
 
Quoted 11 lines

xen/arch/x86/boot/head.S __start: · line 383

Quoted 6 lines
xen/arch/x86/boot/head.S +4 -2
-        /* Load default Xen image load base address. */
-        mov     $sym_offs(__image_base__),%esi
+        /* Calculate the load base address. */
+        call    1f
+1:      pop     %esi
+        sub     $sym_offs(1b), %esi

I have a problem with this. I know that this should be done in that way. Even I considered that during my work on Multiboot2 stuff. However Multiboot and Multiboot2 specs clearly say: The OS image must create its own stack as soon as it needs one. So, we cannot assume here that %ss:%esp points to valid stack and do such things.

Quoted 3 lines

There is no possible way that %ss is bad. It must be a writeable segment (to be loaded in the first place) with 0 base and 4G limit (to fulfil the flat requirement).

Ahhh... Right, I forgot that it is initialized like %ds and others.

Quoted 2 lines

I see MB1 says that %esp is undefined. Where does the MB2 spec live these days (google doesn't seem to know)?

You can find it in multiboot2 branch in GRUB2 git tree. Though I am sending PDF with latest spec (I should put it somewhere) for your convenience.

Quoted 6 lines
Quoted 3 lines

Even if it works with this and that right now. So, I would
prefer to fix it in more reliable way right now even if it means more code here and there.

Does the EFI entry guarantee a good %rsp? That codepath doesn't sanitise %rsp before use.

Yep, Multiboot2 spec says about that.

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel