1 #ifndef _KDB_H
2 #define _KDB_H
3
4 /*
5 * Kernel Debugger Architecture Independent Global Headers
6 *
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive
9 * for more details.
10 *
11 * Copyright (c) 2000-2007 Silicon Graphics, Inc. All Rights Reserved.
12 * Copyright (C) 2000 Stephane Eranian <eranian@hpl.hp.com>
13 * Copyright (C) 2009 Jason Wessel <jason.wessel@windriver.com>
14 */
15
16 #include <linux/list.h>
17 #include <linux/smp.h>
18
19 /* Shifted versions of the command enable bits are be used if the command
20 * has no arguments (see kdb_check_flags). This allows commands, such as
21 * go, to have different permissions depending upon whether it is called
22 * with an argument.
23 */
24 #define KDB_ENABLE_NO_ARGS_SHIFT 10
25
26 typedef enum {
27 KDB_ENABLE_ALL = (1 << 0), /* Enable everything */
28 KDB_ENABLE_MEM_READ = (1 << 1),
29 KDB_ENABLE_MEM_WRITE = (1 << 2),
30 KDB_ENABLE_REG_READ = (1 << 3),
31 KDB_ENABLE_REG_WRITE = (1 << 4),
32 KDB_ENABLE_INSPECT = (1 << 5),
33 KDB_ENABLE_FLOW_CTRL = (1 << 6),
34 KDB_ENABLE_SIGNAL = (1 << 7),
35 KDB_ENABLE_REBOOT = (1 << 8),
36 /* User exposed values stop here, all remaining flags are
37 * exclusively used to describe a commands behaviour.
38 */
39
40 KDB_ENABLE_ALWAYS_SAFE = (1 << 9),
41 KDB_ENABLE_MASK = (1 << KDB_ENABLE_NO_ARGS_SHIFT) - 1,
42
43 KDB_ENABLE_ALL_NO_ARGS = KDB_ENABLE_ALL << KDB_ENABLE_NO_ARGS_SHIFT,
44 KDB_ENABLE_MEM_READ_NO_ARGS = KDB_ENABLE_MEM_READ
45 << KDB_ENABLE_NO_ARGS_SHIFT,
46 KDB_ENABLE_MEM_WRITE_NO_ARGS = KDB_ENABLE_MEM_WRITE
47 << KDB_ENABLE_NO_ARGS_SHIFT,
48 KDB_ENABLE_REG_READ_NO_ARGS = KDB_ENABLE_REG_READ
49 << KDB_ENABLE_NO_ARGS_SHIFT,
50 KDB_ENABLE_REG_WRITE_NO_ARGS = KDB_ENABLE_REG_WRITE
51 << KDB_ENABLE_NO_ARGS_SHIFT,
52 KDB_ENABLE_INSPECT_NO_ARGS = KDB_ENABLE_INSPECT
53 << KDB_ENABLE_NO_ARGS_SHIFT,
54 KDB_ENABLE_FLOW_CTRL_NO_ARGS = KDB_ENABLE_FLOW_CTRL
55 << KDB_ENABLE_NO_ARGS_SHIFT,
56 KDB_ENABLE_SIGNAL_NO_ARGS = KDB_ENABLE_SIGNAL
57 << KDB_ENABLE_NO_ARGS_SHIFT,
58 KDB_ENABLE_REBOOT_NO_ARGS = KDB_ENABLE_REBOOT
59 << KDB_ENABLE_NO_ARGS_SHIFT,
60 KDB_ENABLE_ALWAYS_SAFE_NO_ARGS = KDB_ENABLE_ALWAYS_SAFE
61 << KDB_ENABLE_NO_ARGS_SHIFT,
62 KDB_ENABLE_MASK_NO_ARGS = KDB_ENABLE_MASK << KDB_ENABLE_NO_ARGS_SHIFT,
63
64 KDB_REPEAT_NO_ARGS = 0x40000000, /* Repeat the command w/o arguments */
65 KDB_REPEAT_WITH_ARGS = 0x80000000, /* Repeat the command with args */
66 } kdb_cmdflags_t;
67
68 typedef int (*kdb_func_t)(int, const char **);
69
70 /* The KDB shell command table */
71 typedef struct _kdbtab {
72 char *name; /* Command name */
73 kdb_func_t func; /* Function to execute command */
74 char *usage; /* Usage String for this command */
75 char *help; /* Help message for this command */
76 short minlen; /* Minimum legal # cmd chars required */
77 kdb_cmdflags_t flags; /* Command behaviour flags */
78 struct list_head list_node; /* Command list */
79 } kdbtab_t;
80
81 #ifdef CONFIG_KGDB_KDB
82 #include <linux/init.h>
83 #include <linux/sched.h>
84 #include <linux/atomic.h>
85
86 #define KDB_POLL_FUNC_MAX 5
87 extern int kdb_poll_idx;
88
89 /*
90 * kdb_initial_cpu is initialized to -1, and is set to the cpu
91 * number whenever the kernel debugger is entered.
92 */
93 extern int kdb_initial_cpu;
94
95 /* Types and messages used for dynamically added kdb shell commands */
96
97 #define KDB_MAXARGS 16 /* Maximum number of arguments to a function */
98
99 /* KDB return codes from a command or internal kdb function */
100 #define KDB_NOTFOUND (-1)
101 #define KDB_ARGCOUNT (-2)
102 #define KDB_BADWIDTH (-3)
103 #define KDB_BADRADIX (-4)
104 #define KDB_NOTENV (-5)
105 #define KDB_NOENVVALUE (-6)
106 #define KDB_NOTIMP (-7)
107 #define KDB_ENVFULL (-8)
108 #define KDB_KMALLOCFAILED (-9)
109 #define KDB_TOOMANYBPT (-10)
110 #define KDB_TOOMANYDBREGS (-11)
111 #define KDB_DUPBPT (-12)
112 #define KDB_BPTNOTFOUND (-13)
113 #define KDB_BADMODE (-14)
114 #define KDB_BADINT (-15)
115 #define KDB_INVADDRFMT (-16)
116 #define KDB_BADREG (-17)
117 #define KDB_BADCPUNUM (-18)
118 #define KDB_BADLENGTH (-19)
119 #define KDB_NOBP (-20)
120 #define KDB_BADADDR (-21)
121 #define KDB_NOPERM (-22)
122
123 /*
124 * kdb_diemsg
125 *
126 * Contains a pointer to the last string supplied to the
127 * kernel 'die' panic function.
128 */
129 extern const char *kdb_diemsg;
130
131 #define KDB_FLAG_EARLYKDB (1 << 0) /* set from boot parameter kdb=early */
132 #define KDB_FLAG_CATASTROPHIC (1 << 1) /* A catastrophic event has occurred */
133 #define KDB_FLAG_CMD_INTERRUPT (1 << 2) /* Previous command was interrupted */
134 #define KDB_FLAG_NOIPI (1 << 3) /* Do not send IPIs */
135 #define KDB_FLAG_NO_CONSOLE (1 << 5) /* No console is available,
136 * kdb is disabled */
137 #define KDB_FLAG_NO_VT_CONSOLE (1 << 6) /* No VT console is available, do
138 * not use keyboard */
139 #define KDB_FLAG_NO_I8042 (1 << 7) /* No i8042 chip is available, do
140 * not use keyboard */
141
142 extern unsigned int kdb_flags; /* Global flags, see kdb_state for per cpu state */
143
144 #define KDB_FLAG(flag) (kdb_flags & KDB_FLAG_##flag)
145 #define KDB_FLAG_SET(flag) ((void)(kdb_flags |= KDB_FLAG_##flag))
146 #define KDB_FLAG_CLEAR(flag) ((void)(kdb_flags &= ~KDB_FLAG_##flag))
147
148 /*
149 * External entry point for the kernel debugger. The pt_regs
150 * at the time of entry are supplied along with the reason for
151 * entry to the kernel debugger.
152 */
153
154 typedef enum {
155 KDB_REASON_ENTER = 1, /* KDB_ENTER() trap/fault - regs valid */
156 KDB_REASON_ENTER_SLAVE, /* KDB_ENTER_SLAVE() trap/fault - regs valid */
157 KDB_REASON_BREAK, /* Breakpoint inst. - regs valid */
158 KDB_REASON_DEBUG, /* Debug Fault - regs valid */
159 KDB_REASON_OOPS, /* Kernel Oops - regs valid */
160 KDB_REASON_SWITCH, /* CPU switch - regs valid*/
161 KDB_REASON_KEYBOARD, /* Keyboard entry - regs valid */
162 KDB_REASON_NMI, /* Non-maskable interrupt; regs valid */
163 KDB_REASON_RECURSE, /* Recursive entry to kdb;
164 * regs probably valid */
165 KDB_REASON_SSTEP, /* Single Step trap. - regs valid */
166 KDB_REASON_SYSTEM_NMI, /* In NMI due to SYSTEM cmd; regs valid */
167 } kdb_reason_t;
168
169 enum kdb_msgsrc {
170 KDB_MSGSRC_INTERNAL, /* direct call to kdb_printf() */
171 KDB_MSGSRC_PRINTK, /* trapped from printk() */
172 };
173
174 extern int kdb_trap_printk;
175 extern int kdb_printf_cpu;
176 extern __printf(2, 0) int vkdb_printf(enum kdb_msgsrc src, const char *fmt,
177 va_list args);
178 extern __printf(1, 2) int kdb_printf(const char *, ...);
179 typedef __printf(1, 2) int (*kdb_printf_t)(const char *, ...);
180
181 extern void kdb_init(int level);
182
183 /* Access to kdb specific polling devices */
184 typedef int (*get_char_func)(void);
185 extern get_char_func kdb_poll_funcs[];
186 extern int kdb_get_kbd_char(void);
187
188 static inline
kdb_process_cpu(const struct task_struct * p)189 int kdb_process_cpu(const struct task_struct *p)
190 {
191 unsigned int cpu = task_cpu(p);
192 if (cpu > num_possible_cpus())
193 cpu = 0;
194 return cpu;
195 }
196
197 extern void kdb_send_sig(struct task_struct *p, int sig);
198
199 #ifdef CONFIG_KALLSYMS
200 extern const char *kdb_walk_kallsyms(loff_t *pos);
201 #else /* ! CONFIG_KALLSYMS */
kdb_walk_kallsyms(loff_t * pos)202 static inline const char *kdb_walk_kallsyms(loff_t *pos)
203 {
204 return NULL;
205 }
206 #endif /* ! CONFIG_KALLSYMS */
207
208 /* Dynamic kdb shell command registration */
209 extern int kdb_register(kdbtab_t *cmd);
210 extern void kdb_unregister(kdbtab_t *cmd);
211
212 /* Return true when KDB as locked for printing a message on this CPU. */
213 static inline
kdb_printf_on_this_cpu(void)214 bool kdb_printf_on_this_cpu(void)
215 {
216 /*
217 * We can use raw_smp_processor_id() here because the task could
218 * not get migrated when KDB has locked for printing on this CPU.
219 */
220 return unlikely(READ_ONCE(kdb_printf_cpu) == raw_smp_processor_id());
221 }
222
223 #else /* ! CONFIG_KGDB_KDB */
kdb_printf(const char * fmt,...)224 static inline __printf(1, 2) int kdb_printf(const char *fmt, ...) { return 0; }
kdb_init(int level)225 static inline void kdb_init(int level) {}
kdb_register(kdbtab_t * cmd)226 static inline int kdb_register(kdbtab_t *cmd) { return 0; }
kdb_unregister(kdbtab_t * cmd)227 static inline void kdb_unregister(kdbtab_t *cmd) {}
228
kdb_printf_on_this_cpu(void)229 static inline bool kdb_printf_on_this_cpu(void) { return false; }
230
231 #endif /* CONFIG_KGDB_KDB */
232 enum {
233 KDB_NOT_INITIALIZED,
234 KDB_INIT_EARLY,
235 KDB_INIT_FULL,
236 };
237
238 extern int kdbgetintenv(const char *, int *);
239 extern int kdb_set(int, const char **);
240 int kdb_lsmod(int argc, const char **argv);
241
242 #endif /* !_KDB_H */
243