1 /* 2 * QEMU Motorola 68k CPU 3 * 4 * Copyright (c) 2012 SUSE LINUX Products GmbH 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, see 18 * <http://www.gnu.org/licenses/lgpl-2.1.html> 19 */ 20 21 #include "qemu/osdep.h" 22 #include "qapi/error.h" 23 #include "cpu.h" 24 #include "qemu-common.h" 25 #include "migration/vmstate.h" 26 #include "exec/exec-all.h" 27 28 29 static void m68k_cpu_set_pc(CPUState *cs, vaddr value) 30 { 31 M68kCPU *cpu = M68K_CPU(cs); 32 33 cpu->env.pc = value; 34 } 35 36 static bool m68k_cpu_has_work(CPUState *cs) 37 { 38 return cs->interrupt_request & CPU_INTERRUPT_HARD; 39 } 40 41 static void m68k_set_feature(CPUM68KState *env, int feature) 42 { 43 env->features |= (1u << feature); 44 } 45 46 /* CPUClass::reset() */ 47 static void m68k_cpu_reset(CPUState *s) 48 { 49 M68kCPU *cpu = M68K_CPU(s); 50 M68kCPUClass *mcc = M68K_CPU_GET_CLASS(cpu); 51 CPUM68KState *env = &cpu->env; 52 floatx80 nan = floatx80_default_nan(NULL); 53 int i; 54 55 mcc->parent_reset(s); 56 57 memset(env, 0, offsetof(CPUM68KState, end_reset_fields)); 58 #if !defined(CONFIG_USER_ONLY) 59 env->sr = 0x2700; 60 #endif 61 m68k_switch_sp(env); 62 for (i = 0; i < 8; i++) { 63 env->fregs[i].d = nan; 64 } 65 cpu_m68k_set_fpcr(env, 0); 66 env->fpsr = 0; 67 68 cpu_m68k_set_ccr(env, 0); 69 /* TODO: We should set PC from the interrupt vector. */ 70 env->pc = 0; 71 } 72 73 static void m68k_cpu_disas_set_info(CPUState *s, disassemble_info *info) 74 { 75 M68kCPU *cpu = M68K_CPU(s); 76 CPUM68KState *env = &cpu->env; 77 info->print_insn = print_insn_m68k; 78 if (m68k_feature(env, M68K_FEATURE_M68000)) { 79 info->mach = bfd_mach_m68040; 80 } 81 } 82 83 /* CPU models */ 84 85 static ObjectClass *m68k_cpu_class_by_name(const char *cpu_model) 86 { 87 ObjectClass *oc; 88 char *typename; 89 90 typename = g_strdup_printf("%s-" TYPE_M68K_CPU, cpu_model); 91 oc = object_class_by_name(typename); 92 g_free(typename); 93 if (oc != NULL && (object_class_dynamic_cast(oc, TYPE_M68K_CPU) == NULL || 94 object_class_is_abstract(oc))) { 95 return NULL; 96 } 97 return oc; 98 } 99 100 static void m5206_cpu_initfn(Object *obj) 101 { 102 M68kCPU *cpu = M68K_CPU(obj); 103 CPUM68KState *env = &cpu->env; 104 105 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A); 106 } 107 108 static void m68000_cpu_initfn(Object *obj) 109 { 110 M68kCPU *cpu = M68K_CPU(obj); 111 CPUM68KState *env = &cpu->env; 112 113 m68k_set_feature(env, M68K_FEATURE_M68000); 114 m68k_set_feature(env, M68K_FEATURE_USP); 115 m68k_set_feature(env, M68K_FEATURE_WORD_INDEX); 116 } 117 118 static void m68020_cpu_initfn(Object *obj) 119 { 120 M68kCPU *cpu = M68K_CPU(obj); 121 CPUM68KState *env = &cpu->env; 122 123 m68k_set_feature(env, M68K_FEATURE_M68000); 124 m68k_set_feature(env, M68K_FEATURE_USP); 125 m68k_set_feature(env, M68K_FEATURE_WORD_INDEX); 126 m68k_set_feature(env, M68K_FEATURE_QUAD_MULDIV); 127 m68k_set_feature(env, M68K_FEATURE_BRAL); 128 m68k_set_feature(env, M68K_FEATURE_BCCL); 129 m68k_set_feature(env, M68K_FEATURE_BITFIELD); 130 m68k_set_feature(env, M68K_FEATURE_EXT_FULL); 131 m68k_set_feature(env, M68K_FEATURE_SCALED_INDEX); 132 m68k_set_feature(env, M68K_FEATURE_LONG_MULDIV); 133 m68k_set_feature(env, M68K_FEATURE_FPU); 134 m68k_set_feature(env, M68K_FEATURE_CAS); 135 m68k_set_feature(env, M68K_FEATURE_BKPT); 136 m68k_set_feature(env, M68K_FEATURE_RTD); 137 } 138 #define m68030_cpu_initfn m68020_cpu_initfn 139 #define m68040_cpu_initfn m68020_cpu_initfn 140 141 static void m68060_cpu_initfn(Object *obj) 142 { 143 M68kCPU *cpu = M68K_CPU(obj); 144 CPUM68KState *env = &cpu->env; 145 146 m68k_set_feature(env, M68K_FEATURE_M68000); 147 m68k_set_feature(env, M68K_FEATURE_USP); 148 m68k_set_feature(env, M68K_FEATURE_WORD_INDEX); 149 m68k_set_feature(env, M68K_FEATURE_BRAL); 150 m68k_set_feature(env, M68K_FEATURE_BCCL); 151 m68k_set_feature(env, M68K_FEATURE_BITFIELD); 152 m68k_set_feature(env, M68K_FEATURE_EXT_FULL); 153 m68k_set_feature(env, M68K_FEATURE_SCALED_INDEX); 154 m68k_set_feature(env, M68K_FEATURE_LONG_MULDIV); 155 m68k_set_feature(env, M68K_FEATURE_FPU); 156 m68k_set_feature(env, M68K_FEATURE_CAS); 157 m68k_set_feature(env, M68K_FEATURE_BKPT); 158 m68k_set_feature(env, M68K_FEATURE_RTD); 159 } 160 161 static void m5208_cpu_initfn(Object *obj) 162 { 163 M68kCPU *cpu = M68K_CPU(obj); 164 CPUM68KState *env = &cpu->env; 165 166 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A); 167 m68k_set_feature(env, M68K_FEATURE_CF_ISA_APLUSC); 168 m68k_set_feature(env, M68K_FEATURE_BRAL); 169 m68k_set_feature(env, M68K_FEATURE_CF_EMAC); 170 m68k_set_feature(env, M68K_FEATURE_USP); 171 } 172 173 static void cfv4e_cpu_initfn(Object *obj) 174 { 175 M68kCPU *cpu = M68K_CPU(obj); 176 CPUM68KState *env = &cpu->env; 177 178 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A); 179 m68k_set_feature(env, M68K_FEATURE_CF_ISA_B); 180 m68k_set_feature(env, M68K_FEATURE_BRAL); 181 m68k_set_feature(env, M68K_FEATURE_CF_FPU); 182 m68k_set_feature(env, M68K_FEATURE_CF_EMAC); 183 m68k_set_feature(env, M68K_FEATURE_USP); 184 } 185 186 static void any_cpu_initfn(Object *obj) 187 { 188 M68kCPU *cpu = M68K_CPU(obj); 189 CPUM68KState *env = &cpu->env; 190 191 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A); 192 m68k_set_feature(env, M68K_FEATURE_CF_ISA_B); 193 m68k_set_feature(env, M68K_FEATURE_CF_ISA_APLUSC); 194 m68k_set_feature(env, M68K_FEATURE_BRAL); 195 m68k_set_feature(env, M68K_FEATURE_CF_FPU); 196 /* MAC and EMAC are mututally exclusive, so pick EMAC. 197 It's mostly backwards compatible. */ 198 m68k_set_feature(env, M68K_FEATURE_CF_EMAC); 199 m68k_set_feature(env, M68K_FEATURE_CF_EMAC_B); 200 m68k_set_feature(env, M68K_FEATURE_USP); 201 m68k_set_feature(env, M68K_FEATURE_EXT_FULL); 202 m68k_set_feature(env, M68K_FEATURE_WORD_INDEX); 203 } 204 205 typedef struct M68kCPUInfo { 206 const char *name; 207 void (*instance_init)(Object *obj); 208 } M68kCPUInfo; 209 210 static const M68kCPUInfo m68k_cpus[] = { 211 { .name = "m68000", .instance_init = m68000_cpu_initfn }, 212 { .name = "m68020", .instance_init = m68020_cpu_initfn }, 213 { .name = "m68030", .instance_init = m68030_cpu_initfn }, 214 { .name = "m68040", .instance_init = m68040_cpu_initfn }, 215 { .name = "m68060", .instance_init = m68060_cpu_initfn }, 216 { .name = "m5206", .instance_init = m5206_cpu_initfn }, 217 { .name = "m5208", .instance_init = m5208_cpu_initfn }, 218 { .name = "cfv4e", .instance_init = cfv4e_cpu_initfn }, 219 { .name = "any", .instance_init = any_cpu_initfn }, 220 }; 221 222 static void m68k_cpu_realizefn(DeviceState *dev, Error **errp) 223 { 224 CPUState *cs = CPU(dev); 225 M68kCPU *cpu = M68K_CPU(dev); 226 M68kCPUClass *mcc = M68K_CPU_GET_CLASS(dev); 227 Error *local_err = NULL; 228 229 register_m68k_insns(&cpu->env); 230 231 cpu_exec_realizefn(cs, &local_err); 232 if (local_err != NULL) { 233 error_propagate(errp, local_err); 234 return; 235 } 236 237 m68k_cpu_init_gdb(cpu); 238 239 cpu_reset(cs); 240 qemu_init_vcpu(cs); 241 242 mcc->parent_realize(dev, errp); 243 } 244 245 static void m68k_cpu_initfn(Object *obj) 246 { 247 CPUState *cs = CPU(obj); 248 M68kCPU *cpu = M68K_CPU(obj); 249 CPUM68KState *env = &cpu->env; 250 static bool inited; 251 252 cs->env_ptr = env; 253 254 if (tcg_enabled() && !inited) { 255 inited = true; 256 m68k_tcg_init(); 257 } 258 } 259 260 static const VMStateDescription vmstate_m68k_cpu = { 261 .name = "cpu", 262 .unmigratable = 1, 263 }; 264 265 static void m68k_cpu_class_init(ObjectClass *c, void *data) 266 { 267 M68kCPUClass *mcc = M68K_CPU_CLASS(c); 268 CPUClass *cc = CPU_CLASS(c); 269 DeviceClass *dc = DEVICE_CLASS(c); 270 271 mcc->parent_realize = dc->realize; 272 dc->realize = m68k_cpu_realizefn; 273 274 mcc->parent_reset = cc->reset; 275 cc->reset = m68k_cpu_reset; 276 277 cc->class_by_name = m68k_cpu_class_by_name; 278 cc->has_work = m68k_cpu_has_work; 279 cc->do_interrupt = m68k_cpu_do_interrupt; 280 cc->cpu_exec_interrupt = m68k_cpu_exec_interrupt; 281 cc->dump_state = m68k_cpu_dump_state; 282 cc->set_pc = m68k_cpu_set_pc; 283 cc->gdb_read_register = m68k_cpu_gdb_read_register; 284 cc->gdb_write_register = m68k_cpu_gdb_write_register; 285 #ifdef CONFIG_USER_ONLY 286 cc->handle_mmu_fault = m68k_cpu_handle_mmu_fault; 287 #else 288 cc->get_phys_page_debug = m68k_cpu_get_phys_page_debug; 289 #endif 290 cc->disas_set_info = m68k_cpu_disas_set_info; 291 292 cc->gdb_num_core_regs = 18; 293 cc->gdb_core_xml_file = "cf-core.xml"; 294 295 dc->vmsd = &vmstate_m68k_cpu; 296 } 297 298 static void register_cpu_type(const M68kCPUInfo *info) 299 { 300 TypeInfo type_info = { 301 .parent = TYPE_M68K_CPU, 302 .instance_init = info->instance_init, 303 }; 304 305 type_info.name = g_strdup_printf("%s-" TYPE_M68K_CPU, info->name); 306 type_register(&type_info); 307 g_free((void *)type_info.name); 308 } 309 310 static const TypeInfo m68k_cpu_type_info = { 311 .name = TYPE_M68K_CPU, 312 .parent = TYPE_CPU, 313 .instance_size = sizeof(M68kCPU), 314 .instance_init = m68k_cpu_initfn, 315 .abstract = true, 316 .class_size = sizeof(M68kCPUClass), 317 .class_init = m68k_cpu_class_init, 318 }; 319 320 static void m68k_cpu_register_types(void) 321 { 322 int i; 323 324 type_register_static(&m68k_cpu_type_info); 325 for (i = 0; i < ARRAY_SIZE(m68k_cpus); i++) { 326 register_cpu_type(&m68k_cpus[i]); 327 } 328 } 329 330 type_init(m68k_cpu_register_types) 331