xref: /qemu/include/hw/core/cpu.h (revision bb5de525)
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 "qapi/qapi-types-run-state.h"
29 #include "qemu/bitmap.h"
30 #include "qemu/rcu_queue.h"
31 #include "qemu/queue.h"
32 #include "qemu/thread.h"
33 #include "qemu/plugin-event.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 } MMUAccessType;
88 
89 typedef struct CPUWatchpoint CPUWatchpoint;
90 
91 /* see tcg-cpu-ops.h */
92 struct TCGCPUOps;
93 
94 /* see accel-cpu.h */
95 struct AccelCPUClass;
96 
97 /* see sysemu-cpu-ops.h */
98 struct SysemuCPUOps;
99 
100 /**
101  * CPUClass:
102  * @class_by_name: Callback to map -cpu command line model name to an
103  * instantiatable CPU type.
104  * @parse_features: Callback to parse command line arguments.
105  * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
106  * @has_work: Callback for checking if there is work to do.
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.
129  * @gdb_core_xml_file: File name for core registers GDB XML description.
130  * @gdb_stop_before_watchpoint: Indicates whether GDB expects the CPU to stop
131  *           before the insn which triggers a watchpoint rather than after it.
132  * @gdb_arch_name: Optional callback that returns the architecture name known
133  * to GDB. The caller must free the returned string with g_free.
134  * @gdb_get_dynamic_xml: Callback to return dynamically generated XML for the
135  *   gdb stub. Returns a pointer to the XML contents for the specified XML file
136  *   or NULL if the CPU doesn't have a dynamically generated content for it.
137  * @disas_set_info: Setup architecture specific components of disassembly info
138  * @adjust_watchpoint_address: Perform a target-specific adjustment to an
139  * address before attempting to match it against watchpoints.
140  * @deprecation_note: If this CPUClass is deprecated, this field provides
141  *                    related information.
142  *
143  * Represents a CPU family or model.
144  */
145 struct CPUClass {
146     /*< private >*/
147     DeviceClass parent_class;
148     /*< public >*/
149 
150     ObjectClass *(*class_by_name)(const char *cpu_model);
151     void (*parse_features)(const char *typename, char *str, Error **errp);
152 
153     bool (*has_work)(CPUState *cpu);
154     int (*memory_rw_debug)(CPUState *cpu, vaddr addr,
155                            uint8_t *buf, int len, bool is_write);
156     void (*dump_state)(CPUState *cpu, FILE *, int flags);
157     void (*query_cpu_fast)(CPUState *cpu, CpuInfoFast *value);
158     int64_t (*get_arch_id)(CPUState *cpu);
159     void (*set_pc)(CPUState *cpu, vaddr value);
160     vaddr (*get_pc)(CPUState *cpu);
161     int (*gdb_read_register)(CPUState *cpu, GByteArray *buf, int reg);
162     int (*gdb_write_register)(CPUState *cpu, uint8_t *buf, int reg);
163     vaddr (*gdb_adjust_breakpoint)(CPUState *cpu, vaddr addr);
164 
165     const char *gdb_core_xml_file;
166     gchar * (*gdb_arch_name)(CPUState *cpu);
167     const char * (*gdb_get_dynamic_xml)(CPUState *cpu, const char *xmlname);
168 
169     void (*disas_set_info)(CPUState *cpu, disassemble_info *info);
170 
171     const char *deprecation_note;
172     struct AccelCPUClass *accel_cpu;
173 
174     /* when system emulation is not available, this pointer is NULL */
175     const struct SysemuCPUOps *sysemu_ops;
176 
177     /* when TCG is not available, this pointer is NULL */
178     const struct TCGCPUOps *tcg_ops;
179 
180     /*
181      * if not NULL, this is called in order for the CPUClass to initialize
182      * class data that depends on the accelerator, see accel/accel-common.c.
183      */
184     void (*init_accel_cpu)(struct AccelCPUClass *accel_cpu, CPUClass *cc);
185 
186     /*
187      * Keep non-pointer data at the end to minimize holes.
188      */
189     int reset_dump_flags;
190     int gdb_num_core_regs;
191     bool gdb_stop_before_watchpoint;
192 };
193 
194 /*
195  * Low 16 bits: number of cycles left, used only in icount mode.
196  * High 16 bits: Set to -1 to force TCG to stop executing linked TBs
197  * for this CPU and return to its top level loop (even in non-icount mode).
198  * This allows a single read-compare-cbranch-write sequence to test
199  * for both decrementer underflow and exceptions.
200  */
201 typedef union IcountDecr {
202     uint32_t u32;
203     struct {
204 #if HOST_BIG_ENDIAN
205         uint16_t high;
206         uint16_t low;
207 #else
208         uint16_t low;
209         uint16_t high;
210 #endif
211     } u16;
212 } IcountDecr;
213 
214 typedef struct CPUBreakpoint {
215     vaddr pc;
216     int flags; /* BP_* */
217     QTAILQ_ENTRY(CPUBreakpoint) entry;
218 } CPUBreakpoint;
219 
220 struct CPUWatchpoint {
221     vaddr vaddr;
222     vaddr len;
223     vaddr hitaddr;
224     MemTxAttrs hitattrs;
225     int flags; /* BP_* */
226     QTAILQ_ENTRY(CPUWatchpoint) entry;
227 };
228 
229 #ifdef CONFIG_PLUGIN
230 /*
231  * For plugins we sometime need to save the resolved iotlb data before
232  * the memory regions get moved around  by io_writex.
233  */
234 typedef struct SavedIOTLB {
235     MemoryRegionSection *section;
236     hwaddr mr_offset;
237 } SavedIOTLB;
238 #endif
239 
240 struct KVMState;
241 struct kvm_run;
242 
243 struct hax_vcpu_state;
244 struct hvf_vcpu_state;
245 
246 /* work queue */
247 
248 /* The union type allows passing of 64 bit target pointers on 32 bit
249  * hosts in a single parameter
250  */
251 typedef union {
252     int           host_int;
253     unsigned long host_ulong;
254     void         *host_ptr;
255     vaddr         target_ptr;
256 } run_on_cpu_data;
257 
258 #define RUN_ON_CPU_HOST_PTR(p)    ((run_on_cpu_data){.host_ptr = (p)})
259 #define RUN_ON_CPU_HOST_INT(i)    ((run_on_cpu_data){.host_int = (i)})
260 #define RUN_ON_CPU_HOST_ULONG(ul) ((run_on_cpu_data){.host_ulong = (ul)})
261 #define RUN_ON_CPU_TARGET_PTR(v)  ((run_on_cpu_data){.target_ptr = (v)})
262 #define RUN_ON_CPU_NULL           RUN_ON_CPU_HOST_PTR(NULL)
263 
264 typedef void (*run_on_cpu_func)(CPUState *cpu, run_on_cpu_data data);
265 
266 struct qemu_work_item;
267 
268 #define CPU_UNSET_NUMA_NODE_ID -1
269 
270 /**
271  * CPUState:
272  * @cpu_index: CPU index (informative).
273  * @cluster_index: Identifies which cluster this CPU is in.
274  *   For boards which don't define clusters or for "loose" CPUs not assigned
275  *   to a cluster this will be UNASSIGNED_CLUSTER_INDEX; otherwise it will
276  *   be the same as the cluster-id property of the CPU object's TYPE_CPU_CLUSTER
277  *   QOM parent.
278  *   Under TCG this value is propagated to @tcg_cflags.
279  *   See TranslationBlock::TCG CF_CLUSTER_MASK.
280  * @tcg_cflags: Pre-computed cflags for this cpu.
281  * @nr_cores: Number of cores within this CPU package.
282  * @nr_threads: Number of threads within this CPU.
283  * @running: #true if CPU is currently running (lockless).
284  * @has_waiter: #true if a CPU is currently waiting for the cpu_exec_end;
285  * valid under cpu_list_lock.
286  * @created: Indicates whether the CPU thread has been successfully created.
287  * @interrupt_request: Indicates a pending interrupt request.
288  * @halted: Nonzero if the CPU is in suspended state.
289  * @stop: Indicates a pending stop request.
290  * @stopped: Indicates the CPU has been artificially stopped.
291  * @unplug: Indicates a pending CPU unplug request.
292  * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU
293  * @singlestep_enabled: Flags for single-stepping.
294  * @icount_extra: Instructions until next timer event.
295  * @can_do_io: Nonzero if memory-mapped IO is safe. Deterministic execution
296  * requires that IO only be performed on the last instruction of a TB
297  * so that interrupts take effect immediately.
298  * @cpu_ases: Pointer to array of CPUAddressSpaces (which define the
299  *            AddressSpaces this CPU has)
300  * @num_ases: number of CPUAddressSpaces in @cpu_ases
301  * @as: Pointer to the first AddressSpace, for the convenience of targets which
302  *      only have a single AddressSpace
303  * @env_ptr: Pointer to subclass-specific CPUArchState field.
304  * @icount_decr_ptr: Pointer to IcountDecr field within subclass.
305  * @gdb_regs: Additional GDB registers.
306  * @gdb_num_regs: Number of total registers accessible to GDB.
307  * @gdb_num_g_regs: Number of registers in GDB 'g' packets.
308  * @next_cpu: Next CPU sharing TB cache.
309  * @opaque: User data.
310  * @mem_io_pc: Host Program Counter at which the memory was accessed.
311  * @kvm_fd: vCPU file descriptor for KVM.
312  * @work_mutex: Lock to prevent multiple access to @work_list.
313  * @work_list: List of pending asynchronous work.
314  * @trace_dstate_delayed: Delayed changes to trace_dstate (includes all changes
315  *                        to @trace_dstate).
316  * @trace_dstate: Dynamic tracing state of events for this vCPU (bitmask).
317  * @plugin_mask: Plugin event bitmap. Modified only via async work.
318  * @ignore_memory_transaction_failures: Cached copy of the MachineState
319  *    flag of the same name: allows the board to suppress calling of the
320  *    CPU do_transaction_failed hook function.
321  * @kvm_dirty_gfns: Points to the KVM dirty ring for this CPU when KVM dirty
322  *    ring is enabled.
323  * @kvm_fetch_index: Keeps the index that we last fetched from the per-vCPU
324  *    dirty ring structure.
325  *
326  * State of one CPU core or thread.
327  */
328 struct CPUState {
329     /*< private >*/
330     DeviceState parent_obj;
331     /* cache to avoid expensive CPU_GET_CLASS */
332     CPUClass *cc;
333     /*< public >*/
334 
335     int nr_cores;
336     int nr_threads;
337 
338     struct QemuThread *thread;
339 #ifdef _WIN32
340     HANDLE hThread;
341     QemuSemaphore sem;
342 #endif
343     int thread_id;
344     bool running, has_waiter;
345     struct QemuCond *halt_cond;
346     bool thread_kicked;
347     bool created;
348     bool stop;
349     bool stopped;
350 
351     /* Should CPU start in powered-off state? */
352     bool start_powered_off;
353 
354     bool unplug;
355     bool crash_occurred;
356     bool exit_request;
357     int exclusive_context_count;
358     uint32_t cflags_next_tb;
359     /* updates protected by BQL */
360     uint32_t interrupt_request;
361     int singlestep_enabled;
362     int64_t icount_budget;
363     int64_t icount_extra;
364     uint64_t random_seed;
365     sigjmp_buf jmp_env;
366 
367     QemuMutex work_mutex;
368     QSIMPLEQ_HEAD(, qemu_work_item) work_list;
369 
370     CPUAddressSpace *cpu_ases;
371     int num_ases;
372     AddressSpace *as;
373     MemoryRegion *memory;
374 
375     CPUArchState *env_ptr;
376     IcountDecr *icount_decr_ptr;
377 
378     CPUJumpCache *tb_jmp_cache;
379 
380     struct GDBRegisterState *gdb_regs;
381     int gdb_num_regs;
382     int gdb_num_g_regs;
383     QTAILQ_ENTRY(CPUState) node;
384 
385     /* ice debug support */
386     QTAILQ_HEAD(, CPUBreakpoint) breakpoints;
387 
388     QTAILQ_HEAD(, CPUWatchpoint) watchpoints;
389     CPUWatchpoint *watchpoint_hit;
390 
391     void *opaque;
392 
393     /* In order to avoid passing too many arguments to the MMIO helpers,
394      * we store some rarely used information in the CPU context.
395      */
396     uintptr_t mem_io_pc;
397 
398     /* Only used in KVM */
399     int kvm_fd;
400     struct KVMState *kvm_state;
401     struct kvm_run *kvm_run;
402     struct kvm_dirty_gfn *kvm_dirty_gfns;
403     uint32_t kvm_fetch_index;
404     uint64_t dirty_pages;
405     int kvm_vcpu_stats_fd;
406 
407     /* Use by accel-block: CPU is executing an ioctl() */
408     QemuLockCnt in_ioctl_lock;
409 
410     DECLARE_BITMAP(plugin_mask, QEMU_PLUGIN_EV_MAX);
411 
412 #ifdef CONFIG_PLUGIN
413     GArray *plugin_mem_cbs;
414     /* saved iotlb data from io_writex */
415     SavedIOTLB saved_iotlb;
416 #endif
417 
418     /* TODO Move common fields from CPUArchState here. */
419     int cpu_index;
420     int cluster_index;
421     uint32_t tcg_cflags;
422     uint32_t halted;
423     uint32_t can_do_io;
424     int32_t exception_index;
425 
426     /* shared by kvm, hax and hvf */
427     bool vcpu_dirty;
428 
429     /* Used to keep track of an outstanding cpu throttle thread for migration
430      * autoconverge
431      */
432     bool throttle_thread_scheduled;
433 
434     /*
435      * Sleep throttle_us_per_full microseconds once dirty ring is full
436      * if dirty page rate limit is enabled.
437      */
438     int64_t throttle_us_per_full;
439 
440     bool ignore_memory_transaction_failures;
441 
442     /* Used for user-only emulation of prctl(PR_SET_UNALIGN). */
443     bool prctl_unalign_sigbus;
444 
445     struct hax_vcpu_state *hax_vcpu;
446 
447     struct hvf_vcpu_state *hvf;
448 
449     /* track IOMMUs whose translations we've cached in the TCG TLB */
450     GArray *iommu_notifiers;
451 };
452 
453 typedef QTAILQ_HEAD(CPUTailQ, CPUState) CPUTailQ;
454 extern CPUTailQ cpus;
455 
456 #define first_cpu        QTAILQ_FIRST_RCU(&cpus)
457 #define CPU_NEXT(cpu)    QTAILQ_NEXT_RCU(cpu, node)
458 #define CPU_FOREACH(cpu) QTAILQ_FOREACH_RCU(cpu, &cpus, node)
459 #define CPU_FOREACH_SAFE(cpu, next_cpu) \
460     QTAILQ_FOREACH_SAFE_RCU(cpu, &cpus, node, next_cpu)
461 
462 extern __thread CPUState *current_cpu;
463 
464 /**
465  * qemu_tcg_mttcg_enabled:
466  * Check whether we are running MultiThread TCG or not.
467  *
468  * Returns: %true if we are in MTTCG mode %false otherwise.
469  */
470 extern bool mttcg_enabled;
471 #define qemu_tcg_mttcg_enabled() (mttcg_enabled)
472 
473 /**
474  * cpu_paging_enabled:
475  * @cpu: The CPU whose state is to be inspected.
476  *
477  * Returns: %true if paging is enabled, %false otherwise.
478  */
479 bool cpu_paging_enabled(const CPUState *cpu);
480 
481 /**
482  * cpu_get_memory_mapping:
483  * @cpu: The CPU whose memory mappings are to be obtained.
484  * @list: Where to write the memory mappings to.
485  * @errp: Pointer for reporting an #Error.
486  */
487 void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
488                             Error **errp);
489 
490 #if !defined(CONFIG_USER_ONLY)
491 
492 /**
493  * cpu_write_elf64_note:
494  * @f: pointer to a function that writes memory to a file
495  * @cpu: The CPU whose memory is to be dumped
496  * @cpuid: ID number of the CPU
497  * @opaque: pointer to the CPUState struct
498  */
499 int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
500                          int cpuid, void *opaque);
501 
502 /**
503  * cpu_write_elf64_qemunote:
504  * @f: pointer to a function that writes memory to a file
505  * @cpu: The CPU whose memory is to be dumped
506  * @cpuid: ID number of the CPU
507  * @opaque: pointer to the CPUState struct
508  */
509 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
510                              void *opaque);
511 
512 /**
513  * cpu_write_elf32_note:
514  * @f: pointer to a function that writes memory to a file
515  * @cpu: The CPU whose memory is to be dumped
516  * @cpuid: ID number of the CPU
517  * @opaque: pointer to the CPUState struct
518  */
519 int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
520                          int cpuid, void *opaque);
521 
522 /**
523  * cpu_write_elf32_qemunote:
524  * @f: pointer to a function that writes memory to a file
525  * @cpu: The CPU whose memory is to be dumped
526  * @cpuid: ID number of the CPU
527  * @opaque: pointer to the CPUState struct
528  */
529 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
530                              void *opaque);
531 
532 /**
533  * cpu_get_crash_info:
534  * @cpu: The CPU to get crash information for
535  *
536  * Gets the previously saved crash information.
537  * Caller is responsible for freeing the data.
538  */
539 GuestPanicInformation *cpu_get_crash_info(CPUState *cpu);
540 
541 #endif /* !CONFIG_USER_ONLY */
542 
543 /**
544  * CPUDumpFlags:
545  * @CPU_DUMP_CODE:
546  * @CPU_DUMP_FPU: dump FPU register state, not just integer
547  * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
548  * @CPU_DUMP_VPU: dump VPU registers
549  */
550 enum CPUDumpFlags {
551     CPU_DUMP_CODE = 0x00010000,
552     CPU_DUMP_FPU  = 0x00020000,
553     CPU_DUMP_CCOP = 0x00040000,
554     CPU_DUMP_VPU  = 0x00080000,
555 };
556 
557 /**
558  * cpu_dump_state:
559  * @cpu: The CPU whose state is to be dumped.
560  * @f: If non-null, dump to this stream, else to current print sink.
561  *
562  * Dumps CPU state.
563  */
564 void cpu_dump_state(CPUState *cpu, FILE *f, int flags);
565 
566 #ifndef CONFIG_USER_ONLY
567 /**
568  * cpu_get_phys_page_attrs_debug:
569  * @cpu: The CPU to obtain the physical page address for.
570  * @addr: The virtual address.
571  * @attrs: Updated on return with the memory transaction attributes to use
572  *         for this access.
573  *
574  * Obtains the physical page corresponding to a virtual one, together
575  * with the corresponding memory transaction attributes to use for the access.
576  * Use it only for debugging because no protection checks are done.
577  *
578  * Returns: Corresponding physical page address or -1 if no page found.
579  */
580 hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr,
581                                      MemTxAttrs *attrs);
582 
583 /**
584  * cpu_get_phys_page_debug:
585  * @cpu: The CPU to obtain the physical page address for.
586  * @addr: The virtual address.
587  *
588  * Obtains the physical page corresponding to a virtual one.
589  * Use it only for debugging because no protection checks are done.
590  *
591  * Returns: Corresponding physical page address or -1 if no page found.
592  */
593 hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr);
594 
595 /** cpu_asidx_from_attrs:
596  * @cpu: CPU
597  * @attrs: memory transaction attributes
598  *
599  * Returns the address space index specifying the CPU AddressSpace
600  * to use for a memory access with the given transaction attributes.
601  */
602 int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs);
603 
604 /**
605  * cpu_virtio_is_big_endian:
606  * @cpu: CPU
607 
608  * Returns %true if a CPU which supports runtime configurable endianness
609  * is currently big-endian.
610  */
611 bool cpu_virtio_is_big_endian(CPUState *cpu);
612 
613 #endif /* CONFIG_USER_ONLY */
614 
615 /**
616  * cpu_list_add:
617  * @cpu: The CPU to be added to the list of CPUs.
618  */
619 void cpu_list_add(CPUState *cpu);
620 
621 /**
622  * cpu_list_remove:
623  * @cpu: The CPU to be removed from the list of CPUs.
624  */
625 void cpu_list_remove(CPUState *cpu);
626 
627 /**
628  * cpu_reset:
629  * @cpu: The CPU whose state is to be reset.
630  */
631 void cpu_reset(CPUState *cpu);
632 
633 /**
634  * cpu_class_by_name:
635  * @typename: The CPU base type.
636  * @cpu_model: The model string without any parameters.
637  *
638  * Looks up a CPU #ObjectClass matching name @cpu_model.
639  *
640  * Returns: A #CPUClass or %NULL if not matching class is found.
641  */
642 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
643 
644 /**
645  * cpu_create:
646  * @typename: The CPU type.
647  *
648  * Instantiates a CPU and realizes the CPU.
649  *
650  * Returns: A #CPUState or %NULL if an error occurred.
651  */
652 CPUState *cpu_create(const char *typename);
653 
654 /**
655  * parse_cpu_option:
656  * @cpu_option: The -cpu option including optional parameters.
657  *
658  * processes optional parameters and registers them as global properties
659  *
660  * Returns: type of CPU to create or prints error and terminates process
661  *          if an error occurred.
662  */
663 const char *parse_cpu_option(const char *cpu_option);
664 
665 /**
666  * cpu_has_work:
667  * @cpu: The vCPU to check.
668  *
669  * Checks whether the CPU has work to do.
670  *
671  * Returns: %true if the CPU has work, %false otherwise.
672  */
673 static inline bool cpu_has_work(CPUState *cpu)
674 {
675     CPUClass *cc = CPU_GET_CLASS(cpu);
676 
677     g_assert(cc->has_work);
678     return cc->has_work(cpu);
679 }
680 
681 /**
682  * qemu_cpu_is_self:
683  * @cpu: The vCPU to check against.
684  *
685  * Checks whether the caller is executing on the vCPU thread.
686  *
687  * Returns: %true if called from @cpu's thread, %false otherwise.
688  */
689 bool qemu_cpu_is_self(CPUState *cpu);
690 
691 /**
692  * qemu_cpu_kick:
693  * @cpu: The vCPU to kick.
694  *
695  * Kicks @cpu's thread.
696  */
697 void qemu_cpu_kick(CPUState *cpu);
698 
699 /**
700  * cpu_is_stopped:
701  * @cpu: The CPU to check.
702  *
703  * Checks whether the CPU is stopped.
704  *
705  * Returns: %true if run state is not running or if artificially stopped;
706  * %false otherwise.
707  */
708 bool cpu_is_stopped(CPUState *cpu);
709 
710 /**
711  * do_run_on_cpu:
712  * @cpu: The vCPU to run on.
713  * @func: The function to be executed.
714  * @data: Data to pass to the function.
715  * @mutex: Mutex to release while waiting for @func to run.
716  *
717  * Used internally in the implementation of run_on_cpu.
718  */
719 void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data,
720                    QemuMutex *mutex);
721 
722 /**
723  * run_on_cpu:
724  * @cpu: The vCPU to run on.
725  * @func: The function to be executed.
726  * @data: Data to pass to the function.
727  *
728  * Schedules the function @func for execution on the vCPU @cpu.
729  */
730 void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
731 
732 /**
733  * async_run_on_cpu:
734  * @cpu: The vCPU to run on.
735  * @func: The function to be executed.
736  * @data: Data to pass to the function.
737  *
738  * Schedules the function @func for execution on the vCPU @cpu asynchronously.
739  */
740 void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
741 
742 /**
743  * async_safe_run_on_cpu:
744  * @cpu: The vCPU to run on.
745  * @func: The function to be executed.
746  * @data: Data to pass to the function.
747  *
748  * Schedules the function @func for execution on the vCPU @cpu asynchronously,
749  * while all other vCPUs are sleeping.
750  *
751  * Unlike run_on_cpu and async_run_on_cpu, the function is run outside the
752  * BQL.
753  */
754 void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
755 
756 /**
757  * cpu_in_exclusive_context()
758  * @cpu: The vCPU to check
759  *
760  * Returns true if @cpu is an exclusive context, for example running
761  * something which has previously been queued via async_safe_run_on_cpu().
762  */
763 static inline bool cpu_in_exclusive_context(const CPUState *cpu)
764 {
765     return cpu->exclusive_context_count;
766 }
767 
768 /**
769  * qemu_get_cpu:
770  * @index: The CPUState@cpu_index value of the CPU to obtain.
771  *
772  * Gets a CPU matching @index.
773  *
774  * Returns: The CPU or %NULL if there is no matching CPU.
775  */
776 CPUState *qemu_get_cpu(int index);
777 
778 /**
779  * cpu_exists:
780  * @id: Guest-exposed CPU ID to lookup.
781  *
782  * Search for CPU with specified ID.
783  *
784  * Returns: %true - CPU is found, %false - CPU isn't found.
785  */
786 bool cpu_exists(int64_t id);
787 
788 /**
789  * cpu_by_arch_id:
790  * @id: Guest-exposed CPU ID of the CPU to obtain.
791  *
792  * Get a CPU with matching @id.
793  *
794  * Returns: The CPU or %NULL if there is no matching CPU.
795  */
796 CPUState *cpu_by_arch_id(int64_t id);
797 
798 /**
799  * cpu_interrupt:
800  * @cpu: The CPU to set an interrupt on.
801  * @mask: The interrupts to set.
802  *
803  * Invokes the interrupt handler.
804  */
805 
806 void cpu_interrupt(CPUState *cpu, int mask);
807 
808 /**
809  * cpu_set_pc:
810  * @cpu: The CPU to set the program counter for.
811  * @addr: Program counter value.
812  *
813  * Sets the program counter for a CPU.
814  */
815 static inline void cpu_set_pc(CPUState *cpu, vaddr addr)
816 {
817     CPUClass *cc = CPU_GET_CLASS(cpu);
818 
819     cc->set_pc(cpu, addr);
820 }
821 
822 /**
823  * cpu_reset_interrupt:
824  * @cpu: The CPU to clear the interrupt on.
825  * @mask: The interrupt mask to clear.
826  *
827  * Resets interrupts on the vCPU @cpu.
828  */
829 void cpu_reset_interrupt(CPUState *cpu, int mask);
830 
831 /**
832  * cpu_exit:
833  * @cpu: The CPU to exit.
834  *
835  * Requests the CPU @cpu to exit execution.
836  */
837 void cpu_exit(CPUState *cpu);
838 
839 /**
840  * cpu_resume:
841  * @cpu: The CPU to resume.
842  *
843  * Resumes CPU, i.e. puts CPU into runnable state.
844  */
845 void cpu_resume(CPUState *cpu);
846 
847 /**
848  * cpu_remove_sync:
849  * @cpu: The CPU to remove.
850  *
851  * Requests the CPU to be removed and waits till it is removed.
852  */
853 void cpu_remove_sync(CPUState *cpu);
854 
855 /**
856  * process_queued_cpu_work() - process all items on CPU work queue
857  * @cpu: The CPU which work queue to process.
858  */
859 void process_queued_cpu_work(CPUState *cpu);
860 
861 /**
862  * cpu_exec_start:
863  * @cpu: The CPU for the current thread.
864  *
865  * Record that a CPU has started execution and can be interrupted with
866  * cpu_exit.
867  */
868 void cpu_exec_start(CPUState *cpu);
869 
870 /**
871  * cpu_exec_end:
872  * @cpu: The CPU for the current thread.
873  *
874  * Record that a CPU has stopped execution and exclusive sections
875  * can be executed without interrupting it.
876  */
877 void cpu_exec_end(CPUState *cpu);
878 
879 /**
880  * start_exclusive:
881  *
882  * Wait for a concurrent exclusive section to end, and then start
883  * a section of work that is run while other CPUs are not running
884  * between cpu_exec_start and cpu_exec_end.  CPUs that are running
885  * cpu_exec are exited immediately.  CPUs that call cpu_exec_start
886  * during the exclusive section go to sleep until this CPU calls
887  * end_exclusive.
888  */
889 void start_exclusive(void);
890 
891 /**
892  * end_exclusive:
893  *
894  * Concludes an exclusive execution section started by start_exclusive.
895  */
896 void end_exclusive(void);
897 
898 /**
899  * qemu_init_vcpu:
900  * @cpu: The vCPU to initialize.
901  *
902  * Initializes a vCPU.
903  */
904 void qemu_init_vcpu(CPUState *cpu);
905 
906 #define SSTEP_ENABLE  0x1  /* Enable simulated HW single stepping */
907 #define SSTEP_NOIRQ   0x2  /* Do not use IRQ while single stepping */
908 #define SSTEP_NOTIMER 0x4  /* Do not Timers while single stepping */
909 
910 /**
911  * cpu_single_step:
912  * @cpu: CPU to the flags for.
913  * @enabled: Flags to enable.
914  *
915  * Enables or disables single-stepping for @cpu.
916  */
917 void cpu_single_step(CPUState *cpu, int enabled);
918 
919 /* Breakpoint/watchpoint flags */
920 #define BP_MEM_READ           0x01
921 #define BP_MEM_WRITE          0x02
922 #define BP_MEM_ACCESS         (BP_MEM_READ | BP_MEM_WRITE)
923 #define BP_STOP_BEFORE_ACCESS 0x04
924 /* 0x08 currently unused */
925 #define BP_GDB                0x10
926 #define BP_CPU                0x20
927 #define BP_ANY                (BP_GDB | BP_CPU)
928 #define BP_HIT_SHIFT          6
929 #define BP_WATCHPOINT_HIT_READ  (BP_MEM_READ << BP_HIT_SHIFT)
930 #define BP_WATCHPOINT_HIT_WRITE (BP_MEM_WRITE << BP_HIT_SHIFT)
931 #define BP_WATCHPOINT_HIT       (BP_MEM_ACCESS << BP_HIT_SHIFT)
932 
933 int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
934                           CPUBreakpoint **breakpoint);
935 int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags);
936 void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint);
937 void cpu_breakpoint_remove_all(CPUState *cpu, int mask);
938 
939 /* Return true if PC matches an installed breakpoint.  */
940 static inline bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask)
941 {
942     CPUBreakpoint *bp;
943 
944     if (unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) {
945         QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
946             if (bp->pc == pc && (bp->flags & mask)) {
947                 return true;
948             }
949         }
950     }
951     return false;
952 }
953 
954 #if defined(CONFIG_USER_ONLY)
955 static inline int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
956                                         int flags, CPUWatchpoint **watchpoint)
957 {
958     return -ENOSYS;
959 }
960 
961 static inline int cpu_watchpoint_remove(CPUState *cpu, vaddr addr,
962                                         vaddr len, int flags)
963 {
964     return -ENOSYS;
965 }
966 
967 static inline void cpu_watchpoint_remove_by_ref(CPUState *cpu,
968                                                 CPUWatchpoint *wp)
969 {
970 }
971 
972 static inline void cpu_watchpoint_remove_all(CPUState *cpu, int mask)
973 {
974 }
975 #else
976 int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
977                           int flags, CPUWatchpoint **watchpoint);
978 int cpu_watchpoint_remove(CPUState *cpu, vaddr addr,
979                           vaddr len, int flags);
980 void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint);
981 void cpu_watchpoint_remove_all(CPUState *cpu, int mask);
982 #endif
983 
984 /**
985  * cpu_get_address_space:
986  * @cpu: CPU to get address space from
987  * @asidx: index identifying which address space to get
988  *
989  * Return the requested address space of this CPU. @asidx
990  * specifies which address space to read.
991  */
992 AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx);
993 
994 G_NORETURN void cpu_abort(CPUState *cpu, const char *fmt, ...)
995     G_GNUC_PRINTF(2, 3);
996 
997 /* $(top_srcdir)/cpu.c */
998 void cpu_class_init_props(DeviceClass *dc);
999 void cpu_exec_initfn(CPUState *cpu);
1000 void cpu_exec_realizefn(CPUState *cpu, Error **errp);
1001 void cpu_exec_unrealizefn(CPUState *cpu);
1002 
1003 /**
1004  * target_words_bigendian:
1005  * Returns true if the (default) endianness of the target is big endian,
1006  * false otherwise. Note that in target-specific code, you can use
1007  * TARGET_BIG_ENDIAN directly instead. On the other hand, common
1008  * code should normally never need to know about the endianness of the
1009  * target, so please do *not* use this function unless you know very well
1010  * what you are doing!
1011  */
1012 bool target_words_bigendian(void);
1013 
1014 const char *target_name(void);
1015 
1016 void page_size_init(void);
1017 
1018 #ifdef NEED_CPU_H
1019 
1020 #ifndef CONFIG_USER_ONLY
1021 
1022 extern const VMStateDescription vmstate_cpu_common;
1023 
1024 #define VMSTATE_CPU() {                                                     \
1025     .name = "parent_obj",                                                   \
1026     .size = sizeof(CPUState),                                               \
1027     .vmsd = &vmstate_cpu_common,                                            \
1028     .flags = VMS_STRUCT,                                                    \
1029     .offset = 0,                                                            \
1030 }
1031 #endif /* !CONFIG_USER_ONLY */
1032 
1033 #endif /* NEED_CPU_H */
1034 
1035 #define UNASSIGNED_CPU_INDEX -1
1036 #define UNASSIGNED_CLUSTER_INDEX -1
1037 
1038 #endif
1039