x86: kvm: use alternatives for VMCALL vs. VMMCALL if kernel text is read-only
authorPaolo Bonzini <pbonzini@redhat.com>
Mon, 22 Sep 2014 11:17:48 +0000 (13:17 +0200)
committerBen Hutchings <ben@decadent.org.uk>
Thu, 1 Jan 2015 01:27:52 +0000 (01:27 +0000)
commit c1118b3602c2329671ad5ec8bdf8e374323d6343 upstream.

On x86_64, kernel text mappings are mapped read-only with CONFIG_DEBUG_RODATA.
In that case, KVM will fail to patch VMCALL instructions to VMMCALL
as required on AMD processors.

The failure mode is currently a divide-by-zero exception, which obviously
is a KVM bug that has to be fixed.  However, picking the right instruction
between VMCALL and VMMCALL will be faster and will help if you cannot upgrade
the hypervisor.

Reported-by: Chris Webb <chris@arachsys.com>
Tested-by: Chris Webb <chris@arachsys.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: x86@kernel.org
Acked-by: Borislav Petkov <bp@suse.de>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
[bwh: Backported to 3.2: adjust context]
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>

index a315f1c..b8a5fe5 100644 (file)
 #define X86_FEATURE_DECODEASSISTS (8*32+12) /* AMD Decode Assists support */
 #define X86_FEATURE_PAUSEFILTER (8*32+13) /* AMD filtered pause intercept */
 #define X86_FEATURE_PFTHRESHOLD (8*32+14) /* AMD pause filter threshold */
+#define X86_FEATURE_VMMCALL     ( 8*32+15) /* Prefer vmmcall to vmcall */
 /* Intel-defined CPU features, CPUID level 0x00000007:0 (ebx), word 9 */
index 734c376..9f0a680 100644 (file)
@@ -91,15 +91,21 @@ struct kvm_vcpu_pv_apf_data {
 #ifdef __KERNEL__
 #include <asm/processor.h>
+#include <asm/alternative.h>
 extern void kvmclock_init(void);
 extern int kvm_register_clock(char *txt);
-/* This instruction is vmcall.  On non-VT architectures, it will generate a
- * trap that we will then rewrite to the appropriate instruction.
+#define KVM_HYPERCALL \
+        ALTERNATIVE(".byte 0x0f,0x01,0xc1", ".byte 0x0f,0x01,0xd9", X86_FEATURE_VMMCALL)
+/* On AMD processors, vmcall will generate a trap that we will
+ * then rewrite to the appropriate instruction.
 #define KVM_HYPERCALL ".byte 0x0f,0x01,0xc1"
 /* For KVM hypercalls, a three-byte sequence of either the vmrun or the vmmrun
  * instruction.  The hypervisor may replace it with something else but only the
index f07becc..2d44a28 100644 (file)
@@ -469,6 +469,13 @@ static void __cpuinit early_init_amd(struct cpuinfo_x86 *c)
                        set_cpu_cap(c, X86_FEATURE_EXTD_APICID);
+       /*
+        * This is only needed to tell the kernel whether to use VMCALL
+        * and VMMCALL.  VMMCALL is never executed except under virt, so
+        * we can set it unconditionally.
+        */
+       set_cpu_cap(c, X86_FEATURE_VMMCALL);
 static void __cpuinit init_amd(struct cpuinfo_x86 *c)