book3s_hv_nested.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corporation, 2018
  4. * Authors Suraj Jitindar Singh <[email protected]>
  5. * Paul Mackerras <[email protected]>
  6. *
  7. * Description: KVM functions specific to running nested KVM-HV guests
  8. * on Book3S processors (specifically POWER9 and later).
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/kvm_host.h>
  12. #include <linux/llist.h>
  13. #include <linux/pgtable.h>
  14. #include <asm/kvm_ppc.h>
  15. #include <asm/kvm_book3s.h>
  16. #include <asm/mmu.h>
  17. #include <asm/pgalloc.h>
  18. #include <asm/pte-walk.h>
  19. #include <asm/reg.h>
  20. #include <asm/plpar_wrappers.h>
  21. #include <asm/firmware.h>
  22. static struct patb_entry *pseries_partition_tb;
  23. static void kvmhv_update_ptbl_cache(struct kvm_nested_guest *gp);
  24. static void kvmhv_free_memslot_nest_rmap(struct kvm_memory_slot *free);
  25. void kvmhv_save_hv_regs(struct kvm_vcpu *vcpu, struct hv_guest_state *hr)
  26. {
  27. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  28. hr->pcr = vc->pcr | PCR_MASK;
  29. hr->dpdes = vc->dpdes;
  30. hr->hfscr = vcpu->arch.hfscr;
  31. hr->tb_offset = vc->tb_offset;
  32. hr->dawr0 = vcpu->arch.dawr0;
  33. hr->dawrx0 = vcpu->arch.dawrx0;
  34. hr->ciabr = vcpu->arch.ciabr;
  35. hr->purr = vcpu->arch.purr;
  36. hr->spurr = vcpu->arch.spurr;
  37. hr->ic = vcpu->arch.ic;
  38. hr->vtb = vc->vtb;
  39. hr->srr0 = vcpu->arch.shregs.srr0;
  40. hr->srr1 = vcpu->arch.shregs.srr1;
  41. hr->sprg[0] = vcpu->arch.shregs.sprg0;
  42. hr->sprg[1] = vcpu->arch.shregs.sprg1;
  43. hr->sprg[2] = vcpu->arch.shregs.sprg2;
  44. hr->sprg[3] = vcpu->arch.shregs.sprg3;
  45. hr->pidr = vcpu->arch.pid;
  46. hr->cfar = vcpu->arch.cfar;
  47. hr->ppr = vcpu->arch.ppr;
  48. hr->dawr1 = vcpu->arch.dawr1;
  49. hr->dawrx1 = vcpu->arch.dawrx1;
  50. }
  51. /* Use noinline_for_stack due to https://bugs.llvm.org/show_bug.cgi?id=49610 */
  52. static noinline_for_stack void byteswap_pt_regs(struct pt_regs *regs)
  53. {
  54. unsigned long *addr = (unsigned long *) regs;
  55. for (; addr < ((unsigned long *) (regs + 1)); addr++)
  56. *addr = swab64(*addr);
  57. }
  58. static void byteswap_hv_regs(struct hv_guest_state *hr)
  59. {
  60. hr->version = swab64(hr->version);
  61. hr->lpid = swab32(hr->lpid);
  62. hr->vcpu_token = swab32(hr->vcpu_token);
  63. hr->lpcr = swab64(hr->lpcr);
  64. hr->pcr = swab64(hr->pcr) | PCR_MASK;
  65. hr->amor = swab64(hr->amor);
  66. hr->dpdes = swab64(hr->dpdes);
  67. hr->hfscr = swab64(hr->hfscr);
  68. hr->tb_offset = swab64(hr->tb_offset);
  69. hr->dawr0 = swab64(hr->dawr0);
  70. hr->dawrx0 = swab64(hr->dawrx0);
  71. hr->ciabr = swab64(hr->ciabr);
  72. hr->hdec_expiry = swab64(hr->hdec_expiry);
  73. hr->purr = swab64(hr->purr);
  74. hr->spurr = swab64(hr->spurr);
  75. hr->ic = swab64(hr->ic);
  76. hr->vtb = swab64(hr->vtb);
  77. hr->hdar = swab64(hr->hdar);
  78. hr->hdsisr = swab64(hr->hdsisr);
  79. hr->heir = swab64(hr->heir);
  80. hr->asdr = swab64(hr->asdr);
  81. hr->srr0 = swab64(hr->srr0);
  82. hr->srr1 = swab64(hr->srr1);
  83. hr->sprg[0] = swab64(hr->sprg[0]);
  84. hr->sprg[1] = swab64(hr->sprg[1]);
  85. hr->sprg[2] = swab64(hr->sprg[2]);
  86. hr->sprg[3] = swab64(hr->sprg[3]);
  87. hr->pidr = swab64(hr->pidr);
  88. hr->cfar = swab64(hr->cfar);
  89. hr->ppr = swab64(hr->ppr);
  90. hr->dawr1 = swab64(hr->dawr1);
  91. hr->dawrx1 = swab64(hr->dawrx1);
  92. }
  93. static void save_hv_return_state(struct kvm_vcpu *vcpu,
  94. struct hv_guest_state *hr)
  95. {
  96. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  97. hr->dpdes = vc->dpdes;
  98. hr->purr = vcpu->arch.purr;
  99. hr->spurr = vcpu->arch.spurr;
  100. hr->ic = vcpu->arch.ic;
  101. hr->vtb = vc->vtb;
  102. hr->srr0 = vcpu->arch.shregs.srr0;
  103. hr->srr1 = vcpu->arch.shregs.srr1;
  104. hr->sprg[0] = vcpu->arch.shregs.sprg0;
  105. hr->sprg[1] = vcpu->arch.shregs.sprg1;
  106. hr->sprg[2] = vcpu->arch.shregs.sprg2;
  107. hr->sprg[3] = vcpu->arch.shregs.sprg3;
  108. hr->pidr = vcpu->arch.pid;
  109. hr->cfar = vcpu->arch.cfar;
  110. hr->ppr = vcpu->arch.ppr;
  111. switch (vcpu->arch.trap) {
  112. case BOOK3S_INTERRUPT_H_DATA_STORAGE:
  113. hr->hdar = vcpu->arch.fault_dar;
  114. hr->hdsisr = vcpu->arch.fault_dsisr;
  115. hr->asdr = vcpu->arch.fault_gpa;
  116. break;
  117. case BOOK3S_INTERRUPT_H_INST_STORAGE:
  118. hr->asdr = vcpu->arch.fault_gpa;
  119. break;
  120. case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
  121. hr->hfscr = ((~HFSCR_INTR_CAUSE & hr->hfscr) |
  122. (HFSCR_INTR_CAUSE & vcpu->arch.hfscr));
  123. break;
  124. case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
  125. hr->heir = vcpu->arch.emul_inst;
  126. break;
  127. }
  128. }
  129. static void restore_hv_regs(struct kvm_vcpu *vcpu, const struct hv_guest_state *hr)
  130. {
  131. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  132. vc->pcr = hr->pcr | PCR_MASK;
  133. vc->dpdes = hr->dpdes;
  134. vcpu->arch.hfscr = hr->hfscr;
  135. vcpu->arch.dawr0 = hr->dawr0;
  136. vcpu->arch.dawrx0 = hr->dawrx0;
  137. vcpu->arch.ciabr = hr->ciabr;
  138. vcpu->arch.purr = hr->purr;
  139. vcpu->arch.spurr = hr->spurr;
  140. vcpu->arch.ic = hr->ic;
  141. vc->vtb = hr->vtb;
  142. vcpu->arch.shregs.srr0 = hr->srr0;
  143. vcpu->arch.shregs.srr1 = hr->srr1;
  144. vcpu->arch.shregs.sprg0 = hr->sprg[0];
  145. vcpu->arch.shregs.sprg1 = hr->sprg[1];
  146. vcpu->arch.shregs.sprg2 = hr->sprg[2];
  147. vcpu->arch.shregs.sprg3 = hr->sprg[3];
  148. vcpu->arch.pid = hr->pidr;
  149. vcpu->arch.cfar = hr->cfar;
  150. vcpu->arch.ppr = hr->ppr;
  151. vcpu->arch.dawr1 = hr->dawr1;
  152. vcpu->arch.dawrx1 = hr->dawrx1;
  153. }
  154. void kvmhv_restore_hv_return_state(struct kvm_vcpu *vcpu,
  155. struct hv_guest_state *hr)
  156. {
  157. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  158. vc->dpdes = hr->dpdes;
  159. vcpu->arch.hfscr = hr->hfscr;
  160. vcpu->arch.purr = hr->purr;
  161. vcpu->arch.spurr = hr->spurr;
  162. vcpu->arch.ic = hr->ic;
  163. vc->vtb = hr->vtb;
  164. vcpu->arch.fault_dar = hr->hdar;
  165. vcpu->arch.fault_dsisr = hr->hdsisr;
  166. vcpu->arch.fault_gpa = hr->asdr;
  167. vcpu->arch.emul_inst = hr->heir;
  168. vcpu->arch.shregs.srr0 = hr->srr0;
  169. vcpu->arch.shregs.srr1 = hr->srr1;
  170. vcpu->arch.shregs.sprg0 = hr->sprg[0];
  171. vcpu->arch.shregs.sprg1 = hr->sprg[1];
  172. vcpu->arch.shregs.sprg2 = hr->sprg[2];
  173. vcpu->arch.shregs.sprg3 = hr->sprg[3];
  174. vcpu->arch.pid = hr->pidr;
  175. vcpu->arch.cfar = hr->cfar;
  176. vcpu->arch.ppr = hr->ppr;
  177. }
  178. static void kvmhv_nested_mmio_needed(struct kvm_vcpu *vcpu, u64 regs_ptr)
  179. {
  180. /* No need to reflect the page fault to L1, we've handled it */
  181. vcpu->arch.trap = 0;
  182. /*
  183. * Since the L2 gprs have already been written back into L1 memory when
  184. * we complete the mmio, store the L1 memory location of the L2 gpr
  185. * being loaded into by the mmio so that the loaded value can be
  186. * written there in kvmppc_complete_mmio_load()
  187. */
  188. if (((vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) == KVM_MMIO_REG_GPR)
  189. && (vcpu->mmio_is_write == 0)) {
  190. vcpu->arch.nested_io_gpr = (gpa_t) regs_ptr +
  191. offsetof(struct pt_regs,
  192. gpr[vcpu->arch.io_gpr]);
  193. vcpu->arch.io_gpr = KVM_MMIO_REG_NESTED_GPR;
  194. }
  195. }
  196. static int kvmhv_read_guest_state_and_regs(struct kvm_vcpu *vcpu,
  197. struct hv_guest_state *l2_hv,
  198. struct pt_regs *l2_regs,
  199. u64 hv_ptr, u64 regs_ptr)
  200. {
  201. int size;
  202. if (kvm_vcpu_read_guest(vcpu, hv_ptr, &l2_hv->version,
  203. sizeof(l2_hv->version)))
  204. return -1;
  205. if (kvmppc_need_byteswap(vcpu))
  206. l2_hv->version = swab64(l2_hv->version);
  207. size = hv_guest_state_size(l2_hv->version);
  208. if (size < 0)
  209. return -1;
  210. return kvm_vcpu_read_guest(vcpu, hv_ptr, l2_hv, size) ||
  211. kvm_vcpu_read_guest(vcpu, regs_ptr, l2_regs,
  212. sizeof(struct pt_regs));
  213. }
  214. static int kvmhv_write_guest_state_and_regs(struct kvm_vcpu *vcpu,
  215. struct hv_guest_state *l2_hv,
  216. struct pt_regs *l2_regs,
  217. u64 hv_ptr, u64 regs_ptr)
  218. {
  219. int size;
  220. size = hv_guest_state_size(l2_hv->version);
  221. if (size < 0)
  222. return -1;
  223. return kvm_vcpu_write_guest(vcpu, hv_ptr, l2_hv, size) ||
  224. kvm_vcpu_write_guest(vcpu, regs_ptr, l2_regs,
  225. sizeof(struct pt_regs));
  226. }
  227. static void load_l2_hv_regs(struct kvm_vcpu *vcpu,
  228. const struct hv_guest_state *l2_hv,
  229. const struct hv_guest_state *l1_hv, u64 *lpcr)
  230. {
  231. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  232. u64 mask;
  233. restore_hv_regs(vcpu, l2_hv);
  234. /*
  235. * Don't let L1 change LPCR bits for the L2 except these:
  236. */
  237. mask = LPCR_DPFD | LPCR_ILE | LPCR_TC | LPCR_AIL | LPCR_LD | LPCR_MER;
  238. /*
  239. * Additional filtering is required depending on hardware
  240. * and configuration.
  241. */
  242. *lpcr = kvmppc_filter_lpcr_hv(vcpu->kvm,
  243. (vc->lpcr & ~mask) | (*lpcr & mask));
  244. /*
  245. * Don't let L1 enable features for L2 which we don't allow for L1,
  246. * but preserve the interrupt cause field.
  247. */
  248. vcpu->arch.hfscr = l2_hv->hfscr & (HFSCR_INTR_CAUSE | vcpu->arch.hfscr_permitted);
  249. /* Don't let data address watchpoint match in hypervisor state */
  250. vcpu->arch.dawrx0 = l2_hv->dawrx0 & ~DAWRX_HYP;
  251. vcpu->arch.dawrx1 = l2_hv->dawrx1 & ~DAWRX_HYP;
  252. /* Don't let completed instruction address breakpt match in HV state */
  253. if ((l2_hv->ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
  254. vcpu->arch.ciabr = l2_hv->ciabr & ~CIABR_PRIV;
  255. }
  256. long kvmhv_enter_nested_guest(struct kvm_vcpu *vcpu)
  257. {
  258. long int err, r;
  259. struct kvm_nested_guest *l2;
  260. struct pt_regs l2_regs, saved_l1_regs;
  261. struct hv_guest_state l2_hv = {0}, saved_l1_hv;
  262. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  263. u64 hv_ptr, regs_ptr;
  264. u64 hdec_exp, lpcr;
  265. s64 delta_purr, delta_spurr, delta_ic, delta_vtb;
  266. if (vcpu->kvm->arch.l1_ptcr == 0)
  267. return H_NOT_AVAILABLE;
  268. if (MSR_TM_TRANSACTIONAL(vcpu->arch.shregs.msr))
  269. return H_BAD_MODE;
  270. /* copy parameters in */
  271. hv_ptr = kvmppc_get_gpr(vcpu, 4);
  272. regs_ptr = kvmppc_get_gpr(vcpu, 5);
  273. kvm_vcpu_srcu_read_lock(vcpu);
  274. err = kvmhv_read_guest_state_and_regs(vcpu, &l2_hv, &l2_regs,
  275. hv_ptr, regs_ptr);
  276. kvm_vcpu_srcu_read_unlock(vcpu);
  277. if (err)
  278. return H_PARAMETER;
  279. if (kvmppc_need_byteswap(vcpu))
  280. byteswap_hv_regs(&l2_hv);
  281. if (l2_hv.version > HV_GUEST_STATE_VERSION)
  282. return H_P2;
  283. if (kvmppc_need_byteswap(vcpu))
  284. byteswap_pt_regs(&l2_regs);
  285. if (l2_hv.vcpu_token >= NR_CPUS)
  286. return H_PARAMETER;
  287. /*
  288. * L1 must have set up a suspended state to enter the L2 in a
  289. * transactional state, and only in that case. These have to be
  290. * filtered out here to prevent causing a TM Bad Thing in the
  291. * host HRFID. We could synthesize a TM Bad Thing back to the L1
  292. * here but there doesn't seem like much point.
  293. */
  294. if (MSR_TM_SUSPENDED(vcpu->arch.shregs.msr)) {
  295. if (!MSR_TM_ACTIVE(l2_regs.msr))
  296. return H_BAD_MODE;
  297. } else {
  298. if (l2_regs.msr & MSR_TS_MASK)
  299. return H_BAD_MODE;
  300. if (WARN_ON_ONCE(vcpu->arch.shregs.msr & MSR_TS_MASK))
  301. return H_BAD_MODE;
  302. }
  303. /* translate lpid */
  304. l2 = kvmhv_get_nested(vcpu->kvm, l2_hv.lpid, true);
  305. if (!l2)
  306. return H_PARAMETER;
  307. if (!l2->l1_gr_to_hr) {
  308. mutex_lock(&l2->tlb_lock);
  309. kvmhv_update_ptbl_cache(l2);
  310. mutex_unlock(&l2->tlb_lock);
  311. }
  312. /* save l1 values of things */
  313. vcpu->arch.regs.msr = vcpu->arch.shregs.msr;
  314. saved_l1_regs = vcpu->arch.regs;
  315. kvmhv_save_hv_regs(vcpu, &saved_l1_hv);
  316. /* convert TB values/offsets to host (L0) values */
  317. hdec_exp = l2_hv.hdec_expiry - vc->tb_offset;
  318. vc->tb_offset += l2_hv.tb_offset;
  319. vcpu->arch.dec_expires += l2_hv.tb_offset;
  320. /* set L1 state to L2 state */
  321. vcpu->arch.nested = l2;
  322. vcpu->arch.nested_vcpu_id = l2_hv.vcpu_token;
  323. vcpu->arch.nested_hfscr = l2_hv.hfscr;
  324. vcpu->arch.regs = l2_regs;
  325. /* Guest must always run with ME enabled, HV disabled. */
  326. vcpu->arch.shregs.msr = (vcpu->arch.regs.msr | MSR_ME) & ~MSR_HV;
  327. lpcr = l2_hv.lpcr;
  328. load_l2_hv_regs(vcpu, &l2_hv, &saved_l1_hv, &lpcr);
  329. vcpu->arch.ret = RESUME_GUEST;
  330. vcpu->arch.trap = 0;
  331. do {
  332. r = kvmhv_run_single_vcpu(vcpu, hdec_exp, lpcr);
  333. } while (is_kvmppc_resume_guest(r));
  334. /* save L2 state for return */
  335. l2_regs = vcpu->arch.regs;
  336. l2_regs.msr = vcpu->arch.shregs.msr;
  337. delta_purr = vcpu->arch.purr - l2_hv.purr;
  338. delta_spurr = vcpu->arch.spurr - l2_hv.spurr;
  339. delta_ic = vcpu->arch.ic - l2_hv.ic;
  340. delta_vtb = vc->vtb - l2_hv.vtb;
  341. save_hv_return_state(vcpu, &l2_hv);
  342. /* restore L1 state */
  343. vcpu->arch.nested = NULL;
  344. vcpu->arch.regs = saved_l1_regs;
  345. vcpu->arch.shregs.msr = saved_l1_regs.msr & ~MSR_TS_MASK;
  346. /* set L1 MSR TS field according to L2 transaction state */
  347. if (l2_regs.msr & MSR_TS_MASK)
  348. vcpu->arch.shregs.msr |= MSR_TS_S;
  349. vc->tb_offset = saved_l1_hv.tb_offset;
  350. /* XXX: is this always the same delta as saved_l1_hv.tb_offset? */
  351. vcpu->arch.dec_expires -= l2_hv.tb_offset;
  352. restore_hv_regs(vcpu, &saved_l1_hv);
  353. vcpu->arch.purr += delta_purr;
  354. vcpu->arch.spurr += delta_spurr;
  355. vcpu->arch.ic += delta_ic;
  356. vc->vtb += delta_vtb;
  357. kvmhv_put_nested(l2);
  358. /* copy l2_hv_state and regs back to guest */
  359. if (kvmppc_need_byteswap(vcpu)) {
  360. byteswap_hv_regs(&l2_hv);
  361. byteswap_pt_regs(&l2_regs);
  362. }
  363. kvm_vcpu_srcu_read_lock(vcpu);
  364. err = kvmhv_write_guest_state_and_regs(vcpu, &l2_hv, &l2_regs,
  365. hv_ptr, regs_ptr);
  366. kvm_vcpu_srcu_read_unlock(vcpu);
  367. if (err)
  368. return H_AUTHORITY;
  369. if (r == -EINTR)
  370. return H_INTERRUPT;
  371. if (vcpu->mmio_needed) {
  372. kvmhv_nested_mmio_needed(vcpu, regs_ptr);
  373. return H_TOO_HARD;
  374. }
  375. return vcpu->arch.trap;
  376. }
  377. long kvmhv_nested_init(void)
  378. {
  379. long int ptb_order;
  380. unsigned long ptcr;
  381. long rc;
  382. if (!kvmhv_on_pseries())
  383. return 0;
  384. if (!radix_enabled())
  385. return -ENODEV;
  386. /* Partition table entry is 1<<4 bytes in size, hence the 4. */
  387. ptb_order = KVM_MAX_NESTED_GUESTS_SHIFT + 4;
  388. /* Minimum partition table size is 1<<12 bytes */
  389. if (ptb_order < 12)
  390. ptb_order = 12;
  391. pseries_partition_tb = kmalloc(sizeof(struct patb_entry) << ptb_order,
  392. GFP_KERNEL);
  393. if (!pseries_partition_tb) {
  394. pr_err("kvm-hv: failed to allocated nested partition table\n");
  395. return -ENOMEM;
  396. }
  397. ptcr = __pa(pseries_partition_tb) | (ptb_order - 12);
  398. rc = plpar_hcall_norets(H_SET_PARTITION_TABLE, ptcr);
  399. if (rc != H_SUCCESS) {
  400. pr_err("kvm-hv: Parent hypervisor does not support nesting (rc=%ld)\n",
  401. rc);
  402. kfree(pseries_partition_tb);
  403. pseries_partition_tb = NULL;
  404. return -ENODEV;
  405. }
  406. return 0;
  407. }
  408. void kvmhv_nested_exit(void)
  409. {
  410. /*
  411. * N.B. the kvmhv_on_pseries() test is there because it enables
  412. * the compiler to remove the call to plpar_hcall_norets()
  413. * when CONFIG_PPC_PSERIES=n.
  414. */
  415. if (kvmhv_on_pseries() && pseries_partition_tb) {
  416. plpar_hcall_norets(H_SET_PARTITION_TABLE, 0);
  417. kfree(pseries_partition_tb);
  418. pseries_partition_tb = NULL;
  419. }
  420. }
  421. static void kvmhv_flush_lpid(unsigned int lpid)
  422. {
  423. long rc;
  424. if (!kvmhv_on_pseries()) {
  425. radix__flush_all_lpid(lpid);
  426. return;
  427. }
  428. if (!firmware_has_feature(FW_FEATURE_RPT_INVALIDATE))
  429. rc = plpar_hcall_norets(H_TLB_INVALIDATE, H_TLBIE_P1_ENC(2, 0, 1),
  430. lpid, TLBIEL_INVAL_SET_LPID);
  431. else
  432. rc = pseries_rpt_invalidate(lpid, H_RPTI_TARGET_CMMU,
  433. H_RPTI_TYPE_NESTED |
  434. H_RPTI_TYPE_TLB | H_RPTI_TYPE_PWC |
  435. H_RPTI_TYPE_PAT,
  436. H_RPTI_PAGE_ALL, 0, -1UL);
  437. if (rc)
  438. pr_err("KVM: TLB LPID invalidation hcall failed, rc=%ld\n", rc);
  439. }
  440. void kvmhv_set_ptbl_entry(unsigned int lpid, u64 dw0, u64 dw1)
  441. {
  442. if (!kvmhv_on_pseries()) {
  443. mmu_partition_table_set_entry(lpid, dw0, dw1, true);
  444. return;
  445. }
  446. pseries_partition_tb[lpid].patb0 = cpu_to_be64(dw0);
  447. pseries_partition_tb[lpid].patb1 = cpu_to_be64(dw1);
  448. /* L0 will do the necessary barriers */
  449. kvmhv_flush_lpid(lpid);
  450. }
  451. static void kvmhv_set_nested_ptbl(struct kvm_nested_guest *gp)
  452. {
  453. unsigned long dw0;
  454. dw0 = PATB_HR | radix__get_tree_size() |
  455. __pa(gp->shadow_pgtable) | RADIX_PGD_INDEX_SIZE;
  456. kvmhv_set_ptbl_entry(gp->shadow_lpid, dw0, gp->process_table);
  457. }
  458. /*
  459. * Handle the H_SET_PARTITION_TABLE hcall.
  460. * r4 = guest real address of partition table + log_2(size) - 12
  461. * (formatted as for the PTCR).
  462. */
  463. long kvmhv_set_partition_table(struct kvm_vcpu *vcpu)
  464. {
  465. struct kvm *kvm = vcpu->kvm;
  466. unsigned long ptcr = kvmppc_get_gpr(vcpu, 4);
  467. int srcu_idx;
  468. long ret = H_SUCCESS;
  469. srcu_idx = srcu_read_lock(&kvm->srcu);
  470. /* Check partition size and base address. */
  471. if ((ptcr & PRTS_MASK) + 12 - 4 > KVM_MAX_NESTED_GUESTS_SHIFT ||
  472. !kvm_is_visible_gfn(vcpu->kvm, (ptcr & PRTB_MASK) >> PAGE_SHIFT))
  473. ret = H_PARAMETER;
  474. srcu_read_unlock(&kvm->srcu, srcu_idx);
  475. if (ret == H_SUCCESS)
  476. kvm->arch.l1_ptcr = ptcr;
  477. return ret;
  478. }
  479. /*
  480. * Handle the H_COPY_TOFROM_GUEST hcall.
  481. * r4 = L1 lpid of nested guest
  482. * r5 = pid
  483. * r6 = eaddr to access
  484. * r7 = to buffer (L1 gpa)
  485. * r8 = from buffer (L1 gpa)
  486. * r9 = n bytes to copy
  487. */
  488. long kvmhv_copy_tofrom_guest_nested(struct kvm_vcpu *vcpu)
  489. {
  490. struct kvm_nested_guest *gp;
  491. int l1_lpid = kvmppc_get_gpr(vcpu, 4);
  492. int pid = kvmppc_get_gpr(vcpu, 5);
  493. gva_t eaddr = kvmppc_get_gpr(vcpu, 6);
  494. gpa_t gp_to = (gpa_t) kvmppc_get_gpr(vcpu, 7);
  495. gpa_t gp_from = (gpa_t) kvmppc_get_gpr(vcpu, 8);
  496. void *buf;
  497. unsigned long n = kvmppc_get_gpr(vcpu, 9);
  498. bool is_load = !!gp_to;
  499. long rc;
  500. if (gp_to && gp_from) /* One must be NULL to determine the direction */
  501. return H_PARAMETER;
  502. if (eaddr & (0xFFFUL << 52))
  503. return H_PARAMETER;
  504. buf = kzalloc(n, GFP_KERNEL | __GFP_NOWARN);
  505. if (!buf)
  506. return H_NO_MEM;
  507. gp = kvmhv_get_nested(vcpu->kvm, l1_lpid, false);
  508. if (!gp) {
  509. rc = H_PARAMETER;
  510. goto out_free;
  511. }
  512. mutex_lock(&gp->tlb_lock);
  513. if (is_load) {
  514. /* Load from the nested guest into our buffer */
  515. rc = __kvmhv_copy_tofrom_guest_radix(gp->shadow_lpid, pid,
  516. eaddr, buf, NULL, n);
  517. if (rc)
  518. goto not_found;
  519. /* Write what was loaded into our buffer back to the L1 guest */
  520. kvm_vcpu_srcu_read_lock(vcpu);
  521. rc = kvm_vcpu_write_guest(vcpu, gp_to, buf, n);
  522. kvm_vcpu_srcu_read_unlock(vcpu);
  523. if (rc)
  524. goto not_found;
  525. } else {
  526. /* Load the data to be stored from the L1 guest into our buf */
  527. kvm_vcpu_srcu_read_lock(vcpu);
  528. rc = kvm_vcpu_read_guest(vcpu, gp_from, buf, n);
  529. kvm_vcpu_srcu_read_unlock(vcpu);
  530. if (rc)
  531. goto not_found;
  532. /* Store from our buffer into the nested guest */
  533. rc = __kvmhv_copy_tofrom_guest_radix(gp->shadow_lpid, pid,
  534. eaddr, NULL, buf, n);
  535. if (rc)
  536. goto not_found;
  537. }
  538. out_unlock:
  539. mutex_unlock(&gp->tlb_lock);
  540. kvmhv_put_nested(gp);
  541. out_free:
  542. kfree(buf);
  543. return rc;
  544. not_found:
  545. rc = H_NOT_FOUND;
  546. goto out_unlock;
  547. }
  548. /*
  549. * Reload the partition table entry for a guest.
  550. * Caller must hold gp->tlb_lock.
  551. */
  552. static void kvmhv_update_ptbl_cache(struct kvm_nested_guest *gp)
  553. {
  554. int ret;
  555. struct patb_entry ptbl_entry;
  556. unsigned long ptbl_addr;
  557. struct kvm *kvm = gp->l1_host;
  558. ret = -EFAULT;
  559. ptbl_addr = (kvm->arch.l1_ptcr & PRTB_MASK) + (gp->l1_lpid << 4);
  560. if (gp->l1_lpid < (1ul << ((kvm->arch.l1_ptcr & PRTS_MASK) + 12 - 4))) {
  561. int srcu_idx = srcu_read_lock(&kvm->srcu);
  562. ret = kvm_read_guest(kvm, ptbl_addr,
  563. &ptbl_entry, sizeof(ptbl_entry));
  564. srcu_read_unlock(&kvm->srcu, srcu_idx);
  565. }
  566. if (ret) {
  567. gp->l1_gr_to_hr = 0;
  568. gp->process_table = 0;
  569. } else {
  570. gp->l1_gr_to_hr = be64_to_cpu(ptbl_entry.patb0);
  571. gp->process_table = be64_to_cpu(ptbl_entry.patb1);
  572. }
  573. kvmhv_set_nested_ptbl(gp);
  574. }
  575. void kvmhv_vm_nested_init(struct kvm *kvm)
  576. {
  577. idr_init(&kvm->arch.kvm_nested_guest_idr);
  578. }
  579. static struct kvm_nested_guest *__find_nested(struct kvm *kvm, int lpid)
  580. {
  581. return idr_find(&kvm->arch.kvm_nested_guest_idr, lpid);
  582. }
  583. static bool __prealloc_nested(struct kvm *kvm, int lpid)
  584. {
  585. if (idr_alloc(&kvm->arch.kvm_nested_guest_idr,
  586. NULL, lpid, lpid + 1, GFP_KERNEL) != lpid)
  587. return false;
  588. return true;
  589. }
  590. static void __add_nested(struct kvm *kvm, int lpid, struct kvm_nested_guest *gp)
  591. {
  592. if (idr_replace(&kvm->arch.kvm_nested_guest_idr, gp, lpid))
  593. WARN_ON(1);
  594. }
  595. static void __remove_nested(struct kvm *kvm, int lpid)
  596. {
  597. idr_remove(&kvm->arch.kvm_nested_guest_idr, lpid);
  598. }
  599. static struct kvm_nested_guest *kvmhv_alloc_nested(struct kvm *kvm, unsigned int lpid)
  600. {
  601. struct kvm_nested_guest *gp;
  602. long shadow_lpid;
  603. gp = kzalloc(sizeof(*gp), GFP_KERNEL);
  604. if (!gp)
  605. return NULL;
  606. gp->l1_host = kvm;
  607. gp->l1_lpid = lpid;
  608. mutex_init(&gp->tlb_lock);
  609. gp->shadow_pgtable = pgd_alloc(kvm->mm);
  610. if (!gp->shadow_pgtable)
  611. goto out_free;
  612. shadow_lpid = kvmppc_alloc_lpid();
  613. if (shadow_lpid < 0)
  614. goto out_free2;
  615. gp->shadow_lpid = shadow_lpid;
  616. gp->radix = 1;
  617. memset(gp->prev_cpu, -1, sizeof(gp->prev_cpu));
  618. return gp;
  619. out_free2:
  620. pgd_free(kvm->mm, gp->shadow_pgtable);
  621. out_free:
  622. kfree(gp);
  623. return NULL;
  624. }
  625. /*
  626. * Free up any resources allocated for a nested guest.
  627. */
  628. static void kvmhv_release_nested(struct kvm_nested_guest *gp)
  629. {
  630. struct kvm *kvm = gp->l1_host;
  631. if (gp->shadow_pgtable) {
  632. /*
  633. * No vcpu is using this struct and no call to
  634. * kvmhv_get_nested can find this struct,
  635. * so we don't need to hold kvm->mmu_lock.
  636. */
  637. kvmppc_free_pgtable_radix(kvm, gp->shadow_pgtable,
  638. gp->shadow_lpid);
  639. pgd_free(kvm->mm, gp->shadow_pgtable);
  640. }
  641. kvmhv_set_ptbl_entry(gp->shadow_lpid, 0, 0);
  642. kvmppc_free_lpid(gp->shadow_lpid);
  643. kfree(gp);
  644. }
  645. static void kvmhv_remove_nested(struct kvm_nested_guest *gp)
  646. {
  647. struct kvm *kvm = gp->l1_host;
  648. int lpid = gp->l1_lpid;
  649. long ref;
  650. spin_lock(&kvm->mmu_lock);
  651. if (gp == __find_nested(kvm, lpid)) {
  652. __remove_nested(kvm, lpid);
  653. --gp->refcnt;
  654. }
  655. ref = gp->refcnt;
  656. spin_unlock(&kvm->mmu_lock);
  657. if (ref == 0)
  658. kvmhv_release_nested(gp);
  659. }
  660. /*
  661. * Free up all nested resources allocated for this guest.
  662. * This is called with no vcpus of the guest running, when
  663. * switching the guest to HPT mode or when destroying the
  664. * guest.
  665. */
  666. void kvmhv_release_all_nested(struct kvm *kvm)
  667. {
  668. int lpid;
  669. struct kvm_nested_guest *gp;
  670. struct kvm_nested_guest *freelist = NULL;
  671. struct kvm_memory_slot *memslot;
  672. int srcu_idx, bkt;
  673. spin_lock(&kvm->mmu_lock);
  674. idr_for_each_entry(&kvm->arch.kvm_nested_guest_idr, gp, lpid) {
  675. __remove_nested(kvm, lpid);
  676. if (--gp->refcnt == 0) {
  677. gp->next = freelist;
  678. freelist = gp;
  679. }
  680. }
  681. idr_destroy(&kvm->arch.kvm_nested_guest_idr);
  682. /* idr is empty and may be reused at this point */
  683. spin_unlock(&kvm->mmu_lock);
  684. while ((gp = freelist) != NULL) {
  685. freelist = gp->next;
  686. kvmhv_release_nested(gp);
  687. }
  688. srcu_idx = srcu_read_lock(&kvm->srcu);
  689. kvm_for_each_memslot(memslot, bkt, kvm_memslots(kvm))
  690. kvmhv_free_memslot_nest_rmap(memslot);
  691. srcu_read_unlock(&kvm->srcu, srcu_idx);
  692. }
  693. /* caller must hold gp->tlb_lock */
  694. static void kvmhv_flush_nested(struct kvm_nested_guest *gp)
  695. {
  696. struct kvm *kvm = gp->l1_host;
  697. spin_lock(&kvm->mmu_lock);
  698. kvmppc_free_pgtable_radix(kvm, gp->shadow_pgtable, gp->shadow_lpid);
  699. spin_unlock(&kvm->mmu_lock);
  700. kvmhv_flush_lpid(gp->shadow_lpid);
  701. kvmhv_update_ptbl_cache(gp);
  702. if (gp->l1_gr_to_hr == 0)
  703. kvmhv_remove_nested(gp);
  704. }
  705. struct kvm_nested_guest *kvmhv_get_nested(struct kvm *kvm, int l1_lpid,
  706. bool create)
  707. {
  708. struct kvm_nested_guest *gp, *newgp;
  709. if (l1_lpid >= (1ul << ((kvm->arch.l1_ptcr & PRTS_MASK) + 12 - 4)))
  710. return NULL;
  711. spin_lock(&kvm->mmu_lock);
  712. gp = __find_nested(kvm, l1_lpid);
  713. if (gp)
  714. ++gp->refcnt;
  715. spin_unlock(&kvm->mmu_lock);
  716. if (gp || !create)
  717. return gp;
  718. newgp = kvmhv_alloc_nested(kvm, l1_lpid);
  719. if (!newgp)
  720. return NULL;
  721. if (!__prealloc_nested(kvm, l1_lpid)) {
  722. kvmhv_release_nested(newgp);
  723. return NULL;
  724. }
  725. spin_lock(&kvm->mmu_lock);
  726. gp = __find_nested(kvm, l1_lpid);
  727. if (!gp) {
  728. __add_nested(kvm, l1_lpid, newgp);
  729. ++newgp->refcnt;
  730. gp = newgp;
  731. newgp = NULL;
  732. }
  733. ++gp->refcnt;
  734. spin_unlock(&kvm->mmu_lock);
  735. if (newgp)
  736. kvmhv_release_nested(newgp);
  737. return gp;
  738. }
  739. void kvmhv_put_nested(struct kvm_nested_guest *gp)
  740. {
  741. struct kvm *kvm = gp->l1_host;
  742. long ref;
  743. spin_lock(&kvm->mmu_lock);
  744. ref = --gp->refcnt;
  745. spin_unlock(&kvm->mmu_lock);
  746. if (ref == 0)
  747. kvmhv_release_nested(gp);
  748. }
  749. pte_t *find_kvm_nested_guest_pte(struct kvm *kvm, unsigned long lpid,
  750. unsigned long ea, unsigned *hshift)
  751. {
  752. struct kvm_nested_guest *gp;
  753. pte_t *pte;
  754. gp = __find_nested(kvm, lpid);
  755. if (!gp)
  756. return NULL;
  757. VM_WARN(!spin_is_locked(&kvm->mmu_lock),
  758. "%s called with kvm mmu_lock not held \n", __func__);
  759. pte = __find_linux_pte(gp->shadow_pgtable, ea, NULL, hshift);
  760. return pte;
  761. }
  762. static inline bool kvmhv_n_rmap_is_equal(u64 rmap_1, u64 rmap_2)
  763. {
  764. return !((rmap_1 ^ rmap_2) & (RMAP_NESTED_LPID_MASK |
  765. RMAP_NESTED_GPA_MASK));
  766. }
  767. void kvmhv_insert_nest_rmap(struct kvm *kvm, unsigned long *rmapp,
  768. struct rmap_nested **n_rmap)
  769. {
  770. struct llist_node *entry = ((struct llist_head *) rmapp)->first;
  771. struct rmap_nested *cursor;
  772. u64 rmap, new_rmap = (*n_rmap)->rmap;
  773. /* Are there any existing entries? */
  774. if (!(*rmapp)) {
  775. /* No -> use the rmap as a single entry */
  776. *rmapp = new_rmap | RMAP_NESTED_IS_SINGLE_ENTRY;
  777. return;
  778. }
  779. /* Do any entries match what we're trying to insert? */
  780. for_each_nest_rmap_safe(cursor, entry, &rmap) {
  781. if (kvmhv_n_rmap_is_equal(rmap, new_rmap))
  782. return;
  783. }
  784. /* Do we need to create a list or just add the new entry? */
  785. rmap = *rmapp;
  786. if (rmap & RMAP_NESTED_IS_SINGLE_ENTRY) /* Not previously a list */
  787. *rmapp = 0UL;
  788. llist_add(&((*n_rmap)->list), (struct llist_head *) rmapp);
  789. if (rmap & RMAP_NESTED_IS_SINGLE_ENTRY) /* Not previously a list */
  790. (*n_rmap)->list.next = (struct llist_node *) rmap;
  791. /* Set NULL so not freed by caller */
  792. *n_rmap = NULL;
  793. }
  794. static void kvmhv_update_nest_rmap_rc(struct kvm *kvm, u64 n_rmap,
  795. unsigned long clr, unsigned long set,
  796. unsigned long hpa, unsigned long mask)
  797. {
  798. unsigned long gpa;
  799. unsigned int shift, lpid;
  800. pte_t *ptep;
  801. gpa = n_rmap & RMAP_NESTED_GPA_MASK;
  802. lpid = (n_rmap & RMAP_NESTED_LPID_MASK) >> RMAP_NESTED_LPID_SHIFT;
  803. /* Find the pte */
  804. ptep = find_kvm_nested_guest_pte(kvm, lpid, gpa, &shift);
  805. /*
  806. * If the pte is present and the pfn is still the same, update the pte.
  807. * If the pfn has changed then this is a stale rmap entry, the nested
  808. * gpa actually points somewhere else now, and there is nothing to do.
  809. * XXX A future optimisation would be to remove the rmap entry here.
  810. */
  811. if (ptep && pte_present(*ptep) && ((pte_val(*ptep) & mask) == hpa)) {
  812. __radix_pte_update(ptep, clr, set);
  813. kvmppc_radix_tlbie_page(kvm, gpa, shift, lpid);
  814. }
  815. }
  816. /*
  817. * For a given list of rmap entries, update the rc bits in all ptes in shadow
  818. * page tables for nested guests which are referenced by the rmap list.
  819. */
  820. void kvmhv_update_nest_rmap_rc_list(struct kvm *kvm, unsigned long *rmapp,
  821. unsigned long clr, unsigned long set,
  822. unsigned long hpa, unsigned long nbytes)
  823. {
  824. struct llist_node *entry = ((struct llist_head *) rmapp)->first;
  825. struct rmap_nested *cursor;
  826. unsigned long rmap, mask;
  827. if ((clr | set) & ~(_PAGE_DIRTY | _PAGE_ACCESSED))
  828. return;
  829. mask = PTE_RPN_MASK & ~(nbytes - 1);
  830. hpa &= mask;
  831. for_each_nest_rmap_safe(cursor, entry, &rmap)
  832. kvmhv_update_nest_rmap_rc(kvm, rmap, clr, set, hpa, mask);
  833. }
  834. static void kvmhv_remove_nest_rmap(struct kvm *kvm, u64 n_rmap,
  835. unsigned long hpa, unsigned long mask)
  836. {
  837. struct kvm_nested_guest *gp;
  838. unsigned long gpa;
  839. unsigned int shift, lpid;
  840. pte_t *ptep;
  841. gpa = n_rmap & RMAP_NESTED_GPA_MASK;
  842. lpid = (n_rmap & RMAP_NESTED_LPID_MASK) >> RMAP_NESTED_LPID_SHIFT;
  843. gp = __find_nested(kvm, lpid);
  844. if (!gp)
  845. return;
  846. /* Find and invalidate the pte */
  847. ptep = find_kvm_nested_guest_pte(kvm, lpid, gpa, &shift);
  848. /* Don't spuriously invalidate ptes if the pfn has changed */
  849. if (ptep && pte_present(*ptep) && ((pte_val(*ptep) & mask) == hpa))
  850. kvmppc_unmap_pte(kvm, ptep, gpa, shift, NULL, gp->shadow_lpid);
  851. }
  852. static void kvmhv_remove_nest_rmap_list(struct kvm *kvm, unsigned long *rmapp,
  853. unsigned long hpa, unsigned long mask)
  854. {
  855. struct llist_node *entry = llist_del_all((struct llist_head *) rmapp);
  856. struct rmap_nested *cursor;
  857. unsigned long rmap;
  858. for_each_nest_rmap_safe(cursor, entry, &rmap) {
  859. kvmhv_remove_nest_rmap(kvm, rmap, hpa, mask);
  860. kfree(cursor);
  861. }
  862. }
  863. /* called with kvm->mmu_lock held */
  864. void kvmhv_remove_nest_rmap_range(struct kvm *kvm,
  865. const struct kvm_memory_slot *memslot,
  866. unsigned long gpa, unsigned long hpa,
  867. unsigned long nbytes)
  868. {
  869. unsigned long gfn, end_gfn;
  870. unsigned long addr_mask;
  871. if (!memslot)
  872. return;
  873. gfn = (gpa >> PAGE_SHIFT) - memslot->base_gfn;
  874. end_gfn = gfn + (nbytes >> PAGE_SHIFT);
  875. addr_mask = PTE_RPN_MASK & ~(nbytes - 1);
  876. hpa &= addr_mask;
  877. for (; gfn < end_gfn; gfn++) {
  878. unsigned long *rmap = &memslot->arch.rmap[gfn];
  879. kvmhv_remove_nest_rmap_list(kvm, rmap, hpa, addr_mask);
  880. }
  881. }
  882. static void kvmhv_free_memslot_nest_rmap(struct kvm_memory_slot *free)
  883. {
  884. unsigned long page;
  885. for (page = 0; page < free->npages; page++) {
  886. unsigned long rmap, *rmapp = &free->arch.rmap[page];
  887. struct rmap_nested *cursor;
  888. struct llist_node *entry;
  889. entry = llist_del_all((struct llist_head *) rmapp);
  890. for_each_nest_rmap_safe(cursor, entry, &rmap)
  891. kfree(cursor);
  892. }
  893. }
  894. static bool kvmhv_invalidate_shadow_pte(struct kvm_vcpu *vcpu,
  895. struct kvm_nested_guest *gp,
  896. long gpa, int *shift_ret)
  897. {
  898. struct kvm *kvm = vcpu->kvm;
  899. bool ret = false;
  900. pte_t *ptep;
  901. int shift;
  902. spin_lock(&kvm->mmu_lock);
  903. ptep = find_kvm_nested_guest_pte(kvm, gp->l1_lpid, gpa, &shift);
  904. if (!shift)
  905. shift = PAGE_SHIFT;
  906. if (ptep && pte_present(*ptep)) {
  907. kvmppc_unmap_pte(kvm, ptep, gpa, shift, NULL, gp->shadow_lpid);
  908. ret = true;
  909. }
  910. spin_unlock(&kvm->mmu_lock);
  911. if (shift_ret)
  912. *shift_ret = shift;
  913. return ret;
  914. }
  915. static inline int get_ric(unsigned int instr)
  916. {
  917. return (instr >> 18) & 0x3;
  918. }
  919. static inline int get_prs(unsigned int instr)
  920. {
  921. return (instr >> 17) & 0x1;
  922. }
  923. static inline int get_r(unsigned int instr)
  924. {
  925. return (instr >> 16) & 0x1;
  926. }
  927. static inline int get_lpid(unsigned long r_val)
  928. {
  929. return r_val & 0xffffffff;
  930. }
  931. static inline int get_is(unsigned long r_val)
  932. {
  933. return (r_val >> 10) & 0x3;
  934. }
  935. static inline int get_ap(unsigned long r_val)
  936. {
  937. return (r_val >> 5) & 0x7;
  938. }
  939. static inline long get_epn(unsigned long r_val)
  940. {
  941. return r_val >> 12;
  942. }
  943. static int kvmhv_emulate_tlbie_tlb_addr(struct kvm_vcpu *vcpu, int lpid,
  944. int ap, long epn)
  945. {
  946. struct kvm *kvm = vcpu->kvm;
  947. struct kvm_nested_guest *gp;
  948. long npages;
  949. int shift, shadow_shift;
  950. unsigned long addr;
  951. shift = ap_to_shift(ap);
  952. addr = epn << 12;
  953. if (shift < 0)
  954. /* Invalid ap encoding */
  955. return -EINVAL;
  956. addr &= ~((1UL << shift) - 1);
  957. npages = 1UL << (shift - PAGE_SHIFT);
  958. gp = kvmhv_get_nested(kvm, lpid, false);
  959. if (!gp) /* No such guest -> nothing to do */
  960. return 0;
  961. mutex_lock(&gp->tlb_lock);
  962. /* There may be more than one host page backing this single guest pte */
  963. do {
  964. kvmhv_invalidate_shadow_pte(vcpu, gp, addr, &shadow_shift);
  965. npages -= 1UL << (shadow_shift - PAGE_SHIFT);
  966. addr += 1UL << shadow_shift;
  967. } while (npages > 0);
  968. mutex_unlock(&gp->tlb_lock);
  969. kvmhv_put_nested(gp);
  970. return 0;
  971. }
  972. static void kvmhv_emulate_tlbie_lpid(struct kvm_vcpu *vcpu,
  973. struct kvm_nested_guest *gp, int ric)
  974. {
  975. struct kvm *kvm = vcpu->kvm;
  976. mutex_lock(&gp->tlb_lock);
  977. switch (ric) {
  978. case 0:
  979. /* Invalidate TLB */
  980. spin_lock(&kvm->mmu_lock);
  981. kvmppc_free_pgtable_radix(kvm, gp->shadow_pgtable,
  982. gp->shadow_lpid);
  983. kvmhv_flush_lpid(gp->shadow_lpid);
  984. spin_unlock(&kvm->mmu_lock);
  985. break;
  986. case 1:
  987. /*
  988. * Invalidate PWC
  989. * We don't cache this -> nothing to do
  990. */
  991. break;
  992. case 2:
  993. /* Invalidate TLB, PWC and caching of partition table entries */
  994. kvmhv_flush_nested(gp);
  995. break;
  996. default:
  997. break;
  998. }
  999. mutex_unlock(&gp->tlb_lock);
  1000. }
  1001. static void kvmhv_emulate_tlbie_all_lpid(struct kvm_vcpu *vcpu, int ric)
  1002. {
  1003. struct kvm *kvm = vcpu->kvm;
  1004. struct kvm_nested_guest *gp;
  1005. int lpid;
  1006. spin_lock(&kvm->mmu_lock);
  1007. idr_for_each_entry(&kvm->arch.kvm_nested_guest_idr, gp, lpid) {
  1008. spin_unlock(&kvm->mmu_lock);
  1009. kvmhv_emulate_tlbie_lpid(vcpu, gp, ric);
  1010. spin_lock(&kvm->mmu_lock);
  1011. }
  1012. spin_unlock(&kvm->mmu_lock);
  1013. }
  1014. static int kvmhv_emulate_priv_tlbie(struct kvm_vcpu *vcpu, unsigned int instr,
  1015. unsigned long rsval, unsigned long rbval)
  1016. {
  1017. struct kvm *kvm = vcpu->kvm;
  1018. struct kvm_nested_guest *gp;
  1019. int r, ric, prs, is, ap;
  1020. int lpid;
  1021. long epn;
  1022. int ret = 0;
  1023. ric = get_ric(instr);
  1024. prs = get_prs(instr);
  1025. r = get_r(instr);
  1026. lpid = get_lpid(rsval);
  1027. is = get_is(rbval);
  1028. /*
  1029. * These cases are invalid and are not handled:
  1030. * r != 1 -> Only radix supported
  1031. * prs == 1 -> Not HV privileged
  1032. * ric == 3 -> No cluster bombs for radix
  1033. * is == 1 -> Partition scoped translations not associated with pid
  1034. * (!is) && (ric == 1 || ric == 2) -> Not supported by ISA
  1035. */
  1036. if ((!r) || (prs) || (ric == 3) || (is == 1) ||
  1037. ((!is) && (ric == 1 || ric == 2)))
  1038. return -EINVAL;
  1039. switch (is) {
  1040. case 0:
  1041. /*
  1042. * We know ric == 0
  1043. * Invalidate TLB for a given target address
  1044. */
  1045. epn = get_epn(rbval);
  1046. ap = get_ap(rbval);
  1047. ret = kvmhv_emulate_tlbie_tlb_addr(vcpu, lpid, ap, epn);
  1048. break;
  1049. case 2:
  1050. /* Invalidate matching LPID */
  1051. gp = kvmhv_get_nested(kvm, lpid, false);
  1052. if (gp) {
  1053. kvmhv_emulate_tlbie_lpid(vcpu, gp, ric);
  1054. kvmhv_put_nested(gp);
  1055. }
  1056. break;
  1057. case 3:
  1058. /* Invalidate ALL LPIDs */
  1059. kvmhv_emulate_tlbie_all_lpid(vcpu, ric);
  1060. break;
  1061. default:
  1062. ret = -EINVAL;
  1063. break;
  1064. }
  1065. return ret;
  1066. }
  1067. /*
  1068. * This handles the H_TLB_INVALIDATE hcall.
  1069. * Parameters are (r4) tlbie instruction code, (r5) rS contents,
  1070. * (r6) rB contents.
  1071. */
  1072. long kvmhv_do_nested_tlbie(struct kvm_vcpu *vcpu)
  1073. {
  1074. int ret;
  1075. ret = kvmhv_emulate_priv_tlbie(vcpu, kvmppc_get_gpr(vcpu, 4),
  1076. kvmppc_get_gpr(vcpu, 5), kvmppc_get_gpr(vcpu, 6));
  1077. if (ret)
  1078. return H_PARAMETER;
  1079. return H_SUCCESS;
  1080. }
  1081. static long do_tlb_invalidate_nested_all(struct kvm_vcpu *vcpu,
  1082. unsigned long lpid, unsigned long ric)
  1083. {
  1084. struct kvm *kvm = vcpu->kvm;
  1085. struct kvm_nested_guest *gp;
  1086. gp = kvmhv_get_nested(kvm, lpid, false);
  1087. if (gp) {
  1088. kvmhv_emulate_tlbie_lpid(vcpu, gp, ric);
  1089. kvmhv_put_nested(gp);
  1090. }
  1091. return H_SUCCESS;
  1092. }
  1093. /*
  1094. * Number of pages above which we invalidate the entire LPID rather than
  1095. * flush individual pages.
  1096. */
  1097. static unsigned long tlb_range_flush_page_ceiling __read_mostly = 33;
  1098. static long do_tlb_invalidate_nested_tlb(struct kvm_vcpu *vcpu,
  1099. unsigned long lpid,
  1100. unsigned long pg_sizes,
  1101. unsigned long start,
  1102. unsigned long end)
  1103. {
  1104. int ret = H_P4;
  1105. unsigned long addr, nr_pages;
  1106. struct mmu_psize_def *def;
  1107. unsigned long psize, ap, page_size;
  1108. bool flush_lpid;
  1109. for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
  1110. def = &mmu_psize_defs[psize];
  1111. if (!(pg_sizes & def->h_rpt_pgsize))
  1112. continue;
  1113. nr_pages = (end - start) >> def->shift;
  1114. flush_lpid = nr_pages > tlb_range_flush_page_ceiling;
  1115. if (flush_lpid)
  1116. return do_tlb_invalidate_nested_all(vcpu, lpid,
  1117. RIC_FLUSH_TLB);
  1118. addr = start;
  1119. ap = mmu_get_ap(psize);
  1120. page_size = 1UL << def->shift;
  1121. do {
  1122. ret = kvmhv_emulate_tlbie_tlb_addr(vcpu, lpid, ap,
  1123. get_epn(addr));
  1124. if (ret)
  1125. return H_P4;
  1126. addr += page_size;
  1127. } while (addr < end);
  1128. }
  1129. return ret;
  1130. }
  1131. /*
  1132. * Performs partition-scoped invalidations for nested guests
  1133. * as part of H_RPT_INVALIDATE hcall.
  1134. */
  1135. long do_h_rpt_invalidate_pat(struct kvm_vcpu *vcpu, unsigned long lpid,
  1136. unsigned long type, unsigned long pg_sizes,
  1137. unsigned long start, unsigned long end)
  1138. {
  1139. /*
  1140. * If L2 lpid isn't valid, we need to return H_PARAMETER.
  1141. *
  1142. * However, nested KVM issues a L2 lpid flush call when creating
  1143. * partition table entries for L2. This happens even before the
  1144. * corresponding shadow lpid is created in HV which happens in
  1145. * H_ENTER_NESTED call. Since we can't differentiate this case from
  1146. * the invalid case, we ignore such flush requests and return success.
  1147. */
  1148. if (!__find_nested(vcpu->kvm, lpid))
  1149. return H_SUCCESS;
  1150. /*
  1151. * A flush all request can be handled by a full lpid flush only.
  1152. */
  1153. if ((type & H_RPTI_TYPE_NESTED_ALL) == H_RPTI_TYPE_NESTED_ALL)
  1154. return do_tlb_invalidate_nested_all(vcpu, lpid, RIC_FLUSH_ALL);
  1155. /*
  1156. * We don't need to handle a PWC flush like process table here,
  1157. * because intermediate partition scoped table in nested guest doesn't
  1158. * really have PWC. Only level we have PWC is in L0 and for nested
  1159. * invalidate at L0 we always do kvm_flush_lpid() which does
  1160. * radix__flush_all_lpid(). For range invalidate at any level, we
  1161. * are not removing the higher level page tables and hence there is
  1162. * no PWC invalidate needed.
  1163. *
  1164. * if (type & H_RPTI_TYPE_PWC) {
  1165. * ret = do_tlb_invalidate_nested_all(vcpu, lpid, RIC_FLUSH_PWC);
  1166. * if (ret)
  1167. * return H_P4;
  1168. * }
  1169. */
  1170. if (start == 0 && end == -1)
  1171. return do_tlb_invalidate_nested_all(vcpu, lpid, RIC_FLUSH_TLB);
  1172. if (type & H_RPTI_TYPE_TLB)
  1173. return do_tlb_invalidate_nested_tlb(vcpu, lpid, pg_sizes,
  1174. start, end);
  1175. return H_SUCCESS;
  1176. }
  1177. /* Used to convert a nested guest real address to a L1 guest real address */
  1178. static int kvmhv_translate_addr_nested(struct kvm_vcpu *vcpu,
  1179. struct kvm_nested_guest *gp,
  1180. unsigned long n_gpa, unsigned long dsisr,
  1181. struct kvmppc_pte *gpte_p)
  1182. {
  1183. u64 fault_addr, flags = dsisr & DSISR_ISSTORE;
  1184. int ret;
  1185. ret = kvmppc_mmu_walk_radix_tree(vcpu, n_gpa, gpte_p, gp->l1_gr_to_hr,
  1186. &fault_addr);
  1187. if (ret) {
  1188. /* We didn't find a pte */
  1189. if (ret == -EINVAL) {
  1190. /* Unsupported mmu config */
  1191. flags |= DSISR_UNSUPP_MMU;
  1192. } else if (ret == -ENOENT) {
  1193. /* No translation found */
  1194. flags |= DSISR_NOHPTE;
  1195. } else if (ret == -EFAULT) {
  1196. /* Couldn't access L1 real address */
  1197. flags |= DSISR_PRTABLE_FAULT;
  1198. vcpu->arch.fault_gpa = fault_addr;
  1199. } else {
  1200. /* Unknown error */
  1201. return ret;
  1202. }
  1203. goto forward_to_l1;
  1204. } else {
  1205. /* We found a pte -> check permissions */
  1206. if (dsisr & DSISR_ISSTORE) {
  1207. /* Can we write? */
  1208. if (!gpte_p->may_write) {
  1209. flags |= DSISR_PROTFAULT;
  1210. goto forward_to_l1;
  1211. }
  1212. } else if (vcpu->arch.trap == BOOK3S_INTERRUPT_H_INST_STORAGE) {
  1213. /* Can we execute? */
  1214. if (!gpte_p->may_execute) {
  1215. flags |= SRR1_ISI_N_G_OR_CIP;
  1216. goto forward_to_l1;
  1217. }
  1218. } else {
  1219. /* Can we read? */
  1220. if (!gpte_p->may_read && !gpte_p->may_write) {
  1221. flags |= DSISR_PROTFAULT;
  1222. goto forward_to_l1;
  1223. }
  1224. }
  1225. }
  1226. return 0;
  1227. forward_to_l1:
  1228. vcpu->arch.fault_dsisr = flags;
  1229. if (vcpu->arch.trap == BOOK3S_INTERRUPT_H_INST_STORAGE) {
  1230. vcpu->arch.shregs.msr &= SRR1_MSR_BITS;
  1231. vcpu->arch.shregs.msr |= flags;
  1232. }
  1233. return RESUME_HOST;
  1234. }
  1235. static long kvmhv_handle_nested_set_rc(struct kvm_vcpu *vcpu,
  1236. struct kvm_nested_guest *gp,
  1237. unsigned long n_gpa,
  1238. struct kvmppc_pte gpte,
  1239. unsigned long dsisr)
  1240. {
  1241. struct kvm *kvm = vcpu->kvm;
  1242. bool writing = !!(dsisr & DSISR_ISSTORE);
  1243. u64 pgflags;
  1244. long ret;
  1245. /* Are the rc bits set in the L1 partition scoped pte? */
  1246. pgflags = _PAGE_ACCESSED;
  1247. if (writing)
  1248. pgflags |= _PAGE_DIRTY;
  1249. if (pgflags & ~gpte.rc)
  1250. return RESUME_HOST;
  1251. spin_lock(&kvm->mmu_lock);
  1252. /* Set the rc bit in the pte of our (L0) pgtable for the L1 guest */
  1253. ret = kvmppc_hv_handle_set_rc(kvm, false, writing,
  1254. gpte.raddr, kvm->arch.lpid);
  1255. if (!ret) {
  1256. ret = -EINVAL;
  1257. goto out_unlock;
  1258. }
  1259. /* Set the rc bit in the pte of the shadow_pgtable for the nest guest */
  1260. ret = kvmppc_hv_handle_set_rc(kvm, true, writing,
  1261. n_gpa, gp->l1_lpid);
  1262. if (!ret)
  1263. ret = -EINVAL;
  1264. else
  1265. ret = 0;
  1266. out_unlock:
  1267. spin_unlock(&kvm->mmu_lock);
  1268. return ret;
  1269. }
  1270. static inline int kvmppc_radix_level_to_shift(int level)
  1271. {
  1272. switch (level) {
  1273. case 2:
  1274. return PUD_SHIFT;
  1275. case 1:
  1276. return PMD_SHIFT;
  1277. default:
  1278. return PAGE_SHIFT;
  1279. }
  1280. }
  1281. static inline int kvmppc_radix_shift_to_level(int shift)
  1282. {
  1283. if (shift == PUD_SHIFT)
  1284. return 2;
  1285. if (shift == PMD_SHIFT)
  1286. return 1;
  1287. if (shift == PAGE_SHIFT)
  1288. return 0;
  1289. WARN_ON_ONCE(1);
  1290. return 0;
  1291. }
  1292. /* called with gp->tlb_lock held */
  1293. static long int __kvmhv_nested_page_fault(struct kvm_vcpu *vcpu,
  1294. struct kvm_nested_guest *gp)
  1295. {
  1296. struct kvm *kvm = vcpu->kvm;
  1297. struct kvm_memory_slot *memslot;
  1298. struct rmap_nested *n_rmap;
  1299. struct kvmppc_pte gpte;
  1300. pte_t pte, *pte_p;
  1301. unsigned long mmu_seq;
  1302. unsigned long dsisr = vcpu->arch.fault_dsisr;
  1303. unsigned long ea = vcpu->arch.fault_dar;
  1304. unsigned long *rmapp;
  1305. unsigned long n_gpa, gpa, gfn, perm = 0UL;
  1306. unsigned int shift, l1_shift, level;
  1307. bool writing = !!(dsisr & DSISR_ISSTORE);
  1308. bool kvm_ro = false;
  1309. long int ret;
  1310. if (!gp->l1_gr_to_hr) {
  1311. kvmhv_update_ptbl_cache(gp);
  1312. if (!gp->l1_gr_to_hr)
  1313. return RESUME_HOST;
  1314. }
  1315. /* Convert the nested guest real address into a L1 guest real address */
  1316. n_gpa = vcpu->arch.fault_gpa & ~0xF000000000000FFFULL;
  1317. if (!(dsisr & DSISR_PRTABLE_FAULT))
  1318. n_gpa |= ea & 0xFFF;
  1319. ret = kvmhv_translate_addr_nested(vcpu, gp, n_gpa, dsisr, &gpte);
  1320. /*
  1321. * If the hardware found a translation but we don't now have a usable
  1322. * translation in the l1 partition-scoped tree, remove the shadow pte
  1323. * and let the guest retry.
  1324. */
  1325. if (ret == RESUME_HOST &&
  1326. (dsisr & (DSISR_PROTFAULT | DSISR_BADACCESS | DSISR_NOEXEC_OR_G |
  1327. DSISR_BAD_COPYPASTE)))
  1328. goto inval;
  1329. if (ret)
  1330. return ret;
  1331. /* Failed to set the reference/change bits */
  1332. if (dsisr & DSISR_SET_RC) {
  1333. ret = kvmhv_handle_nested_set_rc(vcpu, gp, n_gpa, gpte, dsisr);
  1334. if (ret == RESUME_HOST)
  1335. return ret;
  1336. if (ret)
  1337. goto inval;
  1338. dsisr &= ~DSISR_SET_RC;
  1339. if (!(dsisr & (DSISR_BAD_FAULT_64S | DSISR_NOHPTE |
  1340. DSISR_PROTFAULT)))
  1341. return RESUME_GUEST;
  1342. }
  1343. /*
  1344. * We took an HISI or HDSI while we were running a nested guest which
  1345. * means we have no partition scoped translation for that. This means
  1346. * we need to insert a pte for the mapping into our shadow_pgtable.
  1347. */
  1348. l1_shift = gpte.page_shift;
  1349. if (l1_shift < PAGE_SHIFT) {
  1350. /* We don't support l1 using a page size smaller than our own */
  1351. pr_err("KVM: L1 guest page shift (%d) less than our own (%d)\n",
  1352. l1_shift, PAGE_SHIFT);
  1353. return -EINVAL;
  1354. }
  1355. gpa = gpte.raddr;
  1356. gfn = gpa >> PAGE_SHIFT;
  1357. /* 1. Get the corresponding host memslot */
  1358. memslot = gfn_to_memslot(kvm, gfn);
  1359. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) {
  1360. if (dsisr & (DSISR_PRTABLE_FAULT | DSISR_BADACCESS)) {
  1361. /* unusual error -> reflect to the guest as a DSI */
  1362. kvmppc_core_queue_data_storage(vcpu, ea, dsisr);
  1363. return RESUME_GUEST;
  1364. }
  1365. /* passthrough of emulated MMIO case */
  1366. return kvmppc_hv_emulate_mmio(vcpu, gpa, ea, writing);
  1367. }
  1368. if (memslot->flags & KVM_MEM_READONLY) {
  1369. if (writing) {
  1370. /* Give the guest a DSI */
  1371. kvmppc_core_queue_data_storage(vcpu, ea,
  1372. DSISR_ISSTORE | DSISR_PROTFAULT);
  1373. return RESUME_GUEST;
  1374. }
  1375. kvm_ro = true;
  1376. }
  1377. /* 2. Find the host pte for this L1 guest real address */
  1378. /* Used to check for invalidations in progress */
  1379. mmu_seq = kvm->mmu_invalidate_seq;
  1380. smp_rmb();
  1381. /* See if can find translation in our partition scoped tables for L1 */
  1382. pte = __pte(0);
  1383. spin_lock(&kvm->mmu_lock);
  1384. pte_p = find_kvm_secondary_pte(kvm, gpa, &shift);
  1385. if (!shift)
  1386. shift = PAGE_SHIFT;
  1387. if (pte_p)
  1388. pte = *pte_p;
  1389. spin_unlock(&kvm->mmu_lock);
  1390. if (!pte_present(pte) || (writing && !(pte_val(pte) & _PAGE_WRITE))) {
  1391. /* No suitable pte found -> try to insert a mapping */
  1392. ret = kvmppc_book3s_instantiate_page(vcpu, gpa, memslot,
  1393. writing, kvm_ro, &pte, &level);
  1394. if (ret == -EAGAIN)
  1395. return RESUME_GUEST;
  1396. else if (ret)
  1397. return ret;
  1398. shift = kvmppc_radix_level_to_shift(level);
  1399. }
  1400. /* Align gfn to the start of the page */
  1401. gfn = (gpa & ~((1UL << shift) - 1)) >> PAGE_SHIFT;
  1402. /* 3. Compute the pte we need to insert for nest_gpa -> host r_addr */
  1403. /* The permissions is the combination of the host and l1 guest ptes */
  1404. perm |= gpte.may_read ? 0UL : _PAGE_READ;
  1405. perm |= gpte.may_write ? 0UL : _PAGE_WRITE;
  1406. perm |= gpte.may_execute ? 0UL : _PAGE_EXEC;
  1407. /* Only set accessed/dirty (rc) bits if set in host and l1 guest ptes */
  1408. perm |= (gpte.rc & _PAGE_ACCESSED) ? 0UL : _PAGE_ACCESSED;
  1409. perm |= ((gpte.rc & _PAGE_DIRTY) && writing) ? 0UL : _PAGE_DIRTY;
  1410. pte = __pte(pte_val(pte) & ~perm);
  1411. /* What size pte can we insert? */
  1412. if (shift > l1_shift) {
  1413. u64 mask;
  1414. unsigned int actual_shift = PAGE_SHIFT;
  1415. if (PMD_SHIFT < l1_shift)
  1416. actual_shift = PMD_SHIFT;
  1417. mask = (1UL << shift) - (1UL << actual_shift);
  1418. pte = __pte(pte_val(pte) | (gpa & mask));
  1419. shift = actual_shift;
  1420. }
  1421. level = kvmppc_radix_shift_to_level(shift);
  1422. n_gpa &= ~((1UL << shift) - 1);
  1423. /* 4. Insert the pte into our shadow_pgtable */
  1424. n_rmap = kzalloc(sizeof(*n_rmap), GFP_KERNEL);
  1425. if (!n_rmap)
  1426. return RESUME_GUEST; /* Let the guest try again */
  1427. n_rmap->rmap = (n_gpa & RMAP_NESTED_GPA_MASK) |
  1428. (((unsigned long) gp->l1_lpid) << RMAP_NESTED_LPID_SHIFT);
  1429. rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn];
  1430. ret = kvmppc_create_pte(kvm, gp->shadow_pgtable, pte, n_gpa, level,
  1431. mmu_seq, gp->shadow_lpid, rmapp, &n_rmap);
  1432. kfree(n_rmap);
  1433. if (ret == -EAGAIN)
  1434. ret = RESUME_GUEST; /* Let the guest try again */
  1435. return ret;
  1436. inval:
  1437. kvmhv_invalidate_shadow_pte(vcpu, gp, n_gpa, NULL);
  1438. return RESUME_GUEST;
  1439. }
  1440. long int kvmhv_nested_page_fault(struct kvm_vcpu *vcpu)
  1441. {
  1442. struct kvm_nested_guest *gp = vcpu->arch.nested;
  1443. long int ret;
  1444. mutex_lock(&gp->tlb_lock);
  1445. ret = __kvmhv_nested_page_fault(vcpu, gp);
  1446. mutex_unlock(&gp->tlb_lock);
  1447. return ret;
  1448. }
  1449. int kvmhv_nested_next_lpid(struct kvm *kvm, int lpid)
  1450. {
  1451. int ret = lpid + 1;
  1452. spin_lock(&kvm->mmu_lock);
  1453. if (!idr_get_next(&kvm->arch.kvm_nested_guest_idr, &ret))
  1454. ret = -1;
  1455. spin_unlock(&kvm->mmu_lock);
  1456. return ret;
  1457. }