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