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