net: cadence_gem: Add support for selecting the DMA MemoryRegion
[qemu/kevin.git] / include / qom / cpu.h
blobdc130cd3072b124e77c502bbfc95f1ccc8b92d3c
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/rcu_queue.h"
30 #include "qemu/queue.h"
31 #include "qemu/thread.h"
33 typedef int (*WriteCoreDumpFunction)(const void *buf, size_t size,
34 void *opaque);
36 /**
37 * vaddr:
38 * Type wide enough to contain any #target_ulong virtual address.
40 typedef uint64_t vaddr;
41 #define VADDR_PRId PRId64
42 #define VADDR_PRIu PRIu64
43 #define VADDR_PRIo PRIo64
44 #define VADDR_PRIx PRIx64
45 #define VADDR_PRIX PRIX64
46 #define VADDR_MAX UINT64_MAX
48 /**
49 * SECTION:cpu
50 * @section_id: QEMU-cpu
51 * @title: CPU Class
52 * @short_description: Base class for all CPUs
55 #define TYPE_CPU "cpu"
57 /* Since this macro is used a lot in hot code paths and in conjunction with
58 * FooCPU *foo_env_get_cpu(), we deviate from usual QOM practice by using
59 * an unchecked cast.
61 #define CPU(obj) ((CPUState *)(obj))
63 #define CPU_CLASS(class) OBJECT_CLASS_CHECK(CPUClass, (class), TYPE_CPU)
64 #define CPU_GET_CLASS(obj) OBJECT_GET_CLASS(CPUClass, (obj), TYPE_CPU)
66 typedef enum MMUAccessType {
67 MMU_DATA_LOAD = 0,
68 MMU_DATA_STORE = 1,
69 MMU_INST_FETCH = 2
70 } MMUAccessType;
72 typedef struct CPUWatchpoint CPUWatchpoint;
74 typedef void (*CPUUnassignedAccess)(CPUState *cpu, hwaddr addr,
75 bool is_write, bool is_exec, int opaque,
76 unsigned size);
78 struct TranslationBlock;
80 /**
81 * CPUClass:
82 * @class_by_name: Callback to map -cpu command line model name to an
83 * instantiatable CPU type.
84 * @parse_features: Callback to parse command line arguments.
85 * @reset: Callback to reset the #CPUState to its initial state.
86 * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
87 * @has_work: Callback for checking if there is work to do.
88 * @do_interrupt: Callback for interrupt handling.
89 * @do_unassigned_access: Callback for unassigned access handling.
90 * (this is deprecated: new targets should use do_transaction_failed instead)
91 * @do_unaligned_access: Callback for unaligned access handling, if
92 * the target defines #ALIGNED_ONLY.
93 * @do_transaction_failed: Callback for handling failed memory transactions
94 * (ie bus faults or external aborts; not MMU faults)
95 * @virtio_is_big_endian: Callback to return %true if a CPU which supports
96 * runtime configurable endianness is currently big-endian. Non-configurable
97 * CPUs can use the default implementation of this method. This method should
98 * not be used by any callers other than the pre-1.0 virtio devices.
99 * @memory_rw_debug: Callback for GDB memory access.
100 * @dump_state: Callback for dumping state.
101 * @dump_statistics: Callback for dumping statistics.
102 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
103 * @get_paging_enabled: Callback for inquiring whether paging is enabled.
104 * @get_memory_mapping: Callback for obtaining the memory mappings.
105 * @set_pc: Callback for setting the Program Counter register.
106 * @synchronize_from_tb: Callback for synchronizing state from a TCG
107 * #TranslationBlock.
108 * @handle_mmu_fault: Callback for handling an MMU fault.
109 * @get_phys_page_debug: Callback for obtaining a physical address.
110 * @get_phys_page_attrs_debug: Callback for obtaining a physical address and the
111 * associated memory transaction attributes to use for the access.
112 * CPUs which use memory transaction attributes should implement this
113 * instead of get_phys_page_debug.
114 * @asidx_from_attrs: Callback to return the CPU AddressSpace to use for
115 * a memory access with the specified memory transaction attributes.
116 * @gdb_read_register: Callback for letting GDB read a register.
117 * @gdb_write_register: Callback for letting GDB write a register.
118 * @debug_check_watchpoint: Callback: return true if the architectural
119 * watchpoint whose address has matched should really fire.
120 * @debug_excp_handler: Callback for handling debug exceptions.
121 * @write_elf64_note: Callback for writing a CPU-specific ELF note to a
122 * 64-bit VM coredump.
123 * @write_elf32_qemunote: Callback for writing a CPU- and QEMU-specific ELF
124 * note to a 32-bit VM coredump.
125 * @write_elf32_note: Callback for writing a CPU-specific ELF note to a
126 * 32-bit VM coredump.
127 * @write_elf32_qemunote: Callback for writing a CPU- and QEMU-specific ELF
128 * note to a 32-bit VM coredump.
129 * @vmsd: State description for migration.
130 * @gdb_num_core_regs: Number of core registers accessible to GDB.
131 * @gdb_core_xml_file: File name for core registers GDB XML description.
132 * @gdb_stop_before_watchpoint: Indicates whether GDB expects the CPU to stop
133 * before the insn which triggers a watchpoint rather than after it.
134 * @gdb_arch_name: Optional callback that returns the architecture name known
135 * to GDB. The caller must free the returned string with g_free.
136 * @gdb_get_dynamic_xml: Callback to return dynamically generated XML for the
137 * gdb stub. Returns a pointer to the XML contents for the specified XML file
138 * or NULL if the CPU doesn't have a dynamically generated content for it.
139 * @cpu_exec_enter: Callback for cpu_exec preparation.
140 * @cpu_exec_exit: Callback for cpu_exec cleanup.
141 * @cpu_exec_interrupt: Callback for processing interrupts in cpu_exec.
142 * @disas_set_info: Setup architecture specific components of disassembly info
143 * @adjust_watchpoint_address: Perform a target-specific adjustment to an
144 * address before attempting to match it against watchpoints.
146 * Represents a CPU family or model.
148 typedef struct CPUClass {
149 /*< private >*/
150 DeviceClass parent_class;
151 /*< public >*/
153 ObjectClass *(*class_by_name)(const char *cpu_model);
154 void (*parse_features)(const char *typename, char *str, Error **errp);
156 void (*reset)(CPUState *cpu);
157 int reset_dump_flags;
158 bool (*has_work)(CPUState *cpu);
159 void (*do_interrupt)(CPUState *cpu);
160 CPUUnassignedAccess do_unassigned_access;
161 void (*do_unaligned_access)(CPUState *cpu, vaddr addr,
162 MMUAccessType access_type,
163 int mmu_idx, uintptr_t retaddr);
164 void (*do_transaction_failed)(CPUState *cpu, hwaddr physaddr, vaddr addr,
165 unsigned size, MMUAccessType access_type,
166 int mmu_idx, MemTxAttrs attrs,
167 MemTxResult response, uintptr_t retaddr);
168 bool (*virtio_is_big_endian)(CPUState *cpu);
169 int (*memory_rw_debug)(CPUState *cpu, vaddr addr,
170 uint8_t *buf, int len, bool is_write);
171 void (*dump_state)(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
172 int flags);
173 GuestPanicInformation* (*get_crash_info)(CPUState *cpu);
174 void (*dump_statistics)(CPUState *cpu, FILE *f,
175 fprintf_function cpu_fprintf, int flags);
176 int64_t (*get_arch_id)(CPUState *cpu);
177 bool (*get_paging_enabled)(const CPUState *cpu);
178 void (*get_memory_mapping)(CPUState *cpu, MemoryMappingList *list,
179 Error **errp);
180 void (*set_pc)(CPUState *cpu, vaddr value);
181 void (*synchronize_from_tb)(CPUState *cpu, struct TranslationBlock *tb);
182 int (*handle_mmu_fault)(CPUState *cpu, vaddr address, int size, int rw,
183 int mmu_index);
184 hwaddr (*get_phys_page_debug)(CPUState *cpu, vaddr addr);
185 hwaddr (*get_phys_page_attrs_debug)(CPUState *cpu, vaddr addr,
186 MemTxAttrs *attrs);
187 int (*asidx_from_attrs)(CPUState *cpu, MemTxAttrs attrs);
188 int (*gdb_read_register)(CPUState *cpu, uint8_t *buf, int reg);
189 int (*gdb_write_register)(CPUState *cpu, uint8_t *buf, int reg);
190 bool (*debug_check_watchpoint)(CPUState *cpu, CPUWatchpoint *wp);
191 void (*debug_excp_handler)(CPUState *cpu);
193 int (*write_elf64_note)(WriteCoreDumpFunction f, CPUState *cpu,
194 int cpuid, void *opaque);
195 int (*write_elf64_qemunote)(WriteCoreDumpFunction f, CPUState *cpu,
196 void *opaque);
197 int (*write_elf32_note)(WriteCoreDumpFunction f, CPUState *cpu,
198 int cpuid, void *opaque);
199 int (*write_elf32_qemunote)(WriteCoreDumpFunction f, CPUState *cpu,
200 void *opaque);
202 const struct VMStateDescription *vmsd;
203 const char *gdb_core_xml_file;
204 gchar * (*gdb_arch_name)(CPUState *cpu);
205 const char * (*gdb_get_dynamic_xml)(CPUState *cpu, const char *xmlname);
206 void (*cpu_exec_enter)(CPUState *cpu);
207 void (*cpu_exec_exit)(CPUState *cpu);
208 bool (*cpu_exec_interrupt)(CPUState *cpu, int interrupt_request);
210 void (*disas_set_info)(CPUState *cpu, disassemble_info *info);
211 vaddr (*adjust_watchpoint_address)(CPUState *cpu, vaddr addr, int len);
212 void (*tcg_initialize)(void);
214 /* Keep non-pointer data at the end to minimize holes. */
215 int gdb_num_core_regs;
216 bool gdb_stop_before_watchpoint;
217 } CPUClass;
219 #ifdef HOST_WORDS_BIGENDIAN
220 typedef struct icount_decr_u16 {
221 uint16_t high;
222 uint16_t low;
223 } icount_decr_u16;
224 #else
225 typedef struct icount_decr_u16 {
226 uint16_t low;
227 uint16_t high;
228 } icount_decr_u16;
229 #endif
231 typedef struct CPUBreakpoint {
232 vaddr pc;
233 int flags; /* BP_* */
234 QTAILQ_ENTRY(CPUBreakpoint) entry;
235 } CPUBreakpoint;
237 struct CPUWatchpoint {
238 vaddr vaddr;
239 vaddr len;
240 vaddr hitaddr;
241 MemTxAttrs hitattrs;
242 int flags; /* BP_* */
243 QTAILQ_ENTRY(CPUWatchpoint) entry;
246 struct KVMState;
247 struct kvm_run;
249 struct hax_vcpu_state;
251 #define TB_JMP_CACHE_BITS 12
252 #define TB_JMP_CACHE_SIZE (1 << TB_JMP_CACHE_BITS)
254 /* work queue */
256 /* The union type allows passing of 64 bit target pointers on 32 bit
257 * hosts in a single parameter
259 typedef union {
260 int host_int;
261 unsigned long host_ulong;
262 void *host_ptr;
263 vaddr target_ptr;
264 } run_on_cpu_data;
266 #define RUN_ON_CPU_HOST_PTR(p) ((run_on_cpu_data){.host_ptr = (p)})
267 #define RUN_ON_CPU_HOST_INT(i) ((run_on_cpu_data){.host_int = (i)})
268 #define RUN_ON_CPU_HOST_ULONG(ul) ((run_on_cpu_data){.host_ulong = (ul)})
269 #define RUN_ON_CPU_TARGET_PTR(v) ((run_on_cpu_data){.target_ptr = (v)})
270 #define RUN_ON_CPU_NULL RUN_ON_CPU_HOST_PTR(NULL)
272 typedef void (*run_on_cpu_func)(CPUState *cpu, run_on_cpu_data data);
274 struct qemu_work_item;
276 #define CPU_UNSET_NUMA_NODE_ID -1
277 #define CPU_TRACE_DSTATE_MAX_EVENTS 32
280 * CPUState:
281 * @cpu_index: CPU index (informative).
282 * @nr_cores: Number of cores within this CPU package.
283 * @nr_threads: Number of threads within this CPU.
284 * @running: #true if CPU is currently running (lockless).
285 * @has_waiter: #true if a CPU is currently waiting for the cpu_exec_end;
286 * valid under cpu_list_lock.
287 * @created: Indicates whether the CPU thread has been successfully created.
288 * @interrupt_request: Indicates a pending interrupt request.
289 * @halted: Nonzero if the CPU is in suspended state.
290 * @stop: Indicates a pending stop request.
291 * @stopped: Indicates the CPU has been artificially stopped.
292 * @unplug: Indicates a pending CPU unplug request.
293 * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU
294 * @singlestep_enabled: Flags for single-stepping.
295 * @icount_extra: Instructions until next timer event.
296 * @icount_decr: Low 16 bits: number of cycles left, only used in icount mode.
297 * High 16 bits: Set to -1 to force TCG to stop executing linked TBs for this
298 * CPU and return to its top level loop (even in non-icount mode).
299 * This allows a single read-compare-cbranch-write sequence to test
300 * for both decrementer underflow and exceptions.
301 * @can_do_io: Nonzero if memory-mapped IO is safe. Deterministic execution
302 * requires that IO only be performed on the last instruction of a TB
303 * so that interrupts take effect immediately.
304 * @cpu_ases: Pointer to array of CPUAddressSpaces (which define the
305 * AddressSpaces this CPU has)
306 * @num_ases: number of CPUAddressSpaces in @cpu_ases
307 * @as: Pointer to the first AddressSpace, for the convenience of targets which
308 * only have a single AddressSpace
309 * @env_ptr: Pointer to subclass-specific CPUArchState field.
310 * @gdb_regs: Additional GDB registers.
311 * @gdb_num_regs: Number of total registers accessible to GDB.
312 * @gdb_num_g_regs: Number of registers in GDB 'g' packets.
313 * @next_cpu: Next CPU sharing TB cache.
314 * @opaque: User data.
315 * @mem_io_pc: Host Program Counter at which the memory was accessed.
316 * @mem_io_vaddr: Target virtual address at which the memory was accessed.
317 * @kvm_fd: vCPU file descriptor for KVM.
318 * @work_mutex: Lock to prevent multiple access to queued_work_*.
319 * @queued_work_first: First asynchronous work pending.
320 * @trace_dstate_delayed: Delayed changes to trace_dstate (includes all changes
321 * to @trace_dstate).
322 * @trace_dstate: Dynamic tracing state of events for this vCPU (bitmask).
323 * @ignore_memory_transaction_failures: Cached copy of the MachineState
324 * flag of the same name: allows the board to suppress calling of the
325 * CPU do_transaction_failed hook function.
327 * State of one CPU core or thread.
329 struct CPUState {
330 /*< private >*/
331 DeviceState parent_obj;
332 /*< public >*/
334 int nr_cores;
335 int nr_threads;
337 struct QemuThread *thread;
338 #ifdef _WIN32
339 HANDLE hThread;
340 #endif
341 int thread_id;
342 bool running, has_waiter;
343 struct QemuCond *halt_cond;
344 bool thread_kicked;
345 bool created;
346 bool stop;
347 bool stopped;
348 bool unplug;
349 bool crash_occurred;
350 bool exit_request;
351 uint32_t cflags_next_tb;
352 /* updates protected by BQL */
353 uint32_t interrupt_request;
354 int singlestep_enabled;
355 int64_t icount_budget;
356 int64_t icount_extra;
357 sigjmp_buf jmp_env;
359 QemuMutex work_mutex;
360 struct qemu_work_item *queued_work_first, *queued_work_last;
362 CPUAddressSpace *cpu_ases;
363 int num_ases;
364 AddressSpace *as;
365 MemoryRegion *memory;
367 void *env_ptr; /* CPUArchState */
369 /* Accessed in parallel; all accesses must be atomic */
370 struct TranslationBlock *tb_jmp_cache[TB_JMP_CACHE_SIZE];
372 struct GDBRegisterState *gdb_regs;
373 int gdb_num_regs;
374 int gdb_num_g_regs;
375 QTAILQ_ENTRY(CPUState) node;
377 /* ice debug support */
378 QTAILQ_HEAD(breakpoints_head, CPUBreakpoint) breakpoints;
380 QTAILQ_HEAD(watchpoints_head, CPUWatchpoint) watchpoints;
381 CPUWatchpoint *watchpoint_hit;
383 void *opaque;
385 /* In order to avoid passing too many arguments to the MMIO helpers,
386 * we store some rarely used information in the CPU context.
388 uintptr_t mem_io_pc;
389 vaddr mem_io_vaddr;
391 * This is only needed for the legacy cpu_unassigned_access() hook;
392 * when all targets using it have been converted to use
393 * cpu_transaction_failed() instead it can be removed.
395 MMUAccessType mem_io_access_type;
397 int kvm_fd;
398 struct KVMState *kvm_state;
399 struct kvm_run *kvm_run;
401 /* Used for events with 'vcpu' and *without* the 'disabled' properties */
402 DECLARE_BITMAP(trace_dstate_delayed, CPU_TRACE_DSTATE_MAX_EVENTS);
403 DECLARE_BITMAP(trace_dstate, CPU_TRACE_DSTATE_MAX_EVENTS);
405 /* TODO Move common fields from CPUArchState here. */
406 int cpu_index;
407 uint32_t halted;
408 uint32_t can_do_io;
409 int32_t exception_index;
411 /* shared by kvm, hax and hvf */
412 bool vcpu_dirty;
414 /* Used to keep track of an outstanding cpu throttle thread for migration
415 * autoconverge
417 bool throttle_thread_scheduled;
419 bool ignore_memory_transaction_failures;
421 /* Note that this is accessed at the start of every TB via a negative
422 offset from AREG0. Leave this field at the end so as to make the
423 (absolute value) offset as small as possible. This reduces code
424 size, especially for hosts without large memory offsets. */
425 union {
426 uint32_t u32;
427 icount_decr_u16 u16;
428 } icount_decr;
430 struct hax_vcpu_state *hax_vcpu;
432 /* The pending_tlb_flush flag is set and cleared atomically to
433 * avoid potential races. The aim of the flag is to avoid
434 * unnecessary flushes.
436 uint16_t pending_tlb_flush;
438 int hvf_fd;
440 /* track IOMMUs whose translations we've cached in the TCG TLB */
441 GArray *iommu_notifiers;
444 QTAILQ_HEAD(CPUTailQ, CPUState);
445 extern struct CPUTailQ cpus;
446 #define first_cpu QTAILQ_FIRST_RCU(&cpus)
447 #define CPU_NEXT(cpu) QTAILQ_NEXT_RCU(cpu, node)
448 #define CPU_FOREACH(cpu) QTAILQ_FOREACH_RCU(cpu, &cpus, node)
449 #define CPU_FOREACH_SAFE(cpu, next_cpu) \
450 QTAILQ_FOREACH_SAFE_RCU(cpu, &cpus, node, next_cpu)
452 extern __thread CPUState *current_cpu;
454 static inline void cpu_tb_jmp_cache_clear(CPUState *cpu)
456 unsigned int i;
458 for (i = 0; i < TB_JMP_CACHE_SIZE; i++) {
459 atomic_set(&cpu->tb_jmp_cache[i], NULL);
464 * qemu_tcg_mttcg_enabled:
465 * Check whether we are running MultiThread TCG or not.
467 * Returns: %true if we are in MTTCG mode %false otherwise.
469 extern bool mttcg_enabled;
470 #define qemu_tcg_mttcg_enabled() (mttcg_enabled)
473 * cpu_paging_enabled:
474 * @cpu: The CPU whose state is to be inspected.
476 * Returns: %true if paging is enabled, %false otherwise.
478 bool cpu_paging_enabled(const CPUState *cpu);
481 * cpu_get_memory_mapping:
482 * @cpu: The CPU whose memory mappings are to be obtained.
483 * @list: Where to write the memory mappings to.
484 * @errp: Pointer for reporting an #Error.
486 void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
487 Error **errp);
490 * cpu_write_elf64_note:
491 * @f: pointer to a function that writes memory to a file
492 * @cpu: The CPU whose memory is to be dumped
493 * @cpuid: ID number of the CPU
494 * @opaque: pointer to the CPUState struct
496 int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
497 int cpuid, void *opaque);
500 * cpu_write_elf64_qemunote:
501 * @f: pointer to a function that writes memory to a file
502 * @cpu: The CPU whose memory is to be dumped
503 * @cpuid: ID number of the CPU
504 * @opaque: pointer to the CPUState struct
506 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
507 void *opaque);
510 * cpu_write_elf32_note:
511 * @f: pointer to a function that writes memory to a file
512 * @cpu: The CPU whose memory is to be dumped
513 * @cpuid: ID number of the CPU
514 * @opaque: pointer to the CPUState struct
516 int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
517 int cpuid, void *opaque);
520 * cpu_write_elf32_qemunote:
521 * @f: pointer to a function that writes memory to a file
522 * @cpu: The CPU whose memory is to be dumped
523 * @cpuid: ID number of the CPU
524 * @opaque: pointer to the CPUState struct
526 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
527 void *opaque);
530 * cpu_get_crash_info:
531 * @cpu: The CPU to get crash information for
533 * Gets the previously saved crash information.
534 * Caller is responsible for freeing the data.
536 GuestPanicInformation *cpu_get_crash_info(CPUState *cpu);
539 * CPUDumpFlags:
540 * @CPU_DUMP_CODE:
541 * @CPU_DUMP_FPU: dump FPU register state, not just integer
542 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
544 enum CPUDumpFlags {
545 CPU_DUMP_CODE = 0x00010000,
546 CPU_DUMP_FPU = 0x00020000,
547 CPU_DUMP_CCOP = 0x00040000,
551 * cpu_dump_state:
552 * @cpu: The CPU whose state is to be dumped.
553 * @f: File to dump to.
554 * @cpu_fprintf: Function to dump with.
555 * @flags: Flags what to dump.
557 * Dumps CPU state.
559 void cpu_dump_state(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
560 int flags);
563 * cpu_dump_statistics:
564 * @cpu: The CPU whose state is to be dumped.
565 * @f: File to dump to.
566 * @cpu_fprintf: Function to dump with.
567 * @flags: Flags what to dump.
569 * Dumps CPU statistics.
571 void cpu_dump_statistics(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
572 int flags);
574 #ifndef CONFIG_USER_ONLY
576 * cpu_get_phys_page_attrs_debug:
577 * @cpu: The CPU to obtain the physical page address for.
578 * @addr: The virtual address.
579 * @attrs: Updated on return with the memory transaction attributes to use
580 * for this access.
582 * Obtains the physical page corresponding to a virtual one, together
583 * with the corresponding memory transaction attributes to use for the access.
584 * Use it only for debugging because no protection checks are done.
586 * Returns: Corresponding physical page address or -1 if no page found.
588 static inline hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr,
589 MemTxAttrs *attrs)
591 CPUClass *cc = CPU_GET_CLASS(cpu);
593 if (cc->get_phys_page_attrs_debug) {
594 return cc->get_phys_page_attrs_debug(cpu, addr, attrs);
596 /* Fallback for CPUs which don't implement the _attrs_ hook */
597 *attrs = MEMTXATTRS_UNSPECIFIED;
598 return cc->get_phys_page_debug(cpu, addr);
602 * cpu_get_phys_page_debug:
603 * @cpu: The CPU to obtain the physical page address for.
604 * @addr: The virtual address.
606 * Obtains the physical page corresponding to a virtual one.
607 * Use it only for debugging because no protection checks are done.
609 * Returns: Corresponding physical page address or -1 if no page found.
611 static inline hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr)
613 MemTxAttrs attrs = {};
615 return cpu_get_phys_page_attrs_debug(cpu, addr, &attrs);
618 /** cpu_asidx_from_attrs:
619 * @cpu: CPU
620 * @attrs: memory transaction attributes
622 * Returns the address space index specifying the CPU AddressSpace
623 * to use for a memory access with the given transaction attributes.
625 static inline int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs)
627 CPUClass *cc = CPU_GET_CLASS(cpu);
628 int ret = 0;
630 if (cc->asidx_from_attrs) {
631 ret = cc->asidx_from_attrs(cpu, attrs);
632 assert(ret < cpu->num_ases && ret >= 0);
634 return ret;
636 #endif
639 * cpu_list_add:
640 * @cpu: The CPU to be added to the list of CPUs.
642 void cpu_list_add(CPUState *cpu);
645 * cpu_list_remove:
646 * @cpu: The CPU to be removed from the list of CPUs.
648 void cpu_list_remove(CPUState *cpu);
651 * cpu_reset:
652 * @cpu: The CPU whose state is to be reset.
654 void cpu_reset(CPUState *cpu);
657 * cpu_class_by_name:
658 * @typename: The CPU base type.
659 * @cpu_model: The model string without any parameters.
661 * Looks up a CPU #ObjectClass matching name @cpu_model.
663 * Returns: A #CPUClass or %NULL if not matching class is found.
665 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
668 * cpu_create:
669 * @typename: The CPU type.
671 * Instantiates a CPU and realizes the CPU.
673 * Returns: A #CPUState or %NULL if an error occurred.
675 CPUState *cpu_create(const char *typename);
678 * parse_cpu_model:
679 * @cpu_model: The model string including optional parameters.
681 * processes optional parameters and registers them as global properties
683 * Returns: type of CPU to create or prints error and terminates process
684 * if an error occurred.
686 const char *parse_cpu_model(const char *cpu_model);
689 * cpu_has_work:
690 * @cpu: The vCPU to check.
692 * Checks whether the CPU has work to do.
694 * Returns: %true if the CPU has work, %false otherwise.
696 static inline bool cpu_has_work(CPUState *cpu)
698 CPUClass *cc = CPU_GET_CLASS(cpu);
700 g_assert(cc->has_work);
701 return cc->has_work(cpu);
705 * qemu_cpu_is_self:
706 * @cpu: The vCPU to check against.
708 * Checks whether the caller is executing on the vCPU thread.
710 * Returns: %true if called from @cpu's thread, %false otherwise.
712 bool qemu_cpu_is_self(CPUState *cpu);
715 * qemu_cpu_kick:
716 * @cpu: The vCPU to kick.
718 * Kicks @cpu's thread.
720 void qemu_cpu_kick(CPUState *cpu);
723 * cpu_is_stopped:
724 * @cpu: The CPU to check.
726 * Checks whether the CPU is stopped.
728 * Returns: %true if run state is not running or if artificially stopped;
729 * %false otherwise.
731 bool cpu_is_stopped(CPUState *cpu);
734 * do_run_on_cpu:
735 * @cpu: The vCPU to run on.
736 * @func: The function to be executed.
737 * @data: Data to pass to the function.
738 * @mutex: Mutex to release while waiting for @func to run.
740 * Used internally in the implementation of run_on_cpu.
742 void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data,
743 QemuMutex *mutex);
746 * run_on_cpu:
747 * @cpu: The vCPU to run on.
748 * @func: The function to be executed.
749 * @data: Data to pass to the function.
751 * Schedules the function @func for execution on the vCPU @cpu.
753 void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
756 * async_run_on_cpu:
757 * @cpu: The vCPU to run on.
758 * @func: The function to be executed.
759 * @data: Data to pass to the function.
761 * Schedules the function @func for execution on the vCPU @cpu asynchronously.
763 void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
766 * async_safe_run_on_cpu:
767 * @cpu: The vCPU to run on.
768 * @func: The function to be executed.
769 * @data: Data to pass to the function.
771 * Schedules the function @func for execution on the vCPU @cpu asynchronously,
772 * while all other vCPUs are sleeping.
774 * Unlike run_on_cpu and async_run_on_cpu, the function is run outside the
775 * BQL.
777 void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
780 * qemu_get_cpu:
781 * @index: The CPUState@cpu_index value of the CPU to obtain.
783 * Gets a CPU matching @index.
785 * Returns: The CPU or %NULL if there is no matching CPU.
787 CPUState *qemu_get_cpu(int index);
790 * cpu_exists:
791 * @id: Guest-exposed CPU ID to lookup.
793 * Search for CPU with specified ID.
795 * Returns: %true - CPU is found, %false - CPU isn't found.
797 bool cpu_exists(int64_t id);
800 * cpu_by_arch_id:
801 * @id: Guest-exposed CPU ID of the CPU to obtain.
803 * Get a CPU with matching @id.
805 * Returns: The CPU or %NULL if there is no matching CPU.
807 CPUState *cpu_by_arch_id(int64_t id);
810 * cpu_throttle_set:
811 * @new_throttle_pct: Percent of sleep time. Valid range is 1 to 99.
813 * Throttles all vcpus by forcing them to sleep for the given percentage of
814 * time. A throttle_percentage of 25 corresponds to a 75% duty cycle roughly.
815 * (example: 10ms sleep for every 30ms awake).
817 * cpu_throttle_set can be called as needed to adjust new_throttle_pct.
818 * Once the throttling starts, it will remain in effect until cpu_throttle_stop
819 * is called.
821 void cpu_throttle_set(int new_throttle_pct);
824 * cpu_throttle_stop:
826 * Stops the vcpu throttling started by cpu_throttle_set.
828 void cpu_throttle_stop(void);
831 * cpu_throttle_active:
833 * Returns: %true if the vcpus are currently being throttled, %false otherwise.
835 bool cpu_throttle_active(void);
838 * cpu_throttle_get_percentage:
840 * Returns the vcpu throttle percentage. See cpu_throttle_set for details.
842 * Returns: The throttle percentage in range 1 to 99.
844 int cpu_throttle_get_percentage(void);
846 #ifndef CONFIG_USER_ONLY
848 typedef void (*CPUInterruptHandler)(CPUState *, int);
850 extern CPUInterruptHandler cpu_interrupt_handler;
853 * cpu_interrupt:
854 * @cpu: The CPU to set an interrupt on.
855 * @mask: The interupts to set.
857 * Invokes the interrupt handler.
859 static inline void cpu_interrupt(CPUState *cpu, int mask)
861 cpu_interrupt_handler(cpu, mask);
864 #else /* USER_ONLY */
866 void cpu_interrupt(CPUState *cpu, int mask);
868 #endif /* USER_ONLY */
870 #ifdef NEED_CPU_H
872 #ifdef CONFIG_SOFTMMU
873 static inline void cpu_unassigned_access(CPUState *cpu, hwaddr addr,
874 bool is_write, bool is_exec,
875 int opaque, unsigned size)
877 CPUClass *cc = CPU_GET_CLASS(cpu);
879 if (cc->do_unassigned_access) {
880 cc->do_unassigned_access(cpu, addr, is_write, is_exec, opaque, size);
884 static inline void cpu_unaligned_access(CPUState *cpu, vaddr addr,
885 MMUAccessType access_type,
886 int mmu_idx, uintptr_t retaddr)
888 CPUClass *cc = CPU_GET_CLASS(cpu);
890 cc->do_unaligned_access(cpu, addr, access_type, mmu_idx, retaddr);
893 static inline void cpu_transaction_failed(CPUState *cpu, hwaddr physaddr,
894 vaddr addr, unsigned size,
895 MMUAccessType access_type,
896 int mmu_idx, MemTxAttrs attrs,
897 MemTxResult response,
898 uintptr_t retaddr)
900 CPUClass *cc = CPU_GET_CLASS(cpu);
902 if (!cpu->ignore_memory_transaction_failures && cc->do_transaction_failed) {
903 cc->do_transaction_failed(cpu, physaddr, addr, size, access_type,
904 mmu_idx, attrs, response, retaddr);
907 #endif
909 #endif /* NEED_CPU_H */
912 * cpu_set_pc:
913 * @cpu: The CPU to set the program counter for.
914 * @addr: Program counter value.
916 * Sets the program counter for a CPU.
918 static inline void cpu_set_pc(CPUState *cpu, vaddr addr)
920 CPUClass *cc = CPU_GET_CLASS(cpu);
922 cc->set_pc(cpu, addr);
926 * cpu_reset_interrupt:
927 * @cpu: The CPU to clear the interrupt on.
928 * @mask: The interrupt mask to clear.
930 * Resets interrupts on the vCPU @cpu.
932 void cpu_reset_interrupt(CPUState *cpu, int mask);
935 * cpu_exit:
936 * @cpu: The CPU to exit.
938 * Requests the CPU @cpu to exit execution.
940 void cpu_exit(CPUState *cpu);
943 * cpu_resume:
944 * @cpu: The CPU to resume.
946 * Resumes CPU, i.e. puts CPU into runnable state.
948 void cpu_resume(CPUState *cpu);
951 * cpu_remove:
952 * @cpu: The CPU to remove.
954 * Requests the CPU to be removed.
956 void cpu_remove(CPUState *cpu);
959 * cpu_remove_sync:
960 * @cpu: The CPU to remove.
962 * Requests the CPU to be removed and waits till it is removed.
964 void cpu_remove_sync(CPUState *cpu);
967 * process_queued_cpu_work() - process all items on CPU work queue
968 * @cpu: The CPU which work queue to process.
970 void process_queued_cpu_work(CPUState *cpu);
973 * cpu_exec_start:
974 * @cpu: The CPU for the current thread.
976 * Record that a CPU has started execution and can be interrupted with
977 * cpu_exit.
979 void cpu_exec_start(CPUState *cpu);
982 * cpu_exec_end:
983 * @cpu: The CPU for the current thread.
985 * Record that a CPU has stopped execution and exclusive sections
986 * can be executed without interrupting it.
988 void cpu_exec_end(CPUState *cpu);
991 * start_exclusive:
993 * Wait for a concurrent exclusive section to end, and then start
994 * a section of work that is run while other CPUs are not running
995 * between cpu_exec_start and cpu_exec_end. CPUs that are running
996 * cpu_exec are exited immediately. CPUs that call cpu_exec_start
997 * during the exclusive section go to sleep until this CPU calls
998 * end_exclusive.
1000 void start_exclusive(void);
1003 * end_exclusive:
1005 * Concludes an exclusive execution section started by start_exclusive.
1007 void end_exclusive(void);
1010 * qemu_init_vcpu:
1011 * @cpu: The vCPU to initialize.
1013 * Initializes a vCPU.
1015 void qemu_init_vcpu(CPUState *cpu);
1017 #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
1018 #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
1019 #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
1022 * cpu_single_step:
1023 * @cpu: CPU to the flags for.
1024 * @enabled: Flags to enable.
1026 * Enables or disables single-stepping for @cpu.
1028 void cpu_single_step(CPUState *cpu, int enabled);
1030 /* Breakpoint/watchpoint flags */
1031 #define BP_MEM_READ 0x01
1032 #define BP_MEM_WRITE 0x02
1033 #define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE)
1034 #define BP_STOP_BEFORE_ACCESS 0x04
1035 /* 0x08 currently unused */
1036 #define BP_GDB 0x10
1037 #define BP_CPU 0x20
1038 #define BP_ANY (BP_GDB | BP_CPU)
1039 #define BP_WATCHPOINT_HIT_READ 0x40
1040 #define BP_WATCHPOINT_HIT_WRITE 0x80
1041 #define BP_WATCHPOINT_HIT (BP_WATCHPOINT_HIT_READ | BP_WATCHPOINT_HIT_WRITE)
1043 int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
1044 CPUBreakpoint **breakpoint);
1045 int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags);
1046 void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint);
1047 void cpu_breakpoint_remove_all(CPUState *cpu, int mask);
1049 /* Return true if PC matches an installed breakpoint. */
1050 static inline bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask)
1052 CPUBreakpoint *bp;
1054 if (unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) {
1055 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
1056 if (bp->pc == pc && (bp->flags & mask)) {
1057 return true;
1061 return false;
1064 int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
1065 int flags, CPUWatchpoint **watchpoint);
1066 int cpu_watchpoint_remove(CPUState *cpu, vaddr addr,
1067 vaddr len, int flags);
1068 void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint);
1069 void cpu_watchpoint_remove_all(CPUState *cpu, int mask);
1072 * cpu_get_address_space:
1073 * @cpu: CPU to get address space from
1074 * @asidx: index identifying which address space to get
1076 * Return the requested address space of this CPU. @asidx
1077 * specifies which address space to read.
1079 AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx);
1081 void QEMU_NORETURN cpu_abort(CPUState *cpu, const char *fmt, ...)
1082 GCC_FMT_ATTR(2, 3);
1083 extern Property cpu_common_props[];
1084 void cpu_exec_initfn(CPUState *cpu);
1085 void cpu_exec_realizefn(CPUState *cpu, Error **errp);
1086 void cpu_exec_unrealizefn(CPUState *cpu);
1088 #ifdef NEED_CPU_H
1090 #ifdef CONFIG_SOFTMMU
1091 extern const struct VMStateDescription vmstate_cpu_common;
1092 #else
1093 #define vmstate_cpu_common vmstate_dummy
1094 #endif
1096 #define VMSTATE_CPU() { \
1097 .name = "parent_obj", \
1098 .size = sizeof(CPUState), \
1099 .vmsd = &vmstate_cpu_common, \
1100 .flags = VMS_STRUCT, \
1101 .offset = 0, \
1104 #endif /* NEED_CPU_H */
1106 #define UNASSIGNED_CPU_INDEX -1
1108 #endif