From mboxrd@z Thu Jan 1 00:00:00 1970 From: Xiao Guangrong Subject: Re: [PATCH V4 1/7] KVM, pkeys: expose CPUID/CR4 to guest Date: Sun, 6 Mar 2016 15:15:49 +0800 Message-ID: <56DBD925.1010807@linux.intel.com> References: <1457177252-7577-1-git-send-email-huaitong.han@intel.com> <1457177252-7577-2-git-send-email-huaitong.han@intel.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Cc: kvm@vger.kernel.org To: Huaitong Han , pbonzini@redhat.com, gleb@kernel.org Return-path: Received: from mga01.intel.com ([192.55.52.88]:22436 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751151AbcCFHQK (ORCPT ); Sun, 6 Mar 2016 02:16:10 -0500 In-Reply-To: <1457177252-7577-2-git-send-email-huaitong.han@intel.com> Sender: kvm-owner@vger.kernel.org List-ID: On 03/05/2016 07:27 PM, Huaitong Han wrote: > X86_FEATURE_PKU is referred to as "PKU" in the hardware documentation: > CPUID.7.0.ECX[3]:PKU. X86_FEATURE_OSPKE is software support for pkeys, > enumerated with CPUID.7.0.ECX[4]:OSPKE, and it reflects the setting of > CR4.PKE(bit 22). > > Signed-off-by: Huaitong Han > --- > arch/x86/include/asm/kvm_host.h | 3 ++- > arch/x86/kvm/cpuid.c | 21 +++++++++++++++++++-- > arch/x86/kvm/cpuid.h | 8 ++++++++ > arch/x86/kvm/x86.c | 9 ++++++--- > 4 files changed, 35 insertions(+), 6 deletions(-) > > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h > index 44adbb8..2867626 100644 > --- a/arch/x86/include/asm/kvm_host.h > +++ b/arch/x86/include/asm/kvm_host.h > @@ -83,7 +83,8 @@ > | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \ > | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR | X86_CR4_PCIDE \ > | X86_CR4_OSXSAVE | X86_CR4_SMEP | X86_CR4_FSGSBASE \ > - | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE | X86_CR4_SMAP)) > + | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE | X86_CR4_SMAP \ > + | X86_CR4_PKE)) > > #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR) > > diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c > index 6525e92..f058db5 100644 > --- a/arch/x86/kvm/cpuid.c > +++ b/arch/x86/kvm/cpuid.c > @@ -81,6 +81,17 @@ int kvm_update_cpuid(struct kvm_vcpu *vcpu) > apic->lapic_timer.timer_mode_mask = 1 << 17; > } > > + best = kvm_find_cpuid_entry(vcpu, 7, 0); > + if (!best) > + return 0; > + > + /*Update OSPKE bit */ Missed a space here. > + if (boot_cpu_has(X86_FEATURE_PKU) && best->function == 0x7) { > + best->ecx &= ~F(OSPKE); > + if (kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) > + best->ecx |= F(OSPKE); > + } > + > best = kvm_find_cpuid_entry(vcpu, 0xD, 0); > if (!best) { > vcpu->arch.guest_supported_xcr0 = 0; > @@ -354,6 +365,9 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, > const u32 kvm_supported_word10_x86_features = > F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | f_xsaves; > > + /* cpuid 7.0.ecx*/ > + const u32 kvm_supported_word11_x86_features = F(PKU) | 0 /*OSPKE*/; > + > /* all calls to cpuid_count() should be made on the same cpu */ > get_cpu(); > > @@ -431,10 +445,13 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, > cpuid_mask(&entry->ebx, 9); > // TSC_ADJUST is emulated > entry->ebx |= F(TSC_ADJUST); > - } else > + entry->ecx &= kvm_supported_word11_x86_features; > + cpuid_mask(&entry->ecx, 13); Can we use a meaningful name defined by cpuid_leafs instead of the raw number?