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