4 * Copyright (c) 2012-2014 SUSE LINUX Products GmbH
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.
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.
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>
21 #include "qemu/osdep.h"
22 #include "qapi/error.h"
23 #include "hw/core/cpu.h"
24 #include "sysemu/hw_accel.h"
26 #include "qemu/main-loop.h"
28 #include "exec/gdbstub.h"
29 #include "sysemu/tcg.h"
30 #include "hw/boards.h"
31 #include "hw/qdev-properties.h"
33 #include "qemu/plugin.h"
35 CPUState
*cpu_by_arch_id(int64_t id
)
40 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
42 if (cc
->get_arch_id(cpu
) == id
) {
49 bool cpu_exists(int64_t id
)
51 return !!cpu_by_arch_id(id
);
54 CPUState
*cpu_create(const char *typename
)
57 CPUState
*cpu
= CPU(object_new(typename
));
58 if (!qdev_realize(DEVICE(cpu
), NULL
, &err
)) {
59 error_report_err(err
);
60 object_unref(OBJECT(cpu
));
66 /* Resetting the IRQ comes from across the code base so we take the
67 * BQL here if we need to. cpu_interrupt assumes it is held.*/
68 void cpu_reset_interrupt(CPUState
*cpu
, int mask
)
70 bool need_lock
= !bql_locked();
75 cpu
->interrupt_request
&= ~mask
;
81 void cpu_exit(CPUState
*cpu
)
83 qatomic_set(&cpu
->exit_request
, 1);
84 /* Ensure cpu_exec will see the exit request after TCG has exited. */
86 qatomic_set(&cpu
->neg
.icount_decr
.u16
.high
, -1);
89 static int cpu_common_gdb_read_register(CPUState
*cpu
, GByteArray
*buf
, int reg
)
94 static int cpu_common_gdb_write_register(CPUState
*cpu
, uint8_t *buf
, int reg
)
99 void cpu_dump_state(CPUState
*cpu
, FILE *f
, int flags
)
101 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
103 if (cc
->dump_state
) {
104 cpu_synchronize_state(cpu
);
105 cc
->dump_state(cpu
, f
, flags
);
109 void cpu_reset(CPUState
*cpu
)
111 device_cold_reset(DEVICE(cpu
));
113 trace_cpu_reset(cpu
->cpu_index
);
116 static void cpu_common_reset_hold(Object
*obj
)
118 CPUState
*cpu
= CPU(obj
);
119 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
121 if (qemu_loglevel_mask(CPU_LOG_RESET
)) {
122 qemu_log("CPU Reset (CPU %d)\n", cpu
->cpu_index
);
123 log_cpu_state(cpu
, cc
->reset_dump_flags
);
126 cpu
->interrupt_request
= 0;
127 cpu
->halted
= cpu
->start_powered_off
;
129 cpu
->icount_extra
= 0;
130 qatomic_set(&cpu
->neg
.icount_decr
.u32
, 0);
131 cpu
->neg
.can_do_io
= true;
132 cpu
->exception_index
= -1;
133 cpu
->crash_occurred
= false;
134 cpu
->cflags_next_tb
= -1;
136 cpu_exec_reset_hold(cpu
);
139 static bool cpu_common_has_work(CPUState
*cs
)
144 ObjectClass
*cpu_class_by_name(const char *typename
, const char *cpu_model
)
149 oc
= object_class_by_name(typename
);
151 assert(cc
->class_by_name
);
153 oc
= cc
->class_by_name(cpu_model
);
154 if (object_class_dynamic_cast(oc
, typename
) &&
155 !object_class_is_abstract(oc
)) {
162 static void cpu_common_parse_features(const char *typename
, char *features
,
166 static bool cpu_globals_initialized
;
167 /* Single "key=value" string being parsed */
168 char *featurestr
= features
? strtok(features
, ",") : NULL
;
170 /* should be called only once, catch invalid users */
171 assert(!cpu_globals_initialized
);
172 cpu_globals_initialized
= true;
175 val
= strchr(featurestr
, '=');
177 GlobalProperty
*prop
= g_new0(typeof(*prop
), 1);
180 prop
->driver
= typename
;
181 prop
->property
= g_strdup(featurestr
);
182 prop
->value
= g_strdup(val
);
183 qdev_prop_register_global(prop
);
185 error_setg(errp
, "Expected key=value format, found %s.",
189 featurestr
= strtok(NULL
, ",");
194 static void qemu_plugin_vcpu_init__async(CPUState
*cpu
, run_on_cpu_data unused
)
196 qemu_plugin_vcpu_init_hook(cpu
);
200 static void cpu_common_realizefn(DeviceState
*dev
, Error
**errp
)
202 CPUState
*cpu
= CPU(dev
);
203 Object
*machine
= qdev_get_machine();
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.
209 if (object_dynamic_cast(machine
, TYPE_MACHINE
)) {
210 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
213 cpu
->ignore_memory_transaction_failures
=
214 mc
->ignore_memory_transaction_failures
;
218 if (dev
->hotplugged
) {
219 cpu_synchronize_post_init(cpu
);
223 /* Plugin initialization must wait until the cpu start executing code */
226 cpu
->plugin_state
= qemu_plugin_create_vcpu_state();
227 async_run_on_cpu(cpu
, qemu_plugin_vcpu_init__async
, RUN_ON_CPU_NULL
);
231 /* NOTE: latest generic point where the cpu is fully realized */
234 static void cpu_common_unrealizefn(DeviceState
*dev
)
236 CPUState
*cpu
= CPU(dev
);
238 /* Call the plugin hook before clearing the cpu is fully unrealized */
240 qemu_plugin_vcpu_exit_hook(cpu
);
243 /* NOTE: latest generic point before the cpu is fully unrealized */
244 cpu_exec_unrealizefn(cpu
);
247 static void cpu_common_initfn(Object
*obj
)
249 CPUState
*cpu
= CPU(obj
);
252 cpu
->cpu_index
= UNASSIGNED_CPU_INDEX
;
253 cpu
->cluster_index
= UNASSIGNED_CLUSTER_INDEX
;
254 /* user-mode doesn't have configurable SMP topology */
255 /* the default value is changed by qemu_init_vcpu() for system-mode */
258 cpu
->cflags_next_tb
= -1;
260 qemu_mutex_init(&cpu
->work_mutex
);
261 qemu_lockcnt_init(&cpu
->in_ioctl_lock
);
262 QSIMPLEQ_INIT(&cpu
->work_list
);
263 QTAILQ_INIT(&cpu
->breakpoints
);
264 QTAILQ_INIT(&cpu
->watchpoints
);
266 cpu_exec_initfn(cpu
);
269 static void cpu_common_finalize(Object
*obj
)
271 CPUState
*cpu
= CPU(obj
);
273 g_array_free(cpu
->gdb_regs
, TRUE
);
274 qemu_lockcnt_destroy(&cpu
->in_ioctl_lock
);
275 qemu_mutex_destroy(&cpu
->work_mutex
);
278 static int64_t cpu_common_get_arch_id(CPUState
*cpu
)
280 return cpu
->cpu_index
;
283 static void cpu_common_class_init(ObjectClass
*klass
, void *data
)
285 DeviceClass
*dc
= DEVICE_CLASS(klass
);
286 ResettableClass
*rc
= RESETTABLE_CLASS(klass
);
287 CPUClass
*k
= CPU_CLASS(klass
);
289 k
->parse_features
= cpu_common_parse_features
;
290 k
->get_arch_id
= cpu_common_get_arch_id
;
291 k
->has_work
= cpu_common_has_work
;
292 k
->gdb_read_register
= cpu_common_gdb_read_register
;
293 k
->gdb_write_register
= cpu_common_gdb_write_register
;
294 set_bit(DEVICE_CATEGORY_CPU
, dc
->categories
);
295 dc
->realize
= cpu_common_realizefn
;
296 dc
->unrealize
= cpu_common_unrealizefn
;
297 rc
->phases
.hold
= cpu_common_reset_hold
;
298 cpu_class_init_props(dc
);
300 * Reason: CPUs still need special care by board code: wiring up
301 * IRQs, adding reset handlers, halting non-first CPUs, ...
303 dc
->user_creatable
= false;
306 static const TypeInfo cpu_type_info
= {
308 .parent
= TYPE_DEVICE
,
309 .instance_size
= sizeof(CPUState
),
310 .instance_init
= cpu_common_initfn
,
311 .instance_finalize
= cpu_common_finalize
,
313 .class_size
= sizeof(CPUClass
),
314 .class_init
= cpu_common_class_init
,
317 static void cpu_register_types(void)
319 type_register_static(&cpu_type_info
);
322 type_init(cpu_register_types
)