hyp.S 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*
  2. * Copyright (C) 2012,2013 - ARM Ltd
  3. * Author: Marc Zyngier <marc.zyngier@arm.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, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/linkage.h>
  18. #include <linux/irqchip/arm-gic.h>
  19. #include <asm/assembler.h>
  20. #include <asm/memory.h>
  21. #include <asm/asm-offsets.h>
  22. #include <asm/fpsimdmacros.h>
  23. #include <asm/kvm.h>
  24. #include <asm/kvm_asm.h>
  25. #include <asm/kvm_arm.h>
  26. #include <asm/kvm_mmu.h>
  27. #define CPU_GP_REG_OFFSET(x) (CPU_GP_REGS + x)
  28. #define CPU_XREG_OFFSET(x) CPU_GP_REG_OFFSET(CPU_USER_PT_REGS + 8*x)
  29. #define CPU_SPSR_OFFSET(x) CPU_GP_REG_OFFSET(CPU_SPSR + 8*x)
  30. #define CPU_SYSREG_OFFSET(x) (CPU_SYSREGS + 8*x)
  31. .text
  32. .pushsection .hyp.text, "ax"
  33. .align PAGE_SHIFT
  34. __kvm_hyp_code_start:
  35. .globl __kvm_hyp_code_start
  36. .macro save_common_regs
  37. // x2: base address for cpu context
  38. // x3: tmp register
  39. add x3, x2, #CPU_XREG_OFFSET(19)
  40. stp x19, x20, [x3]
  41. stp x21, x22, [x3, #16]
  42. stp x23, x24, [x3, #32]
  43. stp x25, x26, [x3, #48]
  44. stp x27, x28, [x3, #64]
  45. stp x29, lr, [x3, #80]
  46. mrs x19, sp_el0
  47. mrs x20, elr_el2 // EL1 PC
  48. mrs x21, spsr_el2 // EL1 pstate
  49. stp x19, x20, [x3, #96]
  50. str x21, [x3, #112]
  51. mrs x22, sp_el1
  52. mrs x23, elr_el1
  53. mrs x24, spsr_el1
  54. str x22, [x2, #CPU_GP_REG_OFFSET(CPU_SP_EL1)]
  55. str x23, [x2, #CPU_GP_REG_OFFSET(CPU_ELR_EL1)]
  56. str x24, [x2, #CPU_SPSR_OFFSET(KVM_SPSR_EL1)]
  57. .endm
  58. .macro restore_common_regs
  59. // x2: base address for cpu context
  60. // x3: tmp register
  61. ldr x22, [x2, #CPU_GP_REG_OFFSET(CPU_SP_EL1)]
  62. ldr x23, [x2, #CPU_GP_REG_OFFSET(CPU_ELR_EL1)]
  63. ldr x24, [x2, #CPU_SPSR_OFFSET(KVM_SPSR_EL1)]
  64. msr sp_el1, x22
  65. msr elr_el1, x23
  66. msr spsr_el1, x24
  67. add x3, x2, #CPU_XREG_OFFSET(31) // SP_EL0
  68. ldp x19, x20, [x3]
  69. ldr x21, [x3, #16]
  70. msr sp_el0, x19
  71. msr elr_el2, x20 // EL1 PC
  72. msr spsr_el2, x21 // EL1 pstate
  73. add x3, x2, #CPU_XREG_OFFSET(19)
  74. ldp x19, x20, [x3]
  75. ldp x21, x22, [x3, #16]
  76. ldp x23, x24, [x3, #32]
  77. ldp x25, x26, [x3, #48]
  78. ldp x27, x28, [x3, #64]
  79. ldp x29, lr, [x3, #80]
  80. .endm
  81. .macro save_host_regs
  82. save_common_regs
  83. .endm
  84. .macro restore_host_regs
  85. restore_common_regs
  86. .endm
  87. .macro save_fpsimd
  88. // x2: cpu context address
  89. // x3, x4: tmp regs
  90. add x3, x2, #CPU_GP_REG_OFFSET(CPU_FP_REGS)
  91. fpsimd_save x3, 4
  92. .endm
  93. .macro restore_fpsimd
  94. // x2: cpu context address
  95. // x3, x4: tmp regs
  96. add x3, x2, #CPU_GP_REG_OFFSET(CPU_FP_REGS)
  97. fpsimd_restore x3, 4
  98. .endm
  99. .macro save_guest_regs
  100. // x0 is the vcpu address
  101. // x1 is the return code, do not corrupt!
  102. // x2 is the cpu context
  103. // x3 is a tmp register
  104. // Guest's x0-x3 are on the stack
  105. // Compute base to save registers
  106. add x3, x2, #CPU_XREG_OFFSET(4)
  107. stp x4, x5, [x3]
  108. stp x6, x7, [x3, #16]
  109. stp x8, x9, [x3, #32]
  110. stp x10, x11, [x3, #48]
  111. stp x12, x13, [x3, #64]
  112. stp x14, x15, [x3, #80]
  113. stp x16, x17, [x3, #96]
  114. str x18, [x3, #112]
  115. pop x6, x7 // x2, x3
  116. pop x4, x5 // x0, x1
  117. add x3, x2, #CPU_XREG_OFFSET(0)
  118. stp x4, x5, [x3]
  119. stp x6, x7, [x3, #16]
  120. save_common_regs
  121. .endm
  122. .macro restore_guest_regs
  123. // x0 is the vcpu address.
  124. // x2 is the cpu context
  125. // x3 is a tmp register
  126. // Prepare x0-x3 for later restore
  127. add x3, x2, #CPU_XREG_OFFSET(0)
  128. ldp x4, x5, [x3]
  129. ldp x6, x7, [x3, #16]
  130. push x4, x5 // Push x0-x3 on the stack
  131. push x6, x7
  132. // x4-x18
  133. ldp x4, x5, [x3, #32]
  134. ldp x6, x7, [x3, #48]
  135. ldp x8, x9, [x3, #64]
  136. ldp x10, x11, [x3, #80]
  137. ldp x12, x13, [x3, #96]
  138. ldp x14, x15, [x3, #112]
  139. ldp x16, x17, [x3, #128]
  140. ldr x18, [x3, #144]
  141. // x19-x29, lr, sp*, elr*, spsr*
  142. restore_common_regs
  143. // Last bits of the 64bit state
  144. pop x2, x3
  145. pop x0, x1
  146. // Do not touch any register after this!
  147. .endm
  148. /*
  149. * Macros to perform system register save/restore.
  150. *
  151. * Ordering here is absolutely critical, and must be kept consistent
  152. * in {save,restore}_sysregs, {save,restore}_guest_32bit_state,
  153. * and in kvm_asm.h.
  154. *
  155. * In other words, don't touch any of these unless you know what
  156. * you are doing.
  157. */
  158. .macro save_sysregs
  159. // x2: base address for cpu context
  160. // x3: tmp register
  161. add x3, x2, #CPU_SYSREG_OFFSET(MPIDR_EL1)
  162. mrs x4, vmpidr_el2
  163. mrs x5, csselr_el1
  164. mrs x6, sctlr_el1
  165. mrs x7, actlr_el1
  166. mrs x8, cpacr_el1
  167. mrs x9, ttbr0_el1
  168. mrs x10, ttbr1_el1
  169. mrs x11, tcr_el1
  170. mrs x12, esr_el1
  171. mrs x13, afsr0_el1
  172. mrs x14, afsr1_el1
  173. mrs x15, far_el1
  174. mrs x16, mair_el1
  175. mrs x17, vbar_el1
  176. mrs x18, contextidr_el1
  177. mrs x19, tpidr_el0
  178. mrs x20, tpidrro_el0
  179. mrs x21, tpidr_el1
  180. mrs x22, amair_el1
  181. mrs x23, cntkctl_el1
  182. stp x4, x5, [x3]
  183. stp x6, x7, [x3, #16]
  184. stp x8, x9, [x3, #32]
  185. stp x10, x11, [x3, #48]
  186. stp x12, x13, [x3, #64]
  187. stp x14, x15, [x3, #80]
  188. stp x16, x17, [x3, #96]
  189. stp x18, x19, [x3, #112]
  190. stp x20, x21, [x3, #128]
  191. stp x22, x23, [x3, #144]
  192. .endm
  193. .macro restore_sysregs
  194. // x2: base address for cpu context
  195. // x3: tmp register
  196. add x3, x2, #CPU_SYSREG_OFFSET(MPIDR_EL1)
  197. ldp x4, x5, [x3]
  198. ldp x6, x7, [x3, #16]
  199. ldp x8, x9, [x3, #32]
  200. ldp x10, x11, [x3, #48]
  201. ldp x12, x13, [x3, #64]
  202. ldp x14, x15, [x3, #80]
  203. ldp x16, x17, [x3, #96]
  204. ldp x18, x19, [x3, #112]
  205. ldp x20, x21, [x3, #128]
  206. ldp x22, x23, [x3, #144]
  207. msr vmpidr_el2, x4
  208. msr csselr_el1, x5
  209. msr sctlr_el1, x6
  210. msr actlr_el1, x7
  211. msr cpacr_el1, x8
  212. msr ttbr0_el1, x9
  213. msr ttbr1_el1, x10
  214. msr tcr_el1, x11
  215. msr esr_el1, x12
  216. msr afsr0_el1, x13
  217. msr afsr1_el1, x14
  218. msr far_el1, x15
  219. msr mair_el1, x16
  220. msr vbar_el1, x17
  221. msr contextidr_el1, x18
  222. msr tpidr_el0, x19
  223. msr tpidrro_el0, x20
  224. msr tpidr_el1, x21
  225. msr amair_el1, x22
  226. msr cntkctl_el1, x23
  227. .endm
  228. .macro activate_traps
  229. ldr x2, [x0, #VCPU_IRQ_LINES]
  230. ldr x1, [x0, #VCPU_HCR_EL2]
  231. orr x2, x2, x1
  232. msr hcr_el2, x2
  233. ldr x2, =(CPTR_EL2_TTA)
  234. msr cptr_el2, x2
  235. ldr x2, =(1 << 15) // Trap CP15 Cr=15
  236. msr hstr_el2, x2
  237. mrs x2, mdcr_el2
  238. and x2, x2, #MDCR_EL2_HPMN_MASK
  239. orr x2, x2, #(MDCR_EL2_TPM | MDCR_EL2_TPMCR)
  240. msr mdcr_el2, x2
  241. .endm
  242. .macro deactivate_traps
  243. mov x2, #HCR_RW
  244. msr hcr_el2, x2
  245. msr cptr_el2, xzr
  246. msr hstr_el2, xzr
  247. mrs x2, mdcr_el2
  248. and x2, x2, #MDCR_EL2_HPMN_MASK
  249. msr mdcr_el2, x2
  250. .endm
  251. .macro activate_vm
  252. ldr x1, [x0, #VCPU_KVM]
  253. kern_hyp_va x1
  254. ldr x2, [x1, #KVM_VTTBR]
  255. msr vttbr_el2, x2
  256. .endm
  257. .macro deactivate_vm
  258. msr vttbr_el2, xzr
  259. .endm
  260. /*
  261. * Save the VGIC CPU state into memory
  262. * x0: Register pointing to VCPU struct
  263. * Do not corrupt x1!!!
  264. */
  265. .macro save_vgic_state
  266. /* Get VGIC VCTRL base into x2 */
  267. ldr x2, [x0, #VCPU_KVM]
  268. kern_hyp_va x2
  269. ldr x2, [x2, #KVM_VGIC_VCTRL]
  270. kern_hyp_va x2
  271. cbz x2, 2f // disabled
  272. /* Compute the address of struct vgic_cpu */
  273. add x3, x0, #VCPU_VGIC_CPU
  274. /* Save all interesting registers */
  275. ldr w4, [x2, #GICH_HCR]
  276. ldr w5, [x2, #GICH_VMCR]
  277. ldr w6, [x2, #GICH_MISR]
  278. ldr w7, [x2, #GICH_EISR0]
  279. ldr w8, [x2, #GICH_EISR1]
  280. ldr w9, [x2, #GICH_ELRSR0]
  281. ldr w10, [x2, #GICH_ELRSR1]
  282. ldr w11, [x2, #GICH_APR]
  283. str w4, [x3, #VGIC_CPU_HCR]
  284. str w5, [x3, #VGIC_CPU_VMCR]
  285. str w6, [x3, #VGIC_CPU_MISR]
  286. str w7, [x3, #VGIC_CPU_EISR]
  287. str w8, [x3, #(VGIC_CPU_EISR + 4)]
  288. str w9, [x3, #VGIC_CPU_ELRSR]
  289. str w10, [x3, #(VGIC_CPU_ELRSR + 4)]
  290. str w11, [x3, #VGIC_CPU_APR]
  291. /* Clear GICH_HCR */
  292. str wzr, [x2, #GICH_HCR]
  293. /* Save list registers */
  294. add x2, x2, #GICH_LR0
  295. ldr w4, [x3, #VGIC_CPU_NR_LR]
  296. add x3, x3, #VGIC_CPU_LR
  297. 1: ldr w5, [x2], #4
  298. str w5, [x3], #4
  299. sub w4, w4, #1
  300. cbnz w4, 1b
  301. 2:
  302. .endm
  303. /*
  304. * Restore the VGIC CPU state from memory
  305. * x0: Register pointing to VCPU struct
  306. */
  307. .macro restore_vgic_state
  308. /* Get VGIC VCTRL base into x2 */
  309. ldr x2, [x0, #VCPU_KVM]
  310. kern_hyp_va x2
  311. ldr x2, [x2, #KVM_VGIC_VCTRL]
  312. kern_hyp_va x2
  313. cbz x2, 2f // disabled
  314. /* Compute the address of struct vgic_cpu */
  315. add x3, x0, #VCPU_VGIC_CPU
  316. /* We only restore a minimal set of registers */
  317. ldr w4, [x3, #VGIC_CPU_HCR]
  318. ldr w5, [x3, #VGIC_CPU_VMCR]
  319. ldr w6, [x3, #VGIC_CPU_APR]
  320. str w4, [x2, #GICH_HCR]
  321. str w5, [x2, #GICH_VMCR]
  322. str w6, [x2, #GICH_APR]
  323. /* Restore list registers */
  324. add x2, x2, #GICH_LR0
  325. ldr w4, [x3, #VGIC_CPU_NR_LR]
  326. add x3, x3, #VGIC_CPU_LR
  327. 1: ldr w5, [x3], #4
  328. str w5, [x2], #4
  329. sub w4, w4, #1
  330. cbnz w4, 1b
  331. 2:
  332. .endm
  333. .macro save_timer_state
  334. // x0: vcpu pointer
  335. ldr x2, [x0, #VCPU_KVM]
  336. kern_hyp_va x2
  337. ldr w3, [x2, #KVM_TIMER_ENABLED]
  338. cbz w3, 1f
  339. mrs x3, cntv_ctl_el0
  340. and x3, x3, #3
  341. str w3, [x0, #VCPU_TIMER_CNTV_CTL]
  342. bic x3, x3, #1 // Clear Enable
  343. msr cntv_ctl_el0, x3
  344. isb
  345. mrs x3, cntv_cval_el0
  346. str x3, [x0, #VCPU_TIMER_CNTV_CVAL]
  347. 1:
  348. // Allow physical timer/counter access for the host
  349. mrs x2, cnthctl_el2
  350. orr x2, x2, #3
  351. msr cnthctl_el2, x2
  352. // Clear cntvoff for the host
  353. msr cntvoff_el2, xzr
  354. .endm
  355. .macro restore_timer_state
  356. // x0: vcpu pointer
  357. // Disallow physical timer access for the guest
  358. // Physical counter access is allowed
  359. mrs x2, cnthctl_el2
  360. orr x2, x2, #1
  361. bic x2, x2, #2
  362. msr cnthctl_el2, x2
  363. ldr x2, [x0, #VCPU_KVM]
  364. kern_hyp_va x2
  365. ldr w3, [x2, #KVM_TIMER_ENABLED]
  366. cbz w3, 1f
  367. ldr x3, [x2, #KVM_TIMER_CNTVOFF]
  368. msr cntvoff_el2, x3
  369. ldr x2, [x0, #VCPU_TIMER_CNTV_CVAL]
  370. msr cntv_cval_el0, x2
  371. isb
  372. ldr w2, [x0, #VCPU_TIMER_CNTV_CTL]
  373. and x2, x2, #3
  374. msr cntv_ctl_el0, x2
  375. 1:
  376. .endm
  377. __save_sysregs:
  378. save_sysregs
  379. ret
  380. __restore_sysregs:
  381. restore_sysregs
  382. ret
  383. __save_fpsimd:
  384. save_fpsimd
  385. ret
  386. __restore_fpsimd:
  387. restore_fpsimd
  388. ret
  389. /*
  390. * u64 __kvm_vcpu_run(struct kvm_vcpu *vcpu);
  391. *
  392. * This is the world switch. The first half of the function
  393. * deals with entering the guest, and anything from __kvm_vcpu_return
  394. * to the end of the function deals with reentering the host.
  395. * On the enter path, only x0 (vcpu pointer) must be preserved until
  396. * the last moment. On the exit path, x0 (vcpu pointer) and x1 (exception
  397. * code) must both be preserved until the epilogue.
  398. * In both cases, x2 points to the CPU context we're saving/restoring from/to.
  399. */
  400. ENTRY(__kvm_vcpu_run)
  401. kern_hyp_va x0
  402. msr tpidr_el2, x0 // Save the vcpu register
  403. // Host context
  404. ldr x2, [x0, #VCPU_HOST_CONTEXT]
  405. kern_hyp_va x2
  406. save_host_regs
  407. bl __save_fpsimd
  408. bl __save_sysregs
  409. activate_traps
  410. activate_vm
  411. restore_vgic_state
  412. restore_timer_state
  413. // Guest context
  414. add x2, x0, #VCPU_CONTEXT
  415. bl __restore_sysregs
  416. bl __restore_fpsimd
  417. restore_guest_regs
  418. // That's it, no more messing around.
  419. eret
  420. __kvm_vcpu_return:
  421. // Assume x0 is the vcpu pointer, x1 the return code
  422. // Guest's x0-x3 are on the stack
  423. // Guest context
  424. add x2, x0, #VCPU_CONTEXT
  425. save_guest_regs
  426. bl __save_fpsimd
  427. bl __save_sysregs
  428. save_timer_state
  429. save_vgic_state
  430. deactivate_traps
  431. deactivate_vm
  432. // Host context
  433. ldr x2, [x0, #VCPU_HOST_CONTEXT]
  434. kern_hyp_va x2
  435. bl __restore_sysregs
  436. bl __restore_fpsimd
  437. restore_host_regs
  438. mov x0, x1
  439. ret
  440. END(__kvm_vcpu_run)
  441. // void __kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa);
  442. ENTRY(__kvm_tlb_flush_vmid_ipa)
  443. kern_hyp_va x0
  444. ldr x2, [x0, #KVM_VTTBR]
  445. msr vttbr_el2, x2
  446. isb
  447. /*
  448. * We could do so much better if we had the VA as well.
  449. * Instead, we invalidate Stage-2 for this IPA, and the
  450. * whole of Stage-1. Weep...
  451. */
  452. tlbi ipas2e1is, x1
  453. dsb sy
  454. tlbi vmalle1is
  455. dsb sy
  456. isb
  457. msr vttbr_el2, xzr
  458. ret
  459. ENDPROC(__kvm_tlb_flush_vmid_ipa)
  460. ENTRY(__kvm_flush_vm_context)
  461. tlbi alle1is
  462. ic ialluis
  463. dsb sy
  464. ret
  465. ENDPROC(__kvm_flush_vm_context)
  466. __kvm_hyp_panic:
  467. // Guess the context by looking at VTTBR:
  468. // If zero, then we're already a host.
  469. // Otherwise restore a minimal host context before panicing.
  470. mrs x0, vttbr_el2
  471. cbz x0, 1f
  472. mrs x0, tpidr_el2
  473. deactivate_traps
  474. deactivate_vm
  475. ldr x2, [x0, #VCPU_HOST_CONTEXT]
  476. kern_hyp_va x2
  477. bl __restore_sysregs
  478. 1: adr x0, __hyp_panic_str
  479. adr x1, 2f
  480. ldp x2, x3, [x1]
  481. sub x0, x0, x2
  482. add x0, x0, x3
  483. mrs x1, spsr_el2
  484. mrs x2, elr_el2
  485. mrs x3, esr_el2
  486. mrs x4, far_el2
  487. mrs x5, hpfar_el2
  488. mrs x6, par_el1
  489. mrs x7, tpidr_el2
  490. mov lr, #(PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT |\
  491. PSR_MODE_EL1h)
  492. msr spsr_el2, lr
  493. ldr lr, =panic
  494. msr elr_el2, lr
  495. eret
  496. .align 3
  497. 2: .quad HYP_PAGE_OFFSET
  498. .quad PAGE_OFFSET
  499. ENDPROC(__kvm_hyp_panic)
  500. __hyp_panic_str:
  501. .ascii "HYP panic:\nPS:%08x PC:%p ESR:%p\nFAR:%p HPFAR:%p PAR:%p\nVCPU:%p\n\0"
  502. .align 2
  503. ENTRY(kvm_call_hyp)
  504. hvc #0
  505. ret
  506. ENDPROC(kvm_call_hyp)
  507. .macro invalid_vector label, target
  508. .align 2
  509. \label:
  510. b \target
  511. ENDPROC(\label)
  512. .endm
  513. /* None of these should ever happen */
  514. invalid_vector el2t_sync_invalid, __kvm_hyp_panic
  515. invalid_vector el2t_irq_invalid, __kvm_hyp_panic
  516. invalid_vector el2t_fiq_invalid, __kvm_hyp_panic
  517. invalid_vector el2t_error_invalid, __kvm_hyp_panic
  518. invalid_vector el2h_sync_invalid, __kvm_hyp_panic
  519. invalid_vector el2h_irq_invalid, __kvm_hyp_panic
  520. invalid_vector el2h_fiq_invalid, __kvm_hyp_panic
  521. invalid_vector el2h_error_invalid, __kvm_hyp_panic
  522. invalid_vector el1_sync_invalid, __kvm_hyp_panic
  523. invalid_vector el1_irq_invalid, __kvm_hyp_panic
  524. invalid_vector el1_fiq_invalid, __kvm_hyp_panic
  525. invalid_vector el1_error_invalid, __kvm_hyp_panic
  526. el1_sync: // Guest trapped into EL2
  527. push x0, x1
  528. push x2, x3
  529. mrs x1, esr_el2
  530. lsr x2, x1, #ESR_EL2_EC_SHIFT
  531. cmp x2, #ESR_EL2_EC_HVC64
  532. b.ne el1_trap
  533. mrs x3, vttbr_el2 // If vttbr is valid, the 64bit guest
  534. cbnz x3, el1_trap // called HVC
  535. /* Here, we're pretty sure the host called HVC. */
  536. pop x2, x3
  537. pop x0, x1
  538. push lr, xzr
  539. /*
  540. * Compute the function address in EL2, and shuffle the parameters.
  541. */
  542. kern_hyp_va x0
  543. mov lr, x0
  544. mov x0, x1
  545. mov x1, x2
  546. mov x2, x3
  547. blr lr
  548. pop lr, xzr
  549. eret
  550. el1_trap:
  551. /*
  552. * x1: ESR
  553. * x2: ESR_EC
  554. */
  555. cmp x2, #ESR_EL2_EC_DABT
  556. mov x0, #ESR_EL2_EC_IABT
  557. ccmp x2, x0, #4, ne
  558. b.ne 1f // Not an abort we care about
  559. /* This is an abort. Check for permission fault */
  560. and x2, x1, #ESR_EL2_FSC_TYPE
  561. cmp x2, #FSC_PERM
  562. b.ne 1f // Not a permission fault
  563. /*
  564. * Check for Stage-1 page table walk, which is guaranteed
  565. * to give a valid HPFAR_EL2.
  566. */
  567. tbnz x1, #7, 1f // S1PTW is set
  568. /*
  569. * Permission fault, HPFAR_EL2 is invalid.
  570. * Resolve the IPA the hard way using the guest VA.
  571. * Stage-1 translation already validated the memory access rights.
  572. * As such, we can use the EL1 translation regime, and don't have
  573. * to distinguish between EL0 and EL1 access.
  574. */
  575. mrs x2, far_el2
  576. at s1e1r, x2
  577. isb
  578. /* Read result */
  579. mrs x3, par_el1
  580. tbnz x3, #0, 3f // Bail out if we failed the translation
  581. ubfx x3, x3, #12, #36 // Extract IPA
  582. lsl x3, x3, #4 // and present it like HPFAR
  583. b 2f
  584. 1: mrs x3, hpfar_el2
  585. mrs x2, far_el2
  586. 2: mrs x0, tpidr_el2
  587. str x1, [x0, #VCPU_ESR_EL2]
  588. str x2, [x0, #VCPU_FAR_EL2]
  589. str x3, [x0, #VCPU_HPFAR_EL2]
  590. mov x1, #ARM_EXCEPTION_TRAP
  591. b __kvm_vcpu_return
  592. /*
  593. * Translation failed. Just return to the guest and
  594. * let it fault again. Another CPU is probably playing
  595. * behind our back.
  596. */
  597. 3: pop x2, x3
  598. pop x0, x1
  599. eret
  600. el1_irq:
  601. push x0, x1
  602. push x2, x3
  603. mrs x0, tpidr_el2
  604. mov x1, #ARM_EXCEPTION_IRQ
  605. b __kvm_vcpu_return
  606. .ltorg
  607. .align 11
  608. ENTRY(__kvm_hyp_vector)
  609. ventry el2t_sync_invalid // Synchronous EL2t
  610. ventry el2t_irq_invalid // IRQ EL2t
  611. ventry el2t_fiq_invalid // FIQ EL2t
  612. ventry el2t_error_invalid // Error EL2t
  613. ventry el2h_sync_invalid // Synchronous EL2h
  614. ventry el2h_irq_invalid // IRQ EL2h
  615. ventry el2h_fiq_invalid // FIQ EL2h
  616. ventry el2h_error_invalid // Error EL2h
  617. ventry el1_sync // Synchronous 64-bit EL1
  618. ventry el1_irq // IRQ 64-bit EL1
  619. ventry el1_fiq_invalid // FIQ 64-bit EL1
  620. ventry el1_error_invalid // Error 64-bit EL1
  621. ventry el1_sync // Synchronous 32-bit EL1
  622. ventry el1_irq // IRQ 32-bit EL1
  623. ventry el1_fiq_invalid // FIQ 32-bit EL1
  624. ventry el1_error_invalid // Error 32-bit EL1
  625. ENDPROC(__kvm_hyp_vector)
  626. __kvm_hyp_code_end:
  627. .globl __kvm_hyp_code_end
  628. .popsection