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/dis-asm.h"
25 #include "exec/breakpoint.h"
26 #include "exec/hwaddr.h"
27 #include "exec/vaddr.h"
28 #include "exec/memattrs.h"
29 #include "exec/mmu-access-type.h"
30 #include "exec/tlb-common.h"
31 #include "qapi/qapi-types-machine.h"
32 #include "qapi/qapi-types-run-state.h"
33 #include "qemu/bitmap.h"
34 #include "qemu/rcu_queue.h"
35 #include "qemu/queue.h"
36 #include "qemu/lockcnt.h"
37 #include "qemu/thread.h"
38 #include "qom/object.h"
39
40 typedef int (*WriteCoreDumpFunction)(const void *buf, size_t size,
41 void *opaque);
42
43 /**
44 * SECTION:cpu
45 * @section_id: QEMU-cpu
46 * @title: CPU Class
47 * @short_description: Base class for all CPUs
48 */
49
50 #define TYPE_CPU "cpu"
51
52 /* Since this macro is used a lot in hot code paths and in conjunction with
53 * FooCPU *foo_env_get_cpu(), we deviate from usual QOM practice by using
54 * an unchecked cast.
55 */
56 #define CPU(obj) ((CPUState *)(obj))
57
58 /*
59 * The class checkers bring in CPU_GET_CLASS() which is potentially
60 * expensive given the eventual call to
61 * object_class_dynamic_cast_assert(). Because of this the CPUState
62 * has a cached value for the class in cs->cc which is set up in
63 * cpu_exec_realizefn() for use in hot code paths.
64 */
65 typedef struct CPUClass CPUClass;
66 DECLARE_CLASS_CHECKERS(CPUClass, CPU,
67 TYPE_CPU)
68
69 /**
70 * OBJECT_DECLARE_CPU_TYPE:
71 * @CpuInstanceType: instance struct name
72 * @CpuClassType: class struct name
73 * @CPU_MODULE_OBJ_NAME: the CPU name in uppercase with underscore separators
74 *
75 * This macro is typically used in "cpu-qom.h" header file, and will:
76 *
77 * - create the typedefs for the CPU object and class structs
78 * - register the type for use with g_autoptr
79 * - provide three standard type cast functions
80 *
81 * The object struct and class struct need to be declared manually.
82 */
83 #define OBJECT_DECLARE_CPU_TYPE(CpuInstanceType, CpuClassType, CPU_MODULE_OBJ_NAME) \
84 typedef struct ArchCPU CpuInstanceType; \
85 OBJECT_DECLARE_TYPE(ArchCPU, CpuClassType, CPU_MODULE_OBJ_NAME);
86
87 typedef struct CPUWatchpoint CPUWatchpoint;
88
89 /* see physmem.c */
90 struct CPUAddressSpace;
91
92 /* see accel/tcg/tb-jmp-cache.h */
93 struct CPUJumpCache;
94
95 /* see accel-cpu.h */
96 struct AccelCPUClass;
97
98 /* see sysemu-cpu-ops.h */
99 struct SysemuCPUOps;
100
101 /**
102 * CPUClass:
103 * @class_by_name: Callback to map -cpu command line model name to an
104 * instantiatable CPU type.
105 * @list_cpus: list available CPU models and flags.
106 * @parse_features: Callback to parse command line arguments.
107 * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
108 * @memory_rw_debug: Callback for GDB memory access.
109 * @dump_state: Callback for dumping state.
110 * @query_cpu_fast:
111 * Fill in target specific information for the "query-cpus-fast"
112 * QAPI call.
113 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
114 * @set_pc: Callback for setting the Program Counter register. This
115 * should have the semantics used by the target architecture when
116 * setting the PC from a source such as an ELF file entry point;
117 * for example on Arm it will also set the Thumb mode bit based
118 * on the least significant bit of the new PC value.
119 * If the target behaviour here is anything other than "set
120 * the PC register to the value passed in" then the target must
121 * also implement the synchronize_from_tb hook.
122 * @get_pc: Callback for getting the Program Counter register.
123 * As above, with the semantics of the target architecture.
124 * @gdb_read_register: Callback for letting GDB read a register.
125 * No more than @gdb_num_core_regs registers can be read.
126 * @gdb_write_register: Callback for letting GDB write a register.
127 * No more than @gdb_num_core_regs registers can be written.
128 * @gdb_adjust_breakpoint: Callback for adjusting the address of a
129 * breakpoint. Used by AVR to handle a gdb mis-feature with
130 * its Harvard architecture split code and data.
131 * @gdb_num_core_regs: Number of core registers accessible to GDB or 0 to infer
132 * from @gdb_core_xml_file.
133 * @gdb_core_xml_file: File name for core registers GDB XML description.
134 * @gdb_get_core_xml_file: Optional callback that returns the file name for
135 * the core registers GDB XML description. The returned value is expected to
136 * be a simple constant string: the caller will not g_free() it. If this
137 * is NULL then @gdb_core_xml_file will be used instead.
138 * @gdb_stop_before_watchpoint: Indicates whether GDB expects the CPU to stop
139 * before the insn which triggers a watchpoint rather than after it.
140 * @gdb_arch_name: Optional callback that returns the architecture name known
141 * to GDB. The returned value is expected to be a simple constant string:
142 * the caller will not g_free() it.
143 * @disas_set_info: Setup architecture specific components of disassembly info
144 * @adjust_watchpoint_address: Perform a target-specific adjustment to an
145 * address before attempting to match it against watchpoints.
146 * @deprecation_note: If this CPUClass is deprecated, this field provides
147 * related information.
148 *
149 * Represents a CPU family or model.
150 */
151 struct CPUClass {
152 /*< private >*/
153 DeviceClass parent_class;
154 /*< public >*/
155
156 ObjectClass *(*class_by_name)(const char *cpu_model);
157 void (*list_cpus)(void);
158 void (*parse_features)(const char *typename, char *str, Error **errp);
159
160 int (*memory_rw_debug)(CPUState *cpu, vaddr addr,
161 uint8_t *buf, size_t len, bool is_write);
162 void (*dump_state)(CPUState *cpu, FILE *, int flags);
163 void (*query_cpu_fast)(CPUState *cpu, CpuInfoFast *value);
164 int64_t (*get_arch_id)(CPUState *cpu);
165 void (*set_pc)(CPUState *cpu, vaddr value);
166 vaddr (*get_pc)(CPUState *cpu);
167 int (*gdb_read_register)(CPUState *cpu, GByteArray *buf, int reg);
168 int (*gdb_write_register)(CPUState *cpu, uint8_t *buf, int reg);
169 vaddr (*gdb_adjust_breakpoint)(CPUState *cpu, vaddr addr);
170
171 const char *gdb_core_xml_file;
172 const gchar * (*gdb_arch_name)(CPUState *cpu);
173 const char * (*gdb_get_core_xml_file)(CPUState *cpu);
174
175 void (*disas_set_info)(CPUState *cpu, disassemble_info *info);
176
177 const char *deprecation_note;
178 struct AccelCPUClass *accel_cpu;
179
180 /* when system emulation is not available, this pointer is NULL */
181 const struct SysemuCPUOps *sysemu_ops;
182
183 /* when TCG is not available, this pointer is NULL */
184 const TCGCPUOps *tcg_ops;
185
186 /*
187 * if not NULL, this is called in order for the CPUClass to initialize
188 * class data that depends on the accelerator, see accel/accel-common.c.
189 */
190 void (*init_accel_cpu)(struct AccelCPUClass *accel_cpu, CPUClass *cc);
191
192 /*
193 * Keep non-pointer data at the end to minimize holes.
194 */
195 int reset_dump_flags;
196 int gdb_num_core_regs;
197 bool gdb_stop_before_watchpoint;
198 };
199
200 /*
201 * Fix the number of mmu modes to 16, which is also the maximum
202 * supported by the softmmu tlb api.
203 */
204 #define NB_MMU_MODES 16
205
206 /* Use a fully associative victim tlb of 8 entries. */
207 #define CPU_VTLB_SIZE 8
208
209 /*
210 * The full TLB entry, which is not accessed by generated TCG code,
211 * so the layout is not as critical as that of CPUTLBEntry. This is
212 * also why we don't want to combine the two structs.
213 */
214 struct CPUTLBEntryFull {
215 /*
216 * @xlat_section contains:
217 * - in the lower TARGET_PAGE_BITS, a physical section number
218 * - with the lower TARGET_PAGE_BITS masked off, an offset which
219 * must be added to the virtual address to obtain:
220 * + the ram_addr_t of the target RAM (if the physical section
221 * number is PHYS_SECTION_NOTDIRTY or PHYS_SECTION_ROM)
222 * + the offset within the target MemoryRegion (otherwise)
223 */
224 hwaddr xlat_section;
225
226 /*
227 * @phys_addr contains the physical address in the address space
228 * given by cpu_asidx_from_attrs(cpu, @attrs).
229 */
230 hwaddr phys_addr;
231
232 /* @attrs contains the memory transaction attributes for the page. */
233 MemTxAttrs attrs;
234
235 /* @prot contains the complete protections for the page. */
236 uint8_t prot;
237
238 /* @lg_page_size contains the log2 of the page size. */
239 uint8_t lg_page_size;
240
241 /* Additional tlb flags requested by tlb_fill. */
242 uint8_t tlb_fill_flags;
243
244 /*
245 * Additional tlb flags for use by the slow path. If non-zero,
246 * the corresponding CPUTLBEntry comparator must have TLB_FORCE_SLOW.
247 */
248 uint8_t slow_flags[MMU_ACCESS_COUNT];
249
250 /*
251 * Allow target-specific additions to this structure.
252 * This may be used to cache items from the guest cpu
253 * page tables for later use by the implementation.
254 */
255 union {
256 /*
257 * Cache the attrs and shareability fields from the page table entry.
258 *
259 * For ARMMMUIdx_Stage2*, pte_attrs is the S2 descriptor bits [5:2].
260 * Otherwise, pte_attrs is the same as the MAIR_EL1 8-bit format.
261 * For shareability and guarded, as in the SH and GP fields respectively
262 * of the VMSAv8-64 PTEs.
263 */
264 struct {
265 uint8_t pte_attrs;
266 uint8_t shareability;
267 bool guarded;
268 } arm;
269 } extra;
270 };
271
272 /*
273 * Data elements that are per MMU mode, minus the bits accessed by
274 * the TCG fast path.
275 */
276 typedef struct CPUTLBDesc {
277 /*
278 * Describe a region covering all of the large pages allocated
279 * into the tlb. When any page within this region is flushed,
280 * we must flush the entire tlb. The region is matched if
281 * (addr & large_page_mask) == large_page_addr.
282 */
283 vaddr large_page_addr;
284 vaddr large_page_mask;
285 /* host time (in ns) at the beginning of the time window */
286 int64_t window_begin_ns;
287 /* maximum number of entries observed in the window */
288 size_t window_max_entries;
289 size_t n_used_entries;
290 /* The next index to use in the tlb victim table. */
291 size_t vindex;
292 /* The tlb victim table, in two parts. */
293 CPUTLBEntry vtable[CPU_VTLB_SIZE];
294 CPUTLBEntryFull vfulltlb[CPU_VTLB_SIZE];
295 CPUTLBEntryFull *fulltlb;
296 } CPUTLBDesc;
297
298 /*
299 * Data elements that are shared between all MMU modes.
300 */
301 typedef struct CPUTLBCommon {
302 /* Serialize updates to f.table and d.vtable, and others as noted. */
303 QemuSpin lock;
304 /*
305 * Within dirty, for each bit N, modifications have been made to
306 * mmu_idx N since the last time that mmu_idx was flushed.
307 * Protected by tlb_c.lock.
308 */
309 uint16_t dirty;
310 /*
311 * Statistics. These are not lock protected, but are read and
312 * written atomically. This allows the monitor to print a snapshot
313 * of the stats without interfering with the cpu.
314 */
315 size_t full_flush_count;
316 size_t part_flush_count;
317 size_t elide_flush_count;
318 } CPUTLBCommon;
319
320 /*
321 * The entire softmmu tlb, for all MMU modes.
322 * The meaning of each of the MMU modes is defined in the target code.
323 * Since this is placed within CPUNegativeOffsetState, the smallest
324 * negative offsets are at the end of the struct.
325 */
326 typedef struct CPUTLB {
327 #ifdef CONFIG_TCG
328 CPUTLBCommon c;
329 CPUTLBDesc d[NB_MMU_MODES];
330 CPUTLBDescFast f[NB_MMU_MODES];
331 #endif
332 } CPUTLB;
333
334 /*
335 * Low 16 bits: number of cycles left, used only in icount mode.
336 * High 16 bits: Set to -1 to force TCG to stop executing linked TBs
337 * for this CPU and return to its top level loop (even in non-icount mode).
338 * This allows a single read-compare-cbranch-write sequence to test
339 * for both decrementer underflow and exceptions.
340 */
341 typedef union IcountDecr {
342 uint32_t u32;
343 struct {
344 #if HOST_BIG_ENDIAN
345 uint16_t high;
346 uint16_t low;
347 #else
348 uint16_t low;
349 uint16_t high;
350 #endif
351 } u16;
352 } IcountDecr;
353
354 /**
355 * CPUNegativeOffsetState: Elements of CPUState most efficiently accessed
356 * from CPUArchState, via small negative offsets.
357 * @can_do_io: True if memory-mapped IO is allowed.
358 * @plugin_mem_cbs: active plugin memory callbacks
359 * @plugin_mem_value_low: 64 lower bits of latest accessed mem value.
360 * @plugin_mem_value_high: 64 higher bits of latest accessed mem value.
361 */
362 typedef struct CPUNegativeOffsetState {
363 CPUTLB tlb;
364 #ifdef CONFIG_PLUGIN
365 /*
366 * The callback pointer are accessed via TCG (see gen_empty_mem_helper).
367 */
368 GArray *plugin_mem_cbs;
369 uint64_t plugin_mem_value_low;
370 uint64_t plugin_mem_value_high;
371 #endif
372 IcountDecr icount_decr;
373 bool can_do_io;
374 } CPUNegativeOffsetState;
375
376 struct KVMState;
377 struct kvm_run;
378
379 /* work queue */
380
381 /* The union type allows passing of 64 bit target pointers on 32 bit
382 * hosts in a single parameter
383 */
384 typedef union {
385 int host_int;
386 unsigned long host_ulong;
387 void *host_ptr;
388 vaddr target_ptr;
389 } run_on_cpu_data;
390
391 #define RUN_ON_CPU_HOST_PTR(p) ((run_on_cpu_data){.host_ptr = (p)})
392 #define RUN_ON_CPU_HOST_INT(i) ((run_on_cpu_data){.host_int = (i)})
393 #define RUN_ON_CPU_HOST_ULONG(ul) ((run_on_cpu_data){.host_ulong = (ul)})
394 #define RUN_ON_CPU_TARGET_PTR(v) ((run_on_cpu_data){.target_ptr = (v)})
395 #define RUN_ON_CPU_NULL RUN_ON_CPU_HOST_PTR(NULL)
396
397 typedef void (*run_on_cpu_func)(CPUState *cpu, run_on_cpu_data data);
398
399 struct qemu_work_item;
400
401 #define CPU_UNSET_NUMA_NODE_ID -1
402
403 /**
404 * struct CPUState - common state of one CPU core or thread.
405 *
406 * @cpu_index: CPU index (informative).
407 * @cluster_index: Identifies which cluster this CPU is in.
408 * For boards which don't define clusters or for "loose" CPUs not assigned
409 * to a cluster this will be UNASSIGNED_CLUSTER_INDEX; otherwise it will
410 * be the same as the cluster-id property of the CPU object's TYPE_CPU_CLUSTER
411 * QOM parent.
412 * Under TCG this value is propagated to @tcg_cflags.
413 * See TranslationBlock::TCG CF_CLUSTER_MASK.
414 * @tcg_cflags: Pre-computed cflags for this cpu.
415 * @nr_threads: Number of threads within this CPU core.
416 * @thread: Host thread details, only live once @created is #true
417 * @sem: WIN32 only semaphore used only for qtest
418 * @thread_id: native thread id of vCPU, only live once @created is #true
419 * @running: #true if CPU is currently running (lockless).
420 * @has_waiter: #true if a CPU is currently waiting for the cpu_exec_end;
421 * valid under cpu_list_lock.
422 * @created: Indicates whether the CPU thread has been successfully created.
423 * @halt_cond: condition variable sleeping threads can wait on.
424 * @interrupt_request: Indicates a pending interrupt request.
425 * @halted: Nonzero if the CPU is in suspended state.
426 * @stop: Indicates a pending stop request.
427 * @stopped: Indicates the CPU has been artificially stopped.
428 * @unplug: Indicates a pending CPU unplug request.
429 * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU
430 * @singlestep_enabled: Flags for single-stepping.
431 * @icount_extra: Instructions until next timer event.
432 * @cpu_ases: Pointer to array of CPUAddressSpaces (which define the
433 * AddressSpaces this CPU has)
434 * @num_ases: number of CPUAddressSpaces in @cpu_ases
435 * @as: Pointer to the first AddressSpace, for the convenience of targets which
436 * only have a single AddressSpace
437 * @gdb_regs: Additional GDB registers.
438 * @gdb_num_regs: Number of total registers accessible to GDB.
439 * @gdb_num_g_regs: Number of registers in GDB 'g' packets.
440 * @node: QTAILQ of CPUs sharing TB cache.
441 * @opaque: User data.
442 * @mem_io_pc: Host Program Counter at which the memory was accessed.
443 * @accel: Pointer to accelerator specific state.
444 * @kvm_fd: vCPU file descriptor for KVM.
445 * @work_mutex: Lock to prevent multiple access to @work_list.
446 * @work_list: List of pending asynchronous work.
447 * @plugin_state: per-CPU plugin state
448 * @ignore_memory_transaction_failures: Cached copy of the MachineState
449 * flag of the same name: allows the board to suppress calling of the
450 * CPU do_transaction_failed hook function.
451 * @kvm_dirty_gfns: Points to the KVM dirty ring for this CPU when KVM dirty
452 * ring is enabled.
453 * @kvm_fetch_index: Keeps the index that we last fetched from the per-vCPU
454 * dirty ring structure.
455 *
456 * @neg_align: The CPUState is the common part of a concrete ArchCPU
457 * which is allocated when an individual CPU instance is created. As
458 * such care is taken is ensure there is no gap between between
459 * CPUState and CPUArchState within ArchCPU.
460 *
461 * @neg: The architectural register state ("cpu_env") immediately follows
462 * CPUState in ArchCPU and is passed to TCG code. The @neg structure holds
463 * some common TCG CPU variables which are accessed with a negative offset
464 * from cpu_env.
465 */
466 struct CPUState {
467 /*< private >*/
468 DeviceState parent_obj;
469 /* cache to avoid expensive CPU_GET_CLASS */
470 CPUClass *cc;
471 /*< public >*/
472
473 int nr_threads;
474
475 struct QemuThread *thread;
476 #ifdef _WIN32
477 QemuSemaphore sem;
478 #endif
479 int thread_id;
480 bool running, has_waiter;
481 struct QemuCond *halt_cond;
482 bool thread_kicked;
483 bool created;
484 bool stop;
485 bool stopped;
486
487 /* Should CPU start in powered-off state? */
488 bool start_powered_off;
489
490 bool unplug;
491 bool crash_occurred;
492 bool exit_request;
493 int exclusive_context_count;
494 uint32_t cflags_next_tb;
495 /* updates protected by BQL */
496 uint32_t interrupt_request;
497 int singlestep_enabled;
498 int64_t icount_budget;
499 int64_t icount_extra;
500 uint64_t random_seed;
501 sigjmp_buf jmp_env;
502
503 QemuMutex work_mutex;
504 QSIMPLEQ_HEAD(, qemu_work_item) work_list;
505
506 struct CPUAddressSpace *cpu_ases;
507 int cpu_ases_count;
508 int num_ases;
509 AddressSpace *as;
510 MemoryRegion *memory;
511
512 struct CPUJumpCache *tb_jmp_cache;
513
514 GArray *gdb_regs;
515 int gdb_num_regs;
516 int gdb_num_g_regs;
517 QTAILQ_ENTRY(CPUState) node;
518
519 /* ice debug support */
520 QTAILQ_HEAD(, CPUBreakpoint) breakpoints;
521
522 QTAILQ_HEAD(, CPUWatchpoint) watchpoints;
523 CPUWatchpoint *watchpoint_hit;
524
525 void *opaque;
526
527 /* In order to avoid passing too many arguments to the MMIO helpers,
528 * we store some rarely used information in the CPU context.
529 */
530 uintptr_t mem_io_pc;
531
532 /* Only used in KVM */
533 int kvm_fd;
534 struct KVMState *kvm_state;
535 struct kvm_run *kvm_run;
536 struct kvm_dirty_gfn *kvm_dirty_gfns;
537 uint32_t kvm_fetch_index;
538 uint64_t dirty_pages;
539 int kvm_vcpu_stats_fd;
540 bool vcpu_dirty;
541
542 /* Use by accel-block: CPU is executing an ioctl() */
543 QemuLockCnt in_ioctl_lock;
544
545 #ifdef CONFIG_PLUGIN
546 CPUPluginState *plugin_state;
547 #endif
548
549 /* TODO Move common fields from CPUArchState here. */
550 int cpu_index;
551 int cluster_index;
552 uint32_t tcg_cflags;
553 uint32_t halted;
554 int32_t exception_index;
555
556 AccelCPUState *accel;
557
558 /* Used to keep track of an outstanding cpu throttle thread for migration
559 * autoconverge
560 */
561 bool throttle_thread_scheduled;
562
563 /*
564 * Sleep throttle_us_per_full microseconds once dirty ring is full
565 * if dirty page rate limit is enabled.
566 */
567 int64_t throttle_us_per_full;
568
569 bool ignore_memory_transaction_failures;
570
571 /* Used for user-only emulation of prctl(PR_SET_UNALIGN). */
572 bool prctl_unalign_sigbus;
573
574 /* track IOMMUs whose translations we've cached in the TCG TLB */
575 GArray *iommu_notifiers;
576
577 /*
578 * MUST BE LAST in order to minimize the displacement to CPUArchState.
579 */
580 char neg_align[-sizeof(CPUNegativeOffsetState) % 16] QEMU_ALIGNED(16);
581 CPUNegativeOffsetState neg;
582 };
583
584 /* Validate placement of CPUNegativeOffsetState. */
585 QEMU_BUILD_BUG_ON(offsetof(CPUState, neg) !=
586 sizeof(CPUState) - sizeof(CPUNegativeOffsetState));
587
cpu_env(CPUState * cpu)588 static inline CPUArchState *cpu_env(CPUState *cpu)
589 {
590 /* We validate that CPUArchState follows CPUState in cpu-target.c */
591 return (CPUArchState *)(cpu + 1);
592 }
593
594 typedef QTAILQ_HEAD(CPUTailQ, CPUState) CPUTailQ;
595 extern CPUTailQ cpus_queue;
596
597 #define first_cpu QTAILQ_FIRST_RCU(&cpus_queue)
598 #define CPU_NEXT(cpu) QTAILQ_NEXT_RCU(cpu, node)
599 #define CPU_FOREACH(cpu) QTAILQ_FOREACH_RCU(cpu, &cpus_queue, node)
600 #define CPU_FOREACH_SAFE(cpu, next_cpu) \
601 QTAILQ_FOREACH_SAFE_RCU(cpu, &cpus_queue, node, next_cpu)
602
603 extern __thread CPUState *current_cpu;
604
605 /**
606 * cpu_paging_enabled:
607 * @cpu: The CPU whose state is to be inspected.
608 *
609 * Returns: %true if paging is enabled, %false otherwise.
610 */
611 bool cpu_paging_enabled(const CPUState *cpu);
612
613 /**
614 * cpu_get_memory_mapping:
615 * @cpu: The CPU whose memory mappings are to be obtained.
616 * @list: Where to write the memory mappings to.
617 * @errp: Pointer for reporting an #Error.
618 *
619 * Returns: %true on success, %false otherwise.
620 */
621 bool cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
622 Error **errp);
623
624 /**
625 * cpu_write_elf64_note:
626 * @f: pointer to a function that writes memory to a file
627 * @cpu: The CPU whose memory is to be dumped
628 * @cpuid: ID number of the CPU
629 * @opaque: pointer to the CPUState struct
630 */
631 int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
632 int cpuid, void *opaque);
633
634 /**
635 * cpu_write_elf64_qemunote:
636 * @f: pointer to a function that writes memory to a file
637 * @cpu: The CPU whose memory is to be dumped
638 * @cpuid: ID number of the CPU
639 * @opaque: pointer to the CPUState struct
640 */
641 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
642 void *opaque);
643
644 /**
645 * cpu_write_elf32_note:
646 * @f: pointer to a function that writes memory to a file
647 * @cpu: The CPU whose memory is to be dumped
648 * @cpuid: ID number of the CPU
649 * @opaque: pointer to the CPUState struct
650 */
651 int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
652 int cpuid, void *opaque);
653
654 /**
655 * cpu_write_elf32_qemunote:
656 * @f: pointer to a function that writes memory to a file
657 * @cpu: The CPU whose memory is to be dumped
658 * @cpuid: ID number of the CPU
659 * @opaque: pointer to the CPUState struct
660 */
661 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
662 void *opaque);
663
664 /**
665 * cpu_get_crash_info:
666 * @cpu: The CPU to get crash information for
667 *
668 * Gets the previously saved crash information.
669 * Caller is responsible for freeing the data.
670 */
671 GuestPanicInformation *cpu_get_crash_info(CPUState *cpu);
672
673 /**
674 * CPUDumpFlags:
675 * @CPU_DUMP_CODE:
676 * @CPU_DUMP_FPU: dump FPU register state, not just integer
677 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
678 * @CPU_DUMP_VPU: dump VPU registers
679 */
680 enum CPUDumpFlags {
681 CPU_DUMP_CODE = 0x00010000,
682 CPU_DUMP_FPU = 0x00020000,
683 CPU_DUMP_CCOP = 0x00040000,
684 CPU_DUMP_VPU = 0x00080000,
685 };
686
687 /**
688 * cpu_dump_state:
689 * @cpu: The CPU whose state is to be dumped.
690 * @f: If non-null, dump to this stream, else to current print sink.
691 *
692 * Dumps CPU state.
693 */
694 void cpu_dump_state(CPUState *cpu, FILE *f, int flags);
695
696 /**
697 * cpu_get_phys_page_attrs_debug:
698 * @cpu: The CPU to obtain the physical page address for.
699 * @addr: The virtual address.
700 * @attrs: Updated on return with the memory transaction attributes to use
701 * for this access.
702 *
703 * Obtains the physical page corresponding to a virtual one, together
704 * with the corresponding memory transaction attributes to use for the access.
705 * Use it only for debugging because no protection checks are done.
706 *
707 * Returns: Corresponding physical page address or -1 if no page found.
708 */
709 hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr,
710 MemTxAttrs *attrs);
711
712 /**
713 * cpu_get_phys_page_debug:
714 * @cpu: The CPU to obtain the physical page address for.
715 * @addr: The virtual address.
716 *
717 * Obtains the physical page corresponding to a virtual one.
718 * Use it only for debugging because no protection checks are done.
719 *
720 * Returns: Corresponding physical page address or -1 if no page found.
721 */
722 hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr);
723
724 /** cpu_asidx_from_attrs:
725 * @cpu: CPU
726 * @attrs: memory transaction attributes
727 *
728 * Returns the address space index specifying the CPU AddressSpace
729 * to use for a memory access with the given transaction attributes.
730 */
731 int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs);
732
733 /**
734 * cpu_virtio_is_big_endian:
735 * @cpu: CPU
736
737 * Returns %true if a CPU which supports runtime configurable endianness
738 * is currently big-endian.
739 */
740 bool cpu_virtio_is_big_endian(CPUState *cpu);
741
742 /**
743 * cpu_has_work:
744 * @cpu: The vCPU to check.
745 *
746 * Checks whether the CPU has work to do.
747 *
748 * Returns: %true if the CPU has work, %false otherwise.
749 */
750 bool cpu_has_work(CPUState *cpu);
751
752 /**
753 * cpu_list_add:
754 * @cpu: The CPU to be added to the list of CPUs.
755 */
756 void cpu_list_add(CPUState *cpu);
757
758 /**
759 * cpu_list_remove:
760 * @cpu: The CPU to be removed from the list of CPUs.
761 */
762 void cpu_list_remove(CPUState *cpu);
763
764 /**
765 * cpu_reset:
766 * @cpu: The CPU whose state is to be reset.
767 */
768 void cpu_reset(CPUState *cpu);
769
770 /**
771 * cpu_class_by_name:
772 * @typename: The CPU base type.
773 * @cpu_model: The model string without any parameters.
774 *
775 * Looks up a concrete CPU #ObjectClass matching name @cpu_model.
776 *
777 * Returns: A concrete #CPUClass or %NULL if no matching class is found
778 * or if the matching class is abstract.
779 */
780 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
781
782 /**
783 * cpu_model_from_type:
784 * @typename: The CPU type name
785 *
786 * Extract the CPU model name from the CPU type name. The
787 * CPU type name is either the combination of the CPU model
788 * name and suffix, or same to the CPU model name.
789 *
790 * Returns: CPU model name or NULL if the CPU class doesn't exist
791 * The user should g_free() the string once no longer needed.
792 */
793 char *cpu_model_from_type(const char *typename);
794
795 /**
796 * cpu_create:
797 * @typename: The CPU type.
798 *
799 * Instantiates a CPU and realizes the CPU.
800 *
801 * Returns: A #CPUState or %NULL if an error occurred.
802 */
803 CPUState *cpu_create(const char *typename);
804
805 /**
806 * parse_cpu_option:
807 * @cpu_option: The -cpu option including optional parameters.
808 *
809 * processes optional parameters and registers them as global properties
810 *
811 * Returns: type of CPU to create or prints error and terminates process
812 * if an error occurred.
813 */
814 const char *parse_cpu_option(const char *cpu_option);
815
816 /**
817 * qemu_cpu_is_self:
818 * @cpu: The vCPU to check against.
819 *
820 * Checks whether the caller is executing on the vCPU thread.
821 *
822 * Returns: %true if called from @cpu's thread, %false otherwise.
823 */
824 bool qemu_cpu_is_self(CPUState *cpu);
825
826 /**
827 * qemu_cpu_kick:
828 * @cpu: The vCPU to kick.
829 *
830 * Kicks @cpu's thread.
831 */
832 void qemu_cpu_kick(CPUState *cpu);
833
834 /**
835 * cpu_is_stopped:
836 * @cpu: The CPU to check.
837 *
838 * Checks whether the CPU is stopped.
839 *
840 * Returns: %true if run state is not running or if artificially stopped;
841 * %false otherwise.
842 */
843 bool cpu_is_stopped(CPUState *cpu);
844
845 /**
846 * do_run_on_cpu:
847 * @cpu: The vCPU to run on.
848 * @func: The function to be executed.
849 * @data: Data to pass to the function.
850 * @mutex: Mutex to release while waiting for @func to run.
851 *
852 * Used internally in the implementation of run_on_cpu.
853 */
854 void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data,
855 QemuMutex *mutex);
856
857 /**
858 * run_on_cpu:
859 * @cpu: The vCPU to run on.
860 * @func: The function to be executed.
861 * @data: Data to pass to the function.
862 *
863 * Schedules the function @func for execution on the vCPU @cpu.
864 */
865 void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
866
867 /**
868 * async_run_on_cpu:
869 * @cpu: The vCPU to run on.
870 * @func: The function to be executed.
871 * @data: Data to pass to the function.
872 *
873 * Schedules the function @func for execution on the vCPU @cpu asynchronously.
874 */
875 void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
876
877 /**
878 * async_safe_run_on_cpu:
879 * @cpu: The vCPU to run on.
880 * @func: The function to be executed.
881 * @data: Data to pass to the function.
882 *
883 * Schedules the function @func for execution on the vCPU @cpu asynchronously,
884 * while all other vCPUs are sleeping.
885 *
886 * Unlike run_on_cpu and async_run_on_cpu, the function is run outside the
887 * BQL.
888 */
889 void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
890
891 /**
892 * cpu_in_exclusive_context()
893 * @cpu: The vCPU to check
894 *
895 * Returns true if @cpu is an exclusive context, for example running
896 * something which has previously been queued via async_safe_run_on_cpu().
897 */
cpu_in_exclusive_context(const CPUState * cpu)898 static inline bool cpu_in_exclusive_context(const CPUState *cpu)
899 {
900 return cpu->exclusive_context_count;
901 }
902
903 /**
904 * qemu_get_cpu:
905 * @index: The CPUState@cpu_index value of the CPU to obtain.
906 *
907 * Gets a CPU matching @index.
908 *
909 * Returns: The CPU or %NULL if there is no matching CPU.
910 */
911 CPUState *qemu_get_cpu(int index);
912
913 /**
914 * cpu_exists:
915 * @id: Guest-exposed CPU ID to lookup.
916 *
917 * Search for CPU with specified ID.
918 *
919 * Returns: %true - CPU is found, %false - CPU isn't found.
920 */
921 bool cpu_exists(int64_t id);
922
923 /**
924 * cpu_by_arch_id:
925 * @id: Guest-exposed CPU ID of the CPU to obtain.
926 *
927 * Get a CPU with matching @id.
928 *
929 * Returns: The CPU or %NULL if there is no matching CPU.
930 */
931 CPUState *cpu_by_arch_id(int64_t id);
932
933 /**
934 * cpu_interrupt:
935 * @cpu: The CPU to set an interrupt on.
936 * @mask: The interrupts to set.
937 *
938 * Invokes the interrupt handler.
939 */
940
941 void cpu_interrupt(CPUState *cpu, int mask);
942
943 /**
944 * cpu_set_pc:
945 * @cpu: The CPU to set the program counter for.
946 * @addr: Program counter value.
947 *
948 * Sets the program counter for a CPU.
949 */
cpu_set_pc(CPUState * cpu,vaddr addr)950 static inline void cpu_set_pc(CPUState *cpu, vaddr addr)
951 {
952 cpu->cc->set_pc(cpu, addr);
953 }
954
955 /**
956 * cpu_reset_interrupt:
957 * @cpu: The CPU to clear the interrupt on.
958 * @mask: The interrupt mask to clear.
959 *
960 * Resets interrupts on the vCPU @cpu.
961 */
962 void cpu_reset_interrupt(CPUState *cpu, int mask);
963
964 /**
965 * cpu_exit:
966 * @cpu: The CPU to exit.
967 *
968 * Requests the CPU @cpu to exit execution.
969 */
970 void cpu_exit(CPUState *cpu);
971
972 /**
973 * cpu_pause:
974 * @cpu: The CPU to pause.
975 *
976 * Pauses CPU, i.e. puts CPU into stopped state.
977 */
978 void cpu_pause(CPUState *cpu);
979
980 /**
981 * cpu_resume:
982 * @cpu: The CPU to resume.
983 *
984 * Resumes CPU, i.e. puts CPU into runnable state.
985 */
986 void cpu_resume(CPUState *cpu);
987
988 /**
989 * cpu_remove_sync:
990 * @cpu: The CPU to remove.
991 *
992 * Requests the CPU to be removed and waits till it is removed.
993 */
994 void cpu_remove_sync(CPUState *cpu);
995
996 /**
997 * free_queued_cpu_work() - free all items on CPU work queue
998 * @cpu: The CPU which work queue to free.
999 */
1000 void free_queued_cpu_work(CPUState *cpu);
1001
1002 /**
1003 * process_queued_cpu_work() - process all items on CPU work queue
1004 * @cpu: The CPU which work queue to process.
1005 */
1006 void process_queued_cpu_work(CPUState *cpu);
1007
1008 /**
1009 * cpu_exec_start:
1010 * @cpu: The CPU for the current thread.
1011 *
1012 * Record that a CPU has started execution and can be interrupted with
1013 * cpu_exit.
1014 */
1015 void cpu_exec_start(CPUState *cpu);
1016
1017 /**
1018 * cpu_exec_end:
1019 * @cpu: The CPU for the current thread.
1020 *
1021 * Record that a CPU has stopped execution and exclusive sections
1022 * can be executed without interrupting it.
1023 */
1024 void cpu_exec_end(CPUState *cpu);
1025
1026 /**
1027 * start_exclusive:
1028 *
1029 * Wait for a concurrent exclusive section to end, and then start
1030 * a section of work that is run while other CPUs are not running
1031 * between cpu_exec_start and cpu_exec_end. CPUs that are running
1032 * cpu_exec are exited immediately. CPUs that call cpu_exec_start
1033 * during the exclusive section go to sleep until this CPU calls
1034 * end_exclusive.
1035 */
1036 void start_exclusive(void);
1037
1038 /**
1039 * end_exclusive:
1040 *
1041 * Concludes an exclusive execution section started by start_exclusive.
1042 */
1043 void end_exclusive(void);
1044
1045 /**
1046 * qemu_init_vcpu:
1047 * @cpu: The vCPU to initialize.
1048 *
1049 * Initializes a vCPU.
1050 */
1051 void qemu_init_vcpu(CPUState *cpu);
1052
1053 #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
1054 #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
1055 #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
1056
1057 /**
1058 * cpu_single_step:
1059 * @cpu: CPU to the flags for.
1060 * @enabled: Flags to enable.
1061 *
1062 * Enables or disables single-stepping for @cpu.
1063 */
1064 void cpu_single_step(CPUState *cpu, int enabled);
1065
1066 /* Breakpoint/watchpoint flags */
1067 #define BP_MEM_READ 0x01
1068 #define BP_MEM_WRITE 0x02
1069 #define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE)
1070 #define BP_STOP_BEFORE_ACCESS 0x04
1071 /* 0x08 currently unused */
1072 #define BP_GDB 0x10
1073 #define BP_CPU 0x20
1074 #define BP_ANY (BP_GDB | BP_CPU)
1075 #define BP_HIT_SHIFT 6
1076 #define BP_WATCHPOINT_HIT_READ (BP_MEM_READ << BP_HIT_SHIFT)
1077 #define BP_WATCHPOINT_HIT_WRITE (BP_MEM_WRITE << BP_HIT_SHIFT)
1078 #define BP_WATCHPOINT_HIT (BP_MEM_ACCESS << BP_HIT_SHIFT)
1079
1080 int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
1081 CPUBreakpoint **breakpoint);
1082 int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags);
1083 void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint);
1084 void cpu_breakpoint_remove_all(CPUState *cpu, int mask);
1085
1086 /* Return true if PC matches an installed breakpoint. */
cpu_breakpoint_test(CPUState * cpu,vaddr pc,int mask)1087 static inline bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask)
1088 {
1089 CPUBreakpoint *bp;
1090
1091 if (unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) {
1092 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
1093 if (bp->pc == pc && (bp->flags & mask)) {
1094 return true;
1095 }
1096 }
1097 }
1098 return false;
1099 }
1100
1101 /**
1102 * cpu_get_address_space:
1103 * @cpu: CPU to get address space from
1104 * @asidx: index identifying which address space to get
1105 *
1106 * Return the requested address space of this CPU. @asidx
1107 * specifies which address space to read.
1108 */
1109 AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx);
1110
1111 G_NORETURN void cpu_abort(CPUState *cpu, const char *fmt, ...)
1112 G_GNUC_PRINTF(2, 3);
1113
1114 /* $(top_srcdir)/cpu.c */
1115 void cpu_class_init_props(DeviceClass *dc);
1116 void cpu_exec_class_post_init(CPUClass *cc);
1117 void cpu_exec_initfn(CPUState *cpu);
1118 void cpu_vmstate_register(CPUState *cpu);
1119 void cpu_vmstate_unregister(CPUState *cpu);
1120 bool cpu_exec_realizefn(CPUState *cpu, Error **errp);
1121 void cpu_exec_unrealizefn(CPUState *cpu);
1122 void cpu_exec_reset_hold(CPUState *cpu);
1123
1124 extern const VMStateDescription vmstate_cpu_common;
1125
1126 #define UNASSIGNED_CPU_INDEX -1
1127 #define UNASSIGNED_CLUSTER_INDEX -1
1128
1129 #endif
1130