]> git.ipfire.org Git - thirdparty/qemu.git/blob - include/qom/cpu.h
5dfe74a0e70f580a25f7f7aa1616238aec1d38db
[thirdparty/qemu.git] / include / qom / cpu.h
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
23 #include "hw/qdev-core.h"
24 #include "disas/bfd.h"
25 #include "exec/hwaddr.h"
26 #include "exec/memattrs.h"
27 #include "qemu/bitmap.h"
28 #include "qemu/queue.h"
29 #include "qemu/thread.h"
30 #include "trace/generated-events.h"
31
32 typedef int (*WriteCoreDumpFunction)(const void *buf, size_t size,
33 void *opaque);
34
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
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
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
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
65 typedef enum MMUAccessType {
66 MMU_DATA_LOAD = 0,
67 MMU_DATA_STORE = 1,
68 MMU_INST_FETCH = 2
69 } MMUAccessType;
70
71 typedef struct CPUWatchpoint CPUWatchpoint;
72
73 typedef void (*CPUUnassignedAccess)(CPUState *cpu, hwaddr addr,
74 bool is_write, bool is_exec, int opaque,
75 unsigned size);
76
77 struct TranslationBlock;
78
79 /**
80 * CPUClass:
81 * @class_by_name: Callback to map -cpu command line model name to an
82 * instantiatable CPU type.
83 * @parse_features: Callback to parse command line arguments.
84 * @reset: Callback to reset the #CPUState to its initial state.
85 * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
86 * @has_work: Callback for checking if there is work to do.
87 * @do_interrupt: Callback for interrupt handling.
88 * @do_unassigned_access: Callback for unassigned access handling.
89 * @do_unaligned_access: Callback for unaligned access handling, if
90 * the target defines #ALIGNED_ONLY.
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.
95 * @memory_rw_debug: Callback for GDB memory access.
96 * @dump_state: Callback for dumping state.
97 * @dump_statistics: Callback for dumping statistics.
98 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
99 * @get_paging_enabled: Callback for inquiring whether paging is enabled.
100 * @get_memory_mapping: Callback for obtaining the memory mappings.
101 * @set_pc: Callback for setting the Program Counter register.
102 * @synchronize_from_tb: Callback for synchronizing state from a TCG
103 * #TranslationBlock.
104 * @handle_mmu_fault: Callback for handling an MMU fault.
105 * @get_phys_page_debug: Callback for obtaining a physical address.
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.
110 * @asidx_from_attrs: Callback to return the CPU AddressSpace to use for
111 * a memory access with the specified memory transaction attributes.
112 * @gdb_read_register: Callback for letting GDB read a register.
113 * @gdb_write_register: Callback for letting GDB write a register.
114 * @debug_check_watchpoint: Callback: return true if the architectural
115 * watchpoint whose address has matched should really fire.
116 * @debug_excp_handler: Callback for handling debug exceptions.
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.
125 * @vmsd: State description for migration.
126 * @gdb_num_core_regs: Number of core registers accessible to GDB.
127 * @gdb_core_xml_file: File name for core registers GDB XML description.
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.
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.
132 * @cpu_exec_enter: Callback for cpu_exec preparation.
133 * @cpu_exec_exit: Callback for cpu_exec cleanup.
134 * @cpu_exec_interrupt: Callback for processing interrupts in cpu_exec.
135 * @disas_set_info: Setup architecture specific components of disassembly info
136 *
137 * Represents a CPU family or model.
138 */
139 typedef struct CPUClass {
140 /*< private >*/
141 DeviceClass parent_class;
142 /*< public >*/
143
144 ObjectClass *(*class_by_name)(const char *cpu_model);
145 void (*parse_features)(const char *typename, char *str, Error **errp);
146
147 void (*reset)(CPUState *cpu);
148 int reset_dump_flags;
149 bool (*has_work)(CPUState *cpu);
150 void (*do_interrupt)(CPUState *cpu);
151 CPUUnassignedAccess do_unassigned_access;
152 void (*do_unaligned_access)(CPUState *cpu, vaddr addr,
153 MMUAccessType access_type,
154 int mmu_idx, uintptr_t retaddr);
155 bool (*virtio_is_big_endian)(CPUState *cpu);
156 int (*memory_rw_debug)(CPUState *cpu, vaddr addr,
157 uint8_t *buf, int len, bool is_write);
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);
162 int64_t (*get_arch_id)(CPUState *cpu);
163 bool (*get_paging_enabled)(const CPUState *cpu);
164 void (*get_memory_mapping)(CPUState *cpu, MemoryMappingList *list,
165 Error **errp);
166 void (*set_pc)(CPUState *cpu, vaddr value);
167 void (*synchronize_from_tb)(CPUState *cpu, struct TranslationBlock *tb);
168 int (*handle_mmu_fault)(CPUState *cpu, vaddr address, int rw,
169 int mmu_index);
170 hwaddr (*get_phys_page_debug)(CPUState *cpu, vaddr addr);
171 hwaddr (*get_phys_page_attrs_debug)(CPUState *cpu, vaddr addr,
172 MemTxAttrs *attrs);
173 int (*asidx_from_attrs)(CPUState *cpu, MemTxAttrs attrs);
174 int (*gdb_read_register)(CPUState *cpu, uint8_t *buf, int reg);
175 int (*gdb_write_register)(CPUState *cpu, uint8_t *buf, int reg);
176 bool (*debug_check_watchpoint)(CPUState *cpu, CPUWatchpoint *wp);
177 void (*debug_excp_handler)(CPUState *cpu);
178
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);
187
188 const struct VMStateDescription *vmsd;
189 int gdb_num_core_regs;
190 const char *gdb_core_xml_file;
191 gchar * (*gdb_arch_name)(CPUState *cpu);
192 bool gdb_stop_before_watchpoint;
193
194 void (*cpu_exec_enter)(CPUState *cpu);
195 void (*cpu_exec_exit)(CPUState *cpu);
196 bool (*cpu_exec_interrupt)(CPUState *cpu, int interrupt_request);
197
198 void (*disas_set_info)(CPUState *cpu, disassemble_info *info);
199 } CPUClass;
200
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
213 typedef struct CPUBreakpoint {
214 vaddr pc;
215 int flags; /* BP_* */
216 QTAILQ_ENTRY(CPUBreakpoint) entry;
217 } CPUBreakpoint;
218
219 struct CPUWatchpoint {
220 vaddr vaddr;
221 vaddr len;
222 vaddr hitaddr;
223 MemTxAttrs hitattrs;
224 int flags; /* BP_* */
225 QTAILQ_ENTRY(CPUWatchpoint) entry;
226 };
227
228 struct KVMState;
229 struct kvm_run;
230
231 #define TB_JMP_CACHE_BITS 12
232 #define TB_JMP_CACHE_SIZE (1 << TB_JMP_CACHE_BITS)
233
234 /* work queue */
235 typedef void (*run_on_cpu_func)(CPUState *cpu, void *data);
236 struct qemu_work_item;
237
238 /**
239 * CPUState:
240 * @cpu_index: CPU index (informative).
241 * @nr_cores: Number of cores within this CPU package.
242 * @nr_threads: Number of threads within this CPU.
243 * @numa_node: NUMA node this CPU is belonging to.
244 * @host_tid: Host thread ID.
245 * @running: #true if CPU is currently running;
246 * valid under cpu_list_lock.
247 * @created: Indicates whether the CPU thread has been successfully created.
248 * @interrupt_request: Indicates a pending interrupt request.
249 * @halted: Nonzero if the CPU is in suspended state.
250 * @stop: Indicates a pending stop request.
251 * @stopped: Indicates the CPU has been artificially stopped.
252 * @unplug: Indicates a pending CPU unplug request.
253 * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU
254 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
255 * CPU and return to its top level loop.
256 * @singlestep_enabled: Flags for single-stepping.
257 * @icount_extra: Instructions until next timer event.
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.
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.
264 * @cpu_ases: Pointer to array of CPUAddressSpaces (which define the
265 * AddressSpaces this CPU has)
266 * @num_ases: number of CPUAddressSpaces in @cpu_ases
267 * @as: Pointer to the first AddressSpace, for the convenience of targets which
268 * only have a single AddressSpace
269 * @env_ptr: Pointer to subclass-specific CPUArchState field.
270 * @gdb_regs: Additional GDB registers.
271 * @gdb_num_regs: Number of total registers accessible to GDB.
272 * @gdb_num_g_regs: Number of registers in GDB 'g' packets.
273 * @next_cpu: Next CPU sharing TB cache.
274 * @opaque: User data.
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.
277 * @kvm_fd: vCPU file descriptor for KVM.
278 * @work_mutex: Lock to prevent multiple access to queued_work_*.
279 * @queued_work_first: First asynchronous work pending.
280 * @trace_dstate: Dynamic tracing state of events for this vCPU (bitmask).
281 *
282 * State of one CPU core or thread.
283 */
284 struct CPUState {
285 /*< private >*/
286 DeviceState parent_obj;
287 /*< public >*/
288
289 int nr_cores;
290 int nr_threads;
291 int numa_node;
292
293 struct QemuThread *thread;
294 #ifdef _WIN32
295 HANDLE hThread;
296 #endif
297 int thread_id;
298 uint32_t host_tid;
299 bool running;
300 struct QemuCond *halt_cond;
301 bool thread_kicked;
302 bool created;
303 bool stop;
304 bool stopped;
305 bool unplug;
306 bool crash_occurred;
307 bool exit_request;
308 uint32_t interrupt_request;
309 int singlestep_enabled;
310 int64_t icount_extra;
311 sigjmp_buf jmp_env;
312
313 QemuMutex work_mutex;
314 struct qemu_work_item *queued_work_first, *queued_work_last;
315
316 CPUAddressSpace *cpu_ases;
317 int num_ases;
318 AddressSpace *as;
319 MemoryRegion *memory;
320
321 void *env_ptr; /* CPUArchState */
322 struct TranslationBlock *tb_jmp_cache[TB_JMP_CACHE_SIZE];
323 struct GDBRegisterState *gdb_regs;
324 int gdb_num_regs;
325 int gdb_num_g_regs;
326 QTAILQ_ENTRY(CPUState) node;
327
328 /* ice debug support */
329 QTAILQ_HEAD(breakpoints_head, CPUBreakpoint) breakpoints;
330
331 QTAILQ_HEAD(watchpoints_head, CPUWatchpoint) watchpoints;
332 CPUWatchpoint *watchpoint_hit;
333
334 void *opaque;
335
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
342 int kvm_fd;
343 bool kvm_vcpu_dirty;
344 struct KVMState *kvm_state;
345 struct kvm_run *kvm_run;
346
347 /* Used for events with 'vcpu' and *without* the 'disabled' properties */
348 DECLARE_BITMAP(trace_dstate, TRACE_VCPU_EVENT_COUNT);
349
350 /* TODO Move common fields from CPUArchState here. */
351 int cpu_index; /* used by alpha TCG */
352 uint32_t halted; /* used by alpha, cris, ppc TCG */
353 union {
354 uint32_t u32;
355 icount_decr_u16 u16;
356 } icount_decr;
357 uint32_t can_do_io;
358 int32_t exception_index; /* used by m68k TCG */
359
360 /* Used to keep track of an outstanding cpu throttle thread for migration
361 * autoconverge
362 */
363 bool throttle_thread_scheduled;
364
365 /* Note that this is accessed at the start of every TB via a negative
366 offset from AREG0. Leave this field at the end so as to make the
367 (absolute value) offset as small as possible. This reduces code
368 size, especially for hosts without large memory offsets. */
369 uint32_t tcg_exit_req;
370 };
371
372 QTAILQ_HEAD(CPUTailQ, CPUState);
373 extern struct CPUTailQ cpus;
374 #define CPU_NEXT(cpu) QTAILQ_NEXT(cpu, node)
375 #define CPU_FOREACH(cpu) QTAILQ_FOREACH(cpu, &cpus, node)
376 #define CPU_FOREACH_SAFE(cpu, next_cpu) \
377 QTAILQ_FOREACH_SAFE(cpu, &cpus, node, next_cpu)
378 #define CPU_FOREACH_REVERSE(cpu) \
379 QTAILQ_FOREACH_REVERSE(cpu, &cpus, CPUTailQ, node)
380 #define first_cpu QTAILQ_FIRST(&cpus)
381
382 extern __thread CPUState *current_cpu;
383
384 /**
385 * cpu_paging_enabled:
386 * @cpu: The CPU whose state is to be inspected.
387 *
388 * Returns: %true if paging is enabled, %false otherwise.
389 */
390 bool cpu_paging_enabled(const CPUState *cpu);
391
392 /**
393 * cpu_get_memory_mapping:
394 * @cpu: The CPU whose memory mappings are to be obtained.
395 * @list: Where to write the memory mappings to.
396 * @errp: Pointer for reporting an #Error.
397 */
398 void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
399 Error **errp);
400
401 /**
402 * cpu_write_elf64_note:
403 * @f: pointer to a function that writes memory to a file
404 * @cpu: The CPU whose memory is to be dumped
405 * @cpuid: ID number of the CPU
406 * @opaque: pointer to the CPUState struct
407 */
408 int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
409 int cpuid, void *opaque);
410
411 /**
412 * cpu_write_elf64_qemunote:
413 * @f: pointer to a function that writes memory to a file
414 * @cpu: The CPU whose memory is to be dumped
415 * @cpuid: ID number of the CPU
416 * @opaque: pointer to the CPUState struct
417 */
418 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
419 void *opaque);
420
421 /**
422 * cpu_write_elf32_note:
423 * @f: pointer to a function that writes memory to a file
424 * @cpu: The CPU whose memory is to be dumped
425 * @cpuid: ID number of the CPU
426 * @opaque: pointer to the CPUState struct
427 */
428 int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
429 int cpuid, void *opaque);
430
431 /**
432 * cpu_write_elf32_qemunote:
433 * @f: pointer to a function that writes memory to a file
434 * @cpu: The CPU whose memory is to be dumped
435 * @cpuid: ID number of the CPU
436 * @opaque: pointer to the CPUState struct
437 */
438 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
439 void *opaque);
440
441 /**
442 * CPUDumpFlags:
443 * @CPU_DUMP_CODE:
444 * @CPU_DUMP_FPU: dump FPU register state, not just integer
445 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
446 */
447 enum CPUDumpFlags {
448 CPU_DUMP_CODE = 0x00010000,
449 CPU_DUMP_FPU = 0x00020000,
450 CPU_DUMP_CCOP = 0x00040000,
451 };
452
453 /**
454 * cpu_dump_state:
455 * @cpu: The CPU whose state is to be dumped.
456 * @f: File to dump to.
457 * @cpu_fprintf: Function to dump with.
458 * @flags: Flags what to dump.
459 *
460 * Dumps CPU state.
461 */
462 void cpu_dump_state(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
463 int flags);
464
465 /**
466 * cpu_dump_statistics:
467 * @cpu: The CPU whose state is to be dumped.
468 * @f: File to dump to.
469 * @cpu_fprintf: Function to dump with.
470 * @flags: Flags what to dump.
471 *
472 * Dumps CPU statistics.
473 */
474 void cpu_dump_statistics(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
475 int flags);
476
477 #ifndef CONFIG_USER_ONLY
478 /**
479 * cpu_get_phys_page_attrs_debug:
480 * @cpu: The CPU to obtain the physical page address for.
481 * @addr: The virtual address.
482 * @attrs: Updated on return with the memory transaction attributes to use
483 * for this access.
484 *
485 * Obtains the physical page corresponding to a virtual one, together
486 * with the corresponding memory transaction attributes to use for the access.
487 * Use it only for debugging because no protection checks are done.
488 *
489 * Returns: Corresponding physical page address or -1 if no page found.
490 */
491 static inline hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr,
492 MemTxAttrs *attrs)
493 {
494 CPUClass *cc = CPU_GET_CLASS(cpu);
495
496 if (cc->get_phys_page_attrs_debug) {
497 return cc->get_phys_page_attrs_debug(cpu, addr, attrs);
498 }
499 /* Fallback for CPUs which don't implement the _attrs_ hook */
500 *attrs = MEMTXATTRS_UNSPECIFIED;
501 return cc->get_phys_page_debug(cpu, addr);
502 }
503
504 /**
505 * cpu_get_phys_page_debug:
506 * @cpu: The CPU to obtain the physical page address for.
507 * @addr: The virtual address.
508 *
509 * Obtains the physical page corresponding to a virtual one.
510 * Use it only for debugging because no protection checks are done.
511 *
512 * Returns: Corresponding physical page address or -1 if no page found.
513 */
514 static inline hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr)
515 {
516 MemTxAttrs attrs = {};
517
518 return cpu_get_phys_page_attrs_debug(cpu, addr, &attrs);
519 }
520
521 /** cpu_asidx_from_attrs:
522 * @cpu: CPU
523 * @attrs: memory transaction attributes
524 *
525 * Returns the address space index specifying the CPU AddressSpace
526 * to use for a memory access with the given transaction attributes.
527 */
528 static inline int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs)
529 {
530 CPUClass *cc = CPU_GET_CLASS(cpu);
531
532 if (cc->asidx_from_attrs) {
533 return cc->asidx_from_attrs(cpu, attrs);
534 }
535 return 0;
536 }
537 #endif
538
539 /**
540 * cpu_list_add:
541 * @cpu: The CPU to be added to the list of CPUs.
542 */
543 void cpu_list_add(CPUState *cpu);
544
545 /**
546 * cpu_list_remove:
547 * @cpu: The CPU to be removed from the list of CPUs.
548 */
549 void cpu_list_remove(CPUState *cpu);
550
551 /**
552 * cpu_reset:
553 * @cpu: The CPU whose state is to be reset.
554 */
555 void cpu_reset(CPUState *cpu);
556
557 /**
558 * cpu_class_by_name:
559 * @typename: The CPU base type.
560 * @cpu_model: The model string without any parameters.
561 *
562 * Looks up a CPU #ObjectClass matching name @cpu_model.
563 *
564 * Returns: A #CPUClass or %NULL if not matching class is found.
565 */
566 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
567
568 /**
569 * cpu_generic_init:
570 * @typename: The CPU base type.
571 * @cpu_model: The model string including optional parameters.
572 *
573 * Instantiates a CPU, processes optional parameters and realizes the CPU.
574 *
575 * Returns: A #CPUState or %NULL if an error occurred.
576 */
577 CPUState *cpu_generic_init(const char *typename, const char *cpu_model);
578
579 /**
580 * cpu_has_work:
581 * @cpu: The vCPU to check.
582 *
583 * Checks whether the CPU has work to do.
584 *
585 * Returns: %true if the CPU has work, %false otherwise.
586 */
587 static inline bool cpu_has_work(CPUState *cpu)
588 {
589 CPUClass *cc = CPU_GET_CLASS(cpu);
590
591 g_assert(cc->has_work);
592 return cc->has_work(cpu);
593 }
594
595 /**
596 * qemu_cpu_is_self:
597 * @cpu: The vCPU to check against.
598 *
599 * Checks whether the caller is executing on the vCPU thread.
600 *
601 * Returns: %true if called from @cpu's thread, %false otherwise.
602 */
603 bool qemu_cpu_is_self(CPUState *cpu);
604
605 /**
606 * qemu_cpu_kick:
607 * @cpu: The vCPU to kick.
608 *
609 * Kicks @cpu's thread.
610 */
611 void qemu_cpu_kick(CPUState *cpu);
612
613 /**
614 * cpu_is_stopped:
615 * @cpu: The CPU to check.
616 *
617 * Checks whether the CPU is stopped.
618 *
619 * Returns: %true if run state is not running or if artificially stopped;
620 * %false otherwise.
621 */
622 bool cpu_is_stopped(CPUState *cpu);
623
624 /**
625 * do_run_on_cpu:
626 * @cpu: The vCPU to run on.
627 * @func: The function to be executed.
628 * @data: Data to pass to the function.
629 * @mutex: Mutex to release while waiting for @func to run.
630 *
631 * Used internally in the implementation of run_on_cpu.
632 */
633 void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data,
634 QemuMutex *mutex);
635
636 /**
637 * run_on_cpu:
638 * @cpu: The vCPU to run on.
639 * @func: The function to be executed.
640 * @data: Data to pass to the function.
641 *
642 * Schedules the function @func for execution on the vCPU @cpu.
643 */
644 void run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data);
645
646 /**
647 * async_run_on_cpu:
648 * @cpu: The vCPU to run on.
649 * @func: The function to be executed.
650 * @data: Data to pass to the function.
651 *
652 * Schedules the function @func for execution on the vCPU @cpu asynchronously.
653 */
654 void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data);
655
656 /**
657 * async_safe_run_on_cpu:
658 * @cpu: The vCPU to run on.
659 * @func: The function to be executed.
660 * @data: Data to pass to the function.
661 *
662 * Schedules the function @func for execution on the vCPU @cpu asynchronously,
663 * while all other vCPUs are sleeping.
664 *
665 * Unlike run_on_cpu and async_run_on_cpu, the function is run outside the
666 * BQL.
667 */
668 void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data);
669
670 /**
671 * qemu_get_cpu:
672 * @index: The CPUState@cpu_index value of the CPU to obtain.
673 *
674 * Gets a CPU matching @index.
675 *
676 * Returns: The CPU or %NULL if there is no matching CPU.
677 */
678 CPUState *qemu_get_cpu(int index);
679
680 /**
681 * cpu_exists:
682 * @id: Guest-exposed CPU ID to lookup.
683 *
684 * Search for CPU with specified ID.
685 *
686 * Returns: %true - CPU is found, %false - CPU isn't found.
687 */
688 bool cpu_exists(int64_t id);
689
690 /**
691 * cpu_throttle_set:
692 * @new_throttle_pct: Percent of sleep time. Valid range is 1 to 99.
693 *
694 * Throttles all vcpus by forcing them to sleep for the given percentage of
695 * time. A throttle_percentage of 25 corresponds to a 75% duty cycle roughly.
696 * (example: 10ms sleep for every 30ms awake).
697 *
698 * cpu_throttle_set can be called as needed to adjust new_throttle_pct.
699 * Once the throttling starts, it will remain in effect until cpu_throttle_stop
700 * is called.
701 */
702 void cpu_throttle_set(int new_throttle_pct);
703
704 /**
705 * cpu_throttle_stop:
706 *
707 * Stops the vcpu throttling started by cpu_throttle_set.
708 */
709 void cpu_throttle_stop(void);
710
711 /**
712 * cpu_throttle_active:
713 *
714 * Returns: %true if the vcpus are currently being throttled, %false otherwise.
715 */
716 bool cpu_throttle_active(void);
717
718 /**
719 * cpu_throttle_get_percentage:
720 *
721 * Returns the vcpu throttle percentage. See cpu_throttle_set for details.
722 *
723 * Returns: The throttle percentage in range 1 to 99.
724 */
725 int cpu_throttle_get_percentage(void);
726
727 #ifndef CONFIG_USER_ONLY
728
729 typedef void (*CPUInterruptHandler)(CPUState *, int);
730
731 extern CPUInterruptHandler cpu_interrupt_handler;
732
733 /**
734 * cpu_interrupt:
735 * @cpu: The CPU to set an interrupt on.
736 * @mask: The interupts to set.
737 *
738 * Invokes the interrupt handler.
739 */
740 static inline void cpu_interrupt(CPUState *cpu, int mask)
741 {
742 cpu_interrupt_handler(cpu, mask);
743 }
744
745 #else /* USER_ONLY */
746
747 void cpu_interrupt(CPUState *cpu, int mask);
748
749 #endif /* USER_ONLY */
750
751 #ifdef CONFIG_SOFTMMU
752 static inline void cpu_unassigned_access(CPUState *cpu, hwaddr addr,
753 bool is_write, bool is_exec,
754 int opaque, unsigned size)
755 {
756 CPUClass *cc = CPU_GET_CLASS(cpu);
757
758 if (cc->do_unassigned_access) {
759 cc->do_unassigned_access(cpu, addr, is_write, is_exec, opaque, size);
760 }
761 }
762
763 static inline void cpu_unaligned_access(CPUState *cpu, vaddr addr,
764 MMUAccessType access_type,
765 int mmu_idx, uintptr_t retaddr)
766 {
767 CPUClass *cc = CPU_GET_CLASS(cpu);
768
769 cc->do_unaligned_access(cpu, addr, access_type, mmu_idx, retaddr);
770 }
771 #endif
772
773 /**
774 * cpu_set_pc:
775 * @cpu: The CPU to set the program counter for.
776 * @addr: Program counter value.
777 *
778 * Sets the program counter for a CPU.
779 */
780 static inline void cpu_set_pc(CPUState *cpu, vaddr addr)
781 {
782 CPUClass *cc = CPU_GET_CLASS(cpu);
783
784 cc->set_pc(cpu, addr);
785 }
786
787 /**
788 * cpu_reset_interrupt:
789 * @cpu: The CPU to clear the interrupt on.
790 * @mask: The interrupt mask to clear.
791 *
792 * Resets interrupts on the vCPU @cpu.
793 */
794 void cpu_reset_interrupt(CPUState *cpu, int mask);
795
796 /**
797 * cpu_exit:
798 * @cpu: The CPU to exit.
799 *
800 * Requests the CPU @cpu to exit execution.
801 */
802 void cpu_exit(CPUState *cpu);
803
804 /**
805 * cpu_resume:
806 * @cpu: The CPU to resume.
807 *
808 * Resumes CPU, i.e. puts CPU into runnable state.
809 */
810 void cpu_resume(CPUState *cpu);
811
812 /**
813 * cpu_remove:
814 * @cpu: The CPU to remove.
815 *
816 * Requests the CPU to be removed.
817 */
818 void cpu_remove(CPUState *cpu);
819
820 /**
821 * cpu_remove_sync:
822 * @cpu: The CPU to remove.
823 *
824 * Requests the CPU to be removed and waits till it is removed.
825 */
826 void cpu_remove_sync(CPUState *cpu);
827
828 /**
829 * process_queued_cpu_work() - process all items on CPU work queue
830 * @cpu: The CPU which work queue to process.
831 */
832 void process_queued_cpu_work(CPUState *cpu);
833
834 /**
835 * cpu_exec_start:
836 * @cpu: The CPU for the current thread.
837 *
838 * Record that a CPU has started execution and can be interrupted with
839 * cpu_exit.
840 */
841 void cpu_exec_start(CPUState *cpu);
842
843 /**
844 * cpu_exec_end:
845 * @cpu: The CPU for the current thread.
846 *
847 * Record that a CPU has stopped execution and exclusive sections
848 * can be executed without interrupting it.
849 */
850 void cpu_exec_end(CPUState *cpu);
851
852 /**
853 * start_exclusive:
854 *
855 * Wait for a concurrent exclusive section to end, and then start
856 * a section of work that is run while other CPUs are not running
857 * between cpu_exec_start and cpu_exec_end. CPUs that are running
858 * cpu_exec are exited immediately. CPUs that call cpu_exec_start
859 * during the exclusive section go to sleep until this CPU calls
860 * end_exclusive.
861 */
862 void start_exclusive(void);
863
864 /**
865 * end_exclusive:
866 *
867 * Concludes an exclusive execution section started by start_exclusive.
868 */
869 void end_exclusive(void);
870
871 /**
872 * qemu_init_vcpu:
873 * @cpu: The vCPU to initialize.
874 *
875 * Initializes a vCPU.
876 */
877 void qemu_init_vcpu(CPUState *cpu);
878
879 #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
880 #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
881 #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
882
883 /**
884 * cpu_single_step:
885 * @cpu: CPU to the flags for.
886 * @enabled: Flags to enable.
887 *
888 * Enables or disables single-stepping for @cpu.
889 */
890 void cpu_single_step(CPUState *cpu, int enabled);
891
892 /* Breakpoint/watchpoint flags */
893 #define BP_MEM_READ 0x01
894 #define BP_MEM_WRITE 0x02
895 #define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE)
896 #define BP_STOP_BEFORE_ACCESS 0x04
897 /* 0x08 currently unused */
898 #define BP_GDB 0x10
899 #define BP_CPU 0x20
900 #define BP_ANY (BP_GDB | BP_CPU)
901 #define BP_WATCHPOINT_HIT_READ 0x40
902 #define BP_WATCHPOINT_HIT_WRITE 0x80
903 #define BP_WATCHPOINT_HIT (BP_WATCHPOINT_HIT_READ | BP_WATCHPOINT_HIT_WRITE)
904
905 int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
906 CPUBreakpoint **breakpoint);
907 int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags);
908 void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint);
909 void cpu_breakpoint_remove_all(CPUState *cpu, int mask);
910
911 /* Return true if PC matches an installed breakpoint. */
912 static inline bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask)
913 {
914 CPUBreakpoint *bp;
915
916 if (unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) {
917 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
918 if (bp->pc == pc && (bp->flags & mask)) {
919 return true;
920 }
921 }
922 }
923 return false;
924 }
925
926 int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
927 int flags, CPUWatchpoint **watchpoint);
928 int cpu_watchpoint_remove(CPUState *cpu, vaddr addr,
929 vaddr len, int flags);
930 void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint);
931 void cpu_watchpoint_remove_all(CPUState *cpu, int mask);
932
933 /**
934 * cpu_get_address_space:
935 * @cpu: CPU to get address space from
936 * @asidx: index identifying which address space to get
937 *
938 * Return the requested address space of this CPU. @asidx
939 * specifies which address space to read.
940 */
941 AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx);
942
943 void QEMU_NORETURN cpu_abort(CPUState *cpu, const char *fmt, ...)
944 GCC_FMT_ATTR(2, 3);
945 void cpu_exec_exit(CPUState *cpu);
946
947 #ifdef CONFIG_SOFTMMU
948 extern const struct VMStateDescription vmstate_cpu_common;
949 #else
950 #define vmstate_cpu_common vmstate_dummy
951 #endif
952
953 #define VMSTATE_CPU() { \
954 .name = "parent_obj", \
955 .size = sizeof(CPUState), \
956 .vmsd = &vmstate_cpu_common, \
957 .flags = VMS_STRUCT, \
958 .offset = 0, \
959 }
960
961 #define UNASSIGNED_CPU_INDEX -1
962
963 #endif