cputlb: Change tlb_flush() argument to CPUState
[qemu.git] / target-m68k / cpu.c
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 "cpu.h"
22 #include "qemu-common.h"
23 #include "migration/vmstate.h"
24
25
26 static void m68k_cpu_set_pc(CPUState *cs, vaddr value)
27 {
28 M68kCPU *cpu = M68K_CPU(cs);
29
30 cpu->env.pc = value;
31 }
32
33 static bool m68k_cpu_has_work(CPUState *cs)
34 {
35 return cs->interrupt_request & CPU_INTERRUPT_HARD;
36 }
37
38 static void m68k_set_feature(CPUM68KState *env, int feature)
39 {
40 env->features |= (1u << feature);
41 }
42
43 /* CPUClass::reset() */
44 static void m68k_cpu_reset(CPUState *s)
45 {
46 M68kCPU *cpu = M68K_CPU(s);
47 M68kCPUClass *mcc = M68K_CPU_GET_CLASS(cpu);
48 CPUM68KState *env = &cpu->env;
49
50 mcc->parent_reset(s);
51
52 memset(env, 0, offsetof(CPUM68KState, features));
53 #if !defined(CONFIG_USER_ONLY)
54 env->sr = 0x2700;
55 #endif
56 m68k_switch_sp(env);
57 /* ??? FP regs should be initialized to NaN. */
58 env->cc_op = CC_OP_FLAGS;
59 /* TODO: We should set PC from the interrupt vector. */
60 env->pc = 0;
61 tlb_flush(s, 1);
62 }
63
64 /* CPU models */
65
66 static ObjectClass *m68k_cpu_class_by_name(const char *cpu_model)
67 {
68 ObjectClass *oc;
69 char *typename;
70
71 if (cpu_model == NULL) {
72 return NULL;
73 }
74
75 typename = g_strdup_printf("%s-" TYPE_M68K_CPU, cpu_model);
76 oc = object_class_by_name(typename);
77 g_free(typename);
78 if (oc != NULL && (object_class_dynamic_cast(oc, TYPE_M68K_CPU) == NULL ||
79 object_class_is_abstract(oc))) {
80 return NULL;
81 }
82 return oc;
83 }
84
85 static void m5206_cpu_initfn(Object *obj)
86 {
87 M68kCPU *cpu = M68K_CPU(obj);
88 CPUM68KState *env = &cpu->env;
89
90 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A);
91 }
92
93 static void m5208_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 m68k_set_feature(env, M68K_FEATURE_CF_ISA_APLUSC);
100 m68k_set_feature(env, M68K_FEATURE_BRAL);
101 m68k_set_feature(env, M68K_FEATURE_CF_EMAC);
102 m68k_set_feature(env, M68K_FEATURE_USP);
103 }
104
105 static void cfv4e_cpu_initfn(Object *obj)
106 {
107 M68kCPU *cpu = M68K_CPU(obj);
108 CPUM68KState *env = &cpu->env;
109
110 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A);
111 m68k_set_feature(env, M68K_FEATURE_CF_ISA_B);
112 m68k_set_feature(env, M68K_FEATURE_BRAL);
113 m68k_set_feature(env, M68K_FEATURE_CF_FPU);
114 m68k_set_feature(env, M68K_FEATURE_CF_EMAC);
115 m68k_set_feature(env, M68K_FEATURE_USP);
116 }
117
118 static void any_cpu_initfn(Object *obj)
119 {
120 M68kCPU *cpu = M68K_CPU(obj);
121 CPUM68KState *env = &cpu->env;
122
123 m68k_set_feature(env, M68K_FEATURE_CF_ISA_A);
124 m68k_set_feature(env, M68K_FEATURE_CF_ISA_B);
125 m68k_set_feature(env, M68K_FEATURE_CF_ISA_APLUSC);
126 m68k_set_feature(env, M68K_FEATURE_BRAL);
127 m68k_set_feature(env, M68K_FEATURE_CF_FPU);
128 /* MAC and EMAC are mututally exclusive, so pick EMAC.
129 It's mostly backwards compatible. */
130 m68k_set_feature(env, M68K_FEATURE_CF_EMAC);
131 m68k_set_feature(env, M68K_FEATURE_CF_EMAC_B);
132 m68k_set_feature(env, M68K_FEATURE_USP);
133 m68k_set_feature(env, M68K_FEATURE_EXT_FULL);
134 m68k_set_feature(env, M68K_FEATURE_WORD_INDEX);
135 }
136
137 typedef struct M68kCPUInfo {
138 const char *name;
139 void (*instance_init)(Object *obj);
140 } M68kCPUInfo;
141
142 static const M68kCPUInfo m68k_cpus[] = {
143 { .name = "m5206", .instance_init = m5206_cpu_initfn },
144 { .name = "m5208", .instance_init = m5208_cpu_initfn },
145 { .name = "cfv4e", .instance_init = cfv4e_cpu_initfn },
146 { .name = "any", .instance_init = any_cpu_initfn },
147 };
148
149 static void m68k_cpu_realizefn(DeviceState *dev, Error **errp)
150 {
151 CPUState *cs = CPU(dev);
152 M68kCPU *cpu = M68K_CPU(dev);
153 M68kCPUClass *mcc = M68K_CPU_GET_CLASS(dev);
154
155 m68k_cpu_init_gdb(cpu);
156
157 cpu_reset(cs);
158 qemu_init_vcpu(cs);
159
160 mcc->parent_realize(dev, errp);
161 }
162
163 static void m68k_cpu_initfn(Object *obj)
164 {
165 CPUState *cs = CPU(obj);
166 M68kCPU *cpu = M68K_CPU(obj);
167 CPUM68KState *env = &cpu->env;
168 static bool inited;
169
170 cs->env_ptr = env;
171 cpu_exec_init(env);
172
173 if (tcg_enabled() && !inited) {
174 inited = true;
175 m68k_tcg_init();
176 }
177 }
178
179 static const VMStateDescription vmstate_m68k_cpu = {
180 .name = "cpu",
181 .unmigratable = 1,
182 };
183
184 static void m68k_cpu_class_init(ObjectClass *c, void *data)
185 {
186 M68kCPUClass *mcc = M68K_CPU_CLASS(c);
187 CPUClass *cc = CPU_CLASS(c);
188 DeviceClass *dc = DEVICE_CLASS(c);
189
190 mcc->parent_realize = dc->realize;
191 dc->realize = m68k_cpu_realizefn;
192
193 mcc->parent_reset = cc->reset;
194 cc->reset = m68k_cpu_reset;
195
196 cc->class_by_name = m68k_cpu_class_by_name;
197 cc->has_work = m68k_cpu_has_work;
198 cc->do_interrupt = m68k_cpu_do_interrupt;
199 cc->dump_state = m68k_cpu_dump_state;
200 cc->set_pc = m68k_cpu_set_pc;
201 cc->gdb_read_register = m68k_cpu_gdb_read_register;
202 cc->gdb_write_register = m68k_cpu_gdb_write_register;
203 #ifdef CONFIG_USER_ONLY
204 cc->handle_mmu_fault = m68k_cpu_handle_mmu_fault;
205 #else
206 cc->get_phys_page_debug = m68k_cpu_get_phys_page_debug;
207 #endif
208 dc->vmsd = &vmstate_m68k_cpu;
209 cc->gdb_num_core_regs = 18;
210 cc->gdb_core_xml_file = "cf-core.xml";
211 }
212
213 static void register_cpu_type(const M68kCPUInfo *info)
214 {
215 TypeInfo type_info = {
216 .parent = TYPE_M68K_CPU,
217 .instance_init = info->instance_init,
218 };
219
220 type_info.name = g_strdup_printf("%s-" TYPE_M68K_CPU, info->name);
221 type_register(&type_info);
222 g_free((void *)type_info.name);
223 }
224
225 static const TypeInfo m68k_cpu_type_info = {
226 .name = TYPE_M68K_CPU,
227 .parent = TYPE_CPU,
228 .instance_size = sizeof(M68kCPU),
229 .instance_init = m68k_cpu_initfn,
230 .abstract = true,
231 .class_size = sizeof(M68kCPUClass),
232 .class_init = m68k_cpu_class_init,
233 };
234
235 static void m68k_cpu_register_types(void)
236 {
237 int i;
238
239 type_register_static(&m68k_cpu_type_info);
240 for (i = 0; i < ARRAY_SIZE(m68k_cpus); i++) {
241 register_cpu_type(&m68k_cpus[i]);
242 }
243 }
244
245 type_init(m68k_cpu_register_types)