[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v8 58/65] x86/mm: map each CPU's stack at a per-CPU address in the per-domain area



From: Roger Pau Monne <roger.pau@xxxxxxxxxx>

A pCPU's stack is xenheap memory, used at its directmap address, so
every root page-table maps every pCPU's stack.  Mapping a stack in fewer
places needs an address at which root page-tables can differ: outside the
directmap, in an area whose page-tables are not the same for every root.

That address has to be one per pCPU, and the same in every root
page-table the pCPU may load.  The pCPU runs on its stack across CR3
writes (in __context_switch(), in the lazy switch's catch-up, around EFI
runtime calls), and the hardware holds the address from bring-up on: the
TSS and IST, MSR_PL0_SSP and the shadow stack tokens, FRED's stack
levels, SYSENTER_ESP and the SYSCALL stubs, VMX's HOST_RSP.  A root
page-table loaded on several pCPUs at once, idle_pg_table or a 64-bit PV
guest's L4, then maps the stacks of all of them, each at its own address.

Give the stacks a fifth sub-area of the per-domain area, PCPU_STACK_SLOT,
with pCPU N's stack at PCPU_STACK_VIRT(N).  The 1 GiB sub-area holds
CONFIG_NR_CPUS stacks (checked at build time).  With per-CPU stacks in
use (cpu_stack_enabled, set from the cpu-stack option for HVM domains, a
PVH hardware domain included):

 - The idle domain gets a per-domain area, whose stack sub-area has the
   page-tables for every possible pCPU and maps each pCPU's stack as it
   comes up (cpu_stack_map(), from cpu_smpboot_alloc() for an AP, from
   __start_xen() for the BSP).  Its L3 goes into idle_pg_table once the
   BSP's idle vCPU is current: before any AP starts, and before the EFI
   runtime page-tables copy idle_pg_table's Xen slots.

 - Every other per-domain area, a domain's or a vCPU's, shares the idle
   domain's stack sub-area by reference, so that every root page-table
   maps every pCPU's stack at the same address.  free_perdomain_l3()
   never frees it.

 - Stack pages 0 and 5, the shadow stacks, are mapped as shadow stack
   pages there, as memguard_guard_stack() maps them at the directmap
   address.  The mappings are not global, so that root page-tables can
   later map the sub-area differently.

 - A stack is not freed while per-CPU stacks are in use, since its
   mappings would outlive it.  An offline pCPU stays parked on its stack
   anyway; what this keeps is the stack of a pCPU whose bring-up failed,
   for the next attempt.

Nothing runs on the stacks at these addresses yet, and no root
page-table maps less than before.

XPTI is unaffected: cpu-stack is off while XPTI is enabled.

Signed-off-by: Roger Pau Monné <roger.pau@xxxxxxxxxx>
Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8, Claude 
Code:claude-opus-5-5
Signed-off-by: George Dunlap <gwd@xxxxxxxxxxxxxx>
---
Changes in v8:
- New in this version.

Changes since previously posted version ("x86: adventures in Address Space 
Isolation", v2):
- Split out of "x86/mm: switch to a per-CPU mapped stack when using
  ASI" (now "x86/mm: run every CPU on a per-CPU stack address"), as
  part of an HVM-first rework: the option comes separately; the stacks
  are mapped at their per-CPU addresses here, and the CPUs run on them
  separately.
- Every per-domain area, vCPU-PT or not, shares the idle domain's stack
  sub-area: the per-vCPU mapping of the running pCPU's stack alone is
  done separately, at migration rather than at every switch, so
  destroy_perdomain_mapping() need not run with interrupts off and
  vcpu_set_stack_mappings() goes.
- The idle domain's per-domain area is created, and installed in
  idle_pg_table, explicitly once the BSP's idle vCPU is current, rather
  than by create_perdomain_mapping() at its creation (current is not
  yet set then, and the allocator reads it).
- A sub-area is shared whenever it is missing, rather than at vCPU 0
  for a domain-wide L3; free_perdomain_l3() skips it unconditionally.
- cpu_stack_enabled, resolved with the option, replaces tests of the
  knobs: it needs HVM, and a PVH hardware domain for the dom0 knob;
  without CONFIG_HVM it is false, so that such builds drop the per-CPU
  stack code.
- Stacks are not freed with per-CPU stacks in use.
- The stacks are mapped from stack_pg[], which cpu_stack_map() checks;
  the per-domain mapping helpers assert that only the idle vCPU writes
  the per-CPU stack sub-area.
- Move cpu_stack_map directly under cpu_stack_init, since they're more
  easily understood next to each other.
---
 xen/arch/x86/domain.c              |  3 +
 xen/arch/x86/include/asm/config.h  | 16 ++++-
 xen/arch/x86/include/asm/current.h |  6 ++
 xen/arch/x86/include/asm/smp.h     | 21 +++++++
 xen/arch/x86/mm.c                  | 95 ++++++++++++++++++++++++++++++
 xen/arch/x86/setup.c               | 10 ++++
 xen/arch/x86/smpboot.c             | 18 +++++-
 xen/arch/x86/spec_ctrl.c           | 10 ++++
 8 files changed, 177 insertions(+), 2 deletions(-)

diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c
index 1df44f6216..d1bb40fba4 100644
--- a/xen/arch/x86/domain.c
+++ b/xen/arch/x86/domain.c
@@ -590,6 +590,9 @@ int arch_vcpu_create(struct vcpu *v)
 
     if ( !is_idle_domain(d) )
     {
+        if ( cpu_stack_enabled )
+            vcpu_stack_mappings_init(v);
+
         BUILD_BUG_ON((MAX_VIRT_CPUS << VCPU_STATE_VA_SHIFT) >
                      (PERDOMAIN_SLOT_MBYTES << 20));
         BUILD_BUG_ON(VCPU_STATE_IDX_VMCB >= VCPU_STATE_NR_PAGES);
diff --git a/xen/arch/x86/include/asm/config.h 
b/xen/arch/x86/include/asm/config.h
index 5768b6efe3..7f114111bc 100644
--- a/xen/arch/x86/include/asm/config.h
+++ b/xen/arch/x86/include/asm/config.h
@@ -158,7 +158,7 @@
 /* Slot 260: per-domain mappings (including map cache). */
 #define PERDOMAIN_VIRT_START    (PML4_ADDR(260))
 #define PERDOMAIN_SLOT_MBYTES   (PML4_ENTRY_BYTES >> (20 + PAGETABLE_ORDER))
-#define PERDOMAIN_SLOTS         4
+#define PERDOMAIN_SLOTS         5
 #define PERDOMAIN_VIRT_SLOT(s)  (PERDOMAIN_VIRT_START + (s) * \
                                  (PERDOMAIN_SLOT_MBYTES << 20))
 /* Slot 4: mirror of per-domain mappings (for compat xlat area accesses). */
@@ -334,6 +334,20 @@ extern unsigned long xen_phys_start;
 #define VCPU_STATE_XSAVE_PAGES   3
 #define VCPU_STATE_IDX_XSAVE     4 /* First of VCPU_STATE_XSAVE_PAGES slots */
 
+/*
+ * Per-CPU stacks.  The fifth per-domain-mapping sub-area: when in use, it
+ * maps each CPU's stack at PCPU_STACK_VIRT(cpu), in every root page-table.
+ * The page-tables behind the sub-area are the idle domain's, mapping every
+ * CPU's stack, which every other per-domain area shares by reference.
+ */
+#define PCPU_STACK_SLOT          4
+#define PCPU_STACK_VIRT_START    PERDOMAIN_VIRT_SLOT(PCPU_STACK_SLOT)
+#define PCPU_STACK_VIRT_END      (PCPU_STACK_VIRT_START + \
+                                  (PERDOMAIN_SLOT_MBYTES << 20))
+#define PCPU_STACK_VIRT(cpu)     (PCPU_STACK_VIRT_START + \
+                                  ((unsigned long)(cpu) << \
+                                   (STACK_ORDER + PAGE_SHIFT)))
+
 #define ELFSIZE 64
 
 #define ARCH_CRASH_SAVE_VMCOREINFO
diff --git a/xen/arch/x86/include/asm/current.h 
b/xen/arch/x86/include/asm/current.h
index 0e2218c0c4..cc518b68b3 100644
--- a/xen/arch/x86/include/asm/current.h
+++ b/xen/arch/x86/include/asm/current.h
@@ -28,6 +28,12 @@
  * are unused.
  */
 
+/* Whether stack page @i holds shadow stacks (pages 0 and 5 above). */
+static inline bool is_shstk_slot(unsigned int i)
+{
+    return i == 0 || i == PRIMARY_SHSTK_SLOT;
+}
+
 /*
  * Identify which stack page the stack pointer is on.  Returns an index
  * as per the comment above.
diff --git a/xen/arch/x86/include/asm/smp.h b/xen/arch/x86/include/asm/smp.h
index 77c489cfe5..c144cfa36d 100644
--- a/xen/arch/x86/include/asm/smp.h
+++ b/xen/arch/x86/include/asm/smp.h
@@ -74,8 +74,29 @@ extern bool unaccounted_cpus;
 /* The page underlying each CPU's stack; see stack_base[]. */
 extern struct page_info *stack_pg[NR_CPUS];
 
+/*
+ * Whether every CPU's stack is mapped at PCPU_STACK_VIRT(cpu), as well as at
+ * its directmap address: set once the cpu-stack option is resolved, when
+ * per-CPU stacks are in use for any domain kind.
+ */
+#ifdef CONFIG_HVM
+extern bool cpu_stack_enabled;
+#else
+#define cpu_stack_enabled false
+#endif
+
 void *cpu_alloc_stack(unsigned int cpu);
 
+/* Give the idle domain the per-domain area mapping every CPU's stack. */
+void cpu_stack_init(void);
+
+/* Map @cpu's stack at PCPU_STACK_VIRT(@cpu) in the idle domain's area. */
+void cpu_stack_map(unsigned int cpu);
+
+struct vcpu;
+/* Set up the per-CPU stack sub-area of a non-idle vCPU's per-domain area. */
+void vcpu_stack_mappings_init(const struct vcpu *v);
+
 #endif /* !__ASSEMBLER__ */
 
 #endif
diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c
index 51f11d8a39..f9a0d50518 100644
--- a/xen/arch/x86/mm.c
+++ b/xen/arch/x86/mm.c
@@ -6468,6 +6468,8 @@ int create_perdomain_mapping(struct vcpu *v, unsigned 
long va,
 
     ASSERT(va >= PERDOMAIN_VIRT_START &&
            va < PERDOMAIN_VIRT_SLOT(PERDOMAIN_SLOTS));
+    /* The per-CPU stack sub-area is written through the idle vCPU only. */
+    ASSERT(l3_table_offset(va) != PCPU_STACK_SLOT || is_idle_vcpu(v));
 
     BUG_ON(!l3_pg);
 
@@ -6589,6 +6591,8 @@ void populate_perdomain_mapping(const struct vcpu *v, 
unsigned long va,
 
     ASSERT(va >= PERDOMAIN_VIRT_START &&
            va < PERDOMAIN_VIRT_SLOT(PERDOMAIN_SLOTS));
+    /* The per-CPU stack sub-area is written through the idle vCPU only. */
+    ASSERT(l3_table_offset(va) != PCPU_STACK_SLOT || is_idle_vcpu(v));
     ASSERT(!nr || !l3_table_offset(va ^ (va + nr * PAGE_SIZE - 1)));
     ASSERT(local_irq_is_enabled());
 
@@ -6641,6 +6645,8 @@ void destroy_perdomain_mapping(const struct vcpu *v, 
unsigned long va,
 
     ASSERT(va >= PERDOMAIN_VIRT_START &&
            va < PERDOMAIN_VIRT_SLOT(PERDOMAIN_SLOTS));
+    /* The per-CPU stack sub-area is written through the idle vCPU only. */
+    ASSERT(l3_table_offset(va) != PCPU_STACK_SLOT || is_idle_vcpu(v));
     ASSERT(!nr || !l3_table_offset(va ^ (va + nr * PAGE_SIZE - 1)));
 
     if ( !l3_pg )
@@ -6685,6 +6691,86 @@ void destroy_perdomain_mapping(const struct vcpu *v, 
unsigned long va,
     unmap_domain_page(l3tab);
 }
 
+/*
+ * Give the idle domain a per-domain area whose stack sub-area maps every
+ * CPU's stack, each added as its CPU comes up.  Its L3 goes into
+ * idle_pg_table, which all CPUs load at once, and with it into the EFI
+ * runtime page-tables, which copy idle_pg_table's Xen slots.  This runs once
+ * the BSP's idle vCPU is current, before any AP starts.
+ */
+void __init cpu_stack_init(void)
+{
+    struct vcpu *idle = idle_vcpu[0];
+    unsigned int slot = l4_table_offset(PERDOMAIN_VIRT_START);
+    l4_pgentry_t l4e;
+
+    ASSERT(cpu_stack_enabled && current == idle);
+    BUG_ON(l4e_get_intpte(idle_pg_table[slot]));
+
+    /* The L3, and the tables for every possible CPU's stack. */
+    idle->domain->arch.perdomain_l3_pg = allocate_perdomain_l3(idle->domain);
+    if ( !idle->domain->arch.perdomain_l3_pg ||
+         create_perdomain_mapping(idle, PCPU_STACK_VIRT(0),
+                                  nr_cpu_ids << STACK_ORDER,
+                                  NIL(l1_pgentry_t *), NULL) )
+        panic("No memory for the per-CPU stack mappings\n");
+
+    l4e = l4e_from_page(idle->domain->arch.perdomain_l3_pg,
+                        __PAGE_HYPERVISOR_RW);
+    l4e_write(&idle_pg_table[slot], l4e);
+    efi_update_l4_pgtable(slot, l4e);
+}
+
+/*
+ * Map @cpu's stack at PCPU_STACK_VIRT(@cpu) in the idle domain's per-domain
+ * area, and so in every root page-table sharing its stack sub-area.
+ */
+void cpu_stack_map(unsigned int cpu)
+{
+    mfn_t mfn;
+    unsigned int i;
+
+    ASSERT(cpu_stack_enabled);
+    BUG_ON(cpu >= nr_cpu_ids || !stack_pg[cpu]);
+
+    mfn = page_to_mfn(stack_pg[cpu]);
+
+    for ( i = 0; i < (1U << STACK_ORDER); i++ )
+    {
+        mfn_t m = mfn_add(mfn, i);
+
+        populate_perdomain_mapping(idle_vcpu[0],
+                                   PCPU_STACK_VIRT(cpu) + i * PAGE_SIZE,
+                                   &m, 1,
+                                   is_shstk_slot(i) ? __PAGE_HYPERVISOR_SHSTK
+                                                    : __PAGE_HYPERVISOR_RW);
+    }
+}
+
+/*
+ * Every other per-domain L3, a domain's or a vCPU's, shares the idle domain's
+ * stack sub-area by reference, so that it maps every CPU's stack too.
+ * @v's per-domain L3 exists.
+ */
+void vcpu_stack_mappings_init(const struct vcpu *v)
+{
+    struct page_info *idle_l3pg = idle_vcpu[0]->domain->arch.perdomain_l3_pg;
+    l3_pgentry_t *l3tab;
+
+    ASSERT(cpu_stack_enabled && !is_idle_vcpu(v));
+    BUG_ON(!idle_l3pg);
+
+    l3tab = __map_domain_page(perdomain_l3(v));
+    if ( !(l3e_get_flags(l3tab[PCPU_STACK_SLOT]) & _PAGE_PRESENT) )
+    {
+        const l3_pgentry_t *idle_l3tab = __map_domain_page(idle_l3pg);
+
+        l3e_write(&l3tab[PCPU_STACK_SLOT], idle_l3tab[PCPU_STACK_SLOT]);
+        unmap_domain_page(idle_l3tab);
+    }
+    unmap_domain_page(l3tab);
+}
+
 /*
  * Take @nr slots of @v's state window, from @idx, out of the per-domain
  * area, before the caller frees the pages behind them: the pages are the
@@ -6738,6 +6824,14 @@ static void free_perdomain_l3(struct page_info *l3pg)
     unsigned int i;
 
     for ( i = 0; i < PERDOMAIN_SLOTS; ++i)
+    {
+        /*
+         * The per-CPU stack sub-area holds page-tables the area does not
+         * own: the idle domain's, shared by reference.
+         */
+        if ( i == PCPU_STACK_SLOT )
+            continue;
+
         if ( l3e_get_flags(l3tab[i]) & _PAGE_PRESENT )
         {
             struct page_info *l2pg = l3e_get_page(l3tab[i]);
@@ -6770,6 +6864,7 @@ static void free_perdomain_l3(struct page_info *l3pg)
             unmap_domain_page(l2tab);
             free_domheap_page(l2pg);
         }
+    }
 
     unmap_domain_page(l3tab);
     free_domheap_page(l3pg);
diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c
index 3a9edb7427..38d68167a9 100644
--- a/xen/arch/x86/setup.c
+++ b/xen/arch/x86/setup.c
@@ -2089,6 +2089,9 @@ void asmlinkage __init noreturn __start_xen(void)
 
     init_idle_domain();
 
+    if ( cpu_stack_enabled )
+        cpu_stack_init();
+
     init_stubs();
 
     bsp_traps_reinit(); /* Needs stubs allocated, must be before 
presmp_initcalls. */
@@ -2130,9 +2133,16 @@ void asmlinkage __init noreturn __start_xen(void)
 
     boot_apply_alt_calls();
 
+    /*
+     * The BSP's stack is allocated once the speculation mitigations are
+     * resolved, as they decide whether it is mapped at PCPU_STACK_VIRT(0),
+     * and with interrupts enabled, as mapping it there needs.
+     */
     bsp_stack = cpu_alloc_stack(0); /* Needs to know IDT vs FRED */
     if ( !bsp_stack )
         panic("No memory for BSP stack\n");
+    if ( cpu_stack_enabled )
+        cpu_stack_map(0);
 
     /*
      * NB: when running as a PV shim VCPUOP_up/down is wired to the shim
diff --git a/xen/arch/x86/smpboot.c b/xen/arch/x86/smpboot.c
index 4bd37e3c99..f2716e1257 100644
--- a/xen/arch/x86/smpboot.c
+++ b/xen/arch/x86/smpboot.c
@@ -96,6 +96,10 @@ void *stack_base[NR_CPUS];
  */
 struct page_info *stack_pg[NR_CPUS];
 
+#ifdef CONFIG_HVM
+bool __ro_after_init cpu_stack_enabled;
+#endif
+
 void initialize_cpu_data(unsigned int cpu)
 {
     struct cpuinfo_x86 *c = &cpu_data[cpu];
@@ -1036,7 +1040,12 @@ static void cpu_smpboot_free(unsigned int cpu, bool 
remove)
         FREE_XENHEAP_PAGE(per_cpu(gdt, cpu));
         FREE_XENHEAP_PAGE(per_cpu(idt, cpu));
 
-        if ( stack_base[cpu] )
+        /*
+         * With per-CPU stacks the stack is kept, as its mappings at
+         * PCPU_STACK_VIRT(cpu), in the idle domain's per-domain area and the
+         * EFI runtime page-tables, stay: a later bring-up uses it again.
+         */
+        if ( stack_base[cpu] && !cpu_stack_enabled )
         {
             memguard_unguard_stack(stack_base[cpu]);
             FREE_XENHEAP_PAGES(stack_base[cpu], STACK_ORDER);
@@ -1080,6 +1089,9 @@ static int cpu_smpboot_alloc(unsigned int cpu)
          (stack_base[cpu] = cpu_alloc_stack(cpu)) == NULL )
             goto out;
 
+    if ( cpu_stack_enabled )
+        cpu_stack_map(cpu);
+
     info = get_cpu_info_from_stack((unsigned long)stack_base[cpu]);
     memset(info, 0, sizeof(*info));
     info->processor_id = cpu;
@@ -1176,6 +1188,10 @@ static struct notifier_block cpu_smpboot_nfb = {
 
 void __init smp_prepare_cpus(void)
 {
+    /* Every possible CPU's stack fits in the sub-area, at an aligned VA. */
+    BUILD_BUG_ON(PCPU_STACK_VIRT(CONFIG_NR_CPUS) > PCPU_STACK_VIRT_END);
+    BUILD_BUG_ON(PCPU_STACK_VIRT_START & (STACK_SIZE - 1));
+
     register_cpu_notifier(&cpu_smpboot_nfb);
 
     mtrr_aps_sync_begin();
diff --git a/xen/arch/x86/spec_ctrl.c b/xen/arch/x86/spec_ctrl.c
index feb46b5826..ccddad842d 100644
--- a/xen/arch/x86/spec_ctrl.c
+++ b/xen/arch/x86/spec_ctrl.c
@@ -2439,6 +2439,16 @@ void __init init_speculation_mitigations(void)
         opt_cpu_stack_hvm = opt_pvh_cpu_stack_hwdom = false;
     }
 
+#ifdef CONFIG_HVM
+    /*
+     * Per-CPU stacks give every CPU's stack an address of its own, whenever
+     * a domain kind uses them: HVM domains, a PVH hardware domain included
+     * (a PV one cannot).
+     */
+    cpu_stack_enabled = opt_cpu_stack_hvm ||
+                        (opt_dom0_pvh && opt_pvh_cpu_stack_hwdom);
+#endif
+
     l1tf_calculations();
 
     /*
-- 
2.56.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.