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