Rev 1221 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1072 | palkovsky | 1 | /* |
2 | * Copyright (C) 2006 Ondrej Palkovsky |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * - The name of the author may not be used to endorse or promote products |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
1702 | cejka | 29 | /** @addtogroup amd64debug ia32/amd64 |
30 | * @ingroup debug |
||
31 | * @{ |
||
32 | */ |
||
33 | /** @file |
||
34 | */ |
||
35 | |||
1072 | palkovsky | 36 | #include <arch/debugger.h> |
37 | #include <console/kconsole.h> |
||
38 | #include <console/cmd.h> |
||
39 | #include <symtab.h> |
||
40 | #include <print.h> |
||
41 | #include <panic.h> |
||
42 | #include <interrupt.h> |
||
43 | #include <arch/asm.h> |
||
44 | #include <arch/cpu.h> |
||
45 | #include <debug.h> |
||
46 | #include <func.h> |
||
1077 | palkovsky | 47 | #include <smp/ipi.h> |
1072 | palkovsky | 48 | |
49 | typedef struct { |
||
50 | __address address; /**< Breakpoint address */ |
||
51 | int flags; /**< Flags regarding breakpoint */ |
||
52 | int counter; /**< How many times the exception occured */ |
||
53 | } bpinfo_t; |
||
54 | |||
55 | static bpinfo_t breakpoints[BKPOINTS_MAX]; |
||
56 | SPINLOCK_INITIALIZE(bkpoint_lock); |
||
57 | |||
58 | static int cmd_print_breakpoints(cmd_arg_t *argv); |
||
59 | static cmd_info_t bkpts_info = { |
||
60 | .name = "bkpts", |
||
61 | .description = "Print breakpoint table.", |
||
62 | .func = cmd_print_breakpoints, |
||
63 | .argc = 0, |
||
64 | }; |
||
65 | |||
1077 | palkovsky | 66 | #ifndef CONFIG_DEBUG_AS_WATCHPOINT |
67 | |||
1072 | palkovsky | 68 | static int cmd_del_breakpoint(cmd_arg_t *argv); |
69 | static cmd_arg_t del_argv = { |
||
70 | .type = ARG_TYPE_INT |
||
71 | }; |
||
72 | static cmd_info_t delbkpt_info = { |
||
73 | .name = "delbkpt", |
||
74 | .description = "delbkpt <number> - Delete breakpoint.", |
||
75 | .func = cmd_del_breakpoint, |
||
76 | .argc = 1, |
||
77 | .argv = &del_argv |
||
78 | }; |
||
79 | |||
80 | static int cmd_add_breakpoint(cmd_arg_t *argv); |
||
81 | static cmd_arg_t add_argv = { |
||
82 | .type = ARG_TYPE_INT |
||
83 | }; |
||
84 | static cmd_info_t addbkpt_info = { |
||
85 | .name = "addbkpt", |
||
86 | .description = "addbkpt <&symbol> - new breakpoint.", |
||
87 | .func = cmd_add_breakpoint, |
||
88 | .argc = 1, |
||
89 | .argv = &add_argv |
||
90 | }; |
||
91 | |||
92 | static cmd_arg_t addw_argv = { |
||
93 | .type = ARG_TYPE_INT |
||
94 | }; |
||
95 | static cmd_info_t addwatchp_info = { |
||
96 | .name = "addwatchp", |
||
97 | .description = "addbwatchp <&symbol> - new write watchpoint.", |
||
98 | .func = cmd_add_breakpoint, |
||
99 | .argc = 1, |
||
100 | .argv = &addw_argv |
||
101 | }; |
||
102 | |||
1077 | palkovsky | 103 | #endif |
1072 | palkovsky | 104 | |
105 | /** Print table of active breakpoints */ |
||
106 | int cmd_print_breakpoints(cmd_arg_t *argv) |
||
107 | { |
||
108 | int i; |
||
109 | char *symbol; |
||
110 | |||
111 | printf("Breakpoint table.\n"); |
||
112 | for (i=0; i < BKPOINTS_MAX; i++) |
||
113 | if (breakpoints[i].address) { |
||
114 | symbol = get_symtab_entry(breakpoints[i].address); |
||
1196 | cejka | 115 | printf("%d. %p in %s\n",i, |
1072 | palkovsky | 116 | breakpoints[i].address, symbol); |
117 | printf(" Count(%d) ", breakpoints[i].counter); |
||
118 | printf("\n"); |
||
119 | } |
||
120 | return 1; |
||
121 | } |
||
122 | |||
1077 | palkovsky | 123 | /* Setup DR register according to table */ |
124 | static void setup_dr(int curidx) |
||
125 | { |
||
126 | __native dr7; |
||
127 | bpinfo_t *cur = &breakpoints[curidx]; |
||
128 | int flags = breakpoints[curidx].flags; |
||
129 | |||
130 | /* Disable breakpoint in DR7 */ |
||
131 | dr7 = read_dr7(); |
||
132 | dr7 &= ~(0x2 << (curidx*2)); |
||
133 | |||
134 | if (cur->address) { /* Setup DR register */ |
||
135 | /* Set breakpoint to debug registers */ |
||
136 | switch (curidx) { |
||
137 | case 0: |
||
138 | write_dr0(cur->address); |
||
139 | break; |
||
140 | case 1: |
||
141 | write_dr1(cur->address); |
||
142 | break; |
||
143 | case 2: |
||
144 | write_dr2(cur->address); |
||
145 | break; |
||
146 | case 3: |
||
147 | write_dr3(cur->address); |
||
148 | break; |
||
149 | } |
||
150 | /* Set type to requested breakpoint & length*/ |
||
151 | dr7 &= ~ (0x3 << (16 + 4*curidx)); |
||
152 | dr7 &= ~ (0x3 << (18 + 4*curidx)); |
||
153 | if ((flags & BKPOINT_INSTR)) { |
||
154 | ; |
||
155 | } else { |
||
156 | if (sizeof(int) == 4) |
||
157 | dr7 |= ((__native) 0x3) << (18 + 4*curidx); |
||
158 | else /* 8 */ |
||
159 | dr7 |= ((__native) 0x2) << (18 + 4*curidx); |
||
160 | |||
161 | if ((flags & BKPOINT_WRITE)) |
||
162 | dr7 |= ((__native) 0x1) << (16 + 4*curidx); |
||
163 | else if ((flags & BKPOINT_READ_WRITE)) |
||
164 | dr7 |= ((__native) 0x3) << (16 + 4*curidx); |
||
165 | } |
||
166 | |||
167 | /* Enable global breakpoint */ |
||
168 | dr7 |= 0x2 << (curidx*2); |
||
169 | |||
170 | write_dr7(dr7); |
||
171 | |||
172 | } |
||
173 | } |
||
174 | |||
1072 | palkovsky | 175 | /** Enable hardware breakpoint |
176 | * |
||
177 | * |
||
178 | * @param where Address of HW breakpoint |
||
179 | * @param flags Type of breakpoint (EXECUTE, WRITE) |
||
180 | * @return Debug slot on success, -1 - no available HW breakpoint |
||
181 | */ |
||
1077 | palkovsky | 182 | int breakpoint_add(void * where, int flags, int curidx) |
1072 | palkovsky | 183 | { |
184 | ipl_t ipl; |
||
185 | int i; |
||
1077 | palkovsky | 186 | bpinfo_t *cur; |
1072 | palkovsky | 187 | |
188 | ASSERT( flags & (BKPOINT_INSTR | BKPOINT_WRITE | BKPOINT_READ_WRITE)); |
||
189 | |||
190 | ipl = interrupts_disable(); |
||
191 | spinlock_lock(&bkpoint_lock); |
||
192 | |||
1077 | palkovsky | 193 | if (curidx == -1) { |
194 | /* Find free space in slots */ |
||
195 | for (i=0; i<BKPOINTS_MAX; i++) |
||
196 | if (!breakpoints[i].address) { |
||
197 | curidx = i; |
||
198 | break; |
||
199 | } |
||
200 | if (curidx == -1) { |
||
201 | /* Too many breakpoints */ |
||
202 | spinlock_unlock(&bkpoint_lock); |
||
203 | interrupts_restore(ipl); |
||
204 | return -1; |
||
1072 | palkovsky | 205 | } |
206 | } |
||
1077 | palkovsky | 207 | cur = &breakpoints[curidx]; |
208 | |||
1072 | palkovsky | 209 | cur->address = (__address) where; |
210 | cur->flags = flags; |
||
211 | cur->counter = 0; |
||
212 | |||
1077 | palkovsky | 213 | setup_dr(curidx); |
1072 | palkovsky | 214 | |
215 | spinlock_unlock(&bkpoint_lock); |
||
216 | interrupts_restore(ipl); |
||
217 | |||
1077 | palkovsky | 218 | /* Send IPI */ |
219 | #ifdef CONFIG_SMP |
||
220 | // ipi_broadcast(VECTOR_DEBUG_IPI); |
||
221 | #endif |
||
222 | |||
1072 | palkovsky | 223 | return curidx; |
224 | } |
||
225 | |||
1074 | palkovsky | 226 | #ifdef amd64 |
227 | # define getip(x) ((x)->rip) |
||
228 | #else |
||
229 | # define getip(x) ((x)->eip) |
||
230 | #endif |
||
231 | |||
1072 | palkovsky | 232 | static void handle_exception(int slot, istate_t *istate) |
233 | { |
||
234 | ASSERT(breakpoints[slot].address); |
||
235 | |||
236 | /* Handle zero checker */ |
||
237 | if (! (breakpoints[slot].flags & BKPOINT_INSTR)) { |
||
238 | if ((breakpoints[slot].flags & BKPOINT_CHECK_ZERO)) { |
||
239 | if (*((__native *) breakpoints[slot].address) != 0) |
||
240 | return; |
||
1221 | decky | 241 | printf("**** Found ZERO on address %p ****\n", |
1072 | palkovsky | 242 | slot, breakpoints[slot].address); |
243 | } else { |
||
1221 | decky | 244 | printf("Data watchpoint - new data: %p\n", |
1072 | palkovsky | 245 | *((__native *) breakpoints[slot].address)); |
246 | } |
||
247 | } |
||
1221 | decky | 248 | printf("Reached breakpoint %d:%p(%s)\n", slot, getip(istate), |
1074 | palkovsky | 249 | get_symtab_entry(getip(istate))); |
1072 | palkovsky | 250 | printf("***Type 'exit' to exit kconsole.\n"); |
251 | atomic_set(&haltstate,1); |
||
252 | kconsole("debug"); |
||
253 | atomic_set(&haltstate,0); |
||
254 | } |
||
255 | |||
256 | void breakpoint_del(int slot) |
||
257 | { |
||
258 | bpinfo_t *cur; |
||
259 | ipl_t ipl; |
||
260 | |||
261 | ipl = interrupts_disable(); |
||
262 | spinlock_lock(&bkpoint_lock); |
||
263 | |||
264 | cur = &breakpoints[slot]; |
||
265 | if (!cur->address) { |
||
266 | spinlock_unlock(&bkpoint_lock); |
||
267 | interrupts_restore(ipl); |
||
268 | return; |
||
269 | } |
||
270 | |||
271 | cur->address = NULL; |
||
272 | |||
1077 | palkovsky | 273 | setup_dr(slot); |
1072 | palkovsky | 274 | |
275 | spinlock_unlock(&bkpoint_lock); |
||
276 | interrupts_restore(ipl); |
||
1077 | palkovsky | 277 | #ifdef CONFIG_SMP |
278 | // ipi_broadcast(VECTOR_DEBUG_IPI); |
||
279 | #endif |
||
1072 | palkovsky | 280 | } |
281 | |||
1077 | palkovsky | 282 | #ifndef CONFIG_DEBUG_AS_WATCHPOINT |
283 | |||
1072 | palkovsky | 284 | /** Remove breakpoint from table */ |
285 | int cmd_del_breakpoint(cmd_arg_t *argv) |
||
286 | { |
||
287 | if (argv->intval < 0 || argv->intval > BKPOINTS_MAX) { |
||
288 | printf("Invalid breakpoint number.\n"); |
||
289 | return 0; |
||
290 | } |
||
291 | breakpoint_del(argv->intval); |
||
292 | return 1; |
||
293 | } |
||
294 | |||
295 | /** Add new breakpoint to table */ |
||
296 | static int cmd_add_breakpoint(cmd_arg_t *argv) |
||
297 | { |
||
298 | int flags; |
||
1077 | palkovsky | 299 | int id; |
1072 | palkovsky | 300 | |
301 | if (argv == &add_argv) { |
||
302 | flags = BKPOINT_INSTR; |
||
303 | } else { /* addwatchp */ |
||
304 | flags = BKPOINT_WRITE; |
||
305 | } |
||
306 | printf("Adding breakpoint on address: %p\n", argv->intval); |
||
1077 | palkovsky | 307 | id = breakpoint_add((void *)argv->intval, flags, -1); |
308 | if (id < 0) |
||
1072 | palkovsky | 309 | printf("Add breakpoint failed.\n"); |
1077 | palkovsky | 310 | else |
311 | printf("Added breakpoint %d.\n", id); |
||
1072 | palkovsky | 312 | |
313 | return 1; |
||
314 | } |
||
1077 | palkovsky | 315 | #endif |
1072 | palkovsky | 316 | |
1077 | palkovsky | 317 | static void debug_exception(int n, istate_t *istate) |
318 | { |
||
319 | __native dr6; |
||
320 | int i; |
||
321 | |||
322 | /* Set RF to restart the instruction */ |
||
323 | #ifdef amd64 |
||
324 | istate->rflags |= RFLAGS_RF; |
||
325 | #else |
||
326 | istate->eflags |= EFLAGS_RF; |
||
327 | #endif |
||
328 | |||
329 | dr6 = read_dr6(); |
||
330 | for (i=0; i < BKPOINTS_MAX; i++) { |
||
331 | if (dr6 & (1 << i)) { |
||
332 | dr6 &= ~ (1 << i); |
||
333 | write_dr6(dr6); |
||
334 | |||
335 | handle_exception(i, istate); |
||
336 | } |
||
337 | } |
||
338 | } |
||
339 | |||
340 | #ifdef CONFIG_SMP |
||
341 | static void debug_ipi(int n, istate_t *istate) |
||
342 | { |
||
343 | int i; |
||
344 | |||
345 | spinlock_lock(&bkpoint_lock); |
||
346 | for (i=0; i < BKPOINTS_MAX; i++) |
||
347 | setup_dr(i); |
||
348 | spinlock_unlock(&bkpoint_lock); |
||
349 | } |
||
350 | #endif |
||
351 | |||
1072 | palkovsky | 352 | /** Initialize debugger */ |
353 | void debugger_init() |
||
354 | { |
||
355 | int i; |
||
356 | |||
357 | for (i=0; i<BKPOINTS_MAX; i++) |
||
358 | breakpoints[i].address = NULL; |
||
359 | |||
360 | cmd_initialize(&bkpts_info); |
||
361 | if (!cmd_register(&bkpts_info)) |
||
362 | panic("could not register command %s\n", bkpts_info.name); |
||
363 | |||
1077 | palkovsky | 364 | #ifndef CONFIG_DEBUG_AS_WATCHPOINT |
1072 | palkovsky | 365 | cmd_initialize(&delbkpt_info); |
366 | if (!cmd_register(&delbkpt_info)) |
||
367 | panic("could not register command %s\n", delbkpt_info.name); |
||
368 | |||
369 | cmd_initialize(&addbkpt_info); |
||
370 | if (!cmd_register(&addbkpt_info)) |
||
371 | panic("could not register command %s\n", addbkpt_info.name); |
||
372 | |||
373 | cmd_initialize(&addwatchp_info); |
||
374 | if (!cmd_register(&addwatchp_info)) |
||
375 | panic("could not register command %s\n", addwatchp_info.name); |
||
1077 | palkovsky | 376 | #endif |
1072 | palkovsky | 377 | |
378 | exc_register(VECTOR_DEBUG, "debugger", |
||
379 | debug_exception); |
||
1077 | palkovsky | 380 | #ifdef CONFIG_SMP |
381 | exc_register(VECTOR_DEBUG_IPI, "debugger_smp", |
||
382 | debug_ipi); |
||
383 | #endif |
||
1072 | palkovsky | 384 | } |
1702 | cejka | 385 | |
386 | /** @} |
||
387 | */ |
||
388 |