1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011 Red Hat, Inc., Frederic Weisbecker <fweisbec@redhat.com>
5 * Selftests for breakpoints (and more generally the do_debug() path) in x86.
9 #include <sys/ptrace.h>
16 #include <sys/types.h>
21 #include "../kselftest.h"
23 #define COUNT_ISN_BPS 4
26 /* Breakpoint access modes */
33 static pid_t child_pid
;
36 * Ensures the child and parent are always "talking" about
37 * the same test sequence. (ie: that we haven't forgotten
38 * to call check_trapped() somewhere).
42 static void set_breakpoint_addr(void *addr
, int n
)
46 ret
= ptrace(PTRACE_POKEUSER
, child_pid
,
47 offsetof(struct user
, u_debugreg
[n
]), addr
);
49 ksft_exit_fail_msg("Can't set breakpoint addr: %s\n",
53 static void toggle_breakpoint(int n
, int type
, int len
,
54 int local
, int global
, int set
)
59 unsigned long vdr7
, dr7
;
88 dr7
= ptrace(PTRACE_PEEKUSER
, child_pid
,
89 offsetof(struct user
, u_debugreg
[7]), 0);
91 vdr7
= (xlen
| xtype
) << 16;
108 ret
= ptrace(PTRACE_POKEUSER
, child_pid
,
109 offsetof(struct user
, u_debugreg
[7]), dr7
);
111 ksft_print_msg("Can't set dr7: %s\n", strerror(errno
));
116 /* Dummy variables to test read/write accesses */
117 static unsigned long long dummy_var
[4];
119 /* Dummy functions to test execution accesses */
120 static void dummy_func(void) { }
121 static void dummy_func1(void) { }
122 static void dummy_func2(void) { }
123 static void dummy_func3(void) { }
125 static void (*dummy_funcs
[])(void) = {
134 static void check_trapped(void)
137 * If we haven't trapped, wake up the parent
138 * so that it notices the failure.
141 kill(getpid(), SIGUSR1
);
147 static void write_var(int len
)
149 char *pcval
; short *psval
; int *pival
; long long *plval
;
152 for (i
= 0; i
< 4; i
++) {
155 pcval
= (char *)&dummy_var
[i
];
159 psval
= (short *)&dummy_var
[i
];
163 pival
= (int *)&dummy_var
[i
];
167 plval
= (long long *)&dummy_var
[i
];
168 *plval
= 0xffffffffffffffffLL
;
175 static void read_var(int len
)
177 char cval
; short sval
; int ival
; long long lval
;
180 for (i
= 0; i
< 4; i
++) {
183 cval
= *(char *)&dummy_var
[i
];
186 sval
= *(short *)&dummy_var
[i
];
189 ival
= *(int *)&dummy_var
[i
];
192 lval
= *(long long *)&dummy_var
[i
];
200 * Do the r/w/x accesses to trigger the breakpoints. And run
203 static void trigger_tests(void)
205 int len
, local
, global
, i
;
209 ret
= ptrace(PTRACE_TRACEME
, 0, NULL
, 0);
211 ksft_print_msg("Can't be traced? %s\n", strerror(errno
));
215 /* Wake up father so that it sets up the first test */
216 kill(getpid(), SIGUSR1
);
218 /* Test instruction breakpoints */
219 for (local
= 0; local
< 2; local
++) {
220 for (global
= 0; global
< 2; global
++) {
221 if (!local
&& !global
)
224 for (i
= 0; i
< COUNT_ISN_BPS
; i
++) {
231 /* Test write watchpoints */
232 for (len
= 1; len
<= sizeof(long); len
<<= 1) {
233 for (local
= 0; local
< 2; local
++) {
234 for (global
= 0; global
< 2; global
++) {
235 if (!local
&& !global
)
242 /* Test read/write watchpoints (on read accesses) */
243 for (len
= 1; len
<= sizeof(long); len
<<= 1) {
244 for (local
= 0; local
< 2; local
++) {
245 for (global
= 0; global
< 2; global
++) {
246 if (!local
&& !global
)
261 kill(getpid(), SIGUSR1
);
264 static void check_success(const char *msg
)
270 /* Wait for the child to SIGTRAP */
275 if (WSTOPSIG(status
) == SIGTRAP
) {
276 child_nr_tests
= ptrace(PTRACE_PEEKDATA
, child_pid
,
278 if (child_nr_tests
== nr_tests
)
280 if (ptrace(PTRACE_POKEDATA
, child_pid
, &trapped
, 1))
281 ksft_exit_fail_msg("Can't poke: %s\n", strerror(errno
));
287 ksft_test_result_pass(msg
);
289 ksft_test_result_fail(msg
);
292 static void launch_instruction_breakpoints(char *buf
, int local
, int global
)
296 for (i
= 0; i
< COUNT_ISN_BPS
; i
++) {
297 set_breakpoint_addr(dummy_funcs
[i
], i
);
298 toggle_breakpoint(i
, BP_X
, 1, local
, global
, 1);
299 ptrace(PTRACE_CONT
, child_pid
, NULL
, 0);
300 sprintf(buf
, "Test breakpoint %d with local: %d global: %d\n",
303 toggle_breakpoint(i
, BP_X
, 1, local
, global
, 0);
307 static void launch_watchpoints(char *buf
, int mode
, int len
,
308 int local
, int global
)
310 const char *mode_str
;
318 for (i
= 0; i
< COUNT_WPS
; i
++) {
319 set_breakpoint_addr(&dummy_var
[i
], i
);
320 toggle_breakpoint(i
, mode
, len
, local
, global
, 1);
321 ptrace(PTRACE_CONT
, child_pid
, NULL
, 0);
323 "Test %s watchpoint %d with len: %d local: %d global: %d\n",
324 mode_str
, i
, len
, local
, global
);
326 toggle_breakpoint(i
, mode
, len
, local
, global
, 0);
330 /* Set the breakpoints and check the child successfully trigger them */
331 static void launch_tests(void)
334 unsigned int tests
= 0;
335 int len
, local
, global
, i
;
337 tests
+= 3 * COUNT_ISN_BPS
;
338 tests
+= sizeof(long) / 2 * 3 * COUNT_WPS
;
339 tests
+= sizeof(long) / 2 * 3 * COUNT_WPS
;
341 ksft_set_plan(tests
);
343 /* Instruction breakpoints */
344 for (local
= 0; local
< 2; local
++) {
345 for (global
= 0; global
< 2; global
++) {
346 if (!local
&& !global
)
348 launch_instruction_breakpoints(buf
, local
, global
);
352 /* Write watchpoint */
353 for (len
= 1; len
<= sizeof(long); len
<<= 1) {
354 for (local
= 0; local
< 2; local
++) {
355 for (global
= 0; global
< 2; global
++) {
356 if (!local
&& !global
)
358 launch_watchpoints(buf
, BP_W
, len
,
364 /* Read-Write watchpoint */
365 for (len
= 1; len
<= sizeof(long); len
<<= 1) {
366 for (local
= 0; local
< 2; local
++) {
367 for (global
= 0; global
< 2; global
++) {
368 if (!local
&& !global
)
370 launch_watchpoints(buf
, BP_RW
, len
,
377 ptrace(PTRACE_CONT
, child_pid
, NULL
, 0);
378 check_success("Test icebp\n");
381 ptrace(PTRACE_CONT
, child_pid
, NULL
, 0);
382 check_success("Test int 3 trap\n");
384 ptrace(PTRACE_CONT
, child_pid
, NULL
, 0);
387 int main(int argc
, char **argv
)