xref: /linux-6.15/arch/xtensa/kernel/entry.S (revision aea8e7c8)
15a0015d6SChris Zankel/*
25a0015d6SChris Zankel * arch/xtensa/kernel/entry.S
35a0015d6SChris Zankel *
45a0015d6SChris Zankel * Low-level exception handling
55a0015d6SChris Zankel *
65a0015d6SChris Zankel * This file is subject to the terms and conditions of the GNU General Public
75a0015d6SChris Zankel * License.  See the file "COPYING" in the main directory of this archive
85a0015d6SChris Zankel * for more details.
95a0015d6SChris Zankel *
102d1c645cSMarc Gauthier * Copyright (C) 2004 - 2008 by Tensilica Inc.
115a0015d6SChris Zankel *
125a0015d6SChris Zankel * Chris Zankel <[email protected]>
135a0015d6SChris Zankel *
145a0015d6SChris Zankel */
155a0015d6SChris Zankel
165a0015d6SChris Zankel#include <linux/linkage.h>
170013a854SSam Ravnborg#include <asm/asm-offsets.h>
185a0015d6SChris Zankel#include <asm/processor.h>
194573e398SChris Zankel#include <asm/coprocessor.h>
205a0015d6SChris Zankel#include <asm/thread_info.h>
215a0015d6SChris Zankel#include <asm/uaccess.h>
225a0015d6SChris Zankel#include <asm/unistd.h>
235a0015d6SChris Zankel#include <asm/ptrace.h>
245a0015d6SChris Zankel#include <asm/current.h>
255a0015d6SChris Zankel#include <asm/pgtable.h>
265a0015d6SChris Zankel#include <asm/page.h>
275a0015d6SChris Zankel#include <asm/signal.h>
28173d6681SChris Zankel#include <asm/tlbflush.h>
29367b8112SChris Zankel#include <variant/tie-asm.h>
305a0015d6SChris Zankel
315a0015d6SChris Zankel/* Unimplemented features. */
325a0015d6SChris Zankel
335a0015d6SChris Zankel#undef KERNEL_STACK_OVERFLOW_CHECK
345a0015d6SChris Zankel#undef PREEMPTIBLE_KERNEL
355a0015d6SChris Zankel#undef ALLOCA_EXCEPTION_IN_IRAM
365a0015d6SChris Zankel
375a0015d6SChris Zankel/* Not well tested.
385a0015d6SChris Zankel *
395a0015d6SChris Zankel * - fast_coprocessor
405a0015d6SChris Zankel */
415a0015d6SChris Zankel
425a0015d6SChris Zankel/*
435a0015d6SChris Zankel * Macro to find first bit set in WINDOWBASE from the left + 1
445a0015d6SChris Zankel *
455a0015d6SChris Zankel * 100....0 -> 1
465a0015d6SChris Zankel * 010....0 -> 2
475a0015d6SChris Zankel * 000....1 -> WSBITS
485a0015d6SChris Zankel */
495a0015d6SChris Zankel
505a0015d6SChris Zankel	.macro ffs_ws bit mask
515a0015d6SChris Zankel
525a0015d6SChris Zankel#if XCHAL_HAVE_NSA
535a0015d6SChris Zankel	nsau    \bit, \mask			# 32-WSBITS ... 31 (32 iff 0)
545a0015d6SChris Zankel	addi    \bit, \bit, WSBITS - 32 + 1   	# uppest bit set -> return 1
555a0015d6SChris Zankel#else
565a0015d6SChris Zankel	movi    \bit, WSBITS
575a0015d6SChris Zankel#if WSBITS > 16
585a0015d6SChris Zankel	_bltui  \mask, 0x10000, 99f
595a0015d6SChris Zankel	addi    \bit, \bit, -16
605a0015d6SChris Zankel	extui   \mask, \mask, 16, 16
615a0015d6SChris Zankel#endif
625a0015d6SChris Zankel#if WSBITS > 8
635a0015d6SChris Zankel99:	_bltui  \mask, 0x100, 99f
645a0015d6SChris Zankel	addi    \bit, \bit, -8
655a0015d6SChris Zankel	srli    \mask, \mask, 8
665a0015d6SChris Zankel#endif
675a0015d6SChris Zankel99:	_bltui  \mask, 0x10, 99f
685a0015d6SChris Zankel	addi    \bit, \bit, -4
695a0015d6SChris Zankel	srli    \mask, \mask, 4
705a0015d6SChris Zankel99:	_bltui  \mask, 0x4, 99f
715a0015d6SChris Zankel	addi    \bit, \bit, -2
725a0015d6SChris Zankel	srli    \mask, \mask, 2
735a0015d6SChris Zankel99:	_bltui  \mask, 0x2, 99f
745a0015d6SChris Zankel	addi    \bit, \bit, -1
755a0015d6SChris Zankel99:
765a0015d6SChris Zankel
775a0015d6SChris Zankel#endif
785a0015d6SChris Zankel	.endm
795a0015d6SChris Zankel
805a0015d6SChris Zankel/* ----------------- DEFAULT FIRST LEVEL EXCEPTION HANDLERS ----------------- */
815a0015d6SChris Zankel
825a0015d6SChris Zankel/*
835a0015d6SChris Zankel * First-level exception handler for user exceptions.
845a0015d6SChris Zankel * Save some special registers, extra states and all registers in the AR
855a0015d6SChris Zankel * register file that were in use in the user task, and jump to the common
865a0015d6SChris Zankel * exception code.
875a0015d6SChris Zankel * We save SAR (used to calculate WMASK), and WB and WS (we don't have to
885a0015d6SChris Zankel * save them for kernel exceptions).
895a0015d6SChris Zankel *
905a0015d6SChris Zankel * Entry condition for user_exception:
915a0015d6SChris Zankel *
925a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
935a0015d6SChris Zankel *   a1:	a1
945a0015d6SChris Zankel *   a2:	new stack pointer, original value in depc
955a0015d6SChris Zankel *   a3:	dispatch table
965a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
975a0015d6SChris Zankel *   excsave1:	a3
985a0015d6SChris Zankel *
995a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
1005a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
1015a0015d6SChris Zankel *
1025a0015d6SChris Zankel * Entry condition for _user_exception:
1035a0015d6SChris Zankel *
1045a0015d6SChris Zankel *   a0-a3 and depc have been saved to PT_AREG0...PT_AREG3 and PT_DEPC
1055a0015d6SChris Zankel *   excsave has been restored, and
1065a0015d6SChris Zankel *   stack pointer (a1) has been set.
1075a0015d6SChris Zankel *
1083ad2f3fbSDaniel Mack * Note: _user_exception might be at an odd address. Don't use call0..call12
1095a0015d6SChris Zankel */
1105a0015d6SChris Zankel
1115a0015d6SChris ZankelENTRY(user_exception)
1125a0015d6SChris Zankel
1135a0015d6SChris Zankel	/* Save a2, a3, and depc, restore excsave_1 and set SP. */
1145a0015d6SChris Zankel
115bc5378fcSMax Filippov	xsr	a3, excsave1
116bc5378fcSMax Filippov	rsr	a0, depc
1175a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
1185a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2
1195a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
1205a0015d6SChris Zankel	mov	a1, a2
1215a0015d6SChris Zankel
1225a0015d6SChris Zankel	.globl _user_exception
1235a0015d6SChris Zankel_user_exception:
1245a0015d6SChris Zankel
1255a0015d6SChris Zankel	/* Save SAR and turn off single stepping */
1265a0015d6SChris Zankel
1275a0015d6SChris Zankel	movi	a2, 0
128bc5378fcSMax Filippov	rsr	a3, sar
129bc5378fcSMax Filippov	xsr	a2, icountlevel
1305a0015d6SChris Zankel	s32i	a3, a1, PT_SAR
13129c4dfd9SChris Zankel	s32i	a2, a1, PT_ICOUNTLEVEL
1325a0015d6SChris Zankel
133c50842dfSChris Zankel#if XCHAL_HAVE_THREADPTR
134c50842dfSChris Zankel	rur	a2, threadptr
135c50842dfSChris Zankel	s32i	a2, a1, PT_THREADPTR
136c50842dfSChris Zankel#endif
137c50842dfSChris Zankel
1385a0015d6SChris Zankel	/* Rotate ws so that the current windowbase is at bit0. */
1395a0015d6SChris Zankel	/* Assume ws = xxwww1yyyy. Rotate ws right, so that a2 = yyyyxxwww1 */
1405a0015d6SChris Zankel
141bc5378fcSMax Filippov	rsr	a2, windowbase
142bc5378fcSMax Filippov	rsr	a3, windowstart
1435a0015d6SChris Zankel	ssr	a2
1445a0015d6SChris Zankel	s32i	a2, a1, PT_WINDOWBASE
1455a0015d6SChris Zankel	s32i	a3, a1, PT_WINDOWSTART
1465a0015d6SChris Zankel	slli	a2, a3, 32-WSBITS
1475a0015d6SChris Zankel	src	a2, a3, a2
1485a0015d6SChris Zankel	srli	a2, a2, 32-WSBITS
1495a0015d6SChris Zankel	s32i	a2, a1, PT_WMASK	# needed for restoring registers
1505a0015d6SChris Zankel
1515a0015d6SChris Zankel	/* Save only live registers. */
1525a0015d6SChris Zankel
1535a0015d6SChris Zankel	_bbsi.l	a2, 1, 1f
1545a0015d6SChris Zankel	s32i	a4, a1, PT_AREG4
1555a0015d6SChris Zankel	s32i	a5, a1, PT_AREG5
1565a0015d6SChris Zankel	s32i	a6, a1, PT_AREG6
1575a0015d6SChris Zankel	s32i	a7, a1, PT_AREG7
1585a0015d6SChris Zankel	_bbsi.l	a2, 2, 1f
1595a0015d6SChris Zankel	s32i	a8, a1, PT_AREG8
1605a0015d6SChris Zankel	s32i	a9, a1, PT_AREG9
1615a0015d6SChris Zankel	s32i	a10, a1, PT_AREG10
1625a0015d6SChris Zankel	s32i	a11, a1, PT_AREG11
1635a0015d6SChris Zankel	_bbsi.l	a2, 3, 1f
1645a0015d6SChris Zankel	s32i	a12, a1, PT_AREG12
1655a0015d6SChris Zankel	s32i	a13, a1, PT_AREG13
1665a0015d6SChris Zankel	s32i	a14, a1, PT_AREG14
1675a0015d6SChris Zankel	s32i	a15, a1, PT_AREG15
1685a0015d6SChris Zankel	_bnei	a2, 1, 1f		# only one valid frame?
1695a0015d6SChris Zankel
1705a0015d6SChris Zankel	/* Only one valid frame, skip saving regs. */
1715a0015d6SChris Zankel
1725a0015d6SChris Zankel	j	2f
1735a0015d6SChris Zankel
1745a0015d6SChris Zankel	/* Save the remaining registers.
1755a0015d6SChris Zankel	 * We have to save all registers up to the first '1' from
1765a0015d6SChris Zankel	 * the right, except the current frame (bit 0).
1775a0015d6SChris Zankel	 * Assume a2 is:  001001000110001
1786656920bSChris Zankel	 * All register frames starting from the top field to the marked '1'
1795a0015d6SChris Zankel	 * must be saved.
1805a0015d6SChris Zankel	 */
1815a0015d6SChris Zankel
1825a0015d6SChris Zankel1:	addi	a3, a2, -1		# eliminate '1' in bit 0: yyyyxxww0
1835a0015d6SChris Zankel	neg	a3, a3			# yyyyxxww0 -> YYYYXXWW1+1
1845a0015d6SChris Zankel	and	a3, a3, a2		# max. only one bit is set
1855a0015d6SChris Zankel
1865a0015d6SChris Zankel	/* Find number of frames to save */
1875a0015d6SChris Zankel
1885a0015d6SChris Zankel	ffs_ws	a0, a3			# number of frames to the '1' from left
1895a0015d6SChris Zankel
1905a0015d6SChris Zankel	/* Store information into WMASK:
1915a0015d6SChris Zankel	 * bits 0..3: xxx1 masked lower 4 bits of the rotated windowstart,
1925a0015d6SChris Zankel	 * bits 4...: number of valid 4-register frames
1935a0015d6SChris Zankel	 */
1945a0015d6SChris Zankel
1955a0015d6SChris Zankel	slli	a3, a0, 4		# number of frames to save in bits 8..4
1965a0015d6SChris Zankel	extui	a2, a2, 0, 4		# mask for the first 16 registers
1975a0015d6SChris Zankel	or	a2, a3, a2
1985a0015d6SChris Zankel	s32i	a2, a1, PT_WMASK	# needed when we restore the reg-file
1995a0015d6SChris Zankel
2005a0015d6SChris Zankel	/* Save 4 registers at a time */
2015a0015d6SChris Zankel
2025a0015d6SChris Zankel1:	rotw	-1
2035a0015d6SChris Zankel	s32i	a0, a5, PT_AREG_END - 16
2045a0015d6SChris Zankel	s32i	a1, a5, PT_AREG_END - 12
2055a0015d6SChris Zankel	s32i	a2, a5, PT_AREG_END - 8
2065a0015d6SChris Zankel	s32i	a3, a5, PT_AREG_END - 4
2075a0015d6SChris Zankel	addi	a0, a4, -1
2085a0015d6SChris Zankel	addi	a1, a5, -16
2095a0015d6SChris Zankel	_bnez	a0, 1b
2105a0015d6SChris Zankel
2115a0015d6SChris Zankel	/* WINDOWBASE still in SAR! */
2125a0015d6SChris Zankel
213bc5378fcSMax Filippov	rsr	a2, sar			# original WINDOWBASE
2145a0015d6SChris Zankel	movi	a3, 1
2155a0015d6SChris Zankel	ssl	a2
2165a0015d6SChris Zankel	sll	a3, a3
217bc5378fcSMax Filippov	wsr	a3, windowstart		# set corresponding WINDOWSTART bit
218bc5378fcSMax Filippov	wsr	a2, windowbase		# and WINDOWSTART
2195a0015d6SChris Zankel	rsync
2205a0015d6SChris Zankel
2215a0015d6SChris Zankel	/* We are back to the original stack pointer (a1) */
2225a0015d6SChris Zankel
223c658eac6SChris Zankel2:	/* Now, jump to the common exception handler. */
2245a0015d6SChris Zankel
2255a0015d6SChris Zankel	j	common_exception
2265a0015d6SChris Zankel
227d1538c46SChris ZankelENDPROC(user_exception)
2285a0015d6SChris Zankel
2295a0015d6SChris Zankel/*
2305a0015d6SChris Zankel * First-level exit handler for kernel exceptions
2315a0015d6SChris Zankel * Save special registers and the live window frame.
2325a0015d6SChris Zankel * Note: Even though we changes the stack pointer, we don't have to do a
2335a0015d6SChris Zankel *	 MOVSP here, as we do that when we return from the exception.
2345a0015d6SChris Zankel *	 (See comment in the kernel exception exit code)
2355a0015d6SChris Zankel *
2365a0015d6SChris Zankel * Entry condition for kernel_exception:
2375a0015d6SChris Zankel *
2385a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
2395a0015d6SChris Zankel *   a1:	a1
2405a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
2415a0015d6SChris Zankel *   a3:	dispatch table
2425a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
2435a0015d6SChris Zankel *   excsave_1:	a3
2445a0015d6SChris Zankel *
2455a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
2465a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
2475a0015d6SChris Zankel *
2485a0015d6SChris Zankel * Entry condition for _kernel_exception:
2495a0015d6SChris Zankel *
2505a0015d6SChris Zankel *   a0-a3 and depc have been saved to PT_AREG0...PT_AREG3 and PT_DEPC
2515a0015d6SChris Zankel *   excsave has been restored, and
2525a0015d6SChris Zankel *   stack pointer (a1) has been set.
2535a0015d6SChris Zankel *
2543ad2f3fbSDaniel Mack * Note: _kernel_exception might be at an odd address. Don't use call0..call12
2555a0015d6SChris Zankel */
2565a0015d6SChris Zankel
2575a0015d6SChris ZankelENTRY(kernel_exception)
2585a0015d6SChris Zankel
2595a0015d6SChris Zankel	/* Save a0, a2, a3, DEPC and set SP. */
2605a0015d6SChris Zankel
261bc5378fcSMax Filippov	xsr	a3, excsave1		# restore a3, excsave_1
262bc5378fcSMax Filippov	rsr	a0, depc		# get a2
2635a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
2645a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2
2655a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
2665a0015d6SChris Zankel	mov	a1, a2
2675a0015d6SChris Zankel
2685a0015d6SChris Zankel	.globl _kernel_exception
2695a0015d6SChris Zankel_kernel_exception:
2705a0015d6SChris Zankel
2715a0015d6SChris Zankel	/* Save SAR and turn off single stepping */
2725a0015d6SChris Zankel
2735a0015d6SChris Zankel	movi	a2, 0
274bc5378fcSMax Filippov	rsr	a3, sar
275bc5378fcSMax Filippov	xsr	a2, icountlevel
2765a0015d6SChris Zankel	s32i	a3, a1, PT_SAR
27729c4dfd9SChris Zankel	s32i	a2, a1, PT_ICOUNTLEVEL
2785a0015d6SChris Zankel
2795a0015d6SChris Zankel	/* Rotate ws so that the current windowbase is at bit0. */
2805a0015d6SChris Zankel	/* Assume ws = xxwww1yyyy. Rotate ws right, so that a2 = yyyyxxwww1 */
2815a0015d6SChris Zankel
282bc5378fcSMax Filippov	rsr	a2, windowbase		# don't need to save these, we only
283bc5378fcSMax Filippov	rsr	a3, windowstart		# need shifted windowstart: windowmask
2845a0015d6SChris Zankel	ssr	a2
2855a0015d6SChris Zankel	slli	a2, a3, 32-WSBITS
2865a0015d6SChris Zankel	src	a2, a3, a2
2875a0015d6SChris Zankel	srli	a2, a2, 32-WSBITS
2885a0015d6SChris Zankel	s32i	a2, a1, PT_WMASK	# needed for kernel_exception_exit
2895a0015d6SChris Zankel
2905a0015d6SChris Zankel	/* Save only the live window-frame */
2915a0015d6SChris Zankel
2925a0015d6SChris Zankel	_bbsi.l	a2, 1, 1f
2935a0015d6SChris Zankel	s32i	a4, a1, PT_AREG4
2945a0015d6SChris Zankel	s32i	a5, a1, PT_AREG5
2955a0015d6SChris Zankel	s32i	a6, a1, PT_AREG6
2965a0015d6SChris Zankel	s32i	a7, a1, PT_AREG7
2975a0015d6SChris Zankel	_bbsi.l	a2, 2, 1f
2985a0015d6SChris Zankel	s32i	a8, a1, PT_AREG8
2995a0015d6SChris Zankel	s32i	a9, a1, PT_AREG9
3005a0015d6SChris Zankel	s32i	a10, a1, PT_AREG10
3015a0015d6SChris Zankel	s32i	a11, a1, PT_AREG11
3025a0015d6SChris Zankel	_bbsi.l	a2, 3, 1f
3035a0015d6SChris Zankel	s32i	a12, a1, PT_AREG12
3045a0015d6SChris Zankel	s32i	a13, a1, PT_AREG13
3055a0015d6SChris Zankel	s32i	a14, a1, PT_AREG14
3065a0015d6SChris Zankel	s32i	a15, a1, PT_AREG15
3075a0015d6SChris Zankel
3085a0015d6SChris Zankel1:
3095a0015d6SChris Zankel
3105a0015d6SChris Zankel#ifdef KERNEL_STACK_OVERFLOW_CHECK
3115a0015d6SChris Zankel
3125a0015d6SChris Zankel	/*  Stack overflow check, for debugging  */
3135a0015d6SChris Zankel	extui	a2, a1, TASK_SIZE_BITS,XX
3145a0015d6SChris Zankel	movi	a3, SIZE??
3155a0015d6SChris Zankel	_bge	a2, a3, out_of_stack_panic
3165a0015d6SChris Zankel
3175a0015d6SChris Zankel#endif
3185a0015d6SChris Zankel
3195a0015d6SChris Zankel/*
3205a0015d6SChris Zankel * This is the common exception handler.
3215a0015d6SChris Zankel * We get here from the user exception handler or simply by falling through
3225a0015d6SChris Zankel * from the kernel exception handler.
3235a0015d6SChris Zankel * Save the remaining special registers, switch to kernel mode, and jump
3245a0015d6SChris Zankel * to the second-level exception handler.
3255a0015d6SChris Zankel *
3265a0015d6SChris Zankel */
3275a0015d6SChris Zankel
3285a0015d6SChris Zankelcommon_exception:
3295a0015d6SChris Zankel
33029c4dfd9SChris Zankel	/* Save some registers, disable loops and clear the syscall flag. */
3315a0015d6SChris Zankel
332bc5378fcSMax Filippov	rsr	a2, debugcause
333bc5378fcSMax Filippov	rsr	a3, epc1
3345a0015d6SChris Zankel	s32i	a2, a1, PT_DEBUGCAUSE
3355a0015d6SChris Zankel	s32i	a3, a1, PT_PC
3365a0015d6SChris Zankel
33729c4dfd9SChris Zankel	movi	a2, -1
338bc5378fcSMax Filippov	rsr	a3, excvaddr
33929c4dfd9SChris Zankel	s32i	a2, a1, PT_SYSCALL
3405a0015d6SChris Zankel	movi	a2, 0
3415a0015d6SChris Zankel	s32i	a3, a1, PT_EXCVADDR
342bc5378fcSMax Filippov	xsr	a2, lcount
3435a0015d6SChris Zankel	s32i	a2, a1, PT_LCOUNT
3445a0015d6SChris Zankel
3455a0015d6SChris Zankel	/* It is now save to restore the EXC_TABLE_FIXUP variable. */
3465a0015d6SChris Zankel
347bc5378fcSMax Filippov	rsr	a0, exccause
3485a0015d6SChris Zankel	movi	a3, 0
349bc5378fcSMax Filippov	rsr	a2, excsave1
3505a0015d6SChris Zankel	s32i	a0, a1, PT_EXCCAUSE
3515a0015d6SChris Zankel	s32i	a3, a2, EXC_TABLE_FIXUP
3525a0015d6SChris Zankel
3535a0015d6SChris Zankel	/* All unrecoverable states are saved on stack, now, and a1 is valid,
3545a0015d6SChris Zankel	 * so we can allow exceptions and interrupts (*) again.
3555a0015d6SChris Zankel	 * Set PS(EXCM = 0, UM = 0, RING = 0, OWB = 0, WOE = 1, INTLEVEL = X)
3565a0015d6SChris Zankel	 *
357895666a9SMax Filippov	 * (*) We only allow interrupts if they were previously enabled and
358895666a9SMax Filippov	 *     we're not handling an IRQ
3595a0015d6SChris Zankel	 */
3605a0015d6SChris Zankel
361bc5378fcSMax Filippov	rsr	a3, ps
362895666a9SMax Filippov	addi	a0, a0, -EXCCAUSE_LEVEL1_INTERRUPT
363895666a9SMax Filippov	movi	a2, LOCKLEVEL
3642d1c645cSMarc Gauthier	extui	a3, a3, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
3652d1c645cSMarc Gauthier					# a3 = PS.INTLEVEL
366895666a9SMax Filippov	moveqz	a3, a2, a0		# a3 = LOCKLEVEL iff interrupt
367173d6681SChris Zankel	movi	a2, 1 << PS_WOE_BIT
3685a0015d6SChris Zankel	or	a3, a3, a2
369bc5378fcSMax Filippov	rsr	a0, exccause
370bc5378fcSMax Filippov	xsr	a3, ps
3715a0015d6SChris Zankel
3725a0015d6SChris Zankel	s32i	a3, a1, PT_PS		# save ps
3735a0015d6SChris Zankel
374bc5378fcSMax Filippov	/* Save lbeg, lend */
3755a0015d6SChris Zankel
376bc5378fcSMax Filippov	rsr	a2, lbeg
377bc5378fcSMax Filippov	rsr	a3, lend
3785a0015d6SChris Zankel	s32i	a2, a1, PT_LBEG
3795a0015d6SChris Zankel	s32i	a3, a1, PT_LEND
3805a0015d6SChris Zankel
381733536b8SMax Filippov	/* Save SCOMPARE1 */
382733536b8SMax Filippov
383733536b8SMax Filippov#if XCHAL_HAVE_S32C1I
384733536b8SMax Filippov	rsr     a2, scompare1
385733536b8SMax Filippov	s32i    a2, a1, PT_SCOMPARE1
386733536b8SMax Filippov#endif
387733536b8SMax Filippov
388c658eac6SChris Zankel	/* Save optional registers. */
389c658eac6SChris Zankel
390c658eac6SChris Zankel	save_xtregs_opt a1 a2 a4 a5 a6 a7 PT_XTREGS_OPT
391c658eac6SChris Zankel
392c92931b2SMax Filippov#ifdef CONFIG_TRACE_IRQFLAGS
393c92931b2SMax Filippov	l32i	a4, a1, PT_DEPC
394c92931b2SMax Filippov	/* Double exception means we came here with an exception
395c92931b2SMax Filippov	 * while PS.EXCM was set, i.e. interrupts disabled.
396c92931b2SMax Filippov	 */
397c92931b2SMax Filippov	bgeui	a4, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
398c92931b2SMax Filippov	l32i	a4, a1, PT_EXCCAUSE
399c92931b2SMax Filippov	bnei	a4, EXCCAUSE_LEVEL1_INTERRUPT, 1f
400c92931b2SMax Filippov	/* We came here with an interrupt means interrupts were enabled
401c92931b2SMax Filippov	 * and we've just disabled them.
402c92931b2SMax Filippov	 */
403c92931b2SMax Filippov	movi	a4, trace_hardirqs_off
404c92931b2SMax Filippov	callx4	a4
405c92931b2SMax Filippov1:
406c92931b2SMax Filippov#endif
407c92931b2SMax Filippov
4085a0015d6SChris Zankel	/* Go to second-level dispatcher. Set up parameters to pass to the
4095a0015d6SChris Zankel	 * exception handler and call the exception handler.
4105a0015d6SChris Zankel	 */
4115a0015d6SChris Zankel
4125a0015d6SChris Zankel	movi	a4, exc_table
4135a0015d6SChris Zankel	mov	a6, a1			# pass stack frame
4145a0015d6SChris Zankel	mov	a7, a0			# pass EXCCAUSE
4155a0015d6SChris Zankel	addx4	a4, a0, a4
4165a0015d6SChris Zankel	l32i	a4, a4, EXC_TABLE_DEFAULT		# load handler
4175a0015d6SChris Zankel
4185a0015d6SChris Zankel	/* Call the second-level handler */
4195a0015d6SChris Zankel
4205a0015d6SChris Zankel	callx4	a4
4215a0015d6SChris Zankel
4225a0015d6SChris Zankel	/* Jump here for exception exit */
423e6ffe17eSdann	.global common_exception_return
4245a0015d6SChris Zankelcommon_exception_return:
4255a0015d6SChris Zankel
426c92931b2SMax Filippov1:
427*aea8e7c8SMax Filippov	rsil	a2, LOCKLEVEL
428c92931b2SMax Filippov
4295a0015d6SChris Zankel	/* Jump if we are returning from kernel exceptions. */
4305a0015d6SChris Zankel
431*aea8e7c8SMax Filippov	l32i	a3, a1, PT_PS
432e1088430SChris Zankel	_bbci.l	a3, PS_UM_BIT, 4f
4335a0015d6SChris Zankel
4345a0015d6SChris Zankel	/* Specific to a user exception exit:
4355a0015d6SChris Zankel	 * We need to check some flags for signal handling and rescheduling,
4365a0015d6SChris Zankel	 * and have to restore WB and WS, extra states, and all registers
4375a0015d6SChris Zankel	 * in the register file that were in use in the user task.
438e1088430SChris Zankel	 * Note that we don't disable interrupts here.
4395a0015d6SChris Zankel	 */
4405a0015d6SChris Zankel
4415a0015d6SChris Zankel	GET_THREAD_INFO(a2,a1)
4425a0015d6SChris Zankel	l32i	a4, a2, TI_FLAGS
4435a0015d6SChris Zankel
4445a0015d6SChris Zankel	_bbsi.l	a4, TIF_NEED_RESCHED, 3f
445a53bb24eSAl Viro	_bbsi.l	a4, TIF_NOTIFY_RESUME, 2f
446a99e07eeSMax Filippov	_bbci.l	a4, TIF_SIGPENDING, 5f
4475a0015d6SChris Zankel
448a53bb24eSAl Viro2:	l32i	a4, a1, PT_DEPC
4495a0015d6SChris Zankel	bgeui	a4, VALID_DOUBLE_EXCEPTION_ADDRESS, 4f
4505a0015d6SChris Zankel
451e1088430SChris Zankel	/* Call do_signal() */
452e1088430SChris Zankel
453*aea8e7c8SMax Filippov	rsil	a2, 0
454a53bb24eSAl Viro	movi	a4, do_notify_resume	# int do_notify_resume(struct pt_regs*)
4555a0015d6SChris Zankel	mov	a6, a1
4565a0015d6SChris Zankel	callx4	a4
4575a0015d6SChris Zankel	j	1b
4585a0015d6SChris Zankel
459e1088430SChris Zankel3:	/* Reschedule */
4605a0015d6SChris Zankel
461*aea8e7c8SMax Filippov	rsil	a2, 0
4625a0015d6SChris Zankel	movi	a4, schedule	# void schedule (void)
4635a0015d6SChris Zankel	callx4	a4
4645a0015d6SChris Zankel	j	1b
4655a0015d6SChris Zankel
466a99e07eeSMax Filippov5:
467a99e07eeSMax Filippov#ifdef CONFIG_DEBUG_TLB_SANITY
468a99e07eeSMax Filippov	l32i	a4, a1, PT_DEPC
469a99e07eeSMax Filippov	bgeui	a4, VALID_DOUBLE_EXCEPTION_ADDRESS, 4f
470a99e07eeSMax Filippov	movi	a4, check_tlb_sanity
471a99e07eeSMax Filippov	callx4	a4
472a99e07eeSMax Filippov#endif
473*aea8e7c8SMax Filippov4:
474*aea8e7c8SMax Filippov#ifdef CONFIG_TRACE_IRQFLAGS
475*aea8e7c8SMax Filippov	l32i	a4, a1, PT_DEPC
476*aea8e7c8SMax Filippov	/* Double exception means we came here with an exception
477*aea8e7c8SMax Filippov	 * while PS.EXCM was set, i.e. interrupts disabled.
478*aea8e7c8SMax Filippov	 */
479*aea8e7c8SMax Filippov	bgeui	a4, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
480*aea8e7c8SMax Filippov	l32i	a4, a1, PT_EXCCAUSE
481*aea8e7c8SMax Filippov	bnei	a4, EXCCAUSE_LEVEL1_INTERRUPT, 1f
482*aea8e7c8SMax Filippov	/* We came here with an interrupt means interrupts were enabled
483*aea8e7c8SMax Filippov	 * and we'll reenable them on return.
484*aea8e7c8SMax Filippov	 */
485*aea8e7c8SMax Filippov	movi	a4, trace_hardirqs_on
486*aea8e7c8SMax Filippov	callx4	a4
487*aea8e7c8SMax Filippov1:
488*aea8e7c8SMax Filippov#endif
489*aea8e7c8SMax Filippov	/* Restore optional registers. */
4905a0015d6SChris Zankel
491e1088430SChris Zankel	load_xtregs_opt a1 a2 a4 a5 a6 a7 PT_XTREGS_OPT
492e1088430SChris Zankel
493733536b8SMax Filippov	/* Restore SCOMPARE1 */
494733536b8SMax Filippov
495733536b8SMax Filippov#if XCHAL_HAVE_S32C1I
496733536b8SMax Filippov	l32i    a2, a1, PT_SCOMPARE1
497733536b8SMax Filippov	wsr     a2, scompare1
498733536b8SMax Filippov#endif
499bc5378fcSMax Filippov	wsr	a3, ps		/* disable interrupts */
500e1088430SChris Zankel
501e1088430SChris Zankel	_bbci.l	a3, PS_UM_BIT, kernel_exception_exit
502e1088430SChris Zankel
503e1088430SChris Zankeluser_exception_exit:
504e1088430SChris Zankel
505e1088430SChris Zankel	/* Restore the state of the task and return from the exception. */
5065a0015d6SChris Zankel
5075a0015d6SChris Zankel	/* Switch to the user thread WINDOWBASE. Save SP temporarily in DEPC */
5085a0015d6SChris Zankel
5095a0015d6SChris Zankel	l32i	a2, a1, PT_WINDOWBASE
5105a0015d6SChris Zankel	l32i	a3, a1, PT_WINDOWSTART
511bc5378fcSMax Filippov	wsr	a1, depc		# use DEPC as temp storage
512bc5378fcSMax Filippov	wsr	a3, windowstart		# restore WINDOWSTART
5135a0015d6SChris Zankel	ssr	a2			# preserve user's WB in the SAR
514bc5378fcSMax Filippov	wsr	a2, windowbase		# switch to user's saved WB
5155a0015d6SChris Zankel	rsync
516bc5378fcSMax Filippov	rsr	a1, depc		# restore stack pointer
5175a0015d6SChris Zankel	l32i	a2, a1, PT_WMASK	# register frames saved (in bits 4...9)
5185a0015d6SChris Zankel	rotw	-1			# we restore a4..a7
5195a0015d6SChris Zankel	_bltui	a6, 16, 1f		# only have to restore current window?
5205a0015d6SChris Zankel
5215a0015d6SChris Zankel	/* The working registers are a0 and a3.  We are restoring to
5225a0015d6SChris Zankel	 * a4..a7.  Be careful not to destroy what we have just restored.
5235a0015d6SChris Zankel	 * Note: wmask has the format YYYYM:
5245a0015d6SChris Zankel	 *       Y: number of registers saved in groups of 4
5255a0015d6SChris Zankel	 *       M: 4 bit mask of first 16 registers
5265a0015d6SChris Zankel	 */
5275a0015d6SChris Zankel
5285a0015d6SChris Zankel	mov	a2, a6
5295a0015d6SChris Zankel	mov	a3, a5
5305a0015d6SChris Zankel
5315a0015d6SChris Zankel2:	rotw	-1			# a0..a3 become a4..a7
5325a0015d6SChris Zankel	addi	a3, a7, -4*4		# next iteration
5335a0015d6SChris Zankel	addi	a2, a6, -16		# decrementing Y in WMASK
5345a0015d6SChris Zankel	l32i	a4, a3, PT_AREG_END + 0
5355a0015d6SChris Zankel	l32i	a5, a3, PT_AREG_END + 4
5365a0015d6SChris Zankel	l32i	a6, a3, PT_AREG_END + 8
5375a0015d6SChris Zankel	l32i	a7, a3, PT_AREG_END + 12
5385a0015d6SChris Zankel	_bgeui	a2, 16, 2b
5395a0015d6SChris Zankel
5405a0015d6SChris Zankel	/* Clear unrestored registers (don't leak anything to user-land */
5415a0015d6SChris Zankel
542bc5378fcSMax Filippov1:	rsr	a0, windowbase
543bc5378fcSMax Filippov	rsr	a3, sar
5445a0015d6SChris Zankel	sub	a3, a0, a3
5455a0015d6SChris Zankel	beqz	a3, 2f
5465a0015d6SChris Zankel	extui	a3, a3, 0, WBBITS
5475a0015d6SChris Zankel
5485a0015d6SChris Zankel1:	rotw	-1
5495a0015d6SChris Zankel	addi	a3, a7, -1
5505a0015d6SChris Zankel	movi	a4, 0
5515a0015d6SChris Zankel	movi	a5, 0
5525a0015d6SChris Zankel	movi	a6, 0
5535a0015d6SChris Zankel	movi	a7, 0
5545a0015d6SChris Zankel	bgei	a3, 1, 1b
5555a0015d6SChris Zankel
5565a0015d6SChris Zankel	/* We are back were we were when we started.
5575a0015d6SChris Zankel	 * Note: a2 still contains WMASK (if we've returned to the original
5585a0015d6SChris Zankel	 *	 frame where we had loaded a2), or at least the lower 4 bits
5595a0015d6SChris Zankel	 *	 (if we have restored WSBITS-1 frames).
5605a0015d6SChris Zankel	 */
5615a0015d6SChris Zankel
562c50842dfSChris Zankel#if XCHAL_HAVE_THREADPTR
563c50842dfSChris Zankel	l32i	a3, a1, PT_THREADPTR
564c50842dfSChris Zankel	wur	a3, threadptr
565c50842dfSChris Zankel#endif
566c50842dfSChris Zankel
5675a0015d6SChris Zankel2:	j	common_exception_exit
5685a0015d6SChris Zankel
5695a0015d6SChris Zankel	/* This is the kernel exception exit.
5705a0015d6SChris Zankel	 * We avoided to do a MOVSP when we entered the exception, but we
5715a0015d6SChris Zankel	 * have to do it here.
5725a0015d6SChris Zankel	 */
5735a0015d6SChris Zankel
5745a0015d6SChris Zankelkernel_exception_exit:
5755a0015d6SChris Zankel
5765a0015d6SChris Zankel#ifdef PREEMPTIBLE_KERNEL
5775a0015d6SChris Zankel
5785a0015d6SChris Zankel#ifdef CONFIG_PREEMPT
5795a0015d6SChris Zankel
5805a0015d6SChris Zankel	/*
5815a0015d6SChris Zankel	 * Note: We've just returned from a call4, so we have
5825a0015d6SChris Zankel	 * at least 4 addt'l regs.
5835a0015d6SChris Zankel	 */
5845a0015d6SChris Zankel
5855a0015d6SChris Zankel	/* Check current_thread_info->preempt_count */
5865a0015d6SChris Zankel
5875a0015d6SChris Zankel	GET_THREAD_INFO(a2)
5885a0015d6SChris Zankel	l32i	a3, a2, TI_PREEMPT
5895a0015d6SChris Zankel	bnez	a3, 1f
5905a0015d6SChris Zankel
5915a0015d6SChris Zankel	l32i	a2, a2, TI_FLAGS
5925a0015d6SChris Zankel
5935a0015d6SChris Zankel1:
5945a0015d6SChris Zankel
5955a0015d6SChris Zankel#endif
5965a0015d6SChris Zankel
5975a0015d6SChris Zankel#endif
5985a0015d6SChris Zankel
5995a0015d6SChris Zankel	/* Check if we have to do a movsp.
6005a0015d6SChris Zankel	 *
6015a0015d6SChris Zankel	 * We only have to do a movsp if the previous window-frame has
6025a0015d6SChris Zankel	 * been spilled to the *temporary* exception stack instead of the
6035a0015d6SChris Zankel	 * task's stack. This is the case if the corresponding bit in
6045a0015d6SChris Zankel	 * WINDOWSTART for the previous window-frame was set before
6055a0015d6SChris Zankel	 * (not spilled) but is zero now (spilled).
6065a0015d6SChris Zankel	 * If this bit is zero, all other bits except the one for the
6075a0015d6SChris Zankel	 * current window frame are also zero. So, we can use a simple test:
6085a0015d6SChris Zankel	 * 'and' WINDOWSTART and WINDOWSTART-1:
6095a0015d6SChris Zankel	 *
6105a0015d6SChris Zankel	 *  (XXXXXX1[0]* - 1) AND XXXXXX1[0]* = XXXXXX0[0]*
6115a0015d6SChris Zankel	 *
6125a0015d6SChris Zankel	 * The result is zero only if one bit was set.
6135a0015d6SChris Zankel	 *
6145a0015d6SChris Zankel	 * (Note: We might have gone through several task switches before
6155a0015d6SChris Zankel	 *        we come back to the current task, so WINDOWBASE might be
6165a0015d6SChris Zankel	 *        different from the time the exception occurred.)
6175a0015d6SChris Zankel	 */
6185a0015d6SChris Zankel
6195a0015d6SChris Zankel	/* Test WINDOWSTART before and after the exception.
6205a0015d6SChris Zankel	 * We actually have WMASK, so we only have to test if it is 1 or not.
6215a0015d6SChris Zankel	 */
6225a0015d6SChris Zankel
6235a0015d6SChris Zankel	l32i	a2, a1, PT_WMASK
6245a0015d6SChris Zankel	_beqi	a2, 1, common_exception_exit	# Spilled before exception,jump
6255a0015d6SChris Zankel
6265a0015d6SChris Zankel	/* Test WINDOWSTART now. If spilled, do the movsp */
6275a0015d6SChris Zankel
628bc5378fcSMax Filippov	rsr     a3, windowstart
6295a0015d6SChris Zankel	addi	a0, a3, -1
6305a0015d6SChris Zankel	and     a3, a3, a0
6315a0015d6SChris Zankel	_bnez	a3, common_exception_exit
6325a0015d6SChris Zankel
6335a0015d6SChris Zankel	/* Do a movsp (we returned from a call4, so we have at least a0..a7) */
6345a0015d6SChris Zankel
6355a0015d6SChris Zankel	addi    a0, a1, -16
6365a0015d6SChris Zankel	l32i    a3, a0, 0
6375a0015d6SChris Zankel	l32i    a4, a0, 4
6385a0015d6SChris Zankel	s32i    a3, a1, PT_SIZE+0
6395a0015d6SChris Zankel	s32i    a4, a1, PT_SIZE+4
6405a0015d6SChris Zankel	l32i    a3, a0, 8
6415a0015d6SChris Zankel	l32i    a4, a0, 12
6425a0015d6SChris Zankel	s32i    a3, a1, PT_SIZE+8
6435a0015d6SChris Zankel	s32i    a4, a1, PT_SIZE+12
6445a0015d6SChris Zankel
6455a0015d6SChris Zankel	/* Common exception exit.
6465a0015d6SChris Zankel	 * We restore the special register and the current window frame, and
6475a0015d6SChris Zankel	 * return from the exception.
6485a0015d6SChris Zankel	 *
6495a0015d6SChris Zankel	 * Note: We expect a2 to hold PT_WMASK
6505a0015d6SChris Zankel	 */
6515a0015d6SChris Zankel
6525a0015d6SChris Zankelcommon_exception_exit:
6535a0015d6SChris Zankel
654c658eac6SChris Zankel	/* Restore address registers. */
655c658eac6SChris Zankel
6565a0015d6SChris Zankel	_bbsi.l	a2, 1, 1f
6575a0015d6SChris Zankel	l32i	a4,  a1, PT_AREG4
6585a0015d6SChris Zankel	l32i	a5,  a1, PT_AREG5
6595a0015d6SChris Zankel	l32i	a6,  a1, PT_AREG6
6605a0015d6SChris Zankel	l32i	a7,  a1, PT_AREG7
6615a0015d6SChris Zankel	_bbsi.l	a2, 2, 1f
6625a0015d6SChris Zankel	l32i	a8,  a1, PT_AREG8
6635a0015d6SChris Zankel	l32i	a9,  a1, PT_AREG9
6645a0015d6SChris Zankel	l32i	a10, a1, PT_AREG10
6655a0015d6SChris Zankel	l32i	a11, a1, PT_AREG11
6665a0015d6SChris Zankel	_bbsi.l	a2, 3, 1f
6675a0015d6SChris Zankel	l32i	a12, a1, PT_AREG12
6685a0015d6SChris Zankel	l32i	a13, a1, PT_AREG13
6695a0015d6SChris Zankel	l32i	a14, a1, PT_AREG14
6705a0015d6SChris Zankel	l32i	a15, a1, PT_AREG15
6715a0015d6SChris Zankel
6725a0015d6SChris Zankel	/* Restore PC, SAR */
6735a0015d6SChris Zankel
6745a0015d6SChris Zankel1:	l32i	a2, a1, PT_PC
6755a0015d6SChris Zankel	l32i	a3, a1, PT_SAR
676bc5378fcSMax Filippov	wsr	a2, epc1
677bc5378fcSMax Filippov	wsr	a3, sar
6785a0015d6SChris Zankel
6795a0015d6SChris Zankel	/* Restore LBEG, LEND, LCOUNT */
6805a0015d6SChris Zankel
6815a0015d6SChris Zankel	l32i	a2, a1, PT_LBEG
6825a0015d6SChris Zankel	l32i	a3, a1, PT_LEND
683bc5378fcSMax Filippov	wsr	a2, lbeg
6845a0015d6SChris Zankel	l32i	a2, a1, PT_LCOUNT
685bc5378fcSMax Filippov	wsr	a3, lend
686bc5378fcSMax Filippov	wsr	a2, lcount
6875a0015d6SChris Zankel
68829c4dfd9SChris Zankel	/* We control single stepping through the ICOUNTLEVEL register. */
68929c4dfd9SChris Zankel
69029c4dfd9SChris Zankel	l32i	a2, a1, PT_ICOUNTLEVEL
69129c4dfd9SChris Zankel	movi	a3, -2
692bc5378fcSMax Filippov	wsr	a2, icountlevel
693bc5378fcSMax Filippov	wsr	a3, icount
69429c4dfd9SChris Zankel
6955a0015d6SChris Zankel	/* Check if it was double exception. */
6965a0015d6SChris Zankel
6975a0015d6SChris Zankel	l32i	a0, a1, PT_DEPC
6985a0015d6SChris Zankel	l32i	a3, a1, PT_AREG3
6992d1c645cSMarc Gauthier	l32i	a2, a1, PT_AREG2
700895666a9SMax Filippov	_bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
7012d1c645cSMarc Gauthier
7025a0015d6SChris Zankel	/* Restore a0...a3 and return */
7035a0015d6SChris Zankel
7045a0015d6SChris Zankel	l32i	a0, a1, PT_AREG0
7055a0015d6SChris Zankel	l32i	a1, a1, PT_AREG1
7065a0015d6SChris Zankel	rfe
7075a0015d6SChris Zankel
708895666a9SMax Filippov1: 	wsr	a0, depc
709895666a9SMax Filippov	l32i	a0, a1, PT_AREG0
710895666a9SMax Filippov	l32i	a1, a1, PT_AREG1
711895666a9SMax Filippov	rfde
7125a0015d6SChris Zankel
713d1538c46SChris ZankelENDPROC(kernel_exception)
714d1538c46SChris Zankel
7155a0015d6SChris Zankel/*
7165a0015d6SChris Zankel * Debug exception handler.
7175a0015d6SChris Zankel *
7185a0015d6SChris Zankel * Currently, we don't support KGDB, so only user application can be debugged.
7195a0015d6SChris Zankel *
7205a0015d6SChris Zankel * When we get here,  a0 is trashed and saved to excsave[debuglevel]
7215a0015d6SChris Zankel */
7225a0015d6SChris Zankel
7235a0015d6SChris ZankelENTRY(debug_exception)
7245a0015d6SChris Zankel
725bc5378fcSMax Filippov	rsr	a0, SREG_EPS + XCHAL_DEBUGLEVEL
726173d6681SChris Zankel	bbsi.l	a0, PS_EXCM_BIT, 1f	# exception mode
7275a0015d6SChris Zankel
728bc5378fcSMax Filippov	/* Set EPC1 and EXCCAUSE */
7295a0015d6SChris Zankel
730bc5378fcSMax Filippov	wsr	a2, depc		# save a2 temporarily
731bc5378fcSMax Filippov	rsr	a2, SREG_EPC + XCHAL_DEBUGLEVEL
732bc5378fcSMax Filippov	wsr	a2, epc1
7335a0015d6SChris Zankel
7345a0015d6SChris Zankel	movi	a2, EXCCAUSE_MAPPED_DEBUG
735bc5378fcSMax Filippov	wsr	a2, exccause
7365a0015d6SChris Zankel
7375a0015d6SChris Zankel	/* Restore PS to the value before the debug exc but with PS.EXCM set.*/
7385a0015d6SChris Zankel
739173d6681SChris Zankel	movi	a2, 1 << PS_EXCM_BIT
7405a0015d6SChris Zankel	or	a2, a0, a2
7415a0015d6SChris Zankel	movi	a0, debug_exception	# restore a3, debug jump vector
742bc5378fcSMax Filippov	wsr	a2, ps
743bc5378fcSMax Filippov	xsr	a0, SREG_EXCSAVE + XCHAL_DEBUGLEVEL
7445a0015d6SChris Zankel
7455a0015d6SChris Zankel	/* Switch to kernel/user stack, restore jump vector, and save a0 */
7465a0015d6SChris Zankel
747173d6681SChris Zankel	bbsi.l	a2, PS_UM_BIT, 2f	# jump if user mode
7485a0015d6SChris Zankel
7495a0015d6SChris Zankel	addi	a2, a1, -16-PT_SIZE	# assume kernel stack
7505a0015d6SChris Zankel	s32i	a0, a2, PT_AREG0
7515a0015d6SChris Zankel	movi	a0, 0
7525a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
7535a0015d6SChris Zankel	s32i	a0, a2, PT_DEPC		# mark it as a regular exception
754bc5378fcSMax Filippov	xsr	a0, depc
7555a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
7565a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2
7575a0015d6SChris Zankel	mov	a1, a2
7585a0015d6SChris Zankel	j	_kernel_exception
7595a0015d6SChris Zankel
760bc5378fcSMax Filippov2:	rsr	a2, excsave1
7615a0015d6SChris Zankel	l32i	a2, a2, EXC_TABLE_KSTK	# load kernel stack pointer
7625a0015d6SChris Zankel	s32i	a0, a2, PT_AREG0
7635a0015d6SChris Zankel	movi	a0, 0
7645a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
7655a0015d6SChris Zankel	s32i	a0, a2, PT_DEPC
766bc5378fcSMax Filippov	xsr	a0, depc
7675a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
7685a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2
7695a0015d6SChris Zankel	mov	a1, a2
7705a0015d6SChris Zankel	j	_user_exception
7715a0015d6SChris Zankel
7725a0015d6SChris Zankel	/* Debug exception while in exception mode. */
7735a0015d6SChris Zankel1:	j	1b	// FIXME!!
7745a0015d6SChris Zankel
775d1538c46SChris ZankelENDPROC(debug_exception)
7765a0015d6SChris Zankel
7775a0015d6SChris Zankel/*
7785a0015d6SChris Zankel * We get here in case of an unrecoverable exception.
7795a0015d6SChris Zankel * The only thing we can do is to be nice and print a panic message.
7805a0015d6SChris Zankel * We only produce a single stack frame for panic, so ???
7815a0015d6SChris Zankel *
7825a0015d6SChris Zankel *
7835a0015d6SChris Zankel * Entry conditions:
7845a0015d6SChris Zankel *
7855a0015d6SChris Zankel *   - a0 contains the caller address; original value saved in excsave1.
7865a0015d6SChris Zankel *   - the original a0 contains a valid return address (backtrace) or 0.
7875a0015d6SChris Zankel *   - a2 contains a valid stackpointer
7885a0015d6SChris Zankel *
7895a0015d6SChris Zankel * Notes:
7905a0015d6SChris Zankel *
7915a0015d6SChris Zankel *   - If the stack pointer could be invalid, the caller has to setup a
7925a0015d6SChris Zankel *     dummy stack pointer (e.g. the stack of the init_task)
7935a0015d6SChris Zankel *
7945a0015d6SChris Zankel *   - If the return address could be invalid, the caller has to set it
7955a0015d6SChris Zankel *     to 0, so the backtrace would stop.
7965a0015d6SChris Zankel *
7975a0015d6SChris Zankel */
7985a0015d6SChris Zankel	.align 4
7995a0015d6SChris Zankelunrecoverable_text:
8005a0015d6SChris Zankel	.ascii "Unrecoverable error in exception handler\0"
8015a0015d6SChris Zankel
8025a0015d6SChris ZankelENTRY(unrecoverable_exception)
8035a0015d6SChris Zankel
8045a0015d6SChris Zankel	movi	a0, 1
8055a0015d6SChris Zankel	movi	a1, 0
8065a0015d6SChris Zankel
807bc5378fcSMax Filippov	wsr	a0, windowstart
808bc5378fcSMax Filippov	wsr	a1, windowbase
8095a0015d6SChris Zankel	rsync
8105a0015d6SChris Zankel
8112d1c645cSMarc Gauthier	movi	a1, (1 << PS_WOE_BIT) | LOCKLEVEL
812bc5378fcSMax Filippov	wsr	a1, ps
8135a0015d6SChris Zankel	rsync
8145a0015d6SChris Zankel
8155a0015d6SChris Zankel	movi	a1, init_task
8165a0015d6SChris Zankel	movi	a0, 0
8175a0015d6SChris Zankel	addi	a1, a1, PT_REGS_OFFSET
8185a0015d6SChris Zankel
8195a0015d6SChris Zankel	movi	a4, panic
8205a0015d6SChris Zankel	movi	a6, unrecoverable_text
8215a0015d6SChris Zankel
8225a0015d6SChris Zankel	callx4	a4
8235a0015d6SChris Zankel
8245a0015d6SChris Zankel1:	j	1b
8255a0015d6SChris Zankel
826d1538c46SChris ZankelENDPROC(unrecoverable_exception)
8275a0015d6SChris Zankel
8285a0015d6SChris Zankel/* -------------------------- FAST EXCEPTION HANDLERS ----------------------- */
8295a0015d6SChris Zankel
8305a0015d6SChris Zankel/*
8315a0015d6SChris Zankel * Fast-handler for alloca exceptions
8325a0015d6SChris Zankel *
8335a0015d6SChris Zankel *  The ALLOCA handler is entered when user code executes the MOVSP
8345a0015d6SChris Zankel *  instruction and the caller's frame is not in the register file.
8355a0015d6SChris Zankel *  In this case, the caller frame's a0..a3 are on the stack just
8365a0015d6SChris Zankel *  below sp (a1), and this handler moves them.
8375a0015d6SChris Zankel *
8385a0015d6SChris Zankel *  For "MOVSP <ar>,<as>" without destination register a1, this routine
8395a0015d6SChris Zankel *  simply moves the value from <as> to <ar> without moving the save area.
8405a0015d6SChris Zankel *
8415a0015d6SChris Zankel * Entry condition:
8425a0015d6SChris Zankel *
8435a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
8445a0015d6SChris Zankel *   a1:	a1
8455a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
8465a0015d6SChris Zankel *   a3:	dispatch table
8475a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
8485a0015d6SChris Zankel *   excsave_1:	a3
8495a0015d6SChris Zankel *
8505a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
8515a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
8525a0015d6SChris Zankel */
8535a0015d6SChris Zankel
8545a0015d6SChris Zankel#if XCHAL_HAVE_BE
8555a0015d6SChris Zankel#define _EXTUI_MOVSP_SRC(ar)	extui ar, ar, 4, 4
8565a0015d6SChris Zankel#define _EXTUI_MOVSP_DST(ar)	extui ar, ar, 0, 4
8575a0015d6SChris Zankel#else
8585a0015d6SChris Zankel#define _EXTUI_MOVSP_SRC(ar)	extui ar, ar, 0, 4
8595a0015d6SChris Zankel#define _EXTUI_MOVSP_DST(ar)	extui ar, ar, 4, 4
8605a0015d6SChris Zankel#endif
8615a0015d6SChris Zankel
8625a0015d6SChris ZankelENTRY(fast_alloca)
8635a0015d6SChris Zankel
8645a0015d6SChris Zankel	/* We shouldn't be in a double exception. */
8655a0015d6SChris Zankel
8665a0015d6SChris Zankel	l32i	a0, a2, PT_DEPC
8675a0015d6SChris Zankel	_bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, .Lunhandled_double
8685a0015d6SChris Zankel
869bc5378fcSMax Filippov	rsr	a0, depc		# get a2
8705a0015d6SChris Zankel	s32i	a4, a2, PT_AREG4	# save a4 and
8715a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2	# a2 to stack
8725a0015d6SChris Zankel
8735a0015d6SChris Zankel	/* Exit critical section. */
8745a0015d6SChris Zankel
8755a0015d6SChris Zankel	movi	a0, 0
8765a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_FIXUP
8775a0015d6SChris Zankel
8785a0015d6SChris Zankel	/* Restore a3, excsave_1 */
8795a0015d6SChris Zankel
880bc5378fcSMax Filippov	xsr	a3, excsave1		# make sure excsave_1 is valid for dbl.
881bc5378fcSMax Filippov	rsr	a4, epc1		# get exception address
8825a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3	# save a3 to stack
8835a0015d6SChris Zankel
8845a0015d6SChris Zankel#ifdef ALLOCA_EXCEPTION_IN_IRAM
8855a0015d6SChris Zankel#error	iram not supported
8865a0015d6SChris Zankel#else
8875a0015d6SChris Zankel	/* Note: l8ui not allowed in IRAM/IROM!! */
8885a0015d6SChris Zankel	l8ui	a0, a4, 1		# read as(src) from MOVSP instruction
8895a0015d6SChris Zankel#endif
8905a0015d6SChris Zankel	movi	a3, .Lmovsp_src
8915a0015d6SChris Zankel	_EXTUI_MOVSP_SRC(a0)		# extract source register number
8925a0015d6SChris Zankel	addx8	a3, a0, a3
8935a0015d6SChris Zankel	jx	a3
8945a0015d6SChris Zankel
8955a0015d6SChris Zankel.Lunhandled_double:
896bc5378fcSMax Filippov	wsr	a0, excsave1
8975a0015d6SChris Zankel	movi	a0, unrecoverable_exception
8985a0015d6SChris Zankel	callx0	a0
8995a0015d6SChris Zankel
9005a0015d6SChris Zankel	.align 8
9015a0015d6SChris Zankel.Lmovsp_src:
9025a0015d6SChris Zankel	l32i	a3, a2, PT_AREG0;	_j 1f;	.align 8
9035a0015d6SChris Zankel	mov	a3, a1;			_j 1f;	.align 8
9045a0015d6SChris Zankel	l32i	a3, a2, PT_AREG2;	_j 1f;	.align 8
9055a0015d6SChris Zankel	l32i	a3, a2, PT_AREG3;	_j 1f;	.align 8
9065a0015d6SChris Zankel	l32i	a3, a2, PT_AREG4;	_j 1f;	.align 8
9075a0015d6SChris Zankel	mov	a3, a5;			_j 1f;	.align 8
9085a0015d6SChris Zankel	mov	a3, a6;			_j 1f;	.align 8
9095a0015d6SChris Zankel	mov	a3, a7;			_j 1f;	.align 8
9105a0015d6SChris Zankel	mov	a3, a8;			_j 1f;	.align 8
9115a0015d6SChris Zankel	mov	a3, a9;			_j 1f;	.align 8
9125a0015d6SChris Zankel	mov	a3, a10;		_j 1f;	.align 8
9135a0015d6SChris Zankel	mov	a3, a11;		_j 1f;	.align 8
9145a0015d6SChris Zankel	mov	a3, a12;		_j 1f;	.align 8
9155a0015d6SChris Zankel	mov	a3, a13;		_j 1f;	.align 8
9165a0015d6SChris Zankel	mov	a3, a14;		_j 1f;	.align 8
9175a0015d6SChris Zankel	mov	a3, a15;		_j 1f;	.align 8
9185a0015d6SChris Zankel
9195a0015d6SChris Zankel1:
9205a0015d6SChris Zankel
9215a0015d6SChris Zankel#ifdef ALLOCA_EXCEPTION_IN_IRAM
9225a0015d6SChris Zankel#error	iram not supported
9235a0015d6SChris Zankel#else
9245a0015d6SChris Zankel	l8ui	a0, a4, 0		# read ar(dst) from MOVSP instruction
9255a0015d6SChris Zankel#endif
9265a0015d6SChris Zankel	addi	a4, a4, 3		# step over movsp
9275a0015d6SChris Zankel	_EXTUI_MOVSP_DST(a0)		# extract destination register
928bc5378fcSMax Filippov	wsr	a4, epc1		# save new epc_1
9295a0015d6SChris Zankel
9305a0015d6SChris Zankel	_bnei	a0, 1, 1f		# no 'movsp a1, ax': jump
9315a0015d6SChris Zankel
9325a0015d6SChris Zankel	/* Move the save area. This implies the use of the L32E
9335a0015d6SChris Zankel	 * and S32E instructions, because this move must be done with
9345a0015d6SChris Zankel	 * the user's PS.RING privilege levels, not with ring 0
9355a0015d6SChris Zankel	 * (kernel's) privileges currently active with PS.EXCM
9365a0015d6SChris Zankel	 * set. Note that we have stil registered a fixup routine with the
9375a0015d6SChris Zankel	 * double exception vector in case a double exception occurs.
9385a0015d6SChris Zankel	 */
9395a0015d6SChris Zankel
9405a0015d6SChris Zankel	/* a0,a4:avail a1:old user stack a2:exc. stack a3:new user stack. */
9415a0015d6SChris Zankel
9425a0015d6SChris Zankel	l32e	a0, a1, -16
9435a0015d6SChris Zankel	l32e	a4, a1, -12
9445a0015d6SChris Zankel	s32e	a0, a3, -16
9455a0015d6SChris Zankel	s32e	a4, a3, -12
9465a0015d6SChris Zankel	l32e	a0, a1, -8
9475a0015d6SChris Zankel	l32e	a4, a1, -4
9485a0015d6SChris Zankel	s32e	a0, a3, -8
9495a0015d6SChris Zankel	s32e	a4, a3, -4
9505a0015d6SChris Zankel
9515a0015d6SChris Zankel	/* Restore stack-pointer and all the other saved registers. */
9525a0015d6SChris Zankel
9535a0015d6SChris Zankel	mov	a1, a3
9545a0015d6SChris Zankel
9555a0015d6SChris Zankel	l32i	a4, a2, PT_AREG4
9565a0015d6SChris Zankel	l32i	a3, a2, PT_AREG3
9575a0015d6SChris Zankel	l32i	a0, a2, PT_AREG0
9585a0015d6SChris Zankel	l32i	a2, a2, PT_AREG2
9595a0015d6SChris Zankel	rfe
9605a0015d6SChris Zankel
9615a0015d6SChris Zankel	/*  MOVSP <at>,<as>  was invoked with <at> != a1.
9625a0015d6SChris Zankel	 *  Because the stack pointer is not being modified,
9635a0015d6SChris Zankel	 *  we should be able to just modify the pointer
9645a0015d6SChris Zankel	 *  without moving any save area.
9655a0015d6SChris Zankel	 *  The processor only traps these occurrences if the
9665a0015d6SChris Zankel	 *  caller window isn't live, so unfortunately we can't
9675a0015d6SChris Zankel	 *  use this as an alternate trap mechanism.
9685a0015d6SChris Zankel	 *  So we just do the move.  This requires that we
9695a0015d6SChris Zankel	 *  resolve the destination register, not just the source,
9705a0015d6SChris Zankel	 *  so there's some extra work.
9715a0015d6SChris Zankel	 *  (PERHAPS NOT REALLY NEEDED, BUT CLEANER...)
9725a0015d6SChris Zankel	 */
9735a0015d6SChris Zankel
9745a0015d6SChris Zankel	/* a0 dst-reg, a1 user-stack, a2 stack, a3 value of src reg. */
9755a0015d6SChris Zankel
9765a0015d6SChris Zankel1:	movi	a4, .Lmovsp_dst
9775a0015d6SChris Zankel	addx8	a4, a0, a4
9785a0015d6SChris Zankel	jx	a4
9795a0015d6SChris Zankel
9805a0015d6SChris Zankel	.align 8
9815a0015d6SChris Zankel.Lmovsp_dst:
9825a0015d6SChris Zankel	s32i	a3, a2, PT_AREG0;	_j 1f;	.align 8
9835a0015d6SChris Zankel	mov	a1, a3;			_j 1f;	.align 8
9845a0015d6SChris Zankel	s32i	a3, a2, PT_AREG2;	_j 1f;	.align 8
9855a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3;	_j 1f;	.align 8
9865a0015d6SChris Zankel	s32i	a3, a2, PT_AREG4;	_j 1f;	.align 8
9875a0015d6SChris Zankel	mov	a5, a3;			_j 1f;	.align 8
9885a0015d6SChris Zankel	mov	a6, a3;			_j 1f;	.align 8
9895a0015d6SChris Zankel	mov	a7, a3;			_j 1f;	.align 8
9905a0015d6SChris Zankel	mov	a8, a3;			_j 1f;	.align 8
9915a0015d6SChris Zankel	mov	a9, a3;			_j 1f;	.align 8
9925a0015d6SChris Zankel	mov	a10, a3;		_j 1f;	.align 8
9935a0015d6SChris Zankel	mov	a11, a3;		_j 1f;	.align 8
9945a0015d6SChris Zankel	mov	a12, a3;		_j 1f;	.align 8
9955a0015d6SChris Zankel	mov	a13, a3;		_j 1f;	.align 8
9965a0015d6SChris Zankel	mov	a14, a3;		_j 1f;	.align 8
9975a0015d6SChris Zankel	mov	a15, a3;		_j 1f;	.align 8
9985a0015d6SChris Zankel
9995a0015d6SChris Zankel1:	l32i	a4, a2, PT_AREG4
10005a0015d6SChris Zankel	l32i	a3, a2, PT_AREG3
10015a0015d6SChris Zankel	l32i	a0, a2, PT_AREG0
10025a0015d6SChris Zankel	l32i	a2, a2, PT_AREG2
10035a0015d6SChris Zankel	rfe
10045a0015d6SChris Zankel
1005d1538c46SChris ZankelENDPROC(fast_alloca)
10065a0015d6SChris Zankel
10075a0015d6SChris Zankel/*
10085a0015d6SChris Zankel * fast system calls.
10095a0015d6SChris Zankel *
10105a0015d6SChris Zankel * WARNING:  The kernel doesn't save the entire user context before
10115a0015d6SChris Zankel * handling a fast system call.  These functions are small and short,
10125a0015d6SChris Zankel * usually offering some functionality not available to user tasks.
10135a0015d6SChris Zankel *
10145a0015d6SChris Zankel * BE CAREFUL TO PRESERVE THE USER'S CONTEXT.
10155a0015d6SChris Zankel *
10165a0015d6SChris Zankel * Entry condition:
10175a0015d6SChris Zankel *
10185a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
10195a0015d6SChris Zankel *   a1:	a1
10205a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
10215a0015d6SChris Zankel *   a3:	dispatch table
10225a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
10235a0015d6SChris Zankel *   excsave_1:	a3
10245a0015d6SChris Zankel */
10255a0015d6SChris Zankel
10265a0015d6SChris ZankelENTRY(fast_syscall_kernel)
10275a0015d6SChris Zankel
10285a0015d6SChris Zankel	/* Skip syscall. */
10295a0015d6SChris Zankel
1030bc5378fcSMax Filippov	rsr	a0, epc1
10315a0015d6SChris Zankel	addi	a0, a0, 3
1032bc5378fcSMax Filippov	wsr	a0, epc1
10335a0015d6SChris Zankel
10345a0015d6SChris Zankel	l32i	a0, a2, PT_DEPC
10355a0015d6SChris Zankel	bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, fast_syscall_unrecoverable
10365a0015d6SChris Zankel
1037bc5378fcSMax Filippov	rsr	a0, depc			# get syscall-nr
10385a0015d6SChris Zankel	_beqz	a0, fast_syscall_spill_registers
1039fc4fb2adSChris Zankel	_beqi	a0, __NR_xtensa, fast_syscall_xtensa
10405a0015d6SChris Zankel
10415a0015d6SChris Zankel	j	kernel_exception
10425a0015d6SChris Zankel
1043d1538c46SChris ZankelENDPROC(fast_syscall_kernel)
1044d1538c46SChris Zankel
10455a0015d6SChris ZankelENTRY(fast_syscall_user)
10465a0015d6SChris Zankel
10475a0015d6SChris Zankel	/* Skip syscall. */
10485a0015d6SChris Zankel
1049bc5378fcSMax Filippov	rsr	a0, epc1
10505a0015d6SChris Zankel	addi	a0, a0, 3
1051bc5378fcSMax Filippov	wsr	a0, epc1
10525a0015d6SChris Zankel
10535a0015d6SChris Zankel	l32i	a0, a2, PT_DEPC
10545a0015d6SChris Zankel	bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, fast_syscall_unrecoverable
10555a0015d6SChris Zankel
1056bc5378fcSMax Filippov	rsr	a0, depc			# get syscall-nr
10575a0015d6SChris Zankel	_beqz	a0, fast_syscall_spill_registers
1058fc4fb2adSChris Zankel	_beqi	a0, __NR_xtensa, fast_syscall_xtensa
10595a0015d6SChris Zankel
10605a0015d6SChris Zankel	j	user_exception
10615a0015d6SChris Zankel
1062d1538c46SChris ZankelENDPROC(fast_syscall_user)
1063d1538c46SChris Zankel
10645a0015d6SChris ZankelENTRY(fast_syscall_unrecoverable)
10655a0015d6SChris Zankel
10665a0015d6SChris Zankel	/* Restore all states. */
10675a0015d6SChris Zankel
10685a0015d6SChris Zankel	l32i    a0, a2, PT_AREG0        # restore a0
1069bc5378fcSMax Filippov	xsr     a2, depc                # restore a2, depc
1070bc5378fcSMax Filippov	rsr     a3, excsave1
10715a0015d6SChris Zankel
1072bc5378fcSMax Filippov	wsr     a0, excsave1
10735a0015d6SChris Zankel	movi    a0, unrecoverable_exception
10745a0015d6SChris Zankel	callx0  a0
10755a0015d6SChris Zankel
1076d1538c46SChris ZankelENDPROC(fast_syscall_unrecoverable)
10775a0015d6SChris Zankel
10785a0015d6SChris Zankel/*
10795a0015d6SChris Zankel * sysxtensa syscall handler
10805a0015d6SChris Zankel *
1081fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_SET,     ptr, val,    unused);
1082fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_ADD,     ptr, val,    unused);
1083fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_EXG_ADD, ptr, val,    unused);
1084fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_CMP_SWP, ptr, oldval, newval);
10855a0015d6SChris Zankel *        a2            a6                   a3    a4      a5
10865a0015d6SChris Zankel *
10875a0015d6SChris Zankel * Entry condition:
10885a0015d6SChris Zankel *
1089fc4fb2adSChris Zankel *   a0:	a2 (syscall-nr), original value saved on stack (PT_AREG0)
10905a0015d6SChris Zankel *   a1:	a1
1091fc4fb2adSChris Zankel *   a2:	new stack pointer, original in a0 and DEPC
1092fc4fb2adSChris Zankel *   a3:	dispatch table, original in excsave_1
1093fc4fb2adSChris Zankel *   a4..a15:	unchanged
10945a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
10955a0015d6SChris Zankel *   excsave_1:	a3
10965a0015d6SChris Zankel *
10975a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
10985a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
10995a0015d6SChris Zankel *
11005a0015d6SChris Zankel * Note: we don't have to save a2; a2 holds the return value
11015a0015d6SChris Zankel *
11025a0015d6SChris Zankel * We use the two macros TRY and CATCH:
11035a0015d6SChris Zankel *
11045a0015d6SChris Zankel * TRY	 adds an entry to the __ex_table fixup table for the immediately
11055a0015d6SChris Zankel *	 following instruction.
11065a0015d6SChris Zankel *
110725985edcSLucas De Marchi * CATCH catches any exception that occurred at one of the preceding TRY
11085a0015d6SChris Zankel *       statements and continues from there
11095a0015d6SChris Zankel *
11105a0015d6SChris Zankel * Usage TRY	l32i	a0, a1, 0
11115a0015d6SChris Zankel *		<other code>
11125a0015d6SChris Zankel *	 done:	rfe
11135a0015d6SChris Zankel *	 CATCH	<set return code>
11145a0015d6SChris Zankel *		j done
11155a0015d6SChris Zankel */
11165a0015d6SChris Zankel
11175a0015d6SChris Zankel#define TRY								\
11185a0015d6SChris Zankel	.section __ex_table, "a";					\
11195a0015d6SChris Zankel	.word	66f, 67f;						\
11205a0015d6SChris Zankel	.text;								\
11215a0015d6SChris Zankel66:
11225a0015d6SChris Zankel
11235a0015d6SChris Zankel#define CATCH								\
11245a0015d6SChris Zankel67:
11255a0015d6SChris Zankel
1126fc4fb2adSChris ZankelENTRY(fast_syscall_xtensa)
11275a0015d6SChris Zankel
1128bc5378fcSMax Filippov	xsr	a3, excsave1		# restore a3, excsave1
11295a0015d6SChris Zankel
1130fc4fb2adSChris Zankel	s32i	a7, a2, PT_AREG7	# we need an additional register
11315a0015d6SChris Zankel	movi	a7, 4			# sizeof(unsigned int)
1132fc4fb2adSChris Zankel	access_ok a3, a7, a0, a2, .Leac	# a0: scratch reg, a2: sp
11335a0015d6SChris Zankel
1134fc4fb2adSChris Zankel	addi	a6, a6, -1		# assuming SYS_XTENSA_ATOMIC_SET = 1
1135fc4fb2adSChris Zankel	_bgeui	a6, SYS_XTENSA_COUNT - 1, .Lill
1136fc4fb2adSChris Zankel	_bnei	a6, SYS_XTENSA_ATOMIC_CMP_SWP - 1, .Lnswp
11375a0015d6SChris Zankel
1138fc4fb2adSChris Zankel	/* Fall through for ATOMIC_CMP_SWP. */
11395a0015d6SChris Zankel
11405a0015d6SChris Zankel.Lswp:	/* Atomic compare and swap */
11415a0015d6SChris Zankel
1142fc4fb2adSChris ZankelTRY	l32i	a0, a3, 0		# read old value
1143fc4fb2adSChris Zankel	bne	a0, a4, 1f		# same as old value? jump
1144fc4fb2adSChris ZankelTRY	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	addi	a6, a6, 1		# restore a6 (really necessary?)
1149fc4fb2adSChris Zankel	rfe
11505a0015d6SChris Zankel
1151fc4fb2adSChris Zankel1:	l32i	a7, a2, PT_AREG7	# restore a7
1152fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1153fc4fb2adSChris Zankel	movi	a2, 0			# return 0 (note that we cannot set
1154fc4fb2adSChris Zankel	addi	a6, a6, 1		# restore a6 (really necessary?)
1155fc4fb2adSChris Zankel	rfe
11565a0015d6SChris Zankel
1157fc4fb2adSChris Zankel.Lnswp:	/* Atomic set, add, and exg_add. */
11585a0015d6SChris Zankel
1159fc4fb2adSChris ZankelTRY	l32i	a7, a3, 0		# orig
1160fc4fb2adSChris Zankel	add	a0, a4, a7		# + arg
1161fc4fb2adSChris Zankel	moveqz	a0, a4, a6		# set
1162fc4fb2adSChris ZankelTRY	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
1168fc4fb2adSChris Zankel	addi	a6, a6, 1		# restore a6 (really necessary?)
11695a0015d6SChris Zankel	rfe
11705a0015d6SChris Zankel
11715a0015d6SChris ZankelCATCH
1172fc4fb2adSChris Zankel.Leac:	l32i	a7, a2, PT_AREG7	# restore a7
1173fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1174fc4fb2adSChris Zankel	movi	a2, -EFAULT
1175fc4fb2adSChris Zankel	rfe
1176fc4fb2adSChris Zankel
1177fc4fb2adSChris Zankel.Lill:	l32i	a7, a2, PT_AREG0	# restore a7
1178fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1179fc4fb2adSChris Zankel	movi	a2, -EINVAL
1180fc4fb2adSChris Zankel	rfe
1181fc4fb2adSChris Zankel
1182d1538c46SChris ZankelENDPROC(fast_syscall_xtensa)
11835a0015d6SChris Zankel
11845a0015d6SChris Zankel
11855a0015d6SChris Zankel/* fast_syscall_spill_registers.
11865a0015d6SChris Zankel *
11875a0015d6SChris Zankel * Entry condition:
11885a0015d6SChris Zankel *
11895a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
11905a0015d6SChris Zankel *   a1:	a1
11915a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
11925a0015d6SChris Zankel *   a3:	dispatch table
11935a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
11945a0015d6SChris Zankel *   excsave_1:	a3
11955a0015d6SChris Zankel *
11965a0015d6SChris Zankel * Note: We assume the stack pointer is EXC_TABLE_KSTK in the fixup handler.
11975a0015d6SChris Zankel */
11985a0015d6SChris Zankel
11995a0015d6SChris ZankelENTRY(fast_syscall_spill_registers)
12005a0015d6SChris Zankel
12015a0015d6SChris Zankel	/* Register a FIXUP handler (pass current wb as a parameter) */
12025a0015d6SChris Zankel
12035a0015d6SChris Zankel	movi	a0, fast_syscall_spill_registers_fixup
12045a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_FIXUP
1205bc5378fcSMax Filippov	rsr	a0, windowbase
12065a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_PARAM
12075a0015d6SChris Zankel
12085a0015d6SChris Zankel	/* Save a3 and SAR on stack. */
12095a0015d6SChris Zankel
1210bc5378fcSMax Filippov	rsr	a0, sar
1211bc5378fcSMax Filippov	xsr	a3, excsave1		# restore a3 and excsave_1
12125a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
1213c658eac6SChris Zankel	s32i	a4, a2, PT_AREG4
1214c658eac6SChris Zankel	s32i	a0, a2, PT_AREG5	# store SAR to PT_AREG5
12155a0015d6SChris Zankel
12165a0015d6SChris Zankel	/* The spill routine might clobber a7, a11, and a15. */
12175a0015d6SChris Zankel
1218c658eac6SChris Zankel	s32i	a7, a2, PT_AREG7
1219c658eac6SChris Zankel	s32i	a11, a2, PT_AREG11
1220c658eac6SChris Zankel	s32i	a15, a2, PT_AREG15
12215a0015d6SChris Zankel
1222c658eac6SChris Zankel	call0	_spill_registers	# destroys a3, a4, and SAR
12235a0015d6SChris Zankel
12245a0015d6SChris Zankel	/* Advance PC, restore registers and SAR, and return from exception. */
12255a0015d6SChris Zankel
1226c658eac6SChris Zankel	l32i	a3, a2, PT_AREG5
1227c658eac6SChris Zankel	l32i	a4, a2, PT_AREG4
12285a0015d6SChris Zankel	l32i	a0, a2, PT_AREG0
1229bc5378fcSMax Filippov	wsr	a3, sar
12305a0015d6SChris Zankel	l32i	a3, a2, PT_AREG3
12315a0015d6SChris Zankel
12325a0015d6SChris Zankel	/* Restore clobbered registers. */
12335a0015d6SChris Zankel
1234c658eac6SChris Zankel	l32i	a7, a2, PT_AREG7
1235c658eac6SChris Zankel	l32i	a11, a2, PT_AREG11
1236c658eac6SChris Zankel	l32i	a15, a2, PT_AREG15
12375a0015d6SChris Zankel
12385a0015d6SChris Zankel	movi	a2, 0
12395a0015d6SChris Zankel	rfe
12405a0015d6SChris Zankel
1241d1538c46SChris ZankelENDPROC(fast_syscall_spill_registers)
1242d1538c46SChris Zankel
12435a0015d6SChris Zankel/* Fixup handler.
12445a0015d6SChris Zankel *
12455a0015d6SChris Zankel * We get here if the spill routine causes an exception, e.g. tlb miss.
12465a0015d6SChris Zankel * We basically restore WINDOWBASE and WINDOWSTART to the condition when
12475a0015d6SChris Zankel * we entered the spill routine and jump to the user exception handler.
12485a0015d6SChris Zankel *
12495a0015d6SChris Zankel * a0: value of depc, original value in depc
12505a0015d6SChris Zankel * a2: trashed, original value in EXC_TABLE_DOUBLE_SAVE
12515a0015d6SChris Zankel * a3: exctable, original value in excsave1
12525a0015d6SChris Zankel */
12535a0015d6SChris Zankel
12545a0015d6SChris Zankelfast_syscall_spill_registers_fixup:
12555a0015d6SChris Zankel
1256bc5378fcSMax Filippov	rsr	a2, windowbase	# get current windowbase (a2 is saved)
1257bc5378fcSMax Filippov	xsr	a0, depc	# restore depc and a0
12585a0015d6SChris Zankel	ssl	a2		# set shift (32 - WB)
12595a0015d6SChris Zankel
12605a0015d6SChris Zankel	/* We need to make sure the current registers (a0-a3) are preserved.
12615a0015d6SChris Zankel	 * To do this, we simply set the bit for the current window frame
12625a0015d6SChris Zankel	 * in WS, so that the exception handlers save them to the task stack.
12635a0015d6SChris Zankel	 */
12645a0015d6SChris Zankel
1265bc5378fcSMax Filippov	rsr	a3, excsave1	# get spill-mask
12665a0015d6SChris Zankel	slli	a2, a3, 1	# shift left by one
12675a0015d6SChris Zankel
12685a0015d6SChris Zankel	slli	a3, a2, 32-WSBITS
12695a0015d6SChris Zankel	src	a2, a2, a3	# a1 = xxwww1yyxxxwww1yy......
1270bc5378fcSMax Filippov	wsr	a2, windowstart	# set corrected windowstart
12715a0015d6SChris Zankel
12725a0015d6SChris Zankel	movi	a3, exc_table
12735a0015d6SChris Zankel	l32i	a2, a3, EXC_TABLE_DOUBLE_SAVE	# restore a2
12745a0015d6SChris Zankel	l32i	a3, a3, EXC_TABLE_PARAM	# original WB (in user task)
12755a0015d6SChris Zankel
12765a0015d6SChris Zankel	/* Return to the original (user task) WINDOWBASE.
12775a0015d6SChris Zankel	 * We leave the following frame behind:
12785a0015d6SChris Zankel	 * a0, a1, a2	same
12795a0015d6SChris Zankel	 * a3:		trashed (saved in excsave_1)
12805a0015d6SChris Zankel	 * depc:	depc (we have to return to that address)
12815a0015d6SChris Zankel	 * excsave_1:	a3
12825a0015d6SChris Zankel	 */
12835a0015d6SChris Zankel
1284bc5378fcSMax Filippov	wsr	a3, windowbase
12855a0015d6SChris Zankel	rsync
12865a0015d6SChris Zankel
12875a0015d6SChris Zankel	/* We are now in the original frame when we entered _spill_registers:
12885a0015d6SChris Zankel	 *  a0: return address
12895a0015d6SChris Zankel	 *  a1: used, stack pointer
12905a0015d6SChris Zankel	 *  a2: kernel stack pointer
12915a0015d6SChris Zankel	 *  a3: available, saved in EXCSAVE_1
12925a0015d6SChris Zankel	 *  depc: exception address
12935a0015d6SChris Zankel	 *  excsave: a3
12945a0015d6SChris Zankel	 * Note: This frame might be the same as above.
12955a0015d6SChris Zankel	 */
12965a0015d6SChris Zankel
12975a0015d6SChris Zankel	/* Setup stack pointer. */
12985a0015d6SChris Zankel
12995a0015d6SChris Zankel	addi	a2, a2, -PT_USER_SIZE
13005a0015d6SChris Zankel	s32i	a0, a2, PT_AREG0
13015a0015d6SChris Zankel
13025a0015d6SChris Zankel	/* Make sure we return to this fixup handler. */
13035a0015d6SChris Zankel
13045a0015d6SChris Zankel	movi	a3, fast_syscall_spill_registers_fixup_return
13055a0015d6SChris Zankel	s32i	a3, a2, PT_DEPC		# setup depc
13065a0015d6SChris Zankel
13075a0015d6SChris Zankel	/* Jump to the exception handler. */
13085a0015d6SChris Zankel
13095a0015d6SChris Zankel	movi	a3, exc_table
1310bc5378fcSMax Filippov	rsr	a0, exccause
13115a0015d6SChris Zankel	addx4	a0, a0, a3              	# find entry in table
13125a0015d6SChris Zankel	l32i	a0, a0, EXC_TABLE_FAST_USER     # load handler
13135a0015d6SChris Zankel	jx	a0
13145a0015d6SChris Zankel
13155a0015d6SChris Zankelfast_syscall_spill_registers_fixup_return:
13165a0015d6SChris Zankel
13175a0015d6SChris Zankel	/* When we return here, all registers have been restored (a2: DEPC) */
13185a0015d6SChris Zankel
1319bc5378fcSMax Filippov	wsr	a2, depc		# exception address
13205a0015d6SChris Zankel
13215a0015d6SChris Zankel	/* Restore fixup handler. */
13225a0015d6SChris Zankel
1323bc5378fcSMax Filippov	xsr	a3, excsave1
13245a0015d6SChris Zankel	movi	a2, fast_syscall_spill_registers_fixup
13255a0015d6SChris Zankel	s32i	a2, a3, EXC_TABLE_FIXUP
1326bc5378fcSMax Filippov	rsr	a2, windowbase
13275a0015d6SChris Zankel	s32i	a2, a3, EXC_TABLE_PARAM
13285a0015d6SChris Zankel	l32i	a2, a3, EXC_TABLE_KSTK
13295a0015d6SChris Zankel
13305a0015d6SChris Zankel	/* Load WB at the time the exception occurred. */
13315a0015d6SChris Zankel
1332bc5378fcSMax Filippov	rsr	a3, sar			# WB is still in SAR
13335a0015d6SChris Zankel	neg	a3, a3
1334bc5378fcSMax Filippov	wsr	a3, windowbase
13355a0015d6SChris Zankel	rsync
13365a0015d6SChris Zankel
13375a0015d6SChris Zankel	/* Restore a3 and return. */
13385a0015d6SChris Zankel
13395a0015d6SChris Zankel	movi	a3, exc_table
1340bc5378fcSMax Filippov	xsr	a3, excsave1
13415a0015d6SChris Zankel
13425a0015d6SChris Zankel	rfde
13435a0015d6SChris Zankel
13445a0015d6SChris Zankel
13455a0015d6SChris Zankel/*
13465a0015d6SChris Zankel * spill all registers.
13475a0015d6SChris Zankel *
13485a0015d6SChris Zankel * This is not a real function. The following conditions must be met:
13495a0015d6SChris Zankel *
13505a0015d6SChris Zankel *  - must be called with call0.
1351c658eac6SChris Zankel *  - uses a3, a4 and SAR.
13525a0015d6SChris Zankel *  - the last 'valid' register of each frame are clobbered.
13535a0015d6SChris Zankel *  - the caller must have registered a fixup handler
13545a0015d6SChris Zankel *    (or be inside a critical section)
13555a0015d6SChris Zankel *  - PS_EXCM must be set (PS_WOE cleared?)
13565a0015d6SChris Zankel */
13575a0015d6SChris Zankel
13585a0015d6SChris ZankelENTRY(_spill_registers)
13595a0015d6SChris Zankel
13605a0015d6SChris Zankel	/*
13615a0015d6SChris Zankel	 * Rotate ws so that the current windowbase is at bit 0.
13625a0015d6SChris Zankel	 * Assume ws = xxxwww1yy (www1 current window frame).
1363c658eac6SChris Zankel	 * Rotate ws right so that a4 = yyxxxwww1.
13645a0015d6SChris Zankel	 */
13655a0015d6SChris Zankel
1366bc5378fcSMax Filippov	rsr	a4, windowbase
1367bc5378fcSMax Filippov	rsr	a3, windowstart		# a3 = xxxwww1yy
1368c658eac6SChris Zankel	ssr	a4			# holds WB
1369c658eac6SChris Zankel	slli	a4, a3, WSBITS
1370c658eac6SChris Zankel	or	a3, a3, a4		# a3 = xxxwww1yyxxxwww1yy
1371ea0b6b06SChris Zankel	srl	a3, a3			# a3 = 00xxxwww1yyxxxwww1
13725a0015d6SChris Zankel
13735a0015d6SChris Zankel	/* We are done if there are no more than the current register frame. */
13745a0015d6SChris Zankel
137550c0716aSChris Zankel	extui	a3, a3, 1, WSBITS-1	# a3 = 0yyxxxwww
1376c658eac6SChris Zankel	movi	a4, (1 << (WSBITS-1))
13775a0015d6SChris Zankel	_beqz	a3, .Lnospill		# only one active frame? jump
13785a0015d6SChris Zankel
13795a0015d6SChris Zankel	/* We want 1 at the top, so that we return to the current windowbase */
13805a0015d6SChris Zankel
1381c658eac6SChris Zankel	or	a3, a3, a4		# 1yyxxxwww
13825a0015d6SChris Zankel
13835a0015d6SChris Zankel	/* Skip empty frames - get 'oldest' WINDOWSTART-bit. */
13845a0015d6SChris Zankel
1385bc5378fcSMax Filippov	wsr	a3, windowstart		# save shifted windowstart
1386c658eac6SChris Zankel	neg	a4, a3
1387c658eac6SChris Zankel	and	a3, a4, a3		# first bit set from right: 000010000
13885a0015d6SChris Zankel
1389c658eac6SChris Zankel	ffs_ws	a4, a3			# a4: shifts to skip empty frames
13905a0015d6SChris Zankel	movi	a3, WSBITS
1391c658eac6SChris Zankel	sub	a4, a3, a4		# WSBITS-a4:number of 0-bits from right
1392c658eac6SChris Zankel	ssr	a4			# save in SAR for later.
13935a0015d6SChris Zankel
1394bc5378fcSMax Filippov	rsr	a3, windowbase
1395c658eac6SChris Zankel	add	a3, a3, a4
1396bc5378fcSMax Filippov	wsr	a3, windowbase
13975a0015d6SChris Zankel	rsync
13985a0015d6SChris Zankel
1399bc5378fcSMax Filippov	rsr	a3, windowstart
14005a0015d6SChris Zankel	srl	a3, a3			# shift windowstart
14015a0015d6SChris Zankel
14025a0015d6SChris Zankel	/* WB is now just one frame below the oldest frame in the register
14035a0015d6SChris Zankel	   window. WS is shifted so the oldest frame is in bit 0, thus, WB
14045a0015d6SChris Zankel	   and WS differ by one 4-register frame. */
14055a0015d6SChris Zankel
14065a0015d6SChris Zankel	/* Save frames. Depending what call was used (call4, call8, call12),
14075a0015d6SChris Zankel	 * we have to save 4,8. or 12 registers.
14085a0015d6SChris Zankel	 */
14095a0015d6SChris Zankel
14105a0015d6SChris Zankel	_bbsi.l	a3, 1, .Lc4
14115a0015d6SChris Zankel	_bbsi.l	a3, 2, .Lc8
14125a0015d6SChris Zankel
14135a0015d6SChris Zankel	/* Special case: we have a call12-frame starting at a4. */
14145a0015d6SChris Zankel
14155a0015d6SChris Zankel	_bbci.l	a3, 3, .Lc12	# bit 3 shouldn't be zero! (Jump to Lc12 first)
14165a0015d6SChris Zankel
14175a0015d6SChris Zankel	s32e	a4, a1, -16	# a1 is valid with an empty spill area
14185a0015d6SChris Zankel	l32e	a4, a5, -12
14195a0015d6SChris Zankel	s32e	a8, a4, -48
14205a0015d6SChris Zankel	mov	a8, a4
14215a0015d6SChris Zankel	l32e	a4, a1, -16
14225a0015d6SChris Zankel	j	.Lc12c
14235a0015d6SChris Zankel
142450c0716aSChris Zankel.Lnospill:
1425ea0b6b06SChris Zankel	ret
142650c0716aSChris Zankel
14275a0015d6SChris Zankel.Lloop: _bbsi.l	a3, 1, .Lc4
14285a0015d6SChris Zankel	_bbci.l	a3, 2, .Lc12
14295a0015d6SChris Zankel
14305a0015d6SChris Zankel.Lc8:	s32e	a4, a13, -16
14315a0015d6SChris Zankel	l32e	a4, a5, -12
14325a0015d6SChris Zankel	s32e	a8, a4, -32
14335a0015d6SChris Zankel	s32e	a5, a13, -12
14345a0015d6SChris Zankel	s32e	a6, a13, -8
14355a0015d6SChris Zankel	s32e	a7, a13, -4
14365a0015d6SChris Zankel	s32e	a9, a4, -28
14375a0015d6SChris Zankel	s32e	a10, a4, -24
14385a0015d6SChris Zankel	s32e	a11, a4, -20
14395a0015d6SChris Zankel
14405a0015d6SChris Zankel	srli	a11, a3, 2		# shift windowbase by 2
14415a0015d6SChris Zankel	rotw	2
14425a0015d6SChris Zankel	_bnei	a3, 1, .Lloop
14435a0015d6SChris Zankel
14445a0015d6SChris Zankel.Lexit: /* Done. Do the final rotation, set WS, and return. */
14455a0015d6SChris Zankel
14465a0015d6SChris Zankel	rotw	1
1447bc5378fcSMax Filippov	rsr	a3, windowbase
14485a0015d6SChris Zankel	ssl	a3
14495a0015d6SChris Zankel	movi	a3, 1
14505a0015d6SChris Zankel	sll	a3, a3
1451bc5378fcSMax Filippov	wsr	a3, windowstart
1452ea0b6b06SChris Zankel	ret
14535a0015d6SChris Zankel
14545a0015d6SChris Zankel.Lc4:	s32e	a4, a9, -16
14555a0015d6SChris Zankel	s32e	a5, a9, -12
14565a0015d6SChris Zankel	s32e	a6, a9, -8
14575a0015d6SChris Zankel	s32e	a7, a9, -4
14585a0015d6SChris Zankel
14595a0015d6SChris Zankel	srli	a7, a3, 1
14605a0015d6SChris Zankel	rotw	1
14615a0015d6SChris Zankel	_bnei	a3, 1, .Lloop
14625a0015d6SChris Zankel	j	.Lexit
14635a0015d6SChris Zankel
14645a0015d6SChris Zankel.Lc12:	_bbci.l	a3, 3, .Linvalid_mask	# bit 2 shouldn't be zero!
14655a0015d6SChris Zankel
14665a0015d6SChris Zankel	/* 12-register frame (call12) */
14675a0015d6SChris Zankel
14685a0015d6SChris Zankel	l32e	a2, a5, -12
14695a0015d6SChris Zankel	s32e	a8, a2, -48
14705a0015d6SChris Zankel	mov	a8, a2
14715a0015d6SChris Zankel
14725a0015d6SChris Zankel.Lc12c: s32e	a9, a8, -44
14735a0015d6SChris Zankel	s32e	a10, a8, -40
14745a0015d6SChris Zankel	s32e	a11, a8, -36
14755a0015d6SChris Zankel	s32e	a12, a8, -32
14765a0015d6SChris Zankel	s32e	a13, a8, -28
14775a0015d6SChris Zankel	s32e	a14, a8, -24
14785a0015d6SChris Zankel	s32e	a15, a8, -20
14795a0015d6SChris Zankel	srli	a15, a3, 3
14805a0015d6SChris Zankel
14815a0015d6SChris Zankel	/* The stack pointer for a4..a7 is out of reach, so we rotate the
14825a0015d6SChris Zankel	 * window, grab the stackpointer, and rotate back.
14835a0015d6SChris Zankel	 * Alternatively, we could also use the following approach, but that
14845a0015d6SChris Zankel	 * makes the fixup routine much more complicated:
14855a0015d6SChris Zankel	 * rotw	1
14865a0015d6SChris Zankel	 * s32e	a0, a13, -16
14875a0015d6SChris Zankel	 * ...
14885a0015d6SChris Zankel	 * rotw 2
14895a0015d6SChris Zankel	 */
14905a0015d6SChris Zankel
14915a0015d6SChris Zankel	rotw	1
14925a0015d6SChris Zankel	mov	a5, a13
14935a0015d6SChris Zankel	rotw	-1
14945a0015d6SChris Zankel
14955a0015d6SChris Zankel	s32e	a4, a9, -16
14965a0015d6SChris Zankel	s32e	a5, a9, -12
14975a0015d6SChris Zankel	s32e	a6, a9, -8
14985a0015d6SChris Zankel	s32e	a7, a9, -4
14995a0015d6SChris Zankel
15005a0015d6SChris Zankel	rotw	3
15015a0015d6SChris Zankel
15025a0015d6SChris Zankel	_beqi	a3, 1, .Lexit
15035a0015d6SChris Zankel	j	.Lloop
15045a0015d6SChris Zankel
15055a0015d6SChris Zankel.Linvalid_mask:
15065a0015d6SChris Zankel
15075a0015d6SChris Zankel	/* We get here because of an unrecoverable error in the window
15085a0015d6SChris Zankel	 * registers. If we are in user space, we kill the application,
15095a0015d6SChris Zankel	 * however, this condition is unrecoverable in kernel space.
15105a0015d6SChris Zankel	 */
15115a0015d6SChris Zankel
1512bc5378fcSMax Filippov	rsr	a0, ps
1513173d6681SChris Zankel	_bbci.l	a0, PS_UM_BIT, 1f
15145a0015d6SChris Zankel
15155a0015d6SChris Zankel	/* User space: Setup a dummy frame and kill application.
15165a0015d6SChris Zankel	 * Note: We assume EXC_TABLE_KSTK contains a valid stack pointer.
15175a0015d6SChris Zankel	 */
15185a0015d6SChris Zankel
15195a0015d6SChris Zankel	movi	a0, 1
15205a0015d6SChris Zankel	movi	a1, 0
15215a0015d6SChris Zankel
1522bc5378fcSMax Filippov	wsr	a0, windowstart
1523bc5378fcSMax Filippov	wsr	a1, windowbase
15245a0015d6SChris Zankel	rsync
15255a0015d6SChris Zankel
15265a0015d6SChris Zankel	movi	a0, 0
15275a0015d6SChris Zankel
15285a0015d6SChris Zankel	movi	a3, exc_table
15295a0015d6SChris Zankel	l32i	a1, a3, EXC_TABLE_KSTK
1530bc5378fcSMax Filippov	wsr	a3, excsave1
15315a0015d6SChris Zankel
15322d1c645cSMarc Gauthier	movi	a4, (1 << PS_WOE_BIT) | LOCKLEVEL
1533bc5378fcSMax Filippov	wsr	a4, ps
15345a0015d6SChris Zankel	rsync
15355a0015d6SChris Zankel
15365a0015d6SChris Zankel	movi	a6, SIGSEGV
15375a0015d6SChris Zankel	movi	a4, do_exit
15385a0015d6SChris Zankel	callx4	a4
15395a0015d6SChris Zankel
15405a0015d6SChris Zankel1:	/* Kernel space: PANIC! */
15415a0015d6SChris Zankel
1542bc5378fcSMax Filippov	wsr	a0, excsave1
15435a0015d6SChris Zankel	movi	a0, unrecoverable_exception
15445a0015d6SChris Zankel	callx0	a0		# should not return
15455a0015d6SChris Zankel1:	j	1b
15465a0015d6SChris Zankel
1547d1538c46SChris ZankelENDPROC(_spill_registers)
1548d1538c46SChris Zankel
1549e5083a63SJohannes Weiner#ifdef CONFIG_MMU
15505a0015d6SChris Zankel/*
15515a0015d6SChris Zankel * We should never get here. Bail out!
15525a0015d6SChris Zankel */
15535a0015d6SChris Zankel
15545a0015d6SChris ZankelENTRY(fast_second_level_miss_double_kernel)
15555a0015d6SChris Zankel
15565a0015d6SChris Zankel1:	movi	a0, unrecoverable_exception
15575a0015d6SChris Zankel	callx0	a0		# should not return
15585a0015d6SChris Zankel1:	j	1b
15595a0015d6SChris Zankel
1560d1538c46SChris ZankelENDPROC(fast_second_level_miss_double_kernel)
1561d1538c46SChris Zankel
15625a0015d6SChris Zankel/* First-level entry handler for user, kernel, and double 2nd-level
15635a0015d6SChris Zankel * TLB miss exceptions.  Note that for now, user and kernel miss
15645a0015d6SChris Zankel * exceptions share the same entry point and are handled identically.
15655a0015d6SChris Zankel *
15665a0015d6SChris Zankel * An old, less-efficient C version of this function used to exist.
15675a0015d6SChris Zankel * We include it below, interleaved as comments, for reference.
15685a0015d6SChris Zankel *
15695a0015d6SChris Zankel * Entry condition:
15705a0015d6SChris Zankel *
15715a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
15725a0015d6SChris Zankel *   a1:	a1
15735a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
15745a0015d6SChris Zankel *   a3:	dispatch table
15755a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
15765a0015d6SChris Zankel *   excsave_1:	a3
15775a0015d6SChris Zankel *
15785a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
15795a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
15805a0015d6SChris Zankel */
15815a0015d6SChris Zankel
15825a0015d6SChris ZankelENTRY(fast_second_level_miss)
15835a0015d6SChris Zankel
15845a0015d6SChris Zankel	/* Save a1. Note: we don't expect a double exception. */
15855a0015d6SChris Zankel
15865a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
15875a0015d6SChris Zankel
15885a0015d6SChris Zankel	/* We need to map the page of PTEs for the user task.  Find
15895a0015d6SChris Zankel	 * the pointer to that page.  Also, it's possible for tsk->mm
15905a0015d6SChris Zankel	 * to be NULL while tsk->active_mm is nonzero if we faulted on
15915a0015d6SChris Zankel	 * a vmalloc address.  In that rare case, we must use
15925a0015d6SChris Zankel	 * active_mm instead to avoid a fault in this handler.  See
15935a0015d6SChris Zankel	 *
15945a0015d6SChris Zankel	 * http://mail.nl.linux.org/linux-mm/2002-08/msg00258.html
15955a0015d6SChris Zankel	 *   (or search Internet on "mm vs. active_mm")
15965a0015d6SChris Zankel	 *
15975a0015d6SChris Zankel	 *	if (!mm)
15985a0015d6SChris Zankel	 *		mm = tsk->active_mm;
15995a0015d6SChris Zankel	 *	pgd = pgd_offset (mm, regs->excvaddr);
16005a0015d6SChris Zankel	 *	pmd = pmd_offset (pgd, regs->excvaddr);
16015a0015d6SChris Zankel	 *	pmdval = *pmd;
16025a0015d6SChris Zankel	 */
16035a0015d6SChris Zankel
16045a0015d6SChris Zankel	GET_CURRENT(a1,a2)
16055a0015d6SChris Zankel	l32i	a0, a1, TASK_MM		# tsk->mm
16065a0015d6SChris Zankel	beqz	a0, 9f
16075a0015d6SChris Zankel
160801858d1bSChris Zankel
160901858d1bSChris Zankel	/* We deliberately destroy a3 that holds the exception table. */
161001858d1bSChris Zankel
1611bc5378fcSMax Filippov8:	rsr	a3, excvaddr		# fault address
161201858d1bSChris Zankel	_PGD_OFFSET(a0, a3, a1)
16135a0015d6SChris Zankel	l32i	a0, a0, 0		# read pmdval
16145a0015d6SChris Zankel	beqz	a0, 2f
16155a0015d6SChris Zankel
16165a0015d6SChris Zankel	/* Read ptevaddr and convert to top of page-table page.
16175a0015d6SChris Zankel	 *
16185a0015d6SChris Zankel	 * 	vpnval = read_ptevaddr_register() & PAGE_MASK;
16195a0015d6SChris Zankel	 * 	vpnval += DTLB_WAY_PGTABLE;
16205a0015d6SChris Zankel	 *	pteval = mk_pte (virt_to_page(pmd_val(pmdval)), PAGE_KERNEL);
16215a0015d6SChris Zankel	 *	write_dtlb_entry (pteval, vpnval);
16225a0015d6SChris Zankel	 *
16235a0015d6SChris Zankel	 * The messy computation for 'pteval' above really simplifies
16245a0015d6SChris Zankel	 * into the following:
16255a0015d6SChris Zankel	 *
16266656920bSChris Zankel	 * pteval = ((pmdval - PAGE_OFFSET) & PAGE_MASK) | PAGE_DIRECTORY
16275a0015d6SChris Zankel	 */
16285a0015d6SChris Zankel
162939070cb8SChris Zankel	movi	a1, (-PAGE_OFFSET) & 0xffffffff
16305a0015d6SChris Zankel	add	a0, a0, a1		# pmdval - PAGE_OFFSET
16315a0015d6SChris Zankel	extui	a1, a0, 0, PAGE_SHIFT	# ... & PAGE_MASK
16325a0015d6SChris Zankel	xor	a0, a0, a1
16335a0015d6SChris Zankel
163401858d1bSChris Zankel	movi	a1, _PAGE_DIRECTORY
16355a0015d6SChris Zankel	or	a0, a0, a1		# ... | PAGE_DIRECTORY
16365a0015d6SChris Zankel
163701858d1bSChris Zankel	/*
16386656920bSChris Zankel	 * We utilize all three wired-ways (7-9) to hold pmd translations.
163901858d1bSChris Zankel	 * Memory regions are mapped to the DTLBs according to bits 28 and 29.
164001858d1bSChris Zankel	 * This allows to map the three most common regions to three different
164101858d1bSChris Zankel	 * DTLBs:
164201858d1bSChris Zankel	 *  0,1 -> way 7	program (0040.0000) and virtual (c000.0000)
164301858d1bSChris Zankel	 *  2   -> way 8	shared libaries (2000.0000)
164401858d1bSChris Zankel	 *  3   -> way 0	stack (3000.0000)
164501858d1bSChris Zankel	 */
16465a0015d6SChris Zankel
164701858d1bSChris Zankel	extui	a3, a3, 28, 2		# addr. bit 28 and 29	0,1,2,3
1648bc5378fcSMax Filippov	rsr	a1, ptevaddr
164901858d1bSChris Zankel	addx2	a3, a3, a3		# ->			0,3,6,9
165001858d1bSChris Zankel	srli	a1, a1, PAGE_SHIFT
165101858d1bSChris Zankel	extui	a3, a3, 2, 2		# ->			0,0,1,2
165201858d1bSChris Zankel	slli	a1, a1, PAGE_SHIFT	# ptevaddr & PAGE_MASK
165301858d1bSChris Zankel	addi	a3, a3, DTLB_WAY_PGD
165401858d1bSChris Zankel	add	a1, a1, a3		# ... + way_number
165501858d1bSChris Zankel
165601858d1bSChris Zankel3:	wdtlb	a0, a1
16575a0015d6SChris Zankel	dsync
16585a0015d6SChris Zankel
16595a0015d6SChris Zankel	/* Exit critical section. */
16605a0015d6SChris Zankel
166101858d1bSChris Zankel4:	movi	a3, exc_table		# restore a3
16625a0015d6SChris Zankel	movi	a0, 0
16635a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_FIXUP
16645a0015d6SChris Zankel
16655a0015d6SChris Zankel	/* Restore the working registers, and return. */
16665a0015d6SChris Zankel
16675a0015d6SChris Zankel	l32i	a0, a2, PT_AREG0
16685a0015d6SChris Zankel	l32i	a1, a2, PT_AREG1
16695a0015d6SChris Zankel	l32i	a2, a2, PT_DEPC
1670bc5378fcSMax Filippov	xsr	a3, excsave1
16715a0015d6SChris Zankel
16725a0015d6SChris Zankel	bgeui	a2, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
16735a0015d6SChris Zankel
16745a0015d6SChris Zankel	/* Restore excsave1 and return. */
16755a0015d6SChris Zankel
1676bc5378fcSMax Filippov	rsr	a2, depc
16775a0015d6SChris Zankel	rfe
16785a0015d6SChris Zankel
16795a0015d6SChris Zankel	/* Return from double exception. */
16805a0015d6SChris Zankel
1681bc5378fcSMax Filippov1:	xsr	a2, depc
16825a0015d6SChris Zankel	esync
16835a0015d6SChris Zankel	rfde
16845a0015d6SChris Zankel
16855a0015d6SChris Zankel9:	l32i	a0, a1, TASK_ACTIVE_MM	# unlikely case mm == 0
16865a0015d6SChris Zankel	j	8b
16875a0015d6SChris Zankel
16886656920bSChris Zankel#if (DCACHE_WAY_SIZE > PAGE_SIZE)
16896656920bSChris Zankel
16906656920bSChris Zankel2:	/* Special case for cache aliasing.
16916656920bSChris Zankel	 * We (should) only get here if a clear_user_page, copy_user_page
16926656920bSChris Zankel	 * or the aliased cache flush functions got preemptively interrupted
16936656920bSChris Zankel	 * by another task. Re-establish temporary mapping to the
16946656920bSChris Zankel	 * TLBTEMP_BASE areas.
16956656920bSChris Zankel	 */
16966656920bSChris Zankel
16976656920bSChris Zankel	/* We shouldn't be in a double exception */
16986656920bSChris Zankel
16996656920bSChris Zankel	l32i	a0, a2, PT_DEPC
17006656920bSChris Zankel	bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, 2f
17016656920bSChris Zankel
17026656920bSChris Zankel	/* Make sure the exception originated in the special functions */
17036656920bSChris Zankel
17046656920bSChris Zankel	movi	a0, __tlbtemp_mapping_start
1705bc5378fcSMax Filippov	rsr	a3, epc1
17066656920bSChris Zankel	bltu	a3, a0, 2f
17076656920bSChris Zankel	movi	a0, __tlbtemp_mapping_end
17086656920bSChris Zankel	bgeu	a3, a0, 2f
17096656920bSChris Zankel
17106656920bSChris Zankel	/* Check if excvaddr was in one of the TLBTEMP_BASE areas. */
17116656920bSChris Zankel
17126656920bSChris Zankel	movi	a3, TLBTEMP_BASE_1
1713bc5378fcSMax Filippov	rsr	a0, excvaddr
17146656920bSChris Zankel	bltu	a0, a3, 2f
17156656920bSChris Zankel
17166656920bSChris Zankel	addi	a1, a0, -(2 << (DCACHE_ALIAS_ORDER + PAGE_SHIFT))
17176656920bSChris Zankel	bgeu	a1, a3, 2f
17186656920bSChris Zankel
17196656920bSChris Zankel	/* Check if we have to restore an ITLB mapping. */
17206656920bSChris Zankel
17216656920bSChris Zankel	movi	a1, __tlbtemp_mapping_itlb
1722bc5378fcSMax Filippov	rsr	a3, epc1
17236656920bSChris Zankel	sub	a3, a3, a1
17246656920bSChris Zankel
17256656920bSChris Zankel	/* Calculate VPN */
17266656920bSChris Zankel
17276656920bSChris Zankel	movi	a1, PAGE_MASK
17286656920bSChris Zankel	and	a1, a1, a0
17296656920bSChris Zankel
17306656920bSChris Zankel	/* Jump for ITLB entry */
17316656920bSChris Zankel
17326656920bSChris Zankel	bgez	a3, 1f
17336656920bSChris Zankel
17346656920bSChris Zankel	/* We can use up to two TLBTEMP areas, one for src and one for dst. */
17356656920bSChris Zankel
17366656920bSChris Zankel	extui	a3, a0, PAGE_SHIFT + DCACHE_ALIAS_ORDER, 1
17376656920bSChris Zankel	add	a1, a3, a1
17386656920bSChris Zankel
17396656920bSChris Zankel	/* PPN is in a6 for the first TLBTEMP area and in a7 for the second. */
17406656920bSChris Zankel
17416656920bSChris Zankel	mov	a0, a6
17426656920bSChris Zankel	movnez	a0, a7, a3
17436656920bSChris Zankel	j	3b
17446656920bSChris Zankel
17456656920bSChris Zankel	/* ITLB entry. We only use dst in a6. */
17466656920bSChris Zankel
17476656920bSChris Zankel1:	witlb	a6, a1
17486656920bSChris Zankel	isync
17496656920bSChris Zankel	j	4b
17506656920bSChris Zankel
17516656920bSChris Zankel
17526656920bSChris Zankel#endif	// DCACHE_WAY_SIZE > PAGE_SIZE
17536656920bSChris Zankel
17546656920bSChris Zankel
17555a0015d6SChris Zankel2:	/* Invalid PGD, default exception handling */
17565a0015d6SChris Zankel
175701858d1bSChris Zankel	movi	a3, exc_table
1758bc5378fcSMax Filippov	rsr	a1, depc
1759bc5378fcSMax Filippov	xsr	a3, excsave1
17605a0015d6SChris Zankel	s32i	a1, a2, PT_AREG2
17615a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
17625a0015d6SChris Zankel	mov	a1, a2
17635a0015d6SChris Zankel
1764bc5378fcSMax Filippov	rsr	a2, ps
1765173d6681SChris Zankel	bbsi.l	a2, PS_UM_BIT, 1f
17665a0015d6SChris Zankel	j	_kernel_exception
17675a0015d6SChris Zankel1:	j	_user_exception
17685a0015d6SChris Zankel
1769d1538c46SChris ZankelENDPROC(fast_second_level_miss)
17705a0015d6SChris Zankel
17715a0015d6SChris Zankel/*
17725a0015d6SChris Zankel * StoreProhibitedException
17735a0015d6SChris Zankel *
17745a0015d6SChris Zankel * Update the pte and invalidate the itlb mapping for this pte.
17755a0015d6SChris Zankel *
17765a0015d6SChris Zankel * Entry condition:
17775a0015d6SChris Zankel *
17785a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
17795a0015d6SChris Zankel *   a1:	a1
17805a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
17815a0015d6SChris Zankel *   a3:	dispatch table
17825a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
17835a0015d6SChris Zankel *   excsave_1:	a3
17845a0015d6SChris Zankel *
17855a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
17865a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
17875a0015d6SChris Zankel */
17885a0015d6SChris Zankel
17895a0015d6SChris ZankelENTRY(fast_store_prohibited)
17905a0015d6SChris Zankel
17915a0015d6SChris Zankel	/* Save a1 and a4. */
17925a0015d6SChris Zankel
17935a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
17945a0015d6SChris Zankel	s32i	a4, a2, PT_AREG4
17955a0015d6SChris Zankel
17965a0015d6SChris Zankel	GET_CURRENT(a1,a2)
17975a0015d6SChris Zankel	l32i	a0, a1, TASK_MM		# tsk->mm
17985a0015d6SChris Zankel	beqz	a0, 9f
17995a0015d6SChris Zankel
1800bc5378fcSMax Filippov8:	rsr	a1, excvaddr		# fault address
18015a0015d6SChris Zankel	_PGD_OFFSET(a0, a1, a4)
18025a0015d6SChris Zankel	l32i	a0, a0, 0
18035a0015d6SChris Zankel	beqz	a0, 2f
18045a0015d6SChris Zankel
180551fc41a9SMax Filippov	/*
180651fc41a9SMax Filippov	 * Note that we test _PAGE_WRITABLE_BIT only if PTE is present
180751fc41a9SMax Filippov	 * and is not PAGE_NONE. See pgtable.h for possible PTE layouts.
180851fc41a9SMax Filippov	 */
180901858d1bSChris Zankel
18105a0015d6SChris Zankel	_PTE_OFFSET(a0, a1, a4)
18115a0015d6SChris Zankel	l32i	a4, a0, 0		# read pteval
181251fc41a9SMax Filippov	movi	a1, _PAGE_CA_INVALID
181351fc41a9SMax Filippov	ball	a4, a1, 2f
181401858d1bSChris Zankel	bbci.l	a4, _PAGE_WRITABLE_BIT, 2f
18155a0015d6SChris Zankel
181601858d1bSChris Zankel	movi	a1, _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_HW_WRITE
18175a0015d6SChris Zankel	or	a4, a4, a1
1818bc5378fcSMax Filippov	rsr	a1, excvaddr
18195a0015d6SChris Zankel	s32i	a4, a0, 0
18205a0015d6SChris Zankel
18215a0015d6SChris Zankel	/* We need to flush the cache if we have page coloring. */
18225a0015d6SChris Zankel#if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
18235a0015d6SChris Zankel	dhwb	a0, 0
18245a0015d6SChris Zankel#endif
18255a0015d6SChris Zankel	pdtlb	a0, a1
18265a0015d6SChris Zankel	wdtlb	a4, a0
18275a0015d6SChris Zankel
18285a0015d6SChris Zankel	/* Exit critical section. */
18295a0015d6SChris Zankel
18305a0015d6SChris Zankel	movi	a0, 0
18315a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_FIXUP
18325a0015d6SChris Zankel
18335a0015d6SChris Zankel	/* Restore the working registers, and return. */
18345a0015d6SChris Zankel
18355a0015d6SChris Zankel	l32i	a4, a2, PT_AREG4
18365a0015d6SChris Zankel	l32i	a1, a2, PT_AREG1
18375a0015d6SChris Zankel	l32i	a0, a2, PT_AREG0
18385a0015d6SChris Zankel	l32i	a2, a2, PT_DEPC
18395a0015d6SChris Zankel
18405a0015d6SChris Zankel	/* Restore excsave1 and a3. */
18415a0015d6SChris Zankel
1842bc5378fcSMax Filippov	xsr	a3, excsave1
18435a0015d6SChris Zankel	bgeui	a2, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
18445a0015d6SChris Zankel
1845bc5378fcSMax Filippov	rsr	a2, depc
18465a0015d6SChris Zankel	rfe
18475a0015d6SChris Zankel
18485a0015d6SChris Zankel	/* Double exception. Restore FIXUP handler and return. */
18495a0015d6SChris Zankel
1850bc5378fcSMax Filippov1:	xsr	a2, depc
18515a0015d6SChris Zankel	esync
18525a0015d6SChris Zankel	rfde
18535a0015d6SChris Zankel
18545a0015d6SChris Zankel9:	l32i	a0, a1, TASK_ACTIVE_MM	# unlikely case mm == 0
18555a0015d6SChris Zankel	j	8b
18565a0015d6SChris Zankel
18575a0015d6SChris Zankel2:	/* If there was a problem, handle fault in C */
18585a0015d6SChris Zankel
1859bc5378fcSMax Filippov	rsr	a4, depc	# still holds a2
1860bc5378fcSMax Filippov	xsr	a3, excsave1
18615a0015d6SChris Zankel	s32i	a4, a2, PT_AREG2
18625a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
18635a0015d6SChris Zankel	l32i	a4, a2, PT_AREG4
18645a0015d6SChris Zankel	mov	a1, a2
18655a0015d6SChris Zankel
1866bc5378fcSMax Filippov	rsr	a2, ps
1867173d6681SChris Zankel	bbsi.l	a2, PS_UM_BIT, 1f
18685a0015d6SChris Zankel	j	_kernel_exception
18695a0015d6SChris Zankel1:	j	_user_exception
1870d1538c46SChris Zankel
1871d1538c46SChris ZankelENDPROC(fast_store_prohibited)
1872d1538c46SChris Zankel
1873e5083a63SJohannes Weiner#endif /* CONFIG_MMU */
18745a0015d6SChris Zankel
18755a0015d6SChris Zankel/*
1876fc4fb2adSChris Zankel * System Calls.
1877fc4fb2adSChris Zankel *
1878fc4fb2adSChris Zankel * void system_call (struct pt_regs* regs, int exccause)
1879fc4fb2adSChris Zankel *                            a2                 a3
1880fc4fb2adSChris Zankel */
1881fc4fb2adSChris Zankel
1882fc4fb2adSChris ZankelENTRY(system_call)
1883d1538c46SChris Zankel
1884fc4fb2adSChris Zankel	entry	a1, 32
1885fc4fb2adSChris Zankel
1886fc4fb2adSChris Zankel	/* regs->syscall = regs->areg[2] */
1887fc4fb2adSChris Zankel
1888fc4fb2adSChris Zankel	l32i	a3, a2, PT_AREG2
1889fc4fb2adSChris Zankel	mov	a6, a2
1890fc4fb2adSChris Zankel	movi	a4, do_syscall_trace_enter
1891fc4fb2adSChris Zankel	s32i	a3, a2, PT_SYSCALL
1892fc4fb2adSChris Zankel	callx4	a4
1893fc4fb2adSChris Zankel
1894fc4fb2adSChris Zankel	/* syscall = sys_call_table[syscall_nr] */
1895fc4fb2adSChris Zankel
1896fc4fb2adSChris Zankel	movi	a4, sys_call_table;
1897fc4fb2adSChris Zankel	movi	a5, __NR_syscall_count
1898fc4fb2adSChris Zankel	movi	a6, -ENOSYS
1899fc4fb2adSChris Zankel	bgeu	a3, a5, 1f
1900fc4fb2adSChris Zankel
1901fc4fb2adSChris Zankel	addx4	a4, a3, a4
1902fc4fb2adSChris Zankel	l32i	a4, a4, 0
1903fc4fb2adSChris Zankel	movi	a5, sys_ni_syscall;
1904fc4fb2adSChris Zankel	beq	a4, a5, 1f
1905fc4fb2adSChris Zankel
1906fc4fb2adSChris Zankel	/* Load args: arg0 - arg5 are passed via regs. */
1907fc4fb2adSChris Zankel
1908fc4fb2adSChris Zankel	l32i	a6, a2, PT_AREG6
1909fc4fb2adSChris Zankel	l32i	a7, a2, PT_AREG3
1910fc4fb2adSChris Zankel	l32i	a8, a2, PT_AREG4
1911fc4fb2adSChris Zankel	l32i	a9, a2, PT_AREG5
1912fc4fb2adSChris Zankel	l32i	a10, a2, PT_AREG8
1913fc4fb2adSChris Zankel	l32i	a11, a2, PT_AREG9
1914fc4fb2adSChris Zankel
1915fc4fb2adSChris Zankel	/* Pass one additional argument to the syscall: pt_regs (on stack) */
1916fc4fb2adSChris Zankel	s32i	a2, a1, 0
1917fc4fb2adSChris Zankel
1918fc4fb2adSChris Zankel	callx4	a4
1919fc4fb2adSChris Zankel
1920fc4fb2adSChris Zankel1:	/* regs->areg[2] = return_value */
1921fc4fb2adSChris Zankel
1922fc4fb2adSChris Zankel	s32i	a6, a2, PT_AREG2
1923fc4fb2adSChris Zankel	movi	a4, do_syscall_trace_leave
1924fc4fb2adSChris Zankel	mov	a6, a2
1925fc4fb2adSChris Zankel	callx4	a4
1926fc4fb2adSChris Zankel	retw
1927fc4fb2adSChris Zankel
1928d1538c46SChris ZankelENDPROC(system_call)
1929d1538c46SChris Zankel
1930fc4fb2adSChris Zankel
1931fc4fb2adSChris Zankel/*
19325a0015d6SChris Zankel * Task switch.
19335a0015d6SChris Zankel *
19345a0015d6SChris Zankel * struct task*  _switch_to (struct task* prev, struct task* next)
19355a0015d6SChris Zankel *         a2                              a2                 a3
19365a0015d6SChris Zankel */
19375a0015d6SChris Zankel
19385a0015d6SChris ZankelENTRY(_switch_to)
19395a0015d6SChris Zankel
19405a0015d6SChris Zankel	entry	a1, 16
19415a0015d6SChris Zankel
1942c658eac6SChris Zankel	mov	a12, a2			# preserve 'prev' (a2)
1943c658eac6SChris Zankel	mov	a13, a3			# and 'next' (a3)
19445a0015d6SChris Zankel
1945c658eac6SChris Zankel	l32i	a4, a2, TASK_THREAD_INFO
1946c658eac6SChris Zankel	l32i	a5, a3, TASK_THREAD_INFO
19475a0015d6SChris Zankel
1948c658eac6SChris Zankel	save_xtregs_user a4 a6 a8 a9 a10 a11 THREAD_XTREGS_USER
19495a0015d6SChris Zankel
1950c658eac6SChris Zankel	s32i	a0, a12, THREAD_RA	# save return address
1951c658eac6SChris Zankel	s32i	a1, a12, THREAD_SP	# save stack pointer
1952c658eac6SChris Zankel
1953c658eac6SChris Zankel	/* Disable ints while we manipulate the stack pointer. */
1954c658eac6SChris Zankel
1955c658eac6SChris Zankel	movi	a14, (1 << PS_EXCM_BIT) | LOCKLEVEL
1956bc5378fcSMax Filippov	xsr	a14, ps
1957bc5378fcSMax Filippov	rsr	a3, excsave1
19585a0015d6SChris Zankel	rsync
19595a0015d6SChris Zankel	s32i	a3, a3, EXC_TABLE_FIXUP	/* enter critical section */
19605a0015d6SChris Zankel
1961c658eac6SChris Zankel	/* Switch CPENABLE */
1962c658eac6SChris Zankel
1963c658eac6SChris Zankel#if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
1964c658eac6SChris Zankel	l32i	a3, a5, THREAD_CPENABLE
1965bc5378fcSMax Filippov	xsr	a3, cpenable
1966c658eac6SChris Zankel	s32i	a3, a4, THREAD_CPENABLE
1967c658eac6SChris Zankel#endif
1968c658eac6SChris Zankel
1969c658eac6SChris Zankel	/* Flush register file. */
1970c658eac6SChris Zankel
1971c658eac6SChris Zankel	call0	_spill_registers	# destroys a3, a4, and SAR
19725a0015d6SChris Zankel
19735a0015d6SChris Zankel	/* Set kernel stack (and leave critical section)
19745a0015d6SChris Zankel	 * Note: It's save to set it here. The stack will not be overwritten
19755a0015d6SChris Zankel	 *       because the kernel stack will only be loaded again after
19765a0015d6SChris Zankel	 *       we return from kernel space.
19775a0015d6SChris Zankel	 */
19785a0015d6SChris Zankel
1979bc5378fcSMax Filippov	rsr	a3, excsave1		# exc_table
1980c658eac6SChris Zankel	movi	a6, 0
1981c658eac6SChris Zankel	addi	a7, a5, PT_REGS_OFFSET
1982c658eac6SChris Zankel	s32i	a6, a3, EXC_TABLE_FIXUP
1983c658eac6SChris Zankel	s32i	a7, a3, EXC_TABLE_KSTK
19845a0015d6SChris Zankel
1985c50842dfSChris Zankel	/* restore context of the task 'next' */
19865a0015d6SChris Zankel
1987c658eac6SChris Zankel	l32i	a0, a13, THREAD_RA	# restore return address
1988c658eac6SChris Zankel	l32i	a1, a13, THREAD_SP	# restore stack pointer
19895a0015d6SChris Zankel
1990c658eac6SChris Zankel	load_xtregs_user a5 a6 a8 a9 a10 a11 THREAD_XTREGS_USER
1991c658eac6SChris Zankel
1992bc5378fcSMax Filippov	wsr	a14, ps
1993c658eac6SChris Zankel	mov	a2, a12			# return 'prev'
19945a0015d6SChris Zankel	rsync
19955a0015d6SChris Zankel
19965a0015d6SChris Zankel	retw
19975a0015d6SChris Zankel
1998d1538c46SChris ZankelENDPROC(_switch_to)
19995a0015d6SChris Zankel
20005a0015d6SChris ZankelENTRY(ret_from_fork)
20015a0015d6SChris Zankel
20025a0015d6SChris Zankel	/* void schedule_tail (struct task_struct *prev)
20035a0015d6SChris Zankel	 * Note: prev is still in a6 (return value from fake call4 frame)
20045a0015d6SChris Zankel	 */
20055a0015d6SChris Zankel	movi	a4, schedule_tail
20065a0015d6SChris Zankel	callx4	a4
20075a0015d6SChris Zankel
2008fc4fb2adSChris Zankel	movi	a4, do_syscall_trace_leave
2009fc4fb2adSChris Zankel	mov	a6, a1
20105a0015d6SChris Zankel	callx4	a4
20115a0015d6SChris Zankel
20125a0015d6SChris Zankel	j	common_exception_return
20135a0015d6SChris Zankel
2014d1538c46SChris ZankelENDPROC(ret_from_fork)
2015d1538c46SChris Zankel
20163306a726SMax Filippov/*
20173306a726SMax Filippov * Kernel thread creation helper
20183306a726SMax Filippov * On entry, set up by copy_thread: a2 = thread_fn, a3 = thread_fn arg
20193306a726SMax Filippov *           left from _switch_to: a6 = prev
20203306a726SMax Filippov */
20213306a726SMax FilippovENTRY(ret_from_kernel_thread)
20223306a726SMax Filippov
20233306a726SMax Filippov	call4	schedule_tail
20243306a726SMax Filippov	mov	a6, a3
20253306a726SMax Filippov	callx4	a2
2026f0a1bf08SMax Filippov	j	common_exception_return
20273306a726SMax Filippov
20283306a726SMax FilippovENDPROC(ret_from_kernel_thread)
2029