xref: /linux-6.15/arch/xtensa/kernel/entry.S (revision 38fef73c)
15a0015d6SChris Zankel/*
25a0015d6SChris Zankel * Low-level exception handling
35a0015d6SChris Zankel *
45a0015d6SChris Zankel * This file is subject to the terms and conditions of the GNU General Public
55a0015d6SChris Zankel * License.  See the file "COPYING" in the main directory of this archive
65a0015d6SChris Zankel * for more details.
75a0015d6SChris Zankel *
82d1c645cSMarc Gauthier * Copyright (C) 2004 - 2008 by Tensilica Inc.
9*38fef73cSMax Filippov * Copyright (C) 2015 Cadence Design Systems Inc.
105a0015d6SChris Zankel *
115a0015d6SChris Zankel * Chris Zankel <[email protected]>
125a0015d6SChris Zankel *
135a0015d6SChris Zankel */
145a0015d6SChris Zankel
155a0015d6SChris Zankel#include <linux/linkage.h>
160013a854SSam Ravnborg#include <asm/asm-offsets.h>
175a0015d6SChris Zankel#include <asm/processor.h>
184573e398SChris Zankel#include <asm/coprocessor.h>
195a0015d6SChris Zankel#include <asm/thread_info.h>
205a0015d6SChris Zankel#include <asm/uaccess.h>
215a0015d6SChris Zankel#include <asm/unistd.h>
225a0015d6SChris Zankel#include <asm/ptrace.h>
235a0015d6SChris Zankel#include <asm/current.h>
245a0015d6SChris Zankel#include <asm/pgtable.h>
255a0015d6SChris Zankel#include <asm/page.h>
265a0015d6SChris Zankel#include <asm/signal.h>
27173d6681SChris Zankel#include <asm/tlbflush.h>
28367b8112SChris Zankel#include <variant/tie-asm.h>
295a0015d6SChris Zankel
305a0015d6SChris Zankel/* Unimplemented features. */
315a0015d6SChris Zankel
325a0015d6SChris Zankel#undef KERNEL_STACK_OVERFLOW_CHECK
335a0015d6SChris Zankel
345a0015d6SChris Zankel/* Not well tested.
355a0015d6SChris Zankel *
365a0015d6SChris Zankel * - fast_coprocessor
375a0015d6SChris Zankel */
385a0015d6SChris Zankel
395a0015d6SChris Zankel/*
405a0015d6SChris Zankel * Macro to find first bit set in WINDOWBASE from the left + 1
415a0015d6SChris Zankel *
425a0015d6SChris Zankel * 100....0 -> 1
435a0015d6SChris Zankel * 010....0 -> 2
445a0015d6SChris Zankel * 000....1 -> WSBITS
455a0015d6SChris Zankel */
465a0015d6SChris Zankel
475a0015d6SChris Zankel	.macro ffs_ws bit mask
485a0015d6SChris Zankel
495a0015d6SChris Zankel#if XCHAL_HAVE_NSA
505a0015d6SChris Zankel	nsau    \bit, \mask			# 32-WSBITS ... 31 (32 iff 0)
515a0015d6SChris Zankel	addi    \bit, \bit, WSBITS - 32 + 1   	# uppest bit set -> return 1
525a0015d6SChris Zankel#else
535a0015d6SChris Zankel	movi    \bit, WSBITS
545a0015d6SChris Zankel#if WSBITS > 16
555a0015d6SChris Zankel	_bltui  \mask, 0x10000, 99f
565a0015d6SChris Zankel	addi    \bit, \bit, -16
575a0015d6SChris Zankel	extui   \mask, \mask, 16, 16
585a0015d6SChris Zankel#endif
595a0015d6SChris Zankel#if WSBITS > 8
605a0015d6SChris Zankel99:	_bltui  \mask, 0x100, 99f
615a0015d6SChris Zankel	addi    \bit, \bit, -8
625a0015d6SChris Zankel	srli    \mask, \mask, 8
635a0015d6SChris Zankel#endif
645a0015d6SChris Zankel99:	_bltui  \mask, 0x10, 99f
655a0015d6SChris Zankel	addi    \bit, \bit, -4
665a0015d6SChris Zankel	srli    \mask, \mask, 4
675a0015d6SChris Zankel99:	_bltui  \mask, 0x4, 99f
685a0015d6SChris Zankel	addi    \bit, \bit, -2
695a0015d6SChris Zankel	srli    \mask, \mask, 2
705a0015d6SChris Zankel99:	_bltui  \mask, 0x2, 99f
715a0015d6SChris Zankel	addi    \bit, \bit, -1
725a0015d6SChris Zankel99:
735a0015d6SChris Zankel
745a0015d6SChris Zankel#endif
755a0015d6SChris Zankel	.endm
765a0015d6SChris Zankel
77*38fef73cSMax Filippov
78*38fef73cSMax Filippov	.macro	irq_save flags tmp
79*38fef73cSMax Filippov#if XTENSA_FAKE_NMI
80*38fef73cSMax Filippov#if defined(CONFIG_DEBUG_KERNEL) && (LOCKLEVEL | TOPLEVEL) >= XCHAL_DEBUGLEVEL
81*38fef73cSMax Filippov	rsr	\flags, ps
82*38fef73cSMax Filippov	extui	\tmp, \flags, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
83*38fef73cSMax Filippov	bgei	\tmp, LOCKLEVEL, 99f
84*38fef73cSMax Filippov	rsil	\tmp, LOCKLEVEL
85*38fef73cSMax Filippov99:
86*38fef73cSMax Filippov#else
87*38fef73cSMax Filippov	movi	\tmp, LOCKLEVEL
88*38fef73cSMax Filippov	rsr	\flags, ps
89*38fef73cSMax Filippov	or	\flags, \flags, \tmp
90*38fef73cSMax Filippov	xsr	\flags, ps
91*38fef73cSMax Filippov	rsync
92*38fef73cSMax Filippov#endif
93*38fef73cSMax Filippov#else
94*38fef73cSMax Filippov	rsil	\flags, LOCKLEVEL
95*38fef73cSMax Filippov#endif
96*38fef73cSMax Filippov	.endm
97*38fef73cSMax Filippov
985a0015d6SChris Zankel/* ----------------- DEFAULT FIRST LEVEL EXCEPTION HANDLERS ----------------- */
995a0015d6SChris Zankel
1005a0015d6SChris Zankel/*
1015a0015d6SChris Zankel * First-level exception handler for user exceptions.
1025a0015d6SChris Zankel * Save some special registers, extra states and all registers in the AR
1035a0015d6SChris Zankel * register file that were in use in the user task, and jump to the common
1045a0015d6SChris Zankel * exception code.
1055a0015d6SChris Zankel * We save SAR (used to calculate WMASK), and WB and WS (we don't have to
1065a0015d6SChris Zankel * save them for kernel exceptions).
1075a0015d6SChris Zankel *
1085a0015d6SChris Zankel * Entry condition for user_exception:
1095a0015d6SChris Zankel *
1105a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
1115a0015d6SChris Zankel *   a1:	a1
1125a0015d6SChris Zankel *   a2:	new stack pointer, original value in depc
11399d5040eSMax Filippov *   a3:	a3
1145a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
11599d5040eSMax Filippov *   excsave1:	dispatch table
1165a0015d6SChris Zankel *
1175a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
1185a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
1195a0015d6SChris Zankel *
1205a0015d6SChris Zankel * Entry condition for _user_exception:
1215a0015d6SChris Zankel *
1225a0015d6SChris Zankel *   a0-a3 and depc have been saved to PT_AREG0...PT_AREG3 and PT_DEPC
1235a0015d6SChris Zankel *   excsave has been restored, and
1245a0015d6SChris Zankel *   stack pointer (a1) has been set.
1255a0015d6SChris Zankel *
1263ad2f3fbSDaniel Mack * Note: _user_exception might be at an odd address. Don't use call0..call12
1275a0015d6SChris Zankel */
1285a0015d6SChris Zankel
1295a0015d6SChris ZankelENTRY(user_exception)
1305a0015d6SChris Zankel
13199d5040eSMax Filippov	/* Save a1, a2, a3, and set SP. */
1325a0015d6SChris Zankel
133bc5378fcSMax Filippov	rsr	a0, depc
1345a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
1355a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2
1365a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
1375a0015d6SChris Zankel	mov	a1, a2
1385a0015d6SChris Zankel
1395a0015d6SChris Zankel	.globl _user_exception
1405a0015d6SChris Zankel_user_exception:
1415a0015d6SChris Zankel
1425a0015d6SChris Zankel	/* Save SAR and turn off single stepping */
1435a0015d6SChris Zankel
1445a0015d6SChris Zankel	movi	a2, 0
145b6569439SMax Filippov	wsr	a2, depc		# terminate user stack trace with 0
146bc5378fcSMax Filippov	rsr	a3, sar
147bc5378fcSMax Filippov	xsr	a2, icountlevel
1485a0015d6SChris Zankel	s32i	a3, a1, PT_SAR
14929c4dfd9SChris Zankel	s32i	a2, a1, PT_ICOUNTLEVEL
1505a0015d6SChris Zankel
151c50842dfSChris Zankel#if XCHAL_HAVE_THREADPTR
152c50842dfSChris Zankel	rur	a2, threadptr
153c50842dfSChris Zankel	s32i	a2, a1, PT_THREADPTR
154c50842dfSChris Zankel#endif
155c50842dfSChris Zankel
1565a0015d6SChris Zankel	/* Rotate ws so that the current windowbase is at bit0. */
1575a0015d6SChris Zankel	/* Assume ws = xxwww1yyyy. Rotate ws right, so that a2 = yyyyxxwww1 */
1585a0015d6SChris Zankel
159bc5378fcSMax Filippov	rsr	a2, windowbase
160bc5378fcSMax Filippov	rsr	a3, windowstart
1615a0015d6SChris Zankel	ssr	a2
1625a0015d6SChris Zankel	s32i	a2, a1, PT_WINDOWBASE
1635a0015d6SChris Zankel	s32i	a3, a1, PT_WINDOWSTART
1645a0015d6SChris Zankel	slli	a2, a3, 32-WSBITS
1655a0015d6SChris Zankel	src	a2, a3, a2
1665a0015d6SChris Zankel	srli	a2, a2, 32-WSBITS
1675a0015d6SChris Zankel	s32i	a2, a1, PT_WMASK	# needed for restoring registers
1685a0015d6SChris Zankel
1695a0015d6SChris Zankel	/* Save only live registers. */
1705a0015d6SChris Zankel
1715a0015d6SChris Zankel	_bbsi.l	a2, 1, 1f
1725a0015d6SChris Zankel	s32i	a4, a1, PT_AREG4
1735a0015d6SChris Zankel	s32i	a5, a1, PT_AREG5
1745a0015d6SChris Zankel	s32i	a6, a1, PT_AREG6
1755a0015d6SChris Zankel	s32i	a7, a1, PT_AREG7
1765a0015d6SChris Zankel	_bbsi.l	a2, 2, 1f
1775a0015d6SChris Zankel	s32i	a8, a1, PT_AREG8
1785a0015d6SChris Zankel	s32i	a9, a1, PT_AREG9
1795a0015d6SChris Zankel	s32i	a10, a1, PT_AREG10
1805a0015d6SChris Zankel	s32i	a11, a1, PT_AREG11
1815a0015d6SChris Zankel	_bbsi.l	a2, 3, 1f
1825a0015d6SChris Zankel	s32i	a12, a1, PT_AREG12
1835a0015d6SChris Zankel	s32i	a13, a1, PT_AREG13
1845a0015d6SChris Zankel	s32i	a14, a1, PT_AREG14
1855a0015d6SChris Zankel	s32i	a15, a1, PT_AREG15
1865a0015d6SChris Zankel	_bnei	a2, 1, 1f		# only one valid frame?
1875a0015d6SChris Zankel
1885a0015d6SChris Zankel	/* Only one valid frame, skip saving regs. */
1895a0015d6SChris Zankel
1905a0015d6SChris Zankel	j	2f
1915a0015d6SChris Zankel
1925a0015d6SChris Zankel	/* Save the remaining registers.
1935a0015d6SChris Zankel	 * We have to save all registers up to the first '1' from
1945a0015d6SChris Zankel	 * the right, except the current frame (bit 0).
1955a0015d6SChris Zankel	 * Assume a2 is:  001001000110001
1966656920bSChris Zankel	 * All register frames starting from the top field to the marked '1'
1975a0015d6SChris Zankel	 * must be saved.
1985a0015d6SChris Zankel	 */
1995a0015d6SChris Zankel
2005a0015d6SChris Zankel1:	addi	a3, a2, -1		# eliminate '1' in bit 0: yyyyxxww0
2015a0015d6SChris Zankel	neg	a3, a3			# yyyyxxww0 -> YYYYXXWW1+1
2025a0015d6SChris Zankel	and	a3, a3, a2		# max. only one bit is set
2035a0015d6SChris Zankel
2045a0015d6SChris Zankel	/* Find number of frames to save */
2055a0015d6SChris Zankel
2065a0015d6SChris Zankel	ffs_ws	a0, a3			# number of frames to the '1' from left
2075a0015d6SChris Zankel
2085a0015d6SChris Zankel	/* Store information into WMASK:
2095a0015d6SChris Zankel	 * bits 0..3: xxx1 masked lower 4 bits of the rotated windowstart,
2105a0015d6SChris Zankel	 * bits 4...: number of valid 4-register frames
2115a0015d6SChris Zankel	 */
2125a0015d6SChris Zankel
2135a0015d6SChris Zankel	slli	a3, a0, 4		# number of frames to save in bits 8..4
2145a0015d6SChris Zankel	extui	a2, a2, 0, 4		# mask for the first 16 registers
2155a0015d6SChris Zankel	or	a2, a3, a2
2165a0015d6SChris Zankel	s32i	a2, a1, PT_WMASK	# needed when we restore the reg-file
2175a0015d6SChris Zankel
2185a0015d6SChris Zankel	/* Save 4 registers at a time */
2195a0015d6SChris Zankel
2205a0015d6SChris Zankel1:	rotw	-1
2215a0015d6SChris Zankel	s32i	a0, a5, PT_AREG_END - 16
2225a0015d6SChris Zankel	s32i	a1, a5, PT_AREG_END - 12
2235a0015d6SChris Zankel	s32i	a2, a5, PT_AREG_END - 8
2245a0015d6SChris Zankel	s32i	a3, a5, PT_AREG_END - 4
2255a0015d6SChris Zankel	addi	a0, a4, -1
2265a0015d6SChris Zankel	addi	a1, a5, -16
2275a0015d6SChris Zankel	_bnez	a0, 1b
2285a0015d6SChris Zankel
2295a0015d6SChris Zankel	/* WINDOWBASE still in SAR! */
2305a0015d6SChris Zankel
231bc5378fcSMax Filippov	rsr	a2, sar			# original WINDOWBASE
2325a0015d6SChris Zankel	movi	a3, 1
2335a0015d6SChris Zankel	ssl	a2
2345a0015d6SChris Zankel	sll	a3, a3
235bc5378fcSMax Filippov	wsr	a3, windowstart		# set corresponding WINDOWSTART bit
236bc5378fcSMax Filippov	wsr	a2, windowbase		# and WINDOWSTART
2375a0015d6SChris Zankel	rsync
2385a0015d6SChris Zankel
2395a0015d6SChris Zankel	/* We are back to the original stack pointer (a1) */
2405a0015d6SChris Zankel
241c658eac6SChris Zankel2:	/* Now, jump to the common exception handler. */
2425a0015d6SChris Zankel
2435a0015d6SChris Zankel	j	common_exception
2445a0015d6SChris Zankel
245d1538c46SChris ZankelENDPROC(user_exception)
2465a0015d6SChris Zankel
2475a0015d6SChris Zankel/*
2485a0015d6SChris Zankel * First-level exit handler for kernel exceptions
2495a0015d6SChris Zankel * Save special registers and the live window frame.
2505a0015d6SChris Zankel * Note: Even though we changes the stack pointer, we don't have to do a
2515a0015d6SChris Zankel *	 MOVSP here, as we do that when we return from the exception.
2525a0015d6SChris Zankel *	 (See comment in the kernel exception exit code)
2535a0015d6SChris Zankel *
2545a0015d6SChris Zankel * Entry condition for kernel_exception:
2555a0015d6SChris Zankel *
2565a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
2575a0015d6SChris Zankel *   a1:	a1
2585a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
25999d5040eSMax Filippov *   a3:	a3
2605a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
26199d5040eSMax Filippov *   excsave_1:	dispatch table
2625a0015d6SChris Zankel *
2635a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
2645a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
2655a0015d6SChris Zankel *
2665a0015d6SChris Zankel * Entry condition for _kernel_exception:
2675a0015d6SChris Zankel *
2685a0015d6SChris Zankel *   a0-a3 and depc have been saved to PT_AREG0...PT_AREG3 and PT_DEPC
2695a0015d6SChris Zankel *   excsave has been restored, and
2705a0015d6SChris Zankel *   stack pointer (a1) has been set.
2715a0015d6SChris Zankel *
2723ad2f3fbSDaniel Mack * Note: _kernel_exception might be at an odd address. Don't use call0..call12
2735a0015d6SChris Zankel */
2745a0015d6SChris Zankel
2755a0015d6SChris ZankelENTRY(kernel_exception)
2765a0015d6SChris Zankel
27799d5040eSMax Filippov	/* Save a1, a2, a3, and set SP. */
2785a0015d6SChris Zankel
279bc5378fcSMax Filippov	rsr	a0, depc		# get a2
2805a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
2815a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2
2825a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
2835a0015d6SChris Zankel	mov	a1, a2
2845a0015d6SChris Zankel
2855a0015d6SChris Zankel	.globl _kernel_exception
2865a0015d6SChris Zankel_kernel_exception:
2875a0015d6SChris Zankel
2885a0015d6SChris Zankel	/* Save SAR and turn off single stepping */
2895a0015d6SChris Zankel
2905a0015d6SChris Zankel	movi	a2, 0
291bc5378fcSMax Filippov	rsr	a3, sar
292bc5378fcSMax Filippov	xsr	a2, icountlevel
2935a0015d6SChris Zankel	s32i	a3, a1, PT_SAR
29429c4dfd9SChris Zankel	s32i	a2, a1, PT_ICOUNTLEVEL
2955a0015d6SChris Zankel
2965a0015d6SChris Zankel	/* Rotate ws so that the current windowbase is at bit0. */
2975a0015d6SChris Zankel	/* Assume ws = xxwww1yyyy. Rotate ws right, so that a2 = yyyyxxwww1 */
2985a0015d6SChris Zankel
299bc5378fcSMax Filippov	rsr	a2, windowbase		# don't need to save these, we only
300bc5378fcSMax Filippov	rsr	a3, windowstart		# need shifted windowstart: windowmask
3015a0015d6SChris Zankel	ssr	a2
3025a0015d6SChris Zankel	slli	a2, a3, 32-WSBITS
3035a0015d6SChris Zankel	src	a2, a3, a2
3045a0015d6SChris Zankel	srli	a2, a2, 32-WSBITS
3055a0015d6SChris Zankel	s32i	a2, a1, PT_WMASK	# needed for kernel_exception_exit
3065a0015d6SChris Zankel
3075a0015d6SChris Zankel	/* Save only the live window-frame */
3085a0015d6SChris Zankel
3095a0015d6SChris Zankel	_bbsi.l	a2, 1, 1f
3105a0015d6SChris Zankel	s32i	a4, a1, PT_AREG4
3115a0015d6SChris Zankel	s32i	a5, a1, PT_AREG5
3125a0015d6SChris Zankel	s32i	a6, a1, PT_AREG6
3135a0015d6SChris Zankel	s32i	a7, a1, PT_AREG7
3145a0015d6SChris Zankel	_bbsi.l	a2, 2, 1f
3155a0015d6SChris Zankel	s32i	a8, a1, PT_AREG8
3165a0015d6SChris Zankel	s32i	a9, a1, PT_AREG9
3175a0015d6SChris Zankel	s32i	a10, a1, PT_AREG10
3185a0015d6SChris Zankel	s32i	a11, a1, PT_AREG11
3195a0015d6SChris Zankel	_bbsi.l	a2, 3, 1f
3205a0015d6SChris Zankel	s32i	a12, a1, PT_AREG12
3215a0015d6SChris Zankel	s32i	a13, a1, PT_AREG13
3225a0015d6SChris Zankel	s32i	a14, a1, PT_AREG14
3235a0015d6SChris Zankel	s32i	a15, a1, PT_AREG15
3245a0015d6SChris Zankel
325b6569439SMax Filippov	_bnei	a2, 1, 1f
326b6569439SMax Filippov
327b6569439SMax Filippov	/* Copy spill slots of a0 and a1 to imitate movsp
328b6569439SMax Filippov	 * in order to keep exception stack continuous
329b6569439SMax Filippov	 */
330b6569439SMax Filippov	l32i	a3, a1, PT_SIZE
331b6569439SMax Filippov	l32i	a0, a1, PT_SIZE + 4
332b6569439SMax Filippov	s32e	a3, a1, -16
333b6569439SMax Filippov	s32e	a0, a1, -12
3345a0015d6SChris Zankel1:
335b6569439SMax Filippov	l32i	a0, a1, PT_AREG0	# restore saved a0
336b6569439SMax Filippov	wsr	a0, depc
3375a0015d6SChris Zankel
3385a0015d6SChris Zankel#ifdef KERNEL_STACK_OVERFLOW_CHECK
3395a0015d6SChris Zankel
3405a0015d6SChris Zankel	/*  Stack overflow check, for debugging  */
3415a0015d6SChris Zankel	extui	a2, a1, TASK_SIZE_BITS,XX
3425a0015d6SChris Zankel	movi	a3, SIZE??
3435a0015d6SChris Zankel	_bge	a2, a3, out_of_stack_panic
3445a0015d6SChris Zankel
3455a0015d6SChris Zankel#endif
3465a0015d6SChris Zankel
3475a0015d6SChris Zankel/*
3485a0015d6SChris Zankel * This is the common exception handler.
3495a0015d6SChris Zankel * We get here from the user exception handler or simply by falling through
3505a0015d6SChris Zankel * from the kernel exception handler.
3515a0015d6SChris Zankel * Save the remaining special registers, switch to kernel mode, and jump
3525a0015d6SChris Zankel * to the second-level exception handler.
3535a0015d6SChris Zankel *
3545a0015d6SChris Zankel */
3555a0015d6SChris Zankel
3565a0015d6SChris Zankelcommon_exception:
3575a0015d6SChris Zankel
35829c4dfd9SChris Zankel	/* Save some registers, disable loops and clear the syscall flag. */
3595a0015d6SChris Zankel
360bc5378fcSMax Filippov	rsr	a2, debugcause
361bc5378fcSMax Filippov	rsr	a3, epc1
3625a0015d6SChris Zankel	s32i	a2, a1, PT_DEBUGCAUSE
3635a0015d6SChris Zankel	s32i	a3, a1, PT_PC
3645a0015d6SChris Zankel
36529c4dfd9SChris Zankel	movi	a2, -1
366bc5378fcSMax Filippov	rsr	a3, excvaddr
36729c4dfd9SChris Zankel	s32i	a2, a1, PT_SYSCALL
3685a0015d6SChris Zankel	movi	a2, 0
3695a0015d6SChris Zankel	s32i	a3, a1, PT_EXCVADDR
370bc5378fcSMax Filippov	xsr	a2, lcount
3715a0015d6SChris Zankel	s32i	a2, a1, PT_LCOUNT
3725a0015d6SChris Zankel
3735a0015d6SChris Zankel	/* It is now save to restore the EXC_TABLE_FIXUP variable. */
3745a0015d6SChris Zankel
375*38fef73cSMax Filippov	rsr	a2, exccause
3765a0015d6SChris Zankel	movi	a3, 0
377*38fef73cSMax Filippov	rsr	a0, excsave1
378*38fef73cSMax Filippov	s32i	a2, a1, PT_EXCCAUSE
379*38fef73cSMax Filippov	s32i	a3, a0, EXC_TABLE_FIXUP
3805a0015d6SChris Zankel
381b6569439SMax Filippov	/* All unrecoverable states are saved on stack, now, and a1 is valid.
382b6569439SMax Filippov	 * Now we can allow exceptions again. In case we've got an interrupt
383b6569439SMax Filippov	 * PS.INTLEVEL is set to LOCKLEVEL disabling furhter interrupts,
384b6569439SMax Filippov	 * otherwise it's left unchanged.
3855a0015d6SChris Zankel	 *
386b6569439SMax Filippov	 * Set PS(EXCM = 0, UM = 0, RING = 0, OWB = 0, WOE = 1, INTLEVEL = X)
3875a0015d6SChris Zankel	 */
3885a0015d6SChris Zankel
389bc5378fcSMax Filippov	rsr	a3, ps
390*38fef73cSMax Filippov	s32i	a3, a1, PT_PS		# save ps
391*38fef73cSMax Filippov
392*38fef73cSMax Filippov#if XTENSA_FAKE_NMI
393*38fef73cSMax Filippov	/* Correct PS needs to be saved in the PT_PS:
394*38fef73cSMax Filippov	 * - in case of exception or level-1 interrupt it's in the PS,
395*38fef73cSMax Filippov	 *   and is already saved.
396*38fef73cSMax Filippov	 * - in case of medium level interrupt it's in the excsave2.
397*38fef73cSMax Filippov	 */
398*38fef73cSMax Filippov	movi	a0, EXCCAUSE_MAPPED_NMI
399*38fef73cSMax Filippov	extui	a3, a3, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
400*38fef73cSMax Filippov	beq	a2, a0, .Lmedium_level_irq
401*38fef73cSMax Filippov	bnei	a2, EXCCAUSE_LEVEL1_INTERRUPT, .Lexception
402*38fef73cSMax Filippov	beqz	a3, .Llevel1_irq	# level-1 IRQ sets ps.intlevel to 0
403*38fef73cSMax Filippov
404*38fef73cSMax Filippov.Lmedium_level_irq:
405*38fef73cSMax Filippov	rsr	a0, excsave2
406*38fef73cSMax Filippov	s32i	a0, a1, PT_PS		# save medium-level interrupt ps
407*38fef73cSMax Filippov	bgei	a3, LOCKLEVEL, .Lexception
408*38fef73cSMax Filippov
409*38fef73cSMax Filippov.Llevel1_irq:
410*38fef73cSMax Filippov	movi	a3, LOCKLEVEL
411*38fef73cSMax Filippov
412*38fef73cSMax Filippov.Lexception:
413*38fef73cSMax Filippov	movi	a0, 1 << PS_WOE_BIT
414*38fef73cSMax Filippov	or	a3, a3, a0
415*38fef73cSMax Filippov#else
416*38fef73cSMax Filippov	addi	a2, a2, -EXCCAUSE_LEVEL1_INTERRUPT
417*38fef73cSMax Filippov	movi	a0, LOCKLEVEL
4182d1c645cSMarc Gauthier	extui	a3, a3, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
4192d1c645cSMarc Gauthier					# a3 = PS.INTLEVEL
420*38fef73cSMax Filippov	moveqz	a3, a0, a2		# a3 = LOCKLEVEL iff interrupt
421173d6681SChris Zankel	movi	a2, 1 << PS_WOE_BIT
4225a0015d6SChris Zankel	or	a3, a3, a2
423b6569439SMax Filippov	rsr	a2, exccause
424*38fef73cSMax Filippov#endif
425*38fef73cSMax Filippov
426b6569439SMax Filippov	/* restore return address (or 0 if return to userspace) */
427b6569439SMax Filippov	rsr	a0, depc
428*38fef73cSMax Filippov	wsr	a3, ps
429*38fef73cSMax Filippov	rsync				# PS.WOE => rsync => overflow
4305a0015d6SChris Zankel
431bc5378fcSMax Filippov	/* Save lbeg, lend */
4325a0015d6SChris Zankel
433b6569439SMax Filippov	rsr	a4, lbeg
434bc5378fcSMax Filippov	rsr	a3, lend
435b6569439SMax Filippov	s32i	a4, a1, PT_LBEG
4365a0015d6SChris Zankel	s32i	a3, a1, PT_LEND
4375a0015d6SChris Zankel
438733536b8SMax Filippov	/* Save SCOMPARE1 */
439733536b8SMax Filippov
440733536b8SMax Filippov#if XCHAL_HAVE_S32C1I
441b6569439SMax Filippov	rsr     a3, scompare1
442b6569439SMax Filippov	s32i    a3, a1, PT_SCOMPARE1
443733536b8SMax Filippov#endif
444733536b8SMax Filippov
445c658eac6SChris Zankel	/* Save optional registers. */
446c658eac6SChris Zankel
447b6569439SMax Filippov	save_xtregs_opt a1 a3 a4 a5 a6 a7 PT_XTREGS_OPT
448c658eac6SChris Zankel
4495a0015d6SChris Zankel	/* Go to second-level dispatcher. Set up parameters to pass to the
4505a0015d6SChris Zankel	 * exception handler and call the exception handler.
4515a0015d6SChris Zankel	 */
4525a0015d6SChris Zankel
45399d5040eSMax Filippov	rsr	a4, excsave1
4545a0015d6SChris Zankel	mov	a6, a1			# pass stack frame
455b6569439SMax Filippov	mov	a7, a2			# pass EXCCAUSE
456b6569439SMax Filippov	addx4	a4, a2, a4
4575a0015d6SChris Zankel	l32i	a4, a4, EXC_TABLE_DEFAULT		# load handler
4585a0015d6SChris Zankel
4595a0015d6SChris Zankel	/* Call the second-level handler */
4605a0015d6SChris Zankel
4615a0015d6SChris Zankel	callx4	a4
4625a0015d6SChris Zankel
4635a0015d6SChris Zankel	/* Jump here for exception exit */
464e6ffe17eSdann	.global common_exception_return
4655a0015d6SChris Zankelcommon_exception_return:
4665a0015d6SChris Zankel
467*38fef73cSMax Filippov#if XTENSA_FAKE_NMI
468*38fef73cSMax Filippov	l32i	a2, a1, PT_EXCCAUSE
469*38fef73cSMax Filippov	movi	a3, EXCCAUSE_MAPPED_NMI
470*38fef73cSMax Filippov	beq	a2, a3, .LNMIexit
471*38fef73cSMax Filippov#endif
472c92931b2SMax Filippov1:
473*38fef73cSMax Filippov	irq_save a2, a3
4747d5f6a9aSMax Filippov#ifdef CONFIG_TRACE_IRQFLAGS
4757d5f6a9aSMax Filippov	movi	a4, trace_hardirqs_off
4767d5f6a9aSMax Filippov	callx4	a4
4777d5f6a9aSMax Filippov#endif
478c92931b2SMax Filippov
4795a0015d6SChris Zankel	/* Jump if we are returning from kernel exceptions. */
4805a0015d6SChris Zankel
481aea8e7c8SMax Filippov	l32i	a3, a1, PT_PS
48216c5becfSMax Filippov	GET_THREAD_INFO(a2, a1)
48316c5becfSMax Filippov	l32i	a4, a2, TI_FLAGS
48416c5becfSMax Filippov	_bbci.l	a3, PS_UM_BIT, 6f
4855a0015d6SChris Zankel
4865a0015d6SChris Zankel	/* Specific to a user exception exit:
4875a0015d6SChris Zankel	 * We need to check some flags for signal handling and rescheduling,
4885a0015d6SChris Zankel	 * and have to restore WB and WS, extra states, and all registers
4895a0015d6SChris Zankel	 * in the register file that were in use in the user task.
490e1088430SChris Zankel	 * Note that we don't disable interrupts here.
4915a0015d6SChris Zankel	 */
4925a0015d6SChris Zankel
4935a0015d6SChris Zankel	_bbsi.l	a4, TIF_NEED_RESCHED, 3f
494a53bb24eSAl Viro	_bbsi.l	a4, TIF_NOTIFY_RESUME, 2f
495a99e07eeSMax Filippov	_bbci.l	a4, TIF_SIGPENDING, 5f
4965a0015d6SChris Zankel
497a53bb24eSAl Viro2:	l32i	a4, a1, PT_DEPC
4985a0015d6SChris Zankel	bgeui	a4, VALID_DOUBLE_EXCEPTION_ADDRESS, 4f
4995a0015d6SChris Zankel
500e1088430SChris Zankel	/* Call do_signal() */
501e1088430SChris Zankel
5027d5f6a9aSMax Filippov#ifdef CONFIG_TRACE_IRQFLAGS
5037d5f6a9aSMax Filippov	movi	a4, trace_hardirqs_on
5047d5f6a9aSMax Filippov	callx4	a4
5057d5f6a9aSMax Filippov#endif
506aea8e7c8SMax Filippov	rsil	a2, 0
507a53bb24eSAl Viro	movi	a4, do_notify_resume	# int do_notify_resume(struct pt_regs*)
5085a0015d6SChris Zankel	mov	a6, a1
5095a0015d6SChris Zankel	callx4	a4
5105a0015d6SChris Zankel	j	1b
5115a0015d6SChris Zankel
512e1088430SChris Zankel3:	/* Reschedule */
5135a0015d6SChris Zankel
5147d5f6a9aSMax Filippov#ifdef CONFIG_TRACE_IRQFLAGS
5157d5f6a9aSMax Filippov	movi	a4, trace_hardirqs_on
5167d5f6a9aSMax Filippov	callx4	a4
5177d5f6a9aSMax Filippov#endif
518aea8e7c8SMax Filippov	rsil	a2, 0
5195a0015d6SChris Zankel	movi	a4, schedule	# void schedule (void)
5205a0015d6SChris Zankel	callx4	a4
5215a0015d6SChris Zankel	j	1b
5225a0015d6SChris Zankel
52316c5becfSMax Filippov#ifdef CONFIG_PREEMPT
52416c5becfSMax Filippov6:
52516c5becfSMax Filippov	_bbci.l	a4, TIF_NEED_RESCHED, 4f
52616c5becfSMax Filippov
52716c5becfSMax Filippov	/* Check current_thread_info->preempt_count */
52816c5becfSMax Filippov
52916c5becfSMax Filippov	l32i	a4, a2, TI_PRE_COUNT
53016c5becfSMax Filippov	bnez	a4, 4f
53116c5becfSMax Filippov	movi	a4, preempt_schedule_irq
53216c5becfSMax Filippov	callx4	a4
53316c5becfSMax Filippov	j	1b
53416c5becfSMax Filippov#endif
53516c5becfSMax Filippov
536*38fef73cSMax Filippov#if XTENSA_FAKE_NMI
537*38fef73cSMax Filippov.LNMIexit:
538*38fef73cSMax Filippov	l32i	a3, a1, PT_PS
539*38fef73cSMax Filippov	_bbci.l	a3, PS_UM_BIT, 4f
540*38fef73cSMax Filippov#endif
541*38fef73cSMax Filippov
542a99e07eeSMax Filippov5:
543a99e07eeSMax Filippov#ifdef CONFIG_DEBUG_TLB_SANITY
544a99e07eeSMax Filippov	l32i	a4, a1, PT_DEPC
545a99e07eeSMax Filippov	bgeui	a4, VALID_DOUBLE_EXCEPTION_ADDRESS, 4f
546a99e07eeSMax Filippov	movi	a4, check_tlb_sanity
547a99e07eeSMax Filippov	callx4	a4
548a99e07eeSMax Filippov#endif
54916c5becfSMax Filippov6:
550aea8e7c8SMax Filippov4:
551aea8e7c8SMax Filippov#ifdef CONFIG_TRACE_IRQFLAGS
5527d5f6a9aSMax Filippov	extui	a4, a3, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
5537d5f6a9aSMax Filippov	bgei	a4, LOCKLEVEL, 1f
554aea8e7c8SMax Filippov	movi	a4, trace_hardirqs_on
555aea8e7c8SMax Filippov	callx4	a4
556aea8e7c8SMax Filippov1:
557aea8e7c8SMax Filippov#endif
558aea8e7c8SMax Filippov	/* Restore optional registers. */
5595a0015d6SChris Zankel
560e1088430SChris Zankel	load_xtregs_opt a1 a2 a4 a5 a6 a7 PT_XTREGS_OPT
561e1088430SChris Zankel
562733536b8SMax Filippov	/* Restore SCOMPARE1 */
563733536b8SMax Filippov
564733536b8SMax Filippov#if XCHAL_HAVE_S32C1I
565733536b8SMax Filippov	l32i    a2, a1, PT_SCOMPARE1
566733536b8SMax Filippov	wsr     a2, scompare1
567733536b8SMax Filippov#endif
568bc5378fcSMax Filippov	wsr	a3, ps		/* disable interrupts */
569e1088430SChris Zankel
570e1088430SChris Zankel	_bbci.l	a3, PS_UM_BIT, kernel_exception_exit
571e1088430SChris Zankel
572e1088430SChris Zankeluser_exception_exit:
573e1088430SChris Zankel
574e1088430SChris Zankel	/* Restore the state of the task and return from the exception. */
5755a0015d6SChris Zankel
5765a0015d6SChris Zankel	/* Switch to the user thread WINDOWBASE. Save SP temporarily in DEPC */
5775a0015d6SChris Zankel
5785a0015d6SChris Zankel	l32i	a2, a1, PT_WINDOWBASE
5795a0015d6SChris Zankel	l32i	a3, a1, PT_WINDOWSTART
580bc5378fcSMax Filippov	wsr	a1, depc		# use DEPC as temp storage
581bc5378fcSMax Filippov	wsr	a3, windowstart		# restore WINDOWSTART
5825a0015d6SChris Zankel	ssr	a2			# preserve user's WB in the SAR
583bc5378fcSMax Filippov	wsr	a2, windowbase		# switch to user's saved WB
5845a0015d6SChris Zankel	rsync
585bc5378fcSMax Filippov	rsr	a1, depc		# restore stack pointer
5865a0015d6SChris Zankel	l32i	a2, a1, PT_WMASK	# register frames saved (in bits 4...9)
5875a0015d6SChris Zankel	rotw	-1			# we restore a4..a7
5885a0015d6SChris Zankel	_bltui	a6, 16, 1f		# only have to restore current window?
5895a0015d6SChris Zankel
5905a0015d6SChris Zankel	/* The working registers are a0 and a3.  We are restoring to
5915a0015d6SChris Zankel	 * a4..a7.  Be careful not to destroy what we have just restored.
5925a0015d6SChris Zankel	 * Note: wmask has the format YYYYM:
5935a0015d6SChris Zankel	 *       Y: number of registers saved in groups of 4
5945a0015d6SChris Zankel	 *       M: 4 bit mask of first 16 registers
5955a0015d6SChris Zankel	 */
5965a0015d6SChris Zankel
5975a0015d6SChris Zankel	mov	a2, a6
5985a0015d6SChris Zankel	mov	a3, a5
5995a0015d6SChris Zankel
6005a0015d6SChris Zankel2:	rotw	-1			# a0..a3 become a4..a7
6015a0015d6SChris Zankel	addi	a3, a7, -4*4		# next iteration
6025a0015d6SChris Zankel	addi	a2, a6, -16		# decrementing Y in WMASK
6035a0015d6SChris Zankel	l32i	a4, a3, PT_AREG_END + 0
6045a0015d6SChris Zankel	l32i	a5, a3, PT_AREG_END + 4
6055a0015d6SChris Zankel	l32i	a6, a3, PT_AREG_END + 8
6065a0015d6SChris Zankel	l32i	a7, a3, PT_AREG_END + 12
6075a0015d6SChris Zankel	_bgeui	a2, 16, 2b
6085a0015d6SChris Zankel
6095a0015d6SChris Zankel	/* Clear unrestored registers (don't leak anything to user-land */
6105a0015d6SChris Zankel
611bc5378fcSMax Filippov1:	rsr	a0, windowbase
612bc5378fcSMax Filippov	rsr	a3, sar
6135a0015d6SChris Zankel	sub	a3, a0, a3
6145a0015d6SChris Zankel	beqz	a3, 2f
6155a0015d6SChris Zankel	extui	a3, a3, 0, WBBITS
6165a0015d6SChris Zankel
6175a0015d6SChris Zankel1:	rotw	-1
6185a0015d6SChris Zankel	addi	a3, a7, -1
6195a0015d6SChris Zankel	movi	a4, 0
6205a0015d6SChris Zankel	movi	a5, 0
6215a0015d6SChris Zankel	movi	a6, 0
6225a0015d6SChris Zankel	movi	a7, 0
6235a0015d6SChris Zankel	bgei	a3, 1, 1b
6245a0015d6SChris Zankel
6255a0015d6SChris Zankel	/* We are back were we were when we started.
6265a0015d6SChris Zankel	 * Note: a2 still contains WMASK (if we've returned to the original
6275a0015d6SChris Zankel	 *	 frame where we had loaded a2), or at least the lower 4 bits
6285a0015d6SChris Zankel	 *	 (if we have restored WSBITS-1 frames).
6295a0015d6SChris Zankel	 */
6305a0015d6SChris Zankel
6314229fb12SMax Filippov2:
632c50842dfSChris Zankel#if XCHAL_HAVE_THREADPTR
633c50842dfSChris Zankel	l32i	a3, a1, PT_THREADPTR
634c50842dfSChris Zankel	wur	a3, threadptr
635c50842dfSChris Zankel#endif
636c50842dfSChris Zankel
6374229fb12SMax Filippov	j	common_exception_exit
6385a0015d6SChris Zankel
6395a0015d6SChris Zankel	/* This is the kernel exception exit.
6405a0015d6SChris Zankel	 * We avoided to do a MOVSP when we entered the exception, but we
6415a0015d6SChris Zankel	 * have to do it here.
6425a0015d6SChris Zankel	 */
6435a0015d6SChris Zankel
6445a0015d6SChris Zankelkernel_exception_exit:
6455a0015d6SChris Zankel
6465a0015d6SChris Zankel	/* Check if we have to do a movsp.
6475a0015d6SChris Zankel	 *
6485a0015d6SChris Zankel	 * We only have to do a movsp if the previous window-frame has
6495a0015d6SChris Zankel	 * been spilled to the *temporary* exception stack instead of the
6505a0015d6SChris Zankel	 * task's stack. This is the case if the corresponding bit in
6515a0015d6SChris Zankel	 * WINDOWSTART for the previous window-frame was set before
6525a0015d6SChris Zankel	 * (not spilled) but is zero now (spilled).
6535a0015d6SChris Zankel	 * If this bit is zero, all other bits except the one for the
6545a0015d6SChris Zankel	 * current window frame are also zero. So, we can use a simple test:
6555a0015d6SChris Zankel	 * 'and' WINDOWSTART and WINDOWSTART-1:
6565a0015d6SChris Zankel	 *
6575a0015d6SChris Zankel	 *  (XXXXXX1[0]* - 1) AND XXXXXX1[0]* = XXXXXX0[0]*
6585a0015d6SChris Zankel	 *
6595a0015d6SChris Zankel	 * The result is zero only if one bit was set.
6605a0015d6SChris Zankel	 *
6615a0015d6SChris Zankel	 * (Note: We might have gone through several task switches before
6625a0015d6SChris Zankel	 *        we come back to the current task, so WINDOWBASE might be
6635a0015d6SChris Zankel	 *        different from the time the exception occurred.)
6645a0015d6SChris Zankel	 */
6655a0015d6SChris Zankel
6665a0015d6SChris Zankel	/* Test WINDOWSTART before and after the exception.
6675a0015d6SChris Zankel	 * We actually have WMASK, so we only have to test if it is 1 or not.
6685a0015d6SChris Zankel	 */
6695a0015d6SChris Zankel
6705a0015d6SChris Zankel	l32i	a2, a1, PT_WMASK
6715a0015d6SChris Zankel	_beqi	a2, 1, common_exception_exit	# Spilled before exception,jump
6725a0015d6SChris Zankel
6735a0015d6SChris Zankel	/* Test WINDOWSTART now. If spilled, do the movsp */
6745a0015d6SChris Zankel
675bc5378fcSMax Filippov	rsr     a3, windowstart
6765a0015d6SChris Zankel	addi	a0, a3, -1
6775a0015d6SChris Zankel	and     a3, a3, a0
6785a0015d6SChris Zankel	_bnez	a3, common_exception_exit
6795a0015d6SChris Zankel
6805a0015d6SChris Zankel	/* Do a movsp (we returned from a call4, so we have at least a0..a7) */
6815a0015d6SChris Zankel
6825a0015d6SChris Zankel	addi    a0, a1, -16
6835a0015d6SChris Zankel	l32i    a3, a0, 0
6845a0015d6SChris Zankel	l32i    a4, a0, 4
6855a0015d6SChris Zankel	s32i    a3, a1, PT_SIZE+0
6865a0015d6SChris Zankel	s32i    a4, a1, PT_SIZE+4
6875a0015d6SChris Zankel	l32i    a3, a0, 8
6885a0015d6SChris Zankel	l32i    a4, a0, 12
6895a0015d6SChris Zankel	s32i    a3, a1, PT_SIZE+8
6905a0015d6SChris Zankel	s32i    a4, a1, PT_SIZE+12
6915a0015d6SChris Zankel
6925a0015d6SChris Zankel	/* Common exception exit.
6935a0015d6SChris Zankel	 * We restore the special register and the current window frame, and
6945a0015d6SChris Zankel	 * return from the exception.
6955a0015d6SChris Zankel	 *
6965a0015d6SChris Zankel	 * Note: We expect a2 to hold PT_WMASK
6975a0015d6SChris Zankel	 */
6985a0015d6SChris Zankel
6995a0015d6SChris Zankelcommon_exception_exit:
7005a0015d6SChris Zankel
701c658eac6SChris Zankel	/* Restore address registers. */
702c658eac6SChris Zankel
7035a0015d6SChris Zankel	_bbsi.l	a2, 1, 1f
7045a0015d6SChris Zankel	l32i	a4,  a1, PT_AREG4
7055a0015d6SChris Zankel	l32i	a5,  a1, PT_AREG5
7065a0015d6SChris Zankel	l32i	a6,  a1, PT_AREG6
7075a0015d6SChris Zankel	l32i	a7,  a1, PT_AREG7
7085a0015d6SChris Zankel	_bbsi.l	a2, 2, 1f
7095a0015d6SChris Zankel	l32i	a8,  a1, PT_AREG8
7105a0015d6SChris Zankel	l32i	a9,  a1, PT_AREG9
7115a0015d6SChris Zankel	l32i	a10, a1, PT_AREG10
7125a0015d6SChris Zankel	l32i	a11, a1, PT_AREG11
7135a0015d6SChris Zankel	_bbsi.l	a2, 3, 1f
7145a0015d6SChris Zankel	l32i	a12, a1, PT_AREG12
7155a0015d6SChris Zankel	l32i	a13, a1, PT_AREG13
7165a0015d6SChris Zankel	l32i	a14, a1, PT_AREG14
7175a0015d6SChris Zankel	l32i	a15, a1, PT_AREG15
7185a0015d6SChris Zankel
7195a0015d6SChris Zankel	/* Restore PC, SAR */
7205a0015d6SChris Zankel
7215a0015d6SChris Zankel1:	l32i	a2, a1, PT_PC
7225a0015d6SChris Zankel	l32i	a3, a1, PT_SAR
723bc5378fcSMax Filippov	wsr	a2, epc1
724bc5378fcSMax Filippov	wsr	a3, sar
7255a0015d6SChris Zankel
7265a0015d6SChris Zankel	/* Restore LBEG, LEND, LCOUNT */
7275a0015d6SChris Zankel
7285a0015d6SChris Zankel	l32i	a2, a1, PT_LBEG
7295a0015d6SChris Zankel	l32i	a3, a1, PT_LEND
730bc5378fcSMax Filippov	wsr	a2, lbeg
7315a0015d6SChris Zankel	l32i	a2, a1, PT_LCOUNT
732bc5378fcSMax Filippov	wsr	a3, lend
733bc5378fcSMax Filippov	wsr	a2, lcount
7345a0015d6SChris Zankel
73529c4dfd9SChris Zankel	/* We control single stepping through the ICOUNTLEVEL register. */
73629c4dfd9SChris Zankel
73729c4dfd9SChris Zankel	l32i	a2, a1, PT_ICOUNTLEVEL
73829c4dfd9SChris Zankel	movi	a3, -2
739bc5378fcSMax Filippov	wsr	a2, icountlevel
740bc5378fcSMax Filippov	wsr	a3, icount
74129c4dfd9SChris Zankel
7425a0015d6SChris Zankel	/* Check if it was double exception. */
7435a0015d6SChris Zankel
7445a0015d6SChris Zankel	l32i	a0, a1, PT_DEPC
7455a0015d6SChris Zankel	l32i	a3, a1, PT_AREG3
7462d1c645cSMarc Gauthier	l32i	a2, a1, PT_AREG2
747895666a9SMax Filippov	_bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
7482d1c645cSMarc Gauthier
7495a0015d6SChris Zankel	/* Restore a0...a3 and return */
7505a0015d6SChris Zankel
7515a0015d6SChris Zankel	l32i	a0, a1, PT_AREG0
7525a0015d6SChris Zankel	l32i	a1, a1, PT_AREG1
7535a0015d6SChris Zankel	rfe
7545a0015d6SChris Zankel
755895666a9SMax Filippov1: 	wsr	a0, depc
756895666a9SMax Filippov	l32i	a0, a1, PT_AREG0
757895666a9SMax Filippov	l32i	a1, a1, PT_AREG1
758895666a9SMax Filippov	rfde
7595a0015d6SChris Zankel
760d1538c46SChris ZankelENDPROC(kernel_exception)
761d1538c46SChris Zankel
7625a0015d6SChris Zankel/*
7635a0015d6SChris Zankel * Debug exception handler.
7645a0015d6SChris Zankel *
7655a0015d6SChris Zankel * Currently, we don't support KGDB, so only user application can be debugged.
7665a0015d6SChris Zankel *
7675a0015d6SChris Zankel * When we get here,  a0 is trashed and saved to excsave[debuglevel]
7685a0015d6SChris Zankel */
7695a0015d6SChris Zankel
7705a0015d6SChris ZankelENTRY(debug_exception)
7715a0015d6SChris Zankel
772bc5378fcSMax Filippov	rsr	a0, SREG_EPS + XCHAL_DEBUGLEVEL
773173d6681SChris Zankel	bbsi.l	a0, PS_EXCM_BIT, 1f	# exception mode
7745a0015d6SChris Zankel
775bc5378fcSMax Filippov	/* Set EPC1 and EXCCAUSE */
7765a0015d6SChris Zankel
777bc5378fcSMax Filippov	wsr	a2, depc		# save a2 temporarily
778bc5378fcSMax Filippov	rsr	a2, SREG_EPC + XCHAL_DEBUGLEVEL
779bc5378fcSMax Filippov	wsr	a2, epc1
7805a0015d6SChris Zankel
7815a0015d6SChris Zankel	movi	a2, EXCCAUSE_MAPPED_DEBUG
782bc5378fcSMax Filippov	wsr	a2, exccause
7835a0015d6SChris Zankel
7845a0015d6SChris Zankel	/* Restore PS to the value before the debug exc but with PS.EXCM set.*/
7855a0015d6SChris Zankel
786173d6681SChris Zankel	movi	a2, 1 << PS_EXCM_BIT
7875a0015d6SChris Zankel	or	a2, a0, a2
7885a0015d6SChris Zankel	movi	a0, debug_exception	# restore a3, debug jump vector
789bc5378fcSMax Filippov	wsr	a2, ps
790bc5378fcSMax Filippov	xsr	a0, SREG_EXCSAVE + XCHAL_DEBUGLEVEL
7915a0015d6SChris Zankel
7925a0015d6SChris Zankel	/* Switch to kernel/user stack, restore jump vector, and save a0 */
7935a0015d6SChris Zankel
794173d6681SChris Zankel	bbsi.l	a2, PS_UM_BIT, 2f	# jump if user mode
7955a0015d6SChris Zankel
7965a0015d6SChris Zankel	addi	a2, a1, -16-PT_SIZE	# assume kernel stack
7975a0015d6SChris Zankel	s32i	a0, a2, PT_AREG0
7985a0015d6SChris Zankel	movi	a0, 0
7995a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
8005a0015d6SChris Zankel	s32i	a0, a2, PT_DEPC		# mark it as a regular exception
801bc5378fcSMax Filippov	xsr	a0, depc
8025a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
8035a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2
8045a0015d6SChris Zankel	mov	a1, a2
8055a0015d6SChris Zankel	j	_kernel_exception
8065a0015d6SChris Zankel
807bc5378fcSMax Filippov2:	rsr	a2, excsave1
8085a0015d6SChris Zankel	l32i	a2, a2, EXC_TABLE_KSTK	# load kernel stack pointer
8095a0015d6SChris Zankel	s32i	a0, a2, PT_AREG0
8105a0015d6SChris Zankel	movi	a0, 0
8115a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
8125a0015d6SChris Zankel	s32i	a0, a2, PT_DEPC
813bc5378fcSMax Filippov	xsr	a0, depc
8145a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
8155a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2
8165a0015d6SChris Zankel	mov	a1, a2
8175a0015d6SChris Zankel	j	_user_exception
8185a0015d6SChris Zankel
8195a0015d6SChris Zankel	/* Debug exception while in exception mode. */
8205a0015d6SChris Zankel1:	j	1b	// FIXME!!
8215a0015d6SChris Zankel
822d1538c46SChris ZankelENDPROC(debug_exception)
8235a0015d6SChris Zankel
8245a0015d6SChris Zankel/*
8255a0015d6SChris Zankel * We get here in case of an unrecoverable exception.
8265a0015d6SChris Zankel * The only thing we can do is to be nice and print a panic message.
8275a0015d6SChris Zankel * We only produce a single stack frame for panic, so ???
8285a0015d6SChris Zankel *
8295a0015d6SChris Zankel *
8305a0015d6SChris Zankel * Entry conditions:
8315a0015d6SChris Zankel *
8325a0015d6SChris Zankel *   - a0 contains the caller address; original value saved in excsave1.
8335a0015d6SChris Zankel *   - the original a0 contains a valid return address (backtrace) or 0.
8345a0015d6SChris Zankel *   - a2 contains a valid stackpointer
8355a0015d6SChris Zankel *
8365a0015d6SChris Zankel * Notes:
8375a0015d6SChris Zankel *
8385a0015d6SChris Zankel *   - If the stack pointer could be invalid, the caller has to setup a
8395a0015d6SChris Zankel *     dummy stack pointer (e.g. the stack of the init_task)
8405a0015d6SChris Zankel *
8415a0015d6SChris Zankel *   - If the return address could be invalid, the caller has to set it
8425a0015d6SChris Zankel *     to 0, so the backtrace would stop.
8435a0015d6SChris Zankel *
8445a0015d6SChris Zankel */
8455a0015d6SChris Zankel	.align 4
8465a0015d6SChris Zankelunrecoverable_text:
8475a0015d6SChris Zankel	.ascii "Unrecoverable error in exception handler\0"
8485a0015d6SChris Zankel
8495a0015d6SChris ZankelENTRY(unrecoverable_exception)
8505a0015d6SChris Zankel
8515a0015d6SChris Zankel	movi	a0, 1
8525a0015d6SChris Zankel	movi	a1, 0
8535a0015d6SChris Zankel
854bc5378fcSMax Filippov	wsr	a0, windowstart
855bc5378fcSMax Filippov	wsr	a1, windowbase
8565a0015d6SChris Zankel	rsync
8575a0015d6SChris Zankel
8582d1c645cSMarc Gauthier	movi	a1, (1 << PS_WOE_BIT) | LOCKLEVEL
859bc5378fcSMax Filippov	wsr	a1, ps
8605a0015d6SChris Zankel	rsync
8615a0015d6SChris Zankel
8625a0015d6SChris Zankel	movi	a1, init_task
8635a0015d6SChris Zankel	movi	a0, 0
8645a0015d6SChris Zankel	addi	a1, a1, PT_REGS_OFFSET
8655a0015d6SChris Zankel
8665a0015d6SChris Zankel	movi	a4, panic
8675a0015d6SChris Zankel	movi	a6, unrecoverable_text
8685a0015d6SChris Zankel
8695a0015d6SChris Zankel	callx4	a4
8705a0015d6SChris Zankel
8715a0015d6SChris Zankel1:	j	1b
8725a0015d6SChris Zankel
873d1538c46SChris ZankelENDPROC(unrecoverable_exception)
8745a0015d6SChris Zankel
8755a0015d6SChris Zankel/* -------------------------- FAST EXCEPTION HANDLERS ----------------------- */
8765a0015d6SChris Zankel
8775a0015d6SChris Zankel/*
8785a0015d6SChris Zankel * Fast-handler for alloca exceptions
8795a0015d6SChris Zankel *
8805a0015d6SChris Zankel *  The ALLOCA handler is entered when user code executes the MOVSP
8815a0015d6SChris Zankel *  instruction and the caller's frame is not in the register file.
8825a0015d6SChris Zankel *
883fff96d69SMax Filippov * This algorithm was taken from the Ross Morley's RTOS Porting Layer:
884fff96d69SMax Filippov *
885fff96d69SMax Filippov *    /home/ross/rtos/porting/XtensaRTOS-PortingLayer-20090507/xtensa_vectors.S
886fff96d69SMax Filippov *
887fff96d69SMax Filippov * It leverages the existing window spill/fill routines and their support for
888fff96d69SMax Filippov * double exceptions. The 'movsp' instruction will only cause an exception if
889fff96d69SMax Filippov * the next window needs to be loaded. In fact this ALLOCA exception may be
890fff96d69SMax Filippov * replaced at some point by changing the hardware to do a underflow exception
891fff96d69SMax Filippov * of the proper size instead.
892fff96d69SMax Filippov *
893fff96d69SMax Filippov * This algorithm simply backs out the register changes started by the user
894fff96d69SMax Filippov * excpetion handler, makes it appear that we have started a window underflow
895fff96d69SMax Filippov * by rotating the window back and then setting the old window base (OWB) in
896fff96d69SMax Filippov * the 'ps' register with the rolled back window base. The 'movsp' instruction
897fff96d69SMax Filippov * will be re-executed and this time since the next window frames is in the
898fff96d69SMax Filippov * active AR registers it won't cause an exception.
899fff96d69SMax Filippov *
900fff96d69SMax Filippov * If the WindowUnderflow code gets a TLB miss the page will get mapped
901fff96d69SMax Filippov * the the partial windeowUnderflow will be handeled in the double exception
902fff96d69SMax Filippov * handler.
9035a0015d6SChris Zankel *
9045a0015d6SChris Zankel * Entry condition:
9055a0015d6SChris Zankel *
9065a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
9075a0015d6SChris Zankel *   a1:	a1
9085a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
90999d5040eSMax Filippov *   a3:	a3
9105a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
91199d5040eSMax Filippov *   excsave_1:	dispatch table
9125a0015d6SChris Zankel *
9135a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
9145a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
9155a0015d6SChris Zankel */
9165a0015d6SChris Zankel
9175a0015d6SChris ZankelENTRY(fast_alloca)
918fff96d69SMax Filippov	rsr	a0, windowbase
919fff96d69SMax Filippov	rotw	-1
920fff96d69SMax Filippov	rsr	a2, ps
921fff96d69SMax Filippov	extui	a3, a2, PS_OWB_SHIFT, PS_OWB_WIDTH
922fff96d69SMax Filippov	xor	a3, a3, a4
923fff96d69SMax Filippov	l32i	a4, a6, PT_AREG0
924fff96d69SMax Filippov	l32i	a1, a6, PT_DEPC
925fff96d69SMax Filippov	rsr	a6, depc
926fff96d69SMax Filippov	wsr	a1, depc
927fff96d69SMax Filippov	slli	a3, a3, PS_OWB_SHIFT
928fff96d69SMax Filippov	xor	a2, a2, a3
929fff96d69SMax Filippov	wsr	a2, ps
930fff96d69SMax Filippov	rsync
9315a0015d6SChris Zankel
932fff96d69SMax Filippov	_bbci.l	a4, 31, 4f
933fff96d69SMax Filippov	rotw	-1
934fff96d69SMax Filippov	_bbci.l	a8, 30, 8f
935fff96d69SMax Filippov	rotw	-1
936fff96d69SMax Filippov	j	_WindowUnderflow12
937fff96d69SMax Filippov8:	j	_WindowUnderflow8
938fff96d69SMax Filippov4:	j	_WindowUnderflow4
939d1538c46SChris ZankelENDPROC(fast_alloca)
9405a0015d6SChris Zankel
9415a0015d6SChris Zankel/*
9425a0015d6SChris Zankel * fast system calls.
9435a0015d6SChris Zankel *
9445a0015d6SChris Zankel * WARNING:  The kernel doesn't save the entire user context before
9455a0015d6SChris Zankel * handling a fast system call.  These functions are small and short,
9465a0015d6SChris Zankel * usually offering some functionality not available to user tasks.
9475a0015d6SChris Zankel *
9485a0015d6SChris Zankel * BE CAREFUL TO PRESERVE THE USER'S CONTEXT.
9495a0015d6SChris Zankel *
9505a0015d6SChris Zankel * Entry condition:
9515a0015d6SChris Zankel *
9525a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
9535a0015d6SChris Zankel *   a1:	a1
9545a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
95599d5040eSMax Filippov *   a3:	a3
9565a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
95799d5040eSMax Filippov *   excsave_1:	dispatch table
9585a0015d6SChris Zankel */
9595a0015d6SChris Zankel
9605a0015d6SChris ZankelENTRY(fast_syscall_kernel)
9615a0015d6SChris Zankel
9625a0015d6SChris Zankel	/* Skip syscall. */
9635a0015d6SChris Zankel
964bc5378fcSMax Filippov	rsr	a0, epc1
9655a0015d6SChris Zankel	addi	a0, a0, 3
966bc5378fcSMax Filippov	wsr	a0, epc1
9675a0015d6SChris Zankel
9685a0015d6SChris Zankel	l32i	a0, a2, PT_DEPC
9695a0015d6SChris Zankel	bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, fast_syscall_unrecoverable
9705a0015d6SChris Zankel
971bc5378fcSMax Filippov	rsr	a0, depc			# get syscall-nr
9725a0015d6SChris Zankel	_beqz	a0, fast_syscall_spill_registers
973fc4fb2adSChris Zankel	_beqi	a0, __NR_xtensa, fast_syscall_xtensa
9745a0015d6SChris Zankel
9755a0015d6SChris Zankel	j	kernel_exception
9765a0015d6SChris Zankel
977d1538c46SChris ZankelENDPROC(fast_syscall_kernel)
978d1538c46SChris Zankel
9795a0015d6SChris ZankelENTRY(fast_syscall_user)
9805a0015d6SChris Zankel
9815a0015d6SChris Zankel	/* Skip syscall. */
9825a0015d6SChris Zankel
983bc5378fcSMax Filippov	rsr	a0, epc1
9845a0015d6SChris Zankel	addi	a0, a0, 3
985bc5378fcSMax Filippov	wsr	a0, epc1
9865a0015d6SChris Zankel
9875a0015d6SChris Zankel	l32i	a0, a2, PT_DEPC
9885a0015d6SChris Zankel	bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, fast_syscall_unrecoverable
9895a0015d6SChris Zankel
990bc5378fcSMax Filippov	rsr	a0, depc			# get syscall-nr
9915a0015d6SChris Zankel	_beqz	a0, fast_syscall_spill_registers
992fc4fb2adSChris Zankel	_beqi	a0, __NR_xtensa, fast_syscall_xtensa
9935a0015d6SChris Zankel
9945a0015d6SChris Zankel	j	user_exception
9955a0015d6SChris Zankel
996d1538c46SChris ZankelENDPROC(fast_syscall_user)
997d1538c46SChris Zankel
9985a0015d6SChris ZankelENTRY(fast_syscall_unrecoverable)
9995a0015d6SChris Zankel
10005a0015d6SChris Zankel	/* Restore all states. */
10015a0015d6SChris Zankel
10025a0015d6SChris Zankel	l32i    a0, a2, PT_AREG0        # restore a0
1003bc5378fcSMax Filippov	xsr     a2, depc                # restore a2, depc
10045a0015d6SChris Zankel
1005bc5378fcSMax Filippov	wsr     a0, excsave1
10065a0015d6SChris Zankel	movi    a0, unrecoverable_exception
10075a0015d6SChris Zankel	callx0  a0
10085a0015d6SChris Zankel
1009d1538c46SChris ZankelENDPROC(fast_syscall_unrecoverable)
10105a0015d6SChris Zankel
10115a0015d6SChris Zankel/*
10125a0015d6SChris Zankel * sysxtensa syscall handler
10135a0015d6SChris Zankel *
1014fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_SET,     ptr, val,    unused);
1015fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_ADD,     ptr, val,    unused);
1016fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_EXG_ADD, ptr, val,    unused);
1017fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_CMP_SWP, ptr, oldval, newval);
10185a0015d6SChris Zankel *        a2            a6                   a3    a4      a5
10195a0015d6SChris Zankel *
10205a0015d6SChris Zankel * Entry condition:
10215a0015d6SChris Zankel *
1022fc4fb2adSChris Zankel *   a0:	a2 (syscall-nr), original value saved on stack (PT_AREG0)
10235a0015d6SChris Zankel *   a1:	a1
1024fc4fb2adSChris Zankel *   a2:	new stack pointer, original in a0 and DEPC
102599d5040eSMax Filippov *   a3:	a3
1026fc4fb2adSChris Zankel *   a4..a15:	unchanged
10275a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
102899d5040eSMax Filippov *   excsave_1:	dispatch table
10295a0015d6SChris Zankel *
10305a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
10315a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
10325a0015d6SChris Zankel *
10335a0015d6SChris Zankel * Note: we don't have to save a2; a2 holds the return value
10345a0015d6SChris Zankel *
10355a0015d6SChris Zankel * We use the two macros TRY and CATCH:
10365a0015d6SChris Zankel *
10375a0015d6SChris Zankel * TRY	 adds an entry to the __ex_table fixup table for the immediately
10385a0015d6SChris Zankel *	 following instruction.
10395a0015d6SChris Zankel *
104025985edcSLucas De Marchi * CATCH catches any exception that occurred at one of the preceding TRY
10415a0015d6SChris Zankel *       statements and continues from there
10425a0015d6SChris Zankel *
10435a0015d6SChris Zankel * Usage TRY	l32i	a0, a1, 0
10445a0015d6SChris Zankel *		<other code>
10455a0015d6SChris Zankel *	 done:	rfe
10465a0015d6SChris Zankel *	 CATCH	<set return code>
10475a0015d6SChris Zankel *		j done
10485a0015d6SChris Zankel */
10495a0015d6SChris Zankel
10509184289cSMax Filippov#ifdef CONFIG_FAST_SYSCALL_XTENSA
10519184289cSMax Filippov
10525a0015d6SChris Zankel#define TRY								\
10535a0015d6SChris Zankel	.section __ex_table, "a";					\
10545a0015d6SChris Zankel	.word	66f, 67f;						\
10555a0015d6SChris Zankel	.text;								\
10565a0015d6SChris Zankel66:
10575a0015d6SChris Zankel
10585a0015d6SChris Zankel#define CATCH								\
10595a0015d6SChris Zankel67:
10605a0015d6SChris Zankel
1061fc4fb2adSChris ZankelENTRY(fast_syscall_xtensa)
10625a0015d6SChris Zankel
1063fc4fb2adSChris Zankel	s32i	a7, a2, PT_AREG7	# we need an additional register
10645a0015d6SChris Zankel	movi	a7, 4			# sizeof(unsigned int)
1065fc4fb2adSChris Zankel	access_ok a3, a7, a0, a2, .Leac	# a0: scratch reg, a2: sp
10665a0015d6SChris Zankel
1067d1b6ba82SMax Filippov	_bgeui	a6, SYS_XTENSA_COUNT, .Lill
1068d1b6ba82SMax Filippov	_bnei	a6, SYS_XTENSA_ATOMIC_CMP_SWP, .Lnswp
10695a0015d6SChris Zankel
1070fc4fb2adSChris Zankel	/* Fall through for ATOMIC_CMP_SWP. */
10715a0015d6SChris Zankel
10725a0015d6SChris Zankel.Lswp:	/* Atomic compare and swap */
10735a0015d6SChris Zankel
1074fc4fb2adSChris ZankelTRY	l32i	a0, a3, 0		# read old value
1075fc4fb2adSChris Zankel	bne	a0, a4, 1f		# same as old value? jump
1076fc4fb2adSChris ZankelTRY	s32i	a5, a3, 0		# different, modify value
1077fc4fb2adSChris Zankel	l32i	a7, a2, PT_AREG7	# restore a7
1078fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1079fc4fb2adSChris Zankel	movi	a2, 1			# and return 1
1080fc4fb2adSChris Zankel	rfe
10815a0015d6SChris Zankel
1082fc4fb2adSChris Zankel1:	l32i	a7, a2, PT_AREG7	# restore a7
1083fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1084fc4fb2adSChris Zankel	movi	a2, 0			# return 0 (note that we cannot set
1085fc4fb2adSChris Zankel	rfe
10865a0015d6SChris Zankel
1087fc4fb2adSChris Zankel.Lnswp:	/* Atomic set, add, and exg_add. */
10885a0015d6SChris Zankel
1089fc4fb2adSChris ZankelTRY	l32i	a7, a3, 0		# orig
1090d1b6ba82SMax Filippov	addi	a6, a6, -SYS_XTENSA_ATOMIC_SET
1091fc4fb2adSChris Zankel	add	a0, a4, a7		# + arg
1092fc4fb2adSChris Zankel	moveqz	a0, a4, a6		# set
1093d1b6ba82SMax Filippov	addi	a6, a6, SYS_XTENSA_ATOMIC_SET
1094fc4fb2adSChris ZankelTRY	s32i	a0, a3, 0		# write new value
10955a0015d6SChris Zankel
1096fc4fb2adSChris Zankel	mov	a0, a2
10975a0015d6SChris Zankel	mov	a2, a7
1098fc4fb2adSChris Zankel	l32i	a7, a0, PT_AREG7	# restore a7
1099fc4fb2adSChris Zankel	l32i	a0, a0, PT_AREG0	# restore a0
11005a0015d6SChris Zankel	rfe
11015a0015d6SChris Zankel
11025a0015d6SChris ZankelCATCH
1103fc4fb2adSChris Zankel.Leac:	l32i	a7, a2, PT_AREG7	# restore a7
1104fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1105fc4fb2adSChris Zankel	movi	a2, -EFAULT
1106fc4fb2adSChris Zankel	rfe
1107fc4fb2adSChris Zankel
1108d1b6ba82SMax Filippov.Lill:	l32i	a7, a2, PT_AREG7	# restore a7
1109fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1110fc4fb2adSChris Zankel	movi	a2, -EINVAL
1111fc4fb2adSChris Zankel	rfe
1112fc4fb2adSChris Zankel
1113d1538c46SChris ZankelENDPROC(fast_syscall_xtensa)
11145a0015d6SChris Zankel
11159184289cSMax Filippov#else /* CONFIG_FAST_SYSCALL_XTENSA */
11169184289cSMax Filippov
11179184289cSMax FilippovENTRY(fast_syscall_xtensa)
11189184289cSMax Filippov
11199184289cSMax Filippov	l32i    a0, a2, PT_AREG0        # restore a0
11209184289cSMax Filippov	movi	a2, -ENOSYS
11219184289cSMax Filippov	rfe
11229184289cSMax Filippov
11239184289cSMax FilippovENDPROC(fast_syscall_xtensa)
11249184289cSMax Filippov
11259184289cSMax Filippov#endif /* CONFIG_FAST_SYSCALL_XTENSA */
11269184289cSMax Filippov
11275a0015d6SChris Zankel
11285a0015d6SChris Zankel/* fast_syscall_spill_registers.
11295a0015d6SChris Zankel *
11305a0015d6SChris Zankel * Entry condition:
11315a0015d6SChris Zankel *
11325a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
11335a0015d6SChris Zankel *   a1:	a1
11345a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
113599d5040eSMax Filippov *   a3:	a3
11365a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
113799d5040eSMax Filippov *   excsave_1:	dispatch table
11385a0015d6SChris Zankel *
11395a0015d6SChris Zankel * Note: We assume the stack pointer is EXC_TABLE_KSTK in the fixup handler.
11405a0015d6SChris Zankel */
11415a0015d6SChris Zankel
11429184289cSMax Filippov#ifdef CONFIG_FAST_SYSCALL_SPILL_REGISTERS
11439184289cSMax Filippov
11445a0015d6SChris ZankelENTRY(fast_syscall_spill_registers)
11455a0015d6SChris Zankel
11465a0015d6SChris Zankel	/* Register a FIXUP handler (pass current wb as a parameter) */
11475a0015d6SChris Zankel
114899d5040eSMax Filippov	xsr	a3, excsave1
11495a0015d6SChris Zankel	movi	a0, fast_syscall_spill_registers_fixup
11505a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_FIXUP
1151bc5378fcSMax Filippov	rsr	a0, windowbase
11525a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_PARAM
115399d5040eSMax Filippov	xsr	a3, excsave1		# restore a3 and excsave_1
11545a0015d6SChris Zankel
115599d5040eSMax Filippov	/* Save a3, a4 and SAR on stack. */
11565a0015d6SChris Zankel
1157bc5378fcSMax Filippov	rsr	a0, sar
11585a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
11596b5a1f74SChris Zankel	s32i	a0, a2, PT_SAR
11606b5a1f74SChris Zankel
11616b5a1f74SChris Zankel	/* The spill routine might clobber a4, a7, a8, a11, a12, and a15. */
11626b5a1f74SChris Zankel
1163c658eac6SChris Zankel	s32i	a4, a2, PT_AREG4
1164c658eac6SChris Zankel	s32i	a7, a2, PT_AREG7
11656b5a1f74SChris Zankel	s32i	a8, a2, PT_AREG8
1166c658eac6SChris Zankel	s32i	a11, a2, PT_AREG11
11676b5a1f74SChris Zankel	s32i	a12, a2, PT_AREG12
1168c658eac6SChris Zankel	s32i	a15, a2, PT_AREG15
11695a0015d6SChris Zankel
11706b5a1f74SChris Zankel	/*
11716b5a1f74SChris Zankel	 * Rotate ws so that the current windowbase is at bit 0.
11726b5a1f74SChris Zankel	 * Assume ws = xxxwww1yy (www1 current window frame).
11736b5a1f74SChris Zankel	 * Rotate ws right so that a4 = yyxxxwww1.
11746b5a1f74SChris Zankel	 */
11756b5a1f74SChris Zankel
11766b5a1f74SChris Zankel	rsr	a0, windowbase
11776b5a1f74SChris Zankel	rsr	a3, windowstart		# a3 = xxxwww1yy
11786b5a1f74SChris Zankel	ssr	a0			# holds WB
11796b5a1f74SChris Zankel	slli	a0, a3, WSBITS
11806b5a1f74SChris Zankel	or	a3, a3, a0		# a3 = xxxwww1yyxxxwww1yy
11816b5a1f74SChris Zankel	srl	a3, a3			# a3 = 00xxxwww1yyxxxwww1
11826b5a1f74SChris Zankel
11836b5a1f74SChris Zankel	/* We are done if there are no more than the current register frame. */
11846b5a1f74SChris Zankel
11856b5a1f74SChris Zankel	extui	a3, a3, 1, WSBITS-1	# a3 = 0yyxxxwww
11866b5a1f74SChris Zankel	movi	a0, (1 << (WSBITS-1))
11876b5a1f74SChris Zankel	_beqz	a3, .Lnospill		# only one active frame? jump
11886b5a1f74SChris Zankel
11896b5a1f74SChris Zankel	/* We want 1 at the top, so that we return to the current windowbase */
11906b5a1f74SChris Zankel
11916b5a1f74SChris Zankel	or	a3, a3, a0		# 1yyxxxwww
11926b5a1f74SChris Zankel
11936b5a1f74SChris Zankel	/* Skip empty frames - get 'oldest' WINDOWSTART-bit. */
11946b5a1f74SChris Zankel
11956b5a1f74SChris Zankel	wsr	a3, windowstart		# save shifted windowstart
11966b5a1f74SChris Zankel	neg	a0, a3
11976b5a1f74SChris Zankel	and	a3, a0, a3		# first bit set from right: 000010000
11986b5a1f74SChris Zankel
11996b5a1f74SChris Zankel	ffs_ws	a0, a3			# a0: shifts to skip empty frames
12006b5a1f74SChris Zankel	movi	a3, WSBITS
12016b5a1f74SChris Zankel	sub	a0, a3, a0		# WSBITS-a0:number of 0-bits from right
12026b5a1f74SChris Zankel	ssr	a0			# save in SAR for later.
12036b5a1f74SChris Zankel
12046b5a1f74SChris Zankel	rsr	a3, windowbase
12056b5a1f74SChris Zankel	add	a3, a3, a0
12066b5a1f74SChris Zankel	wsr	a3, windowbase
12076b5a1f74SChris Zankel	rsync
12086b5a1f74SChris Zankel
12096b5a1f74SChris Zankel	rsr	a3, windowstart
12106b5a1f74SChris Zankel	srl	a3, a3			# shift windowstart
12116b5a1f74SChris Zankel
12126b5a1f74SChris Zankel	/* WB is now just one frame below the oldest frame in the register
12136b5a1f74SChris Zankel	   window. WS is shifted so the oldest frame is in bit 0, thus, WB
12146b5a1f74SChris Zankel	   and WS differ by one 4-register frame. */
12156b5a1f74SChris Zankel
12166b5a1f74SChris Zankel	/* Save frames. Depending what call was used (call4, call8, call12),
12176b5a1f74SChris Zankel	 * we have to save 4,8. or 12 registers.
12186b5a1f74SChris Zankel	 */
12196b5a1f74SChris Zankel
12206b5a1f74SChris Zankel
12216b5a1f74SChris Zankel.Lloop: _bbsi.l	a3, 1, .Lc4
12226b5a1f74SChris Zankel	_bbci.l	a3, 2, .Lc12
12236b5a1f74SChris Zankel
12246b5a1f74SChris Zankel.Lc8:	s32e	a4, a13, -16
12256b5a1f74SChris Zankel	l32e	a4, a5, -12
12266b5a1f74SChris Zankel	s32e	a8, a4, -32
12276b5a1f74SChris Zankel	s32e	a5, a13, -12
12286b5a1f74SChris Zankel	s32e	a6, a13, -8
12296b5a1f74SChris Zankel	s32e	a7, a13, -4
12306b5a1f74SChris Zankel	s32e	a9, a4, -28
12316b5a1f74SChris Zankel	s32e	a10, a4, -24
12326b5a1f74SChris Zankel	s32e	a11, a4, -20
12336b5a1f74SChris Zankel	srli	a11, a3, 2		# shift windowbase by 2
12346b5a1f74SChris Zankel	rotw	2
12356b5a1f74SChris Zankel	_bnei	a3, 1, .Lloop
12366b5a1f74SChris Zankel	j	.Lexit
12376b5a1f74SChris Zankel
12386b5a1f74SChris Zankel.Lc4:	s32e	a4, a9, -16
12396b5a1f74SChris Zankel	s32e	a5, a9, -12
12406b5a1f74SChris Zankel	s32e	a6, a9, -8
12416b5a1f74SChris Zankel	s32e	a7, a9, -4
12426b5a1f74SChris Zankel
12436b5a1f74SChris Zankel	srli	a7, a3, 1
12446b5a1f74SChris Zankel	rotw	1
12456b5a1f74SChris Zankel	_bnei	a3, 1, .Lloop
12466b5a1f74SChris Zankel	j	.Lexit
12476b5a1f74SChris Zankel
12486b5a1f74SChris Zankel.Lc12:	_bbci.l	a3, 3, .Linvalid_mask	# bit 2 shouldn't be zero!
12496b5a1f74SChris Zankel
12506b5a1f74SChris Zankel	/* 12-register frame (call12) */
12516b5a1f74SChris Zankel
12526b5a1f74SChris Zankel	l32e	a0, a5, -12
12536b5a1f74SChris Zankel	s32e	a8, a0, -48
12546b5a1f74SChris Zankel	mov	a8, a0
12556b5a1f74SChris Zankel
12566b5a1f74SChris Zankel	s32e	a9, a8, -44
12576b5a1f74SChris Zankel	s32e	a10, a8, -40
12586b5a1f74SChris Zankel	s32e	a11, a8, -36
12596b5a1f74SChris Zankel	s32e	a12, a8, -32
12606b5a1f74SChris Zankel	s32e	a13, a8, -28
12616b5a1f74SChris Zankel	s32e	a14, a8, -24
12626b5a1f74SChris Zankel	s32e	a15, a8, -20
12636b5a1f74SChris Zankel	srli	a15, a3, 3
12646b5a1f74SChris Zankel
12656b5a1f74SChris Zankel	/* The stack pointer for a4..a7 is out of reach, so we rotate the
12666b5a1f74SChris Zankel	 * window, grab the stackpointer, and rotate back.
12676b5a1f74SChris Zankel	 * Alternatively, we could also use the following approach, but that
12686b5a1f74SChris Zankel	 * makes the fixup routine much more complicated:
12696b5a1f74SChris Zankel	 * rotw	1
12706b5a1f74SChris Zankel	 * s32e	a0, a13, -16
12716b5a1f74SChris Zankel	 * ...
12726b5a1f74SChris Zankel	 * rotw 2
12736b5a1f74SChris Zankel	 */
12746b5a1f74SChris Zankel
12756b5a1f74SChris Zankel	rotw	1
12766b5a1f74SChris Zankel	mov	a4, a13
12776b5a1f74SChris Zankel	rotw	-1
12786b5a1f74SChris Zankel
12796b5a1f74SChris Zankel	s32e	a4, a8, -16
12806b5a1f74SChris Zankel	s32e	a5, a8, -12
12816b5a1f74SChris Zankel	s32e	a6, a8, -8
12826b5a1f74SChris Zankel	s32e	a7, a8, -4
12836b5a1f74SChris Zankel
12846b5a1f74SChris Zankel	rotw	3
12856b5a1f74SChris Zankel
12866b5a1f74SChris Zankel	_beqi	a3, 1, .Lexit
12876b5a1f74SChris Zankel	j	.Lloop
12886b5a1f74SChris Zankel
12896b5a1f74SChris Zankel.Lexit:
12906b5a1f74SChris Zankel
12916b5a1f74SChris Zankel	/* Done. Do the final rotation and set WS */
12926b5a1f74SChris Zankel
12936b5a1f74SChris Zankel	rotw	1
12946b5a1f74SChris Zankel	rsr	a3, windowbase
12956b5a1f74SChris Zankel	ssl	a3
12966b5a1f74SChris Zankel	movi	a3, 1
12976b5a1f74SChris Zankel	sll	a3, a3
12986b5a1f74SChris Zankel	wsr	a3, windowstart
12996b5a1f74SChris Zankel.Lnospill:
13005a0015d6SChris Zankel
13015a0015d6SChris Zankel	/* Advance PC, restore registers and SAR, and return from exception. */
13025a0015d6SChris Zankel
13036b5a1f74SChris Zankel	l32i	a3, a2, PT_SAR
13045a0015d6SChris Zankel	l32i	a0, a2, PT_AREG0
1305bc5378fcSMax Filippov	wsr	a3, sar
13065a0015d6SChris Zankel	l32i	a3, a2, PT_AREG3
13075a0015d6SChris Zankel
13085a0015d6SChris Zankel	/* Restore clobbered registers. */
13095a0015d6SChris Zankel
13106b5a1f74SChris Zankel	l32i	a4, a2, PT_AREG4
1311c658eac6SChris Zankel	l32i	a7, a2, PT_AREG7
13126b5a1f74SChris Zankel	l32i	a8, a2, PT_AREG8
1313c658eac6SChris Zankel	l32i	a11, a2, PT_AREG11
13146b5a1f74SChris Zankel	l32i	a12, a2, PT_AREG12
1315c658eac6SChris Zankel	l32i	a15, a2, PT_AREG15
13165a0015d6SChris Zankel
13175a0015d6SChris Zankel	movi	a2, 0
13185a0015d6SChris Zankel	rfe
13195a0015d6SChris Zankel
13206b5a1f74SChris Zankel.Linvalid_mask:
13216b5a1f74SChris Zankel
13226b5a1f74SChris Zankel	/* We get here because of an unrecoverable error in the window
13236b5a1f74SChris Zankel	 * registers, so set up a dummy frame and kill the user application.
13246b5a1f74SChris Zankel	 * Note: We assume EXC_TABLE_KSTK contains a valid stack pointer.
13256b5a1f74SChris Zankel	 */
13266b5a1f74SChris Zankel
13276b5a1f74SChris Zankel	movi	a0, 1
13286b5a1f74SChris Zankel	movi	a1, 0
13296b5a1f74SChris Zankel
13306b5a1f74SChris Zankel	wsr	a0, windowstart
13316b5a1f74SChris Zankel	wsr	a1, windowbase
13326b5a1f74SChris Zankel	rsync
13336b5a1f74SChris Zankel
13346b5a1f74SChris Zankel	movi	a0, 0
13356b5a1f74SChris Zankel
13366b5a1f74SChris Zankel	rsr	a3, excsave1
13376b5a1f74SChris Zankel	l32i	a1, a3, EXC_TABLE_KSTK
13386b5a1f74SChris Zankel
13396b5a1f74SChris Zankel	movi	a4, (1 << PS_WOE_BIT) | LOCKLEVEL
13406b5a1f74SChris Zankel	wsr	a4, ps
13416b5a1f74SChris Zankel	rsync
13426b5a1f74SChris Zankel
13436b5a1f74SChris Zankel	movi	a6, SIGSEGV
13446b5a1f74SChris Zankel	movi	a4, do_exit
13456b5a1f74SChris Zankel	callx4	a4
13466b5a1f74SChris Zankel
13476b5a1f74SChris Zankel	/* shouldn't return, so panic */
13486b5a1f74SChris Zankel
13496b5a1f74SChris Zankel	wsr	a0, excsave1
13506b5a1f74SChris Zankel	movi	a0, unrecoverable_exception
13516b5a1f74SChris Zankel	callx0	a0		# should not return
13526b5a1f74SChris Zankel1:	j	1b
13536b5a1f74SChris Zankel
13546b5a1f74SChris Zankel
1355d1538c46SChris ZankelENDPROC(fast_syscall_spill_registers)
1356d1538c46SChris Zankel
13575a0015d6SChris Zankel/* Fixup handler.
13585a0015d6SChris Zankel *
13595a0015d6SChris Zankel * We get here if the spill routine causes an exception, e.g. tlb miss.
13605a0015d6SChris Zankel * We basically restore WINDOWBASE and WINDOWSTART to the condition when
13615a0015d6SChris Zankel * we entered the spill routine and jump to the user exception handler.
13625a0015d6SChris Zankel *
13633251f1e2SMax Filippov * Note that we only need to restore the bits in windowstart that have not
13643251f1e2SMax Filippov * been spilled yet by the _spill_register routine. Luckily, a3 contains a
13653251f1e2SMax Filippov * rotated windowstart with only those bits set for frames that haven't been
13663251f1e2SMax Filippov * spilled yet. Because a3 is rotated such that bit 0 represents the register
13673251f1e2SMax Filippov * frame for the current windowbase - 1, we need to rotate a3 left by the
13683251f1e2SMax Filippov * value of the current windowbase + 1 and move it to windowstart.
13693251f1e2SMax Filippov *
13705a0015d6SChris Zankel * a0: value of depc, original value in depc
13715a0015d6SChris Zankel * a2: trashed, original value in EXC_TABLE_DOUBLE_SAVE
13725a0015d6SChris Zankel * a3: exctable, original value in excsave1
13735a0015d6SChris Zankel */
13745a0015d6SChris Zankel
1375244066f4SMax FilippovENTRY(fast_syscall_spill_registers_fixup)
13765a0015d6SChris Zankel
1377bc5378fcSMax Filippov	rsr	a2, windowbase	# get current windowbase (a2 is saved)
1378bc5378fcSMax Filippov	xsr	a0, depc	# restore depc and a0
13795a0015d6SChris Zankel	ssl	a2		# set shift (32 - WB)
13805a0015d6SChris Zankel
13815a0015d6SChris Zankel	/* We need to make sure the current registers (a0-a3) are preserved.
13825a0015d6SChris Zankel	 * To do this, we simply set the bit for the current window frame
13835a0015d6SChris Zankel	 * in WS, so that the exception handlers save them to the task stack.
13843251f1e2SMax Filippov	 *
13853251f1e2SMax Filippov	 * Note: we use a3 to set the windowbase, so we take a special care
13863251f1e2SMax Filippov	 * of it, saving it in the original _spill_registers frame across
13873251f1e2SMax Filippov	 * the exception handler call.
13885a0015d6SChris Zankel	 */
13895a0015d6SChris Zankel
139099d5040eSMax Filippov	xsr	a3, excsave1	# get spill-mask
1391244066f4SMax Filippov	slli	a3, a3, 1	# shift left by one
13923251f1e2SMax Filippov	addi	a3, a3, 1	# set the bit for the current window frame
13935a0015d6SChris Zankel
1394244066f4SMax Filippov	slli	a2, a3, 32-WSBITS
1395244066f4SMax Filippov	src	a2, a3, a2	# a2 = xxwww1yyxxxwww1yy......
1396bc5378fcSMax Filippov	wsr	a2, windowstart	# set corrected windowstart
13975a0015d6SChris Zankel
1398244066f4SMax Filippov	srli	a3, a3, 1
1399244066f4SMax Filippov	rsr	a2, excsave1
1400244066f4SMax Filippov	l32i	a2, a2, EXC_TABLE_DOUBLE_SAVE	# restore a2
1401244066f4SMax Filippov	xsr	a2, excsave1
1402244066f4SMax Filippov	s32i	a3, a2, EXC_TABLE_DOUBLE_SAVE	# save a3
1403244066f4SMax Filippov	l32i	a3, a2, EXC_TABLE_PARAM	# original WB (in user task)
1404244066f4SMax Filippov	xsr	a2, excsave1
14055a0015d6SChris Zankel
14065a0015d6SChris Zankel	/* Return to the original (user task) WINDOWBASE.
14075a0015d6SChris Zankel	 * We leave the following frame behind:
14085a0015d6SChris Zankel	 * a0, a1, a2	same
1409244066f4SMax Filippov	 * a3:		trashed (saved in EXC_TABLE_DOUBLE_SAVE)
14105a0015d6SChris Zankel	 * depc:	depc (we have to return to that address)
1411244066f4SMax Filippov	 * excsave_1:	exctable
14125a0015d6SChris Zankel	 */
14135a0015d6SChris Zankel
1414bc5378fcSMax Filippov	wsr	a3, windowbase
14155a0015d6SChris Zankel	rsync
14165a0015d6SChris Zankel
14175a0015d6SChris Zankel	/* We are now in the original frame when we entered _spill_registers:
14185a0015d6SChris Zankel	 *  a0: return address
14195a0015d6SChris Zankel	 *  a1: used, stack pointer
14205a0015d6SChris Zankel	 *  a2: kernel stack pointer
1421244066f4SMax Filippov	 *  a3: available
14225a0015d6SChris Zankel	 *  depc: exception address
1423244066f4SMax Filippov	 *  excsave: exctable
14245a0015d6SChris Zankel	 * Note: This frame might be the same as above.
14255a0015d6SChris Zankel	 */
14265a0015d6SChris Zankel
14275a0015d6SChris Zankel	/* Setup stack pointer. */
14285a0015d6SChris Zankel
14295a0015d6SChris Zankel	addi	a2, a2, -PT_USER_SIZE
14305a0015d6SChris Zankel	s32i	a0, a2, PT_AREG0
14315a0015d6SChris Zankel
14325a0015d6SChris Zankel	/* Make sure we return to this fixup handler. */
14335a0015d6SChris Zankel
14345a0015d6SChris Zankel	movi	a3, fast_syscall_spill_registers_fixup_return
14355a0015d6SChris Zankel	s32i	a3, a2, PT_DEPC		# setup depc
14365a0015d6SChris Zankel
14375a0015d6SChris Zankel	/* Jump to the exception handler. */
14385a0015d6SChris Zankel
143999d5040eSMax Filippov	rsr	a3, excsave1
1440bc5378fcSMax Filippov	rsr	a0, exccause
14415a0015d6SChris Zankel	addx4	a0, a0, a3              	# find entry in table
14425a0015d6SChris Zankel	l32i	a0, a0, EXC_TABLE_FAST_USER     # load handler
1443244066f4SMax Filippov	l32i	a3, a3, EXC_TABLE_DOUBLE_SAVE
14445a0015d6SChris Zankel	jx	a0
14455a0015d6SChris Zankel
1446244066f4SMax FilippovENDPROC(fast_syscall_spill_registers_fixup)
1447244066f4SMax Filippov
1448244066f4SMax FilippovENTRY(fast_syscall_spill_registers_fixup_return)
14495a0015d6SChris Zankel
14505a0015d6SChris Zankel	/* When we return here, all registers have been restored (a2: DEPC) */
14515a0015d6SChris Zankel
1452bc5378fcSMax Filippov	wsr	a2, depc		# exception address
14535a0015d6SChris Zankel
14545a0015d6SChris Zankel	/* Restore fixup handler. */
14555a0015d6SChris Zankel
1456244066f4SMax Filippov	rsr	a2, excsave1
1457244066f4SMax Filippov	s32i	a3, a2, EXC_TABLE_DOUBLE_SAVE
1458244066f4SMax Filippov	movi	a3, fast_syscall_spill_registers_fixup
1459244066f4SMax Filippov	s32i	a3, a2, EXC_TABLE_FIXUP
1460244066f4SMax Filippov	rsr	a3, windowbase
1461244066f4SMax Filippov	s32i	a3, a2, EXC_TABLE_PARAM
1462244066f4SMax Filippov	l32i	a2, a2, EXC_TABLE_KSTK
14635a0015d6SChris Zankel
14645a0015d6SChris Zankel	/* Load WB at the time the exception occurred. */
14655a0015d6SChris Zankel
1466bc5378fcSMax Filippov	rsr	a3, sar			# WB is still in SAR
14675a0015d6SChris Zankel	neg	a3, a3
1468bc5378fcSMax Filippov	wsr	a3, windowbase
14695a0015d6SChris Zankel	rsync
14705a0015d6SChris Zankel
1471244066f4SMax Filippov	rsr	a3, excsave1
1472244066f4SMax Filippov	l32i	a3, a3, EXC_TABLE_DOUBLE_SAVE
1473244066f4SMax Filippov
14745a0015d6SChris Zankel	rfde
14755a0015d6SChris Zankel
1476244066f4SMax FilippovENDPROC(fast_syscall_spill_registers_fixup_return)
14775a0015d6SChris Zankel
14789184289cSMax Filippov#else /* CONFIG_FAST_SYSCALL_SPILL_REGISTERS */
14799184289cSMax Filippov
14809184289cSMax FilippovENTRY(fast_syscall_spill_registers)
14819184289cSMax Filippov
14829184289cSMax Filippov	l32i    a0, a2, PT_AREG0        # restore a0
14839184289cSMax Filippov	movi	a2, -ENOSYS
14849184289cSMax Filippov	rfe
14859184289cSMax Filippov
14869184289cSMax FilippovENDPROC(fast_syscall_spill_registers)
14879184289cSMax Filippov
14889184289cSMax Filippov#endif /* CONFIG_FAST_SYSCALL_SPILL_REGISTERS */
14899184289cSMax Filippov
1490e5083a63SJohannes Weiner#ifdef CONFIG_MMU
14915a0015d6SChris Zankel/*
14925a0015d6SChris Zankel * We should never get here. Bail out!
14935a0015d6SChris Zankel */
14945a0015d6SChris Zankel
14955a0015d6SChris ZankelENTRY(fast_second_level_miss_double_kernel)
14965a0015d6SChris Zankel
14975a0015d6SChris Zankel1:	movi	a0, unrecoverable_exception
14985a0015d6SChris Zankel	callx0	a0		# should not return
14995a0015d6SChris Zankel1:	j	1b
15005a0015d6SChris Zankel
1501d1538c46SChris ZankelENDPROC(fast_second_level_miss_double_kernel)
1502d1538c46SChris Zankel
15035a0015d6SChris Zankel/* First-level entry handler for user, kernel, and double 2nd-level
15045a0015d6SChris Zankel * TLB miss exceptions.  Note that for now, user and kernel miss
15055a0015d6SChris Zankel * exceptions share the same entry point and are handled identically.
15065a0015d6SChris Zankel *
15075a0015d6SChris Zankel * An old, less-efficient C version of this function used to exist.
15085a0015d6SChris Zankel * We include it below, interleaved as comments, for reference.
15095a0015d6SChris Zankel *
15105a0015d6SChris Zankel * Entry condition:
15115a0015d6SChris Zankel *
15125a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
15135a0015d6SChris Zankel *   a1:	a1
15145a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
151599d5040eSMax Filippov *   a3:	a3
15165a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
151799d5040eSMax Filippov *   excsave_1:	dispatch table
15185a0015d6SChris Zankel *
15195a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
15205a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
15215a0015d6SChris Zankel */
15225a0015d6SChris Zankel
15235a0015d6SChris ZankelENTRY(fast_second_level_miss)
15245a0015d6SChris Zankel
152599d5040eSMax Filippov	/* Save a1 and a3. Note: we don't expect a double exception. */
15265a0015d6SChris Zankel
15275a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
152899d5040eSMax Filippov	s32i	a3, a2, PT_AREG3
15295a0015d6SChris Zankel
15305a0015d6SChris Zankel	/* We need to map the page of PTEs for the user task.  Find
15315a0015d6SChris Zankel	 * the pointer to that page.  Also, it's possible for tsk->mm
15325a0015d6SChris Zankel	 * to be NULL while tsk->active_mm is nonzero if we faulted on
15335a0015d6SChris Zankel	 * a vmalloc address.  In that rare case, we must use
15345a0015d6SChris Zankel	 * active_mm instead to avoid a fault in this handler.  See
15355a0015d6SChris Zankel	 *
15365a0015d6SChris Zankel	 * http://mail.nl.linux.org/linux-mm/2002-08/msg00258.html
15375a0015d6SChris Zankel	 *   (or search Internet on "mm vs. active_mm")
15385a0015d6SChris Zankel	 *
15395a0015d6SChris Zankel	 *	if (!mm)
15405a0015d6SChris Zankel	 *		mm = tsk->active_mm;
15415a0015d6SChris Zankel	 *	pgd = pgd_offset (mm, regs->excvaddr);
15425a0015d6SChris Zankel	 *	pmd = pmd_offset (pgd, regs->excvaddr);
15435a0015d6SChris Zankel	 *	pmdval = *pmd;
15445a0015d6SChris Zankel	 */
15455a0015d6SChris Zankel
15465a0015d6SChris Zankel	GET_CURRENT(a1,a2)
15475a0015d6SChris Zankel	l32i	a0, a1, TASK_MM		# tsk->mm
15485a0015d6SChris Zankel	beqz	a0, 9f
15495a0015d6SChris Zankel
1550bc5378fcSMax Filippov8:	rsr	a3, excvaddr		# fault address
155101858d1bSChris Zankel	_PGD_OFFSET(a0, a3, a1)
15525a0015d6SChris Zankel	l32i	a0, a0, 0		# read pmdval
15535a0015d6SChris Zankel	beqz	a0, 2f
15545a0015d6SChris Zankel
15555a0015d6SChris Zankel	/* Read ptevaddr and convert to top of page-table page.
15565a0015d6SChris Zankel	 *
15575a0015d6SChris Zankel	 * 	vpnval = read_ptevaddr_register() & PAGE_MASK;
15585a0015d6SChris Zankel	 * 	vpnval += DTLB_WAY_PGTABLE;
15595a0015d6SChris Zankel	 *	pteval = mk_pte (virt_to_page(pmd_val(pmdval)), PAGE_KERNEL);
15605a0015d6SChris Zankel	 *	write_dtlb_entry (pteval, vpnval);
15615a0015d6SChris Zankel	 *
15625a0015d6SChris Zankel	 * The messy computation for 'pteval' above really simplifies
15635a0015d6SChris Zankel	 * into the following:
15645a0015d6SChris Zankel	 *
15656656920bSChris Zankel	 * pteval = ((pmdval - PAGE_OFFSET) & PAGE_MASK) | PAGE_DIRECTORY
15665a0015d6SChris Zankel	 */
15675a0015d6SChris Zankel
156839070cb8SChris Zankel	movi	a1, (-PAGE_OFFSET) & 0xffffffff
15695a0015d6SChris Zankel	add	a0, a0, a1		# pmdval - PAGE_OFFSET
15705a0015d6SChris Zankel	extui	a1, a0, 0, PAGE_SHIFT	# ... & PAGE_MASK
15715a0015d6SChris Zankel	xor	a0, a0, a1
15725a0015d6SChris Zankel
157301858d1bSChris Zankel	movi	a1, _PAGE_DIRECTORY
15745a0015d6SChris Zankel	or	a0, a0, a1		# ... | PAGE_DIRECTORY
15755a0015d6SChris Zankel
157601858d1bSChris Zankel	/*
15776656920bSChris Zankel	 * We utilize all three wired-ways (7-9) to hold pmd translations.
157801858d1bSChris Zankel	 * Memory regions are mapped to the DTLBs according to bits 28 and 29.
157901858d1bSChris Zankel	 * This allows to map the three most common regions to three different
158001858d1bSChris Zankel	 * DTLBs:
158101858d1bSChris Zankel	 *  0,1 -> way 7	program (0040.0000) and virtual (c000.0000)
158201858d1bSChris Zankel	 *  2   -> way 8	shared libaries (2000.0000)
158301858d1bSChris Zankel	 *  3   -> way 0	stack (3000.0000)
158401858d1bSChris Zankel	 */
15855a0015d6SChris Zankel
158601858d1bSChris Zankel	extui	a3, a3, 28, 2		# addr. bit 28 and 29	0,1,2,3
1587bc5378fcSMax Filippov	rsr	a1, ptevaddr
158801858d1bSChris Zankel	addx2	a3, a3, a3		# ->			0,3,6,9
158901858d1bSChris Zankel	srli	a1, a1, PAGE_SHIFT
159001858d1bSChris Zankel	extui	a3, a3, 2, 2		# ->			0,0,1,2
159101858d1bSChris Zankel	slli	a1, a1, PAGE_SHIFT	# ptevaddr & PAGE_MASK
159201858d1bSChris Zankel	addi	a3, a3, DTLB_WAY_PGD
159301858d1bSChris Zankel	add	a1, a1, a3		# ... + way_number
159401858d1bSChris Zankel
159501858d1bSChris Zankel3:	wdtlb	a0, a1
15965a0015d6SChris Zankel	dsync
15975a0015d6SChris Zankel
15985a0015d6SChris Zankel	/* Exit critical section. */
15995a0015d6SChris Zankel
160099d5040eSMax Filippov4:	rsr	a3, excsave1
16015a0015d6SChris Zankel	movi	a0, 0
16025a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_FIXUP
16035a0015d6SChris Zankel
16045a0015d6SChris Zankel	/* Restore the working registers, and return. */
16055a0015d6SChris Zankel
16065a0015d6SChris Zankel	l32i	a0, a2, PT_AREG0
16075a0015d6SChris Zankel	l32i	a1, a2, PT_AREG1
160899d5040eSMax Filippov	l32i	a3, a2, PT_AREG3
16095a0015d6SChris Zankel	l32i	a2, a2, PT_DEPC
16105a0015d6SChris Zankel
16115a0015d6SChris Zankel	bgeui	a2, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
16125a0015d6SChris Zankel
16135a0015d6SChris Zankel	/* Restore excsave1 and return. */
16145a0015d6SChris Zankel
1615bc5378fcSMax Filippov	rsr	a2, depc
16165a0015d6SChris Zankel	rfe
16175a0015d6SChris Zankel
16185a0015d6SChris Zankel	/* Return from double exception. */
16195a0015d6SChris Zankel
1620bc5378fcSMax Filippov1:	xsr	a2, depc
16215a0015d6SChris Zankel	esync
16225a0015d6SChris Zankel	rfde
16235a0015d6SChris Zankel
16245a0015d6SChris Zankel9:	l32i	a0, a1, TASK_ACTIVE_MM	# unlikely case mm == 0
1625*38fef73cSMax Filippov	bnez	a0, 8b
1626*38fef73cSMax Filippov
1627*38fef73cSMax Filippov	/* Even more unlikely case active_mm == 0.
1628*38fef73cSMax Filippov	 * We can get here with NMI in the middle of context_switch that
1629*38fef73cSMax Filippov	 * touches vmalloc area.
1630*38fef73cSMax Filippov	 */
1631*38fef73cSMax Filippov	movi	a0, init_mm
16325a0015d6SChris Zankel	j	8b
16335a0015d6SChris Zankel
16346656920bSChris Zankel#if (DCACHE_WAY_SIZE > PAGE_SIZE)
16356656920bSChris Zankel
16366656920bSChris Zankel2:	/* Special case for cache aliasing.
16376656920bSChris Zankel	 * We (should) only get here if a clear_user_page, copy_user_page
16386656920bSChris Zankel	 * or the aliased cache flush functions got preemptively interrupted
16396656920bSChris Zankel	 * by another task. Re-establish temporary mapping to the
16406656920bSChris Zankel	 * TLBTEMP_BASE areas.
16416656920bSChris Zankel	 */
16426656920bSChris Zankel
16436656920bSChris Zankel	/* We shouldn't be in a double exception */
16446656920bSChris Zankel
16456656920bSChris Zankel	l32i	a0, a2, PT_DEPC
16466656920bSChris Zankel	bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, 2f
16476656920bSChris Zankel
16486656920bSChris Zankel	/* Make sure the exception originated in the special functions */
16496656920bSChris Zankel
16506656920bSChris Zankel	movi	a0, __tlbtemp_mapping_start
1651bc5378fcSMax Filippov	rsr	a3, epc1
16526656920bSChris Zankel	bltu	a3, a0, 2f
16536656920bSChris Zankel	movi	a0, __tlbtemp_mapping_end
16546656920bSChris Zankel	bgeu	a3, a0, 2f
16556656920bSChris Zankel
16566656920bSChris Zankel	/* Check if excvaddr was in one of the TLBTEMP_BASE areas. */
16576656920bSChris Zankel
16586656920bSChris Zankel	movi	a3, TLBTEMP_BASE_1
1659bc5378fcSMax Filippov	rsr	a0, excvaddr
16606656920bSChris Zankel	bltu	a0, a3, 2f
16616656920bSChris Zankel
16627128039fSMax Filippov	addi	a1, a0, -TLBTEMP_SIZE
16636656920bSChris Zankel	bgeu	a1, a3, 2f
16646656920bSChris Zankel
16656656920bSChris Zankel	/* Check if we have to restore an ITLB mapping. */
16666656920bSChris Zankel
16676656920bSChris Zankel	movi	a1, __tlbtemp_mapping_itlb
1668bc5378fcSMax Filippov	rsr	a3, epc1
16696656920bSChris Zankel	sub	a3, a3, a1
16706656920bSChris Zankel
16716656920bSChris Zankel	/* Calculate VPN */
16726656920bSChris Zankel
16736656920bSChris Zankel	movi	a1, PAGE_MASK
16746656920bSChris Zankel	and	a1, a1, a0
16756656920bSChris Zankel
16766656920bSChris Zankel	/* Jump for ITLB entry */
16776656920bSChris Zankel
16786656920bSChris Zankel	bgez	a3, 1f
16796656920bSChris Zankel
16806656920bSChris Zankel	/* We can use up to two TLBTEMP areas, one for src and one for dst. */
16816656920bSChris Zankel
16826656920bSChris Zankel	extui	a3, a0, PAGE_SHIFT + DCACHE_ALIAS_ORDER, 1
16836656920bSChris Zankel	add	a1, a3, a1
16846656920bSChris Zankel
16856656920bSChris Zankel	/* PPN is in a6 for the first TLBTEMP area and in a7 for the second. */
16866656920bSChris Zankel
16876656920bSChris Zankel	mov	a0, a6
16886656920bSChris Zankel	movnez	a0, a7, a3
16896656920bSChris Zankel	j	3b
16906656920bSChris Zankel
16916656920bSChris Zankel	/* ITLB entry. We only use dst in a6. */
16926656920bSChris Zankel
16936656920bSChris Zankel1:	witlb	a6, a1
16946656920bSChris Zankel	isync
16956656920bSChris Zankel	j	4b
16966656920bSChris Zankel
16976656920bSChris Zankel
16986656920bSChris Zankel#endif	// DCACHE_WAY_SIZE > PAGE_SIZE
16996656920bSChris Zankel
17006656920bSChris Zankel
17015a0015d6SChris Zankel2:	/* Invalid PGD, default exception handling */
17025a0015d6SChris Zankel
1703bc5378fcSMax Filippov	rsr	a1, depc
17045a0015d6SChris Zankel	s32i	a1, a2, PT_AREG2
17055a0015d6SChris Zankel	mov	a1, a2
17065a0015d6SChris Zankel
1707bc5378fcSMax Filippov	rsr	a2, ps
1708173d6681SChris Zankel	bbsi.l	a2, PS_UM_BIT, 1f
17095a0015d6SChris Zankel	j	_kernel_exception
17105a0015d6SChris Zankel1:	j	_user_exception
17115a0015d6SChris Zankel
1712d1538c46SChris ZankelENDPROC(fast_second_level_miss)
17135a0015d6SChris Zankel
17145a0015d6SChris Zankel/*
17155a0015d6SChris Zankel * StoreProhibitedException
17165a0015d6SChris Zankel *
17175a0015d6SChris Zankel * Update the pte and invalidate the itlb mapping for this pte.
17185a0015d6SChris Zankel *
17195a0015d6SChris Zankel * Entry condition:
17205a0015d6SChris Zankel *
17215a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
17225a0015d6SChris Zankel *   a1:	a1
17235a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
172499d5040eSMax Filippov *   a3:	a3
17255a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
172699d5040eSMax Filippov *   excsave_1:	dispatch table
17275a0015d6SChris Zankel *
17285a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
17295a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
17305a0015d6SChris Zankel */
17315a0015d6SChris Zankel
17325a0015d6SChris ZankelENTRY(fast_store_prohibited)
17335a0015d6SChris Zankel
173499d5040eSMax Filippov	/* Save a1 and a3. */
17355a0015d6SChris Zankel
17365a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
173799d5040eSMax Filippov	s32i	a3, a2, PT_AREG3
17385a0015d6SChris Zankel
17395a0015d6SChris Zankel	GET_CURRENT(a1,a2)
17405a0015d6SChris Zankel	l32i	a0, a1, TASK_MM		# tsk->mm
17415a0015d6SChris Zankel	beqz	a0, 9f
17425a0015d6SChris Zankel
1743bc5378fcSMax Filippov8:	rsr	a1, excvaddr		# fault address
174499d5040eSMax Filippov	_PGD_OFFSET(a0, a1, a3)
17455a0015d6SChris Zankel	l32i	a0, a0, 0
17465a0015d6SChris Zankel	beqz	a0, 2f
17475a0015d6SChris Zankel
174851fc41a9SMax Filippov	/*
174951fc41a9SMax Filippov	 * Note that we test _PAGE_WRITABLE_BIT only if PTE is present
175051fc41a9SMax Filippov	 * and is not PAGE_NONE. See pgtable.h for possible PTE layouts.
175151fc41a9SMax Filippov	 */
175201858d1bSChris Zankel
175399d5040eSMax Filippov	_PTE_OFFSET(a0, a1, a3)
175499d5040eSMax Filippov	l32i	a3, a0, 0		# read pteval
175551fc41a9SMax Filippov	movi	a1, _PAGE_CA_INVALID
175699d5040eSMax Filippov	ball	a3, a1, 2f
175799d5040eSMax Filippov	bbci.l	a3, _PAGE_WRITABLE_BIT, 2f
17585a0015d6SChris Zankel
175901858d1bSChris Zankel	movi	a1, _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_HW_WRITE
176099d5040eSMax Filippov	or	a3, a3, a1
1761bc5378fcSMax Filippov	rsr	a1, excvaddr
176299d5040eSMax Filippov	s32i	a3, a0, 0
17635a0015d6SChris Zankel
17645a0015d6SChris Zankel	/* We need to flush the cache if we have page coloring. */
17655a0015d6SChris Zankel#if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
17665a0015d6SChris Zankel	dhwb	a0, 0
17675a0015d6SChris Zankel#endif
17685a0015d6SChris Zankel	pdtlb	a0, a1
176999d5040eSMax Filippov	wdtlb	a3, a0
17705a0015d6SChris Zankel
17715a0015d6SChris Zankel	/* Exit critical section. */
17725a0015d6SChris Zankel
17735a0015d6SChris Zankel	movi	a0, 0
177499d5040eSMax Filippov	rsr	a3, excsave1
17755a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_FIXUP
17765a0015d6SChris Zankel
17775a0015d6SChris Zankel	/* Restore the working registers, and return. */
17785a0015d6SChris Zankel
177999d5040eSMax Filippov	l32i	a3, a2, PT_AREG3
17805a0015d6SChris Zankel	l32i	a1, a2, PT_AREG1
17815a0015d6SChris Zankel	l32i	a0, a2, PT_AREG0
17825a0015d6SChris Zankel	l32i	a2, a2, PT_DEPC
17835a0015d6SChris Zankel
17845a0015d6SChris Zankel	bgeui	a2, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
17855a0015d6SChris Zankel
1786bc5378fcSMax Filippov	rsr	a2, depc
17875a0015d6SChris Zankel	rfe
17885a0015d6SChris Zankel
17895a0015d6SChris Zankel	/* Double exception. Restore FIXUP handler and return. */
17905a0015d6SChris Zankel
1791bc5378fcSMax Filippov1:	xsr	a2, depc
17925a0015d6SChris Zankel	esync
17935a0015d6SChris Zankel	rfde
17945a0015d6SChris Zankel
17955a0015d6SChris Zankel9:	l32i	a0, a1, TASK_ACTIVE_MM	# unlikely case mm == 0
17965a0015d6SChris Zankel	j	8b
17975a0015d6SChris Zankel
17985a0015d6SChris Zankel2:	/* If there was a problem, handle fault in C */
17995a0015d6SChris Zankel
180099d5040eSMax Filippov	rsr	a3, depc	# still holds a2
180199d5040eSMax Filippov	s32i	a3, a2, PT_AREG2
18025a0015d6SChris Zankel	mov	a1, a2
18035a0015d6SChris Zankel
1804bc5378fcSMax Filippov	rsr	a2, ps
1805173d6681SChris Zankel	bbsi.l	a2, PS_UM_BIT, 1f
18065a0015d6SChris Zankel	j	_kernel_exception
18075a0015d6SChris Zankel1:	j	_user_exception
1808d1538c46SChris Zankel
1809d1538c46SChris ZankelENDPROC(fast_store_prohibited)
1810d1538c46SChris Zankel
1811e5083a63SJohannes Weiner#endif /* CONFIG_MMU */
18125a0015d6SChris Zankel
18135a0015d6SChris Zankel/*
1814fc4fb2adSChris Zankel * System Calls.
1815fc4fb2adSChris Zankel *
1816fc4fb2adSChris Zankel * void system_call (struct pt_regs* regs, int exccause)
1817fc4fb2adSChris Zankel *                            a2                 a3
1818fc4fb2adSChris Zankel */
1819fc4fb2adSChris Zankel
1820fc4fb2adSChris ZankelENTRY(system_call)
1821d1538c46SChris Zankel
1822fc4fb2adSChris Zankel	entry	a1, 32
1823fc4fb2adSChris Zankel
1824fc4fb2adSChris Zankel	/* regs->syscall = regs->areg[2] */
1825fc4fb2adSChris Zankel
1826fc4fb2adSChris Zankel	l32i	a3, a2, PT_AREG2
1827fc4fb2adSChris Zankel	mov	a6, a2
1828fc4fb2adSChris Zankel	movi	a4, do_syscall_trace_enter
1829fc4fb2adSChris Zankel	s32i	a3, a2, PT_SYSCALL
1830fc4fb2adSChris Zankel	callx4	a4
1831fc4fb2adSChris Zankel
1832fc4fb2adSChris Zankel	/* syscall = sys_call_table[syscall_nr] */
1833fc4fb2adSChris Zankel
1834fc4fb2adSChris Zankel	movi	a4, sys_call_table;
1835fc4fb2adSChris Zankel	movi	a5, __NR_syscall_count
1836fc4fb2adSChris Zankel	movi	a6, -ENOSYS
1837fc4fb2adSChris Zankel	bgeu	a3, a5, 1f
1838fc4fb2adSChris Zankel
1839fc4fb2adSChris Zankel	addx4	a4, a3, a4
1840fc4fb2adSChris Zankel	l32i	a4, a4, 0
1841fc4fb2adSChris Zankel	movi	a5, sys_ni_syscall;
1842fc4fb2adSChris Zankel	beq	a4, a5, 1f
1843fc4fb2adSChris Zankel
1844fc4fb2adSChris Zankel	/* Load args: arg0 - arg5 are passed via regs. */
1845fc4fb2adSChris Zankel
1846fc4fb2adSChris Zankel	l32i	a6, a2, PT_AREG6
1847fc4fb2adSChris Zankel	l32i	a7, a2, PT_AREG3
1848fc4fb2adSChris Zankel	l32i	a8, a2, PT_AREG4
1849fc4fb2adSChris Zankel	l32i	a9, a2, PT_AREG5
1850fc4fb2adSChris Zankel	l32i	a10, a2, PT_AREG8
1851fc4fb2adSChris Zankel	l32i	a11, a2, PT_AREG9
1852fc4fb2adSChris Zankel
1853fc4fb2adSChris Zankel	/* Pass one additional argument to the syscall: pt_regs (on stack) */
1854fc4fb2adSChris Zankel	s32i	a2, a1, 0
1855fc4fb2adSChris Zankel
1856fc4fb2adSChris Zankel	callx4	a4
1857fc4fb2adSChris Zankel
1858fc4fb2adSChris Zankel1:	/* regs->areg[2] = return_value */
1859fc4fb2adSChris Zankel
1860fc4fb2adSChris Zankel	s32i	a6, a2, PT_AREG2
1861fc4fb2adSChris Zankel	movi	a4, do_syscall_trace_leave
1862fc4fb2adSChris Zankel	mov	a6, a2
1863fc4fb2adSChris Zankel	callx4	a4
1864fc4fb2adSChris Zankel	retw
1865fc4fb2adSChris Zankel
1866d1538c46SChris ZankelENDPROC(system_call)
1867d1538c46SChris Zankel
1868e2fd1374SMax Filippov/*
1869e2fd1374SMax Filippov * Spill live registers on the kernel stack macro.
1870e2fd1374SMax Filippov *
1871e2fd1374SMax Filippov * Entry condition: ps.woe is set, ps.excm is cleared
1872e2fd1374SMax Filippov * Exit condition: windowstart has single bit set
1873e2fd1374SMax Filippov * May clobber: a12, a13
1874e2fd1374SMax Filippov */
1875e2fd1374SMax Filippov	.macro	spill_registers_kernel
1876e2fd1374SMax Filippov
1877e2fd1374SMax Filippov#if XCHAL_NUM_AREGS > 16
1878e2fd1374SMax Filippov	call12	1f
1879e2fd1374SMax Filippov	_j	2f
1880e2fd1374SMax Filippov	retw
1881e2fd1374SMax Filippov	.align	4
1882e2fd1374SMax Filippov1:
1883e2fd1374SMax Filippov	_entry	a1, 48
1884e2fd1374SMax Filippov	addi	a12, a0, 3
1885e2fd1374SMax Filippov#if XCHAL_NUM_AREGS > 32
1886e2fd1374SMax Filippov	.rept	(XCHAL_NUM_AREGS - 32) / 12
1887e2fd1374SMax Filippov	_entry	a1, 48
1888e2fd1374SMax Filippov	mov	a12, a0
1889e2fd1374SMax Filippov	.endr
1890e2fd1374SMax Filippov#endif
189177d6273eSMax Filippov	_entry	a1, 16
1892e2fd1374SMax Filippov#if XCHAL_NUM_AREGS % 12 == 0
1893e2fd1374SMax Filippov	mov	a8, a8
1894e2fd1374SMax Filippov#elif XCHAL_NUM_AREGS % 12 == 4
1895e2fd1374SMax Filippov	mov	a12, a12
1896e2fd1374SMax Filippov#elif XCHAL_NUM_AREGS % 12 == 8
1897e2fd1374SMax Filippov	mov	a4, a4
1898e2fd1374SMax Filippov#endif
1899e2fd1374SMax Filippov	retw
1900e2fd1374SMax Filippov2:
1901e2fd1374SMax Filippov#else
1902e2fd1374SMax Filippov	mov	a12, a12
1903e2fd1374SMax Filippov#endif
1904e2fd1374SMax Filippov	.endm
1905fc4fb2adSChris Zankel
1906fc4fb2adSChris Zankel/*
19075a0015d6SChris Zankel * Task switch.
19085a0015d6SChris Zankel *
19095a0015d6SChris Zankel * struct task*  _switch_to (struct task* prev, struct task* next)
19105a0015d6SChris Zankel *         a2                              a2                 a3
19115a0015d6SChris Zankel */
19125a0015d6SChris Zankel
19135a0015d6SChris ZankelENTRY(_switch_to)
19145a0015d6SChris Zankel
191577d6273eSMax Filippov	entry	a1, 48
19165a0015d6SChris Zankel
1917e2fd1374SMax Filippov	mov	a11, a3			# and 'next' (a3)
19185a0015d6SChris Zankel
1919c658eac6SChris Zankel	l32i	a4, a2, TASK_THREAD_INFO
1920c658eac6SChris Zankel	l32i	a5, a3, TASK_THREAD_INFO
19215a0015d6SChris Zankel
1922e2fd1374SMax Filippov	save_xtregs_user a4 a6 a8 a9 a12 a13 THREAD_XTREGS_USER
19235a0015d6SChris Zankel
192452247123SMax Filippov#if THREAD_RA > 1020 || THREAD_SP > 1020
192552247123SMax Filippov	addi	a10, a2, TASK_THREAD
192652247123SMax Filippov	s32i	a0, a10, THREAD_RA - TASK_THREAD	# save return address
192752247123SMax Filippov	s32i	a1, a10, THREAD_SP - TASK_THREAD	# save stack pointer
192852247123SMax Filippov#else
192952247123SMax Filippov	s32i	a0, a2, THREAD_RA	# save return address
193052247123SMax Filippov	s32i	a1, a2, THREAD_SP	# save stack pointer
193152247123SMax Filippov#endif
1932c658eac6SChris Zankel
1933c658eac6SChris Zankel	/* Disable ints while we manipulate the stack pointer. */
1934c658eac6SChris Zankel
1935*38fef73cSMax Filippov	irq_save a14, a3
19365a0015d6SChris Zankel	rsync
19375a0015d6SChris Zankel
1938c658eac6SChris Zankel	/* Switch CPENABLE */
1939c658eac6SChris Zankel
1940c658eac6SChris Zankel#if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
1941c658eac6SChris Zankel	l32i	a3, a5, THREAD_CPENABLE
1942bc5378fcSMax Filippov	xsr	a3, cpenable
1943c658eac6SChris Zankel	s32i	a3, a4, THREAD_CPENABLE
1944c658eac6SChris Zankel#endif
1945c658eac6SChris Zankel
1946c658eac6SChris Zankel	/* Flush register file. */
1947c658eac6SChris Zankel
1948e2fd1374SMax Filippov	spill_registers_kernel
19495a0015d6SChris Zankel
19505a0015d6SChris Zankel	/* Set kernel stack (and leave critical section)
19515a0015d6SChris Zankel	 * Note: It's save to set it here. The stack will not be overwritten
19525a0015d6SChris Zankel	 *       because the kernel stack will only be loaded again after
19535a0015d6SChris Zankel	 *       we return from kernel space.
19545a0015d6SChris Zankel	 */
19555a0015d6SChris Zankel
1956bc5378fcSMax Filippov	rsr	a3, excsave1		# exc_table
1957c658eac6SChris Zankel	addi	a7, a5, PT_REGS_OFFSET
1958c658eac6SChris Zankel	s32i	a7, a3, EXC_TABLE_KSTK
19595a0015d6SChris Zankel
1960c50842dfSChris Zankel	/* restore context of the task 'next' */
19615a0015d6SChris Zankel
1962e2fd1374SMax Filippov	l32i	a0, a11, THREAD_RA	# restore return address
1963e2fd1374SMax Filippov	l32i	a1, a11, THREAD_SP	# restore stack pointer
19645a0015d6SChris Zankel
1965e2fd1374SMax Filippov	load_xtregs_user a5 a6 a8 a9 a12 a13 THREAD_XTREGS_USER
1966c658eac6SChris Zankel
1967bc5378fcSMax Filippov	wsr	a14, ps
19685a0015d6SChris Zankel	rsync
19695a0015d6SChris Zankel
19705a0015d6SChris Zankel	retw
19715a0015d6SChris Zankel
1972d1538c46SChris ZankelENDPROC(_switch_to)
19735a0015d6SChris Zankel
19745a0015d6SChris ZankelENTRY(ret_from_fork)
19755a0015d6SChris Zankel
19765a0015d6SChris Zankel	/* void schedule_tail (struct task_struct *prev)
19775a0015d6SChris Zankel	 * Note: prev is still in a6 (return value from fake call4 frame)
19785a0015d6SChris Zankel	 */
19795a0015d6SChris Zankel	movi	a4, schedule_tail
19805a0015d6SChris Zankel	callx4	a4
19815a0015d6SChris Zankel
1982fc4fb2adSChris Zankel	movi	a4, do_syscall_trace_leave
1983fc4fb2adSChris Zankel	mov	a6, a1
19845a0015d6SChris Zankel	callx4	a4
19855a0015d6SChris Zankel
19865a0015d6SChris Zankel	j	common_exception_return
19875a0015d6SChris Zankel
1988d1538c46SChris ZankelENDPROC(ret_from_fork)
1989d1538c46SChris Zankel
19903306a726SMax Filippov/*
19913306a726SMax Filippov * Kernel thread creation helper
19923306a726SMax Filippov * On entry, set up by copy_thread: a2 = thread_fn, a3 = thread_fn arg
19933306a726SMax Filippov *           left from _switch_to: a6 = prev
19943306a726SMax Filippov */
19953306a726SMax FilippovENTRY(ret_from_kernel_thread)
19963306a726SMax Filippov
19973306a726SMax Filippov	call4	schedule_tail
19983306a726SMax Filippov	mov	a6, a3
19993306a726SMax Filippov	callx4	a2
2000f0a1bf08SMax Filippov	j	common_exception_return
20013306a726SMax Filippov
20023306a726SMax FilippovENDPROC(ret_from_kernel_thread)
2003