4 * Copyright (c) 2012 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>
25 #include "hw/qdev-core.h"
26 #include "exec/hwaddr.h"
27 #include "qemu/queue.h"
28 #include "qemu/thread.h"
30 #include "qemu/typedefs.h"
32 typedef int (*WriteCoreDumpFunction
)(const void *buf
, size_t size
,
37 * Type wide enough to contain any #target_ulong virtual address.
39 typedef uint64_t vaddr
;
40 #define VADDR_PRId PRId64
41 #define VADDR_PRIu PRIu64
42 #define VADDR_PRIo PRIo64
43 #define VADDR_PRIx PRIx64
44 #define VADDR_PRIX PRIX64
45 #define VADDR_MAX UINT64_MAX
49 * @section_id: QEMU-cpu
51 * @short_description: Base class for all CPUs
54 #define TYPE_CPU "cpu"
56 /* Since this macro is used a lot in hot code paths and in conjunction with
57 * FooCPU *foo_env_get_cpu(), we deviate from usual QOM practice by using
60 #define CPU(obj) ((CPUState *)(obj))
62 #define CPU_CLASS(class) OBJECT_CLASS_CHECK(CPUClass, (class), TYPE_CPU)
63 #define CPU_GET_CLASS(obj) OBJECT_GET_CLASS(CPUClass, (obj), TYPE_CPU)
65 typedef struct CPUState CPUState
;
67 typedef void (*CPUUnassignedAccess
)(CPUState
*cpu
, hwaddr addr
,
68 bool is_write
, bool is_exec
, int opaque
,
71 struct TranslationBlock
;
75 * @class_by_name: Callback to map -cpu command line model name to an
76 * instantiatable CPU type.
77 * @parse_features: Callback to parse command line arguments.
78 * @reset: Callback to reset the #CPUState to its initial state.
79 * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
80 * @has_work: Callback for checking if there is work to do.
81 * @do_interrupt: Callback for interrupt handling.
82 * @do_unassigned_access: Callback for unassigned access handling.
83 * @do_unaligned_access: Callback for unaligned access handling, if
84 * the target defines #ALIGNED_ONLY.
85 * @memory_rw_debug: Callback for GDB memory access.
86 * @dump_state: Callback for dumping state.
87 * @dump_statistics: Callback for dumping statistics.
88 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
89 * @get_paging_enabled: Callback for inquiring whether paging is enabled.
90 * @get_memory_mapping: Callback for obtaining the memory mappings.
91 * @set_pc: Callback for setting the Program Counter register.
92 * @synchronize_from_tb: Callback for synchronizing state from a TCG
94 * @handle_mmu_fault: Callback for handling an MMU fault.
95 * @get_phys_page_debug: Callback for obtaining a physical address.
96 * @gdb_read_register: Callback for letting GDB read a register.
97 * @gdb_write_register: Callback for letting GDB write a register.
98 * @debug_excp_handler: Callback for handling debug exceptions.
99 * @vmsd: State description for migration.
100 * @gdb_num_core_regs: Number of core registers accessible to GDB.
101 * @gdb_core_xml_file: File name for core registers GDB XML description.
102 * @cpu_exec_enter: Callback for cpu_exec preparation.
103 * @cpu_exec_exit: Callback for cpu_exec cleanup.
105 * Represents a CPU family or model.
107 typedef struct CPUClass
{
109 DeviceClass parent_class
;
112 ObjectClass
*(*class_by_name
)(const char *cpu_model
);
113 void (*parse_features
)(CPUState
*cpu
, char *str
, Error
**errp
);
115 void (*reset
)(CPUState
*cpu
);
116 int reset_dump_flags
;
117 bool (*has_work
)(CPUState
*cpu
);
118 void (*do_interrupt
)(CPUState
*cpu
);
119 CPUUnassignedAccess do_unassigned_access
;
120 void (*do_unaligned_access
)(CPUState
*cpu
, vaddr addr
,
121 int is_write
, int is_user
, uintptr_t retaddr
);
122 bool (*virtio_is_big_endian
)(CPUState
*cpu
);
123 int (*memory_rw_debug
)(CPUState
*cpu
, vaddr addr
,
124 uint8_t *buf
, int len
, bool is_write
);
125 void (*dump_state
)(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
127 void (*dump_statistics
)(CPUState
*cpu
, FILE *f
,
128 fprintf_function cpu_fprintf
, int flags
);
129 int64_t (*get_arch_id
)(CPUState
*cpu
);
130 bool (*get_paging_enabled
)(const CPUState
*cpu
);
131 void (*get_memory_mapping
)(CPUState
*cpu
, MemoryMappingList
*list
,
133 void (*set_pc
)(CPUState
*cpu
, vaddr value
);
134 void (*synchronize_from_tb
)(CPUState
*cpu
, struct TranslationBlock
*tb
);
135 int (*handle_mmu_fault
)(CPUState
*cpu
, vaddr address
, int rw
,
137 hwaddr (*get_phys_page_debug
)(CPUState
*cpu
, vaddr addr
);
138 int (*gdb_read_register
)(CPUState
*cpu
, uint8_t *buf
, int reg
);
139 int (*gdb_write_register
)(CPUState
*cpu
, uint8_t *buf
, int reg
);
140 void (*debug_excp_handler
)(CPUState
*cpu
);
142 int (*write_elf64_note
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
143 int cpuid
, void *opaque
);
144 int (*write_elf64_qemunote
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
146 int (*write_elf32_note
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
147 int cpuid
, void *opaque
);
148 int (*write_elf32_qemunote
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
151 const struct VMStateDescription
*vmsd
;
152 int gdb_num_core_regs
;
153 const char *gdb_core_xml_file
;
155 void (*cpu_exec_enter
)(CPUState
*cpu
);
156 void (*cpu_exec_exit
)(CPUState
*cpu
);
159 #ifdef HOST_WORDS_BIGENDIAN
160 typedef struct icount_decr_u16
{
165 typedef struct icount_decr_u16
{
171 typedef struct CPUBreakpoint
{
173 int flags
; /* BP_* */
174 QTAILQ_ENTRY(CPUBreakpoint
) entry
;
177 typedef struct CPUWatchpoint
{
181 int flags
; /* BP_* */
182 QTAILQ_ENTRY(CPUWatchpoint
) entry
;
188 #define TB_JMP_CACHE_BITS 12
189 #define TB_JMP_CACHE_SIZE (1 << TB_JMP_CACHE_BITS)
193 * @cpu_index: CPU index (informative).
194 * @nr_cores: Number of cores within this CPU package.
195 * @nr_threads: Number of threads within this CPU.
196 * @numa_node: NUMA node this CPU is belonging to.
197 * @host_tid: Host thread ID.
198 * @running: #true if CPU is currently running (usermode).
199 * @created: Indicates whether the CPU thread has been successfully created.
200 * @interrupt_request: Indicates a pending interrupt request.
201 * @halted: Nonzero if the CPU is in suspended state.
202 * @stop: Indicates a pending stop request.
203 * @stopped: Indicates the CPU has been artificially stopped.
204 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
205 * CPU and return to its top level loop.
206 * @singlestep_enabled: Flags for single-stepping.
207 * @icount_extra: Instructions until next timer event.
208 * @icount_decr: Number of cycles left, with interrupt flag in high bit.
209 * This allows a single read-compare-cbranch-write sequence to test
210 * for both decrementer underflow and exceptions.
211 * @can_do_io: Nonzero if memory-mapped IO is safe.
212 * @env_ptr: Pointer to subclass-specific CPUArchState field.
213 * @current_tb: Currently executing TB.
214 * @gdb_regs: Additional GDB registers.
215 * @gdb_num_regs: Number of total registers accessible to GDB.
216 * @gdb_num_g_regs: Number of registers in GDB 'g' packets.
217 * @next_cpu: Next CPU sharing TB cache.
218 * @opaque: User data.
219 * @mem_io_pc: Host Program Counter at which the memory was accessed.
220 * @mem_io_vaddr: Target virtual address at which the memory was accessed.
221 * @kvm_fd: vCPU file descriptor for KVM.
223 * State of one CPU core or thread.
227 DeviceState parent_obj
;
234 struct QemuThread
*thread
;
241 struct QemuCond
*halt_cond
;
242 struct qemu_work_item
*queued_work_first
, *queued_work_last
;
247 volatile sig_atomic_t exit_request
;
248 uint32_t interrupt_request
;
249 int singlestep_enabled
;
250 int64_t icount_extra
;
254 MemoryListener
*tcg_as_listener
;
256 void *env_ptr
; /* CPUArchState */
257 struct TranslationBlock
*current_tb
;
258 struct TranslationBlock
*tb_jmp_cache
[TB_JMP_CACHE_SIZE
];
259 struct GDBRegisterState
*gdb_regs
;
262 QTAILQ_ENTRY(CPUState
) node
;
264 /* ice debug support */
265 QTAILQ_HEAD(breakpoints_head
, CPUBreakpoint
) breakpoints
;
267 QTAILQ_HEAD(watchpoints_head
, CPUWatchpoint
) watchpoints
;
268 CPUWatchpoint
*watchpoint_hit
;
272 /* In order to avoid passing too many arguments to the MMIO helpers,
273 * we store some rarely used information in the CPU context.
280 struct KVMState
*kvm_state
;
281 struct kvm_run
*kvm_run
;
283 /* TODO Move common fields from CPUArchState here. */
284 int cpu_index
; /* used by alpha TCG */
285 uint32_t halted
; /* used by alpha, cris, ppc TCG */
291 int32_t exception_index
; /* used by m68k TCG */
293 /* Note that this is accessed at the start of every TB via a negative
294 offset from AREG0. Leave this field at the end so as to make the
295 (absolute value) offset as small as possible. This reduces code
296 size, especially for hosts without large memory offsets. */
297 volatile sig_atomic_t tcg_exit_req
;
300 QTAILQ_HEAD(CPUTailQ
, CPUState
);
301 extern struct CPUTailQ cpus
;
302 #define CPU_NEXT(cpu) QTAILQ_NEXT(cpu, node)
303 #define CPU_FOREACH(cpu) QTAILQ_FOREACH(cpu, &cpus, node)
304 #define CPU_FOREACH_SAFE(cpu, next_cpu) \
305 QTAILQ_FOREACH_SAFE(cpu, &cpus, node, next_cpu)
306 #define first_cpu QTAILQ_FIRST(&cpus)
308 DECLARE_TLS(CPUState
*, current_cpu
);
309 #define current_cpu tls_var(current_cpu)
312 * cpu_paging_enabled:
313 * @cpu: The CPU whose state is to be inspected.
315 * Returns: %true if paging is enabled, %false otherwise.
317 bool cpu_paging_enabled(const CPUState
*cpu
);
320 * cpu_get_memory_mapping:
321 * @cpu: The CPU whose memory mappings are to be obtained.
322 * @list: Where to write the memory mappings to.
323 * @errp: Pointer for reporting an #Error.
325 void cpu_get_memory_mapping(CPUState
*cpu
, MemoryMappingList
*list
,
329 * cpu_write_elf64_note:
330 * @f: pointer to a function that writes memory to a file
331 * @cpu: The CPU whose memory is to be dumped
332 * @cpuid: ID number of the CPU
333 * @opaque: pointer to the CPUState struct
335 int cpu_write_elf64_note(WriteCoreDumpFunction f
, CPUState
*cpu
,
336 int cpuid
, void *opaque
);
339 * cpu_write_elf64_qemunote:
340 * @f: pointer to a function that writes memory to a file
341 * @cpu: The CPU whose memory is to be dumped
342 * @cpuid: ID number of the CPU
343 * @opaque: pointer to the CPUState struct
345 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f
, CPUState
*cpu
,
349 * cpu_write_elf32_note:
350 * @f: pointer to a function that writes memory to a file
351 * @cpu: The CPU whose memory is to be dumped
352 * @cpuid: ID number of the CPU
353 * @opaque: pointer to the CPUState struct
355 int cpu_write_elf32_note(WriteCoreDumpFunction f
, CPUState
*cpu
,
356 int cpuid
, void *opaque
);
359 * cpu_write_elf32_qemunote:
360 * @f: pointer to a function that writes memory to a file
361 * @cpu: The CPU whose memory is to be dumped
362 * @cpuid: ID number of the CPU
363 * @opaque: pointer to the CPUState struct
365 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f
, CPUState
*cpu
,
371 * @CPU_DUMP_FPU: dump FPU register state, not just integer
372 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
375 CPU_DUMP_CODE
= 0x00010000,
376 CPU_DUMP_FPU
= 0x00020000,
377 CPU_DUMP_CCOP
= 0x00040000,
382 * @cpu: The CPU whose state is to be dumped.
383 * @f: File to dump to.
384 * @cpu_fprintf: Function to dump with.
385 * @flags: Flags what to dump.
389 void cpu_dump_state(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
393 * cpu_dump_statistics:
394 * @cpu: The CPU whose state is to be dumped.
395 * @f: File to dump to.
396 * @cpu_fprintf: Function to dump with.
397 * @flags: Flags what to dump.
399 * Dumps CPU statistics.
401 void cpu_dump_statistics(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
404 #ifndef CONFIG_USER_ONLY
406 * cpu_get_phys_page_debug:
407 * @cpu: The CPU to obtain the physical page address for.
408 * @addr: The virtual address.
410 * Obtains the physical page corresponding to a virtual one.
411 * Use it only for debugging because no protection checks are done.
413 * Returns: Corresponding physical page address or -1 if no page found.
415 static inline hwaddr
cpu_get_phys_page_debug(CPUState
*cpu
, vaddr addr
)
417 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
419 return cc
->get_phys_page_debug(cpu
, addr
);
425 * @cpu: The CPU whose state is to be reset.
427 void cpu_reset(CPUState
*cpu
);
431 * @typename: The CPU base type.
432 * @cpu_model: The model string without any parameters.
434 * Looks up a CPU #ObjectClass matching name @cpu_model.
436 * Returns: A #CPUClass or %NULL if not matching class is found.
438 ObjectClass
*cpu_class_by_name(const char *typename
, const char *cpu_model
);
442 * @typename: The CPU base type.
443 * @cpu_model: The model string including optional parameters.
445 * Instantiates a CPU, processes optional parameters and realizes the CPU.
447 * Returns: A #CPUState or %NULL if an error occurred.
449 CPUState
*cpu_generic_init(const char *typename
, const char *cpu_model
);
453 * @cpu: The vCPU to check.
455 * Checks whether the CPU has work to do.
457 * Returns: %true if the CPU has work, %false otherwise.
459 static inline bool cpu_has_work(CPUState
*cpu
)
461 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
463 g_assert(cc
->has_work
);
464 return cc
->has_work(cpu
);
469 * @cpu: The vCPU to check against.
471 * Checks whether the caller is executing on the vCPU thread.
473 * Returns: %true if called from @cpu's thread, %false otherwise.
475 bool qemu_cpu_is_self(CPUState
*cpu
);
479 * @cpu: The vCPU to kick.
481 * Kicks @cpu's thread.
483 void qemu_cpu_kick(CPUState
*cpu
);
487 * @cpu: The CPU to check.
489 * Checks whether the CPU is stopped.
491 * Returns: %true if run state is not running or if artificially stopped;
494 bool cpu_is_stopped(CPUState
*cpu
);
498 * @cpu: The vCPU to run on.
499 * @func: The function to be executed.
500 * @data: Data to pass to the function.
502 * Schedules the function @func for execution on the vCPU @cpu.
504 void run_on_cpu(CPUState
*cpu
, void (*func
)(void *data
), void *data
);
508 * @cpu: The vCPU to run on.
509 * @func: The function to be executed.
510 * @data: Data to pass to the function.
512 * Schedules the function @func for execution on the vCPU @cpu asynchronously.
514 void async_run_on_cpu(CPUState
*cpu
, void (*func
)(void *data
), void *data
);
518 * @index: The CPUState@cpu_index value of the CPU to obtain.
520 * Gets a CPU matching @index.
522 * Returns: The CPU or %NULL if there is no matching CPU.
524 CPUState
*qemu_get_cpu(int index
);
528 * @id: Guest-exposed CPU ID to lookup.
530 * Search for CPU with specified ID.
532 * Returns: %true - CPU is found, %false - CPU isn't found.
534 bool cpu_exists(int64_t id
);
536 #ifndef CONFIG_USER_ONLY
538 typedef void (*CPUInterruptHandler
)(CPUState
*, int);
540 extern CPUInterruptHandler cpu_interrupt_handler
;
544 * @cpu: The CPU to set an interrupt on.
545 * @mask: The interupts to set.
547 * Invokes the interrupt handler.
549 static inline void cpu_interrupt(CPUState
*cpu
, int mask
)
551 cpu_interrupt_handler(cpu
, mask
);
554 #else /* USER_ONLY */
556 void cpu_interrupt(CPUState
*cpu
, int mask
);
558 #endif /* USER_ONLY */
560 #ifdef CONFIG_SOFTMMU
561 static inline void cpu_unassigned_access(CPUState
*cpu
, hwaddr addr
,
562 bool is_write
, bool is_exec
,
563 int opaque
, unsigned size
)
565 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
567 if (cc
->do_unassigned_access
) {
568 cc
->do_unassigned_access(cpu
, addr
, is_write
, is_exec
, opaque
, size
);
572 static inline void cpu_unaligned_access(CPUState
*cpu
, vaddr addr
,
573 int is_write
, int is_user
,
576 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
578 return cc
->do_unaligned_access(cpu
, addr
, is_write
, is_user
, retaddr
);
583 * cpu_reset_interrupt:
584 * @cpu: The CPU to clear the interrupt on.
585 * @mask: The interrupt mask to clear.
587 * Resets interrupts on the vCPU @cpu.
589 void cpu_reset_interrupt(CPUState
*cpu
, int mask
);
593 * @cpu: The CPU to exit.
595 * Requests the CPU @cpu to exit execution.
597 void cpu_exit(CPUState
*cpu
);
601 * @cpu: The CPU to resume.
603 * Resumes CPU, i.e. puts CPU into runnable state.
605 void cpu_resume(CPUState
*cpu
);
609 * @cpu: The vCPU to initialize.
611 * Initializes a vCPU.
613 void qemu_init_vcpu(CPUState
*cpu
);
615 #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
616 #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
617 #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
621 * @cpu: CPU to the flags for.
622 * @enabled: Flags to enable.
624 * Enables or disables single-stepping for @cpu.
626 void cpu_single_step(CPUState
*cpu
, int enabled
);
628 /* Breakpoint/watchpoint flags */
629 #define BP_MEM_READ 0x01
630 #define BP_MEM_WRITE 0x02
631 #define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE)
632 #define BP_STOP_BEFORE_ACCESS 0x04
633 /* 0x08 currently unused */
636 #define BP_WATCHPOINT_HIT_READ 0x40
637 #define BP_WATCHPOINT_HIT_WRITE 0x80
638 #define BP_WATCHPOINT_HIT (BP_WATCHPOINT_HIT_READ | BP_WATCHPOINT_HIT_WRITE)
640 int cpu_breakpoint_insert(CPUState
*cpu
, vaddr pc
, int flags
,
641 CPUBreakpoint
**breakpoint
);
642 int cpu_breakpoint_remove(CPUState
*cpu
, vaddr pc
, int flags
);
643 void cpu_breakpoint_remove_by_ref(CPUState
*cpu
, CPUBreakpoint
*breakpoint
);
644 void cpu_breakpoint_remove_all(CPUState
*cpu
, int mask
);
646 int cpu_watchpoint_insert(CPUState
*cpu
, vaddr addr
, vaddr len
,
647 int flags
, CPUWatchpoint
**watchpoint
);
648 int cpu_watchpoint_remove(CPUState
*cpu
, vaddr addr
,
649 vaddr len
, int flags
);
650 void cpu_watchpoint_remove_by_ref(CPUState
*cpu
, CPUWatchpoint
*watchpoint
);
651 void cpu_watchpoint_remove_all(CPUState
*cpu
, int mask
);
653 void QEMU_NORETURN
cpu_abort(CPUState
*cpu
, const char *fmt
, ...)
656 #ifdef CONFIG_SOFTMMU
657 extern const struct VMStateDescription vmstate_cpu_common
;
659 #define vmstate_cpu_common vmstate_dummy
662 #define VMSTATE_CPU() { \
663 .name = "parent_obj", \
664 .size = sizeof(CPUState), \
665 .vmsd = &vmstate_cpu_common, \
666 .flags = VMS_STRUCT, \