Linux Audio

Check our new training course

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