2 * handling interprocessor communication
4 * Copyright IBM Corp. 2008, 2013
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, struct kvm_vcpu *dst_vcpu,
26 struct kvm_s390_local_interrupt *li;
30 li = &dst_vcpu->arch.local_int;
32 cpuflags = atomic_read(li->cpuflags);
33 if (!(cpuflags & (CPUSTAT_ECALL_PEND | CPUSTAT_STOPPED)))
34 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
36 *reg &= 0xffffffff00000000UL;
37 if (cpuflags & CPUSTAT_ECALL_PEND)
38 *reg |= SIGP_STATUS_EXT_CALL_PENDING;
39 if (cpuflags & CPUSTAT_STOPPED)
40 *reg |= SIGP_STATUS_STOPPED;
41 rc = SIGP_CC_STATUS_STORED;
44 VCPU_EVENT(vcpu, 4, "sensed status of cpu %x rc %x", dst_vcpu->vcpu_id,
49 static int __sigp_emergency(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu)
51 struct kvm_s390_interrupt s390int = {
52 .type = KVM_S390_INT_EMERGENCY,
53 .parm = vcpu->vcpu_id,
57 rc = kvm_s390_inject_vcpu(dst_vcpu, &s390int);
59 VCPU_EVENT(vcpu, 4, "sent sigp emerg to cpu %x",
62 return rc ? rc : SIGP_CC_ORDER_CODE_ACCEPTED;
65 static int __sigp_conditional_emergency(struct kvm_vcpu *vcpu,
66 struct kvm_vcpu *dst_vcpu,
69 const u64 psw_int_mask = PSW_MASK_IO | PSW_MASK_EXT;
74 flags = atomic_read(&dst_vcpu->arch.sie_block->cpuflags);
75 psw = &dst_vcpu->arch.sie_block->gpsw;
76 p_asn = dst_vcpu->arch.sie_block->gcr[4] & 0xffff; /* Primary ASN */
77 s_asn = dst_vcpu->arch.sie_block->gcr[3] & 0xffff; /* Secondary ASN */
79 /* Deliver the emergency signal? */
80 if (!(flags & CPUSTAT_STOPPED)
81 || (psw->mask & psw_int_mask) != psw_int_mask
82 || ((flags & CPUSTAT_WAIT) && psw->addr != 0)
83 || (!(flags & CPUSTAT_WAIT) && (asn == p_asn || asn == s_asn))) {
84 return __sigp_emergency(vcpu, dst_vcpu);
86 *reg &= 0xffffffff00000000UL;
87 *reg |= SIGP_STATUS_INCORRECT_STATE;
88 return SIGP_CC_STATUS_STORED;
92 static int __sigp_external_call(struct kvm_vcpu *vcpu,
93 struct kvm_vcpu *dst_vcpu)
95 struct kvm_s390_interrupt s390int = {
96 .type = KVM_S390_INT_EXTERNAL_CALL,
97 .parm = vcpu->vcpu_id,
101 rc = kvm_s390_inject_vcpu(dst_vcpu, &s390int);
103 VCPU_EVENT(vcpu, 4, "sent sigp ext call to cpu %x",
106 return rc ? rc : SIGP_CC_ORDER_CODE_ACCEPTED;
109 static int __inject_sigp_stop(struct kvm_vcpu *dst_vcpu, int action)
111 struct kvm_s390_local_interrupt *li = &dst_vcpu->arch.local_int;
112 struct kvm_s390_interrupt_info *inti;
113 int rc = SIGP_CC_ORDER_CODE_ACCEPTED;
115 inti = kzalloc(sizeof(*inti), GFP_ATOMIC);
118 inti->type = KVM_S390_SIGP_STOP;
120 spin_lock(&li->lock);
121 if (li->action_bits & ACTION_STOP_ON_STOP) {
122 /* another SIGP STOP is pending */
127 if ((atomic_read(li->cpuflags) & CPUSTAT_STOPPED)) {
129 if ((action & ACTION_STORE_ON_STOP) != 0)
133 list_add_tail(&inti->list, &li->list);
134 atomic_set(&li->active, 1);
135 li->action_bits |= action;
136 atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags);
137 kvm_s390_vcpu_wakeup(dst_vcpu);
139 spin_unlock(&li->lock);
144 static int __sigp_stop(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu,
149 rc = __inject_sigp_stop(dst_vcpu, action);
150 VCPU_EVENT(vcpu, 4, "sent sigp stop to cpu %x", dst_vcpu->vcpu_id);
152 if ((action & ACTION_STORE_ON_STOP) != 0 && rc == -ESHUTDOWN) {
153 /* If the CPU has already been stopped, we still have
154 * to save the status when doing stop-and-store. This
155 * has to be done after unlocking all spinlocks. */
156 rc = kvm_s390_store_status_unloaded(dst_vcpu,
157 KVM_S390_STORE_STATUS_NOADDR);
163 static int __sigp_set_arch(struct kvm_vcpu *vcpu, u32 parameter)
169 switch (parameter & 0xff) {
171 rc = SIGP_CC_NOT_OPERATIONAL;
175 kvm_for_each_vcpu(i, v, vcpu->kvm) {
176 v->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
177 kvm_clear_async_pf_completion_queue(v);
180 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
188 static int __sigp_set_prefix(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu,
189 u32 address, u64 *reg)
191 struct kvm_s390_local_interrupt *li;
192 struct kvm_s390_interrupt_info *inti;
195 li = &dst_vcpu->arch.local_int;
198 * Make sure the new value is valid memory. We only need to check the
199 * first page, since address is 8k aligned and memory pieces are always
200 * at least 1MB aligned and have at least a size of 1MB.
202 address &= 0x7fffe000u;
203 if (kvm_is_error_gpa(vcpu->kvm, address)) {
204 *reg &= 0xffffffff00000000UL;
205 *reg |= SIGP_STATUS_INVALID_PARAMETER;
206 return SIGP_CC_STATUS_STORED;
209 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
213 spin_lock(&li->lock);
214 /* cpu must be in stopped state */
215 if (!(atomic_read(li->cpuflags) & CPUSTAT_STOPPED)) {
216 *reg &= 0xffffffff00000000UL;
217 *reg |= SIGP_STATUS_INCORRECT_STATE;
218 rc = SIGP_CC_STATUS_STORED;
223 inti->type = KVM_S390_SIGP_SET_PREFIX;
224 inti->prefix.address = address;
226 list_add_tail(&inti->list, &li->list);
227 atomic_set(&li->active, 1);
228 kvm_s390_vcpu_wakeup(dst_vcpu);
229 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
231 VCPU_EVENT(vcpu, 4, "set prefix of cpu %02x to %x", dst_vcpu->vcpu_id,
234 spin_unlock(&li->lock);
238 static int __sigp_store_status_at_addr(struct kvm_vcpu *vcpu,
239 struct kvm_vcpu *dst_vcpu,
245 spin_lock(&dst_vcpu->arch.local_int.lock);
246 flags = atomic_read(dst_vcpu->arch.local_int.cpuflags);
247 spin_unlock(&dst_vcpu->arch.local_int.lock);
248 if (!(flags & CPUSTAT_STOPPED)) {
249 *reg &= 0xffffffff00000000UL;
250 *reg |= SIGP_STATUS_INCORRECT_STATE;
251 return SIGP_CC_STATUS_STORED;
255 rc = kvm_s390_store_status_unloaded(dst_vcpu, addr);
257 *reg &= 0xffffffff00000000UL;
258 *reg |= SIGP_STATUS_INVALID_PARAMETER;
259 rc = SIGP_CC_STATUS_STORED;
264 static int __sigp_sense_running(struct kvm_vcpu *vcpu,
265 struct kvm_vcpu *dst_vcpu, u64 *reg)
267 struct kvm_s390_local_interrupt *li;
270 li = &dst_vcpu->arch.local_int;
271 if (atomic_read(li->cpuflags) & CPUSTAT_RUNNING) {
273 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
276 *reg &= 0xffffffff00000000UL;
277 *reg |= SIGP_STATUS_NOT_RUNNING;
278 rc = SIGP_CC_STATUS_STORED;
281 VCPU_EVENT(vcpu, 4, "sensed running status of cpu %x rc %x",
282 dst_vcpu->vcpu_id, rc);
287 /* Test whether the destination CPU is available and not busy */
288 static int sigp_check_callable(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu)
290 struct kvm_s390_local_interrupt *li = &dst_vcpu->arch.local_int;
291 int rc = SIGP_CC_ORDER_CODE_ACCEPTED;
293 spin_lock(&li->lock);
294 if (li->action_bits & ACTION_STOP_ON_STOP)
296 spin_unlock(&li->lock);
301 static int handle_sigp_dst(struct kvm_vcpu *vcpu, u8 order_code,
302 u16 cpu_addr, u32 parameter, u64 *status_reg)
305 struct kvm_vcpu *dst_vcpu;
307 if (cpu_addr >= KVM_MAX_VCPUS)
308 return SIGP_CC_NOT_OPERATIONAL;
310 dst_vcpu = kvm_get_vcpu(vcpu->kvm, cpu_addr);
312 return SIGP_CC_NOT_OPERATIONAL;
314 switch (order_code) {
316 vcpu->stat.instruction_sigp_sense++;
317 rc = __sigp_sense(vcpu, dst_vcpu, status_reg);
319 case SIGP_EXTERNAL_CALL:
320 vcpu->stat.instruction_sigp_external_call++;
321 rc = __sigp_external_call(vcpu, dst_vcpu);
323 case SIGP_EMERGENCY_SIGNAL:
324 vcpu->stat.instruction_sigp_emergency++;
325 rc = __sigp_emergency(vcpu, dst_vcpu);
328 vcpu->stat.instruction_sigp_stop++;
329 rc = __sigp_stop(vcpu, dst_vcpu, ACTION_STOP_ON_STOP);
331 case SIGP_STOP_AND_STORE_STATUS:
332 vcpu->stat.instruction_sigp_stop++;
333 rc = __sigp_stop(vcpu, dst_vcpu, ACTION_STORE_ON_STOP |
334 ACTION_STOP_ON_STOP);
336 case SIGP_STORE_STATUS_AT_ADDRESS:
337 rc = __sigp_store_status_at_addr(vcpu, dst_vcpu, parameter,
340 case SIGP_SET_PREFIX:
341 vcpu->stat.instruction_sigp_prefix++;
342 rc = __sigp_set_prefix(vcpu, dst_vcpu, parameter, status_reg);
344 case SIGP_COND_EMERGENCY_SIGNAL:
345 rc = __sigp_conditional_emergency(vcpu, dst_vcpu, parameter,
348 case SIGP_SENSE_RUNNING:
349 vcpu->stat.instruction_sigp_sense_running++;
350 rc = __sigp_sense_running(vcpu, dst_vcpu, status_reg);
353 rc = sigp_check_callable(vcpu, dst_vcpu);
354 if (rc == SIGP_CC_ORDER_CODE_ACCEPTED)
355 rc = -EOPNOTSUPP; /* Handle START in user space */
358 vcpu->stat.instruction_sigp_restart++;
359 rc = sigp_check_callable(vcpu, dst_vcpu);
360 if (rc == SIGP_CC_ORDER_CODE_ACCEPTED) {
362 "sigp restart %x to handle userspace",
364 /* user space must know about restart */
375 int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu)
377 int r1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
378 int r3 = vcpu->arch.sie_block->ipa & 0x000f;
380 u16 cpu_addr = vcpu->run->s.regs.gprs[r3];
384 /* sigp in userspace can exit */
385 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
386 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
388 order_code = kvm_s390_get_base_disp_rs(vcpu);
391 parameter = vcpu->run->s.regs.gprs[r1];
393 parameter = vcpu->run->s.regs.gprs[r1 + 1];
395 trace_kvm_s390_handle_sigp(vcpu, order_code, cpu_addr, parameter);
396 switch (order_code) {
397 case SIGP_SET_ARCHITECTURE:
398 vcpu->stat.instruction_sigp_arch++;
399 rc = __sigp_set_arch(vcpu, parameter);
402 rc = handle_sigp_dst(vcpu, order_code, cpu_addr,
404 &vcpu->run->s.regs.gprs[r1]);
410 kvm_s390_set_psw_cc(vcpu, rc);
415 * Handle SIGP partial execution interception.
417 * This interception will occur at the source cpu when a source cpu sends an
418 * external call to a target cpu and the target cpu has the WAIT bit set in
419 * its cpuflags. Interception will occurr after the interrupt indicator bits at
420 * the target cpu have been set. All error cases will lead to instruction
421 * interception, therefore nothing is to be checked or prepared.
423 int kvm_s390_handle_sigp_pei(struct kvm_vcpu *vcpu)
425 int r3 = vcpu->arch.sie_block->ipa & 0x000f;
426 u16 cpu_addr = vcpu->run->s.regs.gprs[r3];
427 struct kvm_vcpu *dest_vcpu;
428 u8 order_code = kvm_s390_get_base_disp_rs(vcpu);
430 trace_kvm_s390_handle_sigp_pei(vcpu, order_code, cpu_addr);
432 if (order_code == SIGP_EXTERNAL_CALL) {
433 dest_vcpu = kvm_get_vcpu(vcpu->kvm, cpu_addr);
434 BUG_ON(dest_vcpu == NULL);
436 kvm_s390_vcpu_wakeup(dest_vcpu);
437 kvm_s390_set_psw_cc(vcpu, SIGP_CC_ORDER_CODE_ACCEPTED);