Jason Wessel | 5d5314d | 2010-05-20 21:04:20 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Created by: Jason Wessel <jason.wessel@windriver.com> |
| 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> |
Paul Gortmaker | 6e5fdee | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 14 | #include <linux/export.h> |
Anton Vorontsov | b10d22d | 2012-07-30 04:58:10 -0700 | [diff] [blame] | 15 | #include <linux/hardirq.h> |
Jason Wessel | 5d5314d | 2010-05-20 21:04:20 -0500 | [diff] [blame] | 16 | #include "kdb_private.h" |
| 17 | #include "../debug_core.h" |
| 18 | |
| 19 | /* |
| 20 | * KDB interface to KGDB internals |
| 21 | */ |
| 22 | get_char_func kdb_poll_funcs[] = { |
| 23 | dbg_io_get_char, |
| 24 | NULL, |
Jason Wessel | f5316b4 | 2010-05-20 21:04:22 -0500 | [diff] [blame] | 25 | NULL, |
| 26 | NULL, |
| 27 | NULL, |
| 28 | NULL, |
Jason Wessel | 5d5314d | 2010-05-20 21:04:20 -0500 | [diff] [blame] | 29 | }; |
Jason Wessel | f5316b4 | 2010-05-20 21:04:22 -0500 | [diff] [blame] | 30 | EXPORT_SYMBOL_GPL(kdb_poll_funcs); |
| 31 | |
| 32 | int kdb_poll_idx = 1; |
| 33 | EXPORT_SYMBOL_GPL(kdb_poll_idx); |
Jason Wessel | 5d5314d | 2010-05-20 21:04:20 -0500 | [diff] [blame] | 34 | |
Jason Wessel | f679c49 | 2011-05-23 13:17:41 -0500 | [diff] [blame] | 35 | static struct kgdb_state *kdb_ks; |
| 36 | |
Matt Klein | 00370b8 | 2013-01-02 13:20:49 -0800 | [diff] [blame^] | 37 | int kdb_common_init_state(struct kgdb_state *ks) |
| 38 | { |
| 39 | kdb_initial_cpu = atomic_read(&kgdb_active); |
| 40 | kdb_current_task = kgdb_info[ks->cpu].task; |
| 41 | kdb_current_regs = kgdb_info[ks->cpu].debuggerinfo; |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | int kdb_common_deinit_state(void) |
| 46 | { |
| 47 | kdb_initial_cpu = -1; |
| 48 | kdb_current_task = NULL; |
| 49 | kdb_current_regs = NULL; |
| 50 | return 0; |
| 51 | } |
| 52 | |
Jason Wessel | 5d5314d | 2010-05-20 21:04:20 -0500 | [diff] [blame] | 53 | int kdb_stub(struct kgdb_state *ks) |
| 54 | { |
| 55 | int error = 0; |
| 56 | kdb_bp_t *bp; |
| 57 | unsigned long addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs); |
| 58 | kdb_reason_t reason = KDB_REASON_OOPS; |
| 59 | kdb_dbtrap_t db_result = KDB_DB_NOBPT; |
| 60 | int i; |
| 61 | |
Jason Wessel | f679c49 | 2011-05-23 13:17:41 -0500 | [diff] [blame] | 62 | kdb_ks = ks; |
Jason Wessel | 5d5314d | 2010-05-20 21:04:20 -0500 | [diff] [blame] | 63 | if (KDB_STATE(REENTRY)) { |
| 64 | reason = KDB_REASON_SWITCH; |
| 65 | KDB_STATE_CLEAR(REENTRY); |
| 66 | addr = instruction_pointer(ks->linux_regs); |
| 67 | } |
| 68 | ks->pass_exception = 0; |
| 69 | if (atomic_read(&kgdb_setting_breakpoint)) |
| 70 | reason = KDB_REASON_KEYBOARD; |
| 71 | |
Anton Vorontsov | b10d22d | 2012-07-30 04:58:10 -0700 | [diff] [blame] | 72 | if (in_nmi()) |
| 73 | reason = KDB_REASON_NMI; |
| 74 | |
Jason Wessel | 5d5314d | 2010-05-20 21:04:20 -0500 | [diff] [blame] | 75 | for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) { |
| 76 | if ((bp->bp_enabled) && (bp->bp_addr == addr)) { |
| 77 | reason = KDB_REASON_BREAK; |
| 78 | db_result = KDB_DB_BPT; |
| 79 | if (addr != instruction_pointer(ks->linux_regs)) |
| 80 | kgdb_arch_set_pc(ks->linux_regs, addr); |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | if (reason == KDB_REASON_BREAK || reason == KDB_REASON_SWITCH) { |
| 85 | for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) { |
| 86 | if (bp->bp_free) |
| 87 | continue; |
| 88 | if (bp->bp_addr == addr) { |
| 89 | bp->bp_delay = 1; |
| 90 | bp->bp_delayed = 1; |
| 91 | /* |
| 92 | * SSBPT is set when the kernel debugger must single step a |
| 93 | * task in order to re-establish an instruction breakpoint |
| 94 | * which uses the instruction replacement mechanism. It is |
| 95 | * cleared by any action that removes the need to single-step |
| 96 | * the breakpoint. |
| 97 | */ |
| 98 | reason = KDB_REASON_BREAK; |
| 99 | db_result = KDB_DB_BPT; |
| 100 | KDB_STATE_SET(SSBPT); |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (reason != KDB_REASON_BREAK && ks->ex_vector == 0 && |
| 107 | ks->signo == SIGTRAP) { |
| 108 | reason = KDB_REASON_SSTEP; |
| 109 | db_result = KDB_DB_BPT; |
| 110 | } |
| 111 | /* Set initial kdb state variables */ |
| 112 | KDB_STATE_CLEAR(KGDB_TRANS); |
Matt Klein | 00370b8 | 2013-01-02 13:20:49 -0800 | [diff] [blame^] | 113 | kdb_common_init_state(ks); |
Jason Wessel | 5d5314d | 2010-05-20 21:04:20 -0500 | [diff] [blame] | 114 | /* Remove any breakpoints as needed by kdb and clear single step */ |
| 115 | kdb_bp_remove(); |
| 116 | KDB_STATE_CLEAR(DOING_SS); |
| 117 | KDB_STATE_CLEAR(DOING_SSB); |
Jason Wessel | f5316b4 | 2010-05-20 21:04:22 -0500 | [diff] [blame] | 118 | KDB_STATE_SET(PAGER); |
Jason Wessel | 5d5314d | 2010-05-20 21:04:20 -0500 | [diff] [blame] | 119 | /* zero out any offline cpu data */ |
| 120 | for_each_present_cpu(i) { |
| 121 | if (!cpu_online(i)) { |
| 122 | kgdb_info[i].debuggerinfo = NULL; |
| 123 | kgdb_info[i].task = NULL; |
| 124 | } |
| 125 | } |
| 126 | if (ks->err_code == DIE_OOPS || reason == KDB_REASON_OOPS) { |
| 127 | ks->pass_exception = 1; |
| 128 | KDB_FLAG_SET(CATASTROPHIC); |
| 129 | } |
Jason Wessel | 5d5314d | 2010-05-20 21:04:20 -0500 | [diff] [blame] | 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 | */ |
Matt Klein | 00370b8 | 2013-01-02 13:20:49 -0800 | [diff] [blame^] | 142 | kdb_common_deinit_state(); |
Jason Wessel | f5316b4 | 2010-05-20 21:04:22 -0500 | [diff] [blame] | 143 | KDB_STATE_CLEAR(PAGER); |
Jason Wessel | 5d5314d | 2010-05-20 21:04:20 -0500 | [diff] [blame] | 144 | kdbnearsym_cleanup(); |
| 145 | if (error == KDB_CMD_KGDB) { |
Jason Wessel | d613d82 | 2011-05-23 13:22:54 -0500 | [diff] [blame] | 146 | if (KDB_STATE(DOING_KGDB)) |
Jason Wessel | 5d5314d | 2010-05-20 21:04:20 -0500 | [diff] [blame] | 147 | KDB_STATE_CLEAR(DOING_KGDB); |
Jason Wessel | 5d5314d | 2010-05-20 21:04:20 -0500 | [diff] [blame] | 148 | return DBG_PASS_EVENT; |
| 149 | } |
| 150 | kdb_bp_install(ks->linux_regs); |
| 151 | dbg_activate_sw_breakpoints(); |
| 152 | /* Set the exit state to a single step or a continue */ |
| 153 | if (KDB_STATE(DOING_SS)) |
| 154 | gdbstub_state(ks, "s"); |
| 155 | else |
| 156 | gdbstub_state(ks, "c"); |
| 157 | |
| 158 | KDB_FLAG_CLEAR(CATASTROPHIC); |
| 159 | |
| 160 | /* Invoke arch specific exception handling prior to system resume */ |
| 161 | kgdb_info[ks->cpu].ret_state = gdbstub_state(ks, "e"); |
| 162 | if (ks->pass_exception) |
| 163 | kgdb_info[ks->cpu].ret_state = 1; |
| 164 | if (error == KDB_CMD_CPU) { |
| 165 | KDB_STATE_SET(REENTRY); |
| 166 | /* |
| 167 | * Force clear the single step bit because kdb emulates this |
| 168 | * differently vs the gdbstub |
| 169 | */ |
| 170 | kgdb_single_step = 0; |
| 171 | dbg_deactivate_sw_breakpoints(); |
| 172 | return DBG_SWITCH_CPU_EVENT; |
| 173 | } |
| 174 | return kgdb_info[ks->cpu].ret_state; |
| 175 | } |
| 176 | |
Jason Wessel | f679c49 | 2011-05-23 13:17:41 -0500 | [diff] [blame] | 177 | void kdb_gdb_state_pass(char *buf) |
| 178 | { |
| 179 | gdbstub_state(kdb_ks, buf); |
| 180 | } |