xref: /linux/tools/testing/selftests/mm/pkey-helpers.h (revision 8804d970fab45726b3c7cd7f240b31122aa94219)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _PKEYS_HELPER_H
3 #define _PKEYS_HELPER_H
4 #define _GNU_SOURCE
5 #include <string.h>
6 #include <stdarg.h>
7 #include <stdio.h>
8 #include <stdint.h>
9 #include <stdbool.h>
10 #include <signal.h>
11 #include <assert.h>
12 #include <stdlib.h>
13 #include <ucontext.h>
14 #include <sys/mman.h>
15 
16 #include <linux/mman.h>
17 #include <linux/types.h>
18 
19 #include "../kselftest.h"
20 
21 /* Define some kernel-like types */
22 typedef __u8	u8;
23 typedef __u16	u16;
24 typedef __u32	u32;
25 typedef __u64	u64;
26 
27 #define PTR_ERR_ENOTSUP ((void *)-ENOTSUP)
28 
29 #ifndef DEBUG_LEVEL
30 #define DEBUG_LEVEL 0
31 #endif
32 extern int dprint_in_signal;
33 
34 extern int test_nr;
35 extern int iteration_nr;
36 
37 #ifdef __GNUC__
38 __printf(1, 2)
39 #endif
sigsafe_printf(const char * format,...)40 static inline void sigsafe_printf(const char *format, ...)
41 {
42 	va_list ap;
43 
44 	if (!dprint_in_signal) {
45 		va_start(ap, format);
46 		vprintf(format, ap);
47 		va_end(ap);
48 	} else {
49 		int ret;
50 		/*
51 		 * No printf() functions are signal-safe.
52 		 * They deadlock easily. Write the format
53 		 * string to get some output, even if
54 		 * incomplete.
55 		 */
56 		ret = write(1, format, strlen(format));
57 		if (ret < 0)
58 			exit(1);
59 	}
60 }
61 #define dprintf_level(level, args...) do {	\
62 	if (level <= DEBUG_LEVEL)		\
63 		sigsafe_printf(args);		\
64 } while (0)
65 #define dprintf0(args...) dprintf_level(0, args)
66 #define dprintf1(args...) dprintf_level(1, args)
67 #define dprintf2(args...) dprintf_level(2, args)
68 #define dprintf3(args...) dprintf_level(3, args)
69 #define dprintf4(args...) dprintf_level(4, args)
70 
71 extern void abort_hooks(void);
72 #define pkey_assert(condition) do {		\
73 	if (!(condition)) {			\
74 		dprintf0("assert() at %s::%d test_nr: %d iteration: %d\n", \
75 				__FILE__, __LINE__,	\
76 				test_nr, iteration_nr);	\
77 		dprintf0("errno at assert: %d", errno);	\
78 		abort_hooks();			\
79 		exit(__LINE__);			\
80 	}					\
81 } while (0)
82 
83 #define barrier() __asm__ __volatile__("": : :"memory")
84 #ifndef noinline
85 # define noinline __attribute__((noinline))
86 #endif
87 
88 int sys_pkey_alloc(unsigned long flags, unsigned long init_val);
89 int sys_pkey_free(unsigned long pkey);
90 int sys_mprotect_pkey(void *ptr, size_t size, unsigned long orig_prot,
91 		unsigned long pkey);
92 
93 /* For functions called from protection_keys.c only */
94 noinline int read_ptr(int *ptr);
95 void expected_pkey_fault(int pkey);
96 int mprotect_pkey(void *ptr, size_t size, unsigned long orig_prot,
97 		unsigned long pkey);
98 void record_pkey_malloc(void *ptr, long size, int prot);
99 
100 #if defined(__i386__) || defined(__x86_64__) /* arch */
101 #include "pkey-x86.h"
102 #elif defined(__powerpc64__) /* arch */
103 #include "pkey-powerpc.h"
104 #elif defined(__aarch64__) /* arch */
105 #include "pkey-arm64.h"
106 #else /* arch */
107 #error Architecture not supported
108 #endif /* arch */
109 
110 #ifndef PKEY_MASK
111 #define PKEY_MASK	(PKEY_DISABLE_ACCESS | PKEY_DISABLE_WRITE)
112 #endif
113 
114 /*
115  * FIXME: Remove once the generic PKEY_UNRESTRICTED definition is merged.
116  */
117 #ifndef PKEY_UNRESTRICTED
118 #define PKEY_UNRESTRICTED 0x0
119 #endif
120 
121 #ifndef set_pkey_bits
set_pkey_bits(u64 reg,int pkey,u64 flags)122 static inline u64 set_pkey_bits(u64 reg, int pkey, u64 flags)
123 {
124 	u32 shift = pkey_bit_position(pkey);
125 	/* mask out bits from pkey in old value */
126 	reg &= ~((u64)PKEY_MASK << shift);
127 	/* OR in new bits for pkey */
128 	reg |= (flags & PKEY_MASK) << shift;
129 	return reg;
130 }
131 #endif
132 
133 #ifndef get_pkey_bits
get_pkey_bits(u64 reg,int pkey)134 static inline u64 get_pkey_bits(u64 reg, int pkey)
135 {
136 	u32 shift = pkey_bit_position(pkey);
137 	/*
138 	 * shift down the relevant bits to the lowest two, then
139 	 * mask off all the other higher bits
140 	 */
141 	return ((reg >> shift) & PKEY_MASK);
142 }
143 #endif
144 
145 extern u64 shadow_pkey_reg;
146 
_read_pkey_reg(int line)147 static inline u64 _read_pkey_reg(int line)
148 {
149 	u64 pkey_reg = __read_pkey_reg();
150 
151 	dprintf4("read_pkey_reg(line=%d) pkey_reg: %016llx"
152 			" shadow: %016llx\n",
153 			line, pkey_reg, shadow_pkey_reg);
154 	assert(pkey_reg == shadow_pkey_reg);
155 
156 	return pkey_reg;
157 }
158 
159 #define read_pkey_reg() _read_pkey_reg(__LINE__)
160 
write_pkey_reg(u64 pkey_reg)161 static inline void write_pkey_reg(u64 pkey_reg)
162 {
163 	dprintf4("%s() changing %016llx to %016llx\n", __func__,
164 			__read_pkey_reg(), pkey_reg);
165 	/* will do the shadow check for us: */
166 	read_pkey_reg();
167 	__write_pkey_reg(pkey_reg);
168 	shadow_pkey_reg = pkey_reg;
169 	dprintf4("%s(%016llx) pkey_reg: %016llx\n", __func__,
170 			pkey_reg, __read_pkey_reg());
171 }
172 
173 #define ALIGN_UP(x, align_to)	(((x) + ((align_to)-1)) & ~((align_to)-1))
174 #define ALIGN_DOWN(x, align_to) ((x) & ~((align_to)-1))
175 #define ALIGN_PTR_UP(p, ptr_align_to)	\
176 	((typeof(p))ALIGN_UP((unsigned long)(p), ptr_align_to))
177 #define ALIGN_PTR_DOWN(p, ptr_align_to)	\
178 	((typeof(p))ALIGN_DOWN((unsigned long)(p), ptr_align_to))
179 #define __stringify_1(x...)     #x
180 #define __stringify(x...)       __stringify_1(x)
181 
siginfo_get_pkey_ptr(siginfo_t * si)182 static inline u32 *siginfo_get_pkey_ptr(siginfo_t *si)
183 {
184 #ifdef si_pkey
185 	return &si->si_pkey;
186 #else
187 	return (u32 *)(((u8 *)si) + si_pkey_offset);
188 #endif
189 }
190 
kernel_has_pkeys(void)191 static inline int kernel_has_pkeys(void)
192 {
193 	/* try allocating a key and see if it succeeds */
194 	int ret = sys_pkey_alloc(0, PKEY_UNRESTRICTED);
195 	if (ret <= 0) {
196 		return 0;
197 	}
198 	sys_pkey_free(ret);
199 	return 1;
200 }
201 
is_pkeys_supported(void)202 static inline int is_pkeys_supported(void)
203 {
204 	/* check if the cpu supports pkeys */
205 	if (!cpu_has_pkeys()) {
206 		dprintf1("SKIP: %s: no CPU support\n", __func__);
207 		return 0;
208 	}
209 
210 	/* check if the kernel supports pkeys */
211 	if (!kernel_has_pkeys()) {
212 		dprintf1("SKIP: %s: no kernel support\n", __func__);
213 		return 0;
214 	}
215 
216 	return 1;
217 }
218 
219 #endif /* _PKEYS_HELPER_H */
220