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