~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/s390/kvm/interrupt.c

Version: ~ [ linux-5.4-rc3 ] ~ [ linux-5.3.6 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.79 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.149 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.196 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.196 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.75 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * handling kvm guest interrupts
  3  *
  4  * Copyright IBM Corp. 2008, 2015
  5  *
  6  * This program is free software; you can redistribute it and/or modify
  7  * it under the terms of the GNU General Public License (version 2 only)
  8  * as published by the Free Software Foundation.
  9  *
 10  *    Author(s): Carsten Otte <cotte@de.ibm.com>
 11  */
 12 
 13 #include <linux/interrupt.h>
 14 #include <linux/kvm_host.h>
 15 #include <linux/hrtimer.h>
 16 #include <linux/mmu_context.h>
 17 #include <linux/signal.h>
 18 #include <linux/slab.h>
 19 #include <linux/bitmap.h>
 20 #include <linux/vmalloc.h>
 21 #include <asm/asm-offsets.h>
 22 #include <asm/dis.h>
 23 #include <asm/uaccess.h>
 24 #include <asm/sclp.h>
 25 #include <asm/isc.h>
 26 #include "kvm-s390.h"
 27 #include "gaccess.h"
 28 #include "trace-s390.h"
 29 
 30 #define IOINT_SCHID_MASK 0x0000ffff
 31 #define IOINT_SSID_MASK 0x00030000
 32 #define IOINT_CSSID_MASK 0x03fc0000
 33 #define IOINT_AI_MASK 0x04000000
 34 #define PFAULT_INIT 0x0600
 35 #define PFAULT_DONE 0x0680
 36 #define VIRTIO_PARAM 0x0d00
 37 
 38 int psw_extint_disabled(struct kvm_vcpu *vcpu)
 39 {
 40         return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT);
 41 }
 42 
 43 static int psw_ioint_disabled(struct kvm_vcpu *vcpu)
 44 {
 45         return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO);
 46 }
 47 
 48 static int psw_mchk_disabled(struct kvm_vcpu *vcpu)
 49 {
 50         return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK);
 51 }
 52 
 53 static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
 54 {
 55         if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER) ||
 56             (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO) ||
 57             (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT))
 58                 return 0;
 59         return 1;
 60 }
 61 
 62 static int ckc_interrupts_enabled(struct kvm_vcpu *vcpu)
 63 {
 64         if (psw_extint_disabled(vcpu) ||
 65             !(vcpu->arch.sie_block->gcr[0] & 0x800ul))
 66                 return 0;
 67         if (guestdbg_enabled(vcpu) && guestdbg_sstep_enabled(vcpu))
 68                 /* No timer interrupts when single stepping */
 69                 return 0;
 70         return 1;
 71 }
 72 
 73 static int ckc_irq_pending(struct kvm_vcpu *vcpu)
 74 {
 75         if (!(vcpu->arch.sie_block->ckc <
 76               get_tod_clock_fast() + vcpu->arch.sie_block->epoch))
 77                 return 0;
 78         return ckc_interrupts_enabled(vcpu);
 79 }
 80 
 81 static int cpu_timer_interrupts_enabled(struct kvm_vcpu *vcpu)
 82 {
 83         return !psw_extint_disabled(vcpu) &&
 84                (vcpu->arch.sie_block->gcr[0] & 0x400ul);
 85 }
 86 
 87 static int cpu_timer_irq_pending(struct kvm_vcpu *vcpu)
 88 {
 89         return (vcpu->arch.sie_block->cputm >> 63) &&
 90                cpu_timer_interrupts_enabled(vcpu);
 91 }
 92 
 93 static inline int is_ioirq(unsigned long irq_type)
 94 {
 95         return ((irq_type >= IRQ_PEND_IO_ISC_0) &&
 96                 (irq_type <= IRQ_PEND_IO_ISC_7));
 97 }
 98 
 99 static uint64_t isc_to_isc_bits(int isc)
100 {
101         return (0x80 >> isc) << 24;
102 }
103 
104 static inline u8 int_word_to_isc(u32 int_word)
105 {
106         return (int_word & 0x38000000) >> 27;
107 }
108 
109 static inline unsigned long pending_floating_irqs(struct kvm_vcpu *vcpu)
110 {
111         return vcpu->kvm->arch.float_int.pending_irqs;
112 }
113 
114 static inline unsigned long pending_local_irqs(struct kvm_vcpu *vcpu)
115 {
116         return vcpu->arch.local_int.pending_irqs;
117 }
118 
119 static unsigned long disable_iscs(struct kvm_vcpu *vcpu,
120                                    unsigned long active_mask)
121 {
122         int i;
123 
124         for (i = 0; i <= MAX_ISC; i++)
125                 if (!(vcpu->arch.sie_block->gcr[6] & isc_to_isc_bits(i)))
126                         active_mask &= ~(1UL << (IRQ_PEND_IO_ISC_0 + i));
127 
128         return active_mask;
129 }
130 
131 static unsigned long deliverable_irqs(struct kvm_vcpu *vcpu)
132 {
133         unsigned long active_mask;
134 
135         active_mask = pending_local_irqs(vcpu);
136         active_mask |= pending_floating_irqs(vcpu);
137         if (!active_mask)
138                 return 0;
139 
140         if (psw_extint_disabled(vcpu))
141                 active_mask &= ~IRQ_PEND_EXT_MASK;
142         if (psw_ioint_disabled(vcpu))
143                 active_mask &= ~IRQ_PEND_IO_MASK;
144         else
145                 active_mask = disable_iscs(vcpu, active_mask);
146         if (!(vcpu->arch.sie_block->gcr[0] & 0x2000ul))
147                 __clear_bit(IRQ_PEND_EXT_EXTERNAL, &active_mask);
148         if (!(vcpu->arch.sie_block->gcr[0] & 0x4000ul))
149                 __clear_bit(IRQ_PEND_EXT_EMERGENCY, &active_mask);
150         if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
151                 __clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &active_mask);
152         if (!(vcpu->arch.sie_block->gcr[0] & 0x400ul))
153                 __clear_bit(IRQ_PEND_EXT_CPU_TIMER, &active_mask);
154         if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
155                 __clear_bit(IRQ_PEND_EXT_SERVICE, &active_mask);
156         if (psw_mchk_disabled(vcpu))
157                 active_mask &= ~IRQ_PEND_MCHK_MASK;
158         if (!(vcpu->arch.sie_block->gcr[14] &
159               vcpu->kvm->arch.float_int.mchk.cr14))
160                 __clear_bit(IRQ_PEND_MCHK_REP, &active_mask);
161 
162         /*
163          * STOP irqs will never be actively delivered. They are triggered via
164          * intercept requests and cleared when the stop intercept is performed.
165          */
166         __clear_bit(IRQ_PEND_SIGP_STOP, &active_mask);
167 
168         return active_mask;
169 }
170 
171 static void __set_cpu_idle(struct kvm_vcpu *vcpu)
172 {
173         atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
174         set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
175 }
176 
177 static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
178 {
179         atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
180         clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
181 }
182 
183 static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
184 {
185         atomic_clear_mask(CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
186                           &vcpu->arch.sie_block->cpuflags);
187         vcpu->arch.sie_block->lctl = 0x0000;
188         vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT);
189 
190         if (guestdbg_enabled(vcpu)) {
191                 vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 |
192                                                LCTL_CR10 | LCTL_CR11);
193                 vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT);
194         }
195 }
196 
197 static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
198 {
199         atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags);
200 }
201 
202 static void set_intercept_indicators_io(struct kvm_vcpu *vcpu)
203 {
204         if (!(pending_floating_irqs(vcpu) & IRQ_PEND_IO_MASK))
205                 return;
206         else if (psw_ioint_disabled(vcpu))
207                 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
208         else
209                 vcpu->arch.sie_block->lctl |= LCTL_CR6;
210 }
211 
212 static void set_intercept_indicators_ext(struct kvm_vcpu *vcpu)
213 {
214         if (!(pending_local_irqs(vcpu) & IRQ_PEND_EXT_MASK))
215                 return;
216         if (psw_extint_disabled(vcpu))
217                 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
218         else
219                 vcpu->arch.sie_block->lctl |= LCTL_CR0;
220 }
221 
222 static void set_intercept_indicators_mchk(struct kvm_vcpu *vcpu)
223 {
224         if (!(pending_local_irqs(vcpu) & IRQ_PEND_MCHK_MASK))
225                 return;
226         if (psw_mchk_disabled(vcpu))
227                 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
228         else
229                 vcpu->arch.sie_block->lctl |= LCTL_CR14;
230 }
231 
232 static void set_intercept_indicators_stop(struct kvm_vcpu *vcpu)
233 {
234         if (kvm_s390_is_stop_irq_pending(vcpu))
235                 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
236 }
237 
238 /* Set interception request for non-deliverable interrupts */
239 static void set_intercept_indicators(struct kvm_vcpu *vcpu)
240 {
241         set_intercept_indicators_io(vcpu);
242         set_intercept_indicators_ext(vcpu);
243         set_intercept_indicators_mchk(vcpu);
244         set_intercept_indicators_stop(vcpu);
245 }
246 
247 static u16 get_ilc(struct kvm_vcpu *vcpu)
248 {
249         switch (vcpu->arch.sie_block->icptcode) {
250         case ICPT_INST:
251         case ICPT_INSTPROGI:
252         case ICPT_OPEREXC:
253         case ICPT_PARTEXEC:
254         case ICPT_IOINST:
255                 /* last instruction only stored for these icptcodes */
256                 return insn_length(vcpu->arch.sie_block->ipa >> 8);
257         case ICPT_PROGI:
258                 return vcpu->arch.sie_block->pgmilc;
259         default:
260                 return 0;
261         }
262 }
263 
264 static int __must_check __deliver_cpu_timer(struct kvm_vcpu *vcpu)
265 {
266         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
267         int rc;
268 
269         trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
270                                          0, 0);
271 
272         rc  = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
273                            (u16 *)__LC_EXT_INT_CODE);
274         rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
275         rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
276                              &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
277         rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
278                             &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
279         clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
280         return rc ? -EFAULT : 0;
281 }
282 
283 static int __must_check __deliver_ckc(struct kvm_vcpu *vcpu)
284 {
285         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
286         int rc;
287 
288         trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
289                                          0, 0);
290 
291         rc  = put_guest_lc(vcpu, EXT_IRQ_CLK_COMP,
292                            (u16 __user *)__LC_EXT_INT_CODE);
293         rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
294         rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
295                              &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
296         rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
297                             &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
298         clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
299         return rc ? -EFAULT : 0;
300 }
301 
302 static int __must_check __deliver_pfault_init(struct kvm_vcpu *vcpu)
303 {
304         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
305         struct kvm_s390_ext_info ext;
306         int rc;
307 
308         spin_lock(&li->lock);
309         ext = li->irq.ext;
310         clear_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
311         li->irq.ext.ext_params2 = 0;
312         spin_unlock(&li->lock);
313 
314         VCPU_EVENT(vcpu, 4, "interrupt: pfault init parm:%x,parm64:%llx",
315                    0, ext.ext_params2);
316         trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
317                                          KVM_S390_INT_PFAULT_INIT,
318                                          0, ext.ext_params2);
319 
320         rc  = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *) __LC_EXT_INT_CODE);
321         rc |= put_guest_lc(vcpu, PFAULT_INIT, (u16 *) __LC_EXT_CPU_ADDR);
322         rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
323                              &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
324         rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
325                             &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
326         rc |= put_guest_lc(vcpu, ext.ext_params2, (u64 *) __LC_EXT_PARAMS2);
327         return rc ? -EFAULT : 0;
328 }
329 
330 static int __must_check __deliver_machine_check(struct kvm_vcpu *vcpu)
331 {
332         struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
333         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
334         struct kvm_s390_mchk_info mchk = {};
335         unsigned long adtl_status_addr;
336         int deliver = 0;
337         int rc = 0;
338 
339         spin_lock(&fi->lock);
340         spin_lock(&li->lock);
341         if (test_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs) ||
342             test_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs)) {
343                 /*
344                  * If there was an exigent machine check pending, then any
345                  * repressible machine checks that might have been pending
346                  * are indicated along with it, so always clear bits for
347                  * repressible and exigent interrupts
348                  */
349                 mchk = li->irq.mchk;
350                 clear_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
351                 clear_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
352                 memset(&li->irq.mchk, 0, sizeof(mchk));
353                 deliver = 1;
354         }
355         /*
356          * We indicate floating repressible conditions along with
357          * other pending conditions. Channel Report Pending and Channel
358          * Subsystem damage are the only two and and are indicated by
359          * bits in mcic and masked in cr14.
360          */
361         if (test_and_clear_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
362                 mchk.mcic |= fi->mchk.mcic;
363                 mchk.cr14 |= fi->mchk.cr14;
364                 memset(&fi->mchk, 0, sizeof(mchk));
365                 deliver = 1;
366         }
367         spin_unlock(&li->lock);
368         spin_unlock(&fi->lock);
369 
370         if (deliver) {
371                 VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx",
372                            mchk.mcic);
373                 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
374                                                  KVM_S390_MCHK,
375                                                  mchk.cr14, mchk.mcic);
376 
377                 rc  = kvm_s390_vcpu_store_status(vcpu,
378                                                  KVM_S390_STORE_STATUS_PREFIXED);
379                 rc |= read_guest_lc(vcpu, __LC_VX_SAVE_AREA_ADDR,
380                                     &adtl_status_addr,
381                                     sizeof(unsigned long));
382                 rc |= kvm_s390_vcpu_store_adtl_status(vcpu,
383                                                       adtl_status_addr);
384                 rc |= put_guest_lc(vcpu, mchk.mcic,
385                                    (u64 __user *) __LC_MCCK_CODE);
386                 rc |= put_guest_lc(vcpu, mchk.failing_storage_address,
387                                    (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR);
388                 rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA,
389                                      &mchk.fixed_logout,
390                                      sizeof(mchk.fixed_logout));
391                 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
392                                      &vcpu->arch.sie_block->gpsw,
393                                      sizeof(psw_t));
394                 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
395                                     &vcpu->arch.sie_block->gpsw,
396                                     sizeof(psw_t));
397         }
398         return rc ? -EFAULT : 0;
399 }
400 
401 static int __must_check __deliver_restart(struct kvm_vcpu *vcpu)
402 {
403         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
404         int rc;
405 
406         VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart");
407         vcpu->stat.deliver_restart_signal++;
408         trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
409 
410         rc  = write_guest_lc(vcpu,
411                              offsetof(struct _lowcore, restart_old_psw),
412                              &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
413         rc |= read_guest_lc(vcpu, offsetof(struct _lowcore, restart_psw),
414                             &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
415         clear_bit(IRQ_PEND_RESTART, &li->pending_irqs);
416         return rc ? -EFAULT : 0;
417 }
418 
419 static int __must_check __deliver_set_prefix(struct kvm_vcpu *vcpu)
420 {
421         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
422         struct kvm_s390_prefix_info prefix;
423 
424         spin_lock(&li->lock);
425         prefix = li->irq.prefix;
426         li->irq.prefix.address = 0;
427         clear_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
428         spin_unlock(&li->lock);
429 
430         VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x", prefix.address);
431         vcpu->stat.deliver_prefix_signal++;
432         trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
433                                          KVM_S390_SIGP_SET_PREFIX,
434                                          prefix.address, 0);
435 
436         kvm_s390_set_prefix(vcpu, prefix.address);
437         return 0;
438 }
439 
440 static int __must_check __deliver_emergency_signal(struct kvm_vcpu *vcpu)
441 {
442         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
443         int rc;
444         int cpu_addr;
445 
446         spin_lock(&li->lock);
447         cpu_addr = find_first_bit(li->sigp_emerg_pending, KVM_MAX_VCPUS);
448         clear_bit(cpu_addr, li->sigp_emerg_pending);
449         if (bitmap_empty(li->sigp_emerg_pending, KVM_MAX_VCPUS))
450                 clear_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
451         spin_unlock(&li->lock);
452 
453         VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg");
454         vcpu->stat.deliver_emergency_signal++;
455         trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
456                                          cpu_addr, 0);
457 
458         rc  = put_guest_lc(vcpu, EXT_IRQ_EMERGENCY_SIG,
459                            (u16 *)__LC_EXT_INT_CODE);
460         rc |= put_guest_lc(vcpu, cpu_addr, (u16 *)__LC_EXT_CPU_ADDR);
461         rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
462                              &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
463         rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
464                             &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
465         return rc ? -EFAULT : 0;
466 }
467 
468 static int __must_check __deliver_external_call(struct kvm_vcpu *vcpu)
469 {
470         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
471         struct kvm_s390_extcall_info extcall;
472         int rc;
473 
474         spin_lock(&li->lock);
475         extcall = li->irq.extcall;
476         li->irq.extcall.code = 0;
477         clear_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
478         spin_unlock(&li->lock);
479 
480         VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call");
481         vcpu->stat.deliver_external_call++;
482         trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
483                                          KVM_S390_INT_EXTERNAL_CALL,
484                                          extcall.code, 0);
485 
486         rc  = put_guest_lc(vcpu, EXT_IRQ_EXTERNAL_CALL,
487                            (u16 *)__LC_EXT_INT_CODE);
488         rc |= put_guest_lc(vcpu, extcall.code, (u16 *)__LC_EXT_CPU_ADDR);
489         rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
490                              &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
491         rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &vcpu->arch.sie_block->gpsw,
492                             sizeof(psw_t));
493         return rc ? -EFAULT : 0;
494 }
495 
496 static int __must_check __deliver_prog(struct kvm_vcpu *vcpu)
497 {
498         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
499         struct kvm_s390_pgm_info pgm_info;
500         int rc = 0, nullifying = false;
501         u16 ilc = get_ilc(vcpu);
502 
503         spin_lock(&li->lock);
504         pgm_info = li->irq.pgm;
505         clear_bit(IRQ_PEND_PROG, &li->pending_irqs);
506         memset(&li->irq.pgm, 0, sizeof(pgm_info));
507         spin_unlock(&li->lock);
508 
509         VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x",
510                    pgm_info.code, ilc);
511         vcpu->stat.deliver_program_int++;
512         trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
513                                          pgm_info.code, 0);
514 
515         switch (pgm_info.code & ~PGM_PER) {
516         case PGM_AFX_TRANSLATION:
517         case PGM_ASX_TRANSLATION:
518         case PGM_EX_TRANSLATION:
519         case PGM_LFX_TRANSLATION:
520         case PGM_LSTE_SEQUENCE:
521         case PGM_LSX_TRANSLATION:
522         case PGM_LX_TRANSLATION:
523         case PGM_PRIMARY_AUTHORITY:
524         case PGM_SECONDARY_AUTHORITY:
525                 nullifying = true;
526                 /* fall through */
527         case PGM_SPACE_SWITCH:
528                 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
529                                   (u64 *)__LC_TRANS_EXC_CODE);
530                 break;
531         case PGM_ALEN_TRANSLATION:
532         case PGM_ALE_SEQUENCE:
533         case PGM_ASTE_INSTANCE:
534         case PGM_ASTE_SEQUENCE:
535         case PGM_ASTE_VALIDITY:
536         case PGM_EXTENDED_AUTHORITY:
537                 rc = put_guest_lc(vcpu, pgm_info.exc_access_id,
538                                   (u8 *)__LC_EXC_ACCESS_ID);
539                 nullifying = true;
540                 break;
541         case PGM_ASCE_TYPE:
542         case PGM_PAGE_TRANSLATION:
543         case PGM_REGION_FIRST_TRANS:
544         case PGM_REGION_SECOND_TRANS:
545         case PGM_REGION_THIRD_TRANS:
546         case PGM_SEGMENT_TRANSLATION:
547                 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
548                                   (u64 *)__LC_TRANS_EXC_CODE);
549                 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
550                                    (u8 *)__LC_EXC_ACCESS_ID);
551                 rc |= put_guest_lc(vcpu, pgm_info.op_access_id,
552                                    (u8 *)__LC_OP_ACCESS_ID);
553                 nullifying = true;
554                 break;
555         case PGM_MONITOR:
556                 rc = put_guest_lc(vcpu, pgm_info.mon_class_nr,
557                                   (u16 *)__LC_MON_CLASS_NR);
558                 rc |= put_guest_lc(vcpu, pgm_info.mon_code,
559                                    (u64 *)__LC_MON_CODE);
560                 break;
561         case PGM_VECTOR_PROCESSING:
562         case PGM_DATA:
563                 rc = put_guest_lc(vcpu, pgm_info.data_exc_code,
564                                   (u32 *)__LC_DATA_EXC_CODE);
565                 break;
566         case PGM_PROTECTION:
567                 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
568                                   (u64 *)__LC_TRANS_EXC_CODE);
569                 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
570                                    (u8 *)__LC_EXC_ACCESS_ID);
571                 break;
572         case PGM_STACK_FULL:
573         case PGM_STACK_EMPTY:
574         case PGM_STACK_SPECIFICATION:
575         case PGM_STACK_TYPE:
576         case PGM_STACK_OPERATION:
577         case PGM_TRACE_TABEL:
578         case PGM_CRYPTO_OPERATION:
579                 nullifying = true;
580                 break;
581         }
582 
583         if (pgm_info.code & PGM_PER) {
584                 rc |= put_guest_lc(vcpu, pgm_info.per_code,
585                                    (u8 *) __LC_PER_CODE);
586                 rc |= put_guest_lc(vcpu, pgm_info.per_atmid,
587                                    (u8 *)__LC_PER_ATMID);
588                 rc |= put_guest_lc(vcpu, pgm_info.per_address,
589                                    (u64 *) __LC_PER_ADDRESS);
590                 rc |= put_guest_lc(vcpu, pgm_info.per_access_id,
591                                    (u8 *) __LC_PER_ACCESS_ID);
592         }
593 
594         if (nullifying && vcpu->arch.sie_block->icptcode == ICPT_INST)
595                 kvm_s390_rewind_psw(vcpu, ilc);
596 
597         rc |= put_guest_lc(vcpu, ilc, (u16 *) __LC_PGM_ILC);
598         rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->gbea,
599                                  (u64 *) __LC_LAST_BREAK);
600         rc |= put_guest_lc(vcpu, pgm_info.code,
601                            (u16 *)__LC_PGM_INT_CODE);
602         rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
603                              &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
604         rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
605                             &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
606         return rc ? -EFAULT : 0;
607 }
608 
609 static int __must_check __deliver_service(struct kvm_vcpu *vcpu)
610 {
611         struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
612         struct kvm_s390_ext_info ext;
613         int rc = 0;
614 
615         spin_lock(&fi->lock);
616         if (!(test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs))) {
617                 spin_unlock(&fi->lock);
618                 return 0;
619         }
620         ext = fi->srv_signal;
621         memset(&fi->srv_signal, 0, sizeof(ext));
622         clear_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
623         spin_unlock(&fi->lock);
624 
625         VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x",
626                    ext.ext_params);
627         vcpu->stat.deliver_service_signal++;
628         trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_SERVICE,
629                                          ext.ext_params, 0);
630 
631         rc  = put_guest_lc(vcpu, EXT_IRQ_SERVICE_SIG, (u16 *)__LC_EXT_INT_CODE);
632         rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
633         rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
634                              &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
635         rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
636                             &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
637         rc |= put_guest_lc(vcpu, ext.ext_params,
638                            (u32 *)__LC_EXT_PARAMS);
639 
640         return rc ? -EFAULT : 0;
641 }
642 
643 static int __must_check __deliver_pfault_done(struct kvm_vcpu *vcpu)
644 {
645         struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
646         struct kvm_s390_interrupt_info *inti;
647         int rc = 0;
648 
649         spin_lock(&fi->lock);
650         inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_PFAULT],
651                                         struct kvm_s390_interrupt_info,
652                                         list);
653         if (inti) {
654                 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
655                                 KVM_S390_INT_PFAULT_DONE, 0,
656                                 inti->ext.ext_params2);
657                 list_del(&inti->list);
658                 fi->counters[FIRQ_CNTR_PFAULT] -= 1;
659         }
660         if (list_empty(&fi->lists[FIRQ_LIST_PFAULT]))
661                 clear_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
662         spin_unlock(&fi->lock);
663 
664         if (inti) {
665                 rc  = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
666                                 (u16 *)__LC_EXT_INT_CODE);
667                 rc |= put_guest_lc(vcpu, PFAULT_DONE,
668                                 (u16 *)__LC_EXT_CPU_ADDR);
669                 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
670                                 &vcpu->arch.sie_block->gpsw,
671                                 sizeof(psw_t));
672                 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
673                                 &vcpu->arch.sie_block->gpsw,
674                                 sizeof(psw_t));
675                 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
676                                 (u64 *)__LC_EXT_PARAMS2);
677                 kfree(inti);
678         }
679         return rc ? -EFAULT : 0;
680 }
681 
682 static int __must_check __deliver_virtio(struct kvm_vcpu *vcpu)
683 {
684         struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
685         struct kvm_s390_interrupt_info *inti;
686         int rc = 0;
687 
688         spin_lock(&fi->lock);
689         inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_VIRTIO],
690                                         struct kvm_s390_interrupt_info,
691                                         list);
692         if (inti) {
693                 VCPU_EVENT(vcpu, 4,
694                            "interrupt: virtio parm:%x,parm64:%llx",
695                            inti->ext.ext_params, inti->ext.ext_params2);
696                 vcpu->stat.deliver_virtio_interrupt++;
697                 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
698                                 inti->type,
699                                 inti->ext.ext_params,
700                                 inti->ext.ext_params2);
701                 list_del(&inti->list);
702                 fi->counters[FIRQ_CNTR_VIRTIO] -= 1;
703         }
704         if (list_empty(&fi->lists[FIRQ_LIST_VIRTIO]))
705                 clear_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
706         spin_unlock(&fi->lock);
707 
708         if (inti) {
709                 rc  = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
710                                 (u16 *)__LC_EXT_INT_CODE);
711                 rc |= put_guest_lc(vcpu, VIRTIO_PARAM,
712                                 (u16 *)__LC_EXT_CPU_ADDR);
713                 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
714                                 &vcpu->arch.sie_block->gpsw,
715                                 sizeof(psw_t));
716                 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
717                                 &vcpu->arch.sie_block->gpsw,
718                                 sizeof(psw_t));
719                 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
720                                 (u32 *)__LC_EXT_PARAMS);
721                 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
722                                 (u64 *)__LC_EXT_PARAMS2);
723                 kfree(inti);
724         }
725         return rc ? -EFAULT : 0;
726 }
727 
728 static int __must_check __deliver_io(struct kvm_vcpu *vcpu,
729                                      unsigned long irq_type)
730 {
731         struct list_head *isc_list;
732         struct kvm_s390_float_interrupt *fi;
733         struct kvm_s390_interrupt_info *inti = NULL;
734         int rc = 0;
735 
736         fi = &vcpu->kvm->arch.float_int;
737 
738         spin_lock(&fi->lock);
739         isc_list = &fi->lists[irq_type - IRQ_PEND_IO_ISC_0];
740         inti = list_first_entry_or_null(isc_list,
741                                         struct kvm_s390_interrupt_info,
742                                         list);
743         if (inti) {
744                 VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type);
745                 vcpu->stat.deliver_io_int++;
746                 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
747                                 inti->type,
748                                 ((__u32)inti->io.subchannel_id << 16) |
749                                 inti->io.subchannel_nr,
750                                 ((__u64)inti->io.io_int_parm << 32) |
751                                 inti->io.io_int_word);
752                 list_del(&inti->list);
753                 fi->counters[FIRQ_CNTR_IO] -= 1;
754         }
755         if (list_empty(isc_list))
756                 clear_bit(irq_type, &fi->pending_irqs);
757         spin_unlock(&fi->lock);
758 
759         if (inti) {
760                 rc  = put_guest_lc(vcpu, inti->io.subchannel_id,
761                                 (u16 *)__LC_SUBCHANNEL_ID);
762                 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
763                                 (u16 *)__LC_SUBCHANNEL_NR);
764                 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
765                                 (u32 *)__LC_IO_INT_PARM);
766                 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
767                                 (u32 *)__LC_IO_INT_WORD);
768                 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
769                                 &vcpu->arch.sie_block->gpsw,
770                                 sizeof(psw_t));
771                 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
772                                 &vcpu->arch.sie_block->gpsw,
773                                 sizeof(psw_t));
774                 kfree(inti);
775         }
776 
777         return rc ? -EFAULT : 0;
778 }
779 
780 typedef int (*deliver_irq_t)(struct kvm_vcpu *vcpu);
781 
782 static const deliver_irq_t deliver_irq_funcs[] = {
783         [IRQ_PEND_MCHK_EX]        = __deliver_machine_check,
784         [IRQ_PEND_MCHK_REP]       = __deliver_machine_check,
785         [IRQ_PEND_PROG]           = __deliver_prog,
786         [IRQ_PEND_EXT_EMERGENCY]  = __deliver_emergency_signal,
787         [IRQ_PEND_EXT_EXTERNAL]   = __deliver_external_call,
788         [IRQ_PEND_EXT_CLOCK_COMP] = __deliver_ckc,
789         [IRQ_PEND_EXT_CPU_TIMER]  = __deliver_cpu_timer,
790         [IRQ_PEND_RESTART]        = __deliver_restart,
791         [IRQ_PEND_SET_PREFIX]     = __deliver_set_prefix,
792         [IRQ_PEND_PFAULT_INIT]    = __deliver_pfault_init,
793         [IRQ_PEND_EXT_SERVICE]    = __deliver_service,
794         [IRQ_PEND_PFAULT_DONE]    = __deliver_pfault_done,
795         [IRQ_PEND_VIRTIO]         = __deliver_virtio,
796 };
797 
798 /* Check whether an external call is pending (deliverable or not) */
799 int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu)
800 {
801         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
802         uint8_t sigp_ctrl = vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl;
803 
804         if (!sclp.has_sigpif)
805                 return test_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
806 
807         return (sigp_ctrl & SIGP_CTRL_C) &&
808                (atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_ECALL_PEND);
809 }
810 
811 int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop)
812 {
813         int rc;
814 
815         rc = !!deliverable_irqs(vcpu);
816 
817         if (!rc && kvm_cpu_has_pending_timer(vcpu))
818                 rc = 1;
819 
820         /* external call pending and deliverable */
821         if (!rc && kvm_s390_ext_call_pending(vcpu) &&
822             !psw_extint_disabled(vcpu) &&
823             (vcpu->arch.sie_block->gcr[0] & 0x2000ul))
824                 rc = 1;
825 
826         if (!rc && !exclude_stop && kvm_s390_is_stop_irq_pending(vcpu))
827                 rc = 1;
828 
829         return rc;
830 }
831 
832 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
833 {
834         return ckc_irq_pending(vcpu) || cpu_timer_irq_pending(vcpu);
835 }
836 
837 int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
838 {
839         u64 now, sltime;
840 
841         vcpu->stat.exit_wait_state++;
842 
843         /* fast path */
844         if (kvm_cpu_has_pending_timer(vcpu) || kvm_arch_vcpu_runnable(vcpu))
845                 return 0;
846 
847         if (psw_interrupts_disabled(vcpu)) {
848                 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
849                 return -EOPNOTSUPP; /* disabled wait */
850         }
851 
852         if (!ckc_interrupts_enabled(vcpu)) {
853                 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
854                 __set_cpu_idle(vcpu);
855                 goto no_timer;
856         }
857 
858         now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
859         sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
860 
861         /* underflow */
862         if (vcpu->arch.sie_block->ckc < now)
863                 return 0;
864 
865         __set_cpu_idle(vcpu);
866         hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
867         VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
868 no_timer:
869         srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
870         kvm_vcpu_block(vcpu);
871         __unset_cpu_idle(vcpu);
872         vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
873 
874         hrtimer_cancel(&vcpu->arch.ckc_timer);
875         return 0;
876 }
877 
878 void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
879 {
880         if (waitqueue_active(&vcpu->wq)) {
881                 /*
882                  * The vcpu gave up the cpu voluntarily, mark it as a good
883                  * yield-candidate.
884                  */
885                 vcpu->preempted = true;
886                 wake_up_interruptible(&vcpu->wq);
887                 vcpu->stat.halt_wakeup++;
888         }
889 }
890 
891 enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
892 {
893         struct kvm_vcpu *vcpu;
894         u64 now, sltime;
895 
896         vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
897         now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
898         sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
899 
900         /*
901          * If the monotonic clock runs faster than the tod clock we might be
902          * woken up too early and have to go back to sleep to avoid deadlocks.
903          */
904         if (vcpu->arch.sie_block->ckc > now &&
905             hrtimer_forward_now(timer, ns_to_ktime(sltime)))
906                 return HRTIMER_RESTART;
907         kvm_s390_vcpu_wakeup(vcpu);
908         return HRTIMER_NORESTART;
909 }
910 
911 void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
912 {
913         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
914 
915         spin_lock(&li->lock);
916         li->pending_irqs = 0;
917         bitmap_zero(li->sigp_emerg_pending, KVM_MAX_VCPUS);
918         memset(&li->irq, 0, sizeof(li->irq));
919         spin_unlock(&li->lock);
920 
921         /* clear pending external calls set by sigp interpretation facility */
922         atomic_clear_mask(CPUSTAT_ECALL_PEND, li->cpuflags);
923         vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl = 0;
924 }
925 
926 int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
927 {
928         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
929         deliver_irq_t func;
930         int rc = 0;
931         unsigned long irq_type;
932         unsigned long irqs;
933 
934         __reset_intercept_indicators(vcpu);
935 
936         /* pending ckc conditions might have been invalidated */
937         clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
938         if (ckc_irq_pending(vcpu))
939                 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
940 
941         /* pending cpu timer conditions might have been invalidated */
942         clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
943         if (cpu_timer_irq_pending(vcpu))
944                 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
945 
946         while ((irqs = deliverable_irqs(vcpu)) && !rc) {
947                 /* bits are in the order of interrupt priority */
948                 irq_type = find_first_bit(&irqs, IRQ_PEND_COUNT);
949                 if (is_ioirq(irq_type)) {
950                         rc = __deliver_io(vcpu, irq_type);
951                 } else {
952                         func = deliver_irq_funcs[irq_type];
953                         if (!func) {
954                                 WARN_ON_ONCE(func == NULL);
955                                 clear_bit(irq_type, &li->pending_irqs);
956                                 continue;
957                         }
958                         rc = func(vcpu);
959                 }
960         }
961 
962         set_intercept_indicators(vcpu);
963 
964         return rc;
965 }
966 
967 static int __inject_prog(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
968 {
969         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
970 
971         li->irq.pgm = irq->u.pgm;
972         set_bit(IRQ_PEND_PROG, &li->pending_irqs);
973         return 0;
974 }
975 
976 int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
977 {
978         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
979         struct kvm_s390_irq irq;
980 
981         VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code);
982         trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT, code,
983                                    0, 1);
984         spin_lock(&li->lock);
985         irq.u.pgm.code = code;
986         __inject_prog(vcpu, &irq);
987         BUG_ON(waitqueue_active(li->wq));
988         spin_unlock(&li->lock);
989         return 0;
990 }
991 
992 int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
993                              struct kvm_s390_pgm_info *pgm_info)
994 {
995         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
996         struct kvm_s390_irq irq;
997         int rc;
998 
999         VCPU_EVENT(vcpu, 3, "inject: prog irq %d (from kernel)",
1000                    pgm_info->code);
1001         trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
1002                                    pgm_info->code, 0, 1);
1003         spin_lock(&li->lock);
1004         irq.u.pgm = *pgm_info;
1005         rc = __inject_prog(vcpu, &irq);
1006         BUG_ON(waitqueue_active(li->wq));
1007         spin_unlock(&li->lock);
1008         return rc;
1009 }
1010 
1011 static int __inject_pfault_init(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1012 {
1013         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1014 
1015         VCPU_EVENT(vcpu, 3, "inject: external irq params:%x, params2:%llx",
1016                    irq->u.ext.ext_params, irq->u.ext.ext_params2);
1017         trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_PFAULT_INIT,
1018                                    irq->u.ext.ext_params,
1019                                    irq->u.ext.ext_params2, 2);
1020 
1021         li->irq.ext = irq->u.ext;
1022         set_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
1023         atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1024         return 0;
1025 }
1026 
1027 static int __inject_extcall_sigpif(struct kvm_vcpu *vcpu, uint16_t src_id)
1028 {
1029         unsigned char new_val, old_val;
1030         uint8_t *sigp_ctrl = &vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl;
1031 
1032         new_val = SIGP_CTRL_C | (src_id & SIGP_CTRL_SCN_MASK);
1033         old_val = *sigp_ctrl & ~SIGP_CTRL_C;
1034         if (cmpxchg(sigp_ctrl, old_val, new_val) != old_val) {
1035                 /* another external call is pending */
1036                 return -EBUSY;
1037         }
1038         atomic_set_mask(CPUSTAT_ECALL_PEND, &vcpu->arch.sie_block->cpuflags);
1039         return 0;
1040 }
1041 
1042 static int __inject_extcall(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1043 {
1044         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1045         struct kvm_s390_extcall_info *extcall = &li->irq.extcall;
1046         uint16_t src_id = irq->u.extcall.code;
1047 
1048         VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u",
1049                    src_id);
1050         trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EXTERNAL_CALL,
1051                                    src_id, 0, 2);
1052 
1053         /* sending vcpu invalid */
1054         if (kvm_get_vcpu_by_id(vcpu->kvm, src_id) == NULL)
1055                 return -EINVAL;
1056 
1057         if (sclp.has_sigpif)
1058                 return __inject_extcall_sigpif(vcpu, src_id);
1059 
1060         if (test_and_set_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs))
1061                 return -EBUSY;
1062         *extcall = irq->u.extcall;
1063         atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1064         return 0;
1065 }
1066 
1067 static int __inject_set_prefix(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1068 {
1069         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1070         struct kvm_s390_prefix_info *prefix = &li->irq.prefix;
1071 
1072         VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
1073                    irq->u.prefix.address);
1074         trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_SET_PREFIX,
1075                                    irq->u.prefix.address, 0, 2);
1076 
1077         if (!is_vcpu_stopped(vcpu))
1078                 return -EBUSY;
1079 
1080         *prefix = irq->u.prefix;
1081         set_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
1082         return 0;
1083 }
1084 
1085 #define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS)
1086 static int __inject_sigp_stop(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1087 {
1088         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1089         struct kvm_s390_stop_info *stop = &li->irq.stop;
1090         int rc = 0;
1091 
1092         trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_STOP, 0, 0, 2);
1093 
1094         if (irq->u.stop.flags & ~KVM_S390_STOP_SUPP_FLAGS)
1095                 return -EINVAL;
1096 
1097         if (is_vcpu_stopped(vcpu)) {
1098                 if (irq->u.stop.flags & KVM_S390_STOP_FLAG_STORE_STATUS)
1099                         rc = kvm_s390_store_status_unloaded(vcpu,
1100                                                 KVM_S390_STORE_STATUS_NOADDR);
1101                 return rc;
1102         }
1103 
1104         if (test_and_set_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs))
1105                 return -EBUSY;
1106         stop->flags = irq->u.stop.flags;
1107         __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
1108         return 0;
1109 }
1110 
1111 static int __inject_sigp_restart(struct kvm_vcpu *vcpu,
1112                                  struct kvm_s390_irq *irq)
1113 {
1114         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1115 
1116         VCPU_EVENT(vcpu, 3, "inject: restart type %llx", irq->type);
1117         trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0, 2);
1118 
1119         set_bit(IRQ_PEND_RESTART, &li->pending_irqs);
1120         return 0;
1121 }
1122 
1123 static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
1124                                    struct kvm_s390_irq *irq)
1125 {
1126         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1127 
1128         VCPU_EVENT(vcpu, 3, "inject: emergency %u\n",
1129                    irq->u.emerg.code);
1130         trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
1131                                    irq->u.emerg.code, 0, 2);
1132 
1133         /* sending vcpu invalid */
1134         if (kvm_get_vcpu_by_id(vcpu->kvm, irq->u.emerg.code) == NULL)
1135                 return -EINVAL;
1136 
1137         set_bit(irq->u.emerg.code, li->sigp_emerg_pending);
1138         set_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
1139         atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1140         return 0;
1141 }
1142 
1143 static int __inject_mchk(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1144 {
1145         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1146         struct kvm_s390_mchk_info *mchk = &li->irq.mchk;
1147 
1148         VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx",
1149                    irq->u.mchk.mcic);
1150         trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_MCHK, 0,
1151                                    irq->u.mchk.mcic, 2);
1152 
1153         /*
1154          * Because repressible machine checks can be indicated along with
1155          * exigent machine checks (PoP, Chapter 11, Interruption action)
1156          * we need to combine cr14, mcic and external damage code.
1157          * Failing storage address and the logout area should not be or'ed
1158          * together, we just indicate the last occurrence of the corresponding
1159          * machine check
1160          */
1161         mchk->cr14 |= irq->u.mchk.cr14;
1162         mchk->mcic |= irq->u.mchk.mcic;
1163         mchk->ext_damage_code |= irq->u.mchk.ext_damage_code;
1164         mchk->failing_storage_address = irq->u.mchk.failing_storage_address;
1165         memcpy(&mchk->fixed_logout, &irq->u.mchk.fixed_logout,
1166                sizeof(mchk->fixed_logout));
1167         if (mchk->mcic & MCHK_EX_MASK)
1168                 set_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
1169         else if (mchk->mcic & MCHK_REP_MASK)
1170                 set_bit(IRQ_PEND_MCHK_REP,  &li->pending_irqs);
1171         return 0;
1172 }
1173 
1174 static int __inject_ckc(struct kvm_vcpu *vcpu)
1175 {
1176         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1177 
1178         VCPU_EVENT(vcpu, 3, "inject: type %x", KVM_S390_INT_CLOCK_COMP);
1179         trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
1180                                    0, 0, 2);
1181 
1182         set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
1183         atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1184         return 0;
1185 }
1186 
1187 static int __inject_cpu_timer(struct kvm_vcpu *vcpu)
1188 {
1189         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1190 
1191         VCPU_EVENT(vcpu, 3, "inject: type %x", KVM_S390_INT_CPU_TIMER);
1192         trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
1193                                    0, 0, 2);
1194 
1195         set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1196         atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1197         return 0;
1198 }
1199 
1200 static struct kvm_s390_interrupt_info *get_io_int(struct kvm *kvm,
1201                                                   int isc, u32 schid)
1202 {
1203         struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1204         struct list_head *isc_list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1205         struct kvm_s390_interrupt_info *iter;
1206         u16 id = (schid & 0xffff0000U) >> 16;
1207         u16 nr = schid & 0x0000ffffU;
1208 
1209         spin_lock(&fi->lock);
1210         list_for_each_entry(iter, isc_list, list) {
1211                 if (schid && (id != iter->io.subchannel_id ||
1212                               nr != iter->io.subchannel_nr))
1213                         continue;
1214                 /* found an appropriate entry */
1215                 list_del_init(&iter->list);
1216                 fi->counters[FIRQ_CNTR_IO] -= 1;
1217                 if (list_empty(isc_list))
1218                         clear_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs);
1219                 spin_unlock(&fi->lock);
1220                 return iter;
1221         }
1222         spin_unlock(&fi->lock);
1223         return NULL;
1224 }
1225 
1226 /*
1227  * Dequeue and return an I/O interrupt matching any of the interruption
1228  * subclasses as designated by the isc mask in cr6 and the schid (if != 0).
1229  */
1230 struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
1231                                                     u64 isc_mask, u32 schid)
1232 {
1233         struct kvm_s390_interrupt_info *inti = NULL;
1234         int isc;
1235 
1236         for (isc = 0; isc <= MAX_ISC && !inti; isc++) {
1237                 if (isc_mask & isc_to_isc_bits(isc))
1238                         inti = get_io_int(kvm, isc, schid);
1239         }
1240         return inti;
1241 }
1242 
1243 #define SCCB_MASK 0xFFFFFFF8
1244 #define SCCB_EVENT_PENDING 0x3
1245 
1246 static int __inject_service(struct kvm *kvm,
1247                              struct kvm_s390_interrupt_info *inti)
1248 {
1249         struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1250 
1251         spin_lock(&fi->lock);
1252         fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_EVENT_PENDING;
1253         /*
1254          * Early versions of the QEMU s390 bios will inject several
1255          * service interrupts after another without handling a
1256          * condition code indicating busy.
1257          * We will silently ignore those superfluous sccb values.
1258          * A future version of QEMU will take care of serialization
1259          * of servc requests
1260          */
1261         if (fi->srv_signal.ext_params & SCCB_MASK)
1262                 goto out;
1263         fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_MASK;
1264         set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
1265 out:
1266         spin_unlock(&fi->lock);
1267         kfree(inti);
1268         return 0;
1269 }
1270 
1271 static int __inject_virtio(struct kvm *kvm,
1272                             struct kvm_s390_interrupt_info *inti)
1273 {
1274         struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1275 
1276         spin_lock(&fi->lock);
1277         if (fi->counters[FIRQ_CNTR_VIRTIO] >= KVM_S390_MAX_VIRTIO_IRQS) {
1278                 spin_unlock(&fi->lock);
1279                 return -EBUSY;
1280         }
1281         fi->counters[FIRQ_CNTR_VIRTIO] += 1;
1282         list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_VIRTIO]);
1283         set_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
1284         spin_unlock(&fi->lock);
1285         return 0;
1286 }
1287 
1288 static int __inject_pfault_done(struct kvm *kvm,
1289                                  struct kvm_s390_interrupt_info *inti)
1290 {
1291         struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1292 
1293         spin_lock(&fi->lock);
1294         if (fi->counters[FIRQ_CNTR_PFAULT] >=
1295                 (ASYNC_PF_PER_VCPU * KVM_MAX_VCPUS)) {
1296                 spin_unlock(&fi->lock);
1297                 return -EBUSY;
1298         }
1299         fi->counters[FIRQ_CNTR_PFAULT] += 1;
1300         list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_PFAULT]);
1301         set_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
1302         spin_unlock(&fi->lock);
1303         return 0;
1304 }
1305 
1306 #define CR_PENDING_SUBCLASS 28
1307 static int __inject_float_mchk(struct kvm *kvm,
1308                                 struct kvm_s390_interrupt_info *inti)
1309 {
1310         struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1311 
1312         spin_lock(&fi->lock);
1313         fi->mchk.cr14 |= inti->mchk.cr14 & (1UL << CR_PENDING_SUBCLASS);
1314         fi->mchk.mcic |= inti->mchk.mcic;
1315         set_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs);
1316         spin_unlock(&fi->lock);
1317         kfree(inti);
1318         return 0;
1319 }
1320 
1321 static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
1322 {
1323         struct kvm_s390_float_interrupt *fi;
1324         struct list_head *list;
1325         int isc;
1326 
1327         fi = &kvm->arch.float_int;
1328         spin_lock(&fi->lock);
1329         if (fi->counters[FIRQ_CNTR_IO] >= KVM_S390_MAX_FLOAT_IRQS) {
1330                 spin_unlock(&fi->lock);
1331                 return -EBUSY;
1332         }
1333         fi->counters[FIRQ_CNTR_IO] += 1;
1334 
1335         isc = int_word_to_isc(inti->io.io_int_word);
1336         list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1337         list_add_tail(&inti->list, list);
1338         set_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs);
1339         spin_unlock(&fi->lock);
1340         return 0;
1341 }
1342 
1343 /*
1344  * Find a destination VCPU for a floating irq and kick it.
1345  */
1346 static void __floating_irq_kick(struct kvm *kvm, u64 type)
1347 {
1348         struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1349         struct kvm_s390_local_interrupt *li;
1350         struct kvm_vcpu *dst_vcpu;
1351         int sigcpu, online_vcpus, nr_tries = 0;
1352 
1353         online_vcpus = atomic_read(&kvm->online_vcpus);
1354         if (!online_vcpus)
1355                 return;
1356 
1357         /* find idle VCPUs first, then round robin */
1358         sigcpu = find_first_bit(fi->idle_mask, online_vcpus);
1359         if (sigcpu == online_vcpus) {
1360                 do {
1361                         sigcpu = fi->next_rr_cpu;
1362                         fi->next_rr_cpu = (fi->next_rr_cpu + 1) % online_vcpus;
1363                         /* avoid endless loops if all vcpus are stopped */
1364                         if (nr_tries++ >= online_vcpus)
1365                                 return;
1366                 } while (is_vcpu_stopped(kvm_get_vcpu(kvm, sigcpu)));
1367         }
1368         dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
1369 
1370         /* make the VCPU drop out of the SIE, or wake it up if sleeping */
1371         li = &dst_vcpu->arch.local_int;
1372         spin_lock(&li->lock);
1373         switch (type) {
1374         case KVM_S390_MCHK:
1375                 atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags);
1376                 break;
1377         case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1378                 atomic_set_mask(CPUSTAT_IO_INT, li->cpuflags);
1379                 break;
1380         default:
1381                 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1382                 break;
1383         }
1384         spin_unlock(&li->lock);
1385         kvm_s390_vcpu_wakeup(dst_vcpu);
1386 }
1387 
1388 static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
1389 {
1390         struct kvm_s390_float_interrupt *fi;
1391         u64 type = READ_ONCE(inti->type);
1392         int rc;
1393 
1394         fi = &kvm->arch.float_int;
1395 
1396         switch (type) {
1397         case KVM_S390_MCHK:
1398                 rc = __inject_float_mchk(kvm, inti);
1399                 break;
1400         case KVM_S390_INT_VIRTIO:
1401                 rc = __inject_virtio(kvm, inti);
1402                 break;
1403         case KVM_S390_INT_SERVICE:
1404                 rc = __inject_service(kvm, inti);
1405                 break;
1406         case KVM_S390_INT_PFAULT_DONE:
1407                 rc = __inject_pfault_done(kvm, inti);
1408                 break;
1409         case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1410                 rc = __inject_io(kvm, inti);
1411                 break;
1412         default:
1413                 rc = -EINVAL;
1414         }
1415         if (rc)
1416                 return rc;
1417 
1418         __floating_irq_kick(kvm, type);
1419         return 0;
1420 }
1421 
1422 int kvm_s390_inject_vm(struct kvm *kvm,
1423                        struct kvm_s390_interrupt *s390int)
1424 {
1425         struct kvm_s390_interrupt_info *inti;
1426         int rc;
1427 
1428         inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1429         if (!inti)
1430                 return -ENOMEM;
1431 
1432         inti->type = s390int->type;
1433         switch (inti->type) {
1434         case KVM_S390_INT_VIRTIO:
1435                 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
1436                          s390int->parm, s390int->parm64);
1437                 inti->ext.ext_params = s390int->parm;
1438                 inti->ext.ext_params2 = s390int->parm64;
1439                 break;
1440         case KVM_S390_INT_SERVICE:
1441                 VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
1442                 inti->ext.ext_params = s390int->parm;
1443                 break;
1444         case KVM_S390_INT_PFAULT_DONE:
1445                 inti->ext.ext_params2 = s390int->parm64;
1446                 break;
1447         case KVM_S390_MCHK:
1448                 VM_EVENT(kvm, 5, "inject: machine check parm64:%llx",
1449                          s390int->parm64);
1450                 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
1451                 inti->mchk.mcic = s390int->parm64;
1452                 break;
1453         case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1454                 if (inti->type & IOINT_AI_MASK)
1455                         VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
1456                 else
1457                         VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
1458                                  s390int->type & IOINT_CSSID_MASK,
1459                                  s390int->type & IOINT_SSID_MASK,
1460                                  s390int->type & IOINT_SCHID_MASK);
1461                 inti->io.subchannel_id = s390int->parm >> 16;
1462                 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
1463                 inti->io.io_int_parm = s390int->parm64 >> 32;
1464                 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
1465                 break;
1466         default:
1467                 kfree(inti);
1468                 return -EINVAL;
1469         }
1470         trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
1471                                  2);
1472 
1473         rc = __inject_vm(kvm, inti);
1474         if (rc)
1475                 kfree(inti);
1476         return rc;
1477 }
1478 
1479 int kvm_s390_reinject_io_int(struct kvm *kvm,
1480                               struct kvm_s390_interrupt_info *inti)
1481 {
1482         return __inject_vm(kvm, inti);
1483 }
1484 
1485 int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
1486                        struct kvm_s390_irq *irq)
1487 {
1488         irq->type = s390int->type;
1489         switch (irq->type) {
1490         case KVM_S390_PROGRAM_INT:
1491                 if (s390int->parm & 0xffff0000)
1492                         return -EINVAL;
1493                 irq->u.pgm.code = s390int->parm;
1494                 break;
1495         case KVM_S390_SIGP_SET_PREFIX:
1496                 irq->u.prefix.address = s390int->parm;
1497                 break;
1498         case KVM_S390_SIGP_STOP:
1499                 irq->u.stop.flags = s390int->parm;
1500                 break;
1501         case KVM_S390_INT_EXTERNAL_CALL:
1502                 if (s390int->parm & 0xffff0000)
1503                         return -EINVAL;
1504                 irq->u.extcall.code = s390int->parm;
1505                 break;
1506         case KVM_S390_INT_EMERGENCY:
1507                 if (s390int->parm & 0xffff0000)
1508                         return -EINVAL;
1509                 irq->u.emerg.code = s390int->parm;
1510                 break;
1511         case KVM_S390_MCHK:
1512                 irq->u.mchk.mcic = s390int->parm64;
1513                 break;
1514         }
1515         return 0;
1516 }
1517 
1518 int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu)
1519 {
1520         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1521 
1522         return test_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1523 }
1524 
1525 void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu)
1526 {
1527         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1528 
1529         spin_lock(&li->lock);
1530         li->irq.stop.flags = 0;
1531         clear_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1532         spin_unlock(&li->lock);
1533 }
1534 
1535 static int do_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1536 {
1537         int rc;
1538 
1539         switch (irq->type) {
1540         case KVM_S390_PROGRAM_INT:
1541                 VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
1542                            irq->u.pgm.code);
1543                 rc = __inject_prog(vcpu, irq);
1544                 break;
1545         case KVM_S390_SIGP_SET_PREFIX:
1546                 rc = __inject_set_prefix(vcpu, irq);
1547                 break;
1548         case KVM_S390_SIGP_STOP:
1549                 rc = __inject_sigp_stop(vcpu, irq);
1550                 break;
1551         case KVM_S390_RESTART:
1552                 rc = __inject_sigp_restart(vcpu, irq);
1553                 break;
1554         case KVM_S390_INT_CLOCK_COMP:
1555                 rc = __inject_ckc(vcpu);
1556                 break;
1557         case KVM_S390_INT_CPU_TIMER:
1558                 rc = __inject_cpu_timer(vcpu);
1559                 break;
1560         case KVM_S390_INT_EXTERNAL_CALL:
1561                 rc = __inject_extcall(vcpu, irq);
1562                 break;
1563         case KVM_S390_INT_EMERGENCY:
1564                 rc = __inject_sigp_emergency(vcpu, irq);
1565                 break;
1566         case KVM_S390_MCHK:
1567                 rc = __inject_mchk(vcpu, irq);
1568                 break;
1569         case KVM_S390_INT_PFAULT_INIT:
1570                 rc = __inject_pfault_init(vcpu, irq);
1571                 break;
1572         case KVM_S390_INT_VIRTIO:
1573         case KVM_S390_INT_SERVICE:
1574         case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1575         default:
1576                 rc = -EINVAL;
1577         }
1578 
1579         return rc;
1580 }
1581 
1582 int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1583 {
1584         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1585         int rc;
1586 
1587         spin_lock(&li->lock);
1588         rc = do_inject_vcpu(vcpu, irq);
1589         spin_unlock(&li->lock);
1590         if (!rc)
1591                 kvm_s390_vcpu_wakeup(vcpu);
1592         return rc;
1593 }
1594 
1595 static inline void clear_irq_list(struct list_head *_list)
1596 {
1597         struct kvm_s390_interrupt_info *inti, *n;
1598 
1599         list_for_each_entry_safe(inti, n, _list, list) {
1600                 list_del(&inti->list);
1601                 kfree(inti);
1602         }
1603 }
1604 
1605 static void inti_to_irq(struct kvm_s390_interrupt_info *inti,
1606                        struct kvm_s390_irq *irq)
1607 {
1608         irq->type = inti->type;
1609         switch (inti->type) {
1610         case KVM_S390_INT_PFAULT_INIT:
1611         case KVM_S390_INT_PFAULT_DONE:
1612         case KVM_S390_INT_VIRTIO:
1613                 irq->u.ext = inti->ext;
1614                 break;
1615         case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1616                 irq->u.io = inti->io;
1617                 break;
1618         }
1619 }
1620 
1621 void kvm_s390_clear_float_irqs(struct kvm *kvm)
1622 {
1623         struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1624         int i;
1625 
1626         spin_lock(&fi->lock);
1627         fi->pending_irqs = 0;
1628         memset(&fi->srv_signal, 0, sizeof(fi->srv_signal));
1629         memset(&fi->mchk, 0, sizeof(fi->mchk));
1630         for (i = 0; i < FIRQ_LIST_COUNT; i++)
1631                 clear_irq_list(&fi->lists[i]);
1632         for (i = 0; i < FIRQ_MAX_COUNT; i++)
1633                 fi->counters[i] = 0;
1634         spin_unlock(&fi->lock);
1635 };
1636 
1637 static int get_all_floating_irqs(struct kvm *kvm, u8 __user *usrbuf, u64 len)
1638 {
1639         struct kvm_s390_interrupt_info *inti;
1640         struct kvm_s390_float_interrupt *fi;
1641         struct kvm_s390_irq *buf;
1642         struct kvm_s390_irq *irq;
1643         int max_irqs;
1644         int ret = 0;
1645         int n = 0;
1646         int i;
1647 
1648         if (len > KVM_S390_FLIC_MAX_BUFFER || len == 0)
1649                 return -EINVAL;
1650 
1651         /*
1652          * We are already using -ENOMEM to signal
1653          * userspace it may retry with a bigger buffer,
1654          * so we need to use something else for this case
1655          */
1656         buf = vzalloc(len);
1657         if (!buf)
1658                 return -ENOBUFS;
1659 
1660         max_irqs = len / sizeof(struct kvm_s390_irq);
1661 
1662         fi = &kvm->arch.float_int;
1663         spin_lock(&fi->lock);
1664         for (i = 0; i < FIRQ_LIST_COUNT; i++) {
1665                 list_for_each_entry(inti, &fi->lists[i], list) {
1666                         if (n == max_irqs) {
1667                                 /* signal userspace to try again */
1668                                 ret = -ENOMEM;
1669                                 goto out;
1670                         }
1671                         inti_to_irq(inti, &buf[n]);
1672                         n++;
1673                 }
1674         }
1675         if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs)) {
1676                 if (n == max_irqs) {
1677                         /* signal userspace to try again */
1678                         ret = -ENOMEM;
1679                         goto out;
1680                 }
1681                 irq = (struct kvm_s390_irq *) &buf[n];
1682                 irq->type = KVM_S390_INT_SERVICE;
1683                 irq->u.ext = fi->srv_signal;
1684                 n++;
1685         }
1686         if (test_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
1687                 if (n == max_irqs) {
1688                                 /* signal userspace to try again */
1689                                 ret = -ENOMEM;
1690                                 goto out;
1691                 }
1692                 irq = (struct kvm_s390_irq *) &buf[n];
1693                 irq->type = KVM_S390_MCHK;
1694                 irq->u.mchk = fi->mchk;
1695                 n++;
1696 }
1697 
1698 out:
1699         spin_unlock(&fi->lock);
1700         if (!ret && n > 0) {
1701                 if (copy_to_user(usrbuf, buf, sizeof(struct kvm_s390_irq) * n))
1702                         ret = -EFAULT;
1703         }
1704         vfree(buf);
1705 
1706         return ret < 0 ? ret : n;
1707 }
1708 
1709 static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1710 {
1711         int r;
1712 
1713         switch (attr->group) {
1714         case KVM_DEV_FLIC_GET_ALL_IRQS:
1715                 r = get_all_floating_irqs(dev->kvm, (u8 __user *) attr->addr,
1716                                           attr->attr);
1717                 break;
1718         default:
1719                 r = -EINVAL;
1720         }
1721 
1722         return r;
1723 }
1724 
1725 static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1726                                      u64 addr)
1727 {
1728         struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1729         void *target = NULL;
1730         void __user *source;
1731         u64 size;
1732 
1733         if (get_user(inti->type, (u64 __user *)addr))
1734                 return -EFAULT;
1735 
1736         switch (inti->type) {
1737         case KVM_S390_INT_PFAULT_INIT:
1738         case KVM_S390_INT_PFAULT_DONE:
1739         case KVM_S390_INT_VIRTIO:
1740         case KVM_S390_INT_SERVICE:
1741                 target = (void *) &inti->ext;
1742                 source = &uptr->u.ext;
1743                 size = sizeof(inti->ext);
1744                 break;
1745         case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1746                 target = (void *) &inti->io;
1747                 source = &uptr->u.io;
1748                 size = sizeof(inti->io);
1749                 break;
1750         case KVM_S390_MCHK:
1751                 target = (void *) &inti->mchk;
1752                 source = &uptr->u.mchk;
1753                 size = sizeof(inti->mchk);
1754                 break;
1755         default:
1756                 return -EINVAL;
1757         }
1758 
1759         if (copy_from_user(target, source, size))
1760                 return -EFAULT;
1761 
1762         return 0;
1763 }
1764 
1765 static int enqueue_floating_irq(struct kvm_device *dev,
1766                                 struct kvm_device_attr *attr)
1767 {
1768         struct kvm_s390_interrupt_info *inti = NULL;
1769         int r = 0;
1770         int len = attr->attr;
1771 
1772         if (len % sizeof(struct kvm_s390_irq) != 0)
1773                 return -EINVAL;
1774         else if (len > KVM_S390_FLIC_MAX_BUFFER)
1775                 return -EINVAL;
1776 
1777         while (len >= sizeof(struct kvm_s390_irq)) {
1778                 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1779                 if (!inti)
1780                         return -ENOMEM;
1781 
1782                 r = copy_irq_from_user(inti, attr->addr);
1783                 if (r) {
1784                         kfree(inti);
1785                         return r;
1786                 }
1787                 r = __inject_vm(dev->kvm, inti);
1788                 if (r) {
1789                         kfree(inti);
1790                         return r;
1791                 }
1792                 len -= sizeof(struct kvm_s390_irq);
1793                 attr->addr += sizeof(struct kvm_s390_irq);
1794         }
1795 
1796         return r;
1797 }
1798 
1799 static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
1800 {
1801         if (id >= MAX_S390_IO_ADAPTERS)
1802                 return NULL;
1803         return kvm->arch.adapters[id];
1804 }
1805 
1806 static int register_io_adapter(struct kvm_device *dev,
1807                                struct kvm_device_attr *attr)
1808 {
1809         struct s390_io_adapter *adapter;
1810         struct kvm_s390_io_adapter adapter_info;
1811 
1812         if (copy_from_user(&adapter_info,
1813                            (void __user *)attr->addr, sizeof(adapter_info)))
1814                 return -EFAULT;
1815 
1816         if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
1817             (dev->kvm->arch.adapters[adapter_info.id] != NULL))
1818                 return -EINVAL;
1819 
1820         adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
1821         if (!adapter)
1822                 return -ENOMEM;
1823 
1824         INIT_LIST_HEAD(&adapter->maps);
1825         init_rwsem(&adapter->maps_lock);
1826         atomic_set(&adapter->nr_maps, 0);
1827         adapter->id = adapter_info.id;
1828         adapter->isc = adapter_info.isc;
1829         adapter->maskable = adapter_info.maskable;
1830         adapter->masked = false;
1831         adapter->swap = adapter_info.swap;
1832         dev->kvm->arch.adapters[adapter->id] = adapter;
1833 
1834         return 0;
1835 }
1836 
1837 int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
1838 {
1839         int ret;
1840         struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1841 
1842         if (!adapter || !adapter->maskable)
1843                 return -EINVAL;
1844         ret = adapter->masked;
1845         adapter->masked = masked;
1846         return ret;
1847 }
1848 
1849 static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
1850 {
1851         struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1852         struct s390_map_info *map;
1853         int ret;
1854 
1855         if (!adapter || !addr)
1856                 return -EINVAL;
1857 
1858         map = kzalloc(sizeof(*map), GFP_KERNEL);
1859         if (!map) {
1860                 ret = -ENOMEM;
1861                 goto out;
1862         }
1863         INIT_LIST_HEAD(&map->list);
1864         map->guest_addr = addr;
1865         map->addr = gmap_translate(kvm->arch.gmap, addr);
1866         if (map->addr == -EFAULT) {
1867                 ret = -EFAULT;
1868                 goto out;
1869         }
1870         ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
1871         if (ret < 0)
1872                 goto out;
1873         BUG_ON(ret != 1);
1874         down_write(&adapter->maps_lock);
1875         if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
1876                 list_add_tail(&map->list, &adapter->maps);
1877                 ret = 0;
1878         } else {
1879                 put_page(map->page);
1880                 ret = -EINVAL;
1881         }
1882         up_write(&adapter->maps_lock);
1883 out:
1884         if (ret)
1885                 kfree(map);
1886         return ret;
1887 }
1888 
1889 static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
1890 {
1891         struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1892         struct s390_map_info *map, *tmp;
1893         int found = 0;
1894 
1895         if (!adapter || !addr)
1896                 return -EINVAL;
1897 
1898         down_write(&adapter->maps_lock);
1899         list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
1900                 if (map->guest_addr == addr) {
1901                         found = 1;
1902                         atomic_dec(&adapter->nr_maps);
1903                         list_del(&map->list);
1904                         put_page(map->page);
1905                         kfree(map);
1906                         break;
1907                 }
1908         }
1909         up_write(&adapter->maps_lock);
1910 
1911         return found ? 0 : -EINVAL;
1912 }
1913 
1914 void kvm_s390_destroy_adapters(struct kvm *kvm)
1915 {
1916         int i;
1917         struct s390_map_info *map, *tmp;
1918 
1919         for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
1920                 if (!kvm->arch.adapters[i])
1921                         continue;
1922                 list_for_each_entry_safe(map, tmp,
1923                                          &kvm->arch.adapters[i]->maps, list) {
1924                         list_del(&map->list);
1925                         put_page(map->page);
1926                         kfree(map);
1927                 }
1928                 kfree(kvm->arch.adapters[i]);
1929         }
1930 }
1931 
1932 static int modify_io_adapter(struct kvm_device *dev,
1933                              struct kvm_device_attr *attr)
1934 {
1935         struct kvm_s390_io_adapter_req req;
1936         struct s390_io_adapter *adapter;
1937         int ret;
1938 
1939         if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
1940                 return -EFAULT;
1941 
1942         adapter = get_io_adapter(dev->kvm, req.id);
1943         if (!adapter)
1944                 return -EINVAL;
1945         switch (req.type) {
1946         case KVM_S390_IO_ADAPTER_MASK:
1947                 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
1948                 if (ret > 0)
1949                         ret = 0;
1950                 break;
1951         case KVM_S390_IO_ADAPTER_MAP:
1952                 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
1953                 break;
1954         case KVM_S390_IO_ADAPTER_UNMAP:
1955                 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
1956                 break;
1957         default:
1958                 ret = -EINVAL;
1959         }
1960 
1961         return ret;
1962 }
1963 
1964 static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1965 {
1966         int r = 0;
1967         unsigned int i;
1968         struct kvm_vcpu *vcpu;
1969 
1970         switch (attr->group) {
1971         case KVM_DEV_FLIC_ENQUEUE:
1972                 r = enqueue_floating_irq(dev, attr);
1973                 break;
1974         case KVM_DEV_FLIC_CLEAR_IRQS:
1975                 kvm_s390_clear_float_irqs(dev->kvm);
1976                 break;
1977         case KVM_DEV_FLIC_APF_ENABLE:
1978                 dev->kvm->arch.gmap->pfault_enabled = 1;
1979                 break;
1980         case KVM_DEV_FLIC_APF_DISABLE_WAIT:
1981                 dev->kvm->arch.gmap->pfault_enabled = 0;
1982                 /*
1983                  * Make sure no async faults are in transition when
1984                  * clearing the queues. So we don't need to worry
1985                  * about late coming workers.
1986                  */
1987                 synchronize_srcu(&dev->kvm->srcu);
1988                 kvm_for_each_vcpu(i, vcpu, dev->kvm)
1989                         kvm_clear_async_pf_completion_queue(vcpu);
1990                 break;
1991         case KVM_DEV_FLIC_ADAPTER_REGISTER:
1992                 r = register_io_adapter(dev, attr);
1993                 break;
1994         case KVM_DEV_FLIC_ADAPTER_MODIFY:
1995                 r = modify_io_adapter(dev, attr);
1996                 break;
1997         default:
1998                 r = -EINVAL;
1999         }
2000 
2001         return r;
2002 }
2003 
2004 static int flic_create(struct kvm_device *dev, u32 type)
2005 {
2006         if (!dev)
2007                 return -EINVAL;
2008         if (dev->kvm->arch.flic)
2009                 return -EINVAL;
2010         dev->kvm->arch.flic = dev;
2011         return 0;
2012 }
2013 
2014 static void flic_destroy(struct kvm_device *dev)
2015 {
2016         dev->kvm->arch.flic = NULL;
2017         kfree(dev);
2018 }
2019 
2020 /* s390 floating irq controller (flic) */
2021 struct kvm_device_ops kvm_flic_ops = {
2022         .name = "kvm-flic",
2023         .get_attr = flic_get_attr,
2024         .set_attr = flic_set_attr,
2025         .create = flic_create,
2026         .destroy = flic_destroy,
2027 };
2028 
2029 static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
2030 {
2031         unsigned long bit;
2032 
2033         bit = bit_nr + (addr % PAGE_SIZE) * 8;
2034 
2035         return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
2036 }
2037 
2038 static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
2039                                           u64 addr)
2040 {
2041         struct s390_map_info *map;
2042 
2043         if (!adapter)
2044                 return NULL;
2045 
2046         list_for_each_entry(map, &adapter->maps, list) {
2047                 if (map->guest_addr == addr)
2048                         return map;
2049         }
2050         return NULL;
2051 }
2052 
2053 static int adapter_indicators_set(struct kvm *kvm,
2054                                   struct s390_io_adapter *adapter,
2055                                   struct kvm_s390_adapter_int *adapter_int)
2056 {
2057         unsigned long bit;
2058         int summary_set, idx;
2059         struct s390_map_info *info;
2060         void *map;
2061 
2062         info = get_map_info(adapter, adapter_int->ind_addr);
2063         if (!info)
2064                 return -1;
2065         map = page_address(info->page);
2066         bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
2067         set_bit(bit, map);
2068         idx = srcu_read_lock(&kvm->srcu);
2069         mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2070         set_page_dirty_lock(info->page);
2071         info = get_map_info(adapter, adapter_int->summary_addr);
2072         if (!info) {
2073                 srcu_read_unlock(&kvm->srcu, idx);
2074                 return -1;
2075         }
2076         map = page_address(info->page);
2077         bit = get_ind_bit(info->addr, adapter_int->summary_offset,
2078                           adapter->swap);
2079         summary_set = test_and_set_bit(bit, map);
2080         mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2081         set_page_dirty_lock(info->page);
2082         srcu_read_unlock(&kvm->srcu, idx);
2083         return summary_set ? 0 : 1;
2084 }
2085 
2086 /*
2087  * < 0 - not injected due to error
2088  * = 0 - coalesced, summary indicator already active
2089  * > 0 - injected interrupt
2090  */
2091 static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
2092                            struct kvm *kvm, int irq_source_id, int level,
2093                            bool line_status)
2094 {
2095         int ret;
2096         struct s390_io_adapter *adapter;
2097 
2098         /* We're only interested in the 0->1 transition. */
2099         if (!level)
2100                 return 0;
2101         adapter = get_io_adapter(kvm, e->adapter.adapter_id);
2102         if (!adapter)
2103                 return -1;
2104         down_read(&adapter->maps_lock);
2105         ret = adapter_indicators_set(kvm, adapter, &e->adapter);
2106         up_read(&adapter->maps_lock);
2107         if ((ret > 0) && !adapter->masked) {
2108                 struct kvm_s390_interrupt s390int = {
2109                         .type = KVM_S390_INT_IO(1, 0, 0, 0),
2110                         .parm = 0,
2111                         .parm64 = (adapter->isc << 27) | 0x80000000,
2112                 };
2113                 ret = kvm_s390_inject_vm(kvm, &s390int);
2114                 if (ret == 0)
2115                         ret = 1;
2116         }
2117         return ret;
2118 }
2119 
2120 int kvm_set_routing_entry(struct kvm_kernel_irq_routing_entry *e,
2121                           const struct kvm_irq_routing_entry *ue)
2122 {
2123         int ret;
2124 
2125         switch (ue->type) {
2126         case KVM_IRQ_ROUTING_S390_ADAPTER:
2127                 e->set = set_adapter_int;
2128                 e->adapter.summary_addr = ue->u.adapter.summary_addr;
2129                 e->adapter.ind_addr = ue->u.adapter.ind_addr;
2130                 e->adapter.summary_offset = ue->u.adapter.summary_offset;
2131                 e->adapter.ind_offset = ue->u.adapter.ind_offset;
2132                 e->adapter.adapter_id = ue->u.adapter.adapter_id;
2133                 ret = 0;
2134                 break;
2135         default:
2136                 ret = -EINVAL;
2137         }
2138 
2139         return ret;
2140 }
2141 
2142 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
2143                 int irq_source_id, int level, bool line_status)
2144 {
2145         return -EINVAL;
2146 }
2147 
2148 int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu, void __user *irqstate, int len)
2149 {
2150         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2151         struct kvm_s390_irq *buf;
2152         int r = 0;
2153         int n;
2154 
2155         buf = vmalloc(len);
2156         if (!buf)
2157                 return -ENOMEM;
2158 
2159         if (copy_from_user((void *) buf, irqstate, len)) {
2160                 r = -EFAULT;
2161                 goto out_free;
2162         }
2163 
2164         /*
2165          * Don't allow setting the interrupt state
2166          * when there are already interrupts pending
2167          */
2168         spin_lock(&li->lock);
2169         if (li->pending_irqs) {
2170                 r = -EBUSY;
2171                 goto out_unlock;
2172         }
2173 
2174         for (n = 0; n < len / sizeof(*buf); n++) {
2175                 r = do_inject_vcpu(vcpu, &buf[n]);
2176                 if (r)
2177                         break;
2178         }
2179 
2180 out_unlock:
2181         spin_unlock(&li->lock);
2182 out_free:
2183         vfree(buf);
2184 
2185         return r;
2186 }
2187 
2188 static void store_local_irq(struct kvm_s390_local_interrupt *li,
2189                             struct kvm_s390_irq *irq,
2190                             unsigned long irq_type)
2191 {
2192         switch (irq_type) {
2193         case IRQ_PEND_MCHK_EX:
2194         case IRQ_PEND_MCHK_REP:
2195                 irq->type = KVM_S390_MCHK;
2196                 irq->u.mchk = li->irq.mchk;
2197                 break;
2198         case IRQ_PEND_PROG:
2199                 irq->type = KVM_S390_PROGRAM_INT;
2200                 irq->u.pgm = li->irq.pgm;
2201                 break;
2202         case IRQ_PEND_PFAULT_INIT:
2203                 irq->type = KVM_S390_INT_PFAULT_INIT;
2204                 irq->u.ext = li->irq.ext;
2205                 break;
2206         case IRQ_PEND_EXT_EXTERNAL:
2207                 irq->type = KVM_S390_INT_EXTERNAL_CALL;
2208                 irq->u.extcall = li->irq.extcall;
2209                 break;
2210         case IRQ_PEND_EXT_CLOCK_COMP:
2211                 irq->type = KVM_S390_INT_CLOCK_COMP;
2212                 break;
2213         case IRQ_PEND_EXT_CPU_TIMER:
2214                 irq->type = KVM_S390_INT_CPU_TIMER;
2215                 break;
2216         case IRQ_PEND_SIGP_STOP:
2217                 irq->type = KVM_S390_SIGP_STOP;
2218                 irq->u.stop = li->irq.stop;
2219                 break;
2220         case IRQ_PEND_RESTART:
2221                 irq->type = KVM_S390_RESTART;
2222                 break;
2223         case IRQ_PEND_SET_PREFIX:
2224                 irq->type = KVM_S390_SIGP_SET_PREFIX;
2225                 irq->u.prefix = li->irq.prefix;
2226                 break;
2227         }
2228 }
2229 
2230 int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu, __u8 __user *buf, int len)
2231 {
2232         uint8_t sigp_ctrl = vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl;
2233         unsigned long sigp_emerg_pending[BITS_TO_LONGS(KVM_MAX_VCPUS)];
2234         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2235         unsigned long pending_irqs;
2236         struct kvm_s390_irq irq;
2237         unsigned long irq_type;
2238         int cpuaddr;
2239         int n = 0;
2240 
2241         spin_lock(&li->lock);
2242         pending_irqs = li->pending_irqs;
2243         memcpy(&sigp_emerg_pending, &li->sigp_emerg_pending,
2244                sizeof(sigp_emerg_pending));
2245         spin_unlock(&li->lock);
2246 
2247         for_each_set_bit(irq_type, &pending_irqs, IRQ_PEND_COUNT) {
2248                 memset(&irq, 0, sizeof(irq));
2249                 if (irq_type == IRQ_PEND_EXT_EMERGENCY)
2250                         continue;
2251                 if (n + sizeof(irq) > len)
2252                         return -ENOBUFS;
2253                 store_local_irq(&vcpu->arch.local_int, &irq, irq_type);
2254                 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2255                         return -EFAULT;
2256                 n += sizeof(irq);
2257         }
2258 
2259         if (test_bit(IRQ_PEND_EXT_EMERGENCY, &pending_irqs)) {
2260                 for_each_set_bit(cpuaddr, sigp_emerg_pending, KVM_MAX_VCPUS) {
2261                         memset(&irq, 0, sizeof(irq));
2262                         if (n + sizeof(irq) > len)
2263                                 return -ENOBUFS;
2264                         irq.type = KVM_S390_INT_EMERGENCY;
2265                         irq.u.emerg.code = cpuaddr;
2266                         if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2267                                 return -EFAULT;
2268                         n += sizeof(irq);
2269                 }
2270         }
2271 
2272         if ((sigp_ctrl & SIGP_CTRL_C) &&
2273             (atomic_read(&vcpu->arch.sie_block->cpuflags) &
2274              CPUSTAT_ECALL_PEND)) {
2275                 if (n + sizeof(irq) > len)
2276                         return -ENOBUFS;
2277                 memset(&irq, 0, sizeof(irq));
2278                 irq.type = KVM_S390_INT_EXTERNAL_CALL;
2279                 irq.u.extcall.code = sigp_ctrl & SIGP_CTRL_SCN_MASK;
2280                 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2281                         return -EFAULT;
2282                 n += sizeof(irq);
2283         }
2284 
2285         return n;
2286 }
2287 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp