[AVR32] Follow the rules when dealing with the OCD system

The current debug trap handling code does a number of things that are
illegal according to the AVR32 Architecture manual. Most importantly,
it may try to schedule from Debug Mode, thus clearing the D bit, which
can lead to "undefined behaviour".

It seems like this works in most cases, but several people have
observed somewhat unstable behaviour when debugging programs,
including soft lockups. So there's definitely something which is not
right with the existing code.

The new code will never schedule from Debug mode, it will always exit
Debug mode with a "retd" instruction, and if something not running in
Debug mode needs to do something debug-related (like doing a single
step), it will enter debug mode through a "breakpoint" instruction.
The monitor code will then return directly to user space, bypassing
its own saved registers if necessary (since we don't actually care
about the trapped context, only the one that came before.)

This adds three instructions to the common exception handling code,
including one branch. It does not touch super-hot paths like the TLB
miss handler.

Signed-off-by: Haavard Skinnemoen <hskinnemoen@atmel.com>

+331 -228
+2
arch/avr32/kernel/asm-offsets.c
··· 21 21 OFFSET(TI_flags, thread_info, flags); 22 22 OFFSET(TI_cpu, thread_info, cpu); 23 23 OFFSET(TI_preempt_count, thread_info, preempt_count); 24 + OFFSET(TI_rar_saved, thread_info, rar_saved); 25 + OFFSET(TI_rsr_saved, thread_info, rsr_saved); 24 26 OFFSET(TI_restart_block, thread_info, restart_block); 25 27 }
+164 -109
arch/avr32/kernel/entry-avr32b.S
··· 264 264 265 265 3: bld r1, TIF_BREAKPOINT 266 266 brcc syscall_exit_cont 267 - mfsr r3, SYSREG_TLBEHI 268 - lddsp r2, sp[REG_PC] 269 - andl r3, 0xff, COH 270 - lsl r3, 1 271 - sbr r3, 30 272 - sbr r3, 0 273 - mtdr OCD_BWA2A, r2 274 - mtdr OCD_BWC2A, r3 275 - rjmp syscall_exit_cont 276 - 267 + rjmp enter_monitor_mode 277 268 278 269 /* The slow path of the TLB miss handler */ 279 270 page_table_not_present: ··· 279 288 rjmp ret_from_exception 280 289 281 290 /* This function expects to find offending PC in SYSREG_RAR_EX */ 291 + .type save_full_context_ex, @function 292 + .align 2 282 293 save_full_context_ex: 294 + mfsr r11, SYSREG_RAR_EX 295 + sub r9, pc, . - debug_trampoline 283 296 mfsr r8, SYSREG_RSR_EX 297 + cp.w r9, r11 298 + breq 3f 284 299 mov r12, r8 285 300 andh r8, (MODE_MASK >> 16), COH 286 - mfsr r11, SYSREG_RAR_EX 287 301 brne 2f 288 302 289 303 1: pushm r11, r12 /* PC and SR */ ··· 298 302 2: sub r10, sp, -(FRAME_SIZE_FULL - REG_LR) 299 303 stdsp sp[4], r10 /* replace saved SP */ 300 304 rjmp 1b 305 + 306 + /* 307 + * The debug handler set up a trampoline to make us 308 + * automatically enter monitor mode upon return, but since 309 + * we're saving the full context, we must assume that the 310 + * exception handler might want to alter the return address 311 + * and/or status register. So we need to restore the original 312 + * context and enter monitor mode manually after the exception 313 + * has been handled. 314 + */ 315 + 3: get_thread_info r8 316 + ld.w r11, r8[TI_rar_saved] 317 + ld.w r12, r8[TI_rsr_saved] 318 + rjmp 1b 319 + .size save_full_context_ex, . - save_full_context_ex 301 320 302 321 /* Low-level exception handlers */ 303 322 handle_critical: ··· 450 439 ret_from_exception: 451 440 mask_interrupts 452 441 lddsp r4, sp[REG_SR] 442 + 453 443 andh r4, (MODE_MASK >> 16), COH 454 444 brne fault_resume_kernel 455 445 ··· 527 515 528 516 2: bld r1, TIF_BREAKPOINT 529 517 brcc fault_resume_user 530 - mfsr r3, SYSREG_TLBEHI 531 - lddsp r2, sp[REG_PC] 532 - andl r3, 0xff, COH 533 - lsl r3, 1 534 - sbr r3, 30 535 - sbr r3, 0 536 - mtdr OCD_BWA2A, r2 537 - mtdr OCD_BWC2A, r3 538 - rjmp fault_resume_user 518 + rjmp enter_monitor_mode 539 519 540 - /* If we get a debug trap from privileged context we end up here */ 541 - handle_debug_priv: 542 - /* Fix up LR and SP in regs. r1 contains the mode we came from */ 543 - mfsr r2, SYSREG_SR 544 - mov r3, r2 545 - bfins r2, r1, SYSREG_MODE_OFFSET, SYSREG_MODE_SIZE 546 - mtsr SYSREG_SR, r2 520 + .section .kprobes.text, "ax", @progbits 521 + .type handle_debug, @function 522 + handle_debug: 523 + sub sp, 4 /* r12_orig */ 524 + stmts --sp, r0-lr 525 + mfsr r8, SYSREG_RAR_DBG 526 + mfsr r9, SYSREG_RSR_DBG 527 + unmask_exceptions 528 + pushm r8-r9 529 + bfextu r9, r9, SYSREG_MODE_OFFSET, SYSREG_MODE_SIZE 530 + brne debug_fixup_regs 531 + 532 + .Ldebug_fixup_cont: 533 + #ifdef CONFIG_TRACE_IRQFLAGS 534 + rcall trace_hardirqs_off 535 + #endif 536 + mov r12, sp 537 + rcall do_debug 538 + mov sp, r12 539 + 540 + lddsp r2, sp[REG_SR] 541 + bfextu r3, r2, SYSREG_MODE_OFFSET, SYSREG_MODE_SIZE 542 + brne debug_resume_kernel 543 + 544 + get_thread_info r0 545 + ld.w r1, r0[TI_flags] 546 + mov r2, _TIF_DBGWORK_MASK 547 + tst r1, r2 548 + brne debug_exit_work 549 + 550 + bld r1, TIF_SINGLE_STEP 551 + brcc 1f 552 + mfdr r4, OCD_DC 553 + sbr r4, OCD_DC_SS_BIT 554 + mtdr OCD_DC, r4 555 + 556 + 1: popm r10,r11 557 + mask_exceptions 558 + mtsr SYSREG_RSR_DBG, r11 559 + mtsr SYSREG_RAR_DBG, r10 560 + #ifdef CONFIG_TRACE_IRQFLAGS 561 + rcall trace_hardirqs_on 562 + 1: 563 + #endif 564 + ldmts sp++, r0-lr 565 + sub sp, -4 566 + retd 567 + .size handle_debug, . - handle_debug 568 + 569 + /* Mode of the trapped context is in r9 */ 570 + .type debug_fixup_regs, @function 571 + debug_fixup_regs: 572 + mfsr r8, SYSREG_SR 573 + mov r10, r8 574 + bfins r8, r9, SYSREG_MODE_OFFSET, SYSREG_MODE_SIZE 575 + mtsr SYSREG_SR, r8 547 576 sub pc, -2 548 577 stdsp sp[REG_LR], lr 549 - mtsr SYSREG_SR, r3 578 + mtsr SYSREG_SR, r10 550 579 sub pc, -2 551 - sub r10, sp, -FRAME_SIZE_FULL 552 - stdsp sp[REG_SP], r10 553 - mov r12, sp 554 - rcall do_debug_priv 580 + sub r8, sp, -FRAME_SIZE_FULL 581 + stdsp sp[REG_SP], r8 582 + rjmp .Ldebug_fixup_cont 583 + .size debug_fixup_regs, . - debug_fixup_regs 555 584 556 - /* Now, put everything back */ 557 - ssrf SR_EM_BIT 585 + .type debug_resume_kernel, @function 586 + debug_resume_kernel: 587 + mask_exceptions 558 588 popm r10, r11 559 589 mtsr SYSREG_RAR_DBG, r10 560 590 mtsr SYSREG_RSR_DBG, r11 ··· 607 553 1: 608 554 #endif 609 555 mfsr r2, SYSREG_SR 610 - mov r3, r2 611 - bfins r2, r1, SYSREG_MODE_OFFSET, SYSREG_MODE_SIZE 556 + mov r1, r2 557 + bfins r2, r3, SYSREG_MODE_OFFSET, SYSREG_MODE_SIZE 612 558 mtsr SYSREG_SR, r2 613 559 sub pc, -2 614 560 popm lr 615 - mtsr SYSREG_SR, r3 561 + mtsr SYSREG_SR, r1 616 562 sub pc, -2 617 563 sub sp, -4 /* skip SP */ 618 564 popm r0-r12 619 565 sub sp, -4 620 566 retd 567 + .size debug_resume_kernel, . - debug_resume_kernel 621 568 569 + .type debug_exit_work, @function 570 + debug_exit_work: 622 571 /* 623 - * At this point, everything is masked, that is, interrupts, 624 - * exceptions and debugging traps. We might get called from 625 - * interrupt or exception context in some rare cases, but this 626 - * will be taken care of by do_debug(), so we're not going to 627 - * do a 100% correct context save here. 572 + * We must return from Monitor Mode using a retd, and we must 573 + * not schedule since that involves the D bit in SR getting 574 + * cleared by something other than the debug hardware. This 575 + * may cause undefined behaviour according to the Architecture 576 + * manual. 577 + * 578 + * So we fix up the return address and status and return to a 579 + * stub below in Exception mode. From there, we can follow the 580 + * normal exception return path. 581 + * 582 + * The real return address and status registers are stored on 583 + * the stack in the way the exception return path understands, 584 + * so no need to fix anything up there. 628 585 */ 629 - handle_debug: 630 - sub sp, 4 /* r12_orig */ 631 - stmts --sp, r0-lr 632 - mfsr r0, SYSREG_RAR_DBG 633 - mfsr r1, SYSREG_RSR_DBG 634 - #ifdef CONFIG_TRACE_IRQFLAGS 635 - rcall trace_hardirqs_off 636 - #endif 637 - unmask_exceptions 638 - stm --sp, r0, r1 639 - bfextu r1, r1, SYSREG_MODE_OFFSET, SYSREG_MODE_SIZE 640 - brne handle_debug_priv 641 - 642 - mov r12, sp 643 - rcall do_debug 644 - 645 - lddsp r10, sp[REG_SR] 646 - andh r10, (MODE_MASK >> 16), COH 647 - breq debug_resume_user 648 - 649 - debug_restore_all: 650 - popm r10,r11 651 - mask_exceptions 652 - mtsr SYSREG_RSR_DBG, r11 653 - mtsr SYSREG_RAR_DBG, r10 654 - #ifdef CONFIG_TRACE_IRQFLAGS 655 - bld r11, SYSREG_GM_OFFSET 656 - brcc 1f 657 - rcall trace_hardirqs_on 658 - 1: 659 - #endif 660 - ldmts sp++, r0-lr 661 - sub sp, -4 586 + sub r8, pc, . - fault_exit_work 587 + mtsr SYSREG_RAR_DBG, r8 588 + mov r9, 0 589 + orh r9, hi(SR_EM | SR_GM | MODE_EXCEPTION) 590 + mtsr SYSREG_RSR_DBG, r9 591 + sub pc, -2 662 592 retd 663 - 664 - debug_resume_user: 665 - get_thread_info r0 666 - mask_interrupts 667 - 668 - ld.w r1, r0[TI_flags] 669 - andl r1, _TIF_DBGWORK_MASK, COH 670 - breq debug_restore_all 671 - 672 - 1: bld r1, TIF_NEED_RESCHED 673 - brcc 2f 674 - unmask_interrupts 675 - rcall schedule 676 - mask_interrupts 677 - ld.w r1, r0[TI_flags] 678 - rjmp 1b 679 - 680 - 2: mov r2, _TIF_SIGPENDING | _TIF_RESTORE_SIGMASK 681 - tst r1, r2 682 - breq 3f 683 - unmask_interrupts 684 - mov r12, sp 685 - mov r11, r0 686 - rcall do_notify_resume 687 - mask_interrupts 688 - ld.w r1, r0[TI_flags] 689 - rjmp 1b 690 - 691 - 3: bld r1, TIF_SINGLE_STEP 692 - brcc debug_restore_all 693 - mfdr r2, OCD_DC 694 - sbr r2, OCD_DC_SS_BIT 695 - mtdr OCD_DC, r2 696 - rjmp debug_restore_all 593 + .size debug_exit_work, . - debug_exit_work 697 594 698 595 .set rsr_int0, SYSREG_RSR_INT0 699 596 .set rsr_int1, SYSREG_RSR_INT1 ··· 769 764 IRQ_LEVEL 1 770 765 IRQ_LEVEL 2 771 766 IRQ_LEVEL 3 767 + 768 + .section .kprobes.text, "ax", @progbits 769 + .type enter_monitor_mode, @function 770 + enter_monitor_mode: 771 + /* 772 + * We need to enter monitor mode to do a single step. The 773 + * monitor code will alter the return address so that we 774 + * return directly to the user instead of returning here. 775 + */ 776 + breakpoint 777 + rjmp breakpoint_failed 778 + 779 + .size enter_monitor_mode, . - enter_monitor_mode 780 + 781 + .type debug_trampoline, @function 782 + .global debug_trampoline 783 + debug_trampoline: 784 + /* 785 + * Save the registers on the stack so that the monitor code 786 + * can find them easily. 787 + */ 788 + sub sp, 4 /* r12_orig */ 789 + stmts --sp, r0-lr 790 + get_thread_info r0 791 + ld.w r8, r0[TI_rar_saved] 792 + ld.w r9, r0[TI_rsr_saved] 793 + pushm r8-r9 794 + 795 + /* 796 + * The monitor code will alter the return address so we don't 797 + * return here. 798 + */ 799 + breakpoint 800 + rjmp breakpoint_failed 801 + .size debug_trampoline, . - debug_trampoline 802 + 803 + .type breakpoint_failed, @function 804 + breakpoint_failed: 805 + /* 806 + * Something went wrong. Perhaps the debug hardware isn't 807 + * enabled? 808 + */ 809 + lda.w r12, msg_breakpoint_failed 810 + mov r11, sp 811 + mov r10, 9 /* SIGKILL */ 812 + call die 813 + 1: rjmp 1b 814 + 815 + msg_breakpoint_failed: 816 + .asciz "Failed to enter Debug Mode"
+153 -114
arch/avr32/kernel/ptrace.c
··· 30 30 31 31 static void ptrace_single_step(struct task_struct *tsk) 32 32 { 33 - pr_debug("ptrace_single_step: pid=%u, SR=0x%08lx\n", 34 - tsk->pid, tsk->thread.cpu_context.sr); 35 - if (!(tsk->thread.cpu_context.sr & SR_D)) { 36 - /* 37 - * Set a breakpoint at the current pc to force the 38 - * process into debug mode. The syscall/exception 39 - * exit code will set a breakpoint at the return 40 - * address when this flag is set. 41 - */ 42 - pr_debug("ptrace_single_step: Setting TIF_BREAKPOINT\n"); 43 - set_tsk_thread_flag(tsk, TIF_BREAKPOINT); 44 - } 33 + pr_debug("ptrace_single_step: pid=%u, PC=0x%08lx, SR=0x%08lx\n", 34 + tsk->pid, task_pt_regs(tsk)->pc, task_pt_regs(tsk)->sr); 45 35 46 - /* The monitor code will do the actual step for us */ 36 + /* 37 + * We can't schedule in Debug mode, so when TIF_BREAKPOINT is 38 + * set, the system call or exception handler will do a 39 + * breakpoint to enter monitor mode before returning to 40 + * userspace. 41 + * 42 + * The monitor code will then notice that TIF_SINGLE_STEP is 43 + * set and return to userspace with single stepping enabled. 44 + * The CPU will then enter monitor mode again after exactly 45 + * one instruction has been executed, and the monitor code 46 + * will then send a SIGTRAP to the process. 47 + */ 48 + set_tsk_thread_flag(tsk, TIF_BREAKPOINT); 47 49 set_tsk_thread_flag(tsk, TIF_SINGLE_STEP); 48 50 } 49 51 ··· 57 55 void ptrace_disable(struct task_struct *child) 58 56 { 59 57 clear_tsk_thread_flag(child, TIF_SINGLE_STEP); 60 - } 61 - 62 - /* 63 - * Handle hitting a breakpoint 64 - */ 65 - static void ptrace_break(struct task_struct *tsk, struct pt_regs *regs) 66 - { 67 - siginfo_t info; 68 - 69 - info.si_signo = SIGTRAP; 70 - info.si_errno = 0; 71 - info.si_code = TRAP_BRKPT; 72 - info.si_addr = (void __user *)instruction_pointer(regs); 73 - 74 - pr_debug("ptrace_break: Sending SIGTRAP to PID %u (pc = 0x%p)\n", 75 - tsk->pid, info.si_addr); 76 - force_sig_info(SIGTRAP, &info, tsk); 58 + clear_tsk_thread_flag(child, TIF_BREAKPOINT); 77 59 } 78 60 79 61 /* ··· 70 84 unsigned long *regs; 71 85 unsigned long value; 72 86 73 - pr_debug("ptrace_read_user(%p, %#lx, %p)\n", 74 - tsk, offset, data); 75 - 76 87 if (offset & 3 || offset >= sizeof(struct user)) { 77 88 printk("ptrace_read_user: invalid offset 0x%08lx\n", offset); 78 89 return -EIO; ··· 80 97 value = 0; 81 98 if (offset < sizeof(struct pt_regs)) 82 99 value = regs[offset / sizeof(regs[0])]; 100 + 101 + pr_debug("ptrace_read_user(%s[%u], %#lx, %p) -> %#lx\n", 102 + tsk->comm, tsk->pid, offset, data, value); 83 103 84 104 return put_user(value, data); 85 105 } ··· 97 111 { 98 112 unsigned long *regs; 99 113 114 + pr_debug("ptrace_write_user(%s[%u], %#lx, %#lx)\n", 115 + tsk->comm, tsk->pid, offset, value); 116 + 100 117 if (offset & 3 || offset >= sizeof(struct user)) { 101 - printk("ptrace_write_user: invalid offset 0x%08lx\n", offset); 118 + pr_debug(" invalid offset 0x%08lx\n", offset); 102 119 return -EIO; 103 120 } 104 121 ··· 143 154 long arch_ptrace(struct task_struct *child, long request, long addr, long data) 144 155 { 145 156 int ret; 146 - 147 - pr_debug("arch_ptrace(%ld, %d, %#lx, %#lx)\n", 148 - request, child->pid, addr, data); 149 157 150 158 pr_debug("ptrace: Enabling monitor mode...\n"); 151 159 ocd_write(DC, ocd_read(DC) | (1 << OCD_DC_MM_BIT) ··· 227 241 break; 228 242 } 229 243 230 - pr_debug("sys_ptrace returning %d (DC = 0x%08lx)\n", 231 - ret, ocd_read(DC)); 232 244 return ret; 233 245 } 234 246 235 247 asmlinkage void syscall_trace(void) 236 248 { 237 - pr_debug("syscall_trace called\n"); 238 249 if (!test_thread_flag(TIF_SYSCALL_TRACE)) 239 250 return; 240 251 if (!(current->ptrace & PT_PTRACED)) 241 252 return; 242 253 243 - pr_debug("syscall_trace: notifying parent\n"); 244 254 /* The 0x80 provides a way for the tracing parent to 245 255 * distinguish between a syscall stop and SIGTRAP delivery */ 246 256 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ··· 255 273 } 256 274 } 257 275 258 - asmlinkage void do_debug_priv(struct pt_regs *regs) 259 - { 260 - unsigned long dc, ds; 261 - unsigned long die_val; 262 - 263 - ds = ocd_read(DS); 264 - 265 - pr_debug("do_debug_priv: pc = %08lx, ds = %08lx\n", regs->pc, ds); 266 - 267 - if (ds & (1 << OCD_DS_SSS_BIT)) 268 - die_val = DIE_SSTEP; 269 - else 270 - die_val = DIE_BREAKPOINT; 271 - 272 - if (notify_die(die_val, "ptrace", regs, 0, 0, SIGTRAP) == NOTIFY_STOP) 273 - return; 274 - 275 - if (likely(ds & (1 << OCD_DS_SSS_BIT))) { 276 - extern void itlb_miss(void); 277 - extern void tlb_miss_common(void); 278 - struct thread_info *ti; 279 - 280 - dc = ocd_read(DC); 281 - dc &= ~(1 << OCD_DC_SS_BIT); 282 - ocd_write(DC, dc); 283 - 284 - ti = current_thread_info(); 285 - set_ti_thread_flag(ti, TIF_BREAKPOINT); 286 - 287 - /* The TLB miss handlers don't check thread flags */ 288 - if ((regs->pc >= (unsigned long)&itlb_miss) 289 - && (regs->pc <= (unsigned long)&tlb_miss_common)) { 290 - ocd_write(BWA2A, sysreg_read(RAR_EX)); 291 - ocd_write(BWC2A, 0x40000001 | (get_asid() << 1)); 292 - } 293 - 294 - /* 295 - * If we're running in supervisor mode, the breakpoint 296 - * will take us where we want directly, no need to 297 - * single step. 298 - */ 299 - if ((regs->sr & MODE_MASK) != MODE_SUPERVISOR) 300 - set_ti_thread_flag(ti, TIF_SINGLE_STEP); 301 - } else { 302 - panic("Unable to handle debug trap at pc = %08lx\n", 303 - regs->pc); 304 - } 305 - } 306 - 307 276 /* 308 - * Handle breakpoints, single steps and other debuggy things. To keep 309 - * things simple initially, we run with interrupts and exceptions 310 - * disabled all the time. 277 + * debug_trampoline() is an assembly stub which will store all user 278 + * registers on the stack and execute a breakpoint instruction. 279 + * 280 + * If we single-step into an exception handler which runs with 281 + * interrupts disabled the whole time so it doesn't have to check for 282 + * pending work, its return address will be modified so that it ends 283 + * up returning to debug_trampoline. 284 + * 285 + * If the exception handler decides to store the user context and 286 + * enable interrupts after all, it will restore the original return 287 + * address and status register value. Before it returns, it will 288 + * notice that TIF_BREAKPOINT is set and execute a breakpoint 289 + * instruction. 311 290 */ 312 - asmlinkage void do_debug(struct pt_regs *regs) 291 + extern void debug_trampoline(void); 292 + 293 + asmlinkage struct pt_regs *do_debug(struct pt_regs *regs) 313 294 { 314 - unsigned long dc, ds; 295 + struct thread_info *ti; 296 + unsigned long trampoline_addr; 297 + u32 status; 298 + u32 ctrl; 299 + int code; 315 300 316 - ds = ocd_read(DS); 317 - pr_debug("do_debug: pc = %08lx, ds = %08lx\n", regs->pc, ds); 301 + status = ocd_read(DS); 302 + ti = current_thread_info(); 303 + code = TRAP_BRKPT; 318 304 319 - if (test_thread_flag(TIF_BREAKPOINT)) { 320 - pr_debug("TIF_BREAKPOINT set\n"); 321 - /* We're taking care of it */ 322 - clear_thread_flag(TIF_BREAKPOINT); 323 - ocd_write(BWC2A, 0); 324 - } 305 + pr_debug("do_debug: status=0x%08x PC=0x%08lx SR=0x%08lx tif=0x%08lx\n", 306 + status, regs->pc, regs->sr, ti->flags); 325 307 326 - if (test_thread_flag(TIF_SINGLE_STEP)) { 327 - pr_debug("TIF_SINGLE_STEP set, ds = 0x%08lx\n", ds); 328 - if (ds & (1 << OCD_DS_SSS_BIT)) { 329 - dc = ocd_read(DC); 330 - dc &= ~(1 << OCD_DC_SS_BIT); 331 - ocd_write(DC, dc); 308 + if (!user_mode(regs)) { 309 + unsigned long die_val = DIE_BREAKPOINT; 332 310 333 - clear_thread_flag(TIF_SINGLE_STEP); 334 - ptrace_break(current, regs); 311 + if (status & (1 << OCD_DS_SSS_BIT)) 312 + die_val = DIE_SSTEP; 313 + 314 + if (notify_die(die_val, "ptrace", regs, 0, 0, SIGTRAP) 315 + == NOTIFY_STOP) 316 + return regs; 317 + 318 + if ((status & (1 << OCD_DS_SWB_BIT)) 319 + && test_and_clear_ti_thread_flag( 320 + ti, TIF_BREAKPOINT)) { 321 + /* 322 + * Explicit breakpoint from trampoline or 323 + * exception/syscall/interrupt handler. 324 + * 325 + * The real saved regs are on the stack right 326 + * after the ones we saved on entry. 327 + */ 328 + regs++; 329 + pr_debug(" -> TIF_BREAKPOINT done, adjusted regs:" 330 + "PC=0x%08lx SR=0x%08lx\n", 331 + regs->pc, regs->sr); 332 + BUG_ON(!user_mode(regs)); 333 + 334 + if (test_thread_flag(TIF_SINGLE_STEP)) { 335 + pr_debug("Going to do single step...\n"); 336 + return regs; 337 + } 338 + 339 + /* 340 + * No TIF_SINGLE_STEP means we're done 341 + * stepping over a syscall. Do the trap now. 342 + */ 343 + code = TRAP_TRACE; 344 + } else if ((status & (1 << OCD_DS_SSS_BIT)) 345 + && test_ti_thread_flag(ti, TIF_SINGLE_STEP)) { 346 + 347 + pr_debug("Stepped into something, " 348 + "setting TIF_BREAKPOINT...\n"); 349 + set_ti_thread_flag(ti, TIF_BREAKPOINT); 350 + 351 + /* 352 + * We stepped into an exception, interrupt or 353 + * syscall handler. Some exception handlers 354 + * don't check for pending work, so we need to 355 + * set up a trampoline just in case. 356 + * 357 + * The exception entry code will undo the 358 + * trampoline stuff if it does a full context 359 + * save (which also means that it'll check for 360 + * pending work later.) 361 + */ 362 + if ((regs->sr & MODE_MASK) == MODE_EXCEPTION) { 363 + trampoline_addr 364 + = (unsigned long)&debug_trampoline; 365 + 366 + pr_debug("Setting up trampoline...\n"); 367 + ti->rar_saved = sysreg_read(RAR_EX); 368 + ti->rsr_saved = sysreg_read(RSR_EX); 369 + sysreg_write(RAR_EX, trampoline_addr); 370 + sysreg_write(RSR_EX, (MODE_EXCEPTION 371 + | SR_EM | SR_GM)); 372 + BUG_ON(ti->rsr_saved & MODE_MASK); 373 + } 374 + 375 + /* 376 + * If we stepped into a system call, we 377 + * shouldn't do a single step after we return 378 + * since the return address is right after the 379 + * "scall" instruction we were told to step 380 + * over. 381 + */ 382 + if ((regs->sr & MODE_MASK) == MODE_SUPERVISOR) { 383 + pr_debug("Supervisor; no single step\n"); 384 + clear_ti_thread_flag(ti, TIF_SINGLE_STEP); 385 + } 386 + 387 + ctrl = ocd_read(DC); 388 + ctrl &= ~(1 << OCD_DC_SS_BIT); 389 + ocd_write(DC, ctrl); 390 + 391 + return regs; 392 + } else { 393 + printk(KERN_ERR "Unexpected OCD_DS value: 0x%08x\n", 394 + status); 395 + printk(KERN_ERR "Thread flags: 0x%08lx\n", ti->flags); 396 + die("Unhandled debug trap in kernel mode", 397 + regs, SIGTRAP); 335 398 } 336 - } else { 337 - /* regular breakpoint */ 338 - ptrace_break(current, regs); 399 + } else if (status & (1 << OCD_DS_SSS_BIT)) { 400 + /* Single step in user mode */ 401 + code = TRAP_TRACE; 402 + 403 + ctrl = ocd_read(DC); 404 + ctrl &= ~(1 << OCD_DC_SS_BIT); 405 + ocd_write(DC, ctrl); 339 406 } 407 + 408 + pr_debug("Sending SIGTRAP: code=%d PC=0x%08lx SR=0x%08lx\n", 409 + code, regs->pc, regs->sr); 410 + 411 + clear_thread_flag(TIF_SINGLE_STEP); 412 + _exception(SIGTRAP, regs, code, instruction_pointer(regs)); 413 + 414 + return regs; 340 415 }
+1 -1
arch/avr32/kernel/vmlinux.lds.S
··· 77 77 . = 0x100; 78 78 *(.scall.text) 79 79 *(.irq.text) 80 + KPROBES_TEXT 80 81 TEXT_TEXT 81 82 SCHED_TEXT 82 83 LOCK_TEXT 83 - KPROBES_TEXT 84 84 *(.fixup) 85 85 *(.gnu.warning) 86 86 _etext = .;
+3
include/asm-avr32/processor.h
··· 139 139 extern void show_stack_log_lvl(struct task_struct *tsk, unsigned long sp, 140 140 struct pt_regs *regs, const char *log_lvl); 141 141 142 + #define task_pt_regs(p) \ 143 + ((struct pt_regs *)(THREAD_SIZE + task_stack_page(p)) - 1) 144 + 142 145 #define KSTK_EIP(tsk) ((tsk)->thread.cpu_context.pc) 143 146 #define KSTK_ESP(tsk) ((tsk)->thread.cpu_context.ksp) 144 147
+8 -4
include/asm-avr32/thread_info.h
··· 25 25 unsigned long flags; /* low level flags */ 26 26 __u32 cpu; 27 27 __s32 preempt_count; /* 0 => preemptable, <0 => BUG */ 28 + __u32 rar_saved; /* return address... */ 29 + __u32 rsr_saved; /* ...and status register 30 + saved by debug handler 31 + when setting up 32 + trampoline */ 28 33 struct restart_block restart_block; 29 34 __u8 supervisor_stack[0]; 30 35 }; ··· 83 78 #define TIF_NEED_RESCHED 2 /* rescheduling necessary */ 84 79 #define TIF_POLLING_NRFLAG 3 /* true if poll_idle() is polling 85 80 TIF_NEED_RESCHED */ 86 - #define TIF_BREAKPOINT 4 /* true if we should break after return */ 87 - #define TIF_SINGLE_STEP 5 /* single step after next break */ 81 + #define TIF_BREAKPOINT 4 /* enter monitor mode on return */ 82 + #define TIF_SINGLE_STEP 5 /* single step in progress */ 88 83 #define TIF_MEMDIE 6 89 84 #define TIF_RESTORE_SIGMASK 7 /* restore signal mask in do_signal */ 90 85 #define TIF_CPU_GOING_TO_SLEEP 8 /* CPU is entering sleep 0 mode */ ··· 94 89 #define _TIF_SIGPENDING (1 << TIF_SIGPENDING) 95 90 #define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED) 96 91 #define _TIF_POLLING_NRFLAG (1 << TIF_POLLING_NRFLAG) 97 - #define _TIF_BREAKPOINT (1 << TIF_BREAKPOINT) 98 92 #define _TIF_SINGLE_STEP (1 << TIF_SINGLE_STEP) 99 93 #define _TIF_MEMDIE (1 << TIF_MEMDIE) 100 94 #define _TIF_RESTORE_SIGMASK (1 << TIF_RESTORE_SIGMASK) ··· 112 108 /* work to do on any return to userspace */ 113 109 #define _TIF_ALLWORK_MASK (_TIF_WORK_MASK | (1 << TIF_SYSCALL_TRACE)) 114 110 /* work to do on return from debug mode */ 115 - #define _TIF_DBGWORK_MASK (_TIF_WORK_MASK | (1 << TIF_SINGLE_STEP)) 111 + #define _TIF_DBGWORK_MASK (_TIF_WORK_MASK & ~(1 << TIF_BREAKPOINT)) 116 112 117 113 #endif /* __ASM_AVR32_THREAD_INFO_H */