Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
[qemu.git] / target-m68k / cpu.c
blob0b5f9a581e7ab966754f5151eca800b6088dd85c
1 /*
2 * QEMU Motorola 68k CPU
4 * Copyright (c) 2012 SUSE LINUX Products GmbH
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.
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.
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>
21 #include "qemu/osdep.h"
22 #include "qapi/error.h"
23 #include "cpu.h"
24 #include "qemu-common.h"
25 #include "migration/vmstate.h"
28 static void m68k_cpu_set_pc(CPUState *cs, vaddr value)
30 M68kCPU *cpu = M68K_CPU(cs);
32 cpu->env.pc = value;
35 static bool m68k_cpu_has_work(CPUState *cs)
37 return cs->interrupt_request & CPU_INTERRUPT_HARD;
40 static void m68k_set_feature(CPUM68KState *env, int feature)
42 env->features |= (1u << feature);
45 /* CPUClass::reset() */
46 static void m68k_cpu_reset(CPUState *s)
48 M68kCPU *cpu = M68K_CPU(s);
49 M68kCPUClass *mcc = M68K_CPU_GET_CLASS(cpu);
50 CPUM68KState *env = &cpu->env;
52 mcc->parent_reset(s);
54 memset(env, 0, offsetof(CPUM68KState, features));
55 #if !defined(CONFIG_USER_ONLY)
56 env->sr = 0x2700;
57 #endif
58 m68k_switch_sp(env);
59 /* ??? FP regs should be initialized to NaN. */
60 env->cc_op = CC_OP_FLAGS;
61 /* TODO: We should set PC from the interrupt vector. */
62 env->pc = 0;
63 tlb_flush(s, 1);
66 static void m68k_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
68 info->print_insn = print_insn_m68k;
71 /* CPU models */
73 static ObjectClass *m68k_cpu_class_by_name(const char *cpu_model)
75 ObjectClass *oc;
76 char *typename;
78 if (cpu_model == NULL) {
79 return NULL;
82 typename = g_strdup_printf("%s-" TYPE_M68K_CPU, cpu_model);
83 oc = object_class_by_name(typename);
84 g_free(typename);
85 if (oc != NULL && (object_class_dynamic_cast(oc, TYPE_M68K_CPU) == NULL ||
86 object_class_is_abstract(oc))) {
87 return NULL;
89 return oc;
92 static void m5206_cpu_initfn(Object *obj)
94 M68kCPU *cpu = M68K_CPU(obj);
95 CPUM68KState *env = &cpu->env;
97 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A);
100 static void m5208_cpu_initfn(Object *obj)
102 M68kCPU *cpu = M68K_CPU(obj);
103 CPUM68KState *env = &cpu->env;
105 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A);
106 m68k_set_feature(env, M68K_FEATURE_CF_ISA_APLUSC);
107 m68k_set_feature(env, M68K_FEATURE_BRAL);
108 m68k_set_feature(env, M68K_FEATURE_CF_EMAC);
109 m68k_set_feature(env, M68K_FEATURE_USP);
112 static void cfv4e_cpu_initfn(Object *obj)
114 M68kCPU *cpu = M68K_CPU(obj);
115 CPUM68KState *env = &cpu->env;
117 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A);
118 m68k_set_feature(env, M68K_FEATURE_CF_ISA_B);
119 m68k_set_feature(env, M68K_FEATURE_BRAL);
120 m68k_set_feature(env, M68K_FEATURE_CF_FPU);
121 m68k_set_feature(env, M68K_FEATURE_CF_EMAC);
122 m68k_set_feature(env, M68K_FEATURE_USP);
125 static void any_cpu_initfn(Object *obj)
127 M68kCPU *cpu = M68K_CPU(obj);
128 CPUM68KState *env = &cpu->env;
130 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A);
131 m68k_set_feature(env, M68K_FEATURE_CF_ISA_B);
132 m68k_set_feature(env, M68K_FEATURE_CF_ISA_APLUSC);
133 m68k_set_feature(env, M68K_FEATURE_BRAL);
134 m68k_set_feature(env, M68K_FEATURE_CF_FPU);
135 /* MAC and EMAC are mututally exclusive, so pick EMAC.
136 It's mostly backwards compatible. */
137 m68k_set_feature(env, M68K_FEATURE_CF_EMAC);
138 m68k_set_feature(env, M68K_FEATURE_CF_EMAC_B);
139 m68k_set_feature(env, M68K_FEATURE_USP);
140 m68k_set_feature(env, M68K_FEATURE_EXT_FULL);
141 m68k_set_feature(env, M68K_FEATURE_WORD_INDEX);
144 typedef struct M68kCPUInfo {
145 const char *name;
146 void (*instance_init)(Object *obj);
147 } M68kCPUInfo;
149 static const M68kCPUInfo m68k_cpus[] = {
150 { .name = "m5206", .instance_init = m5206_cpu_initfn },
151 { .name = "m5208", .instance_init = m5208_cpu_initfn },
152 { .name = "cfv4e", .instance_init = cfv4e_cpu_initfn },
153 { .name = "any", .instance_init = any_cpu_initfn },
156 static void m68k_cpu_realizefn(DeviceState *dev, Error **errp)
158 CPUState *cs = CPU(dev);
159 M68kCPU *cpu = M68K_CPU(dev);
160 M68kCPUClass *mcc = M68K_CPU_GET_CLASS(dev);
162 m68k_cpu_init_gdb(cpu);
164 cpu_reset(cs);
165 qemu_init_vcpu(cs);
167 mcc->parent_realize(dev, errp);
170 static void m68k_cpu_initfn(Object *obj)
172 CPUState *cs = CPU(obj);
173 M68kCPU *cpu = M68K_CPU(obj);
174 CPUM68KState *env = &cpu->env;
175 static bool inited;
177 cs->env_ptr = env;
178 cpu_exec_init(cs, &error_abort);
180 if (tcg_enabled() && !inited) {
181 inited = true;
182 m68k_tcg_init();
186 static const VMStateDescription vmstate_m68k_cpu = {
187 .name = "cpu",
188 .unmigratable = 1,
191 static void m68k_cpu_class_init(ObjectClass *c, void *data)
193 M68kCPUClass *mcc = M68K_CPU_CLASS(c);
194 CPUClass *cc = CPU_CLASS(c);
195 DeviceClass *dc = DEVICE_CLASS(c);
197 mcc->parent_realize = dc->realize;
198 dc->realize = m68k_cpu_realizefn;
200 mcc->parent_reset = cc->reset;
201 cc->reset = m68k_cpu_reset;
203 cc->class_by_name = m68k_cpu_class_by_name;
204 cc->has_work = m68k_cpu_has_work;
205 cc->do_interrupt = m68k_cpu_do_interrupt;
206 cc->cpu_exec_interrupt = m68k_cpu_exec_interrupt;
207 cc->dump_state = m68k_cpu_dump_state;
208 cc->set_pc = m68k_cpu_set_pc;
209 cc->gdb_read_register = m68k_cpu_gdb_read_register;
210 cc->gdb_write_register = m68k_cpu_gdb_write_register;
211 #ifdef CONFIG_USER_ONLY
212 cc->handle_mmu_fault = m68k_cpu_handle_mmu_fault;
213 #else
214 cc->get_phys_page_debug = m68k_cpu_get_phys_page_debug;
215 #endif
216 cc->cpu_exec_enter = m68k_cpu_exec_enter;
217 cc->cpu_exec_exit = m68k_cpu_exec_exit;
218 cc->disas_set_info = m68k_cpu_disas_set_info;
220 cc->gdb_num_core_regs = 18;
221 cc->gdb_core_xml_file = "cf-core.xml";
223 dc->vmsd = &vmstate_m68k_cpu;
226 * Reason: m68k_cpu_initfn() calls cpu_exec_init(), which saves
227 * the object in cpus -> dangling pointer after final
228 * object_unref().
230 dc->cannot_destroy_with_object_finalize_yet = true;
233 static void register_cpu_type(const M68kCPUInfo *info)
235 TypeInfo type_info = {
236 .parent = TYPE_M68K_CPU,
237 .instance_init = info->instance_init,
240 type_info.name = g_strdup_printf("%s-" TYPE_M68K_CPU, info->name);
241 type_register(&type_info);
242 g_free((void *)type_info.name);
245 static const TypeInfo m68k_cpu_type_info = {
246 .name = TYPE_M68K_CPU,
247 .parent = TYPE_CPU,
248 .instance_size = sizeof(M68kCPU),
249 .instance_init = m68k_cpu_initfn,
250 .abstract = true,
251 .class_size = sizeof(M68kCPUClass),
252 .class_init = m68k_cpu_class_init,
255 static void m68k_cpu_register_types(void)
257 int i;
259 type_register_static(&m68k_cpu_type_info);
260 for (i = 0; i < ARRAY_SIZE(m68k_cpus); i++) {
261 register_cpu_type(&m68k_cpus[i]);
265 type_init(m68k_cpu_register_types)