]>
Commit | Line | Data |
---|---|---|
d809aa23 | 1 | // SPDX-License-Identifier: GPL-2.0 |
ba5c1e9b | 2 | /* |
a53c8fab | 3 | * handling kvm guest interrupts |
ba5c1e9b | 4 | * |
f6547066 | 5 | * Copyright IBM Corp. 2008, 2020 |
ba5c1e9b | 6 | * |
ba5c1e9b CO |
7 | * Author(s): Carsten Otte <cotte@de.ibm.com> |
8 | */ | |
9 | ||
1282c21e MM |
10 | #define KMSG_COMPONENT "kvm-s390" |
11 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt | |
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> |
58616e6a | 17 | #include <linux/nospec.h> |
3cd61299 | 18 | #include <linux/signal.h> |
5a0e3ad6 | 19 | #include <linux/slab.h> |
383d0b05 | 20 | #include <linux/bitmap.h> |
94aa033e | 21 | #include <linux/vmalloc.h> |
cbb870c8 | 22 | #include <asm/asm-offsets.h> |
33b412ac | 23 | #include <asm/dis.h> |
7c0f6ba6 | 24 | #include <linux/uaccess.h> |
ea5f4969 | 25 | #include <asm/sclp.h> |
6d3da241 | 26 | #include <asm/isc.h> |
1e133ab2 | 27 | #include <asm/gmap.h> |
0319dae6 | 28 | #include <asm/switch_to.h> |
ff5dc149 | 29 | #include <asm/nmi.h> |
9f30f621 | 30 | #include <asm/airq.h> |
ba5c1e9b CO |
31 | #include "kvm-s390.h" |
32 | #include "gaccess.h" | |
ade38c31 | 33 | #include "trace-s390.h" |
ba5c1e9b | 34 | |
44c6ca3d | 35 | #define PFAULT_INIT 0x0600 |
60f90a14 JF |
36 | #define PFAULT_DONE 0x0680 |
37 | #define VIRTIO_PARAM 0x0d00 | |
d8346b7d | 38 | |
1282c21e MM |
39 | static struct kvm_s390_gib *gib; |
40 | ||
a5bd7647 ED |
41 | /* handle external calls via sigp interpretation facility */ |
42 | static int sca_ext_call_pending(struct kvm_vcpu *vcpu, int *src_id) | |
43 | { | |
7d43bafc ED |
44 | int c, scn; |
45 | ||
8d5fb0dc | 46 | if (!kvm_s390_test_cpuflags(vcpu, CPUSTAT_ECALL_PEND)) |
2c1bb2be DH |
47 | return 0; |
48 | ||
a6940674 | 49 | BUG_ON(!kvm_s390_use_sca_entries()); |
5e044315 | 50 | read_lock(&vcpu->kvm->arch.sca_lock); |
7d43bafc ED |
51 | if (vcpu->kvm->arch.use_esca) { |
52 | struct esca_block *sca = vcpu->kvm->arch.sca; | |
53 | union esca_sigp_ctrl sigp_ctrl = | |
54 | sca->cpu[vcpu->vcpu_id].sigp_ctrl; | |
55 | ||
56 | c = sigp_ctrl.c; | |
57 | scn = sigp_ctrl.scn; | |
58 | } else { | |
59 | struct bsca_block *sca = vcpu->kvm->arch.sca; | |
60 | union bsca_sigp_ctrl sigp_ctrl = | |
61 | sca->cpu[vcpu->vcpu_id].sigp_ctrl; | |
62 | ||
63 | c = sigp_ctrl.c; | |
64 | scn = sigp_ctrl.scn; | |
65 | } | |
5e044315 | 66 | read_unlock(&vcpu->kvm->arch.sca_lock); |
a5bd7647 ED |
67 | |
68 | if (src_id) | |
7d43bafc | 69 | *src_id = scn; |
a5bd7647 | 70 | |
2c1bb2be | 71 | return c; |
a5bd7647 ED |
72 | } |
73 | ||
74 | static int sca_inject_ext_call(struct kvm_vcpu *vcpu, int src_id) | |
75 | { | |
bc784cce | 76 | int expect, rc; |
a5bd7647 | 77 | |
a6940674 | 78 | BUG_ON(!kvm_s390_use_sca_entries()); |
5e044315 | 79 | read_lock(&vcpu->kvm->arch.sca_lock); |
7d43bafc ED |
80 | if (vcpu->kvm->arch.use_esca) { |
81 | struct esca_block *sca = vcpu->kvm->arch.sca; | |
82 | union esca_sigp_ctrl *sigp_ctrl = | |
83 | &(sca->cpu[vcpu->vcpu_id].sigp_ctrl); | |
84 | union esca_sigp_ctrl new_val = {0}, old_val = *sigp_ctrl; | |
85 | ||
86 | new_val.scn = src_id; | |
87 | new_val.c = 1; | |
88 | old_val.c = 0; | |
89 | ||
90 | expect = old_val.value; | |
91 | rc = cmpxchg(&sigp_ctrl->value, old_val.value, new_val.value); | |
92 | } else { | |
93 | struct bsca_block *sca = vcpu->kvm->arch.sca; | |
94 | union bsca_sigp_ctrl *sigp_ctrl = | |
95 | &(sca->cpu[vcpu->vcpu_id].sigp_ctrl); | |
96 | union bsca_sigp_ctrl new_val = {0}, old_val = *sigp_ctrl; | |
bc784cce | 97 | |
7d43bafc ED |
98 | new_val.scn = src_id; |
99 | new_val.c = 1; | |
100 | old_val.c = 0; | |
101 | ||
102 | expect = old_val.value; | |
103 | rc = cmpxchg(&sigp_ctrl->value, old_val.value, new_val.value); | |
104 | } | |
5e044315 | 105 | read_unlock(&vcpu->kvm->arch.sca_lock); |
bc784cce ED |
106 | |
107 | if (rc != expect) { | |
a5bd7647 ED |
108 | /* another external call is pending */ |
109 | return -EBUSY; | |
110 | } | |
ef8f4f49 | 111 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_ECALL_PEND); |
a5bd7647 ED |
112 | return 0; |
113 | } | |
114 | ||
115 | static void sca_clear_ext_call(struct kvm_vcpu *vcpu) | |
116 | { | |
7d43bafc | 117 | int rc, expect; |
a5bd7647 | 118 | |
a6940674 DH |
119 | if (!kvm_s390_use_sca_entries()) |
120 | return; | |
9daecfc6 | 121 | kvm_s390_clear_cpuflags(vcpu, CPUSTAT_ECALL_PEND); |
5e044315 | 122 | read_lock(&vcpu->kvm->arch.sca_lock); |
7d43bafc ED |
123 | if (vcpu->kvm->arch.use_esca) { |
124 | struct esca_block *sca = vcpu->kvm->arch.sca; | |
125 | union esca_sigp_ctrl *sigp_ctrl = | |
126 | &(sca->cpu[vcpu->vcpu_id].sigp_ctrl); | |
127 | union esca_sigp_ctrl old = *sigp_ctrl; | |
128 | ||
129 | expect = old.value; | |
130 | rc = cmpxchg(&sigp_ctrl->value, old.value, 0); | |
131 | } else { | |
132 | struct bsca_block *sca = vcpu->kvm->arch.sca; | |
133 | union bsca_sigp_ctrl *sigp_ctrl = | |
134 | &(sca->cpu[vcpu->vcpu_id].sigp_ctrl); | |
135 | union bsca_sigp_ctrl old = *sigp_ctrl; | |
136 | ||
137 | expect = old.value; | |
138 | rc = cmpxchg(&sigp_ctrl->value, old.value, 0); | |
139 | } | |
5e044315 | 140 | read_unlock(&vcpu->kvm->arch.sca_lock); |
7d43bafc | 141 | WARN_ON(rc != expect); /* cannot clear? */ |
a5bd7647 ED |
142 | } |
143 | ||
3c038e6b | 144 | int psw_extint_disabled(struct kvm_vcpu *vcpu) |
ba5c1e9b CO |
145 | { |
146 | return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT); | |
147 | } | |
148 | ||
d8346b7d CH |
149 | static int psw_ioint_disabled(struct kvm_vcpu *vcpu) |
150 | { | |
151 | return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO); | |
152 | } | |
153 | ||
48a3e950 CH |
154 | static int psw_mchk_disabled(struct kvm_vcpu *vcpu) |
155 | { | |
156 | return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK); | |
157 | } | |
158 | ||
ba5c1e9b CO |
159 | static int psw_interrupts_disabled(struct kvm_vcpu *vcpu) |
160 | { | |
fee0e0fd DH |
161 | return psw_extint_disabled(vcpu) && |
162 | psw_ioint_disabled(vcpu) && | |
163 | psw_mchk_disabled(vcpu); | |
ba5c1e9b CO |
164 | } |
165 | ||
bb78c5ec DH |
166 | static int ckc_interrupts_enabled(struct kvm_vcpu *vcpu) |
167 | { | |
168 | if (psw_extint_disabled(vcpu) || | |
b9224cd7 | 169 | !(vcpu->arch.sie_block->gcr[0] & CR0_CLOCK_COMPARATOR_SUBMASK)) |
bb78c5ec | 170 | return 0; |
f71d0dc5 DH |
171 | if (guestdbg_enabled(vcpu) && guestdbg_sstep_enabled(vcpu)) |
172 | /* No timer interrupts when single stepping */ | |
173 | return 0; | |
bb78c5ec DH |
174 | return 1; |
175 | } | |
176 | ||
b4aec925 DH |
177 | static int ckc_irq_pending(struct kvm_vcpu *vcpu) |
178 | { | |
5fe01793 DH |
179 | const u64 now = kvm_s390_get_tod_clock_fast(vcpu->kvm); |
180 | const u64 ckc = vcpu->arch.sie_block->ckc; | |
181 | ||
b9224cd7 | 182 | if (vcpu->arch.sie_block->gcr[0] & CR0_CLOCK_COMPARATOR_SIGN) { |
5fe01793 DH |
183 | if ((s64)ckc >= (s64)now) |
184 | return 0; | |
185 | } else if (ckc >= now) { | |
b4aec925 | 186 | return 0; |
5fe01793 | 187 | } |
b4aec925 DH |
188 | return ckc_interrupts_enabled(vcpu); |
189 | } | |
190 | ||
191 | static int cpu_timer_interrupts_enabled(struct kvm_vcpu *vcpu) | |
192 | { | |
193 | return !psw_extint_disabled(vcpu) && | |
b9224cd7 | 194 | (vcpu->arch.sie_block->gcr[0] & CR0_CPU_TIMER_SUBMASK); |
b4aec925 DH |
195 | } |
196 | ||
197 | static int cpu_timer_irq_pending(struct kvm_vcpu *vcpu) | |
198 | { | |
4287f247 DH |
199 | if (!cpu_timer_interrupts_enabled(vcpu)) |
200 | return 0; | |
201 | return kvm_s390_get_cpu_timer(vcpu) >> 63; | |
b4aec925 DH |
202 | } |
203 | ||
6d3da241 JF |
204 | static uint64_t isc_to_isc_bits(int isc) |
205 | { | |
79fd50c6 CH |
206 | return (0x80 >> isc) << 24; |
207 | } | |
208 | ||
2496c8e7 MM |
209 | static inline u32 isc_to_int_word(u8 isc) |
210 | { | |
211 | return ((u32)isc << 27) | 0x80000000; | |
212 | } | |
213 | ||
6d3da241 | 214 | static inline u8 int_word_to_isc(u32 int_word) |
ba5c1e9b | 215 | { |
6d3da241 JF |
216 | return (int_word & 0x38000000) >> 27; |
217 | } | |
218 | ||
d77e6414 MM |
219 | /* |
220 | * To use atomic bitmap functions, we have to provide a bitmap address | |
221 | * that is u64 aligned. However, the ipm might be u32 aligned. | |
222 | * Therefore, we logically start the bitmap at the very beginning of the | |
223 | * struct and fixup the bit number. | |
224 | */ | |
225 | #define IPM_BIT_OFFSET (offsetof(struct kvm_s390_gisa, ipm) * BITS_PER_BYTE) | |
226 | ||
6cff2e10 MM |
227 | /** |
228 | * gisa_set_iam - change the GISA interruption alert mask | |
229 | * | |
230 | * @gisa: gisa to operate on | |
231 | * @iam: new IAM value to use | |
232 | * | |
233 | * Change the IAM atomically with the next alert address and the IPM | |
234 | * of the GISA if the GISA is not part of the GIB alert list. All three | |
235 | * fields are located in the first long word of the GISA. | |
236 | * | |
237 | * Returns: 0 on success | |
238 | * -EBUSY in case the gisa is part of the alert list | |
239 | */ | |
240 | static inline int gisa_set_iam(struct kvm_s390_gisa *gisa, u8 iam) | |
241 | { | |
242 | u64 word, _word; | |
243 | ||
244 | do { | |
245 | word = READ_ONCE(gisa->u64.word[0]); | |
246 | if ((u64)gisa != word >> 32) | |
247 | return -EBUSY; | |
248 | _word = (word & ~0xffUL) | iam; | |
249 | } while (cmpxchg(&gisa->u64.word[0], word, _word) != word); | |
250 | ||
251 | return 0; | |
252 | } | |
253 | ||
174dd4f8 MM |
254 | /** |
255 | * gisa_clear_ipm - clear the GISA interruption pending mask | |
256 | * | |
257 | * @gisa: gisa to operate on | |
258 | * | |
259 | * Clear the IPM atomically with the next alert address and the IAM | |
260 | * of the GISA unconditionally. All three fields are located in the | |
261 | * first long word of the GISA. | |
262 | */ | |
263 | static inline void gisa_clear_ipm(struct kvm_s390_gisa *gisa) | |
264 | { | |
265 | u64 word, _word; | |
266 | ||
267 | do { | |
268 | word = READ_ONCE(gisa->u64.word[0]); | |
269 | _word = word & ~(0xffUL << 24); | |
270 | } while (cmpxchg(&gisa->u64.word[0], word, _word) != word); | |
271 | } | |
272 | ||
9f30f621 MM |
273 | /** |
274 | * gisa_get_ipm_or_restore_iam - return IPM or restore GISA IAM | |
275 | * | |
276 | * @gi: gisa interrupt struct to work on | |
277 | * | |
278 | * Atomically restores the interruption alert mask if none of the | |
279 | * relevant ISCs are pending and return the IPM. | |
280 | * | |
281 | * Returns: the relevant pending ISCs | |
282 | */ | |
283 | static inline u8 gisa_get_ipm_or_restore_iam(struct kvm_s390_gisa_interrupt *gi) | |
284 | { | |
285 | u8 pending_mask, alert_mask; | |
286 | u64 word, _word; | |
287 | ||
288 | do { | |
289 | word = READ_ONCE(gi->origin->u64.word[0]); | |
290 | alert_mask = READ_ONCE(gi->alert.mask); | |
291 | pending_mask = (u8)(word >> 24) & alert_mask; | |
292 | if (pending_mask) | |
293 | return pending_mask; | |
294 | _word = (word & ~0xffUL) | alert_mask; | |
295 | } while (cmpxchg(&gi->origin->u64.word[0], word, _word) != word); | |
296 | ||
297 | return 0; | |
298 | } | |
299 | ||
300 | static inline int gisa_in_alert_list(struct kvm_s390_gisa *gisa) | |
301 | { | |
302 | return READ_ONCE(gisa->next_alert) != (u32)(u64)gisa; | |
303 | } | |
304 | ||
bb2fb8cd | 305 | static inline void gisa_set_ipm_gisc(struct kvm_s390_gisa *gisa, u32 gisc) |
d77e6414 MM |
306 | { |
307 | set_bit_inv(IPM_BIT_OFFSET + gisc, (unsigned long *) gisa); | |
308 | } | |
309 | ||
bb2fb8cd | 310 | static inline u8 gisa_get_ipm(struct kvm_s390_gisa *gisa) |
d77e6414 MM |
311 | { |
312 | return READ_ONCE(gisa->ipm); | |
313 | } | |
314 | ||
bb2fb8cd | 315 | static inline void gisa_clear_ipm_gisc(struct kvm_s390_gisa *gisa, u32 gisc) |
d77e6414 MM |
316 | { |
317 | clear_bit_inv(IPM_BIT_OFFSET + gisc, (unsigned long *) gisa); | |
318 | } | |
319 | ||
bb2fb8cd | 320 | static inline int gisa_tac_ipm_gisc(struct kvm_s390_gisa *gisa, u32 gisc) |
d77e6414 MM |
321 | { |
322 | return test_and_clear_bit_inv(IPM_BIT_OFFSET + gisc, (unsigned long *) gisa); | |
323 | } | |
324 | ||
8846f317 | 325 | static inline unsigned long pending_irqs_no_gisa(struct kvm_vcpu *vcpu) |
6d3da241 | 326 | { |
0890ddea CB |
327 | unsigned long pending = vcpu->kvm->arch.float_int.pending_irqs | |
328 | vcpu->arch.local_int.pending_irqs; | |
329 | ||
330 | pending &= ~vcpu->kvm->arch.float_int.masked_irqs; | |
331 | return pending; | |
8846f317 CB |
332 | } |
333 | ||
334 | static inline unsigned long pending_irqs(struct kvm_vcpu *vcpu) | |
335 | { | |
b9fa6d6e MM |
336 | struct kvm_s390_gisa_interrupt *gi = &vcpu->kvm->arch.gisa_int; |
337 | unsigned long pending_mask; | |
338 | ||
339 | pending_mask = pending_irqs_no_gisa(vcpu); | |
340 | if (gi->origin) | |
341 | pending_mask |= gisa_get_ipm(gi->origin) << IRQ_PEND_IO_ISC_7; | |
342 | return pending_mask; | |
383d0b05 JF |
343 | } |
344 | ||
ee739f4b MM |
345 | static inline int isc_to_irq_type(unsigned long isc) |
346 | { | |
c7901a6e | 347 | return IRQ_PEND_IO_ISC_0 - isc; |
ee739f4b MM |
348 | } |
349 | ||
350 | static inline int irq_type_to_isc(unsigned long irq_type) | |
351 | { | |
c7901a6e | 352 | return IRQ_PEND_IO_ISC_0 - irq_type; |
ee739f4b MM |
353 | } |
354 | ||
6d3da241 JF |
355 | static unsigned long disable_iscs(struct kvm_vcpu *vcpu, |
356 | unsigned long active_mask) | |
357 | { | |
358 | int i; | |
359 | ||
360 | for (i = 0; i <= MAX_ISC; i++) | |
361 | if (!(vcpu->arch.sie_block->gcr[6] & isc_to_isc_bits(i))) | |
ee739f4b | 362 | active_mask &= ~(1UL << (isc_to_irq_type(i))); |
6d3da241 JF |
363 | |
364 | return active_mask; | |
365 | } | |
366 | ||
367 | static unsigned long deliverable_irqs(struct kvm_vcpu *vcpu) | |
383d0b05 | 368 | { |
6d3da241 JF |
369 | unsigned long active_mask; |
370 | ||
5f94c58e | 371 | active_mask = pending_irqs(vcpu); |
ffeca0ae JF |
372 | if (!active_mask) |
373 | return 0; | |
383d0b05 JF |
374 | |
375 | if (psw_extint_disabled(vcpu)) | |
376 | active_mask &= ~IRQ_PEND_EXT_MASK; | |
6d3da241 JF |
377 | if (psw_ioint_disabled(vcpu)) |
378 | active_mask &= ~IRQ_PEND_IO_MASK; | |
379 | else | |
380 | active_mask = disable_iscs(vcpu, active_mask); | |
b9224cd7 | 381 | if (!(vcpu->arch.sie_block->gcr[0] & CR0_EXTERNAL_CALL_SUBMASK)) |
383d0b05 | 382 | __clear_bit(IRQ_PEND_EXT_EXTERNAL, &active_mask); |
b9224cd7 | 383 | if (!(vcpu->arch.sie_block->gcr[0] & CR0_EMERGENCY_SIGNAL_SUBMASK)) |
383d0b05 | 384 | __clear_bit(IRQ_PEND_EXT_EMERGENCY, &active_mask); |
b9224cd7 | 385 | if (!(vcpu->arch.sie_block->gcr[0] & CR0_CLOCK_COMPARATOR_SUBMASK)) |
383d0b05 | 386 | __clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &active_mask); |
b9224cd7 | 387 | if (!(vcpu->arch.sie_block->gcr[0] & CR0_CPU_TIMER_SUBMASK)) |
383d0b05 | 388 | __clear_bit(IRQ_PEND_EXT_CPU_TIMER, &active_mask); |
0890ddea | 389 | if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK)) { |
6d3da241 | 390 | __clear_bit(IRQ_PEND_EXT_SERVICE, &active_mask); |
0890ddea CB |
391 | __clear_bit(IRQ_PEND_EXT_SERVICE_EV, &active_mask); |
392 | } | |
383d0b05 JF |
393 | if (psw_mchk_disabled(vcpu)) |
394 | active_mask &= ~IRQ_PEND_MCHK_MASK; | |
201ae986 | 395 | /* PV guest cpus can have a single interruption injected at a time. */ |
d47c4c45 | 396 | if (kvm_s390_pv_cpu_get_handle(vcpu) && |
201ae986 MM |
397 | vcpu->arch.sie_block->iictl != IICTL_CODE_NONE) |
398 | active_mask &= ~(IRQ_PEND_EXT_II_MASK | | |
399 | IRQ_PEND_IO_MASK | | |
400 | IRQ_PEND_MCHK_MASK); | |
4d62fcc0 QH |
401 | /* |
402 | * Check both floating and local interrupt's cr14 because | |
403 | * bit IRQ_PEND_MCHK_REP could be set in both cases. | |
404 | */ | |
6d3da241 | 405 | if (!(vcpu->arch.sie_block->gcr[14] & |
4d62fcc0 QH |
406 | (vcpu->kvm->arch.float_int.mchk.cr14 | |
407 | vcpu->arch.local_int.irq.mchk.cr14))) | |
6d3da241 | 408 | __clear_bit(IRQ_PEND_MCHK_REP, &active_mask); |
383d0b05 | 409 | |
6cddd432 DH |
410 | /* |
411 | * STOP irqs will never be actively delivered. They are triggered via | |
412 | * intercept requests and cleared when the stop intercept is performed. | |
413 | */ | |
414 | __clear_bit(IRQ_PEND_SIGP_STOP, &active_mask); | |
415 | ||
383d0b05 JF |
416 | return active_mask; |
417 | } | |
418 | ||
ba5c1e9b CO |
419 | static void __set_cpu_idle(struct kvm_vcpu *vcpu) |
420 | { | |
ef8f4f49 | 421 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_WAIT); |
4eeef242 | 422 | set_bit(vcpu->vcpu_idx, vcpu->kvm->arch.idle_mask); |
ba5c1e9b CO |
423 | } |
424 | ||
425 | static void __unset_cpu_idle(struct kvm_vcpu *vcpu) | |
426 | { | |
9daecfc6 | 427 | kvm_s390_clear_cpuflags(vcpu, CPUSTAT_WAIT); |
4eeef242 | 428 | clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.idle_mask); |
ba5c1e9b CO |
429 | } |
430 | ||
431 | static void __reset_intercept_indicators(struct kvm_vcpu *vcpu) | |
432 | { | |
9daecfc6 DH |
433 | kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IO_INT | CPUSTAT_EXT_INT | |
434 | CPUSTAT_STOP_INT); | |
ba5c1e9b | 435 | vcpu->arch.sie_block->lctl = 0x0000; |
27291e21 DH |
436 | vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT); |
437 | ||
438 | if (guestdbg_enabled(vcpu)) { | |
439 | vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 | | |
440 | LCTL_CR10 | LCTL_CR11); | |
441 | vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT); | |
442 | } | |
ba5c1e9b CO |
443 | } |
444 | ||
6d3da241 JF |
445 | static void set_intercept_indicators_io(struct kvm_vcpu *vcpu) |
446 | { | |
8846f317 | 447 | if (!(pending_irqs_no_gisa(vcpu) & IRQ_PEND_IO_MASK)) |
6d3da241 | 448 | return; |
b7d45571 | 449 | if (psw_ioint_disabled(vcpu)) |
2018224d | 450 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_IO_INT); |
6d3da241 JF |
451 | else |
452 | vcpu->arch.sie_block->lctl |= LCTL_CR6; | |
453 | } | |
454 | ||
383d0b05 JF |
455 | static void set_intercept_indicators_ext(struct kvm_vcpu *vcpu) |
456 | { | |
96723d32 | 457 | if (!(pending_irqs_no_gisa(vcpu) & IRQ_PEND_EXT_MASK)) |
383d0b05 JF |
458 | return; |
459 | if (psw_extint_disabled(vcpu)) | |
2018224d | 460 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT); |
383d0b05 JF |
461 | else |
462 | vcpu->arch.sie_block->lctl |= LCTL_CR0; | |
463 | } | |
464 | ||
465 | static void set_intercept_indicators_mchk(struct kvm_vcpu *vcpu) | |
466 | { | |
96723d32 | 467 | if (!(pending_irqs_no_gisa(vcpu) & IRQ_PEND_MCHK_MASK)) |
383d0b05 JF |
468 | return; |
469 | if (psw_mchk_disabled(vcpu)) | |
470 | vcpu->arch.sie_block->ictl |= ICTL_LPSW; | |
471 | else | |
472 | vcpu->arch.sie_block->lctl |= LCTL_CR14; | |
473 | } | |
474 | ||
6cddd432 DH |
475 | static void set_intercept_indicators_stop(struct kvm_vcpu *vcpu) |
476 | { | |
477 | if (kvm_s390_is_stop_irq_pending(vcpu)) | |
2018224d | 478 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT); |
6cddd432 DH |
479 | } |
480 | ||
6d3da241 JF |
481 | /* Set interception request for non-deliverable interrupts */ |
482 | static void set_intercept_indicators(struct kvm_vcpu *vcpu) | |
383d0b05 | 483 | { |
6d3da241 | 484 | set_intercept_indicators_io(vcpu); |
383d0b05 JF |
485 | set_intercept_indicators_ext(vcpu); |
486 | set_intercept_indicators_mchk(vcpu); | |
6cddd432 | 487 | set_intercept_indicators_stop(vcpu); |
383d0b05 JF |
488 | } |
489 | ||
0fb97abe JF |
490 | static int __must_check __deliver_cpu_timer(struct kvm_vcpu *vcpu) |
491 | { | |
383d0b05 | 492 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
201ae986 | 493 | int rc = 0; |
0fb97abe | 494 | |
ccc40c53 | 495 | vcpu->stat.deliver_cputm++; |
0fb97abe JF |
496 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER, |
497 | 0, 0); | |
201ae986 MM |
498 | if (kvm_s390_pv_cpu_is_protected(vcpu)) { |
499 | vcpu->arch.sie_block->iictl = IICTL_CODE_EXT; | |
500 | vcpu->arch.sie_block->eic = EXT_IRQ_CPU_TIMER; | |
501 | } else { | |
502 | rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER, | |
503 | (u16 *)__LC_EXT_INT_CODE); | |
504 | rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR); | |
505 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, | |
506 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
507 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, | |
508 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
509 | } | |
383d0b05 | 510 | clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs); |
99e20009 | 511 | return rc ? -EFAULT : 0; |
0fb97abe JF |
512 | } |
513 | ||
514 | static int __must_check __deliver_ckc(struct kvm_vcpu *vcpu) | |
515 | { | |
383d0b05 | 516 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
201ae986 | 517 | int rc = 0; |
0fb97abe | 518 | |
ccc40c53 | 519 | vcpu->stat.deliver_ckc++; |
0fb97abe JF |
520 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP, |
521 | 0, 0); | |
201ae986 MM |
522 | if (kvm_s390_pv_cpu_is_protected(vcpu)) { |
523 | vcpu->arch.sie_block->iictl = IICTL_CODE_EXT; | |
524 | vcpu->arch.sie_block->eic = EXT_IRQ_CLK_COMP; | |
525 | } else { | |
526 | rc = put_guest_lc(vcpu, EXT_IRQ_CLK_COMP, | |
527 | (u16 __user *)__LC_EXT_INT_CODE); | |
528 | rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR); | |
529 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, | |
530 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
531 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, | |
532 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
533 | } | |
383d0b05 | 534 | clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs); |
99e20009 | 535 | return rc ? -EFAULT : 0; |
0fb97abe JF |
536 | } |
537 | ||
383d0b05 | 538 | static int __must_check __deliver_pfault_init(struct kvm_vcpu *vcpu) |
0fb97abe | 539 | { |
383d0b05 JF |
540 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
541 | struct kvm_s390_ext_info ext; | |
0fb97abe JF |
542 | int rc; |
543 | ||
383d0b05 JF |
544 | spin_lock(&li->lock); |
545 | ext = li->irq.ext; | |
546 | clear_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs); | |
547 | li->irq.ext.ext_params2 = 0; | |
548 | spin_unlock(&li->lock); | |
549 | ||
3f24ba15 CB |
550 | VCPU_EVENT(vcpu, 4, "deliver: pfault init token 0x%llx", |
551 | ext.ext_params2); | |
0fb97abe JF |
552 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, |
553 | KVM_S390_INT_PFAULT_INIT, | |
383d0b05 | 554 | 0, ext.ext_params2); |
0fb97abe JF |
555 | |
556 | rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *) __LC_EXT_INT_CODE); | |
557 | rc |= put_guest_lc(vcpu, PFAULT_INIT, (u16 *) __LC_EXT_CPU_ADDR); | |
558 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, | |
559 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
560 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, | |
561 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
383d0b05 | 562 | rc |= put_guest_lc(vcpu, ext.ext_params2, (u64 *) __LC_EXT_PARAMS2); |
99e20009 | 563 | return rc ? -EFAULT : 0; |
0fb97abe JF |
564 | } |
565 | ||
d6404ded DH |
566 | static int __write_machine_check(struct kvm_vcpu *vcpu, |
567 | struct kvm_s390_mchk_info *mchk) | |
568 | { | |
569 | unsigned long ext_sa_addr; | |
4e0b1ab7 | 570 | unsigned long lc; |
0319dae6 | 571 | freg_t fprs[NUM_FPRS]; |
ff5dc149 | 572 | union mci mci; |
d6404ded DH |
573 | int rc; |
574 | ||
201ae986 MM |
575 | /* |
576 | * All other possible payload for a machine check (e.g. the register | |
577 | * contents in the save area) will be handled by the ultravisor, as | |
578 | * the hypervisor does not not have the needed information for | |
579 | * protected guests. | |
580 | */ | |
581 | if (kvm_s390_pv_cpu_is_protected(vcpu)) { | |
582 | vcpu->arch.sie_block->iictl = IICTL_CODE_MCHK; | |
583 | vcpu->arch.sie_block->mcic = mchk->mcic; | |
584 | vcpu->arch.sie_block->faddr = mchk->failing_storage_address; | |
585 | vcpu->arch.sie_block->edc = mchk->ext_damage_code; | |
586 | return 0; | |
587 | } | |
588 | ||
ff5dc149 | 589 | mci.val = mchk->mcic; |
31d8b8d4 | 590 | /* take care of lazy register loading */ |
0319dae6 DH |
591 | save_fpu_regs(); |
592 | save_access_regs(vcpu->run->s.regs.acrs); | |
80248559 CB |
593 | if (MACHINE_HAS_GS && vcpu->arch.gs_enabled) |
594 | save_gs_cb(current->thread.gs_cb); | |
0319dae6 | 595 | |
d6404ded | 596 | /* Extended save area */ |
916cda1a MS |
597 | rc = read_guest_lc(vcpu, __LC_MCESAD, &ext_sa_addr, |
598 | sizeof(unsigned long)); | |
4e0b1ab7 FZ |
599 | /* Only bits 0 through 63-LC are used for address formation */ |
600 | lc = ext_sa_addr & MCESA_LC_MASK; | |
601 | if (test_kvm_facility(vcpu->kvm, 133)) { | |
602 | switch (lc) { | |
603 | case 0: | |
604 | case 10: | |
605 | ext_sa_addr &= ~0x3ffUL; | |
606 | break; | |
607 | case 11: | |
608 | ext_sa_addr &= ~0x7ffUL; | |
609 | break; | |
610 | case 12: | |
611 | ext_sa_addr &= ~0xfffUL; | |
612 | break; | |
613 | default: | |
614 | ext_sa_addr = 0; | |
615 | break; | |
616 | } | |
617 | } else { | |
618 | ext_sa_addr &= ~0x3ffUL; | |
619 | } | |
620 | ||
ff5dc149 DH |
621 | if (!rc && mci.vr && ext_sa_addr && test_kvm_facility(vcpu->kvm, 129)) { |
622 | if (write_guest_abs(vcpu, ext_sa_addr, vcpu->run->s.regs.vrs, | |
623 | 512)) | |
624 | mci.vr = 0; | |
625 | } else { | |
626 | mci.vr = 0; | |
627 | } | |
4e0b1ab7 FZ |
628 | if (!rc && mci.gs && ext_sa_addr && test_kvm_facility(vcpu->kvm, 133) |
629 | && (lc == 11 || lc == 12)) { | |
630 | if (write_guest_abs(vcpu, ext_sa_addr + 1024, | |
631 | &vcpu->run->s.regs.gscb, 32)) | |
632 | mci.gs = 0; | |
633 | } else { | |
634 | mci.gs = 0; | |
635 | } | |
d6404ded DH |
636 | |
637 | /* General interruption information */ | |
0319dae6 | 638 | rc |= put_guest_lc(vcpu, 1, (u8 __user *) __LC_AR_MODE_ID); |
d6404ded DH |
639 | rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW, |
640 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
641 | rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW, | |
642 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
ff5dc149 | 643 | rc |= put_guest_lc(vcpu, mci.val, (u64 __user *) __LC_MCCK_CODE); |
d6404ded DH |
644 | |
645 | /* Register-save areas */ | |
0319dae6 DH |
646 | if (MACHINE_HAS_VX) { |
647 | convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs); | |
648 | rc |= write_guest_lc(vcpu, __LC_FPREGS_SAVE_AREA, fprs, 128); | |
649 | } else { | |
650 | rc |= write_guest_lc(vcpu, __LC_FPREGS_SAVE_AREA, | |
651 | vcpu->run->s.regs.fprs, 128); | |
652 | } | |
653 | rc |= write_guest_lc(vcpu, __LC_GPREGS_SAVE_AREA, | |
654 | vcpu->run->s.regs.gprs, 128); | |
655 | rc |= put_guest_lc(vcpu, current->thread.fpu.fpc, | |
656 | (u32 __user *) __LC_FP_CREG_SAVE_AREA); | |
657 | rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->todpr, | |
658 | (u32 __user *) __LC_TOD_PROGREG_SAVE_AREA); | |
659 | rc |= put_guest_lc(vcpu, kvm_s390_get_cpu_timer(vcpu), | |
660 | (u64 __user *) __LC_CPU_TIMER_SAVE_AREA); | |
661 | rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->ckc >> 8, | |
662 | (u64 __user *) __LC_CLOCK_COMP_SAVE_AREA); | |
663 | rc |= write_guest_lc(vcpu, __LC_AREGS_SAVE_AREA, | |
664 | &vcpu->run->s.regs.acrs, 64); | |
665 | rc |= write_guest_lc(vcpu, __LC_CREGS_SAVE_AREA, | |
666 | &vcpu->arch.sie_block->gcr, 128); | |
d6404ded DH |
667 | |
668 | /* Extended interruption information */ | |
8953fb08 DH |
669 | rc |= put_guest_lc(vcpu, mchk->ext_damage_code, |
670 | (u32 __user *) __LC_EXT_DAMAGE_CODE); | |
d6404ded DH |
671 | rc |= put_guest_lc(vcpu, mchk->failing_storage_address, |
672 | (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR); | |
673 | rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA, &mchk->fixed_logout, | |
674 | sizeof(mchk->fixed_logout)); | |
675 | return rc ? -EFAULT : 0; | |
676 | } | |
677 | ||
383d0b05 | 678 | static int __must_check __deliver_machine_check(struct kvm_vcpu *vcpu) |
0fb97abe | 679 | { |
6d3da241 | 680 | struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int; |
383d0b05 | 681 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
6d3da241 | 682 | struct kvm_s390_mchk_info mchk = {}; |
6d3da241 JF |
683 | int deliver = 0; |
684 | int rc = 0; | |
0fb97abe | 685 | |
6d3da241 | 686 | spin_lock(&fi->lock); |
383d0b05 | 687 | spin_lock(&li->lock); |
6d3da241 JF |
688 | if (test_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs) || |
689 | test_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs)) { | |
690 | /* | |
691 | * If there was an exigent machine check pending, then any | |
692 | * repressible machine checks that might have been pending | |
693 | * are indicated along with it, so always clear bits for | |
694 | * repressible and exigent interrupts | |
695 | */ | |
696 | mchk = li->irq.mchk; | |
697 | clear_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs); | |
698 | clear_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs); | |
699 | memset(&li->irq.mchk, 0, sizeof(mchk)); | |
700 | deliver = 1; | |
701 | } | |
383d0b05 | 702 | /* |
6d3da241 JF |
703 | * We indicate floating repressible conditions along with |
704 | * other pending conditions. Channel Report Pending and Channel | |
705 | * Subsystem damage are the only two and and are indicated by | |
706 | * bits in mcic and masked in cr14. | |
383d0b05 | 707 | */ |
6d3da241 JF |
708 | if (test_and_clear_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) { |
709 | mchk.mcic |= fi->mchk.mcic; | |
710 | mchk.cr14 |= fi->mchk.cr14; | |
711 | memset(&fi->mchk, 0, sizeof(mchk)); | |
712 | deliver = 1; | |
713 | } | |
383d0b05 | 714 | spin_unlock(&li->lock); |
6d3da241 | 715 | spin_unlock(&fi->lock); |
383d0b05 | 716 | |
6d3da241 | 717 | if (deliver) { |
3f24ba15 | 718 | VCPU_EVENT(vcpu, 3, "deliver: machine check mcic 0x%llx", |
6d3da241 JF |
719 | mchk.mcic); |
720 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, | |
721 | KVM_S390_MCHK, | |
722 | mchk.cr14, mchk.mcic); | |
32de0749 | 723 | vcpu->stat.deliver_machine_check++; |
d6404ded | 724 | rc = __write_machine_check(vcpu, &mchk); |
6d3da241 | 725 | } |
d6404ded | 726 | return rc; |
0fb97abe JF |
727 | } |
728 | ||
729 | static int __must_check __deliver_restart(struct kvm_vcpu *vcpu) | |
730 | { | |
383d0b05 | 731 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
201ae986 | 732 | int rc = 0; |
0fb97abe | 733 | |
3f24ba15 | 734 | VCPU_EVENT(vcpu, 3, "%s", "deliver: cpu restart"); |
0fb97abe JF |
735 | vcpu->stat.deliver_restart_signal++; |
736 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0); | |
737 | ||
201ae986 MM |
738 | if (kvm_s390_pv_cpu_is_protected(vcpu)) { |
739 | vcpu->arch.sie_block->iictl = IICTL_CODE_RESTART; | |
740 | } else { | |
741 | rc = write_guest_lc(vcpu, | |
742 | offsetof(struct lowcore, restart_old_psw), | |
743 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
744 | rc |= read_guest_lc(vcpu, offsetof(struct lowcore, restart_psw), | |
745 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
746 | } | |
383d0b05 | 747 | clear_bit(IRQ_PEND_RESTART, &li->pending_irqs); |
99e20009 | 748 | return rc ? -EFAULT : 0; |
0fb97abe JF |
749 | } |
750 | ||
383d0b05 | 751 | static int __must_check __deliver_set_prefix(struct kvm_vcpu *vcpu) |
0fb97abe | 752 | { |
383d0b05 JF |
753 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
754 | struct kvm_s390_prefix_info prefix; | |
755 | ||
756 | spin_lock(&li->lock); | |
757 | prefix = li->irq.prefix; | |
758 | li->irq.prefix.address = 0; | |
759 | clear_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs); | |
760 | spin_unlock(&li->lock); | |
0fb97abe | 761 | |
0fb97abe JF |
762 | vcpu->stat.deliver_prefix_signal++; |
763 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, | |
764 | KVM_S390_SIGP_SET_PREFIX, | |
383d0b05 | 765 | prefix.address, 0); |
0fb97abe | 766 | |
383d0b05 | 767 | kvm_s390_set_prefix(vcpu, prefix.address); |
0fb97abe JF |
768 | return 0; |
769 | } | |
770 | ||
383d0b05 | 771 | static int __must_check __deliver_emergency_signal(struct kvm_vcpu *vcpu) |
0fb97abe | 772 | { |
383d0b05 | 773 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
0fb97abe | 774 | int rc; |
383d0b05 JF |
775 | int cpu_addr; |
776 | ||
777 | spin_lock(&li->lock); | |
778 | cpu_addr = find_first_bit(li->sigp_emerg_pending, KVM_MAX_VCPUS); | |
779 | clear_bit(cpu_addr, li->sigp_emerg_pending); | |
780 | if (bitmap_empty(li->sigp_emerg_pending, KVM_MAX_VCPUS)) | |
781 | clear_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs); | |
782 | spin_unlock(&li->lock); | |
0fb97abe | 783 | |
3f24ba15 | 784 | VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp emerg"); |
0fb97abe | 785 | vcpu->stat.deliver_emergency_signal++; |
383d0b05 JF |
786 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY, |
787 | cpu_addr, 0); | |
201ae986 MM |
788 | if (kvm_s390_pv_cpu_is_protected(vcpu)) { |
789 | vcpu->arch.sie_block->iictl = IICTL_CODE_EXT; | |
790 | vcpu->arch.sie_block->eic = EXT_IRQ_EMERGENCY_SIG; | |
791 | vcpu->arch.sie_block->extcpuaddr = cpu_addr; | |
792 | return 0; | |
793 | } | |
0fb97abe JF |
794 | |
795 | rc = put_guest_lc(vcpu, EXT_IRQ_EMERGENCY_SIG, | |
796 | (u16 *)__LC_EXT_INT_CODE); | |
383d0b05 | 797 | rc |= put_guest_lc(vcpu, cpu_addr, (u16 *)__LC_EXT_CPU_ADDR); |
0fb97abe JF |
798 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, |
799 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
800 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, | |
801 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
99e20009 | 802 | return rc ? -EFAULT : 0; |
0fb97abe JF |
803 | } |
804 | ||
383d0b05 | 805 | static int __must_check __deliver_external_call(struct kvm_vcpu *vcpu) |
0fb97abe | 806 | { |
383d0b05 JF |
807 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
808 | struct kvm_s390_extcall_info extcall; | |
0fb97abe JF |
809 | int rc; |
810 | ||
383d0b05 JF |
811 | spin_lock(&li->lock); |
812 | extcall = li->irq.extcall; | |
813 | li->irq.extcall.code = 0; | |
814 | clear_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs); | |
815 | spin_unlock(&li->lock); | |
816 | ||
3f24ba15 | 817 | VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp ext call"); |
0fb97abe JF |
818 | vcpu->stat.deliver_external_call++; |
819 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, | |
820 | KVM_S390_INT_EXTERNAL_CALL, | |
383d0b05 | 821 | extcall.code, 0); |
201ae986 MM |
822 | if (kvm_s390_pv_cpu_is_protected(vcpu)) { |
823 | vcpu->arch.sie_block->iictl = IICTL_CODE_EXT; | |
824 | vcpu->arch.sie_block->eic = EXT_IRQ_EXTERNAL_CALL; | |
825 | vcpu->arch.sie_block->extcpuaddr = extcall.code; | |
826 | return 0; | |
827 | } | |
0fb97abe JF |
828 | |
829 | rc = put_guest_lc(vcpu, EXT_IRQ_EXTERNAL_CALL, | |
830 | (u16 *)__LC_EXT_INT_CODE); | |
383d0b05 | 831 | rc |= put_guest_lc(vcpu, extcall.code, (u16 *)__LC_EXT_CPU_ADDR); |
0fb97abe JF |
832 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, |
833 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
834 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &vcpu->arch.sie_block->gpsw, | |
835 | sizeof(psw_t)); | |
99e20009 | 836 | return rc ? -EFAULT : 0; |
0fb97abe JF |
837 | } |
838 | ||
ea5c68c3 JF |
839 | static int __deliver_prog_pv(struct kvm_vcpu *vcpu, u16 code) |
840 | { | |
841 | switch (code) { | |
842 | case PGM_SPECIFICATION: | |
843 | vcpu->arch.sie_block->iictl = IICTL_CODE_SPECIFICATION; | |
844 | break; | |
845 | case PGM_OPERAND: | |
846 | vcpu->arch.sie_block->iictl = IICTL_CODE_OPERAND; | |
847 | break; | |
848 | default: | |
849 | return -EINVAL; | |
850 | } | |
851 | return 0; | |
852 | } | |
853 | ||
383d0b05 | 854 | static int __must_check __deliver_prog(struct kvm_vcpu *vcpu) |
8712836b | 855 | { |
383d0b05 JF |
856 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
857 | struct kvm_s390_pgm_info pgm_info; | |
a9a846fd | 858 | int rc = 0, nullifying = false; |
634790b8 | 859 | u16 ilen; |
8712836b | 860 | |
383d0b05 JF |
861 | spin_lock(&li->lock); |
862 | pgm_info = li->irq.pgm; | |
863 | clear_bit(IRQ_PEND_PROG, &li->pending_irqs); | |
864 | memset(&li->irq.pgm, 0, sizeof(pgm_info)); | |
865 | spin_unlock(&li->lock); | |
866 | ||
634790b8 | 867 | ilen = pgm_info.flags & KVM_S390_PGM_FLAGS_ILC_MASK; |
0e8bc06a DH |
868 | VCPU_EVENT(vcpu, 3, "deliver: program irq code 0x%x, ilen:%d", |
869 | pgm_info.code, ilen); | |
ccc40c53 | 870 | vcpu->stat.deliver_program++; |
0fb97abe | 871 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_PROGRAM_INT, |
383d0b05 | 872 | pgm_info.code, 0); |
0fb97abe | 873 | |
ea5c68c3 JF |
874 | /* PER is handled by the ultravisor */ |
875 | if (kvm_s390_pv_cpu_is_protected(vcpu)) | |
876 | return __deliver_prog_pv(vcpu, pgm_info.code & ~PGM_PER); | |
877 | ||
383d0b05 | 878 | switch (pgm_info.code & ~PGM_PER) { |
8712836b DH |
879 | case PGM_AFX_TRANSLATION: |
880 | case PGM_ASX_TRANSLATION: | |
881 | case PGM_EX_TRANSLATION: | |
882 | case PGM_LFX_TRANSLATION: | |
883 | case PGM_LSTE_SEQUENCE: | |
884 | case PGM_LSX_TRANSLATION: | |
885 | case PGM_LX_TRANSLATION: | |
886 | case PGM_PRIMARY_AUTHORITY: | |
887 | case PGM_SECONDARY_AUTHORITY: | |
a9a846fd | 888 | nullifying = true; |
3b684a42 | 889 | fallthrough; |
8712836b | 890 | case PGM_SPACE_SWITCH: |
383d0b05 | 891 | rc = put_guest_lc(vcpu, pgm_info.trans_exc_code, |
8712836b DH |
892 | (u64 *)__LC_TRANS_EXC_CODE); |
893 | break; | |
894 | case PGM_ALEN_TRANSLATION: | |
895 | case PGM_ALE_SEQUENCE: | |
896 | case PGM_ASTE_INSTANCE: | |
897 | case PGM_ASTE_SEQUENCE: | |
898 | case PGM_ASTE_VALIDITY: | |
899 | case PGM_EXTENDED_AUTHORITY: | |
383d0b05 | 900 | rc = put_guest_lc(vcpu, pgm_info.exc_access_id, |
8712836b | 901 | (u8 *)__LC_EXC_ACCESS_ID); |
a9a846fd | 902 | nullifying = true; |
8712836b DH |
903 | break; |
904 | case PGM_ASCE_TYPE: | |
905 | case PGM_PAGE_TRANSLATION: | |
906 | case PGM_REGION_FIRST_TRANS: | |
907 | case PGM_REGION_SECOND_TRANS: | |
908 | case PGM_REGION_THIRD_TRANS: | |
909 | case PGM_SEGMENT_TRANSLATION: | |
383d0b05 | 910 | rc = put_guest_lc(vcpu, pgm_info.trans_exc_code, |
8712836b | 911 | (u64 *)__LC_TRANS_EXC_CODE); |
383d0b05 | 912 | rc |= put_guest_lc(vcpu, pgm_info.exc_access_id, |
8712836b | 913 | (u8 *)__LC_EXC_ACCESS_ID); |
383d0b05 | 914 | rc |= put_guest_lc(vcpu, pgm_info.op_access_id, |
8712836b | 915 | (u8 *)__LC_OP_ACCESS_ID); |
a9a846fd | 916 | nullifying = true; |
8712836b DH |
917 | break; |
918 | case PGM_MONITOR: | |
383d0b05 | 919 | rc = put_guest_lc(vcpu, pgm_info.mon_class_nr, |
a36c5393 | 920 | (u16 *)__LC_MON_CLASS_NR); |
383d0b05 | 921 | rc |= put_guest_lc(vcpu, pgm_info.mon_code, |
8712836b DH |
922 | (u64 *)__LC_MON_CODE); |
923 | break; | |
403c8648 | 924 | case PGM_VECTOR_PROCESSING: |
8712836b | 925 | case PGM_DATA: |
383d0b05 | 926 | rc = put_guest_lc(vcpu, pgm_info.data_exc_code, |
8712836b DH |
927 | (u32 *)__LC_DATA_EXC_CODE); |
928 | break; | |
929 | case PGM_PROTECTION: | |
383d0b05 | 930 | rc = put_guest_lc(vcpu, pgm_info.trans_exc_code, |
8712836b | 931 | (u64 *)__LC_TRANS_EXC_CODE); |
383d0b05 | 932 | rc |= put_guest_lc(vcpu, pgm_info.exc_access_id, |
8712836b DH |
933 | (u8 *)__LC_EXC_ACCESS_ID); |
934 | break; | |
a9a846fd TH |
935 | case PGM_STACK_FULL: |
936 | case PGM_STACK_EMPTY: | |
937 | case PGM_STACK_SPECIFICATION: | |
938 | case PGM_STACK_TYPE: | |
939 | case PGM_STACK_OPERATION: | |
940 | case PGM_TRACE_TABEL: | |
941 | case PGM_CRYPTO_OPERATION: | |
942 | nullifying = true; | |
943 | break; | |
8712836b DH |
944 | } |
945 | ||
383d0b05 JF |
946 | if (pgm_info.code & PGM_PER) { |
947 | rc |= put_guest_lc(vcpu, pgm_info.per_code, | |
8712836b | 948 | (u8 *) __LC_PER_CODE); |
383d0b05 | 949 | rc |= put_guest_lc(vcpu, pgm_info.per_atmid, |
8712836b | 950 | (u8 *)__LC_PER_ATMID); |
383d0b05 | 951 | rc |= put_guest_lc(vcpu, pgm_info.per_address, |
8712836b | 952 | (u64 *) __LC_PER_ADDRESS); |
383d0b05 | 953 | rc |= put_guest_lc(vcpu, pgm_info.per_access_id, |
8712836b DH |
954 | (u8 *) __LC_PER_ACCESS_ID); |
955 | } | |
956 | ||
eaa4f416 | 957 | if (nullifying && !(pgm_info.flags & KVM_S390_PGM_FLAGS_NO_REWIND)) |
0e8bc06a | 958 | kvm_s390_rewind_psw(vcpu, ilen); |
a9a846fd | 959 | |
0e8bc06a DH |
960 | /* bit 1+2 of the target are the ilc, so we can directly use ilen */ |
961 | rc |= put_guest_lc(vcpu, ilen, (u16 *) __LC_PGM_ILC); | |
2ba45968 DH |
962 | rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->gbea, |
963 | (u64 *) __LC_LAST_BREAK); | |
383d0b05 | 964 | rc |= put_guest_lc(vcpu, pgm_info.code, |
8712836b DH |
965 | (u16 *)__LC_PGM_INT_CODE); |
966 | rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW, | |
967 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
968 | rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW, | |
969 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
99e20009 | 970 | return rc ? -EFAULT : 0; |
0fb97abe JF |
971 | } |
972 | ||
0890ddea CB |
973 | #define SCCB_MASK 0xFFFFFFF8 |
974 | #define SCCB_EVENT_PENDING 0x3 | |
975 | ||
976 | static int write_sclp(struct kvm_vcpu *vcpu, u32 parm) | |
977 | { | |
978 | int rc; | |
979 | ||
980 | if (kvm_s390_pv_cpu_get_handle(vcpu)) { | |
981 | vcpu->arch.sie_block->iictl = IICTL_CODE_EXT; | |
982 | vcpu->arch.sie_block->eic = EXT_IRQ_SERVICE_SIG; | |
983 | vcpu->arch.sie_block->eiparams = parm; | |
984 | return 0; | |
985 | } | |
986 | ||
987 | rc = put_guest_lc(vcpu, EXT_IRQ_SERVICE_SIG, (u16 *)__LC_EXT_INT_CODE); | |
988 | rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR); | |
989 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, | |
990 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
991 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, | |
992 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); | |
993 | rc |= put_guest_lc(vcpu, parm, | |
994 | (u32 *)__LC_EXT_PARAMS); | |
995 | ||
996 | return rc ? -EFAULT : 0; | |
997 | } | |
998 | ||
6d3da241 | 999 | static int __must_check __deliver_service(struct kvm_vcpu *vcpu) |
0fb97abe | 1000 | { |
6d3da241 JF |
1001 | struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int; |
1002 | struct kvm_s390_ext_info ext; | |
6d3da241 JF |
1003 | |
1004 | spin_lock(&fi->lock); | |
0890ddea CB |
1005 | if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->masked_irqs) || |
1006 | !(test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs))) { | |
6d3da241 JF |
1007 | spin_unlock(&fi->lock); |
1008 | return 0; | |
1009 | } | |
1010 | ext = fi->srv_signal; | |
1011 | memset(&fi->srv_signal, 0, sizeof(ext)); | |
1012 | clear_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs); | |
0890ddea CB |
1013 | clear_bit(IRQ_PEND_EXT_SERVICE_EV, &fi->pending_irqs); |
1014 | if (kvm_s390_pv_cpu_is_protected(vcpu)) | |
1015 | set_bit(IRQ_PEND_EXT_SERVICE, &fi->masked_irqs); | |
6d3da241 | 1016 | spin_unlock(&fi->lock); |
0fb97abe | 1017 | |
3f24ba15 | 1018 | VCPU_EVENT(vcpu, 4, "deliver: sclp parameter 0x%x", |
6d3da241 | 1019 | ext.ext_params); |
0fb97abe | 1020 | vcpu->stat.deliver_service_signal++; |
6d3da241 JF |
1021 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_SERVICE, |
1022 | ext.ext_params, 0); | |
0fb97abe | 1023 | |
0890ddea CB |
1024 | return write_sclp(vcpu, ext.ext_params); |
1025 | } | |
6d3da241 | 1026 | |
0890ddea CB |
1027 | static int __must_check __deliver_service_ev(struct kvm_vcpu *vcpu) |
1028 | { | |
1029 | struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int; | |
1030 | struct kvm_s390_ext_info ext; | |
1031 | ||
1032 | spin_lock(&fi->lock); | |
1033 | if (!(test_bit(IRQ_PEND_EXT_SERVICE_EV, &fi->pending_irqs))) { | |
1034 | spin_unlock(&fi->lock); | |
1035 | return 0; | |
1036 | } | |
1037 | ext = fi->srv_signal; | |
1038 | /* only clear the event bit */ | |
1039 | fi->srv_signal.ext_params &= ~SCCB_EVENT_PENDING; | |
1040 | clear_bit(IRQ_PEND_EXT_SERVICE_EV, &fi->pending_irqs); | |
1041 | spin_unlock(&fi->lock); | |
1042 | ||
1043 | VCPU_EVENT(vcpu, 4, "%s", "deliver: sclp parameter event"); | |
1044 | vcpu->stat.deliver_service_signal++; | |
1045 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_SERVICE, | |
1046 | ext.ext_params, 0); | |
1047 | ||
1048 | return write_sclp(vcpu, SCCB_EVENT_PENDING); | |
0fb97abe JF |
1049 | } |
1050 | ||
6d3da241 | 1051 | static int __must_check __deliver_pfault_done(struct kvm_vcpu *vcpu) |
0fb97abe | 1052 | { |
6d3da241 JF |
1053 | struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int; |
1054 | struct kvm_s390_interrupt_info *inti; | |
1055 | int rc = 0; | |
0fb97abe | 1056 | |
6d3da241 JF |
1057 | spin_lock(&fi->lock); |
1058 | inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_PFAULT], | |
1059 | struct kvm_s390_interrupt_info, | |
1060 | list); | |
1061 | if (inti) { | |
6d3da241 JF |
1062 | list_del(&inti->list); |
1063 | fi->counters[FIRQ_CNTR_PFAULT] -= 1; | |
1064 | } | |
1065 | if (list_empty(&fi->lists[FIRQ_LIST_PFAULT])) | |
1066 | clear_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs); | |
1067 | spin_unlock(&fi->lock); | |
8712836b | 1068 | |
6d3da241 | 1069 | if (inti) { |
3f24ba15 CB |
1070 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, |
1071 | KVM_S390_INT_PFAULT_DONE, 0, | |
1072 | inti->ext.ext_params2); | |
1073 | VCPU_EVENT(vcpu, 4, "deliver: pfault done token 0x%llx", | |
1074 | inti->ext.ext_params2); | |
1075 | ||
6d3da241 JF |
1076 | rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, |
1077 | (u16 *)__LC_EXT_INT_CODE); | |
1078 | rc |= put_guest_lc(vcpu, PFAULT_DONE, | |
1079 | (u16 *)__LC_EXT_CPU_ADDR); | |
1080 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, | |
1081 | &vcpu->arch.sie_block->gpsw, | |
1082 | sizeof(psw_t)); | |
1083 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, | |
1084 | &vcpu->arch.sie_block->gpsw, | |
1085 | sizeof(psw_t)); | |
1086 | rc |= put_guest_lc(vcpu, inti->ext.ext_params2, | |
1087 | (u64 *)__LC_EXT_PARAMS2); | |
1088 | kfree(inti); | |
1089 | } | |
99e20009 | 1090 | return rc ? -EFAULT : 0; |
0fb97abe JF |
1091 | } |
1092 | ||
6d3da241 | 1093 | static int __must_check __deliver_virtio(struct kvm_vcpu *vcpu) |
0fb97abe | 1094 | { |
6d3da241 JF |
1095 | struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int; |
1096 | struct kvm_s390_interrupt_info *inti; | |
1097 | int rc = 0; | |
0fb97abe | 1098 | |
6d3da241 JF |
1099 | spin_lock(&fi->lock); |
1100 | inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_VIRTIO], | |
1101 | struct kvm_s390_interrupt_info, | |
1102 | list); | |
1103 | if (inti) { | |
1104 | VCPU_EVENT(vcpu, 4, | |
3f24ba15 | 1105 | "deliver: virtio parm: 0x%x,parm64: 0x%llx", |
6d3da241 | 1106 | inti->ext.ext_params, inti->ext.ext_params2); |
ccc40c53 | 1107 | vcpu->stat.deliver_virtio++; |
6d3da241 JF |
1108 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, |
1109 | inti->type, | |
1110 | inti->ext.ext_params, | |
1111 | inti->ext.ext_params2); | |
1112 | list_del(&inti->list); | |
1113 | fi->counters[FIRQ_CNTR_VIRTIO] -= 1; | |
1114 | } | |
1115 | if (list_empty(&fi->lists[FIRQ_LIST_VIRTIO])) | |
1116 | clear_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs); | |
1117 | spin_unlock(&fi->lock); | |
0fb97abe | 1118 | |
6d3da241 JF |
1119 | if (inti) { |
1120 | rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, | |
1121 | (u16 *)__LC_EXT_INT_CODE); | |
1122 | rc |= put_guest_lc(vcpu, VIRTIO_PARAM, | |
1123 | (u16 *)__LC_EXT_CPU_ADDR); | |
1124 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, | |
1125 | &vcpu->arch.sie_block->gpsw, | |
1126 | sizeof(psw_t)); | |
1127 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, | |
1128 | &vcpu->arch.sie_block->gpsw, | |
1129 | sizeof(psw_t)); | |
1130 | rc |= put_guest_lc(vcpu, inti->ext.ext_params, | |
1131 | (u32 *)__LC_EXT_PARAMS); | |
1132 | rc |= put_guest_lc(vcpu, inti->ext.ext_params2, | |
1133 | (u64 *)__LC_EXT_PARAMS2); | |
1134 | kfree(inti); | |
1135 | } | |
99e20009 | 1136 | return rc ? -EFAULT : 0; |
0fb97abe JF |
1137 | } |
1138 | ||
d7c5cb01 MM |
1139 | static int __do_deliver_io(struct kvm_vcpu *vcpu, struct kvm_s390_io_info *io) |
1140 | { | |
1141 | int rc; | |
1142 | ||
201ae986 MM |
1143 | if (kvm_s390_pv_cpu_is_protected(vcpu)) { |
1144 | vcpu->arch.sie_block->iictl = IICTL_CODE_IO; | |
1145 | vcpu->arch.sie_block->subchannel_id = io->subchannel_id; | |
1146 | vcpu->arch.sie_block->subchannel_nr = io->subchannel_nr; | |
1147 | vcpu->arch.sie_block->io_int_parm = io->io_int_parm; | |
1148 | vcpu->arch.sie_block->io_int_word = io->io_int_word; | |
1149 | return 0; | |
1150 | } | |
1151 | ||
d7c5cb01 MM |
1152 | rc = put_guest_lc(vcpu, io->subchannel_id, (u16 *)__LC_SUBCHANNEL_ID); |
1153 | rc |= put_guest_lc(vcpu, io->subchannel_nr, (u16 *)__LC_SUBCHANNEL_NR); | |
1154 | rc |= put_guest_lc(vcpu, io->io_int_parm, (u32 *)__LC_IO_INT_PARM); | |
1155 | rc |= put_guest_lc(vcpu, io->io_int_word, (u32 *)__LC_IO_INT_WORD); | |
1156 | rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW, | |
1157 | &vcpu->arch.sie_block->gpsw, | |
1158 | sizeof(psw_t)); | |
1159 | rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW, | |
1160 | &vcpu->arch.sie_block->gpsw, | |
1161 | sizeof(psw_t)); | |
1162 | return rc ? -EFAULT : 0; | |
1163 | } | |
1164 | ||
0fb97abe | 1165 | static int __must_check __deliver_io(struct kvm_vcpu *vcpu, |
6d3da241 | 1166 | unsigned long irq_type) |
0fb97abe | 1167 | { |
6d3da241 JF |
1168 | struct list_head *isc_list; |
1169 | struct kvm_s390_float_interrupt *fi; | |
982cff42 | 1170 | struct kvm_s390_gisa_interrupt *gi = &vcpu->kvm->arch.gisa_int; |
6d3da241 | 1171 | struct kvm_s390_interrupt_info *inti = NULL; |
d7c5cb01 MM |
1172 | struct kvm_s390_io_info io; |
1173 | u32 isc; | |
6d3da241 | 1174 | int rc = 0; |
0fb97abe | 1175 | |
6d3da241 | 1176 | fi = &vcpu->kvm->arch.float_int; |
8712836b | 1177 | |
6d3da241 | 1178 | spin_lock(&fi->lock); |
d7c5cb01 MM |
1179 | isc = irq_type_to_isc(irq_type); |
1180 | isc_list = &fi->lists[isc]; | |
6d3da241 JF |
1181 | inti = list_first_entry_or_null(isc_list, |
1182 | struct kvm_s390_interrupt_info, | |
1183 | list); | |
1184 | if (inti) { | |
dcc98ea6 CB |
1185 | if (inti->type & KVM_S390_INT_IO_AI_MASK) |
1186 | VCPU_EVENT(vcpu, 4, "%s", "deliver: I/O (AI)"); | |
1187 | else | |
1188 | VCPU_EVENT(vcpu, 4, "deliver: I/O %x ss %x schid %04x", | |
1189 | inti->io.subchannel_id >> 8, | |
1190 | inti->io.subchannel_id >> 1 & 0x3, | |
1191 | inti->io.subchannel_nr); | |
1192 | ||
ccc40c53 | 1193 | vcpu->stat.deliver_io++; |
6d3da241 JF |
1194 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, |
1195 | inti->type, | |
1196 | ((__u32)inti->io.subchannel_id << 16) | | |
1197 | inti->io.subchannel_nr, | |
1198 | ((__u64)inti->io.io_int_parm << 32) | | |
1199 | inti->io.io_int_word); | |
1200 | list_del(&inti->list); | |
1201 | fi->counters[FIRQ_CNTR_IO] -= 1; | |
1202 | } | |
1203 | if (list_empty(isc_list)) | |
1204 | clear_bit(irq_type, &fi->pending_irqs); | |
1205 | spin_unlock(&fi->lock); | |
1206 | ||
1207 | if (inti) { | |
d7c5cb01 | 1208 | rc = __do_deliver_io(vcpu, &(inti->io)); |
6d3da241 | 1209 | kfree(inti); |
d7c5cb01 | 1210 | goto out; |
6d3da241 | 1211 | } |
383d0b05 | 1212 | |
982cff42 | 1213 | if (gi->origin && gisa_tac_ipm_gisc(gi->origin, isc)) { |
d7c5cb01 MM |
1214 | /* |
1215 | * in case an adapter interrupt was not delivered | |
1216 | * in SIE context KVM will handle the delivery | |
1217 | */ | |
1218 | VCPU_EVENT(vcpu, 4, "%s isc %u", "deliver: I/O (AI/gisa)", isc); | |
1219 | memset(&io, 0, sizeof(io)); | |
2496c8e7 | 1220 | io.io_int_word = isc_to_int_word(isc); |
ccc40c53 | 1221 | vcpu->stat.deliver_io++; |
d7c5cb01 MM |
1222 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, |
1223 | KVM_S390_INT_IO(1, 0, 0, 0), | |
1224 | ((__u32)io.subchannel_id << 16) | | |
1225 | io.subchannel_nr, | |
1226 | ((__u64)io.io_int_parm << 32) | | |
1227 | io.io_int_word); | |
1228 | rc = __do_deliver_io(vcpu, &io); | |
1229 | } | |
1230 | out: | |
1231 | return rc; | |
383d0b05 JF |
1232 | } |
1233 | ||
ea5f4969 DH |
1234 | /* Check whether an external call is pending (deliverable or not) */ |
1235 | int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu) | |
4953919f | 1236 | { |
ea5f4969 | 1237 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
4953919f | 1238 | |
37c5f6c8 | 1239 | if (!sclp.has_sigpif) |
ea5f4969 | 1240 | return test_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs); |
4953919f | 1241 | |
a5bd7647 | 1242 | return sca_ext_call_pending(vcpu, NULL); |
4953919f DH |
1243 | } |
1244 | ||
9a022067 | 1245 | int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop) |
ba5c1e9b | 1246 | { |
4d32ad6b DH |
1247 | if (deliverable_irqs(vcpu)) |
1248 | return 1; | |
ba5c1e9b | 1249 | |
4d32ad6b DH |
1250 | if (kvm_cpu_has_pending_timer(vcpu)) |
1251 | return 1; | |
ba5c1e9b | 1252 | |
ea5f4969 | 1253 | /* external call pending and deliverable */ |
4d32ad6b | 1254 | if (kvm_s390_ext_call_pending(vcpu) && |
ea5f4969 | 1255 | !psw_extint_disabled(vcpu) && |
b9224cd7 | 1256 | (vcpu->arch.sie_block->gcr[0] & CR0_EXTERNAL_CALL_SUBMASK)) |
4d32ad6b | 1257 | return 1; |
4953919f | 1258 | |
4d32ad6b DH |
1259 | if (!exclude_stop && kvm_s390_is_stop_irq_pending(vcpu)) |
1260 | return 1; | |
1261 | return 0; | |
ba5c1e9b CO |
1262 | } |
1263 | ||
3d80840d MT |
1264 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) |
1265 | { | |
b4aec925 | 1266 | return ckc_irq_pending(vcpu) || cpu_timer_irq_pending(vcpu); |
3d80840d MT |
1267 | } |
1268 | ||
b3c17f10 DH |
1269 | static u64 __calculate_sltime(struct kvm_vcpu *vcpu) |
1270 | { | |
5fe01793 DH |
1271 | const u64 now = kvm_s390_get_tod_clock_fast(vcpu->kvm); |
1272 | const u64 ckc = vcpu->arch.sie_block->ckc; | |
1273 | u64 cputm, sltime = 0; | |
b3c17f10 DH |
1274 | |
1275 | if (ckc_interrupts_enabled(vcpu)) { | |
b9224cd7 | 1276 | if (vcpu->arch.sie_block->gcr[0] & CR0_CLOCK_COMPARATOR_SIGN) { |
5fe01793 DH |
1277 | if ((s64)now < (s64)ckc) |
1278 | sltime = tod_to_ns((s64)ckc - (s64)now); | |
1279 | } else if (now < ckc) { | |
1280 | sltime = tod_to_ns(ckc - now); | |
1281 | } | |
1282 | /* already expired */ | |
1283 | if (!sltime) | |
b3c17f10 DH |
1284 | return 0; |
1285 | if (cpu_timer_interrupts_enabled(vcpu)) { | |
1286 | cputm = kvm_s390_get_cpu_timer(vcpu); | |
1287 | /* already expired? */ | |
1288 | if (cputm >> 63) | |
1289 | return 0; | |
eba8e1af | 1290 | return min_t(u64, sltime, tod_to_ns(cputm)); |
b3c17f10 DH |
1291 | } |
1292 | } else if (cpu_timer_interrupts_enabled(vcpu)) { | |
1293 | sltime = kvm_s390_get_cpu_timer(vcpu); | |
1294 | /* already expired? */ | |
1295 | if (sltime >> 63) | |
1296 | return 0; | |
1297 | } | |
1298 | return sltime; | |
1299 | } | |
1300 | ||
ba5c1e9b CO |
1301 | int kvm_s390_handle_wait(struct kvm_vcpu *vcpu) |
1302 | { | |
9f30f621 | 1303 | struct kvm_s390_gisa_interrupt *gi = &vcpu->kvm->arch.gisa_int; |
b3c17f10 | 1304 | u64 sltime; |
ba5c1e9b CO |
1305 | |
1306 | vcpu->stat.exit_wait_state++; | |
ba5c1e9b | 1307 | |
0759d068 | 1308 | /* fast path */ |
118b862b | 1309 | if (kvm_arch_vcpu_runnable(vcpu)) |
0759d068 | 1310 | return 0; |
e52b2af5 | 1311 | |
ba5c1e9b CO |
1312 | if (psw_interrupts_disabled(vcpu)) { |
1313 | VCPU_EVENT(vcpu, 3, "%s", "disabled wait"); | |
b8e660b8 | 1314 | return -EOPNOTSUPP; /* disabled wait */ |
ba5c1e9b CO |
1315 | } |
1316 | ||
9f30f621 MM |
1317 | if (gi->origin && |
1318 | (gisa_get_ipm_or_restore_iam(gi) & | |
1319 | vcpu->arch.sie_block->gcr[6] >> 24)) | |
1320 | return 0; | |
1321 | ||
b3c17f10 DH |
1322 | if (!ckc_interrupts_enabled(vcpu) && |
1323 | !cpu_timer_interrupts_enabled(vcpu)) { | |
ba5c1e9b | 1324 | VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer"); |
bda343ef | 1325 | __set_cpu_idle(vcpu); |
ba5c1e9b CO |
1326 | goto no_timer; |
1327 | } | |
1328 | ||
b3c17f10 DH |
1329 | sltime = __calculate_sltime(vcpu); |
1330 | if (!sltime) | |
bda343ef DH |
1331 | return 0; |
1332 | ||
1333 | __set_cpu_idle(vcpu); | |
8b0e1953 | 1334 | hrtimer_start(&vcpu->arch.ckc_timer, sltime, HRTIMER_MODE_REL); |
b3c17f10 | 1335 | VCPU_EVENT(vcpu, 4, "enabled wait: %llu ns", sltime); |
ba5c1e9b | 1336 | no_timer: |
800c1065 | 1337 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
0759d068 | 1338 | kvm_vcpu_block(vcpu); |
ba5c1e9b | 1339 | __unset_cpu_idle(vcpu); |
800c1065 TH |
1340 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
1341 | ||
2d00f759 | 1342 | hrtimer_cancel(&vcpu->arch.ckc_timer); |
ba5c1e9b CO |
1343 | return 0; |
1344 | } | |
1345 | ||
0e9c85a5 DH |
1346 | void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu) |
1347 | { | |
3491caf2 | 1348 | vcpu->valid_wakeup = true; |
d9847409 WL |
1349 | kvm_vcpu_wake_up(vcpu); |
1350 | ||
72e1ad42 | 1351 | /* |
d9847409 | 1352 | * The VCPU might not be sleeping but rather executing VSIE. Let's |
adbf1698 DH |
1353 | * kick it, so it leaves the SIE to process the request. |
1354 | */ | |
1355 | kvm_s390_vsie_kick(vcpu); | |
0e9c85a5 DH |
1356 | } |
1357 | ||
ca872302 CB |
1358 | enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer) |
1359 | { | |
1360 | struct kvm_vcpu *vcpu; | |
b3c17f10 | 1361 | u64 sltime; |
ca872302 CB |
1362 | |
1363 | vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer); | |
b3c17f10 | 1364 | sltime = __calculate_sltime(vcpu); |
ca872302 | 1365 | |
2d00f759 DH |
1366 | /* |
1367 | * If the monotonic clock runs faster than the tod clock we might be | |
1368 | * woken up too early and have to go back to sleep to avoid deadlocks. | |
1369 | */ | |
b3c17f10 | 1370 | if (sltime && hrtimer_forward_now(timer, ns_to_ktime(sltime))) |
2d00f759 DH |
1371 | return HRTIMER_RESTART; |
1372 | kvm_s390_vcpu_wakeup(vcpu); | |
ca872302 CB |
1373 | return HRTIMER_NORESTART; |
1374 | } | |
ba5c1e9b | 1375 | |
2ed10cc1 JF |
1376 | void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu) |
1377 | { | |
1378 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; | |
2ed10cc1 | 1379 | |
4ae3c081 | 1380 | spin_lock(&li->lock); |
383d0b05 JF |
1381 | li->pending_irqs = 0; |
1382 | bitmap_zero(li->sigp_emerg_pending, KVM_MAX_VCPUS); | |
1383 | memset(&li->irq, 0, sizeof(li->irq)); | |
4ae3c081 | 1384 | spin_unlock(&li->lock); |
4953919f | 1385 | |
a5bd7647 | 1386 | sca_clear_ext_call(vcpu); |
2ed10cc1 JF |
1387 | } |
1388 | ||
614aeab4 | 1389 | int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu) |
ba5c1e9b | 1390 | { |
180c12fb | 1391 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
79395031 | 1392 | int rc = 0; |
383d0b05 | 1393 | unsigned long irq_type; |
6d3da241 | 1394 | unsigned long irqs; |
ba5c1e9b CO |
1395 | |
1396 | __reset_intercept_indicators(vcpu); | |
ba5c1e9b | 1397 | |
383d0b05 JF |
1398 | /* pending ckc conditions might have been invalidated */ |
1399 | clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs); | |
b4aec925 | 1400 | if (ckc_irq_pending(vcpu)) |
383d0b05 JF |
1401 | set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs); |
1402 | ||
b4aec925 DH |
1403 | /* pending cpu timer conditions might have been invalidated */ |
1404 | clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs); | |
1405 | if (cpu_timer_irq_pending(vcpu)) | |
1406 | set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs); | |
1407 | ||
ffeca0ae | 1408 | while ((irqs = deliverable_irqs(vcpu)) && !rc) { |
c7901a6e MM |
1409 | /* bits are in the reverse order of interrupt priority */ |
1410 | irq_type = find_last_bit(&irqs, IRQ_PEND_COUNT); | |
baabee67 DH |
1411 | switch (irq_type) { |
1412 | case IRQ_PEND_IO_ISC_0: | |
1413 | case IRQ_PEND_IO_ISC_1: | |
1414 | case IRQ_PEND_IO_ISC_2: | |
1415 | case IRQ_PEND_IO_ISC_3: | |
1416 | case IRQ_PEND_IO_ISC_4: | |
1417 | case IRQ_PEND_IO_ISC_5: | |
1418 | case IRQ_PEND_IO_ISC_6: | |
1419 | case IRQ_PEND_IO_ISC_7: | |
6d3da241 | 1420 | rc = __deliver_io(vcpu, irq_type); |
baabee67 DH |
1421 | break; |
1422 | case IRQ_PEND_MCHK_EX: | |
1423 | case IRQ_PEND_MCHK_REP: | |
1424 | rc = __deliver_machine_check(vcpu); | |
1425 | break; | |
1426 | case IRQ_PEND_PROG: | |
1427 | rc = __deliver_prog(vcpu); | |
1428 | break; | |
1429 | case IRQ_PEND_EXT_EMERGENCY: | |
1430 | rc = __deliver_emergency_signal(vcpu); | |
1431 | break; | |
1432 | case IRQ_PEND_EXT_EXTERNAL: | |
1433 | rc = __deliver_external_call(vcpu); | |
1434 | break; | |
1435 | case IRQ_PEND_EXT_CLOCK_COMP: | |
1436 | rc = __deliver_ckc(vcpu); | |
1437 | break; | |
1438 | case IRQ_PEND_EXT_CPU_TIMER: | |
1439 | rc = __deliver_cpu_timer(vcpu); | |
1440 | break; | |
1441 | case IRQ_PEND_RESTART: | |
1442 | rc = __deliver_restart(vcpu); | |
1443 | break; | |
1444 | case IRQ_PEND_SET_PREFIX: | |
1445 | rc = __deliver_set_prefix(vcpu); | |
1446 | break; | |
1447 | case IRQ_PEND_PFAULT_INIT: | |
1448 | rc = __deliver_pfault_init(vcpu); | |
1449 | break; | |
1450 | case IRQ_PEND_EXT_SERVICE: | |
1451 | rc = __deliver_service(vcpu); | |
1452 | break; | |
0890ddea CB |
1453 | case IRQ_PEND_EXT_SERVICE_EV: |
1454 | rc = __deliver_service_ev(vcpu); | |
1455 | break; | |
baabee67 DH |
1456 | case IRQ_PEND_PFAULT_DONE: |
1457 | rc = __deliver_pfault_done(vcpu); | |
1458 | break; | |
1459 | case IRQ_PEND_VIRTIO: | |
1460 | rc = __deliver_virtio(vcpu); | |
1461 | break; | |
1462 | default: | |
1463 | WARN_ONCE(1, "Unknown pending irq type %ld", irq_type); | |
1464 | clear_bit(irq_type, &li->pending_irqs); | |
383d0b05 | 1465 | } |
ffeca0ae | 1466 | } |
383d0b05 | 1467 | |
6d3da241 | 1468 | set_intercept_indicators(vcpu); |
79395031 JF |
1469 | |
1470 | return rc; | |
ba5c1e9b CO |
1471 | } |
1472 | ||
383d0b05 | 1473 | static int __inject_prog(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq) |
0146a7b0 JF |
1474 | { |
1475 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; | |
1476 | ||
ccc40c53 | 1477 | vcpu->stat.inject_program++; |
ed2afcfa DH |
1478 | VCPU_EVENT(vcpu, 3, "inject: program irq code 0x%x", irq->u.pgm.code); |
1479 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT, | |
1480 | irq->u.pgm.code, 0); | |
1481 | ||
634790b8 DH |
1482 | if (!(irq->u.pgm.flags & KVM_S390_PGM_FLAGS_ILC_VALID)) { |
1483 | /* auto detection if no valid ILC was given */ | |
1484 | irq->u.pgm.flags &= ~KVM_S390_PGM_FLAGS_ILC_MASK; | |
1485 | irq->u.pgm.flags |= kvm_s390_get_ilen(vcpu); | |
1486 | irq->u.pgm.flags |= KVM_S390_PGM_FLAGS_ILC_VALID; | |
1487 | } | |
1488 | ||
238293b1 DH |
1489 | if (irq->u.pgm.code == PGM_PER) { |
1490 | li->irq.pgm.code |= PGM_PER; | |
634790b8 | 1491 | li->irq.pgm.flags = irq->u.pgm.flags; |
238293b1 DH |
1492 | /* only modify PER related information */ |
1493 | li->irq.pgm.per_address = irq->u.pgm.per_address; | |
1494 | li->irq.pgm.per_code = irq->u.pgm.per_code; | |
1495 | li->irq.pgm.per_atmid = irq->u.pgm.per_atmid; | |
1496 | li->irq.pgm.per_access_id = irq->u.pgm.per_access_id; | |
1497 | } else if (!(irq->u.pgm.code & PGM_PER)) { | |
1498 | li->irq.pgm.code = (li->irq.pgm.code & PGM_PER) | | |
1499 | irq->u.pgm.code; | |
634790b8 | 1500 | li->irq.pgm.flags = irq->u.pgm.flags; |
238293b1 DH |
1501 | /* only modify non-PER information */ |
1502 | li->irq.pgm.trans_exc_code = irq->u.pgm.trans_exc_code; | |
1503 | li->irq.pgm.mon_code = irq->u.pgm.mon_code; | |
1504 | li->irq.pgm.data_exc_code = irq->u.pgm.data_exc_code; | |
1505 | li->irq.pgm.mon_class_nr = irq->u.pgm.mon_class_nr; | |
1506 | li->irq.pgm.exc_access_id = irq->u.pgm.exc_access_id; | |
1507 | li->irq.pgm.op_access_id = irq->u.pgm.op_access_id; | |
1508 | } else { | |
1509 | li->irq.pgm = irq->u.pgm; | |
1510 | } | |
9185124e | 1511 | set_bit(IRQ_PEND_PROG, &li->pending_irqs); |
0146a7b0 JF |
1512 | return 0; |
1513 | } | |
1514 | ||
383d0b05 | 1515 | static int __inject_pfault_init(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq) |
0146a7b0 JF |
1516 | { |
1517 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; | |
1518 | ||
ccc40c53 | 1519 | vcpu->stat.inject_pfault_init++; |
3f24ba15 CB |
1520 | VCPU_EVENT(vcpu, 4, "inject: pfault init parameter block at 0x%llx", |
1521 | irq->u.ext.ext_params2); | |
383d0b05 JF |
1522 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_PFAULT_INIT, |
1523 | irq->u.ext.ext_params, | |
ed2afcfa | 1524 | irq->u.ext.ext_params2); |
383d0b05 JF |
1525 | |
1526 | li->irq.ext = irq->u.ext; | |
1527 | set_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs); | |
2018224d | 1528 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT); |
0146a7b0 JF |
1529 | return 0; |
1530 | } | |
1531 | ||
0675d92d | 1532 | static int __inject_extcall(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq) |
0146a7b0 JF |
1533 | { |
1534 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; | |
383d0b05 | 1535 | struct kvm_s390_extcall_info *extcall = &li->irq.extcall; |
ea5f4969 | 1536 | uint16_t src_id = irq->u.extcall.code; |
0146a7b0 | 1537 | |
ccc40c53 | 1538 | vcpu->stat.inject_external_call++; |
3f24ba15 | 1539 | VCPU_EVENT(vcpu, 4, "inject: external call source-cpu:%u", |
ea5f4969 | 1540 | src_id); |
383d0b05 | 1541 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EXTERNAL_CALL, |
ed2afcfa | 1542 | src_id, 0); |
ea5f4969 DH |
1543 | |
1544 | /* sending vcpu invalid */ | |
152e9f65 | 1545 | if (kvm_get_vcpu_by_id(vcpu->kvm, src_id) == NULL) |
ea5f4969 DH |
1546 | return -EINVAL; |
1547 | ||
201ae986 | 1548 | if (sclp.has_sigpif && !kvm_s390_pv_cpu_get_handle(vcpu)) |
a5bd7647 | 1549 | return sca_inject_ext_call(vcpu, src_id); |
383d0b05 | 1550 | |
b938eace | 1551 | if (test_and_set_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs)) |
ea5f4969 | 1552 | return -EBUSY; |
383d0b05 | 1553 | *extcall = irq->u.extcall; |
2018224d | 1554 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT); |
0146a7b0 JF |
1555 | return 0; |
1556 | } | |
1557 | ||
383d0b05 | 1558 | static int __inject_set_prefix(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq) |
0146a7b0 JF |
1559 | { |
1560 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; | |
383d0b05 | 1561 | struct kvm_s390_prefix_info *prefix = &li->irq.prefix; |
0146a7b0 | 1562 | |
ccc40c53 | 1563 | vcpu->stat.inject_set_prefix++; |
ed2afcfa | 1564 | VCPU_EVENT(vcpu, 3, "inject: set prefix to %x", |
556cc0da | 1565 | irq->u.prefix.address); |
383d0b05 | 1566 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_SET_PREFIX, |
ed2afcfa | 1567 | irq->u.prefix.address, 0); |
383d0b05 | 1568 | |
a3a9c59a DH |
1569 | if (!is_vcpu_stopped(vcpu)) |
1570 | return -EBUSY; | |
1571 | ||
383d0b05 JF |
1572 | *prefix = irq->u.prefix; |
1573 | set_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs); | |
0146a7b0 JF |
1574 | return 0; |
1575 | } | |
1576 | ||
6cddd432 | 1577 | #define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS) |
383d0b05 | 1578 | static int __inject_sigp_stop(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq) |
0146a7b0 JF |
1579 | { |
1580 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; | |
2822545f | 1581 | struct kvm_s390_stop_info *stop = &li->irq.stop; |
6cddd432 | 1582 | int rc = 0; |
0146a7b0 | 1583 | |
ccc40c53 | 1584 | vcpu->stat.inject_stop_signal++; |
ed2afcfa | 1585 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_STOP, 0, 0); |
383d0b05 | 1586 | |
2822545f DH |
1587 | if (irq->u.stop.flags & ~KVM_S390_STOP_SUPP_FLAGS) |
1588 | return -EINVAL; | |
1589 | ||
6cddd432 DH |
1590 | if (is_vcpu_stopped(vcpu)) { |
1591 | if (irq->u.stop.flags & KVM_S390_STOP_FLAG_STORE_STATUS) | |
1592 | rc = kvm_s390_store_status_unloaded(vcpu, | |
1593 | KVM_S390_STORE_STATUS_NOADDR); | |
1594 | return rc; | |
1595 | } | |
1596 | ||
1597 | if (test_and_set_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs)) | |
1598 | return -EBUSY; | |
2822545f | 1599 | stop->flags = irq->u.stop.flags; |
2018224d | 1600 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT); |
0146a7b0 JF |
1601 | return 0; |
1602 | } | |
1603 | ||
7775cbaa | 1604 | static int __inject_sigp_restart(struct kvm_vcpu *vcpu) |
0146a7b0 JF |
1605 | { |
1606 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; | |
1607 | ||
ccc40c53 | 1608 | vcpu->stat.inject_restart++; |
3f24ba15 | 1609 | VCPU_EVENT(vcpu, 3, "%s", "inject: restart int"); |
ed2afcfa | 1610 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0); |
383d0b05 JF |
1611 | |
1612 | set_bit(IRQ_PEND_RESTART, &li->pending_irqs); | |
0146a7b0 JF |
1613 | return 0; |
1614 | } | |
1615 | ||
1616 | static int __inject_sigp_emergency(struct kvm_vcpu *vcpu, | |
383d0b05 | 1617 | struct kvm_s390_irq *irq) |
0146a7b0 JF |
1618 | { |
1619 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; | |
1620 | ||
ccc40c53 | 1621 | vcpu->stat.inject_emergency_signal++; |
3f24ba15 | 1622 | VCPU_EVENT(vcpu, 4, "inject: emergency from cpu %u", |
383d0b05 JF |
1623 | irq->u.emerg.code); |
1624 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY, | |
ed2afcfa | 1625 | irq->u.emerg.code, 0); |
383d0b05 | 1626 | |
b85de33a DH |
1627 | /* sending vcpu invalid */ |
1628 | if (kvm_get_vcpu_by_id(vcpu->kvm, irq->u.emerg.code) == NULL) | |
1629 | return -EINVAL; | |
1630 | ||
49538d12 | 1631 | set_bit(irq->u.emerg.code, li->sigp_emerg_pending); |
383d0b05 | 1632 | set_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs); |
2018224d | 1633 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT); |
0146a7b0 JF |
1634 | return 0; |
1635 | } | |
1636 | ||
383d0b05 | 1637 | static int __inject_mchk(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq) |
0146a7b0 JF |
1638 | { |
1639 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; | |
383d0b05 | 1640 | struct kvm_s390_mchk_info *mchk = &li->irq.mchk; |
0146a7b0 | 1641 | |
ccc40c53 | 1642 | vcpu->stat.inject_mchk++; |
3f24ba15 | 1643 | VCPU_EVENT(vcpu, 3, "inject: machine check mcic 0x%llx", |
556cc0da | 1644 | irq->u.mchk.mcic); |
383d0b05 | 1645 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_MCHK, 0, |
ed2afcfa | 1646 | irq->u.mchk.mcic); |
383d0b05 JF |
1647 | |
1648 | /* | |
fc2020cf JF |
1649 | * Because repressible machine checks can be indicated along with |
1650 | * exigent machine checks (PoP, Chapter 11, Interruption action) | |
1651 | * we need to combine cr14, mcic and external damage code. | |
1652 | * Failing storage address and the logout area should not be or'ed | |
1653 | * together, we just indicate the last occurrence of the corresponding | |
1654 | * machine check | |
383d0b05 | 1655 | */ |
fc2020cf | 1656 | mchk->cr14 |= irq->u.mchk.cr14; |
383d0b05 | 1657 | mchk->mcic |= irq->u.mchk.mcic; |
fc2020cf JF |
1658 | mchk->ext_damage_code |= irq->u.mchk.ext_damage_code; |
1659 | mchk->failing_storage_address = irq->u.mchk.failing_storage_address; | |
1660 | memcpy(&mchk->fixed_logout, &irq->u.mchk.fixed_logout, | |
1661 | sizeof(mchk->fixed_logout)); | |
383d0b05 JF |
1662 | if (mchk->mcic & MCHK_EX_MASK) |
1663 | set_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs); | |
1664 | else if (mchk->mcic & MCHK_REP_MASK) | |
1665 | set_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs); | |
0146a7b0 JF |
1666 | return 0; |
1667 | } | |
1668 | ||
383d0b05 | 1669 | static int __inject_ckc(struct kvm_vcpu *vcpu) |
0146a7b0 JF |
1670 | { |
1671 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; | |
1672 | ||
ccc40c53 | 1673 | vcpu->stat.inject_ckc++; |
3f24ba15 | 1674 | VCPU_EVENT(vcpu, 3, "%s", "inject: clock comparator external"); |
383d0b05 | 1675 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP, |
ed2afcfa | 1676 | 0, 0); |
383d0b05 JF |
1677 | |
1678 | set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs); | |
2018224d | 1679 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT); |
0146a7b0 JF |
1680 | return 0; |
1681 | } | |
1682 | ||
383d0b05 | 1683 | static int __inject_cpu_timer(struct kvm_vcpu *vcpu) |
0146a7b0 JF |
1684 | { |
1685 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; | |
1686 | ||
ccc40c53 | 1687 | vcpu->stat.inject_cputm++; |
3f24ba15 | 1688 | VCPU_EVENT(vcpu, 3, "%s", "inject: cpu timer external"); |
383d0b05 | 1689 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER, |
ed2afcfa | 1690 | 0, 0); |
383d0b05 JF |
1691 | |
1692 | set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs); | |
2018224d | 1693 | kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT); |
ba5c1e9b CO |
1694 | return 0; |
1695 | } | |
1696 | ||
6d3da241 JF |
1697 | static struct kvm_s390_interrupt_info *get_io_int(struct kvm *kvm, |
1698 | int isc, u32 schid) | |
1699 | { | |
1700 | struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int; | |
1701 | struct list_head *isc_list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc]; | |
1702 | struct kvm_s390_interrupt_info *iter; | |
1703 | u16 id = (schid & 0xffff0000U) >> 16; | |
1704 | u16 nr = schid & 0x0000ffffU; | |
1705 | ||
1706 | spin_lock(&fi->lock); | |
1707 | list_for_each_entry(iter, isc_list, list) { | |
1708 | if (schid && (id != iter->io.subchannel_id || | |
1709 | nr != iter->io.subchannel_nr)) | |
1710 | continue; | |
1711 | /* found an appropriate entry */ | |
1712 | list_del_init(&iter->list); | |
1713 | fi->counters[FIRQ_CNTR_IO] -= 1; | |
1714 | if (list_empty(isc_list)) | |
ee739f4b | 1715 | clear_bit(isc_to_irq_type(isc), &fi->pending_irqs); |
6d3da241 JF |
1716 | spin_unlock(&fi->lock); |
1717 | return iter; | |
1718 | } | |
1719 | spin_unlock(&fi->lock); | |
1720 | return NULL; | |
1721 | } | |
383d0b05 | 1722 | |
4b35f65e MM |
1723 | static struct kvm_s390_interrupt_info *get_top_io_int(struct kvm *kvm, |
1724 | u64 isc_mask, u32 schid) | |
1725 | { | |
1726 | struct kvm_s390_interrupt_info *inti = NULL; | |
1727 | int isc; | |
1728 | ||
1729 | for (isc = 0; isc <= MAX_ISC && !inti; isc++) { | |
1730 | if (isc_mask & isc_to_isc_bits(isc)) | |
1731 | inti = get_io_int(kvm, isc, schid); | |
1732 | } | |
1733 | return inti; | |
1734 | } | |
1735 | ||
1736 | static int get_top_gisa_isc(struct kvm *kvm, u64 isc_mask, u32 schid) | |
1737 | { | |
982cff42 | 1738 | struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int; |
4b35f65e MM |
1739 | unsigned long active_mask; |
1740 | int isc; | |
1741 | ||
1742 | if (schid) | |
1743 | goto out; | |
982cff42 | 1744 | if (!gi->origin) |
4b35f65e MM |
1745 | goto out; |
1746 | ||
982cff42 | 1747 | active_mask = (isc_mask & gisa_get_ipm(gi->origin) << 24) << 32; |
4b35f65e MM |
1748 | while (active_mask) { |
1749 | isc = __fls(active_mask) ^ (BITS_PER_LONG - 1); | |
982cff42 | 1750 | if (gisa_tac_ipm_gisc(gi->origin, isc)) |
4b35f65e MM |
1751 | return isc; |
1752 | clear_bit_inv(isc, &active_mask); | |
1753 | } | |
1754 | out: | |
1755 | return -EINVAL; | |
1756 | } | |
1757 | ||
6d3da241 JF |
1758 | /* |
1759 | * Dequeue and return an I/O interrupt matching any of the interruption | |
1760 | * subclasses as designated by the isc mask in cr6 and the schid (if != 0). | |
4b35f65e MM |
1761 | * Take into account the interrupts pending in the interrupt list and in GISA. |
1762 | * | |
1763 | * Note that for a guest that does not enable I/O interrupts | |
1764 | * but relies on TPI, a flood of classic interrupts may starve | |
1765 | * out adapter interrupts on the same isc. Linux does not do | |
1766 | * that, and it is possible to work around the issue by configuring | |
1767 | * different iscs for classic and adapter interrupts in the guest, | |
1768 | * but we may want to revisit this in the future. | |
6d3da241 | 1769 | */ |
fa6b7fe9 | 1770 | struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm, |
6d3da241 JF |
1771 | u64 isc_mask, u32 schid) |
1772 | { | |
982cff42 | 1773 | struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int; |
4b35f65e | 1774 | struct kvm_s390_interrupt_info *inti, *tmp_inti; |
6d3da241 JF |
1775 | int isc; |
1776 | ||
4b35f65e MM |
1777 | inti = get_top_io_int(kvm, isc_mask, schid); |
1778 | ||
1779 | isc = get_top_gisa_isc(kvm, isc_mask, schid); | |
1780 | if (isc < 0) | |
1781 | /* no AI in GISA */ | |
1782 | goto out; | |
1783 | ||
1784 | if (!inti) | |
1785 | /* AI in GISA but no classical IO int */ | |
1786 | goto gisa_out; | |
1787 | ||
1788 | /* both types of interrupts present */ | |
1789 | if (int_word_to_isc(inti->io.io_int_word) <= isc) { | |
1790 | /* classical IO int with higher priority */ | |
982cff42 | 1791 | gisa_set_ipm_gisc(gi->origin, isc); |
4b35f65e | 1792 | goto out; |
6d3da241 | 1793 | } |
4b35f65e | 1794 | gisa_out: |
c4196218 | 1795 | tmp_inti = kzalloc(sizeof(*inti), GFP_KERNEL_ACCOUNT); |
4b35f65e MM |
1796 | if (tmp_inti) { |
1797 | tmp_inti->type = KVM_S390_INT_IO(1, 0, 0, 0); | |
1798 | tmp_inti->io.io_int_word = isc_to_int_word(isc); | |
1799 | if (inti) | |
1800 | kvm_s390_reinject_io_int(kvm, inti); | |
1801 | inti = tmp_inti; | |
1802 | } else | |
982cff42 | 1803 | gisa_set_ipm_gisc(gi->origin, isc); |
4b35f65e | 1804 | out: |
6d3da241 JF |
1805 | return inti; |
1806 | } | |
1807 | ||
6d3da241 JF |
1808 | static int __inject_service(struct kvm *kvm, |
1809 | struct kvm_s390_interrupt_info *inti) | |
1810 | { | |
1811 | struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int; | |
1812 | ||
ccc40c53 | 1813 | kvm->stat.inject_service_signal++; |
6d3da241 JF |
1814 | spin_lock(&fi->lock); |
1815 | fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_EVENT_PENDING; | |
0890ddea CB |
1816 | |
1817 | /* We always allow events, track them separately from the sccb ints */ | |
1818 | if (fi->srv_signal.ext_params & SCCB_EVENT_PENDING) | |
1819 | set_bit(IRQ_PEND_EXT_SERVICE_EV, &fi->pending_irqs); | |
1820 | ||
6d3da241 JF |
1821 | /* |
1822 | * Early versions of the QEMU s390 bios will inject several | |
1823 | * service interrupts after another without handling a | |
1824 | * condition code indicating busy. | |
1825 | * We will silently ignore those superfluous sccb values. | |
1826 | * A future version of QEMU will take care of serialization | |
1827 | * of servc requests | |
1828 | */ | |
1829 | if (fi->srv_signal.ext_params & SCCB_MASK) | |
1830 | goto out; | |
1831 | fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_MASK; | |
1832 | set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs); | |
1833 | out: | |
1834 | spin_unlock(&fi->lock); | |
1835 | kfree(inti); | |
1836 | return 0; | |
1837 | } | |
1838 | ||
1839 | static int __inject_virtio(struct kvm *kvm, | |
1840 | struct kvm_s390_interrupt_info *inti) | |
1841 | { | |
1842 | struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int; | |
1843 | ||
ccc40c53 | 1844 | kvm->stat.inject_virtio++; |
6d3da241 JF |
1845 | spin_lock(&fi->lock); |
1846 | if (fi->counters[FIRQ_CNTR_VIRTIO] >= KVM_S390_MAX_VIRTIO_IRQS) { | |
1847 | spin_unlock(&fi->lock); | |
1848 | return -EBUSY; | |
1849 | } | |
1850 | fi->counters[FIRQ_CNTR_VIRTIO] += 1; | |
1851 | list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_VIRTIO]); | |
1852 | set_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs); | |
1853 | spin_unlock(&fi->lock); | |
1854 | return 0; | |
1855 | } | |
1856 | ||
1857 | static int __inject_pfault_done(struct kvm *kvm, | |
1858 | struct kvm_s390_interrupt_info *inti) | |
1859 | { | |
1860 | struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int; | |
1861 | ||
ccc40c53 | 1862 | kvm->stat.inject_pfault_done++; |
6d3da241 JF |
1863 | spin_lock(&fi->lock); |
1864 | if (fi->counters[FIRQ_CNTR_PFAULT] >= | |
1865 | (ASYNC_PF_PER_VCPU * KVM_MAX_VCPUS)) { | |
1866 | spin_unlock(&fi->lock); | |
1867 | return -EBUSY; | |
1868 | } | |
1869 | fi->counters[FIRQ_CNTR_PFAULT] += 1; | |
1870 | list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_PFAULT]); | |
1871 | set_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs); | |
1872 | spin_unlock(&fi->lock); | |
1873 | return 0; | |
1874 | } | |
1875 | ||
1876 | #define CR_PENDING_SUBCLASS 28 | |
1877 | static int __inject_float_mchk(struct kvm *kvm, | |
1878 | struct kvm_s390_interrupt_info *inti) | |
1879 | { | |
1880 | struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int; | |
1881 | ||
ccc40c53 | 1882 | kvm->stat.inject_float_mchk++; |
6d3da241 JF |
1883 | spin_lock(&fi->lock); |
1884 | fi->mchk.cr14 |= inti->mchk.cr14 & (1UL << CR_PENDING_SUBCLASS); | |
1885 | fi->mchk.mcic |= inti->mchk.mcic; | |
1886 | set_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs); | |
1887 | spin_unlock(&fi->lock); | |
1888 | kfree(inti); | |
1889 | return 0; | |
1890 | } | |
1891 | ||
1892 | static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti) | |
fa6b7fe9 | 1893 | { |
982cff42 | 1894 | struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int; |
fa6b7fe9 | 1895 | struct kvm_s390_float_interrupt *fi; |
6d3da241 JF |
1896 | struct list_head *list; |
1897 | int isc; | |
fa6b7fe9 | 1898 | |
ccc40c53 | 1899 | kvm->stat.inject_io++; |
d7c5cb01 MM |
1900 | isc = int_word_to_isc(inti->io.io_int_word); |
1901 | ||
201ae986 MM |
1902 | /* |
1903 | * Do not make use of gisa in protected mode. We do not use the lock | |
1904 | * checking variant as this is just a performance optimization and we | |
1905 | * do not hold the lock here. This is ok as the code will pick | |
1906 | * interrupts from both "lists" for delivery. | |
1907 | */ | |
1908 | if (!kvm_s390_pv_get_handle(kvm) && | |
1909 | gi->origin && inti->type & KVM_S390_INT_IO_AI_MASK) { | |
d7c5cb01 | 1910 | VM_EVENT(kvm, 4, "%s isc %1u", "inject: I/O (AI/gisa)", isc); |
982cff42 | 1911 | gisa_set_ipm_gisc(gi->origin, isc); |
d7c5cb01 MM |
1912 | kfree(inti); |
1913 | return 0; | |
1914 | } | |
1915 | ||
fa6b7fe9 CH |
1916 | fi = &kvm->arch.float_int; |
1917 | spin_lock(&fi->lock); | |
6d3da241 JF |
1918 | if (fi->counters[FIRQ_CNTR_IO] >= KVM_S390_MAX_FLOAT_IRQS) { |
1919 | spin_unlock(&fi->lock); | |
1920 | return -EBUSY; | |
a91b8ebe | 1921 | } |
6d3da241 JF |
1922 | fi->counters[FIRQ_CNTR_IO] += 1; |
1923 | ||
dcc98ea6 CB |
1924 | if (inti->type & KVM_S390_INT_IO_AI_MASK) |
1925 | VM_EVENT(kvm, 4, "%s", "inject: I/O (AI)"); | |
1926 | else | |
1927 | VM_EVENT(kvm, 4, "inject: I/O %x ss %x schid %04x", | |
1928 | inti->io.subchannel_id >> 8, | |
1929 | inti->io.subchannel_id >> 1 & 0x3, | |
1930 | inti->io.subchannel_nr); | |
6d3da241 JF |
1931 | list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc]; |
1932 | list_add_tail(&inti->list, list); | |
ee739f4b | 1933 | set_bit(isc_to_irq_type(isc), &fi->pending_irqs); |
fa6b7fe9 | 1934 | spin_unlock(&fi->lock); |
6d3da241 | 1935 | return 0; |
fa6b7fe9 | 1936 | } |
ba5c1e9b | 1937 | |
96e0ed23 DH |
1938 | /* |
1939 | * Find a destination VCPU for a floating irq and kick it. | |
1940 | */ | |
1941 | static void __floating_irq_kick(struct kvm *kvm, u64 type) | |
ba5c1e9b | 1942 | { |
96e0ed23 DH |
1943 | struct kvm_vcpu *dst_vcpu; |
1944 | int sigcpu, online_vcpus, nr_tries = 0; | |
1945 | ||
1946 | online_vcpus = atomic_read(&kvm->online_vcpus); | |
1947 | if (!online_vcpus) | |
1948 | return; | |
1949 | ||
1950 | /* find idle VCPUs first, then round robin */ | |
246b7218 | 1951 | sigcpu = find_first_bit(kvm->arch.idle_mask, online_vcpus); |
96e0ed23 DH |
1952 | if (sigcpu == online_vcpus) { |
1953 | do { | |
246b7218 MM |
1954 | sigcpu = kvm->arch.float_int.next_rr_cpu++; |
1955 | kvm->arch.float_int.next_rr_cpu %= online_vcpus; | |
96e0ed23 DH |
1956 | /* avoid endless loops if all vcpus are stopped */ |
1957 | if (nr_tries++ >= online_vcpus) | |
1958 | return; | |
1959 | } while (is_vcpu_stopped(kvm_get_vcpu(kvm, sigcpu))); | |
1960 | } | |
1961 | dst_vcpu = kvm_get_vcpu(kvm, sigcpu); | |
1962 | ||
1963 | /* make the VCPU drop out of the SIE, or wake it up if sleeping */ | |
96e0ed23 DH |
1964 | switch (type) { |
1965 | case KVM_S390_MCHK: | |
2018224d | 1966 | kvm_s390_set_cpuflags(dst_vcpu, CPUSTAT_STOP_INT); |
96e0ed23 DH |
1967 | break; |
1968 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: | |
982cff42 | 1969 | if (!(type & KVM_S390_INT_IO_AI_MASK && |
201ae986 MM |
1970 | kvm->arch.gisa_int.origin) || |
1971 | kvm_s390_pv_cpu_get_handle(dst_vcpu)) | |
a9810327 | 1972 | kvm_s390_set_cpuflags(dst_vcpu, CPUSTAT_IO_INT); |
96e0ed23 DH |
1973 | break; |
1974 | default: | |
2018224d | 1975 | kvm_s390_set_cpuflags(dst_vcpu, CPUSTAT_EXT_INT); |
96e0ed23 DH |
1976 | break; |
1977 | } | |
96e0ed23 DH |
1978 | kvm_s390_vcpu_wakeup(dst_vcpu); |
1979 | } | |
1980 | ||
1981 | static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti) | |
1982 | { | |
6d3da241 JF |
1983 | u64 type = READ_ONCE(inti->type); |
1984 | int rc; | |
ba5c1e9b | 1985 | |
6d3da241 JF |
1986 | switch (type) { |
1987 | case KVM_S390_MCHK: | |
1988 | rc = __inject_float_mchk(kvm, inti); | |
1989 | break; | |
1990 | case KVM_S390_INT_VIRTIO: | |
1991 | rc = __inject_virtio(kvm, inti); | |
1992 | break; | |
1993 | case KVM_S390_INT_SERVICE: | |
1994 | rc = __inject_service(kvm, inti); | |
1995 | break; | |
1996 | case KVM_S390_INT_PFAULT_DONE: | |
1997 | rc = __inject_pfault_done(kvm, inti); | |
1998 | break; | |
1999 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: | |
2000 | rc = __inject_io(kvm, inti); | |
2001 | break; | |
2002 | default: | |
a91b8ebe | 2003 | rc = -EINVAL; |
c05c4186 | 2004 | } |
6d3da241 JF |
2005 | if (rc) |
2006 | return rc; | |
2007 | ||
96e0ed23 | 2008 | __floating_irq_kick(kvm, type); |
6d3da241 | 2009 | return 0; |
c05c4186 JF |
2010 | } |
2011 | ||
2012 | int kvm_s390_inject_vm(struct kvm *kvm, | |
2013 | struct kvm_s390_interrupt *s390int) | |
2014 | { | |
2015 | struct kvm_s390_interrupt_info *inti; | |
428d53be | 2016 | int rc; |
c05c4186 | 2017 | |
c4196218 | 2018 | inti = kzalloc(sizeof(*inti), GFP_KERNEL_ACCOUNT); |
ba5c1e9b CO |
2019 | if (!inti) |
2020 | return -ENOMEM; | |
2021 | ||
c05c4186 JF |
2022 | inti->type = s390int->type; |
2023 | switch (inti->type) { | |
ba5c1e9b | 2024 | case KVM_S390_INT_VIRTIO: |
33e19115 | 2025 | VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx", |
ba5c1e9b | 2026 | s390int->parm, s390int->parm64); |
ba5c1e9b CO |
2027 | inti->ext.ext_params = s390int->parm; |
2028 | inti->ext.ext_params2 = s390int->parm64; | |
2029 | break; | |
2030 | case KVM_S390_INT_SERVICE: | |
3f24ba15 | 2031 | VM_EVENT(kvm, 4, "inject: sclp parm:%x", s390int->parm); |
ba5c1e9b CO |
2032 | inti->ext.ext_params = s390int->parm; |
2033 | break; | |
3c038e6b | 2034 | case KVM_S390_INT_PFAULT_DONE: |
3c038e6b DD |
2035 | inti->ext.ext_params2 = s390int->parm64; |
2036 | break; | |
48a3e950 | 2037 | case KVM_S390_MCHK: |
3f24ba15 | 2038 | VM_EVENT(kvm, 3, "inject: machine check mcic 0x%llx", |
48a3e950 | 2039 | s390int->parm64); |
48a3e950 CH |
2040 | inti->mchk.cr14 = s390int->parm; /* upper bits are not used */ |
2041 | inti->mchk.mcic = s390int->parm64; | |
2042 | break; | |
d8346b7d | 2043 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
d8346b7d CH |
2044 | inti->io.subchannel_id = s390int->parm >> 16; |
2045 | inti->io.subchannel_nr = s390int->parm & 0x0000ffffu; | |
2046 | inti->io.io_int_parm = s390int->parm64 >> 32; | |
2047 | inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull; | |
2048 | break; | |
ba5c1e9b CO |
2049 | default: |
2050 | kfree(inti); | |
2051 | return -EINVAL; | |
2052 | } | |
ade38c31 CH |
2053 | trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64, |
2054 | 2); | |
ba5c1e9b | 2055 | |
428d53be DH |
2056 | rc = __inject_vm(kvm, inti); |
2057 | if (rc) | |
2058 | kfree(inti); | |
2059 | return rc; | |
ba5c1e9b CO |
2060 | } |
2061 | ||
15462e37 | 2062 | int kvm_s390_reinject_io_int(struct kvm *kvm, |
2f32d4ea CH |
2063 | struct kvm_s390_interrupt_info *inti) |
2064 | { | |
15462e37 | 2065 | return __inject_vm(kvm, inti); |
2f32d4ea CH |
2066 | } |
2067 | ||
383d0b05 JF |
2068 | int s390int_to_s390irq(struct kvm_s390_interrupt *s390int, |
2069 | struct kvm_s390_irq *irq) | |
2070 | { | |
2071 | irq->type = s390int->type; | |
2072 | switch (irq->type) { | |
2073 | case KVM_S390_PROGRAM_INT: | |
2074 | if (s390int->parm & 0xffff0000) | |
2075 | return -EINVAL; | |
2076 | irq->u.pgm.code = s390int->parm; | |
2077 | break; | |
2078 | case KVM_S390_SIGP_SET_PREFIX: | |
2079 | irq->u.prefix.address = s390int->parm; | |
2080 | break; | |
2822545f DH |
2081 | case KVM_S390_SIGP_STOP: |
2082 | irq->u.stop.flags = s390int->parm; | |
2083 | break; | |
383d0b05 | 2084 | case KVM_S390_INT_EXTERNAL_CALL: |
94d1f564 | 2085 | if (s390int->parm & 0xffff0000) |
383d0b05 JF |
2086 | return -EINVAL; |
2087 | irq->u.extcall.code = s390int->parm; | |
2088 | break; | |
2089 | case KVM_S390_INT_EMERGENCY: | |
94d1f564 | 2090 | if (s390int->parm & 0xffff0000) |
383d0b05 JF |
2091 | return -EINVAL; |
2092 | irq->u.emerg.code = s390int->parm; | |
2093 | break; | |
2094 | case KVM_S390_MCHK: | |
2095 | irq->u.mchk.mcic = s390int->parm64; | |
2096 | break; | |
53936b5b TH |
2097 | case KVM_S390_INT_PFAULT_INIT: |
2098 | irq->u.ext.ext_params = s390int->parm; | |
2099 | irq->u.ext.ext_params2 = s390int->parm64; | |
2100 | break; | |
2101 | case KVM_S390_RESTART: | |
2102 | case KVM_S390_INT_CLOCK_COMP: | |
2103 | case KVM_S390_INT_CPU_TIMER: | |
2104 | break; | |
2105 | default: | |
2106 | return -EINVAL; | |
383d0b05 JF |
2107 | } |
2108 | return 0; | |
2109 | } | |
2110 | ||
6cddd432 DH |
2111 | int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu) |
2112 | { | |
2113 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; | |
2114 | ||
2115 | return test_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs); | |
2116 | } | |
2117 | ||
2118 | void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu) | |
2119 | { | |
2120 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; | |
2121 | ||
2122 | spin_lock(&li->lock); | |
2123 | li->irq.stop.flags = 0; | |
2124 | clear_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs); | |
2125 | spin_unlock(&li->lock); | |
2126 | } | |
2127 | ||
79e87a10 | 2128 | static int do_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq) |
ba5c1e9b | 2129 | { |
0146a7b0 | 2130 | int rc; |
ba5c1e9b | 2131 | |
383d0b05 | 2132 | switch (irq->type) { |
ba5c1e9b | 2133 | case KVM_S390_PROGRAM_INT: |
383d0b05 | 2134 | rc = __inject_prog(vcpu, irq); |
ba5c1e9b | 2135 | break; |
b7e6e4d3 | 2136 | case KVM_S390_SIGP_SET_PREFIX: |
383d0b05 | 2137 | rc = __inject_set_prefix(vcpu, irq); |
b7e6e4d3 | 2138 | break; |
ba5c1e9b | 2139 | case KVM_S390_SIGP_STOP: |
383d0b05 | 2140 | rc = __inject_sigp_stop(vcpu, irq); |
0146a7b0 | 2141 | break; |
ba5c1e9b | 2142 | case KVM_S390_RESTART: |
7775cbaa | 2143 | rc = __inject_sigp_restart(vcpu); |
0146a7b0 | 2144 | break; |
e029ae5b | 2145 | case KVM_S390_INT_CLOCK_COMP: |
383d0b05 | 2146 | rc = __inject_ckc(vcpu); |
0146a7b0 | 2147 | break; |
e029ae5b | 2148 | case KVM_S390_INT_CPU_TIMER: |
383d0b05 | 2149 | rc = __inject_cpu_timer(vcpu); |
82a12737 | 2150 | break; |
7697e71f | 2151 | case KVM_S390_INT_EXTERNAL_CALL: |
383d0b05 | 2152 | rc = __inject_extcall(vcpu, irq); |
82a12737 | 2153 | break; |
ba5c1e9b | 2154 | case KVM_S390_INT_EMERGENCY: |
383d0b05 | 2155 | rc = __inject_sigp_emergency(vcpu, irq); |
ba5c1e9b | 2156 | break; |
48a3e950 | 2157 | case KVM_S390_MCHK: |
383d0b05 | 2158 | rc = __inject_mchk(vcpu, irq); |
48a3e950 | 2159 | break; |
3c038e6b | 2160 | case KVM_S390_INT_PFAULT_INIT: |
383d0b05 | 2161 | rc = __inject_pfault_init(vcpu, irq); |
3c038e6b | 2162 | break; |
ba5c1e9b CO |
2163 | case KVM_S390_INT_VIRTIO: |
2164 | case KVM_S390_INT_SERVICE: | |
d8346b7d | 2165 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
ba5c1e9b | 2166 | default: |
0146a7b0 | 2167 | rc = -EINVAL; |
ba5c1e9b | 2168 | } |
79e87a10 JF |
2169 | |
2170 | return rc; | |
2171 | } | |
2172 | ||
2173 | int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq) | |
2174 | { | |
2175 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; | |
2176 | int rc; | |
2177 | ||
2178 | spin_lock(&li->lock); | |
2179 | rc = do_inject_vcpu(vcpu, irq); | |
4ae3c081 | 2180 | spin_unlock(&li->lock); |
0146a7b0 JF |
2181 | if (!rc) |
2182 | kvm_s390_vcpu_wakeup(vcpu); | |
0146a7b0 | 2183 | return rc; |
ba5c1e9b | 2184 | } |
c05c4186 | 2185 | |
6d3da241 | 2186 | static inline void clear_irq_list(struct list_head *_list) |
c05c4186 | 2187 | { |
6d3da241 | 2188 | struct kvm_s390_interrupt_info *inti, *n; |
c05c4186 | 2189 | |
6d3da241 | 2190 | list_for_each_entry_safe(inti, n, _list, list) { |
c05c4186 JF |
2191 | list_del(&inti->list); |
2192 | kfree(inti); | |
2193 | } | |
c05c4186 JF |
2194 | } |
2195 | ||
94aa033e JF |
2196 | static void inti_to_irq(struct kvm_s390_interrupt_info *inti, |
2197 | struct kvm_s390_irq *irq) | |
c05c4186 | 2198 | { |
94aa033e | 2199 | irq->type = inti->type; |
c05c4186 | 2200 | switch (inti->type) { |
3c038e6b DD |
2201 | case KVM_S390_INT_PFAULT_INIT: |
2202 | case KVM_S390_INT_PFAULT_DONE: | |
c05c4186 | 2203 | case KVM_S390_INT_VIRTIO: |
94aa033e | 2204 | irq->u.ext = inti->ext; |
c05c4186 JF |
2205 | break; |
2206 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: | |
94aa033e | 2207 | irq->u.io = inti->io; |
c05c4186 | 2208 | break; |
c05c4186 | 2209 | } |
c05c4186 JF |
2210 | } |
2211 | ||
6d3da241 JF |
2212 | void kvm_s390_clear_float_irqs(struct kvm *kvm) |
2213 | { | |
2214 | struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int; | |
2215 | int i; | |
2216 | ||
0890ddea CB |
2217 | mutex_lock(&kvm->lock); |
2218 | if (!kvm_s390_pv_is_protected(kvm)) | |
2219 | fi->masked_irqs = 0; | |
2220 | mutex_unlock(&kvm->lock); | |
6d3da241 | 2221 | spin_lock(&fi->lock); |
f2ae45ed JF |
2222 | fi->pending_irqs = 0; |
2223 | memset(&fi->srv_signal, 0, sizeof(fi->srv_signal)); | |
2224 | memset(&fi->mchk, 0, sizeof(fi->mchk)); | |
6d3da241 JF |
2225 | for (i = 0; i < FIRQ_LIST_COUNT; i++) |
2226 | clear_irq_list(&fi->lists[i]); | |
2227 | for (i = 0; i < FIRQ_MAX_COUNT; i++) | |
2228 | fi->counters[i] = 0; | |
2229 | spin_unlock(&fi->lock); | |
24160af6 | 2230 | kvm_s390_gisa_clear(kvm); |
6d3da241 JF |
2231 | }; |
2232 | ||
94aa033e | 2233 | static int get_all_floating_irqs(struct kvm *kvm, u8 __user *usrbuf, u64 len) |
c05c4186 | 2234 | { |
982cff42 | 2235 | struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int; |
c05c4186 JF |
2236 | struct kvm_s390_interrupt_info *inti; |
2237 | struct kvm_s390_float_interrupt *fi; | |
94aa033e | 2238 | struct kvm_s390_irq *buf; |
6d3da241 | 2239 | struct kvm_s390_irq *irq; |
94aa033e | 2240 | int max_irqs; |
c05c4186 JF |
2241 | int ret = 0; |
2242 | int n = 0; | |
6d3da241 | 2243 | int i; |
c05c4186 | 2244 | |
94aa033e JF |
2245 | if (len > KVM_S390_FLIC_MAX_BUFFER || len == 0) |
2246 | return -EINVAL; | |
2247 | ||
2248 | /* | |
2249 | * We are already using -ENOMEM to signal | |
2250 | * userspace it may retry with a bigger buffer, | |
2251 | * so we need to use something else for this case | |
2252 | */ | |
2253 | buf = vzalloc(len); | |
2254 | if (!buf) | |
2255 | return -ENOBUFS; | |
2256 | ||
2257 | max_irqs = len / sizeof(struct kvm_s390_irq); | |
2258 | ||
982cff42 | 2259 | if (gi->origin && gisa_get_ipm(gi->origin)) { |
24160af6 MM |
2260 | for (i = 0; i <= MAX_ISC; i++) { |
2261 | if (n == max_irqs) { | |
2262 | /* signal userspace to try again */ | |
2263 | ret = -ENOMEM; | |
2264 | goto out_nolock; | |
2265 | } | |
982cff42 | 2266 | if (gisa_tac_ipm_gisc(gi->origin, i)) { |
24160af6 MM |
2267 | irq = (struct kvm_s390_irq *) &buf[n]; |
2268 | irq->type = KVM_S390_INT_IO(1, 0, 0, 0); | |
2269 | irq->u.io.io_int_word = isc_to_int_word(i); | |
2270 | n++; | |
2271 | } | |
2272 | } | |
2273 | } | |
c05c4186 JF |
2274 | fi = &kvm->arch.float_int; |
2275 | spin_lock(&fi->lock); | |
6d3da241 JF |
2276 | for (i = 0; i < FIRQ_LIST_COUNT; i++) { |
2277 | list_for_each_entry(inti, &fi->lists[i], list) { | |
2278 | if (n == max_irqs) { | |
2279 | /* signal userspace to try again */ | |
2280 | ret = -ENOMEM; | |
2281 | goto out; | |
2282 | } | |
2283 | inti_to_irq(inti, &buf[n]); | |
2284 | n++; | |
2285 | } | |
2286 | } | |
0890ddea CB |
2287 | if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs) || |
2288 | test_bit(IRQ_PEND_EXT_SERVICE_EV, &fi->pending_irqs)) { | |
94aa033e | 2289 | if (n == max_irqs) { |
c05c4186 JF |
2290 | /* signal userspace to try again */ |
2291 | ret = -ENOMEM; | |
6d3da241 | 2292 | goto out; |
c05c4186 | 2293 | } |
6d3da241 JF |
2294 | irq = (struct kvm_s390_irq *) &buf[n]; |
2295 | irq->type = KVM_S390_INT_SERVICE; | |
2296 | irq->u.ext = fi->srv_signal; | |
c05c4186 JF |
2297 | n++; |
2298 | } | |
6d3da241 JF |
2299 | if (test_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) { |
2300 | if (n == max_irqs) { | |
2301 | /* signal userspace to try again */ | |
2302 | ret = -ENOMEM; | |
2303 | goto out; | |
2304 | } | |
2305 | irq = (struct kvm_s390_irq *) &buf[n]; | |
2306 | irq->type = KVM_S390_MCHK; | |
2307 | irq->u.mchk = fi->mchk; | |
2308 | n++; | |
2309 | } | |
2310 | ||
2311 | out: | |
c05c4186 | 2312 | spin_unlock(&fi->lock); |
24160af6 | 2313 | out_nolock: |
94aa033e JF |
2314 | if (!ret && n > 0) { |
2315 | if (copy_to_user(usrbuf, buf, sizeof(struct kvm_s390_irq) * n)) | |
2316 | ret = -EFAULT; | |
2317 | } | |
2318 | vfree(buf); | |
c05c4186 JF |
2319 | |
2320 | return ret < 0 ? ret : n; | |
2321 | } | |
2322 | ||
2c1a48f2 YMZ |
2323 | static int flic_ais_mode_get_all(struct kvm *kvm, struct kvm_device_attr *attr) |
2324 | { | |
2325 | struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int; | |
2326 | struct kvm_s390_ais_all ais; | |
2327 | ||
2328 | if (attr->attr < sizeof(ais)) | |
2329 | return -EINVAL; | |
2330 | ||
2331 | if (!test_kvm_facility(kvm, 72)) | |
c6119908 | 2332 | return -EOPNOTSUPP; |
2c1a48f2 YMZ |
2333 | |
2334 | mutex_lock(&fi->ais_lock); | |
2335 | ais.simm = fi->simm; | |
2336 | ais.nimm = fi->nimm; | |
2337 | mutex_unlock(&fi->ais_lock); | |
2338 | ||
2339 | if (copy_to_user((void __user *)attr->addr, &ais, sizeof(ais))) | |
2340 | return -EFAULT; | |
2341 | ||
2342 | return 0; | |
2343 | } | |
2344 | ||
c05c4186 JF |
2345 | static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr) |
2346 | { | |
2347 | int r; | |
2348 | ||
2349 | switch (attr->group) { | |
2350 | case KVM_DEV_FLIC_GET_ALL_IRQS: | |
94aa033e | 2351 | r = get_all_floating_irqs(dev->kvm, (u8 __user *) attr->addr, |
c05c4186 JF |
2352 | attr->attr); |
2353 | break; | |
2c1a48f2 YMZ |
2354 | case KVM_DEV_FLIC_AISM_ALL: |
2355 | r = flic_ais_mode_get_all(dev->kvm, attr); | |
2356 | break; | |
c05c4186 JF |
2357 | default: |
2358 | r = -EINVAL; | |
2359 | } | |
2360 | ||
2361 | return r; | |
2362 | } | |
2363 | ||
2364 | static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti, | |
2365 | u64 addr) | |
2366 | { | |
2367 | struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr; | |
2368 | void *target = NULL; | |
2369 | void __user *source; | |
2370 | u64 size; | |
2371 | ||
2372 | if (get_user(inti->type, (u64 __user *)addr)) | |
2373 | return -EFAULT; | |
2374 | ||
2375 | switch (inti->type) { | |
3c038e6b DD |
2376 | case KVM_S390_INT_PFAULT_INIT: |
2377 | case KVM_S390_INT_PFAULT_DONE: | |
c05c4186 JF |
2378 | case KVM_S390_INT_VIRTIO: |
2379 | case KVM_S390_INT_SERVICE: | |
2380 | target = (void *) &inti->ext; | |
2381 | source = &uptr->u.ext; | |
2382 | size = sizeof(inti->ext); | |
2383 | break; | |
2384 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: | |
2385 | target = (void *) &inti->io; | |
2386 | source = &uptr->u.io; | |
2387 | size = sizeof(inti->io); | |
2388 | break; | |
2389 | case KVM_S390_MCHK: | |
2390 | target = (void *) &inti->mchk; | |
2391 | source = &uptr->u.mchk; | |
2392 | size = sizeof(inti->mchk); | |
2393 | break; | |
2394 | default: | |
2395 | return -EINVAL; | |
2396 | } | |
2397 | ||
2398 | if (copy_from_user(target, source, size)) | |
2399 | return -EFAULT; | |
2400 | ||
2401 | return 0; | |
2402 | } | |
2403 | ||
2404 | static int enqueue_floating_irq(struct kvm_device *dev, | |
2405 | struct kvm_device_attr *attr) | |
2406 | { | |
2407 | struct kvm_s390_interrupt_info *inti = NULL; | |
2408 | int r = 0; | |
2409 | int len = attr->attr; | |
2410 | ||
2411 | if (len % sizeof(struct kvm_s390_irq) != 0) | |
2412 | return -EINVAL; | |
2413 | else if (len > KVM_S390_FLIC_MAX_BUFFER) | |
2414 | return -EINVAL; | |
2415 | ||
2416 | while (len >= sizeof(struct kvm_s390_irq)) { | |
c4196218 | 2417 | inti = kzalloc(sizeof(*inti), GFP_KERNEL_ACCOUNT); |
c05c4186 JF |
2418 | if (!inti) |
2419 | return -ENOMEM; | |
2420 | ||
2421 | r = copy_irq_from_user(inti, attr->addr); | |
2422 | if (r) { | |
2423 | kfree(inti); | |
2424 | return r; | |
2425 | } | |
a91b8ebe JF |
2426 | r = __inject_vm(dev->kvm, inti); |
2427 | if (r) { | |
2428 | kfree(inti); | |
2429 | return r; | |
2430 | } | |
c05c4186 JF |
2431 | len -= sizeof(struct kvm_s390_irq); |
2432 | attr->addr += sizeof(struct kvm_s390_irq); | |
2433 | } | |
2434 | ||
2435 | return r; | |
2436 | } | |
2437 | ||
841b91c5 CH |
2438 | static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id) |
2439 | { | |
2440 | if (id >= MAX_S390_IO_ADAPTERS) | |
2441 | return NULL; | |
58616e6a | 2442 | id = array_index_nospec(id, MAX_S390_IO_ADAPTERS); |
841b91c5 CH |
2443 | return kvm->arch.adapters[id]; |
2444 | } | |
2445 | ||
2446 | static int register_io_adapter(struct kvm_device *dev, | |
2447 | struct kvm_device_attr *attr) | |
2448 | { | |
2449 | struct s390_io_adapter *adapter; | |
2450 | struct kvm_s390_io_adapter adapter_info; | |
2451 | ||
2452 | if (copy_from_user(&adapter_info, | |
2453 | (void __user *)attr->addr, sizeof(adapter_info))) | |
2454 | return -EFAULT; | |
2455 | ||
58616e6a EF |
2456 | if (adapter_info.id >= MAX_S390_IO_ADAPTERS) |
2457 | return -EINVAL; | |
2458 | ||
2459 | adapter_info.id = array_index_nospec(adapter_info.id, | |
2460 | MAX_S390_IO_ADAPTERS); | |
2461 | ||
2462 | if (dev->kvm->arch.adapters[adapter_info.id] != NULL) | |
841b91c5 CH |
2463 | return -EINVAL; |
2464 | ||
c4196218 | 2465 | adapter = kzalloc(sizeof(*adapter), GFP_KERNEL_ACCOUNT); |
841b91c5 CH |
2466 | if (!adapter) |
2467 | return -ENOMEM; | |
2468 | ||
841b91c5 CH |
2469 | adapter->id = adapter_info.id; |
2470 | adapter->isc = adapter_info.isc; | |
2471 | adapter->maskable = adapter_info.maskable; | |
2472 | adapter->masked = false; | |
2473 | adapter->swap = adapter_info.swap; | |
08fab50d FL |
2474 | adapter->suppressible = (adapter_info.flags) & |
2475 | KVM_S390_ADAPTER_SUPPRESSIBLE; | |
841b91c5 CH |
2476 | dev->kvm->arch.adapters[adapter->id] = adapter; |
2477 | ||
2478 | return 0; | |
2479 | } | |
2480 | ||
2481 | int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked) | |
2482 | { | |
2483 | int ret; | |
2484 | struct s390_io_adapter *adapter = get_io_adapter(kvm, id); | |
2485 | ||
2486 | if (!adapter || !adapter->maskable) | |
2487 | return -EINVAL; | |
2488 | ret = adapter->masked; | |
2489 | adapter->masked = masked; | |
2490 | return ret; | |
2491 | } | |
2492 | ||
841b91c5 CH |
2493 | void kvm_s390_destroy_adapters(struct kvm *kvm) |
2494 | { | |
2495 | int i; | |
841b91c5 | 2496 | |
f6547066 | 2497 | for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) |
841b91c5 | 2498 | kfree(kvm->arch.adapters[i]); |
841b91c5 CH |
2499 | } |
2500 | ||
2501 | static int modify_io_adapter(struct kvm_device *dev, | |
2502 | struct kvm_device_attr *attr) | |
2503 | { | |
2504 | struct kvm_s390_io_adapter_req req; | |
2505 | struct s390_io_adapter *adapter; | |
2506 | int ret; | |
2507 | ||
2508 | if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req))) | |
2509 | return -EFAULT; | |
2510 | ||
2511 | adapter = get_io_adapter(dev->kvm, req.id); | |
2512 | if (!adapter) | |
2513 | return -EINVAL; | |
2514 | switch (req.type) { | |
2515 | case KVM_S390_IO_ADAPTER_MASK: | |
2516 | ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask); | |
2517 | if (ret > 0) | |
2518 | ret = 0; | |
2519 | break; | |
f6547066 UW |
2520 | /* |
2521 | * The following operations are no longer needed and therefore no-ops. | |
2522 | * The gpa to hva translation is done when an IRQ route is set up. The | |
2523 | * set_irq code uses get_user_pages_remote() to do the actual write. | |
2524 | */ | |
841b91c5 | 2525 | case KVM_S390_IO_ADAPTER_MAP: |
841b91c5 | 2526 | case KVM_S390_IO_ADAPTER_UNMAP: |
f6547066 | 2527 | ret = 0; |
841b91c5 CH |
2528 | break; |
2529 | default: | |
2530 | ret = -EINVAL; | |
2531 | } | |
2532 | ||
2533 | return ret; | |
2534 | } | |
2535 | ||
6d28f789 HP |
2536 | static int clear_io_irq(struct kvm *kvm, struct kvm_device_attr *attr) |
2537 | ||
2538 | { | |
2539 | const u64 isc_mask = 0xffUL << 24; /* all iscs set */ | |
2540 | u32 schid; | |
2541 | ||
2542 | if (attr->flags) | |
2543 | return -EINVAL; | |
2544 | if (attr->attr != sizeof(schid)) | |
2545 | return -EINVAL; | |
2546 | if (copy_from_user(&schid, (void __user *) attr->addr, sizeof(schid))) | |
2547 | return -EFAULT; | |
4dd6f17e MM |
2548 | if (!schid) |
2549 | return -EINVAL; | |
6d28f789 HP |
2550 | kfree(kvm_s390_get_io_int(kvm, isc_mask, schid)); |
2551 | /* | |
2552 | * If userspace is conforming to the architecture, we can have at most | |
2553 | * one pending I/O interrupt per subchannel, so this is effectively a | |
2554 | * clear all. | |
2555 | */ | |
2556 | return 0; | |
2557 | } | |
2558 | ||
51978393 FL |
2559 | static int modify_ais_mode(struct kvm *kvm, struct kvm_device_attr *attr) |
2560 | { | |
2561 | struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int; | |
2562 | struct kvm_s390_ais_req req; | |
2563 | int ret = 0; | |
2564 | ||
1ba15b24 | 2565 | if (!test_kvm_facility(kvm, 72)) |
c6119908 | 2566 | return -EOPNOTSUPP; |
51978393 FL |
2567 | |
2568 | if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req))) | |
2569 | return -EFAULT; | |
2570 | ||
2571 | if (req.isc > MAX_ISC) | |
2572 | return -EINVAL; | |
2573 | ||
2574 | trace_kvm_s390_modify_ais_mode(req.isc, | |
2575 | (fi->simm & AIS_MODE_MASK(req.isc)) ? | |
2576 | (fi->nimm & AIS_MODE_MASK(req.isc)) ? | |
2577 | 2 : KVM_S390_AIS_MODE_SINGLE : | |
2578 | KVM_S390_AIS_MODE_ALL, req.mode); | |
2579 | ||
2580 | mutex_lock(&fi->ais_lock); | |
2581 | switch (req.mode) { | |
2582 | case KVM_S390_AIS_MODE_ALL: | |
2583 | fi->simm &= ~AIS_MODE_MASK(req.isc); | |
2584 | fi->nimm &= ~AIS_MODE_MASK(req.isc); | |
2585 | break; | |
2586 | case KVM_S390_AIS_MODE_SINGLE: | |
2587 | fi->simm |= AIS_MODE_MASK(req.isc); | |
2588 | fi->nimm &= ~AIS_MODE_MASK(req.isc); | |
2589 | break; | |
2590 | default: | |
2591 | ret = -EINVAL; | |
2592 | } | |
2593 | mutex_unlock(&fi->ais_lock); | |
2594 | ||
2595 | return ret; | |
2596 | } | |
2597 | ||
a8920950 YMZ |
2598 | static int kvm_s390_inject_airq(struct kvm *kvm, |
2599 | struct s390_io_adapter *adapter) | |
2600 | { | |
2601 | struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int; | |
2602 | struct kvm_s390_interrupt s390int = { | |
2603 | .type = KVM_S390_INT_IO(1, 0, 0, 0), | |
2604 | .parm = 0, | |
2496c8e7 | 2605 | .parm64 = isc_to_int_word(adapter->isc), |
a8920950 YMZ |
2606 | }; |
2607 | int ret = 0; | |
2608 | ||
1ba15b24 | 2609 | if (!test_kvm_facility(kvm, 72) || !adapter->suppressible) |
a8920950 YMZ |
2610 | return kvm_s390_inject_vm(kvm, &s390int); |
2611 | ||
2612 | mutex_lock(&fi->ais_lock); | |
2613 | if (fi->nimm & AIS_MODE_MASK(adapter->isc)) { | |
2614 | trace_kvm_s390_airq_suppressed(adapter->id, adapter->isc); | |
2615 | goto out; | |
2616 | } | |
2617 | ||
2618 | ret = kvm_s390_inject_vm(kvm, &s390int); | |
2619 | if (!ret && (fi->simm & AIS_MODE_MASK(adapter->isc))) { | |
2620 | fi->nimm |= AIS_MODE_MASK(adapter->isc); | |
2621 | trace_kvm_s390_modify_ais_mode(adapter->isc, | |
2622 | KVM_S390_AIS_MODE_SINGLE, 2); | |
2623 | } | |
2624 | out: | |
2625 | mutex_unlock(&fi->ais_lock); | |
2626 | return ret; | |
2627 | } | |
2628 | ||
2629 | static int flic_inject_airq(struct kvm *kvm, struct kvm_device_attr *attr) | |
2630 | { | |
2631 | unsigned int id = attr->attr; | |
2632 | struct s390_io_adapter *adapter = get_io_adapter(kvm, id); | |
2633 | ||
2634 | if (!adapter) | |
2635 | return -EINVAL; | |
2636 | ||
2637 | return kvm_s390_inject_airq(kvm, adapter); | |
2638 | } | |
2639 | ||
2c1a48f2 YMZ |
2640 | static int flic_ais_mode_set_all(struct kvm *kvm, struct kvm_device_attr *attr) |
2641 | { | |
2642 | struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int; | |
2643 | struct kvm_s390_ais_all ais; | |
2644 | ||
2645 | if (!test_kvm_facility(kvm, 72)) | |
c6119908 | 2646 | return -EOPNOTSUPP; |
2c1a48f2 YMZ |
2647 | |
2648 | if (copy_from_user(&ais, (void __user *)attr->addr, sizeof(ais))) | |
2649 | return -EFAULT; | |
2650 | ||
2651 | mutex_lock(&fi->ais_lock); | |
2652 | fi->simm = ais.simm; | |
2653 | fi->nimm = ais.nimm; | |
2654 | mutex_unlock(&fi->ais_lock); | |
2655 | ||
2656 | return 0; | |
2657 | } | |
2658 | ||
c05c4186 JF |
2659 | static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr) |
2660 | { | |
2661 | int r = 0; | |
3c038e6b DD |
2662 | unsigned int i; |
2663 | struct kvm_vcpu *vcpu; | |
c05c4186 JF |
2664 | |
2665 | switch (attr->group) { | |
2666 | case KVM_DEV_FLIC_ENQUEUE: | |
2667 | r = enqueue_floating_irq(dev, attr); | |
2668 | break; | |
2669 | case KVM_DEV_FLIC_CLEAR_IRQS: | |
67335e63 | 2670 | kvm_s390_clear_float_irqs(dev->kvm); |
c05c4186 | 2671 | break; |
3c038e6b DD |
2672 | case KVM_DEV_FLIC_APF_ENABLE: |
2673 | dev->kvm->arch.gmap->pfault_enabled = 1; | |
2674 | break; | |
2675 | case KVM_DEV_FLIC_APF_DISABLE_WAIT: | |
2676 | dev->kvm->arch.gmap->pfault_enabled = 0; | |
2677 | /* | |
2678 | * Make sure no async faults are in transition when | |
2679 | * clearing the queues. So we don't need to worry | |
2680 | * about late coming workers. | |
2681 | */ | |
2682 | synchronize_srcu(&dev->kvm->srcu); | |
2683 | kvm_for_each_vcpu(i, vcpu, dev->kvm) | |
2684 | kvm_clear_async_pf_completion_queue(vcpu); | |
2685 | break; | |
841b91c5 CH |
2686 | case KVM_DEV_FLIC_ADAPTER_REGISTER: |
2687 | r = register_io_adapter(dev, attr); | |
2688 | break; | |
2689 | case KVM_DEV_FLIC_ADAPTER_MODIFY: | |
2690 | r = modify_io_adapter(dev, attr); | |
2691 | break; | |
6d28f789 HP |
2692 | case KVM_DEV_FLIC_CLEAR_IO_IRQ: |
2693 | r = clear_io_irq(dev->kvm, attr); | |
2694 | break; | |
51978393 FL |
2695 | case KVM_DEV_FLIC_AISM: |
2696 | r = modify_ais_mode(dev->kvm, attr); | |
2697 | break; | |
a8920950 YMZ |
2698 | case KVM_DEV_FLIC_AIRQ_INJECT: |
2699 | r = flic_inject_airq(dev->kvm, attr); | |
2700 | break; | |
2c1a48f2 YMZ |
2701 | case KVM_DEV_FLIC_AISM_ALL: |
2702 | r = flic_ais_mode_set_all(dev->kvm, attr); | |
2703 | break; | |
c05c4186 JF |
2704 | default: |
2705 | r = -EINVAL; | |
2706 | } | |
2707 | ||
2708 | return r; | |
2709 | } | |
2710 | ||
4f129858 HP |
2711 | static int flic_has_attr(struct kvm_device *dev, |
2712 | struct kvm_device_attr *attr) | |
2713 | { | |
2714 | switch (attr->group) { | |
2715 | case KVM_DEV_FLIC_GET_ALL_IRQS: | |
2716 | case KVM_DEV_FLIC_ENQUEUE: | |
2717 | case KVM_DEV_FLIC_CLEAR_IRQS: | |
2718 | case KVM_DEV_FLIC_APF_ENABLE: | |
2719 | case KVM_DEV_FLIC_APF_DISABLE_WAIT: | |
2720 | case KVM_DEV_FLIC_ADAPTER_REGISTER: | |
2721 | case KVM_DEV_FLIC_ADAPTER_MODIFY: | |
6d28f789 | 2722 | case KVM_DEV_FLIC_CLEAR_IO_IRQ: |
51978393 | 2723 | case KVM_DEV_FLIC_AISM: |
a8920950 | 2724 | case KVM_DEV_FLIC_AIRQ_INJECT: |
2c1a48f2 | 2725 | case KVM_DEV_FLIC_AISM_ALL: |
4f129858 HP |
2726 | return 0; |
2727 | } | |
2728 | return -ENXIO; | |
2729 | } | |
2730 | ||
c05c4186 JF |
2731 | static int flic_create(struct kvm_device *dev, u32 type) |
2732 | { | |
2733 | if (!dev) | |
2734 | return -EINVAL; | |
2735 | if (dev->kvm->arch.flic) | |
2736 | return -EINVAL; | |
2737 | dev->kvm->arch.flic = dev; | |
2738 | return 0; | |
2739 | } | |
2740 | ||
2741 | static void flic_destroy(struct kvm_device *dev) | |
2742 | { | |
2743 | dev->kvm->arch.flic = NULL; | |
2744 | kfree(dev); | |
2745 | } | |
2746 | ||
2747 | /* s390 floating irq controller (flic) */ | |
2748 | struct kvm_device_ops kvm_flic_ops = { | |
2749 | .name = "kvm-flic", | |
2750 | .get_attr = flic_get_attr, | |
2751 | .set_attr = flic_set_attr, | |
4f129858 | 2752 | .has_attr = flic_has_attr, |
c05c4186 JF |
2753 | .create = flic_create, |
2754 | .destroy = flic_destroy, | |
2755 | }; | |
84223598 CH |
2756 | |
2757 | static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap) | |
2758 | { | |
2759 | unsigned long bit; | |
2760 | ||
2761 | bit = bit_nr + (addr % PAGE_SIZE) * 8; | |
2762 | ||
2763 | return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit; | |
2764 | } | |
2765 | ||
f6547066 | 2766 | static struct page *get_map_page(struct kvm *kvm, u64 uaddr) |
84223598 | 2767 | { |
f6547066 | 2768 | struct page *page = NULL; |
84223598 | 2769 | |
d8ed45c5 | 2770 | mmap_read_lock(kvm->mm); |
64019a2e | 2771 | get_user_pages_remote(kvm->mm, uaddr, 1, FOLL_WRITE, |
f6547066 | 2772 | &page, NULL, NULL); |
d8ed45c5 | 2773 | mmap_read_unlock(kvm->mm); |
f6547066 | 2774 | return page; |
84223598 CH |
2775 | } |
2776 | ||
2777 | static int adapter_indicators_set(struct kvm *kvm, | |
2778 | struct s390_io_adapter *adapter, | |
2779 | struct kvm_s390_adapter_int *adapter_int) | |
2780 | { | |
2781 | unsigned long bit; | |
2782 | int summary_set, idx; | |
f6547066 | 2783 | struct page *ind_page, *summary_page; |
84223598 CH |
2784 | void *map; |
2785 | ||
f6547066 UW |
2786 | ind_page = get_map_page(kvm, adapter_int->ind_addr); |
2787 | if (!ind_page) | |
84223598 | 2788 | return -1; |
f6547066 UW |
2789 | summary_page = get_map_page(kvm, adapter_int->summary_addr); |
2790 | if (!summary_page) { | |
2791 | put_page(ind_page); | |
84223598 CH |
2792 | return -1; |
2793 | } | |
f6547066 UW |
2794 | |
2795 | idx = srcu_read_lock(&kvm->srcu); | |
2796 | map = page_address(ind_page); | |
2797 | bit = get_ind_bit(adapter_int->ind_addr, | |
2798 | adapter_int->ind_offset, adapter->swap); | |
2799 | set_bit(bit, map); | |
2800 | mark_page_dirty(kvm, adapter_int->ind_addr >> PAGE_SHIFT); | |
2801 | set_page_dirty_lock(ind_page); | |
2802 | map = page_address(summary_page); | |
2803 | bit = get_ind_bit(adapter_int->summary_addr, | |
2804 | adapter_int->summary_offset, adapter->swap); | |
84223598 | 2805 | summary_set = test_and_set_bit(bit, map); |
f6547066 UW |
2806 | mark_page_dirty(kvm, adapter_int->summary_addr >> PAGE_SHIFT); |
2807 | set_page_dirty_lock(summary_page); | |
84223598 | 2808 | srcu_read_unlock(&kvm->srcu, idx); |
f6547066 UW |
2809 | |
2810 | put_page(ind_page); | |
2811 | put_page(summary_page); | |
84223598 CH |
2812 | return summary_set ? 0 : 1; |
2813 | } | |
2814 | ||
2815 | /* | |
2816 | * < 0 - not injected due to error | |
2817 | * = 0 - coalesced, summary indicator already active | |
2818 | * > 0 - injected interrupt | |
2819 | */ | |
2820 | static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e, | |
2821 | struct kvm *kvm, int irq_source_id, int level, | |
2822 | bool line_status) | |
2823 | { | |
2824 | int ret; | |
2825 | struct s390_io_adapter *adapter; | |
2826 | ||
2827 | /* We're only interested in the 0->1 transition. */ | |
2828 | if (!level) | |
2829 | return 0; | |
2830 | adapter = get_io_adapter(kvm, e->adapter.adapter_id); | |
2831 | if (!adapter) | |
2832 | return -1; | |
84223598 | 2833 | ret = adapter_indicators_set(kvm, adapter, &e->adapter); |
84223598 | 2834 | if ((ret > 0) && !adapter->masked) { |
a8920950 | 2835 | ret = kvm_s390_inject_airq(kvm, adapter); |
84223598 CH |
2836 | if (ret == 0) |
2837 | ret = 1; | |
2838 | } | |
2839 | return ret; | |
2840 | } | |
2841 | ||
4d62fcc0 QH |
2842 | /* |
2843 | * Inject the machine check to the guest. | |
2844 | */ | |
2845 | void kvm_s390_reinject_machine_check(struct kvm_vcpu *vcpu, | |
2846 | struct mcck_volatile_info *mcck_info) | |
2847 | { | |
2848 | struct kvm_s390_interrupt_info inti; | |
2849 | struct kvm_s390_irq irq; | |
2850 | struct kvm_s390_mchk_info *mchk; | |
2851 | union mci mci; | |
2852 | __u64 cr14 = 0; /* upper bits are not used */ | |
3dbf0205 | 2853 | int rc; |
4d62fcc0 QH |
2854 | |
2855 | mci.val = mcck_info->mcic; | |
2856 | if (mci.sr) | |
cc65450c | 2857 | cr14 |= CR14_RECOVERY_SUBMASK; |
4d62fcc0 | 2858 | if (mci.dg) |
cc65450c | 2859 | cr14 |= CR14_DEGRADATION_SUBMASK; |
4d62fcc0 | 2860 | if (mci.w) |
cc65450c | 2861 | cr14 |= CR14_WARNING_SUBMASK; |
4d62fcc0 QH |
2862 | |
2863 | mchk = mci.ck ? &inti.mchk : &irq.u.mchk; | |
2864 | mchk->cr14 = cr14; | |
2865 | mchk->mcic = mcck_info->mcic; | |
2866 | mchk->ext_damage_code = mcck_info->ext_damage_code; | |
2867 | mchk->failing_storage_address = mcck_info->failing_storage_address; | |
2868 | if (mci.ck) { | |
2869 | /* Inject the floating machine check */ | |
2870 | inti.type = KVM_S390_MCHK; | |
3dbf0205 | 2871 | rc = __inject_vm(vcpu->kvm, &inti); |
4d62fcc0 QH |
2872 | } else { |
2873 | /* Inject the machine check to specified vcpu */ | |
2874 | irq.type = KVM_S390_MCHK; | |
3dbf0205 | 2875 | rc = kvm_s390_inject_vcpu(vcpu, &irq); |
4d62fcc0 | 2876 | } |
3dbf0205 | 2877 | WARN_ON_ONCE(rc); |
4d62fcc0 QH |
2878 | } |
2879 | ||
c63cf538 RK |
2880 | int kvm_set_routing_entry(struct kvm *kvm, |
2881 | struct kvm_kernel_irq_routing_entry *e, | |
84223598 CH |
2882 | const struct kvm_irq_routing_entry *ue) |
2883 | { | |
f6547066 | 2884 | u64 uaddr; |
84223598 CH |
2885 | |
2886 | switch (ue->type) { | |
f6547066 | 2887 | /* we store the userspace addresses instead of the guest addresses */ |
84223598 CH |
2888 | case KVM_IRQ_ROUTING_S390_ADAPTER: |
2889 | e->set = set_adapter_int; | |
f6547066 UW |
2890 | uaddr = gmap_translate(kvm->arch.gmap, ue->u.adapter.summary_addr); |
2891 | if (uaddr == -EFAULT) | |
2892 | return -EFAULT; | |
2893 | e->adapter.summary_addr = uaddr; | |
2894 | uaddr = gmap_translate(kvm->arch.gmap, ue->u.adapter.ind_addr); | |
2895 | if (uaddr == -EFAULT) | |
2896 | return -EFAULT; | |
2897 | e->adapter.ind_addr = uaddr; | |
84223598 CH |
2898 | e->adapter.summary_offset = ue->u.adapter.summary_offset; |
2899 | e->adapter.ind_offset = ue->u.adapter.ind_offset; | |
2900 | e->adapter.adapter_id = ue->u.adapter.adapter_id; | |
f6547066 | 2901 | return 0; |
84223598 | 2902 | default: |
f6547066 | 2903 | return -EINVAL; |
84223598 | 2904 | } |
84223598 CH |
2905 | } |
2906 | ||
2907 | int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm, | |
2908 | int irq_source_id, int level, bool line_status) | |
2909 | { | |
2910 | return -EINVAL; | |
2911 | } | |
816c7667 JF |
2912 | |
2913 | int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu, void __user *irqstate, int len) | |
2914 | { | |
2915 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; | |
2916 | struct kvm_s390_irq *buf; | |
2917 | int r = 0; | |
2918 | int n; | |
2919 | ||
2920 | buf = vmalloc(len); | |
2921 | if (!buf) | |
2922 | return -ENOMEM; | |
2923 | ||
2924 | if (copy_from_user((void *) buf, irqstate, len)) { | |
2925 | r = -EFAULT; | |
2926 | goto out_free; | |
2927 | } | |
2928 | ||
2929 | /* | |
2930 | * Don't allow setting the interrupt state | |
2931 | * when there are already interrupts pending | |
2932 | */ | |
2933 | spin_lock(&li->lock); | |
2934 | if (li->pending_irqs) { | |
2935 | r = -EBUSY; | |
2936 | goto out_unlock; | |
2937 | } | |
2938 | ||
2939 | for (n = 0; n < len / sizeof(*buf); n++) { | |
2940 | r = do_inject_vcpu(vcpu, &buf[n]); | |
2941 | if (r) | |
2942 | break; | |
2943 | } | |
2944 | ||
2945 | out_unlock: | |
2946 | spin_unlock(&li->lock); | |
2947 | out_free: | |
2948 | vfree(buf); | |
2949 | ||
2950 | return r; | |
2951 | } | |
2952 | ||
2953 | static void store_local_irq(struct kvm_s390_local_interrupt *li, | |
2954 | struct kvm_s390_irq *irq, | |
2955 | unsigned long irq_type) | |
2956 | { | |
2957 | switch (irq_type) { | |
2958 | case IRQ_PEND_MCHK_EX: | |
2959 | case IRQ_PEND_MCHK_REP: | |
2960 | irq->type = KVM_S390_MCHK; | |
2961 | irq->u.mchk = li->irq.mchk; | |
2962 | break; | |
2963 | case IRQ_PEND_PROG: | |
2964 | irq->type = KVM_S390_PROGRAM_INT; | |
2965 | irq->u.pgm = li->irq.pgm; | |
2966 | break; | |
2967 | case IRQ_PEND_PFAULT_INIT: | |
2968 | irq->type = KVM_S390_INT_PFAULT_INIT; | |
2969 | irq->u.ext = li->irq.ext; | |
2970 | break; | |
2971 | case IRQ_PEND_EXT_EXTERNAL: | |
2972 | irq->type = KVM_S390_INT_EXTERNAL_CALL; | |
2973 | irq->u.extcall = li->irq.extcall; | |
2974 | break; | |
2975 | case IRQ_PEND_EXT_CLOCK_COMP: | |
2976 | irq->type = KVM_S390_INT_CLOCK_COMP; | |
2977 | break; | |
2978 | case IRQ_PEND_EXT_CPU_TIMER: | |
2979 | irq->type = KVM_S390_INT_CPU_TIMER; | |
2980 | break; | |
2981 | case IRQ_PEND_SIGP_STOP: | |
2982 | irq->type = KVM_S390_SIGP_STOP; | |
2983 | irq->u.stop = li->irq.stop; | |
2984 | break; | |
2985 | case IRQ_PEND_RESTART: | |
2986 | irq->type = KVM_S390_RESTART; | |
2987 | break; | |
2988 | case IRQ_PEND_SET_PREFIX: | |
2989 | irq->type = KVM_S390_SIGP_SET_PREFIX; | |
2990 | irq->u.prefix = li->irq.prefix; | |
2991 | break; | |
2992 | } | |
2993 | } | |
2994 | ||
2995 | int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu, __u8 __user *buf, int len) | |
2996 | { | |
a5bd7647 | 2997 | int scn; |
689bdf9e | 2998 | DECLARE_BITMAP(sigp_emerg_pending, KVM_MAX_VCPUS); |
816c7667 JF |
2999 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
3000 | unsigned long pending_irqs; | |
3001 | struct kvm_s390_irq irq; | |
3002 | unsigned long irq_type; | |
3003 | int cpuaddr; | |
3004 | int n = 0; | |
3005 | ||
3006 | spin_lock(&li->lock); | |
3007 | pending_irqs = li->pending_irqs; | |
3008 | memcpy(&sigp_emerg_pending, &li->sigp_emerg_pending, | |
3009 | sizeof(sigp_emerg_pending)); | |
3010 | spin_unlock(&li->lock); | |
3011 | ||
3012 | for_each_set_bit(irq_type, &pending_irqs, IRQ_PEND_COUNT) { | |
3013 | memset(&irq, 0, sizeof(irq)); | |
3014 | if (irq_type == IRQ_PEND_EXT_EMERGENCY) | |
3015 | continue; | |
3016 | if (n + sizeof(irq) > len) | |
3017 | return -ENOBUFS; | |
3018 | store_local_irq(&vcpu->arch.local_int, &irq, irq_type); | |
3019 | if (copy_to_user(&buf[n], &irq, sizeof(irq))) | |
3020 | return -EFAULT; | |
3021 | n += sizeof(irq); | |
3022 | } | |
3023 | ||
3024 | if (test_bit(IRQ_PEND_EXT_EMERGENCY, &pending_irqs)) { | |
3025 | for_each_set_bit(cpuaddr, sigp_emerg_pending, KVM_MAX_VCPUS) { | |
3026 | memset(&irq, 0, sizeof(irq)); | |
3027 | if (n + sizeof(irq) > len) | |
3028 | return -ENOBUFS; | |
3029 | irq.type = KVM_S390_INT_EMERGENCY; | |
3030 | irq.u.emerg.code = cpuaddr; | |
3031 | if (copy_to_user(&buf[n], &irq, sizeof(irq))) | |
3032 | return -EFAULT; | |
3033 | n += sizeof(irq); | |
3034 | } | |
3035 | } | |
3036 | ||
a5bd7647 | 3037 | if (sca_ext_call_pending(vcpu, &scn)) { |
816c7667 JF |
3038 | if (n + sizeof(irq) > len) |
3039 | return -ENOBUFS; | |
3040 | memset(&irq, 0, sizeof(irq)); | |
3041 | irq.type = KVM_S390_INT_EXTERNAL_CALL; | |
a5bd7647 | 3042 | irq.u.extcall.code = scn; |
816c7667 JF |
3043 | if (copy_to_user(&buf[n], &irq, sizeof(irq))) |
3044 | return -EFAULT; | |
3045 | n += sizeof(irq); | |
3046 | } | |
3047 | ||
3048 | return n; | |
3049 | } | |
d7c5cb01 | 3050 | |
9f30f621 MM |
3051 | static void __airqs_kick_single_vcpu(struct kvm *kvm, u8 deliverable_mask) |
3052 | { | |
a3e03bc1 | 3053 | int vcpu_idx, online_vcpus = atomic_read(&kvm->online_vcpus); |
9f30f621 MM |
3054 | struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int; |
3055 | struct kvm_vcpu *vcpu; | |
0e9ff65f | 3056 | u8 vcpu_isc_mask; |
9f30f621 | 3057 | |
a3e03bc1 HP |
3058 | for_each_set_bit(vcpu_idx, kvm->arch.idle_mask, online_vcpus) { |
3059 | vcpu = kvm_get_vcpu(kvm, vcpu_idx); | |
9f30f621 MM |
3060 | if (psw_ioint_disabled(vcpu)) |
3061 | continue; | |
0e9ff65f HP |
3062 | vcpu_isc_mask = (u8)(vcpu->arch.sie_block->gcr[6] >> 24); |
3063 | if (deliverable_mask & vcpu_isc_mask) { | |
9f30f621 | 3064 | /* lately kicked but not yet running */ |
a3e03bc1 | 3065 | if (test_and_set_bit(vcpu_idx, gi->kicked_mask)) |
9f30f621 MM |
3066 | return; |
3067 | kvm_s390_vcpu_wakeup(vcpu); | |
3068 | return; | |
3069 | } | |
3070 | } | |
3071 | } | |
3072 | ||
3073 | static enum hrtimer_restart gisa_vcpu_kicker(struct hrtimer *timer) | |
3074 | { | |
3075 | struct kvm_s390_gisa_interrupt *gi = | |
3076 | container_of(timer, struct kvm_s390_gisa_interrupt, timer); | |
3077 | struct kvm *kvm = | |
3078 | container_of(gi->origin, struct sie_page2, gisa)->kvm; | |
3079 | u8 pending_mask; | |
3080 | ||
3081 | pending_mask = gisa_get_ipm_or_restore_iam(gi); | |
3082 | if (pending_mask) { | |
3083 | __airqs_kick_single_vcpu(kvm, pending_mask); | |
3084 | hrtimer_forward_now(timer, ns_to_ktime(gi->expires)); | |
3085 | return HRTIMER_RESTART; | |
0b545fd1 | 3086 | } |
9f30f621 MM |
3087 | |
3088 | return HRTIMER_NORESTART; | |
3089 | } | |
3090 | ||
3091 | #define NULL_GISA_ADDR 0x00000000UL | |
3092 | #define NONE_GISA_ADDR 0x00000001UL | |
3093 | #define GISA_ADDR_MASK 0xfffff000UL | |
3094 | ||
3095 | static void process_gib_alert_list(void) | |
3096 | { | |
3097 | struct kvm_s390_gisa_interrupt *gi; | |
3098 | struct kvm_s390_gisa *gisa; | |
3099 | struct kvm *kvm; | |
3100 | u32 final, origin = 0UL; | |
3101 | ||
3102 | do { | |
3103 | /* | |
3104 | * If the NONE_GISA_ADDR is still stored in the alert list | |
3105 | * origin, we will leave the outer loop. No further GISA has | |
3106 | * been added to the alert list by millicode while processing | |
3107 | * the current alert list. | |
3108 | */ | |
3109 | final = (origin & NONE_GISA_ADDR); | |
3110 | /* | |
3111 | * Cut off the alert list and store the NONE_GISA_ADDR in the | |
3112 | * alert list origin to avoid further GAL interruptions. | |
3113 | * A new alert list can be build up by millicode in parallel | |
3114 | * for guests not in the yet cut-off alert list. When in the | |
3115 | * final loop, store the NULL_GISA_ADDR instead. This will re- | |
3116 | * enable GAL interruptions on the host again. | |
3117 | */ | |
3118 | origin = xchg(&gib->alert_list_origin, | |
3119 | (!final) ? NONE_GISA_ADDR : NULL_GISA_ADDR); | |
3120 | /* | |
3121 | * Loop through the just cut-off alert list and start the | |
3122 | * gisa timers to kick idle vcpus to consume the pending | |
3123 | * interruptions asap. | |
3124 | */ | |
3125 | while (origin & GISA_ADDR_MASK) { | |
3126 | gisa = (struct kvm_s390_gisa *)(u64)origin; | |
3127 | origin = gisa->next_alert; | |
3128 | gisa->next_alert = (u32)(u64)gisa; | |
3129 | kvm = container_of(gisa, struct sie_page2, gisa)->kvm; | |
3130 | gi = &kvm->arch.gisa_int; | |
3131 | if (hrtimer_active(&gi->timer)) | |
3132 | hrtimer_cancel(&gi->timer); | |
3133 | hrtimer_start(&gi->timer, 0, HRTIMER_MODE_REL); | |
3134 | } | |
3135 | } while (!final); | |
3136 | ||
3137 | } | |
3138 | ||
d7c5cb01 MM |
3139 | void kvm_s390_gisa_clear(struct kvm *kvm) |
3140 | { | |
982cff42 MM |
3141 | struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int; |
3142 | ||
3143 | if (!gi->origin) | |
672128bf | 3144 | return; |
174dd4f8 | 3145 | gisa_clear_ipm(gi->origin); |
982cff42 | 3146 | VM_EVENT(kvm, 3, "gisa 0x%pK cleared", gi->origin); |
d7c5cb01 MM |
3147 | } |
3148 | ||
3149 | void kvm_s390_gisa_init(struct kvm *kvm) | |
3150 | { | |
982cff42 MM |
3151 | struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int; |
3152 | ||
672128bf MM |
3153 | if (!css_general_characteristics.aiv) |
3154 | return; | |
982cff42 | 3155 | gi->origin = &kvm->arch.sie_page2->gisa; |
6cff2e10 MM |
3156 | gi->alert.mask = 0; |
3157 | spin_lock_init(&gi->alert.ref_lock); | |
9f30f621 MM |
3158 | gi->expires = 50 * 1000; /* 50 usec */ |
3159 | hrtimer_init(&gi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
3160 | gi->timer.function = gisa_vcpu_kicker; | |
174dd4f8 MM |
3161 | memset(gi->origin, 0, sizeof(struct kvm_s390_gisa)); |
3162 | gi->origin->next_alert = (u32)(u64)gi->origin; | |
982cff42 | 3163 | VM_EVENT(kvm, 3, "gisa 0x%pK initialized", gi->origin); |
d7c5cb01 MM |
3164 | } |
3165 | ||
3166 | void kvm_s390_gisa_destroy(struct kvm *kvm) | |
3167 | { | |
9f30f621 MM |
3168 | struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int; |
3169 | ||
3170 | if (!gi->origin) | |
3171 | return; | |
3172 | if (gi->alert.mask) | |
3173 | KVM_EVENT(3, "vm 0x%pK has unexpected iam 0x%02x", | |
3174 | kvm, gi->alert.mask); | |
3175 | while (gisa_in_alert_list(gi->origin)) | |
3176 | cpu_relax(); | |
3177 | hrtimer_cancel(&gi->timer); | |
3178 | gi->origin = NULL; | |
d7c5cb01 | 3179 | } |
1282c21e | 3180 | |
6cff2e10 MM |
3181 | /** |
3182 | * kvm_s390_gisc_register - register a guest ISC | |
3183 | * | |
3184 | * @kvm: the kernel vm to work with | |
3185 | * @gisc: the guest interruption sub class to register | |
3186 | * | |
3187 | * The function extends the vm specific alert mask to use. | |
3188 | * The effective IAM mask in the GISA is updated as well | |
3189 | * in case the GISA is not part of the GIB alert list. | |
3190 | * It will be updated latest when the IAM gets restored | |
3191 | * by gisa_get_ipm_or_restore_iam(). | |
3192 | * | |
3193 | * Returns: the nonspecific ISC (NISC) the gib alert mechanism | |
3194 | * has registered with the channel subsystem. | |
3195 | * -ENODEV in case the vm uses no GISA | |
3196 | * -ERANGE in case the guest ISC is invalid | |
3197 | */ | |
3198 | int kvm_s390_gisc_register(struct kvm *kvm, u32 gisc) | |
3199 | { | |
3200 | struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int; | |
3201 | ||
3202 | if (!gi->origin) | |
3203 | return -ENODEV; | |
3204 | if (gisc > MAX_ISC) | |
3205 | return -ERANGE; | |
3206 | ||
3207 | spin_lock(&gi->alert.ref_lock); | |
3208 | gi->alert.ref_count[gisc]++; | |
3209 | if (gi->alert.ref_count[gisc] == 1) { | |
3210 | gi->alert.mask |= 0x80 >> gisc; | |
3211 | gisa_set_iam(gi->origin, gi->alert.mask); | |
3212 | } | |
3213 | spin_unlock(&gi->alert.ref_lock); | |
3214 | ||
3215 | return gib->nisc; | |
3216 | } | |
3217 | EXPORT_SYMBOL_GPL(kvm_s390_gisc_register); | |
3218 | ||
3219 | /** | |
3220 | * kvm_s390_gisc_unregister - unregister a guest ISC | |
3221 | * | |
3222 | * @kvm: the kernel vm to work with | |
3223 | * @gisc: the guest interruption sub class to register | |
3224 | * | |
3225 | * The function reduces the vm specific alert mask to use. | |
3226 | * The effective IAM mask in the GISA is updated as well | |
3227 | * in case the GISA is not part of the GIB alert list. | |
3228 | * It will be updated latest when the IAM gets restored | |
3229 | * by gisa_get_ipm_or_restore_iam(). | |
3230 | * | |
3231 | * Returns: the nonspecific ISC (NISC) the gib alert mechanism | |
3232 | * has registered with the channel subsystem. | |
3233 | * -ENODEV in case the vm uses no GISA | |
3234 | * -ERANGE in case the guest ISC is invalid | |
3235 | * -EINVAL in case the guest ISC is not registered | |
3236 | */ | |
3237 | int kvm_s390_gisc_unregister(struct kvm *kvm, u32 gisc) | |
3238 | { | |
3239 | struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int; | |
3240 | int rc = 0; | |
3241 | ||
3242 | if (!gi->origin) | |
3243 | return -ENODEV; | |
3244 | if (gisc > MAX_ISC) | |
3245 | return -ERANGE; | |
3246 | ||
3247 | spin_lock(&gi->alert.ref_lock); | |
3248 | if (gi->alert.ref_count[gisc] == 0) { | |
3249 | rc = -EINVAL; | |
3250 | goto out; | |
3251 | } | |
3252 | gi->alert.ref_count[gisc]--; | |
3253 | if (gi->alert.ref_count[gisc] == 0) { | |
3254 | gi->alert.mask &= ~(0x80 >> gisc); | |
3255 | gisa_set_iam(gi->origin, gi->alert.mask); | |
3256 | } | |
3257 | out: | |
3258 | spin_unlock(&gi->alert.ref_lock); | |
3259 | ||
3260 | return rc; | |
3261 | } | |
3262 | EXPORT_SYMBOL_GPL(kvm_s390_gisc_unregister); | |
3263 | ||
30e63ef2 | 3264 | static void gib_alert_irq_handler(struct airq_struct *airq, bool floating) |
9f30f621 MM |
3265 | { |
3266 | inc_irq_stat(IRQIO_GAL); | |
3267 | process_gib_alert_list(); | |
3268 | } | |
3269 | ||
3270 | static struct airq_struct gib_alert_irq = { | |
3271 | .handler = gib_alert_irq_handler, | |
3272 | .lsi_ptr = &gib_alert_irq.lsi_mask, | |
3273 | }; | |
3274 | ||
1282c21e MM |
3275 | void kvm_s390_gib_destroy(void) |
3276 | { | |
3277 | if (!gib) | |
3278 | return; | |
3279 | chsc_sgib(0); | |
9f30f621 | 3280 | unregister_adapter_interrupt(&gib_alert_irq); |
1282c21e MM |
3281 | free_page((unsigned long)gib); |
3282 | gib = NULL; | |
3283 | } | |
3284 | ||
3285 | int kvm_s390_gib_init(u8 nisc) | |
3286 | { | |
3287 | int rc = 0; | |
3288 | ||
3289 | if (!css_general_characteristics.aiv) { | |
3290 | KVM_EVENT(3, "%s", "gib not initialized, no AIV facility"); | |
3291 | goto out; | |
3292 | } | |
3293 | ||
c4196218 | 3294 | gib = (struct kvm_s390_gib *)get_zeroed_page(GFP_KERNEL_ACCOUNT | GFP_DMA); |
1282c21e MM |
3295 | if (!gib) { |
3296 | rc = -ENOMEM; | |
3297 | goto out; | |
3298 | } | |
3299 | ||
9f30f621 MM |
3300 | gib_alert_irq.isc = nisc; |
3301 | if (register_adapter_interrupt(&gib_alert_irq)) { | |
3302 | pr_err("Registering the GIB alert interruption handler failed\n"); | |
3303 | rc = -EIO; | |
3304 | goto out_free_gib; | |
3305 | } | |
3306 | ||
1282c21e MM |
3307 | gib->nisc = nisc; |
3308 | if (chsc_sgib((u32)(u64)gib)) { | |
3309 | pr_err("Associating the GIB with the AIV facility failed\n"); | |
3310 | free_page((unsigned long)gib); | |
3311 | gib = NULL; | |
3312 | rc = -EIO; | |
9f30f621 | 3313 | goto out_unreg_gal; |
1282c21e MM |
3314 | } |
3315 | ||
3316 | KVM_EVENT(3, "gib 0x%pK (nisc=%d) initialized", gib, gib->nisc); | |
9f30f621 MM |
3317 | goto out; |
3318 | ||
3319 | out_unreg_gal: | |
3320 | unregister_adapter_interrupt(&gib_alert_irq); | |
3321 | out_free_gib: | |
3322 | free_page((unsigned long)gib); | |
3323 | gib = NULL; | |
1282c21e MM |
3324 | out: |
3325 | return rc; | |
3326 | } |