blob: fda1d64f15eec14d81ac4d26e5e0ff4f229480c0 [file] [log] [blame]
Christian Borntraeger5288fbf2008-03-25 18:47:31 +01001/*
2 * sigp.c - handlinge interprocessor communication
3 *
Christian Ehrhardt9ace9032009-05-20 15:34:55 +02004 * Copyright IBM Corp. 2008,2009
Christian Borntraeger5288fbf2008-03-25 18:47:31 +01005 *
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>
Christian Ehrhardt9ace9032009-05-20 15:34:55 +020012 * Christian Ehrhardt <ehrhardt@de.ibm.com>
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010013 */
14
15#include <linux/kvm.h>
16#include <linux/kvm_host.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Heiko Carstensa9ae32c2012-06-04 12:55:15 +020018#include <asm/sigp.h>
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010019#include "gaccess.h"
20#include "kvm-s390.h"
21
Martin Schwidefsky00963692008-07-25 15:51:00 +020022static int __sigp_sense(struct kvm_vcpu *vcpu, u16 cpu_addr,
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +010023 u64 *reg)
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010024{
Christian Borntraeger180c12f2008-06-27 15:05:40 +020025 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010026 int rc;
27
28 if (cpu_addr >= KVM_MAX_VCPUS)
29 return 3; /* not operational */
30
Christian Borntraegerb037a4f2009-05-12 17:21:50 +020031 spin_lock(&fi->lock);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010032 if (fi->local_int[cpu_addr] == NULL)
33 rc = 3; /* not operational */
Cornelia Huck9e6dabe2011-11-17 11:00:41 +010034 else if (!(atomic_read(fi->local_int[cpu_addr]->cpuflags)
35 & CPUSTAT_STOPPED)) {
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010036 *reg &= 0xffffffff00000000UL;
37 rc = 1; /* status stored */
38 } else {
39 *reg &= 0xffffffff00000000UL;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +020040 *reg |= SIGP_STATUS_STOPPED;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010041 rc = 1; /* status stored */
42 }
Christian Borntraegerb037a4f2009-05-12 17:21:50 +020043 spin_unlock(&fi->lock);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010044
45 VCPU_EVENT(vcpu, 4, "sensed status of cpu %x rc %x", cpu_addr, rc);
46 return rc;
47}
48
49static int __sigp_emergency(struct kvm_vcpu *vcpu, u16 cpu_addr)
50{
Christian Borntraeger180c12f2008-06-27 15:05:40 +020051 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
52 struct kvm_s390_local_interrupt *li;
53 struct kvm_s390_interrupt_info *inti;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010054 int rc;
55
56 if (cpu_addr >= KVM_MAX_VCPUS)
57 return 3; /* not operational */
58
59 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
60 if (!inti)
61 return -ENOMEM;
62
63 inti->type = KVM_S390_INT_EMERGENCY;
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020064 inti->emerg.code = vcpu->vcpu_id;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010065
Christian Borntraegerb037a4f2009-05-12 17:21:50 +020066 spin_lock(&fi->lock);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010067 li = fi->local_int[cpu_addr];
68 if (li == NULL) {
69 rc = 3; /* not operational */
70 kfree(inti);
71 goto unlock;
72 }
73 spin_lock_bh(&li->lock);
74 list_add_tail(&inti->list, &li->list);
75 atomic_set(&li->active, 1);
76 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
77 if (waitqueue_active(&li->wq))
78 wake_up_interruptible(&li->wq);
79 spin_unlock_bh(&li->lock);
80 rc = 0; /* order accepted */
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020081 VCPU_EVENT(vcpu, 4, "sent sigp emerg to cpu %x", cpu_addr);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010082unlock:
Christian Borntraegerb037a4f2009-05-12 17:21:50 +020083 spin_unlock(&fi->lock);
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020084 return rc;
85}
86
87static int __sigp_external_call(struct kvm_vcpu *vcpu, u16 cpu_addr)
88{
89 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
90 struct kvm_s390_local_interrupt *li;
91 struct kvm_s390_interrupt_info *inti;
92 int rc;
93
94 if (cpu_addr >= KVM_MAX_VCPUS)
95 return 3; /* not operational */
96
97 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
98 if (!inti)
99 return -ENOMEM;
100
101 inti->type = KVM_S390_INT_EXTERNAL_CALL;
102 inti->extcall.code = vcpu->vcpu_id;
103
104 spin_lock(&fi->lock);
105 li = fi->local_int[cpu_addr];
106 if (li == NULL) {
107 rc = 3; /* not operational */
108 kfree(inti);
109 goto unlock;
110 }
111 spin_lock_bh(&li->lock);
112 list_add_tail(&inti->list, &li->list);
113 atomic_set(&li->active, 1);
114 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
115 if (waitqueue_active(&li->wq))
116 wake_up_interruptible(&li->wq);
117 spin_unlock_bh(&li->lock);
118 rc = 0; /* order accepted */
119 VCPU_EVENT(vcpu, 4, "sent sigp ext call to cpu %x", cpu_addr);
120unlock:
121 spin_unlock(&fi->lock);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100122 return rc;
123}
124
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200125static int __inject_sigp_stop(struct kvm_s390_local_interrupt *li, int action)
126{
127 struct kvm_s390_interrupt_info *inti;
128
Julia Lawall9940fa82010-06-08 18:58:11 +0200129 inti = kzalloc(sizeof(*inti), GFP_ATOMIC);
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200130 if (!inti)
131 return -ENOMEM;
132 inti->type = KVM_S390_SIGP_STOP;
133
134 spin_lock_bh(&li->lock);
Jens Freimann24a13042012-02-06 10:59:05 +0100135 if ((atomic_read(li->cpuflags) & CPUSTAT_STOPPED))
136 goto out;
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200137 list_add_tail(&inti->list, &li->list);
138 atomic_set(&li->active, 1);
139 atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags);
140 li->action_bits |= action;
141 if (waitqueue_active(&li->wq))
142 wake_up_interruptible(&li->wq);
Jens Freimann24a13042012-02-06 10:59:05 +0100143out:
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200144 spin_unlock_bh(&li->lock);
145
146 return 0; /* order accepted */
147}
148
149static int __sigp_stop(struct kvm_vcpu *vcpu, u16 cpu_addr, int action)
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100150{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200151 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
152 struct kvm_s390_local_interrupt *li;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100153 int rc;
154
155 if (cpu_addr >= KVM_MAX_VCPUS)
156 return 3; /* not operational */
157
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200158 spin_lock(&fi->lock);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100159 li = fi->local_int[cpu_addr];
160 if (li == NULL) {
161 rc = 3; /* not operational */
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100162 goto unlock;
163 }
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200164
165 rc = __inject_sigp_stop(li, action);
166
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100167unlock:
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200168 spin_unlock(&fi->lock);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100169 VCPU_EVENT(vcpu, 4, "sent sigp stop to cpu %x", cpu_addr);
170 return rc;
171}
172
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200173int kvm_s390_inject_sigp_stop(struct kvm_vcpu *vcpu, int action)
174{
175 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
176 return __inject_sigp_stop(li, action);
177}
178
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100179static int __sigp_set_arch(struct kvm_vcpu *vcpu, u32 parameter)
180{
181 int rc;
182
183 switch (parameter & 0xff) {
184 case 0:
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100185 rc = 3; /* not operational */
186 break;
187 case 1:
188 case 2:
189 rc = 0; /* order accepted */
190 break;
191 default:
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100192 rc = -EOPNOTSUPP;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100193 }
194 return rc;
195}
196
197static int __sigp_set_prefix(struct kvm_vcpu *vcpu, u16 cpu_addr, u32 address,
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100198 u64 *reg)
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100199{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200200 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
Roel Kluin53cb7802009-08-07 10:39:25 +0200201 struct kvm_s390_local_interrupt *li = NULL;
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200202 struct kvm_s390_interrupt_info *inti;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100203 int rc;
204 u8 tmp;
205
206 /* make sure that the new value is valid memory */
207 address = address & 0x7fffe000u;
Carsten Otte092670c2011-07-24 10:48:22 +0200208 if (copy_from_guest_absolute(vcpu, &tmp, address, 1) ||
209 copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1)) {
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200210 *reg |= SIGP_STATUS_INVALID_PARAMETER;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100211 return 1; /* invalid parameter */
212 }
213
214 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
215 if (!inti)
216 return 2; /* busy */
217
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200218 spin_lock(&fi->lock);
Roel Kluin53cb7802009-08-07 10:39:25 +0200219 if (cpu_addr < KVM_MAX_VCPUS)
220 li = fi->local_int[cpu_addr];
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100221
Roel Kluin53cb7802009-08-07 10:39:25 +0200222 if (li == NULL) {
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100223 rc = 1; /* incorrect state */
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200224 *reg &= SIGP_STATUS_INCORRECT_STATE;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100225 kfree(inti);
226 goto out_fi;
227 }
228
229 spin_lock_bh(&li->lock);
230 /* cpu must be in stopped state */
Cornelia Huck9e6dabe2011-11-17 11:00:41 +0100231 if (!(atomic_read(li->cpuflags) & CPUSTAT_STOPPED)) {
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100232 rc = 1; /* incorrect state */
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200233 *reg &= SIGP_STATUS_INCORRECT_STATE;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100234 kfree(inti);
235 goto out_li;
236 }
237
238 inti->type = KVM_S390_SIGP_SET_PREFIX;
239 inti->prefix.address = address;
240
241 list_add_tail(&inti->list, &li->list);
242 atomic_set(&li->active, 1);
243 if (waitqueue_active(&li->wq))
244 wake_up_interruptible(&li->wq);
245 rc = 0; /* order accepted */
246
247 VCPU_EVENT(vcpu, 4, "set prefix of cpu %02x to %x", cpu_addr, address);
248out_li:
249 spin_unlock_bh(&li->lock);
250out_fi:
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200251 spin_unlock(&fi->lock);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100252 return rc;
253}
254
Cornelia Huckbd59d3a2011-11-17 11:00:42 +0100255static int __sigp_sense_running(struct kvm_vcpu *vcpu, u16 cpu_addr,
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100256 u64 *reg)
Cornelia Huckbd59d3a2011-11-17 11:00:42 +0100257{
258 int rc;
259 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
260
261 if (cpu_addr >= KVM_MAX_VCPUS)
262 return 3; /* not operational */
263
264 spin_lock(&fi->lock);
265 if (fi->local_int[cpu_addr] == NULL)
266 rc = 3; /* not operational */
267 else {
268 if (atomic_read(fi->local_int[cpu_addr]->cpuflags)
269 & CPUSTAT_RUNNING) {
270 /* running */
271 rc = 1;
272 } else {
273 /* not running */
274 *reg &= 0xffffffff00000000UL;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200275 *reg |= SIGP_STATUS_NOT_RUNNING;
Cornelia Huckbd59d3a2011-11-17 11:00:42 +0100276 rc = 0;
277 }
278 }
279 spin_unlock(&fi->lock);
280
281 VCPU_EVENT(vcpu, 4, "sensed running status of cpu %x rc %x", cpu_addr,
282 rc);
283
284 return rc;
285}
286
Jens Freimann151104a2012-02-08 08:28:29 +0100287static int __sigp_restart(struct kvm_vcpu *vcpu, u16 cpu_addr)
288{
289 int rc = 0;
290 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
291 struct kvm_s390_local_interrupt *li;
292
293 if (cpu_addr >= KVM_MAX_VCPUS)
294 return 3; /* not operational */
295
296 spin_lock(&fi->lock);
297 li = fi->local_int[cpu_addr];
298 if (li == NULL) {
299 rc = 3; /* not operational */
300 goto out;
301 }
302
303 spin_lock_bh(&li->lock);
304 if (li->action_bits & ACTION_STOP_ON_STOP)
305 rc = 2; /* busy */
306 else
307 VCPU_EVENT(vcpu, 4, "sigp restart %x to handle userspace",
308 cpu_addr);
309 spin_unlock_bh(&li->lock);
310out:
311 spin_unlock(&fi->lock);
312 return rc;
313}
314
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100315int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu)
316{
317 int r1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
318 int r3 = vcpu->arch.sie_block->ipa & 0x000f;
319 int base2 = vcpu->arch.sie_block->ipb >> 28;
320 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
321 u32 parameter;
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100322 u16 cpu_addr = vcpu->run->s.regs.gprs[r3];
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100323 u8 order_code;
324 int rc;
325
Christian Borntraeger3eb77d52008-11-17 14:50:35 +0100326 /* sigp in userspace can exit */
327 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
328 return kvm_s390_inject_program_int(vcpu,
329 PGM_PRIVILEGED_OPERATION);
330
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100331 order_code = disp2;
332 if (base2)
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100333 order_code += vcpu->run->s.regs.gprs[base2];
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100334
335 if (r1 % 2)
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100336 parameter = vcpu->run->s.regs.gprs[r1];
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100337 else
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100338 parameter = vcpu->run->s.regs.gprs[r1 + 1];
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100339
340 switch (order_code) {
341 case SIGP_SENSE:
342 vcpu->stat.instruction_sigp_sense++;
343 rc = __sigp_sense(vcpu, cpu_addr,
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100344 &vcpu->run->s.regs.gprs[r1]);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100345 break;
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200346 case SIGP_EXTERNAL_CALL:
347 vcpu->stat.instruction_sigp_external_call++;
348 rc = __sigp_external_call(vcpu, cpu_addr);
349 break;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200350 case SIGP_EMERGENCY_SIGNAL:
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100351 vcpu->stat.instruction_sigp_emergency++;
352 rc = __sigp_emergency(vcpu, cpu_addr);
353 break;
354 case SIGP_STOP:
355 vcpu->stat.instruction_sigp_stop++;
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200356 rc = __sigp_stop(vcpu, cpu_addr, ACTION_STOP_ON_STOP);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100357 break;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200358 case SIGP_STOP_AND_STORE_STATUS:
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100359 vcpu->stat.instruction_sigp_stop++;
Jens Freimann9ec2d6d2012-02-06 10:59:06 +0100360 rc = __sigp_stop(vcpu, cpu_addr, ACTION_STORE_ON_STOP |
361 ACTION_STOP_ON_STOP);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100362 break;
Heiko Carstensa9ae32c2012-06-04 12:55:15 +0200363 case SIGP_SET_ARCHITECTURE:
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100364 vcpu->stat.instruction_sigp_arch++;
365 rc = __sigp_set_arch(vcpu, parameter);
366 break;
367 case SIGP_SET_PREFIX:
368 vcpu->stat.instruction_sigp_prefix++;
369 rc = __sigp_set_prefix(vcpu, cpu_addr, parameter,
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100370 &vcpu->run->s.regs.gprs[r1]);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100371 break;
Cornelia Huckbd59d3a2011-11-17 11:00:42 +0100372 case SIGP_SENSE_RUNNING:
373 vcpu->stat.instruction_sigp_sense_running++;
374 rc = __sigp_sense_running(vcpu, cpu_addr,
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +0100375 &vcpu->run->s.regs.gprs[r1]);
Cornelia Huckbd59d3a2011-11-17 11:00:42 +0100376 break;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100377 case SIGP_RESTART:
378 vcpu->stat.instruction_sigp_restart++;
Jens Freimann151104a2012-02-08 08:28:29 +0100379 rc = __sigp_restart(vcpu, cpu_addr);
380 if (rc == 2) /* busy */
381 break;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100382 /* user space must know about restart */
383 default:
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100384 return -EOPNOTSUPP;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100385 }
386
387 if (rc < 0)
388 return rc;
389
390 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
391 vcpu->arch.sie_block->gpsw.mask |= (rc & 3ul) << 44;
392 return 0;
393}