WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / powerpc / ptrace / ptrace-pkey.c
blobbc454f8991246560a7236db3fdec76205475e000
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Ptrace test for Memory Protection Key registers
5 * Copyright (C) 2015 Anshuman Khandual, IBM Corporation.
6 * Copyright (C) 2018 IBM Corporation.
7 */
8 #include "ptrace.h"
9 #include "child.h"
11 #ifndef __NR_pkey_alloc
12 #define __NR_pkey_alloc 384
13 #endif
15 #ifndef __NR_pkey_free
16 #define __NR_pkey_free 385
17 #endif
19 #ifndef NT_PPC_PKEY
20 #define NT_PPC_PKEY 0x110
21 #endif
23 #ifndef PKEY_DISABLE_EXECUTE
24 #define PKEY_DISABLE_EXECUTE 0x4
25 #endif
27 #define AMR_BITS_PER_PKEY 2
28 #define PKEY_REG_BITS (sizeof(u64) * 8)
29 #define pkeyshift(pkey) (PKEY_REG_BITS - ((pkey + 1) * AMR_BITS_PER_PKEY))
31 static const char user_read[] = "[User Read (Running)]";
32 static const char user_write[] = "[User Write (Running)]";
33 static const char ptrace_read_running[] = "[Ptrace Read (Running)]";
34 static const char ptrace_write_running[] = "[Ptrace Write (Running)]";
36 /* Information shared between the parent and the child. */
37 struct shared_info {
38 struct child_sync child_sync;
40 /* AMR value the parent expects to read from the child. */
41 unsigned long amr1;
43 /* AMR value the parent is expected to write to the child. */
44 unsigned long amr2;
46 /* AMR value that ptrace should refuse to write to the child. */
47 unsigned long invalid_amr;
49 /* IAMR value the parent expects to read from the child. */
50 unsigned long expected_iamr;
52 /* UAMOR value the parent expects to read from the child. */
53 unsigned long expected_uamor;
56 * IAMR and UAMOR values that ptrace should refuse to write to the child
57 * (even though they're valid ones) because userspace doesn't have
58 * access to those registers.
60 unsigned long invalid_iamr;
61 unsigned long invalid_uamor;
64 static int sys_pkey_alloc(unsigned long flags, unsigned long init_access_rights)
66 return syscall(__NR_pkey_alloc, flags, init_access_rights);
69 static int child(struct shared_info *info)
71 unsigned long reg;
72 bool disable_execute = true;
73 int pkey1, pkey2, pkey3;
74 int ret;
76 /* Wait until parent fills out the initial register values. */
77 ret = wait_parent(&info->child_sync);
78 if (ret)
79 return ret;
81 /* Get some pkeys so that we can change their bits in the AMR. */
82 pkey1 = sys_pkey_alloc(0, PKEY_DISABLE_EXECUTE);
83 if (pkey1 < 0) {
84 pkey1 = sys_pkey_alloc(0, 0);
85 CHILD_FAIL_IF(pkey1 < 0, &info->child_sync);
87 disable_execute = false;
90 pkey2 = sys_pkey_alloc(0, 0);
91 CHILD_FAIL_IF(pkey2 < 0, &info->child_sync);
93 pkey3 = sys_pkey_alloc(0, 0);
94 CHILD_FAIL_IF(pkey3 < 0, &info->child_sync);
96 info->amr1 |= 3ul << pkeyshift(pkey1);
97 info->amr2 |= 3ul << pkeyshift(pkey2);
99 * invalid amr value where we try to force write
100 * things which are deined by a uamor setting.
102 info->invalid_amr = info->amr2 | (~0x0UL & ~info->expected_uamor);
105 * if PKEY_DISABLE_EXECUTE succeeded we should update the expected_iamr
107 if (disable_execute)
108 info->expected_iamr |= 1ul << pkeyshift(pkey1);
109 else
110 info->expected_iamr &= ~(1ul << pkeyshift(pkey1));
113 * We allocated pkey2 and pkey 3 above. Clear the IAMR bits.
115 info->expected_iamr &= ~(1ul << pkeyshift(pkey2));
116 info->expected_iamr &= ~(1ul << pkeyshift(pkey3));
119 * Create an IAMR value different from expected value.
120 * Kernel will reject an IAMR and UAMOR change.
122 info->invalid_iamr = info->expected_iamr | (1ul << pkeyshift(pkey1) | 1ul << pkeyshift(pkey2));
123 info->invalid_uamor = info->expected_uamor & ~(0x3ul << pkeyshift(pkey1));
125 printf("%-30s AMR: %016lx pkey1: %d pkey2: %d pkey3: %d\n",
126 user_write, info->amr1, pkey1, pkey2, pkey3);
128 set_amr(info->amr1);
130 /* Wait for parent to read our AMR value and write a new one. */
131 ret = prod_parent(&info->child_sync);
132 CHILD_FAIL_IF(ret, &info->child_sync);
134 ret = wait_parent(&info->child_sync);
135 if (ret)
136 return ret;
138 reg = mfspr(SPRN_AMR);
140 printf("%-30s AMR: %016lx\n", user_read, reg);
142 CHILD_FAIL_IF(reg != info->amr2, &info->child_sync);
145 * Wait for parent to try to write an invalid AMR value.
147 ret = prod_parent(&info->child_sync);
148 CHILD_FAIL_IF(ret, &info->child_sync);
150 ret = wait_parent(&info->child_sync);
151 if (ret)
152 return ret;
154 reg = mfspr(SPRN_AMR);
156 printf("%-30s AMR: %016lx\n", user_read, reg);
158 CHILD_FAIL_IF(reg != info->amr2, &info->child_sync);
161 * Wait for parent to try to write an IAMR and a UAMOR value. We can't
162 * verify them, but we can verify that the AMR didn't change.
164 ret = prod_parent(&info->child_sync);
165 CHILD_FAIL_IF(ret, &info->child_sync);
167 ret = wait_parent(&info->child_sync);
168 if (ret)
169 return ret;
171 reg = mfspr(SPRN_AMR);
173 printf("%-30s AMR: %016lx\n", user_read, reg);
175 CHILD_FAIL_IF(reg != info->amr2, &info->child_sync);
177 /* Now let parent now that we are finished. */
179 ret = prod_parent(&info->child_sync);
180 CHILD_FAIL_IF(ret, &info->child_sync);
182 return TEST_PASS;
185 static int parent(struct shared_info *info, pid_t pid)
187 unsigned long regs[3];
188 int ret, status;
191 * Get the initial values for AMR, IAMR and UAMOR and communicate them
192 * to the child.
194 ret = ptrace_read_regs(pid, NT_PPC_PKEY, regs, 3);
195 PARENT_SKIP_IF_UNSUPPORTED(ret, &info->child_sync);
196 PARENT_FAIL_IF(ret, &info->child_sync);
198 info->amr1 = info->amr2 = regs[0];
199 info->expected_iamr = regs[1];
200 info->expected_uamor = regs[2];
202 /* Wake up child so that it can set itself up. */
203 ret = prod_child(&info->child_sync);
204 PARENT_FAIL_IF(ret, &info->child_sync);
206 ret = wait_child(&info->child_sync);
207 if (ret)
208 return ret;
210 /* Verify that we can read the pkey registers from the child. */
211 ret = ptrace_read_regs(pid, NT_PPC_PKEY, regs, 3);
212 PARENT_FAIL_IF(ret, &info->child_sync);
214 printf("%-30s AMR: %016lx IAMR: %016lx UAMOR: %016lx\n",
215 ptrace_read_running, regs[0], regs[1], regs[2]);
217 PARENT_FAIL_IF(regs[0] != info->amr1, &info->child_sync);
218 PARENT_FAIL_IF(regs[1] != info->expected_iamr, &info->child_sync);
219 PARENT_FAIL_IF(regs[2] != info->expected_uamor, &info->child_sync);
221 /* Write valid AMR value in child. */
222 ret = ptrace_write_regs(pid, NT_PPC_PKEY, &info->amr2, 1);
223 PARENT_FAIL_IF(ret, &info->child_sync);
225 printf("%-30s AMR: %016lx\n", ptrace_write_running, info->amr2);
227 /* Wake up child so that it can verify it changed. */
228 ret = prod_child(&info->child_sync);
229 PARENT_FAIL_IF(ret, &info->child_sync);
231 ret = wait_child(&info->child_sync);
232 if (ret)
233 return ret;
235 /* Write invalid AMR value in child. */
236 ret = ptrace_write_regs(pid, NT_PPC_PKEY, &info->invalid_amr, 1);
237 PARENT_FAIL_IF(ret, &info->child_sync);
239 printf("%-30s AMR: %016lx\n", ptrace_write_running, info->invalid_amr);
241 /* Wake up child so that it can verify it didn't change. */
242 ret = prod_child(&info->child_sync);
243 PARENT_FAIL_IF(ret, &info->child_sync);
245 ret = wait_child(&info->child_sync);
246 if (ret)
247 return ret;
249 /* Try to write to IAMR. */
250 regs[0] = info->amr1;
251 regs[1] = info->invalid_iamr;
252 ret = ptrace_write_regs(pid, NT_PPC_PKEY, regs, 2);
253 PARENT_FAIL_IF(!ret, &info->child_sync);
255 printf("%-30s AMR: %016lx IAMR: %016lx\n",
256 ptrace_write_running, regs[0], regs[1]);
258 /* Try to write to IAMR and UAMOR. */
259 regs[2] = info->invalid_uamor;
260 ret = ptrace_write_regs(pid, NT_PPC_PKEY, regs, 3);
261 PARENT_FAIL_IF(!ret, &info->child_sync);
263 printf("%-30s AMR: %016lx IAMR: %016lx UAMOR: %016lx\n",
264 ptrace_write_running, regs[0], regs[1], regs[2]);
266 /* Verify that all registers still have their expected values. */
267 ret = ptrace_read_regs(pid, NT_PPC_PKEY, regs, 3);
268 PARENT_FAIL_IF(ret, &info->child_sync);
270 printf("%-30s AMR: %016lx IAMR: %016lx UAMOR: %016lx\n",
271 ptrace_read_running, regs[0], regs[1], regs[2]);
273 PARENT_FAIL_IF(regs[0] != info->amr2, &info->child_sync);
274 PARENT_FAIL_IF(regs[1] != info->expected_iamr, &info->child_sync);
275 PARENT_FAIL_IF(regs[2] != info->expected_uamor, &info->child_sync);
277 /* Wake up child so that it can verify AMR didn't change and wrap up. */
278 ret = prod_child(&info->child_sync);
279 PARENT_FAIL_IF(ret, &info->child_sync);
281 ret = wait(&status);
282 if (ret != pid) {
283 printf("Child's exit status not captured\n");
284 ret = TEST_PASS;
285 } else if (!WIFEXITED(status)) {
286 printf("Child exited abnormally\n");
287 ret = TEST_FAIL;
288 } else
289 ret = WEXITSTATUS(status) ? TEST_FAIL : TEST_PASS;
291 return ret;
294 static int ptrace_pkey(void)
296 struct shared_info *info;
297 int shm_id;
298 int ret;
299 pid_t pid;
301 shm_id = shmget(IPC_PRIVATE, sizeof(*info), 0777 | IPC_CREAT);
302 info = shmat(shm_id, NULL, 0);
304 ret = init_child_sync(&info->child_sync);
305 if (ret)
306 return ret;
308 pid = fork();
309 if (pid < 0) {
310 perror("fork() failed");
311 ret = TEST_FAIL;
312 } else if (pid == 0)
313 ret = child(info);
314 else
315 ret = parent(info, pid);
317 shmdt(info);
319 if (pid) {
320 destroy_child_sync(&info->child_sync);
321 shmctl(shm_id, IPC_RMID, NULL);
324 return ret;
327 int main(int argc, char *argv[])
329 return test_harness(ptrace_pkey, "ptrace_pkey");