hyp.S 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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 <asm/alternative.h>
  19. #include <asm/asm-offsets.h>
  20. #include <asm/assembler.h>
  21. #include <asm/cpufeature.h>
  22. #include <asm/debug-monitors.h>
  23. #include <asm/esr.h>
  24. #include <asm/fpsimdmacros.h>
  25. #include <asm/kvm.h>
  26. #include <asm/kvm_arm.h>
  27. #include <asm/kvm_asm.h>
  28. #include <asm/kvm_mmu.h>
  29. #include <asm/memory.h>
  30. #define CPU_GP_REG_OFFSET(x) (CPU_GP_REGS + x)
  31. #define CPU_XREG_OFFSET(x) CPU_GP_REG_OFFSET(CPU_USER_PT_REGS + 8*x)
  32. #define CPU_SPSR_OFFSET(x) CPU_GP_REG_OFFSET(CPU_SPSR + 8*x)
  33. #define CPU_SYSREG_OFFSET(x) (CPU_SYSREGS + 8*x)
  34. .text
  35. .pushsection .hyp.text, "ax"
  36. .align PAGE_SHIFT
  37. .macro save_common_regs
  38. // x2: base address for cpu context
  39. // x3: tmp register
  40. add x3, x2, #CPU_XREG_OFFSET(19)
  41. stp x19, x20, [x3]
  42. stp x21, x22, [x3, #16]
  43. stp x23, x24, [x3, #32]
  44. stp x25, x26, [x3, #48]
  45. stp x27, x28, [x3, #64]
  46. stp x29, lr, [x3, #80]
  47. mrs x19, sp_el0
  48. mrs x20, elr_el2 // pc before entering el2
  49. mrs x21, spsr_el2 // pstate before entering el2
  50. stp x19, x20, [x3, #96]
  51. str x21, [x3, #112]
  52. mrs x22, sp_el1
  53. mrs x23, elr_el1
  54. mrs x24, spsr_el1
  55. str x22, [x2, #CPU_GP_REG_OFFSET(CPU_SP_EL1)]
  56. str x23, [x2, #CPU_GP_REG_OFFSET(CPU_ELR_EL1)]
  57. str x24, [x2, #CPU_SPSR_OFFSET(KVM_SPSR_EL1)]
  58. .endm
  59. .macro restore_common_regs
  60. // x2: base address for cpu context
  61. // x3: tmp register
  62. ldr x22, [x2, #CPU_GP_REG_OFFSET(CPU_SP_EL1)]
  63. ldr x23, [x2, #CPU_GP_REG_OFFSET(CPU_ELR_EL1)]
  64. ldr x24, [x2, #CPU_SPSR_OFFSET(KVM_SPSR_EL1)]
  65. msr sp_el1, x22
  66. msr elr_el1, x23
  67. msr spsr_el1, x24
  68. add x3, x2, #CPU_XREG_OFFSET(31) // SP_EL0
  69. ldp x19, x20, [x3]
  70. ldr x21, [x3, #16]
  71. msr sp_el0, x19
  72. msr elr_el2, x20 // pc on return from el2
  73. msr spsr_el2, x21 // pstate on return from el2
  74. add x3, x2, #CPU_XREG_OFFSET(19)
  75. ldp x19, x20, [x3]
  76. ldp x21, x22, [x3, #16]
  77. ldp x23, x24, [x3, #32]
  78. ldp x25, x26, [x3, #48]
  79. ldp x27, x28, [x3, #64]
  80. ldp x29, lr, [x3, #80]
  81. .endm
  82. .macro save_host_regs
  83. save_common_regs
  84. .endm
  85. .macro restore_host_regs
  86. restore_common_regs
  87. .endm
  88. .macro save_fpsimd
  89. // x2: cpu context address
  90. // x3, x4: tmp regs
  91. add x3, x2, #CPU_GP_REG_OFFSET(CPU_FP_REGS)
  92. fpsimd_save x3, 4
  93. .endm
  94. .macro restore_fpsimd
  95. // x2: cpu context address
  96. // x3, x4: tmp regs
  97. add x3, x2, #CPU_GP_REG_OFFSET(CPU_FP_REGS)
  98. fpsimd_restore x3, 4
  99. .endm
  100. .macro save_guest_regs
  101. // x0 is the vcpu address
  102. // x1 is the return code, do not corrupt!
  103. // x2 is the cpu context
  104. // x3 is a tmp register
  105. // Guest's x0-x3 are on the stack
  106. // Compute base to save registers
  107. add x3, x2, #CPU_XREG_OFFSET(4)
  108. stp x4, x5, [x3]
  109. stp x6, x7, [x3, #16]
  110. stp x8, x9, [x3, #32]
  111. stp x10, x11, [x3, #48]
  112. stp x12, x13, [x3, #64]
  113. stp x14, x15, [x3, #80]
  114. stp x16, x17, [x3, #96]
  115. str x18, [x3, #112]
  116. pop x6, x7 // x2, x3
  117. pop x4, x5 // x0, x1
  118. add x3, x2, #CPU_XREG_OFFSET(0)
  119. stp x4, x5, [x3]
  120. stp x6, x7, [x3, #16]
  121. save_common_regs
  122. .endm
  123. .macro restore_guest_regs
  124. // x0 is the vcpu address.
  125. // x2 is the cpu context
  126. // x3 is a tmp register
  127. // Prepare x0-x3 for later restore
  128. add x3, x2, #CPU_XREG_OFFSET(0)
  129. ldp x4, x5, [x3]
  130. ldp x6, x7, [x3, #16]
  131. push x4, x5 // Push x0-x3 on the stack
  132. push x6, x7
  133. // x4-x18
  134. ldp x4, x5, [x3, #32]
  135. ldp x6, x7, [x3, #48]
  136. ldp x8, x9, [x3, #64]
  137. ldp x10, x11, [x3, #80]
  138. ldp x12, x13, [x3, #96]
  139. ldp x14, x15, [x3, #112]
  140. ldp x16, x17, [x3, #128]
  141. ldr x18, [x3, #144]
  142. // x19-x29, lr, sp*, elr*, spsr*
  143. restore_common_regs
  144. // Last bits of the 64bit state
  145. pop x2, x3
  146. pop x0, x1
  147. // Do not touch any register after this!
  148. .endm
  149. /*
  150. * Macros to perform system register save/restore.
  151. *
  152. * Ordering here is absolutely critical, and must be kept consistent
  153. * in {save,restore}_sysregs, {save,restore}_guest_32bit_state,
  154. * and in kvm_asm.h.
  155. *
  156. * In other words, don't touch any of these unless you know what
  157. * you are doing.
  158. */
  159. .macro save_sysregs
  160. // x2: base address for cpu context
  161. // x3: tmp register
  162. add x3, x2, #CPU_SYSREG_OFFSET(MPIDR_EL1)
  163. mrs x4, vmpidr_el2
  164. mrs x5, csselr_el1
  165. mrs x6, sctlr_el1
  166. mrs x7, actlr_el1
  167. mrs x8, cpacr_el1
  168. mrs x9, ttbr0_el1
  169. mrs x10, ttbr1_el1
  170. mrs x11, tcr_el1
  171. mrs x12, esr_el1
  172. mrs x13, afsr0_el1
  173. mrs x14, afsr1_el1
  174. mrs x15, far_el1
  175. mrs x16, mair_el1
  176. mrs x17, vbar_el1
  177. mrs x18, contextidr_el1
  178. mrs x19, tpidr_el0
  179. mrs x20, tpidrro_el0
  180. mrs x21, tpidr_el1
  181. mrs x22, amair_el1
  182. mrs x23, cntkctl_el1
  183. mrs x24, par_el1
  184. mrs x25, mdscr_el1
  185. stp x4, x5, [x3]
  186. stp x6, x7, [x3, #16]
  187. stp x8, x9, [x3, #32]
  188. stp x10, x11, [x3, #48]
  189. stp x12, x13, [x3, #64]
  190. stp x14, x15, [x3, #80]
  191. stp x16, x17, [x3, #96]
  192. stp x18, x19, [x3, #112]
  193. stp x20, x21, [x3, #128]
  194. stp x22, x23, [x3, #144]
  195. stp x24, x25, [x3, #160]
  196. .endm
  197. .macro save_debug type
  198. // x4: pointer to register set
  199. // x5: number of registers to skip
  200. // x6..x22 trashed
  201. adr x22, 1f
  202. add x22, x22, x5, lsl #2
  203. br x22
  204. 1:
  205. mrs x21, \type\()15_el1
  206. mrs x20, \type\()14_el1
  207. mrs x19, \type\()13_el1
  208. mrs x18, \type\()12_el1
  209. mrs x17, \type\()11_el1
  210. mrs x16, \type\()10_el1
  211. mrs x15, \type\()9_el1
  212. mrs x14, \type\()8_el1
  213. mrs x13, \type\()7_el1
  214. mrs x12, \type\()6_el1
  215. mrs x11, \type\()5_el1
  216. mrs x10, \type\()4_el1
  217. mrs x9, \type\()3_el1
  218. mrs x8, \type\()2_el1
  219. mrs x7, \type\()1_el1
  220. mrs x6, \type\()0_el1
  221. adr x22, 1f
  222. add x22, x22, x5, lsl #2
  223. br x22
  224. 1:
  225. str x21, [x4, #(15 * 8)]
  226. str x20, [x4, #(14 * 8)]
  227. str x19, [x4, #(13 * 8)]
  228. str x18, [x4, #(12 * 8)]
  229. str x17, [x4, #(11 * 8)]
  230. str x16, [x4, #(10 * 8)]
  231. str x15, [x4, #(9 * 8)]
  232. str x14, [x4, #(8 * 8)]
  233. str x13, [x4, #(7 * 8)]
  234. str x12, [x4, #(6 * 8)]
  235. str x11, [x4, #(5 * 8)]
  236. str x10, [x4, #(4 * 8)]
  237. str x9, [x4, #(3 * 8)]
  238. str x8, [x4, #(2 * 8)]
  239. str x7, [x4, #(1 * 8)]
  240. str x6, [x4, #(0 * 8)]
  241. .endm
  242. .macro restore_sysregs
  243. // x2: base address for cpu context
  244. // x3: tmp register
  245. add x3, x2, #CPU_SYSREG_OFFSET(MPIDR_EL1)
  246. ldp x4, x5, [x3]
  247. ldp x6, x7, [x3, #16]
  248. ldp x8, x9, [x3, #32]
  249. ldp x10, x11, [x3, #48]
  250. ldp x12, x13, [x3, #64]
  251. ldp x14, x15, [x3, #80]
  252. ldp x16, x17, [x3, #96]
  253. ldp x18, x19, [x3, #112]
  254. ldp x20, x21, [x3, #128]
  255. ldp x22, x23, [x3, #144]
  256. ldp x24, x25, [x3, #160]
  257. msr vmpidr_el2, x4
  258. msr csselr_el1, x5
  259. msr sctlr_el1, x6
  260. msr actlr_el1, x7
  261. msr cpacr_el1, x8
  262. msr ttbr0_el1, x9
  263. msr ttbr1_el1, x10
  264. msr tcr_el1, x11
  265. msr esr_el1, x12
  266. msr afsr0_el1, x13
  267. msr afsr1_el1, x14
  268. msr far_el1, x15
  269. msr mair_el1, x16
  270. msr vbar_el1, x17
  271. msr contextidr_el1, x18
  272. msr tpidr_el0, x19
  273. msr tpidrro_el0, x20
  274. msr tpidr_el1, x21
  275. msr amair_el1, x22
  276. msr cntkctl_el1, x23
  277. msr par_el1, x24
  278. msr mdscr_el1, x25
  279. .endm
  280. .macro restore_debug type
  281. // x4: pointer to register set
  282. // x5: number of registers to skip
  283. // x6..x22 trashed
  284. adr x22, 1f
  285. add x22, x22, x5, lsl #2
  286. br x22
  287. 1:
  288. ldr x21, [x4, #(15 * 8)]
  289. ldr x20, [x4, #(14 * 8)]
  290. ldr x19, [x4, #(13 * 8)]
  291. ldr x18, [x4, #(12 * 8)]
  292. ldr x17, [x4, #(11 * 8)]
  293. ldr x16, [x4, #(10 * 8)]
  294. ldr x15, [x4, #(9 * 8)]
  295. ldr x14, [x4, #(8 * 8)]
  296. ldr x13, [x4, #(7 * 8)]
  297. ldr x12, [x4, #(6 * 8)]
  298. ldr x11, [x4, #(5 * 8)]
  299. ldr x10, [x4, #(4 * 8)]
  300. ldr x9, [x4, #(3 * 8)]
  301. ldr x8, [x4, #(2 * 8)]
  302. ldr x7, [x4, #(1 * 8)]
  303. ldr x6, [x4, #(0 * 8)]
  304. adr x22, 1f
  305. add x22, x22, x5, lsl #2
  306. br x22
  307. 1:
  308. msr \type\()15_el1, x21
  309. msr \type\()14_el1, x20
  310. msr \type\()13_el1, x19
  311. msr \type\()12_el1, x18
  312. msr \type\()11_el1, x17
  313. msr \type\()10_el1, x16
  314. msr \type\()9_el1, x15
  315. msr \type\()8_el1, x14
  316. msr \type\()7_el1, x13
  317. msr \type\()6_el1, x12
  318. msr \type\()5_el1, x11
  319. msr \type\()4_el1, x10
  320. msr \type\()3_el1, x9
  321. msr \type\()2_el1, x8
  322. msr \type\()1_el1, x7
  323. msr \type\()0_el1, x6
  324. .endm
  325. .macro skip_32bit_state tmp, target
  326. // Skip 32bit state if not needed
  327. mrs \tmp, hcr_el2
  328. tbnz \tmp, #HCR_RW_SHIFT, \target
  329. .endm
  330. .macro skip_tee_state tmp, target
  331. // Skip ThumbEE state if not needed
  332. mrs \tmp, id_pfr0_el1
  333. tbz \tmp, #12, \target
  334. .endm
  335. .macro skip_debug_state tmp, target
  336. ldr \tmp, [x0, #VCPU_DEBUG_FLAGS]
  337. tbz \tmp, #KVM_ARM64_DEBUG_DIRTY_SHIFT, \target
  338. .endm
  339. /*
  340. * Branch to target if CPTR_EL2.TFP bit is set (VFP/SIMD trapping enabled)
  341. */
  342. .macro skip_fpsimd_state tmp, target
  343. mrs \tmp, cptr_el2
  344. tbnz \tmp, #CPTR_EL2_TFP_SHIFT, \target
  345. .endm
  346. .macro compute_debug_state target
  347. // Compute debug state: If any of KDE, MDE or KVM_ARM64_DEBUG_DIRTY
  348. // is set, we do a full save/restore cycle and disable trapping.
  349. add x25, x0, #VCPU_CONTEXT
  350. // Check the state of MDSCR_EL1
  351. ldr x25, [x25, #CPU_SYSREG_OFFSET(MDSCR_EL1)]
  352. and x26, x25, #DBG_MDSCR_KDE
  353. and x25, x25, #DBG_MDSCR_MDE
  354. adds xzr, x25, x26
  355. b.eq 9998f // Nothing to see there
  356. // If any interesting bits was set, we must set the flag
  357. mov x26, #KVM_ARM64_DEBUG_DIRTY
  358. str x26, [x0, #VCPU_DEBUG_FLAGS]
  359. b 9999f // Don't skip restore
  360. 9998:
  361. // Otherwise load the flags from memory in case we recently
  362. // trapped
  363. skip_debug_state x25, \target
  364. 9999:
  365. .endm
  366. .macro save_guest_32bit_state
  367. skip_32bit_state x3, 1f
  368. add x3, x2, #CPU_SPSR_OFFSET(KVM_SPSR_ABT)
  369. mrs x4, spsr_abt
  370. mrs x5, spsr_und
  371. mrs x6, spsr_irq
  372. mrs x7, spsr_fiq
  373. stp x4, x5, [x3]
  374. stp x6, x7, [x3, #16]
  375. add x3, x2, #CPU_SYSREG_OFFSET(DACR32_EL2)
  376. mrs x4, dacr32_el2
  377. mrs x5, ifsr32_el2
  378. stp x4, x5, [x3]
  379. skip_fpsimd_state x8, 2f
  380. mrs x6, fpexc32_el2
  381. str x6, [x3, #16]
  382. 2:
  383. skip_debug_state x8, 1f
  384. mrs x7, dbgvcr32_el2
  385. str x7, [x3, #24]
  386. 1:
  387. .endm
  388. .macro restore_guest_32bit_state
  389. skip_32bit_state x3, 1f
  390. add x3, x2, #CPU_SPSR_OFFSET(KVM_SPSR_ABT)
  391. ldp x4, x5, [x3]
  392. ldp x6, x7, [x3, #16]
  393. msr spsr_abt, x4
  394. msr spsr_und, x5
  395. msr spsr_irq, x6
  396. msr spsr_fiq, x7
  397. add x3, x2, #CPU_SYSREG_OFFSET(DACR32_EL2)
  398. ldp x4, x5, [x3]
  399. msr dacr32_el2, x4
  400. msr ifsr32_el2, x5
  401. skip_debug_state x8, 1f
  402. ldr x7, [x3, #24]
  403. msr dbgvcr32_el2, x7
  404. 1:
  405. .endm
  406. .macro activate_traps
  407. ldr x2, [x0, #VCPU_HCR_EL2]
  408. /*
  409. * We are about to set CPTR_EL2.TFP to trap all floating point
  410. * register accesses to EL2, however, the ARM ARM clearly states that
  411. * traps are only taken to EL2 if the operation would not otherwise
  412. * trap to EL1. Therefore, always make sure that for 32-bit guests,
  413. * we set FPEXC.EN to prevent traps to EL1, when setting the TFP bit.
  414. */
  415. tbnz x2, #HCR_RW_SHIFT, 99f // open code skip_32bit_state
  416. mov x3, #(1 << 30)
  417. msr fpexc32_el2, x3
  418. isb
  419. 99:
  420. msr hcr_el2, x2
  421. mov x2, #CPTR_EL2_TTA
  422. orr x2, x2, #CPTR_EL2_TFP
  423. msr cptr_el2, x2
  424. mov x2, #(1 << 15) // Trap CP15 Cr=15
  425. msr hstr_el2, x2
  426. // Monitor Debug Config - see kvm_arm_setup_debug()
  427. ldr x2, [x0, #VCPU_MDCR_EL2]
  428. msr mdcr_el2, x2
  429. .endm
  430. .macro deactivate_traps
  431. mov_q x2, HCR_HOST_NVHE_FLAGS
  432. msr hcr_el2, x2
  433. msr hstr_el2, xzr
  434. mrs x2, mdcr_el2
  435. and x2, x2, #MDCR_EL2_HPMN_MASK
  436. msr mdcr_el2, x2
  437. .endm
  438. .macro activate_vm
  439. ldr x1, [x0, #VCPU_KVM]
  440. kern_hyp_va x1
  441. ldr x2, [x1, #KVM_VTTBR]
  442. msr vttbr_el2, x2
  443. .endm
  444. .macro deactivate_vm
  445. msr vttbr_el2, xzr
  446. .endm
  447. /*
  448. * Call into the vgic backend for state saving
  449. */
  450. .macro save_vgic_state
  451. alternative_if_not ARM64_HAS_SYSREG_GIC_CPUIF
  452. bl __save_vgic_v2_state
  453. alternative_else
  454. bl __save_vgic_v3_state
  455. alternative_endif
  456. mrs x24, hcr_el2
  457. mov x25, #HCR_INT_OVERRIDE
  458. neg x25, x25
  459. and x24, x24, x25
  460. msr hcr_el2, x24
  461. .endm
  462. /*
  463. * Call into the vgic backend for state restoring
  464. */
  465. .macro restore_vgic_state
  466. mrs x24, hcr_el2
  467. ldr x25, [x0, #VCPU_IRQ_LINES]
  468. orr x24, x24, #HCR_INT_OVERRIDE
  469. orr x24, x24, x25
  470. msr hcr_el2, x24
  471. alternative_if_not ARM64_HAS_SYSREG_GIC_CPUIF
  472. bl __restore_vgic_v2_state
  473. alternative_else
  474. bl __restore_vgic_v3_state
  475. alternative_endif
  476. .endm
  477. .macro save_timer_state
  478. // x0: vcpu pointer
  479. ldr x2, [x0, #VCPU_KVM]
  480. kern_hyp_va x2
  481. ldr w3, [x2, #KVM_TIMER_ENABLED]
  482. cbz w3, 1f
  483. mrs x3, cntv_ctl_el0
  484. and x3, x3, #3
  485. str w3, [x0, #VCPU_TIMER_CNTV_CTL]
  486. isb
  487. mrs x3, cntv_cval_el0
  488. str x3, [x0, #VCPU_TIMER_CNTV_CVAL]
  489. 1:
  490. // Disable the virtual timer
  491. msr cntv_ctl_el0, xzr
  492. // Allow physical timer/counter access for the host
  493. mrs x2, cnthctl_el2
  494. orr x2, x2, #3
  495. msr cnthctl_el2, x2
  496. // Clear cntvoff for the host
  497. msr cntvoff_el2, xzr
  498. .endm
  499. .macro restore_timer_state
  500. // x0: vcpu pointer
  501. // Disallow physical timer access for the guest
  502. // Physical counter access is allowed
  503. mrs x2, cnthctl_el2
  504. orr x2, x2, #1
  505. bic x2, x2, #2
  506. msr cnthctl_el2, x2
  507. ldr x2, [x0, #VCPU_KVM]
  508. kern_hyp_va x2
  509. ldr w3, [x2, #KVM_TIMER_ENABLED]
  510. cbz w3, 1f
  511. ldr x3, [x2, #KVM_TIMER_CNTVOFF]
  512. msr cntvoff_el2, x3
  513. ldr x2, [x0, #VCPU_TIMER_CNTV_CVAL]
  514. msr cntv_cval_el0, x2
  515. isb
  516. ldr w2, [x0, #VCPU_TIMER_CNTV_CTL]
  517. and x2, x2, #3
  518. msr cntv_ctl_el0, x2
  519. 1:
  520. .endm
  521. __save_sysregs:
  522. save_sysregs
  523. ret
  524. __restore_sysregs:
  525. restore_sysregs
  526. ret
  527. /* Save debug state */
  528. __save_debug:
  529. // x2: ptr to CPU context
  530. // x3: ptr to debug reg struct
  531. // x4/x5/x6-22/x24-26: trashed
  532. mrs x26, id_aa64dfr0_el1
  533. ubfx x24, x26, #12, #4 // Extract BRPs
  534. ubfx x25, x26, #20, #4 // Extract WRPs
  535. mov w26, #15
  536. sub w24, w26, w24 // How many BPs to skip
  537. sub w25, w26, w25 // How many WPs to skip
  538. mov x5, x24
  539. add x4, x3, #DEBUG_BCR
  540. save_debug dbgbcr
  541. add x4, x3, #DEBUG_BVR
  542. save_debug dbgbvr
  543. mov x5, x25
  544. add x4, x3, #DEBUG_WCR
  545. save_debug dbgwcr
  546. add x4, x3, #DEBUG_WVR
  547. save_debug dbgwvr
  548. mrs x21, mdccint_el1
  549. str x21, [x2, #CPU_SYSREG_OFFSET(MDCCINT_EL1)]
  550. ret
  551. /* Restore debug state */
  552. __restore_debug:
  553. // x2: ptr to CPU context
  554. // x3: ptr to debug reg struct
  555. // x4/x5/x6-22/x24-26: trashed
  556. mrs x26, id_aa64dfr0_el1
  557. ubfx x24, x26, #12, #4 // Extract BRPs
  558. ubfx x25, x26, #20, #4 // Extract WRPs
  559. mov w26, #15
  560. sub w24, w26, w24 // How many BPs to skip
  561. sub w25, w26, w25 // How many WPs to skip
  562. mov x5, x24
  563. add x4, x3, #DEBUG_BCR
  564. restore_debug dbgbcr
  565. add x4, x3, #DEBUG_BVR
  566. restore_debug dbgbvr
  567. mov x5, x25
  568. add x4, x3, #DEBUG_WCR
  569. restore_debug dbgwcr
  570. add x4, x3, #DEBUG_WVR
  571. restore_debug dbgwvr
  572. ldr x21, [x2, #CPU_SYSREG_OFFSET(MDCCINT_EL1)]
  573. msr mdccint_el1, x21
  574. ret
  575. __save_fpsimd:
  576. skip_fpsimd_state x3, 1f
  577. save_fpsimd
  578. 1: ret
  579. __restore_fpsimd:
  580. skip_fpsimd_state x3, 1f
  581. restore_fpsimd
  582. 1: ret
  583. switch_to_guest_fpsimd:
  584. push x4, lr
  585. mrs x2, cptr_el2
  586. bic x2, x2, #CPTR_EL2_TFP
  587. msr cptr_el2, x2
  588. isb
  589. mrs x0, tpidr_el2
  590. ldr x2, [x0, #VCPU_HOST_CONTEXT]
  591. kern_hyp_va x2
  592. bl __save_fpsimd
  593. add x2, x0, #VCPU_CONTEXT
  594. bl __restore_fpsimd
  595. skip_32bit_state x3, 1f
  596. ldr x4, [x2, #CPU_SYSREG_OFFSET(FPEXC32_EL2)]
  597. msr fpexc32_el2, x4
  598. 1:
  599. pop x4, lr
  600. pop x2, x3
  601. pop x0, x1
  602. eret
  603. /*
  604. * u64 __kvm_vcpu_run(struct kvm_vcpu *vcpu);
  605. *
  606. * This is the world switch. The first half of the function
  607. * deals with entering the guest, and anything from __kvm_vcpu_return
  608. * to the end of the function deals with reentering the host.
  609. * On the enter path, only x0 (vcpu pointer) must be preserved until
  610. * the last moment. On the exit path, x0 (vcpu pointer) and x1 (exception
  611. * code) must both be preserved until the epilogue.
  612. * In both cases, x2 points to the CPU context we're saving/restoring from/to.
  613. */
  614. ENTRY(__kvm_vcpu_run)
  615. kern_hyp_va x0
  616. msr tpidr_el2, x0 // Save the vcpu register
  617. // Host context
  618. ldr x2, [x0, #VCPU_HOST_CONTEXT]
  619. kern_hyp_va x2
  620. save_host_regs
  621. bl __save_sysregs
  622. compute_debug_state 1f
  623. add x3, x0, #VCPU_HOST_DEBUG_STATE
  624. bl __save_debug
  625. 1:
  626. activate_traps
  627. activate_vm
  628. restore_vgic_state
  629. restore_timer_state
  630. // Guest context
  631. add x2, x0, #VCPU_CONTEXT
  632. // We must restore the 32-bit state before the sysregs, thanks
  633. // to Cortex-A57 erratum #852523.
  634. restore_guest_32bit_state
  635. bl __restore_sysregs
  636. skip_debug_state x3, 1f
  637. ldr x3, [x0, #VCPU_DEBUG_PTR]
  638. kern_hyp_va x3
  639. bl __restore_debug
  640. 1:
  641. restore_guest_regs
  642. // That's it, no more messing around.
  643. eret
  644. __kvm_vcpu_return:
  645. // Assume x0 is the vcpu pointer, x1 the return code
  646. // Guest's x0-x3 are on the stack
  647. // Guest context
  648. add x2, x0, #VCPU_CONTEXT
  649. save_guest_regs
  650. bl __save_fpsimd
  651. bl __save_sysregs
  652. skip_debug_state x3, 1f
  653. ldr x3, [x0, #VCPU_DEBUG_PTR]
  654. kern_hyp_va x3
  655. bl __save_debug
  656. 1:
  657. save_guest_32bit_state
  658. save_timer_state
  659. save_vgic_state
  660. deactivate_traps
  661. deactivate_vm
  662. // Host context
  663. ldr x2, [x0, #VCPU_HOST_CONTEXT]
  664. kern_hyp_va x2
  665. bl __restore_sysregs
  666. bl __restore_fpsimd
  667. /* Clear FPSIMD and Trace trapping */
  668. msr cptr_el2, xzr
  669. skip_debug_state x3, 1f
  670. // Clear the dirty flag for the next run, as all the state has
  671. // already been saved. Note that we nuke the whole 64bit word.
  672. // If we ever add more flags, we'll have to be more careful...
  673. str xzr, [x0, #VCPU_DEBUG_FLAGS]
  674. add x3, x0, #VCPU_HOST_DEBUG_STATE
  675. bl __restore_debug
  676. 1:
  677. restore_host_regs
  678. mov x0, x1
  679. ret
  680. END(__kvm_vcpu_run)
  681. // void __kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa);
  682. ENTRY(__kvm_tlb_flush_vmid_ipa)
  683. dsb ishst
  684. kern_hyp_va x0
  685. ldr x2, [x0, #KVM_VTTBR]
  686. msr vttbr_el2, x2
  687. isb
  688. /*
  689. * We could do so much better if we had the VA as well.
  690. * Instead, we invalidate Stage-2 for this IPA, and the
  691. * whole of Stage-1. Weep...
  692. */
  693. lsr x1, x1, #12
  694. tlbi ipas2e1is, x1
  695. /*
  696. * We have to ensure completion of the invalidation at Stage-2,
  697. * since a table walk on another CPU could refill a TLB with a
  698. * complete (S1 + S2) walk based on the old Stage-2 mapping if
  699. * the Stage-1 invalidation happened first.
  700. */
  701. dsb ish
  702. tlbi vmalle1is
  703. dsb ish
  704. isb
  705. msr vttbr_el2, xzr
  706. ret
  707. ENDPROC(__kvm_tlb_flush_vmid_ipa)
  708. /**
  709. * void __kvm_tlb_flush_vmid(struct kvm *kvm) - Flush per-VMID TLBs
  710. * @struct kvm *kvm - pointer to kvm structure
  711. *
  712. * Invalidates all Stage 1 and 2 TLB entries for current VMID.
  713. */
  714. ENTRY(__kvm_tlb_flush_vmid)
  715. dsb ishst
  716. kern_hyp_va x0
  717. ldr x2, [x0, #KVM_VTTBR]
  718. msr vttbr_el2, x2
  719. isb
  720. tlbi vmalls12e1is
  721. dsb ish
  722. isb
  723. msr vttbr_el2, xzr
  724. ret
  725. ENDPROC(__kvm_tlb_flush_vmid)
  726. ENTRY(__kvm_flush_vm_context)
  727. dsb ishst
  728. tlbi alle1is
  729. ic ialluis
  730. dsb ish
  731. ret
  732. ENDPROC(__kvm_flush_vm_context)
  733. __kvm_hyp_panic:
  734. // Stash PAR_EL1 before corrupting it in __restore_sysregs
  735. mrs x0, par_el1
  736. push x0, xzr
  737. // Guess the context by looking at VTTBR:
  738. // If zero, then we're already a host.
  739. // Otherwise restore a minimal host context before panicing.
  740. mrs x0, vttbr_el2
  741. cbz x0, 1f
  742. mrs x0, tpidr_el2
  743. deactivate_traps
  744. deactivate_vm
  745. ldr x2, [x0, #VCPU_HOST_CONTEXT]
  746. kern_hyp_va x2
  747. bl __restore_sysregs
  748. /*
  749. * Make sure we have a valid host stack, and don't leave junk in the
  750. * frame pointer that will give us a misleading host stack unwinding.
  751. */
  752. ldr x22, [x2, #CPU_GP_REG_OFFSET(CPU_SP_EL1)]
  753. msr sp_el1, x22
  754. mov x29, xzr
  755. 1: adr x0, __hyp_panic_str
  756. adr x1, 2f
  757. ldp x2, x3, [x1]
  758. sub x0, x0, x2
  759. add x0, x0, x3
  760. mrs x1, spsr_el2
  761. mrs x2, elr_el2
  762. mrs x3, esr_el2
  763. mrs x4, far_el2
  764. mrs x5, hpfar_el2
  765. pop x6, xzr // active context PAR_EL1
  766. mrs x7, tpidr_el2
  767. mov lr, #(PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT |\
  768. PSR_MODE_EL1h)
  769. msr spsr_el2, lr
  770. ldr lr, =panic
  771. msr elr_el2, lr
  772. eret
  773. .align 3
  774. 2: .quad HYP_PAGE_OFFSET
  775. .quad PAGE_OFFSET
  776. ENDPROC(__kvm_hyp_panic)
  777. __hyp_panic_str:
  778. .ascii "HYP panic:\nPS:%08x PC:%016x ESR:%08x\nFAR:%016x HPFAR:%016x PAR:%016x\nVCPU:%p\n\0"
  779. .align 2
  780. /*
  781. * u64 kvm_call_hyp(void *hypfn, ...);
  782. *
  783. * This is not really a variadic function in the classic C-way and care must
  784. * be taken when calling this to ensure parameters are passed in registers
  785. * only, since the stack will change between the caller and the callee.
  786. *
  787. * Call the function with the first argument containing a pointer to the
  788. * function you wish to call in Hyp mode, and subsequent arguments will be
  789. * passed as x0, x1, and x2 (a maximum of 3 arguments in addition to the
  790. * function pointer can be passed). The function being called must be mapped
  791. * in Hyp mode (see init_hyp_mode in arch/arm/kvm/arm.c). Return values are
  792. * passed in r0 and r1.
  793. *
  794. * A function pointer with a value of 0 has a special meaning, and is
  795. * used to implement __hyp_get_vectors in the same way as in
  796. * arch/arm64/kernel/hyp_stub.S.
  797. */
  798. ENTRY(kvm_call_hyp)
  799. hvc #0
  800. ret
  801. ENDPROC(kvm_call_hyp)
  802. .macro invalid_vector label, target
  803. .align 2
  804. \label:
  805. b \target
  806. ENDPROC(\label)
  807. .endm
  808. /* None of these should ever happen */
  809. invalid_vector el2t_sync_invalid, __kvm_hyp_panic
  810. invalid_vector el2t_irq_invalid, __kvm_hyp_panic
  811. invalid_vector el2t_fiq_invalid, __kvm_hyp_panic
  812. invalid_vector el2t_error_invalid, __kvm_hyp_panic
  813. invalid_vector el2h_sync_invalid, __kvm_hyp_panic
  814. invalid_vector el2h_irq_invalid, __kvm_hyp_panic
  815. invalid_vector el2h_fiq_invalid, __kvm_hyp_panic
  816. invalid_vector el2h_error_invalid, __kvm_hyp_panic
  817. invalid_vector el1_sync_invalid, __kvm_hyp_panic
  818. invalid_vector el1_irq_invalid, __kvm_hyp_panic
  819. invalid_vector el1_fiq_invalid, __kvm_hyp_panic
  820. invalid_vector el1_error_invalid, __kvm_hyp_panic
  821. el1_sync: // Guest trapped into EL2
  822. push x0, x1
  823. push x2, x3
  824. mrs x1, esr_el2
  825. lsr x2, x1, #ESR_ELx_EC_SHIFT
  826. cmp x2, #ESR_ELx_EC_HVC64
  827. b.ne el1_trap
  828. mrs x3, vttbr_el2 // If vttbr is valid, the 64bit guest
  829. cbnz x3, el1_trap // called HVC
  830. /* Here, we're pretty sure the host called HVC. */
  831. pop x2, x3
  832. pop x0, x1
  833. /* Check for __hyp_get_vectors */
  834. cbnz x0, 1f
  835. mrs x0, vbar_el2
  836. b 2f
  837. 1: push lr, xzr
  838. /*
  839. * Compute the function address in EL2, and shuffle the parameters.
  840. */
  841. kern_hyp_va x0
  842. mov lr, x0
  843. mov x0, x1
  844. mov x1, x2
  845. mov x2, x3
  846. blr lr
  847. pop lr, xzr
  848. 2: eret
  849. el1_trap:
  850. /*
  851. * x1: ESR
  852. * x2: ESR_EC
  853. */
  854. /* Guest accessed VFP/SIMD registers, save host, restore Guest */
  855. cmp x2, #ESR_ELx_EC_FP_ASIMD
  856. b.eq switch_to_guest_fpsimd
  857. cmp x2, #ESR_ELx_EC_DABT_LOW
  858. mov x0, #ESR_ELx_EC_IABT_LOW
  859. ccmp x2, x0, #4, ne
  860. b.ne 1f // Not an abort we care about
  861. /* This is an abort. Check for permission fault */
  862. alternative_if_not ARM64_WORKAROUND_834220
  863. and x2, x1, #ESR_ELx_FSC_TYPE
  864. cmp x2, #FSC_PERM
  865. b.ne 1f // Not a permission fault
  866. alternative_else
  867. nop // Use the permission fault path to
  868. nop // check for a valid S1 translation,
  869. nop // regardless of the ESR value.
  870. alternative_endif
  871. /*
  872. * Check for Stage-1 page table walk, which is guaranteed
  873. * to give a valid HPFAR_EL2.
  874. */
  875. tbnz x1, #7, 1f // S1PTW is set
  876. /* Preserve PAR_EL1 */
  877. mrs x3, par_el1
  878. push x3, xzr
  879. /*
  880. * Permission fault, HPFAR_EL2 is invalid.
  881. * Resolve the IPA the hard way using the guest VA.
  882. * Stage-1 translation already validated the memory access rights.
  883. * As such, we can use the EL1 translation regime, and don't have
  884. * to distinguish between EL0 and EL1 access.
  885. */
  886. mrs x2, far_el2
  887. at s1e1r, x2
  888. isb
  889. /* Read result */
  890. mrs x3, par_el1
  891. pop x0, xzr // Restore PAR_EL1 from the stack
  892. msr par_el1, x0
  893. tbnz x3, #0, 3f // Bail out if we failed the translation
  894. ubfx x3, x3, #12, #36 // Extract IPA
  895. lsl x3, x3, #4 // and present it like HPFAR
  896. b 2f
  897. 1: mrs x3, hpfar_el2
  898. mrs x2, far_el2
  899. 2: mrs x0, tpidr_el2
  900. str w1, [x0, #VCPU_ESR_EL2]
  901. str x2, [x0, #VCPU_FAR_EL2]
  902. str x3, [x0, #VCPU_HPFAR_EL2]
  903. mov x1, #ARM_EXCEPTION_TRAP
  904. b __kvm_vcpu_return
  905. /*
  906. * Translation failed. Just return to the guest and
  907. * let it fault again. Another CPU is probably playing
  908. * behind our back.
  909. */
  910. 3: pop x2, x3
  911. pop x0, x1
  912. eret
  913. el1_irq:
  914. push x0, x1
  915. push x2, x3
  916. mrs x0, tpidr_el2
  917. mov x1, #ARM_EXCEPTION_IRQ
  918. b __kvm_vcpu_return
  919. .ltorg
  920. .align 11
  921. ENTRY(__kvm_hyp_vector)
  922. ventry el2t_sync_invalid // Synchronous EL2t
  923. ventry el2t_irq_invalid // IRQ EL2t
  924. ventry el2t_fiq_invalid // FIQ EL2t
  925. ventry el2t_error_invalid // Error EL2t
  926. ventry el2h_sync_invalid // Synchronous EL2h
  927. ventry el2h_irq_invalid // IRQ EL2h
  928. ventry el2h_fiq_invalid // FIQ EL2h
  929. ventry el2h_error_invalid // Error EL2h
  930. ventry el1_sync // Synchronous 64-bit EL1
  931. ventry el1_irq // IRQ 64-bit EL1
  932. ventry el1_fiq_invalid // FIQ 64-bit EL1
  933. ventry el1_error_invalid // Error 64-bit EL1
  934. ventry el1_sync // Synchronous 32-bit EL1
  935. ventry el1_irq // IRQ 32-bit EL1
  936. ventry el1_fiq_invalid // FIQ 32-bit EL1
  937. ventry el1_error_invalid // Error 32-bit EL1
  938. ENDPROC(__kvm_hyp_vector)
  939. ENTRY(__kvm_get_mdcr_el2)
  940. mrs x0, mdcr_el2
  941. ret
  942. ENDPROC(__kvm_get_mdcr_el2)
  943. .popsection