• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2019-2024 Red Hat, Inc.
4  * Copyright (c) Linux Test Project, 2019-2024
5  */
6 
7 #ifndef PKEYS_H__
8 #define PKEYS_H__
9 
10 #include "config.h"
11 #include "tst_test.h"
12 #include "lapi/syscalls.h"
13 #include "lapi/mmap.h"
14 
15 #ifndef PKEY_DISABLE_ACCESS
16 # define PKEY_DISABLE_ACCESS 0x1
17 # define PKEY_DISABLE_WRITE  0x2
18 #endif
19 
20 #ifndef PKEY_DISABLE_EXECUTE
21 # define PKEY_DISABLE_EXECUTE 0x4
22 #endif
23 
24 #ifndef HAVE_PKEY_MPROTECT
pkey_mprotect(void * addr,size_t len,int prot,int pkey)25 inline int pkey_mprotect(void *addr, size_t len, int prot, int pkey)
26 {
27 	return tst_syscall(__NR_pkey_mprotect, addr, len, prot, pkey);
28 }
29 
pkey_alloc(unsigned int flags,unsigned int access_rights)30 inline int pkey_alloc(unsigned int flags, unsigned int access_rights)
31 {
32 	return tst_syscall(__NR_pkey_alloc, flags, access_rights);
33 }
34 
pkey_free(int pkey)35 inline int pkey_free(int pkey)
36 {
37 	return tst_syscall(__NR_pkey_free, pkey);
38 }
39 #endif /* HAVE_PKEY_MPROTECT */
40 
check_pkey_support(void)41 static inline void check_pkey_support(void)
42 {
43 	int pkey = tst_syscall(__NR_pkey_alloc, 0, 0);
44 
45 	if (pkey == -1) {
46 		if (errno == ENOSYS)
47 			tst_brk(TCONF, "pkey_alloc is not implemented");
48 		if (errno == EINVAL)
49 			tst_brk(TCONF, "pku is not supported on this CPU");
50 		if (errno == ENOSPC)
51 			tst_brk(TCONF, "pkeys are not available for test");
52 	}
53 
54 	tst_syscall(__NR_pkey_free, pkey);
55 }
56 
57 #endif /* PKEYS_H__ */
58