blob: 82c481ddef76286c9be88ce0e45e4df31780eb5d [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
24static int psw_extint_disabled(struct kvm_vcpu *vcpu)
25{
26 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT);
27}
28
29static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
30{
31 if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER) ||
32 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO) ||
33 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT))
34 return 0;
35 return 1;
36}
37
38static int __interrupt_is_deliverable(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +020039 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +010040{
41 switch (inti->type) {
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020042 case KVM_S390_INT_EXTERNAL_CALL:
43 if (psw_extint_disabled(vcpu))
44 return 0;
45 if (vcpu->arch.sie_block->gcr[0] & 0x2000ul)
46 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +010047 case KVM_S390_INT_EMERGENCY:
48 if (psw_extint_disabled(vcpu))
49 return 0;
50 if (vcpu->arch.sie_block->gcr[0] & 0x4000ul)
51 return 1;
52 return 0;
53 case KVM_S390_INT_SERVICE:
54 if (psw_extint_disabled(vcpu))
55 return 0;
56 if (vcpu->arch.sie_block->gcr[0] & 0x200ul)
57 return 1;
58 return 0;
59 case KVM_S390_INT_VIRTIO:
60 if (psw_extint_disabled(vcpu))
61 return 0;
62 if (vcpu->arch.sie_block->gcr[0] & 0x200ul)
63 return 1;
64 return 0;
65 case KVM_S390_PROGRAM_INT:
66 case KVM_S390_SIGP_STOP:
67 case KVM_S390_SIGP_SET_PREFIX:
68 case KVM_S390_RESTART:
69 return 1;
70 default:
71 BUG();
72 }
73 return 0;
74}
75
76static void __set_cpu_idle(struct kvm_vcpu *vcpu)
77{
78 BUG_ON(vcpu->vcpu_id > KVM_MAX_VCPUS - 1);
79 atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
80 set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
81}
82
83static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
84{
85 BUG_ON(vcpu->vcpu_id > KVM_MAX_VCPUS - 1);
86 atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
87 clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
88}
89
90static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
91{
92 atomic_clear_mask(CPUSTAT_ECALL_PEND |
93 CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
94 &vcpu->arch.sie_block->cpuflags);
95 vcpu->arch.sie_block->lctl = 0x0000;
96}
97
98static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
99{
100 atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags);
101}
102
103static void __set_intercept_indicator(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200104 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100105{
106 switch (inti->type) {
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200107 case KVM_S390_INT_EXTERNAL_CALL:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100108 case KVM_S390_INT_EMERGENCY:
109 case KVM_S390_INT_SERVICE:
110 case KVM_S390_INT_VIRTIO:
111 if (psw_extint_disabled(vcpu))
112 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
113 else
114 vcpu->arch.sie_block->lctl |= LCTL_CR0;
115 break;
116 case KVM_S390_SIGP_STOP:
117 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
118 break;
119 default:
120 BUG();
121 }
122}
123
124static void __do_deliver_interrupt(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200125 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100126{
127 const unsigned short table[] = { 2, 4, 4, 6 };
128 int rc, exception = 0;
129
130 switch (inti->type) {
131 case KVM_S390_INT_EMERGENCY:
132 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg");
133 vcpu->stat.deliver_emergency_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200134 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
135 inti->emerg.code, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100136 rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x1201);
137 if (rc == -EFAULT)
138 exception = 1;
139
Martin Schwidefsky7e180bd2012-03-11 11:59:25 -0400140 rc = put_guest_u16(vcpu, __LC_EXT_CPU_ADDR, inti->emerg.code);
Christian Ehrhardt8bb3a2e2011-07-24 10:48:31 +0200141 if (rc == -EFAULT)
142 exception = 1;
143
Carsten Otteba5c1e92008-03-25 18:47:26 +0100144 rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
145 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
146 if (rc == -EFAULT)
147 exception = 1;
148
149 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
150 __LC_EXT_NEW_PSW, sizeof(psw_t));
151 if (rc == -EFAULT)
152 exception = 1;
153 break;
154
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200155 case KVM_S390_INT_EXTERNAL_CALL:
156 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call");
157 vcpu->stat.deliver_external_call++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200158 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
159 inti->extcall.code, 0);
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200160 rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x1202);
161 if (rc == -EFAULT)
162 exception = 1;
163
Martin Schwidefsky7e180bd2012-03-11 11:59:25 -0400164 rc = put_guest_u16(vcpu, __LC_EXT_CPU_ADDR, inti->extcall.code);
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200165 if (rc == -EFAULT)
166 exception = 1;
167
168 rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
169 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
170 if (rc == -EFAULT)
171 exception = 1;
172
173 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
174 __LC_EXT_NEW_PSW, sizeof(psw_t));
175 if (rc == -EFAULT)
176 exception = 1;
177 break;
178
Carsten Otteba5c1e92008-03-25 18:47:26 +0100179 case KVM_S390_INT_SERVICE:
180 VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x",
181 inti->ext.ext_params);
182 vcpu->stat.deliver_service_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200183 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
184 inti->ext.ext_params, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100185 rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x2401);
186 if (rc == -EFAULT)
187 exception = 1;
188
189 rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
190 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
191 if (rc == -EFAULT)
192 exception = 1;
193
194 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
195 __LC_EXT_NEW_PSW, sizeof(psw_t));
196 if (rc == -EFAULT)
197 exception = 1;
198
199 rc = put_guest_u32(vcpu, __LC_EXT_PARAMS, inti->ext.ext_params);
200 if (rc == -EFAULT)
201 exception = 1;
202 break;
203
204 case KVM_S390_INT_VIRTIO:
Heiko Carstens33e19112009-01-09 12:14:56 +0100205 VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx",
Carsten Otteba5c1e92008-03-25 18:47:26 +0100206 inti->ext.ext_params, inti->ext.ext_params2);
207 vcpu->stat.deliver_virtio_interrupt++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200208 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
209 inti->ext.ext_params,
210 inti->ext.ext_params2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100211 rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x2603);
212 if (rc == -EFAULT)
213 exception = 1;
214
Martin Schwidefsky7e180bd2012-03-11 11:59:25 -0400215 rc = put_guest_u16(vcpu, __LC_EXT_CPU_ADDR, 0x0d00);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100216 if (rc == -EFAULT)
217 exception = 1;
218
219 rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
220 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
221 if (rc == -EFAULT)
222 exception = 1;
223
224 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
225 __LC_EXT_NEW_PSW, sizeof(psw_t));
226 if (rc == -EFAULT)
227 exception = 1;
228
229 rc = put_guest_u32(vcpu, __LC_EXT_PARAMS, inti->ext.ext_params);
230 if (rc == -EFAULT)
231 exception = 1;
232
Heiko Carstenscbb870c2010-02-26 22:37:43 +0100233 rc = put_guest_u64(vcpu, __LC_EXT_PARAMS2,
234 inti->ext.ext_params2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100235 if (rc == -EFAULT)
236 exception = 1;
237 break;
238
239 case KVM_S390_SIGP_STOP:
240 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu stop");
241 vcpu->stat.deliver_stop_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200242 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
243 0, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100244 __set_intercept_indicator(vcpu, inti);
245 break;
246
247 case KVM_S390_SIGP_SET_PREFIX:
248 VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x",
249 inti->prefix.address);
250 vcpu->stat.deliver_prefix_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200251 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
252 inti->prefix.address, 0);
Christian Borntraeger8d26cf72012-01-11 11:19:32 +0100253 kvm_s390_set_prefix(vcpu, inti->prefix.address);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100254 break;
255
256 case KVM_S390_RESTART:
257 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart");
258 vcpu->stat.deliver_restart_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200259 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
260 0, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100261 rc = copy_to_guest(vcpu, offsetof(struct _lowcore,
262 restart_old_psw), &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
263 if (rc == -EFAULT)
264 exception = 1;
265
266 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
267 offsetof(struct _lowcore, restart_psw), sizeof(psw_t));
268 if (rc == -EFAULT)
269 exception = 1;
Cornelia Huck9e6dabe2011-11-17 11:00:41 +0100270 atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100271 break;
272
273 case KVM_S390_PROGRAM_INT:
274 VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x",
275 inti->pgm.code,
276 table[vcpu->arch.sie_block->ipa >> 14]);
277 vcpu->stat.deliver_program_int++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200278 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
279 inti->pgm.code, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100280 rc = put_guest_u16(vcpu, __LC_PGM_INT_CODE, inti->pgm.code);
281 if (rc == -EFAULT)
282 exception = 1;
283
284 rc = put_guest_u16(vcpu, __LC_PGM_ILC,
285 table[vcpu->arch.sie_block->ipa >> 14]);
286 if (rc == -EFAULT)
287 exception = 1;
288
289 rc = copy_to_guest(vcpu, __LC_PGM_OLD_PSW,
290 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
291 if (rc == -EFAULT)
292 exception = 1;
293
294 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
295 __LC_PGM_NEW_PSW, sizeof(psw_t));
296 if (rc == -EFAULT)
297 exception = 1;
298 break;
299
300 default:
301 BUG();
302 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100303 if (exception) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200304 printk("kvm: The guest lowcore is not mapped during interrupt "
305 "delivery, killing userspace\n");
306 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100307 }
308}
309
310static int __try_deliver_ckc_interrupt(struct kvm_vcpu *vcpu)
311{
312 int rc, exception = 0;
313
314 if (psw_extint_disabled(vcpu))
315 return 0;
316 if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
317 return 0;
318 rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x1004);
319 if (rc == -EFAULT)
320 exception = 1;
321 rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
322 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
323 if (rc == -EFAULT)
324 exception = 1;
325 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
326 __LC_EXT_NEW_PSW, sizeof(psw_t));
327 if (rc == -EFAULT)
328 exception = 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100329 if (exception) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200330 printk("kvm: The guest lowcore is not mapped during interrupt "
331 "delivery, killing userspace\n");
332 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100333 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100334 return 1;
335}
336
Gleb Natapova1b37102009-07-09 15:33:52 +0300337static int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100338{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200339 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
340 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
341 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100342 int rc = 0;
343
344 if (atomic_read(&li->active)) {
345 spin_lock_bh(&li->lock);
346 list_for_each_entry(inti, &li->list, list)
347 if (__interrupt_is_deliverable(vcpu, inti)) {
348 rc = 1;
349 break;
350 }
351 spin_unlock_bh(&li->lock);
352 }
353
354 if ((!rc) && atomic_read(&fi->active)) {
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200355 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100356 list_for_each_entry(inti, &fi->list, list)
357 if (__interrupt_is_deliverable(vcpu, inti)) {
358 rc = 1;
359 break;
360 }
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200361 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100362 }
363
364 if ((!rc) && (vcpu->arch.sie_block->ckc <
365 get_clock() + vcpu->arch.sie_block->epoch)) {
366 if ((!psw_extint_disabled(vcpu)) &&
367 (vcpu->arch.sie_block->gcr[0] & 0x800ul))
368 rc = 1;
369 }
370
371 return rc;
372}
373
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300374int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
375{
376 return 0;
377}
378
Carsten Otteba5c1e92008-03-25 18:47:26 +0100379int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
380{
381 u64 now, sltime;
382 DECLARE_WAITQUEUE(wait, current);
383
384 vcpu->stat.exit_wait_state++;
385 if (kvm_cpu_has_interrupt(vcpu))
386 return 0;
387
Carsten Ottee52b2af2008-05-21 13:37:44 +0200388 __set_cpu_idle(vcpu);
389 spin_lock_bh(&vcpu->arch.local_int.lock);
390 vcpu->arch.local_int.timer_due = 0;
391 spin_unlock_bh(&vcpu->arch.local_int.lock);
392
Carsten Otteba5c1e92008-03-25 18:47:26 +0100393 if (psw_interrupts_disabled(vcpu)) {
394 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
395 __unset_cpu_idle(vcpu);
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100396 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +0100397 }
398
399 if (psw_extint_disabled(vcpu) ||
400 (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))) {
401 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
402 goto no_timer;
403 }
404
405 now = get_clock() + vcpu->arch.sie_block->epoch;
406 if (vcpu->arch.sie_block->ckc < now) {
407 __unset_cpu_idle(vcpu);
408 return 0;
409 }
410
Heiko Carstensed4f2092013-01-14 16:55:55 +0100411 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100412
Christian Borntraegerca872302009-05-12 17:21:49 +0200413 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
414 VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100415no_timer:
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200416 spin_lock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100417 spin_lock_bh(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100418 add_wait_queue(&vcpu->arch.local_int.wq, &wait);
419 while (list_empty(&vcpu->arch.local_int.list) &&
420 list_empty(&vcpu->arch.local_int.float_int->list) &&
421 (!vcpu->arch.local_int.timer_due) &&
422 !signal_pending(current)) {
423 set_current_state(TASK_INTERRUPTIBLE);
424 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200425 spin_unlock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100426 schedule();
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200427 spin_lock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100428 spin_lock_bh(&vcpu->arch.local_int.lock);
429 }
430 __unset_cpu_idle(vcpu);
431 __set_current_state(TASK_RUNNING);
Christian Borntraegerd3bc2f92009-07-16 17:17:37 +0200432 remove_wait_queue(&vcpu->arch.local_int.wq, &wait);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100433 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200434 spin_unlock(&vcpu->arch.local_int.float_int->lock);
Christian Borntraegerca872302009-05-12 17:21:49 +0200435 hrtimer_try_to_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100436 return 0;
437}
438
Christian Borntraegerca872302009-05-12 17:21:49 +0200439void kvm_s390_tasklet(unsigned long parm)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100440{
Christian Borntraegerca872302009-05-12 17:21:49 +0200441 struct kvm_vcpu *vcpu = (struct kvm_vcpu *) parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100442
Christian Borntraegerca872302009-05-12 17:21:49 +0200443 spin_lock(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100444 vcpu->arch.local_int.timer_due = 1;
445 if (waitqueue_active(&vcpu->arch.local_int.wq))
446 wake_up_interruptible(&vcpu->arch.local_int.wq);
Christian Borntraegerca872302009-05-12 17:21:49 +0200447 spin_unlock(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100448}
449
Christian Borntraegerca872302009-05-12 17:21:49 +0200450/*
451 * low level hrtimer wake routine. Because this runs in hardirq context
452 * we schedule a tasklet to do the real work.
453 */
454enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
455{
456 struct kvm_vcpu *vcpu;
457
458 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
459 tasklet_schedule(&vcpu->arch.tasklet);
460
461 return HRTIMER_NORESTART;
462}
Carsten Otteba5c1e92008-03-25 18:47:26 +0100463
464void kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
465{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200466 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
467 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
468 struct kvm_s390_interrupt_info *n, *inti = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100469 int deliver;
470
471 __reset_intercept_indicators(vcpu);
472 if (atomic_read(&li->active)) {
473 do {
474 deliver = 0;
475 spin_lock_bh(&li->lock);
476 list_for_each_entry_safe(inti, n, &li->list, list) {
477 if (__interrupt_is_deliverable(vcpu, inti)) {
478 list_del(&inti->list);
479 deliver = 1;
480 break;
481 }
482 __set_intercept_indicator(vcpu, inti);
483 }
484 if (list_empty(&li->list))
485 atomic_set(&li->active, 0);
486 spin_unlock_bh(&li->lock);
487 if (deliver) {
488 __do_deliver_interrupt(vcpu, inti);
489 kfree(inti);
490 }
491 } while (deliver);
492 }
493
494 if ((vcpu->arch.sie_block->ckc <
495 get_clock() + vcpu->arch.sie_block->epoch))
496 __try_deliver_ckc_interrupt(vcpu);
497
498 if (atomic_read(&fi->active)) {
499 do {
500 deliver = 0;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200501 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100502 list_for_each_entry_safe(inti, n, &fi->list, list) {
503 if (__interrupt_is_deliverable(vcpu, inti)) {
504 list_del(&inti->list);
505 deliver = 1;
506 break;
507 }
508 __set_intercept_indicator(vcpu, inti);
509 }
510 if (list_empty(&fi->list))
511 atomic_set(&fi->active, 0);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200512 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100513 if (deliver) {
514 __do_deliver_interrupt(vcpu, inti);
515 kfree(inti);
516 }
517 } while (deliver);
518 }
519}
520
521int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
522{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200523 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
524 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100525
526 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
527 if (!inti)
528 return -ENOMEM;
529
Joe Perchesa419aef2009-08-18 11:18:35 -0700530 inti->type = KVM_S390_PROGRAM_INT;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100531 inti->pgm.code = code;
532
533 VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code);
Cornelia Huckade38c32012-07-23 17:20:30 +0200534 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, inti->type, code, 0, 1);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100535 spin_lock_bh(&li->lock);
536 list_add(&inti->list, &li->list);
537 atomic_set(&li->active, 1);
538 BUG_ON(waitqueue_active(&li->wq));
539 spin_unlock_bh(&li->lock);
540 return 0;
541}
542
543int kvm_s390_inject_vm(struct kvm *kvm,
544 struct kvm_s390_interrupt *s390int)
545{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200546 struct kvm_s390_local_interrupt *li;
547 struct kvm_s390_float_interrupt *fi;
548 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100549 int sigcpu;
550
551 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
552 if (!inti)
553 return -ENOMEM;
554
555 switch (s390int->type) {
556 case KVM_S390_INT_VIRTIO:
Heiko Carstens33e19112009-01-09 12:14:56 +0100557 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
Carsten Otteba5c1e92008-03-25 18:47:26 +0100558 s390int->parm, s390int->parm64);
559 inti->type = s390int->type;
560 inti->ext.ext_params = s390int->parm;
561 inti->ext.ext_params2 = s390int->parm64;
562 break;
563 case KVM_S390_INT_SERVICE:
564 VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
565 inti->type = s390int->type;
566 inti->ext.ext_params = s390int->parm;
567 break;
568 case KVM_S390_PROGRAM_INT:
569 case KVM_S390_SIGP_STOP:
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200570 case KVM_S390_INT_EXTERNAL_CALL:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100571 case KVM_S390_INT_EMERGENCY:
572 default:
573 kfree(inti);
574 return -EINVAL;
575 }
Cornelia Huckade38c32012-07-23 17:20:30 +0200576 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
577 2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100578
579 mutex_lock(&kvm->lock);
580 fi = &kvm->arch.float_int;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200581 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100582 list_add_tail(&inti->list, &fi->list);
583 atomic_set(&fi->active, 1);
584 sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS);
585 if (sigcpu == KVM_MAX_VCPUS) {
586 do {
587 sigcpu = fi->next_rr_cpu++;
588 if (sigcpu == KVM_MAX_VCPUS)
589 sigcpu = fi->next_rr_cpu = 0;
590 } while (fi->local_int[sigcpu] == NULL);
591 }
592 li = fi->local_int[sigcpu];
593 spin_lock_bh(&li->lock);
594 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
595 if (waitqueue_active(&li->wq))
596 wake_up_interruptible(&li->wq);
597 spin_unlock_bh(&li->lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200598 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100599 mutex_unlock(&kvm->lock);
600 return 0;
601}
602
603int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
604 struct kvm_s390_interrupt *s390int)
605{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200606 struct kvm_s390_local_interrupt *li;
607 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100608
609 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
610 if (!inti)
611 return -ENOMEM;
612
613 switch (s390int->type) {
614 case KVM_S390_PROGRAM_INT:
615 if (s390int->parm & 0xffff0000) {
616 kfree(inti);
617 return -EINVAL;
618 }
619 inti->type = s390int->type;
620 inti->pgm.code = s390int->parm;
621 VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
622 s390int->parm);
623 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +0100624 case KVM_S390_SIGP_SET_PREFIX:
625 inti->prefix.address = s390int->parm;
626 inti->type = s390int->type;
627 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
628 s390int->parm);
629 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100630 case KVM_S390_SIGP_STOP:
631 case KVM_S390_RESTART:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100632 VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type);
633 inti->type = s390int->type;
634 break;
Jason J. Herne82a12732012-10-02 16:25:36 +0200635 case KVM_S390_INT_EXTERNAL_CALL:
636 if (s390int->parm & 0xffff0000) {
637 kfree(inti);
638 return -EINVAL;
639 }
640 VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u",
641 s390int->parm);
642 inti->type = s390int->type;
643 inti->extcall.code = s390int->parm;
644 break;
645 case KVM_S390_INT_EMERGENCY:
646 if (s390int->parm & 0xffff0000) {
647 kfree(inti);
648 return -EINVAL;
649 }
650 VCPU_EVENT(vcpu, 3, "inject: emergency %u\n", s390int->parm);
651 inti->type = s390int->type;
652 inti->emerg.code = s390int->parm;
653 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100654 case KVM_S390_INT_VIRTIO:
655 case KVM_S390_INT_SERVICE:
656 default:
657 kfree(inti);
658 return -EINVAL;
659 }
Cornelia Huckade38c32012-07-23 17:20:30 +0200660 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, s390int->type, s390int->parm,
661 s390int->parm64, 2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100662
663 mutex_lock(&vcpu->kvm->lock);
664 li = &vcpu->arch.local_int;
665 spin_lock_bh(&li->lock);
666 if (inti->type == KVM_S390_PROGRAM_INT)
667 list_add(&inti->list, &li->list);
668 else
669 list_add_tail(&inti->list, &li->list);
670 atomic_set(&li->active, 1);
671 if (inti->type == KVM_S390_SIGP_STOP)
672 li->action_bits |= ACTION_STOP_ON_STOP;
673 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
674 if (waitqueue_active(&li->wq))
675 wake_up_interruptible(&vcpu->arch.local_int.wq);
676 spin_unlock_bh(&li->lock);
677 mutex_unlock(&vcpu->kvm->lock);
678 return 0;
679}