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