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