balloon: fix segfault and harden the stats queue
[qemu/kevin.git] / target-sh4 / cpu.c
blob8621d70d4dab5088e4f1c0be33e88cd0d48f3be3
1 /*
2 * QEMU SuperH CPU
4 * Copyright (c) 2005 Samuel Tardieu
5 * Copyright (c) 2012 SUSE LINUX Products GmbH
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see
19 * <http://www.gnu.org/licenses/lgpl-2.1.html>
22 #include "qemu/osdep.h"
23 #include "cpu.h"
24 #include "qemu-common.h"
25 #include "migration/vmstate.h"
28 static void superh_cpu_set_pc(CPUState *cs, vaddr value)
30 SuperHCPU *cpu = SUPERH_CPU(cs);
32 cpu->env.pc = value;
35 static void superh_cpu_synchronize_from_tb(CPUState *cs, TranslationBlock *tb)
37 SuperHCPU *cpu = SUPERH_CPU(cs);
39 cpu->env.pc = tb->pc;
40 cpu->env.flags = tb->flags;
43 static bool superh_cpu_has_work(CPUState *cs)
45 return cs->interrupt_request & CPU_INTERRUPT_HARD;
48 /* CPUClass::reset() */
49 static void superh_cpu_reset(CPUState *s)
51 SuperHCPU *cpu = SUPERH_CPU(s);
52 SuperHCPUClass *scc = SUPERH_CPU_GET_CLASS(cpu);
53 CPUSH4State *env = &cpu->env;
55 scc->parent_reset(s);
57 memset(env, 0, offsetof(CPUSH4State, id));
58 tlb_flush(s, 1);
60 env->pc = 0xA0000000;
61 #if defined(CONFIG_USER_ONLY)
62 env->fpscr = FPSCR_PR; /* value for userspace according to the kernel */
63 set_float_rounding_mode(float_round_nearest_even, &env->fp_status); /* ?! */
64 #else
65 env->sr = (1u << SR_MD) | (1u << SR_RB) | (1u << SR_BL) |
66 (1u << SR_I3) | (1u << SR_I2) | (1u << SR_I1) | (1u << SR_I0);
67 env->fpscr = FPSCR_DN | FPSCR_RM_ZERO; /* CPU reset value according to SH4 manual */
68 set_float_rounding_mode(float_round_to_zero, &env->fp_status);
69 set_flush_to_zero(1, &env->fp_status);
70 #endif
71 set_default_nan_mode(1, &env->fp_status);
74 static void superh_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
76 info->mach = bfd_mach_sh4;
77 info->print_insn = print_insn_sh;
80 typedef struct SuperHCPUListState {
81 fprintf_function cpu_fprintf;
82 FILE *file;
83 } SuperHCPUListState;
85 /* Sort alphabetically by type name. */
86 static gint superh_cpu_list_compare(gconstpointer a, gconstpointer b)
88 ObjectClass *class_a = (ObjectClass *)a;
89 ObjectClass *class_b = (ObjectClass *)b;
90 const char *name_a, *name_b;
92 name_a = object_class_get_name(class_a);
93 name_b = object_class_get_name(class_b);
94 return strcmp(name_a, name_b);
97 static void superh_cpu_list_entry(gpointer data, gpointer user_data)
99 ObjectClass *oc = data;
100 SuperHCPUClass *scc = SUPERH_CPU_CLASS(oc);
101 SuperHCPUListState *s = user_data;
103 (*s->cpu_fprintf)(s->file, "%s\n",
104 scc->name);
107 void sh4_cpu_list(FILE *f, fprintf_function cpu_fprintf)
109 SuperHCPUListState s = {
110 .cpu_fprintf = cpu_fprintf,
111 .file = f,
113 GSList *list;
115 list = object_class_get_list(TYPE_SUPERH_CPU, false);
116 list = g_slist_sort(list, superh_cpu_list_compare);
117 g_slist_foreach(list, superh_cpu_list_entry, &s);
118 g_slist_free(list);
121 static gint superh_cpu_name_compare(gconstpointer a, gconstpointer b)
123 const SuperHCPUClass *scc = SUPERH_CPU_CLASS(a);
124 const char *name = b;
126 return strcasecmp(scc->name, name);
129 static ObjectClass *superh_cpu_class_by_name(const char *cpu_model)
131 ObjectClass *oc;
132 GSList *list, *item;
134 if (cpu_model == NULL) {
135 return NULL;
137 if (strcasecmp(cpu_model, "any") == 0) {
138 return object_class_by_name(TYPE_SH7750R_CPU);
141 oc = object_class_by_name(cpu_model);
142 if (oc != NULL && object_class_dynamic_cast(oc, TYPE_SUPERH_CPU) != NULL
143 && !object_class_is_abstract(oc)) {
144 return oc;
147 oc = NULL;
148 list = object_class_get_list(TYPE_SUPERH_CPU, false);
149 item = g_slist_find_custom(list, cpu_model, superh_cpu_name_compare);
150 if (item != NULL) {
151 oc = item->data;
153 g_slist_free(list);
154 return oc;
157 SuperHCPU *cpu_sh4_init(const char *cpu_model)
159 return SUPERH_CPU(cpu_generic_init(TYPE_SUPERH_CPU, cpu_model));
162 static void sh7750r_cpu_initfn(Object *obj)
164 SuperHCPU *cpu = SUPERH_CPU(obj);
165 CPUSH4State *env = &cpu->env;
167 env->id = SH_CPU_SH7750R;
168 env->features = SH_FEATURE_BCR3_AND_BCR4;
171 static void sh7750r_class_init(ObjectClass *oc, void *data)
173 SuperHCPUClass *scc = SUPERH_CPU_CLASS(oc);
175 scc->name = "SH7750R";
176 scc->pvr = 0x00050000;
177 scc->prr = 0x00000100;
178 scc->cvr = 0x00110000;
181 static const TypeInfo sh7750r_type_info = {
182 .name = TYPE_SH7750R_CPU,
183 .parent = TYPE_SUPERH_CPU,
184 .class_init = sh7750r_class_init,
185 .instance_init = sh7750r_cpu_initfn,
188 static void sh7751r_cpu_initfn(Object *obj)
190 SuperHCPU *cpu = SUPERH_CPU(obj);
191 CPUSH4State *env = &cpu->env;
193 env->id = SH_CPU_SH7751R;
194 env->features = SH_FEATURE_BCR3_AND_BCR4;
197 static void sh7751r_class_init(ObjectClass *oc, void *data)
199 SuperHCPUClass *scc = SUPERH_CPU_CLASS(oc);
201 scc->name = "SH7751R";
202 scc->pvr = 0x04050005;
203 scc->prr = 0x00000113;
204 scc->cvr = 0x00110000; /* Neutered caches, should be 0x20480000 */
207 static const TypeInfo sh7751r_type_info = {
208 .name = TYPE_SH7751R_CPU,
209 .parent = TYPE_SUPERH_CPU,
210 .class_init = sh7751r_class_init,
211 .instance_init = sh7751r_cpu_initfn,
214 static void sh7785_cpu_initfn(Object *obj)
216 SuperHCPU *cpu = SUPERH_CPU(obj);
217 CPUSH4State *env = &cpu->env;
219 env->id = SH_CPU_SH7785;
220 env->features = SH_FEATURE_SH4A;
223 static void sh7785_class_init(ObjectClass *oc, void *data)
225 SuperHCPUClass *scc = SUPERH_CPU_CLASS(oc);
227 scc->name = "SH7785";
228 scc->pvr = 0x10300700;
229 scc->prr = 0x00000200;
230 scc->cvr = 0x71440211;
233 static const TypeInfo sh7785_type_info = {
234 .name = TYPE_SH7785_CPU,
235 .parent = TYPE_SUPERH_CPU,
236 .class_init = sh7785_class_init,
237 .instance_init = sh7785_cpu_initfn,
240 static void superh_cpu_realizefn(DeviceState *dev, Error **errp)
242 CPUState *cs = CPU(dev);
243 SuperHCPUClass *scc = SUPERH_CPU_GET_CLASS(dev);
245 cpu_reset(cs);
246 qemu_init_vcpu(cs);
248 scc->parent_realize(dev, errp);
251 static void superh_cpu_initfn(Object *obj)
253 CPUState *cs = CPU(obj);
254 SuperHCPU *cpu = SUPERH_CPU(obj);
255 CPUSH4State *env = &cpu->env;
257 cs->env_ptr = env;
258 cpu_exec_init(cs, &error_abort);
260 env->movcal_backup_tail = &(env->movcal_backup);
262 if (tcg_enabled()) {
263 sh4_translate_init();
267 static const VMStateDescription vmstate_sh_cpu = {
268 .name = "cpu",
269 .unmigratable = 1,
272 static void superh_cpu_class_init(ObjectClass *oc, void *data)
274 DeviceClass *dc = DEVICE_CLASS(oc);
275 CPUClass *cc = CPU_CLASS(oc);
276 SuperHCPUClass *scc = SUPERH_CPU_CLASS(oc);
278 scc->parent_realize = dc->realize;
279 dc->realize = superh_cpu_realizefn;
281 scc->parent_reset = cc->reset;
282 cc->reset = superh_cpu_reset;
284 cc->class_by_name = superh_cpu_class_by_name;
285 cc->has_work = superh_cpu_has_work;
286 cc->do_interrupt = superh_cpu_do_interrupt;
287 cc->cpu_exec_interrupt = superh_cpu_exec_interrupt;
288 cc->dump_state = superh_cpu_dump_state;
289 cc->set_pc = superh_cpu_set_pc;
290 cc->synchronize_from_tb = superh_cpu_synchronize_from_tb;
291 cc->gdb_read_register = superh_cpu_gdb_read_register;
292 cc->gdb_write_register = superh_cpu_gdb_write_register;
293 #ifdef CONFIG_USER_ONLY
294 cc->handle_mmu_fault = superh_cpu_handle_mmu_fault;
295 #else
296 cc->get_phys_page_debug = superh_cpu_get_phys_page_debug;
297 #endif
298 cc->disas_set_info = superh_cpu_disas_set_info;
300 cc->gdb_num_core_regs = 59;
302 dc->vmsd = &vmstate_sh_cpu;
305 * Reason: superh_cpu_initfn() calls cpu_exec_init(), which saves
306 * the object in cpus -> dangling pointer after final
307 * object_unref().
309 dc->cannot_destroy_with_object_finalize_yet = true;
312 static const TypeInfo superh_cpu_type_info = {
313 .name = TYPE_SUPERH_CPU,
314 .parent = TYPE_CPU,
315 .instance_size = sizeof(SuperHCPU),
316 .instance_init = superh_cpu_initfn,
317 .abstract = true,
318 .class_size = sizeof(SuperHCPUClass),
319 .class_init = superh_cpu_class_init,
322 static void superh_cpu_register_types(void)
324 type_register_static(&superh_cpu_type_info);
325 type_register_static(&sh7750r_type_info);
326 type_register_static(&sh7751r_type_info);
327 type_register_static(&sh7785_type_info);
330 type_init(superh_cpu_register_types)