1 /* 2 * QEMU S390x KVM implementation 3 * 4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de> 5 * Copyright IBM Corp. 2012 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * Contributions after 2012-10-29 are licensed under the terms of the 18 * GNU GPL, version 2 or (at your option) any later version. 19 * 20 * You should have received a copy of the GNU (Lesser) General Public 21 * License along with this library; if not, see <http://www.gnu.org/licenses/>. 22 */ 23 24 #include "qemu/osdep.h" 25 #include <sys/ioctl.h> 26 27 #include <linux/kvm.h> 28 #include <asm/ptrace.h> 29 30 #include "qemu-common.h" 31 #include "cpu.h" 32 #include "internal.h" 33 #include "kvm_s390x.h" 34 #include "qapi/error.h" 35 #include "qemu/error-report.h" 36 #include "qemu/timer.h" 37 #include "qemu/units.h" 38 #include "qemu/mmap-alloc.h" 39 #include "sysemu/sysemu.h" 40 #include "sysemu/hw_accel.h" 41 #include "hw/hw.h" 42 #include "sysemu/device_tree.h" 43 #include "exec/gdbstub.h" 44 #include "trace.h" 45 #include "hw/s390x/s390-pci-inst.h" 46 #include "hw/s390x/s390-pci-bus.h" 47 #include "hw/s390x/ipl.h" 48 #include "hw/s390x/ebcdic.h" 49 #include "exec/memattrs.h" 50 #include "hw/s390x/s390-virtio-ccw.h" 51 #include "hw/s390x/s390-virtio-hcall.h" 52 53 #ifndef DEBUG_KVM 54 #define DEBUG_KVM 0 55 #endif 56 57 #define DPRINTF(fmt, ...) do { \ 58 if (DEBUG_KVM) { \ 59 fprintf(stderr, fmt, ## __VA_ARGS__); \ 60 } \ 61 } while (0) 62 63 #define kvm_vm_check_mem_attr(s, attr) \ 64 kvm_vm_check_attr(s, KVM_S390_VM_MEM_CTRL, attr) 65 66 #define IPA0_DIAG 0x8300 67 #define IPA0_SIGP 0xae00 68 #define IPA0_B2 0xb200 69 #define IPA0_B9 0xb900 70 #define IPA0_EB 0xeb00 71 #define IPA0_E3 0xe300 72 73 #define PRIV_B2_SCLP_CALL 0x20 74 #define PRIV_B2_CSCH 0x30 75 #define PRIV_B2_HSCH 0x31 76 #define PRIV_B2_MSCH 0x32 77 #define PRIV_B2_SSCH 0x33 78 #define PRIV_B2_STSCH 0x34 79 #define PRIV_B2_TSCH 0x35 80 #define PRIV_B2_TPI 0x36 81 #define PRIV_B2_SAL 0x37 82 #define PRIV_B2_RSCH 0x38 83 #define PRIV_B2_STCRW 0x39 84 #define PRIV_B2_STCPS 0x3a 85 #define PRIV_B2_RCHP 0x3b 86 #define PRIV_B2_SCHM 0x3c 87 #define PRIV_B2_CHSC 0x5f 88 #define PRIV_B2_SIGA 0x74 89 #define PRIV_B2_XSCH 0x76 90 91 #define PRIV_EB_SQBS 0x8a 92 #define PRIV_EB_PCISTB 0xd0 93 #define PRIV_EB_SIC 0xd1 94 95 #define PRIV_B9_EQBS 0x9c 96 #define PRIV_B9_CLP 0xa0 97 #define PRIV_B9_PCISTG 0xd0 98 #define PRIV_B9_PCILG 0xd2 99 #define PRIV_B9_RPCIT 0xd3 100 101 #define PRIV_E3_MPCIFC 0xd0 102 #define PRIV_E3_STPCIFC 0xd4 103 104 #define DIAG_TIMEREVENT 0x288 105 #define DIAG_IPL 0x308 106 #define DIAG_KVM_HYPERCALL 0x500 107 #define DIAG_KVM_BREAKPOINT 0x501 108 109 #define ICPT_INSTRUCTION 0x04 110 #define ICPT_PROGRAM 0x08 111 #define ICPT_EXT_INT 0x14 112 #define ICPT_WAITPSW 0x1c 113 #define ICPT_SOFT_INTERCEPT 0x24 114 #define ICPT_CPU_STOP 0x28 115 #define ICPT_OPEREXC 0x2c 116 #define ICPT_IO 0x40 117 118 #define NR_LOCAL_IRQS 32 119 /* 120 * Needs to be big enough to contain max_cpus emergency signals 121 * and in addition NR_LOCAL_IRQS interrupts 122 */ 123 #define VCPU_IRQ_BUF_SIZE (sizeof(struct kvm_s390_irq) * \ 124 (max_cpus + NR_LOCAL_IRQS)) 125 126 static CPUWatchpoint hw_watchpoint; 127 /* 128 * We don't use a list because this structure is also used to transmit the 129 * hardware breakpoints to the kernel. 130 */ 131 static struct kvm_hw_breakpoint *hw_breakpoints; 132 static int nb_hw_breakpoints; 133 134 const KVMCapabilityInfo kvm_arch_required_capabilities[] = { 135 KVM_CAP_LAST_INFO 136 }; 137 138 static int cap_sync_regs; 139 static int cap_async_pf; 140 static int cap_mem_op; 141 static int cap_s390_irq; 142 static int cap_ri; 143 static int cap_gs; 144 static int cap_hpage_1m; 145 146 static int active_cmma; 147 148 static void *legacy_s390_alloc(size_t size, uint64_t *align, bool shared); 149 150 static int kvm_s390_query_mem_limit(uint64_t *memory_limit) 151 { 152 struct kvm_device_attr attr = { 153 .group = KVM_S390_VM_MEM_CTRL, 154 .attr = KVM_S390_VM_MEM_LIMIT_SIZE, 155 .addr = (uint64_t) memory_limit, 156 }; 157 158 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr); 159 } 160 161 int kvm_s390_set_mem_limit(uint64_t new_limit, uint64_t *hw_limit) 162 { 163 int rc; 164 165 struct kvm_device_attr attr = { 166 .group = KVM_S390_VM_MEM_CTRL, 167 .attr = KVM_S390_VM_MEM_LIMIT_SIZE, 168 .addr = (uint64_t) &new_limit, 169 }; 170 171 if (!kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_LIMIT_SIZE)) { 172 return 0; 173 } 174 175 rc = kvm_s390_query_mem_limit(hw_limit); 176 if (rc) { 177 return rc; 178 } else if (*hw_limit < new_limit) { 179 return -E2BIG; 180 } 181 182 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr); 183 } 184 185 int kvm_s390_cmma_active(void) 186 { 187 return active_cmma; 188 } 189 190 static bool kvm_s390_cmma_available(void) 191 { 192 static bool initialized, value; 193 194 if (!initialized) { 195 initialized = true; 196 value = kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_ENABLE_CMMA) && 197 kvm_vm_check_mem_attr(kvm_state, KVM_S390_VM_MEM_CLR_CMMA); 198 } 199 return value; 200 } 201 202 void kvm_s390_cmma_reset(void) 203 { 204 int rc; 205 struct kvm_device_attr attr = { 206 .group = KVM_S390_VM_MEM_CTRL, 207 .attr = KVM_S390_VM_MEM_CLR_CMMA, 208 }; 209 210 if (!kvm_s390_cmma_active()) { 211 return; 212 } 213 214 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr); 215 trace_kvm_clear_cmma(rc); 216 } 217 218 static void kvm_s390_enable_cmma(void) 219 { 220 int rc; 221 struct kvm_device_attr attr = { 222 .group = KVM_S390_VM_MEM_CTRL, 223 .attr = KVM_S390_VM_MEM_ENABLE_CMMA, 224 }; 225 226 if (cap_hpage_1m) { 227 warn_report("CMM will not be enabled because it is not " 228 "compatible with huge memory backings."); 229 return; 230 } 231 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr); 232 active_cmma = !rc; 233 trace_kvm_enable_cmma(rc); 234 } 235 236 static void kvm_s390_set_attr(uint64_t attr) 237 { 238 struct kvm_device_attr attribute = { 239 .group = KVM_S390_VM_CRYPTO, 240 .attr = attr, 241 }; 242 243 int ret = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute); 244 245 if (ret) { 246 error_report("Failed to set crypto device attribute %lu: %s", 247 attr, strerror(-ret)); 248 } 249 } 250 251 static void kvm_s390_init_aes_kw(void) 252 { 253 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_AES_KW; 254 255 if (object_property_get_bool(OBJECT(qdev_get_machine()), "aes-key-wrap", 256 NULL)) { 257 attr = KVM_S390_VM_CRYPTO_ENABLE_AES_KW; 258 } 259 260 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) { 261 kvm_s390_set_attr(attr); 262 } 263 } 264 265 static void kvm_s390_init_dea_kw(void) 266 { 267 uint64_t attr = KVM_S390_VM_CRYPTO_DISABLE_DEA_KW; 268 269 if (object_property_get_bool(OBJECT(qdev_get_machine()), "dea-key-wrap", 270 NULL)) { 271 attr = KVM_S390_VM_CRYPTO_ENABLE_DEA_KW; 272 } 273 274 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) { 275 kvm_s390_set_attr(attr); 276 } 277 } 278 279 void kvm_s390_crypto_reset(void) 280 { 281 if (s390_has_feat(S390_FEAT_MSA_EXT_3)) { 282 kvm_s390_init_aes_kw(); 283 kvm_s390_init_dea_kw(); 284 } 285 } 286 287 static int kvm_s390_configure_mempath_backing(KVMState *s) 288 { 289 size_t path_psize = qemu_mempath_getpagesize(mem_path); 290 291 if (path_psize == 4 * KiB) { 292 return 0; 293 } 294 295 if (!hpage_1m_allowed()) { 296 error_report("This QEMU machine does not support huge page " 297 "mappings"); 298 return -EINVAL; 299 } 300 301 if (path_psize != 1 * MiB) { 302 error_report("Memory backing with 2G pages was specified, " 303 "but KVM does not support this memory backing"); 304 return -EINVAL; 305 } 306 307 if (kvm_vm_enable_cap(s, KVM_CAP_S390_HPAGE_1M, 0)) { 308 error_report("Memory backing with 1M pages was specified, " 309 "but KVM does not support this memory backing"); 310 return -EINVAL; 311 } 312 313 cap_hpage_1m = 1; 314 return 0; 315 } 316 317 int kvm_arch_init(MachineState *ms, KVMState *s) 318 { 319 MachineClass *mc = MACHINE_GET_CLASS(ms); 320 321 if (mem_path && kvm_s390_configure_mempath_backing(s)) { 322 return -EINVAL; 323 } 324 325 mc->default_cpu_type = S390_CPU_TYPE_NAME("host"); 326 cap_sync_regs = kvm_check_extension(s, KVM_CAP_SYNC_REGS); 327 cap_async_pf = kvm_check_extension(s, KVM_CAP_ASYNC_PF); 328 cap_mem_op = kvm_check_extension(s, KVM_CAP_S390_MEM_OP); 329 cap_s390_irq = kvm_check_extension(s, KVM_CAP_S390_INJECT_IRQ); 330 331 if (!kvm_check_extension(s, KVM_CAP_S390_GMAP) 332 || !kvm_check_extension(s, KVM_CAP_S390_COW)) { 333 phys_mem_set_alloc(legacy_s390_alloc); 334 } 335 336 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_SIGP, 0); 337 kvm_vm_enable_cap(s, KVM_CAP_S390_VECTOR_REGISTERS, 0); 338 kvm_vm_enable_cap(s, KVM_CAP_S390_USER_STSI, 0); 339 if (ri_allowed()) { 340 if (kvm_vm_enable_cap(s, KVM_CAP_S390_RI, 0) == 0) { 341 cap_ri = 1; 342 } 343 } 344 if (cpu_model_allowed()) { 345 if (kvm_vm_enable_cap(s, KVM_CAP_S390_GS, 0) == 0) { 346 cap_gs = 1; 347 } 348 } 349 350 /* 351 * The migration interface for ais was introduced with kernel 4.13 352 * but the capability itself had been active since 4.12. As migration 353 * support is considered necessary let's disable ais in the 2.10 354 * machine. 355 */ 356 /* kvm_vm_enable_cap(s, KVM_CAP_S390_AIS, 0); */ 357 358 return 0; 359 } 360 361 int kvm_arch_irqchip_create(MachineState *ms, KVMState *s) 362 { 363 return 0; 364 } 365 366 unsigned long kvm_arch_vcpu_id(CPUState *cpu) 367 { 368 return cpu->cpu_index; 369 } 370 371 int kvm_arch_init_vcpu(CPUState *cs) 372 { 373 S390CPU *cpu = S390_CPU(cs); 374 kvm_s390_set_cpu_state(cpu, cpu->env.cpu_state); 375 cpu->irqstate = g_malloc0(VCPU_IRQ_BUF_SIZE); 376 return 0; 377 } 378 379 void kvm_s390_reset_vcpu(S390CPU *cpu) 380 { 381 CPUState *cs = CPU(cpu); 382 383 /* The initial reset call is needed here to reset in-kernel 384 * vcpu data that we can't access directly from QEMU 385 * (i.e. with older kernels which don't support sync_regs/ONE_REG). 386 * Before this ioctl cpu_synchronize_state() is called in common kvm 387 * code (kvm-all) */ 388 if (kvm_vcpu_ioctl(cs, KVM_S390_INITIAL_RESET, NULL)) { 389 error_report("Initial CPU reset failed on CPU %i", cs->cpu_index); 390 } 391 } 392 393 static int can_sync_regs(CPUState *cs, int regs) 394 { 395 return cap_sync_regs && (cs->kvm_run->kvm_valid_regs & regs) == regs; 396 } 397 398 int kvm_arch_put_registers(CPUState *cs, int level) 399 { 400 S390CPU *cpu = S390_CPU(cs); 401 CPUS390XState *env = &cpu->env; 402 struct kvm_sregs sregs; 403 struct kvm_regs regs; 404 struct kvm_fpu fpu = {}; 405 int r; 406 int i; 407 408 /* always save the PSW and the GPRS*/ 409 cs->kvm_run->psw_addr = env->psw.addr; 410 cs->kvm_run->psw_mask = env->psw.mask; 411 412 if (can_sync_regs(cs, KVM_SYNC_GPRS)) { 413 for (i = 0; i < 16; i++) { 414 cs->kvm_run->s.regs.gprs[i] = env->regs[i]; 415 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GPRS; 416 } 417 } else { 418 for (i = 0; i < 16; i++) { 419 regs.gprs[i] = env->regs[i]; 420 } 421 r = kvm_vcpu_ioctl(cs, KVM_SET_REGS, ®s); 422 if (r < 0) { 423 return r; 424 } 425 } 426 427 if (can_sync_regs(cs, KVM_SYNC_VRS)) { 428 for (i = 0; i < 32; i++) { 429 cs->kvm_run->s.regs.vrs[i][0] = env->vregs[i][0].ll; 430 cs->kvm_run->s.regs.vrs[i][1] = env->vregs[i][1].ll; 431 } 432 cs->kvm_run->s.regs.fpc = env->fpc; 433 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_VRS; 434 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) { 435 for (i = 0; i < 16; i++) { 436 cs->kvm_run->s.regs.fprs[i] = get_freg(env, i)->ll; 437 } 438 cs->kvm_run->s.regs.fpc = env->fpc; 439 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_FPRS; 440 } else { 441 /* Floating point */ 442 for (i = 0; i < 16; i++) { 443 fpu.fprs[i] = get_freg(env, i)->ll; 444 } 445 fpu.fpc = env->fpc; 446 447 r = kvm_vcpu_ioctl(cs, KVM_SET_FPU, &fpu); 448 if (r < 0) { 449 return r; 450 } 451 } 452 453 /* Do we need to save more than that? */ 454 if (level == KVM_PUT_RUNTIME_STATE) { 455 return 0; 456 } 457 458 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) { 459 cs->kvm_run->s.regs.cputm = env->cputm; 460 cs->kvm_run->s.regs.ckc = env->ckc; 461 cs->kvm_run->s.regs.todpr = env->todpr; 462 cs->kvm_run->s.regs.gbea = env->gbea; 463 cs->kvm_run->s.regs.pp = env->pp; 464 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ARCH0; 465 } else { 466 /* 467 * These ONE_REGS are not protected by a capability. As they are only 468 * necessary for migration we just trace a possible error, but don't 469 * return with an error return code. 470 */ 471 kvm_set_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm); 472 kvm_set_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc); 473 kvm_set_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr); 474 kvm_set_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea); 475 kvm_set_one_reg(cs, KVM_REG_S390_PP, &env->pp); 476 } 477 478 if (can_sync_regs(cs, KVM_SYNC_RICCB)) { 479 memcpy(cs->kvm_run->s.regs.riccb, env->riccb, 64); 480 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_RICCB; 481 } 482 483 /* pfault parameters */ 484 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) { 485 cs->kvm_run->s.regs.pft = env->pfault_token; 486 cs->kvm_run->s.regs.pfs = env->pfault_select; 487 cs->kvm_run->s.regs.pfc = env->pfault_compare; 488 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PFAULT; 489 } else if (cap_async_pf) { 490 r = kvm_set_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token); 491 if (r < 0) { 492 return r; 493 } 494 r = kvm_set_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare); 495 if (r < 0) { 496 return r; 497 } 498 r = kvm_set_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select); 499 if (r < 0) { 500 return r; 501 } 502 } 503 504 /* access registers and control registers*/ 505 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) { 506 for (i = 0; i < 16; i++) { 507 cs->kvm_run->s.regs.acrs[i] = env->aregs[i]; 508 cs->kvm_run->s.regs.crs[i] = env->cregs[i]; 509 } 510 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ACRS; 511 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_CRS; 512 } else { 513 for (i = 0; i < 16; i++) { 514 sregs.acrs[i] = env->aregs[i]; 515 sregs.crs[i] = env->cregs[i]; 516 } 517 r = kvm_vcpu_ioctl(cs, KVM_SET_SREGS, &sregs); 518 if (r < 0) { 519 return r; 520 } 521 } 522 523 if (can_sync_regs(cs, KVM_SYNC_GSCB)) { 524 memcpy(cs->kvm_run->s.regs.gscb, env->gscb, 32); 525 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_GSCB; 526 } 527 528 if (can_sync_regs(cs, KVM_SYNC_BPBC)) { 529 cs->kvm_run->s.regs.bpbc = env->bpbc; 530 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_BPBC; 531 } 532 533 if (can_sync_regs(cs, KVM_SYNC_ETOKEN)) { 534 cs->kvm_run->s.regs.etoken = env->etoken; 535 cs->kvm_run->s.regs.etoken_extension = env->etoken_extension; 536 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_ETOKEN; 537 } 538 539 /* Finally the prefix */ 540 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) { 541 cs->kvm_run->s.regs.prefix = env->psa; 542 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_PREFIX; 543 } else { 544 /* prefix is only supported via sync regs */ 545 } 546 return 0; 547 } 548 549 int kvm_arch_get_registers(CPUState *cs) 550 { 551 S390CPU *cpu = S390_CPU(cs); 552 CPUS390XState *env = &cpu->env; 553 struct kvm_sregs sregs; 554 struct kvm_regs regs; 555 struct kvm_fpu fpu; 556 int i, r; 557 558 /* get the PSW */ 559 env->psw.addr = cs->kvm_run->psw_addr; 560 env->psw.mask = cs->kvm_run->psw_mask; 561 562 /* the GPRS */ 563 if (can_sync_regs(cs, KVM_SYNC_GPRS)) { 564 for (i = 0; i < 16; i++) { 565 env->regs[i] = cs->kvm_run->s.regs.gprs[i]; 566 } 567 } else { 568 r = kvm_vcpu_ioctl(cs, KVM_GET_REGS, ®s); 569 if (r < 0) { 570 return r; 571 } 572 for (i = 0; i < 16; i++) { 573 env->regs[i] = regs.gprs[i]; 574 } 575 } 576 577 /* The ACRS and CRS */ 578 if (can_sync_regs(cs, KVM_SYNC_ACRS | KVM_SYNC_CRS)) { 579 for (i = 0; i < 16; i++) { 580 env->aregs[i] = cs->kvm_run->s.regs.acrs[i]; 581 env->cregs[i] = cs->kvm_run->s.regs.crs[i]; 582 } 583 } else { 584 r = kvm_vcpu_ioctl(cs, KVM_GET_SREGS, &sregs); 585 if (r < 0) { 586 return r; 587 } 588 for (i = 0; i < 16; i++) { 589 env->aregs[i] = sregs.acrs[i]; 590 env->cregs[i] = sregs.crs[i]; 591 } 592 } 593 594 /* Floating point and vector registers */ 595 if (can_sync_regs(cs, KVM_SYNC_VRS)) { 596 for (i = 0; i < 32; i++) { 597 env->vregs[i][0].ll = cs->kvm_run->s.regs.vrs[i][0]; 598 env->vregs[i][1].ll = cs->kvm_run->s.regs.vrs[i][1]; 599 } 600 env->fpc = cs->kvm_run->s.regs.fpc; 601 } else if (can_sync_regs(cs, KVM_SYNC_FPRS)) { 602 for (i = 0; i < 16; i++) { 603 get_freg(env, i)->ll = cs->kvm_run->s.regs.fprs[i]; 604 } 605 env->fpc = cs->kvm_run->s.regs.fpc; 606 } else { 607 r = kvm_vcpu_ioctl(cs, KVM_GET_FPU, &fpu); 608 if (r < 0) { 609 return r; 610 } 611 for (i = 0; i < 16; i++) { 612 get_freg(env, i)->ll = fpu.fprs[i]; 613 } 614 env->fpc = fpu.fpc; 615 } 616 617 /* The prefix */ 618 if (can_sync_regs(cs, KVM_SYNC_PREFIX)) { 619 env->psa = cs->kvm_run->s.regs.prefix; 620 } 621 622 if (can_sync_regs(cs, KVM_SYNC_ARCH0)) { 623 env->cputm = cs->kvm_run->s.regs.cputm; 624 env->ckc = cs->kvm_run->s.regs.ckc; 625 env->todpr = cs->kvm_run->s.regs.todpr; 626 env->gbea = cs->kvm_run->s.regs.gbea; 627 env->pp = cs->kvm_run->s.regs.pp; 628 } else { 629 /* 630 * These ONE_REGS are not protected by a capability. As they are only 631 * necessary for migration we just trace a possible error, but don't 632 * return with an error return code. 633 */ 634 kvm_get_one_reg(cs, KVM_REG_S390_CPU_TIMER, &env->cputm); 635 kvm_get_one_reg(cs, KVM_REG_S390_CLOCK_COMP, &env->ckc); 636 kvm_get_one_reg(cs, KVM_REG_S390_TODPR, &env->todpr); 637 kvm_get_one_reg(cs, KVM_REG_S390_GBEA, &env->gbea); 638 kvm_get_one_reg(cs, KVM_REG_S390_PP, &env->pp); 639 } 640 641 if (can_sync_regs(cs, KVM_SYNC_RICCB)) { 642 memcpy(env->riccb, cs->kvm_run->s.regs.riccb, 64); 643 } 644 645 if (can_sync_regs(cs, KVM_SYNC_GSCB)) { 646 memcpy(env->gscb, cs->kvm_run->s.regs.gscb, 32); 647 } 648 649 if (can_sync_regs(cs, KVM_SYNC_BPBC)) { 650 env->bpbc = cs->kvm_run->s.regs.bpbc; 651 } 652 653 if (can_sync_regs(cs, KVM_SYNC_ETOKEN)) { 654 env->etoken = cs->kvm_run->s.regs.etoken; 655 env->etoken_extension = cs->kvm_run->s.regs.etoken_extension; 656 } 657 658 /* pfault parameters */ 659 if (can_sync_regs(cs, KVM_SYNC_PFAULT)) { 660 env->pfault_token = cs->kvm_run->s.regs.pft; 661 env->pfault_select = cs->kvm_run->s.regs.pfs; 662 env->pfault_compare = cs->kvm_run->s.regs.pfc; 663 } else if (cap_async_pf) { 664 r = kvm_get_one_reg(cs, KVM_REG_S390_PFTOKEN, &env->pfault_token); 665 if (r < 0) { 666 return r; 667 } 668 r = kvm_get_one_reg(cs, KVM_REG_S390_PFCOMPARE, &env->pfault_compare); 669 if (r < 0) { 670 return r; 671 } 672 r = kvm_get_one_reg(cs, KVM_REG_S390_PFSELECT, &env->pfault_select); 673 if (r < 0) { 674 return r; 675 } 676 } 677 678 return 0; 679 } 680 681 int kvm_s390_get_clock(uint8_t *tod_high, uint64_t *tod_low) 682 { 683 int r; 684 struct kvm_device_attr attr = { 685 .group = KVM_S390_VM_TOD, 686 .attr = KVM_S390_VM_TOD_LOW, 687 .addr = (uint64_t)tod_low, 688 }; 689 690 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr); 691 if (r) { 692 return r; 693 } 694 695 attr.attr = KVM_S390_VM_TOD_HIGH; 696 attr.addr = (uint64_t)tod_high; 697 return kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr); 698 } 699 700 int kvm_s390_get_clock_ext(uint8_t *tod_high, uint64_t *tod_low) 701 { 702 int r; 703 struct kvm_s390_vm_tod_clock gtod; 704 struct kvm_device_attr attr = { 705 .group = KVM_S390_VM_TOD, 706 .attr = KVM_S390_VM_TOD_EXT, 707 .addr = (uint64_t)>od, 708 }; 709 710 r = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr); 711 *tod_high = gtod.epoch_idx; 712 *tod_low = gtod.tod; 713 714 return r; 715 } 716 717 int kvm_s390_set_clock(uint8_t tod_high, uint64_t tod_low) 718 { 719 int r; 720 struct kvm_device_attr attr = { 721 .group = KVM_S390_VM_TOD, 722 .attr = KVM_S390_VM_TOD_LOW, 723 .addr = (uint64_t)&tod_low, 724 }; 725 726 r = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr); 727 if (r) { 728 return r; 729 } 730 731 attr.attr = KVM_S390_VM_TOD_HIGH; 732 attr.addr = (uint64_t)&tod_high; 733 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr); 734 } 735 736 int kvm_s390_set_clock_ext(uint8_t tod_high, uint64_t tod_low) 737 { 738 struct kvm_s390_vm_tod_clock gtod = { 739 .epoch_idx = tod_high, 740 .tod = tod_low, 741 }; 742 struct kvm_device_attr attr = { 743 .group = KVM_S390_VM_TOD, 744 .attr = KVM_S390_VM_TOD_EXT, 745 .addr = (uint64_t)>od, 746 }; 747 748 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr); 749 } 750 751 /** 752 * kvm_s390_mem_op: 753 * @addr: the logical start address in guest memory 754 * @ar: the access register number 755 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying 756 * @len: length that should be transferred 757 * @is_write: true = write, false = read 758 * Returns: 0 on success, non-zero if an exception or error occurred 759 * 760 * Use KVM ioctl to read/write from/to guest memory. An access exception 761 * is injected into the vCPU in case of translation errors. 762 */ 763 int kvm_s390_mem_op(S390CPU *cpu, vaddr addr, uint8_t ar, void *hostbuf, 764 int len, bool is_write) 765 { 766 struct kvm_s390_mem_op mem_op = { 767 .gaddr = addr, 768 .flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION, 769 .size = len, 770 .op = is_write ? KVM_S390_MEMOP_LOGICAL_WRITE 771 : KVM_S390_MEMOP_LOGICAL_READ, 772 .buf = (uint64_t)hostbuf, 773 .ar = ar, 774 }; 775 int ret; 776 777 if (!cap_mem_op) { 778 return -ENOSYS; 779 } 780 if (!hostbuf) { 781 mem_op.flags |= KVM_S390_MEMOP_F_CHECK_ONLY; 782 } 783 784 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_S390_MEM_OP, &mem_op); 785 if (ret < 0) { 786 error_printf("KVM_S390_MEM_OP failed: %s\n", strerror(-ret)); 787 } 788 return ret; 789 } 790 791 /* 792 * Legacy layout for s390: 793 * Older S390 KVM requires the topmost vma of the RAM to be 794 * smaller than an system defined value, which is at least 256GB. 795 * Larger systems have larger values. We put the guest between 796 * the end of data segment (system break) and this value. We 797 * use 32GB as a base to have enough room for the system break 798 * to grow. We also have to use MAP parameters that avoid 799 * read-only mapping of guest pages. 800 */ 801 static void *legacy_s390_alloc(size_t size, uint64_t *align, bool shared) 802 { 803 static void *mem; 804 805 if (mem) { 806 /* we only support one allocation, which is enough for initial ram */ 807 return NULL; 808 } 809 810 mem = mmap((void *) 0x800000000ULL, size, 811 PROT_EXEC|PROT_READ|PROT_WRITE, 812 MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0); 813 if (mem == MAP_FAILED) { 814 mem = NULL; 815 } 816 if (mem && align) { 817 *align = QEMU_VMALLOC_ALIGN; 818 } 819 return mem; 820 } 821 822 static uint8_t const *sw_bp_inst; 823 static uint8_t sw_bp_ilen; 824 825 static void determine_sw_breakpoint_instr(void) 826 { 827 /* DIAG 501 is used for sw breakpoints with old kernels */ 828 static const uint8_t diag_501[] = {0x83, 0x24, 0x05, 0x01}; 829 /* Instruction 0x0000 is used for sw breakpoints with recent kernels */ 830 static const uint8_t instr_0x0000[] = {0x00, 0x00}; 831 832 if (sw_bp_inst) { 833 return; 834 } 835 if (kvm_vm_enable_cap(kvm_state, KVM_CAP_S390_USER_INSTR0, 0)) { 836 sw_bp_inst = diag_501; 837 sw_bp_ilen = sizeof(diag_501); 838 DPRINTF("KVM: will use 4-byte sw breakpoints.\n"); 839 } else { 840 sw_bp_inst = instr_0x0000; 841 sw_bp_ilen = sizeof(instr_0x0000); 842 DPRINTF("KVM: will use 2-byte sw breakpoints.\n"); 843 } 844 } 845 846 int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp) 847 { 848 determine_sw_breakpoint_instr(); 849 850 if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 851 sw_bp_ilen, 0) || 852 cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)sw_bp_inst, sw_bp_ilen, 1)) { 853 return -EINVAL; 854 } 855 return 0; 856 } 857 858 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp) 859 { 860 uint8_t t[MAX_ILEN]; 861 862 if (cpu_memory_rw_debug(cs, bp->pc, t, sw_bp_ilen, 0)) { 863 return -EINVAL; 864 } else if (memcmp(t, sw_bp_inst, sw_bp_ilen)) { 865 return -EINVAL; 866 } else if (cpu_memory_rw_debug(cs, bp->pc, (uint8_t *)&bp->saved_insn, 867 sw_bp_ilen, 1)) { 868 return -EINVAL; 869 } 870 871 return 0; 872 } 873 874 static struct kvm_hw_breakpoint *find_hw_breakpoint(target_ulong addr, 875 int len, int type) 876 { 877 int n; 878 879 for (n = 0; n < nb_hw_breakpoints; n++) { 880 if (hw_breakpoints[n].addr == addr && hw_breakpoints[n].type == type && 881 (hw_breakpoints[n].len == len || len == -1)) { 882 return &hw_breakpoints[n]; 883 } 884 } 885 886 return NULL; 887 } 888 889 static int insert_hw_breakpoint(target_ulong addr, int len, int type) 890 { 891 int size; 892 893 if (find_hw_breakpoint(addr, len, type)) { 894 return -EEXIST; 895 } 896 897 size = (nb_hw_breakpoints + 1) * sizeof(struct kvm_hw_breakpoint); 898 899 if (!hw_breakpoints) { 900 nb_hw_breakpoints = 0; 901 hw_breakpoints = (struct kvm_hw_breakpoint *)g_try_malloc(size); 902 } else { 903 hw_breakpoints = 904 (struct kvm_hw_breakpoint *)g_try_realloc(hw_breakpoints, size); 905 } 906 907 if (!hw_breakpoints) { 908 nb_hw_breakpoints = 0; 909 return -ENOMEM; 910 } 911 912 hw_breakpoints[nb_hw_breakpoints].addr = addr; 913 hw_breakpoints[nb_hw_breakpoints].len = len; 914 hw_breakpoints[nb_hw_breakpoints].type = type; 915 916 nb_hw_breakpoints++; 917 918 return 0; 919 } 920 921 int kvm_arch_insert_hw_breakpoint(target_ulong addr, 922 target_ulong len, int type) 923 { 924 switch (type) { 925 case GDB_BREAKPOINT_HW: 926 type = KVM_HW_BP; 927 break; 928 case GDB_WATCHPOINT_WRITE: 929 if (len < 1) { 930 return -EINVAL; 931 } 932 type = KVM_HW_WP_WRITE; 933 break; 934 default: 935 return -ENOSYS; 936 } 937 return insert_hw_breakpoint(addr, len, type); 938 } 939 940 int kvm_arch_remove_hw_breakpoint(target_ulong addr, 941 target_ulong len, int type) 942 { 943 int size; 944 struct kvm_hw_breakpoint *bp = find_hw_breakpoint(addr, len, type); 945 946 if (bp == NULL) { 947 return -ENOENT; 948 } 949 950 nb_hw_breakpoints--; 951 if (nb_hw_breakpoints > 0) { 952 /* 953 * In order to trim the array, move the last element to the position to 954 * be removed - if necessary. 955 */ 956 if (bp != &hw_breakpoints[nb_hw_breakpoints]) { 957 *bp = hw_breakpoints[nb_hw_breakpoints]; 958 } 959 size = nb_hw_breakpoints * sizeof(struct kvm_hw_breakpoint); 960 hw_breakpoints = 961 (struct kvm_hw_breakpoint *)g_realloc(hw_breakpoints, size); 962 } else { 963 g_free(hw_breakpoints); 964 hw_breakpoints = NULL; 965 } 966 967 return 0; 968 } 969 970 void kvm_arch_remove_all_hw_breakpoints(void) 971 { 972 nb_hw_breakpoints = 0; 973 g_free(hw_breakpoints); 974 hw_breakpoints = NULL; 975 } 976 977 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *dbg) 978 { 979 int i; 980 981 if (nb_hw_breakpoints > 0) { 982 dbg->arch.nr_hw_bp = nb_hw_breakpoints; 983 dbg->arch.hw_bp = hw_breakpoints; 984 985 for (i = 0; i < nb_hw_breakpoints; ++i) { 986 hw_breakpoints[i].phys_addr = s390_cpu_get_phys_addr_debug(cpu, 987 hw_breakpoints[i].addr); 988 } 989 dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP; 990 } else { 991 dbg->arch.nr_hw_bp = 0; 992 dbg->arch.hw_bp = NULL; 993 } 994 } 995 996 void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run) 997 { 998 } 999 1000 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run) 1001 { 1002 return MEMTXATTRS_UNSPECIFIED; 1003 } 1004 1005 int kvm_arch_process_async_events(CPUState *cs) 1006 { 1007 return cs->halted; 1008 } 1009 1010 static int s390_kvm_irq_to_interrupt(struct kvm_s390_irq *irq, 1011 struct kvm_s390_interrupt *interrupt) 1012 { 1013 int r = 0; 1014 1015 interrupt->type = irq->type; 1016 switch (irq->type) { 1017 case KVM_S390_INT_VIRTIO: 1018 interrupt->parm = irq->u.ext.ext_params; 1019 /* fall through */ 1020 case KVM_S390_INT_PFAULT_INIT: 1021 case KVM_S390_INT_PFAULT_DONE: 1022 interrupt->parm64 = irq->u.ext.ext_params2; 1023 break; 1024 case KVM_S390_PROGRAM_INT: 1025 interrupt->parm = irq->u.pgm.code; 1026 break; 1027 case KVM_S390_SIGP_SET_PREFIX: 1028 interrupt->parm = irq->u.prefix.address; 1029 break; 1030 case KVM_S390_INT_SERVICE: 1031 interrupt->parm = irq->u.ext.ext_params; 1032 break; 1033 case KVM_S390_MCHK: 1034 interrupt->parm = irq->u.mchk.cr14; 1035 interrupt->parm64 = irq->u.mchk.mcic; 1036 break; 1037 case KVM_S390_INT_EXTERNAL_CALL: 1038 interrupt->parm = irq->u.extcall.code; 1039 break; 1040 case KVM_S390_INT_EMERGENCY: 1041 interrupt->parm = irq->u.emerg.code; 1042 break; 1043 case KVM_S390_SIGP_STOP: 1044 case KVM_S390_RESTART: 1045 break; /* These types have no parameters */ 1046 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: 1047 interrupt->parm = irq->u.io.subchannel_id << 16; 1048 interrupt->parm |= irq->u.io.subchannel_nr; 1049 interrupt->parm64 = (uint64_t)irq->u.io.io_int_parm << 32; 1050 interrupt->parm64 |= irq->u.io.io_int_word; 1051 break; 1052 default: 1053 r = -EINVAL; 1054 break; 1055 } 1056 return r; 1057 } 1058 1059 static void inject_vcpu_irq_legacy(CPUState *cs, struct kvm_s390_irq *irq) 1060 { 1061 struct kvm_s390_interrupt kvmint = {}; 1062 int r; 1063 1064 r = s390_kvm_irq_to_interrupt(irq, &kvmint); 1065 if (r < 0) { 1066 fprintf(stderr, "%s called with bogus interrupt\n", __func__); 1067 exit(1); 1068 } 1069 1070 r = kvm_vcpu_ioctl(cs, KVM_S390_INTERRUPT, &kvmint); 1071 if (r < 0) { 1072 fprintf(stderr, "KVM failed to inject interrupt\n"); 1073 exit(1); 1074 } 1075 } 1076 1077 void kvm_s390_vcpu_interrupt(S390CPU *cpu, struct kvm_s390_irq *irq) 1078 { 1079 CPUState *cs = CPU(cpu); 1080 int r; 1081 1082 if (cap_s390_irq) { 1083 r = kvm_vcpu_ioctl(cs, KVM_S390_IRQ, irq); 1084 if (!r) { 1085 return; 1086 } 1087 error_report("KVM failed to inject interrupt %llx", irq->type); 1088 exit(1); 1089 } 1090 1091 inject_vcpu_irq_legacy(cs, irq); 1092 } 1093 1094 void kvm_s390_floating_interrupt_legacy(struct kvm_s390_irq *irq) 1095 { 1096 struct kvm_s390_interrupt kvmint = {}; 1097 int r; 1098 1099 r = s390_kvm_irq_to_interrupt(irq, &kvmint); 1100 if (r < 0) { 1101 fprintf(stderr, "%s called with bogus interrupt\n", __func__); 1102 exit(1); 1103 } 1104 1105 r = kvm_vm_ioctl(kvm_state, KVM_S390_INTERRUPT, &kvmint); 1106 if (r < 0) { 1107 fprintf(stderr, "KVM failed to inject interrupt\n"); 1108 exit(1); 1109 } 1110 } 1111 1112 void kvm_s390_program_interrupt(S390CPU *cpu, uint16_t code) 1113 { 1114 struct kvm_s390_irq irq = { 1115 .type = KVM_S390_PROGRAM_INT, 1116 .u.pgm.code = code, 1117 }; 1118 1119 kvm_s390_vcpu_interrupt(cpu, &irq); 1120 } 1121 1122 void kvm_s390_access_exception(S390CPU *cpu, uint16_t code, uint64_t te_code) 1123 { 1124 struct kvm_s390_irq irq = { 1125 .type = KVM_S390_PROGRAM_INT, 1126 .u.pgm.code = code, 1127 .u.pgm.trans_exc_code = te_code, 1128 .u.pgm.exc_access_id = te_code & 3, 1129 }; 1130 1131 kvm_s390_vcpu_interrupt(cpu, &irq); 1132 } 1133 1134 static int kvm_sclp_service_call(S390CPU *cpu, struct kvm_run *run, 1135 uint16_t ipbh0) 1136 { 1137 CPUS390XState *env = &cpu->env; 1138 uint64_t sccb; 1139 uint32_t code; 1140 int r = 0; 1141 1142 sccb = env->regs[ipbh0 & 0xf]; 1143 code = env->regs[(ipbh0 & 0xf0) >> 4]; 1144 1145 r = sclp_service_call(env, sccb, code); 1146 if (r < 0) { 1147 kvm_s390_program_interrupt(cpu, -r); 1148 } else { 1149 setcc(cpu, r); 1150 } 1151 1152 return 0; 1153 } 1154 1155 static int handle_b2(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1) 1156 { 1157 CPUS390XState *env = &cpu->env; 1158 int rc = 0; 1159 uint16_t ipbh0 = (run->s390_sieic.ipb & 0xffff0000) >> 16; 1160 1161 switch (ipa1) { 1162 case PRIV_B2_XSCH: 1163 ioinst_handle_xsch(cpu, env->regs[1], RA_IGNORED); 1164 break; 1165 case PRIV_B2_CSCH: 1166 ioinst_handle_csch(cpu, env->regs[1], RA_IGNORED); 1167 break; 1168 case PRIV_B2_HSCH: 1169 ioinst_handle_hsch(cpu, env->regs[1], RA_IGNORED); 1170 break; 1171 case PRIV_B2_MSCH: 1172 ioinst_handle_msch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED); 1173 break; 1174 case PRIV_B2_SSCH: 1175 ioinst_handle_ssch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED); 1176 break; 1177 case PRIV_B2_STCRW: 1178 ioinst_handle_stcrw(cpu, run->s390_sieic.ipb, RA_IGNORED); 1179 break; 1180 case PRIV_B2_STSCH: 1181 ioinst_handle_stsch(cpu, env->regs[1], run->s390_sieic.ipb, RA_IGNORED); 1182 break; 1183 case PRIV_B2_TSCH: 1184 /* We should only get tsch via KVM_EXIT_S390_TSCH. */ 1185 fprintf(stderr, "Spurious tsch intercept\n"); 1186 break; 1187 case PRIV_B2_CHSC: 1188 ioinst_handle_chsc(cpu, run->s390_sieic.ipb, RA_IGNORED); 1189 break; 1190 case PRIV_B2_TPI: 1191 /* This should have been handled by kvm already. */ 1192 fprintf(stderr, "Spurious tpi intercept\n"); 1193 break; 1194 case PRIV_B2_SCHM: 1195 ioinst_handle_schm(cpu, env->regs[1], env->regs[2], 1196 run->s390_sieic.ipb, RA_IGNORED); 1197 break; 1198 case PRIV_B2_RSCH: 1199 ioinst_handle_rsch(cpu, env->regs[1], RA_IGNORED); 1200 break; 1201 case PRIV_B2_RCHP: 1202 ioinst_handle_rchp(cpu, env->regs[1], RA_IGNORED); 1203 break; 1204 case PRIV_B2_STCPS: 1205 /* We do not provide this instruction, it is suppressed. */ 1206 break; 1207 case PRIV_B2_SAL: 1208 ioinst_handle_sal(cpu, env->regs[1], RA_IGNORED); 1209 break; 1210 case PRIV_B2_SIGA: 1211 /* Not provided, set CC = 3 for subchannel not operational */ 1212 setcc(cpu, 3); 1213 break; 1214 case PRIV_B2_SCLP_CALL: 1215 rc = kvm_sclp_service_call(cpu, run, ipbh0); 1216 break; 1217 default: 1218 rc = -1; 1219 DPRINTF("KVM: unhandled PRIV: 0xb2%x\n", ipa1); 1220 break; 1221 } 1222 1223 return rc; 1224 } 1225 1226 static uint64_t get_base_disp_rxy(S390CPU *cpu, struct kvm_run *run, 1227 uint8_t *ar) 1228 { 1229 CPUS390XState *env = &cpu->env; 1230 uint32_t x2 = (run->s390_sieic.ipa & 0x000f); 1231 uint32_t base2 = run->s390_sieic.ipb >> 28; 1232 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) + 1233 ((run->s390_sieic.ipb & 0xff00) << 4); 1234 1235 if (disp2 & 0x80000) { 1236 disp2 += 0xfff00000; 1237 } 1238 if (ar) { 1239 *ar = base2; 1240 } 1241 1242 return (base2 ? env->regs[base2] : 0) + 1243 (x2 ? env->regs[x2] : 0) + (long)(int)disp2; 1244 } 1245 1246 static uint64_t get_base_disp_rsy(S390CPU *cpu, struct kvm_run *run, 1247 uint8_t *ar) 1248 { 1249 CPUS390XState *env = &cpu->env; 1250 uint32_t base2 = run->s390_sieic.ipb >> 28; 1251 uint32_t disp2 = ((run->s390_sieic.ipb & 0x0fff0000) >> 16) + 1252 ((run->s390_sieic.ipb & 0xff00) << 4); 1253 1254 if (disp2 & 0x80000) { 1255 disp2 += 0xfff00000; 1256 } 1257 if (ar) { 1258 *ar = base2; 1259 } 1260 1261 return (base2 ? env->regs[base2] : 0) + (long)(int)disp2; 1262 } 1263 1264 static int kvm_clp_service_call(S390CPU *cpu, struct kvm_run *run) 1265 { 1266 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16; 1267 1268 if (s390_has_feat(S390_FEAT_ZPCI)) { 1269 return clp_service_call(cpu, r2, RA_IGNORED); 1270 } else { 1271 return -1; 1272 } 1273 } 1274 1275 static int kvm_pcilg_service_call(S390CPU *cpu, struct kvm_run *run) 1276 { 1277 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20; 1278 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16; 1279 1280 if (s390_has_feat(S390_FEAT_ZPCI)) { 1281 return pcilg_service_call(cpu, r1, r2, RA_IGNORED); 1282 } else { 1283 return -1; 1284 } 1285 } 1286 1287 static int kvm_pcistg_service_call(S390CPU *cpu, struct kvm_run *run) 1288 { 1289 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20; 1290 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16; 1291 1292 if (s390_has_feat(S390_FEAT_ZPCI)) { 1293 return pcistg_service_call(cpu, r1, r2, RA_IGNORED); 1294 } else { 1295 return -1; 1296 } 1297 } 1298 1299 static int kvm_stpcifc_service_call(S390CPU *cpu, struct kvm_run *run) 1300 { 1301 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4; 1302 uint64_t fiba; 1303 uint8_t ar; 1304 1305 if (s390_has_feat(S390_FEAT_ZPCI)) { 1306 fiba = get_base_disp_rxy(cpu, run, &ar); 1307 1308 return stpcifc_service_call(cpu, r1, fiba, ar, RA_IGNORED); 1309 } else { 1310 return -1; 1311 } 1312 } 1313 1314 static int kvm_sic_service_call(S390CPU *cpu, struct kvm_run *run) 1315 { 1316 CPUS390XState *env = &cpu->env; 1317 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4; 1318 uint8_t r3 = run->s390_sieic.ipa & 0x000f; 1319 uint8_t isc; 1320 uint16_t mode; 1321 int r; 1322 1323 mode = env->regs[r1] & 0xffff; 1324 isc = (env->regs[r3] >> 27) & 0x7; 1325 r = css_do_sic(env, isc, mode); 1326 if (r) { 1327 kvm_s390_program_interrupt(cpu, -r); 1328 } 1329 1330 return 0; 1331 } 1332 1333 static int kvm_rpcit_service_call(S390CPU *cpu, struct kvm_run *run) 1334 { 1335 uint8_t r1 = (run->s390_sieic.ipb & 0x00f00000) >> 20; 1336 uint8_t r2 = (run->s390_sieic.ipb & 0x000f0000) >> 16; 1337 1338 if (s390_has_feat(S390_FEAT_ZPCI)) { 1339 return rpcit_service_call(cpu, r1, r2, RA_IGNORED); 1340 } else { 1341 return -1; 1342 } 1343 } 1344 1345 static int kvm_pcistb_service_call(S390CPU *cpu, struct kvm_run *run) 1346 { 1347 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4; 1348 uint8_t r3 = run->s390_sieic.ipa & 0x000f; 1349 uint64_t gaddr; 1350 uint8_t ar; 1351 1352 if (s390_has_feat(S390_FEAT_ZPCI)) { 1353 gaddr = get_base_disp_rsy(cpu, run, &ar); 1354 1355 return pcistb_service_call(cpu, r1, r3, gaddr, ar, RA_IGNORED); 1356 } else { 1357 return -1; 1358 } 1359 } 1360 1361 static int kvm_mpcifc_service_call(S390CPU *cpu, struct kvm_run *run) 1362 { 1363 uint8_t r1 = (run->s390_sieic.ipa & 0x00f0) >> 4; 1364 uint64_t fiba; 1365 uint8_t ar; 1366 1367 if (s390_has_feat(S390_FEAT_ZPCI)) { 1368 fiba = get_base_disp_rxy(cpu, run, &ar); 1369 1370 return mpcifc_service_call(cpu, r1, fiba, ar, RA_IGNORED); 1371 } else { 1372 return -1; 1373 } 1374 } 1375 1376 static int handle_b9(S390CPU *cpu, struct kvm_run *run, uint8_t ipa1) 1377 { 1378 int r = 0; 1379 1380 switch (ipa1) { 1381 case PRIV_B9_CLP: 1382 r = kvm_clp_service_call(cpu, run); 1383 break; 1384 case PRIV_B9_PCISTG: 1385 r = kvm_pcistg_service_call(cpu, run); 1386 break; 1387 case PRIV_B9_PCILG: 1388 r = kvm_pcilg_service_call(cpu, run); 1389 break; 1390 case PRIV_B9_RPCIT: 1391 r = kvm_rpcit_service_call(cpu, run); 1392 break; 1393 case PRIV_B9_EQBS: 1394 /* just inject exception */ 1395 r = -1; 1396 break; 1397 default: 1398 r = -1; 1399 DPRINTF("KVM: unhandled PRIV: 0xb9%x\n", ipa1); 1400 break; 1401 } 1402 1403 return r; 1404 } 1405 1406 static int handle_eb(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl) 1407 { 1408 int r = 0; 1409 1410 switch (ipbl) { 1411 case PRIV_EB_PCISTB: 1412 r = kvm_pcistb_service_call(cpu, run); 1413 break; 1414 case PRIV_EB_SIC: 1415 r = kvm_sic_service_call(cpu, run); 1416 break; 1417 case PRIV_EB_SQBS: 1418 /* just inject exception */ 1419 r = -1; 1420 break; 1421 default: 1422 r = -1; 1423 DPRINTF("KVM: unhandled PRIV: 0xeb%x\n", ipbl); 1424 break; 1425 } 1426 1427 return r; 1428 } 1429 1430 static int handle_e3(S390CPU *cpu, struct kvm_run *run, uint8_t ipbl) 1431 { 1432 int r = 0; 1433 1434 switch (ipbl) { 1435 case PRIV_E3_MPCIFC: 1436 r = kvm_mpcifc_service_call(cpu, run); 1437 break; 1438 case PRIV_E3_STPCIFC: 1439 r = kvm_stpcifc_service_call(cpu, run); 1440 break; 1441 default: 1442 r = -1; 1443 DPRINTF("KVM: unhandled PRIV: 0xe3%x\n", ipbl); 1444 break; 1445 } 1446 1447 return r; 1448 } 1449 1450 static int handle_hypercall(S390CPU *cpu, struct kvm_run *run) 1451 { 1452 CPUS390XState *env = &cpu->env; 1453 int ret; 1454 1455 ret = s390_virtio_hypercall(env); 1456 if (ret == -EINVAL) { 1457 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION); 1458 return 0; 1459 } 1460 1461 return ret; 1462 } 1463 1464 static void kvm_handle_diag_288(S390CPU *cpu, struct kvm_run *run) 1465 { 1466 uint64_t r1, r3; 1467 int rc; 1468 1469 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4; 1470 r3 = run->s390_sieic.ipa & 0x000f; 1471 rc = handle_diag_288(&cpu->env, r1, r3); 1472 if (rc) { 1473 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION); 1474 } 1475 } 1476 1477 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run) 1478 { 1479 uint64_t r1, r3; 1480 1481 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4; 1482 r3 = run->s390_sieic.ipa & 0x000f; 1483 handle_diag_308(&cpu->env, r1, r3, RA_IGNORED); 1484 } 1485 1486 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run) 1487 { 1488 CPUS390XState *env = &cpu->env; 1489 unsigned long pc; 1490 1491 pc = env->psw.addr - sw_bp_ilen; 1492 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) { 1493 env->psw.addr = pc; 1494 return EXCP_DEBUG; 1495 } 1496 1497 return -ENOENT; 1498 } 1499 1500 #define DIAG_KVM_CODE_MASK 0x000000000000ffff 1501 1502 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb) 1503 { 1504 int r = 0; 1505 uint16_t func_code; 1506 1507 /* 1508 * For any diagnose call we support, bits 48-63 of the resulting 1509 * address specify the function code; the remainder is ignored. 1510 */ 1511 func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK; 1512 switch (func_code) { 1513 case DIAG_TIMEREVENT: 1514 kvm_handle_diag_288(cpu, run); 1515 break; 1516 case DIAG_IPL: 1517 kvm_handle_diag_308(cpu, run); 1518 break; 1519 case DIAG_KVM_HYPERCALL: 1520 r = handle_hypercall(cpu, run); 1521 break; 1522 case DIAG_KVM_BREAKPOINT: 1523 r = handle_sw_breakpoint(cpu, run); 1524 break; 1525 default: 1526 DPRINTF("KVM: unknown DIAG: 0x%x\n", func_code); 1527 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION); 1528 break; 1529 } 1530 1531 return r; 1532 } 1533 1534 static int kvm_s390_handle_sigp(S390CPU *cpu, uint8_t ipa1, uint32_t ipb) 1535 { 1536 CPUS390XState *env = &cpu->env; 1537 const uint8_t r1 = ipa1 >> 4; 1538 const uint8_t r3 = ipa1 & 0x0f; 1539 int ret; 1540 uint8_t order; 1541 1542 /* get order code */ 1543 order = decode_basedisp_rs(env, ipb, NULL) & SIGP_ORDER_MASK; 1544 1545 ret = handle_sigp(env, order, r1, r3); 1546 setcc(cpu, ret); 1547 return 0; 1548 } 1549 1550 static int handle_instruction(S390CPU *cpu, struct kvm_run *run) 1551 { 1552 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00); 1553 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff; 1554 int r = -1; 1555 1556 DPRINTF("handle_instruction 0x%x 0x%x\n", 1557 run->s390_sieic.ipa, run->s390_sieic.ipb); 1558 switch (ipa0) { 1559 case IPA0_B2: 1560 r = handle_b2(cpu, run, ipa1); 1561 break; 1562 case IPA0_B9: 1563 r = handle_b9(cpu, run, ipa1); 1564 break; 1565 case IPA0_EB: 1566 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff); 1567 break; 1568 case IPA0_E3: 1569 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff); 1570 break; 1571 case IPA0_DIAG: 1572 r = handle_diag(cpu, run, run->s390_sieic.ipb); 1573 break; 1574 case IPA0_SIGP: 1575 r = kvm_s390_handle_sigp(cpu, ipa1, run->s390_sieic.ipb); 1576 break; 1577 } 1578 1579 if (r < 0) { 1580 r = 0; 1581 kvm_s390_program_interrupt(cpu, PGM_OPERATION); 1582 } 1583 1584 return r; 1585 } 1586 1587 static void unmanageable_intercept(S390CPU *cpu, S390CrashReason reason, 1588 int pswoffset) 1589 { 1590 CPUState *cs = CPU(cpu); 1591 1592 s390_cpu_halt(cpu); 1593 cpu->env.crash_reason = reason; 1594 qemu_system_guest_panicked(cpu_get_crash_info(cs)); 1595 } 1596 1597 /* try to detect pgm check loops */ 1598 static int handle_oper_loop(S390CPU *cpu, struct kvm_run *run) 1599 { 1600 CPUState *cs = CPU(cpu); 1601 PSW oldpsw, newpsw; 1602 1603 newpsw.mask = ldq_phys(cs->as, cpu->env.psa + 1604 offsetof(LowCore, program_new_psw)); 1605 newpsw.addr = ldq_phys(cs->as, cpu->env.psa + 1606 offsetof(LowCore, program_new_psw) + 8); 1607 oldpsw.mask = run->psw_mask; 1608 oldpsw.addr = run->psw_addr; 1609 /* 1610 * Avoid endless loops of operation exceptions, if the pgm new 1611 * PSW will cause a new operation exception. 1612 * The heuristic checks if the pgm new psw is within 6 bytes before 1613 * the faulting psw address (with same DAT, AS settings) and the 1614 * new psw is not a wait psw and the fault was not triggered by 1615 * problem state. In that case go into crashed state. 1616 */ 1617 1618 if (oldpsw.addr - newpsw.addr <= 6 && 1619 !(newpsw.mask & PSW_MASK_WAIT) && 1620 !(oldpsw.mask & PSW_MASK_PSTATE) && 1621 (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) && 1622 (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT)) { 1623 unmanageable_intercept(cpu, S390_CRASH_REASON_OPINT_LOOP, 1624 offsetof(LowCore, program_new_psw)); 1625 return EXCP_HALTED; 1626 } 1627 return 0; 1628 } 1629 1630 static int handle_intercept(S390CPU *cpu) 1631 { 1632 CPUState *cs = CPU(cpu); 1633 struct kvm_run *run = cs->kvm_run; 1634 int icpt_code = run->s390_sieic.icptcode; 1635 int r = 0; 1636 1637 DPRINTF("intercept: 0x%x (at 0x%lx)\n", icpt_code, 1638 (long)cs->kvm_run->psw_addr); 1639 switch (icpt_code) { 1640 case ICPT_INSTRUCTION: 1641 r = handle_instruction(cpu, run); 1642 break; 1643 case ICPT_PROGRAM: 1644 unmanageable_intercept(cpu, S390_CRASH_REASON_PGMINT_LOOP, 1645 offsetof(LowCore, program_new_psw)); 1646 r = EXCP_HALTED; 1647 break; 1648 case ICPT_EXT_INT: 1649 unmanageable_intercept(cpu, S390_CRASH_REASON_EXTINT_LOOP, 1650 offsetof(LowCore, external_new_psw)); 1651 r = EXCP_HALTED; 1652 break; 1653 case ICPT_WAITPSW: 1654 /* disabled wait, since enabled wait is handled in kernel */ 1655 s390_handle_wait(cpu); 1656 r = EXCP_HALTED; 1657 break; 1658 case ICPT_CPU_STOP: 1659 do_stop_interrupt(&cpu->env); 1660 r = EXCP_HALTED; 1661 break; 1662 case ICPT_OPEREXC: 1663 /* check for break points */ 1664 r = handle_sw_breakpoint(cpu, run); 1665 if (r == -ENOENT) { 1666 /* Then check for potential pgm check loops */ 1667 r = handle_oper_loop(cpu, run); 1668 if (r == 0) { 1669 kvm_s390_program_interrupt(cpu, PGM_OPERATION); 1670 } 1671 } 1672 break; 1673 case ICPT_SOFT_INTERCEPT: 1674 fprintf(stderr, "KVM unimplemented icpt SOFT\n"); 1675 exit(1); 1676 break; 1677 case ICPT_IO: 1678 fprintf(stderr, "KVM unimplemented icpt IO\n"); 1679 exit(1); 1680 break; 1681 default: 1682 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code); 1683 exit(1); 1684 break; 1685 } 1686 1687 return r; 1688 } 1689 1690 static int handle_tsch(S390CPU *cpu) 1691 { 1692 CPUState *cs = CPU(cpu); 1693 struct kvm_run *run = cs->kvm_run; 1694 int ret; 1695 1696 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb, 1697 RA_IGNORED); 1698 if (ret < 0) { 1699 /* 1700 * Failure. 1701 * If an I/O interrupt had been dequeued, we have to reinject it. 1702 */ 1703 if (run->s390_tsch.dequeued) { 1704 s390_io_interrupt(run->s390_tsch.subchannel_id, 1705 run->s390_tsch.subchannel_nr, 1706 run->s390_tsch.io_int_parm, 1707 run->s390_tsch.io_int_word); 1708 } 1709 ret = 0; 1710 } 1711 return ret; 1712 } 1713 1714 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar) 1715 { 1716 SysIB_322 sysib; 1717 int del; 1718 1719 if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) { 1720 return; 1721 } 1722 /* Shift the stack of Extended Names to prepare for our own data */ 1723 memmove(&sysib.ext_names[1], &sysib.ext_names[0], 1724 sizeof(sysib.ext_names[0]) * (sysib.count - 1)); 1725 /* First virt level, that doesn't provide Ext Names delimits stack. It is 1726 * assumed it's not capable of managing Extended Names for lower levels. 1727 */ 1728 for (del = 1; del < sysib.count; del++) { 1729 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) { 1730 break; 1731 } 1732 } 1733 if (del < sysib.count) { 1734 memset(sysib.ext_names[del], 0, 1735 sizeof(sysib.ext_names[0]) * (sysib.count - del)); 1736 } 1737 /* Insert short machine name in EBCDIC, padded with blanks */ 1738 if (qemu_name) { 1739 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name)); 1740 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name), 1741 strlen(qemu_name))); 1742 } 1743 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */ 1744 memset(sysib.ext_names[0], 0, sizeof(sysib.ext_names[0])); 1745 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's 1746 * considered by s390 as not capable of providing any Extended Name. 1747 * Therefore if no name was specified on qemu invocation, we go with the 1748 * same "KVMguest" default, which KVM has filled into short name field. 1749 */ 1750 if (qemu_name) { 1751 strncpy((char *)sysib.ext_names[0], qemu_name, 1752 sizeof(sysib.ext_names[0])); 1753 } else { 1754 strcpy((char *)sysib.ext_names[0], "KVMguest"); 1755 } 1756 /* Insert UUID */ 1757 memcpy(sysib.vm[0].uuid, &qemu_uuid, sizeof(sysib.vm[0].uuid)); 1758 1759 s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib)); 1760 } 1761 1762 static int handle_stsi(S390CPU *cpu) 1763 { 1764 CPUState *cs = CPU(cpu); 1765 struct kvm_run *run = cs->kvm_run; 1766 1767 switch (run->s390_stsi.fc) { 1768 case 3: 1769 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) { 1770 return 0; 1771 } 1772 /* Only sysib 3.2.2 needs post-handling for now. */ 1773 insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar); 1774 return 0; 1775 default: 1776 return 0; 1777 } 1778 } 1779 1780 static int kvm_arch_handle_debug_exit(S390CPU *cpu) 1781 { 1782 CPUState *cs = CPU(cpu); 1783 struct kvm_run *run = cs->kvm_run; 1784 1785 int ret = 0; 1786 struct kvm_debug_exit_arch *arch_info = &run->debug.arch; 1787 1788 switch (arch_info->type) { 1789 case KVM_HW_WP_WRITE: 1790 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) { 1791 cs->watchpoint_hit = &hw_watchpoint; 1792 hw_watchpoint.vaddr = arch_info->addr; 1793 hw_watchpoint.flags = BP_MEM_WRITE; 1794 ret = EXCP_DEBUG; 1795 } 1796 break; 1797 case KVM_HW_BP: 1798 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) { 1799 ret = EXCP_DEBUG; 1800 } 1801 break; 1802 case KVM_SINGLESTEP: 1803 if (cs->singlestep_enabled) { 1804 ret = EXCP_DEBUG; 1805 } 1806 break; 1807 default: 1808 ret = -ENOSYS; 1809 } 1810 1811 return ret; 1812 } 1813 1814 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) 1815 { 1816 S390CPU *cpu = S390_CPU(cs); 1817 int ret = 0; 1818 1819 qemu_mutex_lock_iothread(); 1820 1821 kvm_cpu_synchronize_state(cs); 1822 1823 switch (run->exit_reason) { 1824 case KVM_EXIT_S390_SIEIC: 1825 ret = handle_intercept(cpu); 1826 break; 1827 case KVM_EXIT_S390_RESET: 1828 s390_ipl_reset_request(cs, S390_RESET_REIPL); 1829 break; 1830 case KVM_EXIT_S390_TSCH: 1831 ret = handle_tsch(cpu); 1832 break; 1833 case KVM_EXIT_S390_STSI: 1834 ret = handle_stsi(cpu); 1835 break; 1836 case KVM_EXIT_DEBUG: 1837 ret = kvm_arch_handle_debug_exit(cpu); 1838 break; 1839 default: 1840 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason); 1841 break; 1842 } 1843 qemu_mutex_unlock_iothread(); 1844 1845 if (ret == 0) { 1846 ret = EXCP_INTERRUPT; 1847 } 1848 return ret; 1849 } 1850 1851 bool kvm_arch_stop_on_emulation_error(CPUState *cpu) 1852 { 1853 return true; 1854 } 1855 1856 void kvm_s390_enable_css_support(S390CPU *cpu) 1857 { 1858 int r; 1859 1860 /* Activate host kernel channel subsystem support. */ 1861 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0); 1862 assert(r == 0); 1863 } 1864 1865 void kvm_arch_init_irq_routing(KVMState *s) 1866 { 1867 /* 1868 * Note that while irqchip capabilities generally imply that cpustates 1869 * are handled in-kernel, it is not true for s390 (yet); therefore, we 1870 * have to override the common code kvm_halt_in_kernel_allowed setting. 1871 */ 1872 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) { 1873 kvm_gsi_routing_allowed = true; 1874 kvm_halt_in_kernel_allowed = false; 1875 } 1876 } 1877 1878 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch, 1879 int vq, bool assign) 1880 { 1881 struct kvm_ioeventfd kick = { 1882 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY | 1883 KVM_IOEVENTFD_FLAG_DATAMATCH, 1884 .fd = event_notifier_get_fd(notifier), 1885 .datamatch = vq, 1886 .addr = sch, 1887 .len = 8, 1888 }; 1889 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) { 1890 return -ENOSYS; 1891 } 1892 if (!assign) { 1893 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN; 1894 } 1895 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick); 1896 } 1897 1898 int kvm_s390_get_ri(void) 1899 { 1900 return cap_ri; 1901 } 1902 1903 int kvm_s390_get_gs(void) 1904 { 1905 return cap_gs; 1906 } 1907 1908 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state) 1909 { 1910 struct kvm_mp_state mp_state = {}; 1911 int ret; 1912 1913 /* the kvm part might not have been initialized yet */ 1914 if (CPU(cpu)->kvm_state == NULL) { 1915 return 0; 1916 } 1917 1918 switch (cpu_state) { 1919 case S390_CPU_STATE_STOPPED: 1920 mp_state.mp_state = KVM_MP_STATE_STOPPED; 1921 break; 1922 case S390_CPU_STATE_CHECK_STOP: 1923 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP; 1924 break; 1925 case S390_CPU_STATE_OPERATING: 1926 mp_state.mp_state = KVM_MP_STATE_OPERATING; 1927 break; 1928 case S390_CPU_STATE_LOAD: 1929 mp_state.mp_state = KVM_MP_STATE_LOAD; 1930 break; 1931 default: 1932 error_report("Requested CPU state is not a valid S390 CPU state: %u", 1933 cpu_state); 1934 exit(1); 1935 } 1936 1937 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state); 1938 if (ret) { 1939 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state, 1940 strerror(-ret)); 1941 } 1942 1943 return ret; 1944 } 1945 1946 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu) 1947 { 1948 struct kvm_s390_irq_state irq_state = { 1949 .buf = (uint64_t) cpu->irqstate, 1950 .len = VCPU_IRQ_BUF_SIZE, 1951 }; 1952 CPUState *cs = CPU(cpu); 1953 int32_t bytes; 1954 1955 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) { 1956 return; 1957 } 1958 1959 bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state); 1960 if (bytes < 0) { 1961 cpu->irqstate_saved_size = 0; 1962 error_report("Migration of interrupt state failed"); 1963 return; 1964 } 1965 1966 cpu->irqstate_saved_size = bytes; 1967 } 1968 1969 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu) 1970 { 1971 CPUState *cs = CPU(cpu); 1972 struct kvm_s390_irq_state irq_state = { 1973 .buf = (uint64_t) cpu->irqstate, 1974 .len = cpu->irqstate_saved_size, 1975 }; 1976 int r; 1977 1978 if (cpu->irqstate_saved_size == 0) { 1979 return 0; 1980 } 1981 1982 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) { 1983 return -ENOSYS; 1984 } 1985 1986 r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state); 1987 if (r) { 1988 error_report("Setting interrupt state failed %d", r); 1989 } 1990 return r; 1991 } 1992 1993 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route, 1994 uint64_t address, uint32_t data, PCIDevice *dev) 1995 { 1996 S390PCIBusDevice *pbdev; 1997 uint32_t vec = data & ZPCI_MSI_VEC_MASK; 1998 1999 if (!dev) { 2000 DPRINTF("add_msi_route no pci device\n"); 2001 return -ENODEV; 2002 } 2003 2004 pbdev = s390_pci_find_dev_by_target(s390_get_phb(), DEVICE(dev)->id); 2005 if (!pbdev) { 2006 DPRINTF("add_msi_route no zpci device\n"); 2007 return -ENODEV; 2008 } 2009 2010 route->type = KVM_IRQ_ROUTING_S390_ADAPTER; 2011 route->flags = 0; 2012 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr; 2013 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr; 2014 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset; 2015 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset + vec; 2016 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id; 2017 return 0; 2018 } 2019 2020 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route, 2021 int vector, PCIDevice *dev) 2022 { 2023 return 0; 2024 } 2025 2026 int kvm_arch_release_virq_post(int virq) 2027 { 2028 return 0; 2029 } 2030 2031 int kvm_arch_msi_data_to_gsi(uint32_t data) 2032 { 2033 abort(); 2034 } 2035 2036 static int query_cpu_subfunc(S390FeatBitmap features) 2037 { 2038 struct kvm_s390_vm_cpu_subfunc prop; 2039 struct kvm_device_attr attr = { 2040 .group = KVM_S390_VM_CPU_MODEL, 2041 .attr = KVM_S390_VM_CPU_MACHINE_SUBFUNC, 2042 .addr = (uint64_t) &prop, 2043 }; 2044 int rc; 2045 2046 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr); 2047 if (rc) { 2048 return rc; 2049 } 2050 2051 /* 2052 * We're going to add all subfunctions now, if the corresponding feature 2053 * is available that unlocks the query functions. 2054 */ 2055 s390_add_from_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo); 2056 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) { 2057 s390_add_from_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff); 2058 } 2059 if (test_bit(S390_FEAT_MSA, features)) { 2060 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac); 2061 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc); 2062 s390_add_from_feat_block(features, S390_FEAT_TYPE_KM, prop.km); 2063 s390_add_from_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd); 2064 s390_add_from_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd); 2065 } 2066 if (test_bit(S390_FEAT_MSA_EXT_3, features)) { 2067 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo); 2068 } 2069 if (test_bit(S390_FEAT_MSA_EXT_4, features)) { 2070 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr); 2071 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf); 2072 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo); 2073 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc); 2074 } 2075 if (test_bit(S390_FEAT_MSA_EXT_5, features)) { 2076 s390_add_from_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno); 2077 } 2078 if (test_bit(S390_FEAT_MSA_EXT_8, features)) { 2079 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma); 2080 } 2081 return 0; 2082 } 2083 2084 static int configure_cpu_subfunc(const S390FeatBitmap features) 2085 { 2086 struct kvm_s390_vm_cpu_subfunc prop = {}; 2087 struct kvm_device_attr attr = { 2088 .group = KVM_S390_VM_CPU_MODEL, 2089 .attr = KVM_S390_VM_CPU_PROCESSOR_SUBFUNC, 2090 .addr = (uint64_t) &prop, 2091 }; 2092 2093 if (!kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL, 2094 KVM_S390_VM_CPU_PROCESSOR_SUBFUNC)) { 2095 /* hardware support might be missing, IBC will handle most of this */ 2096 return 0; 2097 } 2098 2099 s390_fill_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo); 2100 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) { 2101 s390_fill_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff); 2102 } 2103 if (test_bit(S390_FEAT_MSA, features)) { 2104 s390_fill_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac); 2105 s390_fill_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc); 2106 s390_fill_feat_block(features, S390_FEAT_TYPE_KM, prop.km); 2107 s390_fill_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd); 2108 s390_fill_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd); 2109 } 2110 if (test_bit(S390_FEAT_MSA_EXT_3, features)) { 2111 s390_fill_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo); 2112 } 2113 if (test_bit(S390_FEAT_MSA_EXT_4, features)) { 2114 s390_fill_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr); 2115 s390_fill_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf); 2116 s390_fill_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo); 2117 s390_fill_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc); 2118 } 2119 if (test_bit(S390_FEAT_MSA_EXT_5, features)) { 2120 s390_fill_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno); 2121 } 2122 if (test_bit(S390_FEAT_MSA_EXT_8, features)) { 2123 s390_fill_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma); 2124 } 2125 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr); 2126 } 2127 2128 static int kvm_to_feat[][2] = { 2129 { KVM_S390_VM_CPU_FEAT_ESOP, S390_FEAT_ESOP }, 2130 { KVM_S390_VM_CPU_FEAT_SIEF2, S390_FEAT_SIE_F2 }, 2131 { KVM_S390_VM_CPU_FEAT_64BSCAO , S390_FEAT_SIE_64BSCAO }, 2132 { KVM_S390_VM_CPU_FEAT_SIIF, S390_FEAT_SIE_SIIF }, 2133 { KVM_S390_VM_CPU_FEAT_GPERE, S390_FEAT_SIE_GPERE }, 2134 { KVM_S390_VM_CPU_FEAT_GSLS, S390_FEAT_SIE_GSLS }, 2135 { KVM_S390_VM_CPU_FEAT_IB, S390_FEAT_SIE_IB }, 2136 { KVM_S390_VM_CPU_FEAT_CEI, S390_FEAT_SIE_CEI }, 2137 { KVM_S390_VM_CPU_FEAT_IBS, S390_FEAT_SIE_IBS }, 2138 { KVM_S390_VM_CPU_FEAT_SKEY, S390_FEAT_SIE_SKEY }, 2139 { KVM_S390_VM_CPU_FEAT_CMMA, S390_FEAT_SIE_CMMA }, 2140 { KVM_S390_VM_CPU_FEAT_PFMFI, S390_FEAT_SIE_PFMFI}, 2141 { KVM_S390_VM_CPU_FEAT_SIGPIF, S390_FEAT_SIE_SIGPIF}, 2142 { KVM_S390_VM_CPU_FEAT_KSS, S390_FEAT_SIE_KSS}, 2143 }; 2144 2145 static int query_cpu_feat(S390FeatBitmap features) 2146 { 2147 struct kvm_s390_vm_cpu_feat prop; 2148 struct kvm_device_attr attr = { 2149 .group = KVM_S390_VM_CPU_MODEL, 2150 .attr = KVM_S390_VM_CPU_MACHINE_FEAT, 2151 .addr = (uint64_t) &prop, 2152 }; 2153 int rc; 2154 int i; 2155 2156 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr); 2157 if (rc) { 2158 return rc; 2159 } 2160 2161 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) { 2162 if (test_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat)) { 2163 set_bit(kvm_to_feat[i][1], features); 2164 } 2165 } 2166 return 0; 2167 } 2168 2169 static int configure_cpu_feat(const S390FeatBitmap features) 2170 { 2171 struct kvm_s390_vm_cpu_feat prop = {}; 2172 struct kvm_device_attr attr = { 2173 .group = KVM_S390_VM_CPU_MODEL, 2174 .attr = KVM_S390_VM_CPU_PROCESSOR_FEAT, 2175 .addr = (uint64_t) &prop, 2176 }; 2177 int i; 2178 2179 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) { 2180 if (test_bit(kvm_to_feat[i][1], features)) { 2181 set_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat); 2182 } 2183 } 2184 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr); 2185 } 2186 2187 bool kvm_s390_cpu_models_supported(void) 2188 { 2189 if (!cpu_model_allowed()) { 2190 /* compatibility machines interfere with the cpu model */ 2191 return false; 2192 } 2193 return kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL, 2194 KVM_S390_VM_CPU_MACHINE) && 2195 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL, 2196 KVM_S390_VM_CPU_PROCESSOR) && 2197 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL, 2198 KVM_S390_VM_CPU_MACHINE_FEAT) && 2199 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL, 2200 KVM_S390_VM_CPU_PROCESSOR_FEAT) && 2201 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL, 2202 KVM_S390_VM_CPU_MACHINE_SUBFUNC); 2203 } 2204 2205 void kvm_s390_get_host_cpu_model(S390CPUModel *model, Error **errp) 2206 { 2207 struct kvm_s390_vm_cpu_machine prop = {}; 2208 struct kvm_device_attr attr = { 2209 .group = KVM_S390_VM_CPU_MODEL, 2210 .attr = KVM_S390_VM_CPU_MACHINE, 2211 .addr = (uint64_t) &prop, 2212 }; 2213 uint16_t unblocked_ibc = 0, cpu_type = 0; 2214 int rc; 2215 2216 memset(model, 0, sizeof(*model)); 2217 2218 if (!kvm_s390_cpu_models_supported()) { 2219 error_setg(errp, "KVM doesn't support CPU models"); 2220 return; 2221 } 2222 2223 /* query the basic cpu model properties */ 2224 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr); 2225 if (rc) { 2226 error_setg(errp, "KVM: Error querying host CPU model: %d", rc); 2227 return; 2228 } 2229 2230 cpu_type = cpuid_type(prop.cpuid); 2231 if (has_ibc(prop.ibc)) { 2232 model->lowest_ibc = lowest_ibc(prop.ibc); 2233 unblocked_ibc = unblocked_ibc(prop.ibc); 2234 } 2235 model->cpu_id = cpuid_id(prop.cpuid); 2236 model->cpu_id_format = cpuid_format(prop.cpuid); 2237 model->cpu_ver = 0xff; 2238 2239 /* get supported cpu features indicated via STFL(E) */ 2240 s390_add_from_feat_block(model->features, S390_FEAT_TYPE_STFL, 2241 (uint8_t *) prop.fac_mask); 2242 /* dat-enhancement facility 2 has no bit but was introduced with stfle */ 2243 if (test_bit(S390_FEAT_STFLE, model->features)) { 2244 set_bit(S390_FEAT_DAT_ENH_2, model->features); 2245 } 2246 /* get supported cpu features indicated e.g. via SCLP */ 2247 rc = query_cpu_feat(model->features); 2248 if (rc) { 2249 error_setg(errp, "KVM: Error querying CPU features: %d", rc); 2250 return; 2251 } 2252 /* get supported cpu subfunctions indicated via query / test bit */ 2253 rc = query_cpu_subfunc(model->features); 2254 if (rc) { 2255 error_setg(errp, "KVM: Error querying CPU subfunctions: %d", rc); 2256 return; 2257 } 2258 2259 /* PTFF subfunctions might be indicated although kernel support missing */ 2260 if (!test_bit(S390_FEAT_MULTIPLE_EPOCH, model->features)) { 2261 clear_bit(S390_FEAT_PTFF_QSIE, model->features); 2262 clear_bit(S390_FEAT_PTFF_QTOUE, model->features); 2263 clear_bit(S390_FEAT_PTFF_STOE, model->features); 2264 clear_bit(S390_FEAT_PTFF_STOUE, model->features); 2265 } 2266 2267 /* with cpu model support, CMM is only indicated if really available */ 2268 if (kvm_s390_cmma_available()) { 2269 set_bit(S390_FEAT_CMM, model->features); 2270 } else { 2271 /* no cmm -> no cmm nt */ 2272 clear_bit(S390_FEAT_CMM_NT, model->features); 2273 } 2274 2275 /* bpb needs kernel support for migration, VSIE and reset */ 2276 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_BPB)) { 2277 clear_bit(S390_FEAT_BPB, model->features); 2278 } 2279 2280 /* We emulate a zPCI bus and AEN, therefore we don't need HW support */ 2281 if (pci_available) { 2282 set_bit(S390_FEAT_ZPCI, model->features); 2283 } 2284 set_bit(S390_FEAT_ADAPTER_EVENT_NOTIFICATION, model->features); 2285 2286 if (s390_known_cpu_type(cpu_type)) { 2287 /* we want the exact model, even if some features are missing */ 2288 model->def = s390_find_cpu_def(cpu_type, ibc_gen(unblocked_ibc), 2289 ibc_ec_ga(unblocked_ibc), NULL); 2290 } else { 2291 /* model unknown, e.g. too new - search using features */ 2292 model->def = s390_find_cpu_def(0, ibc_gen(unblocked_ibc), 2293 ibc_ec_ga(unblocked_ibc), 2294 model->features); 2295 } 2296 if (!model->def) { 2297 error_setg(errp, "KVM: host CPU model could not be identified"); 2298 return; 2299 } 2300 /* strip of features that are not part of the maximum model */ 2301 bitmap_and(model->features, model->features, model->def->full_feat, 2302 S390_FEAT_MAX); 2303 } 2304 2305 void kvm_s390_apply_cpu_model(const S390CPUModel *model, Error **errp) 2306 { 2307 struct kvm_s390_vm_cpu_processor prop = { 2308 .fac_list = { 0 }, 2309 }; 2310 struct kvm_device_attr attr = { 2311 .group = KVM_S390_VM_CPU_MODEL, 2312 .attr = KVM_S390_VM_CPU_PROCESSOR, 2313 .addr = (uint64_t) &prop, 2314 }; 2315 int rc; 2316 2317 if (!model) { 2318 /* compatibility handling if cpu models are disabled */ 2319 if (kvm_s390_cmma_available()) { 2320 kvm_s390_enable_cmma(); 2321 } 2322 return; 2323 } 2324 if (!kvm_s390_cpu_models_supported()) { 2325 error_setg(errp, "KVM doesn't support CPU models"); 2326 return; 2327 } 2328 prop.cpuid = s390_cpuid_from_cpu_model(model); 2329 prop.ibc = s390_ibc_from_cpu_model(model); 2330 /* configure cpu features indicated via STFL(e) */ 2331 s390_fill_feat_block(model->features, S390_FEAT_TYPE_STFL, 2332 (uint8_t *) prop.fac_list); 2333 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr); 2334 if (rc) { 2335 error_setg(errp, "KVM: Error configuring the CPU model: %d", rc); 2336 return; 2337 } 2338 /* configure cpu features indicated e.g. via SCLP */ 2339 rc = configure_cpu_feat(model->features); 2340 if (rc) { 2341 error_setg(errp, "KVM: Error configuring CPU features: %d", rc); 2342 return; 2343 } 2344 /* configure cpu subfunctions indicated via query / test bit */ 2345 rc = configure_cpu_subfunc(model->features); 2346 if (rc) { 2347 error_setg(errp, "KVM: Error configuring CPU subfunctions: %d", rc); 2348 return; 2349 } 2350 /* enable CMM via CMMA */ 2351 if (test_bit(S390_FEAT_CMM, model->features)) { 2352 kvm_s390_enable_cmma(); 2353 } 2354 } 2355 2356 void kvm_s390_restart_interrupt(S390CPU *cpu) 2357 { 2358 struct kvm_s390_irq irq = { 2359 .type = KVM_S390_RESTART, 2360 }; 2361 2362 kvm_s390_vcpu_interrupt(cpu, &irq); 2363 } 2364 2365 void kvm_s390_stop_interrupt(S390CPU *cpu) 2366 { 2367 struct kvm_s390_irq irq = { 2368 .type = KVM_S390_SIGP_STOP, 2369 }; 2370 2371 kvm_s390_vcpu_interrupt(cpu, &irq); 2372 } 2373