• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * handling interprocessor communication
4  *
5  * Copyright IBM Corp. 2008, 2013
6  *
7  *    Author(s): Carsten Otte <cotte@de.ibm.com>
8  *               Christian Borntraeger <borntraeger@de.ibm.com>
9  *               Christian Ehrhardt <ehrhardt@de.ibm.com>
10  */
11 
12 #include <linux/kvm.h>
13 #include <linux/kvm_host.h>
14 #include <linux/slab.h>
15 #include <asm/sigp.h>
16 #include "gaccess.h"
17 #include "kvm-s390.h"
18 #include "trace.h"
19 
__sigp_sense(struct kvm_vcpu * vcpu,struct kvm_vcpu * dst_vcpu,u64 * reg)20 static int __sigp_sense(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu,
21 			u64 *reg)
22 {
23 	const bool stopped = kvm_s390_test_cpuflags(dst_vcpu, CPUSTAT_STOPPED);
24 	int rc;
25 	int ext_call_pending;
26 
27 	ext_call_pending = kvm_s390_ext_call_pending(dst_vcpu);
28 	if (!stopped && !ext_call_pending)
29 		rc = SIGP_CC_ORDER_CODE_ACCEPTED;
30 	else {
31 		*reg &= 0xffffffff00000000UL;
32 		if (ext_call_pending)
33 			*reg |= SIGP_STATUS_EXT_CALL_PENDING;
34 		if (stopped)
35 			*reg |= SIGP_STATUS_STOPPED;
36 		rc = SIGP_CC_STATUS_STORED;
37 	}
38 
39 	VCPU_EVENT(vcpu, 4, "sensed status of cpu %x rc %x", dst_vcpu->vcpu_id,
40 		   rc);
41 	return rc;
42 }
43 
__inject_sigp_emergency(struct kvm_vcpu * vcpu,struct kvm_vcpu * dst_vcpu)44 static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
45 				    struct kvm_vcpu *dst_vcpu)
46 {
47 	struct kvm_s390_irq irq = {
48 		.type = KVM_S390_INT_EMERGENCY,
49 		.u.emerg.code = vcpu->vcpu_id,
50 	};
51 	int rc = 0;
52 
53 	rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
54 	if (!rc)
55 		VCPU_EVENT(vcpu, 4, "sent sigp emerg to cpu %x",
56 			   dst_vcpu->vcpu_id);
57 
58 	return rc ? rc : SIGP_CC_ORDER_CODE_ACCEPTED;
59 }
60 
__sigp_emergency(struct kvm_vcpu * vcpu,struct kvm_vcpu * dst_vcpu)61 static int __sigp_emergency(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu)
62 {
63 	return __inject_sigp_emergency(vcpu, dst_vcpu);
64 }
65 
__sigp_conditional_emergency(struct kvm_vcpu * vcpu,struct kvm_vcpu * dst_vcpu,u16 asn,u64 * reg)66 static int __sigp_conditional_emergency(struct kvm_vcpu *vcpu,
67 					struct kvm_vcpu *dst_vcpu,
68 					u16 asn, u64 *reg)
69 {
70 	const u64 psw_int_mask = PSW_MASK_IO | PSW_MASK_EXT;
71 	u16 p_asn, s_asn;
72 	psw_t *psw;
73 	bool idle;
74 
75 	idle = is_vcpu_idle(vcpu);
76 	psw = &dst_vcpu->arch.sie_block->gpsw;
77 	p_asn = dst_vcpu->arch.sie_block->gcr[4] & 0xffff;  /* Primary ASN */
78 	s_asn = dst_vcpu->arch.sie_block->gcr[3] & 0xffff;  /* Secondary ASN */
79 
80 	/* Inject the emergency signal? */
81 	if (!is_vcpu_stopped(vcpu)
82 	    || (psw->mask & psw_int_mask) != psw_int_mask
83 	    || (idle && psw->addr != 0)
84 	    || (!idle && (asn == p_asn || asn == s_asn))) {
85 		return __inject_sigp_emergency(vcpu, dst_vcpu);
86 	} else {
87 		*reg &= 0xffffffff00000000UL;
88 		*reg |= SIGP_STATUS_INCORRECT_STATE;
89 		return SIGP_CC_STATUS_STORED;
90 	}
91 }
92 
__sigp_external_call(struct kvm_vcpu * vcpu,struct kvm_vcpu * dst_vcpu,u64 * reg)93 static int __sigp_external_call(struct kvm_vcpu *vcpu,
94 				struct kvm_vcpu *dst_vcpu, u64 *reg)
95 {
96 	struct kvm_s390_irq irq = {
97 		.type = KVM_S390_INT_EXTERNAL_CALL,
98 		.u.extcall.code = vcpu->vcpu_id,
99 	};
100 	int rc;
101 
102 	rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
103 	if (rc == -EBUSY) {
104 		*reg &= 0xffffffff00000000UL;
105 		*reg |= SIGP_STATUS_EXT_CALL_PENDING;
106 		return SIGP_CC_STATUS_STORED;
107 	} else if (rc == 0) {
108 		VCPU_EVENT(vcpu, 4, "sent sigp ext call to cpu %x",
109 			   dst_vcpu->vcpu_id);
110 	}
111 
112 	return rc ? rc : SIGP_CC_ORDER_CODE_ACCEPTED;
113 }
114 
__sigp_stop(struct kvm_vcpu * vcpu,struct kvm_vcpu * dst_vcpu)115 static int __sigp_stop(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu)
116 {
117 	struct kvm_s390_irq irq = {
118 		.type = KVM_S390_SIGP_STOP,
119 	};
120 	int rc;
121 
122 	rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
123 	if (rc == -EBUSY)
124 		rc = SIGP_CC_BUSY;
125 	else if (rc == 0)
126 		VCPU_EVENT(vcpu, 4, "sent sigp stop to cpu %x",
127 			   dst_vcpu->vcpu_id);
128 
129 	return rc;
130 }
131 
__sigp_stop_and_store_status(struct kvm_vcpu * vcpu,struct kvm_vcpu * dst_vcpu,u64 * reg)132 static int __sigp_stop_and_store_status(struct kvm_vcpu *vcpu,
133 					struct kvm_vcpu *dst_vcpu, u64 *reg)
134 {
135 	struct kvm_s390_irq irq = {
136 		.type = KVM_S390_SIGP_STOP,
137 		.u.stop.flags = KVM_S390_STOP_FLAG_STORE_STATUS,
138 	};
139 	int rc;
140 
141 	rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
142 	if (rc == -EBUSY)
143 		rc = SIGP_CC_BUSY;
144 	else if (rc == 0)
145 		VCPU_EVENT(vcpu, 4, "sent sigp stop and store status to cpu %x",
146 			   dst_vcpu->vcpu_id);
147 
148 	return rc;
149 }
150 
__sigp_set_arch(struct kvm_vcpu * vcpu,u32 parameter,u64 * status_reg)151 static int __sigp_set_arch(struct kvm_vcpu *vcpu, u32 parameter,
152 			   u64 *status_reg)
153 {
154 	unsigned int i;
155 	struct kvm_vcpu *v;
156 	bool all_stopped = true;
157 
158 	kvm_for_each_vcpu(i, v, vcpu->kvm) {
159 		if (v == vcpu)
160 			continue;
161 		if (!is_vcpu_stopped(v))
162 			all_stopped = false;
163 	}
164 
165 	*status_reg &= 0xffffffff00000000UL;
166 
167 	/* Reject set arch order, with czam we're always in z/Arch mode. */
168 	*status_reg |= (all_stopped ? SIGP_STATUS_INVALID_PARAMETER :
169 					SIGP_STATUS_INCORRECT_STATE);
170 	return SIGP_CC_STATUS_STORED;
171 }
172 
__sigp_set_prefix(struct kvm_vcpu * vcpu,struct kvm_vcpu * dst_vcpu,u32 address,u64 * reg)173 static int __sigp_set_prefix(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu,
174 			     u32 address, u64 *reg)
175 {
176 	struct kvm_s390_irq irq = {
177 		.type = KVM_S390_SIGP_SET_PREFIX,
178 		.u.prefix.address = address & 0x7fffe000u,
179 	};
180 	int rc;
181 
182 	/*
183 	 * Make sure the new value is valid memory. We only need to check the
184 	 * first page, since address is 8k aligned and memory pieces are always
185 	 * at least 1MB aligned and have at least a size of 1MB.
186 	 */
187 	if (kvm_is_error_gpa(vcpu->kvm, irq.u.prefix.address)) {
188 		*reg &= 0xffffffff00000000UL;
189 		*reg |= SIGP_STATUS_INVALID_PARAMETER;
190 		return SIGP_CC_STATUS_STORED;
191 	}
192 
193 	rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
194 	if (rc == -EBUSY) {
195 		*reg &= 0xffffffff00000000UL;
196 		*reg |= SIGP_STATUS_INCORRECT_STATE;
197 		return SIGP_CC_STATUS_STORED;
198 	}
199 
200 	return rc;
201 }
202 
__sigp_store_status_at_addr(struct kvm_vcpu * vcpu,struct kvm_vcpu * dst_vcpu,u32 addr,u64 * reg)203 static int __sigp_store_status_at_addr(struct kvm_vcpu *vcpu,
204 				       struct kvm_vcpu *dst_vcpu,
205 				       u32 addr, u64 *reg)
206 {
207 	int rc;
208 
209 	if (!kvm_s390_test_cpuflags(dst_vcpu, CPUSTAT_STOPPED)) {
210 		*reg &= 0xffffffff00000000UL;
211 		*reg |= SIGP_STATUS_INCORRECT_STATE;
212 		return SIGP_CC_STATUS_STORED;
213 	}
214 
215 	addr &= 0x7ffffe00;
216 	rc = kvm_s390_store_status_unloaded(dst_vcpu, addr);
217 	if (rc == -EFAULT) {
218 		*reg &= 0xffffffff00000000UL;
219 		*reg |= SIGP_STATUS_INVALID_PARAMETER;
220 		rc = SIGP_CC_STATUS_STORED;
221 	}
222 	return rc;
223 }
224 
__sigp_sense_running(struct kvm_vcpu * vcpu,struct kvm_vcpu * dst_vcpu,u64 * reg)225 static int __sigp_sense_running(struct kvm_vcpu *vcpu,
226 				struct kvm_vcpu *dst_vcpu, u64 *reg)
227 {
228 	int rc;
229 
230 	if (!test_kvm_facility(vcpu->kvm, 9)) {
231 		*reg &= 0xffffffff00000000UL;
232 		*reg |= SIGP_STATUS_INVALID_ORDER;
233 		return SIGP_CC_STATUS_STORED;
234 	}
235 
236 	if (kvm_s390_test_cpuflags(dst_vcpu, CPUSTAT_RUNNING)) {
237 		/* running */
238 		rc = SIGP_CC_ORDER_CODE_ACCEPTED;
239 	} else {
240 		/* not running */
241 		*reg &= 0xffffffff00000000UL;
242 		*reg |= SIGP_STATUS_NOT_RUNNING;
243 		rc = SIGP_CC_STATUS_STORED;
244 	}
245 
246 	VCPU_EVENT(vcpu, 4, "sensed running status of cpu %x rc %x",
247 		   dst_vcpu->vcpu_id, rc);
248 
249 	return rc;
250 }
251 
__prepare_sigp_re_start(struct kvm_vcpu * vcpu,struct kvm_vcpu * dst_vcpu,u8 order_code)252 static int __prepare_sigp_re_start(struct kvm_vcpu *vcpu,
253 				   struct kvm_vcpu *dst_vcpu, u8 order_code)
254 {
255 	struct kvm_s390_local_interrupt *li = &dst_vcpu->arch.local_int;
256 	/* handle (RE)START in user space */
257 	int rc = -EOPNOTSUPP;
258 
259 	/* make sure we don't race with STOP irq injection */
260 	spin_lock(&li->lock);
261 	if (kvm_s390_is_stop_irq_pending(dst_vcpu))
262 		rc = SIGP_CC_BUSY;
263 	spin_unlock(&li->lock);
264 
265 	return rc;
266 }
267 
__prepare_sigp_cpu_reset(struct kvm_vcpu * vcpu,struct kvm_vcpu * dst_vcpu,u8 order_code)268 static int __prepare_sigp_cpu_reset(struct kvm_vcpu *vcpu,
269 				    struct kvm_vcpu *dst_vcpu, u8 order_code)
270 {
271 	/* handle (INITIAL) CPU RESET in user space */
272 	return -EOPNOTSUPP;
273 }
274 
__prepare_sigp_unknown(struct kvm_vcpu * vcpu,struct kvm_vcpu * dst_vcpu)275 static int __prepare_sigp_unknown(struct kvm_vcpu *vcpu,
276 				  struct kvm_vcpu *dst_vcpu)
277 {
278 	/* handle unknown orders in user space */
279 	return -EOPNOTSUPP;
280 }
281 
handle_sigp_dst(struct kvm_vcpu * vcpu,u8 order_code,u16 cpu_addr,u32 parameter,u64 * status_reg)282 static int handle_sigp_dst(struct kvm_vcpu *vcpu, u8 order_code,
283 			   u16 cpu_addr, u32 parameter, u64 *status_reg)
284 {
285 	int rc;
286 	struct kvm_vcpu *dst_vcpu = kvm_get_vcpu_by_id(vcpu->kvm, cpu_addr);
287 
288 	if (!dst_vcpu)
289 		return SIGP_CC_NOT_OPERATIONAL;
290 
291 	/*
292 	 * SIGP RESTART, SIGP STOP, and SIGP STOP AND STORE STATUS orders
293 	 * are processed asynchronously. Until the affected VCPU finishes
294 	 * its work and calls back into KVM to clear the (RESTART or STOP)
295 	 * interrupt, we need to return any new non-reset orders "busy".
296 	 *
297 	 * This is important because a single VCPU could issue:
298 	 *  1) SIGP STOP $DESTINATION
299 	 *  2) SIGP SENSE $DESTINATION
300 	 *
301 	 * If the SIGP SENSE would not be rejected as "busy", it could
302 	 * return an incorrect answer as to whether the VCPU is STOPPED
303 	 * or OPERATING.
304 	 */
305 	if (order_code != SIGP_INITIAL_CPU_RESET &&
306 	    order_code != SIGP_CPU_RESET) {
307 		/*
308 		 * Lockless check. Both SIGP STOP and SIGP (RE)START
309 		 * properly synchronize everything while processing
310 		 * their orders, while the guest cannot observe a
311 		 * difference when issuing other orders from two
312 		 * different VCPUs.
313 		 */
314 		if (kvm_s390_is_stop_irq_pending(dst_vcpu) ||
315 		    kvm_s390_is_restart_irq_pending(dst_vcpu))
316 			return SIGP_CC_BUSY;
317 	}
318 
319 	switch (order_code) {
320 	case SIGP_SENSE:
321 		vcpu->stat.instruction_sigp_sense++;
322 		rc = __sigp_sense(vcpu, dst_vcpu, status_reg);
323 		break;
324 	case SIGP_EXTERNAL_CALL:
325 		vcpu->stat.instruction_sigp_external_call++;
326 		rc = __sigp_external_call(vcpu, dst_vcpu, status_reg);
327 		break;
328 	case SIGP_EMERGENCY_SIGNAL:
329 		vcpu->stat.instruction_sigp_emergency++;
330 		rc = __sigp_emergency(vcpu, dst_vcpu);
331 		break;
332 	case SIGP_STOP:
333 		vcpu->stat.instruction_sigp_stop++;
334 		rc = __sigp_stop(vcpu, dst_vcpu);
335 		break;
336 	case SIGP_STOP_AND_STORE_STATUS:
337 		vcpu->stat.instruction_sigp_stop_store_status++;
338 		rc = __sigp_stop_and_store_status(vcpu, dst_vcpu, status_reg);
339 		break;
340 	case SIGP_STORE_STATUS_AT_ADDRESS:
341 		vcpu->stat.instruction_sigp_store_status++;
342 		rc = __sigp_store_status_at_addr(vcpu, dst_vcpu, parameter,
343 						 status_reg);
344 		break;
345 	case SIGP_SET_PREFIX:
346 		vcpu->stat.instruction_sigp_prefix++;
347 		rc = __sigp_set_prefix(vcpu, dst_vcpu, parameter, status_reg);
348 		break;
349 	case SIGP_COND_EMERGENCY_SIGNAL:
350 		vcpu->stat.instruction_sigp_cond_emergency++;
351 		rc = __sigp_conditional_emergency(vcpu, dst_vcpu, parameter,
352 						  status_reg);
353 		break;
354 	case SIGP_SENSE_RUNNING:
355 		vcpu->stat.instruction_sigp_sense_running++;
356 		rc = __sigp_sense_running(vcpu, dst_vcpu, status_reg);
357 		break;
358 	case SIGP_START:
359 		vcpu->stat.instruction_sigp_start++;
360 		rc = __prepare_sigp_re_start(vcpu, dst_vcpu, order_code);
361 		break;
362 	case SIGP_RESTART:
363 		vcpu->stat.instruction_sigp_restart++;
364 		rc = __prepare_sigp_re_start(vcpu, dst_vcpu, order_code);
365 		break;
366 	case SIGP_INITIAL_CPU_RESET:
367 		vcpu->stat.instruction_sigp_init_cpu_reset++;
368 		rc = __prepare_sigp_cpu_reset(vcpu, dst_vcpu, order_code);
369 		break;
370 	case SIGP_CPU_RESET:
371 		vcpu->stat.instruction_sigp_cpu_reset++;
372 		rc = __prepare_sigp_cpu_reset(vcpu, dst_vcpu, order_code);
373 		break;
374 	default:
375 		vcpu->stat.instruction_sigp_unknown++;
376 		rc = __prepare_sigp_unknown(vcpu, dst_vcpu);
377 	}
378 
379 	if (rc == -EOPNOTSUPP)
380 		VCPU_EVENT(vcpu, 4,
381 			   "sigp order %u -> cpu %x: handled in user space",
382 			   order_code, dst_vcpu->vcpu_id);
383 
384 	return rc;
385 }
386 
handle_sigp_order_in_user_space(struct kvm_vcpu * vcpu,u8 order_code,u16 cpu_addr)387 static int handle_sigp_order_in_user_space(struct kvm_vcpu *vcpu, u8 order_code,
388 					   u16 cpu_addr)
389 {
390 	if (!vcpu->kvm->arch.user_sigp)
391 		return 0;
392 
393 	switch (order_code) {
394 	case SIGP_SENSE:
395 	case SIGP_EXTERNAL_CALL:
396 	case SIGP_EMERGENCY_SIGNAL:
397 	case SIGP_COND_EMERGENCY_SIGNAL:
398 	case SIGP_SENSE_RUNNING:
399 		return 0;
400 	/* update counters as we're directly dropping to user space */
401 	case SIGP_STOP:
402 		vcpu->stat.instruction_sigp_stop++;
403 		break;
404 	case SIGP_STOP_AND_STORE_STATUS:
405 		vcpu->stat.instruction_sigp_stop_store_status++;
406 		break;
407 	case SIGP_STORE_STATUS_AT_ADDRESS:
408 		vcpu->stat.instruction_sigp_store_status++;
409 		break;
410 	case SIGP_STORE_ADDITIONAL_STATUS:
411 		vcpu->stat.instruction_sigp_store_adtl_status++;
412 		break;
413 	case SIGP_SET_PREFIX:
414 		vcpu->stat.instruction_sigp_prefix++;
415 		break;
416 	case SIGP_START:
417 		vcpu->stat.instruction_sigp_start++;
418 		break;
419 	case SIGP_RESTART:
420 		vcpu->stat.instruction_sigp_restart++;
421 		break;
422 	case SIGP_INITIAL_CPU_RESET:
423 		vcpu->stat.instruction_sigp_init_cpu_reset++;
424 		break;
425 	case SIGP_CPU_RESET:
426 		vcpu->stat.instruction_sigp_cpu_reset++;
427 		break;
428 	default:
429 		vcpu->stat.instruction_sigp_unknown++;
430 	}
431 	VCPU_EVENT(vcpu, 3, "SIGP: order %u for CPU %d handled in userspace",
432 		   order_code, cpu_addr);
433 
434 	return 1;
435 }
436 
kvm_s390_handle_sigp(struct kvm_vcpu * vcpu)437 int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu)
438 {
439 	int r1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
440 	int r3 = vcpu->arch.sie_block->ipa & 0x000f;
441 	u32 parameter;
442 	u16 cpu_addr = vcpu->run->s.regs.gprs[r3];
443 	u8 order_code;
444 	int rc;
445 
446 	/* sigp in userspace can exit */
447 	if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
448 		return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
449 
450 	order_code = kvm_s390_get_base_disp_rs(vcpu, NULL);
451 	if (handle_sigp_order_in_user_space(vcpu, order_code, cpu_addr))
452 		return -EOPNOTSUPP;
453 
454 	if (r1 % 2)
455 		parameter = vcpu->run->s.regs.gprs[r1];
456 	else
457 		parameter = vcpu->run->s.regs.gprs[r1 + 1];
458 
459 	trace_kvm_s390_handle_sigp(vcpu, order_code, cpu_addr, parameter);
460 	switch (order_code) {
461 	case SIGP_SET_ARCHITECTURE:
462 		vcpu->stat.instruction_sigp_arch++;
463 		rc = __sigp_set_arch(vcpu, parameter,
464 				     &vcpu->run->s.regs.gprs[r1]);
465 		break;
466 	default:
467 		rc = handle_sigp_dst(vcpu, order_code, cpu_addr,
468 				     parameter,
469 				     &vcpu->run->s.regs.gprs[r1]);
470 	}
471 
472 	if (rc < 0)
473 		return rc;
474 
475 	kvm_s390_set_psw_cc(vcpu, rc);
476 	return 0;
477 }
478 
479 /*
480  * Handle SIGP partial execution interception.
481  *
482  * This interception will occur at the source cpu when a source cpu sends an
483  * external call to a target cpu and the target cpu has the WAIT bit set in
484  * its cpuflags. Interception will occurr after the interrupt indicator bits at
485  * the target cpu have been set. All error cases will lead to instruction
486  * interception, therefore nothing is to be checked or prepared.
487  */
kvm_s390_handle_sigp_pei(struct kvm_vcpu * vcpu)488 int kvm_s390_handle_sigp_pei(struct kvm_vcpu *vcpu)
489 {
490 	int r3 = vcpu->arch.sie_block->ipa & 0x000f;
491 	u16 cpu_addr = vcpu->run->s.regs.gprs[r3];
492 	struct kvm_vcpu *dest_vcpu;
493 	u8 order_code = kvm_s390_get_base_disp_rs(vcpu, NULL);
494 
495 	if (order_code == SIGP_EXTERNAL_CALL) {
496 		trace_kvm_s390_handle_sigp_pei(vcpu, order_code, cpu_addr);
497 
498 		dest_vcpu = kvm_get_vcpu_by_id(vcpu->kvm, cpu_addr);
499 		BUG_ON(dest_vcpu == NULL);
500 
501 		kvm_s390_vcpu_wakeup(dest_vcpu);
502 		kvm_s390_set_psw_cc(vcpu, SIGP_CC_ORDER_CODE_ACCEPTED);
503 		return 0;
504 	}
505 
506 	return -EOPNOTSUPP;
507 }
508