Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | // SPDX-License-Identifier: GPL-2.0-only /* * Copyright (C) 2016 Google, Inc. * * Original Code by Pavel Labath <labath@google.com> * * Code modified by Pratyush Anand <panand@redhat.com> * for testing different byte select for each access size. */ #define _GNU_SOURCE #include <asm/ptrace.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/ptrace.h> #include <sys/param.h> #include <sys/uio.h> #include <stdint.h> #include <stdbool.h> #include <stddef.h> #include <string.h> #include <stdio.h> #include <unistd.h> #include <elf.h> #include <errno.h> #include <signal.h> #include "../kselftest.h" static volatile uint8_t var[96] __attribute__((__aligned__(32))); static void child(int size, int wr) { volatile uint8_t *addr = &var[32 + wr]; if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != 0) { ksft_print_msg( "ptrace(PTRACE_TRACEME) failed: %s\n", strerror(errno)); _exit(1); } if (raise(SIGSTOP) != 0) { ksft_print_msg( "raise(SIGSTOP) failed: %s\n", strerror(errno)); _exit(1); } if ((uintptr_t) addr % size) { ksft_print_msg( "Wrong address write for the given size: %s\n", strerror(errno)); _exit(1); } switch (size) { case 1: *addr = 47; break; case 2: *(uint16_t *)addr = 47; break; case 4: *(uint32_t *)addr = 47; break; case 8: *(uint64_t *)addr = 47; break; case 16: __asm__ volatile ("stp x29, x30, %0" : "=m" (addr[0])); break; case 32: __asm__ volatile ("stp q29, q30, %0" : "=m" (addr[0])); break; } _exit(0); } static bool set_watchpoint(pid_t pid, int size, int wp) { const volatile uint8_t *addr = &var[32 + wp]; const int offset = (uintptr_t)addr % 8; const unsigned int byte_mask = ((1 << size) - 1) << offset; const unsigned int type = 2; /* Write */ const unsigned int enable = 1; const unsigned int control = byte_mask << 5 | type << 3 | enable; struct user_hwdebug_state dreg_state; struct iovec iov; memset(&dreg_state, 0, sizeof(dreg_state)); dreg_state.dbg_regs[0].addr = (uintptr_t)(addr - offset); dreg_state.dbg_regs[0].ctrl = control; iov.iov_base = &dreg_state; iov.iov_len = offsetof(struct user_hwdebug_state, dbg_regs) + sizeof(dreg_state.dbg_regs[0]); if (ptrace(PTRACE_SETREGSET, pid, NT_ARM_HW_WATCH, &iov) == 0) return true; if (errno == EIO) ksft_print_msg( "ptrace(PTRACE_SETREGSET, NT_ARM_HW_WATCH) not supported on this hardware: %s\n", strerror(errno)); ksft_print_msg( "ptrace(PTRACE_SETREGSET, NT_ARM_HW_WATCH) failed: %s\n", strerror(errno)); return false; } static bool run_test(int wr_size, int wp_size, int wr, int wp) { int status; siginfo_t siginfo; pid_t pid = fork(); pid_t wpid; if (pid < 0) { ksft_test_result_fail( "fork() failed: %s\n", strerror(errno)); return false; } if (pid == 0) child(wr_size, wr); wpid = waitpid(pid, &status, __WALL); if (wpid != pid) { ksft_print_msg( "waitpid() failed: %s\n", strerror(errno)); return false; } if (!WIFSTOPPED(status)) { ksft_print_msg( "child did not stop: %s\n", strerror(errno)); return false; } if (WSTOPSIG(status) != SIGSTOP) { ksft_print_msg("child did not stop with SIGSTOP\n"); return false; } if (!set_watchpoint(pid, wp_size, wp)) return false; if (ptrace(PTRACE_CONT, pid, NULL, NULL) < 0) { ksft_print_msg( "ptrace(PTRACE_CONT) failed: %s\n", strerror(errno)); return false; } alarm(3); wpid = waitpid(pid, &status, __WALL); if (wpid != pid) { ksft_print_msg( "waitpid() failed: %s\n", strerror(errno)); return false; } alarm(0); if (WIFEXITED(status)) { ksft_print_msg("child exited prematurely\n"); return false; } if (!WIFSTOPPED(status)) { ksft_print_msg("child did not stop\n"); return false; } if (WSTOPSIG(status) != SIGTRAP) { ksft_print_msg("child did not stop with SIGTRAP\n"); return false; } if (ptrace(PTRACE_GETSIGINFO, pid, NULL, &siginfo) != 0) { ksft_print_msg( "ptrace(PTRACE_GETSIGINFO): %s\n", strerror(errno)); return false; } if (siginfo.si_code != TRAP_HWBKPT) { ksft_print_msg( "Unexpected si_code %d\n", siginfo.si_code); return false; } kill(pid, SIGKILL); wpid = waitpid(pid, &status, 0); if (wpid != pid) { ksft_print_msg( "waitpid() failed: %s\n", strerror(errno)); return false; } return true; } static void sigalrm(int sig) { } int main(int argc, char **argv) { int opt; bool succeeded = true; struct sigaction act; int wr, wp, size; bool result; ksft_print_header(); ksft_set_plan(213); act.sa_handler = sigalrm; sigemptyset(&act.sa_mask); act.sa_flags = 0; sigaction(SIGALRM, &act, NULL); for (size = 1; size <= 32; size = size*2) { for (wr = 0; wr <= 32; wr = wr + size) { for (wp = wr - size; wp <= wr + size; wp = wp + size) { result = run_test(size, MIN(size, 8), wr, wp); if ((result && wr == wp) || (!result && wr != wp)) ksft_test_result_pass( "Test size = %d write offset = %d watchpoint offset = %d\n", size, wr, wp); else { ksft_test_result_fail( "Test size = %d write offset = %d watchpoint offset = %d\n", size, wr, wp); succeeded = false; } } } } for (size = 1; size <= 32; size = size*2) { if (run_test(size, 8, -size, -8)) ksft_test_result_pass( "Test size = %d write offset = %d watchpoint offset = -8\n", size, -size); else { ksft_test_result_fail( "Test size = %d write offset = %d watchpoint offset = -8\n", size, -size); succeeded = false; } } if (succeeded) ksft_exit_pass(); else ksft_exit_fail(); } |