entry.S 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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 [email protected] and [email protected]
  8. *
  9. * Copyright (C) 1999-2002, Greg Ungerer ([email protected])
  10. * Copyright (C) 1998 D. Jeff Dionne <[email protected]>,
  11. * Kenneth Albanowski <[email protected]>,
  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 ([email protected])
  23. * 5307 fixes by David W. Miller
  24. * linux 2.4 support David McCullough <[email protected]>
  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. stw r2, PT_ORIG_R2(sp)
  168. /* Check that the requested system call is within limits */
  169. movui r1, __NR_syscalls
  170. bgeu r2, r1, ret_invsyscall
  171. slli r1, r2, 2
  172. movhi r11, %hiadj(sys_call_table)
  173. add r1, r1, r11
  174. ldw r1, %lo(sys_call_table)(r1)
  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. ldw r1, PT_ORIG_R2(sp)
  193. addi r1, r1, 1
  194. beq r1, zero, 3f
  195. sub r2, zero, r2
  196. movi r1, 1
  197. 3:
  198. stw r2, PT_R2(sp)
  199. stw r1, PT_R7(sp)
  200. end_translate_rc_and_ret:
  201. ret_from_exception:
  202. ldw r1, PT_ESTATUS(sp)
  203. /* if so, skip resched, signals */
  204. TSTBNZ r1, r1, ESTATUS_EU, Luser_return
  205. restore_all:
  206. rdctl r10, status /* disable intrs */
  207. andi r10, r10, %lo(~STATUS_PIE)
  208. wrctl status, r10
  209. RESTORE_ALL
  210. eret
  211. /* If the syscall number was invalid return ENOSYS */
  212. ret_invsyscall:
  213. movi r2, -ENOSYS
  214. br translate_rc_and_ret
  215. /* This implements the same as above, except it calls
  216. * do_syscall_trace_enter and do_syscall_trace_exit before and after the
  217. * syscall in order for utilities like strace and gdb to work.
  218. */
  219. traced_system_call:
  220. SAVE_SWITCH_STACK
  221. call do_syscall_trace_enter
  222. RESTORE_SWITCH_STACK
  223. /* Create system call register arguments. The 5th and 6th
  224. arguments on stack are already in place at the beginning
  225. of pt_regs. */
  226. ldw r2, PT_R2(sp)
  227. ldw r4, PT_R4(sp)
  228. ldw r5, PT_R5(sp)
  229. ldw r6, PT_R6(sp)
  230. ldw r7, PT_R7(sp)
  231. /* Fetch the syscall function. */
  232. movui r1, __NR_syscalls
  233. bgeu r2, r1, traced_invsyscall
  234. slli r1, r2, 2
  235. movhi r11,%hiadj(sys_call_table)
  236. add r1, r1, r11
  237. ldw r1, %lo(sys_call_table)(r1)
  238. callr r1
  239. /* If the syscall returns a negative result:
  240. * Set r7 to 1 to indicate error,
  241. * Negate r2 to get a positive error code
  242. * If the syscall returns zero or a positive value:
  243. * Set r7 to 0.
  244. * The sigreturn system calls will skip the code below by
  245. * adding to register ra. To avoid destroying registers
  246. */
  247. translate_rc_and_ret2:
  248. movi r1, 0
  249. bge r2, zero, 4f
  250. ldw r1, PT_ORIG_R2(sp)
  251. addi r1, r1, 1
  252. beq r1, zero, 4f
  253. sub r2, zero, r2
  254. movi r1, 1
  255. 4:
  256. stw r2, PT_R2(sp)
  257. stw r1, PT_R7(sp)
  258. end_translate_rc_and_ret2:
  259. SAVE_SWITCH_STACK
  260. call do_syscall_trace_exit
  261. RESTORE_SWITCH_STACK
  262. br ret_from_exception
  263. /* If the syscall number was invalid return ENOSYS */
  264. traced_invsyscall:
  265. movi r2, -ENOSYS
  266. br translate_rc_and_ret2
  267. Luser_return:
  268. GET_THREAD_INFO r11 /* get thread_info pointer */
  269. ldw r10, TI_FLAGS(r11) /* get thread_info->flags */
  270. ANDI32 r11, r10, _TIF_WORK_MASK
  271. beq r11, r0, restore_all /* Nothing to do */
  272. BTBZ r1, r10, TIF_NEED_RESCHED, Lsignal_return
  273. /* Reschedule work */
  274. call schedule
  275. br ret_from_exception
  276. Lsignal_return:
  277. ANDI32 r1, r10, _TIF_SIGPENDING | _TIF_NOTIFY_RESUME
  278. beq r1, r0, restore_all
  279. mov r4, sp /* pt_regs */
  280. SAVE_SWITCH_STACK
  281. call do_notify_resume
  282. beq r2, r0, no_work_pending
  283. RESTORE_SWITCH_STACK
  284. /* prepare restart syscall here without leaving kernel */
  285. ldw r2, PT_R2(sp) /* reload syscall number in r2 */
  286. ldw r4, PT_R4(sp) /* reload syscall arguments r4-r9 */
  287. ldw r5, PT_R5(sp)
  288. ldw r6, PT_R6(sp)
  289. ldw r7, PT_R7(sp)
  290. ldw r8, PT_R8(sp)
  291. ldw r9, PT_R9(sp)
  292. br local_restart /* restart syscall */
  293. no_work_pending:
  294. RESTORE_SWITCH_STACK
  295. br ret_from_exception
  296. /***********************************************************************
  297. * Handle external interrupts.
  298. ***********************************************************************
  299. */
  300. /*
  301. * This is the generic interrupt handler (for all hardware interrupt
  302. * sources). It figures out the vector number and calls the appropriate
  303. * interrupt service routine directly.
  304. */
  305. external_interrupt:
  306. rdctl r12, ipending
  307. rdctl r9, ienable
  308. and r12, r12, r9
  309. /* skip if no interrupt is pending */
  310. beq r12, r0, ret_from_interrupt
  311. /*
  312. * Process an external hardware interrupt.
  313. */
  314. addi ea, ea, -4 /* re-issue the interrupted instruction */
  315. stw ea, PT_EA(sp)
  316. 2: movi r4, %lo(-1) /* Start from bit position 0,
  317. highest priority */
  318. /* This is the IRQ # for handler call */
  319. 1: andi r10, r12, 1 /* Isolate bit we are interested in */
  320. srli r12, r12, 1 /* shift count is costly without hardware
  321. multiplier */
  322. addi r4, r4, 1
  323. beq r10, r0, 1b
  324. mov r5, sp /* Setup pt_regs pointer for handler call */
  325. call do_IRQ
  326. rdctl r12, ipending /* check again if irq still pending */
  327. rdctl r9, ienable /* Isolate possible interrupts */
  328. and r12, r12, r9
  329. bne r12, r0, 2b
  330. /* br ret_from_interrupt */ /* fall through to ret_from_interrupt */
  331. ENTRY(ret_from_interrupt)
  332. ldw r1, PT_ESTATUS(sp) /* check if returning to kernel */
  333. TSTBNZ r1, r1, ESTATUS_EU, Luser_return
  334. #ifdef CONFIG_PREEMPTION
  335. GET_THREAD_INFO r1
  336. ldw r4, TI_PREEMPT_COUNT(r1)
  337. bne r4, r0, restore_all
  338. ldw r4, TI_FLAGS(r1) /* ? Need resched set */
  339. BTBZ r10, r4, TIF_NEED_RESCHED, restore_all
  340. ldw r4, PT_ESTATUS(sp) /* ? Interrupts off */
  341. andi r10, r4, ESTATUS_EPIE
  342. beq r10, r0, restore_all
  343. call preempt_schedule_irq
  344. #endif
  345. br restore_all
  346. /***********************************************************************
  347. * A few syscall wrappers
  348. ***********************************************************************
  349. */
  350. /*
  351. * int clone(unsigned long clone_flags, unsigned long newsp,
  352. * int __user * parent_tidptr, int __user * child_tidptr,
  353. * int tls_val)
  354. */
  355. ENTRY(sys_clone)
  356. SAVE_SWITCH_STACK
  357. subi sp, sp, 4 /* make space for tls pointer */
  358. stw r8, 0(sp) /* pass tls pointer (r8) via stack (5th argument) */
  359. call nios2_clone
  360. addi sp, sp, 4
  361. RESTORE_SWITCH_STACK
  362. ret
  363. ENTRY(sys_rt_sigreturn)
  364. SAVE_SWITCH_STACK
  365. mov r4, sp
  366. call do_rt_sigreturn
  367. RESTORE_SWITCH_STACK
  368. addi ra, ra, (end_translate_rc_and_ret - translate_rc_and_ret)
  369. ret
  370. /***********************************************************************
  371. * A few other wrappers and stubs
  372. ***********************************************************************
  373. */
  374. protection_exception_pte:
  375. rdctl r6, pteaddr
  376. slli r6, r6, 10
  377. call do_page_fault
  378. br ret_from_exception
  379. protection_exception_ba:
  380. rdctl r6, badaddr
  381. call do_page_fault
  382. br ret_from_exception
  383. protection_exception_instr:
  384. call handle_supervisor_instr
  385. br ret_from_exception
  386. handle_breakpoint:
  387. call breakpoint_c
  388. br ret_from_exception
  389. #ifdef CONFIG_NIOS2_ALIGNMENT_TRAP
  390. handle_unaligned:
  391. SAVE_SWITCH_STACK
  392. call handle_unaligned_c
  393. RESTORE_SWITCH_STACK
  394. br ret_from_exception
  395. #else
  396. handle_unaligned:
  397. call handle_unaligned_c
  398. br ret_from_exception
  399. #endif
  400. handle_illegal:
  401. call handle_illegal_c
  402. br ret_from_exception
  403. handle_diverror:
  404. call handle_diverror_c
  405. br ret_from_exception
  406. #ifdef CONFIG_KGDB
  407. handle_kgdb_breakpoint:
  408. call kgdb_breakpoint_c
  409. br ret_from_exception
  410. #endif
  411. handle_trap_1:
  412. call handle_trap_1_c
  413. br ret_from_exception
  414. handle_trap_2:
  415. call handle_trap_2_c
  416. br ret_from_exception
  417. handle_trap_3:
  418. handle_trap_reserved:
  419. call handle_trap_3_c
  420. br ret_from_exception
  421. /*
  422. * Beware - when entering resume, prev (the current task) is
  423. * in r4, next (the new task) is in r5, don't change these
  424. * registers.
  425. */
  426. ENTRY(resume)
  427. rdctl r7, status /* save thread status reg */
  428. stw r7, TASK_THREAD + THREAD_KPSR(r4)
  429. andi r7, r7, %lo(~STATUS_PIE) /* disable interrupts */
  430. wrctl status, r7
  431. SAVE_SWITCH_STACK
  432. stw sp, TASK_THREAD + THREAD_KSP(r4)/* save kernel stack pointer */
  433. ldw sp, TASK_THREAD + THREAD_KSP(r5)/* restore new thread stack */
  434. movia r24, _current_thread /* save thread */
  435. GET_THREAD_INFO r1
  436. stw r1, 0(r24)
  437. RESTORE_SWITCH_STACK
  438. ldw r7, TASK_THREAD + THREAD_KPSR(r5)/* restore thread status reg */
  439. wrctl status, r7
  440. ret
  441. ENTRY(ret_from_fork)
  442. call schedule_tail
  443. br ret_from_exception
  444. ENTRY(ret_from_kernel_thread)
  445. call schedule_tail
  446. mov r4,r17 /* arg */
  447. callr r16 /* function */
  448. br ret_from_exception
  449. /*
  450. * Kernel user helpers.
  451. *
  452. * Each segment is 64-byte aligned and will be mapped to the <User space>.
  453. * New segments (if ever needed) must be added after the existing ones.
  454. * This mechanism should be used only for things that are really small and
  455. * justified, and not be abused freely.
  456. *
  457. */
  458. /* Filling pads with undefined instructions. */
  459. .macro kuser_pad sym size
  460. .if ((. - \sym) & 3)
  461. .rept (4 - (. - \sym) & 3)
  462. .byte 0
  463. .endr
  464. .endif
  465. .rept ((\size - (. - \sym)) / 4)
  466. .word 0xdeadbeef
  467. .endr
  468. .endm
  469. .align 6
  470. .globl __kuser_helper_start
  471. __kuser_helper_start:
  472. __kuser_helper_version: /* @ 0x1000 */
  473. .word ((__kuser_helper_end - __kuser_helper_start) >> 6)
  474. __kuser_cmpxchg: /* @ 0x1004 */
  475. /*
  476. * r4 pointer to exchange variable
  477. * r5 old value
  478. * r6 new value
  479. */
  480. cmpxchg_ldw:
  481. ldw r2, 0(r4) /* load current value */
  482. sub r2, r2, r5 /* compare with old value */
  483. bne r2, zero, cmpxchg_ret
  484. /* We had a match, store the new value */
  485. cmpxchg_stw:
  486. stw r6, 0(r4)
  487. cmpxchg_ret:
  488. ret
  489. kuser_pad __kuser_cmpxchg, 64
  490. .globl __kuser_sigtramp
  491. __kuser_sigtramp:
  492. movi r2, __NR_rt_sigreturn
  493. trap
  494. kuser_pad __kuser_sigtramp, 64
  495. .globl __kuser_helper_end
  496. __kuser_helper_end: