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