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