Skip to contentSkip to conversation

x86/boot: Remove mappings at 0

PATCH In review Andrew Cooper 2 participants 8 messages Jan 9, 2020 – Jan 10, 2020

lore
Share & tools
lore
#0 lore raw

v2 cover x86/boot: Remove mappings at 0

This is the (long overdue) series which finally removes the mapping at 0 during early boot, which has bitten us in the past. It also removes an RWX gadget which persists past boot in the idle pagetables.

Most of the complexity was down to the differing (and hard-to-follow) uses of the bootmap. I first opted to get rid of the bootmap entirely. While this is possible for the current Multiboot paths, it is incompatible with the EFI boot path, and works against David's existing plans to not use the trampoline at all.

Further ideas: (not addressed here because -ETIME on my behalf.)

1) Get PV-shim to use hypercalls for AP startup, at which point we can compile out the trampoline entirely. This is probably helpful for robustness testing in combination with David's plans.

2) Drop BOOTSTRAP_MAP_{BASE,LIMIT} and have bootstrap_map() populate into the directmap, as we only request RAM mappings. This would allow us to drop 3 of the bootmap pagetables. However, I'm not entirely convinced the later logic will cope with cacheability boundaries forcing the use of small mappings.

This series has had complete testing for MB and EFI boot paths. It turns out that grub can chainload xen.efi and test those paths.

v2:
* Two patches already committed. See individual patches for other changes.

Andrew Cooper (4):
x86/boot: Remove the preconstructed low 16M superpage mappings x86/boot: Clean up l?_bootmap[] construction x86/boot: Don't map 0 during boot
x86/boot: Drop INVALID_VCPU

 xen/arch/x86/boot/head.S          | 32 ++++++++++++++++----------------
 xen/arch/x86/boot/x86_64.S        | 21 ++++++++++-----------
 xen/arch/x86/cpu/mcheck/mce.c     |  2 +-
 xen/arch/x86/domain_page.c        |  2 +-
 xen/arch/x86/efi/efi-boot.h       | 20 +++++++++++++-------
 xen/arch/x86/setup.c              | 16 ++++++++--------
 xen/arch/x86/tboot.c              |  2 +-
 xen/arch/x86/x86_64/asm-offsets.c |  3 ---
 xen/arch/x86/xen.lds.S            |  3 +++
 xen/include/asm-x86/setup.h       |  3 ---
 10 files changed, 53 insertions(+), 51 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 v2 1/4 x86/boot: Remove the preconstructed low 16M superpage mappings

These are left over from c/s b2804422 "x86: make Xen early boot code relocatable", which made it possible for Xen not to be in the bottom 16M.

Nothing using the mappings any more. Build them in the directmap when walking the E820 table along with everything else.

Furthermore, it is undefined to have superpages and MTRRs disagree on cacheability boundaries, and nothing actually checks. While we don't fix this explicitly, we do at least honour the E820 now if it says there are boundaries in this range.

As a consequence, there are now no _PAGE_PRESENT entries between __page_tables_{start,end} which need to skip relocation. This simplifies the MB1/2 entry path logic to remove the l2_identmap[] special case.

The low 2M (using 4k pages) is retained for now. Amongst other things, it matters for console logging while the legacy VGA hole is in use.

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

v2:
* Update commit message to explain what the low 16M used to be used for
* Add PREBUILT_MAP_LIMIT

---
 xen/arch/x86/boot/head.S          | 10 ++--------
 xen/arch/x86/boot/x86_64.S        | 17 ++++++-----------
 xen/arch/x86/setup.c              | 13 ++++++++-----
 xen/arch/x86/x86_64/asm-offsets.c |  3 ---
 4 files changed, 16 insertions(+), 27 deletions(-)
xen/arch/x86/boot/head.S +2 -8
index 250587fdf0..d152af4542 100644
@@ -661,15 +661,9 @@ trampoline_setup:
         mov     %eax,sym_fs(boot_tsc_stamp)
         mov     %edx,sym_fs(boot_tsc_stamp)+4
 
-        /*
-         * Update frame addresses in page tables excluding l2_identmap
-         * without its first entry which points to l1_identmap.
-         */
+        /* Relocate pagetables to point at Xen's current location in memory. */
         mov     $((__page_tables_end-__page_tables_start)/8),%ecx
-        mov     $(((l2_identmap-__page_tables_start)/8)+1),%edx
-1:      cmp     $((l2_identmap+l2_identmap_sizeof-__page_tables_start)/8),%ecx
-        cmove   %edx,%ecx
-        testl   $_PAGE_PRESENT,sym_fs(__page_tables_start)-8(,%ecx,8)
+1:      testl   $_PAGE_PRESENT,sym_fs(__page_tables_start)-8(,%ecx,8)
         jz      2f
         add     %esi,sym_fs(__page_tables_start)-8(,%ecx,8)
 2:      loop    1b
xen/arch/x86/boot/x86_64.S +6 -11
index 0acf5e860c..de555f87f4 100644
@@ -65,24 +65,19 @@ l1_identmap:
         .size l1_identmap, . - l1_identmap
 
 /*
- * __page_tables_start does not cover l1_identmap because it (l1_identmap)
- * contains 1-1 mappings. This means that frame addresses of these mappings
- * are static and should not be updated at runtime.
+ * __page_tables_{start,end} cover the range of pagetables which need
+ * relocating as Xen moves around physical memory.  i.e. each sym_offs()
+ * reference to a different pagetable in the Xen image.
  */
 GLOBAL(__page_tables_start)
 
 /*
- * Space for mapping the first 4GB of memory, with the first 16 megabytes
- * actualy mapped (mostly using superpages).  Uses 4x 4k pages.
+ * Space for 4G worth of 2M mappings, first 2M actually mapped via
+ * l1_identmap[].  Uses 4x 4k pages.
  */
 GLOBAL(l2_identmap)
         .quad sym_offs(l1_identmap) + __PAGE_HYPERVISOR
-        idx = 1
-        .rept 7
-        .quad (idx << L2_PAGETABLE_SHIFT) | PAGE_HYPERVISOR | _PAGE_PSE
-        idx = idx + 1
-        .endr
-        .fill 4 * L2_PAGETABLE_ENTRIES - 8, 8, 0
+        .fill 4 * L2_PAGETABLE_ENTRIES - 1, 8, 0
         .size l2_identmap, . - l2_identmap
 
 /*
xen/arch/x86/setup.c +8 -5
index ed54f79fea..62adc9e2a8 100644
@@ -678,6 +678,9 @@ static unsigned int __init copy_bios_e820(struct e820entry *map, unsigned int li
     return n;
 }
 
+/* How much of the directmap is prebuilt at compile time. */
+#define PREBUILT_MAP_LIMIT (1 << L2_PAGETABLE_SHIFT)
+
 void __init noreturn __start_xen(unsigned long mbi_p)
 {
     char *memmap_type = NULL;
@@ -1020,7 +1023,7 @@ void __init noreturn __start_xen(unsigned long mbi_p)
      *
      * We require superpage alignment because the boot allocator is
      * not yet initialised. Hence we can only map superpages in the
-     * address range BOOTSTRAP_MAP_BASE to 4GB, as this is guaranteed
+     * address range PREBUILT_MAP_LIMIT to 4GB, as this is guaranteed
      * not to require dynamic allocation of pagetables.
      *
      * As well as mapping superpages in that range, in preparation for
@@ -1036,10 +1039,10 @@ void __init noreturn __start_xen(unsigned long mbi_p)
         if ( boot_e820.map[i].type != E820_RAM )
             continue;
 
-        /* Superpage-aligned chunks from BOOTSTRAP_MAP_BASE. */
+        /* Superpage-aligned chunks from PREBUILT_MAP_LIMIT. */
         s = (boot_e820.map[i].addr + mask) & ~mask;
         e = (boot_e820.map[i].addr + boot_e820.map[i].size) & ~mask;
-        s = max_t(uint64_t, s, BOOTSTRAP_MAP_BASE);
+        s = max_t(uint64_t, s, PREBUILT_MAP_LIMIT);
         if ( s >= e )
             continue;
 
@@ -1346,8 +1349,8 @@ void __init noreturn __start_xen(unsigned long mbi_p)
 
         set_pdx_range(s >> PAGE_SHIFT, e >> PAGE_SHIFT);
 
-        /* Need to create mappings above BOOTSTRAP_MAP_BASE. */
-        map_s = max_t(uint64_t, s, BOOTSTRAP_MAP_BASE);
+        /* Need to create mappings above PREBUILT_MAP_LIMIT. */
+        map_s = max_t(uint64_t, s, PREBUILT_MAP_LIMIT);
         map_e = min_t(uint64_t, e,
                       ARRAY_SIZE(l2_identmap) << L2_PAGETABLE_SHIFT);
 
xen/arch/x86/x86_64/asm-offsets.c +0 -3
index f9cb78cfdb..07d2155bf5 100644
@@ -165,8 +165,5 @@ void __dummy__(void)
     OFFSET(MB2_efi64_ih, multiboot2_tag_efi64_ih_t, pointer);
     BLANK();
 
-    DEFINE(l2_identmap_sizeof, sizeof(l2_identmap));
-    BLANK();
-
     OFFSET(DOMAIN_vm_assist, struct domain, vm_assist);
 }
Signature · 7 lines
2.11.0


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

Re: [Xen-devel] [PATCH v2 1/4] x86/boot: Remove the preconstructed low 16M superpage mappings

Quoted 11 lines · Andrew Cooper

These are left over from c/s b2804422 "x86: make Xen early boot code relocatable", which made it possible for Xen not to be in the bottom 16M.

Nothing using the mappings any more. Build them in the directmap when walking the E820 table along with everything else.

Furthermore, it is undefined to have superpages and MTRRs disagree on cacheability boundaries, and nothing actually checks. While we don't fix this explicitly, we do at least honour the E820 now if it says there are boundaries in this range.

Quoted 8 lines

As a consequence, there are now no _PAGE_PRESENT entries between __page_tables_{start,end} which need to skip relocation. This simplifies the MB1/2 entry path logic to remove the l2_identmap[] special case.

The low 2M (using 4k pages) is retained for now. Amongst other things, it matters for console logging while the legacy VGA hole is in use.

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

#3 lore raw

PATCH v2 2/4 x86/boot: Clean up l?_bootmap[] construction

In reply to Andrew Cooper

The need for Xen to be identity mapped into the bootmap is not obvious, and differs between the MB and EFI boot paths.

The EFI side is further complicated by an attempt to cope with with l2_bootmap only being 4k long. This is undocumented, confusing, only works if Xen is the single object wanting mapping.

The pageables are common to both the MB and EFI builds, so simplify the EFI bootmap construction code to make exactly one identity-map of Xen, which now makes the two paths consistent. Comment both pieces of logic, explaining what the mappings are needed for.

Finally, leave a linker assert covering the fact that plenty of code blindly assumes that Xen is less that 16M. This wants fixing in due course.

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

v2:
* Extra BUILD_BUG_ON()'s

---
 xen/arch/x86/boot/head.S    |  8 ++++++--
 xen/arch/x86/efi/efi-boot.h | 20 +++++++++++++-------
 xen/arch/x86/xen.lds.S      |  3 +++
 3 files changed, 22 insertions(+), 9 deletions(-)
xen/arch/x86/boot/head.S +6 -2
index d152af4542..0b75d33a25 100644
@@ -668,7 +668,11 @@ trampoline_setup:
         add     %esi,sym_fs(__page_tables_start)-8(,%ecx,8)
 2:      loop    1b
 
-        /* Initialize L2 boot-map/direct map page table entries (16MB). */
+        /*
+         * Map Xen into the directmap (needed for early-boot pagetable
+         * handling/walking), and identity map Xen into bootmap (needed for
+         * the transition into long mode), using 2M superpages.
+         */
         lea     sym_esi(start),%ebx
         lea     (1<<L2_PAGETABLE_SHIFT)*7+(PAGE_HYPERVISOR|_PAGE_PSE)(%ebx),%eax
         shr     $(L2_PAGETABLE_SHIFT-3),%ebx
@@ -678,7 +682,7 @@ trampoline_setup:
         sub     $(1<<L2_PAGETABLE_SHIFT),%eax
         loop    1b
 
-        /* Initialize L3 boot-map page directory entry. */
+        /* Initialize L3 boot-map page directory entries. */
         lea     __PAGE_HYPERVISOR+(L2_PAGETABLE_ENTRIES*8)*3+sym_esi(l2_bootmap),%eax
         mov     $4,%ecx
 1:      mov     %eax,sym_fs(l3_bootmap)-8(,%ecx,8)
xen/arch/x86/efi/efi-boot.h +13 -7
index 9c036d5f4c..203a9d3bb2 100644
@@ -585,21 +585,27 @@ static void __init efi_arch_memory_setup(void)
     if ( !efi_enabled(EFI_LOADER) )
         return;
 
-    /* Initialise L2 identity-map and boot-map page table entries (16MB). */
+    /* Check that there is at least 4G of mapping space in l2_*map[] */
+    BUILD_BUG_ON((sizeof(l2_bootmap)  / L2_PAGETABLE_ENTRIES) < 4);
+    BUILD_BUG_ON((sizeof(l2_identmap) / L2_PAGETABLE_ENTRIES) < 4);
+
+    /* Initialize L3 boot-map page directory entries. */
+    for ( i = 0; i < 4; ++i )
+        l3_bootmap[i] = l3e_from_paddr((UINTN)l2_bootmap + i * PAGE_SIZE,
+                                       __PAGE_HYPERVISOR);
+    /*
+     * Map Xen into the directmap (needed for early-boot pagetable
+     * handling/walking), and identity map Xen into bootmap (needed for the
+     * transition from the EFI pagetables to Xen), using 2M superpages.
+     */
     for ( i = 0; i < 8; ++i )
     {
         unsigned int slot = (xen_phys_start >> L2_PAGETABLE_SHIFT) + i;
         paddr_t addr = slot << L2_PAGETABLE_SHIFT;
 
         l2_identmap[slot] = l2e_from_paddr(addr, PAGE_HYPERVISOR|_PAGE_PSE);
-        slot &= L2_PAGETABLE_ENTRIES - 1;
         l2_bootmap[slot] = l2e_from_paddr(addr, __PAGE_HYPERVISOR|_PAGE_PSE);
     }
-    /* Initialise L3 boot-map page directory entries. */
-    l3_bootmap[l3_table_offset(xen_phys_start)] =
-        l3e_from_paddr((UINTN)l2_bootmap, __PAGE_HYPERVISOR);
-    l3_bootmap[l3_table_offset(xen_phys_start + (8 << L2_PAGETABLE_SHIFT) - 1)] =
-        l3e_from_paddr((UINTN)l2_bootmap, __PAGE_HYPERVISOR);
 }
 
 static void __init efi_arch_handle_module(struct file *file, const CHAR16 *name,
xen/arch/x86/xen.lds.S +3 -0
index 111edb5360..7f82f64078 100644
@@ -381,3 +381,6 @@ ASSERT((trampoline_end - trampoline_start) < TRAMPOLINE_SPACE - MBI_SPACE_MIN,
     "not enough room for trampoline and mbi data")
 ASSERT((wakeup_stack - wakeup_stack_start) >= WAKEUP_STACK_MIN,
     "wakeup stack too small")
+
+/* Plenty of boot code assumes that Xen isn't larger than 16M. */
+ASSERT(_end - _start <= MB(16), "Xen too large for early-boot assumptions")
Signature · 7 lines
2.11.0


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

Re: [Xen-devel] [PATCH v2 2/4] x86/boot: Clean up l?_bootmap[] construction

Quoted 12 lines · Andrew Cooper

The need for Xen to be identity mapped into the bootmap is not obvious, and differs between the MB and EFI boot paths.

The EFI side is further complicated by an attempt to cope with with l2_bootmap only being 4k long. This is undocumented, confusing, only works if Xen is the single object wanting mapping.

The pageables are common to both the MB and EFI builds, so simplify the EFI bootmap construction code to make exactly one identity-map of Xen, which now makes the two paths consistent. Comment both pieces of logic, explaining what the mappings are needed for.

Quoted 4 lines

Finally, leave a linker assert covering the fact that plenty of code blindly assumes that Xen is less that 16M. This wants fixing in due course.

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

#5 lore raw

PATCH v2 3/4 x86/boot: Don't map 0 during boot

In reply to Andrew Cooper

In particular, it causes accidental NULL pointer dereferences to go unnoticed.

The majority of the early operation takes place either in Real mode, or Protected Unpaged mode. The only bit which requires pagetable mappings is the trampoline transition into Long mode and jump to the higher mappings, so there is no need for the whole bottom 2M to be mapped.

Introduce a new l1_bootmap in .init.data, and use it instead of l1_identmap. The EFI boot path doesn't pass through the trampoline, so doesn't need any adjustment.

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

---
 xen/arch/x86/boot/head.S   | 14 ++++++++------
 xen/arch/x86/boot/x86_64.S |  4 ++++
 2 files changed, 12 insertions(+), 6 deletions(-)
xen/arch/x86/boot/head.S +8 -6
index 0b75d33a25..d246e374f1 100644
@@ -689,12 +689,14 @@ trampoline_setup:
         sub     $(L2_PAGETABLE_ENTRIES*8),%eax
         loop    1b
 
-        /*
-         * During boot, hook 4kB mappings of first 2MB of memory into L2.
-         * This avoids mixing cachability for the legacy VGA region.
-         */
-        lea     __PAGE_HYPERVISOR+sym_esi(l1_identmap),%edi
-        mov     %edi,sym_fs(l2_bootmap)
+        /* Map the permanent trampoline page into l{1,2}_bootmap[]. */
+        mov     sym_esi(trampoline_phys), %ecx
+        lea     __PAGE_HYPERVISOR_RX(%ecx), %edx /* %edx = PTE to write  */
+        shr     $PAGE_SHIFT, %ecx                /* %ecx = Slot to write */
+        mov     %edx, sym_offs(l1_bootmap)(%esi, %ecx, 8)
+
+        lea     __PAGE_HYPERVISOR + sym_esi(l1_bootmap), %edx
+        mov     %edx, sym_esi(l2_bootmap)
 
         /* Apply relocations to bootstrap trampoline. */
         mov     sym_fs(trampoline_phys),%edx
xen/arch/x86/boot/x86_64.S +4 -0
index de555f87f4..af62850589 100644
@@ -155,6 +155,10 @@ GLOBAL(__page_tables_end)
         .section .init.data, "aw", @progbits
         .align PAGE_SIZE, 0
 
+l1_bootmap:
+        .fill L1_PAGETABLE_ENTRIES, 8, 0
+        .size l1_bootmap, . - l1_bootmap
+
 GLOBAL(l2_bootmap)
         .fill 4 * L2_PAGETABLE_ENTRIES, 8, 0
         .size l2_bootmap, . - l2_bootmap
Signature · 7 lines
2.11.0


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

PATCH v2 4/4 x86/boot: Drop INVALID_VCPU

In reply to Andrew Cooper

Now that NULL will fault at boot, there is no need for a special constant to signify "current not set up yet".

Since c/s fae249d23413 "x86/boot: Rationalise stack handling during early boot", the BSP cpu_info block is now consistently zero, so drop the adjacent re-zeroing.

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

v2:
* Drop re-zeroign of the cpu_info block

---
 xen/arch/x86/cpu/mcheck/mce.c | 2 +-
 xen/arch/x86/domain_page.c    | 2 +-
 xen/arch/x86/setup.c          | 3 ---
 xen/arch/x86/tboot.c          | 2 +-
 xen/include/asm-x86/setup.h   | 3 ---
 5 files changed, 3 insertions(+), 9 deletions(-)
xen/arch/x86/cpu/mcheck/mce.c +1 -1
index 29f3f9c5e3..198595ff97 100644
@@ -260,7 +260,7 @@ static int mca_init_global(uint32_t flags, struct mcinfo_global *mig)
                         &mig->mc_coreid, &mig->mc_core_threadid,
                         &mig->mc_apicid, NULL, NULL, NULL);
 
-    if ( curr != INVALID_VCPU )
+    if ( curr )
     {
         mig->mc_domid = curr->domain->domain_id;
         mig->mc_vcpuid = curr->vcpu_id;
xen/arch/x86/domain_page.c +1 -1
index 4a07cfb18e..dd32712d2f 100644
@@ -29,7 +29,7 @@ static inline struct vcpu *mapcache_current_vcpu(void)
      * When current isn't properly set up yet, this is equivalent to
      * running in an idle vCPU (callers must check for NULL).
      */
-    if ( v == INVALID_VCPU )
+    if ( !v )
         return NULL;
 
     /*
xen/arch/x86/setup.c +0 -3
index 62adc9e2a8..1b6ca4a47d 100644
@@ -707,9 +707,6 @@ void __init noreturn __start_xen(unsigned long mbi_p)
 
     /* Critical region without IDT or TSS.  Any fault is deadly! */
 
-    set_processor_id(0);
-    set_current(INVALID_VCPU); /* debug sanity. */
-    idle_vcpu[0] = current;
     init_shadow_spec_ctrl_state();
 
     percpu_init_areas();
xen/arch/x86/tboot.c +1 -1
index 3e828fe204..5020c4ad49 100644
@@ -392,7 +392,7 @@ void tboot_shutdown(uint32_t shutdown_type)
      * During early boot, we can be called by panic before idle_vcpu[0] is
      * setup, but in that case we don't need to change page tables.
      */
-    if ( idle_vcpu[0] != INVALID_VCPU )
+    if ( idle_vcpu[0] )
         write_ptbase(idle_vcpu[0]);
 
     ((void(*)(void))(unsigned long)g_tboot_shared->shutdown_entry)();
xen/include/asm-x86/setup.h +0 -3
index 861d46d6ac..28257bc5c8 100644
@@ -4,9 +4,6 @@
 #include <xen/multiboot.h>
 #include <asm/numa.h>
 
-/* vCPU pointer used prior to there being a valid one around */
-#define INVALID_VCPU ((struct vcpu *)0xccccccccccccc000UL)
-
 extern const char __2M_text_start[], __2M_text_end[];
 extern const char __2M_rodata_start[], __2M_rodata_end[];
 extern char __2M_init_start[], __2M_init_end[];
Signature · 7 lines
2.11.0


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

Re: [Xen-devel] [PATCH v2 4/4] x86/boot: Drop INVALID_VCPU

Quoted 8 lines · Andrew Cooper

Now that NULL will fault at boot, there is no need for a special constant to signify "current not set up yet".

Since c/s fae249d23413 "x86/boot: Rationalise stack handling during early boot", the BSP cpu_info block is now consistently zero, so drop the adjacent re-zeroing.

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