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