accel: introduce new accessor functions
[qemu.git] / cpu.c
blob25e6fbfa2c4329563c4a3f74a7ce1bbdf8f8c808
1 /*
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
30 #include "qemu.h"
31 #else
32 #include "exec/address-spaces.h"
33 #endif
34 #include "sysemu/tcg.h"
35 #include "sysemu/kvm.h"
36 #include "sysemu/replay.h"
37 #include "exec/translate-all.h"
38 #include "exec/log.h"
39 #include "hw/core/accel-cpu.h"
41 uintptr_t qemu_host_page_size;
42 intptr_t qemu_host_page_mask;
44 #ifndef CONFIG_USER_ONLY
45 static int cpu_common_post_load(void *opaque, int version_id)
47 CPUState *cpu = opaque;
49 /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
50 version_id is increased. */
51 cpu->interrupt_request &= ~0x01;
52 tlb_flush(cpu);
54 /* loadvm has just updated the content of RAM, bypassing the
55 * usual mechanisms that ensure we flush TBs for writes to
56 * memory we've translated code from. So we must flush all TBs,
57 * which will now be stale.
59 tb_flush(cpu);
61 return 0;
64 static int cpu_common_pre_load(void *opaque)
66 CPUState *cpu = opaque;
68 cpu->exception_index = -1;
70 return 0;
73 static bool cpu_common_exception_index_needed(void *opaque)
75 CPUState *cpu = opaque;
77 return tcg_enabled() && cpu->exception_index != -1;
80 static const VMStateDescription vmstate_cpu_common_exception_index = {
81 .name = "cpu_common/exception_index",
82 .version_id = 1,
83 .minimum_version_id = 1,
84 .needed = cpu_common_exception_index_needed,
85 .fields = (VMStateField[]) {
86 VMSTATE_INT32(exception_index, CPUState),
87 VMSTATE_END_OF_LIST()
91 static bool cpu_common_crash_occurred_needed(void *opaque)
93 CPUState *cpu = opaque;
95 return cpu->crash_occurred;
98 static const VMStateDescription vmstate_cpu_common_crash_occurred = {
99 .name = "cpu_common/crash_occurred",
100 .version_id = 1,
101 .minimum_version_id = 1,
102 .needed = cpu_common_crash_occurred_needed,
103 .fields = (VMStateField[]) {
104 VMSTATE_BOOL(crash_occurred, CPUState),
105 VMSTATE_END_OF_LIST()
109 const VMStateDescription vmstate_cpu_common = {
110 .name = "cpu_common",
111 .version_id = 1,
112 .minimum_version_id = 1,
113 .pre_load = cpu_common_pre_load,
114 .post_load = cpu_common_post_load,
115 .fields = (VMStateField[]) {
116 VMSTATE_UINT32(halted, CPUState),
117 VMSTATE_UINT32(interrupt_request, CPUState),
118 VMSTATE_END_OF_LIST()
120 .subsections = (const VMStateDescription*[]) {
121 &vmstate_cpu_common_exception_index,
122 &vmstate_cpu_common_crash_occurred,
123 NULL
126 #endif
128 void cpu_exec_realizefn(CPUState *cpu, Error **errp)
130 CPUClass *cc = CPU_GET_CLASS(cpu);
132 cpu_list_add(cpu);
133 accel_cpu_realizefn(cpu, errp);
135 #ifdef CONFIG_TCG
136 /* NB: errp parameter is unused currently */
137 if (tcg_enabled()) {
138 tcg_exec_realizefn(cpu, errp);
140 #endif /* CONFIG_TCG */
142 #ifdef CONFIG_USER_ONLY
143 assert(cc->vmsd == NULL);
144 #else
145 if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
146 vmstate_register(NULL, cpu->cpu_index, &vmstate_cpu_common, cpu);
148 if (cc->vmsd != NULL) {
149 vmstate_register(NULL, cpu->cpu_index, cc->vmsd, cpu);
151 #endif /* CONFIG_USER_ONLY */
154 void cpu_exec_unrealizefn(CPUState *cpu)
156 CPUClass *cc = CPU_GET_CLASS(cpu);
158 #ifdef CONFIG_USER_ONLY
159 assert(cc->vmsd == NULL);
160 #else
161 if (cc->vmsd != NULL) {
162 vmstate_unregister(NULL, cc->vmsd, cpu);
164 if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
165 vmstate_unregister(NULL, &vmstate_cpu_common, cpu);
167 #endif
168 #ifdef CONFIG_TCG
169 /* NB: errp parameter is unused currently */
170 if (tcg_enabled()) {
171 tcg_exec_unrealizefn(cpu);
173 #endif /* CONFIG_TCG */
175 cpu_list_remove(cpu);
178 void cpu_exec_initfn(CPUState *cpu)
180 cpu->as = NULL;
181 cpu->num_ases = 0;
183 #ifndef CONFIG_USER_ONLY
184 cpu->thread_id = qemu_get_thread_id();
185 cpu->memory = get_system_memory();
186 object_ref(OBJECT(cpu->memory));
187 #endif
190 const char *parse_cpu_option(const char *cpu_option)
192 ObjectClass *oc;
193 CPUClass *cc;
194 gchar **model_pieces;
195 const char *cpu_type;
197 model_pieces = g_strsplit(cpu_option, ",", 2);
198 if (!model_pieces[0]) {
199 error_report("-cpu option cannot be empty");
200 exit(1);
203 oc = cpu_class_by_name(CPU_RESOLVING_TYPE, model_pieces[0]);
204 if (oc == NULL) {
205 error_report("unable to find CPU model '%s'", model_pieces[0]);
206 g_strfreev(model_pieces);
207 exit(EXIT_FAILURE);
210 cpu_type = object_class_get_name(oc);
211 cc = CPU_CLASS(oc);
212 cc->parse_features(cpu_type, model_pieces[1], &error_fatal);
213 g_strfreev(model_pieces);
214 return cpu_type;
217 #if defined(CONFIG_USER_ONLY)
218 void tb_invalidate_phys_addr(target_ulong addr)
220 mmap_lock();
221 tb_invalidate_phys_page_range(addr, addr + 1);
222 mmap_unlock();
225 static void breakpoint_invalidate(CPUState *cpu, target_ulong pc)
227 tb_invalidate_phys_addr(pc);
229 #else
230 void tb_invalidate_phys_addr(AddressSpace *as, hwaddr addr, MemTxAttrs attrs)
232 ram_addr_t ram_addr;
233 MemoryRegion *mr;
234 hwaddr l = 1;
236 if (!tcg_enabled()) {
237 return;
240 RCU_READ_LOCK_GUARD();
241 mr = address_space_translate(as, addr, &addr, &l, false, attrs);
242 if (!(memory_region_is_ram(mr)
243 || memory_region_is_romd(mr))) {
244 return;
246 ram_addr = memory_region_get_ram_addr(mr) + addr;
247 tb_invalidate_phys_page_range(ram_addr, ram_addr + 1);
250 static void breakpoint_invalidate(CPUState *cpu, target_ulong pc)
253 * There may not be a virtual to physical translation for the pc
254 * right now, but there may exist cached TB for this pc.
255 * Flush the whole TB cache to force re-translation of such TBs.
256 * This is heavyweight, but we're debugging anyway.
258 tb_flush(cpu);
260 #endif
262 /* Add a breakpoint. */
263 int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
264 CPUBreakpoint **breakpoint)
266 CPUBreakpoint *bp;
268 bp = g_malloc(sizeof(*bp));
270 bp->pc = pc;
271 bp->flags = flags;
273 /* keep all GDB-injected breakpoints in front */
274 if (flags & BP_GDB) {
275 QTAILQ_INSERT_HEAD(&cpu->breakpoints, bp, entry);
276 } else {
277 QTAILQ_INSERT_TAIL(&cpu->breakpoints, bp, entry);
280 breakpoint_invalidate(cpu, pc);
282 if (breakpoint) {
283 *breakpoint = bp;
285 return 0;
288 /* Remove a specific breakpoint. */
289 int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags)
291 CPUBreakpoint *bp;
293 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
294 if (bp->pc == pc && bp->flags == flags) {
295 cpu_breakpoint_remove_by_ref(cpu, bp);
296 return 0;
299 return -ENOENT;
302 /* Remove a specific breakpoint by reference. */
303 void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint)
305 QTAILQ_REMOVE(&cpu->breakpoints, breakpoint, entry);
307 breakpoint_invalidate(cpu, breakpoint->pc);
309 g_free(breakpoint);
312 /* Remove all matching breakpoints. */
313 void cpu_breakpoint_remove_all(CPUState *cpu, int mask)
315 CPUBreakpoint *bp, *next;
317 QTAILQ_FOREACH_SAFE(bp, &cpu->breakpoints, entry, next) {
318 if (bp->flags & mask) {
319 cpu_breakpoint_remove_by_ref(cpu, bp);
324 /* enable or disable single step mode. EXCP_DEBUG is returned by the
325 CPU loop after each instruction */
326 void cpu_single_step(CPUState *cpu, int enabled)
328 if (cpu->singlestep_enabled != enabled) {
329 cpu->singlestep_enabled = enabled;
330 if (kvm_enabled()) {
331 kvm_update_guest_debug(cpu, 0);
332 } else {
333 /* must flush all the translated code to avoid inconsistencies */
334 /* XXX: only flush what is necessary */
335 tb_flush(cpu);
340 void cpu_abort(CPUState *cpu, const char *fmt, ...)
342 va_list ap;
343 va_list ap2;
345 va_start(ap, fmt);
346 va_copy(ap2, ap);
347 fprintf(stderr, "qemu: fatal: ");
348 vfprintf(stderr, fmt, ap);
349 fprintf(stderr, "\n");
350 cpu_dump_state(cpu, stderr, CPU_DUMP_FPU | CPU_DUMP_CCOP);
351 if (qemu_log_separate()) {
352 FILE *logfile = qemu_log_lock();
353 qemu_log("qemu: fatal: ");
354 qemu_log_vprintf(fmt, ap2);
355 qemu_log("\n");
356 log_cpu_state(cpu, CPU_DUMP_FPU | CPU_DUMP_CCOP);
357 qemu_log_flush();
358 qemu_log_unlock(logfile);
359 qemu_log_close();
361 va_end(ap2);
362 va_end(ap);
363 replay_finish();
364 #if defined(CONFIG_USER_ONLY)
366 struct sigaction act;
367 sigfillset(&act.sa_mask);
368 act.sa_handler = SIG_DFL;
369 act.sa_flags = 0;
370 sigaction(SIGABRT, &act, NULL);
372 #endif
373 abort();
376 /* physical memory access (slow version, mainly for debug) */
377 #if defined(CONFIG_USER_ONLY)
378 int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr,
379 void *ptr, target_ulong len, bool is_write)
381 int flags;
382 target_ulong l, page;
383 void * p;
384 uint8_t *buf = ptr;
386 while (len > 0) {
387 page = addr & TARGET_PAGE_MASK;
388 l = (page + TARGET_PAGE_SIZE) - addr;
389 if (l > len)
390 l = len;
391 flags = page_get_flags(page);
392 if (!(flags & PAGE_VALID))
393 return -1;
394 if (is_write) {
395 if (!(flags & PAGE_WRITE))
396 return -1;
397 /* XXX: this code should not depend on lock_user */
398 if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
399 return -1;
400 memcpy(p, buf, l);
401 unlock_user(p, addr, l);
402 } else {
403 if (!(flags & PAGE_READ))
404 return -1;
405 /* XXX: this code should not depend on lock_user */
406 if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
407 return -1;
408 memcpy(buf, p, l);
409 unlock_user(p, addr, 0);
411 len -= l;
412 buf += l;
413 addr += l;
415 return 0;
417 #endif
419 bool target_words_bigendian(void)
421 #if defined(TARGET_WORDS_BIGENDIAN)
422 return true;
423 #else
424 return false;
425 #endif
428 void page_size_init(void)
430 /* NOTE: we can always suppose that qemu_host_page_size >=
431 TARGET_PAGE_SIZE */
432 if (qemu_host_page_size == 0) {
433 qemu_host_page_size = qemu_real_host_page_size;
435 if (qemu_host_page_size < TARGET_PAGE_SIZE) {
436 qemu_host_page_size = TARGET_PAGE_SIZE;
438 qemu_host_page_mask = -(intptr_t)qemu_host_page_size;