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