xref: /qemu/hw/core/cpu-common.c (revision 4a119cfc6cd7affc07d4b76c1340cf96b6ff0268)
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 static bool cpu_common_has_work(CPUState *cs)
138 {
139     return false;
140 }
141 
142 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
143 {
144     ObjectClass *oc;
145     CPUClass *cc;
146 
147     oc = object_class_by_name(typename);
148     cc = CPU_CLASS(oc);
149     assert(cc->class_by_name);
150     assert(cpu_model);
151     oc = cc->class_by_name(cpu_model);
152     if (object_class_dynamic_cast(oc, typename) &&
153         !object_class_is_abstract(oc)) {
154         return oc;
155     }
156 
157     return NULL;
158 }
159 
160 static void cpu_common_parse_features(const char *typename, char *features,
161                                       Error **errp)
162 {
163     char *val;
164     static bool cpu_globals_initialized;
165     /* Single "key=value" string being parsed */
166     char *featurestr = features ? strtok(features, ",") : NULL;
167 
168     /* should be called only once, catch invalid users */
169     assert(!cpu_globals_initialized);
170     cpu_globals_initialized = true;
171 
172     while (featurestr) {
173         val = strchr(featurestr, '=');
174         if (val) {
175             GlobalProperty *prop = g_new0(typeof(*prop), 1);
176             *val = 0;
177             val++;
178             prop->driver = typename;
179             prop->property = g_strdup(featurestr);
180             prop->value = g_strdup(val);
181             qdev_prop_register_global(prop);
182         } else {
183             error_setg(errp, "Expected key=value format, found %s.",
184                        featurestr);
185             return;
186         }
187         featurestr = strtok(NULL, ",");
188     }
189 }
190 
191 bool cpu_exec_realizefn(CPUState *cpu, Error **errp)
192 {
193     if (!accel_cpu_common_realize(cpu, errp)) {
194         return false;
195     }
196 
197     /* Wait until cpu initialization complete before exposing cpu. */
198     cpu_list_add(cpu);
199 
200     cpu_vmstate_register(cpu);
201 
202     return true;
203 }
204 
205 static void cpu_common_realizefn(DeviceState *dev, Error **errp)
206 {
207     CPUState *cpu = CPU(dev);
208     Object *machine = qdev_get_machine();
209 
210     /* qdev_get_machine() can return something that's not TYPE_MACHINE
211      * if this is one of the user-only emulators; in that case there's
212      * no need to check the ignore_memory_transaction_failures board flag.
213      */
214     if (object_dynamic_cast(machine, TYPE_MACHINE)) {
215         MachineClass *mc = MACHINE_GET_CLASS(machine);
216 
217         if (mc) {
218             cpu->ignore_memory_transaction_failures =
219                 mc->ignore_memory_transaction_failures;
220         }
221     }
222 
223     if (dev->hotplugged) {
224         cpu_synchronize_post_init(cpu);
225         cpu_resume(cpu);
226     }
227 
228     /* NOTE: latest generic point where the cpu is fully realized */
229 }
230 
231 static void cpu_common_unrealizefn(DeviceState *dev)
232 {
233     CPUState *cpu = CPU(dev);
234 
235     /* Call the plugin hook before clearing the cpu is fully unrealized */
236 #ifdef CONFIG_PLUGIN
237     if (tcg_enabled()) {
238         qemu_plugin_vcpu_exit_hook(cpu);
239     }
240 #endif
241 
242     /* NOTE: latest generic point before the cpu is fully unrealized */
243     cpu_exec_unrealizefn(cpu);
244 }
245 
246 void cpu_exec_unrealizefn(CPUState *cpu)
247 {
248     cpu_vmstate_unregister(cpu);
249 
250     cpu_list_remove(cpu);
251     /*
252      * Now that the vCPU has been removed from the RCU list, we can call
253      * accel_cpu_common_unrealize, which may free fields using call_rcu.
254      */
255     accel_cpu_common_unrealize(cpu);
256 }
257 
258 static void cpu_common_initfn(Object *obj)
259 {
260     CPUState *cpu = CPU(obj);
261 
262     /* cache the cpu class for the hotpath */
263     cpu->cc = CPU_GET_CLASS(cpu);
264 
265     gdb_init_cpu(cpu);
266     cpu->cpu_index = UNASSIGNED_CPU_INDEX;
267     cpu->cluster_index = UNASSIGNED_CLUSTER_INDEX;
268     cpu->as = NULL;
269     cpu->num_ases = 0;
270     /* user-mode doesn't have configurable SMP topology */
271     /* the default value is changed by qemu_init_vcpu() for system-mode */
272     cpu->nr_threads = 1;
273 
274     /* allocate storage for thread info, initialise condition variables */
275     cpu->thread = g_new0(QemuThread, 1);
276     cpu->halt_cond = g_new0(QemuCond, 1);
277     qemu_cond_init(cpu->halt_cond);
278 
279     qemu_mutex_init(&cpu->work_mutex);
280     qemu_lockcnt_init(&cpu->in_ioctl_lock);
281     QSIMPLEQ_INIT(&cpu->work_list);
282     QTAILQ_INIT(&cpu->breakpoints);
283     QTAILQ_INIT(&cpu->watchpoints);
284 
285     cpu_exec_initfn(cpu);
286 
287     /*
288      * Plugin initialization must wait until the cpu start executing
289      * code, but we must queue this work before the threads are
290      * created to ensure we don't race.
291      */
292 #ifdef CONFIG_PLUGIN
293     if (tcg_enabled()) {
294         cpu->plugin_state = qemu_plugin_create_vcpu_state();
295         qemu_plugin_vcpu_init_hook(cpu);
296     }
297 #endif
298 }
299 
300 static void cpu_common_finalize(Object *obj)
301 {
302     CPUState *cpu = CPU(obj);
303 
304 #ifdef CONFIG_PLUGIN
305     if (tcg_enabled()) {
306         g_free(cpu->plugin_state);
307     }
308 #endif
309     free_queued_cpu_work(cpu);
310     /* If cleanup didn't happen in context to gdb_unregister_coprocessor_all */
311     if (cpu->gdb_regs) {
312         g_array_free(cpu->gdb_regs, TRUE);
313     }
314     qemu_lockcnt_destroy(&cpu->in_ioctl_lock);
315     qemu_mutex_destroy(&cpu->work_mutex);
316     qemu_cond_destroy(cpu->halt_cond);
317     g_free(cpu->halt_cond);
318     g_free(cpu->thread);
319 }
320 
321 static int64_t cpu_common_get_arch_id(CPUState *cpu)
322 {
323     return cpu->cpu_index;
324 }
325 
326 static void cpu_common_class_init(ObjectClass *klass, void *data)
327 {
328     DeviceClass *dc = DEVICE_CLASS(klass);
329     ResettableClass *rc = RESETTABLE_CLASS(klass);
330     CPUClass *k = CPU_CLASS(klass);
331 
332     k->parse_features = cpu_common_parse_features;
333     k->get_arch_id = cpu_common_get_arch_id;
334     k->has_work = cpu_common_has_work;
335     k->gdb_read_register = cpu_common_gdb_read_register;
336     k->gdb_write_register = cpu_common_gdb_write_register;
337     set_bit(DEVICE_CATEGORY_CPU, dc->categories);
338     dc->realize = cpu_common_realizefn;
339     dc->unrealize = cpu_common_unrealizefn;
340     rc->phases.hold = cpu_common_reset_hold;
341     cpu_class_init_props(dc);
342     /*
343      * Reason: CPUs still need special care by board code: wiring up
344      * IRQs, adding reset handlers, halting non-first CPUs, ...
345      */
346     dc->user_creatable = false;
347 }
348 
349 static const TypeInfo cpu_type_info = {
350     .name = TYPE_CPU,
351     .parent = TYPE_DEVICE,
352     .instance_size = sizeof(CPUState),
353     .instance_init = cpu_common_initfn,
354     .instance_finalize = cpu_common_finalize,
355     .abstract = true,
356     .class_size = sizeof(CPUClass),
357     .class_init = cpu_common_class_init,
358 };
359 
360 static void cpu_register_types(void)
361 {
362     type_register_static(&cpu_type_info);
363 }
364 
365 type_init(cpu_register_types)
366