vsie.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. /*
  2. * kvm nested virtualization support for s390x
  3. *
  4. * Copyright IBM Corp. 2016
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License (version 2 only)
  8. * as published by the Free Software Foundation.
  9. *
  10. * Author(s): David Hildenbrand <dahi@linux.vnet.ibm.com>
  11. */
  12. #include <linux/vmalloc.h>
  13. #include <linux/kvm_host.h>
  14. #include <linux/bug.h>
  15. #include <linux/list.h>
  16. #include <linux/bitmap.h>
  17. #include <linux/sched/signal.h>
  18. #include <asm/gmap.h>
  19. #include <asm/mmu_context.h>
  20. #include <asm/sclp.h>
  21. #include <asm/nmi.h>
  22. #include <asm/dis.h>
  23. #include "kvm-s390.h"
  24. #include "gaccess.h"
  25. struct vsie_page {
  26. struct kvm_s390_sie_block scb_s; /* 0x0000 */
  27. /* the pinned originial scb */
  28. struct kvm_s390_sie_block *scb_o; /* 0x0200 */
  29. /* the shadow gmap in use by the vsie_page */
  30. struct gmap *gmap; /* 0x0208 */
  31. /* address of the last reported fault to guest2 */
  32. unsigned long fault_addr; /* 0x0210 */
  33. __u8 reserved[0x0700 - 0x0218]; /* 0x0218 */
  34. struct kvm_s390_crypto_cb crycb; /* 0x0700 */
  35. __u8 fac[S390_ARCH_FAC_LIST_SIZE_BYTE]; /* 0x0800 */
  36. } __packed;
  37. /* trigger a validity icpt for the given scb */
  38. static int set_validity_icpt(struct kvm_s390_sie_block *scb,
  39. __u16 reason_code)
  40. {
  41. scb->ipa = 0x1000;
  42. scb->ipb = ((__u32) reason_code) << 16;
  43. scb->icptcode = ICPT_VALIDITY;
  44. return 1;
  45. }
  46. /* mark the prefix as unmapped, this will block the VSIE */
  47. static void prefix_unmapped(struct vsie_page *vsie_page)
  48. {
  49. atomic_or(PROG_REQUEST, &vsie_page->scb_s.prog20);
  50. }
  51. /* mark the prefix as unmapped and wait until the VSIE has been left */
  52. static void prefix_unmapped_sync(struct vsie_page *vsie_page)
  53. {
  54. prefix_unmapped(vsie_page);
  55. if (vsie_page->scb_s.prog0c & PROG_IN_SIE)
  56. atomic_or(CPUSTAT_STOP_INT, &vsie_page->scb_s.cpuflags);
  57. while (vsie_page->scb_s.prog0c & PROG_IN_SIE)
  58. cpu_relax();
  59. }
  60. /* mark the prefix as mapped, this will allow the VSIE to run */
  61. static void prefix_mapped(struct vsie_page *vsie_page)
  62. {
  63. atomic_andnot(PROG_REQUEST, &vsie_page->scb_s.prog20);
  64. }
  65. /* test if the prefix is mapped into the gmap shadow */
  66. static int prefix_is_mapped(struct vsie_page *vsie_page)
  67. {
  68. return !(atomic_read(&vsie_page->scb_s.prog20) & PROG_REQUEST);
  69. }
  70. /* copy the updated intervention request bits into the shadow scb */
  71. static void update_intervention_requests(struct vsie_page *vsie_page)
  72. {
  73. const int bits = CPUSTAT_STOP_INT | CPUSTAT_IO_INT | CPUSTAT_EXT_INT;
  74. int cpuflags;
  75. cpuflags = atomic_read(&vsie_page->scb_o->cpuflags);
  76. atomic_andnot(bits, &vsie_page->scb_s.cpuflags);
  77. atomic_or(cpuflags & bits, &vsie_page->scb_s.cpuflags);
  78. }
  79. /* shadow (filter and validate) the cpuflags */
  80. static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  81. {
  82. struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
  83. struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
  84. int newflags, cpuflags = atomic_read(&scb_o->cpuflags);
  85. /* we don't allow ESA/390 guests */
  86. if (!(cpuflags & CPUSTAT_ZARCH))
  87. return set_validity_icpt(scb_s, 0x0001U);
  88. if (cpuflags & (CPUSTAT_RRF | CPUSTAT_MCDS))
  89. return set_validity_icpt(scb_s, 0x0001U);
  90. else if (cpuflags & (CPUSTAT_SLSV | CPUSTAT_SLSR))
  91. return set_validity_icpt(scb_s, 0x0007U);
  92. /* intervention requests will be set later */
  93. newflags = CPUSTAT_ZARCH;
  94. if (cpuflags & CPUSTAT_GED && test_kvm_facility(vcpu->kvm, 8))
  95. newflags |= CPUSTAT_GED;
  96. if (cpuflags & CPUSTAT_GED2 && test_kvm_facility(vcpu->kvm, 78)) {
  97. if (cpuflags & CPUSTAT_GED)
  98. return set_validity_icpt(scb_s, 0x0001U);
  99. newflags |= CPUSTAT_GED2;
  100. }
  101. if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GPERE))
  102. newflags |= cpuflags & CPUSTAT_P;
  103. if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_GSLS))
  104. newflags |= cpuflags & CPUSTAT_SM;
  105. if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IBS))
  106. newflags |= cpuflags & CPUSTAT_IBS;
  107. if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_KSS))
  108. newflags |= cpuflags & CPUSTAT_KSS;
  109. atomic_set(&scb_s->cpuflags, newflags);
  110. return 0;
  111. }
  112. /*
  113. * Create a shadow copy of the crycb block and setup key wrapping, if
  114. * requested for guest 3 and enabled for guest 2.
  115. *
  116. * We only accept format-1 (no AP in g2), but convert it into format-2
  117. * There is nothing to do for format-0.
  118. *
  119. * Returns: - 0 if shadowed or nothing to do
  120. * - > 0 if control has to be given to guest 2
  121. */
  122. static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  123. {
  124. struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
  125. struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
  126. u32 crycb_addr = scb_o->crycbd & 0x7ffffff8U;
  127. unsigned long *b1, *b2;
  128. u8 ecb3_flags;
  129. scb_s->crycbd = 0;
  130. if (!(scb_o->crycbd & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))
  131. return 0;
  132. /* format-1 is supported with message-security-assist extension 3 */
  133. if (!test_kvm_facility(vcpu->kvm, 76))
  134. return 0;
  135. /* we may only allow it if enabled for guest 2 */
  136. ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 &
  137. (ECB3_AES | ECB3_DEA);
  138. if (!ecb3_flags)
  139. return 0;
  140. if ((crycb_addr & PAGE_MASK) != ((crycb_addr + 128) & PAGE_MASK))
  141. return set_validity_icpt(scb_s, 0x003CU);
  142. else if (!crycb_addr)
  143. return set_validity_icpt(scb_s, 0x0039U);
  144. /* copy only the wrapping keys */
  145. if (read_guest_real(vcpu, crycb_addr + 72, &vsie_page->crycb, 56))
  146. return set_validity_icpt(scb_s, 0x0035U);
  147. scb_s->ecb3 |= ecb3_flags;
  148. scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT1 |
  149. CRYCB_FORMAT2;
  150. /* xor both blocks in one run */
  151. b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask;
  152. b2 = (unsigned long *)
  153. vcpu->kvm->arch.crypto.crycb->dea_wrapping_key_mask;
  154. /* as 56%8 == 0, bitmap_xor won't overwrite any data */
  155. bitmap_xor(b1, b1, b2, BITS_PER_BYTE * 56);
  156. return 0;
  157. }
  158. /* shadow (round up/down) the ibc to avoid validity icpt */
  159. static void prepare_ibc(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  160. {
  161. struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
  162. struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
  163. __u64 min_ibc = (sclp.ibc >> 16) & 0x0fffU;
  164. scb_s->ibc = 0;
  165. /* ibc installed in g2 and requested for g3 */
  166. if (vcpu->kvm->arch.model.ibc && (scb_o->ibc & 0x0fffU)) {
  167. scb_s->ibc = scb_o->ibc & 0x0fffU;
  168. /* takte care of the minimum ibc level of the machine */
  169. if (scb_s->ibc < min_ibc)
  170. scb_s->ibc = min_ibc;
  171. /* take care of the maximum ibc level set for the guest */
  172. if (scb_s->ibc > vcpu->kvm->arch.model.ibc)
  173. scb_s->ibc = vcpu->kvm->arch.model.ibc;
  174. }
  175. }
  176. /* unshadow the scb, copying parameters back to the real scb */
  177. static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  178. {
  179. struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
  180. struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
  181. /* interception */
  182. scb_o->icptcode = scb_s->icptcode;
  183. scb_o->icptstatus = scb_s->icptstatus;
  184. scb_o->ipa = scb_s->ipa;
  185. scb_o->ipb = scb_s->ipb;
  186. scb_o->gbea = scb_s->gbea;
  187. /* timer */
  188. scb_o->cputm = scb_s->cputm;
  189. scb_o->ckc = scb_s->ckc;
  190. scb_o->todpr = scb_s->todpr;
  191. /* guest state */
  192. scb_o->gpsw = scb_s->gpsw;
  193. scb_o->gg14 = scb_s->gg14;
  194. scb_o->gg15 = scb_s->gg15;
  195. memcpy(scb_o->gcr, scb_s->gcr, 128);
  196. scb_o->pp = scb_s->pp;
  197. /* interrupt intercept */
  198. switch (scb_s->icptcode) {
  199. case ICPT_PROGI:
  200. case ICPT_INSTPROGI:
  201. case ICPT_EXTINT:
  202. memcpy((void *)((u64)scb_o + 0xc0),
  203. (void *)((u64)scb_s + 0xc0), 0xf0 - 0xc0);
  204. break;
  205. case ICPT_PARTEXEC:
  206. /* MVPG only */
  207. memcpy((void *)((u64)scb_o + 0xc0),
  208. (void *)((u64)scb_s + 0xc0), 0xd0 - 0xc0);
  209. break;
  210. }
  211. if (scb_s->ihcpu != 0xffffU)
  212. scb_o->ihcpu = scb_s->ihcpu;
  213. }
  214. /*
  215. * Setup the shadow scb by copying and checking the relevant parts of the g2
  216. * provided scb.
  217. *
  218. * Returns: - 0 if the scb has been shadowed
  219. * - > 0 if control has to be given to guest 2
  220. */
  221. static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  222. {
  223. struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
  224. struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
  225. bool had_tx = scb_s->ecb & ECB_TE;
  226. unsigned long new_mso = 0;
  227. int rc;
  228. /* make sure we don't have any leftovers when reusing the scb */
  229. scb_s->icptcode = 0;
  230. scb_s->eca = 0;
  231. scb_s->ecb = 0;
  232. scb_s->ecb2 = 0;
  233. scb_s->ecb3 = 0;
  234. scb_s->ecd = 0;
  235. scb_s->fac = 0;
  236. rc = prepare_cpuflags(vcpu, vsie_page);
  237. if (rc)
  238. goto out;
  239. /* timer */
  240. scb_s->cputm = scb_o->cputm;
  241. scb_s->ckc = scb_o->ckc;
  242. scb_s->todpr = scb_o->todpr;
  243. scb_s->epoch = scb_o->epoch;
  244. /* guest state */
  245. scb_s->gpsw = scb_o->gpsw;
  246. scb_s->gg14 = scb_o->gg14;
  247. scb_s->gg15 = scb_o->gg15;
  248. memcpy(scb_s->gcr, scb_o->gcr, 128);
  249. scb_s->pp = scb_o->pp;
  250. /* interception / execution handling */
  251. scb_s->gbea = scb_o->gbea;
  252. scb_s->lctl = scb_o->lctl;
  253. scb_s->svcc = scb_o->svcc;
  254. scb_s->ictl = scb_o->ictl;
  255. /*
  256. * SKEY handling functions can't deal with false setting of PTE invalid
  257. * bits. Therefore we cannot provide interpretation and would later
  258. * have to provide own emulation handlers.
  259. */
  260. if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_KSS))
  261. scb_s->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
  262. scb_s->icpua = scb_o->icpua;
  263. if (!(atomic_read(&scb_s->cpuflags) & CPUSTAT_SM))
  264. new_mso = scb_o->mso & 0xfffffffffff00000UL;
  265. /* if the hva of the prefix changes, we have to remap the prefix */
  266. if (scb_s->mso != new_mso || scb_s->prefix != scb_o->prefix)
  267. prefix_unmapped(vsie_page);
  268. /* SIE will do mso/msl validity and exception checks for us */
  269. scb_s->msl = scb_o->msl & 0xfffffffffff00000UL;
  270. scb_s->mso = new_mso;
  271. scb_s->prefix = scb_o->prefix;
  272. /* We have to definetly flush the tlb if this scb never ran */
  273. if (scb_s->ihcpu != 0xffffU)
  274. scb_s->ihcpu = scb_o->ihcpu;
  275. /* MVPG and Protection Exception Interpretation are always available */
  276. scb_s->eca |= scb_o->eca & (ECA_MVPGI | ECA_PROTEXCI);
  277. /* Host-protection-interruption introduced with ESOP */
  278. if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_ESOP))
  279. scb_s->ecb |= scb_o->ecb & ECB_HOSTPROTINT;
  280. /* transactional execution */
  281. if (test_kvm_facility(vcpu->kvm, 73)) {
  282. /* remap the prefix is tx is toggled on */
  283. if ((scb_o->ecb & ECB_TE) && !had_tx)
  284. prefix_unmapped(vsie_page);
  285. scb_s->ecb |= scb_o->ecb & ECB_TE;
  286. }
  287. /* SIMD */
  288. if (test_kvm_facility(vcpu->kvm, 129)) {
  289. scb_s->eca |= scb_o->eca & ECA_VX;
  290. scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
  291. }
  292. /* Run-time-Instrumentation */
  293. if (test_kvm_facility(vcpu->kvm, 64))
  294. scb_s->ecb3 |= scb_o->ecb3 & ECB3_RI;
  295. /* Instruction Execution Prevention */
  296. if (test_kvm_facility(vcpu->kvm, 130))
  297. scb_s->ecb2 |= scb_o->ecb2 & ECB2_IEP;
  298. /* Guarded Storage */
  299. if (test_kvm_facility(vcpu->kvm, 133)) {
  300. scb_s->ecb |= scb_o->ecb & ECB_GS;
  301. scb_s->ecd |= scb_o->ecd & ECD_HOSTREGMGMT;
  302. }
  303. if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIIF))
  304. scb_s->eca |= scb_o->eca & ECA_SII;
  305. if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_IB))
  306. scb_s->eca |= scb_o->eca & ECA_IB;
  307. if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_CEI))
  308. scb_s->eca |= scb_o->eca & ECA_CEI;
  309. prepare_ibc(vcpu, vsie_page);
  310. rc = shadow_crycb(vcpu, vsie_page);
  311. out:
  312. if (rc)
  313. unshadow_scb(vcpu, vsie_page);
  314. return rc;
  315. }
  316. void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start,
  317. unsigned long end)
  318. {
  319. struct kvm *kvm = gmap->private;
  320. struct vsie_page *cur;
  321. unsigned long prefix;
  322. struct page *page;
  323. int i;
  324. if (!gmap_is_shadow(gmap))
  325. return;
  326. if (start >= 1UL << 31)
  327. /* We are only interested in prefix pages */
  328. return;
  329. /*
  330. * Only new shadow blocks are added to the list during runtime,
  331. * therefore we can safely reference them all the time.
  332. */
  333. for (i = 0; i < kvm->arch.vsie.page_count; i++) {
  334. page = READ_ONCE(kvm->arch.vsie.pages[i]);
  335. if (!page)
  336. continue;
  337. cur = page_to_virt(page);
  338. if (READ_ONCE(cur->gmap) != gmap)
  339. continue;
  340. prefix = cur->scb_s.prefix << GUEST_PREFIX_SHIFT;
  341. /* with mso/msl, the prefix lies at an offset */
  342. prefix += cur->scb_s.mso;
  343. if (prefix <= end && start <= prefix + 2 * PAGE_SIZE - 1)
  344. prefix_unmapped_sync(cur);
  345. }
  346. }
  347. /*
  348. * Map the first prefix page and if tx is enabled also the second prefix page.
  349. *
  350. * The prefix will be protected, a gmap notifier will inform about unmaps.
  351. * The shadow scb must not be executed until the prefix is remapped, this is
  352. * guaranteed by properly handling PROG_REQUEST.
  353. *
  354. * Returns: - 0 on if successfully mapped or already mapped
  355. * - > 0 if control has to be given to guest 2
  356. * - -EAGAIN if the caller can retry immediately
  357. * - -ENOMEM if out of memory
  358. */
  359. static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  360. {
  361. struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
  362. u64 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT;
  363. int rc;
  364. if (prefix_is_mapped(vsie_page))
  365. return 0;
  366. /* mark it as mapped so we can catch any concurrent unmappers */
  367. prefix_mapped(vsie_page);
  368. /* with mso/msl, the prefix lies at offset *mso* */
  369. prefix += scb_s->mso;
  370. rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, prefix);
  371. if (!rc && (scb_s->ecb & ECB_TE))
  372. rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
  373. prefix + PAGE_SIZE);
  374. /*
  375. * We don't have to mprotect, we will be called for all unshadows.
  376. * SIE will detect if protection applies and trigger a validity.
  377. */
  378. if (rc)
  379. prefix_unmapped(vsie_page);
  380. if (rc > 0 || rc == -EFAULT)
  381. rc = set_validity_icpt(scb_s, 0x0037U);
  382. return rc;
  383. }
  384. /*
  385. * Pin the guest page given by gpa and set hpa to the pinned host address.
  386. * Will always be pinned writable.
  387. *
  388. * Returns: - 0 on success
  389. * - -EINVAL if the gpa is not valid guest storage
  390. * - -ENOMEM if out of memory
  391. */
  392. static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
  393. {
  394. struct page *page;
  395. hva_t hva;
  396. int rc;
  397. hva = gfn_to_hva(kvm, gpa_to_gfn(gpa));
  398. if (kvm_is_error_hva(hva))
  399. return -EINVAL;
  400. rc = get_user_pages_fast(hva, 1, 1, &page);
  401. if (rc < 0)
  402. return rc;
  403. else if (rc != 1)
  404. return -ENOMEM;
  405. *hpa = (hpa_t) page_to_virt(page) + (gpa & ~PAGE_MASK);
  406. return 0;
  407. }
  408. /* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
  409. static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)
  410. {
  411. struct page *page;
  412. page = virt_to_page(hpa);
  413. set_page_dirty_lock(page);
  414. put_page(page);
  415. /* mark the page always as dirty for migration */
  416. mark_page_dirty(kvm, gpa_to_gfn(gpa));
  417. }
  418. /* unpin all blocks previously pinned by pin_blocks(), marking them dirty */
  419. static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  420. {
  421. struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
  422. struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
  423. hpa_t hpa;
  424. gpa_t gpa;
  425. hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
  426. if (hpa) {
  427. gpa = scb_o->scaol & ~0xfUL;
  428. if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
  429. gpa |= (u64) scb_o->scaoh << 32;
  430. unpin_guest_page(vcpu->kvm, gpa, hpa);
  431. scb_s->scaol = 0;
  432. scb_s->scaoh = 0;
  433. }
  434. hpa = scb_s->itdba;
  435. if (hpa) {
  436. gpa = scb_o->itdba & ~0xffUL;
  437. unpin_guest_page(vcpu->kvm, gpa, hpa);
  438. scb_s->itdba = 0;
  439. }
  440. hpa = scb_s->gvrd;
  441. if (hpa) {
  442. gpa = scb_o->gvrd & ~0x1ffUL;
  443. unpin_guest_page(vcpu->kvm, gpa, hpa);
  444. scb_s->gvrd = 0;
  445. }
  446. hpa = scb_s->riccbd;
  447. if (hpa) {
  448. gpa = scb_o->riccbd & ~0x3fUL;
  449. unpin_guest_page(vcpu->kvm, gpa, hpa);
  450. scb_s->riccbd = 0;
  451. }
  452. hpa = scb_s->sdnxo;
  453. if (hpa) {
  454. gpa = scb_o->sdnxo;
  455. unpin_guest_page(vcpu->kvm, gpa, hpa);
  456. scb_s->sdnxo = 0;
  457. }
  458. }
  459. /*
  460. * Instead of shadowing some blocks, we can simply forward them because the
  461. * addresses in the scb are 64 bit long.
  462. *
  463. * This works as long as the data lies in one page. If blocks ever exceed one
  464. * page, we have to fall back to shadowing.
  465. *
  466. * As we reuse the sca, the vcpu pointers contained in it are invalid. We must
  467. * therefore not enable any facilities that access these pointers (e.g. SIGPIF).
  468. *
  469. * Returns: - 0 if all blocks were pinned.
  470. * - > 0 if control has to be given to guest 2
  471. * - -ENOMEM if out of memory
  472. */
  473. static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  474. {
  475. struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
  476. struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
  477. hpa_t hpa;
  478. gpa_t gpa;
  479. int rc = 0;
  480. gpa = scb_o->scaol & ~0xfUL;
  481. if (test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_64BSCAO))
  482. gpa |= (u64) scb_o->scaoh << 32;
  483. if (gpa) {
  484. if (!(gpa & ~0x1fffUL))
  485. rc = set_validity_icpt(scb_s, 0x0038U);
  486. else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu))
  487. rc = set_validity_icpt(scb_s, 0x0011U);
  488. else if ((gpa & PAGE_MASK) !=
  489. ((gpa + sizeof(struct bsca_block) - 1) & PAGE_MASK))
  490. rc = set_validity_icpt(scb_s, 0x003bU);
  491. if (!rc) {
  492. rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
  493. if (rc == -EINVAL)
  494. rc = set_validity_icpt(scb_s, 0x0034U);
  495. }
  496. if (rc)
  497. goto unpin;
  498. scb_s->scaoh = (u32)((u64)hpa >> 32);
  499. scb_s->scaol = (u32)(u64)hpa;
  500. }
  501. gpa = scb_o->itdba & ~0xffUL;
  502. if (gpa && (scb_s->ecb & ECB_TE)) {
  503. if (!(gpa & ~0x1fffU)) {
  504. rc = set_validity_icpt(scb_s, 0x0080U);
  505. goto unpin;
  506. }
  507. /* 256 bytes cannot cross page boundaries */
  508. rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
  509. if (rc == -EINVAL)
  510. rc = set_validity_icpt(scb_s, 0x0080U);
  511. if (rc)
  512. goto unpin;
  513. scb_s->itdba = hpa;
  514. }
  515. gpa = scb_o->gvrd & ~0x1ffUL;
  516. if (gpa && (scb_s->eca & ECA_VX) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
  517. if (!(gpa & ~0x1fffUL)) {
  518. rc = set_validity_icpt(scb_s, 0x1310U);
  519. goto unpin;
  520. }
  521. /*
  522. * 512 bytes vector registers cannot cross page boundaries
  523. * if this block gets bigger, we have to shadow it.
  524. */
  525. rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
  526. if (rc == -EINVAL)
  527. rc = set_validity_icpt(scb_s, 0x1310U);
  528. if (rc)
  529. goto unpin;
  530. scb_s->gvrd = hpa;
  531. }
  532. gpa = scb_o->riccbd & ~0x3fUL;
  533. if (gpa && (scb_s->ecb3 & ECB3_RI)) {
  534. if (!(gpa & ~0x1fffUL)) {
  535. rc = set_validity_icpt(scb_s, 0x0043U);
  536. goto unpin;
  537. }
  538. /* 64 bytes cannot cross page boundaries */
  539. rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
  540. if (rc == -EINVAL)
  541. rc = set_validity_icpt(scb_s, 0x0043U);
  542. /* Validity 0x0044 will be checked by SIE */
  543. if (rc)
  544. goto unpin;
  545. scb_s->riccbd = hpa;
  546. }
  547. if ((scb_s->ecb & ECB_GS) && !(scb_s->ecd & ECD_HOSTREGMGMT)) {
  548. unsigned long sdnxc;
  549. gpa = scb_o->sdnxo & ~0xfUL;
  550. sdnxc = scb_o->sdnxo & 0xfUL;
  551. if (!gpa || !(gpa & ~0x1fffUL)) {
  552. rc = set_validity_icpt(scb_s, 0x10b0U);
  553. goto unpin;
  554. }
  555. if (sdnxc < 6 || sdnxc > 12) {
  556. rc = set_validity_icpt(scb_s, 0x10b1U);
  557. goto unpin;
  558. }
  559. if (gpa & ((1 << sdnxc) - 1)) {
  560. rc = set_validity_icpt(scb_s, 0x10b2U);
  561. goto unpin;
  562. }
  563. /* Due to alignment rules (checked above) this cannot
  564. * cross page boundaries
  565. */
  566. rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
  567. if (rc == -EINVAL)
  568. rc = set_validity_icpt(scb_s, 0x10b0U);
  569. if (rc)
  570. goto unpin;
  571. scb_s->sdnxo = hpa | sdnxc;
  572. }
  573. return 0;
  574. unpin:
  575. unpin_blocks(vcpu, vsie_page);
  576. return rc;
  577. }
  578. /* unpin the scb provided by guest 2, marking it as dirty */
  579. static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
  580. gpa_t gpa)
  581. {
  582. hpa_t hpa = (hpa_t) vsie_page->scb_o;
  583. if (hpa)
  584. unpin_guest_page(vcpu->kvm, gpa, hpa);
  585. vsie_page->scb_o = NULL;
  586. }
  587. /*
  588. * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
  589. *
  590. * Returns: - 0 if the scb was pinned.
  591. * - > 0 if control has to be given to guest 2
  592. * - -ENOMEM if out of memory
  593. */
  594. static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
  595. gpa_t gpa)
  596. {
  597. hpa_t hpa;
  598. int rc;
  599. rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
  600. if (rc == -EINVAL) {
  601. rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  602. if (!rc)
  603. rc = 1;
  604. }
  605. if (!rc)
  606. vsie_page->scb_o = (struct kvm_s390_sie_block *) hpa;
  607. return rc;
  608. }
  609. /*
  610. * Inject a fault into guest 2.
  611. *
  612. * Returns: - > 0 if control has to be given to guest 2
  613. * < 0 if an error occurred during injection.
  614. */
  615. static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr,
  616. bool write_flag)
  617. {
  618. struct kvm_s390_pgm_info pgm = {
  619. .code = code,
  620. .trans_exc_code =
  621. /* 0-51: virtual address */
  622. (vaddr & 0xfffffffffffff000UL) |
  623. /* 52-53: store / fetch */
  624. (((unsigned int) !write_flag) + 1) << 10,
  625. /* 62-63: asce id (alway primary == 0) */
  626. .exc_access_id = 0, /* always primary */
  627. .op_access_id = 0, /* not MVPG */
  628. };
  629. int rc;
  630. if (code == PGM_PROTECTION)
  631. pgm.trans_exc_code |= 0x4UL;
  632. rc = kvm_s390_inject_prog_irq(vcpu, &pgm);
  633. return rc ? rc : 1;
  634. }
  635. /*
  636. * Handle a fault during vsie execution on a gmap shadow.
  637. *
  638. * Returns: - 0 if the fault was resolved
  639. * - > 0 if control has to be given to guest 2
  640. * - < 0 if an error occurred
  641. */
  642. static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  643. {
  644. int rc;
  645. if (current->thread.gmap_int_code == PGM_PROTECTION)
  646. /* we can directly forward all protection exceptions */
  647. return inject_fault(vcpu, PGM_PROTECTION,
  648. current->thread.gmap_addr, 1);
  649. rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
  650. current->thread.gmap_addr);
  651. if (rc > 0) {
  652. rc = inject_fault(vcpu, rc,
  653. current->thread.gmap_addr,
  654. current->thread.gmap_write_flag);
  655. if (rc >= 0)
  656. vsie_page->fault_addr = current->thread.gmap_addr;
  657. }
  658. return rc;
  659. }
  660. /*
  661. * Retry the previous fault that required guest 2 intervention. This avoids
  662. * one superfluous SIE re-entry and direct exit.
  663. *
  664. * Will ignore any errors. The next SIE fault will do proper fault handling.
  665. */
  666. static void handle_last_fault(struct kvm_vcpu *vcpu,
  667. struct vsie_page *vsie_page)
  668. {
  669. if (vsie_page->fault_addr)
  670. kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
  671. vsie_page->fault_addr);
  672. vsie_page->fault_addr = 0;
  673. }
  674. static inline void clear_vsie_icpt(struct vsie_page *vsie_page)
  675. {
  676. vsie_page->scb_s.icptcode = 0;
  677. }
  678. /* rewind the psw and clear the vsie icpt, so we can retry execution */
  679. static void retry_vsie_icpt(struct vsie_page *vsie_page)
  680. {
  681. struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
  682. int ilen = insn_length(scb_s->ipa >> 8);
  683. /* take care of EXECUTE instructions */
  684. if (scb_s->icptstatus & 1) {
  685. ilen = (scb_s->icptstatus >> 4) & 0x6;
  686. if (!ilen)
  687. ilen = 4;
  688. }
  689. scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, ilen);
  690. clear_vsie_icpt(vsie_page);
  691. }
  692. /*
  693. * Try to shadow + enable the guest 2 provided facility list.
  694. * Retry instruction execution if enabled for and provided by guest 2.
  695. *
  696. * Returns: - 0 if handled (retry or guest 2 icpt)
  697. * - > 0 if control has to be given to guest 2
  698. */
  699. static int handle_stfle(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  700. {
  701. struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
  702. __u32 fac = vsie_page->scb_o->fac & 0x7ffffff8U;
  703. if (fac && test_kvm_facility(vcpu->kvm, 7)) {
  704. retry_vsie_icpt(vsie_page);
  705. if (read_guest_real(vcpu, fac, &vsie_page->fac,
  706. sizeof(vsie_page->fac)))
  707. return set_validity_icpt(scb_s, 0x1090U);
  708. scb_s->fac = (__u32)(__u64) &vsie_page->fac;
  709. }
  710. return 0;
  711. }
  712. /*
  713. * Run the vsie on a shadow scb and a shadow gmap, without any further
  714. * sanity checks, handling SIE faults.
  715. *
  716. * Returns: - 0 everything went fine
  717. * - > 0 if control has to be given to guest 2
  718. * - < 0 if an error occurred
  719. */
  720. static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  721. {
  722. struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
  723. struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
  724. int rc;
  725. handle_last_fault(vcpu, vsie_page);
  726. if (need_resched())
  727. schedule();
  728. if (test_cpu_flag(CIF_MCCK_PENDING))
  729. s390_handle_mcck();
  730. srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
  731. local_irq_disable();
  732. guest_enter_irqoff();
  733. local_irq_enable();
  734. rc = sie64a(scb_s, vcpu->run->s.regs.gprs);
  735. local_irq_disable();
  736. guest_exit_irqoff();
  737. local_irq_enable();
  738. vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  739. if (rc > 0)
  740. rc = 0; /* we could still have an icpt */
  741. else if (rc == -EFAULT)
  742. return handle_fault(vcpu, vsie_page);
  743. switch (scb_s->icptcode) {
  744. case ICPT_INST:
  745. if (scb_s->ipa == 0xb2b0)
  746. rc = handle_stfle(vcpu, vsie_page);
  747. break;
  748. case ICPT_STOP:
  749. /* stop not requested by g2 - must have been a kick */
  750. if (!(atomic_read(&scb_o->cpuflags) & CPUSTAT_STOP_INT))
  751. clear_vsie_icpt(vsie_page);
  752. break;
  753. case ICPT_VALIDITY:
  754. if ((scb_s->ipa & 0xf000) != 0xf000)
  755. scb_s->ipa += 0x1000;
  756. break;
  757. }
  758. return rc;
  759. }
  760. static void release_gmap_shadow(struct vsie_page *vsie_page)
  761. {
  762. if (vsie_page->gmap)
  763. gmap_put(vsie_page->gmap);
  764. WRITE_ONCE(vsie_page->gmap, NULL);
  765. prefix_unmapped(vsie_page);
  766. }
  767. static int acquire_gmap_shadow(struct kvm_vcpu *vcpu,
  768. struct vsie_page *vsie_page)
  769. {
  770. unsigned long asce;
  771. union ctlreg0 cr0;
  772. struct gmap *gmap;
  773. int edat;
  774. asce = vcpu->arch.sie_block->gcr[1];
  775. cr0.val = vcpu->arch.sie_block->gcr[0];
  776. edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
  777. edat += edat && test_kvm_facility(vcpu->kvm, 78);
  778. /*
  779. * ASCE or EDAT could have changed since last icpt, or the gmap
  780. * we're holding has been unshadowed. If the gmap is still valid,
  781. * we can safely reuse it.
  782. */
  783. if (vsie_page->gmap && gmap_shadow_valid(vsie_page->gmap, asce, edat))
  784. return 0;
  785. /* release the old shadow - if any, and mark the prefix as unmapped */
  786. release_gmap_shadow(vsie_page);
  787. gmap = gmap_shadow(vcpu->arch.gmap, asce, edat);
  788. if (IS_ERR(gmap))
  789. return PTR_ERR(gmap);
  790. gmap->private = vcpu->kvm;
  791. WRITE_ONCE(vsie_page->gmap, gmap);
  792. return 0;
  793. }
  794. /*
  795. * Register the shadow scb at the VCPU, e.g. for kicking out of vsie.
  796. */
  797. static void register_shadow_scb(struct kvm_vcpu *vcpu,
  798. struct vsie_page *vsie_page)
  799. {
  800. struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
  801. WRITE_ONCE(vcpu->arch.vsie_block, &vsie_page->scb_s);
  802. /*
  803. * External calls have to lead to a kick of the vcpu and
  804. * therefore the vsie -> Simulate Wait state.
  805. */
  806. atomic_or(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
  807. /*
  808. * We have to adjust the g3 epoch by the g2 epoch. The epoch will
  809. * automatically be adjusted on tod clock changes via kvm_sync_clock.
  810. */
  811. preempt_disable();
  812. scb_s->epoch += vcpu->kvm->arch.epoch;
  813. preempt_enable();
  814. }
  815. /*
  816. * Unregister a shadow scb from a VCPU.
  817. */
  818. static void unregister_shadow_scb(struct kvm_vcpu *vcpu)
  819. {
  820. atomic_andnot(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
  821. WRITE_ONCE(vcpu->arch.vsie_block, NULL);
  822. }
  823. /*
  824. * Run the vsie on a shadowed scb, managing the gmap shadow, handling
  825. * prefix pages and faults.
  826. *
  827. * Returns: - 0 if no errors occurred
  828. * - > 0 if control has to be given to guest 2
  829. * - -ENOMEM if out of memory
  830. */
  831. static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  832. {
  833. struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
  834. int rc = 0;
  835. while (1) {
  836. rc = acquire_gmap_shadow(vcpu, vsie_page);
  837. if (!rc)
  838. rc = map_prefix(vcpu, vsie_page);
  839. if (!rc) {
  840. gmap_enable(vsie_page->gmap);
  841. update_intervention_requests(vsie_page);
  842. rc = do_vsie_run(vcpu, vsie_page);
  843. gmap_enable(vcpu->arch.gmap);
  844. }
  845. atomic_andnot(PROG_BLOCK_SIE, &scb_s->prog20);
  846. if (rc == -EAGAIN)
  847. rc = 0;
  848. if (rc || scb_s->icptcode || signal_pending(current) ||
  849. kvm_s390_vcpu_has_irq(vcpu, 0))
  850. break;
  851. }
  852. if (rc == -EFAULT) {
  853. /*
  854. * Addressing exceptions are always presentes as intercepts.
  855. * As addressing exceptions are suppressing and our guest 3 PSW
  856. * points at the responsible instruction, we have to
  857. * forward the PSW and set the ilc. If we can't read guest 3
  858. * instruction, we can use an arbitrary ilc. Let's always use
  859. * ilen = 4 for now, so we can avoid reading in guest 3 virtual
  860. * memory. (we could also fake the shadow so the hardware
  861. * handles it).
  862. */
  863. scb_s->icptcode = ICPT_PROGI;
  864. scb_s->iprcc = PGM_ADDRESSING;
  865. scb_s->pgmilc = 4;
  866. scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4);
  867. }
  868. return rc;
  869. }
  870. /*
  871. * Get or create a vsie page for a scb address.
  872. *
  873. * Returns: - address of a vsie page (cached or new one)
  874. * - NULL if the same scb address is already used by another VCPU
  875. * - ERR_PTR(-ENOMEM) if out of memory
  876. */
  877. static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr)
  878. {
  879. struct vsie_page *vsie_page;
  880. struct page *page;
  881. int nr_vcpus;
  882. rcu_read_lock();
  883. page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9);
  884. rcu_read_unlock();
  885. if (page) {
  886. if (page_ref_inc_return(page) == 2)
  887. return page_to_virt(page);
  888. page_ref_dec(page);
  889. }
  890. /*
  891. * We want at least #online_vcpus shadows, so every VCPU can execute
  892. * the VSIE in parallel.
  893. */
  894. nr_vcpus = atomic_read(&kvm->online_vcpus);
  895. mutex_lock(&kvm->arch.vsie.mutex);
  896. if (kvm->arch.vsie.page_count < nr_vcpus) {
  897. page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA);
  898. if (!page) {
  899. mutex_unlock(&kvm->arch.vsie.mutex);
  900. return ERR_PTR(-ENOMEM);
  901. }
  902. page_ref_inc(page);
  903. kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page;
  904. kvm->arch.vsie.page_count++;
  905. } else {
  906. /* reuse an existing entry that belongs to nobody */
  907. while (true) {
  908. page = kvm->arch.vsie.pages[kvm->arch.vsie.next];
  909. if (page_ref_inc_return(page) == 2)
  910. break;
  911. page_ref_dec(page);
  912. kvm->arch.vsie.next++;
  913. kvm->arch.vsie.next %= nr_vcpus;
  914. }
  915. radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
  916. }
  917. page->index = addr;
  918. /* double use of the same address */
  919. if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) {
  920. page_ref_dec(page);
  921. mutex_unlock(&kvm->arch.vsie.mutex);
  922. return NULL;
  923. }
  924. mutex_unlock(&kvm->arch.vsie.mutex);
  925. vsie_page = page_to_virt(page);
  926. memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block));
  927. release_gmap_shadow(vsie_page);
  928. vsie_page->fault_addr = 0;
  929. vsie_page->scb_s.ihcpu = 0xffffU;
  930. return vsie_page;
  931. }
  932. /* put a vsie page acquired via get_vsie_page */
  933. static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)
  934. {
  935. struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT);
  936. page_ref_dec(page);
  937. }
  938. int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
  939. {
  940. struct vsie_page *vsie_page;
  941. unsigned long scb_addr;
  942. int rc;
  943. vcpu->stat.instruction_sie++;
  944. if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2))
  945. return -EOPNOTSUPP;
  946. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  947. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  948. BUILD_BUG_ON(sizeof(struct vsie_page) != 4096);
  949. scb_addr = kvm_s390_get_base_disp_s(vcpu, NULL);
  950. /* 512 byte alignment */
  951. if (unlikely(scb_addr & 0x1ffUL))
  952. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  953. if (signal_pending(current) || kvm_s390_vcpu_has_irq(vcpu, 0))
  954. return 0;
  955. vsie_page = get_vsie_page(vcpu->kvm, scb_addr);
  956. if (IS_ERR(vsie_page))
  957. return PTR_ERR(vsie_page);
  958. else if (!vsie_page)
  959. /* double use of sie control block - simply do nothing */
  960. return 0;
  961. rc = pin_scb(vcpu, vsie_page, scb_addr);
  962. if (rc)
  963. goto out_put;
  964. rc = shadow_scb(vcpu, vsie_page);
  965. if (rc)
  966. goto out_unpin_scb;
  967. rc = pin_blocks(vcpu, vsie_page);
  968. if (rc)
  969. goto out_unshadow;
  970. register_shadow_scb(vcpu, vsie_page);
  971. rc = vsie_run(vcpu, vsie_page);
  972. unregister_shadow_scb(vcpu);
  973. unpin_blocks(vcpu, vsie_page);
  974. out_unshadow:
  975. unshadow_scb(vcpu, vsie_page);
  976. out_unpin_scb:
  977. unpin_scb(vcpu, vsie_page, scb_addr);
  978. out_put:
  979. put_vsie_page(vcpu->kvm, vsie_page);
  980. return rc < 0 ? rc : 0;
  981. }
  982. /* Init the vsie data structures. To be called when a vm is initialized. */
  983. void kvm_s390_vsie_init(struct kvm *kvm)
  984. {
  985. mutex_init(&kvm->arch.vsie.mutex);
  986. INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL);
  987. }
  988. /* Destroy the vsie data structures. To be called when a vm is destroyed. */
  989. void kvm_s390_vsie_destroy(struct kvm *kvm)
  990. {
  991. struct vsie_page *vsie_page;
  992. struct page *page;
  993. int i;
  994. mutex_lock(&kvm->arch.vsie.mutex);
  995. for (i = 0; i < kvm->arch.vsie.page_count; i++) {
  996. page = kvm->arch.vsie.pages[i];
  997. kvm->arch.vsie.pages[i] = NULL;
  998. vsie_page = page_to_virt(page);
  999. release_gmap_shadow(vsie_page);
  1000. /* free the radix tree entry */
  1001. radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
  1002. __free_page(page);
  1003. }
  1004. kvm->arch.vsie.page_count = 0;
  1005. mutex_unlock(&kvm->arch.vsie.mutex);
  1006. }
  1007. void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu)
  1008. {
  1009. struct kvm_s390_sie_block *scb = READ_ONCE(vcpu->arch.vsie_block);
  1010. /*
  1011. * Even if the VCPU lets go of the shadow sie block reference, it is
  1012. * still valid in the cache. So we can safely kick it.
  1013. */
  1014. if (scb) {
  1015. atomic_or(PROG_BLOCK_SIE, &scb->prog20);
  1016. if (scb->prog0c & PROG_IN_SIE)
  1017. atomic_or(CPUSTAT_STOP_INT, &scb->cpuflags);
  1018. }
  1019. }