Linux Audio

Check our new training course

Loading...
v4.10.11
 
  1/*
  2 * handling diagnose instructions
  3 *
  4 * Copyright IBM Corp. 2008, 2011
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License (version 2 only)
  8 * as published by the Free Software Foundation.
  9 *
 10 *    Author(s): Carsten Otte <cotte@de.ibm.com>
 11 *               Christian Borntraeger <borntraeger@de.ibm.com>
 12 */
 13
 14#include <linux/kvm.h>
 15#include <linux/kvm_host.h>
 16#include <asm/pgalloc.h>
 17#include <asm/gmap.h>
 18#include <asm/virtio-ccw.h>
 19#include "kvm-s390.h"
 20#include "trace.h"
 21#include "trace-s390.h"
 22#include "gaccess.h"
 23
 24static int diag_release_pages(struct kvm_vcpu *vcpu)
 25{
 26	unsigned long start, end;
 27	unsigned long prefix  = kvm_s390_get_prefix(vcpu);
 28
 29	start = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
 30	end = vcpu->run->s.regs.gprs[vcpu->arch.sie_block->ipa & 0xf] + 4096;
 31	vcpu->stat.diagnose_10++;
 32
 33	if (start & ~PAGE_MASK || end & ~PAGE_MASK || start >= end
 34	    || start < 2 * PAGE_SIZE)
 35		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
 36
 37	VCPU_EVENT(vcpu, 5, "diag release pages %lX %lX", start, end);
 38
 39	/*
 40	 * We checked for start >= end above, so lets check for the
 41	 * fast path (no prefix swap page involved)
 42	 */
 43	if (end <= prefix || start >= prefix + 2 * PAGE_SIZE) {
 44		gmap_discard(vcpu->arch.gmap, start, end);
 45	} else {
 46		/*
 47		 * This is slow path.  gmap_discard will check for start
 48		 * so lets split this into before prefix, prefix, after
 49		 * prefix and let gmap_discard make some of these calls
 50		 * NOPs.
 51		 */
 52		gmap_discard(vcpu->arch.gmap, start, prefix);
 53		if (start <= prefix)
 54			gmap_discard(vcpu->arch.gmap, 0, 4096);
 55		if (end > prefix + 4096)
 56			gmap_discard(vcpu->arch.gmap, 4096, 8192);
 57		gmap_discard(vcpu->arch.gmap, prefix + 2 * PAGE_SIZE, end);
 58	}
 59	return 0;
 60}
 61
 62static int __diag_page_ref_service(struct kvm_vcpu *vcpu)
 63{
 64	struct prs_parm {
 65		u16 code;
 66		u16 subcode;
 67		u16 parm_len;
 68		u16 parm_version;
 69		u64 token_addr;
 70		u64 select_mask;
 71		u64 compare_mask;
 72		u64 zarch;
 73	};
 74	struct prs_parm parm;
 75	int rc;
 76	u16 rx = (vcpu->arch.sie_block->ipa & 0xf0) >> 4;
 77	u16 ry = (vcpu->arch.sie_block->ipa & 0x0f);
 78
 79	VCPU_EVENT(vcpu, 3, "diag page reference parameter block at 0x%llx",
 80		   vcpu->run->s.regs.gprs[rx]);
 81	vcpu->stat.diagnose_258++;
 82	if (vcpu->run->s.regs.gprs[rx] & 7)
 83		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
 84	rc = read_guest(vcpu, vcpu->run->s.regs.gprs[rx], rx, &parm, sizeof(parm));
 85	if (rc)
 86		return kvm_s390_inject_prog_cond(vcpu, rc);
 87	if (parm.parm_version != 2 || parm.parm_len < 5 || parm.code != 0x258)
 88		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
 89
 90	switch (parm.subcode) {
 91	case 0: /* TOKEN */
 92		VCPU_EVENT(vcpu, 3, "pageref token addr 0x%llx "
 93			   "select mask 0x%llx compare mask 0x%llx",
 94			   parm.token_addr, parm.select_mask, parm.compare_mask);
 95		if (vcpu->arch.pfault_token != KVM_S390_PFAULT_TOKEN_INVALID) {
 96			/*
 97			 * If the pagefault handshake is already activated,
 98			 * the token must not be changed.  We have to return
 99			 * decimal 8 instead, as mandated in SC24-6084.
100			 */
101			vcpu->run->s.regs.gprs[ry] = 8;
102			return 0;
103		}
104
105		if ((parm.compare_mask & parm.select_mask) != parm.compare_mask ||
106		    parm.token_addr & 7 || parm.zarch != 0x8000000000000000ULL)
107			return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
108
109		if (kvm_is_error_gpa(vcpu->kvm, parm.token_addr))
110			return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
111
112		vcpu->arch.pfault_token = parm.token_addr;
113		vcpu->arch.pfault_select = parm.select_mask;
114		vcpu->arch.pfault_compare = parm.compare_mask;
115		vcpu->run->s.regs.gprs[ry] = 0;
116		rc = 0;
117		break;
118	case 1: /*
119		 * CANCEL
120		 * Specification allows to let already pending tokens survive
121		 * the cancel, therefore to reduce code complexity, we assume
122		 * all outstanding tokens are already pending.
123		 */
124		VCPU_EVENT(vcpu, 3, "pageref cancel addr 0x%llx", parm.token_addr);
125		if (parm.token_addr || parm.select_mask ||
126		    parm.compare_mask || parm.zarch)
127			return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
128
129		vcpu->run->s.regs.gprs[ry] = 0;
130		/*
131		 * If the pfault handling was not established or is already
132		 * canceled SC24-6084 requests to return decimal 4.
133		 */
134		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
135			vcpu->run->s.regs.gprs[ry] = 4;
136		else
137			vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
138
139		rc = 0;
140		break;
141	default:
142		rc = -EOPNOTSUPP;
143		break;
144	}
145
146	return rc;
147}
148
149static int __diag_time_slice_end(struct kvm_vcpu *vcpu)
150{
151	VCPU_EVENT(vcpu, 5, "%s", "diag time slice end");
152	vcpu->stat.diagnose_44++;
153	kvm_vcpu_on_spin(vcpu);
154	return 0;
155}
156
157static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu)
158{
159	struct kvm_vcpu *tcpu;
160	int tid;
161
162	tid = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
163	vcpu->stat.diagnose_9c++;
164	VCPU_EVENT(vcpu, 5, "diag time slice end directed to %d", tid);
165
 
166	if (tid == vcpu->vcpu_id)
167		return 0;
168
 
169	tcpu = kvm_get_vcpu_by_id(vcpu->kvm, tid);
170	if (tcpu)
171		kvm_vcpu_yield_to(tcpu);
 
 
 
 
 
 
 
 
 
 
 
 
 
172	return 0;
173}
174
175static int __diag_ipl_functions(struct kvm_vcpu *vcpu)
176{
177	unsigned int reg = vcpu->arch.sie_block->ipa & 0xf;
178	unsigned long subcode = vcpu->run->s.regs.gprs[reg] & 0xffff;
179
180	VCPU_EVENT(vcpu, 3, "diag ipl functions, subcode %lx", subcode);
181	vcpu->stat.diagnose_308++;
182	switch (subcode) {
183	case 3:
184		vcpu->run->s390_reset_flags = KVM_S390_RESET_CLEAR;
185		break;
186	case 4:
187		vcpu->run->s390_reset_flags = 0;
188		break;
189	default:
190		return -EOPNOTSUPP;
191	}
192
 
 
 
 
193	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
194		kvm_s390_vcpu_stop(vcpu);
195	vcpu->run->s390_reset_flags |= KVM_S390_RESET_SUBSYSTEM;
196	vcpu->run->s390_reset_flags |= KVM_S390_RESET_IPL;
197	vcpu->run->s390_reset_flags |= KVM_S390_RESET_CPU_INIT;
198	vcpu->run->exit_reason = KVM_EXIT_S390_RESET;
199	VCPU_EVENT(vcpu, 3, "requesting userspace resets %llx",
200	  vcpu->run->s390_reset_flags);
201	trace_kvm_s390_request_resets(vcpu->run->s390_reset_flags);
202	return -EREMOTE;
203}
204
205static int __diag_virtio_hypercall(struct kvm_vcpu *vcpu)
206{
207	int ret;
208
209	vcpu->stat.diagnose_500++;
210	/* No virtio-ccw notification? Get out quickly. */
211	if (!vcpu->kvm->arch.css_support ||
212	    (vcpu->run->s.regs.gprs[1] != KVM_S390_VIRTIO_CCW_NOTIFY))
213		return -EOPNOTSUPP;
214
215	VCPU_EVENT(vcpu, 4, "diag 0x500 schid 0x%8.8x queue 0x%x cookie 0x%llx",
216			    (u32) vcpu->run->s.regs.gprs[2],
217			    (u32) vcpu->run->s.regs.gprs[3],
218			    vcpu->run->s.regs.gprs[4]);
219
220	/*
221	 * The layout is as follows:
222	 * - gpr 2 contains the subchannel id (passed as addr)
223	 * - gpr 3 contains the virtqueue index (passed as datamatch)
224	 * - gpr 4 contains the index on the bus (optionally)
225	 */
226	ret = kvm_io_bus_write_cookie(vcpu, KVM_VIRTIO_CCW_NOTIFY_BUS,
227				      vcpu->run->s.regs.gprs[2] & 0xffffffff,
228				      8, &vcpu->run->s.regs.gprs[3],
229				      vcpu->run->s.regs.gprs[4]);
230
231	/*
232	 * Return cookie in gpr 2, but don't overwrite the register if the
233	 * diagnose will be handled by userspace.
234	 */
235	if (ret != -EOPNOTSUPP)
236		vcpu->run->s.regs.gprs[2] = ret;
237	/* kvm_io_bus_write_cookie returns -EOPNOTSUPP if it found no match. */
238	return ret < 0 ? ret : 0;
239}
240
241int kvm_s390_handle_diag(struct kvm_vcpu *vcpu)
242{
243	int code = kvm_s390_get_base_disp_rs(vcpu, NULL) & 0xffff;
244
245	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
246		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
247
248	trace_kvm_s390_handle_diag(vcpu, code);
249	switch (code) {
250	case 0x10:
251		return diag_release_pages(vcpu);
252	case 0x44:
253		return __diag_time_slice_end(vcpu);
254	case 0x9c:
255		return __diag_time_slice_end_directed(vcpu);
256	case 0x258:
257		return __diag_page_ref_service(vcpu);
258	case 0x308:
259		return __diag_ipl_functions(vcpu);
260	case 0x500:
261		return __diag_virtio_hypercall(vcpu);
262	default:
 
263		return -EOPNOTSUPP;
264	}
265}
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * handling diagnose instructions
  4 *
  5 * Copyright IBM Corp. 2008, 2020
 
 
 
 
  6 *
  7 *    Author(s): Carsten Otte <cotte@de.ibm.com>
  8 *               Christian Borntraeger <borntraeger@de.ibm.com>
  9 */
 10
 11#include <linux/kvm.h>
 12#include <linux/kvm_host.h>
 
 13#include <asm/gmap.h>
 14#include <asm/virtio-ccw.h>
 15#include "kvm-s390.h"
 16#include "trace.h"
 17#include "trace-s390.h"
 18#include "gaccess.h"
 19
 20static int diag_release_pages(struct kvm_vcpu *vcpu)
 21{
 22	unsigned long start, end;
 23	unsigned long prefix  = kvm_s390_get_prefix(vcpu);
 24
 25	start = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
 26	end = vcpu->run->s.regs.gprs[vcpu->arch.sie_block->ipa & 0xf] + PAGE_SIZE;
 27	vcpu->stat.diagnose_10++;
 28
 29	if (start & ~PAGE_MASK || end & ~PAGE_MASK || start >= end
 30	    || start < 2 * PAGE_SIZE)
 31		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
 32
 33	VCPU_EVENT(vcpu, 5, "diag release pages %lX %lX", start, end);
 34
 35	/*
 36	 * We checked for start >= end above, so lets check for the
 37	 * fast path (no prefix swap page involved)
 38	 */
 39	if (end <= prefix || start >= prefix + 2 * PAGE_SIZE) {
 40		gmap_discard(vcpu->arch.gmap, start, end);
 41	} else {
 42		/*
 43		 * This is slow path.  gmap_discard will check for start
 44		 * so lets split this into before prefix, prefix, after
 45		 * prefix and let gmap_discard make some of these calls
 46		 * NOPs.
 47		 */
 48		gmap_discard(vcpu->arch.gmap, start, prefix);
 49		if (start <= prefix)
 50			gmap_discard(vcpu->arch.gmap, 0, PAGE_SIZE);
 51		if (end > prefix + PAGE_SIZE)
 52			gmap_discard(vcpu->arch.gmap, PAGE_SIZE, 2 * PAGE_SIZE);
 53		gmap_discard(vcpu->arch.gmap, prefix + 2 * PAGE_SIZE, end);
 54	}
 55	return 0;
 56}
 57
 58static int __diag_page_ref_service(struct kvm_vcpu *vcpu)
 59{
 60	struct prs_parm {
 61		u16 code;
 62		u16 subcode;
 63		u16 parm_len;
 64		u16 parm_version;
 65		u64 token_addr;
 66		u64 select_mask;
 67		u64 compare_mask;
 68		u64 zarch;
 69	};
 70	struct prs_parm parm;
 71	int rc;
 72	u16 rx = (vcpu->arch.sie_block->ipa & 0xf0) >> 4;
 73	u16 ry = (vcpu->arch.sie_block->ipa & 0x0f);
 74
 75	VCPU_EVENT(vcpu, 3, "diag page reference parameter block at 0x%llx",
 76		   vcpu->run->s.regs.gprs[rx]);
 77	vcpu->stat.diagnose_258++;
 78	if (vcpu->run->s.regs.gprs[rx] & 7)
 79		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
 80	rc = read_guest(vcpu, vcpu->run->s.regs.gprs[rx], rx, &parm, sizeof(parm));
 81	if (rc)
 82		return kvm_s390_inject_prog_cond(vcpu, rc);
 83	if (parm.parm_version != 2 || parm.parm_len < 5 || parm.code != 0x258)
 84		return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
 85
 86	switch (parm.subcode) {
 87	case 0: /* TOKEN */
 88		VCPU_EVENT(vcpu, 3, "pageref token addr 0x%llx "
 89			   "select mask 0x%llx compare mask 0x%llx",
 90			   parm.token_addr, parm.select_mask, parm.compare_mask);
 91		if (vcpu->arch.pfault_token != KVM_S390_PFAULT_TOKEN_INVALID) {
 92			/*
 93			 * If the pagefault handshake is already activated,
 94			 * the token must not be changed.  We have to return
 95			 * decimal 8 instead, as mandated in SC24-6084.
 96			 */
 97			vcpu->run->s.regs.gprs[ry] = 8;
 98			return 0;
 99		}
100
101		if ((parm.compare_mask & parm.select_mask) != parm.compare_mask ||
102		    parm.token_addr & 7 || parm.zarch != 0x8000000000000000ULL)
103			return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
104
105		if (kvm_is_error_gpa(vcpu->kvm, parm.token_addr))
106			return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
107
108		vcpu->arch.pfault_token = parm.token_addr;
109		vcpu->arch.pfault_select = parm.select_mask;
110		vcpu->arch.pfault_compare = parm.compare_mask;
111		vcpu->run->s.regs.gprs[ry] = 0;
112		rc = 0;
113		break;
114	case 1: /*
115		 * CANCEL
116		 * Specification allows to let already pending tokens survive
117		 * the cancel, therefore to reduce code complexity, we assume
118		 * all outstanding tokens are already pending.
119		 */
120		VCPU_EVENT(vcpu, 3, "pageref cancel addr 0x%llx", parm.token_addr);
121		if (parm.token_addr || parm.select_mask ||
122		    parm.compare_mask || parm.zarch)
123			return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
124
125		vcpu->run->s.regs.gprs[ry] = 0;
126		/*
127		 * If the pfault handling was not established or is already
128		 * canceled SC24-6084 requests to return decimal 4.
129		 */
130		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
131			vcpu->run->s.regs.gprs[ry] = 4;
132		else
133			vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
134
135		rc = 0;
136		break;
137	default:
138		rc = -EOPNOTSUPP;
139		break;
140	}
141
142	return rc;
143}
144
145static int __diag_time_slice_end(struct kvm_vcpu *vcpu)
146{
147	VCPU_EVENT(vcpu, 5, "%s", "diag time slice end");
148	vcpu->stat.diagnose_44++;
149	kvm_vcpu_on_spin(vcpu, true);
150	return 0;
151}
152
153static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu)
154{
155	struct kvm_vcpu *tcpu;
156	int tid;
157
158	tid = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
159	vcpu->stat.diagnose_9c++;
 
160
161	/* yield to self */
162	if (tid == vcpu->vcpu_id)
163		goto no_yield;
164
165	/* yield to invalid */
166	tcpu = kvm_get_vcpu_by_id(vcpu->kvm, tid);
167	if (!tcpu)
168		goto no_yield;
169
170	/* target already running */
171	if (READ_ONCE(tcpu->cpu) >= 0)
172		goto no_yield;
173
174	if (kvm_vcpu_yield_to(tcpu) <= 0)
175		goto no_yield;
176
177	VCPU_EVENT(vcpu, 5, "diag time slice end directed to %d: done", tid);
178	return 0;
179no_yield:
180	VCPU_EVENT(vcpu, 5, "diag time slice end directed to %d: ignored", tid);
181	vcpu->stat.diagnose_9c_ignored++;
182	return 0;
183}
184
185static int __diag_ipl_functions(struct kvm_vcpu *vcpu)
186{
187	unsigned int reg = vcpu->arch.sie_block->ipa & 0xf;
188	unsigned long subcode = vcpu->run->s.regs.gprs[reg] & 0xffff;
189
190	VCPU_EVENT(vcpu, 3, "diag ipl functions, subcode %lx", subcode);
191	vcpu->stat.diagnose_308++;
192	switch (subcode) {
193	case 3:
194		vcpu->run->s390_reset_flags = KVM_S390_RESET_CLEAR;
195		break;
196	case 4:
197		vcpu->run->s390_reset_flags = 0;
198		break;
199	default:
200		return -EOPNOTSUPP;
201	}
202
203	/*
204	 * no need to check the return value of vcpu_stop as it can only have
205	 * an error for protvirt, but protvirt means user cpu state
206	 */
207	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
208		kvm_s390_vcpu_stop(vcpu);
209	vcpu->run->s390_reset_flags |= KVM_S390_RESET_SUBSYSTEM;
210	vcpu->run->s390_reset_flags |= KVM_S390_RESET_IPL;
211	vcpu->run->s390_reset_flags |= KVM_S390_RESET_CPU_INIT;
212	vcpu->run->exit_reason = KVM_EXIT_S390_RESET;
213	VCPU_EVENT(vcpu, 3, "requesting userspace resets %llx",
214	  vcpu->run->s390_reset_flags);
215	trace_kvm_s390_request_resets(vcpu->run->s390_reset_flags);
216	return -EREMOTE;
217}
218
219static int __diag_virtio_hypercall(struct kvm_vcpu *vcpu)
220{
221	int ret;
222
223	vcpu->stat.diagnose_500++;
224	/* No virtio-ccw notification? Get out quickly. */
225	if (!vcpu->kvm->arch.css_support ||
226	    (vcpu->run->s.regs.gprs[1] != KVM_S390_VIRTIO_CCW_NOTIFY))
227		return -EOPNOTSUPP;
228
229	VCPU_EVENT(vcpu, 4, "diag 0x500 schid 0x%8.8x queue 0x%x cookie 0x%llx",
230			    (u32) vcpu->run->s.regs.gprs[2],
231			    (u32) vcpu->run->s.regs.gprs[3],
232			    vcpu->run->s.regs.gprs[4]);
233
234	/*
235	 * The layout is as follows:
236	 * - gpr 2 contains the subchannel id (passed as addr)
237	 * - gpr 3 contains the virtqueue index (passed as datamatch)
238	 * - gpr 4 contains the index on the bus (optionally)
239	 */
240	ret = kvm_io_bus_write_cookie(vcpu, KVM_VIRTIO_CCW_NOTIFY_BUS,
241				      vcpu->run->s.regs.gprs[2] & 0xffffffff,
242				      8, &vcpu->run->s.regs.gprs[3],
243				      vcpu->run->s.regs.gprs[4]);
244
245	/*
246	 * Return cookie in gpr 2, but don't overwrite the register if the
247	 * diagnose will be handled by userspace.
248	 */
249	if (ret != -EOPNOTSUPP)
250		vcpu->run->s.regs.gprs[2] = ret;
251	/* kvm_io_bus_write_cookie returns -EOPNOTSUPP if it found no match. */
252	return ret < 0 ? ret : 0;
253}
254
255int kvm_s390_handle_diag(struct kvm_vcpu *vcpu)
256{
257	int code = kvm_s390_get_base_disp_rs(vcpu, NULL) & 0xffff;
258
259	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
260		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
261
262	trace_kvm_s390_handle_diag(vcpu, code);
263	switch (code) {
264	case 0x10:
265		return diag_release_pages(vcpu);
266	case 0x44:
267		return __diag_time_slice_end(vcpu);
268	case 0x9c:
269		return __diag_time_slice_end_directed(vcpu);
270	case 0x258:
271		return __diag_page_ref_service(vcpu);
272	case 0x308:
273		return __diag_ipl_functions(vcpu);
274	case 0x500:
275		return __diag_virtio_hypercall(vcpu);
276	default:
277		vcpu->stat.diagnose_other++;
278		return -EOPNOTSUPP;
279	}
280}