xref: /linux-6.15/arch/xtensa/kernel/entry.S (revision bec58f40)
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.
938fef73cSMax 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>
1665fddcfcSMike Rapoport#include <linux/pgtable.h>
170013a854SSam Ravnborg#include <asm/asm-offsets.h>
180013acebSMax Filippov#include <asm/asmmacro.h>
195a0015d6SChris Zankel#include <asm/processor.h>
204573e398SChris Zankel#include <asm/coprocessor.h>
215a0015d6SChris Zankel#include <asm/thread_info.h>
2276580237SAl Viro#include <asm/asm-uaccess.h>
235a0015d6SChris Zankel#include <asm/unistd.h>
245a0015d6SChris Zankel#include <asm/ptrace.h>
255a0015d6SChris Zankel#include <asm/current.h>
265a0015d6SChris Zankel#include <asm/page.h>
275a0015d6SChris Zankel#include <asm/signal.h>
28173d6681SChris Zankel#include <asm/tlbflush.h>
29367b8112SChris Zankel#include <variant/tie-asm.h>
305a0015d6SChris Zankel
315a0015d6SChris Zankel/* Unimplemented features. */
325a0015d6SChris Zankel
335a0015d6SChris Zankel#undef KERNEL_STACK_OVERFLOW_CHECK
345a0015d6SChris Zankel
355a0015d6SChris Zankel/* Not well tested.
365a0015d6SChris Zankel *
375a0015d6SChris Zankel * - fast_coprocessor
385a0015d6SChris Zankel */
395a0015d6SChris Zankel
405a0015d6SChris Zankel/*
415a0015d6SChris Zankel * Macro to find first bit set in WINDOWBASE from the left + 1
425a0015d6SChris Zankel *
435a0015d6SChris Zankel * 100....0 -> 1
445a0015d6SChris Zankel * 010....0 -> 2
455a0015d6SChris Zankel * 000....1 -> WSBITS
465a0015d6SChris Zankel */
475a0015d6SChris Zankel
485a0015d6SChris Zankel	.macro ffs_ws bit mask
495a0015d6SChris Zankel
505a0015d6SChris Zankel#if XCHAL_HAVE_NSA
515a0015d6SChris Zankel	nsau    \bit, \mask			# 32-WSBITS ... 31 (32 iff 0)
525a0015d6SChris Zankel	addi    \bit, \bit, WSBITS - 32 + 1   	# uppest bit set -> return 1
535a0015d6SChris Zankel#else
545a0015d6SChris Zankel	movi    \bit, WSBITS
555a0015d6SChris Zankel#if WSBITS > 16
565a0015d6SChris Zankel	_bltui  \mask, 0x10000, 99f
575a0015d6SChris Zankel	addi    \bit, \bit, -16
585a0015d6SChris Zankel	extui   \mask, \mask, 16, 16
595a0015d6SChris Zankel#endif
605a0015d6SChris Zankel#if WSBITS > 8
615a0015d6SChris Zankel99:	_bltui  \mask, 0x100, 99f
625a0015d6SChris Zankel	addi    \bit, \bit, -8
635a0015d6SChris Zankel	srli    \mask, \mask, 8
645a0015d6SChris Zankel#endif
655a0015d6SChris Zankel99:	_bltui  \mask, 0x10, 99f
665a0015d6SChris Zankel	addi    \bit, \bit, -4
675a0015d6SChris Zankel	srli    \mask, \mask, 4
685a0015d6SChris Zankel99:	_bltui  \mask, 0x4, 99f
695a0015d6SChris Zankel	addi    \bit, \bit, -2
705a0015d6SChris Zankel	srli    \mask, \mask, 2
715a0015d6SChris Zankel99:	_bltui  \mask, 0x2, 99f
725a0015d6SChris Zankel	addi    \bit, \bit, -1
735a0015d6SChris Zankel99:
745a0015d6SChris Zankel
755a0015d6SChris Zankel#endif
765a0015d6SChris Zankel	.endm
775a0015d6SChris Zankel
7838fef73cSMax Filippov
7938fef73cSMax Filippov	.macro	irq_save flags tmp
8038fef73cSMax Filippov#if XTENSA_FAKE_NMI
8138fef73cSMax Filippov#if defined(CONFIG_DEBUG_KERNEL) && (LOCKLEVEL | TOPLEVEL) >= XCHAL_DEBUGLEVEL
8238fef73cSMax Filippov	rsr	\flags, ps
8338fef73cSMax Filippov	extui	\tmp, \flags, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
8438fef73cSMax Filippov	bgei	\tmp, LOCKLEVEL, 99f
8538fef73cSMax Filippov	rsil	\tmp, LOCKLEVEL
8638fef73cSMax Filippov99:
8738fef73cSMax Filippov#else
8838fef73cSMax Filippov	movi	\tmp, LOCKLEVEL
8938fef73cSMax Filippov	rsr	\flags, ps
9038fef73cSMax Filippov	or	\flags, \flags, \tmp
9138fef73cSMax Filippov	xsr	\flags, ps
9238fef73cSMax Filippov	rsync
9338fef73cSMax Filippov#endif
9438fef73cSMax Filippov#else
9538fef73cSMax Filippov	rsil	\flags, LOCKLEVEL
9638fef73cSMax Filippov#endif
9738fef73cSMax Filippov	.endm
9838fef73cSMax Filippov
995a0015d6SChris Zankel/* ----------------- DEFAULT FIRST LEVEL EXCEPTION HANDLERS ----------------- */
1005a0015d6SChris Zankel
1015a0015d6SChris Zankel/*
1025a0015d6SChris Zankel * First-level exception handler for user exceptions.
1035a0015d6SChris Zankel * Save some special registers, extra states and all registers in the AR
1045a0015d6SChris Zankel * register file that were in use in the user task, and jump to the common
1055a0015d6SChris Zankel * exception code.
1065a0015d6SChris Zankel * We save SAR (used to calculate WMASK), and WB and WS (we don't have to
1075a0015d6SChris Zankel * save them for kernel exceptions).
1085a0015d6SChris Zankel *
1095a0015d6SChris Zankel * Entry condition for user_exception:
1105a0015d6SChris Zankel *
1115a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
1125a0015d6SChris Zankel *   a1:	a1
1135a0015d6SChris Zankel *   a2:	new stack pointer, original value in depc
11499d5040eSMax Filippov *   a3:	a3
1155a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
11699d5040eSMax Filippov *   excsave1:	dispatch table
1175a0015d6SChris Zankel *
1185a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
1195a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
1205a0015d6SChris Zankel *
1215a0015d6SChris Zankel * Entry condition for _user_exception:
1225a0015d6SChris Zankel *
1235a0015d6SChris Zankel *   a0-a3 and depc have been saved to PT_AREG0...PT_AREG3 and PT_DEPC
1245a0015d6SChris Zankel *   excsave has been restored, and
1255a0015d6SChris Zankel *   stack pointer (a1) has been set.
1265a0015d6SChris Zankel *
1273ad2f3fbSDaniel Mack * Note: _user_exception might be at an odd address. Don't use call0..call12
1285a0015d6SChris Zankel */
129f8f02ca7SMax Filippov	.literal_position
1305a0015d6SChris Zankel
1315a0015d6SChris ZankelENTRY(user_exception)
1325a0015d6SChris Zankel
13399d5040eSMax Filippov	/* Save a1, a2, a3, and set SP. */
1345a0015d6SChris Zankel
135bc5378fcSMax Filippov	rsr	a0, depc
1365a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
1375a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2
1385a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
1395a0015d6SChris Zankel	mov	a1, a2
1405a0015d6SChris Zankel
1415a0015d6SChris Zankel	.globl _user_exception
1425a0015d6SChris Zankel_user_exception:
1435a0015d6SChris Zankel
1445a0015d6SChris Zankel	/* Save SAR and turn off single stepping */
1455a0015d6SChris Zankel
1465a0015d6SChris Zankel	movi	a2, 0
147b6569439SMax Filippov	wsr	a2, depc		# terminate user stack trace with 0
148bc5378fcSMax Filippov	rsr	a3, sar
149bc5378fcSMax Filippov	xsr	a2, icountlevel
1505a0015d6SChris Zankel	s32i	a3, a1, PT_SAR
15129c4dfd9SChris Zankel	s32i	a2, a1, PT_ICOUNTLEVEL
1525a0015d6SChris Zankel
153c50842dfSChris Zankel#if XCHAL_HAVE_THREADPTR
154c50842dfSChris Zankel	rur	a2, threadptr
155c50842dfSChris Zankel	s32i	a2, a1, PT_THREADPTR
156c50842dfSChris Zankel#endif
157c50842dfSChris Zankel
1585a0015d6SChris Zankel	/* Rotate ws so that the current windowbase is at bit0. */
1595a0015d6SChris Zankel	/* Assume ws = xxwww1yyyy. Rotate ws right, so that a2 = yyyyxxwww1 */
1605a0015d6SChris Zankel
161bc5378fcSMax Filippov	rsr	a2, windowbase
162bc5378fcSMax Filippov	rsr	a3, windowstart
1635a0015d6SChris Zankel	ssr	a2
1645a0015d6SChris Zankel	s32i	a2, a1, PT_WINDOWBASE
1655a0015d6SChris Zankel	s32i	a3, a1, PT_WINDOWSTART
1665a0015d6SChris Zankel	slli	a2, a3, 32-WSBITS
1675a0015d6SChris Zankel	src	a2, a3, a2
1685a0015d6SChris Zankel	srli	a2, a2, 32-WSBITS
1695a0015d6SChris Zankel	s32i	a2, a1, PT_WMASK	# needed for restoring registers
1705a0015d6SChris Zankel
1715a0015d6SChris Zankel	/* Save only live registers. */
1725a0015d6SChris Zankel
1735a0015d6SChris Zankel	_bbsi.l	a2, 1, 1f
1745a0015d6SChris Zankel	s32i	a4, a1, PT_AREG4
1755a0015d6SChris Zankel	s32i	a5, a1, PT_AREG5
1765a0015d6SChris Zankel	s32i	a6, a1, PT_AREG6
1775a0015d6SChris Zankel	s32i	a7, a1, PT_AREG7
1785a0015d6SChris Zankel	_bbsi.l	a2, 2, 1f
1795a0015d6SChris Zankel	s32i	a8, a1, PT_AREG8
1805a0015d6SChris Zankel	s32i	a9, a1, PT_AREG9
1815a0015d6SChris Zankel	s32i	a10, a1, PT_AREG10
1825a0015d6SChris Zankel	s32i	a11, a1, PT_AREG11
1835a0015d6SChris Zankel	_bbsi.l	a2, 3, 1f
1845a0015d6SChris Zankel	s32i	a12, a1, PT_AREG12
1855a0015d6SChris Zankel	s32i	a13, a1, PT_AREG13
1865a0015d6SChris Zankel	s32i	a14, a1, PT_AREG14
1875a0015d6SChris Zankel	s32i	a15, a1, PT_AREG15
1885a0015d6SChris Zankel	_bnei	a2, 1, 1f		# only one valid frame?
1895a0015d6SChris Zankel
1905a0015d6SChris Zankel	/* Only one valid frame, skip saving regs. */
1915a0015d6SChris Zankel
1925a0015d6SChris Zankel	j	2f
1935a0015d6SChris Zankel
1945a0015d6SChris Zankel	/* Save the remaining registers.
1955a0015d6SChris Zankel	 * We have to save all registers up to the first '1' from
1965a0015d6SChris Zankel	 * the right, except the current frame (bit 0).
1975a0015d6SChris Zankel	 * Assume a2 is:  001001000110001
1986656920bSChris Zankel	 * All register frames starting from the top field to the marked '1'
1995a0015d6SChris Zankel	 * must be saved.
2005a0015d6SChris Zankel	 */
2015a0015d6SChris Zankel
2025a0015d6SChris Zankel1:	addi	a3, a2, -1		# eliminate '1' in bit 0: yyyyxxww0
2035a0015d6SChris Zankel	neg	a3, a3			# yyyyxxww0 -> YYYYXXWW1+1
2045a0015d6SChris Zankel	and	a3, a3, a2		# max. only one bit is set
2055a0015d6SChris Zankel
2065a0015d6SChris Zankel	/* Find number of frames to save */
2075a0015d6SChris Zankel
2085a0015d6SChris Zankel	ffs_ws	a0, a3			# number of frames to the '1' from left
2095a0015d6SChris Zankel
2105a0015d6SChris Zankel	/* Store information into WMASK:
2115a0015d6SChris Zankel	 * bits 0..3: xxx1 masked lower 4 bits of the rotated windowstart,
2125a0015d6SChris Zankel	 * bits 4...: number of valid 4-register frames
2135a0015d6SChris Zankel	 */
2145a0015d6SChris Zankel
2155a0015d6SChris Zankel	slli	a3, a0, 4		# number of frames to save in bits 8..4
2165a0015d6SChris Zankel	extui	a2, a2, 0, 4		# mask for the first 16 registers
2175a0015d6SChris Zankel	or	a2, a3, a2
2185a0015d6SChris Zankel	s32i	a2, a1, PT_WMASK	# needed when we restore the reg-file
2195a0015d6SChris Zankel
2205a0015d6SChris Zankel	/* Save 4 registers at a time */
2215a0015d6SChris Zankel
2225a0015d6SChris Zankel1:	rotw	-1
2235a0015d6SChris Zankel	s32i	a0, a5, PT_AREG_END - 16
2245a0015d6SChris Zankel	s32i	a1, a5, PT_AREG_END - 12
2255a0015d6SChris Zankel	s32i	a2, a5, PT_AREG_END - 8
2265a0015d6SChris Zankel	s32i	a3, a5, PT_AREG_END - 4
2275a0015d6SChris Zankel	addi	a0, a4, -1
2285a0015d6SChris Zankel	addi	a1, a5, -16
2295a0015d6SChris Zankel	_bnez	a0, 1b
2305a0015d6SChris Zankel
2315a0015d6SChris Zankel	/* WINDOWBASE still in SAR! */
2325a0015d6SChris Zankel
233bc5378fcSMax Filippov	rsr	a2, sar			# original WINDOWBASE
2345a0015d6SChris Zankel	movi	a3, 1
2355a0015d6SChris Zankel	ssl	a2
2365a0015d6SChris Zankel	sll	a3, a3
237bc5378fcSMax Filippov	wsr	a3, windowstart		# set corresponding WINDOWSTART bit
238bc5378fcSMax Filippov	wsr	a2, windowbase		# and WINDOWSTART
2395a0015d6SChris Zankel	rsync
2405a0015d6SChris Zankel
2415a0015d6SChris Zankel	/* We are back to the original stack pointer (a1) */
2425a0015d6SChris Zankel
243c658eac6SChris Zankel2:	/* Now, jump to the common exception handler. */
2445a0015d6SChris Zankel
2455a0015d6SChris Zankel	j	common_exception
2465a0015d6SChris Zankel
247d1538c46SChris ZankelENDPROC(user_exception)
2485a0015d6SChris Zankel
2495a0015d6SChris Zankel/*
2505a0015d6SChris Zankel * First-level exit handler for kernel exceptions
2515a0015d6SChris Zankel * Save special registers and the live window frame.
2525a0015d6SChris Zankel * Note: Even though we changes the stack pointer, we don't have to do a
2535a0015d6SChris Zankel *	 MOVSP here, as we do that when we return from the exception.
2545a0015d6SChris Zankel *	 (See comment in the kernel exception exit code)
2555a0015d6SChris Zankel *
2565a0015d6SChris Zankel * Entry condition for kernel_exception:
2575a0015d6SChris Zankel *
2585a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
2595a0015d6SChris Zankel *   a1:	a1
2605a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
26199d5040eSMax Filippov *   a3:	a3
2625a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
26399d5040eSMax Filippov *   excsave_1:	dispatch table
2645a0015d6SChris Zankel *
2655a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
2665a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
2675a0015d6SChris Zankel *
2685a0015d6SChris Zankel * Entry condition for _kernel_exception:
2695a0015d6SChris Zankel *
2705a0015d6SChris Zankel *   a0-a3 and depc have been saved to PT_AREG0...PT_AREG3 and PT_DEPC
2715a0015d6SChris Zankel *   excsave has been restored, and
2725a0015d6SChris Zankel *   stack pointer (a1) has been set.
2735a0015d6SChris Zankel *
2743ad2f3fbSDaniel Mack * Note: _kernel_exception might be at an odd address. Don't use call0..call12
2755a0015d6SChris Zankel */
2765a0015d6SChris Zankel
2775a0015d6SChris ZankelENTRY(kernel_exception)
2785a0015d6SChris Zankel
27999d5040eSMax Filippov	/* Save a1, a2, a3, and set SP. */
2805a0015d6SChris Zankel
281bc5378fcSMax Filippov	rsr	a0, depc		# get a2
2825a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
2835a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2
2845a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
2855a0015d6SChris Zankel	mov	a1, a2
2865a0015d6SChris Zankel
2875a0015d6SChris Zankel	.globl _kernel_exception
2885a0015d6SChris Zankel_kernel_exception:
2895a0015d6SChris Zankel
2905a0015d6SChris Zankel	/* Save SAR and turn off single stepping */
2915a0015d6SChris Zankel
2925a0015d6SChris Zankel	movi	a2, 0
293bc5378fcSMax Filippov	rsr	a3, sar
294bc5378fcSMax Filippov	xsr	a2, icountlevel
2955a0015d6SChris Zankel	s32i	a3, a1, PT_SAR
29629c4dfd9SChris Zankel	s32i	a2, a1, PT_ICOUNTLEVEL
2975a0015d6SChris Zankel
2985a0015d6SChris Zankel	/* Rotate ws so that the current windowbase is at bit0. */
2995a0015d6SChris Zankel	/* Assume ws = xxwww1yyyy. Rotate ws right, so that a2 = yyyyxxwww1 */
3005a0015d6SChris Zankel
301bc5378fcSMax Filippov	rsr	a2, windowbase		# don't need to save these, we only
302bc5378fcSMax Filippov	rsr	a3, windowstart		# need shifted windowstart: windowmask
3035a0015d6SChris Zankel	ssr	a2
3045a0015d6SChris Zankel	slli	a2, a3, 32-WSBITS
3055a0015d6SChris Zankel	src	a2, a3, a2
3065a0015d6SChris Zankel	srli	a2, a2, 32-WSBITS
3075a0015d6SChris Zankel	s32i	a2, a1, PT_WMASK	# needed for kernel_exception_exit
3085a0015d6SChris Zankel
3095a0015d6SChris Zankel	/* Save only the live window-frame */
3105a0015d6SChris Zankel
3115a0015d6SChris Zankel	_bbsi.l	a2, 1, 1f
3125a0015d6SChris Zankel	s32i	a4, a1, PT_AREG4
3135a0015d6SChris Zankel	s32i	a5, a1, PT_AREG5
3145a0015d6SChris Zankel	s32i	a6, a1, PT_AREG6
3155a0015d6SChris Zankel	s32i	a7, a1, PT_AREG7
3165a0015d6SChris Zankel	_bbsi.l	a2, 2, 1f
3175a0015d6SChris Zankel	s32i	a8, a1, PT_AREG8
3185a0015d6SChris Zankel	s32i	a9, a1, PT_AREG9
3195a0015d6SChris Zankel	s32i	a10, a1, PT_AREG10
3205a0015d6SChris Zankel	s32i	a11, a1, PT_AREG11
3215a0015d6SChris Zankel	_bbsi.l	a2, 3, 1f
3225a0015d6SChris Zankel	s32i	a12, a1, PT_AREG12
3235a0015d6SChris Zankel	s32i	a13, a1, PT_AREG13
3245a0015d6SChris Zankel	s32i	a14, a1, PT_AREG14
3255a0015d6SChris Zankel	s32i	a15, a1, PT_AREG15
3265a0015d6SChris Zankel
327b6569439SMax Filippov	_bnei	a2, 1, 1f
328b6569439SMax Filippov
329b6569439SMax Filippov	/* Copy spill slots of a0 and a1 to imitate movsp
330b6569439SMax Filippov	 * in order to keep exception stack continuous
331b6569439SMax Filippov	 */
332b6569439SMax Filippov	l32i	a3, a1, PT_SIZE
333b6569439SMax Filippov	l32i	a0, a1, PT_SIZE + 4
334b6569439SMax Filippov	s32e	a3, a1, -16
335b6569439SMax Filippov	s32e	a0, a1, -12
3365a0015d6SChris Zankel1:
337b6569439SMax Filippov	l32i	a0, a1, PT_AREG0	# restore saved a0
338b6569439SMax Filippov	wsr	a0, depc
3395a0015d6SChris Zankel
3405a0015d6SChris Zankel#ifdef KERNEL_STACK_OVERFLOW_CHECK
3415a0015d6SChris Zankel
3425a0015d6SChris Zankel	/*  Stack overflow check, for debugging  */
3435a0015d6SChris Zankel	extui	a2, a1, TASK_SIZE_BITS,XX
3445a0015d6SChris Zankel	movi	a3, SIZE??
3455a0015d6SChris Zankel	_bge	a2, a3, out_of_stack_panic
3465a0015d6SChris Zankel
3475a0015d6SChris Zankel#endif
3485a0015d6SChris Zankel
3495a0015d6SChris Zankel/*
3505a0015d6SChris Zankel * This is the common exception handler.
3515a0015d6SChris Zankel * We get here from the user exception handler or simply by falling through
3525a0015d6SChris Zankel * from the kernel exception handler.
3535a0015d6SChris Zankel * Save the remaining special registers, switch to kernel mode, and jump
3545a0015d6SChris Zankel * to the second-level exception handler.
3555a0015d6SChris Zankel *
3565a0015d6SChris Zankel */
3575a0015d6SChris Zankel
3585a0015d6SChris Zankelcommon_exception:
3595a0015d6SChris Zankel
36029c4dfd9SChris Zankel	/* Save some registers, disable loops and clear the syscall flag. */
3615a0015d6SChris Zankel
362bc5378fcSMax Filippov	rsr	a2, debugcause
363bc5378fcSMax Filippov	rsr	a3, epc1
3645a0015d6SChris Zankel	s32i	a2, a1, PT_DEBUGCAUSE
3655a0015d6SChris Zankel	s32i	a3, a1, PT_PC
3665a0015d6SChris Zankel
3676a986984SMax Filippov	movi	a2, NO_SYSCALL
368bc5378fcSMax Filippov	rsr	a3, excvaddr
36929c4dfd9SChris Zankel	s32i	a2, a1, PT_SYSCALL
3705a0015d6SChris Zankel	movi	a2, 0
3715a0015d6SChris Zankel	s32i	a3, a1, PT_EXCVADDR
3725029615eSMax Filippov#if XCHAL_HAVE_LOOPS
373bc5378fcSMax Filippov	xsr	a2, lcount
3745a0015d6SChris Zankel	s32i	a2, a1, PT_LCOUNT
3755029615eSMax Filippov#endif
3765a0015d6SChris Zankel
377a0fc1436SMax Filippov#if XCHAL_HAVE_EXCLUSIVE
378a0fc1436SMax Filippov	/* Clear exclusive access monitor set by interrupted code */
379a0fc1436SMax Filippov	clrex
380a0fc1436SMax Filippov#endif
381a0fc1436SMax Filippov
3825a0015d6SChris Zankel	/* It is now save to restore the EXC_TABLE_FIXUP variable. */
3835a0015d6SChris Zankel
38438fef73cSMax Filippov	rsr	a2, exccause
3855a0015d6SChris Zankel	movi	a3, 0
38638fef73cSMax Filippov	rsr	a0, excsave1
38738fef73cSMax Filippov	s32i	a2, a1, PT_EXCCAUSE
38838fef73cSMax Filippov	s32i	a3, a0, EXC_TABLE_FIXUP
3895a0015d6SChris Zankel
390b6569439SMax Filippov	/* All unrecoverable states are saved on stack, now, and a1 is valid.
391b6569439SMax Filippov	 * Now we can allow exceptions again. In case we've got an interrupt
392b6569439SMax Filippov	 * PS.INTLEVEL is set to LOCKLEVEL disabling furhter interrupts,
393b6569439SMax Filippov	 * otherwise it's left unchanged.
3945a0015d6SChris Zankel	 *
395b6569439SMax Filippov	 * Set PS(EXCM = 0, UM = 0, RING = 0, OWB = 0, WOE = 1, INTLEVEL = X)
3965a0015d6SChris Zankel	 */
3975a0015d6SChris Zankel
398bc5378fcSMax Filippov	rsr	a3, ps
39938fef73cSMax Filippov	s32i	a3, a1, PT_PS		# save ps
40038fef73cSMax Filippov
40138fef73cSMax Filippov#if XTENSA_FAKE_NMI
40238fef73cSMax Filippov	/* Correct PS needs to be saved in the PT_PS:
40338fef73cSMax Filippov	 * - in case of exception or level-1 interrupt it's in the PS,
40438fef73cSMax Filippov	 *   and is already saved.
40538fef73cSMax Filippov	 * - in case of medium level interrupt it's in the excsave2.
40638fef73cSMax Filippov	 */
40738fef73cSMax Filippov	movi	a0, EXCCAUSE_MAPPED_NMI
40838fef73cSMax Filippov	extui	a3, a3, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
40938fef73cSMax Filippov	beq	a2, a0, .Lmedium_level_irq
41038fef73cSMax Filippov	bnei	a2, EXCCAUSE_LEVEL1_INTERRUPT, .Lexception
41138fef73cSMax Filippov	beqz	a3, .Llevel1_irq	# level-1 IRQ sets ps.intlevel to 0
41238fef73cSMax Filippov
41338fef73cSMax Filippov.Lmedium_level_irq:
41438fef73cSMax Filippov	rsr	a0, excsave2
41538fef73cSMax Filippov	s32i	a0, a1, PT_PS		# save medium-level interrupt ps
41638fef73cSMax Filippov	bgei	a3, LOCKLEVEL, .Lexception
41738fef73cSMax Filippov
41838fef73cSMax Filippov.Llevel1_irq:
41938fef73cSMax Filippov	movi	a3, LOCKLEVEL
42038fef73cSMax Filippov
42138fef73cSMax Filippov.Lexception:
4229e1e41c4SMax Filippov	movi	a0, PS_WOE_MASK
42338fef73cSMax Filippov	or	a3, a3, a0
42438fef73cSMax Filippov#else
42538fef73cSMax Filippov	addi	a2, a2, -EXCCAUSE_LEVEL1_INTERRUPT
42638fef73cSMax Filippov	movi	a0, LOCKLEVEL
4272d1c645cSMarc Gauthier	extui	a3, a3, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
4282d1c645cSMarc Gauthier					# a3 = PS.INTLEVEL
42938fef73cSMax Filippov	moveqz	a3, a0, a2		# a3 = LOCKLEVEL iff interrupt
4309e1e41c4SMax Filippov	movi	a2, PS_WOE_MASK
4315a0015d6SChris Zankel	or	a3, a3, a2
432b6569439SMax Filippov	rsr	a2, exccause
43338fef73cSMax Filippov#endif
43438fef73cSMax Filippov
435b6569439SMax Filippov	/* restore return address (or 0 if return to userspace) */
436b6569439SMax Filippov	rsr	a0, depc
43738fef73cSMax Filippov	wsr	a3, ps
43838fef73cSMax Filippov	rsync				# PS.WOE => rsync => overflow
4395a0015d6SChris Zankel
440bc5378fcSMax Filippov	/* Save lbeg, lend */
4415029615eSMax Filippov#if XCHAL_HAVE_LOOPS
442b6569439SMax Filippov	rsr	a4, lbeg
443bc5378fcSMax Filippov	rsr	a3, lend
444b6569439SMax Filippov	s32i	a4, a1, PT_LBEG
4455a0015d6SChris Zankel	s32i	a3, a1, PT_LEND
4465029615eSMax Filippov#endif
4475a0015d6SChris Zankel
448733536b8SMax Filippov	/* Save SCOMPARE1 */
449733536b8SMax Filippov
450733536b8SMax Filippov#if XCHAL_HAVE_S32C1I
451b6569439SMax Filippov	rsr     a3, scompare1
452b6569439SMax Filippov	s32i    a3, a1, PT_SCOMPARE1
453733536b8SMax Filippov#endif
454733536b8SMax Filippov
455c658eac6SChris Zankel	/* Save optional registers. */
456c658eac6SChris Zankel
457b6569439SMax Filippov	save_xtregs_opt a1 a3 a4 a5 a6 a7 PT_XTREGS_OPT
458c658eac6SChris Zankel
4595a0015d6SChris Zankel	/* Go to second-level dispatcher. Set up parameters to pass to the
4605a0015d6SChris Zankel	 * exception handler and call the exception handler.
4615a0015d6SChris Zankel	 */
4625a0015d6SChris Zankel
46399d5040eSMax Filippov	rsr	a4, excsave1
4645a0015d6SChris Zankel	mov	a6, a1			# pass stack frame
465b6569439SMax Filippov	mov	a7, a2			# pass EXCCAUSE
466b6569439SMax Filippov	addx4	a4, a2, a4
4675a0015d6SChris Zankel	l32i	a4, a4, EXC_TABLE_DEFAULT		# load handler
4685a0015d6SChris Zankel
4695a0015d6SChris Zankel	/* Call the second-level handler */
4705a0015d6SChris Zankel
4715a0015d6SChris Zankel	callx4	a4
4725a0015d6SChris Zankel
4735a0015d6SChris Zankel	/* Jump here for exception exit */
474e6ffe17eSdann	.global common_exception_return
4755a0015d6SChris Zankelcommon_exception_return:
4765a0015d6SChris Zankel
47738fef73cSMax Filippov#if XTENSA_FAKE_NMI
47838fef73cSMax Filippov	l32i	a2, a1, PT_EXCCAUSE
47938fef73cSMax Filippov	movi	a3, EXCCAUSE_MAPPED_NMI
48038fef73cSMax Filippov	beq	a2, a3, .LNMIexit
48138fef73cSMax Filippov#endif
482c92931b2SMax Filippov1:
48338fef73cSMax Filippov	irq_save a2, a3
4847d5f6a9aSMax Filippov#ifdef CONFIG_TRACE_IRQFLAGS
4852da03d41SMax Filippov	call4	trace_hardirqs_off
4867d5f6a9aSMax Filippov#endif
487c92931b2SMax Filippov
4885a0015d6SChris Zankel	/* Jump if we are returning from kernel exceptions. */
4895a0015d6SChris Zankel
490aea8e7c8SMax Filippov	l32i	a3, a1, PT_PS
49116c5becfSMax Filippov	GET_THREAD_INFO(a2, a1)
49216c5becfSMax Filippov	l32i	a4, a2, TI_FLAGS
49316c5becfSMax Filippov	_bbci.l	a3, PS_UM_BIT, 6f
4945a0015d6SChris Zankel
4955a0015d6SChris Zankel	/* Specific to a user exception exit:
4965a0015d6SChris Zankel	 * We need to check some flags for signal handling and rescheduling,
4975a0015d6SChris Zankel	 * and have to restore WB and WS, extra states, and all registers
4985a0015d6SChris Zankel	 * in the register file that were in use in the user task.
499e1088430SChris Zankel	 * Note that we don't disable interrupts here.
5005a0015d6SChris Zankel	 */
5015a0015d6SChris Zankel
5025a0015d6SChris Zankel	_bbsi.l	a4, TIF_NEED_RESCHED, 3f
503*bec58f40SJens Axboe	movi	a2, _TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_NOTIFY_SIGNAL
504*bec58f40SJens Axboe	bnone	a4, a2, 5f
5055a0015d6SChris Zankel
506a53bb24eSAl Viro2:	l32i	a4, a1, PT_DEPC
5075a0015d6SChris Zankel	bgeui	a4, VALID_DOUBLE_EXCEPTION_ADDRESS, 4f
5085a0015d6SChris Zankel
509e1088430SChris Zankel	/* Call do_signal() */
510e1088430SChris Zankel
5117d5f6a9aSMax Filippov#ifdef CONFIG_TRACE_IRQFLAGS
5122da03d41SMax Filippov	call4	trace_hardirqs_on
5137d5f6a9aSMax Filippov#endif
514aea8e7c8SMax Filippov	rsil	a2, 0
5155a0015d6SChris Zankel	mov	a6, a1
5162da03d41SMax Filippov	call4	do_notify_resume	# int do_notify_resume(struct pt_regs*)
5175a0015d6SChris Zankel	j	1b
5185a0015d6SChris Zankel
519e1088430SChris Zankel3:	/* Reschedule */
5205a0015d6SChris Zankel
5217d5f6a9aSMax Filippov#ifdef CONFIG_TRACE_IRQFLAGS
5222da03d41SMax Filippov	call4	trace_hardirqs_on
5237d5f6a9aSMax Filippov#endif
524aea8e7c8SMax Filippov	rsil	a2, 0
5252da03d41SMax Filippov	call4	schedule	# void schedule (void)
5265a0015d6SChris Zankel	j	1b
5275a0015d6SChris Zankel
5286c5260d7SThomas Gleixner#ifdef CONFIG_PREEMPTION
52916c5becfSMax Filippov6:
53016c5becfSMax Filippov	_bbci.l	a4, TIF_NEED_RESCHED, 4f
53116c5becfSMax Filippov
53216c5becfSMax Filippov	/* Check current_thread_info->preempt_count */
53316c5becfSMax Filippov
53416c5becfSMax Filippov	l32i	a4, a2, TI_PRE_COUNT
53516c5becfSMax Filippov	bnez	a4, 4f
5362da03d41SMax Filippov	call4	preempt_schedule_irq
5378b5d7e52SValentin Schneider	j	4f
53816c5becfSMax Filippov#endif
53916c5becfSMax Filippov
54038fef73cSMax Filippov#if XTENSA_FAKE_NMI
54138fef73cSMax Filippov.LNMIexit:
54238fef73cSMax Filippov	l32i	a3, a1, PT_PS
54338fef73cSMax Filippov	_bbci.l	a3, PS_UM_BIT, 4f
54438fef73cSMax Filippov#endif
54538fef73cSMax Filippov
546a99e07eeSMax Filippov5:
547c91e02bdSMax Filippov#ifdef CONFIG_HAVE_HW_BREAKPOINT
548c91e02bdSMax Filippov	_bbci.l	a4, TIF_DB_DISABLED, 7f
5492da03d41SMax Filippov	call4	restore_dbreak
550c91e02bdSMax Filippov7:
551c91e02bdSMax Filippov#endif
552a99e07eeSMax Filippov#ifdef CONFIG_DEBUG_TLB_SANITY
553a99e07eeSMax Filippov	l32i	a4, a1, PT_DEPC
554a99e07eeSMax Filippov	bgeui	a4, VALID_DOUBLE_EXCEPTION_ADDRESS, 4f
5552da03d41SMax Filippov	call4	check_tlb_sanity
556a99e07eeSMax Filippov#endif
55716c5becfSMax Filippov6:
558aea8e7c8SMax Filippov4:
559aea8e7c8SMax Filippov#ifdef CONFIG_TRACE_IRQFLAGS
5607d5f6a9aSMax Filippov	extui	a4, a3, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
5617d5f6a9aSMax Filippov	bgei	a4, LOCKLEVEL, 1f
5622da03d41SMax Filippov	call4	trace_hardirqs_on
563aea8e7c8SMax Filippov1:
564aea8e7c8SMax Filippov#endif
565aea8e7c8SMax Filippov	/* Restore optional registers. */
5665a0015d6SChris Zankel
567e1088430SChris Zankel	load_xtregs_opt a1 a2 a4 a5 a6 a7 PT_XTREGS_OPT
568e1088430SChris Zankel
569733536b8SMax Filippov	/* Restore SCOMPARE1 */
570733536b8SMax Filippov
571733536b8SMax Filippov#if XCHAL_HAVE_S32C1I
572733536b8SMax Filippov	l32i    a2, a1, PT_SCOMPARE1
573733536b8SMax Filippov	wsr     a2, scompare1
574733536b8SMax Filippov#endif
575bc5378fcSMax Filippov	wsr	a3, ps		/* disable interrupts */
576e1088430SChris Zankel
577e1088430SChris Zankel	_bbci.l	a3, PS_UM_BIT, kernel_exception_exit
578e1088430SChris Zankel
579e1088430SChris Zankeluser_exception_exit:
580e1088430SChris Zankel
581e1088430SChris Zankel	/* Restore the state of the task and return from the exception. */
5825a0015d6SChris Zankel
5835a0015d6SChris Zankel	/* Switch to the user thread WINDOWBASE. Save SP temporarily in DEPC */
5845a0015d6SChris Zankel
5855a0015d6SChris Zankel	l32i	a2, a1, PT_WINDOWBASE
5865a0015d6SChris Zankel	l32i	a3, a1, PT_WINDOWSTART
587bc5378fcSMax Filippov	wsr	a1, depc		# use DEPC as temp storage
588bc5378fcSMax Filippov	wsr	a3, windowstart		# restore WINDOWSTART
5895a0015d6SChris Zankel	ssr	a2			# preserve user's WB in the SAR
590bc5378fcSMax Filippov	wsr	a2, windowbase		# switch to user's saved WB
5915a0015d6SChris Zankel	rsync
592bc5378fcSMax Filippov	rsr	a1, depc		# restore stack pointer
5935a0015d6SChris Zankel	l32i	a2, a1, PT_WMASK	# register frames saved (in bits 4...9)
5945a0015d6SChris Zankel	rotw	-1			# we restore a4..a7
5955a0015d6SChris Zankel	_bltui	a6, 16, 1f		# only have to restore current window?
5965a0015d6SChris Zankel
5975a0015d6SChris Zankel	/* The working registers are a0 and a3.  We are restoring to
5985a0015d6SChris Zankel	 * a4..a7.  Be careful not to destroy what we have just restored.
5995a0015d6SChris Zankel	 * Note: wmask has the format YYYYM:
6005a0015d6SChris Zankel	 *       Y: number of registers saved in groups of 4
6015a0015d6SChris Zankel	 *       M: 4 bit mask of first 16 registers
6025a0015d6SChris Zankel	 */
6035a0015d6SChris Zankel
6045a0015d6SChris Zankel	mov	a2, a6
6055a0015d6SChris Zankel	mov	a3, a5
6065a0015d6SChris Zankel
6075a0015d6SChris Zankel2:	rotw	-1			# a0..a3 become a4..a7
6085a0015d6SChris Zankel	addi	a3, a7, -4*4		# next iteration
6095a0015d6SChris Zankel	addi	a2, a6, -16		# decrementing Y in WMASK
6105a0015d6SChris Zankel	l32i	a4, a3, PT_AREG_END + 0
6115a0015d6SChris Zankel	l32i	a5, a3, PT_AREG_END + 4
6125a0015d6SChris Zankel	l32i	a6, a3, PT_AREG_END + 8
6135a0015d6SChris Zankel	l32i	a7, a3, PT_AREG_END + 12
6145a0015d6SChris Zankel	_bgeui	a2, 16, 2b
6155a0015d6SChris Zankel
6165a0015d6SChris Zankel	/* Clear unrestored registers (don't leak anything to user-land */
6175a0015d6SChris Zankel
618bc5378fcSMax Filippov1:	rsr	a0, windowbase
619bc5378fcSMax Filippov	rsr	a3, sar
6205a0015d6SChris Zankel	sub	a3, a0, a3
6215a0015d6SChris Zankel	beqz	a3, 2f
6225a0015d6SChris Zankel	extui	a3, a3, 0, WBBITS
6235a0015d6SChris Zankel
6245a0015d6SChris Zankel1:	rotw	-1
6255a0015d6SChris Zankel	addi	a3, a7, -1
6265a0015d6SChris Zankel	movi	a4, 0
6275a0015d6SChris Zankel	movi	a5, 0
6285a0015d6SChris Zankel	movi	a6, 0
6295a0015d6SChris Zankel	movi	a7, 0
6305a0015d6SChris Zankel	bgei	a3, 1, 1b
6315a0015d6SChris Zankel
6325a0015d6SChris Zankel	/* We are back were we were when we started.
6335a0015d6SChris Zankel	 * Note: a2 still contains WMASK (if we've returned to the original
6345a0015d6SChris Zankel	 *	 frame where we had loaded a2), or at least the lower 4 bits
6355a0015d6SChris Zankel	 *	 (if we have restored WSBITS-1 frames).
6365a0015d6SChris Zankel	 */
6375a0015d6SChris Zankel
6384229fb12SMax Filippov2:
639c50842dfSChris Zankel#if XCHAL_HAVE_THREADPTR
640c50842dfSChris Zankel	l32i	a3, a1, PT_THREADPTR
641c50842dfSChris Zankel	wur	a3, threadptr
642c50842dfSChris Zankel#endif
643c50842dfSChris Zankel
6444229fb12SMax Filippov	j	common_exception_exit
6455a0015d6SChris Zankel
6465a0015d6SChris Zankel	/* This is the kernel exception exit.
6475a0015d6SChris Zankel	 * We avoided to do a MOVSP when we entered the exception, but we
6485a0015d6SChris Zankel	 * have to do it here.
6495a0015d6SChris Zankel	 */
6505a0015d6SChris Zankel
6515a0015d6SChris Zankelkernel_exception_exit:
6525a0015d6SChris Zankel
6535a0015d6SChris Zankel	/* Check if we have to do a movsp.
6545a0015d6SChris Zankel	 *
6555a0015d6SChris Zankel	 * We only have to do a movsp if the previous window-frame has
6565a0015d6SChris Zankel	 * been spilled to the *temporary* exception stack instead of the
6575a0015d6SChris Zankel	 * task's stack. This is the case if the corresponding bit in
6585a0015d6SChris Zankel	 * WINDOWSTART for the previous window-frame was set before
6595a0015d6SChris Zankel	 * (not spilled) but is zero now (spilled).
6605a0015d6SChris Zankel	 * If this bit is zero, all other bits except the one for the
6615a0015d6SChris Zankel	 * current window frame are also zero. So, we can use a simple test:
6625a0015d6SChris Zankel	 * 'and' WINDOWSTART and WINDOWSTART-1:
6635a0015d6SChris Zankel	 *
6645a0015d6SChris Zankel	 *  (XXXXXX1[0]* - 1) AND XXXXXX1[0]* = XXXXXX0[0]*
6655a0015d6SChris Zankel	 *
6665a0015d6SChris Zankel	 * The result is zero only if one bit was set.
6675a0015d6SChris Zankel	 *
6685a0015d6SChris Zankel	 * (Note: We might have gone through several task switches before
6695a0015d6SChris Zankel	 *        we come back to the current task, so WINDOWBASE might be
6705a0015d6SChris Zankel	 *        different from the time the exception occurred.)
6715a0015d6SChris Zankel	 */
6725a0015d6SChris Zankel
6735a0015d6SChris Zankel	/* Test WINDOWSTART before and after the exception.
6745a0015d6SChris Zankel	 * We actually have WMASK, so we only have to test if it is 1 or not.
6755a0015d6SChris Zankel	 */
6765a0015d6SChris Zankel
6775a0015d6SChris Zankel	l32i	a2, a1, PT_WMASK
6785a0015d6SChris Zankel	_beqi	a2, 1, common_exception_exit	# Spilled before exception,jump
6795a0015d6SChris Zankel
6805a0015d6SChris Zankel	/* Test WINDOWSTART now. If spilled, do the movsp */
6815a0015d6SChris Zankel
682bc5378fcSMax Filippov	rsr     a3, windowstart
6835a0015d6SChris Zankel	addi	a0, a3, -1
6845a0015d6SChris Zankel	and     a3, a3, a0
6855a0015d6SChris Zankel	_bnez	a3, common_exception_exit
6865a0015d6SChris Zankel
6875a0015d6SChris Zankel	/* Do a movsp (we returned from a call4, so we have at least a0..a7) */
6885a0015d6SChris Zankel
6895a0015d6SChris Zankel	addi    a0, a1, -16
6905a0015d6SChris Zankel	l32i    a3, a0, 0
6915a0015d6SChris Zankel	l32i    a4, a0, 4
6925a0015d6SChris Zankel	s32i    a3, a1, PT_SIZE+0
6935a0015d6SChris Zankel	s32i    a4, a1, PT_SIZE+4
6945a0015d6SChris Zankel	l32i    a3, a0, 8
6955a0015d6SChris Zankel	l32i    a4, a0, 12
6965a0015d6SChris Zankel	s32i    a3, a1, PT_SIZE+8
6975a0015d6SChris Zankel	s32i    a4, a1, PT_SIZE+12
6985a0015d6SChris Zankel
6995a0015d6SChris Zankel	/* Common exception exit.
7005a0015d6SChris Zankel	 * We restore the special register and the current window frame, and
7015a0015d6SChris Zankel	 * return from the exception.
7025a0015d6SChris Zankel	 *
7035a0015d6SChris Zankel	 * Note: We expect a2 to hold PT_WMASK
7045a0015d6SChris Zankel	 */
7055a0015d6SChris Zankel
7065a0015d6SChris Zankelcommon_exception_exit:
7075a0015d6SChris Zankel
708c658eac6SChris Zankel	/* Restore address registers. */
709c658eac6SChris Zankel
7105a0015d6SChris Zankel	_bbsi.l	a2, 1, 1f
7115a0015d6SChris Zankel	l32i	a4,  a1, PT_AREG4
7125a0015d6SChris Zankel	l32i	a5,  a1, PT_AREG5
7135a0015d6SChris Zankel	l32i	a6,  a1, PT_AREG6
7145a0015d6SChris Zankel	l32i	a7,  a1, PT_AREG7
7155a0015d6SChris Zankel	_bbsi.l	a2, 2, 1f
7165a0015d6SChris Zankel	l32i	a8,  a1, PT_AREG8
7175a0015d6SChris Zankel	l32i	a9,  a1, PT_AREG9
7185a0015d6SChris Zankel	l32i	a10, a1, PT_AREG10
7195a0015d6SChris Zankel	l32i	a11, a1, PT_AREG11
7205a0015d6SChris Zankel	_bbsi.l	a2, 3, 1f
7215a0015d6SChris Zankel	l32i	a12, a1, PT_AREG12
7225a0015d6SChris Zankel	l32i	a13, a1, PT_AREG13
7235a0015d6SChris Zankel	l32i	a14, a1, PT_AREG14
7245a0015d6SChris Zankel	l32i	a15, a1, PT_AREG15
7255a0015d6SChris Zankel
7265a0015d6SChris Zankel	/* Restore PC, SAR */
7275a0015d6SChris Zankel
7285a0015d6SChris Zankel1:	l32i	a2, a1, PT_PC
7295a0015d6SChris Zankel	l32i	a3, a1, PT_SAR
730bc5378fcSMax Filippov	wsr	a2, epc1
731bc5378fcSMax Filippov	wsr	a3, sar
7325a0015d6SChris Zankel
7335a0015d6SChris Zankel	/* Restore LBEG, LEND, LCOUNT */
7345029615eSMax Filippov#if XCHAL_HAVE_LOOPS
7355a0015d6SChris Zankel	l32i	a2, a1, PT_LBEG
7365a0015d6SChris Zankel	l32i	a3, a1, PT_LEND
737bc5378fcSMax Filippov	wsr	a2, lbeg
7385a0015d6SChris Zankel	l32i	a2, a1, PT_LCOUNT
739bc5378fcSMax Filippov	wsr	a3, lend
740bc5378fcSMax Filippov	wsr	a2, lcount
7415029615eSMax Filippov#endif
7425a0015d6SChris Zankel
74329c4dfd9SChris Zankel	/* We control single stepping through the ICOUNTLEVEL register. */
74429c4dfd9SChris Zankel
74529c4dfd9SChris Zankel	l32i	a2, a1, PT_ICOUNTLEVEL
74629c4dfd9SChris Zankel	movi	a3, -2
747bc5378fcSMax Filippov	wsr	a2, icountlevel
748bc5378fcSMax Filippov	wsr	a3, icount
74929c4dfd9SChris Zankel
7505a0015d6SChris Zankel	/* Check if it was double exception. */
7515a0015d6SChris Zankel
7525a0015d6SChris Zankel	l32i	a0, a1, PT_DEPC
7535a0015d6SChris Zankel	l32i	a3, a1, PT_AREG3
7542d1c645cSMarc Gauthier	l32i	a2, a1, PT_AREG2
755895666a9SMax Filippov	_bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
7562d1c645cSMarc Gauthier
7575a0015d6SChris Zankel	/* Restore a0...a3 and return */
7585a0015d6SChris Zankel
7595a0015d6SChris Zankel	l32i	a0, a1, PT_AREG0
7605a0015d6SChris Zankel	l32i	a1, a1, PT_AREG1
7615a0015d6SChris Zankel	rfe
7625a0015d6SChris Zankel
763895666a9SMax Filippov1: 	wsr	a0, depc
764895666a9SMax Filippov	l32i	a0, a1, PT_AREG0
765895666a9SMax Filippov	l32i	a1, a1, PT_AREG1
766895666a9SMax Filippov	rfde
7675a0015d6SChris Zankel
768d1538c46SChris ZankelENDPROC(kernel_exception)
769d1538c46SChris Zankel
7705a0015d6SChris Zankel/*
7715a0015d6SChris Zankel * Debug exception handler.
7725a0015d6SChris Zankel *
7735a0015d6SChris Zankel * Currently, we don't support KGDB, so only user application can be debugged.
7745a0015d6SChris Zankel *
7755a0015d6SChris Zankel * When we get here,  a0 is trashed and saved to excsave[debuglevel]
7765a0015d6SChris Zankel */
7775a0015d6SChris Zankel
778f8f02ca7SMax Filippov	.literal_position
779f8f02ca7SMax Filippov
7805a0015d6SChris ZankelENTRY(debug_exception)
7815a0015d6SChris Zankel
782bc5378fcSMax Filippov	rsr	a0, SREG_EPS + XCHAL_DEBUGLEVEL
783173d6681SChris Zankel	bbsi.l	a0, PS_EXCM_BIT, 1f	# exception mode
7845a0015d6SChris Zankel
785bc5378fcSMax Filippov	/* Set EPC1 and EXCCAUSE */
7865a0015d6SChris Zankel
787bc5378fcSMax Filippov	wsr	a2, depc		# save a2 temporarily
788bc5378fcSMax Filippov	rsr	a2, SREG_EPC + XCHAL_DEBUGLEVEL
789bc5378fcSMax Filippov	wsr	a2, epc1
7905a0015d6SChris Zankel
7915a0015d6SChris Zankel	movi	a2, EXCCAUSE_MAPPED_DEBUG
792bc5378fcSMax Filippov	wsr	a2, exccause
7935a0015d6SChris Zankel
7945a0015d6SChris Zankel	/* Restore PS to the value before the debug exc but with PS.EXCM set.*/
7955a0015d6SChris Zankel
796173d6681SChris Zankel	movi	a2, 1 << PS_EXCM_BIT
7975a0015d6SChris Zankel	or	a2, a0, a2
798bc5378fcSMax Filippov	wsr	a2, ps
7995a0015d6SChris Zankel
8005a0015d6SChris Zankel	/* Switch to kernel/user stack, restore jump vector, and save a0 */
8015a0015d6SChris Zankel
802173d6681SChris Zankel	bbsi.l	a2, PS_UM_BIT, 2f	# jump if user mode
8035a0015d6SChris Zankel
8045a0015d6SChris Zankel	addi	a2, a1, -16-PT_SIZE	# assume kernel stack
8056ec7026aSMax Filippov3:
8066ec7026aSMax Filippov	l32i	a0, a3, DT_DEBUG_SAVE
8076ec7026aSMax Filippov	s32i	a1, a2, PT_AREG1
8085a0015d6SChris Zankel	s32i	a0, a2, PT_AREG0
8095a0015d6SChris Zankel	movi	a0, 0
8105a0015d6SChris Zankel	s32i	a0, a2, PT_DEPC		# mark it as a regular exception
8116ec7026aSMax Filippov	xsr	a3, SREG_EXCSAVE + XCHAL_DEBUGLEVEL
812bc5378fcSMax Filippov	xsr	a0, depc
8135a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
8145a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2
8155a0015d6SChris Zankel	mov	a1, a2
8166ec7026aSMax Filippov
817c91e02bdSMax Filippov	/* Debug exception is handled as an exception, so interrupts will
818c91e02bdSMax Filippov	 * likely be enabled in the common exception handler. Disable
819c91e02bdSMax Filippov	 * preemption if we have HW breakpoints to preserve DEBUGCAUSE.DBNUM
820c91e02bdSMax Filippov	 * meaning.
821c91e02bdSMax Filippov	 */
822c91e02bdSMax Filippov#if defined(CONFIG_PREEMPT_COUNT) && defined(CONFIG_HAVE_HW_BREAKPOINT)
823c91e02bdSMax Filippov	GET_THREAD_INFO(a2, a1)
824c91e02bdSMax Filippov	l32i	a3, a2, TI_PRE_COUNT
825c91e02bdSMax Filippov	addi	a3, a3, 1
826c91e02bdSMax Filippov	s32i	a3, a2, TI_PRE_COUNT
827c91e02bdSMax Filippov#endif
828c91e02bdSMax Filippov
8296ec7026aSMax Filippov	rsr	a2, ps
8306ec7026aSMax Filippov	bbsi.l	a2, PS_UM_BIT, _user_exception
8315a0015d6SChris Zankel	j	_kernel_exception
8325a0015d6SChris Zankel
833bc5378fcSMax Filippov2:	rsr	a2, excsave1
8345a0015d6SChris Zankel	l32i	a2, a2, EXC_TABLE_KSTK	# load kernel stack pointer
8356ec7026aSMax Filippov	j	3b
8365a0015d6SChris Zankel
837c91e02bdSMax Filippov#ifdef CONFIG_HAVE_HW_BREAKPOINT
838c91e02bdSMax Filippov	/* Debug exception while in exception mode. This may happen when
839c91e02bdSMax Filippov	 * window overflow/underflow handler or fast exception handler hits
840c91e02bdSMax Filippov	 * data breakpoint, in which case save and disable all data
841c91e02bdSMax Filippov	 * breakpoints, single-step faulting instruction and restore data
842c91e02bdSMax Filippov	 * breakpoints.
843c91e02bdSMax Filippov	 */
844c91e02bdSMax Filippov1:
845c91e02bdSMax Filippov	bbci.l	a0, PS_UM_BIT, 1b	# jump if kernel mode
846c91e02bdSMax Filippov
847c91e02bdSMax Filippov	rsr	a0, debugcause
848c91e02bdSMax Filippov	bbsi.l	a0, DEBUGCAUSE_DBREAK_BIT, .Ldebug_save_dbreak
849c91e02bdSMax Filippov
850c91e02bdSMax Filippov	.set	_index, 0
851c91e02bdSMax Filippov	.rept	XCHAL_NUM_DBREAK
852c91e02bdSMax Filippov	l32i	a0, a3, DT_DBREAKC_SAVE + _index * 4
853c91e02bdSMax Filippov	wsr	a0, SREG_DBREAKC + _index
854c91e02bdSMax Filippov	.set	_index, _index + 1
855c91e02bdSMax Filippov	.endr
856c91e02bdSMax Filippov
857c91e02bdSMax Filippov	l32i	a0, a3, DT_ICOUNT_LEVEL_SAVE
858c91e02bdSMax Filippov	wsr	a0, icountlevel
859c91e02bdSMax Filippov
860c91e02bdSMax Filippov	l32i	a0, a3, DT_ICOUNT_SAVE
861c91e02bdSMax Filippov	xsr	a0, icount
862c91e02bdSMax Filippov
863c91e02bdSMax Filippov	l32i	a0, a3, DT_DEBUG_SAVE
864c91e02bdSMax Filippov	xsr	a3, SREG_EXCSAVE + XCHAL_DEBUGLEVEL
865c91e02bdSMax Filippov	rfi	XCHAL_DEBUGLEVEL
866c91e02bdSMax Filippov
867c91e02bdSMax Filippov.Ldebug_save_dbreak:
868c91e02bdSMax Filippov	.set	_index, 0
869c91e02bdSMax Filippov	.rept	XCHAL_NUM_DBREAK
870c91e02bdSMax Filippov	movi	a0, 0
871c91e02bdSMax Filippov	xsr	a0, SREG_DBREAKC + _index
872c91e02bdSMax Filippov	s32i	a0, a3, DT_DBREAKC_SAVE + _index * 4
873c91e02bdSMax Filippov	.set	_index, _index + 1
874c91e02bdSMax Filippov	.endr
875c91e02bdSMax Filippov
876c91e02bdSMax Filippov	movi	a0, XCHAL_EXCM_LEVEL + 1
877c91e02bdSMax Filippov	xsr	a0, icountlevel
878c91e02bdSMax Filippov	s32i	a0, a3, DT_ICOUNT_LEVEL_SAVE
879c91e02bdSMax Filippov
880c91e02bdSMax Filippov	movi	a0, 0xfffffffe
881c91e02bdSMax Filippov	xsr	a0, icount
882c91e02bdSMax Filippov	s32i	a0, a3, DT_ICOUNT_SAVE
883c91e02bdSMax Filippov
884c91e02bdSMax Filippov	l32i	a0, a3, DT_DEBUG_SAVE
885c91e02bdSMax Filippov	xsr	a3, SREG_EXCSAVE + XCHAL_DEBUGLEVEL
886c91e02bdSMax Filippov	rfi	XCHAL_DEBUGLEVEL
887c91e02bdSMax Filippov#else
888c91e02bdSMax Filippov	/* Debug exception while in exception mode. Should not happen. */
8895a0015d6SChris Zankel1:	j	1b	// FIXME!!
890c91e02bdSMax Filippov#endif
8915a0015d6SChris Zankel
892d1538c46SChris ZankelENDPROC(debug_exception)
8935a0015d6SChris Zankel
8945a0015d6SChris Zankel/*
8955a0015d6SChris Zankel * We get here in case of an unrecoverable exception.
8965a0015d6SChris Zankel * The only thing we can do is to be nice and print a panic message.
8975a0015d6SChris Zankel * We only produce a single stack frame for panic, so ???
8985a0015d6SChris Zankel *
8995a0015d6SChris Zankel *
9005a0015d6SChris Zankel * Entry conditions:
9015a0015d6SChris Zankel *
9025a0015d6SChris Zankel *   - a0 contains the caller address; original value saved in excsave1.
9035a0015d6SChris Zankel *   - the original a0 contains a valid return address (backtrace) or 0.
9045a0015d6SChris Zankel *   - a2 contains a valid stackpointer
9055a0015d6SChris Zankel *
9065a0015d6SChris Zankel * Notes:
9075a0015d6SChris Zankel *
9085a0015d6SChris Zankel *   - If the stack pointer could be invalid, the caller has to setup a
9095a0015d6SChris Zankel *     dummy stack pointer (e.g. the stack of the init_task)
9105a0015d6SChris Zankel *
9115a0015d6SChris Zankel *   - If the return address could be invalid, the caller has to set it
9125a0015d6SChris Zankel *     to 0, so the backtrace would stop.
9135a0015d6SChris Zankel *
9145a0015d6SChris Zankel */
9155a0015d6SChris Zankel	.align 4
9165a0015d6SChris Zankelunrecoverable_text:
9175a0015d6SChris Zankel	.ascii "Unrecoverable error in exception handler\0"
9185a0015d6SChris Zankel
919f8f02ca7SMax Filippov	.literal_position
920f8f02ca7SMax Filippov
9215a0015d6SChris ZankelENTRY(unrecoverable_exception)
9225a0015d6SChris Zankel
9235a0015d6SChris Zankel	movi	a0, 1
9245a0015d6SChris Zankel	movi	a1, 0
9255a0015d6SChris Zankel
926bc5378fcSMax Filippov	wsr	a0, windowstart
927bc5378fcSMax Filippov	wsr	a1, windowbase
9285a0015d6SChris Zankel	rsync
9295a0015d6SChris Zankel
9309e1e41c4SMax Filippov	movi	a1, PS_WOE_MASK | LOCKLEVEL
931bc5378fcSMax Filippov	wsr	a1, ps
9325a0015d6SChris Zankel	rsync
9335a0015d6SChris Zankel
9345a0015d6SChris Zankel	movi	a1, init_task
9355a0015d6SChris Zankel	movi	a0, 0
9365a0015d6SChris Zankel	addi	a1, a1, PT_REGS_OFFSET
9375a0015d6SChris Zankel
9385a0015d6SChris Zankel	movi	a6, unrecoverable_text
9392da03d41SMax Filippov	call4	panic
9405a0015d6SChris Zankel
9415a0015d6SChris Zankel1:	j	1b
9425a0015d6SChris Zankel
943d1538c46SChris ZankelENDPROC(unrecoverable_exception)
9445a0015d6SChris Zankel
9455a0015d6SChris Zankel/* -------------------------- FAST EXCEPTION HANDLERS ----------------------- */
9465a0015d6SChris Zankel
94750722f0bSMax Filippov	__XTENSA_HANDLER
94850722f0bSMax Filippov	.literal_position
94950722f0bSMax Filippov
9505a0015d6SChris Zankel/*
9515a0015d6SChris Zankel * Fast-handler for alloca exceptions
9525a0015d6SChris Zankel *
9535a0015d6SChris Zankel *  The ALLOCA handler is entered when user code executes the MOVSP
9545a0015d6SChris Zankel *  instruction and the caller's frame is not in the register file.
9555a0015d6SChris Zankel *
956fff96d69SMax Filippov * This algorithm was taken from the Ross Morley's RTOS Porting Layer:
957fff96d69SMax Filippov *
958fff96d69SMax Filippov *    /home/ross/rtos/porting/XtensaRTOS-PortingLayer-20090507/xtensa_vectors.S
959fff96d69SMax Filippov *
960fff96d69SMax Filippov * It leverages the existing window spill/fill routines and their support for
961fff96d69SMax Filippov * double exceptions. The 'movsp' instruction will only cause an exception if
962fff96d69SMax Filippov * the next window needs to be loaded. In fact this ALLOCA exception may be
963fff96d69SMax Filippov * replaced at some point by changing the hardware to do a underflow exception
964fff96d69SMax Filippov * of the proper size instead.
965fff96d69SMax Filippov *
966fff96d69SMax Filippov * This algorithm simply backs out the register changes started by the user
9673ead2f97SChris Packham * exception handler, makes it appear that we have started a window underflow
968fff96d69SMax Filippov * by rotating the window back and then setting the old window base (OWB) in
969fff96d69SMax Filippov * the 'ps' register with the rolled back window base. The 'movsp' instruction
970fff96d69SMax Filippov * will be re-executed and this time since the next window frames is in the
971fff96d69SMax Filippov * active AR registers it won't cause an exception.
972fff96d69SMax Filippov *
973fff96d69SMax Filippov * If the WindowUnderflow code gets a TLB miss the page will get mapped
9743ead2f97SChris Packham * the partial WindowUnderflow will be handled in the double exception
975fff96d69SMax Filippov * handler.
9765a0015d6SChris Zankel *
9775a0015d6SChris Zankel * Entry condition:
9785a0015d6SChris Zankel *
9795a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
9805a0015d6SChris Zankel *   a1:	a1
9815a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
98299d5040eSMax Filippov *   a3:	a3
9835a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
98499d5040eSMax Filippov *   excsave_1:	dispatch table
9855a0015d6SChris Zankel *
9865a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
9875a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
9885a0015d6SChris Zankel */
9895a0015d6SChris Zankel
9905a0015d6SChris ZankelENTRY(fast_alloca)
991fff96d69SMax Filippov	rsr	a0, windowbase
992fff96d69SMax Filippov	rotw	-1
993fff96d69SMax Filippov	rsr	a2, ps
994fff96d69SMax Filippov	extui	a3, a2, PS_OWB_SHIFT, PS_OWB_WIDTH
995fff96d69SMax Filippov	xor	a3, a3, a4
996fff96d69SMax Filippov	l32i	a4, a6, PT_AREG0
997fff96d69SMax Filippov	l32i	a1, a6, PT_DEPC
998fff96d69SMax Filippov	rsr	a6, depc
999fff96d69SMax Filippov	wsr	a1, depc
1000fff96d69SMax Filippov	slli	a3, a3, PS_OWB_SHIFT
1001fff96d69SMax Filippov	xor	a2, a2, a3
1002fff96d69SMax Filippov	wsr	a2, ps
1003fff96d69SMax Filippov	rsync
10045a0015d6SChris Zankel
1005fff96d69SMax Filippov	_bbci.l	a4, 31, 4f
1006fff96d69SMax Filippov	rotw	-1
1007fff96d69SMax Filippov	_bbci.l	a8, 30, 8f
1008fff96d69SMax Filippov	rotw	-1
1009fff96d69SMax Filippov	j	_WindowUnderflow12
1010fff96d69SMax Filippov8:	j	_WindowUnderflow8
1011fff96d69SMax Filippov4:	j	_WindowUnderflow4
1012d1538c46SChris ZankelENDPROC(fast_alloca)
10135a0015d6SChris Zankel
101409f8a6dbSMax Filippov#ifdef CONFIG_USER_ABI_CALL0_PROBE
101509f8a6dbSMax Filippov/*
101609f8a6dbSMax Filippov * fast illegal instruction handler.
101709f8a6dbSMax Filippov *
101809f8a6dbSMax Filippov * This is used to fix up user PS.WOE on the exception caused
101909f8a6dbSMax Filippov * by the first opcode related to register window. If PS.WOE is
102009f8a6dbSMax Filippov * already set it goes directly to the common user exception handler.
102109f8a6dbSMax Filippov *
102209f8a6dbSMax Filippov * Entry condition:
102309f8a6dbSMax Filippov *
102409f8a6dbSMax Filippov *   a0:	trashed, original value saved on stack (PT_AREG0)
102509f8a6dbSMax Filippov *   a1:	a1
102609f8a6dbSMax Filippov *   a2:	new stack pointer, original in DEPC
102709f8a6dbSMax Filippov *   a3:	a3
102809f8a6dbSMax Filippov *   depc:	a2, original value saved on stack (PT_DEPC)
102909f8a6dbSMax Filippov *   excsave_1:	dispatch table
103009f8a6dbSMax Filippov */
103109f8a6dbSMax Filippov
103209f8a6dbSMax FilippovENTRY(fast_illegal_instruction_user)
103309f8a6dbSMax Filippov
103409f8a6dbSMax Filippov	rsr	a0, ps
103550722f0bSMax Filippov	bbsi.l	a0, PS_WOE_BIT, 1f
103609f8a6dbSMax Filippov	s32i	a3, a2, PT_AREG3
103709f8a6dbSMax Filippov	movi	a3, PS_WOE_MASK
103809f8a6dbSMax Filippov	or	a0, a0, a3
103909f8a6dbSMax Filippov	wsr	a0, ps
104009f8a6dbSMax Filippov	l32i	a3, a2, PT_AREG3
104109f8a6dbSMax Filippov	l32i	a0, a2, PT_AREG0
104209f8a6dbSMax Filippov	rsr	a2, depc
104309f8a6dbSMax Filippov	rfe
104450722f0bSMax Filippov1:
104550722f0bSMax Filippov	call0	user_exception
104609f8a6dbSMax Filippov
104709f8a6dbSMax FilippovENDPROC(fast_illegal_instruction_user)
104809f8a6dbSMax Filippov#endif
104909f8a6dbSMax Filippov
10505a0015d6SChris Zankel	/*
10515a0015d6SChris Zankel * fast system calls.
10525a0015d6SChris Zankel *
10535a0015d6SChris Zankel * WARNING:  The kernel doesn't save the entire user context before
10545a0015d6SChris Zankel * handling a fast system call.  These functions are small and short,
10555a0015d6SChris Zankel * usually offering some functionality not available to user tasks.
10565a0015d6SChris Zankel *
10575a0015d6SChris Zankel * BE CAREFUL TO PRESERVE THE USER'S CONTEXT.
10585a0015d6SChris Zankel *
10595a0015d6SChris Zankel * Entry condition:
10605a0015d6SChris Zankel *
10615a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
10625a0015d6SChris Zankel *   a1:	a1
10635a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
106499d5040eSMax Filippov *   a3:	a3
10655a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
106699d5040eSMax Filippov *   excsave_1:	dispatch table
10675a0015d6SChris Zankel */
10685a0015d6SChris Zankel
10695a0015d6SChris ZankelENTRY(fast_syscall_user)
10705a0015d6SChris Zankel
10715a0015d6SChris Zankel	/* Skip syscall. */
10725a0015d6SChris Zankel
1073bc5378fcSMax Filippov	rsr	a0, epc1
10745a0015d6SChris Zankel	addi	a0, a0, 3
1075bc5378fcSMax Filippov	wsr	a0, epc1
10765a0015d6SChris Zankel
10775a0015d6SChris Zankel	l32i	a0, a2, PT_DEPC
10785a0015d6SChris Zankel	bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, fast_syscall_unrecoverable
10795a0015d6SChris Zankel
1080bc5378fcSMax Filippov	rsr	a0, depc			# get syscall-nr
10815a0015d6SChris Zankel	_beqz	a0, fast_syscall_spill_registers
1082fc4fb2adSChris Zankel	_beqi	a0, __NR_xtensa, fast_syscall_xtensa
10835a0015d6SChris Zankel
108450722f0bSMax Filippov	call0	user_exception
10855a0015d6SChris Zankel
1086d1538c46SChris ZankelENDPROC(fast_syscall_user)
1087d1538c46SChris Zankel
10885a0015d6SChris ZankelENTRY(fast_syscall_unrecoverable)
10895a0015d6SChris Zankel
10905a0015d6SChris Zankel	/* Restore all states. */
10915a0015d6SChris Zankel
10925a0015d6SChris Zankel	l32i    a0, a2, PT_AREG0        # restore a0
1093bc5378fcSMax Filippov	xsr     a2, depc                # restore a2, depc
10945a0015d6SChris Zankel
1095bc5378fcSMax Filippov	wsr     a0, excsave1
10962da03d41SMax Filippov	call0	unrecoverable_exception
10975a0015d6SChris Zankel
1098d1538c46SChris ZankelENDPROC(fast_syscall_unrecoverable)
10995a0015d6SChris Zankel
11005a0015d6SChris Zankel/*
11015a0015d6SChris Zankel * sysxtensa syscall handler
11025a0015d6SChris Zankel *
1103fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_SET,     ptr, val,    unused);
1104fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_ADD,     ptr, val,    unused);
1105fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_EXG_ADD, ptr, val,    unused);
1106fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_CMP_SWP, ptr, oldval, newval);
11075a0015d6SChris Zankel *        a2            a6                   a3    a4      a5
11085a0015d6SChris Zankel *
11095a0015d6SChris Zankel * Entry condition:
11105a0015d6SChris Zankel *
1111fc4fb2adSChris Zankel *   a0:	a2 (syscall-nr), original value saved on stack (PT_AREG0)
11125a0015d6SChris Zankel *   a1:	a1
1113fc4fb2adSChris Zankel *   a2:	new stack pointer, original in a0 and DEPC
111499d5040eSMax Filippov *   a3:	a3
1115fc4fb2adSChris Zankel *   a4..a15:	unchanged
11165a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
111799d5040eSMax Filippov *   excsave_1:	dispatch table
11185a0015d6SChris Zankel *
11195a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
11205a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
11215a0015d6SChris Zankel *
11225a0015d6SChris Zankel * Note: we don't have to save a2; a2 holds the return value
11235a0015d6SChris Zankel */
11245a0015d6SChris Zankel
1125f8f02ca7SMax Filippov	.literal_position
1126f8f02ca7SMax Filippov
11279184289cSMax Filippov#ifdef CONFIG_FAST_SYSCALL_XTENSA
11289184289cSMax Filippov
1129fc4fb2adSChris ZankelENTRY(fast_syscall_xtensa)
11305a0015d6SChris Zankel
1131fc4fb2adSChris Zankel	s32i	a7, a2, PT_AREG7	# we need an additional register
11325a0015d6SChris Zankel	movi	a7, 4			# sizeof(unsigned int)
1133fc4fb2adSChris Zankel	access_ok a3, a7, a0, a2, .Leac	# a0: scratch reg, a2: sp
11345a0015d6SChris Zankel
1135d1b6ba82SMax Filippov	_bgeui	a6, SYS_XTENSA_COUNT, .Lill
1136d1b6ba82SMax Filippov	_bnei	a6, SYS_XTENSA_ATOMIC_CMP_SWP, .Lnswp
11375a0015d6SChris Zankel
1138fc4fb2adSChris Zankel	/* Fall through for ATOMIC_CMP_SWP. */
11395a0015d6SChris Zankel
11405a0015d6SChris Zankel.Lswp:	/* Atomic compare and swap */
11415a0015d6SChris Zankel
11420013acebSMax FilippovEX(.Leac) l32i	a0, a3, 0		# read old value
1143fc4fb2adSChris Zankel	bne	a0, a4, 1f		# same as old value? jump
11440013acebSMax FilippovEX(.Leac) s32i	a5, a3, 0		# different, modify value
1145fc4fb2adSChris Zankel	l32i	a7, a2, PT_AREG7	# restore a7
1146fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1147fc4fb2adSChris Zankel	movi	a2, 1			# and return 1
1148fc4fb2adSChris Zankel	rfe
11495a0015d6SChris Zankel
1150fc4fb2adSChris Zankel1:	l32i	a7, a2, PT_AREG7	# restore a7
1151fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1152fc4fb2adSChris Zankel	movi	a2, 0			# return 0 (note that we cannot set
1153fc4fb2adSChris Zankel	rfe
11545a0015d6SChris Zankel
1155fc4fb2adSChris Zankel.Lnswp:	/* Atomic set, add, and exg_add. */
11565a0015d6SChris Zankel
11570013acebSMax FilippovEX(.Leac) l32i	a7, a3, 0		# orig
1158d1b6ba82SMax Filippov	addi	a6, a6, -SYS_XTENSA_ATOMIC_SET
1159fc4fb2adSChris Zankel	add	a0, a4, a7		# + arg
1160fc4fb2adSChris Zankel	moveqz	a0, a4, a6		# set
1161d1b6ba82SMax Filippov	addi	a6, a6, SYS_XTENSA_ATOMIC_SET
11620013acebSMax FilippovEX(.Leac) s32i	a0, a3, 0		# write new value
11635a0015d6SChris Zankel
1164fc4fb2adSChris Zankel	mov	a0, a2
11655a0015d6SChris Zankel	mov	a2, a7
1166fc4fb2adSChris Zankel	l32i	a7, a0, PT_AREG7	# restore a7
1167fc4fb2adSChris Zankel	l32i	a0, a0, PT_AREG0	# restore a0
11685a0015d6SChris Zankel	rfe
11695a0015d6SChris Zankel
1170fc4fb2adSChris Zankel.Leac:	l32i	a7, a2, PT_AREG7	# restore a7
1171fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1172fc4fb2adSChris Zankel	movi	a2, -EFAULT
1173fc4fb2adSChris Zankel	rfe
1174fc4fb2adSChris Zankel
1175d1b6ba82SMax Filippov.Lill:	l32i	a7, a2, PT_AREG7	# restore a7
1176fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1177fc4fb2adSChris Zankel	movi	a2, -EINVAL
1178fc4fb2adSChris Zankel	rfe
1179fc4fb2adSChris Zankel
1180d1538c46SChris ZankelENDPROC(fast_syscall_xtensa)
11815a0015d6SChris Zankel
11829184289cSMax Filippov#else /* CONFIG_FAST_SYSCALL_XTENSA */
11839184289cSMax Filippov
11849184289cSMax FilippovENTRY(fast_syscall_xtensa)
11859184289cSMax Filippov
11869184289cSMax Filippov	l32i    a0, a2, PT_AREG0        # restore a0
11879184289cSMax Filippov	movi	a2, -ENOSYS
11889184289cSMax Filippov	rfe
11899184289cSMax Filippov
11909184289cSMax FilippovENDPROC(fast_syscall_xtensa)
11919184289cSMax Filippov
11929184289cSMax Filippov#endif /* CONFIG_FAST_SYSCALL_XTENSA */
11939184289cSMax Filippov
11945a0015d6SChris Zankel
11955a0015d6SChris Zankel/* fast_syscall_spill_registers.
11965a0015d6SChris Zankel *
11975a0015d6SChris Zankel * Entry condition:
11985a0015d6SChris Zankel *
11995a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
12005a0015d6SChris Zankel *   a1:	a1
12015a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
120299d5040eSMax Filippov *   a3:	a3
12035a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
120499d5040eSMax Filippov *   excsave_1:	dispatch table
12055a0015d6SChris Zankel *
12065a0015d6SChris Zankel * Note: We assume the stack pointer is EXC_TABLE_KSTK in the fixup handler.
12075a0015d6SChris Zankel */
12085a0015d6SChris Zankel
12099184289cSMax Filippov#ifdef CONFIG_FAST_SYSCALL_SPILL_REGISTERS
12109184289cSMax Filippov
12115a0015d6SChris ZankelENTRY(fast_syscall_spill_registers)
12125a0015d6SChris Zankel
12135a0015d6SChris Zankel	/* Register a FIXUP handler (pass current wb as a parameter) */
12145a0015d6SChris Zankel
121599d5040eSMax Filippov	xsr	a3, excsave1
12165a0015d6SChris Zankel	movi	a0, fast_syscall_spill_registers_fixup
12175a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_FIXUP
1218bc5378fcSMax Filippov	rsr	a0, windowbase
12195a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_PARAM
122099d5040eSMax Filippov	xsr	a3, excsave1		# restore a3 and excsave_1
12215a0015d6SChris Zankel
122299d5040eSMax Filippov	/* Save a3, a4 and SAR on stack. */
12235a0015d6SChris Zankel
1224bc5378fcSMax Filippov	rsr	a0, sar
12255a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
12266b5a1f74SChris Zankel	s32i	a0, a2, PT_SAR
12276b5a1f74SChris Zankel
12286b5a1f74SChris Zankel	/* The spill routine might clobber a4, a7, a8, a11, a12, and a15. */
12296b5a1f74SChris Zankel
1230c658eac6SChris Zankel	s32i	a4, a2, PT_AREG4
1231c658eac6SChris Zankel	s32i	a7, a2, PT_AREG7
12326b5a1f74SChris Zankel	s32i	a8, a2, PT_AREG8
1233c658eac6SChris Zankel	s32i	a11, a2, PT_AREG11
12346b5a1f74SChris Zankel	s32i	a12, a2, PT_AREG12
1235c658eac6SChris Zankel	s32i	a15, a2, PT_AREG15
12365a0015d6SChris Zankel
12376b5a1f74SChris Zankel	/*
12386b5a1f74SChris Zankel	 * Rotate ws so that the current windowbase is at bit 0.
12396b5a1f74SChris Zankel	 * Assume ws = xxxwww1yy (www1 current window frame).
12406b5a1f74SChris Zankel	 * Rotate ws right so that a4 = yyxxxwww1.
12416b5a1f74SChris Zankel	 */
12426b5a1f74SChris Zankel
12436b5a1f74SChris Zankel	rsr	a0, windowbase
12446b5a1f74SChris Zankel	rsr	a3, windowstart		# a3 = xxxwww1yy
12456b5a1f74SChris Zankel	ssr	a0			# holds WB
12466b5a1f74SChris Zankel	slli	a0, a3, WSBITS
12476b5a1f74SChris Zankel	or	a3, a3, a0		# a3 = xxxwww1yyxxxwww1yy
12486b5a1f74SChris Zankel	srl	a3, a3			# a3 = 00xxxwww1yyxxxwww1
12496b5a1f74SChris Zankel
12506b5a1f74SChris Zankel	/* We are done if there are no more than the current register frame. */
12516b5a1f74SChris Zankel
12526b5a1f74SChris Zankel	extui	a3, a3, 1, WSBITS-1	# a3 = 0yyxxxwww
12536b5a1f74SChris Zankel	movi	a0, (1 << (WSBITS-1))
12546b5a1f74SChris Zankel	_beqz	a3, .Lnospill		# only one active frame? jump
12556b5a1f74SChris Zankel
12566b5a1f74SChris Zankel	/* We want 1 at the top, so that we return to the current windowbase */
12576b5a1f74SChris Zankel
12586b5a1f74SChris Zankel	or	a3, a3, a0		# 1yyxxxwww
12596b5a1f74SChris Zankel
12606b5a1f74SChris Zankel	/* Skip empty frames - get 'oldest' WINDOWSTART-bit. */
12616b5a1f74SChris Zankel
12626b5a1f74SChris Zankel	wsr	a3, windowstart		# save shifted windowstart
12636b5a1f74SChris Zankel	neg	a0, a3
12646b5a1f74SChris Zankel	and	a3, a0, a3		# first bit set from right: 000010000
12656b5a1f74SChris Zankel
12666b5a1f74SChris Zankel	ffs_ws	a0, a3			# a0: shifts to skip empty frames
12676b5a1f74SChris Zankel	movi	a3, WSBITS
12686b5a1f74SChris Zankel	sub	a0, a3, a0		# WSBITS-a0:number of 0-bits from right
12696b5a1f74SChris Zankel	ssr	a0			# save in SAR for later.
12706b5a1f74SChris Zankel
12716b5a1f74SChris Zankel	rsr	a3, windowbase
12726b5a1f74SChris Zankel	add	a3, a3, a0
12736b5a1f74SChris Zankel	wsr	a3, windowbase
12746b5a1f74SChris Zankel	rsync
12756b5a1f74SChris Zankel
12766b5a1f74SChris Zankel	rsr	a3, windowstart
12776b5a1f74SChris Zankel	srl	a3, a3			# shift windowstart
12786b5a1f74SChris Zankel
12796b5a1f74SChris Zankel	/* WB is now just one frame below the oldest frame in the register
12806b5a1f74SChris Zankel	   window. WS is shifted so the oldest frame is in bit 0, thus, WB
12816b5a1f74SChris Zankel	   and WS differ by one 4-register frame. */
12826b5a1f74SChris Zankel
12836b5a1f74SChris Zankel	/* Save frames. Depending what call was used (call4, call8, call12),
12846b5a1f74SChris Zankel	 * we have to save 4,8. or 12 registers.
12856b5a1f74SChris Zankel	 */
12866b5a1f74SChris Zankel
12876b5a1f74SChris Zankel
12886b5a1f74SChris Zankel.Lloop: _bbsi.l	a3, 1, .Lc4
12896b5a1f74SChris Zankel	_bbci.l	a3, 2, .Lc12
12906b5a1f74SChris Zankel
12916b5a1f74SChris Zankel.Lc8:	s32e	a4, a13, -16
12926b5a1f74SChris Zankel	l32e	a4, a5, -12
12936b5a1f74SChris Zankel	s32e	a8, a4, -32
12946b5a1f74SChris Zankel	s32e	a5, a13, -12
12956b5a1f74SChris Zankel	s32e	a6, a13, -8
12966b5a1f74SChris Zankel	s32e	a7, a13, -4
12976b5a1f74SChris Zankel	s32e	a9, a4, -28
12986b5a1f74SChris Zankel	s32e	a10, a4, -24
12996b5a1f74SChris Zankel	s32e	a11, a4, -20
13006b5a1f74SChris Zankel	srli	a11, a3, 2		# shift windowbase by 2
13016b5a1f74SChris Zankel	rotw	2
13026b5a1f74SChris Zankel	_bnei	a3, 1, .Lloop
13036b5a1f74SChris Zankel	j	.Lexit
13046b5a1f74SChris Zankel
13056b5a1f74SChris Zankel.Lc4:	s32e	a4, a9, -16
13066b5a1f74SChris Zankel	s32e	a5, a9, -12
13076b5a1f74SChris Zankel	s32e	a6, a9, -8
13086b5a1f74SChris Zankel	s32e	a7, a9, -4
13096b5a1f74SChris Zankel
13106b5a1f74SChris Zankel	srli	a7, a3, 1
13116b5a1f74SChris Zankel	rotw	1
13126b5a1f74SChris Zankel	_bnei	a3, 1, .Lloop
13136b5a1f74SChris Zankel	j	.Lexit
13146b5a1f74SChris Zankel
13156b5a1f74SChris Zankel.Lc12:	_bbci.l	a3, 3, .Linvalid_mask	# bit 2 shouldn't be zero!
13166b5a1f74SChris Zankel
13176b5a1f74SChris Zankel	/* 12-register frame (call12) */
13186b5a1f74SChris Zankel
13196b5a1f74SChris Zankel	l32e	a0, a5, -12
13206b5a1f74SChris Zankel	s32e	a8, a0, -48
13216b5a1f74SChris Zankel	mov	a8, a0
13226b5a1f74SChris Zankel
13236b5a1f74SChris Zankel	s32e	a9, a8, -44
13246b5a1f74SChris Zankel	s32e	a10, a8, -40
13256b5a1f74SChris Zankel	s32e	a11, a8, -36
13266b5a1f74SChris Zankel	s32e	a12, a8, -32
13276b5a1f74SChris Zankel	s32e	a13, a8, -28
13286b5a1f74SChris Zankel	s32e	a14, a8, -24
13296b5a1f74SChris Zankel	s32e	a15, a8, -20
13306b5a1f74SChris Zankel	srli	a15, a3, 3
13316b5a1f74SChris Zankel
13326b5a1f74SChris Zankel	/* The stack pointer for a4..a7 is out of reach, so we rotate the
13336b5a1f74SChris Zankel	 * window, grab the stackpointer, and rotate back.
13346b5a1f74SChris Zankel	 * Alternatively, we could also use the following approach, but that
13356b5a1f74SChris Zankel	 * makes the fixup routine much more complicated:
13366b5a1f74SChris Zankel	 * rotw	1
13376b5a1f74SChris Zankel	 * s32e	a0, a13, -16
13386b5a1f74SChris Zankel	 * ...
13396b5a1f74SChris Zankel	 * rotw 2
13406b5a1f74SChris Zankel	 */
13416b5a1f74SChris Zankel
13426b5a1f74SChris Zankel	rotw	1
13436b5a1f74SChris Zankel	mov	a4, a13
13446b5a1f74SChris Zankel	rotw	-1
13456b5a1f74SChris Zankel
13466b5a1f74SChris Zankel	s32e	a4, a8, -16
13476b5a1f74SChris Zankel	s32e	a5, a8, -12
13486b5a1f74SChris Zankel	s32e	a6, a8, -8
13496b5a1f74SChris Zankel	s32e	a7, a8, -4
13506b5a1f74SChris Zankel
13516b5a1f74SChris Zankel	rotw	3
13526b5a1f74SChris Zankel
13536b5a1f74SChris Zankel	_beqi	a3, 1, .Lexit
13546b5a1f74SChris Zankel	j	.Lloop
13556b5a1f74SChris Zankel
13566b5a1f74SChris Zankel.Lexit:
13576b5a1f74SChris Zankel
13586b5a1f74SChris Zankel	/* Done. Do the final rotation and set WS */
13596b5a1f74SChris Zankel
13606b5a1f74SChris Zankel	rotw	1
13616b5a1f74SChris Zankel	rsr	a3, windowbase
13626b5a1f74SChris Zankel	ssl	a3
13636b5a1f74SChris Zankel	movi	a3, 1
13646b5a1f74SChris Zankel	sll	a3, a3
13656b5a1f74SChris Zankel	wsr	a3, windowstart
13666b5a1f74SChris Zankel.Lnospill:
13675a0015d6SChris Zankel
13685a0015d6SChris Zankel	/* Advance PC, restore registers and SAR, and return from exception. */
13695a0015d6SChris Zankel
13706b5a1f74SChris Zankel	l32i	a3, a2, PT_SAR
13715a0015d6SChris Zankel	l32i	a0, a2, PT_AREG0
1372bc5378fcSMax Filippov	wsr	a3, sar
13735a0015d6SChris Zankel	l32i	a3, a2, PT_AREG3
13745a0015d6SChris Zankel
13755a0015d6SChris Zankel	/* Restore clobbered registers. */
13765a0015d6SChris Zankel
13776b5a1f74SChris Zankel	l32i	a4, a2, PT_AREG4
1378c658eac6SChris Zankel	l32i	a7, a2, PT_AREG7
13796b5a1f74SChris Zankel	l32i	a8, a2, PT_AREG8
1380c658eac6SChris Zankel	l32i	a11, a2, PT_AREG11
13816b5a1f74SChris Zankel	l32i	a12, a2, PT_AREG12
1382c658eac6SChris Zankel	l32i	a15, a2, PT_AREG15
13835a0015d6SChris Zankel
13845a0015d6SChris Zankel	movi	a2, 0
13855a0015d6SChris Zankel	rfe
13865a0015d6SChris Zankel
13876b5a1f74SChris Zankel.Linvalid_mask:
13886b5a1f74SChris Zankel
13896b5a1f74SChris Zankel	/* We get here because of an unrecoverable error in the window
13906b5a1f74SChris Zankel	 * registers, so set up a dummy frame and kill the user application.
13916b5a1f74SChris Zankel	 * Note: We assume EXC_TABLE_KSTK contains a valid stack pointer.
13926b5a1f74SChris Zankel	 */
13936b5a1f74SChris Zankel
13946b5a1f74SChris Zankel	movi	a0, 1
13956b5a1f74SChris Zankel	movi	a1, 0
13966b5a1f74SChris Zankel
13976b5a1f74SChris Zankel	wsr	a0, windowstart
13986b5a1f74SChris Zankel	wsr	a1, windowbase
13996b5a1f74SChris Zankel	rsync
14006b5a1f74SChris Zankel
14016b5a1f74SChris Zankel	movi	a0, 0
14026b5a1f74SChris Zankel
14036b5a1f74SChris Zankel	rsr	a3, excsave1
14046b5a1f74SChris Zankel	l32i	a1, a3, EXC_TABLE_KSTK
14056b5a1f74SChris Zankel
14069e1e41c4SMax Filippov	movi	a4, PS_WOE_MASK | LOCKLEVEL
14076b5a1f74SChris Zankel	wsr	a4, ps
14086b5a1f74SChris Zankel	rsync
14096b5a1f74SChris Zankel
14106b5a1f74SChris Zankel	movi	a6, SIGSEGV
14112da03d41SMax Filippov	call4	do_exit
14126b5a1f74SChris Zankel
14136b5a1f74SChris Zankel	/* shouldn't return, so panic */
14146b5a1f74SChris Zankel
14156b5a1f74SChris Zankel	wsr	a0, excsave1
14162da03d41SMax Filippov	call0	unrecoverable_exception		# should not return
14176b5a1f74SChris Zankel1:	j	1b
14186b5a1f74SChris Zankel
14196b5a1f74SChris Zankel
1420d1538c46SChris ZankelENDPROC(fast_syscall_spill_registers)
1421d1538c46SChris Zankel
14225a0015d6SChris Zankel/* Fixup handler.
14235a0015d6SChris Zankel *
14245a0015d6SChris Zankel * We get here if the spill routine causes an exception, e.g. tlb miss.
14255a0015d6SChris Zankel * We basically restore WINDOWBASE and WINDOWSTART to the condition when
14265a0015d6SChris Zankel * we entered the spill routine and jump to the user exception handler.
14275a0015d6SChris Zankel *
14283251f1e2SMax Filippov * Note that we only need to restore the bits in windowstart that have not
14293251f1e2SMax Filippov * been spilled yet by the _spill_register routine. Luckily, a3 contains a
14303251f1e2SMax Filippov * rotated windowstart with only those bits set for frames that haven't been
14313251f1e2SMax Filippov * spilled yet. Because a3 is rotated such that bit 0 represents the register
14323251f1e2SMax Filippov * frame for the current windowbase - 1, we need to rotate a3 left by the
14333251f1e2SMax Filippov * value of the current windowbase + 1 and move it to windowstart.
14343251f1e2SMax Filippov *
14355a0015d6SChris Zankel * a0: value of depc, original value in depc
14365a0015d6SChris Zankel * a2: trashed, original value in EXC_TABLE_DOUBLE_SAVE
14375a0015d6SChris Zankel * a3: exctable, original value in excsave1
14385a0015d6SChris Zankel */
14395a0015d6SChris Zankel
1440244066f4SMax FilippovENTRY(fast_syscall_spill_registers_fixup)
14415a0015d6SChris Zankel
1442bc5378fcSMax Filippov	rsr	a2, windowbase	# get current windowbase (a2 is saved)
1443bc5378fcSMax Filippov	xsr	a0, depc	# restore depc and a0
14445a0015d6SChris Zankel	ssl	a2		# set shift (32 - WB)
14455a0015d6SChris Zankel
14465a0015d6SChris Zankel	/* We need to make sure the current registers (a0-a3) are preserved.
14475a0015d6SChris Zankel	 * To do this, we simply set the bit for the current window frame
14485a0015d6SChris Zankel	 * in WS, so that the exception handlers save them to the task stack.
14493251f1e2SMax Filippov	 *
14503251f1e2SMax Filippov	 * Note: we use a3 to set the windowbase, so we take a special care
14513251f1e2SMax Filippov	 * of it, saving it in the original _spill_registers frame across
14523251f1e2SMax Filippov	 * the exception handler call.
14535a0015d6SChris Zankel	 */
14545a0015d6SChris Zankel
145599d5040eSMax Filippov	xsr	a3, excsave1	# get spill-mask
1456244066f4SMax Filippov	slli	a3, a3, 1	# shift left by one
14573251f1e2SMax Filippov	addi	a3, a3, 1	# set the bit for the current window frame
14585a0015d6SChris Zankel
1459244066f4SMax Filippov	slli	a2, a3, 32-WSBITS
1460244066f4SMax Filippov	src	a2, a3, a2	# a2 = xxwww1yyxxxwww1yy......
1461bc5378fcSMax Filippov	wsr	a2, windowstart	# set corrected windowstart
14625a0015d6SChris Zankel
1463244066f4SMax Filippov	srli	a3, a3, 1
1464244066f4SMax Filippov	rsr	a2, excsave1
1465244066f4SMax Filippov	l32i	a2, a2, EXC_TABLE_DOUBLE_SAVE	# restore a2
1466244066f4SMax Filippov	xsr	a2, excsave1
1467244066f4SMax Filippov	s32i	a3, a2, EXC_TABLE_DOUBLE_SAVE	# save a3
1468244066f4SMax Filippov	l32i	a3, a2, EXC_TABLE_PARAM	# original WB (in user task)
1469244066f4SMax Filippov	xsr	a2, excsave1
14705a0015d6SChris Zankel
14715a0015d6SChris Zankel	/* Return to the original (user task) WINDOWBASE.
14725a0015d6SChris Zankel	 * We leave the following frame behind:
14735a0015d6SChris Zankel	 * a0, a1, a2	same
1474244066f4SMax Filippov	 * a3:		trashed (saved in EXC_TABLE_DOUBLE_SAVE)
14755a0015d6SChris Zankel	 * depc:	depc (we have to return to that address)
1476244066f4SMax Filippov	 * excsave_1:	exctable
14775a0015d6SChris Zankel	 */
14785a0015d6SChris Zankel
1479bc5378fcSMax Filippov	wsr	a3, windowbase
14805a0015d6SChris Zankel	rsync
14815a0015d6SChris Zankel
14825a0015d6SChris Zankel	/* We are now in the original frame when we entered _spill_registers:
14835a0015d6SChris Zankel	 *  a0: return address
14845a0015d6SChris Zankel	 *  a1: used, stack pointer
14855a0015d6SChris Zankel	 *  a2: kernel stack pointer
1486244066f4SMax Filippov	 *  a3: available
14875a0015d6SChris Zankel	 *  depc: exception address
1488244066f4SMax Filippov	 *  excsave: exctable
14895a0015d6SChris Zankel	 * Note: This frame might be the same as above.
14905a0015d6SChris Zankel	 */
14915a0015d6SChris Zankel
14925a0015d6SChris Zankel	/* Setup stack pointer. */
14935a0015d6SChris Zankel
14945a0015d6SChris Zankel	addi	a2, a2, -PT_USER_SIZE
14955a0015d6SChris Zankel	s32i	a0, a2, PT_AREG0
14965a0015d6SChris Zankel
14975a0015d6SChris Zankel	/* Make sure we return to this fixup handler. */
14985a0015d6SChris Zankel
14995a0015d6SChris Zankel	movi	a3, fast_syscall_spill_registers_fixup_return
15005a0015d6SChris Zankel	s32i	a3, a2, PT_DEPC		# setup depc
15015a0015d6SChris Zankel
15025a0015d6SChris Zankel	/* Jump to the exception handler. */
15035a0015d6SChris Zankel
150499d5040eSMax Filippov	rsr	a3, excsave1
1505bc5378fcSMax Filippov	rsr	a0, exccause
15065a0015d6SChris Zankel	addx4	a0, a0, a3              	# find entry in table
15075a0015d6SChris Zankel	l32i	a0, a0, EXC_TABLE_FAST_USER     # load handler
1508244066f4SMax Filippov	l32i	a3, a3, EXC_TABLE_DOUBLE_SAVE
15095a0015d6SChris Zankel	jx	a0
15105a0015d6SChris Zankel
1511244066f4SMax FilippovENDPROC(fast_syscall_spill_registers_fixup)
1512244066f4SMax Filippov
1513244066f4SMax FilippovENTRY(fast_syscall_spill_registers_fixup_return)
15145a0015d6SChris Zankel
15155a0015d6SChris Zankel	/* When we return here, all registers have been restored (a2: DEPC) */
15165a0015d6SChris Zankel
1517bc5378fcSMax Filippov	wsr	a2, depc		# exception address
15185a0015d6SChris Zankel
15195a0015d6SChris Zankel	/* Restore fixup handler. */
15205a0015d6SChris Zankel
1521244066f4SMax Filippov	rsr	a2, excsave1
1522244066f4SMax Filippov	s32i	a3, a2, EXC_TABLE_DOUBLE_SAVE
1523244066f4SMax Filippov	movi	a3, fast_syscall_spill_registers_fixup
1524244066f4SMax Filippov	s32i	a3, a2, EXC_TABLE_FIXUP
1525244066f4SMax Filippov	rsr	a3, windowbase
1526244066f4SMax Filippov	s32i	a3, a2, EXC_TABLE_PARAM
1527244066f4SMax Filippov	l32i	a2, a2, EXC_TABLE_KSTK
15285a0015d6SChris Zankel
15295a0015d6SChris Zankel	/* Load WB at the time the exception occurred. */
15305a0015d6SChris Zankel
1531bc5378fcSMax Filippov	rsr	a3, sar			# WB is still in SAR
15325a0015d6SChris Zankel	neg	a3, a3
1533bc5378fcSMax Filippov	wsr	a3, windowbase
15345a0015d6SChris Zankel	rsync
15355a0015d6SChris Zankel
1536244066f4SMax Filippov	rsr	a3, excsave1
1537244066f4SMax Filippov	l32i	a3, a3, EXC_TABLE_DOUBLE_SAVE
1538244066f4SMax Filippov
15395a0015d6SChris Zankel	rfde
15405a0015d6SChris Zankel
1541244066f4SMax FilippovENDPROC(fast_syscall_spill_registers_fixup_return)
15425a0015d6SChris Zankel
15439184289cSMax Filippov#else /* CONFIG_FAST_SYSCALL_SPILL_REGISTERS */
15449184289cSMax Filippov
15459184289cSMax FilippovENTRY(fast_syscall_spill_registers)
15469184289cSMax Filippov
15479184289cSMax Filippov	l32i    a0, a2, PT_AREG0        # restore a0
15489184289cSMax Filippov	movi	a2, -ENOSYS
15499184289cSMax Filippov	rfe
15509184289cSMax Filippov
15519184289cSMax FilippovENDPROC(fast_syscall_spill_registers)
15529184289cSMax Filippov
15539184289cSMax Filippov#endif /* CONFIG_FAST_SYSCALL_SPILL_REGISTERS */
15549184289cSMax Filippov
1555e5083a63SJohannes Weiner#ifdef CONFIG_MMU
15565a0015d6SChris Zankel/*
15575a0015d6SChris Zankel * We should never get here. Bail out!
15585a0015d6SChris Zankel */
15595a0015d6SChris Zankel
15605a0015d6SChris ZankelENTRY(fast_second_level_miss_double_kernel)
15615a0015d6SChris Zankel
15622da03d41SMax Filippov1:
15632da03d41SMax Filippov	call0	unrecoverable_exception		# should not return
15645a0015d6SChris Zankel1:	j	1b
15655a0015d6SChris Zankel
1566d1538c46SChris ZankelENDPROC(fast_second_level_miss_double_kernel)
1567d1538c46SChris Zankel
15685a0015d6SChris Zankel/* First-level entry handler for user, kernel, and double 2nd-level
15695a0015d6SChris Zankel * TLB miss exceptions.  Note that for now, user and kernel miss
15705a0015d6SChris Zankel * exceptions share the same entry point and are handled identically.
15715a0015d6SChris Zankel *
15725a0015d6SChris Zankel * An old, less-efficient C version of this function used to exist.
15735a0015d6SChris Zankel * We include it below, interleaved as comments, for reference.
15745a0015d6SChris Zankel *
15755a0015d6SChris Zankel * Entry condition:
15765a0015d6SChris Zankel *
15775a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
15785a0015d6SChris Zankel *   a1:	a1
15795a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
158099d5040eSMax Filippov *   a3:	a3
15815a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
158299d5040eSMax Filippov *   excsave_1:	dispatch table
15835a0015d6SChris Zankel *
15845a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
15855a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
15865a0015d6SChris Zankel */
15875a0015d6SChris Zankel
15885a0015d6SChris ZankelENTRY(fast_second_level_miss)
15895a0015d6SChris Zankel
159099d5040eSMax Filippov	/* Save a1 and a3. Note: we don't expect a double exception. */
15915a0015d6SChris Zankel
15925a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
159399d5040eSMax Filippov	s32i	a3, a2, PT_AREG3
15945a0015d6SChris Zankel
15955a0015d6SChris Zankel	/* We need to map the page of PTEs for the user task.  Find
15965a0015d6SChris Zankel	 * the pointer to that page.  Also, it's possible for tsk->mm
15975a0015d6SChris Zankel	 * to be NULL while tsk->active_mm is nonzero if we faulted on
15985a0015d6SChris Zankel	 * a vmalloc address.  In that rare case, we must use
15995a0015d6SChris Zankel	 * active_mm instead to avoid a fault in this handler.  See
16005a0015d6SChris Zankel	 *
16015a0015d6SChris Zankel	 * http://mail.nl.linux.org/linux-mm/2002-08/msg00258.html
16025a0015d6SChris Zankel	 *   (or search Internet on "mm vs. active_mm")
16035a0015d6SChris Zankel	 *
16045a0015d6SChris Zankel	 *	if (!mm)
16055a0015d6SChris Zankel	 *		mm = tsk->active_mm;
16065a0015d6SChris Zankel	 *	pgd = pgd_offset (mm, regs->excvaddr);
16075a0015d6SChris Zankel	 *	pmd = pmd_offset (pgd, regs->excvaddr);
16085a0015d6SChris Zankel	 *	pmdval = *pmd;
16095a0015d6SChris Zankel	 */
16105a0015d6SChris Zankel
16115a0015d6SChris Zankel	GET_CURRENT(a1,a2)
16125a0015d6SChris Zankel	l32i	a0, a1, TASK_MM		# tsk->mm
16135a0015d6SChris Zankel	beqz	a0, 9f
16145a0015d6SChris Zankel
1615bc5378fcSMax Filippov8:	rsr	a3, excvaddr		# fault address
161601858d1bSChris Zankel	_PGD_OFFSET(a0, a3, a1)
16175a0015d6SChris Zankel	l32i	a0, a0, 0		# read pmdval
16185a0015d6SChris Zankel	beqz	a0, 2f
16195a0015d6SChris Zankel
16205a0015d6SChris Zankel	/* Read ptevaddr and convert to top of page-table page.
16215a0015d6SChris Zankel	 *
16225a0015d6SChris Zankel	 * 	vpnval = read_ptevaddr_register() & PAGE_MASK;
16235a0015d6SChris Zankel	 * 	vpnval += DTLB_WAY_PGTABLE;
16245a0015d6SChris Zankel	 *	pteval = mk_pte (virt_to_page(pmd_val(pmdval)), PAGE_KERNEL);
16255a0015d6SChris Zankel	 *	write_dtlb_entry (pteval, vpnval);
16265a0015d6SChris Zankel	 *
16275a0015d6SChris Zankel	 * The messy computation for 'pteval' above really simplifies
16285a0015d6SChris Zankel	 * into the following:
16295a0015d6SChris Zankel	 *
1630a9f2fc62SMax Filippov	 * pteval = ((pmdval - PAGE_OFFSET + PHYS_OFFSET) & PAGE_MASK)
1631a9f2fc62SMax Filippov	 *                 | PAGE_DIRECTORY
16325a0015d6SChris Zankel	 */
16335a0015d6SChris Zankel
1634a9f2fc62SMax Filippov	movi	a1, (PHYS_OFFSET - PAGE_OFFSET) & 0xffffffff
16355a0015d6SChris Zankel	add	a0, a0, a1		# pmdval - PAGE_OFFSET
16365a0015d6SChris Zankel	extui	a1, a0, 0, PAGE_SHIFT	# ... & PAGE_MASK
16375a0015d6SChris Zankel	xor	a0, a0, a1
16385a0015d6SChris Zankel
163901858d1bSChris Zankel	movi	a1, _PAGE_DIRECTORY
16405a0015d6SChris Zankel	or	a0, a0, a1		# ... | PAGE_DIRECTORY
16415a0015d6SChris Zankel
164201858d1bSChris Zankel	/*
16436656920bSChris Zankel	 * We utilize all three wired-ways (7-9) to hold pmd translations.
164401858d1bSChris Zankel	 * Memory regions are mapped to the DTLBs according to bits 28 and 29.
164501858d1bSChris Zankel	 * This allows to map the three most common regions to three different
164601858d1bSChris Zankel	 * DTLBs:
164701858d1bSChris Zankel	 *  0,1 -> way 7	program (0040.0000) and virtual (c000.0000)
164801858d1bSChris Zankel	 *  2   -> way 8	shared libaries (2000.0000)
164901858d1bSChris Zankel	 *  3   -> way 0	stack (3000.0000)
165001858d1bSChris Zankel	 */
16515a0015d6SChris Zankel
165201858d1bSChris Zankel	extui	a3, a3, 28, 2		# addr. bit 28 and 29	0,1,2,3
1653bc5378fcSMax Filippov	rsr	a1, ptevaddr
165401858d1bSChris Zankel	addx2	a3, a3, a3		# ->			0,3,6,9
165501858d1bSChris Zankel	srli	a1, a1, PAGE_SHIFT
165601858d1bSChris Zankel	extui	a3, a3, 2, 2		# ->			0,0,1,2
165701858d1bSChris Zankel	slli	a1, a1, PAGE_SHIFT	# ptevaddr & PAGE_MASK
165801858d1bSChris Zankel	addi	a3, a3, DTLB_WAY_PGD
165901858d1bSChris Zankel	add	a1, a1, a3		# ... + way_number
166001858d1bSChris Zankel
166101858d1bSChris Zankel3:	wdtlb	a0, a1
16625a0015d6SChris Zankel	dsync
16635a0015d6SChris Zankel
16645a0015d6SChris Zankel	/* Exit critical section. */
16655a0015d6SChris Zankel
166699d5040eSMax Filippov4:	rsr	a3, excsave1
16675a0015d6SChris Zankel	movi	a0, 0
16685a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_FIXUP
16695a0015d6SChris Zankel
16705a0015d6SChris Zankel	/* Restore the working registers, and return. */
16715a0015d6SChris Zankel
16725a0015d6SChris Zankel	l32i	a0, a2, PT_AREG0
16735a0015d6SChris Zankel	l32i	a1, a2, PT_AREG1
167499d5040eSMax Filippov	l32i	a3, a2, PT_AREG3
16755a0015d6SChris Zankel	l32i	a2, a2, PT_DEPC
16765a0015d6SChris Zankel
16775a0015d6SChris Zankel	bgeui	a2, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
16785a0015d6SChris Zankel
16795a0015d6SChris Zankel	/* Restore excsave1 and return. */
16805a0015d6SChris Zankel
1681bc5378fcSMax Filippov	rsr	a2, depc
16825a0015d6SChris Zankel	rfe
16835a0015d6SChris Zankel
16845a0015d6SChris Zankel	/* Return from double exception. */
16855a0015d6SChris Zankel
1686bc5378fcSMax Filippov1:	xsr	a2, depc
16875a0015d6SChris Zankel	esync
16885a0015d6SChris Zankel	rfde
16895a0015d6SChris Zankel
16905a0015d6SChris Zankel9:	l32i	a0, a1, TASK_ACTIVE_MM	# unlikely case mm == 0
169138fef73cSMax Filippov	bnez	a0, 8b
169238fef73cSMax Filippov
169338fef73cSMax Filippov	/* Even more unlikely case active_mm == 0.
169438fef73cSMax Filippov	 * We can get here with NMI in the middle of context_switch that
169538fef73cSMax Filippov	 * touches vmalloc area.
169638fef73cSMax Filippov	 */
169738fef73cSMax Filippov	movi	a0, init_mm
16985a0015d6SChris Zankel	j	8b
16995a0015d6SChris Zankel
17006656920bSChris Zankel#if (DCACHE_WAY_SIZE > PAGE_SIZE)
17016656920bSChris Zankel
17026656920bSChris Zankel2:	/* Special case for cache aliasing.
17036656920bSChris Zankel	 * We (should) only get here if a clear_user_page, copy_user_page
17046656920bSChris Zankel	 * or the aliased cache flush functions got preemptively interrupted
17056656920bSChris Zankel	 * by another task. Re-establish temporary mapping to the
17066656920bSChris Zankel	 * TLBTEMP_BASE areas.
17076656920bSChris Zankel	 */
17086656920bSChris Zankel
17096656920bSChris Zankel	/* We shouldn't be in a double exception */
17106656920bSChris Zankel
17116656920bSChris Zankel	l32i	a0, a2, PT_DEPC
17126656920bSChris Zankel	bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, 2f
17136656920bSChris Zankel
17146656920bSChris Zankel	/* Make sure the exception originated in the special functions */
17156656920bSChris Zankel
17166656920bSChris Zankel	movi	a0, __tlbtemp_mapping_start
1717bc5378fcSMax Filippov	rsr	a3, epc1
17186656920bSChris Zankel	bltu	a3, a0, 2f
17196656920bSChris Zankel	movi	a0, __tlbtemp_mapping_end
17206656920bSChris Zankel	bgeu	a3, a0, 2f
17216656920bSChris Zankel
17226656920bSChris Zankel	/* Check if excvaddr was in one of the TLBTEMP_BASE areas. */
17236656920bSChris Zankel
17246656920bSChris Zankel	movi	a3, TLBTEMP_BASE_1
1725bc5378fcSMax Filippov	rsr	a0, excvaddr
17266656920bSChris Zankel	bltu	a0, a3, 2f
17276656920bSChris Zankel
17287128039fSMax Filippov	addi	a1, a0, -TLBTEMP_SIZE
17296656920bSChris Zankel	bgeu	a1, a3, 2f
17306656920bSChris Zankel
17316656920bSChris Zankel	/* Check if we have to restore an ITLB mapping. */
17326656920bSChris Zankel
17336656920bSChris Zankel	movi	a1, __tlbtemp_mapping_itlb
1734bc5378fcSMax Filippov	rsr	a3, epc1
17356656920bSChris Zankel	sub	a3, a3, a1
17366656920bSChris Zankel
17376656920bSChris Zankel	/* Calculate VPN */
17386656920bSChris Zankel
17396656920bSChris Zankel	movi	a1, PAGE_MASK
17406656920bSChris Zankel	and	a1, a1, a0
17416656920bSChris Zankel
17426656920bSChris Zankel	/* Jump for ITLB entry */
17436656920bSChris Zankel
17446656920bSChris Zankel	bgez	a3, 1f
17456656920bSChris Zankel
17466656920bSChris Zankel	/* We can use up to two TLBTEMP areas, one for src and one for dst. */
17476656920bSChris Zankel
17486656920bSChris Zankel	extui	a3, a0, PAGE_SHIFT + DCACHE_ALIAS_ORDER, 1
17496656920bSChris Zankel	add	a1, a3, a1
17506656920bSChris Zankel
17516656920bSChris Zankel	/* PPN is in a6 for the first TLBTEMP area and in a7 for the second. */
17526656920bSChris Zankel
17536656920bSChris Zankel	mov	a0, a6
17546656920bSChris Zankel	movnez	a0, a7, a3
17556656920bSChris Zankel	j	3b
17566656920bSChris Zankel
17576656920bSChris Zankel	/* ITLB entry. We only use dst in a6. */
17586656920bSChris Zankel
17596656920bSChris Zankel1:	witlb	a6, a1
17606656920bSChris Zankel	isync
17616656920bSChris Zankel	j	4b
17626656920bSChris Zankel
17636656920bSChris Zankel
17646656920bSChris Zankel#endif	// DCACHE_WAY_SIZE > PAGE_SIZE
17656656920bSChris Zankel
17666656920bSChris Zankel
17675a0015d6SChris Zankel2:	/* Invalid PGD, default exception handling */
17685a0015d6SChris Zankel
1769bc5378fcSMax Filippov	rsr	a1, depc
17705a0015d6SChris Zankel	s32i	a1, a2, PT_AREG2
17715a0015d6SChris Zankel	mov	a1, a2
17725a0015d6SChris Zankel
1773bc5378fcSMax Filippov	rsr	a2, ps
1774173d6681SChris Zankel	bbsi.l	a2, PS_UM_BIT, 1f
177550722f0bSMax Filippov	call0	_kernel_exception
177650722f0bSMax Filippov1:	call0	_user_exception
17775a0015d6SChris Zankel
1778d1538c46SChris ZankelENDPROC(fast_second_level_miss)
17795a0015d6SChris Zankel
17805a0015d6SChris Zankel/*
17815a0015d6SChris Zankel * StoreProhibitedException
17825a0015d6SChris Zankel *
17835a0015d6SChris Zankel * Update the pte and invalidate the itlb mapping for this pte.
17845a0015d6SChris Zankel *
17855a0015d6SChris Zankel * Entry condition:
17865a0015d6SChris Zankel *
17875a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
17885a0015d6SChris Zankel *   a1:	a1
17895a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
179099d5040eSMax Filippov *   a3:	a3
17915a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
179299d5040eSMax Filippov *   excsave_1:	dispatch table
17935a0015d6SChris Zankel *
17945a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
17955a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
17965a0015d6SChris Zankel */
17975a0015d6SChris Zankel
17985a0015d6SChris ZankelENTRY(fast_store_prohibited)
17995a0015d6SChris Zankel
180099d5040eSMax Filippov	/* Save a1 and a3. */
18015a0015d6SChris Zankel
18025a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
180399d5040eSMax Filippov	s32i	a3, a2, PT_AREG3
18045a0015d6SChris Zankel
18055a0015d6SChris Zankel	GET_CURRENT(a1,a2)
18065a0015d6SChris Zankel	l32i	a0, a1, TASK_MM		# tsk->mm
18075a0015d6SChris Zankel	beqz	a0, 9f
18085a0015d6SChris Zankel
1809bc5378fcSMax Filippov8:	rsr	a1, excvaddr		# fault address
181099d5040eSMax Filippov	_PGD_OFFSET(a0, a1, a3)
18115a0015d6SChris Zankel	l32i	a0, a0, 0
18125a0015d6SChris Zankel	beqz	a0, 2f
18135a0015d6SChris Zankel
181451fc41a9SMax Filippov	/*
181551fc41a9SMax Filippov	 * Note that we test _PAGE_WRITABLE_BIT only if PTE is present
181651fc41a9SMax Filippov	 * and is not PAGE_NONE. See pgtable.h for possible PTE layouts.
181751fc41a9SMax Filippov	 */
181801858d1bSChris Zankel
181999d5040eSMax Filippov	_PTE_OFFSET(a0, a1, a3)
182099d5040eSMax Filippov	l32i	a3, a0, 0		# read pteval
182151fc41a9SMax Filippov	movi	a1, _PAGE_CA_INVALID
182299d5040eSMax Filippov	ball	a3, a1, 2f
182399d5040eSMax Filippov	bbci.l	a3, _PAGE_WRITABLE_BIT, 2f
18245a0015d6SChris Zankel
182501858d1bSChris Zankel	movi	a1, _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_HW_WRITE
182699d5040eSMax Filippov	or	a3, a3, a1
1827bc5378fcSMax Filippov	rsr	a1, excvaddr
182899d5040eSMax Filippov	s32i	a3, a0, 0
18295a0015d6SChris Zankel
18305a0015d6SChris Zankel	/* We need to flush the cache if we have page coloring. */
18315a0015d6SChris Zankel#if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
18325a0015d6SChris Zankel	dhwb	a0, 0
18335a0015d6SChris Zankel#endif
18345a0015d6SChris Zankel	pdtlb	a0, a1
183599d5040eSMax Filippov	wdtlb	a3, a0
18365a0015d6SChris Zankel
18375a0015d6SChris Zankel	/* Exit critical section. */
18385a0015d6SChris Zankel
18395a0015d6SChris Zankel	movi	a0, 0
184099d5040eSMax Filippov	rsr	a3, excsave1
18415a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_FIXUP
18425a0015d6SChris Zankel
18435a0015d6SChris Zankel	/* Restore the working registers, and return. */
18445a0015d6SChris Zankel
184599d5040eSMax Filippov	l32i	a3, a2, PT_AREG3
18465a0015d6SChris Zankel	l32i	a1, a2, PT_AREG1
18475a0015d6SChris Zankel	l32i	a0, a2, PT_AREG0
18485a0015d6SChris Zankel	l32i	a2, a2, PT_DEPC
18495a0015d6SChris Zankel
18505a0015d6SChris Zankel	bgeui	a2, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
18515a0015d6SChris Zankel
1852bc5378fcSMax Filippov	rsr	a2, depc
18535a0015d6SChris Zankel	rfe
18545a0015d6SChris Zankel
18555a0015d6SChris Zankel	/* Double exception. Restore FIXUP handler and return. */
18565a0015d6SChris Zankel
1857bc5378fcSMax Filippov1:	xsr	a2, depc
18585a0015d6SChris Zankel	esync
18595a0015d6SChris Zankel	rfde
18605a0015d6SChris Zankel
18615a0015d6SChris Zankel9:	l32i	a0, a1, TASK_ACTIVE_MM	# unlikely case mm == 0
18625a0015d6SChris Zankel	j	8b
18635a0015d6SChris Zankel
18645a0015d6SChris Zankel2:	/* If there was a problem, handle fault in C */
18655a0015d6SChris Zankel
186699d5040eSMax Filippov	rsr	a3, depc	# still holds a2
186799d5040eSMax Filippov	s32i	a3, a2, PT_AREG2
18685a0015d6SChris Zankel	mov	a1, a2
18695a0015d6SChris Zankel
1870bc5378fcSMax Filippov	rsr	a2, ps
1871173d6681SChris Zankel	bbsi.l	a2, PS_UM_BIT, 1f
187250722f0bSMax Filippov	call0	_kernel_exception
187350722f0bSMax Filippov1:	call0	_user_exception
1874d1538c46SChris Zankel
1875d1538c46SChris ZankelENDPROC(fast_store_prohibited)
1876d1538c46SChris Zankel
1877e5083a63SJohannes Weiner#endif /* CONFIG_MMU */
18785a0015d6SChris Zankel
187950722f0bSMax Filippov	.text
18805a0015d6SChris Zankel/*
1881fc4fb2adSChris Zankel * System Calls.
1882fc4fb2adSChris Zankel *
1883fc4fb2adSChris Zankel * void system_call (struct pt_regs* regs, int exccause)
1884fc4fb2adSChris Zankel *                            a2                 a3
1885fc4fb2adSChris Zankel */
1886f8f02ca7SMax Filippov	.literal_position
1887fc4fb2adSChris Zankel
1888fc4fb2adSChris ZankelENTRY(system_call)
1889d1538c46SChris Zankel
18909d9043f6SMax Filippov	abi_entry_default
1891fc4fb2adSChris Zankel
1892fc4fb2adSChris Zankel	/* regs->syscall = regs->areg[2] */
1893fc4fb2adSChris Zankel
18943aee3e25SMax Filippov	l32i	a7, a2, PT_AREG2
18953aee3e25SMax Filippov	s32i	a7, a2, PT_SYSCALL
1896fc4fb2adSChris Zankel
18973aee3e25SMax Filippov	GET_THREAD_INFO(a4, a1)
18983aee3e25SMax Filippov	l32i	a3, a4, TI_FLAGS
18993aee3e25SMax Filippov	movi	a4, _TIF_WORK_MASK
19003aee3e25SMax Filippov	and	a3, a3, a4
19013aee3e25SMax Filippov	beqz	a3, 1f
19023aee3e25SMax Filippov
19033aee3e25SMax Filippov	mov	a6, a2
19043aee3e25SMax Filippov	call4	do_syscall_trace_enter
190502ce94c2SMax Filippov	beqz	a6, .Lsyscall_exit
19063aee3e25SMax Filippov	l32i	a7, a2, PT_SYSCALL
19073aee3e25SMax Filippov
19083aee3e25SMax Filippov1:
1909fc4fb2adSChris Zankel	/* syscall = sys_call_table[syscall_nr] */
1910fc4fb2adSChris Zankel
1911f984409aSMax Filippov	movi	a4, sys_call_table
1912a86067f5SFiroz Khan	movi	a5, __NR_syscalls
1913fc4fb2adSChris Zankel	movi	a6, -ENOSYS
19143aee3e25SMax Filippov	bgeu	a7, a5, 1f
1915fc4fb2adSChris Zankel
19163aee3e25SMax Filippov	addx4	a4, a7, a4
1917fc4fb2adSChris Zankel	l32i	a4, a4, 0
1918fc4fb2adSChris Zankel
1919fc4fb2adSChris Zankel	/* Load args: arg0 - arg5 are passed via regs. */
1920fc4fb2adSChris Zankel
1921fc4fb2adSChris Zankel	l32i	a6, a2, PT_AREG6
1922fc4fb2adSChris Zankel	l32i	a7, a2, PT_AREG3
1923fc4fb2adSChris Zankel	l32i	a8, a2, PT_AREG4
1924fc4fb2adSChris Zankel	l32i	a9, a2, PT_AREG5
1925fc4fb2adSChris Zankel	l32i	a10, a2, PT_AREG8
1926fc4fb2adSChris Zankel	l32i	a11, a2, PT_AREG9
1927fc4fb2adSChris Zankel
1928fc4fb2adSChris Zankel	callx4	a4
1929fc4fb2adSChris Zankel
1930fc4fb2adSChris Zankel1:	/* regs->areg[2] = return_value */
1931fc4fb2adSChris Zankel
1932fc4fb2adSChris Zankel	s32i	a6, a2, PT_AREG2
19333aee3e25SMax Filippov	bnez	a3, 1f
193402ce94c2SMax Filippov.Lsyscall_exit:
19359d9043f6SMax Filippov	abi_ret_default
19363aee3e25SMax Filippov
19373aee3e25SMax Filippov1:
1938fc4fb2adSChris Zankel	mov	a6, a2
19392da03d41SMax Filippov	call4	do_syscall_trace_leave
19409d9043f6SMax Filippov	abi_ret_default
1941fc4fb2adSChris Zankel
1942d1538c46SChris ZankelENDPROC(system_call)
1943d1538c46SChris Zankel
1944e2fd1374SMax Filippov/*
1945e2fd1374SMax Filippov * Spill live registers on the kernel stack macro.
1946e2fd1374SMax Filippov *
1947e2fd1374SMax Filippov * Entry condition: ps.woe is set, ps.excm is cleared
1948e2fd1374SMax Filippov * Exit condition: windowstart has single bit set
1949e2fd1374SMax Filippov * May clobber: a12, a13
1950e2fd1374SMax Filippov */
1951e2fd1374SMax Filippov	.macro	spill_registers_kernel
1952e2fd1374SMax Filippov
1953e2fd1374SMax Filippov#if XCHAL_NUM_AREGS > 16
1954e2fd1374SMax Filippov	call12	1f
1955e2fd1374SMax Filippov	_j	2f
1956e2fd1374SMax Filippov	retw
1957e2fd1374SMax Filippov	.align	4
1958e2fd1374SMax Filippov1:
1959e2fd1374SMax Filippov	_entry	a1, 48
1960e2fd1374SMax Filippov	addi	a12, a0, 3
1961e2fd1374SMax Filippov#if XCHAL_NUM_AREGS > 32
1962e2fd1374SMax Filippov	.rept	(XCHAL_NUM_AREGS - 32) / 12
1963e2fd1374SMax Filippov	_entry	a1, 48
1964e2fd1374SMax Filippov	mov	a12, a0
1965e2fd1374SMax Filippov	.endr
1966e2fd1374SMax Filippov#endif
196777d6273eSMax Filippov	_entry	a1, 16
1968e2fd1374SMax Filippov#if XCHAL_NUM_AREGS % 12 == 0
1969e2fd1374SMax Filippov	mov	a8, a8
1970e2fd1374SMax Filippov#elif XCHAL_NUM_AREGS % 12 == 4
1971e2fd1374SMax Filippov	mov	a12, a12
1972e2fd1374SMax Filippov#elif XCHAL_NUM_AREGS % 12 == 8
1973e2fd1374SMax Filippov	mov	a4, a4
1974e2fd1374SMax Filippov#endif
1975e2fd1374SMax Filippov	retw
1976e2fd1374SMax Filippov2:
1977e2fd1374SMax Filippov#else
1978e2fd1374SMax Filippov	mov	a12, a12
1979e2fd1374SMax Filippov#endif
1980e2fd1374SMax Filippov	.endm
1981fc4fb2adSChris Zankel
1982fc4fb2adSChris Zankel/*
19835a0015d6SChris Zankel * Task switch.
19845a0015d6SChris Zankel *
19855a0015d6SChris Zankel * struct task*  _switch_to (struct task* prev, struct task* next)
19865a0015d6SChris Zankel *         a2                              a2                 a3
19875a0015d6SChris Zankel */
19885a0015d6SChris Zankel
19895a0015d6SChris ZankelENTRY(_switch_to)
19905a0015d6SChris Zankel
1991d6d5f19eSMax Filippov	abi_entry(XTENSA_SPILL_STACK_RESERVE)
19925a0015d6SChris Zankel
1993e2fd1374SMax Filippov	mov	a11, a3			# and 'next' (a3)
19945a0015d6SChris Zankel
1995c658eac6SChris Zankel	l32i	a4, a2, TASK_THREAD_INFO
1996c658eac6SChris Zankel	l32i	a5, a3, TASK_THREAD_INFO
19975a0015d6SChris Zankel
1998e2fd1374SMax Filippov	save_xtregs_user a4 a6 a8 a9 a12 a13 THREAD_XTREGS_USER
19995a0015d6SChris Zankel
200052247123SMax Filippov#if THREAD_RA > 1020 || THREAD_SP > 1020
200152247123SMax Filippov	addi	a10, a2, TASK_THREAD
200252247123SMax Filippov	s32i	a0, a10, THREAD_RA - TASK_THREAD	# save return address
200352247123SMax Filippov	s32i	a1, a10, THREAD_SP - TASK_THREAD	# save stack pointer
200452247123SMax Filippov#else
200552247123SMax Filippov	s32i	a0, a2, THREAD_RA	# save return address
200652247123SMax Filippov	s32i	a1, a2, THREAD_SP	# save stack pointer
200752247123SMax Filippov#endif
2008c658eac6SChris Zankel
2009050e9baaSLinus Torvalds#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP)
201040d1a07bSMax Filippov	movi	a6, __stack_chk_guard
201140d1a07bSMax Filippov	l32i	a8, a3, TASK_STACK_CANARY
201240d1a07bSMax Filippov	s32i	a8, a6, 0
201340d1a07bSMax Filippov#endif
201440d1a07bSMax Filippov
2015c658eac6SChris Zankel	/* Disable ints while we manipulate the stack pointer. */
2016c658eac6SChris Zankel
201738fef73cSMax Filippov	irq_save a14, a3
20185a0015d6SChris Zankel	rsync
20195a0015d6SChris Zankel
2020c658eac6SChris Zankel	/* Switch CPENABLE */
2021c658eac6SChris Zankel
2022c658eac6SChris Zankel#if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
2023c658eac6SChris Zankel	l32i	a3, a5, THREAD_CPENABLE
2024bc5378fcSMax Filippov	xsr	a3, cpenable
2025c658eac6SChris Zankel	s32i	a3, a4, THREAD_CPENABLE
2026c658eac6SChris Zankel#endif
2027c658eac6SChris Zankel
2028a0fc1436SMax Filippov#if XCHAL_HAVE_EXCLUSIVE
2029a0fc1436SMax Filippov	l32i	a3, a5, THREAD_ATOMCTL8
2030a0fc1436SMax Filippov	getex	a3
2031a0fc1436SMax Filippov	s32i	a3, a4, THREAD_ATOMCTL8
2032a0fc1436SMax Filippov#endif
2033a0fc1436SMax Filippov
2034c658eac6SChris Zankel	/* Flush register file. */
2035c658eac6SChris Zankel
2036e2fd1374SMax Filippov	spill_registers_kernel
20375a0015d6SChris Zankel
20385a0015d6SChris Zankel	/* Set kernel stack (and leave critical section)
20395a0015d6SChris Zankel	 * Note: It's save to set it here. The stack will not be overwritten
20405a0015d6SChris Zankel	 *       because the kernel stack will only be loaded again after
20415a0015d6SChris Zankel	 *       we return from kernel space.
20425a0015d6SChris Zankel	 */
20435a0015d6SChris Zankel
2044bc5378fcSMax Filippov	rsr	a3, excsave1		# exc_table
2045c658eac6SChris Zankel	addi	a7, a5, PT_REGS_OFFSET
2046c658eac6SChris Zankel	s32i	a7, a3, EXC_TABLE_KSTK
20475a0015d6SChris Zankel
2048c50842dfSChris Zankel	/* restore context of the task 'next' */
20495a0015d6SChris Zankel
2050e2fd1374SMax Filippov	l32i	a0, a11, THREAD_RA	# restore return address
2051e2fd1374SMax Filippov	l32i	a1, a11, THREAD_SP	# restore stack pointer
20525a0015d6SChris Zankel
2053e2fd1374SMax Filippov	load_xtregs_user a5 a6 a8 a9 a12 a13 THREAD_XTREGS_USER
2054c658eac6SChris Zankel
2055bc5378fcSMax Filippov	wsr	a14, ps
20565a0015d6SChris Zankel	rsync
20575a0015d6SChris Zankel
2058d6d5f19eSMax Filippov	abi_ret(XTENSA_SPILL_STACK_RESERVE)
20595a0015d6SChris Zankel
2060d1538c46SChris ZankelENDPROC(_switch_to)
20615a0015d6SChris Zankel
20625a0015d6SChris ZankelENTRY(ret_from_fork)
20635a0015d6SChris Zankel
20645a0015d6SChris Zankel	/* void schedule_tail (struct task_struct *prev)
20655a0015d6SChris Zankel	 * Note: prev is still in a6 (return value from fake call4 frame)
20665a0015d6SChris Zankel	 */
20672da03d41SMax Filippov	call4	schedule_tail
20685a0015d6SChris Zankel
2069fc4fb2adSChris Zankel	mov	a6, a1
20702da03d41SMax Filippov	call4	do_syscall_trace_leave
20715a0015d6SChris Zankel
20725a0015d6SChris Zankel	j	common_exception_return
20735a0015d6SChris Zankel
2074d1538c46SChris ZankelENDPROC(ret_from_fork)
2075d1538c46SChris Zankel
20763306a726SMax Filippov/*
20773306a726SMax Filippov * Kernel thread creation helper
20783306a726SMax Filippov * On entry, set up by copy_thread: a2 = thread_fn, a3 = thread_fn arg
20793306a726SMax Filippov *           left from _switch_to: a6 = prev
20803306a726SMax Filippov */
20813306a726SMax FilippovENTRY(ret_from_kernel_thread)
20823306a726SMax Filippov
20833306a726SMax Filippov	call4	schedule_tail
20843306a726SMax Filippov	mov	a6, a3
20853306a726SMax Filippov	callx4	a2
2086f0a1bf08SMax Filippov	j	common_exception_return
20873306a726SMax Filippov
20883306a726SMax FilippovENDPROC(ret_from_kernel_thread)
2089