2 * sigp.c - handlinge interprocessor communication
4 * Copyright IBM Corp. 2008,2009
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.
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 * Christian Borntraeger <borntraeger@de.ibm.com>
12 * Christian Ehrhardt <ehrhardt@de.ibm.com>
15 #include <linux/kvm.h>
16 #include <linux/kvm_host.h>
17 #include <linux/slab.h>
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
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
48 static int __sigp_sense(struct kvm_vcpu *vcpu, u16 cpu_addr,
51 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
54 if (cpu_addr >= KVM_MAX_VCPUS)
55 return 3; /* not operational */
58 if (fi->local_int[cpu_addr] == NULL)
59 rc = 3; /* not operational */
60 else if (atomic_read(fi->local_int[cpu_addr]->cpuflags)
62 *reg &= 0xffffffff00000000UL;
63 rc = 1; /* status stored */
65 *reg &= 0xffffffff00000000UL;
66 *reg |= SIGP_STAT_STOPPED;
67 rc = 1; /* status stored */
69 spin_unlock(&fi->lock);
71 VCPU_EVENT(vcpu, 4, "sensed status of cpu %x rc %x", cpu_addr, rc);
75 static int __sigp_emergency(struct kvm_vcpu *vcpu, u16 cpu_addr)
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;
82 if (cpu_addr >= KVM_MAX_VCPUS)
83 return 3; /* not operational */
85 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
89 inti->type = KVM_S390_INT_EMERGENCY;
90 inti->emerg.code = vcpu->vcpu_id;
93 li = fi->local_int[cpu_addr];
95 rc = 3; /* not operational */
99 spin_lock_bh(&li->lock);
100 list_add_tail(&inti->list, &li->list);
101 atomic_set(&li->active, 1);
102 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
103 if (waitqueue_active(&li->wq))
104 wake_up_interruptible(&li->wq);
105 spin_unlock_bh(&li->lock);
106 rc = 0; /* order accepted */
107 VCPU_EVENT(vcpu, 4, "sent sigp emerg to cpu %x", cpu_addr);
109 spin_unlock(&fi->lock);
113 static int __sigp_external_call(struct kvm_vcpu *vcpu, u16 cpu_addr)
115 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
116 struct kvm_s390_local_interrupt *li;
117 struct kvm_s390_interrupt_info *inti;
120 if (cpu_addr >= KVM_MAX_VCPUS)
121 return 3; /* not operational */
123 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
127 inti->type = KVM_S390_INT_EXTERNAL_CALL;
128 inti->extcall.code = vcpu->vcpu_id;
130 spin_lock(&fi->lock);
131 li = fi->local_int[cpu_addr];
133 rc = 3; /* not operational */
137 spin_lock_bh(&li->lock);
138 list_add_tail(&inti->list, &li->list);
139 atomic_set(&li->active, 1);
140 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
141 if (waitqueue_active(&li->wq))
142 wake_up_interruptible(&li->wq);
143 spin_unlock_bh(&li->lock);
144 rc = 0; /* order accepted */
145 VCPU_EVENT(vcpu, 4, "sent sigp ext call to cpu %x", cpu_addr);
147 spin_unlock(&fi->lock);
151 static int __inject_sigp_stop(struct kvm_s390_local_interrupt *li, int action)
153 struct kvm_s390_interrupt_info *inti;
155 inti = kzalloc(sizeof(*inti), GFP_ATOMIC);
158 inti->type = KVM_S390_SIGP_STOP;
160 spin_lock_bh(&li->lock);
161 list_add_tail(&inti->list, &li->list);
162 atomic_set(&li->active, 1);
163 atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags);
164 li->action_bits |= action;
165 if (waitqueue_active(&li->wq))
166 wake_up_interruptible(&li->wq);
167 spin_unlock_bh(&li->lock);
169 return 0; /* order accepted */
172 static int __sigp_stop(struct kvm_vcpu *vcpu, u16 cpu_addr, int action)
174 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
175 struct kvm_s390_local_interrupt *li;
178 if (cpu_addr >= KVM_MAX_VCPUS)
179 return 3; /* not operational */
181 spin_lock(&fi->lock);
182 li = fi->local_int[cpu_addr];
184 rc = 3; /* not operational */
188 rc = __inject_sigp_stop(li, action);
191 spin_unlock(&fi->lock);
192 VCPU_EVENT(vcpu, 4, "sent sigp stop to cpu %x", cpu_addr);
196 int kvm_s390_inject_sigp_stop(struct kvm_vcpu *vcpu, int action)
198 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
199 return __inject_sigp_stop(li, action);
202 static int __sigp_set_arch(struct kvm_vcpu *vcpu, u32 parameter)
206 switch (parameter & 0xff) {
208 rc = 3; /* not operational */
212 rc = 0; /* order accepted */
220 static int __sigp_set_prefix(struct kvm_vcpu *vcpu, u16 cpu_addr, u32 address,
223 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
224 struct kvm_s390_local_interrupt *li = NULL;
225 struct kvm_s390_interrupt_info *inti;
229 /* make sure that the new value is valid memory */
230 address = address & 0x7fffe000u;
231 if (copy_from_guest_absolute(vcpu, &tmp, address, 1) ||
232 copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1)) {
233 *reg |= SIGP_STAT_INVALID_PARAMETER;
234 return 1; /* invalid parameter */
237 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
241 spin_lock(&fi->lock);
242 if (cpu_addr < KVM_MAX_VCPUS)
243 li = fi->local_int[cpu_addr];
246 rc = 1; /* incorrect state */
247 *reg &= SIGP_STAT_INCORRECT_STATE;
252 spin_lock_bh(&li->lock);
253 /* cpu must be in stopped state */
254 if (atomic_read(li->cpuflags) & CPUSTAT_RUNNING) {
255 rc = 1; /* incorrect state */
256 *reg &= SIGP_STAT_INCORRECT_STATE;
261 inti->type = KVM_S390_SIGP_SET_PREFIX;
262 inti->prefix.address = address;
264 list_add_tail(&inti->list, &li->list);
265 atomic_set(&li->active, 1);
266 if (waitqueue_active(&li->wq))
267 wake_up_interruptible(&li->wq);
268 rc = 0; /* order accepted */
270 VCPU_EVENT(vcpu, 4, "set prefix of cpu %02x to %x", cpu_addr, address);
272 spin_unlock_bh(&li->lock);
274 spin_unlock(&fi->lock);
278 int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu)
280 int r1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
281 int r3 = vcpu->arch.sie_block->ipa & 0x000f;
282 int base2 = vcpu->arch.sie_block->ipb >> 28;
283 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
285 u16 cpu_addr = vcpu->arch.guest_gprs[r3];
289 /* sigp in userspace can exit */
290 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
291 return kvm_s390_inject_program_int(vcpu,
292 PGM_PRIVILEGED_OPERATION);
296 order_code += vcpu->arch.guest_gprs[base2];
299 parameter = vcpu->arch.guest_gprs[r1];
301 parameter = vcpu->arch.guest_gprs[r1 + 1];
303 switch (order_code) {
305 vcpu->stat.instruction_sigp_sense++;
306 rc = __sigp_sense(vcpu, cpu_addr,
307 &vcpu->arch.guest_gprs[r1]);
309 case SIGP_EXTERNAL_CALL:
310 vcpu->stat.instruction_sigp_external_call++;
311 rc = __sigp_external_call(vcpu, cpu_addr);
314 vcpu->stat.instruction_sigp_emergency++;
315 rc = __sigp_emergency(vcpu, cpu_addr);
318 vcpu->stat.instruction_sigp_stop++;
319 rc = __sigp_stop(vcpu, cpu_addr, ACTION_STOP_ON_STOP);
321 case SIGP_STOP_STORE_STATUS:
322 vcpu->stat.instruction_sigp_stop++;
323 rc = __sigp_stop(vcpu, cpu_addr, ACTION_STORE_ON_STOP);
326 vcpu->stat.instruction_sigp_arch++;
327 rc = __sigp_set_arch(vcpu, parameter);
329 case SIGP_SET_PREFIX:
330 vcpu->stat.instruction_sigp_prefix++;
331 rc = __sigp_set_prefix(vcpu, cpu_addr, parameter,
332 &vcpu->arch.guest_gprs[r1]);
335 vcpu->stat.instruction_sigp_restart++;
336 /* user space must know about restart */
344 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
345 vcpu->arch.sie_block->gpsw.mask |= (rc & 3ul) << 44;