entry.S 11 KB

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