As we execute cpuid in the host on behalf of the guest, we have to
derive the OSXSAVE feature bit from the guest state of CR4 which
controls the availability of this while the guest is running.

Currently, if XSAVE is available and Jailhouse enables OSXSAVE for
itself in CR4, we always report it as set to the guest as well, causing
crashes or root cell applications that check for the availability via
cpuid and then run xgetbv.

Fixes: 10c6aac6eff4 ("x86: Always intercept cpuid")

Reported-by: Swaraj Dube <[email protected]>
Signed-off-by: Jan Kiszka <[email protected]>
---
 hypervisor/arch/x86/include/asm/processor.h |  1 +
 hypervisor/arch/x86/vcpu.c                  | 10 ++++++++--
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/hypervisor/arch/x86/include/asm/processor.h 
b/hypervisor/arch/x86/include/asm/processor.h
index 6c50d27..a80b288 100644
--- a/hypervisor/arch/x86/include/asm/processor.h
+++ b/hypervisor/arch/x86/include/asm/processor.h
@@ -25,6 +25,7 @@
 /* leaf 0x01, ECX */
 #define X86_FEATURE_VMX                                        (1 << 5)
 #define X86_FEATURE_XSAVE                              (1 << 26)
+#define X86_FEATURE_OSXSAVE                            (1 << 27)
 #define X86_FEATURE_HYPERVISOR                         (1 << 31)
 
 /* leaf 0x07, subleaf 0, EBX */
diff --git a/hypervisor/arch/x86/vcpu.c b/hypervisor/arch/x86/vcpu.c
index e6e2b1d..5202fb8 100644
--- a/hypervisor/arch/x86/vcpu.c
+++ b/hypervisor/arch/x86/vcpu.c
@@ -333,8 +333,14 @@ void vcpu_handle_cpuid(void)
 
                cpuid((u32 *)&guest_regs->rax, (u32 *)&guest_regs->rbx,
                      (u32 *)&guest_regs->rcx, (u32 *)&guest_regs->rdx);
-               if (function == 0x01 && this_cell() != &root_cell)
-                       guest_regs->rcx |= X86_FEATURE_HYPERVISOR;
+               if (function == 0x01) {
+                       if (this_cell() != &root_cell)
+                               guest_regs->rcx |= X86_FEATURE_HYPERVISOR;
+
+                       guest_regs->rcx &= ~X86_FEATURE_OSXSAVE;
+                       if (vcpu_vendor_get_guest_cr4() & X86_CR4_OSXSAVE)
+                               guest_regs->rcx |= X86_FEATURE_OSXSAVE;
+               }
                break;
        }
 
-- 
2.1.4

-- 
You received this message because you are subscribed to the Google Groups 
"Jailhouse" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to