xref: /qemu/hw/core/cpu-common.c (revision bcfee4938f8d4e8bf5f49981d3c8a78cf267cb4e)
1 /*
2  * QEMU CPU model
3  *
4  * Copyright (c) 2012-2014 SUSE LINUX Products GmbH
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see
18  * <http://www.gnu.org/licenses/gpl-2.0.html>
19  */
20 
21 #include "qemu/osdep.h"
22 #include "qapi/error.h"
23 #include "hw/core/cpu.h"
24 #include "system/hw_accel.h"
25 #include "qemu/log.h"
26 #include "qemu/main-loop.h"
27 #include "qemu/lockcnt.h"
28 #include "qemu/error-report.h"
29 #include "qemu/qemu-print.h"
30 #include "qemu/target-info.h"
31 #include "exec/log.h"
32 #include "exec/gdbstub.h"
33 #include "system/tcg.h"
34 #include "hw/boards.h"
35 #include "hw/qdev-properties.h"
36 #include "trace.h"
37 #ifdef CONFIG_PLUGIN
38 #include "qemu/plugin.h"
39 #endif
40 
41 CPUState *cpu_by_arch_id(int64_t id)
42 {
43     CPUState *cpu;
44 
45     CPU_FOREACH(cpu) {
46         if (cpu->cc->get_arch_id(cpu) == id) {
47             return cpu;
48         }
49     }
50     return NULL;
51 }
52 
53 bool cpu_exists(int64_t id)
54 {
55     return !!cpu_by_arch_id(id);
56 }
57 
58 CPUState *cpu_create(const char *typename)
59 {
60     Error *err = NULL;
61     CPUState *cpu = CPU(object_new(typename));
62     if (!qdev_realize(DEVICE(cpu), NULL, &err)) {
63         error_report_err(err);
64         object_unref(OBJECT(cpu));
65         exit(EXIT_FAILURE);
66     }
67     return cpu;
68 }
69 
70 /* Resetting the IRQ comes from across the code base so we take the
71  * BQL here if we need to.  cpu_interrupt assumes it is held.*/
72 void cpu_reset_interrupt(CPUState *cpu, int mask)
73 {
74     bool need_lock = !bql_locked();
75 
76     if (need_lock) {
77         bql_lock();
78     }
79     cpu->interrupt_request &= ~mask;
80     if (need_lock) {
81         bql_unlock();
82     }
83 }
84 
85 void cpu_exit(CPUState *cpu)
86 {
87     qatomic_set(&cpu->exit_request, 1);
88     /* Ensure cpu_exec will see the exit request after TCG has exited.  */
89     smp_wmb();
90     qatomic_set(&cpu->neg.icount_decr.u16.high, -1);
91 }
92 
93 static int cpu_common_gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
94 {
95     return 0;
96 }
97 
98 static int cpu_common_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg)
99 {
100     return 0;
101 }
102 
103 void cpu_dump_state(CPUState *cpu, FILE *f, int flags)
104 {
105     if (cpu->cc->dump_state) {
106         cpu_synchronize_state(cpu);
107         cpu->cc->dump_state(cpu, f, flags);
108     }
109 }
110 
111 void cpu_reset(CPUState *cpu)
112 {
113     device_cold_reset(DEVICE(cpu));
114 
115     trace_cpu_reset(cpu->cpu_index);
116 }
117 
118 static void cpu_common_reset_hold(Object *obj, ResetType type)
119 {
120     CPUState *cpu = CPU(obj);
121 
122     if (qemu_loglevel_mask(CPU_LOG_RESET)) {
123         qemu_log("CPU Reset (CPU %d)\n", cpu->cpu_index);
124         log_cpu_state(cpu, cpu->cc->reset_dump_flags);
125     }
126 
127     cpu->interrupt_request = 0;
128     cpu->halted = cpu->start_powered_off;
129     cpu->mem_io_pc = 0;
130     cpu->icount_extra = 0;
131     qatomic_set(&cpu->neg.icount_decr.u32, 0);
132     cpu->neg.can_do_io = true;
133     cpu->exception_index = -1;
134     cpu->crash_occurred = false;
135     cpu->cflags_next_tb = -1;
136 
137     cpu_exec_reset_hold(cpu);
138 }
139 
140 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
141 {
142     ObjectClass *oc;
143     CPUClass *cc;
144 
145     oc = object_class_by_name(typename);
146     cc = CPU_CLASS(oc);
147     assert(cc->class_by_name);
148     assert(cpu_model);
149     oc = cc->class_by_name(cpu_model);
150     if (object_class_dynamic_cast(oc, typename) &&
151         !object_class_is_abstract(oc)) {
152         return oc;
153     }
154 
155     return NULL;
156 }
157 
158 char *cpu_model_from_type(const char *typename)
159 {
160     g_autofree char *suffix = g_strdup_printf("-%s", target_cpu_type());
161 
162     if (!object_class_by_name(typename)) {
163         return NULL;
164     }
165 
166     if (g_str_has_suffix(typename, suffix)) {
167         return g_strndup(typename, strlen(typename) - strlen(suffix));
168     }
169 
170     return g_strdup(typename);
171 }
172 
173 static void cpu_common_parse_features(const char *typename, char *features,
174                                       Error **errp)
175 {
176     char *val;
177     static bool cpu_globals_initialized;
178     /* Single "key=value" string being parsed */
179     char *featurestr = features ? strtok(features, ",") : NULL;
180 
181     /* should be called only once, catch invalid users */
182     assert(!cpu_globals_initialized);
183     cpu_globals_initialized = true;
184 
185     while (featurestr) {
186         val = strchr(featurestr, '=');
187         if (val) {
188             GlobalProperty *prop = g_new0(typeof(*prop), 1);
189             *val = 0;
190             val++;
191             prop->driver = typename;
192             prop->property = g_strdup(featurestr);
193             prop->value = g_strdup(val);
194             qdev_prop_register_global(prop);
195         } else {
196             error_setg(errp, "Expected key=value format, found %s.",
197                        featurestr);
198             return;
199         }
200         featurestr = strtok(NULL, ",");
201     }
202 }
203 
204 const char *parse_cpu_option(const char *cpu_option)
205 {
206     ObjectClass *oc;
207     CPUClass *cc;
208     gchar **model_pieces;
209     const char *cpu_type;
210 
211     model_pieces = g_strsplit(cpu_option, ",", 2);
212     if (!model_pieces[0]) {
213         error_report("-cpu option cannot be empty");
214         exit(1);
215     }
216 
217     oc = cpu_class_by_name(target_cpu_type(), model_pieces[0]);
218     if (oc == NULL) {
219         error_report("unable to find CPU model '%s'", model_pieces[0]);
220         g_strfreev(model_pieces);
221         exit(EXIT_FAILURE);
222     }
223 
224     cpu_type = object_class_get_name(oc);
225     cc = CPU_CLASS(oc);
226     cc->parse_features(cpu_type, model_pieces[1], &error_fatal);
227     g_strfreev(model_pieces);
228     return cpu_type;
229 }
230 
231 bool cpu_exec_realizefn(CPUState *cpu, Error **errp)
232 {
233     if (!accel_cpu_common_realize(cpu, errp)) {
234         return false;
235     }
236 
237     /* Wait until cpu initialization complete before exposing cpu. */
238     cpu_list_add(cpu);
239 
240     cpu_vmstate_register(cpu);
241 
242     return true;
243 }
244 
245 static void cpu_common_realizefn(DeviceState *dev, Error **errp)
246 {
247     CPUState *cpu = CPU(dev);
248     Object *machine = qdev_get_machine();
249 
250     /* qdev_get_machine() can return something that's not TYPE_MACHINE
251      * if this is one of the user-only emulators; in that case there's
252      * no need to check the ignore_memory_transaction_failures board flag.
253      */
254     if (object_dynamic_cast(machine, TYPE_MACHINE)) {
255         MachineClass *mc = MACHINE_GET_CLASS(machine);
256 
257         if (mc) {
258             cpu->ignore_memory_transaction_failures =
259                 mc->ignore_memory_transaction_failures;
260         }
261     }
262 
263     if (dev->hotplugged) {
264         cpu_synchronize_post_init(cpu);
265         cpu_resume(cpu);
266     }
267 
268     /* NOTE: latest generic point where the cpu is fully realized */
269 }
270 
271 static void cpu_common_unrealizefn(DeviceState *dev)
272 {
273     CPUState *cpu = CPU(dev);
274 
275     /* Call the plugin hook before clearing the cpu is fully unrealized */
276 #ifdef CONFIG_PLUGIN
277     if (tcg_enabled()) {
278         qemu_plugin_vcpu_exit_hook(cpu);
279     }
280 #endif
281 
282     /* NOTE: latest generic point before the cpu is fully unrealized */
283     cpu_exec_unrealizefn(cpu);
284 }
285 
286 void cpu_exec_unrealizefn(CPUState *cpu)
287 {
288     cpu_vmstate_unregister(cpu);
289 
290     cpu_list_remove(cpu);
291     /*
292      * Now that the vCPU has been removed from the RCU list, we can call
293      * accel_cpu_common_unrealize, which may free fields using call_rcu.
294      */
295     accel_cpu_common_unrealize(cpu);
296 }
297 
298 static void cpu_common_initfn(Object *obj)
299 {
300     CPUState *cpu = CPU(obj);
301 
302     cpu_exec_class_post_init(CPU_GET_CLASS(obj));
303 
304     /* cache the cpu class for the hotpath */
305     cpu->cc = CPU_GET_CLASS(cpu);
306 
307     gdb_init_cpu(cpu);
308     cpu->cpu_index = UNASSIGNED_CPU_INDEX;
309     cpu->cluster_index = UNASSIGNED_CLUSTER_INDEX;
310     cpu->as = NULL;
311     cpu->num_ases = 0;
312     /* user-mode doesn't have configurable SMP topology */
313     /* the default value is changed by qemu_init_vcpu() for system-mode */
314     cpu->nr_threads = 1;
315 
316     /* allocate storage for thread info, initialise condition variables */
317     cpu->thread = g_new0(QemuThread, 1);
318     cpu->halt_cond = g_new0(QemuCond, 1);
319     qemu_cond_init(cpu->halt_cond);
320 
321     qemu_mutex_init(&cpu->work_mutex);
322     qemu_lockcnt_init(&cpu->in_ioctl_lock);
323     QSIMPLEQ_INIT(&cpu->work_list);
324     QTAILQ_INIT(&cpu->breakpoints);
325     QTAILQ_INIT(&cpu->watchpoints);
326 
327     cpu_exec_initfn(cpu);
328 
329     /*
330      * Plugin initialization must wait until the cpu start executing
331      * code, but we must queue this work before the threads are
332      * created to ensure we don't race.
333      */
334 #ifdef CONFIG_PLUGIN
335     if (tcg_enabled()) {
336         cpu->plugin_state = qemu_plugin_create_vcpu_state();
337         qemu_plugin_vcpu_init_hook(cpu);
338     }
339 #endif
340 }
341 
342 static void cpu_common_finalize(Object *obj)
343 {
344     CPUState *cpu = CPU(obj);
345 
346 #ifdef CONFIG_PLUGIN
347     if (tcg_enabled()) {
348         g_free(cpu->plugin_state);
349     }
350 #endif
351     free_queued_cpu_work(cpu);
352     /* If cleanup didn't happen in context to gdb_unregister_coprocessor_all */
353     if (cpu->gdb_regs) {
354         g_array_free(cpu->gdb_regs, TRUE);
355     }
356     qemu_lockcnt_destroy(&cpu->in_ioctl_lock);
357     qemu_mutex_destroy(&cpu->work_mutex);
358     qemu_cond_destroy(cpu->halt_cond);
359     g_free(cpu->halt_cond);
360     g_free(cpu->thread);
361 }
362 
363 static int64_t cpu_common_get_arch_id(CPUState *cpu)
364 {
365     return cpu->cpu_index;
366 }
367 
368 static void cpu_common_class_init(ObjectClass *klass, const void *data)
369 {
370     DeviceClass *dc = DEVICE_CLASS(klass);
371     ResettableClass *rc = RESETTABLE_CLASS(klass);
372     CPUClass *k = CPU_CLASS(klass);
373 
374     k->parse_features = cpu_common_parse_features;
375     k->get_arch_id = cpu_common_get_arch_id;
376     k->gdb_read_register = cpu_common_gdb_read_register;
377     k->gdb_write_register = cpu_common_gdb_write_register;
378     set_bit(DEVICE_CATEGORY_CPU, dc->categories);
379     dc->realize = cpu_common_realizefn;
380     dc->unrealize = cpu_common_unrealizefn;
381     rc->phases.hold = cpu_common_reset_hold;
382     cpu_class_init_props(dc);
383     /*
384      * Reason: CPUs still need special care by board code: wiring up
385      * IRQs, adding reset handlers, halting non-first CPUs, ...
386      */
387     dc->user_creatable = false;
388 }
389 
390 static const TypeInfo cpu_type_info = {
391     .name = TYPE_CPU,
392     .parent = TYPE_DEVICE,
393     .instance_size = sizeof(CPUState),
394     .instance_init = cpu_common_initfn,
395     .instance_finalize = cpu_common_finalize,
396     .abstract = true,
397     .class_size = sizeof(CPUClass),
398     .class_init = cpu_common_class_init,
399 };
400 
401 static void cpu_register_types(void)
402 {
403     type_register_static(&cpu_type_info);
404 }
405 
406 type_init(cpu_register_types)
407 
408 static void cpu_list_entry(gpointer data, gpointer user_data)
409 {
410     CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data));
411     const char *typename = object_class_get_name(OBJECT_CLASS(data));
412     g_autofree char *model = cpu_model_from_type(typename);
413 
414     if (cc->deprecation_note) {
415         qemu_printf("  %s (deprecated)\n", model);
416     } else {
417         qemu_printf("  %s\n", model);
418     }
419 }
420 
421 void list_cpus(void)
422 {
423     CPUClass *cc = CPU_CLASS(object_class_by_name(target_cpu_type()));
424 
425     if (cc->list_cpus) {
426         cc->list_cpus();
427     } else {
428         GSList *list;
429 
430         list = object_class_get_list_sorted(TYPE_CPU, false);
431         qemu_printf("Available CPUs:\n");
432         g_slist_foreach(list, cpu_list_entry, NULL);
433         g_slist_free(list);
434     }
435 }
436