treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / arch / arm64 / kernel / kgdb.c
blob43119922341f81272568fc6049f0ec163d26eaa8
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * AArch64 KGDB support
5 * Based on arch/arm/kernel/kgdb.c
7 * Copyright (C) 2013 Cavium Inc.
8 * Author: Vijaya Kumar K <vijaya.kumar@caviumnetworks.com>
9 */
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>
18 #include <asm/debug-monitors.h>
19 #include <asm/insn.h>
20 #include <asm/traps.h>
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)},
57 * struct pt_regs thinks PSTATE is 64-bits wide but gdb remote
58 * protocol disagrees. Therefore we must extract only the lower
59 * 32-bits. Look for the big comment in asm/kgdb.h for more
60 * detail.
62 { "pstate", 4, offsetof(struct pt_regs, pstate)
63 #ifdef CONFIG_CPU_BIG_ENDIAN
64 + 4
65 #endif
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 },
103 char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
105 if (regno >= DBG_MAX_REG_NUM || regno < 0)
106 return NULL;
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;
116 int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
118 if (regno >= DBG_MAX_REG_NUM || regno < 0)
119 return -EINVAL;
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;
127 void
128 sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
130 struct cpu_context *cpu_context = &task->thread.cpu_context;
132 /* Initialize to zero */
133 memset((char *)gdb_regs, 0, NUMREGBYTES);
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;
147 gdb_regs[31] = cpu_context->sp;
148 gdb_regs[32] = cpu_context->pc;
151 void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
153 regs->pc = pc;
156 static int compiled_break;
158 static void kgdb_arch_update_addr(struct pt_regs *regs,
159 char *remcom_in_buffer)
161 unsigned long addr;
162 char *ptr;
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);
170 compiled_break = 0;
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)
178 int err;
180 switch (remcom_in_buffer[0]) {
181 case 'D':
182 case 'k':
184 * Packet D (Detach), k (kill). No special handling
185 * is required here. Handle same as c packet.
187 case 'c':
189 * Packet c (Continue) to continue executing.
190 * Set pc to required address.
191 * Try to read optional parameter and set pc.
192 * If this was a compiled breakpoint, we need to move
193 * to the next instruction else we will just breakpoint
194 * over and over again.
196 kgdb_arch_update_addr(linux_regs, remcom_in_buffer);
197 atomic_set(&kgdb_cpu_doing_single_step, -1);
198 kgdb_single_step = 0;
201 * Received continue command, disable single step
203 if (kernel_active_single_step())
204 kernel_disable_single_step();
206 err = 0;
207 break;
208 case 's':
210 * Update step address value with address passed
211 * with step packet.
212 * On debug exception return PC is copied to ELR
213 * So just update PC.
214 * If no step address is passed, resume from the address
215 * pointed by PC. Do not update PC
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;
222 * Enable single step handling
224 if (!kernel_active_single_step())
225 kernel_enable_single_step(linux_regs);
226 err = 0;
227 break;
228 default:
229 err = -1;
231 return err;
234 static int kgdb_brk_fn(struct pt_regs *regs, unsigned int esr)
236 kgdb_handle_exception(1, SIGTRAP, 0, regs);
237 return DBG_HOOK_HANDLED;
239 NOKPROBE_SYMBOL(kgdb_brk_fn)
241 static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int esr)
243 compiled_break = 1;
244 kgdb_handle_exception(1, SIGTRAP, 0, regs);
246 return DBG_HOOK_HANDLED;
248 NOKPROBE_SYMBOL(kgdb_compiled_brk_fn);
250 static int kgdb_step_brk_fn(struct pt_regs *regs, unsigned int esr)
252 if (!kgdb_single_step)
253 return DBG_HOOK_ERROR;
255 kgdb_handle_exception(1, SIGTRAP, 0, regs);
256 return DBG_HOOK_HANDLED;
258 NOKPROBE_SYMBOL(kgdb_step_brk_fn);
260 static struct break_hook kgdb_brkpt_hook = {
261 .fn = kgdb_brk_fn,
262 .imm = KGDB_DYN_DBG_BRK_IMM,
265 static struct break_hook kgdb_compiled_brkpt_hook = {
266 .fn = kgdb_compiled_brk_fn,
267 .imm = KGDB_COMPILED_DBG_BRK_IMM,
270 static struct step_hook kgdb_step_hook = {
271 .fn = kgdb_step_brk_fn
274 static int __kgdb_notify(struct die_args *args, unsigned long cmd)
276 struct pt_regs *regs = args->regs;
278 if (kgdb_handle_exception(1, args->signr, cmd, regs))
279 return NOTIFY_DONE;
280 return NOTIFY_STOP;
283 static int
284 kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
286 unsigned long flags;
287 int ret;
289 local_irq_save(flags);
290 ret = __kgdb_notify(ptr, cmd);
291 local_irq_restore(flags);
293 return ret;
296 static struct notifier_block kgdb_notifier = {
297 .notifier_call = kgdb_notify,
299 * Want to be lowest priority
301 .priority = -INT_MAX,
305 * kgdb_arch_init - Perform any architecture specific initialization.
306 * This function will handle the initialization of any architecture
307 * specific callbacks.
309 int kgdb_arch_init(void)
311 int ret = register_die_notifier(&kgdb_notifier);
313 if (ret != 0)
314 return ret;
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;
323 * kgdb_arch_exit - Perform any architecture specific uninitalization.
324 * This function will handle the uninitalization of any architecture
325 * specific callbacks, for dynamic registration and unregistration.
327 void kgdb_arch_exit(void)
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);
335 const struct kgdb_arch arch_kgdb_ops;
337 int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
339 int err;
341 BUILD_BUG_ON(AARCH64_INSN_SIZE != BREAK_INSTR_SIZE);
343 err = aarch64_insn_read((void *)bpt->bpt_addr, (u32 *)bpt->saved_instr);
344 if (err)
345 return err;
347 return aarch64_insn_write((void *)bpt->bpt_addr,
348 (u32)AARCH64_BREAK_KGDB_DYN_DBG);
351 int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
353 return aarch64_insn_write((void *)bpt->bpt_addr,
354 *(u32 *)bpt->saved_instr);