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