KVM: s390: sigp: dispatch orders with one target in a separate function
[deliverable/linux.git] / arch / s390 / kvm / sigp.c
CommitLineData
5288fbf0 1/*
a53c8fab 2 * handling interprocessor communication
5288fbf0 3 *
b13d3580 4 * Copyright IBM Corp. 2008, 2013
5288fbf0
CB
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>
9ace903d 12 * Christian Ehrhardt <ehrhardt@de.ibm.com>
5288fbf0
CB
13 */
14
15#include <linux/kvm.h>
16#include <linux/kvm_host.h>
5a0e3ad6 17#include <linux/slab.h>
a9ae32c3 18#include <asm/sigp.h>
5288fbf0
CB
19#include "gaccess.h"
20#include "kvm-s390.h"
5786fffa 21#include "trace.h"
5288fbf0 22
0096369d 23static int __sigp_sense(struct kvm_vcpu *vcpu, u16 cpu_addr,
5a32c1af 24 u64 *reg)
5288fbf0 25{
1ee0bc55
JF
26 struct kvm_s390_local_interrupt *li;
27 struct kvm_vcpu *dst_vcpu = NULL;
28 int cpuflags;
5288fbf0
CB
29 int rc;
30
31 if (cpu_addr >= KVM_MAX_VCPUS)
ea1918dd 32 return SIGP_CC_NOT_OPERATIONAL;
5288fbf0 33
1ee0bc55
JF
34 dst_vcpu = kvm_get_vcpu(vcpu->kvm, cpu_addr);
35 if (!dst_vcpu)
36 return SIGP_CC_NOT_OPERATIONAL;
37 li = &dst_vcpu->arch.local_int;
38
39 cpuflags = atomic_read(li->cpuflags);
40 if (!(cpuflags & (CPUSTAT_ECALL_PEND | CPUSTAT_STOPPED)))
21b26c08
CH
41 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
42 else {
5288fbf0 43 *reg &= 0xffffffff00000000UL;
1ee0bc55 44 if (cpuflags & CPUSTAT_ECALL_PEND)
21b26c08 45 *reg |= SIGP_STATUS_EXT_CALL_PENDING;
1ee0bc55 46 if (cpuflags & CPUSTAT_STOPPED)
21b26c08 47 *reg |= SIGP_STATUS_STOPPED;
ea1918dd 48 rc = SIGP_CC_STATUS_STORED;
5288fbf0 49 }
5288fbf0
CB
50
51 VCPU_EVENT(vcpu, 4, "sensed status of cpu %x rc %x", cpu_addr, rc);
52 return rc;
53}
54
55static int __sigp_emergency(struct kvm_vcpu *vcpu, u16 cpu_addr)
56{
22ff4a33
JF
57 struct kvm_s390_interrupt s390int = {
58 .type = KVM_S390_INT_EMERGENCY,
59 .parm = vcpu->vcpu_id,
60 };
1ee0bc55 61 struct kvm_vcpu *dst_vcpu = NULL;
22ff4a33 62 int rc = 0;
5288fbf0 63
91880d07
TH
64 if (cpu_addr < KVM_MAX_VCPUS)
65 dst_vcpu = kvm_get_vcpu(vcpu->kvm, cpu_addr);
66 if (!dst_vcpu)
ea1918dd 67 return SIGP_CC_NOT_OPERATIONAL;
5288fbf0 68
22ff4a33
JF
69 rc = kvm_s390_inject_vcpu(dst_vcpu, &s390int);
70 if (!rc)
71 VCPU_EVENT(vcpu, 4, "sent sigp emerg to cpu %x", cpu_addr);
5288fbf0 72
22ff4a33 73 return rc ? rc : SIGP_CC_ORDER_CODE_ACCEPTED;
7697e71f
CE
74}
75
b13d3580
TH
76static int __sigp_conditional_emergency(struct kvm_vcpu *vcpu, u16 cpu_addr,
77 u16 asn, u64 *reg)
78{
79 struct kvm_vcpu *dst_vcpu = NULL;
80 const u64 psw_int_mask = PSW_MASK_IO | PSW_MASK_EXT;
81 u16 p_asn, s_asn;
82 psw_t *psw;
83 u32 flags;
84
85 if (cpu_addr < KVM_MAX_VCPUS)
86 dst_vcpu = kvm_get_vcpu(vcpu->kvm, cpu_addr);
87 if (!dst_vcpu)
88 return SIGP_CC_NOT_OPERATIONAL;
89 flags = atomic_read(&dst_vcpu->arch.sie_block->cpuflags);
90 psw = &dst_vcpu->arch.sie_block->gpsw;
91 p_asn = dst_vcpu->arch.sie_block->gcr[4] & 0xffff; /* Primary ASN */
92 s_asn = dst_vcpu->arch.sie_block->gcr[3] & 0xffff; /* Secondary ASN */
93
94 /* Deliver the emergency signal? */
95 if (!(flags & CPUSTAT_STOPPED)
96 || (psw->mask & psw_int_mask) != psw_int_mask
97 || ((flags & CPUSTAT_WAIT) && psw->addr != 0)
98 || (!(flags & CPUSTAT_WAIT) && (asn == p_asn || asn == s_asn))) {
99 return __sigp_emergency(vcpu, cpu_addr);
100 } else {
101 *reg &= 0xffffffff00000000UL;
102 *reg |= SIGP_STATUS_INCORRECT_STATE;
103 return SIGP_CC_STATUS_STORED;
104 }
105}
106
7697e71f
CE
107static int __sigp_external_call(struct kvm_vcpu *vcpu, u16 cpu_addr)
108{
22ff4a33
JF
109 struct kvm_s390_interrupt s390int = {
110 .type = KVM_S390_INT_EXTERNAL_CALL,
111 .parm = vcpu->vcpu_id,
112 };
1ee0bc55 113 struct kvm_vcpu *dst_vcpu = NULL;
22ff4a33 114 int rc;
7697e71f 115
91880d07
TH
116 if (cpu_addr < KVM_MAX_VCPUS)
117 dst_vcpu = kvm_get_vcpu(vcpu->kvm, cpu_addr);
118 if (!dst_vcpu)
ea1918dd 119 return SIGP_CC_NOT_OPERATIONAL;
7697e71f 120
22ff4a33
JF
121 rc = kvm_s390_inject_vcpu(dst_vcpu, &s390int);
122 if (!rc)
123 VCPU_EVENT(vcpu, 4, "sent sigp ext call to cpu %x", cpu_addr);
7697e71f 124
22ff4a33 125 return rc ? rc : SIGP_CC_ORDER_CODE_ACCEPTED;
5288fbf0
CB
126}
127
0e9c85a5 128static int __inject_sigp_stop(struct kvm_vcpu *dst_vcpu, int action)
5288fbf0 129{
0e9c85a5 130 struct kvm_s390_local_interrupt *li = &dst_vcpu->arch.local_int;
180c12fb 131 struct kvm_s390_interrupt_info *inti;
e879892c 132 int rc = SIGP_CC_ORDER_CODE_ACCEPTED;
5288fbf0 133
9940fa80 134 inti = kzalloc(sizeof(*inti), GFP_ATOMIC);
5288fbf0
CB
135 if (!inti)
136 return -ENOMEM;
5288fbf0
CB
137 inti->type = KVM_S390_SIGP_STOP;
138
4ae3c081 139 spin_lock(&li->lock);
7dfc63cf
DH
140 if (li->action_bits & ACTION_STOP_ON_STOP) {
141 /* another SIGP STOP is pending */
d514f426 142 kfree(inti);
7dfc63cf
DH
143 rc = SIGP_CC_BUSY;
144 goto out;
145 }
a046b816
CD
146 if ((atomic_read(li->cpuflags) & CPUSTAT_STOPPED)) {
147 kfree(inti);
e879892c
TH
148 if ((action & ACTION_STORE_ON_STOP) != 0)
149 rc = -ESHUTDOWN;
24a13044 150 goto out;
a046b816 151 }
5288fbf0
CB
152 list_add_tail(&inti->list, &li->list);
153 atomic_set(&li->active, 1);
9ace903d 154 li->action_bits |= action;
7dfc63cf 155 atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags);
0e9c85a5 156 kvm_s390_vcpu_wakeup(dst_vcpu);
24a13044 157out:
4ae3c081 158 spin_unlock(&li->lock);
9ace903d 159
e879892c 160 return rc;
9ace903d
CE
161}
162
163static int __sigp_stop(struct kvm_vcpu *vcpu, u16 cpu_addr, int action)
164{
1ee0bc55 165 struct kvm_vcpu *dst_vcpu = NULL;
9ace903d
CE
166 int rc;
167
168 if (cpu_addr >= KVM_MAX_VCPUS)
ea1918dd 169 return SIGP_CC_NOT_OPERATIONAL;
9ace903d 170
1ee0bc55
JF
171 dst_vcpu = kvm_get_vcpu(vcpu->kvm, cpu_addr);
172 if (!dst_vcpu)
173 return SIGP_CC_NOT_OPERATIONAL;
9ace903d 174
0e9c85a5 175 rc = __inject_sigp_stop(dst_vcpu, action);
9ace903d 176
5288fbf0 177 VCPU_EVENT(vcpu, 4, "sent sigp stop to cpu %x", cpu_addr);
e879892c
TH
178
179 if ((action & ACTION_STORE_ON_STOP) != 0 && rc == -ESHUTDOWN) {
180 /* If the CPU has already been stopped, we still have
181 * to save the status when doing stop-and-store. This
182 * has to be done after unlocking all spinlocks. */
e879892c
TH
183 rc = kvm_s390_store_status_unloaded(dst_vcpu,
184 KVM_S390_STORE_STATUS_NOADDR);
185 }
186
5288fbf0
CB
187 return rc;
188}
189
190static int __sigp_set_arch(struct kvm_vcpu *vcpu, u32 parameter)
191{
192 int rc;
3c038e6b
DD
193 unsigned int i;
194 struct kvm_vcpu *v;
5288fbf0
CB
195
196 switch (parameter & 0xff) {
197 case 0:
ea1918dd 198 rc = SIGP_CC_NOT_OPERATIONAL;
5288fbf0
CB
199 break;
200 case 1:
201 case 2:
3c038e6b
DD
202 kvm_for_each_vcpu(i, v, vcpu->kvm) {
203 v->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
204 kvm_clear_async_pf_completion_queue(v);
205 }
206
ea1918dd 207 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
5288fbf0
CB
208 break;
209 default:
b8e660b8 210 rc = -EOPNOTSUPP;
5288fbf0
CB
211 }
212 return rc;
213}
214
215static int __sigp_set_prefix(struct kvm_vcpu *vcpu, u16 cpu_addr, u32 address,
5a32c1af 216 u64 *reg)
5288fbf0 217{
13b191ae
TH
218 struct kvm_s390_local_interrupt *li;
219 struct kvm_vcpu *dst_vcpu = NULL;
180c12fb 220 struct kvm_s390_interrupt_info *inti;
5288fbf0 221 int rc;
5288fbf0 222
13b191ae
TH
223 if (cpu_addr < KVM_MAX_VCPUS)
224 dst_vcpu = kvm_get_vcpu(vcpu->kvm, cpu_addr);
225 if (!dst_vcpu)
226 return SIGP_CC_NOT_OPERATIONAL;
227 li = &dst_vcpu->arch.local_int;
228
665170cb
HC
229 /*
230 * Make sure the new value is valid memory. We only need to check the
231 * first page, since address is 8k aligned and memory pieces are always
232 * at least 1MB aligned and have at least a size of 1MB.
233 */
234 address &= 0x7fffe000u;
235 if (kvm_is_error_gpa(vcpu->kvm, address)) {
0744426e 236 *reg &= 0xffffffff00000000UL;
a9ae32c3 237 *reg |= SIGP_STATUS_INVALID_PARAMETER;
ea1918dd 238 return SIGP_CC_STATUS_STORED;
5288fbf0
CB
239 }
240
241 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
242 if (!inti)
ea1918dd 243 return SIGP_CC_BUSY;
5288fbf0 244
4ae3c081 245 spin_lock(&li->lock);
5288fbf0 246 /* cpu must be in stopped state */
9e6dabef 247 if (!(atomic_read(li->cpuflags) & CPUSTAT_STOPPED)) {
0744426e
HC
248 *reg &= 0xffffffff00000000UL;
249 *reg |= SIGP_STATUS_INCORRECT_STATE;
ea1918dd 250 rc = SIGP_CC_STATUS_STORED;
5288fbf0
CB
251 kfree(inti);
252 goto out_li;
253 }
254
255 inti->type = KVM_S390_SIGP_SET_PREFIX;
256 inti->prefix.address = address;
257
258 list_add_tail(&inti->list, &li->list);
259 atomic_set(&li->active, 1);
0e9c85a5 260 kvm_s390_vcpu_wakeup(dst_vcpu);
ea1918dd 261 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
5288fbf0
CB
262
263 VCPU_EVENT(vcpu, 4, "set prefix of cpu %02x to %x", cpu_addr, address);
264out_li:
4ae3c081 265 spin_unlock(&li->lock);
5288fbf0
CB
266 return rc;
267}
268
00e9e435
TH
269static int __sigp_store_status_at_addr(struct kvm_vcpu *vcpu, u16 cpu_id,
270 u32 addr, u64 *reg)
271{
272 struct kvm_vcpu *dst_vcpu = NULL;
273 int flags;
274 int rc;
275
276 if (cpu_id < KVM_MAX_VCPUS)
277 dst_vcpu = kvm_get_vcpu(vcpu->kvm, cpu_id);
278 if (!dst_vcpu)
279 return SIGP_CC_NOT_OPERATIONAL;
280
4ae3c081 281 spin_lock(&dst_vcpu->arch.local_int.lock);
00e9e435 282 flags = atomic_read(dst_vcpu->arch.local_int.cpuflags);
4ae3c081 283 spin_unlock(&dst_vcpu->arch.local_int.lock);
00e9e435
TH
284 if (!(flags & CPUSTAT_STOPPED)) {
285 *reg &= 0xffffffff00000000UL;
286 *reg |= SIGP_STATUS_INCORRECT_STATE;
287 return SIGP_CC_STATUS_STORED;
288 }
289
290 addr &= 0x7ffffe00;
291 rc = kvm_s390_store_status_unloaded(dst_vcpu, addr);
292 if (rc == -EFAULT) {
293 *reg &= 0xffffffff00000000UL;
294 *reg |= SIGP_STATUS_INVALID_PARAMETER;
295 rc = SIGP_CC_STATUS_STORED;
296 }
297 return rc;
298}
299
bd59d3a4 300static int __sigp_sense_running(struct kvm_vcpu *vcpu, u16 cpu_addr,
5a32c1af 301 u64 *reg)
bd59d3a4 302{
1ee0bc55
JF
303 struct kvm_s390_local_interrupt *li;
304 struct kvm_vcpu *dst_vcpu = NULL;
bd59d3a4 305 int rc;
bd59d3a4
CH
306
307 if (cpu_addr >= KVM_MAX_VCPUS)
ea1918dd 308 return SIGP_CC_NOT_OPERATIONAL;
bd59d3a4 309
1ee0bc55
JF
310 dst_vcpu = kvm_get_vcpu(vcpu->kvm, cpu_addr);
311 if (!dst_vcpu)
312 return SIGP_CC_NOT_OPERATIONAL;
313 li = &dst_vcpu->arch.local_int;
314 if (atomic_read(li->cpuflags) & CPUSTAT_RUNNING) {
315 /* running */
316 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
317 } else {
318 /* not running */
319 *reg &= 0xffffffff00000000UL;
320 *reg |= SIGP_STATUS_NOT_RUNNING;
321 rc = SIGP_CC_STATUS_STORED;
bd59d3a4 322 }
bd59d3a4
CH
323
324 VCPU_EVENT(vcpu, 4, "sensed running status of cpu %x rc %x", cpu_addr,
325 rc);
326
327 return rc;
328}
329
cc92d6de
TH
330/* Test whether the destination CPU is available and not busy */
331static int sigp_check_callable(struct kvm_vcpu *vcpu, u16 cpu_addr)
151104a7 332{
151104a7 333 struct kvm_s390_local_interrupt *li;
ea1918dd 334 int rc = SIGP_CC_ORDER_CODE_ACCEPTED;
1ee0bc55 335 struct kvm_vcpu *dst_vcpu = NULL;
151104a7
JF
336
337 if (cpu_addr >= KVM_MAX_VCPUS)
ea1918dd 338 return SIGP_CC_NOT_OPERATIONAL;
151104a7 339
1ee0bc55
JF
340 dst_vcpu = kvm_get_vcpu(vcpu->kvm, cpu_addr);
341 if (!dst_vcpu)
342 return SIGP_CC_NOT_OPERATIONAL;
343 li = &dst_vcpu->arch.local_int;
4ae3c081 344 spin_lock(&li->lock);
151104a7 345 if (li->action_bits & ACTION_STOP_ON_STOP)
ea1918dd 346 rc = SIGP_CC_BUSY;
4ae3c081 347 spin_unlock(&li->lock);
1ee0bc55 348
151104a7
JF
349 return rc;
350}
351
3526a66b
DH
352static int handle_sigp_dst(struct kvm_vcpu *vcpu, u8 order_code,
353 u16 cpu_addr, u32 parameter, u64 *status_reg)
5288fbf0 354{
5288fbf0
CB
355 int rc;
356
5288fbf0
CB
357 switch (order_code) {
358 case SIGP_SENSE:
359 vcpu->stat.instruction_sigp_sense++;
3526a66b 360 rc = __sigp_sense(vcpu, cpu_addr, status_reg);
5288fbf0 361 break;
7697e71f
CE
362 case SIGP_EXTERNAL_CALL:
363 vcpu->stat.instruction_sigp_external_call++;
364 rc = __sigp_external_call(vcpu, cpu_addr);
365 break;
a9ae32c3 366 case SIGP_EMERGENCY_SIGNAL:
5288fbf0
CB
367 vcpu->stat.instruction_sigp_emergency++;
368 rc = __sigp_emergency(vcpu, cpu_addr);
369 break;
370 case SIGP_STOP:
371 vcpu->stat.instruction_sigp_stop++;
9ace903d 372 rc = __sigp_stop(vcpu, cpu_addr, ACTION_STOP_ON_STOP);
5288fbf0 373 break;
a9ae32c3 374 case SIGP_STOP_AND_STORE_STATUS:
5288fbf0 375 vcpu->stat.instruction_sigp_stop++;
9ec2d6dc
JF
376 rc = __sigp_stop(vcpu, cpu_addr, ACTION_STORE_ON_STOP |
377 ACTION_STOP_ON_STOP);
5288fbf0 378 break;
00e9e435
TH
379 case SIGP_STORE_STATUS_AT_ADDRESS:
380 rc = __sigp_store_status_at_addr(vcpu, cpu_addr, parameter,
3526a66b 381 status_reg);
5288fbf0
CB
382 break;
383 case SIGP_SET_PREFIX:
384 vcpu->stat.instruction_sigp_prefix++;
3526a66b 385 rc = __sigp_set_prefix(vcpu, cpu_addr, parameter, status_reg);
5288fbf0 386 break;
b13d3580
TH
387 case SIGP_COND_EMERGENCY_SIGNAL:
388 rc = __sigp_conditional_emergency(vcpu, cpu_addr, parameter,
3526a66b 389 status_reg);
b13d3580 390 break;
bd59d3a4
CH
391 case SIGP_SENSE_RUNNING:
392 vcpu->stat.instruction_sigp_sense_running++;
3526a66b 393 rc = __sigp_sense_running(vcpu, cpu_addr, status_reg);
bd59d3a4 394 break;
58bc33b2
TH
395 case SIGP_START:
396 rc = sigp_check_callable(vcpu, cpu_addr);
397 if (rc == SIGP_CC_ORDER_CODE_ACCEPTED)
398 rc = -EOPNOTSUPP; /* Handle START in user space */
399 break;
5288fbf0
CB
400 case SIGP_RESTART:
401 vcpu->stat.instruction_sigp_restart++;
cc92d6de
TH
402 rc = sigp_check_callable(vcpu, cpu_addr);
403 if (rc == SIGP_CC_ORDER_CODE_ACCEPTED) {
404 VCPU_EVENT(vcpu, 4,
405 "sigp restart %x to handle userspace",
406 cpu_addr);
407 /* user space must know about restart */
408 rc = -EOPNOTSUPP;
409 }
410 break;
5288fbf0 411 default:
3526a66b
DH
412 rc = -EOPNOTSUPP;
413 }
414
415 return rc;
416}
417
418int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu)
419{
420 int r1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
421 int r3 = vcpu->arch.sie_block->ipa & 0x000f;
422 u32 parameter;
423 u16 cpu_addr = vcpu->run->s.regs.gprs[r3];
424 u8 order_code;
425 int rc;
426
427 /* sigp in userspace can exit */
428 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
429 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
430
431 order_code = kvm_s390_get_base_disp_rs(vcpu);
432
433 if (r1 % 2)
434 parameter = vcpu->run->s.regs.gprs[r1];
435 else
436 parameter = vcpu->run->s.regs.gprs[r1 + 1];
437
438 trace_kvm_s390_handle_sigp(vcpu, order_code, cpu_addr, parameter);
439 switch (order_code) {
440 case SIGP_SET_ARCHITECTURE:
441 vcpu->stat.instruction_sigp_arch++;
442 rc = __sigp_set_arch(vcpu, parameter);
443 break;
444 default:
445 rc = handle_sigp_dst(vcpu, order_code, cpu_addr,
446 parameter,
447 &vcpu->run->s.regs.gprs[r1]);
5288fbf0
CB
448 }
449
450 if (rc < 0)
451 return rc;
452
949c007a 453 kvm_s390_set_psw_cc(vcpu, rc);
5288fbf0
CB
454 return 0;
455}
4953919f
DH
456
457/*
458 * Handle SIGP partial execution interception.
459 *
460 * This interception will occur at the source cpu when a source cpu sends an
461 * external call to a target cpu and the target cpu has the WAIT bit set in
462 * its cpuflags. Interception will occurr after the interrupt indicator bits at
463 * the target cpu have been set. All error cases will lead to instruction
464 * interception, therefore nothing is to be checked or prepared.
465 */
466int kvm_s390_handle_sigp_pei(struct kvm_vcpu *vcpu)
467{
468 int r3 = vcpu->arch.sie_block->ipa & 0x000f;
469 u16 cpu_addr = vcpu->run->s.regs.gprs[r3];
470 struct kvm_vcpu *dest_vcpu;
471 u8 order_code = kvm_s390_get_base_disp_rs(vcpu);
472
473 trace_kvm_s390_handle_sigp_pei(vcpu, order_code, cpu_addr);
474
475 if (order_code == SIGP_EXTERNAL_CALL) {
476 dest_vcpu = kvm_get_vcpu(vcpu->kvm, cpu_addr);
477 BUG_ON(dest_vcpu == NULL);
478
0e9c85a5 479 kvm_s390_vcpu_wakeup(dest_vcpu);
4953919f
DH
480 kvm_s390_set_psw_cc(vcpu, SIGP_CC_ORDER_CODE_ACCEPTED);
481 return 0;
482 }
483
484 return -EOPNOTSUPP;
485}
This page took 0.377804 seconds and 5 git commands to generate.