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 "exec/address-spaces.h"
34 #include "sysemu/tcg.h"
35 #include "sysemu/kvm.h"
36 #include "sysemu/replay.h"
37 #include "translate-all.h"
40 uintptr_t qemu_host_page_size
;
41 intptr_t qemu_host_page_mask
;
43 #ifndef CONFIG_USER_ONLY
44 static int cpu_common_post_load(void *opaque
, int version_id
)
46 CPUState
*cpu
= opaque
;
48 /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
49 version_id is increased. */
50 cpu
->interrupt_request
&= ~0x01;
53 /* loadvm has just updated the content of RAM, bypassing the
54 * usual mechanisms that ensure we flush TBs for writes to
55 * memory we've translated code from. So we must flush all TBs,
56 * which will now be stale.
63 static int cpu_common_pre_load(void *opaque
)
65 CPUState
*cpu
= opaque
;
67 cpu
->exception_index
= -1;
72 static bool cpu_common_exception_index_needed(void *opaque
)
74 CPUState
*cpu
= opaque
;
76 return tcg_enabled() && cpu
->exception_index
!= -1;
79 static const VMStateDescription vmstate_cpu_common_exception_index
= {
80 .name
= "cpu_common/exception_index",
82 .minimum_version_id
= 1,
83 .needed
= cpu_common_exception_index_needed
,
84 .fields
= (VMStateField
[]) {
85 VMSTATE_INT32(exception_index
, CPUState
),
90 static bool cpu_common_crash_occurred_needed(void *opaque
)
92 CPUState
*cpu
= opaque
;
94 return cpu
->crash_occurred
;
97 static const VMStateDescription vmstate_cpu_common_crash_occurred
= {
98 .name
= "cpu_common/crash_occurred",
100 .minimum_version_id
= 1,
101 .needed
= cpu_common_crash_occurred_needed
,
102 .fields
= (VMStateField
[]) {
103 VMSTATE_BOOL(crash_occurred
, CPUState
),
104 VMSTATE_END_OF_LIST()
108 const VMStateDescription vmstate_cpu_common
= {
109 .name
= "cpu_common",
111 .minimum_version_id
= 1,
112 .pre_load
= cpu_common_pre_load
,
113 .post_load
= cpu_common_post_load
,
114 .fields
= (VMStateField
[]) {
115 VMSTATE_UINT32(halted
, CPUState
),
116 VMSTATE_UINT32(interrupt_request
, CPUState
),
117 VMSTATE_END_OF_LIST()
119 .subsections
= (const VMStateDescription
*[]) {
120 &vmstate_cpu_common_exception_index
,
121 &vmstate_cpu_common_crash_occurred
,
127 void cpu_exec_unrealizefn(CPUState
*cpu
)
129 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
132 cpu_list_remove(cpu
);
134 #ifdef CONFIG_USER_ONLY
135 assert(cc
->vmsd
== NULL
);
137 if (cc
->vmsd
!= NULL
) {
138 vmstate_unregister(NULL
, cc
->vmsd
, cpu
);
140 if (qdev_get_vmsd(DEVICE(cpu
)) == NULL
) {
141 vmstate_unregister(NULL
, &vmstate_cpu_common
, cpu
);
143 tcg_iommu_free_notifier_list(cpu
);
147 Property cpu_common_props
[] = {
148 #ifndef CONFIG_USER_ONLY
149 /* Create a memory property for softmmu CPU object,
150 * so users can wire up its memory. (This can't go in hw/core/cpu.c
151 * because that file is compiled only once for both user-mode
152 * and system builds.) The default if no link is set up is to use
153 * the system address space.
155 DEFINE_PROP_LINK("memory", CPUState
, memory
, TYPE_MEMORY_REGION
,
158 DEFINE_PROP_BOOL("start-powered-off", CPUState
, start_powered_off
, false),
159 DEFINE_PROP_END_OF_LIST(),
162 void cpu_exec_initfn(CPUState
*cpu
)
167 #ifndef CONFIG_USER_ONLY
168 cpu
->thread_id
= qemu_get_thread_id();
169 cpu
->memory
= get_system_memory();
170 object_ref(OBJECT(cpu
->memory
));
174 void cpu_exec_realizefn(CPUState
*cpu
, Error
**errp
)
176 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
177 static bool tcg_target_initialized
;
181 if (tcg_enabled() && !tcg_target_initialized
) {
182 tcg_target_initialized
= true;
183 cc
->tcg_initialize();
187 qemu_plugin_vcpu_init_hook(cpu
);
189 #ifdef CONFIG_USER_ONLY
190 assert(cc
->vmsd
== NULL
);
191 #else /* !CONFIG_USER_ONLY */
192 if (qdev_get_vmsd(DEVICE(cpu
)) == NULL
) {
193 vmstate_register(NULL
, cpu
->cpu_index
, &vmstate_cpu_common
, cpu
);
195 if (cc
->vmsd
!= NULL
) {
196 vmstate_register(NULL
, cpu
->cpu_index
, cc
->vmsd
, cpu
);
199 tcg_iommu_init_notifier_list(cpu
);
203 const char *parse_cpu_option(const char *cpu_option
)
207 gchar
**model_pieces
;
208 const char *cpu_type
;
210 model_pieces
= g_strsplit(cpu_option
, ",", 2);
211 if (!model_pieces
[0]) {
212 error_report("-cpu option cannot be empty");
216 oc
= cpu_class_by_name(CPU_RESOLVING_TYPE
, model_pieces
[0]);
218 error_report("unable to find CPU model '%s'", model_pieces
[0]);
219 g_strfreev(model_pieces
);
223 cpu_type
= object_class_get_name(oc
);
225 cc
->parse_features(cpu_type
, model_pieces
[1], &error_fatal
);
226 g_strfreev(model_pieces
);
230 #if defined(CONFIG_USER_ONLY)
231 void tb_invalidate_phys_addr(target_ulong addr
)
234 tb_invalidate_phys_page_range(addr
, addr
+ 1);
238 static void breakpoint_invalidate(CPUState
*cpu
, target_ulong pc
)
240 tb_invalidate_phys_addr(pc
);
243 void tb_invalidate_phys_addr(AddressSpace
*as
, hwaddr addr
, MemTxAttrs attrs
)
249 if (!tcg_enabled()) {
253 RCU_READ_LOCK_GUARD();
254 mr
= address_space_translate(as
, addr
, &addr
, &l
, false, attrs
);
255 if (!(memory_region_is_ram(mr
)
256 || memory_region_is_romd(mr
))) {
259 ram_addr
= memory_region_get_ram_addr(mr
) + addr
;
260 tb_invalidate_phys_page_range(ram_addr
, ram_addr
+ 1);
263 static void breakpoint_invalidate(CPUState
*cpu
, target_ulong pc
)
266 * There may not be a virtual to physical translation for the pc
267 * right now, but there may exist cached TB for this pc.
268 * Flush the whole TB cache to force re-translation of such TBs.
269 * This is heavyweight, but we're debugging anyway.
275 /* Add a breakpoint. */
276 int cpu_breakpoint_insert(CPUState
*cpu
, vaddr pc
, int flags
,
277 CPUBreakpoint
**breakpoint
)
281 bp
= g_malloc(sizeof(*bp
));
286 /* keep all GDB-injected breakpoints in front */
287 if (flags
& BP_GDB
) {
288 QTAILQ_INSERT_HEAD(&cpu
->breakpoints
, bp
, entry
);
290 QTAILQ_INSERT_TAIL(&cpu
->breakpoints
, bp
, entry
);
293 breakpoint_invalidate(cpu
, pc
);
301 /* Remove a specific breakpoint. */
302 int cpu_breakpoint_remove(CPUState
*cpu
, vaddr pc
, int flags
)
306 QTAILQ_FOREACH(bp
, &cpu
->breakpoints
, entry
) {
307 if (bp
->pc
== pc
&& bp
->flags
== flags
) {
308 cpu_breakpoint_remove_by_ref(cpu
, bp
);
315 /* Remove a specific breakpoint by reference. */
316 void cpu_breakpoint_remove_by_ref(CPUState
*cpu
, CPUBreakpoint
*breakpoint
)
318 QTAILQ_REMOVE(&cpu
->breakpoints
, breakpoint
, entry
);
320 breakpoint_invalidate(cpu
, breakpoint
->pc
);
325 /* Remove all matching breakpoints. */
326 void cpu_breakpoint_remove_all(CPUState
*cpu
, int mask
)
328 CPUBreakpoint
*bp
, *next
;
330 QTAILQ_FOREACH_SAFE(bp
, &cpu
->breakpoints
, entry
, next
) {
331 if (bp
->flags
& mask
) {
332 cpu_breakpoint_remove_by_ref(cpu
, bp
);
337 /* enable or disable single step mode. EXCP_DEBUG is returned by the
338 CPU loop after each instruction */
339 void cpu_single_step(CPUState
*cpu
, int enabled
)
341 if (cpu
->singlestep_enabled
!= enabled
) {
342 cpu
->singlestep_enabled
= enabled
;
344 kvm_update_guest_debug(cpu
, 0);
346 /* must flush all the translated code to avoid inconsistencies */
347 /* XXX: only flush what is necessary */
353 void cpu_abort(CPUState
*cpu
, const char *fmt
, ...)
360 fprintf(stderr
, "qemu: fatal: ");
361 vfprintf(stderr
, fmt
, ap
);
362 fprintf(stderr
, "\n");
363 cpu_dump_state(cpu
, stderr
, CPU_DUMP_FPU
| CPU_DUMP_CCOP
);
364 if (qemu_log_separate()) {
365 FILE *logfile
= qemu_log_lock();
366 qemu_log("qemu: fatal: ");
367 qemu_log_vprintf(fmt
, ap2
);
369 log_cpu_state(cpu
, CPU_DUMP_FPU
| CPU_DUMP_CCOP
);
371 qemu_log_unlock(logfile
);
377 #if defined(CONFIG_USER_ONLY)
379 struct sigaction act
;
380 sigfillset(&act
.sa_mask
);
381 act
.sa_handler
= SIG_DFL
;
383 sigaction(SIGABRT
, &act
, NULL
);
389 /* physical memory access (slow version, mainly for debug) */
390 #if defined(CONFIG_USER_ONLY)
391 int cpu_memory_rw_debug(CPUState
*cpu
, target_ulong addr
,
392 void *ptr
, target_ulong len
, bool is_write
)
395 target_ulong l
, page
;
400 page
= addr
& TARGET_PAGE_MASK
;
401 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
404 flags
= page_get_flags(page
);
405 if (!(flags
& PAGE_VALID
))
408 if (!(flags
& PAGE_WRITE
))
410 /* XXX: this code should not depend on lock_user */
411 if (!(p
= lock_user(VERIFY_WRITE
, addr
, l
, 0)))
414 unlock_user(p
, addr
, l
);
416 if (!(flags
& PAGE_READ
))
418 /* XXX: this code should not depend on lock_user */
419 if (!(p
= lock_user(VERIFY_READ
, addr
, l
, 1)))
422 unlock_user(p
, addr
, 0);
432 bool target_words_bigendian(void)
434 #if defined(TARGET_WORDS_BIGENDIAN)
441 void page_size_init(void)
443 /* NOTE: we can always suppose that qemu_host_page_size >=
445 if (qemu_host_page_size
== 0) {
446 qemu_host_page_size
= qemu_real_host_page_size
;
448 if (qemu_host_page_size
< TARGET_PAGE_SIZE
) {
449 qemu_host_page_size
= TARGET_PAGE_SIZE
;
451 qemu_host_page_mask
= -(intptr_t)qemu_host_page_size
;