Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux
[pandora-kernel.git] / arch / xtensa / kernel / vectors.S
1 /*
2  * arch/xtensa/kernel/vectors.S
3  *
4  * This file contains all exception vectors (user, kernel, and double),
5  * as well as the window vectors (overflow and underflow), and the debug
6  * vector. These are the primary vectors executed by the processor if an
7  * exception occurs.
8  *
9  * This file is subject to the terms and conditions of the GNU General
10  * Public License.  See the file "COPYING" in the main directory of
11  * this archive for more details.
12  *
13  * Copyright (C) 2005 - 2008 Tensilica, Inc.
14  *
15  * Chris Zankel <chris@zankel.net>
16  *
17  */
18
19 /*
20  * We use a two-level table approach. The user and kernel exception vectors
21  * use a first-level dispatch table to dispatch the exception to a registered
22  * fast handler or the default handler, if no fast handler was registered.
23  * The default handler sets up a C-stack and dispatches the exception to a
24  * registerd C handler in the second-level dispatch table.
25  *
26  * Fast handler entry condition:
27  *
28  *   a0:        trashed, original value saved on stack (PT_AREG0)
29  *   a1:        a1
30  *   a2:        new stack pointer, original value in depc
31  *   a3:        dispatch table
32  *   depc:      a2, original value saved on stack (PT_DEPC)
33  *   excsave_1: a3
34  *
35  * The value for PT_DEPC saved to stack also functions as a boolean to
36  * indicate that the exception is either a double or a regular exception:
37  *
38  *   PT_DEPC    >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception
39  *              <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
40  *
41  * Note:  Neither the kernel nor the user exception handler generate literals.
42  *
43  */
44
45 #include <linux/linkage.h>
46 #include <asm/ptrace.h>
47 #include <asm/current.h>
48 #include <asm/asm-offsets.h>
49 #include <asm/pgtable.h>
50 #include <asm/processor.h>
51 #include <asm/page.h>
52 #include <asm/thread_info.h>
53 #include <asm/vectors.h>
54
55 #define WINDOW_VECTORS_SIZE   0x180
56
57
58 /*
59  * User exception vector. (Exceptions with PS.UM == 1, PS.EXCM == 0)
60  *
61  * We get here when an exception occurred while we were in userland.
62  * We switch to the kernel stack and jump to the first level handler
63  * associated to the exception cause.
64  *
65  * Note: the saved kernel stack pointer (EXC_TABLE_KSTK) is already
66  *       decremented by PT_USER_SIZE.
67  */
68
69         .section .UserExceptionVector.text, "ax"
70
71 ENTRY(_UserExceptionVector)
72
73         xsr     a3, excsave1            # save a3 and get dispatch table
74         wsr     a2, depc                # save a2
75         l32i    a2, a3, EXC_TABLE_KSTK  # load kernel stack to a2
76         s32i    a0, a2, PT_AREG0        # save a0 to ESF
77         rsr     a0, exccause            # retrieve exception cause
78         s32i    a0, a2, PT_DEPC         # mark it as a regular exception
79         addx4   a0, a0, a3              # find entry in table
80         l32i    a0, a0, EXC_TABLE_FAST_USER     # load handler
81         jx      a0
82
83 ENDPROC(_UserExceptionVector)
84
85 /*
86  * Kernel exception vector. (Exceptions with PS.UM == 0, PS.EXCM == 0)
87  *
88  * We get this exception when we were already in kernel space.
89  * We decrement the current stack pointer (kernel) by PT_SIZE and
90  * jump to the first-level handler associated with the exception cause.
91  *
92  * Note: we need to preserve space for the spill region.
93  */
94
95         .section .KernelExceptionVector.text, "ax"
96
97 ENTRY(_KernelExceptionVector)
98
99         xsr     a3, excsave1            # save a3, and get dispatch table
100         wsr     a2, depc                # save a2
101         addi    a2, a1, -16-PT_SIZE     # adjust stack pointer
102         s32i    a0, a2, PT_AREG0        # save a0 to ESF
103         rsr     a0, exccause            # retrieve exception cause
104         s32i    a0, a2, PT_DEPC         # mark it as a regular exception
105         addx4   a0, a0, a3              # find entry in table
106         l32i    a0, a0, EXC_TABLE_FAST_KERNEL   # load handler address
107         jx      a0
108
109 ENDPROC(_KernelExceptionVector)
110
111 /*
112  * Double exception vector (Exceptions with PS.EXCM == 1)
113  * We get this exception when another exception occurs while were are
114  * already in an exception, such as window overflow/underflow exception,
115  * or 'expected' exceptions, for example memory exception when we were trying
116  * to read data from an invalid address in user space.
117  *
118  * Note that this vector is never invoked for level-1 interrupts, because such
119  * interrupts are disabled (masked) when PS.EXCM is set.
120  *
121  * We decode the exception and take the appropriate action.  However, the
122  * double exception vector is much more careful, because a lot more error
123  * cases go through the double exception vector than through the user and
124  * kernel exception vectors.
125  *
126  * Occasionally, the kernel expects a double exception to occur.  This usually
127  * happens when accessing user-space memory with the user's permissions
128  * (l32e/s32e instructions).  The kernel state, though, is not always suitable
129  * for immediate transfer of control to handle_double, where "normal" exception
130  * processing occurs. Also in kernel mode, TLB misses can occur if accessing
131  * vmalloc memory, possibly requiring repair in a double exception handler.
132  *
133  * The variable at TABLE_FIXUP offset from the pointer in EXCSAVE_1 doubles as
134  * a boolean variable and a pointer to a fixup routine. If the variable
135  * EXC_TABLE_FIXUP is non-zero, this handler jumps to that address. A value of
136  * zero indicates to use the default kernel/user exception handler.
137  * There is only one exception, when the value is identical to the exc_table
138  * label, the kernel is in trouble. This mechanism is used to protect critical
139  * sections, mainly when the handler writes to the stack to assert the stack
140  * pointer is valid. Once the fixup/default handler leaves that area, the
141  * EXC_TABLE_FIXUP variable is reset to the fixup handler or zero.
142  *
143  * Procedures wishing to use this mechanism should set EXC_TABLE_FIXUP to the
144  * nonzero address of a fixup routine before it could cause a double exception
145  * and reset it before it returns.
146  *
147  * Some other things to take care of when a fast exception handler doesn't
148  * specify a particular fixup handler but wants to use the default handlers:
149  *
150  *  - The original stack pointer (in a1) must not be modified. The fast
151  *    exception handler should only use a2 as the stack pointer.
152  *
153  *  - If the fast handler manipulates the stack pointer (in a2), it has to
154  *    register a valid fixup handler and cannot use the default handlers.
155  *
156  *  - The handler can use any other generic register from a3 to a15, but it
157  *    must save the content of these registers to stack (PT_AREG3...PT_AREGx)
158  *
159  *  - These registers must be saved before a double exception can occur.
160  *
161  *  - If we ever implement handling signals while in double exceptions, the
162  *    number of registers a fast handler has saved (excluding a0 and a1) must
163  *    be written to  PT_AREG1. (1 if only a3 is used, 2 for a3 and a4, etc. )
164  *
165  * The fixup handlers are special handlers:
166  *
167  *  - Fixup entry conditions differ from regular exceptions:
168  *
169  *      a0:        DEPC
170  *      a1:        a1
171  *      a2:        trashed, original value in EXC_TABLE_DOUBLE_A2
172  *      a3:        exctable
173  *      depc:      a0
174  *      excsave_1: a3
175  *
176  *  - When the kernel enters the fixup handler, it still assumes it is in a
177  *    critical section, so EXC_TABLE_FIXUP variable is set to exc_table.
178  *    The fixup handler, therefore, has to re-register itself as the fixup
179  *    handler before it returns from the double exception.
180  *
181  *  - Fixup handler can share the same exception frame with the fast handler.
182  *    The kernel stack pointer is not changed when entering the fixup handler.
183  *
184  *  - Fixup handlers can jump to the default kernel and user exception
185  *    handlers. Before it jumps, though, it has to setup a exception frame
186  *    on stack. Because the default handler resets the register fixup handler
187  *    the fixup handler must make sure that the default handler returns to
188  *    it instead of the exception address, so it can re-register itself as
189  *    the fixup handler.
190  *
191  * In case of a critical condition where the kernel cannot recover, we jump
192  * to unrecoverable_exception with the following entry conditions.
193  * All registers a0...a15 are unchanged from the last exception, except:
194  *
195  *      a0:        last address before we jumped to the unrecoverable_exception.
196  *      excsave_1: a0
197  *
198  *
199  * See the handle_alloca_user and spill_registers routines for example clients.
200  *
201  * FIXME: Note: we currently don't allow signal handling coming from a double
202  *        exception, so the item markt with (*) is not required.
203  */
204
205         .section .DoubleExceptionVector.text, "ax"
206         .begin literal_prefix .DoubleExceptionVector
207
208 ENTRY(_DoubleExceptionVector)
209
210         /* Deliberately destroy excsave (don't assume it's value was valid). */
211
212         wsr     a3, excsave1            # save a3
213
214         /* Check for kernel double exception (usually fatal). */
215
216         rsr     a3, ps
217         _bbci.l a3, PS_UM_BIT, .Lksp
218
219         /* Check if we are currently handling a window exception. */
220         /* Note: We don't need to indicate that we enter a critical section. */
221
222         xsr     a0, depc                # get DEPC, save a0
223
224         movi    a3, WINDOW_VECTORS_VADDR
225         _bltu   a0, a3, .Lfixup
226         addi    a3, a3, WINDOW_VECTORS_SIZE
227         _bgeu   a0, a3, .Lfixup
228
229         /* Window overflow/underflow exception. Get stack pointer. */
230
231         mov     a3, a2
232         /* This explicit literal and the following references to it are made
233          * in order to fit DoubleExceptionVector.literals into the available
234          * 16-byte gap before DoubleExceptionVector.text in the absence of
235          * link time relaxation. See kernel/vmlinux.lds.S
236          */
237         .literal .Lexc_table, exc_table
238         l32r    a2, .Lexc_table
239         l32i    a2, a2, EXC_TABLE_KSTK
240
241         /* Check for overflow/underflow exception, jump if overflow. */
242
243         _bbci.l a0, 6, .Lovfl
244
245         /* a0: depc, a1: a1, a2: kstk, a3: a2, depc: a0, excsave: a3  */
246
247         /* Restart window underflow exception.
248          * We return to the instruction in user space that caused the window
249          * underflow exception. Therefore, we change window base to the value
250          * before we entered the window underflow exception and prepare the
251          * registers to return as if we were coming from a regular exception
252          * by changing depc (in a0).
253          * Note: We can trash the current window frame (a0...a3) and depc!
254          */
255
256         wsr     a2, depc                # save stack pointer temporarily
257         rsr     a0, ps
258         extui   a0, a0, PS_OWB_SHIFT, 4
259         wsr     a0, windowbase
260         rsync
261
262         /* We are now in the previous window frame. Save registers again. */
263
264         xsr     a2, depc                # save a2 and get stack pointer
265         s32i    a0, a2, PT_AREG0
266
267         wsr     a3, excsave1            # save a3
268         l32r    a3, .Lexc_table
269
270         rsr     a0, exccause
271         s32i    a0, a2, PT_DEPC         # mark it as a regular exception
272         addx4   a0, a0, a3
273         l32i    a0, a0, EXC_TABLE_FAST_USER
274         jx      a0
275
276 .Lfixup:/* Check for a fixup handler or if we were in a critical section. */
277
278         /* a0: depc, a1: a1, a2: a2, a3: trashed, depc: a0, excsave1: a3 */
279
280         l32r    a3, .Lexc_table
281         s32i    a2, a3, EXC_TABLE_DOUBLE_SAVE   # temporary variable
282
283         /* Enter critical section. */
284
285         l32i    a2, a3, EXC_TABLE_FIXUP
286         s32i    a3, a3, EXC_TABLE_FIXUP
287         beq     a2, a3, .Lunrecoverable_fixup   # critical!
288         beqz    a2, .Ldflt                      # no handler was registered
289
290         /* a0: depc, a1: a1, a2: trash, a3: exctable, depc: a0, excsave: a3 */
291
292         jx      a2
293
294 .Ldflt: /* Get stack pointer. */
295
296         l32i    a3, a3, EXC_TABLE_DOUBLE_SAVE
297         addi    a2, a3, -PT_USER_SIZE
298
299 .Lovfl: /* Jump to default handlers. */
300
301         /* a0: depc, a1: a1, a2: kstk, a3: a2, depc: a0, excsave: a3 */
302
303         xsr     a3, depc
304         s32i    a0, a2, PT_DEPC
305         s32i    a3, a2, PT_AREG0
306
307         /* a0: avail, a1: a1, a2: kstk, a3: avail, depc: a2, excsave: a3 */
308
309         l32r    a3, .Lexc_table
310         rsr     a0, exccause
311         addx4   a0, a0, a3
312         l32i    a0, a0, EXC_TABLE_FAST_USER
313         jx      a0
314
315         /*
316          * We only allow the ITLB miss exception if we are in kernel space.
317          * All other exceptions are unexpected and thus unrecoverable!
318          */
319
320 #ifdef CONFIG_MMU
321         .extern fast_second_level_miss_double_kernel
322
323 .Lksp:  /* a0: a0, a1: a1, a2: a2, a3: trashed, depc: depc, excsave: a3 */
324
325         rsr     a3, exccause
326         beqi    a3, EXCCAUSE_ITLB_MISS, 1f
327         addi    a3, a3, -EXCCAUSE_DTLB_MISS
328         bnez    a3, .Lunrecoverable
329 1:      movi    a3, fast_second_level_miss_double_kernel
330         jx      a3
331 #else
332 .equ    .Lksp,  .Lunrecoverable
333 #endif
334
335         /* Critical! We can't handle this situation. PANIC! */
336
337         .extern unrecoverable_exception
338
339 .Lunrecoverable_fixup:
340         l32i    a2, a3, EXC_TABLE_DOUBLE_SAVE
341         xsr     a0, depc
342
343 .Lunrecoverable:
344         rsr     a3, excsave1
345         wsr     a0, excsave1
346         movi    a0, unrecoverable_exception
347         callx0  a0
348
349         .end literal_prefix
350
351 ENDPROC(_DoubleExceptionVector)
352
353 /*
354  * Debug interrupt vector
355  *
356  * There is not much space here, so simply jump to another handler.
357  * EXCSAVE[DEBUGLEVEL] has been set to that handler.
358  */
359
360         .section .DebugInterruptVector.text, "ax"
361
362 ENTRY(_DebugInterruptVector)
363
364         xsr     a0, SREG_EXCSAVE + XCHAL_DEBUGLEVEL
365         jx      a0
366
367 ENDPROC(_DebugInterruptVector)
368
369
370
371 /*
372  * Medium priority level interrupt vectors
373  *
374  * Each takes less than 16 (0x10) bytes, no literals, by placing
375  * the extra 8 bytes that would otherwise be required in the window
376  * vectors area where there is space.  With relocatable vectors,
377  * all vectors are within ~ 4 kB range of each other, so we can
378  * simply jump (J) to another vector without having to use JX.
379  *
380  * common_exception code gets current IRQ level in PS.INTLEVEL
381  * and preserves it for the IRQ handling time.
382  */
383
384         .macro  irq_entry_level level
385
386         .if     XCHAL_EXCM_LEVEL >= \level
387         .section .Level\level\()InterruptVector.text, "ax"
388 ENTRY(_Level\level\()InterruptVector)
389         wsr     a0, excsave2
390         rsr     a0, epc\level
391         wsr     a0, epc1
392         movi    a0, EXCCAUSE_LEVEL1_INTERRUPT
393         wsr     a0, exccause
394         rsr     a0, eps\level
395                                         # branch to user or kernel vector
396         j       _SimulateUserKernelVectorException
397         .endif
398
399         .endm
400
401         irq_entry_level 2
402         irq_entry_level 3
403         irq_entry_level 4
404         irq_entry_level 5
405         irq_entry_level 6
406
407
408 /* Window overflow and underflow handlers.
409  * The handlers must be 64 bytes apart, first starting with the underflow
410  * handlers underflow-4 to underflow-12, then the overflow handlers
411  * overflow-4 to overflow-12.
412  *
413  * Note: We rerun the underflow handlers if we hit an exception, so
414  *       we try to access any page that would cause a page fault early.
415  */
416
417 #define ENTRY_ALIGN64(name)     \
418         .globl name;            \
419         .align 64;              \
420         name:
421
422         .section                .WindowVectors.text, "ax"
423
424
425 /* 4-Register Window Overflow Vector (Handler) */
426
427 ENTRY_ALIGN64(_WindowOverflow4)
428
429         s32e    a0, a5, -16
430         s32e    a1, a5, -12
431         s32e    a2, a5,  -8
432         s32e    a3, a5,  -4
433         rfwo
434
435 ENDPROC(_WindowOverflow4)
436
437
438 #if XCHAL_EXCM_LEVEL >= 2
439         /*  Not a window vector - but a convenient location
440          *  (where we know there's space) for continuation of
441          *  medium priority interrupt dispatch code.
442          *  On entry here, a0 contains PS, and EPC2 contains saved a0:
443          */
444         .align 4
445 _SimulateUserKernelVectorException:
446         addi    a0, a0, (1 << PS_EXCM_BIT)
447         wsr     a0, ps
448         bbsi.l  a0, PS_UM_BIT, 1f       # branch if user mode
449         rsr     a0, excsave2            # restore a0
450         j       _KernelExceptionVector  # simulate kernel vector exception
451 1:      rsr     a0, excsave2            # restore a0
452         j       _UserExceptionVector    # simulate user vector exception
453 #endif
454
455
456 /* 4-Register Window Underflow Vector (Handler) */
457
458 ENTRY_ALIGN64(_WindowUnderflow4)
459
460         l32e    a0, a5, -16
461         l32e    a1, a5, -12
462         l32e    a2, a5,  -8
463         l32e    a3, a5,  -4
464         rfwu
465
466 ENDPROC(_WindowUnderflow4)
467
468 /* 8-Register Window Overflow Vector (Handler) */
469
470 ENTRY_ALIGN64(_WindowOverflow8)
471
472         s32e    a0, a9, -16
473         l32e    a0, a1, -12
474         s32e    a2, a9,  -8
475         s32e    a1, a9, -12
476         s32e    a3, a9,  -4
477         s32e    a4, a0, -32
478         s32e    a5, a0, -28
479         s32e    a6, a0, -24
480         s32e    a7, a0, -20
481         rfwo
482
483 ENDPROC(_WindowOverflow8)
484
485 /* 8-Register Window Underflow Vector (Handler) */
486
487 ENTRY_ALIGN64(_WindowUnderflow8)
488
489         l32e    a1, a9, -12
490         l32e    a0, a9, -16
491         l32e    a7, a1, -12
492         l32e    a2, a9,  -8
493         l32e    a4, a7, -32
494         l32e    a3, a9,  -4
495         l32e    a5, a7, -28
496         l32e    a6, a7, -24
497         l32e    a7, a7, -20
498         rfwu
499
500 ENDPROC(_WindowUnderflow8)
501
502 /* 12-Register Window Overflow Vector (Handler) */
503
504 ENTRY_ALIGN64(_WindowOverflow12)
505
506         s32e    a0,  a13, -16
507         l32e    a0,  a1,  -12
508         s32e    a1,  a13, -12
509         s32e    a2,  a13,  -8
510         s32e    a3,  a13,  -4
511         s32e    a4,  a0,  -48
512         s32e    a5,  a0,  -44
513         s32e    a6,  a0,  -40
514         s32e    a7,  a0,  -36
515         s32e    a8,  a0,  -32
516         s32e    a9,  a0,  -28
517         s32e    a10, a0,  -24
518         s32e    a11, a0,  -20
519         rfwo
520
521 ENDPROC(_WindowOverflow12)
522
523 /* 12-Register Window Underflow Vector (Handler) */
524
525 ENTRY_ALIGN64(_WindowUnderflow12)
526
527         l32e    a1,  a13, -12
528         l32e    a0,  a13, -16
529         l32e    a11, a1,  -12
530         l32e    a2,  a13,  -8
531         l32e    a4,  a11, -48
532         l32e    a8,  a11, -32
533         l32e    a3,  a13,  -4
534         l32e    a5,  a11, -44
535         l32e    a6,  a11, -40
536         l32e    a7,  a11, -36
537         l32e    a9,  a11, -28
538         l32e    a10, a11, -24
539         l32e    a11, a11, -20
540         rfwu
541
542 ENDPROC(_WindowUnderflow12)
543
544         .text