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