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