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

[PATCH v8 59/65] x86/mm: run every CPU on a per-CPU stack address



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

With per-CPU stacks in use, every pCPU's stack is mapped at
PCPU_STACK_VIRT(), in every root page-table.  Run every pCPU on it there:
an AP from its start, the BSP from the end of __start_xen(), where it
leaves cpu0_stack as it does today.

What the hardware holds is derived from the running stack pointer, as
before: the TSS and IST, MSR_PL0_SSP and the interrupt SSP table, FRED's
stack levels, SYSENTER_ESP and the SYSCALL stubs, VMX's HOST_RSP.  The
shadow stack tokens are not: they are written when the stack is
allocated, before anything runs on it.  So a per-CPU stack is guarded by
memguard_guard_cpu_stack(), which writes them for PCPU_STACK_VIRT(), a
function of its own as a later change allocates per-CPU stacks
differently.  The BSP's stack is allocated once the speculation
mitigations are resolved, and is guarded the same way.

stack_base[] keeps the stack's directmap address: the BSP's is recorded
before it switches, rather than taken from its stack pointer after.

No root page-table maps less than before: every one maps every pCPU's
stack, and still at its directmap address too, as long as stacks are
xenheap pages.  This is the first step towards vCPU page-tables which
map only the stack of the pCPU they run on.

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):
- Subject: was "x86/mm: switch to a per-CPU mapped stack when using
  ASI".
- HVM-first rework, split up: the option, the shadow stack tokens'
  address, the BSP stack's later allocation, the stacks' mapping at
  their per-CPU addresses and show_stack_overflow()'s check are patches
  of their own ("x86/spec-ctrl: add the cpu-stack sub-option of asi=",
  "x86/mm: write shadow stack tokens for the address the stack is used
  at", "x86/setup: allocate the BSP's stack after the speculation
  mitigations", "x86/mm: map each CPU's stack at a per-CPU address in
  the per-domain area", "x86/traps: have show_stack_overflow() expect
  the stack the TSS names").
- A per-CPU stack is guarded by memguard_guard_cpu_stack(), rather than
  by memguard_guard_stack() taking a CPU.
- Drop the change to __start_xen()'s AP bring-up test, which undid
  b0597289eb ("x86: always park offline CPUs").
---
 xen/arch/x86/include/asm/config.h |  8 +++----
 xen/arch/x86/include/asm/mm.h     |  1 +
 xen/arch/x86/include/asm/smp.h    |  6 +++---
 xen/arch/x86/mm.c                 | 36 +++++++++++++++++++++++++++++++
 xen/arch/x86/setup.c              | 12 ++++++-----
 xen/arch/x86/smpboot.c            | 10 +++++++--
 xen/arch/x86/spec_ctrl.c          |  6 +++---
 7 files changed, 62 insertions(+), 17 deletions(-)

diff --git a/xen/arch/x86/include/asm/config.h 
b/xen/arch/x86/include/asm/config.h
index 7f114111bc..079dde1340 100644
--- a/xen/arch/x86/include/asm/config.h
+++ b/xen/arch/x86/include/asm/config.h
@@ -335,10 +335,10 @@ extern unsigned long xen_phys_start;
 #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.
+ * Per-CPU stacks.  The fifth per-domain-mapping sub-area: when in use, each
+ * CPU runs on its stack at PCPU_STACK_VIRT(cpu), in every root page-table it
+ * loads.  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)
diff --git a/xen/arch/x86/include/asm/mm.h b/xen/arch/x86/include/asm/mm.h
index ae6f9f34c3..d5bb38f7d2 100644
--- a/xen/arch/x86/include/asm/mm.h
+++ b/xen/arch/x86/include/asm/mm.h
@@ -532,6 +532,7 @@ extern struct rangeset *mmio_ro_ranges;
 #define compat_cr3_to_pfn(cr3) (((unsigned)(cr3) >> 12) | ((unsigned)(cr3) << 
20))
 
 void memguard_guard_stack(void *p);
+void memguard_guard_cpu_stack(unsigned int cpu);
 void memguard_unguard_stack(void *p);
 
 /*
diff --git a/xen/arch/x86/include/asm/smp.h b/xen/arch/x86/include/asm/smp.h
index c144cfa36d..6836076076 100644
--- a/xen/arch/x86/include/asm/smp.h
+++ b/xen/arch/x86/include/asm/smp.h
@@ -75,9 +75,9 @@ extern bool unaccounted_cpus;
 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.
+ * Whether every CPU runs on its stack at PCPU_STACK_VIRT(cpu), rather than at
+ * the stack's 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;
diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c
index f9a0d50518..4a79e8af3a 100644
--- a/xen/arch/x86/mm.c
+++ b/xen/arch/x86/mm.c
@@ -6901,6 +6901,42 @@ static void write_sss_token(unsigned long *ptr, unsigned 
long va)
     *ptr = va;
 }
 
+/*
+ * Guard @cpu's per-CPU stack, stack_pg[@cpu], used at PCPU_STACK_VIRT(@cpu):
+ * the shadow stack pages' tokens hold the address the stack is used at, not
+ * the directmap address they are written through.
+ */
+void memguard_guard_cpu_stack(unsigned int cpu)
+{
+    void *p = page_to_virt(stack_pg[cpu]);
+    unsigned long va = PCPU_STACK_VIRT(cpu);
+
+    ASSERT(opt_fred >= 0); /* Confirm that FRED-ness has been resolved */
+    ASSERT(cpu_stack_enabled);
+
+    /* IST Shadow stacks, as memguard_guard_stack() writes them. */
+    if ( IS_ENABLED(CONFIG_XEN_SHSTK) && !opt_fred )
+    {
+        write_sss_token(p + (IST_MCE * IST_SHSTK_SIZE) - 8,
+                        va + (IST_MCE * IST_SHSTK_SIZE) - 8);
+        write_sss_token(p + (IST_NMI * IST_SHSTK_SIZE) - 8,
+                        va + (IST_NMI * IST_SHSTK_SIZE) - 8);
+        write_sss_token(p + (IST_DB  * IST_SHSTK_SIZE) - 8,
+                        va + (IST_DB  * IST_SHSTK_SIZE) - 8);
+        write_sss_token(p + (IST_DF  * IST_SHSTK_SIZE) - 8,
+                        va + (IST_DF  * IST_SHSTK_SIZE) - 8);
+    }
+    map_pages_to_xen((unsigned long)p, virt_to_mfn(p), 1, 
PAGE_HYPERVISOR_SHSTK);
+
+    /* Primary Shadow Stack.  1x 4k in stack page 5. */
+    p += PRIMARY_SHSTK_SLOT * PAGE_SIZE;
+    va += PRIMARY_SHSTK_SLOT * PAGE_SIZE;
+    if ( IS_ENABLED(CONFIG_XEN_SHSTK) && !opt_fred )
+        write_sss_token(p + PAGE_SIZE - 8, va + PAGE_SIZE - 8);
+
+    map_pages_to_xen((unsigned long)p, virt_to_mfn(p), 1, 
PAGE_HYPERVISOR_SHSTK);
+}
+
 /* Guard a xenheap stack, at @p, which is also the address it is used at. */
 void memguard_guard_stack(void *p)
 {
diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c
index 38d68167a9..e2d7b46cf1 100644
--- a/xen/arch/x86/setup.c
+++ b/xen/arch/x86/setup.c
@@ -902,8 +902,6 @@ static void __init noreturn reinit_bsp_stack(void)
 
     bsp_traps_reinit();
 
-    stack_base[0] = stack;
-
     rc = setup_cpu_root_pgt(0);
     if ( rc )
         panic("Error %d setting up PV root page table\n", rc);
@@ -2135,8 +2133,9 @@ void asmlinkage __init noreturn __start_xen(void)
 
     /*
      * 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.
+     * resolved, as its shadow stack tokens depend on whether it runs 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 )
@@ -2248,7 +2247,10 @@ void asmlinkage __init noreturn __start_xen(void)
     }
 
     /* Copy the cpu info block, and move onto the BSP stack. */
-    bsp_info = get_cpu_info_from_stack((unsigned long)bsp_stack);
+    stack_base[0] = bsp_stack;
+    bsp_info = get_cpu_info_from_stack(cpu_stack_enabled
+                                       ? PCPU_STACK_VIRT(0)
+                                       : (unsigned long)bsp_stack);
     *bsp_info = *info;
 
     asm volatile ("mov %[stk], %%rsp; jmp %c[fn]" ::
diff --git a/xen/arch/x86/smpboot.c b/xen/arch/x86/smpboot.c
index f2716e1257..2943d56a13 100644
--- a/xen/arch/x86/smpboot.c
+++ b/xen/arch/x86/smpboot.c
@@ -588,7 +588,10 @@ static int do_boot_cpu(int apicid, int cpu)
         printk("Booting processor %d/%d eip %lx\n",
                cpu, apicid, start_eip);
 
-    stack_start = stack_base[cpu] + STACK_SIZE - sizeof(struct cpu_info);
+    /* The AP starts on idle_pg_table, which maps the stack at both. */
+    stack_start = (cpu_stack_enabled ? _p(PCPU_STACK_VIRT(cpu))
+                                     : stack_base[cpu]) +
+                  STACK_SIZE - sizeof(struct cpu_info);
 
     /* This grunge runs the startup process for the targeted processor. */
 
@@ -1068,7 +1071,10 @@ void *cpu_alloc_stack(unsigned int cpu)
     if ( stack )
     {
         stack_pg[cpu] = virt_to_page(stack);
-        memguard_guard_stack(stack);
+        if ( cpu_stack_enabled )
+            memguard_guard_cpu_stack(cpu);
+        else
+            memguard_guard_stack(stack);
     }
 
     return stack;
diff --git a/xen/arch/x86/spec_ctrl.c b/xen/arch/x86/spec_ctrl.c
index ccddad842d..c306776374 100644
--- a/xen/arch/x86/spec_ctrl.c
+++ b/xen/arch/x86/spec_ctrl.c
@@ -2441,9 +2441,9 @@ void __init init_speculation_mitigations(void)
 
 #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).
+     * Per-CPU stacks move every CPU onto a stack at 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);
-- 
2.56.0




 


Rackspace

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