Jason Wessel | 17ce452 | 2008-07-23 11:30:15 -0500 | [diff] [blame] | 1 | /* |
| 2 | * PowerPC backend to the KGDB stub. |
| 3 | * |
| 4 | * 1998 (c) Michael AK Tesch (tesch@cs.wisc.edu) |
| 5 | * Copyright (C) 2003 Timesys Corporation. |
| 6 | * Copyright (C) 2004-2006 MontaVista Software, Inc. |
| 7 | * PPC64 Mods (C) 2005 Frank Rowand (frowand@mvista.com) |
| 8 | * PPC32 support restored by Vitaly Wool <vwool@ru.mvista.com> and |
| 9 | * Sergei Shtylyov <sshtylyov@ru.mvista.com> |
| 10 | * Copyright (C) 2007-2008 Wind River Systems, Inc. |
| 11 | * |
| 12 | * This file is licensed under the terms of the GNU General Public License |
| 13 | * version 2. This program as licensed "as is" without any warranty of any |
| 14 | * kind, whether express or implied. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/kgdb.h> |
| 20 | #include <linux/smp.h> |
| 21 | #include <linux/signal.h> |
| 22 | #include <linux/ptrace.h> |
Jason Wessel | ba797b2 | 2010-05-20 21:04:25 -0500 | [diff] [blame] | 23 | #include <linux/kdebug.h> |
Jason Wessel | 17ce452 | 2008-07-23 11:30:15 -0500 | [diff] [blame] | 24 | #include <asm/current.h> |
| 25 | #include <asm/processor.h> |
| 26 | #include <asm/machdep.h> |
| 27 | |
| 28 | /* |
| 29 | * This table contains the mapping between PowerPC hardware trap types, and |
| 30 | * signals, which are primarily what GDB understands. GDB and the kernel |
| 31 | * don't always agree on values, so we use constants taken from gdb-6.2. |
| 32 | */ |
| 33 | static struct hard_trap_info |
| 34 | { |
| 35 | unsigned int tt; /* Trap type code for powerpc */ |
| 36 | unsigned char signo; /* Signal that we map this trap into */ |
| 37 | } hard_trap_info[] = { |
| 38 | { 0x0100, 0x02 /* SIGINT */ }, /* system reset */ |
| 39 | { 0x0200, 0x0b /* SIGSEGV */ }, /* machine check */ |
| 40 | { 0x0300, 0x0b /* SIGSEGV */ }, /* data access */ |
| 41 | { 0x0400, 0x0b /* SIGSEGV */ }, /* instruction access */ |
| 42 | { 0x0500, 0x02 /* SIGINT */ }, /* external interrupt */ |
| 43 | { 0x0600, 0x0a /* SIGBUS */ }, /* alignment */ |
| 44 | { 0x0700, 0x05 /* SIGTRAP */ }, /* program check */ |
| 45 | { 0x0800, 0x08 /* SIGFPE */ }, /* fp unavailable */ |
| 46 | { 0x0900, 0x0e /* SIGALRM */ }, /* decrementer */ |
| 47 | { 0x0c00, 0x14 /* SIGCHLD */ }, /* system call */ |
| 48 | #if defined(CONFIG_40x) || defined(CONFIG_BOOKE) |
| 49 | { 0x2002, 0x05 /* SIGTRAP */ }, /* debug */ |
| 50 | #if defined(CONFIG_FSL_BOOKE) |
| 51 | { 0x2010, 0x08 /* SIGFPE */ }, /* spe unavailable */ |
| 52 | { 0x2020, 0x08 /* SIGFPE */ }, /* spe unavailable */ |
| 53 | { 0x2030, 0x08 /* SIGFPE */ }, /* spe fp data */ |
| 54 | { 0x2040, 0x08 /* SIGFPE */ }, /* spe fp data */ |
| 55 | { 0x2050, 0x08 /* SIGFPE */ }, /* spe fp round */ |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 56 | { 0x2060, 0x0e /* SIGILL */ }, /* performance monitor */ |
Jason Wessel | 17ce452 | 2008-07-23 11:30:15 -0500 | [diff] [blame] | 57 | { 0x2900, 0x08 /* SIGFPE */ }, /* apu unavailable */ |
| 58 | { 0x3100, 0x0e /* SIGALRM */ }, /* fixed interval timer */ |
| 59 | { 0x3200, 0x02 /* SIGINT */ }, /* watchdog */ |
| 60 | #else /* ! CONFIG_FSL_BOOKE */ |
| 61 | { 0x1000, 0x0e /* SIGALRM */ }, /* prog interval timer */ |
| 62 | { 0x1010, 0x0e /* SIGALRM */ }, /* fixed interval timer */ |
| 63 | { 0x1020, 0x02 /* SIGINT */ }, /* watchdog */ |
| 64 | { 0x2010, 0x08 /* SIGFPE */ }, /* fp unavailable */ |
| 65 | { 0x2020, 0x08 /* SIGFPE */ }, /* ap unavailable */ |
| 66 | #endif |
| 67 | #else /* ! (defined(CONFIG_40x) || defined(CONFIG_BOOKE)) */ |
| 68 | { 0x0d00, 0x05 /* SIGTRAP */ }, /* single-step */ |
| 69 | #if defined(CONFIG_8xx) |
| 70 | { 0x1000, 0x04 /* SIGILL */ }, /* software emulation */ |
| 71 | #else /* ! CONFIG_8xx */ |
| 72 | { 0x0f00, 0x04 /* SIGILL */ }, /* performance monitor */ |
| 73 | { 0x0f20, 0x08 /* SIGFPE */ }, /* altivec unavailable */ |
| 74 | { 0x1300, 0x05 /* SIGTRAP */ }, /* instruction address break */ |
| 75 | #if defined(CONFIG_PPC64) |
| 76 | { 0x1200, 0x05 /* SIGILL */ }, /* system error */ |
| 77 | { 0x1500, 0x04 /* SIGILL */ }, /* soft patch */ |
| 78 | { 0x1600, 0x04 /* SIGILL */ }, /* maintenance */ |
| 79 | { 0x1700, 0x08 /* SIGFPE */ }, /* altivec assist */ |
| 80 | { 0x1800, 0x04 /* SIGILL */ }, /* thermal */ |
| 81 | #else /* ! CONFIG_PPC64 */ |
| 82 | { 0x1400, 0x02 /* SIGINT */ }, /* SMI */ |
| 83 | { 0x1600, 0x08 /* SIGFPE */ }, /* altivec assist */ |
| 84 | { 0x1700, 0x04 /* SIGILL */ }, /* TAU */ |
| 85 | { 0x2000, 0x05 /* SIGTRAP */ }, /* run mode */ |
| 86 | #endif |
| 87 | #endif |
| 88 | #endif |
| 89 | { 0x0000, 0x00 } /* Must be last */ |
| 90 | }; |
| 91 | |
| 92 | static int computeSignal(unsigned int tt) |
| 93 | { |
| 94 | struct hard_trap_info *ht; |
| 95 | |
| 96 | for (ht = hard_trap_info; ht->tt && ht->signo; ht++) |
| 97 | if (ht->tt == tt) |
| 98 | return ht->signo; |
| 99 | |
| 100 | return SIGHUP; /* default for things we don't know about */ |
| 101 | } |
| 102 | |
| 103 | static int kgdb_call_nmi_hook(struct pt_regs *regs) |
| 104 | { |
| 105 | kgdb_nmicallback(raw_smp_processor_id(), regs); |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | #ifdef CONFIG_SMP |
| 110 | void kgdb_roundup_cpus(unsigned long flags) |
| 111 | { |
| 112 | smp_send_debugger_break(MSG_ALL_BUT_SELF); |
| 113 | } |
| 114 | #endif |
| 115 | |
| 116 | /* KGDB functions to use existing PowerPC64 hooks. */ |
| 117 | static int kgdb_debugger(struct pt_regs *regs) |
| 118 | { |
Jason Wessel | ba797b2 | 2010-05-20 21:04:25 -0500 | [diff] [blame] | 119 | return !kgdb_handle_exception(1, computeSignal(TRAP(regs)), |
| 120 | DIE_OOPS, regs); |
Jason Wessel | 17ce452 | 2008-07-23 11:30:15 -0500 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static int kgdb_handle_breakpoint(struct pt_regs *regs) |
| 124 | { |
| 125 | if (user_mode(regs)) |
| 126 | return 0; |
| 127 | |
Jason Wessel | ba797b2 | 2010-05-20 21:04:25 -0500 | [diff] [blame] | 128 | if (kgdb_handle_exception(1, SIGTRAP, 0, regs) != 0) |
Jason Wessel | 17ce452 | 2008-07-23 11:30:15 -0500 | [diff] [blame] | 129 | return 0; |
| 130 | |
| 131 | if (*(u32 *) (regs->nip) == *(u32 *) (&arch_kgdb_ops.gdb_bpt_instr)) |
Michal Simek | 3f0a55e | 2010-08-05 09:22:22 -0500 | [diff] [blame^] | 132 | regs->nip += BREAK_INSTR_SIZE; |
Jason Wessel | 17ce452 | 2008-07-23 11:30:15 -0500 | [diff] [blame] | 133 | |
| 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | static int kgdb_singlestep(struct pt_regs *regs) |
| 138 | { |
| 139 | struct thread_info *thread_info, *exception_thread_info; |
| 140 | |
| 141 | if (user_mode(regs)) |
| 142 | return 0; |
| 143 | |
| 144 | /* |
| 145 | * On Book E and perhaps other processsors, singlestep is handled on |
| 146 | * the critical exception stack. This causes current_thread_info() |
| 147 | * to fail, since it it locates the thread_info by masking off |
| 148 | * the low bits of the current stack pointer. We work around |
| 149 | * this issue by copying the thread_info from the kernel stack |
| 150 | * before calling kgdb_handle_exception, and copying it back |
| 151 | * afterwards. On most processors the copy is avoided since |
| 152 | * exception_thread_info == thread_info. |
| 153 | */ |
| 154 | thread_info = (struct thread_info *)(regs->gpr[1] & ~(THREAD_SIZE-1)); |
| 155 | exception_thread_info = current_thread_info(); |
| 156 | |
| 157 | if (thread_info != exception_thread_info) |
| 158 | memcpy(exception_thread_info, thread_info, sizeof *thread_info); |
| 159 | |
| 160 | kgdb_handle_exception(0, SIGTRAP, 0, regs); |
| 161 | |
| 162 | if (thread_info != exception_thread_info) |
| 163 | memcpy(thread_info, exception_thread_info, sizeof *thread_info); |
| 164 | |
| 165 | return 1; |
| 166 | } |
| 167 | |
| 168 | static int kgdb_iabr_match(struct pt_regs *regs) |
| 169 | { |
| 170 | if (user_mode(regs)) |
| 171 | return 0; |
| 172 | |
| 173 | if (kgdb_handle_exception(0, computeSignal(TRAP(regs)), 0, regs) != 0) |
| 174 | return 0; |
| 175 | return 1; |
| 176 | } |
| 177 | |
| 178 | static int kgdb_dabr_match(struct pt_regs *regs) |
| 179 | { |
| 180 | if (user_mode(regs)) |
| 181 | return 0; |
| 182 | |
| 183 | if (kgdb_handle_exception(0, computeSignal(TRAP(regs)), 0, regs) != 0) |
| 184 | return 0; |
| 185 | return 1; |
| 186 | } |
| 187 | |
| 188 | #define PACK64(ptr, src) do { *(ptr++) = (src); } while (0) |
| 189 | |
| 190 | #define PACK32(ptr, src) do { \ |
| 191 | u32 *ptr32; \ |
| 192 | ptr32 = (u32 *)ptr; \ |
| 193 | *(ptr32++) = (src); \ |
| 194 | ptr = (unsigned long *)ptr32; \ |
| 195 | } while (0) |
| 196 | |
| 197 | |
| 198 | void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs) |
| 199 | { |
| 200 | unsigned long *ptr = gdb_regs; |
| 201 | int reg; |
| 202 | |
| 203 | memset(gdb_regs, 0, NUMREGBYTES); |
| 204 | |
| 205 | for (reg = 0; reg < 32; reg++) |
| 206 | PACK64(ptr, regs->gpr[reg]); |
| 207 | |
| 208 | #ifdef CONFIG_FSL_BOOKE |
| 209 | #ifdef CONFIG_SPE |
| 210 | for (reg = 0; reg < 32; reg++) |
| 211 | PACK64(ptr, current->thread.evr[reg]); |
| 212 | #else |
| 213 | ptr += 32; |
| 214 | #endif |
| 215 | #else |
| 216 | /* fp registers not used by kernel, leave zero */ |
| 217 | ptr += 32 * 8 / sizeof(long); |
| 218 | #endif |
| 219 | |
| 220 | PACK64(ptr, regs->nip); |
| 221 | PACK64(ptr, regs->msr); |
| 222 | PACK32(ptr, regs->ccr); |
| 223 | PACK64(ptr, regs->link); |
| 224 | PACK64(ptr, regs->ctr); |
| 225 | PACK32(ptr, regs->xer); |
| 226 | |
| 227 | BUG_ON((unsigned long)ptr > |
| 228 | (unsigned long)(((void *)gdb_regs) + NUMREGBYTES)); |
| 229 | } |
| 230 | |
| 231 | void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p) |
| 232 | { |
| 233 | struct pt_regs *regs = (struct pt_regs *)(p->thread.ksp + |
| 234 | STACK_FRAME_OVERHEAD); |
| 235 | unsigned long *ptr = gdb_regs; |
| 236 | int reg; |
| 237 | |
| 238 | memset(gdb_regs, 0, NUMREGBYTES); |
| 239 | |
| 240 | /* Regs GPR0-2 */ |
| 241 | for (reg = 0; reg < 3; reg++) |
| 242 | PACK64(ptr, regs->gpr[reg]); |
| 243 | |
| 244 | /* Regs GPR3-13 are caller saved, not in regs->gpr[] */ |
| 245 | ptr += 11; |
| 246 | |
| 247 | /* Regs GPR14-31 */ |
| 248 | for (reg = 14; reg < 32; reg++) |
| 249 | PACK64(ptr, regs->gpr[reg]); |
| 250 | |
| 251 | #ifdef CONFIG_FSL_BOOKE |
| 252 | #ifdef CONFIG_SPE |
| 253 | for (reg = 0; reg < 32; reg++) |
| 254 | PACK64(ptr, p->thread.evr[reg]); |
| 255 | #else |
| 256 | ptr += 32; |
| 257 | #endif |
| 258 | #else |
| 259 | /* fp registers not used by kernel, leave zero */ |
| 260 | ptr += 32 * 8 / sizeof(long); |
| 261 | #endif |
| 262 | |
| 263 | PACK64(ptr, regs->nip); |
| 264 | PACK64(ptr, regs->msr); |
| 265 | PACK32(ptr, regs->ccr); |
| 266 | PACK64(ptr, regs->link); |
| 267 | PACK64(ptr, regs->ctr); |
| 268 | PACK32(ptr, regs->xer); |
| 269 | |
| 270 | BUG_ON((unsigned long)ptr > |
| 271 | (unsigned long)(((void *)gdb_regs) + NUMREGBYTES)); |
| 272 | } |
| 273 | |
| 274 | #define UNPACK64(dest, ptr) do { dest = *(ptr++); } while (0) |
| 275 | |
| 276 | #define UNPACK32(dest, ptr) do { \ |
| 277 | u32 *ptr32; \ |
| 278 | ptr32 = (u32 *)ptr; \ |
| 279 | dest = *(ptr32++); \ |
| 280 | ptr = (unsigned long *)ptr32; \ |
| 281 | } while (0) |
| 282 | |
| 283 | void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs) |
| 284 | { |
| 285 | unsigned long *ptr = gdb_regs; |
| 286 | int reg; |
Jason Wessel | 17ce452 | 2008-07-23 11:30:15 -0500 | [diff] [blame] | 287 | |
| 288 | for (reg = 0; reg < 32; reg++) |
| 289 | UNPACK64(regs->gpr[reg], ptr); |
| 290 | |
| 291 | #ifdef CONFIG_FSL_BOOKE |
| 292 | #ifdef CONFIG_SPE |
| 293 | for (reg = 0; reg < 32; reg++) |
| 294 | UNPACK64(current->thread.evr[reg], ptr); |
| 295 | #else |
| 296 | ptr += 32; |
| 297 | #endif |
| 298 | #else |
| 299 | /* fp registers not used by kernel, leave zero */ |
| 300 | ptr += 32 * 8 / sizeof(int); |
| 301 | #endif |
| 302 | |
| 303 | UNPACK64(regs->nip, ptr); |
| 304 | UNPACK64(regs->msr, ptr); |
| 305 | UNPACK32(regs->ccr, ptr); |
| 306 | UNPACK64(regs->link, ptr); |
| 307 | UNPACK64(regs->ctr, ptr); |
| 308 | UNPACK32(regs->xer, ptr); |
| 309 | |
| 310 | BUG_ON((unsigned long)ptr > |
| 311 | (unsigned long)(((void *)gdb_regs) + NUMREGBYTES)); |
| 312 | } |
| 313 | |
Jason Wessel | dcc7871 | 2010-05-20 21:04:21 -0500 | [diff] [blame] | 314 | void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc) |
| 315 | { |
| 316 | regs->nip = pc; |
| 317 | } |
| 318 | |
Jason Wessel | 17ce452 | 2008-07-23 11:30:15 -0500 | [diff] [blame] | 319 | /* |
| 320 | * This function does PowerPC specific procesing for interfacing to gdb. |
| 321 | */ |
| 322 | int kgdb_arch_handle_exception(int vector, int signo, int err_code, |
| 323 | char *remcom_in_buffer, char *remcom_out_buffer, |
| 324 | struct pt_regs *linux_regs) |
| 325 | { |
| 326 | char *ptr = &remcom_in_buffer[1]; |
| 327 | unsigned long addr; |
| 328 | |
| 329 | switch (remcom_in_buffer[0]) { |
| 330 | /* |
| 331 | * sAA..AA Step one instruction from AA..AA |
| 332 | * This will return an error to gdb .. |
| 333 | */ |
| 334 | case 's': |
| 335 | case 'c': |
| 336 | /* handle the optional parameter */ |
| 337 | if (kgdb_hex2long(&ptr, &addr)) |
| 338 | linux_regs->nip = addr; |
| 339 | |
| 340 | atomic_set(&kgdb_cpu_doing_single_step, -1); |
| 341 | /* set the trace bit if we're stepping */ |
| 342 | if (remcom_in_buffer[0] == 's') { |
Dave Kleikamp | 172ae2e | 2010-02-08 11:50:57 +0000 | [diff] [blame] | 343 | #ifdef CONFIG_PPC_ADV_DEBUG_REGS |
Jason Wessel | 17ce452 | 2008-07-23 11:30:15 -0500 | [diff] [blame] | 344 | mtspr(SPRN_DBCR0, |
| 345 | mfspr(SPRN_DBCR0) | DBCR0_IC | DBCR0_IDM); |
| 346 | linux_regs->msr |= MSR_DE; |
| 347 | #else |
| 348 | linux_regs->msr |= MSR_SE; |
| 349 | #endif |
| 350 | kgdb_single_step = 1; |
Jason Wessel | d7161a6 | 2008-09-26 10:36:41 -0500 | [diff] [blame] | 351 | atomic_set(&kgdb_cpu_doing_single_step, |
| 352 | raw_smp_processor_id()); |
Jason Wessel | 17ce452 | 2008-07-23 11:30:15 -0500 | [diff] [blame] | 353 | } |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | return -1; |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | * Global data |
| 362 | */ |
| 363 | struct kgdb_arch arch_kgdb_ops = { |
| 364 | .gdb_bpt_instr = {0x7d, 0x82, 0x10, 0x08}, |
| 365 | }; |
| 366 | |
| 367 | static int kgdb_not_implemented(struct pt_regs *regs) |
| 368 | { |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static void *old__debugger_ipi; |
| 373 | static void *old__debugger; |
| 374 | static void *old__debugger_bpt; |
| 375 | static void *old__debugger_sstep; |
| 376 | static void *old__debugger_iabr_match; |
| 377 | static void *old__debugger_dabr_match; |
| 378 | static void *old__debugger_fault_handler; |
| 379 | |
| 380 | int kgdb_arch_init(void) |
| 381 | { |
| 382 | old__debugger_ipi = __debugger_ipi; |
| 383 | old__debugger = __debugger; |
| 384 | old__debugger_bpt = __debugger_bpt; |
| 385 | old__debugger_sstep = __debugger_sstep; |
| 386 | old__debugger_iabr_match = __debugger_iabr_match; |
| 387 | old__debugger_dabr_match = __debugger_dabr_match; |
| 388 | old__debugger_fault_handler = __debugger_fault_handler; |
| 389 | |
| 390 | __debugger_ipi = kgdb_call_nmi_hook; |
| 391 | __debugger = kgdb_debugger; |
| 392 | __debugger_bpt = kgdb_handle_breakpoint; |
| 393 | __debugger_sstep = kgdb_singlestep; |
| 394 | __debugger_iabr_match = kgdb_iabr_match; |
| 395 | __debugger_dabr_match = kgdb_dabr_match; |
| 396 | __debugger_fault_handler = kgdb_not_implemented; |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | void kgdb_arch_exit(void) |
| 402 | { |
| 403 | __debugger_ipi = old__debugger_ipi; |
| 404 | __debugger = old__debugger; |
| 405 | __debugger_bpt = old__debugger_bpt; |
| 406 | __debugger_sstep = old__debugger_sstep; |
| 407 | __debugger_iabr_match = old__debugger_iabr_match; |
| 408 | __debugger_dabr_match = old__debugger_dabr_match; |
| 409 | __debugger_fault_handler = old__debugger_fault_handler; |
| 410 | } |