1 // SPDX-License-Identifier: GPL-2.0
3 * handling interprocessor communication
5 * Copyright IBM Corp. 2008, 2013
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License (version 2 only)
9 * as published by the Free Software Foundation.
11 * Author(s): Carsten Otte <cotte@de.ibm.com>
12 * Christian Borntraeger <borntraeger@de.ibm.com>
13 * Christian Ehrhardt <ehrhardt@de.ibm.com>
16 #include <linux/kvm.h>
17 #include <linux/kvm_host.h>
18 #include <linux/slab.h>
24 static int __sigp_sense(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu,
27 struct kvm_s390_local_interrupt *li;
32 li = &dst_vcpu->arch.local_int;
34 cpuflags = atomic_read(li->cpuflags);
35 ext_call_pending = kvm_s390_ext_call_pending(dst_vcpu);
36 if (!(cpuflags & CPUSTAT_STOPPED) && !ext_call_pending)
37 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
39 *reg &= 0xffffffff00000000UL;
41 *reg |= SIGP_STATUS_EXT_CALL_PENDING;
42 if (cpuflags & CPUSTAT_STOPPED)
43 *reg |= SIGP_STATUS_STOPPED;
44 rc = SIGP_CC_STATUS_STORED;
47 VCPU_EVENT(vcpu, 4, "sensed status of cpu %x rc %x", dst_vcpu->vcpu_id,
52 static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
53 struct kvm_vcpu *dst_vcpu)
55 struct kvm_s390_irq irq = {
56 .type = KVM_S390_INT_EMERGENCY,
57 .u.emerg.code = vcpu->vcpu_id,
61 rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
63 VCPU_EVENT(vcpu, 4, "sent sigp emerg to cpu %x",
66 return rc ? rc : SIGP_CC_ORDER_CODE_ACCEPTED;
69 static int __sigp_emergency(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu)
71 return __inject_sigp_emergency(vcpu, dst_vcpu);
74 static int __sigp_conditional_emergency(struct kvm_vcpu *vcpu,
75 struct kvm_vcpu *dst_vcpu,
78 const u64 psw_int_mask = PSW_MASK_IO | PSW_MASK_EXT;
83 idle = is_vcpu_idle(vcpu);
84 psw = &dst_vcpu->arch.sie_block->gpsw;
85 p_asn = dst_vcpu->arch.sie_block->gcr[4] & 0xffff; /* Primary ASN */
86 s_asn = dst_vcpu->arch.sie_block->gcr[3] & 0xffff; /* Secondary ASN */
88 /* Inject the emergency signal? */
89 if (!is_vcpu_stopped(vcpu)
90 || (psw->mask & psw_int_mask) != psw_int_mask
91 || (idle && psw->addr != 0)
92 || (!idle && (asn == p_asn || asn == s_asn))) {
93 return __inject_sigp_emergency(vcpu, dst_vcpu);
95 *reg &= 0xffffffff00000000UL;
96 *reg |= SIGP_STATUS_INCORRECT_STATE;
97 return SIGP_CC_STATUS_STORED;
101 static int __sigp_external_call(struct kvm_vcpu *vcpu,
102 struct kvm_vcpu *dst_vcpu, u64 *reg)
104 struct kvm_s390_irq irq = {
105 .type = KVM_S390_INT_EXTERNAL_CALL,
106 .u.extcall.code = vcpu->vcpu_id,
110 rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
112 *reg &= 0xffffffff00000000UL;
113 *reg |= SIGP_STATUS_EXT_CALL_PENDING;
114 return SIGP_CC_STATUS_STORED;
115 } else if (rc == 0) {
116 VCPU_EVENT(vcpu, 4, "sent sigp ext call to cpu %x",
120 return rc ? rc : SIGP_CC_ORDER_CODE_ACCEPTED;
123 static int __sigp_stop(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu)
125 struct kvm_s390_irq irq = {
126 .type = KVM_S390_SIGP_STOP,
130 rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
134 VCPU_EVENT(vcpu, 4, "sent sigp stop to cpu %x",
140 static int __sigp_stop_and_store_status(struct kvm_vcpu *vcpu,
141 struct kvm_vcpu *dst_vcpu, u64 *reg)
143 struct kvm_s390_irq irq = {
144 .type = KVM_S390_SIGP_STOP,
145 .u.stop.flags = KVM_S390_STOP_FLAG_STORE_STATUS,
149 rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
153 VCPU_EVENT(vcpu, 4, "sent sigp stop and store status to cpu %x",
159 static int __sigp_set_arch(struct kvm_vcpu *vcpu, u32 parameter,
164 bool all_stopped = true;
166 kvm_for_each_vcpu(i, v, vcpu->kvm) {
169 if (!is_vcpu_stopped(v))
173 *status_reg &= 0xffffffff00000000UL;
175 /* Reject set arch order, with czam we're always in z/Arch mode. */
176 *status_reg |= (all_stopped ? SIGP_STATUS_INVALID_PARAMETER :
177 SIGP_STATUS_INCORRECT_STATE);
178 return SIGP_CC_STATUS_STORED;
181 static int __sigp_set_prefix(struct kvm_vcpu *vcpu, struct kvm_vcpu *dst_vcpu,
182 u32 address, u64 *reg)
184 struct kvm_s390_irq irq = {
185 .type = KVM_S390_SIGP_SET_PREFIX,
186 .u.prefix.address = address & 0x7fffe000u,
191 * Make sure the new value is valid memory. We only need to check the
192 * first page, since address is 8k aligned and memory pieces are always
193 * at least 1MB aligned and have at least a size of 1MB.
195 if (kvm_is_error_gpa(vcpu->kvm, irq.u.prefix.address)) {
196 *reg &= 0xffffffff00000000UL;
197 *reg |= SIGP_STATUS_INVALID_PARAMETER;
198 return SIGP_CC_STATUS_STORED;
201 rc = kvm_s390_inject_vcpu(dst_vcpu, &irq);
203 *reg &= 0xffffffff00000000UL;
204 *reg |= SIGP_STATUS_INCORRECT_STATE;
205 return SIGP_CC_STATUS_STORED;
211 static int __sigp_store_status_at_addr(struct kvm_vcpu *vcpu,
212 struct kvm_vcpu *dst_vcpu,
218 flags = atomic_read(dst_vcpu->arch.local_int.cpuflags);
219 if (!(flags & CPUSTAT_STOPPED)) {
220 *reg &= 0xffffffff00000000UL;
221 *reg |= SIGP_STATUS_INCORRECT_STATE;
222 return SIGP_CC_STATUS_STORED;
226 rc = kvm_s390_store_status_unloaded(dst_vcpu, addr);
228 *reg &= 0xffffffff00000000UL;
229 *reg |= SIGP_STATUS_INVALID_PARAMETER;
230 rc = SIGP_CC_STATUS_STORED;
235 static int __sigp_sense_running(struct kvm_vcpu *vcpu,
236 struct kvm_vcpu *dst_vcpu, u64 *reg)
238 struct kvm_s390_local_interrupt *li;
241 if (!test_kvm_facility(vcpu->kvm, 9)) {
242 *reg &= 0xffffffff00000000UL;
243 *reg |= SIGP_STATUS_INVALID_ORDER;
244 return SIGP_CC_STATUS_STORED;
247 li = &dst_vcpu->arch.local_int;
248 if (atomic_read(li->cpuflags) & CPUSTAT_RUNNING) {
250 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
253 *reg &= 0xffffffff00000000UL;
254 *reg |= SIGP_STATUS_NOT_RUNNING;
255 rc = SIGP_CC_STATUS_STORED;
258 VCPU_EVENT(vcpu, 4, "sensed running status of cpu %x rc %x",
259 dst_vcpu->vcpu_id, rc);
264 static int __prepare_sigp_re_start(struct kvm_vcpu *vcpu,
265 struct kvm_vcpu *dst_vcpu, u8 order_code)
267 struct kvm_s390_local_interrupt *li = &dst_vcpu->arch.local_int;
268 /* handle (RE)START in user space */
269 int rc = -EOPNOTSUPP;
271 /* make sure we don't race with STOP irq injection */
272 spin_lock(&li->lock);
273 if (kvm_s390_is_stop_irq_pending(dst_vcpu))
275 spin_unlock(&li->lock);
280 static int __prepare_sigp_cpu_reset(struct kvm_vcpu *vcpu,
281 struct kvm_vcpu *dst_vcpu, u8 order_code)
283 /* handle (INITIAL) CPU RESET in user space */
287 static int __prepare_sigp_unknown(struct kvm_vcpu *vcpu,
288 struct kvm_vcpu *dst_vcpu)
290 /* handle unknown orders in user space */
294 static int handle_sigp_dst(struct kvm_vcpu *vcpu, u8 order_code,
295 u16 cpu_addr, u32 parameter, u64 *status_reg)
298 struct kvm_vcpu *dst_vcpu = kvm_get_vcpu_by_id(vcpu->kvm, cpu_addr);
301 return SIGP_CC_NOT_OPERATIONAL;
303 switch (order_code) {
305 vcpu->stat.instruction_sigp_sense++;
306 rc = __sigp_sense(vcpu, dst_vcpu, status_reg);
308 case SIGP_EXTERNAL_CALL:
309 vcpu->stat.instruction_sigp_external_call++;
310 rc = __sigp_external_call(vcpu, dst_vcpu, status_reg);
312 case SIGP_EMERGENCY_SIGNAL:
313 vcpu->stat.instruction_sigp_emergency++;
314 rc = __sigp_emergency(vcpu, dst_vcpu);
317 vcpu->stat.instruction_sigp_stop++;
318 rc = __sigp_stop(vcpu, dst_vcpu);
320 case SIGP_STOP_AND_STORE_STATUS:
321 vcpu->stat.instruction_sigp_stop_store_status++;
322 rc = __sigp_stop_and_store_status(vcpu, dst_vcpu, status_reg);
324 case SIGP_STORE_STATUS_AT_ADDRESS:
325 vcpu->stat.instruction_sigp_store_status++;
326 rc = __sigp_store_status_at_addr(vcpu, dst_vcpu, parameter,
329 case SIGP_SET_PREFIX:
330 vcpu->stat.instruction_sigp_prefix++;
331 rc = __sigp_set_prefix(vcpu, dst_vcpu, parameter, status_reg);
333 case SIGP_COND_EMERGENCY_SIGNAL:
334 vcpu->stat.instruction_sigp_cond_emergency++;
335 rc = __sigp_conditional_emergency(vcpu, dst_vcpu, parameter,
338 case SIGP_SENSE_RUNNING:
339 vcpu->stat.instruction_sigp_sense_running++;
340 rc = __sigp_sense_running(vcpu, dst_vcpu, status_reg);
343 vcpu->stat.instruction_sigp_start++;
344 rc = __prepare_sigp_re_start(vcpu, dst_vcpu, order_code);
347 vcpu->stat.instruction_sigp_restart++;
348 rc = __prepare_sigp_re_start(vcpu, dst_vcpu, order_code);
350 case SIGP_INITIAL_CPU_RESET:
351 vcpu->stat.instruction_sigp_init_cpu_reset++;
352 rc = __prepare_sigp_cpu_reset(vcpu, dst_vcpu, order_code);
355 vcpu->stat.instruction_sigp_cpu_reset++;
356 rc = __prepare_sigp_cpu_reset(vcpu, dst_vcpu, order_code);
359 vcpu->stat.instruction_sigp_unknown++;
360 rc = __prepare_sigp_unknown(vcpu, dst_vcpu);
363 if (rc == -EOPNOTSUPP)
365 "sigp order %u -> cpu %x: handled in user space",
366 order_code, dst_vcpu->vcpu_id);
371 static int handle_sigp_order_in_user_space(struct kvm_vcpu *vcpu, u8 order_code,
374 if (!vcpu->kvm->arch.user_sigp)
377 switch (order_code) {
379 case SIGP_EXTERNAL_CALL:
380 case SIGP_EMERGENCY_SIGNAL:
381 case SIGP_COND_EMERGENCY_SIGNAL:
382 case SIGP_SENSE_RUNNING:
384 /* update counters as we're directly dropping to user space */
386 vcpu->stat.instruction_sigp_stop++;
388 case SIGP_STOP_AND_STORE_STATUS:
389 vcpu->stat.instruction_sigp_stop_store_status++;
391 case SIGP_STORE_STATUS_AT_ADDRESS:
392 vcpu->stat.instruction_sigp_store_status++;
394 case SIGP_STORE_ADDITIONAL_STATUS:
395 vcpu->stat.instruction_sigp_store_adtl_status++;
397 case SIGP_SET_PREFIX:
398 vcpu->stat.instruction_sigp_prefix++;
401 vcpu->stat.instruction_sigp_start++;
404 vcpu->stat.instruction_sigp_restart++;
406 case SIGP_INITIAL_CPU_RESET:
407 vcpu->stat.instruction_sigp_init_cpu_reset++;
410 vcpu->stat.instruction_sigp_cpu_reset++;
413 vcpu->stat.instruction_sigp_unknown++;
415 VCPU_EVENT(vcpu, 3, "SIGP: order %u for CPU %d handled in userspace",
416 order_code, cpu_addr);
421 int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu)
423 int r1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
424 int r3 = vcpu->arch.sie_block->ipa & 0x000f;
426 u16 cpu_addr = vcpu->run->s.regs.gprs[r3];
430 /* sigp in userspace can exit */
431 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
432 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
434 order_code = kvm_s390_get_base_disp_rs(vcpu, NULL);
435 if (handle_sigp_order_in_user_space(vcpu, order_code, cpu_addr))
439 parameter = vcpu->run->s.regs.gprs[r1];
441 parameter = vcpu->run->s.regs.gprs[r1 + 1];
443 trace_kvm_s390_handle_sigp(vcpu, order_code, cpu_addr, parameter);
444 switch (order_code) {
445 case SIGP_SET_ARCHITECTURE:
446 vcpu->stat.instruction_sigp_arch++;
447 rc = __sigp_set_arch(vcpu, parameter,
448 &vcpu->run->s.regs.gprs[r1]);
451 rc = handle_sigp_dst(vcpu, order_code, cpu_addr,
453 &vcpu->run->s.regs.gprs[r1]);
459 kvm_s390_set_psw_cc(vcpu, rc);
464 * Handle SIGP partial execution interception.
466 * This interception will occur at the source cpu when a source cpu sends an
467 * external call to a target cpu and the target cpu has the WAIT bit set in
468 * its cpuflags. Interception will occurr after the interrupt indicator bits at
469 * the target cpu have been set. All error cases will lead to instruction
470 * interception, therefore nothing is to be checked or prepared.
472 int kvm_s390_handle_sigp_pei(struct kvm_vcpu *vcpu)
474 int r3 = vcpu->arch.sie_block->ipa & 0x000f;
475 u16 cpu_addr = vcpu->run->s.regs.gprs[r3];
476 struct kvm_vcpu *dest_vcpu;
477 u8 order_code = kvm_s390_get_base_disp_rs(vcpu, NULL);
479 trace_kvm_s390_handle_sigp_pei(vcpu, order_code, cpu_addr);
481 if (order_code == SIGP_EXTERNAL_CALL) {
482 dest_vcpu = kvm_get_vcpu_by_id(vcpu->kvm, cpu_addr);
483 BUG_ON(dest_vcpu == NULL);
485 kvm_s390_vcpu_wakeup(dest_vcpu);
486 kvm_s390_set_psw_cc(vcpu, SIGP_CC_ORDER_CODE_ACCEPTED);