xref: /openbmc/qemu/include/hw/core/cpu.h (revision dcc6d1d956ff6e5ab8e757b37bb00cc6b421630d)
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/breakpoint.h"
26 #include "exec/hwaddr.h"
27 #include "exec/vaddr.h"
28 #include "exec/memattrs.h"
29 #include "exec/mmu-access-type.h"
30 #include "exec/tlb-common.h"
31 #include "qapi/qapi-types-machine.h"
32 #include "qapi/qapi-types-run-state.h"
33 #include "qemu/bitmap.h"
34 #include "qemu/rcu_queue.h"
35 #include "qemu/queue.h"
36 #include "qemu/lockcnt.h"
37 #include "qemu/thread.h"
38 #include "qom/object.h"
39 
40 typedef int (*WriteCoreDumpFunction)(const void *buf, size_t size,
41                                      void *opaque);
42 
43 /**
44  * SECTION:cpu
45  * @section_id: QEMU-cpu
46  * @title: CPU Class
47  * @short_description: Base class for all CPUs
48  */
49 
50 #define TYPE_CPU "cpu"
51 
52 /* Since this macro is used a lot in hot code paths and in conjunction with
53  * FooCPU *foo_env_get_cpu(), we deviate from usual QOM practice by using
54  * an unchecked cast.
55  */
56 #define CPU(obj) ((CPUState *)(obj))
57 
58 /*
59  * The class checkers bring in CPU_GET_CLASS() which is potentially
60  * expensive given the eventual call to
61  * object_class_dynamic_cast_assert(). Because of this the CPUState
62  * has a cached value for the class in cs->cc which is set up in
63  * cpu_exec_realizefn() for use in hot code paths.
64  */
65 typedef struct CPUClass CPUClass;
66 DECLARE_CLASS_CHECKERS(CPUClass, CPU,
67                        TYPE_CPU)
68 
69 /**
70  * OBJECT_DECLARE_CPU_TYPE:
71  * @CpuInstanceType: instance struct name
72  * @CpuClassType: class struct name
73  * @CPU_MODULE_OBJ_NAME: the CPU name in uppercase with underscore separators
74  *
75  * This macro is typically used in "cpu-qom.h" header file, and will:
76  *
77  *   - create the typedefs for the CPU object and class structs
78  *   - register the type for use with g_autoptr
79  *   - provide three standard type cast functions
80  *
81  * The object struct and class struct need to be declared manually.
82  */
83 #define OBJECT_DECLARE_CPU_TYPE(CpuInstanceType, CpuClassType, CPU_MODULE_OBJ_NAME) \
84     typedef struct ArchCPU CpuInstanceType; \
85     OBJECT_DECLARE_TYPE(ArchCPU, CpuClassType, CPU_MODULE_OBJ_NAME);
86 
87 typedef struct CPUWatchpoint CPUWatchpoint;
88 
89 /* see physmem.c */
90 struct CPUAddressSpace;
91 
92 /* see accel/tcg/tb-jmp-cache.h */
93 struct CPUJumpCache;
94 
95 /* see accel-cpu.h */
96 struct AccelCPUClass;
97 
98 /* see sysemu-cpu-ops.h */
99 struct SysemuCPUOps;
100 
101 /**
102  * CPUClass:
103  * @class_by_name: Callback to map -cpu command line model name to an
104  *                 instantiatable CPU type.
105  * @list_cpus: list available CPU models and flags.
106  * @parse_features: Callback to parse command line arguments.
107  * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
108  * @memory_rw_debug: Callback for GDB memory access.
109  * @dump_state: Callback for dumping state.
110  * @query_cpu_fast:
111  *       Fill in target specific information for the "query-cpus-fast"
112  *       QAPI call.
113  * @get_arch_id: Callback for getting architecture-dependent CPU ID.
114  * @set_pc: Callback for setting the Program Counter register. This
115  *       should have the semantics used by the target architecture when
116  *       setting the PC from a source such as an ELF file entry point;
117  *       for example on Arm it will also set the Thumb mode bit based
118  *       on the least significant bit of the new PC value.
119  *       If the target behaviour here is anything other than "set
120  *       the PC register to the value passed in" then the target must
121  *       also implement the synchronize_from_tb hook.
122  * @get_pc: Callback for getting the Program Counter register.
123  *       As above, with the semantics of the target architecture.
124  * @gdb_read_register: Callback for letting GDB read a register.
125  *                     No more than @gdb_num_core_regs registers can be read.
126  * @gdb_write_register: Callback for letting GDB write a register.
127  *                     No more than @gdb_num_core_regs registers can be written.
128  * @gdb_adjust_breakpoint: Callback for adjusting the address of a
129  *       breakpoint.  Used by AVR to handle a gdb mis-feature with
130  *       its Harvard architecture split code and data.
131  * @gdb_num_core_regs: Number of core registers accessible to GDB or 0 to infer
132  *                     from @gdb_core_xml_file.
133  * @gdb_core_xml_file: File name for core registers GDB XML description.
134  * @gdb_get_core_xml_file: Optional callback that returns the file name for
135  * the core registers GDB XML description. The returned value is expected to
136  * be a simple constant string: the caller will not g_free() it. If this
137  * is NULL then @gdb_core_xml_file will be used instead.
138  * @gdb_stop_before_watchpoint: Indicates whether GDB expects the CPU to stop
139  *           before the insn which triggers a watchpoint rather than after it.
140  * @gdb_arch_name: Optional callback that returns the architecture name known
141  * to GDB. The returned value is expected to be a simple constant string:
142  * the caller will not g_free() it.
143  * @disas_set_info: Setup architecture specific components of disassembly info
144  * @adjust_watchpoint_address: Perform a target-specific adjustment to an
145  * address before attempting to match it against watchpoints.
146  * @deprecation_note: If this CPUClass is deprecated, this field provides
147  *                    related information.
148  *
149  * Represents a CPU family or model.
150  */
151 struct CPUClass {
152     /*< private >*/
153     DeviceClass parent_class;
154     /*< public >*/
155 
156     ObjectClass *(*class_by_name)(const char *cpu_model);
157     void (*list_cpus)(void);
158     void (*parse_features)(const char *typename, char *str, Error **errp);
159 
160     int (*memory_rw_debug)(CPUState *cpu, vaddr addr,
161                            uint8_t *buf, size_t len, bool is_write);
162     void (*dump_state)(CPUState *cpu, FILE *, int flags);
163     void (*query_cpu_fast)(CPUState *cpu, CpuInfoFast *value);
164     int64_t (*get_arch_id)(CPUState *cpu);
165     void (*set_pc)(CPUState *cpu, vaddr value);
166     vaddr (*get_pc)(CPUState *cpu);
167     int (*gdb_read_register)(CPUState *cpu, GByteArray *buf, int reg);
168     int (*gdb_write_register)(CPUState *cpu, uint8_t *buf, int reg);
169     vaddr (*gdb_adjust_breakpoint)(CPUState *cpu, vaddr addr);
170 
171     const char *gdb_core_xml_file;
172     const char * (*gdb_arch_name)(CPUState *cpu);
173     const char * (*gdb_get_core_xml_file)(CPUState *cpu);
174 
175     void (*disas_set_info)(CPUState *cpu, disassemble_info *info);
176 
177     const char *deprecation_note;
178     struct AccelCPUClass *accel_cpu;
179 
180     /* when system emulation is not available, this pointer is NULL */
181     const struct SysemuCPUOps *sysemu_ops;
182 
183     /* when TCG is not available, this pointer is NULL */
184     const TCGCPUOps *tcg_ops;
185 
186     /*
187      * if not NULL, this is called in order for the CPUClass to initialize
188      * class data that depends on the accelerator, see accel/accel-common.c.
189      */
190     void (*init_accel_cpu)(struct AccelCPUClass *accel_cpu, CPUClass *cc);
191 
192     /*
193      * Keep non-pointer data at the end to minimize holes.
194      */
195     int reset_dump_flags;
196     int gdb_num_core_regs;
197     bool gdb_stop_before_watchpoint;
198 };
199 
200 /*
201  * Fix the number of mmu modes across all targets.
202  * Current maximum is target/arm/.
203  */
204 #define NB_MMU_MODES 22
205 typedef uint32_t MMUIdxMap;
206 
207 /* Use a fully associative victim tlb of 8 entries. */
208 #define CPU_VTLB_SIZE 8
209 
210 /*
211  * The full TLB entry, which is not accessed by generated TCG code,
212  * so the layout is not as critical as that of CPUTLBEntry. This is
213  * also why we don't want to combine the two structs.
214  */
215 struct CPUTLBEntryFull {
216     /*
217      * @xlat_section contains:
218      *  - in the lower TARGET_PAGE_BITS, a physical section number
219      *  - with the lower TARGET_PAGE_BITS masked off, an offset which
220      *    must be added to the virtual address to obtain:
221      *     + the ram_addr_t of the target RAM (if the physical section
222      *       number is PHYS_SECTION_NOTDIRTY or PHYS_SECTION_ROM)
223      *     + the offset within the target MemoryRegion (otherwise)
224      */
225     hwaddr xlat_section;
226 
227     /*
228      * @phys_addr contains the physical address in the address space
229      * given by cpu_asidx_from_attrs(cpu, @attrs).
230      */
231     hwaddr phys_addr;
232 
233     /* @attrs contains the memory transaction attributes for the page. */
234     MemTxAttrs attrs;
235 
236     /* @prot contains the complete protections for the page. */
237     uint8_t prot;
238 
239     /* @lg_page_size contains the log2 of the page size. */
240     uint8_t lg_page_size;
241 
242     /* Additional tlb flags requested by tlb_fill. */
243     uint8_t tlb_fill_flags;
244 
245     /*
246      * Additional tlb flags for use by the slow path. If non-zero,
247      * the corresponding CPUTLBEntry comparator must have TLB_FORCE_SLOW.
248      */
249     uint8_t slow_flags[MMU_ACCESS_COUNT];
250 
251     /*
252      * Allow target-specific additions to this structure.
253      * This may be used to cache items from the guest cpu
254      * page tables for later use by the implementation.
255      */
256     union {
257         /*
258          * Cache the attrs and shareability fields from the page table entry.
259          *
260          * For ARMMMUIdx_Stage2*, pte_attrs is the S2 descriptor bits [5:2].
261          * Otherwise, pte_attrs is the same as the MAIR_EL1 8-bit format.
262          * For shareability and guarded, as in the SH and GP fields respectively
263          * of the VMSAv8-64 PTEs.
264          */
265         struct {
266             uint8_t pte_attrs;
267             uint8_t shareability;
268             bool guarded;
269         } arm;
270     } extra;
271 };
272 
273 /*
274  * Data elements that are per MMU mode, minus the bits accessed by
275  * the TCG fast path.
276  */
277 typedef struct CPUTLBDesc {
278     /*
279      * Describe a region covering all of the large pages allocated
280      * into the tlb.  When any page within this region is flushed,
281      * we must flush the entire tlb.  The region is matched if
282      * (addr & large_page_mask) == large_page_addr.
283      */
284     vaddr large_page_addr;
285     vaddr large_page_mask;
286     /* host time (in ns) at the beginning of the time window */
287     int64_t window_begin_ns;
288     /* maximum number of entries observed in the window */
289     size_t window_max_entries;
290     size_t n_used_entries;
291     /* The next index to use in the tlb victim table.  */
292     size_t vindex;
293     /* The tlb victim table, in two parts.  */
294     CPUTLBEntry vtable[CPU_VTLB_SIZE];
295     CPUTLBEntryFull vfulltlb[CPU_VTLB_SIZE];
296     CPUTLBEntryFull *fulltlb;
297 } CPUTLBDesc;
298 
299 /*
300  * Data elements that are shared between all MMU modes.
301  */
302 typedef struct CPUTLBCommon {
303     /* Serialize updates to f.table and d.vtable, and others as noted. */
304     QemuSpin lock;
305     /*
306      * Within dirty, for each bit N, modifications have been made to
307      * mmu_idx N since the last time that mmu_idx was flushed.
308      * Protected by tlb_c.lock.
309      */
310     MMUIdxMap dirty;
311     /*
312      * Statistics.  These are not lock protected, but are read and
313      * written atomically.  This allows the monitor to print a snapshot
314      * of the stats without interfering with the cpu.
315      */
316     size_t full_flush_count;
317     size_t part_flush_count;
318     size_t elide_flush_count;
319 } CPUTLBCommon;
320 
321 /*
322  * The entire softmmu tlb, for all MMU modes.
323  * The meaning of each of the MMU modes is defined in the target code.
324  * Since this is placed within CPUNegativeOffsetState, the smallest
325  * negative offsets are at the end of the struct.
326  */
327 typedef struct CPUTLB {
328 #ifdef CONFIG_TCG
329     CPUTLBCommon c;
330     CPUTLBDesc d[NB_MMU_MODES];
331     CPUTLBDescFast f[NB_MMU_MODES];
332 #endif
333 } CPUTLB;
334 
335 /*
336  * Low 16 bits: number of cycles left, used only in icount mode.
337  * High 16 bits: Set to -1 to force TCG to stop executing linked TBs
338  * for this CPU and return to its top level loop (even in non-icount mode).
339  * This allows a single read-compare-cbranch-write sequence to test
340  * for both decrementer underflow and exceptions.
341  */
342 typedef union IcountDecr {
343     uint32_t u32;
344     struct {
345 #if HOST_BIG_ENDIAN
346         uint16_t high;
347         uint16_t low;
348 #else
349         uint16_t low;
350         uint16_t high;
351 #endif
352     } u16;
353 } IcountDecr;
354 
355 /**
356  * CPUNegativeOffsetState: Elements of CPUState most efficiently accessed
357  *                         from CPUArchState, via small negative offsets.
358  * @can_do_io: True if memory-mapped IO is allowed.
359  * @plugin_mem_cbs: active plugin memory callbacks
360  * @plugin_mem_value_low: 64 lower bits of latest accessed mem value.
361  * @plugin_mem_value_high: 64 higher bits of latest accessed mem value.
362  */
363 typedef struct CPUNegativeOffsetState {
364     CPUTLB tlb;
365 #ifdef CONFIG_PLUGIN
366     /*
367      * The callback pointer are accessed via TCG (see gen_empty_mem_helper).
368      */
369     GArray *plugin_mem_cbs;
370     uint64_t plugin_mem_value_low;
371     uint64_t plugin_mem_value_high;
372     int32_t plugin_cb_flags;
373 #endif
374     IcountDecr icount_decr;
375     bool can_do_io;
376 } CPUNegativeOffsetState;
377 
378 struct KVMState;
379 struct kvm_run;
380 
381 /* work queue */
382 
383 /* The union type allows passing of 64 bit target pointers on 32 bit
384  * hosts in a single parameter
385  */
386 typedef union {
387     int           host_int;
388     unsigned long host_ulong;
389     void         *host_ptr;
390     vaddr         target_ptr;
391 } run_on_cpu_data;
392 
393 #define RUN_ON_CPU_HOST_PTR(p)    ((run_on_cpu_data){.host_ptr = (p)})
394 #define RUN_ON_CPU_HOST_INT(i)    ((run_on_cpu_data){.host_int = (i)})
395 #define RUN_ON_CPU_HOST_ULONG(ul) ((run_on_cpu_data){.host_ulong = (ul)})
396 #define RUN_ON_CPU_TARGET_PTR(v)  ((run_on_cpu_data){.target_ptr = (v)})
397 #define RUN_ON_CPU_NULL           RUN_ON_CPU_HOST_PTR(NULL)
398 
399 typedef void (*run_on_cpu_func)(CPUState *cpu, run_on_cpu_data data);
400 
401 struct qemu_work_item;
402 
403 #define CPU_UNSET_NUMA_NODE_ID -1
404 
405 /**
406  * struct CPUState - common state of one CPU core or thread.
407  *
408  * @cpu_index: CPU index (informative).
409  * @cluster_index: Identifies which cluster this CPU is in.
410  *   For boards which don't define clusters or for "loose" CPUs not assigned
411  *   to a cluster this will be UNASSIGNED_CLUSTER_INDEX; otherwise it will
412  *   be the same as the cluster-id property of the CPU object's TYPE_CPU_CLUSTER
413  *   QOM parent.
414  *   Under TCG this value is propagated to @tcg_cflags.
415  *   See TranslationBlock::TCG CF_CLUSTER_MASK.
416  * @tcg_cflags: Pre-computed cflags for this cpu.
417  * @nr_threads: Number of threads within this CPU core.
418  * @thread: Host thread details, only live once @created is #true
419  * @sem: WIN32 only semaphore used only for qtest
420  * @thread_id: native thread id of vCPU, only live once @created is #true
421  * @running: #true if CPU is currently running (lockless).
422  * @has_waiter: #true if a CPU is currently waiting for the cpu_exec_end;
423  * valid under cpu_list_lock.
424  * @created: Indicates whether the CPU thread has been successfully created.
425  * @halt_cond: condition variable sleeping threads can wait on.
426  * @exit_request: Another thread requests the CPU to call qemu_process_cpu_events().
427  *   Should be read only by CPU thread with load-acquire, to synchronize with
428  *   other threads' store-release operation.
429  *
430  *   In some cases, accelerator-specific code will write exit_request from
431  *   within the same thread, to "bump" the effect of qemu_cpu_kick() to
432  *   the one provided by cpu_exit(), especially when processing interrupt
433  *   flags.  In this case, the write and read happen in the same thread
434  *   and the write therefore can use qemu_atomic_set().
435  * @interrupt_request: Indicates a pending interrupt request.
436  *   Only used by system emulation.
437  * @halted: Nonzero if the CPU is in suspended state.
438  * @stop: Indicates a pending stop request.
439  * @stopped: Indicates the CPU has been artificially stopped.
440  * @unplug: Indicates a pending CPU unplug request.
441  * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU
442  * @singlestep_enabled: Flags for single-stepping.
443  * @icount_extra: Instructions until next timer event.
444  * @cpu_ases: Pointer to array of CPUAddressSpaces (which define the
445  *            AddressSpaces this CPU has)
446  * @num_ases: number of CPUAddressSpaces in @cpu_ases
447  * @as: Pointer to the first AddressSpace, for the convenience of targets which
448  *      only have a single AddressSpace
449  * @gdb_regs: Additional GDB registers.
450  * @gdb_num_regs: Number of total registers accessible to GDB.
451  * @gdb_num_g_regs: Number of registers in GDB 'g' packets.
452  * @node: QTAILQ of CPUs sharing TB cache.
453  * @opaque: User data.
454  * @mem_io_pc: Host Program Counter at which the memory was accessed.
455  * @accel: Pointer to accelerator specific state.
456  * @vcpu_dirty: Hardware accelerator is not synchronized with QEMU state
457  * @kvm_fd: vCPU file descriptor for KVM.
458  * @work_mutex: Lock to prevent multiple access to @work_list.
459  * @work_list: List of pending asynchronous work.
460  * @plugin_state: per-CPU plugin state
461  * @ignore_memory_transaction_failures: Cached copy of the MachineState
462  *    flag of the same name: allows the board to suppress calling of the
463  *    CPU do_transaction_failed hook function.
464  * @kvm_dirty_gfns: Points to the KVM dirty ring for this CPU when KVM dirty
465  *    ring is enabled.
466  * @kvm_fetch_index: Keeps the index that we last fetched from the per-vCPU
467  *    dirty ring structure.
468  *
469  * @neg_align: The CPUState is the common part of a concrete ArchCPU
470  * which is allocated when an individual CPU instance is created. As
471  * such care is taken is ensure there is no gap between between
472  * CPUState and CPUArchState within ArchCPU.
473  *
474  * @neg: The architectural register state ("cpu_env") immediately follows
475  * CPUState in ArchCPU and is passed to TCG code. The @neg structure holds
476  * some common TCG CPU variables which are accessed with a negative offset
477  * from cpu_env.
478  */
479 struct CPUState {
480     /*< private >*/
481     DeviceState parent_obj;
482     /* cache to avoid expensive CPU_GET_CLASS */
483     CPUClass *cc;
484     /*< public >*/
485 
486     int nr_threads;
487 
488     struct QemuThread *thread;
489 #ifdef _WIN32
490     QemuSemaphore sem;
491 #endif
492     int thread_id;
493     bool running, has_waiter;
494     struct QemuCond *halt_cond;
495     bool thread_kicked;
496     bool created;
497     bool stop;
498     bool stopped;
499 
500     /* Should CPU start in powered-off state? */
501     bool start_powered_off;
502 
503     bool unplug;
504     bool crash_occurred;
505     bool exit_request;
506     int exclusive_context_count;
507     uint32_t cflags_next_tb;
508     uint32_t interrupt_request;
509     int singlestep_enabled;
510     int64_t icount_budget;
511     int64_t icount_extra;
512     uint64_t random_seed;
513     sigjmp_buf jmp_env;
514 
515     QemuMutex work_mutex;
516     QSIMPLEQ_HEAD(, qemu_work_item) work_list;
517 
518     struct CPUAddressSpace *cpu_ases;
519     int num_ases;
520     AddressSpace *as;
521     MemoryRegion *memory;
522 
523     struct CPUJumpCache *tb_jmp_cache;
524 
525     GArray *gdb_regs;
526     int gdb_num_regs;
527     int gdb_num_g_regs;
528     QTAILQ_ENTRY(CPUState) node;
529 
530     /* ice debug support */
531     QTAILQ_HEAD(, CPUBreakpoint) breakpoints;
532 
533     QTAILQ_HEAD(, CPUWatchpoint) watchpoints;
534     CPUWatchpoint *watchpoint_hit;
535 
536     void *opaque;
537 
538     /* In order to avoid passing too many arguments to the MMIO helpers,
539      * we store some rarely used information in the CPU context.
540      */
541     uintptr_t mem_io_pc;
542 
543     /* Only used in KVM */
544     int kvm_fd;
545     struct KVMState *kvm_state;
546     struct kvm_run *kvm_run;
547     struct kvm_dirty_gfn *kvm_dirty_gfns;
548     uint32_t kvm_fetch_index;
549     uint64_t dirty_pages;
550     int kvm_vcpu_stats_fd;
551 
552     /* Use by accel-block: CPU is executing an ioctl() */
553     QemuLockCnt in_ioctl_lock;
554 
555 #ifdef CONFIG_PLUGIN
556     CPUPluginState *plugin_state;
557 #endif
558 
559     /* TODO Move common fields from CPUArchState here. */
560     int cpu_index;
561     int cluster_index;
562     uint32_t tcg_cflags;
563     uint32_t halted;
564     int32_t exception_index;
565 
566     bool vcpu_dirty;
567     AccelCPUState *accel;
568 
569     /* Used to keep track of an outstanding cpu throttle thread for migration
570      * autoconverge
571      */
572     bool throttle_thread_scheduled;
573 
574     /*
575      * Sleep throttle_us_per_full microseconds once dirty ring is full
576      * if dirty page rate limit is enabled.
577      */
578     int64_t throttle_us_per_full;
579 
580     bool ignore_memory_transaction_failures;
581 
582     /* Used for user-only emulation of prctl(PR_SET_UNALIGN). */
583     bool prctl_unalign_sigbus;
584 
585     /* track IOMMUs whose translations we've cached in the TCG TLB */
586     GArray *iommu_notifiers;
587 
588     /*
589      * MUST BE LAST in order to minimize the displacement to CPUArchState.
590      */
591     char neg_align[-sizeof(CPUNegativeOffsetState) % 16] QEMU_ALIGNED(16);
592     CPUNegativeOffsetState neg;
593 };
594 
595 /* Validate placement of CPUNegativeOffsetState. */
596 QEMU_BUILD_BUG_ON(offsetof(CPUState, neg) !=
597                   sizeof(CPUState) - sizeof(CPUNegativeOffsetState));
598 
599 static inline CPUArchState *cpu_env(CPUState *cpu)
600 {
601     /* We validate that CPUArchState follows CPUState in cpu-target.c */
602     return (CPUArchState *)(cpu + 1);
603 }
604 
605 #ifdef CONFIG_TCG
606 /*
607  * Invert the index order of the CPUTLBDescFast array so that lower
608  * mmu_idx have offsets from env with smaller magnitude.
609  */
610 static inline int mmuidx_to_fast_index(int mmu_idx)
611 {
612     return NB_MMU_MODES - 1 - mmu_idx;
613 }
614 
615 static inline CPUTLBDescFast *cpu_tlb_fast(CPUState *cpu, int mmu_idx)
616 {
617     return &cpu->neg.tlb.f[mmuidx_to_fast_index(mmu_idx)];
618 }
619 #endif
620 
621 typedef QTAILQ_HEAD(CPUTailQ, CPUState) CPUTailQ;
622 extern CPUTailQ cpus_queue;
623 
624 #define first_cpu        QTAILQ_FIRST_RCU(&cpus_queue)
625 #define CPU_NEXT(cpu)    QTAILQ_NEXT_RCU(cpu, node)
626 #define CPU_FOREACH(cpu) QTAILQ_FOREACH_RCU(cpu, &cpus_queue, node)
627 #define CPU_FOREACH_SAFE(cpu, next_cpu) \
628     QTAILQ_FOREACH_SAFE_RCU(cpu, &cpus_queue, node, next_cpu)
629 
630 extern __thread CPUState *current_cpu;
631 
632 /**
633  * cpu_paging_enabled:
634  * @cpu: The CPU whose state is to be inspected.
635  *
636  * Returns: %true if paging is enabled, %false otherwise.
637  */
638 bool cpu_paging_enabled(const CPUState *cpu);
639 
640 /**
641  * cpu_get_memory_mapping:
642  * @cpu: The CPU whose memory mappings are to be obtained.
643  * @list: Where to write the memory mappings to.
644  * @errp: Pointer for reporting an #Error.
645  *
646  * Returns: %true on success, %false otherwise.
647  */
648 bool cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
649                             Error **errp);
650 
651 /**
652  * cpu_write_elf64_note:
653  * @f: pointer to a function that writes memory to a file
654  * @cpu: The CPU whose memory is to be dumped
655  * @cpuid: ID number of the CPU
656  * @opaque: pointer to the CPUState struct
657  */
658 int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
659                          int cpuid, void *opaque);
660 
661 /**
662  * cpu_write_elf64_qemunote:
663  * @f: pointer to a function that writes memory to a file
664  * @cpu: The CPU whose memory is to be dumped
665  * @cpuid: ID number of the CPU
666  * @opaque: pointer to the CPUState struct
667  */
668 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
669                              void *opaque);
670 
671 /**
672  * cpu_write_elf32_note:
673  * @f: pointer to a function that writes memory to a file
674  * @cpu: The CPU whose memory is to be dumped
675  * @cpuid: ID number of the CPU
676  * @opaque: pointer to the CPUState struct
677  */
678 int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
679                          int cpuid, void *opaque);
680 
681 /**
682  * cpu_write_elf32_qemunote:
683  * @f: pointer to a function that writes memory to a file
684  * @cpu: The CPU whose memory is to be dumped
685  * @cpuid: ID number of the CPU
686  * @opaque: pointer to the CPUState struct
687  */
688 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
689                              void *opaque);
690 
691 /**
692  * cpu_memory_rw_debug:
693  * @cpu: The CPU whose memory is to be accessed
694  * @addr: guest virtual address
695  * @ptr: buffer with the data transferred
696  * @len: the number of bytes to read or write
697  * @is_write: indicates the transfer direction
698  *
699  * Take a virtual address, convert it to a physical address via
700  * an MMU lookup using the current settings of the specified CPU,
701  * and then perform the access (using address_space_rw() for
702  * reads or address_space_write_rom() for writes).
703  *
704  * This function is intended for use by the GDB stub and similar code.
705  *
706  * Returns: 0 on success, -1 on error
707  */
708 int cpu_memory_rw_debug(CPUState *cpu, vaddr addr,
709                         void *ptr, size_t len, bool is_write);
710 
711 /**
712  * cpu_get_crash_info:
713  * @cpu: The CPU to get crash information for
714  *
715  * Gets the previously saved crash information.
716  * Caller is responsible for freeing the data.
717  */
718 GuestPanicInformation *cpu_get_crash_info(CPUState *cpu);
719 
720 /**
721  * CPUDumpFlags:
722  * @CPU_DUMP_CODE:
723  * @CPU_DUMP_FPU: dump FPU register state, not just integer
724  * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
725  * @CPU_DUMP_VPU: dump VPU registers
726  */
727 enum CPUDumpFlags {
728     CPU_DUMP_CODE = 0x00010000,
729     CPU_DUMP_FPU  = 0x00020000,
730     CPU_DUMP_CCOP = 0x00040000,
731     CPU_DUMP_VPU  = 0x00080000,
732 };
733 
734 /**
735  * cpu_dump_state:
736  * @cpu: The CPU whose state is to be dumped.
737  * @f: If non-null, dump to this stream, else to current print sink.
738  *
739  * Dumps CPU state.
740  */
741 void cpu_dump_state(CPUState *cpu, FILE *f, int flags);
742 
743 /**
744  * cpu_get_phys_page_attrs_debug:
745  * @cpu: The CPU to obtain the physical page address for.
746  * @addr: The virtual address.
747  * @attrs: Updated on return with the memory transaction attributes to use
748  *         for this access.
749  *
750  * Obtains the physical page corresponding to a virtual one, together
751  * with the corresponding memory transaction attributes to use for the access.
752  * Use it only for debugging because no protection checks are done.
753  *
754  * Returns: Corresponding physical page address or -1 if no page found.
755  */
756 hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr,
757                                      MemTxAttrs *attrs);
758 
759 /**
760  * cpu_get_phys_page_debug:
761  * @cpu: The CPU to obtain the physical page address for.
762  * @addr: The virtual address.
763  *
764  * Obtains the physical page corresponding to a virtual one.
765  * Use it only for debugging because no protection checks are done.
766  *
767  * Returns: Corresponding physical page address or -1 if no page found.
768  */
769 hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr);
770 
771 /** cpu_asidx_from_attrs:
772  * @cpu: CPU
773  * @attrs: memory transaction attributes
774  *
775  * Returns the address space index specifying the CPU AddressSpace
776  * to use for a memory access with the given transaction attributes.
777  */
778 int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs);
779 
780 /**
781  * cpu_virtio_is_big_endian:
782  * @cpu: CPU
783 
784  * Returns %true if a CPU which supports runtime configurable endianness
785  * is currently big-endian.
786  */
787 bool cpu_virtio_is_big_endian(CPUState *cpu);
788 
789 /**
790  * cpu_has_work:
791  * @cpu: The vCPU to check.
792  *
793  * Checks whether the CPU has work to do.
794  *
795  * Returns: %true if the CPU has work, %false otherwise.
796  */
797 bool cpu_has_work(CPUState *cpu);
798 
799 /**
800  * cpu_list_add:
801  * @cpu: The CPU to be added to the list of CPUs.
802  */
803 void cpu_list_add(CPUState *cpu);
804 
805 /**
806  * cpu_list_remove:
807  * @cpu: The CPU to be removed from the list of CPUs.
808  */
809 void cpu_list_remove(CPUState *cpu);
810 
811 /**
812  * cpu_reset:
813  * @cpu: The CPU whose state is to be reset.
814  */
815 void cpu_reset(CPUState *cpu);
816 
817 /**
818  * cpu_class_by_name:
819  * @typename: The CPU base type.
820  * @cpu_model: The model string without any parameters.
821  *
822  * Looks up a concrete CPU #ObjectClass matching name @cpu_model.
823  *
824  * Returns: A concrete #CPUClass or %NULL if no matching class is found
825  *          or if the matching class is abstract.
826  */
827 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
828 
829 /**
830  * cpu_model_from_type:
831  * @typename: The CPU type name
832  *
833  * Extract the CPU model name from the CPU type name. The
834  * CPU type name is either the combination of the CPU model
835  * name and suffix, or same to the CPU model name.
836  *
837  * Returns: CPU model name or NULL if the CPU class doesn't exist
838  *          The user should g_free() the string once no longer needed.
839  */
840 char *cpu_model_from_type(const char *typename);
841 
842 /**
843  * cpu_create:
844  * @typename: The CPU type.
845  *
846  * Instantiates a CPU and realizes the CPU.
847  *
848  * Returns: A #CPUState or %NULL if an error occurred.
849  */
850 CPUState *cpu_create(const char *typename);
851 
852 /**
853  * parse_cpu_option:
854  * @cpu_option: The -cpu option including optional parameters.
855  *
856  * processes optional parameters and registers them as global properties
857  *
858  * Returns: type of CPU to create or prints error and terminates process
859  *          if an error occurred.
860  */
861 const char *parse_cpu_option(const char *cpu_option);
862 
863 /**
864  * qemu_cpu_is_self:
865  * @cpu: The vCPU to check against.
866  *
867  * Checks whether the caller is executing on the vCPU thread.
868  *
869  * Returns: %true if called from @cpu's thread, %false otherwise.
870  */
871 bool qemu_cpu_is_self(CPUState *cpu);
872 
873 /**
874  * qemu_cpu_kick:
875  * @cpu: The vCPU to kick.
876  *
877  * Kicks @cpu's thread to exit the accelerator.  For accelerators that
878  * can do that, the target vCPU thread will try not to take the BQL.
879  */
880 void qemu_cpu_kick(CPUState *cpu);
881 
882 /**
883  * cpu_is_stopped:
884  * @cpu: The CPU to check.
885  *
886  * Checks whether the CPU is stopped.
887  *
888  * Returns: %true if run state is not running or if artificially stopped;
889  * %false otherwise.
890  */
891 bool cpu_is_stopped(CPUState *cpu);
892 
893 /**
894  * do_run_on_cpu:
895  * @cpu: The vCPU to run on.
896  * @func: The function to be executed.
897  * @data: Data to pass to the function.
898  * @mutex: Mutex to release while waiting for @func to run.
899  *
900  * Used internally in the implementation of run_on_cpu.
901  */
902 void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data,
903                    QemuMutex *mutex);
904 
905 /**
906  * run_on_cpu:
907  * @cpu: The vCPU to run on.
908  * @func: The function to be executed.
909  * @data: Data to pass to the function.
910  *
911  * Schedules the function @func for execution on the vCPU @cpu.
912  */
913 void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
914 
915 /**
916  * async_run_on_cpu:
917  * @cpu: The vCPU to run on.
918  * @func: The function to be executed.
919  * @data: Data to pass to the function.
920  *
921  * Schedules the function @func for execution on the vCPU @cpu asynchronously.
922  */
923 void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
924 
925 /**
926  * async_safe_run_on_cpu:
927  * @cpu: The vCPU to run on.
928  * @func: The function to be executed.
929  * @data: Data to pass to the function.
930  *
931  * Schedules the function @func for execution on the vCPU @cpu asynchronously,
932  * while all other vCPUs are sleeping.
933  *
934  * Unlike run_on_cpu and async_run_on_cpu, the function is run outside the
935  * BQL.
936  */
937 void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
938 
939 /**
940  * cpu_in_exclusive_context()
941  * @cpu: The vCPU to check
942  *
943  * Returns true if @cpu is an exclusive context, for example running
944  * something which has previously been queued via async_safe_run_on_cpu().
945  */
946 static inline bool cpu_in_exclusive_context(const CPUState *cpu)
947 {
948     return cpu->exclusive_context_count;
949 }
950 
951 /**
952  * qemu_get_cpu:
953  * @index: The CPUState@cpu_index value of the CPU to obtain.
954  *
955  * Gets a CPU matching @index.
956  *
957  * Returns: The CPU or %NULL if there is no matching CPU.
958  */
959 CPUState *qemu_get_cpu(int index);
960 
961 /**
962  * cpu_exists:
963  * @id: Guest-exposed CPU ID to lookup.
964  *
965  * Search for CPU with specified ID.
966  *
967  * Returns: %true - CPU is found, %false - CPU isn't found.
968  */
969 bool cpu_exists(int64_t id);
970 
971 /**
972  * cpu_by_arch_id:
973  * @id: Guest-exposed CPU ID of the CPU to obtain.
974  *
975  * Get a CPU with matching @id.
976  *
977  * Returns: The CPU or %NULL if there is no matching CPU.
978  */
979 CPUState *cpu_by_arch_id(int64_t id);
980 
981 /**
982  * cpu_interrupt:
983  * @cpu: The CPU to set an interrupt on.
984  * @mask: The interrupts to set.
985  *
986  * Invokes the interrupt handler.
987  */
988 
989 void cpu_interrupt(CPUState *cpu, int mask);
990 
991 /**
992  * cpu_test_interrupt:
993  * @cpu: The CPU to check interrupt(s) on.
994  * @mask: The interrupts to check.
995  *
996  * Checks if any of interrupts in @mask are pending on @cpu.
997  */
998 static inline bool cpu_test_interrupt(CPUState *cpu, int mask)
999 {
1000     return qatomic_load_acquire(&cpu->interrupt_request) & mask;
1001 }
1002 
1003 /**
1004  * cpu_set_interrupt:
1005  * @cpu: The CPU to set pending interrupt(s) on.
1006  * @mask: The interrupts to set.
1007  *
1008  * Sets interrupts in @mask as pending on @cpu.  Unlike @cpu_interrupt,
1009  * this does not kick the vCPU.
1010  */
1011 void cpu_set_interrupt(CPUState *cpu, int mask);
1012 
1013 /**
1014  * cpu_set_pc:
1015  * @cpu: The CPU to set the program counter for.
1016  * @addr: Program counter value.
1017  *
1018  * Sets the program counter for a CPU.
1019  */
1020 static inline void cpu_set_pc(CPUState *cpu, vaddr addr)
1021 {
1022     cpu->cc->set_pc(cpu, addr);
1023 }
1024 
1025 /**
1026  * cpu_reset_interrupt:
1027  * @cpu: The CPU to clear the interrupt on.
1028  * @mask: The interrupt mask to clear.
1029  *
1030  * Resets interrupts on the vCPU @cpu.
1031  */
1032 void cpu_reset_interrupt(CPUState *cpu, int mask);
1033 
1034 /**
1035  * cpu_exit:
1036  * @cpu: The CPU to exit.
1037  *
1038  * Requests the CPU @cpu to exit execution.
1039  */
1040 void cpu_exit(CPUState *cpu);
1041 
1042 /**
1043  * cpu_pause:
1044  * @cpu: The CPU to pause.
1045  *
1046  * Pauses CPU, i.e. puts CPU into stopped state.
1047  */
1048 void cpu_pause(CPUState *cpu);
1049 
1050 /**
1051  * cpu_resume:
1052  * @cpu: The CPU to resume.
1053  *
1054  * Resumes CPU, i.e. puts CPU into runnable state.
1055  */
1056 void cpu_resume(CPUState *cpu);
1057 
1058 /**
1059  * cpu_remove_sync:
1060  * @cpu: The CPU to remove.
1061  *
1062  * Requests the CPU to be removed and waits till it is removed.
1063  */
1064 void cpu_remove_sync(CPUState *cpu);
1065 
1066 /**
1067  * free_queued_cpu_work() - free all items on CPU work queue
1068  * @cpu: The CPU which work queue to free.
1069  */
1070 void free_queued_cpu_work(CPUState *cpu);
1071 
1072 /**
1073  * process_queued_cpu_work() - process all items on CPU work queue
1074  * @cpu: The CPU which work queue to process.
1075  */
1076 void process_queued_cpu_work(CPUState *cpu);
1077 
1078 /**
1079  * cpu_exec_start:
1080  * @cpu: The CPU for the current thread.
1081  *
1082  * Record that a CPU has started execution and can be interrupted with
1083  * cpu_exit.
1084  */
1085 void cpu_exec_start(CPUState *cpu);
1086 
1087 /**
1088  * cpu_exec_end:
1089  * @cpu: The CPU for the current thread.
1090  *
1091  * Record that a CPU has stopped execution and exclusive sections
1092  * can be executed without interrupting it.
1093  */
1094 void cpu_exec_end(CPUState *cpu);
1095 
1096 /**
1097  * start_exclusive:
1098  *
1099  * Wait for a concurrent exclusive section to end, and then start
1100  * a section of work that is run while other CPUs are not running
1101  * between cpu_exec_start and cpu_exec_end.  CPUs that are running
1102  * cpu_exec are exited immediately.  CPUs that call cpu_exec_start
1103  * during the exclusive section go to sleep until this CPU calls
1104  * end_exclusive.
1105  */
1106 void start_exclusive(void);
1107 
1108 /**
1109  * end_exclusive:
1110  *
1111  * Concludes an exclusive execution section started by start_exclusive.
1112  */
1113 void end_exclusive(void);
1114 
1115 /**
1116  * qemu_init_vcpu:
1117  * @cpu: The vCPU to initialize.
1118  *
1119  * Initializes a vCPU.
1120  */
1121 void qemu_init_vcpu(CPUState *cpu);
1122 
1123 #define SSTEP_ENABLE  0x1  /* Enable simulated HW single stepping */
1124 #define SSTEP_NOIRQ   0x2  /* Do not use IRQ while single stepping */
1125 #define SSTEP_NOTIMER 0x4  /* Do not Timers while single stepping */
1126 
1127 /**
1128  * cpu_single_step:
1129  * @cpu: CPU to the flags for.
1130  * @enabled: Flags to enable.
1131  *
1132  * Enables or disables single-stepping for @cpu.
1133  */
1134 void cpu_single_step(CPUState *cpu, int enabled);
1135 
1136 /* Breakpoint/watchpoint flags */
1137 #define BP_MEM_READ           0x01
1138 #define BP_MEM_WRITE          0x02
1139 #define BP_MEM_ACCESS         (BP_MEM_READ | BP_MEM_WRITE)
1140 #define BP_STOP_BEFORE_ACCESS 0x04
1141 /* 0x08 currently unused */
1142 #define BP_GDB                0x10
1143 #define BP_CPU                0x20
1144 #define BP_ANY                (BP_GDB | BP_CPU)
1145 #define BP_HIT_SHIFT          6
1146 #define BP_WATCHPOINT_HIT_READ  (BP_MEM_READ << BP_HIT_SHIFT)
1147 #define BP_WATCHPOINT_HIT_WRITE (BP_MEM_WRITE << BP_HIT_SHIFT)
1148 #define BP_WATCHPOINT_HIT       (BP_MEM_ACCESS << BP_HIT_SHIFT)
1149 
1150 int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
1151                           CPUBreakpoint **breakpoint);
1152 int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags);
1153 void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint);
1154 void cpu_breakpoint_remove_all(CPUState *cpu, int mask);
1155 
1156 /* Return true if PC matches an installed breakpoint.  */
1157 static inline bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask)
1158 {
1159     CPUBreakpoint *bp;
1160 
1161     if (unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) {
1162         QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
1163             if (bp->pc == pc && (bp->flags & mask)) {
1164                 return true;
1165             }
1166         }
1167     }
1168     return false;
1169 }
1170 
1171 /**
1172  * cpu_get_address_space:
1173  * @cpu: CPU to get address space from
1174  * @asidx: index identifying which address space to get
1175  *
1176  * Return the requested address space of this CPU. @asidx
1177  * specifies which address space to read.
1178  */
1179 AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx);
1180 
1181 G_NORETURN void cpu_abort(CPUState *cpu, const char *fmt, ...)
1182     G_GNUC_PRINTF(2, 3);
1183 
1184 /**
1185  * qemu_process_cpu_events:
1186  * @cpu: CPU that left the execution loop
1187  *
1188  * Perform accelerator-independent work after the CPU has left
1189  * the inner execution loop.
1190  */
1191 void qemu_process_cpu_events(CPUState *cpu);
1192 
1193 /* $(top_srcdir)/cpu.c */
1194 void cpu_class_init_props(DeviceClass *dc);
1195 void cpu_exec_class_post_init(CPUClass *cc);
1196 void cpu_exec_initfn(CPUState *cpu);
1197 void cpu_vmstate_register(CPUState *cpu);
1198 void cpu_vmstate_unregister(CPUState *cpu);
1199 bool cpu_exec_realizefn(CPUState *cpu, Error **errp);
1200 void cpu_exec_unrealizefn(CPUState *cpu);
1201 void cpu_exec_reset_hold(CPUState *cpu);
1202 
1203 extern const VMStateDescription vmstate_cpu_common;
1204 
1205 #define UNASSIGNED_CPU_INDEX -1
1206 #define UNASSIGNED_CLUSTER_INDEX -1
1207 
1208 enum CacheType {
1209     DATA_CACHE,
1210     INSTRUCTION_CACHE,
1211     UNIFIED_CACHE
1212 };
1213 
1214 #endif
1215