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