entry.S 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. * linux/arch/nios2/kernel/entry.S
  3. *
  4. * Copyright (C) 2013-2014 Altera Corporation
  5. * Copyright (C) 2009, Wind River Systems Inc
  6. *
  7. * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com
  8. *
  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.
  14. *
  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
  17. * for more details.
  18. *
  19. * Linux/m68k support by Hamish Macdonald
  20. *
  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>
  25. */
  26. #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>
  36. .macro GET_THREAD_INFO reg
  37. .if THREAD_SIZE & 0xffff0000
  38. andhi \reg, sp, %hi(~(THREAD_SIZE-1))
  39. .else
  40. addi \reg, r0, %lo(~(THREAD_SIZE-1))
  41. and \reg, \reg, sp
  42. .endif
  43. .endm
  44. .macro kuser_cmpxchg_check
  45. /*
  46. * Make sure our user space atomic helper is restarted if it was
  47. * interrupted in a critical region.
  48. * ea-4 = address of interrupted insn (ea must be preserved).
  49. * sp = saved regs.
  50. * cmpxchg_ldw = first critical insn, cmpxchg_stw = last critical insn.
  51. * If ea <= cmpxchg_stw and ea > cmpxchg_ldw then saved EA is set to
  52. * cmpxchg_ldw + 4.
  53. */
  54. /* et = cmpxchg_stw + 4 */
  55. movui et, (KUSER_BASE + 4 + (cmpxchg_stw - __kuser_helper_start))
  56. bgtu ea, et, 1f
  57. subi et, et, (cmpxchg_stw - cmpxchg_ldw) /* et = cmpxchg_ldw + 4 */
  58. bltu ea, et, 1f
  59. stw et, PT_EA(sp) /* fix up EA */
  60. mov ea, et
  61. 1:
  62. .endm
  63. .section .rodata
  64. .align 4
  65. exception_table:
  66. .word unhandled_exception /* 0 - Reset */
  67. .word unhandled_exception /* 1 - Processor-only Reset */
  68. .word external_interrupt /* 2 - Interrupt */
  69. .word handle_trap /* 3 - Trap Instruction */
  70. .word instruction_trap /* 4 - Unimplemented instruction */
  71. .word handle_illegal /* 5 - Illegal instruction */
  72. .word handle_unaligned /* 6 - Misaligned data access */
  73. .word handle_unaligned /* 7 - Misaligned destination address */
  74. .word handle_diverror /* 8 - Division error */
  75. .word protection_exception_ba /* 9 - Supervisor-only instr. address */
  76. .word protection_exception_instr /* 10 - Supervisor only instruction */
  77. .word protection_exception_ba /* 11 - Supervisor only data address */
  78. .word unhandled_exception /* 12 - Double TLB miss (data) */
  79. .word protection_exception_pte /* 13 - TLB permission violation (x) */
  80. .word protection_exception_pte /* 14 - TLB permission violation (r) */
  81. .word protection_exception_pte /* 15 - TLB permission violation (w) */
  82. .word unhandled_exception /* 16 - MPU region violation */
  83. trap_table:
  84. .word handle_system_call /* 0 */
  85. .word handle_trap_1 /* 1 */
  86. .word handle_trap_2 /* 2 */
  87. .word handle_trap_3 /* 3 */
  88. .word handle_trap_reserved /* 4 */
  89. .word handle_trap_reserved /* 5 */
  90. .word handle_trap_reserved /* 6 */
  91. .word handle_trap_reserved /* 7 */
  92. .word handle_trap_reserved /* 8 */
  93. .word handle_trap_reserved /* 9 */
  94. .word handle_trap_reserved /* 10 */
  95. .word handle_trap_reserved /* 11 */
  96. .word handle_trap_reserved /* 12 */
  97. .word handle_trap_reserved /* 13 */
  98. .word handle_trap_reserved /* 14 */
  99. .word handle_trap_reserved /* 15 */
  100. .word handle_trap_reserved /* 16 */
  101. .word handle_trap_reserved /* 17 */
  102. .word handle_trap_reserved /* 18 */
  103. .word handle_trap_reserved /* 19 */
  104. .word handle_trap_reserved /* 20 */
  105. .word handle_trap_reserved /* 21 */
  106. .word handle_trap_reserved /* 22 */
  107. .word handle_trap_reserved /* 23 */
  108. .word handle_trap_reserved /* 24 */
  109. .word handle_trap_reserved /* 25 */
  110. .word handle_trap_reserved /* 26 */
  111. .word handle_trap_reserved /* 27 */
  112. .word handle_trap_reserved /* 28 */
  113. .word handle_trap_reserved /* 29 */
  114. #ifdef CONFIG_KGDB
  115. .word handle_kgdb_breakpoint /* 30 KGDB breakpoint */
  116. #else
  117. .word instruction_trap /* 30 */
  118. #endif
  119. .word handle_breakpoint /* 31 */
  120. .text
  121. .set noat
  122. .set nobreak
  123. ENTRY(inthandler)
  124. SAVE_ALL
  125. kuser_cmpxchg_check
  126. /* Clear EH bit before we get a new excpetion in the kernel
  127. * and after we have saved it to the exception frame. This is done
  128. * whether it's trap, tlb-miss or interrupt. If we don't do this
  129. * estatus is not updated the next exception.
  130. */
  131. rdctl r24, status
  132. movi r9, %lo(~STATUS_EH)
  133. and r24, r24, r9
  134. wrctl status, r24
  135. /* Read cause and vector and branch to the associated handler */
  136. mov r4, sp
  137. rdctl r5, exception
  138. movia r9, exception_table
  139. add r24, r9, r5
  140. ldw r24, 0(r24)
  141. jmp r24
  142. /***********************************************************************
  143. * Handle traps
  144. ***********************************************************************
  145. */
  146. ENTRY(handle_trap)
  147. ldwio r24, -4(ea) /* instruction that caused the exception */
  148. srli r24, r24, 4
  149. andi r24, r24, 0x7c
  150. movia r9,trap_table
  151. add r24, r24, r9
  152. ldw r24, 0(r24)
  153. jmp r24
  154. /***********************************************************************
  155. * Handle system calls
  156. ***********************************************************************
  157. */
  158. ENTRY(handle_system_call)
  159. /* Enable interrupts */
  160. rdctl r10, status
  161. ori r10, r10, STATUS_PIE
  162. wrctl status, r10
  163. /* Reload registers destroyed by common code. */
  164. ldw r4, PT_R4(sp)
  165. ldw r5, PT_R5(sp)
  166. local_restart:
  167. /* Check that the requested system call is within limits */
  168. movui r1, __NR_syscalls
  169. bgeu r2, r1, ret_invsyscall
  170. slli r1, r2, 2
  171. movhi r11, %hiadj(sys_call_table)
  172. add r1, r1, r11
  173. ldw r1, %lo(sys_call_table)(r1)
  174. beq r1, r0, ret_invsyscall
  175. /* Check if we are being traced */
  176. GET_THREAD_INFO r11
  177. ldw r11,TI_FLAGS(r11)
  178. BTBNZ r11,r11,TIF_SYSCALL_TRACE,traced_system_call
  179. /* Execute the system call */
  180. callr r1
  181. /* If the syscall returns a negative result:
  182. * Set r7 to 1 to indicate error,
  183. * Negate r2 to get a positive error code
  184. * If the syscall returns zero or a positive value:
  185. * Set r7 to 0.
  186. * The sigreturn system calls will skip the code below by
  187. * adding to register ra. To avoid destroying registers
  188. */
  189. translate_rc_and_ret:
  190. movi r1, 0
  191. bge r2, zero, 3f
  192. sub r2, zero, r2
  193. movi r1, 1
  194. 3:
  195. stw r2, PT_R2(sp)
  196. stw r1, PT_R7(sp)
  197. end_translate_rc_and_ret:
  198. ret_from_exception:
  199. ldw r1, PT_ESTATUS(sp)
  200. /* if so, skip resched, signals */
  201. TSTBNZ r1, r1, ESTATUS_EU, Luser_return
  202. restore_all:
  203. rdctl r10, status /* disable intrs */
  204. andi r10, r10, %lo(~STATUS_PIE)
  205. wrctl status, r10
  206. RESTORE_ALL
  207. eret
  208. /* If the syscall number was invalid return ENOSYS */
  209. ret_invsyscall:
  210. movi r2, -ENOSYS
  211. br translate_rc_and_ret
  212. /* This implements the same as above, except it calls
  213. * do_syscall_trace_enter and do_syscall_trace_exit before and after the
  214. * syscall in order for utilities like strace and gdb to work.
  215. */
  216. traced_system_call:
  217. SAVE_SWITCH_STACK
  218. call do_syscall_trace_enter
  219. RESTORE_SWITCH_STACK
  220. /* Create system call register arguments. The 5th and 6th
  221. arguments on stack are already in place at the beginning
  222. of pt_regs. */
  223. ldw r2, PT_R2(sp)
  224. ldw r4, PT_R4(sp)
  225. ldw r5, PT_R5(sp)
  226. ldw r6, PT_R6(sp)
  227. ldw r7, PT_R7(sp)
  228. /* Fetch the syscall function, we don't need to check the boundaries
  229. * since this is already done.
  230. */
  231. slli r1, r2, 2
  232. movhi r11,%hiadj(sys_call_table)
  233. add r1, r1, r11
  234. ldw r1, %lo(sys_call_table)(r1)
  235. callr r1
  236. /* If the syscall returns a negative result:
  237. * Set r7 to 1 to indicate error,
  238. * Negate r2 to get a positive error code
  239. * If the syscall returns zero or a positive value:
  240. * Set r7 to 0.
  241. * The sigreturn system calls will skip the code below by
  242. * adding to register ra. To avoid destroying registers
  243. */
  244. translate_rc_and_ret2:
  245. movi r1, 0
  246. bge r2, zero, 4f
  247. sub r2, zero, r2
  248. movi r1, 1
  249. 4:
  250. stw r2, PT_R2(sp)
  251. stw r1, PT_R7(sp)
  252. end_translate_rc_and_ret2:
  253. SAVE_SWITCH_STACK
  254. call do_syscall_trace_exit
  255. RESTORE_SWITCH_STACK
  256. br ret_from_exception
  257. Luser_return:
  258. GET_THREAD_INFO r11 /* get thread_info pointer */
  259. ldw r10, TI_FLAGS(r11) /* get thread_info->flags */
  260. ANDI32 r11, r10, _TIF_WORK_MASK
  261. beq r11, r0, restore_all /* Nothing to do */
  262. BTBZ r1, r10, TIF_NEED_RESCHED, Lsignal_return
  263. /* Reschedule work */
  264. call schedule
  265. br ret_from_exception
  266. Lsignal_return:
  267. ANDI32 r1, r10, _TIF_SIGPENDING | _TIF_NOTIFY_RESUME
  268. beq r1, r0, restore_all
  269. mov r4, sp /* pt_regs */
  270. SAVE_SWITCH_STACK
  271. call do_notify_resume
  272. beq r2, r0, no_work_pending
  273. RESTORE_SWITCH_STACK
  274. /* prepare restart syscall here without leaving kernel */
  275. ldw r2, PT_R2(sp) /* reload syscall number in r2 */
  276. ldw r4, PT_R4(sp) /* reload syscall arguments r4-r9 */
  277. ldw r5, PT_R5(sp)
  278. ldw r6, PT_R6(sp)
  279. ldw r7, PT_R7(sp)
  280. ldw r8, PT_R8(sp)
  281. ldw r9, PT_R9(sp)
  282. br local_restart /* restart syscall */
  283. no_work_pending:
  284. RESTORE_SWITCH_STACK
  285. br ret_from_exception
  286. /***********************************************************************
  287. * Handle external interrupts.
  288. ***********************************************************************
  289. */
  290. /*
  291. * This is the generic interrupt handler (for all hardware interrupt
  292. * sources). It figures out the vector number and calls the appropriate
  293. * interrupt service routine directly.
  294. */
  295. external_interrupt:
  296. rdctl r12, ipending
  297. rdctl r9, ienable
  298. and r12, r12, r9
  299. /* skip if no interrupt is pending */
  300. beq r12, r0, ret_from_interrupt
  301. movi r24, -1
  302. stw r24, PT_ORIG_R2(sp)
  303. /*
  304. * Process an external hardware interrupt.
  305. */
  306. addi ea, ea, -4 /* re-issue the interrupted instruction */
  307. stw ea, PT_EA(sp)
  308. 2: movi r4, %lo(-1) /* Start from bit position 0,
  309. highest priority */
  310. /* This is the IRQ # for handler call */
  311. 1: andi r10, r12, 1 /* Isolate bit we are interested in */
  312. srli r12, r12, 1 /* shift count is costly without hardware
  313. multiplier */
  314. addi r4, r4, 1
  315. beq r10, r0, 1b
  316. mov r5, sp /* Setup pt_regs pointer for handler call */
  317. call do_IRQ
  318. rdctl r12, ipending /* check again if irq still pending */
  319. rdctl r9, ienable /* Isolate possible interrupts */
  320. and r12, r12, r9
  321. bne r12, r0, 2b
  322. /* br ret_from_interrupt */ /* fall through to ret_from_interrupt */
  323. ENTRY(ret_from_interrupt)
  324. ldw r1, PT_ESTATUS(sp) /* check if returning to kernel */
  325. TSTBNZ r1, r1, ESTATUS_EU, Luser_return
  326. #ifdef CONFIG_PREEMPTION
  327. GET_THREAD_INFO r1
  328. ldw r4, TI_PREEMPT_COUNT(r1)
  329. bne r4, r0, restore_all
  330. ldw r4, TI_FLAGS(r1) /* ? Need resched set */
  331. BTBZ r10, r4, TIF_NEED_RESCHED, restore_all
  332. ldw r4, PT_ESTATUS(sp) /* ? Interrupts off */
  333. andi r10, r4, ESTATUS_EPIE
  334. beq r10, r0, restore_all
  335. call preempt_schedule_irq
  336. #endif
  337. br restore_all
  338. /***********************************************************************
  339. * A few syscall wrappers
  340. ***********************************************************************
  341. */
  342. /*
  343. * int clone(unsigned long clone_flags, unsigned long newsp,
  344. * int __user * parent_tidptr, int __user * child_tidptr,
  345. * int tls_val)
  346. */
  347. ENTRY(sys_clone)
  348. SAVE_SWITCH_STACK
  349. subi sp, sp, 4 /* make space for tls pointer */
  350. stw r8, 0(sp) /* pass tls pointer (r8) via stack (5th argument) */
  351. call nios2_clone
  352. addi sp, sp, 4
  353. RESTORE_SWITCH_STACK
  354. ret
  355. ENTRY(sys_rt_sigreturn)
  356. SAVE_SWITCH_STACK
  357. mov r4, sp
  358. call do_rt_sigreturn
  359. RESTORE_SWITCH_STACK
  360. addi ra, ra, (end_translate_rc_and_ret - translate_rc_and_ret)
  361. ret
  362. /***********************************************************************
  363. * A few other wrappers and stubs
  364. ***********************************************************************
  365. */
  366. protection_exception_pte:
  367. rdctl r6, pteaddr
  368. slli r6, r6, 10
  369. call do_page_fault
  370. br ret_from_exception
  371. protection_exception_ba:
  372. rdctl r6, badaddr
  373. call do_page_fault
  374. br ret_from_exception
  375. protection_exception_instr:
  376. call handle_supervisor_instr
  377. br ret_from_exception
  378. handle_breakpoint:
  379. call breakpoint_c
  380. br ret_from_exception
  381. #ifdef CONFIG_NIOS2_ALIGNMENT_TRAP
  382. handle_unaligned:
  383. SAVE_SWITCH_STACK
  384. call handle_unaligned_c
  385. RESTORE_SWITCH_STACK
  386. br ret_from_exception
  387. #else
  388. handle_unaligned:
  389. call handle_unaligned_c
  390. br ret_from_exception
  391. #endif
  392. handle_illegal:
  393. call handle_illegal_c
  394. br ret_from_exception
  395. handle_diverror:
  396. call handle_diverror_c
  397. br ret_from_exception
  398. #ifdef CONFIG_KGDB
  399. handle_kgdb_breakpoint:
  400. call kgdb_breakpoint_c
  401. br ret_from_exception
  402. #endif
  403. handle_trap_1:
  404. call handle_trap_1_c
  405. br ret_from_exception
  406. handle_trap_2:
  407. call handle_trap_2_c
  408. br ret_from_exception
  409. handle_trap_3:
  410. handle_trap_reserved:
  411. call handle_trap_3_c
  412. br ret_from_exception
  413. /*
  414. * Beware - when entering resume, prev (the current task) is
  415. * in r4, next (the new task) is in r5, don't change these
  416. * registers.
  417. */
  418. ENTRY(resume)
  419. rdctl r7, status /* save thread status reg */
  420. stw r7, TASK_THREAD + THREAD_KPSR(r4)
  421. andi r7, r7, %lo(~STATUS_PIE) /* disable interrupts */
  422. wrctl status, r7
  423. SAVE_SWITCH_STACK
  424. stw sp, TASK_THREAD + THREAD_KSP(r4)/* save kernel stack pointer */
  425. ldw sp, TASK_THREAD + THREAD_KSP(r5)/* restore new thread stack */
  426. movia r24, _current_thread /* save thread */
  427. GET_THREAD_INFO r1
  428. stw r1, 0(r24)
  429. RESTORE_SWITCH_STACK
  430. ldw r7, TASK_THREAD + THREAD_KPSR(r5)/* restore thread status reg */
  431. wrctl status, r7
  432. ret
  433. ENTRY(ret_from_fork)
  434. call schedule_tail
  435. br ret_from_exception
  436. ENTRY(ret_from_kernel_thread)
  437. call schedule_tail
  438. mov r4,r17 /* arg */
  439. callr r16 /* function */
  440. br ret_from_exception
  441. /*
  442. * Kernel user helpers.
  443. *
  444. * Each segment is 64-byte aligned and will be mapped to the <User space>.
  445. * New segments (if ever needed) must be added after the existing ones.
  446. * This mechanism should be used only for things that are really small and
  447. * justified, and not be abused freely.
  448. *
  449. */
  450. /* Filling pads with undefined instructions. */
  451. .macro kuser_pad sym size
  452. .if ((. - \sym) & 3)
  453. .rept (4 - (. - \sym) & 3)
  454. .byte 0
  455. .endr
  456. .endif
  457. .rept ((\size - (. - \sym)) / 4)
  458. .word 0xdeadbeef
  459. .endr
  460. .endm
  461. .align 6
  462. .globl __kuser_helper_start
  463. __kuser_helper_start:
  464. __kuser_helper_version: /* @ 0x1000 */
  465. .word ((__kuser_helper_end - __kuser_helper_start) >> 6)
  466. __kuser_cmpxchg: /* @ 0x1004 */
  467. /*
  468. * r4 pointer to exchange variable
  469. * r5 old value
  470. * r6 new value
  471. */
  472. cmpxchg_ldw:
  473. ldw r2, 0(r4) /* load current value */
  474. sub r2, r2, r5 /* compare with old value */
  475. bne r2, zero, cmpxchg_ret
  476. /* We had a match, store the new value */
  477. cmpxchg_stw:
  478. stw r6, 0(r4)
  479. cmpxchg_ret:
  480. ret
  481. kuser_pad __kuser_cmpxchg, 64
  482. .globl __kuser_sigtramp
  483. __kuser_sigtramp:
  484. movi r2, __NR_rt_sigreturn
  485. trap
  486. kuser_pad __kuser_sigtramp, 64
  487. .globl __kuser_helper_end
  488. __kuser_helper_end: