xref: /qemu/hw/core/cpu-common.c (revision 897c68fb795cf03b89b6688a6f945d68a765c3e4)
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 "exec/log.h"
29 #include "exec/gdbstub.h"
30 #include "system/tcg.h"
31 #include "hw/boards.h"
32 #include "hw/qdev-properties.h"
33 #include "trace.h"
34 #ifdef CONFIG_PLUGIN
35 #include "qemu/plugin.h"
36 #endif
37 
38 CPUState *cpu_by_arch_id(int64_t id)
39 {
40     CPUState *cpu;
41 
42     CPU_FOREACH(cpu) {
43         if (cpu->cc->get_arch_id(cpu) == id) {
44             return cpu;
45         }
46     }
47     return NULL;
48 }
49 
50 bool cpu_exists(int64_t id)
51 {
52     return !!cpu_by_arch_id(id);
53 }
54 
55 CPUState *cpu_create(const char *typename)
56 {
57     Error *err = NULL;
58     CPUState *cpu = CPU(object_new(typename));
59     if (!qdev_realize(DEVICE(cpu), NULL, &err)) {
60         error_report_err(err);
61         object_unref(OBJECT(cpu));
62         exit(EXIT_FAILURE);
63     }
64     return cpu;
65 }
66 
67 /* Resetting the IRQ comes from across the code base so we take the
68  * BQL here if we need to.  cpu_interrupt assumes it is held.*/
69 void cpu_reset_interrupt(CPUState *cpu, int mask)
70 {
71     bool need_lock = !bql_locked();
72 
73     if (need_lock) {
74         bql_lock();
75     }
76     cpu->interrupt_request &= ~mask;
77     if (need_lock) {
78         bql_unlock();
79     }
80 }
81 
82 void cpu_exit(CPUState *cpu)
83 {
84     qatomic_set(&cpu->exit_request, 1);
85     /* Ensure cpu_exec will see the exit request after TCG has exited.  */
86     smp_wmb();
87     qatomic_set(&cpu->neg.icount_decr.u16.high, -1);
88 }
89 
90 static int cpu_common_gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
91 {
92     return 0;
93 }
94 
95 static int cpu_common_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg)
96 {
97     return 0;
98 }
99 
100 void cpu_dump_state(CPUState *cpu, FILE *f, int flags)
101 {
102     if (cpu->cc->dump_state) {
103         cpu_synchronize_state(cpu);
104         cpu->cc->dump_state(cpu, f, flags);
105     }
106 }
107 
108 void cpu_reset(CPUState *cpu)
109 {
110     device_cold_reset(DEVICE(cpu));
111 
112     trace_cpu_reset(cpu->cpu_index);
113 }
114 
115 static void cpu_common_reset_hold(Object *obj, ResetType type)
116 {
117     CPUState *cpu = CPU(obj);
118 
119     if (qemu_loglevel_mask(CPU_LOG_RESET)) {
120         qemu_log("CPU Reset (CPU %d)\n", cpu->cpu_index);
121         log_cpu_state(cpu, cpu->cc->reset_dump_flags);
122     }
123 
124     cpu->interrupt_request = 0;
125     cpu->halted = cpu->start_powered_off;
126     cpu->mem_io_pc = 0;
127     cpu->icount_extra = 0;
128     qatomic_set(&cpu->neg.icount_decr.u32, 0);
129     cpu->neg.can_do_io = true;
130     cpu->exception_index = -1;
131     cpu->crash_occurred = false;
132     cpu->cflags_next_tb = -1;
133 
134     cpu_exec_reset_hold(cpu);
135 }
136 
137 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
138 {
139     ObjectClass *oc;
140     CPUClass *cc;
141 
142     oc = object_class_by_name(typename);
143     cc = CPU_CLASS(oc);
144     assert(cc->class_by_name);
145     assert(cpu_model);
146     oc = cc->class_by_name(cpu_model);
147     if (object_class_dynamic_cast(oc, typename) &&
148         !object_class_is_abstract(oc)) {
149         return oc;
150     }
151 
152     return NULL;
153 }
154 
155 static void cpu_common_parse_features(const char *typename, char *features,
156                                       Error **errp)
157 {
158     char *val;
159     static bool cpu_globals_initialized;
160     /* Single "key=value" string being parsed */
161     char *featurestr = features ? strtok(features, ",") : NULL;
162 
163     /* should be called only once, catch invalid users */
164     assert(!cpu_globals_initialized);
165     cpu_globals_initialized = true;
166 
167     while (featurestr) {
168         val = strchr(featurestr, '=');
169         if (val) {
170             GlobalProperty *prop = g_new0(typeof(*prop), 1);
171             *val = 0;
172             val++;
173             prop->driver = typename;
174             prop->property = g_strdup(featurestr);
175             prop->value = g_strdup(val);
176             qdev_prop_register_global(prop);
177         } else {
178             error_setg(errp, "Expected key=value format, found %s.",
179                        featurestr);
180             return;
181         }
182         featurestr = strtok(NULL, ",");
183     }
184 }
185 
186 bool cpu_exec_realizefn(CPUState *cpu, Error **errp)
187 {
188     if (!accel_cpu_common_realize(cpu, errp)) {
189         return false;
190     }
191 
192     /* Wait until cpu initialization complete before exposing cpu. */
193     cpu_list_add(cpu);
194 
195     cpu_vmstate_register(cpu);
196 
197     return true;
198 }
199 
200 static void cpu_common_realizefn(DeviceState *dev, Error **errp)
201 {
202     CPUState *cpu = CPU(dev);
203     Object *machine = qdev_get_machine();
204 
205     /* qdev_get_machine() can return something that's not TYPE_MACHINE
206      * if this is one of the user-only emulators; in that case there's
207      * no need to check the ignore_memory_transaction_failures board flag.
208      */
209     if (object_dynamic_cast(machine, TYPE_MACHINE)) {
210         MachineClass *mc = MACHINE_GET_CLASS(machine);
211 
212         if (mc) {
213             cpu->ignore_memory_transaction_failures =
214                 mc->ignore_memory_transaction_failures;
215         }
216     }
217 
218     if (dev->hotplugged) {
219         cpu_synchronize_post_init(cpu);
220         cpu_resume(cpu);
221     }
222 
223     /* NOTE: latest generic point where the cpu is fully realized */
224 }
225 
226 static void cpu_common_unrealizefn(DeviceState *dev)
227 {
228     CPUState *cpu = CPU(dev);
229 
230     /* Call the plugin hook before clearing the cpu is fully unrealized */
231 #ifdef CONFIG_PLUGIN
232     if (tcg_enabled()) {
233         qemu_plugin_vcpu_exit_hook(cpu);
234     }
235 #endif
236 
237     /* NOTE: latest generic point before the cpu is fully unrealized */
238     cpu_exec_unrealizefn(cpu);
239 }
240 
241 void cpu_exec_unrealizefn(CPUState *cpu)
242 {
243     cpu_vmstate_unregister(cpu);
244 
245     cpu_list_remove(cpu);
246     /*
247      * Now that the vCPU has been removed from the RCU list, we can call
248      * accel_cpu_common_unrealize, which may free fields using call_rcu.
249      */
250     accel_cpu_common_unrealize(cpu);
251 }
252 
253 static void cpu_common_initfn(Object *obj)
254 {
255     CPUState *cpu = CPU(obj);
256 
257     cpu_exec_class_post_init(CPU_GET_CLASS(obj));
258 
259     /* cache the cpu class for the hotpath */
260     cpu->cc = CPU_GET_CLASS(cpu);
261 
262     gdb_init_cpu(cpu);
263     cpu->cpu_index = UNASSIGNED_CPU_INDEX;
264     cpu->cluster_index = UNASSIGNED_CLUSTER_INDEX;
265     cpu->as = NULL;
266     cpu->num_ases = 0;
267     /* user-mode doesn't have configurable SMP topology */
268     /* the default value is changed by qemu_init_vcpu() for system-mode */
269     cpu->nr_threads = 1;
270 
271     /* allocate storage for thread info, initialise condition variables */
272     cpu->thread = g_new0(QemuThread, 1);
273     cpu->halt_cond = g_new0(QemuCond, 1);
274     qemu_cond_init(cpu->halt_cond);
275 
276     qemu_mutex_init(&cpu->work_mutex);
277     qemu_lockcnt_init(&cpu->in_ioctl_lock);
278     QSIMPLEQ_INIT(&cpu->work_list);
279     QTAILQ_INIT(&cpu->breakpoints);
280     QTAILQ_INIT(&cpu->watchpoints);
281 
282     cpu_exec_initfn(cpu);
283 
284     /*
285      * Plugin initialization must wait until the cpu start executing
286      * code, but we must queue this work before the threads are
287      * created to ensure we don't race.
288      */
289 #ifdef CONFIG_PLUGIN
290     if (tcg_enabled()) {
291         cpu->plugin_state = qemu_plugin_create_vcpu_state();
292         qemu_plugin_vcpu_init_hook(cpu);
293     }
294 #endif
295 }
296 
297 static void cpu_common_finalize(Object *obj)
298 {
299     CPUState *cpu = CPU(obj);
300 
301 #ifdef CONFIG_PLUGIN
302     if (tcg_enabled()) {
303         g_free(cpu->plugin_state);
304     }
305 #endif
306     free_queued_cpu_work(cpu);
307     /* If cleanup didn't happen in context to gdb_unregister_coprocessor_all */
308     if (cpu->gdb_regs) {
309         g_array_free(cpu->gdb_regs, TRUE);
310     }
311     qemu_lockcnt_destroy(&cpu->in_ioctl_lock);
312     qemu_mutex_destroy(&cpu->work_mutex);
313     qemu_cond_destroy(cpu->halt_cond);
314     g_free(cpu->halt_cond);
315     g_free(cpu->thread);
316 }
317 
318 static int64_t cpu_common_get_arch_id(CPUState *cpu)
319 {
320     return cpu->cpu_index;
321 }
322 
323 static void cpu_common_class_init(ObjectClass *klass, void *data)
324 {
325     DeviceClass *dc = DEVICE_CLASS(klass);
326     ResettableClass *rc = RESETTABLE_CLASS(klass);
327     CPUClass *k = CPU_CLASS(klass);
328 
329     k->parse_features = cpu_common_parse_features;
330     k->get_arch_id = cpu_common_get_arch_id;
331     k->gdb_read_register = cpu_common_gdb_read_register;
332     k->gdb_write_register = cpu_common_gdb_write_register;
333     set_bit(DEVICE_CATEGORY_CPU, dc->categories);
334     dc->realize = cpu_common_realizefn;
335     dc->unrealize = cpu_common_unrealizefn;
336     rc->phases.hold = cpu_common_reset_hold;
337     cpu_class_init_props(dc);
338     /*
339      * Reason: CPUs still need special care by board code: wiring up
340      * IRQs, adding reset handlers, halting non-first CPUs, ...
341      */
342     dc->user_creatable = false;
343 }
344 
345 static const TypeInfo cpu_type_info = {
346     .name = TYPE_CPU,
347     .parent = TYPE_DEVICE,
348     .instance_size = sizeof(CPUState),
349     .instance_init = cpu_common_initfn,
350     .instance_finalize = cpu_common_finalize,
351     .abstract = true,
352     .class_size = sizeof(CPUClass),
353     .class_init = cpu_common_class_init,
354 };
355 
356 static void cpu_register_types(void)
357 {
358     type_register_static(&cpu_type_info);
359 }
360 
361 type_init(cpu_register_types)
362