Linux Audio

Check our new training course

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