entry.S 45.8 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * arch/xtensa/kernel/entry.S
 *
 * Low-level exception handling
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
10
 * Copyright (C) 2004 - 2008 by Tensilica Inc.
11 12 13 14 15 16
 *
 * Chris Zankel <chris@zankel.net>
 *
 */

#include <linux/linkage.h>
17
#include <asm/asm-offsets.h>
18
#include <asm/processor.h>
19
#include <asm/coprocessor.h>
20 21 22 23 24 25 26 27
#include <asm/thread_info.h>
#include <asm/uaccess.h>
#include <asm/unistd.h>
#include <asm/ptrace.h>
#include <asm/current.h>
#include <asm/pgtable.h>
#include <asm/page.h>
#include <asm/signal.h>
28
#include <asm/tlbflush.h>
29
#include <variant/tie-asm.h>
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

/* Unimplemented features. */

#undef KERNEL_STACK_OVERFLOW_CHECK
#undef ALLOCA_EXCEPTION_IN_IRAM

/* Not well tested.
 *
 * - fast_coprocessor
 */

/*
 * Macro to find first bit set in WINDOWBASE from the left + 1
 *
 * 100....0 -> 1
 * 010....0 -> 2
 * 000....1 -> WSBITS
 */

	.macro ffs_ws bit mask

#if XCHAL_HAVE_NSA
	nsau    \bit, \mask			# 32-WSBITS ... 31 (32 iff 0)
	addi    \bit, \bit, WSBITS - 32 + 1   	# uppest bit set -> return 1
#else
	movi    \bit, WSBITS
#if WSBITS > 16
	_bltui  \mask, 0x10000, 99f
	addi    \bit, \bit, -16
	extui   \mask, \mask, 16, 16
#endif
#if WSBITS > 8
99:	_bltui  \mask, 0x100, 99f
	addi    \bit, \bit, -8
	srli    \mask, \mask, 8
#endif
99:	_bltui  \mask, 0x10, 99f
	addi    \bit, \bit, -4
	srli    \mask, \mask, 4
99:	_bltui  \mask, 0x4, 99f
	addi    \bit, \bit, -2
	srli    \mask, \mask, 2
99:	_bltui  \mask, 0x2, 99f
	addi    \bit, \bit, -1
99:

#endif
	.endm

/* ----------------- DEFAULT FIRST LEVEL EXCEPTION HANDLERS ----------------- */

/*
 * First-level exception handler for user exceptions.
 * Save some special registers, extra states and all registers in the AR
 * register file that were in use in the user task, and jump to the common
 * exception code.
 * We save SAR (used to calculate WMASK), and WB and WS (we don't have to
 * save them for kernel exceptions).
 *
 * Entry condition for user_exception:
 *
 *   a0:	trashed, original value saved on stack (PT_AREG0)
 *   a1:	a1
 *   a2:	new stack pointer, original value in depc
94
 *   a3:	a3
95
 *   depc:	a2, original value saved on stack (PT_DEPC)
96
 *   excsave1:	dispatch table
97 98 99 100 101 102 103 104 105 106
 *
 *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
 *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
 *
 * Entry condition for _user_exception:
 *
 *   a0-a3 and depc have been saved to PT_AREG0...PT_AREG3 and PT_DEPC
 *   excsave has been restored, and
 *   stack pointer (a1) has been set.
 *
D
Daniel Mack 已提交
107
 * Note: _user_exception might be at an odd address. Don't use call0..call12
108 109 110 111
 */

ENTRY(user_exception)

112
	/* Save a1, a2, a3, and set SP. */
113

M
Max Filippov 已提交
114
	rsr	a0, depc
115 116 117 118 119 120 121 122 123 124 125
	s32i	a1, a2, PT_AREG1
	s32i	a0, a2, PT_AREG2
	s32i	a3, a2, PT_AREG3
	mov	a1, a2

	.globl _user_exception
_user_exception:

	/* Save SAR and turn off single stepping */

	movi	a2, 0
M
Max Filippov 已提交
126 127
	rsr	a3, sar
	xsr	a2, icountlevel
128
	s32i	a3, a1, PT_SAR
129
	s32i	a2, a1, PT_ICOUNTLEVEL
130

C
Chris Zankel 已提交
131 132 133 134 135
#if XCHAL_HAVE_THREADPTR
	rur	a2, threadptr
	s32i	a2, a1, PT_THREADPTR
#endif

136 137 138
	/* Rotate ws so that the current windowbase is at bit0. */
	/* Assume ws = xxwww1yyyy. Rotate ws right, so that a2 = yyyyxxwww1 */

M
Max Filippov 已提交
139 140
	rsr	a2, windowbase
	rsr	a3, windowstart
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	ssr	a2
	s32i	a2, a1, PT_WINDOWBASE
	s32i	a3, a1, PT_WINDOWSTART
	slli	a2, a3, 32-WSBITS
	src	a2, a3, a2
	srli	a2, a2, 32-WSBITS
	s32i	a2, a1, PT_WMASK	# needed for restoring registers

	/* Save only live registers. */

	_bbsi.l	a2, 1, 1f
	s32i	a4, a1, PT_AREG4
	s32i	a5, a1, PT_AREG5
	s32i	a6, a1, PT_AREG6
	s32i	a7, a1, PT_AREG7
	_bbsi.l	a2, 2, 1f
	s32i	a8, a1, PT_AREG8
	s32i	a9, a1, PT_AREG9
	s32i	a10, a1, PT_AREG10
	s32i	a11, a1, PT_AREG11
	_bbsi.l	a2, 3, 1f
	s32i	a12, a1, PT_AREG12
	s32i	a13, a1, PT_AREG13
	s32i	a14, a1, PT_AREG14
	s32i	a15, a1, PT_AREG15
	_bnei	a2, 1, 1f		# only one valid frame?

	/* Only one valid frame, skip saving regs. */

	j	2f

	/* Save the remaining registers.
	 * We have to save all registers up to the first '1' from
	 * the right, except the current frame (bit 0).
	 * Assume a2 is:  001001000110001
176
	 * All register frames starting from the top field to the marked '1'
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	 * must be saved.
	 */

1:	addi	a3, a2, -1		# eliminate '1' in bit 0: yyyyxxww0
	neg	a3, a3			# yyyyxxww0 -> YYYYXXWW1+1
	and	a3, a3, a2		# max. only one bit is set

	/* Find number of frames to save */

	ffs_ws	a0, a3			# number of frames to the '1' from left

	/* Store information into WMASK:
	 * bits 0..3: xxx1 masked lower 4 bits of the rotated windowstart,
	 * bits 4...: number of valid 4-register frames
	 */

	slli	a3, a0, 4		# number of frames to save in bits 8..4
	extui	a2, a2, 0, 4		# mask for the first 16 registers
	or	a2, a3, a2
	s32i	a2, a1, PT_WMASK	# needed when we restore the reg-file

	/* Save 4 registers at a time */

1:	rotw	-1
	s32i	a0, a5, PT_AREG_END - 16
	s32i	a1, a5, PT_AREG_END - 12
	s32i	a2, a5, PT_AREG_END - 8
	s32i	a3, a5, PT_AREG_END - 4
	addi	a0, a4, -1
	addi	a1, a5, -16
	_bnez	a0, 1b

	/* WINDOWBASE still in SAR! */

M
Max Filippov 已提交
211
	rsr	a2, sar			# original WINDOWBASE
212 213 214
	movi	a3, 1
	ssl	a2
	sll	a3, a3
M
Max Filippov 已提交
215 216
	wsr	a3, windowstart		# set corresponding WINDOWSTART bit
	wsr	a2, windowbase		# and WINDOWSTART
217 218 219 220
	rsync

	/* We are back to the original stack pointer (a1) */

221
2:	/* Now, jump to the common exception handler. */
222 223 224

	j	common_exception

225
ENDPROC(user_exception)
226 227 228 229 230 231 232 233 234 235 236 237 238

/*
 * First-level exit handler for kernel exceptions
 * Save special registers and the live window frame.
 * Note: Even though we changes the stack pointer, we don't have to do a
 *	 MOVSP here, as we do that when we return from the exception.
 *	 (See comment in the kernel exception exit code)
 *
 * Entry condition for kernel_exception:
 *
 *   a0:	trashed, original value saved on stack (PT_AREG0)
 *   a1:	a1
 *   a2:	new stack pointer, original in DEPC
239
 *   a3:	a3
240
 *   depc:	a2, original value saved on stack (PT_DEPC)
241
 *   excsave_1:	dispatch table
242 243 244 245 246 247 248 249 250 251
 *
 *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
 *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
 *
 * Entry condition for _kernel_exception:
 *
 *   a0-a3 and depc have been saved to PT_AREG0...PT_AREG3 and PT_DEPC
 *   excsave has been restored, and
 *   stack pointer (a1) has been set.
 *
D
Daniel Mack 已提交
252
 * Note: _kernel_exception might be at an odd address. Don't use call0..call12
253 254 255 256
 */

ENTRY(kernel_exception)

257
	/* Save a1, a2, a3, and set SP. */
258

M
Max Filippov 已提交
259
	rsr	a0, depc		# get a2
260 261 262 263 264 265 266 267 268 269 270
	s32i	a1, a2, PT_AREG1
	s32i	a0, a2, PT_AREG2
	s32i	a3, a2, PT_AREG3
	mov	a1, a2

	.globl _kernel_exception
_kernel_exception:

	/* Save SAR and turn off single stepping */

	movi	a2, 0
M
Max Filippov 已提交
271 272
	rsr	a3, sar
	xsr	a2, icountlevel
273
	s32i	a3, a1, PT_SAR
274
	s32i	a2, a1, PT_ICOUNTLEVEL
275 276 277 278

	/* Rotate ws so that the current windowbase is at bit0. */
	/* Assume ws = xxwww1yyyy. Rotate ws right, so that a2 = yyyyxxwww1 */

M
Max Filippov 已提交
279 280
	rsr	a2, windowbase		# don't need to save these, we only
	rsr	a3, windowstart		# need shifted windowstart: windowmask
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
	ssr	a2
	slli	a2, a3, 32-WSBITS
	src	a2, a3, a2
	srli	a2, a2, 32-WSBITS
	s32i	a2, a1, PT_WMASK	# needed for kernel_exception_exit

	/* Save only the live window-frame */

	_bbsi.l	a2, 1, 1f
	s32i	a4, a1, PT_AREG4
	s32i	a5, a1, PT_AREG5
	s32i	a6, a1, PT_AREG6
	s32i	a7, a1, PT_AREG7
	_bbsi.l	a2, 2, 1f
	s32i	a8, a1, PT_AREG8
	s32i	a9, a1, PT_AREG9
	s32i	a10, a1, PT_AREG10
	s32i	a11, a1, PT_AREG11
	_bbsi.l	a2, 3, 1f
	s32i	a12, a1, PT_AREG12
	s32i	a13, a1, PT_AREG13
	s32i	a14, a1, PT_AREG14
	s32i	a15, a1, PT_AREG15

1:

#ifdef KERNEL_STACK_OVERFLOW_CHECK

	/*  Stack overflow check, for debugging  */
	extui	a2, a1, TASK_SIZE_BITS,XX
	movi	a3, SIZE??
	_bge	a2, a3, out_of_stack_panic

#endif

/*
 * This is the common exception handler.
 * We get here from the user exception handler or simply by falling through
 * from the kernel exception handler.
 * Save the remaining special registers, switch to kernel mode, and jump
 * to the second-level exception handler.
 *
 */

common_exception:

327
	/* Save some registers, disable loops and clear the syscall flag. */
328

M
Max Filippov 已提交
329 330
	rsr	a2, debugcause
	rsr	a3, epc1
331 332 333
	s32i	a2, a1, PT_DEBUGCAUSE
	s32i	a3, a1, PT_PC

334
	movi	a2, -1
M
Max Filippov 已提交
335
	rsr	a3, excvaddr
336
	s32i	a2, a1, PT_SYSCALL
337 338
	movi	a2, 0
	s32i	a3, a1, PT_EXCVADDR
M
Max Filippov 已提交
339
	xsr	a2, lcount
340 341 342 343
	s32i	a2, a1, PT_LCOUNT

	/* It is now save to restore the EXC_TABLE_FIXUP variable. */

M
Max Filippov 已提交
344
	rsr	a0, exccause
345
	movi	a3, 0
M
Max Filippov 已提交
346
	rsr	a2, excsave1
347 348 349 350 351 352 353
	s32i	a0, a1, PT_EXCCAUSE
	s32i	a3, a2, EXC_TABLE_FIXUP

	/* All unrecoverable states are saved on stack, now, and a1 is valid,
	 * so we can allow exceptions and interrupts (*) again.
	 * Set PS(EXCM = 0, UM = 0, RING = 0, OWB = 0, WOE = 1, INTLEVEL = X)
	 *
354 355
	 * (*) We only allow interrupts if they were previously enabled and
	 *     we're not handling an IRQ
356 357
	 */

M
Max Filippov 已提交
358
	rsr	a3, ps
359 360
	addi	a0, a0, -EXCCAUSE_LEVEL1_INTERRUPT
	movi	a2, LOCKLEVEL
361 362
	extui	a3, a3, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
					# a3 = PS.INTLEVEL
363
	moveqz	a3, a2, a0		# a3 = LOCKLEVEL iff interrupt
364
	movi	a2, 1 << PS_WOE_BIT
365
	or	a3, a3, a2
M
Max Filippov 已提交
366 367
	rsr	a0, exccause
	xsr	a3, ps
368 369 370

	s32i	a3, a1, PT_PS		# save ps

M
Max Filippov 已提交
371
	/* Save lbeg, lend */
372

M
Max Filippov 已提交
373 374
	rsr	a2, lbeg
	rsr	a3, lend
375 376 377
	s32i	a2, a1, PT_LBEG
	s32i	a3, a1, PT_LEND

378 379 380 381 382 383 384
	/* Save SCOMPARE1 */

#if XCHAL_HAVE_S32C1I
	rsr     a2, scompare1
	s32i    a2, a1, PT_SCOMPARE1
#endif

385 386 387 388
	/* Save optional registers. */

	save_xtregs_opt a1 a2 a4 a5 a6 a7 PT_XTREGS_OPT
	
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
#ifdef CONFIG_TRACE_IRQFLAGS
	l32i	a4, a1, PT_DEPC
	/* Double exception means we came here with an exception
	 * while PS.EXCM was set, i.e. interrupts disabled.
	 */
	bgeui	a4, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
	l32i	a4, a1, PT_EXCCAUSE
	bnei	a4, EXCCAUSE_LEVEL1_INTERRUPT, 1f
	/* We came here with an interrupt means interrupts were enabled
	 * and we've just disabled them.
	 */
	movi	a4, trace_hardirqs_off
	callx4	a4
1:
#endif

405 406 407 408
	/* Go to second-level dispatcher. Set up parameters to pass to the
	 * exception handler and call the exception handler.
	 */

409
	rsr	a4, excsave1
410 411 412 413 414 415 416 417 418 419
	mov	a6, a1			# pass stack frame
	mov	a7, a0			# pass EXCCAUSE
	addx4	a4, a0, a4
	l32i	a4, a4, EXC_TABLE_DEFAULT		# load handler

	/* Call the second-level handler */

	callx4	a4

	/* Jump here for exception exit */
D
dann 已提交
420
	.global common_exception_return
421 422
common_exception_return:

423
1:
424
	rsil	a2, LOCKLEVEL
425

426 427
	/* Jump if we are returning from kernel exceptions. */

428
	l32i	a3, a1, PT_PS
M
Max Filippov 已提交
429 430 431
	GET_THREAD_INFO(a2, a1)
	l32i	a4, a2, TI_FLAGS
	_bbci.l	a3, PS_UM_BIT, 6f
432 433 434 435 436

	/* Specific to a user exception exit:
	 * We need to check some flags for signal handling and rescheduling,
	 * and have to restore WB and WS, extra states, and all registers
	 * in the register file that were in use in the user task.
437
	 * Note that we don't disable interrupts here. 
438 439 440
	 */

	_bbsi.l	a4, TIF_NEED_RESCHED, 3f
441
	_bbsi.l	a4, TIF_NOTIFY_RESUME, 2f
442
	_bbci.l	a4, TIF_SIGPENDING, 5f
443

444
2:	l32i	a4, a1, PT_DEPC
445 446
	bgeui	a4, VALID_DOUBLE_EXCEPTION_ADDRESS, 4f

447 448
	/* Call do_signal() */

449
	rsil	a2, 0
450
	movi	a4, do_notify_resume	# int do_notify_resume(struct pt_regs*)
451 452 453 454
	mov	a6, a1
	callx4	a4
	j	1b

455
3:	/* Reschedule */
456

457
	rsil	a2, 0
458 459 460 461
	movi	a4, schedule	# void schedule (void)
	callx4	a4
	j	1b

M
Max Filippov 已提交
462 463 464 465 466 467 468 469 470 471 472 473 474
#ifdef CONFIG_PREEMPT
6:
	_bbci.l	a4, TIF_NEED_RESCHED, 4f

	/* Check current_thread_info->preempt_count */

	l32i	a4, a2, TI_PRE_COUNT
	bnez	a4, 4f
	movi	a4, preempt_schedule_irq
	callx4	a4
	j	1b
#endif

475 476 477 478 479 480 481
5:
#ifdef CONFIG_DEBUG_TLB_SANITY
	l32i	a4, a1, PT_DEPC
	bgeui	a4, VALID_DOUBLE_EXCEPTION_ADDRESS, 4f
	movi	a4, check_tlb_sanity
	callx4	a4
#endif
M
Max Filippov 已提交
482
6:
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
4:
#ifdef CONFIG_TRACE_IRQFLAGS
	l32i	a4, a1, PT_DEPC
	/* Double exception means we came here with an exception
	 * while PS.EXCM was set, i.e. interrupts disabled.
	 */
	bgeui	a4, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
	l32i	a4, a1, PT_EXCCAUSE
	bnei	a4, EXCCAUSE_LEVEL1_INTERRUPT, 1f
	/* We came here with an interrupt means interrupts were enabled
	 * and we'll reenable them on return.
	 */
	movi	a4, trace_hardirqs_on
	callx4	a4
1:
#endif
	/* Restore optional registers. */
500 501

	load_xtregs_opt a1 a2 a4 a5 a6 a7 PT_XTREGS_OPT
502

503 504 505 506 507 508
	/* Restore SCOMPARE1 */

#if XCHAL_HAVE_S32C1I
	l32i    a2, a1, PT_SCOMPARE1
	wsr     a2, scompare1
#endif
M
Max Filippov 已提交
509
	wsr	a3, ps		/* disable interrupts */
510 511 512 513 514 515

	_bbci.l	a3, PS_UM_BIT, kernel_exception_exit

user_exception_exit:

	/* Restore the state of the task and return from the exception. */
516 517 518 519 520

	/* Switch to the user thread WINDOWBASE. Save SP temporarily in DEPC */

	l32i	a2, a1, PT_WINDOWBASE
	l32i	a3, a1, PT_WINDOWSTART
M
Max Filippov 已提交
521 522
	wsr	a1, depc		# use DEPC as temp storage
	wsr	a3, windowstart		# restore WINDOWSTART
523
	ssr	a2			# preserve user's WB in the SAR
M
Max Filippov 已提交
524
	wsr	a2, windowbase		# switch to user's saved WB
525
	rsync
M
Max Filippov 已提交
526
	rsr	a1, depc		# restore stack pointer
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
	l32i	a2, a1, PT_WMASK	# register frames saved (in bits 4...9)
	rotw	-1			# we restore a4..a7
	_bltui	a6, 16, 1f		# only have to restore current window?

	/* The working registers are a0 and a3.  We are restoring to
	 * a4..a7.  Be careful not to destroy what we have just restored.
	 * Note: wmask has the format YYYYM:
	 *       Y: number of registers saved in groups of 4
	 *       M: 4 bit mask of first 16 registers
	 */

	mov	a2, a6
	mov	a3, a5

2:	rotw	-1			# a0..a3 become a4..a7
	addi	a3, a7, -4*4		# next iteration
	addi	a2, a6, -16		# decrementing Y in WMASK
	l32i	a4, a3, PT_AREG_END + 0
	l32i	a5, a3, PT_AREG_END + 4
	l32i	a6, a3, PT_AREG_END + 8
	l32i	a7, a3, PT_AREG_END + 12
	_bgeui	a2, 16, 2b

	/* Clear unrestored registers (don't leak anything to user-land */

M
Max Filippov 已提交
552 553
1:	rsr	a0, windowbase
	rsr	a3, sar
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
	sub	a3, a0, a3
	beqz	a3, 2f
	extui	a3, a3, 0, WBBITS

1:	rotw	-1
	addi	a3, a7, -1
	movi	a4, 0
	movi	a5, 0
	movi	a6, 0
	movi	a7, 0
	bgei	a3, 1, 1b

	/* We are back were we were when we started.
	 * Note: a2 still contains WMASK (if we've returned to the original
	 *	 frame where we had loaded a2), or at least the lower 4 bits
	 *	 (if we have restored WSBITS-1 frames).
	 */

C
Chris Zankel 已提交
572 573 574 575 576
#if XCHAL_HAVE_THREADPTR
	l32i	a3, a1, PT_THREADPTR
	wur	a3, threadptr
#endif

577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
2:	j	common_exception_exit

	/* This is the kernel exception exit.
	 * We avoided to do a MOVSP when we entered the exception, but we
	 * have to do it here.
	 */

kernel_exception_exit:

	/* Check if we have to do a movsp.
	 *
	 * We only have to do a movsp if the previous window-frame has
	 * been spilled to the *temporary* exception stack instead of the
	 * task's stack. This is the case if the corresponding bit in
	 * WINDOWSTART for the previous window-frame was set before
	 * (not spilled) but is zero now (spilled).
	 * If this bit is zero, all other bits except the one for the
	 * current window frame are also zero. So, we can use a simple test:
	 * 'and' WINDOWSTART and WINDOWSTART-1:
	 *
	 *  (XXXXXX1[0]* - 1) AND XXXXXX1[0]* = XXXXXX0[0]*
	 *
	 * The result is zero only if one bit was set.
	 *
	 * (Note: We might have gone through several task switches before
	 *        we come back to the current task, so WINDOWBASE might be
	 *        different from the time the exception occurred.)
	 */

	/* Test WINDOWSTART before and after the exception.
	 * We actually have WMASK, so we only have to test if it is 1 or not.
	 */

	l32i	a2, a1, PT_WMASK
	_beqi	a2, 1, common_exception_exit	# Spilled before exception,jump

	/* Test WINDOWSTART now. If spilled, do the movsp */

M
Max Filippov 已提交
615
	rsr     a3, windowstart
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
	addi	a0, a3, -1
	and     a3, a3, a0
	_bnez	a3, common_exception_exit

	/* Do a movsp (we returned from a call4, so we have at least a0..a7) */

	addi    a0, a1, -16
	l32i    a3, a0, 0
	l32i    a4, a0, 4
	s32i    a3, a1, PT_SIZE+0
	s32i    a4, a1, PT_SIZE+4
	l32i    a3, a0, 8
	l32i    a4, a0, 12
	s32i    a3, a1, PT_SIZE+8
	s32i    a4, a1, PT_SIZE+12

	/* Common exception exit.
	 * We restore the special register and the current window frame, and
	 * return from the exception.
	 *
	 * Note: We expect a2 to hold PT_WMASK
	 */

common_exception_exit:

641 642
	/* Restore address registers. */

643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
	_bbsi.l	a2, 1, 1f
	l32i	a4,  a1, PT_AREG4
	l32i	a5,  a1, PT_AREG5
	l32i	a6,  a1, PT_AREG6
	l32i	a7,  a1, PT_AREG7
	_bbsi.l	a2, 2, 1f
	l32i	a8,  a1, PT_AREG8
	l32i	a9,  a1, PT_AREG9
	l32i	a10, a1, PT_AREG10
	l32i	a11, a1, PT_AREG11
	_bbsi.l	a2, 3, 1f
	l32i	a12, a1, PT_AREG12
	l32i	a13, a1, PT_AREG13
	l32i	a14, a1, PT_AREG14
	l32i	a15, a1, PT_AREG15

	/* Restore PC, SAR */

1:	l32i	a2, a1, PT_PC
	l32i	a3, a1, PT_SAR
M
Max Filippov 已提交
663 664
	wsr	a2, epc1
	wsr	a3, sar
665 666 667 668 669

	/* Restore LBEG, LEND, LCOUNT */

	l32i	a2, a1, PT_LBEG
	l32i	a3, a1, PT_LEND
M
Max Filippov 已提交
670
	wsr	a2, lbeg
671
	l32i	a2, a1, PT_LCOUNT
M
Max Filippov 已提交
672 673
	wsr	a3, lend
	wsr	a2, lcount
674

675 676 677 678
	/* We control single stepping through the ICOUNTLEVEL register. */

	l32i	a2, a1, PT_ICOUNTLEVEL
	movi	a3, -2
M
Max Filippov 已提交
679 680
	wsr	a2, icountlevel
	wsr	a3, icount
681

682 683 684 685 686
	/* Check if it was double exception. */

	l32i	a0, a1, PT_DEPC
	l32i	a3, a1, PT_AREG3
	l32i	a2, a1, PT_AREG2
687
	_bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
688 689 690 691 692

	/* Restore a0...a3 and return */

	l32i	a0, a1, PT_AREG0
	l32i	a1, a1, PT_AREG1
693
	rfe
694

695
1: 	wsr	a0, depc
696 697
	l32i	a0, a1, PT_AREG0
	l32i	a1, a1, PT_AREG1
698
	rfde
699

700 701
ENDPROC(kernel_exception)

702 703 704 705 706 707 708 709 710 711
/*
 * Debug exception handler.
 *
 * Currently, we don't support KGDB, so only user application can be debugged.
 *
 * When we get here,  a0 is trashed and saved to excsave[debuglevel]
 */

ENTRY(debug_exception)

M
Max Filippov 已提交
712
	rsr	a0, SREG_EPS + XCHAL_DEBUGLEVEL
713
	bbsi.l	a0, PS_EXCM_BIT, 1f	# exception mode
714

M
Max Filippov 已提交
715
	/* Set EPC1 and EXCCAUSE */
716

M
Max Filippov 已提交
717 718 719
	wsr	a2, depc		# save a2 temporarily
	rsr	a2, SREG_EPC + XCHAL_DEBUGLEVEL
	wsr	a2, epc1
720 721

	movi	a2, EXCCAUSE_MAPPED_DEBUG
M
Max Filippov 已提交
722
	wsr	a2, exccause
723 724 725

	/* Restore PS to the value before the debug exc but with PS.EXCM set.*/

726
	movi	a2, 1 << PS_EXCM_BIT
727 728
	or	a2, a0, a2
	movi	a0, debug_exception	# restore a3, debug jump vector
M
Max Filippov 已提交
729 730
	wsr	a2, ps
	xsr	a0, SREG_EXCSAVE + XCHAL_DEBUGLEVEL
731 732 733

	/* Switch to kernel/user stack, restore jump vector, and save a0 */

734
	bbsi.l	a2, PS_UM_BIT, 2f	# jump if user mode
735 736 737 738 739 740

	addi	a2, a1, -16-PT_SIZE	# assume kernel stack
	s32i	a0, a2, PT_AREG0
	movi	a0, 0
	s32i	a1, a2, PT_AREG1
	s32i	a0, a2, PT_DEPC		# mark it as a regular exception
M
Max Filippov 已提交
741
	xsr	a0, depc
742 743 744 745 746
	s32i	a3, a2, PT_AREG3
	s32i	a0, a2, PT_AREG2
	mov	a1, a2
	j	_kernel_exception

M
Max Filippov 已提交
747
2:	rsr	a2, excsave1
748 749 750 751 752
	l32i	a2, a2, EXC_TABLE_KSTK	# load kernel stack pointer
	s32i	a0, a2, PT_AREG0
	movi	a0, 0
	s32i	a1, a2, PT_AREG1
	s32i	a0, a2, PT_DEPC
M
Max Filippov 已提交
753
	xsr	a0, depc
754 755 756 757 758 759 760 761
	s32i	a3, a2, PT_AREG3
	s32i	a0, a2, PT_AREG2
	mov	a1, a2
	j	_user_exception

	/* Debug exception while in exception mode. */
1:	j	1b	// FIXME!!

762
ENDPROC(debug_exception)
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793

/*
 * We get here in case of an unrecoverable exception.
 * The only thing we can do is to be nice and print a panic message.
 * We only produce a single stack frame for panic, so ???
 *
 *
 * Entry conditions:
 *
 *   - a0 contains the caller address; original value saved in excsave1.
 *   - the original a0 contains a valid return address (backtrace) or 0.
 *   - a2 contains a valid stackpointer
 *
 * Notes:
 *
 *   - If the stack pointer could be invalid, the caller has to setup a
 *     dummy stack pointer (e.g. the stack of the init_task)
 *
 *   - If the return address could be invalid, the caller has to set it
 *     to 0, so the backtrace would stop.
 *
 */
	.align 4
unrecoverable_text:
	.ascii "Unrecoverable error in exception handler\0"

ENTRY(unrecoverable_exception)

	movi	a0, 1
	movi	a1, 0

M
Max Filippov 已提交
794 795
	wsr	a0, windowstart
	wsr	a1, windowbase
796 797
	rsync

798
	movi	a1, (1 << PS_WOE_BIT) | LOCKLEVEL
M
Max Filippov 已提交
799
	wsr	a1, ps
800 801 802 803 804 805 806 807 808 809 810 811 812
	rsync

	movi	a1, init_task
	movi	a0, 0
	addi	a1, a1, PT_REGS_OFFSET

	movi	a4, panic
	movi	a6, unrecoverable_text

	callx4	a4

1:	j	1b

813
ENDPROC(unrecoverable_exception)
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832

/* -------------------------- FAST EXCEPTION HANDLERS ----------------------- */

/*
 * Fast-handler for alloca exceptions
 *
 *  The ALLOCA handler is entered when user code executes the MOVSP
 *  instruction and the caller's frame is not in the register file.
 *  In this case, the caller frame's a0..a3 are on the stack just
 *  below sp (a1), and this handler moves them.
 *
 *  For "MOVSP <ar>,<as>" without destination register a1, this routine
 *  simply moves the value from <as> to <ar> without moving the save area.
 *
 * Entry condition:
 *
 *   a0:	trashed, original value saved on stack (PT_AREG0)
 *   a1:	a1
 *   a2:	new stack pointer, original in DEPC
833
 *   a3:	a3
834
 *   depc:	a2, original value saved on stack (PT_DEPC)
835
 *   excsave_1:	dispatch table
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
 *
 *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
 *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
 */

#if XCHAL_HAVE_BE
#define _EXTUI_MOVSP_SRC(ar)	extui ar, ar, 4, 4
#define _EXTUI_MOVSP_DST(ar)	extui ar, ar, 0, 4
#else
#define _EXTUI_MOVSP_SRC(ar)	extui ar, ar, 0, 4
#define _EXTUI_MOVSP_DST(ar)	extui ar, ar, 4, 4
#endif

ENTRY(fast_alloca)

	/* We shouldn't be in a double exception. */

	l32i	a0, a2, PT_DEPC
	_bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, .Lunhandled_double

M
Max Filippov 已提交
856
	rsr	a0, depc		# get a2
857
	s32i	a4, a2, PT_AREG4	# save a4 and
858
	s32i	a3, a2, PT_AREG3
859 860 861 862 863
	s32i	a0, a2, PT_AREG2	# a2 to stack

	/* Exit critical section. */

	movi	a0, 0
864
	rsr	a3, excsave1
865 866
	s32i	a0, a3, EXC_TABLE_FIXUP

M
Max Filippov 已提交
867
	rsr	a4, epc1		# get exception address
868 869 870 871 872 873 874 875 876 877 878 879 880

#ifdef ALLOCA_EXCEPTION_IN_IRAM
#error	iram not supported
#else
	/* Note: l8ui not allowed in IRAM/IROM!! */
	l8ui	a0, a4, 1		# read as(src) from MOVSP instruction
#endif
	movi	a3, .Lmovsp_src
	_EXTUI_MOVSP_SRC(a0)		# extract source register number
	addx8	a3, a0, a3
	jx	a3

.Lunhandled_double:
M
Max Filippov 已提交
881
	wsr	a0, excsave1
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
	movi	a0, unrecoverable_exception
	callx0	a0

	.align 8
.Lmovsp_src:
	l32i	a3, a2, PT_AREG0;	_j 1f;	.align 8
	mov	a3, a1;			_j 1f;	.align 8
	l32i	a3, a2, PT_AREG2;	_j 1f;	.align 8
	l32i	a3, a2, PT_AREG3;	_j 1f;	.align 8
	l32i	a3, a2, PT_AREG4;	_j 1f;	.align 8
	mov	a3, a5;			_j 1f;	.align 8
	mov	a3, a6;			_j 1f;	.align 8
	mov	a3, a7;			_j 1f;	.align 8
	mov	a3, a8;			_j 1f;	.align 8
	mov	a3, a9;			_j 1f;	.align 8
	mov	a3, a10;		_j 1f;	.align 8
	mov	a3, a11;		_j 1f;	.align 8
	mov	a3, a12;		_j 1f;	.align 8
	mov	a3, a13;		_j 1f;	.align 8
	mov	a3, a14;		_j 1f;	.align 8
	mov	a3, a15;		_j 1f;	.align 8

1:

#ifdef ALLOCA_EXCEPTION_IN_IRAM
#error	iram not supported
#else
	l8ui	a0, a4, 0		# read ar(dst) from MOVSP instruction
#endif
	addi	a4, a4, 3		# step over movsp
	_EXTUI_MOVSP_DST(a0)		# extract destination register
M
Max Filippov 已提交
913
	wsr	a4, epc1		# save new epc_1
914 915 916

	_bnei	a0, 1, 1f		# no 'movsp a1, ax': jump

917
	/* Move the save area. This implies the use of the L32E
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
	 * and S32E instructions, because this move must be done with
	 * the user's PS.RING privilege levels, not with ring 0
	 * (kernel's) privileges currently active with PS.EXCM
	 * set. Note that we have stil registered a fixup routine with the
	 * double exception vector in case a double exception occurs.
	 */

	/* a0,a4:avail a1:old user stack a2:exc. stack a3:new user stack. */

	l32e	a0, a1, -16
	l32e	a4, a1, -12
	s32e	a0, a3, -16
	s32e	a4, a3, -12
	l32e	a0, a1, -8
	l32e	a4, a1, -4
	s32e	a0, a3, -8
	s32e	a4, a3, -4

	/* Restore stack-pointer and all the other saved registers. */

	mov	a1, a3

	l32i	a4, a2, PT_AREG4
	l32i	a3, a2, PT_AREG3
	l32i	a0, a2, PT_AREG0
	l32i	a2, a2, PT_AREG2
	rfe

	/*  MOVSP <at>,<as>  was invoked with <at> != a1.
	 *  Because the stack pointer is not being modified,
	 *  we should be able to just modify the pointer
	 *  without moving any save area.
	 *  The processor only traps these occurrences if the
	 *  caller window isn't live, so unfortunately we can't
	 *  use this as an alternate trap mechanism.
	 *  So we just do the move.  This requires that we
	 *  resolve the destination register, not just the source,
	 *  so there's some extra work.
	 *  (PERHAPS NOT REALLY NEEDED, BUT CLEANER...)
	 */

	/* a0 dst-reg, a1 user-stack, a2 stack, a3 value of src reg. */

1:	movi	a4, .Lmovsp_dst
	addx8	a4, a0, a4
	jx	a4

	.align 8
.Lmovsp_dst:
	s32i	a3, a2, PT_AREG0;	_j 1f;	.align 8
	mov	a1, a3;			_j 1f;	.align 8
	s32i	a3, a2, PT_AREG2;	_j 1f;	.align 8
	s32i	a3, a2, PT_AREG3;	_j 1f;	.align 8
	s32i	a3, a2, PT_AREG4;	_j 1f;	.align 8
	mov	a5, a3;			_j 1f;	.align 8
	mov	a6, a3;			_j 1f;	.align 8
	mov	a7, a3;			_j 1f;	.align 8
	mov	a8, a3;			_j 1f;	.align 8
	mov	a9, a3;			_j 1f;	.align 8
	mov	a10, a3;		_j 1f;	.align 8
	mov	a11, a3;		_j 1f;	.align 8
	mov	a12, a3;		_j 1f;	.align 8
	mov	a13, a3;		_j 1f;	.align 8
	mov	a14, a3;		_j 1f;	.align 8
	mov	a15, a3;		_j 1f;	.align 8

1:	l32i	a4, a2, PT_AREG4
	l32i	a3, a2, PT_AREG3
	l32i	a0, a2, PT_AREG0
	l32i	a2, a2, PT_AREG2
	rfe

990
ENDPROC(fast_alloca)
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005

/*
 * fast system calls.
 *
 * WARNING:  The kernel doesn't save the entire user context before
 * handling a fast system call.  These functions are small and short,
 * usually offering some functionality not available to user tasks.
 *
 * BE CAREFUL TO PRESERVE THE USER'S CONTEXT.
 *
 * Entry condition:
 *
 *   a0:	trashed, original value saved on stack (PT_AREG0)
 *   a1:	a1
 *   a2:	new stack pointer, original in DEPC
1006
 *   a3:	a3
1007
 *   depc:	a2, original value saved on stack (PT_DEPC)
1008
 *   excsave_1:	dispatch table
1009 1010 1011 1012 1013 1014
 */

ENTRY(fast_syscall_kernel)

	/* Skip syscall. */

M
Max Filippov 已提交
1015
	rsr	a0, epc1
1016
	addi	a0, a0, 3
M
Max Filippov 已提交
1017
	wsr	a0, epc1
1018 1019 1020 1021

	l32i	a0, a2, PT_DEPC
	bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, fast_syscall_unrecoverable

M
Max Filippov 已提交
1022
	rsr	a0, depc			# get syscall-nr
1023
	_beqz	a0, fast_syscall_spill_registers
1024
	_beqi	a0, __NR_xtensa, fast_syscall_xtensa
1025 1026 1027

	j	kernel_exception

1028 1029
ENDPROC(fast_syscall_kernel)

1030 1031 1032 1033
ENTRY(fast_syscall_user)

	/* Skip syscall. */

M
Max Filippov 已提交
1034
	rsr	a0, epc1
1035
	addi	a0, a0, 3
M
Max Filippov 已提交
1036
	wsr	a0, epc1
1037 1038 1039 1040

	l32i	a0, a2, PT_DEPC
	bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, fast_syscall_unrecoverable

M
Max Filippov 已提交
1041
	rsr	a0, depc			# get syscall-nr
1042
	_beqz	a0, fast_syscall_spill_registers
1043
	_beqi	a0, __NR_xtensa, fast_syscall_xtensa
1044 1045 1046

	j	user_exception

1047 1048
ENDPROC(fast_syscall_user)

1049 1050
ENTRY(fast_syscall_unrecoverable)

1051
	/* Restore all states. */
1052

1053 1054
	l32i    a0, a2, PT_AREG0        # restore a0
	xsr     a2, depc                # restore a2, depc
1055

1056 1057 1058
	wsr     a0, excsave1
	movi    a0, unrecoverable_exception
	callx0  a0
1059

1060
ENDPROC(fast_syscall_unrecoverable)
1061 1062 1063 1064

/*
 * sysxtensa syscall handler
 *
1065 1066 1067 1068 1069
 * int sysxtensa (SYS_XTENSA_ATOMIC_SET,     ptr, val,    unused);
 * int sysxtensa (SYS_XTENSA_ATOMIC_ADD,     ptr, val,    unused);
 * int sysxtensa (SYS_XTENSA_ATOMIC_EXG_ADD, ptr, val,    unused);
 * int sysxtensa (SYS_XTENSA_ATOMIC_CMP_SWP, ptr, oldval, newval);
 *        a2            a6                   a3    a4      a5
1070 1071 1072
 *
 * Entry condition:
 *
1073
 *   a0:	a2 (syscall-nr), original value saved on stack (PT_AREG0)
1074
 *   a1:	a1
1075
 *   a2:	new stack pointer, original in a0 and DEPC
1076
 *   a3:	a3
1077
 *   a4..a15:	unchanged
1078
 *   depc:	a2, original value saved on stack (PT_DEPC)
1079
 *   excsave_1:	dispatch table
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
 *
 *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
 *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
 *
 * Note: we don't have to save a2; a2 holds the return value
 *
 * We use the two macros TRY and CATCH:
 *
 * TRY	 adds an entry to the __ex_table fixup table for the immediately
 *	 following instruction.
 *
L
Lucas De Marchi 已提交
1091
 * CATCH catches any exception that occurred at one of the preceding TRY
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
 *       statements and continues from there
 *
 * Usage TRY	l32i	a0, a1, 0
 *		<other code>
 *	 done:	rfe
 *	 CATCH	<set return code>
 *		j done
 */

#define TRY								\
	.section __ex_table, "a";					\
	.word	66f, 67f;						\
	.text;								\
66:

#define CATCH								\
67:

1110
ENTRY(fast_syscall_xtensa)
1111

1112
	s32i	a7, a2, PT_AREG7	# we need an additional register
1113
	movi	a7, 4			# sizeof(unsigned int)
1114
	access_ok a3, a7, a0, a2, .Leac	# a0: scratch reg, a2: sp
1115

1116 1117 1118
	addi	a6, a6, -1		# assuming SYS_XTENSA_ATOMIC_SET = 1
	_bgeui	a6, SYS_XTENSA_COUNT - 1, .Lill
	_bnei	a6, SYS_XTENSA_ATOMIC_CMP_SWP - 1, .Lnswp
1119

1120
	/* Fall through for ATOMIC_CMP_SWP. */
1121 1122 1123

.Lswp:	/* Atomic compare and swap */

1124 1125 1126 1127 1128 1129 1130 1131
TRY	l32i	a0, a3, 0		# read old value
	bne	a0, a4, 1f		# same as old value? jump
TRY	s32i	a5, a3, 0		# different, modify value
	l32i	a7, a2, PT_AREG7	# restore a7
	l32i	a0, a2, PT_AREG0	# restore a0
	movi	a2, 1			# and return 1
	addi	a6, a6, 1		# restore a6 (really necessary?)
	rfe
1132

1133 1134 1135 1136 1137
1:	l32i	a7, a2, PT_AREG7	# restore a7
	l32i	a0, a2, PT_AREG0	# restore a0
	movi	a2, 0			# return 0 (note that we cannot set
	addi	a6, a6, 1		# restore a6 (really necessary?)
	rfe
1138

1139
.Lnswp:	/* Atomic set, add, and exg_add. */
1140

1141 1142 1143 1144
TRY	l32i	a7, a3, 0		# orig
	add	a0, a4, a7		# + arg
	moveqz	a0, a4, a6		# set
TRY	s32i	a0, a3, 0		# write new value
1145

1146
	mov	a0, a2
1147
	mov	a2, a7
1148 1149 1150
	l32i	a7, a0, PT_AREG7	# restore a7
	l32i	a0, a0, PT_AREG0	# restore a0
	addi	a6, a6, 1		# restore a6 (really necessary?)
1151 1152 1153
	rfe

CATCH
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
.Leac:	l32i	a7, a2, PT_AREG7	# restore a7
	l32i	a0, a2, PT_AREG0	# restore a0
	movi	a2, -EFAULT
	rfe

.Lill:	l32i	a7, a2, PT_AREG0	# restore a7
	l32i	a0, a2, PT_AREG0	# restore a0
	movi	a2, -EINVAL
	rfe

1164
ENDPROC(fast_syscall_xtensa)
1165 1166 1167 1168 1169 1170 1171 1172 1173


/* fast_syscall_spill_registers.
 *
 * Entry condition:
 *
 *   a0:	trashed, original value saved on stack (PT_AREG0)
 *   a1:	a1
 *   a2:	new stack pointer, original in DEPC
1174
 *   a3:	a3
1175
 *   depc:	a2, original value saved on stack (PT_DEPC)
1176
 *   excsave_1:	dispatch table
1177 1178 1179 1180 1181 1182 1183 1184
 *
 * Note: We assume the stack pointer is EXC_TABLE_KSTK in the fixup handler.
 */

ENTRY(fast_syscall_spill_registers)

	/* Register a FIXUP handler (pass current wb as a parameter) */

1185
	xsr	a3, excsave1
1186 1187
	movi	a0, fast_syscall_spill_registers_fixup
	s32i	a0, a3, EXC_TABLE_FIXUP
M
Max Filippov 已提交
1188
	rsr	a0, windowbase
1189
	s32i	a0, a3, EXC_TABLE_PARAM
1190
	xsr	a3, excsave1		# restore a3 and excsave_1
1191

1192
	/* Save a3, a4 and SAR on stack. */
1193

M
Max Filippov 已提交
1194
	rsr	a0, sar
1195
	s32i	a3, a2, PT_AREG3
1196 1197
	s32i	a4, a2, PT_AREG4
	s32i	a0, a2, PT_AREG5	# store SAR to PT_AREG5
1198 1199 1200

	/* The spill routine might clobber a7, a11, and a15. */

1201 1202 1203
	s32i	a7, a2, PT_AREG7
	s32i	a11, a2, PT_AREG11
	s32i	a15, a2, PT_AREG15
1204

1205
	call0	_spill_registers	# destroys a3, a4, and SAR
1206 1207 1208

	/* Advance PC, restore registers and SAR, and return from exception. */

1209 1210
	l32i	a3, a2, PT_AREG5
	l32i	a4, a2, PT_AREG4
1211
	l32i	a0, a2, PT_AREG0
M
Max Filippov 已提交
1212
	wsr	a3, sar
1213 1214 1215 1216
	l32i	a3, a2, PT_AREG3

	/* Restore clobbered registers. */

1217 1218 1219
	l32i	a7, a2, PT_AREG7
	l32i	a11, a2, PT_AREG11
	l32i	a15, a2, PT_AREG15
1220 1221 1222 1223

	movi	a2, 0
	rfe

1224 1225
ENDPROC(fast_syscall_spill_registers)

1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
/* Fixup handler.
 *
 * We get here if the spill routine causes an exception, e.g. tlb miss.
 * We basically restore WINDOWBASE and WINDOWSTART to the condition when
 * we entered the spill routine and jump to the user exception handler.
 *
 * a0: value of depc, original value in depc
 * a2: trashed, original value in EXC_TABLE_DOUBLE_SAVE
 * a3: exctable, original value in excsave1
 */

fast_syscall_spill_registers_fixup:

M
Max Filippov 已提交
1239 1240
	rsr	a2, windowbase	# get current windowbase (a2 is saved)
	xsr	a0, depc	# restore depc and a0
1241 1242 1243 1244 1245 1246 1247
	ssl	a2		# set shift (32 - WB)

	/* We need to make sure the current registers (a0-a3) are preserved.
	 * To do this, we simply set the bit for the current window frame
	 * in WS, so that the exception handlers save them to the task stack.
	 */

1248
	xsr	a3, excsave1	# get spill-mask
1249 1250 1251 1252
	slli	a2, a3, 1	# shift left by one

	slli	a3, a2, 32-WSBITS
	src	a2, a2, a3	# a1 = xxwww1yyxxxwww1yy......
M
Max Filippov 已提交
1253
	wsr	a2, windowstart	# set corrected windowstart
1254

1255
	rsr	a3, excsave1
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
	l32i	a2, a3, EXC_TABLE_DOUBLE_SAVE	# restore a2
	l32i	a3, a3, EXC_TABLE_PARAM	# original WB (in user task)

	/* Return to the original (user task) WINDOWBASE.
	 * We leave the following frame behind:
	 * a0, a1, a2	same
	 * a3:		trashed (saved in excsave_1)
	 * depc:	depc (we have to return to that address)
	 * excsave_1:	a3
	 */

M
Max Filippov 已提交
1267
	wsr	a3, windowbase
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
	rsync

	/* We are now in the original frame when we entered _spill_registers:
	 *  a0: return address
	 *  a1: used, stack pointer
	 *  a2: kernel stack pointer
	 *  a3: available, saved in EXCSAVE_1
	 *  depc: exception address
	 *  excsave: a3
	 * Note: This frame might be the same as above.
	 */

	/* Setup stack pointer. */

	addi	a2, a2, -PT_USER_SIZE
	s32i	a0, a2, PT_AREG0

	/* Make sure we return to this fixup handler. */

	movi	a3, fast_syscall_spill_registers_fixup_return
	s32i	a3, a2, PT_DEPC		# setup depc

	/* Jump to the exception handler. */

1292
	rsr	a3, excsave1
M
Max Filippov 已提交
1293
	rsr	a0, exccause
1294 1295 1296
	addx4	a0, a0, a3              	# find entry in table
	l32i	a0, a0, EXC_TABLE_FAST_USER     # load handler
	jx	a0
1297 1298 1299 1300 1301

fast_syscall_spill_registers_fixup_return:

	/* When we return here, all registers have been restored (a2: DEPC) */

M
Max Filippov 已提交
1302
	wsr	a2, depc		# exception address
1303 1304 1305

	/* Restore fixup handler. */

M
Max Filippov 已提交
1306
	xsr	a3, excsave1
1307 1308
	movi	a2, fast_syscall_spill_registers_fixup
	s32i	a2, a3, EXC_TABLE_FIXUP
1309
	s32i	a0, a3, EXC_TABLE_DOUBLE_SAVE
M
Max Filippov 已提交
1310
	rsr	a2, windowbase
1311 1312 1313 1314 1315
	s32i	a2, a3, EXC_TABLE_PARAM
	l32i	a2, a3, EXC_TABLE_KSTK

	/* Load WB at the time the exception occurred. */

M
Max Filippov 已提交
1316
	rsr	a3, sar			# WB is still in SAR
1317
	neg	a3, a3
M
Max Filippov 已提交
1318
	wsr	a3, windowbase
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
	rsync

	rfde


/*
 * spill all registers.
 *
 * This is not a real function. The following conditions must be met:
 *
 *  - must be called with call0.
1330
 *  - uses a3, a4 and SAR.
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
 *  - the last 'valid' register of each frame are clobbered.
 *  - the caller must have registered a fixup handler
 *    (or be inside a critical section)
 *  - PS_EXCM must be set (PS_WOE cleared?)
 */

ENTRY(_spill_registers)

	/*
	 * Rotate ws so that the current windowbase is at bit 0.
	 * Assume ws = xxxwww1yy (www1 current window frame).
1342
	 * Rotate ws right so that a4 = yyxxxwww1.
1343 1344
	 */

M
Max Filippov 已提交
1345 1346
	rsr	a4, windowbase
	rsr	a3, windowstart		# a3 = xxxwww1yy
1347 1348 1349
	ssr	a4			# holds WB
	slli	a4, a3, WSBITS
	or	a3, a3, a4		# a3 = xxxwww1yyxxxwww1yy
1350
	srl	a3, a3			# a3 = 00xxxwww1yyxxxwww1
1351 1352 1353

	/* We are done if there are no more than the current register frame. */

1354
	extui	a3, a3, 1, WSBITS-1	# a3 = 0yyxxxwww
1355
	movi	a4, (1 << (WSBITS-1))
1356 1357 1358 1359
	_beqz	a3, .Lnospill		# only one active frame? jump

	/* We want 1 at the top, so that we return to the current windowbase */

1360
	or	a3, a3, a4		# 1yyxxxwww
1361 1362 1363

	/* Skip empty frames - get 'oldest' WINDOWSTART-bit. */

M
Max Filippov 已提交
1364
	wsr	a3, windowstart		# save shifted windowstart
1365 1366
	neg	a4, a3
	and	a3, a4, a3		# first bit set from right: 000010000
1367

1368
	ffs_ws	a4, a3			# a4: shifts to skip empty frames
1369
	movi	a3, WSBITS
1370 1371
	sub	a4, a3, a4		# WSBITS-a4:number of 0-bits from right
	ssr	a4			# save in SAR for later.
1372

M
Max Filippov 已提交
1373
	rsr	a3, windowbase
1374
	add	a3, a3, a4
M
Max Filippov 已提交
1375
	wsr	a3, windowbase
1376 1377
	rsync

M
Max Filippov 已提交
1378
	rsr	a3, windowstart
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
	srl	a3, a3			# shift windowstart

	/* WB is now just one frame below the oldest frame in the register
	   window. WS is shifted so the oldest frame is in bit 0, thus, WB
	   and WS differ by one 4-register frame. */

	/* Save frames. Depending what call was used (call4, call8, call12),
	 * we have to save 4,8. or 12 registers.
	 */

	_bbsi.l	a3, 1, .Lc4
	_bbsi.l	a3, 2, .Lc8

	/* Special case: we have a call12-frame starting at a4. */

	_bbci.l	a3, 3, .Lc12	# bit 3 shouldn't be zero! (Jump to Lc12 first)

	s32e	a4, a1, -16	# a1 is valid with an empty spill area
	l32e	a4, a5, -12
	s32e	a8, a4, -48
	mov	a8, a4
	l32e	a4, a1, -16
	j	.Lc12c

1403
.Lnospill:
1404
	ret
1405

1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
.Lloop: _bbsi.l	a3, 1, .Lc4
	_bbci.l	a3, 2, .Lc12

.Lc8:	s32e	a4, a13, -16
	l32e	a4, a5, -12
	s32e	a8, a4, -32
	s32e	a5, a13, -12
	s32e	a6, a13, -8
	s32e	a7, a13, -4
	s32e	a9, a4, -28
	s32e	a10, a4, -24
	s32e	a11, a4, -20

	srli	a11, a3, 2		# shift windowbase by 2
	rotw	2
	_bnei	a3, 1, .Lloop

.Lexit: /* Done. Do the final rotation, set WS, and return. */

	rotw	1
M
Max Filippov 已提交
1426
	rsr	a3, windowbase
1427 1428 1429
	ssl	a3
	movi	a3, 1
	sll	a3, a3
M
Max Filippov 已提交
1430
	wsr	a3, windowstart
1431
	ret
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490

.Lc4:	s32e	a4, a9, -16
	s32e	a5, a9, -12
	s32e	a6, a9, -8
	s32e	a7, a9, -4

	srli	a7, a3, 1
	rotw	1
	_bnei	a3, 1, .Lloop
	j	.Lexit

.Lc12:	_bbci.l	a3, 3, .Linvalid_mask	# bit 2 shouldn't be zero!

	/* 12-register frame (call12) */

	l32e	a2, a5, -12
	s32e	a8, a2, -48
	mov	a8, a2

.Lc12c: s32e	a9, a8, -44
	s32e	a10, a8, -40
	s32e	a11, a8, -36
	s32e	a12, a8, -32
	s32e	a13, a8, -28
	s32e	a14, a8, -24
	s32e	a15, a8, -20
	srli	a15, a3, 3

	/* The stack pointer for a4..a7 is out of reach, so we rotate the
	 * window, grab the stackpointer, and rotate back.
	 * Alternatively, we could also use the following approach, but that
	 * makes the fixup routine much more complicated:
	 * rotw	1
	 * s32e	a0, a13, -16
	 * ...
	 * rotw 2
	 */

	rotw	1
	mov	a5, a13
	rotw	-1

	s32e	a4, a9, -16
	s32e	a5, a9, -12
	s32e	a6, a9, -8
	s32e	a7, a9, -4

	rotw	3

	_beqi	a3, 1, .Lexit
	j	.Lloop

.Linvalid_mask:

	/* We get here because of an unrecoverable error in the window
	 * registers. If we are in user space, we kill the application,
	 * however, this condition is unrecoverable in kernel space.
	 */

M
Max Filippov 已提交
1491
	rsr	a0, ps
1492
	_bbci.l	a0, PS_UM_BIT, 1f
1493

1494
	/* User space: Setup a dummy frame and kill application.
1495 1496 1497 1498 1499 1500
	 * Note: We assume EXC_TABLE_KSTK contains a valid stack pointer.
	 */

	movi	a0, 1
	movi	a1, 0

M
Max Filippov 已提交
1501 1502
	wsr	a0, windowstart
	wsr	a1, windowbase
1503 1504 1505 1506
	rsync

	movi	a0, 0

1507
	rsr	a3, excsave1
1508 1509
	l32i	a1, a3, EXC_TABLE_KSTK

1510
	movi	a4, (1 << PS_WOE_BIT) | LOCKLEVEL
M
Max Filippov 已提交
1511
	wsr	a4, ps
1512 1513 1514 1515 1516 1517 1518 1519
	rsync

	movi	a6, SIGSEGV
	movi	a4, do_exit
	callx4	a4

1:	/* Kernel space: PANIC! */

M
Max Filippov 已提交
1520
	wsr	a0, excsave1
1521 1522 1523 1524
	movi	a0, unrecoverable_exception
	callx0	a0		# should not return
1:	j	1b

1525 1526
ENDPROC(_spill_registers)

J
Johannes Weiner 已提交
1527
#ifdef CONFIG_MMU
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
/*
 * We should never get here. Bail out!
 */

ENTRY(fast_second_level_miss_double_kernel)

1:	movi	a0, unrecoverable_exception
	callx0	a0		# should not return
1:	j	1b

1538 1539
ENDPROC(fast_second_level_miss_double_kernel)

1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
/* First-level entry handler for user, kernel, and double 2nd-level
 * TLB miss exceptions.  Note that for now, user and kernel miss
 * exceptions share the same entry point and are handled identically.
 *
 * An old, less-efficient C version of this function used to exist.
 * We include it below, interleaved as comments, for reference.
 *
 * Entry condition:
 *
 *   a0:	trashed, original value saved on stack (PT_AREG0)
 *   a1:	a1
 *   a2:	new stack pointer, original in DEPC
1552
 *   a3:	a3
1553
 *   depc:	a2, original value saved on stack (PT_DEPC)
1554
 *   excsave_1:	dispatch table
1555 1556 1557 1558 1559 1560 1561
 *
 *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
 *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
 */

ENTRY(fast_second_level_miss)

1562
	/* Save a1 and a3. Note: we don't expect a double exception. */
1563 1564

	s32i	a1, a2, PT_AREG1
1565
	s32i	a3, a2, PT_AREG3
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586

	/* We need to map the page of PTEs for the user task.  Find
	 * the pointer to that page.  Also, it's possible for tsk->mm
	 * to be NULL while tsk->active_mm is nonzero if we faulted on
	 * a vmalloc address.  In that rare case, we must use
	 * active_mm instead to avoid a fault in this handler.  See
	 *
	 * http://mail.nl.linux.org/linux-mm/2002-08/msg00258.html
	 *   (or search Internet on "mm vs. active_mm")
	 *
	 *	if (!mm)
	 *		mm = tsk->active_mm;
	 *	pgd = pgd_offset (mm, regs->excvaddr);
	 *	pmd = pmd_offset (pgd, regs->excvaddr);
	 *	pmdval = *pmd;
	 */

	GET_CURRENT(a1,a2)
	l32i	a0, a1, TASK_MM		# tsk->mm
	beqz	a0, 9f

M
Max Filippov 已提交
1587
8:	rsr	a3, excvaddr		# fault address
1588
	_PGD_OFFSET(a0, a3, a1)
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
	l32i	a0, a0, 0		# read pmdval
	beqz	a0, 2f

	/* Read ptevaddr and convert to top of page-table page.
	 *
	 * 	vpnval = read_ptevaddr_register() & PAGE_MASK;
	 * 	vpnval += DTLB_WAY_PGTABLE;
	 *	pteval = mk_pte (virt_to_page(pmd_val(pmdval)), PAGE_KERNEL);
	 *	write_dtlb_entry (pteval, vpnval);
	 *
	 * The messy computation for 'pteval' above really simplifies
	 * into the following:
	 *
1602
	 * pteval = ((pmdval - PAGE_OFFSET) & PAGE_MASK) | PAGE_DIRECTORY
1603 1604
	 */

C
Chris Zankel 已提交
1605
	movi	a1, (-PAGE_OFFSET) & 0xffffffff
1606 1607 1608 1609
	add	a0, a0, a1		# pmdval - PAGE_OFFSET
	extui	a1, a0, 0, PAGE_SHIFT	# ... & PAGE_MASK
	xor	a0, a0, a1

1610
	movi	a1, _PAGE_DIRECTORY
1611 1612
	or	a0, a0, a1		# ... | PAGE_DIRECTORY

1613
	/*
1614
	 * We utilize all three wired-ways (7-9) to hold pmd translations.
1615 1616 1617 1618 1619 1620 1621 1622 1623
	 * Memory regions are mapped to the DTLBs according to bits 28 and 29.
	 * This allows to map the three most common regions to three different
	 * DTLBs:
	 *  0,1 -> way 7	program (0040.0000) and virtual (c000.0000)
	 *  2   -> way 8	shared libaries (2000.0000)
	 *  3   -> way 0	stack (3000.0000)
	 */

	extui	a3, a3, 28, 2		# addr. bit 28 and 29	0,1,2,3
M
Max Filippov 已提交
1624
	rsr	a1, ptevaddr
1625
	addx2	a3, a3, a3		# ->			0,3,6,9
1626
	srli	a1, a1, PAGE_SHIFT
1627
	extui	a3, a3, 2, 2		# ->			0,0,1,2
1628
	slli	a1, a1, PAGE_SHIFT	# ptevaddr & PAGE_MASK
1629 1630
	addi	a3, a3, DTLB_WAY_PGD
	add	a1, a1, a3		# ... + way_number
1631

1632
3:	wdtlb	a0, a1
1633 1634 1635 1636
	dsync

	/* Exit critical section. */

1637
4:	rsr	a3, excsave1
1638 1639 1640 1641 1642 1643 1644
	movi	a0, 0
	s32i	a0, a3, EXC_TABLE_FIXUP

	/* Restore the working registers, and return. */

	l32i	a0, a2, PT_AREG0
	l32i	a1, a2, PT_AREG1
1645
	l32i	a3, a2, PT_AREG3
1646 1647 1648 1649 1650 1651
	l32i	a2, a2, PT_DEPC

	bgeui	a2, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f

	/* Restore excsave1 and return. */

M
Max Filippov 已提交
1652
	rsr	a2, depc
1653 1654 1655 1656
	rfe

	/* Return from double exception. */

M
Max Filippov 已提交
1657
1:	xsr	a2, depc
1658 1659 1660 1661 1662 1663
	esync
	rfde

9:	l32i	a0, a1, TASK_ACTIVE_MM	# unlikely case mm == 0
	j	8b

1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
#if (DCACHE_WAY_SIZE > PAGE_SIZE)

2:	/* Special case for cache aliasing.
	 * We (should) only get here if a clear_user_page, copy_user_page
	 * or the aliased cache flush functions got preemptively interrupted 
	 * by another task. Re-establish temporary mapping to the 
	 * TLBTEMP_BASE areas.
	 */

	/* We shouldn't be in a double exception */

	l32i	a0, a2, PT_DEPC
	bgeui	a0, VALID_DOUBLE_EXCEPTION_ADDRESS, 2f

	/* Make sure the exception originated in the special functions */

	movi	a0, __tlbtemp_mapping_start
M
Max Filippov 已提交
1681
	rsr	a3, epc1
1682 1683 1684 1685 1686 1687 1688
	bltu	a3, a0, 2f
	movi	a0, __tlbtemp_mapping_end
	bgeu	a3, a0, 2f

	/* Check if excvaddr was in one of the TLBTEMP_BASE areas. */

	movi	a3, TLBTEMP_BASE_1
M
Max Filippov 已提交
1689
	rsr	a0, excvaddr
1690 1691 1692 1693 1694 1695 1696 1697
	bltu	a0, a3, 2f

	addi	a1, a0, -(2 << (DCACHE_ALIAS_ORDER + PAGE_SHIFT))
	bgeu	a1, a3, 2f

	/* Check if we have to restore an ITLB mapping. */

	movi	a1, __tlbtemp_mapping_itlb
M
Max Filippov 已提交
1698
	rsr	a3, epc1
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
	sub	a3, a3, a1

	/* Calculate VPN */

	movi	a1, PAGE_MASK
	and	a1, a1, a0

	/* Jump for ITLB entry */

	bgez	a3, 1f

	/* We can use up to two TLBTEMP areas, one for src and one for dst. */

	extui	a3, a0, PAGE_SHIFT + DCACHE_ALIAS_ORDER, 1
	add	a1, a3, a1

	/* PPN is in a6 for the first TLBTEMP area and in a7 for the second. */

	mov	a0, a6
	movnez	a0, a7, a3
	j	3b

	/* ITLB entry. We only use dst in a6. */

1:	witlb	a6, a1
	isync
	j	4b


#endif	// DCACHE_WAY_SIZE > PAGE_SIZE


1731 1732
2:	/* Invalid PGD, default exception handling */

M
Max Filippov 已提交
1733
	rsr	a1, depc
1734 1735 1736
	s32i	a1, a2, PT_AREG2
	mov	a1, a2

M
Max Filippov 已提交
1737
	rsr	a2, ps
1738
	bbsi.l	a2, PS_UM_BIT, 1f
1739 1740 1741
	j	_kernel_exception
1:	j	_user_exception

1742
ENDPROC(fast_second_level_miss)
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753

/*
 * StoreProhibitedException
 *
 * Update the pte and invalidate the itlb mapping for this pte.
 *
 * Entry condition:
 *
 *   a0:	trashed, original value saved on stack (PT_AREG0)
 *   a1:	a1
 *   a2:	new stack pointer, original in DEPC
1754
 *   a3:	a3
1755
 *   depc:	a2, original value saved on stack (PT_DEPC)
1756
 *   excsave_1:	dispatch table
1757 1758 1759 1760 1761 1762 1763
 *
 *   PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
 *	     <  VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
 */

ENTRY(fast_store_prohibited)

1764
	/* Save a1 and a3. */
1765 1766

	s32i	a1, a2, PT_AREG1
1767
	s32i	a3, a2, PT_AREG3
1768 1769 1770 1771 1772

	GET_CURRENT(a1,a2)
	l32i	a0, a1, TASK_MM		# tsk->mm
	beqz	a0, 9f

M
Max Filippov 已提交
1773
8:	rsr	a1, excvaddr		# fault address
1774
	_PGD_OFFSET(a0, a1, a3)
1775 1776 1777
	l32i	a0, a0, 0
	beqz	a0, 2f

1778 1779 1780 1781
	/*
	 * Note that we test _PAGE_WRITABLE_BIT only if PTE is present
	 * and is not PAGE_NONE. See pgtable.h for possible PTE layouts.
	 */
1782

1783 1784
	_PTE_OFFSET(a0, a1, a3)
	l32i	a3, a0, 0		# read pteval
1785
	movi	a1, _PAGE_CA_INVALID
1786 1787
	ball	a3, a1, 2f
	bbci.l	a3, _PAGE_WRITABLE_BIT, 2f
1788

1789
	movi	a1, _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_HW_WRITE
1790
	or	a3, a3, a1
M
Max Filippov 已提交
1791
	rsr	a1, excvaddr
1792
	s32i	a3, a0, 0
1793 1794 1795 1796 1797 1798

	/* We need to flush the cache if we have page coloring. */
#if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
	dhwb	a0, 0
#endif
	pdtlb	a0, a1
1799
	wdtlb	a3, a0
1800 1801 1802 1803

	/* Exit critical section. */

	movi	a0, 0
1804
	rsr	a3, excsave1
1805 1806 1807 1808
	s32i	a0, a3, EXC_TABLE_FIXUP

	/* Restore the working registers, and return. */

1809
	l32i	a3, a2, PT_AREG3
1810 1811 1812 1813 1814 1815
	l32i	a1, a2, PT_AREG1
	l32i	a0, a2, PT_AREG0
	l32i	a2, a2, PT_DEPC

	bgeui	a2, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f

M
Max Filippov 已提交
1816
	rsr	a2, depc
1817 1818 1819 1820
	rfe

	/* Double exception. Restore FIXUP handler and return. */

M
Max Filippov 已提交
1821
1:	xsr	a2, depc
1822 1823 1824 1825 1826 1827 1828 1829
	esync
	rfde

9:	l32i	a0, a1, TASK_ACTIVE_MM	# unlikely case mm == 0
	j	8b

2:	/* If there was a problem, handle fault in C */

1830 1831
	rsr	a3, depc	# still holds a2
	s32i	a3, a2, PT_AREG2
1832 1833
	mov	a1, a2

M
Max Filippov 已提交
1834
	rsr	a2, ps
1835
	bbsi.l	a2, PS_UM_BIT, 1f
1836 1837
	j	_kernel_exception
1:	j	_user_exception
1838 1839 1840

ENDPROC(fast_store_prohibited)

J
Johannes Weiner 已提交
1841
#endif /* CONFIG_MMU */
1842

1843 1844 1845 1846 1847 1848 1849 1850
/*
 * System Calls.
 *
 * void system_call (struct pt_regs* regs, int exccause)
 *                            a2                 a3
 */

ENTRY(system_call)
1851

1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895
	entry	a1, 32

	/* regs->syscall = regs->areg[2] */

	l32i	a3, a2, PT_AREG2
	mov	a6, a2
	movi	a4, do_syscall_trace_enter
	s32i	a3, a2, PT_SYSCALL
	callx4	a4

	/* syscall = sys_call_table[syscall_nr] */

	movi	a4, sys_call_table;
	movi	a5, __NR_syscall_count
	movi	a6, -ENOSYS
	bgeu	a3, a5, 1f

	addx4	a4, a3, a4
	l32i	a4, a4, 0
	movi	a5, sys_ni_syscall;
	beq	a4, a5, 1f

	/* Load args: arg0 - arg5 are passed via regs. */

	l32i	a6, a2, PT_AREG6
	l32i	a7, a2, PT_AREG3
	l32i	a8, a2, PT_AREG4
	l32i	a9, a2, PT_AREG5
	l32i	a10, a2, PT_AREG8
	l32i	a11, a2, PT_AREG9

	/* Pass one additional argument to the syscall: pt_regs (on stack) */
	s32i	a2, a1, 0

	callx4	a4

1:	/* regs->areg[2] = return_value */

	s32i	a6, a2, PT_AREG2
	movi	a4, do_syscall_trace_leave
	mov	a6, a2
	callx4	a4
	retw

1896 1897
ENDPROC(system_call)

1898

1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
/*
 * Task switch.
 *
 * struct task*  _switch_to (struct task* prev, struct task* next)
 *         a2                              a2                 a3
 */

ENTRY(_switch_to)

	entry	a1, 16

1910 1911
	mov	a12, a2			# preserve 'prev' (a2)
	mov	a13, a3			# and 'next' (a3)
1912

1913 1914
	l32i	a4, a2, TASK_THREAD_INFO
	l32i	a5, a3, TASK_THREAD_INFO
1915

1916
	save_xtregs_user a4 a6 a8 a9 a10 a11 THREAD_XTREGS_USER
1917

1918 1919 1920 1921 1922 1923
	s32i	a0, a12, THREAD_RA	# save return address
	s32i	a1, a12, THREAD_SP	# save stack pointer

	/* Disable ints while we manipulate the stack pointer. */

	movi	a14, (1 << PS_EXCM_BIT) | LOCKLEVEL
M
Max Filippov 已提交
1924 1925
	xsr	a14, ps
	rsr	a3, excsave1
1926 1927 1928
	rsync
	s32i	a3, a3, EXC_TABLE_FIXUP	/* enter critical section */

1929 1930 1931 1932
	/* Switch CPENABLE */

#if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
	l32i	a3, a5, THREAD_CPENABLE
M
Max Filippov 已提交
1933
	xsr	a3, cpenable
1934 1935 1936 1937 1938 1939
	s32i	a3, a4, THREAD_CPENABLE
#endif

	/* Flush register file. */

	call0	_spill_registers	# destroys a3, a4, and SAR
1940 1941 1942 1943 1944 1945 1946

	/* Set kernel stack (and leave critical section)
	 * Note: It's save to set it here. The stack will not be overwritten
	 *       because the kernel stack will only be loaded again after
	 *       we return from kernel space.
	 */

M
Max Filippov 已提交
1947
	rsr	a3, excsave1		# exc_table
1948 1949 1950 1951
	movi	a6, 0
	addi	a7, a5, PT_REGS_OFFSET
	s32i	a6, a3, EXC_TABLE_FIXUP
	s32i	a7, a3, EXC_TABLE_KSTK
1952

C
Chris Zankel 已提交
1953
	/* restore context of the task 'next' */
1954

1955 1956 1957 1958
	l32i	a0, a13, THREAD_RA	# restore return address
	l32i	a1, a13, THREAD_SP	# restore stack pointer

	load_xtregs_user a5 a6 a8 a9 a10 a11 THREAD_XTREGS_USER
1959

M
Max Filippov 已提交
1960
	wsr	a14, ps
1961
	mov	a2, a12			# return 'prev'
1962 1963 1964 1965
	rsync

	retw

1966
ENDPROC(_switch_to)
1967 1968 1969 1970 1971 1972 1973 1974 1975

ENTRY(ret_from_fork)

	/* void schedule_tail (struct task_struct *prev)
	 * Note: prev is still in a6 (return value from fake call4 frame)
	 */
	movi	a4, schedule_tail
	callx4	a4

1976 1977
	movi	a4, do_syscall_trace_leave
	mov	a6, a1
1978 1979 1980 1981
	callx4	a4

	j	common_exception_return

1982 1983
ENDPROC(ret_from_fork)

1984 1985 1986 1987 1988 1989 1990 1991 1992 1993
/*
 * Kernel thread creation helper
 * On entry, set up by copy_thread: a2 = thread_fn, a3 = thread_fn arg
 *           left from _switch_to: a6 = prev
 */
ENTRY(ret_from_kernel_thread)

	call4	schedule_tail
	mov	a6, a3
	callx4	a2
1994
	j	common_exception_return
1995 1996

ENDPROC(ret_from_kernel_thread)