xref: /linux-6.15/arch/xtensa/kernel/entry.S (revision 7f9c9741)
15a0015d6SChris Zankel/*
25a0015d6SChris Zankel * Low-level exception handling
35a0015d6SChris Zankel *
45a0015d6SChris Zankel * This file is subject to the terms and conditions of the GNU General Public
55a0015d6SChris Zankel * License.  See the file "COPYING" in the main directory of this archive
65a0015d6SChris Zankel * for more details.
75a0015d6SChris Zankel *
82d1c645cSMarc Gauthier * Copyright (C) 2004 - 2008 by Tensilica Inc.
938fef73cSMax Filippov * Copyright (C) 2015 Cadence Design Systems Inc.
105a0015d6SChris Zankel *
115a0015d6SChris Zankel * Chris Zankel <[email protected]>
125a0015d6SChris Zankel *
135a0015d6SChris Zankel */
145a0015d6SChris Zankel
155a0015d6SChris Zankel#include <linux/linkage.h>
1665fddcfcSMike Rapoport#include <linux/pgtable.h>
170013a854SSam Ravnborg#include <asm/asm-offsets.h>
180013acebSMax Filippov#include <asm/asmmacro.h>
195a0015d6SChris Zankel#include <asm/processor.h>
204573e398SChris Zankel#include <asm/coprocessor.h>
215a0015d6SChris Zankel#include <asm/thread_info.h>
2276580237SAl Viro#include <asm/asm-uaccess.h>
235a0015d6SChris Zankel#include <asm/unistd.h>
245a0015d6SChris Zankel#include <asm/ptrace.h>
255a0015d6SChris Zankel#include <asm/current.h>
265a0015d6SChris Zankel#include <asm/page.h>
275a0015d6SChris Zankel#include <asm/signal.h>
28173d6681SChris Zankel#include <asm/tlbflush.h>
29367b8112SChris Zankel#include <variant/tie-asm.h>
305a0015d6SChris Zankel
315a0015d6SChris Zankel/*
325a0015d6SChris Zankel * Macro to find first bit set in WINDOWBASE from the left + 1
335a0015d6SChris Zankel *
345a0015d6SChris Zankel * 100....0 -> 1
355a0015d6SChris Zankel * 010....0 -> 2
365a0015d6SChris Zankel * 000....1 -> WSBITS
375a0015d6SChris Zankel */
385a0015d6SChris Zankel
395a0015d6SChris Zankel	.macro ffs_ws bit mask
405a0015d6SChris Zankel
415a0015d6SChris Zankel#if XCHAL_HAVE_NSA
425a0015d6SChris Zankel	nsau    \bit, \mask			# 32-WSBITS ... 31 (32 iff 0)
435a0015d6SChris Zankel	addi    \bit, \bit, WSBITS - 32 + 1   	# uppest bit set -> return 1
445a0015d6SChris Zankel#else
455a0015d6SChris Zankel	movi    \bit, WSBITS
465a0015d6SChris Zankel#if WSBITS > 16
475a0015d6SChris Zankel	_bltui  \mask, 0x10000, 99f
485a0015d6SChris Zankel	addi    \bit, \bit, -16
495a0015d6SChris Zankel	extui   \mask, \mask, 16, 16
505a0015d6SChris Zankel#endif
515a0015d6SChris Zankel#if WSBITS > 8
525a0015d6SChris Zankel99:	_bltui  \mask, 0x100, 99f
535a0015d6SChris Zankel	addi    \bit, \bit, -8
545a0015d6SChris Zankel	srli    \mask, \mask, 8
555a0015d6SChris Zankel#endif
565a0015d6SChris Zankel99:	_bltui  \mask, 0x10, 99f
575a0015d6SChris Zankel	addi    \bit, \bit, -4
585a0015d6SChris Zankel	srli    \mask, \mask, 4
595a0015d6SChris Zankel99:	_bltui  \mask, 0x4, 99f
605a0015d6SChris Zankel	addi    \bit, \bit, -2
615a0015d6SChris Zankel	srli    \mask, \mask, 2
625a0015d6SChris Zankel99:	_bltui  \mask, 0x2, 99f
635a0015d6SChris Zankel	addi    \bit, \bit, -1
645a0015d6SChris Zankel99:
655a0015d6SChris Zankel
665a0015d6SChris Zankel#endif
675a0015d6SChris Zankel	.endm
685a0015d6SChris Zankel
6938fef73cSMax Filippov
7038fef73cSMax Filippov	.macro	irq_save flags tmp
7138fef73cSMax Filippov#if XTENSA_FAKE_NMI
7238fef73cSMax Filippov#if defined(CONFIG_DEBUG_KERNEL) && (LOCKLEVEL | TOPLEVEL) >= XCHAL_DEBUGLEVEL
7338fef73cSMax Filippov	rsr	\flags, ps
7438fef73cSMax Filippov	extui	\tmp, \flags, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
7538fef73cSMax Filippov	bgei	\tmp, LOCKLEVEL, 99f
7638fef73cSMax Filippov	rsil	\tmp, LOCKLEVEL
7738fef73cSMax Filippov99:
7838fef73cSMax Filippov#else
7938fef73cSMax Filippov	movi	\tmp, LOCKLEVEL
8038fef73cSMax Filippov	rsr	\flags, ps
8138fef73cSMax Filippov	or	\flags, \flags, \tmp
8238fef73cSMax Filippov	xsr	\flags, ps
8338fef73cSMax Filippov	rsync
8438fef73cSMax Filippov#endif
8538fef73cSMax Filippov#else
8638fef73cSMax Filippov	rsil	\flags, LOCKLEVEL
8738fef73cSMax Filippov#endif
8838fef73cSMax Filippov	.endm
8938fef73cSMax Filippov
905a0015d6SChris Zankel/* ----------------- DEFAULT FIRST LEVEL EXCEPTION HANDLERS ----------------- */
915a0015d6SChris Zankel
925a0015d6SChris Zankel/*
935a0015d6SChris Zankel * First-level exception handler for user exceptions.
945a0015d6SChris Zankel * Save some special registers, extra states and all registers in the AR
955a0015d6SChris Zankel * register file that were in use in the user task, and jump to the common
965a0015d6SChris Zankel * exception code.
975a0015d6SChris Zankel * We save SAR (used to calculate WMASK), and WB and WS (we don't have to
985a0015d6SChris Zankel * save them for kernel exceptions).
995a0015d6SChris Zankel *
1005a0015d6SChris Zankel * Entry condition for user_exception:
1015a0015d6SChris Zankel *
1025a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
1035a0015d6SChris Zankel *   a1:	a1
1045a0015d6SChris Zankel *   a2:	new stack pointer, original value in depc
10599d5040eSMax Filippov *   a3:	a3
1065a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
10799d5040eSMax Filippov *   excsave1:	dispatch table
1085a0015d6SChris Zankel *
1095a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
1105a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
1115a0015d6SChris Zankel *
1125a0015d6SChris Zankel * Entry condition for _user_exception:
1135a0015d6SChris Zankel *
1145a0015d6SChris Zankel *   a0-a3 and depc have been saved to PT_AREG0...PT_AREG3 and PT_DEPC
1155a0015d6SChris Zankel *   excsave has been restored, and
1165a0015d6SChris Zankel *   stack pointer (a1) has been set.
1175a0015d6SChris Zankel *
1183ad2f3fbSDaniel Mack * Note: _user_exception might be at an odd address. Don't use call0..call12
1195a0015d6SChris Zankel */
120f8f02ca7SMax Filippov	.literal_position
1215a0015d6SChris Zankel
1225a0015d6SChris ZankelENTRY(user_exception)
1235a0015d6SChris Zankel
12499d5040eSMax Filippov	/* Save a1, a2, a3, and set SP. */
1255a0015d6SChris Zankel
126bc5378fcSMax Filippov	rsr	a0, depc
1275a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
1285a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2
1295a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
1305a0015d6SChris Zankel	mov	a1, a2
1315a0015d6SChris Zankel
1325a0015d6SChris Zankel	.globl _user_exception
1335a0015d6SChris Zankel_user_exception:
1345a0015d6SChris Zankel
1355a0015d6SChris Zankel	/* Save SAR and turn off single stepping */
1365a0015d6SChris Zankel
1375a0015d6SChris Zankel	movi	a2, 0
138b6569439SMax Filippov	wsr	a2, depc		# terminate user stack trace with 0
139bc5378fcSMax Filippov	rsr	a3, sar
140bc5378fcSMax Filippov	xsr	a2, icountlevel
1415a0015d6SChris Zankel	s32i	a3, a1, PT_SAR
14229c4dfd9SChris Zankel	s32i	a2, a1, PT_ICOUNTLEVEL
1435a0015d6SChris Zankel
144c50842dfSChris Zankel#if XCHAL_HAVE_THREADPTR
145c50842dfSChris Zankel	rur	a2, threadptr
146c50842dfSChris Zankel	s32i	a2, a1, PT_THREADPTR
147c50842dfSChris Zankel#endif
148c50842dfSChris Zankel
1495a0015d6SChris Zankel	/* Rotate ws so that the current windowbase is at bit0. */
1505a0015d6SChris Zankel	/* Assume ws = xxwww1yyyy. Rotate ws right, so that a2 = yyyyxxwww1 */
1515a0015d6SChris Zankel
1520b537257SMax Filippov#if defined(USER_SUPPORT_WINDOWED)
153bc5378fcSMax Filippov	rsr	a2, windowbase
154bc5378fcSMax Filippov	rsr	a3, windowstart
1555a0015d6SChris Zankel	ssr	a2
1565a0015d6SChris Zankel	s32i	a2, a1, PT_WINDOWBASE
1575a0015d6SChris Zankel	s32i	a3, a1, PT_WINDOWSTART
1585a0015d6SChris Zankel	slli	a2, a3, 32-WSBITS
1595a0015d6SChris Zankel	src	a2, a3, a2
1605a0015d6SChris Zankel	srli	a2, a2, 32-WSBITS
1615a0015d6SChris Zankel	s32i	a2, a1, PT_WMASK	# needed for restoring registers
1620b537257SMax Filippov#else
1630b537257SMax Filippov	movi	a2, 0
1640b537257SMax Filippov	movi	a3, 1
1650b537257SMax Filippov	s32i	a2, a1, PT_WINDOWBASE
1660b537257SMax Filippov	s32i	a3, a1, PT_WINDOWSTART
1670b537257SMax Filippov	s32i	a3, a1, PT_WMASK
1680b537257SMax Filippov#endif
1695a0015d6SChris Zankel
1705a0015d6SChris Zankel	/* Save only live registers. */
1715a0015d6SChris Zankel
172*7f9c9741SMax FilippovUABI_W	_bbsi.l	a2, 1, .Lsave_window_registers
1735a0015d6SChris Zankel	s32i	a4, a1, PT_AREG4
1745a0015d6SChris Zankel	s32i	a5, a1, PT_AREG5
1755a0015d6SChris Zankel	s32i	a6, a1, PT_AREG6
1765a0015d6SChris Zankel	s32i	a7, a1, PT_AREG7
177*7f9c9741SMax FilippovUABI_W	_bbsi.l	a2, 2, .Lsave_window_registers
1785a0015d6SChris Zankel	s32i	a8, a1, PT_AREG8
1795a0015d6SChris Zankel	s32i	a9, a1, PT_AREG9
1805a0015d6SChris Zankel	s32i	a10, a1, PT_AREG10
1815a0015d6SChris Zankel	s32i	a11, a1, PT_AREG11
182*7f9c9741SMax FilippovUABI_W	_bbsi.l	a2, 3, .Lsave_window_registers
1835a0015d6SChris Zankel	s32i	a12, a1, PT_AREG12
1845a0015d6SChris Zankel	s32i	a13, a1, PT_AREG13
1855a0015d6SChris Zankel	s32i	a14, a1, PT_AREG14
1865a0015d6SChris Zankel	s32i	a15, a1, PT_AREG15
1870b537257SMax Filippov
1880b537257SMax Filippov#if defined(USER_SUPPORT_WINDOWED)
189*7f9c9741SMax Filippov	/* If only one valid frame skip saving regs. */
1905a0015d6SChris Zankel
191*7f9c9741SMax Filippov	beqi	a2, 1, common_exception
1925a0015d6SChris Zankel
1935a0015d6SChris Zankel	/* Save the remaining registers.
1945a0015d6SChris Zankel	 * We have to save all registers up to the first '1' from
1955a0015d6SChris Zankel	 * the right, except the current frame (bit 0).
1965a0015d6SChris Zankel	 * Assume a2 is:  001001000110001
1976656920bSChris Zankel	 * All register frames starting from the top field to the marked '1'
1985a0015d6SChris Zankel	 * must be saved.
1995a0015d6SChris Zankel	 */
200*7f9c9741SMax Filippov.Lsave_window_registers:
201*7f9c9741SMax Filippov	addi	a3, a2, -1		# eliminate '1' in bit 0: yyyyxxww0
2025a0015d6SChris Zankel	neg	a3, a3			# yyyyxxww0 -> YYYYXXWW1+1
2035a0015d6SChris Zankel	and	a3, a3, a2		# max. only one bit is set
2045a0015d6SChris Zankel
2055a0015d6SChris Zankel	/* Find number of frames to save */
2065a0015d6SChris Zankel
2075a0015d6SChris Zankel	ffs_ws	a0, a3			# number of frames to the '1' from left
2085a0015d6SChris Zankel
2095a0015d6SChris Zankel	/* Store information into WMASK:
2105a0015d6SChris Zankel	 * bits 0..3: xxx1 masked lower 4 bits of the rotated windowstart,
2115a0015d6SChris Zankel	 * bits 4...: number of valid 4-register frames
2125a0015d6SChris Zankel	 */
2135a0015d6SChris Zankel
2145a0015d6SChris Zankel	slli	a3, a0, 4		# number of frames to save in bits 8..4
2155a0015d6SChris Zankel	extui	a2, a2, 0, 4		# mask for the first 16 registers
2165a0015d6SChris Zankel	or	a2, a3, a2
2175a0015d6SChris Zankel	s32i	a2, a1, PT_WMASK	# needed when we restore the reg-file
2185a0015d6SChris Zankel
2195a0015d6SChris Zankel	/* Save 4 registers at a time */
2205a0015d6SChris Zankel
2215a0015d6SChris Zankel1:	rotw	-1
2225a0015d6SChris Zankel	s32i	a0, a5, PT_AREG_END - 16
2235a0015d6SChris Zankel	s32i	a1, a5, PT_AREG_END - 12
2245a0015d6SChris Zankel	s32i	a2, a5, PT_AREG_END - 8
2255a0015d6SChris Zankel	s32i	a3, a5, PT_AREG_END - 4
2265a0015d6SChris Zankel	addi	a0, a4, -1
2275a0015d6SChris Zankel	addi	a1, a5, -16
2285a0015d6SChris Zankel	_bnez	a0, 1b
2295a0015d6SChris Zankel
2305a0015d6SChris Zankel	/* WINDOWBASE still in SAR! */
2315a0015d6SChris Zankel
232bc5378fcSMax Filippov	rsr	a2, sar			# original WINDOWBASE
2335a0015d6SChris Zankel	movi	a3, 1
2345a0015d6SChris Zankel	ssl	a2
2355a0015d6SChris Zankel	sll	a3, a3
236bc5378fcSMax Filippov	wsr	a3, windowstart		# set corresponding WINDOWSTART bit
237bc5378fcSMax Filippov	wsr	a2, windowbase		# and WINDOWSTART
2385a0015d6SChris Zankel	rsync
2395a0015d6SChris Zankel
2405a0015d6SChris Zankel	/* We are back to the original stack pointer (a1) */
2410b537257SMax Filippov#endif
242*7f9c9741SMax Filippov	/* Now, jump to the common exception handler. */
2435a0015d6SChris Zankel
2445a0015d6SChris Zankel	j	common_exception
2455a0015d6SChris Zankel
246d1538c46SChris ZankelENDPROC(user_exception)
2475a0015d6SChris Zankel
2485a0015d6SChris Zankel/*
2495a0015d6SChris Zankel * First-level exit handler for kernel exceptions
2505a0015d6SChris Zankel * Save special registers and the live window frame.
2515a0015d6SChris Zankel * Note: Even though we changes the stack pointer, we don't have to do a
2525a0015d6SChris Zankel *	 MOVSP here, as we do that when we return from the exception.
2535a0015d6SChris Zankel *	 (See comment in the kernel exception exit code)
2545a0015d6SChris Zankel *
2555a0015d6SChris Zankel * Entry condition for kernel_exception:
2565a0015d6SChris Zankel *
2575a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
2585a0015d6SChris Zankel *   a1:	a1
2595a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
26099d5040eSMax Filippov *   a3:	a3
2615a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
26299d5040eSMax Filippov *   excsave_1:	dispatch table
2635a0015d6SChris Zankel *
2645a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
2655a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
2665a0015d6SChris Zankel *
2675a0015d6SChris Zankel * Entry condition for _kernel_exception:
2685a0015d6SChris Zankel *
2695a0015d6SChris Zankel *   a0-a3 and depc have been saved to PT_AREG0...PT_AREG3 and PT_DEPC
2705a0015d6SChris Zankel *   excsave has been restored, and
2715a0015d6SChris Zankel *   stack pointer (a1) has been set.
2725a0015d6SChris Zankel *
2733ad2f3fbSDaniel Mack * Note: _kernel_exception might be at an odd address. Don't use call0..call12
2745a0015d6SChris Zankel */
2755a0015d6SChris Zankel
2765a0015d6SChris ZankelENTRY(kernel_exception)
2775a0015d6SChris Zankel
27899d5040eSMax Filippov	/* Save a1, a2, a3, and set SP. */
2795a0015d6SChris Zankel
280bc5378fcSMax Filippov	rsr	a0, depc		# get a2
2815a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
2825a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2
2835a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
2845a0015d6SChris Zankel	mov	a1, a2
2855a0015d6SChris Zankel
2865a0015d6SChris Zankel	.globl _kernel_exception
2875a0015d6SChris Zankel_kernel_exception:
2885a0015d6SChris Zankel
2895a0015d6SChris Zankel	/* Save SAR and turn off single stepping */
2905a0015d6SChris Zankel
2915a0015d6SChris Zankel	movi	a2, 0
292bc5378fcSMax Filippov	rsr	a3, sar
293bc5378fcSMax Filippov	xsr	a2, icountlevel
2945a0015d6SChris Zankel	s32i	a3, a1, PT_SAR
29529c4dfd9SChris Zankel	s32i	a2, a1, PT_ICOUNTLEVEL
2965a0015d6SChris Zankel
2970b537257SMax Filippov#if defined(__XTENSA_WINDOWED_ABI__)
2985a0015d6SChris Zankel	/* Rotate ws so that the current windowbase is at bit0. */
2995a0015d6SChris Zankel	/* Assume ws = xxwww1yyyy. Rotate ws right, so that a2 = yyyyxxwww1 */
3005a0015d6SChris Zankel
301bc5378fcSMax Filippov	rsr	a2, windowbase		# don't need to save these, we only
302bc5378fcSMax Filippov	rsr	a3, windowstart		# need shifted windowstart: windowmask
3035a0015d6SChris Zankel	ssr	a2
3045a0015d6SChris Zankel	slli	a2, a3, 32-WSBITS
3055a0015d6SChris Zankel	src	a2, a3, a2
3065a0015d6SChris Zankel	srli	a2, a2, 32-WSBITS
3075a0015d6SChris Zankel	s32i	a2, a1, PT_WMASK	# needed for kernel_exception_exit
3080b537257SMax Filippov#endif
3095a0015d6SChris Zankel
3105a0015d6SChris Zankel	/* Save only the live window-frame */
3115a0015d6SChris Zankel
3120b537257SMax FilippovKABI_W	_bbsi.l	a2, 1, 1f
3135a0015d6SChris Zankel	s32i	a4, a1, PT_AREG4
3145a0015d6SChris Zankel	s32i	a5, a1, PT_AREG5
3155a0015d6SChris Zankel	s32i	a6, a1, PT_AREG6
3165a0015d6SChris Zankel	s32i	a7, a1, PT_AREG7
3170b537257SMax FilippovKABI_W	_bbsi.l	a2, 2, 1f
3185a0015d6SChris Zankel	s32i	a8, a1, PT_AREG8
3195a0015d6SChris Zankel	s32i	a9, a1, PT_AREG9
3205a0015d6SChris Zankel	s32i	a10, a1, PT_AREG10
3215a0015d6SChris Zankel	s32i	a11, a1, PT_AREG11
3220b537257SMax FilippovKABI_W	_bbsi.l	a2, 3, 1f
3235a0015d6SChris Zankel	s32i	a12, a1, PT_AREG12
3245a0015d6SChris Zankel	s32i	a13, a1, PT_AREG13
3255a0015d6SChris Zankel	s32i	a14, a1, PT_AREG14
3265a0015d6SChris Zankel	s32i	a15, a1, PT_AREG15
3275a0015d6SChris Zankel
3280b537257SMax Filippov#ifdef __XTENSA_WINDOWED_ABI__
329b6569439SMax Filippov	_bnei	a2, 1, 1f
330b6569439SMax Filippov	/* Copy spill slots of a0 and a1 to imitate movsp
331b6569439SMax Filippov	 * in order to keep exception stack continuous
332b6569439SMax Filippov	 */
3336fad9ddcSMax Filippov	l32i	a3, a1, PT_KERNEL_SIZE
3346fad9ddcSMax Filippov	l32i	a0, a1, PT_KERNEL_SIZE + 4
335b6569439SMax Filippov	s32e	a3, a1, -16
336b6569439SMax Filippov	s32e	a0, a1, -12
3370b537257SMax Filippov#endif
3385a0015d6SChris Zankel1:
339b6569439SMax Filippov	l32i	a0, a1, PT_AREG0	# restore saved a0
340b6569439SMax Filippov	wsr	a0, depc
3415a0015d6SChris Zankel
3425a0015d6SChris Zankel/*
3435a0015d6SChris Zankel * This is the common exception handler.
3445a0015d6SChris Zankel * We get here from the user exception handler or simply by falling through
3455a0015d6SChris Zankel * from the kernel exception handler.
3465a0015d6SChris Zankel * Save the remaining special registers, switch to kernel mode, and jump
3475a0015d6SChris Zankel * to the second-level exception handler.
3485a0015d6SChris Zankel *
3495a0015d6SChris Zankel */
3505a0015d6SChris Zankel
3515a0015d6SChris Zankelcommon_exception:
3525a0015d6SChris Zankel
35329c4dfd9SChris Zankel	/* Save some registers, disable loops and clear the syscall flag. */
3545a0015d6SChris Zankel
355bc5378fcSMax Filippov	rsr	a2, debugcause
356bc5378fcSMax Filippov	rsr	a3, epc1
3575a0015d6SChris Zankel	s32i	a2, a1, PT_DEBUGCAUSE
3585a0015d6SChris Zankel	s32i	a3, a1, PT_PC
3595a0015d6SChris Zankel
3606a986984SMax Filippov	movi	a2, NO_SYSCALL
361bc5378fcSMax Filippov	rsr	a3, excvaddr
36229c4dfd9SChris Zankel	s32i	a2, a1, PT_SYSCALL
3635a0015d6SChris Zankel	movi	a2, 0
3645a0015d6SChris Zankel	s32i	a3, a1, PT_EXCVADDR
3655029615eSMax Filippov#if XCHAL_HAVE_LOOPS
366bc5378fcSMax Filippov	xsr	a2, lcount
3675a0015d6SChris Zankel	s32i	a2, a1, PT_LCOUNT
3685029615eSMax Filippov#endif
3695a0015d6SChris Zankel
370a0fc1436SMax Filippov#if XCHAL_HAVE_EXCLUSIVE
371a0fc1436SMax Filippov	/* Clear exclusive access monitor set by interrupted code */
372a0fc1436SMax Filippov	clrex
373a0fc1436SMax Filippov#endif
374a0fc1436SMax Filippov
3755a0015d6SChris Zankel	/* It is now save to restore the EXC_TABLE_FIXUP variable. */
3765a0015d6SChris Zankel
37738fef73cSMax Filippov	rsr	a2, exccause
3785a0015d6SChris Zankel	movi	a3, 0
37938fef73cSMax Filippov	rsr	a0, excsave1
38038fef73cSMax Filippov	s32i	a2, a1, PT_EXCCAUSE
38138fef73cSMax Filippov	s32i	a3, a0, EXC_TABLE_FIXUP
3825a0015d6SChris Zankel
383b6569439SMax Filippov	/* All unrecoverable states are saved on stack, now, and a1 is valid.
384b6569439SMax Filippov	 * Now we can allow exceptions again. In case we've got an interrupt
385b6569439SMax Filippov	 * PS.INTLEVEL is set to LOCKLEVEL disabling furhter interrupts,
386b6569439SMax Filippov	 * otherwise it's left unchanged.
3875a0015d6SChris Zankel	 *
388b6569439SMax Filippov	 * Set PS(EXCM = 0, UM = 0, RING = 0, OWB = 0, WOE = 1, INTLEVEL = X)
3895a0015d6SChris Zankel	 */
3905a0015d6SChris Zankel
391bc5378fcSMax Filippov	rsr	a3, ps
39238fef73cSMax Filippov	s32i	a3, a1, PT_PS		# save ps
39338fef73cSMax Filippov
39438fef73cSMax Filippov#if XTENSA_FAKE_NMI
39538fef73cSMax Filippov	/* Correct PS needs to be saved in the PT_PS:
39638fef73cSMax Filippov	 * - in case of exception or level-1 interrupt it's in the PS,
39738fef73cSMax Filippov	 *   and is already saved.
39838fef73cSMax Filippov	 * - in case of medium level interrupt it's in the excsave2.
39938fef73cSMax Filippov	 */
40038fef73cSMax Filippov	movi	a0, EXCCAUSE_MAPPED_NMI
40138fef73cSMax Filippov	extui	a3, a3, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
40238fef73cSMax Filippov	beq	a2, a0, .Lmedium_level_irq
40338fef73cSMax Filippov	bnei	a2, EXCCAUSE_LEVEL1_INTERRUPT, .Lexception
40438fef73cSMax Filippov	beqz	a3, .Llevel1_irq	# level-1 IRQ sets ps.intlevel to 0
40538fef73cSMax Filippov
40638fef73cSMax Filippov.Lmedium_level_irq:
40738fef73cSMax Filippov	rsr	a0, excsave2
40838fef73cSMax Filippov	s32i	a0, a1, PT_PS		# save medium-level interrupt ps
40938fef73cSMax Filippov	bgei	a3, LOCKLEVEL, .Lexception
41038fef73cSMax Filippov
41138fef73cSMax Filippov.Llevel1_irq:
41238fef73cSMax Filippov	movi	a3, LOCKLEVEL
41338fef73cSMax Filippov
41438fef73cSMax Filippov.Lexception:
4150b537257SMax FilippovKABI_W	movi	a0, PS_WOE_MASK
4160b537257SMax FilippovKABI_W	or	a3, a3, a0
41738fef73cSMax Filippov#else
41838fef73cSMax Filippov	addi	a2, a2, -EXCCAUSE_LEVEL1_INTERRUPT
41938fef73cSMax Filippov	movi	a0, LOCKLEVEL
4202d1c645cSMarc Gauthier	extui	a3, a3, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
4212d1c645cSMarc Gauthier					# a3 = PS.INTLEVEL
42238fef73cSMax Filippov	moveqz	a3, a0, a2		# a3 = LOCKLEVEL iff interrupt
4230b537257SMax FilippovKABI_W	movi	a2, PS_WOE_MASK
4240b537257SMax FilippovKABI_W	or	a3, a3, a2
42538fef73cSMax Filippov#endif
42638fef73cSMax Filippov
427b6569439SMax Filippov	/* restore return address (or 0 if return to userspace) */
428b6569439SMax Filippov	rsr	a0, depc
42938fef73cSMax Filippov	wsr	a3, ps
43038fef73cSMax Filippov	rsync				# PS.WOE => rsync => overflow
4315a0015d6SChris Zankel
432bc5378fcSMax Filippov	/* Save lbeg, lend */
4335029615eSMax Filippov#if XCHAL_HAVE_LOOPS
434b6569439SMax Filippov	rsr	a4, lbeg
435bc5378fcSMax Filippov	rsr	a3, lend
436b6569439SMax Filippov	s32i	a4, a1, PT_LBEG
4375a0015d6SChris Zankel	s32i	a3, a1, PT_LEND
4385029615eSMax Filippov#endif
4395a0015d6SChris Zankel
440733536b8SMax Filippov	/* Save SCOMPARE1 */
441733536b8SMax Filippov
442733536b8SMax Filippov#if XCHAL_HAVE_S32C1I
443b6569439SMax Filippov	rsr     a3, scompare1
444b6569439SMax Filippov	s32i    a3, a1, PT_SCOMPARE1
445733536b8SMax Filippov#endif
446733536b8SMax Filippov
447c658eac6SChris Zankel	/* Save optional registers. */
448c658eac6SChris Zankel
449b6569439SMax Filippov	save_xtregs_opt a1 a3 a4 a5 a6 a7 PT_XTREGS_OPT
450c658eac6SChris Zankel
451de4415d0SMax Filippov#ifdef CONFIG_TRACE_IRQFLAGS
452de4415d0SMax Filippov	rsr		abi_tmp0, ps
453de4415d0SMax Filippov	extui		abi_tmp0, abi_tmp0, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
454de4415d0SMax Filippov	beqz		abi_tmp0, 1f
455de4415d0SMax Filippov	abi_call	trace_hardirqs_off
456de4415d0SMax Filippov1:
457de4415d0SMax Filippov#endif
45855427d5bSMax Filippov#ifdef CONFIG_CONTEXT_TRACKING
45955427d5bSMax Filippov	l32i		abi_tmp0, a1, PT_PS
46055427d5bSMax Filippov	bbci.l		abi_tmp0, PS_UM_BIT, 1f
46155427d5bSMax Filippov	abi_call	context_tracking_user_exit
46255427d5bSMax Filippov1:
46355427d5bSMax Filippov#endif
464de4415d0SMax Filippov
4655a0015d6SChris Zankel	/* Go to second-level dispatcher. Set up parameters to pass to the
4665a0015d6SChris Zankel	 * exception handler and call the exception handler.
4675a0015d6SChris Zankel	 */
4685a0015d6SChris Zankel
469de4415d0SMax Filippov	l32i		abi_arg1, a1, PT_EXCCAUSE	# pass EXCCAUSE
470de4415d0SMax Filippov	rsr		abi_tmp0, excsave1
471de4415d0SMax Filippov	addx4		abi_tmp0, abi_arg1, abi_tmp0
472de4415d0SMax Filippov	l32i		abi_tmp0, abi_tmp0, EXC_TABLE_DEFAULT	# load handler
4730b537257SMax Filippov	mov		abi_arg0, a1			# pass stack frame
4745a0015d6SChris Zankel
4755a0015d6SChris Zankel	/* Call the second-level handler */
4765a0015d6SChris Zankel
477de4415d0SMax Filippov	abi_callx	abi_tmp0
4785a0015d6SChris Zankel
4795a0015d6SChris Zankel	/* Jump here for exception exit */
480e6ffe17eSdann	.global common_exception_return
4815a0015d6SChris Zankelcommon_exception_return:
4825a0015d6SChris Zankel
48338fef73cSMax Filippov#if XTENSA_FAKE_NMI
484e6d423aaSMax Filippov	l32i		abi_tmp0, a1, PT_EXCCAUSE
485e6d423aaSMax Filippov	movi		abi_tmp1, EXCCAUSE_MAPPED_NMI
486e6d423aaSMax Filippov	l32i		abi_saved1, a1, PT_PS
487e7e9614bSMax Filippov	beq		abi_tmp0, abi_tmp1, .Lrestore_state
48838fef73cSMax Filippov#endif
489e7e9614bSMax Filippov.Ltif_loop:
490961c5efbSMax Filippov	irq_save	abi_tmp0, abi_tmp1
4917d5f6a9aSMax Filippov#ifdef CONFIG_TRACE_IRQFLAGS
4920b537257SMax Filippov	abi_call	trace_hardirqs_off
4937d5f6a9aSMax Filippov#endif
494c92931b2SMax Filippov
4955a0015d6SChris Zankel	/* Jump if we are returning from kernel exceptions. */
4965a0015d6SChris Zankel
4970b537257SMax Filippov	l32i		abi_saved1, a1, PT_PS
498961c5efbSMax Filippov	GET_THREAD_INFO(abi_tmp0, a1)
499961c5efbSMax Filippov	l32i		abi_saved0, abi_tmp0, TI_FLAGS
500e7e9614bSMax Filippov	_bbci.l		abi_saved1, PS_UM_BIT, .Lexit_tif_loop_kernel
5015a0015d6SChris Zankel
5025a0015d6SChris Zankel	/* Specific to a user exception exit:
5035a0015d6SChris Zankel	 * We need to check some flags for signal handling and rescheduling,
5045a0015d6SChris Zankel	 * and have to restore WB and WS, extra states, and all registers
5055a0015d6SChris Zankel	 * in the register file that were in use in the user task.
506e1088430SChris Zankel	 * Note that we don't disable interrupts here.
5075a0015d6SChris Zankel	 */
5085a0015d6SChris Zankel
509961c5efbSMax Filippov	_bbsi.l		abi_saved0, TIF_NEED_RESCHED, .Lresched
510961c5efbSMax Filippov	movi		abi_tmp0, _TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_NOTIFY_SIGNAL
511961c5efbSMax Filippov	bnone		abi_saved0, abi_tmp0, .Lexit_tif_loop_user
5125a0015d6SChris Zankel
513961c5efbSMax Filippov	l32i		abi_tmp0, a1, PT_DEPC
514961c5efbSMax Filippov	bgeui		abi_tmp0, VALID_DOUBLE_EXCEPTION_ADDRESS, .Lrestore_state
5155a0015d6SChris Zankel
516e1088430SChris Zankel	/* Call do_signal() */
517e1088430SChris Zankel
5187d5f6a9aSMax Filippov#ifdef CONFIG_TRACE_IRQFLAGS
5190b537257SMax Filippov	abi_call	trace_hardirqs_on
5207d5f6a9aSMax Filippov#endif
521961c5efbSMax Filippov	rsil		abi_tmp0, 0
5220b537257SMax Filippov	mov		abi_arg0, a1
5230b537257SMax Filippov	abi_call	do_notify_resume	# int do_notify_resume(struct pt_regs*)
524e7e9614bSMax Filippov	j		.Ltif_loop
5255a0015d6SChris Zankel
526e7e9614bSMax Filippov.Lresched:
5277d5f6a9aSMax Filippov#ifdef CONFIG_TRACE_IRQFLAGS
5280b537257SMax Filippov	abi_call	trace_hardirqs_on
5297d5f6a9aSMax Filippov#endif
530961c5efbSMax Filippov	rsil		abi_tmp0, 0
5310b537257SMax Filippov	abi_call	schedule	# void schedule (void)
532e7e9614bSMax Filippov	j		.Ltif_loop
5335a0015d6SChris Zankel
534e7e9614bSMax Filippov.Lexit_tif_loop_kernel:
5356c5260d7SThomas Gleixner#ifdef CONFIG_PREEMPTION
536961c5efbSMax Filippov	_bbci.l		abi_saved0, TIF_NEED_RESCHED, .Lrestore_state
53716c5becfSMax Filippov
53816c5becfSMax Filippov	/* Check current_thread_info->preempt_count */
53916c5becfSMax Filippov
540961c5efbSMax Filippov	l32i		abi_tmp1, abi_tmp0, TI_PRE_COUNT
541961c5efbSMax Filippov	bnez		abi_tmp1, .Lrestore_state
5420b537257SMax Filippov	abi_call	preempt_schedule_irq
54316c5becfSMax Filippov#endif
544e7e9614bSMax Filippov	j		.Lrestore_state
54516c5becfSMax Filippov
546e7e9614bSMax Filippov.Lexit_tif_loop_user:
54755427d5bSMax Filippov#ifdef CONFIG_CONTEXT_TRACKING
54855427d5bSMax Filippov	abi_call	context_tracking_user_enter
54955427d5bSMax Filippov#endif
550c91e02bdSMax Filippov#ifdef CONFIG_HAVE_HW_BREAKPOINT
551961c5efbSMax Filippov	_bbci.l		abi_saved0, TIF_DB_DISABLED, 1f
5520b537257SMax Filippov	abi_call	restore_dbreak
553e7e9614bSMax Filippov1:
554c91e02bdSMax Filippov#endif
555a99e07eeSMax Filippov#ifdef CONFIG_DEBUG_TLB_SANITY
556961c5efbSMax Filippov	l32i		abi_tmp0, a1, PT_DEPC
557961c5efbSMax Filippov	bgeui		abi_tmp0, VALID_DOUBLE_EXCEPTION_ADDRESS, .Lrestore_state
5580b537257SMax Filippov	abi_call	check_tlb_sanity
559a99e07eeSMax Filippov#endif
560e7e9614bSMax Filippov
561e7e9614bSMax Filippov.Lrestore_state:
562aea8e7c8SMax Filippov#ifdef CONFIG_TRACE_IRQFLAGS
563961c5efbSMax Filippov	extui		abi_tmp0, abi_saved1, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
564961c5efbSMax Filippov	bgei		abi_tmp0, LOCKLEVEL, 1f
5650b537257SMax Filippov	abi_call	trace_hardirqs_on
566aea8e7c8SMax Filippov1:
567aea8e7c8SMax Filippov#endif
568961c5efbSMax Filippov	/*
569961c5efbSMax Filippov	 * Restore optional registers.
570961c5efbSMax Filippov	 * abi_arg* are used as temporary registers here.
571961c5efbSMax Filippov	 */
5725a0015d6SChris Zankel
573961c5efbSMax Filippov	load_xtregs_opt a1 abi_tmp0 abi_arg0 abi_arg1 abi_arg2 abi_arg3 PT_XTREGS_OPT
574e1088430SChris Zankel
575733536b8SMax Filippov	/* Restore SCOMPARE1 */
576733536b8SMax Filippov
577733536b8SMax Filippov#if XCHAL_HAVE_S32C1I
578961c5efbSMax Filippov	l32i		abi_tmp0, a1, PT_SCOMPARE1
579961c5efbSMax Filippov	wsr		abi_tmp0, scompare1
580733536b8SMax Filippov#endif
5810b537257SMax Filippov	wsr		abi_saved1, ps		/* disable interrupts */
5820b537257SMax Filippov	_bbci.l		abi_saved1, PS_UM_BIT, kernel_exception_exit
583e1088430SChris Zankel
584e1088430SChris Zankeluser_exception_exit:
585e1088430SChris Zankel
586e1088430SChris Zankel	/* Restore the state of the task and return from the exception. */
5875a0015d6SChris Zankel
5880b537257SMax Filippov#if defined(USER_SUPPORT_WINDOWED)
5895a0015d6SChris Zankel	/* Switch to the user thread WINDOWBASE. Save SP temporarily in DEPC */
5905a0015d6SChris Zankel
5915a0015d6SChris Zankel	l32i	a2, a1, PT_WINDOWBASE
5925a0015d6SChris Zankel	l32i	a3, a1, PT_WINDOWSTART
593bc5378fcSMax Filippov	wsr	a1, depc		# use DEPC as temp storage
594bc5378fcSMax Filippov	wsr	a3, windowstart		# restore WINDOWSTART
5955a0015d6SChris Zankel	ssr	a2			# preserve user's WB in the SAR
596bc5378fcSMax Filippov	wsr	a2, windowbase		# switch to user's saved WB
5975a0015d6SChris Zankel	rsync
598bc5378fcSMax Filippov	rsr	a1, depc		# restore stack pointer
5995a0015d6SChris Zankel	l32i	a2, a1, PT_WMASK	# register frames saved (in bits 4...9)
6005a0015d6SChris Zankel	rotw	-1			# we restore a4..a7
601e7e9614bSMax Filippov	_bltui	a6, 16, .Lclear_regs	# only have to restore current window?
6025a0015d6SChris Zankel
6035a0015d6SChris Zankel	/* The working registers are a0 and a3.  We are restoring to
6045a0015d6SChris Zankel	 * a4..a7.  Be careful not to destroy what we have just restored.
6055a0015d6SChris Zankel	 * Note: wmask has the format YYYYM:
6065a0015d6SChris Zankel	 *       Y: number of registers saved in groups of 4
6075a0015d6SChris Zankel	 *       M: 4 bit mask of first 16 registers
6085a0015d6SChris Zankel	 */
6095a0015d6SChris Zankel
6105a0015d6SChris Zankel	mov	a2, a6
6115a0015d6SChris Zankel	mov	a3, a5
6125a0015d6SChris Zankel
613e7e9614bSMax Filippov1:	rotw	-1			# a0..a3 become a4..a7
6145a0015d6SChris Zankel	addi	a3, a7, -4*4		# next iteration
6155a0015d6SChris Zankel	addi	a2, a6, -16		# decrementing Y in WMASK
6165a0015d6SChris Zankel	l32i	a4, a3, PT_AREG_END + 0
6175a0015d6SChris Zankel	l32i	a5, a3, PT_AREG_END + 4
6185a0015d6SChris Zankel	l32i	a6, a3, PT_AREG_END + 8
6195a0015d6SChris Zankel	l32i	a7, a3, PT_AREG_END + 12
620e7e9614bSMax Filippov	_bgeui	a2, 16, 1b
6215a0015d6SChris Zankel
6225a0015d6SChris Zankel	/* Clear unrestored registers (don't leak anything to user-land */
6235a0015d6SChris Zankel
624e7e9614bSMax Filippov.Lclear_regs:
625e7e9614bSMax Filippov	rsr	a0, windowbase
626bc5378fcSMax Filippov	rsr	a3, sar
6275a0015d6SChris Zankel	sub	a3, a0, a3
6285a0015d6SChris Zankel	beqz	a3, 2f
6295a0015d6SChris Zankel	extui	a3, a3, 0, WBBITS
6305a0015d6SChris Zankel
6315a0015d6SChris Zankel1:	rotw	-1
6325a0015d6SChris Zankel	addi	a3, a7, -1
6335a0015d6SChris Zankel	movi	a4, 0
6345a0015d6SChris Zankel	movi	a5, 0
6355a0015d6SChris Zankel	movi	a6, 0
6365a0015d6SChris Zankel	movi	a7, 0
6375a0015d6SChris Zankel	bgei	a3, 1, 1b
6385a0015d6SChris Zankel
6395a0015d6SChris Zankel	/* We are back were we were when we started.
6405a0015d6SChris Zankel	 * Note: a2 still contains WMASK (if we've returned to the original
6415a0015d6SChris Zankel	 *	 frame where we had loaded a2), or at least the lower 4 bits
6425a0015d6SChris Zankel	 *	 (if we have restored WSBITS-1 frames).
6435a0015d6SChris Zankel	 */
6444229fb12SMax Filippov2:
6450b537257SMax Filippov#else
6460b537257SMax Filippov	movi	a2, 1
6470b537257SMax Filippov#endif
648c50842dfSChris Zankel#if XCHAL_HAVE_THREADPTR
649c50842dfSChris Zankel	l32i	a3, a1, PT_THREADPTR
650c50842dfSChris Zankel	wur	a3, threadptr
651c50842dfSChris Zankel#endif
652c50842dfSChris Zankel
6534229fb12SMax Filippov	j	common_exception_exit
6545a0015d6SChris Zankel
6555a0015d6SChris Zankel	/* This is the kernel exception exit.
6565a0015d6SChris Zankel	 * We avoided to do a MOVSP when we entered the exception, but we
6575a0015d6SChris Zankel	 * have to do it here.
6585a0015d6SChris Zankel	 */
6595a0015d6SChris Zankel
6605a0015d6SChris Zankelkernel_exception_exit:
6615a0015d6SChris Zankel
6620b537257SMax Filippov#if defined(__XTENSA_WINDOWED_ABI__)
6635a0015d6SChris Zankel	/* Check if we have to do a movsp.
6645a0015d6SChris Zankel	 *
6655a0015d6SChris Zankel	 * We only have to do a movsp if the previous window-frame has
6665a0015d6SChris Zankel	 * been spilled to the *temporary* exception stack instead of the
6675a0015d6SChris Zankel	 * task's stack. This is the case if the corresponding bit in
6685a0015d6SChris Zankel	 * WINDOWSTART for the previous window-frame was set before
6695a0015d6SChris Zankel	 * (not spilled) but is zero now (spilled).
6705a0015d6SChris Zankel	 * If this bit is zero, all other bits except the one for the
6715a0015d6SChris Zankel	 * current window frame are also zero. So, we can use a simple test:
6725a0015d6SChris Zankel	 * 'and' WINDOWSTART and WINDOWSTART-1:
6735a0015d6SChris Zankel	 *
6745a0015d6SChris Zankel	 *  (XXXXXX1[0]* - 1) AND XXXXXX1[0]* = XXXXXX0[0]*
6755a0015d6SChris Zankel	 *
6765a0015d6SChris Zankel	 * The result is zero only if one bit was set.
6775a0015d6SChris Zankel	 *
6785a0015d6SChris Zankel	 * (Note: We might have gone through several task switches before
6795a0015d6SChris Zankel	 *        we come back to the current task, so WINDOWBASE might be
6805a0015d6SChris Zankel	 *        different from the time the exception occurred.)
6815a0015d6SChris Zankel	 */
6825a0015d6SChris Zankel
6835a0015d6SChris Zankel	/* Test WINDOWSTART before and after the exception.
6845a0015d6SChris Zankel	 * We actually have WMASK, so we only have to test if it is 1 or not.
6855a0015d6SChris Zankel	 */
6865a0015d6SChris Zankel
6875a0015d6SChris Zankel	l32i	a2, a1, PT_WMASK
6885a0015d6SChris Zankel	_beqi	a2, 1, common_exception_exit	# Spilled before exception,jump
6895a0015d6SChris Zankel
6905a0015d6SChris Zankel	/* Test WINDOWSTART now. If spilled, do the movsp */
6915a0015d6SChris Zankel
692bc5378fcSMax Filippov	rsr     a3, windowstart
6935a0015d6SChris Zankel	addi	a0, a3, -1
6945a0015d6SChris Zankel	and     a3, a3, a0
6955a0015d6SChris Zankel	_bnez	a3, common_exception_exit
6965a0015d6SChris Zankel
6975a0015d6SChris Zankel	/* Do a movsp (we returned from a call4, so we have at least a0..a7) */
6985a0015d6SChris Zankel
6995a0015d6SChris Zankel	addi    a0, a1, -16
7005a0015d6SChris Zankel	l32i    a3, a0, 0
7015a0015d6SChris Zankel	l32i    a4, a0, 4
7026fad9ddcSMax Filippov	s32i    a3, a1, PT_KERNEL_SIZE + 0
7036fad9ddcSMax Filippov	s32i    a4, a1, PT_KERNEL_SIZE + 4
7045a0015d6SChris Zankel	l32i    a3, a0, 8
7055a0015d6SChris Zankel	l32i    a4, a0, 12
7066fad9ddcSMax Filippov	s32i    a3, a1, PT_KERNEL_SIZE + 8
7076fad9ddcSMax Filippov	s32i    a4, a1, PT_KERNEL_SIZE + 12
7085a0015d6SChris Zankel
7095a0015d6SChris Zankel	/* Common exception exit.
7105a0015d6SChris Zankel	 * We restore the special register and the current window frame, and
7115a0015d6SChris Zankel	 * return from the exception.
7125a0015d6SChris Zankel	 *
7135a0015d6SChris Zankel	 * Note: We expect a2 to hold PT_WMASK
7145a0015d6SChris Zankel	 */
7150b537257SMax Filippov#else
7160b537257SMax Filippov	movi	a2, 1
7170b537257SMax Filippov#endif
7185a0015d6SChris Zankel
7195a0015d6SChris Zankelcommon_exception_exit:
7205a0015d6SChris Zankel
721c658eac6SChris Zankel	/* Restore address registers. */
722c658eac6SChris Zankel
7235a0015d6SChris Zankel	_bbsi.l	a2, 1, 1f
7245a0015d6SChris Zankel	l32i	a4,  a1, PT_AREG4
7255a0015d6SChris Zankel	l32i	a5,  a1, PT_AREG5
7265a0015d6SChris Zankel	l32i	a6,  a1, PT_AREG6
7275a0015d6SChris Zankel	l32i	a7,  a1, PT_AREG7
7285a0015d6SChris Zankel	_bbsi.l	a2, 2, 1f
7295a0015d6SChris Zankel	l32i	a8,  a1, PT_AREG8
7305a0015d6SChris Zankel	l32i	a9,  a1, PT_AREG9
7315a0015d6SChris Zankel	l32i	a10, a1, PT_AREG10
7325a0015d6SChris Zankel	l32i	a11, a1, PT_AREG11
7335a0015d6SChris Zankel	_bbsi.l	a2, 3, 1f
7345a0015d6SChris Zankel	l32i	a12, a1, PT_AREG12
7355a0015d6SChris Zankel	l32i	a13, a1, PT_AREG13
7365a0015d6SChris Zankel	l32i	a14, a1, PT_AREG14
7375a0015d6SChris Zankel	l32i	a15, a1, PT_AREG15
7385a0015d6SChris Zankel
7395a0015d6SChris Zankel	/* Restore PC, SAR */
7405a0015d6SChris Zankel
7415a0015d6SChris Zankel1:	l32i	a2, a1, PT_PC
7425a0015d6SChris Zankel	l32i	a3, a1, PT_SAR
743bc5378fcSMax Filippov	wsr	a2, epc1
744bc5378fcSMax Filippov	wsr	a3, sar
7455a0015d6SChris Zankel
7465a0015d6SChris Zankel	/* Restore LBEG, LEND, LCOUNT */
7475029615eSMax Filippov#if XCHAL_HAVE_LOOPS
7485a0015d6SChris Zankel	l32i	a2, a1, PT_LBEG
7495a0015d6SChris Zankel	l32i	a3, a1, PT_LEND
750bc5378fcSMax Filippov	wsr	a2, lbeg
7515a0015d6SChris Zankel	l32i	a2, a1, PT_LCOUNT
752bc5378fcSMax Filippov	wsr	a3, lend
753bc5378fcSMax Filippov	wsr	a2, lcount
7545029615eSMax Filippov#endif
7555a0015d6SChris Zankel
75629c4dfd9SChris Zankel	/* We control single stepping through the ICOUNTLEVEL register. */
75729c4dfd9SChris Zankel
75829c4dfd9SChris Zankel	l32i	a2, a1, PT_ICOUNTLEVEL
75929c4dfd9SChris Zankel	movi	a3, -2
760bc5378fcSMax Filippov	wsr	a2, icountlevel
761bc5378fcSMax Filippov	wsr	a3, icount
76229c4dfd9SChris Zankel
7635a0015d6SChris Zankel	/* Check if it was double exception. */
7645a0015d6SChris Zankel
7655a0015d6SChris Zankel	l32i	a0, a1, PT_DEPC
7665a0015d6SChris Zankel	l32i	a3, a1, PT_AREG3
7672d1c645cSMarc Gauthier	l32i	a2, a1, PT_AREG2
768895666a9SMax Filippov	_bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
7692d1c645cSMarc Gauthier
7705a0015d6SChris Zankel	/* Restore a0...a3 and return */
7715a0015d6SChris Zankel
7725a0015d6SChris Zankel	l32i	a0, a1, PT_AREG0
7735a0015d6SChris Zankel	l32i	a1, a1, PT_AREG1
7745a0015d6SChris Zankel	rfe
7755a0015d6SChris Zankel
776895666a9SMax Filippov1: 	wsr	a0, depc
777895666a9SMax Filippov	l32i	a0, a1, PT_AREG0
778895666a9SMax Filippov	l32i	a1, a1, PT_AREG1
779895666a9SMax Filippov	rfde
7805a0015d6SChris Zankel
781d1538c46SChris ZankelENDPROC(kernel_exception)
782d1538c46SChris Zankel
7835a0015d6SChris Zankel/*
7845a0015d6SChris Zankel * Debug exception handler.
7855a0015d6SChris Zankel *
7865a0015d6SChris Zankel * Currently, we don't support KGDB, so only user application can be debugged.
7875a0015d6SChris Zankel *
7885a0015d6SChris Zankel * When we get here,  a0 is trashed and saved to excsave[debuglevel]
7895a0015d6SChris Zankel */
7905a0015d6SChris Zankel
791f8f02ca7SMax Filippov	.literal_position
792f8f02ca7SMax Filippov
7935a0015d6SChris ZankelENTRY(debug_exception)
7945a0015d6SChris Zankel
795bc5378fcSMax Filippov	rsr	a0, SREG_EPS + XCHAL_DEBUGLEVEL
796*7f9c9741SMax Filippov	bbsi.l	a0, PS_EXCM_BIT, .Ldebug_exception_in_exception	# exception mode
7975a0015d6SChris Zankel
798bc5378fcSMax Filippov	/* Set EPC1 and EXCCAUSE */
7995a0015d6SChris Zankel
800bc5378fcSMax Filippov	wsr	a2, depc		# save a2 temporarily
801bc5378fcSMax Filippov	rsr	a2, SREG_EPC + XCHAL_DEBUGLEVEL
802bc5378fcSMax Filippov	wsr	a2, epc1
8035a0015d6SChris Zankel
8045a0015d6SChris Zankel	movi	a2, EXCCAUSE_MAPPED_DEBUG
805bc5378fcSMax Filippov	wsr	a2, exccause
8065a0015d6SChris Zankel
8075a0015d6SChris Zankel	/* Restore PS to the value before the debug exc but with PS.EXCM set.*/
8085a0015d6SChris Zankel
809173d6681SChris Zankel	movi	a2, 1 << PS_EXCM_BIT
8105a0015d6SChris Zankel	or	a2, a0, a2
811bc5378fcSMax Filippov	wsr	a2, ps
8125a0015d6SChris Zankel
8135a0015d6SChris Zankel	/* Switch to kernel/user stack, restore jump vector, and save a0 */
8145a0015d6SChris Zankel
815*7f9c9741SMax Filippov	bbsi.l	a2, PS_UM_BIT, .Ldebug_exception_user	# jump if user mode
8166fad9ddcSMax Filippov	addi	a2, a1, -16 - PT_KERNEL_SIZE	# assume kernel stack
817*7f9c9741SMax Filippov
818*7f9c9741SMax Filippov.Ldebug_exception_continue:
8196ec7026aSMax Filippov	l32i	a0, a3, DT_DEBUG_SAVE
8206ec7026aSMax Filippov	s32i	a1, a2, PT_AREG1
8215a0015d6SChris Zankel	s32i	a0, a2, PT_AREG0
8225a0015d6SChris Zankel	movi	a0, 0
8235a0015d6SChris Zankel	s32i	a0, a2, PT_DEPC		# mark it as a regular exception
8246ec7026aSMax Filippov	xsr	a3, SREG_EXCSAVE + XCHAL_DEBUGLEVEL
825bc5378fcSMax Filippov	xsr	a0, depc
8265a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
8275a0015d6SChris Zankel	s32i	a0, a2, PT_AREG2
8285a0015d6SChris Zankel	mov	a1, a2
8296ec7026aSMax Filippov
830c91e02bdSMax Filippov	/* Debug exception is handled as an exception, so interrupts will
831c91e02bdSMax Filippov	 * likely be enabled in the common exception handler. Disable
832c91e02bdSMax Filippov	 * preemption if we have HW breakpoints to preserve DEBUGCAUSE.DBNUM
833c91e02bdSMax Filippov	 * meaning.
834c91e02bdSMax Filippov	 */
835c91e02bdSMax Filippov#if defined(CONFIG_PREEMPT_COUNT) && defined(CONFIG_HAVE_HW_BREAKPOINT)
836c91e02bdSMax Filippov	GET_THREAD_INFO(a2, a1)
837c91e02bdSMax Filippov	l32i	a3, a2, TI_PRE_COUNT
838c91e02bdSMax Filippov	addi	a3, a3, 1
839c91e02bdSMax Filippov	s32i	a3, a2, TI_PRE_COUNT
840c91e02bdSMax Filippov#endif
841c91e02bdSMax Filippov
8426ec7026aSMax Filippov	rsr	a2, ps
8436ec7026aSMax Filippov	bbsi.l	a2, PS_UM_BIT, _user_exception
8445a0015d6SChris Zankel	j	_kernel_exception
8455a0015d6SChris Zankel
846*7f9c9741SMax Filippov.Ldebug_exception_user:
847*7f9c9741SMax Filippov	rsr	a2, excsave1
8485a0015d6SChris Zankel	l32i	a2, a2, EXC_TABLE_KSTK	# load kernel stack pointer
849*7f9c9741SMax Filippov	j	.Ldebug_exception_continue
8505a0015d6SChris Zankel
851*7f9c9741SMax Filippov.Ldebug_exception_in_exception:
852c91e02bdSMax Filippov#ifdef CONFIG_HAVE_HW_BREAKPOINT
853c91e02bdSMax Filippov	/* Debug exception while in exception mode. This may happen when
854c91e02bdSMax Filippov	 * window overflow/underflow handler or fast exception handler hits
855c91e02bdSMax Filippov	 * data breakpoint, in which case save and disable all data
856c91e02bdSMax Filippov	 * breakpoints, single-step faulting instruction and restore data
857c91e02bdSMax Filippov	 * breakpoints.
858c91e02bdSMax Filippov	 */
859*7f9c9741SMax Filippov
860*7f9c9741SMax Filippov	bbci.l	a0, PS_UM_BIT, .Ldebug_exception_in_exception	# jump if kernel mode
861c91e02bdSMax Filippov
862c91e02bdSMax Filippov	rsr	a0, debugcause
863c91e02bdSMax Filippov	bbsi.l	a0, DEBUGCAUSE_DBREAK_BIT, .Ldebug_save_dbreak
864c91e02bdSMax Filippov
865c91e02bdSMax Filippov	.set	_index, 0
866c91e02bdSMax Filippov	.rept	XCHAL_NUM_DBREAK
867c91e02bdSMax Filippov	l32i	a0, a3, DT_DBREAKC_SAVE + _index * 4
868c91e02bdSMax Filippov	wsr	a0, SREG_DBREAKC + _index
869c91e02bdSMax Filippov	.set	_index, _index + 1
870c91e02bdSMax Filippov	.endr
871c91e02bdSMax Filippov
872c91e02bdSMax Filippov	l32i	a0, a3, DT_ICOUNT_LEVEL_SAVE
873c91e02bdSMax Filippov	wsr	a0, icountlevel
874c91e02bdSMax Filippov
875c91e02bdSMax Filippov	l32i	a0, a3, DT_ICOUNT_SAVE
876c91e02bdSMax Filippov	xsr	a0, icount
877c91e02bdSMax Filippov
878c91e02bdSMax Filippov	l32i	a0, a3, DT_DEBUG_SAVE
879c91e02bdSMax Filippov	xsr	a3, SREG_EXCSAVE + XCHAL_DEBUGLEVEL
880c91e02bdSMax Filippov	rfi	XCHAL_DEBUGLEVEL
881c91e02bdSMax Filippov
882c91e02bdSMax Filippov.Ldebug_save_dbreak:
883c91e02bdSMax Filippov	.set	_index, 0
884c91e02bdSMax Filippov	.rept	XCHAL_NUM_DBREAK
885c91e02bdSMax Filippov	movi	a0, 0
886c91e02bdSMax Filippov	xsr	a0, SREG_DBREAKC + _index
887c91e02bdSMax Filippov	s32i	a0, a3, DT_DBREAKC_SAVE + _index * 4
888c91e02bdSMax Filippov	.set	_index, _index + 1
889c91e02bdSMax Filippov	.endr
890c91e02bdSMax Filippov
891c91e02bdSMax Filippov	movi	a0, XCHAL_EXCM_LEVEL + 1
892c91e02bdSMax Filippov	xsr	a0, icountlevel
893c91e02bdSMax Filippov	s32i	a0, a3, DT_ICOUNT_LEVEL_SAVE
894c91e02bdSMax Filippov
895c91e02bdSMax Filippov	movi	a0, 0xfffffffe
896c91e02bdSMax Filippov	xsr	a0, icount
897c91e02bdSMax Filippov	s32i	a0, a3, DT_ICOUNT_SAVE
898c91e02bdSMax Filippov
899c91e02bdSMax Filippov	l32i	a0, a3, DT_DEBUG_SAVE
900c91e02bdSMax Filippov	xsr	a3, SREG_EXCSAVE + XCHAL_DEBUGLEVEL
901c91e02bdSMax Filippov	rfi	XCHAL_DEBUGLEVEL
902c91e02bdSMax Filippov#else
903c91e02bdSMax Filippov	/* Debug exception while in exception mode. Should not happen. */
904*7f9c9741SMax Filippov	j	.Ldebug_exception_in_exception	// FIXME!!
905c91e02bdSMax Filippov#endif
9065a0015d6SChris Zankel
907d1538c46SChris ZankelENDPROC(debug_exception)
9085a0015d6SChris Zankel
9095a0015d6SChris Zankel/*
9105a0015d6SChris Zankel * We get here in case of an unrecoverable exception.
9115a0015d6SChris Zankel * The only thing we can do is to be nice and print a panic message.
9125a0015d6SChris Zankel * We only produce a single stack frame for panic, so ???
9135a0015d6SChris Zankel *
9145a0015d6SChris Zankel *
9155a0015d6SChris Zankel * Entry conditions:
9165a0015d6SChris Zankel *
9175a0015d6SChris Zankel *   - a0 contains the caller address; original value saved in excsave1.
9185a0015d6SChris Zankel *   - the original a0 contains a valid return address (backtrace) or 0.
9195a0015d6SChris Zankel *   - a2 contains a valid stackpointer
9205a0015d6SChris Zankel *
9215a0015d6SChris Zankel * Notes:
9225a0015d6SChris Zankel *
9235a0015d6SChris Zankel *   - If the stack pointer could be invalid, the caller has to setup a
9245a0015d6SChris Zankel *     dummy stack pointer (e.g. the stack of the init_task)
9255a0015d6SChris Zankel *
9265a0015d6SChris Zankel *   - If the return address could be invalid, the caller has to set it
9275a0015d6SChris Zankel *     to 0, so the backtrace would stop.
9285a0015d6SChris Zankel *
9295a0015d6SChris Zankel */
9305a0015d6SChris Zankel	.align 4
9315a0015d6SChris Zankelunrecoverable_text:
9325a0015d6SChris Zankel	.ascii "Unrecoverable error in exception handler\0"
9335a0015d6SChris Zankel
934f8f02ca7SMax Filippov	.literal_position
935f8f02ca7SMax Filippov
9365a0015d6SChris ZankelENTRY(unrecoverable_exception)
9375a0015d6SChris Zankel
93809af39f6SMax Filippov#if XCHAL_HAVE_WINDOWED
9395a0015d6SChris Zankel	movi	a0, 1
9405a0015d6SChris Zankel	movi	a1, 0
9415a0015d6SChris Zankel
942bc5378fcSMax Filippov	wsr	a0, windowstart
943bc5378fcSMax Filippov	wsr	a1, windowbase
9445a0015d6SChris Zankel	rsync
94509af39f6SMax Filippov#endif
9465a0015d6SChris Zankel
9470b537257SMax Filippov	movi	a1, KERNEL_PS_WOE_MASK | LOCKLEVEL
948bc5378fcSMax Filippov	wsr	a1, ps
9495a0015d6SChris Zankel	rsync
9505a0015d6SChris Zankel
9515a0015d6SChris Zankel	movi	a1, init_task
9525a0015d6SChris Zankel	movi	a0, 0
9535a0015d6SChris Zankel	addi	a1, a1, PT_REGS_OFFSET
9545a0015d6SChris Zankel
9550b537257SMax Filippov	movi	abi_arg0, unrecoverable_text
9560b537257SMax Filippov	abi_call	panic
9575a0015d6SChris Zankel
9585a0015d6SChris Zankel1:	j	1b
9595a0015d6SChris Zankel
960d1538c46SChris ZankelENDPROC(unrecoverable_exception)
9615a0015d6SChris Zankel
9625a0015d6SChris Zankel/* -------------------------- FAST EXCEPTION HANDLERS ----------------------- */
9635a0015d6SChris Zankel
96450722f0bSMax Filippov	__XTENSA_HANDLER
96550722f0bSMax Filippov	.literal_position
96650722f0bSMax Filippov
967da0a4e5cSMax Filippov#ifdef SUPPORT_WINDOWED
9685a0015d6SChris Zankel/*
9695a0015d6SChris Zankel * Fast-handler for alloca exceptions
9705a0015d6SChris Zankel *
9715a0015d6SChris Zankel *  The ALLOCA handler is entered when user code executes the MOVSP
9725a0015d6SChris Zankel *  instruction and the caller's frame is not in the register file.
9735a0015d6SChris Zankel *
974fff96d69SMax Filippov * This algorithm was taken from the Ross Morley's RTOS Porting Layer:
975fff96d69SMax Filippov *
976fff96d69SMax Filippov *    /home/ross/rtos/porting/XtensaRTOS-PortingLayer-20090507/xtensa_vectors.S
977fff96d69SMax Filippov *
978fff96d69SMax Filippov * It leverages the existing window spill/fill routines and their support for
979fff96d69SMax Filippov * double exceptions. The 'movsp' instruction will only cause an exception if
980fff96d69SMax Filippov * the next window needs to be loaded. In fact this ALLOCA exception may be
981fff96d69SMax Filippov * replaced at some point by changing the hardware to do a underflow exception
982fff96d69SMax Filippov * of the proper size instead.
983fff96d69SMax Filippov *
984fff96d69SMax Filippov * This algorithm simply backs out the register changes started by the user
9853ead2f97SChris Packham * exception handler, makes it appear that we have started a window underflow
986fff96d69SMax Filippov * by rotating the window back and then setting the old window base (OWB) in
987fff96d69SMax Filippov * the 'ps' register with the rolled back window base. The 'movsp' instruction
988fff96d69SMax Filippov * will be re-executed and this time since the next window frames is in the
989fff96d69SMax Filippov * active AR registers it won't cause an exception.
990fff96d69SMax Filippov *
991fff96d69SMax Filippov * If the WindowUnderflow code gets a TLB miss the page will get mapped
9923ead2f97SChris Packham * the partial WindowUnderflow will be handled in the double exception
993fff96d69SMax Filippov * handler.
9945a0015d6SChris Zankel *
9955a0015d6SChris Zankel * Entry condition:
9965a0015d6SChris Zankel *
9975a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
9985a0015d6SChris Zankel *   a1:	a1
9995a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
100099d5040eSMax Filippov *   a3:	a3
10015a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
100299d5040eSMax Filippov *   excsave_1:	dispatch table
10035a0015d6SChris Zankel *
10045a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
10055a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
10065a0015d6SChris Zankel */
10075a0015d6SChris Zankel
10085a0015d6SChris ZankelENTRY(fast_alloca)
1009fff96d69SMax Filippov	rsr	a0, windowbase
1010fff96d69SMax Filippov	rotw	-1
1011fff96d69SMax Filippov	rsr	a2, ps
1012fff96d69SMax Filippov	extui	a3, a2, PS_OWB_SHIFT, PS_OWB_WIDTH
1013fff96d69SMax Filippov	xor	a3, a3, a4
1014fff96d69SMax Filippov	l32i	a4, a6, PT_AREG0
1015fff96d69SMax Filippov	l32i	a1, a6, PT_DEPC
1016fff96d69SMax Filippov	rsr	a6, depc
1017fff96d69SMax Filippov	wsr	a1, depc
1018fff96d69SMax Filippov	slli	a3, a3, PS_OWB_SHIFT
1019fff96d69SMax Filippov	xor	a2, a2, a3
1020fff96d69SMax Filippov	wsr	a2, ps
1021fff96d69SMax Filippov	rsync
10225a0015d6SChris Zankel
1023fff96d69SMax Filippov	_bbci.l	a4, 31, 4f
1024fff96d69SMax Filippov	rotw	-1
1025fff96d69SMax Filippov	_bbci.l	a8, 30, 8f
1026fff96d69SMax Filippov	rotw	-1
1027fff96d69SMax Filippov	j	_WindowUnderflow12
1028fff96d69SMax Filippov8:	j	_WindowUnderflow8
1029fff96d69SMax Filippov4:	j	_WindowUnderflow4
1030d1538c46SChris ZankelENDPROC(fast_alloca)
1031da0a4e5cSMax Filippov#endif
10325a0015d6SChris Zankel
103309f8a6dbSMax Filippov#ifdef CONFIG_USER_ABI_CALL0_PROBE
103409f8a6dbSMax Filippov/*
103509f8a6dbSMax Filippov * fast illegal instruction handler.
103609f8a6dbSMax Filippov *
103709f8a6dbSMax Filippov * This is used to fix up user PS.WOE on the exception caused
103809f8a6dbSMax Filippov * by the first opcode related to register window. If PS.WOE is
103909f8a6dbSMax Filippov * already set it goes directly to the common user exception handler.
104009f8a6dbSMax Filippov *
104109f8a6dbSMax Filippov * Entry condition:
104209f8a6dbSMax Filippov *
104309f8a6dbSMax Filippov *   a0:	trashed, original value saved on stack (PT_AREG0)
104409f8a6dbSMax Filippov *   a1:	a1
104509f8a6dbSMax Filippov *   a2:	new stack pointer, original in DEPC
104609f8a6dbSMax Filippov *   a3:	a3
104709f8a6dbSMax Filippov *   depc:	a2, original value saved on stack (PT_DEPC)
104809f8a6dbSMax Filippov *   excsave_1:	dispatch table
104909f8a6dbSMax Filippov */
105009f8a6dbSMax Filippov
105109f8a6dbSMax FilippovENTRY(fast_illegal_instruction_user)
105209f8a6dbSMax Filippov
105309f8a6dbSMax Filippov	rsr	a0, ps
105450722f0bSMax Filippov	bbsi.l	a0, PS_WOE_BIT, 1f
105509f8a6dbSMax Filippov	s32i	a3, a2, PT_AREG3
105609f8a6dbSMax Filippov	movi	a3, PS_WOE_MASK
105709f8a6dbSMax Filippov	or	a0, a0, a3
105809f8a6dbSMax Filippov	wsr	a0, ps
105909f8a6dbSMax Filippov	l32i	a3, a2, PT_AREG3
106009f8a6dbSMax Filippov	l32i	a0, a2, PT_AREG0
106109f8a6dbSMax Filippov	rsr	a2, depc
106209f8a6dbSMax Filippov	rfe
106350722f0bSMax Filippov1:
106450722f0bSMax Filippov	call0	user_exception
106509f8a6dbSMax Filippov
106609f8a6dbSMax FilippovENDPROC(fast_illegal_instruction_user)
106709f8a6dbSMax Filippov#endif
106809f8a6dbSMax Filippov
10695a0015d6SChris Zankel	/*
10705a0015d6SChris Zankel * fast system calls.
10715a0015d6SChris Zankel *
10725a0015d6SChris Zankel * WARNING:  The kernel doesn't save the entire user context before
10735a0015d6SChris Zankel * handling a fast system call.  These functions are small and short,
10745a0015d6SChris Zankel * usually offering some functionality not available to user tasks.
10755a0015d6SChris Zankel *
10765a0015d6SChris Zankel * BE CAREFUL TO PRESERVE THE USER'S CONTEXT.
10775a0015d6SChris Zankel *
10785a0015d6SChris Zankel * Entry condition:
10795a0015d6SChris Zankel *
10805a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
10815a0015d6SChris Zankel *   a1:	a1
10825a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
108399d5040eSMax Filippov *   a3:	a3
10845a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
108599d5040eSMax Filippov *   excsave_1:	dispatch table
10865a0015d6SChris Zankel */
10875a0015d6SChris Zankel
10885a0015d6SChris ZankelENTRY(fast_syscall_user)
10895a0015d6SChris Zankel
10905a0015d6SChris Zankel	/* Skip syscall. */
10915a0015d6SChris Zankel
1092bc5378fcSMax Filippov	rsr	a0, epc1
10935a0015d6SChris Zankel	addi	a0, a0, 3
1094bc5378fcSMax Filippov	wsr	a0, epc1
10955a0015d6SChris Zankel
10965a0015d6SChris Zankel	l32i	a0, a2, PT_DEPC
10975a0015d6SChris Zankel	bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, fast_syscall_unrecoverable
10985a0015d6SChris Zankel
1099bc5378fcSMax Filippov	rsr	a0, depc			# get syscall-nr
11005a0015d6SChris Zankel	_beqz	a0, fast_syscall_spill_registers
1101fc4fb2adSChris Zankel	_beqi	a0, __NR_xtensa, fast_syscall_xtensa
11025a0015d6SChris Zankel
110350722f0bSMax Filippov	call0	user_exception
11045a0015d6SChris Zankel
1105d1538c46SChris ZankelENDPROC(fast_syscall_user)
1106d1538c46SChris Zankel
11075a0015d6SChris ZankelENTRY(fast_syscall_unrecoverable)
11085a0015d6SChris Zankel
11095a0015d6SChris Zankel	/* Restore all states. */
11105a0015d6SChris Zankel
11115a0015d6SChris Zankel	l32i    a0, a2, PT_AREG0        # restore a0
1112bc5378fcSMax Filippov	xsr     a2, depc                # restore a2, depc
11135a0015d6SChris Zankel
1114bc5378fcSMax Filippov	wsr     a0, excsave1
11152da03d41SMax Filippov	call0	unrecoverable_exception
11165a0015d6SChris Zankel
1117d1538c46SChris ZankelENDPROC(fast_syscall_unrecoverable)
11185a0015d6SChris Zankel
11195a0015d6SChris Zankel/*
11205a0015d6SChris Zankel * sysxtensa syscall handler
11215a0015d6SChris Zankel *
1122fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_SET,     ptr, val,    unused);
1123fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_ADD,     ptr, val,    unused);
1124fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_EXG_ADD, ptr, val,    unused);
1125fc4fb2adSChris Zankel * int sysxtensa (SYS_XTENSA_ATOMIC_CMP_SWP, ptr, oldval, newval);
11265a0015d6SChris Zankel *        a2            a6                   a3    a4      a5
11275a0015d6SChris Zankel *
11285a0015d6SChris Zankel * Entry condition:
11295a0015d6SChris Zankel *
1130fc4fb2adSChris Zankel *   a0:	a2 (syscall-nr), original value saved on stack (PT_AREG0)
11315a0015d6SChris Zankel *   a1:	a1
1132fc4fb2adSChris Zankel *   a2:	new stack pointer, original in a0 and DEPC
113399d5040eSMax Filippov *   a3:	a3
1134fc4fb2adSChris Zankel *   a4..a15:	unchanged
11355a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
113699d5040eSMax Filippov *   excsave_1:	dispatch table
11375a0015d6SChris Zankel *
11385a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
11395a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
11405a0015d6SChris Zankel *
11415a0015d6SChris Zankel * Note: we don't have to save a2; a2 holds the return value
11425a0015d6SChris Zankel */
11435a0015d6SChris Zankel
1144f8f02ca7SMax Filippov	.literal_position
1145f8f02ca7SMax Filippov
11469184289cSMax Filippov#ifdef CONFIG_FAST_SYSCALL_XTENSA
11479184289cSMax Filippov
1148fc4fb2adSChris ZankelENTRY(fast_syscall_xtensa)
11495a0015d6SChris Zankel
1150fc4fb2adSChris Zankel	s32i	a7, a2, PT_AREG7	# we need an additional register
11515a0015d6SChris Zankel	movi	a7, 4			# sizeof(unsigned int)
1152fc4fb2adSChris Zankel	access_ok a3, a7, a0, a2, .Leac	# a0: scratch reg, a2: sp
11535a0015d6SChris Zankel
1154d1b6ba82SMax Filippov	_bgeui	a6, SYS_XTENSA_COUNT, .Lill
1155d1b6ba82SMax Filippov	_bnei	a6, SYS_XTENSA_ATOMIC_CMP_SWP, .Lnswp
11565a0015d6SChris Zankel
1157fc4fb2adSChris Zankel	/* Fall through for ATOMIC_CMP_SWP. */
11585a0015d6SChris Zankel
11595a0015d6SChris Zankel.Lswp:	/* Atomic compare and swap */
11605a0015d6SChris Zankel
11610013acebSMax FilippovEX(.Leac) l32i	a0, a3, 0		# read old value
1162fc4fb2adSChris Zankel	bne	a0, a4, 1f		# same as old value? jump
11630013acebSMax FilippovEX(.Leac) s32i	a5, a3, 0		# different, modify value
1164fc4fb2adSChris Zankel	l32i	a7, a2, PT_AREG7	# restore a7
1165fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1166fc4fb2adSChris Zankel	movi	a2, 1			# and return 1
1167fc4fb2adSChris Zankel	rfe
11685a0015d6SChris Zankel
1169fc4fb2adSChris Zankel1:	l32i	a7, a2, PT_AREG7	# restore a7
1170fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1171fc4fb2adSChris Zankel	movi	a2, 0			# return 0 (note that we cannot set
1172fc4fb2adSChris Zankel	rfe
11735a0015d6SChris Zankel
1174fc4fb2adSChris Zankel.Lnswp:	/* Atomic set, add, and exg_add. */
11755a0015d6SChris Zankel
11760013acebSMax FilippovEX(.Leac) l32i	a7, a3, 0		# orig
1177d1b6ba82SMax Filippov	addi	a6, a6, -SYS_XTENSA_ATOMIC_SET
1178fc4fb2adSChris Zankel	add	a0, a4, a7		# + arg
1179fc4fb2adSChris Zankel	moveqz	a0, a4, a6		# set
1180d1b6ba82SMax Filippov	addi	a6, a6, SYS_XTENSA_ATOMIC_SET
11810013acebSMax FilippovEX(.Leac) s32i	a0, a3, 0		# write new value
11825a0015d6SChris Zankel
1183fc4fb2adSChris Zankel	mov	a0, a2
11845a0015d6SChris Zankel	mov	a2, a7
1185fc4fb2adSChris Zankel	l32i	a7, a0, PT_AREG7	# restore a7
1186fc4fb2adSChris Zankel	l32i	a0, a0, PT_AREG0	# restore a0
11875a0015d6SChris Zankel	rfe
11885a0015d6SChris Zankel
1189fc4fb2adSChris Zankel.Leac:	l32i	a7, a2, PT_AREG7	# restore a7
1190fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1191fc4fb2adSChris Zankel	movi	a2, -EFAULT
1192fc4fb2adSChris Zankel	rfe
1193fc4fb2adSChris Zankel
1194d1b6ba82SMax Filippov.Lill:	l32i	a7, a2, PT_AREG7	# restore a7
1195fc4fb2adSChris Zankel	l32i	a0, a2, PT_AREG0	# restore a0
1196fc4fb2adSChris Zankel	movi	a2, -EINVAL
1197fc4fb2adSChris Zankel	rfe
1198fc4fb2adSChris Zankel
1199d1538c46SChris ZankelENDPROC(fast_syscall_xtensa)
12005a0015d6SChris Zankel
12019184289cSMax Filippov#else /* CONFIG_FAST_SYSCALL_XTENSA */
12029184289cSMax Filippov
12039184289cSMax FilippovENTRY(fast_syscall_xtensa)
12049184289cSMax Filippov
12059184289cSMax Filippov	l32i    a0, a2, PT_AREG0        # restore a0
12069184289cSMax Filippov	movi	a2, -ENOSYS
12079184289cSMax Filippov	rfe
12089184289cSMax Filippov
12099184289cSMax FilippovENDPROC(fast_syscall_xtensa)
12109184289cSMax Filippov
12119184289cSMax Filippov#endif /* CONFIG_FAST_SYSCALL_XTENSA */
12129184289cSMax Filippov
12135a0015d6SChris Zankel
12145a0015d6SChris Zankel/* fast_syscall_spill_registers.
12155a0015d6SChris Zankel *
12165a0015d6SChris Zankel * Entry condition:
12175a0015d6SChris Zankel *
12185a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
12195a0015d6SChris Zankel *   a1:	a1
12205a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
122199d5040eSMax Filippov *   a3:	a3
12225a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
122399d5040eSMax Filippov *   excsave_1:	dispatch table
12245a0015d6SChris Zankel *
12255a0015d6SChris Zankel * Note: We assume the stack pointer is EXC_TABLE_KSTK in the fixup handler.
12265a0015d6SChris Zankel */
12275a0015d6SChris Zankel
1228da0a4e5cSMax Filippov#if defined(CONFIG_FAST_SYSCALL_SPILL_REGISTERS) && \
1229da0a4e5cSMax Filippov		defined(USER_SUPPORT_WINDOWED)
12309184289cSMax Filippov
12315a0015d6SChris ZankelENTRY(fast_syscall_spill_registers)
12325a0015d6SChris Zankel
12335a0015d6SChris Zankel	/* Register a FIXUP handler (pass current wb as a parameter) */
12345a0015d6SChris Zankel
123599d5040eSMax Filippov	xsr	a3, excsave1
12365a0015d6SChris Zankel	movi	a0, fast_syscall_spill_registers_fixup
12375a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_FIXUP
1238bc5378fcSMax Filippov	rsr	a0, windowbase
12395a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_PARAM
124099d5040eSMax Filippov	xsr	a3, excsave1		# restore a3 and excsave_1
12415a0015d6SChris Zankel
124299d5040eSMax Filippov	/* Save a3, a4 and SAR on stack. */
12435a0015d6SChris Zankel
1244bc5378fcSMax Filippov	rsr	a0, sar
12455a0015d6SChris Zankel	s32i	a3, a2, PT_AREG3
12466b5a1f74SChris Zankel	s32i	a0, a2, PT_SAR
12476b5a1f74SChris Zankel
12486b5a1f74SChris Zankel	/* The spill routine might clobber a4, a7, a8, a11, a12, and a15. */
12496b5a1f74SChris Zankel
1250c658eac6SChris Zankel	s32i	a4, a2, PT_AREG4
1251c658eac6SChris Zankel	s32i	a7, a2, PT_AREG7
12526b5a1f74SChris Zankel	s32i	a8, a2, PT_AREG8
1253c658eac6SChris Zankel	s32i	a11, a2, PT_AREG11
12546b5a1f74SChris Zankel	s32i	a12, a2, PT_AREG12
1255c658eac6SChris Zankel	s32i	a15, a2, PT_AREG15
12565a0015d6SChris Zankel
12576b5a1f74SChris Zankel	/*
12586b5a1f74SChris Zankel	 * Rotate ws so that the current windowbase is at bit 0.
12596b5a1f74SChris Zankel	 * Assume ws = xxxwww1yy (www1 current window frame).
12606b5a1f74SChris Zankel	 * Rotate ws right so that a4 = yyxxxwww1.
12616b5a1f74SChris Zankel	 */
12626b5a1f74SChris Zankel
12636b5a1f74SChris Zankel	rsr	a0, windowbase
12646b5a1f74SChris Zankel	rsr	a3, windowstart		# a3 = xxxwww1yy
12656b5a1f74SChris Zankel	ssr	a0			# holds WB
12666b5a1f74SChris Zankel	slli	a0, a3, WSBITS
12676b5a1f74SChris Zankel	or	a3, a3, a0		# a3 = xxxwww1yyxxxwww1yy
12686b5a1f74SChris Zankel	srl	a3, a3			# a3 = 00xxxwww1yyxxxwww1
12696b5a1f74SChris Zankel
12706b5a1f74SChris Zankel	/* We are done if there are no more than the current register frame. */
12716b5a1f74SChris Zankel
12726b5a1f74SChris Zankel	extui	a3, a3, 1, WSBITS-1	# a3 = 0yyxxxwww
12736b5a1f74SChris Zankel	movi	a0, (1 << (WSBITS-1))
12746b5a1f74SChris Zankel	_beqz	a3, .Lnospill		# only one active frame? jump
12756b5a1f74SChris Zankel
12766b5a1f74SChris Zankel	/* We want 1 at the top, so that we return to the current windowbase */
12776b5a1f74SChris Zankel
12786b5a1f74SChris Zankel	or	a3, a3, a0		# 1yyxxxwww
12796b5a1f74SChris Zankel
12806b5a1f74SChris Zankel	/* Skip empty frames - get 'oldest' WINDOWSTART-bit. */
12816b5a1f74SChris Zankel
12826b5a1f74SChris Zankel	wsr	a3, windowstart		# save shifted windowstart
12836b5a1f74SChris Zankel	neg	a0, a3
12846b5a1f74SChris Zankel	and	a3, a0, a3		# first bit set from right: 000010000
12856b5a1f74SChris Zankel
12866b5a1f74SChris Zankel	ffs_ws	a0, a3			# a0: shifts to skip empty frames
12876b5a1f74SChris Zankel	movi	a3, WSBITS
12886b5a1f74SChris Zankel	sub	a0, a3, a0		# WSBITS-a0:number of 0-bits from right
12896b5a1f74SChris Zankel	ssr	a0			# save in SAR for later.
12906b5a1f74SChris Zankel
12916b5a1f74SChris Zankel	rsr	a3, windowbase
12926b5a1f74SChris Zankel	add	a3, a3, a0
12936b5a1f74SChris Zankel	wsr	a3, windowbase
12946b5a1f74SChris Zankel	rsync
12956b5a1f74SChris Zankel
12966b5a1f74SChris Zankel	rsr	a3, windowstart
12976b5a1f74SChris Zankel	srl	a3, a3			# shift windowstart
12986b5a1f74SChris Zankel
12996b5a1f74SChris Zankel	/* WB is now just one frame below the oldest frame in the register
13006b5a1f74SChris Zankel	   window. WS is shifted so the oldest frame is in bit 0, thus, WB
13016b5a1f74SChris Zankel	   and WS differ by one 4-register frame. */
13026b5a1f74SChris Zankel
13036b5a1f74SChris Zankel	/* Save frames. Depending what call was used (call4, call8, call12),
13046b5a1f74SChris Zankel	 * we have to save 4,8. or 12 registers.
13056b5a1f74SChris Zankel	 */
13066b5a1f74SChris Zankel
13076b5a1f74SChris Zankel
13086b5a1f74SChris Zankel.Lloop: _bbsi.l	a3, 1, .Lc4
13096b5a1f74SChris Zankel	_bbci.l	a3, 2, .Lc12
13106b5a1f74SChris Zankel
13116b5a1f74SChris Zankel.Lc8:	s32e	a4, a13, -16
13126b5a1f74SChris Zankel	l32e	a4, a5, -12
13136b5a1f74SChris Zankel	s32e	a8, a4, -32
13146b5a1f74SChris Zankel	s32e	a5, a13, -12
13156b5a1f74SChris Zankel	s32e	a6, a13, -8
13166b5a1f74SChris Zankel	s32e	a7, a13, -4
13176b5a1f74SChris Zankel	s32e	a9, a4, -28
13186b5a1f74SChris Zankel	s32e	a10, a4, -24
13196b5a1f74SChris Zankel	s32e	a11, a4, -20
13206b5a1f74SChris Zankel	srli	a11, a3, 2		# shift windowbase by 2
13216b5a1f74SChris Zankel	rotw	2
13226b5a1f74SChris Zankel	_bnei	a3, 1, .Lloop
13236b5a1f74SChris Zankel	j	.Lexit
13246b5a1f74SChris Zankel
13256b5a1f74SChris Zankel.Lc4:	s32e	a4, a9, -16
13266b5a1f74SChris Zankel	s32e	a5, a9, -12
13276b5a1f74SChris Zankel	s32e	a6, a9, -8
13286b5a1f74SChris Zankel	s32e	a7, a9, -4
13296b5a1f74SChris Zankel
13306b5a1f74SChris Zankel	srli	a7, a3, 1
13316b5a1f74SChris Zankel	rotw	1
13326b5a1f74SChris Zankel	_bnei	a3, 1, .Lloop
13336b5a1f74SChris Zankel	j	.Lexit
13346b5a1f74SChris Zankel
13356b5a1f74SChris Zankel.Lc12:	_bbci.l	a3, 3, .Linvalid_mask	# bit 2 shouldn't be zero!
13366b5a1f74SChris Zankel
13376b5a1f74SChris Zankel	/* 12-register frame (call12) */
13386b5a1f74SChris Zankel
13396b5a1f74SChris Zankel	l32e	a0, a5, -12
13406b5a1f74SChris Zankel	s32e	a8, a0, -48
13416b5a1f74SChris Zankel	mov	a8, a0
13426b5a1f74SChris Zankel
13436b5a1f74SChris Zankel	s32e	a9, a8, -44
13446b5a1f74SChris Zankel	s32e	a10, a8, -40
13456b5a1f74SChris Zankel	s32e	a11, a8, -36
13466b5a1f74SChris Zankel	s32e	a12, a8, -32
13476b5a1f74SChris Zankel	s32e	a13, a8, -28
13486b5a1f74SChris Zankel	s32e	a14, a8, -24
13496b5a1f74SChris Zankel	s32e	a15, a8, -20
13506b5a1f74SChris Zankel	srli	a15, a3, 3
13516b5a1f74SChris Zankel
13526b5a1f74SChris Zankel	/* The stack pointer for a4..a7 is out of reach, so we rotate the
13536b5a1f74SChris Zankel	 * window, grab the stackpointer, and rotate back.
13546b5a1f74SChris Zankel	 * Alternatively, we could also use the following approach, but that
13556b5a1f74SChris Zankel	 * makes the fixup routine much more complicated:
13566b5a1f74SChris Zankel	 * rotw	1
13576b5a1f74SChris Zankel	 * s32e	a0, a13, -16
13586b5a1f74SChris Zankel	 * ...
13596b5a1f74SChris Zankel	 * rotw 2
13606b5a1f74SChris Zankel	 */
13616b5a1f74SChris Zankel
13626b5a1f74SChris Zankel	rotw	1
13636b5a1f74SChris Zankel	mov	a4, a13
13646b5a1f74SChris Zankel	rotw	-1
13656b5a1f74SChris Zankel
13666b5a1f74SChris Zankel	s32e	a4, a8, -16
13676b5a1f74SChris Zankel	s32e	a5, a8, -12
13686b5a1f74SChris Zankel	s32e	a6, a8, -8
13696b5a1f74SChris Zankel	s32e	a7, a8, -4
13706b5a1f74SChris Zankel
13716b5a1f74SChris Zankel	rotw	3
13726b5a1f74SChris Zankel
13736b5a1f74SChris Zankel	_beqi	a3, 1, .Lexit
13746b5a1f74SChris Zankel	j	.Lloop
13756b5a1f74SChris Zankel
13766b5a1f74SChris Zankel.Lexit:
13776b5a1f74SChris Zankel
13786b5a1f74SChris Zankel	/* Done. Do the final rotation and set WS */
13796b5a1f74SChris Zankel
13806b5a1f74SChris Zankel	rotw	1
13816b5a1f74SChris Zankel	rsr	a3, windowbase
13826b5a1f74SChris Zankel	ssl	a3
13836b5a1f74SChris Zankel	movi	a3, 1
13846b5a1f74SChris Zankel	sll	a3, a3
13856b5a1f74SChris Zankel	wsr	a3, windowstart
13866b5a1f74SChris Zankel.Lnospill:
13875a0015d6SChris Zankel
13885a0015d6SChris Zankel	/* Advance PC, restore registers and SAR, and return from exception. */
13895a0015d6SChris Zankel
13906b5a1f74SChris Zankel	l32i	a3, a2, PT_SAR
13915a0015d6SChris Zankel	l32i	a0, a2, PT_AREG0
1392bc5378fcSMax Filippov	wsr	a3, sar
13935a0015d6SChris Zankel	l32i	a3, a2, PT_AREG3
13945a0015d6SChris Zankel
13955a0015d6SChris Zankel	/* Restore clobbered registers. */
13965a0015d6SChris Zankel
13976b5a1f74SChris Zankel	l32i	a4, a2, PT_AREG4
1398c658eac6SChris Zankel	l32i	a7, a2, PT_AREG7
13996b5a1f74SChris Zankel	l32i	a8, a2, PT_AREG8
1400c658eac6SChris Zankel	l32i	a11, a2, PT_AREG11
14016b5a1f74SChris Zankel	l32i	a12, a2, PT_AREG12
1402c658eac6SChris Zankel	l32i	a15, a2, PT_AREG15
14035a0015d6SChris Zankel
14045a0015d6SChris Zankel	movi	a2, 0
14055a0015d6SChris Zankel	rfe
14065a0015d6SChris Zankel
14076b5a1f74SChris Zankel.Linvalid_mask:
14086b5a1f74SChris Zankel
14096b5a1f74SChris Zankel	/* We get here because of an unrecoverable error in the window
14106b5a1f74SChris Zankel	 * registers, so set up a dummy frame and kill the user application.
14116b5a1f74SChris Zankel	 * Note: We assume EXC_TABLE_KSTK contains a valid stack pointer.
14126b5a1f74SChris Zankel	 */
14136b5a1f74SChris Zankel
14146b5a1f74SChris Zankel	movi	a0, 1
14156b5a1f74SChris Zankel	movi	a1, 0
14166b5a1f74SChris Zankel
14176b5a1f74SChris Zankel	wsr	a0, windowstart
14186b5a1f74SChris Zankel	wsr	a1, windowbase
14196b5a1f74SChris Zankel	rsync
14206b5a1f74SChris Zankel
14216b5a1f74SChris Zankel	movi	a0, 0
14226b5a1f74SChris Zankel
14236b5a1f74SChris Zankel	rsr	a3, excsave1
14246b5a1f74SChris Zankel	l32i	a1, a3, EXC_TABLE_KSTK
14256b5a1f74SChris Zankel
14260b537257SMax Filippov	movi	a4, KERNEL_PS_WOE_MASK | LOCKLEVEL
14276b5a1f74SChris Zankel	wsr	a4, ps
14286b5a1f74SChris Zankel	rsync
14296b5a1f74SChris Zankel
14300b537257SMax Filippov	movi	abi_arg0, SIGSEGV
143185be9ae7SEric W. Biederman	abi_call	make_task_dead
14326b5a1f74SChris Zankel
14336b5a1f74SChris Zankel	/* shouldn't return, so panic */
14346b5a1f74SChris Zankel
14356b5a1f74SChris Zankel	wsr	a0, excsave1
14362da03d41SMax Filippov	call0	unrecoverable_exception		# should not return
14376b5a1f74SChris Zankel1:	j	1b
14386b5a1f74SChris Zankel
14396b5a1f74SChris Zankel
1440d1538c46SChris ZankelENDPROC(fast_syscall_spill_registers)
1441d1538c46SChris Zankel
14425a0015d6SChris Zankel/* Fixup handler.
14435a0015d6SChris Zankel *
14445a0015d6SChris Zankel * We get here if the spill routine causes an exception, e.g. tlb miss.
14455a0015d6SChris Zankel * We basically restore WINDOWBASE and WINDOWSTART to the condition when
14465a0015d6SChris Zankel * we entered the spill routine and jump to the user exception handler.
14475a0015d6SChris Zankel *
14483251f1e2SMax Filippov * Note that we only need to restore the bits in windowstart that have not
14493251f1e2SMax Filippov * been spilled yet by the _spill_register routine. Luckily, a3 contains a
14503251f1e2SMax Filippov * rotated windowstart with only those bits set for frames that haven't been
14513251f1e2SMax Filippov * spilled yet. Because a3 is rotated such that bit 0 represents the register
14523251f1e2SMax Filippov * frame for the current windowbase - 1, we need to rotate a3 left by the
14533251f1e2SMax Filippov * value of the current windowbase + 1 and move it to windowstart.
14543251f1e2SMax Filippov *
14555a0015d6SChris Zankel * a0: value of depc, original value in depc
14565a0015d6SChris Zankel * a2: trashed, original value in EXC_TABLE_DOUBLE_SAVE
14575a0015d6SChris Zankel * a3: exctable, original value in excsave1
14585a0015d6SChris Zankel */
14595a0015d6SChris Zankel
1460244066f4SMax FilippovENTRY(fast_syscall_spill_registers_fixup)
14615a0015d6SChris Zankel
1462bc5378fcSMax Filippov	rsr	a2, windowbase	# get current windowbase (a2 is saved)
1463bc5378fcSMax Filippov	xsr	a0, depc	# restore depc and a0
14645a0015d6SChris Zankel	ssl	a2		# set shift (32 - WB)
14655a0015d6SChris Zankel
14665a0015d6SChris Zankel	/* We need to make sure the current registers (a0-a3) are preserved.
14675a0015d6SChris Zankel	 * To do this, we simply set the bit for the current window frame
14685a0015d6SChris Zankel	 * in WS, so that the exception handlers save them to the task stack.
14693251f1e2SMax Filippov	 *
14703251f1e2SMax Filippov	 * Note: we use a3 to set the windowbase, so we take a special care
14713251f1e2SMax Filippov	 * of it, saving it in the original _spill_registers frame across
14723251f1e2SMax Filippov	 * the exception handler call.
14735a0015d6SChris Zankel	 */
14745a0015d6SChris Zankel
147599d5040eSMax Filippov	xsr	a3, excsave1	# get spill-mask
1476244066f4SMax Filippov	slli	a3, a3, 1	# shift left by one
14773251f1e2SMax Filippov	addi	a3, a3, 1	# set the bit for the current window frame
14785a0015d6SChris Zankel
1479244066f4SMax Filippov	slli	a2, a3, 32-WSBITS
1480244066f4SMax Filippov	src	a2, a3, a2	# a2 = xxwww1yyxxxwww1yy......
1481bc5378fcSMax Filippov	wsr	a2, windowstart	# set corrected windowstart
14825a0015d6SChris Zankel
1483244066f4SMax Filippov	srli	a3, a3, 1
1484244066f4SMax Filippov	rsr	a2, excsave1
1485244066f4SMax Filippov	l32i	a2, a2, EXC_TABLE_DOUBLE_SAVE	# restore a2
1486244066f4SMax Filippov	xsr	a2, excsave1
1487244066f4SMax Filippov	s32i	a3, a2, EXC_TABLE_DOUBLE_SAVE	# save a3
1488244066f4SMax Filippov	l32i	a3, a2, EXC_TABLE_PARAM	# original WB (in user task)
1489244066f4SMax Filippov	xsr	a2, excsave1
14905a0015d6SChris Zankel
14915a0015d6SChris Zankel	/* Return to the original (user task) WINDOWBASE.
14925a0015d6SChris Zankel	 * We leave the following frame behind:
14935a0015d6SChris Zankel	 * a0, a1, a2	same
1494244066f4SMax Filippov	 * a3:		trashed (saved in EXC_TABLE_DOUBLE_SAVE)
14955a0015d6SChris Zankel	 * depc:	depc (we have to return to that address)
1496244066f4SMax Filippov	 * excsave_1:	exctable
14975a0015d6SChris Zankel	 */
14985a0015d6SChris Zankel
1499bc5378fcSMax Filippov	wsr	a3, windowbase
15005a0015d6SChris Zankel	rsync
15015a0015d6SChris Zankel
15025a0015d6SChris Zankel	/* We are now in the original frame when we entered _spill_registers:
15035a0015d6SChris Zankel	 *  a0: return address
15045a0015d6SChris Zankel	 *  a1: used, stack pointer
15055a0015d6SChris Zankel	 *  a2: kernel stack pointer
1506244066f4SMax Filippov	 *  a3: available
15075a0015d6SChris Zankel	 *  depc: exception address
1508244066f4SMax Filippov	 *  excsave: exctable
15095a0015d6SChris Zankel	 * Note: This frame might be the same as above.
15105a0015d6SChris Zankel	 */
15115a0015d6SChris Zankel
15125a0015d6SChris Zankel	/* Setup stack pointer. */
15135a0015d6SChris Zankel
15145a0015d6SChris Zankel	addi	a2, a2, -PT_USER_SIZE
15155a0015d6SChris Zankel	s32i	a0, a2, PT_AREG0
15165a0015d6SChris Zankel
15175a0015d6SChris Zankel	/* Make sure we return to this fixup handler. */
15185a0015d6SChris Zankel
15195a0015d6SChris Zankel	movi	a3, fast_syscall_spill_registers_fixup_return
15205a0015d6SChris Zankel	s32i	a3, a2, PT_DEPC		# setup depc
15215a0015d6SChris Zankel
15225a0015d6SChris Zankel	/* Jump to the exception handler. */
15235a0015d6SChris Zankel
152499d5040eSMax Filippov	rsr	a3, excsave1
1525bc5378fcSMax Filippov	rsr	a0, exccause
15265a0015d6SChris Zankel	addx4	a0, a0, a3              	# find entry in table
15275a0015d6SChris Zankel	l32i	a0, a0, EXC_TABLE_FAST_USER     # load handler
1528244066f4SMax Filippov	l32i	a3, a3, EXC_TABLE_DOUBLE_SAVE
15295a0015d6SChris Zankel	jx	a0
15305a0015d6SChris Zankel
1531244066f4SMax FilippovENDPROC(fast_syscall_spill_registers_fixup)
1532244066f4SMax Filippov
1533244066f4SMax FilippovENTRY(fast_syscall_spill_registers_fixup_return)
15345a0015d6SChris Zankel
15355a0015d6SChris Zankel	/* When we return here, all registers have been restored (a2: DEPC) */
15365a0015d6SChris Zankel
1537bc5378fcSMax Filippov	wsr	a2, depc		# exception address
15385a0015d6SChris Zankel
15395a0015d6SChris Zankel	/* Restore fixup handler. */
15405a0015d6SChris Zankel
1541244066f4SMax Filippov	rsr	a2, excsave1
1542244066f4SMax Filippov	s32i	a3, a2, EXC_TABLE_DOUBLE_SAVE
1543244066f4SMax Filippov	movi	a3, fast_syscall_spill_registers_fixup
1544244066f4SMax Filippov	s32i	a3, a2, EXC_TABLE_FIXUP
1545244066f4SMax Filippov	rsr	a3, windowbase
1546244066f4SMax Filippov	s32i	a3, a2, EXC_TABLE_PARAM
1547244066f4SMax Filippov	l32i	a2, a2, EXC_TABLE_KSTK
15485a0015d6SChris Zankel
15495a0015d6SChris Zankel	/* Load WB at the time the exception occurred. */
15505a0015d6SChris Zankel
1551bc5378fcSMax Filippov	rsr	a3, sar			# WB is still in SAR
15525a0015d6SChris Zankel	neg	a3, a3
1553bc5378fcSMax Filippov	wsr	a3, windowbase
15545a0015d6SChris Zankel	rsync
15555a0015d6SChris Zankel
1556244066f4SMax Filippov	rsr	a3, excsave1
1557244066f4SMax Filippov	l32i	a3, a3, EXC_TABLE_DOUBLE_SAVE
1558244066f4SMax Filippov
15595a0015d6SChris Zankel	rfde
15605a0015d6SChris Zankel
1561244066f4SMax FilippovENDPROC(fast_syscall_spill_registers_fixup_return)
15625a0015d6SChris Zankel
15639184289cSMax Filippov#else /* CONFIG_FAST_SYSCALL_SPILL_REGISTERS */
15649184289cSMax Filippov
15659184289cSMax FilippovENTRY(fast_syscall_spill_registers)
15669184289cSMax Filippov
15679184289cSMax Filippov	l32i    a0, a2, PT_AREG0        # restore a0
15689184289cSMax Filippov	movi	a2, -ENOSYS
15699184289cSMax Filippov	rfe
15709184289cSMax Filippov
15719184289cSMax FilippovENDPROC(fast_syscall_spill_registers)
15729184289cSMax Filippov
15739184289cSMax Filippov#endif /* CONFIG_FAST_SYSCALL_SPILL_REGISTERS */
15749184289cSMax Filippov
1575e5083a63SJohannes Weiner#ifdef CONFIG_MMU
15765a0015d6SChris Zankel/*
15775a0015d6SChris Zankel * We should never get here. Bail out!
15785a0015d6SChris Zankel */
15795a0015d6SChris Zankel
15805a0015d6SChris ZankelENTRY(fast_second_level_miss_double_kernel)
15815a0015d6SChris Zankel
15822da03d41SMax Filippov1:
15832da03d41SMax Filippov	call0	unrecoverable_exception		# should not return
15845a0015d6SChris Zankel1:	j	1b
15855a0015d6SChris Zankel
1586d1538c46SChris ZankelENDPROC(fast_second_level_miss_double_kernel)
1587d1538c46SChris Zankel
15885a0015d6SChris Zankel/* First-level entry handler for user, kernel, and double 2nd-level
15895a0015d6SChris Zankel * TLB miss exceptions.  Note that for now, user and kernel miss
15905a0015d6SChris Zankel * exceptions share the same entry point and are handled identically.
15915a0015d6SChris Zankel *
15925a0015d6SChris Zankel * An old, less-efficient C version of this function used to exist.
15935a0015d6SChris Zankel * We include it below, interleaved as comments, for reference.
15945a0015d6SChris Zankel *
15955a0015d6SChris Zankel * Entry condition:
15965a0015d6SChris Zankel *
15975a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
15985a0015d6SChris Zankel *   a1:	a1
15995a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
160099d5040eSMax Filippov *   a3:	a3
16015a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
160299d5040eSMax Filippov *   excsave_1:	dispatch table
16035a0015d6SChris Zankel *
16045a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
16055a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
16065a0015d6SChris Zankel */
16075a0015d6SChris Zankel
16085a0015d6SChris ZankelENTRY(fast_second_level_miss)
16095a0015d6SChris Zankel
161099d5040eSMax Filippov	/* Save a1 and a3. Note: we don't expect a double exception. */
16115a0015d6SChris Zankel
16125a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
161399d5040eSMax Filippov	s32i	a3, a2, PT_AREG3
16145a0015d6SChris Zankel
16155a0015d6SChris Zankel	/* We need to map the page of PTEs for the user task.  Find
16165a0015d6SChris Zankel	 * the pointer to that page.  Also, it's possible for tsk->mm
16175a0015d6SChris Zankel	 * to be NULL while tsk->active_mm is nonzero if we faulted on
16185a0015d6SChris Zankel	 * a vmalloc address.  In that rare case, we must use
16195a0015d6SChris Zankel	 * active_mm instead to avoid a fault in this handler.  See
16205a0015d6SChris Zankel	 *
16215a0015d6SChris Zankel	 * http://mail.nl.linux.org/linux-mm/2002-08/msg00258.html
16225a0015d6SChris Zankel	 *   (or search Internet on "mm vs. active_mm")
16235a0015d6SChris Zankel	 *
16245a0015d6SChris Zankel	 *	if (!mm)
16255a0015d6SChris Zankel	 *		mm = tsk->active_mm;
16265a0015d6SChris Zankel	 *	pgd = pgd_offset (mm, regs->excvaddr);
16275a0015d6SChris Zankel	 *	pmd = pmd_offset (pgd, regs->excvaddr);
16285a0015d6SChris Zankel	 *	pmdval = *pmd;
16295a0015d6SChris Zankel	 */
16305a0015d6SChris Zankel
16315a0015d6SChris Zankel	GET_CURRENT(a1,a2)
16325a0015d6SChris Zankel	l32i	a0, a1, TASK_MM		# tsk->mm
1633*7f9c9741SMax Filippov	beqz	a0, .Lfast_second_level_miss_no_mm
16345a0015d6SChris Zankel
1635*7f9c9741SMax Filippov.Lfast_second_level_miss_continue:
1636*7f9c9741SMax Filippov	rsr	a3, excvaddr		# fault address
163701858d1bSChris Zankel	_PGD_OFFSET(a0, a3, a1)
16385a0015d6SChris Zankel	l32i	a0, a0, 0		# read pmdval
1639*7f9c9741SMax Filippov	beqz	a0, .Lfast_second_level_miss_no_pmd
16405a0015d6SChris Zankel
16415a0015d6SChris Zankel	/* Read ptevaddr and convert to top of page-table page.
16425a0015d6SChris Zankel	 *
16435a0015d6SChris Zankel	 * 	vpnval = read_ptevaddr_register() & PAGE_MASK;
16445a0015d6SChris Zankel	 * 	vpnval += DTLB_WAY_PGTABLE;
16455a0015d6SChris Zankel	 *	pteval = mk_pte (virt_to_page(pmd_val(pmdval)), PAGE_KERNEL);
16465a0015d6SChris Zankel	 *	write_dtlb_entry (pteval, vpnval);
16475a0015d6SChris Zankel	 *
16485a0015d6SChris Zankel	 * The messy computation for 'pteval' above really simplifies
16495a0015d6SChris Zankel	 * into the following:
16505a0015d6SChris Zankel	 *
1651a9f2fc62SMax Filippov	 * pteval = ((pmdval - PAGE_OFFSET + PHYS_OFFSET) & PAGE_MASK)
1652a9f2fc62SMax Filippov	 *                 | PAGE_DIRECTORY
16535a0015d6SChris Zankel	 */
16545a0015d6SChris Zankel
1655a9f2fc62SMax Filippov	movi	a1, (PHYS_OFFSET - PAGE_OFFSET) & 0xffffffff
16565a0015d6SChris Zankel	add	a0, a0, a1		# pmdval - PAGE_OFFSET
16575a0015d6SChris Zankel	extui	a1, a0, 0, PAGE_SHIFT	# ... & PAGE_MASK
16585a0015d6SChris Zankel	xor	a0, a0, a1
16595a0015d6SChris Zankel
166001858d1bSChris Zankel	movi	a1, _PAGE_DIRECTORY
16615a0015d6SChris Zankel	or	a0, a0, a1		# ... | PAGE_DIRECTORY
16625a0015d6SChris Zankel
166301858d1bSChris Zankel	/*
16646656920bSChris Zankel	 * We utilize all three wired-ways (7-9) to hold pmd translations.
166501858d1bSChris Zankel	 * Memory regions are mapped to the DTLBs according to bits 28 and 29.
166601858d1bSChris Zankel	 * This allows to map the three most common regions to three different
166701858d1bSChris Zankel	 * DTLBs:
166801858d1bSChris Zankel	 *  0,1 -> way 7	program (0040.0000) and virtual (c000.0000)
166901858d1bSChris Zankel	 *  2   -> way 8	shared libaries (2000.0000)
167001858d1bSChris Zankel	 *  3   -> way 0	stack (3000.0000)
167101858d1bSChris Zankel	 */
16725a0015d6SChris Zankel
167301858d1bSChris Zankel	extui	a3, a3, 28, 2		# addr. bit 28 and 29	0,1,2,3
1674bc5378fcSMax Filippov	rsr	a1, ptevaddr
167501858d1bSChris Zankel	addx2	a3, a3, a3		# ->			0,3,6,9
167601858d1bSChris Zankel	srli	a1, a1, PAGE_SHIFT
167701858d1bSChris Zankel	extui	a3, a3, 2, 2		# ->			0,0,1,2
167801858d1bSChris Zankel	slli	a1, a1, PAGE_SHIFT	# ptevaddr & PAGE_MASK
167901858d1bSChris Zankel	addi	a3, a3, DTLB_WAY_PGD
168001858d1bSChris Zankel	add	a1, a1, a3		# ... + way_number
168101858d1bSChris Zankel
1682*7f9c9741SMax Filippov.Lfast_second_level_miss_wdtlb:
1683*7f9c9741SMax Filippov	wdtlb	a0, a1
16845a0015d6SChris Zankel	dsync
16855a0015d6SChris Zankel
16865a0015d6SChris Zankel	/* Exit critical section. */
1687*7f9c9741SMax Filippov.Lfast_second_level_miss_skip_wdtlb:
1688*7f9c9741SMax Filippov	rsr	a3, excsave1
16895a0015d6SChris Zankel	movi	a0, 0
16905a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_FIXUP
16915a0015d6SChris Zankel
16925a0015d6SChris Zankel	/* Restore the working registers, and return. */
16935a0015d6SChris Zankel
16945a0015d6SChris Zankel	l32i	a0, a2, PT_AREG0
16955a0015d6SChris Zankel	l32i	a1, a2, PT_AREG1
169699d5040eSMax Filippov	l32i	a3, a2, PT_AREG3
16975a0015d6SChris Zankel	l32i	a2, a2, PT_DEPC
16985a0015d6SChris Zankel
16995a0015d6SChris Zankel	bgeui	a2, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
17005a0015d6SChris Zankel
17015a0015d6SChris Zankel	/* Restore excsave1 and return. */
17025a0015d6SChris Zankel
1703bc5378fcSMax Filippov	rsr	a2, depc
17045a0015d6SChris Zankel	rfe
17055a0015d6SChris Zankel
17065a0015d6SChris Zankel	/* Return from double exception. */
17075a0015d6SChris Zankel
1708bc5378fcSMax Filippov1:	xsr	a2, depc
17095a0015d6SChris Zankel	esync
17105a0015d6SChris Zankel	rfde
17115a0015d6SChris Zankel
1712*7f9c9741SMax Filippov.Lfast_second_level_miss_no_mm:
1713*7f9c9741SMax Filippov	l32i	a0, a1, TASK_ACTIVE_MM	# unlikely case mm == 0
1714*7f9c9741SMax Filippov	bnez	a0, .Lfast_second_level_miss_continue
171538fef73cSMax Filippov
171638fef73cSMax Filippov	/* Even more unlikely case active_mm == 0.
171738fef73cSMax Filippov	 * We can get here with NMI in the middle of context_switch that
171838fef73cSMax Filippov	 * touches vmalloc area.
171938fef73cSMax Filippov	 */
172038fef73cSMax Filippov	movi	a0, init_mm
1721*7f9c9741SMax Filippov	j	.Lfast_second_level_miss_continue
17225a0015d6SChris Zankel
1723*7f9c9741SMax Filippov.Lfast_second_level_miss_no_pmd:
17246656920bSChris Zankel#if (DCACHE_WAY_SIZE > PAGE_SIZE)
17256656920bSChris Zankel
1726*7f9c9741SMax Filippov	/* Special case for cache aliasing.
17276656920bSChris Zankel	 * We (should) only get here if a clear_user_page, copy_user_page
17286656920bSChris Zankel	 * or the aliased cache flush functions got preemptively interrupted
17296656920bSChris Zankel	 * by another task. Re-establish temporary mapping to the
17306656920bSChris Zankel	 * TLBTEMP_BASE areas.
17316656920bSChris Zankel	 */
17326656920bSChris Zankel
17336656920bSChris Zankel	/* We shouldn't be in a double exception */
17346656920bSChris Zankel
17356656920bSChris Zankel	l32i	a0, a2, PT_DEPC
1736*7f9c9741SMax Filippov	bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, .Lfast_second_level_miss_slow
17376656920bSChris Zankel
17386656920bSChris Zankel	/* Make sure the exception originated in the special functions */
17396656920bSChris Zankel
17406656920bSChris Zankel	movi	a0, __tlbtemp_mapping_start
1741bc5378fcSMax Filippov	rsr	a3, epc1
1742*7f9c9741SMax Filippov	bltu	a3, a0, .Lfast_second_level_miss_slow
17436656920bSChris Zankel	movi	a0, __tlbtemp_mapping_end
1744*7f9c9741SMax Filippov	bgeu	a3, a0, .Lfast_second_level_miss_slow
17456656920bSChris Zankel
17466656920bSChris Zankel	/* Check if excvaddr was in one of the TLBTEMP_BASE areas. */
17476656920bSChris Zankel
17486656920bSChris Zankel	movi	a3, TLBTEMP_BASE_1
1749bc5378fcSMax Filippov	rsr	a0, excvaddr
1750*7f9c9741SMax Filippov	bltu	a0, a3, .Lfast_second_level_miss_slow
17516656920bSChris Zankel
17527128039fSMax Filippov	addi	a1, a0, -TLBTEMP_SIZE
1753*7f9c9741SMax Filippov	bgeu	a1, a3, .Lfast_second_level_miss_slow
17546656920bSChris Zankel
17556656920bSChris Zankel	/* Check if we have to restore an ITLB mapping. */
17566656920bSChris Zankel
17576656920bSChris Zankel	movi	a1, __tlbtemp_mapping_itlb
1758bc5378fcSMax Filippov	rsr	a3, epc1
17596656920bSChris Zankel	sub	a3, a3, a1
17606656920bSChris Zankel
17616656920bSChris Zankel	/* Calculate VPN */
17626656920bSChris Zankel
17636656920bSChris Zankel	movi	a1, PAGE_MASK
17646656920bSChris Zankel	and	a1, a1, a0
17656656920bSChris Zankel
17666656920bSChris Zankel	/* Jump for ITLB entry */
17676656920bSChris Zankel
17686656920bSChris Zankel	bgez	a3, 1f
17696656920bSChris Zankel
17706656920bSChris Zankel	/* We can use up to two TLBTEMP areas, one for src and one for dst. */
17716656920bSChris Zankel
17726656920bSChris Zankel	extui	a3, a0, PAGE_SHIFT + DCACHE_ALIAS_ORDER, 1
17736656920bSChris Zankel	add	a1, a3, a1
17746656920bSChris Zankel
17756656920bSChris Zankel	/* PPN is in a6 for the first TLBTEMP area and in a7 for the second. */
17766656920bSChris Zankel
17776656920bSChris Zankel	mov	a0, a6
17786656920bSChris Zankel	movnez	a0, a7, a3
1779*7f9c9741SMax Filippov	j	.Lfast_second_level_miss_wdtlb
17806656920bSChris Zankel
17816656920bSChris Zankel	/* ITLB entry. We only use dst in a6. */
17826656920bSChris Zankel
17836656920bSChris Zankel1:	witlb	a6, a1
17846656920bSChris Zankel	isync
1785*7f9c9741SMax Filippov	j	.Lfast_second_level_miss_skip_wdtlb
17866656920bSChris Zankel
17876656920bSChris Zankel
17886656920bSChris Zankel#endif	// DCACHE_WAY_SIZE > PAGE_SIZE
17896656920bSChris Zankel
1790*7f9c9741SMax Filippov	/* Invalid PGD, default exception handling */
1791*7f9c9741SMax Filippov.Lfast_second_level_miss_slow:
17925a0015d6SChris Zankel
1793bc5378fcSMax Filippov	rsr	a1, depc
17945a0015d6SChris Zankel	s32i	a1, a2, PT_AREG2
17955a0015d6SChris Zankel	mov	a1, a2
17965a0015d6SChris Zankel
1797bc5378fcSMax Filippov	rsr	a2, ps
1798173d6681SChris Zankel	bbsi.l	a2, PS_UM_BIT, 1f
179950722f0bSMax Filippov	call0	_kernel_exception
180050722f0bSMax Filippov1:	call0	_user_exception
18015a0015d6SChris Zankel
1802d1538c46SChris ZankelENDPROC(fast_second_level_miss)
18035a0015d6SChris Zankel
18045a0015d6SChris Zankel/*
18055a0015d6SChris Zankel * StoreProhibitedException
18065a0015d6SChris Zankel *
18075a0015d6SChris Zankel * Update the pte and invalidate the itlb mapping for this pte.
18085a0015d6SChris Zankel *
18095a0015d6SChris Zankel * Entry condition:
18105a0015d6SChris Zankel *
18115a0015d6SChris Zankel *   a0:	trashed, original value saved on stack (PT_AREG0)
18125a0015d6SChris Zankel *   a1:	a1
18135a0015d6SChris Zankel *   a2:	new stack pointer, original in DEPC
181499d5040eSMax Filippov *   a3:	a3
18155a0015d6SChris Zankel *   depc:	a2, original value saved on stack (PT_DEPC)
181699d5040eSMax Filippov *   excsave_1:	dispatch table
18175a0015d6SChris Zankel *
18185a0015d6SChris Zankel *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
18195a0015d6SChris Zankel *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
18205a0015d6SChris Zankel */
18215a0015d6SChris Zankel
18225a0015d6SChris ZankelENTRY(fast_store_prohibited)
18235a0015d6SChris Zankel
182499d5040eSMax Filippov	/* Save a1 and a3. */
18255a0015d6SChris Zankel
18265a0015d6SChris Zankel	s32i	a1, a2, PT_AREG1
182799d5040eSMax Filippov	s32i	a3, a2, PT_AREG3
18285a0015d6SChris Zankel
18295a0015d6SChris Zankel	GET_CURRENT(a1,a2)
18305a0015d6SChris Zankel	l32i	a0, a1, TASK_MM		# tsk->mm
1831*7f9c9741SMax Filippov	beqz	a0, .Lfast_store_no_mm
18325a0015d6SChris Zankel
1833*7f9c9741SMax Filippov.Lfast_store_continue:
1834*7f9c9741SMax Filippov	rsr	a1, excvaddr		# fault address
183599d5040eSMax Filippov	_PGD_OFFSET(a0, a1, a3)
18365a0015d6SChris Zankel	l32i	a0, a0, 0
1837*7f9c9741SMax Filippov	beqz	a0, .Lfast_store_slow
18385a0015d6SChris Zankel
183951fc41a9SMax Filippov	/*
184051fc41a9SMax Filippov	 * Note that we test _PAGE_WRITABLE_BIT only if PTE is present
184151fc41a9SMax Filippov	 * and is not PAGE_NONE. See pgtable.h for possible PTE layouts.
184251fc41a9SMax Filippov	 */
184301858d1bSChris Zankel
184499d5040eSMax Filippov	_PTE_OFFSET(a0, a1, a3)
184599d5040eSMax Filippov	l32i	a3, a0, 0		# read pteval
184651fc41a9SMax Filippov	movi	a1, _PAGE_CA_INVALID
1847*7f9c9741SMax Filippov	ball	a3, a1, .Lfast_store_slow
1848*7f9c9741SMax Filippov	bbci.l	a3, _PAGE_WRITABLE_BIT, .Lfast_store_slow
18495a0015d6SChris Zankel
185001858d1bSChris Zankel	movi	a1, _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_HW_WRITE
185199d5040eSMax Filippov	or	a3, a3, a1
1852bc5378fcSMax Filippov	rsr	a1, excvaddr
185399d5040eSMax Filippov	s32i	a3, a0, 0
18545a0015d6SChris Zankel
18555a0015d6SChris Zankel	/* We need to flush the cache if we have page coloring. */
18565a0015d6SChris Zankel#if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
18575a0015d6SChris Zankel	dhwb	a0, 0
18585a0015d6SChris Zankel#endif
18595a0015d6SChris Zankel	pdtlb	a0, a1
186099d5040eSMax Filippov	wdtlb	a3, a0
18615a0015d6SChris Zankel
18625a0015d6SChris Zankel	/* Exit critical section. */
18635a0015d6SChris Zankel
18645a0015d6SChris Zankel	movi	a0, 0
186599d5040eSMax Filippov	rsr	a3, excsave1
18665a0015d6SChris Zankel	s32i	a0, a3, EXC_TABLE_FIXUP
18675a0015d6SChris Zankel
18685a0015d6SChris Zankel	/* Restore the working registers, and return. */
18695a0015d6SChris Zankel
187099d5040eSMax Filippov	l32i	a3, a2, PT_AREG3
18715a0015d6SChris Zankel	l32i	a1, a2, PT_AREG1
18725a0015d6SChris Zankel	l32i	a0, a2, PT_AREG0
18735a0015d6SChris Zankel	l32i	a2, a2, PT_DEPC
18745a0015d6SChris Zankel
18755a0015d6SChris Zankel	bgeui	a2, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
1876bc5378fcSMax Filippov	rsr	a2, depc
18775a0015d6SChris Zankel	rfe
18785a0015d6SChris Zankel
18795a0015d6SChris Zankel	/* Double exception. Restore FIXUP handler and return. */
18805a0015d6SChris Zankel
1881bc5378fcSMax Filippov1:	xsr	a2, depc
18825a0015d6SChris Zankel	esync
18835a0015d6SChris Zankel	rfde
18845a0015d6SChris Zankel
1885*7f9c9741SMax Filippov.Lfast_store_no_mm:
1886*7f9c9741SMax Filippov	l32i	a0, a1, TASK_ACTIVE_MM	# unlikely case mm == 0
1887*7f9c9741SMax Filippov	j	.Lfast_store_continue
18885a0015d6SChris Zankel
1889*7f9c9741SMax Filippov	/* If there was a problem, handle fault in C */
1890*7f9c9741SMax Filippov.Lfast_store_slow:
18912a26f4eeSMax Filippov	rsr	a1, excvaddr
18922a26f4eeSMax Filippov	pdtlb	a0, a1
18932a26f4eeSMax Filippov	bbci.l	a0, DTLB_HIT_BIT, 1f
18942a26f4eeSMax Filippov	idtlb	a0
18952a26f4eeSMax Filippov1:
189699d5040eSMax Filippov	rsr	a3, depc	# still holds a2
189799d5040eSMax Filippov	s32i	a3, a2, PT_AREG2
18985a0015d6SChris Zankel	mov	a1, a2
18995a0015d6SChris Zankel
1900bc5378fcSMax Filippov	rsr	a2, ps
1901173d6681SChris Zankel	bbsi.l	a2, PS_UM_BIT, 1f
190250722f0bSMax Filippov	call0	_kernel_exception
190350722f0bSMax Filippov1:	call0	_user_exception
1904d1538c46SChris Zankel
1905d1538c46SChris ZankelENDPROC(fast_store_prohibited)
1906d1538c46SChris Zankel
1907e5083a63SJohannes Weiner#endif /* CONFIG_MMU */
19085a0015d6SChris Zankel
190950722f0bSMax Filippov	.text
19105a0015d6SChris Zankel/*
1911fc4fb2adSChris Zankel * System Calls.
1912fc4fb2adSChris Zankel *
1913fc4fb2adSChris Zankel * void system_call (struct pt_regs* regs, int exccause)
1914fc4fb2adSChris Zankel *                            a2                 a3
1915fc4fb2adSChris Zankel */
1916f8f02ca7SMax Filippov	.literal_position
1917fc4fb2adSChris Zankel
1918fc4fb2adSChris ZankelENTRY(system_call)
1919d1538c46SChris Zankel
19200b537257SMax Filippov#if defined(__XTENSA_WINDOWED_ABI__)
19219d9043f6SMax Filippov	abi_entry_default
19220b537257SMax Filippov#elif defined(__XTENSA_CALL0_ABI__)
19230b537257SMax Filippov	abi_entry(12)
19240b537257SMax Filippov
19250b537257SMax Filippov	s32i	a0, sp, 0
19260b537257SMax Filippov	s32i	abi_saved0, sp, 4
19270b537257SMax Filippov	s32i	abi_saved1, sp, 8
19280b537257SMax Filippov	mov	abi_saved0, a2
19290b537257SMax Filippov#else
19300b537257SMax Filippov#error Unsupported Xtensa ABI
19310b537257SMax Filippov#endif
1932fc4fb2adSChris Zankel
1933fc4fb2adSChris Zankel	/* regs->syscall = regs->areg[2] */
1934fc4fb2adSChris Zankel
19350b537257SMax Filippov	l32i	a7, abi_saved0, PT_AREG2
19360b537257SMax Filippov	s32i	a7, abi_saved0, PT_SYSCALL
1937fc4fb2adSChris Zankel
19383aee3e25SMax Filippov	GET_THREAD_INFO(a4, a1)
19390b537257SMax Filippov	l32i	abi_saved1, a4, TI_FLAGS
19403aee3e25SMax Filippov	movi	a4, _TIF_WORK_MASK
19410b537257SMax Filippov	and	abi_saved1, abi_saved1, a4
19420b537257SMax Filippov	beqz	abi_saved1, 1f
19433aee3e25SMax Filippov
19440b537257SMax Filippov	mov	abi_arg0, abi_saved0
19450b537257SMax Filippov	abi_call	do_syscall_trace_enter
19460b537257SMax Filippov	beqz	abi_rv, .Lsyscall_exit
19470b537257SMax Filippov	l32i	a7, abi_saved0, PT_SYSCALL
19483aee3e25SMax Filippov
19493aee3e25SMax Filippov1:
1950fc4fb2adSChris Zankel	/* syscall = sys_call_table[syscall_nr] */
1951fc4fb2adSChris Zankel
1952f984409aSMax Filippov	movi	a4, sys_call_table
1953a86067f5SFiroz Khan	movi	a5, __NR_syscalls
19540b537257SMax Filippov	movi	abi_rv, -ENOSYS
19553aee3e25SMax Filippov	bgeu	a7, a5, 1f
1956fc4fb2adSChris Zankel
19573aee3e25SMax Filippov	addx4	a4, a7, a4
19580b537257SMax Filippov	l32i	abi_tmp0, a4, 0
1959fc4fb2adSChris Zankel
1960fc4fb2adSChris Zankel	/* Load args: arg0 - arg5 are passed via regs. */
1961fc4fb2adSChris Zankel
19620b537257SMax Filippov	l32i	abi_arg0, abi_saved0, PT_AREG6
19630b537257SMax Filippov	l32i	abi_arg1, abi_saved0, PT_AREG3
19640b537257SMax Filippov	l32i	abi_arg2, abi_saved0, PT_AREG4
19650b537257SMax Filippov	l32i	abi_arg3, abi_saved0, PT_AREG5
19660b537257SMax Filippov	l32i	abi_arg4, abi_saved0, PT_AREG8
19670b537257SMax Filippov	l32i	abi_arg5, abi_saved0, PT_AREG9
1968fc4fb2adSChris Zankel
19690b537257SMax Filippov	abi_callx	abi_tmp0
1970fc4fb2adSChris Zankel
1971fc4fb2adSChris Zankel1:	/* regs->areg[2] = return_value */
1972fc4fb2adSChris Zankel
19730b537257SMax Filippov	s32i	abi_rv, abi_saved0, PT_AREG2
19740b537257SMax Filippov	bnez	abi_saved1, 1f
197502ce94c2SMax Filippov.Lsyscall_exit:
19760b537257SMax Filippov#if defined(__XTENSA_WINDOWED_ABI__)
19779d9043f6SMax Filippov	abi_ret_default
19780b537257SMax Filippov#elif defined(__XTENSA_CALL0_ABI__)
19790b537257SMax Filippov	l32i	a0, sp, 0
19800b537257SMax Filippov	l32i	abi_saved0, sp, 4
19810b537257SMax Filippov	l32i	abi_saved1, sp, 8
19820b537257SMax Filippov	abi_ret(12)
19830b537257SMax Filippov#else
19840b537257SMax Filippov#error Unsupported Xtensa ABI
19850b537257SMax Filippov#endif
19863aee3e25SMax Filippov
19873aee3e25SMax Filippov1:
19880b537257SMax Filippov	mov	abi_arg0, abi_saved0
19890b537257SMax Filippov	abi_call	do_syscall_trace_leave
19900b537257SMax Filippov	j	.Lsyscall_exit
1991fc4fb2adSChris Zankel
1992d1538c46SChris ZankelENDPROC(system_call)
1993d1538c46SChris Zankel
1994e2fd1374SMax Filippov/*
1995e2fd1374SMax Filippov * Spill live registers on the kernel stack macro.
1996e2fd1374SMax Filippov *
1997e2fd1374SMax Filippov * Entry condition: ps.woe is set, ps.excm is cleared
1998e2fd1374SMax Filippov * Exit condition: windowstart has single bit set
1999e2fd1374SMax Filippov * May clobber: a12, a13
2000e2fd1374SMax Filippov */
2001e2fd1374SMax Filippov	.macro	spill_registers_kernel
2002e2fd1374SMax Filippov
2003e2fd1374SMax Filippov#if XCHAL_NUM_AREGS > 16
2004e2fd1374SMax Filippov	call12	1f
2005e2fd1374SMax Filippov	_j	2f
2006e2fd1374SMax Filippov	retw
2007e2fd1374SMax Filippov	.align	4
2008e2fd1374SMax Filippov1:
2009e2fd1374SMax Filippov	_entry	a1, 48
2010e2fd1374SMax Filippov	addi	a12, a0, 3
2011e2fd1374SMax Filippov#if XCHAL_NUM_AREGS > 32
2012e2fd1374SMax Filippov	.rept	(XCHAL_NUM_AREGS - 32) / 12
2013e2fd1374SMax Filippov	_entry	a1, 48
2014e2fd1374SMax Filippov	mov	a12, a0
2015e2fd1374SMax Filippov	.endr
2016e2fd1374SMax Filippov#endif
201777d6273eSMax Filippov	_entry	a1, 16
2018e2fd1374SMax Filippov#if XCHAL_NUM_AREGS % 12 == 0
2019e2fd1374SMax Filippov	mov	a8, a8
2020e2fd1374SMax Filippov#elif XCHAL_NUM_AREGS % 12 == 4
2021e2fd1374SMax Filippov	mov	a12, a12
2022e2fd1374SMax Filippov#elif XCHAL_NUM_AREGS % 12 == 8
2023e2fd1374SMax Filippov	mov	a4, a4
2024e2fd1374SMax Filippov#endif
2025e2fd1374SMax Filippov	retw
2026e2fd1374SMax Filippov2:
2027e2fd1374SMax Filippov#else
2028e2fd1374SMax Filippov	mov	a12, a12
2029e2fd1374SMax Filippov#endif
2030e2fd1374SMax Filippov	.endm
2031fc4fb2adSChris Zankel
2032fc4fb2adSChris Zankel/*
20335a0015d6SChris Zankel * Task switch.
20345a0015d6SChris Zankel *
20355a0015d6SChris Zankel * struct task*  _switch_to (struct task* prev, struct task* next)
20365a0015d6SChris Zankel *         a2                              a2                 a3
20375a0015d6SChris Zankel */
20385a0015d6SChris Zankel
20395a0015d6SChris ZankelENTRY(_switch_to)
20405a0015d6SChris Zankel
20410b537257SMax Filippov#if defined(__XTENSA_WINDOWED_ABI__)
2042d6d5f19eSMax Filippov	abi_entry(XTENSA_SPILL_STACK_RESERVE)
20430b537257SMax Filippov#elif defined(__XTENSA_CALL0_ABI__)
20440b537257SMax Filippov	abi_entry(16)
20455a0015d6SChris Zankel
20460b537257SMax Filippov	s32i	a12, sp, 0
20470b537257SMax Filippov	s32i	a13, sp, 4
20480b537257SMax Filippov	s32i	a14, sp, 8
20490b537257SMax Filippov	s32i	a15, sp, 12
20500b537257SMax Filippov#else
20510b537257SMax Filippov#error Unsupported Xtensa ABI
20520b537257SMax Filippov#endif
2053e2fd1374SMax Filippov	mov	a11, a3			# and 'next' (a3)
20545a0015d6SChris Zankel
2055c658eac6SChris Zankel	l32i	a4, a2, TASK_THREAD_INFO
2056c658eac6SChris Zankel	l32i	a5, a3, TASK_THREAD_INFO
20575a0015d6SChris Zankel
2058e2fd1374SMax Filippov	save_xtregs_user a4 a6 a8 a9 a12 a13 THREAD_XTREGS_USER
20595a0015d6SChris Zankel
206052247123SMax Filippov#if THREAD_RA > 1020 || THREAD_SP > 1020
206152247123SMax Filippov	addi	a10, a2, TASK_THREAD
206252247123SMax Filippov	s32i	a0, a10, THREAD_RA - TASK_THREAD	# save return address
206352247123SMax Filippov	s32i	a1, a10, THREAD_SP - TASK_THREAD	# save stack pointer
206452247123SMax Filippov#else
206552247123SMax Filippov	s32i	a0, a2, THREAD_RA	# save return address
206652247123SMax Filippov	s32i	a1, a2, THREAD_SP	# save stack pointer
206752247123SMax Filippov#endif
2068c658eac6SChris Zankel
2069050e9baaSLinus Torvalds#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP)
207040d1a07bSMax Filippov	movi	a6, __stack_chk_guard
207140d1a07bSMax Filippov	l32i	a8, a3, TASK_STACK_CANARY
207240d1a07bSMax Filippov	s32i	a8, a6, 0
207340d1a07bSMax Filippov#endif
207440d1a07bSMax Filippov
2075c658eac6SChris Zankel	/* Disable ints while we manipulate the stack pointer. */
2076c658eac6SChris Zankel
207738fef73cSMax Filippov	irq_save a14, a3
20785a0015d6SChris Zankel	rsync
20795a0015d6SChris Zankel
2080c658eac6SChris Zankel	/* Switch CPENABLE */
2081c658eac6SChris Zankel
2082c658eac6SChris Zankel#if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
2083c658eac6SChris Zankel	l32i	a3, a5, THREAD_CPENABLE
208411e969bcSMax Filippov#ifdef CONFIG_SMP
208511e969bcSMax Filippov	beqz	a3, 1f
208611e969bcSMax Filippov	memw			# pairs with memw (2) in fast_coprocessor
208711e969bcSMax Filippov	l32i	a6, a5, THREAD_CP_OWNER_CPU
208811e969bcSMax Filippov	l32i	a7, a5, THREAD_CPU
208911e969bcSMax Filippov	beq	a6, a7, 1f	# load 0 into CPENABLE if current CPU is not the owner
209011e969bcSMax Filippov	movi	a3, 0
209111e969bcSMax Filippov1:
209211e969bcSMax Filippov#endif
209311e969bcSMax Filippov	wsr	a3, cpenable
2094c658eac6SChris Zankel#endif
2095c658eac6SChris Zankel
2096a0fc1436SMax Filippov#if XCHAL_HAVE_EXCLUSIVE
2097a0fc1436SMax Filippov	l32i	a3, a5, THREAD_ATOMCTL8
2098a0fc1436SMax Filippov	getex	a3
2099a0fc1436SMax Filippov	s32i	a3, a4, THREAD_ATOMCTL8
2100a0fc1436SMax Filippov#endif
2101a0fc1436SMax Filippov
2102c658eac6SChris Zankel	/* Flush register file. */
2103c658eac6SChris Zankel
21040b537257SMax Filippov#if defined(__XTENSA_WINDOWED_ABI__)
2105e2fd1374SMax Filippov	spill_registers_kernel
21060b537257SMax Filippov#endif
21075a0015d6SChris Zankel
21085a0015d6SChris Zankel	/* Set kernel stack (and leave critical section)
21095a0015d6SChris Zankel	 * Note: It's save to set it here. The stack will not be overwritten
21105a0015d6SChris Zankel	 *       because the kernel stack will only be loaded again after
21115a0015d6SChris Zankel	 *       we return from kernel space.
21125a0015d6SChris Zankel	 */
21135a0015d6SChris Zankel
2114bc5378fcSMax Filippov	rsr	a3, excsave1		# exc_table
2115c658eac6SChris Zankel	addi	a7, a5, PT_REGS_OFFSET
2116c658eac6SChris Zankel	s32i	a7, a3, EXC_TABLE_KSTK
21175a0015d6SChris Zankel
2118c50842dfSChris Zankel	/* restore context of the task 'next' */
21195a0015d6SChris Zankel
2120e2fd1374SMax Filippov	l32i	a0, a11, THREAD_RA	# restore return address
2121e2fd1374SMax Filippov	l32i	a1, a11, THREAD_SP	# restore stack pointer
21225a0015d6SChris Zankel
2123e2fd1374SMax Filippov	load_xtregs_user a5 a6 a8 a9 a12 a13 THREAD_XTREGS_USER
2124c658eac6SChris Zankel
2125bc5378fcSMax Filippov	wsr	a14, ps
21265a0015d6SChris Zankel	rsync
21275a0015d6SChris Zankel
21280b537257SMax Filippov#if defined(__XTENSA_WINDOWED_ABI__)
2129d6d5f19eSMax Filippov	abi_ret(XTENSA_SPILL_STACK_RESERVE)
21300b537257SMax Filippov#elif defined(__XTENSA_CALL0_ABI__)
21310b537257SMax Filippov	l32i	a12, sp, 0
21320b537257SMax Filippov	l32i	a13, sp, 4
21330b537257SMax Filippov	l32i	a14, sp, 8
21340b537257SMax Filippov	l32i	a15, sp, 12
21350b537257SMax Filippov	abi_ret(16)
21360b537257SMax Filippov#else
21370b537257SMax Filippov#error Unsupported Xtensa ABI
21380b537257SMax Filippov#endif
21395a0015d6SChris Zankel
2140d1538c46SChris ZankelENDPROC(_switch_to)
21415a0015d6SChris Zankel
21425a0015d6SChris ZankelENTRY(ret_from_fork)
21435a0015d6SChris Zankel
21445a0015d6SChris Zankel	/* void schedule_tail (struct task_struct *prev)
21450b537257SMax Filippov	 * Note: prev is still in abi_arg0 (return value from fake call frame)
21465a0015d6SChris Zankel	 */
21470b537257SMax Filippov	abi_call	schedule_tail
21485a0015d6SChris Zankel
21490b537257SMax Filippov	mov		abi_arg0, a1
21500b537257SMax Filippov	abi_call	do_syscall_trace_leave
21515a0015d6SChris Zankel	j		common_exception_return
21525a0015d6SChris Zankel
2153d1538c46SChris ZankelENDPROC(ret_from_fork)
2154d1538c46SChris Zankel
21553306a726SMax Filippov/*
21563306a726SMax Filippov * Kernel thread creation helper
21570b537257SMax Filippov * On entry, set up by copy_thread: abi_saved0 = thread_fn,
21580b537257SMax Filippov * abi_saved1 = thread_fn arg. Left from _switch_to: abi_arg0 = prev
21593306a726SMax Filippov */
21603306a726SMax FilippovENTRY(ret_from_kernel_thread)
21613306a726SMax Filippov
21620b537257SMax Filippov	abi_call	schedule_tail
21630b537257SMax Filippov	mov		abi_arg0, abi_saved1
21640b537257SMax Filippov	abi_callx	abi_saved0
2165f0a1bf08SMax Filippov	j		common_exception_return
21663306a726SMax Filippov
21673306a726SMax FilippovENDPROC(ret_from_kernel_thread)
2168733f5c28SMax Filippov
2169733f5c28SMax Filippov#ifdef CONFIG_HIBERNATION
2170733f5c28SMax Filippov
2171733f5c28SMax Filippov	.bss
2172733f5c28SMax Filippov	.align	4
2173733f5c28SMax Filippov.Lsaved_regs:
2174733f5c28SMax Filippov#if defined(__XTENSA_WINDOWED_ABI__)
2175733f5c28SMax Filippov	.fill	2, 4
2176733f5c28SMax Filippov#elif defined(__XTENSA_CALL0_ABI__)
2177733f5c28SMax Filippov	.fill	6, 4
2178733f5c28SMax Filippov#else
2179733f5c28SMax Filippov#error Unsupported Xtensa ABI
2180733f5c28SMax Filippov#endif
2181733f5c28SMax Filippov	.align	XCHAL_NCP_SA_ALIGN
2182733f5c28SMax Filippov.Lsaved_user_regs:
2183733f5c28SMax Filippov	.fill	XTREGS_USER_SIZE, 1
2184733f5c28SMax Filippov
2185733f5c28SMax Filippov	.previous
2186733f5c28SMax Filippov
2187733f5c28SMax FilippovENTRY(swsusp_arch_suspend)
2188733f5c28SMax Filippov
2189733f5c28SMax Filippov	abi_entry_default
2190733f5c28SMax Filippov
2191733f5c28SMax Filippov	movi		a2, .Lsaved_regs
2192733f5c28SMax Filippov	movi		a3, .Lsaved_user_regs
2193733f5c28SMax Filippov	s32i		a0, a2, 0
2194733f5c28SMax Filippov	s32i		a1, a2, 4
2195733f5c28SMax Filippov	save_xtregs_user a3 a4 a5 a6 a7 a8 0
2196733f5c28SMax Filippov#if defined(__XTENSA_WINDOWED_ABI__)
2197733f5c28SMax Filippov	spill_registers_kernel
2198733f5c28SMax Filippov#elif defined(__XTENSA_CALL0_ABI__)
2199733f5c28SMax Filippov	s32i		a12, a2, 8
2200733f5c28SMax Filippov	s32i		a13, a2, 12
2201733f5c28SMax Filippov	s32i		a14, a2, 16
2202733f5c28SMax Filippov	s32i		a15, a2, 20
2203733f5c28SMax Filippov#else
2204733f5c28SMax Filippov#error Unsupported Xtensa ABI
2205733f5c28SMax Filippov#endif
2206733f5c28SMax Filippov	abi_call	swsusp_save
2207733f5c28SMax Filippov	mov		a2, abi_rv
2208733f5c28SMax Filippov	abi_ret_default
2209733f5c28SMax Filippov
2210733f5c28SMax FilippovENDPROC(swsusp_arch_suspend)
2211733f5c28SMax Filippov
2212733f5c28SMax FilippovENTRY(swsusp_arch_resume)
2213733f5c28SMax Filippov
2214733f5c28SMax Filippov	abi_entry_default
2215733f5c28SMax Filippov
2216733f5c28SMax Filippov#if defined(__XTENSA_WINDOWED_ABI__)
2217733f5c28SMax Filippov	spill_registers_kernel
2218733f5c28SMax Filippov#endif
2219733f5c28SMax Filippov
2220733f5c28SMax Filippov	movi		a2, restore_pblist
2221733f5c28SMax Filippov	l32i		a2, a2, 0
2222733f5c28SMax Filippov
2223733f5c28SMax Filippov.Lcopy_pbe:
2224733f5c28SMax Filippov	l32i		a3, a2, PBE_ADDRESS
2225733f5c28SMax Filippov	l32i		a4, a2, PBE_ORIG_ADDRESS
2226733f5c28SMax Filippov
2227733f5c28SMax Filippov	__loopi		a3, a9, PAGE_SIZE, 16
2228733f5c28SMax Filippov	l32i		a5, a3, 0
2229733f5c28SMax Filippov	l32i		a6, a3, 4
2230733f5c28SMax Filippov	l32i		a7, a3, 8
2231733f5c28SMax Filippov	l32i		a8, a3, 12
2232733f5c28SMax Filippov	addi		a3, a3, 16
2233733f5c28SMax Filippov	s32i		a5, a4, 0
2234733f5c28SMax Filippov	s32i		a6, a4, 4
2235733f5c28SMax Filippov	s32i		a7, a4, 8
2236733f5c28SMax Filippov	s32i		a8, a4, 12
2237733f5c28SMax Filippov	addi		a4, a4, 16
2238733f5c28SMax Filippov	__endl		a3, a9
2239733f5c28SMax Filippov
2240733f5c28SMax Filippov	l32i		a2, a2, PBE_NEXT
2241733f5c28SMax Filippov	bnez		a2, .Lcopy_pbe
2242733f5c28SMax Filippov
2243733f5c28SMax Filippov	movi		a2, .Lsaved_regs
2244733f5c28SMax Filippov	movi		a3, .Lsaved_user_regs
2245733f5c28SMax Filippov	l32i		a0, a2, 0
2246733f5c28SMax Filippov	l32i		a1, a2, 4
2247733f5c28SMax Filippov	load_xtregs_user a3 a4 a5 a6 a7 a8 0
2248733f5c28SMax Filippov#if defined(__XTENSA_CALL0_ABI__)
2249733f5c28SMax Filippov	l32i		a12, a2, 8
2250733f5c28SMax Filippov	l32i		a13, a2, 12
2251733f5c28SMax Filippov	l32i		a14, a2, 16
2252733f5c28SMax Filippov	l32i		a15, a2, 20
2253733f5c28SMax Filippov#endif
2254733f5c28SMax Filippov	movi		a2, 0
2255733f5c28SMax Filippov	abi_ret_default
2256733f5c28SMax Filippov
2257733f5c28SMax FilippovENDPROC(swsusp_arch_resume)
2258733f5c28SMax Filippov
2259733f5c28SMax Filippov#endif
2260