xref: /openbmc/qemu/target/m68k/cpu.c (revision 135b03cb)
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 "migration/vmstate.h"
25 #include "fpu/softfloat.h"
26 
27 static void m68k_cpu_set_pc(CPUState *cs, vaddr value)
28 {
29     M68kCPU *cpu = M68K_CPU(cs);
30 
31     cpu->env.pc = value;
32 }
33 
34 static bool m68k_cpu_has_work(CPUState *cs)
35 {
36     return cs->interrupt_request & CPU_INTERRUPT_HARD;
37 }
38 
39 static void m68k_set_feature(CPUM68KState *env, int feature)
40 {
41     env->features |= (1u << feature);
42 }
43 
44 /* CPUClass::reset() */
45 static void m68k_cpu_reset(CPUState *s)
46 {
47     M68kCPU *cpu = M68K_CPU(s);
48     M68kCPUClass *mcc = M68K_CPU_GET_CLASS(cpu);
49     CPUM68KState *env = &cpu->env;
50     floatx80 nan = floatx80_default_nan(NULL);
51     int i;
52 
53     mcc->parent_reset(s);
54 
55     memset(env, 0, offsetof(CPUM68KState, end_reset_fields));
56 #ifdef CONFIG_SOFTMMU
57     cpu_m68k_set_sr(env, SR_S | SR_I);
58 #else
59     cpu_m68k_set_sr(env, 0);
60 #endif
61     for (i = 0; i < 8; i++) {
62         env->fregs[i].d = nan;
63     }
64     cpu_m68k_set_fpcr(env, 0);
65     env->fpsr = 0;
66 
67     /* TODO: We should set PC from the interrupt vector.  */
68     env->pc = 0;
69 }
70 
71 static void m68k_cpu_disas_set_info(CPUState *s, disassemble_info *info)
72 {
73     M68kCPU *cpu = M68K_CPU(s);
74     CPUM68KState *env = &cpu->env;
75     info->print_insn = print_insn_m68k;
76     if (m68k_feature(env, M68K_FEATURE_M68000)) {
77         info->mach = bfd_mach_m68040;
78     }
79 }
80 
81 /* CPU models */
82 
83 static ObjectClass *m68k_cpu_class_by_name(const char *cpu_model)
84 {
85     ObjectClass *oc;
86     char *typename;
87 
88     typename = g_strdup_printf(M68K_CPU_TYPE_NAME("%s"), cpu_model);
89     oc = object_class_by_name(typename);
90     g_free(typename);
91     if (oc != NULL && (object_class_dynamic_cast(oc, TYPE_M68K_CPU) == NULL ||
92                        object_class_is_abstract(oc))) {
93         return NULL;
94     }
95     return oc;
96 }
97 
98 static void m5206_cpu_initfn(Object *obj)
99 {
100     M68kCPU *cpu = M68K_CPU(obj);
101     CPUM68KState *env = &cpu->env;
102 
103     m68k_set_feature(env, M68K_FEATURE_CF_ISA_A);
104 }
105 
106 static void m68000_cpu_initfn(Object *obj)
107 {
108     M68kCPU *cpu = M68K_CPU(obj);
109     CPUM68KState *env = &cpu->env;
110 
111     m68k_set_feature(env, M68K_FEATURE_M68000);
112     m68k_set_feature(env, M68K_FEATURE_USP);
113     m68k_set_feature(env, M68K_FEATURE_WORD_INDEX);
114     m68k_set_feature(env, M68K_FEATURE_MOVEP);
115 }
116 
117 static void m68020_cpu_initfn(Object *obj)
118 {
119     M68kCPU *cpu = M68K_CPU(obj);
120     CPUM68KState *env = &cpu->env;
121 
122     m68k_set_feature(env, M68K_FEATURE_M68000);
123     m68k_set_feature(env, M68K_FEATURE_USP);
124     m68k_set_feature(env, M68K_FEATURE_WORD_INDEX);
125     m68k_set_feature(env, M68K_FEATURE_QUAD_MULDIV);
126     m68k_set_feature(env, M68K_FEATURE_BRAL);
127     m68k_set_feature(env, M68K_FEATURE_BCCL);
128     m68k_set_feature(env, M68K_FEATURE_BITFIELD);
129     m68k_set_feature(env, M68K_FEATURE_EXT_FULL);
130     m68k_set_feature(env, M68K_FEATURE_SCALED_INDEX);
131     m68k_set_feature(env, M68K_FEATURE_LONG_MULDIV);
132     m68k_set_feature(env, M68K_FEATURE_FPU);
133     m68k_set_feature(env, M68K_FEATURE_CAS);
134     m68k_set_feature(env, M68K_FEATURE_BKPT);
135     m68k_set_feature(env, M68K_FEATURE_RTD);
136     m68k_set_feature(env, M68K_FEATURE_CHK2);
137     m68k_set_feature(env, M68K_FEATURE_MOVEP);
138 }
139 #define m68030_cpu_initfn m68020_cpu_initfn
140 
141 static void m68040_cpu_initfn(Object *obj)
142 {
143     M68kCPU *cpu = M68K_CPU(obj);
144     CPUM68KState *env = &cpu->env;
145 
146     m68020_cpu_initfn(obj);
147     m68k_set_feature(env, M68K_FEATURE_M68040);
148 }
149 
150 static void m68060_cpu_initfn(Object *obj)
151 {
152     M68kCPU *cpu = M68K_CPU(obj);
153     CPUM68KState *env = &cpu->env;
154 
155     m68k_set_feature(env, M68K_FEATURE_M68000);
156     m68k_set_feature(env, M68K_FEATURE_USP);
157     m68k_set_feature(env, M68K_FEATURE_WORD_INDEX);
158     m68k_set_feature(env, M68K_FEATURE_BRAL);
159     m68k_set_feature(env, M68K_FEATURE_BCCL);
160     m68k_set_feature(env, M68K_FEATURE_BITFIELD);
161     m68k_set_feature(env, M68K_FEATURE_EXT_FULL);
162     m68k_set_feature(env, M68K_FEATURE_SCALED_INDEX);
163     m68k_set_feature(env, M68K_FEATURE_LONG_MULDIV);
164     m68k_set_feature(env, M68K_FEATURE_FPU);
165     m68k_set_feature(env, M68K_FEATURE_CAS);
166     m68k_set_feature(env, M68K_FEATURE_BKPT);
167     m68k_set_feature(env, M68K_FEATURE_RTD);
168     m68k_set_feature(env, M68K_FEATURE_CHK2);
169 }
170 
171 static void m5208_cpu_initfn(Object *obj)
172 {
173     M68kCPU *cpu = M68K_CPU(obj);
174     CPUM68KState *env = &cpu->env;
175 
176     m68k_set_feature(env, M68K_FEATURE_CF_ISA_A);
177     m68k_set_feature(env, M68K_FEATURE_CF_ISA_APLUSC);
178     m68k_set_feature(env, M68K_FEATURE_BRAL);
179     m68k_set_feature(env, M68K_FEATURE_CF_EMAC);
180     m68k_set_feature(env, M68K_FEATURE_USP);
181 }
182 
183 static void cfv4e_cpu_initfn(Object *obj)
184 {
185     M68kCPU *cpu = M68K_CPU(obj);
186     CPUM68KState *env = &cpu->env;
187 
188     m68k_set_feature(env, M68K_FEATURE_CF_ISA_A);
189     m68k_set_feature(env, M68K_FEATURE_CF_ISA_B);
190     m68k_set_feature(env, M68K_FEATURE_BRAL);
191     m68k_set_feature(env, M68K_FEATURE_CF_FPU);
192     m68k_set_feature(env, M68K_FEATURE_CF_EMAC);
193     m68k_set_feature(env, M68K_FEATURE_USP);
194 }
195 
196 static void any_cpu_initfn(Object *obj)
197 {
198     M68kCPU *cpu = M68K_CPU(obj);
199     CPUM68KState *env = &cpu->env;
200 
201     m68k_set_feature(env, M68K_FEATURE_CF_ISA_A);
202     m68k_set_feature(env, M68K_FEATURE_CF_ISA_B);
203     m68k_set_feature(env, M68K_FEATURE_CF_ISA_APLUSC);
204     m68k_set_feature(env, M68K_FEATURE_BRAL);
205     m68k_set_feature(env, M68K_FEATURE_CF_FPU);
206     /*
207      * MAC and EMAC are mututally exclusive, so pick EMAC.
208      * It's mostly backwards compatible.
209      */
210     m68k_set_feature(env, M68K_FEATURE_CF_EMAC);
211     m68k_set_feature(env, M68K_FEATURE_CF_EMAC_B);
212     m68k_set_feature(env, M68K_FEATURE_USP);
213     m68k_set_feature(env, M68K_FEATURE_EXT_FULL);
214     m68k_set_feature(env, M68K_FEATURE_WORD_INDEX);
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     register_m68k_insns(&cpu->env);
225 
226     cpu_exec_realizefn(cs, &local_err);
227     if (local_err != NULL) {
228         error_propagate(errp, local_err);
229         return;
230     }
231 
232     m68k_cpu_init_gdb(cpu);
233 
234     cpu_reset(cs);
235     qemu_init_vcpu(cs);
236 
237     mcc->parent_realize(dev, errp);
238 }
239 
240 static void m68k_cpu_initfn(Object *obj)
241 {
242     M68kCPU *cpu = M68K_CPU(obj);
243 
244     cpu_set_cpustate_pointers(cpu);
245 }
246 
247 static const VMStateDescription vmstate_m68k_cpu = {
248     .name = "cpu",
249     .unmigratable = 1,
250 };
251 
252 static void m68k_cpu_class_init(ObjectClass *c, void *data)
253 {
254     M68kCPUClass *mcc = M68K_CPU_CLASS(c);
255     CPUClass *cc = CPU_CLASS(c);
256     DeviceClass *dc = DEVICE_CLASS(c);
257 
258     device_class_set_parent_realize(dc, m68k_cpu_realizefn,
259                                     &mcc->parent_realize);
260     mcc->parent_reset = cc->reset;
261     cc->reset = m68k_cpu_reset;
262 
263     cc->class_by_name = m68k_cpu_class_by_name;
264     cc->has_work = m68k_cpu_has_work;
265     cc->do_interrupt = m68k_cpu_do_interrupt;
266     cc->cpu_exec_interrupt = m68k_cpu_exec_interrupt;
267     cc->dump_state = m68k_cpu_dump_state;
268     cc->set_pc = m68k_cpu_set_pc;
269     cc->gdb_read_register = m68k_cpu_gdb_read_register;
270     cc->gdb_write_register = m68k_cpu_gdb_write_register;
271     cc->tlb_fill = m68k_cpu_tlb_fill;
272 #if defined(CONFIG_SOFTMMU)
273     cc->do_transaction_failed = m68k_cpu_transaction_failed;
274     cc->get_phys_page_debug = m68k_cpu_get_phys_page_debug;
275 #endif
276     cc->disas_set_info = m68k_cpu_disas_set_info;
277     cc->tcg_initialize = m68k_tcg_init;
278 
279     cc->gdb_num_core_regs = 18;
280     cc->gdb_core_xml_file = "cf-core.xml";
281 
282     dc->vmsd = &vmstate_m68k_cpu;
283 }
284 
285 #define DEFINE_M68K_CPU_TYPE(cpu_model, initfn) \
286     {                                           \
287         .name = M68K_CPU_TYPE_NAME(cpu_model),  \
288         .instance_init = initfn,                \
289         .parent = TYPE_M68K_CPU,                \
290     }
291 
292 static const TypeInfo m68k_cpus_type_infos[] = {
293     { /* base class should be registered first */
294         .name = TYPE_M68K_CPU,
295         .parent = TYPE_CPU,
296         .instance_size = sizeof(M68kCPU),
297         .instance_init = m68k_cpu_initfn,
298         .abstract = true,
299         .class_size = sizeof(M68kCPUClass),
300         .class_init = m68k_cpu_class_init,
301     },
302     DEFINE_M68K_CPU_TYPE("m68000", m68000_cpu_initfn),
303     DEFINE_M68K_CPU_TYPE("m68020", m68020_cpu_initfn),
304     DEFINE_M68K_CPU_TYPE("m68030", m68030_cpu_initfn),
305     DEFINE_M68K_CPU_TYPE("m68040", m68040_cpu_initfn),
306     DEFINE_M68K_CPU_TYPE("m68060", m68060_cpu_initfn),
307     DEFINE_M68K_CPU_TYPE("m5206", m5206_cpu_initfn),
308     DEFINE_M68K_CPU_TYPE("m5208", m5208_cpu_initfn),
309     DEFINE_M68K_CPU_TYPE("cfv4e", cfv4e_cpu_initfn),
310     DEFINE_M68K_CPU_TYPE("any", any_cpu_initfn),
311 };
312 
313 DEFINE_TYPES(m68k_cpus_type_infos)
314