xref: /linux/arch/s390/kvm/intercept.c (revision 9ace903d171db7dc2fed96e44ac62b6f4c3ccb3d)
18f2abe6aSChristian Borntraeger /*
28f2abe6aSChristian Borntraeger  * intercept.c - in-kernel handling for sie intercepts
38f2abe6aSChristian Borntraeger  *
48f2abe6aSChristian Borntraeger  * Copyright IBM Corp. 2008
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>
198f2abe6aSChristian Borntraeger 
208f2abe6aSChristian Borntraeger #include "kvm-s390.h"
21ba5c1e9bSCarsten Otte #include "gaccess.h"
22ba5c1e9bSCarsten Otte 
23f5e10b09SChristian Borntraeger static int handle_lctlg(struct kvm_vcpu *vcpu)
24ba5c1e9bSCarsten Otte {
25ba5c1e9bSCarsten Otte 	int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
26ba5c1e9bSCarsten Otte 	int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
27ba5c1e9bSCarsten Otte 	int base2 = vcpu->arch.sie_block->ipb >> 28;
28ba5c1e9bSCarsten Otte 	int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16) +
29ba5c1e9bSCarsten Otte 			((vcpu->arch.sie_block->ipb & 0xff00) << 4);
30ba5c1e9bSCarsten Otte 	u64 useraddr;
31ba5c1e9bSCarsten Otte 	int reg, rc;
32ba5c1e9bSCarsten Otte 
33f5e10b09SChristian Borntraeger 	vcpu->stat.instruction_lctlg++;
34ba5c1e9bSCarsten Otte 	if ((vcpu->arch.sie_block->ipb & 0xff) != 0x2f)
35ba5c1e9bSCarsten Otte 		return -ENOTSUPP;
36ba5c1e9bSCarsten Otte 
37ba5c1e9bSCarsten Otte 	useraddr = disp2;
38ba5c1e9bSCarsten Otte 	if (base2)
39ba5c1e9bSCarsten Otte 		useraddr += vcpu->arch.guest_gprs[base2];
40ba5c1e9bSCarsten Otte 
415a00a5e7SChristian Borntraeger 	if (useraddr & 7)
425a00a5e7SChristian Borntraeger 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
435a00a5e7SChristian Borntraeger 
44ba5c1e9bSCarsten Otte 	reg = reg1;
45ba5c1e9bSCarsten Otte 
46f5e10b09SChristian Borntraeger 	VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x,b2:%x,d2:%x", reg1, reg3, base2,
47ba5c1e9bSCarsten Otte 		   disp2);
48ba5c1e9bSCarsten Otte 
49ba5c1e9bSCarsten Otte 	do {
50ba5c1e9bSCarsten Otte 		rc = get_guest_u64(vcpu, useraddr,
51ba5c1e9bSCarsten Otte 				   &vcpu->arch.sie_block->gcr[reg]);
52ba5c1e9bSCarsten Otte 		if (rc == -EFAULT) {
53ba5c1e9bSCarsten Otte 			kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
54ba5c1e9bSCarsten Otte 			break;
55ba5c1e9bSCarsten Otte 		}
56ba5c1e9bSCarsten Otte 		useraddr += 8;
57ba5c1e9bSCarsten Otte 		if (reg == reg3)
58ba5c1e9bSCarsten Otte 			break;
59ba5c1e9bSCarsten Otte 		reg = (reg + 1) % 16;
60ba5c1e9bSCarsten Otte 	} while (1);
61ba5c1e9bSCarsten Otte 	return 0;
62ba5c1e9bSCarsten Otte }
63ba5c1e9bSCarsten Otte 
64ba5c1e9bSCarsten Otte static int handle_lctl(struct kvm_vcpu *vcpu)
65ba5c1e9bSCarsten Otte {
66ba5c1e9bSCarsten Otte 	int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
67ba5c1e9bSCarsten Otte 	int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
68ba5c1e9bSCarsten Otte 	int base2 = vcpu->arch.sie_block->ipb >> 28;
69ba5c1e9bSCarsten Otte 	int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
70ba5c1e9bSCarsten Otte 	u64 useraddr;
71ba5c1e9bSCarsten Otte 	u32 val = 0;
72ba5c1e9bSCarsten Otte 	int reg, rc;
73ba5c1e9bSCarsten Otte 
74ba5c1e9bSCarsten Otte 	vcpu->stat.instruction_lctl++;
75ba5c1e9bSCarsten Otte 
76ba5c1e9bSCarsten Otte 	useraddr = disp2;
77ba5c1e9bSCarsten Otte 	if (base2)
78ba5c1e9bSCarsten Otte 		useraddr += vcpu->arch.guest_gprs[base2];
79ba5c1e9bSCarsten Otte 
805a00a5e7SChristian Borntraeger 	if (useraddr & 3)
815a00a5e7SChristian Borntraeger 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
825a00a5e7SChristian Borntraeger 
83ba5c1e9bSCarsten Otte 	VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x,b2:%x,d2:%x", reg1, reg3, base2,
84ba5c1e9bSCarsten Otte 		   disp2);
85ba5c1e9bSCarsten Otte 
86ba5c1e9bSCarsten Otte 	reg = reg1;
87ba5c1e9bSCarsten Otte 	do {
88ba5c1e9bSCarsten Otte 		rc = get_guest_u32(vcpu, useraddr, &val);
89ba5c1e9bSCarsten Otte 		if (rc == -EFAULT) {
90ba5c1e9bSCarsten Otte 			kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
91ba5c1e9bSCarsten Otte 			break;
92ba5c1e9bSCarsten Otte 		}
93ba5c1e9bSCarsten Otte 		vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
94ba5c1e9bSCarsten Otte 		vcpu->arch.sie_block->gcr[reg] |= val;
95ba5c1e9bSCarsten Otte 		useraddr += 4;
96ba5c1e9bSCarsten Otte 		if (reg == reg3)
97ba5c1e9bSCarsten Otte 			break;
98ba5c1e9bSCarsten Otte 		reg = (reg + 1) % 16;
99ba5c1e9bSCarsten Otte 	} while (1);
100ba5c1e9bSCarsten Otte 	return 0;
101ba5c1e9bSCarsten Otte }
102ba5c1e9bSCarsten Otte 
103ba5c1e9bSCarsten Otte static intercept_handler_t instruction_handlers[256] = {
104e28acfeaSChristian Borntraeger 	[0x83] = kvm_s390_handle_diag,
1055288fbf0SChristian Borntraeger 	[0xae] = kvm_s390_handle_sigp,
10670455a36SChristian Borntraeger 	[0xb2] = kvm_s390_handle_b2,
107ba5c1e9bSCarsten Otte 	[0xb7] = handle_lctl,
108f5e10b09SChristian Borntraeger 	[0xeb] = handle_lctlg,
109ba5c1e9bSCarsten Otte };
1108f2abe6aSChristian Borntraeger 
1118f2abe6aSChristian Borntraeger static int handle_noop(struct kvm_vcpu *vcpu)
1128f2abe6aSChristian Borntraeger {
1138f2abe6aSChristian Borntraeger 	switch (vcpu->arch.sie_block->icptcode) {
1140eaeafa1SChristian Borntraeger 	case 0x0:
1150eaeafa1SChristian Borntraeger 		vcpu->stat.exit_null++;
1160eaeafa1SChristian Borntraeger 		break;
1178f2abe6aSChristian Borntraeger 	case 0x10:
1188f2abe6aSChristian Borntraeger 		vcpu->stat.exit_external_request++;
1198f2abe6aSChristian Borntraeger 		break;
1208f2abe6aSChristian Borntraeger 	case 0x14:
1218f2abe6aSChristian Borntraeger 		vcpu->stat.exit_external_interrupt++;
1228f2abe6aSChristian Borntraeger 		break;
1238f2abe6aSChristian Borntraeger 	default:
1248f2abe6aSChristian Borntraeger 		break; /* nothing */
1258f2abe6aSChristian Borntraeger 	}
1268f2abe6aSChristian Borntraeger 	return 0;
1278f2abe6aSChristian Borntraeger }
1288f2abe6aSChristian Borntraeger 
1298f2abe6aSChristian Borntraeger static int handle_stop(struct kvm_vcpu *vcpu)
1308f2abe6aSChristian Borntraeger {
131*9ace903dSChristian Ehrhardt 	int rc = 0;
1325288fbf0SChristian Borntraeger 
1338f2abe6aSChristian Borntraeger 	vcpu->stat.exit_stop_request++;
1348f2abe6aSChristian Borntraeger 	atomic_clear_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
1355288fbf0SChristian Borntraeger 	spin_lock_bh(&vcpu->arch.local_int.lock);
1365288fbf0SChristian Borntraeger 	if (vcpu->arch.local_int.action_bits & ACTION_STORE_ON_STOP) {
1375288fbf0SChristian Borntraeger 		vcpu->arch.local_int.action_bits &= ~ACTION_STORE_ON_STOP;
1385288fbf0SChristian Borntraeger 		rc = __kvm_s390_vcpu_store_status(vcpu,
1395288fbf0SChristian Borntraeger 						  KVM_S390_STORE_STATUS_NOADDR);
1405288fbf0SChristian Borntraeger 		if (rc >= 0)
1415288fbf0SChristian Borntraeger 			rc = -ENOTSUPP;
1425288fbf0SChristian Borntraeger 	}
1435288fbf0SChristian Borntraeger 
144*9ace903dSChristian Ehrhardt 	if (vcpu->arch.local_int.action_bits & ACTION_RELOADVCPU_ON_STOP) {
145*9ace903dSChristian Ehrhardt 		vcpu->arch.local_int.action_bits &= ~ACTION_RELOADVCPU_ON_STOP;
146*9ace903dSChristian Ehrhardt 		rc = SIE_INTERCEPT_RERUNVCPU;
147*9ace903dSChristian Ehrhardt 		vcpu->run->exit_reason = KVM_EXIT_INTR;
148*9ace903dSChristian Ehrhardt 	}
149*9ace903dSChristian Ehrhardt 
1505288fbf0SChristian Borntraeger 	if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) {
1515288fbf0SChristian Borntraeger 		vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP;
1525288fbf0SChristian Borntraeger 		VCPU_EVENT(vcpu, 3, "%s", "cpu stopped");
1535288fbf0SChristian Borntraeger 		rc = -ENOTSUPP;
154*9ace903dSChristian Ehrhardt 	}
155*9ace903dSChristian Ehrhardt 
1565288fbf0SChristian Borntraeger 	spin_unlock_bh(&vcpu->arch.local_int.lock);
1575288fbf0SChristian Borntraeger 	return rc;
1588f2abe6aSChristian Borntraeger }
1598f2abe6aSChristian Borntraeger 
1608f2abe6aSChristian Borntraeger static int handle_validity(struct kvm_vcpu *vcpu)
1618f2abe6aSChristian Borntraeger {
1628f2abe6aSChristian Borntraeger 	int viwhy = vcpu->arch.sie_block->ipb >> 16;
1633edbcff9SCarsten Otte 	int rc;
1643edbcff9SCarsten Otte 
1658f2abe6aSChristian Borntraeger 	vcpu->stat.exit_validity++;
1663edbcff9SCarsten Otte 	if ((viwhy == 0x37) && (vcpu->arch.sie_block->prefix
1673edbcff9SCarsten Otte 		<= vcpu->kvm->arch.guest_memsize - 2*PAGE_SIZE)){
1683edbcff9SCarsten Otte 		rc = fault_in_pages_writeable((char __user *)
1698f2abe6aSChristian Borntraeger 			 vcpu->kvm->arch.guest_origin +
1708f2abe6aSChristian Borntraeger 			 vcpu->arch.sie_block->prefix,
1713edbcff9SCarsten Otte 			 2*PAGE_SIZE);
1723edbcff9SCarsten Otte 		if (rc)
1733edbcff9SCarsten Otte 			/* user will receive sigsegv, exit to user */
1743edbcff9SCarsten Otte 			rc = -ENOTSUPP;
1753edbcff9SCarsten Otte 	} else
1763edbcff9SCarsten Otte 		rc = -ENOTSUPP;
1773edbcff9SCarsten Otte 
1783edbcff9SCarsten Otte 	if (rc)
1798f2abe6aSChristian Borntraeger 		VCPU_EVENT(vcpu, 2, "unhandled validity intercept code %d",
1808f2abe6aSChristian Borntraeger 			   viwhy);
1813edbcff9SCarsten Otte 	return rc;
1828f2abe6aSChristian Borntraeger }
1838f2abe6aSChristian Borntraeger 
184ba5c1e9bSCarsten Otte static int handle_instruction(struct kvm_vcpu *vcpu)
185ba5c1e9bSCarsten Otte {
186ba5c1e9bSCarsten Otte 	intercept_handler_t handler;
187ba5c1e9bSCarsten Otte 
188ba5c1e9bSCarsten Otte 	vcpu->stat.exit_instruction++;
189ba5c1e9bSCarsten Otte 	handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
190ba5c1e9bSCarsten Otte 	if (handler)
191ba5c1e9bSCarsten Otte 		return handler(vcpu);
192ba5c1e9bSCarsten Otte 	return -ENOTSUPP;
193ba5c1e9bSCarsten Otte }
194ba5c1e9bSCarsten Otte 
195ba5c1e9bSCarsten Otte static int handle_prog(struct kvm_vcpu *vcpu)
196ba5c1e9bSCarsten Otte {
197ba5c1e9bSCarsten Otte 	vcpu->stat.exit_program_interruption++;
198ba5c1e9bSCarsten Otte 	return kvm_s390_inject_program_int(vcpu, vcpu->arch.sie_block->iprcc);
199ba5c1e9bSCarsten Otte }
200ba5c1e9bSCarsten Otte 
201ba5c1e9bSCarsten Otte static int handle_instruction_and_prog(struct kvm_vcpu *vcpu)
202ba5c1e9bSCarsten Otte {
203ba5c1e9bSCarsten Otte 	int rc, rc2;
204ba5c1e9bSCarsten Otte 
205ba5c1e9bSCarsten Otte 	vcpu->stat.exit_instr_and_program++;
206ba5c1e9bSCarsten Otte 	rc = handle_instruction(vcpu);
207ba5c1e9bSCarsten Otte 	rc2 = handle_prog(vcpu);
208ba5c1e9bSCarsten Otte 
209ba5c1e9bSCarsten Otte 	if (rc == -ENOTSUPP)
210ba5c1e9bSCarsten Otte 		vcpu->arch.sie_block->icptcode = 0x04;
211ba5c1e9bSCarsten Otte 	if (rc)
212ba5c1e9bSCarsten Otte 		return rc;
213ba5c1e9bSCarsten Otte 	return rc2;
214ba5c1e9bSCarsten Otte }
215ba5c1e9bSCarsten Otte 
2168f2abe6aSChristian Borntraeger static const intercept_handler_t intercept_funcs[0x48 >> 2] = {
2178f2abe6aSChristian Borntraeger 	[0x00 >> 2] = handle_noop,
218ba5c1e9bSCarsten Otte 	[0x04 >> 2] = handle_instruction,
219ba5c1e9bSCarsten Otte 	[0x08 >> 2] = handle_prog,
220ba5c1e9bSCarsten Otte 	[0x0C >> 2] = handle_instruction_and_prog,
2218f2abe6aSChristian Borntraeger 	[0x10 >> 2] = handle_noop,
2228f2abe6aSChristian Borntraeger 	[0x14 >> 2] = handle_noop,
223ba5c1e9bSCarsten Otte 	[0x1C >> 2] = kvm_s390_handle_wait,
2248f2abe6aSChristian Borntraeger 	[0x20 >> 2] = handle_validity,
2258f2abe6aSChristian Borntraeger 	[0x28 >> 2] = handle_stop,
2268f2abe6aSChristian Borntraeger };
2278f2abe6aSChristian Borntraeger 
2288f2abe6aSChristian Borntraeger int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
2298f2abe6aSChristian Borntraeger {
2308f2abe6aSChristian Borntraeger 	intercept_handler_t func;
2318f2abe6aSChristian Borntraeger 	u8 code = vcpu->arch.sie_block->icptcode;
2328f2abe6aSChristian Borntraeger 
2338f2abe6aSChristian Borntraeger 	if (code & 3 || code > 0x48)
2348f2abe6aSChristian Borntraeger 		return -ENOTSUPP;
2358f2abe6aSChristian Borntraeger 	func = intercept_funcs[code >> 2];
2368f2abe6aSChristian Borntraeger 	if (func)
2378f2abe6aSChristian Borntraeger 		return func(vcpu);
2388f2abe6aSChristian Borntraeger 	return -ENOTSUPP;
2398f2abe6aSChristian Borntraeger }
240