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