1 /* $Id: entry.S,v 1.37 2004/06/11 13:02:46 doyu Exp $
3 * linux/arch/sh/entry.S
5 * Copyright (C) 1999, 2000, 2002 Niibe Yutaka
6 * Copyright (C) 2003 Paul Mundt
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file "COPYING" in the main directory of this archive
13 #include <linux/sys.h>
14 #include <linux/errno.h>
15 #include <linux/linkage.h>
16 #include <asm/asm-offsets.h>
17 #include <asm/thread_info.h>
18 #include <asm/cpu/mmu_context.h>
19 #include <asm/unistd.h>
22 ! GNU as (as of 2.9.1) changes bf/s into bt/s and bra, when the address
23 ! to be jumped is too far, but it causes illegal slot exception.
26 * entry.S contains the system-call and fault low-level handling routines.
27 * This also contains the timer-interrupt handler, as well as all interrupts
28 * and faults that can result in a task-switch.
30 * NOTE: This code handles signal-recognition, which happens every time
31 * after a timer-interrupt and after each system call.
33 * NOTE: This code uses a convention that instructions in the delay slot
34 * of a transfer-control instruction are indented by an extra space, thus:
36 * jmp @k0 ! control-transfer instruction
37 * ldc k1, ssr ! delay slot
39 * Stack layout in 'ret_from_syscall':
40 * ptrace needs to have all regs on the stack.
41 * if the order here is changed, it needs to be
42 * updated in ptrace.c and ptrace.h
56 #if defined(CONFIG_KGDB_NMI)
57 NMI_VEC = 0x1c0 ! Must catch early for debounce
60 /* Offsets to the stack */
61 OFF_R0 = 0 /* Return value. New ABI also arg4 */
62 OFF_R1 = 4 /* New ABI: arg5 */
63 OFF_R2 = 8 /* New ABI: arg6 */
64 OFF_R3 = 12 /* New ABI: syscall_nr */
65 OFF_R4 = 16 /* New ABI: arg0 */
66 OFF_R5 = 20 /* New ABI: arg1 */
67 OFF_R6 = 24 /* New ABI: arg2 */
68 OFF_R7 = 28 /* New ABI: arg3 */
81 #define k_ex_code r2_bank /* r2_bank1 */
82 #define g_imask r6 /* r6_bank1 */
83 #define k_g_imask r6_bank /* r6_bank1 */
84 #define current r7 /* r7_bank1 */
87 * Kernel mode register usage:
90 * k2 scratch (Exception code)
91 * k3 scratch (Return address)
94 * k6 Global Interrupt Mask (0--15 << 4)
95 * k7 CURRENT_THREAD_INFO (pointer to current thread info)
99 ! TLB Miss / Initial Page write exception handling
101 ! TLB hits, but the access violate the protection.
102 ! It can be valid access, such as stack grow and/or C-O-W.
105 ! Find the pmd/pte entry and loadtlb
106 ! If it's not found, cause address error (SEGV)
108 ! Although this could be written in assembly language (and it'd be faster),
109 ! this first version depends *much* on C implementation.
118 mov.l __INV_IMASK, r11; \
121 stc k_g_imask, r11; \
125 #if defined(CONFIG_PREEMPT)
126 # define preempt_stop() CLI()
128 # define preempt_stop()
129 # define resume_kernel restore_all
132 #if defined(CONFIG_MMU)
139 ENTRY(tlb_miss_store)
144 ENTRY(initial_page_write)
149 ENTRY(tlb_protection_violation_load)
154 ENTRY(tlb_protection_violation_store)
182 2: .long __do_page_fault
183 3: .long do_page_fault
186 ENTRY(address_error_load)
188 mov #0,r5 ! writeaccess = 0
191 ENTRY(address_error_store)
193 mov #1,r5 ! writeaccess = 1
198 mov.l @r0, r6 ! address
205 2: .long do_address_error
206 #endif /* CONFIG_MMU */
208 #if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
209 ! Handle kernel debug if either kgdb (SW) or gdb-stub (FW) is present.
210 ! If both are configured, handle the debug traps (breakpoints) in SW,
211 ! but still allow BIOS traps to FW.
215 #if defined(CONFIG_SH_STANDARD_BIOS) && defined(CONFIG_SH_KGDB)
216 /* Force BIOS call to FW (debug_trap put TRA in r8) */
221 #endif /* CONFIG_SH_STANDARD_BIOS && CONFIG_SH_KGDB */
224 #if defined(CONFIG_SH_KGDB)
225 /* Jump to kgdb, pass stacked regs as arg */
231 3: .long kgdb_handle_exception
232 #endif /* CONFIG_SH_KGDB */
234 #if defined(CONFIG_SH_STANDARD_BIOS)
235 /* Unwind the stack and jmp to the debug entry */
246 mov.l 1f, r9 ! BL =1, RB=1, IMASK=0x0F
248 ldc r8, sr ! here, change the register bank
271 2: .long gdb_vbr_vector
272 #endif /* CONFIG_SH_STANDARD_BIOS */
274 #endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */
279 #if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)
281 mov.l @(r0,r15), r0 ! get status register
283 shll r0 ! kernel space?
286 mov.l @r15, r0 ! Restore R0 value
292 ENTRY(exception_error)
301 1: .long break_point_trap_software
302 2: .long do_exception_error
310 mov.l @(r0,r15), r0 ! get status register
312 shll r0 ! kernel space?
313 bt/s resume_kernel ! Yes, it's from kernel, go back soon
316 #ifdef CONFIG_PREEMPT
320 mov.l @(TI_PRE_COUNT,r8), r0 ! current_thread_info->preempt_count
324 mov.l @(TI_FLAGS,r8), r0 ! current_thread_info->flags
325 tst #_TIF_NEED_RESCHED, r0 ! need_resched set?
329 mov.l @(r0,r15), r0 ! get status register
330 and #0xf0, r0 ! interrupts off (exception path)?
335 mov.l r0, @(TI_PRE_COUNT,r8)
342 mov.l r0, @(TI_PRE_COUNT,r8)
352 1: .long PREEMPT_ACTIVE
356 ENTRY(resume_userspace)
357 ! r8: current_thread_info
359 mov.l @(TI_FLAGS,r8), r0 ! current_thread_info->flags
360 tst #_TIF_WORK_MASK, r0
362 tst #_TIF_NEED_RESCHED, r0
366 ! r0: current_thread_info->flags
367 ! r8: current_thread_info
368 ! t: result of "tst #_TIF_NEED_RESCHED, r0"
370 tst #(_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK), r0
374 mov r12, r5 ! set arg1(save_r0)
381 #ifndef CONFIG_PREEMPT
383 mov.l @(OFF_SP,r15), r0 ! get user space stack pointer
390 ! SP >= 0xc0000000 : gUSA mark
391 mov.l @(r0,r15), r2 ! get user space PC (program counter)
392 mov.l @(OFF_R0,r15), r3 ! end point
393 cmp/hs r3, r2 ! r2 >= r3?
395 add r3, r1 ! rewind point #2
396 mov.l r1, @(r0,r15) ! reset PC to rewind point #2
405 mov.l @(TI_FLAGS,r8), r0 ! current_thread_info->flags
406 tst #_TIF_WORK_MASK, r0
409 tst #_TIF_NEED_RESCHED, r0
413 2: .long do_notify_resume
417 ! r0: current_thread_info->flags
418 ! r8: current_thread_info
419 tst #_TIF_SYSCALL_TRACE, r0
421 tst #_TIF_NEED_RESCHED, r0
423 ! XXX setup arguments...
424 mov.l 4f, r0 ! do_syscall_trace
433 ! XXX setup arguments...
434 mov.l 4f, r11 ! Call do_syscall_trace which notifies
435 jsr @r11 ! superior (will chomp R[0-7])
437 ! Reload R0-R4 from kernel stack, where the
438 ! parent may have modified them using
439 ! ptrace(POKEUSR). (Note that R0-R2 are
440 ! used by the system call handler directly
441 ! from the kernel stack anyway, so don't need
442 ! to be reloaded here.) This allows the parent
443 ! to rewrite system calls and args on the fly.
444 mov.l @(OFF_R4,r15), r4 ! arg0
445 mov.l @(OFF_R5,r15), r5
446 mov.l @(OFF_R6,r15), r6
447 mov.l @(OFF_R7,r15), r7 ! arg3
448 mov.l @(OFF_R3,r15), r3 ! syscall_nr
449 ! Arrange for do_syscall_trace to be called
450 ! again as the system call returns.
451 mov.l 2f, r10 ! Number of syscalls
456 mov.l r0, @(OFF_R0,r15) ! Return value
462 * Arguments #0 to #3: R4--R7
463 * Arguments #4 to #6: R0, R1, R2
464 * TRA: (number of arguments + 0x10) x 4
466 * This code also handles delegating other traps to the BIOS/gdb stub
472 * 0x0-0xf old syscall ABI
473 * 0x10-0x1f new syscall ABI
474 * 0x20-0xff delegated through debug_trap to BIOS/gdb stub.
476 * Note: When we're first called, the TRA value must be shifted
477 * right 2 bits in order to get the value that was used as the "trapa"
490 1: .long schedule_tail
494 mov.l @r9, r8 ! Read from TRA (Trap Address) Register
496 ! Is the trap argument >= 0x20? (TRA will be >= 0x80)
503 mov.l r8, @r9 ! set TRA value to tra
505 ! Call the system call handler through the table.
506 ! First check for bad syscall number
508 mov.l 2f, r8 ! Number of syscalls
510 bf/s good_system_call
512 syscall_badsys: ! Bad syscall number
515 mov.l r0, @(OFF_R0,r15) ! Return value
521 good_system_call: ! Good syscall number
522 mov.l @(TI_FLAGS,r8), r8
523 mov #_TIF_SYSCALL_TRACE, r10
525 bf syscall_trace_entry
529 mov.l 3f, r8 ! Load the address of sys_call_table
532 jsr @r8 ! jump to specific syscall handler
534 mov.l @(OFF_R0,r15), r12 ! save r0
535 mov.l r0, @(OFF_R0,r15) ! save the return value
541 mov.l @(TI_FLAGS,r8), r0 ! current_thread_info->flags
542 tst #_TIF_ALLWORK_MASK, r0
556 or r9, r8 ! BL =1, RB=1
557 ldc r8, sr ! here, change the register bank
566 mov.l @r15+, k4 ! original stack pointer
569 mov.l @r15+, k3 ! original SR
573 add #4, r15 ! Skip syscall number
576 mov.l @r15+, k0 ! DSP mode marker
578 cmp/eq k0, k1 ! Do we have a DSP stack frame?
581 stc sr, k0 ! Enable CPU DSP mode
582 or k1, k0 ! (within kernel it may be disabled)
584 mov r2, k0 ! Backup r2
586 ! Restore DSP registers from stack
605 mov k0, r2 ! Restore r2
609 ! Calculate new SR value
610 mov k3, k2 ! original SR value
612 and k1, k2 ! Mask orignal SR value
614 mov k3, k0 ! Calculate IMASK-bits
622 6: or k0, k2 ! Set the IMASK-bits
625 #if defined(CONFIG_KGDB_NMI)
631 mov.l @r15+, k2 ! restore EXPEVT
639 3: .long sys_call_table
640 4: .long do_syscall_trace
641 5: .long 0x00001000 ! DSP
645 .long 0xffffff0f ! ~(IMASK)
647 ! Exception Vector Base
649 ! Should be aligned page boundary.
663 2: .long ret_from_exception
677 #if defined(CONFIG_KGDB_NMI)
678 ! Debounce (filter nested NMI)
692 #endif /* defined(CONFIG_KGDB_NMI) */
699 3: .long ret_from_irq
700 4: .long ret_from_exception
705 ENTRY(handle_exception)
706 ! Using k0, k1 for scratch registers (r0_bank1, r1_bank),
707 ! save all registers onto stack.
709 stc ssr, k0 ! Is it from kernel space?
710 shll k0 ! Check MD bit (bit30) by shifting it into...
711 shll k0 ! ...the T bit
712 bt/s 1f ! It's a kernel to kernel transition.
713 mov r15, k0 ! save original stack to k0
714 /* User space to kernel */
715 mov #(THREAD_SIZE >> 8), k1
716 shll8 k1 ! k1 := THREAD_SIZE
718 mov k1, r15 ! change to kernel stack
724 mov.l r2, @-r15 ! Save r2, we need another reg
727 tst r2, k4 ! Check if in DSP mode
728 mov.l @r15+, r2 ! Restore r2 now
730 mov #0, k4 ! Set marker for no stack frame
732 mov r2, k4 ! Backup r2 (in k4) for later
734 ! Save DSP registers on stack
745 ! GAS is broken, does not generate correct "movs.l Ds,@-As" instr.
747 ! FIXME: Make sure that this is still the case with newer toolchains,
748 ! as we're not at all interested in supporting ancient toolchains at
749 ! this point. -- PFM.
752 .word 0xf653 ! movs.l a1, @-r2
753 .word 0xf6f3 ! movs.l a0g, @-r2
754 .word 0xf6d3 ! movs.l a1g, @-r2
755 .word 0xf6c3 ! movs.l m0, @-r2
756 .word 0xf6e3 ! movs.l m1, @-r2
759 mov k4, r2 ! Restore r2
760 mov.l 1f, k4 ! Force DSP stack frame
762 mov.l k4, @-r15 ! Push DSP mode marker onto stack
764 ! Save the user registers on the stack.
765 mov.l k2, @-r15 ! EXPEVT
766 mov.l k4, @-r15 ! set TRA (default: -1)
775 lds k3, pr ! Set the return address to pr
777 mov.l k0, @-r15 ! save orignal stack
786 stc sr, r8 ! Back to normal register bank, and
787 or k1, r8 ! Block all interrupts
790 ldc r8, sr ! ...changed here.
800 ! Then, dispatch to the handler, according to the exception code.
811 1: .long 0x00001000 ! DSP=1
812 2: .long 0x000080f0 ! FD=1, IMASK=15
813 3: .long 0xcfffffff ! RB=0, BL=0
814 4: .long exception_handling_table
817 ENTRY(exception_none)