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 "sysemu/kvm.h" 29 #include "qemu/module.h" 30 #include "trace.h" 31 #include "qapi/qapi-types-machine.h" 32 #include "sysemu/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 "sysemu/tcg.h" 39 #ifndef CONFIG_USER_ONLY 40 #include "sysemu/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 /* fall through */ 209 case RESET_TYPE_S390_CPU_NORMAL: 210 env->psw.mask &= ~PSW_MASK_RI; 211 memset(&env->start_normal_reset_fields, 0, 212 offsetof(CPUS390XState, end_reset_fields) - 213 offsetof(CPUS390XState, start_normal_reset_fields)); 214 215 env->pfault_token = -1UL; 216 env->bpbc = false; 217 break; 218 } 219 220 /* Reset state inside the kernel that we cannot access yet from QEMU. */ 221 if (kvm_enabled()) { 222 switch (type) { 223 default: 224 kvm_s390_reset_vcpu_clear(cpu); 225 break; 226 case RESET_TYPE_S390_CPU_INITIAL: 227 kvm_s390_reset_vcpu_initial(cpu); 228 break; 229 case RESET_TYPE_S390_CPU_NORMAL: 230 kvm_s390_reset_vcpu_normal(cpu); 231 break; 232 } 233 } 234 } 235 236 static void s390_cpu_disas_set_info(CPUState *cpu, disassemble_info *info) 237 { 238 info->mach = bfd_mach_s390_64; 239 info->cap_arch = CS_ARCH_SYSZ; 240 info->cap_insn_unit = 2; 241 info->cap_insn_split = 6; 242 } 243 244 static void s390_cpu_realizefn(DeviceState *dev, Error **errp) 245 { 246 CPUState *cs = CPU(dev); 247 S390CPUClass *scc = S390_CPU_GET_CLASS(dev); 248 Error *err = NULL; 249 250 /* the model has to be realized before qemu_init_vcpu() due to kvm */ 251 s390_realize_cpu_model(cs, &err); 252 if (err) { 253 goto out; 254 } 255 256 #if !defined(CONFIG_USER_ONLY) 257 if (!s390_cpu_realize_sysemu(dev, &err)) { 258 goto out; 259 } 260 #endif 261 262 cpu_exec_realizefn(cs, &err); 263 if (err != NULL) { 264 goto out; 265 } 266 267 #if !defined(CONFIG_USER_ONLY) 268 qemu_register_reset(s390_cpu_machine_reset_cb, S390_CPU(dev)); 269 #endif 270 s390_cpu_gdb_init(cs); 271 qemu_init_vcpu(cs); 272 273 /* 274 * KVM requires the initial CPU reset ioctl to be executed on the target 275 * CPU thread. CPU hotplug under single-threaded TCG will not work with 276 * run_on_cpu(), as run_on_cpu() will not work properly if called while 277 * the main thread is already running but the CPU hasn't been realized. 278 */ 279 if (kvm_enabled()) { 280 run_on_cpu(cs, s390_do_cpu_full_reset, RUN_ON_CPU_NULL); 281 } else { 282 cpu_reset(cs); 283 } 284 285 scc->parent_realize(dev, &err); 286 out: 287 error_propagate(errp, err); 288 } 289 290 static void s390_cpu_initfn(Object *obj) 291 { 292 CPUState *cs = CPU(obj); 293 294 cs->exception_index = EXCP_HLT; 295 296 #if !defined(CONFIG_USER_ONLY) 297 s390_cpu_init_sysemu(obj); 298 #endif 299 } 300 301 static const gchar *s390_gdb_arch_name(CPUState *cs) 302 { 303 return "s390:64-bit"; 304 } 305 306 static Property s390x_cpu_properties[] = { 307 #if !defined(CONFIG_USER_ONLY) 308 DEFINE_PROP_UINT32("core-id", S390CPU, env.core_id, 0), 309 DEFINE_PROP_INT32("socket-id", S390CPU, env.socket_id, -1), 310 DEFINE_PROP_INT32("book-id", S390CPU, env.book_id, -1), 311 DEFINE_PROP_INT32("drawer-id", S390CPU, env.drawer_id, -1), 312 DEFINE_PROP_BOOL("dedicated", S390CPU, env.dedicated, false), 313 DEFINE_PROP_CPUS390ENTITLEMENT("entitlement", S390CPU, env.entitlement, 314 S390_CPU_ENTITLEMENT_AUTO), 315 #endif 316 DEFINE_PROP_END_OF_LIST() 317 }; 318 319 #ifdef CONFIG_TCG 320 #include "hw/core/tcg-cpu-ops.h" 321 322 void cpu_get_tb_cpu_state(CPUS390XState *env, vaddr *pc, 323 uint64_t *cs_base, uint32_t *pflags) 324 { 325 uint32_t flags; 326 327 if (env->psw.addr & 1) { 328 /* 329 * Instructions must be at even addresses. 330 * This needs to be checked before address translation. 331 */ 332 env->int_pgm_ilen = 2; /* see s390_cpu_tlb_fill() */ 333 tcg_s390_program_interrupt(env, PGM_SPECIFICATION, 0); 334 } 335 336 *pc = env->psw.addr; 337 *cs_base = env->ex_value; 338 339 flags = (env->psw.mask >> FLAG_MASK_PSW_SHIFT) & FLAG_MASK_PSW; 340 if (env->psw.mask & PSW_MASK_PER) { 341 flags |= env->cregs[9] & (FLAG_MASK_PER_BRANCH | 342 FLAG_MASK_PER_IFETCH | 343 FLAG_MASK_PER_IFETCH_NULLIFY); 344 if ((env->cregs[9] & PER_CR9_EVENT_STORE) && 345 (env->cregs[9] & PER_CR9_EVENT_STORE_REAL)) { 346 flags |= FLAG_MASK_PER_STORE_REAL; 347 } 348 } 349 if (env->cregs[0] & CR0_AFP) { 350 flags |= FLAG_MASK_AFP; 351 } 352 if (env->cregs[0] & CR0_VECTOR) { 353 flags |= FLAG_MASK_VECTOR; 354 } 355 *pflags = flags; 356 } 357 358 static const TCGCPUOps s390_tcg_ops = { 359 .initialize = s390x_translate_init, 360 .restore_state_to_opc = s390x_restore_state_to_opc, 361 362 #ifdef CONFIG_USER_ONLY 363 .record_sigsegv = s390_cpu_record_sigsegv, 364 .record_sigbus = s390_cpu_record_sigbus, 365 #else 366 .tlb_fill = s390_cpu_tlb_fill, 367 .cpu_exec_interrupt = s390_cpu_exec_interrupt, 368 .cpu_exec_halt = s390_cpu_has_work, 369 .do_interrupt = s390_cpu_do_interrupt, 370 .debug_excp_handler = s390x_cpu_debug_excp_handler, 371 .do_unaligned_access = s390x_cpu_do_unaligned_access, 372 #endif /* !CONFIG_USER_ONLY */ 373 }; 374 #endif /* CONFIG_TCG */ 375 376 static void s390_cpu_class_init(ObjectClass *oc, void *data) 377 { 378 S390CPUClass *scc = S390_CPU_CLASS(oc); 379 CPUClass *cc = CPU_CLASS(scc); 380 DeviceClass *dc = DEVICE_CLASS(oc); 381 ResettableClass *rc = RESETTABLE_CLASS(oc); 382 383 device_class_set_parent_realize(dc, s390_cpu_realizefn, 384 &scc->parent_realize); 385 device_class_set_props(dc, s390x_cpu_properties); 386 dc->user_creatable = true; 387 388 resettable_class_set_parent_phases(rc, NULL, s390_cpu_reset_hold, NULL, 389 &scc->parent_phases); 390 391 cc->class_by_name = s390_cpu_class_by_name, 392 cc->has_work = s390_cpu_has_work; 393 cc->mmu_index = s390x_cpu_mmu_index; 394 cc->dump_state = s390_cpu_dump_state; 395 cc->query_cpu_fast = s390_query_cpu_fast; 396 cc->set_pc = s390_cpu_set_pc; 397 cc->get_pc = s390_cpu_get_pc; 398 cc->gdb_read_register = s390_cpu_gdb_read_register; 399 cc->gdb_write_register = s390_cpu_gdb_write_register; 400 #ifndef CONFIG_USER_ONLY 401 s390_cpu_class_init_sysemu(cc); 402 #endif 403 cc->disas_set_info = s390_cpu_disas_set_info; 404 cc->gdb_core_xml_file = "s390x-core64.xml"; 405 cc->gdb_arch_name = s390_gdb_arch_name; 406 407 s390_cpu_model_class_register_props(oc); 408 409 #ifdef CONFIG_TCG 410 cc->tcg_ops = &s390_tcg_ops; 411 #endif /* CONFIG_TCG */ 412 } 413 414 static const TypeInfo s390_cpu_type_info = { 415 .name = TYPE_S390_CPU, 416 .parent = TYPE_CPU, 417 .instance_size = sizeof(S390CPU), 418 .instance_align = __alignof__(S390CPU), 419 .instance_init = s390_cpu_initfn, 420 421 #ifndef CONFIG_USER_ONLY 422 .instance_finalize = s390_cpu_finalize, 423 #endif /* !CONFIG_USER_ONLY */ 424 425 .abstract = true, 426 .class_size = sizeof(S390CPUClass), 427 .class_init = s390_cpu_class_init, 428 }; 429 430 static void s390_cpu_register_types(void) 431 { 432 type_register_static(&s390_cpu_type_info); 433 } 434 435 type_init(s390_cpu_register_types) 436