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