tcg: Add CPUState cflags_next_tb
[qemu/ar7.git] / include / qom / cpu.h
blobfa4b0c9dba728e30ffe70d53db156383ca423792
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 "qemu/bitmap.h"
28 #include "qemu/queue.h"
29 #include "qemu/thread.h"
31 typedef int (*WriteCoreDumpFunction)(const void *buf, size_t size,
32 void *opaque);
34 /**
35 * vaddr:
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
46 /**
47 * SECTION:cpu
48 * @section_id: QEMU-cpu
49 * @title: CPU Class
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
57 * an unchecked cast.
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 {
65 MMU_DATA_LOAD = 0,
66 MMU_DATA_STORE = 1,
67 MMU_INST_FETCH = 2
68 } MMUAccessType;
70 typedef struct CPUWatchpoint CPUWatchpoint;
72 typedef void (*CPUUnassignedAccess)(CPUState *cpu, hwaddr addr,
73 bool is_write, bool is_exec, int opaque,
74 unsigned size);
76 struct TranslationBlock;
78 /**
79 * CPUClass:
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
105 * #TranslationBlock.
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 {
144 /*< private >*/
145 DeviceClass parent_class;
146 /*< public >*/
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,
167 int flags);
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,
174 Error **errp);
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,
178 int mmu_index);
179 hwaddr (*get_phys_page_debug)(CPUState *cpu, vaddr addr);
180 hwaddr (*get_phys_page_attrs_debug)(CPUState *cpu, vaddr addr,
181 MemTxAttrs *attrs);
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,
191 void *opaque);
192 int (*write_elf32_note)(WriteCoreDumpFunction f, CPUState *cpu,
193 int cpuid, void *opaque);
194 int (*write_elf32_qemunote)(WriteCoreDumpFunction f, CPUState *cpu,
195 void *opaque);
197 const struct VMStateDescription *vmsd;
198 const char *gdb_core_xml_file;
199 gchar * (*gdb_arch_name)(CPUState *cpu);
201 void (*cpu_exec_enter)(CPUState *cpu);
202 void (*cpu_exec_exit)(CPUState *cpu);
203 bool (*cpu_exec_interrupt)(CPUState *cpu, int interrupt_request);
205 void (*disas_set_info)(CPUState *cpu, disassemble_info *info);
206 vaddr (*adjust_watchpoint_address)(CPUState *cpu, vaddr addr, int len);
207 void (*tcg_initialize)(void);
209 /* Keep non-pointer data at the end to minimize holes. */
210 int gdb_num_core_regs;
211 bool gdb_stop_before_watchpoint;
212 bool tcg_initialized;
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 bool unplug;
345 bool crash_occurred;
346 bool exit_request;
347 uint32_t cflags_next_tb;
348 /* updates protected by BQL */
349 uint32_t interrupt_request;
350 int singlestep_enabled;
351 int64_t icount_budget;
352 int64_t icount_extra;
353 sigjmp_buf jmp_env;
355 QemuMutex work_mutex;
356 struct qemu_work_item *queued_work_first, *queued_work_last;
358 CPUAddressSpace *cpu_ases;
359 int num_ases;
360 AddressSpace *as;
361 MemoryRegion *memory;
363 void *env_ptr; /* CPUArchState */
365 /* Accessed in parallel; all accesses must be atomic */
366 struct TranslationBlock *tb_jmp_cache[TB_JMP_CACHE_SIZE];
368 struct GDBRegisterState *gdb_regs;
369 int gdb_num_regs;
370 int gdb_num_g_regs;
371 QTAILQ_ENTRY(CPUState) node;
373 /* ice debug support */
374 QTAILQ_HEAD(breakpoints_head, CPUBreakpoint) breakpoints;
376 QTAILQ_HEAD(watchpoints_head, CPUWatchpoint) watchpoints;
377 CPUWatchpoint *watchpoint_hit;
379 void *opaque;
381 /* In order to avoid passing too many arguments to the MMIO helpers,
382 * we store some rarely used information in the CPU context.
384 uintptr_t mem_io_pc;
385 vaddr mem_io_vaddr;
387 int kvm_fd;
388 struct KVMState *kvm_state;
389 struct kvm_run *kvm_run;
391 /* Used for events with 'vcpu' and *without* the 'disabled' properties */
392 DECLARE_BITMAP(trace_dstate_delayed, CPU_TRACE_DSTATE_MAX_EVENTS);
393 DECLARE_BITMAP(trace_dstate, CPU_TRACE_DSTATE_MAX_EVENTS);
395 /* TODO Move common fields from CPUArchState here. */
396 int cpu_index;
397 uint32_t halted;
398 uint32_t can_do_io;
399 int32_t exception_index;
401 /* shared by kvm, hax and hvf */
402 bool vcpu_dirty;
404 /* Used to keep track of an outstanding cpu throttle thread for migration
405 * autoconverge
407 bool throttle_thread_scheduled;
409 bool ignore_memory_transaction_failures;
411 /* Note that this is accessed at the start of every TB via a negative
412 offset from AREG0. Leave this field at the end so as to make the
413 (absolute value) offset as small as possible. This reduces code
414 size, especially for hosts without large memory offsets. */
415 union {
416 uint32_t u32;
417 icount_decr_u16 u16;
418 } icount_decr;
420 struct hax_vcpu_state *hax_vcpu;
422 /* The pending_tlb_flush flag is set and cleared atomically to
423 * avoid potential races. The aim of the flag is to avoid
424 * unnecessary flushes.
426 uint16_t pending_tlb_flush;
429 QTAILQ_HEAD(CPUTailQ, CPUState);
430 extern struct CPUTailQ cpus;
431 #define CPU_NEXT(cpu) QTAILQ_NEXT(cpu, node)
432 #define CPU_FOREACH(cpu) QTAILQ_FOREACH(cpu, &cpus, node)
433 #define CPU_FOREACH_SAFE(cpu, next_cpu) \
434 QTAILQ_FOREACH_SAFE(cpu, &cpus, node, next_cpu)
435 #define CPU_FOREACH_REVERSE(cpu) \
436 QTAILQ_FOREACH_REVERSE(cpu, &cpus, CPUTailQ, node)
437 #define first_cpu QTAILQ_FIRST(&cpus)
439 extern __thread CPUState *current_cpu;
441 static inline void cpu_tb_jmp_cache_clear(CPUState *cpu)
443 unsigned int i;
445 for (i = 0; i < TB_JMP_CACHE_SIZE; i++) {
446 atomic_set(&cpu->tb_jmp_cache[i], NULL);
451 * qemu_tcg_mttcg_enabled:
452 * Check whether we are running MultiThread TCG or not.
454 * Returns: %true if we are in MTTCG mode %false otherwise.
456 extern bool mttcg_enabled;
457 #define qemu_tcg_mttcg_enabled() (mttcg_enabled)
460 * cpu_paging_enabled:
461 * @cpu: The CPU whose state is to be inspected.
463 * Returns: %true if paging is enabled, %false otherwise.
465 bool cpu_paging_enabled(const CPUState *cpu);
468 * cpu_get_memory_mapping:
469 * @cpu: The CPU whose memory mappings are to be obtained.
470 * @list: Where to write the memory mappings to.
471 * @errp: Pointer for reporting an #Error.
473 void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
474 Error **errp);
477 * cpu_write_elf64_note:
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_note(WriteCoreDumpFunction f, CPUState *cpu,
484 int cpuid, void *opaque);
487 * cpu_write_elf64_qemunote:
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_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
494 void *opaque);
497 * cpu_write_elf32_note:
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_note(WriteCoreDumpFunction f, CPUState *cpu,
504 int cpuid, void *opaque);
507 * cpu_write_elf32_qemunote:
508 * @f: pointer to a function that writes memory to a file
509 * @cpu: The CPU whose memory is to be dumped
510 * @cpuid: ID number of the CPU
511 * @opaque: pointer to the CPUState struct
513 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
514 void *opaque);
517 * cpu_get_crash_info:
518 * @cpu: The CPU to get crash information for
520 * Gets the previously saved crash information.
521 * Caller is responsible for freeing the data.
523 GuestPanicInformation *cpu_get_crash_info(CPUState *cpu);
526 * CPUDumpFlags:
527 * @CPU_DUMP_CODE:
528 * @CPU_DUMP_FPU: dump FPU register state, not just integer
529 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
531 enum CPUDumpFlags {
532 CPU_DUMP_CODE = 0x00010000,
533 CPU_DUMP_FPU = 0x00020000,
534 CPU_DUMP_CCOP = 0x00040000,
538 * cpu_dump_state:
539 * @cpu: The CPU whose state is to be dumped.
540 * @f: File to dump to.
541 * @cpu_fprintf: Function to dump with.
542 * @flags: Flags what to dump.
544 * Dumps CPU state.
546 void cpu_dump_state(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
547 int flags);
550 * cpu_dump_statistics:
551 * @cpu: The CPU whose state is to be dumped.
552 * @f: File to dump to.
553 * @cpu_fprintf: Function to dump with.
554 * @flags: Flags what to dump.
556 * Dumps CPU statistics.
558 void cpu_dump_statistics(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
559 int flags);
561 #ifndef CONFIG_USER_ONLY
563 * cpu_get_phys_page_attrs_debug:
564 * @cpu: The CPU to obtain the physical page address for.
565 * @addr: The virtual address.
566 * @attrs: Updated on return with the memory transaction attributes to use
567 * for this access.
569 * Obtains the physical page corresponding to a virtual one, together
570 * with the corresponding memory transaction attributes to use for the access.
571 * Use it only for debugging because no protection checks are done.
573 * Returns: Corresponding physical page address or -1 if no page found.
575 static inline hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr,
576 MemTxAttrs *attrs)
578 CPUClass *cc = CPU_GET_CLASS(cpu);
580 if (cc->get_phys_page_attrs_debug) {
581 return cc->get_phys_page_attrs_debug(cpu, addr, attrs);
583 /* Fallback for CPUs which don't implement the _attrs_ hook */
584 *attrs = MEMTXATTRS_UNSPECIFIED;
585 return cc->get_phys_page_debug(cpu, addr);
589 * cpu_get_phys_page_debug:
590 * @cpu: The CPU to obtain the physical page address for.
591 * @addr: The virtual address.
593 * Obtains the physical page corresponding to a virtual one.
594 * Use it only for debugging because no protection checks are done.
596 * Returns: Corresponding physical page address or -1 if no page found.
598 static inline hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr)
600 MemTxAttrs attrs = {};
602 return cpu_get_phys_page_attrs_debug(cpu, addr, &attrs);
605 /** cpu_asidx_from_attrs:
606 * @cpu: CPU
607 * @attrs: memory transaction attributes
609 * Returns the address space index specifying the CPU AddressSpace
610 * to use for a memory access with the given transaction attributes.
612 static inline int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs)
614 CPUClass *cc = CPU_GET_CLASS(cpu);
616 if (cc->asidx_from_attrs) {
617 return cc->asidx_from_attrs(cpu, attrs);
619 return 0;
621 #endif
624 * cpu_list_add:
625 * @cpu: The CPU to be added to the list of CPUs.
627 void cpu_list_add(CPUState *cpu);
630 * cpu_list_remove:
631 * @cpu: The CPU to be removed from the list of CPUs.
633 void cpu_list_remove(CPUState *cpu);
636 * cpu_reset:
637 * @cpu: The CPU whose state is to be reset.
639 void cpu_reset(CPUState *cpu);
642 * cpu_class_by_name:
643 * @typename: The CPU base type.
644 * @cpu_model: The model string without any parameters.
646 * Looks up a CPU #ObjectClass matching name @cpu_model.
648 * Returns: A #CPUClass or %NULL if not matching class is found.
650 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
653 * cpu_create:
654 * @typename: The CPU type.
656 * Instantiates a CPU and realizes the CPU.
658 * Returns: A #CPUState or %NULL if an error occurred.
660 CPUState *cpu_create(const char *typename);
663 * cpu_parse_cpu_model:
664 * @typename: The CPU base type or CPU type.
665 * @cpu_model: The model string including optional parameters.
667 * processes optional parameters and registers them as global properties
669 * Returns: type of CPU to create or prints error and terminates process
670 * if an error occurred.
672 const char *cpu_parse_cpu_model(const char *typename, const char *cpu_model);
675 * cpu_generic_init:
676 * @typename: The CPU base type.
677 * @cpu_model: The model string including optional parameters.
679 * Instantiates a CPU, processes optional parameters and realizes the CPU.
681 * Returns: A #CPUState or %NULL if an error occurred.
683 CPUState *cpu_generic_init(const char *typename, const char *cpu_model);
686 * cpu_has_work:
687 * @cpu: The vCPU to check.
689 * Checks whether the CPU has work to do.
691 * Returns: %true if the CPU has work, %false otherwise.
693 static inline bool cpu_has_work(CPUState *cpu)
695 CPUClass *cc = CPU_GET_CLASS(cpu);
697 g_assert(cc->has_work);
698 return cc->has_work(cpu);
702 * qemu_cpu_is_self:
703 * @cpu: The vCPU to check against.
705 * Checks whether the caller is executing on the vCPU thread.
707 * Returns: %true if called from @cpu's thread, %false otherwise.
709 bool qemu_cpu_is_self(CPUState *cpu);
712 * qemu_cpu_kick:
713 * @cpu: The vCPU to kick.
715 * Kicks @cpu's thread.
717 void qemu_cpu_kick(CPUState *cpu);
720 * cpu_is_stopped:
721 * @cpu: The CPU to check.
723 * Checks whether the CPU is stopped.
725 * Returns: %true if run state is not running or if artificially stopped;
726 * %false otherwise.
728 bool cpu_is_stopped(CPUState *cpu);
731 * do_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.
735 * @mutex: Mutex to release while waiting for @func to run.
737 * Used internally in the implementation of run_on_cpu.
739 void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data,
740 QemuMutex *mutex);
743 * run_on_cpu:
744 * @cpu: The vCPU to run on.
745 * @func: The function to be executed.
746 * @data: Data to pass to the function.
748 * Schedules the function @func for execution on the vCPU @cpu.
750 void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
753 * async_run_on_cpu:
754 * @cpu: The vCPU to run on.
755 * @func: The function to be executed.
756 * @data: Data to pass to the function.
758 * Schedules the function @func for execution on the vCPU @cpu asynchronously.
760 void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
763 * async_safe_run_on_cpu:
764 * @cpu: The vCPU to run on.
765 * @func: The function to be executed.
766 * @data: Data to pass to the function.
768 * Schedules the function @func for execution on the vCPU @cpu asynchronously,
769 * while all other vCPUs are sleeping.
771 * Unlike run_on_cpu and async_run_on_cpu, the function is run outside the
772 * BQL.
774 void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
777 * qemu_get_cpu:
778 * @index: The CPUState@cpu_index value of the CPU to obtain.
780 * Gets a CPU matching @index.
782 * Returns: The CPU or %NULL if there is no matching CPU.
784 CPUState *qemu_get_cpu(int index);
787 * cpu_exists:
788 * @id: Guest-exposed CPU ID to lookup.
790 * Search for CPU with specified ID.
792 * Returns: %true - CPU is found, %false - CPU isn't found.
794 bool cpu_exists(int64_t id);
797 * cpu_by_arch_id:
798 * @id: Guest-exposed CPU ID of the CPU to obtain.
800 * Get a CPU with matching @id.
802 * Returns: The CPU or %NULL if there is no matching CPU.
804 CPUState *cpu_by_arch_id(int64_t id);
807 * cpu_throttle_set:
808 * @new_throttle_pct: Percent of sleep time. Valid range is 1 to 99.
810 * Throttles all vcpus by forcing them to sleep for the given percentage of
811 * time. A throttle_percentage of 25 corresponds to a 75% duty cycle roughly.
812 * (example: 10ms sleep for every 30ms awake).
814 * cpu_throttle_set can be called as needed to adjust new_throttle_pct.
815 * Once the throttling starts, it will remain in effect until cpu_throttle_stop
816 * is called.
818 void cpu_throttle_set(int new_throttle_pct);
821 * cpu_throttle_stop:
823 * Stops the vcpu throttling started by cpu_throttle_set.
825 void cpu_throttle_stop(void);
828 * cpu_throttle_active:
830 * Returns: %true if the vcpus are currently being throttled, %false otherwise.
832 bool cpu_throttle_active(void);
835 * cpu_throttle_get_percentage:
837 * Returns the vcpu throttle percentage. See cpu_throttle_set for details.
839 * Returns: The throttle percentage in range 1 to 99.
841 int cpu_throttle_get_percentage(void);
843 #ifndef CONFIG_USER_ONLY
845 typedef void (*CPUInterruptHandler)(CPUState *, int);
847 extern CPUInterruptHandler cpu_interrupt_handler;
850 * cpu_interrupt:
851 * @cpu: The CPU to set an interrupt on.
852 * @mask: The interupts to set.
854 * Invokes the interrupt handler.
856 static inline void cpu_interrupt(CPUState *cpu, int mask)
858 cpu_interrupt_handler(cpu, mask);
861 #else /* USER_ONLY */
863 void cpu_interrupt(CPUState *cpu, int mask);
865 #endif /* USER_ONLY */
867 #ifdef NEED_CPU_H
869 #ifdef CONFIG_SOFTMMU
870 static inline void cpu_unassigned_access(CPUState *cpu, hwaddr addr,
871 bool is_write, bool is_exec,
872 int opaque, unsigned size)
874 CPUClass *cc = CPU_GET_CLASS(cpu);
876 if (cc->do_unassigned_access) {
877 cc->do_unassigned_access(cpu, addr, is_write, is_exec, opaque, size);
881 static inline void cpu_unaligned_access(CPUState *cpu, vaddr addr,
882 MMUAccessType access_type,
883 int mmu_idx, uintptr_t retaddr)
885 CPUClass *cc = CPU_GET_CLASS(cpu);
887 cc->do_unaligned_access(cpu, addr, access_type, mmu_idx, retaddr);
890 static inline void cpu_transaction_failed(CPUState *cpu, hwaddr physaddr,
891 vaddr addr, unsigned size,
892 MMUAccessType access_type,
893 int mmu_idx, MemTxAttrs attrs,
894 MemTxResult response,
895 uintptr_t retaddr)
897 CPUClass *cc = CPU_GET_CLASS(cpu);
899 if (!cpu->ignore_memory_transaction_failures && cc->do_transaction_failed) {
900 cc->do_transaction_failed(cpu, physaddr, addr, size, access_type,
901 mmu_idx, attrs, response, retaddr);
904 #endif
906 #endif /* NEED_CPU_H */
909 * cpu_set_pc:
910 * @cpu: The CPU to set the program counter for.
911 * @addr: Program counter value.
913 * Sets the program counter for a CPU.
915 static inline void cpu_set_pc(CPUState *cpu, vaddr addr)
917 CPUClass *cc = CPU_GET_CLASS(cpu);
919 cc->set_pc(cpu, addr);
923 * cpu_reset_interrupt:
924 * @cpu: The CPU to clear the interrupt on.
925 * @mask: The interrupt mask to clear.
927 * Resets interrupts on the vCPU @cpu.
929 void cpu_reset_interrupt(CPUState *cpu, int mask);
932 * cpu_exit:
933 * @cpu: The CPU to exit.
935 * Requests the CPU @cpu to exit execution.
937 void cpu_exit(CPUState *cpu);
940 * cpu_resume:
941 * @cpu: The CPU to resume.
943 * Resumes CPU, i.e. puts CPU into runnable state.
945 void cpu_resume(CPUState *cpu);
948 * cpu_remove:
949 * @cpu: The CPU to remove.
951 * Requests the CPU to be removed.
953 void cpu_remove(CPUState *cpu);
956 * cpu_remove_sync:
957 * @cpu: The CPU to remove.
959 * Requests the CPU to be removed and waits till it is removed.
961 void cpu_remove_sync(CPUState *cpu);
964 * process_queued_cpu_work() - process all items on CPU work queue
965 * @cpu: The CPU which work queue to process.
967 void process_queued_cpu_work(CPUState *cpu);
970 * cpu_exec_start:
971 * @cpu: The CPU for the current thread.
973 * Record that a CPU has started execution and can be interrupted with
974 * cpu_exit.
976 void cpu_exec_start(CPUState *cpu);
979 * cpu_exec_end:
980 * @cpu: The CPU for the current thread.
982 * Record that a CPU has stopped execution and exclusive sections
983 * can be executed without interrupting it.
985 void cpu_exec_end(CPUState *cpu);
988 * start_exclusive:
990 * Wait for a concurrent exclusive section to end, and then start
991 * a section of work that is run while other CPUs are not running
992 * between cpu_exec_start and cpu_exec_end. CPUs that are running
993 * cpu_exec are exited immediately. CPUs that call cpu_exec_start
994 * during the exclusive section go to sleep until this CPU calls
995 * end_exclusive.
997 void start_exclusive(void);
1000 * end_exclusive:
1002 * Concludes an exclusive execution section started by start_exclusive.
1004 void end_exclusive(void);
1007 * qemu_init_vcpu:
1008 * @cpu: The vCPU to initialize.
1010 * Initializes a vCPU.
1012 void qemu_init_vcpu(CPUState *cpu);
1014 #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
1015 #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
1016 #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
1019 * cpu_single_step:
1020 * @cpu: CPU to the flags for.
1021 * @enabled: Flags to enable.
1023 * Enables or disables single-stepping for @cpu.
1025 void cpu_single_step(CPUState *cpu, int enabled);
1027 /* Breakpoint/watchpoint flags */
1028 #define BP_MEM_READ 0x01
1029 #define BP_MEM_WRITE 0x02
1030 #define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE)
1031 #define BP_STOP_BEFORE_ACCESS 0x04
1032 /* 0x08 currently unused */
1033 #define BP_GDB 0x10
1034 #define BP_CPU 0x20
1035 #define BP_ANY (BP_GDB | BP_CPU)
1036 #define BP_WATCHPOINT_HIT_READ 0x40
1037 #define BP_WATCHPOINT_HIT_WRITE 0x80
1038 #define BP_WATCHPOINT_HIT (BP_WATCHPOINT_HIT_READ | BP_WATCHPOINT_HIT_WRITE)
1040 int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
1041 CPUBreakpoint **breakpoint);
1042 int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags);
1043 void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint);
1044 void cpu_breakpoint_remove_all(CPUState *cpu, int mask);
1046 /* Return true if PC matches an installed breakpoint. */
1047 static inline bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask)
1049 CPUBreakpoint *bp;
1051 if (unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) {
1052 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
1053 if (bp->pc == pc && (bp->flags & mask)) {
1054 return true;
1058 return false;
1061 int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
1062 int flags, CPUWatchpoint **watchpoint);
1063 int cpu_watchpoint_remove(CPUState *cpu, vaddr addr,
1064 vaddr len, int flags);
1065 void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint);
1066 void cpu_watchpoint_remove_all(CPUState *cpu, int mask);
1069 * cpu_get_address_space:
1070 * @cpu: CPU to get address space from
1071 * @asidx: index identifying which address space to get
1073 * Return the requested address space of this CPU. @asidx
1074 * specifies which address space to read.
1076 AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx);
1078 void QEMU_NORETURN cpu_abort(CPUState *cpu, const char *fmt, ...)
1079 GCC_FMT_ATTR(2, 3);
1080 extern Property cpu_common_props[];
1081 void cpu_exec_initfn(CPUState *cpu);
1082 void cpu_exec_realizefn(CPUState *cpu, Error **errp);
1083 void cpu_exec_unrealizefn(CPUState *cpu);
1085 #ifdef NEED_CPU_H
1087 #ifdef CONFIG_SOFTMMU
1088 extern const struct VMStateDescription vmstate_cpu_common;
1089 #else
1090 #define vmstate_cpu_common vmstate_dummy
1091 #endif
1093 #define VMSTATE_CPU() { \
1094 .name = "parent_obj", \
1095 .size = sizeof(CPUState), \
1096 .vmsd = &vmstate_cpu_common, \
1097 .flags = VMS_STRUCT, \
1098 .offset = 0, \
1101 #endif /* NEED_CPU_H */
1103 #define UNASSIGNED_CPU_INDEX -1
1105 #endif