David S. Miller | e2fdd7f | 2008-04-29 02:38:50 -0700 | [diff] [blame] | 1 | /* kgdb.c: KGDB support for 64-bit sparc. |
| 2 | * |
| 3 | * Copyright (C) 2008 David S. Miller <davem@davemloft.net> |
| 4 | */ |
| 5 | |
| 6 | #include <linux/kgdb.h> |
| 7 | #include <linux/kdebug.h> |
David S. Miller | 9960e9e | 2010-04-07 04:41:33 -0700 | [diff] [blame] | 8 | #include <linux/ftrace.h> |
Kirill Tkhai | 1de425c | 2013-12-03 20:12:37 +0400 | [diff] [blame] | 9 | #include <linux/context_tracking.h> |
David S. Miller | e2fdd7f | 2008-04-29 02:38:50 -0700 | [diff] [blame] | 10 | |
David S. Miller | 8befc9f | 2012-03-29 22:40:52 -0700 | [diff] [blame] | 11 | #include <asm/cacheflush.h> |
David S. Miller | e2fdd7f | 2008-04-29 02:38:50 -0700 | [diff] [blame] | 12 | #include <asm/kdebug.h> |
| 13 | #include <asm/ptrace.h> |
| 14 | #include <asm/irq.h> |
| 15 | |
Sam Ravnborg | 48c7eca | 2014-05-16 23:26:09 +0200 | [diff] [blame] | 16 | #include "kernel.h" |
| 17 | |
David S. Miller | e2fdd7f | 2008-04-29 02:38:50 -0700 | [diff] [blame] | 18 | void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs) |
| 19 | { |
| 20 | struct reg_window *win; |
| 21 | int i; |
| 22 | |
| 23 | gdb_regs[GDB_G0] = 0; |
| 24 | for (i = 0; i < 15; i++) |
| 25 | gdb_regs[GDB_G1 + i] = regs->u_regs[UREG_G1 + i]; |
| 26 | |
| 27 | win = (struct reg_window *) (regs->u_regs[UREG_FP] + STACK_BIAS); |
| 28 | for (i = 0; i < 8; i++) |
| 29 | gdb_regs[GDB_L0 + i] = win->locals[i]; |
| 30 | for (i = 0; i < 8; i++) |
| 31 | gdb_regs[GDB_I0 + i] = win->ins[i]; |
| 32 | |
| 33 | for (i = GDB_F0; i <= GDB_F62; i++) |
| 34 | gdb_regs[i] = 0; |
| 35 | |
| 36 | gdb_regs[GDB_PC] = regs->tpc; |
| 37 | gdb_regs[GDB_NPC] = regs->tnpc; |
| 38 | gdb_regs[GDB_STATE] = regs->tstate; |
| 39 | gdb_regs[GDB_FSR] = 0; |
| 40 | gdb_regs[GDB_FPRS] = 0; |
| 41 | gdb_regs[GDB_Y] = regs->y; |
| 42 | } |
| 43 | |
| 44 | void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p) |
| 45 | { |
| 46 | struct thread_info *t = task_thread_info(p); |
| 47 | extern unsigned int switch_to_pc; |
Kirill Tkhai | 37d6fa3 | 2013-07-26 16:42:39 +0400 | [diff] [blame] | 48 | extern unsigned int ret_from_fork; |
David S. Miller | e2fdd7f | 2008-04-29 02:38:50 -0700 | [diff] [blame] | 49 | struct reg_window *win; |
| 50 | unsigned long pc, cwp; |
| 51 | int i; |
| 52 | |
| 53 | for (i = GDB_G0; i < GDB_G6; i++) |
| 54 | gdb_regs[i] = 0; |
| 55 | gdb_regs[GDB_G6] = (unsigned long) t; |
| 56 | gdb_regs[GDB_G7] = (unsigned long) p; |
| 57 | for (i = GDB_O0; i < GDB_SP; i++) |
| 58 | gdb_regs[i] = 0; |
| 59 | gdb_regs[GDB_SP] = t->ksp; |
| 60 | gdb_regs[GDB_O7] = 0; |
| 61 | |
| 62 | win = (struct reg_window *) (t->ksp + STACK_BIAS); |
| 63 | for (i = 0; i < 8; i++) |
| 64 | gdb_regs[GDB_L0 + i] = win->locals[i]; |
| 65 | for (i = 0; i < 8; i++) |
| 66 | gdb_regs[GDB_I0 + i] = win->ins[i]; |
| 67 | |
| 68 | for (i = GDB_F0; i <= GDB_F62; i++) |
| 69 | gdb_regs[i] = 0; |
| 70 | |
| 71 | if (t->new_child) |
Kirill Tkhai | 37d6fa3 | 2013-07-26 16:42:39 +0400 | [diff] [blame] | 72 | pc = (unsigned long) &ret_from_fork; |
David S. Miller | e2fdd7f | 2008-04-29 02:38:50 -0700 | [diff] [blame] | 73 | else |
| 74 | pc = (unsigned long) &switch_to_pc; |
| 75 | |
| 76 | gdb_regs[GDB_PC] = pc; |
| 77 | gdb_regs[GDB_NPC] = pc + 4; |
| 78 | |
| 79 | cwp = __thread_flag_byte_ptr(t)[TI_FLAG_BYTE_CWP]; |
| 80 | |
| 81 | gdb_regs[GDB_STATE] = (TSTATE_PRIV | TSTATE_IE | cwp); |
| 82 | gdb_regs[GDB_FSR] = 0; |
| 83 | gdb_regs[GDB_FPRS] = 0; |
| 84 | gdb_regs[GDB_Y] = 0; |
| 85 | } |
| 86 | |
| 87 | void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs) |
| 88 | { |
| 89 | struct reg_window *win; |
| 90 | int i; |
| 91 | |
| 92 | for (i = 0; i < 15; i++) |
| 93 | regs->u_regs[UREG_G1 + i] = gdb_regs[GDB_G1 + i]; |
| 94 | |
| 95 | /* If the TSTATE register is changing, we have to preserve |
| 96 | * the CWP field, otherwise window save/restore explodes. |
| 97 | */ |
| 98 | if (regs->tstate != gdb_regs[GDB_STATE]) { |
| 99 | unsigned long cwp = regs->tstate & TSTATE_CWP; |
| 100 | |
| 101 | regs->tstate = (gdb_regs[GDB_STATE] & ~TSTATE_CWP) | cwp; |
| 102 | } |
| 103 | |
| 104 | regs->tpc = gdb_regs[GDB_PC]; |
| 105 | regs->tnpc = gdb_regs[GDB_NPC]; |
| 106 | regs->y = gdb_regs[GDB_Y]; |
| 107 | |
| 108 | win = (struct reg_window *) (regs->u_regs[UREG_FP] + STACK_BIAS); |
| 109 | for (i = 0; i < 8; i++) |
| 110 | win->locals[i] = gdb_regs[GDB_L0 + i]; |
| 111 | for (i = 0; i < 8; i++) |
| 112 | win->ins[i] = gdb_regs[GDB_I0 + i]; |
| 113 | } |
| 114 | |
| 115 | #ifdef CONFIG_SMP |
David S. Miller | 9960e9e | 2010-04-07 04:41:33 -0700 | [diff] [blame] | 116 | void __irq_entry smp_kgdb_capture_client(int irq, struct pt_regs *regs) |
David S. Miller | e2fdd7f | 2008-04-29 02:38:50 -0700 | [diff] [blame] | 117 | { |
| 118 | unsigned long flags; |
| 119 | |
| 120 | __asm__ __volatile__("rdpr %%pstate, %0\n\t" |
| 121 | "wrpr %0, %1, %%pstate" |
| 122 | : "=r" (flags) |
| 123 | : "i" (PSTATE_IE)); |
| 124 | |
| 125 | flushw_all(); |
| 126 | |
| 127 | if (atomic_read(&kgdb_active) != -1) |
| 128 | kgdb_nmicallback(raw_smp_processor_id(), regs); |
| 129 | |
| 130 | __asm__ __volatile__("wrpr %0, 0, %%pstate" |
| 131 | : : "r" (flags)); |
| 132 | } |
| 133 | #endif |
| 134 | |
| 135 | int kgdb_arch_handle_exception(int e_vector, int signo, int err_code, |
| 136 | char *remcomInBuffer, char *remcomOutBuffer, |
| 137 | struct pt_regs *linux_regs) |
| 138 | { |
| 139 | unsigned long addr; |
| 140 | char *ptr; |
| 141 | |
| 142 | switch (remcomInBuffer[0]) { |
| 143 | case 'c': |
| 144 | /* try to read optional parameter, pc unchanged if no parm */ |
| 145 | ptr = &remcomInBuffer[1]; |
| 146 | if (kgdb_hex2long(&ptr, &addr)) { |
| 147 | linux_regs->tpc = addr; |
| 148 | linux_regs->tnpc = addr + 4; |
| 149 | } |
| 150 | /* fallthru */ |
| 151 | |
| 152 | case 'D': |
| 153 | case 'k': |
| 154 | if (linux_regs->tpc == (unsigned long) arch_kgdb_breakpoint) { |
| 155 | linux_regs->tpc = linux_regs->tnpc; |
| 156 | linux_regs->tnpc += 4; |
| 157 | } |
| 158 | return 0; |
| 159 | } |
| 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | asmlinkage void kgdb_trap(unsigned long trap_level, struct pt_regs *regs) |
| 164 | { |
Kirill Tkhai | 812cb83 | 2013-09-14 16:02:11 +0400 | [diff] [blame] | 165 | enum ctx_state prev_state = exception_enter(); |
David S. Miller | e2fdd7f | 2008-04-29 02:38:50 -0700 | [diff] [blame] | 166 | unsigned long flags; |
| 167 | |
| 168 | if (user_mode(regs)) { |
| 169 | bad_trap(regs, trap_level); |
Kirill Tkhai | 812cb83 | 2013-09-14 16:02:11 +0400 | [diff] [blame] | 170 | goto out; |
David S. Miller | e2fdd7f | 2008-04-29 02:38:50 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | flushw_all(); |
| 174 | |
| 175 | local_irq_save(flags); |
| 176 | kgdb_handle_exception(0x172, SIGTRAP, 0, regs); |
| 177 | local_irq_restore(flags); |
Kirill Tkhai | 812cb83 | 2013-09-14 16:02:11 +0400 | [diff] [blame] | 178 | out: |
| 179 | exception_exit(prev_state); |
David S. Miller | e2fdd7f | 2008-04-29 02:38:50 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | int kgdb_arch_init(void) |
| 183 | { |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | void kgdb_arch_exit(void) |
| 188 | { |
| 189 | } |
| 190 | |
Jason Wessel | c75fbb0 | 2010-05-20 21:04:19 -0500 | [diff] [blame] | 191 | void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip) |
| 192 | { |
| 193 | regs->tpc = ip; |
| 194 | regs->tnpc = regs->tpc + 4; |
| 195 | } |
| 196 | |
David S. Miller | e2fdd7f | 2008-04-29 02:38:50 -0700 | [diff] [blame] | 197 | struct kgdb_arch arch_kgdb_ops = { |
| 198 | /* Breakpoint instruction: ta 0x72 */ |
| 199 | .gdb_bpt_instr = { 0x91, 0xd0, 0x20, 0x72 }, |
| 200 | }; |