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 "exec/memory.h"
  25#include "qemu/host-utils.h"
  26#include "exec/helper-proto.h"
  27#include "qemu/timer.h"
  28#include "exec/address-spaces.h"
  29#include "exec/exec-all.h"
  30#include "exec/cpu_ldst.h"
  31
  32#if !defined(CONFIG_USER_ONLY)
  33#include "sysemu/cpus.h"
  34#include "sysemu/sysemu.h"
  35#include "hw/s390x/ebcdic.h"
  36#endif
  37
  38/* #define DEBUG_HELPER */
  39#ifdef DEBUG_HELPER
  40#define HELPER_LOG(x...) qemu_log(x)
  41#else
  42#define HELPER_LOG(x...)
  43#endif
  44
  45/* Raise an exception dynamically from a helper function.  */
  46void QEMU_NORETURN runtime_exception(CPUS390XState *env, int excp,
  47                                     uintptr_t retaddr)
  48{
  49    CPUState *cs = CPU(s390_env_get_cpu(env));
  50
  51    cs->exception_index = EXCP_PGM;
  52    env->int_pgm_code = excp;
  53    env->int_pgm_ilen = ILEN_AUTO;
  54
  55    /* Use the (ultimate) callers address to find the insn that trapped.  */
  56    cpu_restore_state(cs, retaddr);
  57
  58    cpu_loop_exit(cs);
  59}
  60
  61/* Raise an exception statically from a TB.  */
  62void HELPER(exception)(CPUS390XState *env, uint32_t excp)
  63{
  64    CPUState *cs = CPU(s390_env_get_cpu(env));
  65
  66    HELPER_LOG("%s: exception %d\n", __func__, excp);
  67    cs->exception_index = excp;
  68    cpu_loop_exit(cs);
  69}
  70
  71#ifndef CONFIG_USER_ONLY
  72
  73/* SCLP service call */
  74uint32_t HELPER(servc)(CPUS390XState *env, uint64_t r1, uint64_t r2)
  75{
  76    qemu_mutex_lock_iothread();
  77    int r = sclp_service_call(env, r1, r2);
  78    if (r < 0) {
  79        program_interrupt(env, -r, 4);
  80        r = 0;
  81    }
  82    qemu_mutex_unlock_iothread();
  83    return r;
  84}
  85
  86void HELPER(diag)(CPUS390XState *env, uint32_t r1, uint32_t r3, uint32_t num)
  87{
  88    uint64_t r;
  89
  90    switch (num) {
  91    case 0x500:
  92        /* KVM hypercall */
  93        qemu_mutex_lock_iothread();
  94        r = s390_virtio_hypercall(env);
  95        qemu_mutex_unlock_iothread();
  96        break;
  97    case 0x44:
  98        /* yield */
  99        r = 0;
 100        break;
 101    case 0x308:
 102        /* ipl */
 103        handle_diag_308(env, r1, r3);
 104        r = 0;
 105        break;
 106    default:
 107        r = -1;
 108        break;
 109    }
 110
 111    if (r) {
 112        program_interrupt(env, PGM_OPERATION, ILEN_AUTO);
 113    }
 114}
 115
 116/* Set Prefix */
 117void HELPER(spx)(CPUS390XState *env, uint64_t a1)
 118{
 119    CPUState *cs = CPU(s390_env_get_cpu(env));
 120    uint32_t prefix = a1 & 0x7fffe000;
 121
 122    env->psa = prefix;
 123    HELPER_LOG("prefix: %#x\n", prefix);
 124    tlb_flush_page(cs, 0);
 125    tlb_flush_page(cs, TARGET_PAGE_SIZE);
 126}
 127
 128/* Store Clock */
 129uint64_t HELPER(stck)(CPUS390XState *env)
 130{
 131    uint64_t time;
 132
 133    time = env->tod_offset +
 134        time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - env->tod_basetime);
 135
 136    return time;
 137}
 138
 139/* Set Clock Comparator */
 140void HELPER(sckc)(CPUS390XState *env, uint64_t time)
 141{
 142    if (time == -1ULL) {
 143        return;
 144    }
 145
 146    env->ckc = time;
 147
 148    /* difference between origins */
 149    time -= env->tod_offset;
 150
 151    /* nanoseconds */
 152    time = tod2time(time);
 153
 154    timer_mod(env->tod_timer, env->tod_basetime + time);
 155}
 156
 157/* Store Clock Comparator */
 158uint64_t HELPER(stckc)(CPUS390XState *env)
 159{
 160    return env->ckc;
 161}
 162
 163/* Set CPU Timer */
 164void HELPER(spt)(CPUS390XState *env, uint64_t time)
 165{
 166    if (time == -1ULL) {
 167        return;
 168    }
 169
 170    /* nanoseconds */
 171    time = tod2time(time);
 172
 173    env->cputm = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + time;
 174
 175    timer_mod(env->cpu_timer, env->cputm);
 176}
 177
 178/* Store CPU Timer */
 179uint64_t HELPER(stpt)(CPUS390XState *env)
 180{
 181    return time2tod(env->cputm - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
 182}
 183
 184/* Store System Information */
 185uint32_t HELPER(stsi)(CPUS390XState *env, uint64_t a0,
 186                      uint64_t r0, uint64_t r1)
 187{
 188    S390CPU *cpu = s390_env_get_cpu(env);
 189    int cc = 0;
 190    int sel1, sel2;
 191
 192    if ((r0 & STSI_LEVEL_MASK) <= STSI_LEVEL_3 &&
 193        ((r0 & STSI_R0_RESERVED_MASK) || (r1 & STSI_R1_RESERVED_MASK))) {
 194        /* valid function code, invalid reserved bits */
 195        program_interrupt(env, PGM_SPECIFICATION, 4);
 196    }
 197
 198    sel1 = r0 & STSI_R0_SEL1_MASK;
 199    sel2 = r1 & STSI_R1_SEL2_MASK;
 200
 201    /* XXX: spec exception if sysib is not 4k-aligned */
 202
 203    switch (r0 & STSI_LEVEL_MASK) {
 204    case STSI_LEVEL_1:
 205        if ((sel1 == 1) && (sel2 == 1)) {
 206            /* Basic Machine Configuration */
 207            struct sysib_111 sysib;
 208            char type[5] = {};
 209
 210            memset(&sysib, 0, sizeof(sysib));
 211            ebcdic_put(sysib.manuf, "QEMU            ", 16);
 212            /* same as machine type number in STORE CPU ID, but in EBCDIC */
 213            snprintf(type, ARRAY_SIZE(type), "%X", cpu->model->def->type);
 214            ebcdic_put(sysib.type, type, 4);
 215            /* model number (not stored in STORE CPU ID for z/Architecure) */
 216            ebcdic_put(sysib.model, "QEMU            ", 16);
 217            ebcdic_put(sysib.sequence, "QEMU            ", 16);
 218            ebcdic_put(sysib.plant, "QEMU", 4);
 219            cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
 220        } else if ((sel1 == 2) && (sel2 == 1)) {
 221            /* Basic Machine CPU */
 222            struct sysib_121 sysib;
 223
 224            memset(&sysib, 0, sizeof(sysib));
 225            /* XXX make different for different CPUs? */
 226            ebcdic_put(sysib.sequence, "QEMUQEMUQEMUQEMU", 16);
 227            ebcdic_put(sysib.plant, "QEMU", 4);
 228            stw_p(&sysib.cpu_addr, env->cpu_num);
 229            cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
 230        } else if ((sel1 == 2) && (sel2 == 2)) {
 231            /* Basic Machine CPUs */
 232            struct sysib_122 sysib;
 233
 234            memset(&sysib, 0, sizeof(sysib));
 235            stl_p(&sysib.capability, 0x443afc29);
 236            /* XXX change when SMP comes */
 237            stw_p(&sysib.total_cpus, 1);
 238            stw_p(&sysib.active_cpus, 1);
 239            stw_p(&sysib.standby_cpus, 0);
 240            stw_p(&sysib.reserved_cpus, 0);
 241            cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
 242        } else {
 243            cc = 3;
 244        }
 245        break;
 246    case STSI_LEVEL_2:
 247        {
 248            if ((sel1 == 2) && (sel2 == 1)) {
 249                /* LPAR CPU */
 250                struct sysib_221 sysib;
 251
 252                memset(&sysib, 0, sizeof(sysib));
 253                /* XXX make different for different CPUs? */
 254                ebcdic_put(sysib.sequence, "QEMUQEMUQEMUQEMU", 16);
 255                ebcdic_put(sysib.plant, "QEMU", 4);
 256                stw_p(&sysib.cpu_addr, env->cpu_num);
 257                stw_p(&sysib.cpu_id, 0);
 258                cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
 259            } else if ((sel1 == 2) && (sel2 == 2)) {
 260                /* LPAR CPUs */
 261                struct sysib_222 sysib;
 262
 263                memset(&sysib, 0, sizeof(sysib));
 264                stw_p(&sysib.lpar_num, 0);
 265                sysib.lcpuc = 0;
 266                /* XXX change when SMP comes */
 267                stw_p(&sysib.total_cpus, 1);
 268                stw_p(&sysib.conf_cpus, 1);
 269                stw_p(&sysib.standby_cpus, 0);
 270                stw_p(&sysib.reserved_cpus, 0);
 271                ebcdic_put(sysib.name, "QEMU    ", 8);
 272                stl_p(&sysib.caf, 1000);
 273                stw_p(&sysib.dedicated_cpus, 0);
 274                stw_p(&sysib.shared_cpus, 0);
 275                cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
 276            } else {
 277                cc = 3;
 278            }
 279            break;
 280        }
 281    case STSI_LEVEL_3:
 282        {
 283            if ((sel1 == 2) && (sel2 == 2)) {
 284                /* VM CPUs */
 285                struct sysib_322 sysib;
 286
 287                memset(&sysib, 0, sizeof(sysib));
 288                sysib.count = 1;
 289                /* XXX change when SMP comes */
 290                stw_p(&sysib.vm[0].total_cpus, 1);
 291                stw_p(&sysib.vm[0].conf_cpus, 1);
 292                stw_p(&sysib.vm[0].standby_cpus, 0);
 293                stw_p(&sysib.vm[0].reserved_cpus, 0);
 294                ebcdic_put(sysib.vm[0].name, "KVMguest", 8);
 295                stl_p(&sysib.vm[0].caf, 1000);
 296                ebcdic_put(sysib.vm[0].cpi, "KVM/Linux       ", 16);
 297                cpu_physical_memory_write(a0, &sysib, sizeof(sysib));
 298            } else {
 299                cc = 3;
 300            }
 301            break;
 302        }
 303    case STSI_LEVEL_CURRENT:
 304        env->regs[0] = STSI_LEVEL_3;
 305        break;
 306    default:
 307        cc = 3;
 308        break;
 309    }
 310
 311    return cc;
 312}
 313
 314uint32_t HELPER(sigp)(CPUS390XState *env, uint64_t order_code, uint32_t r1,
 315                      uint64_t cpu_addr)
 316{
 317    int cc = SIGP_CC_ORDER_CODE_ACCEPTED;
 318
 319    HELPER_LOG("%s: %016" PRIx64 " %08x %016" PRIx64 "\n",
 320               __func__, order_code, r1, cpu_addr);
 321
 322    /* Remember: Use "R1 or R1 + 1, whichever is the odd-numbered register"
 323       as parameter (input). Status (output) is always R1. */
 324
 325    switch (order_code & SIGP_ORDER_MASK) {
 326    case SIGP_SET_ARCH:
 327        /* switch arch */
 328        break;
 329    case SIGP_SENSE:
 330        /* enumerate CPU status */
 331        if (cpu_addr) {
 332            /* XXX implement when SMP comes */
 333            return 3;
 334        }
 335        env->regs[r1] &= 0xffffffff00000000ULL;
 336        cc = 1;
 337        break;
 338#if !defined(CONFIG_USER_ONLY)
 339    case SIGP_RESTART:
 340        qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
 341        cpu_loop_exit(CPU(s390_env_get_cpu(env)));
 342        break;
 343    case SIGP_STOP:
 344        qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
 345        cpu_loop_exit(CPU(s390_env_get_cpu(env)));
 346        break;
 347#endif
 348    default:
 349        /* unknown sigp */
 350        fprintf(stderr, "XXX unknown sigp: 0x%" PRIx64 "\n", order_code);
 351        cc = SIGP_CC_NOT_OPERATIONAL;
 352    }
 353
 354    return cc;
 355}
 356#endif
 357
 358#ifndef CONFIG_USER_ONLY
 359void HELPER(xsch)(CPUS390XState *env, uint64_t r1)
 360{
 361    S390CPU *cpu = s390_env_get_cpu(env);
 362    qemu_mutex_lock_iothread();
 363    ioinst_handle_xsch(cpu, r1);
 364    qemu_mutex_unlock_iothread();
 365}
 366
 367void HELPER(csch)(CPUS390XState *env, uint64_t r1)
 368{
 369    S390CPU *cpu = s390_env_get_cpu(env);
 370    qemu_mutex_lock_iothread();
 371    ioinst_handle_csch(cpu, r1);
 372    qemu_mutex_unlock_iothread();
 373}
 374
 375void HELPER(hsch)(CPUS390XState *env, uint64_t r1)
 376{
 377    S390CPU *cpu = s390_env_get_cpu(env);
 378    qemu_mutex_lock_iothread();
 379    ioinst_handle_hsch(cpu, r1);
 380    qemu_mutex_unlock_iothread();
 381}
 382
 383void HELPER(msch)(CPUS390XState *env, uint64_t r1, uint64_t inst)
 384{
 385    S390CPU *cpu = s390_env_get_cpu(env);
 386    qemu_mutex_lock_iothread();
 387    ioinst_handle_msch(cpu, r1, inst >> 16);
 388    qemu_mutex_unlock_iothread();
 389}
 390
 391void HELPER(rchp)(CPUS390XState *env, uint64_t r1)
 392{
 393    S390CPU *cpu = s390_env_get_cpu(env);
 394    qemu_mutex_lock_iothread();
 395    ioinst_handle_rchp(cpu, r1);
 396    qemu_mutex_unlock_iothread();
 397}
 398
 399void HELPER(rsch)(CPUS390XState *env, uint64_t r1)
 400{
 401    S390CPU *cpu = s390_env_get_cpu(env);
 402    qemu_mutex_lock_iothread();
 403    ioinst_handle_rsch(cpu, r1);
 404    qemu_mutex_unlock_iothread();
 405}
 406
 407void HELPER(ssch)(CPUS390XState *env, uint64_t r1, uint64_t inst)
 408{
 409    S390CPU *cpu = s390_env_get_cpu(env);
 410    qemu_mutex_lock_iothread();
 411    ioinst_handle_ssch(cpu, r1, inst >> 16);
 412    qemu_mutex_unlock_iothread();
 413}
 414
 415void HELPER(stsch)(CPUS390XState *env, uint64_t r1, uint64_t inst)
 416{
 417    S390CPU *cpu = s390_env_get_cpu(env);
 418    qemu_mutex_lock_iothread();
 419    ioinst_handle_stsch(cpu, r1, inst >> 16);
 420    qemu_mutex_unlock_iothread();
 421}
 422
 423void HELPER(tsch)(CPUS390XState *env, uint64_t r1, uint64_t inst)
 424{
 425    S390CPU *cpu = s390_env_get_cpu(env);
 426    qemu_mutex_lock_iothread();
 427    ioinst_handle_tsch(cpu, r1, inst >> 16);
 428    qemu_mutex_unlock_iothread();
 429}
 430
 431void HELPER(chsc)(CPUS390XState *env, uint64_t inst)
 432{
 433    S390CPU *cpu = s390_env_get_cpu(env);
 434    qemu_mutex_lock_iothread();
 435    ioinst_handle_chsc(cpu, inst >> 16);
 436    qemu_mutex_unlock_iothread();
 437}
 438#endif
 439
 440#ifndef CONFIG_USER_ONLY
 441void HELPER(per_check_exception)(CPUS390XState *env)
 442{
 443    CPUState *cs = CPU(s390_env_get_cpu(env));
 444
 445    if (env->per_perc_atmid) {
 446        env->int_pgm_code = PGM_PER;
 447        env->int_pgm_ilen = get_ilen(cpu_ldub_code(env, env->per_address));
 448
 449        cs->exception_index = EXCP_PGM;
 450        cpu_loop_exit(cs);
 451    }
 452}
 453
 454void HELPER(per_branch)(CPUS390XState *env, uint64_t from, uint64_t to)
 455{
 456    if ((env->cregs[9] & PER_CR9_EVENT_BRANCH)) {
 457        if (!(env->cregs[9] & PER_CR9_CONTROL_BRANCH_ADDRESS)
 458            || get_per_in_range(env, to)) {
 459            env->per_address = from;
 460            env->per_perc_atmid = PER_CODE_EVENT_BRANCH | get_per_atmid(env);
 461        }
 462    }
 463}
 464
 465void HELPER(per_ifetch)(CPUS390XState *env, uint64_t addr)
 466{
 467    if ((env->cregs[9] & PER_CR9_EVENT_IFETCH) && get_per_in_range(env, addr)) {
 468        env->per_address = addr;
 469        env->per_perc_atmid = PER_CODE_EVENT_IFETCH | get_per_atmid(env);
 470
 471        /* If the instruction has to be nullified, trigger the
 472           exception immediately. */
 473        if (env->cregs[9] & PER_CR9_EVENT_NULLIFICATION) {
 474            CPUState *cs = CPU(s390_env_get_cpu(env));
 475
 476            env->per_perc_atmid |= PER_CODE_EVENT_NULLIFICATION;
 477            env->int_pgm_code = PGM_PER;
 478            env->int_pgm_ilen = get_ilen(cpu_ldub_code(env, addr));
 479
 480            cs->exception_index = EXCP_PGM;
 481            cpu_loop_exit(cs);
 482        }
 483    }
 484}
 485#endif
 486
 487/* The maximum bit defined at the moment is 129.  */
 488#define MAX_STFL_WORDS  3
 489
 490/* Canonicalize the current cpu's features into the 64-bit words required
 491   by STFLE.  Return the index-1 of the max word that is non-zero.  */
 492static unsigned do_stfle(CPUS390XState *env, uint64_t words[MAX_STFL_WORDS])
 493{
 494    S390CPU *cpu = s390_env_get_cpu(env);
 495    const unsigned long *features = cpu->model->features;
 496    unsigned max_bit = 0;
 497    S390Feat feat;
 498
 499    memset(words, 0, sizeof(uint64_t) * MAX_STFL_WORDS);
 500
 501    if (test_bit(S390_FEAT_ZARCH, features)) {
 502        /* z/Architecture is always active if around */
 503        words[0] = 1ull << (63 - 2);
 504    }
 505
 506    for (feat = find_first_bit(features, S390_FEAT_MAX);
 507         feat < S390_FEAT_MAX;
 508         feat = find_next_bit(features, S390_FEAT_MAX, feat + 1)) {
 509        const S390FeatDef *def = s390_feat_def(feat);
 510        if (def->type == S390_FEAT_TYPE_STFL) {
 511            unsigned bit = def->bit;
 512            if (bit > max_bit) {
 513                max_bit = bit;
 514            }
 515            assert(bit / 64 < MAX_STFL_WORDS);
 516            words[bit / 64] |= 1ULL << (63 - bit % 64);
 517        }
 518    }
 519
 520    return max_bit / 64;
 521}
 522
 523void HELPER(stfl)(CPUS390XState *env)
 524{
 525    uint64_t words[MAX_STFL_WORDS];
 526
 527    do_stfle(env, words);
 528    cpu_stl_data(env, 200, words[0] >> 32);
 529}
 530
 531uint32_t HELPER(stfle)(CPUS390XState *env, uint64_t addr)
 532{
 533    uint64_t words[MAX_STFL_WORDS];
 534    unsigned count_m1 = env->regs[0] & 0xff;
 535    unsigned max_m1 = do_stfle(env, words);
 536    unsigned i;
 537
 538    for (i = 0; i <= count_m1; ++i) {
 539        cpu_stq_data(env, addr + 8 * i, words[i]);
 540    }
 541
 542    env->regs[0] = deposit64(env->regs[0], 0, 8, max_m1);
 543    return (count_m1 >= max_m1 ? 0 : 3);
 544}
 545