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