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