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