kgdb.h 12 KB

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