4 * Copyright (C) 2012 Synopsys, Inc. (www.synopsys.com)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/kgdb.h>
12 #include <asm/disasm.h>
13 #include <asm/cacheflush.h>
15 static void to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs,
16 struct callee_regs *cregs)
20 for (regno = 0; regno <= 26; regno++)
21 gdb_regs[_R0 + regno] = get_reg(regno, kernel_regs, cregs);
23 for (regno = 27; regno < GDB_MAX_REGS; regno++)
26 gdb_regs[_FP] = kernel_regs->fp;
27 gdb_regs[__SP] = kernel_regs->sp;
28 gdb_regs[_BLINK] = kernel_regs->blink;
29 gdb_regs[_RET] = kernel_regs->ret;
30 gdb_regs[_STATUS32] = kernel_regs->status32;
31 gdb_regs[_LP_COUNT] = kernel_regs->lp_count;
32 gdb_regs[_LP_END] = kernel_regs->lp_end;
33 gdb_regs[_LP_START] = kernel_regs->lp_start;
34 gdb_regs[_BTA] = kernel_regs->bta;
35 gdb_regs[_STOP_PC] = kernel_regs->ret;
38 static void from_gdb_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs,
39 struct callee_regs *cregs)
43 for (regno = 0; regno <= 26; regno++)
44 set_reg(regno, gdb_regs[regno + _R0], kernel_regs, cregs);
46 kernel_regs->fp = gdb_regs[_FP];
47 kernel_regs->sp = gdb_regs[__SP];
48 kernel_regs->blink = gdb_regs[_BLINK];
49 kernel_regs->ret = gdb_regs[_RET];
50 kernel_regs->status32 = gdb_regs[_STATUS32];
51 kernel_regs->lp_count = gdb_regs[_LP_COUNT];
52 kernel_regs->lp_end = gdb_regs[_LP_END];
53 kernel_regs->lp_start = gdb_regs[_LP_START];
54 kernel_regs->bta = gdb_regs[_BTA];
58 void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs)
60 to_gdb_regs(gdb_regs, kernel_regs, (struct callee_regs *)
61 current->thread.callee_reg);
64 void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs)
66 from_gdb_regs(gdb_regs, kernel_regs, (struct callee_regs *)
67 current->thread.callee_reg);
70 void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs,
71 struct task_struct *task)
74 to_gdb_regs(gdb_regs, task_pt_regs(task),
75 (struct callee_regs *) task->thread.callee_reg);
78 struct single_step_data_t {
80 unsigned long address[2];
85 static void undo_single_step(struct pt_regs *regs)
87 if (single_step_data.armed) {
90 for (i = 0; i < (single_step_data.is_branch ? 2 : 1); i++) {
91 memcpy((void *) single_step_data.address[i],
92 &single_step_data.opcode[i],
95 flush_icache_range(single_step_data.address[i],
96 single_step_data.address[i] +
99 single_step_data.armed = 0;
103 static void place_trap(unsigned long address, void *save)
105 memcpy(save, (void *) address, BREAK_INSTR_SIZE);
106 memcpy((void *) address, &arch_kgdb_ops.gdb_bpt_instr,
108 flush_icache_range(address, address + BREAK_INSTR_SIZE);
111 static void do_single_step(struct pt_regs *regs)
113 single_step_data.is_branch = disasm_next_pc((unsigned long)
114 regs->ret, regs, (struct callee_regs *)
115 current->thread.callee_reg,
116 &single_step_data.address[0],
117 &single_step_data.address[1]);
119 place_trap(single_step_data.address[0], &single_step_data.opcode[0]);
121 if (single_step_data.is_branch) {
122 place_trap(single_step_data.address[1],
123 &single_step_data.opcode[1]);
126 single_step_data.armed++;
129 int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
130 char *remcomInBuffer, char *remcomOutBuffer,
131 struct pt_regs *regs)
136 undo_single_step(regs);
138 switch (remcomInBuffer[0]) {
141 ptr = &remcomInBuffer[1];
142 if (kgdb_hex2long(&ptr, &addr))
147 atomic_set(&kgdb_cpu_doing_single_step, -1);
149 if (remcomInBuffer[0] == 's') {
150 do_single_step(regs);
151 atomic_set(&kgdb_cpu_doing_single_step,
160 unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
162 return instruction_pointer(regs);
165 int kgdb_arch_init(void)
167 single_step_data.armed = 0;
171 void kgdb_trap(struct pt_regs *regs, int param)
173 /* trap_s 3 is used for breakpoints that overwrite existing
174 * instructions, while trap_s 4 is used for compiled breakpoints.
176 * with trap_s 3 breakpoints the original instruction needs to be
177 * restored and continuation needs to start at the location of the
180 * with trap_s 4 (compiled) breakpoints, continuation needs to
181 * start after the breakpoint.
184 instruction_pointer(regs) -= BREAK_INSTR_SIZE;
186 kgdb_handle_exception(1, SIGTRAP, 0, regs);
189 void kgdb_arch_exit(void)
193 void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
195 instruction_pointer(regs) = ip;
198 struct kgdb_arch arch_kgdb_ops = {
199 /* breakpoint instruction: TRAP_S 0x3 */
200 #ifdef CONFIG_CPU_BIG_ENDIAN
201 .gdb_bpt_instr = {0x78, 0x7e},
203 .gdb_bpt_instr = {0x7e, 0x78},