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