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