source: mainline/kernel/arch/amd64/src/debugger.c@ 76fca31

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 76fca31 was 76fca31, checked in by Martin Decky <martin@…>, 17 years ago

kconsole is optional
kernel & uspace framebuffer rewrite with speedups (some things are slightly broken yet)

  • Property mode set to 100644
File size: 9.6 KB
RevLine 
[4e49572]1/*
[df4ed85]2 * Copyright (c) 2006 Ondrej Palkovsky
[4e49572]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
[06e1e95]29/** @addtogroup amd64debug
[b45c443]30 * @{
31 */
32/** @file
33 */
34
[4e49572]35#include <arch/debugger.h>
36#include <console/kconsole.h>
37#include <console/cmd.h>
38#include <symtab.h>
39#include <print.h>
40#include <panic.h>
41#include <interrupt.h>
42#include <arch/asm.h>
43#include <arch/cpu.h>
44#include <debug.h>
45#include <func.h>
[6c6a19e6]46#include <smp/ipi.h>
[4e49572]47
48typedef struct {
[7f1c620]49 uintptr_t address; /**< Breakpoint address */
[4e49572]50 int flags; /**< Flags regarding breakpoint */
51 int counter; /**< How many times the exception occured */
52} bpinfo_t;
53
54static bpinfo_t breakpoints[BKPOINTS_MAX];
55SPINLOCK_INITIALIZE(bkpoint_lock);
56
[76fca31]57#ifdef CONFIG_KCONSOLE
58
[4e49572]59static int cmd_print_breakpoints(cmd_arg_t *argv);
60static cmd_info_t bkpts_info = {
61 .name = "bkpts",
62 .description = "Print breakpoint table.",
63 .func = cmd_print_breakpoints,
64 .argc = 0,
65};
66
[6c6a19e6]67#ifndef CONFIG_DEBUG_AS_WATCHPOINT
68
[4e49572]69static int cmd_del_breakpoint(cmd_arg_t *argv);
70static cmd_arg_t del_argv = {
71 .type = ARG_TYPE_INT
72};
73static cmd_info_t delbkpt_info = {
74 .name = "delbkpt",
75 .description = "delbkpt <number> - Delete breakpoint.",
76 .func = cmd_del_breakpoint,
77 .argc = 1,
78 .argv = &del_argv
79};
80
81static int cmd_add_breakpoint(cmd_arg_t *argv);
82static cmd_arg_t add_argv = {
83 .type = ARG_TYPE_INT
84};
85static cmd_info_t addbkpt_info = {
86 .name = "addbkpt",
87 .description = "addbkpt <&symbol> - new breakpoint.",
88 .func = cmd_add_breakpoint,
89 .argc = 1,
90 .argv = &add_argv
91};
92
93static cmd_arg_t addw_argv = {
94 .type = ARG_TYPE_INT
95};
96static cmd_info_t addwatchp_info = {
97 .name = "addwatchp",
98 .description = "addbwatchp <&symbol> - new write watchpoint.",
99 .func = cmd_add_breakpoint,
100 .argc = 1,
101 .argv = &addw_argv
102};
103
[76fca31]104#endif /* CONFIG_DEBUG_AS_WATCHPOINT */
105#endif /* CONFIG_KCONSOLE */
[4e49572]106
[6c6a19e6]107/* Setup DR register according to table */
108static void setup_dr(int curidx)
109{
[7f1c620]110 unative_t dr7;
[6c6a19e6]111 bpinfo_t *cur = &breakpoints[curidx];
112 int flags = breakpoints[curidx].flags;
113
114 /* Disable breakpoint in DR7 */
115 dr7 = read_dr7();
116 dr7 &= ~(0x2 << (curidx*2));
117
118 if (cur->address) { /* Setup DR register */
119 /* Set breakpoint to debug registers */
120 switch (curidx) {
121 case 0:
122 write_dr0(cur->address);
123 break;
124 case 1:
125 write_dr1(cur->address);
126 break;
127 case 2:
128 write_dr2(cur->address);
129 break;
130 case 3:
131 write_dr3(cur->address);
132 break;
133 }
134 /* Set type to requested breakpoint & length*/
135 dr7 &= ~ (0x3 << (16 + 4*curidx));
136 dr7 &= ~ (0x3 << (18 + 4*curidx));
137 if ((flags & BKPOINT_INSTR)) {
138 ;
139 } else {
[f4c2b6a]140
141#ifdef __32_BITS__
142 dr7 |= ((unative_t) 0x3) << (18 + 4 * curidx);
143#endif
144
145#ifdef __64_BITS__
146 dr7 |= ((unative_t) 0x2) << (18 + 4 * curidx);
147#endif
[6c6a19e6]148
149 if ((flags & BKPOINT_WRITE))
[f4c2b6a]150 dr7 |= ((unative_t) 0x1) << (16 + 4 * curidx);
[6c6a19e6]151 else if ((flags & BKPOINT_READ_WRITE))
[f4c2b6a]152 dr7 |= ((unative_t) 0x3) << (16 + 4 * curidx);
[6c6a19e6]153 }
154
155 /* Enable global breakpoint */
[d7c9fcb]156 dr7 |= 0x2 << (curidx * 2);
[6c6a19e6]157
158 write_dr7(dr7);
159
160 }
161}
162
[4e49572]163/** Enable hardware breakpoint
164 *
165 * @param where Address of HW breakpoint
166 * @param flags Type of breakpoint (EXECUTE, WRITE)
167 * @return Debug slot on success, -1 - no available HW breakpoint
168 */
[7f043c0]169int breakpoint_add(const void *where, const int flags, int curidx)
[4e49572]170{
171 ipl_t ipl;
172 int i;
[6c6a19e6]173 bpinfo_t *cur;
[4e49572]174
[7f043c0]175 ASSERT(flags & (BKPOINT_INSTR | BKPOINT_WRITE | BKPOINT_READ_WRITE));
[4e49572]176
177 ipl = interrupts_disable();
178 spinlock_lock(&bkpoint_lock);
179
[6c6a19e6]180 if (curidx == -1) {
181 /* Find free space in slots */
[7f043c0]182 for (i = 0; i < BKPOINTS_MAX; i++)
[6c6a19e6]183 if (!breakpoints[i].address) {
184 curidx = i;
185 break;
186 }
187 if (curidx == -1) {
188 /* Too many breakpoints */
189 spinlock_unlock(&bkpoint_lock);
190 interrupts_restore(ipl);
191 return -1;
[4e49572]192 }
193 }
[6c6a19e6]194 cur = &breakpoints[curidx];
195
[7f1c620]196 cur->address = (uintptr_t) where;
[4e49572]197 cur->flags = flags;
198 cur->counter = 0;
199
[6c6a19e6]200 setup_dr(curidx);
[4e49572]201
202 spinlock_unlock(&bkpoint_lock);
203 interrupts_restore(ipl);
204
[6c6a19e6]205 /* Send IPI */
206#ifdef CONFIG_SMP
207// ipi_broadcast(VECTOR_DEBUG_IPI);
208#endif
209
[4e49572]210 return curidx;
211}
212
[23d22eb]213#ifdef amd64
[7f043c0]214# define getip(x) ((x)->rip)
[23d22eb]215#else
[7f043c0]216# define getip(x) ((x)->eip)
[23d22eb]217#endif
218
[4e49572]219static void handle_exception(int slot, istate_t *istate)
220{
221 ASSERT(breakpoints[slot].address);
222
223 /* Handle zero checker */
224 if (! (breakpoints[slot].flags & BKPOINT_INSTR)) {
225 if ((breakpoints[slot].flags & BKPOINT_CHECK_ZERO)) {
[7f1c620]226 if (*((unative_t *) breakpoints[slot].address) != 0)
[4e49572]227 return;
[d7c9fcb]228 printf("*** Found ZERO on address %lx (slot %d) ***\n",
229 breakpoints[slot].address, slot);
[4e49572]230 } else {
[7f043c0]231 printf("Data watchpoint - new data: %lx\n",
[d7c9fcb]232 *((unative_t *) breakpoints[slot].address));
[4e49572]233 }
234 }
[7f043c0]235 printf("Reached breakpoint %d:%lx(%s)\n", slot, getip(istate),
[d7c9fcb]236 get_symtab_entry(getip(istate)));
[76fca31]237
238#ifdef CONFIG_KCONSOLE
239 atomic_set(&haltstate, 1);
240 kconsole("debug", "Debug console ready (type 'exit' to continue)\n", false);
241 atomic_set(&haltstate, 0);
242#endif
[4e49572]243}
244
245void breakpoint_del(int slot)
246{
247 bpinfo_t *cur;
248 ipl_t ipl;
249
250 ipl = interrupts_disable();
251 spinlock_lock(&bkpoint_lock);
252
253 cur = &breakpoints[slot];
254 if (!cur->address) {
255 spinlock_unlock(&bkpoint_lock);
256 interrupts_restore(ipl);
257 return;
258 }
259
260 cur->address = NULL;
261
[6c6a19e6]262 setup_dr(slot);
[4e49572]263
264 spinlock_unlock(&bkpoint_lock);
265 interrupts_restore(ipl);
[6c6a19e6]266#ifdef CONFIG_SMP
267// ipi_broadcast(VECTOR_DEBUG_IPI);
268#endif
[4e49572]269}
270
[6c6a19e6]271
272
[7f043c0]273static void debug_exception(int n __attribute__((unused)), istate_t *istate)
[6c6a19e6]274{
[7f1c620]275 unative_t dr6;
[6c6a19e6]276 int i;
277
278 /* Set RF to restart the instruction */
279#ifdef amd64
280 istate->rflags |= RFLAGS_RF;
281#else
282 istate->eflags |= EFLAGS_RF;
283#endif
284
285 dr6 = read_dr6();
286 for (i=0; i < BKPOINTS_MAX; i++) {
287 if (dr6 & (1 << i)) {
288 dr6 &= ~ (1 << i);
289 write_dr6(dr6);
290
291 handle_exception(i, istate);
292 }
293 }
294}
295
296#ifdef CONFIG_SMP
[d7c9fcb]297static void
298debug_ipi(int n __attribute__((unused)),
299 istate_t *istate __attribute__((unused)))
[6c6a19e6]300{
301 int i;
302
303 spinlock_lock(&bkpoint_lock);
[7f043c0]304 for (i = 0; i < BKPOINTS_MAX; i++)
[6c6a19e6]305 setup_dr(i);
306 spinlock_unlock(&bkpoint_lock);
307}
308#endif
[4e49572]309
310/** Initialize debugger */
311void debugger_init()
312{
313 int i;
314
[d7c9fcb]315 for (i = 0; i < BKPOINTS_MAX; i++)
[4e49572]316 breakpoints[i].address = NULL;
[76fca31]317
318#ifdef CONFIG_KCONSOLE
[4e49572]319 cmd_initialize(&bkpts_info);
320 if (!cmd_register(&bkpts_info))
[76fca31]321 printf("Cannot register command %s\n", bkpts_info.name);
[4e49572]322
[6c6a19e6]323#ifndef CONFIG_DEBUG_AS_WATCHPOINT
[4e49572]324 cmd_initialize(&delbkpt_info);
325 if (!cmd_register(&delbkpt_info))
[76fca31]326 printf("Cannot register command %s\n", delbkpt_info.name);
[4e49572]327
328 cmd_initialize(&addbkpt_info);
329 if (!cmd_register(&addbkpt_info))
[76fca31]330 printf("Cannot register command %s\n", addbkpt_info.name);
[4e49572]331
332 cmd_initialize(&addwatchp_info);
333 if (!cmd_register(&addwatchp_info))
[76fca31]334 printf("Cannot register command %s\n", addwatchp_info.name);
335#endif /* CONFIG_DEBUG_AS_WATCHPOINT */
336#endif /* CONFIG_KCONSOLE */
[4e49572]337
[d7c9fcb]338 exc_register(VECTOR_DEBUG, "debugger", debug_exception);
[6c6a19e6]339#ifdef CONFIG_SMP
[d7c9fcb]340 exc_register(VECTOR_DEBUG_IPI, "debugger_smp", debug_ipi);
[6c6a19e6]341#endif
[4e49572]342}
[b45c443]343
[76fca31]344#ifdef CONFIG_KCONSOLE
345/** Print table of active breakpoints */
346int cmd_print_breakpoints(cmd_arg_t *argv __attribute__((unused)))
347{
348 unsigned int i;
349 char *symbol;
350
351#ifdef __32_BITS__
352 printf("# Count Address In symbol\n");
353 printf("-- ----- ---------- ---------\n");
354#endif
355
356#ifdef __64_BITS__
357 printf("# Count Address In symbol\n");
358 printf("-- ----- ------------------ ---------\n");
359#endif
360
361 for (i = 0; i < BKPOINTS_MAX; i++)
362 if (breakpoints[i].address) {
363 symbol = get_symtab_entry(breakpoints[i].address);
364
365#ifdef __32_BITS__
366 printf("%-2u %-5d %#10zx %s\n", i,
367 breakpoints[i].counter, breakpoints[i].address,
368 symbol);
369#endif
370
371#ifdef __64_BITS__
372 printf("%-2u %-5d %#18zx %s\n", i,
373 breakpoints[i].counter, breakpoints[i].address,
374 symbol);
375#endif
376
377 }
378 return 1;
379}
380
381#ifndef CONFIG_DEBUG_AS_WATCHPOINT
382
383/** Remove breakpoint from table */
384int cmd_del_breakpoint(cmd_arg_t *argv)
385{
386 unative_t bpno = argv->intval;
387 if (bpno > BKPOINTS_MAX) {
388 printf("Invalid breakpoint number.\n");
389 return 0;
390 }
391 breakpoint_del(argv->intval);
392 return 1;
393}
394
395/** Add new breakpoint to table */
396static int cmd_add_breakpoint(cmd_arg_t *argv)
397{
398 int flags;
399 int id;
400
401 if (argv == &add_argv) {
402 flags = BKPOINT_INSTR;
403 } else { /* addwatchp */
404 flags = BKPOINT_WRITE;
405 }
406 printf("Adding breakpoint on address: %p\n", argv->intval);
407 id = breakpoint_add((void *)argv->intval, flags, -1);
408 if (id < 0)
409 printf("Add breakpoint failed.\n");
410 else
411 printf("Added breakpoint %d.\n", id);
412
413 return 1;
414}
415#endif /* CONFIG_DEBUG_AS_WATCHPOINT */
416#endif /* CONFIG_KCONSOLE */
417
[06e1e95]418/** @}
[b45c443]419 */
Note: See TracBrowser for help on using the repository browser.