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