Skip to contentSkip to conversation

x86/hvm: Add SMAP support to HVM guest

PATCH In review Feng Wu 3 participants 8 messages Apr 15, 2014 – Apr 16, 2014

lore
Share & tools
lore

Feng Wu

author
#0 lore raw

PATCH 4/6 x86/hvm: Add SMAP support to HVM guest

Intel new CPU supports SMAP (Supervisor Mode Access Prevention). SMAP prevents supervisor-mode accesses to any linear address with a valid translation for which the U/S flag (bit 2) is 1 in every paging-structure entry controlling the translation for the linear address.

This patch enable SMAP for HVM geust.

---
 xen/arch/x86/hvm/hvm.c        |  3 +++
 xen/arch/x86/mm/guest_walk.c  | 27 +++++++++++++++++++++++----
 xen/include/asm-x86/hvm/hvm.h | 13 +++++++++++++
 3 files changed, 39 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/hvm.c +3 -0
index b0da8e7..b52476d 100644
@@ -3036,6 +3036,9 @@ void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx,
         if ( (count == 0) && !cpu_has_smep )
             *ebx &= ~cpufeat_mask(X86_FEATURE_SMEP);
 
+        if ( (count == 0) && !cpu_has_smap )
+            *ebx &= ~cpufeat_mask(X86_FEATURE_SMAP);
+
         /* Don't expose MPX to hvm when VMX support is not available */
         if ( (count == 0) &&
              (!(vmx_vmexit_control & VM_EXIT_CLEAR_BNDCFGS) ||
xen/arch/x86/mm/guest_walk.c +23 -4
index 70460b6..1d5f1fc 100644
@@ -29,6 +29,7 @@
 #include <xen/sched.h>
 #include <asm/page.h>
 #include <asm/guest_pt.h>
+#include <asm/hvm/vmx/vmx.h>
 
 
 /* Flags that are needed in a pagetable entry, with the sense of NX inverted */
@@ -144,14 +145,18 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
     guest_l4e_t *l4p;
 #endif
     uint32_t gflags, mflags, iflags, rc = 0;
-    int smep;
+    int smep, smap;
     bool_t pse1G = 0, pse2M = 0;
+    unsigned long sel = 0;
+    uint64_t eflags = guest_cpu_user_regs()->eflags;
     p2m_query_t qt = P2M_ALLOC | P2M_UNSHARE;
 
     perfc_incr(guest_walk);
     memset(gw, 0, sizeof(*gw));
     gw->va = va;
 
+    __vmread(GUEST_CS_SELECTOR, &sel);
+
     /* Mandatory bits that must be set in every entry.  We invert NX and
      * the invalid bits, to calculate as if there were an "X" bit that
      * allowed access.  We will accumulate, in rc, the set of flags that
@@ -165,7 +170,21 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
      * whole walk as if it were a user-mode one and then invert the answer. */
     smep = (is_hvm_vcpu(v) && hvm_smep_enabled(v) 
             && (pfec & PFEC_insn_fetch) && !(pfec & PFEC_user_mode) );
-    if ( smep )
+
+    /*
+     * SMAP: kernel-mode data accesses from user-mode mappings should fault
+     * A fault is considered as a SMAP violation if the following
+     * conditions come ture:
+     *   - X86_CR4_SMAP is set in CR4
+     *   - An user page is accessed
+     *   - CPL = 3 or X86_EFLAGS_AC clear set)
+     *   - Page fault in kernel mode
+     */
+    smap = ( is_hvm_vcpu(v) && hvm_smap_enabled(v)
+            && !(!((sel & 3) == 3) && (eflags & X86_EFLAGS_AC))
+            && !(pfec & PFEC_user_mode) );
+
+    if ( smep || smap )
         mflags |= _PAGE_USER;
 
 #if GUEST_PAGING_LEVELS >= 3 /* PAE or 64... */
@@ -338,8 +357,8 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
 #if GUEST_PAGING_LEVELS >= 4 /* 64-bit only... */
 set_ad:
 #endif
-    /* Now re-invert the user-mode requirement for SMEP. */
-    if ( smep ) 
+    /* Now re-invert the user-mode requirement for SMEP and SMAP */
+    if ( smep || smap )
         rc ^= _PAGE_USER;
 
     /* Go back and set accessed and dirty bits only if the walk was a
xen/include/asm-x86/hvm/hvm.h +13 -0
index dcc3483..b703b93 100644
@@ -257,6 +257,8 @@ int hvm_girq_dest_2_vcpu_id(struct domain *d, uint8_t dest, uint8_t dest_mode);
     (hvm_paging_enabled(v) && ((v)->arch.hvm_vcpu.guest_cr[4] & X86_CR4_PAE))
 #define hvm_smep_enabled(v) \
     (hvm_paging_enabled(v) && ((v)->arch.hvm_vcpu.guest_cr[4] & X86_CR4_SMEP))
+#define hvm_smap_enabled(v) \
+    (hvm_paging_enabled(v) && ((v)->arch.hvm_vcpu.guest_cr[4] & X86_CR4_SMAP))
 #define hvm_nx_enabled(v) \
     (!!((v)->arch.hvm_vcpu.guest_efer & EFER_NX))
 
@@ -363,6 +365,16 @@ static inline int hvm_event_pending(struct vcpu *v)
 #define HVM_CR4_HOST_MASK (mmu_cr4_features & \
     (X86_CR4_VMXE | X86_CR4_PAE | X86_CR4_MCE))
 
+static inline bool_t hvm_cpuid_has_smap(void)
+{
+    unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;
+    unsigned int leaf = 0x7;
+
+    hvm_cpuid(leaf, &eax, &ebx, &ecx, &edx);
+
+    return !!(ebx & cpufeat_mask(X86_FEATURE_SMAP));
+}
+
 /* These bits in CR4 cannot be set by the guest. */
 #define HVM_CR4_GUEST_RESERVED_BITS(_v)                 \
     (~((unsigned long)                                  \
@@ -371,6 +383,7 @@ static inline int hvm_event_pending(struct vcpu *v)
         X86_CR4_MCE | X86_CR4_PGE | X86_CR4_PCE |       \
         X86_CR4_OSFXSR | X86_CR4_OSXMMEXCPT |           \
         (cpu_has_smep ? X86_CR4_SMEP : 0) |             \
+        (hvm_cpuid_has_smap() ? X86_CR4_SMAP : 0) |     \
         (cpu_has_fsgsbase ? X86_CR4_FSGSBASE : 0) |     \
         ((nestedhvm_enabled((_v)->domain) && cpu_has_vmx)\
                       ? X86_CR4_VMXE : 0)  |             \
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 15.04.14 at 15:02, <[email protected]> wrote:
xen/arch/x86/mm/guest_walk.c +1 -0
@@ -29,6 +29,7 @@
 #include <xen/sched.h>
 #include <asm/page.h>
 #include <asm/guest_pt.h>
+#include <asm/hvm/vmx/vmx.h>
 
 
 /* Flags that are needed in a pagetable entry, with the sense of NX inverted */

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 148

Quoted 5 lines
xen/arch/x86/mm/guest_walk.c +1 -1
@@ -144,14 +145,18 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
     guest_l4e_t *l4p;
 #endif
     uint32_t gflags, mflags, iflags, rc = 0;
-    int smep;
+    int smep, smap;

These want to be bool_t I suppose.

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 158

Quoted 10 lines
xen/arch/x86/mm/guest_walk.c +3 -0
     bool_t pse1G = 0, pse2M = 0;
+    unsigned long sel = 0;
+    uint64_t eflags = guest_cpu_user_regs()->eflags;
     p2m_query_t qt = P2M_ALLOC | P2M_UNSHARE;
 
     perfc_incr(guest_walk);
     memset(gw, 0, sizeof(*gw));
     gw->va = va;
 
+    __vmread(GUEST_CS_SELECTOR, &sel);

You're in common code here - please use the proper HVM abstraction.

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 177

Quoted 10 lines
xen/arch/x86/mm/guest_walk.c +5 -1
@@ -165,7 +170,21 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
      * whole walk as if it were a user-mode one and then invert the answer. */
     smep = (is_hvm_vcpu(v) && hvm_smep_enabled(v) 
             && (pfec & PFEC_insn_fetch) && !(pfec & PFEC_user_mode) );
-    if ( smep )
+
+    /*
+     * SMAP: kernel-mode data accesses from user-mode mappings should fault
+     * A fault is considered as a SMAP violation if the following
+     * conditions come ture:

"true"

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 179

Quoted 2 lines
xen/arch/x86/mm/guest_walk.c +2 -0
+     *   - X86_CR4_SMAP is set in CR4
+     *   - An user page is accessed

"A user page ..." afaik.

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 180

Quoted 1 line
xen/arch/x86/mm/guest_walk.c +1 -0
+     *   - CPL = 3 or X86_EFLAGS_AC clear set)

Stray closing parenthesis.

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 184

Quoted 4 lines
xen/arch/x86/mm/guest_walk.c +4 -0
+     *   - Page fault in kernel mode
+     */
+    smap = ( is_hvm_vcpu(v) && hvm_smap_enabled(v)
+            && !(!((sel & 3) == 3) && (eflags & X86_EFLAGS_AC))

!( == ) is better written as ( != ) or, as done elsewhere, ( < ).

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 187

Quoted 3 lines
xen/arch/x86/mm/guest_walk.c +3 -0
+            && !(pfec & PFEC_user_mode) );
+
+    if ( smep || smap )

Again, please fold these are far as possible (is_hvm_vcpu() and the PFEC_user_mode are common and hence should be done just once.

xen/arch/x86/mm/guest_walk.c static inline int hvm_event_pending(struct vcpu *v) · line 373

Quoted 10 lines
xen/arch/x86/mm/guest_walk.c +6 -0
@@ -363,6 +365,16 @@ static inline int hvm_event_pending(struct vcpu *v)
 #define HVM_CR4_HOST_MASK (mmu_cr4_features & \
     (X86_CR4_VMXE | X86_CR4_PAE | X86_CR4_MCE))
 
+static inline bool_t hvm_cpuid_has_smap(void)
+{
+    unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;
+    unsigned int leaf = 0x7;
+
+    hvm_cpuid(leaf, &eax, &ebx, &ecx, &edx);

Please pass NULL for all outputs you don't need.

xen/arch/x86/mm/guest_walk.c static inline int hvm_event_pending(struct vcpu *v) · line 386

Quoted 5 lines
xen/arch/x86/mm/guest_walk.c +1 -0
@@ -371,6 +383,7 @@ static inline int hvm_event_pending(struct vcpu *v)
         X86_CR4_MCE | X86_CR4_PGE | X86_CR4_PCE |       \
         X86_CR4_OSFXSR | X86_CR4_OSXMMEXCPT |           \
         (cpu_has_smep ? X86_CR4_SMEP : 0) |             \
+        (hvm_cpuid_has_smap() ? X86_CR4_SMAP : 0) |     \

What's the reason for the asymmetry with SMEP here? Also, did you verify that v == current in all call paths? And even if you did, passing v into the function and adding a respective ASSERT() would seem rather desirable.

Jan

author
#2 lore raw
Quoted 100 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Tuesday, April 15, 2014 5:26 PM
To: Wu, Feng
Cc: [email protected]; Dong, Eddie; Nakajima, Jun; [email protected]
Subject: Re: [PATCH v1 4/6] x86/hvm: Add SMAP support to HVM guest

xen/arch/x86/mm/guest_walk.c line 35

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 15.04.14 at 15:02, <[email protected]> wrote:
xen/arch/x86/mm/guest_walk.c +1 -0
@@ -29,6 +29,7 @@
 #include <xen/sched.h>
 #include <asm/page.h>
 #include <asm/guest_pt.h>
+#include <asm/hvm/vmx/vmx.h>
 
 
 /* Flags that are needed in a pagetable entry, with the sense of NX inverted

*/

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct

Quoted 1 line
xen/arch/x86/mm/guest_walk.c +0 -0
@@ -144,14 +145,18 @@ guest_walk_tables(struct vcpu *v, struct

p2m_domain *p2m,

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 148

Quoted 5 lines
xen/arch/x86/mm/guest_walk.c +1 -1
     guest_l4e_t *l4p;
 #endif
     uint32_t gflags, mflags, iflags, rc = 0;
-    int smep;
+    int smep, smap;

These want to be bool_t I suppose.

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 158

Quoted 10 lines
xen/arch/x86/mm/guest_walk.c +3 -0
     bool_t pse1G = 0, pse2M = 0;
+    unsigned long sel = 0;
+    uint64_t eflags = guest_cpu_user_regs()->eflags;
     p2m_query_t qt = P2M_ALLOC | P2M_UNSHARE;
 
     perfc_incr(guest_walk);
     memset(gw, 0, sizeof(*gw));
     gw->va = va;
 
+    __vmread(GUEST_CS_SELECTOR, &sel);

You're in common code here - please use the proper HVM abstraction.

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct

Quoted 1 line
xen/arch/x86/mm/guest_walk.c +0 -0
@@ -165,7 +170,21 @@ guest_walk_tables(struct vcpu *v, struct

p2m_domain *p2m,

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct · line 170

Quoted 1 line
xen/arch/x86/mm/guest_walk.c +0 -0
      * whole walk as if it were a user-mode one and then invert the

answer. */

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct · line 172

Quoted 2 lines
xen/arch/x86/mm/guest_walk.c +0 -0
     smep = (is_hvm_vcpu(v) && hvm_smep_enabled(v)
             && (pfec & PFEC_insn_fetch) && !(pfec &

PFEC_user_mode) );

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,

Quoted 4 lines
xen/arch/x86/mm/guest_walk.c +3 -1
-    if ( smep )
+
+    /*
+     * SMAP: kernel-mode data accesses from user-mode mappings should

fault

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 177

Quoted 2 lines
xen/arch/x86/mm/guest_walk.c +2 -0
+     * A fault is considered as a SMAP violation if the following
+     * conditions come ture:

"true"

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 179

Quoted 2 lines
xen/arch/x86/mm/guest_walk.c +2 -0
+     *   - X86_CR4_SMAP is set in CR4
+     *   - An user page is accessed

"A user page ..." afaik.

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 180

Quoted 1 line
xen/arch/x86/mm/guest_walk.c +1 -0
+     *   - CPL = 3 or X86_EFLAGS_AC clear set)

Stray closing parenthesis.

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 184

Quoted 4 lines
xen/arch/x86/mm/guest_walk.c +4 -0
+     *   - Page fault in kernel mode
+     */
+    smap = ( is_hvm_vcpu(v) && hvm_smap_enabled(v)
+            && !(!((sel & 3) == 3) && (eflags & X86_EFLAGS_AC))

!( == ) is better written as ( != ) or, as done elsewhere, ( < ).

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 187

Quoted 3 lines
xen/arch/x86/mm/guest_walk.c +3 -0
+            && !(pfec & PFEC_user_mode) );
+
+    if ( smep || smap )

Again, please fold these are far as possible (is_hvm_vcpu() and the PFEC_user_mode are common and hence should be done just once.

xen/arch/x86/mm/guest_walk.c static inline int hvm_event_pending(struct vcpu *v) · line 373

Quoted 10 lines
xen/arch/x86/mm/guest_walk.c +6 -0
@@ -363,6 +365,16 @@ static inline int hvm_event_pending(struct vcpu *v)
 #define HVM_CR4_HOST_MASK (mmu_cr4_features & \
     (X86_CR4_VMXE | X86_CR4_PAE | X86_CR4_MCE))
 
+static inline bool_t hvm_cpuid_has_smap(void)
+{
+    unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;
+    unsigned int leaf = 0x7;
+
+    hvm_cpuid(leaf, &eax, &ebx, &ecx, &edx);

Please pass NULL for all outputs you don't need.

Quoted 10 lines

xen/arch/x86/mm/guest_walk.c static inline int hvm_event_pending(struct vcpu *v) · line 386

Quoted 5 lines
xen/arch/x86/mm/guest_walk.c +1 -0
@@ -371,6 +383,7 @@ static inline int hvm_event_pending(struct vcpu *v)
         X86_CR4_MCE | X86_CR4_PGE | X86_CR4_PCE |       \
         X86_CR4_OSFXSR | X86_CR4_OSXMMEXCPT |           \
         (cpu_has_smep ? X86_CR4_SMEP : 0) |             \
+        (hvm_cpuid_has_smap() ? X86_CR4_SMAP : 0) |     \

What's the reason for the asymmetry with SMEP here? Also, did you verify that v == current in all call paths? And even if you did, passing v into the function and adding a respective ASSERT() would seem rather desirable.

My original thought is that "cpu_has_smap" reflects whether the host cpu has SMAP feature, however, here we need to check whether the VCPU of the hvm has this feature, so I used hvm_cpuid_has_smap() instead. But I thought about this logic again yesterday, seems using "cpu_has_smap" here is okay, since we always expose the SMAP feature to HVM guest if it exists on the host cpu. So I will change this to align with smep.

Quoted 2 lines

Jan

Thanks,
Feng

#3 lore raw
Quoted 14 lines
Quoted 13 lines
Quoted 1 line · On 16.04.14 at 03:49, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]

xen/include/asm-x86/hvm/hvm.h static inline int hvm_event_pending(struct vcpu *v) · line 386

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 15.04.14 at 15:02, <[email protected]> wrote:
xen/include/asm-x86/hvm/hvm.h +1 -0
@@ -371,6 +383,7 @@ static inline int hvm_event_pending(struct vcpu *v)
         X86_CR4_MCE | X86_CR4_PGE | X86_CR4_PCE |       \
         X86_CR4_OSFXSR | X86_CR4_OSXMMEXCPT |           \
         (cpu_has_smep ? X86_CR4_SMEP : 0) |             \
+        (hvm_cpuid_has_smap() ? X86_CR4_SMAP : 0) |     \

What's the reason for the asymmetry with SMEP here? Also, did you verify that v == current in all call paths? And even if you did, passing v into the function and adding a respective ASSERT() would seem rather desirable.

Quoted 8 lines

My original thought is that "cpu_has_smap" reflects whether the host cpu has SMAP feature, however, here we need to check whether the VCPU of the hvm has this feature, so I used hvm_cpuid_has_smap() instead. But I thought about
this logic again yesterday, seems using "cpu_has_smap" here is okay, since we
always expose the SMAP feature to HVM guest if it exists on the host cpu. So I will change this to align with smep.

Please indeed do so initially, even if your argumentation is slightly
wrong: Via CPUID masking the guest may not see the SMEP and/or SMAP features, and hence cpu_has_sm[ea]p isn't generally the correct qualifier. So we'd need a fixup patch on top (or, alternatively one preceding the SMAP additions, correcting the SMEP aspect here, in which case then your change could remain as is).

Jan

author
#4 lore raw
Quoted 35 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Wednesday, April 16, 2014 4:47 PM
To: Wu, Feng
Cc: [email protected]; Dong, Eddie; Nakajima, Jun; [email protected]
Subject: RE: [PATCH v1 4/6] x86/hvm: Add SMAP support to HVM guest

Quoted 4 lines
Quoted 4 lines
Quoted 1 line · On 16.04.14 at 03:49, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]

Review static inline int hvm_event_pending(struct vcpu

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 15.04.14 at 15:02, <[email protected]> wrote:
(quoted diff) +0 -0
@@ -371,6 +383,7 @@ static inline int hvm_event_pending(struct vcpu

*v)

Quoted 11 lines
Quoted 9 lines

xen/include/asm-x86/hvm/hvm.h static inline int hvm_event_pending(struct vcpu *v) · line 386

Quoted 4 lines
xen/include/asm-x86/hvm/hvm.h +1 -0
         X86_CR4_MCE | X86_CR4_PGE | X86_CR4_PCE |       \
         X86_CR4_OSFXSR | X86_CR4_OSXMMEXCPT |           \
         (cpu_has_smep ? X86_CR4_SMEP : 0) |             \
+        (hvm_cpuid_has_smap() ? X86_CR4_SMAP : 0) |     \

What's the reason for the asymmetry with SMEP here? Also, did you verify that v == current in all call paths? And even if you did, passing v into the function and adding a respective ASSERT() would seem rather desirable.

My original thought is that "cpu_has_smap" reflects whether the host cpu

has

Quoted 7 lines

SMAP feature, however, here we need to check whether the VCPU of the hvm has this feature, so I used hvm_cpuid_has_smap() instead. But I thought about
this logic again yesterday, seems using "cpu_has_smap" here is okay, since we
always expose the SMAP feature to HVM guest if it exists on the host cpu. So I will change this to align with smep.

Quoted 7 lines

Please indeed do so initially, even if your argumentation is slightly
wrong: Via CPUID masking the guest may not see the SMEP and/or SMAP features, and hence cpu_has_sm[ea]p isn't generally the correct qualifier. So we'd need a fixup patch on top (or, alternatively one preceding the SMAP additions, correcting the SMEP aspect here, in which case then your change could remain as is).

So, I need send out a patch to fix this SMEP issue, then make the SMAP patch set based on it, right?

Quoted 1 line

Jan

Thanks,
Feng

#5 lore raw
Quoted 45 lines
Quoted 21 lines
Quoted 1 line · On 16.04.14 at 10:54, <[email protected]> wrote:

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Wednesday, April 16, 2014 4:47 PM
To: Wu, Feng
Cc: [email protected]; Dong, Eddie; Nakajima, Jun; [email protected]
Subject: RE: [PATCH v1 4/6] x86/hvm: Add SMAP support to HVM guest

Quoted 4 lines
Quoted 4 lines
Quoted 1 line · On 16.04.14 at 03:49, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]

Review static inline int hvm_event_pending(struct vcpu

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 15.04.14 at 15:02, <[email protected]> wrote:
(quoted diff) +0 -0
@@ -371,6 +383,7 @@ static inline int hvm_event_pending(struct vcpu

*v)

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
xen/include/asm-x86/hvm/hvm.h +1 -0
         X86_CR4_MCE | X86_CR4_PGE | X86_CR4_PCE |       \
         X86_CR4_OSFXSR | X86_CR4_OSXMMEXCPT |           \
         (cpu_has_smep ? X86_CR4_SMEP : 0) |             \
+        (hvm_cpuid_has_smap() ? X86_CR4_SMAP : 0) |     \
Quoted 13 lines
Quoted 6 lines
Quoted 4 lines

What's the reason for the asymmetry with SMEP here? Also, did you verify that v == current in all call paths? And even if you did, passing v into the function and adding a respective ASSERT() would seem rather desirable.

My original thought is that "cpu_has_smap" reflects whether the host cpu

has

Quoted 6 lines

SMAP feature, however, here we need to check whether the VCPU of the hvm has this feature, so I used hvm_cpuid_has_smap() instead. But I thought about
this logic again yesterday, seems using "cpu_has_smap" here is okay, since we
always expose the SMAP feature to HVM guest if it exists on the host cpu.

So

Quoted 9 lines
Quoted 1 line

I will change this to align with smep.

Please indeed do so initially, even if your argumentation is slightly
wrong: Via CPUID masking the guest may not see the SMEP and/or SMAP features, and hence cpu_has_sm[ea]p isn't generally the correct qualifier. So we'd need a fixup patch on top (or, alternatively one preceding the SMAP additions, correcting the SMEP aspect here, in which case then your change could remain as is).

Quoted 3 lines

So, I need send out a patch to fix this SMEP issue, then make the SMAP patch set
based on it, right?

That would be the ideal flow, yes.

Jan

#6 lore raw
In reply to Feng Wu
Quoted 64 lines · Feng Wu

Intel new CPU supports SMAP (Supervisor Mode Access Prevention). SMAP prevents supervisor-mode accesses to any linear address with a valid translation for which the U/S flag (bit 2) is 1 in every paging-structure entry controlling the translation for the linear address.

This patch enable SMAP for HVM geust.

---
 xen/arch/x86/hvm/hvm.c        |  3 +++
 xen/arch/x86/mm/guest_walk.c  | 27 +++++++++++++++++++++++----
 xen/include/asm-x86/hvm/hvm.h | 13 +++++++++++++
 3 files changed, 39 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/hvm.c +3 -0
index b0da8e7..b52476d 100644
@@ -3036,6 +3036,9 @@ void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx,
         if ( (count == 0) && !cpu_has_smep )
             *ebx &= ~cpufeat_mask(X86_FEATURE_SMEP);
 
+        if ( (count == 0) && !cpu_has_smap )
+            *ebx &= ~cpufeat_mask(X86_FEATURE_SMAP);
+
         /* Don't expose MPX to hvm when VMX support is not available */
         if ( (count == 0) &&
              (!(vmx_vmexit_control & VM_EXIT_CLEAR_BNDCFGS) ||
xen/arch/x86/mm/guest_walk.c +6 -1
index 70460b6..1d5f1fc 100644
@@ -29,6 +29,7 @@
 #include <xen/sched.h>
 #include <asm/page.h>
 #include <asm/guest_pt.h>
+#include <asm/hvm/vmx/vmx.h>
 
 
 /* Flags that are needed in a pagetable entry, with the sense of NX inverted */
@@ -144,14 +145,18 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
     guest_l4e_t *l4p;
 #endif
     uint32_t gflags, mflags, iflags, rc = 0;
-    int smep;
+    int smep, smap;
     bool_t pse1G = 0, pse2M = 0;
+    unsigned long sel = 0;
+    uint64_t eflags = guest_cpu_user_regs()->eflags;
     p2m_query_t qt = P2M_ALLOC | P2M_UNSHARE;
 
     perfc_incr(guest_walk);
     memset(gw, 0, sizeof(*gw));
     gw->va = va;
 
+    __vmread(GUEST_CS_SELECTOR, &sel);
+
     /* Mandatory bits that must be set in every entry.  We invert NX and
      * the invalid bits, to calculate as if there were an "X" bit that
      * allowed access.  We will accumulate, in rc, the set of flags that
@@ -165,7 +170,21 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
      * whole walk as if it were a user-mode one and then invert the answer. */
     smep = (is_hvm_vcpu(v) && hvm_smep_enabled(v) 

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 180

Quoted 10 lines
xen/arch/x86/mm/guest_walk.c +8 -1
             && (pfec & PFEC_insn_fetch) && !(pfec & PFEC_user_mode) );
-    if ( smep )
+
+    /*
+     * SMAP: kernel-mode data accesses from user-mode mappings should fault
+     * A fault is considered as a SMAP violation if the following
+     * conditions come ture:
+     *   - X86_CR4_SMAP is set in CR4
+     *   - An user page is accessed
+     *   - CPL = 3 or X86_EFLAGS_AC clear set)

"X86_EFLAGS_AC clear set" ? What do you mean by this?

~Andrew

Quoted 59 lines
xen/arch/x86/mm/guest_walk.c +9 -2
+     *   - Page fault in kernel mode
+     */
+    smap = ( is_hvm_vcpu(v) && hvm_smap_enabled(v)
+            && !(!((sel & 3) == 3) && (eflags & X86_EFLAGS_AC))
+            && !(pfec & PFEC_user_mode) );
+
+    if ( smep || smap )
         mflags |= _PAGE_USER;
 
 #if GUEST_PAGING_LEVELS >= 3 /* PAE or 64... */
@@ -338,8 +357,8 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
 #if GUEST_PAGING_LEVELS >= 4 /* 64-bit only... */
 set_ad:
 #endif
-    /* Now re-invert the user-mode requirement for SMEP. */
-    if ( smep ) 
+    /* Now re-invert the user-mode requirement for SMEP and SMAP */
+    if ( smep || smap )
         rc ^= _PAGE_USER;
 
     /* Go back and set accessed and dirty bits only if the walk was a
xen/include/asm-x86/hvm/hvm.h +13 -0
index dcc3483..b703b93 100644
@@ -257,6 +257,8 @@ int hvm_girq_dest_2_vcpu_id(struct domain *d, uint8_t dest, uint8_t dest_mode);
     (hvm_paging_enabled(v) && ((v)->arch.hvm_vcpu.guest_cr[4] & X86_CR4_PAE))
 #define hvm_smep_enabled(v) \
     (hvm_paging_enabled(v) && ((v)->arch.hvm_vcpu.guest_cr[4] & X86_CR4_SMEP))
+#define hvm_smap_enabled(v) \
+    (hvm_paging_enabled(v) && ((v)->arch.hvm_vcpu.guest_cr[4] & X86_CR4_SMAP))
 #define hvm_nx_enabled(v) \
     (!!((v)->arch.hvm_vcpu.guest_efer & EFER_NX))
 
@@ -363,6 +365,16 @@ static inline int hvm_event_pending(struct vcpu *v)
 #define HVM_CR4_HOST_MASK (mmu_cr4_features & \
     (X86_CR4_VMXE | X86_CR4_PAE | X86_CR4_MCE))
 
+static inline bool_t hvm_cpuid_has_smap(void)
+{
+    unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;
+    unsigned int leaf = 0x7;
+
+    hvm_cpuid(leaf, &eax, &ebx, &ecx, &edx);
+
+    return !!(ebx & cpufeat_mask(X86_FEATURE_SMAP));
+}
+
 /* These bits in CR4 cannot be set by the guest. */
 #define HVM_CR4_GUEST_RESERVED_BITS(_v)                 \
     (~((unsigned long)                                  \
@@ -371,6 +383,7 @@ static inline int hvm_event_pending(struct vcpu *v)
         X86_CR4_MCE | X86_CR4_PGE | X86_CR4_PCE |       \
         X86_CR4_OSFXSR | X86_CR4_OSXMMEXCPT |           \
         (cpu_has_smep ? X86_CR4_SMEP : 0) |             \
+        (hvm_cpuid_has_smap() ? X86_CR4_SMAP : 0) |     \
         (cpu_has_fsgsbase ? X86_CR4_FSGSBASE : 0) |     \
         ((nestedhvm_enabled((_v)->domain) && cpu_has_vmx)\
                       ? X86_CR4_VMXE : 0)  |             \
author
#7 lore raw
Quoted 95 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: Tuesday, April 15, 2014 6:37 PM
To: Wu, Feng
Cc: [email protected]; [email protected]; [email protected]; Dong, Eddie; Nakajima, Jun
Subject: Re: [Xen-devel] [PATCH v1 4/6] x86/hvm: Add SMAP support to HVM guest

xen/arch/x86/hvm/hvm.c void hvm_cpuid(unsigned int input, unsigned int

Quoted 20 lines · Feng Wu

Intel new CPU supports SMAP (Supervisor Mode Access Prevention). SMAP prevents supervisor-mode accesses to any linear address with a valid translation for which the U/S flag (bit 2) is 1 in every paging-structure entry controlling the translation for the linear address.

This patch enable SMAP for HVM geust.

---
 xen/arch/x86/hvm/hvm.c        |  3 +++
 xen/arch/x86/mm/guest_walk.c  | 27 +++++++++++++++++++++++----
 xen/include/asm-x86/hvm/hvm.h | 13 +++++++++++++
 3 files changed, 39 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/hvm.c +0 -0
index b0da8e7..b52476d 100644
@@ -3036,6 +3036,9 @@ void hvm_cpuid(unsigned int input, unsigned int

*eax, unsigned int *ebx,

xen/arch/x86/hvm/hvm.c void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx,

Quoted 7 lines
xen/arch/x86/hvm/hvm.c +3 -0
         if ( (count == 0) && !cpu_has_smep )
             *ebx &= ~cpufeat_mask(X86_FEATURE_SMEP);
 
+        if ( (count == 0) && !cpu_has_smap )
+            *ebx &= ~cpufeat_mask(X86_FEATURE_SMAP);
+
         /* Don't expose MPX to hvm when VMX support is not available

*/

Quoted 7 lines
xen/arch/x86/hvm/hvm.c +0 -0
         if ( (count == 0) &&
              (!(vmx_vmexit_control & VM_EXIT_CLEAR_BNDCFGS) ||
xen/arch/x86/mm/guest_walk.c +0 -0
index 70460b6..1d5f1fc 100644

xen/arch/x86/mm/guest_walk.c line 35

Quoted 7 lines
xen/arch/x86/mm/guest_walk.c +1 -0
@@ -29,6 +29,7 @@
 #include <xen/sched.h>
 #include <asm/page.h>
 #include <asm/guest_pt.h>
+#include <asm/hvm/vmx/vmx.h>
 
 
 /* Flags that are needed in a pagetable entry, with the sense of NX inverted

*/

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct

Quoted 1 line
xen/arch/x86/mm/guest_walk.c +0 -0
@@ -144,14 +145,18 @@ guest_walk_tables(struct vcpu *v, struct

p2m_domain *p2m,

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct

Quoted 20 lines
xen/arch/x86/mm/guest_walk.c +5 -1
     guest_l4e_t *l4p;
 #endif
     uint32_t gflags, mflags, iflags, rc = 0;
-    int smep;
+    int smep, smap;
     bool_t pse1G = 0, pse2M = 0;
+    unsigned long sel = 0;
+    uint64_t eflags = guest_cpu_user_regs()->eflags;
     p2m_query_t qt = P2M_ALLOC | P2M_UNSHARE;
 
     perfc_incr(guest_walk);
     memset(gw, 0, sizeof(*gw));
     gw->va = va;
 
+    __vmread(GUEST_CS_SELECTOR, &sel);
+
     /* Mandatory bits that must be set in every entry.  We invert NX and
      * the invalid bits, to calculate as if there were an "X" bit that
      * allowed access.  We will accumulate, in rc, the set of flags that
@@ -165,7 +170,21 @@ guest_walk_tables(struct vcpu *v, struct

p2m_domain *p2m,

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct · line 170

Quoted 1 line
xen/arch/x86/mm/guest_walk.c +0 -0
      * whole walk as if it were a user-mode one and then invert the

answer. */

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct · line 172

Quoted 2 lines
xen/arch/x86/mm/guest_walk.c +0 -0
     smep = (is_hvm_vcpu(v) && hvm_smep_enabled(v)
             && (pfec & PFEC_insn_fetch) && !(pfec &

PFEC_user_mode) );

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,

Quoted 4 lines
xen/arch/x86/mm/guest_walk.c +3 -1
-    if ( smep )
+
+    /*
+     * SMAP: kernel-mode data accesses from user-mode mappings should

fault

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m, · line 180

Quoted 5 lines
xen/arch/x86/mm/guest_walk.c +5 -0
+     * A fault is considered as a SMAP violation if the following
+     * conditions come ture:
+     *   - X86_CR4_SMAP is set in CR4
+     *   - An user page is accessed
+     *   - CPL = 3 or X86_EFLAGS_AC clear set)
Quoted 3 lines

"X86_EFLAGS_AC clear set" ? What do you mean by this?

~Andrew

It should be "CPL = 3 or X86_EFLAGS_AC is clear", sorry for this typo.

Quoted 58 lines

xen/arch/x86/mm/guest_walk.c guest_walk_tables(struct vcpu *v, struct

Quoted 12 lines
xen/arch/x86/mm/guest_walk.c +7 -0
+     *   - Page fault in kernel mode
+     */
+    smap = ( is_hvm_vcpu(v) && hvm_smap_enabled(v)
+            && !(!((sel & 3) == 3) && (eflags & X86_EFLAGS_AC))
+            && !(pfec & PFEC_user_mode) );
+
+    if ( smep || smap )
         mflags |= _PAGE_USER;
 
 #if GUEST_PAGING_LEVELS >= 3 /* PAE or 64... */
@@ -338,8 +357,8 @@ guest_walk_tables(struct vcpu *v, struct

p2m_domain *p2m,

Review

Quoted 11 lines
xen/arch/x86/mm/guest_walk.c +2 -2
 #if GUEST_PAGING_LEVELS >= 4 /* 64-bit only... */
 set_ad:
 #endif
-    /* Now re-invert the user-mode requirement for SMEP. */
-    if ( smep )
+    /* Now re-invert the user-mode requirement for SMEP and SMAP */
+    if ( smep || smap )
         rc ^= _PAGE_USER;
 
     /* Go back and set accessed and dirty bits only if the walk was a
(quoted diff) +0 -0

b/xen/include/asm-x86/hvm/hvm.h

xen/include/asm-x86/hvm/hvm.h int hvm_girq_dest_2_vcpu_id(struct domain *d,

Quoted 4 lines

index dcc3483..b703b93 100644

xen/include/asm-x86/hvm/hvm.h +0 -0
@@ -257,6 +257,8 @@ int hvm_girq_dest_2_vcpu_id(struct domain *d,

uint8_t dest, uint8_t dest_mode);

xen/include/asm-x86/hvm/hvm.h int hvm_girq_dest_2_vcpu_id(struct domain *d, · line 257

Quoted 1 line
xen/include/asm-x86/hvm/hvm.h +0 -0
     (hvm_paging_enabled(v) && ((v)->arch.hvm_vcpu.guest_cr[4] &

X86_CR4_PAE))

xen/include/asm-x86/hvm/hvm.h int hvm_girq_dest_2_vcpu_id(struct domain *d, · line 259

Quoted 2 lines
xen/include/asm-x86/hvm/hvm.h +0 -0
 #define hvm_smep_enabled(v) \
     (hvm_paging_enabled(v) && ((v)->arch.hvm_vcpu.guest_cr[4] &

X86_CR4_SMEP))

xen/include/asm-x86/hvm/hvm.h int hvm_girq_dest_2_vcpu_id(struct domain *d, · line 261

Quoted 2 lines
xen/include/asm-x86/hvm/hvm.h +2 -0
+#define hvm_smap_enabled(v) \
+    (hvm_paging_enabled(v) && ((v)->arch.hvm_vcpu.guest_cr[4] &

X86_CR4_SMAP))

Quoted 20 lines
xen/include/asm-x86/hvm/hvm.h +10 -0
 #define hvm_nx_enabled(v) \
     (!!((v)->arch.hvm_vcpu.guest_efer & EFER_NX))
 
@@ -363,6 +365,16 @@ static inline int hvm_event_pending(struct vcpu *v)
 #define HVM_CR4_HOST_MASK (mmu_cr4_features & \
     (X86_CR4_VMXE | X86_CR4_PAE | X86_CR4_MCE))
 
+static inline bool_t hvm_cpuid_has_smap(void)
+{
+    unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;
+    unsigned int leaf = 0x7;
+
+    hvm_cpuid(leaf, &eax, &ebx, &ecx, &edx);
+
+    return !!(ebx & cpufeat_mask(X86_FEATURE_SMAP));
+}
+
 /* These bits in CR4 cannot be set by the guest. */
 #define HVM_CR4_GUEST_RESERVED_BITS(_v)                 \
     (~((unsigned long)                                  \
Quoted 8 lines

xen/include/asm-x86/hvm/hvm.h static inline int hvm_event_pending(struct vcpu *v) · line 389

Quoted 8 lines
xen/include/asm-x86/hvm/hvm.h +1 -0
@@ -371,6 +383,7 @@ static inline int hvm_event_pending(struct vcpu *v)
         X86_CR4_MCE | X86_CR4_PGE | X86_CR4_PCE |       \
         X86_CR4_OSFXSR | X86_CR4_OSXMMEXCPT |           \
         (cpu_has_smep ? X86_CR4_SMEP : 0) |             \
+        (hvm_cpuid_has_smap() ? X86_CR4_SMAP : 0) |     \
         (cpu_has_fsgsbase ? X86_CR4_FSGSBASE : 0) |     \
         ((nestedhvm_enabled((_v)->domain) && cpu_has_vmx)\
                       ? X86_CR4_VMXE : 0)  |             \

Thanks,
Feng