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