vfphw.S 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * linux/arch/arm/vfp/vfphw.S
  3. *
  4. * Copyright (C) 2004 ARM Limited.
  5. * Written by Deep Blue Solutions Limited.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This code is called from the kernel's undefined instruction trap.
  12. * r9 holds the return address for successful handling.
  13. * lr holds the return address for unrecognised instructions.
  14. * r10 points at the start of the private FP workspace in the thread structure
  15. * sp points to a struct pt_regs (as defined in include/asm/proc/ptrace.h)
  16. */
  17. #include <asm/thread_info.h>
  18. #include <asm/vfpmacros.h>
  19. #include "../kernel/entry-header.S"
  20. .macro DBGSTR, str
  21. #ifdef DEBUG
  22. stmfd sp!, {r0-r3, ip, lr}
  23. add r0, pc, #4
  24. bl printk
  25. b 1f
  26. .asciz "<7>VFP: \str\n"
  27. .balign 4
  28. 1: ldmfd sp!, {r0-r3, ip, lr}
  29. #endif
  30. .endm
  31. .macro DBGSTR1, str, arg
  32. #ifdef DEBUG
  33. stmfd sp!, {r0-r3, ip, lr}
  34. mov r1, \arg
  35. add r0, pc, #4
  36. bl printk
  37. b 1f
  38. .asciz "<7>VFP: \str\n"
  39. .balign 4
  40. 1: ldmfd sp!, {r0-r3, ip, lr}
  41. #endif
  42. .endm
  43. .macro DBGSTR3, str, arg1, arg2, arg3
  44. #ifdef DEBUG
  45. stmfd sp!, {r0-r3, ip, lr}
  46. mov r3, \arg3
  47. mov r2, \arg2
  48. mov r1, \arg1
  49. add r0, pc, #4
  50. bl printk
  51. b 1f
  52. .asciz "<7>VFP: \str\n"
  53. .balign 4
  54. 1: ldmfd sp!, {r0-r3, ip, lr}
  55. #endif
  56. .endm
  57. @ VFP hardware support entry point.
  58. @
  59. @ r0 = instruction opcode (32-bit ARM or two 16-bit Thumb)
  60. @ r2 = PC value to resume execution after successful emulation
  61. @ r9 = normal "successful" return address
  62. @ r10 = vfp_state union
  63. @ r11 = CPU number
  64. @ lr = unrecognised instruction return address
  65. @ IRQs enabled.
  66. ENTRY(vfp_support_entry)
  67. DBGSTR3 "instr %08x pc %08x state %p", r0, r2, r10
  68. ldr r3, [sp, #S_PSR] @ Neither lazy restore nor FP exceptions
  69. and r3, r3, #MODE_MASK @ are supported in kernel mode
  70. teq r3, #USR_MODE
  71. bne vfp_kmode_exception @ Returns through lr
  72. VFPFMRX r1, FPEXC @ Is the VFP enabled?
  73. DBGSTR1 "fpexc %08x", r1
  74. tst r1, #FPEXC_EN
  75. bne look_for_VFP_exceptions @ VFP is already enabled
  76. DBGSTR1 "enable %x", r10
  77. ldr r3, vfp_current_hw_state_address
  78. orr r1, r1, #FPEXC_EN @ user FPEXC has the enable bit set
  79. ldr r4, [r3, r11, lsl #2] @ vfp_current_hw_state pointer
  80. bic r5, r1, #FPEXC_EX @ make sure exceptions are disabled
  81. cmp r4, r10 @ this thread owns the hw context?
  82. #ifndef CONFIG_SMP
  83. @ For UP, checking that this thread owns the hw context is
  84. @ sufficient to determine that the hardware state is valid.
  85. beq vfp_hw_state_valid
  86. @ On UP, we lazily save the VFP context. As a different
  87. @ thread wants ownership of the VFP hardware, save the old
  88. @ state if there was a previous (valid) owner.
  89. VFPFMXR FPEXC, r5 @ enable VFP, disable any pending
  90. @ exceptions, so we can get at the
  91. @ rest of it
  92. DBGSTR1 "save old state %p", r4
  93. cmp r4, #0 @ if the vfp_current_hw_state is NULL
  94. beq vfp_reload_hw @ then the hw state needs reloading
  95. VFPFSTMIA r4, r5 @ save the working registers
  96. VFPFMRX r5, FPSCR @ current status
  97. #ifndef CONFIG_CPU_FEROCEON
  98. tst r1, #FPEXC_EX @ is there additional state to save?
  99. beq 1f
  100. VFPFMRX r6, FPINST @ FPINST (only if FPEXC.EX is set)
  101. tst r1, #FPEXC_FP2V @ is there an FPINST2 to read?
  102. beq 1f
  103. VFPFMRX r8, FPINST2 @ FPINST2 if needed (and present)
  104. 1:
  105. #endif
  106. stmia r4, {r1, r5, r6, r8} @ save FPEXC, FPSCR, FPINST, FPINST2
  107. vfp_reload_hw:
  108. #else
  109. @ For SMP, if this thread does not own the hw context, then we
  110. @ need to reload it. No need to save the old state as on SMP,
  111. @ we always save the state when we switch away from a thread.
  112. bne vfp_reload_hw
  113. @ This thread has ownership of the current hardware context.
  114. @ However, it may have been migrated to another CPU, in which
  115. @ case the saved state is newer than the hardware context.
  116. @ Check this by looking at the CPU number which the state was
  117. @ last loaded onto.
  118. ldr ip, [r10, #VFP_CPU]
  119. teq ip, r11
  120. beq vfp_hw_state_valid
  121. vfp_reload_hw:
  122. @ We're loading this threads state into the VFP hardware. Update
  123. @ the CPU number which contains the most up to date VFP context.
  124. str r11, [r10, #VFP_CPU]
  125. VFPFMXR FPEXC, r5 @ enable VFP, disable any pending
  126. @ exceptions, so we can get at the
  127. @ rest of it
  128. #endif
  129. DBGSTR1 "load state %p", r10
  130. str r10, [r3, r11, lsl #2] @ update the vfp_current_hw_state pointer
  131. @ Load the saved state back into the VFP
  132. VFPFLDMIA r10, r5 @ reload the working registers while
  133. @ FPEXC is in a safe state
  134. ldmia r10, {r1, r5, r6, r8} @ load FPEXC, FPSCR, FPINST, FPINST2
  135. #ifndef CONFIG_CPU_FEROCEON
  136. tst r1, #FPEXC_EX @ is there additional state to restore?
  137. beq 1f
  138. VFPFMXR FPINST, r6 @ restore FPINST (only if FPEXC.EX is set)
  139. tst r1, #FPEXC_FP2V @ is there an FPINST2 to write?
  140. beq 1f
  141. VFPFMXR FPINST2, r8 @ FPINST2 if needed (and present)
  142. 1:
  143. #endif
  144. VFPFMXR FPSCR, r5 @ restore status
  145. @ The context stored in the VFP hardware is up to date with this thread
  146. vfp_hw_state_valid:
  147. tst r1, #FPEXC_EX
  148. bne process_exception @ might as well handle the pending
  149. @ exception before retrying branch
  150. @ out before setting an FPEXC that
  151. @ stops us reading stuff
  152. VFPFMXR FPEXC, r1 @ Restore FPEXC last
  153. sub r2, r2, #4 @ Retry current instruction - if Thumb
  154. str r2, [sp, #S_PC] @ mode it's two 16-bit instructions,
  155. @ else it's one 32-bit instruction, so
  156. @ always subtract 4 from the following
  157. @ instruction address.
  158. #ifdef CONFIG_PREEMPT_COUNT
  159. get_thread_info r10
  160. ldr r4, [r10, #TI_PREEMPT] @ get preempt count
  161. sub r11, r4, #1 @ decrement it
  162. str r11, [r10, #TI_PREEMPT]
  163. #endif
  164. mov pc, r9 @ we think we have handled things
  165. look_for_VFP_exceptions:
  166. @ Check for synchronous or asynchronous exception
  167. tst r1, #FPEXC_EX | FPEXC_DEX
  168. bne process_exception
  169. @ On some implementations of the VFP subarch 1, setting FPSCR.IXE
  170. @ causes all the CDP instructions to be bounced synchronously without
  171. @ setting the FPEXC.EX bit
  172. VFPFMRX r5, FPSCR
  173. tst r5, #FPSCR_IXE
  174. bne process_exception
  175. #ifdef CONFIG_ARCH_MSM_KRAIT
  176. @ Krait does not set FPEXC.DEX for unsupported short vector instructions
  177. mrc p15, 0, r2, c0, c0, 0
  178. ldr r4, =0xff00fc00
  179. and r4, r2, r4
  180. ldr r2, =0x51000400
  181. cmp r2, r4
  182. bne skip
  183. tst r5, #FPSCR_LENGTH_MASK
  184. beq skip
  185. orr r1, r1, #FPEXC_DEX
  186. b process_exception
  187. skip:
  188. #endif
  189. @ Fall into hand on to next handler - appropriate coproc instr
  190. @ not recognised by VFP
  191. DBGSTR "not VFP"
  192. #ifdef CONFIG_PREEMPT_COUNT
  193. get_thread_info r10
  194. ldr r4, [r10, #TI_PREEMPT] @ get preempt count
  195. sub r11, r4, #1 @ decrement it
  196. str r11, [r10, #TI_PREEMPT]
  197. #endif
  198. mov pc, lr
  199. process_exception:
  200. DBGSTR "bounce"
  201. mov r2, sp @ nothing stacked - regdump is at TOS
  202. mov lr, r9 @ setup for a return to the user code.
  203. @ Now call the C code to package up the bounce to the support code
  204. @ r0 holds the trigger instruction
  205. @ r1 holds the FPEXC value
  206. @ r2 pointer to register dump
  207. b VFP_bounce @ we have handled this - the support
  208. @ code will raise an exception if
  209. @ required. If not, the user code will
  210. @ retry the faulted instruction
  211. ENDPROC(vfp_support_entry)
  212. ENTRY(vfp_save_state)
  213. @ Save the current VFP state
  214. @ r0 - save location
  215. @ r1 - FPEXC
  216. DBGSTR1 "save VFP state %p", r0
  217. VFPFSTMIA r0, r2 @ save the working registers
  218. VFPFMRX r2, FPSCR @ current status
  219. tst r1, #FPEXC_EX @ is there additional state to save?
  220. beq 1f
  221. VFPFMRX r3, FPINST @ FPINST (only if FPEXC.EX is set)
  222. tst r1, #FPEXC_FP2V @ is there an FPINST2 to read?
  223. beq 1f
  224. VFPFMRX r12, FPINST2 @ FPINST2 if needed (and present)
  225. 1:
  226. stmia r0, {r1, r2, r3, r12} @ save FPEXC, FPSCR, FPINST, FPINST2
  227. mov pc, lr
  228. ENDPROC(vfp_save_state)
  229. .align
  230. vfp_current_hw_state_address:
  231. .word vfp_current_hw_state
  232. .macro tbl_branch, base, tmp, shift
  233. #ifdef CONFIG_THUMB2_KERNEL
  234. adr \tmp, 1f
  235. add \tmp, \tmp, \base, lsl \shift
  236. mov pc, \tmp
  237. #else
  238. add pc, pc, \base, lsl \shift
  239. mov r0, r0
  240. #endif
  241. 1:
  242. .endm
  243. ENTRY(vfp_get_float)
  244. tbl_branch r0, r3, #3
  245. .irp dr,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
  246. 1: mrc p10, 0, r0, c\dr, c0, 0 @ fmrs r0, s0
  247. mov pc, lr
  248. .org 1b + 8
  249. 1: mrc p10, 0, r0, c\dr, c0, 4 @ fmrs r0, s1
  250. mov pc, lr
  251. .org 1b + 8
  252. .endr
  253. ENDPROC(vfp_get_float)
  254. ENTRY(vfp_put_float)
  255. tbl_branch r1, r3, #3
  256. .irp dr,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
  257. 1: mcr p10, 0, r0, c\dr, c0, 0 @ fmsr r0, s0
  258. mov pc, lr
  259. .org 1b + 8
  260. 1: mcr p10, 0, r0, c\dr, c0, 4 @ fmsr r0, s1
  261. mov pc, lr
  262. .org 1b + 8
  263. .endr
  264. ENDPROC(vfp_put_float)
  265. ENTRY(vfp_get_double)
  266. tbl_branch r0, r3, #3
  267. .irp dr,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
  268. 1: fmrrd r0, r1, d\dr
  269. mov pc, lr
  270. .org 1b + 8
  271. .endr
  272. #ifdef CONFIG_VFPv3
  273. @ d16 - d31 registers
  274. .irp dr,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
  275. 1: mrrc p11, 3, r0, r1, c\dr @ fmrrd r0, r1, d\dr
  276. mov pc, lr
  277. .org 1b + 8
  278. .endr
  279. #endif
  280. @ virtual register 16 (or 32 if VFPv3) for compare with zero
  281. mov r0, #0
  282. mov r1, #0
  283. mov pc, lr
  284. ENDPROC(vfp_get_double)
  285. ENTRY(vfp_put_double)
  286. tbl_branch r2, r3, #3
  287. .irp dr,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
  288. 1: fmdrr d\dr, r0, r1
  289. mov pc, lr
  290. .org 1b + 8
  291. .endr
  292. #ifdef CONFIG_VFPv3
  293. @ d16 - d31 registers
  294. .irp dr,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
  295. 1: mcrr p11, 3, r0, r1, c\dr @ fmdrr r0, r1, d\dr
  296. mov pc, lr
  297. .org 1b + 8
  298. .endr
  299. #endif
  300. ENDPROC(vfp_put_double)