2 * Low-level exception handling
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
8 * Copyright (C) 2004 - 2008 by Tensilica Inc.
9 * Copyright (C) 2015 Cadence Design Systems Inc.
15 #include <linux/linkage.h>
16 #include <linux/pgtable.h>
17 #include <asm/asm-offsets.h>
18 #include <asm/asmmacro.h>
19 #include <asm/processor.h>
20 #include <asm/coprocessor.h>
21 #include <asm/thread_info.h>
22 #include <asm/asm-uaccess.h>
23 #include <asm/unistd.h>
24 #include <asm/ptrace.h>
25 #include <asm/current.h>
27 #include <asm/signal.h>
28 #include <asm/tlbflush.h>
29 #include <variant/tie-asm.h>
31 /* Unimplemented features. */
33 #undef KERNEL_STACK_OVERFLOW_CHECK
41 * Macro to find first bit set in WINDOWBASE from the left + 1
48 .macro ffs_ws bit mask
51 nsau \bit, \mask # 32-WSBITS ... 31 (32 iff 0)
52 addi \bit, \bit, WSBITS - 32 + 1 # uppest bit set -> return 1
56 _bltui \mask, 0x10000, 99f
58 extui \mask, \mask, 16, 16
61 99: _bltui \mask, 0x100, 99f
65 99: _bltui \mask, 0x10, 99f
68 99: _bltui \mask, 0x4, 99f
71 99: _bltui \mask, 0x2, 99f
79 .macro irq_save flags tmp
81 #if defined(CONFIG_DEBUG_KERNEL) && (LOCKLEVEL | TOPLEVEL) >= XCHAL_DEBUGLEVEL
83 extui \tmp, \flags, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
84 bgei \tmp, LOCKLEVEL, 99f
90 or \flags, \flags, \tmp
95 rsil \flags, LOCKLEVEL
99 /* ----------------- DEFAULT FIRST LEVEL EXCEPTION HANDLERS ----------------- */
102 * First-level exception handler for user exceptions.
103 * Save some special registers, extra states and all registers in the AR
104 * register file that were in use in the user task, and jump to the common
106 * We save SAR (used to calculate WMASK), and WB and WS (we don't have to
107 * save them for kernel exceptions).
109 * Entry condition for user_exception:
111 * a0: trashed, original value saved on stack (PT_AREG0)
113 * a2: new stack pointer, original value in depc
115 * depc: a2, original value saved on stack (PT_DEPC)
116 * excsave1: dispatch table
118 * PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
119 * < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
121 * Entry condition for _user_exception:
123 * a0-a3 and depc have been saved to PT_AREG0...PT_AREG3 and PT_DEPC
124 * excsave has been restored, and
125 * stack pointer (a1) has been set.
127 * Note: _user_exception might be at an odd address. Don't use call0..call12
131 ENTRY(user_exception)
133 /* Save a1, a2, a3, and set SP. */
136 s32i a1, a2, PT_AREG1
137 s32i a0, a2, PT_AREG2
138 s32i a3, a2, PT_AREG3
141 .globl _user_exception
144 /* Save SAR and turn off single stepping */
147 wsr a2, depc # terminate user stack trace with 0
151 s32i a2, a1, PT_ICOUNTLEVEL
153 #if XCHAL_HAVE_THREADPTR
155 s32i a2, a1, PT_THREADPTR
158 /* Rotate ws so that the current windowbase is at bit0. */
159 /* Assume ws = xxwww1yyyy. Rotate ws right, so that a2 = yyyyxxwww1 */
161 #if defined(USER_SUPPORT_WINDOWED)
165 s32i a2, a1, PT_WINDOWBASE
166 s32i a3, a1, PT_WINDOWSTART
167 slli a2, a3, 32-WSBITS
169 srli a2, a2, 32-WSBITS
170 s32i a2, a1, PT_WMASK # needed for restoring registers
174 s32i a2, a1, PT_WINDOWBASE
175 s32i a3, a1, PT_WINDOWSTART
176 s32i a3, a1, PT_WMASK
179 /* Save only live registers. */
181 UABI_W _bbsi.l a2, 1, 1f
182 s32i a4, a1, PT_AREG4
183 s32i a5, a1, PT_AREG5
184 s32i a6, a1, PT_AREG6
185 s32i a7, a1, PT_AREG7
186 UABI_W _bbsi.l a2, 2, 1f
187 s32i a8, a1, PT_AREG8
188 s32i a9, a1, PT_AREG9
189 s32i a10, a1, PT_AREG10
190 s32i a11, a1, PT_AREG11
191 UABI_W _bbsi.l a2, 3, 1f
192 s32i a12, a1, PT_AREG12
193 s32i a13, a1, PT_AREG13
194 s32i a14, a1, PT_AREG14
195 s32i a15, a1, PT_AREG15
197 #if defined(USER_SUPPORT_WINDOWED)
198 _bnei a2, 1, 1f # only one valid frame?
200 /* Only one valid frame, skip saving regs. */
204 /* Save the remaining registers.
205 * We have to save all registers up to the first '1' from
206 * the right, except the current frame (bit 0).
207 * Assume a2 is: 001001000110001
208 * All register frames starting from the top field to the marked '1'
212 1: addi a3, a2, -1 # eliminate '1' in bit 0: yyyyxxww0
213 neg a3, a3 # yyyyxxww0 -> YYYYXXWW1+1
214 and a3, a3, a2 # max. only one bit is set
216 /* Find number of frames to save */
218 ffs_ws a0, a3 # number of frames to the '1' from left
220 /* Store information into WMASK:
221 * bits 0..3: xxx1 masked lower 4 bits of the rotated windowstart,
222 * bits 4...: number of valid 4-register frames
225 slli a3, a0, 4 # number of frames to save in bits 8..4
226 extui a2, a2, 0, 4 # mask for the first 16 registers
228 s32i a2, a1, PT_WMASK # needed when we restore the reg-file
230 /* Save 4 registers at a time */
233 s32i a0, a5, PT_AREG_END - 16
234 s32i a1, a5, PT_AREG_END - 12
235 s32i a2, a5, PT_AREG_END - 8
236 s32i a3, a5, PT_AREG_END - 4
241 /* WINDOWBASE still in SAR! */
243 rsr a2, sar # original WINDOWBASE
247 wsr a3, windowstart # set corresponding WINDOWSTART bit
248 wsr a2, windowbase # and WINDOWSTART
251 /* We are back to the original stack pointer (a1) */
253 2: /* Now, jump to the common exception handler. */
257 ENDPROC(user_exception)
260 * First-level exit handler for kernel exceptions
261 * Save special registers and the live window frame.
262 * Note: Even though we changes the stack pointer, we don't have to do a
263 * MOVSP here, as we do that when we return from the exception.
264 * (See comment in the kernel exception exit code)
266 * Entry condition for kernel_exception:
268 * a0: trashed, original value saved on stack (PT_AREG0)
270 * a2: new stack pointer, original in DEPC
272 * depc: a2, original value saved on stack (PT_DEPC)
273 * excsave_1: dispatch table
275 * PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
276 * < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
278 * Entry condition for _kernel_exception:
280 * a0-a3 and depc have been saved to PT_AREG0...PT_AREG3 and PT_DEPC
281 * excsave has been restored, and
282 * stack pointer (a1) has been set.
284 * Note: _kernel_exception might be at an odd address. Don't use call0..call12
287 ENTRY(kernel_exception)
289 /* Save a1, a2, a3, and set SP. */
291 rsr a0, depc # get a2
292 s32i a1, a2, PT_AREG1
293 s32i a0, a2, PT_AREG2
294 s32i a3, a2, PT_AREG3
297 .globl _kernel_exception
300 /* Save SAR and turn off single stepping */
306 s32i a2, a1, PT_ICOUNTLEVEL
308 #if defined(__XTENSA_WINDOWED_ABI__)
309 /* Rotate ws so that the current windowbase is at bit0. */
310 /* Assume ws = xxwww1yyyy. Rotate ws right, so that a2 = yyyyxxwww1 */
312 rsr a2, windowbase # don't need to save these, we only
313 rsr a3, windowstart # need shifted windowstart: windowmask
315 slli a2, a3, 32-WSBITS
317 srli a2, a2, 32-WSBITS
318 s32i a2, a1, PT_WMASK # needed for kernel_exception_exit
321 /* Save only the live window-frame */
323 KABI_W _bbsi.l a2, 1, 1f
324 s32i a4, a1, PT_AREG4
325 s32i a5, a1, PT_AREG5
326 s32i a6, a1, PT_AREG6
327 s32i a7, a1, PT_AREG7
328 KABI_W _bbsi.l a2, 2, 1f
329 s32i a8, a1, PT_AREG8
330 s32i a9, a1, PT_AREG9
331 s32i a10, a1, PT_AREG10
332 s32i a11, a1, PT_AREG11
333 KABI_W _bbsi.l a2, 3, 1f
334 s32i a12, a1, PT_AREG12
335 s32i a13, a1, PT_AREG13
336 s32i a14, a1, PT_AREG14
337 s32i a15, a1, PT_AREG15
339 #ifdef __XTENSA_WINDOWED_ABI__
341 /* Copy spill slots of a0 and a1 to imitate movsp
342 * in order to keep exception stack continuous
344 l32i a3, a1, PT_KERNEL_SIZE
345 l32i a0, a1, PT_KERNEL_SIZE + 4
350 l32i a0, a1, PT_AREG0 # restore saved a0
353 #ifdef KERNEL_STACK_OVERFLOW_CHECK
355 /* Stack overflow check, for debugging */
356 extui a2, a1, TASK_SIZE_BITS,XX
358 _bge a2, a3, out_of_stack_panic
363 * This is the common exception handler.
364 * We get here from the user exception handler or simply by falling through
365 * from the kernel exception handler.
366 * Save the remaining special registers, switch to kernel mode, and jump
367 * to the second-level exception handler.
373 /* Save some registers, disable loops and clear the syscall flag. */
377 s32i a2, a1, PT_DEBUGCAUSE
382 s32i a2, a1, PT_SYSCALL
384 s32i a3, a1, PT_EXCVADDR
387 s32i a2, a1, PT_LCOUNT
390 #if XCHAL_HAVE_EXCLUSIVE
391 /* Clear exclusive access monitor set by interrupted code */
395 /* It is now save to restore the EXC_TABLE_FIXUP variable. */
400 s32i a2, a1, PT_EXCCAUSE
401 s32i a3, a0, EXC_TABLE_FIXUP
403 /* All unrecoverable states are saved on stack, now, and a1 is valid.
404 * Now we can allow exceptions again. In case we've got an interrupt
405 * PS.INTLEVEL is set to LOCKLEVEL disabling furhter interrupts,
406 * otherwise it's left unchanged.
408 * Set PS(EXCM = 0, UM = 0, RING = 0, OWB = 0, WOE = 1, INTLEVEL = X)
412 s32i a3, a1, PT_PS # save ps
415 /* Correct PS needs to be saved in the PT_PS:
416 * - in case of exception or level-1 interrupt it's in the PS,
417 * and is already saved.
418 * - in case of medium level interrupt it's in the excsave2.
420 movi a0, EXCCAUSE_MAPPED_NMI
421 extui a3, a3, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
422 beq a2, a0, .Lmedium_level_irq
423 bnei a2, EXCCAUSE_LEVEL1_INTERRUPT, .Lexception
424 beqz a3, .Llevel1_irq # level-1 IRQ sets ps.intlevel to 0
428 s32i a0, a1, PT_PS # save medium-level interrupt ps
429 bgei a3, LOCKLEVEL, .Lexception
435 KABI_W movi a0, PS_WOE_MASK
438 addi a2, a2, -EXCCAUSE_LEVEL1_INTERRUPT
440 extui a3, a3, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
442 moveqz a3, a0, a2 # a3 = LOCKLEVEL iff interrupt
443 KABI_W movi a2, PS_WOE_MASK
448 /* restore return address (or 0 if return to userspace) */
451 rsync # PS.WOE => rsync => overflow
453 /* Save lbeg, lend */
463 #if XCHAL_HAVE_S32C1I
465 s32i a3, a1, PT_SCOMPARE1
468 /* Save optional registers. */
470 save_xtregs_opt a1 a3 a4 a5 a6 a7 PT_XTREGS_OPT
472 /* Go to second-level dispatcher. Set up parameters to pass to the
473 * exception handler and call the exception handler.
478 l32i a4, a4, EXC_TABLE_DEFAULT # load handler
479 mov abi_arg1, a2 # pass EXCCAUSE
480 mov abi_arg0, a1 # pass stack frame
482 /* Call the second-level handler */
486 /* Jump here for exception exit */
487 .global common_exception_return
488 common_exception_return:
491 l32i abi_tmp0, a1, PT_EXCCAUSE
492 movi abi_tmp1, EXCCAUSE_MAPPED_NMI
493 l32i abi_saved1, a1, PT_PS
494 beq abi_tmp0, abi_tmp1, .Lrestore_state
498 #ifdef CONFIG_TRACE_IRQFLAGS
499 abi_call trace_hardirqs_off
502 /* Jump if we are returning from kernel exceptions. */
504 l32i abi_saved1, a1, PT_PS
505 GET_THREAD_INFO(a2, a1)
506 l32i a4, a2, TI_FLAGS
507 _bbci.l abi_saved1, PS_UM_BIT, .Lexit_tif_loop_kernel
509 /* Specific to a user exception exit:
510 * We need to check some flags for signal handling and rescheduling,
511 * and have to restore WB and WS, extra states, and all registers
512 * in the register file that were in use in the user task.
513 * Note that we don't disable interrupts here.
516 _bbsi.l a4, TIF_NEED_RESCHED, .Lresched
517 movi a2, _TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_NOTIFY_SIGNAL
518 bnone a4, a2, .Lexit_tif_loop_user
521 bgeui a4, VALID_DOUBLE_EXCEPTION_ADDRESS, .Lrestore_state
523 /* Call do_signal() */
525 #ifdef CONFIG_TRACE_IRQFLAGS
526 abi_call trace_hardirqs_on
530 abi_call do_notify_resume # int do_notify_resume(struct pt_regs*)
534 #ifdef CONFIG_TRACE_IRQFLAGS
535 abi_call trace_hardirqs_on
538 abi_call schedule # void schedule (void)
541 .Lexit_tif_loop_kernel:
542 #ifdef CONFIG_PREEMPTION
543 _bbci.l a4, TIF_NEED_RESCHED, .Lrestore_state
545 /* Check current_thread_info->preempt_count */
547 l32i a4, a2, TI_PRE_COUNT
548 bnez a4, .Lrestore_state
549 abi_call preempt_schedule_irq
553 .Lexit_tif_loop_user:
554 #ifdef CONFIG_HAVE_HW_BREAKPOINT
555 _bbci.l a4, TIF_DB_DISABLED, 1f
556 abi_call restore_dbreak
559 #ifdef CONFIG_DEBUG_TLB_SANITY
561 bgeui a4, VALID_DOUBLE_EXCEPTION_ADDRESS, .Lrestore_state
562 abi_call check_tlb_sanity
566 #ifdef CONFIG_TRACE_IRQFLAGS
567 extui a4, abi_saved1, PS_INTLEVEL_SHIFT, PS_INTLEVEL_WIDTH
568 bgei a4, LOCKLEVEL, 1f
569 abi_call trace_hardirqs_on
572 /* Restore optional registers. */
574 load_xtregs_opt a1 a2 a4 a5 a6 a7 PT_XTREGS_OPT
576 /* Restore SCOMPARE1 */
578 #if XCHAL_HAVE_S32C1I
579 l32i a2, a1, PT_SCOMPARE1
582 wsr abi_saved1, ps /* disable interrupts */
584 _bbci.l abi_saved1, PS_UM_BIT, kernel_exception_exit
588 /* Restore the state of the task and return from the exception. */
590 #if defined(USER_SUPPORT_WINDOWED)
591 /* Switch to the user thread WINDOWBASE. Save SP temporarily in DEPC */
593 l32i a2, a1, PT_WINDOWBASE
594 l32i a3, a1, PT_WINDOWSTART
595 wsr a1, depc # use DEPC as temp storage
596 wsr a3, windowstart # restore WINDOWSTART
597 ssr a2 # preserve user's WB in the SAR
598 wsr a2, windowbase # switch to user's saved WB
600 rsr a1, depc # restore stack pointer
601 l32i a2, a1, PT_WMASK # register frames saved (in bits 4...9)
602 rotw -1 # we restore a4..a7
603 _bltui a6, 16, .Lclear_regs # only have to restore current window?
605 /* The working registers are a0 and a3. We are restoring to
606 * a4..a7. Be careful not to destroy what we have just restored.
607 * Note: wmask has the format YYYYM:
608 * Y: number of registers saved in groups of 4
609 * M: 4 bit mask of first 16 registers
615 1: rotw -1 # a0..a3 become a4..a7
616 addi a3, a7, -4*4 # next iteration
617 addi a2, a6, -16 # decrementing Y in WMASK
618 l32i a4, a3, PT_AREG_END + 0
619 l32i a5, a3, PT_AREG_END + 4
620 l32i a6, a3, PT_AREG_END + 8
621 l32i a7, a3, PT_AREG_END + 12
624 /* Clear unrestored registers (don't leak anything to user-land */
631 extui a3, a3, 0, WBBITS
641 /* We are back were we were when we started.
642 * Note: a2 still contains WMASK (if we've returned to the original
643 * frame where we had loaded a2), or at least the lower 4 bits
644 * (if we have restored WSBITS-1 frames).
650 #if XCHAL_HAVE_THREADPTR
651 l32i a3, a1, PT_THREADPTR
655 j common_exception_exit
657 /* This is the kernel exception exit.
658 * We avoided to do a MOVSP when we entered the exception, but we
659 * have to do it here.
662 kernel_exception_exit:
664 #if defined(__XTENSA_WINDOWED_ABI__)
665 /* Check if we have to do a movsp.
667 * We only have to do a movsp if the previous window-frame has
668 * been spilled to the *temporary* exception stack instead of the
669 * task's stack. This is the case if the corresponding bit in
670 * WINDOWSTART for the previous window-frame was set before
671 * (not spilled) but is zero now (spilled).
672 * If this bit is zero, all other bits except the one for the
673 * current window frame are also zero. So, we can use a simple test:
674 * 'and' WINDOWSTART and WINDOWSTART-1:
676 * (XXXXXX1[0]* - 1) AND XXXXXX1[0]* = XXXXXX0[0]*
678 * The result is zero only if one bit was set.
680 * (Note: We might have gone through several task switches before
681 * we come back to the current task, so WINDOWBASE might be
682 * different from the time the exception occurred.)
685 /* Test WINDOWSTART before and after the exception.
686 * We actually have WMASK, so we only have to test if it is 1 or not.
689 l32i a2, a1, PT_WMASK
690 _beqi a2, 1, common_exception_exit # Spilled before exception,jump
692 /* Test WINDOWSTART now. If spilled, do the movsp */
697 _bnez a3, common_exception_exit
699 /* Do a movsp (we returned from a call4, so we have at least a0..a7) */
704 s32i a3, a1, PT_KERNEL_SIZE + 0
705 s32i a4, a1, PT_KERNEL_SIZE + 4
708 s32i a3, a1, PT_KERNEL_SIZE + 8
709 s32i a4, a1, PT_KERNEL_SIZE + 12
711 /* Common exception exit.
712 * We restore the special register and the current window frame, and
713 * return from the exception.
715 * Note: We expect a2 to hold PT_WMASK
721 common_exception_exit:
723 /* Restore address registers. */
726 l32i a4, a1, PT_AREG4
727 l32i a5, a1, PT_AREG5
728 l32i a6, a1, PT_AREG6
729 l32i a7, a1, PT_AREG7
731 l32i a8, a1, PT_AREG8
732 l32i a9, a1, PT_AREG9
733 l32i a10, a1, PT_AREG10
734 l32i a11, a1, PT_AREG11
736 l32i a12, a1, PT_AREG12
737 l32i a13, a1, PT_AREG13
738 l32i a14, a1, PT_AREG14
739 l32i a15, a1, PT_AREG15
741 /* Restore PC, SAR */
743 1: l32i a2, a1, PT_PC
748 /* Restore LBEG, LEND, LCOUNT */
753 l32i a2, a1, PT_LCOUNT
758 /* We control single stepping through the ICOUNTLEVEL register. */
760 l32i a2, a1, PT_ICOUNTLEVEL
765 /* Check if it was double exception. */
768 l32i a3, a1, PT_AREG3
769 l32i a2, a1, PT_AREG2
770 _bgeui a0, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
772 /* Restore a0...a3 and return */
774 l32i a0, a1, PT_AREG0
775 l32i a1, a1, PT_AREG1
779 l32i a0, a1, PT_AREG0
780 l32i a1, a1, PT_AREG1
783 ENDPROC(kernel_exception)
786 * Debug exception handler.
788 * Currently, we don't support KGDB, so only user application can be debugged.
790 * When we get here, a0 is trashed and saved to excsave[debuglevel]
795 ENTRY(debug_exception)
797 rsr a0, SREG_EPS + XCHAL_DEBUGLEVEL
798 bbsi.l a0, PS_EXCM_BIT, 1f # exception mode
800 /* Set EPC1 and EXCCAUSE */
802 wsr a2, depc # save a2 temporarily
803 rsr a2, SREG_EPC + XCHAL_DEBUGLEVEL
806 movi a2, EXCCAUSE_MAPPED_DEBUG
809 /* Restore PS to the value before the debug exc but with PS.EXCM set.*/
811 movi a2, 1 << PS_EXCM_BIT
815 /* Switch to kernel/user stack, restore jump vector, and save a0 */
817 bbsi.l a2, PS_UM_BIT, 2f # jump if user mode
819 addi a2, a1, -16 - PT_KERNEL_SIZE # assume kernel stack
821 l32i a0, a3, DT_DEBUG_SAVE
822 s32i a1, a2, PT_AREG1
823 s32i a0, a2, PT_AREG0
825 s32i a0, a2, PT_DEPC # mark it as a regular exception
826 xsr a3, SREG_EXCSAVE + XCHAL_DEBUGLEVEL
828 s32i a3, a2, PT_AREG3
829 s32i a0, a2, PT_AREG2
832 /* Debug exception is handled as an exception, so interrupts will
833 * likely be enabled in the common exception handler. Disable
834 * preemption if we have HW breakpoints to preserve DEBUGCAUSE.DBNUM
837 #if defined(CONFIG_PREEMPT_COUNT) && defined(CONFIG_HAVE_HW_BREAKPOINT)
838 GET_THREAD_INFO(a2, a1)
839 l32i a3, a2, TI_PRE_COUNT
841 s32i a3, a2, TI_PRE_COUNT
845 bbsi.l a2, PS_UM_BIT, _user_exception
849 l32i a2, a2, EXC_TABLE_KSTK # load kernel stack pointer
852 #ifdef CONFIG_HAVE_HW_BREAKPOINT
853 /* Debug exception while in exception mode. This may happen when
854 * window overflow/underflow handler or fast exception handler hits
855 * data breakpoint, in which case save and disable all data
856 * breakpoints, single-step faulting instruction and restore data
860 bbci.l a0, PS_UM_BIT, 1b # jump if kernel mode
863 bbsi.l a0, DEBUGCAUSE_DBREAK_BIT, .Ldebug_save_dbreak
866 .rept XCHAL_NUM_DBREAK
867 l32i a0, a3, DT_DBREAKC_SAVE + _index * 4
868 wsr a0, SREG_DBREAKC + _index
869 .set _index, _index + 1
872 l32i a0, a3, DT_ICOUNT_LEVEL_SAVE
875 l32i a0, a3, DT_ICOUNT_SAVE
878 l32i a0, a3, DT_DEBUG_SAVE
879 xsr a3, SREG_EXCSAVE + XCHAL_DEBUGLEVEL
884 .rept XCHAL_NUM_DBREAK
886 xsr a0, SREG_DBREAKC + _index
887 s32i a0, a3, DT_DBREAKC_SAVE + _index * 4
888 .set _index, _index + 1
891 movi a0, XCHAL_EXCM_LEVEL + 1
893 s32i a0, a3, DT_ICOUNT_LEVEL_SAVE
897 s32i a0, a3, DT_ICOUNT_SAVE
899 l32i a0, a3, DT_DEBUG_SAVE
900 xsr a3, SREG_EXCSAVE + XCHAL_DEBUGLEVEL
903 /* Debug exception while in exception mode. Should not happen. */
907 ENDPROC(debug_exception)
910 * We get here in case of an unrecoverable exception.
911 * The only thing we can do is to be nice and print a panic message.
912 * We only produce a single stack frame for panic, so ???
917 * - a0 contains the caller address; original value saved in excsave1.
918 * - the original a0 contains a valid return address (backtrace) or 0.
919 * - a2 contains a valid stackpointer
923 * - If the stack pointer could be invalid, the caller has to setup a
924 * dummy stack pointer (e.g. the stack of the init_task)
926 * - If the return address could be invalid, the caller has to set it
927 * to 0, so the backtrace would stop.
932 .ascii "Unrecoverable error in exception handler\0"
936 ENTRY(unrecoverable_exception)
938 #if XCHAL_HAVE_WINDOWED
947 movi a1, KERNEL_PS_WOE_MASK | LOCKLEVEL
953 addi a1, a1, PT_REGS_OFFSET
955 movi abi_arg0, unrecoverable_text
960 ENDPROC(unrecoverable_exception)
962 /* -------------------------- FAST EXCEPTION HANDLERS ----------------------- */
967 #ifdef SUPPORT_WINDOWED
969 * Fast-handler for alloca exceptions
971 * The ALLOCA handler is entered when user code executes the MOVSP
972 * instruction and the caller's frame is not in the register file.
974 * This algorithm was taken from the Ross Morley's RTOS Porting Layer:
976 * /home/ross/rtos/porting/XtensaRTOS-PortingLayer-20090507/xtensa_vectors.S
978 * It leverages the existing window spill/fill routines and their support for
979 * double exceptions. The 'movsp' instruction will only cause an exception if
980 * the next window needs to be loaded. In fact this ALLOCA exception may be
981 * replaced at some point by changing the hardware to do a underflow exception
982 * of the proper size instead.
984 * This algorithm simply backs out the register changes started by the user
985 * exception handler, makes it appear that we have started a window underflow
986 * by rotating the window back and then setting the old window base (OWB) in
987 * the 'ps' register with the rolled back window base. The 'movsp' instruction
988 * will be re-executed and this time since the next window frames is in the
989 * active AR registers it won't cause an exception.
991 * If the WindowUnderflow code gets a TLB miss the page will get mapped
992 * the partial WindowUnderflow will be handled in the double exception
997 * a0: trashed, original value saved on stack (PT_AREG0)
999 * a2: new stack pointer, original in DEPC
1001 * depc: a2, original value saved on stack (PT_DEPC)
1002 * excsave_1: dispatch table
1004 * PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
1005 * < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
1012 extui a3, a2, PS_OWB_SHIFT, PS_OWB_WIDTH
1014 l32i a4, a6, PT_AREG0
1015 l32i a1, a6, PT_DEPC
1018 slli a3, a3, PS_OWB_SHIFT
1027 j _WindowUnderflow12
1028 8: j _WindowUnderflow8
1029 4: j _WindowUnderflow4
1030 ENDPROC(fast_alloca)
1033 #ifdef CONFIG_USER_ABI_CALL0_PROBE
1035 * fast illegal instruction handler.
1037 * This is used to fix up user PS.WOE on the exception caused
1038 * by the first opcode related to register window. If PS.WOE is
1039 * already set it goes directly to the common user exception handler.
1043 * a0: trashed, original value saved on stack (PT_AREG0)
1045 * a2: new stack pointer, original in DEPC
1047 * depc: a2, original value saved on stack (PT_DEPC)
1048 * excsave_1: dispatch table
1051 ENTRY(fast_illegal_instruction_user)
1054 bbsi.l a0, PS_WOE_BIT, 1f
1055 s32i a3, a2, PT_AREG3
1056 movi a3, PS_WOE_MASK
1059 l32i a3, a2, PT_AREG3
1060 l32i a0, a2, PT_AREG0
1064 call0 user_exception
1066 ENDPROC(fast_illegal_instruction_user)
1070 * fast system calls.
1072 * WARNING: The kernel doesn't save the entire user context before
1073 * handling a fast system call. These functions are small and short,
1074 * usually offering some functionality not available to user tasks.
1076 * BE CAREFUL TO PRESERVE THE USER'S CONTEXT.
1080 * a0: trashed, original value saved on stack (PT_AREG0)
1082 * a2: new stack pointer, original in DEPC
1084 * depc: a2, original value saved on stack (PT_DEPC)
1085 * excsave_1: dispatch table
1088 ENTRY(fast_syscall_user)
1096 l32i a0, a2, PT_DEPC
1097 bgeui a0, VALID_DOUBLE_EXCEPTION_ADDRESS, fast_syscall_unrecoverable
1099 rsr a0, depc # get syscall-nr
1100 _beqz a0, fast_syscall_spill_registers
1101 _beqi a0, __NR_xtensa, fast_syscall_xtensa
1103 call0 user_exception
1105 ENDPROC(fast_syscall_user)
1107 ENTRY(fast_syscall_unrecoverable)
1109 /* Restore all states. */
1111 l32i a0, a2, PT_AREG0 # restore a0
1112 xsr a2, depc # restore a2, depc
1115 call0 unrecoverable_exception
1117 ENDPROC(fast_syscall_unrecoverable)
1120 * sysxtensa syscall handler
1122 * int sysxtensa (SYS_XTENSA_ATOMIC_SET, ptr, val, unused);
1123 * int sysxtensa (SYS_XTENSA_ATOMIC_ADD, ptr, val, unused);
1124 * int sysxtensa (SYS_XTENSA_ATOMIC_EXG_ADD, ptr, val, unused);
1125 * int sysxtensa (SYS_XTENSA_ATOMIC_CMP_SWP, ptr, oldval, newval);
1130 * a0: a2 (syscall-nr), original value saved on stack (PT_AREG0)
1132 * a2: new stack pointer, original in a0 and DEPC
1134 * a4..a15: unchanged
1135 * depc: a2, original value saved on stack (PT_DEPC)
1136 * excsave_1: dispatch table
1138 * PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
1139 * < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
1141 * Note: we don't have to save a2; a2 holds the return value
1146 #ifdef CONFIG_FAST_SYSCALL_XTENSA
1148 ENTRY(fast_syscall_xtensa)
1150 s32i a7, a2, PT_AREG7 # we need an additional register
1151 movi a7, 4 # sizeof(unsigned int)
1152 access_ok a3, a7, a0, a2, .Leac # a0: scratch reg, a2: sp
1154 _bgeui a6, SYS_XTENSA_COUNT, .Lill
1155 _bnei a6, SYS_XTENSA_ATOMIC_CMP_SWP, .Lnswp
1157 /* Fall through for ATOMIC_CMP_SWP. */
1159 .Lswp: /* Atomic compare and swap */
1161 EX(.Leac) l32i a0, a3, 0 # read old value
1162 bne a0, a4, 1f # same as old value? jump
1163 EX(.Leac) s32i a5, a3, 0 # different, modify value
1164 l32i a7, a2, PT_AREG7 # restore a7
1165 l32i a0, a2, PT_AREG0 # restore a0
1166 movi a2, 1 # and return 1
1169 1: l32i a7, a2, PT_AREG7 # restore a7
1170 l32i a0, a2, PT_AREG0 # restore a0
1171 movi a2, 0 # return 0 (note that we cannot set
1174 .Lnswp: /* Atomic set, add, and exg_add. */
1176 EX(.Leac) l32i a7, a3, 0 # orig
1177 addi a6, a6, -SYS_XTENSA_ATOMIC_SET
1178 add a0, a4, a7 # + arg
1179 moveqz a0, a4, a6 # set
1180 addi a6, a6, SYS_XTENSA_ATOMIC_SET
1181 EX(.Leac) s32i a0, a3, 0 # write new value
1185 l32i a7, a0, PT_AREG7 # restore a7
1186 l32i a0, a0, PT_AREG0 # restore a0
1189 .Leac: l32i a7, a2, PT_AREG7 # restore a7
1190 l32i a0, a2, PT_AREG0 # restore a0
1194 .Lill: l32i a7, a2, PT_AREG7 # restore a7
1195 l32i a0, a2, PT_AREG0 # restore a0
1199 ENDPROC(fast_syscall_xtensa)
1201 #else /* CONFIG_FAST_SYSCALL_XTENSA */
1203 ENTRY(fast_syscall_xtensa)
1205 l32i a0, a2, PT_AREG0 # restore a0
1209 ENDPROC(fast_syscall_xtensa)
1211 #endif /* CONFIG_FAST_SYSCALL_XTENSA */
1214 /* fast_syscall_spill_registers.
1218 * a0: trashed, original value saved on stack (PT_AREG0)
1220 * a2: new stack pointer, original in DEPC
1222 * depc: a2, original value saved on stack (PT_DEPC)
1223 * excsave_1: dispatch table
1225 * Note: We assume the stack pointer is EXC_TABLE_KSTK in the fixup handler.
1228 #if defined(CONFIG_FAST_SYSCALL_SPILL_REGISTERS) && \
1229 defined(USER_SUPPORT_WINDOWED)
1231 ENTRY(fast_syscall_spill_registers)
1233 /* Register a FIXUP handler (pass current wb as a parameter) */
1236 movi a0, fast_syscall_spill_registers_fixup
1237 s32i a0, a3, EXC_TABLE_FIXUP
1239 s32i a0, a3, EXC_TABLE_PARAM
1240 xsr a3, excsave1 # restore a3 and excsave_1
1242 /* Save a3, a4 and SAR on stack. */
1245 s32i a3, a2, PT_AREG3
1248 /* The spill routine might clobber a4, a7, a8, a11, a12, and a15. */
1250 s32i a4, a2, PT_AREG4
1251 s32i a7, a2, PT_AREG7
1252 s32i a8, a2, PT_AREG8
1253 s32i a11, a2, PT_AREG11
1254 s32i a12, a2, PT_AREG12
1255 s32i a15, a2, PT_AREG15
1258 * Rotate ws so that the current windowbase is at bit 0.
1259 * Assume ws = xxxwww1yy (www1 current window frame).
1260 * Rotate ws right so that a4 = yyxxxwww1.
1264 rsr a3, windowstart # a3 = xxxwww1yy
1267 or a3, a3, a0 # a3 = xxxwww1yyxxxwww1yy
1268 srl a3, a3 # a3 = 00xxxwww1yyxxxwww1
1270 /* We are done if there are no more than the current register frame. */
1272 extui a3, a3, 1, WSBITS-1 # a3 = 0yyxxxwww
1273 movi a0, (1 << (WSBITS-1))
1274 _beqz a3, .Lnospill # only one active frame? jump
1276 /* We want 1 at the top, so that we return to the current windowbase */
1278 or a3, a3, a0 # 1yyxxxwww
1280 /* Skip empty frames - get 'oldest' WINDOWSTART-bit. */
1282 wsr a3, windowstart # save shifted windowstart
1284 and a3, a0, a3 # first bit set from right: 000010000
1286 ffs_ws a0, a3 # a0: shifts to skip empty frames
1288 sub a0, a3, a0 # WSBITS-a0:number of 0-bits from right
1289 ssr a0 # save in SAR for later.
1297 srl a3, a3 # shift windowstart
1299 /* WB is now just one frame below the oldest frame in the register
1300 window. WS is shifted so the oldest frame is in bit 0, thus, WB
1301 and WS differ by one 4-register frame. */
1303 /* Save frames. Depending what call was used (call4, call8, call12),
1304 * we have to save 4,8. or 12 registers.
1308 .Lloop: _bbsi.l a3, 1, .Lc4
1309 _bbci.l a3, 2, .Lc12
1311 .Lc8: s32e a4, a13, -16
1320 srli a11, a3, 2 # shift windowbase by 2
1325 .Lc4: s32e a4, a9, -16
1335 .Lc12: _bbci.l a3, 3, .Linvalid_mask # bit 2 shouldn't be zero!
1337 /* 12-register frame (call12) */
1352 /* The stack pointer for a4..a7 is out of reach, so we rotate the
1353 * window, grab the stackpointer, and rotate back.
1354 * Alternatively, we could also use the following approach, but that
1355 * makes the fixup routine much more complicated:
1378 /* Done. Do the final rotation and set WS */
1388 /* Advance PC, restore registers and SAR, and return from exception. */
1391 l32i a0, a2, PT_AREG0
1393 l32i a3, a2, PT_AREG3
1395 /* Restore clobbered registers. */
1397 l32i a4, a2, PT_AREG4
1398 l32i a7, a2, PT_AREG7
1399 l32i a8, a2, PT_AREG8
1400 l32i a11, a2, PT_AREG11
1401 l32i a12, a2, PT_AREG12
1402 l32i a15, a2, PT_AREG15
1409 /* We get here because of an unrecoverable error in the window
1410 * registers, so set up a dummy frame and kill the user application.
1411 * Note: We assume EXC_TABLE_KSTK contains a valid stack pointer.
1424 l32i a1, a3, EXC_TABLE_KSTK
1426 movi a4, KERNEL_PS_WOE_MASK | LOCKLEVEL
1430 movi abi_arg0, SIGSEGV
1431 abi_call make_task_dead
1433 /* shouldn't return, so panic */
1436 call0 unrecoverable_exception # should not return
1440 ENDPROC(fast_syscall_spill_registers)
1444 * We get here if the spill routine causes an exception, e.g. tlb miss.
1445 * We basically restore WINDOWBASE and WINDOWSTART to the condition when
1446 * we entered the spill routine and jump to the user exception handler.
1448 * Note that we only need to restore the bits in windowstart that have not
1449 * been spilled yet by the _spill_register routine. Luckily, a3 contains a
1450 * rotated windowstart with only those bits set for frames that haven't been
1451 * spilled yet. Because a3 is rotated such that bit 0 represents the register
1452 * frame for the current windowbase - 1, we need to rotate a3 left by the
1453 * value of the current windowbase + 1 and move it to windowstart.
1455 * a0: value of depc, original value in depc
1456 * a2: trashed, original value in EXC_TABLE_DOUBLE_SAVE
1457 * a3: exctable, original value in excsave1
1460 ENTRY(fast_syscall_spill_registers_fixup)
1462 rsr a2, windowbase # get current windowbase (a2 is saved)
1463 xsr a0, depc # restore depc and a0
1464 ssl a2 # set shift (32 - WB)
1466 /* We need to make sure the current registers (a0-a3) are preserved.
1467 * To do this, we simply set the bit for the current window frame
1468 * in WS, so that the exception handlers save them to the task stack.
1470 * Note: we use a3 to set the windowbase, so we take a special care
1471 * of it, saving it in the original _spill_registers frame across
1472 * the exception handler call.
1475 xsr a3, excsave1 # get spill-mask
1476 slli a3, a3, 1 # shift left by one
1477 addi a3, a3, 1 # set the bit for the current window frame
1479 slli a2, a3, 32-WSBITS
1480 src a2, a3, a2 # a2 = xxwww1yyxxxwww1yy......
1481 wsr a2, windowstart # set corrected windowstart
1485 l32i a2, a2, EXC_TABLE_DOUBLE_SAVE # restore a2
1487 s32i a3, a2, EXC_TABLE_DOUBLE_SAVE # save a3
1488 l32i a3, a2, EXC_TABLE_PARAM # original WB (in user task)
1491 /* Return to the original (user task) WINDOWBASE.
1492 * We leave the following frame behind:
1494 * a3: trashed (saved in EXC_TABLE_DOUBLE_SAVE)
1495 * depc: depc (we have to return to that address)
1496 * excsave_1: exctable
1502 /* We are now in the original frame when we entered _spill_registers:
1503 * a0: return address
1504 * a1: used, stack pointer
1505 * a2: kernel stack pointer
1507 * depc: exception address
1509 * Note: This frame might be the same as above.
1512 /* Setup stack pointer. */
1514 addi a2, a2, -PT_USER_SIZE
1515 s32i a0, a2, PT_AREG0
1517 /* Make sure we return to this fixup handler. */
1519 movi a3, fast_syscall_spill_registers_fixup_return
1520 s32i a3, a2, PT_DEPC # setup depc
1522 /* Jump to the exception handler. */
1526 addx4 a0, a0, a3 # find entry in table
1527 l32i a0, a0, EXC_TABLE_FAST_USER # load handler
1528 l32i a3, a3, EXC_TABLE_DOUBLE_SAVE
1531 ENDPROC(fast_syscall_spill_registers_fixup)
1533 ENTRY(fast_syscall_spill_registers_fixup_return)
1535 /* When we return here, all registers have been restored (a2: DEPC) */
1537 wsr a2, depc # exception address
1539 /* Restore fixup handler. */
1542 s32i a3, a2, EXC_TABLE_DOUBLE_SAVE
1543 movi a3, fast_syscall_spill_registers_fixup
1544 s32i a3, a2, EXC_TABLE_FIXUP
1546 s32i a3, a2, EXC_TABLE_PARAM
1547 l32i a2, a2, EXC_TABLE_KSTK
1549 /* Load WB at the time the exception occurred. */
1551 rsr a3, sar # WB is still in SAR
1557 l32i a3, a3, EXC_TABLE_DOUBLE_SAVE
1561 ENDPROC(fast_syscall_spill_registers_fixup_return)
1563 #else /* CONFIG_FAST_SYSCALL_SPILL_REGISTERS */
1565 ENTRY(fast_syscall_spill_registers)
1567 l32i a0, a2, PT_AREG0 # restore a0
1571 ENDPROC(fast_syscall_spill_registers)
1573 #endif /* CONFIG_FAST_SYSCALL_SPILL_REGISTERS */
1577 * We should never get here. Bail out!
1580 ENTRY(fast_second_level_miss_double_kernel)
1583 call0 unrecoverable_exception # should not return
1586 ENDPROC(fast_second_level_miss_double_kernel)
1588 /* First-level entry handler for user, kernel, and double 2nd-level
1589 * TLB miss exceptions. Note that for now, user and kernel miss
1590 * exceptions share the same entry point and are handled identically.
1592 * An old, less-efficient C version of this function used to exist.
1593 * We include it below, interleaved as comments, for reference.
1597 * a0: trashed, original value saved on stack (PT_AREG0)
1599 * a2: new stack pointer, original in DEPC
1601 * depc: a2, original value saved on stack (PT_DEPC)
1602 * excsave_1: dispatch table
1604 * PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
1605 * < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
1608 ENTRY(fast_second_level_miss)
1610 /* Save a1 and a3. Note: we don't expect a double exception. */
1612 s32i a1, a2, PT_AREG1
1613 s32i a3, a2, PT_AREG3
1615 /* We need to map the page of PTEs for the user task. Find
1616 * the pointer to that page. Also, it's possible for tsk->mm
1617 * to be NULL while tsk->active_mm is nonzero if we faulted on
1618 * a vmalloc address. In that rare case, we must use
1619 * active_mm instead to avoid a fault in this handler. See
1621 * http://mail.nl.linux.org/linux-mm/2002-08/msg00258.html
1622 * (or search Internet on "mm vs. active_mm")
1625 * mm = tsk->active_mm;
1626 * pgd = pgd_offset (mm, regs->excvaddr);
1627 * pmd = pmd_offset (pgd, regs->excvaddr);
1632 l32i a0, a1, TASK_MM # tsk->mm
1635 8: rsr a3, excvaddr # fault address
1636 _PGD_OFFSET(a0, a3, a1)
1637 l32i a0, a0, 0 # read pmdval
1640 /* Read ptevaddr and convert to top of page-table page.
1642 * vpnval = read_ptevaddr_register() & PAGE_MASK;
1643 * vpnval += DTLB_WAY_PGTABLE;
1644 * pteval = mk_pte (virt_to_page(pmd_val(pmdval)), PAGE_KERNEL);
1645 * write_dtlb_entry (pteval, vpnval);
1647 * The messy computation for 'pteval' above really simplifies
1648 * into the following:
1650 * pteval = ((pmdval - PAGE_OFFSET + PHYS_OFFSET) & PAGE_MASK)
1654 movi a1, (PHYS_OFFSET - PAGE_OFFSET) & 0xffffffff
1655 add a0, a0, a1 # pmdval - PAGE_OFFSET
1656 extui a1, a0, 0, PAGE_SHIFT # ... & PAGE_MASK
1659 movi a1, _PAGE_DIRECTORY
1660 or a0, a0, a1 # ... | PAGE_DIRECTORY
1663 * We utilize all three wired-ways (7-9) to hold pmd translations.
1664 * Memory regions are mapped to the DTLBs according to bits 28 and 29.
1665 * This allows to map the three most common regions to three different
1667 * 0,1 -> way 7 program (0040.0000) and virtual (c000.0000)
1668 * 2 -> way 8 shared libaries (2000.0000)
1669 * 3 -> way 0 stack (3000.0000)
1672 extui a3, a3, 28, 2 # addr. bit 28 and 29 0,1,2,3
1674 addx2 a3, a3, a3 # -> 0,3,6,9
1675 srli a1, a1, PAGE_SHIFT
1676 extui a3, a3, 2, 2 # -> 0,0,1,2
1677 slli a1, a1, PAGE_SHIFT # ptevaddr & PAGE_MASK
1678 addi a3, a3, DTLB_WAY_PGD
1679 add a1, a1, a3 # ... + way_number
1684 /* Exit critical section. */
1688 s32i a0, a3, EXC_TABLE_FIXUP
1690 /* Restore the working registers, and return. */
1692 l32i a0, a2, PT_AREG0
1693 l32i a1, a2, PT_AREG1
1694 l32i a3, a2, PT_AREG3
1695 l32i a2, a2, PT_DEPC
1697 bgeui a2, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
1699 /* Restore excsave1 and return. */
1704 /* Return from double exception. */
1710 9: l32i a0, a1, TASK_ACTIVE_MM # unlikely case mm == 0
1713 /* Even more unlikely case active_mm == 0.
1714 * We can get here with NMI in the middle of context_switch that
1715 * touches vmalloc area.
1720 #if (DCACHE_WAY_SIZE > PAGE_SIZE)
1722 2: /* Special case for cache aliasing.
1723 * We (should) only get here if a clear_user_page, copy_user_page
1724 * or the aliased cache flush functions got preemptively interrupted
1725 * by another task. Re-establish temporary mapping to the
1726 * TLBTEMP_BASE areas.
1729 /* We shouldn't be in a double exception */
1731 l32i a0, a2, PT_DEPC
1732 bgeui a0, VALID_DOUBLE_EXCEPTION_ADDRESS, 2f
1734 /* Make sure the exception originated in the special functions */
1736 movi a0, __tlbtemp_mapping_start
1739 movi a0, __tlbtemp_mapping_end
1742 /* Check if excvaddr was in one of the TLBTEMP_BASE areas. */
1744 movi a3, TLBTEMP_BASE_1
1748 addi a1, a0, -TLBTEMP_SIZE
1751 /* Check if we have to restore an ITLB mapping. */
1753 movi a1, __tlbtemp_mapping_itlb
1762 /* Jump for ITLB entry */
1766 /* We can use up to two TLBTEMP areas, one for src and one for dst. */
1768 extui a3, a0, PAGE_SHIFT + DCACHE_ALIAS_ORDER, 1
1771 /* PPN is in a6 for the first TLBTEMP area and in a7 for the second. */
1777 /* ITLB entry. We only use dst in a6. */
1784 #endif // DCACHE_WAY_SIZE > PAGE_SIZE
1787 2: /* Invalid PGD, default exception handling */
1790 s32i a1, a2, PT_AREG2
1794 bbsi.l a2, PS_UM_BIT, 1f
1795 call0 _kernel_exception
1796 1: call0 _user_exception
1798 ENDPROC(fast_second_level_miss)
1801 * StoreProhibitedException
1803 * Update the pte and invalidate the itlb mapping for this pte.
1807 * a0: trashed, original value saved on stack (PT_AREG0)
1809 * a2: new stack pointer, original in DEPC
1811 * depc: a2, original value saved on stack (PT_DEPC)
1812 * excsave_1: dispatch table
1814 * PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception, DEPC
1815 * < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
1818 ENTRY(fast_store_prohibited)
1820 /* Save a1 and a3. */
1822 s32i a1, a2, PT_AREG1
1823 s32i a3, a2, PT_AREG3
1826 l32i a0, a1, TASK_MM # tsk->mm
1829 8: rsr a1, excvaddr # fault address
1830 _PGD_OFFSET(a0, a1, a3)
1835 * Note that we test _PAGE_WRITABLE_BIT only if PTE is present
1836 * and is not PAGE_NONE. See pgtable.h for possible PTE layouts.
1839 _PTE_OFFSET(a0, a1, a3)
1840 l32i a3, a0, 0 # read pteval
1841 movi a1, _PAGE_CA_INVALID
1843 bbci.l a3, _PAGE_WRITABLE_BIT, 2f
1845 movi a1, _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_HW_WRITE
1850 /* We need to flush the cache if we have page coloring. */
1851 #if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
1857 /* Exit critical section. */
1861 s32i a0, a3, EXC_TABLE_FIXUP
1863 /* Restore the working registers, and return. */
1865 l32i a3, a2, PT_AREG3
1866 l32i a1, a2, PT_AREG1
1867 l32i a0, a2, PT_AREG0
1868 l32i a2, a2, PT_DEPC
1870 bgeui a2, VALID_DOUBLE_EXCEPTION_ADDRESS, 1f
1875 /* Double exception. Restore FIXUP handler and return. */
1881 9: l32i a0, a1, TASK_ACTIVE_MM # unlikely case mm == 0
1884 2: /* If there was a problem, handle fault in C */
1886 rsr a3, depc # still holds a2
1887 s32i a3, a2, PT_AREG2
1891 bbsi.l a2, PS_UM_BIT, 1f
1892 call0 _kernel_exception
1893 1: call0 _user_exception
1895 ENDPROC(fast_store_prohibited)
1897 #endif /* CONFIG_MMU */
1903 * void system_call (struct pt_regs* regs, int exccause)
1910 #if defined(__XTENSA_WINDOWED_ABI__)
1912 #elif defined(__XTENSA_CALL0_ABI__)
1916 s32i abi_saved0, sp, 4
1917 s32i abi_saved1, sp, 8
1920 #error Unsupported Xtensa ABI
1923 /* regs->syscall = regs->areg[2] */
1925 l32i a7, abi_saved0, PT_AREG2
1926 s32i a7, abi_saved0, PT_SYSCALL
1928 GET_THREAD_INFO(a4, a1)
1929 l32i abi_saved1, a4, TI_FLAGS
1930 movi a4, _TIF_WORK_MASK
1931 and abi_saved1, abi_saved1, a4
1934 mov abi_arg0, abi_saved0
1935 abi_call do_syscall_trace_enter
1936 beqz abi_rv, .Lsyscall_exit
1937 l32i a7, abi_saved0, PT_SYSCALL
1940 /* syscall = sys_call_table[syscall_nr] */
1942 movi a4, sys_call_table
1943 movi a5, __NR_syscalls
1944 movi abi_rv, -ENOSYS
1948 l32i abi_tmp0, a4, 0
1950 /* Load args: arg0 - arg5 are passed via regs. */
1952 l32i abi_arg0, abi_saved0, PT_AREG6
1953 l32i abi_arg1, abi_saved0, PT_AREG3
1954 l32i abi_arg2, abi_saved0, PT_AREG4
1955 l32i abi_arg3, abi_saved0, PT_AREG5
1956 l32i abi_arg4, abi_saved0, PT_AREG8
1957 l32i abi_arg5, abi_saved0, PT_AREG9
1961 1: /* regs->areg[2] = return_value */
1963 s32i abi_rv, abi_saved0, PT_AREG2
1966 #if defined(__XTENSA_WINDOWED_ABI__)
1968 #elif defined(__XTENSA_CALL0_ABI__)
1970 l32i abi_saved0, sp, 4
1971 l32i abi_saved1, sp, 8
1974 #error Unsupported Xtensa ABI
1978 mov abi_arg0, abi_saved0
1979 abi_call do_syscall_trace_leave
1982 ENDPROC(system_call)
1985 * Spill live registers on the kernel stack macro.
1987 * Entry condition: ps.woe is set, ps.excm is cleared
1988 * Exit condition: windowstart has single bit set
1989 * May clobber: a12, a13
1991 .macro spill_registers_kernel
1993 #if XCHAL_NUM_AREGS > 16
2001 #if XCHAL_NUM_AREGS > 32
2002 .rept (XCHAL_NUM_AREGS - 32) / 12
2008 #if XCHAL_NUM_AREGS % 12 == 0
2010 #elif XCHAL_NUM_AREGS % 12 == 4
2012 #elif XCHAL_NUM_AREGS % 12 == 8
2025 * struct task* _switch_to (struct task* prev, struct task* next)
2031 #if defined(__XTENSA_WINDOWED_ABI__)
2032 abi_entry(XTENSA_SPILL_STACK_RESERVE)
2033 #elif defined(__XTENSA_CALL0_ABI__)
2041 #error Unsupported Xtensa ABI
2043 mov a11, a3 # and 'next' (a3)
2045 l32i a4, a2, TASK_THREAD_INFO
2046 l32i a5, a3, TASK_THREAD_INFO
2048 save_xtregs_user a4 a6 a8 a9 a12 a13 THREAD_XTREGS_USER
2050 #if THREAD_RA > 1020 || THREAD_SP > 1020
2051 addi a10, a2, TASK_THREAD
2052 s32i a0, a10, THREAD_RA - TASK_THREAD # save return address
2053 s32i a1, a10, THREAD_SP - TASK_THREAD # save stack pointer
2055 s32i a0, a2, THREAD_RA # save return address
2056 s32i a1, a2, THREAD_SP # save stack pointer
2059 #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP)
2060 movi a6, __stack_chk_guard
2061 l32i a8, a3, TASK_STACK_CANARY
2065 /* Disable ints while we manipulate the stack pointer. */
2070 /* Switch CPENABLE */
2072 #if (XTENSA_HAVE_COPROCESSORS || XTENSA_HAVE_IO_PORTS)
2073 l32i a3, a5, THREAD_CPENABLE
2075 s32i a3, a4, THREAD_CPENABLE
2078 #if XCHAL_HAVE_EXCLUSIVE
2079 l32i a3, a5, THREAD_ATOMCTL8
2081 s32i a3, a4, THREAD_ATOMCTL8
2084 /* Flush register file. */
2086 #if defined(__XTENSA_WINDOWED_ABI__)
2087 spill_registers_kernel
2090 /* Set kernel stack (and leave critical section)
2091 * Note: It's save to set it here. The stack will not be overwritten
2092 * because the kernel stack will only be loaded again after
2093 * we return from kernel space.
2096 rsr a3, excsave1 # exc_table
2097 addi a7, a5, PT_REGS_OFFSET
2098 s32i a7, a3, EXC_TABLE_KSTK
2100 /* restore context of the task 'next' */
2102 l32i a0, a11, THREAD_RA # restore return address
2103 l32i a1, a11, THREAD_SP # restore stack pointer
2105 load_xtregs_user a5 a6 a8 a9 a12 a13 THREAD_XTREGS_USER
2110 #if defined(__XTENSA_WINDOWED_ABI__)
2111 abi_ret(XTENSA_SPILL_STACK_RESERVE)
2112 #elif defined(__XTENSA_CALL0_ABI__)
2119 #error Unsupported Xtensa ABI
2124 ENTRY(ret_from_fork)
2126 /* void schedule_tail (struct task_struct *prev)
2127 * Note: prev is still in abi_arg0 (return value from fake call frame)
2129 abi_call schedule_tail
2132 abi_call do_syscall_trace_leave
2133 j common_exception_return
2135 ENDPROC(ret_from_fork)
2138 * Kernel thread creation helper
2139 * On entry, set up by copy_thread: abi_saved0 = thread_fn,
2140 * abi_saved1 = thread_fn arg. Left from _switch_to: abi_arg0 = prev
2142 ENTRY(ret_from_kernel_thread)
2144 abi_call schedule_tail
2145 mov abi_arg0, abi_saved1
2146 abi_callx abi_saved0
2147 j common_exception_return
2149 ENDPROC(ret_from_kernel_thread)