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>
22 #include "../kselftest.h"
25 /* Breakpoint access modes */
32 static pid_t child_pid
;
35 * Ensures the child and parent are always "talking" about
36 * the same test sequence. (ie: that we haven't forgotten
37 * to call check_trapped() somewhere).
41 static void set_breakpoint_addr(void *addr
, int n
)
45 ret
= ptrace(PTRACE_POKEUSER
, child_pid
,
46 offsetof(struct user
, u_debugreg
[n
]), addr
);
48 ksft_exit_fail_msg("Can't set breakpoint addr: %s\n",
52 static void toggle_breakpoint(int n
, int type
, int len
,
53 int local
, int global
, int set
)
58 unsigned long vdr7
, dr7
;
87 dr7
= ptrace(PTRACE_PEEKUSER
, child_pid
,
88 offsetof(struct user
, u_debugreg
[7]), 0);
90 vdr7
= (xlen
| xtype
) << 16;
107 ret
= ptrace(PTRACE_POKEUSER
, child_pid
,
108 offsetof(struct user
, u_debugreg
[7]), dr7
);
110 ksft_print_msg("Can't set dr7: %s\n", strerror(errno
));
115 /* Dummy variables to test read/write accesses */
116 static unsigned long long dummy_var
[4];
118 /* Dummy functions to test execution accesses */
119 static void dummy_func(void) { }
120 static void dummy_func1(void) { }
121 static void dummy_func2(void) { }
122 static void dummy_func3(void) { }
124 static void (*dummy_funcs
[])(void) = {
133 static void check_trapped(void)
136 * If we haven't trapped, wake up the parent
137 * so that it notices the failure.
140 kill(getpid(), SIGUSR1
);
146 static void write_var(int len
)
148 char *pcval
; short *psval
; int *pival
; long long *plval
;
151 for (i
= 0; i
< 4; i
++) {
154 pcval
= (char *)&dummy_var
[i
];
158 psval
= (short *)&dummy_var
[i
];
162 pival
= (int *)&dummy_var
[i
];
166 plval
= (long long *)&dummy_var
[i
];
167 *plval
= 0xffffffffffffffffLL
;
174 static void read_var(int len
)
176 char cval
; short sval
; int ival
; long long lval
;
179 for (i
= 0; i
< 4; i
++) {
182 cval
= *(char *)&dummy_var
[i
];
185 sval
= *(short *)&dummy_var
[i
];
188 ival
= *(int *)&dummy_var
[i
];
191 lval
= *(long long *)&dummy_var
[i
];
199 * Do the r/w/x accesses to trigger the breakpoints. And run
202 static void trigger_tests(void)
204 int len
, local
, global
, i
;
208 ret
= ptrace(PTRACE_TRACEME
, 0, NULL
, 0);
210 ksft_print_msg("Can't be traced? %s\n", strerror(errno
));
214 /* Wake up father so that it sets up the first test */
215 kill(getpid(), SIGUSR1
);
217 /* Test instruction breakpoints */
218 for (local
= 0; local
< 2; local
++) {
219 for (global
= 0; global
< 2; global
++) {
220 if (!local
&& !global
)
223 for (i
= 0; i
< 4; i
++) {
230 /* Test write watchpoints */
231 for (len
= 1; len
<= sizeof(long); len
<<= 1) {
232 for (local
= 0; local
< 2; local
++) {
233 for (global
= 0; global
< 2; global
++) {
234 if (!local
&& !global
)
241 /* Test read/write watchpoints (on read accesses) */
242 for (len
= 1; len
<= sizeof(long); len
<<= 1) {
243 for (local
= 0; local
< 2; local
++) {
244 for (global
= 0; global
< 2; global
++) {
245 if (!local
&& !global
)
260 kill(getpid(), SIGUSR1
);
263 static void check_success(const char *msg
)
269 /* Wait for the child to SIGTRAP */
274 if (WSTOPSIG(status
) == SIGTRAP
) {
275 child_nr_tests
= ptrace(PTRACE_PEEKDATA
, child_pid
,
277 if (child_nr_tests
== nr_tests
)
279 if (ptrace(PTRACE_POKEDATA
, child_pid
, &trapped
, 1))
280 ksft_exit_fail_msg("Can't poke: %s\n", strerror(errno
));
286 ksft_test_result_pass(msg
);
288 ksft_test_result_fail(msg
);
291 static void launch_instruction_breakpoints(char *buf
, int local
, int global
)
295 for (i
= 0; i
< 4; i
++) {
296 set_breakpoint_addr(dummy_funcs
[i
], i
);
297 toggle_breakpoint(i
, BP_X
, 1, local
, global
, 1);
298 ptrace(PTRACE_CONT
, child_pid
, NULL
, 0);
299 sprintf(buf
, "Test breakpoint %d with local: %d global: %d\n",
302 toggle_breakpoint(i
, BP_X
, 1, local
, global
, 0);
306 static void launch_watchpoints(char *buf
, int mode
, int len
,
307 int local
, int global
)
309 const char *mode_str
;
317 for (i
= 0; i
< 4; i
++) {
318 set_breakpoint_addr(&dummy_var
[i
], i
);
319 toggle_breakpoint(i
, mode
, len
, local
, global
, 1);
320 ptrace(PTRACE_CONT
, child_pid
, NULL
, 0);
322 "Test %s watchpoint %d with len: %d local: %d global: %d\n",
323 mode_str
, i
, len
, local
, global
);
325 toggle_breakpoint(i
, mode
, len
, local
, global
, 0);
329 /* Set the breakpoints and check the child successfully trigger them */
330 static void launch_tests(void)
333 int len
, local
, global
, i
;
335 /* Instruction breakpoints */
336 for (local
= 0; local
< 2; local
++) {
337 for (global
= 0; global
< 2; global
++) {
338 if (!local
&& !global
)
340 launch_instruction_breakpoints(buf
, local
, global
);
344 /* Write watchpoint */
345 for (len
= 1; len
<= sizeof(long); len
<<= 1) {
346 for (local
= 0; local
< 2; local
++) {
347 for (global
= 0; global
< 2; global
++) {
348 if (!local
&& !global
)
350 launch_watchpoints(buf
, BP_W
, len
,
356 /* Read-Write watchpoint */
357 for (len
= 1; len
<= sizeof(long); len
<<= 1) {
358 for (local
= 0; local
< 2; local
++) {
359 for (global
= 0; global
< 2; global
++) {
360 if (!local
&& !global
)
362 launch_watchpoints(buf
, BP_RW
, len
,
369 ptrace(PTRACE_CONT
, child_pid
, NULL
, 0);
370 check_success("Test icebp\n");
373 ptrace(PTRACE_CONT
, child_pid
, NULL
, 0);
374 check_success("Test int 3 trap\n");
376 ptrace(PTRACE_CONT
, child_pid
, NULL
, 0);
379 int main(int argc
, char **argv
)