hyp-main.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020 - Google Inc
  4. * Author: Andrew Scull <[email protected]>
  5. */
  6. #include <kvm/arm_hypercalls.h>
  7. #include <hyp/adjust_pc.h>
  8. #include <asm/pgtable-types.h>
  9. #include <asm/kvm_asm.h>
  10. #include <asm/kvm_emulate.h>
  11. #include <asm/kvm_host.h>
  12. #include <asm/kvm_hyp.h>
  13. #include <asm/kvm_hypevents.h>
  14. #include <asm/kvm_mmu.h>
  15. #include <nvhe/ffa.h>
  16. #include <nvhe/iommu.h>
  17. #include <nvhe/mem_protect.h>
  18. #include <nvhe/modules.h>
  19. #include <nvhe/mm.h>
  20. #include <nvhe/pkvm.h>
  21. #include <nvhe/trace.h>
  22. #include <nvhe/trap_handler.h>
  23. #include <linux/irqchip/arm-gic-v3.h>
  24. #include <uapi/linux/psci.h>
  25. #include "../../sys_regs.h"
  26. DEFINE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params);
  27. void __kvm_hyp_host_forward_smc(struct kvm_cpu_context *host_ctxt);
  28. static bool (*default_host_smc_handler)(struct kvm_cpu_context *host_ctxt);
  29. static bool (*default_trap_handler)(struct kvm_cpu_context *host_ctxt);
  30. int __pkvm_register_host_smc_handler(bool (*cb)(struct kvm_cpu_context *))
  31. {
  32. /*
  33. * Paired with smp_load_acquire(&default_host_smc_handler) in
  34. * handle_host_smc(). Ensure memory stores happening during a pKVM module
  35. * init are observed before executing the callback.
  36. */
  37. return cmpxchg_release(&default_host_smc_handler, NULL, cb) ? -EBUSY : 0;
  38. }
  39. int __pkvm_register_default_trap_handler(bool (*cb)(struct kvm_cpu_context *))
  40. {
  41. return cmpxchg(&default_trap_handler, NULL, cb) ? -EBUSY : 0;
  42. }
  43. static int pkvm_refill_memcache(struct pkvm_hyp_vcpu *hyp_vcpu)
  44. {
  45. struct pkvm_hyp_vm *hyp_vm = pkvm_hyp_vcpu_to_hyp_vm(hyp_vcpu);
  46. u64 nr_pages = VTCR_EL2_LVLS(hyp_vm->kvm.arch.vtcr) - 1;
  47. struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
  48. return refill_memcache(&hyp_vcpu->vcpu.arch.pkvm_memcache, nr_pages,
  49. &host_vcpu->arch.pkvm_memcache);
  50. }
  51. typedef void (*hyp_entry_exit_handler_fn)(struct pkvm_hyp_vcpu *);
  52. static void handle_pvm_entry_wfx(struct pkvm_hyp_vcpu *hyp_vcpu)
  53. {
  54. if (vcpu_get_flag(hyp_vcpu->host_vcpu, INCREMENT_PC)) {
  55. vcpu_clear_flag(&hyp_vcpu->vcpu, PC_UPDATE_REQ);
  56. kvm_incr_pc(&hyp_vcpu->vcpu);
  57. }
  58. }
  59. static void handle_pvm_entry_psci(struct pkvm_hyp_vcpu *hyp_vcpu)
  60. {
  61. u32 psci_fn = smccc_get_function(&hyp_vcpu->vcpu);
  62. u64 ret = READ_ONCE(hyp_vcpu->host_vcpu->arch.ctxt.regs.regs[0]);
  63. switch (psci_fn) {
  64. case PSCI_0_2_FN_CPU_ON:
  65. case PSCI_0_2_FN64_CPU_ON:
  66. /*
  67. * Check whether the cpu_on request to the host was successful.
  68. * If not, reset the vcpu state from ON_PENDING to OFF.
  69. * This could happen if this vcpu attempted to turn on the other
  70. * vcpu while the other one is in the process of turning itself
  71. * off.
  72. */
  73. if (ret != PSCI_RET_SUCCESS) {
  74. unsigned long cpu_id = smccc_get_arg1(&hyp_vcpu->vcpu);
  75. struct pkvm_hyp_vcpu *target_vcpu;
  76. struct pkvm_hyp_vm *hyp_vm;
  77. hyp_vm = pkvm_hyp_vcpu_to_hyp_vm(hyp_vcpu);
  78. target_vcpu = pkvm_mpidr_to_hyp_vcpu(hyp_vm, cpu_id);
  79. if (target_vcpu && READ_ONCE(target_vcpu->power_state) == PSCI_0_2_AFFINITY_LEVEL_ON_PENDING)
  80. WRITE_ONCE(target_vcpu->power_state, PSCI_0_2_AFFINITY_LEVEL_OFF);
  81. ret = PSCI_RET_INTERNAL_FAILURE;
  82. }
  83. break;
  84. default:
  85. break;
  86. }
  87. vcpu_set_reg(&hyp_vcpu->vcpu, 0, ret);
  88. }
  89. static void handle_pvm_entry_hvc64(struct pkvm_hyp_vcpu *hyp_vcpu)
  90. {
  91. u32 fn = smccc_get_function(&hyp_vcpu->vcpu);
  92. switch (fn) {
  93. case ARM_SMCCC_VENDOR_HYP_KVM_MMIO_GUARD_MAP_FUNC_ID:
  94. pkvm_refill_memcache(hyp_vcpu);
  95. break;
  96. case ARM_SMCCC_VENDOR_HYP_KVM_MEM_SHARE_FUNC_ID:
  97. fallthrough;
  98. case ARM_SMCCC_VENDOR_HYP_KVM_MEM_UNSHARE_FUNC_ID:
  99. fallthrough;
  100. case ARM_SMCCC_VENDOR_HYP_KVM_MEM_RELINQUISH_FUNC_ID:
  101. vcpu_set_reg(&hyp_vcpu->vcpu, 0, SMCCC_RET_SUCCESS);
  102. break;
  103. default:
  104. handle_pvm_entry_psci(hyp_vcpu);
  105. break;
  106. }
  107. }
  108. static void handle_pvm_entry_sys64(struct pkvm_hyp_vcpu *hyp_vcpu)
  109. {
  110. struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
  111. /* Exceptions have priority on anything else */
  112. if (vcpu_get_flag(host_vcpu, PENDING_EXCEPTION)) {
  113. /* Exceptions caused by this should be undef exceptions. */
  114. u32 esr = (ESR_ELx_EC_UNKNOWN << ESR_ELx_EC_SHIFT);
  115. __vcpu_sys_reg(&hyp_vcpu->vcpu, ESR_EL1) = esr;
  116. kvm_pend_exception(&hyp_vcpu->vcpu, EXCEPT_AA64_EL1_SYNC);
  117. return;
  118. }
  119. if (vcpu_get_flag(host_vcpu, INCREMENT_PC)) {
  120. vcpu_clear_flag(&hyp_vcpu->vcpu, PC_UPDATE_REQ);
  121. kvm_incr_pc(&hyp_vcpu->vcpu);
  122. }
  123. if (!esr_sys64_to_params(hyp_vcpu->vcpu.arch.fault.esr_el2).is_write) {
  124. /* r0 as transfer register between the guest and the host. */
  125. u64 rt_val = READ_ONCE(host_vcpu->arch.ctxt.regs.regs[0]);
  126. int rt = kvm_vcpu_sys_get_rt(&hyp_vcpu->vcpu);
  127. vcpu_set_reg(&hyp_vcpu->vcpu, rt, rt_val);
  128. }
  129. }
  130. static void handle_pvm_entry_iabt(struct pkvm_hyp_vcpu *hyp_vcpu)
  131. {
  132. unsigned long cpsr = *vcpu_cpsr(&hyp_vcpu->vcpu);
  133. u32 esr = ESR_ELx_IL;
  134. if (!vcpu_get_flag(hyp_vcpu->host_vcpu, PENDING_EXCEPTION))
  135. return;
  136. /*
  137. * If the host wants to inject an exception, get syndrom and
  138. * fault address.
  139. */
  140. if ((cpsr & PSR_MODE_MASK) == PSR_MODE_EL0t)
  141. esr |= (ESR_ELx_EC_IABT_LOW << ESR_ELx_EC_SHIFT);
  142. else
  143. esr |= (ESR_ELx_EC_IABT_CUR << ESR_ELx_EC_SHIFT);
  144. esr |= ESR_ELx_FSC_EXTABT;
  145. __vcpu_sys_reg(&hyp_vcpu->vcpu, ESR_EL1) = esr;
  146. __vcpu_sys_reg(&hyp_vcpu->vcpu, FAR_EL1) =
  147. kvm_vcpu_get_hfar(&hyp_vcpu->vcpu);
  148. /* Tell the run loop that we want to inject something */
  149. kvm_pend_exception(&hyp_vcpu->vcpu, EXCEPT_AA64_EL1_SYNC);
  150. }
  151. static void handle_pvm_entry_dabt(struct pkvm_hyp_vcpu *hyp_vcpu)
  152. {
  153. struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
  154. bool pc_update;
  155. /* Exceptions have priority over anything else */
  156. if (vcpu_get_flag(host_vcpu, PENDING_EXCEPTION)) {
  157. unsigned long cpsr = *vcpu_cpsr(&hyp_vcpu->vcpu);
  158. u32 esr = ESR_ELx_IL;
  159. if ((cpsr & PSR_MODE_MASK) == PSR_MODE_EL0t)
  160. esr |= (ESR_ELx_EC_DABT_LOW << ESR_ELx_EC_SHIFT);
  161. else
  162. esr |= (ESR_ELx_EC_DABT_CUR << ESR_ELx_EC_SHIFT);
  163. esr |= ESR_ELx_FSC_EXTABT;
  164. __vcpu_sys_reg(&hyp_vcpu->vcpu, ESR_EL1) = esr;
  165. __vcpu_sys_reg(&hyp_vcpu->vcpu, FAR_EL1) =
  166. kvm_vcpu_get_hfar(&hyp_vcpu->vcpu);
  167. /* Tell the run loop that we want to inject something */
  168. kvm_pend_exception(&hyp_vcpu->vcpu, EXCEPT_AA64_EL1_SYNC);
  169. /* Cancel potential in-flight MMIO */
  170. hyp_vcpu->vcpu.mmio_needed = false;
  171. return;
  172. }
  173. /* Handle PC increment on MMIO */
  174. pc_update = (hyp_vcpu->vcpu.mmio_needed &&
  175. vcpu_get_flag(host_vcpu, INCREMENT_PC));
  176. if (pc_update) {
  177. vcpu_clear_flag(&hyp_vcpu->vcpu, PC_UPDATE_REQ);
  178. kvm_incr_pc(&hyp_vcpu->vcpu);
  179. }
  180. /* If we were doing an MMIO read access, update the register*/
  181. if (pc_update && !kvm_vcpu_dabt_iswrite(&hyp_vcpu->vcpu)) {
  182. /* r0 as transfer register between the guest and the host. */
  183. u64 rd_val = READ_ONCE(host_vcpu->arch.ctxt.regs.regs[0]);
  184. int rd = kvm_vcpu_dabt_get_rd(&hyp_vcpu->vcpu);
  185. vcpu_set_reg(&hyp_vcpu->vcpu, rd, rd_val);
  186. }
  187. hyp_vcpu->vcpu.mmio_needed = false;
  188. }
  189. static void handle_pvm_exit_wfx(struct pkvm_hyp_vcpu *hyp_vcpu)
  190. {
  191. WRITE_ONCE(hyp_vcpu->host_vcpu->arch.ctxt.regs.pstate,
  192. hyp_vcpu->vcpu.arch.ctxt.regs.pstate & PSR_MODE_MASK);
  193. WRITE_ONCE(hyp_vcpu->host_vcpu->arch.fault.esr_el2,
  194. hyp_vcpu->vcpu.arch.fault.esr_el2);
  195. }
  196. static void handle_pvm_exit_sys64(struct pkvm_hyp_vcpu *hyp_vcpu)
  197. {
  198. struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
  199. u32 esr_el2 = hyp_vcpu->vcpu.arch.fault.esr_el2;
  200. /* r0 as transfer register between the guest and the host. */
  201. WRITE_ONCE(host_vcpu->arch.fault.esr_el2,
  202. esr_el2 & ~ESR_ELx_SYS64_ISS_RT_MASK);
  203. /* The mode is required for the host to emulate some sysregs */
  204. WRITE_ONCE(host_vcpu->arch.ctxt.regs.pstate,
  205. hyp_vcpu->vcpu.arch.ctxt.regs.pstate & PSR_MODE_MASK);
  206. if (esr_sys64_to_params(esr_el2).is_write) {
  207. int rt = kvm_vcpu_sys_get_rt(&hyp_vcpu->vcpu);
  208. u64 rt_val = vcpu_get_reg(&hyp_vcpu->vcpu, rt);
  209. WRITE_ONCE(host_vcpu->arch.ctxt.regs.regs[0], rt_val);
  210. }
  211. }
  212. static void handle_pvm_exit_hvc64(struct pkvm_hyp_vcpu *hyp_vcpu)
  213. {
  214. struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
  215. int n, i;
  216. switch (smccc_get_function(&hyp_vcpu->vcpu)) {
  217. /*
  218. * CPU_ON takes 3 arguments, however, to wake up the target vcpu the
  219. * host only needs to know the target's cpu_id, which is passed as the
  220. * first argument. The processing of the reset state is done at hyp.
  221. */
  222. case PSCI_0_2_FN_CPU_ON:
  223. case PSCI_0_2_FN64_CPU_ON:
  224. n = 2;
  225. break;
  226. case PSCI_0_2_FN_CPU_OFF:
  227. case PSCI_0_2_FN_SYSTEM_OFF:
  228. case PSCI_0_2_FN_SYSTEM_RESET:
  229. case PSCI_0_2_FN_CPU_SUSPEND:
  230. case PSCI_0_2_FN64_CPU_SUSPEND:
  231. n = 1;
  232. break;
  233. case ARM_SMCCC_VENDOR_HYP_KVM_MEM_SHARE_FUNC_ID:
  234. fallthrough;
  235. case ARM_SMCCC_VENDOR_HYP_KVM_MEM_UNSHARE_FUNC_ID:
  236. fallthrough;
  237. case ARM_SMCCC_VENDOR_HYP_KVM_MEM_RELINQUISH_FUNC_ID:
  238. n = 4;
  239. break;
  240. case ARM_SMCCC_VENDOR_HYP_KVM_MMIO_GUARD_MAP_FUNC_ID:
  241. n = 3;
  242. break;
  243. case PSCI_1_1_FN_SYSTEM_RESET2:
  244. case PSCI_1_1_FN64_SYSTEM_RESET2:
  245. n = 3;
  246. break;
  247. /*
  248. * The rest are either blocked or handled by HYP, so we should
  249. * really never be here.
  250. */
  251. default:
  252. BUG();
  253. }
  254. WRITE_ONCE(host_vcpu->arch.fault.esr_el2,
  255. hyp_vcpu->vcpu.arch.fault.esr_el2);
  256. /* Pass the hvc function id (r0) as well as any potential arguments. */
  257. for (i = 0; i < n; i++) {
  258. WRITE_ONCE(host_vcpu->arch.ctxt.regs.regs[i],
  259. vcpu_get_reg(&hyp_vcpu->vcpu, i));
  260. }
  261. }
  262. static void handle_pvm_exit_iabt(struct pkvm_hyp_vcpu *hyp_vcpu)
  263. {
  264. WRITE_ONCE(hyp_vcpu->host_vcpu->arch.fault.esr_el2,
  265. hyp_vcpu->vcpu.arch.fault.esr_el2);
  266. WRITE_ONCE(hyp_vcpu->host_vcpu->arch.fault.hpfar_el2,
  267. hyp_vcpu->vcpu.arch.fault.hpfar_el2);
  268. }
  269. static void handle_pvm_exit_dabt(struct pkvm_hyp_vcpu *hyp_vcpu)
  270. {
  271. struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
  272. hyp_vcpu->vcpu.mmio_needed = __pkvm_check_ioguard_page(hyp_vcpu);
  273. if (hyp_vcpu->vcpu.mmio_needed) {
  274. /* r0 as transfer register between the guest and the host. */
  275. WRITE_ONCE(host_vcpu->arch.fault.esr_el2,
  276. hyp_vcpu->vcpu.arch.fault.esr_el2 & ~ESR_ELx_SRT_MASK);
  277. if (kvm_vcpu_dabt_iswrite(&hyp_vcpu->vcpu)) {
  278. int rt = kvm_vcpu_dabt_get_rd(&hyp_vcpu->vcpu);
  279. u64 rt_val = vcpu_get_reg(&hyp_vcpu->vcpu, rt);
  280. WRITE_ONCE(host_vcpu->arch.ctxt.regs.regs[0], rt_val);
  281. }
  282. } else {
  283. WRITE_ONCE(host_vcpu->arch.fault.esr_el2,
  284. hyp_vcpu->vcpu.arch.fault.esr_el2 & ~ESR_ELx_ISV);
  285. }
  286. WRITE_ONCE(host_vcpu->arch.ctxt.regs.pstate,
  287. hyp_vcpu->vcpu.arch.ctxt.regs.pstate & PSR_MODE_MASK);
  288. WRITE_ONCE(host_vcpu->arch.fault.far_el2,
  289. hyp_vcpu->vcpu.arch.fault.far_el2 & GENMASK(11, 0));
  290. WRITE_ONCE(host_vcpu->arch.fault.hpfar_el2,
  291. hyp_vcpu->vcpu.arch.fault.hpfar_el2);
  292. WRITE_ONCE(__vcpu_sys_reg(host_vcpu, SCTLR_EL1),
  293. __vcpu_sys_reg(&hyp_vcpu->vcpu, SCTLR_EL1) &
  294. (SCTLR_ELx_EE | SCTLR_EL1_E0E));
  295. }
  296. static void handle_vm_entry_generic(struct pkvm_hyp_vcpu *hyp_vcpu)
  297. {
  298. vcpu_copy_flag(&hyp_vcpu->vcpu, hyp_vcpu->host_vcpu, PC_UPDATE_REQ);
  299. }
  300. static void handle_vm_exit_generic(struct pkvm_hyp_vcpu *hyp_vcpu)
  301. {
  302. WRITE_ONCE(hyp_vcpu->host_vcpu->arch.fault.esr_el2,
  303. hyp_vcpu->vcpu.arch.fault.esr_el2);
  304. }
  305. static void handle_vm_exit_abt(struct pkvm_hyp_vcpu *hyp_vcpu)
  306. {
  307. struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
  308. WRITE_ONCE(host_vcpu->arch.fault.esr_el2,
  309. hyp_vcpu->vcpu.arch.fault.esr_el2);
  310. WRITE_ONCE(host_vcpu->arch.fault.far_el2,
  311. hyp_vcpu->vcpu.arch.fault.far_el2);
  312. WRITE_ONCE(host_vcpu->arch.fault.hpfar_el2,
  313. hyp_vcpu->vcpu.arch.fault.hpfar_el2);
  314. WRITE_ONCE(host_vcpu->arch.fault.disr_el1,
  315. hyp_vcpu->vcpu.arch.fault.disr_el1);
  316. }
  317. static const hyp_entry_exit_handler_fn entry_hyp_pvm_handlers[] = {
  318. [0 ... ESR_ELx_EC_MAX] = NULL,
  319. [ESR_ELx_EC_WFx] = handle_pvm_entry_wfx,
  320. [ESR_ELx_EC_HVC64] = handle_pvm_entry_hvc64,
  321. [ESR_ELx_EC_SYS64] = handle_pvm_entry_sys64,
  322. [ESR_ELx_EC_IABT_LOW] = handle_pvm_entry_iabt,
  323. [ESR_ELx_EC_DABT_LOW] = handle_pvm_entry_dabt,
  324. };
  325. static const hyp_entry_exit_handler_fn exit_hyp_pvm_handlers[] = {
  326. [0 ... ESR_ELx_EC_MAX] = NULL,
  327. [ESR_ELx_EC_WFx] = handle_pvm_exit_wfx,
  328. [ESR_ELx_EC_HVC64] = handle_pvm_exit_hvc64,
  329. [ESR_ELx_EC_SYS64] = handle_pvm_exit_sys64,
  330. [ESR_ELx_EC_IABT_LOW] = handle_pvm_exit_iabt,
  331. [ESR_ELx_EC_DABT_LOW] = handle_pvm_exit_dabt,
  332. };
  333. static const hyp_entry_exit_handler_fn entry_hyp_vm_handlers[] = {
  334. [0 ... ESR_ELx_EC_MAX] = handle_vm_entry_generic,
  335. };
  336. static const hyp_entry_exit_handler_fn exit_hyp_vm_handlers[] = {
  337. [0 ... ESR_ELx_EC_MAX] = handle_vm_exit_generic,
  338. [ESR_ELx_EC_IABT_LOW] = handle_vm_exit_abt,
  339. [ESR_ELx_EC_DABT_LOW] = handle_vm_exit_abt,
  340. };
  341. static void flush_hyp_vgic_state(struct pkvm_hyp_vcpu *hyp_vcpu)
  342. {
  343. struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
  344. struct vgic_v3_cpu_if *host_cpu_if, *hyp_cpu_if;
  345. unsigned int used_lrs, max_lrs, i;
  346. host_cpu_if = &host_vcpu->arch.vgic_cpu.vgic_v3;
  347. hyp_cpu_if = &hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3;
  348. max_lrs = (read_gicreg(ICH_VTR_EL2) & 0xf) + 1;
  349. used_lrs = READ_ONCE(host_cpu_if->used_lrs);
  350. used_lrs = min(used_lrs, max_lrs);
  351. hyp_cpu_if->vgic_hcr = READ_ONCE(host_cpu_if->vgic_hcr);
  352. /* Should be a one-off */
  353. hyp_cpu_if->vgic_sre = (ICC_SRE_EL1_DIB |
  354. ICC_SRE_EL1_DFB |
  355. ICC_SRE_EL1_SRE);
  356. hyp_cpu_if->used_lrs = used_lrs;
  357. for (i = 0; i < used_lrs; i++)
  358. hyp_cpu_if->vgic_lr[i] = READ_ONCE(host_cpu_if->vgic_lr[i]);
  359. }
  360. static void sync_hyp_vgic_state(struct pkvm_hyp_vcpu *hyp_vcpu)
  361. {
  362. struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
  363. struct vgic_v3_cpu_if *host_cpu_if, *hyp_cpu_if;
  364. unsigned int i;
  365. host_cpu_if = &host_vcpu->arch.vgic_cpu.vgic_v3;
  366. hyp_cpu_if = &hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3;
  367. WRITE_ONCE(host_cpu_if->vgic_hcr, hyp_cpu_if->vgic_hcr);
  368. for (i = 0; i < hyp_cpu_if->used_lrs; i++)
  369. WRITE_ONCE(host_cpu_if->vgic_lr[i], hyp_cpu_if->vgic_lr[i]);
  370. }
  371. static void flush_hyp_timer_state(struct pkvm_hyp_vcpu *hyp_vcpu)
  372. {
  373. if (!pkvm_hyp_vcpu_is_protected(hyp_vcpu))
  374. return;
  375. /*
  376. * A hyp vcpu has no offset, and sees vtime == ptime. The
  377. * ptimer is fully emulated by EL1 and cannot be trusted.
  378. */
  379. write_sysreg(0, cntvoff_el2);
  380. isb();
  381. write_sysreg_el0(__vcpu_sys_reg(&hyp_vcpu->vcpu, CNTV_CVAL_EL0),
  382. SYS_CNTV_CVAL);
  383. write_sysreg_el0(__vcpu_sys_reg(&hyp_vcpu->vcpu, CNTV_CTL_EL0),
  384. SYS_CNTV_CTL);
  385. }
  386. static void sync_hyp_timer_state(struct pkvm_hyp_vcpu *hyp_vcpu)
  387. {
  388. if (!pkvm_hyp_vcpu_is_protected(hyp_vcpu))
  389. return;
  390. /*
  391. * Preserve the vtimer state so that it is always correct,
  392. * even if the host tries to make a mess.
  393. */
  394. __vcpu_sys_reg(&hyp_vcpu->vcpu, CNTV_CVAL_EL0) =
  395. read_sysreg_el0(SYS_CNTV_CVAL);
  396. __vcpu_sys_reg(&hyp_vcpu->vcpu, CNTV_CTL_EL0) =
  397. read_sysreg_el0(SYS_CNTV_CTL);
  398. }
  399. static void __copy_vcpu_state(const struct kvm_vcpu *from_vcpu,
  400. struct kvm_vcpu *to_vcpu)
  401. {
  402. int i;
  403. to_vcpu->arch.ctxt.regs = from_vcpu->arch.ctxt.regs;
  404. to_vcpu->arch.ctxt.spsr_abt = from_vcpu->arch.ctxt.spsr_abt;
  405. to_vcpu->arch.ctxt.spsr_und = from_vcpu->arch.ctxt.spsr_und;
  406. to_vcpu->arch.ctxt.spsr_irq = from_vcpu->arch.ctxt.spsr_irq;
  407. to_vcpu->arch.ctxt.spsr_fiq = from_vcpu->arch.ctxt.spsr_fiq;
  408. /*
  409. * Copy the sysregs, but don't mess with the timer state which
  410. * is directly handled by EL1 and is expected to be preserved.
  411. */
  412. for (i = 1; i < NR_SYS_REGS; i++) {
  413. if (i >= CNTVOFF_EL2 && i <= CNTP_CTL_EL0)
  414. continue;
  415. to_vcpu->arch.ctxt.sys_regs[i] = from_vcpu->arch.ctxt.sys_regs[i];
  416. }
  417. }
  418. static void __sync_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
  419. {
  420. __copy_vcpu_state(&hyp_vcpu->vcpu, hyp_vcpu->host_vcpu);
  421. }
  422. static void __flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
  423. {
  424. __copy_vcpu_state(hyp_vcpu->host_vcpu, &hyp_vcpu->vcpu);
  425. }
  426. static void flush_debug_state(struct pkvm_hyp_vcpu *hyp_vcpu)
  427. {
  428. struct kvm_vcpu *vcpu = &hyp_vcpu->vcpu;
  429. struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
  430. u64 mdcr_el2 = READ_ONCE(host_vcpu->arch.mdcr_el2);
  431. /*
  432. * Propagate the monitor debug configuration of the vcpu from host.
  433. * Preserve HPMN, which is set-up by some knowledgeable bootcode.
  434. * Ensure that MDCR_EL2_E2PB_MASK and MDCR_EL2_E2TB_MASK are clear,
  435. * as guests should not be able to access profiling and trace buffers.
  436. * Ensure that RES0 bits are clear.
  437. */
  438. mdcr_el2 &= ~(MDCR_EL2_RES0 |
  439. MDCR_EL2_HPMN_MASK |
  440. (MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT) |
  441. (MDCR_EL2_E2TB_MASK << MDCR_EL2_E2TB_SHIFT));
  442. vcpu->arch.mdcr_el2 = read_sysreg(mdcr_el2) & MDCR_EL2_HPMN_MASK;
  443. vcpu->arch.mdcr_el2 |= mdcr_el2;
  444. vcpu->arch.pmu = host_vcpu->arch.pmu;
  445. vcpu->guest_debug = READ_ONCE(host_vcpu->guest_debug);
  446. if (!kvm_vcpu_needs_debug_regs(vcpu))
  447. return;
  448. __vcpu_save_guest_debug_regs(vcpu);
  449. /* Switch debug_ptr to the external_debug_state if done by the host. */
  450. if (kern_hyp_va(READ_ONCE(host_vcpu->arch.debug_ptr)) ==
  451. &host_vcpu->arch.external_debug_state)
  452. vcpu->arch.debug_ptr = &host_vcpu->arch.external_debug_state;
  453. /* Propagate any special handling for single step from host. */
  454. vcpu_write_sys_reg(vcpu, vcpu_read_sys_reg(host_vcpu, MDSCR_EL1),
  455. MDSCR_EL1);
  456. *vcpu_cpsr(vcpu) = *vcpu_cpsr(host_vcpu);
  457. }
  458. static void sync_debug_state(struct pkvm_hyp_vcpu *hyp_vcpu)
  459. {
  460. struct kvm_vcpu *vcpu = &hyp_vcpu->vcpu;
  461. struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
  462. if (!kvm_vcpu_needs_debug_regs(vcpu))
  463. return;
  464. __vcpu_restore_guest_debug_regs(vcpu);
  465. vcpu->arch.debug_ptr = &host_vcpu->arch.vcpu_debug_state;
  466. }
  467. static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
  468. {
  469. struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
  470. hyp_entry_exit_handler_fn ec_handler;
  471. u8 esr_ec;
  472. if (READ_ONCE(hyp_vcpu->power_state) == PSCI_0_2_AFFINITY_LEVEL_ON_PENDING)
  473. pkvm_reset_vcpu(hyp_vcpu);
  474. /*
  475. * If we deal with a non-protected guest and the state is potentially
  476. * dirty (from a host perspective), copy the state back into the hyp
  477. * vcpu.
  478. */
  479. if (!pkvm_hyp_vcpu_is_protected(hyp_vcpu)) {
  480. if (vcpu_get_flag(host_vcpu, PKVM_HOST_STATE_DIRTY))
  481. __flush_hyp_vcpu(hyp_vcpu);
  482. hyp_vcpu->vcpu.arch.iflags = READ_ONCE(host_vcpu->arch.iflags);
  483. flush_debug_state(hyp_vcpu);
  484. hyp_vcpu->vcpu.arch.hcr_el2 = HCR_GUEST_FLAGS & ~(HCR_RW | HCR_TWI | HCR_TWE);
  485. hyp_vcpu->vcpu.arch.hcr_el2 |= READ_ONCE(host_vcpu->arch.hcr_el2);
  486. }
  487. hyp_vcpu->vcpu.arch.vsesr_el2 = host_vcpu->arch.vsesr_el2;
  488. flush_hyp_vgic_state(hyp_vcpu);
  489. flush_hyp_timer_state(hyp_vcpu);
  490. switch (ARM_EXCEPTION_CODE(hyp_vcpu->exit_code)) {
  491. case ARM_EXCEPTION_IRQ:
  492. case ARM_EXCEPTION_EL1_SERROR:
  493. case ARM_EXCEPTION_IL:
  494. break;
  495. case ARM_EXCEPTION_TRAP:
  496. esr_ec = ESR_ELx_EC(kvm_vcpu_get_esr(&hyp_vcpu->vcpu));
  497. if (pkvm_hyp_vcpu_is_protected(hyp_vcpu))
  498. ec_handler = entry_hyp_pvm_handlers[esr_ec];
  499. else
  500. ec_handler = entry_hyp_vm_handlers[esr_ec];
  501. if (ec_handler)
  502. ec_handler(hyp_vcpu);
  503. break;
  504. default:
  505. BUG();
  506. }
  507. hyp_vcpu->exit_code = 0;
  508. }
  509. static void sync_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu, u32 exit_reason)
  510. {
  511. struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
  512. hyp_entry_exit_handler_fn ec_handler;
  513. u8 esr_ec;
  514. if (!pkvm_hyp_vcpu_is_protected(hyp_vcpu))
  515. sync_debug_state(hyp_vcpu);
  516. /*
  517. * Don't sync the vcpu GPR/sysreg state after a run. Instead,
  518. * leave it in the hyp vCPU until someone actually requires it.
  519. */
  520. sync_hyp_vgic_state(hyp_vcpu);
  521. sync_hyp_timer_state(hyp_vcpu);
  522. switch (ARM_EXCEPTION_CODE(exit_reason)) {
  523. case ARM_EXCEPTION_IRQ:
  524. break;
  525. case ARM_EXCEPTION_TRAP:
  526. esr_ec = ESR_ELx_EC(kvm_vcpu_get_esr(&hyp_vcpu->vcpu));
  527. if (pkvm_hyp_vcpu_is_protected(hyp_vcpu))
  528. ec_handler = exit_hyp_pvm_handlers[esr_ec];
  529. else
  530. ec_handler = exit_hyp_vm_handlers[esr_ec];
  531. if (ec_handler)
  532. ec_handler(hyp_vcpu);
  533. break;
  534. case ARM_EXCEPTION_EL1_SERROR:
  535. case ARM_EXCEPTION_IL:
  536. break;
  537. default:
  538. BUG();
  539. }
  540. if (pkvm_hyp_vcpu_is_protected(hyp_vcpu))
  541. vcpu_clear_flag(host_vcpu, PC_UPDATE_REQ);
  542. else
  543. host_vcpu->arch.iflags = hyp_vcpu->vcpu.arch.iflags;
  544. hyp_vcpu->exit_code = exit_reason;
  545. }
  546. static void __hyp_sve_save_guest(struct pkvm_hyp_vcpu *hyp_vcpu)
  547. {
  548. struct kvm_vcpu *vcpu = &hyp_vcpu->vcpu;
  549. __sve_save_state(vcpu_sve_pffr(vcpu), &vcpu->arch.ctxt.fp_regs.fpsr);
  550. __vcpu_sys_reg(vcpu, ZCR_EL1) = read_sysreg_el1(SYS_ZCR);
  551. sve_cond_update_zcr_vq(vcpu_sve_max_vq(vcpu) - 1, SYS_ZCR_EL1);
  552. }
  553. static void fpsimd_host_restore(void)
  554. {
  555. sysreg_clear_set(cptr_el2, CPTR_EL2_TZ | CPTR_EL2_TFP, 0);
  556. isb();
  557. if (unlikely(is_protected_kvm_enabled())) {
  558. struct pkvm_hyp_vcpu *hyp_vcpu = pkvm_get_loaded_hyp_vcpu();
  559. struct kvm_vcpu *vcpu = &hyp_vcpu->vcpu;
  560. if (vcpu_has_sve(vcpu))
  561. __hyp_sve_save_guest(hyp_vcpu);
  562. else
  563. __fpsimd_save_state(&hyp_vcpu->vcpu.arch.ctxt.fp_regs);
  564. if (system_supports_sve()) {
  565. struct kvm_host_sve_state *sve_state = get_host_sve_state(vcpu);
  566. write_sysreg_el1(sve_state->zcr_el1, SYS_ZCR);
  567. pkvm_set_max_sve_vq();
  568. __sve_restore_state(sve_state->sve_regs +
  569. sve_ffr_offset(kvm_host_sve_max_vl),
  570. &sve_state->fpsr);
  571. } else {
  572. __fpsimd_restore_state(get_host_fpsimd_state(vcpu));
  573. }
  574. hyp_vcpu->vcpu.arch.fp_state = FP_STATE_HOST_OWNED;
  575. }
  576. if (system_supports_sve())
  577. sve_cond_update_zcr_vq(ZCR_ELx_LEN_MASK, SYS_ZCR_EL2);
  578. }
  579. static void handle___pkvm_vcpu_load(struct kvm_cpu_context *host_ctxt)
  580. {
  581. DECLARE_REG(pkvm_handle_t, handle, host_ctxt, 1);
  582. DECLARE_REG(unsigned int, vcpu_idx, host_ctxt, 2);
  583. DECLARE_REG(u64, hcr_el2, host_ctxt, 3);
  584. struct pkvm_hyp_vcpu *hyp_vcpu;
  585. int __percpu *last_vcpu_ran;
  586. int *last_ran;
  587. if (!is_protected_kvm_enabled())
  588. return;
  589. hyp_vcpu = pkvm_load_hyp_vcpu(handle, vcpu_idx);
  590. if (!hyp_vcpu)
  591. return;
  592. /*
  593. * Guarantee that both TLBs and I-cache are private to each vcpu. If a
  594. * vcpu from the same VM has previously run on the same physical CPU,
  595. * nuke the relevant contexts.
  596. */
  597. last_vcpu_ran = hyp_vcpu->vcpu.arch.hw_mmu->last_vcpu_ran;
  598. last_ran = (__force int *) &last_vcpu_ran[hyp_smp_processor_id()];
  599. if (*last_ran != hyp_vcpu->vcpu.vcpu_id) {
  600. __kvm_flush_cpu_context(hyp_vcpu->vcpu.arch.hw_mmu);
  601. *last_ran = hyp_vcpu->vcpu.vcpu_id;
  602. }
  603. hyp_vcpu->vcpu.arch.fp_state = FP_STATE_HOST_OWNED;
  604. if (pkvm_hyp_vcpu_is_protected(hyp_vcpu)) {
  605. /* Propagate WFx trapping flags, trap ptrauth */
  606. hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWE | HCR_TWI |
  607. HCR_API | HCR_APK);
  608. hyp_vcpu->vcpu.arch.hcr_el2 |= hcr_el2 & (HCR_TWE | HCR_TWI);
  609. }
  610. }
  611. static void handle___pkvm_vcpu_put(struct kvm_cpu_context *host_ctxt)
  612. {
  613. struct pkvm_hyp_vcpu *hyp_vcpu;
  614. if (!is_protected_kvm_enabled())
  615. return;
  616. hyp_vcpu = pkvm_get_loaded_hyp_vcpu();
  617. if (hyp_vcpu) {
  618. struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
  619. if (hyp_vcpu->vcpu.arch.fp_state == FP_STATE_GUEST_OWNED)
  620. fpsimd_host_restore();
  621. if (!pkvm_hyp_vcpu_is_protected(hyp_vcpu) &&
  622. !vcpu_get_flag(host_vcpu, PKVM_HOST_STATE_DIRTY)) {
  623. __sync_hyp_vcpu(hyp_vcpu);
  624. }
  625. pkvm_put_hyp_vcpu(hyp_vcpu);
  626. }
  627. }
  628. static void handle___pkvm_vcpu_sync_state(struct kvm_cpu_context *host_ctxt)
  629. {
  630. struct pkvm_hyp_vcpu *hyp_vcpu;
  631. if (!is_protected_kvm_enabled())
  632. return;
  633. hyp_vcpu = pkvm_get_loaded_hyp_vcpu();
  634. if (!hyp_vcpu || pkvm_hyp_vcpu_is_protected(hyp_vcpu))
  635. return;
  636. if (hyp_vcpu->vcpu.arch.fp_state == FP_STATE_GUEST_OWNED)
  637. fpsimd_host_restore();
  638. __sync_hyp_vcpu(hyp_vcpu);
  639. }
  640. static struct kvm_vcpu *__get_host_hyp_vcpus(struct kvm_vcpu *arg,
  641. struct pkvm_hyp_vcpu **hyp_vcpup)
  642. {
  643. struct kvm_vcpu *host_vcpu = kern_hyp_va(arg);
  644. struct pkvm_hyp_vcpu *hyp_vcpu = NULL;
  645. if (unlikely(is_protected_kvm_enabled())) {
  646. hyp_vcpu = pkvm_get_loaded_hyp_vcpu();
  647. if (!hyp_vcpu || hyp_vcpu->host_vcpu != host_vcpu) {
  648. hyp_vcpu = NULL;
  649. host_vcpu = NULL;
  650. }
  651. }
  652. *hyp_vcpup = hyp_vcpu;
  653. return host_vcpu;
  654. }
  655. #define get_host_hyp_vcpus(ctxt, regnr, hyp_vcpup) \
  656. ({ \
  657. DECLARE_REG(struct kvm_vcpu *, __vcpu, ctxt, regnr); \
  658. __get_host_hyp_vcpus(__vcpu, hyp_vcpup); \
  659. })
  660. #define get_host_hyp_vcpus_from_vgic_v3_cpu_if(ctxt, regnr, hyp_vcpup) \
  661. ({ \
  662. DECLARE_REG(struct vgic_v3_cpu_if *, cif, ctxt, regnr); \
  663. struct kvm_vcpu *__vcpu = container_of(cif, \
  664. struct kvm_vcpu, \
  665. arch.vgic_cpu.vgic_v3); \
  666. \
  667. __get_host_hyp_vcpus(__vcpu, hyp_vcpup); \
  668. })
  669. static void handle___kvm_vcpu_run(struct kvm_cpu_context *host_ctxt)
  670. {
  671. struct pkvm_hyp_vcpu *hyp_vcpu;
  672. struct kvm_vcpu *host_vcpu;
  673. int ret;
  674. host_vcpu = get_host_hyp_vcpus(host_ctxt, 1, &hyp_vcpu);
  675. if (!host_vcpu) {
  676. ret = -EINVAL;
  677. goto out;
  678. }
  679. if (unlikely(hyp_vcpu)) {
  680. flush_hyp_vcpu(hyp_vcpu);
  681. ret = __kvm_vcpu_run(&hyp_vcpu->vcpu);
  682. sync_hyp_vcpu(hyp_vcpu, ret);
  683. if (hyp_vcpu->vcpu.arch.fp_state == FP_STATE_GUEST_OWNED) {
  684. /*
  685. * The guest has used the FP, trap all accesses
  686. * from the host (both FP and SVE).
  687. */
  688. u64 reg = CPTR_EL2_TFP;
  689. if (system_supports_sve())
  690. reg |= CPTR_EL2_TZ;
  691. sysreg_clear_set(cptr_el2, 0, reg);
  692. }
  693. } else {
  694. /* The host is fully trusted, run its vCPU directly. */
  695. ret = __kvm_vcpu_run(host_vcpu);
  696. }
  697. out:
  698. cpu_reg(host_ctxt, 1) = ret;
  699. }
  700. static void handle___pkvm_host_map_guest(struct kvm_cpu_context *host_ctxt)
  701. {
  702. DECLARE_REG(u64, pfn, host_ctxt, 1);
  703. DECLARE_REG(u64, gfn, host_ctxt, 2);
  704. struct pkvm_hyp_vcpu *hyp_vcpu;
  705. int ret = -EINVAL;
  706. if (!is_protected_kvm_enabled())
  707. goto out;
  708. hyp_vcpu = pkvm_get_loaded_hyp_vcpu();
  709. if (!hyp_vcpu)
  710. goto out;
  711. /* Top-up our per-vcpu memcache from the host's */
  712. ret = pkvm_refill_memcache(hyp_vcpu);
  713. if (ret)
  714. goto out;
  715. if (pkvm_hyp_vcpu_is_protected(hyp_vcpu))
  716. ret = __pkvm_host_donate_guest(pfn, gfn, hyp_vcpu);
  717. else
  718. ret = __pkvm_host_share_guest(pfn, gfn, hyp_vcpu);
  719. out:
  720. cpu_reg(host_ctxt, 1) = ret;
  721. }
  722. static void handle___kvm_adjust_pc(struct kvm_cpu_context *host_ctxt)
  723. {
  724. struct pkvm_hyp_vcpu *hyp_vcpu;
  725. struct kvm_vcpu *host_vcpu;
  726. host_vcpu = get_host_hyp_vcpus(host_ctxt, 1, &hyp_vcpu);
  727. if (!host_vcpu)
  728. return;
  729. if (hyp_vcpu) {
  730. /* This only applies to non-protected VMs */
  731. if (pkvm_hyp_vcpu_is_protected(hyp_vcpu))
  732. return;
  733. __kvm_adjust_pc(&hyp_vcpu->vcpu);
  734. } else {
  735. __kvm_adjust_pc(host_vcpu);
  736. }
  737. }
  738. static void handle___kvm_flush_vm_context(struct kvm_cpu_context *host_ctxt)
  739. {
  740. __kvm_flush_vm_context();
  741. }
  742. static void handle___kvm_tlb_flush_vmid_ipa(struct kvm_cpu_context *host_ctxt)
  743. {
  744. DECLARE_REG(struct kvm_s2_mmu *, mmu, host_ctxt, 1);
  745. DECLARE_REG(phys_addr_t, ipa, host_ctxt, 2);
  746. DECLARE_REG(int, level, host_ctxt, 3);
  747. __kvm_tlb_flush_vmid_ipa(kern_hyp_va(mmu), ipa, level);
  748. }
  749. static void handle___kvm_tlb_flush_vmid(struct kvm_cpu_context *host_ctxt)
  750. {
  751. DECLARE_REG(struct kvm_s2_mmu *, mmu, host_ctxt, 1);
  752. __kvm_tlb_flush_vmid(kern_hyp_va(mmu));
  753. }
  754. static void handle___kvm_flush_cpu_context(struct kvm_cpu_context *host_ctxt)
  755. {
  756. DECLARE_REG(struct kvm_s2_mmu *, mmu, host_ctxt, 1);
  757. __kvm_flush_cpu_context(kern_hyp_va(mmu));
  758. }
  759. static void handle___kvm_timer_set_cntvoff(struct kvm_cpu_context *host_ctxt)
  760. {
  761. __kvm_timer_set_cntvoff(cpu_reg(host_ctxt, 1));
  762. }
  763. static void handle___kvm_enable_ssbs(struct kvm_cpu_context *host_ctxt)
  764. {
  765. u64 tmp;
  766. tmp = read_sysreg_el2(SYS_SCTLR);
  767. tmp |= SCTLR_ELx_DSSBS;
  768. write_sysreg_el2(tmp, SYS_SCTLR);
  769. }
  770. static void handle___vgic_v3_get_gic_config(struct kvm_cpu_context *host_ctxt)
  771. {
  772. cpu_reg(host_ctxt, 1) = __vgic_v3_get_gic_config();
  773. }
  774. static void handle___vgic_v3_init_lrs(struct kvm_cpu_context *host_ctxt)
  775. {
  776. __vgic_v3_init_lrs();
  777. }
  778. static void handle___kvm_get_mdcr_el2(struct kvm_cpu_context *host_ctxt)
  779. {
  780. cpu_reg(host_ctxt, 1) = __kvm_get_mdcr_el2();
  781. }
  782. static void handle___vgic_v3_save_vmcr_aprs(struct kvm_cpu_context *host_ctxt)
  783. {
  784. struct pkvm_hyp_vcpu *hyp_vcpu;
  785. struct kvm_vcpu *host_vcpu;
  786. host_vcpu = get_host_hyp_vcpus_from_vgic_v3_cpu_if(host_ctxt, 1,
  787. &hyp_vcpu);
  788. if (!host_vcpu)
  789. return;
  790. if (unlikely(hyp_vcpu)) {
  791. struct vgic_v3_cpu_if *hyp_cpu_if, *host_cpu_if;
  792. int i;
  793. hyp_cpu_if = &hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3;
  794. __vgic_v3_save_vmcr_aprs(hyp_cpu_if);
  795. host_cpu_if = &host_vcpu->arch.vgic_cpu.vgic_v3;
  796. host_cpu_if->vgic_vmcr = hyp_cpu_if->vgic_vmcr;
  797. for (i = 0; i < ARRAY_SIZE(host_cpu_if->vgic_ap0r); i++) {
  798. host_cpu_if->vgic_ap0r[i] = hyp_cpu_if->vgic_ap0r[i];
  799. host_cpu_if->vgic_ap1r[i] = hyp_cpu_if->vgic_ap1r[i];
  800. }
  801. } else {
  802. __vgic_v3_save_vmcr_aprs(&host_vcpu->arch.vgic_cpu.vgic_v3);
  803. }
  804. }
  805. static void handle___vgic_v3_restore_vmcr_aprs(struct kvm_cpu_context *host_ctxt)
  806. {
  807. struct pkvm_hyp_vcpu *hyp_vcpu;
  808. struct kvm_vcpu *host_vcpu;
  809. host_vcpu = get_host_hyp_vcpus_from_vgic_v3_cpu_if(host_ctxt, 1,
  810. &hyp_vcpu);
  811. if (!host_vcpu)
  812. return;
  813. if (unlikely(hyp_vcpu)) {
  814. struct vgic_v3_cpu_if *hyp_cpu_if, *host_cpu_if;
  815. int i;
  816. hyp_cpu_if = &hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3;
  817. host_cpu_if = &host_vcpu->arch.vgic_cpu.vgic_v3;
  818. hyp_cpu_if->vgic_vmcr = host_cpu_if->vgic_vmcr;
  819. /* Should be a one-off */
  820. hyp_cpu_if->vgic_sre = (ICC_SRE_EL1_DIB |
  821. ICC_SRE_EL1_DFB |
  822. ICC_SRE_EL1_SRE);
  823. for (i = 0; i < ARRAY_SIZE(host_cpu_if->vgic_ap0r); i++) {
  824. hyp_cpu_if->vgic_ap0r[i] = host_cpu_if->vgic_ap0r[i];
  825. hyp_cpu_if->vgic_ap1r[i] = host_cpu_if->vgic_ap1r[i];
  826. }
  827. __vgic_v3_restore_vmcr_aprs(hyp_cpu_if);
  828. } else {
  829. __vgic_v3_restore_vmcr_aprs(&host_vcpu->arch.vgic_cpu.vgic_v3);
  830. }
  831. }
  832. static void handle___pkvm_init(struct kvm_cpu_context *host_ctxt)
  833. {
  834. DECLARE_REG(phys_addr_t, phys, host_ctxt, 1);
  835. DECLARE_REG(unsigned long, size, host_ctxt, 2);
  836. DECLARE_REG(unsigned long, nr_cpus, host_ctxt, 3);
  837. DECLARE_REG(unsigned long *, per_cpu_base, host_ctxt, 4);
  838. DECLARE_REG(u32, hyp_va_bits, host_ctxt, 5);
  839. /*
  840. * __pkvm_init() will return only if an error occurred, otherwise it
  841. * will tail-call in __pkvm_init_finalise() which will have to deal
  842. * with the host context directly.
  843. */
  844. cpu_reg(host_ctxt, 1) = __pkvm_init(phys, size, nr_cpus, per_cpu_base,
  845. hyp_va_bits);
  846. }
  847. static void handle___pkvm_cpu_set_vector(struct kvm_cpu_context *host_ctxt)
  848. {
  849. DECLARE_REG(enum arm64_hyp_spectre_vector, slot, host_ctxt, 1);
  850. cpu_reg(host_ctxt, 1) = pkvm_cpu_set_vector(slot);
  851. }
  852. static void handle___pkvm_host_share_hyp(struct kvm_cpu_context *host_ctxt)
  853. {
  854. DECLARE_REG(u64, pfn, host_ctxt, 1);
  855. cpu_reg(host_ctxt, 1) = __pkvm_host_share_hyp(pfn);
  856. }
  857. static void handle___pkvm_host_unshare_hyp(struct kvm_cpu_context *host_ctxt)
  858. {
  859. DECLARE_REG(u64, pfn, host_ctxt, 1);
  860. cpu_reg(host_ctxt, 1) = __pkvm_host_unshare_hyp(pfn);
  861. }
  862. static void handle___pkvm_reclaim_dying_guest_page(struct kvm_cpu_context *host_ctxt)
  863. {
  864. DECLARE_REG(pkvm_handle_t, handle, host_ctxt, 1);
  865. DECLARE_REG(u64, pfn, host_ctxt, 2);
  866. DECLARE_REG(u64, ipa, host_ctxt, 3);
  867. cpu_reg(host_ctxt, 1) = __pkvm_reclaim_dying_guest_page(handle, pfn, ipa);
  868. }
  869. static void handle___pkvm_create_private_mapping(struct kvm_cpu_context *host_ctxt)
  870. {
  871. DECLARE_REG(phys_addr_t, phys, host_ctxt, 1);
  872. DECLARE_REG(size_t, size, host_ctxt, 2);
  873. DECLARE_REG(enum kvm_pgtable_prot, prot, host_ctxt, 3);
  874. /*
  875. * __pkvm_create_private_mapping() populates a pointer with the
  876. * hypervisor start address of the allocation.
  877. *
  878. * However, handle___pkvm_create_private_mapping() hypercall crosses the
  879. * EL1/EL2 boundary so the pointer would not be valid in this context.
  880. *
  881. * Instead pass the allocation address as the return value (or return
  882. * ERR_PTR() on failure).
  883. */
  884. unsigned long haddr;
  885. int err = __pkvm_create_private_mapping(phys, size, prot, &haddr);
  886. if (err)
  887. haddr = (unsigned long)ERR_PTR(err);
  888. cpu_reg(host_ctxt, 1) = haddr;
  889. }
  890. static void handle___pkvm_prot_finalize(struct kvm_cpu_context *host_ctxt)
  891. {
  892. cpu_reg(host_ctxt, 1) = __pkvm_prot_finalize();
  893. }
  894. static void handle___pkvm_init_vm(struct kvm_cpu_context *host_ctxt)
  895. {
  896. DECLARE_REG(struct kvm *, host_kvm, host_ctxt, 1);
  897. DECLARE_REG(unsigned long, vm_hva, host_ctxt, 2);
  898. DECLARE_REG(unsigned long, pgd_hva, host_ctxt, 3);
  899. DECLARE_REG(unsigned long, last_ran_hva, host_ctxt, 4);
  900. host_kvm = kern_hyp_va(host_kvm);
  901. cpu_reg(host_ctxt, 1) = __pkvm_init_vm(host_kvm, vm_hva, pgd_hva,
  902. last_ran_hva);
  903. }
  904. static void handle___pkvm_init_vcpu(struct kvm_cpu_context *host_ctxt)
  905. {
  906. DECLARE_REG(pkvm_handle_t, handle, host_ctxt, 1);
  907. DECLARE_REG(struct kvm_vcpu *, host_vcpu, host_ctxt, 2);
  908. DECLARE_REG(unsigned long, vcpu_hva, host_ctxt, 3);
  909. host_vcpu = kern_hyp_va(host_vcpu);
  910. cpu_reg(host_ctxt, 1) = __pkvm_init_vcpu(handle, host_vcpu, vcpu_hva);
  911. }
  912. static void handle___pkvm_start_teardown_vm(struct kvm_cpu_context *host_ctxt)
  913. {
  914. DECLARE_REG(pkvm_handle_t, handle, host_ctxt, 1);
  915. cpu_reg(host_ctxt, 1) = __pkvm_start_teardown_vm(handle);
  916. }
  917. static void handle___pkvm_finalize_teardown_vm(struct kvm_cpu_context *host_ctxt)
  918. {
  919. DECLARE_REG(pkvm_handle_t, handle, host_ctxt, 1);
  920. cpu_reg(host_ctxt, 1) = __pkvm_finalize_teardown_vm(handle);
  921. }
  922. static void handle___pkvm_iommu_driver_init(struct kvm_cpu_context *host_ctxt)
  923. {
  924. DECLARE_REG(struct pkvm_iommu_driver*, drv, host_ctxt, 1);
  925. DECLARE_REG(void *, data, host_ctxt, 2);
  926. DECLARE_REG(size_t, size, host_ctxt, 3);
  927. data = kern_hyp_va(data);
  928. cpu_reg(host_ctxt, 1) = __pkvm_iommu_driver_init(drv, data, size);
  929. }
  930. static void handle___pkvm_iommu_register(struct kvm_cpu_context *host_ctxt)
  931. {
  932. DECLARE_REG(unsigned long, dev_id, host_ctxt, 1);
  933. DECLARE_REG(unsigned long, drv_id, host_ctxt, 2);
  934. DECLARE_REG(phys_addr_t, dev_pa, host_ctxt, 3);
  935. DECLARE_REG(size_t, dev_size, host_ctxt, 4);
  936. DECLARE_REG(unsigned long, parent_id, host_ctxt, 5);
  937. DECLARE_REG(u8, flags, host_ctxt, 6);
  938. DECLARE_REG(void *, mem, host_ctxt, 7);
  939. cpu_reg(host_ctxt, 1) = __pkvm_iommu_register(dev_id, drv_id, dev_pa,
  940. dev_size, parent_id, flags, mem);
  941. }
  942. static void handle___pkvm_iommu_pm_notify(struct kvm_cpu_context *host_ctxt)
  943. {
  944. DECLARE_REG(unsigned long, dev_id, host_ctxt, 1);
  945. DECLARE_REG(enum pkvm_iommu_pm_event, event, host_ctxt, 2);
  946. cpu_reg(host_ctxt, 1) = __pkvm_iommu_pm_notify(dev_id, event);
  947. }
  948. static void handle___pkvm_iommu_finalize(struct kvm_cpu_context *host_ctxt)
  949. {
  950. DECLARE_REG(int, err, host_ctxt, 1);
  951. cpu_reg(host_ctxt, 1) = __pkvm_iommu_finalize(err);
  952. }
  953. static void handle___pkvm_alloc_module_va(struct kvm_cpu_context *host_ctxt)
  954. {
  955. DECLARE_REG(u64, nr_pages, host_ctxt, 1);
  956. cpu_reg(host_ctxt, 1) = (u64)__pkvm_alloc_module_va(nr_pages);
  957. }
  958. static void handle___pkvm_map_module_page(struct kvm_cpu_context *host_ctxt)
  959. {
  960. DECLARE_REG(u64, pfn, host_ctxt, 1);
  961. DECLARE_REG(void *, va, host_ctxt, 2);
  962. DECLARE_REG(enum kvm_pgtable_prot, prot, host_ctxt, 3);
  963. cpu_reg(host_ctxt, 1) = (u64)__pkvm_map_module_page(pfn, va, prot, false);
  964. }
  965. static void handle___pkvm_unmap_module_page(struct kvm_cpu_context *host_ctxt)
  966. {
  967. DECLARE_REG(u64, pfn, host_ctxt, 1);
  968. DECLARE_REG(void *, va, host_ctxt, 2);
  969. __pkvm_unmap_module_page(pfn, va);
  970. }
  971. static void handle___pkvm_init_module(struct kvm_cpu_context *host_ctxt)
  972. {
  973. DECLARE_REG(void *, ptr, host_ctxt, 1);
  974. cpu_reg(host_ctxt, 1) = __pkvm_init_module(ptr);
  975. }
  976. static void handle___pkvm_register_hcall(struct kvm_cpu_context *host_ctxt)
  977. {
  978. DECLARE_REG(unsigned long, hfn_hyp_va, host_ctxt, 1);
  979. cpu_reg(host_ctxt, 1) = __pkvm_register_hcall(hfn_hyp_va);
  980. }
  981. static void handle___pkvm_load_tracing(struct kvm_cpu_context *host_ctxt)
  982. {
  983. DECLARE_REG(unsigned long, pack_hva, host_ctxt, 1);
  984. DECLARE_REG(size_t, pack_size, host_ctxt, 2);
  985. cpu_reg(host_ctxt, 1) = __pkvm_load_tracing(pack_hva, pack_size);
  986. }
  987. static void handle___pkvm_teardown_tracing(struct kvm_cpu_context *host_ctxt)
  988. {
  989. __pkvm_teardown_tracing();
  990. cpu_reg(host_ctxt, 1) = 0;
  991. }
  992. static void handle___pkvm_enable_tracing(struct kvm_cpu_context *host_ctxt)
  993. {
  994. DECLARE_REG(bool, enable, host_ctxt, 1);
  995. cpu_reg(host_ctxt, 1) = __pkvm_enable_tracing(enable);
  996. }
  997. static void handle___pkvm_rb_swap_reader_page(struct kvm_cpu_context *host_ctxt)
  998. {
  999. DECLARE_REG(int, cpu, host_ctxt, 1);
  1000. cpu_reg(host_ctxt, 1) = __pkvm_rb_swap_reader_page(cpu);
  1001. }
  1002. static void handle___pkvm_rb_update_footers(struct kvm_cpu_context *host_ctxt)
  1003. {
  1004. DECLARE_REG(int, cpu, host_ctxt, 1);
  1005. cpu_reg(host_ctxt, 1) = __pkvm_rb_update_footers(cpu);
  1006. }
  1007. static void handle___pkvm_enable_event(struct kvm_cpu_context *host_ctxt)
  1008. {
  1009. DECLARE_REG(unsigned short, id, host_ctxt, 1);
  1010. DECLARE_REG(bool, enable, host_ctxt, 2);
  1011. cpu_reg(host_ctxt, 1) = __pkvm_enable_event(id, enable);
  1012. }
  1013. #ifdef CONFIG_ANDROID_ARM64_WORKAROUND_DMA_BEYOND_POC
  1014. extern int __pkvm_host_set_stage2_memattr(phys_addr_t phys, bool force_nc);
  1015. static void handle___pkvm_host_set_stage2_memattr(struct kvm_cpu_context *host_ctxt)
  1016. {
  1017. DECLARE_REG(phys_addr_t, phys, host_ctxt, 1);
  1018. DECLARE_REG(bool, force_nc, host_ctxt, 2);
  1019. cpu_reg(host_ctxt, 1) = __pkvm_host_set_stage2_memattr(phys, force_nc);
  1020. }
  1021. #endif
  1022. typedef void (*hcall_t)(struct kvm_cpu_context *);
  1023. #define HANDLE_FUNC(x) [__KVM_HOST_SMCCC_FUNC_##x] = (hcall_t)handle_##x
  1024. static const hcall_t host_hcall[] = {
  1025. /* ___kvm_hyp_init */
  1026. HANDLE_FUNC(__kvm_get_mdcr_el2),
  1027. HANDLE_FUNC(__pkvm_init),
  1028. HANDLE_FUNC(__pkvm_create_private_mapping),
  1029. HANDLE_FUNC(__pkvm_cpu_set_vector),
  1030. HANDLE_FUNC(__kvm_enable_ssbs),
  1031. HANDLE_FUNC(__vgic_v3_init_lrs),
  1032. HANDLE_FUNC(__vgic_v3_get_gic_config),
  1033. HANDLE_FUNC(__kvm_flush_vm_context),
  1034. HANDLE_FUNC(__kvm_tlb_flush_vmid_ipa),
  1035. HANDLE_FUNC(__kvm_tlb_flush_vmid),
  1036. HANDLE_FUNC(__kvm_flush_cpu_context),
  1037. HANDLE_FUNC(__pkvm_alloc_module_va),
  1038. HANDLE_FUNC(__pkvm_map_module_page),
  1039. HANDLE_FUNC(__pkvm_unmap_module_page),
  1040. HANDLE_FUNC(__pkvm_init_module),
  1041. HANDLE_FUNC(__pkvm_register_hcall),
  1042. HANDLE_FUNC(__pkvm_prot_finalize),
  1043. HANDLE_FUNC(__pkvm_host_share_hyp),
  1044. HANDLE_FUNC(__pkvm_host_unshare_hyp),
  1045. HANDLE_FUNC(__pkvm_host_map_guest),
  1046. HANDLE_FUNC(__kvm_adjust_pc),
  1047. HANDLE_FUNC(__kvm_vcpu_run),
  1048. HANDLE_FUNC(__kvm_timer_set_cntvoff),
  1049. HANDLE_FUNC(__vgic_v3_save_vmcr_aprs),
  1050. HANDLE_FUNC(__vgic_v3_restore_vmcr_aprs),
  1051. HANDLE_FUNC(__pkvm_init_vm),
  1052. HANDLE_FUNC(__pkvm_init_vcpu),
  1053. HANDLE_FUNC(__pkvm_start_teardown_vm),
  1054. HANDLE_FUNC(__pkvm_finalize_teardown_vm),
  1055. HANDLE_FUNC(__pkvm_reclaim_dying_guest_page),
  1056. HANDLE_FUNC(__pkvm_vcpu_load),
  1057. HANDLE_FUNC(__pkvm_vcpu_put),
  1058. HANDLE_FUNC(__pkvm_vcpu_sync_state),
  1059. HANDLE_FUNC(__pkvm_iommu_driver_init),
  1060. HANDLE_FUNC(__pkvm_iommu_register),
  1061. HANDLE_FUNC(__pkvm_iommu_pm_notify),
  1062. HANDLE_FUNC(__pkvm_iommu_finalize),
  1063. HANDLE_FUNC(__pkvm_load_tracing),
  1064. HANDLE_FUNC(__pkvm_teardown_tracing),
  1065. HANDLE_FUNC(__pkvm_enable_tracing),
  1066. HANDLE_FUNC(__pkvm_rb_swap_reader_page),
  1067. HANDLE_FUNC(__pkvm_rb_update_footers),
  1068. HANDLE_FUNC(__pkvm_enable_event),
  1069. #ifdef CONFIG_ANDROID_ARM64_WORKAROUND_DMA_BEYOND_POC
  1070. HANDLE_FUNC(__pkvm_host_set_stage2_memattr),
  1071. #endif
  1072. };
  1073. static void handle_host_hcall(struct kvm_cpu_context *host_ctxt)
  1074. {
  1075. DECLARE_REG(unsigned long, id, host_ctxt, 0);
  1076. unsigned long hcall_min = 0;
  1077. hcall_t hfn;
  1078. if (handle_host_dynamic_hcall(host_ctxt) == HCALL_HANDLED)
  1079. return;
  1080. /*
  1081. * If pKVM has been initialised then reject any calls to the
  1082. * early "privileged" hypercalls. Note that we cannot reject
  1083. * calls to __pkvm_prot_finalize for two reasons: (1) The static
  1084. * key used to determine initialisation must be toggled prior to
  1085. * finalisation and (2) finalisation is performed on a per-CPU
  1086. * basis. This is all fine, however, since __pkvm_prot_finalize
  1087. * returns -EPERM after the first call for a given CPU.
  1088. */
  1089. if (static_branch_unlikely(&kvm_protected_mode_initialized))
  1090. hcall_min = __KVM_HOST_SMCCC_FUNC___pkvm_prot_finalize;
  1091. id -= KVM_HOST_SMCCC_ID(0);
  1092. if (unlikely(id < hcall_min || id >= ARRAY_SIZE(host_hcall)))
  1093. goto inval;
  1094. hfn = host_hcall[id];
  1095. if (unlikely(!hfn))
  1096. goto inval;
  1097. cpu_reg(host_ctxt, 0) = SMCCC_RET_SUCCESS;
  1098. hfn(host_ctxt);
  1099. trace_host_hcall(id, 0);
  1100. return;
  1101. inval:
  1102. trace_host_hcall(id, 1);
  1103. cpu_reg(host_ctxt, 0) = SMCCC_RET_NOT_SUPPORTED;
  1104. }
  1105. static void handle_host_smc(struct kvm_cpu_context *host_ctxt)
  1106. {
  1107. DECLARE_REG(u64, func_id, host_ctxt, 0);
  1108. struct pkvm_hyp_vcpu *hyp_vcpu;
  1109. bool handled;
  1110. hyp_vcpu = pkvm_get_loaded_hyp_vcpu();
  1111. if (hyp_vcpu && hyp_vcpu->vcpu.arch.fp_state == FP_STATE_GUEST_OWNED)
  1112. fpsimd_host_restore();
  1113. handled = kvm_host_psci_handler(host_ctxt);
  1114. if (!handled)
  1115. handled = kvm_host_ffa_handler(host_ctxt);
  1116. if (!handled && smp_load_acquire(&default_host_smc_handler))
  1117. handled = default_host_smc_handler(host_ctxt);
  1118. trace_host_smc(func_id, !handled);
  1119. if (!handled) {
  1120. trace_hyp_exit();
  1121. __kvm_hyp_host_forward_smc(host_ctxt);
  1122. trace_hyp_enter();
  1123. }
  1124. /* SMC was trapped, move ELR past the current PC. */
  1125. kvm_skip_host_instr();
  1126. }
  1127. void handle_trap(struct kvm_cpu_context *host_ctxt)
  1128. {
  1129. u64 esr = read_sysreg_el2(SYS_ESR);
  1130. trace_hyp_enter();
  1131. switch (ESR_ELx_EC(esr)) {
  1132. case ESR_ELx_EC_HVC64:
  1133. handle_host_hcall(host_ctxt);
  1134. break;
  1135. case ESR_ELx_EC_SMC64:
  1136. handle_host_smc(host_ctxt);
  1137. break;
  1138. case ESR_ELx_EC_FP_ASIMD:
  1139. case ESR_ELx_EC_SVE:
  1140. fpsimd_host_restore();
  1141. break;
  1142. case ESR_ELx_EC_IABT_LOW:
  1143. case ESR_ELx_EC_DABT_LOW:
  1144. handle_host_mem_abort(host_ctxt);
  1145. break;
  1146. default:
  1147. BUG_ON(!READ_ONCE(default_trap_handler) || !default_trap_handler(host_ctxt));
  1148. }
  1149. trace_hyp_exit();
  1150. }