vfphw.S 8.6 KB

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