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