1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
29static void m68k_cpu_set_pc(CPUState *cs, vaddr value)
30{
31 M68kCPU *cpu = M68K_CPU(cs);
32
33 cpu->env.pc = value;
34}
35
36static bool m68k_cpu_has_work(CPUState *cs)
37{
38 return cs->interrupt_request & CPU_INTERRUPT_HARD;
39}
40
41static void m68k_set_feature(CPUM68KState *env, int feature)
42{
43 env->features |= (1u << feature);
44}
45
46
47static 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, features));
56#if !defined(CONFIG_USER_ONLY)
57 env->sr = 0x2700;
58#endif
59 m68k_switch_sp(env);
60
61 env->cc_op = CC_OP_FLAGS;
62
63 env->pc = 0;
64 tlb_flush(s, 1);
65}
66
67static void m68k_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
68{
69 info->print_insn = print_insn_m68k;
70}
71
72
73
74static ObjectClass *m68k_cpu_class_by_name(const char *cpu_model)
75{
76 ObjectClass *oc;
77 char *typename;
78
79 if (cpu_model == NULL) {
80 return NULL;
81 }
82
83 typename = g_strdup_printf("%s-" TYPE_M68K_CPU, cpu_model);
84 oc = object_class_by_name(typename);
85 g_free(typename);
86 if (oc != NULL && (object_class_dynamic_cast(oc, TYPE_M68K_CPU) == NULL ||
87 object_class_is_abstract(oc))) {
88 return NULL;
89 }
90 return oc;
91}
92
93static void m5206_cpu_initfn(Object *obj)
94{
95 M68kCPU *cpu = M68K_CPU(obj);
96 CPUM68KState *env = &cpu->env;
97
98 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A);
99}
100
101static void m5208_cpu_initfn(Object *obj)
102{
103 M68kCPU *cpu = M68K_CPU(obj);
104 CPUM68KState *env = &cpu->env;
105
106 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A);
107 m68k_set_feature(env, M68K_FEATURE_CF_ISA_APLUSC);
108 m68k_set_feature(env, M68K_FEATURE_BRAL);
109 m68k_set_feature(env, M68K_FEATURE_CF_EMAC);
110 m68k_set_feature(env, M68K_FEATURE_USP);
111}
112
113static void cfv4e_cpu_initfn(Object *obj)
114{
115 M68kCPU *cpu = M68K_CPU(obj);
116 CPUM68KState *env = &cpu->env;
117
118 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A);
119 m68k_set_feature(env, M68K_FEATURE_CF_ISA_B);
120 m68k_set_feature(env, M68K_FEATURE_BRAL);
121 m68k_set_feature(env, M68K_FEATURE_CF_FPU);
122 m68k_set_feature(env, M68K_FEATURE_CF_EMAC);
123 m68k_set_feature(env, M68K_FEATURE_USP);
124}
125
126static void any_cpu_initfn(Object *obj)
127{
128 M68kCPU *cpu = M68K_CPU(obj);
129 CPUM68KState *env = &cpu->env;
130
131 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A);
132 m68k_set_feature(env, M68K_FEATURE_CF_ISA_B);
133 m68k_set_feature(env, M68K_FEATURE_CF_ISA_APLUSC);
134 m68k_set_feature(env, M68K_FEATURE_BRAL);
135 m68k_set_feature(env, M68K_FEATURE_CF_FPU);
136
137
138 m68k_set_feature(env, M68K_FEATURE_CF_EMAC);
139 m68k_set_feature(env, M68K_FEATURE_CF_EMAC_B);
140 m68k_set_feature(env, M68K_FEATURE_USP);
141 m68k_set_feature(env, M68K_FEATURE_EXT_FULL);
142 m68k_set_feature(env, M68K_FEATURE_WORD_INDEX);
143}
144
145typedef struct M68kCPUInfo {
146 const char *name;
147 void (*instance_init)(Object *obj);
148} M68kCPUInfo;
149
150static const M68kCPUInfo m68k_cpus[] = {
151 { .name = "m5206", .instance_init = m5206_cpu_initfn },
152 { .name = "m5208", .instance_init = m5208_cpu_initfn },
153 { .name = "cfv4e", .instance_init = cfv4e_cpu_initfn },
154 { .name = "any", .instance_init = any_cpu_initfn },
155};
156
157static void m68k_cpu_realizefn(DeviceState *dev, Error **errp)
158{
159 CPUState *cs = CPU(dev);
160 M68kCPU *cpu = M68K_CPU(dev);
161 M68kCPUClass *mcc = M68K_CPU_GET_CLASS(dev);
162
163 m68k_cpu_init_gdb(cpu);
164
165 cpu_reset(cs);
166 qemu_init_vcpu(cs);
167
168 mcc->parent_realize(dev, errp);
169}
170
171static void m68k_cpu_initfn(Object *obj)
172{
173 CPUState *cs = CPU(obj);
174 M68kCPU *cpu = M68K_CPU(obj);
175 CPUM68KState *env = &cpu->env;
176 static bool inited;
177
178 cs->env_ptr = env;
179 cpu_exec_init(cs, &error_abort);
180
181 if (tcg_enabled() && !inited) {
182 inited = true;
183 m68k_tcg_init();
184 }
185}
186
187static const VMStateDescription vmstate_m68k_cpu = {
188 .name = "cpu",
189 .unmigratable = 1,
190};
191
192static void m68k_cpu_class_init(ObjectClass *c, void *data)
193{
194 M68kCPUClass *mcc = M68K_CPU_CLASS(c);
195 CPUClass *cc = CPU_CLASS(c);
196 DeviceClass *dc = DEVICE_CLASS(c);
197
198 mcc->parent_realize = dc->realize;
199 dc->realize = m68k_cpu_realizefn;
200
201 mcc->parent_reset = cc->reset;
202 cc->reset = m68k_cpu_reset;
203
204 cc->class_by_name = m68k_cpu_class_by_name;
205 cc->has_work = m68k_cpu_has_work;
206 cc->do_interrupt = m68k_cpu_do_interrupt;
207 cc->cpu_exec_interrupt = m68k_cpu_exec_interrupt;
208 cc->dump_state = m68k_cpu_dump_state;
209 cc->set_pc = m68k_cpu_set_pc;
210 cc->gdb_read_register = m68k_cpu_gdb_read_register;
211 cc->gdb_write_register = m68k_cpu_gdb_write_register;
212#ifdef CONFIG_USER_ONLY
213 cc->handle_mmu_fault = m68k_cpu_handle_mmu_fault;
214#else
215 cc->get_phys_page_debug = m68k_cpu_get_phys_page_debug;
216#endif
217 cc->cpu_exec_enter = m68k_cpu_exec_enter;
218 cc->cpu_exec_exit = m68k_cpu_exec_exit;
219 cc->disas_set_info = m68k_cpu_disas_set_info;
220
221 cc->gdb_num_core_regs = 18;
222 cc->gdb_core_xml_file = "cf-core.xml";
223
224 dc->vmsd = &vmstate_m68k_cpu;
225
226
227
228
229
230
231 dc->cannot_destroy_with_object_finalize_yet = true;
232}
233
234static void register_cpu_type(const M68kCPUInfo *info)
235{
236 TypeInfo type_info = {
237 .parent = TYPE_M68K_CPU,
238 .instance_init = info->instance_init,
239 };
240
241 type_info.name = g_strdup_printf("%s-" TYPE_M68K_CPU, info->name);
242 type_register(&type_info);
243 g_free((void *)type_info.name);
244}
245
246static const TypeInfo m68k_cpu_type_info = {
247 .name = TYPE_M68K_CPU,
248 .parent = TYPE_CPU,
249 .instance_size = sizeof(M68kCPU),
250 .instance_init = m68k_cpu_initfn,
251 .abstract = true,
252 .class_size = sizeof(M68kCPUClass),
253 .class_init = m68k_cpu_class_init,
254};
255
256static void m68k_cpu_register_types(void)
257{
258 int i;
259
260 type_register_static(&m68k_cpu_type_info);
261 for (i = 0; i < ARRAY_SIZE(m68k_cpus); i++) {
262 register_cpu_type(&m68k_cpus[i]);
263 }
264}
265
266type_init(m68k_cpu_register_types)
267