1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "qemu/osdep.h"
22#include "qapi/error.h"
23#include "qemu-common.h"
24#include "qom/cpu.h"
25#include "sysemu/kvm.h"
26#include "qemu/notify.h"
27#include "qemu/log.h"
28#include "exec/log.h"
29#include "qemu/error-report.h"
30#include "sysemu/sysemu.h"
31#include "hw/qdev-properties.h"
32
33
34bool cpu_exists(int64_t id)
35{
36 CPUState *cpu;
37
38 CPU_FOREACH(cpu) {
39 CPUClass *cc = CPU_GET_CLASS(cpu);
40
41 if (cc->get_arch_id(cpu) == id) {
42 return true;
43 }
44 }
45 return false;
46}
47
48CPUState *cpu_generic_init(const char *typename, const char *cpu_model)
49{
50 char *str, *name, *featurestr;
51 CPUState *cpu;
52 ObjectClass *oc;
53 CPUClass *cc;
54 Error *err = NULL;
55
56 str = g_strdup(cpu_model);
57 name = strtok(str, ",");
58
59 oc = cpu_class_by_name(typename, name);
60 if (oc == NULL) {
61 g_free(str);
62 return NULL;
63 }
64
65 cpu = CPU(object_new(object_class_get_name(oc)));
66 cc = CPU_GET_CLASS(cpu);
67
68 featurestr = strtok(NULL, ",");
69 cc->parse_features(cpu, featurestr, &err);
70 g_free(str);
71 if (err != NULL) {
72 goto out;
73 }
74
75 object_property_set_bool(OBJECT(cpu), true, "realized", &err);
76
77out:
78 if (err != NULL) {
79 error_report_err(err);
80 object_unref(OBJECT(cpu));
81 return NULL;
82 }
83
84 return cpu;
85}
86
87bool cpu_paging_enabled(const CPUState *cpu)
88{
89 CPUClass *cc = CPU_GET_CLASS(cpu);
90
91 return cc->get_paging_enabled(cpu);
92}
93
94static bool cpu_common_get_paging_enabled(const CPUState *cpu)
95{
96 return false;
97}
98
99void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
100 Error **errp)
101{
102 CPUClass *cc = CPU_GET_CLASS(cpu);
103
104 cc->get_memory_mapping(cpu, list, errp);
105}
106
107static void cpu_common_get_memory_mapping(CPUState *cpu,
108 MemoryMappingList *list,
109 Error **errp)
110{
111 error_setg(errp, "Obtaining memory mappings is unsupported on this CPU.");
112}
113
114void cpu_reset_interrupt(CPUState *cpu, int mask)
115{
116 cpu->interrupt_request &= ~mask;
117}
118
119void cpu_exit(CPUState *cpu)
120{
121 cpu->exit_request = 1;
122
123 smp_wmb();
124 cpu->tcg_exit_req = 1;
125}
126
127int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
128 void *opaque)
129{
130 CPUClass *cc = CPU_GET_CLASS(cpu);
131
132 return (*cc->write_elf32_qemunote)(f, cpu, opaque);
133}
134
135static int cpu_common_write_elf32_qemunote(WriteCoreDumpFunction f,
136 CPUState *cpu, void *opaque)
137{
138 return 0;
139}
140
141int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
142 int cpuid, void *opaque)
143{
144 CPUClass *cc = CPU_GET_CLASS(cpu);
145
146 return (*cc->write_elf32_note)(f, cpu, cpuid, opaque);
147}
148
149static int cpu_common_write_elf32_note(WriteCoreDumpFunction f,
150 CPUState *cpu, int cpuid,
151 void *opaque)
152{
153 return -1;
154}
155
156int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
157 void *opaque)
158{
159 CPUClass *cc = CPU_GET_CLASS(cpu);
160
161 return (*cc->write_elf64_qemunote)(f, cpu, opaque);
162}
163
164static int cpu_common_write_elf64_qemunote(WriteCoreDumpFunction f,
165 CPUState *cpu, void *opaque)
166{
167 return 0;
168}
169
170int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
171 int cpuid, void *opaque)
172{
173 CPUClass *cc = CPU_GET_CLASS(cpu);
174
175 return (*cc->write_elf64_note)(f, cpu, cpuid, opaque);
176}
177
178static int cpu_common_write_elf64_note(WriteCoreDumpFunction f,
179 CPUState *cpu, int cpuid,
180 void *opaque)
181{
182 return -1;
183}
184
185
186static int cpu_common_gdb_read_register(CPUState *cpu, uint8_t *buf, int reg)
187{
188 return 0;
189}
190
191static int cpu_common_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg)
192{
193 return 0;
194}
195
196static bool cpu_common_debug_check_watchpoint(CPUState *cpu, CPUWatchpoint *wp)
197{
198
199
200
201 return true;
202}
203
204bool target_words_bigendian(void);
205static bool cpu_common_virtio_is_big_endian(CPUState *cpu)
206{
207 return target_words_bigendian();
208}
209
210static void cpu_common_noop(CPUState *cpu)
211{
212}
213
214static bool cpu_common_exec_interrupt(CPUState *cpu, int int_req)
215{
216 return false;
217}
218
219void cpu_dump_state(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
220 int flags)
221{
222 CPUClass *cc = CPU_GET_CLASS(cpu);
223
224 if (cc->dump_state) {
225 cpu_synchronize_state(cpu);
226 cc->dump_state(cpu, f, cpu_fprintf, flags);
227 }
228}
229
230void cpu_dump_statistics(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
231 int flags)
232{
233 CPUClass *cc = CPU_GET_CLASS(cpu);
234
235 if (cc->dump_statistics) {
236 cc->dump_statistics(cpu, f, cpu_fprintf, flags);
237 }
238}
239
240void cpu_reset(CPUState *cpu)
241{
242 CPUClass *klass = CPU_GET_CLASS(cpu);
243
244 if (klass->reset != NULL) {
245 (*klass->reset)(cpu);
246 }
247}
248
249static void cpu_common_reset(CPUState *cpu)
250{
251 CPUClass *cc = CPU_GET_CLASS(cpu);
252 bool old_halt = cpu->halt_pin;
253 bool old_reset = cpu->reset_pin;
254
255 if (qemu_loglevel_mask(CPU_LOG_RESET)) {
256 qemu_log("CPU Reset (CPU %d)\n", cpu->cpu_index);
257 log_cpu_state(cpu, cc->reset_dump_flags);
258 }
259
260 cpu->interrupt_request = 0;
261 cpu->halted = 0;
262 cpu->mem_io_pc = 0;
263 cpu->mem_io_vaddr = 0;
264 cpu->icount_extra = 0;
265 cpu->icount_decr.u32 = 0;
266 cpu->can_do_io = 1;
267 cpu->exception_index = -1;
268 cpu->crash_occurred = false;
269 memset(cpu->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof(void *));
270 cpu_halt_gpio(cpu, 0, old_halt);
271 cpu_reset_gpio(cpu, 0, old_reset);
272}
273
274static bool cpu_common_has_work(CPUState *cs)
275{
276 return false;
277}
278
279static void cpu_device_reset(DeviceState *dev)
280{
281 cpu_reset(CPU(dev));
282}
283
284static void cpu_common_halt(DeviceState *dev)
285{
286 CPUState *s = CPU(dev);
287
288 s->halted = 1;
289}
290
291static void cpu_common_unhalt(DeviceState *dev)
292{
293 CPUState *s = CPU(dev);
294
295 s->halted = 0;
296}
297
298ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
299{
300 CPUClass *cc = CPU_CLASS(object_class_by_name(typename));
301
302 return cc->class_by_name(cpu_model);
303}
304
305static ObjectClass *cpu_common_class_by_name(const char *cpu_model)
306{
307 return NULL;
308}
309
310static void cpu_common_parse_features(CPUState *cpu, char *features,
311 Error **errp)
312{
313 char *featurestr;
314 char *val;
315 Error *err = NULL;
316
317 featurestr = features ? strtok(features, ",") : NULL;
318
319 while (featurestr) {
320 val = strchr(featurestr, '=');
321 if (val) {
322 *val = 0;
323 val++;
324 object_property_parse(OBJECT(cpu), val, featurestr, &err);
325 if (err) {
326 error_propagate(errp, err);
327 return;
328 }
329 } else {
330 error_setg(errp, "Expected key=value format, found %s.",
331 featurestr);
332 return;
333 }
334 featurestr = strtok(NULL, ",");
335 }
336}
337
338static void cpu_common_realizefn(DeviceState *dev, Error **errp)
339{
340 CPUState *cpu = CPU(dev);
341
342 if (dev->hotplugged) {
343 cpu_synchronize_post_init(cpu);
344 cpu_resume(cpu);
345 }
346}
347
348static void cpu_common_initfn(Object *obj)
349{
350 CPUState *cpu = CPU(obj);
351 CPUClass *cc = CPU_GET_CLASS(obj);
352
353 cpu->cpu_index = -1;
354 cpu->gdb_num_regs = cpu->gdb_num_g_regs = cc->gdb_num_core_regs;
355
356 qdev_init_gpio_in_named(DEVICE(obj), cpu_reset_gpio, "reset",1);
357 qdev_init_gpio_in_named(DEVICE(obj), cpu_halt_gpio, "halt", 1);
358
359 qemu_mutex_init(&cpu->work_mutex);
360 QTAILQ_INIT(&cpu->breakpoints);
361 QTAILQ_INIT(&cpu->watchpoints);
362}
363
364static void cpu_common_finalize(Object *obj)
365{
366 cpu_exec_exit(CPU(obj));
367}
368
369static int64_t cpu_common_get_arch_id(CPUState *cpu)
370{
371 return cpu->cpu_index;
372}
373
374static Property cpu_common_properties[] = {
375 DEFINE_PROP_STRING("gdb-id", CPUState, gdb_id),
376 DEFINE_PROP_END_OF_LIST(),
377};
378
379static void cpu_class_init(ObjectClass *klass, void *data)
380{
381 DeviceClass *dc = DEVICE_CLASS(klass);
382 CPUClass *k = CPU_CLASS(klass);
383
384 k->class_by_name = cpu_common_class_by_name;
385 k->parse_features = cpu_common_parse_features;
386 k->reset = cpu_common_reset;
387 k->get_arch_id = cpu_common_get_arch_id;
388 k->has_work = cpu_common_has_work;
389 k->get_paging_enabled = cpu_common_get_paging_enabled;
390 k->get_memory_mapping = cpu_common_get_memory_mapping;
391 k->write_elf32_qemunote = cpu_common_write_elf32_qemunote;
392 k->write_elf32_note = cpu_common_write_elf32_note;
393 k->write_elf64_qemunote = cpu_common_write_elf64_qemunote;
394 k->write_elf64_note = cpu_common_write_elf64_note;
395 k->gdb_read_register = cpu_common_gdb_read_register;
396 k->gdb_write_register = cpu_common_gdb_write_register;
397 k->virtio_is_big_endian = cpu_common_virtio_is_big_endian;
398 k->debug_excp_handler = cpu_common_noop;
399 k->debug_check_watchpoint = cpu_common_debug_check_watchpoint;
400 k->cpu_exec_enter = cpu_common_noop;
401 k->cpu_exec_exit = cpu_common_noop;
402 k->cpu_exec_interrupt = cpu_common_exec_interrupt;
403 dc->reset = cpu_device_reset;
404 dc->halt = cpu_common_halt;
405 dc->unhalt = cpu_common_unhalt;
406 dc->realize = cpu_common_realizefn;
407 dc->props = cpu_common_properties;
408
409
410
411
412 dc->cannot_instantiate_with_device_add_yet = true;
413}
414
415static const TypeInfo cpu_type_info = {
416 .name = TYPE_CPU,
417 .parent = TYPE_DEVICE,
418 .instance_size = sizeof(CPUState),
419 .instance_init = cpu_common_initfn,
420 .instance_finalize = cpu_common_finalize,
421 .abstract = true,
422 .class_size = sizeof(CPUClass),
423 .class_init = cpu_class_init,
424};
425
426static void cpu_register_types(void)
427{
428 type_register_static(&cpu_type_info);
429}
430
431type_init(cpu_register_types)
432