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