From: Huaitong Han Subject: [PATCH V6 4/9] x86: pkey: introduce write_pkru() for KVM Date: Tue, 22 Mar 2016 16:51:17 +0800 Message-ID: <1458636682-5125-5-git-send-email-huaitong.han@intel.com> References: <1458636682-5125-1-git-send-email-huaitong.han@intel.com> Cc: guangrong.xiao@linux.intel.com, kvm@vger.kernel.org, Ingo Molnar , Dave Hansen , Huaitong Han To: pbonzini@redhat.com, gleb@kernel.org Return-path: Received: from mga01.intel.com ([192.55.52.88]:31139 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757966AbcCVIvV (ORCPT ); Tue, 22 Mar 2016 04:51:21 -0400 In-Reply-To: <1458636682-5125-1-git-send-email-huaitong.han@intel.com> Sender: kvm-owner@vger.kernel.org List-ID: From: Xiao Guangrong KVM will use it to switch pkru between guest and host. CC: Ingo Molnar CC: Dave Hansen Signed-off-by: Xiao Guangrong Signed-off-by: Huaitong Han --- arch/x86/include/asm/pgtable.h | 6 ++++++ arch/x86/include/asm/special_insns.h | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 1ff49ec..97f3242 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -107,6 +107,12 @@ static inline u32 read_pkru(void) return 0; } +static inline void write_pkru(u32 pkru) +{ + if (boot_cpu_has(X86_FEATURE_OSPKE)) + __write_pkru(pkru); +} + static inline int pte_young(pte_t pte) { return pte_flags(pte) & _PAGE_ACCESSED; diff --git a/arch/x86/include/asm/special_insns.h b/arch/x86/include/asm/special_insns.h index aee6e76..d96d043 100644 --- a/arch/x86/include/asm/special_insns.h +++ b/arch/x86/include/asm/special_insns.h @@ -113,11 +113,27 @@ static inline u32 __read_pkru(void) : "c" (ecx)); return pkru; } + +static inline void __write_pkru(u32 pkru) +{ + u32 ecx = 0, edx = 0; + + /* + * "wrpkru" instruction. Loads contents in EAX to PKRU, + * requires that ecx = edx = 0. + */ + asm volatile(".byte 0x0f,0x01,0xef\n\t" + : : "a" (pkru), "c"(ecx), "d"(edx)); +} #else static inline u32 __read_pkru(void) { return 0; } + +static inline void __write_pkru(u32 pkru) +{ +} #endif static inline void native_wbinvd(void) -- 1.8.3.1