blob: f815118835f3c3221932c5e01c86847cc1ec9ba8 [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>
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010018#include "gaccess.h"
19#include "kvm-s390.h"
20
21/* sigp order codes */
22#define SIGP_SENSE 0x01
23#define SIGP_EXTERNAL_CALL 0x02
24#define SIGP_EMERGENCY 0x03
25#define SIGP_START 0x04
26#define SIGP_STOP 0x05
27#define SIGP_RESTART 0x06
28#define SIGP_STOP_STORE_STATUS 0x09
29#define SIGP_INITIAL_CPU_RESET 0x0b
30#define SIGP_CPU_RESET 0x0c
31#define SIGP_SET_PREFIX 0x0d
32#define SIGP_STORE_STATUS_ADDR 0x0e
33#define SIGP_SET_ARCH 0x12
34
35/* cpu status bits */
36#define SIGP_STAT_EQUIPMENT_CHECK 0x80000000UL
37#define SIGP_STAT_INCORRECT_STATE 0x00000200UL
38#define SIGP_STAT_INVALID_PARAMETER 0x00000100UL
39#define SIGP_STAT_EXT_CALL_PENDING 0x00000080UL
40#define SIGP_STAT_STOPPED 0x00000040UL
41#define SIGP_STAT_OPERATOR_INTERV 0x00000020UL
42#define SIGP_STAT_CHECK_STOP 0x00000010UL
43#define SIGP_STAT_INOPERATIVE 0x00000004UL
44#define SIGP_STAT_INVALID_ORDER 0x00000002UL
45#define SIGP_STAT_RECEIVER_CHECK 0x00000001UL
46
47
Martin Schwidefsky00963692008-07-25 15:51:00 +020048static int __sigp_sense(struct kvm_vcpu *vcpu, u16 cpu_addr,
49 unsigned long *reg)
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010050{
Christian Borntraeger180c12f2008-06-27 15:05:40 +020051 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010052 int rc;
53
54 if (cpu_addr >= KVM_MAX_VCPUS)
55 return 3; /* not operational */
56
Christian Borntraegerb037a4f2009-05-12 17:21:50 +020057 spin_lock(&fi->lock);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010058 if (fi->local_int[cpu_addr] == NULL)
59 rc = 3; /* not operational */
60 else if (atomic_read(fi->local_int[cpu_addr]->cpuflags)
61 & CPUSTAT_RUNNING) {
62 *reg &= 0xffffffff00000000UL;
63 rc = 1; /* status stored */
64 } else {
65 *reg &= 0xffffffff00000000UL;
66 *reg |= SIGP_STAT_STOPPED;
67 rc = 1; /* status stored */
68 }
Christian Borntraegerb037a4f2009-05-12 17:21:50 +020069 spin_unlock(&fi->lock);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010070
71 VCPU_EVENT(vcpu, 4, "sensed status of cpu %x rc %x", cpu_addr, rc);
72 return rc;
73}
74
75static int __sigp_emergency(struct kvm_vcpu *vcpu, u16 cpu_addr)
76{
Christian Borntraeger180c12f2008-06-27 15:05:40 +020077 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
78 struct kvm_s390_local_interrupt *li;
79 struct kvm_s390_interrupt_info *inti;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010080 int rc;
81
82 if (cpu_addr >= KVM_MAX_VCPUS)
83 return 3; /* not operational */
84
85 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
86 if (!inti)
87 return -ENOMEM;
88
89 inti->type = KVM_S390_INT_EMERGENCY;
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020090 inti->emerg.code = vcpu->vcpu_id;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010091
Christian Borntraegerb037a4f2009-05-12 17:21:50 +020092 spin_lock(&fi->lock);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010093 li = fi->local_int[cpu_addr];
94 if (li == NULL) {
95 rc = 3; /* not operational */
96 kfree(inti);
97 goto unlock;
98 }
99 spin_lock_bh(&li->lock);
100 list_add_tail(&inti->list, &li->list);
101 atomic_set(&li->active, 1);
102 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
103 if (waitqueue_active(&li->wq))
104 wake_up_interruptible(&li->wq);
105 spin_unlock_bh(&li->lock);
106 rc = 0; /* order accepted */
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200107 VCPU_EVENT(vcpu, 4, "sent sigp emerg to cpu %x", cpu_addr);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100108unlock:
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200109 spin_unlock(&fi->lock);
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200110 return rc;
111}
112
113static int __sigp_external_call(struct kvm_vcpu *vcpu, u16 cpu_addr)
114{
115 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
116 struct kvm_s390_local_interrupt *li;
117 struct kvm_s390_interrupt_info *inti;
118 int rc;
119
120 if (cpu_addr >= KVM_MAX_VCPUS)
121 return 3; /* not operational */
122
123 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
124 if (!inti)
125 return -ENOMEM;
126
127 inti->type = KVM_S390_INT_EXTERNAL_CALL;
128 inti->extcall.code = vcpu->vcpu_id;
129
130 spin_lock(&fi->lock);
131 li = fi->local_int[cpu_addr];
132 if (li == NULL) {
133 rc = 3; /* not operational */
134 kfree(inti);
135 goto unlock;
136 }
137 spin_lock_bh(&li->lock);
138 list_add_tail(&inti->list, &li->list);
139 atomic_set(&li->active, 1);
140 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
141 if (waitqueue_active(&li->wq))
142 wake_up_interruptible(&li->wq);
143 spin_unlock_bh(&li->lock);
144 rc = 0; /* order accepted */
145 VCPU_EVENT(vcpu, 4, "sent sigp ext call to cpu %x", cpu_addr);
146unlock:
147 spin_unlock(&fi->lock);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100148 return rc;
149}
150
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200151static int __inject_sigp_stop(struct kvm_s390_local_interrupt *li, int action)
152{
153 struct kvm_s390_interrupt_info *inti;
154
Julia Lawall9940fa82010-06-08 18:58:11 +0200155 inti = kzalloc(sizeof(*inti), GFP_ATOMIC);
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200156 if (!inti)
157 return -ENOMEM;
158 inti->type = KVM_S390_SIGP_STOP;
159
160 spin_lock_bh(&li->lock);
161 list_add_tail(&inti->list, &li->list);
162 atomic_set(&li->active, 1);
163 atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags);
164 li->action_bits |= action;
165 if (waitqueue_active(&li->wq))
166 wake_up_interruptible(&li->wq);
167 spin_unlock_bh(&li->lock);
168
169 return 0; /* order accepted */
170}
171
172static int __sigp_stop(struct kvm_vcpu *vcpu, u16 cpu_addr, int action)
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100173{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200174 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
175 struct kvm_s390_local_interrupt *li;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100176 int rc;
177
178 if (cpu_addr >= KVM_MAX_VCPUS)
179 return 3; /* not operational */
180
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200181 spin_lock(&fi->lock);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100182 li = fi->local_int[cpu_addr];
183 if (li == NULL) {
184 rc = 3; /* not operational */
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100185 goto unlock;
186 }
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200187
188 rc = __inject_sigp_stop(li, action);
189
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100190unlock:
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200191 spin_unlock(&fi->lock);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100192 VCPU_EVENT(vcpu, 4, "sent sigp stop to cpu %x", cpu_addr);
193 return rc;
194}
195
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200196int kvm_s390_inject_sigp_stop(struct kvm_vcpu *vcpu, int action)
197{
198 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
199 return __inject_sigp_stop(li, action);
200}
201
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100202static int __sigp_set_arch(struct kvm_vcpu *vcpu, u32 parameter)
203{
204 int rc;
205
206 switch (parameter & 0xff) {
207 case 0:
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100208 rc = 3; /* not operational */
209 break;
210 case 1:
211 case 2:
212 rc = 0; /* order accepted */
213 break;
214 default:
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100215 rc = -EOPNOTSUPP;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100216 }
217 return rc;
218}
219
220static int __sigp_set_prefix(struct kvm_vcpu *vcpu, u16 cpu_addr, u32 address,
Martin Schwidefsky00963692008-07-25 15:51:00 +0200221 unsigned long *reg)
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100222{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200223 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
Roel Kluin53cb7802009-08-07 10:39:25 +0200224 struct kvm_s390_local_interrupt *li = NULL;
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200225 struct kvm_s390_interrupt_info *inti;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100226 int rc;
227 u8 tmp;
228
229 /* make sure that the new value is valid memory */
230 address = address & 0x7fffe000u;
Carsten Otte092670c2011-07-24 10:48:22 +0200231 if (copy_from_guest_absolute(vcpu, &tmp, address, 1) ||
232 copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1)) {
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100233 *reg |= SIGP_STAT_INVALID_PARAMETER;
234 return 1; /* invalid parameter */
235 }
236
237 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
238 if (!inti)
239 return 2; /* busy */
240
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200241 spin_lock(&fi->lock);
Roel Kluin53cb7802009-08-07 10:39:25 +0200242 if (cpu_addr < KVM_MAX_VCPUS)
243 li = fi->local_int[cpu_addr];
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100244
Roel Kluin53cb7802009-08-07 10:39:25 +0200245 if (li == NULL) {
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100246 rc = 1; /* incorrect state */
247 *reg &= SIGP_STAT_INCORRECT_STATE;
248 kfree(inti);
249 goto out_fi;
250 }
251
252 spin_lock_bh(&li->lock);
253 /* cpu must be in stopped state */
254 if (atomic_read(li->cpuflags) & CPUSTAT_RUNNING) {
255 rc = 1; /* incorrect state */
256 *reg &= SIGP_STAT_INCORRECT_STATE;
257 kfree(inti);
258 goto out_li;
259 }
260
261 inti->type = KVM_S390_SIGP_SET_PREFIX;
262 inti->prefix.address = address;
263
264 list_add_tail(&inti->list, &li->list);
265 atomic_set(&li->active, 1);
266 if (waitqueue_active(&li->wq))
267 wake_up_interruptible(&li->wq);
268 rc = 0; /* order accepted */
269
270 VCPU_EVENT(vcpu, 4, "set prefix of cpu %02x to %x", cpu_addr, address);
271out_li:
272 spin_unlock_bh(&li->lock);
273out_fi:
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200274 spin_unlock(&fi->lock);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100275 return rc;
276}
277
278int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu)
279{
280 int r1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
281 int r3 = vcpu->arch.sie_block->ipa & 0x000f;
282 int base2 = vcpu->arch.sie_block->ipb >> 28;
283 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
284 u32 parameter;
285 u16 cpu_addr = vcpu->arch.guest_gprs[r3];
286 u8 order_code;
287 int rc;
288
Christian Borntraeger3eb77d52008-11-17 14:50:35 +0100289 /* sigp in userspace can exit */
290 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
291 return kvm_s390_inject_program_int(vcpu,
292 PGM_PRIVILEGED_OPERATION);
293
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100294 order_code = disp2;
295 if (base2)
296 order_code += vcpu->arch.guest_gprs[base2];
297
298 if (r1 % 2)
299 parameter = vcpu->arch.guest_gprs[r1];
300 else
301 parameter = vcpu->arch.guest_gprs[r1 + 1];
302
303 switch (order_code) {
304 case SIGP_SENSE:
305 vcpu->stat.instruction_sigp_sense++;
306 rc = __sigp_sense(vcpu, cpu_addr,
307 &vcpu->arch.guest_gprs[r1]);
308 break;
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200309 case SIGP_EXTERNAL_CALL:
310 vcpu->stat.instruction_sigp_external_call++;
311 rc = __sigp_external_call(vcpu, cpu_addr);
312 break;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100313 case SIGP_EMERGENCY:
314 vcpu->stat.instruction_sigp_emergency++;
315 rc = __sigp_emergency(vcpu, cpu_addr);
316 break;
317 case SIGP_STOP:
318 vcpu->stat.instruction_sigp_stop++;
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200319 rc = __sigp_stop(vcpu, cpu_addr, ACTION_STOP_ON_STOP);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100320 break;
321 case SIGP_STOP_STORE_STATUS:
322 vcpu->stat.instruction_sigp_stop++;
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200323 rc = __sigp_stop(vcpu, cpu_addr, ACTION_STORE_ON_STOP);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100324 break;
325 case SIGP_SET_ARCH:
326 vcpu->stat.instruction_sigp_arch++;
327 rc = __sigp_set_arch(vcpu, parameter);
328 break;
329 case SIGP_SET_PREFIX:
330 vcpu->stat.instruction_sigp_prefix++;
331 rc = __sigp_set_prefix(vcpu, cpu_addr, parameter,
332 &vcpu->arch.guest_gprs[r1]);
333 break;
334 case SIGP_RESTART:
335 vcpu->stat.instruction_sigp_restart++;
336 /* user space must know about restart */
337 default:
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100338 return -EOPNOTSUPP;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100339 }
340
341 if (rc < 0)
342 return rc;
343
344 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
345 vcpu->arch.sie_block->gpsw.mask |= (rc & 3ul) << 44;
346 return 0;
347}