# Commit 66dfae0f32bfbc899c2f3446d5ee57068cb7f957
# Date 2018-05-16 12:19:10 +0100
# Author Andrew Cooper <andrew.cooper3@citrix.com>
# Committer Andrew Cooper <andrew.cooper3@citrix.com>
x86/spec_ctrl: Express Xen's choice of MSR_SPEC_CTRL value as a variable

At the moment, we have two different encodings of Xen's MSR_SPEC_CTRL value,
which is a side effect of how the Spectre series developed.  One encoding is
via an alias with the bottom bit of bti_ist_info, and can encode IBRS or not,
but not other configurations such as STIBP.

Break Xen's value out into a separate variable (in the top of stack block for
XPTI reasons) and use this instead of bti_ist_info in the IST path.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Reviewed-by: Wei Liu <wei.liu2@citrix.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>

--- a/xen/arch/x86/spec_ctrl.c
+++ b/xen/arch/x86/spec_ctrl.c
@@ -33,7 +33,7 @@ static int8_t __initdata opt_ibrs = -1;
 static bool_t __initdata opt_rsb_native = 1;
 static bool_t __initdata opt_rsb_vmexit = 1;
 bool_t __read_mostly opt_ibpb = 1;
-int8_t __read_mostly default_xen_ibrs = -1;
+int8_t __read_mostly default_xen_spec_ctrl = -1;
 uint8_t __read_mostly default_xen_rsb = 0;
 
 static int __init parse_bti(const char *s)
@@ -94,8 +94,8 @@ static void __init print_details(enum in
            "BTI mitigations: Thunk %s, Others:%s%s%s%s\n",
            thunk == THUNK_NONE      ? "N/A" :
            thunk == THUNK_RETPOLINE ? "RETPOLINE" : "?",
-           default_xen_ibrs > 0     ? " IBRS+" :
-           default_xen_ibrs == 0    ? " IBRS-" : "",
+           default_xen_spec_ctrl < 0 ? "" :
+           default_xen_spec_ctrl & SPEC_CTRL_IBRS ? " IBRS+" : " IBRS-",
            opt_ibpb                 ? " IBPB"  : "",
            default_xen_rsb & XEN_RSB_NATIVE ? " RSB_NATIVE" : "",
            default_xen_rsb & XEN_RSB_VMEXIT ? " RSB_VMEXIT" : "");
@@ -133,7 +133,7 @@ void __init init_speculation_mitigations
          * need the IBRS entry/exit logic to virtualise IBRS support for
          * guests.
          */
-        default_xen_ibrs = ibrs;
+        default_xen_spec_ctrl = ibrs ? SPEC_CTRL_IBRS : 0;
     }
 
     /*
--- a/xen/arch/x86/x86_32/asm-offsets.c
+++ b/xen/arch/x86/x86_32/asm-offsets.c
@@ -110,7 +110,7 @@ void __dummy__(void)
     OFFSET(CPUINFO_current_vcpu, struct cpu_info, current_vcpu);
     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_xen_ibrs, struct cpu_info, xen_ibrs);
+    OFFSET(CPUINFO_xen_spec_ctrl, struct cpu_info, xen_spec_ctrl);
     OFFSET(CPUINFO_xen_rsb, struct cpu_info, xen_rsb);
     DEFINE(CPUINFO_sizeof, sizeof(struct cpu_info));
     BLANK();
--- a/xen/arch/x86/x86_64/asm-offsets.c
+++ b/xen/arch/x86/x86_64/asm-offsets.c
@@ -141,7 +141,7 @@ void __dummy__(void)
     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_xen_ibrs, struct cpu_info, xen_ibrs);
+    OFFSET(CPUINFO_xen_spec_ctrl, struct cpu_info, xen_spec_ctrl);
     OFFSET(CPUINFO_xen_rsb, struct cpu_info, xen_rsb);
     DEFINE(CPUINFO_sizeof, sizeof(struct cpu_info));
     BLANK();
--- a/xen/include/asm-x86/current.h
+++ b/xen/include/asm-x86/current.h
@@ -26,7 +26,7 @@ struct cpu_info {
     /* See asm-x86/spec_ctrl_asm.h for usage. */
     unsigned int shadow_spec_ctrl;
     bool_t       use_shadow_spec_ctrl;
-    int8_t       xen_ibrs;
+    int8_t       xen_spec_ctrl;
     uint8_t      xen_rsb;
 
 #ifdef __x86_64__
--- a/xen/include/asm-x86/spec_ctrl.h
+++ b/xen/include/asm-x86/spec_ctrl.h
@@ -26,7 +26,7 @@
 void init_speculation_mitigations(void);
 
 extern bool_t opt_ibpb;
-extern int8_t default_xen_ibrs;
+extern int8_t default_xen_spec_ctrl;
 extern uint8_t default_xen_rsb;
 
 static inline void init_shadow_spec_ctrl_state(void)
@@ -34,14 +34,14 @@ static inline void init_shadow_spec_ctrl
     struct cpu_info *info = get_cpu_info();
 
     info->shadow_spec_ctrl = info->use_shadow_spec_ctrl = 0;
-    info->xen_ibrs = default_xen_ibrs;
+    info->xen_spec_ctrl = default_xen_spec_ctrl;
     info->xen_rsb = default_xen_rsb;
 }
 
 /* WARNING! `ret`, `call *`, `jmp *` not safe after this call. */
 static always_inline void spec_ctrl_enter_idle(struct cpu_info *info)
 {
-    if ( info->xen_ibrs <= 0 )
+    if ( info->xen_spec_ctrl <= 0 )
         return;
 
     /*
@@ -58,7 +58,7 @@ static always_inline void spec_ctrl_ente
 /* WARNING! `ret`, `call *`, `jmp *` not safe before this call. */
 static always_inline void spec_ctrl_exit_idle(struct cpu_info *info)
 {
-    if ( info->xen_ibrs <= 0 )
+    if ( info->xen_spec_ctrl <= 0 )
         return;
 
     /*
--- a/xen/include/asm-x86/spec_ctrl_asm.h
+++ b/xen/include/asm-x86/spec_ctrl_asm.h
@@ -139,7 +139,7 @@
  * which point we need to save the guest value before setting IBRS for Xen.
  * Unilaterally saving the guest value is shorter and faster than checking.
  */
-    cmpb $0, CPUINFO_xen_ibrs(r(sp))
+    cmpb $0, CPUINFO_xen_spec_ctrl(r(sp))
     jl .Lspec_entry_done\@
 
     mov $MSR_SPEC_CTRL, %ecx
@@ -153,7 +153,7 @@
     movb %dl, CPUINFO_use_shadow_spec_ctrl(r(sp))
 
     /* Load Xen's intended value. */
-    movzbl CPUINFO_xen_ibrs(r(sp)), %eax
+    movzbl CPUINFO_xen_spec_ctrl(r(sp)), %eax
     wrmsr
 .Lspec_entry_done\@:
 .endm
@@ -178,9 +178,9 @@
     xor %edx, %edx
 
     .if \maybexen
-        cmp %dl, STACK_CPUINFO_FIELD(xen_ibrs)(%r14)
+        cmp %dl, STACK_CPUINFO_FIELD(xen_spec_ctrl)(%r14)
     .else
-        cmp %dl, CPUINFO_xen_ibrs(r(sp))
+        cmp %dl, CPUINFO_xen_spec_ctrl(r(sp))
     .endif
     jl .Lspec_entry_done\@
 
@@ -200,9 +200,9 @@
 
     /* Load Xen's intended value. */
     .if \maybexen
-        movzbl STACK_CPUINFO_FIELD(xen_ibrs)(%r14), %eax
+        movzbl STACK_CPUINFO_FIELD(xen_spec_ctrl)(%r14), %eax
     .else
-        movzbl CPUINFO_xen_ibrs(r(sp)), %eax
+        movzbl CPUINFO_xen_spec_ctrl(r(sp)), %eax
     .endif
 #undef r14
     mov $MSR_SPEC_CTRL, %ecx
@@ -240,7 +240,7 @@
  * guest value.
  */
     xor %edx, %edx
-    cmp %dl, CPUINFO_xen_ibrs(r(sp))
+    cmp %dl, CPUINFO_xen_spec_ctrl(r(sp))
     jl .Lspec_exit_done\@
 
     /* Set up shadow value *before* enabling shadowing. */
