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