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