xref: /linux-6.15/arch/xtensa/kernel/entry.S (revision fc4fb2ad)
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 *
105a0015d6SChris Zankel * Copyright (C) 2004-2005 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>
195a0015d6SChris Zankel#include <asm/thread_info.h>
205a0015d6SChris Zankel#include <asm/uaccess.h>
215a0015d6SChris Zankel#include <asm/unistd.h>
225a0015d6SChris Zankel#include <asm/ptrace.h>
235a0015d6SChris Zankel#include <asm/current.h>
245a0015d6SChris Zankel#include <asm/pgtable.h>
255a0015d6SChris Zankel#include <asm/page.h>
265a0015d6SChris Zankel#include <asm/signal.h>
27173d6681SChris Zankel#include <asm/tlbflush.h>
285a0015d6SChris Zankel
295a0015d6SChris Zankel/* Unimplemented features. */
305a0015d6SChris Zankel
315a0015d6SChris Zankel#undef SIGNAL_HANDLING_IN_DOUBLE_EXCEPTION
325a0015d6SChris Zankel#undef KERNEL_STACK_OVERFLOW_CHECK
335a0015d6SChris Zankel#undef PREEMPTIBLE_KERNEL
345a0015d6SChris Zankel#undef ALLOCA_EXCEPTION_IN_IRAM
355a0015d6SChris Zankel
365a0015d6SChris Zankel/* Not well tested.
375a0015d6SChris Zankel *
385a0015d6SChris Zankel * - fast_coprocessor
395a0015d6SChris Zankel */
405a0015d6SChris Zankel
415a0015d6SChris Zankel/*
425a0015d6SChris Zankel * Macro to find first bit set in WINDOWBASE from the left + 1
435a0015d6SChris Zankel *
445a0015d6SChris Zankel * 100....0 -> 1
455a0015d6SChris Zankel * 010....0 -> 2
465a0015d6SChris Zankel * 000....1 -> WSBITS
475a0015d6SChris Zankel */
485a0015d6SChris Zankel
495a0015d6SChris Zankel	.macro ffs_ws bit mask
505a0015d6SChris Zankel
515a0015d6SChris Zankel#if XCHAL_HAVE_NSA
525a0015d6SChris Zankel	nsau    \bit, \mask			# 32-WSBITS ... 31 (32 iff 0)
535a0015d6SChris Zankel	addi    \bit, \bit, WSBITS - 32 + 1   	# uppest bit set -> return 1
545a0015d6SChris Zankel#else
555a0015d6SChris Zankel	movi    \bit, WSBITS
565a0015d6SChris Zankel#if WSBITS > 16
575a0015d6SChris Zankel	_bltui  \mask, 0x10000, 99f
585a0015d6SChris Zankel	addi    \bit, \bit, -16
595a0015d6SChris Zankel	extui   \mask, \mask, 16, 16
605a0015d6SChris Zankel#endif
615a0015d6SChris Zankel#if WSBITS > 8
625a0015d6SChris Zankel99:	_bltui  \mask, 0x100, 99f
635a0015d6SChris Zankel	addi    \bit, \bit, -8
645a0015d6SChris Zankel	srli    \mask, \mask, 8
655a0015d6SChris Zankel#endif
665a0015d6SChris Zankel99:	_bltui  \mask, 0x10, 99f
675a0015d6SChris Zankel	addi    \bit, \bit, -4
685a0015d6SChris Zankel	srli    \mask, \mask, 4
695a0015d6SChris Zankel99:	_bltui  \mask, 0x4, 99f
705a0015d6SChris Zankel	addi    \bit, \bit, -2
715a0015d6SChris Zankel	srli    \mask, \mask, 2
725a0015d6SChris Zankel99:	_bltui  \mask, 0x2, 99f
735a0015d6SChris Zankel	addi    \bit, \bit, -1
745a0015d6SChris Zankel99:
755a0015d6SChris Zankel
765a0015d6SChris Zankel#endif
775a0015d6SChris Zankel	.endm
785a0015d6SChris Zankel
795a0015d6SChris Zankel/* ----------------- DEFAULT FIRST LEVEL EXCEPTION HANDLERS ----------------- */
805a0015d6SChris Zankel
815a0015d6SChris Zankel/*
825a0015d6SChris Zankel * First-level exception handler for user exceptions.
835a0015d6SChris Zankel * Save some special registers, extra states and all registers in the AR
845a0015d6SChris Zankel * register file that were in use in the user task, and jump to the common
855a0015d6SChris Zankel * exception code.
865a0015d6SChris Zankel * We save SAR (used to calculate WMASK), and WB and WS (we don't have to
875a0015d6SChris Zankel * save them for kernel exceptions).
885a0015d6SChris Zankel *
895a0015d6SChris Zankel * Entry condition for user_exception:
905a0015d6SChris Zankel *
915a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
925a0015d6SChris Zankel *   a1:	a1
935a0015d6SChris Zankel *   a2:	new stack pointer, original value in depc
945a0015d6SChris Zankel *   a3:	dispatch table
955a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
965a0015d6SChris Zankel *   excsave1:	a3
975a0015d6SChris Zankel *
985a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
995a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
1005a0015d6SChris Zankel *
1015a0015d6SChris Zankel * Entry condition for _user_exception:
1025a0015d6SChris Zankel *
1035a0015d6SChris Zankel *   a0-a3 and depc have been saved to PT_AREG0...PT_AREG3 and PT_DEPC
1045a0015d6SChris Zankel *   excsave has been restored, and
1055a0015d6SChris Zankel *   stack pointer (a1) has been set.
1065a0015d6SChris Zankel *
1075a0015d6SChris Zankel * Note: _user_exception might be at an odd adress. Don't use call0..call12
1085a0015d6SChris Zankel */
1095a0015d6SChris Zankel
1105a0015d6SChris ZankelENTRY(user_exception)
1115a0015d6SChris Zankel
1125a0015d6SChris Zankel	/* Save a2, a3, and depc, restore excsave_1 and set SP. */
1135a0015d6SChris Zankel
1145a0015d6SChris Zankel	xsr	a3, EXCSAVE_1
1155a0015d6SChris Zankel	rsr	a0, DEPC
1165a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
1175a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2
1185a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
1195a0015d6SChris Zankel	mov	a1, a2
1205a0015d6SChris Zankel
1215a0015d6SChris Zankel	.globl _user_exception
1225a0015d6SChris Zankel_user_exception:
1235a0015d6SChris Zankel
1245a0015d6SChris Zankel	/* Save SAR and turn off single stepping */
1255a0015d6SChris Zankel
1265a0015d6SChris Zankel	movi	a2, 0
1275a0015d6SChris Zankel	rsr	a3, SAR
1285a0015d6SChris Zankel	wsr	a2, ICOUNTLEVEL
1295a0015d6SChris Zankel	s32i	a3, a1, PT_SAR
1305a0015d6SChris Zankel
1315a0015d6SChris Zankel	/* Rotate ws so that the current windowbase is at bit0. */
1325a0015d6SChris Zankel	/* Assume ws = xxwww1yyyy. Rotate ws right, so that a2 = yyyyxxwww1 */
1335a0015d6SChris Zankel
1345a0015d6SChris Zankel	rsr	a2, WINDOWBASE
1355a0015d6SChris Zankel	rsr	a3, WINDOWSTART
1365a0015d6SChris Zankel	ssr	a2
1375a0015d6SChris Zankel	s32i	a2, a1, PT_WINDOWBASE
1385a0015d6SChris Zankel	s32i	a3, a1, PT_WINDOWSTART
1395a0015d6SChris Zankel	slli	a2, a3, 32-WSBITS
1405a0015d6SChris Zankel	src	a2, a3, a2
1415a0015d6SChris Zankel	srli	a2, a2, 32-WSBITS
1425a0015d6SChris Zankel	s32i	a2, a1, PT_WMASK	# needed for restoring registers
1435a0015d6SChris Zankel
1445a0015d6SChris Zankel	/* Save only live registers. */
1455a0015d6SChris Zankel
1465a0015d6SChris Zankel	_bbsi.l	a2, 1, 1f
1475a0015d6SChris Zankel	s32i	a4, a1, PT_AREG4
1485a0015d6SChris Zankel	s32i	a5, a1, PT_AREG5
1495a0015d6SChris Zankel	s32i	a6, a1, PT_AREG6
1505a0015d6SChris Zankel	s32i	a7, a1, PT_AREG7
1515a0015d6SChris Zankel	_bbsi.l	a2, 2, 1f
1525a0015d6SChris Zankel	s32i	a8, a1, PT_AREG8
1535a0015d6SChris Zankel	s32i	a9, a1, PT_AREG9
1545a0015d6SChris Zankel	s32i	a10, a1, PT_AREG10
1555a0015d6SChris Zankel	s32i	a11, a1, PT_AREG11
1565a0015d6SChris Zankel	_bbsi.l	a2, 3, 1f
1575a0015d6SChris Zankel	s32i	a12, a1, PT_AREG12
1585a0015d6SChris Zankel	s32i	a13, a1, PT_AREG13
1595a0015d6SChris Zankel	s32i	a14, a1, PT_AREG14
1605a0015d6SChris Zankel	s32i	a15, a1, PT_AREG15
1615a0015d6SChris Zankel	_bnei	a2, 1, 1f		# only one valid frame?
1625a0015d6SChris Zankel
1635a0015d6SChris Zankel	/* Only one valid frame, skip saving regs. */
1645a0015d6SChris Zankel
1655a0015d6SChris Zankel	j	2f
1665a0015d6SChris Zankel
1675a0015d6SChris Zankel	/* Save the remaining registers.
1685a0015d6SChris Zankel	 * We have to save all registers up to the first '1' from
1695a0015d6SChris Zankel	 * the right, except the current frame (bit 0).
1705a0015d6SChris Zankel	 * Assume a2 is:  001001000110001
1715a0015d6SChris Zankel	 * All regiser frames starting from the top fiel to the marked '1'
1725a0015d6SChris Zankel	 * must be saved.
1735a0015d6SChris Zankel	 */
1745a0015d6SChris Zankel
1755a0015d6SChris Zankel1:	addi	a3, a2, -1		# eliminate '1' in bit 0: yyyyxxww0
1765a0015d6SChris Zankel	neg	a3, a3			# yyyyxxww0 -> YYYYXXWW1+1
1775a0015d6SChris Zankel	and	a3, a3, a2		# max. only one bit is set
1785a0015d6SChris Zankel
1795a0015d6SChris Zankel	/* Find number of frames to save */
1805a0015d6SChris Zankel
1815a0015d6SChris Zankel	ffs_ws	a0, a3			# number of frames to the '1' from left
1825a0015d6SChris Zankel
1835a0015d6SChris Zankel	/* Store information into WMASK:
1845a0015d6SChris Zankel	 * bits 0..3: xxx1 masked lower 4 bits of the rotated windowstart,
1855a0015d6SChris Zankel	 * bits 4...: number of valid 4-register frames
1865a0015d6SChris Zankel	 */
1875a0015d6SChris Zankel
1885a0015d6SChris Zankel	slli	a3, a0, 4		# number of frames to save in bits 8..4
1895a0015d6SChris Zankel	extui	a2, a2, 0, 4		# mask for the first 16 registers
1905a0015d6SChris Zankel	or	a2, a3, a2
1915a0015d6SChris Zankel	s32i	a2, a1, PT_WMASK	# needed when we restore the reg-file
1925a0015d6SChris Zankel
1935a0015d6SChris Zankel	/* Save 4 registers at a time */
1945a0015d6SChris Zankel
1955a0015d6SChris Zankel1:	rotw	-1
1965a0015d6SChris Zankel	s32i	a0, a5, PT_AREG_END - 16
1975a0015d6SChris Zankel	s32i	a1, a5, PT_AREG_END - 12
1985a0015d6SChris Zankel	s32i	a2, a5, PT_AREG_END - 8
1995a0015d6SChris Zankel	s32i	a3, a5, PT_AREG_END - 4
2005a0015d6SChris Zankel	addi	a0, a4, -1
2015a0015d6SChris Zankel	addi	a1, a5, -16
2025a0015d6SChris Zankel	_bnez	a0, 1b
2035a0015d6SChris Zankel
2045a0015d6SChris Zankel	/* WINDOWBASE still in SAR! */
2055a0015d6SChris Zankel
2065a0015d6SChris Zankel	rsr	a2, SAR			# original WINDOWBASE
2075a0015d6SChris Zankel	movi	a3, 1
2085a0015d6SChris Zankel	ssl	a2
2095a0015d6SChris Zankel	sll	a3, a3
2105a0015d6SChris Zankel	wsr	a3, WINDOWSTART		# set corresponding WINDOWSTART bit
2115a0015d6SChris Zankel	wsr	a2, WINDOWBASE		# and WINDOWSTART
2125a0015d6SChris Zankel	rsync
2135a0015d6SChris Zankel
2145a0015d6SChris Zankel	/* We are back to the original stack pointer (a1) */
2155a0015d6SChris Zankel
2165a0015d6SChris Zankel2:
2175a0015d6SChris Zankel#if XCHAL_EXTRA_SA_SIZE
2185a0015d6SChris Zankel
2195a0015d6SChris Zankel	/* For user exceptions, save the extra state into the user's TCB.
2205a0015d6SChris Zankel	 * Note: We must assume that xchal_extra_store_funcbody destroys a2..a15
2215a0015d6SChris Zankel	 */
2225a0015d6SChris Zankel
2235a0015d6SChris Zankel	GET_CURRENT(a2,a1)
2245a0015d6SChris Zankel	addi	a2, a2, THREAD_CP_SAVE
2255a0015d6SChris Zankel	xchal_extra_store_funcbody
2265a0015d6SChris Zankel#endif
2275a0015d6SChris Zankel
2285a0015d6SChris Zankel	/* Now, jump to the common exception handler. */
2295a0015d6SChris Zankel
2305a0015d6SChris Zankel	j	common_exception
2315a0015d6SChris Zankel
2325a0015d6SChris Zankel
2335a0015d6SChris Zankel/*
2345a0015d6SChris Zankel * First-level exit handler for kernel exceptions
2355a0015d6SChris Zankel * Save special registers and the live window frame.
2365a0015d6SChris Zankel * Note: Even though we changes the stack pointer, we don't have to do a
2375a0015d6SChris Zankel *	 MOVSP here, as we do that when we return from the exception.
2385a0015d6SChris Zankel *	 (See comment in the kernel exception exit code)
2395a0015d6SChris Zankel *
2405a0015d6SChris Zankel * Entry condition for kernel_exception:
2415a0015d6SChris Zankel *
2425a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
2435a0015d6SChris Zankel *   a1:	a1
2445a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
2455a0015d6SChris Zankel *   a3:	dispatch table
2465a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
2475a0015d6SChris Zankel *   excsave_1:	a3
2485a0015d6SChris Zankel *
2495a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
2505a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
2515a0015d6SChris Zankel *
2525a0015d6SChris Zankel * Entry condition for _kernel_exception:
2535a0015d6SChris Zankel *
2545a0015d6SChris Zankel *   a0-a3 and depc have been saved to PT_AREG0...PT_AREG3 and PT_DEPC
2555a0015d6SChris Zankel *   excsave has been restored, and
2565a0015d6SChris Zankel *   stack pointer (a1) has been set.
2575a0015d6SChris Zankel *
2585a0015d6SChris Zankel * Note: _kernel_exception might be at an odd adress. Don't use call0..call12
2595a0015d6SChris Zankel */
2605a0015d6SChris Zankel
2615a0015d6SChris ZankelENTRY(kernel_exception)
2625a0015d6SChris Zankel
2635a0015d6SChris Zankel	/* Save a0, a2, a3, DEPC and set SP. */
2645a0015d6SChris Zankel
2655a0015d6SChris Zankel	xsr	a3, EXCSAVE_1		# restore a3, excsave_1
2665a0015d6SChris Zankel	rsr	a0, DEPC		# get a2
2675a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
2685a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2
2695a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
2705a0015d6SChris Zankel	mov	a1, a2
2715a0015d6SChris Zankel
2725a0015d6SChris Zankel	.globl _kernel_exception
2735a0015d6SChris Zankel_kernel_exception:
2745a0015d6SChris Zankel
2755a0015d6SChris Zankel	/* Save SAR and turn off single stepping */
2765a0015d6SChris Zankel
2775a0015d6SChris Zankel	movi	a2, 0
2785a0015d6SChris Zankel	rsr	a3, SAR
2795a0015d6SChris Zankel	wsr	a2, ICOUNTLEVEL
2805a0015d6SChris Zankel	s32i	a3, a1, PT_SAR
2815a0015d6SChris Zankel
2825a0015d6SChris Zankel	/* Rotate ws so that the current windowbase is at bit0. */
2835a0015d6SChris Zankel	/* Assume ws = xxwww1yyyy. Rotate ws right, so that a2 = yyyyxxwww1 */
2845a0015d6SChris Zankel
2855a0015d6SChris Zankel	rsr	a2, WINDOWBASE		# don't need to save these, we only
2865a0015d6SChris Zankel	rsr	a3, WINDOWSTART		# need shifted windowstart: windowmask
2875a0015d6SChris Zankel	ssr	a2
2885a0015d6SChris Zankel	slli	a2, a3, 32-WSBITS
2895a0015d6SChris Zankel	src	a2, a3, a2
2905a0015d6SChris Zankel	srli	a2, a2, 32-WSBITS
2915a0015d6SChris Zankel	s32i	a2, a1, PT_WMASK	# needed for kernel_exception_exit
2925a0015d6SChris Zankel
2935a0015d6SChris Zankel	/* Save only the live window-frame */
2945a0015d6SChris Zankel
2955a0015d6SChris Zankel	_bbsi.l	a2, 1, 1f
2965a0015d6SChris Zankel	s32i	a4, a1, PT_AREG4
2975a0015d6SChris Zankel	s32i	a5, a1, PT_AREG5
2985a0015d6SChris Zankel	s32i	a6, a1, PT_AREG6
2995a0015d6SChris Zankel	s32i	a7, a1, PT_AREG7
3005a0015d6SChris Zankel	_bbsi.l	a2, 2, 1f
3015a0015d6SChris Zankel	s32i	a8, a1, PT_AREG8
3025a0015d6SChris Zankel	s32i	a9, a1, PT_AREG9
3035a0015d6SChris Zankel	s32i	a10, a1, PT_AREG10
3045a0015d6SChris Zankel	s32i	a11, a1, PT_AREG11
3055a0015d6SChris Zankel	_bbsi.l	a2, 3, 1f
3065a0015d6SChris Zankel	s32i	a12, a1, PT_AREG12
3075a0015d6SChris Zankel	s32i	a13, a1, PT_AREG13
3085a0015d6SChris Zankel	s32i	a14, a1, PT_AREG14
3095a0015d6SChris Zankel	s32i	a15, a1, PT_AREG15
3105a0015d6SChris Zankel
3115a0015d6SChris Zankel1:
3125a0015d6SChris Zankel
3135a0015d6SChris Zankel#ifdef KERNEL_STACK_OVERFLOW_CHECK
3145a0015d6SChris Zankel
3155a0015d6SChris Zankel	/*  Stack overflow check, for debugging  */
3165a0015d6SChris Zankel	extui	a2, a1, TASK_SIZE_BITS,XX
3175a0015d6SChris Zankel	movi	a3, SIZE??
3185a0015d6SChris Zankel	_bge	a2, a3, out_of_stack_panic
3195a0015d6SChris Zankel
3205a0015d6SChris Zankel#endif
3215a0015d6SChris Zankel
3225a0015d6SChris Zankel/*
3235a0015d6SChris Zankel * This is the common exception handler.
3245a0015d6SChris Zankel * We get here from the user exception handler or simply by falling through
3255a0015d6SChris Zankel * from the kernel exception handler.
3265a0015d6SChris Zankel * Save the remaining special registers, switch to kernel mode, and jump
3275a0015d6SChris Zankel * to the second-level exception handler.
3285a0015d6SChris Zankel *
3295a0015d6SChris Zankel */
3305a0015d6SChris Zankel
3315a0015d6SChris Zankelcommon_exception:
3325a0015d6SChris Zankel
3335a0015d6SChris Zankel	/* Save EXCVADDR, DEBUGCAUSE, and PC, and clear LCOUNT */
3345a0015d6SChris Zankel
3355a0015d6SChris Zankel	rsr	a2, DEBUGCAUSE
3365a0015d6SChris Zankel	rsr	a3, EPC_1
3375a0015d6SChris Zankel	s32i	a2, a1, PT_DEBUGCAUSE
3385a0015d6SChris Zankel	s32i	a3, a1, PT_PC
3395a0015d6SChris Zankel
3405a0015d6SChris Zankel	rsr	a3, EXCVADDR
3415a0015d6SChris Zankel	movi	a2, 0
3425a0015d6SChris Zankel	s32i	a3, a1, PT_EXCVADDR
3435a0015d6SChris Zankel	xsr	a2, LCOUNT
3445a0015d6SChris Zankel	s32i	a2, a1, PT_LCOUNT
3455a0015d6SChris Zankel
3465a0015d6SChris Zankel	/* It is now save to restore the EXC_TABLE_FIXUP variable. */
3475a0015d6SChris Zankel
3485a0015d6SChris Zankel	rsr	a0, EXCCAUSE
3495a0015d6SChris Zankel	movi	a3, 0
3505a0015d6SChris Zankel	rsr	a2, EXCSAVE_1
3515a0015d6SChris Zankel	s32i	a0, a1, PT_EXCCAUSE
3525a0015d6SChris Zankel	s32i	a3, a2, EXC_TABLE_FIXUP
3535a0015d6SChris Zankel
3545a0015d6SChris Zankel	/* All unrecoverable states are saved on stack, now, and a1 is valid,
3555a0015d6SChris Zankel	 * so we can allow exceptions and interrupts (*) again.
3565a0015d6SChris Zankel	 * Set PS(EXCM = 0, UM = 0, RING = 0, OWB = 0, WOE = 1, INTLEVEL = X)
3575a0015d6SChris Zankel	 *
3585a0015d6SChris Zankel	 * (*) We only allow interrupts if PS.INTLEVEL was not set to 1 before
3595a0015d6SChris Zankel	 *     (interrupts disabled) and if this exception is not an interrupt.
3605a0015d6SChris Zankel	 */
3615a0015d6SChris Zankel
3625a0015d6SChris Zankel	rsr	a3, PS
3635a0015d6SChris Zankel	addi	a0, a0, -4
3645a0015d6SChris Zankel	movi	a2, 1
3655a0015d6SChris Zankel	extui	a3, a3, 0, 1		# a3 = PS.INTLEVEL[0]
3665a0015d6SChris Zankel	moveqz	a3, a2, a0		# a3 = 1 iff interrupt exception
367173d6681SChris Zankel	movi	a2, 1 << PS_WOE_BIT
3685a0015d6SChris Zankel	or	a3, a3, a2
3695a0015d6SChris Zankel	rsr	a0, EXCCAUSE
3705a0015d6SChris Zankel	xsr	a3, PS
3715a0015d6SChris Zankel
3725a0015d6SChris Zankel	s32i	a3, a1, PT_PS		# save ps
3735a0015d6SChris Zankel
3745a0015d6SChris Zankel	/* Save LBEG, LEND */
3755a0015d6SChris Zankel
3765a0015d6SChris Zankel	rsr	a2, LBEG
3775a0015d6SChris Zankel	rsr	a3, LEND
3785a0015d6SChris Zankel	s32i	a2, a1, PT_LBEG
3795a0015d6SChris Zankel	s32i	a3, a1, PT_LEND
3805a0015d6SChris Zankel
3815a0015d6SChris Zankel	/* Go to second-level dispatcher. Set up parameters to pass to the
3825a0015d6SChris Zankel	 * exception handler and call the exception handler.
3835a0015d6SChris Zankel	 */
3845a0015d6SChris Zankel
3855a0015d6SChris Zankel	movi	a4, exc_table
3865a0015d6SChris Zankel	mov	a6, a1			# pass stack frame
3875a0015d6SChris Zankel	mov	a7, a0			# pass EXCCAUSE
3885a0015d6SChris Zankel	addx4	a4, a0, a4
3895a0015d6SChris Zankel	l32i	a4, a4, EXC_TABLE_DEFAULT		# load handler
3905a0015d6SChris Zankel
3915a0015d6SChris Zankel	/* Call the second-level handler */
3925a0015d6SChris Zankel
3935a0015d6SChris Zankel	callx4	a4
3945a0015d6SChris Zankel
3955a0015d6SChris Zankel	/* Jump here for exception exit */
3965a0015d6SChris Zankel
3975a0015d6SChris Zankelcommon_exception_return:
3985a0015d6SChris Zankel
3995a0015d6SChris Zankel	/* Jump if we are returning from kernel exceptions. */
4005a0015d6SChris Zankel
4015a0015d6SChris Zankel1:	l32i	a3, a1, PT_PS
402173d6681SChris Zankel	_bbsi.l	a3, PS_UM_BIT, 2f
4035a0015d6SChris Zankel	j	kernel_exception_exit
4045a0015d6SChris Zankel
4055a0015d6SChris Zankel	/* Specific to a user exception exit:
4065a0015d6SChris Zankel	 * We need to check some flags for signal handling and rescheduling,
4075a0015d6SChris Zankel	 * and have to restore WB and WS, extra states, and all registers
4085a0015d6SChris Zankel	 * in the register file that were in use in the user task.
4095a0015d6SChris Zankel	 */
4105a0015d6SChris Zankel
4115a0015d6SChris Zankel2:	wsr	a3, PS		/* disable interrupts */
4125a0015d6SChris Zankel
4135a0015d6SChris Zankel	/* Check for signals (keep interrupts disabled while we read TI_FLAGS)
4145a0015d6SChris Zankel	 * Note: PS.INTLEVEL = 0, PS.EXCM = 1
4155a0015d6SChris Zankel	 */
4165a0015d6SChris Zankel
4175a0015d6SChris Zankel	GET_THREAD_INFO(a2,a1)
4185a0015d6SChris Zankel	l32i	a4, a2, TI_FLAGS
4195a0015d6SChris Zankel
4205a0015d6SChris Zankel	/* Enable interrupts again.
4215a0015d6SChris Zankel	 * Note: When we get here, we certainly have handled any interrupts.
4225a0015d6SChris Zankel	 *       (Hint: There is only one user exception frame on stack)
4235a0015d6SChris Zankel	 */
4245a0015d6SChris Zankel
425173d6681SChris Zankel	movi	a3, 1 << PS_WOE_BIT
4265a0015d6SChris Zankel
4275a0015d6SChris Zankel	_bbsi.l	a4, TIF_NEED_RESCHED, 3f
4285a0015d6SChris Zankel	_bbci.l	a4, TIF_SIGPENDING, 4f
4295a0015d6SChris Zankel
4305a0015d6SChris Zankel#ifndef SIGNAL_HANDLING_IN_DOUBLE_EXCEPTION
4315a0015d6SChris Zankel	l32i	a4, a1, PT_DEPC
4325a0015d6SChris Zankel	bgeui	a4, VALID_DOUBLE_EXCEPTION_ADDRESS, 4f
4335a0015d6SChris Zankel#endif
4345a0015d6SChris Zankel
4355a0015d6SChris Zankel	/* Reenable interrupts and call do_signal() */
4365a0015d6SChris Zankel
4375a0015d6SChris Zankel	wsr	a3, PS
4385a0015d6SChris Zankel	movi	a4, do_signal	# int do_signal(struct pt_regs*, sigset_t*)
4395a0015d6SChris Zankel	mov	a6, a1
4405a0015d6SChris Zankel	movi	a7, 0
4415a0015d6SChris Zankel	callx4	a4
4425a0015d6SChris Zankel	j	1b
4435a0015d6SChris Zankel
4445a0015d6SChris Zankel3:	/* Reenable interrupts and reschedule */
4455a0015d6SChris Zankel
4465a0015d6SChris Zankel	wsr	a3, PS
4475a0015d6SChris Zankel	movi	a4, schedule	# void schedule (void)
4485a0015d6SChris Zankel	callx4	a4
4495a0015d6SChris Zankel	j	1b
4505a0015d6SChris Zankel
4515a0015d6SChris Zankel	/* Restore the state of the task and return from the exception. */
4525a0015d6SChris Zankel
4535a0015d6SChris Zankel
4545a0015d6SChris Zankel	/* If we are returning from a user exception, and the process
4555a0015d6SChris Zankel	 * to run next has PT_SINGLESTEP set, we want to setup
4565a0015d6SChris Zankel	 * ICOUNT and ICOUNTLEVEL to step one instruction.
4575a0015d6SChris Zankel	 * PT_SINGLESTEP is set by sys_ptrace (ptrace.c)
4585a0015d6SChris Zankel	 */
4595a0015d6SChris Zankel
4605a0015d6SChris Zankel4:	/* a2 holds GET_CURRENT(a2,a1)  */
4615a0015d6SChris Zankel
4625a0015d6SChris Zankel	l32i	a3, a2, TI_TASK
4635a0015d6SChris Zankel	l32i	a3, a3, TASK_PTRACE
4645a0015d6SChris Zankel	bbci.l	a3, PT_SINGLESTEP_BIT, 1f # jump if single-step flag is not set
4655a0015d6SChris Zankel
4665a0015d6SChris Zankel	movi	a3, -2			# PT_SINGLESTEP flag is set,
4675a0015d6SChris Zankel	movi	a4, 1			# icountlevel of 1 means it won't
4685a0015d6SChris Zankel	wsr	a3, ICOUNT		# start counting until after rfe
4695a0015d6SChris Zankel	wsr	a4, ICOUNTLEVEL		# so setup icount & icountlevel.
4705a0015d6SChris Zankel	isync
4715a0015d6SChris Zankel
4725a0015d6SChris Zankel1:
4735a0015d6SChris Zankel
4745a0015d6SChris Zankel#if XCHAL_EXTRA_SA_SIZE
4755a0015d6SChris Zankel
4765a0015d6SChris Zankel	/* For user exceptions, restore the extra state from the user's TCB. */
4775a0015d6SChris Zankel
4785a0015d6SChris Zankel	/* Note: a2 still contains GET_CURRENT(a2,a1) */
4795a0015d6SChris Zankel	addi	a2, a2, THREAD_CP_SAVE
4805a0015d6SChris Zankel	xchal_extra_load_funcbody
4815a0015d6SChris Zankel
4825a0015d6SChris Zankel	/* We must assume that xchal_extra_store_funcbody destroys
4835a0015d6SChris Zankel	 * registers a2..a15.  FIXME, this list can eventually be
4845a0015d6SChris Zankel	 * reduced once real register requirements of the macro are
4855a0015d6SChris Zankel	 * finalized. */
4865a0015d6SChris Zankel
4875a0015d6SChris Zankel#endif /* XCHAL_EXTRA_SA_SIZE */
4885a0015d6SChris Zankel
4895a0015d6SChris Zankel
4905a0015d6SChris Zankel	/* Switch to the user thread WINDOWBASE. Save SP temporarily in DEPC */
4915a0015d6SChris Zankel
4925a0015d6SChris Zankel	l32i	a2, a1, PT_WINDOWBASE
4935a0015d6SChris Zankel	l32i	a3, a1, PT_WINDOWSTART
4945a0015d6SChris Zankel	wsr	a1, DEPC		# use DEPC as temp storage
4955a0015d6SChris Zankel	wsr	a3, WINDOWSTART		# restore WINDOWSTART
4965a0015d6SChris Zankel	ssr	a2			# preserve user's WB in the SAR
4975a0015d6SChris Zankel	wsr	a2, WINDOWBASE		# switch to user's saved WB
4985a0015d6SChris Zankel	rsync
4995a0015d6SChris Zankel	rsr	a1, DEPC		# restore stack pointer
5005a0015d6SChris Zankel	l32i	a2, a1, PT_WMASK	# register frames saved (in bits 4...9)
5015a0015d6SChris Zankel	rotw	-1			# we restore a4..a7
5025a0015d6SChris Zankel	_bltui	a6, 16, 1f		# only have to restore current window?
5035a0015d6SChris Zankel
5045a0015d6SChris Zankel	/* The working registers are a0 and a3.  We are restoring to
5055a0015d6SChris Zankel	 * a4..a7.  Be careful not to destroy what we have just restored.
5065a0015d6SChris Zankel	 * Note: wmask has the format YYYYM:
5075a0015d6SChris Zankel	 *       Y: number of registers saved in groups of 4
5085a0015d6SChris Zankel	 *       M: 4 bit mask of first 16 registers
5095a0015d6SChris Zankel	 */
5105a0015d6SChris Zankel
5115a0015d6SChris Zankel	mov	a2, a6
5125a0015d6SChris Zankel	mov	a3, a5
5135a0015d6SChris Zankel
5145a0015d6SChris Zankel2:	rotw	-1			# a0..a3 become a4..a7
5155a0015d6SChris Zankel	addi	a3, a7, -4*4		# next iteration
5165a0015d6SChris Zankel	addi	a2, a6, -16		# decrementing Y in WMASK
5175a0015d6SChris Zankel	l32i	a4, a3, PT_AREG_END + 0
5185a0015d6SChris Zankel	l32i	a5, a3, PT_AREG_END + 4
5195a0015d6SChris Zankel	l32i	a6, a3, PT_AREG_END + 8
5205a0015d6SChris Zankel	l32i	a7, a3, PT_AREG_END + 12
5215a0015d6SChris Zankel	_bgeui	a2, 16, 2b
5225a0015d6SChris Zankel
5235a0015d6SChris Zankel	/* Clear unrestored registers (don't leak anything to user-land */
5245a0015d6SChris Zankel
5255a0015d6SChris Zankel1:	rsr	a0, WINDOWBASE
5265a0015d6SChris Zankel	rsr	a3, SAR
5275a0015d6SChris Zankel	sub	a3, a0, a3
5285a0015d6SChris Zankel	beqz	a3, 2f
5295a0015d6SChris Zankel	extui	a3, a3, 0, WBBITS
5305a0015d6SChris Zankel
5315a0015d6SChris Zankel1:	rotw	-1
5325a0015d6SChris Zankel	addi	a3, a7, -1
5335a0015d6SChris Zankel	movi	a4, 0
5345a0015d6SChris Zankel	movi	a5, 0
5355a0015d6SChris Zankel	movi	a6, 0
5365a0015d6SChris Zankel	movi	a7, 0
5375a0015d6SChris Zankel	bgei	a3, 1, 1b
5385a0015d6SChris Zankel
5395a0015d6SChris Zankel	/* We are back were we were when we started.
5405a0015d6SChris Zankel	 * Note: a2 still contains WMASK (if we've returned to the original
5415a0015d6SChris Zankel	 *	 frame where we had loaded a2), or at least the lower 4 bits
5425a0015d6SChris Zankel	 *	 (if we have restored WSBITS-1 frames).
5435a0015d6SChris Zankel	 */
5445a0015d6SChris Zankel
5455a0015d6SChris Zankel2:	j	common_exception_exit
5465a0015d6SChris Zankel
5475a0015d6SChris Zankel	/* This is the kernel exception exit.
5485a0015d6SChris Zankel	 * We avoided to do a MOVSP when we entered the exception, but we
5495a0015d6SChris Zankel	 * have to do it here.
5505a0015d6SChris Zankel	 */
5515a0015d6SChris Zankel
5525a0015d6SChris Zankelkernel_exception_exit:
5535a0015d6SChris Zankel
5545a0015d6SChris Zankel	/* Disable interrupts (a3 holds PT_PS) */
5555a0015d6SChris Zankel
5565a0015d6SChris Zankel	wsr	a3, PS
5575a0015d6SChris Zankel
5585a0015d6SChris Zankel#ifdef PREEMPTIBLE_KERNEL
5595a0015d6SChris Zankel
5605a0015d6SChris Zankel#ifdef CONFIG_PREEMPT
5615a0015d6SChris Zankel
5625a0015d6SChris Zankel	/*
5635a0015d6SChris Zankel	 * Note: We've just returned from a call4, so we have
5645a0015d6SChris Zankel	 * at least 4 addt'l regs.
5655a0015d6SChris Zankel	 */
5665a0015d6SChris Zankel
5675a0015d6SChris Zankel	/* Check current_thread_info->preempt_count */
5685a0015d6SChris Zankel
5695a0015d6SChris Zankel	GET_THREAD_INFO(a2)
5705a0015d6SChris Zankel	l32i	a3, a2, TI_PREEMPT
5715a0015d6SChris Zankel	bnez	a3, 1f
5725a0015d6SChris Zankel
5735a0015d6SChris Zankel	l32i	a2, a2, TI_FLAGS
5745a0015d6SChris Zankel
5755a0015d6SChris Zankel1:
5765a0015d6SChris Zankel
5775a0015d6SChris Zankel#endif
5785a0015d6SChris Zankel
5795a0015d6SChris Zankel#endif
5805a0015d6SChris Zankel
5815a0015d6SChris Zankel	/* Check if we have to do a movsp.
5825a0015d6SChris Zankel	 *
5835a0015d6SChris Zankel	 * We only have to do a movsp if the previous window-frame has
5845a0015d6SChris Zankel	 * been spilled to the *temporary* exception stack instead of the
5855a0015d6SChris Zankel	 * task's stack. This is the case if the corresponding bit in
5865a0015d6SChris Zankel	 * WINDOWSTART for the previous window-frame was set before
5875a0015d6SChris Zankel	 * (not spilled) but is zero now (spilled).
5885a0015d6SChris Zankel	 * If this bit is zero, all other bits except the one for the
5895a0015d6SChris Zankel	 * current window frame are also zero. So, we can use a simple test:
5905a0015d6SChris Zankel	 * 'and' WINDOWSTART and WINDOWSTART-1:
5915a0015d6SChris Zankel	 *
5925a0015d6SChris Zankel	 *  (XXXXXX1[0]* - 1) AND XXXXXX1[0]* = XXXXXX0[0]*
5935a0015d6SChris Zankel	 *
5945a0015d6SChris Zankel	 * The result is zero only if one bit was set.
5955a0015d6SChris Zankel	 *
5965a0015d6SChris Zankel	 * (Note: We might have gone through several task switches before
5975a0015d6SChris Zankel	 *        we come back to the current task, so WINDOWBASE might be
5985a0015d6SChris Zankel	 *        different from the time the exception occurred.)
5995a0015d6SChris Zankel	 */
6005a0015d6SChris Zankel
6015a0015d6SChris Zankel	/* Test WINDOWSTART before and after the exception.
6025a0015d6SChris Zankel	 * We actually have WMASK, so we only have to test if it is 1 or not.
6035a0015d6SChris Zankel	 */
6045a0015d6SChris Zankel
6055a0015d6SChris Zankel	l32i	a2, a1, PT_WMASK
6065a0015d6SChris Zankel	_beqi	a2, 1, common_exception_exit	# Spilled before exception,jump
6075a0015d6SChris Zankel
6085a0015d6SChris Zankel	/* Test WINDOWSTART now. If spilled, do the movsp */
6095a0015d6SChris Zankel
6105a0015d6SChris Zankel	rsr     a3, WINDOWSTART
6115a0015d6SChris Zankel	addi	a0, a3, -1
6125a0015d6SChris Zankel	and     a3, a3, a0
6135a0015d6SChris Zankel	_bnez	a3, common_exception_exit
6145a0015d6SChris Zankel
6155a0015d6SChris Zankel	/* Do a movsp (we returned from a call4, so we have at least a0..a7) */
6165a0015d6SChris Zankel
6175a0015d6SChris Zankel	addi    a0, a1, -16
6185a0015d6SChris Zankel	l32i    a3, a0, 0
6195a0015d6SChris Zankel	l32i    a4, a0, 4
6205a0015d6SChris Zankel	s32i    a3, a1, PT_SIZE+0
6215a0015d6SChris Zankel	s32i    a4, a1, PT_SIZE+4
6225a0015d6SChris Zankel	l32i    a3, a0, 8
6235a0015d6SChris Zankel	l32i    a4, a0, 12
6245a0015d6SChris Zankel	s32i    a3, a1, PT_SIZE+8
6255a0015d6SChris Zankel	s32i    a4, a1, PT_SIZE+12
6265a0015d6SChris Zankel
6275a0015d6SChris Zankel	/* Common exception exit.
6285a0015d6SChris Zankel	 * We restore the special register and the current window frame, and
6295a0015d6SChris Zankel	 * return from the exception.
6305a0015d6SChris Zankel	 *
6315a0015d6SChris Zankel	 * Note: We expect a2 to hold PT_WMASK
6325a0015d6SChris Zankel	 */
6335a0015d6SChris Zankel
6345a0015d6SChris Zankelcommon_exception_exit:
6355a0015d6SChris Zankel
6365a0015d6SChris Zankel	_bbsi.l	a2, 1, 1f
6375a0015d6SChris Zankel	l32i	a4,  a1, PT_AREG4
6385a0015d6SChris Zankel	l32i	a5,  a1, PT_AREG5
6395a0015d6SChris Zankel	l32i	a6,  a1, PT_AREG6
6405a0015d6SChris Zankel	l32i	a7,  a1, PT_AREG7
6415a0015d6SChris Zankel	_bbsi.l	a2, 2, 1f
6425a0015d6SChris Zankel	l32i	a8,  a1, PT_AREG8
6435a0015d6SChris Zankel	l32i	a9,  a1, PT_AREG9
6445a0015d6SChris Zankel	l32i	a10, a1, PT_AREG10
6455a0015d6SChris Zankel	l32i	a11, a1, PT_AREG11
6465a0015d6SChris Zankel	_bbsi.l	a2, 3, 1f
6475a0015d6SChris Zankel	l32i	a12, a1, PT_AREG12
6485a0015d6SChris Zankel	l32i	a13, a1, PT_AREG13
6495a0015d6SChris Zankel	l32i	a14, a1, PT_AREG14
6505a0015d6SChris Zankel	l32i	a15, a1, PT_AREG15
6515a0015d6SChris Zankel
6525a0015d6SChris Zankel	/* Restore PC, SAR */
6535a0015d6SChris Zankel
6545a0015d6SChris Zankel1:	l32i	a2, a1, PT_PC
6555a0015d6SChris Zankel	l32i	a3, a1, PT_SAR
6565a0015d6SChris Zankel	wsr	a2, EPC_1
6575a0015d6SChris Zankel	wsr	a3, SAR
6585a0015d6SChris Zankel
6595a0015d6SChris Zankel	/* Restore LBEG, LEND, LCOUNT */
6605a0015d6SChris Zankel
6615a0015d6SChris Zankel	l32i	a2, a1, PT_LBEG
6625a0015d6SChris Zankel	l32i	a3, a1, PT_LEND
6635a0015d6SChris Zankel	wsr	a2, LBEG
6645a0015d6SChris Zankel	l32i	a2, a1, PT_LCOUNT
6655a0015d6SChris Zankel	wsr	a3, LEND
6665a0015d6SChris Zankel	wsr	a2, LCOUNT
6675a0015d6SChris Zankel
6685a0015d6SChris Zankel	/* Check if it was double exception. */
6695a0015d6SChris Zankel
6705a0015d6SChris Zankel	l32i	a0, a1, PT_DEPC
6715a0015d6SChris Zankel	l32i	a3, a1, PT_AREG3
6725a0015d6SChris Zankel	l32i	a2, a1, PT_AREG2
6735a0015d6SChris Zankel	_bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
6745a0015d6SChris Zankel
6755a0015d6SChris Zankel	/* Restore a0...a3 and return */
6765a0015d6SChris Zankel
6775a0015d6SChris Zankel	l32i	a0, a1, PT_AREG0
6785a0015d6SChris Zankel	l32i	a1, a1, PT_AREG1
6795a0015d6SChris Zankel	rfe
6805a0015d6SChris Zankel
6815a0015d6SChris Zankel1:	wsr	a0, DEPC
6825a0015d6SChris Zankel	l32i	a0, a1, PT_AREG0
6835a0015d6SChris Zankel	l32i	a1, a1, PT_AREG1
6845a0015d6SChris Zankel	rfde
6855a0015d6SChris Zankel
6865a0015d6SChris Zankel/*
6875a0015d6SChris Zankel * Debug exception handler.
6885a0015d6SChris Zankel *
6895a0015d6SChris Zankel * Currently, we don't support KGDB, so only user application can be debugged.
6905a0015d6SChris Zankel *
6915a0015d6SChris Zankel * When we get here,  a0 is trashed and saved to excsave[debuglevel]
6925a0015d6SChris Zankel */
6935a0015d6SChris Zankel
6945a0015d6SChris ZankelENTRY(debug_exception)
6955a0015d6SChris Zankel
6965a0015d6SChris Zankel	rsr	a0, EPS + XCHAL_DEBUGLEVEL
697173d6681SChris Zankel	bbsi.l	a0, PS_EXCM_BIT, 1f	# exception mode
6985a0015d6SChris Zankel
6995a0015d6SChris Zankel	/* Set EPC_1 and EXCCAUSE */
7005a0015d6SChris Zankel
7015a0015d6SChris Zankel	wsr	a2, DEPC		# save a2 temporarily
7025a0015d6SChris Zankel	rsr	a2, EPC + XCHAL_DEBUGLEVEL
7035a0015d6SChris Zankel	wsr	a2, EPC_1
7045a0015d6SChris Zankel
7055a0015d6SChris Zankel	movi	a2, EXCCAUSE_MAPPED_DEBUG
7065a0015d6SChris Zankel	wsr	a2, EXCCAUSE
7075a0015d6SChris Zankel
7085a0015d6SChris Zankel	/* Restore PS to the value before the debug exc but with PS.EXCM set.*/
7095a0015d6SChris Zankel
710173d6681SChris Zankel	movi	a2, 1 << PS_EXCM_BIT
7115a0015d6SChris Zankel	or	a2, a0, a2
7125a0015d6SChris Zankel	movi	a0, debug_exception	# restore a3, debug jump vector
7135a0015d6SChris Zankel	wsr	a2, PS
7145a0015d6SChris Zankel	xsr	a0, EXCSAVE + XCHAL_DEBUGLEVEL
7155a0015d6SChris Zankel
7165a0015d6SChris Zankel	/* Switch to kernel/user stack, restore jump vector, and save a0 */
7175a0015d6SChris Zankel
718173d6681SChris Zankel	bbsi.l	a2, PS_UM_BIT, 2f	# jump if user mode
7195a0015d6SChris Zankel
7205a0015d6SChris Zankel	addi	a2, a1, -16-PT_SIZE	# assume kernel stack
7215a0015d6SChris Zankel	s32i	a0, a2, PT_AREG0
7225a0015d6SChris Zankel	movi	a0, 0
7235a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
7245a0015d6SChris Zankel	s32i	a0, a2, PT_DEPC		# mark it as a regular exception
7255a0015d6SChris Zankel	xsr	a0, DEPC
7265a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
7275a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2
7285a0015d6SChris Zankel	mov	a1, a2
7295a0015d6SChris Zankel	j	_kernel_exception
7305a0015d6SChris Zankel
7315a0015d6SChris Zankel2:	rsr	a2, EXCSAVE_1
7325a0015d6SChris Zankel	l32i	a2, a2, EXC_TABLE_KSTK	# load kernel stack pointer
7335a0015d6SChris Zankel	s32i	a0, a2, PT_AREG0
7345a0015d6SChris Zankel	movi	a0, 0
7355a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
7365a0015d6SChris Zankel	s32i	a0, a2, PT_DEPC
7375a0015d6SChris Zankel	xsr	a0, DEPC
7385a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
7395a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2
7405a0015d6SChris Zankel	mov	a1, a2
7415a0015d6SChris Zankel	j	_user_exception
7425a0015d6SChris Zankel
7435a0015d6SChris Zankel	/* Debug exception while in exception mode. */
7445a0015d6SChris Zankel1:	j	1b	// FIXME!!
7455a0015d6SChris Zankel
7465a0015d6SChris Zankel
7475a0015d6SChris Zankel/*
7485a0015d6SChris Zankel * We get here in case of an unrecoverable exception.
7495a0015d6SChris Zankel * The only thing we can do is to be nice and print a panic message.
7505a0015d6SChris Zankel * We only produce a single stack frame for panic, so ???
7515a0015d6SChris Zankel *
7525a0015d6SChris Zankel *
7535a0015d6SChris Zankel * Entry conditions:
7545a0015d6SChris Zankel *
7555a0015d6SChris Zankel *   - a0 contains the caller address; original value saved in excsave1.
7565a0015d6SChris Zankel *   - the original a0 contains a valid return address (backtrace) or 0.
7575a0015d6SChris Zankel *   - a2 contains a valid stackpointer
7585a0015d6SChris Zankel *
7595a0015d6SChris Zankel * Notes:
7605a0015d6SChris Zankel *
7615a0015d6SChris Zankel *   - If the stack pointer could be invalid, the caller has to setup a
7625a0015d6SChris Zankel *     dummy stack pointer (e.g. the stack of the init_task)
7635a0015d6SChris Zankel *
7645a0015d6SChris Zankel *   - If the return address could be invalid, the caller has to set it
7655a0015d6SChris Zankel *     to 0, so the backtrace would stop.
7665a0015d6SChris Zankel *
7675a0015d6SChris Zankel */
7685a0015d6SChris Zankel	.align 4
7695a0015d6SChris Zankelunrecoverable_text:
7705a0015d6SChris Zankel	.ascii "Unrecoverable error in exception handler\0"
7715a0015d6SChris Zankel
7725a0015d6SChris ZankelENTRY(unrecoverable_exception)
7735a0015d6SChris Zankel
7745a0015d6SChris Zankel	movi	a0, 1
7755a0015d6SChris Zankel	movi	a1, 0
7765a0015d6SChris Zankel
7775a0015d6SChris Zankel	wsr	a0, WINDOWSTART
7785a0015d6SChris Zankel	wsr	a1, WINDOWBASE
7795a0015d6SChris Zankel	rsync
7805a0015d6SChris Zankel
781173d6681SChris Zankel	movi	a1, (1 << PS_WOE_BIT) | 1
7825a0015d6SChris Zankel	wsr	a1, PS
7835a0015d6SChris Zankel	rsync
7845a0015d6SChris Zankel
7855a0015d6SChris Zankel	movi	a1, init_task
7865a0015d6SChris Zankel	movi	a0, 0
7875a0015d6SChris Zankel	addi	a1, a1, PT_REGS_OFFSET
7885a0015d6SChris Zankel
7895a0015d6SChris Zankel	movi	a4, panic
7905a0015d6SChris Zankel	movi	a6, unrecoverable_text
7915a0015d6SChris Zankel
7925a0015d6SChris Zankel	callx4	a4
7935a0015d6SChris Zankel
7945a0015d6SChris Zankel1:	j	1b
7955a0015d6SChris Zankel
7965a0015d6SChris Zankel
7975a0015d6SChris Zankel/* -------------------------- FAST EXCEPTION HANDLERS ----------------------- */
7985a0015d6SChris Zankel
7995a0015d6SChris Zankel/*
8005a0015d6SChris Zankel * Fast-handler for alloca exceptions
8015a0015d6SChris Zankel *
8025a0015d6SChris Zankel *  The ALLOCA handler is entered when user code executes the MOVSP
8035a0015d6SChris Zankel *  instruction and the caller's frame is not in the register file.
8045a0015d6SChris Zankel *  In this case, the caller frame's a0..a3 are on the stack just
8055a0015d6SChris Zankel *  below sp (a1), and this handler moves them.
8065a0015d6SChris Zankel *
8075a0015d6SChris Zankel *  For "MOVSP <ar>,<as>" without destination register a1, this routine
8085a0015d6SChris Zankel *  simply moves the value from <as> to <ar> without moving the save area.
8095a0015d6SChris Zankel *
8105a0015d6SChris Zankel * Entry condition:
8115a0015d6SChris Zankel *
8125a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
8135a0015d6SChris Zankel *   a1:	a1
8145a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
8155a0015d6SChris Zankel *   a3:	dispatch table
8165a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
8175a0015d6SChris Zankel *   excsave_1:	a3
8185a0015d6SChris Zankel *
8195a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
8205a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
8215a0015d6SChris Zankel */
8225a0015d6SChris Zankel
8235a0015d6SChris Zankel#if XCHAL_HAVE_BE
8245a0015d6SChris Zankel#define _EXTUI_MOVSP_SRC(ar)	extui ar, ar, 4, 4
8255a0015d6SChris Zankel#define _EXTUI_MOVSP_DST(ar)	extui ar, ar, 0, 4
8265a0015d6SChris Zankel#else
8275a0015d6SChris Zankel#define _EXTUI_MOVSP_SRC(ar)	extui ar, ar, 0, 4
8285a0015d6SChris Zankel#define _EXTUI_MOVSP_DST(ar)	extui ar, ar, 4, 4
8295a0015d6SChris Zankel#endif
8305a0015d6SChris Zankel
8315a0015d6SChris ZankelENTRY(fast_alloca)
8325a0015d6SChris Zankel
8335a0015d6SChris Zankel	/* We shouldn't be in a double exception. */
8345a0015d6SChris Zankel
8355a0015d6SChris Zankel	l32i	a0, a2, PT_DEPC
8365a0015d6SChris Zankel	_bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, .Lunhandled_double
8375a0015d6SChris Zankel
8385a0015d6SChris Zankel	rsr	a0, DEPC		# get a2
8395a0015d6SChris Zankel	s32i	a4, a2, PT_AREG4	# save a4 and
8405a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2	# a2 to stack
8415a0015d6SChris Zankel
8425a0015d6SChris Zankel	/* Exit critical section. */
8435a0015d6SChris Zankel
8445a0015d6SChris Zankel	movi	a0, 0
8455a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_FIXUP
8465a0015d6SChris Zankel
8475a0015d6SChris Zankel	/* Restore a3, excsave_1 */
8485a0015d6SChris Zankel
8495a0015d6SChris Zankel	xsr	a3, EXCSAVE_1		# make sure excsave_1 is valid for dbl.
8505a0015d6SChris Zankel	rsr	a4, EPC_1		# get exception address
8515a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3	# save a3 to stack
8525a0015d6SChris Zankel
8535a0015d6SChris Zankel#ifdef ALLOCA_EXCEPTION_IN_IRAM
8545a0015d6SChris Zankel#error	iram not supported
8555a0015d6SChris Zankel#else
8565a0015d6SChris Zankel	/* Note: l8ui not allowed in IRAM/IROM!! */
8575a0015d6SChris Zankel	l8ui	a0, a4, 1		# read as(src) from MOVSP instruction
8585a0015d6SChris Zankel#endif
8595a0015d6SChris Zankel	movi	a3, .Lmovsp_src
8605a0015d6SChris Zankel	_EXTUI_MOVSP_SRC(a0)		# extract source register number
8615a0015d6SChris Zankel	addx8	a3, a0, a3
8625a0015d6SChris Zankel	jx	a3
8635a0015d6SChris Zankel
8645a0015d6SChris Zankel.Lunhandled_double:
8655a0015d6SChris Zankel	wsr	a0, EXCSAVE_1
8665a0015d6SChris Zankel	movi	a0, unrecoverable_exception
8675a0015d6SChris Zankel	callx0	a0
8685a0015d6SChris Zankel
8695a0015d6SChris Zankel	.align 8
8705a0015d6SChris Zankel.Lmovsp_src:
8715a0015d6SChris Zankel	l32i	a3, a2, PT_AREG0;	_j 1f;	.align 8
8725a0015d6SChris Zankel	mov	a3, a1;			_j 1f;	.align 8
8735a0015d6SChris Zankel	l32i	a3, a2, PT_AREG2;	_j 1f;	.align 8
8745a0015d6SChris Zankel	l32i	a3, a2, PT_AREG3;	_j 1f;	.align 8
8755a0015d6SChris Zankel	l32i	a3, a2, PT_AREG4;	_j 1f;	.align 8
8765a0015d6SChris Zankel	mov	a3, a5;			_j 1f;	.align 8
8775a0015d6SChris Zankel	mov	a3, a6;			_j 1f;	.align 8
8785a0015d6SChris Zankel	mov	a3, a7;			_j 1f;	.align 8
8795a0015d6SChris Zankel	mov	a3, a8;			_j 1f;	.align 8
8805a0015d6SChris Zankel	mov	a3, a9;			_j 1f;	.align 8
8815a0015d6SChris Zankel	mov	a3, a10;		_j 1f;	.align 8
8825a0015d6SChris Zankel	mov	a3, a11;		_j 1f;	.align 8
8835a0015d6SChris Zankel	mov	a3, a12;		_j 1f;	.align 8
8845a0015d6SChris Zankel	mov	a3, a13;		_j 1f;	.align 8
8855a0015d6SChris Zankel	mov	a3, a14;		_j 1f;	.align 8
8865a0015d6SChris Zankel	mov	a3, a15;		_j 1f;	.align 8
8875a0015d6SChris Zankel
8885a0015d6SChris Zankel1:
8895a0015d6SChris Zankel
8905a0015d6SChris Zankel#ifdef ALLOCA_EXCEPTION_IN_IRAM
8915a0015d6SChris Zankel#error	iram not supported
8925a0015d6SChris Zankel#else
8935a0015d6SChris Zankel	l8ui	a0, a4, 0		# read ar(dst) from MOVSP instruction
8945a0015d6SChris Zankel#endif
8955a0015d6SChris Zankel	addi	a4, a4, 3		# step over movsp
8965a0015d6SChris Zankel	_EXTUI_MOVSP_DST(a0)		# extract destination register
8975a0015d6SChris Zankel	wsr	a4, EPC_1		# save new epc_1
8985a0015d6SChris Zankel
8995a0015d6SChris Zankel	_bnei	a0, 1, 1f		# no 'movsp a1, ax': jump
9005a0015d6SChris Zankel
9015a0015d6SChris Zankel        /* Move the save area. This implies the use of the L32E
9025a0015d6SChris Zankel	 * and S32E instructions, because this move must be done with
9035a0015d6SChris Zankel	 * the user's PS.RING privilege levels, not with ring 0
9045a0015d6SChris Zankel	 * (kernel's) privileges currently active with PS.EXCM
9055a0015d6SChris Zankel	 * set. Note that we have stil registered a fixup routine with the
9065a0015d6SChris Zankel	 * double exception vector in case a double exception occurs.
9075a0015d6SChris Zankel	 */
9085a0015d6SChris Zankel
9095a0015d6SChris Zankel	/* a0,a4:avail a1:old user stack a2:exc. stack a3:new user stack. */
9105a0015d6SChris Zankel
9115a0015d6SChris Zankel	l32e	a0, a1, -16
9125a0015d6SChris Zankel	l32e	a4, a1, -12
9135a0015d6SChris Zankel	s32e	a0, a3, -16
9145a0015d6SChris Zankel	s32e	a4, a3, -12
9155a0015d6SChris Zankel	l32e	a0, a1, -8
9165a0015d6SChris Zankel	l32e	a4, a1, -4
9175a0015d6SChris Zankel	s32e	a0, a3, -8
9185a0015d6SChris Zankel	s32e	a4, a3, -4
9195a0015d6SChris Zankel
9205a0015d6SChris Zankel	/* Restore stack-pointer and all the other saved registers. */
9215a0015d6SChris Zankel
9225a0015d6SChris Zankel	mov	a1, a3
9235a0015d6SChris Zankel
9245a0015d6SChris Zankel	l32i	a4, a2, PT_AREG4
9255a0015d6SChris Zankel	l32i	a3, a2, PT_AREG3
9265a0015d6SChris Zankel	l32i	a0, a2, PT_AREG0
9275a0015d6SChris Zankel	l32i	a2, a2, PT_AREG2
9285a0015d6SChris Zankel	rfe
9295a0015d6SChris Zankel
9305a0015d6SChris Zankel	/*  MOVSP <at>,<as>  was invoked with <at> != a1.
9315a0015d6SChris Zankel	 *  Because the stack pointer is not being modified,
9325a0015d6SChris Zankel	 *  we should be able to just modify the pointer
9335a0015d6SChris Zankel	 *  without moving any save area.
9345a0015d6SChris Zankel	 *  The processor only traps these occurrences if the
9355a0015d6SChris Zankel	 *  caller window isn't live, so unfortunately we can't
9365a0015d6SChris Zankel	 *  use this as an alternate trap mechanism.
9375a0015d6SChris Zankel	 *  So we just do the move.  This requires that we
9385a0015d6SChris Zankel	 *  resolve the destination register, not just the source,
9395a0015d6SChris Zankel	 *  so there's some extra work.
9405a0015d6SChris Zankel	 *  (PERHAPS NOT REALLY NEEDED, BUT CLEANER...)
9415a0015d6SChris Zankel	 */
9425a0015d6SChris Zankel
9435a0015d6SChris Zankel	/* a0 dst-reg, a1 user-stack, a2 stack, a3 value of src reg. */
9445a0015d6SChris Zankel
9455a0015d6SChris Zankel1:	movi	a4, .Lmovsp_dst
9465a0015d6SChris Zankel	addx8	a4, a0, a4
9475a0015d6SChris Zankel	jx	a4
9485a0015d6SChris Zankel
9495a0015d6SChris Zankel	.align 8
9505a0015d6SChris Zankel.Lmovsp_dst:
9515a0015d6SChris Zankel	s32i	a3, a2, PT_AREG0;	_j 1f;	.align 8
9525a0015d6SChris Zankel	mov	a1, a3;			_j 1f;	.align 8
9535a0015d6SChris Zankel	s32i	a3, a2, PT_AREG2;	_j 1f;	.align 8
9545a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3;	_j 1f;	.align 8
9555a0015d6SChris Zankel	s32i	a3, a2, PT_AREG4;	_j 1f;	.align 8
9565a0015d6SChris Zankel	mov	a5, a3;			_j 1f;	.align 8
9575a0015d6SChris Zankel	mov	a6, a3;			_j 1f;	.align 8
9585a0015d6SChris Zankel	mov	a7, a3;			_j 1f;	.align 8
9595a0015d6SChris Zankel	mov	a8, a3;			_j 1f;	.align 8
9605a0015d6SChris Zankel	mov	a9, a3;			_j 1f;	.align 8
9615a0015d6SChris Zankel	mov	a10, a3;		_j 1f;	.align 8
9625a0015d6SChris Zankel	mov	a11, a3;		_j 1f;	.align 8
9635a0015d6SChris Zankel	mov	a12, a3;		_j 1f;	.align 8
9645a0015d6SChris Zankel	mov	a13, a3;		_j 1f;	.align 8
9655a0015d6SChris Zankel	mov	a14, a3;		_j 1f;	.align 8
9665a0015d6SChris Zankel	mov	a15, a3;		_j 1f;	.align 8
9675a0015d6SChris Zankel
9685a0015d6SChris Zankel1:	l32i	a4, a2, PT_AREG4
9695a0015d6SChris Zankel	l32i	a3, a2, PT_AREG3
9705a0015d6SChris Zankel	l32i	a0, a2, PT_AREG0
9715a0015d6SChris Zankel	l32i	a2, a2, PT_AREG2
9725a0015d6SChris Zankel	rfe
9735a0015d6SChris Zankel
9745a0015d6SChris Zankel
9755a0015d6SChris Zankel/*
9765a0015d6SChris Zankel * fast system calls.
9775a0015d6SChris Zankel *
9785a0015d6SChris Zankel * WARNING:  The kernel doesn't save the entire user context before
9795a0015d6SChris Zankel * handling a fast system call.  These functions are small and short,
9805a0015d6SChris Zankel * usually offering some functionality not available to user tasks.
9815a0015d6SChris Zankel *
9825a0015d6SChris Zankel * BE CAREFUL TO PRESERVE THE USER'S CONTEXT.
9835a0015d6SChris Zankel *
9845a0015d6SChris Zankel * Entry condition:
9855a0015d6SChris Zankel *
9865a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
9875a0015d6SChris Zankel *   a1:	a1
9885a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
9895a0015d6SChris Zankel *   a3:	dispatch table
9905a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
9915a0015d6SChris Zankel *   excsave_1:	a3
9925a0015d6SChris Zankel */
9935a0015d6SChris Zankel
9945a0015d6SChris ZankelENTRY(fast_syscall_kernel)
9955a0015d6SChris Zankel
9965a0015d6SChris Zankel	/* Skip syscall. */
9975a0015d6SChris Zankel
9985a0015d6SChris Zankel	rsr	a0, EPC_1
9995a0015d6SChris Zankel	addi	a0, a0, 3
10005a0015d6SChris Zankel	wsr	a0, EPC_1
10015a0015d6SChris Zankel
10025a0015d6SChris Zankel	l32i	a0, a2, PT_DEPC
10035a0015d6SChris Zankel	bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, fast_syscall_unrecoverable
10045a0015d6SChris Zankel
10055a0015d6SChris Zankel	rsr	a0, DEPC			# get syscall-nr
10065a0015d6SChris Zankel	_beqz	a0, fast_syscall_spill_registers
1007*fc4fb2adSChris Zankel	_beqi	a0, __NR_xtensa, fast_syscall_xtensa
10085a0015d6SChris Zankel
10095a0015d6SChris Zankel	j	kernel_exception
10105a0015d6SChris Zankel
10115a0015d6SChris ZankelENTRY(fast_syscall_user)
10125a0015d6SChris Zankel
10135a0015d6SChris Zankel	/* Skip syscall. */
10145a0015d6SChris Zankel
10155a0015d6SChris Zankel	rsr	a0, EPC_1
10165a0015d6SChris Zankel	addi	a0, a0, 3
10175a0015d6SChris Zankel	wsr	a0, EPC_1
10185a0015d6SChris Zankel
10195a0015d6SChris Zankel	l32i	a0, a2, PT_DEPC
10205a0015d6SChris Zankel	bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, fast_syscall_unrecoverable
10215a0015d6SChris Zankel
10225a0015d6SChris Zankel	rsr	a0, DEPC			# get syscall-nr
10235a0015d6SChris Zankel	_beqz	a0, fast_syscall_spill_registers
1024*fc4fb2adSChris Zankel	_beqi	a0, __NR_xtensa, fast_syscall_xtensa
10255a0015d6SChris Zankel
10265a0015d6SChris Zankel	j	user_exception
10275a0015d6SChris Zankel
10285a0015d6SChris ZankelENTRY(fast_syscall_unrecoverable)
10295a0015d6SChris Zankel
10305a0015d6SChris Zankel        /* Restore all states. */
10315a0015d6SChris Zankel
10325a0015d6SChris Zankel        l32i    a0, a2, PT_AREG0        # restore a0
10335a0015d6SChris Zankel        xsr     a2, DEPC                # restore a2, depc
10345a0015d6SChris Zankel        rsr     a3, EXCSAVE_1
10355a0015d6SChris Zankel
10365a0015d6SChris Zankel        wsr     a0, EXCSAVE_1
10375a0015d6SChris Zankel        movi    a0, unrecoverable_exception
10385a0015d6SChris Zankel        callx0  a0
10395a0015d6SChris Zankel
10405a0015d6SChris Zankel
10415a0015d6SChris Zankel
10425a0015d6SChris Zankel/*
10435a0015d6SChris Zankel * sysxtensa syscall handler
10445a0015d6SChris Zankel *
1045*fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_SET,     ptr, val,    unused);
1046*fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_ADD,     ptr, val,    unused);
1047*fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_EXG_ADD, ptr, val,    unused);
1048*fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_CMP_SWP, ptr, oldval, newval);
10495a0015d6SChris Zankel *        a2            a6                   a3    a4      a5
10505a0015d6SChris Zankel *
10515a0015d6SChris Zankel * Entry condition:
10525a0015d6SChris Zankel *
1053*fc4fb2adSChris Zankel *   a0:	a2 (syscall-nr), original value saved on stack (PT_AREG0)
10545a0015d6SChris Zankel *   a1:	a1
1055*fc4fb2adSChris Zankel *   a2:	new stack pointer, original in a0 and DEPC
1056*fc4fb2adSChris Zankel *   a3:	dispatch table, original in excsave_1
1057*fc4fb2adSChris Zankel *   a4..a15:	unchanged
10585a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
10595a0015d6SChris Zankel *   excsave_1:	a3
10605a0015d6SChris Zankel *
10615a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
10625a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
10635a0015d6SChris Zankel *
10645a0015d6SChris Zankel * Note: we don't have to save a2; a2 holds the return value
10655a0015d6SChris Zankel *
10665a0015d6SChris Zankel * We use the two macros TRY and CATCH:
10675a0015d6SChris Zankel *
10685a0015d6SChris Zankel * TRY	 adds an entry to the __ex_table fixup table for the immediately
10695a0015d6SChris Zankel *	 following instruction.
10705a0015d6SChris Zankel *
10715a0015d6SChris Zankel * CATCH catches any exception that occurred at one of the preceeding TRY
10725a0015d6SChris Zankel *       statements and continues from there
10735a0015d6SChris Zankel *
10745a0015d6SChris Zankel * Usage TRY	l32i	a0, a1, 0
10755a0015d6SChris Zankel *		<other code>
10765a0015d6SChris Zankel *	 done:	rfe
10775a0015d6SChris Zankel *	 CATCH	<set return code>
10785a0015d6SChris Zankel *		j done
10795a0015d6SChris Zankel */
10805a0015d6SChris Zankel
10815a0015d6SChris Zankel#define TRY								\
10825a0015d6SChris Zankel	.section __ex_table, "a";					\
10835a0015d6SChris Zankel	.word	66f, 67f;						\
10845a0015d6SChris Zankel	.text;								\
10855a0015d6SChris Zankel66:
10865a0015d6SChris Zankel
10875a0015d6SChris Zankel#define CATCH								\
10885a0015d6SChris Zankel67:
10895a0015d6SChris Zankel
1090*fc4fb2adSChris ZankelENTRY(fast_syscall_xtensa)
10915a0015d6SChris Zankel
1092*fc4fb2adSChris Zankel	xsr	a3, EXCSAVE_1		# restore a3, excsave1
10935a0015d6SChris Zankel
1094*fc4fb2adSChris Zankel	s32i	a7, a2, PT_AREG7	# we need an additional register
10955a0015d6SChris Zankel	movi	a7, 4			# sizeof(unsigned int)
1096*fc4fb2adSChris Zankel	access_ok a3, a7, a0, a2, .Leac	# a0: scratch reg, a2: sp
10975a0015d6SChris Zankel
1098*fc4fb2adSChris Zankel	addi	a6, a6, -1		# assuming SYS_XTENSA_ATOMIC_SET = 1
1099*fc4fb2adSChris Zankel	_bgeui	a6, SYS_XTENSA_COUNT - 1, .Lill
1100*fc4fb2adSChris Zankel	_bnei	a6, SYS_XTENSA_ATOMIC_CMP_SWP - 1, .Lnswp
11015a0015d6SChris Zankel
1102*fc4fb2adSChris Zankel	/* Fall through for ATOMIC_CMP_SWP. */
11035a0015d6SChris Zankel
11045a0015d6SChris Zankel.Lswp:	/* Atomic compare and swap */
11055a0015d6SChris Zankel
1106*fc4fb2adSChris ZankelTRY	l32i	a0, a3, 0		# read old value
1107*fc4fb2adSChris Zankel	bne	a0, a4, 1f		# same as old value? jump
1108*fc4fb2adSChris ZankelTRY	s32i	a5, a3, 0		# different, modify value
1109*fc4fb2adSChris Zankel	l32i	a7, a2, PT_AREG7	# restore a7
1110*fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1111*fc4fb2adSChris Zankel	movi	a2, 1			# and return 1
1112*fc4fb2adSChris Zankel	addi	a6, a6, 1		# restore a6 (really necessary?)
1113*fc4fb2adSChris Zankel	rfe
11145a0015d6SChris Zankel
1115*fc4fb2adSChris Zankel1:	l32i	a7, a2, PT_AREG7	# restore a7
1116*fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1117*fc4fb2adSChris Zankel	movi	a2, 0			# return 0 (note that we cannot set
1118*fc4fb2adSChris Zankel	addi	a6, a6, 1		# restore a6 (really necessary?)
1119*fc4fb2adSChris Zankel	rfe
11205a0015d6SChris Zankel
1121*fc4fb2adSChris Zankel.Lnswp:	/* Atomic set, add, and exg_add. */
11225a0015d6SChris Zankel
1123*fc4fb2adSChris ZankelTRY	l32i	a7, a3, 0		# orig
1124*fc4fb2adSChris Zankel	add	a0, a4, a7		# + arg
1125*fc4fb2adSChris Zankel	moveqz	a0, a4, a6		# set
1126*fc4fb2adSChris ZankelTRY	s32i	a0, a3, 0		# write new value
11275a0015d6SChris Zankel
1128*fc4fb2adSChris Zankel	mov	a0, a2
11295a0015d6SChris Zankel	mov	a2, a7
1130*fc4fb2adSChris Zankel	l32i	a7, a0, PT_AREG7	# restore a7
1131*fc4fb2adSChris Zankel	l32i	a0, a0, PT_AREG0	# restore a0
1132*fc4fb2adSChris Zankel	addi	a6, a6, 1		# restore a6 (really necessary?)
11335a0015d6SChris Zankel	rfe
11345a0015d6SChris Zankel
11355a0015d6SChris ZankelCATCH
1136*fc4fb2adSChris Zankel.Leac:	l32i	a7, a2, PT_AREG7	# restore a7
1137*fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1138*fc4fb2adSChris Zankel	movi	a2, -EFAULT
1139*fc4fb2adSChris Zankel	rfe
1140*fc4fb2adSChris Zankel
1141*fc4fb2adSChris Zankel.Lill:	l32i	a7, a2, PT_AREG0	# restore a7
1142*fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1143*fc4fb2adSChris Zankel	movi	a2, -EINVAL
1144*fc4fb2adSChris Zankel	rfe
1145*fc4fb2adSChris Zankel
11465a0015d6SChris Zankel
11475a0015d6SChris Zankel
11485a0015d6SChris Zankel
11495a0015d6SChris Zankel/* fast_syscall_spill_registers.
11505a0015d6SChris Zankel *
11515a0015d6SChris Zankel * Entry condition:
11525a0015d6SChris Zankel *
11535a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
11545a0015d6SChris Zankel *   a1:	a1
11555a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
11565a0015d6SChris Zankel *   a3:	dispatch table
11575a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
11585a0015d6SChris Zankel *   excsave_1:	a3
11595a0015d6SChris Zankel *
11605a0015d6SChris Zankel * Note: We assume the stack pointer is EXC_TABLE_KSTK in the fixup handler.
11615a0015d6SChris Zankel * Note: We don't need to save a2 in depc (return value)
11625a0015d6SChris Zankel */
11635a0015d6SChris Zankel
11645a0015d6SChris ZankelENTRY(fast_syscall_spill_registers)
11655a0015d6SChris Zankel
11665a0015d6SChris Zankel	/* Register a FIXUP handler (pass current wb as a parameter) */
11675a0015d6SChris Zankel
11685a0015d6SChris Zankel	movi	a0, fast_syscall_spill_registers_fixup
11695a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_FIXUP
11705a0015d6SChris Zankel	rsr	a0, WINDOWBASE
11715a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_PARAM
11725a0015d6SChris Zankel
11735a0015d6SChris Zankel	/* Save a3 and SAR on stack. */
11745a0015d6SChris Zankel
11755a0015d6SChris Zankel	rsr	a0, SAR
11765a0015d6SChris Zankel	xsr	a3, EXCSAVE_1		# restore a3 and excsave_1
11775a0015d6SChris Zankel	s32i	a0, a2, PT_AREG4	# store SAR to PT_AREG4
11785a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
11795a0015d6SChris Zankel
11805a0015d6SChris Zankel	/* The spill routine might clobber a7, a11, and a15. */
11815a0015d6SChris Zankel
11825a0015d6SChris Zankel	s32i	a7, a2, PT_AREG5
11835a0015d6SChris Zankel	s32i	a11, a2, PT_AREG6
11845a0015d6SChris Zankel	s32i	a15, a2, PT_AREG7
11855a0015d6SChris Zankel
11865a0015d6SChris Zankel	call0	_spill_registers	# destroys a3, DEPC, and SAR
11875a0015d6SChris Zankel
11885a0015d6SChris Zankel	/* Advance PC, restore registers and SAR, and return from exception. */
11895a0015d6SChris Zankel
11905a0015d6SChris Zankel	l32i	a3, a2, PT_AREG4
11915a0015d6SChris Zankel	l32i	a0, a2, PT_AREG0
11925a0015d6SChris Zankel	wsr	a3, SAR
11935a0015d6SChris Zankel	l32i	a3, a2, PT_AREG3
11945a0015d6SChris Zankel
11955a0015d6SChris Zankel	/* Restore clobbered registers. */
11965a0015d6SChris Zankel
11975a0015d6SChris Zankel	l32i	a7, a2, PT_AREG5
11985a0015d6SChris Zankel	l32i	a11, a2, PT_AREG6
11995a0015d6SChris Zankel	l32i	a15, a2, PT_AREG7
12005a0015d6SChris Zankel
12015a0015d6SChris Zankel	movi	a2, 0
12025a0015d6SChris Zankel	rfe
12035a0015d6SChris Zankel
12045a0015d6SChris Zankel/* Fixup handler.
12055a0015d6SChris Zankel *
12065a0015d6SChris Zankel * We get here if the spill routine causes an exception, e.g. tlb miss.
12075a0015d6SChris Zankel * We basically restore WINDOWBASE and WINDOWSTART to the condition when
12085a0015d6SChris Zankel * we entered the spill routine and jump to the user exception handler.
12095a0015d6SChris Zankel *
12105a0015d6SChris Zankel * a0: value of depc, original value in depc
12115a0015d6SChris Zankel * a2: trashed, original value in EXC_TABLE_DOUBLE_SAVE
12125a0015d6SChris Zankel * a3: exctable, original value in excsave1
12135a0015d6SChris Zankel */
12145a0015d6SChris Zankel
12155a0015d6SChris Zankelfast_syscall_spill_registers_fixup:
12165a0015d6SChris Zankel
12175a0015d6SChris Zankel	rsr	a2, WINDOWBASE	# get current windowbase (a2 is saved)
12185a0015d6SChris Zankel	xsr	a0, DEPC	# restore depc and a0
12195a0015d6SChris Zankel	ssl	a2		# set shift (32 - WB)
12205a0015d6SChris Zankel
12215a0015d6SChris Zankel	/* We need to make sure the current registers (a0-a3) are preserved.
12225a0015d6SChris Zankel	 * To do this, we simply set the bit for the current window frame
12235a0015d6SChris Zankel	 * in WS, so that the exception handlers save them to the task stack.
12245a0015d6SChris Zankel	 */
12255a0015d6SChris Zankel
12265a0015d6SChris Zankel	rsr	a3, EXCSAVE_1	# get spill-mask
12275a0015d6SChris Zankel	slli	a2, a3, 1	# shift left by one
12285a0015d6SChris Zankel
12295a0015d6SChris Zankel	slli	a3, a2, 32-WSBITS
12305a0015d6SChris Zankel	src	a2, a2, a3	# a1 = xxwww1yyxxxwww1yy......
12315a0015d6SChris Zankel	wsr	a2, WINDOWSTART	# set corrected windowstart
12325a0015d6SChris Zankel
12335a0015d6SChris Zankel	movi	a3, exc_table
12345a0015d6SChris Zankel	l32i	a2, a3, EXC_TABLE_DOUBLE_SAVE	# restore a2
12355a0015d6SChris Zankel	l32i	a3, a3, EXC_TABLE_PARAM	# original WB (in user task)
12365a0015d6SChris Zankel
12375a0015d6SChris Zankel	/* Return to the original (user task) WINDOWBASE.
12385a0015d6SChris Zankel	 * We leave the following frame behind:
12395a0015d6SChris Zankel	 * a0, a1, a2	same
12405a0015d6SChris Zankel	 * a3:		trashed (saved in excsave_1)
12415a0015d6SChris Zankel	 * depc:	depc (we have to return to that address)
12425a0015d6SChris Zankel	 * excsave_1:	a3
12435a0015d6SChris Zankel	 */
12445a0015d6SChris Zankel
12455a0015d6SChris Zankel	wsr	a3, WINDOWBASE
12465a0015d6SChris Zankel	rsync
12475a0015d6SChris Zankel
12485a0015d6SChris Zankel	/* We are now in the original frame when we entered _spill_registers:
12495a0015d6SChris Zankel	 *  a0: return address
12505a0015d6SChris Zankel	 *  a1: used, stack pointer
12515a0015d6SChris Zankel	 *  a2: kernel stack pointer
12525a0015d6SChris Zankel	 *  a3: available, saved in EXCSAVE_1
12535a0015d6SChris Zankel	 *  depc: exception address
12545a0015d6SChris Zankel	 *  excsave: a3
12555a0015d6SChris Zankel	 * Note: This frame might be the same as above.
12565a0015d6SChris Zankel	 */
12575a0015d6SChris Zankel
12585a0015d6SChris Zankel#ifdef SIGNAL_HANDLING_IN_DOUBLE_EXCEPTION
12595a0015d6SChris Zankel	/* Restore registers we precautiously saved.
12605a0015d6SChris Zankel	 * We have the value of the 'right' a3
12615a0015d6SChris Zankel	 */
12625a0015d6SChris Zankel
12635a0015d6SChris Zankel	l32i	a7, a2, PT_AREG5
12645a0015d6SChris Zankel	l32i	a11, a2, PT_AREG6
12655a0015d6SChris Zankel	l32i	a15, a2, PT_AREG7
12665a0015d6SChris Zankel#endif
12675a0015d6SChris Zankel
12685a0015d6SChris Zankel	/* Setup stack pointer. */
12695a0015d6SChris Zankel
12705a0015d6SChris Zankel	addi	a2, a2, -PT_USER_SIZE
12715a0015d6SChris Zankel	s32i	a0, a2, PT_AREG0
12725a0015d6SChris Zankel
12735a0015d6SChris Zankel	/* Make sure we return to this fixup handler. */
12745a0015d6SChris Zankel
12755a0015d6SChris Zankel	movi	a3, fast_syscall_spill_registers_fixup_return
12765a0015d6SChris Zankel	s32i	a3, a2, PT_DEPC		# setup depc
12775a0015d6SChris Zankel
12785a0015d6SChris Zankel	/* Jump to the exception handler. */
12795a0015d6SChris Zankel
12805a0015d6SChris Zankel	movi	a3, exc_table
12815a0015d6SChris Zankel	rsr	a0, EXCCAUSE
12825a0015d6SChris Zankel        addx4   a0, a0, a3              	# find entry in table
12835a0015d6SChris Zankel        l32i    a0, a0, EXC_TABLE_FAST_USER     # load handler
12845a0015d6SChris Zankel        jx      a0
12855a0015d6SChris Zankel
12865a0015d6SChris Zankelfast_syscall_spill_registers_fixup_return:
12875a0015d6SChris Zankel
12885a0015d6SChris Zankel	/* When we return here, all registers have been restored (a2: DEPC) */
12895a0015d6SChris Zankel
12905a0015d6SChris Zankel	wsr	a2, DEPC		# exception address
12915a0015d6SChris Zankel
12925a0015d6SChris Zankel	/* Restore fixup handler. */
12935a0015d6SChris Zankel
12945a0015d6SChris Zankel	xsr	a3, EXCSAVE_1
12955a0015d6SChris Zankel	movi	a2, fast_syscall_spill_registers_fixup
12965a0015d6SChris Zankel	s32i	a2, a3, EXC_TABLE_FIXUP
12975a0015d6SChris Zankel	rsr	a2, WINDOWBASE
12985a0015d6SChris Zankel	s32i	a2, a3, EXC_TABLE_PARAM
12995a0015d6SChris Zankel	l32i	a2, a3, EXC_TABLE_KSTK
13005a0015d6SChris Zankel
13015a0015d6SChris Zankel#ifdef SIGNAL_HANDLING_IN_DOUBLE_EXCEPTION
13025a0015d6SChris Zankel	/* Save registers again that might be clobbered. */
13035a0015d6SChris Zankel
13045a0015d6SChris Zankel	s32i	a7, a2, PT_AREG5
13055a0015d6SChris Zankel	s32i	a11, a2, PT_AREG6
13065a0015d6SChris Zankel	s32i	a15, a2, PT_AREG7
13075a0015d6SChris Zankel#endif
13085a0015d6SChris Zankel
13095a0015d6SChris Zankel	/* Load WB at the time the exception occurred. */
13105a0015d6SChris Zankel
13115a0015d6SChris Zankel	rsr	a3, SAR			# WB is still in SAR
13125a0015d6SChris Zankel	neg	a3, a3
13135a0015d6SChris Zankel	wsr	a3, WINDOWBASE
13145a0015d6SChris Zankel	rsync
13155a0015d6SChris Zankel
13165a0015d6SChris Zankel	/* Restore a3 and return. */
13175a0015d6SChris Zankel
13185a0015d6SChris Zankel	movi	a3, exc_table
13195a0015d6SChris Zankel	xsr	a3, EXCSAVE_1
13205a0015d6SChris Zankel
13215a0015d6SChris Zankel	rfde
13225a0015d6SChris Zankel
13235a0015d6SChris Zankel
13245a0015d6SChris Zankel/*
13255a0015d6SChris Zankel * spill all registers.
13265a0015d6SChris Zankel *
13275a0015d6SChris Zankel * This is not a real function. The following conditions must be met:
13285a0015d6SChris Zankel *
13295a0015d6SChris Zankel *  - must be called with call0.
13305a0015d6SChris Zankel *  - uses DEPC, a3 and SAR.
13315a0015d6SChris Zankel *  - the last 'valid' register of each frame are clobbered.
13325a0015d6SChris Zankel *  - the caller must have registered a fixup handler
13335a0015d6SChris Zankel *    (or be inside a critical section)
13345a0015d6SChris Zankel *  - PS_EXCM must be set (PS_WOE cleared?)
13355a0015d6SChris Zankel */
13365a0015d6SChris Zankel
13375a0015d6SChris ZankelENTRY(_spill_registers)
13385a0015d6SChris Zankel
13395a0015d6SChris Zankel	/*
13405a0015d6SChris Zankel	 * Rotate ws so that the current windowbase is at bit 0.
13415a0015d6SChris Zankel	 * Assume ws = xxxwww1yy (www1 current window frame).
13425a0015d6SChris Zankel	 * Rotate ws right so that a2 = yyxxxwww1.
13435a0015d6SChris Zankel	 */
13445a0015d6SChris Zankel
13455a0015d6SChris Zankel	wsr	a2, DEPC		# preserve a2
13465a0015d6SChris Zankel	rsr	a2, WINDOWBASE
13475a0015d6SChris Zankel	rsr	a3, WINDOWSTART
13485a0015d6SChris Zankel	ssr	a2			# holds WB
13495a0015d6SChris Zankel	slli	a2, a3, WSBITS
13505a0015d6SChris Zankel	or	a3, a3, a2		# a2 = xxxwww1yyxxxwww1yy
13515a0015d6SChris Zankel	srl	a3, a3
13525a0015d6SChris Zankel
13535a0015d6SChris Zankel	/* We are done if there are no more than the current register frame. */
13545a0015d6SChris Zankel
13555a0015d6SChris Zankel	extui	a3, a3, 1, WSBITS-2	# a3 = 0yyxxxwww
13565a0015d6SChris Zankel	movi	a2, (1 << (WSBITS-1))
13575a0015d6SChris Zankel	_beqz	a3, .Lnospill		# only one active frame? jump
13585a0015d6SChris Zankel
13595a0015d6SChris Zankel	/* We want 1 at the top, so that we return to the current windowbase */
13605a0015d6SChris Zankel
13615a0015d6SChris Zankel	or	a3, a3, a2		# 1yyxxxwww
13625a0015d6SChris Zankel
13635a0015d6SChris Zankel	/* Skip empty frames - get 'oldest' WINDOWSTART-bit. */
13645a0015d6SChris Zankel
13655a0015d6SChris Zankel	wsr	a3, WINDOWSTART		# save shifted windowstart
13665a0015d6SChris Zankel	neg	a2, a3
13675a0015d6SChris Zankel	and	a3, a2, a3		# first bit set from right: 000010000
13685a0015d6SChris Zankel
13695a0015d6SChris Zankel	ffs_ws	a2, a3			# a2: shifts to skip empty frames
13705a0015d6SChris Zankel	movi	a3, WSBITS
13715a0015d6SChris Zankel	sub	a2, a3, a2		# WSBITS-a2:number of 0-bits from right
13725a0015d6SChris Zankel	ssr	a2			# save in SAR for later.
13735a0015d6SChris Zankel
13745a0015d6SChris Zankel	rsr	a3, WINDOWBASE
13755a0015d6SChris Zankel	add	a3, a3, a2
13765a0015d6SChris Zankel	rsr	a2, DEPC		# restore a2
13775a0015d6SChris Zankel	wsr	a3, WINDOWBASE
13785a0015d6SChris Zankel	rsync
13795a0015d6SChris Zankel
13805a0015d6SChris Zankel	rsr	a3, WINDOWSTART
13815a0015d6SChris Zankel	srl	a3, a3			# shift windowstart
13825a0015d6SChris Zankel
13835a0015d6SChris Zankel	/* WB is now just one frame below the oldest frame in the register
13845a0015d6SChris Zankel	   window. WS is shifted so the oldest frame is in bit 0, thus, WB
13855a0015d6SChris Zankel	   and WS differ by one 4-register frame. */
13865a0015d6SChris Zankel
13875a0015d6SChris Zankel	/* Save frames. Depending what call was used (call4, call8, call12),
13885a0015d6SChris Zankel	 * we have to save 4,8. or 12 registers.
13895a0015d6SChris Zankel	 */
13905a0015d6SChris Zankel
13915a0015d6SChris Zankel	_bbsi.l	a3, 1, .Lc4
13925a0015d6SChris Zankel	_bbsi.l	a3, 2, .Lc8
13935a0015d6SChris Zankel
13945a0015d6SChris Zankel	/* Special case: we have a call12-frame starting at a4. */
13955a0015d6SChris Zankel
13965a0015d6SChris Zankel	_bbci.l	a3, 3, .Lc12	# bit 3 shouldn't be zero! (Jump to Lc12 first)
13975a0015d6SChris Zankel
13985a0015d6SChris Zankel	s32e	a4, a1, -16	# a1 is valid with an empty spill area
13995a0015d6SChris Zankel	l32e	a4, a5, -12
14005a0015d6SChris Zankel	s32e	a8, a4, -48
14015a0015d6SChris Zankel	mov	a8, a4
14025a0015d6SChris Zankel	l32e	a4, a1, -16
14035a0015d6SChris Zankel	j	.Lc12c
14045a0015d6SChris Zankel
14055a0015d6SChris Zankel.Lloop: _bbsi.l	a3, 1, .Lc4
14065a0015d6SChris Zankel	_bbci.l	a3, 2, .Lc12
14075a0015d6SChris Zankel
14085a0015d6SChris Zankel.Lc8:	s32e	a4, a13, -16
14095a0015d6SChris Zankel	l32e	a4, a5, -12
14105a0015d6SChris Zankel	s32e	a8, a4, -32
14115a0015d6SChris Zankel	s32e	a5, a13, -12
14125a0015d6SChris Zankel	s32e	a6, a13, -8
14135a0015d6SChris Zankel	s32e	a7, a13, -4
14145a0015d6SChris Zankel	s32e	a9, a4, -28
14155a0015d6SChris Zankel	s32e	a10, a4, -24
14165a0015d6SChris Zankel	s32e	a11, a4, -20
14175a0015d6SChris Zankel
14185a0015d6SChris Zankel	srli	a11, a3, 2		# shift windowbase by 2
14195a0015d6SChris Zankel	rotw	2
14205a0015d6SChris Zankel	_bnei	a3, 1, .Lloop
14215a0015d6SChris Zankel
14225a0015d6SChris Zankel.Lexit: /* Done. Do the final rotation, set WS, and return. */
14235a0015d6SChris Zankel
14245a0015d6SChris Zankel	rotw	1
14255a0015d6SChris Zankel	rsr	a3, WINDOWBASE
14265a0015d6SChris Zankel	ssl	a3
14275a0015d6SChris Zankel	movi	a3, 1
14285a0015d6SChris Zankel	sll	a3, a3
14295a0015d6SChris Zankel	wsr	a3, WINDOWSTART
14305a0015d6SChris Zankel
14315a0015d6SChris Zankel.Lnospill:
14325a0015d6SChris Zankel	jx	a0
14335a0015d6SChris Zankel
14345a0015d6SChris Zankel.Lc4:	s32e	a4, a9, -16
14355a0015d6SChris Zankel	s32e	a5, a9, -12
14365a0015d6SChris Zankel	s32e	a6, a9, -8
14375a0015d6SChris Zankel	s32e	a7, a9, -4
14385a0015d6SChris Zankel
14395a0015d6SChris Zankel	srli	a7, a3, 1
14405a0015d6SChris Zankel	rotw	1
14415a0015d6SChris Zankel	_bnei	a3, 1, .Lloop
14425a0015d6SChris Zankel	j	.Lexit
14435a0015d6SChris Zankel
14445a0015d6SChris Zankel.Lc12:	_bbci.l	a3, 3, .Linvalid_mask	# bit 2 shouldn't be zero!
14455a0015d6SChris Zankel
14465a0015d6SChris Zankel	/* 12-register frame (call12) */
14475a0015d6SChris Zankel
14485a0015d6SChris Zankel	l32e	a2, a5, -12
14495a0015d6SChris Zankel	s32e	a8, a2, -48
14505a0015d6SChris Zankel	mov	a8, a2
14515a0015d6SChris Zankel
14525a0015d6SChris Zankel.Lc12c: s32e	a9, a8, -44
14535a0015d6SChris Zankel	s32e	a10, a8, -40
14545a0015d6SChris Zankel	s32e	a11, a8, -36
14555a0015d6SChris Zankel	s32e	a12, a8, -32
14565a0015d6SChris Zankel	s32e	a13, a8, -28
14575a0015d6SChris Zankel	s32e	a14, a8, -24
14585a0015d6SChris Zankel	s32e	a15, a8, -20
14595a0015d6SChris Zankel	srli	a15, a3, 3
14605a0015d6SChris Zankel
14615a0015d6SChris Zankel	/* The stack pointer for a4..a7 is out of reach, so we rotate the
14625a0015d6SChris Zankel	 * window, grab the stackpointer, and rotate back.
14635a0015d6SChris Zankel	 * Alternatively, we could also use the following approach, but that
14645a0015d6SChris Zankel	 * makes the fixup routine much more complicated:
14655a0015d6SChris Zankel	 * rotw	1
14665a0015d6SChris Zankel	 * s32e	a0, a13, -16
14675a0015d6SChris Zankel	 * ...
14685a0015d6SChris Zankel	 * rotw 2
14695a0015d6SChris Zankel	 */
14705a0015d6SChris Zankel
14715a0015d6SChris Zankel	rotw	1
14725a0015d6SChris Zankel	mov	a5, a13
14735a0015d6SChris Zankel	rotw	-1
14745a0015d6SChris Zankel
14755a0015d6SChris Zankel	s32e	a4, a9, -16
14765a0015d6SChris Zankel	s32e	a5, a9, -12
14775a0015d6SChris Zankel	s32e	a6, a9, -8
14785a0015d6SChris Zankel	s32e	a7, a9, -4
14795a0015d6SChris Zankel
14805a0015d6SChris Zankel	rotw	3
14815a0015d6SChris Zankel
14825a0015d6SChris Zankel	_beqi	a3, 1, .Lexit
14835a0015d6SChris Zankel	j	.Lloop
14845a0015d6SChris Zankel
14855a0015d6SChris Zankel.Linvalid_mask:
14865a0015d6SChris Zankel
14875a0015d6SChris Zankel	/* We get here because of an unrecoverable error in the window
14885a0015d6SChris Zankel	 * registers. If we are in user space, we kill the application,
14895a0015d6SChris Zankel	 * however, this condition is unrecoverable in kernel space.
14905a0015d6SChris Zankel	 */
14915a0015d6SChris Zankel
14925a0015d6SChris Zankel	rsr	a0, PS
1493173d6681SChris Zankel	_bbci.l	a0, PS_UM_BIT, 1f
14945a0015d6SChris Zankel
14955a0015d6SChris Zankel 	/* User space: Setup a dummy frame and kill application.
14965a0015d6SChris Zankel	 * Note: We assume EXC_TABLE_KSTK contains a valid stack pointer.
14975a0015d6SChris Zankel	 */
14985a0015d6SChris Zankel
14995a0015d6SChris Zankel	movi	a0, 1
15005a0015d6SChris Zankel	movi	a1, 0
15015a0015d6SChris Zankel
15025a0015d6SChris Zankel	wsr	a0, WINDOWSTART
15035a0015d6SChris Zankel	wsr	a1, WINDOWBASE
15045a0015d6SChris Zankel	rsync
15055a0015d6SChris Zankel
15065a0015d6SChris Zankel	movi	a0, 0
15075a0015d6SChris Zankel
15085a0015d6SChris Zankel	movi	a3, exc_table
15095a0015d6SChris Zankel	l32i	a1, a3, EXC_TABLE_KSTK
15105a0015d6SChris Zankel	wsr	a3, EXCSAVE_1
15115a0015d6SChris Zankel
1512173d6681SChris Zankel	movi	a4, (1 << PS_WOE_BIT) | 1
15135a0015d6SChris Zankel	wsr	a4, PS
15145a0015d6SChris Zankel	rsync
15155a0015d6SChris Zankel
15165a0015d6SChris Zankel	movi	a6, SIGSEGV
15175a0015d6SChris Zankel	movi	a4, do_exit
15185a0015d6SChris Zankel	callx4	a4
15195a0015d6SChris Zankel
15205a0015d6SChris Zankel1:	/* Kernel space: PANIC! */
15215a0015d6SChris Zankel
15225a0015d6SChris Zankel	wsr	a0, EXCSAVE_1
15235a0015d6SChris Zankel	movi	a0, unrecoverable_exception
15245a0015d6SChris Zankel	callx0	a0		# should not return
15255a0015d6SChris Zankel1:	j	1b
15265a0015d6SChris Zankel
15275a0015d6SChris Zankel/*
15285a0015d6SChris Zankel * We should never get here. Bail out!
15295a0015d6SChris Zankel */
15305a0015d6SChris Zankel
15315a0015d6SChris ZankelENTRY(fast_second_level_miss_double_kernel)
15325a0015d6SChris Zankel
15335a0015d6SChris Zankel1:	movi	a0, unrecoverable_exception
15345a0015d6SChris Zankel	callx0	a0		# should not return
15355a0015d6SChris Zankel1:	j	1b
15365a0015d6SChris Zankel
15375a0015d6SChris Zankel/* First-level entry handler for user, kernel, and double 2nd-level
15385a0015d6SChris Zankel * TLB miss exceptions.  Note that for now, user and kernel miss
15395a0015d6SChris Zankel * exceptions share the same entry point and are handled identically.
15405a0015d6SChris Zankel *
15415a0015d6SChris Zankel * An old, less-efficient C version of this function used to exist.
15425a0015d6SChris Zankel * We include it below, interleaved as comments, for reference.
15435a0015d6SChris Zankel *
15445a0015d6SChris Zankel * Entry condition:
15455a0015d6SChris Zankel *
15465a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
15475a0015d6SChris Zankel *   a1:	a1
15485a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
15495a0015d6SChris Zankel *   a3:	dispatch table
15505a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
15515a0015d6SChris Zankel *   excsave_1:	a3
15525a0015d6SChris Zankel *
15535a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
15545a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
15555a0015d6SChris Zankel */
15565a0015d6SChris Zankel
15575a0015d6SChris ZankelENTRY(fast_second_level_miss)
15585a0015d6SChris Zankel
15595a0015d6SChris Zankel	/* Save a1. Note: we don't expect a double exception. */
15605a0015d6SChris Zankel
15615a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
15625a0015d6SChris Zankel
15635a0015d6SChris Zankel	/* We need to map the page of PTEs for the user task.  Find
15645a0015d6SChris Zankel	 * the pointer to that page.  Also, it's possible for tsk->mm
15655a0015d6SChris Zankel	 * to be NULL while tsk->active_mm is nonzero if we faulted on
15665a0015d6SChris Zankel	 * a vmalloc address.  In that rare case, we must use
15675a0015d6SChris Zankel	 * active_mm instead to avoid a fault in this handler.  See
15685a0015d6SChris Zankel	 *
15695a0015d6SChris Zankel	 * http://mail.nl.linux.org/linux-mm/2002-08/msg00258.html
15705a0015d6SChris Zankel	 *   (or search Internet on "mm vs. active_mm")
15715a0015d6SChris Zankel	 *
15725a0015d6SChris Zankel	 *	if (!mm)
15735a0015d6SChris Zankel	 *		mm = tsk->active_mm;
15745a0015d6SChris Zankel	 *	pgd = pgd_offset (mm, regs->excvaddr);
15755a0015d6SChris Zankel	 *	pmd = pmd_offset (pgd, regs->excvaddr);
15765a0015d6SChris Zankel	 *	pmdval = *pmd;
15775a0015d6SChris Zankel	 */
15785a0015d6SChris Zankel
15795a0015d6SChris Zankel	GET_CURRENT(a1,a2)
15805a0015d6SChris Zankel	l32i	a0, a1, TASK_MM		# tsk->mm
15815a0015d6SChris Zankel	beqz	a0, 9f
15825a0015d6SChris Zankel
15835a0015d6SChris Zankel8:	rsr	a1, EXCVADDR		# fault address
15845a0015d6SChris Zankel	_PGD_OFFSET(a0, a1, a1)
15855a0015d6SChris Zankel	l32i	a0, a0, 0		# read pmdval
15865a0015d6SChris Zankel	//beqi	a0, _PAGE_USER, 2f
15875a0015d6SChris Zankel	beqz	a0, 2f
15885a0015d6SChris Zankel
15895a0015d6SChris Zankel	/* Read ptevaddr and convert to top of page-table page.
15905a0015d6SChris Zankel	 *
15915a0015d6SChris Zankel	 * 	vpnval = read_ptevaddr_register() & PAGE_MASK;
15925a0015d6SChris Zankel	 * 	vpnval += DTLB_WAY_PGTABLE;
15935a0015d6SChris Zankel	 *	pteval = mk_pte (virt_to_page(pmd_val(pmdval)), PAGE_KERNEL);
15945a0015d6SChris Zankel	 *	write_dtlb_entry (pteval, vpnval);
15955a0015d6SChris Zankel	 *
15965a0015d6SChris Zankel	 * The messy computation for 'pteval' above really simplifies
15975a0015d6SChris Zankel	 * into the following:
15985a0015d6SChris Zankel	 *
15995a0015d6SChris Zankel	 * pteval = ((pmdval - PAGE_OFFSET) & PAGE_MASK) | PAGE_KERNEL
16005a0015d6SChris Zankel	 */
16015a0015d6SChris Zankel
16025a0015d6SChris Zankel	movi	a1, -PAGE_OFFSET
16035a0015d6SChris Zankel	add	a0, a0, a1		# pmdval - PAGE_OFFSET
16045a0015d6SChris Zankel	extui	a1, a0, 0, PAGE_SHIFT	# ... & PAGE_MASK
16055a0015d6SChris Zankel	xor	a0, a0, a1
16065a0015d6SChris Zankel
16075a0015d6SChris Zankel
16085a0015d6SChris Zankel	movi	a1, PAGE_DIRECTORY
16095a0015d6SChris Zankel	or	a0, a0, a1		# ... | PAGE_DIRECTORY
16105a0015d6SChris Zankel
16115a0015d6SChris Zankel	rsr	a1, PTEVADDR
16125a0015d6SChris Zankel	srli	a1, a1, PAGE_SHIFT
16135a0015d6SChris Zankel	slli	a1, a1, PAGE_SHIFT	# ptevaddr & PAGE_MASK
1614173d6681SChris Zankel	addi	a1, a1, DTLB_WAY_PGD	# ... + way_number
16155a0015d6SChris Zankel
16165a0015d6SChris Zankel	wdtlb	a0, a1
16175a0015d6SChris Zankel	dsync
16185a0015d6SChris Zankel
16195a0015d6SChris Zankel	/* Exit critical section. */
16205a0015d6SChris Zankel
16215a0015d6SChris Zankel	movi	a0, 0
16225a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_FIXUP
16235a0015d6SChris Zankel
16245a0015d6SChris Zankel	/* Restore the working registers, and return. */
16255a0015d6SChris Zankel
16265a0015d6SChris Zankel	l32i	a0, a2, PT_AREG0
16275a0015d6SChris Zankel	l32i	a1, a2, PT_AREG1
16285a0015d6SChris Zankel	l32i	a2, a2, PT_DEPC
16295a0015d6SChris Zankel	xsr	a3, EXCSAVE_1
16305a0015d6SChris Zankel
16315a0015d6SChris Zankel	bgeui	a2, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
16325a0015d6SChris Zankel
16335a0015d6SChris Zankel	/* Restore excsave1 and return. */
16345a0015d6SChris Zankel
16355a0015d6SChris Zankel	rsr	a2, DEPC
16365a0015d6SChris Zankel	rfe
16375a0015d6SChris Zankel
16385a0015d6SChris Zankel	/* Return from double exception. */
16395a0015d6SChris Zankel
16405a0015d6SChris Zankel1:	xsr	a2, DEPC
16415a0015d6SChris Zankel	esync
16425a0015d6SChris Zankel	rfde
16435a0015d6SChris Zankel
16445a0015d6SChris Zankel9:	l32i	a0, a1, TASK_ACTIVE_MM	# unlikely case mm == 0
16455a0015d6SChris Zankel	j	8b
16465a0015d6SChris Zankel
16475a0015d6SChris Zankel2:	/* Invalid PGD, default exception handling */
16485a0015d6SChris Zankel
16495a0015d6SChris Zankel	rsr	a1, DEPC
16505a0015d6SChris Zankel	xsr	a3, EXCSAVE_1
16515a0015d6SChris Zankel	s32i	a1, a2, PT_AREG2
16525a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
16535a0015d6SChris Zankel	mov	a1, a2
16545a0015d6SChris Zankel
16555a0015d6SChris Zankel	rsr	a2, PS
1656173d6681SChris Zankel	bbsi.l	a2, PS_UM_BIT, 1f
16575a0015d6SChris Zankel	j	_kernel_exception
16585a0015d6SChris Zankel1:	j	_user_exception
16595a0015d6SChris Zankel
16605a0015d6SChris Zankel
16615a0015d6SChris Zankel/*
16625a0015d6SChris Zankel * StoreProhibitedException
16635a0015d6SChris Zankel *
16645a0015d6SChris Zankel * Update the pte and invalidate the itlb mapping for this pte.
16655a0015d6SChris Zankel *
16665a0015d6SChris Zankel * Entry condition:
16675a0015d6SChris Zankel *
16685a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
16695a0015d6SChris Zankel *   a1:	a1
16705a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
16715a0015d6SChris Zankel *   a3:	dispatch table
16725a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
16735a0015d6SChris Zankel *   excsave_1:	a3
16745a0015d6SChris Zankel *
16755a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
16765a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
16775a0015d6SChris Zankel */
16785a0015d6SChris Zankel
16795a0015d6SChris ZankelENTRY(fast_store_prohibited)
16805a0015d6SChris Zankel
16815a0015d6SChris Zankel	/* Save a1 and a4. */
16825a0015d6SChris Zankel
16835a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
16845a0015d6SChris Zankel	s32i	a4, a2, PT_AREG4
16855a0015d6SChris Zankel
16865a0015d6SChris Zankel	GET_CURRENT(a1,a2)
16875a0015d6SChris Zankel	l32i	a0, a1, TASK_MM		# tsk->mm
16885a0015d6SChris Zankel	beqz	a0, 9f
16895a0015d6SChris Zankel
16905a0015d6SChris Zankel8:	rsr	a1, EXCVADDR		# fault address
16915a0015d6SChris Zankel	_PGD_OFFSET(a0, a1, a4)
16925a0015d6SChris Zankel	l32i	a0, a0, 0
16935a0015d6SChris Zankel	//beqi	a0, _PAGE_USER, 2f	# FIXME use _PAGE_INVALID
16945a0015d6SChris Zankel	beqz	a0, 2f
16955a0015d6SChris Zankel
16965a0015d6SChris Zankel	_PTE_OFFSET(a0, a1, a4)
16975a0015d6SChris Zankel	l32i	a4, a0, 0		# read pteval
16985a0015d6SChris Zankel	movi	a1, _PAGE_VALID | _PAGE_RW
16995a0015d6SChris Zankel	bnall	a4, a1, 2f
17005a0015d6SChris Zankel
17015a0015d6SChris Zankel	movi	a1, _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_WRENABLE
17025a0015d6SChris Zankel	or	a4, a4, a1
17035a0015d6SChris Zankel	rsr	a1, EXCVADDR
17045a0015d6SChris Zankel	s32i	a4, a0, 0
17055a0015d6SChris Zankel
17065a0015d6SChris Zankel	/* We need to flush the cache if we have page coloring. */
17075a0015d6SChris Zankel#if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
17085a0015d6SChris Zankel	dhwb	a0, 0
17095a0015d6SChris Zankel#endif
17105a0015d6SChris Zankel	pdtlb	a0, a1
17115a0015d6SChris Zankel	beqz	a0, 1f
17125a0015d6SChris Zankel	idtlb	a0		// FIXME do we need this?
17135a0015d6SChris Zankel	wdtlb	a4, a0
17145a0015d6SChris Zankel1:
17155a0015d6SChris Zankel
17165a0015d6SChris Zankel	/* Exit critical section. */
17175a0015d6SChris Zankel
17185a0015d6SChris Zankel	movi	a0, 0
17195a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_FIXUP
17205a0015d6SChris Zankel
17215a0015d6SChris Zankel	/* Restore the working registers, and return. */
17225a0015d6SChris Zankel
17235a0015d6SChris Zankel	l32i	a4, a2, PT_AREG4
17245a0015d6SChris Zankel	l32i	a1, a2, PT_AREG1
17255a0015d6SChris Zankel	l32i	a0, a2, PT_AREG0
17265a0015d6SChris Zankel	l32i	a2, a2, PT_DEPC
17275a0015d6SChris Zankel
17285a0015d6SChris Zankel	/* Restore excsave1 and a3. */
17295a0015d6SChris Zankel
17305a0015d6SChris Zankel	xsr	a3, EXCSAVE_1
17315a0015d6SChris Zankel	bgeui	a2, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
17325a0015d6SChris Zankel
17335a0015d6SChris Zankel	rsr	a2, DEPC
17345a0015d6SChris Zankel	rfe
17355a0015d6SChris Zankel
17365a0015d6SChris Zankel	/* Double exception. Restore FIXUP handler and return. */
17375a0015d6SChris Zankel
17385a0015d6SChris Zankel1:	xsr	a2, DEPC
17395a0015d6SChris Zankel	esync
17405a0015d6SChris Zankel	rfde
17415a0015d6SChris Zankel
17425a0015d6SChris Zankel9:	l32i	a0, a1, TASK_ACTIVE_MM	# unlikely case mm == 0
17435a0015d6SChris Zankel	j	8b
17445a0015d6SChris Zankel
17455a0015d6SChris Zankel2:	/* If there was a problem, handle fault in C */
17465a0015d6SChris Zankel
17475a0015d6SChris Zankel	rsr	a4, DEPC	# still holds a2
17485a0015d6SChris Zankel	xsr	a3, EXCSAVE_1
17495a0015d6SChris Zankel	s32i	a4, a2, PT_AREG2
17505a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
17515a0015d6SChris Zankel	l32i	a4, a2, PT_AREG4
17525a0015d6SChris Zankel	mov	a1, a2
17535a0015d6SChris Zankel
17545a0015d6SChris Zankel	rsr	a2, PS
1755173d6681SChris Zankel	bbsi.l	a2, PS_UM_BIT, 1f
17565a0015d6SChris Zankel	j	_kernel_exception
17575a0015d6SChris Zankel1:	j	_user_exception
17585a0015d6SChris Zankel
17595a0015d6SChris Zankel
17605a0015d6SChris Zankel#if XCHAL_EXTRA_SA_SIZE
17615a0015d6SChris Zankel
17625a0015d6SChris Zankel#warning fast_coprocessor untested
17635a0015d6SChris Zankel
17645a0015d6SChris Zankel/*
17655a0015d6SChris Zankel * Entry condition:
17665a0015d6SChris Zankel *
17675a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
17685a0015d6SChris Zankel *   a1:	a1
17695a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
17705a0015d6SChris Zankel *   a3:	dispatch table
17715a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
17725a0015d6SChris Zankel *   excsave_1:	a3
17735a0015d6SChris Zankel *
17745a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
17755a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
17765a0015d6SChris Zankel */
17775a0015d6SChris Zankel
17785a0015d6SChris ZankelENTRY(fast_coprocessor_double)
17795a0015d6SChris Zankel	wsr	a0, EXCSAVE_1
17805a0015d6SChris Zankel	movi	a0, unrecoverable_exception
17815a0015d6SChris Zankel	callx0	a0
17825a0015d6SChris Zankel
17835a0015d6SChris ZankelENTRY(fast_coprocessor)
17845a0015d6SChris Zankel
17855a0015d6SChris Zankel	/* Fatal if we are in a double exception. */
17865a0015d6SChris Zankel
17875a0015d6SChris Zankel	l32i	a0, a2, PT_DEPC
17885a0015d6SChris Zankel	_bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, fast_coprocessor_double
17895a0015d6SChris Zankel
17905a0015d6SChris Zankel	/* Save some registers a1, a3, a4, SAR */
17915a0015d6SChris Zankel
17925a0015d6SChris Zankel	xsr	a3, EXCSAVE_1
17935a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
17945a0015d6SChris Zankel	rsr	a3, SAR
17955a0015d6SChris Zankel	s32i	a4, a2, PT_AREG4
17965a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
17975a0015d6SChris Zankel	s32i	a5, a1, PT_AREG5
17985a0015d6SChris Zankel	s32i	a3, a2, PT_SAR
17995a0015d6SChris Zankel	mov	a1, a2
18005a0015d6SChris Zankel
18015a0015d6SChris Zankel	/* Currently, the HAL macros only guarantee saving a0 and a1.
18025a0015d6SChris Zankel	 * These can and will be refined in the future, but for now,
18035a0015d6SChris Zankel	 * just save the remaining registers of a2...a15.
18045a0015d6SChris Zankel	 */
18055a0015d6SChris Zankel	s32i	a6, a1, PT_AREG6
18065a0015d6SChris Zankel	s32i	a7, a1, PT_AREG7
18075a0015d6SChris Zankel	s32i	a8, a1, PT_AREG8
18085a0015d6SChris Zankel	s32i	a9, a1, PT_AREG9
18095a0015d6SChris Zankel	s32i	a10, a1, PT_AREG10
18105a0015d6SChris Zankel	s32i	a11, a1, PT_AREG11
18115a0015d6SChris Zankel	s32i	a12, a1, PT_AREG12
18125a0015d6SChris Zankel	s32i	a13, a1, PT_AREG13
18135a0015d6SChris Zankel	s32i	a14, a1, PT_AREG14
18145a0015d6SChris Zankel	s32i	a15, a1, PT_AREG15
18155a0015d6SChris Zankel
18165a0015d6SChris Zankel	/* Find coprocessor number. Subtract first CP EXCCAUSE from EXCCAUSE */
18175a0015d6SChris Zankel
18185a0015d6SChris Zankel	rsr	a0, EXCCAUSE
18195a0015d6SChris Zankel	addi	a3, a0, -XCHAL_EXCCAUSE_COPROCESSOR0_DISABLED
18205a0015d6SChris Zankel
18215a0015d6SChris Zankel	/* Set corresponding CPENABLE bit */
18225a0015d6SChris Zankel
18235a0015d6SChris Zankel	movi	a4, 1
18245a0015d6SChris Zankel	ssl	a3			# SAR: 32 - coprocessor_number
18255a0015d6SChris Zankel	rsr	a5, CPENABLE
18265a0015d6SChris Zankel	sll	a4, a4
18275a0015d6SChris Zankel	or	a4, a5, a4
18285a0015d6SChris Zankel	wsr	a4, CPENABLE
18295a0015d6SChris Zankel	rsync
18305a0015d6SChris Zankel	movi	a5, coprocessor_info	# list of owner and offset into cp_save
18315a0015d6SChris Zankel	addx8	a0, a4, a5		# entry for CP
18325a0015d6SChris Zankel
18335a0015d6SChris Zankel	bne	a4, a5, .Lload		# bit wasn't set before, cp not in use
18345a0015d6SChris Zankel
18355a0015d6SChris Zankel	/* Now compare the current task with the owner of the coprocessor.
18365a0015d6SChris Zankel	 * If they are the same, there is no reason to save or restore any
18375a0015d6SChris Zankel	 * coprocessor state. Having already enabled the coprocessor,
18385a0015d6SChris Zankel	 * branch ahead to return.
18395a0015d6SChris Zankel	 */
18405a0015d6SChris Zankel	GET_CURRENT(a5,a1)
18415a0015d6SChris Zankel	l32i	a4, a0, COPROCESSOR_INFO_OWNER	# a4: current owner for this CP
18425a0015d6SChris Zankel	beq	a4, a5, .Ldone
18435a0015d6SChris Zankel
18445a0015d6SChris Zankel	/* Find location to dump current coprocessor state:
18455a0015d6SChris Zankel	 *  task_struct->task_cp_save_offset + coprocessor_offset[coprocessor]
18465a0015d6SChris Zankel	 *
18475a0015d6SChris Zankel	 * Note: a0 pointer to the entry in the coprocessor owner table,
18485a0015d6SChris Zankel	 *	 a3 coprocessor number,
18495a0015d6SChris Zankel         *	 a4 current owner of coprocessor.
18505a0015d6SChris Zankel	 */
18515a0015d6SChris Zankel	l32i	a5, a0, COPROCESSOR_INFO_OFFSET
18525a0015d6SChris Zankel	addi	a2, a4, THREAD_CP_SAVE
18535a0015d6SChris Zankel	add	a2, a2, a5
18545a0015d6SChris Zankel
18555a0015d6SChris Zankel	/* Store current coprocessor states. (a5 still has CP number) */
18565a0015d6SChris Zankel
18575a0015d6SChris Zankel	xchal_cpi_store_funcbody
18585a0015d6SChris Zankel
18595a0015d6SChris Zankel	/* The macro might have destroyed a3 (coprocessor number), but
18605a0015d6SChris Zankel	 * SAR still has 32 - coprocessor_number!
18615a0015d6SChris Zankel	 */
18625a0015d6SChris Zankel	movi	a3, 32
18635a0015d6SChris Zankel	rsr	a4, SAR
18645a0015d6SChris Zankel	sub	a3, a3, a4
18655a0015d6SChris Zankel
18665a0015d6SChris Zankel.Lload:	/* A new task now owns the corpocessors. Save its TCB pointer into
18675a0015d6SChris Zankel	 * the coprocessor owner table.
18685a0015d6SChris Zankel	 *
18695a0015d6SChris Zankel	 * Note: a0 pointer to the entry in the coprocessor owner table,
18705a0015d6SChris Zankel	 *	 a3 coprocessor number.
18715a0015d6SChris Zankel	 */
18725a0015d6SChris Zankel	GET_CURRENT(a4,a1)
18735a0015d6SChris Zankel	s32i	a4, a0, 0
18745a0015d6SChris Zankel
18755a0015d6SChris Zankel	/* Find location from where to restore the current coprocessor state.*/
18765a0015d6SChris Zankel
18775a0015d6SChris Zankel	l32i	a5, a0, COPROCESSOR_INFO_OFFSET
18785a0015d6SChris Zankel	addi	a2, a4, THREAD_CP_SAVE
18795a0015d6SChris Zankel	add	a2, a2, a4
18805a0015d6SChris Zankel
18815a0015d6SChris Zankel	xchal_cpi_load_funcbody
18825a0015d6SChris Zankel
18835a0015d6SChris Zankel	/* We must assume that the xchal_cpi_store_funcbody macro destroyed
18845a0015d6SChris Zankel	 * registers a2..a15.
18855a0015d6SChris Zankel	 */
18865a0015d6SChris Zankel
18875a0015d6SChris Zankel.Ldone:	l32i	a15, a1, PT_AREG15
18885a0015d6SChris Zankel	l32i	a14, a1, PT_AREG14
18895a0015d6SChris Zankel	l32i	a13, a1, PT_AREG13
18905a0015d6SChris Zankel	l32i	a12, a1, PT_AREG12
18915a0015d6SChris Zankel	l32i	a11, a1, PT_AREG11
18925a0015d6SChris Zankel	l32i	a10, a1, PT_AREG10
18935a0015d6SChris Zankel	l32i	a9, a1, PT_AREG9
18945a0015d6SChris Zankel	l32i	a8, a1, PT_AREG8
18955a0015d6SChris Zankel	l32i	a7, a1, PT_AREG7
18965a0015d6SChris Zankel	l32i	a6, a1, PT_AREG6
18975a0015d6SChris Zankel	l32i	a5, a1, PT_AREG5
18985a0015d6SChris Zankel	l32i	a4, a1, PT_AREG4
18995a0015d6SChris Zankel	l32i	a3, a1, PT_AREG3
19005a0015d6SChris Zankel	l32i	a2, a1, PT_AREG2
19015a0015d6SChris Zankel	l32i	a0, a1, PT_AREG0
19025a0015d6SChris Zankel	l32i	a1, a1, PT_AREG1
19035a0015d6SChris Zankel
19045a0015d6SChris Zankel	rfe
19055a0015d6SChris Zankel
19065a0015d6SChris Zankel#endif /* XCHAL_EXTRA_SA_SIZE */
19075a0015d6SChris Zankel
19085a0015d6SChris Zankel/*
1909*fc4fb2adSChris Zankel * System Calls.
1910*fc4fb2adSChris Zankel *
1911*fc4fb2adSChris Zankel * void system_call (struct pt_regs* regs, int exccause)
1912*fc4fb2adSChris Zankel *                            a2                 a3
1913*fc4fb2adSChris Zankel */
1914*fc4fb2adSChris Zankel
1915*fc4fb2adSChris ZankelENTRY(system_call)
1916*fc4fb2adSChris Zankel	entry	a1, 32
1917*fc4fb2adSChris Zankel
1918*fc4fb2adSChris Zankel	/* regs->syscall = regs->areg[2] */
1919*fc4fb2adSChris Zankel
1920*fc4fb2adSChris Zankel	l32i	a3, a2, PT_AREG2
1921*fc4fb2adSChris Zankel	mov	a6, a2
1922*fc4fb2adSChris Zankel	movi	a4, do_syscall_trace_enter
1923*fc4fb2adSChris Zankel	s32i	a3, a2, PT_SYSCALL
1924*fc4fb2adSChris Zankel	callx4	a4
1925*fc4fb2adSChris Zankel
1926*fc4fb2adSChris Zankel	/* syscall = sys_call_table[syscall_nr] */
1927*fc4fb2adSChris Zankel
1928*fc4fb2adSChris Zankel	movi	a4, sys_call_table;
1929*fc4fb2adSChris Zankel	movi	a5, __NR_syscall_count
1930*fc4fb2adSChris Zankel	movi	a6, -ENOSYS
1931*fc4fb2adSChris Zankel	bgeu	a3, a5, 1f
1932*fc4fb2adSChris Zankel
1933*fc4fb2adSChris Zankel	addx4	a4, a3, a4
1934*fc4fb2adSChris Zankel	l32i	a4, a4, 0
1935*fc4fb2adSChris Zankel	movi	a5, sys_ni_syscall;
1936*fc4fb2adSChris Zankel	beq	a4, a5, 1f
1937*fc4fb2adSChris Zankel
1938*fc4fb2adSChris Zankel	/* Load args: arg0 - arg5 are passed via regs. */
1939*fc4fb2adSChris Zankel
1940*fc4fb2adSChris Zankel	l32i	a6, a2, PT_AREG6
1941*fc4fb2adSChris Zankel	l32i	a7, a2, PT_AREG3
1942*fc4fb2adSChris Zankel	l32i	a8, a2, PT_AREG4
1943*fc4fb2adSChris Zankel	l32i	a9, a2, PT_AREG5
1944*fc4fb2adSChris Zankel	l32i	a10, a2, PT_AREG8
1945*fc4fb2adSChris Zankel	l32i	a11, a2, PT_AREG9
1946*fc4fb2adSChris Zankel
1947*fc4fb2adSChris Zankel	/* Pass one additional argument to the syscall: pt_regs (on stack) */
1948*fc4fb2adSChris Zankel	s32i	a2, a1, 0
1949*fc4fb2adSChris Zankel
1950*fc4fb2adSChris Zankel	callx4	a4
1951*fc4fb2adSChris Zankel
1952*fc4fb2adSChris Zankel1:	/* regs->areg[2] = return_value */
1953*fc4fb2adSChris Zankel
1954*fc4fb2adSChris Zankel	s32i	a6, a2, PT_AREG2
1955*fc4fb2adSChris Zankel	movi	a4, do_syscall_trace_leave
1956*fc4fb2adSChris Zankel	mov	a6, a2
1957*fc4fb2adSChris Zankel	callx4	a4
1958*fc4fb2adSChris Zankel	retw
1959*fc4fb2adSChris Zankel
1960*fc4fb2adSChris Zankel
1961*fc4fb2adSChris Zankel/*
1962*fc4fb2adSChris Zankel * Create a kernel thread
1963*fc4fb2adSChris Zankel *
1964*fc4fb2adSChris Zankel * int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
1965*fc4fb2adSChris Zankel * a2                    a2                 a3             a4
1966*fc4fb2adSChris Zankel */
1967*fc4fb2adSChris Zankel
1968*fc4fb2adSChris ZankelENTRY(kernel_thread)
1969*fc4fb2adSChris Zankel	entry	a1, 16
1970*fc4fb2adSChris Zankel
1971*fc4fb2adSChris Zankel	mov	a5, a2			# preserve fn over syscall
1972*fc4fb2adSChris Zankel	mov	a7, a3			# preserve args over syscall
1973*fc4fb2adSChris Zankel
1974*fc4fb2adSChris Zankel	movi	a3, _CLONE_VM | _CLONE_UNTRACED
1975*fc4fb2adSChris Zankel	movi	a2, __NR_clone
1976*fc4fb2adSChris Zankel	or	a6, a4, a3		# arg0: flags
1977*fc4fb2adSChris Zankel	mov	a3, a1			# arg1: sp
1978*fc4fb2adSChris Zankel	syscall
1979*fc4fb2adSChris Zankel
1980*fc4fb2adSChris Zankel	beq	a3, a1, 1f		# branch if parent
1981*fc4fb2adSChris Zankel	mov	a6, a7			# args
1982*fc4fb2adSChris Zankel	callx4	a5			# fn(args)
1983*fc4fb2adSChris Zankel
1984*fc4fb2adSChris Zankel	movi	a2, __NR_exit
1985*fc4fb2adSChris Zankel	syscall				# return value of fn(args) still in a6
1986*fc4fb2adSChris Zankel
1987*fc4fb2adSChris Zankel1:	retw
1988*fc4fb2adSChris Zankel
1989*fc4fb2adSChris Zankel/*
1990*fc4fb2adSChris Zankel * Do a system call from kernel instead of calling sys_execve, so we end up
1991*fc4fb2adSChris Zankel * with proper pt_regs.
1992*fc4fb2adSChris Zankel *
1993*fc4fb2adSChris Zankel * int kernel_execve(const char *fname, char *const argv[], charg *const envp[])
1994*fc4fb2adSChris Zankel * a2                        a2               a3                  a4
1995*fc4fb2adSChris Zankel */
1996*fc4fb2adSChris Zankel
1997*fc4fb2adSChris ZankelENTRY(kernel_execve)
1998*fc4fb2adSChris Zankel	entry	a1, 16
1999*fc4fb2adSChris Zankel	mov	a6, a2			# arg0 is in a6
2000*fc4fb2adSChris Zankel	movi	a2, __NR_execve
2001*fc4fb2adSChris Zankel	syscall
2002*fc4fb2adSChris Zankel
2003*fc4fb2adSChris Zankel	retw
2004*fc4fb2adSChris Zankel
2005*fc4fb2adSChris Zankel/*
20065a0015d6SChris Zankel * Task switch.
20075a0015d6SChris Zankel *
20085a0015d6SChris Zankel * struct task*  _switch_to (struct task* prev, struct task* next)
20095a0015d6SChris Zankel *         a2                              a2                 a3
20105a0015d6SChris Zankel */
20115a0015d6SChris Zankel
20125a0015d6SChris ZankelENTRY(_switch_to)
20135a0015d6SChris Zankel
20145a0015d6SChris Zankel	entry	a1, 16
20155a0015d6SChris Zankel
20165a0015d6SChris Zankel	mov	a4, a3			# preserve a3
20175a0015d6SChris Zankel
20185a0015d6SChris Zankel	s32i	a0, a2, THREAD_RA	# save return address
20195a0015d6SChris Zankel	s32i	a1, a2, THREAD_SP	# save stack pointer
20205a0015d6SChris Zankel
20215a0015d6SChris Zankel	/* Disable ints while we manipulate the stack pointer; spill regs. */
20225a0015d6SChris Zankel
2023173d6681SChris Zankel	movi	a5, (1 << PS_EXCM_BIT) | LOCKLEVEL
20245a0015d6SChris Zankel	xsr	a5, PS
20255a0015d6SChris Zankel	rsr	a3, EXCSAVE_1
20265a0015d6SChris Zankel	rsync
20275a0015d6SChris Zankel	s32i	a3, a3, EXC_TABLE_FIXUP	/* enter critical section */
20285a0015d6SChris Zankel
20295a0015d6SChris Zankel	call0	_spill_registers
20305a0015d6SChris Zankel
20315a0015d6SChris Zankel	/* Set kernel stack (and leave critical section)
20325a0015d6SChris Zankel	 * Note: It's save to set it here. The stack will not be overwritten
20335a0015d6SChris Zankel	 *       because the kernel stack will only be loaded again after
20345a0015d6SChris Zankel	 *       we return from kernel space.
20355a0015d6SChris Zankel	 */
20365a0015d6SChris Zankel
20375a0015d6SChris Zankel	l32i	a0, a4, TASK_THREAD_INFO
20385a0015d6SChris Zankel	rsr	a3, EXCSAVE_1		# exc_table
20395a0015d6SChris Zankel	movi	a1, 0
20405a0015d6SChris Zankel	addi	a0, a0, PT_REGS_OFFSET
20415a0015d6SChris Zankel	s32i	a1, a3, EXC_TABLE_FIXUP
20425a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_KSTK
20435a0015d6SChris Zankel
20445a0015d6SChris Zankel	/* restore context of the task that 'next' addresses */
20455a0015d6SChris Zankel
20465a0015d6SChris Zankel	l32i	a0, a4, THREAD_RA	/* restore return address */
20475a0015d6SChris Zankel	l32i	a1, a4, THREAD_SP	/* restore stack pointer */
20485a0015d6SChris Zankel
20495a0015d6SChris Zankel	wsr	a5, PS
20505a0015d6SChris Zankel	rsync
20515a0015d6SChris Zankel
20525a0015d6SChris Zankel	retw
20535a0015d6SChris Zankel
20545a0015d6SChris Zankel
20555a0015d6SChris ZankelENTRY(ret_from_fork)
20565a0015d6SChris Zankel
20575a0015d6SChris Zankel	/* void schedule_tail (struct task_struct *prev)
20585a0015d6SChris Zankel	 * Note: prev is still in a6 (return value from fake call4 frame)
20595a0015d6SChris Zankel	 */
20605a0015d6SChris Zankel	movi	a4, schedule_tail
20615a0015d6SChris Zankel	callx4	a4
20625a0015d6SChris Zankel
2063*fc4fb2adSChris Zankel	movi	a4, do_syscall_trace_leave
2064*fc4fb2adSChris Zankel	mov	a6, a1
20655a0015d6SChris Zankel	callx4	a4
20665a0015d6SChris Zankel
20675a0015d6SChris Zankel	j	common_exception_return
20685a0015d6SChris Zankel
2069