xref: /linux-6.15/arch/xtensa/kernel/entry.S (revision 7d5f6a9a)
15a0015d6SChris Zankel/*
25a0015d6SChris Zankel * arch/xtensa/kernel/entry.S
35a0015d6SChris Zankel *
45a0015d6SChris Zankel * Low-level exception handling
55a0015d6SChris Zankel *
65a0015d6SChris Zankel * This file is subject to the terms and conditions of the GNU General Public
75a0015d6SChris Zankel * License.  See the file "COPYING" in the main directory of this archive
85a0015d6SChris Zankel * for more details.
95a0015d6SChris Zankel *
102d1c645cSMarc Gauthier * Copyright (C) 2004 - 2008 by Tensilica Inc.
115a0015d6SChris Zankel *
125a0015d6SChris Zankel * Chris Zankel <[email protected]>
135a0015d6SChris Zankel *
145a0015d6SChris Zankel */
155a0015d6SChris Zankel
165a0015d6SChris Zankel#include <linux/linkage.h>
170013a854SSam Ravnborg#include <asm/asm-offsets.h>
185a0015d6SChris Zankel#include <asm/processor.h>
194573e398SChris Zankel#include <asm/coprocessor.h>
205a0015d6SChris Zankel#include <asm/thread_info.h>
215a0015d6SChris Zankel#include <asm/uaccess.h>
225a0015d6SChris Zankel#include <asm/unistd.h>
235a0015d6SChris Zankel#include <asm/ptrace.h>
245a0015d6SChris Zankel#include <asm/current.h>
255a0015d6SChris Zankel#include <asm/pgtable.h>
265a0015d6SChris Zankel#include <asm/page.h>
275a0015d6SChris Zankel#include <asm/signal.h>
28173d6681SChris Zankel#include <asm/tlbflush.h>
29367b8112SChris Zankel#include <variant/tie-asm.h>
305a0015d6SChris Zankel
315a0015d6SChris Zankel/* Unimplemented features. */
325a0015d6SChris Zankel
335a0015d6SChris Zankel#undef KERNEL_STACK_OVERFLOW_CHECK
345a0015d6SChris Zankel
355a0015d6SChris Zankel/* Not well tested.
365a0015d6SChris Zankel *
375a0015d6SChris Zankel * - fast_coprocessor
385a0015d6SChris Zankel */
395a0015d6SChris Zankel
405a0015d6SChris Zankel/*
415a0015d6SChris Zankel * Macro to find first bit set in WINDOWBASE from the left + 1
425a0015d6SChris Zankel *
435a0015d6SChris Zankel * 100....0 -> 1
445a0015d6SChris Zankel * 010....0 -> 2
455a0015d6SChris Zankel * 000....1 -> WSBITS
465a0015d6SChris Zankel */
475a0015d6SChris Zankel
485a0015d6SChris Zankel	.macro ffs_ws bit mask
495a0015d6SChris Zankel
505a0015d6SChris Zankel#if XCHAL_HAVE_NSA
515a0015d6SChris Zankel	nsau    \bit, \mask			# 32-WSBITS ... 31 (32 iff 0)
525a0015d6SChris Zankel	addi    \bit, \bit, WSBITS - 32 + 1   	# uppest bit set -> return 1
535a0015d6SChris Zankel#else
545a0015d6SChris Zankel	movi    \bit, WSBITS
555a0015d6SChris Zankel#if WSBITS > 16
565a0015d6SChris Zankel	_bltui  \mask, 0x10000, 99f
575a0015d6SChris Zankel	addi    \bit, \bit, -16
585a0015d6SChris Zankel	extui   \mask, \mask, 16, 16
595a0015d6SChris Zankel#endif
605a0015d6SChris Zankel#if WSBITS > 8
615a0015d6SChris Zankel99:	_bltui  \mask, 0x100, 99f
625a0015d6SChris Zankel	addi    \bit, \bit, -8
635a0015d6SChris Zankel	srli    \mask, \mask, 8
645a0015d6SChris Zankel#endif
655a0015d6SChris Zankel99:	_bltui  \mask, 0x10, 99f
665a0015d6SChris Zankel	addi    \bit, \bit, -4
675a0015d6SChris Zankel	srli    \mask, \mask, 4
685a0015d6SChris Zankel99:	_bltui  \mask, 0x4, 99f
695a0015d6SChris Zankel	addi    \bit, \bit, -2
705a0015d6SChris Zankel	srli    \mask, \mask, 2
715a0015d6SChris Zankel99:	_bltui  \mask, 0x2, 99f
725a0015d6SChris Zankel	addi    \bit, \bit, -1
735a0015d6SChris Zankel99:
745a0015d6SChris Zankel
755a0015d6SChris Zankel#endif
765a0015d6SChris Zankel	.endm
775a0015d6SChris Zankel
785a0015d6SChris Zankel/* ----------------- DEFAULT FIRST LEVEL EXCEPTION HANDLERS ----------------- */
795a0015d6SChris Zankel
805a0015d6SChris Zankel/*
815a0015d6SChris Zankel * First-level exception handler for user exceptions.
825a0015d6SChris Zankel * Save some special registers, extra states and all registers in the AR
835a0015d6SChris Zankel * register file that were in use in the user task, and jump to the common
845a0015d6SChris Zankel * exception code.
855a0015d6SChris Zankel * We save SAR (used to calculate WMASK), and WB and WS (we don't have to
865a0015d6SChris Zankel * save them for kernel exceptions).
875a0015d6SChris Zankel *
885a0015d6SChris Zankel * Entry condition for user_exception:
895a0015d6SChris Zankel *
905a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
915a0015d6SChris Zankel *   a1:	a1
925a0015d6SChris Zankel *   a2:	new stack pointer, original value in depc
9399d5040eSMax Filippov *   a3:	a3
945a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
9599d5040eSMax Filippov *   excsave1:	dispatch table
965a0015d6SChris Zankel *
975a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
985a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
995a0015d6SChris Zankel *
1005a0015d6SChris Zankel * Entry condition for _user_exception:
1015a0015d6SChris Zankel *
1025a0015d6SChris Zankel *   a0-a3 and depc have been saved to PT_AREG0...PT_AREG3 and PT_DEPC
1035a0015d6SChris Zankel *   excsave has been restored, and
1045a0015d6SChris Zankel *   stack pointer (a1) has been set.
1055a0015d6SChris Zankel *
1063ad2f3fbSDaniel Mack * Note: _user_exception might be at an odd address. Don't use call0..call12
1075a0015d6SChris Zankel */
1085a0015d6SChris Zankel
1095a0015d6SChris ZankelENTRY(user_exception)
1105a0015d6SChris Zankel
11199d5040eSMax Filippov	/* Save a1, a2, a3, and set SP. */
1125a0015d6SChris Zankel
113bc5378fcSMax Filippov	rsr	a0, depc
1145a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
1155a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2
1165a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
1175a0015d6SChris Zankel	mov	a1, a2
1185a0015d6SChris Zankel
1195a0015d6SChris Zankel	.globl _user_exception
1205a0015d6SChris Zankel_user_exception:
1215a0015d6SChris Zankel
1225a0015d6SChris Zankel	/* Save SAR and turn off single stepping */
1235a0015d6SChris Zankel
1245a0015d6SChris Zankel	movi	a2, 0
125b6569439SMax Filippov	wsr	a2, depc		# terminate user stack trace with 0
126bc5378fcSMax Filippov	rsr	a3, sar
127bc5378fcSMax Filippov	xsr	a2, icountlevel
1285a0015d6SChris Zankel	s32i	a3, a1, PT_SAR
12929c4dfd9SChris Zankel	s32i	a2, a1, PT_ICOUNTLEVEL
1305a0015d6SChris Zankel
131c50842dfSChris Zankel#if XCHAL_HAVE_THREADPTR
132c50842dfSChris Zankel	rur	a2, threadptr
133c50842dfSChris Zankel	s32i	a2, a1, PT_THREADPTR
134c50842dfSChris Zankel#endif
135c50842dfSChris Zankel
1365a0015d6SChris Zankel	/* Rotate ws so that the current windowbase is at bit0. */
1375a0015d6SChris Zankel	/* Assume ws = xxwww1yyyy. Rotate ws right, so that a2 = yyyyxxwww1 */
1385a0015d6SChris Zankel
139bc5378fcSMax Filippov	rsr	a2, windowbase
140bc5378fcSMax Filippov	rsr	a3, windowstart
1415a0015d6SChris Zankel	ssr	a2
1425a0015d6SChris Zankel	s32i	a2, a1, PT_WINDOWBASE
1435a0015d6SChris Zankel	s32i	a3, a1, PT_WINDOWSTART
1445a0015d6SChris Zankel	slli	a2, a3, 32-WSBITS
1455a0015d6SChris Zankel	src	a2, a3, a2
1465a0015d6SChris Zankel	srli	a2, a2, 32-WSBITS
1475a0015d6SChris Zankel	s32i	a2, a1, PT_WMASK	# needed for restoring registers
1485a0015d6SChris Zankel
1495a0015d6SChris Zankel	/* Save only live registers. */
1505a0015d6SChris Zankel
1515a0015d6SChris Zankel	_bbsi.l	a2, 1, 1f
1525a0015d6SChris Zankel	s32i	a4, a1, PT_AREG4
1535a0015d6SChris Zankel	s32i	a5, a1, PT_AREG5
1545a0015d6SChris Zankel	s32i	a6, a1, PT_AREG6
1555a0015d6SChris Zankel	s32i	a7, a1, PT_AREG7
1565a0015d6SChris Zankel	_bbsi.l	a2, 2, 1f
1575a0015d6SChris Zankel	s32i	a8, a1, PT_AREG8
1585a0015d6SChris Zankel	s32i	a9, a1, PT_AREG9
1595a0015d6SChris Zankel	s32i	a10, a1, PT_AREG10
1605a0015d6SChris Zankel	s32i	a11, a1, PT_AREG11
1615a0015d6SChris Zankel	_bbsi.l	a2, 3, 1f
1625a0015d6SChris Zankel	s32i	a12, a1, PT_AREG12
1635a0015d6SChris Zankel	s32i	a13, a1, PT_AREG13
1645a0015d6SChris Zankel	s32i	a14, a1, PT_AREG14
1655a0015d6SChris Zankel	s32i	a15, a1, PT_AREG15
1665a0015d6SChris Zankel	_bnei	a2, 1, 1f		# only one valid frame?
1675a0015d6SChris Zankel
1685a0015d6SChris Zankel	/* Only one valid frame, skip saving regs. */
1695a0015d6SChris Zankel
1705a0015d6SChris Zankel	j	2f
1715a0015d6SChris Zankel
1725a0015d6SChris Zankel	/* Save the remaining registers.
1735a0015d6SChris Zankel	 * We have to save all registers up to the first '1' from
1745a0015d6SChris Zankel	 * the right, except the current frame (bit 0).
1755a0015d6SChris Zankel	 * Assume a2 is:  001001000110001
1766656920bSChris Zankel	 * All register frames starting from the top field to the marked '1'
1775a0015d6SChris Zankel	 * must be saved.
1785a0015d6SChris Zankel	 */
1795a0015d6SChris Zankel
1805a0015d6SChris Zankel1:	addi	a3, a2, -1		# eliminate '1' in bit 0: yyyyxxww0
1815a0015d6SChris Zankel	neg	a3, a3			# yyyyxxww0 -> YYYYXXWW1+1
1825a0015d6SChris Zankel	and	a3, a3, a2		# max. only one bit is set
1835a0015d6SChris Zankel
1845a0015d6SChris Zankel	/* Find number of frames to save */
1855a0015d6SChris Zankel
1865a0015d6SChris Zankel	ffs_ws	a0, a3			# number of frames to the '1' from left
1875a0015d6SChris Zankel
1885a0015d6SChris Zankel	/* Store information into WMASK:
1895a0015d6SChris Zankel	 * bits 0..3: xxx1 masked lower 4 bits of the rotated windowstart,
1905a0015d6SChris Zankel	 * bits 4...: number of valid 4-register frames
1915a0015d6SChris Zankel	 */
1925a0015d6SChris Zankel
1935a0015d6SChris Zankel	slli	a3, a0, 4		# number of frames to save in bits 8..4
1945a0015d6SChris Zankel	extui	a2, a2, 0, 4		# mask for the first 16 registers
1955a0015d6SChris Zankel	or	a2, a3, a2
1965a0015d6SChris Zankel	s32i	a2, a1, PT_WMASK	# needed when we restore the reg-file
1975a0015d6SChris Zankel
1985a0015d6SChris Zankel	/* Save 4 registers at a time */
1995a0015d6SChris Zankel
2005a0015d6SChris Zankel1:	rotw	-1
2015a0015d6SChris Zankel	s32i	a0, a5, PT_AREG_END - 16
2025a0015d6SChris Zankel	s32i	a1, a5, PT_AREG_END - 12
2035a0015d6SChris Zankel	s32i	a2, a5, PT_AREG_END - 8
2045a0015d6SChris Zankel	s32i	a3, a5, PT_AREG_END - 4
2055a0015d6SChris Zankel	addi	a0, a4, -1
2065a0015d6SChris Zankel	addi	a1, a5, -16
2075a0015d6SChris Zankel	_bnez	a0, 1b
2085a0015d6SChris Zankel
2095a0015d6SChris Zankel	/* WINDOWBASE still in SAR! */
2105a0015d6SChris Zankel
211bc5378fcSMax Filippov	rsr	a2, sar			# original WINDOWBASE
2125a0015d6SChris Zankel	movi	a3, 1
2135a0015d6SChris Zankel	ssl	a2
2145a0015d6SChris Zankel	sll	a3, a3
215bc5378fcSMax Filippov	wsr	a3, windowstart		# set corresponding WINDOWSTART bit
216bc5378fcSMax Filippov	wsr	a2, windowbase		# and WINDOWSTART
2175a0015d6SChris Zankel	rsync
2185a0015d6SChris Zankel
2195a0015d6SChris Zankel	/* We are back to the original stack pointer (a1) */
2205a0015d6SChris Zankel
221c658eac6SChris Zankel2:	/* Now, jump to the common exception handler. */
2225a0015d6SChris Zankel
2235a0015d6SChris Zankel	j	common_exception
2245a0015d6SChris Zankel
225d1538c46SChris ZankelENDPROC(user_exception)
2265a0015d6SChris Zankel
2275a0015d6SChris Zankel/*
2285a0015d6SChris Zankel * First-level exit handler for kernel exceptions
2295a0015d6SChris Zankel * Save special registers and the live window frame.
2305a0015d6SChris Zankel * Note: Even though we changes the stack pointer, we don't have to do a
2315a0015d6SChris Zankel *	 MOVSP here, as we do that when we return from the exception.
2325a0015d6SChris Zankel *	 (See comment in the kernel exception exit code)
2335a0015d6SChris Zankel *
2345a0015d6SChris Zankel * Entry condition for kernel_exception:
2355a0015d6SChris Zankel *
2365a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
2375a0015d6SChris Zankel *   a1:	a1
2385a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
23999d5040eSMax Filippov *   a3:	a3
2405a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
24199d5040eSMax Filippov *   excsave_1:	dispatch table
2425a0015d6SChris Zankel *
2435a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
2445a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
2455a0015d6SChris Zankel *
2465a0015d6SChris Zankel * Entry condition for _kernel_exception:
2475a0015d6SChris Zankel *
2485a0015d6SChris Zankel *   a0-a3 and depc have been saved to PT_AREG0...PT_AREG3 and PT_DEPC
2495a0015d6SChris Zankel *   excsave has been restored, and
2505a0015d6SChris Zankel *   stack pointer (a1) has been set.
2515a0015d6SChris Zankel *
2523ad2f3fbSDaniel Mack * Note: _kernel_exception might be at an odd address. Don't use call0..call12
2535a0015d6SChris Zankel */
2545a0015d6SChris Zankel
2555a0015d6SChris ZankelENTRY(kernel_exception)
2565a0015d6SChris Zankel
25799d5040eSMax Filippov	/* Save a1, a2, a3, and set SP. */
2585a0015d6SChris Zankel
259bc5378fcSMax Filippov	rsr	a0, depc		# get a2
2605a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
2615a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2
2625a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
2635a0015d6SChris Zankel	mov	a1, a2
2645a0015d6SChris Zankel
2655a0015d6SChris Zankel	.globl _kernel_exception
2665a0015d6SChris Zankel_kernel_exception:
2675a0015d6SChris Zankel
2685a0015d6SChris Zankel	/* Save SAR and turn off single stepping */
2695a0015d6SChris Zankel
2705a0015d6SChris Zankel	movi	a2, 0
271bc5378fcSMax Filippov	rsr	a3, sar
272bc5378fcSMax Filippov	xsr	a2, icountlevel
2735a0015d6SChris Zankel	s32i	a3, a1, PT_SAR
27429c4dfd9SChris Zankel	s32i	a2, a1, PT_ICOUNTLEVEL
2755a0015d6SChris Zankel
2765a0015d6SChris Zankel	/* Rotate ws so that the current windowbase is at bit0. */
2775a0015d6SChris Zankel	/* Assume ws = xxwww1yyyy. Rotate ws right, so that a2 = yyyyxxwww1 */
2785a0015d6SChris Zankel
279bc5378fcSMax Filippov	rsr	a2, windowbase		# don't need to save these, we only
280bc5378fcSMax Filippov	rsr	a3, windowstart		# need shifted windowstart: windowmask
2815a0015d6SChris Zankel	ssr	a2
2825a0015d6SChris Zankel	slli	a2, a3, 32-WSBITS
2835a0015d6SChris Zankel	src	a2, a3, a2
2845a0015d6SChris Zankel	srli	a2, a2, 32-WSBITS
2855a0015d6SChris Zankel	s32i	a2, a1, PT_WMASK	# needed for kernel_exception_exit
2865a0015d6SChris Zankel
2875a0015d6SChris Zankel	/* Save only the live window-frame */
2885a0015d6SChris Zankel
2895a0015d6SChris Zankel	_bbsi.l	a2, 1, 1f
2905a0015d6SChris Zankel	s32i	a4, a1, PT_AREG4
2915a0015d6SChris Zankel	s32i	a5, a1, PT_AREG5
2925a0015d6SChris Zankel	s32i	a6, a1, PT_AREG6
2935a0015d6SChris Zankel	s32i	a7, a1, PT_AREG7
2945a0015d6SChris Zankel	_bbsi.l	a2, 2, 1f
2955a0015d6SChris Zankel	s32i	a8, a1, PT_AREG8
2965a0015d6SChris Zankel	s32i	a9, a1, PT_AREG9
2975a0015d6SChris Zankel	s32i	a10, a1, PT_AREG10
2985a0015d6SChris Zankel	s32i	a11, a1, PT_AREG11
2995a0015d6SChris Zankel	_bbsi.l	a2, 3, 1f
3005a0015d6SChris Zankel	s32i	a12, a1, PT_AREG12
3015a0015d6SChris Zankel	s32i	a13, a1, PT_AREG13
3025a0015d6SChris Zankel	s32i	a14, a1, PT_AREG14
3035a0015d6SChris Zankel	s32i	a15, a1, PT_AREG15
3045a0015d6SChris Zankel
305b6569439SMax Filippov	_bnei	a2, 1, 1f
306b6569439SMax Filippov
307b6569439SMax Filippov	/* Copy spill slots of a0 and a1 to imitate movsp
308b6569439SMax Filippov	 * in order to keep exception stack continuous
309b6569439SMax Filippov	 */
310b6569439SMax Filippov	l32i	a3, a1, PT_SIZE
311b6569439SMax Filippov	l32i	a0, a1, PT_SIZE + 4
312b6569439SMax Filippov	s32e	a3, a1, -16
313b6569439SMax Filippov	s32e	a0, a1, -12
3145a0015d6SChris Zankel1:
315b6569439SMax Filippov	l32i	a0, a1, PT_AREG0	# restore saved a0
316b6569439SMax Filippov	wsr	a0, depc
3175a0015d6SChris Zankel
3185a0015d6SChris Zankel#ifdef KERNEL_STACK_OVERFLOW_CHECK
3195a0015d6SChris Zankel
3205a0015d6SChris Zankel	/*  Stack overflow check, for debugging  */
3215a0015d6SChris Zankel	extui	a2, a1, TASK_SIZE_BITS,XX
3225a0015d6SChris Zankel	movi	a3, SIZE??
3235a0015d6SChris Zankel	_bge	a2, a3, out_of_stack_panic
3245a0015d6SChris Zankel
3255a0015d6SChris Zankel#endif
3265a0015d6SChris Zankel
3275a0015d6SChris Zankel/*
3285a0015d6SChris Zankel * This is the common exception handler.
3295a0015d6SChris Zankel * We get here from the user exception handler or simply by falling through
3305a0015d6SChris Zankel * from the kernel exception handler.
3315a0015d6SChris Zankel * Save the remaining special registers, switch to kernel mode, and jump
3325a0015d6SChris Zankel * to the second-level exception handler.
3335a0015d6SChris Zankel *
3345a0015d6SChris Zankel */
3355a0015d6SChris Zankel
3365a0015d6SChris Zankelcommon_exception:
3375a0015d6SChris Zankel
33829c4dfd9SChris Zankel	/* Save some registers, disable loops and clear the syscall flag. */
3395a0015d6SChris Zankel
340bc5378fcSMax Filippov	rsr	a2, debugcause
341bc5378fcSMax Filippov	rsr	a3, epc1
3425a0015d6SChris Zankel	s32i	a2, a1, PT_DEBUGCAUSE
3435a0015d6SChris Zankel	s32i	a3, a1, PT_PC
3445a0015d6SChris Zankel
34529c4dfd9SChris Zankel	movi	a2, -1
346bc5378fcSMax Filippov	rsr	a3, excvaddr
34729c4dfd9SChris Zankel	s32i	a2, a1, PT_SYSCALL
3485a0015d6SChris Zankel	movi	a2, 0
3495a0015d6SChris Zankel	s32i	a3, a1, PT_EXCVADDR
350bc5378fcSMax Filippov	xsr	a2, lcount
3515a0015d6SChris Zankel	s32i	a2, a1, PT_LCOUNT
3525a0015d6SChris Zankel
3535a0015d6SChris Zankel	/* It is now save to restore the EXC_TABLE_FIXUP variable. */
3545a0015d6SChris Zankel
355bc5378fcSMax Filippov	rsr	a0, exccause
3565a0015d6SChris Zankel	movi	a3, 0
357bc5378fcSMax Filippov	rsr	a2, excsave1
3585a0015d6SChris Zankel	s32i	a0, a1, PT_EXCCAUSE
3595a0015d6SChris Zankel	s32i	a3, a2, EXC_TABLE_FIXUP
3605a0015d6SChris Zankel
361b6569439SMax Filippov	/* All unrecoverable states are saved on stack, now, and a1 is valid.
362b6569439SMax Filippov	 * Now we can allow exceptions again. In case we've got an interrupt
363b6569439SMax Filippov	 * PS.INTLEVEL is set to LOCKLEVEL disabling furhter interrupts,
364b6569439SMax Filippov	 * otherwise it's left unchanged.
3655a0015d6SChris Zankel	 *
366b6569439SMax Filippov	 * Set PS(EXCM = 0, UM = 0, RING = 0, OWB = 0, WOE = 1, INTLEVEL = X)
3675a0015d6SChris Zankel	 */
3685a0015d6SChris Zankel
369bc5378fcSMax Filippov	rsr	a3, ps
370895666a9SMax Filippov	addi	a0, a0, -EXCCAUSE_LEVEL1_INTERRUPT
371895666a9SMax Filippov	movi	a2, LOCKLEVEL
3722d1c645cSMarc Gauthier	extui	a3, a3, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
3732d1c645cSMarc Gauthier					# a3 = PS.INTLEVEL
374895666a9SMax Filippov	moveqz	a3, a2, a0		# a3 = LOCKLEVEL iff interrupt
375173d6681SChris Zankel	movi	a2, 1 << PS_WOE_BIT
3765a0015d6SChris Zankel	or	a3, a3, a2
377b6569439SMax Filippov	rsr	a2, exccause
378b6569439SMax Filippov	/* restore return address (or 0 if return to userspace) */
379b6569439SMax Filippov	rsr	a0, depc
380bc5378fcSMax Filippov	xsr	a3, ps
3815a0015d6SChris Zankel
3825a0015d6SChris Zankel	s32i	a3, a1, PT_PS		# save ps
3835a0015d6SChris Zankel
384bc5378fcSMax Filippov	/* Save lbeg, lend */
3855a0015d6SChris Zankel
386b6569439SMax Filippov	rsr	a4, lbeg
387bc5378fcSMax Filippov	rsr	a3, lend
388b6569439SMax Filippov	s32i	a4, a1, PT_LBEG
3895a0015d6SChris Zankel	s32i	a3, a1, PT_LEND
3905a0015d6SChris Zankel
391733536b8SMax Filippov	/* Save SCOMPARE1 */
392733536b8SMax Filippov
393733536b8SMax Filippov#if XCHAL_HAVE_S32C1I
394b6569439SMax Filippov	rsr     a3, scompare1
395b6569439SMax Filippov	s32i    a3, a1, PT_SCOMPARE1
396733536b8SMax Filippov#endif
397733536b8SMax Filippov
398c658eac6SChris Zankel	/* Save optional registers. */
399c658eac6SChris Zankel
400b6569439SMax Filippov	save_xtregs_opt a1 a3 a4 a5 a6 a7 PT_XTREGS_OPT
401c658eac6SChris Zankel
4025a0015d6SChris Zankel	/* Go to second-level dispatcher. Set up parameters to pass to the
4035a0015d6SChris Zankel	 * exception handler and call the exception handler.
4045a0015d6SChris Zankel	 */
4055a0015d6SChris Zankel
40699d5040eSMax Filippov	rsr	a4, excsave1
4075a0015d6SChris Zankel	mov	a6, a1			# pass stack frame
408b6569439SMax Filippov	mov	a7, a2			# pass EXCCAUSE
409b6569439SMax Filippov	addx4	a4, a2, a4
4105a0015d6SChris Zankel	l32i	a4, a4, EXC_TABLE_DEFAULT		# load handler
4115a0015d6SChris Zankel
4125a0015d6SChris Zankel	/* Call the second-level handler */
4135a0015d6SChris Zankel
4145a0015d6SChris Zankel	callx4	a4
4155a0015d6SChris Zankel
4165a0015d6SChris Zankel	/* Jump here for exception exit */
417e6ffe17eSdann	.global common_exception_return
4185a0015d6SChris Zankelcommon_exception_return:
4195a0015d6SChris Zankel
420c92931b2SMax Filippov1:
421aea8e7c8SMax Filippov	rsil	a2, LOCKLEVEL
422*7d5f6a9aSMax Filippov#ifdef CONFIG_TRACE_IRQFLAGS
423*7d5f6a9aSMax Filippov	movi	a4, trace_hardirqs_off
424*7d5f6a9aSMax Filippov	callx4	a4
425*7d5f6a9aSMax Filippov#endif
426c92931b2SMax Filippov
4275a0015d6SChris Zankel	/* Jump if we are returning from kernel exceptions. */
4285a0015d6SChris Zankel
429aea8e7c8SMax Filippov	l32i	a3, a1, PT_PS
43016c5becfSMax Filippov	GET_THREAD_INFO(a2, a1)
43116c5becfSMax Filippov	l32i	a4, a2, TI_FLAGS
43216c5becfSMax Filippov	_bbci.l	a3, PS_UM_BIT, 6f
4335a0015d6SChris Zankel
4345a0015d6SChris Zankel	/* Specific to a user exception exit:
4355a0015d6SChris Zankel	 * We need to check some flags for signal handling and rescheduling,
4365a0015d6SChris Zankel	 * and have to restore WB and WS, extra states, and all registers
4375a0015d6SChris Zankel	 * in the register file that were in use in the user task.
438e1088430SChris Zankel	 * Note that we don't disable interrupts here.
4395a0015d6SChris Zankel	 */
4405a0015d6SChris Zankel
4415a0015d6SChris Zankel	_bbsi.l	a4, TIF_NEED_RESCHED, 3f
442a53bb24eSAl Viro	_bbsi.l	a4, TIF_NOTIFY_RESUME, 2f
443a99e07eeSMax Filippov	_bbci.l	a4, TIF_SIGPENDING, 5f
4445a0015d6SChris Zankel
445a53bb24eSAl Viro2:	l32i	a4, a1, PT_DEPC
4465a0015d6SChris Zankel	bgeui	a4, VALID_DOUBLE_EXCEPTION_ADDRESS, 4f
4475a0015d6SChris Zankel
448e1088430SChris Zankel	/* Call do_signal() */
449e1088430SChris Zankel
450*7d5f6a9aSMax Filippov#ifdef CONFIG_TRACE_IRQFLAGS
451*7d5f6a9aSMax Filippov	movi	a4, trace_hardirqs_on
452*7d5f6a9aSMax Filippov	callx4	a4
453*7d5f6a9aSMax Filippov#endif
454aea8e7c8SMax Filippov	rsil	a2, 0
455a53bb24eSAl Viro	movi	a4, do_notify_resume	# int do_notify_resume(struct pt_regs*)
4565a0015d6SChris Zankel	mov	a6, a1
4575a0015d6SChris Zankel	callx4	a4
4585a0015d6SChris Zankel	j	1b
4595a0015d6SChris Zankel
460e1088430SChris Zankel3:	/* Reschedule */
4615a0015d6SChris Zankel
462*7d5f6a9aSMax Filippov#ifdef CONFIG_TRACE_IRQFLAGS
463*7d5f6a9aSMax Filippov	movi	a4, trace_hardirqs_on
464*7d5f6a9aSMax Filippov	callx4	a4
465*7d5f6a9aSMax Filippov#endif
466aea8e7c8SMax Filippov	rsil	a2, 0
4675a0015d6SChris Zankel	movi	a4, schedule	# void schedule (void)
4685a0015d6SChris Zankel	callx4	a4
4695a0015d6SChris Zankel	j	1b
4705a0015d6SChris Zankel
47116c5becfSMax Filippov#ifdef CONFIG_PREEMPT
47216c5becfSMax Filippov6:
47316c5becfSMax Filippov	_bbci.l	a4, TIF_NEED_RESCHED, 4f
47416c5becfSMax Filippov
47516c5becfSMax Filippov	/* Check current_thread_info->preempt_count */
47616c5becfSMax Filippov
47716c5becfSMax Filippov	l32i	a4, a2, TI_PRE_COUNT
47816c5becfSMax Filippov	bnez	a4, 4f
47916c5becfSMax Filippov	movi	a4, preempt_schedule_irq
48016c5becfSMax Filippov	callx4	a4
48116c5becfSMax Filippov	j	1b
48216c5becfSMax Filippov#endif
48316c5becfSMax Filippov
484a99e07eeSMax Filippov5:
485a99e07eeSMax Filippov#ifdef CONFIG_DEBUG_TLB_SANITY
486a99e07eeSMax Filippov	l32i	a4, a1, PT_DEPC
487a99e07eeSMax Filippov	bgeui	a4, VALID_DOUBLE_EXCEPTION_ADDRESS, 4f
488a99e07eeSMax Filippov	movi	a4, check_tlb_sanity
489a99e07eeSMax Filippov	callx4	a4
490a99e07eeSMax Filippov#endif
49116c5becfSMax Filippov6:
492aea8e7c8SMax Filippov4:
493aea8e7c8SMax Filippov#ifdef CONFIG_TRACE_IRQFLAGS
494*7d5f6a9aSMax Filippov	extui	a4, a3, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
495*7d5f6a9aSMax Filippov	bgei	a4, LOCKLEVEL, 1f
496aea8e7c8SMax Filippov	movi	a4, trace_hardirqs_on
497aea8e7c8SMax Filippov	callx4	a4
498aea8e7c8SMax Filippov1:
499aea8e7c8SMax Filippov#endif
500aea8e7c8SMax Filippov	/* Restore optional registers. */
5015a0015d6SChris Zankel
502e1088430SChris Zankel	load_xtregs_opt a1 a2 a4 a5 a6 a7 PT_XTREGS_OPT
503e1088430SChris Zankel
504733536b8SMax Filippov	/* Restore SCOMPARE1 */
505733536b8SMax Filippov
506733536b8SMax Filippov#if XCHAL_HAVE_S32C1I
507733536b8SMax Filippov	l32i    a2, a1, PT_SCOMPARE1
508733536b8SMax Filippov	wsr     a2, scompare1
509733536b8SMax Filippov#endif
510bc5378fcSMax Filippov	wsr	a3, ps		/* disable interrupts */
511e1088430SChris Zankel
512e1088430SChris Zankel	_bbci.l	a3, PS_UM_BIT, kernel_exception_exit
513e1088430SChris Zankel
514e1088430SChris Zankeluser_exception_exit:
515e1088430SChris Zankel
516e1088430SChris Zankel	/* Restore the state of the task and return from the exception. */
5175a0015d6SChris Zankel
5185a0015d6SChris Zankel	/* Switch to the user thread WINDOWBASE. Save SP temporarily in DEPC */
5195a0015d6SChris Zankel
5205a0015d6SChris Zankel	l32i	a2, a1, PT_WINDOWBASE
5215a0015d6SChris Zankel	l32i	a3, a1, PT_WINDOWSTART
522bc5378fcSMax Filippov	wsr	a1, depc		# use DEPC as temp storage
523bc5378fcSMax Filippov	wsr	a3, windowstart		# restore WINDOWSTART
5245a0015d6SChris Zankel	ssr	a2			# preserve user's WB in the SAR
525bc5378fcSMax Filippov	wsr	a2, windowbase		# switch to user's saved WB
5265a0015d6SChris Zankel	rsync
527bc5378fcSMax Filippov	rsr	a1, depc		# restore stack pointer
5285a0015d6SChris Zankel	l32i	a2, a1, PT_WMASK	# register frames saved (in bits 4...9)
5295a0015d6SChris Zankel	rotw	-1			# we restore a4..a7
5305a0015d6SChris Zankel	_bltui	a6, 16, 1f		# only have to restore current window?
5315a0015d6SChris Zankel
5325a0015d6SChris Zankel	/* The working registers are a0 and a3.  We are restoring to
5335a0015d6SChris Zankel	 * a4..a7.  Be careful not to destroy what we have just restored.
5345a0015d6SChris Zankel	 * Note: wmask has the format YYYYM:
5355a0015d6SChris Zankel	 *       Y: number of registers saved in groups of 4
5365a0015d6SChris Zankel	 *       M: 4 bit mask of first 16 registers
5375a0015d6SChris Zankel	 */
5385a0015d6SChris Zankel
5395a0015d6SChris Zankel	mov	a2, a6
5405a0015d6SChris Zankel	mov	a3, a5
5415a0015d6SChris Zankel
5425a0015d6SChris Zankel2:	rotw	-1			# a0..a3 become a4..a7
5435a0015d6SChris Zankel	addi	a3, a7, -4*4		# next iteration
5445a0015d6SChris Zankel	addi	a2, a6, -16		# decrementing Y in WMASK
5455a0015d6SChris Zankel	l32i	a4, a3, PT_AREG_END + 0
5465a0015d6SChris Zankel	l32i	a5, a3, PT_AREG_END + 4
5475a0015d6SChris Zankel	l32i	a6, a3, PT_AREG_END + 8
5485a0015d6SChris Zankel	l32i	a7, a3, PT_AREG_END + 12
5495a0015d6SChris Zankel	_bgeui	a2, 16, 2b
5505a0015d6SChris Zankel
5515a0015d6SChris Zankel	/* Clear unrestored registers (don't leak anything to user-land */
5525a0015d6SChris Zankel
553bc5378fcSMax Filippov1:	rsr	a0, windowbase
554bc5378fcSMax Filippov	rsr	a3, sar
5555a0015d6SChris Zankel	sub	a3, a0, a3
5565a0015d6SChris Zankel	beqz	a3, 2f
5575a0015d6SChris Zankel	extui	a3, a3, 0, WBBITS
5585a0015d6SChris Zankel
5595a0015d6SChris Zankel1:	rotw	-1
5605a0015d6SChris Zankel	addi	a3, a7, -1
5615a0015d6SChris Zankel	movi	a4, 0
5625a0015d6SChris Zankel	movi	a5, 0
5635a0015d6SChris Zankel	movi	a6, 0
5645a0015d6SChris Zankel	movi	a7, 0
5655a0015d6SChris Zankel	bgei	a3, 1, 1b
5665a0015d6SChris Zankel
5675a0015d6SChris Zankel	/* We are back were we were when we started.
5685a0015d6SChris Zankel	 * Note: a2 still contains WMASK (if we've returned to the original
5695a0015d6SChris Zankel	 *	 frame where we had loaded a2), or at least the lower 4 bits
5705a0015d6SChris Zankel	 *	 (if we have restored WSBITS-1 frames).
5715a0015d6SChris Zankel	 */
5725a0015d6SChris Zankel
5734229fb12SMax Filippov2:
574c50842dfSChris Zankel#if XCHAL_HAVE_THREADPTR
575c50842dfSChris Zankel	l32i	a3, a1, PT_THREADPTR
576c50842dfSChris Zankel	wur	a3, threadptr
577c50842dfSChris Zankel#endif
578c50842dfSChris Zankel
5794229fb12SMax Filippov	j	common_exception_exit
5805a0015d6SChris Zankel
5815a0015d6SChris Zankel	/* This is the kernel exception exit.
5825a0015d6SChris Zankel	 * We avoided to do a MOVSP when we entered the exception, but we
5835a0015d6SChris Zankel	 * have to do it here.
5845a0015d6SChris Zankel	 */
5855a0015d6SChris Zankel
5865a0015d6SChris Zankelkernel_exception_exit:
5875a0015d6SChris Zankel
5885a0015d6SChris Zankel	/* Check if we have to do a movsp.
5895a0015d6SChris Zankel	 *
5905a0015d6SChris Zankel	 * We only have to do a movsp if the previous window-frame has
5915a0015d6SChris Zankel	 * been spilled to the *temporary* exception stack instead of the
5925a0015d6SChris Zankel	 * task's stack. This is the case if the corresponding bit in
5935a0015d6SChris Zankel	 * WINDOWSTART for the previous window-frame was set before
5945a0015d6SChris Zankel	 * (not spilled) but is zero now (spilled).
5955a0015d6SChris Zankel	 * If this bit is zero, all other bits except the one for the
5965a0015d6SChris Zankel	 * current window frame are also zero. So, we can use a simple test:
5975a0015d6SChris Zankel	 * 'and' WINDOWSTART and WINDOWSTART-1:
5985a0015d6SChris Zankel	 *
5995a0015d6SChris Zankel	 *  (XXXXXX1[0]* - 1) AND XXXXXX1[0]* = XXXXXX0[0]*
6005a0015d6SChris Zankel	 *
6015a0015d6SChris Zankel	 * The result is zero only if one bit was set.
6025a0015d6SChris Zankel	 *
6035a0015d6SChris Zankel	 * (Note: We might have gone through several task switches before
6045a0015d6SChris Zankel	 *        we come back to the current task, so WINDOWBASE might be
6055a0015d6SChris Zankel	 *        different from the time the exception occurred.)
6065a0015d6SChris Zankel	 */
6075a0015d6SChris Zankel
6085a0015d6SChris Zankel	/* Test WINDOWSTART before and after the exception.
6095a0015d6SChris Zankel	 * We actually have WMASK, so we only have to test if it is 1 or not.
6105a0015d6SChris Zankel	 */
6115a0015d6SChris Zankel
6125a0015d6SChris Zankel	l32i	a2, a1, PT_WMASK
6135a0015d6SChris Zankel	_beqi	a2, 1, common_exception_exit	# Spilled before exception,jump
6145a0015d6SChris Zankel
6155a0015d6SChris Zankel	/* Test WINDOWSTART now. If spilled, do the movsp */
6165a0015d6SChris Zankel
617bc5378fcSMax Filippov	rsr     a3, windowstart
6185a0015d6SChris Zankel	addi	a0, a3, -1
6195a0015d6SChris Zankel	and     a3, a3, a0
6205a0015d6SChris Zankel	_bnez	a3, common_exception_exit
6215a0015d6SChris Zankel
6225a0015d6SChris Zankel	/* Do a movsp (we returned from a call4, so we have at least a0..a7) */
6235a0015d6SChris Zankel
6245a0015d6SChris Zankel	addi    a0, a1, -16
6255a0015d6SChris Zankel	l32i    a3, a0, 0
6265a0015d6SChris Zankel	l32i    a4, a0, 4
6275a0015d6SChris Zankel	s32i    a3, a1, PT_SIZE+0
6285a0015d6SChris Zankel	s32i    a4, a1, PT_SIZE+4
6295a0015d6SChris Zankel	l32i    a3, a0, 8
6305a0015d6SChris Zankel	l32i    a4, a0, 12
6315a0015d6SChris Zankel	s32i    a3, a1, PT_SIZE+8
6325a0015d6SChris Zankel	s32i    a4, a1, PT_SIZE+12
6335a0015d6SChris Zankel
6345a0015d6SChris Zankel	/* Common exception exit.
6355a0015d6SChris Zankel	 * We restore the special register and the current window frame, and
6365a0015d6SChris Zankel	 * return from the exception.
6375a0015d6SChris Zankel	 *
6385a0015d6SChris Zankel	 * Note: We expect a2 to hold PT_WMASK
6395a0015d6SChris Zankel	 */
6405a0015d6SChris Zankel
6415a0015d6SChris Zankelcommon_exception_exit:
6425a0015d6SChris Zankel
643c658eac6SChris Zankel	/* Restore address registers. */
644c658eac6SChris Zankel
6455a0015d6SChris Zankel	_bbsi.l	a2, 1, 1f
6465a0015d6SChris Zankel	l32i	a4,  a1, PT_AREG4
6475a0015d6SChris Zankel	l32i	a5,  a1, PT_AREG5
6485a0015d6SChris Zankel	l32i	a6,  a1, PT_AREG6
6495a0015d6SChris Zankel	l32i	a7,  a1, PT_AREG7
6505a0015d6SChris Zankel	_bbsi.l	a2, 2, 1f
6515a0015d6SChris Zankel	l32i	a8,  a1, PT_AREG8
6525a0015d6SChris Zankel	l32i	a9,  a1, PT_AREG9
6535a0015d6SChris Zankel	l32i	a10, a1, PT_AREG10
6545a0015d6SChris Zankel	l32i	a11, a1, PT_AREG11
6555a0015d6SChris Zankel	_bbsi.l	a2, 3, 1f
6565a0015d6SChris Zankel	l32i	a12, a1, PT_AREG12
6575a0015d6SChris Zankel	l32i	a13, a1, PT_AREG13
6585a0015d6SChris Zankel	l32i	a14, a1, PT_AREG14
6595a0015d6SChris Zankel	l32i	a15, a1, PT_AREG15
6605a0015d6SChris Zankel
6615a0015d6SChris Zankel	/* Restore PC, SAR */
6625a0015d6SChris Zankel
6635a0015d6SChris Zankel1:	l32i	a2, a1, PT_PC
6645a0015d6SChris Zankel	l32i	a3, a1, PT_SAR
665bc5378fcSMax Filippov	wsr	a2, epc1
666bc5378fcSMax Filippov	wsr	a3, sar
6675a0015d6SChris Zankel
6685a0015d6SChris Zankel	/* Restore LBEG, LEND, LCOUNT */
6695a0015d6SChris Zankel
6705a0015d6SChris Zankel	l32i	a2, a1, PT_LBEG
6715a0015d6SChris Zankel	l32i	a3, a1, PT_LEND
672bc5378fcSMax Filippov	wsr	a2, lbeg
6735a0015d6SChris Zankel	l32i	a2, a1, PT_LCOUNT
674bc5378fcSMax Filippov	wsr	a3, lend
675bc5378fcSMax Filippov	wsr	a2, lcount
6765a0015d6SChris Zankel
67729c4dfd9SChris Zankel	/* We control single stepping through the ICOUNTLEVEL register. */
67829c4dfd9SChris Zankel
67929c4dfd9SChris Zankel	l32i	a2, a1, PT_ICOUNTLEVEL
68029c4dfd9SChris Zankel	movi	a3, -2
681bc5378fcSMax Filippov	wsr	a2, icountlevel
682bc5378fcSMax Filippov	wsr	a3, icount
68329c4dfd9SChris Zankel
6845a0015d6SChris Zankel	/* Check if it was double exception. */
6855a0015d6SChris Zankel
6865a0015d6SChris Zankel	l32i	a0, a1, PT_DEPC
6875a0015d6SChris Zankel	l32i	a3, a1, PT_AREG3
6882d1c645cSMarc Gauthier	l32i	a2, a1, PT_AREG2
689895666a9SMax Filippov	_bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
6902d1c645cSMarc Gauthier
6915a0015d6SChris Zankel	/* Restore a0...a3 and return */
6925a0015d6SChris Zankel
6935a0015d6SChris Zankel	l32i	a0, a1, PT_AREG0
6945a0015d6SChris Zankel	l32i	a1, a1, PT_AREG1
6955a0015d6SChris Zankel	rfe
6965a0015d6SChris Zankel
697895666a9SMax Filippov1: 	wsr	a0, depc
698895666a9SMax Filippov	l32i	a0, a1, PT_AREG0
699895666a9SMax Filippov	l32i	a1, a1, PT_AREG1
700895666a9SMax Filippov	rfde
7015a0015d6SChris Zankel
702d1538c46SChris ZankelENDPROC(kernel_exception)
703d1538c46SChris Zankel
7045a0015d6SChris Zankel/*
7055a0015d6SChris Zankel * Debug exception handler.
7065a0015d6SChris Zankel *
7075a0015d6SChris Zankel * Currently, we don't support KGDB, so only user application can be debugged.
7085a0015d6SChris Zankel *
7095a0015d6SChris Zankel * When we get here,  a0 is trashed and saved to excsave[debuglevel]
7105a0015d6SChris Zankel */
7115a0015d6SChris Zankel
7125a0015d6SChris ZankelENTRY(debug_exception)
7135a0015d6SChris Zankel
714bc5378fcSMax Filippov	rsr	a0, SREG_EPS + XCHAL_DEBUGLEVEL
715173d6681SChris Zankel	bbsi.l	a0, PS_EXCM_BIT, 1f	# exception mode
7165a0015d6SChris Zankel
717bc5378fcSMax Filippov	/* Set EPC1 and EXCCAUSE */
7185a0015d6SChris Zankel
719bc5378fcSMax Filippov	wsr	a2, depc		# save a2 temporarily
720bc5378fcSMax Filippov	rsr	a2, SREG_EPC + XCHAL_DEBUGLEVEL
721bc5378fcSMax Filippov	wsr	a2, epc1
7225a0015d6SChris Zankel
7235a0015d6SChris Zankel	movi	a2, EXCCAUSE_MAPPED_DEBUG
724bc5378fcSMax Filippov	wsr	a2, exccause
7255a0015d6SChris Zankel
7265a0015d6SChris Zankel	/* Restore PS to the value before the debug exc but with PS.EXCM set.*/
7275a0015d6SChris Zankel
728173d6681SChris Zankel	movi	a2, 1 << PS_EXCM_BIT
7295a0015d6SChris Zankel	or	a2, a0, a2
7305a0015d6SChris Zankel	movi	a0, debug_exception	# restore a3, debug jump vector
731bc5378fcSMax Filippov	wsr	a2, ps
732bc5378fcSMax Filippov	xsr	a0, SREG_EXCSAVE + XCHAL_DEBUGLEVEL
7335a0015d6SChris Zankel
7345a0015d6SChris Zankel	/* Switch to kernel/user stack, restore jump vector, and save a0 */
7355a0015d6SChris Zankel
736173d6681SChris Zankel	bbsi.l	a2, PS_UM_BIT, 2f	# jump if user mode
7375a0015d6SChris Zankel
7385a0015d6SChris Zankel	addi	a2, a1, -16-PT_SIZE	# assume kernel stack
7395a0015d6SChris Zankel	s32i	a0, a2, PT_AREG0
7405a0015d6SChris Zankel	movi	a0, 0
7415a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
7425a0015d6SChris Zankel	s32i	a0, a2, PT_DEPC		# mark it as a regular exception
743bc5378fcSMax Filippov	xsr	a0, depc
7445a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
7455a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2
7465a0015d6SChris Zankel	mov	a1, a2
7475a0015d6SChris Zankel	j	_kernel_exception
7485a0015d6SChris Zankel
749bc5378fcSMax Filippov2:	rsr	a2, excsave1
7505a0015d6SChris Zankel	l32i	a2, a2, EXC_TABLE_KSTK	# load kernel stack pointer
7515a0015d6SChris Zankel	s32i	a0, a2, PT_AREG0
7525a0015d6SChris Zankel	movi	a0, 0
7535a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
7545a0015d6SChris Zankel	s32i	a0, a2, PT_DEPC
755bc5378fcSMax Filippov	xsr	a0, depc
7565a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
7575a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2
7585a0015d6SChris Zankel	mov	a1, a2
7595a0015d6SChris Zankel	j	_user_exception
7605a0015d6SChris Zankel
7615a0015d6SChris Zankel	/* Debug exception while in exception mode. */
7625a0015d6SChris Zankel1:	j	1b	// FIXME!!
7635a0015d6SChris Zankel
764d1538c46SChris ZankelENDPROC(debug_exception)
7655a0015d6SChris Zankel
7665a0015d6SChris Zankel/*
7675a0015d6SChris Zankel * We get here in case of an unrecoverable exception.
7685a0015d6SChris Zankel * The only thing we can do is to be nice and print a panic message.
7695a0015d6SChris Zankel * We only produce a single stack frame for panic, so ???
7705a0015d6SChris Zankel *
7715a0015d6SChris Zankel *
7725a0015d6SChris Zankel * Entry conditions:
7735a0015d6SChris Zankel *
7745a0015d6SChris Zankel *   - a0 contains the caller address; original value saved in excsave1.
7755a0015d6SChris Zankel *   - the original a0 contains a valid return address (backtrace) or 0.
7765a0015d6SChris Zankel *   - a2 contains a valid stackpointer
7775a0015d6SChris Zankel *
7785a0015d6SChris Zankel * Notes:
7795a0015d6SChris Zankel *
7805a0015d6SChris Zankel *   - If the stack pointer could be invalid, the caller has to setup a
7815a0015d6SChris Zankel *     dummy stack pointer (e.g. the stack of the init_task)
7825a0015d6SChris Zankel *
7835a0015d6SChris Zankel *   - If the return address could be invalid, the caller has to set it
7845a0015d6SChris Zankel *     to 0, so the backtrace would stop.
7855a0015d6SChris Zankel *
7865a0015d6SChris Zankel */
7875a0015d6SChris Zankel	.align 4
7885a0015d6SChris Zankelunrecoverable_text:
7895a0015d6SChris Zankel	.ascii "Unrecoverable error in exception handler\0"
7905a0015d6SChris Zankel
7915a0015d6SChris ZankelENTRY(unrecoverable_exception)
7925a0015d6SChris Zankel
7935a0015d6SChris Zankel	movi	a0, 1
7945a0015d6SChris Zankel	movi	a1, 0
7955a0015d6SChris Zankel
796bc5378fcSMax Filippov	wsr	a0, windowstart
797bc5378fcSMax Filippov	wsr	a1, windowbase
7985a0015d6SChris Zankel	rsync
7995a0015d6SChris Zankel
8002d1c645cSMarc Gauthier	movi	a1, (1 << PS_WOE_BIT) | LOCKLEVEL
801bc5378fcSMax Filippov	wsr	a1, ps
8025a0015d6SChris Zankel	rsync
8035a0015d6SChris Zankel
8045a0015d6SChris Zankel	movi	a1, init_task
8055a0015d6SChris Zankel	movi	a0, 0
8065a0015d6SChris Zankel	addi	a1, a1, PT_REGS_OFFSET
8075a0015d6SChris Zankel
8085a0015d6SChris Zankel	movi	a4, panic
8095a0015d6SChris Zankel	movi	a6, unrecoverable_text
8105a0015d6SChris Zankel
8115a0015d6SChris Zankel	callx4	a4
8125a0015d6SChris Zankel
8135a0015d6SChris Zankel1:	j	1b
8145a0015d6SChris Zankel
815d1538c46SChris ZankelENDPROC(unrecoverable_exception)
8165a0015d6SChris Zankel
8175a0015d6SChris Zankel/* -------------------------- FAST EXCEPTION HANDLERS ----------------------- */
8185a0015d6SChris Zankel
8195a0015d6SChris Zankel/*
8205a0015d6SChris Zankel * Fast-handler for alloca exceptions
8215a0015d6SChris Zankel *
8225a0015d6SChris Zankel *  The ALLOCA handler is entered when user code executes the MOVSP
8235a0015d6SChris Zankel *  instruction and the caller's frame is not in the register file.
8245a0015d6SChris Zankel *
825fff96d69SMax Filippov * This algorithm was taken from the Ross Morley's RTOS Porting Layer:
826fff96d69SMax Filippov *
827fff96d69SMax Filippov *    /home/ross/rtos/porting/XtensaRTOS-PortingLayer-20090507/xtensa_vectors.S
828fff96d69SMax Filippov *
829fff96d69SMax Filippov * It leverages the existing window spill/fill routines and their support for
830fff96d69SMax Filippov * double exceptions. The 'movsp' instruction will only cause an exception if
831fff96d69SMax Filippov * the next window needs to be loaded. In fact this ALLOCA exception may be
832fff96d69SMax Filippov * replaced at some point by changing the hardware to do a underflow exception
833fff96d69SMax Filippov * of the proper size instead.
834fff96d69SMax Filippov *
835fff96d69SMax Filippov * This algorithm simply backs out the register changes started by the user
836fff96d69SMax Filippov * excpetion handler, makes it appear that we have started a window underflow
837fff96d69SMax Filippov * by rotating the window back and then setting the old window base (OWB) in
838fff96d69SMax Filippov * the 'ps' register with the rolled back window base. The 'movsp' instruction
839fff96d69SMax Filippov * will be re-executed and this time since the next window frames is in the
840fff96d69SMax Filippov * active AR registers it won't cause an exception.
841fff96d69SMax Filippov *
842fff96d69SMax Filippov * If the WindowUnderflow code gets a TLB miss the page will get mapped
843fff96d69SMax Filippov * the the partial windeowUnderflow will be handeled in the double exception
844fff96d69SMax Filippov * handler.
8455a0015d6SChris Zankel *
8465a0015d6SChris Zankel * Entry condition:
8475a0015d6SChris Zankel *
8485a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
8495a0015d6SChris Zankel *   a1:	a1
8505a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
85199d5040eSMax Filippov *   a3:	a3
8525a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
85399d5040eSMax Filippov *   excsave_1:	dispatch table
8545a0015d6SChris Zankel *
8555a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
8565a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
8575a0015d6SChris Zankel */
8585a0015d6SChris Zankel
8595a0015d6SChris ZankelENTRY(fast_alloca)
860fff96d69SMax Filippov	rsr	a0, windowbase
861fff96d69SMax Filippov	rotw	-1
862fff96d69SMax Filippov	rsr	a2, ps
863fff96d69SMax Filippov	extui	a3, a2, PS_OWB_SHIFT, PS_OWB_WIDTH
864fff96d69SMax Filippov	xor	a3, a3, a4
865fff96d69SMax Filippov	l32i	a4, a6, PT_AREG0
866fff96d69SMax Filippov	l32i	a1, a6, PT_DEPC
867fff96d69SMax Filippov	rsr	a6, depc
868fff96d69SMax Filippov	wsr	a1, depc
869fff96d69SMax Filippov	slli	a3, a3, PS_OWB_SHIFT
870fff96d69SMax Filippov	xor	a2, a2, a3
871fff96d69SMax Filippov	wsr	a2, ps
872fff96d69SMax Filippov	rsync
8735a0015d6SChris Zankel
874fff96d69SMax Filippov	_bbci.l	a4, 31, 4f
875fff96d69SMax Filippov	rotw	-1
876fff96d69SMax Filippov	_bbci.l	a8, 30, 8f
877fff96d69SMax Filippov	rotw	-1
878fff96d69SMax Filippov	j	_WindowUnderflow12
879fff96d69SMax Filippov8:	j	_WindowUnderflow8
880fff96d69SMax Filippov4:	j	_WindowUnderflow4
881d1538c46SChris ZankelENDPROC(fast_alloca)
8825a0015d6SChris Zankel
8835a0015d6SChris Zankel/*
8845a0015d6SChris Zankel * fast system calls.
8855a0015d6SChris Zankel *
8865a0015d6SChris Zankel * WARNING:  The kernel doesn't save the entire user context before
8875a0015d6SChris Zankel * handling a fast system call.  These functions are small and short,
8885a0015d6SChris Zankel * usually offering some functionality not available to user tasks.
8895a0015d6SChris Zankel *
8905a0015d6SChris Zankel * BE CAREFUL TO PRESERVE THE USER'S CONTEXT.
8915a0015d6SChris Zankel *
8925a0015d6SChris Zankel * Entry condition:
8935a0015d6SChris Zankel *
8945a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
8955a0015d6SChris Zankel *   a1:	a1
8965a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
89799d5040eSMax Filippov *   a3:	a3
8985a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
89999d5040eSMax Filippov *   excsave_1:	dispatch table
9005a0015d6SChris Zankel */
9015a0015d6SChris Zankel
9025a0015d6SChris ZankelENTRY(fast_syscall_kernel)
9035a0015d6SChris Zankel
9045a0015d6SChris Zankel	/* Skip syscall. */
9055a0015d6SChris Zankel
906bc5378fcSMax Filippov	rsr	a0, epc1
9075a0015d6SChris Zankel	addi	a0, a0, 3
908bc5378fcSMax Filippov	wsr	a0, epc1
9095a0015d6SChris Zankel
9105a0015d6SChris Zankel	l32i	a0, a2, PT_DEPC
9115a0015d6SChris Zankel	bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, fast_syscall_unrecoverable
9125a0015d6SChris Zankel
913bc5378fcSMax Filippov	rsr	a0, depc			# get syscall-nr
9145a0015d6SChris Zankel	_beqz	a0, fast_syscall_spill_registers
915fc4fb2adSChris Zankel	_beqi	a0, __NR_xtensa, fast_syscall_xtensa
9165a0015d6SChris Zankel
9175a0015d6SChris Zankel	j	kernel_exception
9185a0015d6SChris Zankel
919d1538c46SChris ZankelENDPROC(fast_syscall_kernel)
920d1538c46SChris Zankel
9215a0015d6SChris ZankelENTRY(fast_syscall_user)
9225a0015d6SChris Zankel
9235a0015d6SChris Zankel	/* Skip syscall. */
9245a0015d6SChris Zankel
925bc5378fcSMax Filippov	rsr	a0, epc1
9265a0015d6SChris Zankel	addi	a0, a0, 3
927bc5378fcSMax Filippov	wsr	a0, epc1
9285a0015d6SChris Zankel
9295a0015d6SChris Zankel	l32i	a0, a2, PT_DEPC
9305a0015d6SChris Zankel	bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, fast_syscall_unrecoverable
9315a0015d6SChris Zankel
932bc5378fcSMax Filippov	rsr	a0, depc			# get syscall-nr
9335a0015d6SChris Zankel	_beqz	a0, fast_syscall_spill_registers
934fc4fb2adSChris Zankel	_beqi	a0, __NR_xtensa, fast_syscall_xtensa
9355a0015d6SChris Zankel
9365a0015d6SChris Zankel	j	user_exception
9375a0015d6SChris Zankel
938d1538c46SChris ZankelENDPROC(fast_syscall_user)
939d1538c46SChris Zankel
9405a0015d6SChris ZankelENTRY(fast_syscall_unrecoverable)
9415a0015d6SChris Zankel
9425a0015d6SChris Zankel	/* Restore all states. */
9435a0015d6SChris Zankel
9445a0015d6SChris Zankel	l32i    a0, a2, PT_AREG0        # restore a0
945bc5378fcSMax Filippov	xsr     a2, depc                # restore a2, depc
9465a0015d6SChris Zankel
947bc5378fcSMax Filippov	wsr     a0, excsave1
9485a0015d6SChris Zankel	movi    a0, unrecoverable_exception
9495a0015d6SChris Zankel	callx0  a0
9505a0015d6SChris Zankel
951d1538c46SChris ZankelENDPROC(fast_syscall_unrecoverable)
9525a0015d6SChris Zankel
9535a0015d6SChris Zankel/*
9545a0015d6SChris Zankel * sysxtensa syscall handler
9555a0015d6SChris Zankel *
956fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_SET,     ptr, val,    unused);
957fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_ADD,     ptr, val,    unused);
958fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_EXG_ADD, ptr, val,    unused);
959fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_CMP_SWP, ptr, oldval, newval);
9605a0015d6SChris Zankel *        a2            a6                   a3    a4      a5
9615a0015d6SChris Zankel *
9625a0015d6SChris Zankel * Entry condition:
9635a0015d6SChris Zankel *
964fc4fb2adSChris Zankel *   a0:	a2 (syscall-nr), original value saved on stack (PT_AREG0)
9655a0015d6SChris Zankel *   a1:	a1
966fc4fb2adSChris Zankel *   a2:	new stack pointer, original in a0 and DEPC
96799d5040eSMax Filippov *   a3:	a3
968fc4fb2adSChris Zankel *   a4..a15:	unchanged
9695a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
97099d5040eSMax Filippov *   excsave_1:	dispatch table
9715a0015d6SChris Zankel *
9725a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
9735a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
9745a0015d6SChris Zankel *
9755a0015d6SChris Zankel * Note: we don't have to save a2; a2 holds the return value
9765a0015d6SChris Zankel *
9775a0015d6SChris Zankel * We use the two macros TRY and CATCH:
9785a0015d6SChris Zankel *
9795a0015d6SChris Zankel * TRY	 adds an entry to the __ex_table fixup table for the immediately
9805a0015d6SChris Zankel *	 following instruction.
9815a0015d6SChris Zankel *
98225985edcSLucas De Marchi * CATCH catches any exception that occurred at one of the preceding TRY
9835a0015d6SChris Zankel *       statements and continues from there
9845a0015d6SChris Zankel *
9855a0015d6SChris Zankel * Usage TRY	l32i	a0, a1, 0
9865a0015d6SChris Zankel *		<other code>
9875a0015d6SChris Zankel *	 done:	rfe
9885a0015d6SChris Zankel *	 CATCH	<set return code>
9895a0015d6SChris Zankel *		j done
9905a0015d6SChris Zankel */
9915a0015d6SChris Zankel
9929184289cSMax Filippov#ifdef CONFIG_FAST_SYSCALL_XTENSA
9939184289cSMax Filippov
9945a0015d6SChris Zankel#define TRY								\
9955a0015d6SChris Zankel	.section __ex_table, "a";					\
9965a0015d6SChris Zankel	.word	66f, 67f;						\
9975a0015d6SChris Zankel	.text;								\
9985a0015d6SChris Zankel66:
9995a0015d6SChris Zankel
10005a0015d6SChris Zankel#define CATCH								\
10015a0015d6SChris Zankel67:
10025a0015d6SChris Zankel
1003fc4fb2adSChris ZankelENTRY(fast_syscall_xtensa)
10045a0015d6SChris Zankel
1005fc4fb2adSChris Zankel	s32i	a7, a2, PT_AREG7	# we need an additional register
10065a0015d6SChris Zankel	movi	a7, 4			# sizeof(unsigned int)
1007fc4fb2adSChris Zankel	access_ok a3, a7, a0, a2, .Leac	# a0: scratch reg, a2: sp
10085a0015d6SChris Zankel
1009d1b6ba82SMax Filippov	_bgeui	a6, SYS_XTENSA_COUNT, .Lill
1010d1b6ba82SMax Filippov	_bnei	a6, SYS_XTENSA_ATOMIC_CMP_SWP, .Lnswp
10115a0015d6SChris Zankel
1012fc4fb2adSChris Zankel	/* Fall through for ATOMIC_CMP_SWP. */
10135a0015d6SChris Zankel
10145a0015d6SChris Zankel.Lswp:	/* Atomic compare and swap */
10155a0015d6SChris Zankel
1016fc4fb2adSChris ZankelTRY	l32i	a0, a3, 0		# read old value
1017fc4fb2adSChris Zankel	bne	a0, a4, 1f		# same as old value? jump
1018fc4fb2adSChris ZankelTRY	s32i	a5, a3, 0		# different, modify value
1019fc4fb2adSChris Zankel	l32i	a7, a2, PT_AREG7	# restore a7
1020fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1021fc4fb2adSChris Zankel	movi	a2, 1			# and return 1
1022fc4fb2adSChris Zankel	rfe
10235a0015d6SChris Zankel
1024fc4fb2adSChris Zankel1:	l32i	a7, a2, PT_AREG7	# restore a7
1025fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1026fc4fb2adSChris Zankel	movi	a2, 0			# return 0 (note that we cannot set
1027fc4fb2adSChris Zankel	rfe
10285a0015d6SChris Zankel
1029fc4fb2adSChris Zankel.Lnswp:	/* Atomic set, add, and exg_add. */
10305a0015d6SChris Zankel
1031fc4fb2adSChris ZankelTRY	l32i	a7, a3, 0		# orig
1032d1b6ba82SMax Filippov	addi	a6, a6, -SYS_XTENSA_ATOMIC_SET
1033fc4fb2adSChris Zankel	add	a0, a4, a7		# + arg
1034fc4fb2adSChris Zankel	moveqz	a0, a4, a6		# set
1035d1b6ba82SMax Filippov	addi	a6, a6, SYS_XTENSA_ATOMIC_SET
1036fc4fb2adSChris ZankelTRY	s32i	a0, a3, 0		# write new value
10375a0015d6SChris Zankel
1038fc4fb2adSChris Zankel	mov	a0, a2
10395a0015d6SChris Zankel	mov	a2, a7
1040fc4fb2adSChris Zankel	l32i	a7, a0, PT_AREG7	# restore a7
1041fc4fb2adSChris Zankel	l32i	a0, a0, PT_AREG0	# restore a0
10425a0015d6SChris Zankel	rfe
10435a0015d6SChris Zankel
10445a0015d6SChris ZankelCATCH
1045fc4fb2adSChris Zankel.Leac:	l32i	a7, a2, PT_AREG7	# restore a7
1046fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1047fc4fb2adSChris Zankel	movi	a2, -EFAULT
1048fc4fb2adSChris Zankel	rfe
1049fc4fb2adSChris Zankel
1050d1b6ba82SMax Filippov.Lill:	l32i	a7, a2, PT_AREG7	# restore a7
1051fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1052fc4fb2adSChris Zankel	movi	a2, -EINVAL
1053fc4fb2adSChris Zankel	rfe
1054fc4fb2adSChris Zankel
1055d1538c46SChris ZankelENDPROC(fast_syscall_xtensa)
10565a0015d6SChris Zankel
10579184289cSMax Filippov#else /* CONFIG_FAST_SYSCALL_XTENSA */
10589184289cSMax Filippov
10599184289cSMax FilippovENTRY(fast_syscall_xtensa)
10609184289cSMax Filippov
10619184289cSMax Filippov	l32i    a0, a2, PT_AREG0        # restore a0
10629184289cSMax Filippov	movi	a2, -ENOSYS
10639184289cSMax Filippov	rfe
10649184289cSMax Filippov
10659184289cSMax FilippovENDPROC(fast_syscall_xtensa)
10669184289cSMax Filippov
10679184289cSMax Filippov#endif /* CONFIG_FAST_SYSCALL_XTENSA */
10689184289cSMax Filippov
10695a0015d6SChris Zankel
10705a0015d6SChris Zankel/* fast_syscall_spill_registers.
10715a0015d6SChris Zankel *
10725a0015d6SChris Zankel * Entry condition:
10735a0015d6SChris Zankel *
10745a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
10755a0015d6SChris Zankel *   a1:	a1
10765a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
107799d5040eSMax Filippov *   a3:	a3
10785a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
107999d5040eSMax Filippov *   excsave_1:	dispatch table
10805a0015d6SChris Zankel *
10815a0015d6SChris Zankel * Note: We assume the stack pointer is EXC_TABLE_KSTK in the fixup handler.
10825a0015d6SChris Zankel */
10835a0015d6SChris Zankel
10849184289cSMax Filippov#ifdef CONFIG_FAST_SYSCALL_SPILL_REGISTERS
10859184289cSMax Filippov
10865a0015d6SChris ZankelENTRY(fast_syscall_spill_registers)
10875a0015d6SChris Zankel
10885a0015d6SChris Zankel	/* Register a FIXUP handler (pass current wb as a parameter) */
10895a0015d6SChris Zankel
109099d5040eSMax Filippov	xsr	a3, excsave1
10915a0015d6SChris Zankel	movi	a0, fast_syscall_spill_registers_fixup
10925a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_FIXUP
1093bc5378fcSMax Filippov	rsr	a0, windowbase
10945a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_PARAM
109599d5040eSMax Filippov	xsr	a3, excsave1		# restore a3 and excsave_1
10965a0015d6SChris Zankel
109799d5040eSMax Filippov	/* Save a3, a4 and SAR on stack. */
10985a0015d6SChris Zankel
1099bc5378fcSMax Filippov	rsr	a0, sar
11005a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
11016b5a1f74SChris Zankel	s32i	a0, a2, PT_SAR
11026b5a1f74SChris Zankel
11036b5a1f74SChris Zankel	/* The spill routine might clobber a4, a7, a8, a11, a12, and a15. */
11046b5a1f74SChris Zankel
1105c658eac6SChris Zankel	s32i	a4, a2, PT_AREG4
1106c658eac6SChris Zankel	s32i	a7, a2, PT_AREG7
11076b5a1f74SChris Zankel	s32i	a8, a2, PT_AREG8
1108c658eac6SChris Zankel	s32i	a11, a2, PT_AREG11
11096b5a1f74SChris Zankel	s32i	a12, a2, PT_AREG12
1110c658eac6SChris Zankel	s32i	a15, a2, PT_AREG15
11115a0015d6SChris Zankel
11126b5a1f74SChris Zankel	/*
11136b5a1f74SChris Zankel	 * Rotate ws so that the current windowbase is at bit 0.
11146b5a1f74SChris Zankel	 * Assume ws = xxxwww1yy (www1 current window frame).
11156b5a1f74SChris Zankel	 * Rotate ws right so that a4 = yyxxxwww1.
11166b5a1f74SChris Zankel	 */
11176b5a1f74SChris Zankel
11186b5a1f74SChris Zankel	rsr	a0, windowbase
11196b5a1f74SChris Zankel	rsr	a3, windowstart		# a3 = xxxwww1yy
11206b5a1f74SChris Zankel	ssr	a0			# holds WB
11216b5a1f74SChris Zankel	slli	a0, a3, WSBITS
11226b5a1f74SChris Zankel	or	a3, a3, a0		# a3 = xxxwww1yyxxxwww1yy
11236b5a1f74SChris Zankel	srl	a3, a3			# a3 = 00xxxwww1yyxxxwww1
11246b5a1f74SChris Zankel
11256b5a1f74SChris Zankel	/* We are done if there are no more than the current register frame. */
11266b5a1f74SChris Zankel
11276b5a1f74SChris Zankel	extui	a3, a3, 1, WSBITS-1	# a3 = 0yyxxxwww
11286b5a1f74SChris Zankel	movi	a0, (1 << (WSBITS-1))
11296b5a1f74SChris Zankel	_beqz	a3, .Lnospill		# only one active frame? jump
11306b5a1f74SChris Zankel
11316b5a1f74SChris Zankel	/* We want 1 at the top, so that we return to the current windowbase */
11326b5a1f74SChris Zankel
11336b5a1f74SChris Zankel	or	a3, a3, a0		# 1yyxxxwww
11346b5a1f74SChris Zankel
11356b5a1f74SChris Zankel	/* Skip empty frames - get 'oldest' WINDOWSTART-bit. */
11366b5a1f74SChris Zankel
11376b5a1f74SChris Zankel	wsr	a3, windowstart		# save shifted windowstart
11386b5a1f74SChris Zankel	neg	a0, a3
11396b5a1f74SChris Zankel	and	a3, a0, a3		# first bit set from right: 000010000
11406b5a1f74SChris Zankel
11416b5a1f74SChris Zankel	ffs_ws	a0, a3			# a0: shifts to skip empty frames
11426b5a1f74SChris Zankel	movi	a3, WSBITS
11436b5a1f74SChris Zankel	sub	a0, a3, a0		# WSBITS-a0:number of 0-bits from right
11446b5a1f74SChris Zankel	ssr	a0			# save in SAR for later.
11456b5a1f74SChris Zankel
11466b5a1f74SChris Zankel	rsr	a3, windowbase
11476b5a1f74SChris Zankel	add	a3, a3, a0
11486b5a1f74SChris Zankel	wsr	a3, windowbase
11496b5a1f74SChris Zankel	rsync
11506b5a1f74SChris Zankel
11516b5a1f74SChris Zankel	rsr	a3, windowstart
11526b5a1f74SChris Zankel	srl	a3, a3			# shift windowstart
11536b5a1f74SChris Zankel
11546b5a1f74SChris Zankel	/* WB is now just one frame below the oldest frame in the register
11556b5a1f74SChris Zankel	   window. WS is shifted so the oldest frame is in bit 0, thus, WB
11566b5a1f74SChris Zankel	   and WS differ by one 4-register frame. */
11576b5a1f74SChris Zankel
11586b5a1f74SChris Zankel	/* Save frames. Depending what call was used (call4, call8, call12),
11596b5a1f74SChris Zankel	 * we have to save 4,8. or 12 registers.
11606b5a1f74SChris Zankel	 */
11616b5a1f74SChris Zankel
11626b5a1f74SChris Zankel
11636b5a1f74SChris Zankel.Lloop: _bbsi.l	a3, 1, .Lc4
11646b5a1f74SChris Zankel	_bbci.l	a3, 2, .Lc12
11656b5a1f74SChris Zankel
11666b5a1f74SChris Zankel.Lc8:	s32e	a4, a13, -16
11676b5a1f74SChris Zankel	l32e	a4, a5, -12
11686b5a1f74SChris Zankel	s32e	a8, a4, -32
11696b5a1f74SChris Zankel	s32e	a5, a13, -12
11706b5a1f74SChris Zankel	s32e	a6, a13, -8
11716b5a1f74SChris Zankel	s32e	a7, a13, -4
11726b5a1f74SChris Zankel	s32e	a9, a4, -28
11736b5a1f74SChris Zankel	s32e	a10, a4, -24
11746b5a1f74SChris Zankel	s32e	a11, a4, -20
11756b5a1f74SChris Zankel	srli	a11, a3, 2		# shift windowbase by 2
11766b5a1f74SChris Zankel	rotw	2
11776b5a1f74SChris Zankel	_bnei	a3, 1, .Lloop
11786b5a1f74SChris Zankel	j	.Lexit
11796b5a1f74SChris Zankel
11806b5a1f74SChris Zankel.Lc4:	s32e	a4, a9, -16
11816b5a1f74SChris Zankel	s32e	a5, a9, -12
11826b5a1f74SChris Zankel	s32e	a6, a9, -8
11836b5a1f74SChris Zankel	s32e	a7, a9, -4
11846b5a1f74SChris Zankel
11856b5a1f74SChris Zankel	srli	a7, a3, 1
11866b5a1f74SChris Zankel	rotw	1
11876b5a1f74SChris Zankel	_bnei	a3, 1, .Lloop
11886b5a1f74SChris Zankel	j	.Lexit
11896b5a1f74SChris Zankel
11906b5a1f74SChris Zankel.Lc12:	_bbci.l	a3, 3, .Linvalid_mask	# bit 2 shouldn't be zero!
11916b5a1f74SChris Zankel
11926b5a1f74SChris Zankel	/* 12-register frame (call12) */
11936b5a1f74SChris Zankel
11946b5a1f74SChris Zankel	l32e	a0, a5, -12
11956b5a1f74SChris Zankel	s32e	a8, a0, -48
11966b5a1f74SChris Zankel	mov	a8, a0
11976b5a1f74SChris Zankel
11986b5a1f74SChris Zankel	s32e	a9, a8, -44
11996b5a1f74SChris Zankel	s32e	a10, a8, -40
12006b5a1f74SChris Zankel	s32e	a11, a8, -36
12016b5a1f74SChris Zankel	s32e	a12, a8, -32
12026b5a1f74SChris Zankel	s32e	a13, a8, -28
12036b5a1f74SChris Zankel	s32e	a14, a8, -24
12046b5a1f74SChris Zankel	s32e	a15, a8, -20
12056b5a1f74SChris Zankel	srli	a15, a3, 3
12066b5a1f74SChris Zankel
12076b5a1f74SChris Zankel	/* The stack pointer for a4..a7 is out of reach, so we rotate the
12086b5a1f74SChris Zankel	 * window, grab the stackpointer, and rotate back.
12096b5a1f74SChris Zankel	 * Alternatively, we could also use the following approach, but that
12106b5a1f74SChris Zankel	 * makes the fixup routine much more complicated:
12116b5a1f74SChris Zankel	 * rotw	1
12126b5a1f74SChris Zankel	 * s32e	a0, a13, -16
12136b5a1f74SChris Zankel	 * ...
12146b5a1f74SChris Zankel	 * rotw 2
12156b5a1f74SChris Zankel	 */
12166b5a1f74SChris Zankel
12176b5a1f74SChris Zankel	rotw	1
12186b5a1f74SChris Zankel	mov	a4, a13
12196b5a1f74SChris Zankel	rotw	-1
12206b5a1f74SChris Zankel
12216b5a1f74SChris Zankel	s32e	a4, a8, -16
12226b5a1f74SChris Zankel	s32e	a5, a8, -12
12236b5a1f74SChris Zankel	s32e	a6, a8, -8
12246b5a1f74SChris Zankel	s32e	a7, a8, -4
12256b5a1f74SChris Zankel
12266b5a1f74SChris Zankel	rotw	3
12276b5a1f74SChris Zankel
12286b5a1f74SChris Zankel	_beqi	a3, 1, .Lexit
12296b5a1f74SChris Zankel	j	.Lloop
12306b5a1f74SChris Zankel
12316b5a1f74SChris Zankel.Lexit:
12326b5a1f74SChris Zankel
12336b5a1f74SChris Zankel	/* Done. Do the final rotation and set WS */
12346b5a1f74SChris Zankel
12356b5a1f74SChris Zankel	rotw	1
12366b5a1f74SChris Zankel	rsr	a3, windowbase
12376b5a1f74SChris Zankel	ssl	a3
12386b5a1f74SChris Zankel	movi	a3, 1
12396b5a1f74SChris Zankel	sll	a3, a3
12406b5a1f74SChris Zankel	wsr	a3, windowstart
12416b5a1f74SChris Zankel.Lnospill:
12425a0015d6SChris Zankel
12435a0015d6SChris Zankel	/* Advance PC, restore registers and SAR, and return from exception. */
12445a0015d6SChris Zankel
12456b5a1f74SChris Zankel	l32i	a3, a2, PT_SAR
12465a0015d6SChris Zankel	l32i	a0, a2, PT_AREG0
1247bc5378fcSMax Filippov	wsr	a3, sar
12485a0015d6SChris Zankel	l32i	a3, a2, PT_AREG3
12495a0015d6SChris Zankel
12505a0015d6SChris Zankel	/* Restore clobbered registers. */
12515a0015d6SChris Zankel
12526b5a1f74SChris Zankel	l32i	a4, a2, PT_AREG4
1253c658eac6SChris Zankel	l32i	a7, a2, PT_AREG7
12546b5a1f74SChris Zankel	l32i	a8, a2, PT_AREG8
1255c658eac6SChris Zankel	l32i	a11, a2, PT_AREG11
12566b5a1f74SChris Zankel	l32i	a12, a2, PT_AREG12
1257c658eac6SChris Zankel	l32i	a15, a2, PT_AREG15
12585a0015d6SChris Zankel
12595a0015d6SChris Zankel	movi	a2, 0
12605a0015d6SChris Zankel	rfe
12615a0015d6SChris Zankel
12626b5a1f74SChris Zankel.Linvalid_mask:
12636b5a1f74SChris Zankel
12646b5a1f74SChris Zankel	/* We get here because of an unrecoverable error in the window
12656b5a1f74SChris Zankel	 * registers, so set up a dummy frame and kill the user application.
12666b5a1f74SChris Zankel	 * Note: We assume EXC_TABLE_KSTK contains a valid stack pointer.
12676b5a1f74SChris Zankel	 */
12686b5a1f74SChris Zankel
12696b5a1f74SChris Zankel	movi	a0, 1
12706b5a1f74SChris Zankel	movi	a1, 0
12716b5a1f74SChris Zankel
12726b5a1f74SChris Zankel	wsr	a0, windowstart
12736b5a1f74SChris Zankel	wsr	a1, windowbase
12746b5a1f74SChris Zankel	rsync
12756b5a1f74SChris Zankel
12766b5a1f74SChris Zankel	movi	a0, 0
12776b5a1f74SChris Zankel
12786b5a1f74SChris Zankel	rsr	a3, excsave1
12796b5a1f74SChris Zankel	l32i	a1, a3, EXC_TABLE_KSTK
12806b5a1f74SChris Zankel
12816b5a1f74SChris Zankel	movi	a4, (1 << PS_WOE_BIT) | LOCKLEVEL
12826b5a1f74SChris Zankel	wsr	a4, ps
12836b5a1f74SChris Zankel	rsync
12846b5a1f74SChris Zankel
12856b5a1f74SChris Zankel	movi	a6, SIGSEGV
12866b5a1f74SChris Zankel	movi	a4, do_exit
12876b5a1f74SChris Zankel	callx4	a4
12886b5a1f74SChris Zankel
12896b5a1f74SChris Zankel	/* shouldn't return, so panic */
12906b5a1f74SChris Zankel
12916b5a1f74SChris Zankel	wsr	a0, excsave1
12926b5a1f74SChris Zankel	movi	a0, unrecoverable_exception
12936b5a1f74SChris Zankel	callx0	a0		# should not return
12946b5a1f74SChris Zankel1:	j	1b
12956b5a1f74SChris Zankel
12966b5a1f74SChris Zankel
1297d1538c46SChris ZankelENDPROC(fast_syscall_spill_registers)
1298d1538c46SChris Zankel
12995a0015d6SChris Zankel/* Fixup handler.
13005a0015d6SChris Zankel *
13015a0015d6SChris Zankel * We get here if the spill routine causes an exception, e.g. tlb miss.
13025a0015d6SChris Zankel * We basically restore WINDOWBASE and WINDOWSTART to the condition when
13035a0015d6SChris Zankel * we entered the spill routine and jump to the user exception handler.
13045a0015d6SChris Zankel *
13053251f1e2SMax Filippov * Note that we only need to restore the bits in windowstart that have not
13063251f1e2SMax Filippov * been spilled yet by the _spill_register routine. Luckily, a3 contains a
13073251f1e2SMax Filippov * rotated windowstart with only those bits set for frames that haven't been
13083251f1e2SMax Filippov * spilled yet. Because a3 is rotated such that bit 0 represents the register
13093251f1e2SMax Filippov * frame for the current windowbase - 1, we need to rotate a3 left by the
13103251f1e2SMax Filippov * value of the current windowbase + 1 and move it to windowstart.
13113251f1e2SMax Filippov *
13125a0015d6SChris Zankel * a0: value of depc, original value in depc
13135a0015d6SChris Zankel * a2: trashed, original value in EXC_TABLE_DOUBLE_SAVE
13145a0015d6SChris Zankel * a3: exctable, original value in excsave1
13155a0015d6SChris Zankel */
13165a0015d6SChris Zankel
1317244066f4SMax FilippovENTRY(fast_syscall_spill_registers_fixup)
13185a0015d6SChris Zankel
1319bc5378fcSMax Filippov	rsr	a2, windowbase	# get current windowbase (a2 is saved)
1320bc5378fcSMax Filippov	xsr	a0, depc	# restore depc and a0
13215a0015d6SChris Zankel	ssl	a2		# set shift (32 - WB)
13225a0015d6SChris Zankel
13235a0015d6SChris Zankel	/* We need to make sure the current registers (a0-a3) are preserved.
13245a0015d6SChris Zankel	 * To do this, we simply set the bit for the current window frame
13255a0015d6SChris Zankel	 * in WS, so that the exception handlers save them to the task stack.
13263251f1e2SMax Filippov	 *
13273251f1e2SMax Filippov	 * Note: we use a3 to set the windowbase, so we take a special care
13283251f1e2SMax Filippov	 * of it, saving it in the original _spill_registers frame across
13293251f1e2SMax Filippov	 * the exception handler call.
13305a0015d6SChris Zankel	 */
13315a0015d6SChris Zankel
133299d5040eSMax Filippov	xsr	a3, excsave1	# get spill-mask
1333244066f4SMax Filippov	slli	a3, a3, 1	# shift left by one
13343251f1e2SMax Filippov	addi	a3, a3, 1	# set the bit for the current window frame
13355a0015d6SChris Zankel
1336244066f4SMax Filippov	slli	a2, a3, 32-WSBITS
1337244066f4SMax Filippov	src	a2, a3, a2	# a2 = xxwww1yyxxxwww1yy......
1338bc5378fcSMax Filippov	wsr	a2, windowstart	# set corrected windowstart
13395a0015d6SChris Zankel
1340244066f4SMax Filippov	srli	a3, a3, 1
1341244066f4SMax Filippov	rsr	a2, excsave1
1342244066f4SMax Filippov	l32i	a2, a2, EXC_TABLE_DOUBLE_SAVE	# restore a2
1343244066f4SMax Filippov	xsr	a2, excsave1
1344244066f4SMax Filippov	s32i	a3, a2, EXC_TABLE_DOUBLE_SAVE	# save a3
1345244066f4SMax Filippov	l32i	a3, a2, EXC_TABLE_PARAM	# original WB (in user task)
1346244066f4SMax Filippov	xsr	a2, excsave1
13475a0015d6SChris Zankel
13485a0015d6SChris Zankel	/* Return to the original (user task) WINDOWBASE.
13495a0015d6SChris Zankel	 * We leave the following frame behind:
13505a0015d6SChris Zankel	 * a0, a1, a2	same
1351244066f4SMax Filippov	 * a3:		trashed (saved in EXC_TABLE_DOUBLE_SAVE)
13525a0015d6SChris Zankel	 * depc:	depc (we have to return to that address)
1353244066f4SMax Filippov	 * excsave_1:	exctable
13545a0015d6SChris Zankel	 */
13555a0015d6SChris Zankel
1356bc5378fcSMax Filippov	wsr	a3, windowbase
13575a0015d6SChris Zankel	rsync
13585a0015d6SChris Zankel
13595a0015d6SChris Zankel	/* We are now in the original frame when we entered _spill_registers:
13605a0015d6SChris Zankel	 *  a0: return address
13615a0015d6SChris Zankel	 *  a1: used, stack pointer
13625a0015d6SChris Zankel	 *  a2: kernel stack pointer
1363244066f4SMax Filippov	 *  a3: available
13645a0015d6SChris Zankel	 *  depc: exception address
1365244066f4SMax Filippov	 *  excsave: exctable
13665a0015d6SChris Zankel	 * Note: This frame might be the same as above.
13675a0015d6SChris Zankel	 */
13685a0015d6SChris Zankel
13695a0015d6SChris Zankel	/* Setup stack pointer. */
13705a0015d6SChris Zankel
13715a0015d6SChris Zankel	addi	a2, a2, -PT_USER_SIZE
13725a0015d6SChris Zankel	s32i	a0, a2, PT_AREG0
13735a0015d6SChris Zankel
13745a0015d6SChris Zankel	/* Make sure we return to this fixup handler. */
13755a0015d6SChris Zankel
13765a0015d6SChris Zankel	movi	a3, fast_syscall_spill_registers_fixup_return
13775a0015d6SChris Zankel	s32i	a3, a2, PT_DEPC		# setup depc
13785a0015d6SChris Zankel
13795a0015d6SChris Zankel	/* Jump to the exception handler. */
13805a0015d6SChris Zankel
138199d5040eSMax Filippov	rsr	a3, excsave1
1382bc5378fcSMax Filippov	rsr	a0, exccause
13835a0015d6SChris Zankel	addx4	a0, a0, a3              	# find entry in table
13845a0015d6SChris Zankel	l32i	a0, a0, EXC_TABLE_FAST_USER     # load handler
1385244066f4SMax Filippov	l32i	a3, a3, EXC_TABLE_DOUBLE_SAVE
13865a0015d6SChris Zankel	jx	a0
13875a0015d6SChris Zankel
1388244066f4SMax FilippovENDPROC(fast_syscall_spill_registers_fixup)
1389244066f4SMax Filippov
1390244066f4SMax FilippovENTRY(fast_syscall_spill_registers_fixup_return)
13915a0015d6SChris Zankel
13925a0015d6SChris Zankel	/* When we return here, all registers have been restored (a2: DEPC) */
13935a0015d6SChris Zankel
1394bc5378fcSMax Filippov	wsr	a2, depc		# exception address
13955a0015d6SChris Zankel
13965a0015d6SChris Zankel	/* Restore fixup handler. */
13975a0015d6SChris Zankel
1398244066f4SMax Filippov	rsr	a2, excsave1
1399244066f4SMax Filippov	s32i	a3, a2, EXC_TABLE_DOUBLE_SAVE
1400244066f4SMax Filippov	movi	a3, fast_syscall_spill_registers_fixup
1401244066f4SMax Filippov	s32i	a3, a2, EXC_TABLE_FIXUP
1402244066f4SMax Filippov	rsr	a3, windowbase
1403244066f4SMax Filippov	s32i	a3, a2, EXC_TABLE_PARAM
1404244066f4SMax Filippov	l32i	a2, a2, EXC_TABLE_KSTK
14055a0015d6SChris Zankel
14065a0015d6SChris Zankel	/* Load WB at the time the exception occurred. */
14075a0015d6SChris Zankel
1408bc5378fcSMax Filippov	rsr	a3, sar			# WB is still in SAR
14095a0015d6SChris Zankel	neg	a3, a3
1410bc5378fcSMax Filippov	wsr	a3, windowbase
14115a0015d6SChris Zankel	rsync
14125a0015d6SChris Zankel
1413244066f4SMax Filippov	rsr	a3, excsave1
1414244066f4SMax Filippov	l32i	a3, a3, EXC_TABLE_DOUBLE_SAVE
1415244066f4SMax Filippov
14165a0015d6SChris Zankel	rfde
14175a0015d6SChris Zankel
1418244066f4SMax FilippovENDPROC(fast_syscall_spill_registers_fixup_return)
14195a0015d6SChris Zankel
14209184289cSMax Filippov#else /* CONFIG_FAST_SYSCALL_SPILL_REGISTERS */
14219184289cSMax Filippov
14229184289cSMax FilippovENTRY(fast_syscall_spill_registers)
14239184289cSMax Filippov
14249184289cSMax Filippov	l32i    a0, a2, PT_AREG0        # restore a0
14259184289cSMax Filippov	movi	a2, -ENOSYS
14269184289cSMax Filippov	rfe
14279184289cSMax Filippov
14289184289cSMax FilippovENDPROC(fast_syscall_spill_registers)
14299184289cSMax Filippov
14309184289cSMax Filippov#endif /* CONFIG_FAST_SYSCALL_SPILL_REGISTERS */
14319184289cSMax Filippov
1432e5083a63SJohannes Weiner#ifdef CONFIG_MMU
14335a0015d6SChris Zankel/*
14345a0015d6SChris Zankel * We should never get here. Bail out!
14355a0015d6SChris Zankel */
14365a0015d6SChris Zankel
14375a0015d6SChris ZankelENTRY(fast_second_level_miss_double_kernel)
14385a0015d6SChris Zankel
14395a0015d6SChris Zankel1:	movi	a0, unrecoverable_exception
14405a0015d6SChris Zankel	callx0	a0		# should not return
14415a0015d6SChris Zankel1:	j	1b
14425a0015d6SChris Zankel
1443d1538c46SChris ZankelENDPROC(fast_second_level_miss_double_kernel)
1444d1538c46SChris Zankel
14455a0015d6SChris Zankel/* First-level entry handler for user, kernel, and double 2nd-level
14465a0015d6SChris Zankel * TLB miss exceptions.  Note that for now, user and kernel miss
14475a0015d6SChris Zankel * exceptions share the same entry point and are handled identically.
14485a0015d6SChris Zankel *
14495a0015d6SChris Zankel * An old, less-efficient C version of this function used to exist.
14505a0015d6SChris Zankel * We include it below, interleaved as comments, for reference.
14515a0015d6SChris Zankel *
14525a0015d6SChris Zankel * Entry condition:
14535a0015d6SChris Zankel *
14545a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
14555a0015d6SChris Zankel *   a1:	a1
14565a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
145799d5040eSMax Filippov *   a3:	a3
14585a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
145999d5040eSMax Filippov *   excsave_1:	dispatch table
14605a0015d6SChris Zankel *
14615a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
14625a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
14635a0015d6SChris Zankel */
14645a0015d6SChris Zankel
14655a0015d6SChris ZankelENTRY(fast_second_level_miss)
14665a0015d6SChris Zankel
146799d5040eSMax Filippov	/* Save a1 and a3. Note: we don't expect a double exception. */
14685a0015d6SChris Zankel
14695a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
147099d5040eSMax Filippov	s32i	a3, a2, PT_AREG3
14715a0015d6SChris Zankel
14725a0015d6SChris Zankel	/* We need to map the page of PTEs for the user task.  Find
14735a0015d6SChris Zankel	 * the pointer to that page.  Also, it's possible for tsk->mm
14745a0015d6SChris Zankel	 * to be NULL while tsk->active_mm is nonzero if we faulted on
14755a0015d6SChris Zankel	 * a vmalloc address.  In that rare case, we must use
14765a0015d6SChris Zankel	 * active_mm instead to avoid a fault in this handler.  See
14775a0015d6SChris Zankel	 *
14785a0015d6SChris Zankel	 * http://mail.nl.linux.org/linux-mm/2002-08/msg00258.html
14795a0015d6SChris Zankel	 *   (or search Internet on "mm vs. active_mm")
14805a0015d6SChris Zankel	 *
14815a0015d6SChris Zankel	 *	if (!mm)
14825a0015d6SChris Zankel	 *		mm = tsk->active_mm;
14835a0015d6SChris Zankel	 *	pgd = pgd_offset (mm, regs->excvaddr);
14845a0015d6SChris Zankel	 *	pmd = pmd_offset (pgd, regs->excvaddr);
14855a0015d6SChris Zankel	 *	pmdval = *pmd;
14865a0015d6SChris Zankel	 */
14875a0015d6SChris Zankel
14885a0015d6SChris Zankel	GET_CURRENT(a1,a2)
14895a0015d6SChris Zankel	l32i	a0, a1, TASK_MM		# tsk->mm
14905a0015d6SChris Zankel	beqz	a0, 9f
14915a0015d6SChris Zankel
1492bc5378fcSMax Filippov8:	rsr	a3, excvaddr		# fault address
149301858d1bSChris Zankel	_PGD_OFFSET(a0, a3, a1)
14945a0015d6SChris Zankel	l32i	a0, a0, 0		# read pmdval
14955a0015d6SChris Zankel	beqz	a0, 2f
14965a0015d6SChris Zankel
14975a0015d6SChris Zankel	/* Read ptevaddr and convert to top of page-table page.
14985a0015d6SChris Zankel	 *
14995a0015d6SChris Zankel	 * 	vpnval = read_ptevaddr_register() & PAGE_MASK;
15005a0015d6SChris Zankel	 * 	vpnval += DTLB_WAY_PGTABLE;
15015a0015d6SChris Zankel	 *	pteval = mk_pte (virt_to_page(pmd_val(pmdval)), PAGE_KERNEL);
15025a0015d6SChris Zankel	 *	write_dtlb_entry (pteval, vpnval);
15035a0015d6SChris Zankel	 *
15045a0015d6SChris Zankel	 * The messy computation for 'pteval' above really simplifies
15055a0015d6SChris Zankel	 * into the following:
15065a0015d6SChris Zankel	 *
15076656920bSChris Zankel	 * pteval = ((pmdval - PAGE_OFFSET) & PAGE_MASK) | PAGE_DIRECTORY
15085a0015d6SChris Zankel	 */
15095a0015d6SChris Zankel
151039070cb8SChris Zankel	movi	a1, (-PAGE_OFFSET) & 0xffffffff
15115a0015d6SChris Zankel	add	a0, a0, a1		# pmdval - PAGE_OFFSET
15125a0015d6SChris Zankel	extui	a1, a0, 0, PAGE_SHIFT	# ... & PAGE_MASK
15135a0015d6SChris Zankel	xor	a0, a0, a1
15145a0015d6SChris Zankel
151501858d1bSChris Zankel	movi	a1, _PAGE_DIRECTORY
15165a0015d6SChris Zankel	or	a0, a0, a1		# ... | PAGE_DIRECTORY
15175a0015d6SChris Zankel
151801858d1bSChris Zankel	/*
15196656920bSChris Zankel	 * We utilize all three wired-ways (7-9) to hold pmd translations.
152001858d1bSChris Zankel	 * Memory regions are mapped to the DTLBs according to bits 28 and 29.
152101858d1bSChris Zankel	 * This allows to map the three most common regions to three different
152201858d1bSChris Zankel	 * DTLBs:
152301858d1bSChris Zankel	 *  0,1 -> way 7	program (0040.0000) and virtual (c000.0000)
152401858d1bSChris Zankel	 *  2   -> way 8	shared libaries (2000.0000)
152501858d1bSChris Zankel	 *  3   -> way 0	stack (3000.0000)
152601858d1bSChris Zankel	 */
15275a0015d6SChris Zankel
152801858d1bSChris Zankel	extui	a3, a3, 28, 2		# addr. bit 28 and 29	0,1,2,3
1529bc5378fcSMax Filippov	rsr	a1, ptevaddr
153001858d1bSChris Zankel	addx2	a3, a3, a3		# ->			0,3,6,9
153101858d1bSChris Zankel	srli	a1, a1, PAGE_SHIFT
153201858d1bSChris Zankel	extui	a3, a3, 2, 2		# ->			0,0,1,2
153301858d1bSChris Zankel	slli	a1, a1, PAGE_SHIFT	# ptevaddr & PAGE_MASK
153401858d1bSChris Zankel	addi	a3, a3, DTLB_WAY_PGD
153501858d1bSChris Zankel	add	a1, a1, a3		# ... + way_number
153601858d1bSChris Zankel
153701858d1bSChris Zankel3:	wdtlb	a0, a1
15385a0015d6SChris Zankel	dsync
15395a0015d6SChris Zankel
15405a0015d6SChris Zankel	/* Exit critical section. */
15415a0015d6SChris Zankel
154299d5040eSMax Filippov4:	rsr	a3, excsave1
15435a0015d6SChris Zankel	movi	a0, 0
15445a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_FIXUP
15455a0015d6SChris Zankel
15465a0015d6SChris Zankel	/* Restore the working registers, and return. */
15475a0015d6SChris Zankel
15485a0015d6SChris Zankel	l32i	a0, a2, PT_AREG0
15495a0015d6SChris Zankel	l32i	a1, a2, PT_AREG1
155099d5040eSMax Filippov	l32i	a3, a2, PT_AREG3
15515a0015d6SChris Zankel	l32i	a2, a2, PT_DEPC
15525a0015d6SChris Zankel
15535a0015d6SChris Zankel	bgeui	a2, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
15545a0015d6SChris Zankel
15555a0015d6SChris Zankel	/* Restore excsave1 and return. */
15565a0015d6SChris Zankel
1557bc5378fcSMax Filippov	rsr	a2, depc
15585a0015d6SChris Zankel	rfe
15595a0015d6SChris Zankel
15605a0015d6SChris Zankel	/* Return from double exception. */
15615a0015d6SChris Zankel
1562bc5378fcSMax Filippov1:	xsr	a2, depc
15635a0015d6SChris Zankel	esync
15645a0015d6SChris Zankel	rfde
15655a0015d6SChris Zankel
15665a0015d6SChris Zankel9:	l32i	a0, a1, TASK_ACTIVE_MM	# unlikely case mm == 0
15675a0015d6SChris Zankel	j	8b
15685a0015d6SChris Zankel
15696656920bSChris Zankel#if (DCACHE_WAY_SIZE > PAGE_SIZE)
15706656920bSChris Zankel
15716656920bSChris Zankel2:	/* Special case for cache aliasing.
15726656920bSChris Zankel	 * We (should) only get here if a clear_user_page, copy_user_page
15736656920bSChris Zankel	 * or the aliased cache flush functions got preemptively interrupted
15746656920bSChris Zankel	 * by another task. Re-establish temporary mapping to the
15756656920bSChris Zankel	 * TLBTEMP_BASE areas.
15766656920bSChris Zankel	 */
15776656920bSChris Zankel
15786656920bSChris Zankel	/* We shouldn't be in a double exception */
15796656920bSChris Zankel
15806656920bSChris Zankel	l32i	a0, a2, PT_DEPC
15816656920bSChris Zankel	bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, 2f
15826656920bSChris Zankel
15836656920bSChris Zankel	/* Make sure the exception originated in the special functions */
15846656920bSChris Zankel
15856656920bSChris Zankel	movi	a0, __tlbtemp_mapping_start
1586bc5378fcSMax Filippov	rsr	a3, epc1
15876656920bSChris Zankel	bltu	a3, a0, 2f
15886656920bSChris Zankel	movi	a0, __tlbtemp_mapping_end
15896656920bSChris Zankel	bgeu	a3, a0, 2f
15906656920bSChris Zankel
15916656920bSChris Zankel	/* Check if excvaddr was in one of the TLBTEMP_BASE areas. */
15926656920bSChris Zankel
15936656920bSChris Zankel	movi	a3, TLBTEMP_BASE_1
1594bc5378fcSMax Filippov	rsr	a0, excvaddr
15956656920bSChris Zankel	bltu	a0, a3, 2f
15966656920bSChris Zankel
15977128039fSMax Filippov	addi	a1, a0, -TLBTEMP_SIZE
15986656920bSChris Zankel	bgeu	a1, a3, 2f
15996656920bSChris Zankel
16006656920bSChris Zankel	/* Check if we have to restore an ITLB mapping. */
16016656920bSChris Zankel
16026656920bSChris Zankel	movi	a1, __tlbtemp_mapping_itlb
1603bc5378fcSMax Filippov	rsr	a3, epc1
16046656920bSChris Zankel	sub	a3, a3, a1
16056656920bSChris Zankel
16066656920bSChris Zankel	/* Calculate VPN */
16076656920bSChris Zankel
16086656920bSChris Zankel	movi	a1, PAGE_MASK
16096656920bSChris Zankel	and	a1, a1, a0
16106656920bSChris Zankel
16116656920bSChris Zankel	/* Jump for ITLB entry */
16126656920bSChris Zankel
16136656920bSChris Zankel	bgez	a3, 1f
16146656920bSChris Zankel
16156656920bSChris Zankel	/* We can use up to two TLBTEMP areas, one for src and one for dst. */
16166656920bSChris Zankel
16176656920bSChris Zankel	extui	a3, a0, PAGE_SHIFT + DCACHE_ALIAS_ORDER, 1
16186656920bSChris Zankel	add	a1, a3, a1
16196656920bSChris Zankel
16206656920bSChris Zankel	/* PPN is in a6 for the first TLBTEMP area and in a7 for the second. */
16216656920bSChris Zankel
16226656920bSChris Zankel	mov	a0, a6
16236656920bSChris Zankel	movnez	a0, a7, a3
16246656920bSChris Zankel	j	3b
16256656920bSChris Zankel
16266656920bSChris Zankel	/* ITLB entry. We only use dst in a6. */
16276656920bSChris Zankel
16286656920bSChris Zankel1:	witlb	a6, a1
16296656920bSChris Zankel	isync
16306656920bSChris Zankel	j	4b
16316656920bSChris Zankel
16326656920bSChris Zankel
16336656920bSChris Zankel#endif	// DCACHE_WAY_SIZE > PAGE_SIZE
16346656920bSChris Zankel
16356656920bSChris Zankel
16365a0015d6SChris Zankel2:	/* Invalid PGD, default exception handling */
16375a0015d6SChris Zankel
1638bc5378fcSMax Filippov	rsr	a1, depc
16395a0015d6SChris Zankel	s32i	a1, a2, PT_AREG2
16405a0015d6SChris Zankel	mov	a1, a2
16415a0015d6SChris Zankel
1642bc5378fcSMax Filippov	rsr	a2, ps
1643173d6681SChris Zankel	bbsi.l	a2, PS_UM_BIT, 1f
16445a0015d6SChris Zankel	j	_kernel_exception
16455a0015d6SChris Zankel1:	j	_user_exception
16465a0015d6SChris Zankel
1647d1538c46SChris ZankelENDPROC(fast_second_level_miss)
16485a0015d6SChris Zankel
16495a0015d6SChris Zankel/*
16505a0015d6SChris Zankel * StoreProhibitedException
16515a0015d6SChris Zankel *
16525a0015d6SChris Zankel * Update the pte and invalidate the itlb mapping for this pte.
16535a0015d6SChris Zankel *
16545a0015d6SChris Zankel * Entry condition:
16555a0015d6SChris Zankel *
16565a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
16575a0015d6SChris Zankel *   a1:	a1
16585a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
165999d5040eSMax Filippov *   a3:	a3
16605a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
166199d5040eSMax Filippov *   excsave_1:	dispatch table
16625a0015d6SChris Zankel *
16635a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
16645a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
16655a0015d6SChris Zankel */
16665a0015d6SChris Zankel
16675a0015d6SChris ZankelENTRY(fast_store_prohibited)
16685a0015d6SChris Zankel
166999d5040eSMax Filippov	/* Save a1 and a3. */
16705a0015d6SChris Zankel
16715a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
167299d5040eSMax Filippov	s32i	a3, a2, PT_AREG3
16735a0015d6SChris Zankel
16745a0015d6SChris Zankel	GET_CURRENT(a1,a2)
16755a0015d6SChris Zankel	l32i	a0, a1, TASK_MM		# tsk->mm
16765a0015d6SChris Zankel	beqz	a0, 9f
16775a0015d6SChris Zankel
1678bc5378fcSMax Filippov8:	rsr	a1, excvaddr		# fault address
167999d5040eSMax Filippov	_PGD_OFFSET(a0, a1, a3)
16805a0015d6SChris Zankel	l32i	a0, a0, 0
16815a0015d6SChris Zankel	beqz	a0, 2f
16825a0015d6SChris Zankel
168351fc41a9SMax Filippov	/*
168451fc41a9SMax Filippov	 * Note that we test _PAGE_WRITABLE_BIT only if PTE is present
168551fc41a9SMax Filippov	 * and is not PAGE_NONE. See pgtable.h for possible PTE layouts.
168651fc41a9SMax Filippov	 */
168701858d1bSChris Zankel
168899d5040eSMax Filippov	_PTE_OFFSET(a0, a1, a3)
168999d5040eSMax Filippov	l32i	a3, a0, 0		# read pteval
169051fc41a9SMax Filippov	movi	a1, _PAGE_CA_INVALID
169199d5040eSMax Filippov	ball	a3, a1, 2f
169299d5040eSMax Filippov	bbci.l	a3, _PAGE_WRITABLE_BIT, 2f
16935a0015d6SChris Zankel
169401858d1bSChris Zankel	movi	a1, _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_HW_WRITE
169599d5040eSMax Filippov	or	a3, a3, a1
1696bc5378fcSMax Filippov	rsr	a1, excvaddr
169799d5040eSMax Filippov	s32i	a3, a0, 0
16985a0015d6SChris Zankel
16995a0015d6SChris Zankel	/* We need to flush the cache if we have page coloring. */
17005a0015d6SChris Zankel#if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
17015a0015d6SChris Zankel	dhwb	a0, 0
17025a0015d6SChris Zankel#endif
17035a0015d6SChris Zankel	pdtlb	a0, a1
170499d5040eSMax Filippov	wdtlb	a3, a0
17055a0015d6SChris Zankel
17065a0015d6SChris Zankel	/* Exit critical section. */
17075a0015d6SChris Zankel
17085a0015d6SChris Zankel	movi	a0, 0
170999d5040eSMax Filippov	rsr	a3, excsave1
17105a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_FIXUP
17115a0015d6SChris Zankel
17125a0015d6SChris Zankel	/* Restore the working registers, and return. */
17135a0015d6SChris Zankel
171499d5040eSMax Filippov	l32i	a3, a2, PT_AREG3
17155a0015d6SChris Zankel	l32i	a1, a2, PT_AREG1
17165a0015d6SChris Zankel	l32i	a0, a2, PT_AREG0
17175a0015d6SChris Zankel	l32i	a2, a2, PT_DEPC
17185a0015d6SChris Zankel
17195a0015d6SChris Zankel	bgeui	a2, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
17205a0015d6SChris Zankel
1721bc5378fcSMax Filippov	rsr	a2, depc
17225a0015d6SChris Zankel	rfe
17235a0015d6SChris Zankel
17245a0015d6SChris Zankel	/* Double exception. Restore FIXUP handler and return. */
17255a0015d6SChris Zankel
1726bc5378fcSMax Filippov1:	xsr	a2, depc
17275a0015d6SChris Zankel	esync
17285a0015d6SChris Zankel	rfde
17295a0015d6SChris Zankel
17305a0015d6SChris Zankel9:	l32i	a0, a1, TASK_ACTIVE_MM	# unlikely case mm == 0
17315a0015d6SChris Zankel	j	8b
17325a0015d6SChris Zankel
17335a0015d6SChris Zankel2:	/* If there was a problem, handle fault in C */
17345a0015d6SChris Zankel
173599d5040eSMax Filippov	rsr	a3, depc	# still holds a2
173699d5040eSMax Filippov	s32i	a3, a2, PT_AREG2
17375a0015d6SChris Zankel	mov	a1, a2
17385a0015d6SChris Zankel
1739bc5378fcSMax Filippov	rsr	a2, ps
1740173d6681SChris Zankel	bbsi.l	a2, PS_UM_BIT, 1f
17415a0015d6SChris Zankel	j	_kernel_exception
17425a0015d6SChris Zankel1:	j	_user_exception
1743d1538c46SChris Zankel
1744d1538c46SChris ZankelENDPROC(fast_store_prohibited)
1745d1538c46SChris Zankel
1746e5083a63SJohannes Weiner#endif /* CONFIG_MMU */
17475a0015d6SChris Zankel
17485a0015d6SChris Zankel/*
1749fc4fb2adSChris Zankel * System Calls.
1750fc4fb2adSChris Zankel *
1751fc4fb2adSChris Zankel * void system_call (struct pt_regs* regs, int exccause)
1752fc4fb2adSChris Zankel *                            a2                 a3
1753fc4fb2adSChris Zankel */
1754fc4fb2adSChris Zankel
1755fc4fb2adSChris ZankelENTRY(system_call)
1756d1538c46SChris Zankel
1757fc4fb2adSChris Zankel	entry	a1, 32
1758fc4fb2adSChris Zankel
1759fc4fb2adSChris Zankel	/* regs->syscall = regs->areg[2] */
1760fc4fb2adSChris Zankel
1761fc4fb2adSChris Zankel	l32i	a3, a2, PT_AREG2
1762fc4fb2adSChris Zankel	mov	a6, a2
1763fc4fb2adSChris Zankel	movi	a4, do_syscall_trace_enter
1764fc4fb2adSChris Zankel	s32i	a3, a2, PT_SYSCALL
1765fc4fb2adSChris Zankel	callx4	a4
1766fc4fb2adSChris Zankel
1767fc4fb2adSChris Zankel	/* syscall = sys_call_table[syscall_nr] */
1768fc4fb2adSChris Zankel
1769fc4fb2adSChris Zankel	movi	a4, sys_call_table;
1770fc4fb2adSChris Zankel	movi	a5, __NR_syscall_count
1771fc4fb2adSChris Zankel	movi	a6, -ENOSYS
1772fc4fb2adSChris Zankel	bgeu	a3, a5, 1f
1773fc4fb2adSChris Zankel
1774fc4fb2adSChris Zankel	addx4	a4, a3, a4
1775fc4fb2adSChris Zankel	l32i	a4, a4, 0
1776fc4fb2adSChris Zankel	movi	a5, sys_ni_syscall;
1777fc4fb2adSChris Zankel	beq	a4, a5, 1f
1778fc4fb2adSChris Zankel
1779fc4fb2adSChris Zankel	/* Load args: arg0 - arg5 are passed via regs. */
1780fc4fb2adSChris Zankel
1781fc4fb2adSChris Zankel	l32i	a6, a2, PT_AREG6
1782fc4fb2adSChris Zankel	l32i	a7, a2, PT_AREG3
1783fc4fb2adSChris Zankel	l32i	a8, a2, PT_AREG4
1784fc4fb2adSChris Zankel	l32i	a9, a2, PT_AREG5
1785fc4fb2adSChris Zankel	l32i	a10, a2, PT_AREG8
1786fc4fb2adSChris Zankel	l32i	a11, a2, PT_AREG9
1787fc4fb2adSChris Zankel
1788fc4fb2adSChris Zankel	/* Pass one additional argument to the syscall: pt_regs (on stack) */
1789fc4fb2adSChris Zankel	s32i	a2, a1, 0
1790fc4fb2adSChris Zankel
1791fc4fb2adSChris Zankel	callx4	a4
1792fc4fb2adSChris Zankel
1793fc4fb2adSChris Zankel1:	/* regs->areg[2] = return_value */
1794fc4fb2adSChris Zankel
1795fc4fb2adSChris Zankel	s32i	a6, a2, PT_AREG2
1796fc4fb2adSChris Zankel	movi	a4, do_syscall_trace_leave
1797fc4fb2adSChris Zankel	mov	a6, a2
1798fc4fb2adSChris Zankel	callx4	a4
1799fc4fb2adSChris Zankel	retw
1800fc4fb2adSChris Zankel
1801d1538c46SChris ZankelENDPROC(system_call)
1802d1538c46SChris Zankel
1803e2fd1374SMax Filippov/*
1804e2fd1374SMax Filippov * Spill live registers on the kernel stack macro.
1805e2fd1374SMax Filippov *
1806e2fd1374SMax Filippov * Entry condition: ps.woe is set, ps.excm is cleared
1807e2fd1374SMax Filippov * Exit condition: windowstart has single bit set
1808e2fd1374SMax Filippov * May clobber: a12, a13
1809e2fd1374SMax Filippov */
1810e2fd1374SMax Filippov	.macro	spill_registers_kernel
1811e2fd1374SMax Filippov
1812e2fd1374SMax Filippov#if XCHAL_NUM_AREGS > 16
1813e2fd1374SMax Filippov	call12	1f
1814e2fd1374SMax Filippov	_j	2f
1815e2fd1374SMax Filippov	retw
1816e2fd1374SMax Filippov	.align	4
1817e2fd1374SMax Filippov1:
1818e2fd1374SMax Filippov	_entry	a1, 48
1819e2fd1374SMax Filippov	addi	a12, a0, 3
1820e2fd1374SMax Filippov#if XCHAL_NUM_AREGS > 32
1821e2fd1374SMax Filippov	.rept	(XCHAL_NUM_AREGS - 32) / 12
1822e2fd1374SMax Filippov	_entry	a1, 48
1823e2fd1374SMax Filippov	mov	a12, a0
1824e2fd1374SMax Filippov	.endr
1825e2fd1374SMax Filippov#endif
1826e2fd1374SMax Filippov	_entry	a1, 48
1827e2fd1374SMax Filippov#if XCHAL_NUM_AREGS % 12 == 0
1828e2fd1374SMax Filippov	mov	a8, a8
1829e2fd1374SMax Filippov#elif XCHAL_NUM_AREGS % 12 == 4
1830e2fd1374SMax Filippov	mov	a12, a12
1831e2fd1374SMax Filippov#elif XCHAL_NUM_AREGS % 12 == 8
1832e2fd1374SMax Filippov	mov	a4, a4
1833e2fd1374SMax Filippov#endif
1834e2fd1374SMax Filippov	retw
1835e2fd1374SMax Filippov2:
1836e2fd1374SMax Filippov#else
1837e2fd1374SMax Filippov	mov	a12, a12
1838e2fd1374SMax Filippov#endif
1839e2fd1374SMax Filippov	.endm
1840fc4fb2adSChris Zankel
1841fc4fb2adSChris Zankel/*
18425a0015d6SChris Zankel * Task switch.
18435a0015d6SChris Zankel *
18445a0015d6SChris Zankel * struct task*  _switch_to (struct task* prev, struct task* next)
18455a0015d6SChris Zankel *         a2                              a2                 a3
18465a0015d6SChris Zankel */
18475a0015d6SChris Zankel
18485a0015d6SChris ZankelENTRY(_switch_to)
18495a0015d6SChris Zankel
18505a0015d6SChris Zankel	entry	a1, 16
18515a0015d6SChris Zankel
1852e2fd1374SMax Filippov	mov	a11, a3			# and 'next' (a3)
18535a0015d6SChris Zankel
1854c658eac6SChris Zankel	l32i	a4, a2, TASK_THREAD_INFO
1855c658eac6SChris Zankel	l32i	a5, a3, TASK_THREAD_INFO
18565a0015d6SChris Zankel
1857e2fd1374SMax Filippov	save_xtregs_user a4 a6 a8 a9 a12 a13 THREAD_XTREGS_USER
18585a0015d6SChris Zankel
185952247123SMax Filippov#if THREAD_RA > 1020 || THREAD_SP > 1020
186052247123SMax Filippov	addi	a10, a2, TASK_THREAD
186152247123SMax Filippov	s32i	a0, a10, THREAD_RA - TASK_THREAD	# save return address
186252247123SMax Filippov	s32i	a1, a10, THREAD_SP - TASK_THREAD	# save stack pointer
186352247123SMax Filippov#else
186452247123SMax Filippov	s32i	a0, a2, THREAD_RA	# save return address
186552247123SMax Filippov	s32i	a1, a2, THREAD_SP	# save stack pointer
186652247123SMax Filippov#endif
1867c658eac6SChris Zankel
1868c658eac6SChris Zankel	/* Disable ints while we manipulate the stack pointer. */
1869c658eac6SChris Zankel
1870e2fd1374SMax Filippov	rsil	a14, LOCKLEVEL
1871bc5378fcSMax Filippov	rsr	a3, excsave1
18725a0015d6SChris Zankel	rsync
18735a0015d6SChris Zankel	s32i	a3, a3, EXC_TABLE_FIXUP	/* enter critical section */
18745a0015d6SChris Zankel
1875c658eac6SChris Zankel	/* Switch CPENABLE */
1876c658eac6SChris Zankel
1877c658eac6SChris Zankel#if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
1878c658eac6SChris Zankel	l32i	a3, a5, THREAD_CPENABLE
1879bc5378fcSMax Filippov	xsr	a3, cpenable
1880c658eac6SChris Zankel	s32i	a3, a4, THREAD_CPENABLE
1881c658eac6SChris Zankel#endif
1882c658eac6SChris Zankel
1883c658eac6SChris Zankel	/* Flush register file. */
1884c658eac6SChris Zankel
1885e2fd1374SMax Filippov	spill_registers_kernel
18865a0015d6SChris Zankel
18875a0015d6SChris Zankel	/* Set kernel stack (and leave critical section)
18885a0015d6SChris Zankel	 * Note: It's save to set it here. The stack will not be overwritten
18895a0015d6SChris Zankel	 *       because the kernel stack will only be loaded again after
18905a0015d6SChris Zankel	 *       we return from kernel space.
18915a0015d6SChris Zankel	 */
18925a0015d6SChris Zankel
1893bc5378fcSMax Filippov	rsr	a3, excsave1		# exc_table
1894c658eac6SChris Zankel	movi	a6, 0
1895c658eac6SChris Zankel	addi	a7, a5, PT_REGS_OFFSET
1896c658eac6SChris Zankel	s32i	a6, a3, EXC_TABLE_FIXUP
1897c658eac6SChris Zankel	s32i	a7, a3, EXC_TABLE_KSTK
18985a0015d6SChris Zankel
1899c50842dfSChris Zankel	/* restore context of the task 'next' */
19005a0015d6SChris Zankel
1901e2fd1374SMax Filippov	l32i	a0, a11, THREAD_RA	# restore return address
1902e2fd1374SMax Filippov	l32i	a1, a11, THREAD_SP	# restore stack pointer
19035a0015d6SChris Zankel
1904e2fd1374SMax Filippov	load_xtregs_user a5 a6 a8 a9 a12 a13 THREAD_XTREGS_USER
1905c658eac6SChris Zankel
1906bc5378fcSMax Filippov	wsr	a14, ps
19075a0015d6SChris Zankel	rsync
19085a0015d6SChris Zankel
19095a0015d6SChris Zankel	retw
19105a0015d6SChris Zankel
1911d1538c46SChris ZankelENDPROC(_switch_to)
19125a0015d6SChris Zankel
19135a0015d6SChris ZankelENTRY(ret_from_fork)
19145a0015d6SChris Zankel
19155a0015d6SChris Zankel	/* void schedule_tail (struct task_struct *prev)
19165a0015d6SChris Zankel	 * Note: prev is still in a6 (return value from fake call4 frame)
19175a0015d6SChris Zankel	 */
19185a0015d6SChris Zankel	movi	a4, schedule_tail
19195a0015d6SChris Zankel	callx4	a4
19205a0015d6SChris Zankel
1921fc4fb2adSChris Zankel	movi	a4, do_syscall_trace_leave
1922fc4fb2adSChris Zankel	mov	a6, a1
19235a0015d6SChris Zankel	callx4	a4
19245a0015d6SChris Zankel
19255a0015d6SChris Zankel	j	common_exception_return
19265a0015d6SChris Zankel
1927d1538c46SChris ZankelENDPROC(ret_from_fork)
1928d1538c46SChris Zankel
19293306a726SMax Filippov/*
19303306a726SMax Filippov * Kernel thread creation helper
19313306a726SMax Filippov * On entry, set up by copy_thread: a2 = thread_fn, a3 = thread_fn arg
19323306a726SMax Filippov *           left from _switch_to: a6 = prev
19333306a726SMax Filippov */
19343306a726SMax FilippovENTRY(ret_from_kernel_thread)
19353306a726SMax Filippov
19363306a726SMax Filippov	call4	schedule_tail
19373306a726SMax Filippov	mov	a6, a3
19383306a726SMax Filippov	callx4	a2
1939f0a1bf08SMax Filippov	j	common_exception_return
19403306a726SMax Filippov
19413306a726SMax FilippovENDPROC(ret_from_kernel_thread)
1942