Skip to contentSkip to conversation

x86/boot: Various bits of trampoline cleanup

PATCH New Andrew Cooper 3 participants 8 messages Aug 19, 2019 – Aug 27, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/boot: Various bits of trampoline cleanup

Andrew Cooper (3):
x86/boot: Further minor GDT corrections
x86/boot: Reposition trampoline data
x86/boot: Drop all use of lmsw

 xen/arch/x86/boot/head.S          |  2 +-
 xen/arch/x86/boot/trampoline.S    | 78 +++++++++++++++++----------------------
 xen/arch/x86/boot/wakeup.S        |  5 ++-
 xen/arch/x86/x86_64/kexec_reloc.S |  4 +-
 4 files changed, 40 insertions(+), 49 deletions(-)
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH 1/1 x86/boot: Further minor GDT corrections

gdt_boot_descr and gdt_48 disagree on how long trampoline_gdt is.

Introduce an end label and have the linker calculate the size, rather than hard coding it.

Also, just as with c/s af292b41e9, there is no point forcing the CPU to set Access bits. Fix all remaining GDTs in Xen.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

The trampoline GDT access bits were actually noticed when trying to clean up our boot time pagetables and map the trampoline read-only.

---
 xen/arch/x86/boot/head.S          |  2 +-
 xen/arch/x86/boot/trampoline.S    | 15 ++++++++-------
 xen/arch/x86/x86_64/kexec_reloc.S |  4 ++--
 3 files changed, 11 insertions(+), 10 deletions(-)
xen/arch/x86/boot/head.S +1 -1
index 26b680521d..9fdb9b3954 100644
@@ -129,7 +129,7 @@ multiboot2_header:
 
         .word   0
 gdt_boot_descr:
-        .word   7*8-1
+        .word   .Ltramopline_gdt_end - trampoline_gdt - 1
 gdt_boot_base:
         .long   sym_offs(trampoline_gdt)
         .long   0 /* Needed for 64-bit lgdt */
xen/arch/x86/boot/trampoline.S +8 -7
index 7c6a2328d2..1761fc1213 100644
@@ -63,22 +63,23 @@ trampoline_gdt:
         /* 0x0000: unused */
         .quad   0x0000000000000000
         /* 0x0008: ring 0 code, 32-bit mode */
-        .quad   0x00cf9a000000ffff
+        .quad   0x00cf9b000000ffff
         /* 0x0010: ring 0 code, 64-bit mode */
-        .quad   0x00af9a000000ffff
+        .quad   0x00af9b000000ffff
         /* 0x0018: ring 0 data */
-        .quad   0x00cf92000000ffff
+        .quad   0x00cf93000000ffff
         /* 0x0020: real-mode code @ BOOT_TRAMPOLINE */
         .long   0x0000ffff
-        .long   0x00009a00
+        .long   0x00009b00
         /* 0x0028: real-mode data @ BOOT_TRAMPOLINE */
         .long   0x0000ffff
-        .long   0x00009200
+        .long   0x00009300
         /*
          * 0x0030: ring 0 Xen data, 16 MiB size, base
          * address is computed at runtime.
          */
-        .quad   0x00c0920000000fff
+        .quad   0x00c0930000000fff
+.Ltramopline_gdt_end:
 
         .pushsection .trampoline_rel, "a"
         .long   trampoline_gdt + BOOT_PSEUDORM_CS + 2 - .
@@ -182,7 +183,7 @@ start64:
         .word   0
 idt_48: .word   0, 0, 0 # base = limit = 0
         .word   0
-gdt_48: .word   6*8-1
+gdt_48: .word   .Ltramopline_gdt_end - trampoline_gdt - 1
         .long   bootsym_rel(trampoline_gdt,4)
 
 /* The first page of trampoline is permanent, the rest boot-time only. */
xen/arch/x86/x86_64/kexec_reloc.S +2 -2
index 5bf61d5c2d..9e5b7a6ba1 100644
@@ -182,8 +182,8 @@ compat_mode_gdt_desc:
         .align 8
 compat_mode_gdt:
         .quad 0x0000000000000000     /* null                              */
-        .quad 0x00cf92000000ffff     /* 0x0008 ring 0 data                */
-        .quad 0x00cf9a000000ffff     /* 0x0010 ring 0 code, compatibility */
+        .quad 0x00cf93000000ffff     /* 0x0008 ring 0 data                */
+        .quad 0x00cf9b000000ffff     /* 0x0010 ring 0 code, compatibility */
 
 compat_mode_idt:
         .word 0                      /* limit */
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 1/1
#2 lore raw

Re: [Xen-devel] [PATCH] x86/boot: Further minor GDT corrections

Quoted 56 lines · Andrew Cooper

gdt_boot_descr and gdt_48 disagree on how long trampoline_gdt is.

Introduce an end label and have the linker calculate the size, rather than hard coding it.

Also, just as with c/s af292b41e9, there is no point forcing the CPU to set Access bits. Fix all remaining GDTs in Xen.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

The trampoline GDT access bits were actually noticed when trying to clean up our boot time pagetables and map the trampoline read-only.

---
  xen/arch/x86/boot/head.S          |  2 +-
  xen/arch/x86/boot/trampoline.S    | 15 ++++++++-------
  xen/arch/x86/x86_64/kexec_reloc.S |  4 ++--
  3 files changed, 11 insertions(+), 10 deletions(-)
xen/arch/x86/boot/head.S +1 -1
index 26b680521d..9fdb9b3954 100644
@@ -129,7 +129,7 @@ multiboot2_header:
 
          .word   0
  gdt_boot_descr:
-        .word   7*8-1
+        .word   .Ltramopline_gdt_end - trampoline_gdt - 1
  gdt_boot_base:
          .long   sym_offs(trampoline_gdt)
          .long   0 /* Needed for 64-bit lgdt */
xen/arch/x86/boot/trampoline.S +4 -4
index 7c6a2328d2..1761fc1213 100644
@@ -63,22 +63,23 @@ trampoline_gdt:
          /* 0x0000: unused */
          .quad   0x0000000000000000
          /* 0x0008: ring 0 code, 32-bit mode */
-        .quad   0x00cf9a000000ffff
+        .quad   0x00cf9b000000ffff
          /* 0x0010: ring 0 code, 64-bit mode */
-        .quad   0x00af9a000000ffff
+        .quad   0x00af9b000000ffff
          /* 0x0018: ring 0 data */
-        .quad   0x00cf92000000ffff
+        .quad   0x00cf93000000ffff
          /* 0x0020: real-mode code @ BOOT_TRAMPOLINE */
          .long   0x0000ffff
-        .long   0x00009a00
+        .long   0x00009b00
          /* 0x0028: real-mode data @ BOOT_TRAMPOLINE */

xen/arch/x86/boot/trampoline.S trampoline_gdt: · line 82

Quoted 10 lines
xen/arch/x86/boot/trampoline.S +3 -2
          .long   0x0000ffff
-        .long   0x00009200
+        .long   0x00009300
          /*
           * 0x0030: ring 0 Xen data, 16 MiB size, base
           * address is computed at runtime.
           */
-        .quad   0x00c0920000000fff
+        .quad   0x00c0930000000fff
+.Ltramopline_gdt_end:

With the typo here fixed

But may I talk you into ...

xen/arch/x86/x86_64/kexec_reloc.S compat_mode_gdt_desc: · line 186

Quoted 10 lines
xen/arch/x86/x86_64/kexec_reloc.S +2 -2
@@ -182,8 +182,8 @@ compat_mode_gdt_desc:
          .align 8
  compat_mode_gdt:
          .quad 0x0000000000000000     /* null                              */
-        .quad 0x00cf92000000ffff     /* 0x0008 ring 0 data                */
-        .quad 0x00cf9a000000ffff     /* 0x0010 ring 0 code, compatibility */
+        .quad 0x00cf93000000ffff     /* 0x0008 ring 0 data                */
+        .quad 0x00cf9b000000ffff     /* 0x0010 ring 0 code, compatibility */

... introducing a similar "end" label here and using it at compat_mode_gdt_desc (the R-b above stands with that change)?

Jan

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

#3 lore raw

PATCH 1/1 x86/boot: Reposition trampoline data

In reply to Andrew Cooper

... to separate code from data. In particular, trampoline_realmode_entry's write to trampoline_cpu_started clobbers the I-cache line containing trampoline_protmode_entry, which won't be great for AP startup.

Reformat the comments for trampoline_gdt to reduce their volume.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/boot/trampoline.S | 67 ++++++++++++++++++------------------------
 1 file changed, 28 insertions(+), 39 deletions(-)
xen/arch/x86/boot/trampoline.S +28 -39
index 1761fc1213..1b11b4757a 100644
@@ -59,45 +59,6 @@ GLOBAL(trampoline_realmode_entry)
         lmsw    %ax                       # CR0.PE = 1 (enter protected mode)
         ljmpl   $BOOT_CS32,$bootsym_rel(trampoline_protmode_entry,6)
 
-trampoline_gdt:
-        /* 0x0000: unused */
-        .quad   0x0000000000000000
-        /* 0x0008: ring 0 code, 32-bit mode */
-        .quad   0x00cf9b000000ffff
-        /* 0x0010: ring 0 code, 64-bit mode */
-        .quad   0x00af9b000000ffff
-        /* 0x0018: ring 0 data */
-        .quad   0x00cf93000000ffff
-        /* 0x0020: real-mode code @ BOOT_TRAMPOLINE */
-        .long   0x0000ffff
-        .long   0x00009b00
-        /* 0x0028: real-mode data @ BOOT_TRAMPOLINE */
-        .long   0x0000ffff
-        .long   0x00009300
-        /*
-         * 0x0030: ring 0 Xen data, 16 MiB size, base
-         * address is computed at runtime.
-         */
-        .quad   0x00c0930000000fff
-.Ltramopline_gdt_end:
-
-        .pushsection .trampoline_rel, "a"
-        .long   trampoline_gdt + BOOT_PSEUDORM_CS + 2 - .
-        .long   trampoline_gdt + BOOT_PSEUDORM_DS + 2 - .
-        .popsection
-
-GLOBAL(trampoline_misc_enable_off)
-        .quad   0
-
-GLOBAL(cpuid_ext_features)
-        .long   0
-
-GLOBAL(trampoline_xen_phys_start)
-        .long   0
-
-GLOBAL(trampoline_cpu_started)
-        .byte   0
-
         .code32
 trampoline_protmode_entry:
         /* Set up a few descriptors: on entry only CS is guaranteed good. */
@@ -186,6 +147,34 @@ idt_48: .word   0, 0, 0 # base = limit = 0
 gdt_48: .word   .Ltramopline_gdt_end - trampoline_gdt - 1
         .long   bootsym_rel(trampoline_gdt,4)
 
+trampoline_gdt:
+        .quad   0x0000000000000000 /* 0x0000: unused */
+        .quad   0x00cf9b000000ffff /* 0x0008: ring 0 code, 32-bit mode */
+        .quad   0x00af9b000000ffff /* 0x0010: ring 0 code, 64-bit mode */
+        .quad   0x00cf93000000ffff /* 0x0018: ring 0 data */
+        .quad   0x00009b000000ffff /* 0x0020: real-mode code @ BOOT_TRAMPOLINE */
+        .quad   0x000093000000ffff /* 0x0028: real-mode data @ BOOT_TRAMPOLINE */
+        .quad   0x00c0930000000fff /* 0x0030: ring 0 Xen data, 16M @ XEN */
+.Ltramopline_gdt_end:
+
+        /* Relocations for trampoline Real Mode segments. */
+        .pushsection .trampoline_rel, "a"
+        .long   trampoline_gdt + BOOT_PSEUDORM_CS + 2 - .
+        .long   trampoline_gdt + BOOT_PSEUDORM_DS + 2 - .
+        .popsection
+
+GLOBAL(trampoline_misc_enable_off)
+        .quad   0
+
+GLOBAL(cpuid_ext_features)
+        .long   0
+
+GLOBAL(trampoline_xen_phys_start)
+        .long   0
+
+GLOBAL(trampoline_cpu_started)
+        .byte   0
+
 /* The first page of trampoline is permanent, the rest boot-time only. */
 /* Reuse the boot trampoline on the 1st trampoline page as stack for wakeup. */
         .equ    wakeup_stack, trampoline_start + PAGE_SIZE
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#4 lore raw

Re: [Xen-devel] [PATCH] x86/boot: Reposition trampoline data

Quoted 8 lines · Andrew Cooper

... to separate code from data. In particular, trampoline_realmode_entry's
write to trampoline_cpu_started clobbers the I-cache line containing trampoline_protmode_entry, which won't be great for AP startup.

Reformat the comments for trampoline_gdt to reduce their volume.

No functional change.

Please, let's not do this one until my other boot cleanups have landed. It just hurts. I have also reordered some of these for functional reasons, because they are used in different contexts (and end up in completely different trampolines).

Quoted 101 lines

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/boot/trampoline.S | 67 ++++++++++++++++++------------
------------
 1 file changed, 28 insertions(+), 39 deletions(-)
xen/arch/x86/boot/trampoline.S +0 -39
index 1761fc1213..1b11b4757a 100644
@@ -59,45 +59,6 @@ GLOBAL(trampoline_realmode_entry)
         lmsw    %ax                       # CR0.PE = 1 (enter
 protected mode)
         ljmpl   $BOOT_CS32,$bootsym_rel(trampoline_protmode_entry,6)
 
-trampoline_gdt:
-        /* 0x0000: unused */
-        .quad   0x0000000000000000
-        /* 0x0008: ring 0 code, 32-bit mode */
-        .quad   0x00cf9b000000ffff
-        /* 0x0010: ring 0 code, 64-bit mode */
-        .quad   0x00af9b000000ffff
-        /* 0x0018: ring 0 data */
-        .quad   0x00cf93000000ffff
-        /* 0x0020: real-mode code @ BOOT_TRAMPOLINE */
-        .long   0x0000ffff
-        .long   0x00009b00
-        /* 0x0028: real-mode data @ BOOT_TRAMPOLINE */
-        .long   0x0000ffff
-        .long   0x00009300
-        /*
-         * 0x0030: ring 0 Xen data, 16 MiB size, base
-         * address is computed at runtime.
-         */
-        .quad   0x00c0930000000fff
-.Ltramopline_gdt_end:
-
-        .pushsection .trampoline_rel, "a"
-        .long   trampoline_gdt + BOOT_PSEUDORM_CS + 2 - .
-        .long   trampoline_gdt + BOOT_PSEUDORM_DS + 2 - .
-        .popsection
-
-GLOBAL(trampoline_misc_enable_off)
-        .quad   0
-
-GLOBAL(cpuid_ext_features)
-        .long   0
-
-GLOBAL(trampoline_xen_phys_start)
-        .long   0
-
-GLOBAL(trampoline_cpu_started)
-        .byte   0
-
         .code32
 trampoline_protmode_entry:
         /* Set up a few descriptors: on entry only CS is guaranteed

good. */

xen/arch/x86/boot/trampoline.S +3 -0
@@ -186,6 +147,34 @@ idt_48: .word   0, 0, 0 # base = limit = 0
 gdt_48: .word   .Ltramopline_gdt_end - trampoline_gdt - 1
         .long   bootsym_rel(trampoline_gdt,4)
 
+trampoline_gdt:
+        .quad   0x0000000000000000 /* 0x0000: unused */
+        .quad   0x00cf9b000000ffff /* 0x0008: ring 0 code, 32-bit

mode */
+ .quad 0x00af9b000000ffff /* 0x0010: ring 0 code, 64-bit
mode */

xen/arch/x86/boot/trampoline.S +2 -0
+        .quad   0x00cf93000000ffff /* 0x0018: ring 0 data */
+        .quad   0x00009b000000ffff /* 0x0020: real-mode code @

BOOT_TRAMPOLINE */
+ .quad 0x000093000000ffff /* 0x0028: real-mode data @
BOOT_TRAMPOLINE */
+ .quad 0x00c0930000000fff /* 0x0030: ring 0 Xen data, 16M @
XEN */
+.Ltramopline_gdt_end:

xen/arch/x86/boot/trampoline.S +19 -0
+
+        /* Relocations for trampoline Real Mode segments. */
+        .pushsection .trampoline_rel, "a"
+        .long   trampoline_gdt + BOOT_PSEUDORM_CS + 2 - .
+        .long   trampoline_gdt + BOOT_PSEUDORM_DS + 2 - .
+        .popsection
+
+GLOBAL(trampoline_misc_enable_off)
+        .quad   0
+
+GLOBAL(cpuid_ext_features)
+        .long   0
+
+GLOBAL(trampoline_xen_phys_start)
+        .long   0
+
+GLOBAL(trampoline_cpu_started)
+        .byte   0
+
 /* The first page of trampoline is permanent, the rest boot-time
Quoted 4 lines

only. */
/* Reuse the boot trampoline on the 1st trampoline page as stack for
wakeup. */
.equ wakeup_stack, trampoline_start + PAGE_SIZE

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

Acked-by on v1 1/1
#5 lore raw

Re: [Xen-devel] [PATCH] x86/boot: Reposition trampoline data

In reply to Andrew Cooper
Quoted 9 lines · Andrew Cooper

... to separate code from data. In particular, trampoline_realmode_entry's write to trampoline_cpu_started clobbers the I-cache line containing trampoline_protmode_entry, which won't be great for AP startup.

Reformat the comments for trampoline_gdt to reduce their volume.

No functional change.

, ... subject to David's concern wrt his series.

Jan

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

#6 lore raw

PATCH 1/1 x86/boot: Drop all use of lmsw

In reply to Andrew Cooper

lmsw is an obsolete relic of the 286 processor - so much so that it even lacks intercept assistance on AMD processors.

Use a plain mov to %cr0 which is easier to follow, certainly faster to virtualise on AMD hardware, and almost certainly a faster microcode path in real hardware.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/boot/trampoline.S | 12 ++++++------
 xen/arch/x86/boot/wakeup.S     |  5 +++--
 2 files changed, 9 insertions(+), 8 deletions(-)
xen/arch/x86/boot/trampoline.S +6 -6
index 1b11b4757a..89f841331d 100644
@@ -54,9 +54,10 @@ GLOBAL(trampoline_realmode_entry)
         lidt    bootsym(idt_48)
         lgdt    bootsym(gdt_48)
         mov     $1,%bl                    # EBX != 0 indicates we are an AP
-        xor     %ax, %ax
-        inc     %ax
-        lmsw    %ax                       # CR0.PE = 1 (enter protected mode)
+
+        mov     $X86_CR0_PE, %eax
+        mov     %eax, %cr0
+
         ljmpl   $BOOT_CS32,$bootsym_rel(trampoline_protmode_entry,6)
 
         .code32
@@ -252,9 +253,8 @@ trampoline_boot_cpu_entry:
         lgdt    bootsym(gdt_48)
 
         /* Enter protected mode, and flush insn queue. */
-        xor     %ax,%ax
-        inc     %ax
-        lmsw    %ax                       # CR0.PE = 1 (enter protected mode)
+        mov     $X86_CR0_PE, %eax
+        mov     %eax, %cr0
 
         /* Load proper protected-mode values into all segment registers. */
         ljmpl   $BOOT_CS32,$bootsym_rel(1f,6)
xen/arch/x86/boot/wakeup.S +3 -2
index e3cb9e033a..2af6c6017c 100644
@@ -51,8 +51,9 @@ ENTRY(wakeup_start)
         lidt    wakesym(idt_48)
         lgdt    wakesym(gdt_48)
 
-        movw    $1, %ax
-        lmsw    %ax             # Turn on CR0.PE 
+        mov     $X86_CR0_PE, %eax
+        mov     %eax, %cr0
+
         ljmpl   $BOOT_CS32, $bootsym_rel(wakeup_32, 6)
 
 /* This code uses an extended set of video mode numbers. These include:
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v1 1/1
#7 lore raw

Re: [Xen-devel] [PATCH] x86/boot: Drop all use of lmsw

Quoted 8 lines · Andrew Cooper

lmsw is an obsolete relic of the 286 processor - so much so that it even lacks intercept assistance on AMD processors.

Use a plain mov to %cr0 which is easier to follow, certainly faster to virtualise on AMD hardware, and almost certainly a faster microcode path in real hardware.

with one further suggestion:

xen/arch/x86/boot/trampoline.S GLOBAL(trampoline_realmode_entry) · line 59

Quoted 12 lines
xen/arch/x86/boot/trampoline.S +3 -3
@@ -54,9 +54,10 @@ GLOBAL(trampoline_realmode_entry)
         lidt    bootsym(idt_48)
         lgdt    bootsym(gdt_48)
         mov     $1,%bl                    # EBX != 0 indicates we are an AP
-        xor     %ax, %ax
-        inc     %ax
-        lmsw    %ax                       # CR0.PE = 1 (enter protected mode)
+
+        mov     $X86_CR0_PE, %eax
+        mov     %eax, %cr0

Why don't you use %ebx here, allowing the move to %bl to be dropped at the same time?

Jan

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