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