Skip to contentSkip to conversation

Alternative Meltdown mitigation

PATCH In review Juergen Gross 3 participants 23 messages Feb 9, 2018 – Feb 13, 2018 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

v3 cover Alternative Meltdown mitigation

This patch series is meant to be used instead of the "XPTI-light" Meltdown mitigation of Jan. It is using a different approach by using a shadow of the guest's L4 page table and keeping those in a cache in order to avoid the need to create the shadow multiple times. I'll name my approach "XPTI" in the following.

The shadow L4 page table used for running in guest mode maps only the guest (of course) and those parts of the hypervisor memory which are needed for entering and leaving the hypervisor: IDT, GDT, TSS, stacks and early interrupt handling code.

To avoid a guest being capable to read other domain's data via the interrupt stacks of other cpus a guest subject to XPTI isn't using the normal stacks for early interrupt handling, but per-vcpu stacks. This allows to map the per-vcpu stacks only when running the guest.

For each guest L4 page table there is exactly one shadow L4 page table. This approach avoids the need to do complicated synchronizations between L4 page tables, as the guest already needs to synchronize multiple cpus in case it is using the same address space on multiple processors concurrently.

Without any further measures it will still be possible for e.g. a guest's user program to read stack data of another vcpu of the same domain, but this can be easily avoided by a little PV-ABI modification introducing per-cpu user address spaces. I'm planning to add that when Linux kernel is learning to use per-cpu address spaces.

This series is available via github:

https://github.com/jgross1/xen.git xpti

Dario wants to do some performance tests for this series to compare performance with Jan's series with all optimizations posted.

Patch 1 is just (IMHO) a bugfix for guest stack dumping. Patches 2 - 3 revert Jan's XPTI-light patches.
Patch 4 modifies the trap handling to use %r12 for addressing the
guest's saved registers instead of using %rsp. This is a prerequisite for being able to switch the stacks in early trap handling.
Patch 5 adds the xpti command line parameter and some basic
infrastructure for the XPTI framework.
Patches 6 - 8 modify some current infrastructure to support the
following XPTI functionality.
Patch 9 adds syscall stubs for XPTI as the current stubs can't be used. Patch 10 allocates the per-vcpu stacks and initializes them. Patch 11 modifies interrupt handling to support stack switching in
case of XPTI.
Patch 12 adds activation of the per-vcpu stacks for domains subject to
XPTI.
Patch 13 adds the L4 page table shadowing including the L4 shadow
cache.
Patch 14 does some more modifications needed for keeping the L4 shadows
up to date.
Patch 15 adds populating the L4 shadow tables with the guest's L4
entries.
Patch 16 adds switching between hypervisor and guest L4 page tables
when entering/leaving the hypervisor.
Patch 17 removes all the hypervisor mappings not needed in the shadow
L4 page table.

Juergen Gross (17):
x86: don't use hypervisor stack size for dumping guest stacks
x86: do a revert of e871e80c38547d9faefc6604532ba3e985e65873
x86: revert 5784de3e2067ed73efc2fe42e62831e8ae7f46c4
x86: don't access saved user regs via rsp in trap handlers
x86: add a xpti command line parameter
x86: allow per-domain mappings without NX bit or with specific mfn xen/x86: split _set_tssldt_desc() into ldt and tss specific functions
x86: add support for spectre mitigation with local thunk
x86: create syscall stub for per-domain mapping
x86: allocate per-vcpu stacks for interrupt entries
x86: modify interrupt handlers to support stack switching
x86: activate per-vcpu stacks in case of xpti
x86: allocate hypervisor L4 page table for XPTI
xen: add domain pointer to fill_ro_mpt() and zap_ro_mpt() functions
x86: fill XPTI shadow pages and keep them in sync with guest L4
x86: do page table switching when entering/leaving hypervisor
x86: hide most hypervisor mappings in XPTI shadow page tables

 docs/misc/xen-command-line.markdown      |   16 +-
 xen/arch/x86/cpu/common.c                |    4 +-
 xen/arch/x86/domain.c                    |  113 +++-
 xen/arch/x86/domctl.c                    |    4 +
 xen/arch/x86/indirect-thunk.S            |   23 +-
 xen/arch/x86/mm.c                        |   92 +--
 xen/arch/x86/mm/shadow/multi.c           |    9 +-
 xen/arch/x86/pv/Makefile                 |    2 +
 xen/arch/x86/pv/dom0_build.c             |    6 +
 xen/arch/x86/pv/domain.c                 |    5 +
 xen/arch/x86/pv/xpti-stub.S              |   61 ++
 xen/arch/x86/pv/xpti.c                   | 1028 ++++++++++++++++++++++++++++++
 xen/arch/x86/smpboot.c                   |  211 ------
 xen/arch/x86/traps.c                     |   35 +-
 xen/arch/x86/x86_64/asm-offsets.c        |    6 +-
 xen/arch/x86/x86_64/compat/entry.S       |   27 +-
 xen/arch/x86/x86_64/entry.S              |  315 +++------
 xen/arch/x86/x86_64/traps.c              |    3 +-
 xen/common/wait.c                        |    8 +-
 xen/include/asm-x86/asm_defns.h          |   68 +-
 xen/include/asm-x86/config.h             |   13 +-
 xen/include/asm-x86/current.h            |   86 ++-
 xen/include/asm-x86/desc.h               |   14 +-
 xen/include/asm-x86/domain.h             |    8 +
 xen/include/asm-x86/indirect_thunk_asm.h |    8 +-
 xen/include/asm-x86/ldt.h                |    2 +-
 xen/include/asm-x86/mm.h                 |    4 +-
 xen/include/asm-x86/nops.h               |    2 +-
 xen/include/asm-x86/processor.h          |   13 +-
 xen/include/asm-x86/pv/mm.h              |   35 +
 xen/include/asm-x86/regs.h               |    2 +
 xen/include/asm-x86/spec_ctrl_asm.h      |   13 +-
 xen/include/asm-x86/system.h             |    5 +
 xen/include/asm-x86/x86_64/page.h        |    5 +-
 34 files changed, 1632 insertions(+), 614 deletions(-)
 create mode 100644 xen/arch/x86/pv/xpti-stub.S
 create mode 100644 xen/arch/x86/pv/xpti.c
Signature · 7 lines
2.13.6


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

PATCH v3 1/17 x86: don't use hypervisor stack size for dumping guest stacks

show_guest_stack() and compat_show_guest_stack() stop dumping the stack of the guest whenever its virtual address reaches the same alignment which is used for the hypervisor stacks.

Remove this arbitrary limit and try to dump a fixed number of lines instead.

---
 xen/arch/x86/traps.c | 26 +++++++++++---------------
 1 file changed, 11 insertions(+), 15 deletions(-)
xen/arch/x86/traps.c +11 -15
index 2e022b09b8..13a852ca4e 100644
@@ -219,7 +219,8 @@ static void compat_show_guest_stack(struct vcpu *v,
                                     const struct cpu_user_regs *regs,
                                     int debug_stack_lines)
 {
-    unsigned int i, *stack, addr, mask = STACK_SIZE;
+    unsigned int i, *stack, addr;
+    unsigned long last_addr = -1L;
 
     stack = (unsigned int *)(unsigned long)regs->esp;
     printk("Guest stack trace from esp=%08lx:\n ", (unsigned long)stack);
@@ -248,13 +249,13 @@ static void compat_show_guest_stack(struct vcpu *v,
                 printk("Inaccessible guest memory.\n");
                 return;
             }
-            mask = PAGE_SIZE;
+            last_addr = round_pgup((unsigned long)stack);
         }
     }
 
     for ( i = 0; i < debug_stack_lines * 8; i++ )
     {
-        if ( (((long)stack - 1) ^ ((long)(stack + 1) - 1)) & mask )
+        if ( (unsigned long)stack >= last_addr )
             break;
         if ( __get_user(addr, stack) )
         {
@@ -269,11 +270,9 @@ static void compat_show_guest_stack(struct vcpu *v,
         printk(" %08x", addr);
         stack++;
     }
-    if ( mask == PAGE_SIZE )
-    {
-        BUILD_BUG_ON(PAGE_SIZE == STACK_SIZE);
+    if ( last_addr != -1L )
         unmap_domain_page(stack);
-    }
+
     if ( i == 0 )
         printk("Stack empty.");
     printk("\n");
@@ -282,8 +281,7 @@ static void compat_show_guest_stack(struct vcpu *v,
 static void show_guest_stack(struct vcpu *v, const struct cpu_user_regs *regs)
 {
     int i;
-    unsigned long *stack, addr;
-    unsigned long mask = STACK_SIZE;
+    unsigned long *stack, addr, last_addr = -1L;
 
     /* Avoid HVM as we don't know what the stack looks like. */
     if ( is_hvm_vcpu(v) )
@@ -318,13 +316,13 @@ static void show_guest_stack(struct vcpu *v, const struct cpu_user_regs *regs)
                 printk("Inaccessible guest memory.\n");
                 return;
             }
-            mask = PAGE_SIZE;
+            last_addr = round_pgup((unsigned long)stack);
         }
     }
 
     for ( i = 0; i < (debug_stack_lines*stack_words_per_line); i++ )
     {
-        if ( (((long)stack - 1) ^ ((long)(stack + 1) - 1)) & mask )
+        if ( (unsigned long)stack >= last_addr )
             break;
         if ( __get_user(addr, stack) )
         {
@@ -339,11 +337,9 @@ static void show_guest_stack(struct vcpu *v, const struct cpu_user_regs *regs)
         printk(" %p", _p(addr));
         stack++;
     }
-    if ( mask == PAGE_SIZE )
-    {
-        BUILD_BUG_ON(PAGE_SIZE == STACK_SIZE);
+    if ( last_addr != -1L )
         unmap_domain_page(stack);
-    }
+
     if ( i == 0 )
         printk("Stack empty.");
     printk("\n");
Signature · 7 lines
2.13.6


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

PATCH v3 2/17 x86: do a revert of e871e80c38547d9faefc6604532ba3e985e65873

In reply to Juergen Gross

Revert "x86: allow Meltdown band-aid to be disabled" in order to prepare for a final Meltdown mitigation.

---
 docs/misc/xen-command-line.markdown | 12 ------------
 xen/arch/x86/domain.c               |  7 ++-----
 xen/arch/x86/mm.c                   | 12 +-----------
 xen/arch/x86/smpboot.c              | 17 +++--------------
 xen/arch/x86/x86_64/entry.S         |  2 --
 5 files changed, 6 insertions(+), 44 deletions(-)
docs/misc/xen-command-line.markdown +0 -12
index 79feba6bcd..6df39dae0b 100644
@@ -1926,18 +1926,6 @@ In the case that x2apic is in use, this option switches between physical and
 clustered mode.  The default, given no hint from the **FADT**, is cluster
 mode.
 
-### xpti
-> `= <boolean>`
-
-> Default: `false` on AMD hardware
-> Default: `true` everywhere else
-
-Override default selection of whether to isolate 64-bit PV guest page
-tables.
-
-** WARNING: Not yet a complete isolation implementation, but better than
-nothing. **
-
 ### xsave
 > `= <boolean>`
 
xen/arch/x86/domain.c +2 -5
index f93327b0a2..752e0fafee 100644
@@ -1518,13 +1518,10 @@ void paravirt_ctxt_switch_from(struct vcpu *v)
 
 void paravirt_ctxt_switch_to(struct vcpu *v)
 {
-    root_pgentry_t *root_pgt = this_cpu(root_pgt);
     unsigned long cr4;
 
-    if ( root_pgt )
-        root_pgt[root_table_offset(PERDOMAIN_VIRT_START)] =
-            l4e_from_page(v->domain->arch.perdomain_l3_pg,
-                          __PAGE_HYPERVISOR_RW);
+    this_cpu(root_pgt)[root_table_offset(PERDOMAIN_VIRT_START)] =
+        l4e_from_page(v->domain->arch.perdomain_l3_pg, __PAGE_HYPERVISOR_RW);
 
     cr4 = pv_guest_cr4_to_real_cr4(v);
     if ( unlikely(cr4 != read_cr4()) )
xen/arch/x86/mm.c +1 -11
index 35f204369b..fa0da7b0ff 100644
@@ -3706,17 +3706,7 @@ long do_mmu_update(
                         break;
                     rc = mod_l4_entry(va, l4e_from_intpte(req.val), mfn,
                                       cmd == MMU_PT_UPDATE_PRESERVE_AD, v);
-                    /*
-                     * No need to sync if all uses of the page can be accounted
-                     * to the page lock we hold, its pinned status, and uses on
-                     * this (v)CPU.
-                     */
-                    if ( !rc && this_cpu(root_pgt) &&
-                         ((page->u.inuse.type_info & PGT_count_mask) >
-                          (1 + !!(page->u.inuse.type_info & PGT_pinned) +
-                           (pagetable_get_pfn(curr->arch.guest_table) == mfn) +
-                           (pagetable_get_pfn(curr->arch.guest_table_user) ==
-                            mfn))) )
+                    if ( !rc )
                         sync_guest = true;
                     break;
 
xen/arch/x86/smpboot.c +3 -14
index 2ebef03027..49978b3697 100644
@@ -331,7 +331,7 @@ void start_secondary(void *unused)
     spin_debug_disable();
 
     get_cpu_info()->xen_cr3 = 0;
-    get_cpu_info()->pv_cr3 = this_cpu(root_pgt) ? __pa(this_cpu(root_pgt)) : 0;
+    get_cpu_info()->pv_cr3 = __pa(this_cpu(root_pgt));
 
     load_system_tables();
 
@@ -740,20 +740,14 @@ static int clone_mapping(const void *ptr, root_pgentry_t *rpt)
     return 0;
 }
 
-static __read_mostly int8_t opt_xpti = -1;
-boolean_param("xpti", opt_xpti);
 DEFINE_PER_CPU(root_pgentry_t *, root_pgt);
 
 static int setup_cpu_root_pgt(unsigned int cpu)
 {
-    root_pgentry_t *rpt;
+    root_pgentry_t *rpt = alloc_xen_pagetable();
     unsigned int off;
     int rc;
 
-    if ( !opt_xpti )
-        return 0;
-
-    rpt = alloc_xen_pagetable();
     if ( !rpt )
         return -ENOMEM;
 
@@ -1002,14 +996,10 @@ void __init smp_prepare_cpus(unsigned int max_cpus)
 
     stack_base[0] = stack_start;
 
-    if ( opt_xpti < 0 )
-        opt_xpti = boot_cpu_data.x86_vendor != X86_VENDOR_AMD;
-
     rc = setup_cpu_root_pgt(0);
     if ( rc )
         panic("Error %d setting up PV root page table\n", rc);
-    if ( per_cpu(root_pgt, 0) )
-        get_cpu_info()->pv_cr3 = __pa(per_cpu(root_pgt, 0));
+    get_cpu_info()->pv_cr3 = __pa(per_cpu(root_pgt, 0));
 
     set_nr_sockets();
 
@@ -1081,7 +1071,6 @@ void __init smp_prepare_boot_cpu(void)
 #endif
 
     get_cpu_info()->xen_cr3 = 0;
-    get_cpu_info()->pv_cr3 = 0;
 }
 
 static void
xen/arch/x86/x86_64/entry.S +0 -2
index 58f652d010..52f64cceda 100644
@@ -50,7 +50,6 @@ restore_all_guest:
         movabs $DIRECTMAP_VIRT_START, %rcx
         mov   %rdi, %rax
         and   %rsi, %rdi
-        jz    .Lrag_keep_cr3
         and   %r9, %rsi
         add   %rcx, %rdi
         add   %rcx, %rsi
@@ -67,7 +66,6 @@ restore_all_guest:
         rep movsq
         mov   %r9, STACK_CPUINFO_FIELD(xen_cr3)(%rdx)
         write_cr3 rax, rdi, rsi
-.Lrag_keep_cr3:
 
         /* Restore stashed SPEC_CTRL value. */
         mov   %r15d, %eax
Signature · 7 lines
2.13.6


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

Re: [PATCH v3 02/17] x86: do a revert of e871e80c38547d9faefc6604532ba3e985e65873

Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 09.02.18 at 15:01, <[email protected]> wrote:

Revert "x86: allow Meltdown band-aid to be disabled" in order to prepare for a final Meltdown mitigation.

This no also reverts a22320e32dca0918ed23799583f470afe4c24330 afaict. I think that it would be better to revert the whole thing in a single patch anyway (i.e. also fold patch 3 into here).

Jan

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

#4 lore raw

PATCH v3 3/17 x86: revert 5784de3e2067ed73efc2fe42e62831e8ae7f46c4

In reply to Juergen Gross

Revert patch "x86: Meltdown band-aid against malicious 64-bit PV guests" in order to prepare for a final Meltdown mitigation.

---
 xen/arch/x86/domain.c              |   5 -
 xen/arch/x86/mm.c                  |  21 ----
 xen/arch/x86/smpboot.c             | 200 -------------------------------------
 xen/arch/x86/x86_64/asm-offsets.c  |   2 -
 xen/arch/x86/x86_64/compat/entry.S |  12 +--
 xen/arch/x86/x86_64/entry.S        | 142 +-------------------------
 xen/include/asm-x86/asm_defns.h    |   9 --
 xen/include/asm-x86/current.h      |  12 ---
 xen/include/asm-x86/processor.h    |   1 -
 xen/include/asm-x86/x86_64/page.h  |   5 +-
 10 files changed, 8 insertions(+), 401 deletions(-)
xen/arch/x86/domain.c +0 -5
index 752e0fafee..6dd47bb2bb 100644
@@ -1520,9 +1520,6 @@ void paravirt_ctxt_switch_to(struct vcpu *v)
 {
     unsigned long cr4;
 
-    this_cpu(root_pgt)[root_table_offset(PERDOMAIN_VIRT_START)] =
-        l4e_from_page(v->domain->arch.perdomain_l3_pg, __PAGE_HYPERVISOR_RW);
-
     cr4 = pv_guest_cr4_to_real_cr4(v);
     if ( unlikely(cr4 != read_cr4()) )
         write_cr4(cr4);
@@ -1694,8 +1691,6 @@ void context_switch(struct vcpu *prev, struct vcpu *next)
 
     ASSERT(local_irq_is_enabled());
 
-    get_cpu_info()->xen_cr3 = 0;
-
     if ( unlikely(dirty_cpu != cpu) && dirty_cpu != VCPU_CPU_CLEAN )
     {
         /* Remote CPU calls __sync_local_execstate() from flush IPI handler. */
xen/arch/x86/mm.c +0 -21
index fa0da7b0ff..e795239829 100644
@@ -3542,7 +3542,6 @@ long do_mmu_update(
     struct vcpu *curr = current, *v = curr;
     struct domain *d = v->domain, *pt_owner = d, *pg_owner;
     mfn_t map_mfn = INVALID_MFN;
-    bool sync_guest = false;
     uint32_t xsm_needed = 0;
     uint32_t xsm_checked = 0;
     int rc = put_old_guest_table(curr);
@@ -3706,8 +3705,6 @@ long do_mmu_update(
                         break;
                     rc = mod_l4_entry(va, l4e_from_intpte(req.val), mfn,
                                       cmd == MMU_PT_UPDATE_PRESERVE_AD, v);
-                    if ( !rc )
-                        sync_guest = true;
                     break;
 
                 case PGT_writable_page:
@@ -3812,24 +3809,6 @@ long do_mmu_update(
     if ( va )
         unmap_domain_page(va);
 
-    if ( sync_guest )
-    {
-        /*
-         * Force other vCPU-s of the affected guest to pick up L4 entry
-         * changes (if any). Issue a flush IPI with empty operation mask to
-         * facilitate this (including ourselves waiting for the IPI to
-         * actually have arrived). Utilize the fact that FLUSH_VA_VALID is
-         * meaningless without FLUSH_CACHE, but will allow to pass the no-op
-         * check in flush_area_mask().
-         */
-        unsigned int cpu = smp_processor_id();
-        cpumask_t *mask = per_cpu(scratch_cpumask, cpu);
-
-        cpumask_andnot(mask, pt_owner->dirty_cpumask, cpumask_of(cpu));
-        if ( !cpumask_empty(mask) )
-            flush_area_mask(mask, ZERO_BLOCK_PTR, FLUSH_VA_VALID);
-    }
-
     perfc_add(num_page_updates, i);
 
  out:
xen/arch/x86/smpboot.c +0 -200

242 lines not on this page · raw message

xen/arch/x86/x86_64/asm-offsets.c +0 -2
index 51be528f89..cc7753c0a9 100644
@@ -138,8 +138,6 @@ void __dummy__(void)
     OFFSET(CPUINFO_processor_id, struct cpu_info, processor_id);
     OFFSET(CPUINFO_current_vcpu, struct cpu_info, current_vcpu);
     OFFSET(CPUINFO_cr4, struct cpu_info, cr4);
-    OFFSET(CPUINFO_xen_cr3, struct cpu_info, xen_cr3);
-    OFFSET(CPUINFO_pv_cr3, struct cpu_info, pv_cr3);
     OFFSET(CPUINFO_shadow_spec_ctrl, struct cpu_info, shadow_spec_ctrl);
     OFFSET(CPUINFO_use_shadow_spec_ctrl, struct cpu_info, use_shadow_spec_ctrl);
     OFFSET(CPUINFO_bti_ist_info, struct cpu_info, bti_ist_info);
xen/arch/x86/x86_64/compat/entry.S +1 -11
index 707c74621b..8fac5d304d 100644
@@ -214,17 +214,7 @@ ENTRY(cstar_enter)
         SPEC_CTRL_ENTRY_FROM_PV /* Req: %rsp=regs/cpuinfo, Clob: acd */
         /* WARNING! `ret`, `call *`, `jmp *` not safe before this point. */
 
-        GET_STACK_END(bx)
-        mov   STACK_CPUINFO_FIELD(xen_cr3)(%rbx), %rcx
-        neg   %rcx
-        jz    .Lcstar_cr3_okay
-        mov   %rcx, STACK_CPUINFO_FIELD(xen_cr3)(%rbx)
-        neg   %rcx
-        write_cr3 rcx, rdi, rsi
-        movq  $0, STACK_CPUINFO_FIELD(xen_cr3)(%rbx)
-.Lcstar_cr3_okay:
-
-        movq  STACK_CPUINFO_FIELD(current_vcpu)(%rbx), %rbx
+        GET_CURRENT(bx)
         movq  VCPU_domain(%rbx),%rcx
         cmpb  $0,DOMAIN_is_32bit_pv(%rcx)
         je    switch_to_kernel
xen/arch/x86/x86_64/entry.S +5 -137

227 lines not on this page · raw message

xen/include/asm-x86/asm_defns.h +0 -9
index aee14ba007..2a79e8a7f4 100644
@@ -205,15 +205,6 @@ void ret_from_intr(void);
 #define ASM_STAC ASM_AC(STAC)
 #define ASM_CLAC ASM_AC(CLAC)
 
-.macro write_cr3 val:req, tmp1:req, tmp2:req
-        mov   %cr4, %\tmp1
-        mov   %\tmp1, %\tmp2
-        and   $~X86_CR4_PGE, %\tmp1
-        mov   %\tmp1, %cr4
-        mov   %\val, %cr3
-        mov   %\tmp2, %cr4
-.endm
-
 #define CR4_PV32_RESTORE                                           \
         667: ASM_NOP5;                                             \
         .pushsection .altinstr_replacement, "ax";                  \
xen/include/asm-x86/current.h +0 -12
index 4678a0fcf5..1087239357 100644
@@ -41,18 +41,6 @@ struct cpu_info {
     struct vcpu *current_vcpu;
     unsigned long per_cpu_offset;
     unsigned long cr4;
-    /*
-     * Of the two following fields the latter is being set to the CR3 value
-     * to be used on the given pCPU for loading whenever 64-bit PV guest
-     * context is being entered. The value never changes once set.
-     * The former is the value to restore when re-entering Xen, if any. IOW
-     * its value being zero means there's nothing to restore. However, its
-     * value can also be negative, indicating to the exit-to-Xen code that
-     * restoring is not necessary, but allowing any nested entry code paths
-     * to still know the value to put back into CR3.
-     */
-    unsigned long xen_cr3;
-    unsigned long pv_cr3;
 
     /* See asm-x86/spec_ctrl_asm.h for usage. */
     unsigned int shadow_spec_ctrl;
xen/include/asm-x86/processor.h +0 -1
index 9c70a98aef..625f6e9f69 100644
@@ -437,7 +437,6 @@ extern idt_entry_t idt_table[];
 extern idt_entry_t *idt_tables[];
 
 DECLARE_PER_CPU(struct tss_struct, init_tss);
-DECLARE_PER_CPU(root_pgentry_t *, root_pgt);
 
 extern void init_int80_direct_trap(struct vcpu *v);
 
xen/include/asm-x86/x86_64/page.h +2 -3
index 05a0334893..6fb7cd5553 100644
@@ -24,8 +24,8 @@
 /* These are architectural limits. Current CPUs support only 40-bit phys. */
 #define PADDR_BITS              52
 #define VADDR_BITS              48
-#define PADDR_MASK              ((_AC(1,UL) << PADDR_BITS) - 1)
-#define VADDR_MASK              ((_AC(1,UL) << VADDR_BITS) - 1)
+#define PADDR_MASK              ((1UL << PADDR_BITS)-1)
+#define VADDR_MASK              ((1UL << VADDR_BITS)-1)
 
 #define VADDR_TOP_BIT           (1UL << (VADDR_BITS - 1))
 #define CANONICAL_MASK          (~0UL & ~VADDR_MASK)
@@ -107,7 +107,6 @@ typedef l4_pgentry_t root_pgentry_t;
       : (((_s) < ROOT_PAGETABLE_FIRST_XEN_SLOT) ||  \
          ((_s) > ROOT_PAGETABLE_LAST_XEN_SLOT)))
 
-#define root_table_offset         l4_table_offset
 #define root_get_pfn              l4e_get_pfn
 #define root_get_flags            l4e_get_flags
 #define root_get_intpte           l4e_get_intpte
Signature · 7 lines
2.13.6


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

PATCH v3 4/17 x86: don't access saved user regs via rsp in trap handlers

In reply to Juergen Gross

In order to support switching stacks when entering the hypervisor for support of page table isolation, don't use %rsp for accessing the saved user registers, but do that via %r12.

---
V3:
- use %r12 instead %rdi (Jan Beulich)
- remove some compat changes (Jan Beulich)

---
 xen/arch/x86/x86_64/compat/entry.S  |  10 ++-
 xen/arch/x86/x86_64/entry.S         | 152 ++++++++++++++++++++----------------
 xen/include/asm-x86/current.h       |   8 +-
 xen/include/asm-x86/nops.h          |   2 +-
 xen/include/asm-x86/spec_ctrl_asm.h |  13 +--
 5 files changed, 102 insertions(+), 83 deletions(-)
xen/arch/x86/x86_64/compat/entry.S +6 -4
index 8fac5d304d..eced1475b7 100644
@@ -18,15 +18,16 @@ ENTRY(entry_int82)
         pushq $0
         movl  $HYPERCALL_VECTOR, 4(%rsp)
         SAVE_ALL compat=1 /* DPL1 gate, restricted to 32bit PV guests only. */
+        mov   %rsp, %r12
 
-        SPEC_CTRL_ENTRY_FROM_PV /* Req: %rsp=regs/cpuinfo, Clob: acd */
+        SPEC_CTRL_ENTRY_FROM_PV /* Req: %r12=regs, %rsp=cpuinfo, Clob: acd */
         /* WARNING! `ret`, `call *`, `jmp *` not safe before this point. */
 
         CR4_PV32_RESTORE
 
         GET_CURRENT(bx)
 
-        mov   %rsp, %rdi
+        mov   %r12, %rdi
         call  do_entry_int82
 
 /* %rbx: struct vcpu */
@@ -201,7 +202,6 @@ ENTRY(compat_post_handle_exception)
 /* See lstar_enter for entry register state. */
 ENTRY(cstar_enter)
         sti
-        CR4_PV32_RESTORE
         movq  8(%rsp),%rax /* Restore %rax. */
         movq  $FLAT_KERNEL_SS,8(%rsp)
         pushq %r11
@@ -210,10 +210,12 @@ ENTRY(cstar_enter)
         pushq $0
         movl  $TRAP_syscall, 4(%rsp)
         SAVE_ALL
+        movq  %rsp, %r12
 
-        SPEC_CTRL_ENTRY_FROM_PV /* Req: %rsp=regs/cpuinfo, Clob: acd */
+        SPEC_CTRL_ENTRY_FROM_PV /* Req: %r12=regs, %rsp=cpuinfo, Clob: acd */
         /* WARNING! `ret`, `call *`, `jmp *` not safe before this point. */
 
+        CR4_PV32_RESTORE
         GET_CURRENT(bx)
         movq  VCPU_domain(%rbx),%rcx
         cmpb  $0,DOMAIN_is_32bit_pv(%rcx)
xen/arch/x86/x86_64/entry.S +83 -69

406 lines not on this page · raw message

xen/include/asm-x86/current.h +5 -3
index 1087239357..83d226a1ba 100644
@@ -102,9 +102,11 @@ unsigned long get_stack_dump_bottom (unsigned long sp);
     ({                                                                  \
         __asm__ __volatile__ (                                          \
             "mov %0,%%"__OP"sp;"                                        \
-            CHECK_FOR_LIVEPATCH_WORK                                      \
-             "jmp %c1"                                                  \
-            : : "r" (guest_cpu_user_regs()), "i" (__fn) : "memory" );   \
+            "mov %1,%%r12;"                                             \
+            CHECK_FOR_LIVEPATCH_WORK                                    \
+            "jmp %c2"                                                   \
+            : : "r" (get_cpu_info()), "r" (guest_cpu_user_regs()),      \
+                "i" (__fn) : "memory" );                                \
         unreachable();                                                  \
     })
 
xen/include/asm-x86/nops.h +1 -1
index 61319ccfba..daf95f7147 100644
@@ -68,7 +68,7 @@
 #define ASM_NOP17 ASM_NOP8; ASM_NOP7; ASM_NOP2
 #define ASM_NOP21 ASM_NOP8; ASM_NOP8; ASM_NOP5
 #define ASM_NOP24 ASM_NOP8; ASM_NOP8; ASM_NOP8
-#define ASM_NOP29 ASM_NOP8; ASM_NOP8; ASM_NOP8; ASM_NOP5
+#define ASM_NOP30 ASM_NOP8; ASM_NOP8; ASM_NOP8; ASM_NOP6
 #define ASM_NOP32 ASM_NOP8; ASM_NOP8; ASM_NOP8; ASM_NOP8
 #define ASM_NOP40 ASM_NOP8; ASM_NOP8; ASM_NOP8; ASM_NOP8; ASM_NOP8
 
xen/include/asm-x86/spec_ctrl_asm.h +7 -6
index 814f53dffc..5868db8db2 100644
@@ -144,7 +144,8 @@
 
 .macro DO_SPEC_CTRL_ENTRY maybexen:req ibrs_val:req
 /*
- * Requires %rsp=regs (also cpuinfo if !maybexen)
+ * Requires %r12=regs
+ * Requires %rsp=stack_end (if !maybexen)
  * Requires %r14=stack_end (if maybexen)
  * Clobbers %rax, %rcx, %rdx
  *
@@ -162,7 +163,7 @@
      */
     .if \maybexen
         /* Branchless `if ( !xen ) clear_shadowing` */
-        testb $3, UREGS_cs(%rsp)
+        testb $3, UREGS_cs(%r12)
         setz %al
         and %al, STACK_CPUINFO_FIELD(use_shadow_spec_ctrl)(%r14)
     .else
@@ -197,7 +198,7 @@
 
 .macro DO_SPEC_CTRL_EXIT_TO_GUEST
 /*
- * Requires %eax=spec_ctrl, %rsp=regs/cpuinfo
+ * Requires %eax=spec_ctrl, %rsp=cpuinfo
  * Clobbers %rcx, %rdx
  *
  * When returning to guest context, set up SPEC_CTRL shadowing and load the
@@ -241,7 +242,7 @@
 #define SPEC_CTRL_ENTRY_FROM_INTR                                       \
     ALTERNATIVE __stringify(ASM_NOP40),                                 \
         DO_OVERWRITE_RSB, X86_FEATURE_RSB_NATIVE;                       \
-    ALTERNATIVE_2 __stringify(ASM_NOP29),                               \
+    ALTERNATIVE_2 __stringify(ASM_NOP30),                               \
         __stringify(DO_SPEC_CTRL_ENTRY maybexen=1                       \
                     ibrs_val=SPEC_CTRL_IBRS),                           \
         X86_FEATURE_XEN_IBRS_SET,                                       \
@@ -263,7 +264,7 @@
 /* TODO: Drop these when the alternatives infrastructure is NMI/#MC safe. */
 .macro SPEC_CTRL_ENTRY_FROM_INTR_IST
 /*
- * Requires %rsp=regs, %r14=stack_end
+ * Requires %r12=regs, %r14=stack_end
  * Clobbers %rax, %rcx, %rdx
  *
  * This is logical merge of DO_OVERWRITE_RSB and DO_SPEC_CTRL_ENTRY
@@ -282,7 +283,7 @@
     jz .L\@_skip_wrmsr
 
     xor %edx, %edx
-    testb $3, UREGS_cs(%rsp)
+    testb $3, UREGS_cs(%r12)
     setz %dl
     and %dl, STACK_CPUINFO_FIELD(use_shadow_spec_ctrl)(%r14)
 
Signature · 7 lines
2.13.6


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

PATCH v3 5/17 x86: add a xpti command line parameter

In reply to Juergen Gross

Add a command line parameter for controlling Xen page table isolation (XPTI): per default it is on for non-AMD systems in 64 bit pv domains.

Possible settings are:
- true: switched on even on AMD systems
- false: switched off for all
- nodom0: switched off for dom0

As we don't want to set XPTI for 32 bit pv domains we have to delay XPTI initialization until XEN_DOMCTL_set_address_size has been called for the domain. Dom0 needs a specific init call.

---
V3:
- move XPTI initialization call to XEN_DOMCTL_set_address_size handling
- move XPTI code into arch/x86/pv/xpti.c
- replace xpti flag in struct domain by pointer
- add is_*_xpti_active() helpers

---
 docs/misc/xen-command-line.markdown |  18 ++++++
 xen/arch/x86/domctl.c               |   4 ++
 xen/arch/x86/pv/Makefile            |   1 +
 xen/arch/x86/pv/dom0_build.c        |   3 +
 xen/arch/x86/pv/domain.c            |   3 +
 xen/arch/x86/pv/xpti.c              | 111 ++++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/domain.h        |   4 ++
 xen/include/asm-x86/pv/mm.h         |  20 +++++++
 8 files changed, 164 insertions(+)
 create mode 100644 xen/arch/x86/pv/xpti.c
docs/misc/xen-command-line.markdown +18 -0
index 6df39dae0b..f96bb6342d 100644
@@ -1926,6 +1926,24 @@ In the case that x2apic is in use, this option switches between physical and
 clustered mode.  The default, given no hint from the **FADT**, is cluster
 mode.
 
+### xpti
+> `= nodom0 | default | <boolean>`
+
+> Default: `false` on AMD hardware, `true` everywhere else.
+
+> Can be modified at runtime
+
+Override default selection of whether to isolate 64-bit PV guest page
+tables.
+
+`true` activates page table isolation even on AMD hardware.
+
+`false` deactivates page table isolation on all systems.
+
+`nodom0` deactivates page table isolation for dom0.
+
+`default` switch to default settings.
+
 ### xsave
 > `= <boolean>`
 
xen/arch/x86/domctl.c +4 -0
index 8fbbf3aeb3..0b448e411d 100644
@@ -31,6 +31,7 @@
 #include <xen/vm_event.h>
 #include <public/vm_event.h>
 #include <asm/mem_sharing.h>
+#include <asm/pv/mm.h>
 #include <asm/xstate.h>
 #include <asm/debugger.h>
 #include <asm/psr.h>
@@ -610,6 +611,9 @@ long arch_do_domctl(
             ret = switch_compat(d);
         else
             ret = -EINVAL;
+
+        if ( ret == 0 )
+            ret = xpti_domain_init(d);
         break;
 
     case XEN_DOMCTL_get_address_size:
xen/arch/x86/pv/Makefile +1 -0
index 65bca04175..a12e4fbd1a 100644
@@ -13,6 +13,7 @@ obj-y += mm.o
 obj-y += ro-page-fault.o
 obj-$(CONFIG_PV_SHIM) += shim.o
 obj-y += traps.o
+obj-y += xpti.o
 
 obj-bin-y += dom0_build.init.o
 obj-bin-y += gpr_switch.o
xen/arch/x86/pv/dom0_build.c +3 -0
index 0bd2f1bf90..6e7bc435ab 100644
@@ -707,6 +707,9 @@ int __init dom0_construct_pv(struct domain *d,
             cpu = p->processor;
     }
 
+    if ( !is_pv_32bit_domain(d) )
+        xpti_domain_init(d);
+
     d->arch.paging.mode = 0;
 
     /* Set up CR3 value for write_ptbase */
xen/arch/x86/pv/domain.c +3 -0
index 2c784fb3cc..a007af94dd 100644
@@ -10,6 +10,7 @@
 #include <xen/sched.h>
 
 #include <asm/pv/domain.h>
+#include <asm/pv/mm.h>
 
 /* Override macros from asm/page.h to make them work with mfn_t */
 #undef mfn_to_page
@@ -174,6 +175,8 @@ void pv_domain_destroy(struct domain *d)
 
     free_xenheap_page(d->arch.pv_domain.gdt_ldt_l1tab);
     d->arch.pv_domain.gdt_ldt_l1tab = NULL;
+
+    xpti_domain_destroy(d);
 }
 
 
xen/arch/x86/pv/xpti.c new file +111 -0

111 lines not on this page · raw message

xen/include/asm-x86/domain.h +4 -0
index 4679d5477d..b33c286807 100644
@@ -247,6 +247,8 @@ struct time_scale {
     u32 mul_frac;
 };
 
+struct xpti_domain;
+
 struct pv_domain
 {
     l1_pgentry_t **gdt_ldt_l1tab;
@@ -257,6 +259,8 @@ struct pv_domain
     struct mapcache_domain mapcache;
 
     struct cpuidmasks *cpuidmasks;
+
+    struct xpti_domain *xpti;
 };
 
 struct monitor_write_data {
xen/include/asm-x86/pv/mm.h +20 -0
index 246b99014c..dfac89df0b 100644
@@ -31,6 +31,19 @@ void pv_destroy_gdt(struct vcpu *v);
 bool pv_map_ldt_shadow_page(unsigned int off);
 bool pv_destroy_ldt(struct vcpu *v);
 
+int xpti_domain_init(struct domain *d);
+void xpti_domain_destroy(struct domain *d);
+
+static inline bool is_domain_xpti_active(const struct domain *d)
+{
+    return is_pv_domain(d) && d->arch.pv_domain.xpti;
+}
+
+static inline bool is_vcpu_xpti_active(const struct vcpu *v)
+{
+    return is_domain_xpti_active(v->domain);
+}
+
 #else
 
 #include <xen/errno.h>
@@ -52,6 +65,13 @@ static inline bool pv_map_ldt_shadow_page(unsigned int off) { return false; }
 static inline bool pv_destroy_ldt(struct vcpu *v)
 { ASSERT_UNREACHABLE(); return false; }
 
+static inline int xpti_domain_init(struct domain *d) { return 0; }
+static inline void xpti_domain_destroy(struct domain *d) { }
+
+static inline bool is_domain_xpti_active(const struct domain *d)
+{ return false; }
+static inline bool is_vcpu_xpti_active(const struct vcpu *v) { return false; }
+
 #endif
 
 #endif /* __X86_PV_MM_H__ */
Signature · 7 lines
2.13.6


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

PATCH v3 6/17 x86: allow per-domain mappings without NX bit or with specific mfn

In reply to Juergen Gross

For support of per-vcpu stacks we need per-vcpu trampolines. To be able to put those into the per-domain mappings the upper levels page tables must not have NX set for per-domain mappings.

As create_perdomain_mapping() creates L1 mappings with flags being __PAGE_HYPERVISOR_RW this won't change any of the current per domain mappings to become executable.

---
V3:
- remove functions for modifying per-domain mappings (Jan Beulich)

---
 xen/arch/x86/mm.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
xen/arch/x86/mm.c +3 -3
index e795239829..d86e07e9f8 100644
@@ -1568,7 +1568,7 @@ void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mfn,
 
     /* Slot 260: Per-domain mappings (if applicable). */
     l4t[l4_table_offset(PERDOMAIN_VIRT_START)] =
-        d ? l4e_from_page(d->arch.perdomain_l3_pg, __PAGE_HYPERVISOR_RW)
+        d ? l4e_from_page(d->arch.perdomain_l3_pg, __PAGE_HYPERVISOR)
           : l4e_empty();
 
     /* Slot 261-: text/data/bss, RW M2P, vmap, frametable, directmap. */
@@ -5319,7 +5319,7 @@ int create_perdomain_mapping(struct domain *d, unsigned long va,
         }
         l2tab = __map_domain_page(pg);
         clear_page(l2tab);
-        l3tab[l3_table_offset(va)] = l3e_from_page(pg, __PAGE_HYPERVISOR_RW);
+        l3tab[l3_table_offset(va)] = l3e_from_page(pg, __PAGE_HYPERVISOR);
     }
     else
         l2tab = map_l2t_from_l3e(l3tab[l3_table_offset(va)]);
@@ -5361,7 +5361,7 @@ int create_perdomain_mapping(struct domain *d, unsigned long va,
                 l1tab = __map_domain_page(pg);
             }
             clear_page(l1tab);
-            *pl2e = l2e_from_page(pg, __PAGE_HYPERVISOR_RW);
+            *pl2e = l2e_from_page(pg, __PAGE_HYPERVISOR);
         }
         else if ( !l1tab )
             l1tab = map_l1t_from_l2e(*pl2e);
Signature · 7 lines
2.13.6


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

PATCH v3 7/17 xen/x86: split _set_tssldt_desc() into ldt and tss specific functions

In reply to Juergen Gross

_set_tssldt_desc() is used to set LDT or TSS descriptors in the GDT. As LDT descriptors might be shared across cpus care is taken to not create a temporary invalid descriptor.

Split _set_tssldt_desc() into dedicated functions for setting either a LDT or a TSS descriptor. For LDT descriptors this is basically the same as today, while TSS descriptors can be written without using barriers as those are written for the local cpu only.

---
V3:
- new patch

---
 xen/arch/x86/cpu/common.c  |  4 ++--
 xen/arch/x86/traps.c       |  4 ++--
 xen/include/asm-x86/desc.h | 14 +++++++++++++-
 xen/include/asm-x86/ldt.h  |  2 +-
 4 files changed, 18 insertions(+), 6 deletions(-)
xen/arch/x86/cpu/common.c +2 -2
index 4306e59650..e0ae8120a6 100644
@@ -755,12 +755,12 @@ void load_system_tables(void)
 		.bitmap = IOBMP_INVALID_OFFSET,
 	};
 
-	_set_tssldt_desc(
+	_set_tss_desc(
 		gdt + TSS_ENTRY,
 		(unsigned long)tss,
 		offsetof(struct tss_struct, __cacheline_filler) - 1,
 		SYS_DESC_tss_avail);
-	_set_tssldt_desc(
+	_set_tss_desc(
 		compat_gdt + TSS_ENTRY,
 		(unsigned long)tss,
 		offsetof(struct tss_struct, __cacheline_filler) - 1,
xen/arch/x86/traps.c +2 -2
index 13a852ca4e..9b29014e2c 100644
@@ -1845,12 +1845,12 @@ void load_TR(void)
         .limit = LAST_RESERVED_GDT_BYTE
     };
 
-    _set_tssldt_desc(
+    _set_tss_desc(
         this_cpu(gdt_table) + TSS_ENTRY - FIRST_RESERVED_GDT_ENTRY,
         (unsigned long)tss,
         offsetof(struct tss_struct, __cacheline_filler) - 1,
         SYS_DESC_tss_avail);
-    _set_tssldt_desc(
+    _set_tss_desc(
         this_cpu(compat_gdt_table) + TSS_ENTRY - FIRST_RESERVED_GDT_ENTRY,
         (unsigned long)tss,
         offsetof(struct tss_struct, __cacheline_filler) - 1,
xen/include/asm-x86/desc.h +13 -1
index 4093c65faa..6ec515582d 100644
@@ -171,7 +171,7 @@ static inline void _update_gate_addr_lower(idt_entry_t *gate, void *addr)
     _write_gate_lower(gate, &idte);
 }
 
-#define _set_tssldt_desc(desc,addr,limit,type)           \
+#define _set_ldt_desc(desc,addr,limit,type)              \
 do {                                                     \
     (desc)[0].b = (desc)[1].b = 0;                       \
     smp_wmb(); /* disable entry /then/ rewrite */        \
@@ -185,6 +185,18 @@ do {                                                     \
         (((u32)(addr) & 0x00FF0000U) >> 16);             \
 } while (0)
 
+#define _set_tss_desc(desc,addr,limit,type)              \
+do {                                                     \
+    (desc)[0].a =                                        \
+        ((u32)(addr) << 16) | ((u32)(limit) & 0xFFFF);   \
+    (desc)[0].b =                                        \
+        ((u32)(addr) & 0xFF000000U) |                    \
+        ((u32)(type) << 8) | 0x8000U |                   \
+        (((u32)(addr) & 0x00FF0000U) >> 16);             \
+    (desc)[1].a = (u32)(((unsigned long)(addr)) >> 32);  \
+    (desc)[1].b = 0;                                     \
+} while (0)
+
 struct __packed desc_ptr {
 	unsigned short limit;
 	unsigned long base;
xen/include/asm-x86/ldt.h +1 -1
index 589daf83c6..6179cef9e9 100644
@@ -16,7 +16,7 @@ static inline void load_LDT(struct vcpu *v)
         desc = (!is_pv_32bit_vcpu(v)
                 ? this_cpu(gdt_table) : this_cpu(compat_gdt_table))
                + LDT_ENTRY - FIRST_RESERVED_GDT_ENTRY;
-        _set_tssldt_desc(desc, LDT_VIRT_START(v), ents*8-1, SYS_DESC_ldt);
+        _set_ldt_desc(desc, LDT_VIRT_START(v), ents*8-1, SYS_DESC_ldt);
         lldt(LDT_ENTRY << 3);
     }
 }
Signature · 7 lines
2.13.6


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

PATCH v3 8/17 x86: add support for spectre mitigation with local thunk

In reply to Juergen Gross

Right now an indirect jump might use a relative jump to a retpoline thunk in order to mitigate the Spectre vulnerability.

In case the code using the indirect jump is remapped to another virtual address this won't work any longer, so add support for indirect jumps using a local thunk instead.

---
V3:
- new patch

---
 xen/arch/x86/indirect-thunk.S            | 23 +----------------------
 xen/include/asm-x86/asm_defns.h          | 25 +++++++++++++++++++++++++
 xen/include/asm-x86/indirect_thunk_asm.h |  8 ++++++--
 3 files changed, 32 insertions(+), 24 deletions(-)
xen/arch/x86/indirect-thunk.S +1 -22
index e03fc14c73..b4d3e4cec4 100644
@@ -11,25 +11,6 @@
 
 #include <asm/asm_defns.h>
 
-.macro IND_THUNK_RETPOLINE reg:req
-        call 2f
-1:
-        lfence
-        jmp 1b
-2:
-        mov %\reg, (%rsp)
-        ret
-.endm
-
-.macro IND_THUNK_LFENCE reg:req
-        lfence
-        jmp *%\reg
-.endm
-
-.macro IND_THUNK_JMP reg:req
-        jmp *%\reg
-.endm
-
 /*
  * Build the __x86_indirect_thunk_* symbols.  Execution lands on an
  * alternative patch point which implements one of the above THUNK_*'s
@@ -38,9 +19,7 @@
         .section .text.__x86_indirect_thunk_\reg, "ax", @progbits
 
 ENTRY(__x86_indirect_thunk_\reg)
-        ALTERNATIVE_2 __stringify(IND_THUNK_RETPOLINE \reg),              \
-        __stringify(IND_THUNK_LFENCE \reg), X86_FEATURE_IND_THUNK_LFENCE, \
-        __stringify(IND_THUNK_JMP \reg),    X86_FEATURE_IND_THUNK_JMP
+        GEN_INDIRECT_THUNK_BODY \reg
 .endm
 
 /* Instantiate GEN_INDIRECT_THUNK for each register except %rsp. */
xen/include/asm-x86/asm_defns.h +25 -0
index 2a79e8a7f4..7d26391be8 100644
@@ -334,6 +334,31 @@ static always_inline void stac(void)
         subq  $-(UREGS_error_code-UREGS_r15+\adj), %rsp
 .endm
 
+.macro IND_THUNK_RETPOLINE reg:req
+        call 2f
+1:
+        lfence
+        jmp 1b
+2:
+        mov %\reg, (%rsp)
+        ret
+.endm
+
+.macro IND_THUNK_LFENCE reg:req
+        lfence
+        jmp *%\reg
+.endm
+
+.macro IND_THUNK_JMP reg:req
+        jmp *%\reg
+.endm
+
+.macro GEN_INDIRECT_THUNK_BODY reg:req
+        ALTERNATIVE_2 __stringify(IND_THUNK_RETPOLINE \reg),              \
+        __stringify(IND_THUNK_LFENCE \reg), X86_FEATURE_IND_THUNK_LFENCE, \
+        __stringify(IND_THUNK_JMP \reg),    X86_FEATURE_IND_THUNK_JMP
+.endm
+
 #endif
 
 #ifdef CONFIG_PERF_COUNTERS
xen/include/asm-x86/indirect_thunk_asm.h +6 -2
index 96bcc25497..3abb32caee 100644
@@ -3,7 +3,7 @@
  * usual #ifdef'ary to turn into comments.
  */
 
-.macro INDIRECT_BRANCH insn:req arg:req
+.macro INDIRECT_BRANCH insn:req arg:req label=__x86_indirect_thunk_r
 /*
  * Create an indirect branch.  insn is one of call/jmp, arg is a single
  * register.
@@ -16,7 +16,7 @@
         $done = 0
         .irp reg, ax, cx, dx, bx, bp, si, di, 8, 9, 10, 11, 12, 13, 14, 15
         .ifeqs "\arg", "%r\reg"
-            \insn __x86_indirect_thunk_r\reg
+            \insn \label\reg
             $done = 1
            .exitm
         .endif
@@ -39,3 +39,7 @@
 .macro INDIRECT_JMP arg:req
     INDIRECT_BRANCH jmp \arg
 .endm
+
+.macro INDIRECT_LOCAL_JMP arg:req
+    INDIRECT_BRANCH jmp \arg local__x86_indirect_thunk_r
+.endm
Signature · 7 lines
2.13.6


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

PATCH v3 9/17 x86: create syscall stub for per-domain mapping

In reply to Juergen Gross

The current syscall stub can't be used mapped in the per domain area as required by XPTI due to the distance for jumping into the common interrupt handling code is larger than 2GB. Using just an indirect jump isn't going to work as this will require mitigations against Spectre.

So use a new trampoline which is no longer unique to a (v)cpu, but can be mapped into the per-domain area as needed. For addressing the stack use the knowledge that the primary stack will be in the next page after the trampoline coding so we can save %rsp via a %rip relative access without needing any further register.

For being able to easily switch between per-cpu and per-vcpu stubs add a macro for the per-cpu stub size and add the prototypes of [cl]star_enter() to a header.

---
V3:
- completely new per-vcpu stub containing Spectre mitigation

---
 xen/arch/x86/pv/Makefile           |  1 +
 xen/arch/x86/pv/xpti-stub.S        | 61 ++++++++++++++++++++++++++++++++++++++
 xen/arch/x86/x86_64/compat/entry.S |  1 +
 xen/arch/x86/x86_64/entry.S        |  1 +
 xen/arch/x86/x86_64/traps.c        |  3 +-
 xen/include/asm-x86/system.h       |  5 ++++
 6 files changed, 70 insertions(+), 2 deletions(-)
 create mode 100644 xen/arch/x86/pv/xpti-stub.S
xen/arch/x86/pv/Makefile +1 -0
index a12e4fbd1a..3f6b5506dc 100644
@@ -17,3 +17,4 @@ obj-y += xpti.o
 
 obj-bin-y += dom0_build.init.o
 obj-bin-y += gpr_switch.o
+obj-bin-y += xpti-stub.o
xen/arch/x86/pv/xpti-stub.S new file +61 -0

61 lines not on this page · raw message

xen/arch/x86/x86_64/compat/entry.S +1 -0
index eced1475b7..206bc9a05a 100644
@@ -215,6 +215,7 @@ ENTRY(cstar_enter)
         SPEC_CTRL_ENTRY_FROM_PV /* Req: %r12=regs, %rsp=cpuinfo, Clob: acd */
         /* WARNING! `ret`, `call *`, `jmp *` not safe before this point. */
 
+GLOBAL(cstar_common)
         CR4_PV32_RESTORE
         GET_CURRENT(bx)
         movq  VCPU_domain(%rbx),%rcx
xen/arch/x86/x86_64/entry.S +1 -0
index f067a74b0f..69590d0b17 100644
@@ -120,6 +120,7 @@ ENTRY(lstar_enter)
         SPEC_CTRL_ENTRY_FROM_PV /* Req: %r12=regs, %rsp=cpuinfo, Clob: acd */
         /* WARNING! `ret`, `call *`, `jmp *` not safe before this point. */
 
+GLOBAL(lstar_common)
         GET_CURRENT(bx)
         testb $TF_kernel_mode,VCPU_thread_flags(%rbx)
         jz    switch_to_kernel
xen/arch/x86/x86_64/traps.c +1 -2
index 3652f5ff21..bd4d37c2ad 100644
@@ -291,8 +291,6 @@ static unsigned int write_stub_trampoline(
 }
 
 DEFINE_PER_CPU(struct stubs, stubs);
-void lstar_enter(void);
-void cstar_enter(void);
 
 void subarch_percpu_traps_init(void)
 {
@@ -315,6 +313,7 @@ void subarch_percpu_traps_init(void)
     offset = write_stub_trampoline(stub_page + (stub_va & ~PAGE_MASK),
                                    stub_va, stack_bottom,
                                    (unsigned long)lstar_enter);
+    ASSERT(offset == STUB_TRAMPOLINE_SIZE_PERCPU);
     stub_va += offset;
 
     if ( boot_cpu_data.x86_vendor == X86_VENDOR_INTEL ||
xen/include/asm-x86/system.h +5 -0
index 8ac170371b..06afc59822 100644
@@ -230,6 +230,11 @@ static inline int local_irq_is_enabled(void)
 
 void trap_init(void);
 void init_idt_traps(void);
+#define STUB_TRAMPOLINE_SIZE_PERCPU   32
+void lstar_enter(void);
+void cstar_enter(void);
+void xpti_lstar(void);
+void xpti_cstar(void);
 void load_system_tables(void);
 void percpu_traps_init(void);
 void subarch_percpu_traps_init(void);
Signature · 7 lines
2.13.6


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

PATCH v3 10/17 x86: allocate per-vcpu stacks for interrupt entries

In reply to Juergen Gross

In case of XPTI being active for a pv-domain allocate and initialize per-vcpu stacks. The stacks are added to the per-domain mappings of the pv-domain.

---
V3:
- move xpti code to xpti.c
- directly modify page table entries as needed for stub and stack page (Jan Beulich)
- use one page for all stacks and TSS
- remap global stub instead allocating one for each vcpu

---
 xen/arch/x86/pv/domain.c      |   2 +
 xen/arch/x86/pv/xpti.c        | 117 +++++++++++++++++++++++++++++++++++++++---
 xen/include/asm-x86/config.h  |  13 ++++-
 xen/include/asm-x86/current.h |  49 +++++++++++++-----
 xen/include/asm-x86/domain.h  |   3 ++
 xen/include/asm-x86/pv/mm.h   |   2 +
 6 files changed, 166 insertions(+), 20 deletions(-)
xen/arch/x86/pv/domain.c +2 -0
index a007af94dd..550fbbf0fe 100644
@@ -120,6 +120,8 @@ void pv_vcpu_destroy(struct vcpu *v)
     pv_destroy_gdt_ldt_l1tab(v);
     xfree(v->arch.pv_vcpu.trap_ctxt);
     v->arch.pv_vcpu.trap_ctxt = NULL;
+
+    xpti_vcpu_destroy(v);
 }
 
 int pv_vcpu_initialise(struct vcpu *v)
xen/arch/x86/pv/xpti.c +110 -7

148 lines not on this page · raw message

xen/include/asm-x86/config.h +12 -1
index 9ef9d03ca7..b563a2f85b 100644
@@ -66,6 +66,7 @@
 #endif
 
 #define STACK_ORDER 3
+#define STACK_PAGES (1 << STACK_ORDER)
 #define STACK_SIZE  (PAGE_SIZE << STACK_ORDER)
 
 #define TRAMPOLINE_STACK_SPACE  PAGE_SIZE
@@ -202,7 +203,7 @@ extern unsigned char boot_edid_info[128];
 /* 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         3
+#define PERDOMAIN_SLOTS         4
 #define PERDOMAIN_VIRT_SLOT(s)  (PERDOMAIN_VIRT_START + (s) * \
                                  (PERDOMAIN_SLOT_MBYTES << 20))
 /* Slot 261: machine-to-phys conversion table (256GB). */
@@ -310,6 +311,16 @@ extern unsigned long xen_phys_start;
 #define ARG_XLAT_START(v)        \
     (ARG_XLAT_VIRT_START + ((v)->vcpu_id << ARG_XLAT_VA_SHIFT))
 
+/* Per-vcpu XPTI pages. The fourth per-domain-mapping sub-area. */
+#define XPTI_VIRT_START          PERDOMAIN_VIRT_SLOT(3)
+#define XPTI_VA_SHIFT            (PAGE_SHIFT + STACK_ORDER)
+#define XPTI_TRAMPOLINE_OFF      ((STACK_PAGES - 2) << PAGE_SHIFT)
+#define XPTI_TSS_OFF             ((STACK_PAGES - 1) << PAGE_SHIFT)
+#define XPTI_START(v)            (XPTI_VIRT_START + \
+                                  ((v)->vcpu_id << XPTI_VA_SHIFT))
+#define XPTI_TRAMPOLINE(v)       (XPTI_START(v) + XPTI_TRAMPOLINE_OFF)
+#define XPTI_TSS(v)              (XPTI_START(v) + XPTI_TSS_OFF)
+
 #define NATIVE_VM_ASSIST_VALID   ((1UL << VMASST_TYPE_4gb_segments)        | \
                                   (1UL << VMASST_TYPE_4gb_segments_notify) | \
                                   (1UL << VMASST_TYPE_writable_pagetables) | \
xen/include/asm-x86/current.h +37 -12

67 lines not on this page · raw message

xen/include/asm-x86/domain.h +3 -0
index b33c286807..1a4e92481c 100644
@@ -505,6 +505,9 @@ struct pv_vcpu
     /* Deferred VA-based update state. */
     bool_t need_update_runstate_area;
     struct vcpu_time_info pending_system_time;
+
+    /* If XPTI is active: pointer to user regs on stack. */
+    struct cpu_user_regs *stack_regs;
 };
 
 typedef enum __packed {
xen/include/asm-x86/pv/mm.h +2 -0
index dfac89df0b..34c51bcfba 100644
@@ -31,6 +31,7 @@ void pv_destroy_gdt(struct vcpu *v);
 bool pv_map_ldt_shadow_page(unsigned int off);
 bool pv_destroy_ldt(struct vcpu *v);
 
+void xpti_vcpu_destroy(struct vcpu *v);
 int xpti_domain_init(struct domain *d);
 void xpti_domain_destroy(struct domain *d);
 
@@ -65,6 +66,7 @@ static inline bool pv_map_ldt_shadow_page(unsigned int off) { return false; }
 static inline bool pv_destroy_ldt(struct vcpu *v)
 { ASSERT_UNREACHABLE(); return false; }
 
+static inline void xpti_vcpu_init(struct vcpu *v) { }
 static inline int xpti_domain_init(struct domain *d) { return 0; }
 static inline void xpti_domain_destroy(struct domain *d) { }
 
Signature · 7 lines
2.13.6


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

PATCH v3 11/17 x86: modify interrupt handlers to support stack switching

In reply to Juergen Gross

Modify the interrupt handlers to switch stacks on interrupt entry in case they are running on a per-vcpu stack. Same applies to returning to the guest: in case the to be loaded context is located on a per-vcpu stack switch to this one before returning to the guest.

The NMI and MCE interrupt handlers share most of their code today. Use the common part only after switching stacks as this will enable us calculating the correct stack address mostly at build time instead of doing it all at runtime.

guest_cpu_user_regs() is modified to always return the correct user registers address, either like today the one of the per physical cpu stack, or that of the per-vcpu stack. Depending on the usage some callers of guest_cpu_user_regs() need to be adapted to use get_cpu_info() instead.

---
V3:
- rework SWITCH_FROM_VCPU_STACK_IST to take the ist as parameter

---
 xen/arch/x86/pv/xpti-stub.S       |  4 ++--
 xen/arch/x86/x86_64/asm-offsets.c |  2 ++
 xen/arch/x86/x86_64/entry.S       | 14 ++++++++++++--
 xen/common/wait.c                 |  8 ++++----
 xen/include/asm-x86/asm_defns.h   | 19 +++++++++++++++++++
 xen/include/asm-x86/current.h     | 15 ++++++++++++++-
 xen/include/asm-x86/processor.h   | 12 ++++++------
 7 files changed, 59 insertions(+), 15 deletions(-)
xen/arch/x86/pv/xpti-stub.S +2 -2
index efa1e3f661..92f2ef6dac 100644
@@ -26,7 +26,7 @@ ENTRY(xpti_lstar)
         movl  $TRAP_syscall, 4(%rsp)
         SAVE_ALL
         mov   %rsp, %r12
-
+        SWITCH_FROM_VCPU_STACK
         sti
 
         SPEC_CTRL_ENTRY_FROM_PV /* Req: %r12=regs, %rsp=cpuinfo, Clob: acd */
@@ -46,7 +46,7 @@ ENTRY(xpti_cstar)
         movl  $TRAP_syscall, 4(%rsp)
         SAVE_ALL
         movq  %rsp, %r12
-
+        SWITCH_FROM_VCPU_STACK
         sti
 
         SPEC_CTRL_ENTRY_FROM_PV /* Req: %r12=regs, %rsp=cpuinfo, Clob: acd */
xen/arch/x86/x86_64/asm-offsets.c +2 -0
index cc7753c0a9..b0060be261 100644
@@ -141,6 +141,8 @@ void __dummy__(void)
     OFFSET(CPUINFO_shadow_spec_ctrl, struct cpu_info, shadow_spec_ctrl);
     OFFSET(CPUINFO_use_shadow_spec_ctrl, struct cpu_info, use_shadow_spec_ctrl);
     OFFSET(CPUINFO_bti_ist_info, struct cpu_info, bti_ist_info);
+    OFFSET(CPUINFO_stack_bottom_cpu, struct cpu_info, stack_bottom_cpu);
+    OFFSET(CPUINFO_flags, struct cpu_info, flags);
     DEFINE(CPUINFO_sizeof, sizeof(struct cpu_info));
     BLANK();
 
xen/arch/x86/x86_64/entry.S +12 -2
index 69590d0b17..909f6eea66 100644
@@ -45,6 +45,7 @@ restore_all_guest:
         /* WARNING! `ret`, `call *`, `jmp *` not safe beyond this point. */
         SPEC_CTRL_EXIT_TO_GUEST /* Req: a=spec_ctrl %rsp=cpuinfo, Clob: cd */
 
+        SWITCH_TO_VCPU_STACK
         RESTORE_ALL
         testw $TRAP_syscall,4(%rsp)
         jz    iret_exit_to_guest
@@ -202,7 +203,6 @@ process_trap:
         jmp  test_all_events
 
 ENTRY(sysenter_entry)
-        sti
         pushq $FLAT_USER_SS
         pushq $0
         pushfq
@@ -214,6 +214,8 @@ GLOBAL(sysenter_eflags_saved)
         movl  $TRAP_syscall, 4(%rsp)
         SAVE_ALL
         mov   %rsp, %r12
+        SWITCH_FROM_VCPU_STACK
+        sti
 
         SPEC_CTRL_ENTRY_FROM_PV /* Req: %r12=regs, %rsp=cpuinfo, Clob: acd */
         /* WARNING! `ret`, `call *`, `jmp *` not safe before this point. */
@@ -254,6 +256,7 @@ ENTRY(int80_direct_trap)
         movl  $0x80, 4(%rsp)
         SAVE_ALL
         mov   %rsp, %r12
+        SWITCH_FROM_VCPU_STACK
 
         SPEC_CTRL_ENTRY_FROM_PV /* Req: %r12=regs, %rsp=cpuinfo, Clob: acd */
         /* WARNING! `ret`, `call *`, `jmp *` not safe before this point. */
@@ -422,6 +425,7 @@ ENTRY(dom_crash_sync_extable)
 ENTRY(common_interrupt)
         SAVE_ALL CLAC
         mov   %rsp, %r12
+        SWITCH_FROM_VCPU_STACK
 
         GET_STACK_END(14)
 
@@ -449,6 +453,7 @@ ENTRY(page_fault)
 GLOBAL(handle_exception)
         SAVE_ALL CLAC
         mov   %rsp, %r12
+        SWITCH_FROM_VCPU_STACK
 
         GET_STACK_END(14)
 
@@ -631,6 +636,7 @@ ENTRY(double_fault)
         /* Set AC to reduce chance of further SMAP faults */
         SAVE_ALL STAC
         movq  %rsp, %r12
+        SWITCH_FROM_VCPU_STACK_IST(IST_DF)
 
         GET_STACK_END(14)
 
@@ -653,10 +659,11 @@ ENTRY(early_page_fault)
 ENTRY(nmi)
         pushq $0
         movl  $TRAP_nmi,4(%rsp)
-handle_ist_exception:
         SAVE_ALL CLAC
         mov   %rsp, %r12
+        SWITCH_FROM_VCPU_STACK_IST(IST_NMI)
 
+handle_ist_exception:
         GET_STACK_END(14)
 
         SPEC_CTRL_ENTRY_FROM_INTR_IST /* Req: %r12=regs, %r14=end, Clob: acd */
@@ -703,6 +710,9 @@ handle_ist_exception:
 ENTRY(machine_check)
         pushq $0
         movl  $TRAP_machine_check,4(%rsp)
+        SAVE_ALL CLAC
+        mov   %rsp, %r12
+        SWITCH_FROM_VCPU_STACK_IST(IST_MCE)
         jmp   handle_ist_exception
 
 /* Enable NMIs.  No special register assumptions. Only %rax is not preserved. */
xen/common/wait.c +4 -4
index a57bc10d61..fbb5d996e5 100644
@@ -122,10 +122,10 @@ void wake_up_all(struct waitqueue_head *wq)
 
 static void __prepare_to_wait(struct waitqueue_vcpu *wqv)
 {
-    struct cpu_info *cpu_info = get_cpu_info();
+    struct cpu_user_regs *user_regs = guest_cpu_user_regs();
     struct vcpu *curr = current;
     unsigned long dummy;
-    u32 entry_vector = cpu_info->guest_cpu_user_regs.entry_vector;
+    u32 entry_vector = user_regs->entry_vector;
 
     ASSERT(wqv->esp == 0);
 
@@ -160,7 +160,7 @@ static void __prepare_to_wait(struct waitqueue_vcpu *wqv)
         "pop %%r11; pop %%r10; pop %%r9;  pop %%r8;"
         "pop %%rbp; pop %%rdx; pop %%rbx; pop %%rax"
         : "=&S" (wqv->esp), "=&c" (dummy), "=&D" (dummy)
-        : "i" (PAGE_SIZE), "0" (0), "1" (cpu_info), "2" (wqv->stack)
+        : "i" (PAGE_SIZE), "0" (0), "1" (user_regs), "2" (wqv->stack)
         : "memory" );
 
     if ( unlikely(wqv->esp == 0) )
@@ -169,7 +169,7 @@ static void __prepare_to_wait(struct waitqueue_vcpu *wqv)
         domain_crash_synchronous();
     }
 
-    cpu_info->guest_cpu_user_regs.entry_vector = entry_vector;
+    user_regs->entry_vector = entry_vector;
 }
 
 static void __finish_wait(struct waitqueue_vcpu *wqv)
xen/include/asm-x86/asm_defns.h +19 -0
index 7d26391be8..f626cc6134 100644
@@ -7,6 +7,7 @@
 #include <asm/asm-offsets.h>
 #endif
 #include <asm/bug.h>
+#include <asm/current.h>
 #include <asm/page.h>
 #include <asm/processor.h>
 #include <asm/percpu.h>
@@ -136,6 +137,24 @@ void ret_from_intr(void);
         GET_STACK_END(reg);                       \
         movq STACK_CPUINFO_FIELD(current_vcpu)(%r##reg), %r##reg
 
+#define SWITCH_FROM_VCPU_STACK                                           \
+        GET_STACK_END(ax);                                               \
+        testb $ON_VCPUSTACK, STACK_CPUINFO_FIELD(flags)(%rax);           \
+        jz    1f;                                                        \
+        movq  STACK_CPUINFO_FIELD(stack_bottom_cpu)(%rax), %rsp;         \
+1:
+
+#define SWITCH_FROM_VCPU_STACK_IST(ist)                                  \
+        GET_STACK_END(ax);                                               \
+        testb $ON_VCPUSTACK, STACK_CPUINFO_FIELD(flags)(%rax);           \
+        jz    1f;                                                        \
+        sub   $(STACK_SIZE - 1 - ist * PAGE_SIZE), %rax;                 \
+        mov   %rax, %rsp;                                                \
+1:
+
+#define SWITCH_TO_VCPU_STACK                                             \
+        mov   %r12, %rsp
+
 #ifndef NDEBUG
 #define ASSERT_NOT_IN_ATOMIC                                             \
     sti; /* sometimes called with interrupts disabled: safe to enable */ \
xen/include/asm-x86/current.h +14 -1
index 5963114e08..e128c13a1e 100644
@@ -9,8 +9,10 @@
 
 #include <xen/percpu.h>
 #include <public/xen.h>
+#include <asm/config.h>
 #include <asm/page.h>
 
+#ifndef __ASSEMBLY__
 /*
  * Xen's physical cpu stacks are 8 pages (8-page aligned), arranged as:
  *
@@ -71,8 +73,10 @@ struct cpu_info {
     };
     unsigned int processor_id;  /* per physical cpu mapping only */
     unsigned int flags;
+#endif /* !__ASSEMBLY__ */
 #define ON_VCPUSTACK      0x00000001
 #define VCPUSTACK_ACTIVE  0x00000002
+#ifndef __ASSEMBLY__
     /* get_stack_bottom() must be 16-byte aligned */
 };
 
@@ -97,9 +101,16 @@ static inline struct cpu_info *get_cpu_info(void)
 #define set_processor_id(id)  do {                                      \
     struct cpu_info *ci__ = get_cpu_info();                             \
     ci__->per_cpu_offset = __per_cpu_offset[ci__->processor_id = (id)]; \
+    ci__->flags = 0;                                                    \
 } while (0)
 
-#define guest_cpu_user_regs() (&get_cpu_info()->guest_cpu_user_regs)
+#define guest_cpu_user_regs() ({                                        \
+    struct cpu_info *info = get_cpu_info();                             \
+    if ( info->flags & VCPUSTACK_ACTIVE )                               \
+        info = (struct cpu_info *)(XPTI_START(info->current_vcpu) +     \
+                                   STACK_SIZE) - 1;                     \
+    &info->guest_cpu_user_regs;                                         \
+})
 
 /*
  * Get the bottom-of-stack, as stored in the per-CPU TSS. This actually points
@@ -142,4 +153,6 @@ unsigned long get_stack_dump_bottom (unsigned long sp);
  */
 DECLARE_PER_CPU(struct vcpu *, curr_vcpu);
 
+#endif /* !__ASSEMBLY__ */
+
 #endif /* __X86_CURRENT_H__ */
xen/include/asm-x86/processor.h +6 -6
index 625f6e9f69..58e47bf6e1 100644
@@ -97,6 +97,12 @@
                           X86_EFLAGS_NT|X86_EFLAGS_DF|X86_EFLAGS_IF|    \
                           X86_EFLAGS_TF)
 
+#define IST_NONE _AC(0,UL)
+#define IST_DF   _AC(1,UL)
+#define IST_NMI  _AC(2,UL)
+#define IST_MCE  _AC(3,UL)
+#define IST_MAX  _AC(3,UL)
+
 #ifndef __ASSEMBLY__
 
 struct domain;
@@ -400,12 +406,6 @@ struct __packed __cacheline_aligned tss_struct {
     uint8_t __cacheline_filler[24];
 };
 
-#define IST_NONE 0UL
-#define IST_DF   1UL
-#define IST_NMI  2UL
-#define IST_MCE  3UL
-#define IST_MAX  3UL
-
 /* Set the interrupt stack table used by a particular interrupt
  * descriptor table entry. */
 static always_inline void set_ist(idt_entry_t *idt, unsigned long ist)
Signature · 7 lines
2.13.6


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

PATCH v3 12/17 x86: activate per-vcpu stacks in case of xpti

In reply to Juergen Gross

When scheduling a vcpu subject to xpti activate the per-vcpu stacks by loading the vcpu specific gdt and tss. When de-scheduling such a vcpu switch back to the per physical cpu gdt and tss.

Accessing the user registers on the stack is done via helpers as depending on XPTI active or not the registers are located either on the per-vcpu stack or on the default stack.

---
V3:
- moved some code to xpti.c
- fix error for HVM domains: reset MSRs of XPTI domain before setting them for HVM
- fix error for 32 bit pv domains: setup GDT in case it was reset after descheduling a XPTI domain
- reuse percpu GDT for XPTI domains by writing TSS entry (Jan Beulich)
- avoid LTRs and WRMSRs if possible (Jan Beulich)

---
 xen/arch/x86/domain.c      | 99 +++++++++++++++++++++++++++++++++++++++++++---
 xen/include/asm-x86/regs.h |  2 +
 2 files changed, 95 insertions(+), 6 deletions(-)
xen/arch/x86/domain.c +93 -6
index 6dd47bb2bb..8d6dc73881 100644
@@ -1594,24 +1594,55 @@ static inline bool need_full_gdt(const struct domain *d)
     return is_pv_domain(d) && !is_idle_domain(d);
 }
 
+/*
+ * Get address of registers on stack: this is either on the per physical cpu
+ * stack (XPTI is off) or on the per-vcpu stack (XPTI is on)
+ */
+static inline struct cpu_user_regs *get_regs_on_stack(const struct vcpu *v)
+{
+    return is_vcpu_xpti_active(v) ? v->arch.pv_vcpu.stack_regs
+                                  : &get_cpu_info()->guest_cpu_user_regs;
+}
+
+static inline void copy_user_regs_from_stack(struct vcpu *v)
+{
+    memcpy(&v->arch.user_regs, get_regs_on_stack(v), CTXT_SWITCH_STACK_BYTES);
+}
+
+static inline void copy_user_regs_to_stack(const struct vcpu *v)
+{
+    memcpy(get_regs_on_stack(v), &v->arch.user_regs, CTXT_SWITCH_STACK_BYTES);
+}
+
 static void __context_switch(void)
 {
-    struct cpu_user_regs *stack_regs = guest_cpu_user_regs();
     unsigned int          cpu = smp_processor_id();
     struct vcpu          *p = per_cpu(curr_vcpu, cpu);
     struct vcpu          *n = current;
     struct domain        *pd = p->domain, *nd = n->domain;
     struct desc_struct   *gdt;
     struct desc_ptr       gdt_desc;
+    bool                  is_pv_gdt;
 
     ASSERT(p != n);
     ASSERT(!vcpu_cpu_dirty(n));
 
     if ( !is_idle_domain(pd) )
     {
-        memcpy(&p->arch.user_regs, stack_regs, CTXT_SWITCH_STACK_BYTES);
+        copy_user_regs_from_stack(p);
         vcpu_save_fpu(p);
         pd->arch.ctxt_switch->from(p);
+        if ( is_domain_xpti_active(pd) && !is_domain_xpti_active(nd) )
+        {
+            unsigned long stub_va = this_cpu(stubs.addr);
+
+            wrmsrl(MSR_LSTAR, stub_va);
+            wrmsrl(MSR_CSTAR, stub_va + STUB_TRAMPOLINE_SIZE_PERCPU);
+            if ( boot_cpu_data.x86_vendor == X86_VENDOR_INTEL ||
+                 boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR )
+                wrmsrl(MSR_IA32_SYSENTER_ESP,
+                       (unsigned long)&get_cpu_info()->guest_cpu_user_regs.es);
+        }
     }
 
     /*
@@ -1625,7 +1656,7 @@ static void __context_switch(void)
 
     if ( !is_idle_domain(nd) )
     {
-        memcpy(stack_regs, &n->arch.user_regs, CTXT_SWITCH_STACK_BYTES);
+        copy_user_regs_to_stack(n);
         if ( cpu_has_xsave )
         {
             u64 xcr0 = n->arch.xcr0 ?: XSTATE_FP_SSE;
@@ -1655,24 +1686,80 @@ static void __context_switch(void)
                       l1e_from_pfn(mfn + i, __PAGE_HYPERVISOR_RW));
     }
 
-    if ( need_full_gdt(pd) &&
-         ((p->vcpu_id != n->vcpu_id) || !need_full_gdt(nd)) )
+    is_pv_gdt = need_full_gdt(pd);
+    if ( is_pv_gdt &&
+         ((p->vcpu_id != n->vcpu_id) || !need_full_gdt(nd) ||
+          (pd->arch.pv_domain.xpti && !is_domain_xpti_active(nd))) )
     {
         gdt_desc.limit = LAST_RESERVED_GDT_BYTE;
         gdt_desc.base  = (unsigned long)(gdt - FIRST_RESERVED_GDT_ENTRY);
 
         lgdt(&gdt_desc);
+        is_pv_gdt = false;
+
+        /*
+         * When switching from XPTI domain to non-XPTI domain or when changing
+         * vcpu_id of XPTI domains we need to switch to the per physical cpu
+         * TSS in order to avoid either unmapped stacks or stacks being in use
+         * on multiple cpus at the same time.
+         */
+        if ( pd->arch.pv_domain.xpti )
+        {
+            _set_tss_desc(gdt + TSS_ENTRY - FIRST_RESERVED_GDT_ENTRY,
+                          (unsigned long)&this_cpu(init_tss),
+                          offsetof(struct tss_struct, __cacheline_filler) - 1,
+                          SYS_DESC_tss_avail);
+            ltr(TSS_ENTRY << 3);
+            get_cpu_info()->flags &= ~VCPUSTACK_ACTIVE;
+        }
     }
 
     write_ptbase(n);
 
+    if ( is_domain_xpti_active(nd) )
+    {
+        struct cpu_info *info;
+
+        /* Don't use guest_cpu_user_regs(), might point to vcpu stack. */
+        info = (struct cpu_info *)(XPTI_START(n) + STACK_SIZE) - 1;
+        info->stack_bottom_cpu =
+                   (unsigned long)&get_cpu_info()->guest_cpu_user_regs;
+    }
+
     if ( need_full_gdt(nd) &&
-         ((p->vcpu_id != n->vcpu_id) || !need_full_gdt(pd)) )
+         (!is_pv_gdt ||
+          (nd->arch.pv_domain.xpti && !is_domain_xpti_active(pd))) )
     {
         gdt_desc.limit = LAST_RESERVED_GDT_BYTE;
         gdt_desc.base = GDT_VIRT_START(n);
 
         lgdt(&gdt_desc);
+
+        /*
+         * Either we are currently on physical cpu TSS and stacks, or we have
+         * switched vcpu_id. In both cases we need to reload TSS and MSRs with
+         * stub addresses when we enter a XPTI domain.
+         */
+        if ( nd->arch.pv_domain.xpti )
+        {
+            unsigned long stub_va = XPTI_TRAMPOLINE(n);
+
+            _set_tss_desc(gdt + TSS_ENTRY - FIRST_RESERVED_GDT_ENTRY,
+                          XPTI_TSS(n),
+                          offsetof(struct tss_struct, __cacheline_filler) - 1,
+                          SYS_DESC_tss_avail);
+
+            ltr(TSS_ENTRY << 3);
+            get_cpu_info()->flags |= VCPUSTACK_ACTIVE;
+            wrmsrl(MSR_LSTAR,
+                   stub_va + ((unsigned long)&xpti_lstar & ~PAGE_MASK));
+            wrmsrl(MSR_CSTAR,
+                   stub_va + ((unsigned long)&xpti_cstar & ~PAGE_MASK));
+            if ( boot_cpu_data.x86_vendor == X86_VENDOR_INTEL ||
+                 boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR )
+                wrmsrl(MSR_IA32_SYSENTER_ESP,
+                       (unsigned long)&guest_cpu_user_regs()->es);
+        }
     }
 
     if ( pd != nd )
xen/include/asm-x86/regs.h +2 -0
index 725a664e0a..361de4c54e 100644
@@ -7,6 +7,8 @@
 #define guest_mode(r)                                                         \
 ({                                                                            \
     unsigned long diff = (char *)guest_cpu_user_regs() - (char *)(r);         \
+    if ( diff >= STACK_SIZE )                                                 \
+        diff = (char *)&get_cpu_info()->guest_cpu_user_regs - (char *)(r);    \
     /* Frame pointer must point into current CPU stack. */                    \
     ASSERT(diff < STACK_SIZE);                                                \
     /* If not a guest frame, it must be a hypervisor frame. */                \
Signature · 7 lines
2.13.6


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

PATCH v3 13/17 x86: allocate hypervisor L4 page table for XPTI

In reply to Juergen Gross

When XPTI for a domain is switched on allocate a hypervisor L4 page table for each guest L4 page table. For performance reasons keep a cache of the last used hypervisor L4 pages with the maximum number depending on the number of vcpus of the guest.

---
 xen/arch/x86/mm.c              |   4 +
 xen/arch/x86/mm/shadow/multi.c |   3 +
 xen/arch/x86/pv/xpti.c         | 549 ++++++++++++++++++++++++++++++++++++++++-
 xen/include/asm-x86/domain.h   |   1 +
 xen/include/asm-x86/pv/mm.h    |   4 +
 5 files changed, 560 insertions(+), 1 deletion(-)
xen/arch/x86/mm.c +4 -0
index d86e07e9f8..f615204dbb 100644
@@ -504,6 +504,8 @@ void free_shared_domheap_page(struct page_info *page)
 
 void make_cr3(struct vcpu *v, mfn_t mfn)
 {
+    if ( is_vcpu_xpti_active(v) )
+        xpti_make_cr3(v, mfn_x(mfn));
     v->arch.cr3 = mfn_x(mfn) << PAGE_SHIFT;
 }
 
@@ -1807,6 +1809,8 @@ static int free_l4_table(struct page_info *page)
     if ( rc >= 0 )
     {
         atomic_dec(&d->arch.pv_domain.nr_l4_pages);
+        if ( d->arch.pv_domain.xpti )
+            xpti_free_l4(d, pfn);
         rc = 0;
     }
 
xen/arch/x86/mm/shadow/multi.c +3 -0
index a6372e3a02..2d42959f53 100644
@@ -38,6 +38,7 @@ asm(".file \"" __OBJECT_FILE__ "\"");
 #include <asm/hvm/hvm.h>
 #include <asm/hvm/cacheattr.h>
 #include <asm/mtrr.h>
+#include <asm/pv/mm.h>
 #include <asm/guest_pt.h>
 #include <public/sched.h>
 #include "private.h"
@@ -1895,6 +1896,8 @@ void sh_destroy_l4_shadow(struct domain *d, mfn_t smfn)
 
     /* Put the memory back in the pool */
     shadow_free(d, smfn);
+    if ( is_domain_xpti_active(d) )
+        xpti_free_l4(d, mfn_x(smfn));
 }
 
 void sh_destroy_l3_shadow(struct domain *d, mfn_t smfn)
xen/arch/x86/pv/xpti.c +548 -1

612 lines not on this page · raw message

xen/include/asm-x86/domain.h +1 -0
index 1a4e92481c..5d14631272 100644
@@ -508,6 +508,7 @@ struct pv_vcpu
 
     /* If XPTI is active: pointer to user regs on stack. */
     struct cpu_user_regs *stack_regs;
+    unsigned xen_cr3_shadow;    /* XPTI: index of current shadow L4 */
 };
 
 typedef enum __packed {
xen/include/asm-x86/pv/mm.h +4 -0
index 34c51bcfba..25c035988c 100644
@@ -34,6 +34,8 @@ bool pv_destroy_ldt(struct vcpu *v);
 void xpti_vcpu_destroy(struct vcpu *v);
 int xpti_domain_init(struct domain *d);
 void xpti_domain_destroy(struct domain *d);
+void xpti_make_cr3(struct vcpu *v, unsigned long mfn);
+void xpti_free_l4(struct domain *d, unsigned long mfn);
 
 static inline bool is_domain_xpti_active(const struct domain *d)
 {
@@ -69,6 +71,8 @@ static inline bool pv_destroy_ldt(struct vcpu *v)
 static inline void xpti_vcpu_init(struct vcpu *v) { }
 static inline int xpti_domain_init(struct domain *d) { return 0; }
 static inline void xpti_domain_destroy(struct domain *d) { }
+static inline void xpti_make_cr3(struct vcpu *v, unsigned long mfn) { }
+static inline void xpti_free_l4(struct domain *d, unsigned long mfn) { }
 
 static inline bool is_domain_xpti_active(const struct domain *d)
 { return false; }
Signature · 7 lines
2.13.6


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

PATCH v3 14/17 xen: add domain pointer to fill_ro_mpt() and zap_ro_mpt() functions

In reply to Juergen Gross

In order to be able to sync L4 page table modifications with XPTI we need the domain pointer in fill_ro_mpt() and zap_ro_mpt().

---
 xen/arch/x86/domain.c          | 6 +++---
 xen/arch/x86/mm.c              | 8 ++++----
 xen/arch/x86/mm/shadow/multi.c | 4 ++--
 xen/include/asm-x86/mm.h       | 4 ++--
 4 files changed, 11 insertions(+), 11 deletions(-)
xen/arch/x86/domain.c +3 -3
index 8d6dc73881..0a6a94d2e1 100644
@@ -1002,7 +1002,7 @@ int arch_set_info_guest(
                 {
                     if ( (page->u.inuse.type_info & PGT_type_mask) ==
                          PGT_l4_page_table )
-                        done = !fill_ro_mpt(_mfn(page_to_mfn(page)));
+                        done = !fill_ro_mpt(d, _mfn(page_to_mfn(page)));
 
                     page_unlock(page);
                 }
@@ -1078,7 +1078,7 @@ int arch_set_info_guest(
         case 0:
             if ( !compat && !VM_ASSIST(d, m2p_strict) &&
                  !paging_mode_refcounts(d) )
-                fill_ro_mpt(_mfn(cr3_gfn));
+                fill_ro_mpt(d, _mfn(cr3_gfn));
             break;
         default:
             if ( cr3_page == current->arch.old_guest_table )
@@ -1118,7 +1118,7 @@ int arch_set_info_guest(
                     break;
                 case 0:
                     if ( VM_ASSIST(d, m2p_strict) )
-                        zap_ro_mpt(_mfn(cr3_gfn));
+                        zap_ro_mpt(d, _mfn(cr3_gfn));
                     break;
                 }
             }
xen/arch/x86/mm.c +4 -4
index f615204dbb..16b004abe6 100644
@@ -1611,7 +1611,7 @@ void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mfn,
     }
 }
 
-bool fill_ro_mpt(mfn_t mfn)
+bool fill_ro_mpt(const struct domain *d, mfn_t mfn)
 {
     l4_pgentry_t *l4tab = map_domain_page(mfn);
     bool ret = false;
@@ -1627,7 +1627,7 @@ bool fill_ro_mpt(mfn_t mfn)
     return ret;
 }
 
-void zap_ro_mpt(mfn_t mfn)
+void zap_ro_mpt(const struct domain *d, mfn_t mfn)
 {
     l4_pgentry_t *l4tab = map_domain_page(mfn);
 
@@ -2891,7 +2891,7 @@ int new_guest_cr3(mfn_t mfn)
     pv_destroy_ldt(curr); /* Unconditional TLB flush later. */
 
     if ( !VM_ASSIST(d, m2p_strict) && !paging_mode_refcounts(d) )
-        fill_ro_mpt(mfn);
+        fill_ro_mpt(d, mfn);
     curr->arch.guest_table = pagetable_from_mfn(mfn);
     update_cr3(curr);
 
@@ -3270,7 +3270,7 @@ long do_mmuext_op(
                 }
 
                 if ( VM_ASSIST(currd, m2p_strict) )
-                    zap_ro_mpt(_mfn(op.arg1.mfn));
+                    zap_ro_mpt(currd, _mfn(op.arg1.mfn));
             }
 
             curr->arch.guest_table_user = pagetable_from_pfn(op.arg1.mfn);
xen/arch/x86/mm/shadow/multi.c +2 -2
index 2d42959f53..170163fbcf 100644
@@ -4168,10 +4168,10 @@ sh_update_cr3(struct vcpu *v, int do_locking)
         mfn_t smfn = pagetable_get_mfn(v->arch.shadow_table[0]);
 
         if ( !(v->arch.flags & TF_kernel_mode) && VM_ASSIST(d, m2p_strict) )
-            zap_ro_mpt(smfn);
+            zap_ro_mpt(d, smfn);
         else if ( (v->arch.flags & TF_kernel_mode) &&
                   !VM_ASSIST(d, m2p_strict) )
-            fill_ro_mpt(smfn);
+            fill_ro_mpt(d, smfn);
     }
 #else
 #error This should never happen
xen/include/asm-x86/mm.h +2 -2
index 3013c266fe..446d8584c0 100644
@@ -344,8 +344,8 @@ int free_page_type(struct page_info *page, unsigned long type,
 void init_xen_pae_l2_slots(l2_pgentry_t *l2t, const struct domain *d);
 void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mfn,
                        const struct domain *d, mfn_t sl4mfn, bool ro_mpt);
-bool fill_ro_mpt(mfn_t mfn);
-void zap_ro_mpt(mfn_t mfn);
+bool fill_ro_mpt(const struct domain *d, mfn_t mfn);
+void zap_ro_mpt(const struct domain *d, mfn_t mfn);
 
 bool is_iomem_page(mfn_t mfn);
 
Signature · 7 lines
2.13.6


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

PATCH v3 15/17 x86: fill XPTI shadow pages and keep them in sync with guest L4

In reply to Juergen Gross

For being able to use the XPTI shadow L4 page tables in the hypervisor fill them with the related entries of their masters and keep them in sync when updates are done by the guest.

---
 xen/arch/x86/mm.c              | 43 ++++++++++++++++++++++++++++++++++++++----
 xen/arch/x86/mm/shadow/multi.c |  2 ++
 xen/arch/x86/pv/dom0_build.c   |  3 +++
 xen/arch/x86/pv/xpti.c         | 35 ++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/pv/mm.h    |  4 ++++
 5 files changed, 83 insertions(+), 4 deletions(-)
xen/arch/x86/mm.c +39 -4
index 16b004abe6..14dc776a52 100644
@@ -1609,6 +1609,18 @@ void init_xen_l4_slots(l4_pgentry_t *l4t, mfn_t l4mfn,
                (ROOT_PAGETABLE_FIRST_XEN_SLOT + slots -
                 l4_table_offset(XEN_VIRT_START)) * sizeof(*l4t));
     }
+
+    if ( is_domain_xpti_active(d) )
+    {
+        unsigned int slot;
+
+        for ( slot = ROOT_PAGETABLE_FIRST_XEN_SLOT;
+              slot <= ROOT_PAGETABLE_LAST_XEN_SLOT;
+              slot++ )
+            xpti_update_l4(d,
+                           mfn_x(mfn_eq(sl4mfn, INVALID_MFN) ? l4mfn : sl4mfn),
+                           slot, l4t[slot]);
+    }
 }
 
 bool fill_ro_mpt(const struct domain *d, mfn_t mfn)
@@ -1621,6 +1633,9 @@ bool fill_ro_mpt(const struct domain *d, mfn_t mfn)
         l4tab[l4_table_offset(RO_MPT_VIRT_START)] =
             idle_pg_table[l4_table_offset(RO_MPT_VIRT_START)];
         ret = true;
+        if ( is_domain_xpti_active(d) )
+            xpti_update_l4(d, mfn_x(mfn), l4_table_offset(RO_MPT_VIRT_START),
+                           idle_pg_table[l4_table_offset(RO_MPT_VIRT_START)]);
     }
     unmap_domain_page(l4tab);
 
@@ -1632,6 +1647,11 @@ void zap_ro_mpt(const struct domain *d, mfn_t mfn)
     l4_pgentry_t *l4tab = map_domain_page(mfn);
 
     l4tab[l4_table_offset(RO_MPT_VIRT_START)] = l4e_empty();
+
+    if ( is_domain_xpti_active(d) )
+        xpti_update_l4(d, mfn_x(mfn), l4_table_offset(RO_MPT_VIRT_START),
+                       l4e_empty());
+
     unmap_domain_page(l4tab);
 }
 
@@ -1682,6 +1702,8 @@ static int alloc_l4_table(struct page_info *page)
         }
 
         pl4e[i] = adjust_guest_l4e(pl4e[i], d);
+        if ( is_domain_xpti_active(d) )
+            xpti_update_l4(d, pfn, i, pl4e[i]);
     }
 
     if ( rc >= 0 )
@@ -2141,6 +2163,20 @@ static int mod_l3_entry(l3_pgentry_t *pl3e,
     return rc;
 }
 
+static bool update_l4pte(l4_pgentry_t *pl4e, l4_pgentry_t ol4e,
+                         l4_pgentry_t nl4e, unsigned long pfn,
+                         struct vcpu *v, bool preserve_ad)
+{
+    bool rc;
+
+    rc = UPDATE_ENTRY(l4, pl4e, ol4e, nl4e, pfn, v, preserve_ad);
+    if ( rc && is_vcpu_xpti_active(v) &&
+         (!paging_mode_shadow(v->domain) || !paging_get_hostmode(v)) )
+        xpti_update_l4(v->domain, pfn, pgentry_ptr_to_slot(pl4e), nl4e);
+
+    return rc;
+}
+
 /* Update the L4 entry at pl4e to new value nl4e. pl4e is within frame pfn. */
 static int mod_l4_entry(l4_pgentry_t *pl4e,
                         l4_pgentry_t nl4e,
@@ -2175,7 +2211,7 @@ static int mod_l4_entry(l4_pgentry_t *pl4e,
         if ( !l4e_has_changed(ol4e, nl4e, ~FASTPATH_FLAG_WHITELIST) )
         {
             nl4e = adjust_guest_l4e(nl4e, d);
-            rc = UPDATE_ENTRY(l4, pl4e, ol4e, nl4e, pfn, vcpu, preserve_ad);
+            rc = update_l4pte(pl4e, ol4e, nl4e, pfn, vcpu, preserve_ad);
             return rc ? 0 : -EFAULT;
         }
 
@@ -2185,14 +2221,13 @@ static int mod_l4_entry(l4_pgentry_t *pl4e,
         rc = 0;
 
         nl4e = adjust_guest_l4e(nl4e, d);
-        if ( unlikely(!UPDATE_ENTRY(l4, pl4e, ol4e, nl4e, pfn, vcpu,
-                                    preserve_ad)) )
+        if ( unlikely(!update_l4pte(pl4e, ol4e, nl4e, pfn, vcpu, preserve_ad)) )
         {
             ol4e = nl4e;
             rc = -EFAULT;
         }
     }
-    else if ( unlikely(!UPDATE_ENTRY(l4, pl4e, ol4e, nl4e, pfn, vcpu,
+    else if ( unlikely(!update_l4pte(pl4e, ol4e, nl4e, pfn, vcpu,
                                      preserve_ad)) )
     {
         return -EFAULT;
xen/arch/x86/mm/shadow/multi.c +2 -0
index 170163fbcf..110a5449a6 100644
@@ -954,6 +954,8 @@ static int shadow_set_l4e(struct domain *d,
     /* Write the new entry */
     shadow_write_entries(sl4e, &new_sl4e, 1, sl4mfn);
     flags |= SHADOW_SET_CHANGED;
+    if ( is_domain_xpti_active(d) )
+        xpti_update_l4(d, mfn_x(sl4mfn), pgentry_ptr_to_slot(sl4e), new_sl4e);
 
     if ( shadow_l4e_get_flags(old_sl4e) & _PAGE_PRESENT )
     {
xen/arch/x86/pv/dom0_build.c +3 -0
index 6e7bc435ab..8ef9c87845 100644
@@ -142,6 +142,9 @@ static __init void setup_pv_physmap(struct domain *d, unsigned long pgtbl_pfn,
             pl3e = __map_domain_page(page);
             clear_page(pl3e);
             *pl4e = l4e_from_page(page, L4_PROT);
+            if ( is_domain_xpti_active(d) )
+                xpti_update_l4(d, pgtbl_pfn, l4_table_offset(vphysmap_start),
+                               *pl4e);
         } else
             pl3e = map_l3t_from_l4e(*pl4e);
 
xen/arch/x86/pv/xpti.c +35 -0
index f663fae806..da83339563 100644
@@ -357,6 +357,18 @@ static unsigned int xpti_shadow_getforce(struct xpti_domain *xd)
     return idx;
 }
 
+static void xpti_init_xen_l4(struct xpti_domain *xd, struct xpti_l4pg *l4pg)
+{
+    unsigned int i;
+    l4_pgentry_t *src, *dest;
+
+    src = map_domain_page(_mfn(l4pg->guest_mfn));
+    dest = mfn_to_virt(l4pg->xen_mfn);
+    for ( i = 0; i < L4_PAGETABLE_ENTRIES; i++ )
+        dest[i] = src[i];
+    unmap_domain_page(src);
+}
+
 static unsigned int xpti_shadow_get(struct xpti_domain *xd, unsigned long mfn)
 {
     unsigned int idx;
@@ -385,6 +397,9 @@ static unsigned int xpti_shadow_get(struct xpti_domain *xd, unsigned long mfn)
     l4pg->ref_next = l4ref->idx;
     l4ref->idx = idx;
 
+    /* Fill the shadow page table entries. */
+    xpti_init_xen_l4(xd, l4pg);
+
     return idx;
 }
 
@@ -403,6 +418,26 @@ static unsigned int xpti_shadow_activate(struct xpti_domain *xd,
     return idx;
 }
 
+void xpti_update_l4(const struct domain *d, unsigned long mfn,
+                    unsigned int slot, l4_pgentry_t e)
+{
+    struct xpti_domain *xd = d->arch.pv_domain.xpti;
+    unsigned long flags;
+    unsigned int idx;
+    l4_pgentry_t *l4;
+
+    spin_lock_irqsave(&xd->lock, flags);
+
+    idx = xpti_shadow_from_hashlist(xd, mfn);
+    if ( idx != L4_INVALID )
+    {
+        l4 = mfn_to_virt(xd->l4pg[idx].xen_mfn);
+        l4[slot] = e;
+    }
+
+    spin_unlock_irqrestore(&xd->lock, flags);
+}
+
 void xpti_make_cr3(struct vcpu *v, unsigned long mfn)
 {
     struct xpti_domain *xd = v->domain->arch.pv_domain.xpti;
xen/include/asm-x86/pv/mm.h +4 -0
index 25c035988c..8a90af1084 100644
@@ -36,6 +36,8 @@ int xpti_domain_init(struct domain *d);
 void xpti_domain_destroy(struct domain *d);
 void xpti_make_cr3(struct vcpu *v, unsigned long mfn);
 void xpti_free_l4(struct domain *d, unsigned long mfn);
+void xpti_update_l4(const struct domain *d, unsigned long mfn,
+                    unsigned int slot, l4_pgentry_t e);
 
 static inline bool is_domain_xpti_active(const struct domain *d)
 {
@@ -73,6 +75,8 @@ static inline int xpti_domain_init(struct domain *d) { return 0; }
 static inline void xpti_domain_destroy(struct domain *d) { }
 static inline void xpti_make_cr3(struct vcpu *v, unsigned long mfn) { }
 static inline void xpti_free_l4(struct domain *d, unsigned long mfn) { }
+static inline void xpti_update_l4(const struct domain *d, unsigned long mfn,
+                                  unsigned int slot, l4_pgentry_t e) { }
 
 static inline bool is_domain_xpti_active(const struct domain *d)
 { return false; }
Signature · 7 lines
2.13.6


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

PATCH v3 16/17 x86: do page table switching when entering/leaving hypervisor

In reply to Juergen Gross

For XPTI enabled domains do page table switching when entering or leaving the hypervisor. This requires both %cr3 values to be stored in the per-vcpu stack regions and adding the switching code to the macros used to switch stacks.

The hypervisor will run on the original L4 page table supplied by the guest, while the guest will use the shadow.

---
 xen/arch/x86/pv/xpti.c            | 17 ++++++++++++-----
 xen/arch/x86/traps.c              |  3 ++-
 xen/arch/x86/x86_64/asm-offsets.c |  2 ++
 xen/include/asm-x86/asm_defns.h   | 17 ++++++++++++++++-
 xen/include/asm-x86/current.h     |  4 +++-
 5 files changed, 35 insertions(+), 8 deletions(-)
xen/arch/x86/pv/xpti.c +12 -5
index da83339563..e08aa782bf 100644
@@ -441,19 +441,26 @@ void xpti_update_l4(const struct domain *d, unsigned long mfn,
 void xpti_make_cr3(struct vcpu *v, unsigned long mfn)
 {
     struct xpti_domain *xd = v->domain->arch.pv_domain.xpti;
+    struct cpu_info *cpu_info;
     unsigned long flags;
-    unsigned int idx;
+    unsigned int old, new;
+
+    cpu_info = (struct cpu_info *)v->arch.pv_vcpu.stack_regs;
 
     spin_lock_irqsave(&xd->lock, flags);
 
-    idx = v->arch.pv_vcpu.xen_cr3_shadow;
+    old = v->arch.pv_vcpu.xen_cr3_shadow;
 
     /* First activate new shadow. */
-    v->arch.pv_vcpu.xen_cr3_shadow = xpti_shadow_activate(xd, mfn);
+    new = xpti_shadow_activate(xd, mfn);
+    v->arch.pv_vcpu.xen_cr3_shadow = new;
 
     /* Deactivate old shadow if applicable. */
-    if ( idx != L4_INVALID )
-        xpti_shadow_deactivate(xd, idx);
+    if ( old != L4_INVALID )
+        xpti_shadow_deactivate(xd, old);
+
+    cpu_info->xen_cr3 = mfn << PAGE_SHIFT;
+    cpu_info->guest_cr3 = xd->l4pg[new].xen_mfn << PAGE_SHIFT;
 
     spin_unlock_irqrestore(&xd->lock, flags);
 }
xen/arch/x86/traps.c +2 -1
index 9b29014e2c..93b228dced 100644
@@ -305,9 +305,10 @@ static void show_guest_stack(struct vcpu *v, const struct cpu_user_regs *regs)
     if ( v != current )
     {
         struct vcpu *vcpu;
+        unsigned long cr3 = read_cr3();
 
         ASSERT(guest_kernel_mode(v, regs));
-        vcpu = maddr_get_owner(read_cr3()) == v->domain ? v : NULL;
+        vcpu = maddr_get_owner(cr3) == v->domain ? v : NULL;
         if ( !vcpu )
         {
             stack = do_page_walk(v, (unsigned long)stack);
xen/arch/x86/x86_64/asm-offsets.c +2 -0
index b0060be261..2855feafa3 100644
@@ -141,6 +141,8 @@ void __dummy__(void)
     OFFSET(CPUINFO_shadow_spec_ctrl, struct cpu_info, shadow_spec_ctrl);
     OFFSET(CPUINFO_use_shadow_spec_ctrl, struct cpu_info, use_shadow_spec_ctrl);
     OFFSET(CPUINFO_bti_ist_info, struct cpu_info, bti_ist_info);
+    OFFSET(CPUINFO_guest_cr3, struct cpu_info, guest_cr3);
+    OFFSET(CPUINFO_xen_cr3, struct cpu_info, xen_cr3);
     OFFSET(CPUINFO_stack_bottom_cpu, struct cpu_info, stack_bottom_cpu);
     OFFSET(CPUINFO_flags, struct cpu_info, flags);
     DEFINE(CPUINFO_sizeof, sizeof(struct cpu_info));
xen/include/asm-x86/asm_defns.h +16 -1
index f626cc6134..f69d1501fb 100644
@@ -141,6 +141,8 @@ void ret_from_intr(void);
         GET_STACK_END(ax);                                               \
         testb $ON_VCPUSTACK, STACK_CPUINFO_FIELD(flags)(%rax);           \
         jz    1f;                                                        \
+        movq  STACK_CPUINFO_FIELD(xen_cr3)(%rax), %rcx;                  \
+        mov   %rcx, %cr3;                                                \
         movq  STACK_CPUINFO_FIELD(stack_bottom_cpu)(%rax), %rsp;         \
 1:
 
@@ -148,12 +150,25 @@ void ret_from_intr(void);
         GET_STACK_END(ax);                                               \
         testb $ON_VCPUSTACK, STACK_CPUINFO_FIELD(flags)(%rax);           \
         jz    1f;                                                        \
+        movq  STACK_CPUINFO_FIELD(xen_cr3)(%rax), %rcx;                  \
+        mov   %rcx, %cr3;                                                \
         sub   $(STACK_SIZE - 1 - ist * PAGE_SIZE), %rax;                 \
         mov   %rax, %rsp;                                                \
 1:
 
 #define SWITCH_TO_VCPU_STACK                                             \
-        mov   %r12, %rsp
+        mov   %r12, %rsp;                                                \
+        GET_STACK_END(ax);                                               \
+        testb $ON_VCPUSTACK, STACK_CPUINFO_FIELD(flags)(%rax);           \
+        jz    1f;                                                        \
+        mov   %cr4, %r8;                                                 \
+        mov   %r8, %r9;                                                  \
+        and   $~X86_CR4_PGE, %r8;                                        \
+        mov   %r8, %cr4;                                                 \
+        movq  STACK_CPUINFO_FIELD(guest_cr3)(%rax), %rcx;                \
+        mov   %rcx, %cr3;                                                \
+        mov   %r9, %cr4;                                                 \
+1:
 
 #ifndef NDEBUG
 #define ASSERT_NOT_IN_ATOMIC                                             \
xen/include/asm-x86/current.h +3 -1
index e128c13a1e..82d76a3746 100644
@@ -67,7 +67,9 @@ struct cpu_info {
         };
         /* per vcpu mapping (xpti) */
         struct {
-            unsigned long v_pad[4];
+            unsigned long v_pad[2];
+            unsigned long guest_cr3;
+            unsigned long xen_cr3;
             unsigned long stack_bottom_cpu;
         };
     };
Signature · 7 lines
2.13.6


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

PATCH v3 17/17 x86: hide most hypervisor mappings in XPTI shadow page tables

In reply to Juergen Gross

Hide all but the absolute necessary hypervisor mappings in the XPTI shadow page tables. The following mappings are needed:

- guest accessible areas, e.g. the RO M2P table
- IDT, TSS, GDT
- interrupt entry stacks
- interrupt handling code

For some of those mappings we need to setup lower level page tables with just some entries populated.

---
 xen/arch/x86/pv/xpti.c             | 229 ++++++++++++++++++++++++++++++++++++-
 xen/arch/x86/traps.c               |   2 +-
 xen/arch/x86/x86_64/compat/entry.S |   4 +
 xen/arch/x86/x86_64/entry.S        |   4 +
 xen/include/asm-x86/pv/mm.h        |   5 +
 5 files changed, 241 insertions(+), 3 deletions(-)
xen/arch/x86/pv/xpti.c +227 -2
index e08aa782bf..dea34322d7 100644
@@ -19,13 +19,16 @@
  * along with this program; If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <xen/cpu.h>
 #include <xen/domain_page.h>
 #include <xen/errno.h>
 #include <xen/init.h>
 #include <xen/keyhandler.h>
 #include <xen/lib.h>
+#include <xen/notifier.h>
 #include <xen/sched.h>
 #include <asm/bitops.h>
+#include <asm/pv/mm.h>
 
 /*
  * For each L4 page table of the guest we need a shadow for the hypervisor.
@@ -118,6 +121,7 @@ struct xpti_domain {
     unsigned int unused_first; /* List of unused slots */
     spinlock_t lock;           /* Protects all shadow lists */
     struct domain *domain;
+    struct page_info *l3_shadow;
     struct tasklet tasklet;
     l1_pgentry_t **perdom_l1tab;
 #ifdef XPTI_DEBUG
@@ -140,6 +144,9 @@ static __read_mostly enum {
     XPTI_NODOM0
 } opt_xpti = XPTI_DEFAULT;
 
+static bool xpti_l3_shadow = false;
+static l3_pgentry_t *xpti_l3_shadows[11];
+
 static int parse_xpti(const char *s)
 {
     int rc = 0;
@@ -357,6 +364,34 @@ static unsigned int xpti_shadow_getforce(struct xpti_domain *xd)
     return idx;
 }
 
+static void xpti_update_l4_entry(struct xpti_domain *xd, l4_pgentry_t *dest,
+                                 l4_pgentry_t entry, unsigned int slot)
+{
+    l3_pgentry_t *l3pg;
+
+    switch ( slot )
+    {
+    case 257: /* ioremap area. */
+    case 258: /* linear page table (guest table). */
+    case 259: /* linear page table (shadow table). */
+        dest[slot] = l4e_empty();
+        break;
+    case 260: /* per-domain mappings. */
+        dest[slot] = l4e_from_page(xd->l3_shadow, __PAGE_HYPERVISOR);
+        break;
+    case 261 ... 271: /* hypervisor text and data, direct phys mapping. */
+        l3pg = xpti_l3_shadows[slot - 261];
+        dest[slot] = l3pg
+                     ? l4e_from_mfn(_mfn(virt_to_mfn(l3pg)), __PAGE_HYPERVISOR)
+                     : l4e_empty();
+        break;
+    case 256: /* read-only guest accessible m2p table. */
+    default:
+        dest[slot] = entry;
+        break;
+    }
+}
+
 static void xpti_init_xen_l4(struct xpti_domain *xd, struct xpti_l4pg *l4pg)
 {
     unsigned int i;
@@ -365,7 +400,7 @@ static void xpti_init_xen_l4(struct xpti_domain *xd, struct xpti_l4pg *l4pg)
     src = map_domain_page(_mfn(l4pg->guest_mfn));
     dest = mfn_to_virt(l4pg->xen_mfn);
     for ( i = 0; i < L4_PAGETABLE_ENTRIES; i++ )
-        dest[i] = src[i];
+        xpti_update_l4_entry(xd, dest, src[i], i);
     unmap_domain_page(src);
 }
 
@@ -432,7 +467,7 @@ void xpti_update_l4(const struct domain *d, unsigned long mfn,
     if ( idx != L4_INVALID )
     {
         l4 = mfn_to_virt(xd->l4pg[idx].xen_mfn);
-        l4[slot] = e;
+        xpti_update_l4_entry(xd, l4, e, slot);
     }
 
     spin_unlock_irqrestore(&xd->lock, flags);
@@ -550,6 +585,8 @@ void xpti_domain_destroy(struct domain *d)
         free_xenheap_page(xpti_shadow_free(xd, idx));
     }
 
+    if ( xd->l3_shadow )
+        free_domheap_page(xd->l3_shadow);
     xfree(xd->perdom_l1tab);
     xfree(xd->l4pg);
     xfree(xd->l4ref);
@@ -642,6 +679,125 @@ static int xpti_vcpu_init(struct vcpu *v)
     return rc;
 }
 
+static int xpti_add_mapping(unsigned long addr)
+{
+    unsigned int slot, flags, mapflags;
+    unsigned long mfn;
+    l3_pgentry_t *pl3e;
+    l2_pgentry_t *pl2e;
+    l1_pgentry_t *pl1e;
+
+    slot = l4_table_offset(addr);
+    pl3e = l4e_to_l3e(idle_pg_table[slot]);
+
+    slot = l3_table_offset(addr);
+    mapflags = l3e_get_flags(pl3e[slot]);
+    ASSERT(mapflags & _PAGE_PRESENT);
+    if ( mapflags & _PAGE_PSE )
+    {
+        mapflags &= ~_PAGE_PSE;
+        mfn = l3e_get_pfn(pl3e[slot]) & ~((1UL << (2 * PAGETABLE_ORDER)) - 1);
+        mfn |= PFN_DOWN(addr) & ((1UL << (2 * PAGETABLE_ORDER)) - 1);
+    }
+    else
+    {
+        pl2e = l3e_to_l2e(pl3e[slot]);
+        slot = l2_table_offset(addr);
+        mapflags = l2e_get_flags(pl2e[slot]);
+        ASSERT(mapflags & _PAGE_PRESENT);
+        if ( mapflags & _PAGE_PSE )
+        {
+            mapflags &= ~_PAGE_PSE;
+            mfn = l2e_get_pfn(pl2e[slot]) & ~((1UL << PAGETABLE_ORDER) - 1);
+            mfn |= PFN_DOWN(addr) & ((1UL << PAGETABLE_ORDER) - 1);
+        }
+        else
+        {
+            pl1e = l2e_to_l1e(pl2e[slot]);
+            slot = l1_table_offset(addr);
+            mapflags = l1e_get_flags(pl1e[slot]);
+            ASSERT(mapflags & _PAGE_PRESENT);
+            mfn = l1e_get_pfn(pl1e[slot]);
+        }
+    }
+
+    slot = l4_table_offset(addr);
+    ASSERT(slot >= 261 && slot <= 271);
+    pl3e = xpti_l3_shadows[slot - 261];
+    if ( !pl3e )
+    {
+        pl3e = alloc_xen_pagetable();
+        if ( !pl3e )
+            return -ENOMEM;
+        clear_page(pl3e);
+        xpti_l3_shadows[slot - 261] = pl3e;
+    }
+
+    slot = l3_table_offset(addr);
+    flags = l3e_get_flags(pl3e[slot]);
+    if ( !(flags & _PAGE_PRESENT) )
+    {
+        pl2e = alloc_xen_pagetable();
+        if ( !pl2e )
+            return -ENOMEM;
+        clear_page(pl2e);
+        pl3e[slot] = l3e_from_mfn(_mfn(virt_to_mfn(pl2e)), __PAGE_HYPERVISOR);
+    }
+    else
+    {
+        pl2e = l3e_to_l2e(pl3e[slot]);
+    }
+
+    slot = l2_table_offset(addr);
+    flags = l2e_get_flags(pl2e[slot]);
+    if ( !(flags & _PAGE_PRESENT) )
+    {
+        pl1e = alloc_xen_pagetable();
+        if ( !pl1e )
+            return -ENOMEM;
+        clear_page(pl1e);
+        pl2e[slot] = l2e_from_mfn(_mfn(virt_to_mfn(pl1e)), __PAGE_HYPERVISOR);
+    }
+    else
+    {
+        pl1e = l2e_to_l1e(pl2e[slot]);
+    }
+
+    slot = l1_table_offset(addr);
+    pl1e[slot] = l1e_from_mfn(_mfn(mfn), mapflags);
+
+    return 0;
+}
+
+static void xpti_rm_mapping(unsigned long addr)
+{
+    unsigned int slot, flags;
+    l3_pgentry_t *pl3e;
+    l2_pgentry_t *pl2e;
+    l1_pgentry_t *pl1e;
+
+    slot = l4_table_offset(addr);
+    ASSERT(slot >= 261 && slot <= 271);
+    pl3e = xpti_l3_shadows[slot - 261];
+    if ( !pl3e )
+        return;
+
+    slot = l3_table_offset(addr);
+    flags = l3e_get_flags(pl3e[slot]);
+    if ( !(flags & _PAGE_PRESENT) )
+        return;
+
+    pl2e = l3e_to_l2e(pl3e[slot]);
+    slot = l2_table_offset(addr);
+    flags = l2e_get_flags(pl2e[slot]);
+    if ( !(flags & _PAGE_PRESENT) )
+        return;
+
+    pl1e = l2e_to_l1e(pl2e[slot]);
+    slot = l1_table_offset(addr);
+    pl1e[slot] = l1e_empty();
+}
+
 int xpti_domain_init(struct domain *d)
 {
     bool xpti = false;
@@ -649,7 +805,9 @@ int xpti_domain_init(struct domain *d)
     struct vcpu *v;
     struct xpti_domain *xd;
     void *virt;
+    unsigned long addr;
     unsigned int i, new;
+    l3_pgentry_t *l3tab, *l3shadow;
 
     if ( !is_pv_domain(d) || is_pv_32bit_domain(d) )
         return 0;
@@ -683,6 +841,27 @@ int xpti_domain_init(struct domain *d)
     xd->lru_last = L4_INVALID;
     xd->free_first = L4_INVALID;
 
+    if ( !xpti_l3_shadow )
+    {
+        xpti_l3_shadow = true;
+
+        for_each_online_cpu ( i )
+            if ( xpti_add_mapping((unsigned long)idt_tables[i]) )
+                goto done;
+
+        for ( addr = round_pgdown((unsigned long)&xpti_map_start);
+              addr <= round_pgdown((unsigned long)&xpti_map_end - 1);
+              addr += PAGE_SIZE )
+            if ( xpti_add_mapping(addr) )
+                goto done;
+
+        for ( addr = round_pgdown((unsigned long)&xpti_map_start_compat);
+              addr <= round_pgdown((unsigned long)&xpti_map_end_compat - 1);
+              addr += PAGE_SIZE )
+            if ( xpti_add_mapping(addr) )
+                goto done;
+    }
+
     spin_lock_init(&xd->lock);
     tasklet_init(&xd->tasklet, xpti_tasklet, (unsigned long)xd);
 
@@ -725,6 +904,16 @@ int xpti_domain_init(struct domain *d)
             goto done;
     }
 
+    xd->l3_shadow = alloc_domheap_page(d, MEMF_no_owner);
+    if ( !xd->l3_shadow )
+        goto done;
+    l3tab = __map_domain_page(d->arch.perdomain_l3_pg);
+    l3shadow = __map_domain_page(xd->l3_shadow);
+    clear_page(l3shadow);
+    l3shadow[0] = l3tab[0];          /* GDT/LDT shadow mapping. */
+    l3shadow[3] = l3tab[3];          /* XPTI mappings. */
+    unmap_domain_page(l3shadow);
+    unmap_domain_page(l3tab);
     ret = 0;
 
     printk("Enabling Xen Pagetable protection (XPTI) for Domain %d\n",
@@ -801,3 +990,39 @@ static int __init xpti_key_init(void)
     return 0;
 }
 __initcall(xpti_key_init);
+
+static int xpti_cpu_callback(struct notifier_block *nfb, unsigned long action,
+                             void *hcpu)
+{
+    unsigned int cpu = (unsigned long)hcpu;
+    int rc = 0;
+
+    if ( !xpti_l3_shadow )
+        return NOTIFY_DONE;
+
+    switch ( action )
+    {
+    case CPU_DOWN_FAILED:
+    case CPU_ONLINE:
+        rc = xpti_add_mapping((unsigned long)idt_tables[cpu]);
+        break;
+    case CPU_DOWN_PREPARE:
+        xpti_rm_mapping((unsigned long)idt_tables[cpu]);
+        break;
+    default:
+        break;
+    }
+
+    return !rc ? NOTIFY_DONE : notifier_from_errno(rc);
+}
+
+static struct notifier_block xpti_cpu_nfb = {
+    .notifier_call = xpti_cpu_callback
+};
+
+static int __init xpti_presmp_init(void)
+{
+    register_cpu_notifier(&xpti_cpu_nfb);
+    return 0;
+}
+presmp_initcall(xpti_presmp_init);
xen/arch/x86/traps.c +1 -1
index 93b228dced..00cc7cd9d7 100644
@@ -102,7 +102,7 @@ DEFINE_PER_CPU_READ_MOSTLY(struct desc_struct *, gdt_table);
 DEFINE_PER_CPU_READ_MOSTLY(struct desc_struct *, compat_gdt_table);
 
 /* Master table, used by CPU0. */
-idt_entry_t idt_table[IDT_ENTRIES];
+idt_entry_t idt_table[IDT_ENTRIES] __aligned(PAGE_SIZE);
 
 /* Pointer to the IDT of every CPU. */
 idt_entry_t *idt_tables[NR_CPUS] __read_mostly;
xen/arch/x86/x86_64/compat/entry.S +4 -0
index 206bc9a05a..575a3e5d8e 100644
@@ -13,6 +13,8 @@
 #include <public/xen.h>
 #include <irq_vectors.h>
 
+ENTRY(xpti_map_start_compat)
+
 ENTRY(entry_int82)
         ASM_CLAC
         pushq $0
@@ -367,3 +369,5 @@ compat_crash_page_fault:
         jmp   .Lft14
 .previous
         _ASM_EXTABLE(.Lft14, .Lfx14)
+
+ENTRY(xpti_map_end_compat)
xen/arch/x86/x86_64/entry.S +4 -0
index 909f6eea66..d1cb355044 100644
@@ -14,6 +14,8 @@
 #include <public/xen.h>
 #include <irq_vectors.h>
 
+ENTRY(xpti_map_start)
+
 /* %rbx: struct vcpu, %r12: user_regs */
 ENTRY(switch_to_kernel)
         leaq  VCPU_trap_bounce(%rbx),%rdx
@@ -735,6 +737,8 @@ ENTRY(enable_nmis)
 GLOBAL(trap_nop)
         iretq
 
+ENTRY(xpti_map_end)
+
 /* Table of automatically generated entry points.  One per vector. */
         .section .init.rodata, "a", @progbits
 GLOBAL(autogen_entrypoints)
xen/include/asm-x86/pv/mm.h +5 -0
index 8a90af1084..36e1856b8d 100644
@@ -23,6 +23,11 @@
 
 #ifdef CONFIG_PV
 
+extern void *xpti_map_start;
+extern void *xpti_map_end;
+extern void *xpti_map_start_compat;
+extern void *xpti_map_end_compat;
+
 int pv_ro_page_fault(unsigned long addr, struct cpu_user_regs *regs);
 
 long pv_set_gdt(struct vcpu *v, unsigned long *frames, unsigned int entries);
Signature · 7 lines
2.13.6


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#19 lore raw
In reply to Juergen Gross
Quoted 7 lines · Juergen Gross

This series is available via github:

https://github.com/jgross1/xen.git xpti

Dario wants to do some performance tests for this series to compare performance with Jan's series with all optimizations posted.

And some of this is indeed ready.

So, this is again on my testbox, with 16 pCPUs and 12GB of RAM, and I used a guest with 16 vCPUs and 10GB of RAM.

I benchmarked Jan's patch *plus* all the optimizations and overhead mitigation patches he posted on xen-devel (the ones that are already in staging, and also the ones that are not yet there). That's "XPTI-Light" in the table and in the graphs. Booting this with 'xpti=false' is considered the baseline, while booting with 'xpti=true' is the actual thing we want to measure. :-)

Then I ran the same benchmarks on Juergen's branch above, enabled at boot. That's "XPYI" in the table and graphs (yes, I know, sorry for the typo!).

http://openbenchmarking.org/result/1802125-DARI-180211144
http://openbenchmarking.org/result/1802125-DARI-180211144&obr_hgv=XPTI-Light+xpti%3Dfalse&obr_nor=y&obr_hgv=XPTI-Light+xpti%3Dfalse

As far as the following benchmarks go:
- [disk] I/O benchmarks (like aio-stress, fio, iozone)
- compress/uncompress benchmarks
- sw building benchmarks
- system benchmarks (pgbench, nginx, most of the stress-ng cases)
- scheduling latency benchmarks (schbench)

the two approach are very very close. It may be said that 'XPTI-Light optimized' has, overall, still a little bit of an edge. But really, that varies from test to test, and most of the time is marginal (either way).

System-V message passing and semaphores, as well as socket activity tests, together with hackbench ones, seems to cause Juergen's XPTI serious problems, though.

With Juergen, we decided to dig this a bit more. He hypothesized that, currently, (vCPU) context switching costs are high in his solution. Therefore, I went and check (roughly) how many context switches occurs in Xen, during a few of the benchmarks.

Here's a summary.

******** stress-ng CPU ********
== XPTI

  stress-ng: info: cpu               1795.71 bogo ops/s
  sched: runs through scheduler      29822 
  sched: context switches            14391
 == XPTI-Light
  stress-ng: info: cpu               1821.60 bogo ops/s
  sched: runs through scheduler      24544 
  sched: context switches            9128

******** stress-ng Memory Copying ********
== XPTI

  stress-ng: info: memcpy            831.79 bogo ops/s
  sched: runs through scheduler      22875 
  sched: context switches            8230
 == XPTI-Light
  stress-ng: info: memcpy            827.68
  sched: runs through scheduler      23142 
  sched: context switches            8279

******** schbench ********
== XPTI
Latency percentiles (usec)

	50.0000th: 36672
	75.0000th: 79488
	90.0000th: 124032
	95.0000th: 154880
	*99.0000th: 232192
	99.5000th: 259328
	99.9000th: 332288
	min=0, max=568244
  sched: runs through scheduler      25736 
  sched: context switches            10622 
 == XPTI-Light
  Latency percentiles (usec)
	50.0000th: 37824
	75.0000th: 81024
	90.0000th: 127872
	95.0000th: 156416
	*99.0000th: 235776
	99.5000th: 271872
	99.9000th: 348672
	min=0, max=643999

sched: runs through scheduler 25604
sched: context switches 10741

******** hackbench ********
== XPTI

  Running with 4*40 (== 160) tasks   250.707 s
  sched: runs through scheduler      1322606 
  sched: context switches            1208853

== XPTI-Light
Running with 4*40 (== 160) tasks 60.961 s
sched: runs through scheduler 1680535
sched: context switches 1668358

******** stress-ng SysV Msg Passing ********
== XPTI
stress-ng: info: msg 276321.24 bogo ops/s
sched: runs through scheduler 25144
sched: context switches 10391
== XPTI-Light

  stress-ng: info: msg               1775035.18 bogo ops/s
  sched: runs through scheduler      33453 
  sched: context switches            18566

******** schbench -p *********
== XPTI
Latency percentiles (usec)

	50.0000th: 53
	75.0000th: 56
	90.0000th: 103
	95.0000th: 161
	*99.0000th: 1326
	99.5000th: 2172
	99.9000th: 4760
	min=0, max=124594
  avg worker transfer: 478.63 ops/sec 1.87KB/s
  sched: runs through scheduler      34161 
  sched: context switches            19556
 == XPTI-Light
  Latency percentiles (usec)
	50.0000th: 16
	75.0000th: 17
	90.0000th: 18
	95.0000th: 35
	*99.0000th: 258
	99.5000th: 424
	99.9000th: 1005
	min=0, max=110505

avg worker transfer: 1791.82 ops/sec 7.00KB/s
sched: runs through scheduler 41905
sched: context switches 27013

So, basically, the intuition seems to me to be confirmed. In fact, we see that until the number of context switches happening during the specific benchmark are limited to ~ below 10k, Juergen's XPTI is fine, and on par or better than Jan's XPTI-Light (see stress-ng:cpu, stress- ng:memorycopying, schbench).

Above 10k, XPTI begins to suffer; and the more context switches there are, the worse (e.g., see how bad it goes in the hackbench case).

Note that, in the stress-ng:sysvmsg case, we see that in the XPTI-Light case that there are ~20k context switches, and I believe that the fact that we only see ~10k of them in the XPTI case, is that, due to context switch being slower, the benchmark did less work in its 30s of execution.

We can have a confirmation of that by looking at the schedbench-p case, where the slowdown is evident by looking at the average data transferred by the workers.

So, that's it for now. Thoughts are welcome. :-)

...

Or, actually, that's not it! :-O In fact, right while I was writing this report, it came out on IRC that something can be done, on Juergen's XPTI series, to mitigate the performance impact a bit.

Juergen sent me a patch already, and I'm re-running the benchmarks with that applied. I'll let know how the results ends up looking like.

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Software Engineer @ SUSE https://www.suse.com/

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

#20 lore raw
Quoted 26 lines · Dario Faggioli
Quoted 7 lines · Juergen Gross

This series is available via github:

https://github.com/jgross1/xen.git xpti

Dario wants to do some performance tests for this series to compare performance with Jan's series with all optimizations posted.

And some of this is indeed ready.

So, this is again on my testbox, with 16 pCPUs and 12GB of RAM, and I used a guest with 16 vCPUs and 10GB of RAM.

I benchmarked Jan's patch *plus* all the optimizations and overhead mitigation patches he posted on xen-devel (the ones that are already in staging, and also the ones that are not yet there). That's "XPTI-Light" in the table and in the graphs. Booting this with 'xpti=false' is considered the baseline, while booting with 'xpti=true' is the actual thing we want to measure. :-)

Then I ran the same benchmarks on Juergen's branch above, enabled at boot. That's "XPYI" in the table and graphs (yes, I know, sorry for the typo!).

http://openbenchmarking.org/result/1802125-DARI-180211144
http://openbenchmarking.org/result/1802125-DARI-180211144&obr_hgv=XPTI-Light+xpti%3Dfalse&obr_nor=y&obr_hgv=XPTI-Light+xpti%3Dfalse

...

Quoted 6 lines

Or, actually, that's not it! :-O In fact, right while I was writing this report, it came out on IRC that something can be done, on Juergen's XPTI series, to mitigate the performance impact a bit.

Juergen sent me a patch already, and I'm re-running the benchmarks with that applied. I'll let know how the results ends up looking like.

It turned out the results are not basically different. So the general problem with context switches is still there (which I expected, BTW).

So I guess the really bad results with benchmarks triggering a lot of vcpu scheduling show that my approach isn't going to fly, as the most probable cause for the slow context switches are the introduced serializing instructions (LTR, WRMSRs) which can't be avoided when we want to use per-vcpu stacks.

OTOH the results of the other benchmarks showing some advantage over Jan's solution indicate there is indeed an aspect which can be improved.

Instead of preferring one approach over the other I have thought about a way to use the best parts of each solution in a combined variant. In case nobody is feeling strong to pursue my current approach further I'd like to suggest the following scheme:

- Whenever a L4 page table of the guest is in use on one physical cpu only use the L4 shadow cache of my series in order to avoid having to copy the L4 contents each time the hypervisor is left.

- As soon as a L4 page table is being activated on a second cpu fall back to use the per-cpu page table on that cpu (the cpu already using the L4 page table can continue doing so).

- Before activation of a L4 shadow page table it is modified to map the per-cpu data needed in guest mode for the local cpu only.

- Use INVPCID instead of %cr4 PGE toggling to speed up purging global TLB entries (depending on the availability of the feature, of course).

- Use the PCID feature for being able to avoid purging TLB entries which might be needed later (depending on hardware again). I expect this will help especially for cases where the guest often switches between kernel and user mode. Whether we want 3 or 4 PCID values for each guest address space has to be discussed: do we need 2 different Xen variants for guest user and guest kernel (IOW: are there any problems possible when the hypervisor is using a guest kernel's permission to access guest data when the guest was running in user mode before entering the hypervisor)?

Thoughts?

Juergen

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

#21 lore raw
Quoted 57 lines
Quoted 1 line
Quoted 1 line · On 13.02.18 at 12:36, <[email protected]> wrote:
Quoted 20 lines · Dario Faggioli
Quoted 7 lines · Juergen Gross

This series is available via github:

https://github.com/jgross1/xen.git xpti

Dario wants to do some performance tests for this series to compare performance with Jan's series with all optimizations posted.

And some of this is indeed ready.

So, this is again on my testbox, with 16 pCPUs and 12GB of RAM, and I used a guest with 16 vCPUs and 10GB of RAM.

I benchmarked Jan's patch *plus* all the optimizations and overhead mitigation patches he posted on xen-devel (the ones that are already in staging, and also the ones that are not yet there). That's "XPTI-Light" in the table and in the graphs. Booting this with 'xpti=false' is considered the baseline, while booting with 'xpti=true' is the actual thing we want to measure. :-)

Quoted 6 lines

Then I ran the same benchmarks on Juergen's branch above, enabled at boot. That's "XPYI" in the table and graphs (yes, I know, sorry for the typo!).

http://openbenchmarking.org/result/1802125-DARI-180211144

http://openbenchmarking.org/result/1802125-DARI-180211144&obr_hgv=XPTI-Light+x
pti%3Dfalse&obr_nor=y&obr_hgv=XPTI-Light+xpti%3Dfalse

...

Quoted 6 lines

Or, actually, that's not it! :-O In fact, right while I was writing this report, it came out on IRC that something can be done, on Juergen's XPTI series, to mitigate the performance impact a bit.

Juergen sent me a patch already, and I'm re-running the benchmarks with that applied. I'll let know how the results ends up looking like.

It turned out the results are not basically different. So the general problem with context switches is still there (which I expected, BTW).

So I guess the really bad results with benchmarks triggering a lot of vcpu scheduling show that my approach isn't going to fly, as the most probable cause for the slow context switches are the introduced serializing instructions (LTR, WRMSRs) which can't be avoided when we want to use per-vcpu stacks.

OTOH the results of the other benchmarks showing some advantage over Jan's solution indicate there is indeed an aspect which can be improved.

Instead of preferring one approach over the other I have thought about a way to use the best parts of each solution in a combined variant. In case nobody is feeling strong to pursue my current approach further I'd like to suggest the following scheme:

Quoted 7 lines

- Whenever a L4 page table of the guest is in use on one physical cpu only use the L4 shadow cache of my series in order to avoid having to copy the L4 contents each time the hypervisor is left.

- As soon as a L4 page table is being activated on a second cpu fall back to use the per-cpu page table on that cpu (the cpu already using the L4 page table can continue doing so).

Would the first of these CPUs continue to run on the shadow L4 in that case? If so, would there be no synchronization issues? If not, how do you envision "telling" it to move to the per-CPU L4 (which, afaict, includes knowing which vCPU / pCPU that is)?

Quoted 2 lines

- Before activation of a L4 shadow page table it is modified to map the per-cpu data needed in guest mode for the local cpu only.

I had been considering to do this in XPTI light for other purposes too (for example it might be possible to short circuit the guest system call path to get away without multiple page table switches). We really first need to settle on how much we feel is safe to expose while the guest is running. So far I've been under the impression that people actually think we should further reduce exposed pieces of code/data, rather than widen the "window".

Quoted 2 lines

- Use INVPCID instead of %cr4 PGE toggling to speed up purging global TLB entries (depending on the availability of the feature, of course).

That's something we should do independent of what XPTI model we'd like to retain long term.

Quoted 2 lines

- Use the PCID feature for being able to avoid purging TLB entries which might be needed later (depending on hardware again).

Which first of all raises the question: Does PCID (other than the U bit) prevent use of TLB entries in the wrong context? IOW is the PCID check done early (during TLB lookup) rather than late (during insn retirement)?

Jan

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

A message that is not in the archive (sent off-list or not yet received): [email protected]

#23 lore raw
Quoted 61 lines · Jan Beulich
Quoted 61 lines
Quoted 1 line
Quoted 1 line · On 13.02.18 at 12:36, <[email protected]> wrote:
Quoted 20 lines · Dario Faggioli
Quoted 7 lines · Juergen Gross

This series is available via github:

https://github.com/jgross1/xen.git xpti

Dario wants to do some performance tests for this series to compare performance with Jan's series with all optimizations posted.

And some of this is indeed ready.

So, this is again on my testbox, with 16 pCPUs and 12GB of RAM, and I used a guest with 16 vCPUs and 10GB of RAM.

I benchmarked Jan's patch *plus* all the optimizations and overhead mitigation patches he posted on xen-devel (the ones that are already in staging, and also the ones that are not yet there). That's "XPTI-Light" in the table and in the graphs. Booting this with 'xpti=false' is considered the baseline, while booting with 'xpti=true' is the actual thing we want to measure. :-)

Quoted 6 lines

Then I ran the same benchmarks on Juergen's branch above, enabled at boot. That's "XPYI" in the table and graphs (yes, I know, sorry for the typo!).

http://openbenchmarking.org/result/1802125-DARI-180211144

http://openbenchmarking.org/result/1802125-DARI-180211144&obr_hgv=XPTI-Light+x
pti%3Dfalse&obr_nor=y&obr_hgv=XPTI-Light+xpti%3Dfalse

...

Quoted 6 lines

Or, actually, that's not it! :-O In fact, right while I was writing this report, it came out on IRC that something can be done, on Juergen's XPTI series, to mitigate the performance impact a bit.

Juergen sent me a patch already, and I'm re-running the benchmarks with that applied. I'll let know how the results ends up looking like.

It turned out the results are not basically different. So the general problem with context switches is still there (which I expected, BTW).

So I guess the really bad results with benchmarks triggering a lot of vcpu scheduling show that my approach isn't going to fly, as the most probable cause for the slow context switches are the introduced serializing instructions (LTR, WRMSRs) which can't be avoided when we want to use per-vcpu stacks.

OTOH the results of the other benchmarks showing some advantage over Jan's solution indicate there is indeed an aspect which can be improved.

Instead of preferring one approach over the other I have thought about a way to use the best parts of each solution in a combined variant. In case nobody is feeling strong to pursue my current approach further I'd like to suggest the following scheme:

- Whenever a L4 page table of the guest is in use on one physical cpu only use the L4 shadow cache of my series in order to avoid having to copy the L4 contents each time the hypervisor is left.

Quoted 8 lines
Quoted 3 lines

- As soon as a L4 page table is being activated on a second cpu fall back to use the per-cpu page table on that cpu (the cpu already using the L4 page table can continue doing so).

Would the first of these CPUs continue to run on the shadow L4 in that case? If so, would there be no synchronization issues? If not, how do you envision "telling" it to move to the per-CPU L4 (which, afaict, includes knowing which vCPU / pCPU that is)?

I thought to let the CPU running on the shadow L4. This L4 already is configured for the CPU it is being used on, so we just have to avoid to activate it on a second CPU.

I don't see synchronization issues as all guest L4 modifications would be mirrored in the shadow, as done in my series already.

Quoted 10 lines
Quoted 2 lines

- Before activation of a L4 shadow page table it is modified to map the per-cpu data needed in guest mode for the local cpu only.

I had been considering to do this in XPTI light for other purposes too (for example it might be possible to short circuit the guest system call path to get away without multiple page table switches). We really first need to settle on how much we feel is safe to expose while the guest is running. So far I've been under the impression that people actually think we should further reduce exposed pieces of code/data, rather than widen the "window".

I would like to have some prepared L3 page tables for each cpu meant to be hooked into the correct shadow L4 slots. The shadow L4 should map as few hypervisor parts as possible (again like in my current series).

Quoted 5 lines
Quoted 2 lines

- Use INVPCID instead of %cr4 PGE toggling to speed up purging global TLB entries (depending on the availability of the feature, of course).

That's something we should do independent of what XPTI model we'd like to retain long term.

Right. That was just for completeness.

Quoted 7 lines
Quoted 2 lines

- Use the PCID feature for being able to avoid purging TLB entries which might be needed later (depending on hardware again).

Which first of all raises the question: Does PCID (other than the U bit) prevent use of TLB entries in the wrong context? IOW is the PCID check done early (during TLB lookup) rather than late (during insn retirement)?

We can test this easily. As Linux kernel is already using this mechanism for Meltdown mitigation I assume it is save, or the Linux kernel way to avoid Meltdown attacks wouldn't work.

Juergen

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