Skip to content

Commit

Permalink
selftests: vm: pkeys: Use sane types for pkey register
Browse files Browse the repository at this point in the history
The size of the pkey register can vary across architectures.  This
converts the data type of all its references to u64 in preparation for
multi-arch support.

To keep the definition of the u64 type consistent and remove format
specifier related warnings, __SANE_USERSPACE_TYPES__ is defined as
suggested by Michael Ellerman.

Signed-off-by: Sandipan Das <sandipan@linux.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Dave Hansen <dave.hansen@intel.com>
Cc: "Desnes A. Nunes do Rosario" <desnesn@linux.vnet.ibm.com>
Cc: Florian Weimer <fweimer@redhat.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Ram Pai <linuxram@us.ibm.com>
Cc: Thiago Jung Bauermann <bauerman@linux.ibm.com>
Cc: "Aneesh Kumar K.V" <aneesh.kumar@linux.ibm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Michal Suchanek <msuchanek@suse.de>
Cc: Shuah Khan <shuah@kernel.org>
Link: http://lkml.kernel.org/r/d3e271798455d940e395e56e1ff1e82a31bcb7aa.1585646528.git.sandipan@linux.ibm.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
  • Loading branch information
Sandipan Das authored and Linus Torvalds committed Jun 5, 2020
1 parent a09160e commit 4dbdd94
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 53 deletions.
31 changes: 16 additions & 15 deletions tools/testing/selftests/vm/pkey-helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
#include <sys/mman.h>

/* Define some kernel-like types */
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
#define u64 uint64_t
#define u8 __u8
#define u16 __u16
#define u32 __u32
#define u64 __u64

#define PTR_ERR_ENOTSUP ((void *)-ENOTSUP)

Expand Down Expand Up @@ -80,13 +80,14 @@ extern void abort_hooks(void);
#error Architecture not supported
#endif /* arch */

extern unsigned int shadow_pkey_reg;
extern u64 shadow_pkey_reg;

static inline unsigned int _read_pkey_reg(int line)
static inline u64 _read_pkey_reg(int line)
{
unsigned int pkey_reg = __read_pkey_reg();
u64 pkey_reg = __read_pkey_reg();

dprintf4("read_pkey_reg(line=%d) pkey_reg: %x shadow: %x\n",
dprintf4("read_pkey_reg(line=%d) pkey_reg: %016llx"
" shadow: %016llx\n",
line, pkey_reg, shadow_pkey_reg);
assert(pkey_reg == shadow_pkey_reg);

Expand All @@ -95,15 +96,15 @@ static inline unsigned int _read_pkey_reg(int line)

#define read_pkey_reg() _read_pkey_reg(__LINE__)

static inline void write_pkey_reg(unsigned int pkey_reg)
static inline void write_pkey_reg(u64 pkey_reg)
{
dprintf4("%s() changing %08x to %08x\n", __func__,
dprintf4("%s() changing %016llx to %016llx\n", __func__,
__read_pkey_reg(), pkey_reg);
/* will do the shadow check for us: */
read_pkey_reg();
__write_pkey_reg(pkey_reg);
shadow_pkey_reg = pkey_reg;
dprintf4("%s(%08x) pkey_reg: %08x\n", __func__,
dprintf4("%s(%016llx) pkey_reg: %016llx\n", __func__,
pkey_reg, __read_pkey_reg());
}

Expand All @@ -113,21 +114,21 @@ static inline void write_pkey_reg(unsigned int pkey_reg)
*/
static inline void __pkey_access_allow(int pkey, int do_allow)
{
unsigned int pkey_reg = read_pkey_reg();
u64 pkey_reg = read_pkey_reg();
int bit = pkey * 2;

if (do_allow)
pkey_reg &= (1<<bit);
else
pkey_reg |= (1<<bit);

dprintf4("pkey_reg now: %08x\n", read_pkey_reg());
dprintf4("pkey_reg now: %016llx\n", read_pkey_reg());
write_pkey_reg(pkey_reg);
}

static inline void __pkey_write_allow(int pkey, int do_allow_write)
{
long pkey_reg = read_pkey_reg();
u64 pkey_reg = read_pkey_reg();
int bit = pkey * 2 + 1;

if (do_allow_write)
Expand All @@ -136,7 +137,7 @@ static inline void __pkey_write_allow(int pkey, int do_allow_write)
pkey_reg |= (1<<bit);

write_pkey_reg(pkey_reg);
dprintf4("pkey_reg now: %08x\n", read_pkey_reg());
dprintf4("pkey_reg now: %016llx\n", read_pkey_reg());
}

#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
Expand Down
8 changes: 4 additions & 4 deletions tools/testing/selftests/vm/pkey-x86.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ static inline void __page_o_noops(void)
asm(".rept 512 ; nopl 0x7eeeeeee(%eax) ; .endr");
}

static inline unsigned int __read_pkey_reg(void)
static inline u64 __read_pkey_reg(void)
{
unsigned int eax, edx;
unsigned int ecx = 0;
unsigned int pkey_reg;
unsigned pkey_reg;

asm volatile(".byte 0x0f,0x01,0xee\n\t"
: "=a" (eax), "=d" (edx)
Expand All @@ -66,13 +66,13 @@ static inline unsigned int __read_pkey_reg(void)
return pkey_reg;
}

static inline void __write_pkey_reg(unsigned int pkey_reg)
static inline void __write_pkey_reg(u64 pkey_reg)
{
unsigned int eax = pkey_reg;
unsigned int ecx = 0;
unsigned int edx = 0;

dprintf4("%s() changing %08x to %08x\n", __func__,
dprintf4("%s() changing %016llx to %016llx\n", __func__,
__read_pkey_reg(), pkey_reg);
asm volatile(".byte 0x0f,0x01,0xef\n\t"
: : "a" (eax), "c" (ecx), "d" (edx));
Expand Down
Loading

0 comments on commit 4dbdd94

Please sign in to comment.