entry.S 10 KB

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