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/dis-asm.h"
25 #include "exec/hwaddr.h"
26 #include "exec/memattrs.h"
27 #include "qapi/qapi-types-run-state.h"
28 #include "qemu/bitmap.h"
29 #include "qemu/rcu_queue.h"
30 #include "qemu/queue.h"
31 #include "qemu/thread.h"
32 #include "qemu/plugin.h"
34 typedef int (*WriteCoreDumpFunction
)(const void *buf
, size_t size
,
39 * Type wide enough to contain any #target_ulong virtual address.
41 typedef uint64_t vaddr
;
42 #define VADDR_PRId PRId64
43 #define VADDR_PRIu PRIu64
44 #define VADDR_PRIo PRIo64
45 #define VADDR_PRIx PRIx64
46 #define VADDR_PRIX PRIX64
47 #define VADDR_MAX UINT64_MAX
51 * @section_id: QEMU-cpu
53 * @short_description: Base class for all CPUs
56 #define TYPE_CPU "cpu"
58 /* Since this macro is used a lot in hot code paths and in conjunction with
59 * FooCPU *foo_env_get_cpu(), we deviate from usual QOM practice by using
62 #define CPU(obj) ((CPUState *)(obj))
64 #define CPU_CLASS(class) OBJECT_CLASS_CHECK(CPUClass, (class), TYPE_CPU)
65 #define CPU_GET_CLASS(obj) OBJECT_GET_CLASS(CPUClass, (obj), TYPE_CPU)
67 typedef enum MMUAccessType
{
73 typedef struct CPUWatchpoint CPUWatchpoint
;
75 struct TranslationBlock
;
79 * @class_by_name: Callback to map -cpu command line model name to an
80 * instantiatable CPU type.
81 * @parse_features: Callback to parse command line arguments.
82 * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
83 * @has_work: Callback for checking if there is work to do.
84 * @do_interrupt: Callback for interrupt handling.
85 * @do_unaligned_access: Callback for unaligned access handling, if
86 * the target defines #TARGET_ALIGNED_ONLY.
87 * @do_transaction_failed: Callback for handling failed memory transactions
88 * (ie bus faults or external aborts; not MMU faults)
89 * @virtio_is_big_endian: Callback to return %true if a CPU which supports
90 * runtime configurable endianness is currently big-endian. Non-configurable
91 * CPUs can use the default implementation of this method. This method should
92 * not be used by any callers other than the pre-1.0 virtio devices.
93 * @memory_rw_debug: Callback for GDB memory access.
94 * @dump_state: Callback for dumping state.
95 * @dump_statistics: Callback for dumping statistics.
96 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
97 * @get_paging_enabled: Callback for inquiring whether paging is enabled.
98 * @get_memory_mapping: Callback for obtaining the memory mappings.
99 * @set_pc: Callback for setting the Program Counter register. This
100 * should have the semantics used by the target architecture when
101 * setting the PC from a source such as an ELF file entry point;
102 * for example on Arm it will also set the Thumb mode bit based
103 * on the least significant bit of the new PC value.
104 * If the target behaviour here is anything other than "set
105 * the PC register to the value passed in" then the target must
106 * also implement the synchronize_from_tb hook.
107 * @synchronize_from_tb: Callback for synchronizing state from a TCG
108 * #TranslationBlock. This is called when we abandon execution
109 * of a TB before starting it, and must set all parts of the CPU
110 * state which the previous TB in the chain may not have updated.
111 * This always includes at least the program counter; some targets
112 * will need to do more. If this hook is not implemented then the
113 * default is to call @set_pc(tb->pc).
114 * @tlb_fill: Callback for handling a softmmu tlb miss or user-only
115 * address fault. For system mode, if the access is valid, call
116 * tlb_set_page and return true; if the access is invalid, and
117 * probe is true, return false; otherwise raise an exception and
118 * do not return. For user-only mode, always raise an exception
120 * @get_phys_page_debug: Callback for obtaining a physical address.
121 * @get_phys_page_attrs_debug: Callback for obtaining a physical address and the
122 * associated memory transaction attributes to use for the access.
123 * CPUs which use memory transaction attributes should implement this
124 * instead of get_phys_page_debug.
125 * @asidx_from_attrs: Callback to return the CPU AddressSpace to use for
126 * a memory access with the specified memory transaction attributes.
127 * @gdb_read_register: Callback for letting GDB read a register.
128 * @gdb_write_register: Callback for letting GDB write a register.
129 * @debug_check_watchpoint: Callback: return true if the architectural
130 * watchpoint whose address has matched should really fire.
131 * @debug_excp_handler: Callback for handling debug exceptions.
132 * @write_elf64_note: Callback for writing a CPU-specific ELF note to a
133 * 64-bit VM coredump.
134 * @write_elf32_qemunote: Callback for writing a CPU- and QEMU-specific ELF
135 * note to a 32-bit VM coredump.
136 * @write_elf32_note: Callback for writing a CPU-specific ELF note to a
137 * 32-bit VM coredump.
138 * @write_elf32_qemunote: Callback for writing a CPU- and QEMU-specific ELF
139 * note to a 32-bit VM coredump.
140 * @vmsd: State description for migration.
141 * @gdb_num_core_regs: Number of core registers accessible to GDB.
142 * @gdb_core_xml_file: File name for core registers GDB XML description.
143 * @gdb_stop_before_watchpoint: Indicates whether GDB expects the CPU to stop
144 * before the insn which triggers a watchpoint rather than after it.
145 * @gdb_arch_name: Optional callback that returns the architecture name known
146 * to GDB. The caller must free the returned string with g_free.
147 * @gdb_get_dynamic_xml: Callback to return dynamically generated XML for the
148 * gdb stub. Returns a pointer to the XML contents for the specified XML file
149 * or NULL if the CPU doesn't have a dynamically generated content for it.
150 * @cpu_exec_enter: Callback for cpu_exec preparation.
151 * @cpu_exec_exit: Callback for cpu_exec cleanup.
152 * @cpu_exec_interrupt: Callback for processing interrupts in cpu_exec.
153 * @disas_set_info: Setup architecture specific components of disassembly info
154 * @adjust_watchpoint_address: Perform a target-specific adjustment to an
155 * address before attempting to match it against watchpoints.
157 * Represents a CPU family or model.
159 typedef struct CPUClass
{
161 DeviceClass parent_class
;
164 ObjectClass
*(*class_by_name
)(const char *cpu_model
);
165 void (*parse_features
)(const char *typename
, char *str
, Error
**errp
);
167 int reset_dump_flags
;
168 bool (*has_work
)(CPUState
*cpu
);
169 void (*do_interrupt
)(CPUState
*cpu
);
170 void (*do_unaligned_access
)(CPUState
*cpu
, vaddr addr
,
171 MMUAccessType access_type
,
172 int mmu_idx
, uintptr_t retaddr
);
173 void (*do_transaction_failed
)(CPUState
*cpu
, hwaddr physaddr
, vaddr addr
,
174 unsigned size
, MMUAccessType access_type
,
175 int mmu_idx
, MemTxAttrs attrs
,
176 MemTxResult response
, uintptr_t retaddr
);
177 bool (*virtio_is_big_endian
)(CPUState
*cpu
);
178 int (*memory_rw_debug
)(CPUState
*cpu
, vaddr addr
,
179 uint8_t *buf
, int len
, bool is_write
);
180 void (*dump_state
)(CPUState
*cpu
, FILE *, int flags
);
181 GuestPanicInformation
* (*get_crash_info
)(CPUState
*cpu
);
182 void (*dump_statistics
)(CPUState
*cpu
, int flags
);
183 int64_t (*get_arch_id
)(CPUState
*cpu
);
184 bool (*get_paging_enabled
)(const CPUState
*cpu
);
185 void (*get_memory_mapping
)(CPUState
*cpu
, MemoryMappingList
*list
,
187 void (*set_pc
)(CPUState
*cpu
, vaddr value
);
188 void (*synchronize_from_tb
)(CPUState
*cpu
, struct TranslationBlock
*tb
);
189 bool (*tlb_fill
)(CPUState
*cpu
, vaddr address
, int size
,
190 MMUAccessType access_type
, int mmu_idx
,
191 bool probe
, uintptr_t retaddr
);
192 hwaddr (*get_phys_page_debug
)(CPUState
*cpu
, vaddr addr
);
193 hwaddr (*get_phys_page_attrs_debug
)(CPUState
*cpu
, vaddr addr
,
195 int (*asidx_from_attrs
)(CPUState
*cpu
, MemTxAttrs attrs
);
196 int (*gdb_read_register
)(CPUState
*cpu
, GByteArray
*buf
, int reg
);
197 int (*gdb_write_register
)(CPUState
*cpu
, uint8_t *buf
, int reg
);
198 bool (*debug_check_watchpoint
)(CPUState
*cpu
, CPUWatchpoint
*wp
);
199 void (*debug_excp_handler
)(CPUState
*cpu
);
201 int (*write_elf64_note
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
202 int cpuid
, void *opaque
);
203 int (*write_elf64_qemunote
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
205 int (*write_elf32_note
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
206 int cpuid
, void *opaque
);
207 int (*write_elf32_qemunote
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
210 const VMStateDescription
*vmsd
;
211 const char *gdb_core_xml_file
;
212 gchar
* (*gdb_arch_name
)(CPUState
*cpu
);
213 const char * (*gdb_get_dynamic_xml
)(CPUState
*cpu
, const char *xmlname
);
214 void (*cpu_exec_enter
)(CPUState
*cpu
);
215 void (*cpu_exec_exit
)(CPUState
*cpu
);
216 bool (*cpu_exec_interrupt
)(CPUState
*cpu
, int interrupt_request
);
218 void (*disas_set_info
)(CPUState
*cpu
, disassemble_info
*info
);
219 vaddr (*adjust_watchpoint_address
)(CPUState
*cpu
, vaddr addr
, int len
);
220 void (*tcg_initialize
)(void);
222 /* Keep non-pointer data at the end to minimize holes. */
223 int gdb_num_core_regs
;
224 bool gdb_stop_before_watchpoint
;
228 * Low 16 bits: number of cycles left, used only in icount mode.
229 * High 16 bits: Set to -1 to force TCG to stop executing linked TBs
230 * for this CPU and return to its top level loop (even in non-icount mode).
231 * This allows a single read-compare-cbranch-write sequence to test
232 * for both decrementer underflow and exceptions.
234 typedef union IcountDecr
{
237 #ifdef HOST_WORDS_BIGENDIAN
247 typedef struct CPUBreakpoint
{
249 int flags
; /* BP_* */
250 QTAILQ_ENTRY(CPUBreakpoint
) entry
;
253 struct CPUWatchpoint
{
258 int flags
; /* BP_* */
259 QTAILQ_ENTRY(CPUWatchpoint
) entry
;
265 struct hax_vcpu_state
;
267 #define TB_JMP_CACHE_BITS 12
268 #define TB_JMP_CACHE_SIZE (1 << TB_JMP_CACHE_BITS)
272 /* The union type allows passing of 64 bit target pointers on 32 bit
273 * hosts in a single parameter
277 unsigned long host_ulong
;
282 #define RUN_ON_CPU_HOST_PTR(p) ((run_on_cpu_data){.host_ptr = (p)})
283 #define RUN_ON_CPU_HOST_INT(i) ((run_on_cpu_data){.host_int = (i)})
284 #define RUN_ON_CPU_HOST_ULONG(ul) ((run_on_cpu_data){.host_ulong = (ul)})
285 #define RUN_ON_CPU_TARGET_PTR(v) ((run_on_cpu_data){.target_ptr = (v)})
286 #define RUN_ON_CPU_NULL RUN_ON_CPU_HOST_PTR(NULL)
288 typedef void (*run_on_cpu_func
)(CPUState
*cpu
, run_on_cpu_data data
);
290 struct qemu_work_item
;
292 #define CPU_UNSET_NUMA_NODE_ID -1
293 #define CPU_TRACE_DSTATE_MAX_EVENTS 32
297 * @cpu_index: CPU index (informative).
298 * @cluster_index: Identifies which cluster this CPU is in.
299 * For boards which don't define clusters or for "loose" CPUs not assigned
300 * to a cluster this will be UNASSIGNED_CLUSTER_INDEX; otherwise it will
301 * be the same as the cluster-id property of the CPU object's TYPE_CPU_CLUSTER
303 * @nr_cores: Number of cores within this CPU package.
304 * @nr_threads: Number of threads within this CPU.
305 * @running: #true if CPU is currently running (lockless).
306 * @has_waiter: #true if a CPU is currently waiting for the cpu_exec_end;
307 * valid under cpu_list_lock.
308 * @created: Indicates whether the CPU thread has been successfully created.
309 * @interrupt_request: Indicates a pending interrupt request.
310 * @halted: Nonzero if the CPU is in suspended state.
311 * @stop: Indicates a pending stop request.
312 * @stopped: Indicates the CPU has been artificially stopped.
313 * @unplug: Indicates a pending CPU unplug request.
314 * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU
315 * @singlestep_enabled: Flags for single-stepping.
316 * @icount_extra: Instructions until next timer event.
317 * @can_do_io: Nonzero if memory-mapped IO is safe. Deterministic execution
318 * requires that IO only be performed on the last instruction of a TB
319 * so that interrupts take effect immediately.
320 * @cpu_ases: Pointer to array of CPUAddressSpaces (which define the
321 * AddressSpaces this CPU has)
322 * @num_ases: number of CPUAddressSpaces in @cpu_ases
323 * @as: Pointer to the first AddressSpace, for the convenience of targets which
324 * only have a single AddressSpace
325 * @env_ptr: Pointer to subclass-specific CPUArchState field.
326 * @icount_decr_ptr: Pointer to IcountDecr field within subclass.
327 * @gdb_regs: Additional GDB registers.
328 * @gdb_num_regs: Number of total registers accessible to GDB.
329 * @gdb_num_g_regs: Number of registers in GDB 'g' packets.
330 * @next_cpu: Next CPU sharing TB cache.
331 * @opaque: User data.
332 * @mem_io_pc: Host Program Counter at which the memory was accessed.
333 * @kvm_fd: vCPU file descriptor for KVM.
334 * @work_mutex: Lock to prevent multiple access to queued_work_*.
335 * @queued_work_first: First asynchronous work pending.
336 * @trace_dstate_delayed: Delayed changes to trace_dstate (includes all changes
338 * @trace_dstate: Dynamic tracing state of events for this vCPU (bitmask).
339 * @plugin_mask: Plugin event bitmap. Modified only via async work.
340 * @ignore_memory_transaction_failures: Cached copy of the MachineState
341 * flag of the same name: allows the board to suppress calling of the
342 * CPU do_transaction_failed hook function.
344 * State of one CPU core or thread.
348 DeviceState parent_obj
;
354 struct QemuThread
*thread
;
359 bool running
, has_waiter
;
360 struct QemuCond
*halt_cond
;
368 bool in_exclusive_context
;
369 uint32_t cflags_next_tb
;
370 /* updates protected by BQL */
371 uint32_t interrupt_request
;
372 int singlestep_enabled
;
373 int64_t icount_budget
;
374 int64_t icount_extra
;
375 uint64_t random_seed
;
378 QemuMutex work_mutex
;
379 struct qemu_work_item
*queued_work_first
, *queued_work_last
;
381 CPUAddressSpace
*cpu_ases
;
384 MemoryRegion
*memory
;
386 void *env_ptr
; /* CPUArchState */
387 IcountDecr
*icount_decr_ptr
;
389 /* Accessed in parallel; all accesses must be atomic */
390 struct TranslationBlock
*tb_jmp_cache
[TB_JMP_CACHE_SIZE
];
392 struct GDBRegisterState
*gdb_regs
;
395 QTAILQ_ENTRY(CPUState
) node
;
397 /* ice debug support */
398 QTAILQ_HEAD(, CPUBreakpoint
) breakpoints
;
400 QTAILQ_HEAD(, CPUWatchpoint
) watchpoints
;
401 CPUWatchpoint
*watchpoint_hit
;
405 /* In order to avoid passing too many arguments to the MMIO helpers,
406 * we store some rarely used information in the CPU context.
411 struct KVMState
*kvm_state
;
412 struct kvm_run
*kvm_run
;
414 /* Used for events with 'vcpu' and *without* the 'disabled' properties */
415 DECLARE_BITMAP(trace_dstate_delayed
, CPU_TRACE_DSTATE_MAX_EVENTS
);
416 DECLARE_BITMAP(trace_dstate
, CPU_TRACE_DSTATE_MAX_EVENTS
);
418 DECLARE_BITMAP(plugin_mask
, QEMU_PLUGIN_EV_MAX
);
420 GArray
*plugin_mem_cbs
;
422 /* TODO Move common fields from CPUArchState here. */
427 int32_t exception_index
;
429 /* shared by kvm, hax and hvf */
432 /* Used to keep track of an outstanding cpu throttle thread for migration
435 bool throttle_thread_scheduled
;
437 bool ignore_memory_transaction_failures
;
439 struct hax_vcpu_state
*hax_vcpu
;
443 /* track IOMMUs whose translations we've cached in the TCG TLB */
444 GArray
*iommu_notifiers
;
447 typedef QTAILQ_HEAD(CPUTailQ
, CPUState
) CPUTailQ
;
448 extern CPUTailQ cpus
;
450 #define first_cpu QTAILQ_FIRST_RCU(&cpus)
451 #define CPU_NEXT(cpu) QTAILQ_NEXT_RCU(cpu, node)
452 #define CPU_FOREACH(cpu) QTAILQ_FOREACH_RCU(cpu, &cpus, node)
453 #define CPU_FOREACH_SAFE(cpu, next_cpu) \
454 QTAILQ_FOREACH_SAFE_RCU(cpu, &cpus, node, next_cpu)
456 extern __thread CPUState
*current_cpu
;
458 static inline void cpu_tb_jmp_cache_clear(CPUState
*cpu
)
462 for (i
= 0; i
< TB_JMP_CACHE_SIZE
; i
++) {
463 atomic_set(&cpu
->tb_jmp_cache
[i
], NULL
);
468 * qemu_tcg_mttcg_enabled:
469 * Check whether we are running MultiThread TCG or not.
471 * Returns: %true if we are in MTTCG mode %false otherwise.
473 extern bool mttcg_enabled
;
474 #define qemu_tcg_mttcg_enabled() (mttcg_enabled)
477 * cpu_paging_enabled:
478 * @cpu: The CPU whose state is to be inspected.
480 * Returns: %true if paging is enabled, %false otherwise.
482 bool cpu_paging_enabled(const CPUState
*cpu
);
485 * cpu_get_memory_mapping:
486 * @cpu: The CPU whose memory mappings are to be obtained.
487 * @list: Where to write the memory mappings to.
488 * @errp: Pointer for reporting an #Error.
490 void cpu_get_memory_mapping(CPUState
*cpu
, MemoryMappingList
*list
,
493 #if !defined(CONFIG_USER_ONLY)
496 * cpu_write_elf64_note:
497 * @f: pointer to a function that writes memory to a file
498 * @cpu: The CPU whose memory is to be dumped
499 * @cpuid: ID number of the CPU
500 * @opaque: pointer to the CPUState struct
502 int cpu_write_elf64_note(WriteCoreDumpFunction f
, CPUState
*cpu
,
503 int cpuid
, void *opaque
);
506 * cpu_write_elf64_qemunote:
507 * @f: pointer to a function that writes memory to a file
508 * @cpu: The CPU whose memory is to be dumped
509 * @cpuid: ID number of the CPU
510 * @opaque: pointer to the CPUState struct
512 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f
, CPUState
*cpu
,
516 * cpu_write_elf32_note:
517 * @f: pointer to a function that writes memory to a file
518 * @cpu: The CPU whose memory is to be dumped
519 * @cpuid: ID number of the CPU
520 * @opaque: pointer to the CPUState struct
522 int cpu_write_elf32_note(WriteCoreDumpFunction f
, CPUState
*cpu
,
523 int cpuid
, void *opaque
);
526 * cpu_write_elf32_qemunote:
527 * @f: pointer to a function that writes memory to a file
528 * @cpu: The CPU whose memory is to be dumped
529 * @cpuid: ID number of the CPU
530 * @opaque: pointer to the CPUState struct
532 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f
, CPUState
*cpu
,
536 * cpu_get_crash_info:
537 * @cpu: The CPU to get crash information for
539 * Gets the previously saved crash information.
540 * Caller is responsible for freeing the data.
542 GuestPanicInformation
*cpu_get_crash_info(CPUState
*cpu
);
544 #endif /* !CONFIG_USER_ONLY */
549 * @CPU_DUMP_FPU: dump FPU register state, not just integer
550 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
553 CPU_DUMP_CODE
= 0x00010000,
554 CPU_DUMP_FPU
= 0x00020000,
555 CPU_DUMP_CCOP
= 0x00040000,
560 * @cpu: The CPU whose state is to be dumped.
561 * @f: If non-null, dump to this stream, else to current print sink.
565 void cpu_dump_state(CPUState
*cpu
, FILE *f
, int flags
);
568 * cpu_dump_statistics:
569 * @cpu: The CPU whose state is to be dumped.
570 * @flags: Flags what to dump.
572 * Dump CPU statistics to the current monitor if we have one, else to
575 void cpu_dump_statistics(CPUState
*cpu
, int flags
);
577 #ifndef CONFIG_USER_ONLY
579 * cpu_get_phys_page_attrs_debug:
580 * @cpu: The CPU to obtain the physical page address for.
581 * @addr: The virtual address.
582 * @attrs: Updated on return with the memory transaction attributes to use
585 * Obtains the physical page corresponding to a virtual one, together
586 * with the corresponding memory transaction attributes to use for the access.
587 * Use it only for debugging because no protection checks are done.
589 * Returns: Corresponding physical page address or -1 if no page found.
591 static inline hwaddr
cpu_get_phys_page_attrs_debug(CPUState
*cpu
, vaddr addr
,
594 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
596 if (cc
->get_phys_page_attrs_debug
) {
597 return cc
->get_phys_page_attrs_debug(cpu
, addr
, attrs
);
599 /* Fallback for CPUs which don't implement the _attrs_ hook */
600 *attrs
= MEMTXATTRS_UNSPECIFIED
;
601 return cc
->get_phys_page_debug(cpu
, addr
);
605 * cpu_get_phys_page_debug:
606 * @cpu: The CPU to obtain the physical page address for.
607 * @addr: The virtual address.
609 * Obtains the physical page corresponding to a virtual one.
610 * Use it only for debugging because no protection checks are done.
612 * Returns: Corresponding physical page address or -1 if no page found.
614 static inline hwaddr
cpu_get_phys_page_debug(CPUState
*cpu
, vaddr addr
)
616 MemTxAttrs attrs
= {};
618 return cpu_get_phys_page_attrs_debug(cpu
, addr
, &attrs
);
621 /** cpu_asidx_from_attrs:
623 * @attrs: memory transaction attributes
625 * Returns the address space index specifying the CPU AddressSpace
626 * to use for a memory access with the given transaction attributes.
628 static inline int cpu_asidx_from_attrs(CPUState
*cpu
, MemTxAttrs attrs
)
630 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
633 if (cc
->asidx_from_attrs
) {
634 ret
= cc
->asidx_from_attrs(cpu
, attrs
);
635 assert(ret
< cpu
->num_ases
&& ret
>= 0);
640 #endif /* CONFIG_USER_ONLY */
644 * @cpu: The CPU to be added to the list of CPUs.
646 void cpu_list_add(CPUState
*cpu
);
650 * @cpu: The CPU to be removed from the list of CPUs.
652 void cpu_list_remove(CPUState
*cpu
);
656 * @cpu: The CPU whose state is to be reset.
658 void cpu_reset(CPUState
*cpu
);
662 * @typename: The CPU base type.
663 * @cpu_model: The model string without any parameters.
665 * Looks up a CPU #ObjectClass matching name @cpu_model.
667 * Returns: A #CPUClass or %NULL if not matching class is found.
669 ObjectClass
*cpu_class_by_name(const char *typename
, const char *cpu_model
);
673 * @typename: The CPU type.
675 * Instantiates a CPU and realizes the CPU.
677 * Returns: A #CPUState or %NULL if an error occurred.
679 CPUState
*cpu_create(const char *typename
);
683 * @cpu_option: The -cpu option including optional parameters.
685 * processes optional parameters and registers them as global properties
687 * Returns: type of CPU to create or prints error and terminates process
688 * if an error occurred.
690 const char *parse_cpu_option(const char *cpu_option
);
694 * @cpu: The vCPU to check.
696 * Checks whether the CPU has work to do.
698 * Returns: %true if the CPU has work, %false otherwise.
700 static inline bool cpu_has_work(CPUState
*cpu
)
702 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
704 g_assert(cc
->has_work
);
705 return cc
->has_work(cpu
);
710 * @cpu: The vCPU to check against.
712 * Checks whether the caller is executing on the vCPU thread.
714 * Returns: %true if called from @cpu's thread, %false otherwise.
716 bool qemu_cpu_is_self(CPUState
*cpu
);
720 * @cpu: The vCPU to kick.
722 * Kicks @cpu's thread.
724 void qemu_cpu_kick(CPUState
*cpu
);
728 * @cpu: The CPU to check.
730 * Checks whether the CPU is stopped.
732 * Returns: %true if run state is not running or if artificially stopped;
735 bool cpu_is_stopped(CPUState
*cpu
);
739 * @cpu: The vCPU to run on.
740 * @func: The function to be executed.
741 * @data: Data to pass to the function.
742 * @mutex: Mutex to release while waiting for @func to run.
744 * Used internally in the implementation of run_on_cpu.
746 void do_run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, run_on_cpu_data data
,
751 * @cpu: The vCPU to run on.
752 * @func: The function to be executed.
753 * @data: Data to pass to the function.
755 * Schedules the function @func for execution on the vCPU @cpu.
757 void run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, run_on_cpu_data data
);
761 * @cpu: The vCPU to run on.
762 * @func: The function to be executed.
763 * @data: Data to pass to the function.
765 * Schedules the function @func for execution on the vCPU @cpu asynchronously.
767 void async_run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, run_on_cpu_data data
);
770 * async_safe_run_on_cpu:
771 * @cpu: The vCPU to run on.
772 * @func: The function to be executed.
773 * @data: Data to pass to the function.
775 * Schedules the function @func for execution on the vCPU @cpu asynchronously,
776 * while all other vCPUs are sleeping.
778 * Unlike run_on_cpu and async_run_on_cpu, the function is run outside the
781 void async_safe_run_on_cpu(CPUState
*cpu
, run_on_cpu_func func
, run_on_cpu_data data
);
784 * cpu_in_exclusive_context()
785 * @cpu: The vCPU to check
787 * Returns true if @cpu is an exclusive context, for example running
788 * something which has previously been queued via async_safe_run_on_cpu().
790 static inline bool cpu_in_exclusive_context(const CPUState
*cpu
)
792 return cpu
->in_exclusive_context
;
797 * @index: The CPUState@cpu_index value of the CPU to obtain.
799 * Gets a CPU matching @index.
801 * Returns: The CPU or %NULL if there is no matching CPU.
803 CPUState
*qemu_get_cpu(int index
);
807 * @id: Guest-exposed CPU ID to lookup.
809 * Search for CPU with specified ID.
811 * Returns: %true - CPU is found, %false - CPU isn't found.
813 bool cpu_exists(int64_t id
);
817 * @id: Guest-exposed CPU ID of the CPU to obtain.
819 * Get a CPU with matching @id.
821 * Returns: The CPU or %NULL if there is no matching CPU.
823 CPUState
*cpu_by_arch_id(int64_t id
);
827 * @new_throttle_pct: Percent of sleep time. Valid range is 1 to 99.
829 * Throttles all vcpus by forcing them to sleep for the given percentage of
830 * time. A throttle_percentage of 25 corresponds to a 75% duty cycle roughly.
831 * (example: 10ms sleep for every 30ms awake).
833 * cpu_throttle_set can be called as needed to adjust new_throttle_pct.
834 * Once the throttling starts, it will remain in effect until cpu_throttle_stop
837 void cpu_throttle_set(int new_throttle_pct
);
842 * Stops the vcpu throttling started by cpu_throttle_set.
844 void cpu_throttle_stop(void);
847 * cpu_throttle_active:
849 * Returns: %true if the vcpus are currently being throttled, %false otherwise.
851 bool cpu_throttle_active(void);
854 * cpu_throttle_get_percentage:
856 * Returns the vcpu throttle percentage. See cpu_throttle_set for details.
858 * Returns: The throttle percentage in range 1 to 99.
860 int cpu_throttle_get_percentage(void);
862 #ifndef CONFIG_USER_ONLY
864 typedef void (*CPUInterruptHandler
)(CPUState
*, int);
866 extern CPUInterruptHandler cpu_interrupt_handler
;
870 * @cpu: The CPU to set an interrupt on.
871 * @mask: The interrupts to set.
873 * Invokes the interrupt handler.
875 static inline void cpu_interrupt(CPUState
*cpu
, int mask
)
877 cpu_interrupt_handler(cpu
, mask
);
880 #else /* USER_ONLY */
882 void cpu_interrupt(CPUState
*cpu
, int mask
);
884 #endif /* USER_ONLY */
888 #ifdef CONFIG_SOFTMMU
889 static inline void cpu_unaligned_access(CPUState
*cpu
, vaddr addr
,
890 MMUAccessType access_type
,
891 int mmu_idx
, uintptr_t retaddr
)
893 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
895 cc
->do_unaligned_access(cpu
, addr
, access_type
, mmu_idx
, retaddr
);
898 static inline void cpu_transaction_failed(CPUState
*cpu
, hwaddr physaddr
,
899 vaddr addr
, unsigned size
,
900 MMUAccessType access_type
,
901 int mmu_idx
, MemTxAttrs attrs
,
902 MemTxResult response
,
905 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
907 if (!cpu
->ignore_memory_transaction_failures
&& cc
->do_transaction_failed
) {
908 cc
->do_transaction_failed(cpu
, physaddr
, addr
, size
, access_type
,
909 mmu_idx
, attrs
, response
, retaddr
);
914 #endif /* NEED_CPU_H */
918 * @cpu: The CPU to set the program counter for.
919 * @addr: Program counter value.
921 * Sets the program counter for a CPU.
923 static inline void cpu_set_pc(CPUState
*cpu
, vaddr addr
)
925 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
927 cc
->set_pc(cpu
, addr
);
931 * cpu_reset_interrupt:
932 * @cpu: The CPU to clear the interrupt on.
933 * @mask: The interrupt mask to clear.
935 * Resets interrupts on the vCPU @cpu.
937 void cpu_reset_interrupt(CPUState
*cpu
, int mask
);
941 * @cpu: The CPU to exit.
943 * Requests the CPU @cpu to exit execution.
945 void cpu_exit(CPUState
*cpu
);
949 * @cpu: The CPU to resume.
951 * Resumes CPU, i.e. puts CPU into runnable state.
953 void cpu_resume(CPUState
*cpu
);
957 * @cpu: The CPU to remove.
959 * Requests the CPU to be removed.
961 void cpu_remove(CPUState
*cpu
);
965 * @cpu: The CPU to remove.
967 * Requests the CPU to be removed and waits till it is removed.
969 void cpu_remove_sync(CPUState
*cpu
);
972 * process_queued_cpu_work() - process all items on CPU work queue
973 * @cpu: The CPU which work queue to process.
975 void process_queued_cpu_work(CPUState
*cpu
);
979 * @cpu: The CPU for the current thread.
981 * Record that a CPU has started execution and can be interrupted with
984 void cpu_exec_start(CPUState
*cpu
);
988 * @cpu: The CPU for the current thread.
990 * Record that a CPU has stopped execution and exclusive sections
991 * can be executed without interrupting it.
993 void cpu_exec_end(CPUState
*cpu
);
998 * Wait for a concurrent exclusive section to end, and then start
999 * a section of work that is run while other CPUs are not running
1000 * between cpu_exec_start and cpu_exec_end. CPUs that are running
1001 * cpu_exec are exited immediately. CPUs that call cpu_exec_start
1002 * during the exclusive section go to sleep until this CPU calls
1005 void start_exclusive(void);
1010 * Concludes an exclusive execution section started by start_exclusive.
1012 void end_exclusive(void);
1016 * @cpu: The vCPU to initialize.
1018 * Initializes a vCPU.
1020 void qemu_init_vcpu(CPUState
*cpu
);
1022 #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
1023 #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
1024 #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
1028 * @cpu: CPU to the flags for.
1029 * @enabled: Flags to enable.
1031 * Enables or disables single-stepping for @cpu.
1033 void cpu_single_step(CPUState
*cpu
, int enabled
);
1035 /* Breakpoint/watchpoint flags */
1036 #define BP_MEM_READ 0x01
1037 #define BP_MEM_WRITE 0x02
1038 #define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE)
1039 #define BP_STOP_BEFORE_ACCESS 0x04
1040 /* 0x08 currently unused */
1043 #define BP_ANY (BP_GDB | BP_CPU)
1044 #define BP_WATCHPOINT_HIT_READ 0x40
1045 #define BP_WATCHPOINT_HIT_WRITE 0x80
1046 #define BP_WATCHPOINT_HIT (BP_WATCHPOINT_HIT_READ | BP_WATCHPOINT_HIT_WRITE)
1048 int cpu_breakpoint_insert(CPUState
*cpu
, vaddr pc
, int flags
,
1049 CPUBreakpoint
**breakpoint
);
1050 int cpu_breakpoint_remove(CPUState
*cpu
, vaddr pc
, int flags
);
1051 void cpu_breakpoint_remove_by_ref(CPUState
*cpu
, CPUBreakpoint
*breakpoint
);
1052 void cpu_breakpoint_remove_all(CPUState
*cpu
, int mask
);
1054 /* Return true if PC matches an installed breakpoint. */
1055 static inline bool cpu_breakpoint_test(CPUState
*cpu
, vaddr pc
, int mask
)
1059 if (unlikely(!QTAILQ_EMPTY(&cpu
->breakpoints
))) {
1060 QTAILQ_FOREACH(bp
, &cpu
->breakpoints
, entry
) {
1061 if (bp
->pc
== pc
&& (bp
->flags
& mask
)) {
1069 #ifdef CONFIG_USER_ONLY
1070 static inline int cpu_watchpoint_insert(CPUState
*cpu
, vaddr addr
, vaddr len
,
1071 int flags
, CPUWatchpoint
**watchpoint
)
1076 static inline int cpu_watchpoint_remove(CPUState
*cpu
, vaddr addr
,
1077 vaddr len
, int flags
)
1082 static inline void cpu_watchpoint_remove_by_ref(CPUState
*cpu
,
1087 static inline void cpu_watchpoint_remove_all(CPUState
*cpu
, int mask
)
1091 static inline void cpu_check_watchpoint(CPUState
*cpu
, vaddr addr
, vaddr len
,
1092 MemTxAttrs atr
, int fl
, uintptr_t ra
)
1096 static inline int cpu_watchpoint_address_matches(CPUState
*cpu
,
1097 vaddr addr
, vaddr len
)
1102 int cpu_watchpoint_insert(CPUState
*cpu
, vaddr addr
, vaddr len
,
1103 int flags
, CPUWatchpoint
**watchpoint
);
1104 int cpu_watchpoint_remove(CPUState
*cpu
, vaddr addr
,
1105 vaddr len
, int flags
);
1106 void cpu_watchpoint_remove_by_ref(CPUState
*cpu
, CPUWatchpoint
*watchpoint
);
1107 void cpu_watchpoint_remove_all(CPUState
*cpu
, int mask
);
1110 * cpu_check_watchpoint:
1112 * @addr: guest virtual address
1113 * @len: access length
1114 * @attrs: memory access attributes
1115 * @flags: watchpoint access type
1116 * @ra: unwind return address
1118 * Check for a watchpoint hit in [addr, addr+len) of the type
1119 * specified by @flags. Exit via exception with a hit.
1121 void cpu_check_watchpoint(CPUState
*cpu
, vaddr addr
, vaddr len
,
1122 MemTxAttrs attrs
, int flags
, uintptr_t ra
);
1125 * cpu_watchpoint_address_matches:
1127 * @addr: guest virtual address
1128 * @len: access length
1130 * Return the watchpoint flags that apply to [addr, addr+len).
1131 * If no watchpoint is registered for the range, the result is 0.
1133 int cpu_watchpoint_address_matches(CPUState
*cpu
, vaddr addr
, vaddr len
);
1137 * cpu_get_address_space:
1138 * @cpu: CPU to get address space from
1139 * @asidx: index identifying which address space to get
1141 * Return the requested address space of this CPU. @asidx
1142 * specifies which address space to read.
1144 AddressSpace
*cpu_get_address_space(CPUState
*cpu
, int asidx
);
1146 void QEMU_NORETURN
cpu_abort(CPUState
*cpu
, const char *fmt
, ...)
1148 extern Property cpu_common_props
[];
1149 void cpu_exec_initfn(CPUState
*cpu
);
1150 void cpu_exec_realizefn(CPUState
*cpu
, Error
**errp
);
1151 void cpu_exec_unrealizefn(CPUState
*cpu
);
1154 * target_words_bigendian:
1155 * Returns true if the (default) endianness of the target is big endian,
1156 * false otherwise. Note that in target-specific code, you can use
1157 * TARGET_WORDS_BIGENDIAN directly instead. On the other hand, common
1158 * code should normally never need to know about the endianness of the
1159 * target, so please do *not* use this function unless you know very well
1160 * what you are doing!
1162 bool target_words_bigendian(void);
1166 #ifdef CONFIG_SOFTMMU
1167 extern const VMStateDescription vmstate_cpu_common
;
1169 #define vmstate_cpu_common vmstate_dummy
1172 #define VMSTATE_CPU() { \
1173 .name = "parent_obj", \
1174 .size = sizeof(CPUState), \
1175 .vmsd = &vmstate_cpu_common, \
1176 .flags = VMS_STRUCT, \
1180 #endif /* NEED_CPU_H */
1182 #define UNASSIGNED_CPU_INDEX -1
1183 #define UNASSIGNED_CLUSTER_INDEX -1