blob: 1d0f9d532c0b85ba1caf84400d80fe7d1609dda9 [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
Dominik Dingel3c038e62013-10-07 17:11:48 +020034int psw_extint_disabled(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +010035{
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
Cornelia Huck79fd50c2013-02-07 13:20:52 +010058static u64 int_word_to_isc_bits(u32 int_word)
59{
60 u8 isc = (int_word & 0x38000000) >> 27;
61
62 return (0x80 >> isc) << 24;
63}
64
Carsten Otteba5c1e92008-03-25 18:47:26 +010065static int __interrupt_is_deliverable(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +020066 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +010067{
68 switch (inti->type) {
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020069 case KVM_S390_INT_EXTERNAL_CALL:
70 if (psw_extint_disabled(vcpu))
71 return 0;
72 if (vcpu->arch.sie_block->gcr[0] & 0x2000ul)
73 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +010074 case KVM_S390_INT_EMERGENCY:
75 if (psw_extint_disabled(vcpu))
76 return 0;
77 if (vcpu->arch.sie_block->gcr[0] & 0x4000ul)
78 return 1;
79 return 0;
80 case KVM_S390_INT_SERVICE:
Dominik Dingel3c038e62013-10-07 17:11:48 +020081 case KVM_S390_INT_PFAULT_INIT:
82 case KVM_S390_INT_PFAULT_DONE:
Carsten Otteba5c1e92008-03-25 18:47:26 +010083 case KVM_S390_INT_VIRTIO:
84 if (psw_extint_disabled(vcpu))
85 return 0;
86 if (vcpu->arch.sie_block->gcr[0] & 0x200ul)
87 return 1;
88 return 0;
89 case KVM_S390_PROGRAM_INT:
90 case KVM_S390_SIGP_STOP:
91 case KVM_S390_SIGP_SET_PREFIX:
92 case KVM_S390_RESTART:
93 return 1;
Cornelia Huck48a3e952012-12-20 15:32:09 +010094 case KVM_S390_MCHK:
95 if (psw_mchk_disabled(vcpu))
96 return 0;
97 if (vcpu->arch.sie_block->gcr[14] & inti->mchk.cr14)
98 return 1;
99 return 0;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100100 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
101 if (psw_ioint_disabled(vcpu))
102 return 0;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100103 if (vcpu->arch.sie_block->gcr[6] &
104 int_word_to_isc_bits(inti->io.io_int_word))
Cornelia Huckd8346b72012-12-20 15:32:08 +0100105 return 1;
106 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100107 default:
Cornelia Huckd8346b72012-12-20 15:32:08 +0100108 printk(KERN_WARNING "illegal interrupt type %llx\n",
109 inti->type);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100110 BUG();
111 }
112 return 0;
113}
114
115static void __set_cpu_idle(struct kvm_vcpu *vcpu)
116{
117 BUG_ON(vcpu->vcpu_id > KVM_MAX_VCPUS - 1);
118 atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
119 set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
120}
121
122static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
123{
124 BUG_ON(vcpu->vcpu_id > KVM_MAX_VCPUS - 1);
125 atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
126 clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
127}
128
129static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
130{
131 atomic_clear_mask(CPUSTAT_ECALL_PEND |
132 CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
133 &vcpu->arch.sie_block->cpuflags);
134 vcpu->arch.sie_block->lctl = 0x0000;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100135 vcpu->arch.sie_block->ictl &= ~ICTL_LPSW;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100136}
137
138static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
139{
140 atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags);
141}
142
143static void __set_intercept_indicator(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200144 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100145{
146 switch (inti->type) {
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200147 case KVM_S390_INT_EXTERNAL_CALL:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100148 case KVM_S390_INT_EMERGENCY:
149 case KVM_S390_INT_SERVICE:
Dominik Dingel3c038e62013-10-07 17:11:48 +0200150 case KVM_S390_INT_PFAULT_INIT:
151 case KVM_S390_INT_PFAULT_DONE:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100152 case KVM_S390_INT_VIRTIO:
153 if (psw_extint_disabled(vcpu))
154 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
155 else
156 vcpu->arch.sie_block->lctl |= LCTL_CR0;
157 break;
158 case KVM_S390_SIGP_STOP:
159 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
160 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100161 case KVM_S390_MCHK:
162 if (psw_mchk_disabled(vcpu))
163 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
164 else
165 vcpu->arch.sie_block->lctl |= LCTL_CR14;
166 break;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100167 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
168 if (psw_ioint_disabled(vcpu))
169 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
170 else
171 vcpu->arch.sie_block->lctl |= LCTL_CR6;
172 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100173 default:
174 BUG();
175 }
176}
177
178static void __do_deliver_interrupt(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200179 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100180{
181 const unsigned short table[] = { 2, 4, 4, 6 };
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100182 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100183
184 switch (inti->type) {
185 case KVM_S390_INT_EMERGENCY:
186 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg");
187 vcpu->stat.deliver_emergency_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200188 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
189 inti->emerg.code, 0);
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100190 rc = put_guest(vcpu, 0x1201, (u16 __user *)__LC_EXT_INT_CODE);
Heiko Carstens396083a2013-03-05 13:14:44 +0100191 rc |= put_guest(vcpu, inti->emerg.code,
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100192 (u16 __user *)__LC_EXT_CPU_ADDR);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100193 rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
194 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
195 rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
196 __LC_EXT_NEW_PSW, sizeof(psw_t));
Carsten Otteba5c1e92008-03-25 18:47:26 +0100197 break;
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200198 case KVM_S390_INT_EXTERNAL_CALL:
199 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call");
200 vcpu->stat.deliver_external_call++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200201 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
202 inti->extcall.code, 0);
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100203 rc = put_guest(vcpu, 0x1202, (u16 __user *)__LC_EXT_INT_CODE);
Heiko Carstens396083a2013-03-05 13:14:44 +0100204 rc |= put_guest(vcpu, inti->extcall.code,
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100205 (u16 __user *)__LC_EXT_CPU_ADDR);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100206 rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
207 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
208 rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
209 __LC_EXT_NEW_PSW, sizeof(psw_t));
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200210 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100211 case KVM_S390_INT_SERVICE:
212 VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x",
213 inti->ext.ext_params);
214 vcpu->stat.deliver_service_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200215 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
216 inti->ext.ext_params, 0);
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100217 rc = put_guest(vcpu, 0x2401, (u16 __user *)__LC_EXT_INT_CODE);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100218 rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
219 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
220 rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
221 __LC_EXT_NEW_PSW, sizeof(psw_t));
Heiko Carstens396083a2013-03-05 13:14:44 +0100222 rc |= put_guest(vcpu, inti->ext.ext_params,
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100223 (u32 __user *)__LC_EXT_PARAMS);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100224 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200225 case KVM_S390_INT_PFAULT_INIT:
226 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0,
227 inti->ext.ext_params2);
228 rc = put_guest(vcpu, 0x2603, (u16 __user *) __LC_EXT_INT_CODE);
229 rc |= put_guest(vcpu, 0x0600, (u16 __user *) __LC_EXT_CPU_ADDR);
230 rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
231 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
232 rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
233 __LC_EXT_NEW_PSW, sizeof(psw_t));
234 rc |= put_guest(vcpu, inti->ext.ext_params2,
235 (u64 __user *) __LC_EXT_PARAMS2);
236 break;
237 case KVM_S390_INT_PFAULT_DONE:
238 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0,
239 inti->ext.ext_params2);
240 rc = put_guest(vcpu, 0x2603, (u16 __user *) __LC_EXT_INT_CODE);
241 rc |= put_guest(vcpu, 0x0680, (u16 __user *) __LC_EXT_CPU_ADDR);
242 rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
243 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
244 rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
245 __LC_EXT_NEW_PSW, sizeof(psw_t));
246 rc |= put_guest(vcpu, inti->ext.ext_params2,
247 (u64 __user *) __LC_EXT_PARAMS2);
248 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100249 case KVM_S390_INT_VIRTIO:
Heiko Carstens33e19112009-01-09 12:14:56 +0100250 VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx",
Carsten Otteba5c1e92008-03-25 18:47:26 +0100251 inti->ext.ext_params, inti->ext.ext_params2);
252 vcpu->stat.deliver_virtio_interrupt++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200253 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
254 inti->ext.ext_params,
255 inti->ext.ext_params2);
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100256 rc = put_guest(vcpu, 0x2603, (u16 __user *)__LC_EXT_INT_CODE);
257 rc |= put_guest(vcpu, 0x0d00, (u16 __user *)__LC_EXT_CPU_ADDR);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100258 rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
259 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
260 rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
261 __LC_EXT_NEW_PSW, sizeof(psw_t));
Heiko Carstens396083a2013-03-05 13:14:44 +0100262 rc |= put_guest(vcpu, inti->ext.ext_params,
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100263 (u32 __user *)__LC_EXT_PARAMS);
Heiko Carstens396083a2013-03-05 13:14:44 +0100264 rc |= put_guest(vcpu, inti->ext.ext_params2,
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100265 (u64 __user *)__LC_EXT_PARAMS2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100266 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100267 case KVM_S390_SIGP_STOP:
268 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu stop");
269 vcpu->stat.deliver_stop_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200270 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
271 0, 0);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100272 __set_intercept_indicator(vcpu, inti);
273 break;
274
275 case KVM_S390_SIGP_SET_PREFIX:
276 VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x",
277 inti->prefix.address);
278 vcpu->stat.deliver_prefix_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200279 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
280 inti->prefix.address, 0);
Christian Borntraeger8d26cf72012-01-11 11:19:32 +0100281 kvm_s390_set_prefix(vcpu, inti->prefix.address);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100282 break;
283
284 case KVM_S390_RESTART:
285 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart");
286 vcpu->stat.deliver_restart_signal++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200287 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
288 0, 0);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100289 rc = copy_to_guest(vcpu,
290 offsetof(struct _lowcore, restart_old_psw),
291 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
292 rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
293 offsetof(struct _lowcore, restart_psw),
294 sizeof(psw_t));
Cornelia Huck9e6dabe2011-11-17 11:00:41 +0100295 atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100296 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100297 case KVM_S390_PROGRAM_INT:
298 VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x",
299 inti->pgm.code,
300 table[vcpu->arch.sie_block->ipa >> 14]);
301 vcpu->stat.deliver_program_int++;
Cornelia Huckade38c32012-07-23 17:20:30 +0200302 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
303 inti->pgm.code, 0);
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100304 rc = put_guest(vcpu, inti->pgm.code, (u16 __user *)__LC_PGM_INT_CODE);
Heiko Carstens396083a2013-03-05 13:14:44 +0100305 rc |= put_guest(vcpu, table[vcpu->arch.sie_block->ipa >> 14],
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100306 (u16 __user *)__LC_PGM_ILC);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100307 rc |= copy_to_guest(vcpu, __LC_PGM_OLD_PSW,
308 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
309 rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
310 __LC_PGM_NEW_PSW, sizeof(psw_t));
Carsten Otteba5c1e92008-03-25 18:47:26 +0100311 break;
312
Cornelia Huck48a3e952012-12-20 15:32:09 +0100313 case KVM_S390_MCHK:
314 VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx",
315 inti->mchk.mcic);
316 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
317 inti->mchk.cr14,
318 inti->mchk.mcic);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100319 rc = kvm_s390_vcpu_store_status(vcpu,
320 KVM_S390_STORE_STATUS_PREFIXED);
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100321 rc |= put_guest(vcpu, inti->mchk.mcic, (u64 __user *) __LC_MCCK_CODE);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100322 rc |= copy_to_guest(vcpu, __LC_MCK_OLD_PSW,
323 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
324 rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
325 __LC_MCK_NEW_PSW, sizeof(psw_t));
Cornelia Huck48a3e952012-12-20 15:32:09 +0100326 break;
327
Cornelia Huckd8346b72012-12-20 15:32:08 +0100328 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
329 {
330 __u32 param0 = ((__u32)inti->io.subchannel_id << 16) |
331 inti->io.subchannel_nr;
332 __u64 param1 = ((__u64)inti->io.io_int_parm << 32) |
333 inti->io.io_int_word;
334 VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type);
335 vcpu->stat.deliver_io_int++;
336 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
337 param0, param1);
Heiko Carstens396083a2013-03-05 13:14:44 +0100338 rc = put_guest(vcpu, inti->io.subchannel_id,
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100339 (u16 __user *) __LC_SUBCHANNEL_ID);
Heiko Carstens396083a2013-03-05 13:14:44 +0100340 rc |= put_guest(vcpu, inti->io.subchannel_nr,
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100341 (u16 __user *) __LC_SUBCHANNEL_NR);
Heiko Carstens396083a2013-03-05 13:14:44 +0100342 rc |= put_guest(vcpu, inti->io.io_int_parm,
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100343 (u32 __user *) __LC_IO_INT_PARM);
Heiko Carstens396083a2013-03-05 13:14:44 +0100344 rc |= put_guest(vcpu, inti->io.io_int_word,
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100345 (u32 __user *) __LC_IO_INT_WORD);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100346 rc |= copy_to_guest(vcpu, __LC_IO_OLD_PSW,
347 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
348 rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
349 __LC_IO_NEW_PSW, sizeof(psw_t));
Cornelia Huckd8346b72012-12-20 15:32:08 +0100350 break;
351 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100352 default:
353 BUG();
354 }
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100355 if (rc) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200356 printk("kvm: The guest lowcore is not mapped during interrupt "
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100357 "delivery, killing userspace\n");
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200358 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100359 }
360}
361
362static int __try_deliver_ckc_interrupt(struct kvm_vcpu *vcpu)
363{
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100364 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100365
366 if (psw_extint_disabled(vcpu))
367 return 0;
368 if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
369 return 0;
Heiko Carstens0a75ca22013-03-05 13:14:47 +0100370 rc = put_guest(vcpu, 0x1004, (u16 __user *)__LC_EXT_INT_CODE);
Heiko Carstensdc5008b2013-03-05 13:14:43 +0100371 rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
372 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
373 rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
374 __LC_EXT_NEW_PSW, sizeof(psw_t));
375 if (rc) {
Christian Borntraeger3cd61292008-07-25 15:51:54 +0200376 printk("kvm: The guest lowcore is not mapped during interrupt "
377 "delivery, killing userspace\n");
378 do_exit(SIGKILL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100379 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100380 return 1;
381}
382
Dominik Dingel3c038e62013-10-07 17:11:48 +0200383int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100384{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200385 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
386 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
387 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100388 int rc = 0;
389
390 if (atomic_read(&li->active)) {
391 spin_lock_bh(&li->lock);
392 list_for_each_entry(inti, &li->list, list)
393 if (__interrupt_is_deliverable(vcpu, inti)) {
394 rc = 1;
395 break;
396 }
397 spin_unlock_bh(&li->lock);
398 }
399
400 if ((!rc) && atomic_read(&fi->active)) {
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200401 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100402 list_for_each_entry(inti, &fi->list, list)
403 if (__interrupt_is_deliverable(vcpu, inti)) {
404 rc = 1;
405 break;
406 }
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200407 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100408 }
409
410 if ((!rc) && (vcpu->arch.sie_block->ckc <
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200411 get_tod_clock_fast() + vcpu->arch.sie_block->epoch)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100412 if ((!psw_extint_disabled(vcpu)) &&
413 (vcpu->arch.sie_block->gcr[0] & 0x800ul))
414 rc = 1;
415 }
416
417 return rc;
418}
419
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300420int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
421{
422 return 0;
423}
424
Carsten Otteba5c1e92008-03-25 18:47:26 +0100425int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
426{
427 u64 now, sltime;
428 DECLARE_WAITQUEUE(wait, current);
429
430 vcpu->stat.exit_wait_state++;
431 if (kvm_cpu_has_interrupt(vcpu))
432 return 0;
433
Carsten Ottee52b2af2008-05-21 13:37:44 +0200434 __set_cpu_idle(vcpu);
435 spin_lock_bh(&vcpu->arch.local_int.lock);
436 vcpu->arch.local_int.timer_due = 0;
437 spin_unlock_bh(&vcpu->arch.local_int.lock);
438
Carsten Otteba5c1e92008-03-25 18:47:26 +0100439 if (psw_interrupts_disabled(vcpu)) {
440 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
441 __unset_cpu_idle(vcpu);
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100442 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +0100443 }
444
445 if (psw_extint_disabled(vcpu) ||
446 (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))) {
447 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
448 goto no_timer;
449 }
450
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200451 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100452 if (vcpu->arch.sie_block->ckc < now) {
453 __unset_cpu_idle(vcpu);
454 return 0;
455 }
456
Heiko Carstensed4f2092013-01-14 16:55:55 +0100457 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100458
Christian Borntraegerca872302009-05-12 17:21:49 +0200459 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
460 VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100461no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +0200462 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200463 spin_lock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100464 spin_lock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200465 add_wait_queue(&vcpu->wq, &wait);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100466 while (list_empty(&vcpu->arch.local_int.list) &&
467 list_empty(&vcpu->arch.local_int.float_int->list) &&
468 (!vcpu->arch.local_int.timer_due) &&
469 !signal_pending(current)) {
470 set_current_state(TASK_INTERRUPTIBLE);
471 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200472 spin_unlock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100473 schedule();
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200474 spin_lock(&vcpu->arch.local_int.float_int->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100475 spin_lock_bh(&vcpu->arch.local_int.lock);
476 }
477 __unset_cpu_idle(vcpu);
478 __set_current_state(TASK_RUNNING);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200479 remove_wait_queue(&vcpu->wq, &wait);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100480 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200481 spin_unlock(&vcpu->arch.local_int.float_int->lock);
Thomas Huth800c1062013-09-12 10:33:45 +0200482 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
483
Christian Borntraegerca872302009-05-12 17:21:49 +0200484 hrtimer_try_to_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100485 return 0;
486}
487
Christian Borntraegerca872302009-05-12 17:21:49 +0200488void kvm_s390_tasklet(unsigned long parm)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100489{
Christian Borntraegerca872302009-05-12 17:21:49 +0200490 struct kvm_vcpu *vcpu = (struct kvm_vcpu *) parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100491
Christian Borntraegerca872302009-05-12 17:21:49 +0200492 spin_lock(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100493 vcpu->arch.local_int.timer_due = 1;
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200494 if (waitqueue_active(&vcpu->wq))
495 wake_up_interruptible(&vcpu->wq);
Christian Borntraegerca872302009-05-12 17:21:49 +0200496 spin_unlock(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100497}
498
Christian Borntraegerca872302009-05-12 17:21:49 +0200499/*
500 * low level hrtimer wake routine. Because this runs in hardirq context
501 * we schedule a tasklet to do the real work.
502 */
503enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
504{
505 struct kvm_vcpu *vcpu;
506
507 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
Michael Mueller9cac38d2014-02-26 16:14:19 +0100508 vcpu->preempted = true;
Christian Borntraegerca872302009-05-12 17:21:49 +0200509 tasklet_schedule(&vcpu->arch.tasklet);
510
511 return HRTIMER_NORESTART;
512}
Carsten Otteba5c1e92008-03-25 18:47:26 +0100513
514void kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
515{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200516 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
517 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
518 struct kvm_s390_interrupt_info *n, *inti = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100519 int deliver;
520
521 __reset_intercept_indicators(vcpu);
522 if (atomic_read(&li->active)) {
523 do {
524 deliver = 0;
525 spin_lock_bh(&li->lock);
526 list_for_each_entry_safe(inti, n, &li->list, list) {
527 if (__interrupt_is_deliverable(vcpu, inti)) {
528 list_del(&inti->list);
529 deliver = 1;
530 break;
531 }
532 __set_intercept_indicator(vcpu, inti);
533 }
534 if (list_empty(&li->list))
535 atomic_set(&li->active, 0);
536 spin_unlock_bh(&li->lock);
537 if (deliver) {
538 __do_deliver_interrupt(vcpu, inti);
539 kfree(inti);
540 }
541 } while (deliver);
542 }
543
544 if ((vcpu->arch.sie_block->ckc <
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200545 get_tod_clock_fast() + vcpu->arch.sie_block->epoch))
Carsten Otteba5c1e92008-03-25 18:47:26 +0100546 __try_deliver_ckc_interrupt(vcpu);
547
548 if (atomic_read(&fi->active)) {
549 do {
550 deliver = 0;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200551 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100552 list_for_each_entry_safe(inti, n, &fi->list, list) {
553 if (__interrupt_is_deliverable(vcpu, inti)) {
554 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100555 fi->irq_count--;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100556 deliver = 1;
557 break;
558 }
559 __set_intercept_indicator(vcpu, inti);
560 }
561 if (list_empty(&fi->list))
562 atomic_set(&fi->active, 0);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200563 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100564 if (deliver) {
565 __do_deliver_interrupt(vcpu, inti);
566 kfree(inti);
567 }
568 } while (deliver);
569 }
570}
571
Cornelia Huck48a3e952012-12-20 15:32:09 +0100572void kvm_s390_deliver_pending_machine_checks(struct kvm_vcpu *vcpu)
573{
574 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
575 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
576 struct kvm_s390_interrupt_info *n, *inti = NULL;
577 int deliver;
578
579 __reset_intercept_indicators(vcpu);
580 if (atomic_read(&li->active)) {
581 do {
582 deliver = 0;
583 spin_lock_bh(&li->lock);
584 list_for_each_entry_safe(inti, n, &li->list, list) {
585 if ((inti->type == KVM_S390_MCHK) &&
586 __interrupt_is_deliverable(vcpu, inti)) {
587 list_del(&inti->list);
588 deliver = 1;
589 break;
590 }
591 __set_intercept_indicator(vcpu, inti);
592 }
593 if (list_empty(&li->list))
594 atomic_set(&li->active, 0);
595 spin_unlock_bh(&li->lock);
596 if (deliver) {
597 __do_deliver_interrupt(vcpu, inti);
598 kfree(inti);
599 }
600 } while (deliver);
601 }
602
603 if (atomic_read(&fi->active)) {
604 do {
605 deliver = 0;
606 spin_lock(&fi->lock);
607 list_for_each_entry_safe(inti, n, &fi->list, list) {
608 if ((inti->type == KVM_S390_MCHK) &&
609 __interrupt_is_deliverable(vcpu, inti)) {
610 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100611 fi->irq_count--;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100612 deliver = 1;
613 break;
614 }
615 __set_intercept_indicator(vcpu, inti);
616 }
617 if (list_empty(&fi->list))
618 atomic_set(&fi->active, 0);
619 spin_unlock(&fi->lock);
620 if (deliver) {
621 __do_deliver_interrupt(vcpu, inti);
622 kfree(inti);
623 }
624 } while (deliver);
625 }
626}
627
Carsten Otteba5c1e92008-03-25 18:47:26 +0100628int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
629{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200630 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
631 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100632
633 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
634 if (!inti)
635 return -ENOMEM;
636
Joe Perchesa419aef2009-08-18 11:18:35 -0700637 inti->type = KVM_S390_PROGRAM_INT;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100638 inti->pgm.code = code;
639
640 VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code);
Cornelia Huckade38c32012-07-23 17:20:30 +0200641 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, inti->type, code, 0, 1);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100642 spin_lock_bh(&li->lock);
643 list_add(&inti->list, &li->list);
644 atomic_set(&li->active, 1);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200645 BUG_ON(waitqueue_active(li->wq));
Carsten Otteba5c1e92008-03-25 18:47:26 +0100646 spin_unlock_bh(&li->lock);
647 return 0;
648}
649
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100650struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
651 u64 cr6, u64 schid)
652{
653 struct kvm_s390_float_interrupt *fi;
654 struct kvm_s390_interrupt_info *inti, *iter;
655
656 if ((!schid && !cr6) || (schid && cr6))
657 return NULL;
658 mutex_lock(&kvm->lock);
659 fi = &kvm->arch.float_int;
660 spin_lock(&fi->lock);
661 inti = NULL;
662 list_for_each_entry(iter, &fi->list, list) {
663 if (!is_ioint(iter->type))
664 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100665 if (cr6 &&
666 ((cr6 & int_word_to_isc_bits(iter->io.io_int_word)) == 0))
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100667 continue;
668 if (schid) {
669 if (((schid & 0x00000000ffff0000) >> 16) !=
670 iter->io.subchannel_id)
671 continue;
672 if ((schid & 0x000000000000ffff) !=
673 iter->io.subchannel_nr)
674 continue;
675 }
676 inti = iter;
677 break;
678 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100679 if (inti) {
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100680 list_del_init(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100681 fi->irq_count--;
682 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100683 if (list_empty(&fi->list))
684 atomic_set(&fi->active, 0);
685 spin_unlock(&fi->lock);
686 mutex_unlock(&kvm->lock);
687 return inti;
688}
689
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100690static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100691{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200692 struct kvm_s390_local_interrupt *li;
693 struct kvm_s390_float_interrupt *fi;
Jens Freimannc05c4182013-10-07 16:13:45 +0200694 struct kvm_s390_interrupt_info *iter;
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100695 struct kvm_vcpu *dst_vcpu = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100696 int sigcpu;
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100697 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100698
Carsten Otteba5c1e92008-03-25 18:47:26 +0100699 mutex_lock(&kvm->lock);
700 fi = &kvm->arch.float_int;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200701 spin_lock(&fi->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100702 if (fi->irq_count >= KVM_S390_MAX_FLOAT_IRQS) {
703 rc = -EINVAL;
704 goto unlock_fi;
705 }
706 fi->irq_count++;
Jens Freimannc05c4182013-10-07 16:13:45 +0200707 if (!is_ioint(inti->type)) {
Cornelia Huckd8346b72012-12-20 15:32:08 +0100708 list_add_tail(&inti->list, &fi->list);
Jens Freimannc05c4182013-10-07 16:13:45 +0200709 } else {
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100710 u64 isc_bits = int_word_to_isc_bits(inti->io.io_int_word);
711
Cornelia Huckd8346b72012-12-20 15:32:08 +0100712 /* Keep I/O interrupts sorted in isc order. */
713 list_for_each_entry(iter, &fi->list, list) {
714 if (!is_ioint(iter->type))
715 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100716 if (int_word_to_isc_bits(iter->io.io_int_word)
717 <= isc_bits)
Cornelia Huckd8346b72012-12-20 15:32:08 +0100718 continue;
719 break;
720 }
721 list_add_tail(&inti->list, &iter->list);
722 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100723 atomic_set(&fi->active, 1);
724 sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS);
725 if (sigcpu == KVM_MAX_VCPUS) {
726 do {
727 sigcpu = fi->next_rr_cpu++;
728 if (sigcpu == KVM_MAX_VCPUS)
729 sigcpu = fi->next_rr_cpu = 0;
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100730 } while (kvm_get_vcpu(kvm, sigcpu) == NULL);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100731 }
Jens Freimann1ee0bc52014-02-25 15:36:45 +0100732 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
733 li = &dst_vcpu->arch.local_int;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100734 spin_lock_bh(&li->lock);
735 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200736 if (waitqueue_active(li->wq))
737 wake_up_interruptible(li->wq);
Michael Mueller9cac38d2014-02-26 16:14:19 +0100738 kvm_get_vcpu(kvm, sigcpu)->preempted = true;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100739 spin_unlock_bh(&li->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100740unlock_fi:
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200741 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100742 mutex_unlock(&kvm->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100743 return rc;
Jens Freimannc05c4182013-10-07 16:13:45 +0200744}
745
746int kvm_s390_inject_vm(struct kvm *kvm,
747 struct kvm_s390_interrupt *s390int)
748{
749 struct kvm_s390_interrupt_info *inti;
750
751 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
752 if (!inti)
753 return -ENOMEM;
754
755 inti->type = s390int->type;
756 switch (inti->type) {
757 case KVM_S390_INT_VIRTIO:
758 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
759 s390int->parm, s390int->parm64);
760 inti->ext.ext_params = s390int->parm;
761 inti->ext.ext_params2 = s390int->parm64;
762 break;
763 case KVM_S390_INT_SERVICE:
764 VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
765 inti->ext.ext_params = s390int->parm;
766 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200767 case KVM_S390_INT_PFAULT_DONE:
768 inti->type = s390int->type;
769 inti->ext.ext_params2 = s390int->parm64;
770 break;
Jens Freimannc05c4182013-10-07 16:13:45 +0200771 case KVM_S390_MCHK:
772 VM_EVENT(kvm, 5, "inject: machine check parm64:%llx",
773 s390int->parm64);
774 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
775 inti->mchk.mcic = s390int->parm64;
776 break;
777 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
778 if (inti->type & IOINT_AI_MASK)
779 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
780 else
781 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
782 s390int->type & IOINT_CSSID_MASK,
783 s390int->type & IOINT_SSID_MASK,
784 s390int->type & IOINT_SCHID_MASK);
785 inti->io.subchannel_id = s390int->parm >> 16;
786 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
787 inti->io.io_int_parm = s390int->parm64 >> 32;
788 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
789 break;
790 default:
791 kfree(inti);
792 return -EINVAL;
793 }
794 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
795 2);
796
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100797 return __inject_vm(kvm, inti);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100798}
799
800int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
801 struct kvm_s390_interrupt *s390int)
802{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200803 struct kvm_s390_local_interrupt *li;
804 struct kvm_s390_interrupt_info *inti;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100805
806 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
807 if (!inti)
808 return -ENOMEM;
809
810 switch (s390int->type) {
811 case KVM_S390_PROGRAM_INT:
812 if (s390int->parm & 0xffff0000) {
813 kfree(inti);
814 return -EINVAL;
815 }
816 inti->type = s390int->type;
817 inti->pgm.code = s390int->parm;
818 VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
819 s390int->parm);
820 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +0100821 case KVM_S390_SIGP_SET_PREFIX:
822 inti->prefix.address = s390int->parm;
823 inti->type = s390int->type;
824 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
825 s390int->parm);
826 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100827 case KVM_S390_SIGP_STOP:
828 case KVM_S390_RESTART:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100829 VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type);
830 inti->type = s390int->type;
831 break;
Jason J. Herne82a12732012-10-02 16:25:36 +0200832 case KVM_S390_INT_EXTERNAL_CALL:
833 if (s390int->parm & 0xffff0000) {
834 kfree(inti);
835 return -EINVAL;
836 }
837 VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u",
838 s390int->parm);
839 inti->type = s390int->type;
840 inti->extcall.code = s390int->parm;
841 break;
842 case KVM_S390_INT_EMERGENCY:
843 if (s390int->parm & 0xffff0000) {
844 kfree(inti);
845 return -EINVAL;
846 }
847 VCPU_EVENT(vcpu, 3, "inject: emergency %u\n", s390int->parm);
848 inti->type = s390int->type;
849 inti->emerg.code = s390int->parm;
850 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100851 case KVM_S390_MCHK:
852 VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx",
853 s390int->parm64);
854 inti->type = s390int->type;
855 inti->mchk.mcic = s390int->parm64;
856 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200857 case KVM_S390_INT_PFAULT_INIT:
858 inti->type = s390int->type;
859 inti->ext.ext_params2 = s390int->parm64;
860 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100861 case KVM_S390_INT_VIRTIO:
862 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +0100863 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100864 default:
865 kfree(inti);
866 return -EINVAL;
867 }
Cornelia Huckade38c32012-07-23 17:20:30 +0200868 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, s390int->type, s390int->parm,
869 s390int->parm64, 2);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100870
871 mutex_lock(&vcpu->kvm->lock);
872 li = &vcpu->arch.local_int;
873 spin_lock_bh(&li->lock);
874 if (inti->type == KVM_S390_PROGRAM_INT)
875 list_add(&inti->list, &li->list);
876 else
877 list_add_tail(&inti->list, &li->list);
878 atomic_set(&li->active, 1);
879 if (inti->type == KVM_S390_SIGP_STOP)
880 li->action_bits |= ACTION_STOP_ON_STOP;
881 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200882 if (waitqueue_active(&vcpu->wq))
883 wake_up_interruptible(&vcpu->wq);
Michael Mueller9cac38d2014-02-26 16:14:19 +0100884 vcpu->preempted = true;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100885 spin_unlock_bh(&li->lock);
886 mutex_unlock(&vcpu->kvm->lock);
887 return 0;
888}
Jens Freimannc05c4182013-10-07 16:13:45 +0200889
890static void clear_floating_interrupts(struct kvm *kvm)
891{
892 struct kvm_s390_float_interrupt *fi;
893 struct kvm_s390_interrupt_info *n, *inti = NULL;
894
895 mutex_lock(&kvm->lock);
896 fi = &kvm->arch.float_int;
897 spin_lock(&fi->lock);
898 list_for_each_entry_safe(inti, n, &fi->list, list) {
899 list_del(&inti->list);
900 kfree(inti);
901 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100902 fi->irq_count = 0;
Jens Freimannc05c4182013-10-07 16:13:45 +0200903 atomic_set(&fi->active, 0);
904 spin_unlock(&fi->lock);
905 mutex_unlock(&kvm->lock);
906}
907
908static inline int copy_irq_to_user(struct kvm_s390_interrupt_info *inti,
909 u8 *addr)
910{
911 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
912 struct kvm_s390_irq irq = {0};
913
914 irq.type = inti->type;
915 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +0200916 case KVM_S390_INT_PFAULT_INIT:
917 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +0200918 case KVM_S390_INT_VIRTIO:
919 case KVM_S390_INT_SERVICE:
920 irq.u.ext = inti->ext;
921 break;
922 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
923 irq.u.io = inti->io;
924 break;
925 case KVM_S390_MCHK:
926 irq.u.mchk = inti->mchk;
927 break;
928 default:
929 return -EINVAL;
930 }
931
932 if (copy_to_user(uptr, &irq, sizeof(irq)))
933 return -EFAULT;
934
935 return 0;
936}
937
938static int get_all_floating_irqs(struct kvm *kvm, __u8 *buf, __u64 len)
939{
940 struct kvm_s390_interrupt_info *inti;
941 struct kvm_s390_float_interrupt *fi;
942 int ret = 0;
943 int n = 0;
944
945 mutex_lock(&kvm->lock);
946 fi = &kvm->arch.float_int;
947 spin_lock(&fi->lock);
948
949 list_for_each_entry(inti, &fi->list, list) {
950 if (len < sizeof(struct kvm_s390_irq)) {
951 /* signal userspace to try again */
952 ret = -ENOMEM;
953 break;
954 }
955 ret = copy_irq_to_user(inti, buf);
956 if (ret)
957 break;
958 buf += sizeof(struct kvm_s390_irq);
959 len -= sizeof(struct kvm_s390_irq);
960 n++;
961 }
962
963 spin_unlock(&fi->lock);
964 mutex_unlock(&kvm->lock);
965
966 return ret < 0 ? ret : n;
967}
968
969static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
970{
971 int r;
972
973 switch (attr->group) {
974 case KVM_DEV_FLIC_GET_ALL_IRQS:
975 r = get_all_floating_irqs(dev->kvm, (u8 *) attr->addr,
976 attr->attr);
977 break;
978 default:
979 r = -EINVAL;
980 }
981
982 return r;
983}
984
985static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
986 u64 addr)
987{
988 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
989 void *target = NULL;
990 void __user *source;
991 u64 size;
992
993 if (get_user(inti->type, (u64 __user *)addr))
994 return -EFAULT;
995
996 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +0200997 case KVM_S390_INT_PFAULT_INIT:
998 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +0200999 case KVM_S390_INT_VIRTIO:
1000 case KVM_S390_INT_SERVICE:
1001 target = (void *) &inti->ext;
1002 source = &uptr->u.ext;
1003 size = sizeof(inti->ext);
1004 break;
1005 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1006 target = (void *) &inti->io;
1007 source = &uptr->u.io;
1008 size = sizeof(inti->io);
1009 break;
1010 case KVM_S390_MCHK:
1011 target = (void *) &inti->mchk;
1012 source = &uptr->u.mchk;
1013 size = sizeof(inti->mchk);
1014 break;
1015 default:
1016 return -EINVAL;
1017 }
1018
1019 if (copy_from_user(target, source, size))
1020 return -EFAULT;
1021
1022 return 0;
1023}
1024
1025static int enqueue_floating_irq(struct kvm_device *dev,
1026 struct kvm_device_attr *attr)
1027{
1028 struct kvm_s390_interrupt_info *inti = NULL;
1029 int r = 0;
1030 int len = attr->attr;
1031
1032 if (len % sizeof(struct kvm_s390_irq) != 0)
1033 return -EINVAL;
1034 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1035 return -EINVAL;
1036
1037 while (len >= sizeof(struct kvm_s390_irq)) {
1038 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1039 if (!inti)
1040 return -ENOMEM;
1041
1042 r = copy_irq_from_user(inti, attr->addr);
1043 if (r) {
1044 kfree(inti);
1045 return r;
1046 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001047 r = __inject_vm(dev->kvm, inti);
1048 if (r) {
1049 kfree(inti);
1050 return r;
1051 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001052 len -= sizeof(struct kvm_s390_irq);
1053 attr->addr += sizeof(struct kvm_s390_irq);
1054 }
1055
1056 return r;
1057}
1058
1059static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1060{
1061 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001062 unsigned int i;
1063 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02001064
1065 switch (attr->group) {
1066 case KVM_DEV_FLIC_ENQUEUE:
1067 r = enqueue_floating_irq(dev, attr);
1068 break;
1069 case KVM_DEV_FLIC_CLEAR_IRQS:
1070 r = 0;
1071 clear_floating_interrupts(dev->kvm);
1072 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001073 case KVM_DEV_FLIC_APF_ENABLE:
1074 dev->kvm->arch.gmap->pfault_enabled = 1;
1075 break;
1076 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
1077 dev->kvm->arch.gmap->pfault_enabled = 0;
1078 /*
1079 * Make sure no async faults are in transition when
1080 * clearing the queues. So we don't need to worry
1081 * about late coming workers.
1082 */
1083 synchronize_srcu(&dev->kvm->srcu);
1084 kvm_for_each_vcpu(i, vcpu, dev->kvm)
1085 kvm_clear_async_pf_completion_queue(vcpu);
1086 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001087 default:
1088 r = -EINVAL;
1089 }
1090
1091 return r;
1092}
1093
1094static int flic_create(struct kvm_device *dev, u32 type)
1095{
1096 if (!dev)
1097 return -EINVAL;
1098 if (dev->kvm->arch.flic)
1099 return -EINVAL;
1100 dev->kvm->arch.flic = dev;
1101 return 0;
1102}
1103
1104static void flic_destroy(struct kvm_device *dev)
1105{
1106 dev->kvm->arch.flic = NULL;
1107 kfree(dev);
1108}
1109
1110/* s390 floating irq controller (flic) */
1111struct kvm_device_ops kvm_flic_ops = {
1112 .name = "kvm-flic",
1113 .get_attr = flic_get_attr,
1114 .set_attr = flic_set_attr,
1115 .create = flic_create,
1116 .destroy = flic_destroy,
1117};