vsie.c 31 KB

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