This source file includes following definitions.
- dbg_get_reg
- dbg_set_reg
- sleeping_thread_to_gdb_regs
- kgdb_arch_set_pc
- kgdb_arch_update_addr
- kgdb_arch_handle_exception
- kgdb_brk_fn
- NOKPROBE_SYMBOL
- kgdb_step_brk_fn
- __kgdb_notify
- kgdb_notify
- kgdb_arch_init
- kgdb_arch_exit
- kgdb_arch_set_breakpoint
- kgdb_arch_remove_breakpoint
1
2
3
4
5
6
7
8
9
10
11 #include <linux/bug.h>
12 #include <linux/irq.h>
13 #include <linux/kdebug.h>
14 #include <linux/kgdb.h>
15 #include <linux/kprobes.h>
16 #include <linux/sched/task_stack.h>
17
18 #include <asm/debug-monitors.h>
19 #include <asm/insn.h>
20 #include <asm/traps.h>
21
22 struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = {
23 { "x0", 8, offsetof(struct pt_regs, regs[0])},
24 { "x1", 8, offsetof(struct pt_regs, regs[1])},
25 { "x2", 8, offsetof(struct pt_regs, regs[2])},
26 { "x3", 8, offsetof(struct pt_regs, regs[3])},
27 { "x4", 8, offsetof(struct pt_regs, regs[4])},
28 { "x5", 8, offsetof(struct pt_regs, regs[5])},
29 { "x6", 8, offsetof(struct pt_regs, regs[6])},
30 { "x7", 8, offsetof(struct pt_regs, regs[7])},
31 { "x8", 8, offsetof(struct pt_regs, regs[8])},
32 { "x9", 8, offsetof(struct pt_regs, regs[9])},
33 { "x10", 8, offsetof(struct pt_regs, regs[10])},
34 { "x11", 8, offsetof(struct pt_regs, regs[11])},
35 { "x12", 8, offsetof(struct pt_regs, regs[12])},
36 { "x13", 8, offsetof(struct pt_regs, regs[13])},
37 { "x14", 8, offsetof(struct pt_regs, regs[14])},
38 { "x15", 8, offsetof(struct pt_regs, regs[15])},
39 { "x16", 8, offsetof(struct pt_regs, regs[16])},
40 { "x17", 8, offsetof(struct pt_regs, regs[17])},
41 { "x18", 8, offsetof(struct pt_regs, regs[18])},
42 { "x19", 8, offsetof(struct pt_regs, regs[19])},
43 { "x20", 8, offsetof(struct pt_regs, regs[20])},
44 { "x21", 8, offsetof(struct pt_regs, regs[21])},
45 { "x22", 8, offsetof(struct pt_regs, regs[22])},
46 { "x23", 8, offsetof(struct pt_regs, regs[23])},
47 { "x24", 8, offsetof(struct pt_regs, regs[24])},
48 { "x25", 8, offsetof(struct pt_regs, regs[25])},
49 { "x26", 8, offsetof(struct pt_regs, regs[26])},
50 { "x27", 8, offsetof(struct pt_regs, regs[27])},
51 { "x28", 8, offsetof(struct pt_regs, regs[28])},
52 { "x29", 8, offsetof(struct pt_regs, regs[29])},
53 { "x30", 8, offsetof(struct pt_regs, regs[30])},
54 { "sp", 8, offsetof(struct pt_regs, sp)},
55 { "pc", 8, offsetof(struct pt_regs, pc)},
56
57
58
59
60
61
62 { "pstate", 4, offsetof(struct pt_regs, pstate)
63 #ifdef CONFIG_CPU_BIG_ENDIAN
64 + 4
65 #endif
66 },
67 { "v0", 16, -1 },
68 { "v1", 16, -1 },
69 { "v2", 16, -1 },
70 { "v3", 16, -1 },
71 { "v4", 16, -1 },
72 { "v5", 16, -1 },
73 { "v6", 16, -1 },
74 { "v7", 16, -1 },
75 { "v8", 16, -1 },
76 { "v9", 16, -1 },
77 { "v10", 16, -1 },
78 { "v11", 16, -1 },
79 { "v12", 16, -1 },
80 { "v13", 16, -1 },
81 { "v14", 16, -1 },
82 { "v15", 16, -1 },
83 { "v16", 16, -1 },
84 { "v17", 16, -1 },
85 { "v18", 16, -1 },
86 { "v19", 16, -1 },
87 { "v20", 16, -1 },
88 { "v21", 16, -1 },
89 { "v22", 16, -1 },
90 { "v23", 16, -1 },
91 { "v24", 16, -1 },
92 { "v25", 16, -1 },
93 { "v26", 16, -1 },
94 { "v27", 16, -1 },
95 { "v28", 16, -1 },
96 { "v29", 16, -1 },
97 { "v30", 16, -1 },
98 { "v31", 16, -1 },
99 { "fpsr", 4, -1 },
100 { "fpcr", 4, -1 },
101 };
102
103 char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
104 {
105 if (regno >= DBG_MAX_REG_NUM || regno < 0)
106 return NULL;
107
108 if (dbg_reg_def[regno].offset != -1)
109 memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
110 dbg_reg_def[regno].size);
111 else
112 memset(mem, 0, dbg_reg_def[regno].size);
113 return dbg_reg_def[regno].name;
114 }
115
116 int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
117 {
118 if (regno >= DBG_MAX_REG_NUM || regno < 0)
119 return -EINVAL;
120
121 if (dbg_reg_def[regno].offset != -1)
122 memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
123 dbg_reg_def[regno].size);
124 return 0;
125 }
126
127 void
128 sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
129 {
130 struct cpu_context *cpu_context = &task->thread.cpu_context;
131
132
133 memset((char *)gdb_regs, 0, NUMREGBYTES);
134
135 gdb_regs[19] = cpu_context->x19;
136 gdb_regs[20] = cpu_context->x20;
137 gdb_regs[21] = cpu_context->x21;
138 gdb_regs[22] = cpu_context->x22;
139 gdb_regs[23] = cpu_context->x23;
140 gdb_regs[24] = cpu_context->x24;
141 gdb_regs[25] = cpu_context->x25;
142 gdb_regs[26] = cpu_context->x26;
143 gdb_regs[27] = cpu_context->x27;
144 gdb_regs[28] = cpu_context->x28;
145 gdb_regs[29] = cpu_context->fp;
146
147 gdb_regs[31] = cpu_context->sp;
148 gdb_regs[32] = cpu_context->pc;
149 }
150
151 void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
152 {
153 regs->pc = pc;
154 }
155
156 static int compiled_break;
157
158 static void kgdb_arch_update_addr(struct pt_regs *regs,
159 char *remcom_in_buffer)
160 {
161 unsigned long addr;
162 char *ptr;
163
164 ptr = &remcom_in_buffer[1];
165 if (kgdb_hex2long(&ptr, &addr))
166 kgdb_arch_set_pc(regs, addr);
167 else if (compiled_break == 1)
168 kgdb_arch_set_pc(regs, regs->pc + 4);
169
170 compiled_break = 0;
171 }
172
173 int kgdb_arch_handle_exception(int exception_vector, int signo,
174 int err_code, char *remcom_in_buffer,
175 char *remcom_out_buffer,
176 struct pt_regs *linux_regs)
177 {
178 int err;
179
180 switch (remcom_in_buffer[0]) {
181 case 'D':
182 case 'k':
183
184
185
186
187 case 'c':
188
189
190
191
192
193
194
195
196 kgdb_arch_update_addr(linux_regs, remcom_in_buffer);
197 atomic_set(&kgdb_cpu_doing_single_step, -1);
198 kgdb_single_step = 0;
199
200
201
202
203 if (kernel_active_single_step())
204 kernel_disable_single_step();
205
206 err = 0;
207 break;
208 case 's':
209
210
211
212
213
214
215
216
217 kgdb_arch_update_addr(linux_regs, remcom_in_buffer);
218 atomic_set(&kgdb_cpu_doing_single_step, raw_smp_processor_id());
219 kgdb_single_step = 1;
220
221
222
223
224 if (!kernel_active_single_step())
225 kernel_enable_single_step(linux_regs);
226 err = 0;
227 break;
228 default:
229 err = -1;
230 }
231 return err;
232 }
233
234 static int kgdb_brk_fn(struct pt_regs *regs, unsigned int esr)
235 {
236 kgdb_handle_exception(1, SIGTRAP, 0, regs);
237 return DBG_HOOK_HANDLED;
238 }
239 NOKPROBE_SYMBOL(kgdb_brk_fn)
240
241 static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int esr)
242 {
243 compiled_break = 1;
244 kgdb_handle_exception(1, SIGTRAP, 0, regs);
245
246 return DBG_HOOK_HANDLED;
247 }
248 NOKPROBE_SYMBOL(kgdb_compiled_brk_fn);
249
250 static int kgdb_step_brk_fn(struct pt_regs *regs, unsigned int esr)
251 {
252 if (!kgdb_single_step)
253 return DBG_HOOK_ERROR;
254
255 kgdb_handle_exception(1, SIGTRAP, 0, regs);
256 return DBG_HOOK_HANDLED;
257 }
258 NOKPROBE_SYMBOL(kgdb_step_brk_fn);
259
260 static struct break_hook kgdb_brkpt_hook = {
261 .fn = kgdb_brk_fn,
262 .imm = KGDB_DYN_DBG_BRK_IMM,
263 };
264
265 static struct break_hook kgdb_compiled_brkpt_hook = {
266 .fn = kgdb_compiled_brk_fn,
267 .imm = KGDB_COMPILED_DBG_BRK_IMM,
268 };
269
270 static struct step_hook kgdb_step_hook = {
271 .fn = kgdb_step_brk_fn
272 };
273
274 static int __kgdb_notify(struct die_args *args, unsigned long cmd)
275 {
276 struct pt_regs *regs = args->regs;
277
278 if (kgdb_handle_exception(1, args->signr, cmd, regs))
279 return NOTIFY_DONE;
280 return NOTIFY_STOP;
281 }
282
283 static int
284 kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
285 {
286 unsigned long flags;
287 int ret;
288
289 local_irq_save(flags);
290 ret = __kgdb_notify(ptr, cmd);
291 local_irq_restore(flags);
292
293 return ret;
294 }
295
296 static struct notifier_block kgdb_notifier = {
297 .notifier_call = kgdb_notify,
298
299
300
301 .priority = -INT_MAX,
302 };
303
304
305
306
307
308
309 int kgdb_arch_init(void)
310 {
311 int ret = register_die_notifier(&kgdb_notifier);
312
313 if (ret != 0)
314 return ret;
315
316 register_kernel_break_hook(&kgdb_brkpt_hook);
317 register_kernel_break_hook(&kgdb_compiled_brkpt_hook);
318 register_kernel_step_hook(&kgdb_step_hook);
319 return 0;
320 }
321
322
323
324
325
326
327 void kgdb_arch_exit(void)
328 {
329 unregister_kernel_break_hook(&kgdb_brkpt_hook);
330 unregister_kernel_break_hook(&kgdb_compiled_brkpt_hook);
331 unregister_kernel_step_hook(&kgdb_step_hook);
332 unregister_die_notifier(&kgdb_notifier);
333 }
334
335 const struct kgdb_arch arch_kgdb_ops;
336
337 int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
338 {
339 int err;
340
341 BUILD_BUG_ON(AARCH64_INSN_SIZE != BREAK_INSTR_SIZE);
342
343 err = aarch64_insn_read((void *)bpt->bpt_addr, (u32 *)bpt->saved_instr);
344 if (err)
345 return err;
346
347 return aarch64_insn_write((void *)bpt->bpt_addr,
348 (u32)AARCH64_BREAK_KGDB_DYN_DBG);
349 }
350
351 int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
352 {
353 return aarch64_insn_write((void *)bpt->bpt_addr,
354 *(u32 *)bpt->saved_instr);
355 }