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