qemu/target/s390x/cpu.c
<<
>>
Prefs
   1/*
   2 * QEMU S/390 CPU
   3 *
   4 * Copyright (c) 2009 Ulrich Hecht
   5 * Copyright (c) 2011 Alexander Graf
   6 * Copyright (c) 2012 SUSE LINUX Products GmbH
   7 * Copyright (c) 2012 IBM Corp.
   8 *
   9 * This program is free software; you can redistribute it and/or modify
  10 * it under the terms of the GNU General Public License as published by
  11 * the Free Software Foundation; either version 2 of the License, or
  12 * (at your option) any later version.
  13 *
  14 * This program is distributed in the hope that it will be useful,
  15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17 * General Public License for more details.
  18 *
  19 * You should have received a copy of the GNU General Public License
  20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
  21 */
  22
  23#include "qemu/osdep.h"
  24#include "qapi/error.h"
  25#include "cpu.h"
  26#include "internal.h"
  27#include "kvm_s390x.h"
  28#include "sysemu/kvm.h"
  29#include "sysemu/reset.h"
  30#include "qemu/timer.h"
  31#include "qemu/error-report.h"
  32#include "qemu/module.h"
  33#include "trace.h"
  34#include "qapi/visitor.h"
  35#include "qapi/qapi-types-machine.h"
  36#include "qapi/qapi-visit-run-state.h"
  37#include "sysemu/hw_accel.h"
  38#include "hw/qdev-properties.h"
  39#ifndef CONFIG_USER_ONLY
  40#include "hw/s390x/pv.h"
  41#include "hw/boards.h"
  42#include "sysemu/arch_init.h"
  43#include "sysemu/sysemu.h"
  44#include "sysemu/tcg.h"
  45#endif
  46#include "fpu/softfloat-helpers.h"
  47#include "disas/capstone.h"
  48
  49#define CR0_RESET       0xE0UL
  50#define CR14_RESET      0xC2000000UL;
  51
  52static void s390_cpu_set_pc(CPUState *cs, vaddr value)
  53{
  54    S390CPU *cpu = S390_CPU(cs);
  55
  56    cpu->env.psw.addr = value;
  57}
  58
  59static bool s390_cpu_has_work(CPUState *cs)
  60{
  61    S390CPU *cpu = S390_CPU(cs);
  62
  63    /* STOPPED cpus can never wake up */
  64    if (s390_cpu_get_state(cpu) != S390_CPU_STATE_LOAD &&
  65        s390_cpu_get_state(cpu) != S390_CPU_STATE_OPERATING) {
  66        return false;
  67    }
  68
  69    if (!(cs->interrupt_request & CPU_INTERRUPT_HARD)) {
  70        return false;
  71    }
  72
  73    return s390_cpu_has_int(cpu);
  74}
  75
  76#if !defined(CONFIG_USER_ONLY)
  77/* S390CPUClass::load_normal() */
  78static void s390_cpu_load_normal(CPUState *s)
  79{
  80    S390CPU *cpu = S390_CPU(s);
  81    uint64_t spsw;
  82
  83    if (!s390_is_pv()) {
  84        spsw = ldq_phys(s->as, 0);
  85        cpu->env.psw.mask = spsw & PSW_MASK_SHORT_CTRL;
  86        /*
  87         * Invert short psw indication, so SIE will report a specification
  88         * exception if it was not set.
  89         */
  90        cpu->env.psw.mask ^= PSW_MASK_SHORTPSW;
  91        cpu->env.psw.addr = spsw & PSW_MASK_SHORT_ADDR;
  92    } else {
  93        /*
  94         * Firmware requires us to set the load state before we set
  95         * the cpu to operating on protected guests.
  96         */
  97        s390_cpu_set_state(S390_CPU_STATE_LOAD, cpu);
  98    }
  99    s390_cpu_set_state(S390_CPU_STATE_OPERATING, cpu);
 100}
 101#endif
 102
 103/* S390CPUClass::reset() */
 104static void s390_cpu_reset(CPUState *s, cpu_reset_type type)
 105{
 106    S390CPU *cpu = S390_CPU(s);
 107    S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
 108    CPUS390XState *env = &cpu->env;
 109    DeviceState *dev = DEVICE(s);
 110
 111    scc->parent_reset(dev);
 112    cpu->env.sigp_order = 0;
 113    s390_cpu_set_state(S390_CPU_STATE_STOPPED, cpu);
 114
 115    switch (type) {
 116    case S390_CPU_RESET_CLEAR:
 117        memset(env, 0, offsetof(CPUS390XState, start_initial_reset_fields));
 118        /* fall through */
 119    case S390_CPU_RESET_INITIAL:
 120        /* initial reset does not clear everything! */
 121        memset(&env->start_initial_reset_fields, 0,
 122               offsetof(CPUS390XState, start_normal_reset_fields) -
 123               offsetof(CPUS390XState, start_initial_reset_fields));
 124
 125        /* architectured initial value for Breaking-Event-Address register */
 126        env->gbea = 1;
 127
 128        /* architectured initial values for CR 0 and 14 */
 129        env->cregs[0] = CR0_RESET;
 130        env->cregs[14] = CR14_RESET;
 131
 132#if defined(CONFIG_USER_ONLY)
 133        /* user mode should always be allowed to use the full FPU */
 134        env->cregs[0] |= CR0_AFP;
 135        if (s390_has_feat(S390_FEAT_VECTOR)) {
 136            env->cregs[0] |= CR0_VECTOR;
 137        }
 138#endif
 139
 140        /* tininess for underflow is detected before rounding */
 141        set_float_detect_tininess(float_tininess_before_rounding,
 142                                  &env->fpu_status);
 143       /* fall through */
 144    case S390_CPU_RESET_NORMAL:
 145        env->psw.mask &= ~PSW_MASK_RI;
 146        memset(&env->start_normal_reset_fields, 0,
 147               offsetof(CPUS390XState, end_reset_fields) -
 148               offsetof(CPUS390XState, start_normal_reset_fields));
 149
 150        env->pfault_token = -1UL;
 151        env->bpbc = false;
 152        break;
 153    default:
 154        g_assert_not_reached();
 155    }
 156
 157    /* Reset state inside the kernel that we cannot access yet from QEMU. */
 158    if (kvm_enabled()) {
 159        switch (type) {
 160        case S390_CPU_RESET_CLEAR:
 161            kvm_s390_reset_vcpu_clear(cpu);
 162            break;
 163        case S390_CPU_RESET_INITIAL:
 164            kvm_s390_reset_vcpu_initial(cpu);
 165            break;
 166        case S390_CPU_RESET_NORMAL:
 167            kvm_s390_reset_vcpu_normal(cpu);
 168            break;
 169        }
 170    }
 171}
 172
 173#if !defined(CONFIG_USER_ONLY)
 174static void s390_cpu_machine_reset_cb(void *opaque)
 175{
 176    S390CPU *cpu = opaque;
 177
 178    run_on_cpu(CPU(cpu), s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
 179}
 180#endif
 181
 182static void s390_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
 183{
 184    info->mach = bfd_mach_s390_64;
 185    info->print_insn = print_insn_s390;
 186    info->cap_arch = CS_ARCH_SYSZ;
 187    info->cap_insn_unit = 2;
 188    info->cap_insn_split = 6;
 189}
 190
 191static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
 192{
 193    CPUState *cs = CPU(dev);
 194    S390CPUClass *scc = S390_CPU_GET_CLASS(dev);
 195#if !defined(CONFIG_USER_ONLY)
 196    S390CPU *cpu = S390_CPU(dev);
 197#endif
 198    Error *err = NULL;
 199
 200    /* the model has to be realized before qemu_init_vcpu() due to kvm */
 201    s390_realize_cpu_model(cs, &err);
 202    if (err) {
 203        goto out;
 204    }
 205
 206#if !defined(CONFIG_USER_ONLY)
 207    MachineState *ms = MACHINE(qdev_get_machine());
 208    unsigned int max_cpus = ms->smp.max_cpus;
 209    if (cpu->env.core_id >= max_cpus) {
 210        error_setg(&err, "Unable to add CPU with core-id: %" PRIu32
 211                   ", maximum core-id: %d", cpu->env.core_id,
 212                   max_cpus - 1);
 213        goto out;
 214    }
 215
 216    if (cpu_exists(cpu->env.core_id)) {
 217        error_setg(&err, "Unable to add CPU with core-id: %" PRIu32
 218                   ", it already exists", cpu->env.core_id);
 219        goto out;
 220    }
 221
 222    /* sync cs->cpu_index and env->core_id. The latter is needed for TCG. */
 223    cs->cpu_index = cpu->env.core_id;
 224#endif
 225
 226    cpu_exec_realizefn(cs, &err);
 227    if (err != NULL) {
 228        goto out;
 229    }
 230
 231#if !defined(CONFIG_USER_ONLY)
 232    qemu_register_reset(s390_cpu_machine_reset_cb, cpu);
 233#endif
 234    s390_cpu_gdb_init(cs);
 235    qemu_init_vcpu(cs);
 236
 237    /*
 238     * KVM requires the initial CPU reset ioctl to be executed on the target
 239     * CPU thread. CPU hotplug under single-threaded TCG will not work with
 240     * run_on_cpu(), as run_on_cpu() will not work properly if called while
 241     * the main thread is already running but the CPU hasn't been realized.
 242     */
 243    if (kvm_enabled()) {
 244        run_on_cpu(cs, s390_do_cpu_full_reset, RUN_ON_CPU_NULL);
 245    } else {
 246        cpu_reset(cs);
 247    }
 248
 249    scc->parent_realize(dev, &err);
 250out:
 251    error_propagate(errp, err);
 252}
 253
 254#if !defined(CONFIG_USER_ONLY)
 255static GuestPanicInformation *s390_cpu_get_crash_info(CPUState *cs)
 256{
 257    GuestPanicInformation *panic_info;
 258    S390CPU *cpu = S390_CPU(cs);
 259
 260    cpu_synchronize_state(cs);
 261    panic_info = g_malloc0(sizeof(GuestPanicInformation));
 262
 263    panic_info->type = GUEST_PANIC_INFORMATION_TYPE_S390;
 264    panic_info->u.s390.core = cpu->env.core_id;
 265    panic_info->u.s390.psw_mask = cpu->env.psw.mask;
 266    panic_info->u.s390.psw_addr = cpu->env.psw.addr;
 267    panic_info->u.s390.reason = cpu->env.crash_reason;
 268
 269    return panic_info;
 270}
 271
 272static void s390_cpu_get_crash_info_qom(Object *obj, Visitor *v,
 273                                        const char *name, void *opaque,
 274                                        Error **errp)
 275{
 276    CPUState *cs = CPU(obj);
 277    GuestPanicInformation *panic_info;
 278
 279    if (!cs->crash_occurred) {
 280        error_setg(errp, "No crash occurred");
 281        return;
 282    }
 283
 284    panic_info = s390_cpu_get_crash_info(cs);
 285
 286    visit_type_GuestPanicInformation(v, "crash-information", &panic_info,
 287                                     errp);
 288    qapi_free_GuestPanicInformation(panic_info);
 289}
 290#endif
 291
 292static void s390_cpu_initfn(Object *obj)
 293{
 294    CPUState *cs = CPU(obj);
 295    S390CPU *cpu = S390_CPU(obj);
 296
 297    cpu_set_cpustate_pointers(cpu);
 298    cs->exception_index = EXCP_HLT;
 299#if !defined(CONFIG_USER_ONLY)
 300    cs->start_powered_off = true;
 301    object_property_add(obj, "crash-information", "GuestPanicInformation",
 302                        s390_cpu_get_crash_info_qom, NULL, NULL, NULL);
 303    cpu->env.tod_timer =
 304        timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_tod_timer, cpu);
 305    cpu->env.cpu_timer =
 306        timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu);
 307    s390_cpu_set_state(S390_CPU_STATE_STOPPED, cpu);
 308#endif
 309}
 310
 311static void s390_cpu_finalize(Object *obj)
 312{
 313#if !defined(CONFIG_USER_ONLY)
 314    S390CPU *cpu = S390_CPU(obj);
 315
 316    timer_free(cpu->env.tod_timer);
 317    timer_free(cpu->env.cpu_timer);
 318
 319    qemu_unregister_reset(s390_cpu_machine_reset_cb, cpu);
 320    g_free(cpu->irqstate);
 321#endif
 322}
 323
 324#if !defined(CONFIG_USER_ONLY)
 325static bool disabled_wait(CPUState *cpu)
 326{
 327    return cpu->halted && !(S390_CPU(cpu)->env.psw.mask &
 328                            (PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK));
 329}
 330
 331static unsigned s390_count_running_cpus(void)
 332{
 333    CPUState *cpu;
 334    int nr_running = 0;
 335
 336    CPU_FOREACH(cpu) {
 337        uint8_t state = S390_CPU(cpu)->env.cpu_state;
 338        if (state == S390_CPU_STATE_OPERATING ||
 339            state == S390_CPU_STATE_LOAD) {
 340            if (!disabled_wait(cpu)) {
 341                nr_running++;
 342            }
 343        }
 344    }
 345
 346    return nr_running;
 347}
 348
 349unsigned int s390_cpu_halt(S390CPU *cpu)
 350{
 351    CPUState *cs = CPU(cpu);
 352    trace_cpu_halt(cs->cpu_index);
 353
 354    if (!cs->halted) {
 355        cs->halted = 1;
 356        cs->exception_index = EXCP_HLT;
 357    }
 358
 359    return s390_count_running_cpus();
 360}
 361
 362void s390_cpu_unhalt(S390CPU *cpu)
 363{
 364    CPUState *cs = CPU(cpu);
 365    trace_cpu_unhalt(cs->cpu_index);
 366
 367    if (cs->halted) {
 368        cs->halted = 0;
 369        cs->exception_index = -1;
 370    }
 371}
 372
 373unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu)
 374 {
 375    trace_cpu_set_state(CPU(cpu)->cpu_index, cpu_state);
 376
 377    switch (cpu_state) {
 378    case S390_CPU_STATE_STOPPED:
 379    case S390_CPU_STATE_CHECK_STOP:
 380        /* halt the cpu for common infrastructure */
 381        s390_cpu_halt(cpu);
 382        break;
 383    case S390_CPU_STATE_OPERATING:
 384    case S390_CPU_STATE_LOAD:
 385        /*
 386         * Starting a CPU with a PSW WAIT bit set:
 387         * KVM: handles this internally and triggers another WAIT exit.
 388         * TCG: will actually try to continue to run. Don't unhalt, will
 389         *      be done when the CPU actually has work (an interrupt).
 390         */
 391        if (!tcg_enabled() || !(cpu->env.psw.mask & PSW_MASK_WAIT)) {
 392            s390_cpu_unhalt(cpu);
 393        }
 394        break;
 395    default:
 396        error_report("Requested CPU state is not a valid S390 CPU state: %u",
 397                     cpu_state);
 398        exit(1);
 399    }
 400    if (kvm_enabled() && cpu->env.cpu_state != cpu_state) {
 401        kvm_s390_set_cpu_state(cpu, cpu_state);
 402    }
 403    cpu->env.cpu_state = cpu_state;
 404
 405    return s390_count_running_cpus();
 406}
 407
 408int s390_set_memory_limit(uint64_t new_limit, uint64_t *hw_limit)
 409{
 410    if (kvm_enabled()) {
 411        return kvm_s390_set_mem_limit(new_limit, hw_limit);
 412    }
 413    return 0;
 414}
 415
 416void s390_set_max_pagesize(uint64_t pagesize, Error **errp)
 417{
 418    if (kvm_enabled()) {
 419        kvm_s390_set_max_pagesize(pagesize, errp);
 420    }
 421}
 422
 423void s390_cmma_reset(void)
 424{
 425    if (kvm_enabled()) {
 426        kvm_s390_cmma_reset();
 427    }
 428}
 429
 430int s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch_id,
 431                                int vq, bool assign)
 432{
 433    if (kvm_enabled()) {
 434        return kvm_s390_assign_subch_ioeventfd(notifier, sch_id, vq, assign);
 435    } else {
 436        return 0;
 437    }
 438}
 439
 440void s390_crypto_reset(void)
 441{
 442    if (kvm_enabled()) {
 443        kvm_s390_crypto_reset();
 444    }
 445}
 446
 447void s390_enable_css_support(S390CPU *cpu)
 448{
 449    if (kvm_enabled()) {
 450        kvm_s390_enable_css_support(cpu);
 451    }
 452}
 453
 454void s390_do_cpu_set_diag318(CPUState *cs, run_on_cpu_data arg)
 455{
 456    if (kvm_enabled()) {
 457        kvm_s390_set_diag318(cs, arg.host_ulong);
 458    }
 459}
 460#endif
 461
 462static gchar *s390_gdb_arch_name(CPUState *cs)
 463{
 464    return g_strdup("s390:64-bit");
 465}
 466
 467static Property s390x_cpu_properties[] = {
 468#if !defined(CONFIG_USER_ONLY)
 469    DEFINE_PROP_UINT32("core-id", S390CPU, env.core_id, 0),
 470#endif
 471    DEFINE_PROP_END_OF_LIST()
 472};
 473
 474static void s390_cpu_reset_full(DeviceState *dev)
 475{
 476    CPUState *s = CPU(dev);
 477    return s390_cpu_reset(s, S390_CPU_RESET_CLEAR);
 478}
 479
 480#ifdef CONFIG_TCG
 481#include "hw/core/tcg-cpu-ops.h"
 482
 483static struct TCGCPUOps s390_tcg_ops = {
 484    .initialize = s390x_translate_init,
 485    .tlb_fill = s390_cpu_tlb_fill,
 486
 487#if !defined(CONFIG_USER_ONLY)
 488    .cpu_exec_interrupt = s390_cpu_exec_interrupt,
 489    .do_interrupt = s390_cpu_do_interrupt,
 490    .debug_excp_handler = s390x_cpu_debug_excp_handler,
 491    .do_unaligned_access = s390x_cpu_do_unaligned_access,
 492#endif /* !CONFIG_USER_ONLY */
 493};
 494#endif /* CONFIG_TCG */
 495
 496static void s390_cpu_class_init(ObjectClass *oc, void *data)
 497{
 498    S390CPUClass *scc = S390_CPU_CLASS(oc);
 499    CPUClass *cc = CPU_CLASS(scc);
 500    DeviceClass *dc = DEVICE_CLASS(oc);
 501
 502    device_class_set_parent_realize(dc, s390_cpu_realizefn,
 503                                    &scc->parent_realize);
 504    device_class_set_props(dc, s390x_cpu_properties);
 505    dc->user_creatable = true;
 506
 507    device_class_set_parent_reset(dc, s390_cpu_reset_full, &scc->parent_reset);
 508#if !defined(CONFIG_USER_ONLY)
 509    scc->load_normal = s390_cpu_load_normal;
 510#endif
 511    scc->reset = s390_cpu_reset;
 512    cc->class_by_name = s390_cpu_class_by_name,
 513    cc->has_work = s390_cpu_has_work;
 514    cc->dump_state = s390_cpu_dump_state;
 515    cc->set_pc = s390_cpu_set_pc;
 516    cc->gdb_read_register = s390_cpu_gdb_read_register;
 517    cc->gdb_write_register = s390_cpu_gdb_write_register;
 518#ifndef CONFIG_USER_ONLY
 519    cc->get_phys_page_debug = s390_cpu_get_phys_page_debug;
 520    cc->vmsd = &vmstate_s390_cpu;
 521    cc->get_crash_info = s390_cpu_get_crash_info;
 522    cc->write_elf64_note = s390_cpu_write_elf64_note;
 523#endif
 524    cc->disas_set_info = s390_cpu_disas_set_info;
 525    cc->gdb_num_core_regs = S390_NUM_CORE_REGS;
 526    cc->gdb_core_xml_file = "s390x-core64.xml";
 527    cc->gdb_arch_name = s390_gdb_arch_name;
 528
 529    s390_cpu_model_class_register_props(oc);
 530
 531#ifdef CONFIG_TCG
 532    cc->tcg_ops = &s390_tcg_ops;
 533#endif /* CONFIG_TCG */
 534}
 535
 536static const TypeInfo s390_cpu_type_info = {
 537    .name = TYPE_S390_CPU,
 538    .parent = TYPE_CPU,
 539    .instance_size = sizeof(S390CPU),
 540    .instance_align = __alignof__(S390CPU),
 541    .instance_init = s390_cpu_initfn,
 542    .instance_finalize = s390_cpu_finalize,
 543    .abstract = true,
 544    .class_size = sizeof(S390CPUClass),
 545    .class_init = s390_cpu_class_init,
 546};
 547
 548static void s390_cpu_register_types(void)
 549{
 550    type_register_static(&s390_cpu_type_info);
 551}
 552
 553type_init(s390_cpu_register_types)
 554