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