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 int gdb_num_core_regs;
199 const char *gdb_core_xml_file;
200 gchar * (*gdb_arch_name)(CPUState *cpu);
201 bool gdb_stop_before_watchpoint;
203 void (*cpu_exec_enter)(CPUState *cpu);
204 void (*cpu_exec_exit)(CPUState *cpu);
205 bool (*cpu_exec_interrupt)(CPUState *cpu, int interrupt_request);
207 void (*disas_set_info)(CPUState *cpu, disassemble_info *info);
208 vaddr (*adjust_watchpoint_address)(CPUState *cpu, vaddr addr, int len);
211 #ifdef HOST_WORDS_BIGENDIAN
212 typedef struct icount_decr_u16 {
217 typedef struct icount_decr_u16 {
223 typedef struct CPUBreakpoint {
225 int flags; /* BP_* */
226 QTAILQ_ENTRY(CPUBreakpoint) entry;
229 struct CPUWatchpoint {
234 int flags; /* BP_* */
235 QTAILQ_ENTRY(CPUWatchpoint) entry;
241 struct hax_vcpu_state;
243 #define TB_JMP_CACHE_BITS 12
244 #define TB_JMP_CACHE_SIZE (1 << TB_JMP_CACHE_BITS)
248 /* The union type allows passing of 64 bit target pointers on 32 bit
249 * hosts in a single parameter
253 unsigned long host_ulong;
258 #define RUN_ON_CPU_HOST_PTR(p) ((run_on_cpu_data){.host_ptr = (p)})
259 #define RUN_ON_CPU_HOST_INT(i) ((run_on_cpu_data){.host_int = (i)})
260 #define RUN_ON_CPU_HOST_ULONG(ul) ((run_on_cpu_data){.host_ulong = (ul)})
261 #define RUN_ON_CPU_TARGET_PTR(v) ((run_on_cpu_data){.target_ptr = (v)})
262 #define RUN_ON_CPU_NULL RUN_ON_CPU_HOST_PTR(NULL)
264 typedef void (*run_on_cpu_func)(CPUState *cpu, run_on_cpu_data data);
266 struct qemu_work_item;
268 #define CPU_UNSET_NUMA_NODE_ID -1
269 #define CPU_TRACE_DSTATE_MAX_EVENTS 32
273 * @cpu_index: CPU index (informative).
274 * @nr_cores: Number of cores within this CPU package.
275 * @nr_threads: Number of threads within this CPU.
276 * @running: #true if CPU is currently running (lockless).
277 * @has_waiter: #true if a CPU is currently waiting for the cpu_exec_end;
278 * valid under cpu_list_lock.
279 * @created: Indicates whether the CPU thread has been successfully created.
280 * @interrupt_request: Indicates a pending interrupt request.
281 * @halted: Nonzero if the CPU is in suspended state.
282 * @stop: Indicates a pending stop request.
283 * @stopped: Indicates the CPU has been artificially stopped.
284 * @unplug: Indicates a pending CPU unplug request.
285 * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU
286 * @singlestep_enabled: Flags for single-stepping.
287 * @icount_extra: Instructions until next timer event.
288 * @icount_decr: Low 16 bits: number of cycles left, only used in icount mode.
289 * High 16 bits: Set to -1 to force TCG to stop executing linked TBs for this
290 * CPU and return to its top level loop (even in non-icount mode).
291 * This allows a single read-compare-cbranch-write sequence to test
292 * for both decrementer underflow and exceptions.
293 * @can_do_io: Nonzero if memory-mapped IO is safe. Deterministic execution
294 * requires that IO only be performed on the last instruction of a TB
295 * so that interrupts take effect immediately.
296 * @cpu_ases: Pointer to array of CPUAddressSpaces (which define the
297 * AddressSpaces this CPU has)
298 * @num_ases: number of CPUAddressSpaces in @cpu_ases
299 * @as: Pointer to the first AddressSpace, for the convenience of targets which
300 * only have a single AddressSpace
301 * @env_ptr: Pointer to subclass-specific CPUArchState field.
302 * @gdb_regs: Additional GDB registers.
303 * @gdb_num_regs: Number of total registers accessible to GDB.
304 * @gdb_num_g_regs: Number of registers in GDB 'g' packets.
305 * @next_cpu: Next CPU sharing TB cache.
306 * @opaque: User data.
307 * @mem_io_pc: Host Program Counter at which the memory was accessed.
308 * @mem_io_vaddr: Target virtual address at which the memory was accessed.
309 * @kvm_fd: vCPU file descriptor for KVM.
310 * @work_mutex: Lock to prevent multiple access to queued_work_*.
311 * @queued_work_first: First asynchronous work pending.
312 * @trace_dstate_delayed: Delayed changes to trace_dstate (includes all changes
314 * @trace_dstate: Dynamic tracing state of events for this vCPU (bitmask).
316 * State of one CPU core or thread.
320 DeviceState parent_obj;
326 struct QemuThread *thread;
331 bool running, has_waiter;
332 struct QemuCond *halt_cond;
340 /* updates protected by BQL */
341 uint32_t interrupt_request;
342 int singlestep_enabled;
343 int64_t icount_budget;
344 int64_t icount_extra;
347 QemuMutex work_mutex;
348 struct qemu_work_item *queued_work_first, *queued_work_last;
350 CPUAddressSpace *cpu_ases;
353 MemoryRegion *memory;
355 void *env_ptr; /* CPUArchState */
357 /* Accessed in parallel; all accesses must be atomic */
358 struct TranslationBlock *tb_jmp_cache[TB_JMP_CACHE_SIZE];
360 struct GDBRegisterState *gdb_regs;
363 QTAILQ_ENTRY(CPUState) node;
365 /* ice debug support */
366 QTAILQ_HEAD(breakpoints_head, CPUBreakpoint) breakpoints;
368 QTAILQ_HEAD(watchpoints_head, CPUWatchpoint) watchpoints;
369 CPUWatchpoint *watchpoint_hit;
373 /* In order to avoid passing too many arguments to the MMIO helpers,
374 * we store some rarely used information in the CPU context.
380 struct KVMState *kvm_state;
381 struct kvm_run *kvm_run;
383 /* Used for events with 'vcpu' and *without* the 'disabled' properties */
384 DECLARE_BITMAP(trace_dstate_delayed, CPU_TRACE_DSTATE_MAX_EVENTS);
385 DECLARE_BITMAP(trace_dstate, CPU_TRACE_DSTATE_MAX_EVENTS);
387 /* TODO Move common fields from CPUArchState here. */
388 int cpu_index; /* used by alpha TCG */
389 uint32_t halted; /* used by alpha, cris, ppc TCG */
391 int32_t exception_index; /* used by m68k TCG */
393 /* shared by kvm, hax and hvf */
396 /* Used to keep track of an outstanding cpu throttle thread for migration
399 bool throttle_thread_scheduled;
401 /* Note that this is accessed at the start of every TB via a negative
402 offset from AREG0. Leave this field at the end so as to make the
403 (absolute value) offset as small as possible. This reduces code
404 size, especially for hosts without large memory offsets. */
410 struct hax_vcpu_state *hax_vcpu;
412 /* The pending_tlb_flush flag is set and cleared atomically to
413 * avoid potential races. The aim of the flag is to avoid
414 * unnecessary flushes.
416 uint16_t pending_tlb_flush;
419 QTAILQ_HEAD(CPUTailQ, CPUState);
420 extern struct CPUTailQ cpus;
421 #define CPU_NEXT(cpu) QTAILQ_NEXT(cpu, node)
422 #define CPU_FOREACH(cpu) QTAILQ_FOREACH(cpu, &cpus, node)
423 #define CPU_FOREACH_SAFE(cpu, next_cpu) \
424 QTAILQ_FOREACH_SAFE(cpu, &cpus, node, next_cpu)
425 #define CPU_FOREACH_REVERSE(cpu) \
426 QTAILQ_FOREACH_REVERSE(cpu, &cpus, CPUTailQ, node)
427 #define first_cpu QTAILQ_FIRST(&cpus)
429 extern __thread CPUState *current_cpu;
431 static inline void cpu_tb_jmp_cache_clear(CPUState *cpu)
435 for (i = 0; i < TB_JMP_CACHE_SIZE; i++) {
436 atomic_set(&cpu->tb_jmp_cache[i], NULL);
441 * qemu_tcg_mttcg_enabled:
442 * Check whether we are running MultiThread TCG or not.
444 * Returns: %true if we are in MTTCG mode %false otherwise.
446 extern bool mttcg_enabled;
447 #define qemu_tcg_mttcg_enabled() (mttcg_enabled)
450 * cpu_paging_enabled:
451 * @cpu: The CPU whose state is to be inspected.
453 * Returns: %true if paging is enabled, %false otherwise.
455 bool cpu_paging_enabled(const CPUState *cpu);
458 * cpu_get_memory_mapping:
459 * @cpu: The CPU whose memory mappings are to be obtained.
460 * @list: Where to write the memory mappings to.
461 * @errp: Pointer for reporting an #Error.
463 void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
467 * cpu_write_elf64_note:
468 * @f: pointer to a function that writes memory to a file
469 * @cpu: The CPU whose memory is to be dumped
470 * @cpuid: ID number of the CPU
471 * @opaque: pointer to the CPUState struct
473 int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
474 int cpuid, void *opaque);
477 * cpu_write_elf64_qemunote:
478 * @f: pointer to a function that writes memory to a file
479 * @cpu: The CPU whose memory is to be dumped
480 * @cpuid: ID number of the CPU
481 * @opaque: pointer to the CPUState struct
483 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
487 * cpu_write_elf32_note:
488 * @f: pointer to a function that writes memory to a file
489 * @cpu: The CPU whose memory is to be dumped
490 * @cpuid: ID number of the CPU
491 * @opaque: pointer to the CPUState struct
493 int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
494 int cpuid, void *opaque);
497 * cpu_write_elf32_qemunote:
498 * @f: pointer to a function that writes memory to a file
499 * @cpu: The CPU whose memory is to be dumped
500 * @cpuid: ID number of the CPU
501 * @opaque: pointer to the CPUState struct
503 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
507 * cpu_get_crash_info:
508 * @cpu: The CPU to get crash information for
510 * Gets the previously saved crash information.
511 * Caller is responsible for freeing the data.
513 GuestPanicInformation *cpu_get_crash_info(CPUState *cpu);
518 * @CPU_DUMP_FPU: dump FPU register state, not just integer
519 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
522 CPU_DUMP_CODE = 0x00010000,
523 CPU_DUMP_FPU = 0x00020000,
524 CPU_DUMP_CCOP = 0x00040000,
529 * @cpu: The CPU whose state is to be dumped.
530 * @f: File to dump to.
531 * @cpu_fprintf: Function to dump with.
532 * @flags: Flags what to dump.
536 void cpu_dump_state(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
540 * cpu_dump_statistics:
541 * @cpu: The CPU whose state is to be dumped.
542 * @f: File to dump to.
543 * @cpu_fprintf: Function to dump with.
544 * @flags: Flags what to dump.
546 * Dumps CPU statistics.
548 void cpu_dump_statistics(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
551 #ifndef CONFIG_USER_ONLY
553 * cpu_get_phys_page_attrs_debug:
554 * @cpu: The CPU to obtain the physical page address for.
555 * @addr: The virtual address.
556 * @attrs: Updated on return with the memory transaction attributes to use
559 * Obtains the physical page corresponding to a virtual one, together
560 * with the corresponding memory transaction attributes to use for the access.
561 * Use it only for debugging because no protection checks are done.
563 * Returns: Corresponding physical page address or -1 if no page found.
565 static inline hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr,
568 CPUClass *cc = CPU_GET_CLASS(cpu);
570 if (cc->get_phys_page_attrs_debug) {
571 return cc->get_phys_page_attrs_debug(cpu, addr, attrs);
573 /* Fallback for CPUs which don't implement the _attrs_ hook */
574 *attrs = MEMTXATTRS_UNSPECIFIED;
575 return cc->get_phys_page_debug(cpu, addr);
579 * cpu_get_phys_page_debug:
580 * @cpu: The CPU to obtain the physical page address for.
581 * @addr: The virtual address.
583 * Obtains the physical page corresponding to a virtual one.
584 * Use it only for debugging because no protection checks are done.
586 * Returns: Corresponding physical page address or -1 if no page found.
588 static inline hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr)
590 MemTxAttrs attrs = {};
592 return cpu_get_phys_page_attrs_debug(cpu, addr, &attrs);
595 /** cpu_asidx_from_attrs:
597 * @attrs: memory transaction attributes
599 * Returns the address space index specifying the CPU AddressSpace
600 * to use for a memory access with the given transaction attributes.
602 static inline int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs)
604 CPUClass *cc = CPU_GET_CLASS(cpu);
606 if (cc->asidx_from_attrs) {
607 return cc->asidx_from_attrs(cpu, attrs);
615 * @cpu: The CPU to be added to the list of CPUs.
617 void cpu_list_add(CPUState *cpu);
621 * @cpu: The CPU to be removed from the list of CPUs.
623 void cpu_list_remove(CPUState *cpu);
627 * @cpu: The CPU whose state is to be reset.
629 void cpu_reset(CPUState *cpu);
633 * @typename: The CPU base type.
634 * @cpu_model: The model string without any parameters.
636 * Looks up a CPU #ObjectClass matching name @cpu_model.
638 * Returns: A #CPUClass or %NULL if not matching class is found.
640 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
644 * @typename: The CPU base type.
645 * @cpu_model: The model string including optional parameters.
647 * Instantiates a CPU, processes optional parameters and realizes the CPU.
649 * Returns: A #CPUState or %NULL if an error occurred.
651 CPUState *cpu_generic_init(const char *typename, const char *cpu_model);
655 * @cpu: The vCPU to check.
657 * Checks whether the CPU has work to do.
659 * Returns: %true if the CPU has work, %false otherwise.
661 static inline bool cpu_has_work(CPUState *cpu)
663 CPUClass *cc = CPU_GET_CLASS(cpu);
665 g_assert(cc->has_work);
666 return cc->has_work(cpu);
671 * @cpu: The vCPU to check against.
673 * Checks whether the caller is executing on the vCPU thread.
675 * Returns: %true if called from @cpu's thread, %false otherwise.
677 bool qemu_cpu_is_self(CPUState *cpu);
681 * @cpu: The vCPU to kick.
683 * Kicks @cpu's thread.
685 void qemu_cpu_kick(CPUState *cpu);
689 * @cpu: The CPU to check.
691 * Checks whether the CPU is stopped.
693 * Returns: %true if run state is not running or if artificially stopped;
696 bool cpu_is_stopped(CPUState *cpu);
700 * @cpu: The vCPU to run on.
701 * @func: The function to be executed.
702 * @data: Data to pass to the function.
703 * @mutex: Mutex to release while waiting for @func to run.
705 * Used internally in the implementation of run_on_cpu.
707 void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data,
712 * @cpu: The vCPU to run on.
713 * @func: The function to be executed.
714 * @data: Data to pass to the function.
716 * Schedules the function @func for execution on the vCPU @cpu.
718 void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
722 * @cpu: The vCPU to run on.
723 * @func: The function to be executed.
724 * @data: Data to pass to the function.
726 * Schedules the function @func for execution on the vCPU @cpu asynchronously.
728 void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
731 * async_safe_run_on_cpu:
732 * @cpu: The vCPU to run on.
733 * @func: The function to be executed.
734 * @data: Data to pass to the function.
736 * Schedules the function @func for execution on the vCPU @cpu asynchronously,
737 * while all other vCPUs are sleeping.
739 * Unlike run_on_cpu and async_run_on_cpu, the function is run outside the
742 void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
746 * @index: The CPUState@cpu_index value of the CPU to obtain.
748 * Gets a CPU matching @index.
750 * Returns: The CPU or %NULL if there is no matching CPU.
752 CPUState *qemu_get_cpu(int index);
756 * @id: Guest-exposed CPU ID to lookup.
758 * Search for CPU with specified ID.
760 * Returns: %true - CPU is found, %false - CPU isn't found.
762 bool cpu_exists(int64_t id);
766 * @id: Guest-exposed CPU ID of the CPU to obtain.
768 * Get a CPU with matching @id.
770 * Returns: The CPU or %NULL if there is no matching CPU.
772 CPUState *cpu_by_arch_id(int64_t id);
776 * @new_throttle_pct: Percent of sleep time. Valid range is 1 to 99.
778 * Throttles all vcpus by forcing them to sleep for the given percentage of
779 * time. A throttle_percentage of 25 corresponds to a 75% duty cycle roughly.
780 * (example: 10ms sleep for every 30ms awake).
782 * cpu_throttle_set can be called as needed to adjust new_throttle_pct.
783 * Once the throttling starts, it will remain in effect until cpu_throttle_stop
786 void cpu_throttle_set(int new_throttle_pct);
791 * Stops the vcpu throttling started by cpu_throttle_set.
793 void cpu_throttle_stop(void);
796 * cpu_throttle_active:
798 * Returns: %true if the vcpus are currently being throttled, %false otherwise.
800 bool cpu_throttle_active(void);
803 * cpu_throttle_get_percentage:
805 * Returns the vcpu throttle percentage. See cpu_throttle_set for details.
807 * Returns: The throttle percentage in range 1 to 99.
809 int cpu_throttle_get_percentage(void);
811 #ifndef CONFIG_USER_ONLY
813 typedef void (*CPUInterruptHandler)(CPUState *, int);
815 extern CPUInterruptHandler cpu_interrupt_handler;
819 * @cpu: The CPU to set an interrupt on.
820 * @mask: The interupts to set.
822 * Invokes the interrupt handler.
824 static inline void cpu_interrupt(CPUState *cpu, int mask)
826 cpu_interrupt_handler(cpu, mask);
829 #else /* USER_ONLY */
831 void cpu_interrupt(CPUState *cpu, int mask);
833 #endif /* USER_ONLY */
837 #ifdef CONFIG_SOFTMMU
838 static inline void cpu_unassigned_access(CPUState *cpu, hwaddr addr,
839 bool is_write, bool is_exec,
840 int opaque, unsigned size)
842 CPUClass *cc = CPU_GET_CLASS(cpu);
844 if (cc->do_unassigned_access) {
845 cc->do_unassigned_access(cpu, addr, is_write, is_exec, opaque, size);
849 static inline void cpu_unaligned_access(CPUState *cpu, vaddr addr,
850 MMUAccessType access_type,
851 int mmu_idx, uintptr_t retaddr)
853 CPUClass *cc = CPU_GET_CLASS(cpu);
855 cc->do_unaligned_access(cpu, addr, access_type, mmu_idx, retaddr);
858 static inline void cpu_transaction_failed(CPUState *cpu, hwaddr physaddr,
859 vaddr addr, unsigned size,
860 MMUAccessType access_type,
861 int mmu_idx, MemTxAttrs attrs,
862 MemTxResult response,
865 CPUClass *cc = CPU_GET_CLASS(cpu);
867 if (cc->do_transaction_failed) {
868 cc->do_transaction_failed(cpu, physaddr, addr, size, access_type,
869 mmu_idx, attrs, response, retaddr);
874 #endif /* NEED_CPU_H */
878 * @cpu: The CPU to set the program counter for.
879 * @addr: Program counter value.
881 * Sets the program counter for a CPU.
883 static inline void cpu_set_pc(CPUState *cpu, vaddr addr)
885 CPUClass *cc = CPU_GET_CLASS(cpu);
887 cc->set_pc(cpu, addr);
891 * cpu_reset_interrupt:
892 * @cpu: The CPU to clear the interrupt on.
893 * @mask: The interrupt mask to clear.
895 * Resets interrupts on the vCPU @cpu.
897 void cpu_reset_interrupt(CPUState *cpu, int mask);
901 * @cpu: The CPU to exit.
903 * Requests the CPU @cpu to exit execution.
905 void cpu_exit(CPUState *cpu);
909 * @cpu: The CPU to resume.
911 * Resumes CPU, i.e. puts CPU into runnable state.
913 void cpu_resume(CPUState *cpu);
917 * @cpu: The CPU to remove.
919 * Requests the CPU to be removed.
921 void cpu_remove(CPUState *cpu);
925 * @cpu: The CPU to remove.
927 * Requests the CPU to be removed and waits till it is removed.
929 void cpu_remove_sync(CPUState *cpu);
932 * process_queued_cpu_work() - process all items on CPU work queue
933 * @cpu: The CPU which work queue to process.
935 void process_queued_cpu_work(CPUState *cpu);
939 * @cpu: The CPU for the current thread.
941 * Record that a CPU has started execution and can be interrupted with
944 void cpu_exec_start(CPUState *cpu);
948 * @cpu: The CPU for the current thread.
950 * Record that a CPU has stopped execution and exclusive sections
951 * can be executed without interrupting it.
953 void cpu_exec_end(CPUState *cpu);
958 * Wait for a concurrent exclusive section to end, and then start
959 * a section of work that is run while other CPUs are not running
960 * between cpu_exec_start and cpu_exec_end. CPUs that are running
961 * cpu_exec are exited immediately. CPUs that call cpu_exec_start
962 * during the exclusive section go to sleep until this CPU calls
965 void start_exclusive(void);
970 * Concludes an exclusive execution section started by start_exclusive.
972 void end_exclusive(void);
976 * @cpu: The vCPU to initialize.
978 * Initializes a vCPU.
980 void qemu_init_vcpu(CPUState *cpu);
982 #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
983 #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
984 #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
988 * @cpu: CPU to the flags for.
989 * @enabled: Flags to enable.
991 * Enables or disables single-stepping for @cpu.
993 void cpu_single_step(CPUState *cpu, int enabled);
995 /* Breakpoint/watchpoint flags */
996 #define BP_MEM_READ 0x01
997 #define BP_MEM_WRITE 0x02
998 #define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE)
999 #define BP_STOP_BEFORE_ACCESS 0x04
1000 /* 0x08 currently unused */
1003 #define BP_ANY (BP_GDB | BP_CPU)
1004 #define BP_WATCHPOINT_HIT_READ 0x40
1005 #define BP_WATCHPOINT_HIT_WRITE 0x80
1006 #define BP_WATCHPOINT_HIT (BP_WATCHPOINT_HIT_READ | BP_WATCHPOINT_HIT_WRITE)
1008 int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
1009 CPUBreakpoint **breakpoint);
1010 int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags);
1011 void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint);
1012 void cpu_breakpoint_remove_all(CPUState *cpu, int mask);
1014 /* Return true if PC matches an installed breakpoint. */
1015 static inline bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask)
1019 if (unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) {
1020 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
1021 if (bp->pc == pc && (bp->flags & mask)) {
1029 int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
1030 int flags, CPUWatchpoint **watchpoint);
1031 int cpu_watchpoint_remove(CPUState *cpu, vaddr addr,
1032 vaddr len, int flags);
1033 void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint);
1034 void cpu_watchpoint_remove_all(CPUState *cpu, int mask);
1037 * cpu_get_address_space:
1038 * @cpu: CPU to get address space from
1039 * @asidx: index identifying which address space to get
1041 * Return the requested address space of this CPU. @asidx
1042 * specifies which address space to read.
1044 AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx);
1046 void QEMU_NORETURN cpu_abort(CPUState *cpu, const char *fmt, ...)
1048 extern Property cpu_common_props[];
1049 void cpu_exec_initfn(CPUState *cpu);
1050 void cpu_exec_realizefn(CPUState *cpu, Error **errp);
1051 void cpu_exec_unrealizefn(CPUState *cpu);
1055 #ifdef CONFIG_SOFTMMU
1056 extern const struct VMStateDescription vmstate_cpu_common;
1058 #define vmstate_cpu_common vmstate_dummy
1061 #define VMSTATE_CPU() { \
1062 .name = "parent_obj", \
1063 .size = sizeof(CPUState), \
1064 .vmsd = &vmstate_cpu_common, \
1065 .flags = VMS_STRUCT, \
1069 #endif /* NEED_CPU_H */
1071 #define UNASSIGNED_CPU_INDEX -1