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-virtio-hcall.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 int handle_hypercall(S390CPU *cpu, struct kvm_run *run) 1496 { 1497 CPUS390XState *env = &cpu->env; 1498 int ret = -EINVAL; 1499 1500 #ifdef CONFIG_S390_CCW_VIRTIO 1501 ret = s390_virtio_hypercall(env); 1502 #endif /* CONFIG_S390_CCW_VIRTIO */ 1503 if (ret == -EINVAL) { 1504 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION); 1505 return 0; 1506 } 1507 1508 return ret; 1509 } 1510 1511 static void kvm_handle_diag_288(S390CPU *cpu, struct kvm_run *run) 1512 { 1513 uint64_t r1, r3; 1514 int rc; 1515 1516 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4; 1517 r3 = run->s390_sieic.ipa & 0x000f; 1518 rc = handle_diag_288(&cpu->env, r1, r3); 1519 if (rc) { 1520 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION); 1521 } 1522 } 1523 1524 static void kvm_handle_diag_308(S390CPU *cpu, struct kvm_run *run) 1525 { 1526 uint64_t r1, r3; 1527 1528 r1 = (run->s390_sieic.ipa & 0x00f0) >> 4; 1529 r3 = run->s390_sieic.ipa & 0x000f; 1530 handle_diag_308(&cpu->env, r1, r3, RA_IGNORED); 1531 } 1532 1533 static int handle_sw_breakpoint(S390CPU *cpu, struct kvm_run *run) 1534 { 1535 CPUS390XState *env = &cpu->env; 1536 unsigned long pc; 1537 1538 pc = env->psw.addr - sw_bp_ilen; 1539 if (kvm_find_sw_breakpoint(CPU(cpu), pc)) { 1540 env->psw.addr = pc; 1541 return EXCP_DEBUG; 1542 } 1543 1544 return -ENOENT; 1545 } 1546 1547 void kvm_s390_set_diag318(CPUState *cs, uint64_t diag318_info) 1548 { 1549 CPUS390XState *env = &S390_CPU(cs)->env; 1550 1551 /* Feat bit is set only if KVM supports sync for diag318 */ 1552 if (s390_has_feat(S390_FEAT_DIAG_318)) { 1553 env->diag318_info = diag318_info; 1554 cs->kvm_run->s.regs.diag318 = diag318_info; 1555 cs->kvm_run->kvm_dirty_regs |= KVM_SYNC_DIAG318; 1556 /* 1557 * diag 318 info is zeroed during a clear reset and 1558 * diag 308 IPL subcodes. 1559 */ 1560 } 1561 } 1562 1563 static void handle_diag_318(S390CPU *cpu, struct kvm_run *run) 1564 { 1565 uint64_t reg = (run->s390_sieic.ipa & 0x00f0) >> 4; 1566 uint64_t diag318_info = run->s.regs.gprs[reg]; 1567 CPUState *t; 1568 1569 /* 1570 * DIAG 318 can only be enabled with KVM support. As such, let's 1571 * ensure a guest cannot execute this instruction erroneously. 1572 */ 1573 if (!s390_has_feat(S390_FEAT_DIAG_318)) { 1574 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION); 1575 return; 1576 } 1577 1578 CPU_FOREACH(t) { 1579 run_on_cpu(t, s390_do_cpu_set_diag318, 1580 RUN_ON_CPU_HOST_ULONG(diag318_info)); 1581 } 1582 } 1583 1584 #define DIAG_KVM_CODE_MASK 0x000000000000ffff 1585 1586 static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb) 1587 { 1588 int r = 0; 1589 uint16_t func_code; 1590 1591 /* 1592 * For any diagnose call we support, bits 48-63 of the resulting 1593 * address specify the function code; the remainder is ignored. 1594 */ 1595 func_code = decode_basedisp_rs(&cpu->env, ipb, NULL) & DIAG_KVM_CODE_MASK; 1596 switch (func_code) { 1597 case DIAG_TIMEREVENT: 1598 kvm_handle_diag_288(cpu, run); 1599 break; 1600 case DIAG_IPL: 1601 kvm_handle_diag_308(cpu, run); 1602 break; 1603 case DIAG_SET_CONTROL_PROGRAM_CODES: 1604 handle_diag_318(cpu, run); 1605 break; 1606 case DIAG_KVM_HYPERCALL: 1607 r = handle_hypercall(cpu, run); 1608 break; 1609 case DIAG_KVM_BREAKPOINT: 1610 r = handle_sw_breakpoint(cpu, run); 1611 break; 1612 default: 1613 trace_kvm_insn_diag(func_code); 1614 kvm_s390_program_interrupt(cpu, PGM_SPECIFICATION); 1615 break; 1616 } 1617 1618 return r; 1619 } 1620 1621 static int kvm_s390_handle_sigp(S390CPU *cpu, uint8_t ipa1, uint32_t ipb) 1622 { 1623 CPUS390XState *env = &cpu->env; 1624 const uint8_t r1 = ipa1 >> 4; 1625 const uint8_t r3 = ipa1 & 0x0f; 1626 int ret; 1627 uint8_t order; 1628 1629 /* get order code */ 1630 order = decode_basedisp_rs(env, ipb, NULL) & SIGP_ORDER_MASK; 1631 1632 ret = handle_sigp(env, order, r1, r3); 1633 setcc(cpu, ret); 1634 return 0; 1635 } 1636 1637 static int handle_instruction(S390CPU *cpu, struct kvm_run *run) 1638 { 1639 unsigned int ipa0 = (run->s390_sieic.ipa & 0xff00); 1640 uint8_t ipa1 = run->s390_sieic.ipa & 0x00ff; 1641 int r = -1; 1642 1643 trace_kvm_insn(run->s390_sieic.ipa, run->s390_sieic.ipb); 1644 switch (ipa0) { 1645 case IPA0_B2: 1646 r = handle_b2(cpu, run, ipa1); 1647 break; 1648 case IPA0_B9: 1649 r = handle_b9(cpu, run, ipa1); 1650 break; 1651 case IPA0_EB: 1652 r = handle_eb(cpu, run, run->s390_sieic.ipb & 0xff); 1653 break; 1654 case IPA0_E3: 1655 r = handle_e3(cpu, run, run->s390_sieic.ipb & 0xff); 1656 break; 1657 case IPA0_DIAG: 1658 r = handle_diag(cpu, run, run->s390_sieic.ipb); 1659 break; 1660 case IPA0_SIGP: 1661 r = kvm_s390_handle_sigp(cpu, ipa1, run->s390_sieic.ipb); 1662 break; 1663 } 1664 1665 if (r < 0) { 1666 r = 0; 1667 kvm_s390_program_interrupt(cpu, PGM_OPERATION); 1668 } 1669 1670 return r; 1671 } 1672 1673 static void unmanageable_intercept(S390CPU *cpu, S390CrashReason reason, 1674 int pswoffset) 1675 { 1676 CPUState *cs = CPU(cpu); 1677 1678 s390_cpu_halt(cpu); 1679 cpu->env.crash_reason = reason; 1680 qemu_system_guest_panicked(cpu_get_crash_info(cs)); 1681 } 1682 1683 /* try to detect pgm check loops */ 1684 static int handle_oper_loop(S390CPU *cpu, struct kvm_run *run) 1685 { 1686 CPUState *cs = CPU(cpu); 1687 PSW oldpsw, newpsw; 1688 1689 newpsw.mask = ldq_phys(cs->as, cpu->env.psa + 1690 offsetof(LowCore, program_new_psw)); 1691 newpsw.addr = ldq_phys(cs->as, cpu->env.psa + 1692 offsetof(LowCore, program_new_psw) + 8); 1693 oldpsw.mask = run->psw_mask; 1694 oldpsw.addr = run->psw_addr; 1695 /* 1696 * Avoid endless loops of operation exceptions, if the pgm new 1697 * PSW will cause a new operation exception. 1698 * The heuristic checks if the pgm new psw is within 6 bytes before 1699 * the faulting psw address (with same DAT, AS settings) and the 1700 * new psw is not a wait psw and the fault was not triggered by 1701 * problem state. In that case go into crashed state. 1702 */ 1703 1704 if (oldpsw.addr - newpsw.addr <= 6 && 1705 !(newpsw.mask & PSW_MASK_WAIT) && 1706 !(oldpsw.mask & PSW_MASK_PSTATE) && 1707 (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) && 1708 (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT)) { 1709 unmanageable_intercept(cpu, S390_CRASH_REASON_OPINT_LOOP, 1710 offsetof(LowCore, program_new_psw)); 1711 return EXCP_HALTED; 1712 } 1713 return 0; 1714 } 1715 1716 static int handle_intercept(S390CPU *cpu) 1717 { 1718 CPUState *cs = CPU(cpu); 1719 struct kvm_run *run = cs->kvm_run; 1720 int icpt_code = run->s390_sieic.icptcode; 1721 int r = 0; 1722 1723 trace_kvm_intercept(icpt_code, (long)run->psw_addr); 1724 switch (icpt_code) { 1725 case ICPT_INSTRUCTION: 1726 case ICPT_PV_INSTR: 1727 case ICPT_PV_INSTR_NOTIFICATION: 1728 r = handle_instruction(cpu, run); 1729 break; 1730 case ICPT_PROGRAM: 1731 unmanageable_intercept(cpu, S390_CRASH_REASON_PGMINT_LOOP, 1732 offsetof(LowCore, program_new_psw)); 1733 r = EXCP_HALTED; 1734 break; 1735 case ICPT_EXT_INT: 1736 unmanageable_intercept(cpu, S390_CRASH_REASON_EXTINT_LOOP, 1737 offsetof(LowCore, external_new_psw)); 1738 r = EXCP_HALTED; 1739 break; 1740 case ICPT_WAITPSW: 1741 /* disabled wait, since enabled wait is handled in kernel */ 1742 s390_handle_wait(cpu); 1743 r = EXCP_HALTED; 1744 break; 1745 case ICPT_CPU_STOP: 1746 do_stop_interrupt(&cpu->env); 1747 r = EXCP_HALTED; 1748 break; 1749 case ICPT_OPEREXC: 1750 /* check for break points */ 1751 r = handle_sw_breakpoint(cpu, run); 1752 if (r == -ENOENT) { 1753 /* Then check for potential pgm check loops */ 1754 r = handle_oper_loop(cpu, run); 1755 if (r == 0) { 1756 kvm_s390_program_interrupt(cpu, PGM_OPERATION); 1757 } 1758 } 1759 break; 1760 case ICPT_SOFT_INTERCEPT: 1761 fprintf(stderr, "KVM unimplemented icpt SOFT\n"); 1762 exit(1); 1763 break; 1764 case ICPT_IO: 1765 fprintf(stderr, "KVM unimplemented icpt IO\n"); 1766 exit(1); 1767 break; 1768 default: 1769 fprintf(stderr, "Unknown intercept code: %d\n", icpt_code); 1770 exit(1); 1771 break; 1772 } 1773 1774 return r; 1775 } 1776 1777 static int handle_tsch(S390CPU *cpu) 1778 { 1779 CPUState *cs = CPU(cpu); 1780 struct kvm_run *run = cs->kvm_run; 1781 int ret; 1782 1783 ret = ioinst_handle_tsch(cpu, cpu->env.regs[1], run->s390_tsch.ipb, 1784 RA_IGNORED); 1785 if (ret < 0) { 1786 /* 1787 * Failure. 1788 * If an I/O interrupt had been dequeued, we have to reinject it. 1789 */ 1790 if (run->s390_tsch.dequeued) { 1791 s390_io_interrupt(run->s390_tsch.subchannel_id, 1792 run->s390_tsch.subchannel_nr, 1793 run->s390_tsch.io_int_parm, 1794 run->s390_tsch.io_int_word); 1795 } 1796 ret = 0; 1797 } 1798 return ret; 1799 } 1800 1801 static void insert_stsi_3_2_2(S390CPU *cpu, __u64 addr, uint8_t ar) 1802 { 1803 const MachineState *ms = MACHINE(qdev_get_machine()); 1804 uint16_t conf_cpus = 0, reserved_cpus = 0; 1805 SysIB_322 sysib; 1806 int del, i; 1807 1808 if (s390_is_pv()) { 1809 s390_cpu_pv_mem_read(cpu, 0, &sysib, sizeof(sysib)); 1810 } else if (s390_cpu_virt_mem_read(cpu, addr, ar, &sysib, sizeof(sysib))) { 1811 return; 1812 } 1813 /* Shift the stack of Extended Names to prepare for our own data */ 1814 memmove(&sysib.ext_names[1], &sysib.ext_names[0], 1815 sizeof(sysib.ext_names[0]) * (sysib.count - 1)); 1816 /* First virt level, that doesn't provide Ext Names delimits stack. It is 1817 * assumed it's not capable of managing Extended Names for lower levels. 1818 */ 1819 for (del = 1; del < sysib.count; del++) { 1820 if (!sysib.vm[del].ext_name_encoding || !sysib.ext_names[del][0]) { 1821 break; 1822 } 1823 } 1824 if (del < sysib.count) { 1825 memset(sysib.ext_names[del], 0, 1826 sizeof(sysib.ext_names[0]) * (sysib.count - del)); 1827 } 1828 1829 /* count the cpus and split them into configured and reserved ones */ 1830 for (i = 0; i < ms->possible_cpus->len; i++) { 1831 if (ms->possible_cpus->cpus[i].cpu) { 1832 conf_cpus++; 1833 } else { 1834 reserved_cpus++; 1835 } 1836 } 1837 sysib.vm[0].total_cpus = conf_cpus + reserved_cpus; 1838 sysib.vm[0].conf_cpus = conf_cpus; 1839 sysib.vm[0].reserved_cpus = reserved_cpus; 1840 1841 /* Insert short machine name in EBCDIC, padded with blanks */ 1842 if (qemu_name) { 1843 memset(sysib.vm[0].name, 0x40, sizeof(sysib.vm[0].name)); 1844 ebcdic_put(sysib.vm[0].name, qemu_name, MIN(sizeof(sysib.vm[0].name), 1845 strlen(qemu_name))); 1846 } 1847 sysib.vm[0].ext_name_encoding = 2; /* 2 = UTF-8 */ 1848 /* If hypervisor specifies zero Extended Name in STSI322 SYSIB, it's 1849 * considered by s390 as not capable of providing any Extended Name. 1850 * Therefore if no name was specified on qemu invocation, we go with the 1851 * same "KVMguest" default, which KVM has filled into short name field. 1852 */ 1853 strpadcpy((char *)sysib.ext_names[0], 1854 sizeof(sysib.ext_names[0]), 1855 qemu_name ?: "KVMguest", '\0'); 1856 1857 /* Insert UUID */ 1858 memcpy(sysib.vm[0].uuid, &qemu_uuid, sizeof(sysib.vm[0].uuid)); 1859 1860 if (s390_is_pv()) { 1861 s390_cpu_pv_mem_write(cpu, 0, &sysib, sizeof(sysib)); 1862 } else { 1863 s390_cpu_virt_mem_write(cpu, addr, ar, &sysib, sizeof(sysib)); 1864 } 1865 } 1866 1867 static int handle_stsi(S390CPU *cpu) 1868 { 1869 CPUState *cs = CPU(cpu); 1870 struct kvm_run *run = cs->kvm_run; 1871 1872 switch (run->s390_stsi.fc) { 1873 case 3: 1874 if (run->s390_stsi.sel1 != 2 || run->s390_stsi.sel2 != 2) { 1875 return 0; 1876 } 1877 insert_stsi_3_2_2(cpu, run->s390_stsi.addr, run->s390_stsi.ar); 1878 return 0; 1879 case 15: 1880 insert_stsi_15_1_x(cpu, run->s390_stsi.sel2, run->s390_stsi.addr, 1881 run->s390_stsi.ar, RA_IGNORED); 1882 return 0; 1883 default: 1884 return 0; 1885 } 1886 } 1887 1888 static int kvm_arch_handle_debug_exit(S390CPU *cpu) 1889 { 1890 CPUState *cs = CPU(cpu); 1891 struct kvm_run *run = cs->kvm_run; 1892 1893 int ret = 0; 1894 struct kvm_debug_exit_arch *arch_info = &run->debug.arch; 1895 1896 switch (arch_info->type) { 1897 case KVM_HW_WP_WRITE: 1898 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) { 1899 cs->watchpoint_hit = &hw_watchpoint; 1900 hw_watchpoint.vaddr = arch_info->addr; 1901 hw_watchpoint.flags = BP_MEM_WRITE; 1902 ret = EXCP_DEBUG; 1903 } 1904 break; 1905 case KVM_HW_BP: 1906 if (find_hw_breakpoint(arch_info->addr, -1, arch_info->type)) { 1907 ret = EXCP_DEBUG; 1908 } 1909 break; 1910 case KVM_SINGLESTEP: 1911 if (cs->singlestep_enabled) { 1912 ret = EXCP_DEBUG; 1913 } 1914 break; 1915 default: 1916 ret = -ENOSYS; 1917 } 1918 1919 return ret; 1920 } 1921 1922 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) 1923 { 1924 S390CPU *cpu = S390_CPU(cs); 1925 int ret = 0; 1926 1927 bql_lock(); 1928 1929 kvm_cpu_synchronize_state(cs); 1930 1931 switch (run->exit_reason) { 1932 case KVM_EXIT_S390_SIEIC: 1933 ret = handle_intercept(cpu); 1934 break; 1935 case KVM_EXIT_S390_RESET: 1936 s390_ipl_reset_request(cs, S390_RESET_REIPL); 1937 break; 1938 case KVM_EXIT_S390_TSCH: 1939 ret = handle_tsch(cpu); 1940 break; 1941 case KVM_EXIT_S390_STSI: 1942 ret = handle_stsi(cpu); 1943 break; 1944 case KVM_EXIT_DEBUG: 1945 ret = kvm_arch_handle_debug_exit(cpu); 1946 break; 1947 default: 1948 fprintf(stderr, "Unknown KVM exit: %d\n", run->exit_reason); 1949 break; 1950 } 1951 bql_unlock(); 1952 1953 if (ret == 0) { 1954 ret = EXCP_INTERRUPT; 1955 } 1956 return ret; 1957 } 1958 1959 bool kvm_arch_stop_on_emulation_error(CPUState *cpu) 1960 { 1961 return true; 1962 } 1963 1964 void kvm_s390_enable_css_support(S390CPU *cpu) 1965 { 1966 int r; 1967 1968 /* Activate host kernel channel subsystem support. */ 1969 r = kvm_vcpu_enable_cap(CPU(cpu), KVM_CAP_S390_CSS_SUPPORT, 0); 1970 assert(r == 0); 1971 } 1972 1973 void kvm_arch_init_irq_routing(KVMState *s) 1974 { 1975 /* 1976 * Note that while irqchip capabilities generally imply that cpustates 1977 * are handled in-kernel, it is not true for s390 (yet); therefore, we 1978 * have to override the common code kvm_halt_in_kernel_allowed setting. 1979 */ 1980 if (kvm_check_extension(s, KVM_CAP_IRQ_ROUTING)) { 1981 kvm_gsi_routing_allowed = true; 1982 kvm_halt_in_kernel_allowed = false; 1983 } 1984 } 1985 1986 int kvm_s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch, 1987 int vq, bool assign) 1988 { 1989 struct kvm_ioeventfd kick = { 1990 .flags = KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY | 1991 KVM_IOEVENTFD_FLAG_DATAMATCH, 1992 .fd = event_notifier_get_fd(notifier), 1993 .datamatch = vq, 1994 .addr = sch, 1995 .len = 8, 1996 }; 1997 trace_kvm_assign_subch_ioeventfd(kick.fd, kick.addr, assign, 1998 kick.datamatch); 1999 if (!kvm_check_extension(kvm_state, KVM_CAP_IOEVENTFD)) { 2000 return -ENOSYS; 2001 } 2002 if (!assign) { 2003 kick.flags |= KVM_IOEVENTFD_FLAG_DEASSIGN; 2004 } 2005 return kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &kick); 2006 } 2007 2008 int kvm_s390_get_protected_dump(void) 2009 { 2010 return cap_protected_dump; 2011 } 2012 2013 int kvm_s390_get_ri(void) 2014 { 2015 return cap_ri; 2016 } 2017 2018 int kvm_s390_set_cpu_state(S390CPU *cpu, uint8_t cpu_state) 2019 { 2020 struct kvm_mp_state mp_state = {}; 2021 int ret; 2022 2023 /* the kvm part might not have been initialized yet */ 2024 if (CPU(cpu)->kvm_state == NULL) { 2025 return 0; 2026 } 2027 2028 switch (cpu_state) { 2029 case S390_CPU_STATE_STOPPED: 2030 mp_state.mp_state = KVM_MP_STATE_STOPPED; 2031 break; 2032 case S390_CPU_STATE_CHECK_STOP: 2033 mp_state.mp_state = KVM_MP_STATE_CHECK_STOP; 2034 break; 2035 case S390_CPU_STATE_OPERATING: 2036 mp_state.mp_state = KVM_MP_STATE_OPERATING; 2037 break; 2038 case S390_CPU_STATE_LOAD: 2039 mp_state.mp_state = KVM_MP_STATE_LOAD; 2040 break; 2041 default: 2042 error_report("Requested CPU state is not a valid S390 CPU state: %u", 2043 cpu_state); 2044 exit(1); 2045 } 2046 2047 ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_MP_STATE, &mp_state); 2048 if (ret) { 2049 trace_kvm_failed_cpu_state_set(CPU(cpu)->cpu_index, cpu_state, 2050 strerror(-ret)); 2051 } 2052 2053 return ret; 2054 } 2055 2056 void kvm_s390_vcpu_interrupt_pre_save(S390CPU *cpu) 2057 { 2058 unsigned int max_cpus = MACHINE(qdev_get_machine())->smp.max_cpus; 2059 struct kvm_s390_irq_state irq_state = { 2060 .buf = (uint64_t) cpu->irqstate, 2061 .len = VCPU_IRQ_BUF_SIZE(max_cpus), 2062 }; 2063 CPUState *cs = CPU(cpu); 2064 int32_t bytes; 2065 2066 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) { 2067 return; 2068 } 2069 2070 bytes = kvm_vcpu_ioctl(cs, KVM_S390_GET_IRQ_STATE, &irq_state); 2071 if (bytes < 0) { 2072 cpu->irqstate_saved_size = 0; 2073 error_report("Migration of interrupt state failed"); 2074 return; 2075 } 2076 2077 cpu->irqstate_saved_size = bytes; 2078 } 2079 2080 int kvm_s390_vcpu_interrupt_post_load(S390CPU *cpu) 2081 { 2082 CPUState *cs = CPU(cpu); 2083 struct kvm_s390_irq_state irq_state = { 2084 .buf = (uint64_t) cpu->irqstate, 2085 .len = cpu->irqstate_saved_size, 2086 }; 2087 int r; 2088 2089 if (cpu->irqstate_saved_size == 0) { 2090 return 0; 2091 } 2092 2093 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_IRQ_STATE)) { 2094 return -ENOSYS; 2095 } 2096 2097 r = kvm_vcpu_ioctl(cs, KVM_S390_SET_IRQ_STATE, &irq_state); 2098 if (r) { 2099 error_report("Setting interrupt state failed %d", r); 2100 } 2101 return r; 2102 } 2103 2104 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route, 2105 uint64_t address, uint32_t data, PCIDevice *dev) 2106 { 2107 S390PCIBusDevice *pbdev; 2108 uint32_t vec = data & ZPCI_MSI_VEC_MASK; 2109 2110 if (!dev) { 2111 trace_kvm_msi_route_fixup("no pci device"); 2112 return -ENODEV; 2113 } 2114 2115 pbdev = s390_pci_find_dev_by_target(s390_get_phb(), DEVICE(dev)->id); 2116 if (!pbdev) { 2117 trace_kvm_msi_route_fixup("no zpci device"); 2118 return -ENODEV; 2119 } 2120 2121 route->type = KVM_IRQ_ROUTING_S390_ADAPTER; 2122 route->flags = 0; 2123 route->u.adapter.summary_addr = pbdev->routes.adapter.summary_addr; 2124 route->u.adapter.ind_addr = pbdev->routes.adapter.ind_addr; 2125 route->u.adapter.summary_offset = pbdev->routes.adapter.summary_offset; 2126 route->u.adapter.ind_offset = pbdev->routes.adapter.ind_offset + vec; 2127 route->u.adapter.adapter_id = pbdev->routes.adapter.adapter_id; 2128 return 0; 2129 } 2130 2131 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route, 2132 int vector, PCIDevice *dev) 2133 { 2134 return 0; 2135 } 2136 2137 int kvm_arch_release_virq_post(int virq) 2138 { 2139 return 0; 2140 } 2141 2142 int kvm_arch_msi_data_to_gsi(uint32_t data) 2143 { 2144 abort(); 2145 } 2146 2147 static int query_cpu_subfunc(S390FeatBitmap features) 2148 { 2149 struct kvm_s390_vm_cpu_subfunc prop = {}; 2150 struct kvm_device_attr attr = { 2151 .group = KVM_S390_VM_CPU_MODEL, 2152 .attr = KVM_S390_VM_CPU_MACHINE_SUBFUNC, 2153 .addr = (uint64_t) &prop, 2154 }; 2155 int rc; 2156 2157 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr); 2158 if (rc) { 2159 return rc; 2160 } 2161 2162 /* 2163 * We're going to add all subfunctions now, if the corresponding feature 2164 * is available that unlocks the query functions. 2165 */ 2166 s390_add_from_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo); 2167 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) { 2168 s390_add_from_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff); 2169 } 2170 if (test_bit(S390_FEAT_MSA, features)) { 2171 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac); 2172 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc); 2173 s390_add_from_feat_block(features, S390_FEAT_TYPE_KM, prop.km); 2174 s390_add_from_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd); 2175 s390_add_from_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd); 2176 } 2177 if (test_bit(S390_FEAT_MSA_EXT_3, features)) { 2178 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo); 2179 } 2180 if (test_bit(S390_FEAT_MSA_EXT_4, features)) { 2181 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr); 2182 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf); 2183 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo); 2184 s390_add_from_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc); 2185 } 2186 if (test_bit(S390_FEAT_MSA_EXT_5, features)) { 2187 s390_add_from_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno); 2188 } 2189 if (test_bit(S390_FEAT_MSA_EXT_8, features)) { 2190 s390_add_from_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma); 2191 } 2192 if (test_bit(S390_FEAT_MSA_EXT_9, features)) { 2193 s390_add_from_feat_block(features, S390_FEAT_TYPE_KDSA, prop.kdsa); 2194 } 2195 if (test_bit(S390_FEAT_ESORT_BASE, features)) { 2196 s390_add_from_feat_block(features, S390_FEAT_TYPE_SORTL, prop.sortl); 2197 } 2198 if (test_bit(S390_FEAT_DEFLATE_BASE, features)) { 2199 s390_add_from_feat_block(features, S390_FEAT_TYPE_DFLTCC, prop.dfltcc); 2200 } 2201 if (test_bit(S390_FEAT_CCF_BASE, features)) { 2202 s390_add_from_feat_block(features, S390_FEAT_TYPE_PFCR, prop.pfcr); 2203 } 2204 return 0; 2205 } 2206 2207 static int configure_cpu_subfunc(const S390FeatBitmap features) 2208 { 2209 struct kvm_s390_vm_cpu_subfunc prop = {}; 2210 struct kvm_device_attr attr = { 2211 .group = KVM_S390_VM_CPU_MODEL, 2212 .attr = KVM_S390_VM_CPU_PROCESSOR_SUBFUNC, 2213 .addr = (uint64_t) &prop, 2214 }; 2215 2216 if (!kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL, 2217 KVM_S390_VM_CPU_PROCESSOR_SUBFUNC)) { 2218 /* hardware support might be missing, IBC will handle most of this */ 2219 return 0; 2220 } 2221 2222 s390_fill_feat_block(features, S390_FEAT_TYPE_PLO, prop.plo); 2223 if (test_bit(S390_FEAT_TOD_CLOCK_STEERING, features)) { 2224 s390_fill_feat_block(features, S390_FEAT_TYPE_PTFF, prop.ptff); 2225 } 2226 if (test_bit(S390_FEAT_MSA, features)) { 2227 s390_fill_feat_block(features, S390_FEAT_TYPE_KMAC, prop.kmac); 2228 s390_fill_feat_block(features, S390_FEAT_TYPE_KMC, prop.kmc); 2229 s390_fill_feat_block(features, S390_FEAT_TYPE_KM, prop.km); 2230 s390_fill_feat_block(features, S390_FEAT_TYPE_KIMD, prop.kimd); 2231 s390_fill_feat_block(features, S390_FEAT_TYPE_KLMD, prop.klmd); 2232 } 2233 if (test_bit(S390_FEAT_MSA_EXT_3, features)) { 2234 s390_fill_feat_block(features, S390_FEAT_TYPE_PCKMO, prop.pckmo); 2235 } 2236 if (test_bit(S390_FEAT_MSA_EXT_4, features)) { 2237 s390_fill_feat_block(features, S390_FEAT_TYPE_KMCTR, prop.kmctr); 2238 s390_fill_feat_block(features, S390_FEAT_TYPE_KMF, prop.kmf); 2239 s390_fill_feat_block(features, S390_FEAT_TYPE_KMO, prop.kmo); 2240 s390_fill_feat_block(features, S390_FEAT_TYPE_PCC, prop.pcc); 2241 } 2242 if (test_bit(S390_FEAT_MSA_EXT_5, features)) { 2243 s390_fill_feat_block(features, S390_FEAT_TYPE_PPNO, prop.ppno); 2244 } 2245 if (test_bit(S390_FEAT_MSA_EXT_8, features)) { 2246 s390_fill_feat_block(features, S390_FEAT_TYPE_KMA, prop.kma); 2247 } 2248 if (test_bit(S390_FEAT_MSA_EXT_9, features)) { 2249 s390_fill_feat_block(features, S390_FEAT_TYPE_KDSA, prop.kdsa); 2250 } 2251 if (test_bit(S390_FEAT_ESORT_BASE, features)) { 2252 s390_fill_feat_block(features, S390_FEAT_TYPE_SORTL, prop.sortl); 2253 } 2254 if (test_bit(S390_FEAT_DEFLATE_BASE, features)) { 2255 s390_fill_feat_block(features, S390_FEAT_TYPE_DFLTCC, prop.dfltcc); 2256 } 2257 if (test_bit(S390_FEAT_CCF_BASE, features)) { 2258 s390_fill_feat_block(features, S390_FEAT_TYPE_PFCR, prop.pfcr); 2259 } 2260 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr); 2261 } 2262 2263 static bool ap_available(void) 2264 { 2265 return kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, 2266 KVM_S390_VM_CRYPTO_ENABLE_APIE); 2267 } 2268 2269 static bool ap_enabled(const S390FeatBitmap features) 2270 { 2271 return test_bit(S390_FEAT_AP, features); 2272 } 2273 2274 static bool uv_feat_supported(void) 2275 { 2276 return kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL, 2277 KVM_S390_VM_CPU_PROCESSOR_UV_FEAT_GUEST); 2278 } 2279 2280 static int query_uv_feat_guest(S390FeatBitmap features) 2281 { 2282 struct kvm_s390_vm_cpu_uv_feat prop = {}; 2283 struct kvm_device_attr attr = { 2284 .group = KVM_S390_VM_CPU_MODEL, 2285 .attr = KVM_S390_VM_CPU_MACHINE_UV_FEAT_GUEST, 2286 .addr = (uint64_t) &prop, 2287 }; 2288 int rc; 2289 2290 /* AP support check is currently the only user of the UV feature test */ 2291 if (!(uv_feat_supported() && ap_available())) { 2292 return 0; 2293 } 2294 2295 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr); 2296 if (rc) { 2297 return rc; 2298 } 2299 2300 if (prop.ap) { 2301 set_bit(S390_FEAT_UV_FEAT_AP, features); 2302 } 2303 if (prop.ap_intr) { 2304 set_bit(S390_FEAT_UV_FEAT_AP_INTR, features); 2305 } 2306 2307 return 0; 2308 } 2309 2310 static int kvm_to_feat[][2] = { 2311 { KVM_S390_VM_CPU_FEAT_ESOP, S390_FEAT_ESOP }, 2312 { KVM_S390_VM_CPU_FEAT_SIEF2, S390_FEAT_SIE_F2 }, 2313 { KVM_S390_VM_CPU_FEAT_64BSCAO , S390_FEAT_SIE_64BSCAO }, 2314 { KVM_S390_VM_CPU_FEAT_SIIF, S390_FEAT_SIE_SIIF }, 2315 { KVM_S390_VM_CPU_FEAT_GPERE, S390_FEAT_SIE_GPERE }, 2316 { KVM_S390_VM_CPU_FEAT_GSLS, S390_FEAT_SIE_GSLS }, 2317 { KVM_S390_VM_CPU_FEAT_IB, S390_FEAT_SIE_IB }, 2318 { KVM_S390_VM_CPU_FEAT_CEI, S390_FEAT_SIE_CEI }, 2319 { KVM_S390_VM_CPU_FEAT_IBS, S390_FEAT_SIE_IBS }, 2320 { KVM_S390_VM_CPU_FEAT_SKEY, S390_FEAT_SIE_SKEY }, 2321 { KVM_S390_VM_CPU_FEAT_CMMA, S390_FEAT_SIE_CMMA }, 2322 { KVM_S390_VM_CPU_FEAT_PFMFI, S390_FEAT_SIE_PFMFI}, 2323 { KVM_S390_VM_CPU_FEAT_SIGPIF, S390_FEAT_SIE_SIGPIF}, 2324 { KVM_S390_VM_CPU_FEAT_KSS, S390_FEAT_SIE_KSS}, 2325 }; 2326 2327 static int query_cpu_feat(S390FeatBitmap features) 2328 { 2329 struct kvm_s390_vm_cpu_feat prop = {}; 2330 struct kvm_device_attr attr = { 2331 .group = KVM_S390_VM_CPU_MODEL, 2332 .attr = KVM_S390_VM_CPU_MACHINE_FEAT, 2333 .addr = (uint64_t) &prop, 2334 }; 2335 int rc; 2336 int i; 2337 2338 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr); 2339 if (rc) { 2340 return rc; 2341 } 2342 2343 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) { 2344 if (test_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat)) { 2345 set_bit(kvm_to_feat[i][1], features); 2346 } 2347 } 2348 return 0; 2349 } 2350 2351 static int configure_cpu_feat(const S390FeatBitmap features) 2352 { 2353 struct kvm_s390_vm_cpu_feat prop = {}; 2354 struct kvm_device_attr attr = { 2355 .group = KVM_S390_VM_CPU_MODEL, 2356 .attr = KVM_S390_VM_CPU_PROCESSOR_FEAT, 2357 .addr = (uint64_t) &prop, 2358 }; 2359 int i; 2360 2361 for (i = 0; i < ARRAY_SIZE(kvm_to_feat); i++) { 2362 if (test_bit(kvm_to_feat[i][1], features)) { 2363 set_be_bit(kvm_to_feat[i][0], (uint8_t *) prop.feat); 2364 } 2365 } 2366 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr); 2367 } 2368 2369 bool kvm_s390_cpu_models_supported(void) 2370 { 2371 if (!cpu_model_allowed()) { 2372 /* compatibility machines interfere with the cpu model */ 2373 return false; 2374 } 2375 return kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL, 2376 KVM_S390_VM_CPU_MACHINE) && 2377 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL, 2378 KVM_S390_VM_CPU_PROCESSOR) && 2379 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL, 2380 KVM_S390_VM_CPU_MACHINE_FEAT) && 2381 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL, 2382 KVM_S390_VM_CPU_PROCESSOR_FEAT) && 2383 kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_MODEL, 2384 KVM_S390_VM_CPU_MACHINE_SUBFUNC); 2385 } 2386 2387 bool kvm_s390_get_host_cpu_model(S390CPUModel *model, Error **errp) 2388 { 2389 struct kvm_s390_vm_cpu_machine prop = {}; 2390 struct kvm_device_attr attr = { 2391 .group = KVM_S390_VM_CPU_MODEL, 2392 .attr = KVM_S390_VM_CPU_MACHINE, 2393 .addr = (uint64_t) &prop, 2394 }; 2395 uint16_t unblocked_ibc = 0, cpu_type = 0; 2396 int rc; 2397 2398 memset(model, 0, sizeof(*model)); 2399 2400 if (!kvm_s390_cpu_models_supported()) { 2401 error_setg(errp, "KVM doesn't support CPU models"); 2402 return false; 2403 } 2404 2405 /* query the basic cpu model properties */ 2406 rc = kvm_vm_ioctl(kvm_state, KVM_GET_DEVICE_ATTR, &attr); 2407 if (rc) { 2408 error_setg(errp, "KVM: Error querying host CPU model: %d", rc); 2409 return false; 2410 } 2411 2412 cpu_type = cpuid_type(prop.cpuid); 2413 if (has_ibc(prop.ibc)) { 2414 model->lowest_ibc = lowest_ibc(prop.ibc); 2415 unblocked_ibc = unblocked_ibc(prop.ibc); 2416 } 2417 model->cpu_id = cpuid_id(prop.cpuid); 2418 model->cpu_id_format = cpuid_format(prop.cpuid); 2419 model->cpu_ver = 0xff; 2420 2421 /* get supported cpu features indicated via STFL(E) */ 2422 s390_add_from_feat_block(model->features, S390_FEAT_TYPE_STFL, 2423 (uint8_t *) prop.fac_mask); 2424 /* dat-enhancement facility 2 has no bit but was introduced with stfle */ 2425 if (test_bit(S390_FEAT_STFLE, model->features)) { 2426 set_bit(S390_FEAT_DAT_ENH_2, model->features); 2427 } 2428 /* get supported cpu features indicated e.g. via SCLP */ 2429 rc = query_cpu_feat(model->features); 2430 if (rc) { 2431 error_setg(errp, "KVM: Error querying CPU features: %d", rc); 2432 return false; 2433 } 2434 /* get supported cpu subfunctions indicated via query / test bit */ 2435 rc = query_cpu_subfunc(model->features); 2436 if (rc) { 2437 error_setg(errp, "KVM: Error querying CPU subfunctions: %d", rc); 2438 return false; 2439 } 2440 2441 /* PTFF subfunctions might be indicated although kernel support missing */ 2442 if (!test_bit(S390_FEAT_MULTIPLE_EPOCH, model->features)) { 2443 clear_bit(S390_FEAT_PTFF_QSIE, model->features); 2444 clear_bit(S390_FEAT_PTFF_QTOUE, model->features); 2445 clear_bit(S390_FEAT_PTFF_STOE, model->features); 2446 clear_bit(S390_FEAT_PTFF_STOUE, model->features); 2447 } 2448 2449 /* with cpu model support, CMM is only indicated if really available */ 2450 if (kvm_s390_cmma_available()) { 2451 set_bit(S390_FEAT_CMM, model->features); 2452 } else { 2453 /* no cmm -> no cmm nt */ 2454 clear_bit(S390_FEAT_CMM_NT, model->features); 2455 } 2456 2457 /* bpb needs kernel support for migration, VSIE and reset */ 2458 if (!kvm_check_extension(kvm_state, KVM_CAP_S390_BPB)) { 2459 clear_bit(S390_FEAT_BPB, model->features); 2460 } 2461 2462 /* 2463 * If we have support for protected virtualization, indicate 2464 * the protected virtualization IPL unpack facility. 2465 */ 2466 if (cap_protected) { 2467 set_bit(S390_FEAT_UNPACK, model->features); 2468 } 2469 2470 /* 2471 * If we have kernel support for CPU Topology indicate the 2472 * configuration-topology facility. 2473 */ 2474 if (kvm_check_extension(kvm_state, KVM_CAP_S390_CPU_TOPOLOGY)) { 2475 set_bit(S390_FEAT_CONFIGURATION_TOPOLOGY, model->features); 2476 } 2477 2478 /* We emulate a zPCI bus and AEN, therefore we don't need HW support */ 2479 set_bit(S390_FEAT_ZPCI, model->features); 2480 set_bit(S390_FEAT_ADAPTER_EVENT_NOTIFICATION, model->features); 2481 2482 if (s390_known_cpu_type(cpu_type)) { 2483 /* we want the exact model, even if some features are missing */ 2484 model->def = s390_find_cpu_def(cpu_type, ibc_gen(unblocked_ibc), 2485 ibc_ec_ga(unblocked_ibc), NULL); 2486 } else { 2487 /* model unknown, e.g. too new - search using features */ 2488 model->def = s390_find_cpu_def(0, ibc_gen(unblocked_ibc), 2489 ibc_ec_ga(unblocked_ibc), 2490 model->features); 2491 } 2492 if (!model->def) { 2493 error_setg(errp, "KVM: host CPU model could not be identified"); 2494 return false; 2495 } 2496 /* for now, we can only provide the AP feature with HW support */ 2497 if (ap_available()) { 2498 set_bit(S390_FEAT_AP, model->features); 2499 } 2500 2501 /* 2502 * Extended-Length SCCB is handled entirely within QEMU. 2503 * For PV guests this is completely fenced by the Ultravisor, as Service 2504 * Call error checking and STFLE interpretation are handled via SIE. 2505 */ 2506 set_bit(S390_FEAT_EXTENDED_LENGTH_SCCB, model->features); 2507 2508 if (kvm_check_extension(kvm_state, KVM_CAP_S390_DIAG318)) { 2509 set_bit(S390_FEAT_DIAG_318, model->features); 2510 } 2511 2512 /* Test for Ultravisor features that influence secure guest behavior */ 2513 query_uv_feat_guest(model->features); 2514 2515 /* strip of features that are not part of the maximum model */ 2516 bitmap_and(model->features, model->features, model->def->full_feat, 2517 S390_FEAT_MAX); 2518 return true; 2519 } 2520 2521 static int configure_uv_feat_guest(const S390FeatBitmap features) 2522 { 2523 struct kvm_s390_vm_cpu_uv_feat uv_feat = {}; 2524 struct kvm_device_attr attribute = { 2525 .group = KVM_S390_VM_CPU_MODEL, 2526 .attr = KVM_S390_VM_CPU_PROCESSOR_UV_FEAT_GUEST, 2527 .addr = (__u64) &uv_feat, 2528 }; 2529 2530 /* AP support check is currently the only user of the UV feature test */ 2531 if (!(uv_feat_supported() && ap_enabled(features))) { 2532 return 0; 2533 } 2534 2535 if (test_bit(S390_FEAT_UV_FEAT_AP, features)) { 2536 uv_feat.ap = 1; 2537 } 2538 if (test_bit(S390_FEAT_UV_FEAT_AP_INTR, features)) { 2539 uv_feat.ap_intr = 1; 2540 } 2541 2542 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute); 2543 } 2544 2545 static void kvm_s390_configure_apie(bool interpret) 2546 { 2547 uint64_t attr = interpret ? KVM_S390_VM_CRYPTO_ENABLE_APIE : 2548 KVM_S390_VM_CRYPTO_DISABLE_APIE; 2549 2550 if (kvm_vm_check_attr(kvm_state, KVM_S390_VM_CRYPTO, attr)) { 2551 kvm_s390_set_crypto_attr(attr); 2552 } 2553 } 2554 2555 bool kvm_s390_apply_cpu_model(const S390CPUModel *model, Error **errp) 2556 { 2557 struct kvm_s390_vm_cpu_processor prop = { 2558 .fac_list = { 0 }, 2559 }; 2560 struct kvm_device_attr attr = { 2561 .group = KVM_S390_VM_CPU_MODEL, 2562 .attr = KVM_S390_VM_CPU_PROCESSOR, 2563 .addr = (uint64_t) &prop, 2564 }; 2565 int rc; 2566 2567 if (!model) { 2568 /* compatibility handling if cpu models are disabled */ 2569 if (kvm_s390_cmma_available()) { 2570 kvm_s390_enable_cmma(); 2571 } 2572 return true; 2573 } 2574 if (!kvm_s390_cpu_models_supported()) { 2575 error_setg(errp, "KVM doesn't support CPU models"); 2576 return false; 2577 } 2578 prop.cpuid = s390_cpuid_from_cpu_model(model); 2579 prop.ibc = s390_ibc_from_cpu_model(model); 2580 /* configure cpu features indicated via STFL(e) */ 2581 s390_fill_feat_block(model->features, S390_FEAT_TYPE_STFL, 2582 (uint8_t *) prop.fac_list); 2583 rc = kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attr); 2584 if (rc) { 2585 error_setg(errp, "KVM: Error configuring the CPU model: %d", rc); 2586 return false; 2587 } 2588 /* configure cpu features indicated e.g. via SCLP */ 2589 rc = configure_cpu_feat(model->features); 2590 if (rc) { 2591 error_setg(errp, "KVM: Error configuring CPU features: %d", rc); 2592 return false; 2593 } 2594 /* configure cpu subfunctions indicated via query / test bit */ 2595 rc = configure_cpu_subfunc(model->features); 2596 if (rc) { 2597 error_setg(errp, "KVM: Error configuring CPU subfunctions: %d", rc); 2598 return false; 2599 } 2600 /* enable CMM via CMMA */ 2601 if (test_bit(S390_FEAT_CMM, model->features)) { 2602 kvm_s390_enable_cmma(); 2603 } 2604 2605 if (ap_enabled(model->features)) { 2606 kvm_s390_configure_apie(true); 2607 } 2608 2609 /* configure UV-features for the guest indicated via query / test_bit */ 2610 rc = configure_uv_feat_guest(model->features); 2611 if (rc) { 2612 error_setg(errp, "KVM: Error configuring CPU UV features %d", rc); 2613 return false; 2614 } 2615 return true; 2616 } 2617 2618 void kvm_s390_restart_interrupt(S390CPU *cpu) 2619 { 2620 struct kvm_s390_irq irq = { 2621 .type = KVM_S390_RESTART, 2622 }; 2623 2624 kvm_s390_vcpu_interrupt(cpu, &irq); 2625 } 2626 2627 void kvm_s390_stop_interrupt(S390CPU *cpu) 2628 { 2629 struct kvm_s390_irq irq = { 2630 .type = KVM_S390_SIGP_STOP, 2631 }; 2632 2633 kvm_s390_vcpu_interrupt(cpu, &irq); 2634 } 2635 2636 int kvm_s390_get_zpci_op(void) 2637 { 2638 return cap_zpci_op; 2639 } 2640 2641 int kvm_s390_topology_set_mtcr(uint64_t attr) 2642 { 2643 struct kvm_device_attr attribute = { 2644 .group = KVM_S390_VM_CPU_TOPOLOGY, 2645 .attr = attr, 2646 }; 2647 2648 if (!s390_has_feat(S390_FEAT_CONFIGURATION_TOPOLOGY)) { 2649 return 0; 2650 } 2651 if (!kvm_vm_check_attr(kvm_state, KVM_S390_VM_CPU_TOPOLOGY, attr)) { 2652 return -ENOTSUP; 2653 } 2654 2655 return kvm_vm_ioctl(kvm_state, KVM_SET_DEVICE_ATTR, &attribute); 2656 } 2657 2658 void kvm_arch_accel_class_init(ObjectClass *oc) 2659 { 2660 } 2661