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 52 static 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 59 static 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() */ 78 static 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() */ 104 static 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) 174 static 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 182 static 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 191 static 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); 250 out: 251 error_propagate(errp, err); 252 } 253 254 #if !defined(CONFIG_USER_ONLY) 255 static 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 272 static 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 292 static 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 311 static void s390_cpu_finalize(Object *obj) 312 { 313 #if !defined(CONFIG_USER_ONLY) 314 S390CPU *cpu = S390_CPU(obj); 315 316 qemu_unregister_reset(s390_cpu_machine_reset_cb, cpu); 317 g_free(cpu->irqstate); 318 #endif 319 } 320 321 #if !defined(CONFIG_USER_ONLY) 322 static bool disabled_wait(CPUState *cpu) 323 { 324 return cpu->halted && !(S390_CPU(cpu)->env.psw.mask & 325 (PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK)); 326 } 327 328 static unsigned s390_count_running_cpus(void) 329 { 330 CPUState *cpu; 331 int nr_running = 0; 332 333 CPU_FOREACH(cpu) { 334 uint8_t state = S390_CPU(cpu)->env.cpu_state; 335 if (state == S390_CPU_STATE_OPERATING || 336 state == S390_CPU_STATE_LOAD) { 337 if (!disabled_wait(cpu)) { 338 nr_running++; 339 } 340 } 341 } 342 343 return nr_running; 344 } 345 346 unsigned int s390_cpu_halt(S390CPU *cpu) 347 { 348 CPUState *cs = CPU(cpu); 349 trace_cpu_halt(cs->cpu_index); 350 351 if (!cs->halted) { 352 cs->halted = 1; 353 cs->exception_index = EXCP_HLT; 354 } 355 356 return s390_count_running_cpus(); 357 } 358 359 void s390_cpu_unhalt(S390CPU *cpu) 360 { 361 CPUState *cs = CPU(cpu); 362 trace_cpu_unhalt(cs->cpu_index); 363 364 if (cs->halted) { 365 cs->halted = 0; 366 cs->exception_index = -1; 367 } 368 } 369 370 unsigned int s390_cpu_set_state(uint8_t cpu_state, S390CPU *cpu) 371 { 372 trace_cpu_set_state(CPU(cpu)->cpu_index, cpu_state); 373 374 switch (cpu_state) { 375 case S390_CPU_STATE_STOPPED: 376 case S390_CPU_STATE_CHECK_STOP: 377 /* halt the cpu for common infrastructure */ 378 s390_cpu_halt(cpu); 379 break; 380 case S390_CPU_STATE_OPERATING: 381 case S390_CPU_STATE_LOAD: 382 /* 383 * Starting a CPU with a PSW WAIT bit set: 384 * KVM: handles this internally and triggers another WAIT exit. 385 * TCG: will actually try to continue to run. Don't unhalt, will 386 * be done when the CPU actually has work (an interrupt). 387 */ 388 if (!tcg_enabled() || !(cpu->env.psw.mask & PSW_MASK_WAIT)) { 389 s390_cpu_unhalt(cpu); 390 } 391 break; 392 default: 393 error_report("Requested CPU state is not a valid S390 CPU state: %u", 394 cpu_state); 395 exit(1); 396 } 397 if (kvm_enabled() && cpu->env.cpu_state != cpu_state) { 398 kvm_s390_set_cpu_state(cpu, cpu_state); 399 } 400 cpu->env.cpu_state = cpu_state; 401 402 return s390_count_running_cpus(); 403 } 404 405 int s390_set_memory_limit(uint64_t new_limit, uint64_t *hw_limit) 406 { 407 if (kvm_enabled()) { 408 return kvm_s390_set_mem_limit(new_limit, hw_limit); 409 } 410 return 0; 411 } 412 413 void s390_set_max_pagesize(uint64_t pagesize, Error **errp) 414 { 415 if (kvm_enabled()) { 416 kvm_s390_set_max_pagesize(pagesize, errp); 417 } 418 } 419 420 void s390_cmma_reset(void) 421 { 422 if (kvm_enabled()) { 423 kvm_s390_cmma_reset(); 424 } 425 } 426 427 int s390_assign_subch_ioeventfd(EventNotifier *notifier, uint32_t sch_id, 428 int vq, bool assign) 429 { 430 if (kvm_enabled()) { 431 return kvm_s390_assign_subch_ioeventfd(notifier, sch_id, vq, assign); 432 } else { 433 return 0; 434 } 435 } 436 437 void s390_crypto_reset(void) 438 { 439 if (kvm_enabled()) { 440 kvm_s390_crypto_reset(); 441 } 442 } 443 444 void s390_enable_css_support(S390CPU *cpu) 445 { 446 if (kvm_enabled()) { 447 kvm_s390_enable_css_support(cpu); 448 } 449 } 450 451 void s390_do_cpu_set_diag318(CPUState *cs, run_on_cpu_data arg) 452 { 453 if (kvm_enabled()) { 454 kvm_s390_set_diag318(cs, arg.host_ulong); 455 } 456 } 457 #endif 458 459 static gchar *s390_gdb_arch_name(CPUState *cs) 460 { 461 return g_strdup("s390:64-bit"); 462 } 463 464 static Property s390x_cpu_properties[] = { 465 #if !defined(CONFIG_USER_ONLY) 466 DEFINE_PROP_UINT32("core-id", S390CPU, env.core_id, 0), 467 #endif 468 DEFINE_PROP_END_OF_LIST() 469 }; 470 471 static void s390_cpu_reset_full(DeviceState *dev) 472 { 473 CPUState *s = CPU(dev); 474 return s390_cpu_reset(s, S390_CPU_RESET_CLEAR); 475 } 476 477 static void s390_cpu_class_init(ObjectClass *oc, void *data) 478 { 479 S390CPUClass *scc = S390_CPU_CLASS(oc); 480 CPUClass *cc = CPU_CLASS(scc); 481 DeviceClass *dc = DEVICE_CLASS(oc); 482 483 device_class_set_parent_realize(dc, s390_cpu_realizefn, 484 &scc->parent_realize); 485 device_class_set_props(dc, s390x_cpu_properties); 486 dc->user_creatable = true; 487 488 device_class_set_parent_reset(dc, s390_cpu_reset_full, &scc->parent_reset); 489 #if !defined(CONFIG_USER_ONLY) 490 scc->load_normal = s390_cpu_load_normal; 491 #endif 492 scc->reset = s390_cpu_reset; 493 cc->class_by_name = s390_cpu_class_by_name, 494 cc->has_work = s390_cpu_has_work; 495 #ifdef CONFIG_TCG 496 cc->do_interrupt = s390_cpu_do_interrupt; 497 #endif 498 cc->dump_state = s390_cpu_dump_state; 499 cc->set_pc = s390_cpu_set_pc; 500 cc->gdb_read_register = s390_cpu_gdb_read_register; 501 cc->gdb_write_register = s390_cpu_gdb_write_register; 502 #ifndef CONFIG_USER_ONLY 503 cc->get_phys_page_debug = s390_cpu_get_phys_page_debug; 504 cc->vmsd = &vmstate_s390_cpu; 505 cc->get_crash_info = s390_cpu_get_crash_info; 506 cc->write_elf64_note = s390_cpu_write_elf64_note; 507 #ifdef CONFIG_TCG 508 cc->cpu_exec_interrupt = s390_cpu_exec_interrupt; 509 cc->debug_excp_handler = s390x_cpu_debug_excp_handler; 510 cc->do_unaligned_access = s390x_cpu_do_unaligned_access; 511 #endif 512 #endif 513 cc->disas_set_info = s390_cpu_disas_set_info; 514 #ifdef CONFIG_TCG 515 cc->tcg_initialize = s390x_translate_init; 516 cc->tlb_fill = s390_cpu_tlb_fill; 517 #endif 518 519 cc->gdb_num_core_regs = S390_NUM_CORE_REGS; 520 cc->gdb_core_xml_file = "s390x-core64.xml"; 521 cc->gdb_arch_name = s390_gdb_arch_name; 522 523 s390_cpu_model_class_register_props(oc); 524 } 525 526 static const TypeInfo s390_cpu_type_info = { 527 .name = TYPE_S390_CPU, 528 .parent = TYPE_CPU, 529 .instance_size = sizeof(S390CPU), 530 .instance_align = __alignof__(S390CPU), 531 .instance_init = s390_cpu_initfn, 532 .instance_finalize = s390_cpu_finalize, 533 .abstract = true, 534 .class_size = sizeof(S390CPUClass), 535 .class_init = s390_cpu_class_init, 536 }; 537 538 static void s390_cpu_register_types(void) 539 { 540 type_register_static(&s390_cpu_type_info); 541 } 542 543 type_init(s390_cpu_register_types) 544