xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Juergen Gross <jgross@suse.com>
To: xen-devel@lists.xenproject.org
Cc: Juergen Gross <jgross@suse.com>,
	andrew.cooper3@citrix.com, dfaggioli@suse.com, jbeulich@suse.com
Subject: [PATCH v3 16/17] x86: do page table switching when entering/leaving hypervisor
Date: Fri,  9 Feb 2018 15:01:50 +0100	[thread overview]
Message-ID: <20180209140151.24714-17-jgross@suse.com> (raw)
In-Reply-To: <20180209140151.24714-1-jgross@suse.com>

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.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 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(-)

diff --git a/xen/arch/x86/pv/xpti.c b/xen/arch/x86/pv/xpti.c
index da83339563..e08aa782bf 100644
--- a/xen/arch/x86/pv/xpti.c
+++ b/xen/arch/x86/pv/xpti.c
@@ -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);
 }
diff --git a/xen/arch/x86/traps.c b/xen/arch/x86/traps.c
index 9b29014e2c..93b228dced 100644
--- a/xen/arch/x86/traps.c
+++ b/xen/arch/x86/traps.c
@@ -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);
diff --git a/xen/arch/x86/x86_64/asm-offsets.c b/xen/arch/x86/x86_64/asm-offsets.c
index b0060be261..2855feafa3 100644
--- a/xen/arch/x86/x86_64/asm-offsets.c
+++ b/xen/arch/x86/x86_64/asm-offsets.c
@@ -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));
diff --git a/xen/include/asm-x86/asm_defns.h b/xen/include/asm-x86/asm_defns.h
index f626cc6134..f69d1501fb 100644
--- a/xen/include/asm-x86/asm_defns.h
+++ b/xen/include/asm-x86/asm_defns.h
@@ -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                                             \
diff --git a/xen/include/asm-x86/current.h b/xen/include/asm-x86/current.h
index e128c13a1e..82d76a3746 100644
--- a/xen/include/asm-x86/current.h
+++ b/xen/include/asm-x86/current.h
@@ -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;
         };
     };
-- 
2.13.6


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

  parent reply	other threads:[~2018-02-09 14:03 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-09 14:01 [PATCH v3 00/17] Alternative Meltdown mitigation Juergen Gross
2018-02-09 14:01 ` [PATCH v3 01/17] x86: don't use hypervisor stack size for dumping guest stacks Juergen Gross
2018-02-09 14:01 ` [PATCH v3 02/17] x86: do a revert of e871e80c38547d9faefc6604532ba3e985e65873 Juergen Gross
2018-02-13 10:14   ` Jan Beulich
2018-02-09 14:01 ` [PATCH v3 03/17] x86: revert 5784de3e2067ed73efc2fe42e62831e8ae7f46c4 Juergen Gross
2018-02-09 14:01 ` [PATCH v3 04/17] x86: don't access saved user regs via rsp in trap handlers Juergen Gross
2018-02-09 14:01 ` [PATCH v3 05/17] x86: add a xpti command line parameter Juergen Gross
2018-02-09 14:01 ` [PATCH v3 06/17] x86: allow per-domain mappings without NX bit or with specific mfn Juergen Gross
2018-02-09 14:01 ` [PATCH v3 07/17] xen/x86: split _set_tssldt_desc() into ldt and tss specific functions Juergen Gross
2018-02-09 14:01 ` [PATCH v3 08/17] x86: add support for spectre mitigation with local thunk Juergen Gross
2018-02-09 14:01 ` [PATCH v3 09/17] x86: create syscall stub for per-domain mapping Juergen Gross
2018-02-09 14:01 ` [PATCH v3 10/17] x86: allocate per-vcpu stacks for interrupt entries Juergen Gross
2018-02-09 14:01 ` [PATCH v3 11/17] x86: modify interrupt handlers to support stack switching Juergen Gross
2018-02-09 14:01 ` [PATCH v3 12/17] x86: activate per-vcpu stacks in case of xpti Juergen Gross
2018-02-09 14:01 ` [PATCH v3 13/17] x86: allocate hypervisor L4 page table for XPTI Juergen Gross
2018-02-09 14:01 ` [PATCH v3 14/17] xen: add domain pointer to fill_ro_mpt() and zap_ro_mpt() functions Juergen Gross
2018-02-09 14:01 ` [PATCH v3 15/17] x86: fill XPTI shadow pages and keep them in sync with guest L4 Juergen Gross
2018-02-09 14:01 ` Juergen Gross [this message]
2018-02-09 14:01 ` [PATCH v3 17/17] x86: hide most hypervisor mappings in XPTI shadow page tables Juergen Gross
2018-02-12 17:54 ` [PATCH v3 00/17] Alternative Meltdown mitigation Dario Faggioli
2018-02-13 11:36   ` Juergen Gross
2018-02-13 14:16     ` Jan Beulich
     [not found]     ` <5A83014E02000078001A7619@suse.com>
2018-02-13 14:29       ` Juergen Gross

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180209140151.24714-17-jgross@suse.com \
    --to=jgross@suse.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=dfaggioli@suse.com \
    --cc=jbeulich@suse.com \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).