blob: 9a128357fd1569a3256750d197f476d0af22598f [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
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100712struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
713 u64 cr6, u64 schid)
714{
715 struct kvm_s390_float_interrupt *fi;
716 struct kvm_s390_interrupt_info *inti, *iter;
717
718 if ((!schid && !cr6) || (schid && cr6))
719 return NULL;
720 mutex_lock(&kvm->lock);
721 fi = &kvm->arch.float_int;
722 spin_lock(&fi->lock);
723 inti = NULL;
724 list_for_each_entry(iter, &fi->list, list) {
725 if (!is_ioint(iter->type))
726 continue;
727 if (cr6 && ((cr6 & iter->io.io_int_word) == 0))
728 continue;
729 if (schid) {
730 if (((schid & 0x00000000ffff0000) >> 16) !=
731 iter->io.subchannel_id)
732 continue;
733 if ((schid & 0x000000000000ffff) !=
734 iter->io.subchannel_nr)
735 continue;
736 }
737 inti = iter;
738 break;
739 }
740 if (inti)
741 list_del_init(&inti->list);
742 if (list_empty(&fi->list))
743 atomic_set(&fi->active, 0);
744 spin_unlock(&fi->lock);
745 mutex_unlock(&kvm->lock);
746 return inti;
747}
748
Carsten Otteba5c1e92008-03-25 18:47:26 +0100749int kvm_s390_inject_vm(struct kvm *kvm,
750 struct kvm_s390_interrupt *s390int)
751{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200752 struct kvm_s390_local_interrupt *li;
753 struct kvm_s390_float_interrupt *fi;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100754 struct kvm_s390_interrupt_info *inti, *iter;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100755 int sigcpu;
756
757 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
758 if (!inti)
759 return -ENOMEM;
760
761 switch (s390int->type) {
762 case KVM_S390_INT_VIRTIO:
Heiko Carstens33e19112009-01-09 12:14:56 +0100763 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
Carsten Otteba5c1e92008-03-25 18:47:26 +0100764 s390int->parm, s390int->parm64);
765 inti->type = s390int->type;
766 inti->ext.ext_params = s390int->parm;
767 inti->ext.ext_params2 = s390int->parm64;
768 break;
769 case KVM_S390_INT_SERVICE:
770 VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
771 inti->type = s390int->type;
772 inti->ext.ext_params = s390int->parm;
773 break;
774 case KVM_S390_PROGRAM_INT:
775 case KVM_S390_SIGP_STOP:
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200776 case KVM_S390_INT_EXTERNAL_CALL:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100777 case KVM_S390_INT_EMERGENCY:
Cornelia Huckd8346b72012-12-20 15:32:08 +0100778 kfree(inti);
779 return -EINVAL;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100780 case KVM_S390_MCHK:
781 VM_EVENT(kvm, 5, "inject: machine check parm64:%llx",
782 s390int->parm64);
783 inti->type = s390int->type;
784 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
785 inti->mchk.mcic = s390int->parm64;
786 break;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100787 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
788 if (s390int->type & IOINT_AI_MASK)
789 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
790 else
791 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
792 s390int->type & IOINT_CSSID_MASK,
793 s390int->type & IOINT_SSID_MASK,
794 s390int->type & IOINT_SCHID_MASK);
795 inti->type = s390int->type;
796 inti->io.subchannel_id = s390int->parm >> 16;
797 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
798 inti->io.io_int_parm = s390int->parm64 >> 32;
799 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
800 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100801 default:
802 kfree(inti);
803 return -EINVAL;
804 }
Cornelia Huckade38c32012-07-23 17:20:30 +0200805 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
806 2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100807
808 mutex_lock(&kvm->lock);
809 fi = &kvm->arch.float_int;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200810 spin_lock(&fi->lock);
Cornelia Huckd8346b72012-12-20 15:32:08 +0100811 if (!is_ioint(inti->type))
812 list_add_tail(&inti->list, &fi->list);
813 else {
814 /* Keep I/O interrupts sorted in isc order. */
815 list_for_each_entry(iter, &fi->list, list) {
816 if (!is_ioint(iter->type))
817 continue;
818 if (iter->io.io_int_word <= inti->io.io_int_word)
819 continue;
820 break;
821 }
822 list_add_tail(&inti->list, &iter->list);
823 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100824 atomic_set(&fi->active, 1);
825 sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS);
826 if (sigcpu == KVM_MAX_VCPUS) {
827 do {
828 sigcpu = fi->next_rr_cpu++;
829 if (sigcpu == KVM_MAX_VCPUS)
830 sigcpu = fi->next_rr_cpu = 0;
831 } while (fi->local_int[sigcpu] == NULL);
832 }
833 li = fi->local_int[sigcpu];
834 spin_lock_bh(&li->lock);
835 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
836 if (waitqueue_active(&li->wq))
837 wake_up_interruptible(&li->wq);
838 spin_unlock_bh(&li->lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200839 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100840 mutex_unlock(&kvm->lock);
841 return 0;
842}
843
844int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
845 struct kvm_s390_interrupt *s390int)
846{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200847 struct kvm_s390_local_interrupt *li;
848 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100849
850 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
851 if (!inti)
852 return -ENOMEM;
853
854 switch (s390int->type) {
855 case KVM_S390_PROGRAM_INT:
856 if (s390int->parm & 0xffff0000) {
857 kfree(inti);
858 return -EINVAL;
859 }
860 inti->type = s390int->type;
861 inti->pgm.code = s390int->parm;
862 VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
863 s390int->parm);
864 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +0100865 case KVM_S390_SIGP_SET_PREFIX:
866 inti->prefix.address = s390int->parm;
867 inti->type = s390int->type;
868 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
869 s390int->parm);
870 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100871 case KVM_S390_SIGP_STOP:
872 case KVM_S390_RESTART:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100873 VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type);
874 inti->type = s390int->type;
875 break;
Jason J. Herne82a12732012-10-02 16:25:36 +0200876 case KVM_S390_INT_EXTERNAL_CALL:
877 if (s390int->parm & 0xffff0000) {
878 kfree(inti);
879 return -EINVAL;
880 }
881 VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u",
882 s390int->parm);
883 inti->type = s390int->type;
884 inti->extcall.code = s390int->parm;
885 break;
886 case KVM_S390_INT_EMERGENCY:
887 if (s390int->parm & 0xffff0000) {
888 kfree(inti);
889 return -EINVAL;
890 }
891 VCPU_EVENT(vcpu, 3, "inject: emergency %u\n", s390int->parm);
892 inti->type = s390int->type;
893 inti->emerg.code = s390int->parm;
894 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100895 case KVM_S390_MCHK:
896 VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx",
897 s390int->parm64);
898 inti->type = s390int->type;
899 inti->mchk.mcic = s390int->parm64;
900 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100901 case KVM_S390_INT_VIRTIO:
902 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +0100903 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100904 default:
905 kfree(inti);
906 return -EINVAL;
907 }
Cornelia Huckade38c32012-07-23 17:20:30 +0200908 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, s390int->type, s390int->parm,
909 s390int->parm64, 2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100910
911 mutex_lock(&vcpu->kvm->lock);
912 li = &vcpu->arch.local_int;
913 spin_lock_bh(&li->lock);
914 if (inti->type == KVM_S390_PROGRAM_INT)
915 list_add(&inti->list, &li->list);
916 else
917 list_add_tail(&inti->list, &li->list);
918 atomic_set(&li->active, 1);
919 if (inti->type == KVM_S390_SIGP_STOP)
920 li->action_bits |= ACTION_STOP_ON_STOP;
921 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
922 if (waitqueue_active(&li->wq))
923 wake_up_interruptible(&vcpu->arch.local_int.wq);
924 spin_unlock_bh(&li->lock);
925 mutex_unlock(&vcpu->kvm->lock);
926 return 0;
927}