diff options
author | Nadav Amit <namit@cs.technion.ac.il> | 2014-07-15 10:37:46 -0400 |
---|---|---|
committer | Paolo Bonzini <pbonzini@redhat.com> | 2014-07-21 11:17:52 -0400 |
commit | 6f43ed01e87c8a8dbd8c826eaf0f714c1342c039 (patch) | |
tree | 96064816637d154889b1b4bea5e6c5dd6062c9bf | |
parent | 9a2a05b9ed618b1bb6d4cbec0c2e1f80d6636609 (diff) |
KVM: x86: DR6/7.RTM cannot be written
Haswell and newer Intel CPUs have support for RTM, and in that case DR6.RTM is
not fixed to 1 and DR7.RTM is not fixed to zero. That is not the case in the
current KVM implementation. This bug is apparent only if the MOV-DR instruction
is emulated or the host also debugs the guest.
This patch is a partial fix which enables DR6.RTM and DR7.RTM to be cleared and
set respectively. It also sets DR6.RTM upon every debug exception. Obviously,
it is not a complete fix, as debugging of RTM is still unsupported.
Signed-off-by: Nadav Amit <namit@cs.technion.ac.il>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
-rw-r--r-- | arch/x86/include/asm/kvm_host.h | 8 | ||||
-rw-r--r-- | arch/x86/kvm/cpuid.h | 8 | ||||
-rw-r--r-- | arch/x86/kvm/vmx.c | 4 | ||||
-rw-r--r-- | arch/x86/kvm/x86.c | 22 |
4 files changed, 31 insertions, 11 deletions
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index b8a4480176b9..a84eaf7ba33f 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h | |||
@@ -152,14 +152,16 @@ enum { | |||
152 | 152 | ||
153 | #define DR6_BD (1 << 13) | 153 | #define DR6_BD (1 << 13) |
154 | #define DR6_BS (1 << 14) | 154 | #define DR6_BS (1 << 14) |
155 | #define DR6_FIXED_1 0xffff0ff0 | 155 | #define DR6_RTM (1 << 16) |
156 | #define DR6_VOLATILE 0x0000e00f | 156 | #define DR6_FIXED_1 0xfffe0ff0 |
157 | #define DR6_INIT 0xffff0ff0 | ||
158 | #define DR6_VOLATILE 0x0001e00f | ||
157 | 159 | ||
158 | #define DR7_BP_EN_MASK 0x000000ff | 160 | #define DR7_BP_EN_MASK 0x000000ff |
159 | #define DR7_GE (1 << 9) | 161 | #define DR7_GE (1 << 9) |
160 | #define DR7_GD (1 << 13) | 162 | #define DR7_GD (1 << 13) |
161 | #define DR7_FIXED_1 0x00000400 | 163 | #define DR7_FIXED_1 0x00000400 |
162 | #define DR7_VOLATILE 0xffff23ff | 164 | #define DR7_VOLATILE 0xffff2bff |
163 | 165 | ||
164 | /* apic attention bits */ | 166 | /* apic attention bits */ |
165 | #define KVM_APIC_CHECK_VAPIC 0 | 167 | #define KVM_APIC_CHECK_VAPIC 0 |
diff --git a/arch/x86/kvm/cpuid.h b/arch/x86/kvm/cpuid.h index f9087315e0cd..a5380590ab0e 100644 --- a/arch/x86/kvm/cpuid.h +++ b/arch/x86/kvm/cpuid.h | |||
@@ -95,4 +95,12 @@ static inline bool guest_cpuid_has_gbpages(struct kvm_vcpu *vcpu) | |||
95 | best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); | 95 | best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); |
96 | return best && (best->edx & bit(X86_FEATURE_GBPAGES)); | 96 | return best && (best->edx & bit(X86_FEATURE_GBPAGES)); |
97 | } | 97 | } |
98 | |||
99 | static inline bool guest_cpuid_has_rtm(struct kvm_vcpu *vcpu) | ||
100 | { | ||
101 | struct kvm_cpuid_entry2 *best; | ||
102 | |||
103 | best = kvm_find_cpuid_entry(vcpu, 7, 0); | ||
104 | return best && (best->ebx & bit(X86_FEATURE_RTM)); | ||
105 | } | ||
98 | #endif | 106 | #endif |
diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index 3300f4f2da48..fd24f68378a7 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c | |||
@@ -4892,7 +4892,7 @@ static int handle_exception(struct kvm_vcpu *vcpu) | |||
4892 | if (!(vcpu->guest_debug & | 4892 | if (!(vcpu->guest_debug & |
4893 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { | 4893 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { |
4894 | vcpu->arch.dr6 &= ~15; | 4894 | vcpu->arch.dr6 &= ~15; |
4895 | vcpu->arch.dr6 |= dr6; | 4895 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
4896 | if (!(dr6 & ~DR6_RESERVED)) /* icebp */ | 4896 | if (!(dr6 & ~DR6_RESERVED)) /* icebp */ |
4897 | skip_emulated_instruction(vcpu); | 4897 | skip_emulated_instruction(vcpu); |
4898 | 4898 | ||
@@ -5151,7 +5151,7 @@ static int handle_dr(struct kvm_vcpu *vcpu) | |||
5151 | return 0; | 5151 | return 0; |
5152 | } else { | 5152 | } else { |
5153 | vcpu->arch.dr7 &= ~DR7_GD; | 5153 | vcpu->arch.dr7 &= ~DR7_GD; |
5154 | vcpu->arch.dr6 |= DR6_BD; | 5154 | vcpu->arch.dr6 |= DR6_BD | DR6_RTM; |
5155 | vmcs_writel(GUEST_DR7, vcpu->arch.dr7); | 5155 | vmcs_writel(GUEST_DR7, vcpu->arch.dr7); |
5156 | kvm_queue_exception(vcpu, DB_VECTOR); | 5156 | kvm_queue_exception(vcpu, DB_VECTOR); |
5157 | return 1; | 5157 | return 1; |
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index cc4f65f39b87..08c48a3c7c3a 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c | |||
@@ -759,6 +759,15 @@ static void kvm_update_dr7(struct kvm_vcpu *vcpu) | |||
759 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED; | 759 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED; |
760 | } | 760 | } |
761 | 761 | ||
762 | static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu) | ||
763 | { | ||
764 | u64 fixed = DR6_FIXED_1; | ||
765 | |||
766 | if (!guest_cpuid_has_rtm(vcpu)) | ||
767 | fixed |= DR6_RTM; | ||
768 | return fixed; | ||
769 | } | ||
770 | |||
762 | static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val) | 771 | static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val) |
763 | { | 772 | { |
764 | switch (dr) { | 773 | switch (dr) { |
@@ -774,7 +783,7 @@ static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val) | |||
774 | case 6: | 783 | case 6: |
775 | if (val & 0xffffffff00000000ULL) | 784 | if (val & 0xffffffff00000000ULL) |
776 | return -1; /* #GP */ | 785 | return -1; /* #GP */ |
777 | vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1; | 786 | vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu); |
778 | kvm_update_dr6(vcpu); | 787 | kvm_update_dr6(vcpu); |
779 | break; | 788 | break; |
780 | case 5: | 789 | case 5: |
@@ -5115,7 +5124,8 @@ static void kvm_vcpu_check_singlestep(struct kvm_vcpu *vcpu, unsigned long rflag | |||
5115 | */ | 5124 | */ |
5116 | if (unlikely(rflags & X86_EFLAGS_TF)) { | 5125 | if (unlikely(rflags & X86_EFLAGS_TF)) { |
5117 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { | 5126 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { |
5118 | kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1; | 5127 | kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | |
5128 | DR6_RTM; | ||
5119 | kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip; | 5129 | kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip; |
5120 | kvm_run->debug.arch.exception = DB_VECTOR; | 5130 | kvm_run->debug.arch.exception = DB_VECTOR; |
5121 | kvm_run->exit_reason = KVM_EXIT_DEBUG; | 5131 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
@@ -5128,7 +5138,7 @@ static void kvm_vcpu_check_singlestep(struct kvm_vcpu *vcpu, unsigned long rflag | |||
5128 | * cleared by the processor". | 5138 | * cleared by the processor". |
5129 | */ | 5139 | */ |
5130 | vcpu->arch.dr6 &= ~15; | 5140 | vcpu->arch.dr6 &= ~15; |
5131 | vcpu->arch.dr6 |= DR6_BS; | 5141 | vcpu->arch.dr6 |= DR6_BS | DR6_RTM; |
5132 | kvm_queue_exception(vcpu, DB_VECTOR); | 5142 | kvm_queue_exception(vcpu, DB_VECTOR); |
5133 | } | 5143 | } |
5134 | } | 5144 | } |
@@ -5147,7 +5157,7 @@ static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r) | |||
5147 | vcpu->arch.eff_db); | 5157 | vcpu->arch.eff_db); |
5148 | 5158 | ||
5149 | if (dr6 != 0) { | 5159 | if (dr6 != 0) { |
5150 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; | 5160 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM; |
5151 | kvm_run->debug.arch.pc = kvm_rip_read(vcpu) + | 5161 | kvm_run->debug.arch.pc = kvm_rip_read(vcpu) + |
5152 | get_segment_base(vcpu, VCPU_SREG_CS); | 5162 | get_segment_base(vcpu, VCPU_SREG_CS); |
5153 | 5163 | ||
@@ -5166,7 +5176,7 @@ static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r) | |||
5166 | 5176 | ||
5167 | if (dr6 != 0) { | 5177 | if (dr6 != 0) { |
5168 | vcpu->arch.dr6 &= ~15; | 5178 | vcpu->arch.dr6 &= ~15; |
5169 | vcpu->arch.dr6 |= dr6; | 5179 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
5170 | kvm_queue_exception(vcpu, DB_VECTOR); | 5180 | kvm_queue_exception(vcpu, DB_VECTOR); |
5171 | *r = EMULATE_DONE; | 5181 | *r = EMULATE_DONE; |
5172 | return true; | 5182 | return true; |
@@ -6866,7 +6876,7 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu) | |||
6866 | kvm_clear_exception_queue(vcpu); | 6876 | kvm_clear_exception_queue(vcpu); |
6867 | 6877 | ||
6868 | memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db)); | 6878 | memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db)); |
6869 | vcpu->arch.dr6 = DR6_FIXED_1; | 6879 | vcpu->arch.dr6 = DR6_INIT; |
6870 | kvm_update_dr6(vcpu); | 6880 | kvm_update_dr6(vcpu); |
6871 | vcpu->arch.dr7 = DR7_FIXED_1; | 6881 | vcpu->arch.dr7 = DR7_FIXED_1; |
6872 | kvm_update_dr7(vcpu); | 6882 | kvm_update_dr7(vcpu); |