hyp.S 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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. CPU_BE( rev w4, w4 )
  344. CPU_BE( rev w5, w5 )
  345. CPU_BE( rev w6, w6 )
  346. CPU_BE( rev w7, w7 )
  347. CPU_BE( rev w8, w8 )
  348. CPU_BE( rev w9, w9 )
  349. CPU_BE( rev w10, w10 )
  350. CPU_BE( rev w11, w11 )
  351. str w4, [x3, #VGIC_CPU_HCR]
  352. str w5, [x3, #VGIC_CPU_VMCR]
  353. str w6, [x3, #VGIC_CPU_MISR]
  354. str w7, [x3, #VGIC_CPU_EISR]
  355. str w8, [x3, #(VGIC_CPU_EISR + 4)]
  356. str w9, [x3, #VGIC_CPU_ELRSR]
  357. str w10, [x3, #(VGIC_CPU_ELRSR + 4)]
  358. str w11, [x3, #VGIC_CPU_APR]
  359. /* Clear GICH_HCR */
  360. str wzr, [x2, #GICH_HCR]
  361. /* Save list registers */
  362. add x2, x2, #GICH_LR0
  363. ldr w4, [x3, #VGIC_CPU_NR_LR]
  364. add x3, x3, #VGIC_CPU_LR
  365. 1: ldr w5, [x2], #4
  366. CPU_BE( rev w5, w5 )
  367. str w5, [x3], #4
  368. sub w4, w4, #1
  369. cbnz w4, 1b
  370. 2:
  371. .endm
  372. /*
  373. * Restore the VGIC CPU state from memory
  374. * x0: Register pointing to VCPU struct
  375. */
  376. .macro restore_vgic_state
  377. /* Get VGIC VCTRL base into x2 */
  378. ldr x2, [x0, #VCPU_KVM]
  379. kern_hyp_va x2
  380. ldr x2, [x2, #KVM_VGIC_VCTRL]
  381. kern_hyp_va x2
  382. cbz x2, 2f // disabled
  383. /* Compute the address of struct vgic_cpu */
  384. add x3, x0, #VCPU_VGIC_CPU
  385. /* We only restore a minimal set of registers */
  386. ldr w4, [x3, #VGIC_CPU_HCR]
  387. ldr w5, [x3, #VGIC_CPU_VMCR]
  388. ldr w6, [x3, #VGIC_CPU_APR]
  389. CPU_BE( rev w4, w4 )
  390. CPU_BE( rev w5, w5 )
  391. CPU_BE( rev w6, w6 )
  392. str w4, [x2, #GICH_HCR]
  393. str w5, [x2, #GICH_VMCR]
  394. str w6, [x2, #GICH_APR]
  395. /* Restore list registers */
  396. add x2, x2, #GICH_LR0
  397. ldr w4, [x3, #VGIC_CPU_NR_LR]
  398. add x3, x3, #VGIC_CPU_LR
  399. 1: ldr w5, [x3], #4
  400. CPU_BE( rev w5, w5 )
  401. str w5, [x2], #4
  402. sub w4, w4, #1
  403. cbnz w4, 1b
  404. 2:
  405. .endm
  406. .macro save_timer_state
  407. // x0: vcpu pointer
  408. ldr x2, [x0, #VCPU_KVM]
  409. kern_hyp_va x2
  410. ldr w3, [x2, #KVM_TIMER_ENABLED]
  411. cbz w3, 1f
  412. mrs x3, cntv_ctl_el0
  413. and x3, x3, #3
  414. str w3, [x0, #VCPU_TIMER_CNTV_CTL]
  415. bic x3, x3, #1 // Clear Enable
  416. msr cntv_ctl_el0, x3
  417. isb
  418. mrs x3, cntv_cval_el0
  419. str x3, [x0, #VCPU_TIMER_CNTV_CVAL]
  420. 1:
  421. // Allow physical timer/counter access for the host
  422. mrs x2, cnthctl_el2
  423. orr x2, x2, #3
  424. msr cnthctl_el2, x2
  425. // Clear cntvoff for the host
  426. msr cntvoff_el2, xzr
  427. .endm
  428. .macro restore_timer_state
  429. // x0: vcpu pointer
  430. // Disallow physical timer access for the guest
  431. // Physical counter access is allowed
  432. mrs x2, cnthctl_el2
  433. orr x2, x2, #1
  434. bic x2, x2, #2
  435. msr cnthctl_el2, x2
  436. ldr x2, [x0, #VCPU_KVM]
  437. kern_hyp_va x2
  438. ldr w3, [x2, #KVM_TIMER_ENABLED]
  439. cbz w3, 1f
  440. ldr x3, [x2, #KVM_TIMER_CNTVOFF]
  441. msr cntvoff_el2, x3
  442. ldr x2, [x0, #VCPU_TIMER_CNTV_CVAL]
  443. msr cntv_cval_el0, x2
  444. isb
  445. ldr w2, [x0, #VCPU_TIMER_CNTV_CTL]
  446. and x2, x2, #3
  447. msr cntv_ctl_el0, x2
  448. 1:
  449. .endm
  450. __save_sysregs:
  451. save_sysregs
  452. ret
  453. __restore_sysregs:
  454. restore_sysregs
  455. ret
  456. __save_fpsimd:
  457. save_fpsimd
  458. ret
  459. __restore_fpsimd:
  460. restore_fpsimd
  461. ret
  462. /*
  463. * u64 __kvm_vcpu_run(struct kvm_vcpu *vcpu);
  464. *
  465. * This is the world switch. The first half of the function
  466. * deals with entering the guest, and anything from __kvm_vcpu_return
  467. * to the end of the function deals with reentering the host.
  468. * On the enter path, only x0 (vcpu pointer) must be preserved until
  469. * the last moment. On the exit path, x0 (vcpu pointer) and x1 (exception
  470. * code) must both be preserved until the epilogue.
  471. * In both cases, x2 points to the CPU context we're saving/restoring from/to.
  472. */
  473. ENTRY(__kvm_vcpu_run)
  474. kern_hyp_va x0
  475. msr tpidr_el2, x0 // Save the vcpu register
  476. // Host context
  477. ldr x2, [x0, #VCPU_HOST_CONTEXT]
  478. kern_hyp_va x2
  479. save_host_regs
  480. bl __save_fpsimd
  481. bl __save_sysregs
  482. activate_traps
  483. activate_vm
  484. restore_vgic_state
  485. restore_timer_state
  486. // Guest context
  487. add x2, x0, #VCPU_CONTEXT
  488. bl __restore_sysregs
  489. bl __restore_fpsimd
  490. restore_guest_32bit_state
  491. restore_guest_regs
  492. // That's it, no more messing around.
  493. eret
  494. __kvm_vcpu_return:
  495. // Assume x0 is the vcpu pointer, x1 the return code
  496. // Guest's x0-x3 are on the stack
  497. // Guest context
  498. add x2, x0, #VCPU_CONTEXT
  499. save_guest_regs
  500. bl __save_fpsimd
  501. bl __save_sysregs
  502. save_guest_32bit_state
  503. save_timer_state
  504. save_vgic_state
  505. deactivate_traps
  506. deactivate_vm
  507. // Host context
  508. ldr x2, [x0, #VCPU_HOST_CONTEXT]
  509. kern_hyp_va x2
  510. bl __restore_sysregs
  511. bl __restore_fpsimd
  512. restore_host_regs
  513. mov x0, x1
  514. ret
  515. END(__kvm_vcpu_run)
  516. // void __kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa);
  517. ENTRY(__kvm_tlb_flush_vmid_ipa)
  518. dsb ishst
  519. kern_hyp_va x0
  520. ldr x2, [x0, #KVM_VTTBR]
  521. msr vttbr_el2, x2
  522. isb
  523. /*
  524. * We could do so much better if we had the VA as well.
  525. * Instead, we invalidate Stage-2 for this IPA, and the
  526. * whole of Stage-1. Weep...
  527. */
  528. tlbi ipas2e1is, x1
  529. dsb sy
  530. tlbi vmalle1is
  531. dsb sy
  532. isb
  533. msr vttbr_el2, xzr
  534. ret
  535. ENDPROC(__kvm_tlb_flush_vmid_ipa)
  536. ENTRY(__kvm_flush_vm_context)
  537. dsb ishst
  538. tlbi alle1is
  539. ic ialluis
  540. dsb sy
  541. ret
  542. ENDPROC(__kvm_flush_vm_context)
  543. __kvm_hyp_panic:
  544. // Guess the context by looking at VTTBR:
  545. // If zero, then we're already a host.
  546. // Otherwise restore a minimal host context before panicing.
  547. mrs x0, vttbr_el2
  548. cbz x0, 1f
  549. mrs x0, tpidr_el2
  550. deactivate_traps
  551. deactivate_vm
  552. ldr x2, [x0, #VCPU_HOST_CONTEXT]
  553. kern_hyp_va x2
  554. bl __restore_sysregs
  555. 1: adr x0, __hyp_panic_str
  556. adr x1, 2f
  557. ldp x2, x3, [x1]
  558. sub x0, x0, x2
  559. add x0, x0, x3
  560. mrs x1, spsr_el2
  561. mrs x2, elr_el2
  562. mrs x3, esr_el2
  563. mrs x4, far_el2
  564. mrs x5, hpfar_el2
  565. mrs x6, par_el1
  566. mrs x7, tpidr_el2
  567. mov lr, #(PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT |\
  568. PSR_MODE_EL1h)
  569. msr spsr_el2, lr
  570. ldr lr, =panic
  571. msr elr_el2, lr
  572. eret
  573. .align 3
  574. 2: .quad HYP_PAGE_OFFSET
  575. .quad PAGE_OFFSET
  576. ENDPROC(__kvm_hyp_panic)
  577. __hyp_panic_str:
  578. .ascii "HYP panic:\nPS:%08x PC:%p ESR:%p\nFAR:%p HPFAR:%p PAR:%p\nVCPU:%p\n\0"
  579. .align 2
  580. ENTRY(kvm_call_hyp)
  581. hvc #0
  582. ret
  583. ENDPROC(kvm_call_hyp)
  584. .macro invalid_vector label, target
  585. .align 2
  586. \label:
  587. b \target
  588. ENDPROC(\label)
  589. .endm
  590. /* None of these should ever happen */
  591. invalid_vector el2t_sync_invalid, __kvm_hyp_panic
  592. invalid_vector el2t_irq_invalid, __kvm_hyp_panic
  593. invalid_vector el2t_fiq_invalid, __kvm_hyp_panic
  594. invalid_vector el2t_error_invalid, __kvm_hyp_panic
  595. invalid_vector el2h_sync_invalid, __kvm_hyp_panic
  596. invalid_vector el2h_irq_invalid, __kvm_hyp_panic
  597. invalid_vector el2h_fiq_invalid, __kvm_hyp_panic
  598. invalid_vector el2h_error_invalid, __kvm_hyp_panic
  599. invalid_vector el1_sync_invalid, __kvm_hyp_panic
  600. invalid_vector el1_irq_invalid, __kvm_hyp_panic
  601. invalid_vector el1_fiq_invalid, __kvm_hyp_panic
  602. invalid_vector el1_error_invalid, __kvm_hyp_panic
  603. el1_sync: // Guest trapped into EL2
  604. push x0, x1
  605. push x2, x3
  606. mrs x1, esr_el2
  607. lsr x2, x1, #ESR_EL2_EC_SHIFT
  608. cmp x2, #ESR_EL2_EC_HVC64
  609. b.ne el1_trap
  610. mrs x3, vttbr_el2 // If vttbr is valid, the 64bit guest
  611. cbnz x3, el1_trap // called HVC
  612. /* Here, we're pretty sure the host called HVC. */
  613. pop x2, x3
  614. pop x0, x1
  615. push lr, xzr
  616. /*
  617. * Compute the function address in EL2, and shuffle the parameters.
  618. */
  619. kern_hyp_va x0
  620. mov lr, x0
  621. mov x0, x1
  622. mov x1, x2
  623. mov x2, x3
  624. blr lr
  625. pop lr, xzr
  626. eret
  627. el1_trap:
  628. /*
  629. * x1: ESR
  630. * x2: ESR_EC
  631. */
  632. cmp x2, #ESR_EL2_EC_DABT
  633. mov x0, #ESR_EL2_EC_IABT
  634. ccmp x2, x0, #4, ne
  635. b.ne 1f // Not an abort we care about
  636. /* This is an abort. Check for permission fault */
  637. and x2, x1, #ESR_EL2_FSC_TYPE
  638. cmp x2, #FSC_PERM
  639. b.ne 1f // Not a permission fault
  640. /*
  641. * Check for Stage-1 page table walk, which is guaranteed
  642. * to give a valid HPFAR_EL2.
  643. */
  644. tbnz x1, #7, 1f // S1PTW is set
  645. /* Preserve PAR_EL1 */
  646. mrs x3, par_el1
  647. push x3, xzr
  648. /*
  649. * Permission fault, HPFAR_EL2 is invalid.
  650. * Resolve the IPA the hard way using the guest VA.
  651. * Stage-1 translation already validated the memory access rights.
  652. * As such, we can use the EL1 translation regime, and don't have
  653. * to distinguish between EL0 and EL1 access.
  654. */
  655. mrs x2, far_el2
  656. at s1e1r, x2
  657. isb
  658. /* Read result */
  659. mrs x3, par_el1
  660. pop x0, xzr // Restore PAR_EL1 from the stack
  661. msr par_el1, x0
  662. tbnz x3, #0, 3f // Bail out if we failed the translation
  663. ubfx x3, x3, #12, #36 // Extract IPA
  664. lsl x3, x3, #4 // and present it like HPFAR
  665. b 2f
  666. 1: mrs x3, hpfar_el2
  667. mrs x2, far_el2
  668. 2: mrs x0, tpidr_el2
  669. str x1, [x0, #VCPU_ESR_EL2]
  670. str x2, [x0, #VCPU_FAR_EL2]
  671. str x3, [x0, #VCPU_HPFAR_EL2]
  672. mov x1, #ARM_EXCEPTION_TRAP
  673. b __kvm_vcpu_return
  674. /*
  675. * Translation failed. Just return to the guest and
  676. * let it fault again. Another CPU is probably playing
  677. * behind our back.
  678. */
  679. 3: pop x2, x3
  680. pop x0, x1
  681. eret
  682. el1_irq:
  683. push x0, x1
  684. push x2, x3
  685. mrs x0, tpidr_el2
  686. mov x1, #ARM_EXCEPTION_IRQ
  687. b __kvm_vcpu_return
  688. .ltorg
  689. .align 11
  690. ENTRY(__kvm_hyp_vector)
  691. ventry el2t_sync_invalid // Synchronous EL2t
  692. ventry el2t_irq_invalid // IRQ EL2t
  693. ventry el2t_fiq_invalid // FIQ EL2t
  694. ventry el2t_error_invalid // Error EL2t
  695. ventry el2h_sync_invalid // Synchronous EL2h
  696. ventry el2h_irq_invalid // IRQ EL2h
  697. ventry el2h_fiq_invalid // FIQ EL2h
  698. ventry el2h_error_invalid // Error EL2h
  699. ventry el1_sync // Synchronous 64-bit EL1
  700. ventry el1_irq // IRQ 64-bit EL1
  701. ventry el1_fiq_invalid // FIQ 64-bit EL1
  702. ventry el1_error_invalid // Error 64-bit EL1
  703. ventry el1_sync // Synchronous 32-bit EL1
  704. ventry el1_irq // IRQ 32-bit EL1
  705. ventry el1_fiq_invalid // FIQ 32-bit EL1
  706. ventry el1_error_invalid // Error 32-bit EL1
  707. ENDPROC(__kvm_hyp_vector)
  708. __kvm_hyp_code_end:
  709. .globl __kvm_hyp_code_end
  710. .popsection