entry.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. ! NOTE:
  20. ! GNU as (as of 2.9.1) changes bf/s into bt/s and bra, when the address
  21. ! to be jumped is too far, but it causes illegal slot exception.
  22. /*
  23. * entry.S contains the system-call and fault low-level handling routines.
  24. * This also contains the timer-interrupt handler, as well as all interrupts
  25. * and faults that can result in a task-switch.
  26. *
  27. * NOTE: This code handles signal-recognition, which happens every time
  28. * after a timer-interrupt and after each system call.
  29. *
  30. * NOTE: This code uses a convention that instructions in the delay slot
  31. * of a transfer-control instruction are indented by an extra space, thus:
  32. *
  33. * jmp @k0 ! control-transfer instruction
  34. * ldc k1, ssr ! delay slot
  35. *
  36. * Stack layout in 'ret_from_syscall':
  37. * ptrace needs to have all regs on the stack.
  38. * if the order here is changed, it needs to be
  39. * updated in ptrace.c and ptrace.h
  40. *
  41. * r0
  42. * ...
  43. * r15 = stack pointer
  44. * spc
  45. * pr
  46. * ssr
  47. * gbr
  48. * mach
  49. * macl
  50. * syscall #
  51. *
  52. */
  53. #if defined(CONFIG_KGDB_NMI)
  54. NMI_VEC = 0x1c0 ! Must catch early for debounce
  55. #endif
  56. /* Offsets to the stack */
  57. OFF_R0 = 0 /* Return value. New ABI also arg4 */
  58. OFF_R1 = 4 /* New ABI: arg5 */
  59. OFF_R2 = 8 /* New ABI: arg6 */
  60. OFF_R3 = 12 /* New ABI: syscall_nr */
  61. OFF_R4 = 16 /* New ABI: arg0 */
  62. OFF_R5 = 20 /* New ABI: arg1 */
  63. OFF_R6 = 24 /* New ABI: arg2 */
  64. OFF_R7 = 28 /* New ABI: arg3 */
  65. OFF_SP = (15*4)
  66. OFF_PC = (16*4)
  67. OFF_SR = (16*4+8)
  68. OFF_TRA = (16*4+6*4)
  69. #define k0 r0
  70. #define k1 r1
  71. #define k2 r2
  72. #define k3 r3
  73. #define k4 r4
  74. #define g_imask r6 /* r6_bank1 */
  75. #define k_g_imask r6_bank /* r6_bank1 */
  76. #define current r7 /* r7_bank1 */
  77. #include <asm/entry-macros.S>
  78. /*
  79. * Kernel mode register usage:
  80. * k0 scratch
  81. * k1 scratch
  82. * k2 scratch (Exception code)
  83. * k3 scratch (Return address)
  84. * k4 scratch
  85. * k5 reserved
  86. * k6 Global Interrupt Mask (0--15 << 4)
  87. * k7 CURRENT_THREAD_INFO (pointer to current thread info)
  88. */
  89. !
  90. ! TLB Miss / Initial Page write exception handling
  91. ! _and_
  92. ! TLB hits, but the access violate the protection.
  93. ! It can be valid access, such as stack grow and/or C-O-W.
  94. !
  95. !
  96. ! Find the pmd/pte entry and loadtlb
  97. ! If it's not found, cause address error (SEGV)
  98. !
  99. ! Although this could be written in assembly language (and it'd be faster),
  100. ! this first version depends *much* on C implementation.
  101. !
  102. #if defined(CONFIG_MMU)
  103. .align 2
  104. ENTRY(tlb_miss_load)
  105. bra call_dpf
  106. mov #0, r5
  107. .align 2
  108. ENTRY(tlb_miss_store)
  109. bra call_dpf
  110. mov #1, r5
  111. .align 2
  112. ENTRY(initial_page_write)
  113. bra call_dpf
  114. mov #1, r5
  115. .align 2
  116. ENTRY(tlb_protection_violation_load)
  117. bra call_dpf
  118. mov #0, r5
  119. .align 2
  120. ENTRY(tlb_protection_violation_store)
  121. bra call_dpf
  122. mov #1, r5
  123. call_dpf:
  124. mov.l 1f, r0
  125. mov r5, r8
  126. mov.l @r0, r6
  127. mov r6, r9
  128. mov.l 2f, r0
  129. sts pr, r10
  130. jsr @r0
  131. mov r15, r4
  132. !
  133. tst r0, r0
  134. bf/s 0f
  135. lds r10, pr
  136. rts
  137. nop
  138. 0: mov.l 3f, r0
  139. mov r9, r6
  140. mov r8, r5
  141. jmp @r0
  142. mov r15, r4
  143. .align 2
  144. 1: .long MMU_TEA
  145. 2: .long __do_page_fault
  146. 3: .long do_page_fault
  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 @r15+, r0
  170. mov.l @r15+, r1
  171. mov.l @r15+, r2
  172. mov.l @r15+, r3
  173. mov.l @r15+, r4
  174. mov.l @r15+, r5
  175. mov.l @r15+, r6
  176. mov.l @r15+, r7
  177. stc sr, r8
  178. mov.l 1f, r9 ! BL =1, RB=1, IMASK=0x0F
  179. or r9, r8
  180. ldc r8, sr ! here, change the register bank
  181. mov.l @r15+, r8
  182. mov.l @r15+, r9
  183. mov.l @r15+, r10
  184. mov.l @r15+, r11
  185. mov.l @r15+, r12
  186. mov.l @r15+, r13
  187. mov.l @r15+, r14
  188. mov.l @r15+, k0
  189. ldc.l @r15+, spc
  190. lds.l @r15+, pr
  191. mov.l @r15+, k1
  192. ldc.l @r15+, gbr
  193. lds.l @r15+, mach
  194. lds.l @r15+, macl
  195. mov k0, r15
  196. !
  197. mov.l 2f, k0
  198. mov.l @k0, k0
  199. jmp @k0
  200. ldc k1, ssr
  201. .align 2
  202. 1: .long 0x300000f0
  203. 2: .long gdb_vbr_vector
  204. #endif /* CONFIG_SH_STANDARD_BIOS */
  205. restore_all:
  206. mov.l @r15+, r0
  207. mov.l @r15+, r1
  208. mov.l @r15+, r2
  209. mov.l @r15+, r3
  210. mov.l @r15+, r4
  211. mov.l @r15+, r5
  212. mov.l @r15+, r6
  213. mov.l @r15+, r7
  214. !
  215. stc sr, r8
  216. mov.l 7f, r9
  217. or r9, r8 ! BL =1, RB=1
  218. ldc r8, sr ! here, change the register bank
  219. !
  220. mov.l @r15+, r8
  221. mov.l @r15+, r9
  222. mov.l @r15+, r10
  223. mov.l @r15+, r11
  224. mov.l @r15+, r12
  225. mov.l @r15+, r13
  226. mov.l @r15+, r14
  227. mov.l @r15+, k4 ! original stack pointer
  228. ldc.l @r15+, spc
  229. lds.l @r15+, pr
  230. mov.l @r15+, k3 ! original SR
  231. ldc.l @r15+, gbr
  232. lds.l @r15+, mach
  233. lds.l @r15+, macl
  234. add #4, r15 ! Skip syscall number
  235. !
  236. #ifdef CONFIG_SH_DSP
  237. mov.l @r15+, k0 ! DSP mode marker
  238. mov.l 5f, k1
  239. cmp/eq k0, k1 ! Do we have a DSP stack frame?
  240. bf skip_restore
  241. stc sr, k0 ! Enable CPU DSP mode
  242. or k1, k0 ! (within kernel it may be disabled)
  243. ldc k0, sr
  244. mov r2, k0 ! Backup r2
  245. ! Restore DSP registers from stack
  246. mov r15, r2
  247. movs.l @r2+, a1
  248. movs.l @r2+, a0g
  249. movs.l @r2+, a1g
  250. movs.l @r2+, m0
  251. movs.l @r2+, m1
  252. mov r2, r15
  253. lds.l @r15+, a0
  254. lds.l @r15+, x0
  255. lds.l @r15+, x1
  256. lds.l @r15+, y0
  257. lds.l @r15+, y1
  258. lds.l @r15+, dsr
  259. ldc.l @r15+, rs
  260. ldc.l @r15+, re
  261. ldc.l @r15+, mod
  262. mov k0, r2 ! Restore r2
  263. skip_restore:
  264. #endif
  265. !
  266. ! Calculate new SR value
  267. mov k3, k2 ! original SR value
  268. mov #0xf0, k1
  269. extu.b k1, k1
  270. not k1, k1
  271. and k1, k2 ! Mask orignal SR value
  272. !
  273. mov k3, k0 ! Calculate IMASK-bits
  274. shlr2 k0
  275. and #0x3c, k0
  276. cmp/eq #0x3c, k0
  277. bt/s 6f
  278. shll2 k0
  279. mov g_imask, k0
  280. !
  281. 6: or k0, k2 ! Set the IMASK-bits
  282. ldc k2, ssr
  283. !
  284. #if defined(CONFIG_KGDB_NMI)
  285. ! Clear in_nmi
  286. mov.l 6f, k0
  287. mov #0, k1
  288. mov.b k1, @k0
  289. #endif
  290. mov.l @r15+, k2 ! restore EXPEVT
  291. mov k4, r15
  292. rte
  293. nop
  294. .align 2
  295. 5: .long 0x00001000 ! DSP
  296. #ifdef CONFIG_KGDB_NMI
  297. 6: .long in_nmi
  298. #endif
  299. 7: .long 0x30000000
  300. ! common exception handler
  301. #include "../../entry-common.S"
  302. ! Exception Vector Base
  303. !
  304. ! Should be aligned page boundary.
  305. !
  306. .balign 4096,0,4096
  307. ENTRY(vbr_base)
  308. .long 0
  309. !
  310. .balign 256,0,256
  311. general_exception:
  312. mov.l 1f, k2
  313. mov.l 2f, k3
  314. #ifdef CONFIG_CPU_SUBTYPE_SHX3
  315. mov.l @k2, k2
  316. ! Is EXPEVT larger than 0x800?
  317. mov #0x8, k0
  318. shll8 k0
  319. cmp/hs k0, k2
  320. bf 0f
  321. ! then add 0x580 (k2 is 0xd80 or 0xda0)
  322. mov #0x58, k0
  323. shll2 k0
  324. shll2 k0
  325. add k0, k2
  326. 0:
  327. bra handle_exception
  328. nop
  329. #else
  330. bra handle_exception
  331. mov.l @k2, k2
  332. #endif
  333. .align 2
  334. 1: .long EXPEVT
  335. 2: .long ret_from_exception
  336. !
  337. !
  338. .balign 1024,0,1024
  339. tlb_miss:
  340. mov.l 1f, k2
  341. mov.l 4f, k3
  342. bra handle_exception
  343. mov.l @k2, k2
  344. !
  345. .balign 512,0,512
  346. interrupt:
  347. mov.l 2f, k2
  348. mov.l 3f, k3
  349. #if defined(CONFIG_KGDB_NMI)
  350. ! Debounce (filter nested NMI)
  351. mov.l @k2, k0
  352. mov.l 5f, k1
  353. cmp/eq k1, k0
  354. bf 0f
  355. mov.l 6f, k1
  356. tas.b @k1
  357. bt 0f
  358. rte
  359. nop
  360. .align 2
  361. 5: .long NMI_VEC
  362. 6: .long in_nmi
  363. 0:
  364. #endif /* defined(CONFIG_KGDB_NMI) */
  365. bra handle_exception
  366. mov #-1, k2 ! interrupt exception marker
  367. .align 2
  368. 1: .long EXPEVT
  369. 2: .long INTEVT
  370. 3: .long ret_from_irq
  371. 4: .long ret_from_exception
  372. !
  373. !
  374. .align 2
  375. ENTRY(handle_exception)
  376. ! Using k0, k1 for scratch registers (r0_bank1, r1_bank),
  377. ! save all registers onto stack.
  378. !
  379. #ifdef CONFIG_GUSA
  380. ! Check for roll back gRB (User and Kernel)
  381. mov r15, k0
  382. shll k0
  383. bf/s 1f
  384. shll k0
  385. bf/s 1f
  386. stc spc, k1
  387. stc r0_bank, k0
  388. cmp/hs k0, k1 ! test k1 (saved PC) >= k0 (saved r0)
  389. bt/s 2f
  390. stc r1_bank, k1
  391. add #-2, k0
  392. add r15, k0
  393. ldc k0, spc ! PC = saved r0 + r15 - 2
  394. 2: mov k1, r15 ! SP = r1
  395. 1:
  396. #endif
  397. stc ssr, k0 ! Is it from kernel space?
  398. shll k0 ! Check MD bit (bit30) by shifting it into...
  399. shll k0 ! ...the T bit
  400. bt/s 1f ! It's a kernel to kernel transition.
  401. mov r15, k0 ! save original stack to k0
  402. /* User space to kernel */
  403. mov #(THREAD_SIZE >> 10), k1
  404. shll8 k1 ! k1 := THREAD_SIZE
  405. shll2 k1
  406. add current, k1
  407. mov k1, r15 ! change to kernel stack
  408. !
  409. 1: mov.l 2f, k1
  410. !
  411. #ifdef CONFIG_SH_DSP
  412. mov.l r2, @-r15 ! Save r2, we need another reg
  413. stc sr, k4
  414. mov.l 1f, r2
  415. tst r2, k4 ! Check if in DSP mode
  416. mov.l @r15+, r2 ! Restore r2 now
  417. bt/s skip_save
  418. mov #0, k4 ! Set marker for no stack frame
  419. mov r2, k4 ! Backup r2 (in k4) for later
  420. ! Save DSP registers on stack
  421. stc.l mod, @-r15
  422. stc.l re, @-r15
  423. stc.l rs, @-r15
  424. sts.l dsr, @-r15
  425. sts.l y1, @-r15
  426. sts.l y0, @-r15
  427. sts.l x1, @-r15
  428. sts.l x0, @-r15
  429. sts.l a0, @-r15
  430. ! GAS is broken, does not generate correct "movs.l Ds,@-As" instr.
  431. ! FIXME: Make sure that this is still the case with newer toolchains,
  432. ! as we're not at all interested in supporting ancient toolchains at
  433. ! this point. -- PFM.
  434. mov r15, r2
  435. .word 0xf653 ! movs.l a1, @-r2
  436. .word 0xf6f3 ! movs.l a0g, @-r2
  437. .word 0xf6d3 ! movs.l a1g, @-r2
  438. .word 0xf6c3 ! movs.l m0, @-r2
  439. .word 0xf6e3 ! movs.l m1, @-r2
  440. mov r2, r15
  441. mov k4, r2 ! Restore r2
  442. mov.l 1f, k4 ! Force DSP stack frame
  443. skip_save:
  444. mov.l k4, @-r15 ! Push DSP mode marker onto stack
  445. #endif
  446. ! Save the user registers on the stack.
  447. mov.l k2, @-r15 ! EXPEVT
  448. mov #-1, k4
  449. mov.l k4, @-r15 ! set TRA (default: -1)
  450. !
  451. sts.l macl, @-r15
  452. sts.l mach, @-r15
  453. stc.l gbr, @-r15
  454. stc.l ssr, @-r15
  455. sts.l pr, @-r15
  456. stc.l spc, @-r15
  457. !
  458. lds k3, pr ! Set the return address to pr
  459. !
  460. mov.l k0, @-r15 ! save orignal stack
  461. mov.l r14, @-r15
  462. mov.l r13, @-r15
  463. mov.l r12, @-r15
  464. mov.l r11, @-r15
  465. mov.l r10, @-r15
  466. mov.l r9, @-r15
  467. mov.l r8, @-r15
  468. !
  469. stc sr, r8 ! Back to normal register bank, and
  470. or k1, r8 ! Block all interrupts
  471. mov.l 3f, k1
  472. and k1, r8 ! ...
  473. ldc r8, sr ! ...changed here.
  474. !
  475. mov.l r7, @-r15
  476. mov.l r6, @-r15
  477. mov.l r5, @-r15
  478. mov.l r4, @-r15
  479. mov.l r3, @-r15
  480. mov.l r2, @-r15
  481. mov.l r1, @-r15
  482. mov.l r0, @-r15
  483. /*
  484. * This gets a bit tricky.. in the INTEVT case we don't want to use
  485. * the VBR offset as a destination in the jump call table, since all
  486. * of the destinations are the same. In this case, (interrupt) sets
  487. * a marker in r2 (now r2_bank since SR.RB changed), which we check
  488. * to determine the exception type. For all other exceptions, we
  489. * forcibly read EXPEVT from memory and fix up the jump address, in
  490. * the interrupt exception case we jump to do_IRQ() and defer the
  491. * INTEVT read until there. As a bonus, we can also clean up the SR.RB
  492. * checks that do_IRQ() was doing..
  493. */
  494. stc r2_bank, r8
  495. cmp/pz r8
  496. bf interrupt_exception
  497. shlr2 r8
  498. shlr r8
  499. mov.l 4f, r9
  500. add r8, r9
  501. mov.l @r9, r9
  502. jmp @r9
  503. nop
  504. rts
  505. nop
  506. .align 2
  507. 1: .long 0x00001000 ! DSP=1
  508. 2: .long 0x000080f0 ! FD=1, IMASK=15
  509. 3: .long 0xcfffffff ! RB=0, BL=0
  510. 4: .long exception_handling_table
  511. interrupt_exception:
  512. mov.l 1f, r9
  513. mov.l 2f, r4
  514. mov.l @r4, r4
  515. jmp @r9
  516. mov r15, r5
  517. rts
  518. nop
  519. .align 2
  520. 1: .long do_IRQ
  521. 2: .long INTEVT
  522. .align 2
  523. ENTRY(exception_none)
  524. rts
  525. nop