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