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