2 * Created by: Jason Wessel <jason.wessel@windriver.com>
4 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
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.
11 #include <linux/kgdb.h>
12 #include <linux/kdb.h>
13 #include <linux/kdebug.h>
14 #include <linux/export.h>
15 #include <linux/hardirq.h>
16 #include "kdb_private.h"
17 #include "../debug_core.h"
20 * KDB interface to KGDB internals
22 get_char_func kdb_poll_funcs
[] = {
30 EXPORT_SYMBOL_GPL(kdb_poll_funcs
);
33 EXPORT_SYMBOL_GPL(kdb_poll_idx
);
35 static struct kgdb_state
*kdb_ks
;
37 int kdb_common_init_state(struct kgdb_state
*ks
)
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
;
45 int kdb_common_deinit_state(void)
48 kdb_current_task
= NULL
;
49 kdb_current_regs
= NULL
;
53 int kdb_stub(struct kgdb_state
*ks
)
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
;
63 if (KDB_STATE(REENTRY
)) {
64 reason
= KDB_REASON_SWITCH
;
65 KDB_STATE_CLEAR(REENTRY
);
66 addr
= instruction_pointer(ks
->linux_regs
);
68 ks
->pass_exception
= 0;
69 if (atomic_read(&kgdb_setting_breakpoint
))
70 reason
= KDB_REASON_KEYBOARD
;
72 if (ks
->err_code
== KDB_REASON_SYSTEM_NMI
&& ks
->signo
== SIGTRAP
)
73 reason
= KDB_REASON_SYSTEM_NMI
;
76 reason
= KDB_REASON_NMI
;
78 for (i
= 0, bp
= kdb_breakpoints
; i
< KDB_MAXBPT
; i
++, bp
++) {
79 if ((bp
->bp_enabled
) && (bp
->bp_addr
== addr
)) {
80 reason
= KDB_REASON_BREAK
;
81 db_result
= KDB_DB_BPT
;
82 if (addr
!= instruction_pointer(ks
->linux_regs
))
83 kgdb_arch_set_pc(ks
->linux_regs
, addr
);
87 if (reason
== KDB_REASON_BREAK
|| reason
== KDB_REASON_SWITCH
) {
88 for (i
= 0, bp
= kdb_breakpoints
; i
< KDB_MAXBPT
; i
++, bp
++) {
91 if (bp
->bp_addr
== addr
) {
95 * SSBPT is set when the kernel debugger must single step a
96 * task in order to re-establish an instruction breakpoint
97 * which uses the instruction replacement mechanism. It is
98 * cleared by any action that removes the need to single-step
101 reason
= KDB_REASON_BREAK
;
102 db_result
= KDB_DB_BPT
;
103 KDB_STATE_SET(SSBPT
);
109 if (reason
!= KDB_REASON_BREAK
&& ks
->ex_vector
== 0 &&
110 ks
->signo
== SIGTRAP
) {
111 reason
= KDB_REASON_SSTEP
;
112 db_result
= KDB_DB_BPT
;
114 /* Set initial kdb state variables */
115 KDB_STATE_CLEAR(KGDB_TRANS
);
116 kdb_common_init_state(ks
);
117 /* Remove any breakpoints as needed by kdb and clear single step */
119 KDB_STATE_CLEAR(DOING_SS
);
120 KDB_STATE_SET(PAGER
);
121 /* zero out any offline cpu data */
122 for_each_present_cpu(i
) {
123 if (!cpu_online(i
)) {
124 kgdb_info
[i
].debuggerinfo
= NULL
;
125 kgdb_info
[i
].task
= NULL
;
128 if (ks
->err_code
== DIE_OOPS
|| reason
== KDB_REASON_OOPS
) {
129 ks
->pass_exception
= 1;
130 KDB_FLAG_SET(CATASTROPHIC
);
132 /* set CATASTROPHIC if the system contains unresponsive processors */
133 for_each_online_cpu(i
)
134 if (!kgdb_info
[i
].enter_kgdb
)
135 KDB_FLAG_SET(CATASTROPHIC
);
136 if (KDB_STATE(SSBPT
) && reason
== KDB_REASON_SSTEP
) {
137 KDB_STATE_CLEAR(SSBPT
);
138 KDB_STATE_CLEAR(DOING_SS
);
140 /* Start kdb main loop */
141 error
= kdb_main_loop(KDB_REASON_ENTER
, reason
,
142 ks
->err_code
, db_result
, ks
->linux_regs
);
145 * Upon exit from the kdb main loop setup break points and restart
146 * the system based on the requested continue state
148 kdb_common_deinit_state();
149 KDB_STATE_CLEAR(PAGER
);
150 kdbnearsym_cleanup();
151 if (error
== KDB_CMD_KGDB
) {
152 if (KDB_STATE(DOING_KGDB
))
153 KDB_STATE_CLEAR(DOING_KGDB
);
154 return DBG_PASS_EVENT
;
156 kdb_bp_install(ks
->linux_regs
);
157 dbg_activate_sw_breakpoints();
158 /* Set the exit state to a single step or a continue */
159 if (KDB_STATE(DOING_SS
))
160 gdbstub_state(ks
, "s");
162 gdbstub_state(ks
, "c");
164 KDB_FLAG_CLEAR(CATASTROPHIC
);
166 /* Invoke arch specific exception handling prior to system resume */
167 kgdb_info
[ks
->cpu
].ret_state
= gdbstub_state(ks
, "e");
168 if (ks
->pass_exception
)
169 kgdb_info
[ks
->cpu
].ret_state
= 1;
170 if (error
== KDB_CMD_CPU
) {
171 KDB_STATE_SET(REENTRY
);
173 * Force clear the single step bit because kdb emulates this
174 * differently vs the gdbstub
176 kgdb_single_step
= 0;
177 dbg_deactivate_sw_breakpoints();
178 return DBG_SWITCH_CPU_EVENT
;
180 return kgdb_info
[ks
->cpu
].ret_state
;
183 void kdb_gdb_state_pass(char *buf
)
185 gdbstub_state(kdb_ks
, buf
);