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