x86/hvm: pkeys, add memory protection-key support
v3 cover x86/hvm: pkeys, add memory protection-key support
Changes in v3:
*Get CPUID:ospke depend on guest cpuid instead of host hardware capable, and Move cpuid patch to the last of patches.
*Move opt_pku to cpu/common.c.
*Use MASK_EXTR for get_pte_pkeys.
*Add quoting for pkru macro, and use static inline pkru_read functions. *Rebase get_xsave_addr for updated codes, and add uncompressed format support for xsave state.
*Use fpu_xsave instead of vcpu_save_fpu, and adjust the code style for leaf_pte_pkeys_check.
*Add parentheses for PFEC_prot_key of gva2gfn funcitons.
Changes in v2:
*Rebase all patches in staging branch
*Disable X86_CR4_PKE on hypervisor, and delete pkru_read/write functions, and use xsave state read to get pkru value.
*Delete the patch that adds pkeys support for do_page_fault.
*Add pkeys support for gva2gfn so that setting _PAGE_PK_BIT in the return value can get propagated to the guest correctly.
The protection-key feature provides an additional mechanism by which IA-32e paging controls access to usermode addresses.
Hardware support for protection keys for user pages is enumerated with CPUID feature flag CPUID.7.0.ECX[3]:PKU. Software support is CPUID.7.0.ECX[4]:OSPKE with the setting of CR4.PKE(bit 22).
When CR4.PKE = 1, every linear address is associated with the 4-bit protection key located in bits 62:59 of the paging-structure entry that mapped the page containing the linear address. The PKRU register determines, for each protection key, whether user-mode addresses with that protection key may be read or written.
The PKRU register (protection key rights for user pages) is a 32-bit register with the following format: for each i (0 ≤ i ≤ 15), PKRU[2i] is the access-disable bit for protection key i (ADi); PKRU[2i+1] is the write-disable bit for protection key i (WDi).
Software can use the RDPKRU and WRPKRU instructions with ECX = 0 to read and write PKRU. In addition, the PKRU register is XSAVE-managed state and can thus be read and written by instructions in the XSAVE feature set.
PFEC.PK (bit 5) is defined as protection key violations.
The specification of Protection Keys can be found at SDM (4.6.2, volume 3)
http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-manual-325462.pdf.
Huaitong Han (9):
x86/hvm: pkeys, add the flag to enable Memory Protection Keys x86/hvm: pkeys, add pkeys support when setting CR4 x86/hvm: pkeys, disable pkeys for guests in non-paging mode x86/hvm: pkeys, add functions to get pkeys value from PTE x86/hvm: pkeys, add functions to support PKRU access x86/hvm: pkeys, add xstate support for pkeys x86/hvm: pkeys, add pkeys support for guest_walk_tables x86/hvm: pkeys, add pkeys support for gva2gfn funcitons x86/hvm: pkeys, add pkeys support for cpuid handling
docs/misc/xen-command-line.markdown | 21 +++++++++++ tools/libxc/xc_cpufeature.h | 2 + tools/libxc/xc_cpuid_x86.c | 6 ++- xen/arch/x86/cpu/common.c | 10 ++++- xen/arch/x86/hvm/hvm.c | 34 +++++++++++++---- xen/arch/x86/hvm/vmx/vmx.c | 11 +++--- xen/arch/x86/i387.c | 2 +- xen/arch/x86/mm/guest_walk.c | 73 +++++++++++++++++++++++++++++++++++++ xen/arch/x86/xstate.c | 7 +++- xen/include/asm-x86/cpufeature.h | 6 ++- xen/include/asm-x86/guest_pt.h | 7 ++++ xen/include/asm-x86/hvm/hvm.h | 2 + xen/include/asm-x86/i387.h | 1 + xen/include/asm-x86/page.h | 5 +++ xen/include/asm-x86/processor.h | 20 ++++++++++ xen/include/asm-x86/x86_64/page.h | 12 ++++++ xen/include/asm-x86/xstate.h | 4 +- 17 files changed, 203 insertions(+), 20 deletions(-)
Signature · 7 lines
2.4.3 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
PATCH v3 1/9 x86/hvm: pkeys, add the flag to enable Memory Protection Keys
This patch adds the flag to enable Memory Protection Keys.
--- docs/misc/xen-command-line.markdown | 21 +++++++++++++++++++++ xen/arch/x86/cpu/common.c | 10 +++++++++- xen/include/asm-x86/cpufeature.h | 6 +++++- 3 files changed, 35 insertions(+), 2 deletions(-)
docs/misc/xen-command-line.markdown +21 -0
@@ -1177,6 +1177,27 @@ This option can be specified more than once (up to 8 times at present). ### ple\_window > `= <integer>` +### pku +> `= <boolean>` + +> Default: `true` + +Flag to enable Memory Protection Keys. + +The protection-key feature provides an additional mechanism by which IA-32e +paging controls access to usermode addresses. + +When CR4.PKE = 1, every linear address is associated with the 4-bit protection +key located in bits 62:59 of the paging-structure entry that mapped the page +containing the linear address. The PKRU register determines, for each +protection key, whether user-mode addresses with that protection key may be +read or written. + +The PKRU register (protection key rights for user pages) is a 32-bit register +with the following format: for each i (0 ≤ i ≤ 15), PKRU[2i] is the +access-disable bit for protection key i (ADi); PKRU[2i+1] is the write-disable +bit for protection key i (WDi). + ### psr (Intel) > `= List of ( cmt:<boolean> | rmid_max:<integer> | cat:<boolean> | cos_max:<integer> | cdp:<boolean> )`
xen/arch/x86/cpu/common.c +9 -1
@@ -22,6 +22,10 @@ boolean_param("xsave", use_xsave);bool_t opt_arat =1 ;boolean_param ("arat" , opt_arat); +/* pku: Flag to enable Memory Protection Keys (default on). */ +bool_t opt_pku =1 ; +boolean_param ("pku" , opt_pku); +unsigned int opt_cpuid_mask_ecx = ~0u ;integer_param ("cpuid_mask_ecx" , opt_cpuid_mask_ecx);unsigned int opt_cpuid_mask_edx = ~0u ;
@@ -270,7 +274,8 @@ static void __cpuinit generic_identify(struct cpuinfo_x86 *c)if ( c->cpuid_level >=0x00000007 )cpuid_count (0x00000007 ,0 , &tmp, &c->x86_capability[cpufeat_word (X86_FEATURE_FSGSBASE )], - &tmp, &tmp); + &c->x86_capability[cpufeat_word (X86_FEATURE_PKU )], + &tmp); }/*
@@ -323,6 +328,9 @@ void __cpuinit identify_cpu(struct cpuinfo_x86 *c)if ( cpu_has_xsave )xstate_init (c); +if ( !opt_pku ) +setup_clear_cpu_cap (X86_FEATURE_PKU ); +/* * The vendor-specific functions might have changed features. Now * we do "generic changes."
xen/include/asm-x86/cpufeature.h +5 -1
@@ -11,7 +11,7 @@#include <xen/const.h> -#define NCAPINTS 8/* N 32-bit words worth of info */ +#define NCAPINTS 9/* N 32-bit words worth of info */ /* Intel-defined CPU features, CPUID level 0x00000001 (edx), word 0 */ #define X86_FEATURE_FPU (0*32+ 0)/* Onboard FPU */
@@ -163,6 +163,10 @@#define X86_FEATURE_ADX (7*32+19)/* ADCX, ADOX instructions */ #define X86_FEATURE_SMAP (7*32+20)/* Supervisor Mode Access Prevention */ +/* Intel-defined CPU features, CPUID level 0x00000007:0 (ecx), word 8 */ +#define X86_FEATURE_PKU (8*32+ 3)/* Protection Keys for Userspace */ +#define X86_FEATURE_OSPKE (8*32+ 4)/* OS Protection Keys Enable */ +#define cpufeat_word (idx) ((idx) / 32)#define cpufeat_bit (idx) ((idx) % 32)#define cpufeat_mask (idx) (_AC(1, U) << cpufeat_bit(idx))
Signature · 7 lines
2.4.3 _______________________________________________ Xen-devel mailing list [email protected] http://lists.xen.org/xen-devel
Re: [V3 PATCH 1/9] x86/hvm: pkeys, add the flag to enable Memory Protection Keys
Quoted 28 lines · Huaitong Han
This patch adds the flag to enable Memory Protection Keys.
Signed-off-by Huaitong Han <[email protected]>Reviewed-by Andrew Cooper <[email protected]>--- docs/misc/xen-command-line.markdown | 21 +++++++++++++++++++++ xen/arch/x86/cpu/common.c | 10 +++++++++- xen/include/asm-x86/cpufeature.h | 6 +++++- 3 files changed, 35 insertions(+), 2 deletions(-)docs/misc/xen-command-line.markdown +10
-0@@ -1177,6 +1177,27 @@ This option can be specified more than once (up to 8 times at present). ### ple\_window > `= <integer>` +### pku +> `= <boolean>` + +> Default: `true` + +Flag to enable Memory Protection Keys. + +The protection-key feature provides an additional mechanism by which IA-32e +paging controls access to usermode addresses. +
docs/misc/xen-command-line.markdown This option can be specified more than once (up to 8 times at present). · line 1199
Quoted 10 lines
docs/misc/xen-command-line.markdown +10
-0+When CR4.PKE = 1, every linear address is associated with the 4-bit protection +key located in bits 62:59 of the paging-structure entry that mapped the page +containing the linear address. The PKRU register determines, for each +protection key, whether user-mode addresses with that protection key may be +read or written. + +The PKRU register (protection key rights for user pages) is a 32-bit register +with the following format: for each i (0 ≤ i ≤ 15), PKRU[2i] is the +access-disable bit for protection key i (ADi); PKRU[2i+1] is the write-disable +bit for protection key i (WDi).
These last two paragraphs are awfully technically detailed for a command-line reference. I think the first two paragraphs would be sufficient.
-George
Quoted 54 lines
docs/misc/xen-command-line.markdown +1
-0+ ### psr (Intel) > `= List of ( cmt:<boolean> | rmid_max:<integer> | cat:<boolean> | cos_max:<integer> | cdp:<boolean> )`xen/arch/x86/cpu/common.c +9
-1@@ -22,6 +22,10 @@ boolean_param("xsave", use_xsave);bool_t opt_arat =1 ;boolean_param ("arat" , opt_arat); +/* pku: Flag to enable Memory Protection Keys (default on). */ +bool_t opt_pku =1 ; +boolean_param ("pku" , opt_pku); +unsigned int opt_cpuid_mask_ecx = ~0u ;integer_param ("cpuid_mask_ecx" , opt_cpuid_mask_ecx);unsigned int opt_cpuid_mask_edx = ~0u ;@@ -270,7 +274,8 @@ static void __cpuinit generic_identify(struct cpuinfo_x86 *c)if ( c->cpuid_level >=0x00000007 )cpuid_count (0x00000007 ,0 , &tmp, &c->x86_capability[cpufeat_word (X86_FEATURE_FSGSBASE )], - &tmp, &tmp); + &c->x86_capability[cpufeat_word (X86_FEATURE_PKU )], + &tmp); }/* @@ -323,6 +328,9 @@ void __cpuinit identify_cpu(struct cpuinfo_x86 *c)if ( cpu_has_xsave )xstate_init (c); +if ( !opt_pku ) +setup_clear_cpu_cap (X86_FEATURE_PKU ); +/* * The vendor-specific functions might have changed features. Now * we do "generic changes." xen/include/asm-x86/cpufeature.h +1
-1@@ -11,7 +11,7 @@#include <xen/const.h> -#define NCAPINTS 8/* N 32-bit words worth of info */ +#define NCAPINTS 9/* N 32-bit words worth of info */ /* Intel-defined CPU features, CPUID level 0x00000001 (edx), word 0 */ #define X86_FEATURE_FPU (0*32+ 0)/* Onboard FPU */
xen/include/asm-x86/cpufeature.h line 172
Quoted 10 lines
xen/include/asm-x86/cpufeature.h +4
-0@@ -163,6 +163,10 @@#define X86_FEATURE_ADX (7*32+19)/* ADCX, ADOX instructions */ #define X86_FEATURE_SMAP (7*32+20)/* Supervisor Mode Access Prevention */ +/* Intel-defined CPU features, CPUID level 0x00000007:0 (ecx), word 8 */ +#define X86_FEATURE_PKU (8*32+ 3)/* Protection Keys for Userspace */ +#define X86_FEATURE_OSPKE (8*32+ 4)/* OS Protection Keys Enable */ +#define cpufeat_word (idx) ((idx) / 32)#define cpufeat_bit (idx) ((idx) % 32)#define cpufeat_mask (idx) (_AC(1, U) << cpufeat_bit(idx))
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH v3 2/9 x86/hvm: pkeys, add pkeys support when setting CR4
In reply to Huaitong HanThis patch adds pkeys support when setting CR4.
--- xen/arch/x86/hvm/hvm.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
xen/arch/x86/hvm/hvm.c +4 -1
@@ -1924,6 +1924,7 @@ static unsigned long hvm_cr4_guest_reserved_bits(const struct vcpu *v, leaf1_edx = boot_cpu_data.x86_capability[cpufeat_word (X86_FEATURE_VME )]; leaf1_ecx = boot_cpu_data.x86_capability[cpufeat_word (X86_FEATURE_PCID )]; leaf7_0_ebx = boot_cpu_data.x86_capability[cpufeat_word (X86_FEATURE_FSGSBASE )]; + leaf7_0_ecx = boot_cpu_data.x86_capability[cpufeat_word (X86_FEATURE_PKU )]; } return ~(unsigned long)
@@ -1959,7 +1960,9 @@ static unsigned long hvm_cr4_guest_reserved_bits(const struct vcpu *v, (leaf7_0_ebx &cpufeat_mask (X86_FEATURE_SMEP ) ?X86_CR4_SMEP :0 ) | (leaf7_0_ebx &cpufeat_mask (X86_FEATURE_SMAP ) ? - X86_CR4_SMAP : 0)); +X86_CR4_SMAP :0 ) | + (leaf7_0_ecx &cpufeat_mask (X86_FEATURE_PKU ) ? + X86_CR4_PKE : 0)); }static int hvm_load_cpu_ctxt (struct domain *d,hvm_domain_context_t *h)
Signature · 1 line
2.4.3
PATCH v3 3/9 x86/hvm: pkeys, disable pkeys for guests in non-paging mode
In reply to Huaitong HanThis patch disables pkeys for guest in non-paging mode, However XEN always uses paging mode to emulate guest non-paging mode, To emulate this behavior, pkeys needs to be manually disabled when guest switches to non-paging mode.
--- xen/arch/x86/hvm/vmx/vmx.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/vmx/vmx.c +6 -5
@@ -1380,12 +1380,13 @@ static void vmx_update_guest_cr(struct vcpu *v, unsigned int cr)if ( !hvm_paging_enabled (v) ) {/* -* SMEP/SMAP is disabled if CPU is in non-paging mode in hardware. -* However Xen always uses paging mode to emulate guest non-paging -* mode. To emulate this behavior, SMEP/SMAP needs to be manually -* disabled when guest VCPU is in non-paging mode. +* SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in +* hardware. However Xen always uses paging mode to emulate guest +* non-paging mode. To emulate this behavior, SMEP/SMAP/PKU needs +* to be manually disabled when guest VCPU is in non-paging mode. */ - v->arch.hvm_vcpu.hw_cr[4 ] &= ~(X86_CR4_SMEP |X86_CR4_SMAP ); + v->arch.hvm_vcpu.hw_cr[4 ] &= + ~(X86_CR4_SMEP |X86_CR4_SMAP |X86_CR4_PKE ); }__vmwrite (GUEST_CR4 , v->arch.hvm_vcpu.hw_cr[4 ]);break ;
Signature · 1 line
2.4.3
PATCH v3 4/9 x86/hvm: pkeys, add functions to get pkeys value from PTE
In reply to Huaitong HanThis patch adds functions to get pkeys value from PTE.
--- xen/include/asm-x86/guest_pt.h | 7 +++++++ xen/include/asm-x86/page.h | 5 +++++ xen/include/asm-x86/x86_64/page.h | 12 ++++++++++++ 3 files changed, 24 insertions(+)
xen/include/asm-x86/guest_pt.h +7 -0
@@ -154,6 +154,13 @@ static inline u32 guest_l4e_get_flags(guest_l4e_t gl4e) {return l4e_get_flags (gl4e); }#endif +static inline u32 guest_l1e_get_pkeys (guest_l1e_t gl1e) +{return l1e_get_pkeys (gl1e); } +static inline u32 guest_l2e_get_pkeys (guest_l2e_t gl2e) +{return l2e_get_pkeys (gl2e); } +static inline u32 guest_l3e_get_pkeys (guest_l3e_t gl3e) +{return l3e_get_pkeys (gl3e); } +static inline guest_l1e_t guest_l1e_from_gfn (gfn_t gfn,u32 flags) {return l1e_from_pfn (gfn_x (gfn), flags); }static inline guest_l2e_t guest_l2e_from_gfn (gfn_t gfn,u32 flags)
xen/include/asm-x86/page.h +5 -0
@@ -93,6 +93,11 @@#define l3e_get_flags (x) (get_pte_flags((x).l3))#define l4e_get_flags (x) (get_pte_flags((x).l4)) +/* Get pte pkeys (unsigned int). */ +#define l1e_get_pkeys (x) (get_pte_pkeys((x).l1)) +#define l2e_get_pkeys (x) (get_pte_pkeys((x).l2)) +#define l3e_get_pkeys (x) (get_pte_pkeys((x).l3)) +/* Construct an empty pte. */ #define l1e_empty () ((l1_pgentry_t) { 0 })#define l2e_empty () ((l2_pgentry_t) { 0 })
xen/include/asm-x86/x86_64/page.h +12 -0
@@ -134,6 +134,18 @@ typedef l4_pgentry_t root_pgentry_t;#define get_pte_flags (x) (((int)((x) >> 40) & ~0xFFF) | ((int)(x) & 0xFFF))#define put_pte_flags (x) (((intpte_t)((x) & ~0xFFF) << 40) | ((x) & 0xFFF)) +/* +* Protection keys define a new 4-bit protection key field +* (PKEY) in bits 62:59 of leaf entries of the page tables. +* This corresponds to bit 22:19 of a 24-bit flags. +* +* Notice: Bit 22 is used by _PAGE_GNTTAB which is visible to PV guests, +* so Protection keys must be disabled on PV guests. +*/ +#define _PAGE_PKEY_BITS (0x780000)/* Protection Keys, 22:19 */ + +#define get_pte_pkeys (x) (MASK_EXTR(get_pte_flags(x), _PAGE_PKEY_BITS)) +/* Bit 23 of a 24-bit flag mask. This corresponds to bit 63 of a pte.*/ #define _PAGE_NX_BIT (1U<<23)
Signature · 1 line
2.4.3
Re: [V3 PATCH 4/9] x86/hvm: pkeys, add functions to get pkeys value from PTE
Quoted 52 lines · Huaitong Han
This patch adds functions to get pkeys value from PTE.
Signed-off-by Huaitong Han <[email protected]>--- xen/include/asm-x86/guest_pt.h | 7 +++++++ xen/include/asm-x86/page.h | 5 +++++ xen/include/asm-x86/x86_64/page.h | 12 ++++++++++++ 3 files changed, 24 insertions(+)xen/include/asm-x86/guest_pt.h +7
-0@@ -154,6 +154,13 @@ static inline u32 guest_l4e_get_flags(guest_l4e_t gl4e) {return l4e_get_flags (gl4e); }#endif +static inline u32 guest_l1e_get_pkeys (guest_l1e_t gl1e) +{return l1e_get_pkeys (gl1e); } +static inline u32 guest_l2e_get_pkeys (guest_l2e_t gl2e) +{return l2e_get_pkeys (gl2e); } +static inline u32 guest_l3e_get_pkeys (guest_l3e_t gl3e) +{return l3e_get_pkeys (gl3e); } +static inline guest_l1e_t guest_l1e_from_gfn (gfn_t gfn,u32 flags) {return l1e_from_pfn (gfn_x (gfn), flags); }static inline guest_l2e_t guest_l2e_from_gfn (gfn_t gfn,u32 flags)xen/include/asm-x86/page.h +5
-0@@ -93,6 +93,11 @@#define l3e_get_flags (x) (get_pte_flags((x).l3))#define l4e_get_flags (x) (get_pte_flags((x).l4)) +/* Get pte pkeys (unsigned int). */ +#define l1e_get_pkeys (x) (get_pte_pkeys((x).l1)) +#define l2e_get_pkeys (x) (get_pte_pkeys((x).l2)) +#define l3e_get_pkeys (x) (get_pte_pkeys((x).l3)) +/* Construct an empty pte. */ #define l1e_empty () ((l1_pgentry_t) { 0 })#define l2e_empty () ((l2_pgentry_t) { 0 })xen/include/asm-x86/x86_64/page.h +1
-0@@ -134,6 +134,18 @@ typedef l4_pgentry_t root_pgentry_t;#define get_pte_flags (x) (((int)((x) >> 40) & ~0xFFF) | ((int)(x) & 0xFFF))#define put_pte_flags (x) (((intpte_t)((x) & ~0xFFF) << 40) | ((x) & 0xFFF)) +/*
xen/include/asm-x86/x86_64/page.h typedef l4_pgentry_t root_pgentry_t; · line 147
Quoted 10 lines
xen/include/asm-x86/x86_64/page.h +10
-0+* Protection keys define a new 4-bit protection key field +* (PKEY) in bits 62:59 of leaf entries of the page tables. +* This corresponds to bit 22:19 of a 24-bit flags. +* +* Notice: Bit 22 is used by _PAGE_GNTTAB which is visible to PV guests, +* so Protection keys must be disabled on PV guests. +*/ +#define _PAGE_PKEY_BITS (0x780000)/* Protection Keys, 22:19 */ + +#define get_pte_pkeys (x) (MASK_EXTR(get_pte_flags(x), _PAGE_PKEY_BITS))
Sorry if I'm getting nit-picky here, but any given pte will only have a single pkey, right? Would it be better if these were "get_pte_pkey()" (i.e., singular)?
-George
Re: [V3 PATCH 4/9] x86/hvm: pkeys, add functions to get pkeys value from PTE
Quoted 52 lines · George Dunlap
Quoted 51 lines · Huaitong Han
This patch adds functions to get pkeys value from PTE.
Signed-off-by Huaitong Han <[email protected]>--- xen/include/asm-x86/guest_pt.h | 7 +++++++ xen/include/asm-x86/page.h | 5 +++++ xen/include/asm-x86/x86_64/page.h | 12 ++++++++++++ 3 files changed, 24 insertions(+)xen/include/asm-x86/guest_pt.h +7
-0@@ -154,6 +154,13 @@ static inline u32 guest_l4e_get_flags(guest_l4e_t gl4e) {return l4e_get_flags (gl4e); }#endif +static inline u32 guest_l1e_get_pkeys (guest_l1e_t gl1e) +{return l1e_get_pkeys (gl1e); } +static inline u32 guest_l2e_get_pkeys (guest_l2e_t gl2e) +{return l2e_get_pkeys (gl2e); } +static inline u32 guest_l3e_get_pkeys (guest_l3e_t gl3e) +{return l3e_get_pkeys (gl3e); } +static inline guest_l1e_t guest_l1e_from_gfn (gfn_t gfn,u32 flags) {return l1e_from_pfn (gfn_x (gfn), flags); }static inline guest_l2e_t guest_l2e_from_gfn (gfn_t gfn,u32 flags)xen/include/asm-x86/page.h +5
-0@@ -93,6 +93,11 @@#define l3e_get_flags (x) (get_pte_flags((x).l3))#define l4e_get_flags (x) (get_pte_flags((x).l4)) +/* Get pte pkeys (unsigned int). */ +#define l1e_get_pkeys (x) (get_pte_pkeys((x).l1)) +#define l2e_get_pkeys (x) (get_pte_pkeys((x).l2)) +#define l3e_get_pkeys (x) (get_pte_pkeys((x).l3)) +/* Construct an empty pte. */ #define l1e_empty () ((l1_pgentry_t) { 0 })#define l2e_empty () ((l2_pgentry_t) { 0 })xen/include/asm-x86/x86_64/page.h +0
-0@@ -134,6 +134,18 @@ typedef l4_pgentry_t root_pgentry_t;#define get_pte_flags (x) (((int)((x) >> 40) & ~0xFFF) | ((int)(x) & 0xFFF))#define put_pte_flags (x) (((intpte_t)((x) & ~0xFFF) << 40) | ((x) & 0xFFF))
Quoted 14 lines
xen/include/asm-x86/x86_64/page.h typedef l4_pgentry_t root_pgentry_t; · line 147
Quoted 11 lines
xen/include/asm-x86/x86_64/page.h +11
-0+/* +* Protection keys define a new 4-bit protection key field +* (PKEY) in bits 62:59 of leaf entries of the page tables. +* This corresponds to bit 22:19 of a 24-bit flags. +* +* Notice: Bit 22 is used by _PAGE_GNTTAB which is visible to PV guests, +* so Protection keys must be disabled on PV guests. +*/ +#define _PAGE_PKEY_BITS (0x780000)/* Protection Keys, 22:19 */ + +#define get_pte_pkeys (x) (MASK_EXTR(get_pte_flags(x), _PAGE_PKEY_BITS))Sorry if I'm getting nit-picky here, but any given pte will only have a single pkey, right? Would it be better if these were "
get_pte_pkey()" (i.e., singular)?
Correct. An individual pte contains 4 bits which is "the protection key", an index (in the range 0-15) into the protection key register, which ultimately determines access-denied/write-denied on the linear address.
~Andrew
PATCH v3 5/9 x86/hvm: pkeys, add functions to support PKRU access
In reply to Huaitong HanThis patch adds functions to support PKRU access.
--- xen/include/asm-x86/processor.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
xen/include/asm-x86/processor.h +20 -0
@@ -342,6 +342,26 @@ static inline void write_cr4(unsigned long val) asmvolatile ("mov %0,%%cr4" : :"r" (val) ); } +/* Macros for PKRU domain */ +#define PKRU_READ (0) +#define PKRU_WRITE (1) +#define PKRU_ATTRS (2) + +/* +* PKRU defines 32 bits, there are 16 domains and 2 attribute bits per +* domain in pkru, pkeys is index to a defined domain, so the value of +* pte_pkeys * PKRU_ATTRS + R/W is offset of a defined domain attribute. +*/ +static inline bool_t read_pkru_ad (unsigned int pkru,unsigned int pkey) +{ +return (pkru >> (pkey *PKRU_ATTRS +PKRU_READ )) &1 ; +} + +static inline bool_t read_pkru_wd (unsigned int pkru,unsigned int pkey) +{ +return (pkru >> (pkey *PKRU_ATTRS +PKRU_WRITE )) &1 ; +} +/* Clear and set 'TS' bit respectively */ static inline void clts (void ) {
Signature · 1 line
2.4.3
Re: [V3 PATCH 5/9] x86/hvm: pkeys, add functions to support PKRU access
Quoted 3 lines · Huaitong Han
This patch adds functions to support PKRU access.
Signed-off-by Huaitong Han <[email protected]>
Quoted 14 lines
--- xen/include/asm-x86/processor.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)xen/include/asm-x86/processor.h +2
-0@@ -342,6 +342,26 @@ static inline void write_cr4(unsigned long val) asmvolatile ("mov %0,%%cr4" : :"r" (val) ); } +/* Macros for PKRU domain */ +#define PKRU_READ (0)
xen/include/asm-x86/processor.h static inline void write_cr4(unsigned long val) · line 356
Quoted 10 lines
xen/include/asm-x86/processor.h +10
-0+#define PKRU_WRITE (1) +#define PKRU_ATTRS (2) + +/* +* PKRU defines 32 bits, there are 16 domains and 2 attribute bits per +* domain in pkru, pkeys is index to a defined domain, so the value of +* pte_pkeys * PKRU_ATTRS + R/W is offset of a defined domain attribute. +*/ +static inline bool_t read_pkru_ad (unsigned int pkru,unsigned int pkey) +{
ASSERT(pkey < 16);
xen/include/asm-x86/processor.h static inline void write_cr4(unsigned long val) · line 361
Quoted 5 lines
xen/include/asm-x86/processor.h +5
-0+return (pkru >> (pkey *PKRU_ATTRS +PKRU_READ )) &1 ; +} + +static inline bool_t read_pkru_wd (unsigned int pkru,unsigned int pkey) +{
ASSERT(pkey < 16);
~Andrew
Quoted 6 lines
xen/include/asm-x86/processor.h +3
-0+return (pkru >> (pkey *PKRU_ATTRS +PKRU_WRITE )) &1 ; +} +/* Clear and set 'TS' bit respectively */ static inline void clts (void ) {
PATCH v3 6/9 x86/hvm: pkeys, add xstate support for pkeys
In reply to Huaitong HanThis patch adds xstate support for pkeys.
--- xen/arch/x86/xstate.c | 7 +++++-- xen/include/asm-x86/xstate.h | 4 +++- 2 files changed, 8 insertions(+), 3 deletions(-)
xen/arch/x86/xstate.c +5 -2
@@ -146,12 +146,15 @@ static void __init setup_xstate_comp(void) } } -static void *get_xsave_addr (void *xsave,unsigned int xfeature_idx) +void *get_xsave_addr (void *xsave,unsigned int xfeature_idx) {if ( !((1ul << xfeature_idx) & xfeature_mask) )return NULL ; -return xsave + xstate_comp_offsets[xfeature_idx]; +if (xsave_area_compressed (xsave) ) +return xsave + xstate_comp_offsets[xfeature_idx]; +else +return xsave + xstate_offsets[xfeature_idx]; }void expand_xsave_states (struct vcpu *v,void *dest,unsigned int size)
xen/include/asm-x86/xstate.h +3 -1
@@ -34,13 +34,14 @@#define XSTATE_OPMASK (1ULL << 5)#define XSTATE_ZMM (1ULL << 6)#define XSTATE_HI_ZMM (1ULL << 7) +#define XSTATE_PKRU (1ULL << 9)#define XSTATE_LWP (1ULL << 62)/* AMD lightweight profiling */ #define XSTATE_FP_SSE (XSTATE_FP | XSTATE_SSE)#define XCNTXT_MASK (XSTATE_FP | XSTATE_SSE | XSTATE_YMM | XSTATE_OPMASK | \ XSTATE_ZMM | XSTATE_HI_ZMM | XSTATE_NONLAZY)#define XSTATE_ALL (~(1ULL << 63)) -#define XSTATE_NONLAZY (XSTATE_LWP | XSTATE_BNDREGS | XSTATE_BNDCSR) +#define XSTATE_NONLAZY (XSTATE_LWP | XSTATE_BNDREGS | XSTATE_BNDCSR | XSTATE_PKRU)#define XSTATE_LAZY (XSTATE_ALL & ~XSTATE_NONLAZY)#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
@@ -90,6 +91,7 @@ uint64_t get_msr_xss(void);void xsave (struct vcpu *v,uint64_t mask);void xrstor (struct vcpu *v,uint64_t mask);bool_t xsave_enabled (const struct vcpu *v); +void *get_xsave_addr (void *xsave,unsigned int xfeature_idx);int __must_check validate_xstate (u64 xcr0,u64 xcr0_accum,u64 xstate_bv);int __must_check handle_xsetbv (u32 index,u64 new_bv);void expand_xsave_states (struct vcpu *v,void *dest,unsigned int size);
Signature · 1 line
2.4.3
Re: [V3 PATCH 6/9] x86/hvm: pkeys, add xstate support for pkeys
Quoted 1 line · Huaitong Han
This patch adds xstate support for pkeys.
Hey Huaitong,
Hope you don't mind me giving you a little feedback on the way you've broken down your patches here. The purpose for breaking a change down into separate patches like this is to make it easier for people reviewing (and for people later who come and look at the commits) to figure out what's going on. But if you break the patch series down to much, you go back to making things harder again.
Take this patch, for instance. You're making get_xsave_addr() global (non-static), and also making it handle the case where the xsave area was compressed (which it didn't before).
But as a reviewer, I don't know at this point who is calling get_xsave_addr() or why; I don't know if this change is necessary, or if it is correct for all the callers (or if the change wrt compressed xsave areas is even necessary).
So one problem with this patch is that you don't include that information in the description. But part of it is that the change doesn't really make much sense by itself.
I think I'd squash patches 4-7 into a single patch.
More comments about the approach in patch 7.
-George
Quoted 58 lines
Signed-off-by Huaitong Han <[email protected]>--- xen/arch/x86/xstate.c | 7 +++++-- xen/include/asm-x86/xstate.h | 4 +++- 2 files changed, 8 insertions(+), 3 deletions(-)xen/arch/x86/xstate.c +5
-2@@ -146,12 +146,15 @@ static void __init setup_xstate_comp(void) } } -static void *get_xsave_addr (void *xsave,unsigned int xfeature_idx) +void *get_xsave_addr (void *xsave,unsigned int xfeature_idx) {if ( !((1ul << xfeature_idx) & xfeature_mask) )return NULL ; -return xsave + xstate_comp_offsets[xfeature_idx]; +if (xsave_area_compressed (xsave) ) +return xsave + xstate_comp_offsets[xfeature_idx]; +else +return xsave + xstate_offsets[xfeature_idx]; }void expand_xsave_states (struct vcpu *v,void *dest,unsigned int size)xen/include/asm-x86/xstate.h +3
-1@@ -34,13 +34,14 @@#define XSTATE_OPMASK (1ULL << 5)#define XSTATE_ZMM (1ULL << 6)#define XSTATE_HI_ZMM (1ULL << 7) +#define XSTATE_PKRU (1ULL << 9)#define XSTATE_LWP (1ULL << 62)/* AMD lightweight profiling */ #define XSTATE_FP_SSE (XSTATE_FP | XSTATE_SSE)#define XCNTXT_MASK (XSTATE_FP | XSTATE_SSE | XSTATE_YMM | XSTATE_OPMASK | \ XSTATE_ZMM | XSTATE_HI_ZMM | XSTATE_NONLAZY)#define XSTATE_ALL (~(1ULL << 63)) -#define XSTATE_NONLAZY (XSTATE_LWP | XSTATE_BNDREGS | XSTATE_BNDCSR) +#define XSTATE_NONLAZY (XSTATE_LWP | XSTATE_BNDREGS | XSTATE_BNDCSR | XSTATE_PKRU)#define XSTATE_LAZY (XSTATE_ALL & ~XSTATE_NONLAZY)#define XSTATE_COMPACTION_ENABLED (1ULL << 63)@@ -90,6 +91,7 @@ uint64_t get_msr_xss(void);void xsave (struct vcpu *v,uint64_t mask);void xrstor (struct vcpu *v,uint64_t mask);bool_t xsave_enabled (const struct vcpu *v); +void *get_xsave_addr (void *xsave,unsigned int xfeature_idx);int __must_check validate_xstate (u64 xcr0,u64 xcr0_accum,u64 xstate_bv);int __must_check handle_xsetbv (u32 index,u64 new_bv);void expand_xsave_states (struct vcpu *v,void *dest,unsigned int size);
Re: [V3 PATCH 6/9] x86/hvm: pkeys, add xstate support for pkeys
In reply to Huaitong HanQuoted 13 lines · Huaitong Han
This patch adds xstate support for pkeys.
Signed-off-by Huaitong Han <[email protected]>--- xen/arch/x86/xstate.c | 7 +++++-- xen/include/asm-x86/xstate.h | 4 +++- 2 files changed, 8 insertions(+), 3 deletions(-)xen/arch/x86/xstate.c +0
-0
xen/arch/x86/xstate.c static void __init setup_xstate_comp(void) · line 149
Quoted 5 lines
xen/arch/x86/xstate.c +1
-1@@ -146,12 +146,15 @@ static void __init setup_xstate_comp(void) } } -static void *get_xsave_addr (void *xsave,unsigned int xfeature_idx) +void *get_xsave_addr (void *xsave,unsigned int xfeature_idx)
This really should take a struct_xsave *xsave, rather than a void pointer.
xen/arch/x86/xstate.c static void __init setup_xstate_comp(void) · line 152
Quoted 3 lines
xen/arch/x86/xstate.c +0
-0{if ( !((1ul << xfeature_idx) & xfeature_mask) )return NULL ;
Now I look at it, this check is bogus. The check needs to be against the xsave header in the area, rather than Xen's maximum xfeature_mask. A guest might easily have a smaller xcr0 than the maximum Xen is willing to allow, causing the pointer below to be bogus.
~Andrew
Re: [V3 PATCH 6/9] x86/hvm: pkeys, add xstate support for pkeys
In reply to Huaitong HanQuoted 8 lines
Quoted 1 line
Quoted 1 line · On 07.12.15 at 10:16, <[email protected]> wrote:
xen/arch/x86/xstate.c +0
-1@@ -146,12 +146,15 @@ static void __init setup_xstate_comp(void) } } -static void *get_xsave_addr (void *xsave,unsigned int xfeature_idx)
xen/arch/x86/xstate.c static void __init setup_xstate_comp(void) · line 157
Quoted 10 lines
xen/arch/x86/xstate.c +5
-1+void *get_xsave_addr (void *xsave,unsigned int xfeature_idx) {if ( !((1ul << xfeature_idx) & xfeature_mask) )return NULL ; -return xsave + xstate_comp_offsets[xfeature_idx]; +if (xsave_area_compressed (xsave) ) +return xsave + xstate_comp_offsets[xfeature_idx]; +else +return xsave + xstate_offsets[xfeature_idx];
For constructs like this I'd really like to ask to avoid as much of the redundancy as possible. The most compact form would probably be
return xsave + (xsave_area_compressed(xsave)
? xstate_comp_offsets
: xstate_offsets)[xfeature_idx];
But at the very least the "else" should go away.
Jan
PATCH v3 7/9 x86/hvm: pkeys, add pkeys support for guest_walk_tables
In reply to Huaitong HanThis patch adds pkeys support for guest_walk_tables.
--- xen/arch/x86/i387.c | 2 +- xen/arch/x86/mm/guest_walk.c | 73 +++++++++++++++++++++++++++++++++++++++++++ xen/include/asm-x86/hvm/hvm.h | 2 ++ xen/include/asm-x86/i387.h | 1 + 4 files changed, 77 insertions(+), 1 deletion(-)
xen/arch/x86/i387.c +1 -1
@@ -132,7 +132,7 @@ static inline uint64_t vcpu_xsave_mask(const struct vcpu *v) }/* Save x87 extended state */ -static inline void fpu_xsave (struct vcpu *v) +void fpu_xsave (struct vcpu *v) {bool_t ok;uint64_t mask =vcpu_xsave_mask (v);
xen/arch/x86/mm/guest_walk.c +73 -0
115 lines not on this page · raw message
xen/include/asm-x86/hvm/hvm.h +2 -0
@@ -275,6 +275,8 @@ int hvm_girq_dest_2_vcpu_id(struct domain *d, uint8_t dest, uint8_t dest_mode); (hvm_paging_enabled(v) && ((v)->arch.hvm_vcpu.guest_cr[4] & X86_CR4_SMAP))#define hvm_nx_enabled (v) \ (!!((v)->arch.hvm_vcpu.guest_efer & EFER_NX)) +#define hvm_pku_enabled (v) \ + (hvm_paging_enabled(v) && ((v)->arch.hvm_vcpu.guest_cr[4] & X86_CR4_PKE))/* Can we use superpages in the HAP p2m table? */ #define hap_has_1gb (!!(hvm_funcs.hap_capabilities & HVM_HAP_SUPERPAGE_1GB))
xen/include/asm-x86/i387.h +1 -0
@@ -30,6 +30,7 @@ struct ix87_env {void vcpu_restore_fpu_eager (struct vcpu *v);void vcpu_restore_fpu_lazy (struct vcpu *v); +void fpu_xsave (struct vcpu *v);void vcpu_save_fpu (struct vcpu *v);void save_fpu_enable (void );
Signature · 1 line
2.4.3
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
Quoted 37 lines · Huaitong Han
This patch adds pkeys support for
guest_walk_tables.Signed-off-by Huaitong Han <[email protected]>--- xen/arch/x86/i387.c | 2 +- xen/arch/x86/mm/guest_walk.c | 73 +++++++++++++++++++++++++++++++++++++++++++ xen/include/asm-x86/hvm/hvm.h | 2 ++ xen/include/asm-x86/i387.h | 1 + 4 files changed, 77 insertions(+), 1 deletion(-)xen/arch/x86/i387.c +1
-1@@ -132,7 +132,7 @@ static inline uint64_t vcpu_xsave_mask(const struct vcpu *v) }/* Save x87 extended state */ -static inline void fpu_xsave (struct vcpu *v) +void fpu_xsave (struct vcpu *v) {bool_t ok;uint64_t mask =vcpu_xsave_mask (v);xen/arch/x86/mm/guest_walk.c +2
-0@@ -31,6 +31,8 @@ asm(".file \"" __OBJECT_FILE__ "\"");#include <xen/sched.h> #include <asm/page.h> #include <asm/guest_pt.h> +#include <asm/xstate.h> +#include <asm/i387.h> extern const uint32_t gw_page_flags[];#if GUEST_PAGING_LEVELS ==CONFIG_PAGING_LEVELS
xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 97
Quoted 6 lines
xen/arch/x86/mm/guest_walk.c +3
-0@@ -90,6 +92,61 @@ static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty)return 0 ; } +#if GUEST_PAGING_LEVELS >=CONFIG_PAGING_LEVELS +bool_t leaf_pte_pkeys_check (struct vcpu *vcpu,uint32_t pfec, +uint32_t pte_access,uint32_t pte_pkeys)
pte_access doesn't seem to be used at all.
xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 107
Quoted 10 lines
xen/arch/x86/mm/guest_walk.c +10
-0+{ +void *xsave_addr; +unsigned int pkru =0 ; +bool_t pkru_ad, pkru_wd; + +bool_t uf = !!(pfec & PFEC_user_mode); +bool_t wf = !!(pfec & PFEC_write_access); +bool_t ff = !!(pfec & PFEC_insn_fetch); +bool_t rsvdf = !!(pfec & PFEC_reserved_bit); +bool_t pkuf = !!(pfec & PFEC_prot_key);
So I'm just wondering out loud here -- is there actually any situation in which we would want guest_walk_tables to act differently than the real hardware?
That is, is there actually any situation where, pku is enabled, the vcpu is in long mode, PFEC_write_access and/or PFEC_page_present is set, and the pkey is non-zero, that we want guest_walk_tables() to only check the write-protect bit for the pte, and not also check the pkru?
Because if not, it seems like it would be much more robust to simply *always* check for pkru_ad if PFEC_page_present is set, and for pkru_wd if PFEC_write_access is set.
Then in patch 8, you wouldn't need to go around all the __hvm_copy functions adding in PFEC_prot; instead, you'd just need to add PFEC_insn_fetch to the "fetch" (as is already done for SMEP and NX), and you'd be done.
xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 112
Quoted 5 lines
xen/arch/x86/mm/guest_walk.c +5
-0+ +if ( !cpu_has_xsave || !pkuf ||is_pv_vcpu (vcpu) ) +return 0 ; + +/* PKRU dom0 is always zero */
"dom0" has a very specific meaning in Xen. I think this would be better written "pkey 0 always has full access".
xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 117
Quoted 5 lines
xen/arch/x86/mm/guest_walk.c +5
-0+if (likely (!pte_pkeys) ) +return 0 ; + +/* Update vcpu xsave area */ +fpu_xsave (vcpu);
Is there a reason you're calling fpu_xsave() directly here, rather than just calling vcpu_save_fpu()? That saves you actually doing the xsave if the fpu hasn't been modified since the last time you read it.
xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 120
Quoted 3 lines
xen/arch/x86/mm/guest_walk.c +3
-0+ xsave_addr =get_xsave_addr (vcpu->arch.xsave_area,fls64 (XSTATE_PKRU )-1 ); +if ( !!xsave_addr ) +memcpy (&pkru, xsave_addr,sizeof (pkru));
There's no need for the !! here. But in any case, isn't there a better function for reading the xsave state than manually calculating the address and doing a memcpy?
Quoted 13 lines
xen/arch/x86/mm/guest_walk.c +13
-0+ +if (unlikely (pkru) ) + { +/* +* PKU: additional mechanism by which the paging controls +* access to user-mode addresses based on the value in the +* PKRU register. A fault is considered as a PKU violation if all +* of the following conditions are ture: +* 1.CR4_PKE=1. +* 2.EFER_LMA=1. +* 3.page is present with no reserved bit violations. +* 4.the access is not an instruction fetch. +* 5.the access is to a user page.
xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 143
Quoted 10 lines
xen/arch/x86/mm/guest_walk.c +10
-0+* 6.PKRU.AD=1 +* or The access is a data write and PKRU.WD=1 +* and either CR0.WP=1 or it is a user access. +*/ + pkru_ad =read_pkru_ad (pkru, pte_pkeys); + pkru_wd =read_pkru_wd (pkru, pte_pkeys); +if (hvm_pku_enabled (vcpu) &&hvm_long_mode_enabled (vcpu) && + !rsvdf && !ff && (pkru_ad || + (pkru_wd && wf && (hvm_wp_enabled (vcpu) || uf)))) +return 1 ;
This statement here is really difficult to read. Why don't you put the checks which don't depend on the pkru up before you read it? e.g., hvm_pku_enabled(), hvm_long_mode_enabled(), rsvdf, ff, &c?
-George
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
Quoted 81 lines
-----Original Message-----
From: [email protected] [mailto:xen-devel- [email protected]] On Behalf Of George Dunlap
Sent: Friday, December 11, 2015 2:20 AM
To: Han, Huaitong <[email protected]>; [email protected]; [email protected]; Nakajima, Jun <[email protected]>; Dong, Eddie <[email protected]>; Tian, Kevin <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]
Subject: Re: [Xen-devel] [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support forguest_walk_tablesQuoted 6 lines · Huaitong Han
This patch adds pkeys support for
guest_walk_tables.Signed-off-by Huaitong Han <[email protected]>--- xen/arch/x86/i387.c | 2 +- xen/arch/x86/mm/guest_walk.c | 73+++++++++++++++++++++++++++++++++++++++++++
xen/arch/x86/i387.c static inline uint64_t vcpu_xsave_mask(const struct
Quoted 9 lines
xen/include/asm-x86/hvm/hvm.h | 2 ++ xen/include/asm-x86/i387.h | 1 + 4 files changed, 77 insertions(+), 1 deletion(-)xen/arch/x86/i387.c +0
-0@@ -132,7 +132,7 @@ static inline uint64_t vcpu_xsave_mask(const structvcpu *v)
xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p,
Quoted 22 lines
xen/arch/x86/i387.c +1
-1}/* Save x87 extended state */ -static inline void fpu_xsave (struct vcpu *v) +void fpu_xsave (struct vcpu *v) {bool_t ok;uint64_t mask =vcpu_xsave_mask (v);xen/arch/x86/mm/guest_walk.c +2
-0@@ -31,6 +31,8 @@ asm(".file \"" __OBJECT_FILE__ "\"");#include <xen/sched.h> #include <asm/page.h> #include <asm/guest_pt.h> +#include <asm/xstate.h> +#include <asm/i387.h> extern const uint32_t gw_page_flags[];#if GUEST_PAGING_LEVELS ==CONFIG_PAGING_LEVELS @@ -90,6 +92,61 @@ static uint32_t set_ad_bits(void *guest_p, void *walk_p,int
set_dirty)xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 97
Quoted 6 lines
xen/arch/x86/mm/guest_walk.c +3
-0return 0 ; } +#if GUEST_PAGING_LEVELS >=CONFIG_PAGING_LEVELS +bool_t leaf_pte_pkeys_check (struct vcpu *vcpu,uint32_t pfec, +uint32_t pte_access,uint32_t pte_pkeys)
pte_accessdoesn't seem to be used at all.xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 107
Quoted 10 lines
xen/arch/x86/mm/guest_walk.c +10
-0+{ +void *xsave_addr; +unsigned int pkru =0 ; +bool_t pkru_ad, pkru_wd; + +bool_t uf = !!(pfec & PFEC_user_mode); +bool_t wf = !!(pfec & PFEC_write_access); +bool_t ff = !!(pfec & PFEC_insn_fetch); +bool_t rsvdf = !!(pfec & PFEC_reserved_bit); +bool_t pkuf = !!(pfec & PFEC_prot_key);So I'm just wondering out loud here -- is there actually any situation in which we would want
guest_walk_tablesto act differently than the real hardware?
Quoted 8 lines
That is, is there actually any situation where, pku is enabled, the vcpu is in long mode,
PFEC_write_accessand/orPFEC_page_presentis set, and the pkey is non-zero, that we wantguest_walk_tables()to only check the write-protect bit for the pte, and not also check the pkru?Because if not, it seems like it would be much more robust to simply *always* check for
pkru_adifPFEC_page_presentis set, and forpkru_wdifPFEC_write_accessis set.
guest_walk_tables() is also used by shadow code, though we don't plan to support pkeys for shadow now, however, in that case, the 'pfec' is generated by hardware, and the pkuf bit may be 0 or 1 depending on the real page fault. If we unconditionally check pkeys in guest_walk_tables(), it is not a good ideas for someone who may want to implement the pkeys for shadow in future, since we only need to check pkeys when the pkuf is set in pfec.
Quoted 5 lines
Then in patch 8, you wouldn't need to go around all the
__hvm_copyfunctions adding inPFEC_prot; instead, you'd just need to addPFEC_insn_fetchto the "fetch" (as is already done for SMEP and NX), and you'd be done.
Pkeys has no business with instructions fetch, why do we need to add PFEC_insn_fetch?
Thanks,
Feng
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
Quoted 78 lines
Quoted 69 lines
Quoted 1 line · On 11.12.15 at 10:16, <[email protected]> wrote:
From: [email protected] [mailto:xen-devel- [email protected]] On Behalf Of George Dunlap
Sent: Friday, December 11, 2015 2:20 AMQuoted 6 lines · Huaitong Han
This patch adds pkeys support for
guest_walk_tables.Signed-off-by Huaitong Han <[email protected]>--- xen/arch/x86/i387.c | 2 +- xen/arch/x86/mm/guest_walk.c | 73+++++++++++++++++++++++++++++++++++++++++++
xen/arch/x86/i387.c static inline uint64_t vcpu_xsave_mask(const struct
Quoted 9 lines
xen/include/asm-x86/hvm/hvm.h | 2 ++ xen/include/asm-x86/i387.h | 1 + 4 files changed, 77 insertions(+), 1 deletion(-)xen/arch/x86/i387.c +0
-0@@ -132,7 +132,7 @@ static inline uint64_t vcpu_xsave_mask(const structvcpu *v)
xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p,
Quoted 22 lines
xen/arch/x86/i387.c +1
-1}/* Save x87 extended state */ -static inline void fpu_xsave (struct vcpu *v) +void fpu_xsave (struct vcpu *v) {bool_t ok;uint64_t mask =vcpu_xsave_mask (v);xen/arch/x86/mm/guest_walk.c +2
-0@@ -31,6 +31,8 @@ asm(".file \"" __OBJECT_FILE__ "\"");#include <xen/sched.h> #include <asm/page.h> #include <asm/guest_pt.h> +#include <asm/xstate.h> +#include <asm/i387.h> extern const uint32_t gw_page_flags[];#if GUEST_PAGING_LEVELS ==CONFIG_PAGING_LEVELS @@ -90,6 +92,61 @@ static uint32_t set_ad_bits(void *guest_p, void *walk_p,int
set_dirty)xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 97
Quoted 6 lines
xen/arch/x86/mm/guest_walk.c +3
-0return 0 ; } +#if GUEST_PAGING_LEVELS >=CONFIG_PAGING_LEVELS +bool_t leaf_pte_pkeys_check (struct vcpu *vcpu,uint32_t pfec, +uint32_t pte_access,uint32_t pte_pkeys)
pte_accessdoesn't seem to be used at all.xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 107
Quoted 10 lines
xen/arch/x86/mm/guest_walk.c +10
-0+{ +void *xsave_addr; +unsigned int pkru =0 ; +bool_t pkru_ad, pkru_wd; + +bool_t uf = !!(pfec & PFEC_user_mode); +bool_t wf = !!(pfec & PFEC_write_access); +bool_t ff = !!(pfec & PFEC_insn_fetch); +bool_t rsvdf = !!(pfec & PFEC_reserved_bit); +bool_t pkuf = !!(pfec & PFEC_prot_key);So I'm just wondering out loud here -- is there actually any situation in which we would want
guest_walk_tablesto act differently than the real hardware?Quoted 8 lines
That is, is there actually any situation where, pku is enabled, the vcpu is in long mode,
PFEC_write_accessand/orPFEC_page_presentis set, and the pkey is non-zero, that we wantguest_walk_tables()to only check the write-protect bit for the pte, and not also check the pkru?Because if not, it seems like it would be much more robust to simply *always* check for
pkru_adifPFEC_page_presentis set, and forpkru_wdifPFEC_write_accessis set.
Quoted 7 lines
guest_walk_tables()is also used by shadow code, though we don't plan to support pkeys for shadow now, however, in that case, the 'pfec' is generated by hardware, and the pkuf bit may be 0 or 1 depending on the real page fault. If we unconditionally check pkeys inguest_walk_tables(), it is not a good ideas for someone who may want to implement the pkeys for shadow in future, since we only need to check pkeys when the pkuf is set in pfec.
If shadow code isn't to support pkeys, how would an error code ever have the respective bit set?
Jan
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
In reply to Wu, Feng[Adding Tim, the previous mm maintainer]
Quoted 24 lines · Feng
Quoted 15 lines
xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 107
Quoted 10 lines
xen/arch/x86/mm/guest_walk.c +10
-0+{ +void *xsave_addr; +unsigned int pkru =0 ; +bool_t pkru_ad, pkru_wd; + +bool_t uf = !!(pfec & PFEC_user_mode); +bool_t wf = !!(pfec & PFEC_write_access); +bool_t ff = !!(pfec & PFEC_insn_fetch); +bool_t rsvdf = !!(pfec & PFEC_reserved_bit); +bool_t pkuf = !!(pfec & PFEC_prot_key);So I'm just wondering out loud here -- is there actually any situation in which we would want
guest_walk_tablesto act differently than the real hardware?Quoted 8 lines
That is, is there actually any situation where, pku is enabled, the vcpu is in long mode,
PFEC_write_accessand/orPFEC_page_presentis set, and the pkey is non-zero, that we wantguest_walk_tables()to only check the write-protect bit for the pte, and not also check the pkru?Because if not, it seems like it would be much more robust to simply *always* check for
pkru_adifPFEC_page_presentis set, and forpkru_wdifPFEC_write_accessis set.
Quoted 7 lines
guest_walk_tables()is also used by shadow code, though we don't plan to support pkeys for shadow now, however, in that case, the 'pfec' is generated by hardware, and the pkuf bit may be 0 or 1 depending on the real page fault. If we unconditionally check pkeys inguest_walk_tables(), it is not a good ideas for someone who may want to implement the pkeys for shadow in future, since we only need to check pkeys when the pkuf is set in pfec.
So you'll have to forgive me for being a bit slow here -- I stepped away from the mm code for a while, and a lot has changed since I agreed to become mm maintainer earlier this year.
So here is what I see in the tree; please correct me if I missed something important.
The function guest_walk_tables():
1. Accepts a pfec argument which is meant to describe the *access type*
that is happening
2. Returns a value with the "wrong" flags (i.e., flags which needed to
be set that were missing, or flags that needed to be clear that were set).
3. It checks reserved bits in the pagetable regardless of whetherPFEC_reserved_bit is set in the caller, and returns _PAGE_INVALID_BITS if so.
4. If PFEC_insn_fetch is set, it will only check for nx and smep if
those features are enabled for the guest.
The main callers are the various gva_to_gfn(), which accept a *pointer* to a pfec argument. This pointer is actually bidirectional: its value passed to guest_walk_tables() to determine what access types are checked; what is returned is meant to be a pfec value which can be passed to a guest as part of a fault (and is by several callers). But it may also return the Xen-internal flags, PFEC_page_{paged,shared}. And, importantly, it modifies the pfec value based on the bits that are returned from guest_walk_tables(): It will clear PFEC_present if guest_walk_tables() returns _PAGE_PRESENT, and it will set PFEC_reserved_bit if guest_walk_tables() returns _PAGE_INVALID_BIT.
The next logical level up are the hvm_{copy,fetch}_{to,from}_guest_virt(), which also take a pfec
argument: but really the only purpose of the pfec argument is to allow the caller to add the PFEC_user_mode flag; the other access-type flags are set automatically by the functions themselves (e.g., "to" sets PFEC_write_access, "fetch" sets PFEC_insn_fetch, &c). Several callers set PFEC_present as well, but callers set any other bits.
(hvm_fetch_from_guest_virt() seems to only set PFEC_insn_fetch if nx or smep are enabled in the guest. This seems inconsistent to me with the treatment of PFEC_reserved_bit: it seems like hvm_fetch_from_guest_virt() should always pass in PFEC_insn_fetch, particularly as guest_walk_tables() will already gate the checks based on whether nx or smep is enabled in the guest. Tim, you know of any reason for this?)
So there seems to me to be no reason to pass PFEC_prot_key into guest_walk_tables() or gva_to_gfn(). The pfec value passed *into* those should simply indicate the type of memory access being done: present, write, instruction fetch, user.
With your current series, guest_walk_tables() already checks for pkeys being enabled in the guest before checking for them in the pagetables. For shadow mode, these will be false, and so no checks will be done. If anyone ever implements pkeys for shadow mode, then these will be enabled, and the checks will be done, without any intervention on the part of the caller.
The only thing that's missing is:
1. for gva_to_gfn() to check for _PAGE_PKEY_BITS on the return from guest_walk_tables(), and set PFEC_prot_key if so
2. to set PFEC_insn_fetch when pkeys are enabled in hvm_fetch_from_guest_virt(), so that guest_walk_tables() knows that it's an instruction fetch.
Quoted 7 lines
Quoted 4 lines
Then in patch 8, you wouldn't need to go around all the
__hvm_copyfunctions adding inPFEC_prot; instead, you'd just need to addPFEC_insn_fetchto the "fetch" (as is already done for SMEP and NX), and you'd be done.Pkeys has no business with instructions fetch, why do we need to add
PFEC_insn_fetch?
PFEC_insn_fetch passed *into* guest_walk_tables() indicates that the access is an instruction fetch. Your code treats instruction fetches differently than normal reads, yes?
At the moment PFEC_insn_fetch is only set in hvm_fetch_from_guest_virt() if hvm_nx_enabled() or hvm_smep_enabled() are true. Which means that if you *don't* have nx or smep enabled, then the patch series as is will fault on instruction fetches when it shouldn't. (I don't remember anyone mentioning nx or smep being enabled as a prerequisite for pkeys.)
(And it seems to me that it would be better to always set PFEC_insn_fetch in hvm_fetch_from_guest_virt(), but that's a bit beyond the scope of what you're trying to accomplish here.)
-George
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
Hi,
Quoted 7 lines · George Dunlap
(
hvm_fetch_from_guest_virt()seems to only setPFEC_insn_fetchif nx or smep are enabled in the guest. This seems inconsistent to me with the treatment ofPFEC_reserved_bit: it seems likehvm_fetch_from_guest_virt()should always pass inPFEC_insn_fetch, particularly asguest_walk_tables()will already gate the checks based on whether nx or smep is enabled in the guest. Tim, you know of any reason for this?)
This code is following the hardware, IIRC: on real CPUs without NX enabled, pagefault error codes will not have that bit set even when caused by instruction fetches. Looking at the SDM (3A.4.7) the current rules are for that bit to be set iff ((PAE && NXE) || SMEP).
It seems unfortunate to have this check duplicated between guest_walk_tables() and its callers, so potentially g_w_t() should explicitly _clear_ PFEC_insn_fetch in cases where it should not be returned, and then its callers can set it unconditionally for all instruction fetches.
(There are similar rules for the new PK bit, which should not be shown to the guest if CR4.PKE is clear.)
Cheers,
Tim.
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
Quoted 10 lines · Tim Deegan
Hi,
Quoted 7 lines · George Dunlap
(
hvm_fetch_from_guest_virt()seems to only setPFEC_insn_fetchif nx or smep are enabled in the guest. This seems inconsistent to me with the treatment ofPFEC_reserved_bit: it seems likehvm_fetch_from_guest_virt()should always pass inPFEC_insn_fetch, particularly asguest_walk_tables()will already gate the checks based on whether nx or smep is enabled in the guest. Tim, you know of any reason for this?)
Quoted 3 lines
This code is following the hardware, IIRC: on real CPUs without NX enabled, pagefault error codes will not have that bit set even when caused by instruction fetches.
There is a difference between Intel and AMD. Intel has the bit reserved (and therefore faults), while AMD declares the bit as ignored (and doesn't fault).
These are yet more subtle differences which are not quite accounted for correctly in Xen (which I am frankly unlikely to even get to in cpuid v2).
~Andrew
Quoted 20 lines
Looking at the SDM (3A.4.7) the
current rules are for that bit to be set iff ((PAE && NXE) || SMEP).It seems unfortunate to have this check duplicated between
guest_walk_tables()and its callers, so potentiallyg_w_t()should explicitly _clear_PFEC_insn_fetchin cases where it should not be returned, and then its callers can set it unconditionally for all instruction fetches.(There are similar rules for the new PK bit, which should not be shown to the guest if CR4.PKE is clear.)
Cheers,
Tim.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
Quoted 11 lines · Andrew Cooper
Quoted 10 lines · Tim Deegan
Hi,
Quoted 7 lines · George Dunlap
(
hvm_fetch_from_guest_virt()seems to only setPFEC_insn_fetchif nx or smep are enabled in the guest. This seems inconsistent to me with the treatment ofPFEC_reserved_bit: it seems likehvm_fetch_from_guest_virt()should always pass inPFEC_insn_fetch, particularly asguest_walk_tables()will already gate the checks based on whether nx or smep is enabled in the guest. Tim, you know of any reason for this?)
Quoted 7 lines
Quoted 3 lines
This code is following the hardware, IIRC: on real CPUs without NX enabled, pagefault error codes will not have that bit set even when caused by instruction fetches.
There is a difference between Intel and AMD. Intel has the bit reserved (and therefore faults), while AMD declares the bit as ignored (and doesn't fault).
I think we are talking about different things. Like the U/S and W/R bits, the I/D bit of a PFEC describes what kind of access was being made, and is not affected by the contents of the pagetables at all. (cf. the P bit and the new PK bit, which do depend on PT contents).
When neither NX nor SMEP is enabled, we must not set the I/D bit in the PFEC we give to the guest, since real CPUs would not do so.
Cheers,
Tim.
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
In reply to Tim DeeganQuoted 11 lines · Tim Deegan
Hi,
Quoted 7 lines · George Dunlap
(
hvm_fetch_from_guest_virt()seems to only setPFEC_insn_fetchif nx or smep are enabled in the guest. This seems inconsistent to me with the treatment ofPFEC_reserved_bit: it seems likehvm_fetch_from_guest_virt()should always pass inPFEC_insn_fetch, particularly asguest_walk_tables()will already gate the checks based on whether nx or smep is enabled in the guest. Tim, you know of any reason for this?)
Quoted 4 lines
This code is following the hardware, IIRC: on real CPUs without NX enabled, pagefault error codes will not have that bit set even when caused by instruction fetches. Looking at the SDM (3A.4.7) the current rules are for that bit to be set iff ((PAE && NXE) || SMEP).
Right, but I think your answer points to the fundamental problem with the way this code has been written. The pfec value passed to gva_to_gfn() is used for two separate functions: 1) to tell guest_walk_tables() what kind of access is happening, so that it can check the appropriate bits 2) to be used as a pfec value to pass back to the guest when delivering a page fault.
These two functions should be clearly separated, but right now they're
not: hvm_fetch_from_guest_virt() is "pre-clearing" PFEC_insn_fetch, so that guest_walk_tables() doesn't even know that an instruction fetch is happening; as such, it's likely that the pkey code in this series will DTWT (fault on an instruction fetch) if pkeys are enabled but nx and smep are not.
Really those should be two separate parameters, one in and one out; and it should be gva_to_gfn()'s job to translate the missing bits from guest_walk_tables() into a pfec value suitable to use when injecting a fault. (Or potentially guest_walk_tables()'s job.)
-George
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
Quoted 17 lines · George Dunlap
Quoted 11 lines · Tim Deegan
Hi,
Quoted 7 lines · George Dunlap
(
hvm_fetch_from_guest_virt()seems to only setPFEC_insn_fetchif nx or smep are enabled in the guest. This seems inconsistent to me with the treatment ofPFEC_reserved_bit: it seems likehvm_fetch_from_guest_virt()should always pass inPFEC_insn_fetch, particularly asguest_walk_tables()will already gate the checks based on whether nx or smep is enabled in the guest. Tim, you know of any reason for this?)Quoted 4 lines
This code is following the hardware, IIRC: on real CPUs without NX enabled, pagefault error codes will not have that bit set even when caused by instruction fetches. Looking at the SDM (3A.4.7) the current rules are for that bit to be set iff ((PAE && NXE) || SMEP).
Quoted 6 lines
Right, but I think your answer points to the fundamental problem with the way this code has been written. The pfec value passed to
gva_to_gfn()is used for two separate functions: 1) to tellguest_walk_tables()what kind of access is happening, so that it can check the appropriate bits 2) to be used as a pfec value to pass back to the guest when delivering a page fault.
Yes. At the time, this made sense: the caller and the walker were collectively assembling the PFEC, with the caller providing bits that depended on the kind of access and the walker managing the bits that depended on the pagetables. We could have had a separate flags parameter to describe (read/write/fetch X user/supervisor), but that would by construction have been identical to the inital set of PFEC bits.
Now that we have a potential case where the hardware behaves differently for instruction fetches but _doesn't_ flag them in the PFEC, then we clearly need to track them separately.
Quoted 4 lines
Really those should be two separate parameters, one in and one out; and it should be gva_to_gfn()'s job to translate the missing bits from
guest_walk_tables()into a pfec value suitable to use when injecting a fault. (Or potentially guest_walk_tables()'s job.)
Agreed.
Tim.
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
In reply to George DunlapQuoted 109 lines · George Dunlap
[Adding Tim, the previous mm maintainer]
Quoted 31 lines · Feng
Quoted 25 lines
xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 107
Quoted 10 lines
xen/arch/x86/mm/guest_walk.c +10
-0+{ +void *xsave_addr; +unsigned int pkru =0 ; +bool_t pkru_ad, pkru_wd; + +bool_t uf = !!(pfec & PFEC_user_mode); +bool_t wf = !!(pfec & PFEC_write_access); +bool_t ff = !!(pfec & PFEC_insn_fetch); +bool_t rsvdf = !!(pfec & PFEC_reserved_bit); +bool_t pkuf = !!(pfec & PFEC_prot_key);So I'm just wondering out loud here -- is there actually any situation
in which we would wantguest_walk_tablesto act differently than the
real hardware?That is, is there actually any situation where, pku is enabled, the vcpu
is in long mode,PFEC_write_accessand/orPFEC_page_presentis set, and
the pkey is non-zero, that we wantguest_walk_tables()to only check the
write-protect bit for the pte, and not also check the pkru?Quoted 5 lines
Because if not, it seems like it would be much more robust to simply
*always* check forpkru_adifPFEC_page_presentis set, and forpkru_wd
ifPFEC_write_accessis set.Quoted 8 lines
guest_walk_tables()is also used by shadow code, though we don't plan to support pkeys for shadow now, however, in that case, the 'pfec'
is generated by hardware, and the pkuf bit may be 0 or 1 depending on the real page fault. If we unconditionally check pkeys inguest_walk_tables(), it is not a good ideas for someone who may want to implement the pkeys for shadow in future, since we only need to check pkeys when the pkuf is set in pfec.So you'll have to forgive me for being a bit slow here -- I stepped away
from the mm code for a while, and a lot has changed since I agreed to become mm maintainer earlier this year.So here is what I see in the tree; please correct me if I missed something important.
The function
guest_walk_tables():
1. Accepts a pfec argument which is meant to describe the *access
type*
that is happening
2. Returns a value with the "wrong" flags (i.e., flags which needed
to
be set that were missing, or flags that needed to be clear that were set).
3. It checks reserved bits in the pagetable regardless of whetherPFEC_reserved_bitis set in the caller, and returns_PAGE_INVALID_BITS
if so.
4. IfPFEC_insn_fetchis set, it will only check for nx and smep if
those features are enabled for the guest.The main callers are the various
gva_to_gfn(), which accept a *pointer*
to a pfec argument. This pointer is actually bidirectional: its value
passed toguest_walk_tables()to determine what access types are checked; what is returned is meant to be a pfec value which can be passed to a guest as part of a fault (and is by several callers).
But
it may also return the Xen-internal flags, PFEC_page_{paged,shared}. And, importantly, it modifies the pfec value based on the bits that are
returned fromguest_walk_tables(): It will clearPFEC_presentifguest_walk_tables()returns_PAGE_PRESENT, and it will setPFEC_reserved_bitifguest_walk_tables()returns_PAGE_INVALID_BIT.The next logical level up are the hvm_{copy,fetch}_{to,from}_guest_virt(), which also take a pfec
argument: but really the only purpose of the pfec argument is to allow
the caller to add thePFEC_user_modeflag; the other access-type flags
are set automatically by the functions themselves (e.g., "to" setsPFEC_write_access, "fetch" setsPFEC_insn_fetch, &c). Several callers
setPFEC_presentas well, but callers set any other bits.(
hvm_fetch_from_guest_virt()seems to only setPFEC_insn_fetchif nx or
smep are enabled in the guest. This seems inconsistent to me with the
treatment ofPFEC_reserved_bit: it seems likehvm_fetch_from_guest_virt()should always pass inPFEC_insn_fetch, particularly asguest_walk_tables()will already gate the checks based
on whether nx or smep is enabled in the guest. Tim, you know of any reason for this?)So there seems to me to be no reason to pass
PFEC_prot_keyintoguest_walk_tables()orgva_to_gfn(). The pfec value passed *into* those
should simply indicate the type of memory access being done: present, write, instruction fetch, user.
Quoted 9 lines
With your current series,
guest_walk_tables()already checks for pkeys
being enabled in the guest before checking for them in the pagetables.
For shadow mode, these will be false, and so no checks will be done.
If
anyone ever implements pkeys for shadow mode, then these will be enabled, and the checks will be done, without any intervention on the part of the caller.
I have understood it, but, the problem with shadow mode is that pfec may come from regs->error_code(hardware), just like:
rc = sh_walk_guest_tables(v, va, &gw, regs->error_code); so, when regs->error_code does not have PFEC_prot_key, guest_walk_tables may still check PKEY when codes is writen according to what you said, and it maybe return a different result.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
Quoted 120 lines · Huaitong
Quoted 109 lines · George Dunlap
[Adding Tim, the previous mm maintainer]
Quoted 31 lines · Feng
Quoted 25 lines
xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 107
Quoted 10 lines
xen/arch/x86/mm/guest_walk.c +10
-0+{ +void *xsave_addr; +unsigned int pkru =0 ; +bool_t pkru_ad, pkru_wd; + +bool_t uf = !!(pfec & PFEC_user_mode); +bool_t wf = !!(pfec & PFEC_write_access); +bool_t ff = !!(pfec & PFEC_insn_fetch); +bool_t rsvdf = !!(pfec & PFEC_reserved_bit); +bool_t pkuf = !!(pfec & PFEC_prot_key);So I'm just wondering out loud here -- is there actually any situation
in which we would wantguest_walk_tablesto act differently than the
real hardware?That is, is there actually any situation where, pku is enabled, the vcpu
is in long mode,PFEC_write_accessand/orPFEC_page_presentis set, and
the pkey is non-zero, that we wantguest_walk_tables()to only check the
write-protect bit for the pte, and not also check the pkru?Quoted 5 lines
Because if not, it seems like it would be much more robust to simply
*always* check forpkru_adifPFEC_page_presentis set, and forpkru_wd
ifPFEC_write_accessis set.Quoted 8 lines
guest_walk_tables()is also used by shadow code, though we don't plan to support pkeys for shadow now, however, in that case, the 'pfec'
is generated by hardware, and the pkuf bit may be 0 or 1 depending on the real page fault. If we unconditionally check pkeys inguest_walk_tables(), it is not a good ideas for someone who may want to implement the pkeys for shadow in future, since we only need to check pkeys when the pkuf is set in pfec.So you'll have to forgive me for being a bit slow here -- I stepped away
from the mm code for a while, and a lot has changed since I agreed to become mm maintainer earlier this year.So here is what I see in the tree; please correct me if I missed something important.
The function
guest_walk_tables():
1. Accepts a pfec argument which is meant to describe the *access
type*
that is happening
2. Returns a value with the "wrong" flags (i.e., flags which needed
to
be set that were missing, or flags that needed to be clear that were set).
3. It checks reserved bits in the pagetable regardless of whetherPFEC_reserved_bitis set in the caller, and returns_PAGE_INVALID_BITS
if so.
4. IfPFEC_insn_fetchis set, it will only check for nx and smep if
those features are enabled for the guest.The main callers are the various
gva_to_gfn(), which accept a *pointer*
to a pfec argument. This pointer is actually bidirectional: its value
passed toguest_walk_tables()to determine what access types are checked; what is returned is meant to be a pfec value which can be passed to a guest as part of a fault (and is by several callers).
But
it may also return the Xen-internal flags, PFEC_page_{paged,shared}. And, importantly, it modifies the pfec value based on the bits that are
returned fromguest_walk_tables(): It will clearPFEC_presentifguest_walk_tables()returns_PAGE_PRESENT, and it will setPFEC_reserved_bitifguest_walk_tables()returns_PAGE_INVALID_BIT.The next logical level up are the hvm_{copy,fetch}_{to,from}_guest_virt(), which also take a pfec
argument: but really the only purpose of the pfec argument is to allow
the caller to add thePFEC_user_modeflag; the other access-type flags
are set automatically by the functions themselves (e.g., "to" setsPFEC_write_access, "fetch" setsPFEC_insn_fetch, &c). Several callers
setPFEC_presentas well, but callers set any other bits.(
hvm_fetch_from_guest_virt()seems to only setPFEC_insn_fetchif nx or
smep are enabled in the guest. This seems inconsistent to me with the
treatment ofPFEC_reserved_bit: it seems likehvm_fetch_from_guest_virt()should always pass inPFEC_insn_fetch, particularly asguest_walk_tables()will already gate the checks based
on whether nx or smep is enabled in the guest. Tim, you know of any reason for this?)So there seems to me to be no reason to pass
PFEC_prot_keyintoguest_walk_tables()orgva_to_gfn(). The pfec value passed *into* those
should simply indicate the type of memory access being done: present, write, instruction fetch, user.Quoted 9 lines
With your current series,
guest_walk_tables()already checks for pkeys
being enabled in the guest before checking for them in the pagetables.
For shadow mode, these will be false, and so no checks will be done.
If
anyone ever implements pkeys for shadow mode, then these will be enabled, and the checks will be done, without any intervention on the part of the caller.
Quoted 6 lines
I have understood it, but, the problem with shadow mode is that pfec may come from regs->error_code(hardware), just like:
rc =sh_walk_guest_tables(v, va, &gw, regs->error_code); so, whenregs->error_codedoes not havePFEC_prot_key,guest_walk_tablesmay still check PKEY when codes is writen according to what you said, and it maybe return a different result.
Under what situation would the *hardware* not generate PFEC_prot_key, but guest_walk_tables() would, under exactly the same conditions, generate PFEC_prot_key? Shouldn't guest_walk_tables() be made to behave identically to the hardware?
The only situation where that might legitimately happen is when the shadow pagetables and the guest pagetables diverge; for instance, when a page had been write-protected because Xen thought it was a pagetable. Then the guest pte might have write permission, but the pkey read-only permission; but the shadow pte would not have write permission. In that case, the hardware might give PFEC_write_access but not PFEC_prot_key, I suppose (haven't read the manual to be sure); but in that case, we *want* guest_walk_tables() to detect and add PFEC_prot_key, don't we?
-George
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
In reply to Han, HuaitongQuoted 11 lines · Huaitong
Quoted 9 lines · George Dunlap
With your current series,
guest_walk_tables()already checks for pkeys
being enabled in the guest before checking for them in the pagetables.
For shadow mode, these will be false, and so no checks will be done.
If
anyone ever implements pkeys for shadow mode, then these will be enabled, and the checks will be done, without any intervention on the part of the caller.
Quoted 6 lines
I have understood it, but, the problem with shadow mode is that pfec may come from regs->error_code?hardware?, just like:
rc =sh_walk_guest_tables(v, va, &gw, regs->error_code); so, whenregs->error_codedoes not havePFEC_prot_key,guest_walk_tablesmay still check PKEY when codes is writen according to what you said, and it maybe return a different result.
That's OK -- in general there's no guarantee that the guest walk will produce the same result as the original hardware fault, since the guest may have modified its pagetables.
So flagging something as a pkey error when the original fault didn't is OK. OTOH, we shouldn't keep a hardware-supplied pkey bit in the PFEC unless the guest walk finds a reason for it to be there. IOW, we should treat it like the PFEC_present bit and have it depend only on the walk (or go straight to George's plan of separating inputs from outputs so that's not an issue).
Cheers,
Tim.
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
In reply to George DunlapQuoted 25 lines · George Dunlap
xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 107
Quoted 10 lines · Huaitong Han
xen/arch/x86/mm/guest_walk.c +10
-0+{ +void *xsave_addr; +unsigned int pkru =0 ; +bool_t pkru_ad, pkru_wd; + +bool_t uf = !!(pfec & PFEC_user_mode); +bool_t wf = !!(pfec & PFEC_write_access); +bool_t ff = !!(pfec & PFEC_insn_fetch); +bool_t rsvdf = !!(pfec & PFEC_reserved_bit); +bool_t pkuf = !!(pfec & PFEC_prot_key);So I'm just wondering out loud here -- is there actually any situation
in which we would wantguest_walk_tablesto act differently than the real hardware?That is, is there actually any situation where, pku is enabled, the vcpu
is in long mode,PFEC_write_accessand/orPFEC_page_presentis set, and
the pkey is non-zero, that we wantguest_walk_tables()to only check the
write-protect bit for the pte, and not also check the pkru?
Quoted 9 lines
Because if not, it seems like it would be much more robust to simply *always* check for
pkru_adifPFEC_page_presentis set, and forpkru_wd
ifPFEC_write_accessis set.
Then in patch 8, you wouldn't need to go around all the__hvm_copyfunctions adding inPFEC_prot; instead, you'd just need to addPFEC_insn_fetchto the "fetch" (as is already done for SMEP and NX), and
you'd be done.
See reply email from Feng discussed with me.
Quoted 16 lines
xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 112
Quoted 5 lines
xen/arch/x86/mm/guest_walk.c +5
-0+ +if ( !cpu_has_xsave || !pkuf ||is_pv_vcpu (vcpu) ) +return 0 ; + +/* PKRU dom0 is always zero */ "dom0" has a very specific meaning in Xen. I think this would be better
written "pkey 0 always has full access".xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 117
Quoted 5 lines
xen/arch/x86/mm/guest_walk.c +5
-0+if (likely (!pte_pkeys) ) +return 0 ; + +/* Update vcpu xsave area */ +fpu_xsave (vcpu);
Quoted 5 lines
Is there a reason you're calling
fpu_xsave()directly here, rather than
just callingvcpu_save_fpu()? That saves you actually doing the xsave
if the fpu hasn't been modified since the last time you read it.
use fpu_xsave instead of fpu_xsave because Jan's comment:
Which is bogus by itself: That function isn't meant to be used for purposes like the one you have, e.g. due to its side effect of clearing ->fpu_dirtied. You really ought to be using a lower level function here (and I don't think the corresponding struct vcpu should get altered in any way). --Jan
And I can add
if ( !vcpu->fpu_dirtied )
before fpu_xsave(vcpu);
Quoted 9 lines
xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 120
Quoted 4 lines
+
xsave_addr= get_xsave_addr(vcpu->arch.xsave_area,
fls64(XSTATE_PKRU)-1);xen/arch/x86/mm/guest_walk.c +2
-0+if ( !!xsave_addr ) +memcpy (&pkru, xsave_addr,sizeof (pkru));There's no need for the !! here. But in any case, isn't there a better
function for reading the xsave state than manually calculating the address and doing a memcpy?
RDPKRU is disabled by hypervisor CR4 because PV mode must disable CR4.PKE, getting PKRU value only depends on xsave.
Quoted 32 lines
Quoted 22 lines
xen/arch/x86/mm/guest_walk.c +7
-0+ +if (unlikely (pkru) ) + { +/* +* PKU: additional mechanism by which the paging controls +* access to user-mode addresses based on the value in the +* PKRU register. A fault is considered as a PKU violation if all
xen/arch/x86/mm/guest_walk.c +13
-0+* of the following conditions are ture: +* 1.CR4_PKE=1. +* 2.EFER_LMA=1. +* 3.page is present with no reserved bit violations. +* 4.the access is not an instruction fetch. +* 5.the access is to a user page. +* 6.PKRU.AD=1 +* or The access is a data write and PKRU.WD=1 +* and either CR0.WP=1 or it is a user access. +*/ + pkru_ad =read_pkru_ad (pkru, pte_pkeys); + pkru_wd =read_pkru_wd (pkru, pte_pkeys); + if ( hvm_pku_enabled(vcpu) && hvm_long_mode_enabled(vcpu)&&
xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 143
Quoted 3 lines
xen/arch/x86/mm/guest_walk.c +3
-0+ !rsvdf && !ff && (pkru_ad || + (pkru_wd && wf && (hvm_wp_enabled(vcpu) || uf)))) +return 1 ;This statement here is really difficult to read. Why don't you put the
checks which don't depend on the pkru up before you read it? e.g.,hvm_pku_enabled(),hvm_long_mode_enabled(), rsvdf, ff, &c?-George
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 11.12.15 at 10:23, <[email protected]> wrote:
Quoted 7 lines · George Dunlap
xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 117
Quoted 5 lines · Huaitong Han
xen/arch/x86/mm/guest_walk.c +5
-0+if (likely (!pte_pkeys) ) +return 0 ; + +/* Update vcpu xsave area */ +fpu_xsave (vcpu);Quoted 5 lines
Is there a reason you're calling
fpu_xsave()directly here, rather than
just callingvcpu_save_fpu()? That saves you actually doing the xsave
if the fpu hasn't been modified since the last time you read it.use
fpu_xsaveinstead offpu_xsavebecause Jan's comment:
Which is bogus by itself: That function isn't meant to be used for purposes like the one you have, e.g. due to its side effect of clearing ->fpu_dirtied. You really ought to be using a lower level function here (and I don't think the corresponding struct vcpu should get altered in any way). --Jan
Quoted 3 lines
And I can add
if ( !vcpu->fpu_dirtied )before
fpu_xsave(vcpu);
PKRU being non-lazy state, how would fpu_dirtied matter here?
Jan
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
In reply to George DunlapQuoted 11 lines
Quoted 1 line
Quoted 1 line · On 10.12.15 at 19:19, <[email protected]> wrote:
xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 117
Quoted 5 lines · Huaitong Han
xen/arch/x86/mm/guest_walk.c +5
-0+if (likely (!pte_pkeys) ) +return 0 ; + +/* Update vcpu xsave area */ +fpu_xsave (vcpu);Is there a reason you're calling
fpu_xsave()directly here, rather than just callingvcpu_save_fpu()? That saves you actually doing the xsave if the fpu hasn't been modified since the last time you read it.
I've already said on an earlier version that wholesale saving of the entire XSAVE state is wrong here. It should just be the single piece that we're actually interested in, and it quite likely shouldn't go into struct vcpu (but e.g. into a local buffer).
Jan
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
Quoted 14 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 10.12.15 at 19:19, <[email protected]> wrote:
xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 117
Quoted 5 lines · Huaitong Han
xen/arch/x86/mm/guest_walk.c +5
-0+if (likely (!pte_pkeys) ) +return 0 ; + +/* Update vcpu xsave area */ +fpu_xsave (vcpu);Quoted 5 lines
Is there a reason you're calling
fpu_xsave()directly here, rather than
just callingvcpu_save_fpu()? That saves you actually doing the xsave
if the fpu hasn't been modified since the last time you read it.
Quoted 5 lines
I've already said on an earlier version that wholesale saving of the entire XSAVE state is wrong here. It should just be the single piece that we're actually interested in, and it quite likely shouldn't go into
struct vcpu (but e.g. into a local buffer).
The comments on V2 version said using vcpu_save_fpu is wrong because of
v->fpu_dirtied, but why wholesale saving of the entire XSAVE state is
wrong here? I understand xsave maybe cost a little more. But if we just save the single piece, many functions are not reused because xstate_comp_offsets is pointless, and we need add a new function as follow that looks not good:
(just a example):
unsigned int get_xsave_pkru(void)
{
struct xsave_struct *xstate;
void *xsave_addr;
unsigned int pkru = 0;
xstate = _xzalloc(xsave_cntxt_size, 64);
if ( xstate == NULL )
return -ENOMEM;
asm volatile ( ".byte 0x0f,0xae,0x27" /* XSAVE */
: "=m" (*xstate)
: "a" (XSTATE_PKRU), "d" (0), "D" (xstate) );
xsave_addr = get_xsave_addr(xstate, fls64(XSTATE_PKRU)-1);
if ( xsave_addr )
memcpy(&pkru, xsave_addr, sizeof(pkru));
xfree(xstate);
return pkru;
}Thanks a lot.
Huaitong
Quoted 3 lines
Jan
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 15.12.15 at 09:14, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 10.12.15 at 19:19, <[email protected]> wrote:
xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 117
Quoted 5 lines · Huaitong Han
xen/arch/x86/mm/guest_walk.c +5
-0+if (likely (!pte_pkeys) ) +return 0 ; + +/* Update vcpu xsave area */ +fpu_xsave (vcpu);Quoted 5 lines
Is there a reason you're calling
fpu_xsave()directly here, rather than
just callingvcpu_save_fpu()? That saves you actually doing the xsave
if the fpu hasn't been modified since the last time you read it.Quoted 5 lines
I've already said on an earlier version that wholesale saving of the entire XSAVE state is wrong here. It should just be the single piece that we're actually interested in, and it quite likely shouldn't go into
struct vcpu (but e.g. into a local buffer).
Quoted 3 lines
The comments on V2 version said using
vcpu_save_fpuis wrong because of
v->fpu_dirtied, but why wholesale saving of the entire XSAVE state is
wrong here? I understand xsave maybe cost a little more.
"A little" is quite a bit of an understatement.
Quoted 4 lines
But if we just
save the single piece, many functions are not reused becausexstate_comp_offsetsis pointless, and we need add a new function as follow that looks not good:
Well, I wouldn't want you to introduce a brand new function, but instead just factor out the necessary piece from xsave() (making the new one take a struct xsave_struct * instead of a struct vcpu *, and calling it from what is now xsave()).
Jan
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
Quoted 41 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 15.12.15 at 09:14, <[email protected]> wrote:
Quoted 17 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 10.12.15 at 19:19, <[email protected]> wrote:
xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 117
Quoted 5 lines · Huaitong Han
xen/arch/x86/mm/guest_walk.c +5
-0+if (likely (!pte_pkeys) ) +return 0 ; + +/* Update vcpu xsave area */ +fpu_xsave (vcpu);Quoted 8 lines
Is there a reason you're calling
fpu_xsave()directly here, rather
than
just callingvcpu_save_fpu()? That saves you actually doing the
xsave
if the fpu hasn't been modified since the last time you read it.Quoted 8 lines
I've already said on an earlier version that wholesale saving of the
entire XSAVE state is wrong here. It should just be the single piece
that we're actually interested in, and it quite likely shouldn't go
into
struct vcpu (but e.g. into a local buffer).Quoted 5 lines
The comments on V2 version said using
vcpu_save_fpuis wrong because of
v->fpu_dirtied, but why wholesale saving of the entire XSAVE state
is
wrong here? I understand xsave maybe cost a little more."A little" is quite a bit of an understatement.
Quoted 4 lines
But if we just
save the single piece, many functions are not reused becausexstate_comp_offsetsis pointless, and we need add a new function as follow that looks not good:
Quoted 4 lines
Well, I wouldn't want you to introduce a brand new function, but instead just factor out the necessary piece from
xsave()(making the new one take a structxsave_struct* instead of a struct vcpu *, and calling it from what is nowxsave()).
So the function looks like this:
unsigned int get_xsave_pkru(struct vcpu *v)
{
void *offset;
struct xsave_struct *xsave_area;
uint64_t mask = XSTATE_PKRU;
unsigned int index = fls64(mask) - 1;
unsigned int pkru = 0;
if ( !cpu_has_xsave )
return 0;
BUG_ON(xsave_cntxt_size < XSTATE_AREA_MIN_SIZE);
xsave_area = _xzalloc(xsave_cntxt_size, 64);
if ( xsave_area == NULL )
return 0;
xsave(xsave_area, mask);
offset = (void *)xsave_area + (xsave_area_compressed(xsave) ?
XSTATE_AREA_MIN_SIZE : xstate_offsets[index] );
memcpy(&pkru, offset, sizeof(pkru));
xfree(xsave_area);
return pkru;
}Quoted 3 lines
Jan
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 16.12.15 at 09:16, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich
Well, I wouldn't want you to introduce a brand new function, but instead just factor out the necessary piece from
xsave()(making the new one take a structxsave_struct* instead of a struct vcpu *, and calling it from what is nowxsave()).So the function looks like this:
unsigned intget_xsave_pkru(struct vcpu *v){ void *offset; struct xsave_struct *xsave_area; uint64_t mask = XSTATE_PKRU; unsigned int index = fls64(mask) - 1; unsigned int pkru = 0; if ( !cpu_has_xsave ) return 0; BUG_ON(xsave_cntxt_size < XSTATE_AREA_MIN_SIZE); xsave_area = _xzalloc(xsave_cntxt_size, 64); if ( xsave_area == NULL ) return 0; xsave(xsave_area, mask);
Quoted 8 lines
offset = (void *)xsave_area + (xsave_area_compressed(xsave) ?
XSTATE_AREA_MIN_SIZE : xstate_offsets[index] );
memcpy(&pkru, offset, sizeof(pkru));
xfree(xsave_area);
return pkru;
}Depending on how frequently this might get called, the allocation overhead may not be tolerable. I.e. you may want to set up e.g. a per-CPU buffer up front. Or you check whether using RDPKRU (with temporarily setting CR4.PKE) is cheaper than what you do right now.
Also I don't think the buffer needs to be xsave_cntxt_size in size; xstate_offsets[index] + sizeof(pkru) (and its equivalent in the non-compressed case) should suffice.
And finally I don't think returning 0 in the allocation failure case would be valid, as that - iiuc - means no restrictions at all, and hence would hamper security inside the guest. But that's of course moot if the allocation gets moved out of here.
Jan
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
Quoted 31 lines · Jan Beulich
Quoted 31 lines
Quoted 1 line
Quoted 1 line · On 16.12.15 at 09:16, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Well, I wouldn't want you to introduce a brand new function, but instead just factor out the necessary piece from
xsave()(making the new one take a structxsave_struct* instead of a struct vcpu *,
and calling it from what is nowxsave()).So the function looks like this:
unsigned intget_xsave_pkru(struct vcpu *v){ void *offset; struct xsave_struct *xsave_area; uint64_t mask = XSTATE_PKRU; unsigned int index = fls64(mask) - 1; unsigned int pkru = 0; if ( !cpu_has_xsave ) return 0; BUG_ON(xsave_cntxt_size < XSTATE_AREA_MIN_SIZE); xsave_area = _xzalloc(xsave_cntxt_size, 64); if ( xsave_area == NULL ) return 0; xsave(xsave_area, mask); offset = (void *)xsave_area + (xsave_area_compressed(xsave) ? XSTATE_AREA_MIN_SIZE : xstate_offsets[index] ); memcpy(&pkru, offset, sizeof(pkru)); xfree(xsave_area);
Quoted 8 lines
Quoted 2 lines
return pkru; }Depending on how frequently this might get called, the allocation overhead may not be tolerable. I.e. you may want to set up e.g. a per-CPU buffer up front. Or you check whether using RDPKRU (with temporarily setting CR4.PKE) is cheaper than what you do right now.
RDPKRU does cost less than the function, and if temporarily setting CR4.PKE is accepted, I will use RDPKRU instead of the function.
Andrew, what is your opinion?
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
Quoted 36 lines
Quoted 1 line
Quoted 1 line · On 16.12.15 at 10:03, <[email protected]> wrote:
Quoted 34 lines · Jan Beulich
Quoted 33 lines
Quoted 1 line
Quoted 1 line · On 16.12.15 at 09:16, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Well, I wouldn't want you to introduce a brand new function, but instead just factor out the necessary piece from
xsave()(making the new one take a structxsave_struct* instead of a struct vcpu *,
and calling it from what is nowxsave()).So the function looks like this:
unsigned intget_xsave_pkru(struct vcpu *v){ void *offset; struct xsave_struct *xsave_area; uint64_t mask = XSTATE_PKRU; unsigned int index = fls64(mask) - 1; unsigned int pkru = 0; if ( !cpu_has_xsave ) return 0; BUG_ON(xsave_cntxt_size < XSTATE_AREA_MIN_SIZE); xsave_area = _xzalloc(xsave_cntxt_size, 64); if ( xsave_area == NULL ) return 0; xsave(xsave_area, mask); offset = (void *)xsave_area + (xsave_area_compressed(xsave) ? XSTATE_AREA_MIN_SIZE : xstate_offsets[index] ); memcpy(&pkru, offset, sizeof(pkru)); xfree(xsave_area); return pkru; }
Quoted 7 lines
Quoted 5 lines
Depending on how frequently this might get called, the allocation overhead may not be tolerable. I.e. you may want to set up e.g. a per-CPU buffer up front. Or you check whether using RDPKRU (with temporarily setting CR4.PKE) is cheaper than what you do right now.
RDPKRU does cost less than the function, and if temporarily setting CR4.PKE is accepted, I will use RDPKRU instead of the function.
The question isn't just the RDPKRU cost, but that of the two CR4 writes.
Jan
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
Quoted 45 lines · Jan Beulich
Quoted 45 lines
Quoted 1 line
Quoted 1 line · On 16.12.15 at 10:03, <[email protected]> wrote:
Quoted 43 lines · Jan Beulich
Quoted 29 lines
Quoted 1 line
Quoted 1 line · On 16.12.15 at 09:16, <[email protected]> wrote:
Quoted 8 lines · Jan Beulich
Well, I wouldn't want you to introduce a brand new function, but
instead just factor out the necessary piece fromxsave()(making
the new one take a structxsave_struct* instead of a struct vcpu
*,
and calling it from what is nowxsave()).So the function looks like this:
unsigned intget_xsave_pkru(struct vcpu *v){ void *offset; struct xsave_struct *xsave_area; uint64_t mask = XSTATE_PKRU; unsigned int index = fls64(mask) - 1; unsigned int pkru = 0; if ( !cpu_has_xsave ) return 0; BUG_ON(xsave_cntxt_size < XSTATE_AREA_MIN_SIZE); xsave_area = _xzalloc(xsave_cntxt_size, 64); if ( xsave_area == NULL ) return 0; xsave(xsave_area, mask); offset = (void *)xsave_area + (xsave_area_compressed(xsave)Quoted 8 lines
?
XSTATE_AREA_MIN_SIZE : xstate_offsets[index] ); memcpy(&pkru, offset, sizeof(pkru)); xfree(xsave_area); return pkru; }Depending on how frequently this might get called, the allocation overhead may not be tolerable. I.e. you may want to set up e.g. a per-CPU buffer up front. Or you check whether using RDPKRU (with temporarily setting CR4.PKE) is cheaper than what you do right now.
Quoted 5 lines
Quoted 2 lines
RDPKRU does cost less than the function, and if temporarily setting CR4.PKE is accepted, I will use RDPKRU instead of the function.
The question isn't just the RDPKRU cost, but that of the two CR4 writes.
Testing result with NOW() function:
Time of the function 10 times execution
(XEN)xsave time is 1376 ns
(XEN)read_pkru time is 28 ns
So, read_pkru function does cost less than get_xsave_pkru, and I will use read_pkru.
Testing codes:
static void reboot_machine(unsigned char key, struct cpu_user_regs
*regs)
{
- printk("'%c' pressed -> rebooting machine\n", key);
- machine_restart(0);
+ // printk("read pkru test\n", key);
+ s_time_t pre, last;
+ unsigned int pkru = 0;
+ unsigned int i = 0;
+ void * offset;
+ struct xsave_struct *save_area = _xzalloc(1024, 64);
+
+ pre = NOW();
+ for (;i< 10;i++){
+ xsave(current, XSTATE_PKRU, save_area);
+ offset = (void *)save_area + XSTATE_AREA_MIN_SIZE;
+ memcpy(&pkru, offset, sizeof(pkru));}
+ last = NOW();
+ printk("xsave time is %lu\n", (last - pre));
+
+ pre = NOW();
+ for (;i< 10;i++){
+ pkru = read_pkru();
+ }
+ last = NOW();
+ printk("read_pkru time is %lu\n", (last - pre));+ // machine_restart(0);
}
static inline unsigned int read_pkru(void)
{
unsigned int pkru;
set_in_cr4(X86_CR4_PKE);
asm volatile (".byte 0x0f,0x01,0xee"
: "=a" (pkru) : "c" (0) : "dx");
clear_in_cr4(X86_CR4_PKE);
return pkru;
}Quoted 3 lines
Jan
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 17.12.15 at 10:18, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 16.12.15 at 10:03, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Depending on how frequently this might get called, the allocation overhead may not be tolerable. I.e. you may want to set up e.g. a per-CPU buffer up front. Or you check whether using RDPKRU (with temporarily setting CR4.PKE) is cheaper than what you do right now.
RDPKRU does cost less than the function, and if temporarily setting CR4.PKE is accepted, I will use RDPKRU instead of the function.
Quoted 6 lines
Quoted 2 lines
The question isn't just the RDPKRU cost, but that of the two CR4 writes.
Testing result with
NOW()function:
Time of the function 10 times execution
(XEN)xsave time is 1376 ns
(XEN)read_pkru time is 28 ns
Wow, that's a huge difference. I'm having trouble to believe two CR4 writes are this fast, and an XSAVE of just a single 32-bit item is this slow. But well - you measured it.
Jan
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
In reply to Huaitong Hanxen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 114
Quoted 4 lines · Huaitong Han
xen/arch/x86/mm/guest_walk.c +4
-0+ +/* PKRU dom0 is always zero */ +if (likely (!pte_pkeys) ) +return 0 ;
This is not an architectural restriction (as far as I can tell). Xen must never make assumptions about how a guest chooses to use a feature.
~Andrew
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
Quoted 9 lines · Andrew Cooper
xen/arch/x86/mm/guest_walk.c static uint32_t set_ad_bits(void *guest_p, void *walk_p, int set_dirty) · line 114
Quoted 4 lines · Huaitong Han
xen/arch/x86/mm/guest_walk.c +4
-0+ +/* PKRU dom0 is always zero */ +if (likely (!pte_pkeys) ) +return 0 ;This is not an architectural restriction (as far as I can tell). Xen must never make assumptions about how a guest chooses to use a feature.
Yes, I will remove the code section.
On my test machine, writing non-zero value to PKRU domain 0 will lead to a GP fault, this is also conform to the usage scenario, and I will ask the intel architect the reason why SDM does not describe.
If it is OK, I will send the other patch or new version patch to improve the efficiency of the code.
Quoted 2 lines
~Andrew
Re: [V3 PATCH 7/9] x86/hvm: pkeys, add pkeys support for guest_walk_tables
Quoted 6 lines · Huaitong
Quoted 5 lines · Andrew Cooper
Quoted 4 lines · Huaitong Han
xen/arch/x86/mm/guest_walk.c +4
-0+ +/* PKRU dom0 is always zero */ +if (likely (!pte_pkeys) ) +return 0 ;
Quoted 8 lines
Quoted 3 lines
This is not an architectural restriction (as far as I can tell). Xen must never make assumptions about how a guest chooses to use a feature.
Yes, I will remove the code section.
On my test machine, writing non-zero value to PKRU domain 0 will lead to a GP fault, this is also conform to the usage scenario, and I will ask the intel architect the reason why SDM does not describe.
If this is (or is going to be documented as) an architectural restriction, then the code can stay, although I would adjust the comment to
/* Architecturally, pkey 0 is always zero. */
Thinking about it, the restriction does make sense. It equates to "the default behaviour of things before the PKRU feature came along".
~Andrew
PATCH v3 8/9 x86/hvm: pkeys, add pkeys support for gva2gfn funcitons
In reply to Huaitong HanThis patch adds pkeys support for gva2gfn funcitons.
--- xen/arch/x86/hvm/hvm.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/hvm.c +10 -5
@@ -4319,7 +4319,8 @@ static enum hvm_copy_result __hvm_clear(paddr_t addr, int size)p2m_type_t p2mt;char *p;int count, todo = size; -uint32_t pfec = PFEC_page_present | PFEC_write_access; +uint32_t pfec = PFEC_page_present | PFEC_write_access | + (hvm_pku_enabled (curr) ? PFEC_prot_key :0 );/* * XXX Disable for 4.1.0: PV-on-HVM drivers will do grant-table ops
@@ -4420,7 +4421,8 @@ enum hvm_copy_result hvm_copy_to_guest_virt( {return __hvm_copy (buf, vaddr, size, HVMCOPY_to_guest | HVMCOPY_fault | HVMCOPY_virt, - PFEC_page_present | PFEC_write_access | pfec); + PFEC_page_present | PFEC_write_access | pfec | + (hvm_pku_enabled (current) ? PFEC_prot_key :0 )); } enum hvm_copy_result hvm_copy_from_guest_virt(
@@ -4428,7 +4430,8 @@ enum hvm_copy_result hvm_copy_from_guest_virt( {return __hvm_copy (buf, vaddr, size, HVMCOPY_from_guest | HVMCOPY_fault | HVMCOPY_virt, - PFEC_page_present | pfec); + PFEC_page_present | pfec | + (hvm_pku_enabled (current) ? PFEC_prot_key :0 )); } enum hvm_copy_result hvm_fetch_from_guest_virt(
@@ -4446,7 +4449,8 @@ enum hvm_copy_result hvm_copy_to_guest_virt_nofault( {return __hvm_copy (buf, vaddr, size, HVMCOPY_to_guest | HVMCOPY_no_fault | HVMCOPY_virt, - PFEC_page_present | PFEC_write_access | pfec); + PFEC_page_present | PFEC_write_access | pfec | + (hvm_pku_enabled (current) ? PFEC_prot_key :0 )); } enum hvm_copy_result hvm_copy_from_guest_virt_nofault(
@@ -4454,7 +4458,8 @@ enum hvm_copy_result hvm_copy_from_guest_virt_nofault( {return __hvm_copy (buf, vaddr, size, HVMCOPY_from_guest | HVMCOPY_no_fault | HVMCOPY_virt, - PFEC_page_present | pfec); + PFEC_page_present | pfec | + (hvm_pku_enabled (current) ? PFEC_prot_key :0 )); } enum hvm_copy_result hvm_fetch_from_guest_virt_nofault(
Signature · 1 line
2.4.3
PATCH v3 9/9 x86/hvm: pkeys, add pkeys support for cpuid handling
In reply to Huaitong HanThis patch adds pkeys support for cpuid handing.
Pkeys hardware support is CPUID.7.0.ECX[3]:PKU. software support is CPUID.7.0.ECX[4]:OSPKE and it reflects the support setting of CR4.PKE.
--- tools/libxc/xc_cpufeature.h | 2 ++ tools/libxc/xc_cpuid_x86.c | 6 ++++-- xen/arch/x86/hvm/hvm.c | 14 +++++++++++++- 3 files changed, 19 insertions(+), 3 deletions(-)
tools/libxc/xc_cpufeature.h +2 -0
@@ -141,5 +141,7 @@#define X86_FEATURE_ADX 19/* ADCX, ADOX instructions */ #define X86_FEATURE_SMAP 20/* Supervisor Mode Access Protection */ +/* Intel-defined CPU features, CPUID level 0x00000007:0 (ecx) */ +#define X86_FEATURE_PKU 3#endif /* __LIBXC_CPUFEATURE_H */
tools/libxc/xc_cpuid_x86.c +4 -2
@@ -427,9 +427,11 @@ static void xc_cpuid_hvm_policy(xc_interface *xch,bitmaskof (X86_FEATURE_ADX ) |bitmaskof (X86_FEATURE_SMAP ) | bitmaskof(X86_FEATURE_FSGSBASE)); + regs[2 ] &=bitmaskof (X86_FEATURE_PKU ); }else - regs[1 ] =0 ; - regs[0 ] = regs[2 ] = regs[3 ] =0 ; + regs[1 ] = regs[2 ] =0 ; + + regs[0 ] = regs[3 ] =0 ;break ;case 0x0000000d :
xen/arch/x86/hvm/hvm.c +13 -1
@@ -4577,7 +4577,7 @@ void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx,__clear_bit (X86_FEATURE_APIC &31 , edx);/* Fix up OSXSAVE. */ -if ( cpu_has_xsave ) +if ( *ecx &cpufeat_mask (X86_FEATURE_XSAVE ) ) *ecx |= (v->arch.hvm_vcpu.guest_cr[4 ] &X86_CR4_OSXSAVE ) ?cpufeat_mask (X86_FEATURE_OSXSAVE ) :0 ;
@@ -4605,6 +4605,18 @@ void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx,/* Don't expose INVPCID to non-hap hvm. */ if ( (count ==0 ) && !hap_enabled (d) ) *ebx &= ~cpufeat_mask (X86_FEATURE_INVPCID ); + +/* X86_FEATURE_PKU is not yet implemented for shadow paging +* +* Hypervisor gets guest pkru value from XSAVE state, because +* Hypervisor CR4 without X86_CR4_PKE disables RDPKRU instruction. +*/ +if ( (count ==0 ) && (!hap_enabled (d) || !cpu_has_xsave) ) + *ecx &= ~cpufeat_mask (X86_FEATURE_PKU ); + +if ( (count ==0 ) && (*ecx &cpufeat_mask (X86_FEATURE_PKU )) ) + *ecx |= (v->arch.hvm_vcpu.guest_cr[4 ] &X86_CR4_PKE ) ? +cpufeat_mask (X86_FEATURE_OSPKE ) :0 ;break ;case 0xb :/* Fix the x2APIC identifier. */
Signature · 1 line
2.4.3
Re: [V3 PATCH 9/9] x86/hvm: pkeys, add pkeys support for cpuid handling
xen/arch/x86/hvm/hvm.c void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx, · line 4609
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 07.12.15 at 10:16, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +2
-0@@ -4605,6 +4605,18 @@ void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx,/* Don't expose INVPCID to non-hap hvm. */ if ( (count ==0 ) && !hap_enabled (d) ) *ebx &= ~cpufeat_mask (X86_FEATURE_INVPCID ); + +/* X86_FEATURE_PKU is not yet implemented for shadow paging
Coding style.
xen/arch/x86/hvm/hvm.c void hvm_cpuid(unsigned int input, unsigned int *eax, unsigned int *ebx, · line 4614
Quoted 5 lines
xen/arch/x86/hvm/hvm.c +5
-0+* +* Hypervisor gets guest pkru value from XSAVE state, because +* Hypervisor CR4 without X86_CR4_PKE disables RDPKRU instruction. +*/ +if ( (count ==0 ) && (!hap_enabled (d) || !cpu_has_xsave) )
I has been said before that you should check the guest property here, not the host one. Without you doing so I can't even see the point of you adjusting the logic to set OSXSAVE above.
Jan