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