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