/*1* linux/arch/nios2/kernel/entry.S2*3* Copyright (C) 2013-2014 Altera Corporation4* Copyright (C) 2009, Wind River Systems Inc5*6* Implemented by [email protected] and [email protected]7*8* Copyright (C) 1999-2002, Greg Ungerer ([email protected])9* Copyright (C) 1998 D. Jeff Dionne <[email protected]>,10* Kenneth Albanowski <[email protected]>,11* Copyright (C) 2000 Lineo Inc. (www.lineo.com)12* Copyright (C) 2004 Microtronix Datacom Ltd.13*14* This file is subject to the terms and conditions of the GNU General Public15* License. See the file "COPYING" in the main directory of this archive16* for more details.17*18* Linux/m68k support by Hamish Macdonald19*20* 68060 fixes by Jesper Skov21* ColdFire support by Greg Ungerer ([email protected])22* 5307 fixes by David W. Miller23* linux 2.4 support David McCullough <[email protected]>24*/2526#include <linux/sys.h>27#include <linux/linkage.h>28#include <asm/asm-offsets.h>29#include <asm/asm-macros.h>30#include <asm/thread_info.h>31#include <asm/errno.h>32#include <asm/setup.h>33#include <asm/entry.h>34#include <asm/unistd.h>35#include <asm/processor.h>3637.macro GET_THREAD_INFO reg38.if THREAD_SIZE & 0xffff000039andhi \reg, sp, %hi(~(THREAD_SIZE-1))40.else41addi \reg, r0, %lo(~(THREAD_SIZE-1))42and \reg, \reg, sp43.endif44.endm4546.macro kuser_cmpxchg_check47/*48* Make sure our user space atomic helper is restarted if it was49* interrupted in a critical region.50* ea-4 = address of interrupted insn (ea must be preserved).51* sp = saved regs.52* cmpxchg_ldw = first critical insn, cmpxchg_stw = last critical insn.53* If ea <= cmpxchg_stw and ea > cmpxchg_ldw then saved EA is set to54* cmpxchg_ldw + 4.55*/56/* et = cmpxchg_stw + 4 */57movui et, (KUSER_BASE + 4 + (cmpxchg_stw - __kuser_helper_start))58bgtu ea, et, 1f5960subi et, et, (cmpxchg_stw - cmpxchg_ldw) /* et = cmpxchg_ldw + 4 */61bltu ea, et, 1f62stw et, PT_EA(sp) /* fix up EA */63mov ea, et641:65.endm6667.section .rodata68.align 469exception_table:70.word unhandled_exception /* 0 - Reset */71.word unhandled_exception /* 1 - Processor-only Reset */72.word external_interrupt /* 2 - Interrupt */73.word handle_trap /* 3 - Trap Instruction */7475.word instruction_trap /* 4 - Unimplemented instruction */76.word handle_illegal /* 5 - Illegal instruction */77.word handle_unaligned /* 6 - Misaligned data access */78.word handle_unaligned /* 7 - Misaligned destination address */7980.word handle_diverror /* 8 - Division error */81.word protection_exception_ba /* 9 - Supervisor-only instr. address */82.word protection_exception_instr /* 10 - Supervisor only instruction */83.word protection_exception_ba /* 11 - Supervisor only data address */8485.word unhandled_exception /* 12 - Double TLB miss (data) */86.word protection_exception_pte /* 13 - TLB permission violation (x) */87.word protection_exception_pte /* 14 - TLB permission violation (r) */88.word protection_exception_pte /* 15 - TLB permission violation (w) */8990.word unhandled_exception /* 16 - MPU region violation */9192trap_table:93.word handle_system_call /* 0 */94.word handle_trap_1 /* 1 */95.word handle_trap_2 /* 2 */96.word handle_trap_3 /* 3 */97.word handle_trap_reserved /* 4 */98.word handle_trap_reserved /* 5 */99.word handle_trap_reserved /* 6 */100.word handle_trap_reserved /* 7 */101.word handle_trap_reserved /* 8 */102.word handle_trap_reserved /* 9 */103.word handle_trap_reserved /* 10 */104.word handle_trap_reserved /* 11 */105.word handle_trap_reserved /* 12 */106.word handle_trap_reserved /* 13 */107.word handle_trap_reserved /* 14 */108.word handle_trap_reserved /* 15 */109.word handle_trap_reserved /* 16 */110.word handle_trap_reserved /* 17 */111.word handle_trap_reserved /* 18 */112.word handle_trap_reserved /* 19 */113.word handle_trap_reserved /* 20 */114.word handle_trap_reserved /* 21 */115.word handle_trap_reserved /* 22 */116.word handle_trap_reserved /* 23 */117.word handle_trap_reserved /* 24 */118.word handle_trap_reserved /* 25 */119.word handle_trap_reserved /* 26 */120.word handle_trap_reserved /* 27 */121.word handle_trap_reserved /* 28 */122.word handle_trap_reserved /* 29 */123#ifdef CONFIG_KGDB124.word handle_kgdb_breakpoint /* 30 KGDB breakpoint */125#else126.word instruction_trap /* 30 */127#endif128.word handle_breakpoint /* 31 */129130.text131.set noat132.set nobreak133134ENTRY(inthandler)135SAVE_ALL136137kuser_cmpxchg_check138139/* Clear EH bit before we get a new excpetion in the kernel140* and after we have saved it to the exception frame. This is done141* whether it's trap, tlb-miss or interrupt. If we don't do this142* estatus is not updated the next exception.143*/144rdctl r24, status145movi r9, %lo(~STATUS_EH)146and r24, r24, r9147wrctl status, r24148149/* Read cause and vector and branch to the associated handler */150mov r4, sp151rdctl r5, exception152movia r9, exception_table153add r24, r9, r5154ldw r24, 0(r24)155jmp r24156157158/***********************************************************************159* Handle traps160***********************************************************************161*/162ENTRY(handle_trap)163ldwio r24, -4(ea) /* instruction that caused the exception */164srli r24, r24, 4165andi r24, r24, 0x7c166movia r9,trap_table167add r24, r24, r9168ldw r24, 0(r24)169jmp r24170171172/***********************************************************************173* Handle system calls174***********************************************************************175*/176ENTRY(handle_system_call)177/* Enable interrupts */178rdctl r10, status179ori r10, r10, STATUS_PIE180wrctl status, r10181182/* Reload registers destroyed by common code. */183ldw r4, PT_R4(sp)184ldw r5, PT_R5(sp)185186local_restart:187stw r2, PT_ORIG_R2(sp)188/* Check that the requested system call is within limits */189movui r1, __NR_syscalls190bgeu r2, r1, ret_invsyscall191slli r1, r2, 2192movhi r11, %hiadj(sys_call_table)193add r1, r1, r11194ldw r1, %lo(sys_call_table)(r1)195196/* Check if we are being traced */197GET_THREAD_INFO r11198ldw r11,TI_FLAGS(r11)199BTBNZ r11,r11,TIF_SYSCALL_TRACE,traced_system_call200201/* Execute the system call */202callr r1203204/* If the syscall returns a negative result:205* Set r7 to 1 to indicate error,206* Negate r2 to get a positive error code207* If the syscall returns zero or a positive value:208* Set r7 to 0.209* The sigreturn system calls will skip the code below by210* adding to register ra. To avoid destroying registers211*/212translate_rc_and_ret:213movi r1, 0214bge r2, zero, 3f215ldw r1, PT_ORIG_R2(sp)216addi r1, r1, 1217beq r1, zero, 3f218sub r2, zero, r2219movi r1, 12203:221stw r2, PT_R2(sp)222stw r1, PT_R7(sp)223end_translate_rc_and_ret:224225ret_from_exception:226ldw r1, PT_ESTATUS(sp)227/* if so, skip resched, signals */228TSTBNZ r1, r1, ESTATUS_EU, Luser_return229230restore_all:231rdctl r10, status /* disable intrs */232andi r10, r10, %lo(~STATUS_PIE)233wrctl status, r10234RESTORE_ALL235eret236237/* If the syscall number was invalid return ENOSYS */238ret_invsyscall:239movi r2, -ENOSYS240br translate_rc_and_ret241242/* This implements the same as above, except it calls243* do_syscall_trace_enter and do_syscall_trace_exit before and after the244* syscall in order for utilities like strace and gdb to work.245*/246traced_system_call:247SAVE_SWITCH_STACK248call do_syscall_trace_enter249RESTORE_SWITCH_STACK250251/* Create system call register arguments. The 5th and 6th252arguments on stack are already in place at the beginning253of pt_regs. */254ldw r2, PT_R2(sp)255ldw r4, PT_R4(sp)256ldw r5, PT_R5(sp)257ldw r6, PT_R6(sp)258ldw r7, PT_R7(sp)259260/* Fetch the syscall function. */261movui r1, __NR_syscalls262bgeu r2, r1, traced_invsyscall263slli r1, r2, 2264movhi r11,%hiadj(sys_call_table)265add r1, r1, r11266ldw r1, %lo(sys_call_table)(r1)267268callr r1269270/* If the syscall returns a negative result:271* Set r7 to 1 to indicate error,272* Negate r2 to get a positive error code273* If the syscall returns zero or a positive value:274* Set r7 to 0.275* The sigreturn system calls will skip the code below by276* adding to register ra. To avoid destroying registers277*/278translate_rc_and_ret2:279movi r1, 0280bge r2, zero, 4f281ldw r1, PT_ORIG_R2(sp)282addi r1, r1, 1283beq r1, zero, 4f284sub r2, zero, r2285movi r1, 12864:287stw r2, PT_R2(sp)288stw r1, PT_R7(sp)289end_translate_rc_and_ret2:290SAVE_SWITCH_STACK291call do_syscall_trace_exit292RESTORE_SWITCH_STACK293br ret_from_exception294295/* If the syscall number was invalid return ENOSYS */296traced_invsyscall:297movi r2, -ENOSYS298br translate_rc_and_ret2299300Luser_return:301GET_THREAD_INFO r11 /* get thread_info pointer */302ldw r10, TI_FLAGS(r11) /* get thread_info->flags */303ANDI32 r11, r10, _TIF_WORK_MASK304beq r11, r0, restore_all /* Nothing to do */305BTBZ r1, r10, TIF_NEED_RESCHED, Lsignal_return306307/* Reschedule work */308call schedule309br ret_from_exception310311Lsignal_return:312ANDI32 r1, r10, _TIF_SIGPENDING | _TIF_NOTIFY_RESUME313beq r1, r0, restore_all314mov r4, sp /* pt_regs */315SAVE_SWITCH_STACK316call do_notify_resume317beq r2, r0, no_work_pending318RESTORE_SWITCH_STACK319/* prepare restart syscall here without leaving kernel */320ldw r2, PT_R2(sp) /* reload syscall number in r2 */321ldw r4, PT_R4(sp) /* reload syscall arguments r4-r9 */322ldw r5, PT_R5(sp)323ldw r6, PT_R6(sp)324ldw r7, PT_R7(sp)325ldw r8, PT_R8(sp)326ldw r9, PT_R9(sp)327br local_restart /* restart syscall */328329no_work_pending:330RESTORE_SWITCH_STACK331br ret_from_exception332333/***********************************************************************334* Handle external interrupts.335***********************************************************************336*/337/*338* This is the generic interrupt handler (for all hardware interrupt339* sources). It figures out the vector number and calls the appropriate340* interrupt service routine directly.341*/342external_interrupt:343rdctl r12, ipending344rdctl r9, ienable345and r12, r12, r9346/* skip if no interrupt is pending */347beq r12, r0, ret_from_interrupt348349/*350* Process an external hardware interrupt.351*/352353addi ea, ea, -4 /* re-issue the interrupted instruction */354stw ea, PT_EA(sp)3552: movi r4, %lo(-1) /* Start from bit position 0,356highest priority */357/* This is the IRQ # for handler call */3581: andi r10, r12, 1 /* Isolate bit we are interested in */359srli r12, r12, 1 /* shift count is costly without hardware360multiplier */361addi r4, r4, 1362beq r10, r0, 1b363mov r5, sp /* Setup pt_regs pointer for handler call */364call do_IRQ365rdctl r12, ipending /* check again if irq still pending */366rdctl r9, ienable /* Isolate possible interrupts */367and r12, r12, r9368bne r12, r0, 2b369/* br ret_from_interrupt */ /* fall through to ret_from_interrupt */370371ENTRY(ret_from_interrupt)372ldw r1, PT_ESTATUS(sp) /* check if returning to kernel */373TSTBNZ r1, r1, ESTATUS_EU, Luser_return374375#ifdef CONFIG_PREEMPTION376GET_THREAD_INFO r1377ldw r4, TI_PREEMPT_COUNT(r1)378bne r4, r0, restore_all379ldw r4, TI_FLAGS(r1) /* ? Need resched set */380BTBZ r10, r4, TIF_NEED_RESCHED, restore_all381ldw r4, PT_ESTATUS(sp) /* ? Interrupts off */382andi r10, r4, ESTATUS_EPIE383beq r10, r0, restore_all384call preempt_schedule_irq385#endif386br restore_all387388/***********************************************************************389* A few syscall wrappers390***********************************************************************391*/392/*393* int clone(unsigned long clone_flags, unsigned long newsp,394* int __user * parent_tidptr, int __user * child_tidptr,395* int tls_val)396*/397ENTRY(sys_clone)398SAVE_SWITCH_STACK399subi sp, sp, 4 /* make space for tls pointer */400stw r8, 0(sp) /* pass tls pointer (r8) via stack (5th argument) */401call nios2_clone402addi sp, sp, 4403RESTORE_SWITCH_STACK404ret405406ENTRY(sys_rt_sigreturn)407SAVE_SWITCH_STACK408mov r4, sp409call do_rt_sigreturn410RESTORE_SWITCH_STACK411addi ra, ra, (end_translate_rc_and_ret - translate_rc_and_ret)412ret413414/***********************************************************************415* A few other wrappers and stubs416***********************************************************************417*/418protection_exception_pte:419rdctl r6, pteaddr420slli r6, r6, 10421call do_page_fault422br ret_from_exception423424protection_exception_ba:425rdctl r6, badaddr426call do_page_fault427br ret_from_exception428429protection_exception_instr:430call handle_supervisor_instr431br ret_from_exception432433handle_breakpoint:434call breakpoint_c435br ret_from_exception436437#ifdef CONFIG_NIOS2_ALIGNMENT_TRAP438handle_unaligned:439SAVE_SWITCH_STACK440call handle_unaligned_c441RESTORE_SWITCH_STACK442br ret_from_exception443#else444handle_unaligned:445call handle_unaligned_c446br ret_from_exception447#endif448449handle_illegal:450call handle_illegal_c451br ret_from_exception452453handle_diverror:454call handle_diverror_c455br ret_from_exception456457#ifdef CONFIG_KGDB458handle_kgdb_breakpoint:459call kgdb_breakpoint_c460br ret_from_exception461#endif462463handle_trap_1:464call handle_trap_1_c465br ret_from_exception466467handle_trap_2:468call handle_trap_2_c469br ret_from_exception470471handle_trap_3:472handle_trap_reserved:473call handle_trap_3_c474br ret_from_exception475476/*477* Beware - when entering resume, prev (the current task) is478* in r4, next (the new task) is in r5, don't change these479* registers.480*/481ENTRY(resume)482483rdctl r7, status /* save thread status reg */484stw r7, TASK_THREAD + THREAD_KPSR(r4)485486andi r7, r7, %lo(~STATUS_PIE) /* disable interrupts */487wrctl status, r7488489SAVE_SWITCH_STACK490stw sp, TASK_THREAD + THREAD_KSP(r4)/* save kernel stack pointer */491ldw sp, TASK_THREAD + THREAD_KSP(r5)/* restore new thread stack */492movia r24, _current_thread /* save thread */493GET_THREAD_INFO r1494stw r1, 0(r24)495RESTORE_SWITCH_STACK496497ldw r7, TASK_THREAD + THREAD_KPSR(r5)/* restore thread status reg */498wrctl status, r7499ret500501ENTRY(ret_from_fork)502call schedule_tail503br ret_from_exception504505ENTRY(ret_from_kernel_thread)506call schedule_tail507mov r4,r17 /* arg */508callr r16 /* function */509br ret_from_exception510511/*512* Kernel user helpers.513*514* Each segment is 64-byte aligned and will be mapped to the <User space>.515* New segments (if ever needed) must be added after the existing ones.516* This mechanism should be used only for things that are really small and517* justified, and not be abused freely.518*519*/520521/* Filling pads with undefined instructions. */522.macro kuser_pad sym size523.if ((. - \sym) & 3)524.rept (4 - (. - \sym) & 3)525.byte 0526.endr527.endif528.rept ((\size - (. - \sym)) / 4)529.word 0xdeadbeef530.endr531.endm532533.align 6534.globl __kuser_helper_start535__kuser_helper_start:536537__kuser_helper_version: /* @ 0x1000 */538.word ((__kuser_helper_end - __kuser_helper_start) >> 6)539540__kuser_cmpxchg: /* @ 0x1004 */541/*542* r4 pointer to exchange variable543* r5 old value544* r6 new value545*/546cmpxchg_ldw:547ldw r2, 0(r4) /* load current value */548sub r2, r2, r5 /* compare with old value */549bne r2, zero, cmpxchg_ret550551/* We had a match, store the new value */552cmpxchg_stw:553stw r6, 0(r4)554cmpxchg_ret:555ret556557kuser_pad __kuser_cmpxchg, 64558559.globl __kuser_sigtramp560__kuser_sigtramp:561movi r2, __NR_rt_sigreturn562trap563564kuser_pad __kuser_sigtramp, 64565566.globl __kuser_helper_end567__kuser_helper_end:568569570