entry.S 11 KB

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