Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _ASM_X86_PKRU_H
0003 #define _ASM_X86_PKRU_H
0004 
0005 #include <asm/cpufeature.h>
0006 
0007 #define PKRU_AD_BIT 0x1u
0008 #define PKRU_WD_BIT 0x2u
0009 #define PKRU_BITS_PER_PKEY 2
0010 
0011 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
0012 extern u32 init_pkru_value;
0013 #define pkru_get_init_value()   READ_ONCE(init_pkru_value)
0014 #else
0015 #define init_pkru_value 0
0016 #define pkru_get_init_value()   0
0017 #endif
0018 
0019 static inline bool __pkru_allows_read(u32 pkru, u16 pkey)
0020 {
0021     int pkru_pkey_bits = pkey * PKRU_BITS_PER_PKEY;
0022     return !(pkru & (PKRU_AD_BIT << pkru_pkey_bits));
0023 }
0024 
0025 static inline bool __pkru_allows_write(u32 pkru, u16 pkey)
0026 {
0027     int pkru_pkey_bits = pkey * PKRU_BITS_PER_PKEY;
0028     /*
0029      * Access-disable disables writes too so we need to check
0030      * both bits here.
0031      */
0032     return !(pkru & ((PKRU_AD_BIT|PKRU_WD_BIT) << pkru_pkey_bits));
0033 }
0034 
0035 static inline u32 read_pkru(void)
0036 {
0037     if (cpu_feature_enabled(X86_FEATURE_OSPKE))
0038         return rdpkru();
0039     return 0;
0040 }
0041 
0042 static inline void write_pkru(u32 pkru)
0043 {
0044     if (!cpu_feature_enabled(X86_FEATURE_OSPKE))
0045         return;
0046     /*
0047      * WRPKRU is relatively expensive compared to RDPKRU.
0048      * Avoid WRPKRU when it would not change the value.
0049      */
0050     if (pkru != rdpkru())
0051         wrpkru(pkru);
0052 }
0053 
0054 static inline void pkru_write_default(void)
0055 {
0056     if (!cpu_feature_enabled(X86_FEATURE_OSPKE))
0057         return;
0058 
0059     wrpkru(pkru_get_init_value());
0060 }
0061 
0062 #endif