vfphw.S 8.7 KB

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