xref: /openbmc/linux/kernel/debug/kdb/kdb_debugger.c (revision 0d456bad)
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>
14 #include <linux/export.h>
15 #include <linux/hardirq.h>
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,
25 	NULL,
26 	NULL,
27 	NULL,
28 	NULL,
29 };
30 EXPORT_SYMBOL_GPL(kdb_poll_funcs);
31 
32 int kdb_poll_idx = 1;
33 EXPORT_SYMBOL_GPL(kdb_poll_idx);
34 
35 static struct kgdb_state *kdb_ks;
36 
37 int kdb_stub(struct kgdb_state *ks)
38 {
39 	int error = 0;
40 	kdb_bp_t *bp;
41 	unsigned long addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
42 	kdb_reason_t reason = KDB_REASON_OOPS;
43 	kdb_dbtrap_t db_result = KDB_DB_NOBPT;
44 	int i;
45 
46 	kdb_ks = ks;
47 	if (KDB_STATE(REENTRY)) {
48 		reason = KDB_REASON_SWITCH;
49 		KDB_STATE_CLEAR(REENTRY);
50 		addr = instruction_pointer(ks->linux_regs);
51 	}
52 	ks->pass_exception = 0;
53 	if (atomic_read(&kgdb_setting_breakpoint))
54 		reason = KDB_REASON_KEYBOARD;
55 
56 	if (in_nmi())
57 		reason = KDB_REASON_NMI;
58 
59 	for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) {
60 		if ((bp->bp_enabled) && (bp->bp_addr == addr)) {
61 			reason = KDB_REASON_BREAK;
62 			db_result = KDB_DB_BPT;
63 			if (addr != instruction_pointer(ks->linux_regs))
64 				kgdb_arch_set_pc(ks->linux_regs, addr);
65 			break;
66 		}
67 	}
68 	if (reason == KDB_REASON_BREAK || reason == KDB_REASON_SWITCH) {
69 		for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) {
70 			if (bp->bp_free)
71 				continue;
72 			if (bp->bp_addr == addr) {
73 				bp->bp_delay = 1;
74 				bp->bp_delayed = 1;
75 	/*
76 	 * SSBPT is set when the kernel debugger must single step a
77 	 * task in order to re-establish an instruction breakpoint
78 	 * which uses the instruction replacement mechanism.  It is
79 	 * cleared by any action that removes the need to single-step
80 	 * the breakpoint.
81 	 */
82 				reason = KDB_REASON_BREAK;
83 				db_result = KDB_DB_BPT;
84 				KDB_STATE_SET(SSBPT);
85 				break;
86 			}
87 		}
88 	}
89 
90 	if (reason != KDB_REASON_BREAK && ks->ex_vector == 0 &&
91 		ks->signo == SIGTRAP) {
92 		reason = KDB_REASON_SSTEP;
93 		db_result = KDB_DB_BPT;
94 	}
95 	/* Set initial kdb state variables */
96 	KDB_STATE_CLEAR(KGDB_TRANS);
97 	kdb_initial_cpu = atomic_read(&kgdb_active);
98 	kdb_current_task = kgdb_info[ks->cpu].task;
99 	kdb_current_regs = kgdb_info[ks->cpu].debuggerinfo;
100 	/* Remove any breakpoints as needed by kdb and clear single step */
101 	kdb_bp_remove();
102 	KDB_STATE_CLEAR(DOING_SS);
103 	KDB_STATE_CLEAR(DOING_SSB);
104 	KDB_STATE_SET(PAGER);
105 	/* zero out any offline cpu data */
106 	for_each_present_cpu(i) {
107 		if (!cpu_online(i)) {
108 			kgdb_info[i].debuggerinfo = NULL;
109 			kgdb_info[i].task = NULL;
110 		}
111 	}
112 	if (ks->err_code == DIE_OOPS || reason == KDB_REASON_OOPS) {
113 		ks->pass_exception = 1;
114 		KDB_FLAG_SET(CATASTROPHIC);
115 	}
116 	if (KDB_STATE(SSBPT) && reason == KDB_REASON_SSTEP) {
117 		KDB_STATE_CLEAR(SSBPT);
118 		KDB_STATE_CLEAR(DOING_SS);
119 	} else {
120 		/* Start kdb main loop */
121 		error = kdb_main_loop(KDB_REASON_ENTER, reason,
122 				      ks->err_code, db_result, ks->linux_regs);
123 	}
124 	/*
125 	 * Upon exit from the kdb main loop setup break points and restart
126 	 * the system based on the requested continue state
127 	 */
128 	kdb_initial_cpu = -1;
129 	kdb_current_task = NULL;
130 	kdb_current_regs = NULL;
131 	KDB_STATE_CLEAR(PAGER);
132 	kdbnearsym_cleanup();
133 	if (error == KDB_CMD_KGDB) {
134 		if (KDB_STATE(DOING_KGDB))
135 			KDB_STATE_CLEAR(DOING_KGDB);
136 		return DBG_PASS_EVENT;
137 	}
138 	kdb_bp_install(ks->linux_regs);
139 	dbg_activate_sw_breakpoints();
140 	/* Set the exit state to a single step or a continue */
141 	if (KDB_STATE(DOING_SS))
142 		gdbstub_state(ks, "s");
143 	else
144 		gdbstub_state(ks, "c");
145 
146 	KDB_FLAG_CLEAR(CATASTROPHIC);
147 
148 	/* Invoke arch specific exception handling prior to system resume */
149 	kgdb_info[ks->cpu].ret_state = gdbstub_state(ks, "e");
150 	if (ks->pass_exception)
151 		kgdb_info[ks->cpu].ret_state = 1;
152 	if (error == KDB_CMD_CPU) {
153 		KDB_STATE_SET(REENTRY);
154 		/*
155 		 * Force clear the single step bit because kdb emulates this
156 		 * differently vs the gdbstub
157 		 */
158 		kgdb_single_step = 0;
159 		dbg_deactivate_sw_breakpoints();
160 		return DBG_SWITCH_CPU_EVENT;
161 	}
162 	return kgdb_info[ks->cpu].ret_state;
163 }
164 
165 void kdb_gdb_state_pass(char *buf)
166 {
167 	gdbstub_state(kdb_ks, buf);
168 }
169