]> git.ipfire.org Git - thirdparty/qemu.git/blame - include/qom/cpu.h
Include less of the generated modular QAPI headers
[thirdparty/qemu.git] / include / qom / cpu.h
CommitLineData
dd83b06a
AF
1/*
2 * QEMU CPU model
3 *
4 * Copyright (c) 2012 SUSE LINUX Products GmbH
5 *
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.
10 *
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.
15 *
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>
19 */
20#ifndef QEMU_CPU_H
21#define QEMU_CPU_H
22
961f8395 23#include "hw/qdev-core.h"
37b9de46 24#include "disas/bfd.h"
c658b94f 25#include "exec/hwaddr.h"
66b9b43c 26#include "exec/memattrs.h"
9af23989 27#include "qapi/qapi-types-run-state.h"
48151859 28#include "qemu/bitmap.h"
bdc44640 29#include "qemu/queue.h"
1de7afc9 30#include "qemu/thread.h"
dd83b06a 31
b5ba1cc6
QN
32typedef int (*WriteCoreDumpFunction)(const void *buf, size_t size,
33 void *opaque);
c72bf468 34
577f42c0
AF
35/**
36 * vaddr:
37 * Type wide enough to contain any #target_ulong virtual address.
38 */
39typedef 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
46
dd83b06a
AF
47/**
48 * SECTION:cpu
49 * @section_id: QEMU-cpu
50 * @title: CPU Class
51 * @short_description: Base class for all CPUs
52 */
53
54#define TYPE_CPU "cpu"
55
0d6d1ab4
AF
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.
59 */
60#define CPU(obj) ((CPUState *)(obj))
61
dd83b06a
AF
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)
64
b35399bb
SS
65typedef enum MMUAccessType {
66 MMU_DATA_LOAD = 0,
67 MMU_DATA_STORE = 1,
68 MMU_INST_FETCH = 2
69} MMUAccessType;
70
568496c0 71typedef struct CPUWatchpoint CPUWatchpoint;
dd83b06a 72
c658b94f
AF
73typedef void (*CPUUnassignedAccess)(CPUState *cpu, hwaddr addr,
74 bool is_write, bool is_exec, int opaque,
75 unsigned size);
76
bdf7ae5b
AF
77struct TranslationBlock;
78
dd83b06a
AF
79/**
80 * CPUClass:
2b8c2754
AF
81 * @class_by_name: Callback to map -cpu command line model name to an
82 * instantiatable CPU type.
94a444b2 83 * @parse_features: Callback to parse command line arguments.
f5df5baf 84 * @reset: Callback to reset the #CPUState to its initial state.
91b1df8c 85 * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
8c2e1b00 86 * @has_work: Callback for checking if there is work to do.
97a8ea5a 87 * @do_interrupt: Callback for interrupt handling.
c658b94f 88 * @do_unassigned_access: Callback for unassigned access handling.
0dff0939 89 * (this is deprecated: new targets should use do_transaction_failed instead)
93e22326
PB
90 * @do_unaligned_access: Callback for unaligned access handling, if
91 * the target defines #ALIGNED_ONLY.
0dff0939
PM
92 * @do_transaction_failed: Callback for handling failed memory transactions
93 * (ie bus faults or external aborts; not MMU faults)
c08295d4
PM
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.
f3659eee 98 * @memory_rw_debug: Callback for GDB memory access.
878096ee
AF
99 * @dump_state: Callback for dumping state.
100 * @dump_statistics: Callback for dumping statistics.
997395d3 101 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
444d5590 102 * @get_paging_enabled: Callback for inquiring whether paging is enabled.
a23bbfda 103 * @get_memory_mapping: Callback for obtaining the memory mappings.
f45748f1 104 * @set_pc: Callback for setting the Program Counter register.
bdf7ae5b
AF
105 * @synchronize_from_tb: Callback for synchronizing state from a TCG
106 * #TranslationBlock.
7510454e 107 * @handle_mmu_fault: Callback for handling an MMU fault.
00b941e5 108 * @get_phys_page_debug: Callback for obtaining a physical address.
1dc6fb1f
PM
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.
d7f25a9e
PM
113 * @asidx_from_attrs: Callback to return the CPU AddressSpace to use for
114 * a memory access with the specified memory transaction attributes.
5b50e790
AF
115 * @gdb_read_register: Callback for letting GDB read a register.
116 * @gdb_write_register: Callback for letting GDB write a register.
568496c0
SF
117 * @debug_check_watchpoint: Callback: return true if the architectural
118 * watchpoint whose address has matched should really fire.
86025ee4 119 * @debug_excp_handler: Callback for handling debug exceptions.
c08295d4
PM
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.
b170fce3 128 * @vmsd: State description for migration.
a0e372f0 129 * @gdb_num_core_regs: Number of core registers accessible to GDB.
5b24c641 130 * @gdb_core_xml_file: File name for core registers GDB XML description.
2472b6c0
PM
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.
b3820e6c
DH
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.
cffe7b32
RH
135 * @cpu_exec_enter: Callback for cpu_exec preparation.
136 * @cpu_exec_exit: Callback for cpu_exec cleanup.
9585db68 137 * @cpu_exec_interrupt: Callback for processing interrupts in cpu_exec.
37b9de46 138 * @disas_set_info: Setup architecture specific components of disassembly info
40612000
JB
139 * @adjust_watchpoint_address: Perform a target-specific adjustment to an
140 * address before attempting to match it against watchpoints.
dd83b06a
AF
141 *
142 * Represents a CPU family or model.
143 */
144typedef struct CPUClass {
145 /*< private >*/
961f8395 146 DeviceClass parent_class;
dd83b06a
AF
147 /*< public >*/
148
2b8c2754 149 ObjectClass *(*class_by_name)(const char *cpu_model);
62a48a2a 150 void (*parse_features)(const char *typename, char *str, Error **errp);
2b8c2754 151
dd83b06a 152 void (*reset)(CPUState *cpu);
91b1df8c 153 int reset_dump_flags;
8c2e1b00 154 bool (*has_work)(CPUState *cpu);
97a8ea5a 155 void (*do_interrupt)(CPUState *cpu);
c658b94f 156 CPUUnassignedAccess do_unassigned_access;
93e22326 157 void (*do_unaligned_access)(CPUState *cpu, vaddr addr,
b35399bb
SS
158 MMUAccessType access_type,
159 int mmu_idx, uintptr_t retaddr);
0dff0939
PM
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);
bf7663c4 164 bool (*virtio_is_big_endian)(CPUState *cpu);
f3659eee
AF
165 int (*memory_rw_debug)(CPUState *cpu, vaddr addr,
166 uint8_t *buf, int len, bool is_write);
878096ee
AF
167 void (*dump_state)(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
168 int flags);
c86f106b 169 GuestPanicInformation* (*get_crash_info)(CPUState *cpu);
878096ee
AF
170 void (*dump_statistics)(CPUState *cpu, FILE *f,
171 fprintf_function cpu_fprintf, int flags);
997395d3 172 int64_t (*get_arch_id)(CPUState *cpu);
444d5590 173 bool (*get_paging_enabled)(const CPUState *cpu);
a23bbfda
AF
174 void (*get_memory_mapping)(CPUState *cpu, MemoryMappingList *list,
175 Error **errp);
f45748f1 176 void (*set_pc)(CPUState *cpu, vaddr value);
bdf7ae5b 177 void (*synchronize_from_tb)(CPUState *cpu, struct TranslationBlock *tb);
98670d47 178 int (*handle_mmu_fault)(CPUState *cpu, vaddr address, int size, int rw,
7510454e 179 int mmu_index);
00b941e5 180 hwaddr (*get_phys_page_debug)(CPUState *cpu, vaddr addr);
1dc6fb1f
PM
181 hwaddr (*get_phys_page_attrs_debug)(CPUState *cpu, vaddr addr,
182 MemTxAttrs *attrs);
d7f25a9e 183 int (*asidx_from_attrs)(CPUState *cpu, MemTxAttrs attrs);
5b50e790
AF
184 int (*gdb_read_register)(CPUState *cpu, uint8_t *buf, int reg);
185 int (*gdb_write_register)(CPUState *cpu, uint8_t *buf, int reg);
568496c0 186 bool (*debug_check_watchpoint)(CPUState *cpu, CPUWatchpoint *wp);
86025ee4 187 void (*debug_excp_handler)(CPUState *cpu);
b170fce3 188
c72bf468
JF
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);
a0e372f0
AF
197
198 const struct VMStateDescription *vmsd;
5b24c641 199 const char *gdb_core_xml_file;
b3820e6c 200 gchar * (*gdb_arch_name)(CPUState *cpu);
cffe7b32
RH
201
202 void (*cpu_exec_enter)(CPUState *cpu);
203 void (*cpu_exec_exit)(CPUState *cpu);
9585db68 204 bool (*cpu_exec_interrupt)(CPUState *cpu, int interrupt_request);
37b9de46
PC
205
206 void (*disas_set_info)(CPUState *cpu, disassemble_info *info);
40612000 207 vaddr (*adjust_watchpoint_address)(CPUState *cpu, vaddr addr, int len);
55c3ceef
RH
208 void (*tcg_initialize)(void);
209
210 /* Keep non-pointer data at the end to minimize holes. */
211 int gdb_num_core_regs;
212 bool gdb_stop_before_watchpoint;
dd83b06a
AF
213} CPUClass;
214
28ecfd7a
AF
215#ifdef HOST_WORDS_BIGENDIAN
216typedef struct icount_decr_u16 {
217 uint16_t high;
218 uint16_t low;
219} icount_decr_u16;
220#else
221typedef struct icount_decr_u16 {
222 uint16_t low;
223 uint16_t high;
224} icount_decr_u16;
225#endif
226
f0c3c505
AF
227typedef struct CPUBreakpoint {
228 vaddr pc;
229 int flags; /* BP_* */
230 QTAILQ_ENTRY(CPUBreakpoint) entry;
231} CPUBreakpoint;
232
568496c0 233struct CPUWatchpoint {
ff4700b0 234 vaddr vaddr;
05068c0d 235 vaddr len;
08225676 236 vaddr hitaddr;
66b9b43c 237 MemTxAttrs hitattrs;
ff4700b0
AF
238 int flags; /* BP_* */
239 QTAILQ_ENTRY(CPUWatchpoint) entry;
568496c0 240};
ff4700b0 241
a60f24b5 242struct KVMState;
f7575c96 243struct kvm_run;
a60f24b5 244
b0cb0a66
VP
245struct hax_vcpu_state;
246
8cd70437
AF
247#define TB_JMP_CACHE_BITS 12
248#define TB_JMP_CACHE_SIZE (1 << TB_JMP_CACHE_BITS)
249
4b4629d9 250/* work queue */
14e6fe12
PB
251
252/* The union type allows passing of 64 bit target pointers on 32 bit
253 * hosts in a single parameter
254 */
255typedef union {
256 int host_int;
257 unsigned long host_ulong;
258 void *host_ptr;
259 vaddr target_ptr;
260} run_on_cpu_data;
261
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)
267
268typedef void (*run_on_cpu_func)(CPUState *cpu, run_on_cpu_data data);
269
d148d90e 270struct qemu_work_item;
4b4629d9 271
0b8497f0 272#define CPU_UNSET_NUMA_NODE_ID -1
d01c05c9 273#define CPU_TRACE_DSTATE_MAX_EVENTS 32
0b8497f0 274
dd83b06a
AF
275/**
276 * CPUState:
55e5c285 277 * @cpu_index: CPU index (informative).
ce3960eb
AF
278 * @nr_cores: Number of cores within this CPU package.
279 * @nr_threads: Number of threads within this CPU.
c265e976
PB
280 * @running: #true if CPU is currently running (lockless).
281 * @has_waiter: #true if a CPU is currently waiting for the cpu_exec_end;
ab129972 282 * valid under cpu_list_lock.
61a46217 283 * @created: Indicates whether the CPU thread has been successfully created.
259186a7
AF
284 * @interrupt_request: Indicates a pending interrupt request.
285 * @halted: Nonzero if the CPU is in suspended state.
4fdeee7c 286 * @stop: Indicates a pending stop request.
f324e766 287 * @stopped: Indicates the CPU has been artificially stopped.
4c055ab5 288 * @unplug: Indicates a pending CPU unplug request.
bac05aa9 289 * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU
ed2803da 290 * @singlestep_enabled: Flags for single-stepping.
efee7340 291 * @icount_extra: Instructions until next timer event.
1aab16c2
PB
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).
28ecfd7a
AF
295 * This allows a single read-compare-cbranch-write sequence to test
296 * for both decrementer underflow and exceptions.
414b15c9
PB
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.
32857f4d
PM
300 * @cpu_ases: Pointer to array of CPUAddressSpaces (which define the
301 * AddressSpaces this CPU has)
12ebc9a7 302 * @num_ases: number of CPUAddressSpaces in @cpu_ases
32857f4d
PM
303 * @as: Pointer to the first AddressSpace, for the convenience of targets which
304 * only have a single AddressSpace
c05efcb1 305 * @env_ptr: Pointer to subclass-specific CPUArchState field.
eac8b355 306 * @gdb_regs: Additional GDB registers.
a0e372f0 307 * @gdb_num_regs: Number of total registers accessible to GDB.
35143f01 308 * @gdb_num_g_regs: Number of registers in GDB 'g' packets.
182735ef 309 * @next_cpu: Next CPU sharing TB cache.
0429a971 310 * @opaque: User data.
93afeade
AF
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.
8737c51c 313 * @kvm_fd: vCPU file descriptor for KVM.
376692b9
PB
314 * @work_mutex: Lock to prevent multiple access to queued_work_*.
315 * @queued_work_first: First asynchronous work pending.
d4381116
LV
316 * @trace_dstate_delayed: Delayed changes to trace_dstate (includes all changes
317 * to @trace_dstate).
48151859 318 * @trace_dstate: Dynamic tracing state of events for this vCPU (bitmask).
ed860129
PM
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.
dd83b06a
AF
322 *
323 * State of one CPU core or thread.
324 */
325struct CPUState {
326 /*< private >*/
961f8395 327 DeviceState parent_obj;
dd83b06a
AF
328 /*< public >*/
329
ce3960eb
AF
330 int nr_cores;
331 int nr_threads;
332
814e612e 333 struct QemuThread *thread;
bcba2a72
AF
334#ifdef _WIN32
335 HANDLE hThread;
336#endif
9f09e18a 337 int thread_id;
c265e976 338 bool running, has_waiter;
f5c121b8 339 struct QemuCond *halt_cond;
216fc9a4 340 bool thread_kicked;
61a46217 341 bool created;
4fdeee7c 342 bool stop;
f324e766 343 bool stopped;
4c055ab5 344 bool unplug;
bac05aa9 345 bool crash_occurred;
e0c38211 346 bool exit_request;
9b990ee5 347 uint32_t cflags_next_tb;
8d04fb55 348 /* updates protected by BQL */
259186a7 349 uint32_t interrupt_request;
ed2803da 350 int singlestep_enabled;
e4cd9657 351 int64_t icount_budget;
efee7340 352 int64_t icount_extra;
6f03bef0 353 sigjmp_buf jmp_env;
bcba2a72 354
376692b9
PB
355 QemuMutex work_mutex;
356 struct qemu_work_item *queued_work_first, *queued_work_last;
357
32857f4d 358 CPUAddressSpace *cpu_ases;
12ebc9a7 359 int num_ases;
09daed84 360 AddressSpace *as;
6731d864 361 MemoryRegion *memory;
09daed84 362
c05efcb1 363 void *env_ptr; /* CPUArchState */
7d7500d9 364
f3ced3c5 365 /* Accessed in parallel; all accesses must be atomic */
8cd70437 366 struct TranslationBlock *tb_jmp_cache[TB_JMP_CACHE_SIZE];
7d7500d9 367
eac8b355 368 struct GDBRegisterState *gdb_regs;
a0e372f0 369 int gdb_num_regs;
35143f01 370 int gdb_num_g_regs;
bdc44640 371 QTAILQ_ENTRY(CPUState) node;
d77953b9 372
f0c3c505
AF
373 /* ice debug support */
374 QTAILQ_HEAD(breakpoints_head, CPUBreakpoint) breakpoints;
375
ff4700b0
AF
376 QTAILQ_HEAD(watchpoints_head, CPUWatchpoint) watchpoints;
377 CPUWatchpoint *watchpoint_hit;
378
0429a971
AF
379 void *opaque;
380
93afeade
AF
381 /* In order to avoid passing too many arguments to the MMIO helpers,
382 * we store some rarely used information in the CPU context.
383 */
384 uintptr_t mem_io_pc;
385 vaddr mem_io_vaddr;
386
8737c51c 387 int kvm_fd;
a60f24b5 388 struct KVMState *kvm_state;
f7575c96 389 struct kvm_run *kvm_run;
8737c51c 390
d01c05c9 391 /* Used for events with 'vcpu' and *without* the 'disabled' properties */
d4381116 392 DECLARE_BITMAP(trace_dstate_delayed, CPU_TRACE_DSTATE_MAX_EVENTS);
d01c05c9 393 DECLARE_BITMAP(trace_dstate, CPU_TRACE_DSTATE_MAX_EVENTS);
48151859 394
f5df5baf 395 /* TODO Move common fields from CPUArchState here. */
6fda014e
DH
396 int cpu_index;
397 uint32_t halted;
99df7dce 398 uint32_t can_do_io;
6fda014e 399 int32_t exception_index;
7e4fb26d 400
99f31832
SAGDR
401 /* shared by kvm, hax and hvf */
402 bool vcpu_dirty;
403
2adcc85d
JH
404 /* Used to keep track of an outstanding cpu throttle thread for migration
405 * autoconverge
406 */
407 bool throttle_thread_scheduled;
408
ed860129
PM
409 bool ignore_memory_transaction_failures;
410
7e4fb26d
RH
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. */
1aab16c2
PB
415 union {
416 uint32_t u32;
417 icount_decr_u16 u16;
418 } icount_decr;
b0cb0a66 419
b0cb0a66 420 struct hax_vcpu_state *hax_vcpu;
e3b9ca81
FK
421
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.
425 */
e7218445 426 uint16_t pending_tlb_flush;
c97d6d2c
SAGDR
427
428 int hvf_fd;
dd83b06a
AF
429};
430
bdc44640
AF
431QTAILQ_HEAD(CPUTailQ, CPUState);
432extern 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)
8487d123
BR
437#define CPU_FOREACH_REVERSE(cpu) \
438 QTAILQ_FOREACH_REVERSE(cpu, &cpus, CPUTailQ, node)
bdc44640 439#define first_cpu QTAILQ_FIRST(&cpus)
182735ef 440
f240eb6f 441extern __thread CPUState *current_cpu;
4917cf44 442
f3ced3c5
EC
443static inline void cpu_tb_jmp_cache_clear(CPUState *cpu)
444{
445 unsigned int i;
446
447 for (i = 0; i < TB_JMP_CACHE_SIZE; i++) {
448 atomic_set(&cpu->tb_jmp_cache[i], NULL);
449 }
450}
451
8d4e9146
FK
452/**
453 * qemu_tcg_mttcg_enabled:
454 * Check whether we are running MultiThread TCG or not.
455 *
456 * Returns: %true if we are in MTTCG mode %false otherwise.
457 */
458extern bool mttcg_enabled;
459#define qemu_tcg_mttcg_enabled() (mttcg_enabled)
460
444d5590
AF
461/**
462 * cpu_paging_enabled:
463 * @cpu: The CPU whose state is to be inspected.
464 *
465 * Returns: %true if paging is enabled, %false otherwise.
466 */
467bool cpu_paging_enabled(const CPUState *cpu);
468
a23bbfda
AF
469/**
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.
474 */
475void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
476 Error **errp);
477
c72bf468
JF
478/**
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
484 */
485int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
486 int cpuid, void *opaque);
487
488/**
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
494 */
495int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
496 void *opaque);
497
498/**
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
504 */
505int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
506 int cpuid, void *opaque);
507
508/**
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
514 */
515int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
516 void *opaque);
dd83b06a 517
c86f106b
AN
518/**
519 * cpu_get_crash_info:
520 * @cpu: The CPU to get crash information for
521 *
522 * Gets the previously saved crash information.
523 * Caller is responsible for freeing the data.
524 */
525GuestPanicInformation *cpu_get_crash_info(CPUState *cpu);
526
878096ee
AF
527/**
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
532 */
533enum CPUDumpFlags {
534 CPU_DUMP_CODE = 0x00010000,
535 CPU_DUMP_FPU = 0x00020000,
536 CPU_DUMP_CCOP = 0x00040000,
537};
538
539/**
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.
545 *
546 * Dumps CPU state.
547 */
548void cpu_dump_state(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
549 int flags);
550
551/**
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.
557 *
558 * Dumps CPU statistics.
559 */
560void cpu_dump_statistics(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
561 int flags);
562
00b941e5 563#ifndef CONFIG_USER_ONLY
1dc6fb1f
PM
564/**
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.
570 *
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.
574 *
575 * Returns: Corresponding physical page address or -1 if no page found.
576 */
577static inline hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr,
578 MemTxAttrs *attrs)
579{
580 CPUClass *cc = CPU_GET_CLASS(cpu);
581
582 if (cc->get_phys_page_attrs_debug) {
583 return cc->get_phys_page_attrs_debug(cpu, addr, attrs);
584 }
585 /* Fallback for CPUs which don't implement the _attrs_ hook */
586 *attrs = MEMTXATTRS_UNSPECIFIED;
587 return cc->get_phys_page_debug(cpu, addr);
588}
589
00b941e5
AF
590/**
591 * cpu_get_phys_page_debug:
592 * @cpu: The CPU to obtain the physical page address for.
593 * @addr: The virtual address.
594 *
595 * Obtains the physical page corresponding to a virtual one.
596 * Use it only for debugging because no protection checks are done.
597 *
598 * Returns: Corresponding physical page address or -1 if no page found.
599 */
600static inline hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr)
601{
1dc6fb1f 602 MemTxAttrs attrs = {};
00b941e5 603
1dc6fb1f 604 return cpu_get_phys_page_attrs_debug(cpu, addr, &attrs);
00b941e5 605}
d7f25a9e
PM
606
607/** cpu_asidx_from_attrs:
608 * @cpu: CPU
609 * @attrs: memory transaction attributes
610 *
611 * Returns the address space index specifying the CPU AddressSpace
612 * to use for a memory access with the given transaction attributes.
613 */
614static inline int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs)
615{
616 CPUClass *cc = CPU_GET_CLASS(cpu);
617
618 if (cc->asidx_from_attrs) {
619 return cc->asidx_from_attrs(cpu, attrs);
620 }
621 return 0;
622}
00b941e5
AF
623#endif
624
267f685b
PB
625/**
626 * cpu_list_add:
627 * @cpu: The CPU to be added to the list of CPUs.
628 */
629void cpu_list_add(CPUState *cpu);
630
631/**
632 * cpu_list_remove:
633 * @cpu: The CPU to be removed from the list of CPUs.
634 */
635void cpu_list_remove(CPUState *cpu);
636
dd83b06a
AF
637/**
638 * cpu_reset:
639 * @cpu: The CPU whose state is to be reset.
640 */
641void cpu_reset(CPUState *cpu);
642
2b8c2754
AF
643/**
644 * cpu_class_by_name:
645 * @typename: The CPU base type.
646 * @cpu_model: The model string without any parameters.
647 *
648 * Looks up a CPU #ObjectClass matching name @cpu_model.
649 *
650 * Returns: A #CPUClass or %NULL if not matching class is found.
651 */
652ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
653
3c72234c
IM
654/**
655 * cpu_create:
656 * @typename: The CPU type.
657 *
658 * Instantiates a CPU and realizes the CPU.
659 *
660 * Returns: A #CPUState or %NULL if an error occurred.
661 */
662CPUState *cpu_create(const char *typename);
663
664/**
665 * cpu_parse_cpu_model:
666 * @typename: The CPU base type or CPU type.
667 * @cpu_model: The model string including optional parameters.
668 *
669 * processes optional parameters and registers them as global properties
670 *
4482e05c
IM
671 * Returns: type of CPU to create or prints error and terminates process
672 * if an error occurred.
3c72234c
IM
673 */
674const char *cpu_parse_cpu_model(const char *typename, const char *cpu_model);
675
9262685b
AF
676/**
677 * cpu_generic_init:
678 * @typename: The CPU base type.
679 * @cpu_model: The model string including optional parameters.
680 *
681 * Instantiates a CPU, processes optional parameters and realizes the CPU.
682 *
683 * Returns: A #CPUState or %NULL if an error occurred.
684 */
685CPUState *cpu_generic_init(const char *typename, const char *cpu_model);
686
3993c6bd 687/**
8c2e1b00 688 * cpu_has_work:
3993c6bd
AF
689 * @cpu: The vCPU to check.
690 *
691 * Checks whether the CPU has work to do.
692 *
693 * Returns: %true if the CPU has work, %false otherwise.
694 */
8c2e1b00
AF
695static inline bool cpu_has_work(CPUState *cpu)
696{
697 CPUClass *cc = CPU_GET_CLASS(cpu);
698
699 g_assert(cc->has_work);
700 return cc->has_work(cpu);
701}
3993c6bd 702
60e82579
AF
703/**
704 * qemu_cpu_is_self:
705 * @cpu: The vCPU to check against.
706 *
707 * Checks whether the caller is executing on the vCPU thread.
708 *
709 * Returns: %true if called from @cpu's thread, %false otherwise.
710 */
711bool qemu_cpu_is_self(CPUState *cpu);
712
c08d7424
AF
713/**
714 * qemu_cpu_kick:
715 * @cpu: The vCPU to kick.
716 *
717 * Kicks @cpu's thread.
718 */
719void qemu_cpu_kick(CPUState *cpu);
720
2fa45344
AF
721/**
722 * cpu_is_stopped:
723 * @cpu: The CPU to check.
724 *
725 * Checks whether the CPU is stopped.
726 *
727 * Returns: %true if run state is not running or if artificially stopped;
728 * %false otherwise.
729 */
730bool cpu_is_stopped(CPUState *cpu);
731
d148d90e
SF
732/**
733 * do_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.
737 * @mutex: Mutex to release while waiting for @func to run.
738 *
739 * Used internally in the implementation of run_on_cpu.
740 */
14e6fe12 741void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data,
d148d90e
SF
742 QemuMutex *mutex);
743
f100f0b3
AF
744/**
745 * run_on_cpu:
746 * @cpu: The vCPU to run on.
747 * @func: The function to be executed.
748 * @data: Data to pass to the function.
749 *
750 * Schedules the function @func for execution on the vCPU @cpu.
751 */
14e6fe12 752void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
f100f0b3 753
3c02270d
CV
754/**
755 * async_run_on_cpu:
756 * @cpu: The vCPU to run on.
757 * @func: The function to be executed.
758 * @data: Data to pass to the function.
759 *
760 * Schedules the function @func for execution on the vCPU @cpu asynchronously.
761 */
14e6fe12 762void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
3c02270d 763
53f5ed95
PB
764/**
765 * async_safe_run_on_cpu:
766 * @cpu: The vCPU to run on.
767 * @func: The function to be executed.
768 * @data: Data to pass to the function.
769 *
770 * Schedules the function @func for execution on the vCPU @cpu asynchronously,
771 * while all other vCPUs are sleeping.
772 *
773 * Unlike run_on_cpu and async_run_on_cpu, the function is run outside the
774 * BQL.
775 */
14e6fe12 776void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
53f5ed95 777
38d8f5c8
AF
778/**
779 * qemu_get_cpu:
780 * @index: The CPUState@cpu_index value of the CPU to obtain.
781 *
782 * Gets a CPU matching @index.
783 *
784 * Returns: The CPU or %NULL if there is no matching CPU.
785 */
786CPUState *qemu_get_cpu(int index);
787
69e5ff06
IM
788/**
789 * cpu_exists:
790 * @id: Guest-exposed CPU ID to lookup.
791 *
792 * Search for CPU with specified ID.
793 *
794 * Returns: %true - CPU is found, %false - CPU isn't found.
795 */
796bool cpu_exists(int64_t id);
797
5ce46cb3
EH
798/**
799 * cpu_by_arch_id:
800 * @id: Guest-exposed CPU ID of the CPU to obtain.
801 *
802 * Get a CPU with matching @id.
803 *
804 * Returns: The CPU or %NULL if there is no matching CPU.
805 */
806CPUState *cpu_by_arch_id(int64_t id);
807
2adcc85d
JH
808/**
809 * cpu_throttle_set:
810 * @new_throttle_pct: Percent of sleep time. Valid range is 1 to 99.
811 *
812 * Throttles all vcpus by forcing them to sleep for the given percentage of
813 * time. A throttle_percentage of 25 corresponds to a 75% duty cycle roughly.
814 * (example: 10ms sleep for every 30ms awake).
815 *
816 * cpu_throttle_set can be called as needed to adjust new_throttle_pct.
817 * Once the throttling starts, it will remain in effect until cpu_throttle_stop
818 * is called.
819 */
820void cpu_throttle_set(int new_throttle_pct);
821
822/**
823 * cpu_throttle_stop:
824 *
825 * Stops the vcpu throttling started by cpu_throttle_set.
826 */
827void cpu_throttle_stop(void);
828
829/**
830 * cpu_throttle_active:
831 *
832 * Returns: %true if the vcpus are currently being throttled, %false otherwise.
833 */
834bool cpu_throttle_active(void);
835
836/**
837 * cpu_throttle_get_percentage:
838 *
839 * Returns the vcpu throttle percentage. See cpu_throttle_set for details.
840 *
841 * Returns: The throttle percentage in range 1 to 99.
842 */
843int cpu_throttle_get_percentage(void);
844
c3affe56
AF
845#ifndef CONFIG_USER_ONLY
846
847typedef void (*CPUInterruptHandler)(CPUState *, int);
848
849extern CPUInterruptHandler cpu_interrupt_handler;
850
851/**
852 * cpu_interrupt:
853 * @cpu: The CPU to set an interrupt on.
854 * @mask: The interupts to set.
855 *
856 * Invokes the interrupt handler.
857 */
858static inline void cpu_interrupt(CPUState *cpu, int mask)
859{
860 cpu_interrupt_handler(cpu, mask);
861}
862
863#else /* USER_ONLY */
864
865void cpu_interrupt(CPUState *cpu, int mask);
866
867#endif /* USER_ONLY */
868
47507383
TH
869#ifdef NEED_CPU_H
870
93e22326 871#ifdef CONFIG_SOFTMMU
c658b94f
AF
872static inline void cpu_unassigned_access(CPUState *cpu, hwaddr addr,
873 bool is_write, bool is_exec,
874 int opaque, unsigned size)
875{
876 CPUClass *cc = CPU_GET_CLASS(cpu);
877
878 if (cc->do_unassigned_access) {
879 cc->do_unassigned_access(cpu, addr, is_write, is_exec, opaque, size);
880 }
881}
882
93e22326 883static inline void cpu_unaligned_access(CPUState *cpu, vaddr addr,
b35399bb
SS
884 MMUAccessType access_type,
885 int mmu_idx, uintptr_t retaddr)
93e22326
PB
886{
887 CPUClass *cc = CPU_GET_CLASS(cpu);
888
b35399bb 889 cc->do_unaligned_access(cpu, addr, access_type, mmu_idx, retaddr);
93e22326 890}
0dff0939
PM
891
892static inline void cpu_transaction_failed(CPUState *cpu, hwaddr physaddr,
893 vaddr addr, unsigned size,
894 MMUAccessType access_type,
895 int mmu_idx, MemTxAttrs attrs,
896 MemTxResult response,
897 uintptr_t retaddr)
898{
899 CPUClass *cc = CPU_GET_CLASS(cpu);
900
ed860129 901 if (!cpu->ignore_memory_transaction_failures && cc->do_transaction_failed) {
0dff0939
PM
902 cc->do_transaction_failed(cpu, physaddr, addr, size, access_type,
903 mmu_idx, attrs, response, retaddr);
904 }
905}
c658b94f
AF
906#endif
907
47507383
TH
908#endif /* NEED_CPU_H */
909
2991b890
PC
910/**
911 * cpu_set_pc:
912 * @cpu: The CPU to set the program counter for.
913 * @addr: Program counter value.
914 *
915 * Sets the program counter for a CPU.
916 */
917static inline void cpu_set_pc(CPUState *cpu, vaddr addr)
918{
919 CPUClass *cc = CPU_GET_CLASS(cpu);
920
921 cc->set_pc(cpu, addr);
922}
923
d8ed887b
AF
924/**
925 * cpu_reset_interrupt:
926 * @cpu: The CPU to clear the interrupt on.
927 * @mask: The interrupt mask to clear.
928 *
929 * Resets interrupts on the vCPU @cpu.
930 */
931void cpu_reset_interrupt(CPUState *cpu, int mask);
932
60a3e17a
AF
933/**
934 * cpu_exit:
935 * @cpu: The CPU to exit.
936 *
937 * Requests the CPU @cpu to exit execution.
938 */
939void cpu_exit(CPUState *cpu);
940
2993683b
IM
941/**
942 * cpu_resume:
943 * @cpu: The CPU to resume.
944 *
945 * Resumes CPU, i.e. puts CPU into runnable state.
946 */
947void cpu_resume(CPUState *cpu);
dd83b06a 948
4c055ab5
GZ
949/**
950 * cpu_remove:
951 * @cpu: The CPU to remove.
952 *
953 * Requests the CPU to be removed.
954 */
955void cpu_remove(CPUState *cpu);
956
2c579042
BR
957 /**
958 * cpu_remove_sync:
959 * @cpu: The CPU to remove.
960 *
961 * Requests the CPU to be removed and waits till it is removed.
962 */
963void cpu_remove_sync(CPUState *cpu);
964
d148d90e
SF
965/**
966 * process_queued_cpu_work() - process all items on CPU work queue
967 * @cpu: The CPU which work queue to process.
968 */
969void process_queued_cpu_work(CPUState *cpu);
970
ab129972
PB
971/**
972 * cpu_exec_start:
973 * @cpu: The CPU for the current thread.
974 *
975 * Record that a CPU has started execution and can be interrupted with
976 * cpu_exit.
977 */
978void cpu_exec_start(CPUState *cpu);
979
980/**
981 * cpu_exec_end:
982 * @cpu: The CPU for the current thread.
983 *
984 * Record that a CPU has stopped execution and exclusive sections
985 * can be executed without interrupting it.
986 */
987void cpu_exec_end(CPUState *cpu);
988
989/**
990 * start_exclusive:
991 *
992 * Wait for a concurrent exclusive section to end, and then start
993 * a section of work that is run while other CPUs are not running
994 * between cpu_exec_start and cpu_exec_end. CPUs that are running
995 * cpu_exec are exited immediately. CPUs that call cpu_exec_start
996 * during the exclusive section go to sleep until this CPU calls
997 * end_exclusive.
ab129972
PB
998 */
999void start_exclusive(void);
1000
1001/**
1002 * end_exclusive:
1003 *
1004 * Concludes an exclusive execution section started by start_exclusive.
ab129972
PB
1005 */
1006void end_exclusive(void);
1007
c643bed9
AF
1008/**
1009 * qemu_init_vcpu:
1010 * @cpu: The vCPU to initialize.
1011 *
1012 * Initializes a vCPU.
1013 */
1014void qemu_init_vcpu(CPUState *cpu);
1015
3825b28f
AF
1016#define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
1017#define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
1018#define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
1019
1020/**
1021 * cpu_single_step:
1022 * @cpu: CPU to the flags for.
1023 * @enabled: Flags to enable.
1024 *
1025 * Enables or disables single-stepping for @cpu.
1026 */
1027void cpu_single_step(CPUState *cpu, int enabled);
1028
b3310ab3
AF
1029/* Breakpoint/watchpoint flags */
1030#define BP_MEM_READ 0x01
1031#define BP_MEM_WRITE 0x02
1032#define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE)
1033#define BP_STOP_BEFORE_ACCESS 0x04
08225676 1034/* 0x08 currently unused */
b3310ab3
AF
1035#define BP_GDB 0x10
1036#define BP_CPU 0x20
b933066a 1037#define BP_ANY (BP_GDB | BP_CPU)
08225676
PM
1038#define BP_WATCHPOINT_HIT_READ 0x40
1039#define BP_WATCHPOINT_HIT_WRITE 0x80
1040#define BP_WATCHPOINT_HIT (BP_WATCHPOINT_HIT_READ | BP_WATCHPOINT_HIT_WRITE)
b3310ab3
AF
1041
1042int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
1043 CPUBreakpoint **breakpoint);
1044int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags);
1045void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint);
1046void cpu_breakpoint_remove_all(CPUState *cpu, int mask);
1047
b933066a
RH
1048/* Return true if PC matches an installed breakpoint. */
1049static inline bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask)
1050{
1051 CPUBreakpoint *bp;
1052
1053 if (unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) {
1054 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
1055 if (bp->pc == pc && (bp->flags & mask)) {
1056 return true;
1057 }
1058 }
1059 }
1060 return false;
1061}
1062
75a34036
AF
1063int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
1064 int flags, CPUWatchpoint **watchpoint);
1065int cpu_watchpoint_remove(CPUState *cpu, vaddr addr,
1066 vaddr len, int flags);
1067void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint);
1068void cpu_watchpoint_remove_all(CPUState *cpu, int mask);
1069
63c91552
PB
1070/**
1071 * cpu_get_address_space:
1072 * @cpu: CPU to get address space from
1073 * @asidx: index identifying which address space to get
1074 *
1075 * Return the requested address space of this CPU. @asidx
1076 * specifies which address space to read.
1077 */
1078AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx);
1079
a47dddd7
AF
1080void QEMU_NORETURN cpu_abort(CPUState *cpu, const char *fmt, ...)
1081 GCC_FMT_ATTR(2, 3);
c7e002c5 1082extern Property cpu_common_props[];
39e329e3 1083void cpu_exec_initfn(CPUState *cpu);
ce5b1bbf 1084void cpu_exec_realizefn(CPUState *cpu, Error **errp);
7bbc124e 1085void cpu_exec_unrealizefn(CPUState *cpu);
a47dddd7 1086
47507383
TH
1087#ifdef NEED_CPU_H
1088
1a1562f5
AF
1089#ifdef CONFIG_SOFTMMU
1090extern const struct VMStateDescription vmstate_cpu_common;
1091#else
1092#define vmstate_cpu_common vmstate_dummy
1093#endif
1094
1095#define VMSTATE_CPU() { \
1096 .name = "parent_obj", \
1097 .size = sizeof(CPUState), \
1098 .vmsd = &vmstate_cpu_common, \
1099 .flags = VMS_STRUCT, \
1100 .offset = 0, \
1101}
1102
47507383
TH
1103#endif /* NEED_CPU_H */
1104
a07f953e
IM
1105#define UNASSIGNED_CPU_INDEX -1
1106
dd83b06a 1107#endif