Merge tag 'v2.12.0-rc0'
[qemu/ar7.git] / include / qom / cpu.h
blob8ec4074a318d05083f90b7e634394f2b9cd89409
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 * @cpu_exec_enter: Callback for cpu_exec preparation.
136 * @cpu_exec_exit: Callback for cpu_exec cleanup.
137 * @cpu_exec_interrupt: Callback for processing interrupts in cpu_exec.
138 * @disas_set_info: Setup architecture specific components of disassembly info
139 * @adjust_watchpoint_address: Perform a target-specific adjustment to an
140 * address before attempting to match it against watchpoints.
142 * Represents a CPU family or model.
144 typedef struct CPUClass {
145 /*< private >*/
146 DeviceClass parent_class;
147 /*< public >*/
149 ObjectClass *(*class_by_name)(const char *cpu_model);
150 void (*parse_features)(const char *typename, char *str, Error **errp);
152 void (*reset)(CPUState *cpu);
153 int reset_dump_flags;
154 bool (*has_work)(CPUState *cpu);
155 void (*do_interrupt)(CPUState *cpu);
156 CPUUnassignedAccess do_unassigned_access;
157 void (*do_unaligned_access)(CPUState *cpu, vaddr addr,
158 MMUAccessType access_type,
159 int mmu_idx, uintptr_t retaddr);
160 void (*do_transaction_failed)(CPUState *cpu, hwaddr physaddr, vaddr addr,
161 unsigned size, MMUAccessType access_type,
162 int mmu_idx, MemTxAttrs attrs,
163 MemTxResult response, uintptr_t retaddr);
164 bool (*virtio_is_big_endian)(CPUState *cpu);
165 int (*memory_rw_debug)(CPUState *cpu, vaddr addr,
166 uint8_t *buf, int len, bool is_write);
167 void (*dump_state)(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
168 int flags);
169 GuestPanicInformation* (*get_crash_info)(CPUState *cpu);
170 void (*dump_statistics)(CPUState *cpu, FILE *f,
171 fprintf_function cpu_fprintf, int flags);
172 int64_t (*get_arch_id)(CPUState *cpu);
173 bool (*get_paging_enabled)(const CPUState *cpu);
174 void (*get_memory_mapping)(CPUState *cpu, MemoryMappingList *list,
175 Error **errp);
176 void (*set_pc)(CPUState *cpu, vaddr value);
177 void (*synchronize_from_tb)(CPUState *cpu, struct TranslationBlock *tb);
178 int (*handle_mmu_fault)(CPUState *cpu, vaddr address, int size, int rw,
179 int mmu_index);
180 hwaddr (*get_phys_page_debug)(CPUState *cpu, vaddr addr);
181 hwaddr (*get_phys_page_attrs_debug)(CPUState *cpu, vaddr addr,
182 MemTxAttrs *attrs);
183 int (*asidx_from_attrs)(CPUState *cpu, MemTxAttrs attrs);
184 int (*gdb_read_register)(CPUState *cpu, uint8_t *buf, int reg);
185 int (*gdb_write_register)(CPUState *cpu, uint8_t *buf, int reg);
186 bool (*debug_check_watchpoint)(CPUState *cpu, CPUWatchpoint *wp);
187 void (*debug_excp_handler)(CPUState *cpu);
189 int (*write_elf64_note)(WriteCoreDumpFunction f, CPUState *cpu,
190 int cpuid, void *opaque);
191 int (*write_elf64_qemunote)(WriteCoreDumpFunction f, CPUState *cpu,
192 void *opaque);
193 int (*write_elf32_note)(WriteCoreDumpFunction f, CPUState *cpu,
194 int cpuid, void *opaque);
195 int (*write_elf32_qemunote)(WriteCoreDumpFunction f, CPUState *cpu,
196 void *opaque);
198 const struct VMStateDescription *vmsd;
199 const char *gdb_core_xml_file;
200 gchar * (*gdb_arch_name)(CPUState *cpu);
202 void (*cpu_exec_enter)(CPUState *cpu);
203 void (*cpu_exec_exit)(CPUState *cpu);
204 bool (*cpu_exec_interrupt)(CPUState *cpu, int interrupt_request);
206 void (*disas_set_info)(CPUState *cpu, disassemble_info *info);
207 vaddr (*adjust_watchpoint_address)(CPUState *cpu, vaddr addr, int len);
208 void (*tcg_initialize)(void);
210 /* Keep non-pointer data at the end to minimize holes. */
211 int gdb_num_core_regs;
212 bool gdb_stop_before_watchpoint;
213 } CPUClass;
215 #ifdef HOST_WORDS_BIGENDIAN
216 typedef struct icount_decr_u16 {
217 uint16_t high;
218 uint16_t low;
219 } icount_decr_u16;
220 #else
221 typedef struct icount_decr_u16 {
222 uint16_t low;
223 uint16_t high;
224 } icount_decr_u16;
225 #endif
227 typedef struct CPUBreakpoint {
228 vaddr pc;
229 int flags; /* BP_* */
230 QTAILQ_ENTRY(CPUBreakpoint) entry;
231 } CPUBreakpoint;
233 struct CPUWatchpoint {
234 vaddr vaddr;
235 vaddr len;
236 vaddr hitaddr;
237 MemTxAttrs hitattrs;
238 int flags; /* BP_* */
239 QTAILQ_ENTRY(CPUWatchpoint) entry;
242 struct KVMState;
243 struct kvm_run;
245 struct hax_vcpu_state;
247 #define TB_JMP_CACHE_BITS 12
248 #define TB_JMP_CACHE_SIZE (1 << TB_JMP_CACHE_BITS)
250 /* work queue */
252 /* The union type allows passing of 64 bit target pointers on 32 bit
253 * hosts in a single parameter
255 typedef union {
256 int host_int;
257 unsigned long host_ulong;
258 void *host_ptr;
259 vaddr target_ptr;
260 } run_on_cpu_data;
262 #define RUN_ON_CPU_HOST_PTR(p) ((run_on_cpu_data){.host_ptr = (p)})
263 #define RUN_ON_CPU_HOST_INT(i) ((run_on_cpu_data){.host_int = (i)})
264 #define RUN_ON_CPU_HOST_ULONG(ul) ((run_on_cpu_data){.host_ulong = (ul)})
265 #define RUN_ON_CPU_TARGET_PTR(v) ((run_on_cpu_data){.target_ptr = (v)})
266 #define RUN_ON_CPU_NULL RUN_ON_CPU_HOST_PTR(NULL)
268 typedef void (*run_on_cpu_func)(CPUState *cpu, run_on_cpu_data data);
270 struct qemu_work_item;
272 #define CPU_UNSET_NUMA_NODE_ID -1
273 #define CPU_TRACE_DSTATE_MAX_EVENTS 32
276 * CPUState:
277 * @cpu_index: CPU index (informative).
278 * @nr_cores: Number of cores within this CPU package.
279 * @nr_threads: Number of threads within this CPU.
280 * @running: #true if CPU is currently running (lockless).
281 * @has_waiter: #true if a CPU is currently waiting for the cpu_exec_end;
282 * valid under cpu_list_lock.
283 * @created: Indicates whether the CPU thread has been successfully created.
284 * @interrupt_request: Indicates a pending interrupt request.
285 * @halted: Nonzero if the CPU is in suspended state.
286 * @stop: Indicates a pending stop request.
287 * @stopped: Indicates the CPU has been artificially stopped.
288 * @unplug: Indicates a pending CPU unplug request.
289 * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU
290 * @singlestep_enabled: Flags for single-stepping.
291 * @icount_extra: Instructions until next timer event.
292 * @icount_decr: Low 16 bits: number of cycles left, only used in icount mode.
293 * High 16 bits: Set to -1 to force TCG to stop executing linked TBs for this
294 * CPU and return to its top level loop (even in non-icount mode).
295 * This allows a single read-compare-cbranch-write sequence to test
296 * for both decrementer underflow and exceptions.
297 * @can_do_io: Nonzero if memory-mapped IO is safe. Deterministic execution
298 * requires that IO only be performed on the last instruction of a TB
299 * so that interrupts take effect immediately.
300 * @cpu_ases: Pointer to array of CPUAddressSpaces (which define the
301 * AddressSpaces this CPU has)
302 * @num_ases: number of CPUAddressSpaces in @cpu_ases
303 * @as: Pointer to the first AddressSpace, for the convenience of targets which
304 * only have a single AddressSpace
305 * @env_ptr: Pointer to subclass-specific CPUArchState field.
306 * @gdb_regs: Additional GDB registers.
307 * @gdb_num_regs: Number of total registers accessible to GDB.
308 * @gdb_num_g_regs: Number of registers in GDB 'g' packets.
309 * @next_cpu: Next CPU sharing TB cache.
310 * @opaque: User data.
311 * @mem_io_pc: Host Program Counter at which the memory was accessed.
312 * @mem_io_vaddr: Target virtual address at which the memory was accessed.
313 * @kvm_fd: vCPU file descriptor for KVM.
314 * @work_mutex: Lock to prevent multiple access to queued_work_*.
315 * @queued_work_first: First asynchronous work pending.
316 * @trace_dstate_delayed: Delayed changes to trace_dstate (includes all changes
317 * to @trace_dstate).
318 * @trace_dstate: Dynamic tracing state of events for this vCPU (bitmask).
319 * @ignore_memory_transaction_failures: Cached copy of the MachineState
320 * flag of the same name: allows the board to suppress calling of the
321 * CPU do_transaction_failed hook function.
323 * State of one CPU core or thread.
325 struct CPUState {
326 /*< private >*/
327 DeviceState parent_obj;
328 /*< public >*/
330 int nr_cores;
331 int nr_threads;
333 struct QemuThread *thread;
334 #ifdef _WIN32
335 HANDLE hThread;
336 #endif
337 int thread_id;
338 bool running, has_waiter;
339 struct QemuCond *halt_cond;
340 bool thread_kicked;
341 bool created;
342 bool stop;
343 bool stopped;
344 /* Endianness, false = little endian, true = big endian. */
345 bool bigendian;
346 bool unplug;
347 bool crash_occurred;
348 bool exit_request;
349 uint32_t cflags_next_tb;
350 /* updates protected by BQL */
351 uint32_t interrupt_request;
352 int singlestep_enabled;
353 int64_t icount_budget;
354 int64_t icount_extra;
355 sigjmp_buf jmp_env;
357 QemuMutex work_mutex;
358 struct qemu_work_item *queued_work_first, *queued_work_last;
360 CPUAddressSpace *cpu_ases;
361 int num_ases;
362 AddressSpace *as;
363 MemoryRegion *memory;
365 void *env_ptr; /* CPUArchState */
367 /* Accessed in parallel; all accesses must be atomic */
368 struct TranslationBlock *tb_jmp_cache[TB_JMP_CACHE_SIZE];
370 struct GDBRegisterState *gdb_regs;
371 int gdb_num_regs;
372 int gdb_num_g_regs;
373 QTAILQ_ENTRY(CPUState) node;
375 /* ice debug support */
376 QTAILQ_HEAD(breakpoints_head, CPUBreakpoint) breakpoints;
378 QTAILQ_HEAD(watchpoints_head, CPUWatchpoint) watchpoints;
379 CPUWatchpoint *watchpoint_hit;
381 void *opaque;
383 /* In order to avoid passing too many arguments to the MMIO helpers,
384 * we store some rarely used information in the CPU context.
386 uintptr_t mem_io_pc;
387 vaddr mem_io_vaddr;
389 int kvm_fd;
390 struct KVMState *kvm_state;
391 struct kvm_run *kvm_run;
393 /* Used for events with 'vcpu' and *without* the 'disabled' properties */
394 DECLARE_BITMAP(trace_dstate_delayed, CPU_TRACE_DSTATE_MAX_EVENTS);
395 DECLARE_BITMAP(trace_dstate, CPU_TRACE_DSTATE_MAX_EVENTS);
397 /* TODO Move common fields from CPUArchState here. */
398 int cpu_index;
399 uint32_t halted;
400 uint32_t can_do_io;
401 int32_t exception_index;
403 /* shared by kvm, hax and hvf */
404 bool vcpu_dirty;
406 /* Used to keep track of an outstanding cpu throttle thread for migration
407 * autoconverge
409 bool throttle_thread_scheduled;
411 bool ignore_memory_transaction_failures;
413 /* Note that this is accessed at the start of every TB via a negative
414 offset from AREG0. Leave this field at the end so as to make the
415 (absolute value) offset as small as possible. This reduces code
416 size, especially for hosts without large memory offsets. */
417 union {
418 uint32_t u32;
419 icount_decr_u16 u16;
420 } icount_decr;
422 struct hax_vcpu_state *hax_vcpu;
424 /* The pending_tlb_flush flag is set and cleared atomically to
425 * avoid potential races. The aim of the flag is to avoid
426 * unnecessary flushes.
428 uint16_t pending_tlb_flush;
430 int hvf_fd;
433 QTAILQ_HEAD(CPUTailQ, CPUState);
434 extern struct CPUTailQ cpus;
435 #define CPU_NEXT(cpu) QTAILQ_NEXT(cpu, node)
436 #define CPU_FOREACH(cpu) QTAILQ_FOREACH(cpu, &cpus, node)
437 #define CPU_FOREACH_SAFE(cpu, next_cpu) \
438 QTAILQ_FOREACH_SAFE(cpu, &cpus, node, next_cpu)
439 #define CPU_FOREACH_REVERSE(cpu) \
440 QTAILQ_FOREACH_REVERSE(cpu, &cpus, CPUTailQ, node)
441 #define first_cpu QTAILQ_FIRST(&cpus)
443 extern __thread CPUState *current_cpu;
445 static inline void cpu_tb_jmp_cache_clear(CPUState *cpu)
447 unsigned int i;
449 for (i = 0; i < TB_JMP_CACHE_SIZE; i++) {
450 atomic_set(&cpu->tb_jmp_cache[i], NULL);
455 * qemu_tcg_mttcg_enabled:
456 * Check whether we are running MultiThread TCG or not.
458 * Returns: %true if we are in MTTCG mode %false otherwise.
460 extern bool mttcg_enabled;
461 #define qemu_tcg_mttcg_enabled() (mttcg_enabled)
464 * cpu_paging_enabled:
465 * @cpu: The CPU whose state is to be inspected.
467 * Returns: %true if paging is enabled, %false otherwise.
469 bool cpu_paging_enabled(const CPUState *cpu);
472 * cpu_get_memory_mapping:
473 * @cpu: The CPU whose memory mappings are to be obtained.
474 * @list: Where to write the memory mappings to.
475 * @errp: Pointer for reporting an #Error.
477 void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
478 Error **errp);
481 * cpu_write_elf64_note:
482 * @f: pointer to a function that writes memory to a file
483 * @cpu: The CPU whose memory is to be dumped
484 * @cpuid: ID number of the CPU
485 * @opaque: pointer to the CPUState struct
487 int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
488 int cpuid, void *opaque);
491 * cpu_write_elf64_qemunote:
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_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
498 void *opaque);
501 * cpu_write_elf32_note:
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_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
508 int cpuid, void *opaque);
511 * cpu_write_elf32_qemunote:
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_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
518 void *opaque);
521 * cpu_get_crash_info:
522 * @cpu: The CPU to get crash information for
524 * Gets the previously saved crash information.
525 * Caller is responsible for freeing the data.
527 GuestPanicInformation *cpu_get_crash_info(CPUState *cpu);
530 * CPUDumpFlags:
531 * @CPU_DUMP_CODE:
532 * @CPU_DUMP_FPU: dump FPU register state, not just integer
533 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
535 enum CPUDumpFlags {
536 CPU_DUMP_CODE = 0x00010000,
537 CPU_DUMP_FPU = 0x00020000,
538 CPU_DUMP_CCOP = 0x00040000,
542 * cpu_dump_state:
543 * @cpu: The CPU whose state is to be dumped.
544 * @f: File to dump to.
545 * @cpu_fprintf: Function to dump with.
546 * @flags: Flags what to dump.
548 * Dumps CPU state.
550 void cpu_dump_state(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
551 int flags);
554 * cpu_dump_statistics:
555 * @cpu: The CPU whose state is to be dumped.
556 * @f: File to dump to.
557 * @cpu_fprintf: Function to dump with.
558 * @flags: Flags what to dump.
560 * Dumps CPU statistics.
562 void cpu_dump_statistics(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
563 int flags);
565 #ifndef CONFIG_USER_ONLY
567 * cpu_get_phys_page_attrs_debug:
568 * @cpu: The CPU to obtain the physical page address for.
569 * @addr: The virtual address.
570 * @attrs: Updated on return with the memory transaction attributes to use
571 * for this access.
573 * Obtains the physical page corresponding to a virtual one, together
574 * with the corresponding memory transaction attributes to use for the access.
575 * Use it only for debugging because no protection checks are done.
577 * Returns: Corresponding physical page address or -1 if no page found.
579 static inline hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr,
580 MemTxAttrs *attrs)
582 CPUClass *cc = CPU_GET_CLASS(cpu);
584 if (cc->get_phys_page_attrs_debug) {
585 return cc->get_phys_page_attrs_debug(cpu, addr, attrs);
587 /* Fallback for CPUs which don't implement the _attrs_ hook */
588 *attrs = MEMTXATTRS_UNSPECIFIED;
589 return cc->get_phys_page_debug(cpu, addr);
593 * cpu_get_phys_page_debug:
594 * @cpu: The CPU to obtain the physical page address for.
595 * @addr: The virtual address.
597 * Obtains the physical page corresponding to a virtual one.
598 * Use it only for debugging because no protection checks are done.
600 * Returns: Corresponding physical page address or -1 if no page found.
602 static inline hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr)
604 MemTxAttrs attrs = {};
606 return cpu_get_phys_page_attrs_debug(cpu, addr, &attrs);
609 /** cpu_asidx_from_attrs:
610 * @cpu: CPU
611 * @attrs: memory transaction attributes
613 * Returns the address space index specifying the CPU AddressSpace
614 * to use for a memory access with the given transaction attributes.
616 static inline int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs)
618 CPUClass *cc = CPU_GET_CLASS(cpu);
620 if (cc->asidx_from_attrs) {
621 return cc->asidx_from_attrs(cpu, attrs);
623 return 0;
625 #endif
628 * cpu_list_add:
629 * @cpu: The CPU to be added to the list of CPUs.
631 void cpu_list_add(CPUState *cpu);
634 * cpu_list_remove:
635 * @cpu: The CPU to be removed from the list of CPUs.
637 void cpu_list_remove(CPUState *cpu);
640 * cpu_reset:
641 * @cpu: The CPU whose state is to be reset.
643 void cpu_reset(CPUState *cpu);
646 * cpu_class_by_name:
647 * @typename: The CPU base type.
648 * @cpu_model: The model string without any parameters.
650 * Looks up a CPU #ObjectClass matching name @cpu_model.
652 * Returns: A #CPUClass or %NULL if not matching class is found.
654 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
657 * cpu_create:
658 * @typename: The CPU type.
660 * Instantiates a CPU and realizes the CPU.
662 * Returns: A #CPUState or %NULL if an error occurred.
664 CPUState *cpu_create(const char *typename);
667 * parse_cpu_model:
668 * @cpu_model: The model string including optional parameters.
670 * processes optional parameters and registers them as global properties
672 * Returns: type of CPU to create or prints error and terminates process
673 * if an error occurred.
675 const char *parse_cpu_model(const char *cpu_model);
678 * cpu_has_work:
679 * @cpu: The vCPU to check.
681 * Checks whether the CPU has work to do.
683 * Returns: %true if the CPU has work, %false otherwise.
685 static inline bool cpu_has_work(CPUState *cpu)
687 CPUClass *cc = CPU_GET_CLASS(cpu);
689 g_assert(cc->has_work);
690 return cc->has_work(cpu);
694 * qemu_cpu_is_self:
695 * @cpu: The vCPU to check against.
697 * Checks whether the caller is executing on the vCPU thread.
699 * Returns: %true if called from @cpu's thread, %false otherwise.
701 bool qemu_cpu_is_self(CPUState *cpu);
704 * qemu_cpu_kick:
705 * @cpu: The vCPU to kick.
707 * Kicks @cpu's thread.
709 void qemu_cpu_kick(CPUState *cpu);
712 * cpu_is_stopped:
713 * @cpu: The CPU to check.
715 * Checks whether the CPU is stopped.
717 * Returns: %true if run state is not running or if artificially stopped;
718 * %false otherwise.
720 bool cpu_is_stopped(CPUState *cpu);
723 * do_run_on_cpu:
724 * @cpu: The vCPU to run on.
725 * @func: The function to be executed.
726 * @data: Data to pass to the function.
727 * @mutex: Mutex to release while waiting for @func to run.
729 * Used internally in the implementation of run_on_cpu.
731 void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data,
732 QemuMutex *mutex);
735 * 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.
740 * Schedules the function @func for execution on the vCPU @cpu.
742 void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
745 * async_run_on_cpu:
746 * @cpu: The vCPU to run on.
747 * @func: The function to be executed.
748 * @data: Data to pass to the function.
750 * Schedules the function @func for execution on the vCPU @cpu asynchronously.
752 void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
755 * async_safe_run_on_cpu:
756 * @cpu: The vCPU to run on.
757 * @func: The function to be executed.
758 * @data: Data to pass to the function.
760 * Schedules the function @func for execution on the vCPU @cpu asynchronously,
761 * while all other vCPUs are sleeping.
763 * Unlike run_on_cpu and async_run_on_cpu, the function is run outside the
764 * BQL.
766 void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
769 * qemu_get_cpu:
770 * @index: The CPUState@cpu_index value of the CPU to obtain.
772 * Gets a CPU matching @index.
774 * Returns: The CPU or %NULL if there is no matching CPU.
776 CPUState *qemu_get_cpu(int index);
779 * cpu_exists:
780 * @id: Guest-exposed CPU ID to lookup.
782 * Search for CPU with specified ID.
784 * Returns: %true - CPU is found, %false - CPU isn't found.
786 bool cpu_exists(int64_t id);
789 * cpu_by_arch_id:
790 * @id: Guest-exposed CPU ID of the CPU to obtain.
792 * Get a CPU with matching @id.
794 * Returns: The CPU or %NULL if there is no matching CPU.
796 CPUState *cpu_by_arch_id(int64_t id);
799 * cpu_throttle_set:
800 * @new_throttle_pct: Percent of sleep time. Valid range is 1 to 99.
802 * Throttles all vcpus by forcing them to sleep for the given percentage of
803 * time. A throttle_percentage of 25 corresponds to a 75% duty cycle roughly.
804 * (example: 10ms sleep for every 30ms awake).
806 * cpu_throttle_set can be called as needed to adjust new_throttle_pct.
807 * Once the throttling starts, it will remain in effect until cpu_throttle_stop
808 * is called.
810 void cpu_throttle_set(int new_throttle_pct);
813 * cpu_throttle_stop:
815 * Stops the vcpu throttling started by cpu_throttle_set.
817 void cpu_throttle_stop(void);
820 * cpu_throttle_active:
822 * Returns: %true if the vcpus are currently being throttled, %false otherwise.
824 bool cpu_throttle_active(void);
827 * cpu_throttle_get_percentage:
829 * Returns the vcpu throttle percentage. See cpu_throttle_set for details.
831 * Returns: The throttle percentage in range 1 to 99.
833 int cpu_throttle_get_percentage(void);
835 #ifndef CONFIG_USER_ONLY
837 typedef void (*CPUInterruptHandler)(CPUState *, int);
839 extern CPUInterruptHandler cpu_interrupt_handler;
842 * cpu_interrupt:
843 * @cpu: The CPU to set an interrupt on.
844 * @mask: The interupts to set.
846 * Invokes the interrupt handler.
848 static inline void cpu_interrupt(CPUState *cpu, int mask)
850 cpu_interrupt_handler(cpu, mask);
853 #else /* USER_ONLY */
855 void cpu_interrupt(CPUState *cpu, int mask);
857 #endif /* USER_ONLY */
859 #ifdef NEED_CPU_H
861 #ifdef CONFIG_SOFTMMU
862 static inline void cpu_unassigned_access(CPUState *cpu, hwaddr addr,
863 bool is_write, bool is_exec,
864 int opaque, unsigned size)
866 CPUClass *cc = CPU_GET_CLASS(cpu);
868 if (cc->do_unassigned_access) {
869 cc->do_unassigned_access(cpu, addr, is_write, is_exec, opaque, size);
873 static inline void cpu_unaligned_access(CPUState *cpu, vaddr addr,
874 MMUAccessType access_type,
875 int mmu_idx, uintptr_t retaddr)
877 CPUClass *cc = CPU_GET_CLASS(cpu);
879 cc->do_unaligned_access(cpu, addr, access_type, mmu_idx, retaddr);
882 static inline void cpu_transaction_failed(CPUState *cpu, hwaddr physaddr,
883 vaddr addr, unsigned size,
884 MMUAccessType access_type,
885 int mmu_idx, MemTxAttrs attrs,
886 MemTxResult response,
887 uintptr_t retaddr)
889 CPUClass *cc = CPU_GET_CLASS(cpu);
891 if (!cpu->ignore_memory_transaction_failures && cc->do_transaction_failed) {
892 cc->do_transaction_failed(cpu, physaddr, addr, size, access_type,
893 mmu_idx, attrs, response, retaddr);
896 #endif
898 #endif /* NEED_CPU_H */
901 * cpu_set_pc:
902 * @cpu: The CPU to set the program counter for.
903 * @addr: Program counter value.
905 * Sets the program counter for a CPU.
907 static inline void cpu_set_pc(CPUState *cpu, vaddr addr)
909 CPUClass *cc = CPU_GET_CLASS(cpu);
911 cc->set_pc(cpu, addr);
915 * cpu_reset_interrupt:
916 * @cpu: The CPU to clear the interrupt on.
917 * @mask: The interrupt mask to clear.
919 * Resets interrupts on the vCPU @cpu.
921 void cpu_reset_interrupt(CPUState *cpu, int mask);
924 * cpu_exit:
925 * @cpu: The CPU to exit.
927 * Requests the CPU @cpu to exit execution.
929 void cpu_exit(CPUState *cpu);
932 * cpu_resume:
933 * @cpu: The CPU to resume.
935 * Resumes CPU, i.e. puts CPU into runnable state.
937 void cpu_resume(CPUState *cpu);
940 * cpu_remove:
941 * @cpu: The CPU to remove.
943 * Requests the CPU to be removed.
945 void cpu_remove(CPUState *cpu);
948 * cpu_remove_sync:
949 * @cpu: The CPU to remove.
951 * Requests the CPU to be removed and waits till it is removed.
953 void cpu_remove_sync(CPUState *cpu);
956 * process_queued_cpu_work() - process all items on CPU work queue
957 * @cpu: The CPU which work queue to process.
959 void process_queued_cpu_work(CPUState *cpu);
962 * cpu_exec_start:
963 * @cpu: The CPU for the current thread.
965 * Record that a CPU has started execution and can be interrupted with
966 * cpu_exit.
968 void cpu_exec_start(CPUState *cpu);
971 * cpu_exec_end:
972 * @cpu: The CPU for the current thread.
974 * Record that a CPU has stopped execution and exclusive sections
975 * can be executed without interrupting it.
977 void cpu_exec_end(CPUState *cpu);
980 * start_exclusive:
982 * Wait for a concurrent exclusive section to end, and then start
983 * a section of work that is run while other CPUs are not running
984 * between cpu_exec_start and cpu_exec_end. CPUs that are running
985 * cpu_exec are exited immediately. CPUs that call cpu_exec_start
986 * during the exclusive section go to sleep until this CPU calls
987 * end_exclusive.
989 void start_exclusive(void);
992 * end_exclusive:
994 * Concludes an exclusive execution section started by start_exclusive.
996 void end_exclusive(void);
999 * qemu_init_vcpu:
1000 * @cpu: The vCPU to initialize.
1002 * Initializes a vCPU.
1004 void qemu_init_vcpu(CPUState *cpu);
1006 #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
1007 #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
1008 #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
1011 * cpu_single_step:
1012 * @cpu: CPU to the flags for.
1013 * @enabled: Flags to enable.
1015 * Enables or disables single-stepping for @cpu.
1017 void cpu_single_step(CPUState *cpu, int enabled);
1019 /* Breakpoint/watchpoint flags */
1020 #define BP_MEM_READ 0x01
1021 #define BP_MEM_WRITE 0x02
1022 #define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE)
1023 #define BP_STOP_BEFORE_ACCESS 0x04
1024 /* 0x08 currently unused */
1025 #define BP_GDB 0x10
1026 #define BP_CPU 0x20
1027 #define BP_ANY (BP_GDB | BP_CPU)
1028 #define BP_WATCHPOINT_HIT_READ 0x40
1029 #define BP_WATCHPOINT_HIT_WRITE 0x80
1030 #define BP_WATCHPOINT_HIT (BP_WATCHPOINT_HIT_READ | BP_WATCHPOINT_HIT_WRITE)
1032 int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
1033 CPUBreakpoint **breakpoint);
1034 int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags);
1035 void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint);
1036 void cpu_breakpoint_remove_all(CPUState *cpu, int mask);
1038 /* Return true if PC matches an installed breakpoint. */
1039 static inline bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask)
1041 CPUBreakpoint *bp;
1043 if (unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) {
1044 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
1045 if (bp->pc == pc && (bp->flags & mask)) {
1046 return true;
1050 return false;
1053 int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
1054 int flags, CPUWatchpoint **watchpoint);
1055 int cpu_watchpoint_remove(CPUState *cpu, vaddr addr,
1056 vaddr len, int flags);
1057 void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint);
1058 void cpu_watchpoint_remove_all(CPUState *cpu, int mask);
1061 * cpu_get_address_space:
1062 * @cpu: CPU to get address space from
1063 * @asidx: index identifying which address space to get
1065 * Return the requested address space of this CPU. @asidx
1066 * specifies which address space to read.
1068 AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx);
1070 void QEMU_NORETURN cpu_abort(CPUState *cpu, const char *fmt, ...)
1071 GCC_FMT_ATTR(2, 3);
1072 extern Property cpu_common_props[];
1073 void cpu_exec_initfn(CPUState *cpu);
1074 void cpu_exec_realizefn(CPUState *cpu, Error **errp);
1075 void cpu_exec_unrealizefn(CPUState *cpu);
1077 #ifdef NEED_CPU_H
1079 #ifdef CONFIG_SOFTMMU
1080 extern const struct VMStateDescription vmstate_cpu_common;
1081 #else
1082 #define vmstate_cpu_common vmstate_dummy
1083 #endif
1085 #define VMSTATE_CPU() { \
1086 .name = "parent_obj", \
1087 .size = sizeof(CPUState), \
1088 .vmsd = &vmstate_cpu_common, \
1089 .flags = VMS_STRUCT, \
1090 .offset = 0, \
1093 #endif /* NEED_CPU_H */
1095 #define UNASSIGNED_CPU_INDEX -1
1097 #endif