hyp.S 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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. mrs x24, par_el1
  183. stp x4, x5, [x3]
  184. stp x6, x7, [x3, #16]
  185. stp x8, x9, [x3, #32]
  186. stp x10, x11, [x3, #48]
  187. stp x12, x13, [x3, #64]
  188. stp x14, x15, [x3, #80]
  189. stp x16, x17, [x3, #96]
  190. stp x18, x19, [x3, #112]
  191. stp x20, x21, [x3, #128]
  192. stp x22, x23, [x3, #144]
  193. str x24, [x3, #160]
  194. .endm
  195. .macro restore_sysregs
  196. // x2: base address for cpu context
  197. // x3: tmp register
  198. add x3, x2, #CPU_SYSREG_OFFSET(MPIDR_EL1)
  199. ldp x4, x5, [x3]
  200. ldp x6, x7, [x3, #16]
  201. ldp x8, x9, [x3, #32]
  202. ldp x10, x11, [x3, #48]
  203. ldp x12, x13, [x3, #64]
  204. ldp x14, x15, [x3, #80]
  205. ldp x16, x17, [x3, #96]
  206. ldp x18, x19, [x3, #112]
  207. ldp x20, x21, [x3, #128]
  208. ldp x22, x23, [x3, #144]
  209. ldr x24, [x3, #160]
  210. msr vmpidr_el2, x4
  211. msr csselr_el1, x5
  212. msr sctlr_el1, x6
  213. msr actlr_el1, x7
  214. msr cpacr_el1, x8
  215. msr ttbr0_el1, x9
  216. msr ttbr1_el1, x10
  217. msr tcr_el1, x11
  218. msr esr_el1, x12
  219. msr afsr0_el1, x13
  220. msr afsr1_el1, x14
  221. msr far_el1, x15
  222. msr mair_el1, x16
  223. msr vbar_el1, x17
  224. msr contextidr_el1, x18
  225. msr tpidr_el0, x19
  226. msr tpidrro_el0, x20
  227. msr tpidr_el1, x21
  228. msr amair_el1, x22
  229. msr cntkctl_el1, x23
  230. msr par_el1, x24
  231. .endm
  232. .macro skip_32bit_state tmp, target
  233. // Skip 32bit state if not needed
  234. mrs \tmp, hcr_el2
  235. tbnz \tmp, #HCR_RW_SHIFT, \target
  236. .endm
  237. .macro skip_tee_state tmp, target
  238. // Skip ThumbEE state if not needed
  239. mrs \tmp, id_pfr0_el1
  240. tbz \tmp, #12, \target
  241. .endm
  242. .macro save_guest_32bit_state
  243. skip_32bit_state x3, 1f
  244. add x3, x2, #CPU_SPSR_OFFSET(KVM_SPSR_ABT)
  245. mrs x4, spsr_abt
  246. mrs x5, spsr_und
  247. mrs x6, spsr_irq
  248. mrs x7, spsr_fiq
  249. stp x4, x5, [x3]
  250. stp x6, x7, [x3, #16]
  251. add x3, x2, #CPU_SYSREG_OFFSET(DACR32_EL2)
  252. mrs x4, dacr32_el2
  253. mrs x5, ifsr32_el2
  254. mrs x6, fpexc32_el2
  255. mrs x7, dbgvcr32_el2
  256. stp x4, x5, [x3]
  257. stp x6, x7, [x3, #16]
  258. skip_tee_state x8, 1f
  259. add x3, x2, #CPU_SYSREG_OFFSET(TEECR32_EL1)
  260. mrs x4, teecr32_el1
  261. mrs x5, teehbr32_el1
  262. stp x4, x5, [x3]
  263. 1:
  264. .endm
  265. .macro restore_guest_32bit_state
  266. skip_32bit_state x3, 1f
  267. add x3, x2, #CPU_SPSR_OFFSET(KVM_SPSR_ABT)
  268. ldp x4, x5, [x3]
  269. ldp x6, x7, [x3, #16]
  270. msr spsr_abt, x4
  271. msr spsr_und, x5
  272. msr spsr_irq, x6
  273. msr spsr_fiq, x7
  274. add x3, x2, #CPU_SYSREG_OFFSET(DACR32_EL2)
  275. ldp x4, x5, [x3]
  276. ldp x6, x7, [x3, #16]
  277. msr dacr32_el2, x4
  278. msr ifsr32_el2, x5
  279. msr fpexc32_el2, x6
  280. msr dbgvcr32_el2, x7
  281. skip_tee_state x8, 1f
  282. add x3, x2, #CPU_SYSREG_OFFSET(TEECR32_EL1)
  283. ldp x4, x5, [x3]
  284. msr teecr32_el1, x4
  285. msr teehbr32_el1, x5
  286. 1:
  287. .endm
  288. .macro activate_traps
  289. ldr x2, [x0, #VCPU_IRQ_LINES]
  290. ldr x1, [x0, #VCPU_HCR_EL2]
  291. orr x2, x2, x1
  292. msr hcr_el2, x2
  293. ldr x2, =(CPTR_EL2_TTA)
  294. msr cptr_el2, x2
  295. ldr x2, =(1 << 15) // Trap CP15 Cr=15
  296. msr hstr_el2, x2
  297. mrs x2, mdcr_el2
  298. and x2, x2, #MDCR_EL2_HPMN_MASK
  299. orr x2, x2, #(MDCR_EL2_TPM | MDCR_EL2_TPMCR)
  300. msr mdcr_el2, x2
  301. .endm
  302. .macro deactivate_traps
  303. mov x2, #HCR_RW
  304. msr hcr_el2, x2
  305. msr cptr_el2, xzr
  306. msr hstr_el2, xzr
  307. mrs x2, mdcr_el2
  308. and x2, x2, #MDCR_EL2_HPMN_MASK
  309. msr mdcr_el2, x2
  310. .endm
  311. .macro activate_vm
  312. ldr x1, [x0, #VCPU_KVM]
  313. kern_hyp_va x1
  314. ldr x2, [x1, #KVM_VTTBR]
  315. msr vttbr_el2, x2
  316. .endm
  317. .macro deactivate_vm
  318. msr vttbr_el2, xzr
  319. .endm
  320. /*
  321. * Save the VGIC CPU state into memory
  322. * x0: Register pointing to VCPU struct
  323. * Do not corrupt x1!!!
  324. */
  325. .macro save_vgic_state
  326. /* Get VGIC VCTRL base into x2 */
  327. ldr x2, [x0, #VCPU_KVM]
  328. kern_hyp_va x2
  329. ldr x2, [x2, #KVM_VGIC_VCTRL]
  330. kern_hyp_va x2
  331. cbz x2, 2f // disabled
  332. /* Compute the address of struct vgic_cpu */
  333. add x3, x0, #VCPU_VGIC_CPU
  334. /* Save all interesting registers */
  335. ldr w4, [x2, #GICH_HCR]
  336. ldr w5, [x2, #GICH_VMCR]
  337. ldr w6, [x2, #GICH_MISR]
  338. ldr w7, [x2, #GICH_EISR0]
  339. ldr w8, [x2, #GICH_EISR1]
  340. ldr w9, [x2, #GICH_ELRSR0]
  341. ldr w10, [x2, #GICH_ELRSR1]
  342. ldr w11, [x2, #GICH_APR]
  343. str w4, [x3, #VGIC_CPU_HCR]
  344. str w5, [x3, #VGIC_CPU_VMCR]
  345. str w6, [x3, #VGIC_CPU_MISR]
  346. str w7, [x3, #VGIC_CPU_EISR]
  347. str w8, [x3, #(VGIC_CPU_EISR + 4)]
  348. str w9, [x3, #VGIC_CPU_ELRSR]
  349. str w10, [x3, #(VGIC_CPU_ELRSR + 4)]
  350. str w11, [x3, #VGIC_CPU_APR]
  351. /* Clear GICH_HCR */
  352. str wzr, [x2, #GICH_HCR]
  353. /* Save list registers */
  354. add x2, x2, #GICH_LR0
  355. ldr w4, [x3, #VGIC_CPU_NR_LR]
  356. add x3, x3, #VGIC_CPU_LR
  357. 1: ldr w5, [x2], #4
  358. str w5, [x3], #4
  359. sub w4, w4, #1
  360. cbnz w4, 1b
  361. 2:
  362. .endm
  363. /*
  364. * Restore the VGIC CPU state from memory
  365. * x0: Register pointing to VCPU struct
  366. */
  367. .macro restore_vgic_state
  368. /* Get VGIC VCTRL base into x2 */
  369. ldr x2, [x0, #VCPU_KVM]
  370. kern_hyp_va x2
  371. ldr x2, [x2, #KVM_VGIC_VCTRL]
  372. kern_hyp_va x2
  373. cbz x2, 2f // disabled
  374. /* Compute the address of struct vgic_cpu */
  375. add x3, x0, #VCPU_VGIC_CPU
  376. /* We only restore a minimal set of registers */
  377. ldr w4, [x3, #VGIC_CPU_HCR]
  378. ldr w5, [x3, #VGIC_CPU_VMCR]
  379. ldr w6, [x3, #VGIC_CPU_APR]
  380. str w4, [x2, #GICH_HCR]
  381. str w5, [x2, #GICH_VMCR]
  382. str w6, [x2, #GICH_APR]
  383. /* Restore list registers */
  384. add x2, x2, #GICH_LR0
  385. ldr w4, [x3, #VGIC_CPU_NR_LR]
  386. add x3, x3, #VGIC_CPU_LR
  387. 1: ldr w5, [x3], #4
  388. str w5, [x2], #4
  389. sub w4, w4, #1
  390. cbnz w4, 1b
  391. 2:
  392. .endm
  393. .macro save_timer_state
  394. // x0: vcpu pointer
  395. ldr x2, [x0, #VCPU_KVM]
  396. kern_hyp_va x2
  397. ldr w3, [x2, #KVM_TIMER_ENABLED]
  398. cbz w3, 1f
  399. mrs x3, cntv_ctl_el0
  400. and x3, x3, #3
  401. str w3, [x0, #VCPU_TIMER_CNTV_CTL]
  402. bic x3, x3, #1 // Clear Enable
  403. msr cntv_ctl_el0, x3
  404. isb
  405. mrs x3, cntv_cval_el0
  406. str x3, [x0, #VCPU_TIMER_CNTV_CVAL]
  407. 1:
  408. // Allow physical timer/counter access for the host
  409. mrs x2, cnthctl_el2
  410. orr x2, x2, #3
  411. msr cnthctl_el2, x2
  412. // Clear cntvoff for the host
  413. msr cntvoff_el2, xzr
  414. .endm
  415. .macro restore_timer_state
  416. // x0: vcpu pointer
  417. // Disallow physical timer access for the guest
  418. // Physical counter access is allowed
  419. mrs x2, cnthctl_el2
  420. orr x2, x2, #1
  421. bic x2, x2, #2
  422. msr cnthctl_el2, x2
  423. ldr x2, [x0, #VCPU_KVM]
  424. kern_hyp_va x2
  425. ldr w3, [x2, #KVM_TIMER_ENABLED]
  426. cbz w3, 1f
  427. ldr x3, [x2, #KVM_TIMER_CNTVOFF]
  428. msr cntvoff_el2, x3
  429. ldr x2, [x0, #VCPU_TIMER_CNTV_CVAL]
  430. msr cntv_cval_el0, x2
  431. isb
  432. ldr w2, [x0, #VCPU_TIMER_CNTV_CTL]
  433. and x2, x2, #3
  434. msr cntv_ctl_el0, x2
  435. 1:
  436. .endm
  437. __save_sysregs:
  438. save_sysregs
  439. ret
  440. __restore_sysregs:
  441. restore_sysregs
  442. ret
  443. __save_fpsimd:
  444. save_fpsimd
  445. ret
  446. __restore_fpsimd:
  447. restore_fpsimd
  448. ret
  449. /*
  450. * u64 __kvm_vcpu_run(struct kvm_vcpu *vcpu);
  451. *
  452. * This is the world switch. The first half of the function
  453. * deals with entering the guest, and anything from __kvm_vcpu_return
  454. * to the end of the function deals with reentering the host.
  455. * On the enter path, only x0 (vcpu pointer) must be preserved until
  456. * the last moment. On the exit path, x0 (vcpu pointer) and x1 (exception
  457. * code) must both be preserved until the epilogue.
  458. * In both cases, x2 points to the CPU context we're saving/restoring from/to.
  459. */
  460. ENTRY(__kvm_vcpu_run)
  461. kern_hyp_va x0
  462. msr tpidr_el2, x0 // Save the vcpu register
  463. // Host context
  464. ldr x2, [x0, #VCPU_HOST_CONTEXT]
  465. kern_hyp_va x2
  466. save_host_regs
  467. bl __save_fpsimd
  468. bl __save_sysregs
  469. activate_traps
  470. activate_vm
  471. restore_vgic_state
  472. restore_timer_state
  473. // Guest context
  474. add x2, x0, #VCPU_CONTEXT
  475. bl __restore_sysregs
  476. bl __restore_fpsimd
  477. restore_guest_32bit_state
  478. restore_guest_regs
  479. // That's it, no more messing around.
  480. eret
  481. __kvm_vcpu_return:
  482. // Assume x0 is the vcpu pointer, x1 the return code
  483. // Guest's x0-x3 are on the stack
  484. // Guest context
  485. add x2, x0, #VCPU_CONTEXT
  486. save_guest_regs
  487. bl __save_fpsimd
  488. bl __save_sysregs
  489. save_guest_32bit_state
  490. save_timer_state
  491. save_vgic_state
  492. deactivate_traps
  493. deactivate_vm
  494. // Host context
  495. ldr x2, [x0, #VCPU_HOST_CONTEXT]
  496. kern_hyp_va x2
  497. bl __restore_sysregs
  498. bl __restore_fpsimd
  499. restore_host_regs
  500. mov x0, x1
  501. ret
  502. END(__kvm_vcpu_run)
  503. // void __kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa);
  504. ENTRY(__kvm_tlb_flush_vmid_ipa)
  505. dsb ishst
  506. kern_hyp_va x0
  507. ldr x2, [x0, #KVM_VTTBR]
  508. msr vttbr_el2, x2
  509. isb
  510. /*
  511. * We could do so much better if we had the VA as well.
  512. * Instead, we invalidate Stage-2 for this IPA, and the
  513. * whole of Stage-1. Weep...
  514. */
  515. tlbi ipas2e1is, x1
  516. dsb sy
  517. tlbi vmalle1is
  518. dsb sy
  519. isb
  520. msr vttbr_el2, xzr
  521. ret
  522. ENDPROC(__kvm_tlb_flush_vmid_ipa)
  523. ENTRY(__kvm_flush_vm_context)
  524. dsb ishst
  525. tlbi alle1is
  526. ic ialluis
  527. dsb sy
  528. ret
  529. ENDPROC(__kvm_flush_vm_context)
  530. __kvm_hyp_panic:
  531. // Guess the context by looking at VTTBR:
  532. // If zero, then we're already a host.
  533. // Otherwise restore a minimal host context before panicing.
  534. mrs x0, vttbr_el2
  535. cbz x0, 1f
  536. mrs x0, tpidr_el2
  537. deactivate_traps
  538. deactivate_vm
  539. ldr x2, [x0, #VCPU_HOST_CONTEXT]
  540. kern_hyp_va x2
  541. bl __restore_sysregs
  542. 1: adr x0, __hyp_panic_str
  543. adr x1, 2f
  544. ldp x2, x3, [x1]
  545. sub x0, x0, x2
  546. add x0, x0, x3
  547. mrs x1, spsr_el2
  548. mrs x2, elr_el2
  549. mrs x3, esr_el2
  550. mrs x4, far_el2
  551. mrs x5, hpfar_el2
  552. mrs x6, par_el1
  553. mrs x7, tpidr_el2
  554. mov lr, #(PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT |\
  555. PSR_MODE_EL1h)
  556. msr spsr_el2, lr
  557. ldr lr, =panic
  558. msr elr_el2, lr
  559. eret
  560. .align 3
  561. 2: .quad HYP_PAGE_OFFSET
  562. .quad PAGE_OFFSET
  563. ENDPROC(__kvm_hyp_panic)
  564. __hyp_panic_str:
  565. .ascii "HYP panic:\nPS:%08x PC:%p ESR:%p\nFAR:%p HPFAR:%p PAR:%p\nVCPU:%p\n\0"
  566. .align 2
  567. ENTRY(kvm_call_hyp)
  568. hvc #0
  569. ret
  570. ENDPROC(kvm_call_hyp)
  571. .macro invalid_vector label, target
  572. .align 2
  573. \label:
  574. b \target
  575. ENDPROC(\label)
  576. .endm
  577. /* None of these should ever happen */
  578. invalid_vector el2t_sync_invalid, __kvm_hyp_panic
  579. invalid_vector el2t_irq_invalid, __kvm_hyp_panic
  580. invalid_vector el2t_fiq_invalid, __kvm_hyp_panic
  581. invalid_vector el2t_error_invalid, __kvm_hyp_panic
  582. invalid_vector el2h_sync_invalid, __kvm_hyp_panic
  583. invalid_vector el2h_irq_invalid, __kvm_hyp_panic
  584. invalid_vector el2h_fiq_invalid, __kvm_hyp_panic
  585. invalid_vector el2h_error_invalid, __kvm_hyp_panic
  586. invalid_vector el1_sync_invalid, __kvm_hyp_panic
  587. invalid_vector el1_irq_invalid, __kvm_hyp_panic
  588. invalid_vector el1_fiq_invalid, __kvm_hyp_panic
  589. invalid_vector el1_error_invalid, __kvm_hyp_panic
  590. el1_sync: // Guest trapped into EL2
  591. push x0, x1
  592. push x2, x3
  593. mrs x1, esr_el2
  594. lsr x2, x1, #ESR_EL2_EC_SHIFT
  595. cmp x2, #ESR_EL2_EC_HVC64
  596. b.ne el1_trap
  597. mrs x3, vttbr_el2 // If vttbr is valid, the 64bit guest
  598. cbnz x3, el1_trap // called HVC
  599. /* Here, we're pretty sure the host called HVC. */
  600. pop x2, x3
  601. pop x0, x1
  602. push lr, xzr
  603. /*
  604. * Compute the function address in EL2, and shuffle the parameters.
  605. */
  606. kern_hyp_va x0
  607. mov lr, x0
  608. mov x0, x1
  609. mov x1, x2
  610. mov x2, x3
  611. blr lr
  612. pop lr, xzr
  613. eret
  614. el1_trap:
  615. /*
  616. * x1: ESR
  617. * x2: ESR_EC
  618. */
  619. cmp x2, #ESR_EL2_EC_DABT
  620. mov x0, #ESR_EL2_EC_IABT
  621. ccmp x2, x0, #4, ne
  622. b.ne 1f // Not an abort we care about
  623. /* This is an abort. Check for permission fault */
  624. and x2, x1, #ESR_EL2_FSC_TYPE
  625. cmp x2, #FSC_PERM
  626. b.ne 1f // Not a permission fault
  627. /*
  628. * Check for Stage-1 page table walk, which is guaranteed
  629. * to give a valid HPFAR_EL2.
  630. */
  631. tbnz x1, #7, 1f // S1PTW is set
  632. /* Preserve PAR_EL1 */
  633. mrs x3, par_el1
  634. push x3, xzr
  635. /*
  636. * Permission fault, HPFAR_EL2 is invalid.
  637. * Resolve the IPA the hard way using the guest VA.
  638. * Stage-1 translation already validated the memory access rights.
  639. * As such, we can use the EL1 translation regime, and don't have
  640. * to distinguish between EL0 and EL1 access.
  641. */
  642. mrs x2, far_el2
  643. at s1e1r, x2
  644. isb
  645. /* Read result */
  646. mrs x3, par_el1
  647. pop x0, xzr // Restore PAR_EL1 from the stack
  648. msr par_el1, x0
  649. tbnz x3, #0, 3f // Bail out if we failed the translation
  650. ubfx x3, x3, #12, #36 // Extract IPA
  651. lsl x3, x3, #4 // and present it like HPFAR
  652. b 2f
  653. 1: mrs x3, hpfar_el2
  654. mrs x2, far_el2
  655. 2: mrs x0, tpidr_el2
  656. str x1, [x0, #VCPU_ESR_EL2]
  657. str x2, [x0, #VCPU_FAR_EL2]
  658. str x3, [x0, #VCPU_HPFAR_EL2]
  659. mov x1, #ARM_EXCEPTION_TRAP
  660. b __kvm_vcpu_return
  661. /*
  662. * Translation failed. Just return to the guest and
  663. * let it fault again. Another CPU is probably playing
  664. * behind our back.
  665. */
  666. 3: pop x2, x3
  667. pop x0, x1
  668. eret
  669. el1_irq:
  670. push x0, x1
  671. push x2, x3
  672. mrs x0, tpidr_el2
  673. mov x1, #ARM_EXCEPTION_IRQ
  674. b __kvm_vcpu_return
  675. .ltorg
  676. .align 11
  677. ENTRY(__kvm_hyp_vector)
  678. ventry el2t_sync_invalid // Synchronous EL2t
  679. ventry el2t_irq_invalid // IRQ EL2t
  680. ventry el2t_fiq_invalid // FIQ EL2t
  681. ventry el2t_error_invalid // Error EL2t
  682. ventry el2h_sync_invalid // Synchronous EL2h
  683. ventry el2h_irq_invalid // IRQ EL2h
  684. ventry el2h_fiq_invalid // FIQ EL2h
  685. ventry el2h_error_invalid // Error EL2h
  686. ventry el1_sync // Synchronous 64-bit EL1
  687. ventry el1_irq // IRQ 64-bit EL1
  688. ventry el1_fiq_invalid // FIQ 64-bit EL1
  689. ventry el1_error_invalid // Error 64-bit EL1
  690. ventry el1_sync // Synchronous 32-bit EL1
  691. ventry el1_irq // IRQ 32-bit EL1
  692. ventry el1_fiq_invalid // FIQ 32-bit EL1
  693. ventry el1_error_invalid // Error 32-bit EL1
  694. ENDPROC(__kvm_hyp_vector)
  695. __kvm_hyp_code_end:
  696. .globl __kvm_hyp_code_end
  697. .popsection