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