xref: /linux/arch/xtensa/kernel/vectors.S (revision 95298d63c67673c654c08952672d016212b26054)
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 <linux/pgtable.h>
47#include <asm/asmmacro.h>
48#include <asm/ptrace.h>
49#include <asm/current.h>
50#include <asm/asm-offsets.h>
51#include <asm/processor.h>
52#include <asm/page.h>
53#include <asm/thread_info.h>
54#include <asm/vectors.h>
55
56#define WINDOW_VECTORS_SIZE   0x180
57
58
59/*
60 * User exception vector. (Exceptions with PS.UM == 1, PS.EXCM == 0)
61 *
62 * We get here when an exception occurred while we were in userland.
63 * We switch to the kernel stack and jump to the first level handler
64 * associated to the exception cause.
65 *
66 * Note: the saved kernel stack pointer (EXC_TABLE_KSTK) is already
67 *       decremented by PT_USER_SIZE.
68 */
69
70	.section .UserExceptionVector.text, "ax"
71
72ENTRY(_UserExceptionVector)
73
74	xsr	a3, excsave1		# save a3 and get dispatch table
75	wsr	a2, depc		# save a2
76	l32i	a2, a3, EXC_TABLE_KSTK	# load kernel stack to a2
77	s32i	a0, a2, PT_AREG0	# save a0 to ESF
78	rsr	a0, exccause		# retrieve exception cause
79	s32i	a0, a2, PT_DEPC		# mark it as a regular exception
80	addx4	a0, a0, a3		# find entry in table
81	l32i	a0, a0, EXC_TABLE_FAST_USER	# load handler
82	xsr	a3, excsave1		# restore a3 and dispatch table
83	jx	a0
84
85ENDPROC(_UserExceptionVector)
86
87/*
88 * Kernel exception vector. (Exceptions with PS.UM == 0, PS.EXCM == 0)
89 *
90 * We get this exception when we were already in kernel space.
91 * We decrement the current stack pointer (kernel) by PT_SIZE and
92 * jump to the first-level handler associated with the exception cause.
93 *
94 * Note: we need to preserve space for the spill region.
95 */
96
97	.section .KernelExceptionVector.text, "ax"
98
99ENTRY(_KernelExceptionVector)
100
101	xsr	a3, excsave1		# save a3, and get dispatch table
102	wsr	a2, depc		# save a2
103	addi	a2, a1, -16-PT_SIZE	# adjust stack pointer
104	s32i	a0, a2, PT_AREG0	# save a0 to ESF
105	rsr	a0, exccause		# retrieve exception cause
106	s32i	a0, a2, PT_DEPC		# mark it as a regular exception
107	addx4	a0, a0, a3		# find entry in table
108	l32i	a0, a0, EXC_TABLE_FAST_KERNEL	# load handler address
109	xsr	a3, excsave1		# restore a3 and dispatch table
110	jx	a0
111
112ENDPROC(_KernelExceptionVector)
113
114/*
115 * Double exception vector (Exceptions with PS.EXCM == 1)
116 * We get this exception when another exception occurs while were are
117 * already in an exception, such as window overflow/underflow exception,
118 * or 'expected' exceptions, for example memory exception when we were trying
119 * to read data from an invalid address in user space.
120 *
121 * Note that this vector is never invoked for level-1 interrupts, because such
122 * interrupts are disabled (masked) when PS.EXCM is set.
123 *
124 * We decode the exception and take the appropriate action.  However, the
125 * double exception vector is much more careful, because a lot more error
126 * cases go through the double exception vector than through the user and
127 * kernel exception vectors.
128 *
129 * Occasionally, the kernel expects a double exception to occur.  This usually
130 * happens when accessing user-space memory with the user's permissions
131 * (l32e/s32e instructions).  The kernel state, though, is not always suitable
132 * for immediate transfer of control to handle_double, where "normal" exception
133 * processing occurs. Also in kernel mode, TLB misses can occur if accessing
134 * vmalloc memory, possibly requiring repair in a double exception handler.
135 *
136 * The variable at TABLE_FIXUP offset from the pointer in EXCSAVE_1 doubles as
137 * a boolean variable and a pointer to a fixup routine. If the variable
138 * EXC_TABLE_FIXUP is non-zero, this handler jumps to that address. A value of
139 * zero indicates to use the default kernel/user exception handler.
140 * There is only one exception, when the value is identical to the exc_table
141 * label, the kernel is in trouble. This mechanism is used to protect critical
142 * sections, mainly when the handler writes to the stack to assert the stack
143 * pointer is valid. Once the fixup/default handler leaves that area, the
144 * EXC_TABLE_FIXUP variable is reset to the fixup handler or zero.
145 *
146 * Procedures wishing to use this mechanism should set EXC_TABLE_FIXUP to the
147 * nonzero address of a fixup routine before it could cause a double exception
148 * and reset it before it returns.
149 *
150 * Some other things to take care of when a fast exception handler doesn't
151 * specify a particular fixup handler but wants to use the default handlers:
152 *
153 *  - The original stack pointer (in a1) must not be modified. The fast
154 *    exception handler should only use a2 as the stack pointer.
155 *
156 *  - If the fast handler manipulates the stack pointer (in a2), it has to
157 *    register a valid fixup handler and cannot use the default handlers.
158 *
159 *  - The handler can use any other generic register from a3 to a15, but it
160 *    must save the content of these registers to stack (PT_AREG3...PT_AREGx)
161 *
162 *  - These registers must be saved before a double exception can occur.
163 *
164 *  - If we ever implement handling signals while in double exceptions, the
165 *    number of registers a fast handler has saved (excluding a0 and a1) must
166 *    be written to  PT_AREG1. (1 if only a3 is used, 2 for a3 and a4, etc. )
167 *
168 * The fixup handlers are special handlers:
169 *
170 *  - Fixup entry conditions differ from regular exceptions:
171 *
172 *	a0:	   DEPC
173 *	a1: 	   a1
174 *	a2:	   trashed, original value in EXC_TABLE_DOUBLE_SAVE
175 *	a3:	   exctable
176 *	depc:	   a0
177 *	excsave_1: a3
178 *
179 *  - When the kernel enters the fixup handler, it still assumes it is in a
180 *    critical section, so EXC_TABLE_FIXUP variable is set to exc_table.
181 *    The fixup handler, therefore, has to re-register itself as the fixup
182 *    handler before it returns from the double exception.
183 *
184 *  - Fixup handler can share the same exception frame with the fast handler.
185 *    The kernel stack pointer is not changed when entering the fixup handler.
186 *
187 *  - Fixup handlers can jump to the default kernel and user exception
188 *    handlers. Before it jumps, though, it has to setup a exception frame
189 *    on stack. Because the default handler resets the register fixup handler
190 *    the fixup handler must make sure that the default handler returns to
191 *    it instead of the exception address, so it can re-register itself as
192 *    the fixup handler.
193 *
194 * In case of a critical condition where the kernel cannot recover, we jump
195 * to unrecoverable_exception with the following entry conditions.
196 * All registers a0...a15 are unchanged from the last exception, except:
197 *
198 *	a0:	   last address before we jumped to the unrecoverable_exception.
199 *	excsave_1: a0
200 *
201 *
202 * See the handle_alloca_user and spill_registers routines for example clients.
203 *
204 * FIXME: Note: we currently don't allow signal handling coming from a double
205 *        exception, so the item markt with (*) is not required.
206 */
207
208	.section .DoubleExceptionVector.text, "ax"
209
210ENTRY(_DoubleExceptionVector)
211
212	xsr	a3, excsave1
213	s32i	a2, a3, EXC_TABLE_DOUBLE_SAVE
214
215	/* Check for kernel double exception (usually fatal). */
216
217	rsr	a2, ps
218	_bbsi.l	a2, PS_UM_BIT, 1f
219	j	.Lksp
220
221	.align	4
222	.literal_position
2231:
224	/* Check if we are currently handling a window exception. */
225	/* Note: We don't need to indicate that we enter a critical section. */
226
227	xsr	a0, depc		# get DEPC, save a0
228
229	movi	a2, WINDOW_VECTORS_VADDR
230	_bltu	a0, a2, .Lfixup
231	addi	a2, a2, WINDOW_VECTORS_SIZE
232	_bgeu	a0, a2, .Lfixup
233
234	/* Window overflow/underflow exception. Get stack pointer. */
235
236	l32i	a2, a3, EXC_TABLE_KSTK
237
238	/* Check for overflow/underflow exception, jump if overflow. */
239
240	bbci.l	a0, 6, _DoubleExceptionVector_WindowOverflow
241
242	/*
243	 * Restart window underflow exception.
244	 * Currently:
245	 *	depc = orig a0,
246	 *	a0 = orig DEPC,
247	 *	a2 = new sp based on KSTK from exc_table
248	 *	a3 = excsave_1
249	 *	excsave_1 = orig a3
250	 *
251	 * We return to the instruction in user space that caused the window
252	 * underflow exception. Therefore, we change window base to the value
253	 * before we entered the window underflow exception and prepare the
254	 * registers to return as if we were coming from a regular exception
255	 * by changing depc (in a0).
256	 * Note: We can trash the current window frame (a0...a3) and depc!
257	 */
258_DoubleExceptionVector_WindowUnderflow:
259	xsr	a3, excsave1
260	wsr	a2, depc		# save stack pointer temporarily
261	rsr	a0, ps
262	extui	a0, a0, PS_OWB_SHIFT, PS_OWB_WIDTH
263	wsr	a0, windowbase
264	rsync
265
266	/* We are now in the previous window frame. Save registers again. */
267
268	xsr	a2, depc		# save a2 and get stack pointer
269	s32i	a0, a2, PT_AREG0
270	xsr	a3, excsave1
271	rsr	a0, exccause
272	s32i	a0, a2, PT_DEPC		# mark it as a regular exception
273	addx4	a0, a0, a3
274	xsr	a3, excsave1
275	l32i	a0, a0, EXC_TABLE_FAST_USER
276	jx	a0
277
278	/*
279	 * We only allow the ITLB miss exception if we are in kernel space.
280	 * All other exceptions are unexpected and thus unrecoverable!
281	 */
282
283#ifdef CONFIG_MMU
284	.extern fast_second_level_miss_double_kernel
285
286.Lksp:	/* a0: a0, a1: a1, a2: a2, a3: trashed, depc: depc, excsave: a3 */
287
288	rsr	a3, exccause
289	beqi	a3, EXCCAUSE_ITLB_MISS, 1f
290	addi	a3, a3, -EXCCAUSE_DTLB_MISS
291	bnez	a3, .Lunrecoverable
2921:	movi	a3, fast_second_level_miss_double_kernel
293	jx	a3
294#else
295.equ	.Lksp,	.Lunrecoverable
296#endif
297
298	/* Critical! We can't handle this situation. PANIC! */
299
300	.extern unrecoverable_exception
301
302.Lunrecoverable_fixup:
303	l32i	a2, a3, EXC_TABLE_DOUBLE_SAVE
304	xsr	a0, depc
305
306.Lunrecoverable:
307	rsr	a3, excsave1
308	wsr	a0, excsave1
309	call0	unrecoverable_exception
310
311.Lfixup:/* Check for a fixup handler or if we were in a critical section. */
312
313	/* a0: depc, a1: a1, a2: trash, a3: exctable, depc: a0, excsave1: a3 */
314
315	/* Enter critical section. */
316
317	l32i	a2, a3, EXC_TABLE_FIXUP
318	s32i	a3, a3, EXC_TABLE_FIXUP
319	beq	a2, a3, .Lunrecoverable_fixup	# critical section
320	beqz	a2, .Ldflt			# no handler was registered
321
322	/* a0: depc, a1: a1, a2: trash, a3: exctable, depc: a0, excsave: a3 */
323
324	jx	a2
325
326.Ldflt:	/* Get stack pointer. */
327
328	l32i	a2, a3, EXC_TABLE_DOUBLE_SAVE
329	addi	a2, a2, -PT_USER_SIZE
330
331	/* a0: depc, a1: a1, a2: kstk, a3: exctable, depc: a0, excsave: a3 */
332
333	s32i	a0, a2, PT_DEPC
334	l32i	a0, a3, EXC_TABLE_DOUBLE_SAVE
335	xsr	a0, depc
336	s32i	a0, a2, PT_AREG0
337
338	/* a0: avail, a1: a1, a2: kstk, a3: exctable, depc: a2, excsave: a3 */
339
340	rsr	a0, exccause
341	addx4	a0, a0, a3
342	xsr	a3, excsave1
343	l32i	a0, a0, EXC_TABLE_FAST_USER
344	jx	a0
345
346	/*
347	 * Restart window OVERFLOW exception.
348	 * Currently:
349	 *	depc = orig a0,
350	 *	a0 = orig DEPC,
351	 *	a2 = new sp based on KSTK from exc_table
352	 *	a3 = EXCSAVE_1
353	 *	excsave_1 = orig a3
354	 *
355	 * We return to the instruction in user space that caused the window
356	 * overflow exception. Therefore, we change window base to the value
357	 * before we entered the window overflow exception and prepare the
358	 * registers to return as if we were coming from a regular exception
359	 * by changing DEPC (in a0).
360	 *
361	 * NOTE: We CANNOT trash the current window frame (a0...a3), but we
362	 * can clobber depc.
363	 *
364	 * The tricky part here is that overflow8 and overflow12 handlers
365	 * save a0, then clobber a0.  To restart the handler, we have to restore
366	 * a0 if the double exception was past the point where a0 was clobbered.
367	 *
368	 * To keep things simple, we take advantage of the fact all overflow
369	 * handlers save a0 in their very first instruction.  If DEPC was past
370	 * that instruction, we can safely restore a0 from where it was saved
371	 * on the stack.
372	 *
373	 * a0: depc, a1: a1, a2: kstk, a3: exc_table, depc: a0, excsave1: a3
374	 */
375_DoubleExceptionVector_WindowOverflow:
376	extui	a2, a0, 0, 6	# get offset into 64-byte vector handler
377	beqz	a2, 1f		# if at start of vector, don't restore
378
379	addi	a0, a0, -128
380	bbsi.l	a0, 8, 1f	# don't restore except for overflow 8 and 12
381
382	/*
383	 * This fixup handler is for the extremely unlikely case where the
384	 * overflow handler's reference thru a0 gets a hardware TLB refill
385	 * that bumps out the (distinct, aliasing) TLB entry that mapped its
386	 * prior references thru a9/a13, and where our reference now thru
387	 * a9/a13 gets a 2nd-level miss exception (not hardware TLB refill).
388	 */
389	movi	a2, window_overflow_restore_a0_fixup
390	s32i	a2, a3, EXC_TABLE_FIXUP
391	l32i	a2, a3, EXC_TABLE_DOUBLE_SAVE
392	xsr	a3, excsave1
393
394	bbsi.l	a0, 7, 2f
395
396	/*
397	 * Restore a0 as saved by _WindowOverflow8().
398	 */
399
400	l32e	a0, a9, -16
401	wsr	a0, depc	# replace the saved a0
402	j	3f
403
4042:
405	/*
406	 * Restore a0 as saved by _WindowOverflow12().
407	 */
408
409	l32e	a0, a13, -16
410	wsr	a0, depc	# replace the saved a0
4113:
412	xsr	a3, excsave1
413	movi	a0, 0
414	s32i	a0, a3, EXC_TABLE_FIXUP
415	s32i	a2, a3, EXC_TABLE_DOUBLE_SAVE
4161:
417	/*
418	 * Restore WindowBase while leaving all address registers restored.
419	 * We have to use ROTW for this, because WSR.WINDOWBASE requires
420	 * an address register (which would prevent restore).
421	 *
422	 * Window Base goes from 0 ... 7 (Module 8)
423	 * Window Start is 8 bits; Ex: (0b1010 1010):0x55 from series of call4s
424	 */
425
426	rsr	a0, ps
427	extui	a0, a0, PS_OWB_SHIFT, PS_OWB_WIDTH
428	rsr	a2, windowbase
429	sub	a0, a2, a0
430	extui	a0, a0, 0, 3
431
432	l32i	a2, a3, EXC_TABLE_DOUBLE_SAVE
433	xsr	a3, excsave1
434	beqi	a0, 1, .L1pane
435	beqi	a0, 3, .L3pane
436
437	rsr	a0, depc
438	rotw	-2
439
440	/*
441	 * We are now in the user code's original window frame.
442	 * Process the exception as a user exception as if it was
443	 * taken by the user code.
444	 *
445	 * This is similar to the user exception vector,
446	 * except that PT_DEPC isn't set to EXCCAUSE.
447	 */
4481:
449	xsr	a3, excsave1
450	wsr	a2, depc
451	l32i	a2, a3, EXC_TABLE_KSTK
452	s32i	a0, a2, PT_AREG0
453	rsr	a0, exccause
454
455	s32i	a0, a2, PT_DEPC
456
457_DoubleExceptionVector_handle_exception:
458	addi	a0, a0, -EXCCAUSE_UNALIGNED
459	beqz	a0, 2f
460	addx4	a0, a0, a3
461	l32i	a0, a0, EXC_TABLE_FAST_USER + 4 * EXCCAUSE_UNALIGNED
462	xsr	a3, excsave1
463	jx	a0
4642:
465	movi	a0, user_exception
466	xsr	a3, excsave1
467	jx	a0
468
469.L1pane:
470	rsr	a0, depc
471	rotw	-1
472	j	1b
473
474.L3pane:
475	rsr	a0, depc
476	rotw	-3
477	j	1b
478
479ENDPROC(_DoubleExceptionVector)
480
481/*
482 * Fixup handler for TLB miss in double exception handler for window owerflow.
483 * We get here with windowbase set to the window that was being spilled and
484 * a0 trashed. a0 bit 7 determines if this is a call8 (bit clear) or call12
485 * (bit set) window.
486 *
487 * We do the following here:
488 * - go to the original window retaining a0 value;
489 * - set up exception stack to return back to appropriate a0 restore code
490 *   (we'll need to rotate window back and there's no place to save this
491 *    information, use different return address for that);
492 * - handle the exception;
493 * - go to the window that was being spilled;
494 * - set up window_overflow_restore_a0_fixup as a fixup routine;
495 * - reload a0;
496 * - restore the original window;
497 * - reset the default fixup routine;
498 * - return to user. By the time we get to this fixup handler all information
499 *   about the conditions of the original double exception that happened in
500 *   the window overflow handler is lost, so we just return to userspace to
501 *   retry overflow from start.
502 *
503 * a0: value of depc, original value in depc
504 * a2: trashed, original value in EXC_TABLE_DOUBLE_SAVE
505 * a3: exctable, original value in excsave1
506 */
507
508	__XTENSA_HANDLER
509	.literal_position
510
511ENTRY(window_overflow_restore_a0_fixup)
512
513	rsr	a0, ps
514	extui	a0, a0, PS_OWB_SHIFT, PS_OWB_WIDTH
515	rsr	a2, windowbase
516	sub	a0, a2, a0
517	extui	a0, a0, 0, 3
518	l32i	a2, a3, EXC_TABLE_DOUBLE_SAVE
519	xsr	a3, excsave1
520
521	_beqi	a0, 1, .Lhandle_1
522	_beqi	a0, 3, .Lhandle_3
523
524	.macro	overflow_fixup_handle_exception_pane n
525
526	rsr	a0, depc
527	rotw	-\n
528
529	xsr	a3, excsave1
530	wsr	a2, depc
531	l32i	a2, a3, EXC_TABLE_KSTK
532	s32i	a0, a2, PT_AREG0
533
534	movi	a0, .Lrestore_\n
535	s32i	a0, a2, PT_DEPC
536	rsr	a0, exccause
537	j	_DoubleExceptionVector_handle_exception
538
539	.endm
540
541	overflow_fixup_handle_exception_pane 2
542.Lhandle_1:
543	overflow_fixup_handle_exception_pane 1
544.Lhandle_3:
545	overflow_fixup_handle_exception_pane 3
546
547	.macro	overflow_fixup_restore_a0_pane n
548
549	rotw	\n
550	/* Need to preserve a0 value here to be able to handle exception
551	 * that may occur on a0 reload from stack. It may occur because
552	 * TLB miss handler may not be atomic and pointer to page table
553	 * may be lost before we get here. There are no free registers,
554	 * so we need to use EXC_TABLE_DOUBLE_SAVE area.
555	 */
556	xsr	a3, excsave1
557	s32i	a2, a3, EXC_TABLE_DOUBLE_SAVE
558	movi	a2, window_overflow_restore_a0_fixup
559	s32i	a2, a3, EXC_TABLE_FIXUP
560	l32i	a2, a3, EXC_TABLE_DOUBLE_SAVE
561	xsr	a3, excsave1
562	bbsi.l	a0, 7, 1f
563	l32e	a0, a9, -16
564	j	2f
5651:
566	l32e	a0, a13, -16
5672:
568	rotw	-\n
569
570	.endm
571
572.Lrestore_2:
573	overflow_fixup_restore_a0_pane 2
574
575.Lset_default_fixup:
576	xsr	a3, excsave1
577	s32i	a2, a3, EXC_TABLE_DOUBLE_SAVE
578	movi	a2, 0
579	s32i	a2, a3, EXC_TABLE_FIXUP
580	l32i	a2, a3, EXC_TABLE_DOUBLE_SAVE
581	xsr	a3, excsave1
582	rfe
583
584.Lrestore_1:
585	overflow_fixup_restore_a0_pane 1
586	j	.Lset_default_fixup
587.Lrestore_3:
588	overflow_fixup_restore_a0_pane 3
589	j	.Lset_default_fixup
590
591ENDPROC(window_overflow_restore_a0_fixup)
592
593/*
594 * Debug interrupt vector
595 *
596 * There is not much space here, so simply jump to another handler.
597 * EXCSAVE[DEBUGLEVEL] has been set to that handler.
598 */
599
600	.section .DebugInterruptVector.text, "ax"
601
602ENTRY(_DebugInterruptVector)
603
604	xsr	a3, SREG_EXCSAVE + XCHAL_DEBUGLEVEL
605	s32i	a0, a3, DT_DEBUG_SAVE
606	l32i	a0, a3, DT_DEBUG_EXCEPTION
607	jx	a0
608
609ENDPROC(_DebugInterruptVector)
610
611
612
613/*
614 * Medium priority level interrupt vectors
615 *
616 * Each takes less than 16 (0x10) bytes, no literals, by placing
617 * the extra 8 bytes that would otherwise be required in the window
618 * vectors area where there is space.  With relocatable vectors,
619 * all vectors are within ~ 4 kB range of each other, so we can
620 * simply jump (J) to another vector without having to use JX.
621 *
622 * common_exception code gets current IRQ level in PS.INTLEVEL
623 * and preserves it for the IRQ handling time.
624 */
625
626	.macro	irq_entry_level level
627
628	.if	XCHAL_EXCM_LEVEL >= \level
629	.section .Level\level\()InterruptVector.text, "ax"
630ENTRY(_Level\level\()InterruptVector)
631	wsr	a0, excsave2
632	rsr	a0, epc\level
633	wsr	a0, epc1
634	.if	\level <= LOCKLEVEL
635	movi	a0, EXCCAUSE_LEVEL1_INTERRUPT
636	.else
637	movi	a0, EXCCAUSE_MAPPED_NMI
638	.endif
639	wsr	a0, exccause
640	rsr	a0, eps\level
641					# branch to user or kernel vector
642	j	_SimulateUserKernelVectorException
643	.endif
644
645	.endm
646
647	irq_entry_level 2
648	irq_entry_level 3
649	irq_entry_level 4
650	irq_entry_level 5
651	irq_entry_level 6
652
653
654/* Window overflow and underflow handlers.
655 * The handlers must be 64 bytes apart, first starting with the underflow
656 * handlers underflow-4 to underflow-12, then the overflow handlers
657 * overflow-4 to overflow-12.
658 *
659 * Note: We rerun the underflow handlers if we hit an exception, so
660 *	 we try to access any page that would cause a page fault early.
661 */
662
663#define ENTRY_ALIGN64(name)	\
664	.globl name;		\
665	.align 64;		\
666	name:
667
668	.section		.WindowVectors.text, "ax"
669
670
671/* 4-Register Window Overflow Vector (Handler) */
672
673ENTRY_ALIGN64(_WindowOverflow4)
674
675	s32e	a0, a5, -16
676	s32e	a1, a5, -12
677	s32e	a2, a5,  -8
678	s32e	a3, a5,  -4
679	rfwo
680
681ENDPROC(_WindowOverflow4)
682
683
684#if XCHAL_EXCM_LEVEL >= 2
685	/*  Not a window vector - but a convenient location
686	 *  (where we know there's space) for continuation of
687	 *  medium priority interrupt dispatch code.
688	 *  On entry here, a0 contains PS, and EPC2 contains saved a0:
689	 */
690	.align 4
691_SimulateUserKernelVectorException:
692	addi	a0, a0, (1 << PS_EXCM_BIT)
693#if !XTENSA_FAKE_NMI
694	wsr	a0, ps
695#endif
696	bbsi.l	a0, PS_UM_BIT, 1f	# branch if user mode
697	xsr	a0, excsave2		# restore a0
698	j	_KernelExceptionVector	# simulate kernel vector exception
6991:	xsr	a0, excsave2		# restore a0
700	j	_UserExceptionVector	# simulate user vector exception
701#endif
702
703
704/* 4-Register Window Underflow Vector (Handler) */
705
706ENTRY_ALIGN64(_WindowUnderflow4)
707
708	l32e	a0, a5, -16
709	l32e	a1, a5, -12
710	l32e	a2, a5,  -8
711	l32e	a3, a5,  -4
712	rfwu
713
714ENDPROC(_WindowUnderflow4)
715
716/* 8-Register Window Overflow Vector (Handler) */
717
718ENTRY_ALIGN64(_WindowOverflow8)
719
720	s32e	a0, a9, -16
721	l32e	a0, a1, -12
722	s32e	a2, a9,  -8
723	s32e	a1, a9, -12
724	s32e	a3, a9,  -4
725	s32e	a4, a0, -32
726	s32e	a5, a0, -28
727	s32e	a6, a0, -24
728	s32e	a7, a0, -20
729	rfwo
730
731ENDPROC(_WindowOverflow8)
732
733/* 8-Register Window Underflow Vector (Handler) */
734
735ENTRY_ALIGN64(_WindowUnderflow8)
736
737	l32e	a1, a9, -12
738	l32e	a0, a9, -16
739	l32e	a7, a1, -12
740	l32e	a2, a9,  -8
741	l32e	a4, a7, -32
742	l32e	a3, a9,  -4
743	l32e	a5, a7, -28
744	l32e	a6, a7, -24
745	l32e	a7, a7, -20
746	rfwu
747
748ENDPROC(_WindowUnderflow8)
749
750/* 12-Register Window Overflow Vector (Handler) */
751
752ENTRY_ALIGN64(_WindowOverflow12)
753
754	s32e	a0,  a13, -16
755	l32e	a0,  a1,  -12
756	s32e	a1,  a13, -12
757	s32e	a2,  a13,  -8
758	s32e	a3,  a13,  -4
759	s32e	a4,  a0,  -48
760	s32e	a5,  a0,  -44
761	s32e	a6,  a0,  -40
762	s32e	a7,  a0,  -36
763	s32e	a8,  a0,  -32
764	s32e	a9,  a0,  -28
765	s32e	a10, a0,  -24
766	s32e	a11, a0,  -20
767	rfwo
768
769ENDPROC(_WindowOverflow12)
770
771/* 12-Register Window Underflow Vector (Handler) */
772
773ENTRY_ALIGN64(_WindowUnderflow12)
774
775	l32e	a1,  a13, -12
776	l32e	a0,  a13, -16
777	l32e	a11, a1,  -12
778	l32e	a2,  a13,  -8
779	l32e	a4,  a11, -48
780	l32e	a8,  a11, -32
781	l32e	a3,  a13,  -4
782	l32e	a5,  a11, -44
783	l32e	a6,  a11, -40
784	l32e	a7,  a11, -36
785	l32e	a9,  a11, -28
786	l32e	a10, a11, -24
787	l32e	a11, a11, -20
788	rfwu
789
790ENDPROC(_WindowUnderflow12)
791
792	.text
793