|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [PATCH v8 61/65] x86/mm: allocate per-CPU stacks from the domain heap
With per-CPU stacks each pCPU runs on its stack at PCPU_STACK_VIRT(),
and the page-tables of a cpu_stack vCPU map there the stack of the pCPU
it runs on and no other. But the stacks are xenheap pages, which the
sparse directmap view maps as it maps all of the xenheap: every root
page-table on the sparse view, a cpu_stack vCPU's included, still
reaches every pCPU's stack at its directmap address.
Allocate per-CPU stacks from the domain heap instead, which the sparse
view does not map, so that the per-CPU mappings are the only ones the
sparse view's page-tables have. The pages have no owner, like the
per-domain page-tables. Details:
- stack_base[cpu] is NULL in this mode: the stack has no address but
PCPU_STACK_VIRT(cpu). cpu_alloc_stack() returns the page, and a
pCPU coming back reuses stack_pg[cpu]. XPTI builds no root
page-table in this mode, as no PV guest uses XPTI then, and there
would be no address to clone.
- The cpu_info block is initialised through a transient mapping of the
stack's top page: PCPU_STACK_VIRT(cpu) need not be mapped in the
page-tables cpu_smpboot_alloc() runs on.
- memguard_guard_cpu_stack() writes the shadow stack tokens through
transient mappings too, with values for PCPU_STACK_VIRT(cpu), as
before. The full directmap maps the domain heap, so the shadow
stack pages' alias there is made read-only, as for a xenheap stack: a
shadow stack page must have no writable mapping. That uses
modify_xen_mappings(), which changes existing mappings only, in the
full directmap and wherever the sparse view maps the same address,
which for the domain heap is nowhere; map_pages_to_xen() would add
the two pages to the sparse view. A failure, possible only when a
superpage must be split and no page-table page can be had, comes
before the page in hand is changed: memguard_guard_cpu_stack() sets
a page it made read-only before back to read-write, which needs no
allocation, and returns -ENOMEM; the pages are freed and the pCPU's
bring-up fails; the BSP's is fatal, as before. Should a page stay
read-only all the same, it returns -EBUSY, and the pages are given
up, not freed.
- Stacks are never freed in this mode: an offline pCPU stays parked on
its stack, and its mappings stay in the idle domain's per-domain
area and the EFI runtime page-tables.
- The allocator scrubs the pages where they were freed as needing
it, as for any domain heap allocation; xenheap allocations, the
stacks' until now, are scrubbed only with a sparse directmap view
configured. The cpu_info memset covers what must be deterministic.
- tboot's S3 integrity MAC, which covers xenheap pages, no longer
covers the stacks; it does not cover the per-domain page-tables
either.
The full directmap still maps every stack, as it maps all memory: the
idle domain, PV domains not on the sparse view, and HVM domains without
vCPU-PT reach every stack through it, as they reach it through their
shared stack sub-area. Without per-CPU stacks the xenheap path is
unchanged.
Assisted-by: 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):
- New: v2 kept the stacks in the xenheap.
---
docs/misc/xen-command-line.pandoc | 4 +-
xen/arch/x86/include/asm/mm.h | 2 +-
xen/arch/x86/include/asm/smp.h | 2 +-
xen/arch/x86/mm.c | 67 ++++++++++++++++++-------
xen/arch/x86/setup.c | 8 ++-
xen/arch/x86/smpboot.c | 81 +++++++++++++++++++++++++------
6 files changed, 123 insertions(+), 41 deletions(-)
diff --git a/docs/misc/xen-command-line.pandoc
b/docs/misc/xen-command-line.pandoc
index 1941d790dd..bf9f7f2130 100644
--- a/docs/misc/xen-command-line.pandoc
+++ b/docs/misc/xen-command-line.pandoc
@@ -236,7 +236,9 @@ are meant to be used for debugging purposes only.**
* `cpu-stack` runs every CPU on a stack at an address of its own, and
gives each vCPU of the selected domains page-tables which map, at those
addresses, the stack of the CPU it runs on and no other; the page-tables
- of all other contexts map every CPU's stack there. `cpu-stack` relies
+ of all other contexts map every CPU's stack there. The stacks come from
+ the domain heap, which the sparse directmap view does not map (see
+ `directmap`); the full directmap maps every stack. `cpu-stack` relies
on `vcpu-pt`: enabling the former enables the latter, and disabling the
latter disables the former, whichever comes last. It is disabled, with
a boot-log warning, while XPTI is enabled for the hardware domain or for
diff --git a/xen/arch/x86/include/asm/mm.h b/xen/arch/x86/include/asm/mm.h
index d5bb38f7d2..9f25781369 100644
--- a/xen/arch/x86/include/asm/mm.h
+++ b/xen/arch/x86/include/asm/mm.h
@@ -532,7 +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);
+int 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 d8195923f7..ec243c229a 100644
--- a/xen/arch/x86/include/asm/smp.h
+++ b/xen/arch/x86/include/asm/smp.h
@@ -85,7 +85,7 @@ extern bool cpu_stack_enabled;
#define cpu_stack_enabled false
#endif
-void *cpu_alloc_stack(unsigned int cpu);
+struct page_info *cpu_alloc_stack(unsigned int cpu);
/* Give the idle domain the per-domain area mapping every CPU's stack. */
void cpu_stack_init(void);
diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c
index ff119c6134..2e585ae120 100644
--- a/xen/arch/x86/mm.c
+++ b/xen/arch/x86/mm.c
@@ -7079,39 +7079,72 @@ static void write_sss_token(unsigned long *ptr,
unsigned long 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.
+ * Guard @cpu's per-CPU stack, used at PCPU_STACK_VIRT(@cpu): stack_pg[@cpu],
+ * from the domain heap, with no address of its own. The shadow stack pages'
+ * tokens hold the address the stack is used at. Returns 0; -ENOMEM when the
+ * shadow stack pages could not be made read-only in the full directmap, and
+ * are writable there again; or -EBUSY when one of them is read-only still.
*/
-void memguard_guard_cpu_stack(unsigned int cpu)
+int memguard_guard_cpu_stack(unsigned int cpu)
{
- void *p = page_to_virt(stack_pg[cpu]);
unsigned long va = PCPU_STACK_VIRT(cpu);
+ mfn_t mfn = page_to_mfn(stack_pg[cpu]);
+ unsigned int i;
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,
+ void *map = map_domain_page(mfn);
+
+ write_sss_token(map + (IST_MCE * IST_SHSTK_SIZE) - 8,
va + (IST_MCE * IST_SHSTK_SIZE) - 8);
- write_sss_token(p + (IST_NMI * IST_SHSTK_SIZE) - 8,
+ write_sss_token(map + (IST_NMI * IST_SHSTK_SIZE) - 8,
va + (IST_NMI * IST_SHSTK_SIZE) - 8);
- write_sss_token(p + (IST_DB * IST_SHSTK_SIZE) - 8,
+ write_sss_token(map + (IST_DB * IST_SHSTK_SIZE) - 8,
va + (IST_DB * IST_SHSTK_SIZE) - 8);
- write_sss_token(p + (IST_DF * IST_SHSTK_SIZE) - 8,
+ write_sss_token(map + (IST_DF * IST_SHSTK_SIZE) - 8,
va + (IST_DF * IST_SHSTK_SIZE) - 8);
+ unmap_domain_page(map);
+
+ map = map_domain_page(mfn_add(mfn, PRIMARY_SHSTK_SLOT));
+ write_sss_token(map + PAGE_SIZE - 8,
+ va + (PRIMARY_SHSTK_SLOT + 1) * PAGE_SIZE - 8);
+ unmap_domain_page(map);
}
- 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);
+ /*
+ * A shadow stack page must have no writable mapping, and the full
+ * directmap maps the domain heap: make the shadow stack pages read-only
+ * there, whatever arch_mfns_in_directmap() says (false for every page once
+ * a sparse view is configured). map_pages_to_xen() would add them to the
+ * sparse view. modify_xen_mappings() fails only before changing the page
+ * in hand, for want of a page-table page to split a superpage; the pages
+ * done before are 4k-mapped by then: setting them back allocates nothing.
+ */
+ for ( i = 0; i < (1U << STACK_ORDER); i++ )
+ {
+ unsigned long dva = (unsigned long)mfn_to_virt(mfn_x(mfn) + i);
- map_pages_to_xen((unsigned long)p, virt_to_mfn(p), 1,
PAGE_HYPERVISOR_SHSTK);
+ if ( !is_shstk_slot(i) ||
+ !modify_xen_mappings(dva, dva + PAGE_SIZE,
+ PAGE_HYPERVISOR_SHSTK) )
+ continue;
+
+ while ( i-- )
+ {
+ dva = (unsigned long)mfn_to_virt(mfn_x(mfn) + i);
+ if ( is_shstk_slot(i) &&
+ modify_xen_mappings(dva, dva + PAGE_SIZE,
+ PAGE_HYPERVISOR_RW) )
+ return -EBUSY;
+ }
+
+ return -ENOMEM;
+ }
+
+ return 0;
}
/* Guard a xenheap stack, at @p, which is also the address it is used at. */
diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c
index f760498c89..3934f9a8e0 100644
--- a/xen/arch/x86/setup.c
+++ b/xen/arch/x86/setup.c
@@ -1151,7 +1151,6 @@ void asmlinkage __init noreturn __start_xen(void)
{
const char *memmap_type = NULL;
char *kextra;
- void *bsp_stack;
struct cpu_info *info = get_cpu_info(), *bsp_info;
unsigned int initrdidx, num_parked = 0;
struct boot_info *bi;
@@ -2137,8 +2136,7 @@ void asmlinkage __init noreturn __start_xen(void)
* 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 )
+ if ( !cpu_alloc_stack(0) ) /* Needs to know IDT vs FRED */
panic("No memory for BSP stack\n");
if ( cpu_stack_enabled && cpu_stack_map(0) )
panic("No memory for the BSP's stack mappings\n");
@@ -2247,10 +2245,10 @@ void asmlinkage __init noreturn __start_xen(void)
}
/* Copy the cpu info block, and move onto the BSP stack. */
- stack_base[0] = bsp_stack;
+ stack_base[0] = cpu_stack_enabled ? NULL : page_to_virt(stack_pg[0]);
bsp_info = get_cpu_info_from_stack(cpu_stack_enabled
? PCPU_STACK_VIRT(0)
- : (unsigned long)bsp_stack);
+ : (unsigned long)stack_base[0]);
*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 bff09931ab..122b3abf7f 100644
--- a/xen/arch/x86/smpboot.c
+++ b/xen/arch/x86/smpboot.c
@@ -93,6 +93,8 @@ void *stack_base[NR_CPUS];
* The page underlying each CPU's stack. stack_base[] names the same stack,
* except that stack_base[0] holds the BSP's image stack (cpu0_stack) until
* the BSP moves to its heap stack, which stack_pg[0] names from allocation.
+ * With per-CPU stacks, which come from the domain heap and are used at
+ * PCPU_STACK_VIRT(), stack_base[] is NULL once the heap stacks are in use.
*/
struct page_info *stack_pg[NR_CPUS];
@@ -859,7 +861,12 @@ int setup_cpu_root_pgt(unsigned int cpu)
unsigned int off;
int rc;
- if ( !opt_xpti_hwdom && !opt_xpti_domu )
+ /*
+ * With per-CPU stacks no PV guest uses XPTI (cpu-stack is off while XPTI
+ * is enabled for one), and the stack has no directmap address in
+ * stack_base[] to clone: no XPTI root page-table.
+ */
+ if ( (!opt_xpti_hwdom && !opt_xpti_domu) || cpu_stack_enabled )
return 0;
rpt = alloc_xenheap_page();
@@ -1057,32 +1064,57 @@ static void cpu_smpboot_free(unsigned int cpu, bool
remove)
}
}
-void *cpu_alloc_stack(unsigned int cpu)
+struct page_info *cpu_alloc_stack(unsigned int cpu)
{
nodeid_t node = cpu_to_node(cpu);
unsigned int memflags = 0;
- void *stack;
+ struct page_info *pg;
+ int rc;
if ( node != NUMA_NO_NODE )
memflags = MEMF_node(node);
- stack = alloc_xenheap_pages(STACK_ORDER, memflags);
+ if ( cpu_stack_enabled )
+ {
+ /*
+ * A per-CPU stack is used at PCPU_STACK_VIRT(cpu): take it from the
+ * domain heap, which the sparse directmap view does not map, unlike
+ * the xenheap.
+ */
+ pg = alloc_domheap_pages(NULL, STACK_ORDER, memflags);
+ if ( !pg )
+ return NULL;
- if ( stack )
+ stack_pg[cpu] = pg;
+ rc = memguard_guard_cpu_stack(cpu);
+ if ( rc )
+ {
+ stack_pg[cpu] = NULL;
+ /* -EBUSY: a shadow stack page is read-only still: give it up. */
+ if ( rc != -EBUSY )
+ free_domheap_pages(pg, STACK_ORDER);
+ return NULL;
+ }
+ }
+ else
{
- stack_pg[cpu] = virt_to_page(stack);
- if ( cpu_stack_enabled )
- memguard_guard_cpu_stack(cpu);
- else
- memguard_guard_stack(stack);
+ void *stack = alloc_xenheap_pages(STACK_ORDER, memflags);
+
+ if ( !stack )
+ return NULL;
+
+ pg = virt_to_page(stack);
+ stack_pg[cpu] = pg;
+ memguard_guard_stack(stack);
}
- return stack;
+ return pg;
}
static int cpu_smpboot_alloc(unsigned int cpu)
{
struct cpu_info *info;
+ void *stack_top = NULL;
unsigned int memflags = 0;
nodeid_t node = cpu_to_node(cpu);
seg_desc_t *gdt;
@@ -1091,14 +1123,29 @@ static int cpu_smpboot_alloc(unsigned int cpu)
if ( node != NUMA_NO_NODE )
memflags = MEMF_node(node);
- if ( stack_base[cpu] == NULL &&
- (stack_base[cpu] = cpu_alloc_stack(cpu)) == NULL )
+ if ( stack_pg[cpu] == NULL && cpu_alloc_stack(cpu) == NULL )
+ goto out;
+
+ if ( cpu_stack_enabled )
+ {
+ if ( cpu_stack_map(cpu) )
goto out;
- if ( cpu_stack_enabled && cpu_stack_map(cpu) )
- goto out;
+ /*
+ * Initialise the cpu_info block through a transient mapping of its
+ * page: PCPU_STACK_VIRT(cpu) need not be mapped in the page-tables
+ * this runs on.
+ */
+ stack_top = map_domain_page(mfn_add(page_to_mfn(stack_pg[cpu]),
+ (1U << STACK_ORDER) - 1));
+ info = (struct cpu_info *)(stack_top + PAGE_SIZE) - 1;
+ }
+ else
+ {
+ stack_base[cpu] = page_to_virt(stack_pg[cpu]);
+ info = get_cpu_info_from_stack((unsigned long)stack_base[cpu]);
+ }
- info = get_cpu_info_from_stack((unsigned long)stack_base[cpu]);
memset(info, 0, sizeof(*info));
info->processor_id = cpu;
info->per_cpu_offset = __per_cpu_offset[cpu];
@@ -1110,6 +1157,8 @@ static int cpu_smpboot_alloc(unsigned int cpu)
per_cpu(curr_vcpu, cpu) = idle_vcpu[cpu];
per_cpu(pgtable_vcpu, cpu) = idle_vcpu[cpu];
+ unmap_domain_page(stack_top);
+
gdt = per_cpu(gdt, cpu) ?: alloc_xenheap_pages(0, memflags);
if ( gdt == NULL )
goto out;
--
2.56.0
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |