1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * linux/arch/unicore32/kernel/entry.S
5 * Code specific to PKUnity SoC and UniCore ISA
7 * Copyright (C) 2001-2010 GUAN Xue-tao
9 * Low-level vector interface routines
11 #include <linux/init.h>
12 #include <linux/linkage.h>
13 #include <asm/assembler.h>
14 #include <asm/errno.h>
15 #include <asm/thread_info.h>
16 #include <asm/memory.h>
17 #include <asm/unistd.h>
18 #include <generated/asm-offsets.h>
19 #include "debug-macro.S"
22 @ Most of the stack format comes from struct pt_regs, but with
23 @ the addition of 8 bytes for storing syscall args 5 and 6.
28 * The SWI code relies on the fact that R0 is at the bottom of the stack
29 * (due to slow/fast restore user regs).
36 #ifdef CONFIG_FRAME_POINTER
41 .macro alignment_trap, rtemp
42 #ifdef CONFIG_ALIGNMENT_TRAP
43 ldw \rtemp, .LCcralign
45 movc p0.c1, \rtemp, #0
49 .macro load_user_sp_lr, rd, rtemp, offset = 0
51 xor \rtemp, \rtemp, #(PRIV_MODE ^ SUSR_MODE)
52 mov.a asr, \rtemp @ switch to the SUSR mode
54 ldw sp, [\rd+], #\offset @ load sp_user
55 ldw lr, [\rd+], #\offset + 4 @ load lr_user
57 xor \rtemp, \rtemp, #(PRIV_MODE ^ SUSR_MODE)
58 mov.a asr, \rtemp @ switch back to the PRIV mode
61 .macro priv_exit, rpsr
63 ldm.w (r0 - r15), [sp]+
64 ldm.b (r16 - pc), [sp]+ @ load r0 - pc, asr
67 .macro restore_user_regs, fast = 0, offset = 0
68 ldw r1, [sp+], #\offset + S_PSR @ get calling asr
69 ldw lr, [sp+], #\offset + S_PC @ get pc
70 mov.a bsr, r1 @ save in bsr_priv
72 add sp, sp, #\offset + S_R1 @ r0 is syscall return value
73 ldm.w (r1 - r15), [sp]+ @ get calling r1 - r15
74 ldur (r16 - lr), [sp]+ @ get calling r16 - lr
76 ldm.w (r0 - r15), [sp]+ @ get calling r0 - r15
77 ldur (r16 - lr), [sp]+ @ get calling r16 - lr
80 add sp, sp, #S_FRAME_SIZE - S_R16
82 @ and move bsr_priv into asr
85 .macro get_thread_info, rd
90 .macro get_irqnr_and_base, irqnr, irqstat, base, tmp
91 ldw \base, =(PKUNITY_INTC_BASE)
92 ldw \irqstat, [\base+], #0xC @ INTC_ICIP
93 ldw \tmp, [\base+], #0x4 @ INTC_ICMR
94 and.a \irqstat, \irqstat, \tmp
96 cntlz \irqnr, \irqstat
97 rsub \irqnr, \irqnr, #31
98 1001: /* EQ will be set if no irqs pending */
101 #ifdef CONFIG_DEBUG_LL
102 .macro printreg, reg, temp
104 stm (r0-r3), [\temp]+
105 stw lr, [\temp+], #0x10
115 ldm (r0-r3), [\temp]+
116 ldw lr, [\temp+], #0x10
118 901: .word 0, 0, 0, 0, 0 @ r0-r3, lr
119 902: .asciz ": epip4d\n"
126 * These are the registers used in the syscall handler, and allow us to
127 * have in theory up to 7 arguments to a function - r0 to r6.
129 * Note that tbl == why is intentional.
131 * We must set at least "tsk" and "why" when calling ret_with_reschedule.
133 scno .req r21 @ syscall number
134 tbl .req r22 @ syscall table pointer
135 why .req r22 @ Linux syscall (!= 0)
136 tsk .req r23 @ current thread_info
139 * Interrupt handling. Preserves r17, r18, r19
142 1: get_irqnr_and_base r0, r6, r5, lr
146 @ routine called with r0 = irq number, r1 = struct pt_regs *
157 sub sp, sp, #(S_FRAME_SIZE - 4)
158 stm (r1 - r15), [sp]+
160 stm (r16 - r28), [r5]+
163 add r5, sp, #S_SP - 4 @ here for interlock avoidance
164 mov r4, #-1 @ "" "" "" ""
165 add r0, sp, #(S_FRAME_SIZE - 4)
166 stw.w r1, [sp+], #-4 @ save the "real" r0 copied
167 @ from the exception stack
172 @ We are now ready to fill in the remaining blanks on the stack:
176 @ r2 - lr_<exception>, already fixed up for correct return/restart
177 @ r3 - bsr_<exception>
178 @ r4 - orig_r0 (see pt_regs definition in ptrace.h)
188 sub sp, sp, #S_FRAME_SIZE
189 stm (r1 - r15), [sp+]
191 stm (r16 - r28), [r4]+
194 add r0, sp, #S_PC @ here for interlock avoidance
195 mov r4, #-1 @ "" "" "" ""
197 stw r1, [sp] @ save the "real" r0 copied
198 @ from the exception stack
201 @ We are now ready to fill in the remaining blanks on the stack:
203 @ r2 - lr_<exception>, already fixed up for correct return/restart
204 @ r3 - bsr_<exception>
205 @ r4 - orig_r0 (see pt_regs definition in ptrace.h)
207 @ Also, separately save sp_user and lr_user
213 @ Enable the alignment trap while in kernel mode
218 @ Clear FP to mark the first stack frame
226 @ __invalid - generic code for failed exception
227 @ (re-entrant version of handlers)
230 sub sp, sp, #S_FRAME_SIZE
231 stm (r1 - r15), [sp+]
233 stm (r16 - r28, sp, lr), [r1]+
238 add r0, sp, #S_PC @ here for interlock avoidance
239 mov r7, #-1 @ "" "" "" ""
240 stw r4, [sp] @ save preserved r0
241 stm (r5 - r7), [r0]+ @ lr_<exception>,
242 @ asr_<exception>, "old_r0"
254 @ get ready to re-enable interrupts if appropriate
257 cand.a r3, #PSR_I_BIT
259 andn r17, r17, #PSR_I_BIT
263 @ Call the processor-specific abort handler:
265 @ r2 - aborted context pc
266 @ r3 - aborted context asr
268 @ The abort handler must return the aborted address in r0, and
269 @ the fault status register in r1.
271 movc r1, p0.c3, #0 @ get FSR
272 movc r0, p0.c4, #0 @ get FAR
275 @ set desired INTR state, then call main handler
282 @ INTRs off again before pulling preserved data off the stack
287 @ restore BSR and restart the instruction
289 ldw r2, [sp+], #S_PSR
290 priv_exit r2 @ return from exception
301 nop; nop; nop; nop; nop; nop; nop; nop
303 ldw r4, [sp+], #S_PSR @ irqs are already disabled
305 priv_exit r4 @ return from exception
314 mov r0, sp @ struct pt_regs *regs
316 b bad_mode @ not supported
324 @ re-enable interrupts if appropriate
327 cand.a r3, #PSR_I_BIT
329 andn r17, r17, #PSR_I_BIT
333 @ set args, then call main handler
335 @ r0 - address of faulting instruction
336 @ r1 - pointer to registers on stack
338 mov r0, r2 @ pass address of aborted instruction
342 b.l do_PrefetchAbort @ call abort handler
345 @ INTRs off again before pulling preserved data off the stack
350 @ restore BSR and restart the instruction
352 ldw r2, [sp+], #S_PSR
353 priv_exit r2 @ return from exception
364 #ifdef CONFIG_UNICORE_FPU_F64
366 cand.a ip, #0x08000000 @ FPU execption traps?
373 @ fall through to the emulation code, which returns using r19 if
374 @ it has emulated the instruction, or the more conventional lr
375 @ if we are to treat this as a real extended instruction
380 adr r19, ret_from_exception
383 @ fallthrough to call do_uc_f64
386 * Check whether the instruction is a co-processor instruction.
387 * If yes, we need to call the relevant co-processor handler.
389 * Note that we don't do a full check here for the co-processor
390 * instructions; all instructions with bit 27 set are well
391 * defined. The only instructions that should fault are the
392 * co-processor instructions.
394 * Emulators may wish to make use of the following registers:
395 * r0 = instruction opcode.
397 * r19 = normal "successful" return address
398 * r20 = this threads thread_info structure.
399 * lr = unrecognised instruction return address
401 get_thread_info r20 @ get current thread
402 and r8, r0, #0x00003c00 @ mask out CP number
404 stb r7, [r20+], #TI_USED_CP + 2 @ set appropriate used_cp[]
406 @ F64 hardware support entry point.
407 @ r0 = faulted instruction
408 @ r19 = return address
411 add r20, r20, #TI_FPSTATE @ r20 = workspace
412 cff r1, s31 @ get fpu FPSCR
413 andn r2, r1, #0x08000000
414 ctf r2, s31 @ clear 27 bit
415 mov r2, sp @ nothing stacked - regdump is at TOS
416 mov lr, r19 @ setup for a return to the user code
418 @ Now call the C code to package up the bounce to the support code
419 @ r0 holds the trigger instruction
420 @ r1 holds the FPSCR value
421 @ r2 pointer to register dump
426 @ Call the processor-specific abort handler:
428 @ r2 - aborted context pc
429 @ r3 - aborted context asr
431 @ The abort handler must return the aborted address in r0, and
432 @ the fault status register in r1.
434 movc r1, p0.c3, #0 @ get FSR
435 movc r0, p0.c4, #0 @ get FAR
438 @ INTRs on, then call the main handler
442 adr lr, ret_from_exception
473 mov r0, r2 @ pass address of aborted instruction.
475 enable_irq r1 @ Enable interrupts
477 b.l do_PrefetchAbort @ call abort handler
480 * This is the return code to user mode for abort handlers
482 ENTRY(ret_from_exception)
487 ENDPROC(ret_from_exception)
490 * Register switch for UniCore V2 processors
491 * r0 = previous task_struct, r1 = previous thread_info, r2 = next thread_info
492 * previous and next are guaranteed not to be the same.
495 add ip, r1, #TI_CPU_SAVE
496 stm.w (r4 - r15), [ip]+
497 stm.w (r16 - r27, sp, lr), [ip]+
499 #ifdef CONFIG_UNICORE_FPU_F64
500 add ip, r1, #TI_FPSTATE
501 sfm.w (f0 - f7 ), [ip]+
502 sfm.w (f8 - f15), [ip]+
503 sfm.w (f16 - f23), [ip]+
504 sfm.w (f24 - f31), [ip]+
508 add ip, r2, #TI_FPSTATE
509 lfm.w (f0 - f7 ), [ip]+
510 lfm.w (f8 - f15), [ip]+
511 lfm.w (f16 - f23), [ip]+
512 lfm.w (f24 - f31), [ip]+
516 add ip, r2, #TI_CPU_SAVE
517 ldm.w (r4 - r15), [ip]+
518 ldm (r16 - r27, sp, pc), [ip]+ @ Load all regs saved previously
523 * This is the fast syscall return path. We do as little as
524 * possible here, and this includes saving r0 back into the PRIV
528 disable_irq r1 @ disable interrupts
529 ldw r1, [tsk+], #TI_FLAGS
530 cand.a r1, #_TIF_WORK_MASK
531 bne fast_work_pending
533 @ fast_restore_user_regs
534 restore_user_regs fast = 1, offset = S_OFF
537 * Ok, we need to do extra processing, enter the slow path.
540 stw.w r0, [sp+], #S_R0+S_OFF @ returned r0
542 cand.a r1, #_TIF_NEED_RESCHED
545 mov r2, why @ 'syscall'
546 cand.a r1, #_TIF_SIGPENDING @ delivering a signal?
547 cmovne why, #0 @ prevent further restarts
549 b ret_slow_syscall @ Check work again
554 * "slow" syscall return path. "why" tells us if this was a real syscall.
558 disable_irq r1 @ disable interrupts
559 get_thread_info tsk @ epip4d, one path error?!
560 ldw r1, [tsk+], #TI_FLAGS
561 cand.a r1, #_TIF_WORK_MASK
564 @ slow_restore_user_regs
565 restore_user_regs fast = 0, offset = 0
569 * This is how we return from a fork.
574 ENDPROC(ret_from_fork)
576 ENTRY(ret_from_kernel_thread)
579 adr lr, ret_slow_syscall
581 ENDPROC(ret_from_kernel_thread)
583 /*=============================================================================
585 *-----------------------------------------------------------------------------
589 sub sp, sp, #S_FRAME_SIZE
590 stm (r0 - r15), [sp]+ @ Calling r0 - r15
592 stm (r16 - r28), [r8]+ @ Calling r16 - r28
594 stur (sp, lr), [r8-] @ Calling sp, lr
595 mov r8, bsr @ called from non-REAL mode
596 stw lr, [sp+], #S_PC @ Save calling PC
597 stw r8, [sp+], #S_PSR @ Save ASR
598 stw r0, [sp+], #S_OLD_R0 @ Save OLD_R0
602 * Get the system call number.
605 ldw.u scno, [ip] @ get SWI instruction
607 #ifdef CONFIG_ALIGNMENT_TRAP
608 ldw ip, __cr_alignment
610 movc p0.c1, ip, #0 @ update control register
615 ldw tbl, =sys_call_table @ load syscall table pointer
617 andn scno, scno, #0xff000000 @ mask off SWI op-code
618 andn scno, scno, #0x00ff0000 @ mask off SWI op-code
620 stm.w (r4, r5), [sp-] @ push fifth and sixth args
621 ldw ip, [tsk+], #TI_FLAGS @ check for syscall tracing
622 cand.a ip, #_TIF_SYSCALL_TRACE @ are we tracing syscalls?
625 csub.a scno, #__NR_syscalls @ check upper syscall limit
626 adr lr, ret_fast_syscall @ return address
628 ldw pc, [tbl+], scno << #2 @ call sys_* routine
631 2: mov why, #0 @ no longer a real syscall
632 b sys_ni_syscall @ not private func
635 * This is the really slow path. We're going to be doing
636 * context switches, and waiting for our parent to respond.
641 mov r0, #0 @ trace entry [IP = 0]
644 adr lr, __sys_trace_return @ return address
645 mov scno, r0 @ syscall number (possibly new)
646 add r1, sp, #S_R0 + S_OFF @ pointer to regs
647 csub.a scno, #__NR_syscalls @ check upper syscall limit
649 ldm (r0 - r3), [r1]+ @ have to reload r0 - r3
650 ldw pc, [tbl+], scno << #2 @ call sys_* routine
653 stw.w r0, [sp+], #S_R0 + S_OFF @ save returned r0
656 mov r0, #1 @ trace exit [IP = 1]
661 #ifdef CONFIG_ALIGNMENT_TRAP
662 .type __cr_alignment, #object
668 ENTRY(sys_rt_sigreturn)
670 mov why, #0 @ prevent syscall restart handling
672 ENDPROC(sys_rt_sigreturn)
679 * This code is copied to 0xffff0200 so we can use branches in the
680 * vectors, rather than ldr's. Note that this code must not
681 * exceed 0x300 bytes.
683 * Common stub entry macro:
684 * Enter in INTR mode, bsr = PRIV/USER ASR, lr = PRIV/USER PC
686 * SP points to a minimal amount of processor-private memory, the address
687 * of which is copied into r0 for the mode specific abort handler.
689 .macro vector_stub, name, mode
694 @ Save r0, lr_<exception> (parent PC) and bsr_<exception>
698 stw lr, [sp+], #4 @ save r0, lr
700 stw lr, [sp+], #8 @ save bsr
703 @ Prepare for PRIV mode. INTRs remain disabled.
706 xor r0, r0, #(\mode ^ PRIV_MODE)
710 @ the branch table must immediately follow this code
715 ldw lr, [pc+], lr << #2
716 mov.a pc, lr @ branch to handler in PRIV mode
717 ENDPROC(vector_\name)
719 @ handler addresses follow this label
725 * Interrupt dispatcher
727 vector_stub intr, INTR_MODE
729 .long __intr_user @ 0 (USER)
732 .long __intr_priv @ 3 (PRIV)
735 * Data abort dispatcher
736 * Enter in ABT mode, bsr = USER ASR, lr = USER PC
738 vector_stub dabt, ABRT_MODE
740 .long __dabt_user @ 0 (USER)
742 .long __invalid @ 2 (INTR)
743 .long __dabt_priv @ 3 (PRIV)
746 * Prefetch abort dispatcher
747 * Enter in ABT mode, bsr = USER ASR, lr = USER PC
749 vector_stub pabt, ABRT_MODE
751 .long __pabt_user @ 0 (USER)
753 .long __invalid @ 2 (INTR)
754 .long __pabt_priv @ 3 (PRIV)
757 * Undef instr entry dispatcher
758 * Enter in EXTN mode, bsr = PRIV/USER ASR, lr = PRIV/USER PC
760 vector_stub extn, EXTN_MODE
762 .long __extn_user @ 0 (USER)
764 .long __invalid @ 2 (INTR)
765 .long __extn_priv @ 3 (PRIV)
768 * We group all the following data together to optimise
769 * for CPUs with separate I & D caches.
779 .equ stubs_offset, __vectors_start + 0x200 - __stubs_start
781 .globl __vectors_start
784 b vector_extn + stubs_offset
785 ldw pc, .LCvswi + stubs_offset
786 b vector_pabt + stubs_offset
787 b vector_dabt + stubs_offset
789 b vector_intr + stubs_offset
798 .globl cr_no_alignment