2 * linux/arch/nios2/kernel/entry.S
4 * Copyright (C) 2013-2014 Altera Corporation
5 * Copyright (C) 2009, Wind River Systems Inc
7 * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com
9 * Copyright (C) 1999-2002, Greg Ungerer (gerg@snapgear.com)
10 * Copyright (C) 1998 D. Jeff Dionne <jeff@lineo.ca>,
11 * Kenneth Albanowski <kjahds@kjahds.com>,
12 * Copyright (C) 2000 Lineo Inc. (www.lineo.com)
13 * Copyright (C) 2004 Microtronix Datacom Ltd.
15 * This file is subject to the terms and conditions of the GNU General Public
16 * License. See the file "COPYING" in the main directory of this archive
19 * Linux/m68k support by Hamish Macdonald
21 * 68060 fixes by Jesper Skov
22 * ColdFire support by Greg Ungerer (gerg@snapgear.com)
23 * 5307 fixes by David W. Miller
24 * linux 2.4 support David McCullough <davidm@snapgear.com>
27 #include <linux/sys.h>
28 #include <linux/linkage.h>
29 #include <asm/asm-offsets.h>
30 #include <asm/asm-macros.h>
31 #include <asm/thread_info.h>
32 #include <asm/errno.h>
33 #include <asm/setup.h>
34 #include <asm/entry.h>
35 #include <asm/unistd.h>
36 #include <asm/processor.h>
38 .macro GET_THREAD_INFO reg
39 .if THREAD_SIZE & 0xffff0000
40 andhi \reg, sp, %hi(~(THREAD_SIZE-1))
42 addi \reg, r0, %lo(~(THREAD_SIZE-1))
47 .macro kuser_cmpxchg_check
49 * Make sure our user space atomic helper is restarted if it was
50 * interrupted in a critical region.
51 * ea-4 = address of interrupted insn (ea must be preserved).
53 * cmpxchg_ldw = first critical insn, cmpxchg_stw = last critical insn.
54 * If ea <= cmpxchg_stw and ea > cmpxchg_ldw then saved EA is set to
57 /* et = cmpxchg_stw + 4 */
58 movui et, (KUSER_BASE + 4 + (cmpxchg_stw - __kuser_helper_start))
61 subi et, et, (cmpxchg_stw - cmpxchg_ldw) /* et = cmpxchg_ldw + 4 */
63 stw et, PT_EA(sp) /* fix up EA */
71 .word unhandled_exception /* 0 - Reset */
72 .word unhandled_exception /* 1 - Processor-only Reset */
73 .word external_interrupt /* 2 - Interrupt */
74 .word handle_trap /* 3 - Trap Instruction */
76 .word instruction_trap /* 4 - Unimplemented instruction */
77 .word handle_illegal /* 5 - Illegal instruction */
78 .word handle_unaligned /* 6 - Misaligned data access */
79 .word handle_unaligned /* 7 - Misaligned destination address */
81 .word handle_diverror /* 8 - Division error */
82 .word protection_exception_ba /* 9 - Supervisor-only instr. address */
83 .word protection_exception_instr /* 10 - Supervisor only instruction */
84 .word protection_exception_ba /* 11 - Supervisor only data address */
86 .word unhandled_exception /* 12 - Double TLB miss (data) */
87 .word protection_exception_pte /* 13 - TLB permission violation (x) */
88 .word protection_exception_pte /* 14 - TLB permission violation (r) */
89 .word protection_exception_pte /* 15 - TLB permission violation (w) */
91 .word unhandled_exception /* 16 - MPU region violation */
94 .word handle_system_call /* 0 */
95 .word handle_trap_1 /* 1 */
96 .word handle_trap_2 /* 2 */
97 .word handle_trap_3 /* 3 */
98 .word handle_trap_reserved /* 4 */
99 .word handle_trap_reserved /* 5 */
100 .word handle_trap_reserved /* 6 */
101 .word handle_trap_reserved /* 7 */
102 .word handle_trap_reserved /* 8 */
103 .word handle_trap_reserved /* 9 */
104 .word handle_trap_reserved /* 10 */
105 .word handle_trap_reserved /* 11 */
106 .word handle_trap_reserved /* 12 */
107 .word handle_trap_reserved /* 13 */
108 .word handle_trap_reserved /* 14 */
109 .word handle_trap_reserved /* 15 */
110 .word handle_trap_reserved /* 16 */
111 .word handle_trap_reserved /* 17 */
112 .word handle_trap_reserved /* 18 */
113 .word handle_trap_reserved /* 19 */
114 .word handle_trap_reserved /* 20 */
115 .word handle_trap_reserved /* 21 */
116 .word handle_trap_reserved /* 22 */
117 .word handle_trap_reserved /* 23 */
118 .word handle_trap_reserved /* 24 */
119 .word handle_trap_reserved /* 25 */
120 .word handle_trap_reserved /* 26 */
121 .word handle_trap_reserved /* 27 */
122 .word handle_trap_reserved /* 28 */
123 .word handle_trap_reserved /* 29 */
125 .word handle_kgdb_breakpoint /* 30 KGDB breakpoint */
127 .word instruction_trap /* 30 */
129 .word handle_breakpoint /* 31 */
140 /* Clear EH bit before we get a new excpetion in the kernel
141 * and after we have saved it to the exception frame. This is done
142 * whether it's trap, tlb-miss or interrupt. If we don't do this
143 * estatus is not updated the next exception.
146 movi r9, %lo(~STATUS_EH)
150 /* Read cause and vector and branch to the associated handler */
153 movia r9, exception_table
159 /***********************************************************************
161 ***********************************************************************
164 ldwio r24, -4(ea) /* instruction that caused the exception */
173 /***********************************************************************
174 * Handle system calls
175 ***********************************************************************
177 ENTRY(handle_system_call)
178 /* Enable interrupts */
180 ori r10, r10, STATUS_PIE
183 /* Reload registers destroyed by common code. */
188 /* Check that the requested system call is within limits */
189 movui r1, __NR_syscalls
190 bgeu r2, r1, ret_invsyscall
192 movhi r11, %hiadj(sys_call_table)
194 ldw r1, %lo(sys_call_table)(r1)
195 beq r1, r0, ret_invsyscall
197 /* Check if we are being traced */
199 ldw r11,TI_FLAGS(r11)
200 BTBNZ r11,r11,TIF_SYSCALL_TRACE,traced_system_call
202 /* Execute the system call */
205 /* If the syscall returns a negative result:
206 * Set r7 to 1 to indicate error,
207 * Negate r2 to get a positive error code
208 * If the syscall returns zero or a positive value:
210 * The sigreturn system calls will skip the code below by
211 * adding to register ra. To avoid destroying registers
213 translate_rc_and_ret:
221 end_translate_rc_and_ret:
224 ldw r1, PT_ESTATUS(sp)
225 /* if so, skip resched, signals */
226 TSTBNZ r1, r1, ESTATUS_EU, Luser_return
229 rdctl r10, status /* disable intrs */
230 andi r10, r10, %lo(~STATUS_PIE)
235 /* If the syscall number was invalid return ENOSYS */
238 br translate_rc_and_ret
240 /* This implements the same as above, except it calls
241 * do_syscall_trace_enter and do_syscall_trace_exit before and after the
242 * syscall in order for utilities like strace and gdb to work.
246 call do_syscall_trace_enter
249 /* Create system call register arguments. The 5th and 6th
250 arguments on stack are already in place at the beginning
258 /* Fetch the syscall function, we don't need to check the boundaries
259 * since this is already done.
262 movhi r11,%hiadj(sys_call_table)
264 ldw r1, %lo(sys_call_table)(r1)
268 /* If the syscall returns a negative result:
269 * Set r7 to 1 to indicate error,
270 * Negate r2 to get a positive error code
271 * If the syscall returns zero or a positive value:
273 * The sigreturn system calls will skip the code below by
274 * adding to register ra. To avoid destroying registers
276 translate_rc_and_ret2:
284 end_translate_rc_and_ret2:
286 call do_syscall_trace_exit
288 br ret_from_exception
291 GET_THREAD_INFO r11 /* get thread_info pointer */
292 ldw r10, TI_FLAGS(r11) /* get thread_info->flags */
293 ANDI32 r11, r10, _TIF_WORK_MASK
294 beq r11, r0, restore_all /* Nothing to do */
295 BTBZ r1, r10, TIF_NEED_RESCHED, Lsignal_return
297 /* Reschedule work */
299 br ret_from_exception
302 ANDI32 r1, r10, _TIF_SIGPENDING | _TIF_NOTIFY_RESUME
303 beq r1, r0, restore_all
304 mov r4, sp /* pt_regs */
306 call do_notify_resume
307 beq r2, r0, no_work_pending
309 /* prepare restart syscall here without leaving kernel */
310 ldw r2, PT_R2(sp) /* reload syscall number in r2 */
311 ldw r4, PT_R4(sp) /* reload syscall arguments r4-r9 */
317 br local_restart /* restart syscall */
321 br ret_from_exception
323 /***********************************************************************
324 * Handle external interrupts.
325 ***********************************************************************
328 * This is the generic interrupt handler (for all hardware interrupt
329 * sources). It figures out the vector number and calls the appropriate
330 * interrupt service routine directly.
336 /* skip if no interrupt is pending */
337 beq r12, r0, ret_from_interrupt
340 stw r24, PT_ORIG_R2(sp)
343 * Process an external hardware interrupt.
346 addi ea, ea, -4 /* re-issue the interrupted instruction */
348 2: movi r4, %lo(-1) /* Start from bit position 0,
350 /* This is the IRQ # for handler call */
351 1: andi r10, r12, 1 /* Isolate bit we are interested in */
352 srli r12, r12, 1 /* shift count is costly without hardware
356 mov r5, sp /* Setup pt_regs pointer for handler call */
358 rdctl r12, ipending /* check again if irq still pending */
359 rdctl r9, ienable /* Isolate possible interrupts */
362 /* br ret_from_interrupt */ /* fall through to ret_from_interrupt */
364 ENTRY(ret_from_interrupt)
365 ldw r1, PT_ESTATUS(sp) /* check if returning to kernel */
366 TSTBNZ r1, r1, ESTATUS_EU, Luser_return
368 #ifdef CONFIG_PREEMPTION
370 ldw r4, TI_PREEMPT_COUNT(r1)
371 bne r4, r0, restore_all
372 ldw r4, TI_FLAGS(r1) /* ? Need resched set */
373 BTBZ r10, r4, TIF_NEED_RESCHED, restore_all
374 ldw r4, PT_ESTATUS(sp) /* ? Interrupts off */
375 andi r10, r4, ESTATUS_EPIE
376 beq r10, r0, restore_all
377 call preempt_schedule_irq
381 /***********************************************************************
382 * A few syscall wrappers
383 ***********************************************************************
386 * int clone(unsigned long clone_flags, unsigned long newsp,
387 * int __user * parent_tidptr, int __user * child_tidptr,
396 ENTRY(sys_rt_sigreturn)
401 addi ra, ra, (end_translate_rc_and_ret - translate_rc_and_ret)
404 /***********************************************************************
405 * A few other wrappers and stubs
406 ***********************************************************************
408 protection_exception_pte:
412 br ret_from_exception
414 protection_exception_ba:
417 br ret_from_exception
419 protection_exception_instr:
420 call handle_supervisor_instr
421 br ret_from_exception
425 br ret_from_exception
427 #ifdef CONFIG_NIOS2_ALIGNMENT_TRAP
430 call handle_unaligned_c
432 br ret_from_exception
435 call handle_unaligned_c
436 br ret_from_exception
440 call handle_illegal_c
441 br ret_from_exception
444 call handle_diverror_c
445 br ret_from_exception
448 handle_kgdb_breakpoint:
449 call kgdb_breakpoint_c
450 br ret_from_exception
455 br ret_from_exception
459 br ret_from_exception
462 handle_trap_reserved:
464 br ret_from_exception
467 * Beware - when entering resume, prev (the current task) is
468 * in r4, next (the new task) is in r5, don't change these
473 rdctl r7, status /* save thread status reg */
474 stw r7, TASK_THREAD + THREAD_KPSR(r4)
476 andi r7, r7, %lo(~STATUS_PIE) /* disable interrupts */
480 stw sp, TASK_THREAD + THREAD_KSP(r4)/* save kernel stack pointer */
481 ldw sp, TASK_THREAD + THREAD_KSP(r5)/* restore new thread stack */
482 movia r24, _current_thread /* save thread */
487 ldw r7, TASK_THREAD + THREAD_KPSR(r5)/* restore thread status reg */
493 br ret_from_exception
495 ENTRY(ret_from_kernel_thread)
498 callr r16 /* function */
499 br ret_from_exception
502 * Kernel user helpers.
504 * Each segment is 64-byte aligned and will be mapped to the <User space>.
505 * New segments (if ever needed) must be added after the existing ones.
506 * This mechanism should be used only for things that are really small and
507 * justified, and not be abused freely.
511 /* Filling pads with undefined instructions. */
512 .macro kuser_pad sym size
514 .rept (4 - (. - \sym) & 3)
518 .rept ((\size - (. - \sym)) / 4)
524 .globl __kuser_helper_start
525 __kuser_helper_start:
527 __kuser_helper_version: /* @ 0x1000 */
528 .word ((__kuser_helper_end - __kuser_helper_start) >> 6)
530 __kuser_cmpxchg: /* @ 0x1004 */
532 * r4 pointer to exchange variable
537 ldw r2, 0(r4) /* load current value */
538 sub r2, r2, r5 /* compare with old value */
539 bne r2, zero, cmpxchg_ret
541 /* We had a match, store the new value */
547 kuser_pad __kuser_cmpxchg, 64
549 .globl __kuser_sigtramp
551 movi r2, __NR_rt_sigreturn
554 kuser_pad __kuser_sigtramp, 64
556 .globl __kuser_helper_end