xref: /openbmc/linux/arch/s390/kvm/diag.c (revision f7a960affc6e5a33e8c7fcef065affc4f0461041)
1e28acfeaSChristian Borntraeger /*
2a53c8fabSHeiko Carstens  * handling diagnose instructions
3e28acfeaSChristian Borntraeger  *
4388186bcSChristian Borntraeger  * Copyright IBM Corp. 2008, 2011
5e28acfeaSChristian Borntraeger  *
6e28acfeaSChristian Borntraeger  * This program is free software; you can redistribute it and/or modify
7e28acfeaSChristian Borntraeger  * it under the terms of the GNU General Public License (version 2 only)
8e28acfeaSChristian Borntraeger  * as published by the Free Software Foundation.
9e28acfeaSChristian Borntraeger  *
10e28acfeaSChristian Borntraeger  *    Author(s): Carsten Otte <cotte@de.ibm.com>
11e28acfeaSChristian Borntraeger  *               Christian Borntraeger <borntraeger@de.ibm.com>
12e28acfeaSChristian Borntraeger  */
13e28acfeaSChristian Borntraeger 
14e28acfeaSChristian Borntraeger #include <linux/kvm.h>
15e28acfeaSChristian Borntraeger #include <linux/kvm_host.h>
16deedabb2SMartin Schwidefsky #include <asm/pgalloc.h>
1710ccaa1eSCornelia Huck #include <asm/virtio-ccw.h>
18e28acfeaSChristian Borntraeger #include "kvm-s390.h"
195786fffaSCornelia Huck #include "trace.h"
20ade38c31SCornelia Huck #include "trace-s390.h"
213c038e6bSDominik Dingel #include "gaccess.h"
22e28acfeaSChristian Borntraeger 
23388186bcSChristian Borntraeger static int diag_release_pages(struct kvm_vcpu *vcpu)
24388186bcSChristian Borntraeger {
25388186bcSChristian Borntraeger 	unsigned long start, end;
26fda902cbSMichael Mueller 	unsigned long prefix  = kvm_s390_get_prefix(vcpu);
27388186bcSChristian Borntraeger 
285a32c1afSChristian Borntraeger 	start = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
295a32c1afSChristian Borntraeger 	end = vcpu->run->s.regs.gprs[vcpu->arch.sie_block->ipa & 0xf] + 4096;
30388186bcSChristian Borntraeger 
31*f7a960afSChristian Borntraeger 	if (start & ~PAGE_MASK || end & ~PAGE_MASK || start >= end
32388186bcSChristian Borntraeger 	    || start < 2 * PAGE_SIZE)
33388186bcSChristian Borntraeger 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
34388186bcSChristian Borntraeger 
35388186bcSChristian Borntraeger 	VCPU_EVENT(vcpu, 5, "diag release pages %lX %lX", start, end);
36388186bcSChristian Borntraeger 	vcpu->stat.diagnose_10++;
37388186bcSChristian Borntraeger 
38*f7a960afSChristian Borntraeger 	/*
39*f7a960afSChristian Borntraeger 	 * We checked for start >= end above, so lets check for the
40*f7a960afSChristian Borntraeger 	 * fast path (no prefix swap page involved)
41*f7a960afSChristian Borntraeger 	 */
42*f7a960afSChristian Borntraeger 	if (end <= prefix || start >= prefix + 2 * PAGE_SIZE) {
436e0a0431SMartin Schwidefsky 		gmap_discard(vcpu->arch.gmap, start, end);
44388186bcSChristian Borntraeger 	} else {
45*f7a960afSChristian Borntraeger 		/*
46*f7a960afSChristian Borntraeger 		 * This is slow path.  gmap_discard will check for start
47*f7a960afSChristian Borntraeger 		 * so lets split this into before prefix, prefix, after
48*f7a960afSChristian Borntraeger 		 * prefix and let gmap_discard make some of these calls
49*f7a960afSChristian Borntraeger 		 * NOPs.
50*f7a960afSChristian Borntraeger 		 */
516e0a0431SMartin Schwidefsky 		gmap_discard(vcpu->arch.gmap, start, prefix);
52*f7a960afSChristian Borntraeger 		if (start <= prefix)
53*f7a960afSChristian Borntraeger 			gmap_discard(vcpu->arch.gmap, 0, 4096);
54*f7a960afSChristian Borntraeger 		if (end > prefix + 4096)
55*f7a960afSChristian Borntraeger 			gmap_discard(vcpu->arch.gmap, 4096, 8192);
56*f7a960afSChristian Borntraeger 		gmap_discard(vcpu->arch.gmap, prefix + 2 * PAGE_SIZE, end);
57388186bcSChristian Borntraeger 	}
58388186bcSChristian Borntraeger 	return 0;
59388186bcSChristian Borntraeger }
60388186bcSChristian Borntraeger 
613c038e6bSDominik Dingel static int __diag_page_ref_service(struct kvm_vcpu *vcpu)
623c038e6bSDominik Dingel {
633c038e6bSDominik Dingel 	struct prs_parm {
643c038e6bSDominik Dingel 		u16 code;
653c038e6bSDominik Dingel 		u16 subcode;
663c038e6bSDominik Dingel 		u16 parm_len;
673c038e6bSDominik Dingel 		u16 parm_version;
683c038e6bSDominik Dingel 		u64 token_addr;
693c038e6bSDominik Dingel 		u64 select_mask;
703c038e6bSDominik Dingel 		u64 compare_mask;
713c038e6bSDominik Dingel 		u64 zarch;
723c038e6bSDominik Dingel 	};
733c038e6bSDominik Dingel 	struct prs_parm parm;
743c038e6bSDominik Dingel 	int rc;
753c038e6bSDominik Dingel 	u16 rx = (vcpu->arch.sie_block->ipa & 0xf0) >> 4;
763c038e6bSDominik Dingel 	u16 ry = (vcpu->arch.sie_block->ipa & 0x0f);
773c038e6bSDominik Dingel 
783c038e6bSDominik Dingel 	if (vcpu->run->s.regs.gprs[rx] & 7)
793c038e6bSDominik Dingel 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
8081480cc1SHeiko Carstens 	rc = read_guest(vcpu, vcpu->run->s.regs.gprs[rx], &parm, sizeof(parm));
8181480cc1SHeiko Carstens 	if (rc)
8281480cc1SHeiko Carstens 		return kvm_s390_inject_prog_cond(vcpu, rc);
833c038e6bSDominik Dingel 	if (parm.parm_version != 2 || parm.parm_len < 5 || parm.code != 0x258)
843c038e6bSDominik Dingel 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
853c038e6bSDominik Dingel 
863c038e6bSDominik Dingel 	switch (parm.subcode) {
873c038e6bSDominik Dingel 	case 0: /* TOKEN */
883c038e6bSDominik Dingel 		if (vcpu->arch.pfault_token != KVM_S390_PFAULT_TOKEN_INVALID) {
893c038e6bSDominik Dingel 			/*
903c038e6bSDominik Dingel 			 * If the pagefault handshake is already activated,
913c038e6bSDominik Dingel 			 * the token must not be changed.  We have to return
923c038e6bSDominik Dingel 			 * decimal 8 instead, as mandated in SC24-6084.
933c038e6bSDominik Dingel 			 */
943c038e6bSDominik Dingel 			vcpu->run->s.regs.gprs[ry] = 8;
953c038e6bSDominik Dingel 			return 0;
963c038e6bSDominik Dingel 		}
973c038e6bSDominik Dingel 
983c038e6bSDominik Dingel 		if ((parm.compare_mask & parm.select_mask) != parm.compare_mask ||
993c038e6bSDominik Dingel 		    parm.token_addr & 7 || parm.zarch != 0x8000000000000000ULL)
1003c038e6bSDominik Dingel 			return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1013c038e6bSDominik Dingel 
10281480cc1SHeiko Carstens 		if (kvm_is_error_gpa(vcpu->kvm, parm.token_addr))
1033c038e6bSDominik Dingel 			return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1043c038e6bSDominik Dingel 
1053c038e6bSDominik Dingel 		vcpu->arch.pfault_token = parm.token_addr;
1063c038e6bSDominik Dingel 		vcpu->arch.pfault_select = parm.select_mask;
1073c038e6bSDominik Dingel 		vcpu->arch.pfault_compare = parm.compare_mask;
1083c038e6bSDominik Dingel 		vcpu->run->s.regs.gprs[ry] = 0;
1093c038e6bSDominik Dingel 		rc = 0;
1103c038e6bSDominik Dingel 		break;
1113c038e6bSDominik Dingel 	case 1: /*
1123c038e6bSDominik Dingel 		 * CANCEL
1133c038e6bSDominik Dingel 		 * Specification allows to let already pending tokens survive
1143c038e6bSDominik Dingel 		 * the cancel, therefore to reduce code complexity, we assume
1153c038e6bSDominik Dingel 		 * all outstanding tokens are already pending.
1163c038e6bSDominik Dingel 		 */
1173c038e6bSDominik Dingel 		if (parm.token_addr || parm.select_mask ||
1183c038e6bSDominik Dingel 		    parm.compare_mask || parm.zarch)
1193c038e6bSDominik Dingel 			return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1203c038e6bSDominik Dingel 
1213c038e6bSDominik Dingel 		vcpu->run->s.regs.gprs[ry] = 0;
1223c038e6bSDominik Dingel 		/*
1233c038e6bSDominik Dingel 		 * If the pfault handling was not established or is already
1243c038e6bSDominik Dingel 		 * canceled SC24-6084 requests to return decimal 4.
1253c038e6bSDominik Dingel 		 */
1263c038e6bSDominik Dingel 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
1273c038e6bSDominik Dingel 			vcpu->run->s.regs.gprs[ry] = 4;
1283c038e6bSDominik Dingel 		else
1293c038e6bSDominik Dingel 			vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
1303c038e6bSDominik Dingel 
1313c038e6bSDominik Dingel 		rc = 0;
1323c038e6bSDominik Dingel 		break;
1333c038e6bSDominik Dingel 	default:
1343c038e6bSDominik Dingel 		rc = -EOPNOTSUPP;
1353c038e6bSDominik Dingel 		break;
1363c038e6bSDominik Dingel 	}
1373c038e6bSDominik Dingel 
1383c038e6bSDominik Dingel 	return rc;
1393c038e6bSDominik Dingel }
1403c038e6bSDominik Dingel 
141e28acfeaSChristian Borntraeger static int __diag_time_slice_end(struct kvm_vcpu *vcpu)
142e28acfeaSChristian Borntraeger {
143e28acfeaSChristian Borntraeger 	VCPU_EVENT(vcpu, 5, "%s", "diag time slice end");
144e28acfeaSChristian Borntraeger 	vcpu->stat.diagnose_44++;
1458733ac36SChristian Borntraeger 	kvm_vcpu_on_spin(vcpu);
146e28acfeaSChristian Borntraeger 	return 0;
147e28acfeaSChristian Borntraeger }
148e28acfeaSChristian Borntraeger 
14941628d33SKonstantin Weitz static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu)
15041628d33SKonstantin Weitz {
15141628d33SKonstantin Weitz 	struct kvm *kvm = vcpu->kvm;
15241628d33SKonstantin Weitz 	struct kvm_vcpu *tcpu;
15341628d33SKonstantin Weitz 	int tid;
15441628d33SKonstantin Weitz 	int i;
15541628d33SKonstantin Weitz 
15641628d33SKonstantin Weitz 	tid = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
15741628d33SKonstantin Weitz 	vcpu->stat.diagnose_9c++;
15841628d33SKonstantin Weitz 	VCPU_EVENT(vcpu, 5, "diag time slice end directed to %d", tid);
15941628d33SKonstantin Weitz 
16041628d33SKonstantin Weitz 	if (tid == vcpu->vcpu_id)
16141628d33SKonstantin Weitz 		return 0;
16241628d33SKonstantin Weitz 
16341628d33SKonstantin Weitz 	kvm_for_each_vcpu(i, tcpu, kvm)
16441628d33SKonstantin Weitz 		if (tcpu->vcpu_id == tid) {
16541628d33SKonstantin Weitz 			kvm_vcpu_yield_to(tcpu);
16641628d33SKonstantin Weitz 			break;
16741628d33SKonstantin Weitz 		}
16841628d33SKonstantin Weitz 
16941628d33SKonstantin Weitz 	return 0;
17041628d33SKonstantin Weitz }
17141628d33SKonstantin Weitz 
172e28acfeaSChristian Borntraeger static int __diag_ipl_functions(struct kvm_vcpu *vcpu)
173e28acfeaSChristian Borntraeger {
174e28acfeaSChristian Borntraeger 	unsigned int reg = vcpu->arch.sie_block->ipa & 0xf;
1755a32c1afSChristian Borntraeger 	unsigned long subcode = vcpu->run->s.regs.gprs[reg] & 0xffff;
176e28acfeaSChristian Borntraeger 
177e28acfeaSChristian Borntraeger 	VCPU_EVENT(vcpu, 5, "diag ipl functions, subcode %lx", subcode);
178e28acfeaSChristian Borntraeger 	switch (subcode) {
179e28acfeaSChristian Borntraeger 	case 3:
180e28acfeaSChristian Borntraeger 		vcpu->run->s390_reset_flags = KVM_S390_RESET_CLEAR;
181e28acfeaSChristian Borntraeger 		break;
182e28acfeaSChristian Borntraeger 	case 4:
183e28acfeaSChristian Borntraeger 		vcpu->run->s390_reset_flags = 0;
184e28acfeaSChristian Borntraeger 		break;
185e28acfeaSChristian Borntraeger 	default:
186b8e660b8SHeiko Carstens 		return -EOPNOTSUPP;
187e28acfeaSChristian Borntraeger 	}
188e28acfeaSChristian Borntraeger 
1896352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
1906852d7b6SDavid Hildenbrand 		kvm_s390_vcpu_stop(vcpu);
191e28acfeaSChristian Borntraeger 	vcpu->run->s390_reset_flags |= KVM_S390_RESET_SUBSYSTEM;
192e28acfeaSChristian Borntraeger 	vcpu->run->s390_reset_flags |= KVM_S390_RESET_IPL;
193e28acfeaSChristian Borntraeger 	vcpu->run->s390_reset_flags |= KVM_S390_RESET_CPU_INIT;
194e28acfeaSChristian Borntraeger 	vcpu->run->exit_reason = KVM_EXIT_S390_RESET;
19533e19115SHeiko Carstens 	VCPU_EVENT(vcpu, 3, "requesting userspace resets %llx",
196e28acfeaSChristian Borntraeger 	  vcpu->run->s390_reset_flags);
197ade38c31SCornelia Huck 	trace_kvm_s390_request_resets(vcpu->run->s390_reset_flags);
198e28acfeaSChristian Borntraeger 	return -EREMOTE;
199e28acfeaSChristian Borntraeger }
200e28acfeaSChristian Borntraeger 
20110ccaa1eSCornelia Huck static int __diag_virtio_hypercall(struct kvm_vcpu *vcpu)
20210ccaa1eSCornelia Huck {
203800c1065SThomas Huth 	int ret;
20410ccaa1eSCornelia Huck 
20510ccaa1eSCornelia Huck 	/* No virtio-ccw notification? Get out quickly. */
20610ccaa1eSCornelia Huck 	if (!vcpu->kvm->arch.css_support ||
20710ccaa1eSCornelia Huck 	    (vcpu->run->s.regs.gprs[1] != KVM_S390_VIRTIO_CCW_NOTIFY))
20810ccaa1eSCornelia Huck 		return -EOPNOTSUPP;
20910ccaa1eSCornelia Huck 
21010ccaa1eSCornelia Huck 	/*
21110ccaa1eSCornelia Huck 	 * The layout is as follows:
21210ccaa1eSCornelia Huck 	 * - gpr 2 contains the subchannel id (passed as addr)
21310ccaa1eSCornelia Huck 	 * - gpr 3 contains the virtqueue index (passed as datamatch)
21485dfe87eSCornelia Huck 	 * - gpr 4 contains the index on the bus (optionally)
21510ccaa1eSCornelia Huck 	 */
21685dfe87eSCornelia Huck 	ret = kvm_io_bus_write_cookie(vcpu->kvm, KVM_VIRTIO_CCW_NOTIFY_BUS,
217ff1f3cb4SDominik Dingel 				      vcpu->run->s.regs.gprs[2] & 0xffffffff,
21885dfe87eSCornelia Huck 				      8, &vcpu->run->s.regs.gprs[3],
21985dfe87eSCornelia Huck 				      vcpu->run->s.regs.gprs[4]);
22085dfe87eSCornelia Huck 
22185dfe87eSCornelia Huck 	/*
22285dfe87eSCornelia Huck 	 * Return cookie in gpr 2, but don't overwrite the register if the
22385dfe87eSCornelia Huck 	 * diagnose will be handled by userspace.
22485dfe87eSCornelia Huck 	 */
22585dfe87eSCornelia Huck 	if (ret != -EOPNOTSUPP)
22685dfe87eSCornelia Huck 		vcpu->run->s.regs.gprs[2] = ret;
22785dfe87eSCornelia Huck 	/* kvm_io_bus_write_cookie returns -EOPNOTSUPP if it found no match. */
22810ccaa1eSCornelia Huck 	return ret < 0 ? ret : 0;
22910ccaa1eSCornelia Huck }
23010ccaa1eSCornelia Huck 
231e28acfeaSChristian Borntraeger int kvm_s390_handle_diag(struct kvm_vcpu *vcpu)
232e28acfeaSChristian Borntraeger {
233743db27cSHeiko Carstens 	int code = kvm_s390_get_base_disp_rs(vcpu) & 0xffff;
234e28acfeaSChristian Borntraeger 
23593e1750fSThomas Huth 	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
23693e1750fSThomas Huth 		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
23793e1750fSThomas Huth 
2385786fffaSCornelia Huck 	trace_kvm_s390_handle_diag(vcpu, code);
239e28acfeaSChristian Borntraeger 	switch (code) {
240388186bcSChristian Borntraeger 	case 0x10:
241388186bcSChristian Borntraeger 		return diag_release_pages(vcpu);
242e28acfeaSChristian Borntraeger 	case 0x44:
243e28acfeaSChristian Borntraeger 		return __diag_time_slice_end(vcpu);
24441628d33SKonstantin Weitz 	case 0x9c:
24541628d33SKonstantin Weitz 		return __diag_time_slice_end_directed(vcpu);
2463c038e6bSDominik Dingel 	case 0x258:
2473c038e6bSDominik Dingel 		return __diag_page_ref_service(vcpu);
248e28acfeaSChristian Borntraeger 	case 0x308:
249e28acfeaSChristian Borntraeger 		return __diag_ipl_functions(vcpu);
25010ccaa1eSCornelia Huck 	case 0x500:
25110ccaa1eSCornelia Huck 		return __diag_virtio_hypercall(vcpu);
252e28acfeaSChristian Borntraeger 	default:
253b8e660b8SHeiko Carstens 		return -EOPNOTSUPP;
254e28acfeaSChristian Borntraeger 	}
255e28acfeaSChristian Borntraeger }
256