1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * handling privileged instructions 4 * 5 * Copyright IBM Corp. 2008, 2020 6 * 7 * Author(s): Carsten Otte <cotte@de.ibm.com> 8 * Christian Borntraeger <borntraeger@de.ibm.com> 9 */ 10 11 #include <linux/kvm.h> 12 #include <linux/gfp.h> 13 #include <linux/errno.h> 14 #include <linux/mm_types.h> 15 #include <linux/pgtable.h> 16 #include <linux/io.h> 17 #include <asm/asm-offsets.h> 18 #include <asm/facility.h> 19 #include <asm/current.h> 20 #include <asm/debug.h> 21 #include <asm/ebcdic.h> 22 #include <asm/sysinfo.h> 23 #include <asm/page-states.h> 24 #include <asm/ptrace.h> 25 #include <asm/sclp.h> 26 #include <asm/ap.h> 27 #include <asm/gmap_helpers.h> 28 #include "gaccess.h" 29 #include "kvm-s390.h" 30 #include "trace.h" 31 #include "gmap.h" 32 33 static int handle_ri(struct kvm_vcpu *vcpu) 34 { 35 vcpu->stat.instruction_ri++; 36 37 if (test_kvm_facility(vcpu->kvm, 64)) { 38 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (lazy)"); 39 vcpu->arch.sie_block->ecb3 |= ECB3_RI; 40 kvm_s390_retry_instr(vcpu); 41 return 0; 42 } else 43 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION); 44 } 45 46 int kvm_s390_handle_aa(struct kvm_vcpu *vcpu) 47 { 48 if ((vcpu->arch.sie_block->ipa & 0xf) <= 4) 49 return handle_ri(vcpu); 50 else 51 return -EOPNOTSUPP; 52 } 53 54 static int handle_gs(struct kvm_vcpu *vcpu) 55 { 56 vcpu->stat.instruction_gs++; 57 58 if (test_kvm_facility(vcpu->kvm, 133)) { 59 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (lazy)"); 60 preempt_disable(); 61 local_ctl_set_bit(2, CR2_GUARDED_STORAGE_BIT); 62 current->thread.gs_cb = (struct gs_cb *)&vcpu->run->s.regs.gscb; 63 restore_gs_cb(current->thread.gs_cb); 64 preempt_enable(); 65 vcpu->arch.sie_block->ecb |= ECB_GS; 66 vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 67 vcpu->arch.gs_enabled = 1; 68 kvm_s390_retry_instr(vcpu); 69 return 0; 70 } else 71 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION); 72 } 73 74 int kvm_s390_handle_e3(struct kvm_vcpu *vcpu) 75 { 76 int code = vcpu->arch.sie_block->ipb & 0xff; 77 78 if (code == 0x49 || code == 0x4d) 79 return handle_gs(vcpu); 80 else 81 return -EOPNOTSUPP; 82 } 83 /* Handle SCK (SET CLOCK) interception */ 84 static int handle_set_clock(struct kvm_vcpu *vcpu) 85 { 86 struct kvm_s390_vm_tod_clock gtod = { 0 }; 87 int rc; 88 u8 ar; 89 u64 op2; 90 91 vcpu->stat.instruction_sck++; 92 93 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 94 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 95 96 op2 = kvm_s390_get_base_disp_s(vcpu, &ar); 97 if (op2 & 7) /* Operand must be on a doubleword boundary */ 98 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 99 rc = read_guest(vcpu, op2, ar, >od.tod, sizeof(gtod.tod)); 100 if (rc) 101 return kvm_s390_inject_prog_cond(vcpu, rc); 102 103 VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", gtod.tod); 104 /* 105 * To set the TOD clock the kvm lock must be taken, but the vcpu lock 106 * is already held in handle_set_clock. The usual lock order is the 107 * opposite. As SCK is deprecated and should not be used in several 108 * cases, for example when the multiple epoch facility or TOD clock 109 * steering facility is installed (see Principles of Operation), a 110 * slow path can be used. If the lock can not be taken via try_lock, 111 * the instruction will be retried via -EAGAIN at a later point in 112 * time. 113 */ 114 if (!kvm_s390_try_set_tod_clock(vcpu->kvm, >od)) { 115 kvm_s390_retry_instr(vcpu); 116 return -EAGAIN; 117 } 118 119 kvm_s390_set_psw_cc(vcpu, 0); 120 return 0; 121 } 122 123 static int handle_set_prefix(struct kvm_vcpu *vcpu) 124 { 125 u64 operand2; 126 u32 address; 127 int rc; 128 u8 ar; 129 130 vcpu->stat.instruction_spx++; 131 132 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 133 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 134 135 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar); 136 137 /* must be word boundary */ 138 if (operand2 & 3) 139 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 140 141 /* get the value */ 142 rc = read_guest(vcpu, operand2, ar, &address, sizeof(address)); 143 if (rc) 144 return kvm_s390_inject_prog_cond(vcpu, rc); 145 146 address &= 0x7fffe000u; 147 148 /* 149 * Make sure the new value is valid memory. We only need to check the 150 * first page, since address is 8k aligned and memory pieces are always 151 * at least 1MB aligned and have at least a size of 1MB. 152 */ 153 if (!kvm_is_gpa_in_memslot(vcpu->kvm, address)) 154 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); 155 156 kvm_s390_set_prefix(vcpu, address); 157 trace_kvm_s390_handle_prefix(vcpu, 1, address); 158 return 0; 159 } 160 161 static int handle_store_prefix(struct kvm_vcpu *vcpu) 162 { 163 u64 operand2; 164 u32 address; 165 int rc; 166 u8 ar; 167 168 vcpu->stat.instruction_stpx++; 169 170 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 171 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 172 173 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar); 174 175 /* must be word boundary */ 176 if (operand2 & 3) 177 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 178 179 address = kvm_s390_get_prefix(vcpu); 180 181 /* get the value */ 182 rc = write_guest(vcpu, operand2, ar, &address, sizeof(address)); 183 if (rc) 184 return kvm_s390_inject_prog_cond(vcpu, rc); 185 186 VCPU_EVENT(vcpu, 3, "STPX: storing prefix 0x%x into 0x%llx", address, operand2); 187 trace_kvm_s390_handle_prefix(vcpu, 0, address); 188 return 0; 189 } 190 191 static int handle_store_cpu_address(struct kvm_vcpu *vcpu) 192 { 193 u16 vcpu_id = vcpu->vcpu_id; 194 u64 ga; 195 int rc; 196 u8 ar; 197 198 vcpu->stat.instruction_stap++; 199 200 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 201 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 202 203 ga = kvm_s390_get_base_disp_s(vcpu, &ar); 204 205 if (ga & 1) 206 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 207 208 rc = write_guest(vcpu, ga, ar, &vcpu_id, sizeof(vcpu_id)); 209 if (rc) 210 return kvm_s390_inject_prog_cond(vcpu, rc); 211 212 VCPU_EVENT(vcpu, 3, "STAP: storing cpu address (%u) to 0x%llx", vcpu_id, ga); 213 trace_kvm_s390_handle_stap(vcpu, ga); 214 return 0; 215 } 216 217 int kvm_s390_skey_check_enable(struct kvm_vcpu *vcpu) 218 { 219 int rc; 220 221 trace_kvm_s390_skey_related_inst(vcpu); 222 /* Already enabled? */ 223 if (vcpu->arch.skey_enabled) 224 return 0; 225 226 rc = gmap_enable_skeys(vcpu->arch.gmap); 227 VCPU_EVENT(vcpu, 3, "enabling storage keys for guest: %d", rc); 228 if (rc) 229 return rc; 230 231 if (kvm_s390_test_cpuflags(vcpu, CPUSTAT_KSS)) 232 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_KSS); 233 if (!vcpu->kvm->arch.use_skf) 234 vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE; 235 else 236 vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE); 237 vcpu->arch.skey_enabled = true; 238 return 0; 239 } 240 241 static int try_handle_skey(struct kvm_vcpu *vcpu) 242 { 243 int rc; 244 245 rc = kvm_s390_skey_check_enable(vcpu); 246 if (rc) 247 return rc; 248 if (vcpu->kvm->arch.use_skf) { 249 /* with storage-key facility, SIE interprets it for us */ 250 kvm_s390_retry_instr(vcpu); 251 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation"); 252 return -EAGAIN; 253 } 254 return 0; 255 } 256 257 static int handle_iske(struct kvm_vcpu *vcpu) 258 { 259 unsigned long gaddr; 260 int reg1, reg2; 261 union skey key; 262 int rc; 263 264 vcpu->stat.instruction_iske++; 265 266 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 267 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 268 269 rc = try_handle_skey(vcpu); 270 if (rc) 271 return rc != -EAGAIN ? rc : 0; 272 273 kvm_s390_get_regs_rre(vcpu, ®1, ®2); 274 275 gaddr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK; 276 gaddr = kvm_s390_logical_to_effective(vcpu, gaddr); 277 gaddr = kvm_s390_real_to_abs(vcpu, gaddr); 278 scoped_guard(read_lock, &vcpu->kvm->mmu_lock) 279 rc = dat_get_storage_key(vcpu->arch.gmap->asce, gpa_to_gfn(gaddr), &key); 280 if (rc > 0) 281 return kvm_s390_inject_program_int(vcpu, rc); 282 if (rc < 0) 283 return rc; 284 vcpu->run->s.regs.gprs[reg1] &= ~0xff; 285 vcpu->run->s.regs.gprs[reg1] |= key.skey; 286 return 0; 287 } 288 289 static int handle_rrbe(struct kvm_vcpu *vcpu) 290 { 291 unsigned long gaddr; 292 int reg1, reg2; 293 int rc; 294 295 vcpu->stat.instruction_rrbe++; 296 297 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 298 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 299 300 rc = try_handle_skey(vcpu); 301 if (rc) 302 return rc != -EAGAIN ? rc : 0; 303 304 kvm_s390_get_regs_rre(vcpu, ®1, ®2); 305 306 gaddr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK; 307 gaddr = kvm_s390_logical_to_effective(vcpu, gaddr); 308 gaddr = kvm_s390_real_to_abs(vcpu, gaddr); 309 scoped_guard(read_lock, &vcpu->kvm->mmu_lock) 310 rc = dat_reset_reference_bit(vcpu->arch.gmap->asce, gpa_to_gfn(gaddr)); 311 if (rc > 0) 312 return kvm_s390_inject_program_int(vcpu, rc); 313 if (rc < 0) 314 return rc; 315 kvm_s390_set_psw_cc(vcpu, rc); 316 return 0; 317 } 318 319 #define SSKE_NQ 0x8 320 #define SSKE_MR 0x4 321 #define SSKE_MC 0x2 322 #define SSKE_MB 0x1 323 static int handle_sske(struct kvm_vcpu *vcpu) 324 { 325 unsigned char m3 = vcpu->arch.sie_block->ipb >> 28; 326 unsigned long start, end; 327 union skey key, oldkey; 328 int reg1, reg2; 329 int rc; 330 331 vcpu->stat.instruction_sske++; 332 333 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 334 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 335 336 rc = try_handle_skey(vcpu); 337 if (rc) 338 return rc != -EAGAIN ? rc : 0; 339 340 if (!test_kvm_facility(vcpu->kvm, 8)) 341 m3 &= ~SSKE_MB; 342 if (!test_kvm_facility(vcpu->kvm, 10)) 343 m3 &= ~(SSKE_MC | SSKE_MR); 344 if (!test_kvm_facility(vcpu->kvm, 14)) 345 m3 &= ~SSKE_NQ; 346 347 kvm_s390_get_regs_rre(vcpu, ®1, ®2); 348 349 key.skey = vcpu->run->s.regs.gprs[reg1] & 0xfe; 350 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK; 351 start = kvm_s390_logical_to_effective(vcpu, start); 352 if (m3 & SSKE_MB) { 353 /* start already designates an absolute address */ 354 end = (start + _SEGMENT_SIZE) & ~(_SEGMENT_SIZE - 1); 355 } else { 356 start = kvm_s390_real_to_abs(vcpu, start); 357 end = start + PAGE_SIZE; 358 } 359 360 while (start != end) { 361 scoped_guard(read_lock, &vcpu->kvm->mmu_lock) { 362 rc = dat_cond_set_storage_key(vcpu->arch.mc, vcpu->arch.gmap->asce, 363 gpa_to_gfn(start), key, &oldkey, 364 m3 & SSKE_NQ, m3 & SSKE_MR, m3 & SSKE_MC); 365 } 366 if (rc > 1) 367 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); 368 if (rc == -ENOMEM) { 369 rc = kvm_s390_mmu_cache_topup(vcpu->arch.mc); 370 if (rc) 371 return rc; 372 continue; 373 } 374 if (rc < 0) 375 return rc; 376 start += PAGE_SIZE; 377 } 378 379 if (m3 & (SSKE_MC | SSKE_MR)) { 380 if (m3 & SSKE_MB) { 381 /* skey in reg1 is unpredictable */ 382 kvm_s390_set_psw_cc(vcpu, 3); 383 } else { 384 kvm_s390_set_psw_cc(vcpu, rc); 385 vcpu->run->s.regs.gprs[reg1] &= ~0xff00UL; 386 vcpu->run->s.regs.gprs[reg1] |= (u64)oldkey.skey << 8; 387 } 388 } 389 if (m3 & SSKE_MB) { 390 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT) 391 vcpu->run->s.regs.gprs[reg2] &= ~PAGE_MASK; 392 else 393 vcpu->run->s.regs.gprs[reg2] &= ~0xfffff000UL; 394 end = kvm_s390_logical_to_effective(vcpu, end); 395 vcpu->run->s.regs.gprs[reg2] |= end; 396 } 397 return 0; 398 } 399 400 static int handle_ipte_interlock(struct kvm_vcpu *vcpu) 401 { 402 vcpu->stat.instruction_ipte_interlock++; 403 if (psw_bits(vcpu->arch.sie_block->gpsw).pstate) 404 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 405 wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu->kvm)); 406 kvm_s390_retry_instr(vcpu); 407 VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation"); 408 return 0; 409 } 410 411 static int handle_test_block(struct kvm_vcpu *vcpu) 412 { 413 gpa_t addr; 414 int reg2; 415 416 vcpu->stat.instruction_tb++; 417 418 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 419 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 420 421 kvm_s390_get_regs_rre(vcpu, NULL, ®2); 422 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK; 423 addr = kvm_s390_logical_to_effective(vcpu, addr); 424 if (kvm_s390_check_low_addr_prot_real(vcpu, addr)) 425 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm); 426 addr = kvm_s390_real_to_abs(vcpu, addr); 427 428 if (!kvm_is_gpa_in_memslot(vcpu->kvm, addr)) 429 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); 430 /* 431 * We don't expect errors on modern systems, and do not care 432 * about storage keys (yet), so let's just clear the page. 433 */ 434 if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE)) 435 return -EFAULT; 436 kvm_s390_set_psw_cc(vcpu, 0); 437 vcpu->run->s.regs.gprs[0] = 0; 438 return 0; 439 } 440 441 static int handle_tpi(struct kvm_vcpu *vcpu) 442 { 443 struct kvm_s390_interrupt_info *inti; 444 unsigned long len; 445 u32 tpi_data[3]; 446 int rc; 447 u64 addr; 448 u8 ar; 449 450 vcpu->stat.instruction_tpi++; 451 452 addr = kvm_s390_get_base_disp_s(vcpu, &ar); 453 if (addr & 3) 454 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 455 456 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0); 457 if (!inti) { 458 kvm_s390_set_psw_cc(vcpu, 0); 459 return 0; 460 } 461 462 tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr; 463 tpi_data[1] = inti->io.io_int_parm; 464 tpi_data[2] = inti->io.io_int_word; 465 if (addr) { 466 /* 467 * Store the two-word I/O interruption code into the 468 * provided area. 469 */ 470 len = sizeof(tpi_data) - 4; 471 rc = write_guest(vcpu, addr, ar, &tpi_data, len); 472 if (rc) { 473 rc = kvm_s390_inject_prog_cond(vcpu, rc); 474 goto reinject_interrupt; 475 } 476 } else { 477 /* 478 * Store the three-word I/O interruption code into 479 * the appropriate lowcore area. 480 */ 481 len = sizeof(tpi_data); 482 if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) { 483 /* failed writes to the low core are not recoverable */ 484 rc = -EFAULT; 485 goto reinject_interrupt; 486 } 487 } 488 489 /* irq was successfully handed to the guest */ 490 kfree(inti); 491 kvm_s390_set_psw_cc(vcpu, 1); 492 return 0; 493 reinject_interrupt: 494 /* 495 * If we encounter a problem storing the interruption code, the 496 * instruction is suppressed from the guest's view: reinject the 497 * interrupt. 498 */ 499 if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) { 500 kfree(inti); 501 rc = -EFAULT; 502 } 503 /* don't set the cc, a pgm irq was injected or we drop to user space */ 504 return rc ? -EFAULT : 0; 505 } 506 507 static int handle_tsch(struct kvm_vcpu *vcpu) 508 { 509 struct kvm_s390_interrupt_info *inti = NULL; 510 const u64 isc_mask = 0xffUL << 24; /* all iscs set */ 511 512 vcpu->stat.instruction_tsch++; 513 514 /* a valid schid has at least one bit set */ 515 if (vcpu->run->s.regs.gprs[1]) 516 inti = kvm_s390_get_io_int(vcpu->kvm, isc_mask, 517 vcpu->run->s.regs.gprs[1]); 518 519 /* 520 * Prepare exit to userspace. 521 * We indicate whether we dequeued a pending I/O interrupt 522 * so that userspace can re-inject it if the instruction gets 523 * a program check. While this may re-order the pending I/O 524 * interrupts, this is no problem since the priority is kept 525 * intact. 526 */ 527 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH; 528 vcpu->run->s390_tsch.dequeued = !!inti; 529 if (inti) { 530 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id; 531 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr; 532 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm; 533 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word; 534 } 535 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb; 536 kfree(inti); 537 return -EREMOTE; 538 } 539 540 static int handle_io_inst(struct kvm_vcpu *vcpu) 541 { 542 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction"); 543 544 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 545 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 546 547 if (vcpu->kvm->arch.css_support) { 548 /* 549 * Most I/O instructions will be handled by userspace. 550 * Exceptions are tpi and the interrupt portion of tsch. 551 */ 552 if (vcpu->arch.sie_block->ipa == 0xb236) 553 return handle_tpi(vcpu); 554 if (vcpu->arch.sie_block->ipa == 0xb235) 555 return handle_tsch(vcpu); 556 /* Handle in userspace. */ 557 vcpu->stat.instruction_io_other++; 558 return -EOPNOTSUPP; 559 } else { 560 /* 561 * Set condition code 3 to stop the guest from issuing channel 562 * I/O instructions. 563 */ 564 kvm_s390_set_psw_cc(vcpu, 3); 565 return 0; 566 } 567 } 568 569 #if IS_ENABLED(CONFIG_VFIO_AP) 570 bool kvm_s390_is_gpa_in_memslot(struct kvm *kvm, gpa_t gpa) 571 { 572 return kvm_is_gpa_in_memslot(kvm, gpa); 573 } 574 EXPORT_SYMBOL_FOR_MODULES(kvm_s390_is_gpa_in_memslot, "vfio_ap"); 575 #endif 576 577 /* 578 * handle_pqap: Handling pqap interception 579 * @vcpu: the vcpu having issue the pqap instruction 580 * 581 * We now support PQAP/AQIC instructions and we need to correctly 582 * answer the guest even if no dedicated driver's hook is available. 583 * 584 * The intercepting code calls a dedicated callback for this instruction 585 * if a driver did register one in the CRYPTO satellite of the 586 * SIE block. 587 * 588 * If no callback is available, the queues are not available, return this 589 * response code to the caller and set CC to 3. 590 * Else return the response code returned by the callback. 591 */ 592 static int handle_pqap(struct kvm_vcpu *vcpu) 593 { 594 struct ap_queue_status status = {}; 595 crypto_hook pqap_hook; 596 unsigned long reg0; 597 int ret; 598 uint8_t fc; 599 600 /* Verify that the AP instruction are available */ 601 if (!ap_instructions_available()) 602 return -EOPNOTSUPP; 603 /* Verify that the guest is allowed to use AP instructions */ 604 if (!(vcpu->arch.sie_block->eca & ECA_APIE)) 605 return -EOPNOTSUPP; 606 /* 607 * The only possibly intercepted functions when AP instructions are 608 * available for the guest are AQIC and TAPQ with the t bit set 609 * since we do not set IC.3 (FIII) we currently will only intercept 610 * the AQIC function code. 611 * Note: running nested under z/VM can result in intercepts for other 612 * function codes, e.g. PQAP(QCI). We do not support this and bail out. 613 */ 614 reg0 = vcpu->run->s.regs.gprs[0]; 615 fc = (reg0 >> 24) & 0xff; 616 if (fc != 0x03) 617 return -EOPNOTSUPP; 618 619 /* PQAP instruction is allowed for guest kernel only */ 620 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 621 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 622 623 /* Common PQAP instruction specification exceptions */ 624 /* bits 41-47 must all be zeros */ 625 if (reg0 & 0x007f0000UL) 626 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 627 /* APFT not install and T bit set */ 628 if (!test_kvm_facility(vcpu->kvm, 15) && (reg0 & 0x00800000UL)) 629 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 630 /* APXA not installed and APID greater 64 or APQI greater 16 */ 631 if (!(vcpu->kvm->arch.crypto.crycbd & 0x02) && (reg0 & 0x0000c0f0UL)) 632 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 633 634 /* AQIC function code specific exception */ 635 /* facility 65 not present for AQIC function code */ 636 if (!test_kvm_facility(vcpu->kvm, 65)) 637 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 638 639 /* 640 * If the hook callback is registered, there will be a pointer to the 641 * hook function pointer in the kvm_s390_crypto structure. Lock the 642 * owner, retrieve the hook function pointer and call the hook. 643 */ 644 down_read(&vcpu->kvm->arch.crypto.pqap_hook_rwsem); 645 if (vcpu->kvm->arch.crypto.pqap_hook) { 646 pqap_hook = *vcpu->kvm->arch.crypto.pqap_hook; 647 ret = pqap_hook(vcpu); 648 if (!ret) { 649 if (vcpu->run->s.regs.gprs[1] & 0x00ff0000) 650 kvm_s390_set_psw_cc(vcpu, 3); 651 else 652 kvm_s390_set_psw_cc(vcpu, 0); 653 } 654 up_read(&vcpu->kvm->arch.crypto.pqap_hook_rwsem); 655 return ret; 656 } 657 up_read(&vcpu->kvm->arch.crypto.pqap_hook_rwsem); 658 /* 659 * A vfio_driver must register a hook. 660 * No hook means no driver to enable the SIE CRYCB and no queues. 661 * We send this response to the guest. 662 */ 663 status.response_code = 0x01; 664 memcpy(&vcpu->run->s.regs.gprs[1], &status, sizeof(status)); 665 kvm_s390_set_psw_cc(vcpu, 3); 666 return 0; 667 } 668 669 static int handle_stfl(struct kvm_vcpu *vcpu) 670 { 671 int rc; 672 unsigned int fac; 673 674 vcpu->stat.instruction_stfl++; 675 676 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 677 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 678 679 /* 680 * We need to shift the lower 32 facility bits (bit 0-31) from a u64 681 * into a u32 memory representation. They will remain bits 0-31. 682 */ 683 fac = *vcpu->kvm->arch.model.fac_list >> 32; 684 rc = write_guest_lc(vcpu, offsetof(struct lowcore, stfl_fac_list), 685 &fac, sizeof(fac)); 686 if (rc) 687 return rc; 688 VCPU_EVENT(vcpu, 3, "STFL: store facility list 0x%x", fac); 689 trace_kvm_s390_handle_stfl(vcpu, fac); 690 return 0; 691 } 692 693 #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA) 694 #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL 695 #define PSW_ADDR_24 0x0000000000ffffffUL 696 #define PSW_ADDR_31 0x000000007fffffffUL 697 698 int is_valid_psw(psw_t *psw) 699 { 700 if (psw->mask & PSW_MASK_UNASSIGNED) 701 return 0; 702 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) { 703 if (psw->addr & ~PSW_ADDR_31) 704 return 0; 705 } 706 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24)) 707 return 0; 708 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA) 709 return 0; 710 if (psw->addr & 1) 711 return 0; 712 return 1; 713 } 714 715 int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu) 716 { 717 psw_t *gpsw = &vcpu->arch.sie_block->gpsw; 718 psw32_t new_psw; 719 u64 addr, iaddr; 720 int rc; 721 u8 ar; 722 723 vcpu->stat.instruction_lpsw++; 724 725 iaddr = gpsw->addr - kvm_s390_get_ilen(vcpu); 726 if (gpsw->mask & PSW_MASK_PSTATE) 727 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 728 729 addr = kvm_s390_get_base_disp_s(vcpu, &ar); 730 if (addr & 7) 731 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 732 733 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw)); 734 if (rc) 735 return kvm_s390_inject_prog_cond(vcpu, rc); 736 if (!(new_psw.mask & PSW32_MASK_BASE)) 737 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 738 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32; 739 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE; 740 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE; 741 if (!is_valid_psw(gpsw)) 742 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 743 vcpu->arch.sie_block->gbea = iaddr; 744 return 0; 745 } 746 747 static int handle_lpswe(struct kvm_vcpu *vcpu) 748 { 749 psw_t new_psw; 750 u64 addr, iaddr; 751 int rc; 752 u8 ar; 753 754 vcpu->stat.instruction_lpswe++; 755 756 iaddr = vcpu->arch.sie_block->gpsw.addr - kvm_s390_get_ilen(vcpu); 757 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 758 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 759 760 addr = kvm_s390_get_base_disp_s(vcpu, &ar); 761 if (addr & 7) 762 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 763 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw)); 764 if (rc) 765 return kvm_s390_inject_prog_cond(vcpu, rc); 766 vcpu->arch.sie_block->gpsw = new_psw; 767 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw)) 768 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 769 vcpu->arch.sie_block->gbea = iaddr; 770 return 0; 771 } 772 773 static int handle_lpswey(struct kvm_vcpu *vcpu) 774 { 775 psw_t new_psw; 776 u64 addr; 777 int rc; 778 u8 ar; 779 780 vcpu->stat.instruction_lpswey++; 781 782 if (!test_kvm_facility(vcpu->kvm, 193)) 783 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION); 784 785 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 786 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 787 788 addr = kvm_s390_get_base_disp_siy(vcpu, &ar); 789 if (addr & 7) 790 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 791 792 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw)); 793 if (rc) 794 return kvm_s390_inject_prog_cond(vcpu, rc); 795 796 vcpu->arch.sie_block->gpsw = new_psw; 797 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw)) 798 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 799 800 return 0; 801 } 802 803 static int handle_stidp(struct kvm_vcpu *vcpu) 804 { 805 u64 stidp_data = vcpu->kvm->arch.model.cpuid; 806 u64 operand2; 807 int rc; 808 u8 ar; 809 810 vcpu->stat.instruction_stidp++; 811 812 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 813 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 814 815 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar); 816 817 if (operand2 & 7) 818 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 819 820 rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data)); 821 if (rc) 822 return kvm_s390_inject_prog_cond(vcpu, rc); 823 824 VCPU_EVENT(vcpu, 3, "STIDP: store cpu id 0x%llx", stidp_data); 825 return 0; 826 } 827 828 static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem) 829 { 830 int cpus = 0; 831 int n; 832 833 cpus = atomic_read(&vcpu->kvm->online_vcpus); 834 835 /* deal with other level 3 hypervisors */ 836 if (stsi(mem, 3, 2, 2)) 837 mem->count = 0; 838 if (mem->count < 8) 839 mem->count++; 840 for (n = mem->count - 1; n > 0 ; n--) 841 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0])); 842 843 memset(&mem->vm[0], 0, sizeof(mem->vm[0])); 844 mem->vm[0].cpus_total = cpus; 845 mem->vm[0].cpus_configured = cpus; 846 mem->vm[0].cpus_standby = 0; 847 mem->vm[0].cpus_reserved = 0; 848 mem->vm[0].caf = 1000; 849 memcpy(mem->vm[0].name, "KVMguest", 8); 850 ASCEBC(mem->vm[0].name, 8); 851 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16); 852 ASCEBC(mem->vm[0].cpi, 16); 853 } 854 855 static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, u8 ar, 856 u8 fc, u8 sel1, u16 sel2) 857 { 858 vcpu->run->exit_reason = KVM_EXIT_S390_STSI; 859 vcpu->run->s390_stsi.addr = addr; 860 vcpu->run->s390_stsi.ar = ar; 861 vcpu->run->s390_stsi.fc = fc; 862 vcpu->run->s390_stsi.sel1 = sel1; 863 vcpu->run->s390_stsi.sel2 = sel2; 864 } 865 866 static int handle_stsi(struct kvm_vcpu *vcpu) 867 { 868 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28; 869 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff; 870 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff; 871 unsigned long mem = 0; 872 u64 operand2; 873 int rc = 0; 874 u8 ar; 875 876 vcpu->stat.instruction_stsi++; 877 VCPU_EVENT(vcpu, 3, "STSI: fc: %u sel1: %u sel2: %u", fc, sel1, sel2); 878 879 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 880 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 881 882 /* Bailout forbidden function codes */ 883 if (fc > 3 && fc != 15) 884 goto out_no_data; 885 886 /* 887 * fc 15 is provided only with 888 * - PTF/CPU topology support through facility 15 889 * - KVM_CAP_S390_USER_STSI 890 */ 891 if (fc == 15 && (!test_kvm_facility(vcpu->kvm, 11) || 892 !vcpu->kvm->arch.user_stsi)) 893 goto out_no_data; 894 895 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00 896 || vcpu->run->s.regs.gprs[1] & 0xffff0000) 897 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 898 899 if (fc == 0) { 900 vcpu->run->s.regs.gprs[0] = 3 << 28; 901 kvm_s390_set_psw_cc(vcpu, 0); 902 return 0; 903 } 904 905 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar); 906 907 if (!kvm_s390_pv_cpu_is_protected(vcpu) && (operand2 & 0xfff)) 908 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 909 910 switch (fc) { 911 case 1: /* same handling for 1 and 2 */ 912 case 2: 913 mem = get_zeroed_page(GFP_KERNEL_ACCOUNT); 914 if (!mem) 915 goto out_no_data; 916 if (stsi((void *) mem, fc, sel1, sel2)) 917 goto out_no_data; 918 break; 919 case 3: 920 if (sel1 != 2 || sel2 != 2) 921 goto out_no_data; 922 mem = get_zeroed_page(GFP_KERNEL_ACCOUNT); 923 if (!mem) 924 goto out_no_data; 925 handle_stsi_3_2_2(vcpu, (void *) mem); 926 break; 927 case 15: /* fc 15 is fully handled in userspace */ 928 insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2); 929 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2); 930 return -EREMOTE; 931 } 932 if (kvm_s390_pv_cpu_is_protected(vcpu)) { 933 memcpy(sida_addr(vcpu->arch.sie_block), (void *)mem, PAGE_SIZE); 934 rc = 0; 935 } else { 936 rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE); 937 } 938 if (rc) { 939 rc = kvm_s390_inject_prog_cond(vcpu, rc); 940 goto out; 941 } 942 if (vcpu->kvm->arch.user_stsi) { 943 insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2); 944 rc = -EREMOTE; 945 } 946 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2); 947 free_page(mem); 948 kvm_s390_set_psw_cc(vcpu, 0); 949 vcpu->run->s.regs.gprs[0] = 0; 950 return rc; 951 out_no_data: 952 kvm_s390_set_psw_cc(vcpu, 3); 953 out: 954 free_page(mem); 955 return rc; 956 } 957 958 int kvm_s390_handle_b2(struct kvm_vcpu *vcpu) 959 { 960 switch (vcpu->arch.sie_block->ipa & 0x00ff) { 961 case 0x02: 962 return handle_stidp(vcpu); 963 case 0x04: 964 return handle_set_clock(vcpu); 965 case 0x10: 966 return handle_set_prefix(vcpu); 967 case 0x11: 968 return handle_store_prefix(vcpu); 969 case 0x12: 970 return handle_store_cpu_address(vcpu); 971 case 0x14: 972 return kvm_s390_handle_vsie(vcpu); 973 case 0x21: 974 case 0x50: 975 return handle_ipte_interlock(vcpu); 976 case 0x29: 977 return handle_iske(vcpu); 978 case 0x2a: 979 return handle_rrbe(vcpu); 980 case 0x2b: 981 return handle_sske(vcpu); 982 case 0x2c: 983 return handle_test_block(vcpu); 984 case 0x30: 985 case 0x31: 986 case 0x32: 987 case 0x33: 988 case 0x34: 989 case 0x35: 990 case 0x36: 991 case 0x37: 992 case 0x38: 993 case 0x39: 994 case 0x3a: 995 case 0x3b: 996 case 0x3c: 997 case 0x5f: 998 case 0x74: 999 case 0x76: 1000 return handle_io_inst(vcpu); 1001 case 0x56: 1002 return handle_sthyi(vcpu); 1003 case 0x7d: 1004 return handle_stsi(vcpu); 1005 case 0xaf: 1006 return handle_pqap(vcpu); 1007 case 0xb1: 1008 return handle_stfl(vcpu); 1009 case 0xb2: 1010 return handle_lpswe(vcpu); 1011 default: 1012 return -EOPNOTSUPP; 1013 } 1014 } 1015 1016 static int handle_epsw(struct kvm_vcpu *vcpu) 1017 { 1018 int reg1, reg2; 1019 1020 vcpu->stat.instruction_epsw++; 1021 1022 kvm_s390_get_regs_rre(vcpu, ®1, ®2); 1023 1024 /* This basically extracts the mask half of the psw. */ 1025 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL; 1026 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32; 1027 if (reg2) { 1028 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL; 1029 vcpu->run->s.regs.gprs[reg2] |= 1030 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL; 1031 } 1032 return 0; 1033 } 1034 1035 #define PFMF_RESERVED 0xfffc0101UL 1036 #define PFMF_SK 0x00020000UL 1037 #define PFMF_CF 0x00010000UL 1038 #define PFMF_UI 0x00008000UL 1039 #define PFMF_FSC 0x00007000UL 1040 #define PFMF_NQ 0x00000800UL 1041 #define PFMF_MR 0x00000400UL 1042 #define PFMF_MC 0x00000200UL 1043 #define PFMF_KEY 0x000000feUL 1044 1045 static int handle_pfmf(struct kvm_vcpu *vcpu) 1046 { 1047 bool mr = false, mc = false, nq; 1048 int reg1, reg2; 1049 unsigned long start, end; 1050 union skey key; 1051 1052 vcpu->stat.instruction_pfmf++; 1053 1054 kvm_s390_get_regs_rre(vcpu, ®1, ®2); 1055 1056 if (!test_kvm_facility(vcpu->kvm, 8)) 1057 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION); 1058 1059 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 1060 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 1061 1062 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED) 1063 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 1064 1065 /* Only provide non-quiescing support if enabled for the guest */ 1066 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ && 1067 !test_kvm_facility(vcpu->kvm, 14)) 1068 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 1069 1070 /* Only provide conditional-SSKE support if enabled for the guest */ 1071 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK && 1072 test_kvm_facility(vcpu->kvm, 10)) { 1073 mr = vcpu->run->s.regs.gprs[reg1] & PFMF_MR; 1074 mc = vcpu->run->s.regs.gprs[reg1] & PFMF_MC; 1075 } 1076 1077 nq = vcpu->run->s.regs.gprs[reg1] & PFMF_NQ; 1078 key.skey = vcpu->run->s.regs.gprs[reg1] & PFMF_KEY; 1079 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK; 1080 start = kvm_s390_logical_to_effective(vcpu, start); 1081 1082 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) { 1083 if (kvm_s390_check_low_addr_prot_real(vcpu, start)) 1084 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm); 1085 } 1086 1087 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) { 1088 case 0x00000000: 1089 /* only 4k frames specify a real address */ 1090 start = kvm_s390_real_to_abs(vcpu, start); 1091 end = (start + PAGE_SIZE) & ~(PAGE_SIZE - 1); 1092 break; 1093 case 0x00001000: 1094 end = (start + _SEGMENT_SIZE) & ~(_SEGMENT_SIZE - 1); 1095 break; 1096 case 0x00002000: 1097 /* only support 2G frame size if EDAT2 is available and we are 1098 not in 24-bit addressing mode */ 1099 if (!test_kvm_facility(vcpu->kvm, 78) || 1100 psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_24BIT) 1101 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 1102 end = (start + _REGION3_SIZE) & ~(_REGION3_SIZE - 1); 1103 break; 1104 default: 1105 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 1106 } 1107 1108 while (start != end) { 1109 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) { 1110 if (kvm_clear_guest(vcpu->kvm, start, PAGE_SIZE)) 1111 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); 1112 } 1113 1114 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) { 1115 int rc = kvm_s390_skey_check_enable(vcpu); 1116 1117 if (rc) 1118 return rc; 1119 scoped_guard(read_lock, &vcpu->kvm->mmu_lock) { 1120 rc = dat_cond_set_storage_key(vcpu->arch.mc, vcpu->arch.gmap->asce, 1121 gpa_to_gfn(start), key, 1122 NULL, nq, mr, mc); 1123 } 1124 if (rc > 1) 1125 return kvm_s390_inject_program_int(vcpu, rc); 1126 if (rc == -ENOMEM) { 1127 rc = kvm_s390_mmu_cache_topup(vcpu->arch.mc); 1128 if (rc) 1129 return rc; 1130 continue; 1131 } 1132 if (rc < 0) 1133 return rc; 1134 } 1135 start += PAGE_SIZE; 1136 } 1137 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) { 1138 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT) { 1139 vcpu->run->s.regs.gprs[reg2] = end; 1140 } else { 1141 vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL; 1142 end = kvm_s390_logical_to_effective(vcpu, end); 1143 vcpu->run->s.regs.gprs[reg2] |= end; 1144 } 1145 } 1146 return 0; 1147 } 1148 1149 /* 1150 * Must be called with relevant read locks held (kvm->mm->mmap_lock, kvm->srcu) 1151 */ 1152 static inline int __do_essa(struct kvm_vcpu *vcpu, const int orc) 1153 { 1154 int r1, r2, nappended, entries; 1155 union essa_state state; 1156 unsigned long *cbrlo; 1157 unsigned long gfn; 1158 bool dirtied; 1159 1160 /* 1161 * We don't need to set SD.FPF.SK to 1 here, because if we have a 1162 * machine check here we either handle it or crash 1163 */ 1164 1165 kvm_s390_get_regs_rre(vcpu, &r1, &r2); 1166 gfn = vcpu->run->s.regs.gprs[r2] >> PAGE_SHIFT; 1167 entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3; 1168 1169 nappended = dat_perform_essa(vcpu->arch.gmap->asce, gfn, orc, &state, &dirtied); 1170 vcpu->run->s.regs.gprs[r1] = state.val; 1171 if (nappended < 0) 1172 return 0; 1173 /* 1174 * It is possible that all the normal 511 slots were full, in which case 1175 * we will now write in the 512th slot, which is reserved for host use. 1176 * In both cases we let the normal essa handling code process all the 1177 * slots, including the reserved one, if needed. 1178 */ 1179 if (nappended > 0) { 1180 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo & PAGE_MASK); 1181 cbrlo[entries] = gfn << PAGE_SHIFT; 1182 } 1183 1184 if (dirtied) 1185 atomic64_inc(&vcpu->kvm->arch.cmma_dirty_pages); 1186 1187 return nappended; 1188 } 1189 1190 static void _essa_clear_cbrl(struct kvm_vcpu *vcpu, unsigned long *cbrl, int len) 1191 { 1192 union crste *crstep; 1193 union pgste pgste; 1194 union pte *ptep; 1195 hva_t hva; 1196 int i; 1197 1198 lockdep_assert_held(&vcpu->kvm->mmu_lock); 1199 1200 for (i = 0; i < len; i++) { 1201 if (dat_entry_walk(NULL, gpa_to_gfn(cbrl[i]), vcpu->arch.gmap->asce, 1202 0, TABLE_TYPE_PAGE_TABLE, &crstep, &ptep)) 1203 continue; 1204 if (!ptep || ptep->s.pr) 1205 continue; 1206 pgste = pgste_get_lock(ptep); 1207 if (pgste.usage == PGSTE_GPS_USAGE_UNUSED || pgste.zero) { 1208 hva = gpa_to_hva(vcpu->kvm, cbrl[i]); 1209 if (!kvm_is_error_hva(hva)) 1210 gmap_helper_zap_one_page(vcpu->kvm->mm, hva); 1211 } 1212 pgste_set_unlock(ptep, pgste); 1213 } 1214 } 1215 1216 static int handle_essa(struct kvm_vcpu *vcpu) 1217 { 1218 lockdep_assert_held(&vcpu->kvm->srcu); 1219 1220 /* entries expected to be 1FF */ 1221 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3; 1222 unsigned long *cbrlo; 1223 int i, orc; 1224 1225 VCPU_EVENT(vcpu, 4, "ESSA: release %d pages", entries); 1226 vcpu->stat.instruction_essa++; 1227 if (!vcpu->kvm->arch.use_cmma) 1228 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION); 1229 1230 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 1231 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 1232 /* Check for invalid operation request code */ 1233 orc = (vcpu->arch.sie_block->ipb & 0xf0000000) >> 28; 1234 /* ORCs 0-6 are always valid */ 1235 if (orc > (test_kvm_facility(vcpu->kvm, 147) ? ESSA_SET_STABLE_NODAT 1236 : ESSA_SET_STABLE_IF_RESIDENT)) 1237 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 1238 1239 if (!READ_ONCE(vcpu->kvm->arch.migration_mode)) { 1240 /* 1241 * CMMA is enabled in the KVM settings, but is disabled in 1242 * the SIE block and in the mm_context, and we are not doing 1243 * a migration. Enable CMMA in the mm_context. 1244 * Since we need to take a write lock to write to the context 1245 * to avoid races with storage keys handling, we check if the 1246 * value really needs to be written to; if the value is 1247 * already correct, we do nothing and avoid the lock. 1248 */ 1249 set_bit(GMAP_FLAG_USES_CMM, &vcpu->arch.gmap->flags); 1250 /* 1251 * If we are here, we are supposed to have CMMA enabled in 1252 * the SIE block. Enabling CMMA works on a per-CPU basis, 1253 * while the context use_cmma flag is per process. 1254 * It's possible that the context flag is enabled and the 1255 * SIE flag is not, so we set the flag always; if it was 1256 * already set, nothing changes, otherwise we enable it 1257 * on this CPU too. 1258 */ 1259 vcpu->arch.sie_block->ecb2 |= ECB2_CMMA; 1260 /* Retry the ESSA instruction */ 1261 kvm_s390_retry_instr(vcpu); 1262 } else { 1263 scoped_guard(read_lock, &vcpu->kvm->mmu_lock) 1264 i = __do_essa(vcpu, orc); 1265 if (i < 0) 1266 return i; 1267 /* Account for the possible extra cbrl entry */ 1268 entries += i; 1269 } 1270 /* reset nceo */ 1271 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; 1272 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo); 1273 1274 mmap_read_lock(vcpu->kvm->mm); 1275 scoped_guard(read_lock, &vcpu->kvm->mmu_lock) 1276 _essa_clear_cbrl(vcpu, cbrlo, entries); 1277 mmap_read_unlock(vcpu->kvm->mm); 1278 1279 return 0; 1280 } 1281 1282 int kvm_s390_handle_b9(struct kvm_vcpu *vcpu) 1283 { 1284 switch (vcpu->arch.sie_block->ipa & 0x00ff) { 1285 case 0x8a: 1286 case 0x8e: 1287 case 0x8f: 1288 return handle_ipte_interlock(vcpu); 1289 case 0x8d: 1290 return handle_epsw(vcpu); 1291 case 0xab: 1292 return handle_essa(vcpu); 1293 case 0xaf: 1294 return handle_pfmf(vcpu); 1295 default: 1296 return -EOPNOTSUPP; 1297 } 1298 } 1299 1300 int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu) 1301 { 1302 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4; 1303 int reg3 = vcpu->arch.sie_block->ipa & 0x000f; 1304 int reg, rc, nr_regs; 1305 u32 ctl_array[16]; 1306 u64 ga; 1307 u8 ar; 1308 1309 vcpu->stat.instruction_lctl++; 1310 1311 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 1312 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 1313 1314 ga = kvm_s390_get_base_disp_rs(vcpu, &ar); 1315 1316 if (ga & 3) 1317 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 1318 1319 VCPU_EVENT(vcpu, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga); 1320 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga); 1321 1322 nr_regs = ((reg3 - reg1) & 0xf) + 1; 1323 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32)); 1324 if (rc) 1325 return kvm_s390_inject_prog_cond(vcpu, rc); 1326 reg = reg1; 1327 nr_regs = 0; 1328 do { 1329 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul; 1330 vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++]; 1331 if (reg == reg3) 1332 break; 1333 reg = (reg + 1) % 16; 1334 } while (1); 1335 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 1336 return 0; 1337 } 1338 1339 int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu) 1340 { 1341 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4; 1342 int reg3 = vcpu->arch.sie_block->ipa & 0x000f; 1343 int reg, rc, nr_regs; 1344 u32 ctl_array[16]; 1345 u64 ga; 1346 u8 ar; 1347 1348 vcpu->stat.instruction_stctl++; 1349 1350 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 1351 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 1352 1353 ga = kvm_s390_get_base_disp_rs(vcpu, &ar); 1354 1355 if (ga & 3) 1356 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 1357 1358 VCPU_EVENT(vcpu, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga); 1359 trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga); 1360 1361 reg = reg1; 1362 nr_regs = 0; 1363 do { 1364 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg]; 1365 if (reg == reg3) 1366 break; 1367 reg = (reg + 1) % 16; 1368 } while (1); 1369 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32)); 1370 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0; 1371 } 1372 1373 static int handle_lctlg(struct kvm_vcpu *vcpu) 1374 { 1375 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4; 1376 int reg3 = vcpu->arch.sie_block->ipa & 0x000f; 1377 int reg, rc, nr_regs; 1378 u64 ctl_array[16]; 1379 u64 ga; 1380 u8 ar; 1381 1382 vcpu->stat.instruction_lctlg++; 1383 1384 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 1385 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 1386 1387 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar); 1388 1389 if (ga & 7) 1390 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 1391 1392 VCPU_EVENT(vcpu, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga); 1393 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga); 1394 1395 nr_regs = ((reg3 - reg1) & 0xf) + 1; 1396 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64)); 1397 if (rc) 1398 return kvm_s390_inject_prog_cond(vcpu, rc); 1399 reg = reg1; 1400 nr_regs = 0; 1401 do { 1402 vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++]; 1403 if (reg == reg3) 1404 break; 1405 reg = (reg + 1) % 16; 1406 } while (1); 1407 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 1408 return 0; 1409 } 1410 1411 static int handle_stctg(struct kvm_vcpu *vcpu) 1412 { 1413 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4; 1414 int reg3 = vcpu->arch.sie_block->ipa & 0x000f; 1415 int reg, rc, nr_regs; 1416 u64 ctl_array[16]; 1417 u64 ga; 1418 u8 ar; 1419 1420 vcpu->stat.instruction_stctg++; 1421 1422 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 1423 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 1424 1425 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar); 1426 1427 if (ga & 7) 1428 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 1429 1430 VCPU_EVENT(vcpu, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga); 1431 trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga); 1432 1433 reg = reg1; 1434 nr_regs = 0; 1435 do { 1436 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg]; 1437 if (reg == reg3) 1438 break; 1439 reg = (reg + 1) % 16; 1440 } while (1); 1441 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64)); 1442 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0; 1443 } 1444 1445 int kvm_s390_handle_eb(struct kvm_vcpu *vcpu) 1446 { 1447 switch (vcpu->arch.sie_block->ipb & 0x000000ff) { 1448 case 0x25: 1449 return handle_stctg(vcpu); 1450 case 0x2f: 1451 return handle_lctlg(vcpu); 1452 case 0x60: 1453 case 0x61: 1454 case 0x62: 1455 return handle_ri(vcpu); 1456 case 0x71: 1457 return handle_lpswey(vcpu); 1458 default: 1459 return -EOPNOTSUPP; 1460 } 1461 } 1462 1463 static int handle_tprot(struct kvm_vcpu *vcpu) 1464 { 1465 u64 address, operand2; 1466 unsigned long gpa; 1467 u8 access_key; 1468 bool writable; 1469 int ret, cc; 1470 u8 ar; 1471 1472 vcpu->stat.instruction_tprot++; 1473 1474 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 1475 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 1476 1477 kvm_s390_get_base_disp_sse(vcpu, &address, &operand2, &ar, NULL); 1478 access_key = (operand2 & 0xf0) >> 4; 1479 1480 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT) 1481 ipte_lock(vcpu->kvm); 1482 1483 ret = guest_translate_address_with_key(vcpu, address, ar, &gpa, 1484 GACC_STORE, access_key); 1485 if (ret == 0) { 1486 gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable); 1487 } else if (ret == PGM_PROTECTION) { 1488 writable = false; 1489 /* Write protected? Try again with read-only... */ 1490 ret = guest_translate_address_with_key(vcpu, address, ar, &gpa, 1491 GACC_FETCH, access_key); 1492 } 1493 if (ret >= 0) { 1494 cc = -1; 1495 1496 /* Fetching permitted; storing permitted */ 1497 if (ret == 0 && writable) 1498 cc = 0; 1499 /* Fetching permitted; storing not permitted */ 1500 else if (ret == 0 && !writable) 1501 cc = 1; 1502 /* Fetching not permitted; storing not permitted */ 1503 else if (ret == PGM_PROTECTION) 1504 cc = 2; 1505 /* Translation not available */ 1506 else if (ret != PGM_ADDRESSING && ret != PGM_TRANSLATION_SPEC) 1507 cc = 3; 1508 1509 if (cc != -1) { 1510 kvm_s390_set_psw_cc(vcpu, cc); 1511 ret = 0; 1512 } else { 1513 ret = kvm_s390_inject_program_int(vcpu, ret); 1514 } 1515 } 1516 1517 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT) 1518 ipte_unlock(vcpu->kvm); 1519 return ret; 1520 } 1521 1522 int kvm_s390_handle_e5(struct kvm_vcpu *vcpu) 1523 { 1524 switch (vcpu->arch.sie_block->ipa & 0x00ff) { 1525 case 0x01: 1526 return handle_tprot(vcpu); 1527 default: 1528 return -EOPNOTSUPP; 1529 } 1530 } 1531 1532 static int handle_sckpf(struct kvm_vcpu *vcpu) 1533 { 1534 u32 value; 1535 1536 vcpu->stat.instruction_sckpf++; 1537 1538 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 1539 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 1540 1541 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000) 1542 return kvm_s390_inject_program_int(vcpu, 1543 PGM_SPECIFICATION); 1544 1545 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff; 1546 vcpu->arch.sie_block->todpr = value; 1547 1548 return 0; 1549 } 1550 1551 static int handle_ptff(struct kvm_vcpu *vcpu) 1552 { 1553 vcpu->stat.instruction_ptff++; 1554 1555 /* we don't emulate any control instructions yet */ 1556 kvm_s390_set_psw_cc(vcpu, 3); 1557 return 0; 1558 } 1559 1560 int kvm_s390_handle_01(struct kvm_vcpu *vcpu) 1561 { 1562 switch (vcpu->arch.sie_block->ipa & 0x00ff) { 1563 case 0x04: 1564 return handle_ptff(vcpu); 1565 case 0x07: 1566 return handle_sckpf(vcpu); 1567 default: 1568 return -EOPNOTSUPP; 1569 } 1570 } 1571