]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/s390/kvm/vsie.c
KVM: s390: vsie: support setting the ibc
[mirror_ubuntu-artful-kernel.git] / arch / s390 / kvm / vsie.c
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 "kvm-s390.h"
22 #include "gaccess.h"
23
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 __u8 reserved[0x1000 - 0x0210]; /* 0x0210 */
31 } __packed;
32
33 /* trigger a validity icpt for the given scb */
34 static int set_validity_icpt(struct kvm_s390_sie_block *scb,
35 __u16 reason_code)
36 {
37 scb->ipa = 0x1000;
38 scb->ipb = ((__u32) reason_code) << 16;
39 scb->icptcode = ICPT_VALIDITY;
40 return 1;
41 }
42
43 /* mark the prefix as unmapped, this will block the VSIE */
44 static void prefix_unmapped(struct vsie_page *vsie_page)
45 {
46 atomic_or(PROG_REQUEST, &vsie_page->scb_s.prog20);
47 }
48
49 /* mark the prefix as unmapped and wait until the VSIE has been left */
50 static void prefix_unmapped_sync(struct vsie_page *vsie_page)
51 {
52 prefix_unmapped(vsie_page);
53 if (vsie_page->scb_s.prog0c & PROG_IN_SIE)
54 atomic_or(CPUSTAT_STOP_INT, &vsie_page->scb_s.cpuflags);
55 while (vsie_page->scb_s.prog0c & PROG_IN_SIE)
56 cpu_relax();
57 }
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
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
71 /* copy the updated intervention request bits into the shadow scb */
72 static void update_intervention_requests(struct vsie_page *vsie_page)
73 {
74 const int bits = CPUSTAT_STOP_INT | CPUSTAT_IO_INT | CPUSTAT_EXT_INT;
75 int cpuflags;
76
77 cpuflags = atomic_read(&vsie_page->scb_o->cpuflags);
78 atomic_andnot(bits, &vsie_page->scb_s.cpuflags);
79 atomic_or(cpuflags & bits, &vsie_page->scb_s.cpuflags);
80 }
81
82 /* shadow (filter and validate) the cpuflags */
83 static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
84 {
85 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
86 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
87 int newflags, cpuflags = atomic_read(&scb_o->cpuflags);
88
89 /* we don't allow ESA/390 guests */
90 if (!(cpuflags & CPUSTAT_ZARCH))
91 return set_validity_icpt(scb_s, 0x0001U);
92
93 if (cpuflags & (CPUSTAT_RRF | CPUSTAT_MCDS))
94 return set_validity_icpt(scb_s, 0x0001U);
95 else if (cpuflags & (CPUSTAT_SLSV | CPUSTAT_SLSR))
96 return set_validity_icpt(scb_s, 0x0007U);
97
98 /* intervention requests will be set later */
99 newflags = CPUSTAT_ZARCH;
100
101 atomic_set(&scb_s->cpuflags, newflags);
102 return 0;
103 }
104
105 /* shadow (round up/down) the ibc to avoid validity icpt */
106 static void prepare_ibc(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
107 {
108 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
109 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
110 __u64 min_ibc = (sclp.ibc >> 16) & 0x0fffU;
111
112 scb_s->ibc = 0;
113 /* ibc installed in g2 and requested for g3 */
114 if (vcpu->kvm->arch.model.ibc && (scb_o->ibc & 0x0fffU)) {
115 scb_s->ibc = scb_o->ibc & 0x0fffU;
116 /* takte care of the minimum ibc level of the machine */
117 if (scb_s->ibc < min_ibc)
118 scb_s->ibc = min_ibc;
119 /* take care of the maximum ibc level set for the guest */
120 if (scb_s->ibc > vcpu->kvm->arch.model.ibc)
121 scb_s->ibc = vcpu->kvm->arch.model.ibc;
122 }
123 }
124
125 /* unshadow the scb, copying parameters back to the real scb */
126 static void unshadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
127 {
128 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
129 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
130
131 /* interception */
132 scb_o->icptcode = scb_s->icptcode;
133 scb_o->icptstatus = scb_s->icptstatus;
134 scb_o->ipa = scb_s->ipa;
135 scb_o->ipb = scb_s->ipb;
136 scb_o->gbea = scb_s->gbea;
137
138 /* timer */
139 scb_o->cputm = scb_s->cputm;
140 scb_o->ckc = scb_s->ckc;
141 scb_o->todpr = scb_s->todpr;
142
143 /* guest state */
144 scb_o->gpsw = scb_s->gpsw;
145 scb_o->gg14 = scb_s->gg14;
146 scb_o->gg15 = scb_s->gg15;
147 memcpy(scb_o->gcr, scb_s->gcr, 128);
148 scb_o->pp = scb_s->pp;
149
150 /* interrupt intercept */
151 switch (scb_s->icptcode) {
152 case ICPT_PROGI:
153 case ICPT_INSTPROGI:
154 case ICPT_EXTINT:
155 memcpy((void *)((u64)scb_o + 0xc0),
156 (void *)((u64)scb_s + 0xc0), 0xf0 - 0xc0);
157 break;
158 case ICPT_PARTEXEC:
159 /* MVPG only */
160 memcpy((void *)((u64)scb_o + 0xc0),
161 (void *)((u64)scb_s + 0xc0), 0xd0 - 0xc0);
162 break;
163 }
164
165 if (scb_s->ihcpu != 0xffffU)
166 scb_o->ihcpu = scb_s->ihcpu;
167 }
168
169 /*
170 * Setup the shadow scb by copying and checking the relevant parts of the g2
171 * provided scb.
172 *
173 * Returns: - 0 if the scb has been shadowed
174 * - > 0 if control has to be given to guest 2
175 */
176 static int shadow_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
177 {
178 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
179 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
180 unsigned long new_mso;
181 int rc;
182
183 /* make sure we don't have any leftovers when reusing the scb */
184 scb_s->icptcode = 0;
185 scb_s->eca = 0;
186 scb_s->ecb = 0;
187 scb_s->ecb2 = 0;
188 scb_s->ecb3 = 0;
189 scb_s->ecd = 0;
190
191 rc = prepare_cpuflags(vcpu, vsie_page);
192 if (rc)
193 goto out;
194
195 /* timer */
196 scb_s->cputm = scb_o->cputm;
197 scb_s->ckc = scb_o->ckc;
198 scb_s->todpr = scb_o->todpr;
199 scb_s->epoch = scb_o->epoch;
200
201 /* guest state */
202 scb_s->gpsw = scb_o->gpsw;
203 scb_s->gg14 = scb_o->gg14;
204 scb_s->gg15 = scb_o->gg15;
205 memcpy(scb_s->gcr, scb_o->gcr, 128);
206 scb_s->pp = scb_o->pp;
207
208 /* interception / execution handling */
209 scb_s->gbea = scb_o->gbea;
210 scb_s->lctl = scb_o->lctl;
211 scb_s->svcc = scb_o->svcc;
212 scb_s->ictl = scb_o->ictl;
213 /*
214 * SKEY handling functions can't deal with false setting of PTE invalid
215 * bits. Therefore we cannot provide interpretation and would later
216 * have to provide own emulation handlers.
217 */
218 scb_s->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
219 scb_s->icpua = scb_o->icpua;
220
221 new_mso = scb_o->mso & 0xfffffffffff00000UL;
222 /* if the hva of the prefix changes, we have to remap the prefix */
223 if (scb_s->mso != new_mso || scb_s->prefix != scb_o->prefix)
224 prefix_unmapped(vsie_page);
225 /* SIE will do mso/msl validity and exception checks for us */
226 scb_s->msl = scb_o->msl & 0xfffffffffff00000UL;
227 scb_s->mso = new_mso;
228 scb_s->prefix = scb_o->prefix;
229
230 /* We have to definetly flush the tlb if this scb never ran */
231 if (scb_s->ihcpu != 0xffffU)
232 scb_s->ihcpu = scb_o->ihcpu;
233
234 /* MVPG and Protection Exception Interpretation are always available */
235 scb_s->eca |= scb_o->eca & 0x01002000U;
236
237 prepare_ibc(vcpu, vsie_page);
238 out:
239 if (rc)
240 unshadow_scb(vcpu, vsie_page);
241 return rc;
242 }
243
244 void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, unsigned long start,
245 unsigned long end)
246 {
247 struct kvm *kvm = gmap->private;
248 struct vsie_page *cur;
249 unsigned long prefix;
250 struct page *page;
251 int i;
252
253 if (!gmap_is_shadow(gmap))
254 return;
255 if (start >= 1UL << 31)
256 /* We are only interested in prefix pages */
257 return;
258
259 /*
260 * Only new shadow blocks are added to the list during runtime,
261 * therefore we can safely reference them all the time.
262 */
263 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
264 page = READ_ONCE(kvm->arch.vsie.pages[i]);
265 if (!page)
266 continue;
267 cur = page_to_virt(page);
268 if (READ_ONCE(cur->gmap) != gmap)
269 continue;
270 prefix = cur->scb_s.prefix << GUEST_PREFIX_SHIFT;
271 /* with mso/msl, the prefix lies at an offset */
272 prefix += cur->scb_s.mso;
273 if (prefix <= end && start <= prefix + PAGE_SIZE - 1)
274 prefix_unmapped_sync(cur);
275 }
276 }
277
278 /*
279 * Map the first prefix page.
280 *
281 * The prefix will be protected, a gmap notifier will inform about unmaps.
282 * The shadow scb must not be executed until the prefix is remapped, this is
283 * guaranteed by properly handling PROG_REQUEST.
284 *
285 * Returns: - 0 on if successfully mapped or already mapped
286 * - > 0 if control has to be given to guest 2
287 * - -EAGAIN if the caller can retry immediately
288 * - -ENOMEM if out of memory
289 */
290 static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
291 {
292 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
293 u64 prefix = scb_s->prefix << GUEST_PREFIX_SHIFT;
294 int rc;
295
296 if (prefix_is_mapped(vsie_page))
297 return 0;
298
299 /* mark it as mapped so we can catch any concurrent unmappers */
300 prefix_mapped(vsie_page);
301
302 /* with mso/msl, the prefix lies at offset *mso* */
303 prefix += scb_s->mso;
304
305 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap, prefix);
306 /*
307 * We don't have to mprotect, we will be called for all unshadows.
308 * SIE will detect if protection applies and trigger a validity.
309 */
310 if (rc)
311 prefix_unmapped(vsie_page);
312 if (rc > 0 || rc == -EFAULT)
313 rc = set_validity_icpt(scb_s, 0x0037U);
314 return rc;
315 }
316
317 /*
318 * Pin the guest page given by gpa and set hpa to the pinned host address.
319 * Will always be pinned writable.
320 *
321 * Returns: - 0 on success
322 * - -EINVAL if the gpa is not valid guest storage
323 * - -ENOMEM if out of memory
324 */
325 static int pin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t *hpa)
326 {
327 struct page *page;
328 hva_t hva;
329 int rc;
330
331 hva = gfn_to_hva(kvm, gpa_to_gfn(gpa));
332 if (kvm_is_error_hva(hva))
333 return -EINVAL;
334 rc = get_user_pages_fast(hva, 1, 1, &page);
335 if (rc < 0)
336 return rc;
337 else if (rc != 1)
338 return -ENOMEM;
339 *hpa = (hpa_t) page_to_virt(page) + (gpa & ~PAGE_MASK);
340 return 0;
341 }
342
343 /* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */
344 static void unpin_guest_page(struct kvm *kvm, gpa_t gpa, hpa_t hpa)
345 {
346 struct page *page;
347
348 page = virt_to_page(hpa);
349 set_page_dirty_lock(page);
350 put_page(page);
351 /* mark the page always as dirty for migration */
352 mark_page_dirty(kvm, gpa_to_gfn(gpa));
353 }
354
355 /* unpin all blocks previously pinned by pin_blocks(), marking them dirty */
356 static void unpin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
357 {
358 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
359 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
360 hpa_t hpa;
361 gpa_t gpa;
362
363 hpa = (u64) scb_s->scaoh << 32 | scb_s->scaol;
364 if (hpa) {
365 gpa = scb_o->scaol & ~0xfUL;
366 unpin_guest_page(vcpu->kvm, gpa, hpa);
367 scb_s->scaol = 0;
368 scb_s->scaoh = 0;
369 }
370 }
371
372 /*
373 * Instead of shadowing some blocks, we can simply forward them because the
374 * addresses in the scb are 64 bit long.
375 *
376 * This works as long as the data lies in one page. If blocks ever exceed one
377 * page, we have to fall back to shadowing.
378 *
379 * As we reuse the sca, the vcpu pointers contained in it are invalid. We must
380 * therefore not enable any facilities that access these pointers (e.g. SIGPIF).
381 *
382 * Returns: - 0 if all blocks were pinned.
383 * - > 0 if control has to be given to guest 2
384 * - -ENOMEM if out of memory
385 */
386 static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
387 {
388 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
389 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
390 hpa_t hpa;
391 gpa_t gpa;
392 int rc = 0;
393
394 gpa = scb_o->scaol & ~0xfUL;
395 if (gpa) {
396 if (!(gpa & ~0x1fffUL))
397 rc = set_validity_icpt(scb_s, 0x0038U);
398 else if ((gpa & ~0x1fffUL) == kvm_s390_get_prefix(vcpu))
399 rc = set_validity_icpt(scb_s, 0x0011U);
400 else if ((gpa & PAGE_MASK) !=
401 ((gpa + sizeof(struct bsca_block) - 1) & PAGE_MASK))
402 rc = set_validity_icpt(scb_s, 0x003bU);
403 if (!rc) {
404 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
405 if (rc == -EINVAL)
406 rc = set_validity_icpt(scb_s, 0x0034U);
407 }
408 if (rc)
409 goto unpin;
410 scb_s->scaoh = (u32)((u64)hpa >> 32);
411 scb_s->scaol = (u32)(u64)hpa;
412 }
413 return 0;
414 unpin:
415 unpin_blocks(vcpu, vsie_page);
416 return rc;
417 }
418
419 /* unpin the scb provided by guest 2, marking it as dirty */
420 static void unpin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
421 gpa_t gpa)
422 {
423 hpa_t hpa = (hpa_t) vsie_page->scb_o;
424
425 if (hpa)
426 unpin_guest_page(vcpu->kvm, gpa, hpa);
427 vsie_page->scb_o = NULL;
428 }
429
430 /*
431 * Pin the scb at gpa provided by guest 2 at vsie_page->scb_o.
432 *
433 * Returns: - 0 if the scb was pinned.
434 * - > 0 if control has to be given to guest 2
435 * - -ENOMEM if out of memory
436 */
437 static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page,
438 gpa_t gpa)
439 {
440 hpa_t hpa;
441 int rc;
442
443 rc = pin_guest_page(vcpu->kvm, gpa, &hpa);
444 if (rc == -EINVAL) {
445 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
446 if (!rc)
447 rc = 1;
448 }
449 if (!rc)
450 vsie_page->scb_o = (struct kvm_s390_sie_block *) hpa;
451 return rc;
452 }
453
454 /*
455 * Inject a fault into guest 2.
456 *
457 * Returns: - > 0 if control has to be given to guest 2
458 * < 0 if an error occurred during injection.
459 */
460 static int inject_fault(struct kvm_vcpu *vcpu, __u16 code, __u64 vaddr,
461 bool write_flag)
462 {
463 struct kvm_s390_pgm_info pgm = {
464 .code = code,
465 .trans_exc_code =
466 /* 0-51: virtual address */
467 (vaddr & 0xfffffffffffff000UL) |
468 /* 52-53: store / fetch */
469 (((unsigned int) !write_flag) + 1) << 10,
470 /* 62-63: asce id (alway primary == 0) */
471 .exc_access_id = 0, /* always primary */
472 .op_access_id = 0, /* not MVPG */
473 };
474 int rc;
475
476 if (code == PGM_PROTECTION)
477 pgm.trans_exc_code |= 0x4UL;
478
479 rc = kvm_s390_inject_prog_irq(vcpu, &pgm);
480 return rc ? rc : 1;
481 }
482
483 /*
484 * Handle a fault during vsie execution on a gmap shadow.
485 *
486 * Returns: - 0 if the fault was resolved
487 * - > 0 if control has to be given to guest 2
488 * - < 0 if an error occurred
489 */
490 static int handle_fault(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
491 {
492 int rc;
493
494 if (current->thread.gmap_int_code == PGM_PROTECTION)
495 /* we can directly forward all protection exceptions */
496 return inject_fault(vcpu, PGM_PROTECTION,
497 current->thread.gmap_addr, 1);
498
499 rc = kvm_s390_shadow_fault(vcpu, vsie_page->gmap,
500 current->thread.gmap_addr);
501 if (rc > 0) {
502 rc = inject_fault(vcpu, rc,
503 current->thread.gmap_addr,
504 current->thread.gmap_write_flag);
505 }
506 return rc;
507 }
508
509 static inline void clear_vsie_icpt(struct vsie_page *vsie_page)
510 {
511 vsie_page->scb_s.icptcode = 0;
512 }
513
514 /*
515 * Run the vsie on a shadow scb and a shadow gmap, without any further
516 * sanity checks, handling SIE faults.
517 *
518 * Returns: - 0 everything went fine
519 * - > 0 if control has to be given to guest 2
520 * - < 0 if an error occurred
521 */
522 static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
523 {
524 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
525 struct kvm_s390_sie_block *scb_o = vsie_page->scb_o;
526 int rc;
527
528 if (need_resched())
529 schedule();
530 if (test_cpu_flag(CIF_MCCK_PENDING))
531 s390_handle_mcck();
532
533 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
534 local_irq_disable();
535 kvm_guest_enter();
536 local_irq_enable();
537
538 rc = sie64a(scb_s, vcpu->run->s.regs.gprs);
539
540 local_irq_disable();
541 kvm_guest_exit();
542 local_irq_enable();
543 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
544
545 if (rc > 0)
546 rc = 0; /* we could still have an icpt */
547 else if (rc == -EFAULT)
548 return handle_fault(vcpu, vsie_page);
549
550 switch (scb_s->icptcode) {
551 case ICPT_STOP:
552 /* stop not requested by g2 - must have been a kick */
553 if (!(atomic_read(&scb_o->cpuflags) & CPUSTAT_STOP_INT))
554 clear_vsie_icpt(vsie_page);
555 break;
556 case ICPT_VALIDITY:
557 if ((scb_s->ipa & 0xf000) != 0xf000)
558 scb_s->ipa += 0x1000;
559 break;
560 }
561 return rc;
562 }
563
564 static void release_gmap_shadow(struct vsie_page *vsie_page)
565 {
566 if (vsie_page->gmap)
567 gmap_put(vsie_page->gmap);
568 WRITE_ONCE(vsie_page->gmap, NULL);
569 prefix_unmapped(vsie_page);
570 }
571
572 static int acquire_gmap_shadow(struct kvm_vcpu *vcpu,
573 struct vsie_page *vsie_page)
574 {
575 unsigned long asce;
576 union ctlreg0 cr0;
577 struct gmap *gmap;
578 int edat;
579
580 asce = vcpu->arch.sie_block->gcr[1];
581 cr0.val = vcpu->arch.sie_block->gcr[0];
582 edat = cr0.edat && test_kvm_facility(vcpu->kvm, 8);
583 edat += edat && test_kvm_facility(vcpu->kvm, 78);
584
585 /*
586 * ASCE or EDAT could have changed since last icpt, or the gmap
587 * we're holding has been unshadowed. If the gmap is still valid,
588 * we can safely reuse it.
589 */
590 if (vsie_page->gmap && gmap_shadow_valid(vsie_page->gmap, asce, edat))
591 return 0;
592
593 /* release the old shadow - if any, and mark the prefix as unmapped */
594 release_gmap_shadow(vsie_page);
595 gmap = gmap_shadow(vcpu->arch.gmap, asce, edat);
596 if (IS_ERR(gmap))
597 return PTR_ERR(gmap);
598 gmap->private = vcpu->kvm;
599 WRITE_ONCE(vsie_page->gmap, gmap);
600 return 0;
601 }
602
603 /*
604 * Run the vsie on a shadowed scb, managing the gmap shadow, handling
605 * prefix pages and faults.
606 *
607 * Returns: - 0 if no errors occurred
608 * - > 0 if control has to be given to guest 2
609 * - -ENOMEM if out of memory
610 */
611 static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
612 {
613 struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s;
614 int rc = 0;
615
616 while (1) {
617 rc = acquire_gmap_shadow(vcpu, vsie_page);
618 if (!rc)
619 rc = map_prefix(vcpu, vsie_page);
620 if (!rc) {
621 gmap_enable(vsie_page->gmap);
622 update_intervention_requests(vsie_page);
623 rc = do_vsie_run(vcpu, vsie_page);
624 gmap_enable(vcpu->arch.gmap);
625 }
626
627 if (rc == -EAGAIN)
628 rc = 0;
629 if (rc || scb_s->icptcode || signal_pending(current) ||
630 kvm_s390_vcpu_has_irq(vcpu, 0))
631 break;
632 };
633
634 if (rc == -EFAULT) {
635 /*
636 * Addressing exceptions are always presentes as intercepts.
637 * As addressing exceptions are suppressing and our guest 3 PSW
638 * points at the responsible instruction, we have to
639 * forward the PSW and set the ilc. If we can't read guest 3
640 * instruction, we can use an arbitrary ilc. Let's always use
641 * ilen = 4 for now, so we can avoid reading in guest 3 virtual
642 * memory. (we could also fake the shadow so the hardware
643 * handles it).
644 */
645 scb_s->icptcode = ICPT_PROGI;
646 scb_s->iprcc = PGM_ADDRESSING;
647 scb_s->pgmilc = 4;
648 scb_s->gpsw.addr = __rewind_psw(scb_s->gpsw, 4);
649 }
650 return rc;
651 }
652
653 /*
654 * Get or create a vsie page for a scb address.
655 *
656 * Returns: - address of a vsie page (cached or new one)
657 * - NULL if the same scb address is already used by another VCPU
658 * - ERR_PTR(-ENOMEM) if out of memory
659 */
660 static struct vsie_page *get_vsie_page(struct kvm *kvm, unsigned long addr)
661 {
662 struct vsie_page *vsie_page;
663 struct page *page;
664 int nr_vcpus;
665
666 rcu_read_lock();
667 page = radix_tree_lookup(&kvm->arch.vsie.addr_to_page, addr >> 9);
668 rcu_read_unlock();
669 if (page) {
670 if (page_ref_inc_return(page) == 2)
671 return page_to_virt(page);
672 page_ref_dec(page);
673 }
674
675 /*
676 * We want at least #online_vcpus shadows, so every VCPU can execute
677 * the VSIE in parallel.
678 */
679 nr_vcpus = atomic_read(&kvm->online_vcpus);
680
681 mutex_lock(&kvm->arch.vsie.mutex);
682 if (kvm->arch.vsie.page_count < nr_vcpus) {
683 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
684 if (!page) {
685 mutex_unlock(&kvm->arch.vsie.mutex);
686 return ERR_PTR(-ENOMEM);
687 }
688 page_ref_inc(page);
689 kvm->arch.vsie.pages[kvm->arch.vsie.page_count] = page;
690 kvm->arch.vsie.page_count++;
691 } else {
692 /* reuse an existing entry that belongs to nobody */
693 while (true) {
694 page = kvm->arch.vsie.pages[kvm->arch.vsie.next];
695 if (page_ref_inc_return(page) == 2)
696 break;
697 page_ref_dec(page);
698 kvm->arch.vsie.next++;
699 kvm->arch.vsie.next %= nr_vcpus;
700 }
701 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
702 }
703 page->index = addr;
704 /* double use of the same address */
705 if (radix_tree_insert(&kvm->arch.vsie.addr_to_page, addr >> 9, page)) {
706 page_ref_dec(page);
707 mutex_unlock(&kvm->arch.vsie.mutex);
708 return NULL;
709 }
710 mutex_unlock(&kvm->arch.vsie.mutex);
711
712 vsie_page = page_to_virt(page);
713 memset(&vsie_page->scb_s, 0, sizeof(struct kvm_s390_sie_block));
714 release_gmap_shadow(vsie_page);
715 vsie_page->scb_s.ihcpu = 0xffffU;
716 return vsie_page;
717 }
718
719 /* put a vsie page acquired via get_vsie_page */
720 static void put_vsie_page(struct kvm *kvm, struct vsie_page *vsie_page)
721 {
722 struct page *page = pfn_to_page(__pa(vsie_page) >> PAGE_SHIFT);
723
724 page_ref_dec(page);
725 }
726
727 int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)
728 {
729 struct vsie_page *vsie_page;
730 unsigned long scb_addr;
731 int rc;
732
733 vcpu->stat.instruction_sie++;
734 if (!test_kvm_cpu_feat(vcpu->kvm, KVM_S390_VM_CPU_FEAT_SIEF2))
735 return -EOPNOTSUPP;
736 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
737 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
738
739 BUILD_BUG_ON(sizeof(struct vsie_page) != 4096);
740 scb_addr = kvm_s390_get_base_disp_s(vcpu, NULL);
741
742 /* 512 byte alignment */
743 if (unlikely(scb_addr & 0x1ffUL))
744 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
745
746 if (signal_pending(current) || kvm_s390_vcpu_has_irq(vcpu, 0))
747 return 0;
748
749 vsie_page = get_vsie_page(vcpu->kvm, scb_addr);
750 if (IS_ERR(vsie_page))
751 return PTR_ERR(vsie_page);
752 else if (!vsie_page)
753 /* double use of sie control block - simply do nothing */
754 return 0;
755
756 rc = pin_scb(vcpu, vsie_page, scb_addr);
757 if (rc)
758 goto out_put;
759 rc = shadow_scb(vcpu, vsie_page);
760 if (rc)
761 goto out_unpin_scb;
762 rc = pin_blocks(vcpu, vsie_page);
763 if (rc)
764 goto out_unshadow;
765 rc = vsie_run(vcpu, vsie_page);
766 unpin_blocks(vcpu, vsie_page);
767 out_unshadow:
768 unshadow_scb(vcpu, vsie_page);
769 out_unpin_scb:
770 unpin_scb(vcpu, vsie_page, scb_addr);
771 out_put:
772 put_vsie_page(vcpu->kvm, vsie_page);
773
774 return rc < 0 ? rc : 0;
775 }
776
777 /* Init the vsie data structures. To be called when a vm is initialized. */
778 void kvm_s390_vsie_init(struct kvm *kvm)
779 {
780 mutex_init(&kvm->arch.vsie.mutex);
781 INIT_RADIX_TREE(&kvm->arch.vsie.addr_to_page, GFP_KERNEL);
782 }
783
784 /* Destroy the vsie data structures. To be called when a vm is destroyed. */
785 void kvm_s390_vsie_destroy(struct kvm *kvm)
786 {
787 struct vsie_page *vsie_page;
788 struct page *page;
789 int i;
790
791 mutex_lock(&kvm->arch.vsie.mutex);
792 for (i = 0; i < kvm->arch.vsie.page_count; i++) {
793 page = kvm->arch.vsie.pages[i];
794 kvm->arch.vsie.pages[i] = NULL;
795 vsie_page = page_to_virt(page);
796 release_gmap_shadow(vsie_page);
797 /* free the radix tree entry */
798 radix_tree_delete(&kvm->arch.vsie.addr_to_page, page->index >> 9);
799 __free_page(page);
800 }
801 kvm->arch.vsie.page_count = 0;
802 mutex_unlock(&kvm->arch.vsie.mutex);
803 }