xref: /linux/arch/s390/kvm/intercept.c (revision 22d768c3e960f10707017dfbee54e472f2c6a778)
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 
158f2abe6aSChristian Borntraeger #include <asm/kvm_host.h>
16a86dcc24SMichael Mueller #include <asm/asm-offsets.h>
17f14d82e0SThomas Huth #include <asm/irq.h>
18b7c92f1aSQingFeng Hao #include <asm/sysinfo.h>
1953227810SClaudio Imbrenda #include <asm/uv.h>
208f2abe6aSChristian Borntraeger 
218f2abe6aSChristian Borntraeger #include "kvm-s390.h"
22ba5c1e9bSCarsten Otte #include "gaccess.h"
235786fffaSCornelia Huck #include "trace.h"
24ade38c31SCornelia Huck #include "trace-s390.h"
25ba5c1e9bSCarsten Otte 
260e8bc06aSDavid Hildenbrand u8 kvm_s390_get_ilen(struct kvm_vcpu *vcpu)
2704b41acdSThomas Huth {
2804b41acdSThomas Huth 	struct kvm_s390_sie_block *sie_block = vcpu->arch.sie_block;
290e8bc06aSDavid Hildenbrand 	u8 ilen = 0;
3004b41acdSThomas Huth 
310e8bc06aSDavid Hildenbrand 	switch (vcpu->arch.sie_block->icptcode) {
320e8bc06aSDavid Hildenbrand 	case ICPT_INST:
330e8bc06aSDavid Hildenbrand 	case ICPT_INSTPROGI:
340e8bc06aSDavid Hildenbrand 	case ICPT_OPEREXC:
350e8bc06aSDavid Hildenbrand 	case ICPT_PARTEXEC:
360e8bc06aSDavid Hildenbrand 	case ICPT_IOINST:
370e8bc06aSDavid Hildenbrand 		/* instruction only stored for these icptcodes */
380e8bc06aSDavid Hildenbrand 		ilen = insn_length(vcpu->arch.sie_block->ipa >> 8);
3904b41acdSThomas Huth 		/* Use the length of the EXECUTE instruction if necessary */
4004b41acdSThomas Huth 		if (sie_block->icptstatus & 1) {
410e8bc06aSDavid Hildenbrand 			ilen = (sie_block->icptstatus >> 4) & 0x6;
420e8bc06aSDavid Hildenbrand 			if (!ilen)
430e8bc06aSDavid Hildenbrand 				ilen = 4;
4404b41acdSThomas Huth 		}
450e8bc06aSDavid Hildenbrand 		break;
460e8bc06aSDavid Hildenbrand 	case ICPT_PROGI:
470e8bc06aSDavid Hildenbrand 		/* bit 1+2 of pgmilc are the ilc, so we directly get ilen */
480e8bc06aSDavid Hildenbrand 		ilen = vcpu->arch.sie_block->pgmilc & 0x6;
490e8bc06aSDavid Hildenbrand 		break;
500e8bc06aSDavid Hildenbrand 	}
510e8bc06aSDavid Hildenbrand 	return ilen;
5204b41acdSThomas Huth }
5304b41acdSThomas Huth 
548f2abe6aSChristian Borntraeger static int handle_stop(struct kvm_vcpu *vcpu)
558f2abe6aSChristian Borntraeger {
566cddd432SDavid Hildenbrand 	struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
579ace903dSChristian Ehrhardt 	int rc = 0;
586cddd432SDavid Hildenbrand 	uint8_t flags, stop_pending;
595288fbf0SChristian Borntraeger 
608f2abe6aSChristian Borntraeger 	vcpu->stat.exit_stop_request++;
61ade38c31SCornelia Huck 
629a022067SDavid Hildenbrand 	/* delay the stop if any non-stop irq is pending */
639a022067SDavid Hildenbrand 	if (kvm_s390_vcpu_has_irq(vcpu, 1))
649a022067SDavid Hildenbrand 		return 0;
659a022067SDavid Hildenbrand 
666cddd432SDavid Hildenbrand 	/* avoid races with the injection/SIGP STOP code */
676cddd432SDavid Hildenbrand 	spin_lock(&li->lock);
686cddd432SDavid Hildenbrand 	flags = li->irq.stop.flags;
696cddd432SDavid Hildenbrand 	stop_pending = kvm_s390_is_stop_irq_pending(vcpu);
706cddd432SDavid Hildenbrand 	spin_unlock(&li->lock);
719ace903dSChristian Ehrhardt 
726cddd432SDavid Hildenbrand 	trace_kvm_s390_stop_request(stop_pending, flags);
736cddd432SDavid Hildenbrand 	if (!stop_pending)
7432f5ff63SDavid Hildenbrand 		return 0;
7532f5ff63SDavid Hildenbrand 
766cddd432SDavid Hildenbrand 	if (flags & KVM_S390_STOP_FLAG_STORE_STATUS) {
779e0d5473SJens Freimann 		rc = kvm_s390_vcpu_store_status(vcpu,
789e0d5473SJens Freimann 						KVM_S390_STORE_STATUS_NOADDR);
7932f5ff63SDavid Hildenbrand 		if (rc)
805288fbf0SChristian Borntraeger 			return rc;
818f2abe6aSChristian Borntraeger 	}
828f2abe6aSChristian Borntraeger 
836352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
8432f5ff63SDavid Hildenbrand 		kvm_s390_vcpu_stop(vcpu);
8532f5ff63SDavid Hildenbrand 	return -EOPNOTSUPP;
8632f5ff63SDavid Hildenbrand }
8732f5ff63SDavid Hildenbrand 
888f2abe6aSChristian Borntraeger static int handle_validity(struct kvm_vcpu *vcpu)
898f2abe6aSChristian Borntraeger {
908f2abe6aSChristian Borntraeger 	int viwhy = vcpu->arch.sie_block->ipb >> 16;
913edbcff9SCarsten Otte 
928f2abe6aSChristian Borntraeger 	vcpu->stat.exit_validity++;
935786fffaSCornelia Huck 	trace_kvm_s390_intercept_validity(vcpu, viwhy);
94a5efb6b6SChristian Borntraeger 	KVM_EVENT(3, "validity intercept 0x%x for pid %u (kvm 0x%pK)", viwhy,
95a5efb6b6SChristian Borntraeger 		  current->pid, vcpu->kvm);
96a5efb6b6SChristian Borntraeger 
97a5efb6b6SChristian Borntraeger 	/* do not warn on invalid runtime instrumentation mode */
98a5efb6b6SChristian Borntraeger 	WARN_ONCE(viwhy != 0x44, "kvm: unhandled validity intercept 0x%x\n",
99a5efb6b6SChristian Borntraeger 		  viwhy);
100a5efb6b6SChristian Borntraeger 	return -EINVAL;
1018f2abe6aSChristian Borntraeger }
1028f2abe6aSChristian Borntraeger 
103ba5c1e9bSCarsten Otte static int handle_instruction(struct kvm_vcpu *vcpu)
104ba5c1e9bSCarsten Otte {
105ba5c1e9bSCarsten Otte 	vcpu->stat.exit_instruction++;
1065786fffaSCornelia Huck 	trace_kvm_s390_intercept_instruction(vcpu,
1075786fffaSCornelia Huck 					     vcpu->arch.sie_block->ipa,
1085786fffaSCornelia Huck 					     vcpu->arch.sie_block->ipb);
109cb7485daSChristian Borntraeger 
110cb7485daSChristian Borntraeger 	switch (vcpu->arch.sie_block->ipa >> 8) {
111cb7485daSChristian Borntraeger 	case 0x01:
112cb7485daSChristian Borntraeger 		return kvm_s390_handle_01(vcpu);
113cb7485daSChristian Borntraeger 	case 0x82:
114cb7485daSChristian Borntraeger 		return kvm_s390_handle_lpsw(vcpu);
115cb7485daSChristian Borntraeger 	case 0x83:
116cb7485daSChristian Borntraeger 		return kvm_s390_handle_diag(vcpu);
117cb7485daSChristian Borntraeger 	case 0xaa:
118cb7485daSChristian Borntraeger 		return kvm_s390_handle_aa(vcpu);
119cb7485daSChristian Borntraeger 	case 0xae:
120cb7485daSChristian Borntraeger 		return kvm_s390_handle_sigp(vcpu);
121cb7485daSChristian Borntraeger 	case 0xb2:
122cb7485daSChristian Borntraeger 		return kvm_s390_handle_b2(vcpu);
123cb7485daSChristian Borntraeger 	case 0xb6:
124cb7485daSChristian Borntraeger 		return kvm_s390_handle_stctl(vcpu);
125cb7485daSChristian Borntraeger 	case 0xb7:
126cb7485daSChristian Borntraeger 		return kvm_s390_handle_lctl(vcpu);
127cb7485daSChristian Borntraeger 	case 0xb9:
128cb7485daSChristian Borntraeger 		return kvm_s390_handle_b9(vcpu);
129cb7485daSChristian Borntraeger 	case 0xe3:
130cb7485daSChristian Borntraeger 		return kvm_s390_handle_e3(vcpu);
131cb7485daSChristian Borntraeger 	case 0xe5:
132cb7485daSChristian Borntraeger 		return kvm_s390_handle_e5(vcpu);
133cb7485daSChristian Borntraeger 	case 0xeb:
134cb7485daSChristian Borntraeger 		return kvm_s390_handle_eb(vcpu);
135cb7485daSChristian Borntraeger 	default:
136b8e660b8SHeiko Carstens 		return -EOPNOTSUPP;
137ba5c1e9bSCarsten Otte 	}
138cb7485daSChristian Borntraeger }
139ba5c1e9bSCarsten Otte 
140f6af84e7SDavid Hildenbrand static int inject_prog_on_prog_intercept(struct kvm_vcpu *vcpu)
141439716a5SDavid Hildenbrand {
142f6af84e7SDavid Hildenbrand 	struct kvm_s390_pgm_info pgm_info = {
143f6af84e7SDavid Hildenbrand 		.code = vcpu->arch.sie_block->iprcc,
144eaa4f416SDavid Hildenbrand 		/* the PSW has already been rewound */
145eaa4f416SDavid Hildenbrand 		.flags = KVM_S390_PGM_FLAGS_NO_REWIND,
146f6af84e7SDavid Hildenbrand 	};
147439716a5SDavid Hildenbrand 
148439716a5SDavid Hildenbrand 	switch (vcpu->arch.sie_block->iprcc & ~PGM_PER) {
149439716a5SDavid Hildenbrand 	case PGM_AFX_TRANSLATION:
150439716a5SDavid Hildenbrand 	case PGM_ASX_TRANSLATION:
151439716a5SDavid Hildenbrand 	case PGM_EX_TRANSLATION:
152439716a5SDavid Hildenbrand 	case PGM_LFX_TRANSLATION:
153439716a5SDavid Hildenbrand 	case PGM_LSTE_SEQUENCE:
154439716a5SDavid Hildenbrand 	case PGM_LSX_TRANSLATION:
155439716a5SDavid Hildenbrand 	case PGM_LX_TRANSLATION:
156439716a5SDavid Hildenbrand 	case PGM_PRIMARY_AUTHORITY:
157439716a5SDavid Hildenbrand 	case PGM_SECONDARY_AUTHORITY:
158439716a5SDavid Hildenbrand 	case PGM_SPACE_SWITCH:
159f6af84e7SDavid Hildenbrand 		pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
160439716a5SDavid Hildenbrand 		break;
161439716a5SDavid Hildenbrand 	case PGM_ALEN_TRANSLATION:
162439716a5SDavid Hildenbrand 	case PGM_ALE_SEQUENCE:
163439716a5SDavid Hildenbrand 	case PGM_ASTE_INSTANCE:
164439716a5SDavid Hildenbrand 	case PGM_ASTE_SEQUENCE:
165439716a5SDavid Hildenbrand 	case PGM_ASTE_VALIDITY:
166439716a5SDavid Hildenbrand 	case PGM_EXTENDED_AUTHORITY:
167f6af84e7SDavid Hildenbrand 		pgm_info.exc_access_id = vcpu->arch.sie_block->eai;
168439716a5SDavid Hildenbrand 		break;
169439716a5SDavid Hildenbrand 	case PGM_ASCE_TYPE:
170439716a5SDavid Hildenbrand 	case PGM_PAGE_TRANSLATION:
171439716a5SDavid Hildenbrand 	case PGM_REGION_FIRST_TRANS:
172439716a5SDavid Hildenbrand 	case PGM_REGION_SECOND_TRANS:
173439716a5SDavid Hildenbrand 	case PGM_REGION_THIRD_TRANS:
174439716a5SDavid Hildenbrand 	case PGM_SEGMENT_TRANSLATION:
175f6af84e7SDavid Hildenbrand 		pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
176f6af84e7SDavid Hildenbrand 		pgm_info.exc_access_id  = vcpu->arch.sie_block->eai;
177f6af84e7SDavid Hildenbrand 		pgm_info.op_access_id  = vcpu->arch.sie_block->oai;
178439716a5SDavid Hildenbrand 		break;
179439716a5SDavid Hildenbrand 	case PGM_MONITOR:
180f6af84e7SDavid Hildenbrand 		pgm_info.mon_class_nr = vcpu->arch.sie_block->mcn;
181f6af84e7SDavid Hildenbrand 		pgm_info.mon_code = vcpu->arch.sie_block->tecmc;
182439716a5SDavid Hildenbrand 		break;
183403c8648SEric Farman 	case PGM_VECTOR_PROCESSING:
184439716a5SDavid Hildenbrand 	case PGM_DATA:
185f6af84e7SDavid Hildenbrand 		pgm_info.data_exc_code = vcpu->arch.sie_block->dxc;
186439716a5SDavid Hildenbrand 		break;
187439716a5SDavid Hildenbrand 	case PGM_PROTECTION:
188f6af84e7SDavid Hildenbrand 		pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
189f6af84e7SDavid Hildenbrand 		pgm_info.exc_access_id  = vcpu->arch.sie_block->eai;
190439716a5SDavid Hildenbrand 		break;
191439716a5SDavid Hildenbrand 	default:
192439716a5SDavid Hildenbrand 		break;
193439716a5SDavid Hildenbrand 	}
194439716a5SDavid Hildenbrand 
195439716a5SDavid Hildenbrand 	if (vcpu->arch.sie_block->iprcc & PGM_PER) {
196f6af84e7SDavid Hildenbrand 		pgm_info.per_code = vcpu->arch.sie_block->perc;
197f6af84e7SDavid Hildenbrand 		pgm_info.per_atmid = vcpu->arch.sie_block->peratmid;
198f6af84e7SDavid Hildenbrand 		pgm_info.per_address = vcpu->arch.sie_block->peraddr;
199f6af84e7SDavid Hildenbrand 		pgm_info.per_access_id = vcpu->arch.sie_block->peraid;
200439716a5SDavid Hildenbrand 	}
201f6af84e7SDavid Hildenbrand 	return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
202439716a5SDavid Hildenbrand }
203439716a5SDavid Hildenbrand 
204e325fe69SMichael Mueller /*
205e325fe69SMichael Mueller  * restore ITDB to program-interruption TDB in guest lowcore
206e325fe69SMichael Mueller  * and set TX abort indication if required
207e325fe69SMichael Mueller */
208e325fe69SMichael Mueller static int handle_itdb(struct kvm_vcpu *vcpu)
209e325fe69SMichael Mueller {
210e325fe69SMichael Mueller 	struct kvm_s390_itdb *itdb;
211e325fe69SMichael Mueller 	int rc;
212e325fe69SMichael Mueller 
213e325fe69SMichael Mueller 	if (!IS_TE_ENABLED(vcpu) || !IS_ITDB_VALID(vcpu))
214e325fe69SMichael Mueller 		return 0;
215e325fe69SMichael Mueller 	if (current->thread.per_flags & PER_FLAG_NO_TE)
216e325fe69SMichael Mueller 		return 0;
217e325fe69SMichael Mueller 	itdb = (struct kvm_s390_itdb *)vcpu->arch.sie_block->itdba;
218e325fe69SMichael Mueller 	rc = write_guest_lc(vcpu, __LC_PGM_TDB, itdb, sizeof(*itdb));
219e325fe69SMichael Mueller 	if (rc)
220e325fe69SMichael Mueller 		return rc;
221e325fe69SMichael Mueller 	memset(itdb, 0, sizeof(*itdb));
222e325fe69SMichael Mueller 
223e325fe69SMichael Mueller 	return 0;
224e325fe69SMichael Mueller }
225e325fe69SMichael Mueller 
22627291e21SDavid Hildenbrand #define per_event(vcpu) (vcpu->arch.sie_block->iprcc & PGM_PER)
22727291e21SDavid Hildenbrand 
228ba5c1e9bSCarsten Otte static int handle_prog(struct kvm_vcpu *vcpu)
229ba5c1e9bSCarsten Otte {
230684135e0SThomas Huth 	psw_t psw;
2310040e7d2SHeiko Carstens 	int rc;
2320040e7d2SHeiko Carstens 
233ba5c1e9bSCarsten Otte 	vcpu->stat.exit_program_interruption++;
2347feb6bb8SMichael Mueller 
235e663df91SJanosch Frank 	/*
236e663df91SJanosch Frank 	 * Intercept 8 indicates a loop of specification exceptions
237e663df91SJanosch Frank 	 * for protected guests.
238e663df91SJanosch Frank 	 */
239e663df91SJanosch Frank 	if (kvm_s390_pv_cpu_is_protected(vcpu))
240e663df91SJanosch Frank 		return -EOPNOTSUPP;
241e663df91SJanosch Frank 
24227291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu) && per_event(vcpu)) {
243a69cbe81SDavid Hildenbrand 		rc = kvm_s390_handle_per_event(vcpu);
244a69cbe81SDavid Hildenbrand 		if (rc)
245a69cbe81SDavid Hildenbrand 			return rc;
24627291e21SDavid Hildenbrand 		/* the interrupt might have been filtered out completely */
24727291e21SDavid Hildenbrand 		if (vcpu->arch.sie_block->iprcc == 0)
24827291e21SDavid Hildenbrand 			return 0;
24927291e21SDavid Hildenbrand 	}
25027291e21SDavid Hildenbrand 
251e325fe69SMichael Mueller 	trace_kvm_s390_intercept_prog(vcpu, vcpu->arch.sie_block->iprcc);
252684135e0SThomas Huth 	if (vcpu->arch.sie_block->iprcc == PGM_SPECIFICATION) {
253684135e0SThomas Huth 		rc = read_guest_lc(vcpu, __LC_PGM_NEW_PSW, &psw, sizeof(psw_t));
254684135e0SThomas Huth 		if (rc)
255684135e0SThomas Huth 			return rc;
256684135e0SThomas Huth 		/* Avoid endless loops of specification exceptions */
257684135e0SThomas Huth 		if (!is_valid_psw(&psw))
258684135e0SThomas Huth 			return -EOPNOTSUPP;
259684135e0SThomas Huth 	}
260e325fe69SMichael Mueller 	rc = handle_itdb(vcpu);
2610040e7d2SHeiko Carstens 	if (rc)
2620040e7d2SHeiko Carstens 		return rc;
263439716a5SDavid Hildenbrand 
264f6af84e7SDavid Hildenbrand 	return inject_prog_on_prog_intercept(vcpu);
265ba5c1e9bSCarsten Otte }
266ba5c1e9bSCarsten Otte 
2679a558ee3SThomas Huth /**
268f14d82e0SThomas Huth  * handle_external_interrupt - used for external interruption interceptions
269f14d82e0SThomas Huth  *
270f14d82e0SThomas Huth  * This interception only occurs if the CPUSTAT_EXT_INT bit was set, or if
271f14d82e0SThomas Huth  * the new PSW does not have external interrupts disabled. In the first case,
272f14d82e0SThomas Huth  * we've got to deliver the interrupt manually, and in the second case, we
273f14d82e0SThomas Huth  * drop to userspace to handle the situation there.
274f14d82e0SThomas Huth  */
275f14d82e0SThomas Huth static int handle_external_interrupt(struct kvm_vcpu *vcpu)
276f14d82e0SThomas Huth {
277f14d82e0SThomas Huth 	u16 eic = vcpu->arch.sie_block->eic;
278383d0b05SJens Freimann 	struct kvm_s390_irq irq;
279f14d82e0SThomas Huth 	psw_t newpsw;
280f14d82e0SThomas Huth 	int rc;
281f14d82e0SThomas Huth 
282f14d82e0SThomas Huth 	vcpu->stat.exit_external_interrupt++;
283f14d82e0SThomas Huth 
284f14d82e0SThomas Huth 	rc = read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &newpsw, sizeof(psw_t));
285f14d82e0SThomas Huth 	if (rc)
286f14d82e0SThomas Huth 		return rc;
287f14d82e0SThomas Huth 	/* We can not handle clock comparator or timer interrupt with bad PSW */
288f14d82e0SThomas Huth 	if ((eic == EXT_IRQ_CLK_COMP || eic == EXT_IRQ_CPU_TIMER) &&
289f14d82e0SThomas Huth 	    (newpsw.mask & PSW_MASK_EXT))
290f14d82e0SThomas Huth 		return -EOPNOTSUPP;
291f14d82e0SThomas Huth 
292f14d82e0SThomas Huth 	switch (eic) {
293f14d82e0SThomas Huth 	case EXT_IRQ_CLK_COMP:
294f14d82e0SThomas Huth 		irq.type = KVM_S390_INT_CLOCK_COMP;
295f14d82e0SThomas Huth 		break;
296f14d82e0SThomas Huth 	case EXT_IRQ_CPU_TIMER:
297f14d82e0SThomas Huth 		irq.type = KVM_S390_INT_CPU_TIMER;
298f14d82e0SThomas Huth 		break;
299f14d82e0SThomas Huth 	case EXT_IRQ_EXTERNAL_CALL:
300f14d82e0SThomas Huth 		irq.type = KVM_S390_INT_EXTERNAL_CALL;
301383d0b05SJens Freimann 		irq.u.extcall.code = vcpu->arch.sie_block->extcpuaddr;
302ea5f4969SDavid Hildenbrand 		rc = kvm_s390_inject_vcpu(vcpu, &irq);
303ea5f4969SDavid Hildenbrand 		/* ignore if another external call is already pending */
304ea5f4969SDavid Hildenbrand 		if (rc == -EBUSY)
305ea5f4969SDavid Hildenbrand 			return 0;
306ea5f4969SDavid Hildenbrand 		return rc;
307f14d82e0SThomas Huth 	default:
308f14d82e0SThomas Huth 		return -EOPNOTSUPP;
309f14d82e0SThomas Huth 	}
310f14d82e0SThomas Huth 
311f14d82e0SThomas Huth 	return kvm_s390_inject_vcpu(vcpu, &irq);
312f14d82e0SThomas Huth }
313f14d82e0SThomas Huth 
314f14d82e0SThomas Huth /**
3159a558ee3SThomas Huth  * Handle MOVE PAGE partial execution interception.
3169a558ee3SThomas Huth  *
3179a558ee3SThomas Huth  * This interception can only happen for guests with DAT disabled and
3189a558ee3SThomas Huth  * addresses that are currently not mapped in the host. Thus we try to
3199a558ee3SThomas Huth  * set up the mappings for the corresponding user pages here (or throw
3209a558ee3SThomas Huth  * addressing exceptions in case of illegal guest addresses).
3219a558ee3SThomas Huth  */
3229a558ee3SThomas Huth static int handle_mvpg_pei(struct kvm_vcpu *vcpu)
3239a558ee3SThomas Huth {
324f22166dcSThomas Huth 	unsigned long srcaddr, dstaddr;
3259a558ee3SThomas Huth 	int reg1, reg2, rc;
3269a558ee3SThomas Huth 
3279a558ee3SThomas Huth 	kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
3289a558ee3SThomas Huth 
3299a558ee3SThomas Huth 	/* Make sure that the source is paged-in */
3303cfad023SThomas Huth 	rc = guest_translate_address(vcpu, vcpu->run->s.regs.gprs[reg2],
33192c96321SDavid Hildenbrand 				     reg2, &srcaddr, GACC_FETCH);
3323cfad023SThomas Huth 	if (rc)
3333cfad023SThomas Huth 		return kvm_s390_inject_prog_cond(vcpu, rc);
334f22166dcSThomas Huth 	rc = kvm_arch_fault_in_page(vcpu, srcaddr, 0);
335f22166dcSThomas Huth 	if (rc != 0)
3369a558ee3SThomas Huth 		return rc;
3379a558ee3SThomas Huth 
3389a558ee3SThomas Huth 	/* Make sure that the destination is paged-in */
3393cfad023SThomas Huth 	rc = guest_translate_address(vcpu, vcpu->run->s.regs.gprs[reg1],
34092c96321SDavid Hildenbrand 				     reg1, &dstaddr, GACC_STORE);
3413cfad023SThomas Huth 	if (rc)
3423cfad023SThomas Huth 		return kvm_s390_inject_prog_cond(vcpu, rc);
343f22166dcSThomas Huth 	rc = kvm_arch_fault_in_page(vcpu, dstaddr, 1);
344f22166dcSThomas Huth 	if (rc != 0)
3459a558ee3SThomas Huth 		return rc;
3469a558ee3SThomas Huth 
3470e8bc06aSDavid Hildenbrand 	kvm_s390_retry_instr(vcpu);
3489a558ee3SThomas Huth 
3499a558ee3SThomas Huth 	return 0;
3509a558ee3SThomas Huth }
3519a558ee3SThomas Huth 
3529a558ee3SThomas Huth static int handle_partial_execution(struct kvm_vcpu *vcpu)
3539a558ee3SThomas Huth {
3549ec6de19SAlexander Yarygin 	vcpu->stat.exit_pei++;
3559ec6de19SAlexander Yarygin 
3569a558ee3SThomas Huth 	if (vcpu->arch.sie_block->ipa == 0xb254)	/* MVPG */
3579a558ee3SThomas Huth 		return handle_mvpg_pei(vcpu);
3584953919fSDavid Hildenbrand 	if (vcpu->arch.sie_block->ipa >> 8 == 0xae)	/* SIGP */
3594953919fSDavid Hildenbrand 		return kvm_s390_handle_sigp_pei(vcpu);
3609a558ee3SThomas Huth 
3619a558ee3SThomas Huth 	return -EOPNOTSUPP;
3629a558ee3SThomas Huth }
3639a558ee3SThomas Huth 
364b7c92f1aSQingFeng Hao /*
365b7c92f1aSQingFeng Hao  * Handle the sthyi instruction that provides the guest with system
366b7c92f1aSQingFeng Hao  * information, like current CPU resources available at each level of
367b7c92f1aSQingFeng Hao  * the machine.
368b7c92f1aSQingFeng Hao  */
369b7c92f1aSQingFeng Hao int handle_sthyi(struct kvm_vcpu *vcpu)
370b7c92f1aSQingFeng Hao {
371b7c92f1aSQingFeng Hao 	int reg1, reg2, r = 0;
372b7c92f1aSQingFeng Hao 	u64 code, addr, cc = 0, rc = 0;
373b7c92f1aSQingFeng Hao 	struct sthyi_sctns *sctns = NULL;
374b7c92f1aSQingFeng Hao 
375b7c92f1aSQingFeng Hao 	if (!test_kvm_facility(vcpu->kvm, 74))
376b7c92f1aSQingFeng Hao 		return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
377b7c92f1aSQingFeng Hao 
378b7c92f1aSQingFeng Hao 	kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
379b7c92f1aSQingFeng Hao 	code = vcpu->run->s.regs.gprs[reg1];
380b7c92f1aSQingFeng Hao 	addr = vcpu->run->s.regs.gprs[reg2];
381b7c92f1aSQingFeng Hao 
382b7c92f1aSQingFeng Hao 	vcpu->stat.instruction_sthyi++;
383b7c92f1aSQingFeng Hao 	VCPU_EVENT(vcpu, 3, "STHYI: fc: %llu addr: 0x%016llx", code, addr);
384b7c92f1aSQingFeng Hao 	trace_kvm_s390_handle_sthyi(vcpu, code, addr);
385b7c92f1aSQingFeng Hao 
386b7c92f1aSQingFeng Hao 	if (reg1 == reg2 || reg1 & 1 || reg2 & 1)
387b7c92f1aSQingFeng Hao 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
388b7c92f1aSQingFeng Hao 
389b7c92f1aSQingFeng Hao 	if (code & 0xffff) {
390b7c92f1aSQingFeng Hao 		cc = 3;
391b7c92f1aSQingFeng Hao 		rc = 4;
392b7c92f1aSQingFeng Hao 		goto out;
393b7c92f1aSQingFeng Hao 	}
394b7c92f1aSQingFeng Hao 
395*22d768c3SJanosch Frank 	if (!kvm_s390_pv_cpu_is_protected(vcpu) && (addr & ~PAGE_MASK))
396b7c92f1aSQingFeng Hao 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
397b7c92f1aSQingFeng Hao 
398b7c92f1aSQingFeng Hao 	sctns = (void *)get_zeroed_page(GFP_KERNEL);
399b7c92f1aSQingFeng Hao 	if (!sctns)
400b7c92f1aSQingFeng Hao 		return -ENOMEM;
401b7c92f1aSQingFeng Hao 
402b7c92f1aSQingFeng Hao 	cc = sthyi_fill(sctns, &rc);
403b7c92f1aSQingFeng Hao 
404b7c92f1aSQingFeng Hao out:
405b7c92f1aSQingFeng Hao 	if (!cc) {
406*22d768c3SJanosch Frank 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
407*22d768c3SJanosch Frank 			memcpy((void *)(sida_origin(vcpu->arch.sie_block)),
408*22d768c3SJanosch Frank 			       sctns, PAGE_SIZE);
409*22d768c3SJanosch Frank 		} else {
410b7c92f1aSQingFeng Hao 			r = write_guest(vcpu, addr, reg2, sctns, PAGE_SIZE);
411b7c92f1aSQingFeng Hao 			if (r) {
412b7c92f1aSQingFeng Hao 				free_page((unsigned long)sctns);
413b7c92f1aSQingFeng Hao 				return kvm_s390_inject_prog_cond(vcpu, r);
414b7c92f1aSQingFeng Hao 			}
415b7c92f1aSQingFeng Hao 		}
416*22d768c3SJanosch Frank 	}
417b7c92f1aSQingFeng Hao 
418b7c92f1aSQingFeng Hao 	free_page((unsigned long)sctns);
419b7c92f1aSQingFeng Hao 	vcpu->run->s.regs.gprs[reg2 + 1] = rc;
420b7c92f1aSQingFeng Hao 	kvm_s390_set_psw_cc(vcpu, cc);
421b7c92f1aSQingFeng Hao 	return r;
422b7c92f1aSQingFeng Hao }
423b7c92f1aSQingFeng Hao 
424a011eeb2SJanosch Frank static int handle_operexc(struct kvm_vcpu *vcpu)
425a011eeb2SJanosch Frank {
426fb7dc1d4SChristian Borntraeger 	psw_t oldpsw, newpsw;
427fb7dc1d4SChristian Borntraeger 	int rc;
428fb7dc1d4SChristian Borntraeger 
429a011eeb2SJanosch Frank 	vcpu->stat.exit_operation_exception++;
430a011eeb2SJanosch Frank 	trace_kvm_s390_handle_operexc(vcpu, vcpu->arch.sie_block->ipa,
431a011eeb2SJanosch Frank 				      vcpu->arch.sie_block->ipb);
432a011eeb2SJanosch Frank 
433c0a6bfdcSChristian Borntraeger 	if (vcpu->arch.sie_block->ipa == 0xb256)
43495ca2cb5SJanosch Frank 		return handle_sthyi(vcpu);
43595ca2cb5SJanosch Frank 
4366502a34cSDavid Hildenbrand 	if (vcpu->arch.sie_block->ipa == 0 && vcpu->kvm->arch.user_instr0)
4376502a34cSDavid Hildenbrand 		return -EOPNOTSUPP;
438fb7dc1d4SChristian Borntraeger 	rc = read_guest_lc(vcpu, __LC_PGM_NEW_PSW, &newpsw, sizeof(psw_t));
439fb7dc1d4SChristian Borntraeger 	if (rc)
440fb7dc1d4SChristian Borntraeger 		return rc;
441fb7dc1d4SChristian Borntraeger 	/*
442fb7dc1d4SChristian Borntraeger 	 * Avoid endless loops of operation exceptions, if the pgm new
443fb7dc1d4SChristian Borntraeger 	 * PSW will cause a new operation exception.
444fb7dc1d4SChristian Borntraeger 	 * The heuristic checks if the pgm new psw is within 6 bytes before
445fb7dc1d4SChristian Borntraeger 	 * the faulting psw address (with same DAT, AS settings) and the
446fb7dc1d4SChristian Borntraeger 	 * new psw is not a wait psw and the fault was not triggered by
447fb7dc1d4SChristian Borntraeger 	 * problem state.
448fb7dc1d4SChristian Borntraeger 	 */
449fb7dc1d4SChristian Borntraeger 	oldpsw = vcpu->arch.sie_block->gpsw;
450fb7dc1d4SChristian Borntraeger 	if (oldpsw.addr - newpsw.addr <= 6 &&
451fb7dc1d4SChristian Borntraeger 	    !(newpsw.mask & PSW_MASK_WAIT) &&
452fb7dc1d4SChristian Borntraeger 	    !(oldpsw.mask & PSW_MASK_PSTATE) &&
453fb7dc1d4SChristian Borntraeger 	    (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) &&
454fb7dc1d4SChristian Borntraeger 	    (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT))
455fb7dc1d4SChristian Borntraeger 		return -EOPNOTSUPP;
4566502a34cSDavid Hildenbrand 
457a011eeb2SJanosch Frank 	return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
458a011eeb2SJanosch Frank }
459a011eeb2SJanosch Frank 
460d274995eSJanosch Frank static int handle_pv_spx(struct kvm_vcpu *vcpu)
461d274995eSJanosch Frank {
462d274995eSJanosch Frank 	u32 pref = *(u32 *)vcpu->arch.sie_block->sidad;
463d274995eSJanosch Frank 
464d274995eSJanosch Frank 	kvm_s390_set_prefix(vcpu, pref);
465d274995eSJanosch Frank 	trace_kvm_s390_handle_prefix(vcpu, 1, pref);
466d274995eSJanosch Frank 	return 0;
467d274995eSJanosch Frank }
468d274995eSJanosch Frank 
4690890ddeaSChristian Borntraeger static int handle_pv_sclp(struct kvm_vcpu *vcpu)
4700890ddeaSChristian Borntraeger {
4710890ddeaSChristian Borntraeger 	struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
4720890ddeaSChristian Borntraeger 
4730890ddeaSChristian Borntraeger 	spin_lock(&fi->lock);
4740890ddeaSChristian Borntraeger 	/*
4750890ddeaSChristian Borntraeger 	 * 2 cases:
4760890ddeaSChristian Borntraeger 	 * a: an sccb answering interrupt was already pending or in flight.
4770890ddeaSChristian Borntraeger 	 *    As the sccb value is not known we can simply set some value to
4780890ddeaSChristian Borntraeger 	 *    trigger delivery of a saved SCCB. UV will then use its saved
4790890ddeaSChristian Borntraeger 	 *    copy of the SCCB value.
4800890ddeaSChristian Borntraeger 	 * b: an error SCCB interrupt needs to be injected so we also inject
4810890ddeaSChristian Borntraeger 	 *    a fake SCCB address. Firmware will use the proper one.
4820890ddeaSChristian Borntraeger 	 * This makes sure, that both errors and real sccb returns will only
4830890ddeaSChristian Borntraeger 	 * be delivered after a notification intercept (instruction has
4840890ddeaSChristian Borntraeger 	 * finished) but not after others.
4850890ddeaSChristian Borntraeger 	 */
4860890ddeaSChristian Borntraeger 	fi->srv_signal.ext_params |= 0x43000;
4870890ddeaSChristian Borntraeger 	set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
4880890ddeaSChristian Borntraeger 	clear_bit(IRQ_PEND_EXT_SERVICE, &fi->masked_irqs);
4890890ddeaSChristian Borntraeger 	spin_unlock(&fi->lock);
4900890ddeaSChristian Borntraeger 	return 0;
4910890ddeaSChristian Borntraeger }
4920890ddeaSChristian Borntraeger 
49353227810SClaudio Imbrenda static int handle_pv_uvc(struct kvm_vcpu *vcpu)
49453227810SClaudio Imbrenda {
49553227810SClaudio Imbrenda 	struct uv_cb_share *guest_uvcb = (void *)vcpu->arch.sie_block->sidad;
49653227810SClaudio Imbrenda 	struct uv_cb_cts uvcb = {
49753227810SClaudio Imbrenda 		.header.cmd	= UVC_CMD_UNPIN_PAGE_SHARED,
49853227810SClaudio Imbrenda 		.header.len	= sizeof(uvcb),
49953227810SClaudio Imbrenda 		.guest_handle	= kvm_s390_pv_get_handle(vcpu->kvm),
50053227810SClaudio Imbrenda 		.gaddr		= guest_uvcb->paddr,
50153227810SClaudio Imbrenda 	};
50253227810SClaudio Imbrenda 	int rc;
50353227810SClaudio Imbrenda 
50453227810SClaudio Imbrenda 	if (guest_uvcb->header.cmd != UVC_CMD_REMOVE_SHARED_ACCESS) {
50553227810SClaudio Imbrenda 		WARN_ONCE(1, "Unexpected notification intercept for UVC 0x%x\n",
50653227810SClaudio Imbrenda 			  guest_uvcb->header.cmd);
50753227810SClaudio Imbrenda 		return 0;
50853227810SClaudio Imbrenda 	}
50953227810SClaudio Imbrenda 	rc = gmap_make_secure(vcpu->arch.gmap, uvcb.gaddr, &uvcb);
51053227810SClaudio Imbrenda 	/*
51153227810SClaudio Imbrenda 	 * If the unpin did not succeed, the guest will exit again for the UVC
51253227810SClaudio Imbrenda 	 * and we will retry the unpin.
51353227810SClaudio Imbrenda 	 */
51453227810SClaudio Imbrenda 	if (rc == -EINVAL)
51553227810SClaudio Imbrenda 		return 0;
51653227810SClaudio Imbrenda 	return rc;
51753227810SClaudio Imbrenda }
51853227810SClaudio Imbrenda 
519da24a0ccSJanosch Frank static int handle_pv_notification(struct kvm_vcpu *vcpu)
520da24a0ccSJanosch Frank {
521d274995eSJanosch Frank 	if (vcpu->arch.sie_block->ipa == 0xb210)
522d274995eSJanosch Frank 		return handle_pv_spx(vcpu);
5230890ddeaSChristian Borntraeger 	if (vcpu->arch.sie_block->ipa == 0xb220)
5240890ddeaSChristian Borntraeger 		return handle_pv_sclp(vcpu);
52553227810SClaudio Imbrenda 	if (vcpu->arch.sie_block->ipa == 0xb9a4)
52653227810SClaudio Imbrenda 		return handle_pv_uvc(vcpu);
5270890ddeaSChristian Borntraeger 
528da24a0ccSJanosch Frank 	return handle_instruction(vcpu);
529da24a0ccSJanosch Frank }
530da24a0ccSJanosch Frank 
5318f2abe6aSChristian Borntraeger int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
5328f2abe6aSChristian Borntraeger {
5335ffe466cSDavid Hildenbrand 	int rc, per_rc = 0;
5345ffe466cSDavid Hildenbrand 
53571f116bfSDavid Hildenbrand 	if (kvm_is_ucontrol(vcpu->kvm))
53671f116bfSDavid Hildenbrand 		return -EOPNOTSUPP;
53771f116bfSDavid Hildenbrand 
53846b708eaSChristian Borntraeger 	switch (vcpu->arch.sie_block->icptcode) {
539947b8972SFarhan Ali 	case ICPT_EXTREQ:
540a5e0aceaSChristian Borntraeger 		vcpu->stat.exit_external_request++;
541a5e0aceaSChristian Borntraeger 		return 0;
542947b8972SFarhan Ali 	case ICPT_IOREQ:
543a5e0aceaSChristian Borntraeger 		vcpu->stat.exit_io_request++;
544a5e0aceaSChristian Borntraeger 		return 0;
545947b8972SFarhan Ali 	case ICPT_INST:
5465ffe466cSDavid Hildenbrand 		rc = handle_instruction(vcpu);
5475ffe466cSDavid Hildenbrand 		break;
548947b8972SFarhan Ali 	case ICPT_PROGI:
54946b708eaSChristian Borntraeger 		return handle_prog(vcpu);
550947b8972SFarhan Ali 	case ICPT_EXTINT:
55146b708eaSChristian Borntraeger 		return handle_external_interrupt(vcpu);
552947b8972SFarhan Ali 	case ICPT_WAIT:
55346b708eaSChristian Borntraeger 		return kvm_s390_handle_wait(vcpu);
554947b8972SFarhan Ali 	case ICPT_VALIDITY:
55546b708eaSChristian Borntraeger 		return handle_validity(vcpu);
556947b8972SFarhan Ali 	case ICPT_STOP:
55746b708eaSChristian Borntraeger 		return handle_stop(vcpu);
558947b8972SFarhan Ali 	case ICPT_OPEREXC:
5595ffe466cSDavid Hildenbrand 		rc = handle_operexc(vcpu);
5605ffe466cSDavid Hildenbrand 		break;
561947b8972SFarhan Ali 	case ICPT_PARTEXEC:
5625ffe466cSDavid Hildenbrand 		rc = handle_partial_execution(vcpu);
5635ffe466cSDavid Hildenbrand 		break;
564730cd632SFarhan Ali 	case ICPT_KSS:
565730cd632SFarhan Ali 		rc = kvm_s390_skey_check_enable(vcpu);
566730cd632SFarhan Ali 		break;
56749710db0SJanosch Frank 	case ICPT_MCHKREQ:
56849710db0SJanosch Frank 	case ICPT_INT_ENABLE:
56949710db0SJanosch Frank 		/*
57049710db0SJanosch Frank 		 * PSW bit 13 or a CR (0, 6, 14) changed and we might
57149710db0SJanosch Frank 		 * now be able to deliver interrupts. The pre-run code
57249710db0SJanosch Frank 		 * will take care of this.
57349710db0SJanosch Frank 		 */
57449710db0SJanosch Frank 		rc = 0;
57549710db0SJanosch Frank 		break;
576da24a0ccSJanosch Frank 	case ICPT_PV_INSTR:
577da24a0ccSJanosch Frank 		rc = handle_instruction(vcpu);
578da24a0ccSJanosch Frank 		break;
579da24a0ccSJanosch Frank 	case ICPT_PV_NOTIFY:
580da24a0ccSJanosch Frank 		rc = handle_pv_notification(vcpu);
581da24a0ccSJanosch Frank 		break;
582d274995eSJanosch Frank 	case ICPT_PV_PREF:
583d274995eSJanosch Frank 		rc = 0;
584d274995eSJanosch Frank 		gmap_convert_to_secure(vcpu->arch.gmap,
585d274995eSJanosch Frank 				       kvm_s390_get_prefix(vcpu));
586d274995eSJanosch Frank 		gmap_convert_to_secure(vcpu->arch.gmap,
587d274995eSJanosch Frank 				       kvm_s390_get_prefix(vcpu) + PAGE_SIZE);
588d274995eSJanosch Frank 		break;
58946b708eaSChristian Borntraeger 	default:
590b8e660b8SHeiko Carstens 		return -EOPNOTSUPP;
59146b708eaSChristian Borntraeger 	}
5925ffe466cSDavid Hildenbrand 
5935ffe466cSDavid Hildenbrand 	/* process PER, also if the instrution is processed in user space */
5945ffe466cSDavid Hildenbrand 	if (vcpu->arch.sie_block->icptstatus & 0x02 &&
5955ffe466cSDavid Hildenbrand 	    (!rc || rc == -EOPNOTSUPP))
5965ffe466cSDavid Hildenbrand 		per_rc = kvm_s390_handle_per_ifetch_icpt(vcpu);
5975ffe466cSDavid Hildenbrand 	return per_rc ? per_rc : rc;
5988f2abe6aSChristian Borntraeger }
599