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