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