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