source: mainline/kernel/arch/amd64/src/debugger.c@ 4bb31f7

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

fix ICC compilation

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