2 * handling kvm guest interrupts
4 * Copyright IBM Corp. 2008, 2015
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.
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
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>
23 #include <asm/uaccess.h>
28 #include "trace-s390.h"
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
38 int psw_extint_disabled(struct kvm_vcpu
*vcpu
)
40 return !(vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_EXT
);
43 static int psw_ioint_disabled(struct kvm_vcpu
*vcpu
)
45 return !(vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_IO
);
48 static int psw_mchk_disabled(struct kvm_vcpu
*vcpu
)
50 return !(vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_MCHECK
);
53 static int psw_interrupts_disabled(struct kvm_vcpu
*vcpu
)
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
))
62 static int ckc_interrupts_enabled(struct kvm_vcpu
*vcpu
)
64 if (psw_extint_disabled(vcpu
) ||
65 !(vcpu
->arch
.sie_block
->gcr
[0] & 0x800ul
))
67 if (guestdbg_enabled(vcpu
) && guestdbg_sstep_enabled(vcpu
))
68 /* No timer interrupts when single stepping */
73 static int ckc_irq_pending(struct kvm_vcpu
*vcpu
)
75 if (!(vcpu
->arch
.sie_block
->ckc
<
76 get_tod_clock_fast() + vcpu
->arch
.sie_block
->epoch
))
78 return ckc_interrupts_enabled(vcpu
);
81 static int cpu_timer_interrupts_enabled(struct kvm_vcpu
*vcpu
)
83 return !psw_extint_disabled(vcpu
) &&
84 (vcpu
->arch
.sie_block
->gcr
[0] & 0x400ul
);
87 static int cpu_timer_irq_pending(struct kvm_vcpu
*vcpu
)
89 return (vcpu
->arch
.sie_block
->cputm
>> 63) &&
90 cpu_timer_interrupts_enabled(vcpu
);
93 static inline int is_ioirq(unsigned long irq_type
)
95 return ((irq_type
>= IRQ_PEND_IO_ISC_0
) &&
96 (irq_type
<= IRQ_PEND_IO_ISC_7
));
99 static uint64_t isc_to_isc_bits(int isc
)
101 return (0x80 >> isc
) << 24;
104 static inline u8
int_word_to_isc(u32 int_word
)
106 return (int_word
& 0x38000000) >> 27;
109 static inline unsigned long pending_floating_irqs(struct kvm_vcpu
*vcpu
)
111 return vcpu
->kvm
->arch
.float_int
.pending_irqs
;
114 static inline unsigned long pending_local_irqs(struct kvm_vcpu
*vcpu
)
116 return vcpu
->arch
.local_int
.pending_irqs
;
119 static unsigned long disable_iscs(struct kvm_vcpu
*vcpu
,
120 unsigned long active_mask
)
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
));
131 static unsigned long deliverable_irqs(struct kvm_vcpu
*vcpu
)
133 unsigned long active_mask
;
135 active_mask
= pending_local_irqs(vcpu
);
136 active_mask
|= pending_floating_irqs(vcpu
);
138 if (psw_extint_disabled(vcpu
))
139 active_mask
&= ~IRQ_PEND_EXT_MASK
;
140 if (psw_ioint_disabled(vcpu
))
141 active_mask
&= ~IRQ_PEND_IO_MASK
;
143 active_mask
= disable_iscs(vcpu
, active_mask
);
144 if (!(vcpu
->arch
.sie_block
->gcr
[0] & 0x2000ul
))
145 __clear_bit(IRQ_PEND_EXT_EXTERNAL
, &active_mask
);
146 if (!(vcpu
->arch
.sie_block
->gcr
[0] & 0x4000ul
))
147 __clear_bit(IRQ_PEND_EXT_EMERGENCY
, &active_mask
);
148 if (!(vcpu
->arch
.sie_block
->gcr
[0] & 0x800ul
))
149 __clear_bit(IRQ_PEND_EXT_CLOCK_COMP
, &active_mask
);
150 if (!(vcpu
->arch
.sie_block
->gcr
[0] & 0x400ul
))
151 __clear_bit(IRQ_PEND_EXT_CPU_TIMER
, &active_mask
);
152 if (!(vcpu
->arch
.sie_block
->gcr
[0] & 0x200ul
))
153 __clear_bit(IRQ_PEND_EXT_SERVICE
, &active_mask
);
154 if (psw_mchk_disabled(vcpu
))
155 active_mask
&= ~IRQ_PEND_MCHK_MASK
;
156 if (!(vcpu
->arch
.sie_block
->gcr
[14] &
157 vcpu
->kvm
->arch
.float_int
.mchk
.cr14
))
158 __clear_bit(IRQ_PEND_MCHK_REP
, &active_mask
);
161 * STOP irqs will never be actively delivered. They are triggered via
162 * intercept requests and cleared when the stop intercept is performed.
164 __clear_bit(IRQ_PEND_SIGP_STOP
, &active_mask
);
169 static void __set_cpu_idle(struct kvm_vcpu
*vcpu
)
171 atomic_set_mask(CPUSTAT_WAIT
, &vcpu
->arch
.sie_block
->cpuflags
);
172 set_bit(vcpu
->vcpu_id
, vcpu
->arch
.local_int
.float_int
->idle_mask
);
175 static void __unset_cpu_idle(struct kvm_vcpu
*vcpu
)
177 atomic_clear_mask(CPUSTAT_WAIT
, &vcpu
->arch
.sie_block
->cpuflags
);
178 clear_bit(vcpu
->vcpu_id
, vcpu
->arch
.local_int
.float_int
->idle_mask
);
181 static void __reset_intercept_indicators(struct kvm_vcpu
*vcpu
)
183 atomic_clear_mask(CPUSTAT_IO_INT
| CPUSTAT_EXT_INT
| CPUSTAT_STOP_INT
,
184 &vcpu
->arch
.sie_block
->cpuflags
);
185 vcpu
->arch
.sie_block
->lctl
= 0x0000;
186 vcpu
->arch
.sie_block
->ictl
&= ~(ICTL_LPSW
| ICTL_STCTL
| ICTL_PINT
);
188 if (guestdbg_enabled(vcpu
)) {
189 vcpu
->arch
.sie_block
->lctl
|= (LCTL_CR0
| LCTL_CR9
|
190 LCTL_CR10
| LCTL_CR11
);
191 vcpu
->arch
.sie_block
->ictl
|= (ICTL_STCTL
| ICTL_PINT
);
195 static void __set_cpuflag(struct kvm_vcpu
*vcpu
, u32 flag
)
197 atomic_set_mask(flag
, &vcpu
->arch
.sie_block
->cpuflags
);
200 static void set_intercept_indicators_io(struct kvm_vcpu
*vcpu
)
202 if (!(pending_floating_irqs(vcpu
) & IRQ_PEND_IO_MASK
))
204 else if (psw_ioint_disabled(vcpu
))
205 __set_cpuflag(vcpu
, CPUSTAT_IO_INT
);
207 vcpu
->arch
.sie_block
->lctl
|= LCTL_CR6
;
210 static void set_intercept_indicators_ext(struct kvm_vcpu
*vcpu
)
212 if (!(pending_local_irqs(vcpu
) & IRQ_PEND_EXT_MASK
))
214 if (psw_extint_disabled(vcpu
))
215 __set_cpuflag(vcpu
, CPUSTAT_EXT_INT
);
217 vcpu
->arch
.sie_block
->lctl
|= LCTL_CR0
;
220 static void set_intercept_indicators_mchk(struct kvm_vcpu
*vcpu
)
222 if (!(pending_local_irqs(vcpu
) & IRQ_PEND_MCHK_MASK
))
224 if (psw_mchk_disabled(vcpu
))
225 vcpu
->arch
.sie_block
->ictl
|= ICTL_LPSW
;
227 vcpu
->arch
.sie_block
->lctl
|= LCTL_CR14
;
230 static void set_intercept_indicators_stop(struct kvm_vcpu
*vcpu
)
232 if (kvm_s390_is_stop_irq_pending(vcpu
))
233 __set_cpuflag(vcpu
, CPUSTAT_STOP_INT
);
236 /* Set interception request for non-deliverable interrupts */
237 static void set_intercept_indicators(struct kvm_vcpu
*vcpu
)
239 set_intercept_indicators_io(vcpu
);
240 set_intercept_indicators_ext(vcpu
);
241 set_intercept_indicators_mchk(vcpu
);
242 set_intercept_indicators_stop(vcpu
);
245 static u16
get_ilc(struct kvm_vcpu
*vcpu
)
247 switch (vcpu
->arch
.sie_block
->icptcode
) {
253 /* last instruction only stored for these icptcodes */
254 return insn_length(vcpu
->arch
.sie_block
->ipa
>> 8);
256 return vcpu
->arch
.sie_block
->pgmilc
;
262 static int __must_check
__deliver_cpu_timer(struct kvm_vcpu
*vcpu
)
264 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
267 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
, KVM_S390_INT_CPU_TIMER
,
270 rc
= put_guest_lc(vcpu
, EXT_IRQ_CPU_TIMER
,
271 (u16
*)__LC_EXT_INT_CODE
);
272 rc
|= put_guest_lc(vcpu
, 0, (u16
*)__LC_EXT_CPU_ADDR
);
273 rc
|= write_guest_lc(vcpu
, __LC_EXT_OLD_PSW
,
274 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
275 rc
|= read_guest_lc(vcpu
, __LC_EXT_NEW_PSW
,
276 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
277 clear_bit(IRQ_PEND_EXT_CPU_TIMER
, &li
->pending_irqs
);
278 return rc
? -EFAULT
: 0;
281 static int __must_check
__deliver_ckc(struct kvm_vcpu
*vcpu
)
283 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
286 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
, KVM_S390_INT_CLOCK_COMP
,
289 rc
= put_guest_lc(vcpu
, EXT_IRQ_CLK_COMP
,
290 (u16 __user
*)__LC_EXT_INT_CODE
);
291 rc
|= put_guest_lc(vcpu
, 0, (u16
*)__LC_EXT_CPU_ADDR
);
292 rc
|= write_guest_lc(vcpu
, __LC_EXT_OLD_PSW
,
293 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
294 rc
|= read_guest_lc(vcpu
, __LC_EXT_NEW_PSW
,
295 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
296 clear_bit(IRQ_PEND_EXT_CLOCK_COMP
, &li
->pending_irqs
);
297 return rc
? -EFAULT
: 0;
300 static int __must_check
__deliver_pfault_init(struct kvm_vcpu
*vcpu
)
302 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
303 struct kvm_s390_ext_info ext
;
306 spin_lock(&li
->lock
);
308 clear_bit(IRQ_PEND_PFAULT_INIT
, &li
->pending_irqs
);
309 li
->irq
.ext
.ext_params2
= 0;
310 spin_unlock(&li
->lock
);
312 VCPU_EVENT(vcpu
, 4, "interrupt: pfault init parm:%x,parm64:%llx",
314 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
,
315 KVM_S390_INT_PFAULT_INIT
,
318 rc
= put_guest_lc(vcpu
, EXT_IRQ_CP_SERVICE
, (u16
*) __LC_EXT_INT_CODE
);
319 rc
|= put_guest_lc(vcpu
, PFAULT_INIT
, (u16
*) __LC_EXT_CPU_ADDR
);
320 rc
|= write_guest_lc(vcpu
, __LC_EXT_OLD_PSW
,
321 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
322 rc
|= read_guest_lc(vcpu
, __LC_EXT_NEW_PSW
,
323 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
324 rc
|= put_guest_lc(vcpu
, ext
.ext_params2
, (u64
*) __LC_EXT_PARAMS2
);
325 return rc
? -EFAULT
: 0;
328 static int __must_check
__deliver_machine_check(struct kvm_vcpu
*vcpu
)
330 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
331 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
332 struct kvm_s390_mchk_info mchk
= {};
333 unsigned long adtl_status_addr
;
337 spin_lock(&fi
->lock
);
338 spin_lock(&li
->lock
);
339 if (test_bit(IRQ_PEND_MCHK_EX
, &li
->pending_irqs
) ||
340 test_bit(IRQ_PEND_MCHK_REP
, &li
->pending_irqs
)) {
342 * If there was an exigent machine check pending, then any
343 * repressible machine checks that might have been pending
344 * are indicated along with it, so always clear bits for
345 * repressible and exigent interrupts
348 clear_bit(IRQ_PEND_MCHK_EX
, &li
->pending_irqs
);
349 clear_bit(IRQ_PEND_MCHK_REP
, &li
->pending_irqs
);
350 memset(&li
->irq
.mchk
, 0, sizeof(mchk
));
354 * We indicate floating repressible conditions along with
355 * other pending conditions. Channel Report Pending and Channel
356 * Subsystem damage are the only two and and are indicated by
357 * bits in mcic and masked in cr14.
359 if (test_and_clear_bit(IRQ_PEND_MCHK_REP
, &fi
->pending_irqs
)) {
360 mchk
.mcic
|= fi
->mchk
.mcic
;
361 mchk
.cr14
|= fi
->mchk
.cr14
;
362 memset(&fi
->mchk
, 0, sizeof(mchk
));
365 spin_unlock(&li
->lock
);
366 spin_unlock(&fi
->lock
);
369 VCPU_EVENT(vcpu
, 4, "interrupt: machine check mcic=%llx",
371 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
,
373 mchk
.cr14
, mchk
.mcic
);
375 rc
= kvm_s390_vcpu_store_status(vcpu
,
376 KVM_S390_STORE_STATUS_PREFIXED
);
377 rc
|= read_guest_lc(vcpu
, __LC_VX_SAVE_AREA_ADDR
,
379 sizeof(unsigned long));
380 rc
|= kvm_s390_vcpu_store_adtl_status(vcpu
,
382 rc
|= put_guest_lc(vcpu
, mchk
.mcic
,
383 (u64 __user
*) __LC_MCCK_CODE
);
384 rc
|= put_guest_lc(vcpu
, mchk
.failing_storage_address
,
385 (u64 __user
*) __LC_MCCK_FAIL_STOR_ADDR
);
386 rc
|= write_guest_lc(vcpu
, __LC_PSW_SAVE_AREA
,
388 sizeof(mchk
.fixed_logout
));
389 rc
|= write_guest_lc(vcpu
, __LC_MCK_OLD_PSW
,
390 &vcpu
->arch
.sie_block
->gpsw
,
392 rc
|= read_guest_lc(vcpu
, __LC_MCK_NEW_PSW
,
393 &vcpu
->arch
.sie_block
->gpsw
,
396 return rc
? -EFAULT
: 0;
399 static int __must_check
__deliver_restart(struct kvm_vcpu
*vcpu
)
401 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
404 VCPU_EVENT(vcpu
, 4, "%s", "interrupt: cpu restart");
405 vcpu
->stat
.deliver_restart_signal
++;
406 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
, KVM_S390_RESTART
, 0, 0);
408 rc
= write_guest_lc(vcpu
,
409 offsetof(struct _lowcore
, restart_old_psw
),
410 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
411 rc
|= read_guest_lc(vcpu
, offsetof(struct _lowcore
, restart_psw
),
412 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
413 clear_bit(IRQ_PEND_RESTART
, &li
->pending_irqs
);
414 return rc
? -EFAULT
: 0;
417 static int __must_check
__deliver_set_prefix(struct kvm_vcpu
*vcpu
)
419 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
420 struct kvm_s390_prefix_info prefix
;
422 spin_lock(&li
->lock
);
423 prefix
= li
->irq
.prefix
;
424 li
->irq
.prefix
.address
= 0;
425 clear_bit(IRQ_PEND_SET_PREFIX
, &li
->pending_irqs
);
426 spin_unlock(&li
->lock
);
428 VCPU_EVENT(vcpu
, 4, "interrupt: set prefix to %x", prefix
.address
);
429 vcpu
->stat
.deliver_prefix_signal
++;
430 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
,
431 KVM_S390_SIGP_SET_PREFIX
,
434 kvm_s390_set_prefix(vcpu
, prefix
.address
);
438 static int __must_check
__deliver_emergency_signal(struct kvm_vcpu
*vcpu
)
440 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
444 spin_lock(&li
->lock
);
445 cpu_addr
= find_first_bit(li
->sigp_emerg_pending
, KVM_MAX_VCPUS
);
446 clear_bit(cpu_addr
, li
->sigp_emerg_pending
);
447 if (bitmap_empty(li
->sigp_emerg_pending
, KVM_MAX_VCPUS
))
448 clear_bit(IRQ_PEND_EXT_EMERGENCY
, &li
->pending_irqs
);
449 spin_unlock(&li
->lock
);
451 VCPU_EVENT(vcpu
, 4, "%s", "interrupt: sigp emerg");
452 vcpu
->stat
.deliver_emergency_signal
++;
453 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
, KVM_S390_INT_EMERGENCY
,
456 rc
= put_guest_lc(vcpu
, EXT_IRQ_EMERGENCY_SIG
,
457 (u16
*)__LC_EXT_INT_CODE
);
458 rc
|= put_guest_lc(vcpu
, cpu_addr
, (u16
*)__LC_EXT_CPU_ADDR
);
459 rc
|= write_guest_lc(vcpu
, __LC_EXT_OLD_PSW
,
460 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
461 rc
|= read_guest_lc(vcpu
, __LC_EXT_NEW_PSW
,
462 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
463 return rc
? -EFAULT
: 0;
466 static int __must_check
__deliver_external_call(struct kvm_vcpu
*vcpu
)
468 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
469 struct kvm_s390_extcall_info extcall
;
472 spin_lock(&li
->lock
);
473 extcall
= li
->irq
.extcall
;
474 li
->irq
.extcall
.code
= 0;
475 clear_bit(IRQ_PEND_EXT_EXTERNAL
, &li
->pending_irqs
);
476 spin_unlock(&li
->lock
);
478 VCPU_EVENT(vcpu
, 4, "%s", "interrupt: sigp ext call");
479 vcpu
->stat
.deliver_external_call
++;
480 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
,
481 KVM_S390_INT_EXTERNAL_CALL
,
484 rc
= put_guest_lc(vcpu
, EXT_IRQ_EXTERNAL_CALL
,
485 (u16
*)__LC_EXT_INT_CODE
);
486 rc
|= put_guest_lc(vcpu
, extcall
.code
, (u16
*)__LC_EXT_CPU_ADDR
);
487 rc
|= write_guest_lc(vcpu
, __LC_EXT_OLD_PSW
,
488 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
489 rc
|= read_guest_lc(vcpu
, __LC_EXT_NEW_PSW
, &vcpu
->arch
.sie_block
->gpsw
,
491 return rc
? -EFAULT
: 0;
494 static int __must_check
__deliver_prog(struct kvm_vcpu
*vcpu
)
496 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
497 struct kvm_s390_pgm_info pgm_info
;
498 int rc
= 0, nullifying
= false;
499 u16 ilc
= get_ilc(vcpu
);
501 spin_lock(&li
->lock
);
502 pgm_info
= li
->irq
.pgm
;
503 clear_bit(IRQ_PEND_PROG
, &li
->pending_irqs
);
504 memset(&li
->irq
.pgm
, 0, sizeof(pgm_info
));
505 spin_unlock(&li
->lock
);
507 VCPU_EVENT(vcpu
, 4, "interrupt: pgm check code:%x, ilc:%x",
509 vcpu
->stat
.deliver_program_int
++;
510 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
, KVM_S390_PROGRAM_INT
,
513 switch (pgm_info
.code
& ~PGM_PER
) {
514 case PGM_AFX_TRANSLATION
:
515 case PGM_ASX_TRANSLATION
:
516 case PGM_EX_TRANSLATION
:
517 case PGM_LFX_TRANSLATION
:
518 case PGM_LSTE_SEQUENCE
:
519 case PGM_LSX_TRANSLATION
:
520 case PGM_LX_TRANSLATION
:
521 case PGM_PRIMARY_AUTHORITY
:
522 case PGM_SECONDARY_AUTHORITY
:
525 case PGM_SPACE_SWITCH
:
526 rc
= put_guest_lc(vcpu
, pgm_info
.trans_exc_code
,
527 (u64
*)__LC_TRANS_EXC_CODE
);
529 case PGM_ALEN_TRANSLATION
:
530 case PGM_ALE_SEQUENCE
:
531 case PGM_ASTE_INSTANCE
:
532 case PGM_ASTE_SEQUENCE
:
533 case PGM_ASTE_VALIDITY
:
534 case PGM_EXTENDED_AUTHORITY
:
535 rc
= put_guest_lc(vcpu
, pgm_info
.exc_access_id
,
536 (u8
*)__LC_EXC_ACCESS_ID
);
540 case PGM_PAGE_TRANSLATION
:
541 case PGM_REGION_FIRST_TRANS
:
542 case PGM_REGION_SECOND_TRANS
:
543 case PGM_REGION_THIRD_TRANS
:
544 case PGM_SEGMENT_TRANSLATION
:
545 rc
= put_guest_lc(vcpu
, pgm_info
.trans_exc_code
,
546 (u64
*)__LC_TRANS_EXC_CODE
);
547 rc
|= put_guest_lc(vcpu
, pgm_info
.exc_access_id
,
548 (u8
*)__LC_EXC_ACCESS_ID
);
549 rc
|= put_guest_lc(vcpu
, pgm_info
.op_access_id
,
550 (u8
*)__LC_OP_ACCESS_ID
);
554 rc
= put_guest_lc(vcpu
, pgm_info
.mon_class_nr
,
555 (u16
*)__LC_MON_CLASS_NR
);
556 rc
|= put_guest_lc(vcpu
, pgm_info
.mon_code
,
557 (u64
*)__LC_MON_CODE
);
559 case PGM_VECTOR_PROCESSING
:
561 rc
= put_guest_lc(vcpu
, pgm_info
.data_exc_code
,
562 (u32
*)__LC_DATA_EXC_CODE
);
565 rc
= put_guest_lc(vcpu
, pgm_info
.trans_exc_code
,
566 (u64
*)__LC_TRANS_EXC_CODE
);
567 rc
|= put_guest_lc(vcpu
, pgm_info
.exc_access_id
,
568 (u8
*)__LC_EXC_ACCESS_ID
);
571 case PGM_STACK_EMPTY
:
572 case PGM_STACK_SPECIFICATION
:
574 case PGM_STACK_OPERATION
:
575 case PGM_TRACE_TABEL
:
576 case PGM_CRYPTO_OPERATION
:
581 if (pgm_info
.code
& PGM_PER
) {
582 rc
|= put_guest_lc(vcpu
, pgm_info
.per_code
,
583 (u8
*) __LC_PER_CODE
);
584 rc
|= put_guest_lc(vcpu
, pgm_info
.per_atmid
,
585 (u8
*)__LC_PER_ATMID
);
586 rc
|= put_guest_lc(vcpu
, pgm_info
.per_address
,
587 (u64
*) __LC_PER_ADDRESS
);
588 rc
|= put_guest_lc(vcpu
, pgm_info
.per_access_id
,
589 (u8
*) __LC_PER_ACCESS_ID
);
592 if (nullifying
&& vcpu
->arch
.sie_block
->icptcode
== ICPT_INST
)
593 kvm_s390_rewind_psw(vcpu
, ilc
);
595 rc
|= put_guest_lc(vcpu
, ilc
, (u16
*) __LC_PGM_ILC
);
596 rc
|= put_guest_lc(vcpu
, vcpu
->arch
.sie_block
->gbea
,
597 (u64
*) __LC_LAST_BREAK
);
598 rc
|= put_guest_lc(vcpu
, pgm_info
.code
,
599 (u16
*)__LC_PGM_INT_CODE
);
600 rc
|= write_guest_lc(vcpu
, __LC_PGM_OLD_PSW
,
601 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
602 rc
|= read_guest_lc(vcpu
, __LC_PGM_NEW_PSW
,
603 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
604 return rc
? -EFAULT
: 0;
607 static int __must_check
__deliver_service(struct kvm_vcpu
*vcpu
)
609 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
610 struct kvm_s390_ext_info ext
;
613 spin_lock(&fi
->lock
);
614 if (!(test_bit(IRQ_PEND_EXT_SERVICE
, &fi
->pending_irqs
))) {
615 spin_unlock(&fi
->lock
);
618 ext
= fi
->srv_signal
;
619 memset(&fi
->srv_signal
, 0, sizeof(ext
));
620 clear_bit(IRQ_PEND_EXT_SERVICE
, &fi
->pending_irqs
);
621 spin_unlock(&fi
->lock
);
623 VCPU_EVENT(vcpu
, 4, "interrupt: sclp parm:%x",
625 vcpu
->stat
.deliver_service_signal
++;
626 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
, KVM_S390_INT_SERVICE
,
629 rc
= put_guest_lc(vcpu
, EXT_IRQ_SERVICE_SIG
, (u16
*)__LC_EXT_INT_CODE
);
630 rc
|= put_guest_lc(vcpu
, 0, (u16
*)__LC_EXT_CPU_ADDR
);
631 rc
|= write_guest_lc(vcpu
, __LC_EXT_OLD_PSW
,
632 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
633 rc
|= read_guest_lc(vcpu
, __LC_EXT_NEW_PSW
,
634 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
635 rc
|= put_guest_lc(vcpu
, ext
.ext_params
,
636 (u32
*)__LC_EXT_PARAMS
);
638 return rc
? -EFAULT
: 0;
641 static int __must_check
__deliver_pfault_done(struct kvm_vcpu
*vcpu
)
643 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
644 struct kvm_s390_interrupt_info
*inti
;
647 spin_lock(&fi
->lock
);
648 inti
= list_first_entry_or_null(&fi
->lists
[FIRQ_LIST_PFAULT
],
649 struct kvm_s390_interrupt_info
,
652 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
,
653 KVM_S390_INT_PFAULT_DONE
, 0,
654 inti
->ext
.ext_params2
);
655 list_del(&inti
->list
);
656 fi
->counters
[FIRQ_CNTR_PFAULT
] -= 1;
658 if (list_empty(&fi
->lists
[FIRQ_LIST_PFAULT
]))
659 clear_bit(IRQ_PEND_PFAULT_DONE
, &fi
->pending_irqs
);
660 spin_unlock(&fi
->lock
);
663 rc
= put_guest_lc(vcpu
, EXT_IRQ_CP_SERVICE
,
664 (u16
*)__LC_EXT_INT_CODE
);
665 rc
|= put_guest_lc(vcpu
, PFAULT_DONE
,
666 (u16
*)__LC_EXT_CPU_ADDR
);
667 rc
|= write_guest_lc(vcpu
, __LC_EXT_OLD_PSW
,
668 &vcpu
->arch
.sie_block
->gpsw
,
670 rc
|= read_guest_lc(vcpu
, __LC_EXT_NEW_PSW
,
671 &vcpu
->arch
.sie_block
->gpsw
,
673 rc
|= put_guest_lc(vcpu
, inti
->ext
.ext_params2
,
674 (u64
*)__LC_EXT_PARAMS2
);
677 return rc
? -EFAULT
: 0;
680 static int __must_check
__deliver_virtio(struct kvm_vcpu
*vcpu
)
682 struct kvm_s390_float_interrupt
*fi
= &vcpu
->kvm
->arch
.float_int
;
683 struct kvm_s390_interrupt_info
*inti
;
686 spin_lock(&fi
->lock
);
687 inti
= list_first_entry_or_null(&fi
->lists
[FIRQ_LIST_VIRTIO
],
688 struct kvm_s390_interrupt_info
,
692 "interrupt: virtio parm:%x,parm64:%llx",
693 inti
->ext
.ext_params
, inti
->ext
.ext_params2
);
694 vcpu
->stat
.deliver_virtio_interrupt
++;
695 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
,
697 inti
->ext
.ext_params
,
698 inti
->ext
.ext_params2
);
699 list_del(&inti
->list
);
700 fi
->counters
[FIRQ_CNTR_VIRTIO
] -= 1;
702 if (list_empty(&fi
->lists
[FIRQ_LIST_VIRTIO
]))
703 clear_bit(IRQ_PEND_VIRTIO
, &fi
->pending_irqs
);
704 spin_unlock(&fi
->lock
);
707 rc
= put_guest_lc(vcpu
, EXT_IRQ_CP_SERVICE
,
708 (u16
*)__LC_EXT_INT_CODE
);
709 rc
|= put_guest_lc(vcpu
, VIRTIO_PARAM
,
710 (u16
*)__LC_EXT_CPU_ADDR
);
711 rc
|= write_guest_lc(vcpu
, __LC_EXT_OLD_PSW
,
712 &vcpu
->arch
.sie_block
->gpsw
,
714 rc
|= read_guest_lc(vcpu
, __LC_EXT_NEW_PSW
,
715 &vcpu
->arch
.sie_block
->gpsw
,
717 rc
|= put_guest_lc(vcpu
, inti
->ext
.ext_params
,
718 (u32
*)__LC_EXT_PARAMS
);
719 rc
|= put_guest_lc(vcpu
, inti
->ext
.ext_params2
,
720 (u64
*)__LC_EXT_PARAMS2
);
723 return rc
? -EFAULT
: 0;
726 static int __must_check
__deliver_io(struct kvm_vcpu
*vcpu
,
727 unsigned long irq_type
)
729 struct list_head
*isc_list
;
730 struct kvm_s390_float_interrupt
*fi
;
731 struct kvm_s390_interrupt_info
*inti
= NULL
;
734 fi
= &vcpu
->kvm
->arch
.float_int
;
736 spin_lock(&fi
->lock
);
737 isc_list
= &fi
->lists
[irq_type
- IRQ_PEND_IO_ISC_0
];
738 inti
= list_first_entry_or_null(isc_list
,
739 struct kvm_s390_interrupt_info
,
742 VCPU_EVENT(vcpu
, 4, "interrupt: I/O %llx", inti
->type
);
743 vcpu
->stat
.deliver_io_int
++;
744 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
,
746 ((__u32
)inti
->io
.subchannel_id
<< 16) |
747 inti
->io
.subchannel_nr
,
748 ((__u64
)inti
->io
.io_int_parm
<< 32) |
749 inti
->io
.io_int_word
);
750 list_del(&inti
->list
);
751 fi
->counters
[FIRQ_CNTR_IO
] -= 1;
753 if (list_empty(isc_list
))
754 clear_bit(irq_type
, &fi
->pending_irqs
);
755 spin_unlock(&fi
->lock
);
758 rc
= put_guest_lc(vcpu
, inti
->io
.subchannel_id
,
759 (u16
*)__LC_SUBCHANNEL_ID
);
760 rc
|= put_guest_lc(vcpu
, inti
->io
.subchannel_nr
,
761 (u16
*)__LC_SUBCHANNEL_NR
);
762 rc
|= put_guest_lc(vcpu
, inti
->io
.io_int_parm
,
763 (u32
*)__LC_IO_INT_PARM
);
764 rc
|= put_guest_lc(vcpu
, inti
->io
.io_int_word
,
765 (u32
*)__LC_IO_INT_WORD
);
766 rc
|= write_guest_lc(vcpu
, __LC_IO_OLD_PSW
,
767 &vcpu
->arch
.sie_block
->gpsw
,
769 rc
|= read_guest_lc(vcpu
, __LC_IO_NEW_PSW
,
770 &vcpu
->arch
.sie_block
->gpsw
,
775 return rc
? -EFAULT
: 0;
778 typedef int (*deliver_irq_t
)(struct kvm_vcpu
*vcpu
);
780 static const deliver_irq_t deliver_irq_funcs
[] = {
781 [IRQ_PEND_MCHK_EX
] = __deliver_machine_check
,
782 [IRQ_PEND_MCHK_REP
] = __deliver_machine_check
,
783 [IRQ_PEND_PROG
] = __deliver_prog
,
784 [IRQ_PEND_EXT_EMERGENCY
] = __deliver_emergency_signal
,
785 [IRQ_PEND_EXT_EXTERNAL
] = __deliver_external_call
,
786 [IRQ_PEND_EXT_CLOCK_COMP
] = __deliver_ckc
,
787 [IRQ_PEND_EXT_CPU_TIMER
] = __deliver_cpu_timer
,
788 [IRQ_PEND_RESTART
] = __deliver_restart
,
789 [IRQ_PEND_SET_PREFIX
] = __deliver_set_prefix
,
790 [IRQ_PEND_PFAULT_INIT
] = __deliver_pfault_init
,
791 [IRQ_PEND_EXT_SERVICE
] = __deliver_service
,
792 [IRQ_PEND_PFAULT_DONE
] = __deliver_pfault_done
,
793 [IRQ_PEND_VIRTIO
] = __deliver_virtio
,
796 /* Check whether an external call is pending (deliverable or not) */
797 int kvm_s390_ext_call_pending(struct kvm_vcpu
*vcpu
)
799 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
800 uint8_t sigp_ctrl
= vcpu
->kvm
->arch
.sca
->cpu
[vcpu
->vcpu_id
].sigp_ctrl
;
802 if (!sclp_has_sigpif())
803 return test_bit(IRQ_PEND_EXT_EXTERNAL
, &li
->pending_irqs
);
805 return (sigp_ctrl
& SIGP_CTRL_C
) &&
806 (atomic_read(&vcpu
->arch
.sie_block
->cpuflags
) & CPUSTAT_ECALL_PEND
);
809 int kvm_s390_vcpu_has_irq(struct kvm_vcpu
*vcpu
, int exclude_stop
)
813 rc
= !!deliverable_irqs(vcpu
);
815 if (!rc
&& kvm_cpu_has_pending_timer(vcpu
))
818 /* external call pending and deliverable */
819 if (!rc
&& kvm_s390_ext_call_pending(vcpu
) &&
820 !psw_extint_disabled(vcpu
) &&
821 (vcpu
->arch
.sie_block
->gcr
[0] & 0x2000ul
))
824 if (!rc
&& !exclude_stop
&& kvm_s390_is_stop_irq_pending(vcpu
))
830 int kvm_cpu_has_pending_timer(struct kvm_vcpu
*vcpu
)
832 return ckc_irq_pending(vcpu
) || cpu_timer_irq_pending(vcpu
);
835 int kvm_s390_handle_wait(struct kvm_vcpu
*vcpu
)
839 vcpu
->stat
.exit_wait_state
++;
842 if (kvm_cpu_has_pending_timer(vcpu
) || kvm_arch_vcpu_runnable(vcpu
))
845 if (psw_interrupts_disabled(vcpu
)) {
846 VCPU_EVENT(vcpu
, 3, "%s", "disabled wait");
847 return -EOPNOTSUPP
; /* disabled wait */
850 if (!ckc_interrupts_enabled(vcpu
)) {
851 VCPU_EVENT(vcpu
, 3, "%s", "enabled wait w/o timer");
852 __set_cpu_idle(vcpu
);
856 now
= get_tod_clock_fast() + vcpu
->arch
.sie_block
->epoch
;
857 sltime
= tod_to_ns(vcpu
->arch
.sie_block
->ckc
- now
);
860 if (vcpu
->arch
.sie_block
->ckc
< now
)
863 __set_cpu_idle(vcpu
);
864 hrtimer_start(&vcpu
->arch
.ckc_timer
, ktime_set (0, sltime
) , HRTIMER_MODE_REL
);
865 VCPU_EVENT(vcpu
, 5, "enabled wait via clock comparator: %llx ns", sltime
);
867 srcu_read_unlock(&vcpu
->kvm
->srcu
, vcpu
->srcu_idx
);
868 kvm_vcpu_block(vcpu
);
869 __unset_cpu_idle(vcpu
);
870 vcpu
->srcu_idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
872 hrtimer_cancel(&vcpu
->arch
.ckc_timer
);
876 void kvm_s390_vcpu_wakeup(struct kvm_vcpu
*vcpu
)
878 if (waitqueue_active(&vcpu
->wq
)) {
880 * The vcpu gave up the cpu voluntarily, mark it as a good
883 vcpu
->preempted
= true;
884 wake_up_interruptible(&vcpu
->wq
);
885 vcpu
->stat
.halt_wakeup
++;
889 enum hrtimer_restart
kvm_s390_idle_wakeup(struct hrtimer
*timer
)
891 struct kvm_vcpu
*vcpu
;
894 vcpu
= container_of(timer
, struct kvm_vcpu
, arch
.ckc_timer
);
895 now
= get_tod_clock_fast() + vcpu
->arch
.sie_block
->epoch
;
896 sltime
= tod_to_ns(vcpu
->arch
.sie_block
->ckc
- now
);
899 * If the monotonic clock runs faster than the tod clock we might be
900 * woken up too early and have to go back to sleep to avoid deadlocks.
902 if (vcpu
->arch
.sie_block
->ckc
> now
&&
903 hrtimer_forward_now(timer
, ns_to_ktime(sltime
)))
904 return HRTIMER_RESTART
;
905 kvm_s390_vcpu_wakeup(vcpu
);
906 return HRTIMER_NORESTART
;
909 void kvm_s390_clear_local_irqs(struct kvm_vcpu
*vcpu
)
911 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
913 spin_lock(&li
->lock
);
914 li
->pending_irqs
= 0;
915 bitmap_zero(li
->sigp_emerg_pending
, KVM_MAX_VCPUS
);
916 memset(&li
->irq
, 0, sizeof(li
->irq
));
917 spin_unlock(&li
->lock
);
919 /* clear pending external calls set by sigp interpretation facility */
920 atomic_clear_mask(CPUSTAT_ECALL_PEND
, li
->cpuflags
);
921 vcpu
->kvm
->arch
.sca
->cpu
[vcpu
->vcpu_id
].sigp_ctrl
= 0;
924 int __must_check
kvm_s390_deliver_pending_interrupts(struct kvm_vcpu
*vcpu
)
926 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
929 unsigned long irq_type
;
932 __reset_intercept_indicators(vcpu
);
934 /* pending ckc conditions might have been invalidated */
935 clear_bit(IRQ_PEND_EXT_CLOCK_COMP
, &li
->pending_irqs
);
936 if (ckc_irq_pending(vcpu
))
937 set_bit(IRQ_PEND_EXT_CLOCK_COMP
, &li
->pending_irqs
);
939 /* pending cpu timer conditions might have been invalidated */
940 clear_bit(IRQ_PEND_EXT_CPU_TIMER
, &li
->pending_irqs
);
941 if (cpu_timer_irq_pending(vcpu
))
942 set_bit(IRQ_PEND_EXT_CPU_TIMER
, &li
->pending_irqs
);
945 irqs
= deliverable_irqs(vcpu
);
946 /* bits are in the order of interrupt priority */
947 irq_type
= find_first_bit(&irqs
, IRQ_PEND_COUNT
);
948 if (irq_type
== IRQ_PEND_COUNT
)
950 if (is_ioirq(irq_type
)) {
951 rc
= __deliver_io(vcpu
, irq_type
);
953 func
= deliver_irq_funcs
[irq_type
];
955 WARN_ON_ONCE(func
== NULL
);
956 clear_bit(irq_type
, &li
->pending_irqs
);
965 set_intercept_indicators(vcpu
);
970 static int __inject_prog(struct kvm_vcpu
*vcpu
, struct kvm_s390_irq
*irq
)
972 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
974 li
->irq
.pgm
= irq
->u
.pgm
;
975 set_bit(IRQ_PEND_PROG
, &li
->pending_irqs
);
979 int kvm_s390_inject_program_int(struct kvm_vcpu
*vcpu
, u16 code
)
981 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
982 struct kvm_s390_irq irq
;
984 VCPU_EVENT(vcpu
, 3, "inject: program check %d (from kernel)", code
);
985 trace_kvm_s390_inject_vcpu(vcpu
->vcpu_id
, KVM_S390_PROGRAM_INT
, code
,
987 spin_lock(&li
->lock
);
988 irq
.u
.pgm
.code
= code
;
989 __inject_prog(vcpu
, &irq
);
990 BUG_ON(waitqueue_active(li
->wq
));
991 spin_unlock(&li
->lock
);
995 int kvm_s390_inject_prog_irq(struct kvm_vcpu
*vcpu
,
996 struct kvm_s390_pgm_info
*pgm_info
)
998 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
999 struct kvm_s390_irq irq
;
1002 VCPU_EVENT(vcpu
, 3, "inject: prog irq %d (from kernel)",
1004 trace_kvm_s390_inject_vcpu(vcpu
->vcpu_id
, KVM_S390_PROGRAM_INT
,
1005 pgm_info
->code
, 0, 1);
1006 spin_lock(&li
->lock
);
1007 irq
.u
.pgm
= *pgm_info
;
1008 rc
= __inject_prog(vcpu
, &irq
);
1009 BUG_ON(waitqueue_active(li
->wq
));
1010 spin_unlock(&li
->lock
);
1014 static int __inject_pfault_init(struct kvm_vcpu
*vcpu
, struct kvm_s390_irq
*irq
)
1016 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1018 VCPU_EVENT(vcpu
, 3, "inject: external irq params:%x, params2:%llx",
1019 irq
->u
.ext
.ext_params
, irq
->u
.ext
.ext_params2
);
1020 trace_kvm_s390_inject_vcpu(vcpu
->vcpu_id
, KVM_S390_INT_PFAULT_INIT
,
1021 irq
->u
.ext
.ext_params
,
1022 irq
->u
.ext
.ext_params2
, 2);
1024 li
->irq
.ext
= irq
->u
.ext
;
1025 set_bit(IRQ_PEND_PFAULT_INIT
, &li
->pending_irqs
);
1026 atomic_set_mask(CPUSTAT_EXT_INT
, li
->cpuflags
);
1030 static int __inject_extcall_sigpif(struct kvm_vcpu
*vcpu
, uint16_t src_id
)
1032 unsigned char new_val
, old_val
;
1033 uint8_t *sigp_ctrl
= &vcpu
->kvm
->arch
.sca
->cpu
[vcpu
->vcpu_id
].sigp_ctrl
;
1035 new_val
= SIGP_CTRL_C
| (src_id
& SIGP_CTRL_SCN_MASK
);
1036 old_val
= *sigp_ctrl
& ~SIGP_CTRL_C
;
1037 if (cmpxchg(sigp_ctrl
, old_val
, new_val
) != old_val
) {
1038 /* another external call is pending */
1041 atomic_set_mask(CPUSTAT_ECALL_PEND
, &vcpu
->arch
.sie_block
->cpuflags
);
1045 static int __inject_extcall(struct kvm_vcpu
*vcpu
, struct kvm_s390_irq
*irq
)
1047 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1048 struct kvm_s390_extcall_info
*extcall
= &li
->irq
.extcall
;
1049 uint16_t src_id
= irq
->u
.extcall
.code
;
1051 VCPU_EVENT(vcpu
, 3, "inject: external call source-cpu:%u",
1053 trace_kvm_s390_inject_vcpu(vcpu
->vcpu_id
, KVM_S390_INT_EXTERNAL_CALL
,
1056 /* sending vcpu invalid */
1057 if (src_id
>= KVM_MAX_VCPUS
||
1058 kvm_get_vcpu(vcpu
->kvm
, src_id
) == NULL
)
1061 if (sclp_has_sigpif())
1062 return __inject_extcall_sigpif(vcpu
, src_id
);
1064 if (test_and_set_bit(IRQ_PEND_EXT_EXTERNAL
, &li
->pending_irqs
))
1066 *extcall
= irq
->u
.extcall
;
1067 atomic_set_mask(CPUSTAT_EXT_INT
, li
->cpuflags
);
1071 static int __inject_set_prefix(struct kvm_vcpu
*vcpu
, struct kvm_s390_irq
*irq
)
1073 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1074 struct kvm_s390_prefix_info
*prefix
= &li
->irq
.prefix
;
1076 VCPU_EVENT(vcpu
, 3, "inject: set prefix to %x (from user)",
1077 irq
->u
.prefix
.address
);
1078 trace_kvm_s390_inject_vcpu(vcpu
->vcpu_id
, KVM_S390_SIGP_SET_PREFIX
,
1079 irq
->u
.prefix
.address
, 0, 2);
1081 if (!is_vcpu_stopped(vcpu
))
1084 *prefix
= irq
->u
.prefix
;
1085 set_bit(IRQ_PEND_SET_PREFIX
, &li
->pending_irqs
);
1089 #define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS)
1090 static int __inject_sigp_stop(struct kvm_vcpu
*vcpu
, struct kvm_s390_irq
*irq
)
1092 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1093 struct kvm_s390_stop_info
*stop
= &li
->irq
.stop
;
1096 trace_kvm_s390_inject_vcpu(vcpu
->vcpu_id
, KVM_S390_SIGP_STOP
, 0, 0, 2);
1098 if (irq
->u
.stop
.flags
& ~KVM_S390_STOP_SUPP_FLAGS
)
1101 if (is_vcpu_stopped(vcpu
)) {
1102 if (irq
->u
.stop
.flags
& KVM_S390_STOP_FLAG_STORE_STATUS
)
1103 rc
= kvm_s390_store_status_unloaded(vcpu
,
1104 KVM_S390_STORE_STATUS_NOADDR
);
1108 if (test_and_set_bit(IRQ_PEND_SIGP_STOP
, &li
->pending_irqs
))
1110 stop
->flags
= irq
->u
.stop
.flags
;
1111 __set_cpuflag(vcpu
, CPUSTAT_STOP_INT
);
1115 static int __inject_sigp_restart(struct kvm_vcpu
*vcpu
,
1116 struct kvm_s390_irq
*irq
)
1118 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1120 VCPU_EVENT(vcpu
, 3, "inject: restart type %llx", irq
->type
);
1121 trace_kvm_s390_inject_vcpu(vcpu
->vcpu_id
, KVM_S390_RESTART
, 0, 0, 2);
1123 set_bit(IRQ_PEND_RESTART
, &li
->pending_irqs
);
1127 static int __inject_sigp_emergency(struct kvm_vcpu
*vcpu
,
1128 struct kvm_s390_irq
*irq
)
1130 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1132 VCPU_EVENT(vcpu
, 3, "inject: emergency %u\n",
1134 trace_kvm_s390_inject_vcpu(vcpu
->vcpu_id
, KVM_S390_INT_EMERGENCY
,
1135 irq
->u
.emerg
.code
, 0, 2);
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
);
1143 static int __inject_mchk(struct kvm_vcpu
*vcpu
, struct kvm_s390_irq
*irq
)
1145 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1146 struct kvm_s390_mchk_info
*mchk
= &li
->irq
.mchk
;
1148 VCPU_EVENT(vcpu
, 5, "inject: machine check parm64:%llx",
1150 trace_kvm_s390_inject_vcpu(vcpu
->vcpu_id
, KVM_S390_MCHK
, 0,
1151 irq
->u
.mchk
.mcic
, 2);
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
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
);
1174 static int __inject_ckc(struct kvm_vcpu
*vcpu
)
1176 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
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
,
1182 set_bit(IRQ_PEND_EXT_CLOCK_COMP
, &li
->pending_irqs
);
1183 atomic_set_mask(CPUSTAT_EXT_INT
, li
->cpuflags
);
1187 static int __inject_cpu_timer(struct kvm_vcpu
*vcpu
)
1189 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
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
,
1195 set_bit(IRQ_PEND_EXT_CPU_TIMER
, &li
->pending_irqs
);
1196 atomic_set_mask(CPUSTAT_EXT_INT
, li
->cpuflags
);
1200 static struct kvm_s390_interrupt_info
*get_io_int(struct kvm
*kvm
,
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
;
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
))
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
);
1222 spin_unlock(&fi
->lock
);
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).
1230 struct kvm_s390_interrupt_info
*kvm_s390_get_io_int(struct kvm
*kvm
,
1231 u64 isc_mask
, u32 schid
)
1233 struct kvm_s390_interrupt_info
*inti
= NULL
;
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
);
1243 #define SCCB_MASK 0xFFFFFFF8
1244 #define SCCB_EVENT_PENDING 0x3
1246 static int __inject_service(struct kvm
*kvm
,
1247 struct kvm_s390_interrupt_info
*inti
)
1249 struct kvm_s390_float_interrupt
*fi
= &kvm
->arch
.float_int
;
1251 spin_lock(&fi
->lock
);
1252 fi
->srv_signal
.ext_params
|= inti
->ext
.ext_params
& SCCB_EVENT_PENDING
;
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
1261 if (fi
->srv_signal
.ext_params
& SCCB_MASK
)
1263 fi
->srv_signal
.ext_params
|= inti
->ext
.ext_params
& SCCB_MASK
;
1264 set_bit(IRQ_PEND_EXT_SERVICE
, &fi
->pending_irqs
);
1266 spin_unlock(&fi
->lock
);
1271 static int __inject_virtio(struct kvm
*kvm
,
1272 struct kvm_s390_interrupt_info
*inti
)
1274 struct kvm_s390_float_interrupt
*fi
= &kvm
->arch
.float_int
;
1276 spin_lock(&fi
->lock
);
1277 if (fi
->counters
[FIRQ_CNTR_VIRTIO
] >= KVM_S390_MAX_VIRTIO_IRQS
) {
1278 spin_unlock(&fi
->lock
);
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
);
1288 static int __inject_pfault_done(struct kvm
*kvm
,
1289 struct kvm_s390_interrupt_info
*inti
)
1291 struct kvm_s390_float_interrupt
*fi
= &kvm
->arch
.float_int
;
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
);
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
);
1306 #define CR_PENDING_SUBCLASS 28
1307 static int __inject_float_mchk(struct kvm
*kvm
,
1308 struct kvm_s390_interrupt_info
*inti
)
1310 struct kvm_s390_float_interrupt
*fi
= &kvm
->arch
.float_int
;
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
);
1321 static int __inject_io(struct kvm
*kvm
, struct kvm_s390_interrupt_info
*inti
)
1323 struct kvm_s390_float_interrupt
*fi
;
1324 struct list_head
*list
;
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
);
1333 fi
->counters
[FIRQ_CNTR_IO
] += 1;
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
);
1343 static int __inject_vm(struct kvm
*kvm
, struct kvm_s390_interrupt_info
*inti
)
1345 struct kvm_s390_local_interrupt
*li
;
1346 struct kvm_s390_float_interrupt
*fi
;
1347 struct kvm_vcpu
*dst_vcpu
= NULL
;
1349 u64 type
= READ_ONCE(inti
->type
);
1352 fi
= &kvm
->arch
.float_int
;
1356 rc
= __inject_float_mchk(kvm
, inti
);
1358 case KVM_S390_INT_VIRTIO
:
1359 rc
= __inject_virtio(kvm
, inti
);
1361 case KVM_S390_INT_SERVICE
:
1362 rc
= __inject_service(kvm
, inti
);
1364 case KVM_S390_INT_PFAULT_DONE
:
1365 rc
= __inject_pfault_done(kvm
, inti
);
1367 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
1368 rc
= __inject_io(kvm
, inti
);
1376 sigcpu
= find_first_bit(fi
->idle_mask
, KVM_MAX_VCPUS
);
1377 if (sigcpu
== KVM_MAX_VCPUS
) {
1379 sigcpu
= fi
->next_rr_cpu
++;
1380 if (sigcpu
== KVM_MAX_VCPUS
)
1381 sigcpu
= fi
->next_rr_cpu
= 0;
1382 } while (kvm_get_vcpu(kvm
, sigcpu
) == NULL
);
1384 dst_vcpu
= kvm_get_vcpu(kvm
, sigcpu
);
1385 li
= &dst_vcpu
->arch
.local_int
;
1386 spin_lock(&li
->lock
);
1389 atomic_set_mask(CPUSTAT_STOP_INT
, li
->cpuflags
);
1391 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
1392 atomic_set_mask(CPUSTAT_IO_INT
, li
->cpuflags
);
1395 atomic_set_mask(CPUSTAT_EXT_INT
, li
->cpuflags
);
1398 spin_unlock(&li
->lock
);
1399 kvm_s390_vcpu_wakeup(kvm_get_vcpu(kvm
, sigcpu
));
1404 int kvm_s390_inject_vm(struct kvm
*kvm
,
1405 struct kvm_s390_interrupt
*s390int
)
1407 struct kvm_s390_interrupt_info
*inti
;
1410 inti
= kzalloc(sizeof(*inti
), GFP_KERNEL
);
1414 inti
->type
= s390int
->type
;
1415 switch (inti
->type
) {
1416 case KVM_S390_INT_VIRTIO
:
1417 VM_EVENT(kvm
, 5, "inject: virtio parm:%x,parm64:%llx",
1418 s390int
->parm
, s390int
->parm64
);
1419 inti
->ext
.ext_params
= s390int
->parm
;
1420 inti
->ext
.ext_params2
= s390int
->parm64
;
1422 case KVM_S390_INT_SERVICE
:
1423 VM_EVENT(kvm
, 5, "inject: sclp parm:%x", s390int
->parm
);
1424 inti
->ext
.ext_params
= s390int
->parm
;
1426 case KVM_S390_INT_PFAULT_DONE
:
1427 inti
->ext
.ext_params2
= s390int
->parm64
;
1430 VM_EVENT(kvm
, 5, "inject: machine check parm64:%llx",
1432 inti
->mchk
.cr14
= s390int
->parm
; /* upper bits are not used */
1433 inti
->mchk
.mcic
= s390int
->parm64
;
1435 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
1436 if (inti
->type
& IOINT_AI_MASK
)
1437 VM_EVENT(kvm
, 5, "%s", "inject: I/O (AI)");
1439 VM_EVENT(kvm
, 5, "inject: I/O css %x ss %x schid %04x",
1440 s390int
->type
& IOINT_CSSID_MASK
,
1441 s390int
->type
& IOINT_SSID_MASK
,
1442 s390int
->type
& IOINT_SCHID_MASK
);
1443 inti
->io
.subchannel_id
= s390int
->parm
>> 16;
1444 inti
->io
.subchannel_nr
= s390int
->parm
& 0x0000ffffu
;
1445 inti
->io
.io_int_parm
= s390int
->parm64
>> 32;
1446 inti
->io
.io_int_word
= s390int
->parm64
& 0x00000000ffffffffull
;
1452 trace_kvm_s390_inject_vm(s390int
->type
, s390int
->parm
, s390int
->parm64
,
1455 rc
= __inject_vm(kvm
, inti
);
1461 int kvm_s390_reinject_io_int(struct kvm
*kvm
,
1462 struct kvm_s390_interrupt_info
*inti
)
1464 return __inject_vm(kvm
, inti
);
1467 int s390int_to_s390irq(struct kvm_s390_interrupt
*s390int
,
1468 struct kvm_s390_irq
*irq
)
1470 irq
->type
= s390int
->type
;
1471 switch (irq
->type
) {
1472 case KVM_S390_PROGRAM_INT
:
1473 if (s390int
->parm
& 0xffff0000)
1475 irq
->u
.pgm
.code
= s390int
->parm
;
1477 case KVM_S390_SIGP_SET_PREFIX
:
1478 irq
->u
.prefix
.address
= s390int
->parm
;
1480 case KVM_S390_SIGP_STOP
:
1481 irq
->u
.stop
.flags
= s390int
->parm
;
1483 case KVM_S390_INT_EXTERNAL_CALL
:
1484 if (s390int
->parm
& 0xffff0000)
1486 irq
->u
.extcall
.code
= s390int
->parm
;
1488 case KVM_S390_INT_EMERGENCY
:
1489 if (s390int
->parm
& 0xffff0000)
1491 irq
->u
.emerg
.code
= s390int
->parm
;
1494 irq
->u
.mchk
.mcic
= s390int
->parm64
;
1500 int kvm_s390_is_stop_irq_pending(struct kvm_vcpu
*vcpu
)
1502 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1504 return test_bit(IRQ_PEND_SIGP_STOP
, &li
->pending_irqs
);
1507 void kvm_s390_clear_stop_irq(struct kvm_vcpu
*vcpu
)
1509 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1511 spin_lock(&li
->lock
);
1512 li
->irq
.stop
.flags
= 0;
1513 clear_bit(IRQ_PEND_SIGP_STOP
, &li
->pending_irqs
);
1514 spin_unlock(&li
->lock
);
1517 static int do_inject_vcpu(struct kvm_vcpu
*vcpu
, struct kvm_s390_irq
*irq
)
1521 switch (irq
->type
) {
1522 case KVM_S390_PROGRAM_INT
:
1523 VCPU_EVENT(vcpu
, 3, "inject: program check %d (from user)",
1525 rc
= __inject_prog(vcpu
, irq
);
1527 case KVM_S390_SIGP_SET_PREFIX
:
1528 rc
= __inject_set_prefix(vcpu
, irq
);
1530 case KVM_S390_SIGP_STOP
:
1531 rc
= __inject_sigp_stop(vcpu
, irq
);
1533 case KVM_S390_RESTART
:
1534 rc
= __inject_sigp_restart(vcpu
, irq
);
1536 case KVM_S390_INT_CLOCK_COMP
:
1537 rc
= __inject_ckc(vcpu
);
1539 case KVM_S390_INT_CPU_TIMER
:
1540 rc
= __inject_cpu_timer(vcpu
);
1542 case KVM_S390_INT_EXTERNAL_CALL
:
1543 rc
= __inject_extcall(vcpu
, irq
);
1545 case KVM_S390_INT_EMERGENCY
:
1546 rc
= __inject_sigp_emergency(vcpu
, irq
);
1549 rc
= __inject_mchk(vcpu
, irq
);
1551 case KVM_S390_INT_PFAULT_INIT
:
1552 rc
= __inject_pfault_init(vcpu
, irq
);
1554 case KVM_S390_INT_VIRTIO
:
1555 case KVM_S390_INT_SERVICE
:
1556 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
1564 int kvm_s390_inject_vcpu(struct kvm_vcpu
*vcpu
, struct kvm_s390_irq
*irq
)
1566 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
1569 spin_lock(&li
->lock
);
1570 rc
= do_inject_vcpu(vcpu
, irq
);
1571 spin_unlock(&li
->lock
);
1573 kvm_s390_vcpu_wakeup(vcpu
);
1577 static inline void clear_irq_list(struct list_head
*_list
)
1579 struct kvm_s390_interrupt_info
*inti
, *n
;
1581 list_for_each_entry_safe(inti
, n
, _list
, list
) {
1582 list_del(&inti
->list
);
1587 static void inti_to_irq(struct kvm_s390_interrupt_info
*inti
,
1588 struct kvm_s390_irq
*irq
)
1590 irq
->type
= inti
->type
;
1591 switch (inti
->type
) {
1592 case KVM_S390_INT_PFAULT_INIT
:
1593 case KVM_S390_INT_PFAULT_DONE
:
1594 case KVM_S390_INT_VIRTIO
:
1595 irq
->u
.ext
= inti
->ext
;
1597 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
1598 irq
->u
.io
= inti
->io
;
1603 void kvm_s390_clear_float_irqs(struct kvm
*kvm
)
1605 struct kvm_s390_float_interrupt
*fi
= &kvm
->arch
.float_int
;
1608 spin_lock(&fi
->lock
);
1609 for (i
= 0; i
< FIRQ_LIST_COUNT
; i
++)
1610 clear_irq_list(&fi
->lists
[i
]);
1611 for (i
= 0; i
< FIRQ_MAX_COUNT
; i
++)
1612 fi
->counters
[i
] = 0;
1613 spin_unlock(&fi
->lock
);
1616 static int get_all_floating_irqs(struct kvm
*kvm
, u8 __user
*usrbuf
, u64 len
)
1618 struct kvm_s390_interrupt_info
*inti
;
1619 struct kvm_s390_float_interrupt
*fi
;
1620 struct kvm_s390_irq
*buf
;
1621 struct kvm_s390_irq
*irq
;
1627 if (len
> KVM_S390_FLIC_MAX_BUFFER
|| len
== 0)
1631 * We are already using -ENOMEM to signal
1632 * userspace it may retry with a bigger buffer,
1633 * so we need to use something else for this case
1639 max_irqs
= len
/ sizeof(struct kvm_s390_irq
);
1641 fi
= &kvm
->arch
.float_int
;
1642 spin_lock(&fi
->lock
);
1643 for (i
= 0; i
< FIRQ_LIST_COUNT
; i
++) {
1644 list_for_each_entry(inti
, &fi
->lists
[i
], list
) {
1645 if (n
== max_irqs
) {
1646 /* signal userspace to try again */
1650 inti_to_irq(inti
, &buf
[n
]);
1654 if (test_bit(IRQ_PEND_EXT_SERVICE
, &fi
->pending_irqs
)) {
1655 if (n
== max_irqs
) {
1656 /* signal userspace to try again */
1660 irq
= (struct kvm_s390_irq
*) &buf
[n
];
1661 irq
->type
= KVM_S390_INT_SERVICE
;
1662 irq
->u
.ext
= fi
->srv_signal
;
1665 if (test_bit(IRQ_PEND_MCHK_REP
, &fi
->pending_irqs
)) {
1666 if (n
== max_irqs
) {
1667 /* signal userspace to try again */
1671 irq
= (struct kvm_s390_irq
*) &buf
[n
];
1672 irq
->type
= KVM_S390_MCHK
;
1673 irq
->u
.mchk
= fi
->mchk
;
1678 spin_unlock(&fi
->lock
);
1679 if (!ret
&& n
> 0) {
1680 if (copy_to_user(usrbuf
, buf
, sizeof(struct kvm_s390_irq
) * n
))
1685 return ret
< 0 ? ret
: n
;
1688 static int flic_get_attr(struct kvm_device
*dev
, struct kvm_device_attr
*attr
)
1692 switch (attr
->group
) {
1693 case KVM_DEV_FLIC_GET_ALL_IRQS
:
1694 r
= get_all_floating_irqs(dev
->kvm
, (u8 __user
*) attr
->addr
,
1704 static inline int copy_irq_from_user(struct kvm_s390_interrupt_info
*inti
,
1707 struct kvm_s390_irq __user
*uptr
= (struct kvm_s390_irq __user
*) addr
;
1708 void *target
= NULL
;
1709 void __user
*source
;
1712 if (get_user(inti
->type
, (u64 __user
*)addr
))
1715 switch (inti
->type
) {
1716 case KVM_S390_INT_PFAULT_INIT
:
1717 case KVM_S390_INT_PFAULT_DONE
:
1718 case KVM_S390_INT_VIRTIO
:
1719 case KVM_S390_INT_SERVICE
:
1720 target
= (void *) &inti
->ext
;
1721 source
= &uptr
->u
.ext
;
1722 size
= sizeof(inti
->ext
);
1724 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
1725 target
= (void *) &inti
->io
;
1726 source
= &uptr
->u
.io
;
1727 size
= sizeof(inti
->io
);
1730 target
= (void *) &inti
->mchk
;
1731 source
= &uptr
->u
.mchk
;
1732 size
= sizeof(inti
->mchk
);
1738 if (copy_from_user(target
, source
, size
))
1744 static int enqueue_floating_irq(struct kvm_device
*dev
,
1745 struct kvm_device_attr
*attr
)
1747 struct kvm_s390_interrupt_info
*inti
= NULL
;
1749 int len
= attr
->attr
;
1751 if (len
% sizeof(struct kvm_s390_irq
) != 0)
1753 else if (len
> KVM_S390_FLIC_MAX_BUFFER
)
1756 while (len
>= sizeof(struct kvm_s390_irq
)) {
1757 inti
= kzalloc(sizeof(*inti
), GFP_KERNEL
);
1761 r
= copy_irq_from_user(inti
, attr
->addr
);
1766 r
= __inject_vm(dev
->kvm
, inti
);
1771 len
-= sizeof(struct kvm_s390_irq
);
1772 attr
->addr
+= sizeof(struct kvm_s390_irq
);
1778 static struct s390_io_adapter
*get_io_adapter(struct kvm
*kvm
, unsigned int id
)
1780 if (id
>= MAX_S390_IO_ADAPTERS
)
1782 return kvm
->arch
.adapters
[id
];
1785 static int register_io_adapter(struct kvm_device
*dev
,
1786 struct kvm_device_attr
*attr
)
1788 struct s390_io_adapter
*adapter
;
1789 struct kvm_s390_io_adapter adapter_info
;
1791 if (copy_from_user(&adapter_info
,
1792 (void __user
*)attr
->addr
, sizeof(adapter_info
)))
1795 if ((adapter_info
.id
>= MAX_S390_IO_ADAPTERS
) ||
1796 (dev
->kvm
->arch
.adapters
[adapter_info
.id
] != NULL
))
1799 adapter
= kzalloc(sizeof(*adapter
), GFP_KERNEL
);
1803 INIT_LIST_HEAD(&adapter
->maps
);
1804 init_rwsem(&adapter
->maps_lock
);
1805 atomic_set(&adapter
->nr_maps
, 0);
1806 adapter
->id
= adapter_info
.id
;
1807 adapter
->isc
= adapter_info
.isc
;
1808 adapter
->maskable
= adapter_info
.maskable
;
1809 adapter
->masked
= false;
1810 adapter
->swap
= adapter_info
.swap
;
1811 dev
->kvm
->arch
.adapters
[adapter
->id
] = adapter
;
1816 int kvm_s390_mask_adapter(struct kvm
*kvm
, unsigned int id
, bool masked
)
1819 struct s390_io_adapter
*adapter
= get_io_adapter(kvm
, id
);
1821 if (!adapter
|| !adapter
->maskable
)
1823 ret
= adapter
->masked
;
1824 adapter
->masked
= masked
;
1828 static int kvm_s390_adapter_map(struct kvm
*kvm
, unsigned int id
, __u64 addr
)
1830 struct s390_io_adapter
*adapter
= get_io_adapter(kvm
, id
);
1831 struct s390_map_info
*map
;
1834 if (!adapter
|| !addr
)
1837 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
1842 INIT_LIST_HEAD(&map
->list
);
1843 map
->guest_addr
= addr
;
1844 map
->addr
= gmap_translate(kvm
->arch
.gmap
, addr
);
1845 if (map
->addr
== -EFAULT
) {
1849 ret
= get_user_pages_fast(map
->addr
, 1, 1, &map
->page
);
1853 down_write(&adapter
->maps_lock
);
1854 if (atomic_inc_return(&adapter
->nr_maps
) < MAX_S390_ADAPTER_MAPS
) {
1855 list_add_tail(&map
->list
, &adapter
->maps
);
1858 put_page(map
->page
);
1861 up_write(&adapter
->maps_lock
);
1868 static int kvm_s390_adapter_unmap(struct kvm
*kvm
, unsigned int id
, __u64 addr
)
1870 struct s390_io_adapter
*adapter
= get_io_adapter(kvm
, id
);
1871 struct s390_map_info
*map
, *tmp
;
1874 if (!adapter
|| !addr
)
1877 down_write(&adapter
->maps_lock
);
1878 list_for_each_entry_safe(map
, tmp
, &adapter
->maps
, list
) {
1879 if (map
->guest_addr
== addr
) {
1881 atomic_dec(&adapter
->nr_maps
);
1882 list_del(&map
->list
);
1883 put_page(map
->page
);
1888 up_write(&adapter
->maps_lock
);
1890 return found
? 0 : -EINVAL
;
1893 void kvm_s390_destroy_adapters(struct kvm
*kvm
)
1896 struct s390_map_info
*map
, *tmp
;
1898 for (i
= 0; i
< MAX_S390_IO_ADAPTERS
; i
++) {
1899 if (!kvm
->arch
.adapters
[i
])
1901 list_for_each_entry_safe(map
, tmp
,
1902 &kvm
->arch
.adapters
[i
]->maps
, list
) {
1903 list_del(&map
->list
);
1904 put_page(map
->page
);
1907 kfree(kvm
->arch
.adapters
[i
]);
1911 static int modify_io_adapter(struct kvm_device
*dev
,
1912 struct kvm_device_attr
*attr
)
1914 struct kvm_s390_io_adapter_req req
;
1915 struct s390_io_adapter
*adapter
;
1918 if (copy_from_user(&req
, (void __user
*)attr
->addr
, sizeof(req
)))
1921 adapter
= get_io_adapter(dev
->kvm
, req
.id
);
1925 case KVM_S390_IO_ADAPTER_MASK
:
1926 ret
= kvm_s390_mask_adapter(dev
->kvm
, req
.id
, req
.mask
);
1930 case KVM_S390_IO_ADAPTER_MAP
:
1931 ret
= kvm_s390_adapter_map(dev
->kvm
, req
.id
, req
.addr
);
1933 case KVM_S390_IO_ADAPTER_UNMAP
:
1934 ret
= kvm_s390_adapter_unmap(dev
->kvm
, req
.id
, req
.addr
);
1943 static int flic_set_attr(struct kvm_device
*dev
, struct kvm_device_attr
*attr
)
1947 struct kvm_vcpu
*vcpu
;
1949 switch (attr
->group
) {
1950 case KVM_DEV_FLIC_ENQUEUE
:
1951 r
= enqueue_floating_irq(dev
, attr
);
1953 case KVM_DEV_FLIC_CLEAR_IRQS
:
1954 kvm_s390_clear_float_irqs(dev
->kvm
);
1956 case KVM_DEV_FLIC_APF_ENABLE
:
1957 dev
->kvm
->arch
.gmap
->pfault_enabled
= 1;
1959 case KVM_DEV_FLIC_APF_DISABLE_WAIT
:
1960 dev
->kvm
->arch
.gmap
->pfault_enabled
= 0;
1962 * Make sure no async faults are in transition when
1963 * clearing the queues. So we don't need to worry
1964 * about late coming workers.
1966 synchronize_srcu(&dev
->kvm
->srcu
);
1967 kvm_for_each_vcpu(i
, vcpu
, dev
->kvm
)
1968 kvm_clear_async_pf_completion_queue(vcpu
);
1970 case KVM_DEV_FLIC_ADAPTER_REGISTER
:
1971 r
= register_io_adapter(dev
, attr
);
1973 case KVM_DEV_FLIC_ADAPTER_MODIFY
:
1974 r
= modify_io_adapter(dev
, attr
);
1983 static int flic_create(struct kvm_device
*dev
, u32 type
)
1987 if (dev
->kvm
->arch
.flic
)
1989 dev
->kvm
->arch
.flic
= dev
;
1993 static void flic_destroy(struct kvm_device
*dev
)
1995 dev
->kvm
->arch
.flic
= NULL
;
1999 /* s390 floating irq controller (flic) */
2000 struct kvm_device_ops kvm_flic_ops
= {
2002 .get_attr
= flic_get_attr
,
2003 .set_attr
= flic_set_attr
,
2004 .create
= flic_create
,
2005 .destroy
= flic_destroy
,
2008 static unsigned long get_ind_bit(__u64 addr
, unsigned long bit_nr
, bool swap
)
2012 bit
= bit_nr
+ (addr
% PAGE_SIZE
) * 8;
2014 return swap
? (bit
^ (BITS_PER_LONG
- 1)) : bit
;
2017 static struct s390_map_info
*get_map_info(struct s390_io_adapter
*adapter
,
2020 struct s390_map_info
*map
;
2025 list_for_each_entry(map
, &adapter
->maps
, list
) {
2026 if (map
->guest_addr
== addr
)
2032 static int adapter_indicators_set(struct kvm
*kvm
,
2033 struct s390_io_adapter
*adapter
,
2034 struct kvm_s390_adapter_int
*adapter_int
)
2037 int summary_set
, idx
;
2038 struct s390_map_info
*info
;
2041 info
= get_map_info(adapter
, adapter_int
->ind_addr
);
2044 map
= page_address(info
->page
);
2045 bit
= get_ind_bit(info
->addr
, adapter_int
->ind_offset
, adapter
->swap
);
2047 idx
= srcu_read_lock(&kvm
->srcu
);
2048 mark_page_dirty(kvm
, info
->guest_addr
>> PAGE_SHIFT
);
2049 set_page_dirty_lock(info
->page
);
2050 info
= get_map_info(adapter
, adapter_int
->summary_addr
);
2052 srcu_read_unlock(&kvm
->srcu
, idx
);
2055 map
= page_address(info
->page
);
2056 bit
= get_ind_bit(info
->addr
, adapter_int
->summary_offset
,
2058 summary_set
= test_and_set_bit(bit
, map
);
2059 mark_page_dirty(kvm
, info
->guest_addr
>> PAGE_SHIFT
);
2060 set_page_dirty_lock(info
->page
);
2061 srcu_read_unlock(&kvm
->srcu
, idx
);
2062 return summary_set
? 0 : 1;
2066 * < 0 - not injected due to error
2067 * = 0 - coalesced, summary indicator already active
2068 * > 0 - injected interrupt
2070 static int set_adapter_int(struct kvm_kernel_irq_routing_entry
*e
,
2071 struct kvm
*kvm
, int irq_source_id
, int level
,
2075 struct s390_io_adapter
*adapter
;
2077 /* We're only interested in the 0->1 transition. */
2080 adapter
= get_io_adapter(kvm
, e
->adapter
.adapter_id
);
2083 down_read(&adapter
->maps_lock
);
2084 ret
= adapter_indicators_set(kvm
, adapter
, &e
->adapter
);
2085 up_read(&adapter
->maps_lock
);
2086 if ((ret
> 0) && !adapter
->masked
) {
2087 struct kvm_s390_interrupt s390int
= {
2088 .type
= KVM_S390_INT_IO(1, 0, 0, 0),
2090 .parm64
= (adapter
->isc
<< 27) | 0x80000000,
2092 ret
= kvm_s390_inject_vm(kvm
, &s390int
);
2099 int kvm_set_routing_entry(struct kvm_kernel_irq_routing_entry
*e
,
2100 const struct kvm_irq_routing_entry
*ue
)
2105 case KVM_IRQ_ROUTING_S390_ADAPTER
:
2106 e
->set
= set_adapter_int
;
2107 e
->adapter
.summary_addr
= ue
->u
.adapter
.summary_addr
;
2108 e
->adapter
.ind_addr
= ue
->u
.adapter
.ind_addr
;
2109 e
->adapter
.summary_offset
= ue
->u
.adapter
.summary_offset
;
2110 e
->adapter
.ind_offset
= ue
->u
.adapter
.ind_offset
;
2111 e
->adapter
.adapter_id
= ue
->u
.adapter
.adapter_id
;
2121 int kvm_set_msi(struct kvm_kernel_irq_routing_entry
*e
, struct kvm
*kvm
,
2122 int irq_source_id
, int level
, bool line_status
)
2127 int kvm_s390_set_irq_state(struct kvm_vcpu
*vcpu
, void __user
*irqstate
, int len
)
2129 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
2130 struct kvm_s390_irq
*buf
;
2138 if (copy_from_user((void *) buf
, irqstate
, len
)) {
2144 * Don't allow setting the interrupt state
2145 * when there are already interrupts pending
2147 spin_lock(&li
->lock
);
2148 if (li
->pending_irqs
) {
2153 for (n
= 0; n
< len
/ sizeof(*buf
); n
++) {
2154 r
= do_inject_vcpu(vcpu
, &buf
[n
]);
2160 spin_unlock(&li
->lock
);
2167 static void store_local_irq(struct kvm_s390_local_interrupt
*li
,
2168 struct kvm_s390_irq
*irq
,
2169 unsigned long irq_type
)
2172 case IRQ_PEND_MCHK_EX
:
2173 case IRQ_PEND_MCHK_REP
:
2174 irq
->type
= KVM_S390_MCHK
;
2175 irq
->u
.mchk
= li
->irq
.mchk
;
2178 irq
->type
= KVM_S390_PROGRAM_INT
;
2179 irq
->u
.pgm
= li
->irq
.pgm
;
2181 case IRQ_PEND_PFAULT_INIT
:
2182 irq
->type
= KVM_S390_INT_PFAULT_INIT
;
2183 irq
->u
.ext
= li
->irq
.ext
;
2185 case IRQ_PEND_EXT_EXTERNAL
:
2186 irq
->type
= KVM_S390_INT_EXTERNAL_CALL
;
2187 irq
->u
.extcall
= li
->irq
.extcall
;
2189 case IRQ_PEND_EXT_CLOCK_COMP
:
2190 irq
->type
= KVM_S390_INT_CLOCK_COMP
;
2192 case IRQ_PEND_EXT_CPU_TIMER
:
2193 irq
->type
= KVM_S390_INT_CPU_TIMER
;
2195 case IRQ_PEND_SIGP_STOP
:
2196 irq
->type
= KVM_S390_SIGP_STOP
;
2197 irq
->u
.stop
= li
->irq
.stop
;
2199 case IRQ_PEND_RESTART
:
2200 irq
->type
= KVM_S390_RESTART
;
2202 case IRQ_PEND_SET_PREFIX
:
2203 irq
->type
= KVM_S390_SIGP_SET_PREFIX
;
2204 irq
->u
.prefix
= li
->irq
.prefix
;
2209 int kvm_s390_get_irq_state(struct kvm_vcpu
*vcpu
, __u8 __user
*buf
, int len
)
2211 uint8_t sigp_ctrl
= vcpu
->kvm
->arch
.sca
->cpu
[vcpu
->vcpu_id
].sigp_ctrl
;
2212 unsigned long sigp_emerg_pending
[BITS_TO_LONGS(KVM_MAX_VCPUS
)];
2213 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
2214 unsigned long pending_irqs
;
2215 struct kvm_s390_irq irq
;
2216 unsigned long irq_type
;
2220 spin_lock(&li
->lock
);
2221 pending_irqs
= li
->pending_irqs
;
2222 memcpy(&sigp_emerg_pending
, &li
->sigp_emerg_pending
,
2223 sizeof(sigp_emerg_pending
));
2224 spin_unlock(&li
->lock
);
2226 for_each_set_bit(irq_type
, &pending_irqs
, IRQ_PEND_COUNT
) {
2227 memset(&irq
, 0, sizeof(irq
));
2228 if (irq_type
== IRQ_PEND_EXT_EMERGENCY
)
2230 if (n
+ sizeof(irq
) > len
)
2232 store_local_irq(&vcpu
->arch
.local_int
, &irq
, irq_type
);
2233 if (copy_to_user(&buf
[n
], &irq
, sizeof(irq
)))
2238 if (test_bit(IRQ_PEND_EXT_EMERGENCY
, &pending_irqs
)) {
2239 for_each_set_bit(cpuaddr
, sigp_emerg_pending
, KVM_MAX_VCPUS
) {
2240 memset(&irq
, 0, sizeof(irq
));
2241 if (n
+ sizeof(irq
) > len
)
2243 irq
.type
= KVM_S390_INT_EMERGENCY
;
2244 irq
.u
.emerg
.code
= cpuaddr
;
2245 if (copy_to_user(&buf
[n
], &irq
, sizeof(irq
)))
2251 if ((sigp_ctrl
& SIGP_CTRL_C
) &&
2252 (atomic_read(&vcpu
->arch
.sie_block
->cpuflags
) &
2253 CPUSTAT_ECALL_PEND
)) {
2254 if (n
+ sizeof(irq
) > len
)
2256 memset(&irq
, 0, sizeof(irq
));
2257 irq
.type
= KVM_S390_INT_EXTERNAL_CALL
;
2258 irq
.u
.extcall
.code
= sigp_ctrl
& SIGP_CTRL_SCN_MASK
;
2259 if (copy_to_user(&buf
[n
], &irq
, sizeof(irq
)))