xref: /linux/arch/s390/kvm/intercept.c (revision e38c884df92119d96f652d51f82661dd2fc0b885)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * in-kernel handling for sie intercepts
4  *
5  * Copyright IBM Corp. 2008, 2020
6  *
7  *    Author(s): Carsten Otte <cotte@de.ibm.com>
8  *               Christian Borntraeger <borntraeger@de.ibm.com>
9  */
10 
11 #include <linux/kvm_host.h>
12 #include <linux/errno.h>
13 #include <linux/pagemap.h>
14 
15 #include <asm/asm-offsets.h>
16 #include <asm/irq.h>
17 #include <asm/sysinfo.h>
18 #include <asm/uv.h>
19 
20 #include "kvm-s390.h"
21 #include "gaccess.h"
22 #include "trace.h"
23 #include "trace-s390.h"
24 #include "faultin.h"
25 
26 u8 kvm_s390_get_ilen(struct kvm_vcpu *vcpu)
27 {
28 	struct kvm_s390_sie_block *sie_block = vcpu->arch.sie_block;
29 	u8 ilen = 0;
30 
31 	switch (vcpu->arch.sie_block->icptcode) {
32 	case ICPT_INST:
33 	case ICPT_INSTPROGI:
34 	case ICPT_OPEREXC:
35 	case ICPT_PARTEXEC:
36 	case ICPT_IOINST:
37 		/* instruction only stored for these icptcodes */
38 		ilen = insn_length(vcpu->arch.sie_block->ipa >> 8);
39 		/* Use the length of the EXECUTE instruction if necessary */
40 		if (sie_block->icptstatus & 1) {
41 			ilen = (sie_block->icptstatus >> 4) & 0x6;
42 			if (!ilen)
43 				ilen = 4;
44 		}
45 		break;
46 	case ICPT_PROGI:
47 		/* bit 1+2 of pgmilc are the ilc, so we directly get ilen */
48 		ilen = vcpu->arch.sie_block->pgmilc & 0x6;
49 		break;
50 	}
51 	return ilen;
52 }
53 
54 static int handle_stop(struct kvm_vcpu *vcpu)
55 {
56 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
57 	int rc = 0;
58 	uint8_t flags, stop_pending;
59 
60 	vcpu->stat.exit_stop_request++;
61 
62 	/* delay the stop if any non-stop irq is pending */
63 	if (kvm_s390_vcpu_has_irq(vcpu, 1))
64 		return 0;
65 
66 	/* avoid races with the injection/SIGP STOP code */
67 	spin_lock(&li->lock);
68 	flags = li->irq.stop.flags;
69 	stop_pending = kvm_s390_is_stop_irq_pending(vcpu);
70 	spin_unlock(&li->lock);
71 
72 	trace_kvm_s390_stop_request(stop_pending, flags);
73 	if (!stop_pending)
74 		return 0;
75 
76 	if (flags & KVM_S390_STOP_FLAG_STORE_STATUS) {
77 		rc = kvm_s390_vcpu_store_status(vcpu,
78 						KVM_S390_STORE_STATUS_NOADDR);
79 		if (rc)
80 			return rc;
81 	}
82 
83 	/*
84 	 * no need to check the return value of vcpu_stop as it can only have
85 	 * an error for protvirt, but protvirt means user cpu state
86 	 */
87 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
88 		kvm_s390_vcpu_stop(vcpu);
89 	return -EOPNOTSUPP;
90 }
91 
92 static int handle_validity(struct kvm_vcpu *vcpu)
93 {
94 	int viwhy = vcpu->arch.sie_block->ipb >> 16;
95 
96 	vcpu->stat.exit_validity++;
97 	trace_kvm_s390_intercept_validity(vcpu, viwhy);
98 	KVM_EVENT(3, "validity intercept 0x%x for pid %u (kvm 0x%p)", viwhy,
99 		  current->pid, vcpu->kvm);
100 
101 	/* do not warn on invalid runtime instrumentation mode */
102 	WARN_ONCE(viwhy != 0x44, "kvm: unhandled validity intercept 0x%x\n",
103 		  viwhy);
104 	return -EINVAL;
105 }
106 
107 static int handle_instruction(struct kvm_vcpu *vcpu)
108 {
109 	vcpu->stat.exit_instruction++;
110 	trace_kvm_s390_intercept_instruction(vcpu,
111 					     vcpu->arch.sie_block->ipa,
112 					     vcpu->arch.sie_block->ipb);
113 
114 	switch (vcpu->arch.sie_block->ipa >> 8) {
115 	case 0x01:
116 		return kvm_s390_handle_01(vcpu);
117 	case 0x82:
118 		return kvm_s390_handle_lpsw(vcpu);
119 	case 0x83:
120 		return kvm_s390_handle_diag(vcpu);
121 	case 0xaa:
122 		return kvm_s390_handle_aa(vcpu);
123 	case 0xae:
124 		return kvm_s390_handle_sigp(vcpu);
125 	case 0xb2:
126 		return kvm_s390_handle_b2(vcpu);
127 	case 0xb6:
128 		return kvm_s390_handle_stctl(vcpu);
129 	case 0xb7:
130 		return kvm_s390_handle_lctl(vcpu);
131 	case 0xb9:
132 		return kvm_s390_handle_b9(vcpu);
133 	case 0xe3:
134 		return kvm_s390_handle_e3(vcpu);
135 	case 0xe5:
136 		return kvm_s390_handle_e5(vcpu);
137 	case 0xeb:
138 		return kvm_s390_handle_eb(vcpu);
139 	default:
140 		return -EOPNOTSUPP;
141 	}
142 }
143 
144 static int inject_prog_on_prog_intercept(struct kvm_vcpu *vcpu)
145 {
146 	struct kvm_s390_pgm_info pgm_info = {
147 		.code = vcpu->arch.sie_block->iprcc,
148 		/* the PSW has already been rewound */
149 		.flags = KVM_S390_PGM_FLAGS_NO_REWIND,
150 	};
151 
152 	switch (vcpu->arch.sie_block->iprcc & ~PGM_PER) {
153 	case PGM_AFX_TRANSLATION:
154 	case PGM_ASX_TRANSLATION:
155 	case PGM_EX_TRANSLATION:
156 	case PGM_LFX_TRANSLATION:
157 	case PGM_LSTE_SEQUENCE:
158 	case PGM_LSX_TRANSLATION:
159 	case PGM_LX_TRANSLATION:
160 	case PGM_PRIMARY_AUTHORITY:
161 	case PGM_SECONDARY_AUTHORITY:
162 	case PGM_SPACE_SWITCH:
163 		pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
164 		break;
165 	case PGM_ALEN_TRANSLATION:
166 	case PGM_ALE_SEQUENCE:
167 	case PGM_ASTE_INSTANCE:
168 	case PGM_ASTE_SEQUENCE:
169 	case PGM_ASTE_VALIDITY:
170 	case PGM_EXTENDED_AUTHORITY:
171 		pgm_info.exc_access_id = vcpu->arch.sie_block->eai;
172 		break;
173 	case PGM_ASCE_TYPE:
174 	case PGM_PAGE_TRANSLATION:
175 	case PGM_REGION_FIRST_TRANS:
176 	case PGM_REGION_SECOND_TRANS:
177 	case PGM_REGION_THIRD_TRANS:
178 	case PGM_SEGMENT_TRANSLATION:
179 		pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
180 		pgm_info.exc_access_id  = vcpu->arch.sie_block->eai;
181 		pgm_info.op_access_id  = vcpu->arch.sie_block->oai;
182 		break;
183 	case PGM_MONITOR:
184 		pgm_info.mon_class_nr = vcpu->arch.sie_block->mcn;
185 		pgm_info.mon_code = vcpu->arch.sie_block->tecmc;
186 		break;
187 	case PGM_VECTOR_PROCESSING:
188 	case PGM_DATA:
189 		pgm_info.data_exc_code = vcpu->arch.sie_block->dxc;
190 		break;
191 	case PGM_PROTECTION:
192 		pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
193 		pgm_info.exc_access_id  = vcpu->arch.sie_block->eai;
194 		break;
195 	default:
196 		break;
197 	}
198 
199 	if (vcpu->arch.sie_block->iprcc & PGM_PER) {
200 		pgm_info.per_code = vcpu->arch.sie_block->perc;
201 		pgm_info.per_atmid = vcpu->arch.sie_block->peratmid;
202 		pgm_info.per_address = vcpu->arch.sie_block->peraddr;
203 		pgm_info.per_access_id = vcpu->arch.sie_block->peraid;
204 	}
205 	return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
206 }
207 
208 /*
209  * restore ITDB to program-interruption TDB in guest lowcore
210  * and set TX abort indication if required
211 */
212 static int handle_itdb(struct kvm_vcpu *vcpu)
213 {
214 	struct kvm_s390_itdb *itdb;
215 	int rc;
216 
217 	if (!IS_TE_ENABLED(vcpu) || !IS_ITDB_VALID(vcpu))
218 		return 0;
219 	if (current->thread.per_flags & PER_FLAG_NO_TE)
220 		return 0;
221 	itdb = phys_to_virt(vcpu->arch.sie_block->itdba);
222 	rc = write_guest_lc(vcpu, __LC_PGM_TDB, itdb, sizeof(*itdb));
223 	if (rc)
224 		return rc;
225 	memset(itdb, 0, sizeof(*itdb));
226 
227 	return 0;
228 }
229 
230 #define per_event(vcpu) (vcpu->arch.sie_block->iprcc & PGM_PER)
231 
232 static bool should_handle_per_event(const struct kvm_vcpu *vcpu)
233 {
234 	if (!guestdbg_enabled(vcpu) || !per_event(vcpu))
235 		return false;
236 	if (guestdbg_sstep_enabled(vcpu) &&
237 	    vcpu->arch.sie_block->iprcc != PGM_PER) {
238 		/*
239 		 * __vcpu_run() will exit after delivering the concurrently
240 		 * indicated condition.
241 		 */
242 		return false;
243 	}
244 	return true;
245 }
246 
247 static int handle_prog(struct kvm_vcpu *vcpu)
248 {
249 	psw_t psw;
250 	int rc;
251 
252 	vcpu->stat.exit_program_interruption++;
253 
254 	/*
255 	 * Intercept 8 indicates a loop of specification exceptions
256 	 * for protected guests.
257 	 */
258 	if (kvm_s390_pv_cpu_is_protected(vcpu))
259 		return -EOPNOTSUPP;
260 
261 	if (should_handle_per_event(vcpu)) {
262 		rc = kvm_s390_handle_per_event(vcpu);
263 		if (rc)
264 			return rc;
265 		/* the interrupt might have been filtered out completely */
266 		if (vcpu->arch.sie_block->iprcc == 0)
267 			return 0;
268 	}
269 
270 	trace_kvm_s390_intercept_prog(vcpu, vcpu->arch.sie_block->iprcc);
271 	if (vcpu->arch.sie_block->iprcc == PGM_SPECIFICATION) {
272 		rc = read_guest_lc(vcpu, __LC_PGM_NEW_PSW, &psw, sizeof(psw_t));
273 		if (rc)
274 			return rc;
275 		/* Avoid endless loops of specification exceptions */
276 		if (!is_valid_psw(&psw))
277 			return -EOPNOTSUPP;
278 	}
279 	rc = handle_itdb(vcpu);
280 	if (rc)
281 		return rc;
282 
283 	return inject_prog_on_prog_intercept(vcpu);
284 }
285 
286 /**
287  * handle_external_interrupt - used for external interruption interceptions
288  * @vcpu: virtual cpu
289  *
290  * This interception occurs if:
291  * - the CPUSTAT_EXT_INT bit was already set when the external interrupt
292  *   occurred. In this case, the interrupt needs to be injected manually to
293  *   preserve interrupt priority.
294  * - the external new PSW has external interrupts enabled, which will cause an
295  *   interruption loop. We drop to userspace in this case.
296  *
297  * The latter case can be detected by inspecting the external mask bit in the
298  * external new psw.
299  *
300  * Under PV, only the latter case can occur, since interrupt priorities are
301  * handled in the ultravisor.
302  */
303 static int handle_external_interrupt(struct kvm_vcpu *vcpu)
304 {
305 	u16 eic = vcpu->arch.sie_block->eic;
306 	struct kvm_s390_irq irq;
307 	psw_t newpsw;
308 	int rc;
309 
310 	vcpu->stat.exit_external_interrupt++;
311 
312 	if (kvm_s390_pv_cpu_is_protected(vcpu)) {
313 		newpsw = vcpu->arch.sie_block->gpsw;
314 	} else {
315 		rc = read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &newpsw, sizeof(psw_t));
316 		if (rc)
317 			return rc;
318 	}
319 
320 	/*
321 	 * Clock comparator or timer interrupt with external interrupt enabled
322 	 * will cause interrupt loop. Drop to userspace.
323 	 */
324 	if ((eic == EXT_IRQ_CLK_COMP || eic == EXT_IRQ_CPU_TIMER) &&
325 	    (newpsw.mask & PSW_MASK_EXT))
326 		return -EOPNOTSUPP;
327 
328 	switch (eic) {
329 	case EXT_IRQ_CLK_COMP:
330 		irq.type = KVM_S390_INT_CLOCK_COMP;
331 		break;
332 	case EXT_IRQ_CPU_TIMER:
333 		irq.type = KVM_S390_INT_CPU_TIMER;
334 		break;
335 	case EXT_IRQ_EXTERNAL_CALL:
336 		irq.type = KVM_S390_INT_EXTERNAL_CALL;
337 		irq.u.extcall.code = vcpu->arch.sie_block->extcpuaddr;
338 		rc = kvm_s390_inject_vcpu(vcpu, &irq);
339 		/* ignore if another external call is already pending */
340 		if (rc == -EBUSY)
341 			return 0;
342 		return rc;
343 	default:
344 		return -EOPNOTSUPP;
345 	}
346 
347 	return kvm_s390_inject_vcpu(vcpu, &irq);
348 }
349 
350 /**
351  * handle_mvpg_pei - Handle MOVE PAGE partial execution interception.
352  * @vcpu: virtual cpu
353  *
354  * This interception can only happen for guests with DAT disabled and
355  * addresses that are currently not mapped in the host. Thus we try to
356  * set up the mappings for the corresponding user pages here (or throw
357  * addressing exceptions in case of illegal guest addresses).
358  */
359 static int handle_mvpg_pei(struct kvm_vcpu *vcpu)
360 {
361 	unsigned long srcaddr, dstaddr;
362 	int reg1, reg2, rc;
363 
364 	kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
365 
366 	/* Ensure that the source is paged-in, no actual access -> no key checking */
367 	rc = guest_translate_address_with_key(vcpu, vcpu->run->s.regs.gprs[reg2],
368 					      reg2, &srcaddr, GACC_FETCH, 0);
369 	if (rc)
370 		return kvm_s390_inject_prog_cond(vcpu, rc);
371 
372 	do {
373 		rc = kvm_s390_faultin_gfn_simple(vcpu, NULL, gpa_to_gfn(srcaddr), false);
374 	} while (rc == -EAGAIN);
375 	if (rc)
376 		return rc;
377 
378 	/* Ensure that the source is paged-in, no actual access -> no key checking */
379 	rc = guest_translate_address_with_key(vcpu, vcpu->run->s.regs.gprs[reg1],
380 					      reg1, &dstaddr, GACC_STORE, 0);
381 	if (rc)
382 		return kvm_s390_inject_prog_cond(vcpu, rc);
383 
384 	do {
385 		rc = kvm_s390_faultin_gfn_simple(vcpu, NULL, gpa_to_gfn(dstaddr), true);
386 	} while (rc == -EAGAIN);
387 	if (rc)
388 		return rc;
389 
390 	kvm_s390_retry_instr(vcpu);
391 
392 	return 0;
393 }
394 
395 static int handle_partial_execution(struct kvm_vcpu *vcpu)
396 {
397 	vcpu->stat.exit_pei++;
398 
399 	if (vcpu->arch.sie_block->ipa == 0xb254)	/* MVPG */
400 		return handle_mvpg_pei(vcpu);
401 	if (vcpu->arch.sie_block->ipa >> 8 == 0xae)	/* SIGP */
402 		return kvm_s390_handle_sigp_pei(vcpu);
403 
404 	return -EOPNOTSUPP;
405 }
406 
407 /*
408  * Handle the sthyi instruction that provides the guest with system
409  * information, like current CPU resources available at each level of
410  * the machine.
411  */
412 int handle_sthyi(struct kvm_vcpu *vcpu)
413 {
414 	int reg1, reg2, cc = 0, r = 0;
415 	u64 code, addr, rc = 0;
416 	struct sthyi_sctns *sctns = NULL;
417 
418 	if (!test_kvm_facility(vcpu->kvm, 74))
419 		return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
420 
421 	kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
422 	code = vcpu->run->s.regs.gprs[reg1];
423 	addr = vcpu->run->s.regs.gprs[reg2];
424 
425 	vcpu->stat.instruction_sthyi++;
426 	VCPU_EVENT(vcpu, 3, "STHYI: fc: %llu addr: 0x%016llx", code, addr);
427 	trace_kvm_s390_handle_sthyi(vcpu, code, addr);
428 
429 	if (reg1 == reg2 || reg1 & 1 || reg2 & 1)
430 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
431 
432 	if (code & 0xffff) {
433 		cc = 3;
434 		rc = 4;
435 		goto out;
436 	}
437 
438 	if (!kvm_s390_pv_cpu_is_protected(vcpu) && (addr & ~PAGE_MASK))
439 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
440 
441 	sctns = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
442 	if (!sctns)
443 		return -ENOMEM;
444 
445 	cc = sthyi_fill(sctns, &rc);
446 	if (cc < 0) {
447 		free_page((unsigned long)sctns);
448 		return cc;
449 	}
450 out:
451 	if (!cc) {
452 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
453 			memcpy(sida_addr(vcpu->arch.sie_block), sctns, PAGE_SIZE);
454 		} else {
455 			r = write_guest(vcpu, addr, reg2, sctns, PAGE_SIZE);
456 			if (r) {
457 				free_page((unsigned long)sctns);
458 				return kvm_s390_inject_prog_cond(vcpu, r);
459 			}
460 		}
461 	}
462 
463 	free_page((unsigned long)sctns);
464 	vcpu->run->s.regs.gprs[reg2 + 1] = rc;
465 	kvm_s390_set_psw_cc(vcpu, cc);
466 	return r;
467 }
468 
469 static int handle_operexc(struct kvm_vcpu *vcpu)
470 {
471 	psw_t oldpsw, newpsw;
472 	int rc;
473 
474 	vcpu->stat.exit_operation_exception++;
475 	trace_kvm_s390_handle_operexc(vcpu, vcpu->arch.sie_block->ipa,
476 				      vcpu->arch.sie_block->ipb);
477 
478 	if (vcpu->arch.sie_block->ipa == 0xb256)
479 		return handle_sthyi(vcpu);
480 
481 	if (vcpu->kvm->arch.user_operexec)
482 		return -EOPNOTSUPP;
483 
484 	if (vcpu->arch.sie_block->ipa == 0 && vcpu->kvm->arch.user_instr0)
485 		return -EOPNOTSUPP;
486 	rc = read_guest_lc(vcpu, __LC_PGM_NEW_PSW, &newpsw, sizeof(psw_t));
487 	if (rc)
488 		return rc;
489 	/*
490 	 * Avoid endless loops of operation exceptions, if the pgm new
491 	 * PSW will cause a new operation exception.
492 	 * The heuristic checks if the pgm new psw is within 6 bytes before
493 	 * the faulting psw address (with same DAT, AS settings) and the
494 	 * new psw is not a wait psw and the fault was not triggered by
495 	 * problem state.
496 	 */
497 	oldpsw = vcpu->arch.sie_block->gpsw;
498 	if (oldpsw.addr - newpsw.addr <= 6 &&
499 	    !(newpsw.mask & PSW_MASK_WAIT) &&
500 	    !(oldpsw.mask & PSW_MASK_PSTATE) &&
501 	    (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) &&
502 	    (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT))
503 		return -EOPNOTSUPP;
504 
505 	return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
506 }
507 
508 static int handle_pv_spx(struct kvm_vcpu *vcpu)
509 {
510 	u32 pref = *(u32 *)sida_addr(vcpu->arch.sie_block);
511 
512 	kvm_s390_set_prefix(vcpu, pref);
513 	trace_kvm_s390_handle_prefix(vcpu, 1, pref);
514 	return 0;
515 }
516 
517 static int handle_pv_sclp(struct kvm_vcpu *vcpu)
518 {
519 	struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
520 
521 	spin_lock(&fi->lock);
522 	/*
523 	 * 2 cases:
524 	 * a: an sccb answering interrupt was already pending or in flight.
525 	 *    As the sccb value is not known we can simply set some value to
526 	 *    trigger delivery of a saved SCCB. UV will then use its saved
527 	 *    copy of the SCCB value.
528 	 * b: an error SCCB interrupt needs to be injected so we also inject
529 	 *    a fake SCCB address. Firmware will use the proper one.
530 	 * This makes sure, that both errors and real sccb returns will only
531 	 * be delivered after a notification intercept (instruction has
532 	 * finished) but not after others.
533 	 */
534 	fi->srv_signal.ext_params |= 0x43000;
535 	set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
536 	clear_bit(IRQ_PEND_EXT_SERVICE, &fi->masked_irqs);
537 	spin_unlock(&fi->lock);
538 	return 0;
539 }
540 
541 static int handle_pv_uvc(struct kvm_vcpu *vcpu)
542 {
543 	struct uv_cb_share *guest_uvcb = sida_addr(vcpu->arch.sie_block);
544 	struct uv_cb_cts uvcb = {
545 		.header.cmd	= UVC_CMD_UNPIN_PAGE_SHARED,
546 		.header.len	= sizeof(uvcb),
547 		.guest_handle	= kvm_s390_pv_get_handle(vcpu->kvm),
548 		.gaddr		= guest_uvcb->paddr,
549 	};
550 	int rc;
551 
552 	if (guest_uvcb->header.cmd != UVC_CMD_REMOVE_SHARED_ACCESS) {
553 		WARN_ONCE(1, "Unexpected notification intercept for UVC 0x%x\n",
554 			  guest_uvcb->header.cmd);
555 		return 0;
556 	}
557 	rc = kvm_s390_pv_make_secure(vcpu->kvm, uvcb.gaddr, &uvcb);
558 	/*
559 	 * If the unpin did not succeed, the guest will exit again for the UVC
560 	 * and we will retry the unpin.
561 	 */
562 	if (rc == -EINVAL || rc == -ENXIO)
563 		return 0;
564 	/*
565 	 * If we got -EAGAIN here, we simply return it. It will eventually
566 	 * get propagated all the way to userspace, which should then try
567 	 * again.
568 	 */
569 	return rc;
570 }
571 
572 static int handle_pv_notification(struct kvm_vcpu *vcpu)
573 {
574 	int ret;
575 
576 	if (vcpu->arch.sie_block->ipa == 0xb210)
577 		return handle_pv_spx(vcpu);
578 	if (vcpu->arch.sie_block->ipa == 0xb220)
579 		return handle_pv_sclp(vcpu);
580 	if (vcpu->arch.sie_block->ipa == 0xb9a4)
581 		return handle_pv_uvc(vcpu);
582 	if (vcpu->arch.sie_block->ipa >> 8 == 0xae) {
583 		/*
584 		 * Besides external call, other SIGP orders also cause a
585 		 * 108 (pv notify) intercept. In contrast to external call,
586 		 * these orders need to be emulated and hence the appropriate
587 		 * place to handle them is in handle_instruction().
588 		 * So first try kvm_s390_handle_sigp_pei() and if that isn't
589 		 * successful, go on with handle_instruction().
590 		 */
591 		ret = kvm_s390_handle_sigp_pei(vcpu);
592 		if (!ret)
593 			return ret;
594 	}
595 
596 	return handle_instruction(vcpu);
597 }
598 
599 static bool should_handle_per_ifetch(const struct kvm_vcpu *vcpu, int rc)
600 {
601 	/* Process PER, also if the instruction is processed in user space. */
602 	if (!(vcpu->arch.sie_block->icptstatus & 0x02))
603 		return false;
604 	if (rc != 0 && rc != -EOPNOTSUPP)
605 		return false;
606 	if (guestdbg_sstep_enabled(vcpu) && vcpu->arch.local_int.pending_irqs)
607 		/* __vcpu_run() will exit after delivering the interrupt. */
608 		return false;
609 	return true;
610 }
611 
612 int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
613 {
614 	int rc, per_rc = 0;
615 
616 	if (kvm_is_ucontrol(vcpu->kvm))
617 		return -EOPNOTSUPP;
618 
619 	switch (vcpu->arch.sie_block->icptcode) {
620 	case ICPT_EXTREQ:
621 		vcpu->stat.exit_external_request++;
622 		return 0;
623 	case ICPT_IOREQ:
624 		vcpu->stat.exit_io_request++;
625 		return 0;
626 	case ICPT_INST:
627 		rc = handle_instruction(vcpu);
628 		break;
629 	case ICPT_PROGI:
630 		return handle_prog(vcpu);
631 	case ICPT_EXTINT:
632 		return handle_external_interrupt(vcpu);
633 	case ICPT_WAIT:
634 		return kvm_s390_handle_wait(vcpu);
635 	case ICPT_VALIDITY:
636 		return handle_validity(vcpu);
637 	case ICPT_STOP:
638 		return handle_stop(vcpu);
639 	case ICPT_OPEREXC:
640 		rc = handle_operexc(vcpu);
641 		break;
642 	case ICPT_PARTEXEC:
643 		rc = handle_partial_execution(vcpu);
644 		break;
645 	case ICPT_KSS:
646 		/* Instruction will be redriven, skip the PER check. */
647 		return kvm_s390_skey_check_enable(vcpu);
648 	case ICPT_MCHKREQ:
649 	case ICPT_INT_ENABLE:
650 		/*
651 		 * PSW bit 13 or a CR (0, 6, 14) changed and we might
652 		 * now be able to deliver interrupts. The pre-run code
653 		 * will take care of this.
654 		 */
655 		rc = 0;
656 		break;
657 	case ICPT_PV_INSTR:
658 		rc = handle_instruction(vcpu);
659 		break;
660 	case ICPT_PV_NOTIFY:
661 		rc = handle_pv_notification(vcpu);
662 		break;
663 	case ICPT_PV_PREF:
664 		rc = 0;
665 		kvm_s390_pv_convert_to_secure(vcpu->kvm, kvm_s390_get_prefix(vcpu));
666 		kvm_s390_pv_convert_to_secure(vcpu->kvm, kvm_s390_get_prefix(vcpu) + PAGE_SIZE);
667 		break;
668 	default:
669 		return -EOPNOTSUPP;
670 	}
671 
672 	if (should_handle_per_ifetch(vcpu, rc))
673 		per_rc = kvm_s390_handle_per_ifetch_icpt(vcpu);
674 	return per_rc ? per_rc : rc;
675 }
676