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