KVM: s390: remove _bh locking from start_stop_lock
[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
9ace903d 128static int __inject_sigp_stop(struct kvm_s390_local_interrupt *li, int action)
5288fbf0 129{
180c12fb 130 struct kvm_s390_interrupt_info *inti;
e879892c 131 int rc = SIGP_CC_ORDER_CODE_ACCEPTED;
5288fbf0 132
9940fa80 133 inti = kzalloc(sizeof(*inti), GFP_ATOMIC);
5288fbf0
CB
134 if (!inti)
135 return -ENOMEM;
5288fbf0
CB
136 inti->type = KVM_S390_SIGP_STOP;
137
4ae3c081 138 spin_lock(&li->lock);
7dfc63cf
DH
139 if (li->action_bits & ACTION_STOP_ON_STOP) {
140 /* another SIGP STOP is pending */
141 rc = SIGP_CC_BUSY;
142 goto out;
143 }
a046b816
CD
144 if ((atomic_read(li->cpuflags) & CPUSTAT_STOPPED)) {
145 kfree(inti);
e879892c
TH
146 if ((action & ACTION_STORE_ON_STOP) != 0)
147 rc = -ESHUTDOWN;
24a13044 148 goto out;
a046b816 149 }
5288fbf0
CB
150 list_add_tail(&inti->list, &li->list);
151 atomic_set(&li->active, 1);
9ace903d 152 li->action_bits |= action;
7dfc63cf 153 atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags);
d0321a24
CB
154 if (waitqueue_active(li->wq))
155 wake_up_interruptible(li->wq);
24a13044 156out:
4ae3c081 157 spin_unlock(&li->lock);
9ace903d 158
e879892c 159 return rc;
9ace903d
CE
160}
161
162static int __sigp_stop(struct kvm_vcpu *vcpu, u16 cpu_addr, int action)
163{
9ace903d 164 struct kvm_s390_local_interrupt *li;
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;
174 li = &dst_vcpu->arch.local_int;
9ace903d
CE
175
176 rc = __inject_sigp_stop(li, action);
177
5288fbf0 178 VCPU_EVENT(vcpu, 4, "sent sigp stop to cpu %x", cpu_addr);
e879892c
TH
179
180 if ((action & ACTION_STORE_ON_STOP) != 0 && rc == -ESHUTDOWN) {
181 /* If the CPU has already been stopped, we still have
182 * to save the status when doing stop-and-store. This
183 * has to be done after unlocking all spinlocks. */
e879892c
TH
184 rc = kvm_s390_store_status_unloaded(dst_vcpu,
185 KVM_S390_STORE_STATUS_NOADDR);
186 }
187
5288fbf0
CB
188 return rc;
189}
190
191static int __sigp_set_arch(struct kvm_vcpu *vcpu, u32 parameter)
192{
193 int rc;
3c038e6b
DD
194 unsigned int i;
195 struct kvm_vcpu *v;
5288fbf0
CB
196
197 switch (parameter & 0xff) {
198 case 0:
ea1918dd 199 rc = SIGP_CC_NOT_OPERATIONAL;
5288fbf0
CB
200 break;
201 case 1:
202 case 2:
3c038e6b
DD
203 kvm_for_each_vcpu(i, v, vcpu->kvm) {
204 v->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
205 kvm_clear_async_pf_completion_queue(v);
206 }
207
ea1918dd 208 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
5288fbf0
CB
209 break;
210 default:
b8e660b8 211 rc = -EOPNOTSUPP;
5288fbf0
CB
212 }
213 return rc;
214}
215
216static int __sigp_set_prefix(struct kvm_vcpu *vcpu, u16 cpu_addr, u32 address,
5a32c1af 217 u64 *reg)
5288fbf0 218{
13b191ae
TH
219 struct kvm_s390_local_interrupt *li;
220 struct kvm_vcpu *dst_vcpu = NULL;
180c12fb 221 struct kvm_s390_interrupt_info *inti;
5288fbf0 222 int rc;
5288fbf0 223
13b191ae
TH
224 if (cpu_addr < KVM_MAX_VCPUS)
225 dst_vcpu = kvm_get_vcpu(vcpu->kvm, cpu_addr);
226 if (!dst_vcpu)
227 return SIGP_CC_NOT_OPERATIONAL;
228 li = &dst_vcpu->arch.local_int;
229
665170cb
HC
230 /*
231 * Make sure the new value is valid memory. We only need to check the
232 * first page, since address is 8k aligned and memory pieces are always
233 * at least 1MB aligned and have at least a size of 1MB.
234 */
235 address &= 0x7fffe000u;
236 if (kvm_is_error_gpa(vcpu->kvm, address)) {
0744426e 237 *reg &= 0xffffffff00000000UL;
a9ae32c3 238 *reg |= SIGP_STATUS_INVALID_PARAMETER;
ea1918dd 239 return SIGP_CC_STATUS_STORED;
5288fbf0
CB
240 }
241
242 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
243 if (!inti)
ea1918dd 244 return SIGP_CC_BUSY;
5288fbf0 245
4ae3c081 246 spin_lock(&li->lock);
5288fbf0 247 /* cpu must be in stopped state */
9e6dabef 248 if (!(atomic_read(li->cpuflags) & CPUSTAT_STOPPED)) {
0744426e
HC
249 *reg &= 0xffffffff00000000UL;
250 *reg |= SIGP_STATUS_INCORRECT_STATE;
ea1918dd 251 rc = SIGP_CC_STATUS_STORED;
5288fbf0
CB
252 kfree(inti);
253 goto out_li;
254 }
255
256 inti->type = KVM_S390_SIGP_SET_PREFIX;
257 inti->prefix.address = address;
258
259 list_add_tail(&inti->list, &li->list);
260 atomic_set(&li->active, 1);
d0321a24
CB
261 if (waitqueue_active(li->wq))
262 wake_up_interruptible(li->wq);
ea1918dd 263 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
5288fbf0
CB
264
265 VCPU_EVENT(vcpu, 4, "set prefix of cpu %02x to %x", cpu_addr, address);
266out_li:
4ae3c081 267 spin_unlock(&li->lock);
5288fbf0
CB
268 return rc;
269}
270
00e9e435
TH
271static int __sigp_store_status_at_addr(struct kvm_vcpu *vcpu, u16 cpu_id,
272 u32 addr, u64 *reg)
273{
274 struct kvm_vcpu *dst_vcpu = NULL;
275 int flags;
276 int rc;
277
278 if (cpu_id < KVM_MAX_VCPUS)
279 dst_vcpu = kvm_get_vcpu(vcpu->kvm, cpu_id);
280 if (!dst_vcpu)
281 return SIGP_CC_NOT_OPERATIONAL;
282
4ae3c081 283 spin_lock(&dst_vcpu->arch.local_int.lock);
00e9e435 284 flags = atomic_read(dst_vcpu->arch.local_int.cpuflags);
4ae3c081 285 spin_unlock(&dst_vcpu->arch.local_int.lock);
00e9e435
TH
286 if (!(flags & CPUSTAT_STOPPED)) {
287 *reg &= 0xffffffff00000000UL;
288 *reg |= SIGP_STATUS_INCORRECT_STATE;
289 return SIGP_CC_STATUS_STORED;
290 }
291
292 addr &= 0x7ffffe00;
293 rc = kvm_s390_store_status_unloaded(dst_vcpu, addr);
294 if (rc == -EFAULT) {
295 *reg &= 0xffffffff00000000UL;
296 *reg |= SIGP_STATUS_INVALID_PARAMETER;
297 rc = SIGP_CC_STATUS_STORED;
298 }
299 return rc;
300}
301
bd59d3a4 302static int __sigp_sense_running(struct kvm_vcpu *vcpu, u16 cpu_addr,
5a32c1af 303 u64 *reg)
bd59d3a4 304{
1ee0bc55
JF
305 struct kvm_s390_local_interrupt *li;
306 struct kvm_vcpu *dst_vcpu = NULL;
bd59d3a4 307 int rc;
bd59d3a4
CH
308
309 if (cpu_addr >= KVM_MAX_VCPUS)
ea1918dd 310 return SIGP_CC_NOT_OPERATIONAL;
bd59d3a4 311
1ee0bc55
JF
312 dst_vcpu = kvm_get_vcpu(vcpu->kvm, cpu_addr);
313 if (!dst_vcpu)
314 return SIGP_CC_NOT_OPERATIONAL;
315 li = &dst_vcpu->arch.local_int;
316 if (atomic_read(li->cpuflags) & CPUSTAT_RUNNING) {
317 /* running */
318 rc = SIGP_CC_ORDER_CODE_ACCEPTED;
319 } else {
320 /* not running */
321 *reg &= 0xffffffff00000000UL;
322 *reg |= SIGP_STATUS_NOT_RUNNING;
323 rc = SIGP_CC_STATUS_STORED;
bd59d3a4 324 }
bd59d3a4
CH
325
326 VCPU_EVENT(vcpu, 4, "sensed running status of cpu %x rc %x", cpu_addr,
327 rc);
328
329 return rc;
330}
331
cc92d6de
TH
332/* Test whether the destination CPU is available and not busy */
333static int sigp_check_callable(struct kvm_vcpu *vcpu, u16 cpu_addr)
151104a7 334{
151104a7 335 struct kvm_s390_local_interrupt *li;
ea1918dd 336 int rc = SIGP_CC_ORDER_CODE_ACCEPTED;
1ee0bc55 337 struct kvm_vcpu *dst_vcpu = NULL;
151104a7
JF
338
339 if (cpu_addr >= KVM_MAX_VCPUS)
ea1918dd 340 return SIGP_CC_NOT_OPERATIONAL;
151104a7 341
1ee0bc55
JF
342 dst_vcpu = kvm_get_vcpu(vcpu->kvm, cpu_addr);
343 if (!dst_vcpu)
344 return SIGP_CC_NOT_OPERATIONAL;
345 li = &dst_vcpu->arch.local_int;
4ae3c081 346 spin_lock(&li->lock);
151104a7 347 if (li->action_bits & ACTION_STOP_ON_STOP)
ea1918dd 348 rc = SIGP_CC_BUSY;
4ae3c081 349 spin_unlock(&li->lock);
1ee0bc55 350
151104a7
JF
351 return rc;
352}
353
5288fbf0
CB
354int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu)
355{
356 int r1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
357 int r3 = vcpu->arch.sie_block->ipa & 0x000f;
5288fbf0 358 u32 parameter;
5a32c1af 359 u16 cpu_addr = vcpu->run->s.regs.gprs[r3];
5288fbf0
CB
360 u8 order_code;
361 int rc;
362
3eb77d51
CB
363 /* sigp in userspace can exit */
364 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
208dd756 365 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
3eb77d51 366
b1c571a5 367 order_code = kvm_s390_get_base_disp_rs(vcpu);
5288fbf0
CB
368
369 if (r1 % 2)
5a32c1af 370 parameter = vcpu->run->s.regs.gprs[r1];
5288fbf0 371 else
5a32c1af 372 parameter = vcpu->run->s.regs.gprs[r1 + 1];
5288fbf0 373
5786fffa 374 trace_kvm_s390_handle_sigp(vcpu, order_code, cpu_addr, parameter);
5288fbf0
CB
375 switch (order_code) {
376 case SIGP_SENSE:
377 vcpu->stat.instruction_sigp_sense++;
378 rc = __sigp_sense(vcpu, cpu_addr,
5a32c1af 379 &vcpu->run->s.regs.gprs[r1]);
5288fbf0 380 break;
7697e71f
CE
381 case SIGP_EXTERNAL_CALL:
382 vcpu->stat.instruction_sigp_external_call++;
383 rc = __sigp_external_call(vcpu, cpu_addr);
384 break;
a9ae32c3 385 case SIGP_EMERGENCY_SIGNAL:
5288fbf0
CB
386 vcpu->stat.instruction_sigp_emergency++;
387 rc = __sigp_emergency(vcpu, cpu_addr);
388 break;
389 case SIGP_STOP:
390 vcpu->stat.instruction_sigp_stop++;
9ace903d 391 rc = __sigp_stop(vcpu, cpu_addr, ACTION_STOP_ON_STOP);
5288fbf0 392 break;
a9ae32c3 393 case SIGP_STOP_AND_STORE_STATUS:
5288fbf0 394 vcpu->stat.instruction_sigp_stop++;
9ec2d6dc
JF
395 rc = __sigp_stop(vcpu, cpu_addr, ACTION_STORE_ON_STOP |
396 ACTION_STOP_ON_STOP);
5288fbf0 397 break;
00e9e435
TH
398 case SIGP_STORE_STATUS_AT_ADDRESS:
399 rc = __sigp_store_status_at_addr(vcpu, cpu_addr, parameter,
400 &vcpu->run->s.regs.gprs[r1]);
401 break;
a9ae32c3 402 case SIGP_SET_ARCHITECTURE:
5288fbf0
CB
403 vcpu->stat.instruction_sigp_arch++;
404 rc = __sigp_set_arch(vcpu, parameter);
405 break;
406 case SIGP_SET_PREFIX:
407 vcpu->stat.instruction_sigp_prefix++;
408 rc = __sigp_set_prefix(vcpu, cpu_addr, parameter,
5a32c1af 409 &vcpu->run->s.regs.gprs[r1]);
5288fbf0 410 break;
b13d3580
TH
411 case SIGP_COND_EMERGENCY_SIGNAL:
412 rc = __sigp_conditional_emergency(vcpu, cpu_addr, parameter,
413 &vcpu->run->s.regs.gprs[r1]);
414 break;
bd59d3a4
CH
415 case SIGP_SENSE_RUNNING:
416 vcpu->stat.instruction_sigp_sense_running++;
417 rc = __sigp_sense_running(vcpu, cpu_addr,
5a32c1af 418 &vcpu->run->s.regs.gprs[r1]);
bd59d3a4 419 break;
58bc33b2
TH
420 case SIGP_START:
421 rc = sigp_check_callable(vcpu, cpu_addr);
422 if (rc == SIGP_CC_ORDER_CODE_ACCEPTED)
423 rc = -EOPNOTSUPP; /* Handle START in user space */
424 break;
5288fbf0
CB
425 case SIGP_RESTART:
426 vcpu->stat.instruction_sigp_restart++;
cc92d6de
TH
427 rc = sigp_check_callable(vcpu, cpu_addr);
428 if (rc == SIGP_CC_ORDER_CODE_ACCEPTED) {
429 VCPU_EVENT(vcpu, 4,
430 "sigp restart %x to handle userspace",
431 cpu_addr);
432 /* user space must know about restart */
433 rc = -EOPNOTSUPP;
434 }
435 break;
5288fbf0 436 default:
b8e660b8 437 return -EOPNOTSUPP;
5288fbf0
CB
438 }
439
440 if (rc < 0)
441 return rc;
442
949c007a 443 kvm_s390_set_psw_cc(vcpu, rc);
5288fbf0
CB
444 return 0;
445}
4953919f
DH
446
447/*
448 * Handle SIGP partial execution interception.
449 *
450 * This interception will occur at the source cpu when a source cpu sends an
451 * external call to a target cpu and the target cpu has the WAIT bit set in
452 * its cpuflags. Interception will occurr after the interrupt indicator bits at
453 * the target cpu have been set. All error cases will lead to instruction
454 * interception, therefore nothing is to be checked or prepared.
455 */
456int kvm_s390_handle_sigp_pei(struct kvm_vcpu *vcpu)
457{
458 int r3 = vcpu->arch.sie_block->ipa & 0x000f;
459 u16 cpu_addr = vcpu->run->s.regs.gprs[r3];
460 struct kvm_vcpu *dest_vcpu;
461 u8 order_code = kvm_s390_get_base_disp_rs(vcpu);
462
463 trace_kvm_s390_handle_sigp_pei(vcpu, order_code, cpu_addr);
464
465 if (order_code == SIGP_EXTERNAL_CALL) {
466 dest_vcpu = kvm_get_vcpu(vcpu->kvm, cpu_addr);
467 BUG_ON(dest_vcpu == NULL);
468
4ae3c081 469 spin_lock(&dest_vcpu->arch.local_int.lock);
4953919f
DH
470 if (waitqueue_active(&dest_vcpu->wq))
471 wake_up_interruptible(&dest_vcpu->wq);
472 dest_vcpu->preempted = true;
4ae3c081 473 spin_unlock(&dest_vcpu->arch.local_int.lock);
4953919f
DH
474
475 kvm_s390_set_psw_cc(vcpu, SIGP_CC_ORDER_CODE_ACCEPTED);
476 return 0;
477 }
478
479 return -EOPNOTSUPP;
480}
This page took 0.359573 seconds and 5 git commands to generate.