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