]>
Commit | Line | Data |
---|---|---|
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" |
48151859 | 30 | #include "trace/generated-events.h" |
dd83b06a | 31 | |
b5ba1cc6 QN |
32 | typedef int (*WriteCoreDumpFunction)(const void *buf, size_t size, |
33 | void *opaque); | |
c72bf468 | 34 | |
577f42c0 AF |
35 | /** |
36 | * vaddr: | |
37 | * Type wide enough to contain any #target_ulong virtual address. | |
38 | */ | |
39 | typedef uint64_t vaddr; | |
40 | #define VADDR_PRId PRId64 | |
41 | #define VADDR_PRIu PRIu64 | |
42 | #define VADDR_PRIo PRIo64 | |
43 | #define VADDR_PRIx PRIx64 | |
44 | #define VADDR_PRIX PRIX64 | |
45 | #define VADDR_MAX UINT64_MAX | |
46 | ||
dd83b06a AF |
47 | /** |
48 | * SECTION:cpu | |
49 | * @section_id: QEMU-cpu | |
50 | * @title: CPU Class | |
51 | * @short_description: Base class for all CPUs | |
52 | */ | |
53 | ||
54 | #define TYPE_CPU "cpu" | |
55 | ||
0d6d1ab4 AF |
56 | /* Since this macro is used a lot in hot code paths and in conjunction with |
57 | * FooCPU *foo_env_get_cpu(), we deviate from usual QOM practice by using | |
58 | * an unchecked cast. | |
59 | */ | |
60 | #define CPU(obj) ((CPUState *)(obj)) | |
61 | ||
dd83b06a AF |
62 | #define CPU_CLASS(class) OBJECT_CLASS_CHECK(CPUClass, (class), TYPE_CPU) |
63 | #define CPU_GET_CLASS(obj) OBJECT_GET_CLASS(CPUClass, (obj), TYPE_CPU) | |
64 | ||
b35399bb SS |
65 | typedef enum MMUAccessType { |
66 | MMU_DATA_LOAD = 0, | |
67 | MMU_DATA_STORE = 1, | |
68 | MMU_INST_FETCH = 2 | |
69 | } MMUAccessType; | |
70 | ||
568496c0 | 71 | typedef struct CPUWatchpoint CPUWatchpoint; |
dd83b06a | 72 | |
c658b94f AF |
73 | typedef void (*CPUUnassignedAccess)(CPUState *cpu, hwaddr addr, |
74 | bool is_write, bool is_exec, int opaque, | |
75 | unsigned size); | |
76 | ||
bdf7ae5b AF |
77 | struct TranslationBlock; |
78 | ||
dd83b06a AF |
79 | /** |
80 | * CPUClass: | |
2b8c2754 AF |
81 | * @class_by_name: Callback to map -cpu command line model name to an |
82 | * instantiatable CPU type. | |
94a444b2 | 83 | * @parse_features: Callback to parse command line arguments. |
f5df5baf | 84 | * @reset: Callback to reset the #CPUState to its initial state. |
91b1df8c | 85 | * @reset_dump_flags: #CPUDumpFlags to use for reset logging. |
8c2e1b00 | 86 | * @has_work: Callback for checking if there is work to do. |
97a8ea5a | 87 | * @do_interrupt: Callback for interrupt handling. |
c658b94f | 88 | * @do_unassigned_access: Callback for unassigned access handling. |
93e22326 PB |
89 | * @do_unaligned_access: Callback for unaligned access handling, if |
90 | * the target defines #ALIGNED_ONLY. | |
c08295d4 PM |
91 | * @virtio_is_big_endian: Callback to return %true if a CPU which supports |
92 | * runtime configurable endianness is currently big-endian. Non-configurable | |
93 | * CPUs can use the default implementation of this method. This method should | |
94 | * not be used by any callers other than the pre-1.0 virtio devices. | |
f3659eee | 95 | * @memory_rw_debug: Callback for GDB memory access. |
878096ee AF |
96 | * @dump_state: Callback for dumping state. |
97 | * @dump_statistics: Callback for dumping statistics. | |
997395d3 | 98 | * @get_arch_id: Callback for getting architecture-dependent CPU ID. |
444d5590 | 99 | * @get_paging_enabled: Callback for inquiring whether paging is enabled. |
a23bbfda | 100 | * @get_memory_mapping: Callback for obtaining the memory mappings. |
f45748f1 | 101 | * @set_pc: Callback for setting the Program Counter register. |
bdf7ae5b AF |
102 | * @synchronize_from_tb: Callback for synchronizing state from a TCG |
103 | * #TranslationBlock. | |
7510454e | 104 | * @handle_mmu_fault: Callback for handling an MMU fault. |
00b941e5 | 105 | * @get_phys_page_debug: Callback for obtaining a physical address. |
1dc6fb1f PM |
106 | * @get_phys_page_attrs_debug: Callback for obtaining a physical address and the |
107 | * associated memory transaction attributes to use for the access. | |
108 | * CPUs which use memory transaction attributes should implement this | |
109 | * instead of get_phys_page_debug. | |
d7f25a9e PM |
110 | * @asidx_from_attrs: Callback to return the CPU AddressSpace to use for |
111 | * a memory access with the specified memory transaction attributes. | |
5b50e790 AF |
112 | * @gdb_read_register: Callback for letting GDB read a register. |
113 | * @gdb_write_register: Callback for letting GDB write a register. | |
568496c0 SF |
114 | * @debug_check_watchpoint: Callback: return true if the architectural |
115 | * watchpoint whose address has matched should really fire. | |
86025ee4 | 116 | * @debug_excp_handler: Callback for handling debug exceptions. |
c08295d4 PM |
117 | * @write_elf64_note: Callback for writing a CPU-specific ELF note to a |
118 | * 64-bit VM coredump. | |
119 | * @write_elf32_qemunote: Callback for writing a CPU- and QEMU-specific ELF | |
120 | * note to a 32-bit VM coredump. | |
121 | * @write_elf32_note: Callback for writing a CPU-specific ELF note to a | |
122 | * 32-bit VM coredump. | |
123 | * @write_elf32_qemunote: Callback for writing a CPU- and QEMU-specific ELF | |
124 | * note to a 32-bit VM coredump. | |
b170fce3 | 125 | * @vmsd: State description for migration. |
a0e372f0 | 126 | * @gdb_num_core_regs: Number of core registers accessible to GDB. |
5b24c641 | 127 | * @gdb_core_xml_file: File name for core registers GDB XML description. |
2472b6c0 PM |
128 | * @gdb_stop_before_watchpoint: Indicates whether GDB expects the CPU to stop |
129 | * before the insn which triggers a watchpoint rather than after it. | |
b3820e6c DH |
130 | * @gdb_arch_name: Optional callback that returns the architecture name known |
131 | * to GDB. The caller must free the returned string with g_free. | |
cffe7b32 RH |
132 | * @cpu_exec_enter: Callback for cpu_exec preparation. |
133 | * @cpu_exec_exit: Callback for cpu_exec cleanup. | |
9585db68 | 134 | * @cpu_exec_interrupt: Callback for processing interrupts in cpu_exec. |
37b9de46 | 135 | * @disas_set_info: Setup architecture specific components of disassembly info |
dd83b06a AF |
136 | * |
137 | * Represents a CPU family or model. | |
138 | */ | |
139 | typedef struct CPUClass { | |
140 | /*< private >*/ | |
961f8395 | 141 | DeviceClass parent_class; |
dd83b06a AF |
142 | /*< public >*/ |
143 | ||
2b8c2754 | 144 | ObjectClass *(*class_by_name)(const char *cpu_model); |
62a48a2a | 145 | void (*parse_features)(const char *typename, char *str, Error **errp); |
2b8c2754 | 146 | |
dd83b06a | 147 | void (*reset)(CPUState *cpu); |
91b1df8c | 148 | int reset_dump_flags; |
8c2e1b00 | 149 | bool (*has_work)(CPUState *cpu); |
97a8ea5a | 150 | void (*do_interrupt)(CPUState *cpu); |
c658b94f | 151 | CPUUnassignedAccess do_unassigned_access; |
93e22326 | 152 | void (*do_unaligned_access)(CPUState *cpu, vaddr addr, |
b35399bb SS |
153 | MMUAccessType access_type, |
154 | int mmu_idx, uintptr_t retaddr); | |
bf7663c4 | 155 | bool (*virtio_is_big_endian)(CPUState *cpu); |
f3659eee AF |
156 | int (*memory_rw_debug)(CPUState *cpu, vaddr addr, |
157 | uint8_t *buf, int len, bool is_write); | |
878096ee AF |
158 | void (*dump_state)(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf, |
159 | int flags); | |
160 | void (*dump_statistics)(CPUState *cpu, FILE *f, | |
161 | fprintf_function cpu_fprintf, int flags); | |
997395d3 | 162 | int64_t (*get_arch_id)(CPUState *cpu); |
444d5590 | 163 | bool (*get_paging_enabled)(const CPUState *cpu); |
a23bbfda AF |
164 | void (*get_memory_mapping)(CPUState *cpu, MemoryMappingList *list, |
165 | Error **errp); | |
f45748f1 | 166 | void (*set_pc)(CPUState *cpu, vaddr value); |
bdf7ae5b | 167 | void (*synchronize_from_tb)(CPUState *cpu, struct TranslationBlock *tb); |
7510454e AF |
168 | int (*handle_mmu_fault)(CPUState *cpu, vaddr address, int rw, |
169 | int mmu_index); | |
00b941e5 | 170 | hwaddr (*get_phys_page_debug)(CPUState *cpu, vaddr addr); |
1dc6fb1f PM |
171 | hwaddr (*get_phys_page_attrs_debug)(CPUState *cpu, vaddr addr, |
172 | MemTxAttrs *attrs); | |
d7f25a9e | 173 | int (*asidx_from_attrs)(CPUState *cpu, MemTxAttrs attrs); |
5b50e790 AF |
174 | int (*gdb_read_register)(CPUState *cpu, uint8_t *buf, int reg); |
175 | int (*gdb_write_register)(CPUState *cpu, uint8_t *buf, int reg); | |
568496c0 | 176 | bool (*debug_check_watchpoint)(CPUState *cpu, CPUWatchpoint *wp); |
86025ee4 | 177 | void (*debug_excp_handler)(CPUState *cpu); |
b170fce3 | 178 | |
c72bf468 JF |
179 | int (*write_elf64_note)(WriteCoreDumpFunction f, CPUState *cpu, |
180 | int cpuid, void *opaque); | |
181 | int (*write_elf64_qemunote)(WriteCoreDumpFunction f, CPUState *cpu, | |
182 | void *opaque); | |
183 | int (*write_elf32_note)(WriteCoreDumpFunction f, CPUState *cpu, | |
184 | int cpuid, void *opaque); | |
185 | int (*write_elf32_qemunote)(WriteCoreDumpFunction f, CPUState *cpu, | |
186 | void *opaque); | |
a0e372f0 AF |
187 | |
188 | const struct VMStateDescription *vmsd; | |
189 | int gdb_num_core_regs; | |
5b24c641 | 190 | const char *gdb_core_xml_file; |
b3820e6c | 191 | gchar * (*gdb_arch_name)(CPUState *cpu); |
2472b6c0 | 192 | bool gdb_stop_before_watchpoint; |
cffe7b32 RH |
193 | |
194 | void (*cpu_exec_enter)(CPUState *cpu); | |
195 | void (*cpu_exec_exit)(CPUState *cpu); | |
9585db68 | 196 | bool (*cpu_exec_interrupt)(CPUState *cpu, int interrupt_request); |
37b9de46 PC |
197 | |
198 | void (*disas_set_info)(CPUState *cpu, disassemble_info *info); | |
dd83b06a AF |
199 | } CPUClass; |
200 | ||
28ecfd7a AF |
201 | #ifdef HOST_WORDS_BIGENDIAN |
202 | typedef struct icount_decr_u16 { | |
203 | uint16_t high; | |
204 | uint16_t low; | |
205 | } icount_decr_u16; | |
206 | #else | |
207 | typedef struct icount_decr_u16 { | |
208 | uint16_t low; | |
209 | uint16_t high; | |
210 | } icount_decr_u16; | |
211 | #endif | |
212 | ||
f0c3c505 AF |
213 | typedef struct CPUBreakpoint { |
214 | vaddr pc; | |
215 | int flags; /* BP_* */ | |
216 | QTAILQ_ENTRY(CPUBreakpoint) entry; | |
217 | } CPUBreakpoint; | |
218 | ||
568496c0 | 219 | struct CPUWatchpoint { |
ff4700b0 | 220 | vaddr vaddr; |
05068c0d | 221 | vaddr len; |
08225676 | 222 | vaddr hitaddr; |
66b9b43c | 223 | MemTxAttrs hitattrs; |
ff4700b0 AF |
224 | int flags; /* BP_* */ |
225 | QTAILQ_ENTRY(CPUWatchpoint) entry; | |
568496c0 | 226 | }; |
ff4700b0 | 227 | |
a60f24b5 | 228 | struct KVMState; |
f7575c96 | 229 | struct kvm_run; |
a60f24b5 | 230 | |
8cd70437 AF |
231 | #define TB_JMP_CACHE_BITS 12 |
232 | #define TB_JMP_CACHE_SIZE (1 << TB_JMP_CACHE_BITS) | |
233 | ||
4b4629d9 | 234 | /* work queue */ |
e0eeb4a2 | 235 | typedef void (*run_on_cpu_func)(CPUState *cpu, void *data); |
d148d90e | 236 | struct qemu_work_item; |
4b4629d9 | 237 | |
dd83b06a AF |
238 | /** |
239 | * CPUState: | |
55e5c285 | 240 | * @cpu_index: CPU index (informative). |
ce3960eb AF |
241 | * @nr_cores: Number of cores within this CPU package. |
242 | * @nr_threads: Number of threads within this CPU. | |
1b1ed8dc | 243 | * @numa_node: NUMA node this CPU is belonging to. |
0d34282f | 244 | * @host_tid: Host thread ID. |
0315c31c | 245 | * @running: #true if CPU is currently running (usermode). |
61a46217 | 246 | * @created: Indicates whether the CPU thread has been successfully created. |
259186a7 AF |
247 | * @interrupt_request: Indicates a pending interrupt request. |
248 | * @halted: Nonzero if the CPU is in suspended state. | |
4fdeee7c | 249 | * @stop: Indicates a pending stop request. |
f324e766 | 250 | * @stopped: Indicates the CPU has been artificially stopped. |
4c055ab5 | 251 | * @unplug: Indicates a pending CPU unplug request. |
bac05aa9 | 252 | * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU |
378df4b2 PM |
253 | * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this |
254 | * CPU and return to its top level loop. | |
6f789be5 | 255 | * @tb_flushed: Indicates the translation buffer has been flushed. |
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 | */ | |
284 | struct 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; |
0315c31c | 299 | bool running; |
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; |
6f789be5 | 308 | bool tb_flushed; |
259186a7 | 309 | uint32_t interrupt_request; |
ed2803da | 310 | int singlestep_enabled; |
efee7340 | 311 | int64_t icount_extra; |
6f03bef0 | 312 | sigjmp_buf jmp_env; |
bcba2a72 | 313 | |
376692b9 PB |
314 | QemuMutex work_mutex; |
315 | struct qemu_work_item *queued_work_first, *queued_work_last; | |
316 | ||
32857f4d | 317 | CPUAddressSpace *cpu_ases; |
12ebc9a7 | 318 | int num_ases; |
09daed84 | 319 | AddressSpace *as; |
6731d864 | 320 | MemoryRegion *memory; |
09daed84 | 321 | |
c05efcb1 | 322 | void *env_ptr; /* CPUArchState */ |
8cd70437 | 323 | struct TranslationBlock *tb_jmp_cache[TB_JMP_CACHE_SIZE]; |
eac8b355 | 324 | struct GDBRegisterState *gdb_regs; |
a0e372f0 | 325 | int gdb_num_regs; |
35143f01 | 326 | int gdb_num_g_regs; |
bdc44640 | 327 | QTAILQ_ENTRY(CPUState) node; |
d77953b9 | 328 | |
f0c3c505 AF |
329 | /* ice debug support */ |
330 | QTAILQ_HEAD(breakpoints_head, CPUBreakpoint) breakpoints; | |
331 | ||
ff4700b0 AF |
332 | QTAILQ_HEAD(watchpoints_head, CPUWatchpoint) watchpoints; |
333 | CPUWatchpoint *watchpoint_hit; | |
334 | ||
0429a971 AF |
335 | void *opaque; |
336 | ||
93afeade AF |
337 | /* In order to avoid passing too many arguments to the MMIO helpers, |
338 | * we store some rarely used information in the CPU context. | |
339 | */ | |
340 | uintptr_t mem_io_pc; | |
341 | vaddr mem_io_vaddr; | |
342 | ||
8737c51c | 343 | int kvm_fd; |
20d695a9 | 344 | bool kvm_vcpu_dirty; |
a60f24b5 | 345 | struct KVMState *kvm_state; |
f7575c96 | 346 | struct kvm_run *kvm_run; |
8737c51c | 347 | |
48151859 LV |
348 | /* Used for events with 'vcpu' and *without* the 'disabled' properties */ |
349 | DECLARE_BITMAP(trace_dstate, TRACE_VCPU_EVENT_COUNT); | |
350 | ||
f5df5baf | 351 | /* TODO Move common fields from CPUArchState here. */ |
55e5c285 | 352 | int cpu_index; /* used by alpha TCG */ |
259186a7 | 353 | uint32_t halted; /* used by alpha, cris, ppc TCG */ |
28ecfd7a AF |
354 | union { |
355 | uint32_t u32; | |
356 | icount_decr_u16 u16; | |
357 | } icount_decr; | |
99df7dce | 358 | uint32_t can_do_io; |
27103424 | 359 | int32_t exception_index; /* used by m68k TCG */ |
7e4fb26d | 360 | |
2adcc85d JH |
361 | /* Used to keep track of an outstanding cpu throttle thread for migration |
362 | * autoconverge | |
363 | */ | |
364 | bool throttle_thread_scheduled; | |
365 | ||
7e4fb26d RH |
366 | /* Note that this is accessed at the start of every TB via a negative |
367 | offset from AREG0. Leave this field at the end so as to make the | |
368 | (absolute value) offset as small as possible. This reduces code | |
369 | size, especially for hosts without large memory offsets. */ | |
e0c38211 | 370 | uint32_t tcg_exit_req; |
dd83b06a AF |
371 | }; |
372 | ||
bdc44640 AF |
373 | QTAILQ_HEAD(CPUTailQ, CPUState); |
374 | extern struct CPUTailQ cpus; | |
375 | #define CPU_NEXT(cpu) QTAILQ_NEXT(cpu, node) | |
376 | #define CPU_FOREACH(cpu) QTAILQ_FOREACH(cpu, &cpus, node) | |
377 | #define CPU_FOREACH_SAFE(cpu, next_cpu) \ | |
378 | QTAILQ_FOREACH_SAFE(cpu, &cpus, node, next_cpu) | |
8487d123 BR |
379 | #define CPU_FOREACH_REVERSE(cpu) \ |
380 | QTAILQ_FOREACH_REVERSE(cpu, &cpus, CPUTailQ, node) | |
bdc44640 | 381 | #define first_cpu QTAILQ_FIRST(&cpus) |
182735ef | 382 | |
f240eb6f | 383 | extern __thread CPUState *current_cpu; |
4917cf44 | 384 | |
444d5590 AF |
385 | /** |
386 | * cpu_paging_enabled: | |
387 | * @cpu: The CPU whose state is to be inspected. | |
388 | * | |
389 | * Returns: %true if paging is enabled, %false otherwise. | |
390 | */ | |
391 | bool cpu_paging_enabled(const CPUState *cpu); | |
392 | ||
a23bbfda AF |
393 | /** |
394 | * cpu_get_memory_mapping: | |
395 | * @cpu: The CPU whose memory mappings are to be obtained. | |
396 | * @list: Where to write the memory mappings to. | |
397 | * @errp: Pointer for reporting an #Error. | |
398 | */ | |
399 | void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list, | |
400 | Error **errp); | |
401 | ||
c72bf468 JF |
402 | /** |
403 | * cpu_write_elf64_note: | |
404 | * @f: pointer to a function that writes memory to a file | |
405 | * @cpu: The CPU whose memory is to be dumped | |
406 | * @cpuid: ID number of the CPU | |
407 | * @opaque: pointer to the CPUState struct | |
408 | */ | |
409 | int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu, | |
410 | int cpuid, void *opaque); | |
411 | ||
412 | /** | |
413 | * cpu_write_elf64_qemunote: | |
414 | * @f: pointer to a function that writes memory to a file | |
415 | * @cpu: The CPU whose memory is to be dumped | |
416 | * @cpuid: ID number of the CPU | |
417 | * @opaque: pointer to the CPUState struct | |
418 | */ | |
419 | int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu, | |
420 | void *opaque); | |
421 | ||
422 | /** | |
423 | * cpu_write_elf32_note: | |
424 | * @f: pointer to a function that writes memory to a file | |
425 | * @cpu: The CPU whose memory is to be dumped | |
426 | * @cpuid: ID number of the CPU | |
427 | * @opaque: pointer to the CPUState struct | |
428 | */ | |
429 | int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu, | |
430 | int cpuid, void *opaque); | |
431 | ||
432 | /** | |
433 | * cpu_write_elf32_qemunote: | |
434 | * @f: pointer to a function that writes memory to a file | |
435 | * @cpu: The CPU whose memory is to be dumped | |
436 | * @cpuid: ID number of the CPU | |
437 | * @opaque: pointer to the CPUState struct | |
438 | */ | |
439 | int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu, | |
440 | void *opaque); | |
dd83b06a | 441 | |
878096ee AF |
442 | /** |
443 | * CPUDumpFlags: | |
444 | * @CPU_DUMP_CODE: | |
445 | * @CPU_DUMP_FPU: dump FPU register state, not just integer | |
446 | * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state | |
447 | */ | |
448 | enum CPUDumpFlags { | |
449 | CPU_DUMP_CODE = 0x00010000, | |
450 | CPU_DUMP_FPU = 0x00020000, | |
451 | CPU_DUMP_CCOP = 0x00040000, | |
452 | }; | |
453 | ||
454 | /** | |
455 | * cpu_dump_state: | |
456 | * @cpu: The CPU whose state is to be dumped. | |
457 | * @f: File to dump to. | |
458 | * @cpu_fprintf: Function to dump with. | |
459 | * @flags: Flags what to dump. | |
460 | * | |
461 | * Dumps CPU state. | |
462 | */ | |
463 | void cpu_dump_state(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf, | |
464 | int flags); | |
465 | ||
466 | /** | |
467 | * cpu_dump_statistics: | |
468 | * @cpu: The CPU whose state is to be dumped. | |
469 | * @f: File to dump to. | |
470 | * @cpu_fprintf: Function to dump with. | |
471 | * @flags: Flags what to dump. | |
472 | * | |
473 | * Dumps CPU statistics. | |
474 | */ | |
475 | void cpu_dump_statistics(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf, | |
476 | int flags); | |
477 | ||
00b941e5 | 478 | #ifndef CONFIG_USER_ONLY |
1dc6fb1f PM |
479 | /** |
480 | * cpu_get_phys_page_attrs_debug: | |
481 | * @cpu: The CPU to obtain the physical page address for. | |
482 | * @addr: The virtual address. | |
483 | * @attrs: Updated on return with the memory transaction attributes to use | |
484 | * for this access. | |
485 | * | |
486 | * Obtains the physical page corresponding to a virtual one, together | |
487 | * with the corresponding memory transaction attributes to use for the access. | |
488 | * Use it only for debugging because no protection checks are done. | |
489 | * | |
490 | * Returns: Corresponding physical page address or -1 if no page found. | |
491 | */ | |
492 | static inline hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr, | |
493 | MemTxAttrs *attrs) | |
494 | { | |
495 | CPUClass *cc = CPU_GET_CLASS(cpu); | |
496 | ||
497 | if (cc->get_phys_page_attrs_debug) { | |
498 | return cc->get_phys_page_attrs_debug(cpu, addr, attrs); | |
499 | } | |
500 | /* Fallback for CPUs which don't implement the _attrs_ hook */ | |
501 | *attrs = MEMTXATTRS_UNSPECIFIED; | |
502 | return cc->get_phys_page_debug(cpu, addr); | |
503 | } | |
504 | ||
00b941e5 AF |
505 | /** |
506 | * cpu_get_phys_page_debug: | |
507 | * @cpu: The CPU to obtain the physical page address for. | |
508 | * @addr: The virtual address. | |
509 | * | |
510 | * Obtains the physical page corresponding to a virtual one. | |
511 | * Use it only for debugging because no protection checks are done. | |
512 | * | |
513 | * Returns: Corresponding physical page address or -1 if no page found. | |
514 | */ | |
515 | static inline hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr) | |
516 | { | |
1dc6fb1f | 517 | MemTxAttrs attrs = {}; |
00b941e5 | 518 | |
1dc6fb1f | 519 | return cpu_get_phys_page_attrs_debug(cpu, addr, &attrs); |
00b941e5 | 520 | } |
d7f25a9e PM |
521 | |
522 | /** cpu_asidx_from_attrs: | |
523 | * @cpu: CPU | |
524 | * @attrs: memory transaction attributes | |
525 | * | |
526 | * Returns the address space index specifying the CPU AddressSpace | |
527 | * to use for a memory access with the given transaction attributes. | |
528 | */ | |
529 | static inline int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs) | |
530 | { | |
531 | CPUClass *cc = CPU_GET_CLASS(cpu); | |
532 | ||
533 | if (cc->asidx_from_attrs) { | |
534 | return cc->asidx_from_attrs(cpu, attrs); | |
535 | } | |
536 | return 0; | |
537 | } | |
00b941e5 AF |
538 | #endif |
539 | ||
267f685b PB |
540 | /** |
541 | * cpu_list_add: | |
542 | * @cpu: The CPU to be added to the list of CPUs. | |
543 | */ | |
544 | void cpu_list_add(CPUState *cpu); | |
545 | ||
546 | /** | |
547 | * cpu_list_remove: | |
548 | * @cpu: The CPU to be removed from the list of CPUs. | |
549 | */ | |
550 | void cpu_list_remove(CPUState *cpu); | |
551 | ||
dd83b06a AF |
552 | /** |
553 | * cpu_reset: | |
554 | * @cpu: The CPU whose state is to be reset. | |
555 | */ | |
556 | void cpu_reset(CPUState *cpu); | |
557 | ||
2b8c2754 AF |
558 | /** |
559 | * cpu_class_by_name: | |
560 | * @typename: The CPU base type. | |
561 | * @cpu_model: The model string without any parameters. | |
562 | * | |
563 | * Looks up a CPU #ObjectClass matching name @cpu_model. | |
564 | * | |
565 | * Returns: A #CPUClass or %NULL if not matching class is found. | |
566 | */ | |
567 | ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model); | |
568 | ||
9262685b AF |
569 | /** |
570 | * cpu_generic_init: | |
571 | * @typename: The CPU base type. | |
572 | * @cpu_model: The model string including optional parameters. | |
573 | * | |
574 | * Instantiates a CPU, processes optional parameters and realizes the CPU. | |
575 | * | |
576 | * Returns: A #CPUState or %NULL if an error occurred. | |
577 | */ | |
578 | CPUState *cpu_generic_init(const char *typename, const char *cpu_model); | |
579 | ||
3993c6bd | 580 | /** |
8c2e1b00 | 581 | * cpu_has_work: |
3993c6bd AF |
582 | * @cpu: The vCPU to check. |
583 | * | |
584 | * Checks whether the CPU has work to do. | |
585 | * | |
586 | * Returns: %true if the CPU has work, %false otherwise. | |
587 | */ | |
8c2e1b00 AF |
588 | static inline bool cpu_has_work(CPUState *cpu) |
589 | { | |
590 | CPUClass *cc = CPU_GET_CLASS(cpu); | |
591 | ||
592 | g_assert(cc->has_work); | |
593 | return cc->has_work(cpu); | |
594 | } | |
3993c6bd | 595 | |
60e82579 AF |
596 | /** |
597 | * qemu_cpu_is_self: | |
598 | * @cpu: The vCPU to check against. | |
599 | * | |
600 | * Checks whether the caller is executing on the vCPU thread. | |
601 | * | |
602 | * Returns: %true if called from @cpu's thread, %false otherwise. | |
603 | */ | |
604 | bool qemu_cpu_is_self(CPUState *cpu); | |
605 | ||
c08d7424 AF |
606 | /** |
607 | * qemu_cpu_kick: | |
608 | * @cpu: The vCPU to kick. | |
609 | * | |
610 | * Kicks @cpu's thread. | |
611 | */ | |
612 | void qemu_cpu_kick(CPUState *cpu); | |
613 | ||
2fa45344 AF |
614 | /** |
615 | * cpu_is_stopped: | |
616 | * @cpu: The CPU to check. | |
617 | * | |
618 | * Checks whether the CPU is stopped. | |
619 | * | |
620 | * Returns: %true if run state is not running or if artificially stopped; | |
621 | * %false otherwise. | |
622 | */ | |
623 | bool cpu_is_stopped(CPUState *cpu); | |
624 | ||
d148d90e SF |
625 | /** |
626 | * do_run_on_cpu: | |
627 | * @cpu: The vCPU to run on. | |
628 | * @func: The function to be executed. | |
629 | * @data: Data to pass to the function. | |
630 | * @mutex: Mutex to release while waiting for @func to run. | |
631 | * | |
632 | * Used internally in the implementation of run_on_cpu. | |
633 | */ | |
634 | void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data, | |
635 | QemuMutex *mutex); | |
636 | ||
f100f0b3 AF |
637 | /** |
638 | * run_on_cpu: | |
639 | * @cpu: The vCPU to run on. | |
640 | * @func: The function to be executed. | |
641 | * @data: Data to pass to the function. | |
642 | * | |
643 | * Schedules the function @func for execution on the vCPU @cpu. | |
644 | */ | |
e0eeb4a2 | 645 | void run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data); |
f100f0b3 | 646 | |
3c02270d CV |
647 | /** |
648 | * async_run_on_cpu: | |
649 | * @cpu: The vCPU to run on. | |
650 | * @func: The function to be executed. | |
651 | * @data: Data to pass to the function. | |
652 | * | |
653 | * Schedules the function @func for execution on the vCPU @cpu asynchronously. | |
654 | */ | |
e0eeb4a2 | 655 | void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data); |
3c02270d | 656 | |
38d8f5c8 AF |
657 | /** |
658 | * qemu_get_cpu: | |
659 | * @index: The CPUState@cpu_index value of the CPU to obtain. | |
660 | * | |
661 | * Gets a CPU matching @index. | |
662 | * | |
663 | * Returns: The CPU or %NULL if there is no matching CPU. | |
664 | */ | |
665 | CPUState *qemu_get_cpu(int index); | |
666 | ||
69e5ff06 IM |
667 | /** |
668 | * cpu_exists: | |
669 | * @id: Guest-exposed CPU ID to lookup. | |
670 | * | |
671 | * Search for CPU with specified ID. | |
672 | * | |
673 | * Returns: %true - CPU is found, %false - CPU isn't found. | |
674 | */ | |
675 | bool cpu_exists(int64_t id); | |
676 | ||
2adcc85d JH |
677 | /** |
678 | * cpu_throttle_set: | |
679 | * @new_throttle_pct: Percent of sleep time. Valid range is 1 to 99. | |
680 | * | |
681 | * Throttles all vcpus by forcing them to sleep for the given percentage of | |
682 | * time. A throttle_percentage of 25 corresponds to a 75% duty cycle roughly. | |
683 | * (example: 10ms sleep for every 30ms awake). | |
684 | * | |
685 | * cpu_throttle_set can be called as needed to adjust new_throttle_pct. | |
686 | * Once the throttling starts, it will remain in effect until cpu_throttle_stop | |
687 | * is called. | |
688 | */ | |
689 | void cpu_throttle_set(int new_throttle_pct); | |
690 | ||
691 | /** | |
692 | * cpu_throttle_stop: | |
693 | * | |
694 | * Stops the vcpu throttling started by cpu_throttle_set. | |
695 | */ | |
696 | void cpu_throttle_stop(void); | |
697 | ||
698 | /** | |
699 | * cpu_throttle_active: | |
700 | * | |
701 | * Returns: %true if the vcpus are currently being throttled, %false otherwise. | |
702 | */ | |
703 | bool cpu_throttle_active(void); | |
704 | ||
705 | /** | |
706 | * cpu_throttle_get_percentage: | |
707 | * | |
708 | * Returns the vcpu throttle percentage. See cpu_throttle_set for details. | |
709 | * | |
710 | * Returns: The throttle percentage in range 1 to 99. | |
711 | */ | |
712 | int cpu_throttle_get_percentage(void); | |
713 | ||
c3affe56 AF |
714 | #ifndef CONFIG_USER_ONLY |
715 | ||
716 | typedef void (*CPUInterruptHandler)(CPUState *, int); | |
717 | ||
718 | extern CPUInterruptHandler cpu_interrupt_handler; | |
719 | ||
720 | /** | |
721 | * cpu_interrupt: | |
722 | * @cpu: The CPU to set an interrupt on. | |
723 | * @mask: The interupts to set. | |
724 | * | |
725 | * Invokes the interrupt handler. | |
726 | */ | |
727 | static inline void cpu_interrupt(CPUState *cpu, int mask) | |
728 | { | |
729 | cpu_interrupt_handler(cpu, mask); | |
730 | } | |
731 | ||
732 | #else /* USER_ONLY */ | |
733 | ||
734 | void cpu_interrupt(CPUState *cpu, int mask); | |
735 | ||
736 | #endif /* USER_ONLY */ | |
737 | ||
93e22326 | 738 | #ifdef CONFIG_SOFTMMU |
c658b94f AF |
739 | static inline void cpu_unassigned_access(CPUState *cpu, hwaddr addr, |
740 | bool is_write, bool is_exec, | |
741 | int opaque, unsigned size) | |
742 | { | |
743 | CPUClass *cc = CPU_GET_CLASS(cpu); | |
744 | ||
745 | if (cc->do_unassigned_access) { | |
746 | cc->do_unassigned_access(cpu, addr, is_write, is_exec, opaque, size); | |
747 | } | |
748 | } | |
749 | ||
93e22326 | 750 | static inline void cpu_unaligned_access(CPUState *cpu, vaddr addr, |
b35399bb SS |
751 | MMUAccessType access_type, |
752 | int mmu_idx, uintptr_t retaddr) | |
93e22326 PB |
753 | { |
754 | CPUClass *cc = CPU_GET_CLASS(cpu); | |
755 | ||
b35399bb | 756 | cc->do_unaligned_access(cpu, addr, access_type, mmu_idx, retaddr); |
93e22326 | 757 | } |
c658b94f AF |
758 | #endif |
759 | ||
2991b890 PC |
760 | /** |
761 | * cpu_set_pc: | |
762 | * @cpu: The CPU to set the program counter for. | |
763 | * @addr: Program counter value. | |
764 | * | |
765 | * Sets the program counter for a CPU. | |
766 | */ | |
767 | static inline void cpu_set_pc(CPUState *cpu, vaddr addr) | |
768 | { | |
769 | CPUClass *cc = CPU_GET_CLASS(cpu); | |
770 | ||
771 | cc->set_pc(cpu, addr); | |
772 | } | |
773 | ||
d8ed887b AF |
774 | /** |
775 | * cpu_reset_interrupt: | |
776 | * @cpu: The CPU to clear the interrupt on. | |
777 | * @mask: The interrupt mask to clear. | |
778 | * | |
779 | * Resets interrupts on the vCPU @cpu. | |
780 | */ | |
781 | void cpu_reset_interrupt(CPUState *cpu, int mask); | |
782 | ||
60a3e17a AF |
783 | /** |
784 | * cpu_exit: | |
785 | * @cpu: The CPU to exit. | |
786 | * | |
787 | * Requests the CPU @cpu to exit execution. | |
788 | */ | |
789 | void cpu_exit(CPUState *cpu); | |
790 | ||
2993683b IM |
791 | /** |
792 | * cpu_resume: | |
793 | * @cpu: The CPU to resume. | |
794 | * | |
795 | * Resumes CPU, i.e. puts CPU into runnable state. | |
796 | */ | |
797 | void cpu_resume(CPUState *cpu); | |
dd83b06a | 798 | |
4c055ab5 GZ |
799 | /** |
800 | * cpu_remove: | |
801 | * @cpu: The CPU to remove. | |
802 | * | |
803 | * Requests the CPU to be removed. | |
804 | */ | |
805 | void cpu_remove(CPUState *cpu); | |
806 | ||
2c579042 BR |
807 | /** |
808 | * cpu_remove_sync: | |
809 | * @cpu: The CPU to remove. | |
810 | * | |
811 | * Requests the CPU to be removed and waits till it is removed. | |
812 | */ | |
813 | void cpu_remove_sync(CPUState *cpu); | |
814 | ||
d148d90e SF |
815 | /** |
816 | * process_queued_cpu_work() - process all items on CPU work queue | |
817 | * @cpu: The CPU which work queue to process. | |
818 | */ | |
819 | void process_queued_cpu_work(CPUState *cpu); | |
820 | ||
c643bed9 AF |
821 | /** |
822 | * qemu_init_vcpu: | |
823 | * @cpu: The vCPU to initialize. | |
824 | * | |
825 | * Initializes a vCPU. | |
826 | */ | |
827 | void qemu_init_vcpu(CPUState *cpu); | |
828 | ||
3825b28f AF |
829 | #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */ |
830 | #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */ | |
831 | #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */ | |
832 | ||
833 | /** | |
834 | * cpu_single_step: | |
835 | * @cpu: CPU to the flags for. | |
836 | * @enabled: Flags to enable. | |
837 | * | |
838 | * Enables or disables single-stepping for @cpu. | |
839 | */ | |
840 | void cpu_single_step(CPUState *cpu, int enabled); | |
841 | ||
b3310ab3 AF |
842 | /* Breakpoint/watchpoint flags */ |
843 | #define BP_MEM_READ 0x01 | |
844 | #define BP_MEM_WRITE 0x02 | |
845 | #define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE) | |
846 | #define BP_STOP_BEFORE_ACCESS 0x04 | |
08225676 | 847 | /* 0x08 currently unused */ |
b3310ab3 AF |
848 | #define BP_GDB 0x10 |
849 | #define BP_CPU 0x20 | |
b933066a | 850 | #define BP_ANY (BP_GDB | BP_CPU) |
08225676 PM |
851 | #define BP_WATCHPOINT_HIT_READ 0x40 |
852 | #define BP_WATCHPOINT_HIT_WRITE 0x80 | |
853 | #define BP_WATCHPOINT_HIT (BP_WATCHPOINT_HIT_READ | BP_WATCHPOINT_HIT_WRITE) | |
b3310ab3 AF |
854 | |
855 | int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags, | |
856 | CPUBreakpoint **breakpoint); | |
857 | int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags); | |
858 | void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint); | |
859 | void cpu_breakpoint_remove_all(CPUState *cpu, int mask); | |
860 | ||
b933066a RH |
861 | /* Return true if PC matches an installed breakpoint. */ |
862 | static inline bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask) | |
863 | { | |
864 | CPUBreakpoint *bp; | |
865 | ||
866 | if (unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) { | |
867 | QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) { | |
868 | if (bp->pc == pc && (bp->flags & mask)) { | |
869 | return true; | |
870 | } | |
871 | } | |
872 | } | |
873 | return false; | |
874 | } | |
875 | ||
75a34036 AF |
876 | int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len, |
877 | int flags, CPUWatchpoint **watchpoint); | |
878 | int cpu_watchpoint_remove(CPUState *cpu, vaddr addr, | |
879 | vaddr len, int flags); | |
880 | void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint); | |
881 | void cpu_watchpoint_remove_all(CPUState *cpu, int mask); | |
882 | ||
63c91552 PB |
883 | /** |
884 | * cpu_get_address_space: | |
885 | * @cpu: CPU to get address space from | |
886 | * @asidx: index identifying which address space to get | |
887 | * | |
888 | * Return the requested address space of this CPU. @asidx | |
889 | * specifies which address space to read. | |
890 | */ | |
891 | AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx); | |
892 | ||
a47dddd7 AF |
893 | void QEMU_NORETURN cpu_abort(CPUState *cpu, const char *fmt, ...) |
894 | GCC_FMT_ATTR(2, 3); | |
b7bca733 | 895 | void cpu_exec_exit(CPUState *cpu); |
a47dddd7 | 896 | |
1a1562f5 AF |
897 | #ifdef CONFIG_SOFTMMU |
898 | extern const struct VMStateDescription vmstate_cpu_common; | |
899 | #else | |
900 | #define vmstate_cpu_common vmstate_dummy | |
901 | #endif | |
902 | ||
903 | #define VMSTATE_CPU() { \ | |
904 | .name = "parent_obj", \ | |
905 | .size = sizeof(CPUState), \ | |
906 | .vmsd = &vmstate_cpu_common, \ | |
907 | .flags = VMS_STRUCT, \ | |
908 | .offset = 0, \ | |
909 | } | |
910 | ||
a07f953e IM |
911 | #define UNASSIGNED_CPU_INDEX -1 |
912 | ||
dd83b06a | 913 | #endif |