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