2 * Target-specific parts of the CPU object
4 * Copyright (c) 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "qemu-common.h"
22 #include "qapi/error.h"
24 #include "exec/target_page.h"
25 #include "hw/qdev-core.h"
26 #include "hw/qdev-properties.h"
27 #include "qemu/error-report.h"
28 #include "migration/vmstate.h"
29 #ifdef CONFIG_USER_ONLY
32 #include "hw/core/sysemu-cpu-ops.h"
33 #include "exec/address-spaces.h"
35 #include "sysemu/tcg.h"
36 #include "sysemu/kvm.h"
37 #include "sysemu/replay.h"
38 #include "exec/cpu-common.h"
39 #include "exec/exec-all.h"
40 #include "exec/translate-all.h"
42 #include "hw/core/accel-cpu.h"
43 #include "trace/trace-root.h"
44 #include "qemu/accel.h"
46 uintptr_t qemu_host_page_size
;
47 intptr_t qemu_host_page_mask
;
49 #ifndef CONFIG_USER_ONLY
50 static int cpu_common_post_load(void *opaque
, int version_id
)
52 CPUState
*cpu
= opaque
;
54 /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
55 version_id is increased. */
56 cpu
->interrupt_request
&= ~0x01;
59 /* loadvm has just updated the content of RAM, bypassing the
60 * usual mechanisms that ensure we flush TBs for writes to
61 * memory we've translated code from. So we must flush all TBs,
62 * which will now be stale.
69 static int cpu_common_pre_load(void *opaque
)
71 CPUState
*cpu
= opaque
;
73 cpu
->exception_index
= -1;
78 static bool cpu_common_exception_index_needed(void *opaque
)
80 CPUState
*cpu
= opaque
;
82 return tcg_enabled() && cpu
->exception_index
!= -1;
85 static const VMStateDescription vmstate_cpu_common_exception_index
= {
86 .name
= "cpu_common/exception_index",
88 .minimum_version_id
= 1,
89 .needed
= cpu_common_exception_index_needed
,
90 .fields
= (VMStateField
[]) {
91 VMSTATE_INT32(exception_index
, CPUState
),
96 static bool cpu_common_crash_occurred_needed(void *opaque
)
98 CPUState
*cpu
= opaque
;
100 return cpu
->crash_occurred
;
103 static const VMStateDescription vmstate_cpu_common_crash_occurred
= {
104 .name
= "cpu_common/crash_occurred",
106 .minimum_version_id
= 1,
107 .needed
= cpu_common_crash_occurred_needed
,
108 .fields
= (VMStateField
[]) {
109 VMSTATE_BOOL(crash_occurred
, CPUState
),
110 VMSTATE_END_OF_LIST()
114 const VMStateDescription vmstate_cpu_common
= {
115 .name
= "cpu_common",
117 .minimum_version_id
= 1,
118 .pre_load
= cpu_common_pre_load
,
119 .post_load
= cpu_common_post_load
,
120 .fields
= (VMStateField
[]) {
121 VMSTATE_UINT32(halted
, CPUState
),
122 VMSTATE_UINT32(interrupt_request
, CPUState
),
123 VMSTATE_END_OF_LIST()
125 .subsections
= (const VMStateDescription
*[]) {
126 &vmstate_cpu_common_exception_index
,
127 &vmstate_cpu_common_crash_occurred
,
133 void cpu_exec_realizefn(CPUState
*cpu
, Error
**errp
)
135 #ifndef CONFIG_USER_ONLY
136 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
140 if (!accel_cpu_realizefn(cpu
, errp
)) {
143 /* NB: errp parameter is unused currently */
145 tcg_exec_realizefn(cpu
, errp
);
148 #ifdef CONFIG_USER_ONLY
149 assert(qdev_get_vmsd(DEVICE(cpu
)) == NULL
||
150 qdev_get_vmsd(DEVICE(cpu
))->unmigratable
);
152 if (qdev_get_vmsd(DEVICE(cpu
)) == NULL
) {
153 vmstate_register(NULL
, cpu
->cpu_index
, &vmstate_cpu_common
, cpu
);
155 if (cc
->sysemu_ops
->legacy_vmsd
!= NULL
) {
156 vmstate_register(NULL
, cpu
->cpu_index
, cc
->sysemu_ops
->legacy_vmsd
, cpu
);
158 #endif /* CONFIG_USER_ONLY */
161 void cpu_exec_unrealizefn(CPUState
*cpu
)
163 #ifndef CONFIG_USER_ONLY
164 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
166 if (cc
->sysemu_ops
->legacy_vmsd
!= NULL
) {
167 vmstate_unregister(NULL
, cc
->sysemu_ops
->legacy_vmsd
, cpu
);
169 if (qdev_get_vmsd(DEVICE(cpu
)) == NULL
) {
170 vmstate_unregister(NULL
, &vmstate_cpu_common
, cpu
);
174 tcg_exec_unrealizefn(cpu
);
177 cpu_list_remove(cpu
);
181 * This can't go in hw/core/cpu.c because that file is compiled only
182 * once for both user-mode and system builds.
184 static Property cpu_common_props
[] = {
185 #ifdef CONFIG_USER_ONLY
187 * Create a property for the user-only object, so users can
188 * adjust prctl(PR_SET_UNALIGN) from the command-line.
189 * Has no effect if the target does not support the feature.
191 DEFINE_PROP_BOOL("prctl-unalign-sigbus", CPUState
,
192 prctl_unalign_sigbus
, false),
195 * Create a memory property for softmmu CPU object, so users can
196 * wire up its memory. The default if no link is set up is to use
197 * the system address space.
199 DEFINE_PROP_LINK("memory", CPUState
, memory
, TYPE_MEMORY_REGION
,
202 DEFINE_PROP_END_OF_LIST(),
205 static bool cpu_get_start_powered_off(Object
*obj
, Error
**errp
)
207 CPUState
*cpu
= CPU(obj
);
208 return cpu
->start_powered_off
;
211 static void cpu_set_start_powered_off(Object
*obj
, bool value
, Error
**errp
)
213 CPUState
*cpu
= CPU(obj
);
214 cpu
->start_powered_off
= value
;
217 void cpu_class_init_props(DeviceClass
*dc
)
219 ObjectClass
*oc
= OBJECT_CLASS(dc
);
221 device_class_set_props(dc
, cpu_common_props
);
223 * We can't use DEFINE_PROP_BOOL in the Property array for this
224 * property, because we want this to be settable after realize.
226 object_class_property_add_bool(oc
, "start-powered-off",
227 cpu_get_start_powered_off
,
228 cpu_set_start_powered_off
);
231 void cpu_exec_initfn(CPUState
*cpu
)
236 #ifndef CONFIG_USER_ONLY
237 cpu
->thread_id
= qemu_get_thread_id();
238 cpu
->memory
= get_system_memory();
239 object_ref(OBJECT(cpu
->memory
));
243 const char *parse_cpu_option(const char *cpu_option
)
247 gchar
**model_pieces
;
248 const char *cpu_type
;
250 model_pieces
= g_strsplit(cpu_option
, ",", 2);
251 if (!model_pieces
[0]) {
252 error_report("-cpu option cannot be empty");
256 oc
= cpu_class_by_name(CPU_RESOLVING_TYPE
, model_pieces
[0]);
258 error_report("unable to find CPU model '%s'", model_pieces
[0]);
259 g_strfreev(model_pieces
);
263 cpu_type
= object_class_get_name(oc
);
265 cc
->parse_features(cpu_type
, model_pieces
[1], &error_fatal
);
266 g_strfreev(model_pieces
);
270 void list_cpus(const char *optarg
)
272 /* XXX: implement xxx_cpu_list for targets that still miss it */
273 #if defined(cpu_list)
278 #if defined(CONFIG_USER_ONLY)
279 void tb_invalidate_phys_addr(target_ulong addr
)
282 tb_invalidate_phys_page_range(addr
, addr
+ 1);
286 void tb_invalidate_phys_addr(AddressSpace
*as
, hwaddr addr
, MemTxAttrs attrs
)
292 if (!tcg_enabled()) {
296 RCU_READ_LOCK_GUARD();
297 mr
= address_space_translate(as
, addr
, &addr
, &l
, false, attrs
);
298 if (!(memory_region_is_ram(mr
)
299 || memory_region_is_romd(mr
))) {
302 ram_addr
= memory_region_get_ram_addr(mr
) + addr
;
303 tb_invalidate_phys_page_range(ram_addr
, ram_addr
+ 1);
307 /* Add a breakpoint. */
308 int cpu_breakpoint_insert(CPUState
*cpu
, vaddr pc
, int flags
,
309 CPUBreakpoint
**breakpoint
)
311 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
314 if (cc
->gdb_adjust_breakpoint
) {
315 pc
= cc
->gdb_adjust_breakpoint(cpu
, pc
);
318 bp
= g_malloc(sizeof(*bp
));
323 /* keep all GDB-injected breakpoints in front */
324 if (flags
& BP_GDB
) {
325 QTAILQ_INSERT_HEAD(&cpu
->breakpoints
, bp
, entry
);
327 QTAILQ_INSERT_TAIL(&cpu
->breakpoints
, bp
, entry
);
334 trace_breakpoint_insert(cpu
->cpu_index
, pc
, flags
);
338 /* Remove a specific breakpoint. */
339 int cpu_breakpoint_remove(CPUState
*cpu
, vaddr pc
, int flags
)
341 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
344 if (cc
->gdb_adjust_breakpoint
) {
345 pc
= cc
->gdb_adjust_breakpoint(cpu
, pc
);
348 QTAILQ_FOREACH(bp
, &cpu
->breakpoints
, entry
) {
349 if (bp
->pc
== pc
&& bp
->flags
== flags
) {
350 cpu_breakpoint_remove_by_ref(cpu
, bp
);
357 /* Remove a specific breakpoint by reference. */
358 void cpu_breakpoint_remove_by_ref(CPUState
*cpu
, CPUBreakpoint
*bp
)
360 QTAILQ_REMOVE(&cpu
->breakpoints
, bp
, entry
);
362 trace_breakpoint_remove(cpu
->cpu_index
, bp
->pc
, bp
->flags
);
366 /* Remove all matching breakpoints. */
367 void cpu_breakpoint_remove_all(CPUState
*cpu
, int mask
)
369 CPUBreakpoint
*bp
, *next
;
371 QTAILQ_FOREACH_SAFE(bp
, &cpu
->breakpoints
, entry
, next
) {
372 if (bp
->flags
& mask
) {
373 cpu_breakpoint_remove_by_ref(cpu
, bp
);
378 /* enable or disable single step mode. EXCP_DEBUG is returned by the
379 CPU loop after each instruction */
380 void cpu_single_step(CPUState
*cpu
, int enabled
)
382 if (cpu
->singlestep_enabled
!= enabled
) {
383 cpu
->singlestep_enabled
= enabled
;
385 kvm_update_guest_debug(cpu
, 0);
387 trace_breakpoint_singlestep(cpu
->cpu_index
, enabled
);
391 void cpu_abort(CPUState
*cpu
, const char *fmt
, ...)
398 fprintf(stderr
, "qemu: fatal: ");
399 vfprintf(stderr
, fmt
, ap
);
400 fprintf(stderr
, "\n");
401 cpu_dump_state(cpu
, stderr
, CPU_DUMP_FPU
| CPU_DUMP_CCOP
);
402 if (qemu_log_separate()) {
403 FILE *logfile
= qemu_log_lock();
404 qemu_log("qemu: fatal: ");
405 qemu_log_vprintf(fmt
, ap2
);
407 log_cpu_state(cpu
, CPU_DUMP_FPU
| CPU_DUMP_CCOP
);
409 qemu_log_unlock(logfile
);
415 #if defined(CONFIG_USER_ONLY)
417 struct sigaction act
;
418 sigfillset(&act
.sa_mask
);
419 act
.sa_handler
= SIG_DFL
;
421 sigaction(SIGABRT
, &act
, NULL
);
427 /* physical memory access (slow version, mainly for debug) */
428 #if defined(CONFIG_USER_ONLY)
429 int cpu_memory_rw_debug(CPUState
*cpu
, vaddr addr
,
430 void *ptr
, size_t len
, bool is_write
)
438 page
= addr
& TARGET_PAGE_MASK
;
439 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
442 flags
= page_get_flags(page
);
443 if (!(flags
& PAGE_VALID
))
446 if (!(flags
& PAGE_WRITE
))
448 /* XXX: this code should not depend on lock_user */
449 if (!(p
= lock_user(VERIFY_WRITE
, addr
, l
, 0)))
452 unlock_user(p
, addr
, l
);
454 if (!(flags
& PAGE_READ
))
456 /* XXX: this code should not depend on lock_user */
457 if (!(p
= lock_user(VERIFY_READ
, addr
, l
, 1)))
460 unlock_user(p
, addr
, 0);
470 bool target_words_bigendian(void)
472 #if defined(TARGET_WORDS_BIGENDIAN)
479 void page_size_init(void)
481 /* NOTE: we can always suppose that qemu_host_page_size >=
483 if (qemu_host_page_size
== 0) {
484 qemu_host_page_size
= qemu_real_host_page_size
;
486 if (qemu_host_page_size
< TARGET_PAGE_SIZE
) {
487 qemu_host_page_size
= TARGET_PAGE_SIZE
;
489 qemu_host_page_mask
= -(intptr_t)qemu_host_page_size
;