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