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 53 mcc->parent_reset(s); 54 55 memset(env, 0, offsetof(CPUM68KState, end_reset_fields)); 56 #if !defined(CONFIG_USER_ONLY) 57 env->sr = 0x2700; 58 #endif 59 m68k_switch_sp(env); 60 /* ??? FP regs should be initialized to NaN. */ 61 cpu_m68k_set_ccr(env, 0); 62 /* TODO: We should set PC from the interrupt vector. */ 63 env->pc = 0; 64 } 65 66 static void m68k_cpu_disas_set_info(CPUState *s, disassemble_info *info) 67 { 68 M68kCPU *cpu = M68K_CPU(s); 69 CPUM68KState *env = &cpu->env; 70 info->print_insn = print_insn_m68k; 71 if (m68k_feature(env, M68K_FEATURE_M68000)) { 72 info->mach = bfd_mach_m68040; 73 } 74 } 75 76 /* CPU models */ 77 78 static ObjectClass *m68k_cpu_class_by_name(const char *cpu_model) 79 { 80 ObjectClass *oc; 81 char *typename; 82 83 if (cpu_model == NULL) { 84 return NULL; 85 } 86 87 typename = g_strdup_printf("%s-" TYPE_M68K_CPU, cpu_model); 88 oc = object_class_by_name(typename); 89 g_free(typename); 90 if (oc != NULL && (object_class_dynamic_cast(oc, TYPE_M68K_CPU) == NULL || 91 object_class_is_abstract(oc))) { 92 return NULL; 93 } 94 return oc; 95 } 96 97 static void m5206_cpu_initfn(Object *obj) 98 { 99 M68kCPU *cpu = M68K_CPU(obj); 100 CPUM68KState *env = &cpu->env; 101 102 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A); 103 } 104 105 static void m68000_cpu_initfn(Object *obj) 106 { 107 M68kCPU *cpu = M68K_CPU(obj); 108 CPUM68KState *env = &cpu->env; 109 110 m68k_set_feature(env, M68K_FEATURE_M68000); 111 m68k_set_feature(env, M68K_FEATURE_USP); 112 m68k_set_feature(env, M68K_FEATURE_WORD_INDEX); 113 } 114 115 static void m68020_cpu_initfn(Object *obj) 116 { 117 M68kCPU *cpu = M68K_CPU(obj); 118 CPUM68KState *env = &cpu->env; 119 120 m68k_set_feature(env, M68K_FEATURE_M68000); 121 m68k_set_feature(env, M68K_FEATURE_USP); 122 m68k_set_feature(env, M68K_FEATURE_WORD_INDEX); 123 m68k_set_feature(env, M68K_FEATURE_QUAD_MULDIV); 124 m68k_set_feature(env, M68K_FEATURE_BRAL); 125 m68k_set_feature(env, M68K_FEATURE_BCCL); 126 m68k_set_feature(env, M68K_FEATURE_BITFIELD); 127 m68k_set_feature(env, M68K_FEATURE_EXT_FULL); 128 m68k_set_feature(env, M68K_FEATURE_SCALED_INDEX); 129 m68k_set_feature(env, M68K_FEATURE_LONG_MULDIV); 130 m68k_set_feature(env, M68K_FEATURE_FPU); 131 m68k_set_feature(env, M68K_FEATURE_CAS); 132 m68k_set_feature(env, M68K_FEATURE_BKPT); 133 } 134 #define m68030_cpu_initfn m68020_cpu_initfn 135 #define m68040_cpu_initfn m68020_cpu_initfn 136 137 static void m68060_cpu_initfn(Object *obj) 138 { 139 M68kCPU *cpu = M68K_CPU(obj); 140 CPUM68KState *env = &cpu->env; 141 142 m68k_set_feature(env, M68K_FEATURE_M68000); 143 m68k_set_feature(env, M68K_FEATURE_USP); 144 m68k_set_feature(env, M68K_FEATURE_WORD_INDEX); 145 m68k_set_feature(env, M68K_FEATURE_BRAL); 146 m68k_set_feature(env, M68K_FEATURE_BCCL); 147 m68k_set_feature(env, M68K_FEATURE_BITFIELD); 148 m68k_set_feature(env, M68K_FEATURE_EXT_FULL); 149 m68k_set_feature(env, M68K_FEATURE_SCALED_INDEX); 150 m68k_set_feature(env, M68K_FEATURE_LONG_MULDIV); 151 m68k_set_feature(env, M68K_FEATURE_FPU); 152 m68k_set_feature(env, M68K_FEATURE_CAS); 153 m68k_set_feature(env, M68K_FEATURE_BKPT); 154 } 155 156 static void m5208_cpu_initfn(Object *obj) 157 { 158 M68kCPU *cpu = M68K_CPU(obj); 159 CPUM68KState *env = &cpu->env; 160 161 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A); 162 m68k_set_feature(env, M68K_FEATURE_CF_ISA_APLUSC); 163 m68k_set_feature(env, M68K_FEATURE_BRAL); 164 m68k_set_feature(env, M68K_FEATURE_CF_EMAC); 165 m68k_set_feature(env, M68K_FEATURE_USP); 166 } 167 168 static void cfv4e_cpu_initfn(Object *obj) 169 { 170 M68kCPU *cpu = M68K_CPU(obj); 171 CPUM68KState *env = &cpu->env; 172 173 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A); 174 m68k_set_feature(env, M68K_FEATURE_CF_ISA_B); 175 m68k_set_feature(env, M68K_FEATURE_BRAL); 176 m68k_set_feature(env, M68K_FEATURE_CF_FPU); 177 m68k_set_feature(env, M68K_FEATURE_CF_EMAC); 178 m68k_set_feature(env, M68K_FEATURE_USP); 179 } 180 181 static void any_cpu_initfn(Object *obj) 182 { 183 M68kCPU *cpu = M68K_CPU(obj); 184 CPUM68KState *env = &cpu->env; 185 186 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A); 187 m68k_set_feature(env, M68K_FEATURE_CF_ISA_B); 188 m68k_set_feature(env, M68K_FEATURE_CF_ISA_APLUSC); 189 m68k_set_feature(env, M68K_FEATURE_BRAL); 190 m68k_set_feature(env, M68K_FEATURE_CF_FPU); 191 /* MAC and EMAC are mututally exclusive, so pick EMAC. 192 It's mostly backwards compatible. */ 193 m68k_set_feature(env, M68K_FEATURE_CF_EMAC); 194 m68k_set_feature(env, M68K_FEATURE_CF_EMAC_B); 195 m68k_set_feature(env, M68K_FEATURE_USP); 196 m68k_set_feature(env, M68K_FEATURE_EXT_FULL); 197 m68k_set_feature(env, M68K_FEATURE_WORD_INDEX); 198 } 199 200 typedef struct M68kCPUInfo { 201 const char *name; 202 void (*instance_init)(Object *obj); 203 } M68kCPUInfo; 204 205 static const M68kCPUInfo m68k_cpus[] = { 206 { .name = "m68000", .instance_init = m68000_cpu_initfn }, 207 { .name = "m68020", .instance_init = m68020_cpu_initfn }, 208 { .name = "m68030", .instance_init = m68030_cpu_initfn }, 209 { .name = "m68040", .instance_init = m68040_cpu_initfn }, 210 { .name = "m68060", .instance_init = m68060_cpu_initfn }, 211 { .name = "m5206", .instance_init = m5206_cpu_initfn }, 212 { .name = "m5208", .instance_init = m5208_cpu_initfn }, 213 { .name = "cfv4e", .instance_init = cfv4e_cpu_initfn }, 214 { .name = "any", .instance_init = any_cpu_initfn }, 215 }; 216 217 static void m68k_cpu_realizefn(DeviceState *dev, Error **errp) 218 { 219 CPUState *cs = CPU(dev); 220 M68kCPU *cpu = M68K_CPU(dev); 221 M68kCPUClass *mcc = M68K_CPU_GET_CLASS(dev); 222 Error *local_err = NULL; 223 224 cpu_exec_realizefn(cs, &local_err); 225 if (local_err != NULL) { 226 error_propagate(errp, local_err); 227 return; 228 } 229 230 m68k_cpu_init_gdb(cpu); 231 232 cpu_reset(cs); 233 qemu_init_vcpu(cs); 234 235 mcc->parent_realize(dev, errp); 236 } 237 238 static void m68k_cpu_initfn(Object *obj) 239 { 240 CPUState *cs = CPU(obj); 241 M68kCPU *cpu = M68K_CPU(obj); 242 CPUM68KState *env = &cpu->env; 243 static bool inited; 244 245 cs->env_ptr = env; 246 247 if (tcg_enabled() && !inited) { 248 inited = true; 249 m68k_tcg_init(); 250 } 251 } 252 253 static const VMStateDescription vmstate_m68k_cpu = { 254 .name = "cpu", 255 .unmigratable = 1, 256 }; 257 258 static void m68k_cpu_class_init(ObjectClass *c, void *data) 259 { 260 M68kCPUClass *mcc = M68K_CPU_CLASS(c); 261 CPUClass *cc = CPU_CLASS(c); 262 DeviceClass *dc = DEVICE_CLASS(c); 263 264 mcc->parent_realize = dc->realize; 265 dc->realize = m68k_cpu_realizefn; 266 267 mcc->parent_reset = cc->reset; 268 cc->reset = m68k_cpu_reset; 269 270 cc->class_by_name = m68k_cpu_class_by_name; 271 cc->has_work = m68k_cpu_has_work; 272 cc->do_interrupt = m68k_cpu_do_interrupt; 273 cc->cpu_exec_interrupt = m68k_cpu_exec_interrupt; 274 cc->dump_state = m68k_cpu_dump_state; 275 cc->set_pc = m68k_cpu_set_pc; 276 cc->gdb_read_register = m68k_cpu_gdb_read_register; 277 cc->gdb_write_register = m68k_cpu_gdb_write_register; 278 #ifdef CONFIG_USER_ONLY 279 cc->handle_mmu_fault = m68k_cpu_handle_mmu_fault; 280 #else 281 cc->get_phys_page_debug = m68k_cpu_get_phys_page_debug; 282 #endif 283 cc->disas_set_info = m68k_cpu_disas_set_info; 284 285 cc->gdb_num_core_regs = 18; 286 cc->gdb_core_xml_file = "cf-core.xml"; 287 288 dc->vmsd = &vmstate_m68k_cpu; 289 } 290 291 static void register_cpu_type(const M68kCPUInfo *info) 292 { 293 TypeInfo type_info = { 294 .parent = TYPE_M68K_CPU, 295 .instance_init = info->instance_init, 296 }; 297 298 type_info.name = g_strdup_printf("%s-" TYPE_M68K_CPU, info->name); 299 type_register(&type_info); 300 g_free((void *)type_info.name); 301 } 302 303 static const TypeInfo m68k_cpu_type_info = { 304 .name = TYPE_M68K_CPU, 305 .parent = TYPE_CPU, 306 .instance_size = sizeof(M68kCPU), 307 .instance_init = m68k_cpu_initfn, 308 .abstract = true, 309 .class_size = sizeof(M68kCPUClass), 310 .class_init = m68k_cpu_class_init, 311 }; 312 313 static void m68k_cpu_register_types(void) 314 { 315 int i; 316 317 type_register_static(&m68k_cpu_type_info); 318 for (i = 0; i < ARRAY_SIZE(m68k_cpus); i++) { 319 register_cpu_type(&m68k_cpus[i]); 320 } 321 } 322 323 type_init(m68k_cpu_register_types) 324