2015-04-27 13:12:15

by Radim Krčmář

[permalink] [raw]
Subject: [PATCH] KVM: x86: fix initial PAT value

PAT should be 0007_0406_0007_0406h on RESET and not modified on INIT.
VMX used a wrong value (host's PAT) and while SVM used the right one,
it never got to arch.pat.

This is not an issue with QEMU as it will force the correct value.

Signed-off-by: Radim Krčmář <[email protected]>
---
arch/x86/kvm/svm.c | 2 +-
arch/x86/kvm/vmx.c | 12 ++----------
arch/x86/kvm/x86.c | 2 ++
arch/x86/kvm/x86.h | 2 ++
4 files changed, 7 insertions(+), 11 deletions(-)

diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c
index ce741b8650f6..9aceb4a8c147 100644
--- a/arch/x86/kvm/svm.c
+++ b/arch/x86/kvm/svm.c
@@ -1176,7 +1176,7 @@ static void init_vmcb(struct vcpu_svm *svm)
clr_exception_intercept(svm, PF_VECTOR);
clr_cr_intercept(svm, INTERCEPT_CR3_READ);
clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
- save->g_pat = 0x0007040600070406ULL;
+ save->g_pat = svm->vcpu.arch.pat;
save->cr3 = 0;
save->cr4 = 0;
}
diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
index f7b61687bd79..62459382d305 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -4667,16 +4667,8 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));

- if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
- u32 msr_low, msr_high;
- u64 host_pat;
- rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
- host_pat = msr_low | ((u64) msr_high << 32);
- /* Write the default value follow host pat */
- vmcs_write64(GUEST_IA32_PAT, host_pat);
- /* Keep arch.pat sync with GUEST_IA32_PAT */
- vmx->vcpu.arch.pat = host_pat;
- }
+ if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
+ vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);

for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
u32 index = vmx_msr_index[i];
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index c73efcd03e29..a20898f9e43a 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -7363,6 +7363,8 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)

vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);

+ vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
+
kvm_async_pf_hash_reset(vcpu);
kvm_pmu_init(vcpu);

diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h
index f5fef1868096..01a1d011e073 100644
--- a/arch/x86/kvm/x86.h
+++ b/arch/x86/kvm/x86.h
@@ -4,6 +4,8 @@
#include <linux/kvm_host.h>
#include "kvm_cache_regs.h"

+#define MSR_IA32_CR_PAT_DEFAULT 0x0007040600070406ULL
+
static inline void kvm_clear_exception_queue(struct kvm_vcpu *vcpu)
{
vcpu->arch.exception.pending = false;
--
2.3.6


2015-04-27 13:37:53

by Paolo Bonzini

[permalink] [raw]
Subject: Re: [PATCH] KVM: x86: fix initial PAT value



On 27/04/2015 15:11, Radim Krčmář wrote:
> PAT should be 0007_0406_0007_0406h on RESET and not modified on INIT.
> VMX used a wrong value (host's PAT) and while SVM used the right one,
> it never got to arch.pat.
>
> This is not an issue with QEMU as it will force the correct value.
>
> Signed-off-by: Radim Krčmář <[email protected]>
> ---
> arch/x86/kvm/svm.c | 2 +-
> arch/x86/kvm/vmx.c | 12 ++----------
> arch/x86/kvm/x86.c | 2 ++
> arch/x86/kvm/x86.h | 2 ++
> 4 files changed, 7 insertions(+), 11 deletions(-)
>
> diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c
> index ce741b8650f6..9aceb4a8c147 100644
> --- a/arch/x86/kvm/svm.c
> +++ b/arch/x86/kvm/svm.c
> @@ -1176,7 +1176,7 @@ static void init_vmcb(struct vcpu_svm *svm)
> clr_exception_intercept(svm, PF_VECTOR);
> clr_cr_intercept(svm, INTERCEPT_CR3_READ);
> clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
> - save->g_pat = 0x0007040600070406ULL;
> + save->g_pat = svm->vcpu.arch.pat;
> save->cr3 = 0;
> save->cr4 = 0;
> }
> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
> index f7b61687bd79..62459382d305 100644
> --- a/arch/x86/kvm/vmx.c
> +++ b/arch/x86/kvm/vmx.c
> @@ -4667,16 +4667,8 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
> vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
> vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
>
> - if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
> - u32 msr_low, msr_high;
> - u64 host_pat;
> - rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
> - host_pat = msr_low | ((u64) msr_high << 32);
> - /* Write the default value follow host pat */
> - vmcs_write64(GUEST_IA32_PAT, host_pat);
> - /* Keep arch.pat sync with GUEST_IA32_PAT */
> - vmx->vcpu.arch.pat = host_pat;
> - }
> + if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
> + vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
>
> for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
> u32 index = vmx_msr_index[i];
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index c73efcd03e29..a20898f9e43a 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -7363,6 +7363,8 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
>
> vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
>
> + vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
> +
> kvm_async_pf_hash_reset(vcpu);
> kvm_pmu_init(vcpu);
>
> diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h
> index f5fef1868096..01a1d011e073 100644
> --- a/arch/x86/kvm/x86.h
> +++ b/arch/x86/kvm/x86.h
> @@ -4,6 +4,8 @@
> #include <linux/kvm_host.h>
> #include "kvm_cache_regs.h"
>
> +#define MSR_IA32_CR_PAT_DEFAULT 0x0007040600070406ULL
> +
> static inline void kvm_clear_exception_queue(struct kvm_vcpu *vcpu)
> {
> vcpu->arch.exception.pending = false;
>

Applied, thanks.

Paolo

2015-05-13 05:20:38

by Wanpeng Li

[permalink] [raw]
Subject: Re: [PATCH] KVM: x86: fix initial PAT value

Hi Radim,
On Mon, Apr 27, 2015 at 03:11:25PM +0200, Radim Krčmář wrote:
>PAT should be 0007_0406_0007_0406h on RESET and not modified on INIT.

Could you point out where this value is described in SDM? :)

Regards,
Wanpeng Li

>VMX used a wrong value (host's PAT) and while SVM used the right one,
>it never got to arch.pat.
>
>This is not an issue with QEMU as it will force the correct value.
>
>Signed-off-by: Radim Krčmář <[email protected]>
>---
> arch/x86/kvm/svm.c | 2 +-
> arch/x86/kvm/vmx.c | 12 ++----------
> arch/x86/kvm/x86.c | 2 ++
> arch/x86/kvm/x86.h | 2 ++
> 4 files changed, 7 insertions(+), 11 deletions(-)
>
>diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c
>index ce741b8650f6..9aceb4a8c147 100644
>--- a/arch/x86/kvm/svm.c
>+++ b/arch/x86/kvm/svm.c
>@@ -1176,7 +1176,7 @@ static void init_vmcb(struct vcpu_svm *svm)
> clr_exception_intercept(svm, PF_VECTOR);
> clr_cr_intercept(svm, INTERCEPT_CR3_READ);
> clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
>- save->g_pat = 0x0007040600070406ULL;
>+ save->g_pat = svm->vcpu.arch.pat;
> save->cr3 = 0;
> save->cr4 = 0;
> }
>diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
>index f7b61687bd79..62459382d305 100644
>--- a/arch/x86/kvm/vmx.c
>+++ b/arch/x86/kvm/vmx.c
>@@ -4667,16 +4667,8 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
> vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
> vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
>
>- if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
>- u32 msr_low, msr_high;
>- u64 host_pat;
>- rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
>- host_pat = msr_low | ((u64) msr_high << 32);
>- /* Write the default value follow host pat */
>- vmcs_write64(GUEST_IA32_PAT, host_pat);
>- /* Keep arch.pat sync with GUEST_IA32_PAT */
>- vmx->vcpu.arch.pat = host_pat;
>- }
>+ if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
>+ vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
>
> for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
> u32 index = vmx_msr_index[i];
>diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
>index c73efcd03e29..a20898f9e43a 100644
>--- a/arch/x86/kvm/x86.c
>+++ b/arch/x86/kvm/x86.c
>@@ -7363,6 +7363,8 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
>
> vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
>
>+ vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
>+
> kvm_async_pf_hash_reset(vcpu);
> kvm_pmu_init(vcpu);
>
>diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h
>index f5fef1868096..01a1d011e073 100644
>--- a/arch/x86/kvm/x86.h
>+++ b/arch/x86/kvm/x86.h
>@@ -4,6 +4,8 @@
> #include <linux/kvm_host.h>
> #include "kvm_cache_regs.h"
>
>+#define MSR_IA32_CR_PAT_DEFAULT 0x0007040600070406ULL
>+
> static inline void kvm_clear_exception_queue(struct kvm_vcpu *vcpu)
> {
> vcpu->arch.exception.pending = false;
>--
>2.3.6
>
>--
>To unsubscribe from this list: send the line "unsubscribe kvm" in
>the body of a message to [email protected]
>More majordomo info at http://vger.kernel.org/majordomo-info.html

2015-05-13 06:07:28

by Jan Kiszka

[permalink] [raw]
Subject: Re: [PATCH] KVM: x86: fix initial PAT value

On 2015-05-13 07:02, Wanpeng Li wrote:
> Hi Radim,
> On Mon, Apr 27, 2015 at 03:11:25PM +0200, Radim Krčmář wrote:
>> PAT should be 0007_0406_0007_0406h on RESET and not modified on INIT.
>
> Could you point out where this value is described in SDM? :)

11.12.4 (revision from September 2014)

Jan

--
Siemens AG, Corporate Technology, CT RTC ITP SES-DE
Corporate Competence Center Embedded Linux