2021-06-23 08:02:05 -04:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
#ifndef _ASM_X86_PKRU_H
|
|
|
|
#define _ASM_X86_PKRU_H
|
|
|
|
|
2021-10-14 21:15:57 -04:00
|
|
|
#include <asm/cpufeature.h>
|
2021-06-23 08:02:05 -04:00
|
|
|
|
2021-12-15 19:08:56 -05:00
|
|
|
#define PKRU_AD_BIT 0x1u
|
|
|
|
#define PKRU_WD_BIT 0x2u
|
2021-06-23 08:02:05 -04:00
|
|
|
#define PKRU_BITS_PER_PKEY 2
|
|
|
|
|
|
|
|
#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
|
|
|
|
extern u32 init_pkru_value;
|
2021-06-23 08:02:08 -04:00
|
|
|
#define pkru_get_init_value() READ_ONCE(init_pkru_value)
|
2021-06-23 08:02:05 -04:00
|
|
|
#else
|
|
|
|
#define init_pkru_value 0
|
2021-06-23 08:02:08 -04:00
|
|
|
#define pkru_get_init_value() 0
|
2021-06-23 08:02:05 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static inline bool __pkru_allows_read(u32 pkru, u16 pkey)
|
|
|
|
{
|
|
|
|
int pkru_pkey_bits = pkey * PKRU_BITS_PER_PKEY;
|
|
|
|
return !(pkru & (PKRU_AD_BIT << pkru_pkey_bits));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool __pkru_allows_write(u32 pkru, u16 pkey)
|
|
|
|
{
|
|
|
|
int pkru_pkey_bits = pkey * PKRU_BITS_PER_PKEY;
|
|
|
|
/*
|
|
|
|
* Access-disable disables writes too so we need to check
|
|
|
|
* both bits here.
|
|
|
|
*/
|
|
|
|
return !(pkru & ((PKRU_AD_BIT|PKRU_WD_BIT) << pkru_pkey_bits));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 read_pkru(void)
|
|
|
|
{
|
2021-06-23 08:02:07 -04:00
|
|
|
if (cpu_feature_enabled(X86_FEATURE_OSPKE))
|
2021-06-23 08:02:05 -04:00
|
|
|
return rdpkru();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void write_pkru(u32 pkru)
|
|
|
|
{
|
2021-06-23 08:02:07 -04:00
|
|
|
if (!cpu_feature_enabled(X86_FEATURE_OSPKE))
|
2021-06-23 08:02:05 -04:00
|
|
|
return;
|
|
|
|
/*
|
2021-06-23 08:02:23 -04:00
|
|
|
* WRPKRU is relatively expensive compared to RDPKRU.
|
|
|
|
* Avoid WRPKRU when it would not change the value.
|
2021-06-23 08:02:05 -04:00
|
|
|
*/
|
2021-06-23 08:02:23 -04:00
|
|
|
if (pkru != rdpkru())
|
|
|
|
wrpkru(pkru);
|
2021-06-23 08:02:05 -04:00
|
|
|
}
|
|
|
|
|
2021-06-23 08:02:09 -04:00
|
|
|
static inline void pkru_write_default(void)
|
|
|
|
{
|
|
|
|
if (!cpu_feature_enabled(X86_FEATURE_OSPKE))
|
|
|
|
return;
|
|
|
|
|
|
|
|
wrpkru(pkru_get_init_value());
|
|
|
|
}
|
|
|
|
|
2021-06-23 08:02:05 -04:00
|
|
|
#endif
|