2 * Copyright (C) 2011 Red Hat, Inc., Frederic Weisbecker <fweisbec@redhat.com>
4 * Licensed under the terms of the GNU GPL License version 2
6 * Selftests for breakpoints (and more generally the do_debug() path) in x86.
10 #include <sys/ptrace.h>
17 #include <sys/types.h>
20 #include "../kselftest.h"
23 /* Breakpoint access modes */
30 static pid_t child_pid
;
33 * Ensures the child and parent are always "talking" about
34 * the same test sequence. (ie: that we haven't forgotten
35 * to call check_trapped() somewhere).
39 static void set_breakpoint_addr(void *addr
, int n
)
43 ret
= ptrace(PTRACE_POKEUSER
, child_pid
,
44 offsetof(struct user
, u_debugreg
[n
]), addr
);
46 perror("Can't set breakpoint addr\n");
51 static void toggle_breakpoint(int n
, int type
, int len
,
52 int local
, int global
, int set
)
57 unsigned long vdr7
, dr7
;
86 dr7
= ptrace(PTRACE_PEEKUSER
, child_pid
,
87 offsetof(struct user
, u_debugreg
[7]), 0);
89 vdr7
= (xlen
| xtype
) << 16;
106 ret
= ptrace(PTRACE_POKEUSER
, child_pid
,
107 offsetof(struct user
, u_debugreg
[7]), dr7
);
109 perror("Can't set dr7");
114 /* Dummy variables to test read/write accesses */
115 static unsigned long long dummy_var
[4];
117 /* Dummy functions to test execution accesses */
118 static void dummy_func(void) { }
119 static void dummy_func1(void) { }
120 static void dummy_func2(void) { }
121 static void dummy_func3(void) { }
123 static void (*dummy_funcs
[])(void) = {
132 static void check_trapped(void)
135 * If we haven't trapped, wake up the parent
136 * so that it notices the failure.
139 kill(getpid(), SIGUSR1
);
145 static void write_var(int len
)
147 char *pcval
; short *psval
; int *pival
; long long *plval
;
150 for (i
= 0; i
< 4; i
++) {
153 pcval
= (char *)&dummy_var
[i
];
157 psval
= (short *)&dummy_var
[i
];
161 pival
= (int *)&dummy_var
[i
];
165 plval
= (long long *)&dummy_var
[i
];
166 *plval
= 0xffffffffffffffffLL
;
173 static void read_var(int len
)
175 char cval
; short sval
; int ival
; long long lval
;
178 for (i
= 0; i
< 4; i
++) {
181 cval
= *(char *)&dummy_var
[i
];
184 sval
= *(short *)&dummy_var
[i
];
187 ival
= *(int *)&dummy_var
[i
];
190 lval
= *(long long *)&dummy_var
[i
];
198 * Do the r/w/x accesses to trigger the breakpoints. And run
201 static void trigger_tests(void)
203 int len
, local
, global
, i
;
207 ret
= ptrace(PTRACE_TRACEME
, 0, NULL
, 0);
209 perror("Can't be traced?\n");
213 /* Wake up father so that it sets up the first test */
214 kill(getpid(), SIGUSR1
);
216 /* Test instruction breakpoints */
217 for (local
= 0; local
< 2; local
++) {
218 for (global
= 0; global
< 2; global
++) {
219 if (!local
&& !global
)
222 for (i
= 0; i
< 4; i
++) {
229 /* Test write watchpoints */
230 for (len
= 1; len
<= sizeof(long); len
<<= 1) {
231 for (local
= 0; local
< 2; local
++) {
232 for (global
= 0; global
< 2; global
++) {
233 if (!local
&& !global
)
240 /* Test read/write watchpoints (on read accesses) */
241 for (len
= 1; len
<= sizeof(long); len
<<= 1) {
242 for (local
= 0; local
< 2; local
++) {
243 for (global
= 0; global
< 2; global
++) {
244 if (!local
&& !global
)
259 kill(getpid(), SIGUSR1
);
262 static void check_success(const char *msg
)
268 /* Wait for the child to SIGTRAP */
273 if (WSTOPSIG(status
) == SIGTRAP
) {
274 child_nr_tests
= ptrace(PTRACE_PEEKDATA
, child_pid
,
276 if (child_nr_tests
== nr_tests
)
278 if (ptrace(PTRACE_POKEDATA
, child_pid
, &trapped
, 1)) {
279 perror("Can't poke\n");
286 printf("%s [%s]\n", msg
, msg2
);
289 static void launch_instruction_breakpoints(char *buf
, int local
, int global
)
293 for (i
= 0; i
< 4; i
++) {
294 set_breakpoint_addr(dummy_funcs
[i
], i
);
295 toggle_breakpoint(i
, BP_X
, 1, local
, global
, 1);
296 ptrace(PTRACE_CONT
, child_pid
, NULL
, 0);
297 sprintf(buf
, "Test breakpoint %d with local: %d global: %d",
300 toggle_breakpoint(i
, BP_X
, 1, local
, global
, 0);
304 static void launch_watchpoints(char *buf
, int mode
, int len
,
305 int local
, int global
)
307 const char *mode_str
;
315 for (i
= 0; i
< 4; i
++) {
316 set_breakpoint_addr(&dummy_var
[i
], i
);
317 toggle_breakpoint(i
, mode
, len
, local
, global
, 1);
318 ptrace(PTRACE_CONT
, child_pid
, NULL
, 0);
319 sprintf(buf
, "Test %s watchpoint %d with len: %d local: "
320 "%d global: %d", mode_str
, i
, len
, local
, global
);
322 toggle_breakpoint(i
, mode
, len
, local
, global
, 0);
326 /* Set the breakpoints and check the child successfully trigger them */
327 static void launch_tests(void)
330 int len
, local
, global
, i
;
332 /* Instruction breakpoints */
333 for (local
= 0; local
< 2; local
++) {
334 for (global
= 0; global
< 2; global
++) {
335 if (!local
&& !global
)
337 launch_instruction_breakpoints(buf
, local
, global
);
341 /* Write watchpoint */
342 for (len
= 1; len
<= sizeof(long); len
<<= 1) {
343 for (local
= 0; local
< 2; local
++) {
344 for (global
= 0; global
< 2; global
++) {
345 if (!local
&& !global
)
347 launch_watchpoints(buf
, BP_W
, len
,
353 /* Read-Write watchpoint */
354 for (len
= 1; len
<= sizeof(long); len
<<= 1) {
355 for (local
= 0; local
< 2; local
++) {
356 for (global
= 0; global
< 2; global
++) {
357 if (!local
&& !global
)
359 launch_watchpoints(buf
, BP_RW
, len
,
366 ptrace(PTRACE_CONT
, child_pid
, NULL
, 0);
367 check_success("Test icebp");
370 ptrace(PTRACE_CONT
, child_pid
, NULL
, 0);
371 check_success("Test int 3 trap");
373 ptrace(PTRACE_CONT
, child_pid
, NULL
, 0);
376 int main(int argc
, char **argv
)
395 return ksft_exit_pass();