2 * Copyright (C) 2016 Google, Inc.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * Original Code by Pavel Labath <labath@google.com>
15 * Code modified by Pratyush Anand <panand@redhat.com>
16 * for testing different byte select for each access size.
22 #include <sys/types.h>
24 #include <sys/ptrace.h>
25 #include <sys/param.h>
37 #include "../kselftest.h"
39 static volatile uint8_t var[96] __attribute__((__aligned__(32)));
41 static void child(int size, int wr)
43 volatile uint8_t *addr = &var[32 + wr];
45 if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != 0) {
47 "ptrace(PTRACE_TRACEME) failed: %s\n",
52 if (raise(SIGSTOP) != 0) {
54 "raise(SIGSTOP) failed: %s\n", strerror(errno));
58 if ((uintptr_t) addr % size) {
60 "Wrong address write for the given size: %s\n",
70 *(uint16_t *)addr = 47;
73 *(uint32_t *)addr = 47;
76 *(uint64_t *)addr = 47;
79 __asm__ volatile ("stp x29, x30, %0" : "=m" (addr[0]));
82 __asm__ volatile ("stp q29, q30, %0" : "=m" (addr[0]));
89 static bool set_watchpoint(pid_t pid, int size, int wp)
91 const volatile uint8_t *addr = &var[32 + wp];
92 const int offset = (uintptr_t)addr % 8;
93 const unsigned int byte_mask = ((1 << size) - 1) << offset;
94 const unsigned int type = 2; /* Write */
95 const unsigned int enable = 1;
96 const unsigned int control = byte_mask << 5 | type << 3 | enable;
97 struct user_hwdebug_state dreg_state;
100 memset(&dreg_state, 0, sizeof(dreg_state));
101 dreg_state.dbg_regs[0].addr = (uintptr_t)(addr - offset);
102 dreg_state.dbg_regs[0].ctrl = control;
103 iov.iov_base = &dreg_state;
104 iov.iov_len = offsetof(struct user_hwdebug_state, dbg_regs) +
105 sizeof(dreg_state.dbg_regs[0]);
106 if (ptrace(PTRACE_SETREGSET, pid, NT_ARM_HW_WATCH, &iov) == 0)
111 "ptrace(PTRACE_SETREGSET, NT_ARM_HW_WATCH) not supported on this hardware: %s\n",
115 "ptrace(PTRACE_SETREGSET, NT_ARM_HW_WATCH) failed: %s\n",
120 static bool run_test(int wr_size, int wp_size, int wr, int wp)
128 ksft_test_result_fail(
129 "fork() failed: %s\n", strerror(errno));
135 wpid = waitpid(pid, &status, __WALL);
138 "waitpid() failed: %s\n", strerror(errno));
141 if (!WIFSTOPPED(status)) {
143 "child did not stop: %s\n", strerror(errno));
146 if (WSTOPSIG(status) != SIGSTOP) {
147 ksft_print_msg("child did not stop with SIGSTOP\n");
151 if (!set_watchpoint(pid, wp_size, wp))
154 if (ptrace(PTRACE_CONT, pid, NULL, NULL) < 0) {
156 "ptrace(PTRACE_SINGLESTEP) failed: %s\n",
162 wpid = waitpid(pid, &status, __WALL);
165 "waitpid() failed: %s\n", strerror(errno));
169 if (WIFEXITED(status)) {
170 ksft_print_msg("child did not single-step\n");
173 if (!WIFSTOPPED(status)) {
174 ksft_print_msg("child did not stop\n");
177 if (WSTOPSIG(status) != SIGTRAP) {
178 ksft_print_msg("child did not stop with SIGTRAP\n");
181 if (ptrace(PTRACE_GETSIGINFO, pid, NULL, &siginfo) != 0) {
183 "ptrace(PTRACE_GETSIGINFO): %s\n",
187 if (siginfo.si_code != TRAP_HWBKPT) {
189 "Unexpected si_code %d\n", siginfo.si_code);
194 wpid = waitpid(pid, &status, 0);
197 "waitpid() failed: %s\n", strerror(errno));
203 static void sigalrm(int sig)
207 int main(int argc, char **argv)
210 bool succeeded = true;
211 struct sigaction act;
217 act.sa_handler = sigalrm;
218 sigemptyset(&act.sa_mask);
220 sigaction(SIGALRM, &act, NULL);
221 for (size = 1; size <= 32; size = size*2) {
222 for (wr = 0; wr <= 32; wr = wr + size) {
223 for (wp = wr - size; wp <= wr + size; wp = wp + size) {
224 result = run_test(size, MIN(size, 8), wr, wp);
225 if ((result && wr == wp) ||
226 (!result && wr != wp))
227 ksft_test_result_pass(
228 "Test size = %d write offset = %d watchpoint offset = %d\n",
231 ksft_test_result_fail(
232 "Test size = %d write offset = %d watchpoint offset = %d\n",
240 for (size = 1; size <= 32; size = size*2) {
241 if (run_test(size, 8, -size, -8))
242 ksft_test_result_pass(
243 "Test size = %d write offset = %d watchpoint offset = -8\n",
246 ksft_test_result_fail(
247 "Test size = %d write offset = %d watchpoint offset = -8\n",