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