xref: /linux/arch/s390/kvm/intercept.c (revision 9a558ee3ccb8afcf43c8d9d4d206ab6de4aa30a2)
18f2abe6aSChristian Borntraeger /*
2a53c8fabSHeiko Carstens  * in-kernel handling for sie intercepts
38f2abe6aSChristian Borntraeger  *
4*9a558ee3SThomas Huth  * Copyright IBM Corp. 2008, 2014
58f2abe6aSChristian Borntraeger  *
68f2abe6aSChristian Borntraeger  * This program is free software; you can redistribute it and/or modify
78f2abe6aSChristian Borntraeger  * it under the terms of the GNU General Public License (version 2 only)
88f2abe6aSChristian Borntraeger  * as published by the Free Software Foundation.
98f2abe6aSChristian Borntraeger  *
108f2abe6aSChristian Borntraeger  *    Author(s): Carsten Otte <cotte@de.ibm.com>
118f2abe6aSChristian Borntraeger  *               Christian Borntraeger <borntraeger@de.ibm.com>
128f2abe6aSChristian Borntraeger  */
138f2abe6aSChristian Borntraeger 
148f2abe6aSChristian Borntraeger #include <linux/kvm_host.h>
158f2abe6aSChristian Borntraeger #include <linux/errno.h>
168f2abe6aSChristian Borntraeger #include <linux/pagemap.h>
178f2abe6aSChristian Borntraeger 
188f2abe6aSChristian Borntraeger #include <asm/kvm_host.h>
19a86dcc24SMichael Mueller #include <asm/asm-offsets.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 
26f379aae5SCornelia Huck 
2777975357SCornelia Huck static const intercept_handler_t instruction_handlers[256] = {
288c3f61e2SCornelia Huck 	[0x01] = kvm_s390_handle_01,
2948a3e950SCornelia Huck 	[0x82] = kvm_s390_handle_lpsw,
30e28acfeaSChristian Borntraeger 	[0x83] = kvm_s390_handle_diag,
315288fbf0SChristian Borntraeger 	[0xae] = kvm_s390_handle_sigp,
3270455a36SChristian Borntraeger 	[0xb2] = kvm_s390_handle_b2,
33aba07508SDavid Hildenbrand 	[0xb6] = kvm_s390_handle_stctl,
34953ed88dSThomas Huth 	[0xb7] = kvm_s390_handle_lctl,
3548a3e950SCornelia Huck 	[0xb9] = kvm_s390_handle_b9,
36bb25b9baSChristian Borntraeger 	[0xe5] = kvm_s390_handle_e5,
37953ed88dSThomas Huth 	[0xeb] = kvm_s390_handle_eb,
38ba5c1e9bSCarsten Otte };
398f2abe6aSChristian Borntraeger 
408f2abe6aSChristian Borntraeger static int handle_noop(struct kvm_vcpu *vcpu)
418f2abe6aSChristian Borntraeger {
428f2abe6aSChristian Borntraeger 	switch (vcpu->arch.sie_block->icptcode) {
430eaeafa1SChristian Borntraeger 	case 0x0:
440eaeafa1SChristian Borntraeger 		vcpu->stat.exit_null++;
450eaeafa1SChristian Borntraeger 		break;
468f2abe6aSChristian Borntraeger 	case 0x10:
478f2abe6aSChristian Borntraeger 		vcpu->stat.exit_external_request++;
488f2abe6aSChristian Borntraeger 		break;
498f2abe6aSChristian Borntraeger 	case 0x14:
508f2abe6aSChristian Borntraeger 		vcpu->stat.exit_external_interrupt++;
518f2abe6aSChristian Borntraeger 		break;
528f2abe6aSChristian Borntraeger 	default:
538f2abe6aSChristian Borntraeger 		break; /* nothing */
548f2abe6aSChristian Borntraeger 	}
558f2abe6aSChristian Borntraeger 	return 0;
568f2abe6aSChristian Borntraeger }
578f2abe6aSChristian Borntraeger 
588f2abe6aSChristian Borntraeger static int handle_stop(struct kvm_vcpu *vcpu)
598f2abe6aSChristian Borntraeger {
609ace903dSChristian Ehrhardt 	int rc = 0;
615288fbf0SChristian Borntraeger 
628f2abe6aSChristian Borntraeger 	vcpu->stat.exit_stop_request++;
635288fbf0SChristian Borntraeger 	spin_lock_bh(&vcpu->arch.local_int.lock);
645288fbf0SChristian Borntraeger 
65ade38c31SCornelia Huck 	trace_kvm_s390_stop_request(vcpu->arch.local_int.action_bits);
66ade38c31SCornelia Huck 
675288fbf0SChristian Borntraeger 	if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) {
689e6dabefSCornelia Huck 		atomic_set_mask(CPUSTAT_STOPPED,
699e6dabefSCornelia Huck 				&vcpu->arch.sie_block->cpuflags);
705288fbf0SChristian Borntraeger 		vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP;
715288fbf0SChristian Borntraeger 		VCPU_EVENT(vcpu, 3, "%s", "cpu stopped");
72b8e660b8SHeiko Carstens 		rc = -EOPNOTSUPP;
739ace903dSChristian Ehrhardt 	}
749ace903dSChristian Ehrhardt 
759e0d5473SJens Freimann 	if (vcpu->arch.local_int.action_bits & ACTION_STORE_ON_STOP) {
769e0d5473SJens Freimann 		vcpu->arch.local_int.action_bits &= ~ACTION_STORE_ON_STOP;
779e0d5473SJens Freimann 		/* store status must be called unlocked. Since local_int.lock
789e0d5473SJens Freimann 		 * only protects local_int.* and not guest memory we can give
799e0d5473SJens Freimann 		 * up the lock here */
809e0d5473SJens Freimann 		spin_unlock_bh(&vcpu->arch.local_int.lock);
819e0d5473SJens Freimann 		rc = kvm_s390_vcpu_store_status(vcpu,
829e0d5473SJens Freimann 						KVM_S390_STORE_STATUS_NOADDR);
839e0d5473SJens Freimann 		if (rc >= 0)
849e0d5473SJens Freimann 			rc = -EOPNOTSUPP;
859e0d5473SJens Freimann 	} else
865288fbf0SChristian Borntraeger 		spin_unlock_bh(&vcpu->arch.local_int.lock);
875288fbf0SChristian Borntraeger 	return rc;
888f2abe6aSChristian Borntraeger }
898f2abe6aSChristian Borntraeger 
908f2abe6aSChristian Borntraeger static int handle_validity(struct kvm_vcpu *vcpu)
918f2abe6aSChristian Borntraeger {
928f2abe6aSChristian Borntraeger 	int viwhy = vcpu->arch.sie_block->ipb >> 16;
933edbcff9SCarsten Otte 
948f2abe6aSChristian Borntraeger 	vcpu->stat.exit_validity++;
955786fffaSCornelia Huck 	trace_kvm_s390_intercept_validity(vcpu, viwhy);
962c70fe44SChristian Borntraeger 	WARN_ONCE(true, "kvm: unhandled validity intercept 0x%x\n", viwhy);
972c70fe44SChristian Borntraeger 	return -EOPNOTSUPP;
988f2abe6aSChristian Borntraeger }
998f2abe6aSChristian Borntraeger 
100ba5c1e9bSCarsten Otte static int handle_instruction(struct kvm_vcpu *vcpu)
101ba5c1e9bSCarsten Otte {
102ba5c1e9bSCarsten Otte 	intercept_handler_t handler;
103ba5c1e9bSCarsten Otte 
104ba5c1e9bSCarsten Otte 	vcpu->stat.exit_instruction++;
1055786fffaSCornelia Huck 	trace_kvm_s390_intercept_instruction(vcpu,
1065786fffaSCornelia Huck 					     vcpu->arch.sie_block->ipa,
1075786fffaSCornelia Huck 					     vcpu->arch.sie_block->ipb);
108ba5c1e9bSCarsten Otte 	handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
109ba5c1e9bSCarsten Otte 	if (handler)
110ba5c1e9bSCarsten Otte 		return handler(vcpu);
111b8e660b8SHeiko Carstens 	return -EOPNOTSUPP;
112ba5c1e9bSCarsten Otte }
113ba5c1e9bSCarsten Otte 
114439716a5SDavid Hildenbrand static void __extract_prog_irq(struct kvm_vcpu *vcpu,
115439716a5SDavid Hildenbrand 			       struct kvm_s390_pgm_info *pgm_info)
116439716a5SDavid Hildenbrand {
117439716a5SDavid Hildenbrand 	memset(pgm_info, 0, sizeof(struct kvm_s390_pgm_info));
118439716a5SDavid Hildenbrand 	pgm_info->code = vcpu->arch.sie_block->iprcc;
119439716a5SDavid Hildenbrand 
120439716a5SDavid Hildenbrand 	switch (vcpu->arch.sie_block->iprcc & ~PGM_PER) {
121439716a5SDavid Hildenbrand 	case PGM_AFX_TRANSLATION:
122439716a5SDavid Hildenbrand 	case PGM_ASX_TRANSLATION:
123439716a5SDavid Hildenbrand 	case PGM_EX_TRANSLATION:
124439716a5SDavid Hildenbrand 	case PGM_LFX_TRANSLATION:
125439716a5SDavid Hildenbrand 	case PGM_LSTE_SEQUENCE:
126439716a5SDavid Hildenbrand 	case PGM_LSX_TRANSLATION:
127439716a5SDavid Hildenbrand 	case PGM_LX_TRANSLATION:
128439716a5SDavid Hildenbrand 	case PGM_PRIMARY_AUTHORITY:
129439716a5SDavid Hildenbrand 	case PGM_SECONDARY_AUTHORITY:
130439716a5SDavid Hildenbrand 	case PGM_SPACE_SWITCH:
131439716a5SDavid Hildenbrand 		pgm_info->trans_exc_code = vcpu->arch.sie_block->tecmc;
132439716a5SDavid Hildenbrand 		break;
133439716a5SDavid Hildenbrand 	case PGM_ALEN_TRANSLATION:
134439716a5SDavid Hildenbrand 	case PGM_ALE_SEQUENCE:
135439716a5SDavid Hildenbrand 	case PGM_ASTE_INSTANCE:
136439716a5SDavid Hildenbrand 	case PGM_ASTE_SEQUENCE:
137439716a5SDavid Hildenbrand 	case PGM_ASTE_VALIDITY:
138439716a5SDavid Hildenbrand 	case PGM_EXTENDED_AUTHORITY:
139439716a5SDavid Hildenbrand 		pgm_info->exc_access_id = vcpu->arch.sie_block->eai;
140439716a5SDavid Hildenbrand 		break;
141439716a5SDavid Hildenbrand 	case PGM_ASCE_TYPE:
142439716a5SDavid Hildenbrand 	case PGM_PAGE_TRANSLATION:
143439716a5SDavid Hildenbrand 	case PGM_REGION_FIRST_TRANS:
144439716a5SDavid Hildenbrand 	case PGM_REGION_SECOND_TRANS:
145439716a5SDavid Hildenbrand 	case PGM_REGION_THIRD_TRANS:
146439716a5SDavid Hildenbrand 	case PGM_SEGMENT_TRANSLATION:
147439716a5SDavid Hildenbrand 		pgm_info->trans_exc_code = vcpu->arch.sie_block->tecmc;
148439716a5SDavid Hildenbrand 		pgm_info->exc_access_id  = vcpu->arch.sie_block->eai;
149439716a5SDavid Hildenbrand 		pgm_info->op_access_id  = vcpu->arch.sie_block->oai;
150439716a5SDavid Hildenbrand 		break;
151439716a5SDavid Hildenbrand 	case PGM_MONITOR:
152439716a5SDavid Hildenbrand 		pgm_info->mon_class_nr = vcpu->arch.sie_block->mcn;
153439716a5SDavid Hildenbrand 		pgm_info->mon_code = vcpu->arch.sie_block->tecmc;
154439716a5SDavid Hildenbrand 		break;
155439716a5SDavid Hildenbrand 	case PGM_DATA:
156439716a5SDavid Hildenbrand 		pgm_info->data_exc_code = vcpu->arch.sie_block->dxc;
157439716a5SDavid Hildenbrand 		break;
158439716a5SDavid Hildenbrand 	case PGM_PROTECTION:
159439716a5SDavid Hildenbrand 		pgm_info->trans_exc_code = vcpu->arch.sie_block->tecmc;
160439716a5SDavid Hildenbrand 		pgm_info->exc_access_id  = vcpu->arch.sie_block->eai;
161439716a5SDavid Hildenbrand 		break;
162439716a5SDavid Hildenbrand 	default:
163439716a5SDavid Hildenbrand 		break;
164439716a5SDavid Hildenbrand 	}
165439716a5SDavid Hildenbrand 
166439716a5SDavid Hildenbrand 	if (vcpu->arch.sie_block->iprcc & PGM_PER) {
167439716a5SDavid Hildenbrand 		pgm_info->per_code = vcpu->arch.sie_block->perc;
168439716a5SDavid Hildenbrand 		pgm_info->per_atmid = vcpu->arch.sie_block->peratmid;
169439716a5SDavid Hildenbrand 		pgm_info->per_address = vcpu->arch.sie_block->peraddr;
170439716a5SDavid Hildenbrand 		pgm_info->per_access_id = vcpu->arch.sie_block->peraid;
171439716a5SDavid Hildenbrand 	}
172439716a5SDavid Hildenbrand }
173439716a5SDavid Hildenbrand 
174e325fe69SMichael Mueller /*
175e325fe69SMichael Mueller  * restore ITDB to program-interruption TDB in guest lowcore
176e325fe69SMichael Mueller  * and set TX abort indication if required
177e325fe69SMichael Mueller */
178e325fe69SMichael Mueller static int handle_itdb(struct kvm_vcpu *vcpu)
179e325fe69SMichael Mueller {
180e325fe69SMichael Mueller 	struct kvm_s390_itdb *itdb;
181e325fe69SMichael Mueller 	int rc;
182e325fe69SMichael Mueller 
183e325fe69SMichael Mueller 	if (!IS_TE_ENABLED(vcpu) || !IS_ITDB_VALID(vcpu))
184e325fe69SMichael Mueller 		return 0;
185e325fe69SMichael Mueller 	if (current->thread.per_flags & PER_FLAG_NO_TE)
186e325fe69SMichael Mueller 		return 0;
187e325fe69SMichael Mueller 	itdb = (struct kvm_s390_itdb *)vcpu->arch.sie_block->itdba;
188e325fe69SMichael Mueller 	rc = write_guest_lc(vcpu, __LC_PGM_TDB, itdb, sizeof(*itdb));
189e325fe69SMichael Mueller 	if (rc)
190e325fe69SMichael Mueller 		return rc;
191e325fe69SMichael Mueller 	memset(itdb, 0, sizeof(*itdb));
192e325fe69SMichael Mueller 
193e325fe69SMichael Mueller 	return 0;
194e325fe69SMichael Mueller }
195e325fe69SMichael Mueller 
19627291e21SDavid Hildenbrand #define per_event(vcpu) (vcpu->arch.sie_block->iprcc & PGM_PER)
19727291e21SDavid Hildenbrand 
198ba5c1e9bSCarsten Otte static int handle_prog(struct kvm_vcpu *vcpu)
199ba5c1e9bSCarsten Otte {
200439716a5SDavid Hildenbrand 	struct kvm_s390_pgm_info pgm_info;
2010040e7d2SHeiko Carstens 	int rc;
2020040e7d2SHeiko Carstens 
203ba5c1e9bSCarsten Otte 	vcpu->stat.exit_program_interruption++;
2047feb6bb8SMichael Mueller 
20527291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu) && per_event(vcpu)) {
20627291e21SDavid Hildenbrand 		kvm_s390_handle_per_event(vcpu);
20727291e21SDavid Hildenbrand 		/* the interrupt might have been filtered out completely */
20827291e21SDavid Hildenbrand 		if (vcpu->arch.sie_block->iprcc == 0)
20927291e21SDavid Hildenbrand 			return 0;
21027291e21SDavid Hildenbrand 	}
21127291e21SDavid Hildenbrand 
212e325fe69SMichael Mueller 	trace_kvm_s390_intercept_prog(vcpu, vcpu->arch.sie_block->iprcc);
213e325fe69SMichael Mueller 
214e325fe69SMichael Mueller 	rc = handle_itdb(vcpu);
2150040e7d2SHeiko Carstens 	if (rc)
2160040e7d2SHeiko Carstens 		return rc;
217439716a5SDavid Hildenbrand 
218e325fe69SMichael Mueller 	__extract_prog_irq(vcpu, &pgm_info);
219439716a5SDavid Hildenbrand 	return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
220ba5c1e9bSCarsten Otte }
221ba5c1e9bSCarsten Otte 
222ba5c1e9bSCarsten Otte static int handle_instruction_and_prog(struct kvm_vcpu *vcpu)
223ba5c1e9bSCarsten Otte {
224ba5c1e9bSCarsten Otte 	int rc, rc2;
225ba5c1e9bSCarsten Otte 
226ba5c1e9bSCarsten Otte 	vcpu->stat.exit_instr_and_program++;
227ba5c1e9bSCarsten Otte 	rc = handle_instruction(vcpu);
228ba5c1e9bSCarsten Otte 	rc2 = handle_prog(vcpu);
229ba5c1e9bSCarsten Otte 
230b8e660b8SHeiko Carstens 	if (rc == -EOPNOTSUPP)
231ba5c1e9bSCarsten Otte 		vcpu->arch.sie_block->icptcode = 0x04;
232ba5c1e9bSCarsten Otte 	if (rc)
233ba5c1e9bSCarsten Otte 		return rc;
234ba5c1e9bSCarsten Otte 	return rc2;
235ba5c1e9bSCarsten Otte }
236ba5c1e9bSCarsten Otte 
237*9a558ee3SThomas Huth /**
238*9a558ee3SThomas Huth  * Handle MOVE PAGE partial execution interception.
239*9a558ee3SThomas Huth  *
240*9a558ee3SThomas Huth  * This interception can only happen for guests with DAT disabled and
241*9a558ee3SThomas Huth  * addresses that are currently not mapped in the host. Thus we try to
242*9a558ee3SThomas Huth  * set up the mappings for the corresponding user pages here (or throw
243*9a558ee3SThomas Huth  * addressing exceptions in case of illegal guest addresses).
244*9a558ee3SThomas Huth  */
245*9a558ee3SThomas Huth static int handle_mvpg_pei(struct kvm_vcpu *vcpu)
246*9a558ee3SThomas Huth {
247*9a558ee3SThomas Huth 	unsigned long hostaddr, srcaddr, dstaddr;
248*9a558ee3SThomas Huth 	psw_t *psw = &vcpu->arch.sie_block->gpsw;
249*9a558ee3SThomas Huth 	struct mm_struct *mm = current->mm;
250*9a558ee3SThomas Huth 	int reg1, reg2, rc;
251*9a558ee3SThomas Huth 
252*9a558ee3SThomas Huth 	kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
253*9a558ee3SThomas Huth 	srcaddr = kvm_s390_real_to_abs(vcpu, vcpu->run->s.regs.gprs[reg2]);
254*9a558ee3SThomas Huth 	dstaddr = kvm_s390_real_to_abs(vcpu, vcpu->run->s.regs.gprs[reg1]);
255*9a558ee3SThomas Huth 
256*9a558ee3SThomas Huth 	/* Make sure that the source is paged-in */
257*9a558ee3SThomas Huth 	hostaddr = gmap_fault(srcaddr, vcpu->arch.gmap);
258*9a558ee3SThomas Huth 	if (IS_ERR_VALUE(hostaddr))
259*9a558ee3SThomas Huth 		return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
260*9a558ee3SThomas Huth 	down_read(&mm->mmap_sem);
261*9a558ee3SThomas Huth 	rc = get_user_pages(current, mm, hostaddr, 1, 0, 0, NULL, NULL);
262*9a558ee3SThomas Huth 	up_read(&mm->mmap_sem);
263*9a558ee3SThomas Huth 	if (rc < 0)
264*9a558ee3SThomas Huth 		return rc;
265*9a558ee3SThomas Huth 
266*9a558ee3SThomas Huth 	/* Make sure that the destination is paged-in */
267*9a558ee3SThomas Huth 	hostaddr = gmap_fault(dstaddr, vcpu->arch.gmap);
268*9a558ee3SThomas Huth 	if (IS_ERR_VALUE(hostaddr))
269*9a558ee3SThomas Huth 		return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
270*9a558ee3SThomas Huth 	down_read(&mm->mmap_sem);
271*9a558ee3SThomas Huth 	rc = get_user_pages(current, mm, hostaddr, 1, 1, 0, NULL, NULL);
272*9a558ee3SThomas Huth 	up_read(&mm->mmap_sem);
273*9a558ee3SThomas Huth 	if (rc < 0)
274*9a558ee3SThomas Huth 		return rc;
275*9a558ee3SThomas Huth 
276*9a558ee3SThomas Huth 	psw->addr = __rewind_psw(*psw, 4);
277*9a558ee3SThomas Huth 
278*9a558ee3SThomas Huth 	return 0;
279*9a558ee3SThomas Huth }
280*9a558ee3SThomas Huth 
281*9a558ee3SThomas Huth static int handle_partial_execution(struct kvm_vcpu *vcpu)
282*9a558ee3SThomas Huth {
283*9a558ee3SThomas Huth 	if (vcpu->arch.sie_block->ipa == 0xb254)	/* MVPG */
284*9a558ee3SThomas Huth 		return handle_mvpg_pei(vcpu);
285*9a558ee3SThomas Huth 
286*9a558ee3SThomas Huth 	return -EOPNOTSUPP;
287*9a558ee3SThomas Huth }
288*9a558ee3SThomas Huth 
289062d5e9bSChristian Borntraeger static const intercept_handler_t intercept_funcs[] = {
2908f2abe6aSChristian Borntraeger 	[0x00 >> 2] = handle_noop,
291ba5c1e9bSCarsten Otte 	[0x04 >> 2] = handle_instruction,
292ba5c1e9bSCarsten Otte 	[0x08 >> 2] = handle_prog,
293ba5c1e9bSCarsten Otte 	[0x0C >> 2] = handle_instruction_and_prog,
2948f2abe6aSChristian Borntraeger 	[0x10 >> 2] = handle_noop,
2958f2abe6aSChristian Borntraeger 	[0x14 >> 2] = handle_noop,
296fa6b7fe9SCornelia Huck 	[0x18 >> 2] = handle_noop,
297ba5c1e9bSCarsten Otte 	[0x1C >> 2] = kvm_s390_handle_wait,
2988f2abe6aSChristian Borntraeger 	[0x20 >> 2] = handle_validity,
2998f2abe6aSChristian Borntraeger 	[0x28 >> 2] = handle_stop,
300*9a558ee3SThomas Huth 	[0x38 >> 2] = handle_partial_execution,
3018f2abe6aSChristian Borntraeger };
3028f2abe6aSChristian Borntraeger 
3038f2abe6aSChristian Borntraeger int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
3048f2abe6aSChristian Borntraeger {
3058f2abe6aSChristian Borntraeger 	intercept_handler_t func;
3068f2abe6aSChristian Borntraeger 	u8 code = vcpu->arch.sie_block->icptcode;
3078f2abe6aSChristian Borntraeger 
308062d5e9bSChristian Borntraeger 	if (code & 3 || (code >> 2) >= ARRAY_SIZE(intercept_funcs))
309b8e660b8SHeiko Carstens 		return -EOPNOTSUPP;
3108f2abe6aSChristian Borntraeger 	func = intercept_funcs[code >> 2];
3118f2abe6aSChristian Borntraeger 	if (func)
3128f2abe6aSChristian Borntraeger 		return func(vcpu);
313b8e660b8SHeiko Carstens 	return -EOPNOTSUPP;
3148f2abe6aSChristian Borntraeger }
315