pc-dimm: move actual plug/unplug of a memory region to MemoryDevice
[qemu/ar7.git] / include / qom / cpu.h
blob14e45c4282e53606fbba1234283bd38cfcbc33ff
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 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;
428 int hvf_fd;
431 QTAILQ_HEAD(CPUTailQ, CPUState);
432 extern struct CPUTailQ cpus;
433 #define CPU_NEXT(cpu) QTAILQ_NEXT(cpu, node)
434 #define CPU_FOREACH(cpu) QTAILQ_FOREACH(cpu, &cpus, node)
435 #define CPU_FOREACH_SAFE(cpu, next_cpu) \
436 QTAILQ_FOREACH_SAFE(cpu, &cpus, node, next_cpu)
437 #define CPU_FOREACH_REVERSE(cpu) \
438 QTAILQ_FOREACH_REVERSE(cpu, &cpus, CPUTailQ, node)
439 #define first_cpu QTAILQ_FIRST(&cpus)
441 extern __thread CPUState *current_cpu;
443 static inline void cpu_tb_jmp_cache_clear(CPUState *cpu)
445 unsigned int i;
447 for (i = 0; i < TB_JMP_CACHE_SIZE; i++) {
448 atomic_set(&cpu->tb_jmp_cache[i], NULL);
453 * qemu_tcg_mttcg_enabled:
454 * Check whether we are running MultiThread TCG or not.
456 * Returns: %true if we are in MTTCG mode %false otherwise.
458 extern bool mttcg_enabled;
459 #define qemu_tcg_mttcg_enabled() (mttcg_enabled)
462 * cpu_paging_enabled:
463 * @cpu: The CPU whose state is to be inspected.
465 * Returns: %true if paging is enabled, %false otherwise.
467 bool cpu_paging_enabled(const CPUState *cpu);
470 * cpu_get_memory_mapping:
471 * @cpu: The CPU whose memory mappings are to be obtained.
472 * @list: Where to write the memory mappings to.
473 * @errp: Pointer for reporting an #Error.
475 void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
476 Error **errp);
479 * cpu_write_elf64_note:
480 * @f: pointer to a function that writes memory to a file
481 * @cpu: The CPU whose memory is to be dumped
482 * @cpuid: ID number of the CPU
483 * @opaque: pointer to the CPUState struct
485 int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
486 int cpuid, void *opaque);
489 * cpu_write_elf64_qemunote:
490 * @f: pointer to a function that writes memory to a file
491 * @cpu: The CPU whose memory is to be dumped
492 * @cpuid: ID number of the CPU
493 * @opaque: pointer to the CPUState struct
495 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
496 void *opaque);
499 * cpu_write_elf32_note:
500 * @f: pointer to a function that writes memory to a file
501 * @cpu: The CPU whose memory is to be dumped
502 * @cpuid: ID number of the CPU
503 * @opaque: pointer to the CPUState struct
505 int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
506 int cpuid, void *opaque);
509 * cpu_write_elf32_qemunote:
510 * @f: pointer to a function that writes memory to a file
511 * @cpu: The CPU whose memory is to be dumped
512 * @cpuid: ID number of the CPU
513 * @opaque: pointer to the CPUState struct
515 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
516 void *opaque);
519 * cpu_get_crash_info:
520 * @cpu: The CPU to get crash information for
522 * Gets the previously saved crash information.
523 * Caller is responsible for freeing the data.
525 GuestPanicInformation *cpu_get_crash_info(CPUState *cpu);
528 * CPUDumpFlags:
529 * @CPU_DUMP_CODE:
530 * @CPU_DUMP_FPU: dump FPU register state, not just integer
531 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
533 enum CPUDumpFlags {
534 CPU_DUMP_CODE = 0x00010000,
535 CPU_DUMP_FPU = 0x00020000,
536 CPU_DUMP_CCOP = 0x00040000,
540 * cpu_dump_state:
541 * @cpu: The CPU whose state is to be dumped.
542 * @f: File to dump to.
543 * @cpu_fprintf: Function to dump with.
544 * @flags: Flags what to dump.
546 * Dumps CPU state.
548 void cpu_dump_state(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
549 int flags);
552 * cpu_dump_statistics:
553 * @cpu: The CPU whose state is to be dumped.
554 * @f: File to dump to.
555 * @cpu_fprintf: Function to dump with.
556 * @flags: Flags what to dump.
558 * Dumps CPU statistics.
560 void cpu_dump_statistics(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
561 int flags);
563 #ifndef CONFIG_USER_ONLY
565 * cpu_get_phys_page_attrs_debug:
566 * @cpu: The CPU to obtain the physical page address for.
567 * @addr: The virtual address.
568 * @attrs: Updated on return with the memory transaction attributes to use
569 * for this access.
571 * Obtains the physical page corresponding to a virtual one, together
572 * with the corresponding memory transaction attributes to use for the access.
573 * Use it only for debugging because no protection checks are done.
575 * Returns: Corresponding physical page address or -1 if no page found.
577 static inline hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr,
578 MemTxAttrs *attrs)
580 CPUClass *cc = CPU_GET_CLASS(cpu);
582 if (cc->get_phys_page_attrs_debug) {
583 return cc->get_phys_page_attrs_debug(cpu, addr, attrs);
585 /* Fallback for CPUs which don't implement the _attrs_ hook */
586 *attrs = MEMTXATTRS_UNSPECIFIED;
587 return cc->get_phys_page_debug(cpu, addr);
591 * cpu_get_phys_page_debug:
592 * @cpu: The CPU to obtain the physical page address for.
593 * @addr: The virtual address.
595 * Obtains the physical page corresponding to a virtual one.
596 * Use it only for debugging because no protection checks are done.
598 * Returns: Corresponding physical page address or -1 if no page found.
600 static inline hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr)
602 MemTxAttrs attrs = {};
604 return cpu_get_phys_page_attrs_debug(cpu, addr, &attrs);
607 /** cpu_asidx_from_attrs:
608 * @cpu: CPU
609 * @attrs: memory transaction attributes
611 * Returns the address space index specifying the CPU AddressSpace
612 * to use for a memory access with the given transaction attributes.
614 static inline int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs)
616 CPUClass *cc = CPU_GET_CLASS(cpu);
618 if (cc->asidx_from_attrs) {
619 return cc->asidx_from_attrs(cpu, attrs);
621 return 0;
623 #endif
626 * cpu_list_add:
627 * @cpu: The CPU to be added to the list of CPUs.
629 void cpu_list_add(CPUState *cpu);
632 * cpu_list_remove:
633 * @cpu: The CPU to be removed from the list of CPUs.
635 void cpu_list_remove(CPUState *cpu);
638 * cpu_reset:
639 * @cpu: The CPU whose state is to be reset.
641 void cpu_reset(CPUState *cpu);
644 * cpu_class_by_name:
645 * @typename: The CPU base type.
646 * @cpu_model: The model string without any parameters.
648 * Looks up a CPU #ObjectClass matching name @cpu_model.
650 * Returns: A #CPUClass or %NULL if not matching class is found.
652 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
655 * cpu_create:
656 * @typename: The CPU type.
658 * Instantiates a CPU and realizes the CPU.
660 * Returns: A #CPUState or %NULL if an error occurred.
662 CPUState *cpu_create(const char *typename);
665 * parse_cpu_model:
666 * @cpu_model: The model string including optional parameters.
668 * processes optional parameters and registers them as global properties
670 * Returns: type of CPU to create or prints error and terminates process
671 * if an error occurred.
673 const char *parse_cpu_model(const char *cpu_model);
676 * cpu_has_work:
677 * @cpu: The vCPU to check.
679 * Checks whether the CPU has work to do.
681 * Returns: %true if the CPU has work, %false otherwise.
683 static inline bool cpu_has_work(CPUState *cpu)
685 CPUClass *cc = CPU_GET_CLASS(cpu);
687 g_assert(cc->has_work);
688 return cc->has_work(cpu);
692 * qemu_cpu_is_self:
693 * @cpu: The vCPU to check against.
695 * Checks whether the caller is executing on the vCPU thread.
697 * Returns: %true if called from @cpu's thread, %false otherwise.
699 bool qemu_cpu_is_self(CPUState *cpu);
702 * qemu_cpu_kick:
703 * @cpu: The vCPU to kick.
705 * Kicks @cpu's thread.
707 void qemu_cpu_kick(CPUState *cpu);
710 * cpu_is_stopped:
711 * @cpu: The CPU to check.
713 * Checks whether the CPU is stopped.
715 * Returns: %true if run state is not running or if artificially stopped;
716 * %false otherwise.
718 bool cpu_is_stopped(CPUState *cpu);
721 * do_run_on_cpu:
722 * @cpu: The vCPU to run on.
723 * @func: The function to be executed.
724 * @data: Data to pass to the function.
725 * @mutex: Mutex to release while waiting for @func to run.
727 * Used internally in the implementation of run_on_cpu.
729 void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data,
730 QemuMutex *mutex);
733 * run_on_cpu:
734 * @cpu: The vCPU to run on.
735 * @func: The function to be executed.
736 * @data: Data to pass to the function.
738 * Schedules the function @func for execution on the vCPU @cpu.
740 void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
743 * async_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 asynchronously.
750 void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
753 * async_safe_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,
759 * while all other vCPUs are sleeping.
761 * Unlike run_on_cpu and async_run_on_cpu, the function is run outside the
762 * BQL.
764 void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
767 * qemu_get_cpu:
768 * @index: The CPUState@cpu_index value of the CPU to obtain.
770 * Gets a CPU matching @index.
772 * Returns: The CPU or %NULL if there is no matching CPU.
774 CPUState *qemu_get_cpu(int index);
777 * cpu_exists:
778 * @id: Guest-exposed CPU ID to lookup.
780 * Search for CPU with specified ID.
782 * Returns: %true - CPU is found, %false - CPU isn't found.
784 bool cpu_exists(int64_t id);
787 * cpu_by_arch_id:
788 * @id: Guest-exposed CPU ID of the CPU to obtain.
790 * Get a CPU with matching @id.
792 * Returns: The CPU or %NULL if there is no matching CPU.
794 CPUState *cpu_by_arch_id(int64_t id);
797 * cpu_throttle_set:
798 * @new_throttle_pct: Percent of sleep time. Valid range is 1 to 99.
800 * Throttles all vcpus by forcing them to sleep for the given percentage of
801 * time. A throttle_percentage of 25 corresponds to a 75% duty cycle roughly.
802 * (example: 10ms sleep for every 30ms awake).
804 * cpu_throttle_set can be called as needed to adjust new_throttle_pct.
805 * Once the throttling starts, it will remain in effect until cpu_throttle_stop
806 * is called.
808 void cpu_throttle_set(int new_throttle_pct);
811 * cpu_throttle_stop:
813 * Stops the vcpu throttling started by cpu_throttle_set.
815 void cpu_throttle_stop(void);
818 * cpu_throttle_active:
820 * Returns: %true if the vcpus are currently being throttled, %false otherwise.
822 bool cpu_throttle_active(void);
825 * cpu_throttle_get_percentage:
827 * Returns the vcpu throttle percentage. See cpu_throttle_set for details.
829 * Returns: The throttle percentage in range 1 to 99.
831 int cpu_throttle_get_percentage(void);
833 #ifndef CONFIG_USER_ONLY
835 typedef void (*CPUInterruptHandler)(CPUState *, int);
837 extern CPUInterruptHandler cpu_interrupt_handler;
840 * cpu_interrupt:
841 * @cpu: The CPU to set an interrupt on.
842 * @mask: The interupts to set.
844 * Invokes the interrupt handler.
846 static inline void cpu_interrupt(CPUState *cpu, int mask)
848 cpu_interrupt_handler(cpu, mask);
851 #else /* USER_ONLY */
853 void cpu_interrupt(CPUState *cpu, int mask);
855 #endif /* USER_ONLY */
857 #ifdef NEED_CPU_H
859 #ifdef CONFIG_SOFTMMU
860 static inline void cpu_unassigned_access(CPUState *cpu, hwaddr addr,
861 bool is_write, bool is_exec,
862 int opaque, unsigned size)
864 CPUClass *cc = CPU_GET_CLASS(cpu);
866 if (cc->do_unassigned_access) {
867 cc->do_unassigned_access(cpu, addr, is_write, is_exec, opaque, size);
871 static inline void cpu_unaligned_access(CPUState *cpu, vaddr addr,
872 MMUAccessType access_type,
873 int mmu_idx, uintptr_t retaddr)
875 CPUClass *cc = CPU_GET_CLASS(cpu);
877 cc->do_unaligned_access(cpu, addr, access_type, mmu_idx, retaddr);
880 static inline void cpu_transaction_failed(CPUState *cpu, hwaddr physaddr,
881 vaddr addr, unsigned size,
882 MMUAccessType access_type,
883 int mmu_idx, MemTxAttrs attrs,
884 MemTxResult response,
885 uintptr_t retaddr)
887 CPUClass *cc = CPU_GET_CLASS(cpu);
889 if (!cpu->ignore_memory_transaction_failures && cc->do_transaction_failed) {
890 cc->do_transaction_failed(cpu, physaddr, addr, size, access_type,
891 mmu_idx, attrs, response, retaddr);
894 #endif
896 #endif /* NEED_CPU_H */
899 * cpu_set_pc:
900 * @cpu: The CPU to set the program counter for.
901 * @addr: Program counter value.
903 * Sets the program counter for a CPU.
905 static inline void cpu_set_pc(CPUState *cpu, vaddr addr)
907 CPUClass *cc = CPU_GET_CLASS(cpu);
909 cc->set_pc(cpu, addr);
913 * cpu_reset_interrupt:
914 * @cpu: The CPU to clear the interrupt on.
915 * @mask: The interrupt mask to clear.
917 * Resets interrupts on the vCPU @cpu.
919 void cpu_reset_interrupt(CPUState *cpu, int mask);
922 * cpu_exit:
923 * @cpu: The CPU to exit.
925 * Requests the CPU @cpu to exit execution.
927 void cpu_exit(CPUState *cpu);
930 * cpu_resume:
931 * @cpu: The CPU to resume.
933 * Resumes CPU, i.e. puts CPU into runnable state.
935 void cpu_resume(CPUState *cpu);
938 * cpu_remove:
939 * @cpu: The CPU to remove.
941 * Requests the CPU to be removed.
943 void cpu_remove(CPUState *cpu);
946 * cpu_remove_sync:
947 * @cpu: The CPU to remove.
949 * Requests the CPU to be removed and waits till it is removed.
951 void cpu_remove_sync(CPUState *cpu);
954 * process_queued_cpu_work() - process all items on CPU work queue
955 * @cpu: The CPU which work queue to process.
957 void process_queued_cpu_work(CPUState *cpu);
960 * cpu_exec_start:
961 * @cpu: The CPU for the current thread.
963 * Record that a CPU has started execution and can be interrupted with
964 * cpu_exit.
966 void cpu_exec_start(CPUState *cpu);
969 * cpu_exec_end:
970 * @cpu: The CPU for the current thread.
972 * Record that a CPU has stopped execution and exclusive sections
973 * can be executed without interrupting it.
975 void cpu_exec_end(CPUState *cpu);
978 * start_exclusive:
980 * Wait for a concurrent exclusive section to end, and then start
981 * a section of work that is run while other CPUs are not running
982 * between cpu_exec_start and cpu_exec_end. CPUs that are running
983 * cpu_exec are exited immediately. CPUs that call cpu_exec_start
984 * during the exclusive section go to sleep until this CPU calls
985 * end_exclusive.
987 void start_exclusive(void);
990 * end_exclusive:
992 * Concludes an exclusive execution section started by start_exclusive.
994 void end_exclusive(void);
997 * qemu_init_vcpu:
998 * @cpu: The vCPU to initialize.
1000 * Initializes a vCPU.
1002 void qemu_init_vcpu(CPUState *cpu);
1004 #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
1005 #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
1006 #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
1009 * cpu_single_step:
1010 * @cpu: CPU to the flags for.
1011 * @enabled: Flags to enable.
1013 * Enables or disables single-stepping for @cpu.
1015 void cpu_single_step(CPUState *cpu, int enabled);
1017 /* Breakpoint/watchpoint flags */
1018 #define BP_MEM_READ 0x01
1019 #define BP_MEM_WRITE 0x02
1020 #define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE)
1021 #define BP_STOP_BEFORE_ACCESS 0x04
1022 /* 0x08 currently unused */
1023 #define BP_GDB 0x10
1024 #define BP_CPU 0x20
1025 #define BP_ANY (BP_GDB | BP_CPU)
1026 #define BP_WATCHPOINT_HIT_READ 0x40
1027 #define BP_WATCHPOINT_HIT_WRITE 0x80
1028 #define BP_WATCHPOINT_HIT (BP_WATCHPOINT_HIT_READ | BP_WATCHPOINT_HIT_WRITE)
1030 int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
1031 CPUBreakpoint **breakpoint);
1032 int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags);
1033 void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint);
1034 void cpu_breakpoint_remove_all(CPUState *cpu, int mask);
1036 /* Return true if PC matches an installed breakpoint. */
1037 static inline bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask)
1039 CPUBreakpoint *bp;
1041 if (unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) {
1042 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
1043 if (bp->pc == pc && (bp->flags & mask)) {
1044 return true;
1048 return false;
1051 int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
1052 int flags, CPUWatchpoint **watchpoint);
1053 int cpu_watchpoint_remove(CPUState *cpu, vaddr addr,
1054 vaddr len, int flags);
1055 void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint);
1056 void cpu_watchpoint_remove_all(CPUState *cpu, int mask);
1059 * cpu_get_address_space:
1060 * @cpu: CPU to get address space from
1061 * @asidx: index identifying which address space to get
1063 * Return the requested address space of this CPU. @asidx
1064 * specifies which address space to read.
1066 AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx);
1068 void QEMU_NORETURN cpu_abort(CPUState *cpu, const char *fmt, ...)
1069 GCC_FMT_ATTR(2, 3);
1070 extern Property cpu_common_props[];
1071 void cpu_exec_initfn(CPUState *cpu);
1072 void cpu_exec_realizefn(CPUState *cpu, Error **errp);
1073 void cpu_exec_unrealizefn(CPUState *cpu);
1075 #ifdef NEED_CPU_H
1077 #ifdef CONFIG_SOFTMMU
1078 extern const struct VMStateDescription vmstate_cpu_common;
1079 #else
1080 #define vmstate_cpu_common vmstate_dummy
1081 #endif
1083 #define VMSTATE_CPU() { \
1084 .name = "parent_obj", \
1085 .size = sizeof(CPUState), \
1086 .vmsd = &vmstate_cpu_common, \
1087 .flags = VMS_STRUCT, \
1088 .offset = 0, \
1091 #endif /* NEED_CPU_H */
1093 #define UNASSIGNED_CPU_INDEX -1
1095 #endif