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