1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * PowerPC backend to the KGDB stub. 4 * 5 * 1998 (c) Michael AK Tesch (tesch@cs.wisc.edu) 6 * Copyright (C) 2003 Timesys Corporation. 7 * Copyright (C) 2004-2006 MontaVista Software, Inc. 8 * PPC64 Mods (C) 2005 Frank Rowand (frowand@mvista.com) 9 * PPC32 support restored by Vitaly Wool <vwool@ru.mvista.com> and 10 * Sergei Shtylyov <sshtylyov@ru.mvista.com> 11 * Copyright (C) 2007-2008 Wind River Systems, Inc. 12 */ 13 14 #include <linux/kernel.h> 15 #include <linux/kgdb.h> 16 #include <linux/smp.h> 17 #include <linux/signal.h> 18 #include <linux/ptrace.h> 19 #include <linux/kdebug.h> 20 #include <asm/current.h> 21 #include <asm/processor.h> 22 #include <asm/machdep.h> 23 #include <asm/debug.h> 24 #include <asm/text-patching.h> 25 #include <linux/slab.h> 26 #include <asm/inst.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 #ifdef CONFIG_BOOKE 49 { 0x2002, 0x05 /* SIGTRAP */ }, /* debug */ 50 #if defined(CONFIG_PPC_85xx) 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 */ 56 { 0x2060, 0x0e /* SIGILL */ }, /* performance monitor */ 57 { 0x2900, 0x08 /* SIGFPE */ }, /* apu unavailable */ 58 { 0x3100, 0x0e /* SIGALRM */ }, /* fixed interval timer */ 59 { 0x3200, 0x02 /* SIGINT */ }, /* watchdog */ 60 #else /* ! CONFIG_PPC_85xx */ 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 /* !CONFIG_BOOKE */ 68 { 0x0d00, 0x05 /* SIGTRAP */ }, /* single-step */ 69 #if defined(CONFIG_PPC_8xx) 70 { 0x1000, 0x04 /* SIGILL */ }, /* software emulation */ 71 #else /* ! CONFIG_PPC_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 /** 104 * kgdb_skipexception - Bail out of KGDB when we've been triggered. 105 * @exception: Exception vector number 106 * @regs: Current &struct pt_regs. 107 * 108 * On some architectures we need to skip a breakpoint exception when 109 * it occurs after a breakpoint has been removed. 110 * 111 * Return: return %1 if the breakpoint for this address has been removed, 112 * otherwise return %0 113 */ 114 int kgdb_skipexception(int exception, struct pt_regs *regs) 115 { 116 return kgdb_isremovedbreak(regs->nip); 117 } 118 119 static int kgdb_debugger_ipi(struct pt_regs *regs) 120 { 121 kgdb_nmicallback(raw_smp_processor_id(), regs); 122 return 0; 123 } 124 125 #ifdef CONFIG_SMP 126 void kgdb_roundup_cpus(void) 127 { 128 smp_send_debugger_break(); 129 } 130 #endif 131 132 /* KGDB functions to use existing PowerPC64 hooks. */ 133 static int kgdb_debugger(struct pt_regs *regs) 134 { 135 return !kgdb_handle_exception(1, computeSignal(TRAP(regs)), 136 DIE_OOPS, regs); 137 } 138 139 static int kgdb_handle_breakpoint(struct pt_regs *regs) 140 { 141 if (user_mode(regs)) 142 return 0; 143 144 if (kgdb_handle_exception(1, SIGTRAP, 0, regs) != 0) 145 return 0; 146 147 if (*(u32 *)regs->nip == BREAK_INSTR) 148 regs_add_return_ip(regs, BREAK_INSTR_SIZE); 149 150 return 1; 151 } 152 153 static int kgdb_singlestep(struct pt_regs *regs) 154 { 155 if (user_mode(regs)) 156 return 0; 157 158 kgdb_handle_exception(0, SIGTRAP, 0, regs); 159 160 return 1; 161 } 162 163 static int kgdb_iabr_match(struct pt_regs *regs) 164 { 165 if (user_mode(regs)) 166 return 0; 167 168 if (kgdb_handle_exception(0, computeSignal(TRAP(regs)), 0, regs) != 0) 169 return 0; 170 return 1; 171 } 172 173 static int kgdb_break_match(struct pt_regs *regs) 174 { 175 if (user_mode(regs)) 176 return 0; 177 178 if (kgdb_handle_exception(0, computeSignal(TRAP(regs)), 0, regs) != 0) 179 return 0; 180 return 1; 181 } 182 183 #define PACK64(ptr, src) do { *(ptr++) = (src); } while (0) 184 185 #define PACK32(ptr, src) do { \ 186 u32 *ptr32; \ 187 ptr32 = (u32 *)ptr; \ 188 *(ptr32++) = (src); \ 189 ptr = (unsigned long *)ptr32; \ 190 } while (0) 191 192 void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p) 193 { 194 struct pt_regs *regs = (struct pt_regs *)(p->thread.ksp + 195 STACK_INT_FRAME_REGS); 196 unsigned long *ptr = gdb_regs; 197 int reg; 198 199 memset(gdb_regs, 0, NUMREGBYTES); 200 201 /* Regs GPR0-2 */ 202 for (reg = 0; reg < 3; reg++) 203 PACK64(ptr, regs->gpr[reg]); 204 205 /* Regs GPR3-13 are caller saved, not in regs->gpr[] */ 206 ptr += 11; 207 208 /* Regs GPR14-31 */ 209 for (reg = 14; reg < 32; reg++) 210 PACK64(ptr, regs->gpr[reg]); 211 212 #ifdef CONFIG_PPC_85xx 213 #ifdef CONFIG_SPE 214 for (reg = 0; reg < 32; reg++) 215 PACK64(ptr, p->thread.evr[reg]); 216 #else 217 ptr += 32; 218 #endif 219 #else 220 /* fp registers not used by kernel, leave zero */ 221 ptr += 32 * 8 / sizeof(long); 222 #endif 223 224 PACK64(ptr, regs->nip); 225 PACK64(ptr, regs->msr); 226 PACK32(ptr, regs->ccr); 227 PACK64(ptr, regs->link); 228 PACK64(ptr, regs->ctr); 229 PACK32(ptr, regs->xer); 230 231 BUG_ON((unsigned long)ptr > 232 (unsigned long)(((void *)gdb_regs) + NUMREGBYTES)); 233 } 234 235 #define GDB_SIZEOF_REG sizeof(unsigned long) 236 #define GDB_SIZEOF_REG_U32 sizeof(u32) 237 238 #ifdef CONFIG_PPC_85xx 239 #define GDB_SIZEOF_FLOAT_REG sizeof(unsigned long) 240 #else 241 #define GDB_SIZEOF_FLOAT_REG sizeof(u64) 242 #endif 243 244 struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = 245 { 246 { "r0", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[0]) }, 247 { "r1", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[1]) }, 248 { "r2", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[2]) }, 249 { "r3", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[3]) }, 250 { "r4", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[4]) }, 251 { "r5", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[5]) }, 252 { "r6", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[6]) }, 253 { "r7", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[7]) }, 254 { "r8", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[8]) }, 255 { "r9", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[9]) }, 256 { "r10", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[10]) }, 257 { "r11", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[11]) }, 258 { "r12", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[12]) }, 259 { "r13", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[13]) }, 260 { "r14", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[14]) }, 261 { "r15", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[15]) }, 262 { "r16", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[16]) }, 263 { "r17", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[17]) }, 264 { "r18", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[18]) }, 265 { "r19", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[19]) }, 266 { "r20", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[20]) }, 267 { "r21", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[21]) }, 268 { "r22", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[22]) }, 269 { "r23", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[23]) }, 270 { "r24", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[24]) }, 271 { "r25", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[25]) }, 272 { "r26", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[26]) }, 273 { "r27", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[27]) }, 274 { "r28", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[28]) }, 275 { "r29", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[29]) }, 276 { "r30", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[30]) }, 277 { "r31", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[31]) }, 278 279 { "f0", GDB_SIZEOF_FLOAT_REG, 0 }, 280 { "f1", GDB_SIZEOF_FLOAT_REG, 1 }, 281 { "f2", GDB_SIZEOF_FLOAT_REG, 2 }, 282 { "f3", GDB_SIZEOF_FLOAT_REG, 3 }, 283 { "f4", GDB_SIZEOF_FLOAT_REG, 4 }, 284 { "f5", GDB_SIZEOF_FLOAT_REG, 5 }, 285 { "f6", GDB_SIZEOF_FLOAT_REG, 6 }, 286 { "f7", GDB_SIZEOF_FLOAT_REG, 7 }, 287 { "f8", GDB_SIZEOF_FLOAT_REG, 8 }, 288 { "f9", GDB_SIZEOF_FLOAT_REG, 9 }, 289 { "f10", GDB_SIZEOF_FLOAT_REG, 10 }, 290 { "f11", GDB_SIZEOF_FLOAT_REG, 11 }, 291 { "f12", GDB_SIZEOF_FLOAT_REG, 12 }, 292 { "f13", GDB_SIZEOF_FLOAT_REG, 13 }, 293 { "f14", GDB_SIZEOF_FLOAT_REG, 14 }, 294 { "f15", GDB_SIZEOF_FLOAT_REG, 15 }, 295 { "f16", GDB_SIZEOF_FLOAT_REG, 16 }, 296 { "f17", GDB_SIZEOF_FLOAT_REG, 17 }, 297 { "f18", GDB_SIZEOF_FLOAT_REG, 18 }, 298 { "f19", GDB_SIZEOF_FLOAT_REG, 19 }, 299 { "f20", GDB_SIZEOF_FLOAT_REG, 20 }, 300 { "f21", GDB_SIZEOF_FLOAT_REG, 21 }, 301 { "f22", GDB_SIZEOF_FLOAT_REG, 22 }, 302 { "f23", GDB_SIZEOF_FLOAT_REG, 23 }, 303 { "f24", GDB_SIZEOF_FLOAT_REG, 24 }, 304 { "f25", GDB_SIZEOF_FLOAT_REG, 25 }, 305 { "f26", GDB_SIZEOF_FLOAT_REG, 26 }, 306 { "f27", GDB_SIZEOF_FLOAT_REG, 27 }, 307 { "f28", GDB_SIZEOF_FLOAT_REG, 28 }, 308 { "f29", GDB_SIZEOF_FLOAT_REG, 29 }, 309 { "f30", GDB_SIZEOF_FLOAT_REG, 30 }, 310 { "f31", GDB_SIZEOF_FLOAT_REG, 31 }, 311 312 { "pc", GDB_SIZEOF_REG, offsetof(struct pt_regs, nip) }, 313 { "msr", GDB_SIZEOF_REG, offsetof(struct pt_regs, msr) }, 314 { "cr", GDB_SIZEOF_REG_U32, offsetof(struct pt_regs, ccr) }, 315 { "lr", GDB_SIZEOF_REG, offsetof(struct pt_regs, link) }, 316 { "ctr", GDB_SIZEOF_REG_U32, offsetof(struct pt_regs, ctr) }, 317 { "xer", GDB_SIZEOF_REG, offsetof(struct pt_regs, xer) }, 318 }; 319 320 char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs) 321 { 322 if (regno >= DBG_MAX_REG_NUM || regno < 0) 323 return NULL; 324 325 if (regno < 32 || regno >= 64) 326 /* First 0 -> 31 gpr registers*/ 327 /* pc, msr, ls... registers 64 -> 69 */ 328 memcpy(mem, (void *)regs + dbg_reg_def[regno].offset, 329 dbg_reg_def[regno].size); 330 331 if (regno >= 32 && regno < 64) { 332 /* FP registers 32 -> 63 */ 333 #if defined(CONFIG_PPC_85xx) && defined(CONFIG_SPE) 334 if (current) 335 memcpy(mem, ¤t->thread.evr[regno-32], 336 dbg_reg_def[regno].size); 337 #else 338 /* fp registers not used by kernel, leave zero */ 339 memset(mem, 0, dbg_reg_def[regno].size); 340 #endif 341 } 342 343 return dbg_reg_def[regno].name; 344 } 345 346 int dbg_set_reg(int regno, void *mem, struct pt_regs *regs) 347 { 348 if (regno >= DBG_MAX_REG_NUM || regno < 0) 349 return -EINVAL; 350 351 if (regno < 32 || regno >= 64) 352 /* First 0 -> 31 gpr registers*/ 353 /* pc, msr, ls... registers 64 -> 69 */ 354 memcpy((void *)regs + dbg_reg_def[regno].offset, mem, 355 dbg_reg_def[regno].size); 356 357 if (regno >= 32 && regno < 64) { 358 /* FP registers 32 -> 63 */ 359 #if defined(CONFIG_PPC_85xx) && defined(CONFIG_SPE) 360 memcpy(¤t->thread.evr[regno-32], mem, 361 dbg_reg_def[regno].size); 362 #else 363 /* fp registers not used by kernel, leave zero */ 364 return 0; 365 #endif 366 } 367 368 return 0; 369 } 370 371 void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc) 372 { 373 regs_set_return_ip(regs, pc); 374 } 375 376 /* 377 * This function does PowerPC specific processing for interfacing to gdb. 378 */ 379 int kgdb_arch_handle_exception(int vector, int signo, int err_code, 380 char *remcom_in_buffer, char *remcom_out_buffer, 381 struct pt_regs *linux_regs) 382 { 383 char *ptr = &remcom_in_buffer[1]; 384 unsigned long addr; 385 386 switch (remcom_in_buffer[0]) { 387 /* 388 * sAA..AA Step one instruction from AA..AA 389 * This will return an error to gdb .. 390 */ 391 case 's': 392 case 'c': 393 /* handle the optional parameter */ 394 if (kgdb_hex2long(&ptr, &addr)) 395 regs_set_return_ip(linux_regs, addr); 396 397 atomic_set(&kgdb_cpu_doing_single_step, -1); 398 /* set the trace bit if we're stepping */ 399 if (remcom_in_buffer[0] == 's') { 400 #ifdef CONFIG_PPC_ADV_DEBUG_REGS 401 mtspr(SPRN_DBCR0, 402 mfspr(SPRN_DBCR0) | DBCR0_IC | DBCR0_IDM); 403 regs_set_return_msr(linux_regs, linux_regs->msr | MSR_DE); 404 #else 405 regs_set_return_msr(linux_regs, linux_regs->msr | MSR_SE); 406 #endif 407 atomic_set(&kgdb_cpu_doing_single_step, 408 raw_smp_processor_id()); 409 } 410 return 0; 411 } 412 413 return -1; 414 } 415 416 int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt) 417 { 418 u32 instr, *addr = (u32 *)bpt->bpt_addr; 419 int err; 420 421 err = get_kernel_nofault(instr, addr); 422 if (err) 423 return err; 424 425 err = patch_instruction(addr, ppc_inst(BREAK_INSTR)); 426 if (err) 427 return -EFAULT; 428 429 *(u32 *)bpt->saved_instr = instr; 430 431 return 0; 432 } 433 434 int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt) 435 { 436 int err; 437 unsigned int instr = *(unsigned int *)bpt->saved_instr; 438 u32 *addr = (u32 *)bpt->bpt_addr; 439 440 err = patch_instruction(addr, ppc_inst(instr)); 441 if (err) 442 return -EFAULT; 443 444 return 0; 445 } 446 447 /* 448 * Global data 449 */ 450 const struct kgdb_arch arch_kgdb_ops; 451 452 static int kgdb_not_implemented(struct pt_regs *regs) 453 { 454 return 0; 455 } 456 457 static void *old__debugger_ipi; 458 static void *old__debugger; 459 static void *old__debugger_bpt; 460 static void *old__debugger_sstep; 461 static void *old__debugger_iabr_match; 462 static void *old__debugger_break_match; 463 static void *old__debugger_fault_handler; 464 465 int kgdb_arch_init(void) 466 { 467 old__debugger_ipi = __debugger_ipi; 468 old__debugger = __debugger; 469 old__debugger_bpt = __debugger_bpt; 470 old__debugger_sstep = __debugger_sstep; 471 old__debugger_iabr_match = __debugger_iabr_match; 472 old__debugger_break_match = __debugger_break_match; 473 old__debugger_fault_handler = __debugger_fault_handler; 474 475 __debugger_ipi = kgdb_debugger_ipi; 476 __debugger = kgdb_debugger; 477 __debugger_bpt = kgdb_handle_breakpoint; 478 __debugger_sstep = kgdb_singlestep; 479 __debugger_iabr_match = kgdb_iabr_match; 480 __debugger_break_match = kgdb_break_match; 481 __debugger_fault_handler = kgdb_not_implemented; 482 483 return 0; 484 } 485 486 void kgdb_arch_exit(void) 487 { 488 __debugger_ipi = old__debugger_ipi; 489 __debugger = old__debugger; 490 __debugger_bpt = old__debugger_bpt; 491 __debugger_sstep = old__debugger_sstep; 492 __debugger_iabr_match = old__debugger_iabr_match; 493 __debugger_break_match = old__debugger_break_match; 494 __debugger_fault_handler = old__debugger_fault_handler; 495 } 496