xref: /linux/arch/s390/kvm/intercept.c (revision 954a209f431c06b62718a49b403bd4c549f0d6fb)
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 "gmap.h"
25 
kvm_s390_get_ilen(struct kvm_vcpu * vcpu)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 
handle_stop(struct kvm_vcpu * vcpu)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 
handle_validity(struct kvm_vcpu * vcpu)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%pK)", 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 
handle_instruction(struct kvm_vcpu * vcpu)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 
inject_prog_on_prog_intercept(struct kvm_vcpu * vcpu)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 */
handle_itdb(struct kvm_vcpu * vcpu)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 
should_handle_per_event(const struct kvm_vcpu * vcpu)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 
handle_prog(struct kvm_vcpu * vcpu)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  */
handle_external_interrupt(struct kvm_vcpu * vcpu)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  */
handle_mvpg_pei(struct kvm_vcpu * vcpu)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 	rc = kvm_s390_handle_dat_fault(vcpu, srcaddr, 0);
372 	if (rc != 0)
373 		return rc;
374 
375 	/* Ensure that the source is paged-in, no actual access -> no key checking */
376 	rc = guest_translate_address_with_key(vcpu, vcpu->run->s.regs.gprs[reg1],
377 					      reg1, &dstaddr, GACC_STORE, 0);
378 	if (rc)
379 		return kvm_s390_inject_prog_cond(vcpu, rc);
380 	rc = kvm_s390_handle_dat_fault(vcpu, dstaddr, FOLL_WRITE);
381 	if (rc != 0)
382 		return rc;
383 
384 	kvm_s390_retry_instr(vcpu);
385 
386 	return 0;
387 }
388 
handle_partial_execution(struct kvm_vcpu * vcpu)389 static int handle_partial_execution(struct kvm_vcpu *vcpu)
390 {
391 	vcpu->stat.exit_pei++;
392 
393 	if (vcpu->arch.sie_block->ipa == 0xb254)	/* MVPG */
394 		return handle_mvpg_pei(vcpu);
395 	if (vcpu->arch.sie_block->ipa >> 8 == 0xae)	/* SIGP */
396 		return kvm_s390_handle_sigp_pei(vcpu);
397 
398 	return -EOPNOTSUPP;
399 }
400 
401 /*
402  * Handle the sthyi instruction that provides the guest with system
403  * information, like current CPU resources available at each level of
404  * the machine.
405  */
handle_sthyi(struct kvm_vcpu * vcpu)406 int handle_sthyi(struct kvm_vcpu *vcpu)
407 {
408 	int reg1, reg2, cc = 0, r = 0;
409 	u64 code, addr, rc = 0;
410 	struct sthyi_sctns *sctns = NULL;
411 
412 	if (!test_kvm_facility(vcpu->kvm, 74))
413 		return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
414 
415 	kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
416 	code = vcpu->run->s.regs.gprs[reg1];
417 	addr = vcpu->run->s.regs.gprs[reg2];
418 
419 	vcpu->stat.instruction_sthyi++;
420 	VCPU_EVENT(vcpu, 3, "STHYI: fc: %llu addr: 0x%016llx", code, addr);
421 	trace_kvm_s390_handle_sthyi(vcpu, code, addr);
422 
423 	if (reg1 == reg2 || reg1 & 1 || reg2 & 1)
424 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
425 
426 	if (code & 0xffff) {
427 		cc = 3;
428 		rc = 4;
429 		goto out;
430 	}
431 
432 	if (!kvm_s390_pv_cpu_is_protected(vcpu) && (addr & ~PAGE_MASK))
433 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
434 
435 	sctns = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
436 	if (!sctns)
437 		return -ENOMEM;
438 
439 	cc = sthyi_fill(sctns, &rc);
440 	if (cc < 0) {
441 		free_page((unsigned long)sctns);
442 		return cc;
443 	}
444 out:
445 	if (!cc) {
446 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
447 			memcpy(sida_addr(vcpu->arch.sie_block), sctns, PAGE_SIZE);
448 		} else {
449 			r = write_guest(vcpu, addr, reg2, sctns, PAGE_SIZE);
450 			if (r) {
451 				free_page((unsigned long)sctns);
452 				return kvm_s390_inject_prog_cond(vcpu, r);
453 			}
454 		}
455 	}
456 
457 	free_page((unsigned long)sctns);
458 	vcpu->run->s.regs.gprs[reg2 + 1] = rc;
459 	kvm_s390_set_psw_cc(vcpu, cc);
460 	return r;
461 }
462 
handle_operexc(struct kvm_vcpu * vcpu)463 static int handle_operexc(struct kvm_vcpu *vcpu)
464 {
465 	psw_t oldpsw, newpsw;
466 	int rc;
467 
468 	vcpu->stat.exit_operation_exception++;
469 	trace_kvm_s390_handle_operexc(vcpu, vcpu->arch.sie_block->ipa,
470 				      vcpu->arch.sie_block->ipb);
471 
472 	if (vcpu->arch.sie_block->ipa == 0xb256)
473 		return handle_sthyi(vcpu);
474 
475 	if (vcpu->arch.sie_block->ipa == 0 && vcpu->kvm->arch.user_instr0)
476 		return -EOPNOTSUPP;
477 	rc = read_guest_lc(vcpu, __LC_PGM_NEW_PSW, &newpsw, sizeof(psw_t));
478 	if (rc)
479 		return rc;
480 	/*
481 	 * Avoid endless loops of operation exceptions, if the pgm new
482 	 * PSW will cause a new operation exception.
483 	 * The heuristic checks if the pgm new psw is within 6 bytes before
484 	 * the faulting psw address (with same DAT, AS settings) and the
485 	 * new psw is not a wait psw and the fault was not triggered by
486 	 * problem state.
487 	 */
488 	oldpsw = vcpu->arch.sie_block->gpsw;
489 	if (oldpsw.addr - newpsw.addr <= 6 &&
490 	    !(newpsw.mask & PSW_MASK_WAIT) &&
491 	    !(oldpsw.mask & PSW_MASK_PSTATE) &&
492 	    (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) &&
493 	    (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT))
494 		return -EOPNOTSUPP;
495 
496 	return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
497 }
498 
handle_pv_spx(struct kvm_vcpu * vcpu)499 static int handle_pv_spx(struct kvm_vcpu *vcpu)
500 {
501 	u32 pref = *(u32 *)sida_addr(vcpu->arch.sie_block);
502 
503 	kvm_s390_set_prefix(vcpu, pref);
504 	trace_kvm_s390_handle_prefix(vcpu, 1, pref);
505 	return 0;
506 }
507 
handle_pv_sclp(struct kvm_vcpu * vcpu)508 static int handle_pv_sclp(struct kvm_vcpu *vcpu)
509 {
510 	struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
511 
512 	spin_lock(&fi->lock);
513 	/*
514 	 * 2 cases:
515 	 * a: an sccb answering interrupt was already pending or in flight.
516 	 *    As the sccb value is not known we can simply set some value to
517 	 *    trigger delivery of a saved SCCB. UV will then use its saved
518 	 *    copy of the SCCB value.
519 	 * b: an error SCCB interrupt needs to be injected so we also inject
520 	 *    a fake SCCB address. Firmware will use the proper one.
521 	 * This makes sure, that both errors and real sccb returns will only
522 	 * be delivered after a notification intercept (instruction has
523 	 * finished) but not after others.
524 	 */
525 	fi->srv_signal.ext_params |= 0x43000;
526 	set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
527 	clear_bit(IRQ_PEND_EXT_SERVICE, &fi->masked_irqs);
528 	spin_unlock(&fi->lock);
529 	return 0;
530 }
531 
handle_pv_uvc(struct kvm_vcpu * vcpu)532 static int handle_pv_uvc(struct kvm_vcpu *vcpu)
533 {
534 	struct uv_cb_share *guest_uvcb = sida_addr(vcpu->arch.sie_block);
535 	struct uv_cb_cts uvcb = {
536 		.header.cmd	= UVC_CMD_UNPIN_PAGE_SHARED,
537 		.header.len	= sizeof(uvcb),
538 		.guest_handle	= kvm_s390_pv_get_handle(vcpu->kvm),
539 		.gaddr		= guest_uvcb->paddr,
540 	};
541 	int rc;
542 
543 	if (guest_uvcb->header.cmd != UVC_CMD_REMOVE_SHARED_ACCESS) {
544 		WARN_ONCE(1, "Unexpected notification intercept for UVC 0x%x\n",
545 			  guest_uvcb->header.cmd);
546 		return 0;
547 	}
548 	rc = gmap_make_secure(vcpu->arch.gmap, uvcb.gaddr, &uvcb);
549 	/*
550 	 * If the unpin did not succeed, the guest will exit again for the UVC
551 	 * and we will retry the unpin.
552 	 */
553 	if (rc == -EINVAL || rc == -ENXIO)
554 		return 0;
555 	/*
556 	 * If we got -EAGAIN here, we simply return it. It will eventually
557 	 * get propagated all the way to userspace, which should then try
558 	 * again.
559 	 */
560 	return rc;
561 }
562 
handle_pv_notification(struct kvm_vcpu * vcpu)563 static int handle_pv_notification(struct kvm_vcpu *vcpu)
564 {
565 	int ret;
566 
567 	if (vcpu->arch.sie_block->ipa == 0xb210)
568 		return handle_pv_spx(vcpu);
569 	if (vcpu->arch.sie_block->ipa == 0xb220)
570 		return handle_pv_sclp(vcpu);
571 	if (vcpu->arch.sie_block->ipa == 0xb9a4)
572 		return handle_pv_uvc(vcpu);
573 	if (vcpu->arch.sie_block->ipa >> 8 == 0xae) {
574 		/*
575 		 * Besides external call, other SIGP orders also cause a
576 		 * 108 (pv notify) intercept. In contrast to external call,
577 		 * these orders need to be emulated and hence the appropriate
578 		 * place to handle them is in handle_instruction().
579 		 * So first try kvm_s390_handle_sigp_pei() and if that isn't
580 		 * successful, go on with handle_instruction().
581 		 */
582 		ret = kvm_s390_handle_sigp_pei(vcpu);
583 		if (!ret)
584 			return ret;
585 	}
586 
587 	return handle_instruction(vcpu);
588 }
589 
should_handle_per_ifetch(const struct kvm_vcpu * vcpu,int rc)590 static bool should_handle_per_ifetch(const struct kvm_vcpu *vcpu, int rc)
591 {
592 	/* Process PER, also if the instruction is processed in user space. */
593 	if (!(vcpu->arch.sie_block->icptstatus & 0x02))
594 		return false;
595 	if (rc != 0 && rc != -EOPNOTSUPP)
596 		return false;
597 	if (guestdbg_sstep_enabled(vcpu) && vcpu->arch.local_int.pending_irqs)
598 		/* __vcpu_run() will exit after delivering the interrupt. */
599 		return false;
600 	return true;
601 }
602 
kvm_handle_sie_intercept(struct kvm_vcpu * vcpu)603 int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
604 {
605 	int rc, per_rc = 0;
606 
607 	if (kvm_is_ucontrol(vcpu->kvm))
608 		return -EOPNOTSUPP;
609 
610 	switch (vcpu->arch.sie_block->icptcode) {
611 	case ICPT_EXTREQ:
612 		vcpu->stat.exit_external_request++;
613 		return 0;
614 	case ICPT_IOREQ:
615 		vcpu->stat.exit_io_request++;
616 		return 0;
617 	case ICPT_INST:
618 		rc = handle_instruction(vcpu);
619 		break;
620 	case ICPT_PROGI:
621 		return handle_prog(vcpu);
622 	case ICPT_EXTINT:
623 		return handle_external_interrupt(vcpu);
624 	case ICPT_WAIT:
625 		return kvm_s390_handle_wait(vcpu);
626 	case ICPT_VALIDITY:
627 		return handle_validity(vcpu);
628 	case ICPT_STOP:
629 		return handle_stop(vcpu);
630 	case ICPT_OPEREXC:
631 		rc = handle_operexc(vcpu);
632 		break;
633 	case ICPT_PARTEXEC:
634 		rc = handle_partial_execution(vcpu);
635 		break;
636 	case ICPT_KSS:
637 		/* Instruction will be redriven, skip the PER check. */
638 		return kvm_s390_skey_check_enable(vcpu);
639 	case ICPT_MCHKREQ:
640 	case ICPT_INT_ENABLE:
641 		/*
642 		 * PSW bit 13 or a CR (0, 6, 14) changed and we might
643 		 * now be able to deliver interrupts. The pre-run code
644 		 * will take care of this.
645 		 */
646 		rc = 0;
647 		break;
648 	case ICPT_PV_INSTR:
649 		rc = handle_instruction(vcpu);
650 		break;
651 	case ICPT_PV_NOTIFY:
652 		rc = handle_pv_notification(vcpu);
653 		break;
654 	case ICPT_PV_PREF:
655 		rc = 0;
656 		gmap_convert_to_secure(vcpu->arch.gmap,
657 				       kvm_s390_get_prefix(vcpu));
658 		gmap_convert_to_secure(vcpu->arch.gmap,
659 				       kvm_s390_get_prefix(vcpu) + PAGE_SIZE);
660 		break;
661 	default:
662 		return -EOPNOTSUPP;
663 	}
664 
665 	if (should_handle_per_ifetch(vcpu, rc))
666 		per_rc = kvm_s390_handle_per_ifetch_icpt(vcpu);
667 	return per_rc ? per_rc : rc;
668 }
669