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