Commit | Line | Data |
---|---|---|
ba5c1e9b | 1 | /* |
a53c8fab | 2 | * handling kvm guest interrupts |
ba5c1e9b | 3 | * |
841b91c5 | 4 | * Copyright IBM Corp. 2008,2014 |
ba5c1e9b CO |
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 | ||
ca872302 | 13 | #include <linux/interrupt.h> |
ba5c1e9b | 14 | #include <linux/kvm_host.h> |
cbb870c8 | 15 | #include <linux/hrtimer.h> |
84223598 | 16 | #include <linux/mmu_context.h> |
3cd61299 | 17 | #include <linux/signal.h> |
5a0e3ad6 | 18 | #include <linux/slab.h> |
cbb870c8 HC |
19 | #include <asm/asm-offsets.h> |
20 | #include <asm/uaccess.h> | |
ba5c1e9b CO |
21 | #include "kvm-s390.h" |
22 | #include "gaccess.h" | |
ade38c31 | 23 | #include "trace-s390.h" |
ba5c1e9b | 24 | |
d8346b7d CH |
25 | #define IOINT_SCHID_MASK 0x0000ffff |
26 | #define IOINT_SSID_MASK 0x00030000 | |
27 | #define IOINT_CSSID_MASK 0x03fc0000 | |
28 | #define IOINT_AI_MASK 0x04000000 | |
29 | ||
30 | static int is_ioint(u64 type) | |
31 | { | |
32 | return ((type & 0xfffe0000u) != 0xfffe0000u); | |
33 | } | |
34 | ||
3c038e6b | 35 | int psw_extint_disabled(struct kvm_vcpu *vcpu) |
ba5c1e9b CO |
36 | { |
37 | return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT); | |
38 | } | |
39 | ||
d8346b7d CH |
40 | static int psw_ioint_disabled(struct kvm_vcpu *vcpu) |
41 | { | |
42 | return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO); | |
43 | } | |
44 | ||
48a3e950 CH |
45 | static int psw_mchk_disabled(struct kvm_vcpu *vcpu) |
46 | { | |
47 | return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK); | |
48 | } | |
49 | ||
ba5c1e9b CO |
50 | static int psw_interrupts_disabled(struct kvm_vcpu *vcpu) |
51 | { | |
52 | if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER) || | |
53 | (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO) || | |
54 | (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT)) | |
55 | return 0; | |
56 | return 1; | |
57 | } | |
58 | ||
bb78c5ec DH |
59 | static int ckc_interrupts_enabled(struct kvm_vcpu *vcpu) |
60 | { | |
61 | if (psw_extint_disabled(vcpu) || | |
62 | !(vcpu->arch.sie_block->gcr[0] & 0x800ul)) | |
63 | return 0; | |
64 | return 1; | |
65 | } | |
66 | ||
79fd50c6 CH |
67 | static u64 int_word_to_isc_bits(u32 int_word) |
68 | { | |
69 | u8 isc = (int_word & 0x38000000) >> 27; | |
70 | ||
71 | return (0x80 >> isc) << 24; | |
72 | } | |
73 | ||
ba5c1e9b | 74 | static int __interrupt_is_deliverable(struct kvm_vcpu *vcpu, |
180c12fb | 75 | struct kvm_s390_interrupt_info *inti) |
ba5c1e9b CO |
76 | { |
77 | switch (inti->type) { | |
7697e71f CE |
78 | case KVM_S390_INT_EXTERNAL_CALL: |
79 | if (psw_extint_disabled(vcpu)) | |
80 | return 0; | |
81 | if (vcpu->arch.sie_block->gcr[0] & 0x2000ul) | |
82 | return 1; | |
ba5c1e9b CO |
83 | case KVM_S390_INT_EMERGENCY: |
84 | if (psw_extint_disabled(vcpu)) | |
85 | return 0; | |
86 | if (vcpu->arch.sie_block->gcr[0] & 0x4000ul) | |
87 | return 1; | |
88 | return 0; | |
89 | case KVM_S390_INT_SERVICE: | |
3c038e6b DD |
90 | case KVM_S390_INT_PFAULT_INIT: |
91 | case KVM_S390_INT_PFAULT_DONE: | |
ba5c1e9b CO |
92 | case KVM_S390_INT_VIRTIO: |
93 | if (psw_extint_disabled(vcpu)) | |
94 | return 0; | |
95 | if (vcpu->arch.sie_block->gcr[0] & 0x200ul) | |
96 | return 1; | |
97 | return 0; | |
98 | case KVM_S390_PROGRAM_INT: | |
99 | case KVM_S390_SIGP_STOP: | |
100 | case KVM_S390_SIGP_SET_PREFIX: | |
101 | case KVM_S390_RESTART: | |
102 | return 1; | |
48a3e950 CH |
103 | case KVM_S390_MCHK: |
104 | if (psw_mchk_disabled(vcpu)) | |
105 | return 0; | |
106 | if (vcpu->arch.sie_block->gcr[14] & inti->mchk.cr14) | |
107 | return 1; | |
108 | return 0; | |
d8346b7d CH |
109 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
110 | if (psw_ioint_disabled(vcpu)) | |
111 | return 0; | |
79fd50c6 CH |
112 | if (vcpu->arch.sie_block->gcr[6] & |
113 | int_word_to_isc_bits(inti->io.io_int_word)) | |
d8346b7d CH |
114 | return 1; |
115 | return 0; | |
ba5c1e9b | 116 | default: |
d8346b7d CH |
117 | printk(KERN_WARNING "illegal interrupt type %llx\n", |
118 | inti->type); | |
ba5c1e9b CO |
119 | BUG(); |
120 | } | |
121 | return 0; | |
122 | } | |
123 | ||
124 | static void __set_cpu_idle(struct kvm_vcpu *vcpu) | |
125 | { | |
ba5c1e9b CO |
126 | atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags); |
127 | set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask); | |
128 | } | |
129 | ||
130 | static void __unset_cpu_idle(struct kvm_vcpu *vcpu) | |
131 | { | |
ba5c1e9b CO |
132 | atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags); |
133 | clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask); | |
134 | } | |
135 | ||
136 | static void __reset_intercept_indicators(struct kvm_vcpu *vcpu) | |
137 | { | |
138 | atomic_clear_mask(CPUSTAT_ECALL_PEND | | |
139 | CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT, | |
140 | &vcpu->arch.sie_block->cpuflags); | |
141 | vcpu->arch.sie_block->lctl = 0x0000; | |
27291e21 DH |
142 | vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT); |
143 | ||
144 | if (guestdbg_enabled(vcpu)) { | |
145 | vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 | | |
146 | LCTL_CR10 | LCTL_CR11); | |
147 | vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT); | |
148 | } | |
ba5c1e9b CO |
149 | } |
150 | ||
151 | static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag) | |
152 | { | |
153 | atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags); | |
154 | } | |
155 | ||
156 | static void __set_intercept_indicator(struct kvm_vcpu *vcpu, | |
180c12fb | 157 | struct kvm_s390_interrupt_info *inti) |
ba5c1e9b CO |
158 | { |
159 | switch (inti->type) { | |
7697e71f | 160 | case KVM_S390_INT_EXTERNAL_CALL: |
ba5c1e9b CO |
161 | case KVM_S390_INT_EMERGENCY: |
162 | case KVM_S390_INT_SERVICE: | |
3c038e6b DD |
163 | case KVM_S390_INT_PFAULT_INIT: |
164 | case KVM_S390_INT_PFAULT_DONE: | |
ba5c1e9b CO |
165 | case KVM_S390_INT_VIRTIO: |
166 | if (psw_extint_disabled(vcpu)) | |
167 | __set_cpuflag(vcpu, CPUSTAT_EXT_INT); | |
168 | else | |
169 | vcpu->arch.sie_block->lctl |= LCTL_CR0; | |
170 | break; | |
171 | case KVM_S390_SIGP_STOP: | |
172 | __set_cpuflag(vcpu, CPUSTAT_STOP_INT); | |
173 | break; | |
48a3e950 CH |
174 | case KVM_S390_MCHK: |
175 | if (psw_mchk_disabled(vcpu)) | |
176 | vcpu->arch.sie_block->ictl |= ICTL_LPSW; | |
177 | else | |
178 | vcpu->arch.sie_block->lctl |= LCTL_CR14; | |
179 | break; | |
d8346b7d CH |
180 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
181 | if (psw_ioint_disabled(vcpu)) | |
182 | __set_cpuflag(vcpu, CPUSTAT_IO_INT); | |
183 | else | |
184 | vcpu->arch.sie_block->lctl |= LCTL_CR6; | |
185 | break; | |
ba5c1e9b CO |
186 | default: |
187 | BUG(); | |
188 | } | |
189 | } | |
190 | ||
8712836b DH |
191 | static int __deliver_prog_irq(struct kvm_vcpu *vcpu, |
192 | struct kvm_s390_pgm_info *pgm_info) | |
193 | { | |
194 | const unsigned short table[] = { 2, 4, 4, 6 }; | |
195 | int rc = 0; | |
196 | ||
197 | switch (pgm_info->code & ~PGM_PER) { | |
198 | case PGM_AFX_TRANSLATION: | |
199 | case PGM_ASX_TRANSLATION: | |
200 | case PGM_EX_TRANSLATION: | |
201 | case PGM_LFX_TRANSLATION: | |
202 | case PGM_LSTE_SEQUENCE: | |
203 | case PGM_LSX_TRANSLATION: | |
204 | case PGM_LX_TRANSLATION: | |
205 | case PGM_PRIMARY_AUTHORITY: | |
206 | case PGM_SECONDARY_AUTHORITY: | |
207 | case PGM_SPACE_SWITCH: | |
208 | rc = put_guest_lc(vcpu, pgm_info->trans_exc_code, | |
209 | (u64 *)__LC_TRANS_EXC_CODE); | |
210 | break; | |
211 | case PGM_ALEN_TRANSLATION: | |
212 | case PGM_ALE_SEQUENCE: | |
213 | case PGM_ASTE_INSTANCE: | |
214 | case PGM_ASTE_SEQUENCE: | |
215 | case PGM_ASTE_VALIDITY: | |
216 | case PGM_EXTENDED_AUTHORITY: | |
217 | rc = put_guest_lc(vcpu, pgm_info->exc_access_id, | |
218 | (u8 *)__LC_EXC_ACCESS_ID); | |
219 | break; | |
220 | case PGM_ASCE_TYPE: | |
221 | case PGM_PAGE_TRANSLATION: | |
222 | case PGM_REGION_FIRST_TRANS: | |
223 | case PGM_REGION_SECOND_TRANS: | |
224 | case PGM_REGION_THIRD_TRANS: | |
225 | case PGM_SEGMENT_TRANSLATION: | |
226 | rc = put_guest_lc(vcpu, pgm_info->trans_exc_code, | |
227 | (u64 *)__LC_TRANS_EXC_CODE); | |
228 | rc |= put_guest_lc(vcpu, pgm_info->exc_access_id, | |
229 | (u8 *)__LC_EXC_ACCESS_ID); | |
230 | rc |= put_guest_lc(vcpu, pgm_info->op_access_id, | |
231 | (u8 *)__LC_OP_ACCESS_ID); | |
232 | break; | |
233 | case PGM_MONITOR: | |
234 | rc = put_guest_lc(vcpu, pgm_info->mon_class_nr, | |
235 | (u64 *)__LC_MON_CLASS_NR); | |
236 | rc |= put_guest_lc(vcpu, pgm_info->mon_code, | |
237 | (u64 *)__LC_MON_CODE); | |
238 | break; | |
239 | case PGM_DATA: | |
240 | rc = put_guest_lc(vcpu, pgm_info->data_exc_code, | |
241 | (u32 *)__LC_DATA_EXC_CODE); | |
242 | break; | |
243 | case PGM_PROTECTION: | |
244 | rc = put_guest_lc(vcpu, pgm_info->trans_exc_code, | |
245 | (u64 *)__LC_TRANS_EXC_CODE); | |
246 | rc |= put_guest_lc(vcpu, pgm_info->exc_access_id, | |
247 | (u8 *)__LC_EXC_ACCESS_ID); | |
248 | break; | |
249 | } | |
250 | ||
251 | if (pgm_info->code & PGM_PER) { | |
252 | rc |= put_guest_lc(vcpu, pgm_info->per_code, | |
253 | (u8 *) __LC_PER_CODE); | |
254 | rc |= put_guest_lc(vcpu, pgm_info->per_atmid, | |
255 | (u8 *)__LC_PER_ATMID); | |
256 | rc |= put_guest_lc(vcpu, pgm_info->per_address, | |
257 | (u64 *) __LC_PER_ADDRESS); | |
258 | rc |= put_guest_lc(vcpu, pgm_info->per_access_id, | |
259 | (u8 *) __LC_PER_ACCESS_ID); | |
260 | } | |
261 | ||
262 | switch (vcpu->arch.sie_block->icptcode) { | |
263 | case ICPT_INST: | |
264 | case ICPT_INSTPROGI: | |
265 | case ICPT_OPEREXC: | |
266 | case ICPT_PARTEXEC: | |
267 | case ICPT_IOINST: | |
268 | /* last instruction only stored for these icptcodes */ | |
269 | rc |= put_guest_lc(vcpu, table[vcpu->arch.sie_block->ipa >> 14], | |
270 | (u16 *) __LC_PGM_ILC); | |
271 | break; | |
272 | case ICPT_PROGI: | |
273 | rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->pgmilc, | |
274 | (u16 *) __LC_PGM_ILC); | |
275 | break; | |
276 | default: | |
277 | rc |= put_guest_lc(vcpu, 0, | |
278 | (u16 *) __LC_PGM_ILC); | |
279 | } | |
280 | ||
281 | rc |= put_guest_lc(vcpu, pgm_info->code, | |
282 | (u16 *)__LC_PGM_INT_CODE); | |
283 | rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW, | |
284 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
285 | rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW, | |
286 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
287 | ||
288 | return rc; | |
289 | } | |
290 | ||
ba5c1e9b | 291 | static void __do_deliver_interrupt(struct kvm_vcpu *vcpu, |
180c12fb | 292 | struct kvm_s390_interrupt_info *inti) |
ba5c1e9b CO |
293 | { |
294 | const unsigned short table[] = { 2, 4, 4, 6 }; | |
dc5008b9 | 295 | int rc = 0; |
ba5c1e9b CO |
296 | |
297 | switch (inti->type) { | |
298 | case KVM_S390_INT_EMERGENCY: | |
299 | VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg"); | |
300 | vcpu->stat.deliver_emergency_signal++; | |
ade38c31 CH |
301 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
302 | inti->emerg.code, 0); | |
1a03b764 JF |
303 | rc = put_guest_lc(vcpu, 0x1201, (u16 *)__LC_EXT_INT_CODE); |
304 | rc |= put_guest_lc(vcpu, inti->emerg.code, | |
305 | (u16 *)__LC_EXT_CPU_ADDR); | |
306 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, | |
307 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
308 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, | |
dc5008b9 | 309 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
ba5c1e9b | 310 | break; |
7697e71f CE |
311 | case KVM_S390_INT_EXTERNAL_CALL: |
312 | VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call"); | |
313 | vcpu->stat.deliver_external_call++; | |
ade38c31 CH |
314 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
315 | inti->extcall.code, 0); | |
1a03b764 JF |
316 | rc = put_guest_lc(vcpu, 0x1202, (u16 *)__LC_EXT_INT_CODE); |
317 | rc |= put_guest_lc(vcpu, inti->extcall.code, | |
318 | (u16 *)__LC_EXT_CPU_ADDR); | |
319 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, | |
320 | &vcpu->arch.sie_block->gpsw, | |
321 | sizeof(psw_t)); | |
322 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, | |
323 | &vcpu->arch.sie_block->gpsw, | |
324 | sizeof(psw_t)); | |
7697e71f | 325 | break; |
ba5c1e9b CO |
326 | case KVM_S390_INT_SERVICE: |
327 | VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x", | |
328 | inti->ext.ext_params); | |
329 | vcpu->stat.deliver_service_signal++; | |
ade38c31 CH |
330 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
331 | inti->ext.ext_params, 0); | |
7988276d HC |
332 | rc = put_guest_lc(vcpu, 0x2401, (u16 *)__LC_EXT_INT_CODE); |
333 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, | |
334 | &vcpu->arch.sie_block->gpsw, | |
335 | sizeof(psw_t)); | |
336 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, | |
dc5008b9 | 337 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
7988276d HC |
338 | rc |= put_guest_lc(vcpu, inti->ext.ext_params, |
339 | (u32 *)__LC_EXT_PARAMS); | |
ba5c1e9b | 340 | break; |
3c038e6b DD |
341 | case KVM_S390_INT_PFAULT_INIT: |
342 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0, | |
343 | inti->ext.ext_params2); | |
1a03b764 JF |
344 | rc = put_guest_lc(vcpu, 0x2603, (u16 *) __LC_EXT_INT_CODE); |
345 | rc |= put_guest_lc(vcpu, 0x0600, (u16 *) __LC_EXT_CPU_ADDR); | |
346 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, | |
347 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
348 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, | |
3c038e6b | 349 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
1a03b764 JF |
350 | rc |= put_guest_lc(vcpu, inti->ext.ext_params2, |
351 | (u64 *) __LC_EXT_PARAMS2); | |
3c038e6b DD |
352 | break; |
353 | case KVM_S390_INT_PFAULT_DONE: | |
354 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0, | |
355 | inti->ext.ext_params2); | |
7988276d HC |
356 | rc = put_guest_lc(vcpu, 0x2603, (u16 *)__LC_EXT_INT_CODE); |
357 | rc |= put_guest_lc(vcpu, 0x0680, (u16 *)__LC_EXT_CPU_ADDR); | |
358 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, | |
359 | &vcpu->arch.sie_block->gpsw, | |
360 | sizeof(psw_t)); | |
361 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, | |
3c038e6b | 362 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
7988276d HC |
363 | rc |= put_guest_lc(vcpu, inti->ext.ext_params2, |
364 | (u64 *)__LC_EXT_PARAMS2); | |
3c038e6b | 365 | break; |
ba5c1e9b | 366 | case KVM_S390_INT_VIRTIO: |
33e19115 | 367 | VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx", |
ba5c1e9b CO |
368 | inti->ext.ext_params, inti->ext.ext_params2); |
369 | vcpu->stat.deliver_virtio_interrupt++; | |
ade38c31 CH |
370 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
371 | inti->ext.ext_params, | |
372 | inti->ext.ext_params2); | |
7988276d HC |
373 | rc = put_guest_lc(vcpu, 0x2603, (u16 *)__LC_EXT_INT_CODE); |
374 | rc |= put_guest_lc(vcpu, 0x0d00, (u16 *)__LC_EXT_CPU_ADDR); | |
375 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, | |
376 | &vcpu->arch.sie_block->gpsw, | |
377 | sizeof(psw_t)); | |
378 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, | |
dc5008b9 | 379 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
7988276d HC |
380 | rc |= put_guest_lc(vcpu, inti->ext.ext_params, |
381 | (u32 *)__LC_EXT_PARAMS); | |
382 | rc |= put_guest_lc(vcpu, inti->ext.ext_params2, | |
383 | (u64 *)__LC_EXT_PARAMS2); | |
ba5c1e9b | 384 | break; |
ba5c1e9b CO |
385 | case KVM_S390_SIGP_STOP: |
386 | VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu stop"); | |
387 | vcpu->stat.deliver_stop_signal++; | |
ade38c31 CH |
388 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
389 | 0, 0); | |
ba5c1e9b CO |
390 | __set_intercept_indicator(vcpu, inti); |
391 | break; | |
392 | ||
393 | case KVM_S390_SIGP_SET_PREFIX: | |
394 | VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x", | |
395 | inti->prefix.address); | |
396 | vcpu->stat.deliver_prefix_signal++; | |
ade38c31 CH |
397 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
398 | inti->prefix.address, 0); | |
8d26cf7b | 399 | kvm_s390_set_prefix(vcpu, inti->prefix.address); |
ba5c1e9b CO |
400 | break; |
401 | ||
402 | case KVM_S390_RESTART: | |
403 | VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart"); | |
404 | vcpu->stat.deliver_restart_signal++; | |
ade38c31 CH |
405 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
406 | 0, 0); | |
1a03b764 JF |
407 | rc = write_guest_lc(vcpu, |
408 | offsetof(struct _lowcore, restart_old_psw), | |
409 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
410 | rc |= read_guest_lc(vcpu, offsetof(struct _lowcore, restart_psw), | |
411 | &vcpu->arch.sie_block->gpsw, | |
412 | sizeof(psw_t)); | |
9e6dabef | 413 | atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags); |
ba5c1e9b | 414 | break; |
ba5c1e9b CO |
415 | case KVM_S390_PROGRAM_INT: |
416 | VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x", | |
417 | inti->pgm.code, | |
418 | table[vcpu->arch.sie_block->ipa >> 14]); | |
419 | vcpu->stat.deliver_program_int++; | |
ade38c31 CH |
420 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
421 | inti->pgm.code, 0); | |
8712836b | 422 | rc = __deliver_prog_irq(vcpu, &inti->pgm); |
ba5c1e9b CO |
423 | break; |
424 | ||
48a3e950 CH |
425 | case KVM_S390_MCHK: |
426 | VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx", | |
427 | inti->mchk.mcic); | |
428 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, | |
429 | inti->mchk.cr14, | |
430 | inti->mchk.mcic); | |
dc5008b9 HC |
431 | rc = kvm_s390_vcpu_store_status(vcpu, |
432 | KVM_S390_STORE_STATUS_PREFIXED); | |
7988276d HC |
433 | rc |= put_guest_lc(vcpu, inti->mchk.mcic, (u64 *)__LC_MCCK_CODE); |
434 | rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW, | |
435 | &vcpu->arch.sie_block->gpsw, | |
436 | sizeof(psw_t)); | |
437 | rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW, | |
dc5008b9 | 438 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
48a3e950 CH |
439 | break; |
440 | ||
d8346b7d CH |
441 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
442 | { | |
443 | __u32 param0 = ((__u32)inti->io.subchannel_id << 16) | | |
444 | inti->io.subchannel_nr; | |
445 | __u64 param1 = ((__u64)inti->io.io_int_parm << 32) | | |
446 | inti->io.io_int_word; | |
447 | VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type); | |
448 | vcpu->stat.deliver_io_int++; | |
449 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, | |
450 | param0, param1); | |
7988276d HC |
451 | rc = put_guest_lc(vcpu, inti->io.subchannel_id, |
452 | (u16 *)__LC_SUBCHANNEL_ID); | |
453 | rc |= put_guest_lc(vcpu, inti->io.subchannel_nr, | |
454 | (u16 *)__LC_SUBCHANNEL_NR); | |
455 | rc |= put_guest_lc(vcpu, inti->io.io_int_parm, | |
456 | (u32 *)__LC_IO_INT_PARM); | |
457 | rc |= put_guest_lc(vcpu, inti->io.io_int_word, | |
458 | (u32 *)__LC_IO_INT_WORD); | |
459 | rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW, | |
460 | &vcpu->arch.sie_block->gpsw, | |
461 | sizeof(psw_t)); | |
462 | rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW, | |
463 | &vcpu->arch.sie_block->gpsw, | |
464 | sizeof(psw_t)); | |
d8346b7d CH |
465 | break; |
466 | } | |
ba5c1e9b CO |
467 | default: |
468 | BUG(); | |
469 | } | |
dc5008b9 | 470 | if (rc) { |
3cd61299 | 471 | printk("kvm: The guest lowcore is not mapped during interrupt " |
dc5008b9 | 472 | "delivery, killing userspace\n"); |
3cd61299 | 473 | do_exit(SIGKILL); |
ba5c1e9b CO |
474 | } |
475 | } | |
476 | ||
bb78c5ec | 477 | static void deliver_ckc_interrupt(struct kvm_vcpu *vcpu) |
ba5c1e9b | 478 | { |
dc5008b9 | 479 | int rc; |
ba5c1e9b | 480 | |
1a03b764 JF |
481 | rc = put_guest_lc(vcpu, 0x1004, (u16 __user *)__LC_EXT_INT_CODE); |
482 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, | |
483 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
484 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, | |
485 | &vcpu->arch.sie_block->gpsw, | |
486 | sizeof(psw_t)); | |
dc5008b9 | 487 | if (rc) { |
3cd61299 CB |
488 | printk("kvm: The guest lowcore is not mapped during interrupt " |
489 | "delivery, killing userspace\n"); | |
490 | do_exit(SIGKILL); | |
ba5c1e9b | 491 | } |
ba5c1e9b CO |
492 | } |
493 | ||
3c038e6b | 494 | int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu) |
ba5c1e9b | 495 | { |
180c12fb CB |
496 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
497 | struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int; | |
498 | struct kvm_s390_interrupt_info *inti; | |
ba5c1e9b CO |
499 | int rc = 0; |
500 | ||
501 | if (atomic_read(&li->active)) { | |
502 | spin_lock_bh(&li->lock); | |
503 | list_for_each_entry(inti, &li->list, list) | |
504 | if (__interrupt_is_deliverable(vcpu, inti)) { | |
505 | rc = 1; | |
506 | break; | |
507 | } | |
508 | spin_unlock_bh(&li->lock); | |
509 | } | |
510 | ||
511 | if ((!rc) && atomic_read(&fi->active)) { | |
b037a4f3 | 512 | spin_lock(&fi->lock); |
ba5c1e9b CO |
513 | list_for_each_entry(inti, &fi->list, list) |
514 | if (__interrupt_is_deliverable(vcpu, inti)) { | |
515 | rc = 1; | |
516 | break; | |
517 | } | |
b037a4f3 | 518 | spin_unlock(&fi->lock); |
ba5c1e9b CO |
519 | } |
520 | ||
bb78c5ec DH |
521 | if (!rc && kvm_cpu_has_pending_timer(vcpu)) |
522 | rc = 1; | |
ba5c1e9b CO |
523 | |
524 | return rc; | |
525 | } | |
526 | ||
3d80840d MT |
527 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) |
528 | { | |
bb78c5ec DH |
529 | if (!(vcpu->arch.sie_block->ckc < |
530 | get_tod_clock_fast() + vcpu->arch.sie_block->epoch)) | |
531 | return 0; | |
532 | if (!ckc_interrupts_enabled(vcpu)) | |
533 | return 0; | |
534 | return 1; | |
3d80840d MT |
535 | } |
536 | ||
ba5c1e9b CO |
537 | int kvm_s390_handle_wait(struct kvm_vcpu *vcpu) |
538 | { | |
539 | u64 now, sltime; | |
540 | DECLARE_WAITQUEUE(wait, current); | |
541 | ||
542 | vcpu->stat.exit_wait_state++; | |
543 | if (kvm_cpu_has_interrupt(vcpu)) | |
544 | return 0; | |
545 | ||
e52b2af5 CO |
546 | __set_cpu_idle(vcpu); |
547 | spin_lock_bh(&vcpu->arch.local_int.lock); | |
548 | vcpu->arch.local_int.timer_due = 0; | |
549 | spin_unlock_bh(&vcpu->arch.local_int.lock); | |
550 | ||
ba5c1e9b CO |
551 | if (psw_interrupts_disabled(vcpu)) { |
552 | VCPU_EVENT(vcpu, 3, "%s", "disabled wait"); | |
553 | __unset_cpu_idle(vcpu); | |
b8e660b8 | 554 | return -EOPNOTSUPP; /* disabled wait */ |
ba5c1e9b CO |
555 | } |
556 | ||
bb78c5ec | 557 | if (!ckc_interrupts_enabled(vcpu)) { |
ba5c1e9b CO |
558 | VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer"); |
559 | goto no_timer; | |
560 | } | |
561 | ||
8c071b0f | 562 | now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch; |
ba5c1e9b CO |
563 | if (vcpu->arch.sie_block->ckc < now) { |
564 | __unset_cpu_idle(vcpu); | |
565 | return 0; | |
566 | } | |
567 | ||
ed4f2094 | 568 | sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now); |
ba5c1e9b | 569 | |
ca872302 CB |
570 | hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL); |
571 | VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime); | |
ba5c1e9b | 572 | no_timer: |
800c1065 | 573 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
b037a4f3 | 574 | spin_lock(&vcpu->arch.local_int.float_int->lock); |
ba5c1e9b | 575 | spin_lock_bh(&vcpu->arch.local_int.lock); |
d0321a24 | 576 | add_wait_queue(&vcpu->wq, &wait); |
ba5c1e9b CO |
577 | while (list_empty(&vcpu->arch.local_int.list) && |
578 | list_empty(&vcpu->arch.local_int.float_int->list) && | |
579 | (!vcpu->arch.local_int.timer_due) && | |
580 | !signal_pending(current)) { | |
581 | set_current_state(TASK_INTERRUPTIBLE); | |
582 | spin_unlock_bh(&vcpu->arch.local_int.lock); | |
b037a4f3 | 583 | spin_unlock(&vcpu->arch.local_int.float_int->lock); |
ba5c1e9b | 584 | schedule(); |
b037a4f3 | 585 | spin_lock(&vcpu->arch.local_int.float_int->lock); |
ba5c1e9b CO |
586 | spin_lock_bh(&vcpu->arch.local_int.lock); |
587 | } | |
588 | __unset_cpu_idle(vcpu); | |
589 | __set_current_state(TASK_RUNNING); | |
d0321a24 | 590 | remove_wait_queue(&vcpu->wq, &wait); |
ba5c1e9b | 591 | spin_unlock_bh(&vcpu->arch.local_int.lock); |
b037a4f3 | 592 | spin_unlock(&vcpu->arch.local_int.float_int->lock); |
800c1065 TH |
593 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
594 | ||
ca872302 | 595 | hrtimer_try_to_cancel(&vcpu->arch.ckc_timer); |
ba5c1e9b CO |
596 | return 0; |
597 | } | |
598 | ||
ca872302 | 599 | void kvm_s390_tasklet(unsigned long parm) |
ba5c1e9b | 600 | { |
ca872302 | 601 | struct kvm_vcpu *vcpu = (struct kvm_vcpu *) parm; |
ba5c1e9b | 602 | |
ca872302 | 603 | spin_lock(&vcpu->arch.local_int.lock); |
ba5c1e9b | 604 | vcpu->arch.local_int.timer_due = 1; |
d0321a24 CB |
605 | if (waitqueue_active(&vcpu->wq)) |
606 | wake_up_interruptible(&vcpu->wq); | |
ca872302 | 607 | spin_unlock(&vcpu->arch.local_int.lock); |
ba5c1e9b CO |
608 | } |
609 | ||
ca872302 CB |
610 | /* |
611 | * low level hrtimer wake routine. Because this runs in hardirq context | |
612 | * we schedule a tasklet to do the real work. | |
613 | */ | |
614 | enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer) | |
615 | { | |
616 | struct kvm_vcpu *vcpu; | |
617 | ||
618 | vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer); | |
9cac38dd | 619 | vcpu->preempted = true; |
ca872302 CB |
620 | tasklet_schedule(&vcpu->arch.tasklet); |
621 | ||
622 | return HRTIMER_NORESTART; | |
623 | } | |
ba5c1e9b | 624 | |
2ed10cc1 JF |
625 | void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu) |
626 | { | |
627 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; | |
628 | struct kvm_s390_interrupt_info *n, *inti = NULL; | |
629 | ||
630 | spin_lock_bh(&li->lock); | |
631 | list_for_each_entry_safe(inti, n, &li->list, list) { | |
632 | list_del(&inti->list); | |
633 | kfree(inti); | |
634 | } | |
635 | atomic_set(&li->active, 0); | |
636 | spin_unlock_bh(&li->lock); | |
637 | } | |
638 | ||
ba5c1e9b CO |
639 | void kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu) |
640 | { | |
180c12fb CB |
641 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
642 | struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int; | |
643 | struct kvm_s390_interrupt_info *n, *inti = NULL; | |
ba5c1e9b CO |
644 | int deliver; |
645 | ||
646 | __reset_intercept_indicators(vcpu); | |
647 | if (atomic_read(&li->active)) { | |
648 | do { | |
649 | deliver = 0; | |
650 | spin_lock_bh(&li->lock); | |
651 | list_for_each_entry_safe(inti, n, &li->list, list) { | |
652 | if (__interrupt_is_deliverable(vcpu, inti)) { | |
653 | list_del(&inti->list); | |
654 | deliver = 1; | |
655 | break; | |
656 | } | |
657 | __set_intercept_indicator(vcpu, inti); | |
658 | } | |
659 | if (list_empty(&li->list)) | |
660 | atomic_set(&li->active, 0); | |
661 | spin_unlock_bh(&li->lock); | |
662 | if (deliver) { | |
663 | __do_deliver_interrupt(vcpu, inti); | |
664 | kfree(inti); | |
665 | } | |
666 | } while (deliver); | |
667 | } | |
668 | ||
bb78c5ec DH |
669 | if (kvm_cpu_has_pending_timer(vcpu)) |
670 | deliver_ckc_interrupt(vcpu); | |
ba5c1e9b CO |
671 | |
672 | if (atomic_read(&fi->active)) { | |
673 | do { | |
674 | deliver = 0; | |
b037a4f3 | 675 | spin_lock(&fi->lock); |
ba5c1e9b CO |
676 | list_for_each_entry_safe(inti, n, &fi->list, list) { |
677 | if (__interrupt_is_deliverable(vcpu, inti)) { | |
678 | list_del(&inti->list); | |
a91b8ebe | 679 | fi->irq_count--; |
ba5c1e9b CO |
680 | deliver = 1; |
681 | break; | |
682 | } | |
683 | __set_intercept_indicator(vcpu, inti); | |
684 | } | |
685 | if (list_empty(&fi->list)) | |
686 | atomic_set(&fi->active, 0); | |
b037a4f3 | 687 | spin_unlock(&fi->lock); |
ba5c1e9b CO |
688 | if (deliver) { |
689 | __do_deliver_interrupt(vcpu, inti); | |
690 | kfree(inti); | |
691 | } | |
692 | } while (deliver); | |
693 | } | |
694 | } | |
695 | ||
48a3e950 CH |
696 | void kvm_s390_deliver_pending_machine_checks(struct kvm_vcpu *vcpu) |
697 | { | |
698 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; | |
699 | struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int; | |
700 | struct kvm_s390_interrupt_info *n, *inti = NULL; | |
701 | int deliver; | |
702 | ||
703 | __reset_intercept_indicators(vcpu); | |
704 | if (atomic_read(&li->active)) { | |
705 | do { | |
706 | deliver = 0; | |
707 | spin_lock_bh(&li->lock); | |
708 | list_for_each_entry_safe(inti, n, &li->list, list) { | |
709 | if ((inti->type == KVM_S390_MCHK) && | |
710 | __interrupt_is_deliverable(vcpu, inti)) { | |
711 | list_del(&inti->list); | |
712 | deliver = 1; | |
713 | break; | |
714 | } | |
715 | __set_intercept_indicator(vcpu, inti); | |
716 | } | |
717 | if (list_empty(&li->list)) | |
718 | atomic_set(&li->active, 0); | |
719 | spin_unlock_bh(&li->lock); | |
720 | if (deliver) { | |
721 | __do_deliver_interrupt(vcpu, inti); | |
722 | kfree(inti); | |
723 | } | |
724 | } while (deliver); | |
725 | } | |
726 | ||
727 | if (atomic_read(&fi->active)) { | |
728 | do { | |
729 | deliver = 0; | |
730 | spin_lock(&fi->lock); | |
731 | list_for_each_entry_safe(inti, n, &fi->list, list) { | |
732 | if ((inti->type == KVM_S390_MCHK) && | |
733 | __interrupt_is_deliverable(vcpu, inti)) { | |
734 | list_del(&inti->list); | |
a91b8ebe | 735 | fi->irq_count--; |
48a3e950 CH |
736 | deliver = 1; |
737 | break; | |
738 | } | |
739 | __set_intercept_indicator(vcpu, inti); | |
740 | } | |
741 | if (list_empty(&fi->list)) | |
742 | atomic_set(&fi->active, 0); | |
743 | spin_unlock(&fi->lock); | |
744 | if (deliver) { | |
745 | __do_deliver_interrupt(vcpu, inti); | |
746 | kfree(inti); | |
747 | } | |
748 | } while (deliver); | |
749 | } | |
750 | } | |
751 | ||
ba5c1e9b CO |
752 | int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code) |
753 | { | |
180c12fb CB |
754 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
755 | struct kvm_s390_interrupt_info *inti; | |
ba5c1e9b CO |
756 | |
757 | inti = kzalloc(sizeof(*inti), GFP_KERNEL); | |
758 | if (!inti) | |
759 | return -ENOMEM; | |
760 | ||
a419aef8 | 761 | inti->type = KVM_S390_PROGRAM_INT; |
ba5c1e9b CO |
762 | inti->pgm.code = code; |
763 | ||
764 | VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code); | |
ade38c31 | 765 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, inti->type, code, 0, 1); |
ba5c1e9b CO |
766 | spin_lock_bh(&li->lock); |
767 | list_add(&inti->list, &li->list); | |
768 | atomic_set(&li->active, 1); | |
d0321a24 | 769 | BUG_ON(waitqueue_active(li->wq)); |
ba5c1e9b | 770 | spin_unlock_bh(&li->lock); |
bcd84683 JF |
771 | return 0; |
772 | } | |
773 | ||
774 | int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu, | |
775 | struct kvm_s390_pgm_info *pgm_info) | |
776 | { | |
777 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; | |
778 | struct kvm_s390_interrupt_info *inti; | |
779 | ||
780 | inti = kzalloc(sizeof(*inti), GFP_KERNEL); | |
781 | if (!inti) | |
782 | return -ENOMEM; | |
783 | ||
784 | VCPU_EVENT(vcpu, 3, "inject: prog irq %d (from kernel)", | |
785 | pgm_info->code); | |
786 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT, | |
787 | pgm_info->code, 0, 1); | |
788 | ||
789 | inti->type = KVM_S390_PROGRAM_INT; | |
790 | memcpy(&inti->pgm, pgm_info, sizeof(inti->pgm)); | |
791 | spin_lock_bh(&li->lock); | |
792 | list_add(&inti->list, &li->list); | |
793 | atomic_set(&li->active, 1); | |
794 | BUG_ON(waitqueue_active(li->wq)); | |
795 | spin_unlock_bh(&li->lock); | |
ba5c1e9b CO |
796 | return 0; |
797 | } | |
798 | ||
fa6b7fe9 CH |
799 | struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm, |
800 | u64 cr6, u64 schid) | |
801 | { | |
802 | struct kvm_s390_float_interrupt *fi; | |
803 | struct kvm_s390_interrupt_info *inti, *iter; | |
804 | ||
805 | if ((!schid && !cr6) || (schid && cr6)) | |
806 | return NULL; | |
807 | mutex_lock(&kvm->lock); | |
808 | fi = &kvm->arch.float_int; | |
809 | spin_lock(&fi->lock); | |
810 | inti = NULL; | |
811 | list_for_each_entry(iter, &fi->list, list) { | |
812 | if (!is_ioint(iter->type)) | |
813 | continue; | |
79fd50c6 CH |
814 | if (cr6 && |
815 | ((cr6 & int_word_to_isc_bits(iter->io.io_int_word)) == 0)) | |
fa6b7fe9 CH |
816 | continue; |
817 | if (schid) { | |
818 | if (((schid & 0x00000000ffff0000) >> 16) != | |
819 | iter->io.subchannel_id) | |
820 | continue; | |
821 | if ((schid & 0x000000000000ffff) != | |
822 | iter->io.subchannel_nr) | |
823 | continue; | |
824 | } | |
825 | inti = iter; | |
826 | break; | |
827 | } | |
a91b8ebe | 828 | if (inti) { |
fa6b7fe9 | 829 | list_del_init(&inti->list); |
a91b8ebe JF |
830 | fi->irq_count--; |
831 | } | |
fa6b7fe9 CH |
832 | if (list_empty(&fi->list)) |
833 | atomic_set(&fi->active, 0); | |
834 | spin_unlock(&fi->lock); | |
835 | mutex_unlock(&kvm->lock); | |
836 | return inti; | |
837 | } | |
ba5c1e9b | 838 | |
a91b8ebe | 839 | static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti) |
ba5c1e9b | 840 | { |
180c12fb CB |
841 | struct kvm_s390_local_interrupt *li; |
842 | struct kvm_s390_float_interrupt *fi; | |
c05c4186 | 843 | struct kvm_s390_interrupt_info *iter; |
1ee0bc55 | 844 | struct kvm_vcpu *dst_vcpu = NULL; |
ba5c1e9b | 845 | int sigcpu; |
a91b8ebe | 846 | int rc = 0; |
ba5c1e9b | 847 | |
c05c4186 JF |
848 | mutex_lock(&kvm->lock); |
849 | fi = &kvm->arch.float_int; | |
850 | spin_lock(&fi->lock); | |
a91b8ebe JF |
851 | if (fi->irq_count >= KVM_S390_MAX_FLOAT_IRQS) { |
852 | rc = -EINVAL; | |
853 | goto unlock_fi; | |
854 | } | |
855 | fi->irq_count++; | |
c05c4186 JF |
856 | if (!is_ioint(inti->type)) { |
857 | list_add_tail(&inti->list, &fi->list); | |
858 | } else { | |
859 | u64 isc_bits = int_word_to_isc_bits(inti->io.io_int_word); | |
860 | ||
861 | /* Keep I/O interrupts sorted in isc order. */ | |
862 | list_for_each_entry(iter, &fi->list, list) { | |
863 | if (!is_ioint(iter->type)) | |
864 | continue; | |
865 | if (int_word_to_isc_bits(iter->io.io_int_word) | |
866 | <= isc_bits) | |
867 | continue; | |
868 | break; | |
869 | } | |
870 | list_add_tail(&inti->list, &iter->list); | |
871 | } | |
872 | atomic_set(&fi->active, 1); | |
873 | sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS); | |
874 | if (sigcpu == KVM_MAX_VCPUS) { | |
875 | do { | |
876 | sigcpu = fi->next_rr_cpu++; | |
877 | if (sigcpu == KVM_MAX_VCPUS) | |
878 | sigcpu = fi->next_rr_cpu = 0; | |
1ee0bc55 | 879 | } while (kvm_get_vcpu(kvm, sigcpu) == NULL); |
c05c4186 | 880 | } |
1ee0bc55 JF |
881 | dst_vcpu = kvm_get_vcpu(kvm, sigcpu); |
882 | li = &dst_vcpu->arch.local_int; | |
c05c4186 JF |
883 | spin_lock_bh(&li->lock); |
884 | atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags); | |
885 | if (waitqueue_active(li->wq)) | |
886 | wake_up_interruptible(li->wq); | |
9cac38dd | 887 | kvm_get_vcpu(kvm, sigcpu)->preempted = true; |
c05c4186 | 888 | spin_unlock_bh(&li->lock); |
a91b8ebe | 889 | unlock_fi: |
c05c4186 JF |
890 | spin_unlock(&fi->lock); |
891 | mutex_unlock(&kvm->lock); | |
a91b8ebe | 892 | return rc; |
c05c4186 JF |
893 | } |
894 | ||
895 | int kvm_s390_inject_vm(struct kvm *kvm, | |
896 | struct kvm_s390_interrupt *s390int) | |
897 | { | |
898 | struct kvm_s390_interrupt_info *inti; | |
899 | ||
ba5c1e9b CO |
900 | inti = kzalloc(sizeof(*inti), GFP_KERNEL); |
901 | if (!inti) | |
902 | return -ENOMEM; | |
903 | ||
c05c4186 JF |
904 | inti->type = s390int->type; |
905 | switch (inti->type) { | |
ba5c1e9b | 906 | case KVM_S390_INT_VIRTIO: |
33e19115 | 907 | VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx", |
ba5c1e9b | 908 | s390int->parm, s390int->parm64); |
ba5c1e9b CO |
909 | inti->ext.ext_params = s390int->parm; |
910 | inti->ext.ext_params2 = s390int->parm64; | |
911 | break; | |
912 | case KVM_S390_INT_SERVICE: | |
913 | VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm); | |
ba5c1e9b CO |
914 | inti->ext.ext_params = s390int->parm; |
915 | break; | |
3c038e6b DD |
916 | case KVM_S390_INT_PFAULT_DONE: |
917 | inti->type = s390int->type; | |
918 | inti->ext.ext_params2 = s390int->parm64; | |
919 | break; | |
48a3e950 CH |
920 | case KVM_S390_MCHK: |
921 | VM_EVENT(kvm, 5, "inject: machine check parm64:%llx", | |
922 | s390int->parm64); | |
48a3e950 CH |
923 | inti->mchk.cr14 = s390int->parm; /* upper bits are not used */ |
924 | inti->mchk.mcic = s390int->parm64; | |
925 | break; | |
d8346b7d | 926 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
c05c4186 | 927 | if (inti->type & IOINT_AI_MASK) |
d8346b7d CH |
928 | VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)"); |
929 | else | |
930 | VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x", | |
931 | s390int->type & IOINT_CSSID_MASK, | |
932 | s390int->type & IOINT_SSID_MASK, | |
933 | s390int->type & IOINT_SCHID_MASK); | |
d8346b7d CH |
934 | inti->io.subchannel_id = s390int->parm >> 16; |
935 | inti->io.subchannel_nr = s390int->parm & 0x0000ffffu; | |
936 | inti->io.io_int_parm = s390int->parm64 >> 32; | |
937 | inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull; | |
938 | break; | |
ba5c1e9b CO |
939 | default: |
940 | kfree(inti); | |
941 | return -EINVAL; | |
942 | } | |
ade38c31 CH |
943 | trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64, |
944 | 2); | |
ba5c1e9b | 945 | |
a91b8ebe | 946 | return __inject_vm(kvm, inti); |
ba5c1e9b CO |
947 | } |
948 | ||
2f32d4ea CH |
949 | void kvm_s390_reinject_io_int(struct kvm *kvm, |
950 | struct kvm_s390_interrupt_info *inti) | |
951 | { | |
952 | __inject_vm(kvm, inti); | |
953 | } | |
954 | ||
ba5c1e9b CO |
955 | int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, |
956 | struct kvm_s390_interrupt *s390int) | |
957 | { | |
180c12fb CB |
958 | struct kvm_s390_local_interrupt *li; |
959 | struct kvm_s390_interrupt_info *inti; | |
ba5c1e9b CO |
960 | |
961 | inti = kzalloc(sizeof(*inti), GFP_KERNEL); | |
962 | if (!inti) | |
963 | return -ENOMEM; | |
964 | ||
965 | switch (s390int->type) { | |
966 | case KVM_S390_PROGRAM_INT: | |
967 | if (s390int->parm & 0xffff0000) { | |
968 | kfree(inti); | |
969 | return -EINVAL; | |
970 | } | |
971 | inti->type = s390int->type; | |
972 | inti->pgm.code = s390int->parm; | |
973 | VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)", | |
974 | s390int->parm); | |
975 | break; | |
b7e6e4d3 CB |
976 | case KVM_S390_SIGP_SET_PREFIX: |
977 | inti->prefix.address = s390int->parm; | |
978 | inti->type = s390int->type; | |
979 | VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)", | |
980 | s390int->parm); | |
981 | break; | |
ba5c1e9b CO |
982 | case KVM_S390_SIGP_STOP: |
983 | case KVM_S390_RESTART: | |
82a12737 JH |
984 | VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type); |
985 | inti->type = s390int->type; | |
986 | break; | |
7697e71f | 987 | case KVM_S390_INT_EXTERNAL_CALL: |
82a12737 JH |
988 | if (s390int->parm & 0xffff0000) { |
989 | kfree(inti); | |
990 | return -EINVAL; | |
991 | } | |
992 | VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u", | |
993 | s390int->parm); | |
994 | inti->type = s390int->type; | |
995 | inti->extcall.code = s390int->parm; | |
996 | break; | |
ba5c1e9b | 997 | case KVM_S390_INT_EMERGENCY: |
82a12737 JH |
998 | if (s390int->parm & 0xffff0000) { |
999 | kfree(inti); | |
1000 | return -EINVAL; | |
1001 | } | |
1002 | VCPU_EVENT(vcpu, 3, "inject: emergency %u\n", s390int->parm); | |
ba5c1e9b | 1003 | inti->type = s390int->type; |
82a12737 | 1004 | inti->emerg.code = s390int->parm; |
ba5c1e9b | 1005 | break; |
48a3e950 CH |
1006 | case KVM_S390_MCHK: |
1007 | VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx", | |
1008 | s390int->parm64); | |
1009 | inti->type = s390int->type; | |
1010 | inti->mchk.mcic = s390int->parm64; | |
1011 | break; | |
3c038e6b DD |
1012 | case KVM_S390_INT_PFAULT_INIT: |
1013 | inti->type = s390int->type; | |
1014 | inti->ext.ext_params2 = s390int->parm64; | |
1015 | break; | |
ba5c1e9b CO |
1016 | case KVM_S390_INT_VIRTIO: |
1017 | case KVM_S390_INT_SERVICE: | |
d8346b7d | 1018 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
ba5c1e9b CO |
1019 | default: |
1020 | kfree(inti); | |
1021 | return -EINVAL; | |
1022 | } | |
ade38c31 CH |
1023 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, s390int->type, s390int->parm, |
1024 | s390int->parm64, 2); | |
ba5c1e9b CO |
1025 | |
1026 | mutex_lock(&vcpu->kvm->lock); | |
1027 | li = &vcpu->arch.local_int; | |
1028 | spin_lock_bh(&li->lock); | |
1029 | if (inti->type == KVM_S390_PROGRAM_INT) | |
1030 | list_add(&inti->list, &li->list); | |
1031 | else | |
1032 | list_add_tail(&inti->list, &li->list); | |
1033 | atomic_set(&li->active, 1); | |
1034 | if (inti->type == KVM_S390_SIGP_STOP) | |
1035 | li->action_bits |= ACTION_STOP_ON_STOP; | |
1036 | atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags); | |
d0321a24 CB |
1037 | if (waitqueue_active(&vcpu->wq)) |
1038 | wake_up_interruptible(&vcpu->wq); | |
9cac38dd | 1039 | vcpu->preempted = true; |
ba5c1e9b CO |
1040 | spin_unlock_bh(&li->lock); |
1041 | mutex_unlock(&vcpu->kvm->lock); | |
1042 | return 0; | |
1043 | } | |
c05c4186 JF |
1044 | |
1045 | static void clear_floating_interrupts(struct kvm *kvm) | |
1046 | { | |
1047 | struct kvm_s390_float_interrupt *fi; | |
1048 | struct kvm_s390_interrupt_info *n, *inti = NULL; | |
1049 | ||
1050 | mutex_lock(&kvm->lock); | |
1051 | fi = &kvm->arch.float_int; | |
1052 | spin_lock(&fi->lock); | |
1053 | list_for_each_entry_safe(inti, n, &fi->list, list) { | |
1054 | list_del(&inti->list); | |
1055 | kfree(inti); | |
1056 | } | |
a91b8ebe | 1057 | fi->irq_count = 0; |
c05c4186 JF |
1058 | atomic_set(&fi->active, 0); |
1059 | spin_unlock(&fi->lock); | |
1060 | mutex_unlock(&kvm->lock); | |
1061 | } | |
1062 | ||
1063 | static inline int copy_irq_to_user(struct kvm_s390_interrupt_info *inti, | |
1064 | u8 *addr) | |
1065 | { | |
1066 | struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr; | |
1067 | struct kvm_s390_irq irq = {0}; | |
1068 | ||
1069 | irq.type = inti->type; | |
1070 | switch (inti->type) { | |
3c038e6b DD |
1071 | case KVM_S390_INT_PFAULT_INIT: |
1072 | case KVM_S390_INT_PFAULT_DONE: | |
c05c4186 JF |
1073 | case KVM_S390_INT_VIRTIO: |
1074 | case KVM_S390_INT_SERVICE: | |
1075 | irq.u.ext = inti->ext; | |
1076 | break; | |
1077 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: | |
1078 | irq.u.io = inti->io; | |
1079 | break; | |
1080 | case KVM_S390_MCHK: | |
1081 | irq.u.mchk = inti->mchk; | |
1082 | break; | |
1083 | default: | |
1084 | return -EINVAL; | |
1085 | } | |
1086 | ||
1087 | if (copy_to_user(uptr, &irq, sizeof(irq))) | |
1088 | return -EFAULT; | |
1089 | ||
1090 | return 0; | |
1091 | } | |
1092 | ||
1093 | static int get_all_floating_irqs(struct kvm *kvm, __u8 *buf, __u64 len) | |
1094 | { | |
1095 | struct kvm_s390_interrupt_info *inti; | |
1096 | struct kvm_s390_float_interrupt *fi; | |
1097 | int ret = 0; | |
1098 | int n = 0; | |
1099 | ||
1100 | mutex_lock(&kvm->lock); | |
1101 | fi = &kvm->arch.float_int; | |
1102 | spin_lock(&fi->lock); | |
1103 | ||
1104 | list_for_each_entry(inti, &fi->list, list) { | |
1105 | if (len < sizeof(struct kvm_s390_irq)) { | |
1106 | /* signal userspace to try again */ | |
1107 | ret = -ENOMEM; | |
1108 | break; | |
1109 | } | |
1110 | ret = copy_irq_to_user(inti, buf); | |
1111 | if (ret) | |
1112 | break; | |
1113 | buf += sizeof(struct kvm_s390_irq); | |
1114 | len -= sizeof(struct kvm_s390_irq); | |
1115 | n++; | |
1116 | } | |
1117 | ||
1118 | spin_unlock(&fi->lock); | |
1119 | mutex_unlock(&kvm->lock); | |
1120 | ||
1121 | return ret < 0 ? ret : n; | |
1122 | } | |
1123 | ||
1124 | static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr) | |
1125 | { | |
1126 | int r; | |
1127 | ||
1128 | switch (attr->group) { | |
1129 | case KVM_DEV_FLIC_GET_ALL_IRQS: | |
1130 | r = get_all_floating_irqs(dev->kvm, (u8 *) attr->addr, | |
1131 | attr->attr); | |
1132 | break; | |
1133 | default: | |
1134 | r = -EINVAL; | |
1135 | } | |
1136 | ||
1137 | return r; | |
1138 | } | |
1139 | ||
1140 | static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti, | |
1141 | u64 addr) | |
1142 | { | |
1143 | struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr; | |
1144 | void *target = NULL; | |
1145 | void __user *source; | |
1146 | u64 size; | |
1147 | ||
1148 | if (get_user(inti->type, (u64 __user *)addr)) | |
1149 | return -EFAULT; | |
1150 | ||
1151 | switch (inti->type) { | |
3c038e6b DD |
1152 | case KVM_S390_INT_PFAULT_INIT: |
1153 | case KVM_S390_INT_PFAULT_DONE: | |
c05c4186 JF |
1154 | case KVM_S390_INT_VIRTIO: |
1155 | case KVM_S390_INT_SERVICE: | |
1156 | target = (void *) &inti->ext; | |
1157 | source = &uptr->u.ext; | |
1158 | size = sizeof(inti->ext); | |
1159 | break; | |
1160 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: | |
1161 | target = (void *) &inti->io; | |
1162 | source = &uptr->u.io; | |
1163 | size = sizeof(inti->io); | |
1164 | break; | |
1165 | case KVM_S390_MCHK: | |
1166 | target = (void *) &inti->mchk; | |
1167 | source = &uptr->u.mchk; | |
1168 | size = sizeof(inti->mchk); | |
1169 | break; | |
1170 | default: | |
1171 | return -EINVAL; | |
1172 | } | |
1173 | ||
1174 | if (copy_from_user(target, source, size)) | |
1175 | return -EFAULT; | |
1176 | ||
1177 | return 0; | |
1178 | } | |
1179 | ||
1180 | static int enqueue_floating_irq(struct kvm_device *dev, | |
1181 | struct kvm_device_attr *attr) | |
1182 | { | |
1183 | struct kvm_s390_interrupt_info *inti = NULL; | |
1184 | int r = 0; | |
1185 | int len = attr->attr; | |
1186 | ||
1187 | if (len % sizeof(struct kvm_s390_irq) != 0) | |
1188 | return -EINVAL; | |
1189 | else if (len > KVM_S390_FLIC_MAX_BUFFER) | |
1190 | return -EINVAL; | |
1191 | ||
1192 | while (len >= sizeof(struct kvm_s390_irq)) { | |
1193 | inti = kzalloc(sizeof(*inti), GFP_KERNEL); | |
1194 | if (!inti) | |
1195 | return -ENOMEM; | |
1196 | ||
1197 | r = copy_irq_from_user(inti, attr->addr); | |
1198 | if (r) { | |
1199 | kfree(inti); | |
1200 | return r; | |
1201 | } | |
a91b8ebe JF |
1202 | r = __inject_vm(dev->kvm, inti); |
1203 | if (r) { | |
1204 | kfree(inti); | |
1205 | return r; | |
1206 | } | |
c05c4186 JF |
1207 | len -= sizeof(struct kvm_s390_irq); |
1208 | attr->addr += sizeof(struct kvm_s390_irq); | |
1209 | } | |
1210 | ||
1211 | return r; | |
1212 | } | |
1213 | ||
841b91c5 CH |
1214 | static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id) |
1215 | { | |
1216 | if (id >= MAX_S390_IO_ADAPTERS) | |
1217 | return NULL; | |
1218 | return kvm->arch.adapters[id]; | |
1219 | } | |
1220 | ||
1221 | static int register_io_adapter(struct kvm_device *dev, | |
1222 | struct kvm_device_attr *attr) | |
1223 | { | |
1224 | struct s390_io_adapter *adapter; | |
1225 | struct kvm_s390_io_adapter adapter_info; | |
1226 | ||
1227 | if (copy_from_user(&adapter_info, | |
1228 | (void __user *)attr->addr, sizeof(adapter_info))) | |
1229 | return -EFAULT; | |
1230 | ||
1231 | if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) || | |
1232 | (dev->kvm->arch.adapters[adapter_info.id] != NULL)) | |
1233 | return -EINVAL; | |
1234 | ||
1235 | adapter = kzalloc(sizeof(*adapter), GFP_KERNEL); | |
1236 | if (!adapter) | |
1237 | return -ENOMEM; | |
1238 | ||
1239 | INIT_LIST_HEAD(&adapter->maps); | |
1240 | init_rwsem(&adapter->maps_lock); | |
1241 | atomic_set(&adapter->nr_maps, 0); | |
1242 | adapter->id = adapter_info.id; | |
1243 | adapter->isc = adapter_info.isc; | |
1244 | adapter->maskable = adapter_info.maskable; | |
1245 | adapter->masked = false; | |
1246 | adapter->swap = adapter_info.swap; | |
1247 | dev->kvm->arch.adapters[adapter->id] = adapter; | |
1248 | ||
1249 | return 0; | |
1250 | } | |
1251 | ||
1252 | int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked) | |
1253 | { | |
1254 | int ret; | |
1255 | struct s390_io_adapter *adapter = get_io_adapter(kvm, id); | |
1256 | ||
1257 | if (!adapter || !adapter->maskable) | |
1258 | return -EINVAL; | |
1259 | ret = adapter->masked; | |
1260 | adapter->masked = masked; | |
1261 | return ret; | |
1262 | } | |
1263 | ||
1264 | static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr) | |
1265 | { | |
1266 | struct s390_io_adapter *adapter = get_io_adapter(kvm, id); | |
1267 | struct s390_map_info *map; | |
1268 | int ret; | |
1269 | ||
1270 | if (!adapter || !addr) | |
1271 | return -EINVAL; | |
1272 | ||
1273 | map = kzalloc(sizeof(*map), GFP_KERNEL); | |
1274 | if (!map) { | |
1275 | ret = -ENOMEM; | |
1276 | goto out; | |
1277 | } | |
1278 | INIT_LIST_HEAD(&map->list); | |
1279 | map->guest_addr = addr; | |
1280 | map->addr = gmap_translate(addr, kvm->arch.gmap); | |
1281 | if (map->addr == -EFAULT) { | |
1282 | ret = -EFAULT; | |
1283 | goto out; | |
1284 | } | |
1285 | ret = get_user_pages_fast(map->addr, 1, 1, &map->page); | |
1286 | if (ret < 0) | |
1287 | goto out; | |
1288 | BUG_ON(ret != 1); | |
1289 | down_write(&adapter->maps_lock); | |
1290 | if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) { | |
1291 | list_add_tail(&map->list, &adapter->maps); | |
1292 | ret = 0; | |
1293 | } else { | |
1294 | put_page(map->page); | |
1295 | ret = -EINVAL; | |
1296 | } | |
1297 | up_write(&adapter->maps_lock); | |
1298 | out: | |
1299 | if (ret) | |
1300 | kfree(map); | |
1301 | return ret; | |
1302 | } | |
1303 | ||
1304 | static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr) | |
1305 | { | |
1306 | struct s390_io_adapter *adapter = get_io_adapter(kvm, id); | |
1307 | struct s390_map_info *map, *tmp; | |
1308 | int found = 0; | |
1309 | ||
1310 | if (!adapter || !addr) | |
1311 | return -EINVAL; | |
1312 | ||
1313 | down_write(&adapter->maps_lock); | |
1314 | list_for_each_entry_safe(map, tmp, &adapter->maps, list) { | |
1315 | if (map->guest_addr == addr) { | |
1316 | found = 1; | |
1317 | atomic_dec(&adapter->nr_maps); | |
1318 | list_del(&map->list); | |
1319 | put_page(map->page); | |
1320 | kfree(map); | |
1321 | break; | |
1322 | } | |
1323 | } | |
1324 | up_write(&adapter->maps_lock); | |
1325 | ||
1326 | return found ? 0 : -EINVAL; | |
1327 | } | |
1328 | ||
1329 | void kvm_s390_destroy_adapters(struct kvm *kvm) | |
1330 | { | |
1331 | int i; | |
1332 | struct s390_map_info *map, *tmp; | |
1333 | ||
1334 | for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) { | |
1335 | if (!kvm->arch.adapters[i]) | |
1336 | continue; | |
1337 | list_for_each_entry_safe(map, tmp, | |
1338 | &kvm->arch.adapters[i]->maps, list) { | |
1339 | list_del(&map->list); | |
1340 | put_page(map->page); | |
1341 | kfree(map); | |
1342 | } | |
1343 | kfree(kvm->arch.adapters[i]); | |
1344 | } | |
1345 | } | |
1346 | ||
1347 | static int modify_io_adapter(struct kvm_device *dev, | |
1348 | struct kvm_device_attr *attr) | |
1349 | { | |
1350 | struct kvm_s390_io_adapter_req req; | |
1351 | struct s390_io_adapter *adapter; | |
1352 | int ret; | |
1353 | ||
1354 | if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req))) | |
1355 | return -EFAULT; | |
1356 | ||
1357 | adapter = get_io_adapter(dev->kvm, req.id); | |
1358 | if (!adapter) | |
1359 | return -EINVAL; | |
1360 | switch (req.type) { | |
1361 | case KVM_S390_IO_ADAPTER_MASK: | |
1362 | ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask); | |
1363 | if (ret > 0) | |
1364 | ret = 0; | |
1365 | break; | |
1366 | case KVM_S390_IO_ADAPTER_MAP: | |
1367 | ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr); | |
1368 | break; | |
1369 | case KVM_S390_IO_ADAPTER_UNMAP: | |
1370 | ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr); | |
1371 | break; | |
1372 | default: | |
1373 | ret = -EINVAL; | |
1374 | } | |
1375 | ||
1376 | return ret; | |
1377 | } | |
1378 | ||
c05c4186 JF |
1379 | static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr) |
1380 | { | |
1381 | int r = 0; | |
3c038e6b DD |
1382 | unsigned int i; |
1383 | struct kvm_vcpu *vcpu; | |
c05c4186 JF |
1384 | |
1385 | switch (attr->group) { | |
1386 | case KVM_DEV_FLIC_ENQUEUE: | |
1387 | r = enqueue_floating_irq(dev, attr); | |
1388 | break; | |
1389 | case KVM_DEV_FLIC_CLEAR_IRQS: | |
1390 | r = 0; | |
1391 | clear_floating_interrupts(dev->kvm); | |
1392 | break; | |
3c038e6b DD |
1393 | case KVM_DEV_FLIC_APF_ENABLE: |
1394 | dev->kvm->arch.gmap->pfault_enabled = 1; | |
1395 | break; | |
1396 | case KVM_DEV_FLIC_APF_DISABLE_WAIT: | |
1397 | dev->kvm->arch.gmap->pfault_enabled = 0; | |
1398 | /* | |
1399 | * Make sure no async faults are in transition when | |
1400 | * clearing the queues. So we don't need to worry | |
1401 | * about late coming workers. | |
1402 | */ | |
1403 | synchronize_srcu(&dev->kvm->srcu); | |
1404 | kvm_for_each_vcpu(i, vcpu, dev->kvm) | |
1405 | kvm_clear_async_pf_completion_queue(vcpu); | |
1406 | break; | |
841b91c5 CH |
1407 | case KVM_DEV_FLIC_ADAPTER_REGISTER: |
1408 | r = register_io_adapter(dev, attr); | |
1409 | break; | |
1410 | case KVM_DEV_FLIC_ADAPTER_MODIFY: | |
1411 | r = modify_io_adapter(dev, attr); | |
1412 | break; | |
c05c4186 JF |
1413 | default: |
1414 | r = -EINVAL; | |
1415 | } | |
1416 | ||
1417 | return r; | |
1418 | } | |
1419 | ||
1420 | static int flic_create(struct kvm_device *dev, u32 type) | |
1421 | { | |
1422 | if (!dev) | |
1423 | return -EINVAL; | |
1424 | if (dev->kvm->arch.flic) | |
1425 | return -EINVAL; | |
1426 | dev->kvm->arch.flic = dev; | |
1427 | return 0; | |
1428 | } | |
1429 | ||
1430 | static void flic_destroy(struct kvm_device *dev) | |
1431 | { | |
1432 | dev->kvm->arch.flic = NULL; | |
1433 | kfree(dev); | |
1434 | } | |
1435 | ||
1436 | /* s390 floating irq controller (flic) */ | |
1437 | struct kvm_device_ops kvm_flic_ops = { | |
1438 | .name = "kvm-flic", | |
1439 | .get_attr = flic_get_attr, | |
1440 | .set_attr = flic_set_attr, | |
1441 | .create = flic_create, | |
1442 | .destroy = flic_destroy, | |
1443 | }; | |
84223598 CH |
1444 | |
1445 | static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap) | |
1446 | { | |
1447 | unsigned long bit; | |
1448 | ||
1449 | bit = bit_nr + (addr % PAGE_SIZE) * 8; | |
1450 | ||
1451 | return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit; | |
1452 | } | |
1453 | ||
1454 | static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter, | |
1455 | u64 addr) | |
1456 | { | |
1457 | struct s390_map_info *map; | |
1458 | ||
1459 | if (!adapter) | |
1460 | return NULL; | |
1461 | ||
1462 | list_for_each_entry(map, &adapter->maps, list) { | |
1463 | if (map->guest_addr == addr) | |
1464 | return map; | |
1465 | } | |
1466 | return NULL; | |
1467 | } | |
1468 | ||
1469 | static int adapter_indicators_set(struct kvm *kvm, | |
1470 | struct s390_io_adapter *adapter, | |
1471 | struct kvm_s390_adapter_int *adapter_int) | |
1472 | { | |
1473 | unsigned long bit; | |
1474 | int summary_set, idx; | |
1475 | struct s390_map_info *info; | |
1476 | void *map; | |
1477 | ||
1478 | info = get_map_info(adapter, adapter_int->ind_addr); | |
1479 | if (!info) | |
1480 | return -1; | |
1481 | map = page_address(info->page); | |
1482 | bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap); | |
1483 | set_bit(bit, map); | |
1484 | idx = srcu_read_lock(&kvm->srcu); | |
1485 | mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT); | |
1486 | set_page_dirty_lock(info->page); | |
1487 | info = get_map_info(adapter, adapter_int->summary_addr); | |
1488 | if (!info) { | |
1489 | srcu_read_unlock(&kvm->srcu, idx); | |
1490 | return -1; | |
1491 | } | |
1492 | map = page_address(info->page); | |
1493 | bit = get_ind_bit(info->addr, adapter_int->summary_offset, | |
1494 | adapter->swap); | |
1495 | summary_set = test_and_set_bit(bit, map); | |
1496 | mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT); | |
1497 | set_page_dirty_lock(info->page); | |
1498 | srcu_read_unlock(&kvm->srcu, idx); | |
1499 | return summary_set ? 0 : 1; | |
1500 | } | |
1501 | ||
1502 | /* | |
1503 | * < 0 - not injected due to error | |
1504 | * = 0 - coalesced, summary indicator already active | |
1505 | * > 0 - injected interrupt | |
1506 | */ | |
1507 | static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e, | |
1508 | struct kvm *kvm, int irq_source_id, int level, | |
1509 | bool line_status) | |
1510 | { | |
1511 | int ret; | |
1512 | struct s390_io_adapter *adapter; | |
1513 | ||
1514 | /* We're only interested in the 0->1 transition. */ | |
1515 | if (!level) | |
1516 | return 0; | |
1517 | adapter = get_io_adapter(kvm, e->adapter.adapter_id); | |
1518 | if (!adapter) | |
1519 | return -1; | |
1520 | down_read(&adapter->maps_lock); | |
1521 | ret = adapter_indicators_set(kvm, adapter, &e->adapter); | |
1522 | up_read(&adapter->maps_lock); | |
1523 | if ((ret > 0) && !adapter->masked) { | |
1524 | struct kvm_s390_interrupt s390int = { | |
1525 | .type = KVM_S390_INT_IO(1, 0, 0, 0), | |
1526 | .parm = 0, | |
1527 | .parm64 = (adapter->isc << 27) | 0x80000000, | |
1528 | }; | |
1529 | ret = kvm_s390_inject_vm(kvm, &s390int); | |
1530 | if (ret == 0) | |
1531 | ret = 1; | |
1532 | } | |
1533 | return ret; | |
1534 | } | |
1535 | ||
1536 | int kvm_set_routing_entry(struct kvm_irq_routing_table *rt, | |
1537 | struct kvm_kernel_irq_routing_entry *e, | |
1538 | const struct kvm_irq_routing_entry *ue) | |
1539 | { | |
1540 | int ret; | |
1541 | ||
1542 | switch (ue->type) { | |
1543 | case KVM_IRQ_ROUTING_S390_ADAPTER: | |
1544 | e->set = set_adapter_int; | |
1545 | e->adapter.summary_addr = ue->u.adapter.summary_addr; | |
1546 | e->adapter.ind_addr = ue->u.adapter.ind_addr; | |
1547 | e->adapter.summary_offset = ue->u.adapter.summary_offset; | |
1548 | e->adapter.ind_offset = ue->u.adapter.ind_offset; | |
1549 | e->adapter.adapter_id = ue->u.adapter.adapter_id; | |
1550 | ret = 0; | |
1551 | break; | |
1552 | default: | |
1553 | ret = -EINVAL; | |
1554 | } | |
1555 | ||
1556 | return ret; | |
1557 | } | |
1558 | ||
1559 | int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm, | |
1560 | int irq_source_id, int level, bool line_status) | |
1561 | { | |
1562 | return -EINVAL; | |
1563 | } |