xref: /linux/arch/s390/kvm/intercept.c (revision e613d83454d7da1c37d78edb278db9c20afb21a2)
1d809aa23SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
28f2abe6aSChristian Borntraeger /*
3a53c8fabSHeiko Carstens  * in-kernel handling for sie intercepts
48f2abe6aSChristian Borntraeger  *
549710db0SJanosch Frank  * Copyright IBM Corp. 2008, 2020
68f2abe6aSChristian Borntraeger  *
78f2abe6aSChristian Borntraeger  *    Author(s): Carsten Otte <cotte@de.ibm.com>
88f2abe6aSChristian Borntraeger  *               Christian Borntraeger <borntraeger@de.ibm.com>
98f2abe6aSChristian Borntraeger  */
108f2abe6aSChristian Borntraeger 
118f2abe6aSChristian Borntraeger #include <linux/kvm_host.h>
128f2abe6aSChristian Borntraeger #include <linux/errno.h>
138f2abe6aSChristian Borntraeger #include <linux/pagemap.h>
148f2abe6aSChristian Borntraeger 
15a86dcc24SMichael Mueller #include <asm/asm-offsets.h>
16f14d82e0SThomas Huth #include <asm/irq.h>
17b7c92f1aSQingFeng Hao #include <asm/sysinfo.h>
1853227810SClaudio Imbrenda #include <asm/uv.h>
198f2abe6aSChristian Borntraeger 
208f2abe6aSChristian Borntraeger #include "kvm-s390.h"
21ba5c1e9bSCarsten Otte #include "gaccess.h"
225786fffaSCornelia Huck #include "trace.h"
23ade38c31SCornelia Huck #include "trace-s390.h"
24ba5c1e9bSCarsten Otte 
250e8bc06aSDavid Hildenbrand u8 kvm_s390_get_ilen(struct kvm_vcpu *vcpu)
2604b41acdSThomas Huth {
2704b41acdSThomas Huth 	struct kvm_s390_sie_block *sie_block = vcpu->arch.sie_block;
280e8bc06aSDavid Hildenbrand 	u8 ilen = 0;
2904b41acdSThomas Huth 
300e8bc06aSDavid Hildenbrand 	switch (vcpu->arch.sie_block->icptcode) {
310e8bc06aSDavid Hildenbrand 	case ICPT_INST:
320e8bc06aSDavid Hildenbrand 	case ICPT_INSTPROGI:
330e8bc06aSDavid Hildenbrand 	case ICPT_OPEREXC:
340e8bc06aSDavid Hildenbrand 	case ICPT_PARTEXEC:
350e8bc06aSDavid Hildenbrand 	case ICPT_IOINST:
360e8bc06aSDavid Hildenbrand 		/* instruction only stored for these icptcodes */
370e8bc06aSDavid Hildenbrand 		ilen = insn_length(vcpu->arch.sie_block->ipa >> 8);
3804b41acdSThomas Huth 		/* Use the length of the EXECUTE instruction if necessary */
3904b41acdSThomas Huth 		if (sie_block->icptstatus & 1) {
400e8bc06aSDavid Hildenbrand 			ilen = (sie_block->icptstatus >> 4) & 0x6;
410e8bc06aSDavid Hildenbrand 			if (!ilen)
420e8bc06aSDavid Hildenbrand 				ilen = 4;
4304b41acdSThomas Huth 		}
440e8bc06aSDavid Hildenbrand 		break;
450e8bc06aSDavid Hildenbrand 	case ICPT_PROGI:
460e8bc06aSDavid Hildenbrand 		/* bit 1+2 of pgmilc are the ilc, so we directly get ilen */
470e8bc06aSDavid Hildenbrand 		ilen = vcpu->arch.sie_block->pgmilc & 0x6;
480e8bc06aSDavid Hildenbrand 		break;
490e8bc06aSDavid Hildenbrand 	}
500e8bc06aSDavid Hildenbrand 	return ilen;
5104b41acdSThomas Huth }
5204b41acdSThomas Huth 
538f2abe6aSChristian Borntraeger static int handle_stop(struct kvm_vcpu *vcpu)
548f2abe6aSChristian Borntraeger {
556cddd432SDavid Hildenbrand 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
569ace903dSChristian Ehrhardt 	int rc = 0;
576cddd432SDavid Hildenbrand 	uint8_t flags, stop_pending;
585288fbf0SChristian Borntraeger 
598f2abe6aSChristian Borntraeger 	vcpu->stat.exit_stop_request++;
60ade38c31SCornelia Huck 
619a022067SDavid Hildenbrand 	/* delay the stop if any non-stop irq is pending */
629a022067SDavid Hildenbrand 	if (kvm_s390_vcpu_has_irq(vcpu, 1))
639a022067SDavid Hildenbrand 		return 0;
649a022067SDavid Hildenbrand 
656cddd432SDavid Hildenbrand 	/* avoid races with the injection/SIGP STOP code */
666cddd432SDavid Hildenbrand 	spin_lock(&li->lock);
676cddd432SDavid Hildenbrand 	flags = li->irq.stop.flags;
686cddd432SDavid Hildenbrand 	stop_pending = kvm_s390_is_stop_irq_pending(vcpu);
696cddd432SDavid Hildenbrand 	spin_unlock(&li->lock);
709ace903dSChristian Ehrhardt 
716cddd432SDavid Hildenbrand 	trace_kvm_s390_stop_request(stop_pending, flags);
726cddd432SDavid Hildenbrand 	if (!stop_pending)
7332f5ff63SDavid Hildenbrand 		return 0;
7432f5ff63SDavid Hildenbrand 
756cddd432SDavid Hildenbrand 	if (flags & KVM_S390_STOP_FLAG_STORE_STATUS) {
769e0d5473SJens Freimann 		rc = kvm_s390_vcpu_store_status(vcpu,
779e0d5473SJens Freimann 						KVM_S390_STORE_STATUS_NOADDR);
7832f5ff63SDavid Hildenbrand 		if (rc)
795288fbf0SChristian Borntraeger 			return rc;
808f2abe6aSChristian Borntraeger 	}
818f2abe6aSChristian Borntraeger 
82fe28c786SJanosch Frank 	/*
83fe28c786SJanosch Frank 	 * no need to check the return value of vcpu_stop as it can only have
84fe28c786SJanosch Frank 	 * an error for protvirt, but protvirt means user cpu state
85fe28c786SJanosch Frank 	 */
866352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
8732f5ff63SDavid Hildenbrand 		kvm_s390_vcpu_stop(vcpu);
8832f5ff63SDavid Hildenbrand 	return -EOPNOTSUPP;
8932f5ff63SDavid Hildenbrand }
9032f5ff63SDavid Hildenbrand 
918f2abe6aSChristian Borntraeger static int handle_validity(struct kvm_vcpu *vcpu)
928f2abe6aSChristian Borntraeger {
938f2abe6aSChristian Borntraeger 	int viwhy = vcpu->arch.sie_block->ipb >> 16;
943edbcff9SCarsten Otte 
958f2abe6aSChristian Borntraeger 	vcpu->stat.exit_validity++;
965786fffaSCornelia Huck 	trace_kvm_s390_intercept_validity(vcpu, viwhy);
97a5efb6b6SChristian Borntraeger 	KVM_EVENT(3, "validity intercept 0x%x for pid %u (kvm 0x%pK)", viwhy,
98a5efb6b6SChristian Borntraeger 		  current->pid, vcpu->kvm);
99a5efb6b6SChristian Borntraeger 
100a5efb6b6SChristian Borntraeger 	/* do not warn on invalid runtime instrumentation mode */
101a5efb6b6SChristian Borntraeger 	WARN_ONCE(viwhy != 0x44, "kvm: unhandled validity intercept 0x%x\n",
102a5efb6b6SChristian Borntraeger 		  viwhy);
103a5efb6b6SChristian Borntraeger 	return -EINVAL;
1048f2abe6aSChristian Borntraeger }
1058f2abe6aSChristian Borntraeger 
106ba5c1e9bSCarsten Otte static int handle_instruction(struct kvm_vcpu *vcpu)
107ba5c1e9bSCarsten Otte {
108ba5c1e9bSCarsten Otte 	vcpu->stat.exit_instruction++;
1095786fffaSCornelia Huck 	trace_kvm_s390_intercept_instruction(vcpu,
1105786fffaSCornelia Huck 					     vcpu->arch.sie_block->ipa,
1115786fffaSCornelia Huck 					     vcpu->arch.sie_block->ipb);
112cb7485daSChristian Borntraeger 
113cb7485daSChristian Borntraeger 	switch (vcpu->arch.sie_block->ipa >> 8) {
114cb7485daSChristian Borntraeger 	case 0x01:
115cb7485daSChristian Borntraeger 		return kvm_s390_handle_01(vcpu);
116cb7485daSChristian Borntraeger 	case 0x82:
117cb7485daSChristian Borntraeger 		return kvm_s390_handle_lpsw(vcpu);
118cb7485daSChristian Borntraeger 	case 0x83:
119cb7485daSChristian Borntraeger 		return kvm_s390_handle_diag(vcpu);
120cb7485daSChristian Borntraeger 	case 0xaa:
121cb7485daSChristian Borntraeger 		return kvm_s390_handle_aa(vcpu);
122cb7485daSChristian Borntraeger 	case 0xae:
123cb7485daSChristian Borntraeger 		return kvm_s390_handle_sigp(vcpu);
124cb7485daSChristian Borntraeger 	case 0xb2:
125cb7485daSChristian Borntraeger 		return kvm_s390_handle_b2(vcpu);
126cb7485daSChristian Borntraeger 	case 0xb6:
127cb7485daSChristian Borntraeger 		return kvm_s390_handle_stctl(vcpu);
128cb7485daSChristian Borntraeger 	case 0xb7:
129cb7485daSChristian Borntraeger 		return kvm_s390_handle_lctl(vcpu);
130cb7485daSChristian Borntraeger 	case 0xb9:
131cb7485daSChristian Borntraeger 		return kvm_s390_handle_b9(vcpu);
132cb7485daSChristian Borntraeger 	case 0xe3:
133cb7485daSChristian Borntraeger 		return kvm_s390_handle_e3(vcpu);
134cb7485daSChristian Borntraeger 	case 0xe5:
135cb7485daSChristian Borntraeger 		return kvm_s390_handle_e5(vcpu);
136cb7485daSChristian Borntraeger 	case 0xeb:
137cb7485daSChristian Borntraeger 		return kvm_s390_handle_eb(vcpu);
138cb7485daSChristian Borntraeger 	default:
139b8e660b8SHeiko Carstens 		return -EOPNOTSUPP;
140ba5c1e9bSCarsten Otte 	}
141cb7485daSChristian Borntraeger }
142ba5c1e9bSCarsten Otte 
143f6af84e7SDavid Hildenbrand static int inject_prog_on_prog_intercept(struct kvm_vcpu *vcpu)
144439716a5SDavid Hildenbrand {
145f6af84e7SDavid Hildenbrand 	struct kvm_s390_pgm_info pgm_info = {
146f6af84e7SDavid Hildenbrand 		.code = vcpu->arch.sie_block->iprcc,
147eaa4f416SDavid Hildenbrand 		/* the PSW has already been rewound */
148eaa4f416SDavid Hildenbrand 		.flags = KVM_S390_PGM_FLAGS_NO_REWIND,
149f6af84e7SDavid Hildenbrand 	};
150439716a5SDavid Hildenbrand 
151439716a5SDavid Hildenbrand 	switch (vcpu->arch.sie_block->iprcc & ~PGM_PER) {
152439716a5SDavid Hildenbrand 	case PGM_AFX_TRANSLATION:
153439716a5SDavid Hildenbrand 	case PGM_ASX_TRANSLATION:
154439716a5SDavid Hildenbrand 	case PGM_EX_TRANSLATION:
155439716a5SDavid Hildenbrand 	case PGM_LFX_TRANSLATION:
156439716a5SDavid Hildenbrand 	case PGM_LSTE_SEQUENCE:
157439716a5SDavid Hildenbrand 	case PGM_LSX_TRANSLATION:
158439716a5SDavid Hildenbrand 	case PGM_LX_TRANSLATION:
159439716a5SDavid Hildenbrand 	case PGM_PRIMARY_AUTHORITY:
160439716a5SDavid Hildenbrand 	case PGM_SECONDARY_AUTHORITY:
161439716a5SDavid Hildenbrand 	case PGM_SPACE_SWITCH:
162f6af84e7SDavid Hildenbrand 		pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
163439716a5SDavid Hildenbrand 		break;
164439716a5SDavid Hildenbrand 	case PGM_ALEN_TRANSLATION:
165439716a5SDavid Hildenbrand 	case PGM_ALE_SEQUENCE:
166439716a5SDavid Hildenbrand 	case PGM_ASTE_INSTANCE:
167439716a5SDavid Hildenbrand 	case PGM_ASTE_SEQUENCE:
168439716a5SDavid Hildenbrand 	case PGM_ASTE_VALIDITY:
169439716a5SDavid Hildenbrand 	case PGM_EXTENDED_AUTHORITY:
170f6af84e7SDavid Hildenbrand 		pgm_info.exc_access_id = vcpu->arch.sie_block->eai;
171439716a5SDavid Hildenbrand 		break;
172439716a5SDavid Hildenbrand 	case PGM_ASCE_TYPE:
173439716a5SDavid Hildenbrand 	case PGM_PAGE_TRANSLATION:
174439716a5SDavid Hildenbrand 	case PGM_REGION_FIRST_TRANS:
175439716a5SDavid Hildenbrand 	case PGM_REGION_SECOND_TRANS:
176439716a5SDavid Hildenbrand 	case PGM_REGION_THIRD_TRANS:
177439716a5SDavid Hildenbrand 	case PGM_SEGMENT_TRANSLATION:
178f6af84e7SDavid Hildenbrand 		pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
179f6af84e7SDavid Hildenbrand 		pgm_info.exc_access_id  = vcpu->arch.sie_block->eai;
180f6af84e7SDavid Hildenbrand 		pgm_info.op_access_id  = vcpu->arch.sie_block->oai;
181439716a5SDavid Hildenbrand 		break;
182439716a5SDavid Hildenbrand 	case PGM_MONITOR:
183f6af84e7SDavid Hildenbrand 		pgm_info.mon_class_nr = vcpu->arch.sie_block->mcn;
184f6af84e7SDavid Hildenbrand 		pgm_info.mon_code = vcpu->arch.sie_block->tecmc;
185439716a5SDavid Hildenbrand 		break;
186403c8648SEric Farman 	case PGM_VECTOR_PROCESSING:
187439716a5SDavid Hildenbrand 	case PGM_DATA:
188f6af84e7SDavid Hildenbrand 		pgm_info.data_exc_code = vcpu->arch.sie_block->dxc;
189439716a5SDavid Hildenbrand 		break;
190439716a5SDavid Hildenbrand 	case PGM_PROTECTION:
191f6af84e7SDavid Hildenbrand 		pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
192f6af84e7SDavid Hildenbrand 		pgm_info.exc_access_id  = vcpu->arch.sie_block->eai;
193439716a5SDavid Hildenbrand 		break;
194439716a5SDavid Hildenbrand 	default:
195439716a5SDavid Hildenbrand 		break;
196439716a5SDavid Hildenbrand 	}
197439716a5SDavid Hildenbrand 
198439716a5SDavid Hildenbrand 	if (vcpu->arch.sie_block->iprcc & PGM_PER) {
199f6af84e7SDavid Hildenbrand 		pgm_info.per_code = vcpu->arch.sie_block->perc;
200f6af84e7SDavid Hildenbrand 		pgm_info.per_atmid = vcpu->arch.sie_block->peratmid;
201f6af84e7SDavid Hildenbrand 		pgm_info.per_address = vcpu->arch.sie_block->peraddr;
202f6af84e7SDavid Hildenbrand 		pgm_info.per_access_id = vcpu->arch.sie_block->peraid;
203439716a5SDavid Hildenbrand 	}
204f6af84e7SDavid Hildenbrand 	return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
205439716a5SDavid Hildenbrand }
206439716a5SDavid Hildenbrand 
207e325fe69SMichael Mueller /*
208e325fe69SMichael Mueller  * restore ITDB to program-interruption TDB in guest lowcore
209e325fe69SMichael Mueller  * and set TX abort indication if required
210e325fe69SMichael Mueller */
211e325fe69SMichael Mueller static int handle_itdb(struct kvm_vcpu *vcpu)
212e325fe69SMichael Mueller {
213e325fe69SMichael Mueller 	struct kvm_s390_itdb *itdb;
214e325fe69SMichael Mueller 	int rc;
215e325fe69SMichael Mueller 
216e325fe69SMichael Mueller 	if (!IS_TE_ENABLED(vcpu) || !IS_ITDB_VALID(vcpu))
217e325fe69SMichael Mueller 		return 0;
218e325fe69SMichael Mueller 	if (current->thread.per_flags & PER_FLAG_NO_TE)
219e325fe69SMichael Mueller 		return 0;
220e325fe69SMichael Mueller 	itdb = (struct kvm_s390_itdb *)vcpu->arch.sie_block->itdba;
221e325fe69SMichael Mueller 	rc = write_guest_lc(vcpu, __LC_PGM_TDB, itdb, sizeof(*itdb));
222e325fe69SMichael Mueller 	if (rc)
223e325fe69SMichael Mueller 		return rc;
224e325fe69SMichael Mueller 	memset(itdb, 0, sizeof(*itdb));
225e325fe69SMichael Mueller 
226e325fe69SMichael Mueller 	return 0;
227e325fe69SMichael Mueller }
228e325fe69SMichael Mueller 
22927291e21SDavid Hildenbrand #define per_event(vcpu) (vcpu->arch.sie_block->iprcc & PGM_PER)
23027291e21SDavid Hildenbrand 
231ba5c1e9bSCarsten Otte static int handle_prog(struct kvm_vcpu *vcpu)
232ba5c1e9bSCarsten Otte {
233684135e0SThomas Huth 	psw_t psw;
2340040e7d2SHeiko Carstens 	int rc;
2350040e7d2SHeiko Carstens 
236ba5c1e9bSCarsten Otte 	vcpu->stat.exit_program_interruption++;
2377feb6bb8SMichael Mueller 
238e663df91SJanosch Frank 	/*
239e663df91SJanosch Frank 	 * Intercept 8 indicates a loop of specification exceptions
240e663df91SJanosch Frank 	 * for protected guests.
241e663df91SJanosch Frank 	 */
242e663df91SJanosch Frank 	if (kvm_s390_pv_cpu_is_protected(vcpu))
243e663df91SJanosch Frank 		return -EOPNOTSUPP;
244e663df91SJanosch Frank 
24527291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu) && per_event(vcpu)) {
246a69cbe81SDavid Hildenbrand 		rc = kvm_s390_handle_per_event(vcpu);
247a69cbe81SDavid Hildenbrand 		if (rc)
248a69cbe81SDavid Hildenbrand 			return rc;
24927291e21SDavid Hildenbrand 		/* the interrupt might have been filtered out completely */
25027291e21SDavid Hildenbrand 		if (vcpu->arch.sie_block->iprcc == 0)
25127291e21SDavid Hildenbrand 			return 0;
25227291e21SDavid Hildenbrand 	}
25327291e21SDavid Hildenbrand 
254e325fe69SMichael Mueller 	trace_kvm_s390_intercept_prog(vcpu, vcpu->arch.sie_block->iprcc);
255684135e0SThomas Huth 	if (vcpu->arch.sie_block->iprcc == PGM_SPECIFICATION) {
256684135e0SThomas Huth 		rc = read_guest_lc(vcpu, __LC_PGM_NEW_PSW, &psw, sizeof(psw_t));
257684135e0SThomas Huth 		if (rc)
258684135e0SThomas Huth 			return rc;
259684135e0SThomas Huth 		/* Avoid endless loops of specification exceptions */
260684135e0SThomas Huth 		if (!is_valid_psw(&psw))
261684135e0SThomas Huth 			return -EOPNOTSUPP;
262684135e0SThomas Huth 	}
263e325fe69SMichael Mueller 	rc = handle_itdb(vcpu);
2640040e7d2SHeiko Carstens 	if (rc)
2650040e7d2SHeiko Carstens 		return rc;
266439716a5SDavid Hildenbrand 
267f6af84e7SDavid Hildenbrand 	return inject_prog_on_prog_intercept(vcpu);
268ba5c1e9bSCarsten Otte }
269ba5c1e9bSCarsten Otte 
2709a558ee3SThomas Huth /**
271f14d82e0SThomas Huth  * handle_external_interrupt - used for external interruption interceptions
27225b5476aSJanosch Frank  * @vcpu: virtual cpu
273f14d82e0SThomas Huth  *
274f14d82e0SThomas Huth  * This interception only occurs if the CPUSTAT_EXT_INT bit was set, or if
275f14d82e0SThomas Huth  * the new PSW does not have external interrupts disabled. In the first case,
276f14d82e0SThomas Huth  * we've got to deliver the interrupt manually, and in the second case, we
277f14d82e0SThomas Huth  * drop to userspace to handle the situation there.
278f14d82e0SThomas Huth  */
279f14d82e0SThomas Huth static int handle_external_interrupt(struct kvm_vcpu *vcpu)
280f14d82e0SThomas Huth {
281f14d82e0SThomas Huth 	u16 eic = vcpu->arch.sie_block->eic;
282383d0b05SJens Freimann 	struct kvm_s390_irq irq;
283f14d82e0SThomas Huth 	psw_t newpsw;
284f14d82e0SThomas Huth 	int rc;
285f14d82e0SThomas Huth 
286f14d82e0SThomas Huth 	vcpu->stat.exit_external_interrupt++;
287f14d82e0SThomas Huth 
288f14d82e0SThomas Huth 	rc = read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &newpsw, sizeof(psw_t));
289f14d82e0SThomas Huth 	if (rc)
290f14d82e0SThomas Huth 		return rc;
291f14d82e0SThomas Huth 	/* We can not handle clock comparator or timer interrupt with bad PSW */
292f14d82e0SThomas Huth 	if ((eic == EXT_IRQ_CLK_COMP || eic == EXT_IRQ_CPU_TIMER) &&
293f14d82e0SThomas Huth 	    (newpsw.mask & PSW_MASK_EXT))
294f14d82e0SThomas Huth 		return -EOPNOTSUPP;
295f14d82e0SThomas Huth 
296f14d82e0SThomas Huth 	switch (eic) {
297f14d82e0SThomas Huth 	case EXT_IRQ_CLK_COMP:
298f14d82e0SThomas Huth 		irq.type = KVM_S390_INT_CLOCK_COMP;
299f14d82e0SThomas Huth 		break;
300f14d82e0SThomas Huth 	case EXT_IRQ_CPU_TIMER:
301f14d82e0SThomas Huth 		irq.type = KVM_S390_INT_CPU_TIMER;
302f14d82e0SThomas Huth 		break;
303f14d82e0SThomas Huth 	case EXT_IRQ_EXTERNAL_CALL:
304f14d82e0SThomas Huth 		irq.type = KVM_S390_INT_EXTERNAL_CALL;
305383d0b05SJens Freimann 		irq.u.extcall.code = vcpu->arch.sie_block->extcpuaddr;
306ea5f4969SDavid Hildenbrand 		rc = kvm_s390_inject_vcpu(vcpu, &irq);
307ea5f4969SDavid Hildenbrand 		/* ignore if another external call is already pending */
308ea5f4969SDavid Hildenbrand 		if (rc == -EBUSY)
309ea5f4969SDavid Hildenbrand 			return 0;
310ea5f4969SDavid Hildenbrand 		return rc;
311f14d82e0SThomas Huth 	default:
312f14d82e0SThomas Huth 		return -EOPNOTSUPP;
313f14d82e0SThomas Huth 	}
314f14d82e0SThomas Huth 
315f14d82e0SThomas Huth 	return kvm_s390_inject_vcpu(vcpu, &irq);
316f14d82e0SThomas Huth }
317f14d82e0SThomas Huth 
318f14d82e0SThomas Huth /**
31925b5476aSJanosch Frank  * handle_mvpg_pei - Handle MOVE PAGE partial execution interception.
32025b5476aSJanosch Frank  * @vcpu: virtual cpu
3219a558ee3SThomas Huth  *
3229a558ee3SThomas Huth  * This interception can only happen for guests with DAT disabled and
3239a558ee3SThomas Huth  * addresses that are currently not mapped in the host. Thus we try to
3249a558ee3SThomas Huth  * set up the mappings for the corresponding user pages here (or throw
3259a558ee3SThomas Huth  * addressing exceptions in case of illegal guest addresses).
3269a558ee3SThomas Huth  */
3279a558ee3SThomas Huth static int handle_mvpg_pei(struct kvm_vcpu *vcpu)
3289a558ee3SThomas Huth {
329f22166dcSThomas Huth 	unsigned long srcaddr, dstaddr;
3309a558ee3SThomas Huth 	int reg1, reg2, rc;
3319a558ee3SThomas Huth 
3329a558ee3SThomas Huth 	kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
3339a558ee3SThomas Huth 
334*e613d834SJanis Schoetterl-Glausch 	/* Ensure that the source is paged-in, no actual access -> no key checking */
335*e613d834SJanis Schoetterl-Glausch 	rc = guest_translate_address_with_key(vcpu, vcpu->run->s.regs.gprs[reg2],
336*e613d834SJanis Schoetterl-Glausch 					      reg2, &srcaddr, GACC_FETCH, 0);
3373cfad023SThomas Huth 	if (rc)
3383cfad023SThomas Huth 		return kvm_s390_inject_prog_cond(vcpu, rc);
339f22166dcSThomas Huth 	rc = kvm_arch_fault_in_page(vcpu, srcaddr, 0);
340f22166dcSThomas Huth 	if (rc != 0)
3419a558ee3SThomas Huth 		return rc;
3429a558ee3SThomas Huth 
343*e613d834SJanis Schoetterl-Glausch 	/* Ensure that the source is paged-in, no actual access -> no key checking */
344*e613d834SJanis Schoetterl-Glausch 	rc = guest_translate_address_with_key(vcpu, vcpu->run->s.regs.gprs[reg1],
345*e613d834SJanis Schoetterl-Glausch 					      reg1, &dstaddr, GACC_STORE, 0);
3463cfad023SThomas Huth 	if (rc)
3473cfad023SThomas Huth 		return kvm_s390_inject_prog_cond(vcpu, rc);
348f22166dcSThomas Huth 	rc = kvm_arch_fault_in_page(vcpu, dstaddr, 1);
349f22166dcSThomas Huth 	if (rc != 0)
3509a558ee3SThomas Huth 		return rc;
3519a558ee3SThomas Huth 
3520e8bc06aSDavid Hildenbrand 	kvm_s390_retry_instr(vcpu);
3539a558ee3SThomas Huth 
3549a558ee3SThomas Huth 	return 0;
3559a558ee3SThomas Huth }
3569a558ee3SThomas Huth 
3579a558ee3SThomas Huth static int handle_partial_execution(struct kvm_vcpu *vcpu)
3589a558ee3SThomas Huth {
3599ec6de19SAlexander Yarygin 	vcpu->stat.exit_pei++;
3609ec6de19SAlexander Yarygin 
3619a558ee3SThomas Huth 	if (vcpu->arch.sie_block->ipa == 0xb254)	/* MVPG */
3629a558ee3SThomas Huth 		return handle_mvpg_pei(vcpu);
3634953919fSDavid Hildenbrand 	if (vcpu->arch.sie_block->ipa >> 8 == 0xae)	/* SIGP */
3644953919fSDavid Hildenbrand 		return kvm_s390_handle_sigp_pei(vcpu);
3659a558ee3SThomas Huth 
3669a558ee3SThomas Huth 	return -EOPNOTSUPP;
3679a558ee3SThomas Huth }
3689a558ee3SThomas Huth 
369b7c92f1aSQingFeng Hao /*
370b7c92f1aSQingFeng Hao  * Handle the sthyi instruction that provides the guest with system
371b7c92f1aSQingFeng Hao  * information, like current CPU resources available at each level of
372b7c92f1aSQingFeng Hao  * the machine.
373b7c92f1aSQingFeng Hao  */
374b7c92f1aSQingFeng Hao int handle_sthyi(struct kvm_vcpu *vcpu)
375b7c92f1aSQingFeng Hao {
376b7c92f1aSQingFeng Hao 	int reg1, reg2, r = 0;
377b7c92f1aSQingFeng Hao 	u64 code, addr, cc = 0, rc = 0;
378b7c92f1aSQingFeng Hao 	struct sthyi_sctns *sctns = NULL;
379b7c92f1aSQingFeng Hao 
380b7c92f1aSQingFeng Hao 	if (!test_kvm_facility(vcpu->kvm, 74))
381b7c92f1aSQingFeng Hao 		return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
382b7c92f1aSQingFeng Hao 
383b7c92f1aSQingFeng Hao 	kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
384b7c92f1aSQingFeng Hao 	code = vcpu->run->s.regs.gprs[reg1];
385b7c92f1aSQingFeng Hao 	addr = vcpu->run->s.regs.gprs[reg2];
386b7c92f1aSQingFeng Hao 
387b7c92f1aSQingFeng Hao 	vcpu->stat.instruction_sthyi++;
388b7c92f1aSQingFeng Hao 	VCPU_EVENT(vcpu, 3, "STHYI: fc: %llu addr: 0x%016llx", code, addr);
389b7c92f1aSQingFeng Hao 	trace_kvm_s390_handle_sthyi(vcpu, code, addr);
390b7c92f1aSQingFeng Hao 
391b7c92f1aSQingFeng Hao 	if (reg1 == reg2 || reg1 & 1 || reg2 & 1)
392b7c92f1aSQingFeng Hao 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
393b7c92f1aSQingFeng Hao 
394b7c92f1aSQingFeng Hao 	if (code & 0xffff) {
395b7c92f1aSQingFeng Hao 		cc = 3;
396b7c92f1aSQingFeng Hao 		rc = 4;
397b7c92f1aSQingFeng Hao 		goto out;
398b7c92f1aSQingFeng Hao 	}
399b7c92f1aSQingFeng Hao 
40022d768c3SJanosch Frank 	if (!kvm_s390_pv_cpu_is_protected(vcpu) && (addr & ~PAGE_MASK))
401b7c92f1aSQingFeng Hao 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
402b7c92f1aSQingFeng Hao 
403c4196218SChristian Borntraeger 	sctns = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
404b7c92f1aSQingFeng Hao 	if (!sctns)
405b7c92f1aSQingFeng Hao 		return -ENOMEM;
406b7c92f1aSQingFeng Hao 
407b7c92f1aSQingFeng Hao 	cc = sthyi_fill(sctns, &rc);
408b7c92f1aSQingFeng Hao 
409b7c92f1aSQingFeng Hao out:
410b7c92f1aSQingFeng Hao 	if (!cc) {
41122d768c3SJanosch Frank 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
41222d768c3SJanosch Frank 			memcpy((void *)(sida_origin(vcpu->arch.sie_block)),
41322d768c3SJanosch Frank 			       sctns, PAGE_SIZE);
41422d768c3SJanosch Frank 		} else {
415b7c92f1aSQingFeng Hao 			r = write_guest(vcpu, addr, reg2, sctns, PAGE_SIZE);
416b7c92f1aSQingFeng Hao 			if (r) {
417b7c92f1aSQingFeng Hao 				free_page((unsigned long)sctns);
418b7c92f1aSQingFeng Hao 				return kvm_s390_inject_prog_cond(vcpu, r);
419b7c92f1aSQingFeng Hao 			}
420b7c92f1aSQingFeng Hao 		}
42122d768c3SJanosch Frank 	}
422b7c92f1aSQingFeng Hao 
423b7c92f1aSQingFeng Hao 	free_page((unsigned long)sctns);
424b7c92f1aSQingFeng Hao 	vcpu->run->s.regs.gprs[reg2 + 1] = rc;
425b7c92f1aSQingFeng Hao 	kvm_s390_set_psw_cc(vcpu, cc);
426b7c92f1aSQingFeng Hao 	return r;
427b7c92f1aSQingFeng Hao }
428b7c92f1aSQingFeng Hao 
429a011eeb2SJanosch Frank static int handle_operexc(struct kvm_vcpu *vcpu)
430a011eeb2SJanosch Frank {
431fb7dc1d4SChristian Borntraeger 	psw_t oldpsw, newpsw;
432fb7dc1d4SChristian Borntraeger 	int rc;
433fb7dc1d4SChristian Borntraeger 
434a011eeb2SJanosch Frank 	vcpu->stat.exit_operation_exception++;
435a011eeb2SJanosch Frank 	trace_kvm_s390_handle_operexc(vcpu, vcpu->arch.sie_block->ipa,
436a011eeb2SJanosch Frank 				      vcpu->arch.sie_block->ipb);
437a011eeb2SJanosch Frank 
438c0a6bfdcSChristian Borntraeger 	if (vcpu->arch.sie_block->ipa == 0xb256)
43995ca2cb5SJanosch Frank 		return handle_sthyi(vcpu);
44095ca2cb5SJanosch Frank 
4416502a34cSDavid Hildenbrand 	if (vcpu->arch.sie_block->ipa == 0 && vcpu->kvm->arch.user_instr0)
4426502a34cSDavid Hildenbrand 		return -EOPNOTSUPP;
443fb7dc1d4SChristian Borntraeger 	rc = read_guest_lc(vcpu, __LC_PGM_NEW_PSW, &newpsw, sizeof(psw_t));
444fb7dc1d4SChristian Borntraeger 	if (rc)
445fb7dc1d4SChristian Borntraeger 		return rc;
446fb7dc1d4SChristian Borntraeger 	/*
447fb7dc1d4SChristian Borntraeger 	 * Avoid endless loops of operation exceptions, if the pgm new
448fb7dc1d4SChristian Borntraeger 	 * PSW will cause a new operation exception.
449fb7dc1d4SChristian Borntraeger 	 * The heuristic checks if the pgm new psw is within 6 bytes before
450fb7dc1d4SChristian Borntraeger 	 * the faulting psw address (with same DAT, AS settings) and the
451fb7dc1d4SChristian Borntraeger 	 * new psw is not a wait psw and the fault was not triggered by
452fb7dc1d4SChristian Borntraeger 	 * problem state.
453fb7dc1d4SChristian Borntraeger 	 */
454fb7dc1d4SChristian Borntraeger 	oldpsw = vcpu->arch.sie_block->gpsw;
455fb7dc1d4SChristian Borntraeger 	if (oldpsw.addr - newpsw.addr <= 6 &&
456fb7dc1d4SChristian Borntraeger 	    !(newpsw.mask & PSW_MASK_WAIT) &&
457fb7dc1d4SChristian Borntraeger 	    !(oldpsw.mask & PSW_MASK_PSTATE) &&
458fb7dc1d4SChristian Borntraeger 	    (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) &&
459fb7dc1d4SChristian Borntraeger 	    (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT))
460fb7dc1d4SChristian Borntraeger 		return -EOPNOTSUPP;
4616502a34cSDavid Hildenbrand 
462a011eeb2SJanosch Frank 	return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
463a011eeb2SJanosch Frank }
464a011eeb2SJanosch Frank 
465d274995eSJanosch Frank static int handle_pv_spx(struct kvm_vcpu *vcpu)
466d274995eSJanosch Frank {
467d274995eSJanosch Frank 	u32 pref = *(u32 *)vcpu->arch.sie_block->sidad;
468d274995eSJanosch Frank 
469d274995eSJanosch Frank 	kvm_s390_set_prefix(vcpu, pref);
470d274995eSJanosch Frank 	trace_kvm_s390_handle_prefix(vcpu, 1, pref);
471d274995eSJanosch Frank 	return 0;
472d274995eSJanosch Frank }
473d274995eSJanosch Frank 
4740890ddeaSChristian Borntraeger static int handle_pv_sclp(struct kvm_vcpu *vcpu)
4750890ddeaSChristian Borntraeger {
4760890ddeaSChristian Borntraeger 	struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
4770890ddeaSChristian Borntraeger 
4780890ddeaSChristian Borntraeger 	spin_lock(&fi->lock);
4790890ddeaSChristian Borntraeger 	/*
4800890ddeaSChristian Borntraeger 	 * 2 cases:
4810890ddeaSChristian Borntraeger 	 * a: an sccb answering interrupt was already pending or in flight.
4820890ddeaSChristian Borntraeger 	 *    As the sccb value is not known we can simply set some value to
4830890ddeaSChristian Borntraeger 	 *    trigger delivery of a saved SCCB. UV will then use its saved
4840890ddeaSChristian Borntraeger 	 *    copy of the SCCB value.
4850890ddeaSChristian Borntraeger 	 * b: an error SCCB interrupt needs to be injected so we also inject
4860890ddeaSChristian Borntraeger 	 *    a fake SCCB address. Firmware will use the proper one.
4870890ddeaSChristian Borntraeger 	 * This makes sure, that both errors and real sccb returns will only
4880890ddeaSChristian Borntraeger 	 * be delivered after a notification intercept (instruction has
4890890ddeaSChristian Borntraeger 	 * finished) but not after others.
4900890ddeaSChristian Borntraeger 	 */
4910890ddeaSChristian Borntraeger 	fi->srv_signal.ext_params |= 0x43000;
4920890ddeaSChristian Borntraeger 	set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
4930890ddeaSChristian Borntraeger 	clear_bit(IRQ_PEND_EXT_SERVICE, &fi->masked_irqs);
4940890ddeaSChristian Borntraeger 	spin_unlock(&fi->lock);
4950890ddeaSChristian Borntraeger 	return 0;
4960890ddeaSChristian Borntraeger }
4970890ddeaSChristian Borntraeger 
49853227810SClaudio Imbrenda static int handle_pv_uvc(struct kvm_vcpu *vcpu)
49953227810SClaudio Imbrenda {
50053227810SClaudio Imbrenda 	struct uv_cb_share *guest_uvcb = (void *)vcpu->arch.sie_block->sidad;
50153227810SClaudio Imbrenda 	struct uv_cb_cts uvcb = {
50253227810SClaudio Imbrenda 		.header.cmd	= UVC_CMD_UNPIN_PAGE_SHARED,
50353227810SClaudio Imbrenda 		.header.len	= sizeof(uvcb),
50453227810SClaudio Imbrenda 		.guest_handle	= kvm_s390_pv_get_handle(vcpu->kvm),
50553227810SClaudio Imbrenda 		.gaddr		= guest_uvcb->paddr,
50653227810SClaudio Imbrenda 	};
50753227810SClaudio Imbrenda 	int rc;
50853227810SClaudio Imbrenda 
50953227810SClaudio Imbrenda 	if (guest_uvcb->header.cmd != UVC_CMD_REMOVE_SHARED_ACCESS) {
51053227810SClaudio Imbrenda 		WARN_ONCE(1, "Unexpected notification intercept for UVC 0x%x\n",
51153227810SClaudio Imbrenda 			  guest_uvcb->header.cmd);
51253227810SClaudio Imbrenda 		return 0;
51353227810SClaudio Imbrenda 	}
51453227810SClaudio Imbrenda 	rc = gmap_make_secure(vcpu->arch.gmap, uvcb.gaddr, &uvcb);
51553227810SClaudio Imbrenda 	/*
51653227810SClaudio Imbrenda 	 * If the unpin did not succeed, the guest will exit again for the UVC
51753227810SClaudio Imbrenda 	 * and we will retry the unpin.
51853227810SClaudio Imbrenda 	 */
51953227810SClaudio Imbrenda 	if (rc == -EINVAL)
52053227810SClaudio Imbrenda 		return 0;
521f0a1a061SClaudio Imbrenda 	/*
522f0a1a061SClaudio Imbrenda 	 * If we got -EAGAIN here, we simply return it. It will eventually
523f0a1a061SClaudio Imbrenda 	 * get propagated all the way to userspace, which should then try
524f0a1a061SClaudio Imbrenda 	 * again.
525f0a1a061SClaudio Imbrenda 	 */
52653227810SClaudio Imbrenda 	return rc;
52753227810SClaudio Imbrenda }
52853227810SClaudio Imbrenda 
529da24a0ccSJanosch Frank static int handle_pv_notification(struct kvm_vcpu *vcpu)
530da24a0ccSJanosch Frank {
531d274995eSJanosch Frank 	if (vcpu->arch.sie_block->ipa == 0xb210)
532d274995eSJanosch Frank 		return handle_pv_spx(vcpu);
5330890ddeaSChristian Borntraeger 	if (vcpu->arch.sie_block->ipa == 0xb220)
5340890ddeaSChristian Borntraeger 		return handle_pv_sclp(vcpu);
53553227810SClaudio Imbrenda 	if (vcpu->arch.sie_block->ipa == 0xb9a4)
53653227810SClaudio Imbrenda 		return handle_pv_uvc(vcpu);
5370890ddeaSChristian Borntraeger 
538da24a0ccSJanosch Frank 	return handle_instruction(vcpu);
539da24a0ccSJanosch Frank }
540da24a0ccSJanosch Frank 
5418f2abe6aSChristian Borntraeger int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
5428f2abe6aSChristian Borntraeger {
5435ffe466cSDavid Hildenbrand 	int rc, per_rc = 0;
5445ffe466cSDavid Hildenbrand 
54571f116bfSDavid Hildenbrand 	if (kvm_is_ucontrol(vcpu->kvm))
54671f116bfSDavid Hildenbrand 		return -EOPNOTSUPP;
54771f116bfSDavid Hildenbrand 
54846b708eaSChristian Borntraeger 	switch (vcpu->arch.sie_block->icptcode) {
549947b8972SFarhan Ali 	case ICPT_EXTREQ:
550a5e0aceaSChristian Borntraeger 		vcpu->stat.exit_external_request++;
551a5e0aceaSChristian Borntraeger 		return 0;
552947b8972SFarhan Ali 	case ICPT_IOREQ:
553a5e0aceaSChristian Borntraeger 		vcpu->stat.exit_io_request++;
554a5e0aceaSChristian Borntraeger 		return 0;
555947b8972SFarhan Ali 	case ICPT_INST:
5565ffe466cSDavid Hildenbrand 		rc = handle_instruction(vcpu);
5575ffe466cSDavid Hildenbrand 		break;
558947b8972SFarhan Ali 	case ICPT_PROGI:
55946b708eaSChristian Borntraeger 		return handle_prog(vcpu);
560947b8972SFarhan Ali 	case ICPT_EXTINT:
56146b708eaSChristian Borntraeger 		return handle_external_interrupt(vcpu);
562947b8972SFarhan Ali 	case ICPT_WAIT:
56346b708eaSChristian Borntraeger 		return kvm_s390_handle_wait(vcpu);
564947b8972SFarhan Ali 	case ICPT_VALIDITY:
56546b708eaSChristian Borntraeger 		return handle_validity(vcpu);
566947b8972SFarhan Ali 	case ICPT_STOP:
56746b708eaSChristian Borntraeger 		return handle_stop(vcpu);
568947b8972SFarhan Ali 	case ICPT_OPEREXC:
5695ffe466cSDavid Hildenbrand 		rc = handle_operexc(vcpu);
5705ffe466cSDavid Hildenbrand 		break;
571947b8972SFarhan Ali 	case ICPT_PARTEXEC:
5725ffe466cSDavid Hildenbrand 		rc = handle_partial_execution(vcpu);
5735ffe466cSDavid Hildenbrand 		break;
574730cd632SFarhan Ali 	case ICPT_KSS:
575730cd632SFarhan Ali 		rc = kvm_s390_skey_check_enable(vcpu);
576730cd632SFarhan Ali 		break;
57749710db0SJanosch Frank 	case ICPT_MCHKREQ:
57849710db0SJanosch Frank 	case ICPT_INT_ENABLE:
57949710db0SJanosch Frank 		/*
58049710db0SJanosch Frank 		 * PSW bit 13 or a CR (0, 6, 14) changed and we might
58149710db0SJanosch Frank 		 * now be able to deliver interrupts. The pre-run code
58249710db0SJanosch Frank 		 * will take care of this.
58349710db0SJanosch Frank 		 */
58449710db0SJanosch Frank 		rc = 0;
58549710db0SJanosch Frank 		break;
586da24a0ccSJanosch Frank 	case ICPT_PV_INSTR:
587da24a0ccSJanosch Frank 		rc = handle_instruction(vcpu);
588da24a0ccSJanosch Frank 		break;
589da24a0ccSJanosch Frank 	case ICPT_PV_NOTIFY:
590da24a0ccSJanosch Frank 		rc = handle_pv_notification(vcpu);
591da24a0ccSJanosch Frank 		break;
592d274995eSJanosch Frank 	case ICPT_PV_PREF:
593d274995eSJanosch Frank 		rc = 0;
594d274995eSJanosch Frank 		gmap_convert_to_secure(vcpu->arch.gmap,
595d274995eSJanosch Frank 				       kvm_s390_get_prefix(vcpu));
596d274995eSJanosch Frank 		gmap_convert_to_secure(vcpu->arch.gmap,
597d274995eSJanosch Frank 				       kvm_s390_get_prefix(vcpu) + PAGE_SIZE);
598d274995eSJanosch Frank 		break;
59946b708eaSChristian Borntraeger 	default:
600b8e660b8SHeiko Carstens 		return -EOPNOTSUPP;
60146b708eaSChristian Borntraeger 	}
6025ffe466cSDavid Hildenbrand 
6035ffe466cSDavid Hildenbrand 	/* process PER, also if the instrution is processed in user space */
6045ffe466cSDavid Hildenbrand 	if (vcpu->arch.sie_block->icptstatus & 0x02 &&
6055ffe466cSDavid Hildenbrand 	    (!rc || rc == -EOPNOTSUPP))
6065ffe466cSDavid Hildenbrand 		per_rc = kvm_s390_handle_per_ifetch_icpt(vcpu);
6075ffe466cSDavid Hildenbrand 	return per_rc ? per_rc : rc;
6088f2abe6aSChristian Borntraeger }
609