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 library is free software; you can redistribute it and/or
  10 * modify it under the terms of the GNU Lesser General Public
  11 * License as published by the Free Software Foundation; either
  12 * version 2.1 of the License, or (at your option) any later version.
  13 *
  14 * This library 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 * Lesser General Public License for more details.
  18 *
  19 * You should have received a copy of the GNU Lesser General Public
  20 * License along with this library; if not, see
  21 * <http://www.gnu.org/licenses/lgpl-2.1.html>
  22 * Contributions after 2012-12-11 are licensed under the terms of the
  23 * GNU GPL, version 2 or (at your option) any later version.
  24 */
  25
  26#include "qemu/osdep.h"
  27#include "qapi/error.h"
  28#include "cpu.h"
  29#include "qemu-common.h"
  30#include "qemu/cutils.h"
  31#include "qemu/timer.h"
  32#include "qemu/error-report.h"
  33#include "hw/hw.h"
  34#include "trace.h"
  35#include "qapi/visitor.h"
  36#ifndef CONFIG_USER_ONLY
  37#include "sysemu/arch_init.h"
  38#include "sysemu/sysemu.h"
  39#include "hw/s390x/sclp.h"
  40#endif
  41
  42#define CR0_RESET       0xE0UL
  43#define CR14_RESET      0xC2000000UL;
  44
  45/* generate CPU information for cpu -? */
  46void s390_cpu_list(FILE *f, fprintf_function cpu_fprintf)
  47{
  48#ifdef CONFIG_KVM
  49    (*cpu_fprintf)(f, "s390 %16s\n", "host");
  50#endif
  51}
  52
  53#ifndef CONFIG_USER_ONLY
  54CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
  55{
  56    CpuDefinitionInfoList *entry;
  57    CpuDefinitionInfo *info;
  58
  59    info = g_malloc0(sizeof(*info));
  60    info->name = g_strdup("host");
  61
  62    entry = g_malloc0(sizeof(*entry));
  63    entry->value = info;
  64
  65    return entry;
  66}
  67#endif
  68
  69static void s390_cpu_set_pc(CPUState *cs, vaddr value)
  70{
  71    S390CPU *cpu = S390_CPU(cs);
  72
  73    cpu->env.psw.addr = value;
  74}
  75
  76static bool s390_cpu_has_work(CPUState *cs)
  77{
  78    S390CPU *cpu = S390_CPU(cs);
  79    CPUS390XState *env = &cpu->env;
  80
  81    return (cs->interrupt_request & CPU_INTERRUPT_HARD) &&
  82           (env->psw.mask & PSW_MASK_EXT);
  83}
  84
  85#if !defined(CONFIG_USER_ONLY)
  86/* S390CPUClass::load_normal() */
  87static void s390_cpu_load_normal(CPUState *s)
  88{
  89    S390CPU *cpu = S390_CPU(s);
  90    cpu->env.psw.addr = ldl_phys(s->as, 4) & PSW_MASK_ESA_ADDR;
  91    cpu->env.psw.mask = PSW_MASK_32 | PSW_MASK_64;
  92    s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
  93}
  94#endif
  95
  96/* S390CPUClass::cpu_reset() */
  97static void s390_cpu_reset(CPUState *s)
  98{
  99    S390CPU *cpu = S390_CPU(s);
 100    S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
 101    CPUS390XState *env = &cpu->env;
 102
 103    env->pfault_token = -1UL;
 104    scc->parent_reset(s);
 105    cpu->env.sigp_order = 0;
 106    s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
 107    tlb_flush(s, 1);
 108}
 109
 110/* S390CPUClass::initial_reset() */
 111static void s390_cpu_initial_reset(CPUState *s)
 112{
 113    S390CPU *cpu = S390_CPU(s);
 114    CPUS390XState *env = &cpu->env;
 115    int i;
 116
 117    s390_cpu_reset(s);
 118    /* initial reset does not touch regs,fregs and aregs */
 119    memset(&env->fpc, 0, offsetof(CPUS390XState, cpu_num) -
 120                         offsetof(CPUS390XState, fpc));
 121
 122    /* architectured initial values for CR 0 and 14 */
 123    env->cregs[0] = CR0_RESET;
 124    env->cregs[14] = CR14_RESET;
 125
 126    /* architectured initial value for Breaking-Event-Address register */
 127    env->gbea = 1;
 128
 129    env->pfault_token = -1UL;
 130    env->ext_index = -1;
 131    for (i = 0; i < ARRAY_SIZE(env->io_index); i++) {
 132        env->io_index[i] = -1;
 133    }
 134
 135    /* tininess for underflow is detected before rounding */
 136    set_float_detect_tininess(float_tininess_before_rounding,
 137                              &env->fpu_status);
 138
 139    /* Reset state inside the kernel that we cannot access yet from QEMU. */
 140    if (kvm_enabled()) {
 141        kvm_s390_reset_vcpu(cpu);
 142    }
 143    tlb_flush(s, 1);
 144}
 145
 146/* CPUClass:reset() */
 147static void s390_cpu_full_reset(CPUState *s)
 148{
 149    S390CPU *cpu = S390_CPU(s);
 150    S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
 151    CPUS390XState *env = &cpu->env;
 152    int i;
 153
 154    scc->parent_reset(s);
 155    cpu->env.sigp_order = 0;
 156    s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
 157
 158    memset(env, 0, offsetof(CPUS390XState, cpu_num));
 159
 160    /* architectured initial values for CR 0 and 14 */
 161    env->cregs[0] = CR0_RESET;
 162    env->cregs[14] = CR14_RESET;
 163
 164    /* architectured initial value for Breaking-Event-Address register */
 165    env->gbea = 1;
 166
 167    env->pfault_token = -1UL;
 168    env->ext_index = -1;
 169    for (i = 0; i < ARRAY_SIZE(env->io_index); i++) {
 170        env->io_index[i] = -1;
 171    }
 172
 173    /* tininess for underflow is detected before rounding */
 174    set_float_detect_tininess(float_tininess_before_rounding,
 175                              &env->fpu_status);
 176
 177    /* Reset state inside the kernel that we cannot access yet from QEMU. */
 178    if (kvm_enabled()) {
 179        kvm_s390_reset_vcpu(cpu);
 180    }
 181    tlb_flush(s, 1);
 182}
 183
 184#if !defined(CONFIG_USER_ONLY)
 185static void s390_cpu_machine_reset_cb(void *opaque)
 186{
 187    S390CPU *cpu = opaque;
 188
 189    run_on_cpu(CPU(cpu), s390_do_cpu_full_reset, CPU(cpu));
 190}
 191#endif
 192
 193static void s390_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
 194{
 195    info->mach = bfd_mach_s390_64;
 196    info->print_insn = print_insn_s390;
 197}
 198
 199static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
 200{
 201    CPUState *cs = CPU(dev);
 202    S390CPUClass *scc = S390_CPU_GET_CLASS(dev);
 203    S390CPU *cpu = S390_CPU(dev);
 204    CPUS390XState *env = &cpu->env;
 205    Error *err = NULL;
 206
 207#if !defined(CONFIG_USER_ONLY)
 208    if (cpu->id >= max_cpus) {
 209        error_setg(&err, "Unable to add CPU: %" PRIi64
 210                   ", max allowed: %d", cpu->id, max_cpus - 1);
 211        goto out;
 212    }
 213#endif
 214    if (cpu_exists(cpu->id)) {
 215        error_setg(&err, "Unable to add CPU: %" PRIi64
 216                   ", it already exists", cpu->id);
 217        goto out;
 218    }
 219    if (cpu->id != scc->next_cpu_id) {
 220        error_setg(&err, "Unable to add CPU: %" PRIi64
 221                   ", The next available id is %" PRIi64, cpu->id,
 222                   scc->next_cpu_id);
 223        goto out;
 224    }
 225
 226    cpu_exec_init(cs, &err);
 227    if (err != NULL) {
 228        goto out;
 229    }
 230    scc->next_cpu_id++;
 231
 232#if !defined(CONFIG_USER_ONLY)
 233    qemu_register_reset(s390_cpu_machine_reset_cb, cpu);
 234#endif
 235    env->cpu_num = cpu->id;
 236    s390_cpu_gdb_init(cs);
 237    qemu_init_vcpu(cs);
 238#if !defined(CONFIG_USER_ONLY)
 239    run_on_cpu(cs, s390_do_cpu_full_reset, cs);
 240#else
 241    cpu_reset(cs);
 242#endif
 243
 244    scc->parent_realize(dev, &err);
 245
 246#if !defined(CONFIG_USER_ONLY)
 247    if (dev->hotplugged) {
 248        raise_irq_cpu_hotplug();
 249    }
 250#endif
 251
 252out:
 253    error_propagate(errp, err);
 254}
 255
 256static void s390x_cpu_get_id(Object *obj, Visitor *v, const char *name,
 257                             void *opaque, Error **errp)
 258{
 259    S390CPU *cpu = S390_CPU(obj);
 260    int64_t value = cpu->id;
 261
 262    visit_type_int(v, name, &value, errp);
 263}
 264
 265static void s390x_cpu_set_id(Object *obj, Visitor *v, const char *name,
 266                             void *opaque, Error **errp)
 267{
 268    S390CPU *cpu = S390_CPU(obj);
 269    DeviceState *dev = DEVICE(obj);
 270    const int64_t min = 0;
 271    const int64_t max = UINT32_MAX;
 272    Error *err = NULL;
 273    int64_t value;
 274
 275    if (dev->realized) {
 276        error_setg(errp, "Attempt to set property '%s' on '%s' after "
 277                   "it was realized", name, object_get_typename(obj));
 278        return;
 279    }
 280
 281    visit_type_int(v, name, &value, &err);
 282    if (err) {
 283        error_propagate(errp, err);
 284        return;
 285    }
 286    if (value < min || value > max) {
 287        error_setg(errp, "Property %s.%s doesn't take value %" PRId64
 288                   " (minimum: %" PRId64 ", maximum: %" PRId64 ")" ,
 289                   object_get_typename(obj), name, value, min, max);
 290        return;
 291    }
 292    cpu->id = value;
 293}
 294
 295static void s390_cpu_initfn(Object *obj)
 296{
 297    CPUState *cs = CPU(obj);
 298    S390CPU *cpu = S390_CPU(obj);
 299    CPUS390XState *env = &cpu->env;
 300    static bool inited;
 301#if !defined(CONFIG_USER_ONLY)
 302    struct tm tm;
 303#endif
 304
 305    cs->env_ptr = env;
 306    cs->halted = 1;
 307    cs->exception_index = EXCP_HLT;
 308    object_property_add(OBJECT(cpu), "id", "int64_t", s390x_cpu_get_id,
 309                        s390x_cpu_set_id, NULL, NULL, NULL);
 310#if !defined(CONFIG_USER_ONLY)
 311    qemu_get_timedate(&tm, 0);
 312    env->tod_offset = TOD_UNIX_EPOCH +
 313                      (time2tod(mktimegm(&tm)) * 1000000000ULL);
 314    env->tod_basetime = 0;
 315    env->tod_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_tod_timer, cpu);
 316    env->cpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu);
 317    s390_cpu_set_state(CPU_STATE_STOPPED, cpu);
 318#endif
 319
 320    if (tcg_enabled() && !inited) {
 321        inited = true;
 322        s390x_translate_init();
 323    }
 324}
 325
 326static void s390_cpu_finalize(Object *obj)
 327{
 328#if !defined(CONFIG_USER_ONLY)
 329    S390CPU *cpu = S390_CPU(obj);
 330
 331    qemu_unregister_reset(s390_cpu_machine_reset_cb, cpu);
 332    g_free(cpu->irqstate);
 333#endif
 334}
 335
 336#if !defined(CONFIG_USER_ONLY)
 337static bool disabled_wait(CPUState *cpu)
 338{
 339    return cpu->halted && !(S390_CPU(cpu)->env.psw.mask &
 340                            (PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK));
 341}
 342
 343static unsigned s390_count_running_cpus(void)
 344{
 345    CPUState *cpu;
 346    int nr_running = 0;
 347
 348    CPU_FOREACH(cpu) {
 349        uint8_t state = S390_CPU(cpu)->env.cpu_state;
 350        if (state == CPU_STATE_OPERATING ||
 351            state == CPU_STATE_LOAD) {
 352            if (!disabled_wait(cpu)) {
 353                nr_running++;
 354            }
 355        }
 356    }
 357
 358    return nr_running;
 359}
 360
 361unsigned int s390_cpu_halt(S390CPU *cpu)
 362{
 363    CPUState *cs = CPU(cpu);
 364    trace_cpu_halt(cs->cpu_index);
 365
 366    if (!cs->halted) {
 367        cs->halted = 1;
 368        cs->exception_index = EXCP_HLT;
 369    }
 370
 371    return s390_count_running_cpus();
 372}
 373
 374void s390_cpu_unhalt(S390CPU *cpu)
 375{
 376    CPUState *cs = CPU(cpu);
 377    trace_cpu_unhalt(cs->cpu_index);
 378
 379    if (cs->halted) {
 380        cs->halted = 0;
 381        cs->exception_index = -1;
 382    }
 383}
 384
 385unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu)
 386 {
 387    trace_cpu_set_state(CPU(cpu)->cpu_index, cpu_state);
 388
 389    switch (cpu_state) {
 390    case CPU_STATE_STOPPED:
 391    case CPU_STATE_CHECK_STOP:
 392        /* halt the cpu for common infrastructure */
 393        s390_cpu_halt(cpu);
 394        break;
 395    case CPU_STATE_OPERATING:
 396    case CPU_STATE_LOAD:
 397        /* unhalt the cpu for common infrastructure */
 398        s390_cpu_unhalt(cpu);
 399        break;
 400    default:
 401        error_report("Requested CPU state is not a valid S390 CPU state: %u",
 402                     cpu_state);
 403        exit(1);
 404    }
 405    if (kvm_enabled() && cpu->env.cpu_state != cpu_state) {
 406        kvm_s390_set_cpu_state(cpu, cpu_state);
 407    }
 408    cpu->env.cpu_state = cpu_state;
 409
 410    return s390_count_running_cpus();
 411}
 412#endif
 413
 414static gchar *s390_gdb_arch_name(CPUState *cs)
 415{
 416    return g_strdup("s390:64-bit");
 417}
 418
 419static void s390_cpu_class_init(ObjectClass *oc, void *data)
 420{
 421    S390CPUClass *scc = S390_CPU_CLASS(oc);
 422    CPUClass *cc = CPU_CLASS(scc);
 423    DeviceClass *dc = DEVICE_CLASS(oc);
 424
 425    scc->next_cpu_id = 0;
 426    scc->parent_realize = dc->realize;
 427    dc->realize = s390_cpu_realizefn;
 428
 429    scc->parent_reset = cc->reset;
 430#if !defined(CONFIG_USER_ONLY)
 431    scc->load_normal = s390_cpu_load_normal;
 432#endif
 433    scc->cpu_reset = s390_cpu_reset;
 434    scc->initial_cpu_reset = s390_cpu_initial_reset;
 435    cc->reset = s390_cpu_full_reset;
 436    cc->has_work = s390_cpu_has_work;
 437    cc->do_interrupt = s390_cpu_do_interrupt;
 438    cc->dump_state = s390_cpu_dump_state;
 439    cc->set_pc = s390_cpu_set_pc;
 440    cc->gdb_read_register = s390_cpu_gdb_read_register;
 441    cc->gdb_write_register = s390_cpu_gdb_write_register;
 442#ifdef CONFIG_USER_ONLY
 443    cc->handle_mmu_fault = s390_cpu_handle_mmu_fault;
 444#else
 445    cc->get_phys_page_debug = s390_cpu_get_phys_page_debug;
 446    cc->vmsd = &vmstate_s390_cpu;
 447    cc->write_elf64_note = s390_cpu_write_elf64_note;
 448    cc->cpu_exec_interrupt = s390_cpu_exec_interrupt;
 449    cc->debug_excp_handler = s390x_cpu_debug_excp_handler;
 450#endif
 451    cc->disas_set_info = s390_cpu_disas_set_info;
 452
 453    cc->gdb_num_core_regs = S390_NUM_CORE_REGS;
 454    cc->gdb_core_xml_file = "s390x-core64.xml";
 455    cc->gdb_arch_name = s390_gdb_arch_name;
 456
 457    /*
 458     * Reason: s390_cpu_realizefn() calls cpu_exec_init(), which saves
 459     * the object in cpus -> dangling pointer after final
 460     * object_unref().
 461     */
 462    dc->cannot_destroy_with_object_finalize_yet = true;
 463}
 464
 465static const TypeInfo s390_cpu_type_info = {
 466    .name = TYPE_S390_CPU,
 467    .parent = TYPE_CPU,
 468    .instance_size = sizeof(S390CPU),
 469    .instance_init = s390_cpu_initfn,
 470    .instance_finalize = s390_cpu_finalize,
 471    .abstract = false,
 472    .class_size = sizeof(S390CPUClass),
 473    .class_init = s390_cpu_class_init,
 474};
 475
 476static void s390_cpu_register_types(void)
 477{
 478    type_register_static(&s390_cpu_type_info);
 479}
 480
 481type_init(s390_cpu_register_types)
 482