KVM: s390: fix sigp set prefix status stored cases
[deliverable/linux.git] / arch / s390 / kvm / sigp.c
CommitLineData
5288fbf0
CB
1/*
2 * sigp.c - handlinge interprocessor communication
3 *
9ace903d 4 * Copyright IBM Corp. 2008,2009
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>
9b747530 18#include <asm/sigp.h>
5288fbf0
CB
19#include "gaccess.h"
20#include "kvm-s390.h"
21
0096369d 22static int __sigp_sense(struct kvm_vcpu *vcpu, u16 cpu_addr,
5a32c1af 23 u64 *reg)
5288fbf0 24{
180c12fb 25 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
5288fbf0
CB
26 int rc;
27
28 if (cpu_addr >= KVM_MAX_VCPUS)
29 return 3; /* not operational */
30
b037a4f3 31 spin_lock(&fi->lock);
5288fbf0
CB
32 if (fi->local_int[cpu_addr] == NULL)
33 rc = 3; /* not operational */
9e6dabef
CH
34 else if (!(atomic_read(fi->local_int[cpu_addr]->cpuflags)
35 & CPUSTAT_STOPPED)) {
5288fbf0
CB
36 *reg &= 0xffffffff00000000UL;
37 rc = 1; /* status stored */
38 } else {
39 *reg &= 0xffffffff00000000UL;
9b747530 40 *reg |= SIGP_STATUS_STOPPED;
5288fbf0
CB
41 rc = 1; /* status stored */
42 }
b037a4f3 43 spin_unlock(&fi->lock);
5288fbf0
CB
44
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{
180c12fb
CB
51 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;
5288fbf0
CB
54 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;
7697e71f 64 inti->emerg.code = vcpu->vcpu_id;
5288fbf0 65
b037a4f3 66 spin_lock(&fi->lock);
5288fbf0
CB
67 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 */
7697e71f
CE
81 VCPU_EVENT(vcpu, 4, "sent sigp emerg to cpu %x", cpu_addr);
82unlock:
83 spin_unlock(&fi->lock);
84 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);
5288fbf0 120unlock:
b037a4f3 121 spin_unlock(&fi->lock);
5288fbf0
CB
122 return rc;
123}
124
9ace903d 125static int __inject_sigp_stop(struct kvm_s390_local_interrupt *li, int action)
5288fbf0 126{
180c12fb 127 struct kvm_s390_interrupt_info *inti;
5288fbf0 128
9940fa80 129 inti = kzalloc(sizeof(*inti), GFP_ATOMIC);
5288fbf0
CB
130 if (!inti)
131 return -ENOMEM;
5288fbf0
CB
132 inti->type = KVM_S390_SIGP_STOP;
133
5288fbf0 134 spin_lock_bh(&li->lock);
24a13044
JF
135 if ((atomic_read(li->cpuflags) & CPUSTAT_STOPPED))
136 goto out;
5288fbf0
CB
137 list_add_tail(&inti->list, &li->list);
138 atomic_set(&li->active, 1);
139 atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags);
9ace903d 140 li->action_bits |= action;
5288fbf0
CB
141 if (waitqueue_active(&li->wq))
142 wake_up_interruptible(&li->wq);
24a13044 143out:
5288fbf0 144 spin_unlock_bh(&li->lock);
9ace903d
CE
145
146 return 0; /* order accepted */
147}
148
149static int __sigp_stop(struct kvm_vcpu *vcpu, u16 cpu_addr, int action)
150{
151 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
152 struct kvm_s390_local_interrupt *li;
153 int rc;
154
155 if (cpu_addr >= KVM_MAX_VCPUS)
156 return 3; /* not operational */
157
158 spin_lock(&fi->lock);
159 li = fi->local_int[cpu_addr];
160 if (li == NULL) {
161 rc = 3; /* not operational */
162 goto unlock;
163 }
164
165 rc = __inject_sigp_stop(li, action);
166
5288fbf0 167unlock:
b037a4f3 168 spin_unlock(&fi->lock);
5288fbf0
CB
169 VCPU_EVENT(vcpu, 4, "sent sigp stop to cpu %x", cpu_addr);
170 return rc;
171}
172
9ace903d
CE
173int 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
5288fbf0
CB
179static int __sigp_set_arch(struct kvm_vcpu *vcpu, u32 parameter)
180{
181 int rc;
182
183 switch (parameter & 0xff) {
184 case 0:
5288fbf0
CB
185 rc = 3; /* not operational */
186 break;
187 case 1:
188 case 2:
189 rc = 0; /* order accepted */
190 break;
191 default:
b8e660b8 192 rc = -EOPNOTSUPP;
5288fbf0
CB
193 }
194 return rc;
195}
196
197static int __sigp_set_prefix(struct kvm_vcpu *vcpu, u16 cpu_addr, u32 address,
5a32c1af 198 u64 *reg)
5288fbf0 199{
180c12fb 200 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
53cb780a 201 struct kvm_s390_local_interrupt *li = NULL;
180c12fb 202 struct kvm_s390_interrupt_info *inti;
5288fbf0
CB
203 int rc;
204 u8 tmp;
205
206 /* make sure that the new value is valid memory */
207 address = address & 0x7fffe000u;
092670cd
CO
208 if (copy_from_guest_absolute(vcpu, &tmp, address, 1) ||
209 copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1)) {
0744426e 210 *reg &= 0xffffffff00000000UL;
9b747530 211 *reg |= SIGP_STATUS_INVALID_PARAMETER;
5288fbf0
CB
212 return 1; /* invalid parameter */
213 }
214
215 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
216 if (!inti)
217 return 2; /* busy */
218
b037a4f3 219 spin_lock(&fi->lock);
53cb780a
RK
220 if (cpu_addr < KVM_MAX_VCPUS)
221 li = fi->local_int[cpu_addr];
5288fbf0 222
53cb780a 223 if (li == NULL) {
0744426e
HC
224 *reg &= 0xffffffff00000000UL;
225 *reg |= SIGP_STATUS_INCORRECT_STATE;
5288fbf0 226 rc = 1; /* incorrect state */
5288fbf0
CB
227 kfree(inti);
228 goto out_fi;
229 }
230
231 spin_lock_bh(&li->lock);
232 /* cpu must be in stopped state */
9e6dabef 233 if (!(atomic_read(li->cpuflags) & CPUSTAT_STOPPED)) {
0744426e
HC
234 *reg &= 0xffffffff00000000UL;
235 *reg |= SIGP_STATUS_INCORRECT_STATE;
5288fbf0 236 rc = 1; /* incorrect state */
5288fbf0
CB
237 kfree(inti);
238 goto out_li;
239 }
240
241 inti->type = KVM_S390_SIGP_SET_PREFIX;
242 inti->prefix.address = address;
243
244 list_add_tail(&inti->list, &li->list);
245 atomic_set(&li->active, 1);
246 if (waitqueue_active(&li->wq))
247 wake_up_interruptible(&li->wq);
248 rc = 0; /* order accepted */
249
250 VCPU_EVENT(vcpu, 4, "set prefix of cpu %02x to %x", cpu_addr, address);
251out_li:
252 spin_unlock_bh(&li->lock);
253out_fi:
b037a4f3 254 spin_unlock(&fi->lock);
5288fbf0
CB
255 return rc;
256}
257
bd59d3a4 258static int __sigp_sense_running(struct kvm_vcpu *vcpu, u16 cpu_addr,
5a32c1af 259 u64 *reg)
bd59d3a4
CH
260{
261 int rc;
262 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
263
264 if (cpu_addr >= KVM_MAX_VCPUS)
265 return 3; /* not operational */
266
267 spin_lock(&fi->lock);
268 if (fi->local_int[cpu_addr] == NULL)
269 rc = 3; /* not operational */
270 else {
271 if (atomic_read(fi->local_int[cpu_addr]->cpuflags)
272 & CPUSTAT_RUNNING) {
273 /* running */
7ba26c48 274 rc = 0;
bd59d3a4
CH
275 } else {
276 /* not running */
277 *reg &= 0xffffffff00000000UL;
9b747530 278 *reg |= SIGP_STATUS_NOT_RUNNING;
7ba26c48 279 rc = 1;
bd59d3a4
CH
280 }
281 }
282 spin_unlock(&fi->lock);
283
284 VCPU_EVENT(vcpu, 4, "sensed running status of cpu %x rc %x", cpu_addr,
285 rc);
286
287 return rc;
288}
289
151104a7
JF
290static int __sigp_restart(struct kvm_vcpu *vcpu, u16 cpu_addr)
291{
292 int rc = 0;
293 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
294 struct kvm_s390_local_interrupt *li;
295
296 if (cpu_addr >= KVM_MAX_VCPUS)
297 return 3; /* not operational */
298
299 spin_lock(&fi->lock);
300 li = fi->local_int[cpu_addr];
301 if (li == NULL) {
302 rc = 3; /* not operational */
303 goto out;
304 }
305
306 spin_lock_bh(&li->lock);
307 if (li->action_bits & ACTION_STOP_ON_STOP)
308 rc = 2; /* busy */
309 else
310 VCPU_EVENT(vcpu, 4, "sigp restart %x to handle userspace",
311 cpu_addr);
312 spin_unlock_bh(&li->lock);
313out:
314 spin_unlock(&fi->lock);
315 return rc;
316}
317
5288fbf0
CB
318int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu)
319{
320 int r1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
321 int r3 = vcpu->arch.sie_block->ipa & 0x000f;
322 int base2 = vcpu->arch.sie_block->ipb >> 28;
323 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
324 u32 parameter;
5a32c1af 325 u16 cpu_addr = vcpu->run->s.regs.gprs[r3];
5288fbf0
CB
326 u8 order_code;
327 int rc;
328
3eb77d51
CB
329 /* sigp in userspace can exit */
330 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
331 return kvm_s390_inject_program_int(vcpu,
332 PGM_PRIVILEGED_OPERATION);
333
5288fbf0
CB
334 order_code = disp2;
335 if (base2)
5a32c1af 336 order_code += vcpu->run->s.regs.gprs[base2];
5288fbf0
CB
337
338 if (r1 % 2)
5a32c1af 339 parameter = vcpu->run->s.regs.gprs[r1];
5288fbf0 340 else
5a32c1af 341 parameter = vcpu->run->s.regs.gprs[r1 + 1];
5288fbf0
CB
342
343 switch (order_code) {
344 case SIGP_SENSE:
345 vcpu->stat.instruction_sigp_sense++;
346 rc = __sigp_sense(vcpu, cpu_addr,
5a32c1af 347 &vcpu->run->s.regs.gprs[r1]);
5288fbf0 348 break;
7697e71f
CE
349 case SIGP_EXTERNAL_CALL:
350 vcpu->stat.instruction_sigp_external_call++;
351 rc = __sigp_external_call(vcpu, cpu_addr);
352 break;
9b747530 353 case SIGP_EMERGENCY_SIGNAL:
5288fbf0
CB
354 vcpu->stat.instruction_sigp_emergency++;
355 rc = __sigp_emergency(vcpu, cpu_addr);
356 break;
357 case SIGP_STOP:
358 vcpu->stat.instruction_sigp_stop++;
9ace903d 359 rc = __sigp_stop(vcpu, cpu_addr, ACTION_STOP_ON_STOP);
5288fbf0 360 break;
9b747530 361 case SIGP_STOP_AND_STORE_STATUS:
5288fbf0 362 vcpu->stat.instruction_sigp_stop++;
9ec2d6dc
JF
363 rc = __sigp_stop(vcpu, cpu_addr, ACTION_STORE_ON_STOP |
364 ACTION_STOP_ON_STOP);
5288fbf0 365 break;
9b747530 366 case SIGP_SET_ARCHITECTURE:
5288fbf0
CB
367 vcpu->stat.instruction_sigp_arch++;
368 rc = __sigp_set_arch(vcpu, parameter);
369 break;
370 case SIGP_SET_PREFIX:
371 vcpu->stat.instruction_sigp_prefix++;
372 rc = __sigp_set_prefix(vcpu, cpu_addr, parameter,
5a32c1af 373 &vcpu->run->s.regs.gprs[r1]);
5288fbf0 374 break;
bd59d3a4
CH
375 case SIGP_SENSE_RUNNING:
376 vcpu->stat.instruction_sigp_sense_running++;
377 rc = __sigp_sense_running(vcpu, cpu_addr,
5a32c1af 378 &vcpu->run->s.regs.gprs[r1]);
bd59d3a4 379 break;
5288fbf0
CB
380 case SIGP_RESTART:
381 vcpu->stat.instruction_sigp_restart++;
151104a7
JF
382 rc = __sigp_restart(vcpu, cpu_addr);
383 if (rc == 2) /* busy */
384 break;
5288fbf0
CB
385 /* user space must know about restart */
386 default:
b8e660b8 387 return -EOPNOTSUPP;
5288fbf0
CB
388 }
389
390 if (rc < 0)
391 return rc;
392
393 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
394 vcpu->arch.sie_block->gpsw.mask |= (rc & 3ul) << 44;
395 return 0;
396}
This page took 0.329584 seconds and 5 git commands to generate.