blob: 52cdf20906ab76d79224bec1b49a9125373507c9 [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
Carsten Otteba5c1e92008-03-25 18:47:26 +010044static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
45{
46 if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER) ||
47 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO) ||
48 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT))
49 return 0;
50 return 1;
51}
52
53static int __interrupt_is_deliverable(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +020054 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +010055{
56 switch (inti->type) {
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020057 case KVM_S390_INT_EXTERNAL_CALL:
58 if (psw_extint_disabled(vcpu))
59 return 0;
60 if (vcpu->arch.sie_block->gcr[0] & 0x2000ul)
61 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +010062 case KVM_S390_INT_EMERGENCY:
63 if (psw_extint_disabled(vcpu))
64 return 0;
65 if (vcpu->arch.sie_block->gcr[0] & 0x4000ul)
66 return 1;
67 return 0;
68 case KVM_S390_INT_SERVICE:
69 if (psw_extint_disabled(vcpu))
70 return 0;
71 if (vcpu->arch.sie_block->gcr[0] & 0x200ul)
72 return 1;
73 return 0;
74 case KVM_S390_INT_VIRTIO:
75 if (psw_extint_disabled(vcpu))
76 return 0;
77 if (vcpu->arch.sie_block->gcr[0] & 0x200ul)
78 return 1;
79 return 0;
80 case KVM_S390_PROGRAM_INT:
81 case KVM_S390_SIGP_STOP:
82 case KVM_S390_SIGP_SET_PREFIX:
83 case KVM_S390_RESTART:
84 return 1;
Cornelia Huckd8346b72012-12-20 15:32:08 +010085 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
86 if (psw_ioint_disabled(vcpu))
87 return 0;
88 if (vcpu->arch.sie_block->gcr[6] & inti->io.io_int_word)
89 return 1;
90 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +010091 default:
Cornelia Huckd8346b72012-12-20 15:32:08 +010092 printk(KERN_WARNING "illegal interrupt type %llx\n",
93 inti->type);
Carsten Otteba5c1e92008-03-25 18:47:26 +010094 BUG();
95 }
96 return 0;
97}
98
99static void __set_cpu_idle(struct kvm_vcpu *vcpu)
100{
101 BUG_ON(vcpu->vcpu_id > KVM_MAX_VCPUS - 1);
102 atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
103 set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
104}
105
106static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
107{
108 BUG_ON(vcpu->vcpu_id > KVM_MAX_VCPUS - 1);
109 atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
110 clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
111}
112
113static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
114{
115 atomic_clear_mask(CPUSTAT_ECALL_PEND |
116 CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
117 &vcpu->arch.sie_block->cpuflags);
118 vcpu->arch.sie_block->lctl = 0x0000;
119}
120
121static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
122{
123 atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags);
124}
125
126static void __set_intercept_indicator(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200127 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100128{
129 switch (inti->type) {
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200130 case KVM_S390_INT_EXTERNAL_CALL:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100131 case KVM_S390_INT_EMERGENCY:
132 case KVM_S390_INT_SERVICE:
133 case KVM_S390_INT_VIRTIO:
134 if (psw_extint_disabled(vcpu))
135 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
136 else
137 vcpu->arch.sie_block->lctl |= LCTL_CR0;
138 break;
139 case KVM_S390_SIGP_STOP:
140 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
141 break;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100142 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
143 if (psw_ioint_disabled(vcpu))
144 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
145 else
146 vcpu->arch.sie_block->lctl |= LCTL_CR6;
147 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100148 default:
149 BUG();
150 }
151}
152
153static void __do_deliver_interrupt(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200154 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100155{
156 const unsigned short table[] = { 2, 4, 4, 6 };
157 int rc, exception = 0;
158
159 switch (inti->type) {
160 case KVM_S390_INT_EMERGENCY:
161 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg");
162 vcpu->stat.deliver_emergency_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200163 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
164 inti->emerg.code, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100165 rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x1201);
166 if (rc == -EFAULT)
167 exception = 1;
168
Martin Schwidefsky7e180bd2012-03-11 11:59:25 -0400169 rc = put_guest_u16(vcpu, __LC_EXT_CPU_ADDR, inti->emerg.code);
Christian Ehrhardt8bb3a2e2011-07-24 10:48:31 +0200170 if (rc == -EFAULT)
171 exception = 1;
172
Carsten Otteba5c1e92008-03-25 18:47:26 +0100173 rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
174 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
175 if (rc == -EFAULT)
176 exception = 1;
177
178 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
179 __LC_EXT_NEW_PSW, sizeof(psw_t));
180 if (rc == -EFAULT)
181 exception = 1;
182 break;
183
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200184 case KVM_S390_INT_EXTERNAL_CALL:
185 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call");
186 vcpu->stat.deliver_external_call++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200187 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
188 inti->extcall.code, 0);
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200189 rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x1202);
190 if (rc == -EFAULT)
191 exception = 1;
192
Martin Schwidefsky7e180bd2012-03-11 11:59:25 -0400193 rc = put_guest_u16(vcpu, __LC_EXT_CPU_ADDR, inti->extcall.code);
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200194 if (rc == -EFAULT)
195 exception = 1;
196
197 rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
198 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
199 if (rc == -EFAULT)
200 exception = 1;
201
202 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
203 __LC_EXT_NEW_PSW, sizeof(psw_t));
204 if (rc == -EFAULT)
205 exception = 1;
206 break;
207
Carsten Otteba5c1e92008-03-25 18:47:26 +0100208 case KVM_S390_INT_SERVICE:
209 VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x",
210 inti->ext.ext_params);
211 vcpu->stat.deliver_service_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200212 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
213 inti->ext.ext_params, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100214 rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x2401);
215 if (rc == -EFAULT)
216 exception = 1;
217
218 rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
219 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
220 if (rc == -EFAULT)
221 exception = 1;
222
223 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
224 __LC_EXT_NEW_PSW, sizeof(psw_t));
225 if (rc == -EFAULT)
226 exception = 1;
227
228 rc = put_guest_u32(vcpu, __LC_EXT_PARAMS, inti->ext.ext_params);
229 if (rc == -EFAULT)
230 exception = 1;
231 break;
232
233 case KVM_S390_INT_VIRTIO:
Heiko Carstens33e19112009-01-09 12:14:56 +0100234 VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx",
Carsten Otteba5c1e92008-03-25 18:47:26 +0100235 inti->ext.ext_params, inti->ext.ext_params2);
236 vcpu->stat.deliver_virtio_interrupt++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200237 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
238 inti->ext.ext_params,
239 inti->ext.ext_params2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100240 rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x2603);
241 if (rc == -EFAULT)
242 exception = 1;
243
Martin Schwidefsky7e180bd2012-03-11 11:59:25 -0400244 rc = put_guest_u16(vcpu, __LC_EXT_CPU_ADDR, 0x0d00);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100245 if (rc == -EFAULT)
246 exception = 1;
247
248 rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
249 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
250 if (rc == -EFAULT)
251 exception = 1;
252
253 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
254 __LC_EXT_NEW_PSW, sizeof(psw_t));
255 if (rc == -EFAULT)
256 exception = 1;
257
258 rc = put_guest_u32(vcpu, __LC_EXT_PARAMS, inti->ext.ext_params);
259 if (rc == -EFAULT)
260 exception = 1;
261
Heiko Carstenscbb870c2010-02-26 22:37:43 +0100262 rc = put_guest_u64(vcpu, __LC_EXT_PARAMS2,
263 inti->ext.ext_params2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100264 if (rc == -EFAULT)
265 exception = 1;
266 break;
267
268 case KVM_S390_SIGP_STOP:
269 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu stop");
270 vcpu->stat.deliver_stop_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200271 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
272 0, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100273 __set_intercept_indicator(vcpu, inti);
274 break;
275
276 case KVM_S390_SIGP_SET_PREFIX:
277 VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x",
278 inti->prefix.address);
279 vcpu->stat.deliver_prefix_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200280 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
281 inti->prefix.address, 0);
Christian Borntraeger8d26cf72012-01-11 11:19:32 +0100282 kvm_s390_set_prefix(vcpu, inti->prefix.address);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100283 break;
284
285 case KVM_S390_RESTART:
286 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart");
287 vcpu->stat.deliver_restart_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200288 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
289 0, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100290 rc = copy_to_guest(vcpu, offsetof(struct _lowcore,
291 restart_old_psw), &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
292 if (rc == -EFAULT)
293 exception = 1;
294
295 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
296 offsetof(struct _lowcore, restart_psw), sizeof(psw_t));
297 if (rc == -EFAULT)
298 exception = 1;
Cornelia Huck9e6dabe2011-11-17 11:00:41 +0100299 atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100300 break;
301
302 case KVM_S390_PROGRAM_INT:
303 VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x",
304 inti->pgm.code,
305 table[vcpu->arch.sie_block->ipa >> 14]);
306 vcpu->stat.deliver_program_int++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200307 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
308 inti->pgm.code, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100309 rc = put_guest_u16(vcpu, __LC_PGM_INT_CODE, inti->pgm.code);
310 if (rc == -EFAULT)
311 exception = 1;
312
313 rc = put_guest_u16(vcpu, __LC_PGM_ILC,
314 table[vcpu->arch.sie_block->ipa >> 14]);
315 if (rc == -EFAULT)
316 exception = 1;
317
318 rc = copy_to_guest(vcpu, __LC_PGM_OLD_PSW,
319 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
320 if (rc == -EFAULT)
321 exception = 1;
322
323 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
324 __LC_PGM_NEW_PSW, sizeof(psw_t));
325 if (rc == -EFAULT)
326 exception = 1;
327 break;
328
Cornelia Huckd8346b72012-12-20 15:32:08 +0100329 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
330 {
331 __u32 param0 = ((__u32)inti->io.subchannel_id << 16) |
332 inti->io.subchannel_nr;
333 __u64 param1 = ((__u64)inti->io.io_int_parm << 32) |
334 inti->io.io_int_word;
335 VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type);
336 vcpu->stat.deliver_io_int++;
337 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
338 param0, param1);
339 rc = put_guest_u16(vcpu, __LC_SUBCHANNEL_ID,
340 inti->io.subchannel_id);
341 if (rc == -EFAULT)
342 exception = 1;
343
344 rc = put_guest_u16(vcpu, __LC_SUBCHANNEL_NR,
345 inti->io.subchannel_nr);
346 if (rc == -EFAULT)
347 exception = 1;
348
349 rc = put_guest_u32(vcpu, __LC_IO_INT_PARM,
350 inti->io.io_int_parm);
351 if (rc == -EFAULT)
352 exception = 1;
353
354 rc = put_guest_u32(vcpu, __LC_IO_INT_WORD,
355 inti->io.io_int_word);
356 if (rc == -EFAULT)
357 exception = 1;
358
359 rc = copy_to_guest(vcpu, __LC_IO_OLD_PSW,
360 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
361 if (rc == -EFAULT)
362 exception = 1;
363
364 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
365 __LC_IO_NEW_PSW, sizeof(psw_t));
366 if (rc == -EFAULT)
367 exception = 1;
368 break;
369 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100370 default:
371 BUG();
372 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100373 if (exception) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200374 printk("kvm: The guest lowcore is not mapped during interrupt "
375 "delivery, killing userspace\n");
376 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100377 }
378}
379
380static int __try_deliver_ckc_interrupt(struct kvm_vcpu *vcpu)
381{
382 int rc, exception = 0;
383
384 if (psw_extint_disabled(vcpu))
385 return 0;
386 if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
387 return 0;
388 rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x1004);
389 if (rc == -EFAULT)
390 exception = 1;
391 rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
392 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
393 if (rc == -EFAULT)
394 exception = 1;
395 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
396 __LC_EXT_NEW_PSW, sizeof(psw_t));
397 if (rc == -EFAULT)
398 exception = 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100399 if (exception) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200400 printk("kvm: The guest lowcore is not mapped during interrupt "
401 "delivery, killing userspace\n");
402 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100403 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100404 return 1;
405}
406
Gleb Natapova1b37102009-07-09 15:33:52 +0300407static int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100408{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200409 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
410 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
411 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100412 int rc = 0;
413
414 if (atomic_read(&li->active)) {
415 spin_lock_bh(&li->lock);
416 list_for_each_entry(inti, &li->list, list)
417 if (__interrupt_is_deliverable(vcpu, inti)) {
418 rc = 1;
419 break;
420 }
421 spin_unlock_bh(&li->lock);
422 }
423
424 if ((!rc) && atomic_read(&fi->active)) {
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200425 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100426 list_for_each_entry(inti, &fi->list, list)
427 if (__interrupt_is_deliverable(vcpu, inti)) {
428 rc = 1;
429 break;
430 }
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200431 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100432 }
433
434 if ((!rc) && (vcpu->arch.sie_block->ckc <
435 get_clock() + vcpu->arch.sie_block->epoch)) {
436 if ((!psw_extint_disabled(vcpu)) &&
437 (vcpu->arch.sie_block->gcr[0] & 0x800ul))
438 rc = 1;
439 }
440
441 return rc;
442}
443
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300444int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
445{
446 return 0;
447}
448
Carsten Otteba5c1e92008-03-25 18:47:26 +0100449int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
450{
451 u64 now, sltime;
452 DECLARE_WAITQUEUE(wait, current);
453
454 vcpu->stat.exit_wait_state++;
455 if (kvm_cpu_has_interrupt(vcpu))
456 return 0;
457
Carsten Ottee52b2af2008-05-21 13:37:44 +0200458 __set_cpu_idle(vcpu);
459 spin_lock_bh(&vcpu->arch.local_int.lock);
460 vcpu->arch.local_int.timer_due = 0;
461 spin_unlock_bh(&vcpu->arch.local_int.lock);
462
Carsten Otteba5c1e92008-03-25 18:47:26 +0100463 if (psw_interrupts_disabled(vcpu)) {
464 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
465 __unset_cpu_idle(vcpu);
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100466 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +0100467 }
468
469 if (psw_extint_disabled(vcpu) ||
470 (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))) {
471 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
472 goto no_timer;
473 }
474
475 now = get_clock() + vcpu->arch.sie_block->epoch;
476 if (vcpu->arch.sie_block->ckc < now) {
477 __unset_cpu_idle(vcpu);
478 return 0;
479 }
480
Christian Borntraegerca872302009-05-12 17:21:49 +0200481 sltime = ((vcpu->arch.sie_block->ckc - now)*125)>>9;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100482
Christian Borntraegerca872302009-05-12 17:21:49 +0200483 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
484 VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100485no_timer:
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200486 spin_lock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100487 spin_lock_bh(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100488 add_wait_queue(&vcpu->arch.local_int.wq, &wait);
489 while (list_empty(&vcpu->arch.local_int.list) &&
490 list_empty(&vcpu->arch.local_int.float_int->list) &&
491 (!vcpu->arch.local_int.timer_due) &&
492 !signal_pending(current)) {
493 set_current_state(TASK_INTERRUPTIBLE);
494 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200495 spin_unlock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100496 schedule();
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200497 spin_lock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100498 spin_lock_bh(&vcpu->arch.local_int.lock);
499 }
500 __unset_cpu_idle(vcpu);
501 __set_current_state(TASK_RUNNING);
Christian Borntraegerd3bc2f92009-07-16 17:17:37 +0200502 remove_wait_queue(&vcpu->arch.local_int.wq, &wait);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100503 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200504 spin_unlock(&vcpu->arch.local_int.float_int->lock);
Christian Borntraegerca872302009-05-12 17:21:49 +0200505 hrtimer_try_to_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100506 return 0;
507}
508
Christian Borntraegerca872302009-05-12 17:21:49 +0200509void kvm_s390_tasklet(unsigned long parm)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100510{
Christian Borntraegerca872302009-05-12 17:21:49 +0200511 struct kvm_vcpu *vcpu = (struct kvm_vcpu *) parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100512
Christian Borntraegerca872302009-05-12 17:21:49 +0200513 spin_lock(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100514 vcpu->arch.local_int.timer_due = 1;
515 if (waitqueue_active(&vcpu->arch.local_int.wq))
516 wake_up_interruptible(&vcpu->arch.local_int.wq);
Christian Borntraegerca872302009-05-12 17:21:49 +0200517 spin_unlock(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100518}
519
Christian Borntraegerca872302009-05-12 17:21:49 +0200520/*
521 * low level hrtimer wake routine. Because this runs in hardirq context
522 * we schedule a tasklet to do the real work.
523 */
524enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
525{
526 struct kvm_vcpu *vcpu;
527
528 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
529 tasklet_schedule(&vcpu->arch.tasklet);
530
531 return HRTIMER_NORESTART;
532}
Carsten Otteba5c1e92008-03-25 18:47:26 +0100533
534void kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
535{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200536 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
537 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
538 struct kvm_s390_interrupt_info *n, *inti = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100539 int deliver;
540
541 __reset_intercept_indicators(vcpu);
542 if (atomic_read(&li->active)) {
543 do {
544 deliver = 0;
545 spin_lock_bh(&li->lock);
546 list_for_each_entry_safe(inti, n, &li->list, list) {
547 if (__interrupt_is_deliverable(vcpu, inti)) {
548 list_del(&inti->list);
549 deliver = 1;
550 break;
551 }
552 __set_intercept_indicator(vcpu, inti);
553 }
554 if (list_empty(&li->list))
555 atomic_set(&li->active, 0);
556 spin_unlock_bh(&li->lock);
557 if (deliver) {
558 __do_deliver_interrupt(vcpu, inti);
559 kfree(inti);
560 }
561 } while (deliver);
562 }
563
564 if ((vcpu->arch.sie_block->ckc <
565 get_clock() + vcpu->arch.sie_block->epoch))
566 __try_deliver_ckc_interrupt(vcpu);
567
568 if (atomic_read(&fi->active)) {
569 do {
570 deliver = 0;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200571 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100572 list_for_each_entry_safe(inti, n, &fi->list, list) {
573 if (__interrupt_is_deliverable(vcpu, inti)) {
574 list_del(&inti->list);
575 deliver = 1;
576 break;
577 }
578 __set_intercept_indicator(vcpu, inti);
579 }
580 if (list_empty(&fi->list))
581 atomic_set(&fi->active, 0);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200582 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100583 if (deliver) {
584 __do_deliver_interrupt(vcpu, inti);
585 kfree(inti);
586 }
587 } while (deliver);
588 }
589}
590
591int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
592{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200593 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
594 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100595
596 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
597 if (!inti)
598 return -ENOMEM;
599
Joe Perchesa419aef2009-08-18 11:18:35 -0700600 inti->type = KVM_S390_PROGRAM_INT;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100601 inti->pgm.code = code;
602
603 VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code);
Cornelia Huckade38c32012-07-23 17:20:30 +0200604 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, inti->type, code, 0, 1);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100605 spin_lock_bh(&li->lock);
606 list_add(&inti->list, &li->list);
607 atomic_set(&li->active, 1);
608 BUG_ON(waitqueue_active(&li->wq));
609 spin_unlock_bh(&li->lock);
610 return 0;
611}
612
613int kvm_s390_inject_vm(struct kvm *kvm,
614 struct kvm_s390_interrupt *s390int)
615{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200616 struct kvm_s390_local_interrupt *li;
617 struct kvm_s390_float_interrupt *fi;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100618 struct kvm_s390_interrupt_info *inti, *iter;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100619 int sigcpu;
620
621 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
622 if (!inti)
623 return -ENOMEM;
624
625 switch (s390int->type) {
626 case KVM_S390_INT_VIRTIO:
Heiko Carstens33e19112009-01-09 12:14:56 +0100627 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
Carsten Otteba5c1e92008-03-25 18:47:26 +0100628 s390int->parm, s390int->parm64);
629 inti->type = s390int->type;
630 inti->ext.ext_params = s390int->parm;
631 inti->ext.ext_params2 = s390int->parm64;
632 break;
633 case KVM_S390_INT_SERVICE:
634 VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
635 inti->type = s390int->type;
636 inti->ext.ext_params = s390int->parm;
637 break;
638 case KVM_S390_PROGRAM_INT:
639 case KVM_S390_SIGP_STOP:
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200640 case KVM_S390_INT_EXTERNAL_CALL:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100641 case KVM_S390_INT_EMERGENCY:
Cornelia Huckd8346b72012-12-20 15:32:08 +0100642 kfree(inti);
643 return -EINVAL;
644 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
645 if (s390int->type & IOINT_AI_MASK)
646 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
647 else
648 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
649 s390int->type & IOINT_CSSID_MASK,
650 s390int->type & IOINT_SSID_MASK,
651 s390int->type & IOINT_SCHID_MASK);
652 inti->type = s390int->type;
653 inti->io.subchannel_id = s390int->parm >> 16;
654 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
655 inti->io.io_int_parm = s390int->parm64 >> 32;
656 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
657 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100658 default:
659 kfree(inti);
660 return -EINVAL;
661 }
Cornelia Huckade38c32012-07-23 17:20:30 +0200662 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
663 2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100664
665 mutex_lock(&kvm->lock);
666 fi = &kvm->arch.float_int;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200667 spin_lock(&fi->lock);
Cornelia Huckd8346b72012-12-20 15:32:08 +0100668 if (!is_ioint(inti->type))
669 list_add_tail(&inti->list, &fi->list);
670 else {
671 /* Keep I/O interrupts sorted in isc order. */
672 list_for_each_entry(iter, &fi->list, list) {
673 if (!is_ioint(iter->type))
674 continue;
675 if (iter->io.io_int_word <= inti->io.io_int_word)
676 continue;
677 break;
678 }
679 list_add_tail(&inti->list, &iter->list);
680 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100681 atomic_set(&fi->active, 1);
682 sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS);
683 if (sigcpu == KVM_MAX_VCPUS) {
684 do {
685 sigcpu = fi->next_rr_cpu++;
686 if (sigcpu == KVM_MAX_VCPUS)
687 sigcpu = fi->next_rr_cpu = 0;
688 } while (fi->local_int[sigcpu] == NULL);
689 }
690 li = fi->local_int[sigcpu];
691 spin_lock_bh(&li->lock);
692 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
693 if (waitqueue_active(&li->wq))
694 wake_up_interruptible(&li->wq);
695 spin_unlock_bh(&li->lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200696 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100697 mutex_unlock(&kvm->lock);
698 return 0;
699}
700
701int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
702 struct kvm_s390_interrupt *s390int)
703{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200704 struct kvm_s390_local_interrupt *li;
705 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100706
707 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
708 if (!inti)
709 return -ENOMEM;
710
711 switch (s390int->type) {
712 case KVM_S390_PROGRAM_INT:
713 if (s390int->parm & 0xffff0000) {
714 kfree(inti);
715 return -EINVAL;
716 }
717 inti->type = s390int->type;
718 inti->pgm.code = s390int->parm;
719 VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
720 s390int->parm);
721 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +0100722 case KVM_S390_SIGP_SET_PREFIX:
723 inti->prefix.address = s390int->parm;
724 inti->type = s390int->type;
725 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
726 s390int->parm);
727 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100728 case KVM_S390_SIGP_STOP:
729 case KVM_S390_RESTART:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100730 VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type);
731 inti->type = s390int->type;
732 break;
Jason J. Herne82a12732012-10-02 16:25:36 +0200733 case KVM_S390_INT_EXTERNAL_CALL:
734 if (s390int->parm & 0xffff0000) {
735 kfree(inti);
736 return -EINVAL;
737 }
738 VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u",
739 s390int->parm);
740 inti->type = s390int->type;
741 inti->extcall.code = s390int->parm;
742 break;
743 case KVM_S390_INT_EMERGENCY:
744 if (s390int->parm & 0xffff0000) {
745 kfree(inti);
746 return -EINVAL;
747 }
748 VCPU_EVENT(vcpu, 3, "inject: emergency %u\n", s390int->parm);
749 inti->type = s390int->type;
750 inti->emerg.code = s390int->parm;
751 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100752 case KVM_S390_INT_VIRTIO:
753 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +0100754 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100755 default:
756 kfree(inti);
757 return -EINVAL;
758 }
Cornelia Huckade38c32012-07-23 17:20:30 +0200759 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, s390int->type, s390int->parm,
760 s390int->parm64, 2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100761
762 mutex_lock(&vcpu->kvm->lock);
763 li = &vcpu->arch.local_int;
764 spin_lock_bh(&li->lock);
765 if (inti->type == KVM_S390_PROGRAM_INT)
766 list_add(&inti->list, &li->list);
767 else
768 list_add_tail(&inti->list, &li->list);
769 atomic_set(&li->active, 1);
770 if (inti->type == KVM_S390_SIGP_STOP)
771 li->action_bits |= ACTION_STOP_ON_STOP;
772 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
773 if (waitqueue_active(&li->wq))
774 wake_up_interruptible(&vcpu->arch.local_int.wq);
775 spin_unlock_bh(&li->lock);
776 mutex_unlock(&vcpu->kvm->lock);
777 return 0;
778}