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