Skip to contentSkip to conversation

x86/emul: Implement the STAC and CLAC instructions

PATCH In review Andrew Cooper 2 participants 2 messages Dec 12, 2016 – Dec 13, 2016

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/emul: Implement the STAC and CLAC instructions

Note that unlike most privilege restricted instructions, STAC and CLAC are documented to raise #UD rather than #GP[0], and indeed do so.

---
CC: Jan Beulich <[email protected]>

v2:
* Exclude the use of legacy prefixes. Their exclusion is undocumented but consistent with observed behaviour and the other Grp7 instructions.
* Drop the %cr4 check. Introduce a CPUID check instead.

---
 xen/arch/x86/x86_emulate/x86_emulate.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
xen/arch/x86/x86_emulate/x86_emulate.c +14 -0
index fe055cf..dfb6f47 100644
@@ -433,6 +433,7 @@ typedef union {
 #define CR4_OSXMMEXCPT (1<<10)
 #define CR4_UMIP       (1<<11)
 #define CR4_OSXSAVE    (1<<18)
+#define CR4_SMAP       (1<<21)
 
 /* EFLAGS bit definitions. */
 #define EFLG_ID   (1<<21)
@@ -1295,6 +1296,7 @@ static bool vcpu_has(
 #define vcpu_has_bmi1()        vcpu_has(         7, EBX,  3, ctxt, ops)
 #define vcpu_has_hle()         vcpu_has(         7, EBX,  4, ctxt, ops)
 #define vcpu_has_rtm()         vcpu_has(         7, EBX, 11, ctxt, ops)
+#define vcpu_has_smap()        vcpu_has(         7, EBX, 20, ctxt, ops)
 
 #define vcpu_must_have(feat) \
     generate_exception_if(!vcpu_has_##feat(), EXC_UD)
@@ -4354,6 +4356,18 @@ x86_emulate(
 
         switch( modrm )
         {
+        case 0xca: /* clac */
+        case 0xcb: /* stac */
+            vcpu_must_have(smap);
+            generate_exception_if(
+                lock_prefix || vex.pfx ||
+                (_regs.eflags & EFLG_VM) || !mode_ring0(), EXC_UD);
+
+            _regs.eflags &= ~EFLG_AC;
+            if ( modrm == 0xcb )
+                _regs.eflags |= EFLG_AC;
+            goto no_writeback;
+
 #ifdef __XEN__
         case 0xd1: /* xsetbv */
         {
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v2 1/1
#1 lore raw

xen/arch/x86/x86_emulate/x86_emulate.c typedef union { · line 436

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 12.12.16 at 18:39, <[email protected]> wrote:
xen/arch/x86/x86_emulate/x86_emulate.c +1 -0
@@ -433,6 +433,7 @@ typedef union {
 #define CR4_OSXMMEXCPT (1<<10)
 #define CR4_UMIP       (1<<11)
 #define CR4_OSXSAVE    (1<<18)
+#define CR4_SMAP       (1<<21)

I don't think this is needed anymore?

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 4364

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +6 -0
@@ -4354,6 +4356,18 @@ x86_emulate(
 
         switch( modrm )
         {
+        case 0xca: /* clac */
+        case 0xcb: /* stac */
+            vcpu_must_have(smap);
+            generate_exception_if(
+                lock_prefix || vex.pfx ||
+                (_regs.eflags & EFLG_VM) || !mode_ring0(), EXC_UD);

mode_ring0() already excludes EFLAGS.VM being set. Also please use more conventional indentation here (no need to break the line before the first argument). With all of these taken care of

Jan

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