diff mbox series

[RFC,5/5] setup mapping for trampoline in setup_pagesXX

Message ID 20240904145648.33707-6-frediano.ziglio@cloud.com (mailing list archive)
State Superseded
Headers show
Series Reuse 32 bit C code more safely | expand

Commit Message

Frediano Ziglio Sept. 4, 2024, 2:56 p.m. UTC
Reduce assembly code, make boot page mappings more similar between
multiple paths (direct EFI and not).

---
 xen/arch/x86/boot/build32.lds.S |  2 ++
 xen/arch/x86/boot/head.S        | 10 ----------
 xen/arch/x86/boot/setup-pages.c | 25 ++++++++++++++++++-------
 xen/arch/x86/boot/x86_64.S      |  2 +-
 xen/arch/x86/include/asm/page.h |  3 ++-
 5 files changed, 23 insertions(+), 19 deletions(-)
diff mbox series

Patch

diff --git a/xen/arch/x86/boot/build32.lds.S b/xen/arch/x86/boot/build32.lds.S
index 3796f9603b..aca747eb1d 100644
--- a/xen/arch/x86/boot/build32.lds.S
+++ b/xen/arch/x86/boot/build32.lds.S
@@ -49,11 +49,13 @@  SECTIONS
         DECLARE_IMPORT(__trampoline_seg_stop);
         DECLARE_IMPORT(l2_xenmap);
         DECLARE_IMPORT(l2_directmap);
+        DECLARE_IMPORT(l1_bootmap);
         DECLARE_IMPORT(l2_bootmap);
         DECLARE_IMPORT(l3_bootmap);
         DECLARE_IMPORT(_start);
         DECLARE_IMPORT(_end);
         DECLARE_IMPORT(xen_phys_start);
+        DECLARE_IMPORT(trampoline_phys);
         //DECLARE_IMPORT();
         . = . + GAP;
         *(.text)
diff --git a/xen/arch/x86/boot/head.S b/xen/arch/x86/boot/head.S
index 5ae0c2009e..84d3c469de 100644
--- a/xen/arch/x86/boot/head.S
+++ b/xen/arch/x86/boot/head.S
@@ -765,16 +765,6 @@  trampoline_setup:
 
         call    setup_pages32
 
-        /* Map l1_bootmap[] into l2_bootmap[0]. */
-        lea     __PAGE_HYPERVISOR + sym_esi(l1_bootmap), %eax
-        mov     %eax, sym_esi(l2_bootmap)
-
-        /* Map the permanent trampoline page into l1_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)
-
         /* Apply relocations to bootstrap trampoline. */
         mov     sym_esi(trampoline_phys), %eax
         call    reloc_trampoline32
diff --git a/xen/arch/x86/boot/setup-pages.c b/xen/arch/x86/boot/setup-pages.c
index 0961282a01..f74734c036 100644
--- a/xen/arch/x86/boot/setup-pages.c
+++ b/xen/arch/x86/boot/setup-pages.c
@@ -9,8 +9,10 @@ 
 
 #pragma GCC visibility push(hidden)
 extern char _start[], _end[];
-extern uint64_t l2_xenmap[512], l3_bootmap[512], l2_directmap[512], l2_bootmap[512];
+extern uint64_t l2_xenmap[512], l2_directmap[512],
+	l3_bootmap[512], l2_bootmap[512], l1_bootmap[512];
 extern unsigned long xen_phys_start;
+extern unsigned long trampoline_phys;
 #pragma GCC visibility pop
 
 #define _PAGE_PRESENT 0x001
@@ -19,12 +21,11 @@  extern unsigned long xen_phys_start;
 #define _PAGE_DIRTY 0x040
 #define _PAGE_PSE 0x080
 #define _PAGE_GLOBAL 0x100
+#define _PAGE_NX (1LLU << 63)
 
 #define PAGE_HYPERVISOR       PAGE_HYPERVISOR_RW
 #define PAGE_HYPERVISOR_RW    (__PAGE_HYPERVISOR_RW | _PAGE_GLOBAL)
 #define __PAGE_HYPERVISOR_RW  (__PAGE_HYPERVISOR_RO | _PAGE_DIRTY | _PAGE_RW)
-// TODO
-#define _PAGE_NX 0
 #define __PAGE_HYPERVISOR_RO  (_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_NX)
 #define PAGE_HYPERVISOR_RWX   (__PAGE_HYPERVISOR | _PAGE_GLOBAL)
 #define __PAGE_HYPERVISOR     (__PAGE_HYPERVISOR_RX | _PAGE_DIRTY | _PAGE_RW)
@@ -33,14 +34,16 @@  extern unsigned long xen_phys_start;
 #define L2_PAGETABLE_SHIFT 21
 #define L2_PAGETABLE_ENTRIES 512
 #define PAGE_SIZE 4096
+#define PAGE_SHIFT 12
 #define l2_table_offset(a) (((a) >> L2_PAGETABLE_SHIFT) & (L2_PAGETABLE_ENTRIES - 1))
-#define l2e_from_paddr(a,f) ((a) | put_pte_flags(f))
-#define l3e_from_paddr(a,f) ((a) | put_pte_flags(f))
+#define l1e_from_paddr(a, flags) ((a) | put_pte_flags(flags))
+#define l2e_from_paddr(a, flags) ((a) | put_pte_flags(flags))
+#define l3e_from_paddr(a, flags) ((a) | put_pte_flags(flags))
 #define l2e_add_flags(x, flags)    (x |= put_pte_flags(flags))
 typedef uint64_t l2_pgentry_t;
-static inline int64_t put_pte_flags(unsigned int x)
+static inline uint64_t put_pte_flags(uint64_t x)
 {
-    return (((int64_t)x & ~0xfff) << 40) | (x & 0xfff);
+    return x;
 }
 
 void __attribute__((__stdcall__)) setup_pages32(void)
@@ -102,4 +105,12 @@  void setup_pages64(void)
         l2_directmap[i] = pte;
     }
 #undef l2_4G_offset
+
+    /* Map l1_bootmap[] into l2_bootmap[0]. */
+    l2_bootmap[0] = l2e_from_paddr((unsigned long)l1_bootmap,
+                                   __PAGE_HYPERVISOR);
+
+    /* Map the permanent trampoline page into l1_bootmap[]. */
+    l1_bootmap[(unsigned long)trampoline_phys >> PAGE_SHIFT] =
+        l1e_from_paddr((unsigned long)trampoline_phys, __PAGE_HYPERVISOR_RX);
 }
diff --git a/xen/arch/x86/boot/x86_64.S b/xen/arch/x86/boot/x86_64.S
index 04bb62ae86..230ae6e2fb 100644
--- a/xen/arch/x86/boot/x86_64.S
+++ b/xen/arch/x86/boot/x86_64.S
@@ -195,7 +195,7 @@  GLOBAL(__page_tables_end)
         .section .init.data, "aw", @progbits
         .align PAGE_SIZE, 0
 
-l1_bootmap:
+GLOBAL(l1_bootmap)
         .fill L1_PAGETABLE_ENTRIES, 8, 0
         .size l1_bootmap, . - l1_bootmap
 
diff --git a/xen/arch/x86/include/asm/page.h b/xen/arch/x86/include/asm/page.h
index e01af28916..7e8c506dbc 100644
--- a/xen/arch/x86/include/asm/page.h
+++ b/xen/arch/x86/include/asm/page.h
@@ -286,7 +286,8 @@  extern l2_pgentry_t l2_xenmap[L2_PAGETABLE_ENTRIES],
     l2_bootmap[4*L2_PAGETABLE_ENTRIES];
 extern l3_pgentry_t l3_bootmap[L3_PAGETABLE_ENTRIES];
 extern l2_pgentry_t l2_directmap[4*L2_PAGETABLE_ENTRIES];
-extern l1_pgentry_t l1_fixmap[L1_PAGETABLE_ENTRIES];
+extern l1_pgentry_t l1_fixmap[L1_PAGETABLE_ENTRIES],
+    l1_bootmap[L1_PAGETABLE_ENTRIES];
 void paging_init(void);
 void efi_update_l4_pgtable(unsigned int l4idx, l4_pgentry_t l4e);
 #endif /* !defined(__ASSEMBLY__) */