1 /* 2 * Created by: Jason Wessel <[email protected]> 3 * 4 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved. 5 * 6 * This file is licensed under the terms of the GNU General Public 7 * License version 2. This program is licensed "as is" without any 8 * warranty of any kind, whether express or implied. 9 */ 10 11 #include <linux/kgdb.h> 12 #include <linux/kdb.h> 13 #include <linux/kdebug.h> 14 #include "kdb_private.h" 15 #include "../debug_core.h" 16 17 /* 18 * KDB interface to KGDB internals 19 */ 20 get_char_func kdb_poll_funcs[] = { 21 dbg_io_get_char, 22 NULL, 23 }; 24 25 int kdb_stub(struct kgdb_state *ks) 26 { 27 int error = 0; 28 kdb_bp_t *bp; 29 unsigned long addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs); 30 kdb_reason_t reason = KDB_REASON_OOPS; 31 kdb_dbtrap_t db_result = KDB_DB_NOBPT; 32 int i; 33 34 if (KDB_STATE(REENTRY)) { 35 reason = KDB_REASON_SWITCH; 36 KDB_STATE_CLEAR(REENTRY); 37 addr = instruction_pointer(ks->linux_regs); 38 } 39 ks->pass_exception = 0; 40 if (atomic_read(&kgdb_setting_breakpoint)) 41 reason = KDB_REASON_KEYBOARD; 42 43 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) { 44 if ((bp->bp_enabled) && (bp->bp_addr == addr)) { 45 reason = KDB_REASON_BREAK; 46 db_result = KDB_DB_BPT; 47 if (addr != instruction_pointer(ks->linux_regs)) 48 kgdb_arch_set_pc(ks->linux_regs, addr); 49 break; 50 } 51 } 52 if (reason == KDB_REASON_BREAK || reason == KDB_REASON_SWITCH) { 53 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) { 54 if (bp->bp_free) 55 continue; 56 if (bp->bp_addr == addr) { 57 bp->bp_delay = 1; 58 bp->bp_delayed = 1; 59 /* 60 * SSBPT is set when the kernel debugger must single step a 61 * task in order to re-establish an instruction breakpoint 62 * which uses the instruction replacement mechanism. It is 63 * cleared by any action that removes the need to single-step 64 * the breakpoint. 65 */ 66 reason = KDB_REASON_BREAK; 67 db_result = KDB_DB_BPT; 68 KDB_STATE_SET(SSBPT); 69 break; 70 } 71 } 72 } 73 74 if (reason != KDB_REASON_BREAK && ks->ex_vector == 0 && 75 ks->signo == SIGTRAP) { 76 reason = KDB_REASON_SSTEP; 77 db_result = KDB_DB_BPT; 78 } 79 /* Set initial kdb state variables */ 80 KDB_STATE_CLEAR(KGDB_TRANS); 81 kdb_initial_cpu = ks->cpu; 82 kdb_current_task = kgdb_info[ks->cpu].task; 83 kdb_current_regs = kgdb_info[ks->cpu].debuggerinfo; 84 /* Remove any breakpoints as needed by kdb and clear single step */ 85 kdb_bp_remove(); 86 KDB_STATE_CLEAR(DOING_SS); 87 KDB_STATE_CLEAR(DOING_SSB); 88 /* zero out any offline cpu data */ 89 for_each_present_cpu(i) { 90 if (!cpu_online(i)) { 91 kgdb_info[i].debuggerinfo = NULL; 92 kgdb_info[i].task = NULL; 93 } 94 } 95 if (ks->err_code == DIE_OOPS || reason == KDB_REASON_OOPS) { 96 ks->pass_exception = 1; 97 KDB_FLAG_SET(CATASTROPHIC); 98 } 99 kdb_initial_cpu = ks->cpu; 100 if (KDB_STATE(SSBPT) && reason == KDB_REASON_SSTEP) { 101 KDB_STATE_CLEAR(SSBPT); 102 KDB_STATE_CLEAR(DOING_SS); 103 } else { 104 /* Start kdb main loop */ 105 error = kdb_main_loop(KDB_REASON_ENTER, reason, 106 ks->err_code, db_result, ks->linux_regs); 107 } 108 /* 109 * Upon exit from the kdb main loop setup break points and restart 110 * the system based on the requested continue state 111 */ 112 kdb_initial_cpu = -1; 113 kdb_current_task = NULL; 114 kdb_current_regs = NULL; 115 kdbnearsym_cleanup(); 116 if (error == KDB_CMD_KGDB) { 117 if (KDB_STATE(DOING_KGDB) || KDB_STATE(DOING_KGDB2)) { 118 /* 119 * This inteface glue which allows kdb to transition in into 120 * the gdb stub. In order to do this the '?' or '' gdb serial 121 * packet response is processed here. And then control is 122 * passed to the gdbstub. 123 */ 124 if (KDB_STATE(DOING_KGDB)) 125 gdbstub_state(ks, "?"); 126 else 127 gdbstub_state(ks, ""); 128 KDB_STATE_CLEAR(DOING_KGDB); 129 KDB_STATE_CLEAR(DOING_KGDB2); 130 } 131 return DBG_PASS_EVENT; 132 } 133 kdb_bp_install(ks->linux_regs); 134 dbg_activate_sw_breakpoints(); 135 /* Set the exit state to a single step or a continue */ 136 if (KDB_STATE(DOING_SS)) 137 gdbstub_state(ks, "s"); 138 else 139 gdbstub_state(ks, "c"); 140 141 KDB_FLAG_CLEAR(CATASTROPHIC); 142 143 /* Invoke arch specific exception handling prior to system resume */ 144 kgdb_info[ks->cpu].ret_state = gdbstub_state(ks, "e"); 145 if (ks->pass_exception) 146 kgdb_info[ks->cpu].ret_state = 1; 147 if (error == KDB_CMD_CPU) { 148 KDB_STATE_SET(REENTRY); 149 /* 150 * Force clear the single step bit because kdb emulates this 151 * differently vs the gdbstub 152 */ 153 kgdb_single_step = 0; 154 dbg_deactivate_sw_breakpoints(); 155 return DBG_SWITCH_CPU_EVENT; 156 } 157 return kgdb_info[ks->cpu].ret_state; 158 } 159 160