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