entry.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * arch/sh/kernel/cpu/sh3/entry.S
  3. *
  4. * Copyright (C) 1999, 2000, 2002 Niibe Yutaka
  5. * Copyright (C) 2003 - 2012 Paul Mundt
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. */
  11. #include <linux/sys.h>
  12. #include <linux/errno.h>
  13. #include <linux/linkage.h>
  14. #include <asm/asm-offsets.h>
  15. #include <asm/thread_info.h>
  16. #include <asm/unistd.h>
  17. #include <cpu/mmu_context.h>
  18. #include <asm/page.h>
  19. #include <asm/cache.h>
  20. #include <asm/thread_info.h>
  21. ! NOTE:
  22. ! GNU as (as of 2.9.1) changes bf/s into bt/s and bra, when the address
  23. ! to be jumped is too far, but it causes illegal slot exception.
  24. /*
  25. * entry.S contains the system-call and fault low-level handling routines.
  26. * This also contains the timer-interrupt handler, as well as all interrupts
  27. * and faults that can result in a task-switch.
  28. *
  29. * NOTE: This code handles signal-recognition, which happens every time
  30. * after a timer-interrupt and after each system call.
  31. *
  32. * NOTE: This code uses a convention that instructions in the delay slot
  33. * of a transfer-control instruction are indented by an extra space, thus:
  34. *
  35. * jmp @k0 ! control-transfer instruction
  36. * ldc k1, ssr ! delay slot
  37. *
  38. * Stack layout in 'ret_from_syscall':
  39. * ptrace needs to have all regs on the stack.
  40. * if the order here is changed, it needs to be
  41. * updated in ptrace.c and ptrace.h
  42. *
  43. * r0
  44. * ...
  45. * r15 = stack pointer
  46. * spc
  47. * pr
  48. * ssr
  49. * gbr
  50. * mach
  51. * macl
  52. * syscall #
  53. *
  54. */
  55. /* Offsets to the stack */
  56. OFF_R0 = 0 /* Return value. New ABI also arg4 */
  57. OFF_R1 = 4 /* New ABI: arg5 */
  58. OFF_R2 = 8 /* New ABI: arg6 */
  59. OFF_R3 = 12 /* New ABI: syscall_nr */
  60. OFF_R4 = 16 /* New ABI: arg0 */
  61. OFF_R5 = 20 /* New ABI: arg1 */
  62. OFF_R6 = 24 /* New ABI: arg2 */
  63. OFF_R7 = 28 /* New ABI: arg3 */
  64. OFF_SP = (15*4)
  65. OFF_PC = (16*4)
  66. OFF_SR = (16*4+8)
  67. OFF_TRA = (16*4+6*4)
  68. #define k0 r0
  69. #define k1 r1
  70. #define k2 r2
  71. #define k3 r3
  72. #define k4 r4
  73. #define g_imask r6 /* r6_bank1 */
  74. #define k_g_imask r6_bank /* r6_bank1 */
  75. #define current r7 /* r7_bank1 */
  76. #include <asm/entry-macros.S>
  77. /*
  78. * Kernel mode register usage:
  79. * k0 scratch
  80. * k1 scratch
  81. * k2 scratch (Exception code)
  82. * k3 scratch (Return address)
  83. * k4 scratch
  84. * k5 reserved
  85. * k6 Global Interrupt Mask (0--15 << 4)
  86. * k7 CURRENT_THREAD_INFO (pointer to current thread info)
  87. */
  88. !
  89. ! TLB Miss / Initial Page write exception handling
  90. ! _and_
  91. ! TLB hits, but the access violate the protection.
  92. ! It can be valid access, such as stack grow and/or C-O-W.
  93. !
  94. !
  95. ! Find the pmd/pte entry and loadtlb
  96. ! If it's not found, cause address error (SEGV)
  97. !
  98. ! Although this could be written in assembly language (and it'd be faster),
  99. ! this first version depends *much* on C implementation.
  100. !
  101. #if defined(CONFIG_MMU)
  102. .align 2
  103. ENTRY(tlb_miss_load)
  104. bra call_handle_tlbmiss
  105. mov #0, r5
  106. .align 2
  107. ENTRY(tlb_miss_store)
  108. bra call_handle_tlbmiss
  109. mov #FAULT_CODE_WRITE, r5
  110. .align 2
  111. ENTRY(initial_page_write)
  112. bra call_handle_tlbmiss
  113. mov #FAULT_CODE_INITIAL, r5
  114. .align 2
  115. ENTRY(tlb_protection_violation_load)
  116. bra call_do_page_fault
  117. mov #FAULT_CODE_PROT, r5
  118. .align 2
  119. ENTRY(tlb_protection_violation_store)
  120. bra call_do_page_fault
  121. mov #(FAULT_CODE_PROT | FAULT_CODE_WRITE), r5
  122. call_handle_tlbmiss:
  123. mov.l 1f, r0
  124. mov r5, r8
  125. mov.l @r0, r6
  126. mov.l 2f, r0
  127. sts pr, r10
  128. jsr @r0
  129. mov r15, r4
  130. !
  131. tst r0, r0
  132. bf/s 0f
  133. lds r10, pr
  134. rts
  135. nop
  136. 0:
  137. mov r8, r5
  138. call_do_page_fault:
  139. mov.l 1f, r0
  140. mov.l @r0, r6
  141. mov.l 3f, r0
  142. mov.l 4f, r1
  143. mov r15, r4
  144. jmp @r0
  145. lds r1, pr
  146. .align 2
  147. 1: .long MMU_TEA
  148. 2: .long handle_tlbmiss
  149. 3: .long do_page_fault
  150. 4: .long ret_from_exception
  151. .align 2
  152. ENTRY(address_error_load)
  153. bra call_dae
  154. mov #0,r5 ! writeaccess = 0
  155. .align 2
  156. ENTRY(address_error_store)
  157. bra call_dae
  158. mov #1,r5 ! writeaccess = 1
  159. .align 2
  160. call_dae:
  161. mov.l 1f, r0
  162. mov.l @r0, r6 ! address
  163. mov.l 2f, r0
  164. jmp @r0
  165. mov r15, r4 ! regs
  166. .align 2
  167. 1: .long MMU_TEA
  168. 2: .long do_address_error
  169. #endif /* CONFIG_MMU */
  170. #if defined(CONFIG_SH_STANDARD_BIOS)
  171. /* Unwind the stack and jmp to the debug entry */
  172. ENTRY(sh_bios_handler)
  173. mov.l 1f, r8
  174. bsr restore_regs
  175. nop
  176. lds k2, pr ! restore pr
  177. mov k4, r15
  178. !
  179. mov.l 2f, k0
  180. mov.l @k0, k0
  181. jmp @k0
  182. ldc k3, ssr
  183. .align 2
  184. 1: .long 0x300000f0
  185. 2: .long gdb_vbr_vector
  186. #endif /* CONFIG_SH_STANDARD_BIOS */
  187. ! restore_regs()
  188. ! - restore r0, r1, r2, r3, r4, r5, r6, r7 from the stack
  189. ! - switch bank
  190. ! - restore r8, r9, r10, r11, r12, r13, r14, r15 from the stack
  191. ! - restore spc, pr*, ssr, gbr, mach, macl, skip default tra
  192. ! k2 returns original pr
  193. ! k3 returns original sr
  194. ! k4 returns original stack pointer
  195. ! r8 passes SR bitmask, overwritten with restored data on return
  196. ! r9 trashed
  197. ! BL=0 on entry, on exit BL=1 (depending on r8).
  198. ENTRY(restore_regs)
  199. mov.l @r15+, r0
  200. mov.l @r15+, r1
  201. mov.l @r15+, r2
  202. mov.l @r15+, r3
  203. mov.l @r15+, r4
  204. mov.l @r15+, r5
  205. mov.l @r15+, r6
  206. mov.l @r15+, r7
  207. !
  208. stc sr, r9
  209. or r8, r9
  210. ldc r9, sr
  211. !
  212. mov.l @r15+, r8
  213. mov.l @r15+, r9
  214. mov.l @r15+, r10
  215. mov.l @r15+, r11
  216. mov.l @r15+, r12
  217. mov.l @r15+, r13
  218. mov.l @r15+, r14
  219. mov.l @r15+, k4 ! original stack pointer
  220. ldc.l @r15+, spc
  221. mov.l @r15+, k2 ! original PR
  222. mov.l @r15+, k3 ! original SR
  223. ldc.l @r15+, gbr
  224. lds.l @r15+, mach
  225. lds.l @r15+, macl
  226. rts
  227. add #4, r15 ! Skip syscall number
  228. restore_all:
  229. mov.l 7f, r8
  230. bsr restore_regs
  231. nop
  232. lds k2, pr ! restore pr
  233. !
  234. ! Calculate new SR value
  235. mov k3, k2 ! original SR value
  236. mov #0xfffffff0, k1
  237. extu.b k1, k1
  238. not k1, k1
  239. and k1, k2 ! Mask original SR value
  240. !
  241. mov k3, k0 ! Calculate IMASK-bits
  242. shlr2 k0
  243. and #0x3c, k0
  244. cmp/eq #0x3c, k0
  245. bt/s 6f
  246. shll2 k0
  247. mov g_imask, k0
  248. !
  249. 6: or k0, k2 ! Set the IMASK-bits
  250. ldc k2, ssr
  251. !
  252. mov k4, r15
  253. rte
  254. nop
  255. .align 2
  256. 5: .long 0x00001000 ! DSP
  257. 7: .long 0x30000000
  258. ! common exception handler
  259. #include "../../entry-common.S"
  260. ! Exception Vector Base
  261. !
  262. ! Should be aligned page boundary.
  263. !
  264. .balign 4096,0,4096
  265. ENTRY(vbr_base)
  266. .long 0
  267. !
  268. ! 0x100: General exception vector
  269. !
  270. .balign 256,0,256
  271. general_exception:
  272. bra handle_exception
  273. sts pr, k3 ! save original pr value in k3
  274. ! prepare_stack()
  275. ! - roll back gRB
  276. ! - switch to kernel stack
  277. ! k0 returns original sp (after roll back)
  278. ! k1 trashed
  279. ! k2 trashed
  280. prepare_stack:
  281. #ifdef CONFIG_GUSA
  282. ! Check for roll back gRB (User and Kernel)
  283. mov r15, k0
  284. shll k0
  285. bf/s 1f
  286. shll k0
  287. bf/s 1f
  288. stc spc, k1
  289. stc r0_bank, k0
  290. cmp/hs k0, k1 ! test k1 (saved PC) >= k0 (saved r0)
  291. bt/s 2f
  292. stc r1_bank, k1
  293. add #-2, k0
  294. add r15, k0
  295. ldc k0, spc ! PC = saved r0 + r15 - 2
  296. 2: mov k1, r15 ! SP = r1
  297. 1:
  298. #endif
  299. ! Switch to kernel stack if needed
  300. stc ssr, k0 ! Is it from kernel space?
  301. shll k0 ! Check MD bit (bit30) by shifting it into...
  302. shll k0 ! ...the T bit
  303. bt/s 1f ! It's a kernel to kernel transition.
  304. mov r15, k0 ! save original stack to k0
  305. /* User space to kernel */
  306. mov #(THREAD_SIZE >> 10), k1
  307. shll8 k1 ! k1 := THREAD_SIZE
  308. shll2 k1
  309. add current, k1
  310. mov k1, r15 ! change to kernel stack
  311. !
  312. 1:
  313. rts
  314. nop
  315. !
  316. ! 0x400: Instruction and Data TLB miss exception vector
  317. !
  318. .balign 1024,0,1024
  319. tlb_miss:
  320. sts pr, k3 ! save original pr value in k3
  321. handle_exception:
  322. mova exception_data, k0
  323. ! Setup stack and save DSP context (k0 contains original r15 on return)
  324. bsr prepare_stack
  325. PREF(k0)
  326. ! Save registers / Switch to bank 0
  327. mov.l 5f, k2 ! vector register address
  328. mov.l 1f, k4 ! SR bits to clear in k4
  329. bsr save_regs ! needs original pr value in k3
  330. mov.l @k2, k2 ! read out vector and keep in k2
  331. handle_exception_special:
  332. setup_frame_reg
  333. ! Setup return address and jump to exception handler
  334. mov.l 7f, r9 ! fetch return address
  335. stc r2_bank, r0 ! k2 (vector)
  336. mov.l 6f, r10
  337. shlr2 r0
  338. shlr r0
  339. mov.l @(r0, r10), r10
  340. jmp @r10
  341. lds r9, pr ! put return address in pr
  342. .align L1_CACHE_SHIFT
  343. ! save_regs()
  344. ! - save default tra, macl, mach, gbr, ssr, pr* and spc on the stack
  345. ! - save r15*, r14, r13, r12, r11, r10, r9, r8 on the stack
  346. ! - switch bank
  347. ! - save r7, r6, r5, r4, r3, r2, r1, r0 on the stack
  348. ! k0 contains original stack pointer*
  349. ! k1 trashed
  350. ! k3 passes original pr*
  351. ! k4 passes SR bitmask
  352. ! BL=1 on entry, on exit BL=0.
  353. ENTRY(save_regs)
  354. mov #-1, r1
  355. mov.l k1, @-r15 ! set TRA (default: -1)
  356. sts.l macl, @-r15
  357. sts.l mach, @-r15
  358. stc.l gbr, @-r15
  359. stc.l ssr, @-r15
  360. mov.l k3, @-r15 ! original pr in k3
  361. stc.l spc, @-r15
  362. mov.l k0, @-r15 ! original stack pointer in k0
  363. mov.l r14, @-r15
  364. mov.l r13, @-r15
  365. mov.l r12, @-r15
  366. mov.l r11, @-r15
  367. mov.l r10, @-r15
  368. mov.l r9, @-r15
  369. mov.l r8, @-r15
  370. mov.l 0f, k3 ! SR bits to set in k3
  371. ! fall-through
  372. ! save_low_regs()
  373. ! - modify SR for bank switch
  374. ! - save r7, r6, r5, r4, r3, r2, r1, r0 on the stack
  375. ! k3 passes bits to set in SR
  376. ! k4 passes bits to clear in SR
  377. ENTRY(save_low_regs)
  378. stc sr, r8
  379. or k3, r8
  380. and k4, r8
  381. ldc r8, sr
  382. mov.l r7, @-r15
  383. mov.l r6, @-r15
  384. mov.l r5, @-r15
  385. mov.l r4, @-r15
  386. mov.l r3, @-r15
  387. mov.l r2, @-r15
  388. mov.l r1, @-r15
  389. rts
  390. mov.l r0, @-r15
  391. !
  392. ! 0x600: Interrupt / NMI vector
  393. !
  394. .balign 512,0,512
  395. ENTRY(handle_interrupt)
  396. sts pr, k3 ! save original pr value in k3
  397. mova exception_data, k0
  398. ! Setup stack and save DSP context (k0 contains original r15 on return)
  399. bsr prepare_stack
  400. PREF(k0)
  401. ! Save registers / Switch to bank 0
  402. mov.l 1f, k4 ! SR bits to clear in k4
  403. bsr save_regs ! needs original pr value in k3
  404. mov #-1, k2 ! default vector kept in k2
  405. setup_frame_reg
  406. stc sr, r0 ! get status register
  407. shlr2 r0
  408. and #0x3c, r0
  409. cmp/eq #0x3c, r0
  410. bf 9f
  411. TRACE_IRQS_OFF
  412. 9:
  413. ! Setup return address and jump to do_IRQ
  414. mov.l 4f, r9 ! fetch return address
  415. lds r9, pr ! put return address in pr
  416. mov.l 2f, r4
  417. mov.l 3f, r9
  418. mov.l @r4, r4 ! pass INTEVT vector as arg0
  419. shlr2 r4
  420. shlr r4
  421. mov r4, r0 ! save vector->jmp table offset for later
  422. shlr2 r4 ! vector to IRQ# conversion
  423. add #-0x10, r4
  424. cmp/pz r4 ! is it a valid IRQ?
  425. bt 10f
  426. /*
  427. * We got here as a result of taking the INTEVT path for something
  428. * that isn't a valid hard IRQ, therefore we bypass the do_IRQ()
  429. * path and special case the event dispatch instead. This is the
  430. * expected path for the NMI (and any other brilliantly implemented
  431. * exception), which effectively wants regular exception dispatch
  432. * but is unfortunately reported through INTEVT rather than
  433. * EXPEVT. Grr.
  434. */
  435. mov.l 6f, r9
  436. mov.l @(r0, r9), r9
  437. jmp @r9
  438. mov r15, r8 ! trap handlers take saved regs in r8
  439. 10:
  440. jmp @r9 ! Off to do_IRQ() we go.
  441. mov r15, r5 ! pass saved registers as arg1
  442. ENTRY(exception_none)
  443. rts
  444. nop
  445. .align L1_CACHE_SHIFT
  446. exception_data:
  447. 0: .long 0x000080f0 ! FD=1, IMASK=15
  448. 1: .long 0xcfffffff ! RB=0, BL=0
  449. 2: .long INTEVT
  450. 3: .long do_IRQ
  451. 4: .long ret_from_irq
  452. 5: .long EXPEVT
  453. 6: .long exception_handling_table
  454. 7: .long ret_from_exception