libqos: remove some leaks
[qemu/kevin.git] / target-m68k / cpu.c
blob6de74bf28eea297ab6c0801e660aa6a885e00670
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 "cpu.h"
23 #include "qemu-common.h"
24 #include "migration/vmstate.h"
27 static void m68k_cpu_set_pc(CPUState *cs, vaddr value)
29 M68kCPU *cpu = M68K_CPU(cs);
31 cpu->env.pc = value;
34 static bool m68k_cpu_has_work(CPUState *cs)
36 return cs->interrupt_request & CPU_INTERRUPT_HARD;
39 static void m68k_set_feature(CPUM68KState *env, int feature)
41 env->features |= (1u << feature);
44 /* CPUClass::reset() */
45 static void m68k_cpu_reset(CPUState *s)
47 M68kCPU *cpu = M68K_CPU(s);
48 M68kCPUClass *mcc = M68K_CPU_GET_CLASS(cpu);
49 CPUM68KState *env = &cpu->env;
51 mcc->parent_reset(s);
53 memset(env, 0, offsetof(CPUM68KState, features));
54 #if !defined(CONFIG_USER_ONLY)
55 env->sr = 0x2700;
56 #endif
57 m68k_switch_sp(env);
58 /* ??? FP regs should be initialized to NaN. */
59 env->cc_op = CC_OP_FLAGS;
60 /* TODO: We should set PC from the interrupt vector. */
61 env->pc = 0;
62 tlb_flush(s, 1);
65 static void m68k_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
67 info->print_insn = print_insn_m68k;
70 /* CPU models */
72 static ObjectClass *m68k_cpu_class_by_name(const char *cpu_model)
74 ObjectClass *oc;
75 char *typename;
77 if (cpu_model == NULL) {
78 return NULL;
81 typename = g_strdup_printf("%s-" TYPE_M68K_CPU, cpu_model);
82 oc = object_class_by_name(typename);
83 g_free(typename);
84 if (oc != NULL && (object_class_dynamic_cast(oc, TYPE_M68K_CPU) == NULL ||
85 object_class_is_abstract(oc))) {
86 return NULL;
88 return oc;
91 static void m5206_cpu_initfn(Object *obj)
93 M68kCPU *cpu = M68K_CPU(obj);
94 CPUM68KState *env = &cpu->env;
96 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A);
99 static void m5208_cpu_initfn(Object *obj)
101 M68kCPU *cpu = M68K_CPU(obj);
102 CPUM68KState *env = &cpu->env;
104 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A);
105 m68k_set_feature(env, M68K_FEATURE_CF_ISA_APLUSC);
106 m68k_set_feature(env, M68K_FEATURE_BRAL);
107 m68k_set_feature(env, M68K_FEATURE_CF_EMAC);
108 m68k_set_feature(env, M68K_FEATURE_USP);
111 static void cfv4e_cpu_initfn(Object *obj)
113 M68kCPU *cpu = M68K_CPU(obj);
114 CPUM68KState *env = &cpu->env;
116 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A);
117 m68k_set_feature(env, M68K_FEATURE_CF_ISA_B);
118 m68k_set_feature(env, M68K_FEATURE_BRAL);
119 m68k_set_feature(env, M68K_FEATURE_CF_FPU);
120 m68k_set_feature(env, M68K_FEATURE_CF_EMAC);
121 m68k_set_feature(env, M68K_FEATURE_USP);
124 static void any_cpu_initfn(Object *obj)
126 M68kCPU *cpu = M68K_CPU(obj);
127 CPUM68KState *env = &cpu->env;
129 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A);
130 m68k_set_feature(env, M68K_FEATURE_CF_ISA_B);
131 m68k_set_feature(env, M68K_FEATURE_CF_ISA_APLUSC);
132 m68k_set_feature(env, M68K_FEATURE_BRAL);
133 m68k_set_feature(env, M68K_FEATURE_CF_FPU);
134 /* MAC and EMAC are mututally exclusive, so pick EMAC.
135 It's mostly backwards compatible. */
136 m68k_set_feature(env, M68K_FEATURE_CF_EMAC);
137 m68k_set_feature(env, M68K_FEATURE_CF_EMAC_B);
138 m68k_set_feature(env, M68K_FEATURE_USP);
139 m68k_set_feature(env, M68K_FEATURE_EXT_FULL);
140 m68k_set_feature(env, M68K_FEATURE_WORD_INDEX);
143 typedef struct M68kCPUInfo {
144 const char *name;
145 void (*instance_init)(Object *obj);
146 } M68kCPUInfo;
148 static const M68kCPUInfo m68k_cpus[] = {
149 { .name = "m5206", .instance_init = m5206_cpu_initfn },
150 { .name = "m5208", .instance_init = m5208_cpu_initfn },
151 { .name = "cfv4e", .instance_init = cfv4e_cpu_initfn },
152 { .name = "any", .instance_init = any_cpu_initfn },
155 static void m68k_cpu_realizefn(DeviceState *dev, Error **errp)
157 CPUState *cs = CPU(dev);
158 M68kCPU *cpu = M68K_CPU(dev);
159 M68kCPUClass *mcc = M68K_CPU_GET_CLASS(dev);
161 m68k_cpu_init_gdb(cpu);
163 cpu_reset(cs);
164 qemu_init_vcpu(cs);
166 mcc->parent_realize(dev, errp);
169 static void m68k_cpu_initfn(Object *obj)
171 CPUState *cs = CPU(obj);
172 M68kCPU *cpu = M68K_CPU(obj);
173 CPUM68KState *env = &cpu->env;
174 static bool inited;
176 cs->env_ptr = env;
177 cpu_exec_init(cs, &error_abort);
179 if (tcg_enabled() && !inited) {
180 inited = true;
181 m68k_tcg_init();
185 static const VMStateDescription vmstate_m68k_cpu = {
186 .name = "cpu",
187 .unmigratable = 1,
190 static void m68k_cpu_class_init(ObjectClass *c, void *data)
192 M68kCPUClass *mcc = M68K_CPU_CLASS(c);
193 CPUClass *cc = CPU_CLASS(c);
194 DeviceClass *dc = DEVICE_CLASS(c);
196 mcc->parent_realize = dc->realize;
197 dc->realize = m68k_cpu_realizefn;
199 mcc->parent_reset = cc->reset;
200 cc->reset = m68k_cpu_reset;
202 cc->class_by_name = m68k_cpu_class_by_name;
203 cc->has_work = m68k_cpu_has_work;
204 cc->do_interrupt = m68k_cpu_do_interrupt;
205 cc->cpu_exec_interrupt = m68k_cpu_exec_interrupt;
206 cc->dump_state = m68k_cpu_dump_state;
207 cc->set_pc = m68k_cpu_set_pc;
208 cc->gdb_read_register = m68k_cpu_gdb_read_register;
209 cc->gdb_write_register = m68k_cpu_gdb_write_register;
210 #ifdef CONFIG_USER_ONLY
211 cc->handle_mmu_fault = m68k_cpu_handle_mmu_fault;
212 #else
213 cc->get_phys_page_debug = m68k_cpu_get_phys_page_debug;
214 #endif
215 cc->cpu_exec_enter = m68k_cpu_exec_enter;
216 cc->cpu_exec_exit = m68k_cpu_exec_exit;
217 cc->disas_set_info = m68k_cpu_disas_set_info;
219 cc->gdb_num_core_regs = 18;
220 cc->gdb_core_xml_file = "cf-core.xml";
222 dc->vmsd = &vmstate_m68k_cpu;
225 * Reason: m68k_cpu_initfn() calls cpu_exec_init(), which saves
226 * the object in cpus -> dangling pointer after final
227 * object_unref().
229 dc->cannot_destroy_with_object_finalize_yet = true;
232 static void register_cpu_type(const M68kCPUInfo *info)
234 TypeInfo type_info = {
235 .parent = TYPE_M68K_CPU,
236 .instance_init = info->instance_init,
239 type_info.name = g_strdup_printf("%s-" TYPE_M68K_CPU, info->name);
240 type_register(&type_info);
241 g_free((void *)type_info.name);
244 static const TypeInfo m68k_cpu_type_info = {
245 .name = TYPE_M68K_CPU,
246 .parent = TYPE_CPU,
247 .instance_size = sizeof(M68kCPU),
248 .instance_init = m68k_cpu_initfn,
249 .abstract = true,
250 .class_size = sizeof(M68kCPUClass),
251 .class_init = m68k_cpu_class_init,
254 static void m68k_cpu_register_types(void)
256 int i;
258 type_register_static(&m68k_cpu_type_info);
259 for (i = 0; i < ARRAY_SIZE(m68k_cpus); i++) {
260 register_cpu_type(&m68k_cpus[i]);
264 type_init(m68k_cpu_register_types)