blob: b3b4748485eefa072127b0ccc5d92e2dab6fb3a2 [file] [log] [blame]
Carsten Otteba5c1e92008-03-25 18:47:26 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * handling kvm guest interrupts
Carsten Otteba5c1e92008-03-25 18:47:26 +01003 *
4 * Copyright IBM Corp. 2008
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 */
12
Christian Borntraegerca872302009-05-12 17:21:49 +020013#include <linux/interrupt.h>
Carsten Otteba5c1e92008-03-25 18:47:26 +010014#include <linux/kvm_host.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010015#include <linux/hrtimer.h>
Christian Borntraeger3cd61292008-07-25 15:51:54 +020016#include <linux/signal.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010018#include <asm/asm-offsets.h>
19#include <asm/uaccess.h>
Carsten Otteba5c1e92008-03-25 18:47:26 +010020#include "kvm-s390.h"
21#include "gaccess.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020022#include "trace-s390.h"
Carsten Otteba5c1e92008-03-25 18:47:26 +010023
Cornelia Huckd8346b72012-12-20 15:32:08 +010024#define IOINT_SCHID_MASK 0x0000ffff
25#define IOINT_SSID_MASK 0x00030000
26#define IOINT_CSSID_MASK 0x03fc0000
27#define IOINT_AI_MASK 0x04000000
28
29static int is_ioint(u64 type)
30{
31 return ((type & 0xfffe0000u) != 0xfffe0000u);
32}
33
Carsten Otteba5c1e92008-03-25 18:47:26 +010034static int psw_extint_disabled(struct kvm_vcpu *vcpu)
35{
36 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT);
37}
38
Cornelia Huckd8346b72012-12-20 15:32:08 +010039static int psw_ioint_disabled(struct kvm_vcpu *vcpu)
40{
41 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO);
42}
43
Cornelia Huck48a3e952012-12-20 15:32:09 +010044static int psw_mchk_disabled(struct kvm_vcpu *vcpu)
45{
46 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK);
47}
48
Carsten Otteba5c1e92008-03-25 18:47:26 +010049static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
50{
51 if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER) ||
52 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO) ||
53 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT))
54 return 0;
55 return 1;
56}
57
58static int __interrupt_is_deliverable(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +020059 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +010060{
61 switch (inti->type) {
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020062 case KVM_S390_INT_EXTERNAL_CALL:
63 if (psw_extint_disabled(vcpu))
64 return 0;
65 if (vcpu->arch.sie_block->gcr[0] & 0x2000ul)
66 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +010067 case KVM_S390_INT_EMERGENCY:
68 if (psw_extint_disabled(vcpu))
69 return 0;
70 if (vcpu->arch.sie_block->gcr[0] & 0x4000ul)
71 return 1;
72 return 0;
73 case KVM_S390_INT_SERVICE:
74 if (psw_extint_disabled(vcpu))
75 return 0;
76 if (vcpu->arch.sie_block->gcr[0] & 0x200ul)
77 return 1;
78 return 0;
79 case KVM_S390_INT_VIRTIO:
80 if (psw_extint_disabled(vcpu))
81 return 0;
82 if (vcpu->arch.sie_block->gcr[0] & 0x200ul)
83 return 1;
84 return 0;
85 case KVM_S390_PROGRAM_INT:
86 case KVM_S390_SIGP_STOP:
87 case KVM_S390_SIGP_SET_PREFIX:
88 case KVM_S390_RESTART:
89 return 1;
Cornelia Huck48a3e952012-12-20 15:32:09 +010090 case KVM_S390_MCHK:
91 if (psw_mchk_disabled(vcpu))
92 return 0;
93 if (vcpu->arch.sie_block->gcr[14] & inti->mchk.cr14)
94 return 1;
95 return 0;
Cornelia Huckd8346b72012-12-20 15:32:08 +010096 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
97 if (psw_ioint_disabled(vcpu))
98 return 0;
99 if (vcpu->arch.sie_block->gcr[6] & inti->io.io_int_word)
100 return 1;
101 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100102 default:
Cornelia Huckd8346b72012-12-20 15:32:08 +0100103 printk(KERN_WARNING "illegal interrupt type %llx\n",
104 inti->type);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100105 BUG();
106 }
107 return 0;
108}
109
110static void __set_cpu_idle(struct kvm_vcpu *vcpu)
111{
112 BUG_ON(vcpu->vcpu_id > KVM_MAX_VCPUS - 1);
113 atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
114 set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
115}
116
117static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
118{
119 BUG_ON(vcpu->vcpu_id > KVM_MAX_VCPUS - 1);
120 atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
121 clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
122}
123
124static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
125{
126 atomic_clear_mask(CPUSTAT_ECALL_PEND |
127 CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
128 &vcpu->arch.sie_block->cpuflags);
129 vcpu->arch.sie_block->lctl = 0x0000;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100130 vcpu->arch.sie_block->ictl &= ~ICTL_LPSW;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100131}
132
133static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
134{
135 atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags);
136}
137
138static void __set_intercept_indicator(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200139 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100140{
141 switch (inti->type) {
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200142 case KVM_S390_INT_EXTERNAL_CALL:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100143 case KVM_S390_INT_EMERGENCY:
144 case KVM_S390_INT_SERVICE:
145 case KVM_S390_INT_VIRTIO:
146 if (psw_extint_disabled(vcpu))
147 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
148 else
149 vcpu->arch.sie_block->lctl |= LCTL_CR0;
150 break;
151 case KVM_S390_SIGP_STOP:
152 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
153 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100154 case KVM_S390_MCHK:
155 if (psw_mchk_disabled(vcpu))
156 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
157 else
158 vcpu->arch.sie_block->lctl |= LCTL_CR14;
159 break;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100160 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
161 if (psw_ioint_disabled(vcpu))
162 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
163 else
164 vcpu->arch.sie_block->lctl |= LCTL_CR6;
165 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100166 default:
167 BUG();
168 }
169}
170
171static void __do_deliver_interrupt(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200172 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100173{
174 const unsigned short table[] = { 2, 4, 4, 6 };
175 int rc, exception = 0;
176
177 switch (inti->type) {
178 case KVM_S390_INT_EMERGENCY:
179 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg");
180 vcpu->stat.deliver_emergency_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200181 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
182 inti->emerg.code, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100183 rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x1201);
184 if (rc == -EFAULT)
185 exception = 1;
186
Martin Schwidefsky7e180bd2012-03-11 11:59:25 -0400187 rc = put_guest_u16(vcpu, __LC_EXT_CPU_ADDR, inti->emerg.code);
Christian Ehrhardt8bb3a2e2011-07-24 10:48:31 +0200188 if (rc == -EFAULT)
189 exception = 1;
190
Carsten Otteba5c1e92008-03-25 18:47:26 +0100191 rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
192 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
193 if (rc == -EFAULT)
194 exception = 1;
195
196 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
197 __LC_EXT_NEW_PSW, sizeof(psw_t));
198 if (rc == -EFAULT)
199 exception = 1;
200 break;
201
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200202 case KVM_S390_INT_EXTERNAL_CALL:
203 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call");
204 vcpu->stat.deliver_external_call++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200205 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
206 inti->extcall.code, 0);
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200207 rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x1202);
208 if (rc == -EFAULT)
209 exception = 1;
210
Martin Schwidefsky7e180bd2012-03-11 11:59:25 -0400211 rc = put_guest_u16(vcpu, __LC_EXT_CPU_ADDR, inti->extcall.code);
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200212 if (rc == -EFAULT)
213 exception = 1;
214
215 rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
216 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
217 if (rc == -EFAULT)
218 exception = 1;
219
220 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
221 __LC_EXT_NEW_PSW, sizeof(psw_t));
222 if (rc == -EFAULT)
223 exception = 1;
224 break;
225
Carsten Otteba5c1e92008-03-25 18:47:26 +0100226 case KVM_S390_INT_SERVICE:
227 VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x",
228 inti->ext.ext_params);
229 vcpu->stat.deliver_service_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200230 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
231 inti->ext.ext_params, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100232 rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x2401);
233 if (rc == -EFAULT)
234 exception = 1;
235
236 rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
237 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
238 if (rc == -EFAULT)
239 exception = 1;
240
241 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
242 __LC_EXT_NEW_PSW, sizeof(psw_t));
243 if (rc == -EFAULT)
244 exception = 1;
245
246 rc = put_guest_u32(vcpu, __LC_EXT_PARAMS, inti->ext.ext_params);
247 if (rc == -EFAULT)
248 exception = 1;
249 break;
250
251 case KVM_S390_INT_VIRTIO:
Heiko Carstens33e19112009-01-09 12:14:56 +0100252 VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx",
Carsten Otteba5c1e92008-03-25 18:47:26 +0100253 inti->ext.ext_params, inti->ext.ext_params2);
254 vcpu->stat.deliver_virtio_interrupt++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200255 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
256 inti->ext.ext_params,
257 inti->ext.ext_params2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100258 rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x2603);
259 if (rc == -EFAULT)
260 exception = 1;
261
Martin Schwidefsky7e180bd2012-03-11 11:59:25 -0400262 rc = put_guest_u16(vcpu, __LC_EXT_CPU_ADDR, 0x0d00);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100263 if (rc == -EFAULT)
264 exception = 1;
265
266 rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
267 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
268 if (rc == -EFAULT)
269 exception = 1;
270
271 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
272 __LC_EXT_NEW_PSW, sizeof(psw_t));
273 if (rc == -EFAULT)
274 exception = 1;
275
276 rc = put_guest_u32(vcpu, __LC_EXT_PARAMS, inti->ext.ext_params);
277 if (rc == -EFAULT)
278 exception = 1;
279
Heiko Carstenscbb870c2010-02-26 22:37:43 +0100280 rc = put_guest_u64(vcpu, __LC_EXT_PARAMS2,
281 inti->ext.ext_params2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100282 if (rc == -EFAULT)
283 exception = 1;
284 break;
285
286 case KVM_S390_SIGP_STOP:
287 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu stop");
288 vcpu->stat.deliver_stop_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200289 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
290 0, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100291 __set_intercept_indicator(vcpu, inti);
292 break;
293
294 case KVM_S390_SIGP_SET_PREFIX:
295 VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x",
296 inti->prefix.address);
297 vcpu->stat.deliver_prefix_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200298 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
299 inti->prefix.address, 0);
Christian Borntraeger8d26cf72012-01-11 11:19:32 +0100300 kvm_s390_set_prefix(vcpu, inti->prefix.address);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100301 break;
302
303 case KVM_S390_RESTART:
304 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart");
305 vcpu->stat.deliver_restart_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200306 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
307 0, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100308 rc = copy_to_guest(vcpu, offsetof(struct _lowcore,
309 restart_old_psw), &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
310 if (rc == -EFAULT)
311 exception = 1;
312
313 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
314 offsetof(struct _lowcore, restart_psw), sizeof(psw_t));
315 if (rc == -EFAULT)
316 exception = 1;
Cornelia Huck9e6dabe2011-11-17 11:00:41 +0100317 atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100318 break;
319
320 case KVM_S390_PROGRAM_INT:
321 VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x",
322 inti->pgm.code,
323 table[vcpu->arch.sie_block->ipa >> 14]);
324 vcpu->stat.deliver_program_int++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200325 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
326 inti->pgm.code, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100327 rc = put_guest_u16(vcpu, __LC_PGM_INT_CODE, inti->pgm.code);
328 if (rc == -EFAULT)
329 exception = 1;
330
331 rc = put_guest_u16(vcpu, __LC_PGM_ILC,
332 table[vcpu->arch.sie_block->ipa >> 14]);
333 if (rc == -EFAULT)
334 exception = 1;
335
336 rc = copy_to_guest(vcpu, __LC_PGM_OLD_PSW,
337 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
338 if (rc == -EFAULT)
339 exception = 1;
340
341 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
342 __LC_PGM_NEW_PSW, sizeof(psw_t));
343 if (rc == -EFAULT)
344 exception = 1;
345 break;
346
Cornelia Huck48a3e952012-12-20 15:32:09 +0100347 case KVM_S390_MCHK:
348 VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx",
349 inti->mchk.mcic);
350 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
351 inti->mchk.cr14,
352 inti->mchk.mcic);
353 rc = kvm_s390_vcpu_store_status(vcpu,
354 KVM_S390_STORE_STATUS_PREFIXED);
355 if (rc == -EFAULT)
356 exception = 1;
357
358 rc = put_guest_u64(vcpu, __LC_MCCK_CODE, inti->mchk.mcic);
359 if (rc == -EFAULT)
360 exception = 1;
361
362 rc = copy_to_guest(vcpu, __LC_MCK_OLD_PSW,
363 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
364 if (rc == -EFAULT)
365 exception = 1;
366
367 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
368 __LC_MCK_NEW_PSW, sizeof(psw_t));
369 if (rc == -EFAULT)
370 exception = 1;
371 break;
372
Cornelia Huckd8346b72012-12-20 15:32:08 +0100373 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
374 {
375 __u32 param0 = ((__u32)inti->io.subchannel_id << 16) |
376 inti->io.subchannel_nr;
377 __u64 param1 = ((__u64)inti->io.io_int_parm << 32) |
378 inti->io.io_int_word;
379 VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type);
380 vcpu->stat.deliver_io_int++;
381 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
382 param0, param1);
383 rc = put_guest_u16(vcpu, __LC_SUBCHANNEL_ID,
384 inti->io.subchannel_id);
385 if (rc == -EFAULT)
386 exception = 1;
387
388 rc = put_guest_u16(vcpu, __LC_SUBCHANNEL_NR,
389 inti->io.subchannel_nr);
390 if (rc == -EFAULT)
391 exception = 1;
392
393 rc = put_guest_u32(vcpu, __LC_IO_INT_PARM,
394 inti->io.io_int_parm);
395 if (rc == -EFAULT)
396 exception = 1;
397
398 rc = put_guest_u32(vcpu, __LC_IO_INT_WORD,
399 inti->io.io_int_word);
400 if (rc == -EFAULT)
401 exception = 1;
402
403 rc = copy_to_guest(vcpu, __LC_IO_OLD_PSW,
404 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
405 if (rc == -EFAULT)
406 exception = 1;
407
408 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
409 __LC_IO_NEW_PSW, sizeof(psw_t));
410 if (rc == -EFAULT)
411 exception = 1;
412 break;
413 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100414 default:
415 BUG();
416 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100417 if (exception) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200418 printk("kvm: The guest lowcore is not mapped during interrupt "
419 "delivery, killing userspace\n");
420 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100421 }
422}
423
424static int __try_deliver_ckc_interrupt(struct kvm_vcpu *vcpu)
425{
426 int rc, exception = 0;
427
428 if (psw_extint_disabled(vcpu))
429 return 0;
430 if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
431 return 0;
432 rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x1004);
433 if (rc == -EFAULT)
434 exception = 1;
435 rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
436 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
437 if (rc == -EFAULT)
438 exception = 1;
439 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
440 __LC_EXT_NEW_PSW, sizeof(psw_t));
441 if (rc == -EFAULT)
442 exception = 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100443 if (exception) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200444 printk("kvm: The guest lowcore is not mapped during interrupt "
445 "delivery, killing userspace\n");
446 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100447 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100448 return 1;
449}
450
Gleb Natapova1b37102009-07-09 15:33:52 +0300451static int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100452{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200453 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
454 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
455 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100456 int rc = 0;
457
458 if (atomic_read(&li->active)) {
459 spin_lock_bh(&li->lock);
460 list_for_each_entry(inti, &li->list, list)
461 if (__interrupt_is_deliverable(vcpu, inti)) {
462 rc = 1;
463 break;
464 }
465 spin_unlock_bh(&li->lock);
466 }
467
468 if ((!rc) && atomic_read(&fi->active)) {
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200469 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100470 list_for_each_entry(inti, &fi->list, list)
471 if (__interrupt_is_deliverable(vcpu, inti)) {
472 rc = 1;
473 break;
474 }
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200475 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100476 }
477
478 if ((!rc) && (vcpu->arch.sie_block->ckc <
479 get_clock() + vcpu->arch.sie_block->epoch)) {
480 if ((!psw_extint_disabled(vcpu)) &&
481 (vcpu->arch.sie_block->gcr[0] & 0x800ul))
482 rc = 1;
483 }
484
485 return rc;
486}
487
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300488int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
489{
490 return 0;
491}
492
Carsten Otteba5c1e92008-03-25 18:47:26 +0100493int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
494{
495 u64 now, sltime;
496 DECLARE_WAITQUEUE(wait, current);
497
498 vcpu->stat.exit_wait_state++;
499 if (kvm_cpu_has_interrupt(vcpu))
500 return 0;
501
Carsten Ottee52b2af2008-05-21 13:37:44 +0200502 __set_cpu_idle(vcpu);
503 spin_lock_bh(&vcpu->arch.local_int.lock);
504 vcpu->arch.local_int.timer_due = 0;
505 spin_unlock_bh(&vcpu->arch.local_int.lock);
506
Carsten Otteba5c1e92008-03-25 18:47:26 +0100507 if (psw_interrupts_disabled(vcpu)) {
508 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
509 __unset_cpu_idle(vcpu);
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100510 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +0100511 }
512
513 if (psw_extint_disabled(vcpu) ||
514 (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))) {
515 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
516 goto no_timer;
517 }
518
519 now = get_clock() + vcpu->arch.sie_block->epoch;
520 if (vcpu->arch.sie_block->ckc < now) {
521 __unset_cpu_idle(vcpu);
522 return 0;
523 }
524
Christian Borntraegerca872302009-05-12 17:21:49 +0200525 sltime = ((vcpu->arch.sie_block->ckc - now)*125)>>9;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100526
Christian Borntraegerca872302009-05-12 17:21:49 +0200527 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
528 VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100529no_timer:
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200530 spin_lock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100531 spin_lock_bh(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100532 add_wait_queue(&vcpu->arch.local_int.wq, &wait);
533 while (list_empty(&vcpu->arch.local_int.list) &&
534 list_empty(&vcpu->arch.local_int.float_int->list) &&
535 (!vcpu->arch.local_int.timer_due) &&
536 !signal_pending(current)) {
537 set_current_state(TASK_INTERRUPTIBLE);
538 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200539 spin_unlock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100540 schedule();
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200541 spin_lock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100542 spin_lock_bh(&vcpu->arch.local_int.lock);
543 }
544 __unset_cpu_idle(vcpu);
545 __set_current_state(TASK_RUNNING);
Christian Borntraegerd3bc2f92009-07-16 17:17:37 +0200546 remove_wait_queue(&vcpu->arch.local_int.wq, &wait);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100547 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200548 spin_unlock(&vcpu->arch.local_int.float_int->lock);
Christian Borntraegerca872302009-05-12 17:21:49 +0200549 hrtimer_try_to_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100550 return 0;
551}
552
Christian Borntraegerca872302009-05-12 17:21:49 +0200553void kvm_s390_tasklet(unsigned long parm)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100554{
Christian Borntraegerca872302009-05-12 17:21:49 +0200555 struct kvm_vcpu *vcpu = (struct kvm_vcpu *) parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100556
Christian Borntraegerca872302009-05-12 17:21:49 +0200557 spin_lock(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100558 vcpu->arch.local_int.timer_due = 1;
559 if (waitqueue_active(&vcpu->arch.local_int.wq))
560 wake_up_interruptible(&vcpu->arch.local_int.wq);
Christian Borntraegerca872302009-05-12 17:21:49 +0200561 spin_unlock(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100562}
563
Christian Borntraegerca872302009-05-12 17:21:49 +0200564/*
565 * low level hrtimer wake routine. Because this runs in hardirq context
566 * we schedule a tasklet to do the real work.
567 */
568enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
569{
570 struct kvm_vcpu *vcpu;
571
572 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
573 tasklet_schedule(&vcpu->arch.tasklet);
574
575 return HRTIMER_NORESTART;
576}
Carsten Otteba5c1e92008-03-25 18:47:26 +0100577
578void kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
579{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200580 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
581 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
582 struct kvm_s390_interrupt_info *n, *inti = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100583 int deliver;
584
585 __reset_intercept_indicators(vcpu);
586 if (atomic_read(&li->active)) {
587 do {
588 deliver = 0;
589 spin_lock_bh(&li->lock);
590 list_for_each_entry_safe(inti, n, &li->list, list) {
591 if (__interrupt_is_deliverable(vcpu, inti)) {
592 list_del(&inti->list);
593 deliver = 1;
594 break;
595 }
596 __set_intercept_indicator(vcpu, inti);
597 }
598 if (list_empty(&li->list))
599 atomic_set(&li->active, 0);
600 spin_unlock_bh(&li->lock);
601 if (deliver) {
602 __do_deliver_interrupt(vcpu, inti);
603 kfree(inti);
604 }
605 } while (deliver);
606 }
607
608 if ((vcpu->arch.sie_block->ckc <
609 get_clock() + vcpu->arch.sie_block->epoch))
610 __try_deliver_ckc_interrupt(vcpu);
611
612 if (atomic_read(&fi->active)) {
613 do {
614 deliver = 0;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200615 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100616 list_for_each_entry_safe(inti, n, &fi->list, list) {
617 if (__interrupt_is_deliverable(vcpu, inti)) {
618 list_del(&inti->list);
619 deliver = 1;
620 break;
621 }
622 __set_intercept_indicator(vcpu, inti);
623 }
624 if (list_empty(&fi->list))
625 atomic_set(&fi->active, 0);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200626 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100627 if (deliver) {
628 __do_deliver_interrupt(vcpu, inti);
629 kfree(inti);
630 }
631 } while (deliver);
632 }
633}
634
Cornelia Huck48a3e952012-12-20 15:32:09 +0100635void kvm_s390_deliver_pending_machine_checks(struct kvm_vcpu *vcpu)
636{
637 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
638 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
639 struct kvm_s390_interrupt_info *n, *inti = NULL;
640 int deliver;
641
642 __reset_intercept_indicators(vcpu);
643 if (atomic_read(&li->active)) {
644 do {
645 deliver = 0;
646 spin_lock_bh(&li->lock);
647 list_for_each_entry_safe(inti, n, &li->list, list) {
648 if ((inti->type == KVM_S390_MCHK) &&
649 __interrupt_is_deliverable(vcpu, inti)) {
650 list_del(&inti->list);
651 deliver = 1;
652 break;
653 }
654 __set_intercept_indicator(vcpu, inti);
655 }
656 if (list_empty(&li->list))
657 atomic_set(&li->active, 0);
658 spin_unlock_bh(&li->lock);
659 if (deliver) {
660 __do_deliver_interrupt(vcpu, inti);
661 kfree(inti);
662 }
663 } while (deliver);
664 }
665
666 if (atomic_read(&fi->active)) {
667 do {
668 deliver = 0;
669 spin_lock(&fi->lock);
670 list_for_each_entry_safe(inti, n, &fi->list, list) {
671 if ((inti->type == KVM_S390_MCHK) &&
672 __interrupt_is_deliverable(vcpu, inti)) {
673 list_del(&inti->list);
674 deliver = 1;
675 break;
676 }
677 __set_intercept_indicator(vcpu, inti);
678 }
679 if (list_empty(&fi->list))
680 atomic_set(&fi->active, 0);
681 spin_unlock(&fi->lock);
682 if (deliver) {
683 __do_deliver_interrupt(vcpu, inti);
684 kfree(inti);
685 }
686 } while (deliver);
687 }
688}
689
Carsten Otteba5c1e92008-03-25 18:47:26 +0100690int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
691{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200692 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
693 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100694
695 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
696 if (!inti)
697 return -ENOMEM;
698
Joe Perchesa419aef2009-08-18 11:18:35 -0700699 inti->type = KVM_S390_PROGRAM_INT;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100700 inti->pgm.code = code;
701
702 VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code);
Cornelia Huckade38c32012-07-23 17:20:30 +0200703 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, inti->type, code, 0, 1);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100704 spin_lock_bh(&li->lock);
705 list_add(&inti->list, &li->list);
706 atomic_set(&li->active, 1);
707 BUG_ON(waitqueue_active(&li->wq));
708 spin_unlock_bh(&li->lock);
709 return 0;
710}
711
712int kvm_s390_inject_vm(struct kvm *kvm,
713 struct kvm_s390_interrupt *s390int)
714{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200715 struct kvm_s390_local_interrupt *li;
716 struct kvm_s390_float_interrupt *fi;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100717 struct kvm_s390_interrupt_info *inti, *iter;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100718 int sigcpu;
719
720 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
721 if (!inti)
722 return -ENOMEM;
723
724 switch (s390int->type) {
725 case KVM_S390_INT_VIRTIO:
Heiko Carstens33e19112009-01-09 12:14:56 +0100726 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
Carsten Otteba5c1e92008-03-25 18:47:26 +0100727 s390int->parm, s390int->parm64);
728 inti->type = s390int->type;
729 inti->ext.ext_params = s390int->parm;
730 inti->ext.ext_params2 = s390int->parm64;
731 break;
732 case KVM_S390_INT_SERVICE:
733 VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
734 inti->type = s390int->type;
735 inti->ext.ext_params = s390int->parm;
736 break;
737 case KVM_S390_PROGRAM_INT:
738 case KVM_S390_SIGP_STOP:
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200739 case KVM_S390_INT_EXTERNAL_CALL:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100740 case KVM_S390_INT_EMERGENCY:
Cornelia Huckd8346b72012-12-20 15:32:08 +0100741 kfree(inti);
742 return -EINVAL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100743 case KVM_S390_MCHK:
744 VM_EVENT(kvm, 5, "inject: machine check parm64:%llx",
745 s390int->parm64);
746 inti->type = s390int->type;
747 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
748 inti->mchk.mcic = s390int->parm64;
749 break;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100750 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
751 if (s390int->type & IOINT_AI_MASK)
752 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
753 else
754 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
755 s390int->type & IOINT_CSSID_MASK,
756 s390int->type & IOINT_SSID_MASK,
757 s390int->type & IOINT_SCHID_MASK);
758 inti->type = s390int->type;
759 inti->io.subchannel_id = s390int->parm >> 16;
760 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
761 inti->io.io_int_parm = s390int->parm64 >> 32;
762 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
763 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100764 default:
765 kfree(inti);
766 return -EINVAL;
767 }
Cornelia Huckade38c32012-07-23 17:20:30 +0200768 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
769 2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100770
771 mutex_lock(&kvm->lock);
772 fi = &kvm->arch.float_int;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200773 spin_lock(&fi->lock);
Cornelia Huckd8346b72012-12-20 15:32:08 +0100774 if (!is_ioint(inti->type))
775 list_add_tail(&inti->list, &fi->list);
776 else {
777 /* Keep I/O interrupts sorted in isc order. */
778 list_for_each_entry(iter, &fi->list, list) {
779 if (!is_ioint(iter->type))
780 continue;
781 if (iter->io.io_int_word <= inti->io.io_int_word)
782 continue;
783 break;
784 }
785 list_add_tail(&inti->list, &iter->list);
786 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100787 atomic_set(&fi->active, 1);
788 sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS);
789 if (sigcpu == KVM_MAX_VCPUS) {
790 do {
791 sigcpu = fi->next_rr_cpu++;
792 if (sigcpu == KVM_MAX_VCPUS)
793 sigcpu = fi->next_rr_cpu = 0;
794 } while (fi->local_int[sigcpu] == NULL);
795 }
796 li = fi->local_int[sigcpu];
797 spin_lock_bh(&li->lock);
798 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
799 if (waitqueue_active(&li->wq))
800 wake_up_interruptible(&li->wq);
801 spin_unlock_bh(&li->lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200802 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100803 mutex_unlock(&kvm->lock);
804 return 0;
805}
806
807int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
808 struct kvm_s390_interrupt *s390int)
809{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200810 struct kvm_s390_local_interrupt *li;
811 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100812
813 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
814 if (!inti)
815 return -ENOMEM;
816
817 switch (s390int->type) {
818 case KVM_S390_PROGRAM_INT:
819 if (s390int->parm & 0xffff0000) {
820 kfree(inti);
821 return -EINVAL;
822 }
823 inti->type = s390int->type;
824 inti->pgm.code = s390int->parm;
825 VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
826 s390int->parm);
827 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +0100828 case KVM_S390_SIGP_SET_PREFIX:
829 inti->prefix.address = s390int->parm;
830 inti->type = s390int->type;
831 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
832 s390int->parm);
833 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100834 case KVM_S390_SIGP_STOP:
835 case KVM_S390_RESTART:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100836 VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type);
837 inti->type = s390int->type;
838 break;
Jason J. Herne82a12732012-10-02 16:25:36 +0200839 case KVM_S390_INT_EXTERNAL_CALL:
840 if (s390int->parm & 0xffff0000) {
841 kfree(inti);
842 return -EINVAL;
843 }
844 VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u",
845 s390int->parm);
846 inti->type = s390int->type;
847 inti->extcall.code = s390int->parm;
848 break;
849 case KVM_S390_INT_EMERGENCY:
850 if (s390int->parm & 0xffff0000) {
851 kfree(inti);
852 return -EINVAL;
853 }
854 VCPU_EVENT(vcpu, 3, "inject: emergency %u\n", s390int->parm);
855 inti->type = s390int->type;
856 inti->emerg.code = s390int->parm;
857 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100858 case KVM_S390_MCHK:
859 VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx",
860 s390int->parm64);
861 inti->type = s390int->type;
862 inti->mchk.mcic = s390int->parm64;
863 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100864 case KVM_S390_INT_VIRTIO:
865 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +0100866 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100867 default:
868 kfree(inti);
869 return -EINVAL;
870 }
Cornelia Huckade38c32012-07-23 17:20:30 +0200871 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, s390int->type, s390int->parm,
872 s390int->parm64, 2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100873
874 mutex_lock(&vcpu->kvm->lock);
875 li = &vcpu->arch.local_int;
876 spin_lock_bh(&li->lock);
877 if (inti->type == KVM_S390_PROGRAM_INT)
878 list_add(&inti->list, &li->list);
879 else
880 list_add_tail(&inti->list, &li->list);
881 atomic_set(&li->active, 1);
882 if (inti->type == KVM_S390_SIGP_STOP)
883 li->action_bits |= ACTION_STOP_ON_STOP;
884 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
885 if (waitqueue_active(&li->wq))
886 wake_up_interruptible(&vcpu->arch.local_int.wq);
887 spin_unlock_bh(&li->lock);
888 mutex_unlock(&vcpu->kvm->lock);
889 return 0;
890}