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