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 "s390x-internal.h" 27 #include "kvm/kvm_s390x.h" 28 #include "system/kvm.h" 29 #include "qemu/module.h" 30 #include "trace.h" 31 #include "qapi/qapi-types-machine.h" 32 #include "system/hw_accel.h" 33 #include "hw/qdev-properties.h" 34 #include "hw/qdev-properties-system.h" 35 #include "hw/resettable.h" 36 #include "fpu/softfloat-helpers.h" 37 #include "disas/capstone.h" 38 #include "system/tcg.h" 39 #ifndef CONFIG_USER_ONLY 40 #include "system/reset.h" 41 #endif 42 #include "hw/s390x/cpu-topology.h" 43 44 #define CR0_RESET 0xE0UL 45 #define CR14_RESET 0xC2000000UL; 46 47 #ifndef CONFIG_USER_ONLY 48 static bool is_early_exception_psw(uint64_t mask, uint64_t addr) 49 { 50 if (mask & PSW_MASK_RESERVED) { 51 return true; 52 } 53 54 switch (mask & (PSW_MASK_32 | PSW_MASK_64)) { 55 case 0: 56 return addr & ~0xffffffULL; 57 case PSW_MASK_32: 58 return addr & ~0x7fffffffULL; 59 case PSW_MASK_32 | PSW_MASK_64: 60 return false; 61 default: /* PSW_MASK_64 */ 62 return true; 63 } 64 } 65 #endif 66 67 void s390_cpu_set_psw(CPUS390XState *env, uint64_t mask, uint64_t addr) 68 { 69 #ifndef CONFIG_USER_ONLY 70 uint64_t old_mask = env->psw.mask; 71 #endif 72 73 env->psw.addr = addr; 74 env->psw.mask = mask; 75 76 /* KVM will handle all WAITs and trigger a WAIT exit on disabled_wait */ 77 if (!tcg_enabled()) { 78 return; 79 } 80 env->cc_op = (mask >> 44) & 3; 81 82 #ifndef CONFIG_USER_ONLY 83 if (is_early_exception_psw(mask, addr)) { 84 env->int_pgm_ilen = 0; 85 trigger_pgm_exception(env, PGM_SPECIFICATION); 86 return; 87 } 88 89 if ((old_mask ^ mask) & PSW_MASK_PER) { 90 s390_cpu_recompute_watchpoints(env_cpu(env)); 91 } 92 93 if (mask & PSW_MASK_WAIT) { 94 s390_handle_wait(env_archcpu(env)); 95 } 96 #endif 97 } 98 99 uint64_t s390_cpu_get_psw_mask(CPUS390XState *env) 100 { 101 uint64_t r = env->psw.mask; 102 103 if (tcg_enabled()) { 104 uint64_t cc = calc_cc(env, env->cc_op, env->cc_src, 105 env->cc_dst, env->cc_vr); 106 107 assert(cc <= 3); 108 r &= ~PSW_MASK_CC; 109 r |= cc << 44; 110 } 111 112 return r; 113 } 114 115 static void s390_cpu_set_pc(CPUState *cs, vaddr value) 116 { 117 S390CPU *cpu = S390_CPU(cs); 118 119 cpu->env.psw.addr = value; 120 } 121 122 static vaddr s390_cpu_get_pc(CPUState *cs) 123 { 124 S390CPU *cpu = S390_CPU(cs); 125 126 return cpu->env.psw.addr; 127 } 128 129 static bool s390_cpu_has_work(CPUState *cs) 130 { 131 S390CPU *cpu = S390_CPU(cs); 132 133 /* STOPPED cpus can never wake up */ 134 if (s390_cpu_get_state(cpu) != S390_CPU_STATE_LOAD && 135 s390_cpu_get_state(cpu) != S390_CPU_STATE_OPERATING) { 136 return false; 137 } 138 139 if (!(cs->interrupt_request & CPU_INTERRUPT_HARD)) { 140 return false; 141 } 142 143 return s390_cpu_has_int(cpu); 144 } 145 146 static int s390x_cpu_mmu_index(CPUState *cs, bool ifetch) 147 { 148 return s390x_env_mmu_index(cpu_env(cs), ifetch); 149 } 150 151 static void s390_query_cpu_fast(CPUState *cpu, CpuInfoFast *value) 152 { 153 S390CPU *s390_cpu = S390_CPU(cpu); 154 155 value->u.s390x.cpu_state = s390_cpu->env.cpu_state; 156 #if !defined(CONFIG_USER_ONLY) 157 if (s390_has_topology()) { 158 value->u.s390x.has_dedicated = true; 159 value->u.s390x.dedicated = s390_cpu->env.dedicated; 160 value->u.s390x.has_entitlement = true; 161 value->u.s390x.entitlement = s390_cpu->env.entitlement; 162 } 163 #endif 164 } 165 166 /* S390CPUClass Resettable reset_hold phase method */ 167 static void s390_cpu_reset_hold(Object *obj, ResetType type) 168 { 169 S390CPU *cpu = S390_CPU(obj); 170 S390CPUClass *scc = S390_CPU_GET_CLASS(cpu); 171 CPUS390XState *env = &cpu->env; 172 173 if (scc->parent_phases.hold) { 174 scc->parent_phases.hold(obj, type); 175 } 176 cpu->env.sigp_order = 0; 177 s390_cpu_set_state(S390_CPU_STATE_STOPPED, cpu); 178 179 switch (type) { 180 default: 181 /* RESET_TYPE_COLD: power on or "clear" reset */ 182 memset(env, 0, offsetof(CPUS390XState, start_initial_reset_fields)); 183 /* fall through */ 184 case RESET_TYPE_S390_CPU_INITIAL: 185 /* initial reset does not clear everything! */ 186 memset(&env->start_initial_reset_fields, 0, 187 offsetof(CPUS390XState, start_normal_reset_fields) - 188 offsetof(CPUS390XState, start_initial_reset_fields)); 189 190 /* architectured initial value for Breaking-Event-Address register */ 191 env->gbea = 1; 192 193 /* architectured initial values for CR 0 and 14 */ 194 env->cregs[0] = CR0_RESET; 195 env->cregs[14] = CR14_RESET; 196 197 #if defined(CONFIG_USER_ONLY) 198 /* user mode should always be allowed to use the full FPU */ 199 env->cregs[0] |= CR0_AFP; 200 if (s390_has_feat(S390_FEAT_VECTOR)) { 201 env->cregs[0] |= CR0_VECTOR; 202 } 203 #endif 204 205 /* tininess for underflow is detected before rounding */ 206 set_float_detect_tininess(float_tininess_before_rounding, 207 &env->fpu_status); 208 set_float_2nan_prop_rule(float_2nan_prop_s_ab, &env->fpu_status); 209 set_float_3nan_prop_rule(float_3nan_prop_s_abc, &env->fpu_status); 210 set_float_infzeronan_rule(float_infzeronan_dnan_always, 211 &env->fpu_status); 212 /* Default NaN value: sign bit clear, frac msb set */ 213 set_float_default_nan_pattern(0b01000000, &env->fpu_status); 214 /* fall through */ 215 case RESET_TYPE_S390_CPU_NORMAL: 216 env->psw.mask &= ~PSW_MASK_RI; 217 memset(&env->start_normal_reset_fields, 0, 218 offsetof(CPUS390XState, end_reset_fields) - 219 offsetof(CPUS390XState, start_normal_reset_fields)); 220 221 env->pfault_token = -1UL; 222 env->bpbc = false; 223 break; 224 } 225 226 /* Reset state inside the kernel that we cannot access yet from QEMU. */ 227 if (kvm_enabled()) { 228 switch (type) { 229 default: 230 kvm_s390_reset_vcpu_clear(cpu); 231 break; 232 case RESET_TYPE_S390_CPU_INITIAL: 233 kvm_s390_reset_vcpu_initial(cpu); 234 break; 235 case RESET_TYPE_S390_CPU_NORMAL: 236 kvm_s390_reset_vcpu_normal(cpu); 237 break; 238 } 239 } 240 } 241 242 static void s390_cpu_disas_set_info(CPUState *cpu, disassemble_info *info) 243 { 244 info->mach = bfd_mach_s390_64; 245 info->cap_arch = CS_ARCH_SYSZ; 246 info->cap_insn_unit = 2; 247 info->cap_insn_split = 6; 248 } 249 250 static void s390_cpu_realizefn(DeviceState *dev, Error **errp) 251 { 252 CPUState *cs = CPU(dev); 253 S390CPUClass *scc = S390_CPU_GET_CLASS(dev); 254 Error *err = NULL; 255 256 /* the model has to be realized before qemu_init_vcpu() due to kvm */ 257 s390_realize_cpu_model(cs, &err); 258 if (err) { 259 goto out; 260 } 261 262 #if !defined(CONFIG_USER_ONLY) 263 if (!s390_cpu_system_realize(dev, &err)) { 264 goto out; 265 } 266 #endif 267 268 cpu_exec_realizefn(cs, &err); 269 if (err != NULL) { 270 goto out; 271 } 272 273 #if !defined(CONFIG_USER_ONLY) 274 qemu_register_reset(s390_cpu_machine_reset_cb, S390_CPU(dev)); 275 #endif 276 s390_cpu_gdb_init(cs); 277 qemu_init_vcpu(cs); 278 279 /* 280 * KVM requires the initial CPU reset ioctl to be executed on the target 281 * CPU thread. CPU hotplug under single-threaded TCG will not work with 282 * run_on_cpu(), as run_on_cpu() will not work properly if called while 283 * the main thread is already running but the CPU hasn't been realized. 284 */ 285 if (kvm_enabled()) { 286 run_on_cpu(cs, s390_do_cpu_full_reset, RUN_ON_CPU_NULL); 287 } else { 288 cpu_reset(cs); 289 } 290 291 scc->parent_realize(dev, &err); 292 out: 293 error_propagate(errp, err); 294 } 295 296 static void s390_cpu_initfn(Object *obj) 297 { 298 CPUState *cs = CPU(obj); 299 300 cs->exception_index = EXCP_HLT; 301 302 #if !defined(CONFIG_USER_ONLY) 303 s390_cpu_system_init(obj); 304 #endif 305 } 306 307 static const gchar *s390_gdb_arch_name(CPUState *cs) 308 { 309 return "s390:64-bit"; 310 } 311 312 #ifndef CONFIG_USER_ONLY 313 static const Property s390x_cpu_properties[] = { 314 DEFINE_PROP_UINT32("core-id", S390CPU, env.core_id, 0), 315 DEFINE_PROP_INT32("socket-id", S390CPU, env.socket_id, -1), 316 DEFINE_PROP_INT32("book-id", S390CPU, env.book_id, -1), 317 DEFINE_PROP_INT32("drawer-id", S390CPU, env.drawer_id, -1), 318 DEFINE_PROP_BOOL("dedicated", S390CPU, env.dedicated, false), 319 DEFINE_PROP_CPUS390ENTITLEMENT("entitlement", S390CPU, env.entitlement, 320 S390_CPU_ENTITLEMENT_AUTO), 321 }; 322 #endif 323 324 #ifdef CONFIG_TCG 325 #include "hw/core/tcg-cpu-ops.h" 326 327 void cpu_get_tb_cpu_state(CPUS390XState *env, vaddr *pc, 328 uint64_t *cs_base, uint32_t *pflags) 329 { 330 uint32_t flags; 331 332 if (env->psw.addr & 1) { 333 /* 334 * Instructions must be at even addresses. 335 * This needs to be checked before address translation. 336 */ 337 env->int_pgm_ilen = 2; /* see s390_cpu_tlb_fill() */ 338 tcg_s390_program_interrupt(env, PGM_SPECIFICATION, 0); 339 } 340 341 *pc = env->psw.addr; 342 *cs_base = env->ex_value; 343 344 flags = (env->psw.mask >> FLAG_MASK_PSW_SHIFT) & FLAG_MASK_PSW; 345 if (env->psw.mask & PSW_MASK_PER) { 346 flags |= env->cregs[9] & (FLAG_MASK_PER_BRANCH | 347 FLAG_MASK_PER_IFETCH | 348 FLAG_MASK_PER_IFETCH_NULLIFY); 349 if ((env->cregs[9] & PER_CR9_EVENT_STORE) && 350 (env->cregs[9] & PER_CR9_EVENT_STORE_REAL)) { 351 flags |= FLAG_MASK_PER_STORE_REAL; 352 } 353 } 354 if (env->cregs[0] & CR0_AFP) { 355 flags |= FLAG_MASK_AFP; 356 } 357 if (env->cregs[0] & CR0_VECTOR) { 358 flags |= FLAG_MASK_VECTOR; 359 } 360 *pflags = flags; 361 } 362 363 static const TCGCPUOps s390_tcg_ops = { 364 .initialize = s390x_translate_init, 365 .translate_code = s390x_translate_code, 366 .restore_state_to_opc = s390x_restore_state_to_opc, 367 368 #ifdef CONFIG_USER_ONLY 369 .record_sigsegv = s390_cpu_record_sigsegv, 370 .record_sigbus = s390_cpu_record_sigbus, 371 #else 372 .tlb_fill = s390_cpu_tlb_fill, 373 .cpu_exec_interrupt = s390_cpu_exec_interrupt, 374 .cpu_exec_halt = s390_cpu_has_work, 375 .do_interrupt = s390_cpu_do_interrupt, 376 .debug_excp_handler = s390x_cpu_debug_excp_handler, 377 .do_unaligned_access = s390x_cpu_do_unaligned_access, 378 #endif /* !CONFIG_USER_ONLY */ 379 }; 380 #endif /* CONFIG_TCG */ 381 382 static void s390_cpu_class_init(ObjectClass *oc, void *data) 383 { 384 S390CPUClass *scc = S390_CPU_CLASS(oc); 385 CPUClass *cc = CPU_CLASS(scc); 386 DeviceClass *dc = DEVICE_CLASS(oc); 387 ResettableClass *rc = RESETTABLE_CLASS(oc); 388 389 device_class_set_parent_realize(dc, s390_cpu_realizefn, 390 &scc->parent_realize); 391 dc->user_creatable = true; 392 393 resettable_class_set_parent_phases(rc, NULL, s390_cpu_reset_hold, NULL, 394 &scc->parent_phases); 395 396 cc->class_by_name = s390_cpu_class_by_name, 397 cc->has_work = s390_cpu_has_work; 398 cc->mmu_index = s390x_cpu_mmu_index; 399 cc->dump_state = s390_cpu_dump_state; 400 cc->query_cpu_fast = s390_query_cpu_fast; 401 cc->set_pc = s390_cpu_set_pc; 402 cc->get_pc = s390_cpu_get_pc; 403 cc->gdb_read_register = s390_cpu_gdb_read_register; 404 cc->gdb_write_register = s390_cpu_gdb_write_register; 405 #ifndef CONFIG_USER_ONLY 406 device_class_set_props(dc, s390x_cpu_properties); 407 s390_cpu_system_class_init(cc); 408 #endif 409 cc->disas_set_info = s390_cpu_disas_set_info; 410 cc->gdb_core_xml_file = "s390x-core64.xml"; 411 cc->gdb_arch_name = s390_gdb_arch_name; 412 413 s390_cpu_model_class_register_props(oc); 414 415 #ifdef CONFIG_TCG 416 cc->tcg_ops = &s390_tcg_ops; 417 #endif /* CONFIG_TCG */ 418 } 419 420 static const TypeInfo s390_cpu_type_info = { 421 .name = TYPE_S390_CPU, 422 .parent = TYPE_CPU, 423 .instance_size = sizeof(S390CPU), 424 .instance_align = __alignof__(S390CPU), 425 .instance_init = s390_cpu_initfn, 426 427 #ifndef CONFIG_USER_ONLY 428 .instance_finalize = s390_cpu_finalize, 429 #endif /* !CONFIG_USER_ONLY */ 430 431 .abstract = true, 432 .class_size = sizeof(S390CPUClass), 433 .class_init = s390_cpu_class_init, 434 }; 435 436 static void s390_cpu_register_types(void) 437 { 438 type_register_static(&s390_cpu_type_info); 439 } 440 441 type_init(s390_cpu_register_types) 442