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_wait(). Note that on some arches,
206 * the NMI approach is not used for rounding up all the CPUs. Normally
207 * those architectures can just not implement this and get the default.
208 *
209 * On non-SMP systems, this is not called.
210 */
211 extern void kgdb_roundup_cpus(void);
212
213 /**
214 * kgdb_arch_set_pc - Generic call back to the program counter
215 * @regs: Current &struct pt_regs.
216 * @pc: The new value for the program counter
217 *
218 * This function handles updating the program counter and requires an
219 * architecture specific implementation.
220 */
221 extern void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc);
222
223
224 /* Optional functions. */
225 extern int kgdb_validate_break_address(unsigned long addr);
226 extern int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt);
227 extern int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt);
228
229 /**
230 * kgdb_arch_late - Perform any architecture specific initialization.
231 *
232 * This function will handle the late initialization of any
233 * architecture specific callbacks. This is an optional function for
234 * handling things like late initialization of hw breakpoints. The
235 * default implementation does nothing.
236 */
237 extern void kgdb_arch_late(void);
238
239
240 /**
241 * struct kgdb_arch - Describe architecture specific values.
242 * @gdb_bpt_instr: The instruction to trigger a breakpoint.
243 * @flags: Flags for the breakpoint, currently just %KGDB_HW_BREAKPOINT.
244 * @set_breakpoint: Allow an architecture to specify how to set a software
245 * breakpoint.
246 * @remove_breakpoint: Allow an architecture to specify how to remove a
247 * software breakpoint.
248 * @set_hw_breakpoint: Allow an architecture to specify how to set a hardware
249 * breakpoint.
250 * @remove_hw_breakpoint: Allow an architecture to specify how to remove a
251 * hardware breakpoint.
252 * @disable_hw_break: Allow an architecture to specify how to disable
253 * hardware breakpoints for a single cpu.
254 * @remove_all_hw_break: Allow an architecture to specify how to remove all
255 * hardware breakpoints.
256 * @correct_hw_break: Allow an architecture to specify how to correct the
257 * hardware debug registers.
258 */
259 struct kgdb_arch {
260 unsigned char gdb_bpt_instr[BREAK_INSTR_SIZE];
261 unsigned long flags;
262
263 int (*set_breakpoint)(unsigned long, char *);
264 int (*remove_breakpoint)(unsigned long, char *);
265 int (*set_hw_breakpoint)(unsigned long, int, enum kgdb_bptype);
266 int (*remove_hw_breakpoint)(unsigned long, int, enum kgdb_bptype);
267 void (*disable_hw_break)(struct pt_regs *regs);
268 void (*remove_all_hw_break)(void);
269 void (*correct_hw_break)(void);
270 };
271
272 /**
273 * struct kgdb_io - Describe the interface for an I/O driver to talk with KGDB.
274 * @name: Name of the I/O driver.
275 * @read_char: Pointer to a function that will return one char.
276 * @write_char: Pointer to a function that will write one char.
277 * @flush: Pointer to a function that will flush any pending writes.
278 * @init: Pointer to a function that will initialize the device.
279 * @deinit: Pointer to a function that will deinit the device. Implies that
280 * this I/O driver is temporary and expects to be replaced. Called when
281 * an I/O driver is replaced or explicitly unregistered.
282 * @pre_exception: Pointer to a function that will do any prep work for
283 * the I/O driver.
284 * @post_exception: Pointer to a function that will do any cleanup work
285 * for the I/O driver.
286 * @cons: valid if the I/O device is a console; else NULL.
287 */
288 struct kgdb_io {
289 const char *name;
290 int (*read_char) (void);
291 void (*write_char) (u8);
292 void (*flush) (void);
293 int (*init) (void);
294 void (*deinit) (void);
295 void (*pre_exception) (void);
296 void (*post_exception) (void);
297 struct console *cons;
298 };
299
300 extern const struct kgdb_arch arch_kgdb_ops;
301
302 extern unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs);
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 int kgdb_has_hit_break(unsigned long addr);
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 /*
324 * kgdb and kprobes both use the same (kprobe) blocklist (which makes sense
325 * given they are both typically hooked up to the same trap meaning on most
326 * architectures one cannot be used to debug the other)
327 *
328 * However on architectures where kprobes is not (yet) implemented we permit
329 * breakpoints everywhere rather than blocking everything by default.
330 */
kgdb_within_blocklist(unsigned long addr)331 static inline bool kgdb_within_blocklist(unsigned long addr)
332 {
333 #ifdef CONFIG_KGDB_HONOUR_BLOCKLIST
334 return within_kprobe_blacklist(addr);
335 #else
336 return false;
337 #endif
338 }
339
340 extern int kgdb_single_step;
341 extern atomic_t kgdb_active;
342 #define in_dbg_master() \
343 (irqs_disabled() && (smp_processor_id() == atomic_read(&kgdb_active)))
344 extern bool dbg_is_early;
345 extern void __init dbg_late_init(void);
346 extern void kgdb_panic(const char *msg);
347 extern void kgdb_free_init_mem(void);
348 #else /* ! CONFIG_KGDB */
349 #define in_dbg_master() (0)
350 #define dbg_late_init()
kgdb_panic(const char * msg)351 static inline void kgdb_panic(const char *msg) {}
kgdb_free_init_mem(void)352 static inline void kgdb_free_init_mem(void) { }
kgdb_nmicallback(int cpu,void * regs)353 static inline int kgdb_nmicallback(int cpu, void *regs) { return 1; }
354 #endif /* ! CONFIG_KGDB */
355 #endif /* _KGDB_H_ */
356