xref: /openbmc/linux/arch/s390/kvm/diag.c (revision 58cdf5eb13d83e7ec7444a3f88115b21d7512369)
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>
171e133ab2SMartin Schwidefsky #include <asm/gmap.h>
1810ccaa1eSCornelia Huck #include <asm/virtio-ccw.h>
19e28acfeaSChristian Borntraeger #include "kvm-s390.h"
205786fffaSCornelia Huck #include "trace.h"
21ade38c31SCornelia Huck #include "trace-s390.h"
223c038e6bSDominik Dingel #include "gaccess.h"
23e28acfeaSChristian Borntraeger 
24388186bcSChristian Borntraeger static int diag_release_pages(struct kvm_vcpu *vcpu)
25388186bcSChristian Borntraeger {
26388186bcSChristian Borntraeger 	unsigned long start, end;
27fda902cbSMichael Mueller 	unsigned long prefix  = kvm_s390_get_prefix(vcpu);
28388186bcSChristian Borntraeger 
295a32c1afSChristian Borntraeger 	start = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
30*58cdf5ebSHeiko Carstens 	end = vcpu->run->s.regs.gprs[vcpu->arch.sie_block->ipa & 0xf] + PAGE_SIZE;
31175a5c9eSChristian Borntraeger 	vcpu->stat.diagnose_10++;
32388186bcSChristian Borntraeger 
33f7a960afSChristian Borntraeger 	if (start & ~PAGE_MASK || end & ~PAGE_MASK || start >= end
34388186bcSChristian Borntraeger 	    || start < 2 * PAGE_SIZE)
35388186bcSChristian Borntraeger 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
36388186bcSChristian Borntraeger 
37388186bcSChristian Borntraeger 	VCPU_EVENT(vcpu, 5, "diag release pages %lX %lX", start, end);
38388186bcSChristian Borntraeger 
39f7a960afSChristian Borntraeger 	/*
40f7a960afSChristian Borntraeger 	 * We checked for start >= end above, so lets check for the
41f7a960afSChristian Borntraeger 	 * fast path (no prefix swap page involved)
42f7a960afSChristian Borntraeger 	 */
43f7a960afSChristian Borntraeger 	if (end <= prefix || start >= prefix + 2 * PAGE_SIZE) {
446e0a0431SMartin Schwidefsky 		gmap_discard(vcpu->arch.gmap, start, end);
45388186bcSChristian Borntraeger 	} else {
46f7a960afSChristian Borntraeger 		/*
47f7a960afSChristian Borntraeger 		 * This is slow path.  gmap_discard will check for start
48f7a960afSChristian Borntraeger 		 * so lets split this into before prefix, prefix, after
49f7a960afSChristian Borntraeger 		 * prefix and let gmap_discard make some of these calls
50f7a960afSChristian Borntraeger 		 * NOPs.
51f7a960afSChristian Borntraeger 		 */
526e0a0431SMartin Schwidefsky 		gmap_discard(vcpu->arch.gmap, start, prefix);
53f7a960afSChristian Borntraeger 		if (start <= prefix)
54*58cdf5ebSHeiko Carstens 			gmap_discard(vcpu->arch.gmap, 0, PAGE_SIZE);
55*58cdf5ebSHeiko Carstens 		if (end > prefix + PAGE_SIZE)
56*58cdf5ebSHeiko Carstens 			gmap_discard(vcpu->arch.gmap, PAGE_SIZE, 2 * PAGE_SIZE);
57f7a960afSChristian Borntraeger 		gmap_discard(vcpu->arch.gmap, prefix + 2 * PAGE_SIZE, end);
58388186bcSChristian Borntraeger 	}
59388186bcSChristian Borntraeger 	return 0;
60388186bcSChristian Borntraeger }
61388186bcSChristian Borntraeger 
623c038e6bSDominik Dingel static int __diag_page_ref_service(struct kvm_vcpu *vcpu)
633c038e6bSDominik Dingel {
643c038e6bSDominik Dingel 	struct prs_parm {
653c038e6bSDominik Dingel 		u16 code;
663c038e6bSDominik Dingel 		u16 subcode;
673c038e6bSDominik Dingel 		u16 parm_len;
683c038e6bSDominik Dingel 		u16 parm_version;
693c038e6bSDominik Dingel 		u64 token_addr;
703c038e6bSDominik Dingel 		u64 select_mask;
713c038e6bSDominik Dingel 		u64 compare_mask;
723c038e6bSDominik Dingel 		u64 zarch;
733c038e6bSDominik Dingel 	};
743c038e6bSDominik Dingel 	struct prs_parm parm;
753c038e6bSDominik Dingel 	int rc;
763c038e6bSDominik Dingel 	u16 rx = (vcpu->arch.sie_block->ipa & 0xf0) >> 4;
773c038e6bSDominik Dingel 	u16 ry = (vcpu->arch.sie_block->ipa & 0x0f);
783c038e6bSDominik Dingel 
7915e8b5daSChristian Borntraeger 	VCPU_EVENT(vcpu, 3, "diag page reference parameter block at 0x%llx",
8015e8b5daSChristian Borntraeger 		   vcpu->run->s.regs.gprs[rx]);
81175a5c9eSChristian Borntraeger 	vcpu->stat.diagnose_258++;
823c038e6bSDominik Dingel 	if (vcpu->run->s.regs.gprs[rx] & 7)
833c038e6bSDominik Dingel 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
848ae04b8fSAlexander Yarygin 	rc = read_guest(vcpu, vcpu->run->s.regs.gprs[rx], rx, &parm, sizeof(parm));
8581480cc1SHeiko Carstens 	if (rc)
8681480cc1SHeiko Carstens 		return kvm_s390_inject_prog_cond(vcpu, rc);
873c038e6bSDominik Dingel 	if (parm.parm_version != 2 || parm.parm_len < 5 || parm.code != 0x258)
883c038e6bSDominik Dingel 		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
893c038e6bSDominik Dingel 
903c038e6bSDominik Dingel 	switch (parm.subcode) {
913c038e6bSDominik Dingel 	case 0: /* TOKEN */
92ab7090a6SChristian Borntraeger 		VCPU_EVENT(vcpu, 3, "pageref token addr 0x%llx "
93ab7090a6SChristian Borntraeger 			   "select mask 0x%llx compare mask 0x%llx",
94ab7090a6SChristian Borntraeger 			   parm.token_addr, parm.select_mask, parm.compare_mask);
953c038e6bSDominik Dingel 		if (vcpu->arch.pfault_token != KVM_S390_PFAULT_TOKEN_INVALID) {
963c038e6bSDominik Dingel 			/*
973c038e6bSDominik Dingel 			 * If the pagefault handshake is already activated,
983c038e6bSDominik Dingel 			 * the token must not be changed.  We have to return
993c038e6bSDominik Dingel 			 * decimal 8 instead, as mandated in SC24-6084.
1003c038e6bSDominik Dingel 			 */
1013c038e6bSDominik Dingel 			vcpu->run->s.regs.gprs[ry] = 8;
1023c038e6bSDominik Dingel 			return 0;
1033c038e6bSDominik Dingel 		}
1043c038e6bSDominik Dingel 
1053c038e6bSDominik Dingel 		if ((parm.compare_mask & parm.select_mask) != parm.compare_mask ||
1063c038e6bSDominik Dingel 		    parm.token_addr & 7 || parm.zarch != 0x8000000000000000ULL)
1073c038e6bSDominik Dingel 			return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1083c038e6bSDominik Dingel 
10981480cc1SHeiko Carstens 		if (kvm_is_error_gpa(vcpu->kvm, parm.token_addr))
1103c038e6bSDominik Dingel 			return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1113c038e6bSDominik Dingel 
1123c038e6bSDominik Dingel 		vcpu->arch.pfault_token = parm.token_addr;
1133c038e6bSDominik Dingel 		vcpu->arch.pfault_select = parm.select_mask;
1143c038e6bSDominik Dingel 		vcpu->arch.pfault_compare = parm.compare_mask;
1153c038e6bSDominik Dingel 		vcpu->run->s.regs.gprs[ry] = 0;
1163c038e6bSDominik Dingel 		rc = 0;
1173c038e6bSDominik Dingel 		break;
1183c038e6bSDominik Dingel 	case 1: /*
1193c038e6bSDominik Dingel 		 * CANCEL
1203c038e6bSDominik Dingel 		 * Specification allows to let already pending tokens survive
1213c038e6bSDominik Dingel 		 * the cancel, therefore to reduce code complexity, we assume
1223c038e6bSDominik Dingel 		 * all outstanding tokens are already pending.
1233c038e6bSDominik Dingel 		 */
124ab7090a6SChristian Borntraeger 		VCPU_EVENT(vcpu, 3, "pageref cancel addr 0x%llx", parm.token_addr);
1253c038e6bSDominik Dingel 		if (parm.token_addr || parm.select_mask ||
1263c038e6bSDominik Dingel 		    parm.compare_mask || parm.zarch)
1273c038e6bSDominik Dingel 			return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1283c038e6bSDominik Dingel 
1293c038e6bSDominik Dingel 		vcpu->run->s.regs.gprs[ry] = 0;
1303c038e6bSDominik Dingel 		/*
1313c038e6bSDominik Dingel 		 * If the pfault handling was not established or is already
1323c038e6bSDominik Dingel 		 * canceled SC24-6084 requests to return decimal 4.
1333c038e6bSDominik Dingel 		 */
1343c038e6bSDominik Dingel 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
1353c038e6bSDominik Dingel 			vcpu->run->s.regs.gprs[ry] = 4;
1363c038e6bSDominik Dingel 		else
1373c038e6bSDominik Dingel 			vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
1383c038e6bSDominik Dingel 
1393c038e6bSDominik Dingel 		rc = 0;
1403c038e6bSDominik Dingel 		break;
1413c038e6bSDominik Dingel 	default:
1423c038e6bSDominik Dingel 		rc = -EOPNOTSUPP;
1433c038e6bSDominik Dingel 		break;
1443c038e6bSDominik Dingel 	}
1453c038e6bSDominik Dingel 
1463c038e6bSDominik Dingel 	return rc;
1473c038e6bSDominik Dingel }
1483c038e6bSDominik Dingel 
149e28acfeaSChristian Borntraeger static int __diag_time_slice_end(struct kvm_vcpu *vcpu)
150e28acfeaSChristian Borntraeger {
151e28acfeaSChristian Borntraeger 	VCPU_EVENT(vcpu, 5, "%s", "diag time slice end");
152e28acfeaSChristian Borntraeger 	vcpu->stat.diagnose_44++;
1538733ac36SChristian Borntraeger 	kvm_vcpu_on_spin(vcpu);
154e28acfeaSChristian Borntraeger 	return 0;
155e28acfeaSChristian Borntraeger }
156e28acfeaSChristian Borntraeger 
15741628d33SKonstantin Weitz static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu)
15841628d33SKonstantin Weitz {
15941628d33SKonstantin Weitz 	struct kvm_vcpu *tcpu;
16041628d33SKonstantin Weitz 	int tid;
16141628d33SKonstantin Weitz 
16241628d33SKonstantin Weitz 	tid = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
16341628d33SKonstantin Weitz 	vcpu->stat.diagnose_9c++;
16441628d33SKonstantin Weitz 	VCPU_EVENT(vcpu, 5, "diag time slice end directed to %d", tid);
16541628d33SKonstantin Weitz 
16641628d33SKonstantin Weitz 	if (tid == vcpu->vcpu_id)
16741628d33SKonstantin Weitz 		return 0;
16841628d33SKonstantin Weitz 
169e09fefdeSDavid Hildenbrand 	tcpu = kvm_get_vcpu_by_id(vcpu->kvm, tid);
170e09fefdeSDavid Hildenbrand 	if (tcpu)
17141628d33SKonstantin Weitz 		kvm_vcpu_yield_to(tcpu);
17241628d33SKonstantin Weitz 	return 0;
17341628d33SKonstantin Weitz }
17441628d33SKonstantin Weitz 
175e28acfeaSChristian Borntraeger static int __diag_ipl_functions(struct kvm_vcpu *vcpu)
176e28acfeaSChristian Borntraeger {
177e28acfeaSChristian Borntraeger 	unsigned int reg = vcpu->arch.sie_block->ipa & 0xf;
1785a32c1afSChristian Borntraeger 	unsigned long subcode = vcpu->run->s.regs.gprs[reg] & 0xffff;
179e28acfeaSChristian Borntraeger 
18015e8b5daSChristian Borntraeger 	VCPU_EVENT(vcpu, 3, "diag ipl functions, subcode %lx", subcode);
181175a5c9eSChristian Borntraeger 	vcpu->stat.diagnose_308++;
182e28acfeaSChristian Borntraeger 	switch (subcode) {
183e28acfeaSChristian Borntraeger 	case 3:
184e28acfeaSChristian Borntraeger 		vcpu->run->s390_reset_flags = KVM_S390_RESET_CLEAR;
185e28acfeaSChristian Borntraeger 		break;
186e28acfeaSChristian Borntraeger 	case 4:
187e28acfeaSChristian Borntraeger 		vcpu->run->s390_reset_flags = 0;
188e28acfeaSChristian Borntraeger 		break;
189e28acfeaSChristian Borntraeger 	default:
190b8e660b8SHeiko Carstens 		return -EOPNOTSUPP;
191e28acfeaSChristian Borntraeger 	}
192e28acfeaSChristian Borntraeger 
1936352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
1946852d7b6SDavid Hildenbrand 		kvm_s390_vcpu_stop(vcpu);
195e28acfeaSChristian Borntraeger 	vcpu->run->s390_reset_flags |= KVM_S390_RESET_SUBSYSTEM;
196e28acfeaSChristian Borntraeger 	vcpu->run->s390_reset_flags |= KVM_S390_RESET_IPL;
197e28acfeaSChristian Borntraeger 	vcpu->run->s390_reset_flags |= KVM_S390_RESET_CPU_INIT;
198e28acfeaSChristian Borntraeger 	vcpu->run->exit_reason = KVM_EXIT_S390_RESET;
19933e19115SHeiko Carstens 	VCPU_EVENT(vcpu, 3, "requesting userspace resets %llx",
200e28acfeaSChristian Borntraeger 	  vcpu->run->s390_reset_flags);
201ade38c31SCornelia Huck 	trace_kvm_s390_request_resets(vcpu->run->s390_reset_flags);
202e28acfeaSChristian Borntraeger 	return -EREMOTE;
203e28acfeaSChristian Borntraeger }
204e28acfeaSChristian Borntraeger 
20510ccaa1eSCornelia Huck static int __diag_virtio_hypercall(struct kvm_vcpu *vcpu)
20610ccaa1eSCornelia Huck {
207800c1065SThomas Huth 	int ret;
20810ccaa1eSCornelia Huck 
209175a5c9eSChristian Borntraeger 	vcpu->stat.diagnose_500++;
21010ccaa1eSCornelia Huck 	/* No virtio-ccw notification? Get out quickly. */
21110ccaa1eSCornelia Huck 	if (!vcpu->kvm->arch.css_support ||
21210ccaa1eSCornelia Huck 	    (vcpu->run->s.regs.gprs[1] != KVM_S390_VIRTIO_CCW_NOTIFY))
21310ccaa1eSCornelia Huck 		return -EOPNOTSUPP;
21410ccaa1eSCornelia Huck 
2151bb78d16SChristian Borntraeger 	VCPU_EVENT(vcpu, 4, "diag 0x500 schid 0x%8.8x queue 0x%x cookie 0x%llx",
2161bb78d16SChristian Borntraeger 			    (u32) vcpu->run->s.regs.gprs[2],
2171bb78d16SChristian Borntraeger 			    (u32) vcpu->run->s.regs.gprs[3],
2181bb78d16SChristian Borntraeger 			    vcpu->run->s.regs.gprs[4]);
2191bb78d16SChristian Borntraeger 
22010ccaa1eSCornelia Huck 	/*
22110ccaa1eSCornelia Huck 	 * The layout is as follows:
22210ccaa1eSCornelia Huck 	 * - gpr 2 contains the subchannel id (passed as addr)
22310ccaa1eSCornelia Huck 	 * - gpr 3 contains the virtqueue index (passed as datamatch)
22485dfe87eSCornelia Huck 	 * - gpr 4 contains the index on the bus (optionally)
22510ccaa1eSCornelia Huck 	 */
226e32edf4fSNikolay Nikolaev 	ret = kvm_io_bus_write_cookie(vcpu, KVM_VIRTIO_CCW_NOTIFY_BUS,
227ff1f3cb4SDominik Dingel 				      vcpu->run->s.regs.gprs[2] & 0xffffffff,
22885dfe87eSCornelia Huck 				      8, &vcpu->run->s.regs.gprs[3],
22985dfe87eSCornelia Huck 				      vcpu->run->s.regs.gprs[4]);
23085dfe87eSCornelia Huck 
23185dfe87eSCornelia Huck 	/*
23285dfe87eSCornelia Huck 	 * Return cookie in gpr 2, but don't overwrite the register if the
23385dfe87eSCornelia Huck 	 * diagnose will be handled by userspace.
23485dfe87eSCornelia Huck 	 */
23585dfe87eSCornelia Huck 	if (ret != -EOPNOTSUPP)
23685dfe87eSCornelia Huck 		vcpu->run->s.regs.gprs[2] = ret;
23785dfe87eSCornelia Huck 	/* kvm_io_bus_write_cookie returns -EOPNOTSUPP if it found no match. */
23810ccaa1eSCornelia Huck 	return ret < 0 ? ret : 0;
23910ccaa1eSCornelia Huck }
24010ccaa1eSCornelia Huck 
241e28acfeaSChristian Borntraeger int kvm_s390_handle_diag(struct kvm_vcpu *vcpu)
242e28acfeaSChristian Borntraeger {
2438ae04b8fSAlexander Yarygin 	int code = kvm_s390_get_base_disp_rs(vcpu, NULL) & 0xffff;
244e28acfeaSChristian Borntraeger 
24593e1750fSThomas Huth 	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
24693e1750fSThomas Huth 		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
24793e1750fSThomas Huth 
2485786fffaSCornelia Huck 	trace_kvm_s390_handle_diag(vcpu, code);
249e28acfeaSChristian Borntraeger 	switch (code) {
250388186bcSChristian Borntraeger 	case 0x10:
251388186bcSChristian Borntraeger 		return diag_release_pages(vcpu);
252e28acfeaSChristian Borntraeger 	case 0x44:
253e28acfeaSChristian Borntraeger 		return __diag_time_slice_end(vcpu);
25441628d33SKonstantin Weitz 	case 0x9c:
25541628d33SKonstantin Weitz 		return __diag_time_slice_end_directed(vcpu);
2563c038e6bSDominik Dingel 	case 0x258:
2573c038e6bSDominik Dingel 		return __diag_page_ref_service(vcpu);
258e28acfeaSChristian Borntraeger 	case 0x308:
259e28acfeaSChristian Borntraeger 		return __diag_ipl_functions(vcpu);
26010ccaa1eSCornelia Huck 	case 0x500:
26110ccaa1eSCornelia Huck 		return __diag_virtio_hypercall(vcpu);
262e28acfeaSChristian Borntraeger 	default:
263b8e660b8SHeiko Carstens 		return -EOPNOTSUPP;
264e28acfeaSChristian Borntraeger 	}
265e28acfeaSChristian Borntraeger }
266