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 <linux/sched.h>
13 #include <linux/sched/task_stack.h>
14 #include <asm/disasm.h>
15 #include <asm/cacheflush.h>
17 static void to_gdb_regs(unsigned long *gdb_regs
, struct pt_regs
*kernel_regs
,
18 struct callee_regs
*cregs
)
22 for (regno
= 0; regno
<= 26; regno
++)
23 gdb_regs
[_R0
+ regno
] = get_reg(regno
, kernel_regs
, cregs
);
25 for (regno
= 27; regno
< GDB_MAX_REGS
; regno
++)
28 gdb_regs
[_FP
] = kernel_regs
->fp
;
29 gdb_regs
[__SP
] = kernel_regs
->sp
;
30 gdb_regs
[_BLINK
] = kernel_regs
->blink
;
31 gdb_regs
[_RET
] = kernel_regs
->ret
;
32 gdb_regs
[_STATUS32
] = kernel_regs
->status32
;
33 gdb_regs
[_LP_COUNT
] = kernel_regs
->lp_count
;
34 gdb_regs
[_LP_END
] = kernel_regs
->lp_end
;
35 gdb_regs
[_LP_START
] = kernel_regs
->lp_start
;
36 gdb_regs
[_BTA
] = kernel_regs
->bta
;
37 gdb_regs
[_STOP_PC
] = kernel_regs
->ret
;
40 static void from_gdb_regs(unsigned long *gdb_regs
, struct pt_regs
*kernel_regs
,
41 struct callee_regs
*cregs
)
45 for (regno
= 0; regno
<= 26; regno
++)
46 set_reg(regno
, gdb_regs
[regno
+ _R0
], kernel_regs
, cregs
);
48 kernel_regs
->fp
= gdb_regs
[_FP
];
49 kernel_regs
->sp
= gdb_regs
[__SP
];
50 kernel_regs
->blink
= gdb_regs
[_BLINK
];
51 kernel_regs
->ret
= gdb_regs
[_RET
];
52 kernel_regs
->status32
= gdb_regs
[_STATUS32
];
53 kernel_regs
->lp_count
= gdb_regs
[_LP_COUNT
];
54 kernel_regs
->lp_end
= gdb_regs
[_LP_END
];
55 kernel_regs
->lp_start
= gdb_regs
[_LP_START
];
56 kernel_regs
->bta
= gdb_regs
[_BTA
];
60 void pt_regs_to_gdb_regs(unsigned long *gdb_regs
, struct pt_regs
*kernel_regs
)
62 to_gdb_regs(gdb_regs
, kernel_regs
, (struct callee_regs
*)
63 current
->thread
.callee_reg
);
66 void gdb_regs_to_pt_regs(unsigned long *gdb_regs
, struct pt_regs
*kernel_regs
)
68 from_gdb_regs(gdb_regs
, kernel_regs
, (struct callee_regs
*)
69 current
->thread
.callee_reg
);
72 void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs
,
73 struct task_struct
*task
)
76 to_gdb_regs(gdb_regs
, task_pt_regs(task
),
77 (struct callee_regs
*) task
->thread
.callee_reg
);
80 struct single_step_data_t
{
82 unsigned long address
[2];
87 static void undo_single_step(struct pt_regs
*regs
)
89 if (single_step_data
.armed
) {
92 for (i
= 0; i
< (single_step_data
.is_branch
? 2 : 1); i
++) {
93 memcpy((void *) single_step_data
.address
[i
],
94 &single_step_data
.opcode
[i
],
97 flush_icache_range(single_step_data
.address
[i
],
98 single_step_data
.address
[i
] +
101 single_step_data
.armed
= 0;
105 static void place_trap(unsigned long address
, void *save
)
107 memcpy(save
, (void *) address
, BREAK_INSTR_SIZE
);
108 memcpy((void *) address
, &arch_kgdb_ops
.gdb_bpt_instr
,
110 flush_icache_range(address
, address
+ BREAK_INSTR_SIZE
);
113 static void do_single_step(struct pt_regs
*regs
)
115 single_step_data
.is_branch
= disasm_next_pc((unsigned long)
116 regs
->ret
, regs
, (struct callee_regs
*)
117 current
->thread
.callee_reg
,
118 &single_step_data
.address
[0],
119 &single_step_data
.address
[1]);
121 place_trap(single_step_data
.address
[0], &single_step_data
.opcode
[0]);
123 if (single_step_data
.is_branch
) {
124 place_trap(single_step_data
.address
[1],
125 &single_step_data
.opcode
[1]);
128 single_step_data
.armed
++;
131 int kgdb_arch_handle_exception(int e_vector
, int signo
, int err_code
,
132 char *remcomInBuffer
, char *remcomOutBuffer
,
133 struct pt_regs
*regs
)
138 undo_single_step(regs
);
140 switch (remcomInBuffer
[0]) {
143 ptr
= &remcomInBuffer
[1];
144 if (kgdb_hex2long(&ptr
, &addr
))
149 atomic_set(&kgdb_cpu_doing_single_step
, -1);
151 if (remcomInBuffer
[0] == 's') {
152 do_single_step(regs
);
153 atomic_set(&kgdb_cpu_doing_single_step
,
162 int kgdb_arch_init(void)
164 single_step_data
.armed
= 0;
168 void kgdb_trap(struct pt_regs
*regs
)
170 /* trap_s 3 is used for breakpoints that overwrite existing
171 * instructions, while trap_s 4 is used for compiled breakpoints.
173 * with trap_s 3 breakpoints the original instruction needs to be
174 * restored and continuation needs to start at the location of the
177 * with trap_s 4 (compiled) breakpoints, continuation needs to
178 * start after the breakpoint.
180 if (regs
->ecr_param
== 3)
181 instruction_pointer(regs
) -= BREAK_INSTR_SIZE
;
183 kgdb_handle_exception(1, SIGTRAP
, 0, regs
);
186 void kgdb_arch_exit(void)
190 void kgdb_arch_set_pc(struct pt_regs
*regs
, unsigned long ip
)
192 instruction_pointer(regs
) = ip
;
195 static void kgdb_call_nmi_hook(void *ignored
)
197 kgdb_nmicallback(raw_smp_processor_id(), NULL
);
200 void kgdb_roundup_cpus(unsigned long flags
)
203 smp_call_function(kgdb_call_nmi_hook
, NULL
, 0);
207 struct kgdb_arch arch_kgdb_ops
= {
208 /* breakpoint instruction: TRAP_S 0x3 */
209 #ifdef CONFIG_CPU_BIG_ENDIAN
210 .gdb_bpt_instr
= {0x78, 0x7e},
212 .gdb_bpt_instr
= {0x7e, 0x78},