blob: 2888ba5be47e4be4e796d0840abb9570253a416a [file] [log] [blame]
Mischa Jonkerf46121b2013-01-18 15:12:24 +05301/*
2 * kgdb support for ARC
3 *
4 * Copyright (C) 2012 Synopsys, Inc. (www.synopsys.com)
5 *
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.
9 */
10
11#include <linux/kgdb.h>
12#include <asm/disasm.h>
13#include <asm/cacheflush.h>
14
15static void to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs,
16 struct callee_regs *cregs)
17{
18 int regno;
19
20 for (regno = 0; regno <= 26; regno++)
21 gdb_regs[_R0 + regno] = get_reg(regno, kernel_regs, cregs);
22
23 for (regno = 27; regno < GDB_MAX_REGS; regno++)
24 gdb_regs[regno] = 0;
25
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;
36}
37
38static void from_gdb_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs,
39 struct callee_regs *cregs)
40{
41 int regno;
42
43 for (regno = 0; regno <= 26; regno++)
44 set_reg(regno, gdb_regs[regno + _R0], kernel_regs, cregs);
45
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];
55}
56
57
58void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs)
59{
60 to_gdb_regs(gdb_regs, kernel_regs, (struct callee_regs *)
61 current->thread.callee_reg);
62}
63
64void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs)
65{
66 from_gdb_regs(gdb_regs, kernel_regs, (struct callee_regs *)
67 current->thread.callee_reg);
68}
69
70void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs,
71 struct task_struct *task)
72{
73 if (task)
74 to_gdb_regs(gdb_regs, task_pt_regs(task),
75 (struct callee_regs *) task->thread.callee_reg);
76}
77
78struct single_step_data_t {
79 uint16_t opcode[2];
80 unsigned long address[2];
81 int is_branch;
82 int armed;
83} single_step_data;
84
85static void undo_single_step(struct pt_regs *regs)
86{
87 if (single_step_data.armed) {
88 int i;
89
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],
93 BREAK_INSTR_SIZE);
94
95 flush_icache_range(single_step_data.address[i],
96 single_step_data.address[i] +
97 BREAK_INSTR_SIZE);
98 }
99 single_step_data.armed = 0;
100 }
101}
102
103static void place_trap(unsigned long address, void *save)
104{
105 memcpy(save, (void *) address, BREAK_INSTR_SIZE);
106 memcpy((void *) address, &arch_kgdb_ops.gdb_bpt_instr,
107 BREAK_INSTR_SIZE);
108 flush_icache_range(address, address + BREAK_INSTR_SIZE);
109}
110
111static void do_single_step(struct pt_regs *regs)
112{
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]);
118
119 place_trap(single_step_data.address[0], &single_step_data.opcode[0]);
120
121 if (single_step_data.is_branch) {
122 place_trap(single_step_data.address[1],
123 &single_step_data.opcode[1]);
124 }
125
126 single_step_data.armed++;
127}
128
129int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
130 char *remcomInBuffer, char *remcomOutBuffer,
131 struct pt_regs *regs)
132{
133 unsigned long addr;
134 char *ptr;
135
136 undo_single_step(regs);
137
138 switch (remcomInBuffer[0]) {
139 case 's':
140 case 'c':
141 ptr = &remcomInBuffer[1];
142 if (kgdb_hex2long(&ptr, &addr))
143 regs->ret = addr;
144
145 case 'D':
146 case 'k':
147 atomic_set(&kgdb_cpu_doing_single_step, -1);
148
149 if (remcomInBuffer[0] == 's') {
150 do_single_step(regs);
151 atomic_set(&kgdb_cpu_doing_single_step,
152 smp_processor_id());
153 }
154
155 return 0;
156 }
157 return -1;
158}
159
160unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
161{
162 return instruction_pointer(regs);
163}
164
165int kgdb_arch_init(void)
166{
167 single_step_data.armed = 0;
168 return 0;
169}
170
171void kgdb_trap(struct pt_regs *regs, int param)
172{
173 /* trap_s 3 is used for breakpoints that overwrite existing
174 * instructions, while trap_s 4 is used for compiled breakpoints.
175 *
176 * with trap_s 3 breakpoints the original instruction needs to be
177 * restored and continuation needs to start at the location of the
178 * breakpoint.
179 *
180 * with trap_s 4 (compiled) breakpoints, continuation needs to
181 * start after the breakpoint.
182 */
183 if (param == 3)
184 instruction_pointer(regs) -= BREAK_INSTR_SIZE;
185
186 kgdb_handle_exception(1, SIGTRAP, 0, regs);
187}
188
189void kgdb_arch_exit(void)
190{
191}
192
193void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
194{
195 instruction_pointer(regs) = ip;
196}
197
198struct 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
205};