xref: /linux/arch/s390/kvm/intercept.c (revision 0eaeafa10f3b2bd027e95859a6785d4c7fcc174c)
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 
23ba5c1e9bSCarsten Otte static int handle_lctg(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 
33ba5c1e9bSCarsten Otte 	vcpu->stat.instruction_lctg++;
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 
41ba5c1e9bSCarsten Otte 	reg = reg1;
42ba5c1e9bSCarsten Otte 
43ba5c1e9bSCarsten Otte 	VCPU_EVENT(vcpu, 5, "lctg r1:%x, r3:%x,b2:%x,d2:%x", reg1, reg3, base2,
44ba5c1e9bSCarsten Otte 		   disp2);
45ba5c1e9bSCarsten Otte 
46ba5c1e9bSCarsten Otte 	do {
47ba5c1e9bSCarsten Otte 		rc = get_guest_u64(vcpu, useraddr,
48ba5c1e9bSCarsten Otte 				   &vcpu->arch.sie_block->gcr[reg]);
49ba5c1e9bSCarsten Otte 		if (rc == -EFAULT) {
50ba5c1e9bSCarsten Otte 			kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
51ba5c1e9bSCarsten Otte 			break;
52ba5c1e9bSCarsten Otte 		}
53ba5c1e9bSCarsten Otte 		useraddr += 8;
54ba5c1e9bSCarsten Otte 		if (reg == reg3)
55ba5c1e9bSCarsten Otte 			break;
56ba5c1e9bSCarsten Otte 		reg = (reg + 1) % 16;
57ba5c1e9bSCarsten Otte 	} while (1);
58ba5c1e9bSCarsten Otte 	return 0;
59ba5c1e9bSCarsten Otte }
60ba5c1e9bSCarsten Otte 
61ba5c1e9bSCarsten Otte static int handle_lctl(struct kvm_vcpu *vcpu)
62ba5c1e9bSCarsten Otte {
63ba5c1e9bSCarsten Otte 	int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
64ba5c1e9bSCarsten Otte 	int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
65ba5c1e9bSCarsten Otte 	int base2 = vcpu->arch.sie_block->ipb >> 28;
66ba5c1e9bSCarsten Otte 	int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
67ba5c1e9bSCarsten Otte 	u64 useraddr;
68ba5c1e9bSCarsten Otte 	u32 val = 0;
69ba5c1e9bSCarsten Otte 	int reg, rc;
70ba5c1e9bSCarsten Otte 
71ba5c1e9bSCarsten Otte 	vcpu->stat.instruction_lctl++;
72ba5c1e9bSCarsten Otte 
73ba5c1e9bSCarsten Otte 	useraddr = disp2;
74ba5c1e9bSCarsten Otte 	if (base2)
75ba5c1e9bSCarsten Otte 		useraddr += vcpu->arch.guest_gprs[base2];
76ba5c1e9bSCarsten Otte 
77ba5c1e9bSCarsten Otte 	VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x,b2:%x,d2:%x", reg1, reg3, base2,
78ba5c1e9bSCarsten Otte 		   disp2);
79ba5c1e9bSCarsten Otte 
80ba5c1e9bSCarsten Otte 	reg = reg1;
81ba5c1e9bSCarsten Otte 	do {
82ba5c1e9bSCarsten Otte 		rc = get_guest_u32(vcpu, useraddr, &val);
83ba5c1e9bSCarsten Otte 		if (rc == -EFAULT) {
84ba5c1e9bSCarsten Otte 			kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
85ba5c1e9bSCarsten Otte 			break;
86ba5c1e9bSCarsten Otte 		}
87ba5c1e9bSCarsten Otte 		vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
88ba5c1e9bSCarsten Otte 		vcpu->arch.sie_block->gcr[reg] |= val;
89ba5c1e9bSCarsten Otte 		useraddr += 4;
90ba5c1e9bSCarsten Otte 		if (reg == reg3)
91ba5c1e9bSCarsten Otte 			break;
92ba5c1e9bSCarsten Otte 		reg = (reg + 1) % 16;
93ba5c1e9bSCarsten Otte 	} while (1);
94ba5c1e9bSCarsten Otte 	return 0;
95ba5c1e9bSCarsten Otte }
96ba5c1e9bSCarsten Otte 
97ba5c1e9bSCarsten Otte static intercept_handler_t instruction_handlers[256] = {
98e28acfeaSChristian Borntraeger 	[0x83] = kvm_s390_handle_diag,
995288fbf0SChristian Borntraeger 	[0xae] = kvm_s390_handle_sigp,
100453423dcSChristian Borntraeger 	[0xb2] = kvm_s390_handle_priv,
101ba5c1e9bSCarsten Otte 	[0xb7] = handle_lctl,
102ba5c1e9bSCarsten Otte 	[0xeb] = handle_lctg,
103ba5c1e9bSCarsten Otte };
1048f2abe6aSChristian Borntraeger 
1058f2abe6aSChristian Borntraeger static int handle_noop(struct kvm_vcpu *vcpu)
1068f2abe6aSChristian Borntraeger {
1078f2abe6aSChristian Borntraeger 	switch (vcpu->arch.sie_block->icptcode) {
108*0eaeafa1SChristian Borntraeger 	case 0x0:
109*0eaeafa1SChristian Borntraeger 		vcpu->stat.exit_null++;
110*0eaeafa1SChristian Borntraeger 		break;
1118f2abe6aSChristian Borntraeger 	case 0x10:
1128f2abe6aSChristian Borntraeger 		vcpu->stat.exit_external_request++;
1138f2abe6aSChristian Borntraeger 		break;
1148f2abe6aSChristian Borntraeger 	case 0x14:
1158f2abe6aSChristian Borntraeger 		vcpu->stat.exit_external_interrupt++;
1168f2abe6aSChristian Borntraeger 		break;
1178f2abe6aSChristian Borntraeger 	default:
1188f2abe6aSChristian Borntraeger 		break; /* nothing */
1198f2abe6aSChristian Borntraeger 	}
1208f2abe6aSChristian Borntraeger 	return 0;
1218f2abe6aSChristian Borntraeger }
1228f2abe6aSChristian Borntraeger 
1238f2abe6aSChristian Borntraeger static int handle_stop(struct kvm_vcpu *vcpu)
1248f2abe6aSChristian Borntraeger {
1255288fbf0SChristian Borntraeger 	int rc;
1265288fbf0SChristian Borntraeger 
1278f2abe6aSChristian Borntraeger 	vcpu->stat.exit_stop_request++;
1288f2abe6aSChristian Borntraeger 	atomic_clear_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
1295288fbf0SChristian Borntraeger 	spin_lock_bh(&vcpu->arch.local_int.lock);
1305288fbf0SChristian Borntraeger 	if (vcpu->arch.local_int.action_bits & ACTION_STORE_ON_STOP) {
1315288fbf0SChristian Borntraeger 		vcpu->arch.local_int.action_bits &= ~ACTION_STORE_ON_STOP;
1325288fbf0SChristian Borntraeger 		rc = __kvm_s390_vcpu_store_status(vcpu,
1335288fbf0SChristian Borntraeger 						  KVM_S390_STORE_STATUS_NOADDR);
1345288fbf0SChristian Borntraeger 		if (rc >= 0)
1355288fbf0SChristian Borntraeger 			rc = -ENOTSUPP;
1365288fbf0SChristian Borntraeger 	}
1375288fbf0SChristian Borntraeger 
1385288fbf0SChristian Borntraeger 	if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) {
1395288fbf0SChristian Borntraeger 		vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP;
1405288fbf0SChristian Borntraeger 		VCPU_EVENT(vcpu, 3, "%s", "cpu stopped");
1415288fbf0SChristian Borntraeger 		rc = -ENOTSUPP;
1425288fbf0SChristian Borntraeger 	} else
1435288fbf0SChristian Borntraeger 		rc = 0;
1445288fbf0SChristian Borntraeger 	spin_unlock_bh(&vcpu->arch.local_int.lock);
1455288fbf0SChristian Borntraeger 	return rc;
1468f2abe6aSChristian Borntraeger }
1478f2abe6aSChristian Borntraeger 
1488f2abe6aSChristian Borntraeger static int handle_validity(struct kvm_vcpu *vcpu)
1498f2abe6aSChristian Borntraeger {
1508f2abe6aSChristian Borntraeger 	int viwhy = vcpu->arch.sie_block->ipb >> 16;
1518f2abe6aSChristian Borntraeger 	vcpu->stat.exit_validity++;
1528f2abe6aSChristian Borntraeger 	if (viwhy == 0x37) {
1538f2abe6aSChristian Borntraeger 		fault_in_pages_writeable((char __user *)
1548f2abe6aSChristian Borntraeger 					 vcpu->kvm->arch.guest_origin +
1558f2abe6aSChristian Borntraeger 					 vcpu->arch.sie_block->prefix,
1568f2abe6aSChristian Borntraeger 					 PAGE_SIZE);
1578f2abe6aSChristian Borntraeger 		return 0;
1588f2abe6aSChristian Borntraeger 	}
1598f2abe6aSChristian Borntraeger 	VCPU_EVENT(vcpu, 2, "unhandled validity intercept code %d",
1608f2abe6aSChristian Borntraeger 		   viwhy);
1618f2abe6aSChristian Borntraeger 	return -ENOTSUPP;
1628f2abe6aSChristian Borntraeger }
1638f2abe6aSChristian Borntraeger 
164ba5c1e9bSCarsten Otte static int handle_instruction(struct kvm_vcpu *vcpu)
165ba5c1e9bSCarsten Otte {
166ba5c1e9bSCarsten Otte 	intercept_handler_t handler;
167ba5c1e9bSCarsten Otte 
168ba5c1e9bSCarsten Otte 	vcpu->stat.exit_instruction++;
169ba5c1e9bSCarsten Otte 	handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
170ba5c1e9bSCarsten Otte 	if (handler)
171ba5c1e9bSCarsten Otte 		return handler(vcpu);
172ba5c1e9bSCarsten Otte 	return -ENOTSUPP;
173ba5c1e9bSCarsten Otte }
174ba5c1e9bSCarsten Otte 
175ba5c1e9bSCarsten Otte static int handle_prog(struct kvm_vcpu *vcpu)
176ba5c1e9bSCarsten Otte {
177ba5c1e9bSCarsten Otte 	vcpu->stat.exit_program_interruption++;
178ba5c1e9bSCarsten Otte 	return kvm_s390_inject_program_int(vcpu, vcpu->arch.sie_block->iprcc);
179ba5c1e9bSCarsten Otte }
180ba5c1e9bSCarsten Otte 
181ba5c1e9bSCarsten Otte static int handle_instruction_and_prog(struct kvm_vcpu *vcpu)
182ba5c1e9bSCarsten Otte {
183ba5c1e9bSCarsten Otte 	int rc, rc2;
184ba5c1e9bSCarsten Otte 
185ba5c1e9bSCarsten Otte 	vcpu->stat.exit_instr_and_program++;
186ba5c1e9bSCarsten Otte 	rc = handle_instruction(vcpu);
187ba5c1e9bSCarsten Otte 	rc2 = handle_prog(vcpu);
188ba5c1e9bSCarsten Otte 
189ba5c1e9bSCarsten Otte 	if (rc == -ENOTSUPP)
190ba5c1e9bSCarsten Otte 		vcpu->arch.sie_block->icptcode = 0x04;
191ba5c1e9bSCarsten Otte 	if (rc)
192ba5c1e9bSCarsten Otte 		return rc;
193ba5c1e9bSCarsten Otte 	return rc2;
194ba5c1e9bSCarsten Otte }
195ba5c1e9bSCarsten Otte 
1968f2abe6aSChristian Borntraeger static const intercept_handler_t intercept_funcs[0x48 >> 2] = {
1978f2abe6aSChristian Borntraeger 	[0x00 >> 2] = handle_noop,
198ba5c1e9bSCarsten Otte 	[0x04 >> 2] = handle_instruction,
199ba5c1e9bSCarsten Otte 	[0x08 >> 2] = handle_prog,
200ba5c1e9bSCarsten Otte 	[0x0C >> 2] = handle_instruction_and_prog,
2018f2abe6aSChristian Borntraeger 	[0x10 >> 2] = handle_noop,
2028f2abe6aSChristian Borntraeger 	[0x14 >> 2] = handle_noop,
203ba5c1e9bSCarsten Otte 	[0x1C >> 2] = kvm_s390_handle_wait,
2048f2abe6aSChristian Borntraeger 	[0x20 >> 2] = handle_validity,
2058f2abe6aSChristian Borntraeger 	[0x28 >> 2] = handle_stop,
2068f2abe6aSChristian Borntraeger };
2078f2abe6aSChristian Borntraeger 
2088f2abe6aSChristian Borntraeger int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
2098f2abe6aSChristian Borntraeger {
2108f2abe6aSChristian Borntraeger 	intercept_handler_t func;
2118f2abe6aSChristian Borntraeger 	u8 code = vcpu->arch.sie_block->icptcode;
2128f2abe6aSChristian Borntraeger 
2138f2abe6aSChristian Borntraeger 	if (code & 3 || code > 0x48)
2148f2abe6aSChristian Borntraeger 		return -ENOTSUPP;
2158f2abe6aSChristian Borntraeger 	func = intercept_funcs[code >> 2];
2168f2abe6aSChristian Borntraeger 	if (func)
2178f2abe6aSChristian Borntraeger 		return func(vcpu);
2188f2abe6aSChristian Borntraeger 	return -ENOTSUPP;
2198f2abe6aSChristian Borntraeger }
220