sh: TLB protection violation exception optimizations.
[safe/jmp/linux-2.6] / arch / sh / kernel / cpu / sh3 / entry.S
1 /*
2  * arch/sh/kernel/cpu/sh3/entry.S
3  *
4  *  Copyright (C) 1999, 2000, 2002  Niibe Yutaka
5  *  Copyright (C) 2003 - 2006  Paul Mundt
6  *
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
9  * for more details.
10  */
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>
18 #include <asm/page.h>
19 #include <asm/cache.h>
20
21 ! NOTE:
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.
24
25 /*      
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.
29  *
30  * NOTE: This code handles signal-recognition, which happens every time
31  * after a timer-interrupt and after each system call.
32  *
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:
35  *
36  *    jmp       @k0         ! control-transfer instruction
37  *     ldc      k1, ssr     ! delay slot
38  *
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
43  *
44  *      r0
45  *      ...
46  *      r15 = stack pointer
47  *      spc
48  *      pr
49  *      ssr
50  *      gbr
51  *      mach
52  *      macl
53  *      syscall #
54  *
55  */
56 #if defined(CONFIG_KGDB)
57 NMI_VEC = 0x1c0                 ! Must catch early for debounce
58 #endif
59
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 */
69 OFF_SP  =  (15*4)
70 OFF_PC  =  (16*4)
71 OFF_SR  =  (16*4+8)
72 OFF_TRA =  (16*4+6*4)
73
74
75 #define k0      r0
76 #define k1      r1
77 #define k2      r2
78 #define k3      r3
79 #define k4      r4
80
81 #define g_imask         r6      /* r6_bank1 */
82 #define k_g_imask       r6_bank /* r6_bank1 */
83 #define current         r7      /* r7_bank1 */
84
85 #include <asm/entry-macros.S>
86         
87 /*
88  * Kernel mode register usage:
89  *      k0      scratch
90  *      k1      scratch
91  *      k2      scratch (Exception code)
92  *      k3      scratch (Return address)
93  *      k4      scratch
94  *      k5      reserved
95  *      k6      Global Interrupt Mask (0--15 << 4)
96  *      k7      CURRENT_THREAD_INFO (pointer to current thread info)
97  */
98
99 !
100 ! TLB Miss / Initial Page write exception handling
101 !                       _and_
102 ! TLB hits, but the access violate the protection.
103 ! It can be valid access, such as stack grow and/or C-O-W.
104 !
105 !
106 ! Find the pmd/pte entry and loadtlb
107 ! If it's not found, cause address error (SEGV)
108 !
109 ! Although this could be written in assembly language (and it'd be faster),
110 ! this first version depends *much* on C implementation.
111 !
112
113 #if defined(CONFIG_MMU)
114         .align  2
115 ENTRY(tlb_miss_load)
116         bra     call_handle_tlbmiss
117          mov    #0, r5
118
119         .align  2
120 ENTRY(tlb_miss_store)
121         bra     call_handle_tlbmiss
122          mov    #1, r5
123
124         .align  2
125 ENTRY(initial_page_write)
126         bra     call_handle_tlbmiss
127          mov    #1, r5
128
129         .align  2
130 ENTRY(tlb_protection_violation_load)
131         bra     call_do_page_fault
132          mov    #0, r5
133
134         .align  2
135 ENTRY(tlb_protection_violation_store)
136         bra     call_do_page_fault
137          mov    #1, r5
138
139 call_handle_tlbmiss:
140         mov.l   1f, r0
141         mov     r5, r8
142         mov.l   @r0, r6
143         mov.l   2f, r0
144         sts     pr, r10
145         jsr     @r0
146          mov    r15, r4
147         !
148         tst     r0, r0
149         bf/s    0f
150          lds    r10, pr
151         rts
152          nop
153 0:
154         mov     r8, r5
155 call_do_page_fault:
156         mov.l   1f, r0
157         mov.l   @r0, r6
158
159         sti
160
161         mov.l   3f, r0
162         mov.l   4f, r1
163         mov     r15, r4
164         jmp     @r0
165          lds    r1, pr
166
167         .align 2
168 1:      .long   MMU_TEA
169 2:      .long   handle_tlbmiss
170 3:      .long   do_page_fault
171 4:      .long   ret_from_exception
172
173         .align  2
174 ENTRY(address_error_load)
175         bra     call_dae
176          mov    #0,r5           ! writeaccess = 0
177
178         .align  2
179 ENTRY(address_error_store)
180         bra     call_dae
181          mov    #1,r5           ! writeaccess = 1
182
183         .align  2
184 call_dae:
185         mov.l   1f, r0
186         mov.l   @r0, r6         ! address
187         mov.l   2f, r0
188         jmp     @r0
189          mov    r15, r4         ! regs
190
191         .align 2
192 1:      .long   MMU_TEA
193 2:      .long   do_address_error
194 #endif /* CONFIG_MMU */
195
196 #if defined(CONFIG_SH_STANDARD_BIOS)
197         /* Unwind the stack and jmp to the debug entry */
198 ENTRY(sh_bios_handler)
199         mov.l   1f, r8
200         bsr     restore_regs
201          nop
202
203         lds     k2, pr                  ! restore pr
204         mov     k4, r15
205         !
206         mov.l   2f, k0
207         mov.l   @k0, k0
208         jmp     @k0
209          ldc    k3, ssr
210         .align  2
211 1:      .long   0x300000f0
212 2:      .long   gdb_vbr_vector
213 #endif /* CONFIG_SH_STANDARD_BIOS */
214
215 ! restore_regs()
216 ! - restore r0, r1, r2, r3, r4, r5, r6, r7 from the stack
217 ! - switch bank
218 ! - restore r8, r9, r10, r11, r12, r13, r14, r15 from the stack
219 ! - restore spc, pr*, ssr, gbr, mach, macl, skip default tra
220 ! k2 returns original pr
221 ! k3 returns original sr
222 ! k4 returns original stack pointer
223 ! r8 passes SR bitmask, overwritten with restored data on return
224 ! r9 trashed
225 ! BL=0 on entry, on exit BL=1 (depending on r8).
226
227 ENTRY(restore_regs)
228         mov.l   @r15+, r0
229         mov.l   @r15+, r1
230         mov.l   @r15+, r2
231         mov.l   @r15+, r3
232         mov.l   @r15+, r4
233         mov.l   @r15+, r5
234         mov.l   @r15+, r6
235         mov.l   @r15+, r7
236         !
237         stc     sr, r9
238         or      r8, r9
239         ldc     r9, sr
240         !
241         mov.l   @r15+, r8
242         mov.l   @r15+, r9
243         mov.l   @r15+, r10
244         mov.l   @r15+, r11
245         mov.l   @r15+, r12
246         mov.l   @r15+, r13
247         mov.l   @r15+, r14
248         mov.l   @r15+, k4               ! original stack pointer
249         ldc.l   @r15+, spc
250         mov.l   @r15+, k2               ! original PR
251         mov.l   @r15+, k3               ! original SR
252         ldc.l   @r15+, gbr
253         lds.l   @r15+, mach
254         lds.l   @r15+, macl
255         rts
256          add    #4, r15                 ! Skip syscall number
257
258 restore_all:
259         mov.l   7f, r8
260         bsr     restore_regs
261          nop
262
263         lds     k2, pr                  ! restore pr
264         !
265         ! Calculate new SR value
266         mov     k3, k2                  ! original SR value
267         mov     #0xf0, k1
268         extu.b  k1, k1
269         not     k1, k1
270         and     k1, k2                  ! Mask original SR value
271         !
272         mov     k3, k0                  ! Calculate IMASK-bits
273         shlr2   k0
274         and     #0x3c, k0
275         cmp/eq  #0x3c, k0
276         bt/s    6f
277          shll2  k0
278         mov     g_imask, k0
279         !
280 6:      or      k0, k2                  ! Set the IMASK-bits
281         ldc     k2, ssr
282         !
283 #if defined(CONFIG_KGDB)
284         ! Clear in_nmi
285         mov.l   6f, k0
286         mov     #0, k1
287         mov.b   k1, @k0
288 #endif
289         mov     k4, r15
290         rte
291          nop
292
293         .align  2
294 5:      .long   0x00001000      ! DSP
295 #ifdef CONFIG_KGDB
296 6:      .long   in_nmi
297 #endif
298 7:      .long   0x30000000
299
300 ! common exception handler
301 #include "../../entry-common.S"
302         
303 ! Exception Vector Base
304 !
305 !       Should be aligned page boundary.
306 !
307         .balign         4096,0,4096
308 ENTRY(vbr_base)
309         .long   0
310 !
311 ! 0x100: General exception vector
312 !
313         .balign         256,0,256
314 general_exception:
315 #ifndef CONFIG_CPU_SUBTYPE_SHX3
316         bra     handle_exception
317          sts    pr, k3          ! save original pr value in k3
318 #else
319         mov.l   1f, k4
320         mov.l   @k4, k4
321
322         ! Is EXPEVT larger than 0x800?
323         mov     #0x8, k0
324         shll8   k0
325         cmp/hs  k0, k4
326         bf      0f
327
328         ! then add 0x580 (k2 is 0xd80 or 0xda0)
329         mov     #0x58, k0
330         shll2   k0
331         shll2   k0
332         add     k0, k4
333 0:
334         ! Setup stack and save DSP context (k0 contains original r15 on return)
335         bsr     prepare_stack
336          nop
337
338         ! Save registers / Switch to bank 0
339         mov             k4, k2          ! keep vector in k2
340         mov.l   1f, k4          ! SR bits to clear in k4
341         bsr     save_regs       ! needs original pr value in k3
342          nop
343
344         bra     handle_exception_special
345          nop
346
347         .align  2
348 1:      .long   EXPEVT
349 #endif
350
351 ! prepare_stack()
352 ! - roll back gRB
353 ! - switch to kernel stack
354 ! k0 returns original sp (after roll back)
355 ! k1 trashed
356 ! k2 trashed
357
358 prepare_stack:
359 #ifdef CONFIG_GUSA
360         ! Check for roll back gRB (User and Kernel)
361         mov     r15, k0
362         shll    k0
363         bf/s    1f
364          shll   k0
365         bf/s    1f
366          stc    spc, k1
367         stc     r0_bank, k0
368         cmp/hs  k0, k1          ! test k1 (saved PC) >= k0 (saved r0)
369         bt/s    2f
370          stc    r1_bank, k1
371
372         add     #-2, k0
373         add     r15, k0
374         ldc     k0, spc         ! PC = saved r0 + r15 - 2
375 2:      mov     k1, r15         ! SP = r1
376 1:
377 #endif
378         ! Switch to kernel stack if needed
379         stc     ssr, k0         ! Is it from kernel space?
380         shll    k0              ! Check MD bit (bit30) by shifting it into...
381         shll    k0              !       ...the T bit
382         bt/s    1f              ! It's a kernel to kernel transition.
383          mov    r15, k0         ! save original stack to k0
384         /* User space to kernel */
385         mov     #(THREAD_SIZE >> 10), k1
386         shll8   k1              ! k1 := THREAD_SIZE
387         shll2   k1
388         add     current, k1
389         mov     k1, r15         ! change to kernel stack
390         !
391 1:
392         rts
393          nop
394
395 !
396 ! 0x400: Instruction and Data TLB miss exception vector
397 !
398         .balign         1024,0,1024
399 tlb_miss:
400         sts     pr, k3          ! save original pr value in k3
401
402 handle_exception:
403         mova    exception_data, k0
404
405         ! Setup stack and save DSP context (k0 contains original r15 on return)
406         bsr     prepare_stack
407          PREF(k0)
408
409         ! Save registers / Switch to bank 0
410         mov.l   5f, k2          ! vector register address
411         mov.l   1f, k4          ! SR bits to clear in k4
412         bsr     save_regs       ! needs original pr value in k3
413          mov.l  @k2, k2         ! read out vector and keep in k2
414
415 handle_exception_special:
416         ! Setup return address and jump to exception handler
417         mov.l   7f, r9          ! fetch return address
418         stc     r2_bank, r0     ! k2 (vector)
419         mov.l   6f, r10
420         shlr2   r0
421         shlr    r0
422         mov.l   @(r0, r10), r10
423         jmp     @r10
424          lds    r9, pr          ! put return address in pr
425
426         .align  L1_CACHE_SHIFT
427
428 ! save_regs()
429 ! - save default tra, macl, mach, gbr, ssr, pr* and spc on the stack
430 ! - save r15*, r14, r13, r12, r11, r10, r9, r8 on the stack
431 ! - switch bank
432 ! - save r7, r6, r5, r4, r3, r2, r1, r0 on the stack
433 ! k0 contains original stack pointer*
434 ! k1 trashed
435 ! k3 passes original pr*
436 ! k4 passes SR bitmask
437 ! BL=1 on entry, on exit BL=0.
438
439 ENTRY(save_regs)
440         mov     #-1, r1
441         mov.l   k1, @-r15       ! set TRA (default: -1)
442         sts.l   macl, @-r15
443         sts.l   mach, @-r15
444         stc.l   gbr, @-r15
445         stc.l   ssr, @-r15
446         mov.l   k3, @-r15       ! original pr in k3
447         stc.l   spc, @-r15
448
449         mov.l   k0, @-r15       ! original stack pointer in k0
450         mov.l   r14, @-r15
451         mov.l   r13, @-r15
452         mov.l   r12, @-r15
453         mov.l   r11, @-r15
454         mov.l   r10, @-r15
455         mov.l   r9, @-r15
456         mov.l   r8, @-r15
457
458         mov.l   0f, k3          ! SR bits to set in k3
459
460         ! fall-through
461
462 ! save_low_regs()
463 ! - modify SR for bank switch
464 ! - save r7, r6, r5, r4, r3, r2, r1, r0 on the stack
465 ! k3 passes bits to set in SR
466 ! k4 passes bits to clear in SR
467
468 ENTRY(save_low_regs)
469         stc     sr, r8
470         or      k3, r8
471         and     k4, r8
472         ldc     r8, sr
473
474         mov.l   r7, @-r15
475         mov.l   r6, @-r15
476         mov.l   r5, @-r15
477         mov.l   r4, @-r15
478         mov.l   r3, @-r15
479         mov.l   r2, @-r15
480         mov.l   r1, @-r15
481         rts
482          mov.l  r0, @-r15
483
484 !
485 ! 0x600: Interrupt / NMI vector
486 !
487         .balign         512,0,512
488 ENTRY(handle_interrupt)
489 #if defined(CONFIG_KGDB)
490         mov.l   2f, k2
491         ! Debounce (filter nested NMI)
492         mov.l   @k2, k0
493         mov.l   9f, k1
494         cmp/eq  k1, k0
495         bf      11f
496         mov.l   10f, k1
497         tas.b   @k1
498         bt      11f
499         rte
500          nop
501         .align  2
502 9:      .long   NMI_VEC
503 10:     .long   in_nmi
504 11:
505 #endif /* defined(CONFIG_KGDB) */
506         sts     pr, k3          ! save original pr value in k3
507         mova    exception_data, k0
508
509         ! Setup stack and save DSP context (k0 contains original r15 on return)
510         bsr     prepare_stack
511          PREF(k0)
512
513         ! Save registers / Switch to bank 0
514         mov.l   1f, k4          ! SR bits to clear in k4
515         bsr     save_regs       ! needs original pr value in k3
516          mov    #-1, k2         ! default vector kept in k2
517
518         ! Setup return address and jump to do_IRQ
519         mov.l   4f, r9          ! fetch return address
520         lds     r9, pr          ! put return address in pr
521         mov.l   2f, r4
522         mov.l   3f, r9
523         mov.l   @r4, r4         ! pass INTEVT vector as arg0
524         jmp     @r9
525          mov    r15, r5         ! pass saved registers as arg1
526
527 ENTRY(exception_none)
528         rts
529          nop
530
531         .align  L1_CACHE_SHIFT
532 exception_data:
533 0:      .long   0x000080f0      ! FD=1, IMASK=15
534 1:      .long   0xcfffffff      ! RB=0, BL=0
535 2:      .long   INTEVT
536 3:      .long   do_IRQ
537 4:      .long   ret_from_irq
538 5:      .long   EXPEVT
539 6:      .long   exception_handling_table
540 7:      .long   ret_from_exception