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