qemu/target/s390x/misc_helper.c
<<
>>
Prefs
   1/*
   2 *  S/390 misc helper routines
   3 *
   4 *  Copyright (c) 2009 Ulrich Hecht
   5 *  Copyright (c) 2009 Alexander Graf
   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 * You should have received a copy of the GNU Lesser General Public
  18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  19 */
  20
  21#include "qemu/osdep.h"
  22#include "qemu/main-loop.h"
  23#include "cpu.h"
  24#include "internal.h"
  25#include "exec/memory.h"
  26#include "qemu/host-utils.h"
  27#include "exec/helper-proto.h"
  28#include "qemu/timer.h"
  29#include "exec/exec-all.h"
  30#include "exec/cpu_ldst.h"
  31#include "qapi/error.h"
  32#include "tcg_s390x.h"
  33
  34#if !defined(CONFIG_USER_ONLY)
  35#include "sysemu/cpus.h"
  36#include "sysemu/sysemu.h"
  37#include "hw/s390x/ebcdic.h"
  38#include "hw/s390x/s390-virtio-hcall.h"
  39#include "hw/s390x/sclp.h"
  40#include "hw/s390x/s390_flic.h"
  41#include "hw/s390x/ioinst.h"
  42#include "hw/s390x/s390-pci-inst.h"
  43#include "hw/boards.h"
  44#include "hw/s390x/tod.h"
  45#endif
  46
  47/* #define DEBUG_HELPER */
  48#ifdef DEBUG_HELPER
  49#define HELPER_LOG(x...) qemu_log(x)
  50#else
  51#define HELPER_LOG(x...)
  52#endif
  53
  54/* Raise an exception statically from a TB.  */
  55void HELPER(exception)(CPUS390XState *env, uint32_t excp)
  56{
  57    CPUState *cs = CPU(s390_env_get_cpu(env));
  58
  59    HELPER_LOG("%s: exception %d\n", __func__, excp);
  60    cs->exception_index = excp;
  61    cpu_loop_exit(cs);
  62}
  63
  64/* Store CPU Timer (also used for EXTRACT CPU TIME) */
  65uint64_t HELPER(stpt)(CPUS390XState *env)
  66{
  67#if defined(CONFIG_USER_ONLY)
  68    /*
  69     * Fake a descending CPU timer. We could get negative values here,
  70     * but we don't care as it is up to the OS when to process that
  71     * interrupt and reset to > 0.
  72     */
  73    return UINT64_MAX - (uint64_t)cpu_get_host_ticks();
  74#else
  75    return time2tod(env->cputm - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  76#endif
  77}
  78
  79#ifndef CONFIG_USER_ONLY
  80
  81/* SCLP service call */
  82uint32_t HELPER(servc)(CPUS390XState *env, uint64_t r1, uint64_t r2)
  83{
  84    qemu_mutex_lock_iothread();
  85    int r = sclp_service_call(env, r1, r2);
  86    qemu_mutex_unlock_iothread();
  87    if (r < 0) {
  88        s390_program_interrupt(env, -r, 4, GETPC());
  89    }
  90    return r;
  91}
  92
  93void HELPER(diag)(CPUS390XState *env, uint32_t r1, uint32_t r3, uint32_t num)
  94{
  95    uint64_t r;
  96
  97    switch (num) {
  98    case 0x500:
  99        /* KVM hypercall */
 100        qemu_mutex_lock_iothread();
 101        r = s390_virtio_hypercall(env);
 102        qemu_mutex_unlock_iothread();
 103        break;
 104    case 0x44:
 105        /* yield */
 106        r = 0;
 107        break;
 108    case 0x308:
 109        /* ipl */
 110        qemu_mutex_lock_iothread();
 111        handle_diag_308(env, r1, r3, GETPC());
 112        qemu_mutex_unlock_iothread();
 113        r = 0;
 114        break;
 115    case 0x288:
 116        /* time bomb (watchdog) */
 117        r = handle_diag_288(env, r1, r3);
 118        break;
 119    default:
 120        r = -1;
 121        break;
 122    }
 123
 124    if (r) {
 125        s390_program_interrupt(env, PGM_SPECIFICATION, ILEN_AUTO, GETPC());
 126    }
 127}
 128
 129/* Set Prefix */
 130void HELPER(spx)(CPUS390XState *env, uint64_t a1)
 131{
 132    CPUState *cs = CPU(s390_env_get_cpu(env));
 133    uint32_t prefix = a1 & 0x7fffe000;
 134
 135    env->psa = prefix;
 136    HELPER_LOG("prefix: %#x\n", prefix);
 137    tlb_flush_page(cs, 0);
 138    tlb_flush_page(cs, TARGET_PAGE_SIZE);
 139}
 140
 141/* Store Clock */
 142uint64_t HELPER(stck)(CPUS390XState *env)
 143{
 144    S390TODState *td = s390_get_todstate();
 145    S390TODClass *tdc = S390_TOD_GET_CLASS(td);
 146    S390TOD tod;
 147
 148    tdc->get(td, &tod, &error_abort);
 149    return tod.low;
 150}
 151
 152static void update_ckc_timer(CPUS390XState *env)
 153{
 154    S390TODState *td = s390_get_todstate();
 155    uint64_t time;
 156
 157    /* stop the timer and remove pending CKC IRQs */
 158    timer_del(env->tod_timer);
 159    g_assert(qemu_mutex_iothread_locked());
 160    env->pending_int &= ~INTERRUPT_EXT_CLOCK_COMPARATOR;
 161
 162    /* the tod has to exceed the ckc, this can never happen if ckc is all 1's */
 163    if (env->ckc == -1ULL) {
 164        return;
 165    }
 166
 167    /* difference between origins */
 168    time = env->ckc - td->base.low;
 169
 170    /* nanoseconds */
 171    time = tod2time(time);
 172
 173    timer_mod(env->tod_timer, time);
 174}
 175
 176/* Set Clock Comparator */
 177void HELPER(sckc)(CPUS390XState *env, uint64_t ckc)
 178{
 179    env->ckc = ckc;
 180
 181    qemu_mutex_lock_iothread();
 182    update_ckc_timer(env);
 183    qemu_mutex_unlock_iothread();
 184}
 185
 186void tcg_s390_tod_updated(CPUState *cs, run_on_cpu_data opaque)
 187{
 188    S390CPU *cpu = S390_CPU(cs);
 189
 190    update_ckc_timer(&cpu->env);
 191}
 192
 193/* Set Clock */
 194uint32_t HELPER(sck)(CPUS390XState *env, uint64_t tod_low)
 195{
 196    S390TODState *td = s390_get_todstate();
 197    S390TODClass *tdc = S390_TOD_GET_CLASS(td);
 198    S390TOD tod = {
 199        .high = 0,
 200        .low = tod_low,
 201    };
 202
 203    qemu_mutex_lock_iothread();
 204    tdc->set(td, &tod, &error_abort);
 205    qemu_mutex_unlock_iothread();
 206    return 0;
 207}
 208
 209/* Set Tod Programmable Field */
 210void HELPER(sckpf)(CPUS390XState *env, uint64_t r0)
 211{
 212    uint32_t val = r0;
 213
 214    if (val & 0xffff0000) {
 215        s390_program_interrupt(env, PGM_SPECIFICATION, 2, GETPC());
 216    }
 217    env->todpr = val;
 218}
 219
 220/* Store Clock Comparator */
 221uint64_t HELPER(stckc)(CPUS390XState *env)
 222{
 223    return env->ckc;
 224}
 225
 226/* Set CPU Timer */
 227void HELPER(spt)(CPUS390XState *env, uint64_t time)
 228{
 229    if (time == -1ULL) {
 230        return;
 231    }
 232
 233    /* nanoseconds */
 234    time = tod2time(time);
 235
 236    env->cputm = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + time;
 237
 238    timer_mod(env->cpu_timer, env->cputm);
 239}
 240
 241/* Store System Information */
 242uint32_t HELPER(stsi)(CPUS390XState *env, uint64_t a0, uint64_t r0, uint64_t r1)
 243{
 244    const uintptr_t ra = GETPC();
 245    const uint32_t sel1 = r0 & STSI_R0_SEL1_MASK;
 246    const uint32_t sel2 = r1 & STSI_R1_SEL2_MASK;
 247    const MachineState *ms = MACHINE(qdev_get_machine());
 248    uint16_t total_cpus = 0, conf_cpus = 0, reserved_cpus = 0;
 249    S390CPU *cpu = s390_env_get_cpu(env);
 250    SysIB sysib = { };
 251    int i, cc = 0;
 252
 253    if ((r0 & STSI_R0_FC_MASK) > STSI_R0_FC_LEVEL_3) {
 254        /* invalid function code: no other checks are performed */
 255        return 3;
 256    }
 257
 258    if ((r0 & STSI_R0_RESERVED_MASK) || (r1 & STSI_R1_RESERVED_MASK)) {
 259        s390_program_interrupt(env, PGM_SPECIFICATION, 4, ra);
 260    }
 261
 262    if ((r0 & STSI_R0_FC_MASK) == STSI_R0_FC_CURRENT) {
 263        /* query the current level: no further checks are performed */
 264        env->regs[0] = STSI_R0_FC_LEVEL_3;
 265        return 0;
 266    }
 267
 268    if (a0 & ~TARGET_PAGE_MASK) {
 269        s390_program_interrupt(env, PGM_SPECIFICATION, 4, ra);
 270    }
 271
 272    /* count the cpus and split them into configured and reserved ones */
 273    for (i = 0; i < ms->possible_cpus->len; i++) {
 274        total_cpus++;
 275        if (ms->possible_cpus->cpus[i].cpu) {
 276            conf_cpus++;
 277        } else {
 278            reserved_cpus++;
 279        }
 280    }
 281
 282    /*
 283     * In theory, we could report Level 1 / Level 2 as current. However,
 284     * the Linux kernel will detect this as running under LPAR and assume
 285     * that we have a sclp linemode console (which is always present on
 286     * LPAR, but not the default for QEMU), therefore not displaying boot
 287     * messages and making booting a Linux kernel under TCG harder.
 288     *
 289     * For now we fake the same SMP configuration on all levels.
 290     *
 291     * TODO: We could later make the level configurable via the machine
 292     *       and change defaults (linemode console) based on machine type
 293     *       and accelerator.
 294     */
 295    switch (r0 & STSI_R0_FC_MASK) {
 296    case STSI_R0_FC_LEVEL_1:
 297        if ((sel1 == 1) && (sel2 == 1)) {
 298            /* Basic Machine Configuration */
 299            char type[5] = {};
 300
 301            ebcdic_put(sysib.sysib_111.manuf, "QEMU            ", 16);
 302            /* same as machine type number in STORE CPU ID, but in EBCDIC */
 303            snprintf(type, ARRAY_SIZE(type), "%X", cpu->model->def->type);
 304            ebcdic_put(sysib.sysib_111.type, type, 4);
 305            /* model number (not stored in STORE CPU ID for z/Architecure) */
 306            ebcdic_put(sysib.sysib_111.model, "QEMU            ", 16);
 307            ebcdic_put(sysib.sysib_111.sequence, "QEMU            ", 16);
 308            ebcdic_put(sysib.sysib_111.plant, "QEMU", 4);
 309        } else if ((sel1 == 2) && (sel2 == 1)) {
 310            /* Basic Machine CPU */
 311            ebcdic_put(sysib.sysib_121.sequence, "QEMUQEMUQEMUQEMU", 16);
 312            ebcdic_put(sysib.sysib_121.plant, "QEMU", 4);
 313            sysib.sysib_121.cpu_addr = cpu_to_be16(env->core_id);
 314        } else if ((sel1 == 2) && (sel2 == 2)) {
 315            /* Basic Machine CPUs */
 316            sysib.sysib_122.capability = cpu_to_be32(0x443afc29);
 317            sysib.sysib_122.total_cpus = cpu_to_be16(total_cpus);
 318            sysib.sysib_122.conf_cpus = cpu_to_be16(conf_cpus);
 319            sysib.sysib_122.reserved_cpus = cpu_to_be16(reserved_cpus);
 320        } else {
 321            cc = 3;
 322        }
 323        break;
 324    case STSI_R0_FC_LEVEL_2:
 325        if ((sel1 == 2) && (sel2 == 1)) {
 326            /* LPAR CPU */
 327            ebcdic_put(sysib.sysib_221.sequence, "QEMUQEMUQEMUQEMU", 16);
 328            ebcdic_put(sysib.sysib_221.plant, "QEMU", 4);
 329            sysib.sysib_221.cpu_addr = cpu_to_be16(env->core_id);
 330        } else if ((sel1 == 2) && (sel2 == 2)) {
 331            /* LPAR CPUs */
 332            sysib.sysib_222.lcpuc = 0x80; /* dedicated */
 333            sysib.sysib_222.total_cpus = cpu_to_be16(total_cpus);
 334            sysib.sysib_222.conf_cpus = cpu_to_be16(conf_cpus);
 335            sysib.sysib_222.reserved_cpus = cpu_to_be16(reserved_cpus);
 336            ebcdic_put(sysib.sysib_222.name, "QEMU    ", 8);
 337            sysib.sysib_222.caf = cpu_to_be32(1000);
 338            sysib.sysib_222.dedicated_cpus = cpu_to_be16(conf_cpus);
 339        } else {
 340            cc = 3;
 341        }
 342        break;
 343    case STSI_R0_FC_LEVEL_3:
 344        if ((sel1 == 2) && (sel2 == 2)) {
 345            /* VM CPUs */
 346            sysib.sysib_322.count = 1;
 347            sysib.sysib_322.vm[0].total_cpus = cpu_to_be16(total_cpus);
 348            sysib.sysib_322.vm[0].conf_cpus = cpu_to_be16(conf_cpus);
 349            sysib.sysib_322.vm[0].reserved_cpus = cpu_to_be16(reserved_cpus);
 350            sysib.sysib_322.vm[0].caf = cpu_to_be32(1000);
 351            /* Linux kernel uses this to distinguish us from z/VM */
 352            ebcdic_put(sysib.sysib_322.vm[0].cpi, "KVM/Linux       ", 16);
 353            sysib.sysib_322.vm[0].ext_name_encoding = 2; /* UTF-8 */
 354
 355            /* If our VM has a name, use the real name */
 356            if (qemu_name) {
 357                memset(sysib.sysib_322.vm[0].name, 0x40,
 358                       sizeof(sysib.sysib_322.vm[0].name));
 359                ebcdic_put(sysib.sysib_322.vm[0].name, qemu_name,
 360                           MIN(sizeof(sysib.sysib_322.vm[0].name),
 361                               strlen(qemu_name)));
 362                strncpy((char *)sysib.sysib_322.ext_names[0], qemu_name,
 363                        sizeof(sysib.sysib_322.ext_names[0]));
 364            } else {
 365                ebcdic_put(sysib.sysib_322.vm[0].name, "TCGguest", 8);
 366                strcpy((char *)sysib.sysib_322.ext_names[0], "TCGguest");
 367            }
 368
 369            /* add the uuid */
 370            memcpy(sysib.sysib_322.vm[0].uuid, &qemu_uuid,
 371                   sizeof(sysib.sysib_322.vm[0].uuid));
 372        } else {
 373            cc = 3;
 374        }
 375        break;
 376    }
 377
 378    if (cc == 0) {
 379        if (s390_cpu_virt_mem_write(cpu, a0, 0, &sysib, sizeof(sysib))) {
 380            s390_cpu_virt_mem_handle_exc(cpu, ra);
 381        }
 382    }
 383
 384    return cc;
 385}
 386
 387uint32_t HELPER(sigp)(CPUS390XState *env, uint64_t order_code, uint32_t r1,
 388                      uint32_t r3)
 389{
 390    int cc;
 391
 392    /* TODO: needed to inject interrupts  - push further down */
 393    qemu_mutex_lock_iothread();
 394    cc = handle_sigp(env, order_code & SIGP_ORDER_MASK, r1, r3);
 395    qemu_mutex_unlock_iothread();
 396
 397    return cc;
 398}
 399#endif
 400
 401#ifndef CONFIG_USER_ONLY
 402void HELPER(xsch)(CPUS390XState *env, uint64_t r1)
 403{
 404    S390CPU *cpu = s390_env_get_cpu(env);
 405    qemu_mutex_lock_iothread();
 406    ioinst_handle_xsch(cpu, r1, GETPC());
 407    qemu_mutex_unlock_iothread();
 408}
 409
 410void HELPER(csch)(CPUS390XState *env, uint64_t r1)
 411{
 412    S390CPU *cpu = s390_env_get_cpu(env);
 413    qemu_mutex_lock_iothread();
 414    ioinst_handle_csch(cpu, r1, GETPC());
 415    qemu_mutex_unlock_iothread();
 416}
 417
 418void HELPER(hsch)(CPUS390XState *env, uint64_t r1)
 419{
 420    S390CPU *cpu = s390_env_get_cpu(env);
 421    qemu_mutex_lock_iothread();
 422    ioinst_handle_hsch(cpu, r1, GETPC());
 423    qemu_mutex_unlock_iothread();
 424}
 425
 426void HELPER(msch)(CPUS390XState *env, uint64_t r1, uint64_t inst)
 427{
 428    S390CPU *cpu = s390_env_get_cpu(env);
 429    qemu_mutex_lock_iothread();
 430    ioinst_handle_msch(cpu, r1, inst >> 16, GETPC());
 431    qemu_mutex_unlock_iothread();
 432}
 433
 434void HELPER(rchp)(CPUS390XState *env, uint64_t r1)
 435{
 436    S390CPU *cpu = s390_env_get_cpu(env);
 437    qemu_mutex_lock_iothread();
 438    ioinst_handle_rchp(cpu, r1, GETPC());
 439    qemu_mutex_unlock_iothread();
 440}
 441
 442void HELPER(rsch)(CPUS390XState *env, uint64_t r1)
 443{
 444    S390CPU *cpu = s390_env_get_cpu(env);
 445    qemu_mutex_lock_iothread();
 446    ioinst_handle_rsch(cpu, r1, GETPC());
 447    qemu_mutex_unlock_iothread();
 448}
 449
 450void HELPER(sal)(CPUS390XState *env, uint64_t r1)
 451{
 452    S390CPU *cpu = s390_env_get_cpu(env);
 453
 454    qemu_mutex_lock_iothread();
 455    ioinst_handle_sal(cpu, r1, GETPC());
 456    qemu_mutex_unlock_iothread();
 457}
 458
 459void HELPER(schm)(CPUS390XState *env, uint64_t r1, uint64_t r2, uint64_t inst)
 460{
 461    S390CPU *cpu = s390_env_get_cpu(env);
 462
 463    qemu_mutex_lock_iothread();
 464    ioinst_handle_schm(cpu, r1, r2, inst >> 16, GETPC());
 465    qemu_mutex_unlock_iothread();
 466}
 467
 468void HELPER(ssch)(CPUS390XState *env, uint64_t r1, uint64_t inst)
 469{
 470    S390CPU *cpu = s390_env_get_cpu(env);
 471    qemu_mutex_lock_iothread();
 472    ioinst_handle_ssch(cpu, r1, inst >> 16, GETPC());
 473    qemu_mutex_unlock_iothread();
 474}
 475
 476void HELPER(stcrw)(CPUS390XState *env, uint64_t inst)
 477{
 478    S390CPU *cpu = s390_env_get_cpu(env);
 479
 480    qemu_mutex_lock_iothread();
 481    ioinst_handle_stcrw(cpu, inst >> 16, GETPC());
 482    qemu_mutex_unlock_iothread();
 483}
 484
 485void HELPER(stsch)(CPUS390XState *env, uint64_t r1, uint64_t inst)
 486{
 487    S390CPU *cpu = s390_env_get_cpu(env);
 488    qemu_mutex_lock_iothread();
 489    ioinst_handle_stsch(cpu, r1, inst >> 16, GETPC());
 490    qemu_mutex_unlock_iothread();
 491}
 492
 493uint32_t HELPER(tpi)(CPUS390XState *env, uint64_t addr)
 494{
 495    const uintptr_t ra = GETPC();
 496    S390CPU *cpu = s390_env_get_cpu(env);
 497    QEMUS390FLICState *flic = s390_get_qemu_flic(s390_get_flic());
 498    QEMUS390FlicIO *io = NULL;
 499    LowCore *lowcore;
 500
 501    if (addr & 0x3) {
 502        s390_program_interrupt(env, PGM_SPECIFICATION, 4, ra);
 503    }
 504
 505    qemu_mutex_lock_iothread();
 506    io = qemu_s390_flic_dequeue_io(flic, env->cregs[6]);
 507    if (!io) {
 508        qemu_mutex_unlock_iothread();
 509        return 0;
 510    }
 511
 512    if (addr) {
 513        struct {
 514            uint16_t id;
 515            uint16_t nr;
 516            uint32_t parm;
 517        } intc = {
 518            .id = cpu_to_be16(io->id),
 519            .nr = cpu_to_be16(io->nr),
 520            .parm = cpu_to_be32(io->parm),
 521        };
 522
 523        if (s390_cpu_virt_mem_write(cpu, addr, 0, &intc, sizeof(intc))) {
 524            /* writing failed, reinject and properly clean up */
 525            s390_io_interrupt(io->id, io->nr, io->parm, io->word);
 526            qemu_mutex_unlock_iothread();
 527            g_free(io);
 528            s390_cpu_virt_mem_handle_exc(cpu, ra);
 529            return 0;
 530        }
 531    } else {
 532        /* no protection applies */
 533        lowcore = cpu_map_lowcore(env);
 534        lowcore->subchannel_id = cpu_to_be16(io->id);
 535        lowcore->subchannel_nr = cpu_to_be16(io->nr);
 536        lowcore->io_int_parm = cpu_to_be32(io->parm);
 537        lowcore->io_int_word = cpu_to_be32(io->word);
 538        cpu_unmap_lowcore(lowcore);
 539    }
 540
 541    g_free(io);
 542    qemu_mutex_unlock_iothread();
 543    return 1;
 544}
 545
 546void HELPER(tsch)(CPUS390XState *env, uint64_t r1, uint64_t inst)
 547{
 548    S390CPU *cpu = s390_env_get_cpu(env);
 549    qemu_mutex_lock_iothread();
 550    ioinst_handle_tsch(cpu, r1, inst >> 16, GETPC());
 551    qemu_mutex_unlock_iothread();
 552}
 553
 554void HELPER(chsc)(CPUS390XState *env, uint64_t inst)
 555{
 556    S390CPU *cpu = s390_env_get_cpu(env);
 557    qemu_mutex_lock_iothread();
 558    ioinst_handle_chsc(cpu, inst >> 16, GETPC());
 559    qemu_mutex_unlock_iothread();
 560}
 561#endif
 562
 563#ifndef CONFIG_USER_ONLY
 564void HELPER(per_check_exception)(CPUS390XState *env)
 565{
 566    uint32_t ilen;
 567
 568    if (env->per_perc_atmid) {
 569        /*
 570         * FIXME: ILEN_AUTO is most probably the right thing to use. ilen
 571         * always has to match the instruction referenced in the PSW. E.g.
 572         * if a PER interrupt is triggered via EXECUTE, we have to use ilen
 573         * of EXECUTE, while per_address contains the target of EXECUTE.
 574         */
 575        ilen = get_ilen(cpu_ldub_code(env, env->per_address));
 576        s390_program_interrupt(env, PGM_PER, ilen, GETPC());
 577    }
 578}
 579
 580/* Check if an address is within the PER starting address and the PER
 581   ending address.  The address range might loop.  */
 582static inline bool get_per_in_range(CPUS390XState *env, uint64_t addr)
 583{
 584    if (env->cregs[10] <= env->cregs[11]) {
 585        return env->cregs[10] <= addr && addr <= env->cregs[11];
 586    } else {
 587        return env->cregs[10] <= addr || addr <= env->cregs[11];
 588    }
 589}
 590
 591void HELPER(per_branch)(CPUS390XState *env, uint64_t from, uint64_t to)
 592{
 593    if ((env->cregs[9] & PER_CR9_EVENT_BRANCH)) {
 594        if (!(env->cregs[9] & PER_CR9_CONTROL_BRANCH_ADDRESS)
 595            || get_per_in_range(env, to)) {
 596            env->per_address = from;
 597            env->per_perc_atmid = PER_CODE_EVENT_BRANCH | get_per_atmid(env);
 598        }
 599    }
 600}
 601
 602void HELPER(per_ifetch)(CPUS390XState *env, uint64_t addr)
 603{
 604    if ((env->cregs[9] & PER_CR9_EVENT_IFETCH) && get_per_in_range(env, addr)) {
 605        env->per_address = addr;
 606        env->per_perc_atmid = PER_CODE_EVENT_IFETCH | get_per_atmid(env);
 607
 608        /* If the instruction has to be nullified, trigger the
 609           exception immediately. */
 610        if (env->cregs[9] & PER_CR9_EVENT_NULLIFICATION) {
 611            CPUState *cs = CPU(s390_env_get_cpu(env));
 612
 613            env->per_perc_atmid |= PER_CODE_EVENT_NULLIFICATION;
 614            env->int_pgm_code = PGM_PER;
 615            env->int_pgm_ilen = get_ilen(cpu_ldub_code(env, addr));
 616
 617            cs->exception_index = EXCP_PGM;
 618            cpu_loop_exit(cs);
 619        }
 620    }
 621}
 622#endif
 623
 624static uint8_t stfl_bytes[2048];
 625static unsigned int used_stfl_bytes;
 626
 627static void prepare_stfl(void)
 628{
 629    static bool initialized;
 630    int i;
 631
 632    /* racy, but we don't care, the same values are always written */
 633    if (initialized) {
 634        return;
 635    }
 636
 637    s390_get_feat_block(S390_FEAT_TYPE_STFL, stfl_bytes);
 638    for (i = 0; i < sizeof(stfl_bytes); i++) {
 639        if (stfl_bytes[i]) {
 640            used_stfl_bytes = i + 1;
 641        }
 642    }
 643    initialized = true;
 644}
 645
 646#ifndef CONFIG_USER_ONLY
 647void HELPER(stfl)(CPUS390XState *env)
 648{
 649    LowCore *lowcore;
 650
 651    lowcore = cpu_map_lowcore(env);
 652    prepare_stfl();
 653    memcpy(&lowcore->stfl_fac_list, stfl_bytes, sizeof(lowcore->stfl_fac_list));
 654    cpu_unmap_lowcore(lowcore);
 655}
 656#endif
 657
 658uint32_t HELPER(stfle)(CPUS390XState *env, uint64_t addr)
 659{
 660    const uintptr_t ra = GETPC();
 661    const int count_bytes = ((env->regs[0] & 0xff) + 1) * 8;
 662    const int max_bytes = ROUND_UP(used_stfl_bytes, 8);
 663    int i;
 664
 665    if (addr & 0x7) {
 666        s390_program_interrupt(env, PGM_SPECIFICATION, 4, ra);
 667    }
 668
 669    prepare_stfl();
 670    for (i = 0; i < count_bytes; ++i) {
 671        cpu_stb_data_ra(env, addr + i, stfl_bytes[i], ra);
 672    }
 673
 674    env->regs[0] = deposit64(env->regs[0], 0, 8, (max_bytes / 8) - 1);
 675    return count_bytes >= max_bytes ? 0 : 3;
 676}
 677
 678#ifndef CONFIG_USER_ONLY
 679/*
 680 * Note: we ignore any return code of the functions called for the pci
 681 * instructions, as the only time they return !0 is when the stub is
 682 * called, and in that case we didn't even offer the zpci facility.
 683 * The only exception is SIC, where program checks need to be handled
 684 * by the caller.
 685 */
 686void HELPER(clp)(CPUS390XState *env, uint32_t r2)
 687{
 688    S390CPU *cpu = s390_env_get_cpu(env);
 689
 690    qemu_mutex_lock_iothread();
 691    clp_service_call(cpu, r2, GETPC());
 692    qemu_mutex_unlock_iothread();
 693}
 694
 695void HELPER(pcilg)(CPUS390XState *env, uint32_t r1, uint32_t r2)
 696{
 697    S390CPU *cpu = s390_env_get_cpu(env);
 698
 699    qemu_mutex_lock_iothread();
 700    pcilg_service_call(cpu, r1, r2, GETPC());
 701    qemu_mutex_unlock_iothread();
 702}
 703
 704void HELPER(pcistg)(CPUS390XState *env, uint32_t r1, uint32_t r2)
 705{
 706    S390CPU *cpu = s390_env_get_cpu(env);
 707
 708    qemu_mutex_lock_iothread();
 709    pcistg_service_call(cpu, r1, r2, GETPC());
 710    qemu_mutex_unlock_iothread();
 711}
 712
 713void HELPER(stpcifc)(CPUS390XState *env, uint32_t r1, uint64_t fiba,
 714                     uint32_t ar)
 715{
 716    S390CPU *cpu = s390_env_get_cpu(env);
 717
 718    qemu_mutex_lock_iothread();
 719    stpcifc_service_call(cpu, r1, fiba, ar, GETPC());
 720    qemu_mutex_unlock_iothread();
 721}
 722
 723void HELPER(sic)(CPUS390XState *env, uint64_t r1, uint64_t r3)
 724{
 725    int r;
 726
 727    qemu_mutex_lock_iothread();
 728    r = css_do_sic(env, (r3 >> 27) & 0x7, r1 & 0xffff);
 729    qemu_mutex_unlock_iothread();
 730    /* css_do_sic() may actually return a PGM_xxx value to inject */
 731    if (r) {
 732        s390_program_interrupt(env, -r, 4, GETPC());
 733    }
 734}
 735
 736void HELPER(rpcit)(CPUS390XState *env, uint32_t r1, uint32_t r2)
 737{
 738    S390CPU *cpu = s390_env_get_cpu(env);
 739
 740    qemu_mutex_lock_iothread();
 741    rpcit_service_call(cpu, r1, r2, GETPC());
 742    qemu_mutex_unlock_iothread();
 743}
 744
 745void HELPER(pcistb)(CPUS390XState *env, uint32_t r1, uint32_t r3,
 746                    uint64_t gaddr, uint32_t ar)
 747{
 748    S390CPU *cpu = s390_env_get_cpu(env);
 749
 750    qemu_mutex_lock_iothread();
 751    pcistb_service_call(cpu, r1, r3, gaddr, ar, GETPC());
 752    qemu_mutex_unlock_iothread();
 753}
 754
 755void HELPER(mpcifc)(CPUS390XState *env, uint32_t r1, uint64_t fiba,
 756                    uint32_t ar)
 757{
 758    S390CPU *cpu = s390_env_get_cpu(env);
 759
 760    qemu_mutex_lock_iothread();
 761    mpcifc_service_call(cpu, r1, fiba, ar, GETPC());
 762    qemu_mutex_unlock_iothread();
 763}
 764#endif
 765