2 * handling 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>
23 static int __sigp_sense(struct kvm_vcpu *vcpu, u16 cpu_addr,
26 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
29 if (cpu_addr >= KVM_MAX_VCPUS)
30 return SIGP_CC_NOT_OPERATIONAL;
33 if (fi->local_int[cpu_addr] == NULL)
34 rc = SIGP_CC_NOT_OPERATIONAL;
35 else if (!(atomic_read(fi->local_int[cpu_addr]->cpuflags)
36 & (CPUSTAT_ECALL_PEND | CPUSTAT_STOPPED)))
37 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
39 *reg &= 0xffffffff00000000UL;
40 if (atomic_read(fi->local_int[cpu_addr]->cpuflags)
42 *reg |= SIGP_STATUS_EXT_CALL_PENDING;
43 if (atomic_read(fi->local_int[cpu_addr]->cpuflags)
45 *reg |= SIGP_STATUS_STOPPED;
46 rc = SIGP_CC_STATUS_STORED;
48 spin_unlock(&fi->lock);
50 VCPU_EVENT(vcpu, 4, "sensed status of cpu %x rc %x", cpu_addr, rc);
54 static int __sigp_emergency(struct kvm_vcpu *vcpu, u16 cpu_addr)
56 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
57 struct kvm_s390_local_interrupt *li;
58 struct kvm_s390_interrupt_info *inti;
61 if (cpu_addr >= KVM_MAX_VCPUS)
62 return SIGP_CC_NOT_OPERATIONAL;
64 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
68 inti->type = KVM_S390_INT_EMERGENCY;
69 inti->emerg.code = vcpu->vcpu_id;
72 li = fi->local_int[cpu_addr];
74 rc = SIGP_CC_NOT_OPERATIONAL;
78 spin_lock_bh(&li->lock);
79 list_add_tail(&inti->list, &li->list);
80 atomic_set(&li->active, 1);
81 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
82 if (waitqueue_active(li->wq))
83 wake_up_interruptible(li->wq);
84 spin_unlock_bh(&li->lock);
85 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
86 VCPU_EVENT(vcpu, 4, "sent sigp emerg to cpu %x", cpu_addr);
88 spin_unlock(&fi->lock);
92 static int __sigp_external_call(struct kvm_vcpu *vcpu, u16 cpu_addr)
94 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
95 struct kvm_s390_local_interrupt *li;
96 struct kvm_s390_interrupt_info *inti;
99 if (cpu_addr >= KVM_MAX_VCPUS)
100 return SIGP_CC_NOT_OPERATIONAL;
102 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
106 inti->type = KVM_S390_INT_EXTERNAL_CALL;
107 inti->extcall.code = vcpu->vcpu_id;
109 spin_lock(&fi->lock);
110 li = fi->local_int[cpu_addr];
112 rc = SIGP_CC_NOT_OPERATIONAL;
116 spin_lock_bh(&li->lock);
117 list_add_tail(&inti->list, &li->list);
118 atomic_set(&li->active, 1);
119 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
120 if (waitqueue_active(li->wq))
121 wake_up_interruptible(li->wq);
122 spin_unlock_bh(&li->lock);
123 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
124 VCPU_EVENT(vcpu, 4, "sent sigp ext call to cpu %x", cpu_addr);
126 spin_unlock(&fi->lock);
130 static int __inject_sigp_stop(struct kvm_s390_local_interrupt *li, int action)
132 struct kvm_s390_interrupt_info *inti;
133 int rc = SIGP_CC_ORDER_CODE_ACCEPTED;
135 inti = kzalloc(sizeof(*inti), GFP_ATOMIC);
138 inti->type = KVM_S390_SIGP_STOP;
140 spin_lock_bh(&li->lock);
141 if ((atomic_read(li->cpuflags) & CPUSTAT_STOPPED)) {
143 if ((action & ACTION_STORE_ON_STOP) != 0)
147 list_add_tail(&inti->list, &li->list);
148 atomic_set(&li->active, 1);
149 atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags);
150 li->action_bits |= action;
151 if (waitqueue_active(li->wq))
152 wake_up_interruptible(li->wq);
154 spin_unlock_bh(&li->lock);
159 static int __sigp_stop(struct kvm_vcpu *vcpu, u16 cpu_addr, int action)
161 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
162 struct kvm_s390_local_interrupt *li;
165 if (cpu_addr >= KVM_MAX_VCPUS)
166 return SIGP_CC_NOT_OPERATIONAL;
168 spin_lock(&fi->lock);
169 li = fi->local_int[cpu_addr];
171 rc = SIGP_CC_NOT_OPERATIONAL;
175 rc = __inject_sigp_stop(li, action);
178 spin_unlock(&fi->lock);
179 VCPU_EVENT(vcpu, 4, "sent sigp stop to cpu %x", cpu_addr);
181 if ((action & ACTION_STORE_ON_STOP) != 0 && rc == -ESHUTDOWN) {
182 /* If the CPU has already been stopped, we still have
183 * to save the status when doing stop-and-store. This
184 * has to be done after unlocking all spinlocks. */
185 struct kvm_vcpu *dst_vcpu = kvm_get_vcpu(vcpu->kvm, cpu_addr);
186 rc = kvm_s390_store_status_unloaded(dst_vcpu,
187 KVM_S390_STORE_STATUS_NOADDR);
193 int kvm_s390_inject_sigp_stop(struct kvm_vcpu *vcpu, int action)
195 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
196 return __inject_sigp_stop(li, action);
199 static int __sigp_set_arch(struct kvm_vcpu *vcpu, u32 parameter)
203 switch (parameter & 0xff) {
205 rc = SIGP_CC_NOT_OPERATIONAL;
209 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
217 static int __sigp_set_prefix(struct kvm_vcpu *vcpu, u16 cpu_addr, u32 address,
220 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
221 struct kvm_s390_local_interrupt *li = NULL;
222 struct kvm_s390_interrupt_info *inti;
226 /* make sure that the new value is valid memory */
227 address = address & 0x7fffe000u;
228 if (copy_from_guest_absolute(vcpu, &tmp, address, 1) ||
229 copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1)) {
230 *reg &= 0xffffffff00000000UL;
231 *reg |= SIGP_STATUS_INVALID_PARAMETER;
232 return SIGP_CC_STATUS_STORED;
235 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
239 spin_lock(&fi->lock);
240 if (cpu_addr < KVM_MAX_VCPUS)
241 li = fi->local_int[cpu_addr];
244 *reg &= 0xffffffff00000000UL;
245 *reg |= SIGP_STATUS_INCORRECT_STATE;
246 rc = SIGP_CC_STATUS_STORED;
251 spin_lock_bh(&li->lock);
252 /* cpu must be in stopped state */
253 if (!(atomic_read(li->cpuflags) & CPUSTAT_STOPPED)) {
254 *reg &= 0xffffffff00000000UL;
255 *reg |= SIGP_STATUS_INCORRECT_STATE;
256 rc = SIGP_CC_STATUS_STORED;
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 = SIGP_CC_ORDER_CODE_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 static int __sigp_store_status_at_addr(struct kvm_vcpu *vcpu, u16 cpu_id,
281 struct kvm_vcpu *dst_vcpu = NULL;
285 if (cpu_id < KVM_MAX_VCPUS)
286 dst_vcpu = kvm_get_vcpu(vcpu->kvm, cpu_id);
288 return SIGP_CC_NOT_OPERATIONAL;
290 spin_lock_bh(&dst_vcpu->arch.local_int.lock);
291 flags = atomic_read(dst_vcpu->arch.local_int.cpuflags);
292 spin_unlock_bh(&dst_vcpu->arch.local_int.lock);
293 if (!(flags & CPUSTAT_STOPPED)) {
294 *reg &= 0xffffffff00000000UL;
295 *reg |= SIGP_STATUS_INCORRECT_STATE;
296 return SIGP_CC_STATUS_STORED;
300 rc = kvm_s390_store_status_unloaded(dst_vcpu, addr);
302 *reg &= 0xffffffff00000000UL;
303 *reg |= SIGP_STATUS_INVALID_PARAMETER;
304 rc = SIGP_CC_STATUS_STORED;
309 static int __sigp_sense_running(struct kvm_vcpu *vcpu, u16 cpu_addr,
313 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
315 if (cpu_addr >= KVM_MAX_VCPUS)
316 return SIGP_CC_NOT_OPERATIONAL;
318 spin_lock(&fi->lock);
319 if (fi->local_int[cpu_addr] == NULL)
320 rc = SIGP_CC_NOT_OPERATIONAL;
322 if (atomic_read(fi->local_int[cpu_addr]->cpuflags)
325 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
328 *reg &= 0xffffffff00000000UL;
329 *reg |= SIGP_STATUS_NOT_RUNNING;
330 rc = SIGP_CC_STATUS_STORED;
333 spin_unlock(&fi->lock);
335 VCPU_EVENT(vcpu, 4, "sensed running status of cpu %x rc %x", cpu_addr,
341 static int __sigp_restart(struct kvm_vcpu *vcpu, u16 cpu_addr)
343 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
344 struct kvm_s390_local_interrupt *li;
345 int rc = SIGP_CC_ORDER_CODE_ACCEPTED;
347 if (cpu_addr >= KVM_MAX_VCPUS)
348 return SIGP_CC_NOT_OPERATIONAL;
350 spin_lock(&fi->lock);
351 li = fi->local_int[cpu_addr];
353 rc = SIGP_CC_NOT_OPERATIONAL;
357 spin_lock_bh(&li->lock);
358 if (li->action_bits & ACTION_STOP_ON_STOP)
361 VCPU_EVENT(vcpu, 4, "sigp restart %x to handle userspace",
363 spin_unlock_bh(&li->lock);
365 spin_unlock(&fi->lock);
369 int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu)
371 int r1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
372 int r3 = vcpu->arch.sie_block->ipa & 0x000f;
374 u16 cpu_addr = vcpu->run->s.regs.gprs[r3];
378 /* sigp in userspace can exit */
379 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
380 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
382 order_code = kvm_s390_get_base_disp_rs(vcpu);
385 parameter = vcpu->run->s.regs.gprs[r1];
387 parameter = vcpu->run->s.regs.gprs[r1 + 1];
389 trace_kvm_s390_handle_sigp(vcpu, order_code, cpu_addr, parameter);
390 switch (order_code) {
392 vcpu->stat.instruction_sigp_sense++;
393 rc = __sigp_sense(vcpu, cpu_addr,
394 &vcpu->run->s.regs.gprs[r1]);
396 case SIGP_EXTERNAL_CALL:
397 vcpu->stat.instruction_sigp_external_call++;
398 rc = __sigp_external_call(vcpu, cpu_addr);
400 case SIGP_EMERGENCY_SIGNAL:
401 vcpu->stat.instruction_sigp_emergency++;
402 rc = __sigp_emergency(vcpu, cpu_addr);
405 vcpu->stat.instruction_sigp_stop++;
406 rc = __sigp_stop(vcpu, cpu_addr, ACTION_STOP_ON_STOP);
408 case SIGP_STOP_AND_STORE_STATUS:
409 vcpu->stat.instruction_sigp_stop++;
410 rc = __sigp_stop(vcpu, cpu_addr, ACTION_STORE_ON_STOP |
411 ACTION_STOP_ON_STOP);
413 case SIGP_STORE_STATUS_AT_ADDRESS:
414 rc = __sigp_store_status_at_addr(vcpu, cpu_addr, parameter,
415 &vcpu->run->s.regs.gprs[r1]);
417 case SIGP_SET_ARCHITECTURE:
418 vcpu->stat.instruction_sigp_arch++;
419 rc = __sigp_set_arch(vcpu, parameter);
421 case SIGP_SET_PREFIX:
422 vcpu->stat.instruction_sigp_prefix++;
423 rc = __sigp_set_prefix(vcpu, cpu_addr, parameter,
424 &vcpu->run->s.regs.gprs[r1]);
426 case SIGP_SENSE_RUNNING:
427 vcpu->stat.instruction_sigp_sense_running++;
428 rc = __sigp_sense_running(vcpu, cpu_addr,
429 &vcpu->run->s.regs.gprs[r1]);
432 vcpu->stat.instruction_sigp_restart++;
433 rc = __sigp_restart(vcpu, cpu_addr);
434 if (rc == SIGP_CC_BUSY)
436 /* user space must know about restart */
444 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
445 vcpu->arch.sie_block->gpsw.mask |= (rc & 3ul) << 44;