Commit | Line | Data |
---|---|---|
5288fbf0 | 1 | /* |
a53c8fab | 2 | * handling interprocessor communication |
5288fbf0 | 3 | * |
a53c8fab | 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> |
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 | 23 | static int __sigp_sense(struct kvm_vcpu *vcpu, u16 cpu_addr, |
5a32c1af | 24 | u64 *reg) |
5288fbf0 | 25 | { |
180c12fb | 26 | struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int; |
5288fbf0 CB |
27 | int rc; |
28 | ||
29 | if (cpu_addr >= KVM_MAX_VCPUS) | |
ea1918dd | 30 | return SIGP_CC_NOT_OPERATIONAL; |
5288fbf0 | 31 | |
b037a4f3 | 32 | spin_lock(&fi->lock); |
5288fbf0 | 33 | if (fi->local_int[cpu_addr] == NULL) |
ea1918dd | 34 | rc = SIGP_CC_NOT_OPERATIONAL; |
9e6dabef | 35 | else if (!(atomic_read(fi->local_int[cpu_addr]->cpuflags) |
21b26c08 CH |
36 | & (CPUSTAT_ECALL_PEND | CPUSTAT_STOPPED))) |
37 | rc = SIGP_CC_ORDER_CODE_ACCEPTED; | |
38 | else { | |
5288fbf0 | 39 | *reg &= 0xffffffff00000000UL; |
21b26c08 CH |
40 | if (atomic_read(fi->local_int[cpu_addr]->cpuflags) |
41 | & CPUSTAT_ECALL_PEND) | |
42 | *reg |= SIGP_STATUS_EXT_CALL_PENDING; | |
43 | if (atomic_read(fi->local_int[cpu_addr]->cpuflags) | |
44 | & CPUSTAT_STOPPED) | |
45 | *reg |= SIGP_STATUS_STOPPED; | |
ea1918dd | 46 | rc = SIGP_CC_STATUS_STORED; |
5288fbf0 | 47 | } |
b037a4f3 | 48 | spin_unlock(&fi->lock); |
5288fbf0 CB |
49 | |
50 | VCPU_EVENT(vcpu, 4, "sensed status of cpu %x rc %x", cpu_addr, rc); | |
51 | return rc; | |
52 | } | |
53 | ||
54 | static int __sigp_emergency(struct kvm_vcpu *vcpu, u16 cpu_addr) | |
55 | { | |
180c12fb CB |
56 | struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int; |
57 | struct kvm_s390_local_interrupt *li; | |
58 | struct kvm_s390_interrupt_info *inti; | |
5288fbf0 CB |
59 | int rc; |
60 | ||
61 | if (cpu_addr >= KVM_MAX_VCPUS) | |
ea1918dd | 62 | return SIGP_CC_NOT_OPERATIONAL; |
5288fbf0 CB |
63 | |
64 | inti = kzalloc(sizeof(*inti), GFP_KERNEL); | |
65 | if (!inti) | |
66 | return -ENOMEM; | |
67 | ||
68 | inti->type = KVM_S390_INT_EMERGENCY; | |
7697e71f | 69 | inti->emerg.code = vcpu->vcpu_id; |
5288fbf0 | 70 | |
b037a4f3 | 71 | spin_lock(&fi->lock); |
5288fbf0 CB |
72 | li = fi->local_int[cpu_addr]; |
73 | if (li == NULL) { | |
ea1918dd | 74 | rc = SIGP_CC_NOT_OPERATIONAL; |
5288fbf0 CB |
75 | kfree(inti); |
76 | goto unlock; | |
77 | } | |
78 | spin_lock_bh(&li->lock); | |
79 | list_add_tail(&inti->list, &li->list); | |
80 | atomic_set(&li->active, 1); | |
81 | atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags); | |
d0321a24 CB |
82 | if (waitqueue_active(li->wq)) |
83 | wake_up_interruptible(li->wq); | |
5288fbf0 | 84 | spin_unlock_bh(&li->lock); |
ea1918dd | 85 | rc = SIGP_CC_ORDER_CODE_ACCEPTED; |
7697e71f CE |
86 | VCPU_EVENT(vcpu, 4, "sent sigp emerg to cpu %x", cpu_addr); |
87 | unlock: | |
88 | spin_unlock(&fi->lock); | |
89 | return rc; | |
90 | } | |
91 | ||
92 | static int __sigp_external_call(struct kvm_vcpu *vcpu, u16 cpu_addr) | |
93 | { | |
94 | struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int; | |
95 | struct kvm_s390_local_interrupt *li; | |
96 | struct kvm_s390_interrupt_info *inti; | |
97 | int rc; | |
98 | ||
99 | if (cpu_addr >= KVM_MAX_VCPUS) | |
ea1918dd | 100 | return SIGP_CC_NOT_OPERATIONAL; |
7697e71f CE |
101 | |
102 | inti = kzalloc(sizeof(*inti), GFP_KERNEL); | |
103 | if (!inti) | |
104 | return -ENOMEM; | |
105 | ||
106 | inti->type = KVM_S390_INT_EXTERNAL_CALL; | |
107 | inti->extcall.code = vcpu->vcpu_id; | |
108 | ||
109 | spin_lock(&fi->lock); | |
110 | li = fi->local_int[cpu_addr]; | |
111 | if (li == NULL) { | |
ea1918dd | 112 | rc = SIGP_CC_NOT_OPERATIONAL; |
7697e71f CE |
113 | kfree(inti); |
114 | goto unlock; | |
115 | } | |
116 | spin_lock_bh(&li->lock); | |
117 | list_add_tail(&inti->list, &li->list); | |
118 | atomic_set(&li->active, 1); | |
119 | atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags); | |
d0321a24 CB |
120 | if (waitqueue_active(li->wq)) |
121 | wake_up_interruptible(li->wq); | |
7697e71f | 122 | spin_unlock_bh(&li->lock); |
ea1918dd | 123 | rc = SIGP_CC_ORDER_CODE_ACCEPTED; |
7697e71f | 124 | VCPU_EVENT(vcpu, 4, "sent sigp ext call to cpu %x", cpu_addr); |
5288fbf0 | 125 | unlock: |
b037a4f3 | 126 | spin_unlock(&fi->lock); |
5288fbf0 CB |
127 | return rc; |
128 | } | |
129 | ||
9ace903d | 130 | static int __inject_sigp_stop(struct kvm_s390_local_interrupt *li, int action) |
5288fbf0 | 131 | { |
180c12fb | 132 | struct kvm_s390_interrupt_info *inti; |
e879892c | 133 | int rc = SIGP_CC_ORDER_CODE_ACCEPTED; |
5288fbf0 | 134 | |
9940fa80 | 135 | inti = kzalloc(sizeof(*inti), GFP_ATOMIC); |
5288fbf0 CB |
136 | if (!inti) |
137 | return -ENOMEM; | |
5288fbf0 CB |
138 | inti->type = KVM_S390_SIGP_STOP; |
139 | ||
5288fbf0 | 140 | spin_lock_bh(&li->lock); |
a046b816 CD |
141 | if ((atomic_read(li->cpuflags) & CPUSTAT_STOPPED)) { |
142 | kfree(inti); | |
e879892c TH |
143 | if ((action & ACTION_STORE_ON_STOP) != 0) |
144 | rc = -ESHUTDOWN; | |
24a13044 | 145 | goto out; |
a046b816 | 146 | } |
5288fbf0 CB |
147 | list_add_tail(&inti->list, &li->list); |
148 | atomic_set(&li->active, 1); | |
149 | atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags); | |
9ace903d | 150 | li->action_bits |= action; |
d0321a24 CB |
151 | if (waitqueue_active(li->wq)) |
152 | wake_up_interruptible(li->wq); | |
24a13044 | 153 | out: |
5288fbf0 | 154 | spin_unlock_bh(&li->lock); |
9ace903d | 155 | |
e879892c | 156 | return rc; |
9ace903d CE |
157 | } |
158 | ||
159 | static int __sigp_stop(struct kvm_vcpu *vcpu, u16 cpu_addr, int action) | |
160 | { | |
161 | struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int; | |
162 | struct kvm_s390_local_interrupt *li; | |
163 | int rc; | |
164 | ||
165 | if (cpu_addr >= KVM_MAX_VCPUS) | |
ea1918dd | 166 | return SIGP_CC_NOT_OPERATIONAL; |
9ace903d CE |
167 | |
168 | spin_lock(&fi->lock); | |
169 | li = fi->local_int[cpu_addr]; | |
170 | if (li == NULL) { | |
ea1918dd | 171 | rc = SIGP_CC_NOT_OPERATIONAL; |
9ace903d CE |
172 | goto unlock; |
173 | } | |
174 | ||
175 | rc = __inject_sigp_stop(li, action); | |
176 | ||
5288fbf0 | 177 | unlock: |
b037a4f3 | 178 | spin_unlock(&fi->lock); |
5288fbf0 | 179 | VCPU_EVENT(vcpu, 4, "sent sigp stop to cpu %x", cpu_addr); |
e879892c TH |
180 | |
181 | if ((action & ACTION_STORE_ON_STOP) != 0 && rc == -ESHUTDOWN) { | |
182 | /* If the CPU has already been stopped, we still have | |
183 | * to save the status when doing stop-and-store. This | |
184 | * has to be done after unlocking all spinlocks. */ | |
185 | struct kvm_vcpu *dst_vcpu = kvm_get_vcpu(vcpu->kvm, cpu_addr); | |
186 | rc = kvm_s390_store_status_unloaded(dst_vcpu, | |
187 | KVM_S390_STORE_STATUS_NOADDR); | |
188 | } | |
189 | ||
5288fbf0 CB |
190 | return rc; |
191 | } | |
192 | ||
9ace903d CE |
193 | int kvm_s390_inject_sigp_stop(struct kvm_vcpu *vcpu, int action) |
194 | { | |
195 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; | |
196 | return __inject_sigp_stop(li, action); | |
197 | } | |
198 | ||
5288fbf0 CB |
199 | static int __sigp_set_arch(struct kvm_vcpu *vcpu, u32 parameter) |
200 | { | |
201 | int rc; | |
202 | ||
203 | switch (parameter & 0xff) { | |
204 | case 0: | |
ea1918dd | 205 | rc = SIGP_CC_NOT_OPERATIONAL; |
5288fbf0 CB |
206 | break; |
207 | case 1: | |
208 | case 2: | |
ea1918dd | 209 | rc = SIGP_CC_ORDER_CODE_ACCEPTED; |
5288fbf0 CB |
210 | break; |
211 | default: | |
b8e660b8 | 212 | rc = -EOPNOTSUPP; |
5288fbf0 CB |
213 | } |
214 | return rc; | |
215 | } | |
216 | ||
217 | static int __sigp_set_prefix(struct kvm_vcpu *vcpu, u16 cpu_addr, u32 address, | |
5a32c1af | 218 | u64 *reg) |
5288fbf0 | 219 | { |
180c12fb | 220 | struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int; |
53cb780a | 221 | struct kvm_s390_local_interrupt *li = NULL; |
180c12fb | 222 | struct kvm_s390_interrupt_info *inti; |
5288fbf0 CB |
223 | int rc; |
224 | u8 tmp; | |
225 | ||
226 | /* make sure that the new value is valid memory */ | |
227 | address = address & 0x7fffe000u; | |
092670cd CO |
228 | if (copy_from_guest_absolute(vcpu, &tmp, address, 1) || |
229 | copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1)) { | |
0744426e | 230 | *reg &= 0xffffffff00000000UL; |
a9ae32c3 | 231 | *reg |= SIGP_STATUS_INVALID_PARAMETER; |
ea1918dd | 232 | return SIGP_CC_STATUS_STORED; |
5288fbf0 CB |
233 | } |
234 | ||
235 | inti = kzalloc(sizeof(*inti), GFP_KERNEL); | |
236 | if (!inti) | |
ea1918dd | 237 | return SIGP_CC_BUSY; |
5288fbf0 | 238 | |
b037a4f3 | 239 | spin_lock(&fi->lock); |
53cb780a RK |
240 | if (cpu_addr < KVM_MAX_VCPUS) |
241 | li = fi->local_int[cpu_addr]; | |
5288fbf0 | 242 | |
53cb780a | 243 | if (li == NULL) { |
0744426e HC |
244 | *reg &= 0xffffffff00000000UL; |
245 | *reg |= SIGP_STATUS_INCORRECT_STATE; | |
ea1918dd | 246 | rc = SIGP_CC_STATUS_STORED; |
5288fbf0 CB |
247 | kfree(inti); |
248 | goto out_fi; | |
249 | } | |
250 | ||
251 | spin_lock_bh(&li->lock); | |
252 | /* cpu must be in stopped state */ | |
9e6dabef | 253 | if (!(atomic_read(li->cpuflags) & CPUSTAT_STOPPED)) { |
0744426e HC |
254 | *reg &= 0xffffffff00000000UL; |
255 | *reg |= SIGP_STATUS_INCORRECT_STATE; | |
ea1918dd | 256 | rc = SIGP_CC_STATUS_STORED; |
5288fbf0 CB |
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); | |
d0321a24 CB |
266 | if (waitqueue_active(li->wq)) |
267 | wake_up_interruptible(li->wq); | |
ea1918dd | 268 | rc = SIGP_CC_ORDER_CODE_ACCEPTED; |
5288fbf0 CB |
269 | |
270 | VCPU_EVENT(vcpu, 4, "set prefix of cpu %02x to %x", cpu_addr, address); | |
271 | out_li: | |
272 | spin_unlock_bh(&li->lock); | |
273 | out_fi: | |
b037a4f3 | 274 | spin_unlock(&fi->lock); |
5288fbf0 CB |
275 | return rc; |
276 | } | |
277 | ||
bd59d3a4 | 278 | static int __sigp_sense_running(struct kvm_vcpu *vcpu, u16 cpu_addr, |
5a32c1af | 279 | u64 *reg) |
bd59d3a4 CH |
280 | { |
281 | int rc; | |
282 | struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int; | |
283 | ||
284 | if (cpu_addr >= KVM_MAX_VCPUS) | |
ea1918dd | 285 | return SIGP_CC_NOT_OPERATIONAL; |
bd59d3a4 CH |
286 | |
287 | spin_lock(&fi->lock); | |
288 | if (fi->local_int[cpu_addr] == NULL) | |
ea1918dd | 289 | rc = SIGP_CC_NOT_OPERATIONAL; |
bd59d3a4 CH |
290 | else { |
291 | if (atomic_read(fi->local_int[cpu_addr]->cpuflags) | |
292 | & CPUSTAT_RUNNING) { | |
293 | /* running */ | |
ea1918dd | 294 | rc = SIGP_CC_ORDER_CODE_ACCEPTED; |
bd59d3a4 CH |
295 | } else { |
296 | /* not running */ | |
297 | *reg &= 0xffffffff00000000UL; | |
a9ae32c3 | 298 | *reg |= SIGP_STATUS_NOT_RUNNING; |
ea1918dd | 299 | rc = SIGP_CC_STATUS_STORED; |
bd59d3a4 CH |
300 | } |
301 | } | |
302 | spin_unlock(&fi->lock); | |
303 | ||
304 | VCPU_EVENT(vcpu, 4, "sensed running status of cpu %x rc %x", cpu_addr, | |
305 | rc); | |
306 | ||
307 | return rc; | |
308 | } | |
309 | ||
151104a7 JF |
310 | static int __sigp_restart(struct kvm_vcpu *vcpu, u16 cpu_addr) |
311 | { | |
151104a7 JF |
312 | struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int; |
313 | struct kvm_s390_local_interrupt *li; | |
ea1918dd | 314 | int rc = SIGP_CC_ORDER_CODE_ACCEPTED; |
151104a7 JF |
315 | |
316 | if (cpu_addr >= KVM_MAX_VCPUS) | |
ea1918dd | 317 | return SIGP_CC_NOT_OPERATIONAL; |
151104a7 JF |
318 | |
319 | spin_lock(&fi->lock); | |
320 | li = fi->local_int[cpu_addr]; | |
321 | if (li == NULL) { | |
ea1918dd | 322 | rc = SIGP_CC_NOT_OPERATIONAL; |
151104a7 JF |
323 | goto out; |
324 | } | |
325 | ||
326 | spin_lock_bh(&li->lock); | |
327 | if (li->action_bits & ACTION_STOP_ON_STOP) | |
ea1918dd | 328 | rc = SIGP_CC_BUSY; |
151104a7 JF |
329 | else |
330 | VCPU_EVENT(vcpu, 4, "sigp restart %x to handle userspace", | |
331 | cpu_addr); | |
332 | spin_unlock_bh(&li->lock); | |
333 | out: | |
334 | spin_unlock(&fi->lock); | |
335 | return rc; | |
336 | } | |
337 | ||
5288fbf0 CB |
338 | int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu) |
339 | { | |
340 | int r1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4; | |
341 | int r3 = vcpu->arch.sie_block->ipa & 0x000f; | |
5288fbf0 | 342 | u32 parameter; |
5a32c1af | 343 | u16 cpu_addr = vcpu->run->s.regs.gprs[r3]; |
5288fbf0 CB |
344 | u8 order_code; |
345 | int rc; | |
346 | ||
3eb77d51 CB |
347 | /* sigp in userspace can exit */ |
348 | if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) | |
208dd756 | 349 | return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); |
3eb77d51 | 350 | |
b1c571a5 | 351 | order_code = kvm_s390_get_base_disp_rs(vcpu); |
5288fbf0 CB |
352 | |
353 | if (r1 % 2) | |
5a32c1af | 354 | parameter = vcpu->run->s.regs.gprs[r1]; |
5288fbf0 | 355 | else |
5a32c1af | 356 | parameter = vcpu->run->s.regs.gprs[r1 + 1]; |
5288fbf0 | 357 | |
5786fffa | 358 | trace_kvm_s390_handle_sigp(vcpu, order_code, cpu_addr, parameter); |
5288fbf0 CB |
359 | switch (order_code) { |
360 | case SIGP_SENSE: | |
361 | vcpu->stat.instruction_sigp_sense++; | |
362 | rc = __sigp_sense(vcpu, cpu_addr, | |
5a32c1af | 363 | &vcpu->run->s.regs.gprs[r1]); |
5288fbf0 | 364 | break; |
7697e71f CE |
365 | case SIGP_EXTERNAL_CALL: |
366 | vcpu->stat.instruction_sigp_external_call++; | |
367 | rc = __sigp_external_call(vcpu, cpu_addr); | |
368 | break; | |
a9ae32c3 | 369 | case SIGP_EMERGENCY_SIGNAL: |
5288fbf0 CB |
370 | vcpu->stat.instruction_sigp_emergency++; |
371 | rc = __sigp_emergency(vcpu, cpu_addr); | |
372 | break; | |
373 | case SIGP_STOP: | |
374 | vcpu->stat.instruction_sigp_stop++; | |
9ace903d | 375 | rc = __sigp_stop(vcpu, cpu_addr, ACTION_STOP_ON_STOP); |
5288fbf0 | 376 | break; |
a9ae32c3 | 377 | case SIGP_STOP_AND_STORE_STATUS: |
5288fbf0 | 378 | vcpu->stat.instruction_sigp_stop++; |
9ec2d6dc JF |
379 | rc = __sigp_stop(vcpu, cpu_addr, ACTION_STORE_ON_STOP | |
380 | ACTION_STOP_ON_STOP); | |
5288fbf0 | 381 | break; |
a9ae32c3 | 382 | case SIGP_SET_ARCHITECTURE: |
5288fbf0 CB |
383 | vcpu->stat.instruction_sigp_arch++; |
384 | rc = __sigp_set_arch(vcpu, parameter); | |
385 | break; | |
386 | case SIGP_SET_PREFIX: | |
387 | vcpu->stat.instruction_sigp_prefix++; | |
388 | rc = __sigp_set_prefix(vcpu, cpu_addr, parameter, | |
5a32c1af | 389 | &vcpu->run->s.regs.gprs[r1]); |
5288fbf0 | 390 | break; |
bd59d3a4 CH |
391 | case SIGP_SENSE_RUNNING: |
392 | vcpu->stat.instruction_sigp_sense_running++; | |
393 | rc = __sigp_sense_running(vcpu, cpu_addr, | |
5a32c1af | 394 | &vcpu->run->s.regs.gprs[r1]); |
bd59d3a4 | 395 | break; |
5288fbf0 CB |
396 | case SIGP_RESTART: |
397 | vcpu->stat.instruction_sigp_restart++; | |
151104a7 | 398 | rc = __sigp_restart(vcpu, cpu_addr); |
ea1918dd | 399 | if (rc == SIGP_CC_BUSY) |
151104a7 | 400 | break; |
5288fbf0 CB |
401 | /* user space must know about restart */ |
402 | default: | |
b8e660b8 | 403 | return -EOPNOTSUPP; |
5288fbf0 CB |
404 | } |
405 | ||
406 | if (rc < 0) | |
407 | return rc; | |
408 | ||
409 | vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44); | |
410 | vcpu->arch.sie_block->gpsw.mask |= (rc & 3ul) << 44; | |
411 | return 0; | |
412 | } |