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