Commit dc7d552705215ac50a0617fcf51bb9c736255b8e
Committed by
Ingo Molnar
1 parent
c33fa9f560
Exists in
master
and in
4 other branches
kgdb: core
kgdb core code. Handles the protocol and the arch details. [ mingo@elte.hu: heavily modified, simplified and cleaned up. ] [ xemul@openvz.org: use find_task_by_pid_ns ] Signed-off-by: Jason Wessel <jason.wessel@windriver.com> Signed-off-by: Ingo Molnar <mingo@elte.hu> Signed-off-by: Jan Kiszka <jan.kiszka@web.de> Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Showing 5 changed files with 1994 additions and 0 deletions Side-by-side Diff
include/linux/kgdb.h
1 | +/* | |
2 | + * This provides the callbacks and functions that KGDB needs to share between | |
3 | + * the core, I/O and arch-specific portions. | |
4 | + * | |
5 | + * Author: Amit Kale <amitkale@linsyssoft.com> and | |
6 | + * Tom Rini <trini@kernel.crashing.org> | |
7 | + * | |
8 | + * 2001-2004 (c) Amit S. Kale and 2003-2005 (c) MontaVista Software, Inc. | |
9 | + * This file is licensed under the terms of the GNU General Public License | |
10 | + * version 2. This program is licensed "as is" without any warranty of any | |
11 | + * kind, whether express or implied. | |
12 | + */ | |
13 | +#ifndef _KGDB_H_ | |
14 | +#define _KGDB_H_ | |
15 | + | |
16 | +#include <linux/serial_8250.h> | |
17 | +#include <linux/linkage.h> | |
18 | +#include <linux/init.h> | |
19 | + | |
20 | +#include <asm/atomic.h> | |
21 | +#include <asm/kgdb.h> | |
22 | + | |
23 | +struct pt_regs; | |
24 | + | |
25 | +/* | |
26 | + * kgdb_skipexception - Bail out of KGDB when we've been triggered. | |
27 | + * @exception: Exception vector number | |
28 | + * @regs: Current &struct pt_regs. | |
29 | + * | |
30 | + * On some architectures we need to skip a breakpoint exception when | |
31 | + * it occurs after a breakpoint has been removed. | |
32 | + */ | |
33 | +extern int kgdb_skipexception(int exception, struct pt_regs *regs); | |
34 | + | |
35 | +/* | |
36 | + * kgdb_post_primary_code - Save error vector/code numbers. | |
37 | + * @regs: Original pt_regs. | |
38 | + * @e_vector: Original error vector. | |
39 | + * @err_code: Original error code. | |
40 | + * | |
41 | + * This is needed on architectures which support SMP and KGDB. | |
42 | + * This function is called after all the secondary cpus have been put | |
43 | + * to a know spin state and the primary CPU has control over KGDB. | |
44 | + */ | |
45 | +extern void kgdb_post_primary_code(struct pt_regs *regs, int e_vector, | |
46 | + int err_code); | |
47 | + | |
48 | +/* | |
49 | + * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb. | |
50 | + * @regs: Current &struct pt_regs. | |
51 | + * | |
52 | + * This function will be called if the particular architecture must | |
53 | + * disable hardware debugging while it is processing gdb packets or | |
54 | + * handling exception. | |
55 | + */ | |
56 | +extern void kgdb_disable_hw_debug(struct pt_regs *regs); | |
57 | + | |
58 | +struct tasklet_struct; | |
59 | +struct task_struct; | |
60 | +struct uart_port; | |
61 | + | |
62 | +/* To enter the debugger explicitly. */ | |
63 | +void kgdb_breakpoint(void); | |
64 | + | |
65 | +extern int kgdb_connected; | |
66 | + | |
67 | +extern atomic_t kgdb_setting_breakpoint; | |
68 | +extern atomic_t kgdb_cpu_doing_single_step; | |
69 | + | |
70 | +extern struct task_struct *kgdb_usethread; | |
71 | +extern struct task_struct *kgdb_contthread; | |
72 | + | |
73 | +enum kgdb_bptype { | |
74 | + BP_BREAKPOINT = 0, | |
75 | + BP_HARDWARE_BREAKPOINT, | |
76 | + BP_WRITE_WATCHPOINT, | |
77 | + BP_READ_WATCHPOINT, | |
78 | + BP_ACCESS_WATCHPOINT | |
79 | +}; | |
80 | + | |
81 | +enum kgdb_bpstate { | |
82 | + BP_UNDEFINED = 0, | |
83 | + BP_REMOVED, | |
84 | + BP_SET, | |
85 | + BP_ACTIVE | |
86 | +}; | |
87 | + | |
88 | +struct kgdb_bkpt { | |
89 | + unsigned long bpt_addr; | |
90 | + unsigned char saved_instr[BREAK_INSTR_SIZE]; | |
91 | + enum kgdb_bptype type; | |
92 | + enum kgdb_bpstate state; | |
93 | +}; | |
94 | + | |
95 | +#ifndef KGDB_MAX_BREAKPOINTS | |
96 | +# define KGDB_MAX_BREAKPOINTS 1000 | |
97 | +#endif | |
98 | + | |
99 | +#define KGDB_HW_BREAKPOINT 1 | |
100 | + | |
101 | +/* | |
102 | + * Functions each KGDB-supporting architecture must provide: | |
103 | + */ | |
104 | + | |
105 | +/* | |
106 | + * kgdb_arch_init - Perform any architecture specific initalization. | |
107 | + * | |
108 | + * This function will handle the initalization of any architecture | |
109 | + * specific callbacks. | |
110 | + */ | |
111 | +extern int kgdb_arch_init(void); | |
112 | + | |
113 | +/* | |
114 | + * kgdb_arch_exit - Perform any architecture specific uninitalization. | |
115 | + * | |
116 | + * This function will handle the uninitalization of any architecture | |
117 | + * specific callbacks, for dynamic registration and unregistration. | |
118 | + */ | |
119 | +extern void kgdb_arch_exit(void); | |
120 | + | |
121 | +/* | |
122 | + * pt_regs_to_gdb_regs - Convert ptrace regs to GDB regs | |
123 | + * @gdb_regs: A pointer to hold the registers in the order GDB wants. | |
124 | + * @regs: The &struct pt_regs of the current process. | |
125 | + * | |
126 | + * Convert the pt_regs in @regs into the format for registers that | |
127 | + * GDB expects, stored in @gdb_regs. | |
128 | + */ | |
129 | +extern void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs); | |
130 | + | |
131 | +/* | |
132 | + * sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs | |
133 | + * @gdb_regs: A pointer to hold the registers in the order GDB wants. | |
134 | + * @p: The &struct task_struct of the desired process. | |
135 | + * | |
136 | + * Convert the register values of the sleeping process in @p to | |
137 | + * the format that GDB expects. | |
138 | + * This function is called when kgdb does not have access to the | |
139 | + * &struct pt_regs and therefore it should fill the gdb registers | |
140 | + * @gdb_regs with what has been saved in &struct thread_struct | |
141 | + * thread field during switch_to. | |
142 | + */ | |
143 | +extern void | |
144 | +sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p); | |
145 | + | |
146 | +/* | |
147 | + * gdb_regs_to_pt_regs - Convert GDB regs to ptrace regs. | |
148 | + * @gdb_regs: A pointer to hold the registers we've received from GDB. | |
149 | + * @regs: A pointer to a &struct pt_regs to hold these values in. | |
150 | + * | |
151 | + * Convert the GDB regs in @gdb_regs into the pt_regs, and store them | |
152 | + * in @regs. | |
153 | + */ | |
154 | +extern void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs); | |
155 | + | |
156 | +/* | |
157 | + * kgdb_arch_handle_exception - Handle architecture specific GDB packets. | |
158 | + * @vector: The error vector of the exception that happened. | |
159 | + * @signo: The signal number of the exception that happened. | |
160 | + * @err_code: The error code of the exception that happened. | |
161 | + * @remcom_in_buffer: The buffer of the packet we have read. | |
162 | + * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into. | |
163 | + * @regs: The &struct pt_regs of the current process. | |
164 | + * | |
165 | + * This function MUST handle the 'c' and 's' command packets, | |
166 | + * as well packets to set / remove a hardware breakpoint, if used. | |
167 | + * If there are additional packets which the hardware needs to handle, | |
168 | + * they are handled here. The code should return -1 if it wants to | |
169 | + * process more packets, and a %0 or %1 if it wants to exit from the | |
170 | + * kgdb callback. | |
171 | + */ | |
172 | +extern int | |
173 | +kgdb_arch_handle_exception(int vector, int signo, int err_code, | |
174 | + char *remcom_in_buffer, | |
175 | + char *remcom_out_buffer, | |
176 | + struct pt_regs *regs); | |
177 | + | |
178 | +/* | |
179 | + * kgdb_roundup_cpus - Get other CPUs into a holding pattern | |
180 | + * @flags: Current IRQ state | |
181 | + * | |
182 | + * On SMP systems, we need to get the attention of the other CPUs | |
183 | + * and get them be in a known state. This should do what is needed | |
184 | + * to get the other CPUs to call kgdb_wait(). Note that on some arches, | |
185 | + * the NMI approach is not used for rounding up all the CPUs. For example, | |
186 | + * in case of MIPS, smp_call_function() is used to roundup CPUs. In | |
187 | + * this case, we have to make sure that interrupts are enabled before | |
188 | + * calling smp_call_function(). The argument to this function is | |
189 | + * the flags that will be used when restoring the interrupts. There is | |
190 | + * local_irq_save() call before kgdb_roundup_cpus(). | |
191 | + * | |
192 | + * On non-SMP systems, this is not called. | |
193 | + */ | |
194 | +extern void kgdb_roundup_cpus(unsigned long flags); | |
195 | + | |
196 | +/* Optional functions. */ | |
197 | +extern int kgdb_validate_break_address(unsigned long addr); | |
198 | +extern int kgdb_arch_set_breakpoint(unsigned long addr, char *saved_instr); | |
199 | +extern int kgdb_arch_remove_breakpoint(unsigned long addr, char *bundle); | |
200 | + | |
201 | +/* | |
202 | + * struct kgdb_arch - Describe architecture specific values. | |
203 | + * @gdb_bpt_instr: The instruction to trigger a breakpoint. | |
204 | + * @flags: Flags for the breakpoint, currently just %KGDB_HW_BREAKPOINT. | |
205 | + * @set_breakpoint: Allow an architecture to specify how to set a software | |
206 | + * breakpoint. | |
207 | + * @remove_breakpoint: Allow an architecture to specify how to remove a | |
208 | + * software breakpoint. | |
209 | + * @set_hw_breakpoint: Allow an architecture to specify how to set a hardware | |
210 | + * breakpoint. | |
211 | + * @remove_hw_breakpoint: Allow an architecture to specify how to remove a | |
212 | + * hardware breakpoint. | |
213 | + * @remove_all_hw_break: Allow an architecture to specify how to remove all | |
214 | + * hardware breakpoints. | |
215 | + * @correct_hw_break: Allow an architecture to specify how to correct the | |
216 | + * hardware debug registers. | |
217 | + */ | |
218 | +struct kgdb_arch { | |
219 | + unsigned char gdb_bpt_instr[BREAK_INSTR_SIZE]; | |
220 | + unsigned long flags; | |
221 | + | |
222 | + int (*set_breakpoint)(unsigned long, char *); | |
223 | + int (*remove_breakpoint)(unsigned long, char *); | |
224 | + int (*set_hw_breakpoint)(unsigned long, int, enum kgdb_bptype); | |
225 | + int (*remove_hw_breakpoint)(unsigned long, int, enum kgdb_bptype); | |
226 | + void (*remove_all_hw_break)(void); | |
227 | + void (*correct_hw_break)(void); | |
228 | +}; | |
229 | + | |
230 | +/* | |
231 | + * struct kgdb_io - Describe the interface for an I/O driver to talk with KGDB. | |
232 | + * @name: Name of the I/O driver. | |
233 | + * @read_char: Pointer to a function that will return one char. | |
234 | + * @write_char: Pointer to a function that will write one char. | |
235 | + * @flush: Pointer to a function that will flush any pending writes. | |
236 | + * @init: Pointer to a function that will initialize the device. | |
237 | + * @pre_exception: Pointer to a function that will do any prep work for | |
238 | + * the I/O driver. | |
239 | + * @post_exception: Pointer to a function that will do any cleanup work | |
240 | + * for the I/O driver. | |
241 | + */ | |
242 | +struct kgdb_io { | |
243 | + const char *name; | |
244 | + int (*read_char) (void); | |
245 | + void (*write_char) (u8); | |
246 | + void (*flush) (void); | |
247 | + int (*init) (void); | |
248 | + void (*pre_exception) (void); | |
249 | + void (*post_exception) (void); | |
250 | +}; | |
251 | + | |
252 | +extern struct kgdb_arch arch_kgdb_ops; | |
253 | + | |
254 | +extern int kgdb_register_io_module(struct kgdb_io *local_kgdb_io_ops); | |
255 | +extern void kgdb_unregister_io_module(struct kgdb_io *local_kgdb_io_ops); | |
256 | + | |
257 | +extern int kgdb_hex2long(char **ptr, long *long_val); | |
258 | +extern int kgdb_mem2hex(char *mem, char *buf, int count); | |
259 | +extern int kgdb_hex2mem(char *buf, char *mem, int count); | |
260 | + | |
261 | +extern int kgdb_isremovedbreak(unsigned long addr); | |
262 | + | |
263 | +extern int | |
264 | +kgdb_handle_exception(int ex_vector, int signo, int err_code, | |
265 | + struct pt_regs *regs); | |
266 | +extern int kgdb_nmicallback(int cpu, void *regs); | |
267 | + | |
268 | +extern int kgdb_single_step; | |
269 | +extern atomic_t kgdb_active; | |
270 | + | |
271 | +#endif /* _KGDB_H_ */ |
kernel/Makefile
... | ... | @@ -53,6 +53,7 @@ |
53 | 53 | obj-$(CONFIG_AUDITSYSCALL) += auditsc.o |
54 | 54 | obj-$(CONFIG_AUDIT_TREE) += audit_tree.o |
55 | 55 | obj-$(CONFIG_KPROBES) += kprobes.o |
56 | +obj-$(CONFIG_KGDB) += kgdb.o | |
56 | 57 | obj-$(CONFIG_DETECT_SOFTLOCKUP) += softlockup.o |
57 | 58 | obj-$(CONFIG_GENERIC_HARDIRQS) += irq/ |
58 | 59 | obj-$(CONFIG_SECCOMP) += seccomp.o |
kernel/kgdb.c
Changes suppressed. Click to show
1 | +/* | |
2 | + * KGDB stub. | |
3 | + * | |
4 | + * Maintainer: Jason Wessel <jason.wessel@windriver.com> | |
5 | + * | |
6 | + * Copyright (C) 2000-2001 VERITAS Software Corporation. | |
7 | + * Copyright (C) 2002-2004 Timesys Corporation | |
8 | + * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com> | |
9 | + * Copyright (C) 2004 Pavel Machek <pavel@suse.cz> | |
10 | + * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org> | |
11 | + * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd. | |
12 | + * Copyright (C) 2005-2008 Wind River Systems, Inc. | |
13 | + * Copyright (C) 2007 MontaVista Software, Inc. | |
14 | + * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> | |
15 | + * | |
16 | + * Contributors at various stages not listed above: | |
17 | + * Jason Wessel ( jason.wessel@windriver.com ) | |
18 | + * George Anzinger <george@mvista.com> | |
19 | + * Anurekh Saxena (anurekh.saxena@timesys.com) | |
20 | + * Lake Stevens Instrument Division (Glenn Engel) | |
21 | + * Jim Kingdon, Cygnus Support. | |
22 | + * | |
23 | + * Original KGDB stub: David Grothe <dave@gcom.com>, | |
24 | + * Tigran Aivazian <tigran@sco.com> | |
25 | + * | |
26 | + * This file is licensed under the terms of the GNU General Public License | |
27 | + * version 2. This program is licensed "as is" without any warranty of any | |
28 | + * kind, whether express or implied. | |
29 | + */ | |
30 | +#include <linux/pid_namespace.h> | |
31 | +#include <linux/interrupt.h> | |
32 | +#include <linux/spinlock.h> | |
33 | +#include <linux/console.h> | |
34 | +#include <linux/threads.h> | |
35 | +#include <linux/uaccess.h> | |
36 | +#include <linux/kernel.h> | |
37 | +#include <linux/module.h> | |
38 | +#include <linux/ptrace.h> | |
39 | +#include <linux/reboot.h> | |
40 | +#include <linux/string.h> | |
41 | +#include <linux/delay.h> | |
42 | +#include <linux/sched.h> | |
43 | +#include <linux/sysrq.h> | |
44 | +#include <linux/init.h> | |
45 | +#include <linux/kgdb.h> | |
46 | +#include <linux/pid.h> | |
47 | +#include <linux/smp.h> | |
48 | +#include <linux/mm.h> | |
49 | + | |
50 | +#include <asm/cacheflush.h> | |
51 | +#include <asm/byteorder.h> | |
52 | +#include <asm/atomic.h> | |
53 | +#include <asm/system.h> | |
54 | + | |
55 | +static int kgdb_break_asap; | |
56 | + | |
57 | +struct kgdb_state { | |
58 | + int ex_vector; | |
59 | + int signo; | |
60 | + int err_code; | |
61 | + int cpu; | |
62 | + int pass_exception; | |
63 | + long threadid; | |
64 | + long kgdb_usethreadid; | |
65 | + struct pt_regs *linux_regs; | |
66 | +}; | |
67 | + | |
68 | +static struct debuggerinfo_struct { | |
69 | + void *debuggerinfo; | |
70 | + struct task_struct *task; | |
71 | +} kgdb_info[NR_CPUS]; | |
72 | + | |
73 | +/** | |
74 | + * kgdb_connected - Is a host GDB connected to us? | |
75 | + */ | |
76 | +int kgdb_connected; | |
77 | +EXPORT_SYMBOL_GPL(kgdb_connected); | |
78 | + | |
79 | +/* All the KGDB handlers are installed */ | |
80 | +static int kgdb_io_module_registered; | |
81 | + | |
82 | +/* Guard for recursive entry */ | |
83 | +static int exception_level; | |
84 | + | |
85 | +static struct kgdb_io *kgdb_io_ops; | |
86 | +static DEFINE_SPINLOCK(kgdb_registration_lock); | |
87 | + | |
88 | +/* kgdb console driver is loaded */ | |
89 | +static int kgdb_con_registered; | |
90 | +/* determine if kgdb console output should be used */ | |
91 | +static int kgdb_use_con; | |
92 | + | |
93 | +static int __init opt_kgdb_con(char *str) | |
94 | +{ | |
95 | + kgdb_use_con = 1; | |
96 | + return 0; | |
97 | +} | |
98 | + | |
99 | +early_param("kgdbcon", opt_kgdb_con); | |
100 | + | |
101 | +module_param(kgdb_use_con, int, 0644); | |
102 | + | |
103 | +/* | |
104 | + * Holds information about breakpoints in a kernel. These breakpoints are | |
105 | + * added and removed by gdb. | |
106 | + */ | |
107 | +static struct kgdb_bkpt kgdb_break[KGDB_MAX_BREAKPOINTS] = { | |
108 | + [0 ... KGDB_MAX_BREAKPOINTS-1] = { .state = BP_UNDEFINED } | |
109 | +}; | |
110 | + | |
111 | +/* | |
112 | + * The CPU# of the active CPU, or -1 if none: | |
113 | + */ | |
114 | +atomic_t kgdb_active = ATOMIC_INIT(-1); | |
115 | + | |
116 | +/* | |
117 | + * We use NR_CPUs not PERCPU, in case kgdb is used to debug early | |
118 | + * bootup code (which might not have percpu set up yet): | |
119 | + */ | |
120 | +static atomic_t passive_cpu_wait[NR_CPUS]; | |
121 | +static atomic_t cpu_in_kgdb[NR_CPUS]; | |
122 | +atomic_t kgdb_setting_breakpoint; | |
123 | + | |
124 | +struct task_struct *kgdb_usethread; | |
125 | +struct task_struct *kgdb_contthread; | |
126 | + | |
127 | +int kgdb_single_step; | |
128 | + | |
129 | +/* Our I/O buffers. */ | |
130 | +static char remcom_in_buffer[BUFMAX]; | |
131 | +static char remcom_out_buffer[BUFMAX]; | |
132 | + | |
133 | +/* Storage for the registers, in GDB format. */ | |
134 | +static unsigned long gdb_regs[(NUMREGBYTES + | |
135 | + sizeof(unsigned long) - 1) / | |
136 | + sizeof(unsigned long)]; | |
137 | + | |
138 | +/* to keep track of the CPU which is doing the single stepping*/ | |
139 | +atomic_t kgdb_cpu_doing_single_step = ATOMIC_INIT(-1); | |
140 | + | |
141 | +/* | |
142 | + * If you are debugging a problem where roundup (the collection of | |
143 | + * all other CPUs) is a problem [this should be extremely rare], | |
144 | + * then use the nokgdbroundup option to avoid roundup. In that case | |
145 | + * the other CPUs might interfere with your debugging context, so | |
146 | + * use this with care: | |
147 | + */ | |
148 | +int kgdb_do_roundup = 1; | |
149 | + | |
150 | +static int __init opt_nokgdbroundup(char *str) | |
151 | +{ | |
152 | + kgdb_do_roundup = 0; | |
153 | + | |
154 | + return 0; | |
155 | +} | |
156 | + | |
157 | +early_param("nokgdbroundup", opt_nokgdbroundup); | |
158 | + | |
159 | +/* | |
160 | + * Finally, some KGDB code :-) | |
161 | + */ | |
162 | + | |
163 | +/* | |
164 | + * Weak aliases for breakpoint management, | |
165 | + * can be overriden by architectures when needed: | |
166 | + */ | |
167 | +int __weak kgdb_validate_break_address(unsigned long addr) | |
168 | +{ | |
169 | + char tmp_variable[BREAK_INSTR_SIZE]; | |
170 | + | |
171 | + return probe_kernel_read(tmp_variable, (char *)addr, BREAK_INSTR_SIZE); | |
172 | +} | |
173 | + | |
174 | +int __weak kgdb_arch_set_breakpoint(unsigned long addr, char *saved_instr) | |
175 | +{ | |
176 | + int err; | |
177 | + | |
178 | + err = probe_kernel_read(saved_instr, (char *)addr, BREAK_INSTR_SIZE); | |
179 | + if (err) | |
180 | + return err; | |
181 | + | |
182 | + return probe_kernel_write((char *)addr, arch_kgdb_ops.gdb_bpt_instr, | |
183 | + BREAK_INSTR_SIZE); | |
184 | +} | |
185 | + | |
186 | +int __weak kgdb_arch_remove_breakpoint(unsigned long addr, char *bundle) | |
187 | +{ | |
188 | + return probe_kernel_write((char *)addr, | |
189 | + (char *)bundle, BREAK_INSTR_SIZE); | |
190 | +} | |
191 | + | |
192 | +unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs) | |
193 | +{ | |
194 | + return instruction_pointer(regs); | |
195 | +} | |
196 | + | |
197 | +int __weak kgdb_arch_init(void) | |
198 | +{ | |
199 | + return 0; | |
200 | +} | |
201 | + | |
202 | +/** | |
203 | + * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb. | |
204 | + * @regs: Current &struct pt_regs. | |
205 | + * | |
206 | + * This function will be called if the particular architecture must | |
207 | + * disable hardware debugging while it is processing gdb packets or | |
208 | + * handling exception. | |
209 | + */ | |
210 | +void __weak kgdb_disable_hw_debug(struct pt_regs *regs) | |
211 | +{ | |
212 | +} | |
213 | + | |
214 | +/* | |
215 | + * GDB remote protocol parser: | |
216 | + */ | |
217 | + | |
218 | +static const char hexchars[] = "0123456789abcdef"; | |
219 | + | |
220 | +static int hex(char ch) | |
221 | +{ | |
222 | + if ((ch >= 'a') && (ch <= 'f')) | |
223 | + return ch - 'a' + 10; | |
224 | + if ((ch >= '0') && (ch <= '9')) | |
225 | + return ch - '0'; | |
226 | + if ((ch >= 'A') && (ch <= 'F')) | |
227 | + return ch - 'A' + 10; | |
228 | + return -1; | |
229 | +} | |
230 | + | |
231 | +/* scan for the sequence $<data>#<checksum> */ | |
232 | +static void get_packet(char *buffer) | |
233 | +{ | |
234 | + unsigned char checksum; | |
235 | + unsigned char xmitcsum; | |
236 | + int count; | |
237 | + char ch; | |
238 | + | |
239 | + do { | |
240 | + /* | |
241 | + * Spin and wait around for the start character, ignore all | |
242 | + * other characters: | |
243 | + */ | |
244 | + while ((ch = (kgdb_io_ops->read_char())) != '$') | |
245 | + /* nothing */; | |
246 | + | |
247 | + kgdb_connected = 1; | |
248 | + checksum = 0; | |
249 | + xmitcsum = -1; | |
250 | + | |
251 | + count = 0; | |
252 | + | |
253 | + /* | |
254 | + * now, read until a # or end of buffer is found: | |
255 | + */ | |
256 | + while (count < (BUFMAX - 1)) { | |
257 | + ch = kgdb_io_ops->read_char(); | |
258 | + if (ch == '#') | |
259 | + break; | |
260 | + checksum = checksum + ch; | |
261 | + buffer[count] = ch; | |
262 | + count = count + 1; | |
263 | + } | |
264 | + buffer[count] = 0; | |
265 | + | |
266 | + if (ch == '#') { | |
267 | + xmitcsum = hex(kgdb_io_ops->read_char()) << 4; | |
268 | + xmitcsum += hex(kgdb_io_ops->read_char()); | |
269 | + | |
270 | + if (checksum != xmitcsum) | |
271 | + /* failed checksum */ | |
272 | + kgdb_io_ops->write_char('-'); | |
273 | + else | |
274 | + /* successful transfer */ | |
275 | + kgdb_io_ops->write_char('+'); | |
276 | + if (kgdb_io_ops->flush) | |
277 | + kgdb_io_ops->flush(); | |
278 | + } | |
279 | + } while (checksum != xmitcsum); | |
280 | +} | |
281 | + | |
282 | +/* | |
283 | + * Send the packet in buffer. | |
284 | + * Check for gdb connection if asked for. | |
285 | + */ | |
286 | +static void put_packet(char *buffer) | |
287 | +{ | |
288 | + unsigned char checksum; | |
289 | + int count; | |
290 | + char ch; | |
291 | + | |
292 | + /* | |
293 | + * $<packet info>#<checksum>. | |
294 | + */ | |
295 | + while (1) { | |
296 | + kgdb_io_ops->write_char('$'); | |
297 | + checksum = 0; | |
298 | + count = 0; | |
299 | + | |
300 | + while ((ch = buffer[count])) { | |
301 | + kgdb_io_ops->write_char(ch); | |
302 | + checksum += ch; | |
303 | + count++; | |
304 | + } | |
305 | + | |
306 | + kgdb_io_ops->write_char('#'); | |
307 | + kgdb_io_ops->write_char(hexchars[checksum >> 4]); | |
308 | + kgdb_io_ops->write_char(hexchars[checksum & 0xf]); | |
309 | + if (kgdb_io_ops->flush) | |
310 | + kgdb_io_ops->flush(); | |
311 | + | |
312 | + /* Now see what we get in reply. */ | |
313 | + ch = kgdb_io_ops->read_char(); | |
314 | + | |
315 | + if (ch == 3) | |
316 | + ch = kgdb_io_ops->read_char(); | |
317 | + | |
318 | + /* If we get an ACK, we are done. */ | |
319 | + if (ch == '+') | |
320 | + return; | |
321 | + | |
322 | + /* | |
323 | + * If we get the start of another packet, this means | |
324 | + * that GDB is attempting to reconnect. We will NAK | |
325 | + * the packet being sent, and stop trying to send this | |
326 | + * packet. | |
327 | + */ | |
328 | + if (ch == '$') { | |
329 | + kgdb_io_ops->write_char('-'); | |
330 | + if (kgdb_io_ops->flush) | |
331 | + kgdb_io_ops->flush(); | |
332 | + return; | |
333 | + } | |
334 | + } | |
335 | +} | |
336 | + | |
337 | +static char *pack_hex_byte(char *pkt, u8 byte) | |
338 | +{ | |
339 | + *pkt++ = hexchars[byte >> 4]; | |
340 | + *pkt++ = hexchars[byte & 0xf]; | |
341 | + | |
342 | + return pkt; | |
343 | +} | |
344 | + | |
345 | +/* | |
346 | + * Convert the memory pointed to by mem into hex, placing result in buf. | |
347 | + * Return a pointer to the last char put in buf (null). May return an error. | |
348 | + */ | |
349 | +int kgdb_mem2hex(char *mem, char *buf, int count) | |
350 | +{ | |
351 | + char *tmp; | |
352 | + int err; | |
353 | + | |
354 | + /* | |
355 | + * We use the upper half of buf as an intermediate buffer for the | |
356 | + * raw memory copy. Hex conversion will work against this one. | |
357 | + */ | |
358 | + tmp = buf + count; | |
359 | + | |
360 | + err = probe_kernel_read(tmp, mem, count); | |
361 | + if (!err) { | |
362 | + while (count > 0) { | |
363 | + buf = pack_hex_byte(buf, *tmp); | |
364 | + tmp++; | |
365 | + count--; | |
366 | + } | |
367 | + | |
368 | + *buf = 0; | |
369 | + } | |
370 | + | |
371 | + return err; | |
372 | +} | |
373 | + | |
374 | +/* | |
375 | + * Copy the binary array pointed to by buf into mem. Fix $, #, and | |
376 | + * 0x7d escaped with 0x7d. Return a pointer to the character after | |
377 | + * the last byte written. | |
378 | + */ | |
379 | +static int kgdb_ebin2mem(char *buf, char *mem, int count) | |
380 | +{ | |
381 | + int err = 0; | |
382 | + char c; | |
383 | + | |
384 | + while (count-- > 0) { | |
385 | + c = *buf++; | |
386 | + if (c == 0x7d) | |
387 | + c = *buf++ ^ 0x20; | |
388 | + | |
389 | + err = probe_kernel_write(mem, &c, 1); | |
390 | + if (err) | |
391 | + break; | |
392 | + | |
393 | + mem++; | |
394 | + } | |
395 | + | |
396 | + return err; | |
397 | +} | |
398 | + | |
399 | +/* | |
400 | + * Convert the hex array pointed to by buf into binary to be placed in mem. | |
401 | + * Return a pointer to the character AFTER the last byte written. | |
402 | + * May return an error. | |
403 | + */ | |
404 | +int kgdb_hex2mem(char *buf, char *mem, int count) | |
405 | +{ | |
406 | + char *tmp_raw; | |
407 | + char *tmp_hex; | |
408 | + | |
409 | + /* | |
410 | + * We use the upper half of buf as an intermediate buffer for the | |
411 | + * raw memory that is converted from hex. | |
412 | + */ | |
413 | + tmp_raw = buf + count * 2; | |
414 | + | |
415 | + tmp_hex = tmp_raw - 1; | |
416 | + while (tmp_hex >= buf) { | |
417 | + tmp_raw--; | |
418 | + *tmp_raw = hex(*tmp_hex--); | |
419 | + *tmp_raw |= hex(*tmp_hex--) << 4; | |
420 | + } | |
421 | + | |
422 | + return probe_kernel_write(mem, tmp_raw, count); | |
423 | +} | |
424 | + | |
425 | +/* | |
426 | + * While we find nice hex chars, build a long_val. | |
427 | + * Return number of chars processed. | |
428 | + */ | |
429 | +int kgdb_hex2long(char **ptr, long *long_val) | |
430 | +{ | |
431 | + int hex_val; | |
432 | + int num = 0; | |
433 | + | |
434 | + *long_val = 0; | |
435 | + | |
436 | + while (**ptr) { | |
437 | + hex_val = hex(**ptr); | |
438 | + if (hex_val < 0) | |
439 | + break; | |
440 | + | |
441 | + *long_val = (*long_val << 4) | hex_val; | |
442 | + num++; | |
443 | + (*ptr)++; | |
444 | + } | |
445 | + | |
446 | + return num; | |
447 | +} | |
448 | + | |
449 | +/* Write memory due to an 'M' or 'X' packet. */ | |
450 | +static int write_mem_msg(int binary) | |
451 | +{ | |
452 | + char *ptr = &remcom_in_buffer[1]; | |
453 | + unsigned long addr; | |
454 | + unsigned long length; | |
455 | + int err; | |
456 | + | |
457 | + if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' && | |
458 | + kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') { | |
459 | + if (binary) | |
460 | + err = kgdb_ebin2mem(ptr, (char *)addr, length); | |
461 | + else | |
462 | + err = kgdb_hex2mem(ptr, (char *)addr, length); | |
463 | + if (err) | |
464 | + return err; | |
465 | + if (CACHE_FLUSH_IS_SAFE) | |
466 | + flush_icache_range(addr, addr + length + 1); | |
467 | + return 0; | |
468 | + } | |
469 | + | |
470 | + return -EINVAL; | |
471 | +} | |
472 | + | |
473 | +static void error_packet(char *pkt, int error) | |
474 | +{ | |
475 | + error = -error; | |
476 | + pkt[0] = 'E'; | |
477 | + pkt[1] = hexchars[(error / 10)]; | |
478 | + pkt[2] = hexchars[(error % 10)]; | |
479 | + pkt[3] = '\0'; | |
480 | +} | |
481 | + | |
482 | +/* | |
483 | + * Thread ID accessors. We represent a flat TID space to GDB, where | |
484 | + * the per CPU idle threads (which under Linux all have PID 0) are | |
485 | + * remapped to negative TIDs. | |
486 | + */ | |
487 | + | |
488 | +#define BUF_THREAD_ID_SIZE 16 | |
489 | + | |
490 | +static char *pack_threadid(char *pkt, unsigned char *id) | |
491 | +{ | |
492 | + char *limit; | |
493 | + | |
494 | + limit = pkt + BUF_THREAD_ID_SIZE; | |
495 | + while (pkt < limit) | |
496 | + pkt = pack_hex_byte(pkt, *id++); | |
497 | + | |
498 | + return pkt; | |
499 | +} | |
500 | + | |
501 | +static void int_to_threadref(unsigned char *id, int value) | |
502 | +{ | |
503 | + unsigned char *scan; | |
504 | + int i = 4; | |
505 | + | |
506 | + scan = (unsigned char *)id; | |
507 | + while (i--) | |
508 | + *scan++ = 0; | |
509 | + *scan++ = (value >> 24) & 0xff; | |
510 | + *scan++ = (value >> 16) & 0xff; | |
511 | + *scan++ = (value >> 8) & 0xff; | |
512 | + *scan++ = (value & 0xff); | |
513 | +} | |
514 | + | |
515 | +static struct task_struct *getthread(struct pt_regs *regs, int tid) | |
516 | +{ | |
517 | + /* | |
518 | + * Non-positive TIDs are remapped idle tasks: | |
519 | + */ | |
520 | + if (tid <= 0) | |
521 | + return idle_task(-tid); | |
522 | + | |
523 | + /* | |
524 | + * find_task_by_pid_ns() does not take the tasklist lock anymore | |
525 | + * but is nicely RCU locked - hence is a pretty resilient | |
526 | + * thing to use: | |
527 | + */ | |
528 | + return find_task_by_pid_ns(tid, &init_pid_ns); | |
529 | +} | |
530 | + | |
531 | +/* | |
532 | + * CPU debug state control: | |
533 | + */ | |
534 | + | |
535 | +#ifdef CONFIG_SMP | |
536 | +static void kgdb_wait(struct pt_regs *regs) | |
537 | +{ | |
538 | + unsigned long flags; | |
539 | + int cpu; | |
540 | + | |
541 | + local_irq_save(flags); | |
542 | + cpu = raw_smp_processor_id(); | |
543 | + kgdb_info[cpu].debuggerinfo = regs; | |
544 | + kgdb_info[cpu].task = current; | |
545 | + /* | |
546 | + * Make sure the above info reaches the primary CPU before | |
547 | + * our cpu_in_kgdb[] flag setting does: | |
548 | + */ | |
549 | + smp_wmb(); | |
550 | + atomic_set(&cpu_in_kgdb[cpu], 1); | |
551 | + | |
552 | + /* | |
553 | + * The primary CPU must be active to enter here, but this is | |
554 | + * guard in case the primary CPU had not been selected if | |
555 | + * this was an entry via nmi. | |
556 | + */ | |
557 | + while (atomic_read(&kgdb_active) == -1) | |
558 | + cpu_relax(); | |
559 | + | |
560 | + /* Wait till primary CPU goes completely into the debugger. */ | |
561 | + while (!atomic_read(&cpu_in_kgdb[atomic_read(&kgdb_active)])) | |
562 | + cpu_relax(); | |
563 | + | |
564 | + /* Wait till primary CPU is done with debugging */ | |
565 | + while (atomic_read(&passive_cpu_wait[cpu])) | |
566 | + cpu_relax(); | |
567 | + | |
568 | + kgdb_info[cpu].debuggerinfo = NULL; | |
569 | + kgdb_info[cpu].task = NULL; | |
570 | + | |
571 | + /* fix up hardware debug registers on local cpu */ | |
572 | + if (arch_kgdb_ops.correct_hw_break) | |
573 | + arch_kgdb_ops.correct_hw_break(); | |
574 | + | |
575 | + /* Signal the primary CPU that we are done: */ | |
576 | + atomic_set(&cpu_in_kgdb[cpu], 0); | |
577 | + local_irq_restore(flags); | |
578 | +} | |
579 | +#endif | |
580 | + | |
581 | +/* | |
582 | + * Some architectures need cache flushes when we set/clear a | |
583 | + * breakpoint: | |
584 | + */ | |
585 | +static void kgdb_flush_swbreak_addr(unsigned long addr) | |
586 | +{ | |
587 | + if (!CACHE_FLUSH_IS_SAFE) | |
588 | + return; | |
589 | + | |
590 | + if (current->mm) { | |
591 | + flush_cache_range(current->mm->mmap_cache, | |
592 | + addr, addr + BREAK_INSTR_SIZE); | |
593 | + } else { | |
594 | + flush_icache_range(addr, addr + BREAK_INSTR_SIZE); | |
595 | + } | |
596 | +} | |
597 | + | |
598 | +/* | |
599 | + * SW breakpoint management: | |
600 | + */ | |
601 | +static int kgdb_activate_sw_breakpoints(void) | |
602 | +{ | |
603 | + unsigned long addr; | |
604 | + int error = 0; | |
605 | + int i; | |
606 | + | |
607 | + for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { | |
608 | + if (kgdb_break[i].state != BP_SET) | |
609 | + continue; | |
610 | + | |
611 | + addr = kgdb_break[i].bpt_addr; | |
612 | + error = kgdb_arch_set_breakpoint(addr, | |
613 | + kgdb_break[i].saved_instr); | |
614 | + if (error) | |
615 | + return error; | |
616 | + | |
617 | + kgdb_flush_swbreak_addr(addr); | |
618 | + kgdb_break[i].state = BP_ACTIVE; | |
619 | + } | |
620 | + return 0; | |
621 | +} | |
622 | + | |
623 | +static int kgdb_set_sw_break(unsigned long addr) | |
624 | +{ | |
625 | + int err = kgdb_validate_break_address(addr); | |
626 | + int breakno = -1; | |
627 | + int i; | |
628 | + | |
629 | + if (err) | |
630 | + return err; | |
631 | + | |
632 | + for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { | |
633 | + if ((kgdb_break[i].state == BP_SET) && | |
634 | + (kgdb_break[i].bpt_addr == addr)) | |
635 | + return -EEXIST; | |
636 | + } | |
637 | + for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { | |
638 | + if (kgdb_break[i].state == BP_REMOVED && | |
639 | + kgdb_break[i].bpt_addr == addr) { | |
640 | + breakno = i; | |
641 | + break; | |
642 | + } | |
643 | + } | |
644 | + | |
645 | + if (breakno == -1) { | |
646 | + for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { | |
647 | + if (kgdb_break[i].state == BP_UNDEFINED) { | |
648 | + breakno = i; | |
649 | + break; | |
650 | + } | |
651 | + } | |
652 | + } | |
653 | + | |
654 | + if (breakno == -1) | |
655 | + return -E2BIG; | |
656 | + | |
657 | + kgdb_break[breakno].state = BP_SET; | |
658 | + kgdb_break[breakno].type = BP_BREAKPOINT; | |
659 | + kgdb_break[breakno].bpt_addr = addr; | |
660 | + | |
661 | + return 0; | |
662 | +} | |
663 | + | |
664 | +static int kgdb_deactivate_sw_breakpoints(void) | |
665 | +{ | |
666 | + unsigned long addr; | |
667 | + int error = 0; | |
668 | + int i; | |
669 | + | |
670 | + for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { | |
671 | + if (kgdb_break[i].state != BP_ACTIVE) | |
672 | + continue; | |
673 | + addr = kgdb_break[i].bpt_addr; | |
674 | + error = kgdb_arch_remove_breakpoint(addr, | |
675 | + kgdb_break[i].saved_instr); | |
676 | + if (error) | |
677 | + return error; | |
678 | + | |
679 | + kgdb_flush_swbreak_addr(addr); | |
680 | + kgdb_break[i].state = BP_SET; | |
681 | + } | |
682 | + return 0; | |
683 | +} | |
684 | + | |
685 | +static int kgdb_remove_sw_break(unsigned long addr) | |
686 | +{ | |
687 | + int i; | |
688 | + | |
689 | + for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { | |
690 | + if ((kgdb_break[i].state == BP_SET) && | |
691 | + (kgdb_break[i].bpt_addr == addr)) { | |
692 | + kgdb_break[i].state = BP_REMOVED; | |
693 | + return 0; | |
694 | + } | |
695 | + } | |
696 | + return -ENOENT; | |
697 | +} | |
698 | + | |
699 | +int kgdb_isremovedbreak(unsigned long addr) | |
700 | +{ | |
701 | + int i; | |
702 | + | |
703 | + for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { | |
704 | + if ((kgdb_break[i].state == BP_REMOVED) && | |
705 | + (kgdb_break[i].bpt_addr == addr)) | |
706 | + return 1; | |
707 | + } | |
708 | + return 0; | |
709 | +} | |
710 | + | |
711 | +int remove_all_break(void) | |
712 | +{ | |
713 | + unsigned long addr; | |
714 | + int error; | |
715 | + int i; | |
716 | + | |
717 | + /* Clear memory breakpoints. */ | |
718 | + for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { | |
719 | + if (kgdb_break[i].state != BP_SET) | |
720 | + continue; | |
721 | + addr = kgdb_break[i].bpt_addr; | |
722 | + error = kgdb_arch_remove_breakpoint(addr, | |
723 | + kgdb_break[i].saved_instr); | |
724 | + if (error) | |
725 | + return error; | |
726 | + kgdb_break[i].state = BP_REMOVED; | |
727 | + } | |
728 | + | |
729 | + /* Clear hardware breakpoints. */ | |
730 | + if (arch_kgdb_ops.remove_all_hw_break) | |
731 | + arch_kgdb_ops.remove_all_hw_break(); | |
732 | + | |
733 | + return 0; | |
734 | +} | |
735 | + | |
736 | +/* | |
737 | + * Remap normal tasks to their real PID, idle tasks to -1 ... -NR_CPUs: | |
738 | + */ | |
739 | +static inline int shadow_pid(int realpid) | |
740 | +{ | |
741 | + if (realpid) | |
742 | + return realpid; | |
743 | + | |
744 | + return -1-raw_smp_processor_id(); | |
745 | +} | |
746 | + | |
747 | +static char gdbmsgbuf[BUFMAX + 1]; | |
748 | + | |
749 | +static void kgdb_msg_write(const char *s, int len) | |
750 | +{ | |
751 | + char *bufptr; | |
752 | + int wcount; | |
753 | + int i; | |
754 | + | |
755 | + /* 'O'utput */ | |
756 | + gdbmsgbuf[0] = 'O'; | |
757 | + | |
758 | + /* Fill and send buffers... */ | |
759 | + while (len > 0) { | |
760 | + bufptr = gdbmsgbuf + 1; | |
761 | + | |
762 | + /* Calculate how many this time */ | |
763 | + if ((len << 1) > (BUFMAX - 2)) | |
764 | + wcount = (BUFMAX - 2) >> 1; | |
765 | + else | |
766 | + wcount = len; | |
767 | + | |
768 | + /* Pack in hex chars */ | |
769 | + for (i = 0; i < wcount; i++) | |
770 | + bufptr = pack_hex_byte(bufptr, s[i]); | |
771 | + *bufptr = '\0'; | |
772 | + | |
773 | + /* Move up */ | |
774 | + s += wcount; | |
775 | + len -= wcount; | |
776 | + | |
777 | + /* Write packet */ | |
778 | + put_packet(gdbmsgbuf); | |
779 | + } | |
780 | +} | |
781 | + | |
782 | +/* | |
783 | + * Return true if there is a valid kgdb I/O module. Also if no | |
784 | + * debugger is attached a message can be printed to the console about | |
785 | + * waiting for the debugger to attach. | |
786 | + * | |
787 | + * The print_wait argument is only to be true when called from inside | |
788 | + * the core kgdb_handle_exception, because it will wait for the | |
789 | + * debugger to attach. | |
790 | + */ | |
791 | +static int kgdb_io_ready(int print_wait) | |
792 | +{ | |
793 | + if (!kgdb_io_ops) | |
794 | + return 0; | |
795 | + if (kgdb_connected) | |
796 | + return 1; | |
797 | + if (atomic_read(&kgdb_setting_breakpoint)) | |
798 | + return 1; | |
799 | + if (print_wait) | |
800 | + printk(KERN_CRIT "KGDB: Waiting for remote debugger\n"); | |
801 | + return 1; | |
802 | +} | |
803 | + | |
804 | +/* | |
805 | + * All the functions that start with gdb_cmd are the various | |
806 | + * operations to implement the handlers for the gdbserial protocol | |
807 | + * where KGDB is communicating with an external debugger | |
808 | + */ | |
809 | + | |
810 | +/* Handle the '?' status packets */ | |
811 | +static void gdb_cmd_status(struct kgdb_state *ks) | |
812 | +{ | |
813 | + /* | |
814 | + * We know that this packet is only sent | |
815 | + * during initial connect. So to be safe, | |
816 | + * we clear out our breakpoints now in case | |
817 | + * GDB is reconnecting. | |
818 | + */ | |
819 | + remove_all_break(); | |
820 | + | |
821 | + remcom_out_buffer[0] = 'S'; | |
822 | + pack_hex_byte(&remcom_out_buffer[1], ks->signo); | |
823 | +} | |
824 | + | |
825 | +/* Handle the 'g' get registers request */ | |
826 | +static void gdb_cmd_getregs(struct kgdb_state *ks) | |
827 | +{ | |
828 | + struct task_struct *thread; | |
829 | + void *local_debuggerinfo; | |
830 | + int i; | |
831 | + | |
832 | + thread = kgdb_usethread; | |
833 | + if (!thread) { | |
834 | + thread = kgdb_info[ks->cpu].task; | |
835 | + local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo; | |
836 | + } else { | |
837 | + local_debuggerinfo = NULL; | |
838 | + for (i = 0; i < NR_CPUS; i++) { | |
839 | + /* | |
840 | + * Try to find the task on some other | |
841 | + * or possibly this node if we do not | |
842 | + * find the matching task then we try | |
843 | + * to approximate the results. | |
844 | + */ | |
845 | + if (thread == kgdb_info[i].task) | |
846 | + local_debuggerinfo = kgdb_info[i].debuggerinfo; | |
847 | + } | |
848 | + } | |
849 | + | |
850 | + /* | |
851 | + * All threads that don't have debuggerinfo should be | |
852 | + * in __schedule() sleeping, since all other CPUs | |
853 | + * are in kgdb_wait, and thus have debuggerinfo. | |
854 | + */ | |
855 | + if (local_debuggerinfo) { | |
856 | + pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo); | |
857 | + } else { | |
858 | + /* | |
859 | + * Pull stuff saved during switch_to; nothing | |
860 | + * else is accessible (or even particularly | |
861 | + * relevant). | |
862 | + * | |
863 | + * This should be enough for a stack trace. | |
864 | + */ | |
865 | + sleeping_thread_to_gdb_regs(gdb_regs, thread); | |
866 | + } | |
867 | + kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES); | |
868 | +} | |
869 | + | |
870 | +/* Handle the 'G' set registers request */ | |
871 | +static void gdb_cmd_setregs(struct kgdb_state *ks) | |
872 | +{ | |
873 | + kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES); | |
874 | + | |
875 | + if (kgdb_usethread && kgdb_usethread != current) { | |
876 | + error_packet(remcom_out_buffer, -EINVAL); | |
877 | + } else { | |
878 | + gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs); | |
879 | + strcpy(remcom_out_buffer, "OK"); | |
880 | + } | |
881 | +} | |
882 | + | |
883 | +/* Handle the 'm' memory read bytes */ | |
884 | +static void gdb_cmd_memread(struct kgdb_state *ks) | |
885 | +{ | |
886 | + char *ptr = &remcom_in_buffer[1]; | |
887 | + unsigned long length; | |
888 | + unsigned long addr; | |
889 | + int err; | |
890 | + | |
891 | + if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' && | |
892 | + kgdb_hex2long(&ptr, &length) > 0) { | |
893 | + err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length); | |
894 | + if (err) | |
895 | + error_packet(remcom_out_buffer, err); | |
896 | + } else { | |
897 | + error_packet(remcom_out_buffer, -EINVAL); | |
898 | + } | |
899 | +} | |
900 | + | |
901 | +/* Handle the 'M' memory write bytes */ | |
902 | +static void gdb_cmd_memwrite(struct kgdb_state *ks) | |
903 | +{ | |
904 | + int err = write_mem_msg(0); | |
905 | + | |
906 | + if (err) | |
907 | + error_packet(remcom_out_buffer, err); | |
908 | + else | |
909 | + strcpy(remcom_out_buffer, "OK"); | |
910 | +} | |
911 | + | |
912 | +/* Handle the 'X' memory binary write bytes */ | |
913 | +static void gdb_cmd_binwrite(struct kgdb_state *ks) | |
914 | +{ | |
915 | + int err = write_mem_msg(1); | |
916 | + | |
917 | + if (err) | |
918 | + error_packet(remcom_out_buffer, err); | |
919 | + else | |
920 | + strcpy(remcom_out_buffer, "OK"); | |
921 | +} | |
922 | + | |
923 | +/* Handle the 'D' or 'k', detach or kill packets */ | |
924 | +static void gdb_cmd_detachkill(struct kgdb_state *ks) | |
925 | +{ | |
926 | + int error; | |
927 | + | |
928 | + /* The detach case */ | |
929 | + if (remcom_in_buffer[0] == 'D') { | |
930 | + error = remove_all_break(); | |
931 | + if (error < 0) { | |
932 | + error_packet(remcom_out_buffer, error); | |
933 | + } else { | |
934 | + strcpy(remcom_out_buffer, "OK"); | |
935 | + kgdb_connected = 0; | |
936 | + } | |
937 | + put_packet(remcom_out_buffer); | |
938 | + } else { | |
939 | + /* | |
940 | + * Assume the kill case, with no exit code checking, | |
941 | + * trying to force detach the debugger: | |
942 | + */ | |
943 | + remove_all_break(); | |
944 | + kgdb_connected = 0; | |
945 | + } | |
946 | +} | |
947 | + | |
948 | +/* Handle the 'R' reboot packets */ | |
949 | +static int gdb_cmd_reboot(struct kgdb_state *ks) | |
950 | +{ | |
951 | + /* For now, only honor R0 */ | |
952 | + if (strcmp(remcom_in_buffer, "R0") == 0) { | |
953 | + printk(KERN_CRIT "Executing emergency reboot\n"); | |
954 | + strcpy(remcom_out_buffer, "OK"); | |
955 | + put_packet(remcom_out_buffer); | |
956 | + | |
957 | + /* | |
958 | + * Execution should not return from | |
959 | + * machine_emergency_restart() | |
960 | + */ | |
961 | + machine_emergency_restart(); | |
962 | + kgdb_connected = 0; | |
963 | + | |
964 | + return 1; | |
965 | + } | |
966 | + return 0; | |
967 | +} | |
968 | + | |
969 | +/* Handle the 'q' query packets */ | |
970 | +static void gdb_cmd_query(struct kgdb_state *ks) | |
971 | +{ | |
972 | + struct task_struct *thread; | |
973 | + unsigned char thref[8]; | |
974 | + char *ptr; | |
975 | + int i; | |
976 | + | |
977 | + switch (remcom_in_buffer[1]) { | |
978 | + case 's': | |
979 | + case 'f': | |
980 | + if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10)) { | |
981 | + error_packet(remcom_out_buffer, -EINVAL); | |
982 | + break; | |
983 | + } | |
984 | + | |
985 | + if (remcom_in_buffer[1] == 'f') | |
986 | + ks->threadid = 1; | |
987 | + | |
988 | + remcom_out_buffer[0] = 'm'; | |
989 | + ptr = remcom_out_buffer + 1; | |
990 | + | |
991 | + for (i = 0; i < 17; ks->threadid++) { | |
992 | + thread = getthread(ks->linux_regs, ks->threadid); | |
993 | + if (thread) { | |
994 | + int_to_threadref(thref, ks->threadid); | |
995 | + pack_threadid(ptr, thref); | |
996 | + ptr += BUF_THREAD_ID_SIZE; | |
997 | + *(ptr++) = ','; | |
998 | + i++; | |
999 | + } | |
1000 | + } | |
1001 | + *(--ptr) = '\0'; | |
1002 | + break; | |
1003 | + | |
1004 | + case 'C': | |
1005 | + /* Current thread id */ | |
1006 | + strcpy(remcom_out_buffer, "QC"); | |
1007 | + ks->threadid = shadow_pid(current->pid); | |
1008 | + int_to_threadref(thref, ks->threadid); | |
1009 | + pack_threadid(remcom_out_buffer + 2, thref); | |
1010 | + break; | |
1011 | + case 'T': | |
1012 | + if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16)) { | |
1013 | + error_packet(remcom_out_buffer, -EINVAL); | |
1014 | + break; | |
1015 | + } | |
1016 | + ks->threadid = 0; | |
1017 | + ptr = remcom_in_buffer + 17; | |
1018 | + kgdb_hex2long(&ptr, &ks->threadid); | |
1019 | + if (!getthread(ks->linux_regs, ks->threadid)) { | |
1020 | + error_packet(remcom_out_buffer, -EINVAL); | |
1021 | + break; | |
1022 | + } | |
1023 | + if (ks->threadid > 0) { | |
1024 | + kgdb_mem2hex(getthread(ks->linux_regs, | |
1025 | + ks->threadid)->comm, | |
1026 | + remcom_out_buffer, 16); | |
1027 | + } else { | |
1028 | + static char tmpstr[23 + BUF_THREAD_ID_SIZE]; | |
1029 | + | |
1030 | + sprintf(tmpstr, "Shadow task %d for pid 0", | |
1031 | + (int)(-ks->threadid-1)); | |
1032 | + kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr)); | |
1033 | + } | |
1034 | + break; | |
1035 | + } | |
1036 | +} | |
1037 | + | |
1038 | +/* Handle the 'H' task query packets */ | |
1039 | +static void gdb_cmd_task(struct kgdb_state *ks) | |
1040 | +{ | |
1041 | + struct task_struct *thread; | |
1042 | + char *ptr; | |
1043 | + | |
1044 | + switch (remcom_in_buffer[1]) { | |
1045 | + case 'g': | |
1046 | + ptr = &remcom_in_buffer[2]; | |
1047 | + kgdb_hex2long(&ptr, &ks->threadid); | |
1048 | + thread = getthread(ks->linux_regs, ks->threadid); | |
1049 | + if (!thread && ks->threadid > 0) { | |
1050 | + error_packet(remcom_out_buffer, -EINVAL); | |
1051 | + break; | |
1052 | + } | |
1053 | + kgdb_usethread = thread; | |
1054 | + ks->kgdb_usethreadid = ks->threadid; | |
1055 | + strcpy(remcom_out_buffer, "OK"); | |
1056 | + break; | |
1057 | + case 'c': | |
1058 | + ptr = &remcom_in_buffer[2]; | |
1059 | + kgdb_hex2long(&ptr, &ks->threadid); | |
1060 | + if (!ks->threadid) { | |
1061 | + kgdb_contthread = NULL; | |
1062 | + } else { | |
1063 | + thread = getthread(ks->linux_regs, ks->threadid); | |
1064 | + if (!thread && ks->threadid > 0) { | |
1065 | + error_packet(remcom_out_buffer, -EINVAL); | |
1066 | + break; | |
1067 | + } | |
1068 | + kgdb_contthread = thread; | |
1069 | + } | |
1070 | + strcpy(remcom_out_buffer, "OK"); | |
1071 | + break; | |
1072 | + } | |
1073 | +} | |
1074 | + | |
1075 | +/* Handle the 'T' thread query packets */ | |
1076 | +static void gdb_cmd_thread(struct kgdb_state *ks) | |
1077 | +{ | |
1078 | + char *ptr = &remcom_in_buffer[1]; | |
1079 | + struct task_struct *thread; | |
1080 | + | |
1081 | + kgdb_hex2long(&ptr, &ks->threadid); | |
1082 | + thread = getthread(ks->linux_regs, ks->threadid); | |
1083 | + if (thread) | |
1084 | + strcpy(remcom_out_buffer, "OK"); | |
1085 | + else | |
1086 | + error_packet(remcom_out_buffer, -EINVAL); | |
1087 | +} | |
1088 | + | |
1089 | +/* Handle the 'z' or 'Z' breakpoint remove or set packets */ | |
1090 | +static void gdb_cmd_break(struct kgdb_state *ks) | |
1091 | +{ | |
1092 | + /* | |
1093 | + * Since GDB-5.3, it's been drafted that '0' is a software | |
1094 | + * breakpoint, '1' is a hardware breakpoint, so let's do that. | |
1095 | + */ | |
1096 | + char *bpt_type = &remcom_in_buffer[1]; | |
1097 | + char *ptr = &remcom_in_buffer[2]; | |
1098 | + unsigned long addr; | |
1099 | + unsigned long length; | |
1100 | + int error = 0; | |
1101 | + | |
1102 | + if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') { | |
1103 | + /* Unsupported */ | |
1104 | + if (*bpt_type > '4') | |
1105 | + return; | |
1106 | + } else { | |
1107 | + if (*bpt_type != '0' && *bpt_type != '1') | |
1108 | + /* Unsupported. */ | |
1109 | + return; | |
1110 | + } | |
1111 | + | |
1112 | + /* | |
1113 | + * Test if this is a hardware breakpoint, and | |
1114 | + * if we support it: | |
1115 | + */ | |
1116 | + if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT)) | |
1117 | + /* Unsupported. */ | |
1118 | + return; | |
1119 | + | |
1120 | + if (*(ptr++) != ',') { | |
1121 | + error_packet(remcom_out_buffer, -EINVAL); | |
1122 | + return; | |
1123 | + } | |
1124 | + if (!kgdb_hex2long(&ptr, &addr)) { | |
1125 | + error_packet(remcom_out_buffer, -EINVAL); | |
1126 | + return; | |
1127 | + } | |
1128 | + if (*(ptr++) != ',' || | |
1129 | + !kgdb_hex2long(&ptr, &length)) { | |
1130 | + error_packet(remcom_out_buffer, -EINVAL); | |
1131 | + return; | |
1132 | + } | |
1133 | + | |
1134 | + if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0') | |
1135 | + error = kgdb_set_sw_break(addr); | |
1136 | + else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0') | |
1137 | + error = kgdb_remove_sw_break(addr); | |
1138 | + else if (remcom_in_buffer[0] == 'Z') | |
1139 | + error = arch_kgdb_ops.set_hw_breakpoint(addr, | |
1140 | + (int)length, *bpt_type); | |
1141 | + else if (remcom_in_buffer[0] == 'z') | |
1142 | + error = arch_kgdb_ops.remove_hw_breakpoint(addr, | |
1143 | + (int) length, *bpt_type); | |
1144 | + | |
1145 | + if (error == 0) | |
1146 | + strcpy(remcom_out_buffer, "OK"); | |
1147 | + else | |
1148 | + error_packet(remcom_out_buffer, error); | |
1149 | +} | |
1150 | + | |
1151 | +/* Handle the 'C' signal / exception passing packets */ | |
1152 | +static int gdb_cmd_exception_pass(struct kgdb_state *ks) | |
1153 | +{ | |
1154 | + /* C09 == pass exception | |
1155 | + * C15 == detach kgdb, pass exception | |
1156 | + */ | |
1157 | + if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') { | |
1158 | + | |
1159 | + ks->pass_exception = 1; | |
1160 | + remcom_in_buffer[0] = 'c'; | |
1161 | + | |
1162 | + } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') { | |
1163 | + | |
1164 | + ks->pass_exception = 1; | |
1165 | + remcom_in_buffer[0] = 'D'; | |
1166 | + remove_all_break(); | |
1167 | + kgdb_connected = 0; | |
1168 | + return 1; | |
1169 | + | |
1170 | + } else { | |
1171 | + error_packet(remcom_out_buffer, -EINVAL); | |
1172 | + return 0; | |
1173 | + } | |
1174 | + | |
1175 | + /* Indicate fall through */ | |
1176 | + return -1; | |
1177 | +} | |
1178 | + | |
1179 | +/* | |
1180 | + * This function performs all gdbserial command procesing | |
1181 | + */ | |
1182 | +static int gdb_serial_stub(struct kgdb_state *ks) | |
1183 | +{ | |
1184 | + int error = 0; | |
1185 | + int tmp; | |
1186 | + | |
1187 | + /* Clear the out buffer. */ | |
1188 | + memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer)); | |
1189 | + | |
1190 | + if (kgdb_connected) { | |
1191 | + unsigned char thref[8]; | |
1192 | + char *ptr; | |
1193 | + | |
1194 | + /* Reply to host that an exception has occurred */ | |
1195 | + ptr = remcom_out_buffer; | |
1196 | + *ptr++ = 'T'; | |
1197 | + ptr = pack_hex_byte(ptr, ks->signo); | |
1198 | + ptr += strlen(strcpy(ptr, "thread:")); | |
1199 | + int_to_threadref(thref, shadow_pid(current->pid)); | |
1200 | + ptr = pack_threadid(ptr, thref); | |
1201 | + *ptr++ = ';'; | |
1202 | + put_packet(remcom_out_buffer); | |
1203 | + } | |
1204 | + | |
1205 | + kgdb_usethread = kgdb_info[ks->cpu].task; | |
1206 | + ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid); | |
1207 | + ks->pass_exception = 0; | |
1208 | + | |
1209 | + while (1) { | |
1210 | + error = 0; | |
1211 | + | |
1212 | + /* Clear the out buffer. */ | |
1213 | + memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer)); | |
1214 | + | |
1215 | + get_packet(remcom_in_buffer); | |
1216 | + | |
1217 | + switch (remcom_in_buffer[0]) { | |
1218 | + case '?': /* gdbserial status */ | |
1219 | + gdb_cmd_status(ks); | |
1220 | + break; | |
1221 | + case 'g': /* return the value of the CPU registers */ | |
1222 | + gdb_cmd_getregs(ks); | |
1223 | + break; | |
1224 | + case 'G': /* set the value of the CPU registers - return OK */ | |
1225 | + gdb_cmd_setregs(ks); | |
1226 | + break; | |
1227 | + case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */ | |
1228 | + gdb_cmd_memread(ks); | |
1229 | + break; | |
1230 | + case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */ | |
1231 | + gdb_cmd_memwrite(ks); | |
1232 | + break; | |
1233 | + case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */ | |
1234 | + gdb_cmd_binwrite(ks); | |
1235 | + break; | |
1236 | + /* kill or detach. KGDB should treat this like a | |
1237 | + * continue. | |
1238 | + */ | |
1239 | + case 'D': /* Debugger detach */ | |
1240 | + case 'k': /* Debugger detach via kill */ | |
1241 | + gdb_cmd_detachkill(ks); | |
1242 | + goto default_handle; | |
1243 | + case 'R': /* Reboot */ | |
1244 | + if (gdb_cmd_reboot(ks)) | |
1245 | + goto default_handle; | |
1246 | + break; | |
1247 | + case 'q': /* query command */ | |
1248 | + gdb_cmd_query(ks); | |
1249 | + break; | |
1250 | + case 'H': /* task related */ | |
1251 | + gdb_cmd_task(ks); | |
1252 | + break; | |
1253 | + case 'T': /* Query thread status */ | |
1254 | + gdb_cmd_thread(ks); | |
1255 | + break; | |
1256 | + case 'z': /* Break point remove */ | |
1257 | + case 'Z': /* Break point set */ | |
1258 | + gdb_cmd_break(ks); | |
1259 | + break; | |
1260 | + case 'C': /* Exception passing */ | |
1261 | + tmp = gdb_cmd_exception_pass(ks); | |
1262 | + if (tmp > 0) | |
1263 | + goto default_handle; | |
1264 | + if (tmp == 0) | |
1265 | + break; | |
1266 | + /* Fall through on tmp < 0 */ | |
1267 | + case 'c': /* Continue packet */ | |
1268 | + case 's': /* Single step packet */ | |
1269 | + if (kgdb_contthread && kgdb_contthread != current) { | |
1270 | + /* Can't switch threads in kgdb */ | |
1271 | + error_packet(remcom_out_buffer, -EINVAL); | |
1272 | + break; | |
1273 | + } | |
1274 | + kgdb_activate_sw_breakpoints(); | |
1275 | + /* Fall through to default processing */ | |
1276 | + default: | |
1277 | +default_handle: | |
1278 | + error = kgdb_arch_handle_exception(ks->ex_vector, | |
1279 | + ks->signo, | |
1280 | + ks->err_code, | |
1281 | + remcom_in_buffer, | |
1282 | + remcom_out_buffer, | |
1283 | + ks->linux_regs); | |
1284 | + /* | |
1285 | + * Leave cmd processing on error, detach, | |
1286 | + * kill, continue, or single step. | |
1287 | + */ | |
1288 | + if (error >= 0 || remcom_in_buffer[0] == 'D' || | |
1289 | + remcom_in_buffer[0] == 'k') { | |
1290 | + error = 0; | |
1291 | + goto kgdb_exit; | |
1292 | + } | |
1293 | + | |
1294 | + } | |
1295 | + | |
1296 | + /* reply to the request */ | |
1297 | + put_packet(remcom_out_buffer); | |
1298 | + } | |
1299 | + | |
1300 | +kgdb_exit: | |
1301 | + if (ks->pass_exception) | |
1302 | + error = 1; | |
1303 | + return error; | |
1304 | +} | |
1305 | + | |
1306 | +static int kgdb_reenter_check(struct kgdb_state *ks) | |
1307 | +{ | |
1308 | + unsigned long addr; | |
1309 | + | |
1310 | + if (atomic_read(&kgdb_active) != raw_smp_processor_id()) | |
1311 | + return 0; | |
1312 | + | |
1313 | + /* Panic on recursive debugger calls: */ | |
1314 | + exception_level++; | |
1315 | + addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs); | |
1316 | + kgdb_deactivate_sw_breakpoints(); | |
1317 | + | |
1318 | + /* | |
1319 | + * If the break point removed ok at the place exception | |
1320 | + * occurred, try to recover and print a warning to the end | |
1321 | + * user because the user planted a breakpoint in a place that | |
1322 | + * KGDB needs in order to function. | |
1323 | + */ | |
1324 | + if (kgdb_remove_sw_break(addr) == 0) { | |
1325 | + exception_level = 0; | |
1326 | + kgdb_skipexception(ks->ex_vector, ks->linux_regs); | |
1327 | + kgdb_activate_sw_breakpoints(); | |
1328 | + printk(KERN_CRIT "KGDB: re-enter error: breakpoint removed\n"); | |
1329 | + WARN_ON_ONCE(1); | |
1330 | + | |
1331 | + return 1; | |
1332 | + } | |
1333 | + remove_all_break(); | |
1334 | + kgdb_skipexception(ks->ex_vector, ks->linux_regs); | |
1335 | + | |
1336 | + if (exception_level > 1) { | |
1337 | + dump_stack(); | |
1338 | + panic("Recursive entry to debugger"); | |
1339 | + } | |
1340 | + | |
1341 | + printk(KERN_CRIT "KGDB: re-enter exception: ALL breakpoints killed\n"); | |
1342 | + dump_stack(); | |
1343 | + panic("Recursive entry to debugger"); | |
1344 | + | |
1345 | + return 1; | |
1346 | +} | |
1347 | + | |
1348 | +/* | |
1349 | + * kgdb_handle_exception() - main entry point from a kernel exception | |
1350 | + * | |
1351 | + * Locking hierarchy: | |
1352 | + * interface locks, if any (begin_session) | |
1353 | + * kgdb lock (kgdb_active) | |
1354 | + */ | |
1355 | +int | |
1356 | +kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs) | |
1357 | +{ | |
1358 | + struct kgdb_state kgdb_var; | |
1359 | + struct kgdb_state *ks = &kgdb_var; | |
1360 | + unsigned long flags; | |
1361 | + int error = 0; | |
1362 | + int i, cpu; | |
1363 | + | |
1364 | + ks->cpu = raw_smp_processor_id(); | |
1365 | + ks->ex_vector = evector; | |
1366 | + ks->signo = signo; | |
1367 | + ks->ex_vector = evector; | |
1368 | + ks->err_code = ecode; | |
1369 | + ks->kgdb_usethreadid = 0; | |
1370 | + ks->linux_regs = regs; | |
1371 | + | |
1372 | + if (kgdb_reenter_check(ks)) | |
1373 | + return 0; /* Ouch, double exception ! */ | |
1374 | + | |
1375 | +acquirelock: | |
1376 | + /* | |
1377 | + * Interrupts will be restored by the 'trap return' code, except when | |
1378 | + * single stepping. | |
1379 | + */ | |
1380 | + local_irq_save(flags); | |
1381 | + | |
1382 | + cpu = raw_smp_processor_id(); | |
1383 | + | |
1384 | + /* | |
1385 | + * Acquire the kgdb_active lock: | |
1386 | + */ | |
1387 | + while (atomic_cmpxchg(&kgdb_active, -1, cpu) != -1) | |
1388 | + cpu_relax(); | |
1389 | + | |
1390 | + /* | |
1391 | + * Do not start the debugger connection on this CPU if the last | |
1392 | + * instance of the exception handler wanted to come into the | |
1393 | + * debugger on a different CPU via a single step | |
1394 | + */ | |
1395 | + if (atomic_read(&kgdb_cpu_doing_single_step) != -1 && | |
1396 | + atomic_read(&kgdb_cpu_doing_single_step) != cpu) { | |
1397 | + | |
1398 | + atomic_set(&kgdb_active, -1); | |
1399 | + local_irq_restore(flags); | |
1400 | + | |
1401 | + goto acquirelock; | |
1402 | + } | |
1403 | + | |
1404 | + if (!kgdb_io_ready(1)) { | |
1405 | + error = 1; | |
1406 | + goto kgdb_restore; /* No I/O connection, so resume the system */ | |
1407 | + } | |
1408 | + | |
1409 | + /* | |
1410 | + * Don't enter if we have hit a removed breakpoint. | |
1411 | + */ | |
1412 | + if (kgdb_skipexception(ks->ex_vector, ks->linux_regs)) | |
1413 | + goto kgdb_restore; | |
1414 | + | |
1415 | + /* Call the I/O driver's pre_exception routine */ | |
1416 | + if (kgdb_io_ops->pre_exception) | |
1417 | + kgdb_io_ops->pre_exception(); | |
1418 | + | |
1419 | + kgdb_info[ks->cpu].debuggerinfo = ks->linux_regs; | |
1420 | + kgdb_info[ks->cpu].task = current; | |
1421 | + | |
1422 | + kgdb_disable_hw_debug(ks->linux_regs); | |
1423 | + | |
1424 | + /* | |
1425 | + * Get the passive CPU lock which will hold all the non-primary | |
1426 | + * CPU in a spin state while the debugger is active | |
1427 | + */ | |
1428 | + if (!kgdb_single_step || !kgdb_contthread) { | |
1429 | + for (i = 0; i < NR_CPUS; i++) | |
1430 | + atomic_set(&passive_cpu_wait[i], 1); | |
1431 | + } | |
1432 | + | |
1433 | +#ifdef CONFIG_SMP | |
1434 | + /* Signal the other CPUs to enter kgdb_wait() */ | |
1435 | + if ((!kgdb_single_step || !kgdb_contthread) && kgdb_do_roundup) | |
1436 | + kgdb_roundup_cpus(flags); | |
1437 | +#endif | |
1438 | + | |
1439 | + /* | |
1440 | + * spin_lock code is good enough as a barrier so we don't | |
1441 | + * need one here: | |
1442 | + */ | |
1443 | + atomic_set(&cpu_in_kgdb[ks->cpu], 1); | |
1444 | + | |
1445 | + /* | |
1446 | + * Wait for the other CPUs to be notified and be waiting for us: | |
1447 | + */ | |
1448 | + for_each_online_cpu(i) { | |
1449 | + while (!atomic_read(&cpu_in_kgdb[i])) | |
1450 | + cpu_relax(); | |
1451 | + } | |
1452 | + | |
1453 | + /* | |
1454 | + * At this point the primary processor is completely | |
1455 | + * in the debugger and all secondary CPUs are quiescent | |
1456 | + */ | |
1457 | + kgdb_post_primary_code(ks->linux_regs, ks->ex_vector, ks->err_code); | |
1458 | + kgdb_deactivate_sw_breakpoints(); | |
1459 | + kgdb_single_step = 0; | |
1460 | + kgdb_contthread = NULL; | |
1461 | + exception_level = 0; | |
1462 | + | |
1463 | + /* Talk to debugger with gdbserial protocol */ | |
1464 | + error = gdb_serial_stub(ks); | |
1465 | + | |
1466 | + /* Call the I/O driver's post_exception routine */ | |
1467 | + if (kgdb_io_ops->post_exception) | |
1468 | + kgdb_io_ops->post_exception(); | |
1469 | + | |
1470 | + kgdb_info[ks->cpu].debuggerinfo = NULL; | |
1471 | + kgdb_info[ks->cpu].task = NULL; | |
1472 | + atomic_set(&cpu_in_kgdb[ks->cpu], 0); | |
1473 | + | |
1474 | + if (!kgdb_single_step || !kgdb_contthread) { | |
1475 | + for (i = NR_CPUS-1; i >= 0; i--) | |
1476 | + atomic_set(&passive_cpu_wait[i], 0); | |
1477 | + /* | |
1478 | + * Wait till all the CPUs have quit | |
1479 | + * from the debugger. | |
1480 | + */ | |
1481 | + for_each_online_cpu(i) { | |
1482 | + while (atomic_read(&cpu_in_kgdb[i])) | |
1483 | + cpu_relax(); | |
1484 | + } | |
1485 | + } | |
1486 | + | |
1487 | +kgdb_restore: | |
1488 | + /* Free kgdb_active */ | |
1489 | + atomic_set(&kgdb_active, -1); | |
1490 | + local_irq_restore(flags); | |
1491 | + | |
1492 | + return error; | |
1493 | +} | |
1494 | + | |
1495 | +int kgdb_nmicallback(int cpu, void *regs) | |
1496 | +{ | |
1497 | +#ifdef CONFIG_SMP | |
1498 | + if (!atomic_read(&cpu_in_kgdb[cpu]) && | |
1499 | + atomic_read(&kgdb_active) != cpu) { | |
1500 | + kgdb_wait((struct pt_regs *)regs); | |
1501 | + return 0; | |
1502 | + } | |
1503 | +#endif | |
1504 | + return 1; | |
1505 | +} | |
1506 | + | |
1507 | +void kgdb_console_write(struct console *co, const char *s, unsigned count) | |
1508 | +{ | |
1509 | + unsigned long flags; | |
1510 | + | |
1511 | + /* If we're debugging, or KGDB has not connected, don't try | |
1512 | + * and print. */ | |
1513 | + if (!kgdb_connected || atomic_read(&kgdb_active) != -1) | |
1514 | + return; | |
1515 | + | |
1516 | + local_irq_save(flags); | |
1517 | + kgdb_msg_write(s, count); | |
1518 | + local_irq_restore(flags); | |
1519 | +} | |
1520 | + | |
1521 | +static struct console kgdbcons = { | |
1522 | + .name = "kgdb", | |
1523 | + .write = kgdb_console_write, | |
1524 | + .flags = CON_PRINTBUFFER | CON_ENABLED, | |
1525 | + .index = -1, | |
1526 | +}; | |
1527 | + | |
1528 | +#ifdef CONFIG_MAGIC_SYSRQ | |
1529 | +static void sysrq_handle_gdb(int key, struct tty_struct *tty) | |
1530 | +{ | |
1531 | + if (!kgdb_io_ops) { | |
1532 | + printk(KERN_CRIT "ERROR: No KGDB I/O module available\n"); | |
1533 | + return; | |
1534 | + } | |
1535 | + if (!kgdb_connected) | |
1536 | + printk(KERN_CRIT "Entering KGDB\n"); | |
1537 | + | |
1538 | + kgdb_breakpoint(); | |
1539 | +} | |
1540 | + | |
1541 | +static struct sysrq_key_op sysrq_gdb_op = { | |
1542 | + .handler = sysrq_handle_gdb, | |
1543 | + .help_msg = "Gdb", | |
1544 | + .action_msg = "GDB", | |
1545 | +}; | |
1546 | +#endif | |
1547 | + | |
1548 | +static void kgdb_register_callbacks(void) | |
1549 | +{ | |
1550 | + if (!kgdb_io_module_registered) { | |
1551 | + kgdb_io_module_registered = 1; | |
1552 | + kgdb_arch_init(); | |
1553 | +#ifdef CONFIG_MAGIC_SYSRQ | |
1554 | + register_sysrq_key('g', &sysrq_gdb_op); | |
1555 | +#endif | |
1556 | + if (kgdb_use_con && !kgdb_con_registered) { | |
1557 | + register_console(&kgdbcons); | |
1558 | + kgdb_con_registered = 1; | |
1559 | + } | |
1560 | + } | |
1561 | +} | |
1562 | + | |
1563 | +static void kgdb_unregister_callbacks(void) | |
1564 | +{ | |
1565 | + /* | |
1566 | + * When this routine is called KGDB should unregister from the | |
1567 | + * panic handler and clean up, making sure it is not handling any | |
1568 | + * break exceptions at the time. | |
1569 | + */ | |
1570 | + if (kgdb_io_module_registered) { | |
1571 | + kgdb_io_module_registered = 0; | |
1572 | + kgdb_arch_exit(); | |
1573 | +#ifdef CONFIG_MAGIC_SYSRQ | |
1574 | + unregister_sysrq_key('g', &sysrq_gdb_op); | |
1575 | +#endif | |
1576 | + if (kgdb_con_registered) { | |
1577 | + unregister_console(&kgdbcons); | |
1578 | + kgdb_con_registered = 0; | |
1579 | + } | |
1580 | + } | |
1581 | +} | |
1582 | + | |
1583 | +static void kgdb_initial_breakpoint(void) | |
1584 | +{ | |
1585 | + kgdb_break_asap = 0; | |
1586 | + | |
1587 | + printk(KERN_CRIT "kgdb: Waiting for connection from remote gdb...\n"); | |
1588 | + kgdb_breakpoint(); | |
1589 | +} | |
1590 | + | |
1591 | +/** | |
1592 | + * kkgdb_register_io_module - register KGDB IO module | |
1593 | + * @new_kgdb_io_ops: the io ops vector | |
1594 | + * | |
1595 | + * Register it with the KGDB core. | |
1596 | + */ | |
1597 | +int kgdb_register_io_module(struct kgdb_io *new_kgdb_io_ops) | |
1598 | +{ | |
1599 | + int err; | |
1600 | + | |
1601 | + spin_lock(&kgdb_registration_lock); | |
1602 | + | |
1603 | + if (kgdb_io_ops) { | |
1604 | + spin_unlock(&kgdb_registration_lock); | |
1605 | + | |
1606 | + printk(KERN_ERR "kgdb: Another I/O driver is already " | |
1607 | + "registered with KGDB.\n"); | |
1608 | + return -EBUSY; | |
1609 | + } | |
1610 | + | |
1611 | + if (new_kgdb_io_ops->init) { | |
1612 | + err = new_kgdb_io_ops->init(); | |
1613 | + if (err) { | |
1614 | + spin_unlock(&kgdb_registration_lock); | |
1615 | + return err; | |
1616 | + } | |
1617 | + } | |
1618 | + | |
1619 | + kgdb_io_ops = new_kgdb_io_ops; | |
1620 | + | |
1621 | + spin_unlock(&kgdb_registration_lock); | |
1622 | + | |
1623 | + printk(KERN_INFO "kgdb: Registered I/O driver %s.\n", | |
1624 | + new_kgdb_io_ops->name); | |
1625 | + | |
1626 | + /* Arm KGDB now. */ | |
1627 | + kgdb_register_callbacks(); | |
1628 | + | |
1629 | + if (kgdb_break_asap) | |
1630 | + kgdb_initial_breakpoint(); | |
1631 | + | |
1632 | + return 0; | |
1633 | +} | |
1634 | +EXPORT_SYMBOL_GPL(kgdb_register_io_module); | |
1635 | + | |
1636 | +/** | |
1637 | + * kkgdb_unregister_io_module - unregister KGDB IO module | |
1638 | + * @old_kgdb_io_ops: the io ops vector | |
1639 | + * | |
1640 | + * Unregister it with the KGDB core. | |
1641 | + */ | |
1642 | +void kgdb_unregister_io_module(struct kgdb_io *old_kgdb_io_ops) | |
1643 | +{ | |
1644 | + BUG_ON(kgdb_connected); | |
1645 | + | |
1646 | + /* | |
1647 | + * KGDB is no longer able to communicate out, so | |
1648 | + * unregister our callbacks and reset state. | |
1649 | + */ | |
1650 | + kgdb_unregister_callbacks(); | |
1651 | + | |
1652 | + spin_lock(&kgdb_registration_lock); | |
1653 | + | |
1654 | + WARN_ON_ONCE(kgdb_io_ops != old_kgdb_io_ops); | |
1655 | + kgdb_io_ops = NULL; | |
1656 | + | |
1657 | + spin_unlock(&kgdb_registration_lock); | |
1658 | + | |
1659 | + printk(KERN_INFO | |
1660 | + "kgdb: Unregistered I/O driver %s, debugger disabled.\n", | |
1661 | + old_kgdb_io_ops->name); | |
1662 | +} | |
1663 | +EXPORT_SYMBOL_GPL(kgdb_unregister_io_module); | |
1664 | + | |
1665 | +/** | |
1666 | + * kgdb_breakpoint - generate breakpoint exception | |
1667 | + * | |
1668 | + * This function will generate a breakpoint exception. It is used at the | |
1669 | + * beginning of a program to sync up with a debugger and can be used | |
1670 | + * otherwise as a quick means to stop program execution and "break" into | |
1671 | + * the debugger. | |
1672 | + */ | |
1673 | +void kgdb_breakpoint(void) | |
1674 | +{ | |
1675 | + atomic_set(&kgdb_setting_breakpoint, 1); | |
1676 | + wmb(); /* Sync point before breakpoint */ | |
1677 | + arch_kgdb_breakpoint(); | |
1678 | + wmb(); /* Sync point after breakpoint */ | |
1679 | + atomic_set(&kgdb_setting_breakpoint, 0); | |
1680 | +} | |
1681 | +EXPORT_SYMBOL_GPL(kgdb_breakpoint); | |
1682 | + | |
1683 | +static int __init opt_kgdb_wait(char *str) | |
1684 | +{ | |
1685 | + kgdb_break_asap = 1; | |
1686 | + | |
1687 | + if (kgdb_io_module_registered) | |
1688 | + kgdb_initial_breakpoint(); | |
1689 | + | |
1690 | + return 0; | |
1691 | +} | |
1692 | + | |
1693 | +early_param("kgdbwait", opt_kgdb_wait); |
lib/Kconfig.debug
lib/Kconfig.kgdb
1 | + | |
2 | +menuconfig KGDB | |
3 | + bool "KGDB: kernel debugging with remote gdb" | |
4 | + select FRAME_POINTER | |
5 | + depends on HAVE_ARCH_KGDB | |
6 | + depends on DEBUG_KERNEL && EXPERIMENTAL | |
7 | + help | |
8 | + If you say Y here, it will be possible to remotely debug the | |
9 | + kernel using gdb. Documentation of kernel debugger is available | |
10 | + at http://kgdb.sourceforge.net as well as in DocBook form | |
11 | + in Documentation/DocBook/. If unsure, say N. | |
12 | + | |
13 | +config HAVE_ARCH_KGDB_SHADOW_INFO | |
14 | + bool | |
15 | + | |
16 | +config HAVE_ARCH_KGDB | |
17 | + bool | |
18 | + | |
19 | +config KGDB_SERIAL_CONSOLE | |
20 | + tristate "KGDB: use kgdb over the serial console" | |
21 | + depends on KGDB | |
22 | + select CONSOLE_POLL | |
23 | + select MAGIC_SYSRQ | |
24 | + default y | |
25 | + help | |
26 | + Share a serial console with kgdb. Sysrq-g must be used | |
27 | + to break in initially. |