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/linkage.h>
17 #include <linux/init.h>
18 #include <linux/atomic.h>
19 #ifdef CONFIG_HAVE_ARCH_KGDB
20 #include <asm/kgdb.h>
21 #endif
22 
23 #ifdef CONFIG_KGDB
24 struct pt_regs;
25 
26 /**
27  *	kgdb_skipexception - (optional) exit kgdb_handle_exception early
28  *	@exception: Exception vector number
29  *	@regs: Current &struct pt_regs.
30  *
31  *	On some architectures it is required to skip a breakpoint
32  *	exception when it occurs after a breakpoint has been removed.
33  *	This can be implemented in the architecture specific portion of kgdb.
34  */
35 extern int kgdb_skipexception(int exception, struct pt_regs *regs);
36 
37 struct tasklet_struct;
38 struct task_struct;
39 struct uart_port;
40 
41 /**
42  *	kgdb_breakpoint - compiled in breakpoint
43  *
44  *	This will be implemented as a static inline per architecture.  This
45  *	function is called by the kgdb core to execute an architecture
46  *	specific trap to cause kgdb to enter the exception processing.
47  *
48  */
49 void kgdb_breakpoint(void);
50 
51 extern int kgdb_connected;
52 extern int kgdb_io_module_registered;
53 
54 extern atomic_t			kgdb_setting_breakpoint;
55 extern atomic_t			kgdb_cpu_doing_single_step;
56 
57 extern struct task_struct	*kgdb_usethread;
58 extern struct task_struct	*kgdb_contthread;
59 
60 enum kgdb_bptype {
61 	BP_BREAKPOINT = 0,
62 	BP_HARDWARE_BREAKPOINT,
63 	BP_WRITE_WATCHPOINT,
64 	BP_READ_WATCHPOINT,
65 	BP_ACCESS_WATCHPOINT,
66 	BP_POKE_BREAKPOINT,
67 };
68 
69 enum kgdb_bpstate {
70 	BP_UNDEFINED = 0,
71 	BP_REMOVED,
72 	BP_SET,
73 	BP_ACTIVE
74 };
75 
76 struct kgdb_bkpt {
77 	unsigned long		bpt_addr;
78 	unsigned char		saved_instr[BREAK_INSTR_SIZE];
79 	enum kgdb_bptype	type;
80 	enum kgdb_bpstate	state;
81 };
82 
83 struct dbg_reg_def_t {
84 	char *name;
85 	int size;
86 	int offset;
87 };
88 
89 #ifndef DBG_MAX_REG_NUM
90 #define DBG_MAX_REG_NUM 0
91 #else
92 extern struct dbg_reg_def_t dbg_reg_def[];
93 extern char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs);
94 extern int dbg_set_reg(int regno, void *mem, struct pt_regs *regs);
95 #endif
96 #ifndef KGDB_MAX_BREAKPOINTS
97 # define KGDB_MAX_BREAKPOINTS	1000
98 #endif
99 
100 #define KGDB_HW_BREAKPOINT	1
101 
102 /*
103  * Functions each KGDB-supporting architecture must provide:
104  */
105 
106 /**
107  *	kgdb_arch_init - Perform any architecture specific initalization.
108  *
109  *	This function will handle the initalization of any architecture
110  *	specific callbacks.
111  */
112 extern int kgdb_arch_init(void);
113 
114 /**
115  *	kgdb_arch_exit - Perform any architecture specific uninitalization.
116  *
117  *	This function will handle the uninitalization of any architecture
118  *	specific callbacks, for dynamic registration and unregistration.
119  */
120 extern void kgdb_arch_exit(void);
121 
122 /**
123  *	pt_regs_to_gdb_regs - Convert ptrace regs to GDB regs
124  *	@gdb_regs: A pointer to hold the registers in the order GDB wants.
125  *	@regs: The &struct pt_regs of the current process.
126  *
127  *	Convert the pt_regs in @regs into the format for registers that
128  *	GDB expects, stored in @gdb_regs.
129  */
130 extern void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs);
131 
132 /**
133  *	sleeping_thread_to_gdb_regs - Convert ptrace regs to GDB regs
134  *	@gdb_regs: A pointer to hold the registers in the order GDB wants.
135  *	@p: The &struct task_struct of the desired process.
136  *
137  *	Convert the register values of the sleeping process in @p to
138  *	the format that GDB expects.
139  *	This function is called when kgdb does not have access to the
140  *	&struct pt_regs and therefore it should fill the gdb registers
141  *	@gdb_regs with what has	been saved in &struct thread_struct
142  *	thread field during switch_to.
143  */
144 extern void
145 sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p);
146 
147 /**
148  *	gdb_regs_to_pt_regs - Convert GDB regs to ptrace regs.
149  *	@gdb_regs: A pointer to hold the registers we've received from GDB.
150  *	@regs: A pointer to a &struct pt_regs to hold these values in.
151  *
152  *	Convert the GDB regs in @gdb_regs into the pt_regs, and store them
153  *	in @regs.
154  */
155 extern void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs);
156 
157 /**
158  *	kgdb_arch_handle_exception - Handle architecture specific GDB packets.
159  *	@vector: The error vector of the exception that happened.
160  *	@signo: The signal number of the exception that happened.
161  *	@err_code: The error code of the exception that happened.
162  *	@remcom_in_buffer: The buffer of the packet we have read.
163  *	@remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
164  *	@regs: The &struct pt_regs of the current process.
165  *
166  *	This function MUST handle the 'c' and 's' command packets,
167  *	as well packets to set / remove a hardware breakpoint, if used.
168  *	If there are additional packets which the hardware needs to handle,
169  *	they are handled here.  The code should return -1 if it wants to
170  *	process more packets, and a %0 or %1 if it wants to exit from the
171  *	kgdb callback.
172  */
173 extern int
174 kgdb_arch_handle_exception(int vector, int signo, int err_code,
175 			   char *remcom_in_buffer,
176 			   char *remcom_out_buffer,
177 			   struct pt_regs *regs);
178 
179 /**
180  *	kgdb_call_nmi_hook - Call kgdb_nmicallback() on the current CPU
181  *	@ignored: This parameter is only here to match the prototype.
182  *
183  *	If you're using the default implementation of kgdb_roundup_cpus()
184  *	this function will be called per CPU.  If you don't implement
185  *	kgdb_call_nmi_hook() a default will be used.
186  */
187 
188 extern void kgdb_call_nmi_hook(void *ignored);
189 
190 /**
191  *	kgdb_roundup_cpus - Get other CPUs into a holding pattern
192  *
193  *	On SMP systems, we need to get the attention of the other CPUs
194  *	and get them into a known state.  This should do what is needed
195  *	to get the other CPUs to call kgdb_wait(). Note that on some arches,
196  *	the NMI approach is not used for rounding up all the CPUs.  Normally
197  *	those architectures can just not implement this and get the default.
198  *
199  *	On non-SMP systems, this is not called.
200  */
201 extern void kgdb_roundup_cpus(void);
202 
203 /**
204  *	kgdb_arch_set_pc - Generic call back to the program counter
205  *	@regs: Current &struct pt_regs.
206  *  @pc: The new value for the program counter
207  *
208  *	This function handles updating the program counter and requires an
209  *	architecture specific implementation.
210  */
211 extern void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc);
212 
213 
214 /* Optional functions. */
215 extern int kgdb_validate_break_address(unsigned long addr);
216 extern int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt);
217 extern int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt);
218 
219 /**
220  *	kgdb_arch_late - Perform any architecture specific initalization.
221  *
222  *	This function will handle the late initalization of any
223  *	architecture specific callbacks.  This is an optional function for
224  *	handling things like late initialization of hw breakpoints.  The
225  *	default implementation does nothing.
226  */
227 extern void kgdb_arch_late(void);
228 
229 
230 /**
231  * struct kgdb_arch - Describe architecture specific values.
232  * @gdb_bpt_instr: The instruction to trigger a breakpoint.
233  * @flags: Flags for the breakpoint, currently just %KGDB_HW_BREAKPOINT.
234  * @set_breakpoint: Allow an architecture to specify how to set a software
235  * breakpoint.
236  * @remove_breakpoint: Allow an architecture to specify how to remove a
237  * software breakpoint.
238  * @set_hw_breakpoint: Allow an architecture to specify how to set a hardware
239  * breakpoint.
240  * @remove_hw_breakpoint: Allow an architecture to specify how to remove a
241  * hardware breakpoint.
242  * @disable_hw_break: Allow an architecture to specify how to disable
243  * hardware breakpoints for a single cpu.
244  * @remove_all_hw_break: Allow an architecture to specify how to remove all
245  * hardware breakpoints.
246  * @correct_hw_break: Allow an architecture to specify how to correct the
247  * hardware debug registers.
248  * @enable_nmi: Manage NMI-triggered entry to KGDB
249  */
250 struct kgdb_arch {
251 	unsigned char		gdb_bpt_instr[BREAK_INSTR_SIZE];
252 	unsigned long		flags;
253 
254 	int	(*set_breakpoint)(unsigned long, char *);
255 	int	(*remove_breakpoint)(unsigned long, char *);
256 	int	(*set_hw_breakpoint)(unsigned long, int, enum kgdb_bptype);
257 	int	(*remove_hw_breakpoint)(unsigned long, int, enum kgdb_bptype);
258 	void	(*disable_hw_break)(struct pt_regs *regs);
259 	void	(*remove_all_hw_break)(void);
260 	void	(*correct_hw_break)(void);
261 
262 	void	(*enable_nmi)(bool on);
263 };
264 
265 /**
266  * struct kgdb_io - Describe the interface for an I/O driver to talk with KGDB.
267  * @name: Name of the I/O driver.
268  * @read_char: Pointer to a function that will return one char.
269  * @write_char: Pointer to a function that will write one char.
270  * @flush: Pointer to a function that will flush any pending writes.
271  * @init: Pointer to a function that will initialize the device.
272  * @pre_exception: Pointer to a function that will do any prep work for
273  * the I/O driver.
274  * @post_exception: Pointer to a function that will do any cleanup work
275  * for the I/O driver.
276  * @is_console: 1 if the end device is a console 0 if the I/O device is
277  * not a console
278  */
279 struct kgdb_io {
280 	const char		*name;
281 	int			(*read_char) (void);
282 	void			(*write_char) (u8);
283 	void			(*flush) (void);
284 	int			(*init) (void);
285 	void			(*pre_exception) (void);
286 	void			(*post_exception) (void);
287 	int			is_console;
288 };
289 
290 extern const struct kgdb_arch		arch_kgdb_ops;
291 
292 extern unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs);
293 
294 #ifdef CONFIG_SERIAL_KGDB_NMI
295 extern int kgdb_register_nmi_console(void);
296 extern int kgdb_unregister_nmi_console(void);
297 extern bool kgdb_nmi_poll_knock(void);
298 #else
kgdb_register_nmi_console(void)299 static inline int kgdb_register_nmi_console(void) { return 0; }
kgdb_unregister_nmi_console(void)300 static inline int kgdb_unregister_nmi_console(void) { return 0; }
kgdb_nmi_poll_knock(void)301 static inline bool kgdb_nmi_poll_knock(void) { return 1; }
302 #endif
303 
304 extern int kgdb_register_io_module(struct kgdb_io *local_kgdb_io_ops);
305 extern void kgdb_unregister_io_module(struct kgdb_io *local_kgdb_io_ops);
306 extern struct kgdb_io *dbg_io_ops;
307 
308 extern int kgdb_hex2long(char **ptr, unsigned long *long_val);
309 extern char *kgdb_mem2hex(char *mem, char *buf, int count);
310 extern int kgdb_hex2mem(char *buf, char *mem, int count);
311 
312 extern int kgdb_isremovedbreak(unsigned long addr);
313 extern void kgdb_schedule_breakpoint(void);
314 
315 extern int
316 kgdb_handle_exception(int ex_vector, int signo, int err_code,
317 		      struct pt_regs *regs);
318 extern int kgdb_nmicallback(int cpu, void *regs);
319 extern int kgdb_nmicallin(int cpu, int trapnr, void *regs, int err_code,
320 			  atomic_t *snd_rdy);
321 extern void gdbstub_exit(int status);
322 
323 extern int			kgdb_single_step;
324 extern atomic_t			kgdb_active;
325 #define in_dbg_master() \
326 	(raw_smp_processor_id() == atomic_read(&kgdb_active))
327 extern bool dbg_is_early;
328 extern void __init dbg_late_init(void);
329 extern void kgdb_panic(const char *msg);
330 #else /* ! CONFIG_KGDB */
331 #define in_dbg_master() (0)
332 #define dbg_late_init()
kgdb_panic(const char * msg)333 static inline void kgdb_panic(const char *msg) {}
334 #endif /* ! CONFIG_KGDB */
335 #endif /* _KGDB_H_ */
336