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