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