xref: /linux/arch/s390/kvm/intercept.c (revision e325fe69aa37b485635521568651642791d6d140)
18f2abe6aSChristian Borntraeger /*
2a53c8fabSHeiko Carstens  * in-kernel handling for sie intercepts
38f2abe6aSChristian Borntraeger  *
4628eb9b8SChristian Ehrhardt  * Copyright IBM Corp. 2008, 2009
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 
174*e325fe69SMichael Mueller /*
175*e325fe69SMichael Mueller  * restore ITDB to program-interruption TDB in guest lowcore
176*e325fe69SMichael Mueller  * and set TX abort indication if required
177*e325fe69SMichael Mueller */
178*e325fe69SMichael Mueller static int handle_itdb(struct kvm_vcpu *vcpu)
179*e325fe69SMichael Mueller {
180*e325fe69SMichael Mueller 	struct kvm_s390_itdb *itdb;
181*e325fe69SMichael Mueller 	int rc;
182*e325fe69SMichael Mueller 
183*e325fe69SMichael Mueller 	if (!IS_TE_ENABLED(vcpu) || !IS_ITDB_VALID(vcpu))
184*e325fe69SMichael Mueller 		return 0;
185*e325fe69SMichael Mueller 	if (current->thread.per_flags & PER_FLAG_NO_TE)
186*e325fe69SMichael Mueller 		return 0;
187*e325fe69SMichael Mueller 	itdb = (struct kvm_s390_itdb *)vcpu->arch.sie_block->itdba;
188*e325fe69SMichael Mueller 	rc = write_guest_lc(vcpu, __LC_PGM_TDB, itdb, sizeof(*itdb));
189*e325fe69SMichael Mueller 	if (rc)
190*e325fe69SMichael Mueller 		return rc;
191*e325fe69SMichael Mueller 	memset(itdb, 0, sizeof(*itdb));
192*e325fe69SMichael Mueller 
193*e325fe69SMichael Mueller 	return 0;
194*e325fe69SMichael Mueller }
195*e325fe69SMichael 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 
212*e325fe69SMichael Mueller 	trace_kvm_s390_intercept_prog(vcpu, vcpu->arch.sie_block->iprcc);
213*e325fe69SMichael Mueller 
214*e325fe69SMichael Mueller 	rc = handle_itdb(vcpu);
2150040e7d2SHeiko Carstens 	if (rc)
2160040e7d2SHeiko Carstens 		return rc;
217439716a5SDavid Hildenbrand 
218*e325fe69SMichael 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 
237062d5e9bSChristian Borntraeger static const intercept_handler_t intercept_funcs[] = {
2388f2abe6aSChristian Borntraeger 	[0x00 >> 2] = handle_noop,
239ba5c1e9bSCarsten Otte 	[0x04 >> 2] = handle_instruction,
240ba5c1e9bSCarsten Otte 	[0x08 >> 2] = handle_prog,
241ba5c1e9bSCarsten Otte 	[0x0C >> 2] = handle_instruction_and_prog,
2428f2abe6aSChristian Borntraeger 	[0x10 >> 2] = handle_noop,
2438f2abe6aSChristian Borntraeger 	[0x14 >> 2] = handle_noop,
244fa6b7fe9SCornelia Huck 	[0x18 >> 2] = handle_noop,
245ba5c1e9bSCarsten Otte 	[0x1C >> 2] = kvm_s390_handle_wait,
2468f2abe6aSChristian Borntraeger 	[0x20 >> 2] = handle_validity,
2478f2abe6aSChristian Borntraeger 	[0x28 >> 2] = handle_stop,
2488f2abe6aSChristian Borntraeger };
2498f2abe6aSChristian Borntraeger 
2508f2abe6aSChristian Borntraeger int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
2518f2abe6aSChristian Borntraeger {
2528f2abe6aSChristian Borntraeger 	intercept_handler_t func;
2538f2abe6aSChristian Borntraeger 	u8 code = vcpu->arch.sie_block->icptcode;
2548f2abe6aSChristian Borntraeger 
255062d5e9bSChristian Borntraeger 	if (code & 3 || (code >> 2) >= ARRAY_SIZE(intercept_funcs))
256b8e660b8SHeiko Carstens 		return -EOPNOTSUPP;
2578f2abe6aSChristian Borntraeger 	func = intercept_funcs[code >> 2];
2588f2abe6aSChristian Borntraeger 	if (func)
2598f2abe6aSChristian Borntraeger 		return func(vcpu);
260b8e660b8SHeiko Carstens 	return -EOPNOTSUPP;
2618f2abe6aSChristian Borntraeger }
262