Loading...
1/*
2 * arch/arm/kernel/kgdb.c
3 *
4 * ARM KGDB support
5 *
6 * Copyright (c) 2002-2004 MontaVista Software, Inc
7 * Copyright (c) 2008 Wind River Systems, Inc.
8 *
9 * Authors: George Davis <davis_g@mvista.com>
10 * Deepak Saxena <dsaxena@plexity.net>
11 */
12#include <linux/irq.h>
13#include <linux/kdebug.h>
14#include <linux/kgdb.h>
15#include <asm/traps.h>
16
17struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
18{
19 { "r0", 4, offsetof(struct pt_regs, ARM_r0)},
20 { "r1", 4, offsetof(struct pt_regs, ARM_r1)},
21 { "r2", 4, offsetof(struct pt_regs, ARM_r2)},
22 { "r3", 4, offsetof(struct pt_regs, ARM_r3)},
23 { "r4", 4, offsetof(struct pt_regs, ARM_r4)},
24 { "r5", 4, offsetof(struct pt_regs, ARM_r5)},
25 { "r6", 4, offsetof(struct pt_regs, ARM_r6)},
26 { "r7", 4, offsetof(struct pt_regs, ARM_r7)},
27 { "r8", 4, offsetof(struct pt_regs, ARM_r8)},
28 { "r9", 4, offsetof(struct pt_regs, ARM_r9)},
29 { "r10", 4, offsetof(struct pt_regs, ARM_r10)},
30 { "fp", 4, offsetof(struct pt_regs, ARM_fp)},
31 { "ip", 4, offsetof(struct pt_regs, ARM_ip)},
32 { "sp", 4, offsetof(struct pt_regs, ARM_sp)},
33 { "lr", 4, offsetof(struct pt_regs, ARM_lr)},
34 { "pc", 4, offsetof(struct pt_regs, ARM_pc)},
35 { "f0", 12, -1 },
36 { "f1", 12, -1 },
37 { "f2", 12, -1 },
38 { "f3", 12, -1 },
39 { "f4", 12, -1 },
40 { "f5", 12, -1 },
41 { "f6", 12, -1 },
42 { "f7", 12, -1 },
43 { "fps", 4, -1 },
44 { "cpsr", 4, offsetof(struct pt_regs, ARM_cpsr)},
45};
46
47char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
48{
49 if (regno >= DBG_MAX_REG_NUM || regno < 0)
50 return NULL;
51
52 if (dbg_reg_def[regno].offset != -1)
53 memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
54 dbg_reg_def[regno].size);
55 else
56 memset(mem, 0, dbg_reg_def[regno].size);
57 return dbg_reg_def[regno].name;
58}
59
60int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
61{
62 if (regno >= DBG_MAX_REG_NUM || regno < 0)
63 return -EINVAL;
64
65 if (dbg_reg_def[regno].offset != -1)
66 memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
67 dbg_reg_def[regno].size);
68 return 0;
69}
70
71void
72sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
73{
74 struct pt_regs *thread_regs;
75 int regno;
76
77 /* Just making sure... */
78 if (task == NULL)
79 return;
80
81 /* Initialize to zero */
82 for (regno = 0; regno < GDB_MAX_REGS; regno++)
83 gdb_regs[regno] = 0;
84
85 /* Otherwise, we have only some registers from switch_to() */
86 thread_regs = task_pt_regs(task);
87 gdb_regs[_R0] = thread_regs->ARM_r0;
88 gdb_regs[_R1] = thread_regs->ARM_r1;
89 gdb_regs[_R2] = thread_regs->ARM_r2;
90 gdb_regs[_R3] = thread_regs->ARM_r3;
91 gdb_regs[_R4] = thread_regs->ARM_r4;
92 gdb_regs[_R5] = thread_regs->ARM_r5;
93 gdb_regs[_R6] = thread_regs->ARM_r6;
94 gdb_regs[_R7] = thread_regs->ARM_r7;
95 gdb_regs[_R8] = thread_regs->ARM_r8;
96 gdb_regs[_R9] = thread_regs->ARM_r9;
97 gdb_regs[_R10] = thread_regs->ARM_r10;
98 gdb_regs[_FP] = thread_regs->ARM_fp;
99 gdb_regs[_IP] = thread_regs->ARM_ip;
100 gdb_regs[_SPT] = thread_regs->ARM_sp;
101 gdb_regs[_LR] = thread_regs->ARM_lr;
102 gdb_regs[_PC] = thread_regs->ARM_pc;
103 gdb_regs[_CPSR] = thread_regs->ARM_cpsr;
104}
105
106void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
107{
108 regs->ARM_pc = pc;
109}
110
111static int compiled_break;
112
113int kgdb_arch_handle_exception(int exception_vector, int signo,
114 int err_code, char *remcom_in_buffer,
115 char *remcom_out_buffer,
116 struct pt_regs *linux_regs)
117{
118 unsigned long addr;
119 char *ptr;
120
121 switch (remcom_in_buffer[0]) {
122 case 'D':
123 case 'k':
124 case 'c':
125 /*
126 * Try to read optional parameter, pc unchanged if no parm.
127 * If this was a compiled breakpoint, we need to move
128 * to the next instruction or we will just breakpoint
129 * over and over again.
130 */
131 ptr = &remcom_in_buffer[1];
132 if (kgdb_hex2long(&ptr, &addr))
133 linux_regs->ARM_pc = addr;
134 else if (compiled_break == 1)
135 linux_regs->ARM_pc += 4;
136
137 compiled_break = 0;
138
139 return 0;
140 }
141
142 return -1;
143}
144
145static int kgdb_brk_fn(struct pt_regs *regs, unsigned int instr)
146{
147 kgdb_handle_exception(1, SIGTRAP, 0, regs);
148
149 return 0;
150}
151
152static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int instr)
153{
154 compiled_break = 1;
155 kgdb_handle_exception(1, SIGTRAP, 0, regs);
156
157 return 0;
158}
159
160static struct undef_hook kgdb_brkpt_hook = {
161 .instr_mask = 0xffffffff,
162 .instr_val = KGDB_BREAKINST,
163 .fn = kgdb_brk_fn
164};
165
166static struct undef_hook kgdb_compiled_brkpt_hook = {
167 .instr_mask = 0xffffffff,
168 .instr_val = KGDB_COMPILED_BREAK,
169 .fn = kgdb_compiled_brk_fn
170};
171
172static void kgdb_call_nmi_hook(void *ignored)
173{
174 kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
175}
176
177void kgdb_roundup_cpus(unsigned long flags)
178{
179 local_irq_enable();
180 smp_call_function(kgdb_call_nmi_hook, NULL, 0);
181 local_irq_disable();
182}
183
184static int __kgdb_notify(struct die_args *args, unsigned long cmd)
185{
186 struct pt_regs *regs = args->regs;
187
188 if (kgdb_handle_exception(1, args->signr, cmd, regs))
189 return NOTIFY_DONE;
190 return NOTIFY_STOP;
191}
192static int
193kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
194{
195 unsigned long flags;
196 int ret;
197
198 local_irq_save(flags);
199 ret = __kgdb_notify(ptr, cmd);
200 local_irq_restore(flags);
201
202 return ret;
203}
204
205static struct notifier_block kgdb_notifier = {
206 .notifier_call = kgdb_notify,
207 .priority = -INT_MAX,
208};
209
210
211/**
212 * kgdb_arch_init - Perform any architecture specific initalization.
213 *
214 * This function will handle the initalization of any architecture
215 * specific callbacks.
216 */
217int kgdb_arch_init(void)
218{
219 int ret = register_die_notifier(&kgdb_notifier);
220
221 if (ret != 0)
222 return ret;
223
224 register_undef_hook(&kgdb_brkpt_hook);
225 register_undef_hook(&kgdb_compiled_brkpt_hook);
226
227 return 0;
228}
229
230/**
231 * kgdb_arch_exit - Perform any architecture specific uninitalization.
232 *
233 * This function will handle the uninitalization of any architecture
234 * specific callbacks, for dynamic registration and unregistration.
235 */
236void kgdb_arch_exit(void)
237{
238 unregister_undef_hook(&kgdb_brkpt_hook);
239 unregister_undef_hook(&kgdb_compiled_brkpt_hook);
240 unregister_die_notifier(&kgdb_notifier);
241}
242
243/*
244 * Register our undef instruction hooks with ARM undef core.
245 * We regsiter a hook specifically looking for the KGB break inst
246 * and we handle the normal undef case within the do_undefinstr
247 * handler.
248 */
249struct kgdb_arch arch_kgdb_ops = {
250#ifndef __ARMEB__
251 .gdb_bpt_instr = {0xfe, 0xde, 0xff, 0xe7}
252#else /* ! __ARMEB__ */
253 .gdb_bpt_instr = {0xe7, 0xff, 0xde, 0xfe}
254#endif
255};
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * arch/arm/kernel/kgdb.c
4 *
5 * ARM KGDB support
6 *
7 * Copyright (c) 2002-2004 MontaVista Software, Inc
8 * Copyright (c) 2008 Wind River Systems, Inc.
9 *
10 * Authors: George Davis <davis_g@mvista.com>
11 * Deepak Saxena <dsaxena@plexity.net>
12 */
13#include <linux/irq.h>
14#include <linux/kdebug.h>
15#include <linux/kgdb.h>
16#include <linux/uaccess.h>
17
18#include <asm/patch.h>
19#include <asm/traps.h>
20
21struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
22{
23 { "r0", 4, offsetof(struct pt_regs, ARM_r0)},
24 { "r1", 4, offsetof(struct pt_regs, ARM_r1)},
25 { "r2", 4, offsetof(struct pt_regs, ARM_r2)},
26 { "r3", 4, offsetof(struct pt_regs, ARM_r3)},
27 { "r4", 4, offsetof(struct pt_regs, ARM_r4)},
28 { "r5", 4, offsetof(struct pt_regs, ARM_r5)},
29 { "r6", 4, offsetof(struct pt_regs, ARM_r6)},
30 { "r7", 4, offsetof(struct pt_regs, ARM_r7)},
31 { "r8", 4, offsetof(struct pt_regs, ARM_r8)},
32 { "r9", 4, offsetof(struct pt_regs, ARM_r9)},
33 { "r10", 4, offsetof(struct pt_regs, ARM_r10)},
34 { "fp", 4, offsetof(struct pt_regs, ARM_fp)},
35 { "ip", 4, offsetof(struct pt_regs, ARM_ip)},
36 { "sp", 4, offsetof(struct pt_regs, ARM_sp)},
37 { "lr", 4, offsetof(struct pt_regs, ARM_lr)},
38 { "pc", 4, offsetof(struct pt_regs, ARM_pc)},
39 { "f0", 12, -1 },
40 { "f1", 12, -1 },
41 { "f2", 12, -1 },
42 { "f3", 12, -1 },
43 { "f4", 12, -1 },
44 { "f5", 12, -1 },
45 { "f6", 12, -1 },
46 { "f7", 12, -1 },
47 { "fps", 4, -1 },
48 { "cpsr", 4, offsetof(struct pt_regs, ARM_cpsr)},
49};
50
51char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
52{
53 if (regno >= DBG_MAX_REG_NUM || regno < 0)
54 return NULL;
55
56 if (dbg_reg_def[regno].offset != -1)
57 memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
58 dbg_reg_def[regno].size);
59 else
60 memset(mem, 0, dbg_reg_def[regno].size);
61 return dbg_reg_def[regno].name;
62}
63
64int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
65{
66 if (regno >= DBG_MAX_REG_NUM || regno < 0)
67 return -EINVAL;
68
69 if (dbg_reg_def[regno].offset != -1)
70 memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
71 dbg_reg_def[regno].size);
72 return 0;
73}
74
75void
76sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
77{
78 struct thread_info *ti;
79 int regno;
80
81 /* Just making sure... */
82 if (task == NULL)
83 return;
84
85 /* Initialize to zero */
86 for (regno = 0; regno < GDB_MAX_REGS; regno++)
87 gdb_regs[regno] = 0;
88
89 /* Otherwise, we have only some registers from switch_to() */
90 ti = task_thread_info(task);
91 gdb_regs[_R4] = ti->cpu_context.r4;
92 gdb_regs[_R5] = ti->cpu_context.r5;
93 gdb_regs[_R6] = ti->cpu_context.r6;
94 gdb_regs[_R7] = ti->cpu_context.r7;
95 gdb_regs[_R8] = ti->cpu_context.r8;
96 gdb_regs[_R9] = ti->cpu_context.r9;
97 gdb_regs[_R10] = ti->cpu_context.sl;
98 gdb_regs[_FP] = ti->cpu_context.fp;
99 gdb_regs[_SPT] = ti->cpu_context.sp;
100 gdb_regs[_PC] = ti->cpu_context.pc;
101}
102
103void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
104{
105 regs->ARM_pc = pc;
106}
107
108static int compiled_break;
109
110int kgdb_arch_handle_exception(int exception_vector, int signo,
111 int err_code, char *remcom_in_buffer,
112 char *remcom_out_buffer,
113 struct pt_regs *linux_regs)
114{
115 unsigned long addr;
116 char *ptr;
117
118 switch (remcom_in_buffer[0]) {
119 case 'D':
120 case 'k':
121 case 'c':
122 /*
123 * Try to read optional parameter, pc unchanged if no parm.
124 * If this was a compiled breakpoint, we need to move
125 * to the next instruction or we will just breakpoint
126 * over and over again.
127 */
128 ptr = &remcom_in_buffer[1];
129 if (kgdb_hex2long(&ptr, &addr))
130 linux_regs->ARM_pc = addr;
131 else if (compiled_break == 1)
132 linux_regs->ARM_pc += 4;
133
134 compiled_break = 0;
135
136 return 0;
137 }
138
139 return -1;
140}
141
142static int kgdb_brk_fn(struct pt_regs *regs, unsigned int instr)
143{
144 kgdb_handle_exception(1, SIGTRAP, 0, regs);
145
146 return 0;
147}
148
149static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int instr)
150{
151 compiled_break = 1;
152 kgdb_handle_exception(1, SIGTRAP, 0, regs);
153
154 return 0;
155}
156
157static struct undef_hook kgdb_brkpt_hook = {
158 .instr_mask = 0xffffffff,
159 .instr_val = KGDB_BREAKINST,
160 .cpsr_mask = MODE_MASK,
161 .cpsr_val = SVC_MODE,
162 .fn = kgdb_brk_fn
163};
164
165static struct undef_hook kgdb_compiled_brkpt_hook = {
166 .instr_mask = 0xffffffff,
167 .instr_val = KGDB_COMPILED_BREAK,
168 .cpsr_mask = MODE_MASK,
169 .cpsr_val = SVC_MODE,
170 .fn = kgdb_compiled_brk_fn
171};
172
173static int __kgdb_notify(struct die_args *args, unsigned long cmd)
174{
175 struct pt_regs *regs = args->regs;
176
177 if (kgdb_handle_exception(1, args->signr, cmd, regs))
178 return NOTIFY_DONE;
179 return NOTIFY_STOP;
180}
181static int
182kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
183{
184 unsigned long flags;
185 int ret;
186
187 local_irq_save(flags);
188 ret = __kgdb_notify(ptr, cmd);
189 local_irq_restore(flags);
190
191 return ret;
192}
193
194static struct notifier_block kgdb_notifier = {
195 .notifier_call = kgdb_notify,
196 .priority = -INT_MAX,
197};
198
199
200/**
201 * kgdb_arch_init - Perform any architecture specific initalization.
202 *
203 * This function will handle the initalization of any architecture
204 * specific callbacks.
205 */
206int kgdb_arch_init(void)
207{
208 int ret = register_die_notifier(&kgdb_notifier);
209
210 if (ret != 0)
211 return ret;
212
213 register_undef_hook(&kgdb_brkpt_hook);
214 register_undef_hook(&kgdb_compiled_brkpt_hook);
215
216 return 0;
217}
218
219/**
220 * kgdb_arch_exit - Perform any architecture specific uninitalization.
221 *
222 * This function will handle the uninitalization of any architecture
223 * specific callbacks, for dynamic registration and unregistration.
224 */
225void kgdb_arch_exit(void)
226{
227 unregister_undef_hook(&kgdb_brkpt_hook);
228 unregister_undef_hook(&kgdb_compiled_brkpt_hook);
229 unregister_die_notifier(&kgdb_notifier);
230}
231
232int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
233{
234 int err;
235
236 /* patch_text() only supports int-sized breakpoints */
237 BUILD_BUG_ON(sizeof(int) != BREAK_INSTR_SIZE);
238
239 err = copy_from_kernel_nofault(bpt->saved_instr, (char *)bpt->bpt_addr,
240 BREAK_INSTR_SIZE);
241 if (err)
242 return err;
243
244 /* Machine is already stopped, so we can use __patch_text() directly */
245 __patch_text((void *)bpt->bpt_addr,
246 *(unsigned int *)arch_kgdb_ops.gdb_bpt_instr);
247
248 return err;
249}
250
251int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
252{
253 /* Machine is already stopped, so we can use __patch_text() directly */
254 __patch_text((void *)bpt->bpt_addr, *(unsigned int *)bpt->saved_instr);
255
256 return 0;
257}
258
259/*
260 * Register our undef instruction hooks with ARM undef core.
261 * We register a hook specifically looking for the KGB break inst
262 * and we handle the normal undef case within the do_undefinstr
263 * handler.
264 */
265const struct kgdb_arch arch_kgdb_ops = {
266#ifndef __ARMEB__
267 .gdb_bpt_instr = {0xfe, 0xde, 0xff, 0xe7}
268#else /* ! __ARMEB__ */
269 .gdb_bpt_instr = {0xe7, 0xff, 0xde, 0xfe}
270#endif
271};