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