loader: extract rom_free() function
[qemu/ar7.git] / include / qom / cpu.h
blobecf6ed556a9cc95a21888b82ecc4a87d22450566
1 /*
2 * QEMU CPU model
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>
20 #ifndef QEMU_CPU_H
21 #define QEMU_CPU_H
23 #include "hw/qdev-core.h"
24 #include "disas/bfd.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/queue.h"
30 #include "qemu/thread.h"
32 typedef int (*WriteCoreDumpFunction)(const void *buf, size_t size,
33 void *opaque);
35 /**
36 * vaddr:
37 * Type wide enough to contain any #target_ulong virtual address.
39 typedef uint64_t vaddr;
40 #define VADDR_PRId PRId64
41 #define VADDR_PRIu PRIu64
42 #define VADDR_PRIo PRIo64
43 #define VADDR_PRIx PRIx64
44 #define VADDR_PRIX PRIX64
45 #define VADDR_MAX UINT64_MAX
47 /**
48 * SECTION:cpu
49 * @section_id: QEMU-cpu
50 * @title: CPU Class
51 * @short_description: Base class for all CPUs
54 #define TYPE_CPU "cpu"
56 /* Since this macro is used a lot in hot code paths and in conjunction with
57 * FooCPU *foo_env_get_cpu(), we deviate from usual QOM practice by using
58 * an unchecked cast.
60 #define CPU(obj) ((CPUState *)(obj))
62 #define CPU_CLASS(class) OBJECT_CLASS_CHECK(CPUClass, (class), TYPE_CPU)
63 #define CPU_GET_CLASS(obj) OBJECT_GET_CLASS(CPUClass, (obj), TYPE_CPU)
65 typedef enum MMUAccessType {
66 MMU_DATA_LOAD = 0,
67 MMU_DATA_STORE = 1,
68 MMU_INST_FETCH = 2
69 } MMUAccessType;
71 typedef struct CPUWatchpoint CPUWatchpoint;
73 typedef void (*CPUUnassignedAccess)(CPUState *cpu, hwaddr addr,
74 bool is_write, bool is_exec, int opaque,
75 unsigned size);
77 struct TranslationBlock;
79 /**
80 * CPUClass:
81 * @class_by_name: Callback to map -cpu command line model name to an
82 * instantiatable CPU type.
83 * @parse_features: Callback to parse command line arguments.
84 * @reset: Callback to reset the #CPUState to its initial state.
85 * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
86 * @has_work: Callback for checking if there is work to do.
87 * @do_interrupt: Callback for interrupt handling.
88 * @do_unassigned_access: Callback for unassigned access handling.
89 * (this is deprecated: new targets should use do_transaction_failed instead)
90 * @do_unaligned_access: Callback for unaligned access handling, if
91 * the target defines #ALIGNED_ONLY.
92 * @do_transaction_failed: Callback for handling failed memory transactions
93 * (ie bus faults or external aborts; not MMU faults)
94 * @virtio_is_big_endian: Callback to return %true if a CPU which supports
95 * runtime configurable endianness is currently big-endian. Non-configurable
96 * CPUs can use the default implementation of this method. This method should
97 * not be used by any callers other than the pre-1.0 virtio devices.
98 * @memory_rw_debug: Callback for GDB memory access.
99 * @dump_state: Callback for dumping state.
100 * @dump_statistics: Callback for dumping statistics.
101 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
102 * @get_paging_enabled: Callback for inquiring whether paging is enabled.
103 * @get_memory_mapping: Callback for obtaining the memory mappings.
104 * @set_pc: Callback for setting the Program Counter register.
105 * @synchronize_from_tb: Callback for synchronizing state from a TCG
106 * #TranslationBlock.
107 * @handle_mmu_fault: Callback for handling an MMU fault.
108 * @get_phys_page_debug: Callback for obtaining a physical address.
109 * @get_phys_page_attrs_debug: Callback for obtaining a physical address and the
110 * associated memory transaction attributes to use for the access.
111 * CPUs which use memory transaction attributes should implement this
112 * instead of get_phys_page_debug.
113 * @asidx_from_attrs: Callback to return the CPU AddressSpace to use for
114 * a memory access with the specified memory transaction attributes.
115 * @gdb_read_register: Callback for letting GDB read a register.
116 * @gdb_write_register: Callback for letting GDB write a register.
117 * @debug_check_watchpoint: Callback: return true if the architectural
118 * watchpoint whose address has matched should really fire.
119 * @debug_excp_handler: Callback for handling debug exceptions.
120 * @write_elf64_note: Callback for writing a CPU-specific ELF note to a
121 * 64-bit VM coredump.
122 * @write_elf32_qemunote: Callback for writing a CPU- and QEMU-specific ELF
123 * note to a 32-bit VM coredump.
124 * @write_elf32_note: Callback for writing a CPU-specific ELF note to a
125 * 32-bit VM coredump.
126 * @write_elf32_qemunote: Callback for writing a CPU- and QEMU-specific ELF
127 * note to a 32-bit VM coredump.
128 * @vmsd: State description for migration.
129 * @gdb_num_core_regs: Number of core registers accessible to GDB.
130 * @gdb_core_xml_file: File name for core registers GDB XML description.
131 * @gdb_stop_before_watchpoint: Indicates whether GDB expects the CPU to stop
132 * before the insn which triggers a watchpoint rather than after it.
133 * @gdb_arch_name: Optional callback that returns the architecture name known
134 * to GDB. The caller must free the returned string with g_free.
135 * @gdb_get_dynamic_xml: Callback to return dynamically generated XML for the
136 * gdb stub. Returns a pointer to the XML contents for the specified XML file
137 * or NULL if the CPU doesn't have a dynamically generated content for it.
138 * @cpu_exec_enter: Callback for cpu_exec preparation.
139 * @cpu_exec_exit: Callback for cpu_exec cleanup.
140 * @cpu_exec_interrupt: Callback for processing interrupts in cpu_exec.
141 * @disas_set_info: Setup architecture specific components of disassembly info
142 * @adjust_watchpoint_address: Perform a target-specific adjustment to an
143 * address before attempting to match it against watchpoints.
145 * Represents a CPU family or model.
147 typedef struct CPUClass {
148 /*< private >*/
149 DeviceClass parent_class;
150 /*< public >*/
152 ObjectClass *(*class_by_name)(const char *cpu_model);
153 void (*parse_features)(const char *typename, char *str, Error **errp);
155 void (*reset)(CPUState *cpu);
156 int reset_dump_flags;
157 bool (*has_work)(CPUState *cpu);
158 void (*do_interrupt)(CPUState *cpu);
159 CPUUnassignedAccess do_unassigned_access;
160 void (*do_unaligned_access)(CPUState *cpu, vaddr addr,
161 MMUAccessType access_type,
162 int mmu_idx, uintptr_t retaddr);
163 void (*do_transaction_failed)(CPUState *cpu, hwaddr physaddr, vaddr addr,
164 unsigned size, MMUAccessType access_type,
165 int mmu_idx, MemTxAttrs attrs,
166 MemTxResult response, uintptr_t retaddr);
167 bool (*virtio_is_big_endian)(CPUState *cpu);
168 int (*memory_rw_debug)(CPUState *cpu, vaddr addr,
169 uint8_t *buf, int len, bool is_write);
170 void (*dump_state)(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
171 int flags);
172 GuestPanicInformation* (*get_crash_info)(CPUState *cpu);
173 void (*dump_statistics)(CPUState *cpu, FILE *f,
174 fprintf_function cpu_fprintf, int flags);
175 int64_t (*get_arch_id)(CPUState *cpu);
176 bool (*get_paging_enabled)(const CPUState *cpu);
177 void (*get_memory_mapping)(CPUState *cpu, MemoryMappingList *list,
178 Error **errp);
179 void (*set_pc)(CPUState *cpu, vaddr value);
180 void (*synchronize_from_tb)(CPUState *cpu, struct TranslationBlock *tb);
181 int (*handle_mmu_fault)(CPUState *cpu, vaddr address, int size, int rw,
182 int mmu_index);
183 hwaddr (*get_phys_page_debug)(CPUState *cpu, vaddr addr);
184 hwaddr (*get_phys_page_attrs_debug)(CPUState *cpu, vaddr addr,
185 MemTxAttrs *attrs);
186 int (*asidx_from_attrs)(CPUState *cpu, MemTxAttrs attrs);
187 int (*gdb_read_register)(CPUState *cpu, uint8_t *buf, int reg);
188 int (*gdb_write_register)(CPUState *cpu, uint8_t *buf, int reg);
189 bool (*debug_check_watchpoint)(CPUState *cpu, CPUWatchpoint *wp);
190 void (*debug_excp_handler)(CPUState *cpu);
192 int (*write_elf64_note)(WriteCoreDumpFunction f, CPUState *cpu,
193 int cpuid, void *opaque);
194 int (*write_elf64_qemunote)(WriteCoreDumpFunction f, CPUState *cpu,
195 void *opaque);
196 int (*write_elf32_note)(WriteCoreDumpFunction f, CPUState *cpu,
197 int cpuid, void *opaque);
198 int (*write_elf32_qemunote)(WriteCoreDumpFunction f, CPUState *cpu,
199 void *opaque);
201 const struct VMStateDescription *vmsd;
202 const char *gdb_core_xml_file;
203 gchar * (*gdb_arch_name)(CPUState *cpu);
204 const char * (*gdb_get_dynamic_xml)(CPUState *cpu, const char *xmlname);
205 void (*cpu_exec_enter)(CPUState *cpu);
206 void (*cpu_exec_exit)(CPUState *cpu);
207 bool (*cpu_exec_interrupt)(CPUState *cpu, int interrupt_request);
209 void (*disas_set_info)(CPUState *cpu, disassemble_info *info);
210 vaddr (*adjust_watchpoint_address)(CPUState *cpu, vaddr addr, int len);
211 void (*tcg_initialize)(void);
213 /* Keep non-pointer data at the end to minimize holes. */
214 int gdb_num_core_regs;
215 bool gdb_stop_before_watchpoint;
216 } CPUClass;
218 #ifdef HOST_WORDS_BIGENDIAN
219 typedef struct icount_decr_u16 {
220 uint16_t high;
221 uint16_t low;
222 } icount_decr_u16;
223 #else
224 typedef struct icount_decr_u16 {
225 uint16_t low;
226 uint16_t high;
227 } icount_decr_u16;
228 #endif
230 typedef struct CPUBreakpoint {
231 vaddr pc;
232 int flags; /* BP_* */
233 QTAILQ_ENTRY(CPUBreakpoint) entry;
234 } CPUBreakpoint;
236 struct CPUWatchpoint {
237 vaddr vaddr;
238 vaddr len;
239 vaddr hitaddr;
240 MemTxAttrs hitattrs;
241 int flags; /* BP_* */
242 QTAILQ_ENTRY(CPUWatchpoint) entry;
245 struct KVMState;
246 struct kvm_run;
248 struct hax_vcpu_state;
250 #define TB_JMP_CACHE_BITS 12
251 #define TB_JMP_CACHE_SIZE (1 << TB_JMP_CACHE_BITS)
253 /* work queue */
255 /* The union type allows passing of 64 bit target pointers on 32 bit
256 * hosts in a single parameter
258 typedef union {
259 int host_int;
260 unsigned long host_ulong;
261 void *host_ptr;
262 vaddr target_ptr;
263 } run_on_cpu_data;
265 #define RUN_ON_CPU_HOST_PTR(p) ((run_on_cpu_data){.host_ptr = (p)})
266 #define RUN_ON_CPU_HOST_INT(i) ((run_on_cpu_data){.host_int = (i)})
267 #define RUN_ON_CPU_HOST_ULONG(ul) ((run_on_cpu_data){.host_ulong = (ul)})
268 #define RUN_ON_CPU_TARGET_PTR(v) ((run_on_cpu_data){.target_ptr = (v)})
269 #define RUN_ON_CPU_NULL RUN_ON_CPU_HOST_PTR(NULL)
271 typedef void (*run_on_cpu_func)(CPUState *cpu, run_on_cpu_data data);
273 struct qemu_work_item;
275 #define CPU_UNSET_NUMA_NODE_ID -1
276 #define CPU_TRACE_DSTATE_MAX_EVENTS 32
279 * CPUState:
280 * @cpu_index: CPU index (informative).
281 * @nr_cores: Number of cores within this CPU package.
282 * @nr_threads: Number of threads within this CPU.
283 * @running: #true if CPU is currently running (lockless).
284 * @has_waiter: #true if a CPU is currently waiting for the cpu_exec_end;
285 * valid under cpu_list_lock.
286 * @created: Indicates whether the CPU thread has been successfully created.
287 * @interrupt_request: Indicates a pending interrupt request.
288 * @halted: Nonzero if the CPU is in suspended state.
289 * @stop: Indicates a pending stop request.
290 * @stopped: Indicates the CPU has been artificially stopped.
291 * @unplug: Indicates a pending CPU unplug request.
292 * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU
293 * @singlestep_enabled: Flags for single-stepping.
294 * @icount_extra: Instructions until next timer event.
295 * @icount_decr: Low 16 bits: number of cycles left, only used in icount mode.
296 * High 16 bits: Set to -1 to force TCG to stop executing linked TBs for this
297 * CPU and return to its top level loop (even in non-icount mode).
298 * This allows a single read-compare-cbranch-write sequence to test
299 * for both decrementer underflow and exceptions.
300 * @can_do_io: Nonzero if memory-mapped IO is safe. Deterministic execution
301 * requires that IO only be performed on the last instruction of a TB
302 * so that interrupts take effect immediately.
303 * @cpu_ases: Pointer to array of CPUAddressSpaces (which define the
304 * AddressSpaces this CPU has)
305 * @num_ases: number of CPUAddressSpaces in @cpu_ases
306 * @as: Pointer to the first AddressSpace, for the convenience of targets which
307 * only have a single AddressSpace
308 * @env_ptr: Pointer to subclass-specific CPUArchState field.
309 * @gdb_regs: Additional GDB registers.
310 * @gdb_num_regs: Number of total registers accessible to GDB.
311 * @gdb_num_g_regs: Number of registers in GDB 'g' packets.
312 * @next_cpu: Next CPU sharing TB cache.
313 * @opaque: User data.
314 * @mem_io_pc: Host Program Counter at which the memory was accessed.
315 * @mem_io_vaddr: Target virtual address at which the memory was accessed.
316 * @kvm_fd: vCPU file descriptor for KVM.
317 * @work_mutex: Lock to prevent multiple access to queued_work_*.
318 * @queued_work_first: First asynchronous work pending.
319 * @trace_dstate_delayed: Delayed changes to trace_dstate (includes all changes
320 * to @trace_dstate).
321 * @trace_dstate: Dynamic tracing state of events for this vCPU (bitmask).
322 * @ignore_memory_transaction_failures: Cached copy of the MachineState
323 * flag of the same name: allows the board to suppress calling of the
324 * CPU do_transaction_failed hook function.
326 * State of one CPU core or thread.
328 struct CPUState {
329 /*< private >*/
330 DeviceState parent_obj;
331 /*< public >*/
333 int nr_cores;
334 int nr_threads;
336 struct QemuThread *thread;
337 #ifdef _WIN32
338 HANDLE hThread;
339 #endif
340 int thread_id;
341 bool running, has_waiter;
342 struct QemuCond *halt_cond;
343 bool thread_kicked;
344 bool created;
345 bool stop;
346 bool stopped;
347 bool unplug;
348 bool crash_occurred;
349 bool exit_request;
350 uint32_t cflags_next_tb;
351 /* updates protected by BQL */
352 uint32_t interrupt_request;
353 int singlestep_enabled;
354 int64_t icount_budget;
355 int64_t icount_extra;
356 sigjmp_buf jmp_env;
358 QemuMutex work_mutex;
359 struct qemu_work_item *queued_work_first, *queued_work_last;
361 CPUAddressSpace *cpu_ases;
362 int num_ases;
363 AddressSpace *as;
364 MemoryRegion *memory;
366 void *env_ptr; /* CPUArchState */
368 /* Accessed in parallel; all accesses must be atomic */
369 struct TranslationBlock *tb_jmp_cache[TB_JMP_CACHE_SIZE];
371 struct GDBRegisterState *gdb_regs;
372 int gdb_num_regs;
373 int gdb_num_g_regs;
374 QTAILQ_ENTRY(CPUState) node;
376 /* ice debug support */
377 QTAILQ_HEAD(breakpoints_head, CPUBreakpoint) breakpoints;
379 QTAILQ_HEAD(watchpoints_head, CPUWatchpoint) watchpoints;
380 CPUWatchpoint *watchpoint_hit;
382 void *opaque;
384 /* In order to avoid passing too many arguments to the MMIO helpers,
385 * we store some rarely used information in the CPU context.
387 uintptr_t mem_io_pc;
388 vaddr mem_io_vaddr;
390 * This is only needed for the legacy cpu_unassigned_access() hook;
391 * when all targets using it have been converted to use
392 * cpu_transaction_failed() instead it can be removed.
394 MMUAccessType mem_io_access_type;
396 int kvm_fd;
397 struct KVMState *kvm_state;
398 struct kvm_run *kvm_run;
400 /* Used for events with 'vcpu' and *without* the 'disabled' properties */
401 DECLARE_BITMAP(trace_dstate_delayed, CPU_TRACE_DSTATE_MAX_EVENTS);
402 DECLARE_BITMAP(trace_dstate, CPU_TRACE_DSTATE_MAX_EVENTS);
404 /* TODO Move common fields from CPUArchState here. */
405 int cpu_index;
406 uint32_t halted;
407 uint32_t can_do_io;
408 int32_t exception_index;
410 /* shared by kvm, hax and hvf */
411 bool vcpu_dirty;
413 /* Used to keep track of an outstanding cpu throttle thread for migration
414 * autoconverge
416 bool throttle_thread_scheduled;
418 bool ignore_memory_transaction_failures;
420 /* Note that this is accessed at the start of every TB via a negative
421 offset from AREG0. Leave this field at the end so as to make the
422 (absolute value) offset as small as possible. This reduces code
423 size, especially for hosts without large memory offsets. */
424 union {
425 uint32_t u32;
426 icount_decr_u16 u16;
427 } icount_decr;
429 struct hax_vcpu_state *hax_vcpu;
431 /* The pending_tlb_flush flag is set and cleared atomically to
432 * avoid potential races. The aim of the flag is to avoid
433 * unnecessary flushes.
435 uint16_t pending_tlb_flush;
437 int hvf_fd;
439 /* track IOMMUs whose translations we've cached in the TCG TLB */
440 GArray *iommu_notifiers;
443 QTAILQ_HEAD(CPUTailQ, CPUState);
444 extern struct CPUTailQ cpus;
445 #define CPU_NEXT(cpu) QTAILQ_NEXT(cpu, node)
446 #define CPU_FOREACH(cpu) QTAILQ_FOREACH(cpu, &cpus, node)
447 #define CPU_FOREACH_SAFE(cpu, next_cpu) \
448 QTAILQ_FOREACH_SAFE(cpu, &cpus, node, next_cpu)
449 #define CPU_FOREACH_REVERSE(cpu) \
450 QTAILQ_FOREACH_REVERSE(cpu, &cpus, CPUTailQ, node)
451 #define first_cpu QTAILQ_FIRST(&cpus)
453 extern __thread CPUState *current_cpu;
455 static inline void cpu_tb_jmp_cache_clear(CPUState *cpu)
457 unsigned int i;
459 for (i = 0; i < TB_JMP_CACHE_SIZE; i++) {
460 atomic_set(&cpu->tb_jmp_cache[i], NULL);
465 * qemu_tcg_mttcg_enabled:
466 * Check whether we are running MultiThread TCG or not.
468 * Returns: %true if we are in MTTCG mode %false otherwise.
470 extern bool mttcg_enabled;
471 #define qemu_tcg_mttcg_enabled() (mttcg_enabled)
474 * cpu_paging_enabled:
475 * @cpu: The CPU whose state is to be inspected.
477 * Returns: %true if paging is enabled, %false otherwise.
479 bool cpu_paging_enabled(const CPUState *cpu);
482 * cpu_get_memory_mapping:
483 * @cpu: The CPU whose memory mappings are to be obtained.
484 * @list: Where to write the memory mappings to.
485 * @errp: Pointer for reporting an #Error.
487 void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
488 Error **errp);
491 * cpu_write_elf64_note:
492 * @f: pointer to a function that writes memory to a file
493 * @cpu: The CPU whose memory is to be dumped
494 * @cpuid: ID number of the CPU
495 * @opaque: pointer to the CPUState struct
497 int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
498 int cpuid, void *opaque);
501 * cpu_write_elf64_qemunote:
502 * @f: pointer to a function that writes memory to a file
503 * @cpu: The CPU whose memory is to be dumped
504 * @cpuid: ID number of the CPU
505 * @opaque: pointer to the CPUState struct
507 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
508 void *opaque);
511 * cpu_write_elf32_note:
512 * @f: pointer to a function that writes memory to a file
513 * @cpu: The CPU whose memory is to be dumped
514 * @cpuid: ID number of the CPU
515 * @opaque: pointer to the CPUState struct
517 int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
518 int cpuid, void *opaque);
521 * cpu_write_elf32_qemunote:
522 * @f: pointer to a function that writes memory to a file
523 * @cpu: The CPU whose memory is to be dumped
524 * @cpuid: ID number of the CPU
525 * @opaque: pointer to the CPUState struct
527 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
528 void *opaque);
531 * cpu_get_crash_info:
532 * @cpu: The CPU to get crash information for
534 * Gets the previously saved crash information.
535 * Caller is responsible for freeing the data.
537 GuestPanicInformation *cpu_get_crash_info(CPUState *cpu);
540 * CPUDumpFlags:
541 * @CPU_DUMP_CODE:
542 * @CPU_DUMP_FPU: dump FPU register state, not just integer
543 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
545 enum CPUDumpFlags {
546 CPU_DUMP_CODE = 0x00010000,
547 CPU_DUMP_FPU = 0x00020000,
548 CPU_DUMP_CCOP = 0x00040000,
552 * cpu_dump_state:
553 * @cpu: The CPU whose state is to be dumped.
554 * @f: File to dump to.
555 * @cpu_fprintf: Function to dump with.
556 * @flags: Flags what to dump.
558 * Dumps CPU state.
560 void cpu_dump_state(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
561 int flags);
564 * cpu_dump_statistics:
565 * @cpu: The CPU whose state is to be dumped.
566 * @f: File to dump to.
567 * @cpu_fprintf: Function to dump with.
568 * @flags: Flags what to dump.
570 * Dumps CPU statistics.
572 void cpu_dump_statistics(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
573 int flags);
575 #ifndef CONFIG_USER_ONLY
577 * cpu_get_phys_page_attrs_debug:
578 * @cpu: The CPU to obtain the physical page address for.
579 * @addr: The virtual address.
580 * @attrs: Updated on return with the memory transaction attributes to use
581 * for this access.
583 * Obtains the physical page corresponding to a virtual one, together
584 * with the corresponding memory transaction attributes to use for the access.
585 * Use it only for debugging because no protection checks are done.
587 * Returns: Corresponding physical page address or -1 if no page found.
589 static inline hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr,
590 MemTxAttrs *attrs)
592 CPUClass *cc = CPU_GET_CLASS(cpu);
594 if (cc->get_phys_page_attrs_debug) {
595 return cc->get_phys_page_attrs_debug(cpu, addr, attrs);
597 /* Fallback for CPUs which don't implement the _attrs_ hook */
598 *attrs = MEMTXATTRS_UNSPECIFIED;
599 return cc->get_phys_page_debug(cpu, addr);
603 * cpu_get_phys_page_debug:
604 * @cpu: The CPU to obtain the physical page address for.
605 * @addr: The virtual address.
607 * Obtains the physical page corresponding to a virtual one.
608 * Use it only for debugging because no protection checks are done.
610 * Returns: Corresponding physical page address or -1 if no page found.
612 static inline hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr)
614 MemTxAttrs attrs = {};
616 return cpu_get_phys_page_attrs_debug(cpu, addr, &attrs);
619 /** cpu_asidx_from_attrs:
620 * @cpu: CPU
621 * @attrs: memory transaction attributes
623 * Returns the address space index specifying the CPU AddressSpace
624 * to use for a memory access with the given transaction attributes.
626 static inline int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs)
628 CPUClass *cc = CPU_GET_CLASS(cpu);
629 int ret = 0;
631 if (cc->asidx_from_attrs) {
632 ret = cc->asidx_from_attrs(cpu, attrs);
633 assert(ret < cpu->num_ases && ret >= 0);
635 return ret;
637 #endif
640 * cpu_list_add:
641 * @cpu: The CPU to be added to the list of CPUs.
643 void cpu_list_add(CPUState *cpu);
646 * cpu_list_remove:
647 * @cpu: The CPU to be removed from the list of CPUs.
649 void cpu_list_remove(CPUState *cpu);
652 * cpu_reset:
653 * @cpu: The CPU whose state is to be reset.
655 void cpu_reset(CPUState *cpu);
658 * cpu_class_by_name:
659 * @typename: The CPU base type.
660 * @cpu_model: The model string without any parameters.
662 * Looks up a CPU #ObjectClass matching name @cpu_model.
664 * Returns: A #CPUClass or %NULL if not matching class is found.
666 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
669 * cpu_create:
670 * @typename: The CPU type.
672 * Instantiates a CPU and realizes the CPU.
674 * Returns: A #CPUState or %NULL if an error occurred.
676 CPUState *cpu_create(const char *typename);
679 * parse_cpu_model:
680 * @cpu_model: The model string including optional parameters.
682 * processes optional parameters and registers them as global properties
684 * Returns: type of CPU to create or prints error and terminates process
685 * if an error occurred.
687 const char *parse_cpu_model(const char *cpu_model);
690 * cpu_has_work:
691 * @cpu: The vCPU to check.
693 * Checks whether the CPU has work to do.
695 * Returns: %true if the CPU has work, %false otherwise.
697 static inline bool cpu_has_work(CPUState *cpu)
699 CPUClass *cc = CPU_GET_CLASS(cpu);
701 g_assert(cc->has_work);
702 return cc->has_work(cpu);
706 * qemu_cpu_is_self:
707 * @cpu: The vCPU to check against.
709 * Checks whether the caller is executing on the vCPU thread.
711 * Returns: %true if called from @cpu's thread, %false otherwise.
713 bool qemu_cpu_is_self(CPUState *cpu);
716 * qemu_cpu_kick:
717 * @cpu: The vCPU to kick.
719 * Kicks @cpu's thread.
721 void qemu_cpu_kick(CPUState *cpu);
724 * cpu_is_stopped:
725 * @cpu: The CPU to check.
727 * Checks whether the CPU is stopped.
729 * Returns: %true if run state is not running or if artificially stopped;
730 * %false otherwise.
732 bool cpu_is_stopped(CPUState *cpu);
735 * do_run_on_cpu:
736 * @cpu: The vCPU to run on.
737 * @func: The function to be executed.
738 * @data: Data to pass to the function.
739 * @mutex: Mutex to release while waiting for @func to run.
741 * Used internally in the implementation of run_on_cpu.
743 void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data,
744 QemuMutex *mutex);
747 * run_on_cpu:
748 * @cpu: The vCPU to run on.
749 * @func: The function to be executed.
750 * @data: Data to pass to the function.
752 * Schedules the function @func for execution on the vCPU @cpu.
754 void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
757 * async_run_on_cpu:
758 * @cpu: The vCPU to run on.
759 * @func: The function to be executed.
760 * @data: Data to pass to the function.
762 * Schedules the function @func for execution on the vCPU @cpu asynchronously.
764 void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
767 * async_safe_run_on_cpu:
768 * @cpu: The vCPU to run on.
769 * @func: The function to be executed.
770 * @data: Data to pass to the function.
772 * Schedules the function @func for execution on the vCPU @cpu asynchronously,
773 * while all other vCPUs are sleeping.
775 * Unlike run_on_cpu and async_run_on_cpu, the function is run outside the
776 * BQL.
778 void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
781 * qemu_get_cpu:
782 * @index: The CPUState@cpu_index value of the CPU to obtain.
784 * Gets a CPU matching @index.
786 * Returns: The CPU or %NULL if there is no matching CPU.
788 CPUState *qemu_get_cpu(int index);
791 * cpu_exists:
792 * @id: Guest-exposed CPU ID to lookup.
794 * Search for CPU with specified ID.
796 * Returns: %true - CPU is found, %false - CPU isn't found.
798 bool cpu_exists(int64_t id);
801 * cpu_by_arch_id:
802 * @id: Guest-exposed CPU ID of the CPU to obtain.
804 * Get a CPU with matching @id.
806 * Returns: The CPU or %NULL if there is no matching CPU.
808 CPUState *cpu_by_arch_id(int64_t id);
811 * cpu_throttle_set:
812 * @new_throttle_pct: Percent of sleep time. Valid range is 1 to 99.
814 * Throttles all vcpus by forcing them to sleep for the given percentage of
815 * time. A throttle_percentage of 25 corresponds to a 75% duty cycle roughly.
816 * (example: 10ms sleep for every 30ms awake).
818 * cpu_throttle_set can be called as needed to adjust new_throttle_pct.
819 * Once the throttling starts, it will remain in effect until cpu_throttle_stop
820 * is called.
822 void cpu_throttle_set(int new_throttle_pct);
825 * cpu_throttle_stop:
827 * Stops the vcpu throttling started by cpu_throttle_set.
829 void cpu_throttle_stop(void);
832 * cpu_throttle_active:
834 * Returns: %true if the vcpus are currently being throttled, %false otherwise.
836 bool cpu_throttle_active(void);
839 * cpu_throttle_get_percentage:
841 * Returns the vcpu throttle percentage. See cpu_throttle_set for details.
843 * Returns: The throttle percentage in range 1 to 99.
845 int cpu_throttle_get_percentage(void);
847 #ifndef CONFIG_USER_ONLY
849 typedef void (*CPUInterruptHandler)(CPUState *, int);
851 extern CPUInterruptHandler cpu_interrupt_handler;
854 * cpu_interrupt:
855 * @cpu: The CPU to set an interrupt on.
856 * @mask: The interupts to set.
858 * Invokes the interrupt handler.
860 static inline void cpu_interrupt(CPUState *cpu, int mask)
862 cpu_interrupt_handler(cpu, mask);
865 #else /* USER_ONLY */
867 void cpu_interrupt(CPUState *cpu, int mask);
869 #endif /* USER_ONLY */
871 #ifdef NEED_CPU_H
873 #ifdef CONFIG_SOFTMMU
874 static inline void cpu_unassigned_access(CPUState *cpu, hwaddr addr,
875 bool is_write, bool is_exec,
876 int opaque, unsigned size)
878 CPUClass *cc = CPU_GET_CLASS(cpu);
880 if (cc->do_unassigned_access) {
881 cc->do_unassigned_access(cpu, addr, is_write, is_exec, opaque, size);
885 static inline void cpu_unaligned_access(CPUState *cpu, vaddr addr,
886 MMUAccessType access_type,
887 int mmu_idx, uintptr_t retaddr)
889 CPUClass *cc = CPU_GET_CLASS(cpu);
891 cc->do_unaligned_access(cpu, addr, access_type, mmu_idx, retaddr);
894 static inline void cpu_transaction_failed(CPUState *cpu, hwaddr physaddr,
895 vaddr addr, unsigned size,
896 MMUAccessType access_type,
897 int mmu_idx, MemTxAttrs attrs,
898 MemTxResult response,
899 uintptr_t retaddr)
901 CPUClass *cc = CPU_GET_CLASS(cpu);
903 if (!cpu->ignore_memory_transaction_failures && cc->do_transaction_failed) {
904 cc->do_transaction_failed(cpu, physaddr, addr, size, access_type,
905 mmu_idx, attrs, response, retaddr);
908 #endif
910 #endif /* NEED_CPU_H */
913 * cpu_set_pc:
914 * @cpu: The CPU to set the program counter for.
915 * @addr: Program counter value.
917 * Sets the program counter for a CPU.
919 static inline void cpu_set_pc(CPUState *cpu, vaddr addr)
921 CPUClass *cc = CPU_GET_CLASS(cpu);
923 cc->set_pc(cpu, addr);
927 * cpu_reset_interrupt:
928 * @cpu: The CPU to clear the interrupt on.
929 * @mask: The interrupt mask to clear.
931 * Resets interrupts on the vCPU @cpu.
933 void cpu_reset_interrupt(CPUState *cpu, int mask);
936 * cpu_exit:
937 * @cpu: The CPU to exit.
939 * Requests the CPU @cpu to exit execution.
941 void cpu_exit(CPUState *cpu);
944 * cpu_resume:
945 * @cpu: The CPU to resume.
947 * Resumes CPU, i.e. puts CPU into runnable state.
949 void cpu_resume(CPUState *cpu);
952 * cpu_remove:
953 * @cpu: The CPU to remove.
955 * Requests the CPU to be removed.
957 void cpu_remove(CPUState *cpu);
960 * cpu_remove_sync:
961 * @cpu: The CPU to remove.
963 * Requests the CPU to be removed and waits till it is removed.
965 void cpu_remove_sync(CPUState *cpu);
968 * process_queued_cpu_work() - process all items on CPU work queue
969 * @cpu: The CPU which work queue to process.
971 void process_queued_cpu_work(CPUState *cpu);
974 * cpu_exec_start:
975 * @cpu: The CPU for the current thread.
977 * Record that a CPU has started execution and can be interrupted with
978 * cpu_exit.
980 void cpu_exec_start(CPUState *cpu);
983 * cpu_exec_end:
984 * @cpu: The CPU for the current thread.
986 * Record that a CPU has stopped execution and exclusive sections
987 * can be executed without interrupting it.
989 void cpu_exec_end(CPUState *cpu);
992 * start_exclusive:
994 * Wait for a concurrent exclusive section to end, and then start
995 * a section of work that is run while other CPUs are not running
996 * between cpu_exec_start and cpu_exec_end. CPUs that are running
997 * cpu_exec are exited immediately. CPUs that call cpu_exec_start
998 * during the exclusive section go to sleep until this CPU calls
999 * end_exclusive.
1001 void start_exclusive(void);
1004 * end_exclusive:
1006 * Concludes an exclusive execution section started by start_exclusive.
1008 void end_exclusive(void);
1011 * qemu_init_vcpu:
1012 * @cpu: The vCPU to initialize.
1014 * Initializes a vCPU.
1016 void qemu_init_vcpu(CPUState *cpu);
1018 #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
1019 #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
1020 #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
1023 * cpu_single_step:
1024 * @cpu: CPU to the flags for.
1025 * @enabled: Flags to enable.
1027 * Enables or disables single-stepping for @cpu.
1029 void cpu_single_step(CPUState *cpu, int enabled);
1031 /* Breakpoint/watchpoint flags */
1032 #define BP_MEM_READ 0x01
1033 #define BP_MEM_WRITE 0x02
1034 #define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE)
1035 #define BP_STOP_BEFORE_ACCESS 0x04
1036 /* 0x08 currently unused */
1037 #define BP_GDB 0x10
1038 #define BP_CPU 0x20
1039 #define BP_ANY (BP_GDB | BP_CPU)
1040 #define BP_WATCHPOINT_HIT_READ 0x40
1041 #define BP_WATCHPOINT_HIT_WRITE 0x80
1042 #define BP_WATCHPOINT_HIT (BP_WATCHPOINT_HIT_READ | BP_WATCHPOINT_HIT_WRITE)
1044 int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
1045 CPUBreakpoint **breakpoint);
1046 int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags);
1047 void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint);
1048 void cpu_breakpoint_remove_all(CPUState *cpu, int mask);
1050 /* Return true if PC matches an installed breakpoint. */
1051 static inline bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask)
1053 CPUBreakpoint *bp;
1055 if (unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) {
1056 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
1057 if (bp->pc == pc && (bp->flags & mask)) {
1058 return true;
1062 return false;
1065 int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
1066 int flags, CPUWatchpoint **watchpoint);
1067 int cpu_watchpoint_remove(CPUState *cpu, vaddr addr,
1068 vaddr len, int flags);
1069 void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint);
1070 void cpu_watchpoint_remove_all(CPUState *cpu, int mask);
1073 * cpu_get_address_space:
1074 * @cpu: CPU to get address space from
1075 * @asidx: index identifying which address space to get
1077 * Return the requested address space of this CPU. @asidx
1078 * specifies which address space to read.
1080 AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx);
1082 void QEMU_NORETURN cpu_abort(CPUState *cpu, const char *fmt, ...)
1083 GCC_FMT_ATTR(2, 3);
1084 extern Property cpu_common_props[];
1085 void cpu_exec_initfn(CPUState *cpu);
1086 void cpu_exec_realizefn(CPUState *cpu, Error **errp);
1087 void cpu_exec_unrealizefn(CPUState *cpu);
1089 #ifdef NEED_CPU_H
1091 #ifdef CONFIG_SOFTMMU
1092 extern const struct VMStateDescription vmstate_cpu_common;
1093 #else
1094 #define vmstate_cpu_common vmstate_dummy
1095 #endif
1097 #define VMSTATE_CPU() { \
1098 .name = "parent_obj", \
1099 .size = sizeof(CPUState), \
1100 .vmsd = &vmstate_cpu_common, \
1101 .flags = VMS_STRUCT, \
1102 .offset = 0, \
1105 #endif /* NEED_CPU_H */
1107 #define UNASSIGNED_CPU_INDEX -1
1109 #endif