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