Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / testing / selftests / powerpc / mm / exec_prot.c
blobdb75b2225de19cbae77f7376fc5070eecd853e48
1 // SPDX-License-Identifier: GPL-2.0
3 /*
4 * Copyright 2022, Nicholas Miehlbradt, IBM Corporation
5 * based on pkey_exec_prot.c
7 * Test if applying execute protection on pages works as expected.
8 */
10 #define _GNU_SOURCE
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <signal.h>
16 #include <unistd.h>
17 #include <sys/mman.h>
19 #include "pkeys.h"
22 #define PPC_INST_NOP 0x60000000
23 #define PPC_INST_TRAP 0x7fe00008
24 #define PPC_INST_BLR 0x4e800020
26 static volatile sig_atomic_t fault_code;
27 static volatile sig_atomic_t remaining_faults;
28 static volatile unsigned int *fault_addr;
29 static unsigned long pgsize, numinsns;
30 static unsigned int *insns;
31 static bool pkeys_supported;
33 static bool is_fault_expected(int fault_code)
35 if (fault_code == SEGV_ACCERR)
36 return true;
38 /* Assume any pkey error is fine since pkey_exec_prot test covers them */
39 if (fault_code == SEGV_PKUERR && pkeys_supported)
40 return true;
42 return false;
45 static void trap_handler(int signum, siginfo_t *sinfo, void *ctx)
47 /* Check if this fault originated from the expected address */
48 if (sinfo->si_addr != (void *)fault_addr)
49 sigsafe_err("got a fault for an unexpected address\n");
51 _exit(1);
54 static void segv_handler(int signum, siginfo_t *sinfo, void *ctx)
56 fault_code = sinfo->si_code;
58 /* Check if this fault originated from the expected address */
59 if (sinfo->si_addr != (void *)fault_addr) {
60 sigsafe_err("got a fault for an unexpected address\n");
61 _exit(1);
64 /* Check if too many faults have occurred for a single test case */
65 if (!remaining_faults) {
66 sigsafe_err("got too many faults for the same address\n");
67 _exit(1);
71 /* Restore permissions in order to continue */
72 if (is_fault_expected(fault_code)) {
73 if (mprotect(insns, pgsize, PROT_READ | PROT_WRITE | PROT_EXEC)) {
74 sigsafe_err("failed to set access permissions\n");
75 _exit(1);
77 } else {
78 sigsafe_err("got a fault with an unexpected code\n");
79 _exit(1);
82 remaining_faults--;
85 static int check_exec_fault(int rights)
88 * Jump to the executable region.
90 * The first iteration also checks if the overwrite of the
91 * first instruction word from a trap to a no-op succeeded.
93 fault_code = -1;
94 remaining_faults = 0;
95 if (!(rights & PROT_EXEC))
96 remaining_faults = 1;
98 FAIL_IF(mprotect(insns, pgsize, rights) != 0);
99 asm volatile("mtctr %0; bctrl" : : "r"(insns));
101 FAIL_IF(remaining_faults != 0);
102 if (!(rights & PROT_EXEC))
103 FAIL_IF(!is_fault_expected(fault_code));
105 return 0;
108 static int test(void)
110 struct sigaction segv_act, trap_act;
111 int i;
113 /* Skip the test if the CPU doesn't support Radix */
114 SKIP_IF(!have_hwcap2(PPC_FEATURE2_ARCH_3_00));
116 /* Check if pkeys are supported */
117 pkeys_supported = pkeys_unsupported() == 0;
119 /* Setup SIGSEGV handler */
120 segv_act.sa_handler = 0;
121 segv_act.sa_sigaction = segv_handler;
122 FAIL_IF(sigprocmask(SIG_SETMASK, 0, &segv_act.sa_mask) != 0);
123 segv_act.sa_flags = SA_SIGINFO;
124 segv_act.sa_restorer = 0;
125 FAIL_IF(sigaction(SIGSEGV, &segv_act, NULL) != 0);
127 /* Setup SIGTRAP handler */
128 trap_act.sa_handler = 0;
129 trap_act.sa_sigaction = trap_handler;
130 FAIL_IF(sigprocmask(SIG_SETMASK, 0, &trap_act.sa_mask) != 0);
131 trap_act.sa_flags = SA_SIGINFO;
132 trap_act.sa_restorer = 0;
133 FAIL_IF(sigaction(SIGTRAP, &trap_act, NULL) != 0);
135 /* Setup executable region */
136 pgsize = getpagesize();
137 numinsns = pgsize / sizeof(unsigned int);
138 insns = (unsigned int *)mmap(NULL, pgsize, PROT_READ | PROT_WRITE,
139 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
140 FAIL_IF(insns == MAP_FAILED);
142 /* Write the instruction words */
143 for (i = 1; i < numinsns - 1; i++)
144 insns[i] = PPC_INST_NOP;
147 * Set the first instruction as an unconditional trap. If
148 * the last write to this address succeeds, this should
149 * get overwritten by a no-op.
151 insns[0] = PPC_INST_TRAP;
154 * Later, to jump to the executable region, we use a branch
155 * and link instruction (bctrl) which sets the return address
156 * automatically in LR. Use that to return back.
158 insns[numinsns - 1] = PPC_INST_BLR;
161 * Pick the first instruction's address from the executable
162 * region.
164 fault_addr = insns;
167 * Read an instruction word from the address when the page
168 * is execute only. This should generate an access fault.
170 fault_code = -1;
171 remaining_faults = 1;
172 printf("Testing read on --x, should fault...");
173 FAIL_IF(mprotect(insns, pgsize, PROT_EXEC) != 0);
174 i = *fault_addr;
175 FAIL_IF(remaining_faults != 0 || !is_fault_expected(fault_code));
176 printf("ok!\n");
179 * Write an instruction word to the address when the page
180 * execute only. This should also generate an access fault.
182 fault_code = -1;
183 remaining_faults = 1;
184 printf("Testing write on --x, should fault...");
185 FAIL_IF(mprotect(insns, pgsize, PROT_EXEC) != 0);
186 *fault_addr = PPC_INST_NOP;
187 FAIL_IF(remaining_faults != 0 || !is_fault_expected(fault_code));
188 printf("ok!\n");
190 printf("Testing exec on ---, should fault...");
191 FAIL_IF(check_exec_fault(PROT_NONE));
192 printf("ok!\n");
194 printf("Testing exec on r--, should fault...");
195 FAIL_IF(check_exec_fault(PROT_READ));
196 printf("ok!\n");
198 printf("Testing exec on -w-, should fault...");
199 FAIL_IF(check_exec_fault(PROT_WRITE));
200 printf("ok!\n");
202 printf("Testing exec on rw-, should fault...");
203 FAIL_IF(check_exec_fault(PROT_READ | PROT_WRITE));
204 printf("ok!\n");
206 printf("Testing exec on --x, should succeed...");
207 FAIL_IF(check_exec_fault(PROT_EXEC));
208 printf("ok!\n");
210 printf("Testing exec on r-x, should succeed...");
211 FAIL_IF(check_exec_fault(PROT_READ | PROT_EXEC));
212 printf("ok!\n");
214 printf("Testing exec on -wx, should succeed...");
215 FAIL_IF(check_exec_fault(PROT_WRITE | PROT_EXEC));
216 printf("ok!\n");
218 printf("Testing exec on rwx, should succeed...");
219 FAIL_IF(check_exec_fault(PROT_READ | PROT_WRITE | PROT_EXEC));
220 printf("ok!\n");
222 /* Cleanup */
223 FAIL_IF(munmap((void *)insns, pgsize));
225 return 0;
228 int main(void)
230 return test_harness(test, "exec_prot");