interrupts.S 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  3. * Author: Christoffer Dall <c.dall@virtualopensystems.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License, version 2, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #include <linux/linkage.h>
  19. #include <linux/const.h>
  20. #include <asm/unified.h>
  21. #include <asm/page.h>
  22. #include <asm/ptrace.h>
  23. #include <asm/asm-offsets.h>
  24. #include <asm/kvm_asm.h>
  25. #include <asm/kvm_arm.h>
  26. #include <asm/vfpmacros.h>
  27. #include "interrupts_head.S"
  28. .text
  29. __kvm_hyp_code_start:
  30. .globl __kvm_hyp_code_start
  31. /********************************************************************
  32. * Flush per-VMID TLBs
  33. *
  34. * void __kvm_tlb_flush_vmid(struct kvm *kvm);
  35. *
  36. * We rely on the hardware to broadcast the TLB invalidation to all CPUs
  37. * inside the inner-shareable domain (which is the case for all v7
  38. * implementations). If we come across a non-IS SMP implementation, we'll
  39. * have to use an IPI based mechanism. Until then, we stick to the simple
  40. * hardware assisted version.
  41. */
  42. ENTRY(__kvm_tlb_flush_vmid)
  43. push {r2, r3}
  44. add r0, r0, #KVM_VTTBR
  45. ldrd r2, r3, [r0]
  46. mcrr p15, 6, r2, r3, c2 @ Write VTTBR
  47. isb
  48. mcr p15, 0, r0, c8, c3, 0 @ TLBIALLIS (rt ignored)
  49. dsb
  50. isb
  51. mov r2, #0
  52. mov r3, #0
  53. mcrr p15, 6, r2, r3, c2 @ Back to VMID #0
  54. isb @ Not necessary if followed by eret
  55. pop {r2, r3}
  56. bx lr
  57. ENDPROC(__kvm_tlb_flush_vmid)
  58. /********************************************************************
  59. * Flush TLBs and instruction caches of all CPUs inside the inner-shareable
  60. * domain, for all VMIDs
  61. *
  62. * void __kvm_flush_vm_context(void);
  63. */
  64. ENTRY(__kvm_flush_vm_context)
  65. mov r0, #0 @ rn parameter for c15 flushes is SBZ
  66. /* Invalidate NS Non-Hyp TLB Inner Shareable (TLBIALLNSNHIS) */
  67. mcr p15, 4, r0, c8, c3, 4
  68. /* Invalidate instruction caches Inner Shareable (ICIALLUIS) */
  69. mcr p15, 0, r0, c7, c1, 0
  70. dsb
  71. isb @ Not necessary if followed by eret
  72. bx lr
  73. ENDPROC(__kvm_flush_vm_context)
  74. /********************************************************************
  75. * Hypervisor world-switch code
  76. *
  77. *
  78. * int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
  79. */
  80. ENTRY(__kvm_vcpu_run)
  81. @ Save the vcpu pointer
  82. mcr p15, 4, vcpu, c13, c0, 2 @ HTPIDR
  83. save_host_regs
  84. @ Store hardware CP15 state and load guest state
  85. read_cp15_state store_to_vcpu = 0
  86. write_cp15_state read_from_vcpu = 1
  87. @ If the host kernel has not been configured with VFPv3 support,
  88. @ then it is safer if we deny guests from using it as well.
  89. #ifdef CONFIG_VFPv3
  90. @ Set FPEXC_EN so the guest doesn't trap floating point instructions
  91. VFPFMRX r2, FPEXC @ VMRS
  92. push {r2}
  93. orr r2, r2, #FPEXC_EN
  94. VFPFMXR FPEXC, r2 @ VMSR
  95. #endif
  96. @ Configure Hyp-role
  97. configure_hyp_role vmentry
  98. @ Trap coprocessor CRx accesses
  99. set_hstr vmentry
  100. set_hcptr vmentry, (HCPTR_TTA | HCPTR_TCP(10) | HCPTR_TCP(11))
  101. set_hdcr vmentry
  102. @ Write configured ID register into MIDR alias
  103. ldr r1, [vcpu, #VCPU_MIDR]
  104. mcr p15, 4, r1, c0, c0, 0
  105. @ Write guest view of MPIDR into VMPIDR
  106. ldr r1, [vcpu, #CP15_OFFSET(c0_MPIDR)]
  107. mcr p15, 4, r1, c0, c0, 5
  108. @ Set up guest memory translation
  109. ldr r1, [vcpu, #VCPU_KVM]
  110. add r1, r1, #KVM_VTTBR
  111. ldrd r2, r3, [r1]
  112. mcrr p15, 6, r2, r3, c2 @ Write VTTBR
  113. @ We're all done, just restore the GPRs and go to the guest
  114. restore_guest_regs
  115. clrex @ Clear exclusive monitor
  116. eret
  117. __kvm_vcpu_return:
  118. /*
  119. * return convention:
  120. * guest r0, r1, r2 saved on the stack
  121. * r0: vcpu pointer
  122. * r1: exception code
  123. */
  124. save_guest_regs
  125. @ Set VMID == 0
  126. mov r2, #0
  127. mov r3, #0
  128. mcrr p15, 6, r2, r3, c2 @ Write VTTBR
  129. @ Don't trap coprocessor accesses for host kernel
  130. set_hstr vmexit
  131. set_hdcr vmexit
  132. set_hcptr vmexit, (HCPTR_TTA | HCPTR_TCP(10) | HCPTR_TCP(11))
  133. #ifdef CONFIG_VFPv3
  134. @ Save floating point registers we if let guest use them.
  135. tst r2, #(HCPTR_TCP(10) | HCPTR_TCP(11))
  136. bne after_vfp_restore
  137. @ Switch VFP/NEON hardware state to the host's
  138. add r7, vcpu, #VCPU_VFP_GUEST
  139. store_vfp_state r7
  140. add r7, vcpu, #VCPU_VFP_HOST
  141. ldr r7, [r7]
  142. restore_vfp_state r7
  143. after_vfp_restore:
  144. @ Restore FPEXC_EN which we clobbered on entry
  145. pop {r2}
  146. VFPFMXR FPEXC, r2
  147. #endif
  148. @ Reset Hyp-role
  149. configure_hyp_role vmexit
  150. @ Let host read hardware MIDR
  151. mrc p15, 0, r2, c0, c0, 0
  152. mcr p15, 4, r2, c0, c0, 0
  153. @ Back to hardware MPIDR
  154. mrc p15, 0, r2, c0, c0, 5
  155. mcr p15, 4, r2, c0, c0, 5
  156. @ Store guest CP15 state and restore host state
  157. read_cp15_state store_to_vcpu = 1
  158. write_cp15_state read_from_vcpu = 0
  159. restore_host_regs
  160. clrex @ Clear exclusive monitor
  161. mov r0, r1 @ Return the return code
  162. mov r1, #0 @ Clear upper bits in return value
  163. bx lr @ return to IOCTL
  164. /********************************************************************
  165. * Call function in Hyp mode
  166. *
  167. *
  168. * u64 kvm_call_hyp(void *hypfn, ...);
  169. *
  170. * This is not really a variadic function in the classic C-way and care must
  171. * be taken when calling this to ensure parameters are passed in registers
  172. * only, since the stack will change between the caller and the callee.
  173. *
  174. * Call the function with the first argument containing a pointer to the
  175. * function you wish to call in Hyp mode, and subsequent arguments will be
  176. * passed as r0, r1, and r2 (a maximum of 3 arguments in addition to the
  177. * function pointer can be passed). The function being called must be mapped
  178. * in Hyp mode (see init_hyp_mode in arch/arm/kvm/arm.c). Return values are
  179. * passed in r0 and r1.
  180. *
  181. * The calling convention follows the standard AAPCS:
  182. * r0 - r3: caller save
  183. * r12: caller save
  184. * rest: callee save
  185. */
  186. ENTRY(kvm_call_hyp)
  187. hvc #0
  188. bx lr
  189. /********************************************************************
  190. * Hypervisor exception vector and handlers
  191. *
  192. *
  193. * The KVM/ARM Hypervisor ABI is defined as follows:
  194. *
  195. * Entry to Hyp mode from the host kernel will happen _only_ when an HVC
  196. * instruction is issued since all traps are disabled when running the host
  197. * kernel as per the Hyp-mode initialization at boot time.
  198. *
  199. * HVC instructions cause a trap to the vector page + offset 0x18 (see hyp_hvc
  200. * below) when the HVC instruction is called from SVC mode (i.e. a guest or the
  201. * host kernel) and they cause a trap to the vector page + offset 0xc when HVC
  202. * instructions are called from within Hyp-mode.
  203. *
  204. * Hyp-ABI: Calling HYP-mode functions from host (in SVC mode):
  205. * Switching to Hyp mode is done through a simple HVC #0 instruction. The
  206. * exception vector code will check that the HVC comes from VMID==0 and if
  207. * so will push the necessary state (SPSR, lr_usr) on the Hyp stack.
  208. * - r0 contains a pointer to a HYP function
  209. * - r1, r2, and r3 contain arguments to the above function.
  210. * - The HYP function will be called with its arguments in r0, r1 and r2.
  211. * On HYP function return, we return directly to SVC.
  212. *
  213. * Note that the above is used to execute code in Hyp-mode from a host-kernel
  214. * point of view, and is a different concept from performing a world-switch and
  215. * executing guest code SVC mode (with a VMID != 0).
  216. */
  217. /* Handle undef, svc, pabt, or dabt by crashing with a user notice */
  218. .macro bad_exception exception_code, panic_str
  219. push {r0-r2}
  220. mrrc p15, 6, r0, r1, c2 @ Read VTTBR
  221. lsr r1, r1, #16
  222. ands r1, r1, #0xff
  223. beq 99f
  224. load_vcpu @ Load VCPU pointer
  225. .if \exception_code == ARM_EXCEPTION_DATA_ABORT
  226. mrc p15, 4, r2, c5, c2, 0 @ HSR
  227. mrc p15, 4, r1, c6, c0, 0 @ HDFAR
  228. str r2, [vcpu, #VCPU_HSR]
  229. str r1, [vcpu, #VCPU_HxFAR]
  230. .endif
  231. .if \exception_code == ARM_EXCEPTION_PREF_ABORT
  232. mrc p15, 4, r2, c5, c2, 0 @ HSR
  233. mrc p15, 4, r1, c6, c0, 2 @ HIFAR
  234. str r2, [vcpu, #VCPU_HSR]
  235. str r1, [vcpu, #VCPU_HxFAR]
  236. .endif
  237. mov r1, #\exception_code
  238. b __kvm_vcpu_return
  239. @ We were in the host already. Let's craft a panic-ing return to SVC.
  240. 99: mrs r2, cpsr
  241. bic r2, r2, #MODE_MASK
  242. orr r2, r2, #SVC_MODE
  243. THUMB( orr r2, r2, #PSR_T_BIT )
  244. msr spsr_cxsf, r2
  245. mrs r1, ELR_hyp
  246. ldr r2, =BSYM(panic)
  247. msr ELR_hyp, r2
  248. ldr r0, =\panic_str
  249. eret
  250. .endm
  251. .text
  252. .align 5
  253. __kvm_hyp_vector:
  254. .globl __kvm_hyp_vector
  255. @ Hyp-mode exception vector
  256. W(b) hyp_reset
  257. W(b) hyp_undef
  258. W(b) hyp_svc
  259. W(b) hyp_pabt
  260. W(b) hyp_dabt
  261. W(b) hyp_hvc
  262. W(b) hyp_irq
  263. W(b) hyp_fiq
  264. .align
  265. hyp_reset:
  266. b hyp_reset
  267. .align
  268. hyp_undef:
  269. bad_exception ARM_EXCEPTION_UNDEFINED, und_die_str
  270. .align
  271. hyp_svc:
  272. bad_exception ARM_EXCEPTION_HVC, svc_die_str
  273. .align
  274. hyp_pabt:
  275. bad_exception ARM_EXCEPTION_PREF_ABORT, pabt_die_str
  276. .align
  277. hyp_dabt:
  278. bad_exception ARM_EXCEPTION_DATA_ABORT, dabt_die_str
  279. .align
  280. hyp_hvc:
  281. /*
  282. * Getting here is either becuase of a trap from a guest or from calling
  283. * HVC from the host kernel, which means "switch to Hyp mode".
  284. */
  285. push {r0, r1, r2}
  286. @ Check syndrome register
  287. mrc p15, 4, r1, c5, c2, 0 @ HSR
  288. lsr r0, r1, #HSR_EC_SHIFT
  289. #ifdef CONFIG_VFPv3
  290. cmp r0, #HSR_EC_CP_0_13
  291. beq switch_to_guest_vfp
  292. #endif
  293. cmp r0, #HSR_EC_HVC
  294. bne guest_trap @ Not HVC instr.
  295. /*
  296. * Let's check if the HVC came from VMID 0 and allow simple
  297. * switch to Hyp mode
  298. */
  299. mrrc p15, 6, r0, r2, c2
  300. lsr r2, r2, #16
  301. and r2, r2, #0xff
  302. cmp r2, #0
  303. bne guest_trap @ Guest called HVC
  304. host_switch_to_hyp:
  305. pop {r0, r1, r2}
  306. push {lr}
  307. mrs lr, SPSR
  308. push {lr}
  309. mov lr, r0
  310. mov r0, r1
  311. mov r1, r2
  312. mov r2, r3
  313. THUMB( orr lr, #1)
  314. blx lr @ Call the HYP function
  315. pop {lr}
  316. msr SPSR_csxf, lr
  317. pop {lr}
  318. eret
  319. guest_trap:
  320. load_vcpu @ Load VCPU pointer to r0
  321. str r1, [vcpu, #VCPU_HSR]
  322. @ Check if we need the fault information
  323. lsr r1, r1, #HSR_EC_SHIFT
  324. cmp r1, #HSR_EC_IABT
  325. mrceq p15, 4, r2, c6, c0, 2 @ HIFAR
  326. beq 2f
  327. cmp r1, #HSR_EC_DABT
  328. bne 1f
  329. mrc p15, 4, r2, c6, c0, 0 @ HDFAR
  330. 2: str r2, [vcpu, #VCPU_HxFAR]
  331. /*
  332. * B3.13.5 Reporting exceptions taken to the Non-secure PL2 mode:
  333. *
  334. * Abort on the stage 2 translation for a memory access from a
  335. * Non-secure PL1 or PL0 mode:
  336. *
  337. * For any Access flag fault or Translation fault, and also for any
  338. * Permission fault on the stage 2 translation of a memory access
  339. * made as part of a translation table walk for a stage 1 translation,
  340. * the HPFAR holds the IPA that caused the fault. Otherwise, the HPFAR
  341. * is UNKNOWN.
  342. */
  343. /* Check for permission fault, and S1PTW */
  344. mrc p15, 4, r1, c5, c2, 0 @ HSR
  345. and r0, r1, #HSR_FSC_TYPE
  346. cmp r0, #FSC_PERM
  347. tsteq r1, #(1 << 7) @ S1PTW
  348. mrcne p15, 4, r2, c6, c0, 4 @ HPFAR
  349. bne 3f
  350. /* Resolve IPA using the xFAR */
  351. mcr p15, 0, r2, c7, c8, 0 @ ATS1CPR
  352. isb
  353. mrrc p15, 0, r0, r1, c7 @ PAR
  354. tst r0, #1
  355. bne 4f @ Failed translation
  356. ubfx r2, r0, #12, #20
  357. lsl r2, r2, #4
  358. orr r2, r2, r1, lsl #24
  359. 3: load_vcpu @ Load VCPU pointer to r0
  360. str r2, [r0, #VCPU_HPFAR]
  361. 1: mov r1, #ARM_EXCEPTION_HVC
  362. b __kvm_vcpu_return
  363. 4: pop {r0, r1, r2} @ Failed translation, return to guest
  364. eret
  365. /*
  366. * If VFPv3 support is not available, then we will not switch the VFP
  367. * registers; however cp10 and cp11 accesses will still trap and fallback
  368. * to the regular coprocessor emulation code, which currently will
  369. * inject an undefined exception to the guest.
  370. */
  371. #ifdef CONFIG_VFPv3
  372. switch_to_guest_vfp:
  373. load_vcpu @ Load VCPU pointer to r0
  374. push {r3-r7}
  375. @ NEON/VFP used. Turn on VFP access.
  376. set_hcptr vmexit, (HCPTR_TCP(10) | HCPTR_TCP(11))
  377. @ Switch VFP/NEON hardware state to the guest's
  378. add r7, r0, #VCPU_VFP_HOST
  379. ldr r7, [r7]
  380. store_vfp_state r7
  381. add r7, r0, #VCPU_VFP_GUEST
  382. restore_vfp_state r7
  383. pop {r3-r7}
  384. pop {r0-r2}
  385. eret
  386. #endif
  387. .align
  388. hyp_irq:
  389. push {r0, r1, r2}
  390. mov r1, #ARM_EXCEPTION_IRQ
  391. load_vcpu @ Load VCPU pointer to r0
  392. b __kvm_vcpu_return
  393. .align
  394. hyp_fiq:
  395. b hyp_fiq
  396. .ltorg
  397. __kvm_hyp_code_end:
  398. .globl __kvm_hyp_code_end
  399. .section ".rodata"
  400. und_die_str:
  401. .ascii "unexpected undefined exception in Hyp mode at: %#08x"
  402. pabt_die_str:
  403. .ascii "unexpected prefetch abort in Hyp mode at: %#08x"
  404. dabt_die_str:
  405. .ascii "unexpected data abort in Hyp mode at: %#08x"
  406. svc_die_str:
  407. .ascii "unexpected HVC/SVC trap in Hyp mode at: %#08x"