entry.S 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. #if defined(CONFIG_KGDB)
  55. NMI_VEC = 0x1c0 ! Must catch early for debounce
  56. #endif
  57. /* Offsets to the stack */
  58. OFF_R0 = 0 /* Return value. New ABI also arg4 */
  59. OFF_R1 = 4 /* New ABI: arg5 */
  60. OFF_R2 = 8 /* New ABI: arg6 */
  61. OFF_R3 = 12 /* New ABI: syscall_nr */
  62. OFF_R4 = 16 /* New ABI: arg0 */
  63. OFF_R5 = 20 /* New ABI: arg1 */
  64. OFF_R6 = 24 /* New ABI: arg2 */
  65. OFF_R7 = 28 /* New ABI: arg3 */
  66. OFF_SP = (15*4)
  67. OFF_PC = (16*4)
  68. OFF_SR = (16*4+8)
  69. OFF_TRA = (16*4+6*4)
  70. #define k0 r0
  71. #define k1 r1
  72. #define k2 r2
  73. #define k3 r3
  74. #define k4 r4
  75. #define g_imask r6 /* r6_bank1 */
  76. #define k_g_imask r6_bank /* r6_bank1 */
  77. #define current r7 /* r7_bank1 */
  78. #include <asm/entry-macros.S>
  79. /*
  80. * Kernel mode register usage:
  81. * k0 scratch
  82. * k1 scratch
  83. * k2 scratch (Exception code)
  84. * k3 scratch (Return address)
  85. * k4 scratch
  86. * k5 reserved
  87. * k6 Global Interrupt Mask (0--15 << 4)
  88. * k7 CURRENT_THREAD_INFO (pointer to current thread info)
  89. */
  90. !
  91. ! TLB Miss / Initial Page write exception handling
  92. ! _and_
  93. ! TLB hits, but the access violate the protection.
  94. ! It can be valid access, such as stack grow and/or C-O-W.
  95. !
  96. !
  97. ! Find the pmd/pte entry and loadtlb
  98. ! If it's not found, cause address error (SEGV)
  99. !
  100. ! Although this could be written in assembly language (and it'd be faster),
  101. ! this first version depends *much* on C implementation.
  102. !
  103. #if defined(CONFIG_MMU)
  104. .align 2
  105. ENTRY(tlb_miss_load)
  106. bra call_dpf
  107. mov #0, r5
  108. .align 2
  109. ENTRY(tlb_miss_store)
  110. bra call_dpf
  111. mov #1, r5
  112. .align 2
  113. ENTRY(initial_page_write)
  114. bra call_dpf
  115. mov #1, r5
  116. .align 2
  117. ENTRY(tlb_protection_violation_load)
  118. bra call_dpf
  119. mov #0, r5
  120. .align 2
  121. ENTRY(tlb_protection_violation_store)
  122. bra call_dpf
  123. mov #1, r5
  124. call_dpf:
  125. mov.l 1f, r0
  126. mov r5, r8
  127. mov.l @r0, r6
  128. mov r6, r9
  129. mov.l 2f, r0
  130. sts pr, r10
  131. jsr @r0
  132. mov r15, r4
  133. !
  134. tst r0, r0
  135. bf/s 0f
  136. lds r10, pr
  137. rts
  138. nop
  139. 0: mov.l 3f, r0
  140. mov r9, r6
  141. mov r8, r5
  142. jmp @r0
  143. mov r15, r4
  144. .align 2
  145. 1: .long MMU_TEA
  146. 2: .long __do_page_fault
  147. 3: .long do_page_fault
  148. .align 2
  149. ENTRY(address_error_load)
  150. bra call_dae
  151. mov #0,r5 ! writeaccess = 0
  152. .align 2
  153. ENTRY(address_error_store)
  154. bra call_dae
  155. mov #1,r5 ! writeaccess = 1
  156. .align 2
  157. call_dae:
  158. mov.l 1f, r0
  159. mov.l @r0, r6 ! address
  160. mov.l 2f, r0
  161. jmp @r0
  162. mov r15, r4 ! regs
  163. .align 2
  164. 1: .long MMU_TEA
  165. 2: .long do_address_error
  166. #endif /* CONFIG_MMU */
  167. #if defined(CONFIG_SH_STANDARD_BIOS)
  168. /* Unwind the stack and jmp to the debug entry */
  169. ENTRY(sh_bios_handler)
  170. mov.l 1f, r8
  171. bsr restore_regs
  172. nop
  173. lds k2, pr ! restore pr
  174. mov k4, r15
  175. !
  176. mov.l 2f, k0
  177. mov.l @k0, k0
  178. jmp @k0
  179. ldc k3, ssr
  180. .align 2
  181. 1: .long 0x300000f0
  182. 2: .long gdb_vbr_vector
  183. #endif /* CONFIG_SH_STANDARD_BIOS */
  184. ! restore_regs()
  185. ! - restore r0, r1, r2, r3, r4, r5, r6, r7 from the stack
  186. ! - switch bank
  187. ! - restore r8, r9, r10, r11, r12, r13, r14, r15 from the stack
  188. ! - restore spc, pr*, ssr, gbr, mach, macl, skip default tra
  189. ! k2 returns original pr
  190. ! k3 returns original sr
  191. ! k4 returns original stack pointer
  192. ! r8 passes SR bitmask, overwritten with restored data on return
  193. ! r9 trashed
  194. ! BL=0 on entry, on exit BL=1 (depending on r8).
  195. ENTRY(restore_regs)
  196. mov.l @r15+, r0
  197. mov.l @r15+, r1
  198. mov.l @r15+, r2
  199. mov.l @r15+, r3
  200. mov.l @r15+, r4
  201. mov.l @r15+, r5
  202. mov.l @r15+, r6
  203. mov.l @r15+, r7
  204. !
  205. stc sr, r9
  206. or r8, r9
  207. ldc r9, sr
  208. !
  209. mov.l @r15+, r8
  210. mov.l @r15+, r9
  211. mov.l @r15+, r10
  212. mov.l @r15+, r11
  213. mov.l @r15+, r12
  214. mov.l @r15+, r13
  215. mov.l @r15+, r14
  216. mov.l @r15+, k4 ! original stack pointer
  217. ldc.l @r15+, spc
  218. mov.l @r15+, k2 ! original PR
  219. mov.l @r15+, k3 ! original SR
  220. ldc.l @r15+, gbr
  221. lds.l @r15+, mach
  222. lds.l @r15+, macl
  223. rts
  224. add #4, r15 ! Skip syscall number
  225. restore_all:
  226. mov.l 7f, r8
  227. bsr restore_regs
  228. nop
  229. lds k2, pr ! restore pr
  230. !
  231. #ifdef CONFIG_SH_DSP
  232. mov.l @r15+, k0 ! DSP mode marker
  233. mov.l 5f, k1
  234. cmp/eq k0, k1 ! Do we have a DSP stack frame?
  235. bf skip_restore
  236. stc sr, k0 ! Enable CPU DSP mode
  237. or k1, k0 ! (within kernel it may be disabled)
  238. ldc k0, sr
  239. mov r2, k0 ! Backup r2
  240. ! Restore DSP registers from stack
  241. mov r15, r2
  242. movs.l @r2+, a1
  243. movs.l @r2+, a0g
  244. movs.l @r2+, a1g
  245. movs.l @r2+, m0
  246. movs.l @r2+, m1
  247. mov r2, r15
  248. lds.l @r15+, a0
  249. lds.l @r15+, x0
  250. lds.l @r15+, x1
  251. lds.l @r15+, y0
  252. lds.l @r15+, y1
  253. lds.l @r15+, dsr
  254. ldc.l @r15+, rs
  255. ldc.l @r15+, re
  256. ldc.l @r15+, mod
  257. mov k0, r2 ! Restore r2
  258. skip_restore:
  259. #endif
  260. !
  261. ! Calculate new SR value
  262. mov k3, k2 ! original SR value
  263. mov #0xf0, k1
  264. extu.b k1, k1
  265. not k1, k1
  266. and k1, k2 ! Mask original SR value
  267. !
  268. mov k3, k0 ! Calculate IMASK-bits
  269. shlr2 k0
  270. and #0x3c, k0
  271. cmp/eq #0x3c, k0
  272. bt/s 6f
  273. shll2 k0
  274. mov g_imask, k0
  275. !
  276. 6: or k0, k2 ! Set the IMASK-bits
  277. ldc k2, ssr
  278. !
  279. #if defined(CONFIG_KGDB)
  280. ! Clear in_nmi
  281. mov.l 6f, k0
  282. mov #0, k1
  283. mov.b k1, @k0
  284. #endif
  285. mov k4, r15
  286. rte
  287. nop
  288. .align 2
  289. 5: .long 0x00001000 ! DSP
  290. #ifdef CONFIG_KGDB
  291. 6: .long in_nmi
  292. #endif
  293. 7: .long 0x30000000
  294. ! common exception handler
  295. #include "../../entry-common.S"
  296. ! Exception Vector Base
  297. !
  298. ! Should be aligned page boundary.
  299. !
  300. .balign 4096,0,4096
  301. ENTRY(vbr_base)
  302. .long 0
  303. !
  304. ! 0x100: General exception vector
  305. !
  306. .balign 256,0,256
  307. general_exception:
  308. #ifndef CONFIG_CPU_SUBTYPE_SHX3
  309. bra handle_exception
  310. sts pr, k3 ! save original pr value in k3
  311. #else
  312. mov.l 1f, k4
  313. mov.l @k4, k4
  314. ! Is EXPEVT larger than 0x800?
  315. mov #0x8, k0
  316. shll8 k0
  317. cmp/hs k0, k4
  318. bf 0f
  319. ! then add 0x580 (k2 is 0xd80 or 0xda0)
  320. mov #0x58, k0
  321. shll2 k0
  322. shll2 k0
  323. add k0, k4
  324. 0:
  325. ! Setup stack and save DSP context (k0 contains original r15 on return)
  326. bsr prepare_stack_save_dsp
  327. nop
  328. ! Save registers / Switch to bank 0
  329. mov k4, k2 ! keep vector in k2
  330. mov.l 1f, k4 ! SR bits to clear in k4
  331. bsr save_regs ! needs original pr value in k3
  332. nop
  333. bra handle_exception_special
  334. nop
  335. .align 2
  336. 1: .long EXPEVT
  337. #endif
  338. ! prepare_stack_save_dsp()
  339. ! - roll back gRB
  340. ! - switch to kernel stack
  341. ! - save DSP
  342. ! k0 returns original sp (after roll back)
  343. ! k1 trashed
  344. ! k2 trashed
  345. prepare_stack_save_dsp:
  346. #ifdef CONFIG_GUSA
  347. ! Check for roll back gRB (User and Kernel)
  348. mov r15, k0
  349. shll k0
  350. bf/s 1f
  351. shll k0
  352. bf/s 1f
  353. stc spc, k1
  354. stc r0_bank, k0
  355. cmp/hs k0, k1 ! test k1 (saved PC) >= k0 (saved r0)
  356. bt/s 2f
  357. stc r1_bank, k1
  358. add #-2, k0
  359. add r15, k0
  360. ldc k0, spc ! PC = saved r0 + r15 - 2
  361. 2: mov k1, r15 ! SP = r1
  362. 1:
  363. #endif
  364. ! Switch to kernel stack if needed
  365. stc ssr, k0 ! Is it from kernel space?
  366. shll k0 ! Check MD bit (bit30) by shifting it into...
  367. shll k0 ! ...the T bit
  368. bt/s 1f ! It's a kernel to kernel transition.
  369. mov r15, k0 ! save original stack to k0
  370. /* User space to kernel */
  371. mov #(THREAD_SIZE >> 10), k1
  372. shll8 k1 ! k1 := THREAD_SIZE
  373. shll2 k1
  374. add current, k1
  375. mov k1, r15 ! change to kernel stack
  376. !
  377. 1:
  378. #ifdef CONFIG_SH_DSP
  379. ! Save DSP context if needed
  380. stc sr, k1
  381. mov #0x10, k2
  382. shll8 k2 ! DSP=1 (0x00001000)
  383. tst k2, k1 ! Check if in DSP mode (passed in k2)
  384. bt/s skip_save
  385. mov #0, k1 ! Set marker for no stack frame
  386. mov k2, k1 ! Save has-frame marker
  387. ! Save DSP registers on stack
  388. stc.l mod, @-r15
  389. stc.l re, @-r15
  390. stc.l rs, @-r15
  391. sts.l dsr, @-r15
  392. sts.l y1, @-r15
  393. sts.l y0, @-r15
  394. sts.l x1, @-r15
  395. sts.l x0, @-r15
  396. sts.l a0, @-r15
  397. ! GAS is broken, does not generate correct "movs.l Ds,@-As" instr.
  398. ! FIXME: Make sure that this is still the case with newer toolchains,
  399. ! as we're not at all interested in supporting ancient toolchains at
  400. ! this point. -- PFM.
  401. mov r15, k2
  402. .word 0xf653 ! movs.l a1, @-r2
  403. .word 0xf6f3 ! movs.l a0g, @-r2
  404. .word 0xf6d3 ! movs.l a1g, @-r2
  405. .word 0xf6c3 ! movs.l m0, @-r2
  406. .word 0xf6e3 ! movs.l m1, @-r2
  407. mov k2, r15
  408. skip_save:
  409. mov.l k1, @-r15 ! Push DSP mode marker onto stack
  410. #endif
  411. rts
  412. nop
  413. !
  414. ! 0x400: Instruction and Data TLB miss exception vector
  415. !
  416. .balign 1024,0,1024
  417. tlb_miss:
  418. sts pr, k3 ! save original pr value in k3
  419. handle_exception:
  420. mova exception_data, k0
  421. ! Setup stack and save DSP context (k0 contains original r15 on return)
  422. bsr prepare_stack_save_dsp
  423. PREF(k0)
  424. ! Save registers / Switch to bank 0
  425. mov.l 5f, k2 ! vector register address
  426. mov.l 1f, k4 ! SR bits to clear in k4
  427. bsr save_regs ! needs original pr value in k3
  428. mov.l @k2, k2 ! read out vector and keep in k2
  429. handle_exception_special:
  430. ! Setup return address and jump to exception handler
  431. mov.l 7f, r9 ! fetch return address
  432. stc r2_bank, r0 ! k2 (vector)
  433. mov.l 6f, r10
  434. shlr2 r0
  435. shlr r0
  436. mov.l @(r0, r10), r10
  437. jmp @r10
  438. lds r9, pr ! put return address in pr
  439. .align L1_CACHE_SHIFT
  440. ! save_regs()
  441. ! - save default tra, macl, mach, gbr, ssr, pr* and spc on the stack
  442. ! - save r15*, r14, r13, r12, r11, r10, r9, r8 on the stack
  443. ! - switch bank
  444. ! - save r7, r6, r5, r4, r3, r2, r1, r0 on the stack
  445. ! k0 contains original stack pointer*
  446. ! k1 trashed
  447. ! k3 passes original pr*
  448. ! k4 passes SR bitmask
  449. ! BL=1 on entry, on exit BL=0.
  450. ENTRY(save_regs)
  451. mov #-1, r1
  452. mov.l k1, @-r15 ! set TRA (default: -1)
  453. sts.l macl, @-r15
  454. sts.l mach, @-r15
  455. stc.l gbr, @-r15
  456. stc.l ssr, @-r15
  457. mov.l k3, @-r15 ! original pr in k3
  458. stc.l spc, @-r15
  459. mov.l k0, @-r15 ! original stack pointer in k0
  460. mov.l r14, @-r15
  461. mov.l r13, @-r15
  462. mov.l r12, @-r15
  463. mov.l r11, @-r15
  464. mov.l r10, @-r15
  465. mov.l r9, @-r15
  466. mov.l r8, @-r15
  467. mov.l 0f, k3 ! SR bits to set in k3
  468. ! fall-through
  469. ! save_low_regs()
  470. ! - modify SR for bank switch
  471. ! - save r7, r6, r5, r4, r3, r2, r1, r0 on the stack
  472. ! k3 passes bits to set in SR
  473. ! k4 passes bits to clear in SR
  474. ENTRY(save_low_regs)
  475. stc sr, r8
  476. or k3, r8
  477. and k4, r8
  478. ldc r8, sr
  479. mov.l r7, @-r15
  480. mov.l r6, @-r15
  481. mov.l r5, @-r15
  482. mov.l r4, @-r15
  483. mov.l r3, @-r15
  484. mov.l r2, @-r15
  485. mov.l r1, @-r15
  486. rts
  487. mov.l r0, @-r15
  488. !
  489. ! 0x600: Interrupt / NMI vector
  490. !
  491. .balign 512,0,512
  492. ENTRY(handle_interrupt)
  493. #if defined(CONFIG_KGDB)
  494. mov.l 2f, k2
  495. ! Debounce (filter nested NMI)
  496. mov.l @k2, k0
  497. mov.l 9f, k1
  498. cmp/eq k1, k0
  499. bf 11f
  500. mov.l 10f, k1
  501. tas.b @k1
  502. bt 11f
  503. rte
  504. nop
  505. .align 2
  506. 9: .long NMI_VEC
  507. 10: .long in_nmi
  508. 11:
  509. #endif /* defined(CONFIG_KGDB) */
  510. sts pr, k3 ! save original pr value in k3
  511. mova exception_data, k0
  512. ! Setup stack and save DSP context (k0 contains original r15 on return)
  513. bsr prepare_stack_save_dsp
  514. PREF(k0)
  515. ! Save registers / Switch to bank 0
  516. mov.l 1f, k4 ! SR bits to clear in k4
  517. bsr save_regs ! needs original pr value in k3
  518. mov #-1, k2 ! default vector kept in k2
  519. ! Setup return address and jump to do_IRQ
  520. mov.l 4f, r9 ! fetch return address
  521. lds r9, pr ! put return address in pr
  522. mov.l 2f, r4
  523. mov.l 3f, r9
  524. mov.l @r4, r4 ! pass INTEVT vector as arg0
  525. jmp @r9
  526. mov r15, r5 ! pass saved registers as arg1
  527. ENTRY(exception_none)
  528. rts
  529. nop
  530. .align L1_CACHE_SHIFT
  531. exception_data:
  532. 0: .long 0x000080f0 ! FD=1, IMASK=15
  533. 1: .long 0xcfffffff ! RB=0, BL=0
  534. 2: .long INTEVT
  535. 3: .long do_IRQ
  536. 4: .long ret_from_irq
  537. 5: .long EXPEVT
  538. 6: .long exception_handling_table
  539. 7: .long ret_from_exception