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