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>
23 #include "hw/qdev-core.h"
24 #include "disas/bfd.h"
25 #include "exec/hwaddr.h"
26 #include "exec/memattrs.h"
27 #include "qemu/bitmap.h"
28 #include "qemu/queue.h"
29 #include "qemu/thread.h"
31 typedef int (*WriteCoreDumpFunction
)(const void *buf
, size_t size
,
36 * Type wide enough to contain any #target_ulong virtual address.
38 typedef uint64_t vaddr
;
39 #define VADDR_PRId PRId64
40 #define VADDR_PRIu PRIu64
41 #define VADDR_PRIo PRIo64
42 #define VADDR_PRIx PRIx64
43 #define VADDR_PRIX PRIX64
44 #define VADDR_MAX UINT64_MAX
48 * @section_id: QEMU-cpu
50 * @short_description: Base class for all CPUs
53 #define TYPE_CPU "cpu"
55 /* Since this macro is used a lot in hot code paths and in conjunction with
56 * FooCPU *foo_env_get_cpu(), we deviate from usual QOM practice by using
59 #define CPU(obj) ((CPUState *)(obj))
61 #define CPU_CLASS(class) OBJECT_CLASS_CHECK(CPUClass, (class), TYPE_CPU)
62 #define CPU_GET_CLASS(obj) OBJECT_GET_CLASS(CPUClass, (obj), TYPE_CPU)
64 typedef enum MMUAccessType
{
70 typedef struct CPUWatchpoint CPUWatchpoint
;
72 typedef void (*CPUUnassignedAccess
)(CPUState
*cpu
, hwaddr addr
,
73 bool is_write
, bool is_exec
, int opaque
,
76 struct TranslationBlock
;
80 * @class_by_name: Callback to map -cpu command line model name to an
81 * instantiatable CPU type.
82 * @parse_features: Callback to parse command line arguments.
83 * @reset: Callback to reset the #CPUState to its initial state.
84 * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
85 * @has_work: Callback for checking if there is work to do.
86 * @do_interrupt: Callback for interrupt handling.
87 * @do_unassigned_access: Callback for unassigned access handling.
88 * (this is deprecated: new targets should use do_transaction_failed instead)
89 * @do_unaligned_access: Callback for unaligned access handling, if
90 * the target defines #ALIGNED_ONLY.
91 * @do_transaction_failed: Callback for handling failed memory transactions
92 * (ie bus faults or external aborts; not MMU faults)
93 * @virtio_is_big_endian: Callback to return %true if a CPU which supports
94 * runtime configurable endianness is currently big-endian. Non-configurable
95 * CPUs can use the default implementation of this method. This method should
96 * not be used by any callers other than the pre-1.0 virtio devices.
97 * @memory_rw_debug: Callback for GDB memory access.
98 * @dump_state: Callback for dumping state.
99 * @dump_statistics: Callback for dumping statistics.
100 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
101 * @get_paging_enabled: Callback for inquiring whether paging is enabled.
102 * @get_memory_mapping: Callback for obtaining the memory mappings.
103 * @set_pc: Callback for setting the Program Counter register.
104 * @synchronize_from_tb: Callback for synchronizing state from a TCG
106 * @handle_mmu_fault: Callback for handling an MMU fault.
107 * @get_phys_page_debug: Callback for obtaining a physical address.
108 * @get_phys_page_attrs_debug: Callback for obtaining a physical address and the
109 * associated memory transaction attributes to use for the access.
110 * CPUs which use memory transaction attributes should implement this
111 * instead of get_phys_page_debug.
112 * @asidx_from_attrs: Callback to return the CPU AddressSpace to use for
113 * a memory access with the specified memory transaction attributes.
114 * @gdb_read_register: Callback for letting GDB read a register.
115 * @gdb_write_register: Callback for letting GDB write a register.
116 * @debug_check_watchpoint: Callback: return true if the architectural
117 * watchpoint whose address has matched should really fire.
118 * @debug_excp_handler: Callback for handling debug exceptions.
119 * @write_elf64_note: Callback for writing a CPU-specific ELF note to a
120 * 64-bit VM coredump.
121 * @write_elf32_qemunote: Callback for writing a CPU- and QEMU-specific ELF
122 * note to a 32-bit VM coredump.
123 * @write_elf32_note: Callback for writing a CPU-specific ELF note to a
124 * 32-bit VM coredump.
125 * @write_elf32_qemunote: Callback for writing a CPU- and QEMU-specific ELF
126 * note to a 32-bit VM coredump.
127 * @vmsd: State description for migration.
128 * @gdb_num_core_regs: Number of core registers accessible to GDB.
129 * @gdb_core_xml_file: File name for core registers GDB XML description.
130 * @gdb_stop_before_watchpoint: Indicates whether GDB expects the CPU to stop
131 * before the insn which triggers a watchpoint rather than after it.
132 * @gdb_arch_name: Optional callback that returns the architecture name known
133 * to GDB. The caller must free the returned string with g_free.
134 * @cpu_exec_enter: Callback for cpu_exec preparation.
135 * @cpu_exec_exit: Callback for cpu_exec cleanup.
136 * @cpu_exec_interrupt: Callback for processing interrupts in cpu_exec.
137 * @disas_set_info: Setup architecture specific components of disassembly info
138 * @adjust_watchpoint_address: Perform a target-specific adjustment to an
139 * address before attempting to match it against watchpoints.
141 * Represents a CPU family or model.
143 typedef struct CPUClass
{
145 DeviceClass parent_class
;
148 ObjectClass
*(*class_by_name
)(const char *cpu_model
);
149 void (*parse_features
)(const char *typename
, char *str
, Error
**errp
);
151 void (*reset
)(CPUState
*cpu
);
152 int reset_dump_flags
;
153 bool (*has_work
)(CPUState
*cpu
);
154 void (*do_interrupt
)(CPUState
*cpu
);
155 CPUUnassignedAccess do_unassigned_access
;
156 void (*do_unaligned_access
)(CPUState
*cpu
, vaddr addr
,
157 MMUAccessType access_type
,
158 int mmu_idx
, uintptr_t retaddr
);
159 void (*do_transaction_failed
)(CPUState
*cpu
, hwaddr physaddr
, vaddr addr
,
160 unsigned size
, MMUAccessType access_type
,
161 int mmu_idx
, MemTxAttrs attrs
,
162 MemTxResult response
, uintptr_t retaddr
);
163 bool (*virtio_is_big_endian
)(CPUState
*cpu
);
164 int (*memory_rw_debug
)(CPUState
*cpu
, vaddr addr
,
165 uint8_t *buf
, int len
, bool is_write
);
166 void (*dump_state
)(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
168 GuestPanicInformation
* (*get_crash_info
)(CPUState
*cpu
);
169 void (*dump_statistics
)(CPUState
*cpu
, FILE *f
,
170 fprintf_function cpu_fprintf
, int flags
);
171 int64_t (*get_arch_id
)(CPUState
*cpu
);
172 bool (*get_paging_enabled
)(const CPUState
*cpu
);
173 void (*get_memory_mapping
)(CPUState
*cpu
, MemoryMappingList
*list
,
175 void (*set_pc
)(CPUState
*cpu
, vaddr value
);
176 void (*synchronize_from_tb
)(CPUState
*cpu
, struct TranslationBlock
*tb
);
177 int (*handle_mmu_fault
)(CPUState
*cpu
, vaddr address
, int rw
,
179 hwaddr (*get_phys_page_debug
)(CPUState
*cpu
, vaddr addr
);
180 hwaddr (*get_phys_page_attrs_debug
)(CPUState
*cpu
, vaddr addr
,
182 int (*asidx_from_attrs
)(CPUState
*cpu
, MemTxAttrs attrs
);
183 int (*gdb_read_register
)(CPUState
*cpu
, uint8_t *buf
, int reg
);
184 int (*gdb_write_register
)(CPUState
*cpu
, uint8_t *buf
, int reg
);
185 bool (*debug_check_watchpoint
)(CPUState
*cpu
, CPUWatchpoint
*wp
);
186 void (*debug_excp_handler
)(CPUState
*cpu
);
188 int (*write_elf64_note
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
189 int cpuid
, void *opaque
);
190 int (*write_elf64_qemunote
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
192 int (*write_elf32_note
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
193 int cpuid
, void *opaque
);
194 int (*write_elf32_qemunote
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
197 const struct VMStateDescription
*vmsd
;
198 const char *gdb_core_xml_file
;
199 gchar
* (*gdb_arch_name
)(CPUState
*cpu
);
201 void (*cpu_exec_enter
)(CPUState
*cpu
);
202 void (*cpu_exec_exit
)(CPUState
*cpu
);
203 bool (*cpu_exec_interrupt
)(CPUState
*cpu
, int interrupt_request
);
205 void (*disas_set_info
)(CPUState
*cpu
, disassemble_info
*info
);
206 vaddr (*adjust_watchpoint_address
)(CPUState
*cpu
, vaddr addr
, int len
);
207 void (*tcg_initialize
)(void);
209 /* Keep non-pointer data at the end to minimize holes. */
210 int gdb_num_core_regs
;
211 bool gdb_stop_before_watchpoint
;
214 #ifdef HOST_WORDS_BIGENDIAN
215 typedef struct icount_decr_u16
{
220 typedef struct icount_decr_u16
{
226 typedef struct CPUBreakpoint
{
228 int flags
; /* BP_* */
229 QTAILQ_ENTRY(CPUBreakpoint
) entry
;
232 struct CPUWatchpoint
{
237 int flags
; /* BP_* */
238 QTAILQ_ENTRY(CPUWatchpoint
) entry
;
244 struct hax_vcpu_state
;
246 #define TB_JMP_CACHE_BITS 12
247 #define TB_JMP_CACHE_SIZE (1 << TB_JMP_CACHE_BITS)
251 /* The union type allows passing of 64 bit target pointers on 32 bit
252 * hosts in a single parameter
256 unsigned long host_ulong
;
261 #define RUN_ON_CPU_HOST_PTR(p) ((run_on_cpu_data){.host_ptr = (p)})
262 #define RUN_ON_CPU_HOST_INT(i) ((run_on_cpu_data){.host_int = (i)})
263 #define RUN_ON_CPU_HOST_ULONG(ul) ((run_on_cpu_data){.host_ulong = (ul)})
264 #define RUN_ON_CPU_TARGET_PTR(v) ((run_on_cpu_data){.target_ptr = (v)})
265 #define RUN_ON_CPU_NULL RUN_ON_CPU_HOST_PTR(NULL)
267 typedef void (*run_on_cpu_func
)(CPUState
*cpu
, run_on_cpu_data data
);
269 struct qemu_work_item
;
271 #define CPU_UNSET_NUMA_NODE_ID -1
272 #define CPU_TRACE_DSTATE_MAX_EVENTS 32
276 * @cpu_index: CPU index (informative).
277 * @nr_cores: Number of cores within this CPU package.
278 * @nr_threads: Number of threads within this CPU.
279 * @running: #true if CPU is currently running (lockless).
280 * @has_waiter: #true if a CPU is currently waiting for the cpu_exec_end;
281 * valid under cpu_list_lock.
282 * @created: Indicates whether the CPU thread has been successfully created.
283 * @interrupt_request: Indicates a pending interrupt request.
284 * @halted: Nonzero if the CPU is in suspended state.
285 * @stop: Indicates a pending stop request.
286 * @stopped: Indicates the CPU has been artificially stopped.
287 * @unplug: Indicates a pending CPU unplug request.
288 * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU
289 * @singlestep_enabled: Flags for single-stepping.
290 * @icount_extra: Instructions until next timer event.
291 * @icount_decr: Low 16 bits: number of cycles left, only used in icount mode.
292 * High 16 bits: Set to -1 to force TCG to stop executing linked TBs for this
293 * CPU and return to its top level loop (even in non-icount mode).
294 * This allows a single read-compare-cbranch-write sequence to test
295 * for both decrementer underflow and exceptions.
296 * @can_do_io: Nonzero if memory-mapped IO is safe. Deterministic execution
297 * requires that IO only be performed on the last instruction of a TB
298 * so that interrupts take effect immediately.
299 * @cpu_ases: Pointer to array of CPUAddressSpaces (which define the
300 * AddressSpaces this CPU has)
301 * @num_ases: number of CPUAddressSpaces in @cpu_ases
302 * @as: Pointer to the first AddressSpace, for the convenience of targets which
303 * only have a single AddressSpace
304 * @env_ptr: Pointer to subclass-specific CPUArchState field.
305 * @gdb_regs: Additional GDB registers.
306 * @gdb_num_regs: Number of total registers accessible to GDB.
307 * @gdb_num_g_regs: Number of registers in GDB 'g' packets.
308 * @next_cpu: Next CPU sharing TB cache.
309 * @opaque: User data.
310 * @mem_io_pc: Host Program Counter at which the memory was accessed.
311 * @mem_io_vaddr: Target virtual address at which the memory was accessed.
312 * @kvm_fd: vCPU file descriptor for KVM.
313 * @work_mutex: Lock to prevent multiple access to queued_work_*.
314 * @queued_work_first: First asynchronous work pending.
315 * @trace_dstate_delayed: Delayed changes to trace_dstate (includes all changes
317 * @trace_dstate: Dynamic tracing state of events for this vCPU (bitmask).
318 * @ignore_memory_transaction_failures: Cached copy of the MachineState
319 * flag of the same name: allows the board to suppress calling of the
320 * CPU do_transaction_failed hook function.
322 * State of one CPU core or thread.
326 DeviceState parent_obj
;
332 struct QemuThread
*thread
;
337 bool running
, has_waiter
;
338 struct QemuCond
*halt_cond
;
346 uint32_t cflags_next_tb
;
347 /* updates protected by BQL */
348 uint32_t interrupt_request
;
349 int singlestep_enabled
;
350 int64_t icount_budget
;
351 int64_t icount_extra
;
354 QemuMutex work_mutex
;
355 struct qemu_work_item
*queued_work_first
, *queued_work_last
;
357 CPUAddressSpace
*cpu_ases
;
360 MemoryRegion
*memory
;
362 void *env_ptr
; /* CPUArchState */
364 /* Accessed in parallel; all accesses must be atomic */
365 struct TranslationBlock
*tb_jmp_cache
[TB_JMP_CACHE_SIZE
];
367 struct GDBRegisterState
*gdb_regs
;
370 QTAILQ_ENTRY(CPUState
) node
;
372 /* ice debug support */
373 QTAILQ_HEAD(breakpoints_head
, CPUBreakpoint
) breakpoints
;
375 QTAILQ_HEAD(watchpoints_head
, CPUWatchpoint
) watchpoints
;
376 CPUWatchpoint
*watchpoint_hit
;
380 /* In order to avoid passing too many arguments to the MMIO helpers,
381 * we store some rarely used information in the CPU context.
387 struct KVMState
*kvm_state
;
388 struct kvm_run
*kvm_run
;
390 /* Used for events with 'vcpu' and *without* the 'disabled' properties */
391 DECLARE_BITMAP(trace_dstate_delayed
, CPU_TRACE_DSTATE_MAX_EVENTS
);
392 DECLARE_BITMAP(trace_dstate
, CPU_TRACE_DSTATE_MAX_EVENTS
);
394 /* TODO Move common fields from CPUArchState here. */
398 int32_t exception_index
;
400 /* shared by kvm, hax and hvf */
403 /* Used to keep track of an outstanding cpu throttle thread for migration
406 bool throttle_thread_scheduled
;
408 bool ignore_memory_transaction_failures
;
410 /* Note that this is accessed at the start of every TB via a negative
411 offset from AREG0. Leave this field at the end so as to make the
412 (absolute value) offset as small as possible. This reduces code
413 size, especially for hosts without large memory offsets. */
419 struct hax_vcpu_state
*hax_vcpu
;
421 /* The pending_tlb_flush flag is set and cleared atomically to
422 * avoid potential races. The aim of the flag is to avoid
423 * unnecessary flushes.
425 uint16_t pending_tlb_flush
;
430 QTAILQ_HEAD(CPUTailQ
, CPUState
);
431 extern struct CPUTailQ cpus
;
432 #define CPU_NEXT(cpu) QTAILQ_NEXT(cpu, node)
433 #define CPU_FOREACH(cpu) QTAILQ_FOREACH(cpu, &cpus, node)
434 #define CPU_FOREACH_SAFE(cpu, next_cpu) \
435 QTAILQ_FOREACH_SAFE(cpu, &cpus, node, next_cpu)
436 #define CPU_FOREACH_REVERSE(cpu) \
437 QTAILQ_FOREACH_REVERSE(cpu, &cpus, CPUTailQ, node)
438 #define first_cpu QTAILQ_FIRST(&cpus)
440 extern __thread CPUState
*current_cpu
;
442 static inline void cpu_tb_jmp_cache_clear(CPUState
*cpu
)
446 for (i
= 0; i
< TB_JMP_CACHE_SIZE
; i
++) {
447 atomic_set(&cpu
->tb_jmp_cache
[i
], NULL
);
452 * qemu_tcg_mttcg_enabled:
453 * Check whether we are running MultiThread TCG or not.
455 * Returns: %true if we are in MTTCG mode %false otherwise.
457 extern bool mttcg_enabled
;
458 #define qemu_tcg_mttcg_enabled() (mttcg_enabled)
461 * cpu_paging_enabled:
462 * @cpu: The CPU whose state is to be inspected.
464 * Returns: %true if paging is enabled, %false otherwise.
466 bool cpu_paging_enabled(const CPUState
*cpu
);
469 * cpu_get_memory_mapping:
470 * @cpu: The CPU whose memory mappings are to be obtained.
471 * @list: Where to write the memory mappings to.
472 * @errp: Pointer for reporting an #Error.
474 void cpu_get_memory_mapping(CPUState
*cpu
, MemoryMappingList
*list
,
478 * cpu_write_elf64_note:
479 * @f: pointer to a function that writes memory to a file
480 * @cpu: The CPU whose memory is to be dumped
481 * @cpuid: ID number of the CPU
482 * @opaque: pointer to the CPUState struct
484 int cpu_write_elf64_note(WriteCoreDumpFunction f
, CPUState
*cpu
,
485 int cpuid
, void *opaque
);
488 * cpu_write_elf64_qemunote:
489 * @f: pointer to a function that writes memory to a file
490 * @cpu: The CPU whose memory is to be dumped
491 * @cpuid: ID number of the CPU
492 * @opaque: pointer to the CPUState struct
494 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f
, CPUState
*cpu
,
498 * cpu_write_elf32_note:
499 * @f: pointer to a function that writes memory to a file
500 * @cpu: The CPU whose memory is to be dumped
501 * @cpuid: ID number of the CPU
502 * @opaque: pointer to the CPUState struct
504 int cpu_write_elf32_note(WriteCoreDumpFunction f
, CPUState
*cpu
,
505 int cpuid
, void *opaque
);
508 * cpu_write_elf32_qemunote:
509 * @f: pointer to a function that writes memory to a file
510 * @cpu: The CPU whose memory is to be dumped
511 * @cpuid: ID number of the CPU
512 * @opaque: pointer to the CPUState struct
514 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f
, CPUState
*cpu
,
518 * cpu_get_crash_info:
519 * @cpu: The CPU to get crash information for
521 * Gets the previously saved crash information.
522 * Caller is responsible for freeing the data.
524 GuestPanicInformation
*cpu_get_crash_info(CPUState
*cpu
);
529 * @CPU_DUMP_FPU: dump FPU register state, not just integer
530 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
533 CPU_DUMP_CODE
= 0x00010000,
534 CPU_DUMP_FPU
= 0x00020000,
535 CPU_DUMP_CCOP
= 0x00040000,
540 * @cpu: The CPU whose state is to be dumped.
541 * @f: File to dump to.
542 * @cpu_fprintf: Function to dump with.
543 * @flags: Flags what to dump.
547 void cpu_dump_state(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
551 * cpu_dump_statistics:
552 * @cpu: The CPU whose state is to be dumped.
553 * @f: File to dump to.
554 * @cpu_fprintf: Function to dump with.
555 * @flags: Flags what to dump.
557 * Dumps CPU statistics.
559 void cpu_dump_statistics(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
562 #ifndef CONFIG_USER_ONLY
564 * cpu_get_phys_page_attrs_debug:
565 * @cpu: The CPU to obtain the physical page address for.
566 * @addr: The virtual address.
567 * @attrs: Updated on return with the memory transaction attributes to use
570 * Obtains the physical page corresponding to a virtual one, together
571 * with the corresponding memory transaction attributes to use for the access.
572 * Use it only for debugging because no protection checks are done.
574 * Returns: Corresponding physical page address or -1 if no page found.
576 static inline hwaddr
cpu_get_phys_page_attrs_debug(CPUState
*cpu
, vaddr addr
,
579 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
581 if (cc
->get_phys_page_attrs_debug
) {
582 return cc
->get_phys_page_attrs_debug(cpu
, addr
, attrs
);
584 /* Fallback for CPUs which don't implement the _attrs_ hook */
585 *attrs
= MEMTXATTRS_UNSPECIFIED
;
586 return cc
->get_phys_page_debug(cpu
, addr
);
590 * cpu_get_phys_page_debug:
591 * @cpu: The CPU to obtain the physical page address for.
592 * @addr: The virtual address.
594 * Obtains the physical page corresponding to a virtual one.
595 * Use it only for debugging because no protection checks are done.
597 * Returns: Corresponding physical page address or -1 if no page found.
599 static inline hwaddr
cpu_get_phys_page_debug(CPUState
*cpu
, vaddr addr
)
601 MemTxAttrs attrs
= {};
603 return cpu_get_phys_page_attrs_debug(cpu
, addr
, &attrs
);
606 /** cpu_asidx_from_attrs:
608 * @attrs: memory transaction attributes
610 * Returns the address space index specifying the CPU AddressSpace
611 * to use for a memory access with the given transaction attributes.
613 static inline int cpu_asidx_from_attrs(CPUState
*cpu
, MemTxAttrs attrs
)
615 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
617 if (cc
->asidx_from_attrs
) {
618 return cc
->asidx_from_attrs(cpu
, attrs
);
626 * @cpu: The CPU to be added to the list of CPUs.
628 void cpu_list_add(CPUState
*cpu
);
632 * @cpu: The CPU to be removed from the list of CPUs.
634 void cpu_list_remove(CPUState
*cpu
);
638 * @cpu: The CPU whose state is to be reset.
640 void cpu_reset(CPUState
*cpu
);
644 * @typename: The CPU base type.
645 * @cpu_model: The model string without any parameters.
647 * Looks up a CPU #ObjectClass matching name @cpu_model.
649 * Returns: A #CPUClass or %NULL if not matching class is found.
651 ObjectClass
*cpu_class_by_name(const char *typename
, const char *cpu_model
);
655 * @typename: The CPU type.
657 * Instantiates a CPU and realizes the CPU.
659 * Returns: A #CPUState or %NULL if an error occurred.
661 CPUState
*cpu_create(const char *typename
);
664 * cpu_parse_cpu_model:
665 * @typename: The CPU base type or CPU type.
666 * @cpu_model: The model string including optional parameters.
668 * processes optional parameters and registers them as global properties
670 * Returns: type of CPU to create or prints error and terminates process
671 * if an error occurred.
673 const char *cpu_parse_cpu_model(const char *typename
, const char *cpu_model
);
677 * @typename: The CPU base type.
678 * @cpu_model: The model string including optional parameters.
680 * Instantiates a CPU, processes optional parameters and realizes the CPU.
682 * Returns: A #CPUState or %NULL if an error occurred.
684 CPUState
*cpu_generic_init(const char *typename
, const char *cpu_model
);
688 * @cpu: The vCPU to check.
690 * Checks whether the CPU has work to do.
692 * Returns: %true if the CPU has work, %false otherwise.
694 static inline bool cpu_has_work(CPUState
*cpu
)
696 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
698 g_assert(cc
->has_work
);
699 return cc
->has_work(cpu
);
704 * @cpu: The vCPU to check against.
706 * Checks whether the caller is executing on the vCPU thread.
708 * Returns: %true if called from @cpu's thread, %false otherwise.
710 bool qemu_cpu_is_self(CPUState
*cpu
);
714 * @cpu: The vCPU to kick.
716 * Kicks @cpu's thread.
718 void qemu_cpu_kick(CPUState
*cpu
);
722 * @cpu: The CPU to check.
724 * Checks whether the CPU is stopped.
726 * Returns: %true if run state is not running or if artificially stopped;
729 bool cpu_is_stopped(CPUState
*cpu
);
733 * @cpu: The vCPU to run on.
734 * @func: The function to be executed.
735 * @data: Data to pass to the function.
736 * @mutex: Mutex to release while waiting for @func to run.
738 * Used internally in the implementation of run_on_cpu.
740 void do_run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, run_on_cpu_data data
,
745 * @cpu: The vCPU to run on.
746 * @func: The function to be executed.
747 * @data: Data to pass to the function.
749 * Schedules the function @func for execution on the vCPU @cpu.
751 void run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, run_on_cpu_data data
);
755 * @cpu: The vCPU to run on.
756 * @func: The function to be executed.
757 * @data: Data to pass to the function.
759 * Schedules the function @func for execution on the vCPU @cpu asynchronously.
761 void async_run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, run_on_cpu_data data
);
764 * async_safe_run_on_cpu:
765 * @cpu: The vCPU to run on.
766 * @func: The function to be executed.
767 * @data: Data to pass to the function.
769 * Schedules the function @func for execution on the vCPU @cpu asynchronously,
770 * while all other vCPUs are sleeping.
772 * Unlike run_on_cpu and async_run_on_cpu, the function is run outside the
775 void async_safe_run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, run_on_cpu_data data
);
779 * @index: The CPUState@cpu_index value of the CPU to obtain.
781 * Gets a CPU matching @index.
783 * Returns: The CPU or %NULL if there is no matching CPU.
785 CPUState
*qemu_get_cpu(int index
);
789 * @id: Guest-exposed CPU ID to lookup.
791 * Search for CPU with specified ID.
793 * Returns: %true - CPU is found, %false - CPU isn't found.
795 bool cpu_exists(int64_t id
);
799 * @id: Guest-exposed CPU ID of the CPU to obtain.
801 * Get a CPU with matching @id.
803 * Returns: The CPU or %NULL if there is no matching CPU.
805 CPUState
*cpu_by_arch_id(int64_t id
);
809 * @new_throttle_pct: Percent of sleep time. Valid range is 1 to 99.
811 * Throttles all vcpus by forcing them to sleep for the given percentage of
812 * time. A throttle_percentage of 25 corresponds to a 75% duty cycle roughly.
813 * (example: 10ms sleep for every 30ms awake).
815 * cpu_throttle_set can be called as needed to adjust new_throttle_pct.
816 * Once the throttling starts, it will remain in effect until cpu_throttle_stop
819 void cpu_throttle_set(int new_throttle_pct
);
824 * Stops the vcpu throttling started by cpu_throttle_set.
826 void cpu_throttle_stop(void);
829 * cpu_throttle_active:
831 * Returns: %true if the vcpus are currently being throttled, %false otherwise.
833 bool cpu_throttle_active(void);
836 * cpu_throttle_get_percentage:
838 * Returns the vcpu throttle percentage. See cpu_throttle_set for details.
840 * Returns: The throttle percentage in range 1 to 99.
842 int cpu_throttle_get_percentage(void);
844 #ifndef CONFIG_USER_ONLY
846 typedef void (*CPUInterruptHandler
)(CPUState
*, int);
848 extern CPUInterruptHandler cpu_interrupt_handler
;
852 * @cpu: The CPU to set an interrupt on.
853 * @mask: The interupts to set.
855 * Invokes the interrupt handler.
857 static inline void cpu_interrupt(CPUState
*cpu
, int mask
)
859 cpu_interrupt_handler(cpu
, mask
);
862 #else /* USER_ONLY */
864 void cpu_interrupt(CPUState
*cpu
, int mask
);
866 #endif /* USER_ONLY */
870 #ifdef CONFIG_SOFTMMU
871 static inline void cpu_unassigned_access(CPUState
*cpu
, hwaddr addr
,
872 bool is_write
, bool is_exec
,
873 int opaque
, unsigned size
)
875 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
877 if (cc
->do_unassigned_access
) {
878 cc
->do_unassigned_access(cpu
, addr
, is_write
, is_exec
, opaque
, size
);
882 static inline void cpu_unaligned_access(CPUState
*cpu
, vaddr addr
,
883 MMUAccessType access_type
,
884 int mmu_idx
, uintptr_t retaddr
)
886 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
888 cc
->do_unaligned_access(cpu
, addr
, access_type
, mmu_idx
, retaddr
);
891 static inline void cpu_transaction_failed(CPUState
*cpu
, hwaddr physaddr
,
892 vaddr addr
, unsigned size
,
893 MMUAccessType access_type
,
894 int mmu_idx
, MemTxAttrs attrs
,
895 MemTxResult response
,
898 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
900 if (!cpu
->ignore_memory_transaction_failures
&& cc
->do_transaction_failed
) {
901 cc
->do_transaction_failed(cpu
, physaddr
, addr
, size
, access_type
,
902 mmu_idx
, attrs
, response
, retaddr
);
907 #endif /* NEED_CPU_H */
911 * @cpu: The CPU to set the program counter for.
912 * @addr: Program counter value.
914 * Sets the program counter for a CPU.
916 static inline void cpu_set_pc(CPUState
*cpu
, vaddr addr
)
918 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
920 cc
->set_pc(cpu
, addr
);
924 * cpu_reset_interrupt:
925 * @cpu: The CPU to clear the interrupt on.
926 * @mask: The interrupt mask to clear.
928 * Resets interrupts on the vCPU @cpu.
930 void cpu_reset_interrupt(CPUState
*cpu
, int mask
);
934 * @cpu: The CPU to exit.
936 * Requests the CPU @cpu to exit execution.
938 void cpu_exit(CPUState
*cpu
);
942 * @cpu: The CPU to resume.
944 * Resumes CPU, i.e. puts CPU into runnable state.
946 void cpu_resume(CPUState
*cpu
);
950 * @cpu: The CPU to remove.
952 * Requests the CPU to be removed.
954 void cpu_remove(CPUState
*cpu
);
958 * @cpu: The CPU to remove.
960 * Requests the CPU to be removed and waits till it is removed.
962 void cpu_remove_sync(CPUState
*cpu
);
965 * process_queued_cpu_work() - process all items on CPU work queue
966 * @cpu: The CPU which work queue to process.
968 void process_queued_cpu_work(CPUState
*cpu
);
972 * @cpu: The CPU for the current thread.
974 * Record that a CPU has started execution and can be interrupted with
977 void cpu_exec_start(CPUState
*cpu
);
981 * @cpu: The CPU for the current thread.
983 * Record that a CPU has stopped execution and exclusive sections
984 * can be executed without interrupting it.
986 void cpu_exec_end(CPUState
*cpu
);
991 * Wait for a concurrent exclusive section to end, and then start
992 * a section of work that is run while other CPUs are not running
993 * between cpu_exec_start and cpu_exec_end. CPUs that are running
994 * cpu_exec are exited immediately. CPUs that call cpu_exec_start
995 * during the exclusive section go to sleep until this CPU calls
998 void start_exclusive(void);
1003 * Concludes an exclusive execution section started by start_exclusive.
1005 void end_exclusive(void);
1009 * @cpu: The vCPU to initialize.
1011 * Initializes a vCPU.
1013 void qemu_init_vcpu(CPUState
*cpu
);
1015 #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
1016 #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
1017 #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
1021 * @cpu: CPU to the flags for.
1022 * @enabled: Flags to enable.
1024 * Enables or disables single-stepping for @cpu.
1026 void cpu_single_step(CPUState
*cpu
, int enabled
);
1028 /* Breakpoint/watchpoint flags */
1029 #define BP_MEM_READ 0x01
1030 #define BP_MEM_WRITE 0x02
1031 #define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE)
1032 #define BP_STOP_BEFORE_ACCESS 0x04
1033 /* 0x08 currently unused */
1036 #define BP_ANY (BP_GDB | BP_CPU)
1037 #define BP_WATCHPOINT_HIT_READ 0x40
1038 #define BP_WATCHPOINT_HIT_WRITE 0x80
1039 #define BP_WATCHPOINT_HIT (BP_WATCHPOINT_HIT_READ | BP_WATCHPOINT_HIT_WRITE)
1041 int cpu_breakpoint_insert(CPUState
*cpu
, vaddr pc
, int flags
,
1042 CPUBreakpoint
**breakpoint
);
1043 int cpu_breakpoint_remove(CPUState
*cpu
, vaddr pc
, int flags
);
1044 void cpu_breakpoint_remove_by_ref(CPUState
*cpu
, CPUBreakpoint
*breakpoint
);
1045 void cpu_breakpoint_remove_all(CPUState
*cpu
, int mask
);
1047 /* Return true if PC matches an installed breakpoint. */
1048 static inline bool cpu_breakpoint_test(CPUState
*cpu
, vaddr pc
, int mask
)
1052 if (unlikely(!QTAILQ_EMPTY(&cpu
->breakpoints
))) {
1053 QTAILQ_FOREACH(bp
, &cpu
->breakpoints
, entry
) {
1054 if (bp
->pc
== pc
&& (bp
->flags
& mask
)) {
1062 int cpu_watchpoint_insert(CPUState
*cpu
, vaddr addr
, vaddr len
,
1063 int flags
, CPUWatchpoint
**watchpoint
);
1064 int cpu_watchpoint_remove(CPUState
*cpu
, vaddr addr
,
1065 vaddr len
, int flags
);
1066 void cpu_watchpoint_remove_by_ref(CPUState
*cpu
, CPUWatchpoint
*watchpoint
);
1067 void cpu_watchpoint_remove_all(CPUState
*cpu
, int mask
);
1070 * cpu_get_address_space:
1071 * @cpu: CPU to get address space from
1072 * @asidx: index identifying which address space to get
1074 * Return the requested address space of this CPU. @asidx
1075 * specifies which address space to read.
1077 AddressSpace
*cpu_get_address_space(CPUState
*cpu
, int asidx
);
1079 void QEMU_NORETURN
cpu_abort(CPUState
*cpu
, const char *fmt
, ...)
1081 extern Property cpu_common_props
[];
1082 void cpu_exec_initfn(CPUState
*cpu
);
1083 void cpu_exec_realizefn(CPUState
*cpu
, Error
**errp
);
1084 void cpu_exec_unrealizefn(CPUState
*cpu
);
1088 #ifdef CONFIG_SOFTMMU
1089 extern const struct VMStateDescription vmstate_cpu_common
;
1091 #define vmstate_cpu_common vmstate_dummy
1094 #define VMSTATE_CPU() { \
1095 .name = "parent_obj", \
1096 .size = sizeof(CPUState), \
1097 .vmsd = &vmstate_cpu_common, \
1098 .flags = VMS_STRUCT, \
1102 #endif /* NEED_CPU_H */
1104 #define UNASSIGNED_CPU_INDEX -1