2 * arch/sh/kernel/cpu/sh3/entry.S
4 * Copyright (C) 1999, 2000, 2002 Niibe Yutaka
5 * Copyright (C) 2003 - 2012 Paul Mundt
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive
11 #include <linux/sys.h>
12 #include <linux/errno.h>
13 #include <linux/linkage.h>
14 #include <asm/asm-offsets.h>
15 #include <asm/thread_info.h>
16 #include <asm/unistd.h>
17 #include <cpu/mmu_context.h>
19 #include <asm/cache.h>
20 #include <asm/thread_info.h>
23 ! GNU as (as of 2.9.1) changes bf/s into bt/s and bra, when the address
24 ! to be jumped is too far, but it causes illegal slot exception.
27 * entry.S contains the system-call and fault low-level handling routines.
28 * This also contains the timer-interrupt handler, as well as all interrupts
29 * and faults that can result in a task-switch.
31 * NOTE: This code handles signal-recognition, which happens every time
32 * after a timer-interrupt and after each system call.
34 * NOTE: This code uses a convention that instructions in the delay slot
35 * of a transfer-control instruction are indented by an extra space, thus:
37 * jmp @k0 ! control-transfer instruction
38 * ldc k1, ssr ! delay slot
40 * Stack layout in 'ret_from_syscall':
41 * ptrace needs to have all regs on the stack.
42 * if the order here is changed, it needs to be
43 * updated in ptrace.c and ptrace.h
57 /* Offsets to the stack */
58 OFF_R0 = 0 /* Return value. New ABI also arg4 */
59 OFF_R1 = 4 /* New ABI: arg5 */
60 OFF_R2 = 8 /* New ABI: arg6 */
61 OFF_R3 = 12 /* New ABI: syscall_nr */
62 OFF_R4 = 16 /* New ABI: arg0 */
63 OFF_R5 = 20 /* New ABI: arg1 */
64 OFF_R6 = 24 /* New ABI: arg2 */
65 OFF_R7 = 28 /* New ABI: arg3 */
77 #define g_imask r6 /* r6_bank1 */
78 #define k_g_imask r6_bank /* r6_bank1 */
79 #define current r7 /* r7_bank1 */
81 #include <asm/entry-macros.S>
84 * Kernel mode register usage:
87 * k2 scratch (Exception code)
88 * k3 scratch (Return address)
91 * k6 Global Interrupt Mask (0--15 << 4)
92 * k7 CURRENT_THREAD_INFO (pointer to current thread info)
96 ! TLB Miss / Initial Page write exception handling
98 ! TLB hits, but the access violate the protection.
99 ! It can be valid access, such as stack grow and/or C-O-W.
102 ! Find the pmd/pte entry and loadtlb
103 ! If it's not found, cause address error (SEGV)
105 ! Although this could be written in assembly language (and it'd be faster),
106 ! this first version depends *much* on C implementation.
109 #if defined(CONFIG_MMU)
112 bra call_handle_tlbmiss
116 ENTRY(tlb_miss_store)
117 bra call_handle_tlbmiss
118 mov #FAULT_CODE_WRITE, r5
121 ENTRY(initial_page_write)
122 bra call_handle_tlbmiss
123 mov #FAULT_CODE_INITIAL, r5
126 ENTRY(tlb_protection_violation_load)
127 bra call_do_page_fault
128 mov #FAULT_CODE_PROT, r5
131 ENTRY(tlb_protection_violation_store)
132 bra call_do_page_fault
133 mov #(FAULT_CODE_PROT | FAULT_CODE_WRITE), r5
163 2: .long handle_tlbmiss
164 3: .long do_page_fault
165 4: .long ret_from_exception
168 ENTRY(address_error_load)
170 mov #0,r5 ! writeaccess = 0
173 ENTRY(address_error_store)
175 mov #1,r5 ! writeaccess = 1
180 mov.l @r0, r6 ! address
187 2: .long do_address_error
188 #endif /* CONFIG_MMU */
190 #if defined(CONFIG_SH_STANDARD_BIOS)
191 /* Unwind the stack and jmp to the debug entry */
192 ENTRY(sh_bios_handler)
197 lds k2, pr ! restore pr
206 2: .long gdb_vbr_vector
207 #endif /* CONFIG_SH_STANDARD_BIOS */
210 ! - restore r0, r1, r2, r3, r4, r5, r6, r7 from the stack
212 ! - restore r8, r9, r10, r11, r12, r13, r14, r15 from the stack
213 ! - restore spc, pr*, ssr, gbr, mach, macl, skip default tra
214 ! k2 returns original pr
215 ! k3 returns original sr
216 ! k4 returns original stack pointer
217 ! r8 passes SR bitmask, overwritten with restored data on return
219 ! BL=0 on entry, on exit BL=1 (depending on r8).
242 mov.l @r15+, k4 ! original stack pointer
244 mov.l @r15+, k2 ! original PR
245 mov.l @r15+, k3 ! original SR
250 add #4, r15 ! Skip syscall number
257 lds k2, pr ! restore pr
259 ! Calculate new SR value
260 mov k3, k2 ! original SR value
264 and k1, k2 ! Mask original SR value
266 mov k3, k0 ! Calculate IMASK-bits
274 6: or k0, k2 ! Set the IMASK-bits
282 5: .long 0x00001000 ! DSP
285 ! common exception handler
286 #include "../../entry-common.S"
288 ! Exception Vector Base
290 ! Should be aligned page boundary.
296 ! 0x100: General exception vector
301 sts pr, k3 ! save original pr value in k3
305 ! - switch to kernel stack
306 ! k0 returns original sp (after roll back)
312 ! Check for roll back gRB (User and Kernel)
320 cmp/hs k0, k1 ! test k1 (saved PC) >= k0 (saved r0)
326 ldc k0, spc ! PC = saved r0 + r15 - 2
327 2: mov k1, r15 ! SP = r1
330 ! Switch to kernel stack if needed
331 stc ssr, k0 ! Is it from kernel space?
332 shll k0 ! Check MD bit (bit30) by shifting it into...
333 shll k0 ! ...the T bit
334 bt/s 1f ! It's a kernel to kernel transition.
335 mov r15, k0 ! save original stack to k0
336 /* User space to kernel */
337 mov #(THREAD_SIZE >> 10), k1
338 shll8 k1 ! k1 := THREAD_SIZE
341 mov k1, r15 ! change to kernel stack
348 ! 0x400: Instruction and Data TLB miss exception vector
352 sts pr, k3 ! save original pr value in k3
355 mova exception_data, k0
357 ! Setup stack and save DSP context (k0 contains original r15 on return)
361 ! Save registers / Switch to bank 0
362 mov.l 5f, k2 ! vector register address
363 mov.l 1f, k4 ! SR bits to clear in k4
364 bsr save_regs ! needs original pr value in k3
365 mov.l @k2, k2 ! read out vector and keep in k2
367 handle_exception_special:
370 ! Setup return address and jump to exception handler
371 mov.l 7f, r9 ! fetch return address
372 stc r2_bank, r0 ! k2 (vector)
376 mov.l @(r0, r10), r10
378 lds r9, pr ! put return address in pr
380 .align L1_CACHE_SHIFT
383 ! - save default tra, macl, mach, gbr, ssr, pr* and spc on the stack
384 ! - save r15*, r14, r13, r12, r11, r10, r9, r8 on the stack
386 ! - save r7, r6, r5, r4, r3, r2, r1, r0 on the stack
387 ! k0 contains original stack pointer*
389 ! k3 passes original pr*
390 ! k4 passes SR bitmask
391 ! BL=1 on entry, on exit BL=0.
395 mov.l k1, @-r15 ! set TRA (default: -1)
400 mov.l k3, @-r15 ! original pr in k3
403 mov.l k0, @-r15 ! original stack pointer in k0
412 mov.l 0f, k3 ! SR bits to set in k3
417 ! - modify SR for bank switch
418 ! - save r7, r6, r5, r4, r3, r2, r1, r0 on the stack
419 ! k3 passes bits to set in SR
420 ! k4 passes bits to clear in SR
439 ! 0x600: Interrupt / NMI vector
442 ENTRY(handle_interrupt)
443 sts pr, k3 ! save original pr value in k3
444 mova exception_data, k0
446 ! Setup stack and save DSP context (k0 contains original r15 on return)
450 ! Save registers / Switch to bank 0
451 mov.l 1f, k4 ! SR bits to clear in k4
452 bsr save_regs ! needs original pr value in k3
453 mov #-1, k2 ! default vector kept in k2
457 stc sr, r0 ! get status register
465 ! Setup return address and jump to do_IRQ
466 mov.l 4f, r9 ! fetch return address
467 lds r9, pr ! put return address in pr
470 mov.l @r4, r4 ! pass INTEVT vector as arg0
474 mov r4, r0 ! save vector->jmp table offset for later
476 shlr2 r4 ! vector to IRQ# conversion
479 cmp/pz r4 ! is it a valid IRQ?
483 * We got here as a result of taking the INTEVT path for something
484 * that isn't a valid hard IRQ, therefore we bypass the do_IRQ()
485 * path and special case the event dispatch instead. This is the
486 * expected path for the NMI (and any other brilliantly implemented
487 * exception), which effectively wants regular exception dispatch
488 * but is unfortunately reported through INTEVT rather than
494 mov r15, r8 ! trap handlers take saved regs in r8
497 jmp @r9 ! Off to do_IRQ() we go.
498 mov r15, r5 ! pass saved registers as arg1
500 ENTRY(exception_none)
504 .align L1_CACHE_SHIFT
506 0: .long 0x000080f0 ! FD=1, IMASK=15
507 1: .long 0xcfffffff ! RB=0, BL=0
510 4: .long ret_from_irq
512 6: .long exception_handling_table
513 7: .long ret_from_exception