WIP FPC-III support
[linux/fpc-iii.git] / arch / arc / kernel / kgdb.c
blobecfbc42d3a40f0f799438f5177cb54ce66146e50
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * kgdb support for ARC
5 * Copyright (C) 2012 Synopsys, Inc. (www.synopsys.com)
6 */
8 #include <linux/kgdb.h>
9 #include <linux/sched.h>
10 #include <linux/sched/task_stack.h>
11 #include <asm/disasm.h>
12 #include <asm/cacheflush.h>
14 static void to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs,
15 struct callee_regs *cregs)
17 int regno;
19 for (regno = 0; regno <= 26; regno++)
20 gdb_regs[_R0 + regno] = get_reg(regno, kernel_regs, cregs);
22 for (regno = 27; regno < GDB_MAX_REGS; regno++)
23 gdb_regs[regno] = 0;
25 gdb_regs[_FP] = kernel_regs->fp;
26 gdb_regs[__SP] = kernel_regs->sp;
27 gdb_regs[_BLINK] = kernel_regs->blink;
28 gdb_regs[_RET] = kernel_regs->ret;
29 gdb_regs[_STATUS32] = kernel_regs->status32;
30 gdb_regs[_LP_COUNT] = kernel_regs->lp_count;
31 gdb_regs[_LP_END] = kernel_regs->lp_end;
32 gdb_regs[_LP_START] = kernel_regs->lp_start;
33 gdb_regs[_BTA] = kernel_regs->bta;
34 gdb_regs[_STOP_PC] = kernel_regs->ret;
37 static void from_gdb_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs,
38 struct callee_regs *cregs)
40 int regno;
42 for (regno = 0; regno <= 26; regno++)
43 set_reg(regno, gdb_regs[regno + _R0], kernel_regs, cregs);
45 kernel_regs->fp = gdb_regs[_FP];
46 kernel_regs->sp = gdb_regs[__SP];
47 kernel_regs->blink = gdb_regs[_BLINK];
48 kernel_regs->ret = gdb_regs[_RET];
49 kernel_regs->status32 = gdb_regs[_STATUS32];
50 kernel_regs->lp_count = gdb_regs[_LP_COUNT];
51 kernel_regs->lp_end = gdb_regs[_LP_END];
52 kernel_regs->lp_start = gdb_regs[_LP_START];
53 kernel_regs->bta = gdb_regs[_BTA];
57 void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs)
59 to_gdb_regs(gdb_regs, kernel_regs, (struct callee_regs *)
60 current->thread.callee_reg);
63 void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs)
65 from_gdb_regs(gdb_regs, kernel_regs, (struct callee_regs *)
66 current->thread.callee_reg);
69 void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs,
70 struct task_struct *task)
72 if (task)
73 to_gdb_regs(gdb_regs, task_pt_regs(task),
74 (struct callee_regs *) task->thread.callee_reg);
77 struct single_step_data_t {
78 uint16_t opcode[2];
79 unsigned long address[2];
80 int is_branch;
81 int armed;
82 } single_step_data;
84 static void undo_single_step(struct pt_regs *regs)
86 if (single_step_data.armed) {
87 int i;
89 for (i = 0; i < (single_step_data.is_branch ? 2 : 1); i++) {
90 memcpy((void *) single_step_data.address[i],
91 &single_step_data.opcode[i],
92 BREAK_INSTR_SIZE);
94 flush_icache_range(single_step_data.address[i],
95 single_step_data.address[i] +
96 BREAK_INSTR_SIZE);
98 single_step_data.armed = 0;
102 static void place_trap(unsigned long address, void *save)
104 memcpy(save, (void *) address, BREAK_INSTR_SIZE);
105 memcpy((void *) address, &arch_kgdb_ops.gdb_bpt_instr,
106 BREAK_INSTR_SIZE);
107 flush_icache_range(address, address + BREAK_INSTR_SIZE);
110 static void do_single_step(struct pt_regs *regs)
112 single_step_data.is_branch = disasm_next_pc((unsigned long)
113 regs->ret, regs, (struct callee_regs *)
114 current->thread.callee_reg,
115 &single_step_data.address[0],
116 &single_step_data.address[1]);
118 place_trap(single_step_data.address[0], &single_step_data.opcode[0]);
120 if (single_step_data.is_branch) {
121 place_trap(single_step_data.address[1],
122 &single_step_data.opcode[1]);
125 single_step_data.armed++;
128 int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
129 char *remcomInBuffer, char *remcomOutBuffer,
130 struct pt_regs *regs)
132 unsigned long addr;
133 char *ptr;
135 undo_single_step(regs);
137 switch (remcomInBuffer[0]) {
138 case 's':
139 case 'c':
140 ptr = &remcomInBuffer[1];
141 if (kgdb_hex2long(&ptr, &addr))
142 regs->ret = addr;
144 case 'D':
145 case 'k':
146 atomic_set(&kgdb_cpu_doing_single_step, -1);
148 if (remcomInBuffer[0] == 's') {
149 do_single_step(regs);
150 atomic_set(&kgdb_cpu_doing_single_step,
151 smp_processor_id());
154 return 0;
156 return -1;
159 int kgdb_arch_init(void)
161 single_step_data.armed = 0;
162 return 0;
165 void kgdb_trap(struct pt_regs *regs)
167 /* trap_s 3 is used for breakpoints that overwrite existing
168 * instructions, while trap_s 4 is used for compiled breakpoints.
170 * with trap_s 3 breakpoints the original instruction needs to be
171 * restored and continuation needs to start at the location of the
172 * breakpoint.
174 * with trap_s 4 (compiled) breakpoints, continuation needs to
175 * start after the breakpoint.
177 if (regs->ecr_param == 3)
178 instruction_pointer(regs) -= BREAK_INSTR_SIZE;
180 kgdb_handle_exception(1, SIGTRAP, 0, regs);
183 void kgdb_arch_exit(void)
187 void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
189 instruction_pointer(regs) = ip;
192 void kgdb_call_nmi_hook(void *ignored)
194 /* Default implementation passes get_irq_regs() but we don't */
195 kgdb_nmicallback(raw_smp_processor_id(), NULL);
198 const struct kgdb_arch arch_kgdb_ops = {
199 /* breakpoint instruction: TRAP_S 0x3 */
200 #ifdef CONFIG_CPU_BIG_ENDIAN
201 .gdb_bpt_instr = {0x78, 0x7e},
202 #else
203 .gdb_bpt_instr = {0x7e, 0x78},
204 #endif