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