xref: /linux/include/linux/kgdb.h (revision 38ee6e1fb6f66ca6c82ad638a3c622feeaa488f2)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * This provides the callbacks and functions that KGDB needs to share between
4  * the core, I/O and arch-specific portions.
5  *
6  * Author: Amit Kale <amitkale@linsyssoft.com> and
7  *         Tom Rini <trini@kernel.crashing.org>
8  *
9  * 2001-2004 (c) Amit S. Kale and 2003-2005 (c) MontaVista Software, Inc.
10  */
11 #ifndef _KGDB_H_
12 #define _KGDB_H_
13 
14 #include <linux/linkage.h>
15 #include <linux/init.h>
16 #include <linux/atomic.h>
17 #include <linux/kprobes.h>
18 #ifdef CONFIG_HAVE_ARCH_KGDB
19 #include <asm/kgdb.h>
20 #endif
21 
22 #ifdef CONFIG_KGDB
23 struct pt_regs;
24 
25 /**
26  *	kgdb_skipexception - (optional) exit kgdb_handle_exception early
27  *	@exception: Exception vector number
28  *	@regs: Current &struct pt_regs.
29  *
30  *	On some architectures it is required to skip a breakpoint
31  *	exception when it occurs after a breakpoint has been removed.
32  *	This can be implemented in the architecture specific portion of kgdb.
33  */
34 extern int kgdb_skipexception(int exception, struct pt_regs *regs);
35 
36 struct tasklet_struct;
37 struct task_struct;
38 struct uart_port;
39 
40 /**
41  *	kgdb_breakpoint - compiled in breakpoint
42  *
43  *	This will be implemented as a static inline per architecture.  This
44  *	function is called by the kgdb core to execute an architecture
45  *	specific trap to cause kgdb to enter the exception processing.
46  *
47  */
48 void kgdb_breakpoint(void);
49 
50 extern int kgdb_connected;
51 extern int kgdb_io_module_registered;
52 
53 extern atomic_t			kgdb_setting_breakpoint;
54 extern atomic_t			kgdb_cpu_doing_single_step;
55 
56 extern struct task_struct	*kgdb_usethread;
57 extern struct task_struct	*kgdb_contthread;
58 
59 enum kgdb_bptype {
60 	BP_BREAKPOINT = 0,
61 	BP_HARDWARE_BREAKPOINT,
62 	BP_WRITE_WATCHPOINT,
63 	BP_READ_WATCHPOINT,
64 	BP_ACCESS_WATCHPOINT,
65 	BP_POKE_BREAKPOINT,
66 };
67 
68 enum kgdb_bpstate {
69 	BP_UNDEFINED = 0,
70 	BP_REMOVED,
71 	BP_SET,
72 	BP_ACTIVE
73 };
74 
75 struct kgdb_bkpt {
76 	unsigned long		bpt_addr;
77 	unsigned char		saved_instr[BREAK_INSTR_SIZE];
78 	enum kgdb_bptype	type;
79 	enum kgdb_bpstate	state;
80 };
81 
82 struct dbg_reg_def_t {
83 	char *name;
84 	int size;
85 	int offset;
86 };
87 
88 #ifndef DBG_MAX_REG_NUM
89 #define DBG_MAX_REG_NUM 0
90 #else
91 extern struct dbg_reg_def_t dbg_reg_def[];
92 extern char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs);
93 extern int dbg_set_reg(int regno, void *mem, struct pt_regs *regs);
94 #endif
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 initialization.
107  *
108  *	This function will handle the initialization 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_arch_handle_qxfer_pkt - Handle architecture specific GDB XML
180  *				     packets.
181  *	@remcom_in_buffer: The buffer of the packet we have read.
182  *	@remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
183  */
184 
185 extern void
186 kgdb_arch_handle_qxfer_pkt(char *remcom_in_buffer,
187 			   char *remcom_out_buffer);
188 
189 /**
190  *	kgdb_call_nmi_hook - Call kgdb_nmicallback() on the current CPU
191  *	@ignored: This parameter is only here to match the prototype.
192  *
193  *	If you're using the default implementation of kgdb_roundup_cpus()
194  *	this function will be called per CPU.  If you don't implement
195  *	kgdb_call_nmi_hook() a default will be used.
196  */
197 
198 extern void kgdb_call_nmi_hook(void *ignored);
199 
200 /**
201  *	kgdb_roundup_cpus - Get other CPUs into a holding pattern
202  *
203  *	On SMP systems, we need to get the attention of the other CPUs
204  *	and get them into a known state.  This should do what is needed
205  *	to get the other CPUs to call kgdb_handle_exception().  Note that
206  *	on some arches, the NMI approach is not used for rounding up all
207  *	the CPUs.  Normally those architectures can just not implement
208  *	this and get the default.
209  *
210  *	On non-SMP systems, this is not called.
211  */
212 extern void kgdb_roundup_cpus(void);
213 
214 /**
215  *	kgdb_arch_set_pc - Generic call back to the program counter
216  *	@regs: Current &struct pt_regs.
217  *  @pc: The new value for the program counter
218  *
219  *	This function handles updating the program counter and requires an
220  *	architecture specific implementation.
221  */
222 extern void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc);
223 
224 
225 /* Optional functions. */
226 extern int kgdb_validate_break_address(unsigned long addr);
227 extern int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt);
228 extern int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt);
229 
230 /**
231  *	kgdb_arch_late - Perform any architecture specific initialization.
232  *
233  *	This function will handle the late initialization of any
234  *	architecture specific callbacks.  This is an optional function for
235  *	handling things like late initialization of hw breakpoints.  The
236  *	default implementation does nothing.
237  */
238 extern void kgdb_arch_late(void);
239 
240 
241 /**
242  * struct kgdb_arch - Describe architecture specific values.
243  * @gdb_bpt_instr: The instruction to trigger a breakpoint.
244  * @flags: Flags for the breakpoint, currently just %KGDB_HW_BREAKPOINT.
245  * @set_breakpoint: Allow an architecture to specify how to set a software
246  * breakpoint.
247  * @remove_breakpoint: Allow an architecture to specify how to remove a
248  * software breakpoint.
249  * @set_hw_breakpoint: Allow an architecture to specify how to set a hardware
250  * breakpoint.
251  * @remove_hw_breakpoint: Allow an architecture to specify how to remove a
252  * hardware breakpoint.
253  * @disable_hw_break: Allow an architecture to specify how to disable
254  * hardware breakpoints for a single cpu.
255  * @remove_all_hw_break: Allow an architecture to specify how to remove all
256  * hardware breakpoints.
257  * @correct_hw_break: Allow an architecture to specify how to correct the
258  * hardware debug registers.
259  */
260 struct kgdb_arch {
261 	unsigned char		gdb_bpt_instr[BREAK_INSTR_SIZE];
262 	unsigned long		flags;
263 
264 	int	(*set_breakpoint)(unsigned long, char *);
265 	int	(*remove_breakpoint)(unsigned long, char *);
266 	int	(*set_hw_breakpoint)(unsigned long, int, enum kgdb_bptype);
267 	int	(*remove_hw_breakpoint)(unsigned long, int, enum kgdb_bptype);
268 	void	(*disable_hw_break)(struct pt_regs *regs);
269 	void	(*remove_all_hw_break)(void);
270 	void	(*correct_hw_break)(void);
271 };
272 
273 /**
274  * struct kgdb_io - Describe the interface for an I/O driver to talk with KGDB.
275  * @name: Name of the I/O driver.
276  * @read_char: Pointer to a function that will return one char.
277  * @write_char: Pointer to a function that will write one char.
278  * @flush: Pointer to a function that will flush any pending writes.
279  * @init: Pointer to a function that will initialize the device.
280  * @deinit: Pointer to a function that will deinit the device. Implies that
281  * this I/O driver is temporary and expects to be replaced. Called when
282  * an I/O driver is replaced or explicitly unregistered.
283  * @pre_exception: Pointer to a function that will do any prep work for
284  * the I/O driver.
285  * @post_exception: Pointer to a function that will do any cleanup work
286  * for the I/O driver.
287  * @cons: valid if the I/O device is a console; else NULL.
288  */
289 struct kgdb_io {
290 	const char		*name;
291 	int			(*read_char) (void);
292 	void			(*write_char) (u8);
293 	void			(*flush) (void);
294 	int			(*init) (void);
295 	void			(*deinit) (void);
296 	void			(*pre_exception) (void);
297 	void			(*post_exception) (void);
298 	struct console		*cons;
299 };
300 
301 extern const struct kgdb_arch		arch_kgdb_ops;
302 
303 extern unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs);
304 
305 extern int kgdb_register_io_module(struct kgdb_io *local_kgdb_io_ops);
306 extern void kgdb_unregister_io_module(struct kgdb_io *local_kgdb_io_ops);
307 extern struct kgdb_io *dbg_io_ops;
308 
309 extern int kgdb_hex2long(char **ptr, unsigned long *long_val);
310 extern char *kgdb_mem2hex(char *mem, char *buf, int count);
311 extern int kgdb_hex2mem(char *buf, char *mem, int count);
312 
313 extern int kgdb_isremovedbreak(unsigned long addr);
314 extern int kgdb_has_hit_break(unsigned long addr);
315 
316 extern int
317 kgdb_handle_exception(int ex_vector, int signo, int err_code,
318 		      struct pt_regs *regs);
319 extern int kgdb_nmicallback(int cpu, void *regs);
320 extern int kgdb_nmicallin(int cpu, int trapnr, void *regs, int err_code,
321 			  atomic_t *snd_rdy);
322 extern void gdbstub_exit(int status);
323 
324 /*
325  * kgdb and kprobes both use the same (kprobe) blocklist (which makes sense
326  * given they are both typically hooked up to the same trap meaning on most
327  * architectures one cannot be used to debug the other)
328  *
329  * However on architectures where kprobes is not (yet) implemented we permit
330  * breakpoints everywhere rather than blocking everything by default.
331  */
kgdb_within_blocklist(unsigned long addr)332 static inline bool kgdb_within_blocklist(unsigned long addr)
333 {
334 #ifdef CONFIG_KGDB_HONOUR_BLOCKLIST
335 	return within_kprobe_blacklist(addr);
336 #else
337 	return false;
338 #endif
339 }
340 
341 extern int			kgdb_single_step;
342 extern atomic_t			kgdb_active;
343 #define in_dbg_master() \
344 	(irqs_disabled() && (smp_processor_id() == atomic_read(&kgdb_active)))
345 extern bool dbg_is_early;
346 extern void __init dbg_late_init(void);
347 extern void kgdb_panic(const char *msg);
348 extern void kgdb_free_init_mem(void);
349 #else /* ! CONFIG_KGDB */
350 #define in_dbg_master() (0)
351 #define dbg_late_init()
kgdb_panic(const char * msg)352 static inline void kgdb_panic(const char *msg) {}
kgdb_free_init_mem(void)353 static inline void kgdb_free_init_mem(void) { }
kgdb_nmicallback(int cpu,void * regs)354 static inline int kgdb_nmicallback(int cpu, void *regs) { return 1; }
355 #endif /* ! CONFIG_KGDB */
356 #endif /* _KGDB_H_ */
357