1 /*
2 * QEMU CPU model
3 *
4 * Copyright (c) 2012 SUSE LINUX Products GmbH
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see
18 * <http://www.gnu.org/licenses/gpl-2.0.html>
19 */
20 #ifndef QEMU_CPU_H
21 #define QEMU_CPU_H
22
23 #include "hw/qdev-core.h"
24 #include "disas/dis-asm.h"
25 #include "exec/breakpoint.h"
26 #include "exec/hwaddr.h"
27 #include "exec/vaddr.h"
28 #include "exec/memattrs.h"
29 #include "exec/mmu-access-type.h"
30 #include "exec/tlb-common.h"
31 #include "qapi/qapi-types-machine.h"
32 #include "qapi/qapi-types-run-state.h"
33 #include "qemu/bitmap.h"
34 #include "qemu/rcu_queue.h"
35 #include "qemu/queue.h"
36 #include "qemu/lockcnt.h"
37 #include "qemu/thread.h"
38 #include "qom/object.h"
39
40 typedef int (*WriteCoreDumpFunction)(const void *buf, size_t size,
41 void *opaque);
42
43 /**
44 * SECTION:cpu
45 * @section_id: QEMU-cpu
46 * @title: CPU Class
47 * @short_description: Base class for all CPUs
48 */
49
50 #define TYPE_CPU "cpu"
51
52 /* Since this macro is used a lot in hot code paths and in conjunction with
53 * FooCPU *foo_env_get_cpu(), we deviate from usual QOM practice by using
54 * an unchecked cast.
55 */
56 #define CPU(obj) ((CPUState *)(obj))
57
58 /*
59 * The class checkers bring in CPU_GET_CLASS() which is potentially
60 * expensive given the eventual call to
61 * object_class_dynamic_cast_assert(). Because of this the CPUState
62 * has a cached value for the class in cs->cc which is set up in
63 * cpu_exec_realizefn() for use in hot code paths.
64 */
65 typedef struct CPUClass CPUClass;
66 DECLARE_CLASS_CHECKERS(CPUClass, CPU,
67 TYPE_CPU)
68
69 /**
70 * OBJECT_DECLARE_CPU_TYPE:
71 * @CpuInstanceType: instance struct name
72 * @CpuClassType: class struct name
73 * @CPU_MODULE_OBJ_NAME: the CPU name in uppercase with underscore separators
74 *
75 * This macro is typically used in "cpu-qom.h" header file, and will:
76 *
77 * - create the typedefs for the CPU object and class structs
78 * - register the type for use with g_autoptr
79 * - provide three standard type cast functions
80 *
81 * The object struct and class struct need to be declared manually.
82 */
83 #define OBJECT_DECLARE_CPU_TYPE(CpuInstanceType, CpuClassType, CPU_MODULE_OBJ_NAME) \
84 typedef struct ArchCPU CpuInstanceType; \
85 OBJECT_DECLARE_TYPE(ArchCPU, CpuClassType, CPU_MODULE_OBJ_NAME);
86
87 typedef struct CPUWatchpoint CPUWatchpoint;
88
89 /* see physmem.c */
90 struct CPUAddressSpace;
91
92 /* see accel/tcg/tb-jmp-cache.h */
93 struct CPUJumpCache;
94
95 /* see accel-cpu.h */
96 struct AccelCPUClass;
97
98 /* see sysemu-cpu-ops.h */
99 struct SysemuCPUOps;
100
101 /**
102 * CPUClass:
103 * @class_by_name: Callback to map -cpu command line model name to an
104 * instantiatable CPU type.
105 * @parse_features: Callback to parse command line arguments.
106 * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
107 * @mmu_index: Callback for choosing softmmu mmu index;
108 * may be used internally by memory_rw_debug without TCG.
109 * @memory_rw_debug: Callback for GDB memory access.
110 * @dump_state: Callback for dumping state.
111 * @query_cpu_fast:
112 * Fill in target specific information for the "query-cpus-fast"
113 * QAPI call.
114 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
115 * @set_pc: Callback for setting the Program Counter register. This
116 * should have the semantics used by the target architecture when
117 * setting the PC from a source such as an ELF file entry point;
118 * for example on Arm it will also set the Thumb mode bit based
119 * on the least significant bit of the new PC value.
120 * If the target behaviour here is anything other than "set
121 * the PC register to the value passed in" then the target must
122 * also implement the synchronize_from_tb hook.
123 * @get_pc: Callback for getting the Program Counter register.
124 * As above, with the semantics of the target architecture.
125 * @gdb_read_register: Callback for letting GDB read a register.
126 * No more than @gdb_num_core_regs registers can be read.
127 * @gdb_write_register: Callback for letting GDB write a register.
128 * No more than @gdb_num_core_regs registers can be written.
129 * @gdb_adjust_breakpoint: Callback for adjusting the address of a
130 * breakpoint. Used by AVR to handle a gdb mis-feature with
131 * its Harvard architecture split code and data.
132 * @gdb_num_core_regs: Number of core registers accessible to GDB or 0 to infer
133 * from @gdb_core_xml_file.
134 * @gdb_core_xml_file: File name for core registers GDB XML description.
135 * @gdb_stop_before_watchpoint: Indicates whether GDB expects the CPU to stop
136 * before the insn which triggers a watchpoint rather than after it.
137 * @gdb_arch_name: Optional callback that returns the architecture name known
138 * to GDB. The returned value is expected to be a simple constant string:
139 * the caller will not g_free() it.
140 * @disas_set_info: Setup architecture specific components of disassembly info
141 * @adjust_watchpoint_address: Perform a target-specific adjustment to an
142 * address before attempting to match it against watchpoints.
143 * @deprecation_note: If this CPUClass is deprecated, this field provides
144 * related information.
145 *
146 * Represents a CPU family or model.
147 */
148 struct CPUClass {
149 /*< private >*/
150 DeviceClass parent_class;
151 /*< public >*/
152
153 ObjectClass *(*class_by_name)(const char *cpu_model);
154 void (*parse_features)(const char *typename, char *str, Error **errp);
155
156 int (*mmu_index)(CPUState *cpu, bool ifetch);
157 int (*memory_rw_debug)(CPUState *cpu, vaddr addr,
158 uint8_t *buf, size_t len, bool is_write);
159 void (*dump_state)(CPUState *cpu, FILE *, int flags);
160 void (*query_cpu_fast)(CPUState *cpu, CpuInfoFast *value);
161 int64_t (*get_arch_id)(CPUState *cpu);
162 void (*set_pc)(CPUState *cpu, vaddr value);
163 vaddr (*get_pc)(CPUState *cpu);
164 int (*gdb_read_register)(CPUState *cpu, GByteArray *buf, int reg);
165 int (*gdb_write_register)(CPUState *cpu, uint8_t *buf, int reg);
166 vaddr (*gdb_adjust_breakpoint)(CPUState *cpu, vaddr addr);
167
168 const char *gdb_core_xml_file;
169 const gchar * (*gdb_arch_name)(CPUState *cpu);
170
171 void (*disas_set_info)(CPUState *cpu, disassemble_info *info);
172
173 const char *deprecation_note;
174 struct AccelCPUClass *accel_cpu;
175
176 /* when system emulation is not available, this pointer is NULL */
177 const struct SysemuCPUOps *sysemu_ops;
178
179 /* when TCG is not available, this pointer is NULL */
180 const TCGCPUOps *tcg_ops;
181
182 /*
183 * if not NULL, this is called in order for the CPUClass to initialize
184 * class data that depends on the accelerator, see accel/accel-common.c.
185 */
186 void (*init_accel_cpu)(struct AccelCPUClass *accel_cpu, CPUClass *cc);
187
188 /*
189 * Keep non-pointer data at the end to minimize holes.
190 */
191 int reset_dump_flags;
192 int gdb_num_core_regs;
193 bool gdb_stop_before_watchpoint;
194 };
195
196 /*
197 * Fix the number of mmu modes to 16, which is also the maximum
198 * supported by the softmmu tlb api.
199 */
200 #define NB_MMU_MODES 16
201
202 /* Use a fully associative victim tlb of 8 entries. */
203 #define CPU_VTLB_SIZE 8
204
205 /*
206 * The full TLB entry, which is not accessed by generated TCG code,
207 * so the layout is not as critical as that of CPUTLBEntry. This is
208 * also why we don't want to combine the two structs.
209 */
210 struct CPUTLBEntryFull {
211 /*
212 * @xlat_section contains:
213 * - in the lower TARGET_PAGE_BITS, a physical section number
214 * - with the lower TARGET_PAGE_BITS masked off, an offset which
215 * must be added to the virtual address to obtain:
216 * + the ram_addr_t of the target RAM (if the physical section
217 * number is PHYS_SECTION_NOTDIRTY or PHYS_SECTION_ROM)
218 * + the offset within the target MemoryRegion (otherwise)
219 */
220 hwaddr xlat_section;
221
222 /*
223 * @phys_addr contains the physical address in the address space
224 * given by cpu_asidx_from_attrs(cpu, @attrs).
225 */
226 hwaddr phys_addr;
227
228 /* @attrs contains the memory transaction attributes for the page. */
229 MemTxAttrs attrs;
230
231 /* @prot contains the complete protections for the page. */
232 uint8_t prot;
233
234 /* @lg_page_size contains the log2 of the page size. */
235 uint8_t lg_page_size;
236
237 /* Additional tlb flags requested by tlb_fill. */
238 uint8_t tlb_fill_flags;
239
240 /*
241 * Additional tlb flags for use by the slow path. If non-zero,
242 * the corresponding CPUTLBEntry comparator must have TLB_FORCE_SLOW.
243 */
244 uint8_t slow_flags[MMU_ACCESS_COUNT];
245
246 /*
247 * Allow target-specific additions to this structure.
248 * This may be used to cache items from the guest cpu
249 * page tables for later use by the implementation.
250 */
251 union {
252 /*
253 * Cache the attrs and shareability fields from the page table entry.
254 *
255 * For ARMMMUIdx_Stage2*, pte_attrs is the S2 descriptor bits [5:2].
256 * Otherwise, pte_attrs is the same as the MAIR_EL1 8-bit format.
257 * For shareability and guarded, as in the SH and GP fields respectively
258 * of the VMSAv8-64 PTEs.
259 */
260 struct {
261 uint8_t pte_attrs;
262 uint8_t shareability;
263 bool guarded;
264 } arm;
265 } extra;
266 };
267
268 /*
269 * Data elements that are per MMU mode, minus the bits accessed by
270 * the TCG fast path.
271 */
272 typedef struct CPUTLBDesc {
273 /*
274 * Describe a region covering all of the large pages allocated
275 * into the tlb. When any page within this region is flushed,
276 * we must flush the entire tlb. The region is matched if
277 * (addr & large_page_mask) == large_page_addr.
278 */
279 vaddr large_page_addr;
280 vaddr large_page_mask;
281 /* host time (in ns) at the beginning of the time window */
282 int64_t window_begin_ns;
283 /* maximum number of entries observed in the window */
284 size_t window_max_entries;
285 size_t n_used_entries;
286 /* The next index to use in the tlb victim table. */
287 size_t vindex;
288 /* The tlb victim table, in two parts. */
289 CPUTLBEntry vtable[CPU_VTLB_SIZE];
290 CPUTLBEntryFull vfulltlb[CPU_VTLB_SIZE];
291 CPUTLBEntryFull *fulltlb;
292 } CPUTLBDesc;
293
294 /*
295 * Data elements that are shared between all MMU modes.
296 */
297 typedef struct CPUTLBCommon {
298 /* Serialize updates to f.table and d.vtable, and others as noted. */
299 QemuSpin lock;
300 /*
301 * Within dirty, for each bit N, modifications have been made to
302 * mmu_idx N since the last time that mmu_idx was flushed.
303 * Protected by tlb_c.lock.
304 */
305 uint16_t dirty;
306 /*
307 * Statistics. These are not lock protected, but are read and
308 * written atomically. This allows the monitor to print a snapshot
309 * of the stats without interfering with the cpu.
310 */
311 size_t full_flush_count;
312 size_t part_flush_count;
313 size_t elide_flush_count;
314 } CPUTLBCommon;
315
316 /*
317 * The entire softmmu tlb, for all MMU modes.
318 * The meaning of each of the MMU modes is defined in the target code.
319 * Since this is placed within CPUNegativeOffsetState, the smallest
320 * negative offsets are at the end of the struct.
321 */
322 typedef struct CPUTLB {
323 #ifdef CONFIG_TCG
324 CPUTLBCommon c;
325 CPUTLBDesc d[NB_MMU_MODES];
326 CPUTLBDescFast f[NB_MMU_MODES];
327 #endif
328 } CPUTLB;
329
330 /*
331 * Low 16 bits: number of cycles left, used only in icount mode.
332 * High 16 bits: Set to -1 to force TCG to stop executing linked TBs
333 * for this CPU and return to its top level loop (even in non-icount mode).
334 * This allows a single read-compare-cbranch-write sequence to test
335 * for both decrementer underflow and exceptions.
336 */
337 typedef union IcountDecr {
338 uint32_t u32;
339 struct {
340 #if HOST_BIG_ENDIAN
341 uint16_t high;
342 uint16_t low;
343 #else
344 uint16_t low;
345 uint16_t high;
346 #endif
347 } u16;
348 } IcountDecr;
349
350 /**
351 * CPUNegativeOffsetState: Elements of CPUState most efficiently accessed
352 * from CPUArchState, via small negative offsets.
353 * @can_do_io: True if memory-mapped IO is allowed.
354 * @plugin_mem_cbs: active plugin memory callbacks
355 * @plugin_mem_value_low: 64 lower bits of latest accessed mem value.
356 * @plugin_mem_value_high: 64 higher bits of latest accessed mem value.
357 */
358 typedef struct CPUNegativeOffsetState {
359 CPUTLB tlb;
360 #ifdef CONFIG_PLUGIN
361 /*
362 * The callback pointer are accessed via TCG (see gen_empty_mem_helper).
363 */
364 GArray *plugin_mem_cbs;
365 uint64_t plugin_mem_value_low;
366 uint64_t plugin_mem_value_high;
367 #endif
368 IcountDecr icount_decr;
369 bool can_do_io;
370 } CPUNegativeOffsetState;
371
372 struct KVMState;
373 struct kvm_run;
374
375 /* work queue */
376
377 /* The union type allows passing of 64 bit target pointers on 32 bit
378 * hosts in a single parameter
379 */
380 typedef union {
381 int host_int;
382 unsigned long host_ulong;
383 void *host_ptr;
384 vaddr target_ptr;
385 } run_on_cpu_data;
386
387 #define RUN_ON_CPU_HOST_PTR(p) ((run_on_cpu_data){.host_ptr = (p)})
388 #define RUN_ON_CPU_HOST_INT(i) ((run_on_cpu_data){.host_int = (i)})
389 #define RUN_ON_CPU_HOST_ULONG(ul) ((run_on_cpu_data){.host_ulong = (ul)})
390 #define RUN_ON_CPU_TARGET_PTR(v) ((run_on_cpu_data){.target_ptr = (v)})
391 #define RUN_ON_CPU_NULL RUN_ON_CPU_HOST_PTR(NULL)
392
393 typedef void (*run_on_cpu_func)(CPUState *cpu, run_on_cpu_data data);
394
395 struct qemu_work_item;
396
397 #define CPU_UNSET_NUMA_NODE_ID -1
398
399 /**
400 * struct CPUState - common state of one CPU core or thread.
401 *
402 * @cpu_index: CPU index (informative).
403 * @cluster_index: Identifies which cluster this CPU is in.
404 * For boards which don't define clusters or for "loose" CPUs not assigned
405 * to a cluster this will be UNASSIGNED_CLUSTER_INDEX; otherwise it will
406 * be the same as the cluster-id property of the CPU object's TYPE_CPU_CLUSTER
407 * QOM parent.
408 * Under TCG this value is propagated to @tcg_cflags.
409 * See TranslationBlock::TCG CF_CLUSTER_MASK.
410 * @tcg_cflags: Pre-computed cflags for this cpu.
411 * @nr_threads: Number of threads within this CPU core.
412 * @thread: Host thread details, only live once @created is #true
413 * @sem: WIN32 only semaphore used only for qtest
414 * @thread_id: native thread id of vCPU, only live once @created is #true
415 * @running: #true if CPU is currently running (lockless).
416 * @has_waiter: #true if a CPU is currently waiting for the cpu_exec_end;
417 * valid under cpu_list_lock.
418 * @created: Indicates whether the CPU thread has been successfully created.
419 * @halt_cond: condition variable sleeping threads can wait on.
420 * @interrupt_request: Indicates a pending interrupt request.
421 * @halted: Nonzero if the CPU is in suspended state.
422 * @stop: Indicates a pending stop request.
423 * @stopped: Indicates the CPU has been artificially stopped.
424 * @unplug: Indicates a pending CPU unplug request.
425 * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU
426 * @singlestep_enabled: Flags for single-stepping.
427 * @icount_extra: Instructions until next timer event.
428 * @cpu_ases: Pointer to array of CPUAddressSpaces (which define the
429 * AddressSpaces this CPU has)
430 * @num_ases: number of CPUAddressSpaces in @cpu_ases
431 * @as: Pointer to the first AddressSpace, for the convenience of targets which
432 * only have a single AddressSpace
433 * @gdb_regs: Additional GDB registers.
434 * @gdb_num_regs: Number of total registers accessible to GDB.
435 * @gdb_num_g_regs: Number of registers in GDB 'g' packets.
436 * @node: QTAILQ of CPUs sharing TB cache.
437 * @opaque: User data.
438 * @mem_io_pc: Host Program Counter at which the memory was accessed.
439 * @accel: Pointer to accelerator specific state.
440 * @kvm_fd: vCPU file descriptor for KVM.
441 * @work_mutex: Lock to prevent multiple access to @work_list.
442 * @work_list: List of pending asynchronous work.
443 * @plugin_state: per-CPU plugin state
444 * @ignore_memory_transaction_failures: Cached copy of the MachineState
445 * flag of the same name: allows the board to suppress calling of the
446 * CPU do_transaction_failed hook function.
447 * @kvm_dirty_gfns: Points to the KVM dirty ring for this CPU when KVM dirty
448 * ring is enabled.
449 * @kvm_fetch_index: Keeps the index that we last fetched from the per-vCPU
450 * dirty ring structure.
451 *
452 * @neg_align: The CPUState is the common part of a concrete ArchCPU
453 * which is allocated when an individual CPU instance is created. As
454 * such care is taken is ensure there is no gap between between
455 * CPUState and CPUArchState within ArchCPU.
456 *
457 * @neg: The architectural register state ("cpu_env") immediately follows
458 * CPUState in ArchCPU and is passed to TCG code. The @neg structure holds
459 * some common TCG CPU variables which are accessed with a negative offset
460 * from cpu_env.
461 */
462 struct CPUState {
463 /*< private >*/
464 DeviceState parent_obj;
465 /* cache to avoid expensive CPU_GET_CLASS */
466 CPUClass *cc;
467 /*< public >*/
468
469 int nr_threads;
470
471 struct QemuThread *thread;
472 #ifdef _WIN32
473 QemuSemaphore sem;
474 #endif
475 int thread_id;
476 bool running, has_waiter;
477 struct QemuCond *halt_cond;
478 bool thread_kicked;
479 bool created;
480 bool stop;
481 bool stopped;
482
483 /* Should CPU start in powered-off state? */
484 bool start_powered_off;
485
486 bool unplug;
487 bool crash_occurred;
488 bool exit_request;
489 int exclusive_context_count;
490 uint32_t cflags_next_tb;
491 /* updates protected by BQL */
492 uint32_t interrupt_request;
493 int singlestep_enabled;
494 int64_t icount_budget;
495 int64_t icount_extra;
496 uint64_t random_seed;
497 sigjmp_buf jmp_env;
498
499 QemuMutex work_mutex;
500 QSIMPLEQ_HEAD(, qemu_work_item) work_list;
501
502 struct CPUAddressSpace *cpu_ases;
503 int cpu_ases_count;
504 int num_ases;
505 AddressSpace *as;
506 MemoryRegion *memory;
507
508 struct CPUJumpCache *tb_jmp_cache;
509
510 GArray *gdb_regs;
511 int gdb_num_regs;
512 int gdb_num_g_regs;
513 QTAILQ_ENTRY(CPUState) node;
514
515 /* ice debug support */
516 QTAILQ_HEAD(, CPUBreakpoint) breakpoints;
517
518 QTAILQ_HEAD(, CPUWatchpoint) watchpoints;
519 CPUWatchpoint *watchpoint_hit;
520
521 void *opaque;
522
523 /* In order to avoid passing too many arguments to the MMIO helpers,
524 * we store some rarely used information in the CPU context.
525 */
526 uintptr_t mem_io_pc;
527
528 /* Only used in KVM */
529 int kvm_fd;
530 struct KVMState *kvm_state;
531 struct kvm_run *kvm_run;
532 struct kvm_dirty_gfn *kvm_dirty_gfns;
533 uint32_t kvm_fetch_index;
534 uint64_t dirty_pages;
535 int kvm_vcpu_stats_fd;
536 bool vcpu_dirty;
537
538 /* Use by accel-block: CPU is executing an ioctl() */
539 QemuLockCnt in_ioctl_lock;
540
541 #ifdef CONFIG_PLUGIN
542 CPUPluginState *plugin_state;
543 #endif
544
545 /* TODO Move common fields from CPUArchState here. */
546 int cpu_index;
547 int cluster_index;
548 uint32_t tcg_cflags;
549 uint32_t halted;
550 int32_t exception_index;
551
552 AccelCPUState *accel;
553
554 /* Used to keep track of an outstanding cpu throttle thread for migration
555 * autoconverge
556 */
557 bool throttle_thread_scheduled;
558
559 /*
560 * Sleep throttle_us_per_full microseconds once dirty ring is full
561 * if dirty page rate limit is enabled.
562 */
563 int64_t throttle_us_per_full;
564
565 bool ignore_memory_transaction_failures;
566
567 /* Used for user-only emulation of prctl(PR_SET_UNALIGN). */
568 bool prctl_unalign_sigbus;
569
570 /* track IOMMUs whose translations we've cached in the TCG TLB */
571 GArray *iommu_notifiers;
572
573 /*
574 * MUST BE LAST in order to minimize the displacement to CPUArchState.
575 */
576 char neg_align[-sizeof(CPUNegativeOffsetState) % 16] QEMU_ALIGNED(16);
577 CPUNegativeOffsetState neg;
578 };
579
580 /* Validate placement of CPUNegativeOffsetState. */
581 QEMU_BUILD_BUG_ON(offsetof(CPUState, neg) !=
582 sizeof(CPUState) - sizeof(CPUNegativeOffsetState));
583
cpu_env(CPUState * cpu)584 static inline CPUArchState *cpu_env(CPUState *cpu)
585 {
586 /* We validate that CPUArchState follows CPUState in cpu-all.h. */
587 return (CPUArchState *)(cpu + 1);
588 }
589
590 typedef QTAILQ_HEAD(CPUTailQ, CPUState) CPUTailQ;
591 extern CPUTailQ cpus_queue;
592
593 #define first_cpu QTAILQ_FIRST_RCU(&cpus_queue)
594 #define CPU_NEXT(cpu) QTAILQ_NEXT_RCU(cpu, node)
595 #define CPU_FOREACH(cpu) QTAILQ_FOREACH_RCU(cpu, &cpus_queue, node)
596 #define CPU_FOREACH_SAFE(cpu, next_cpu) \
597 QTAILQ_FOREACH_SAFE_RCU(cpu, &cpus_queue, node, next_cpu)
598
599 extern __thread CPUState *current_cpu;
600
601 /**
602 * qemu_tcg_mttcg_enabled:
603 * Check whether we are running MultiThread TCG or not.
604 *
605 * Returns: %true if we are in MTTCG mode %false otherwise.
606 */
607 extern bool mttcg_enabled;
608 #define qemu_tcg_mttcg_enabled() (mttcg_enabled)
609
610 /**
611 * cpu_paging_enabled:
612 * @cpu: The CPU whose state is to be inspected.
613 *
614 * Returns: %true if paging is enabled, %false otherwise.
615 */
616 bool cpu_paging_enabled(const CPUState *cpu);
617
618 #if !defined(CONFIG_USER_ONLY)
619
620 /**
621 * cpu_get_memory_mapping:
622 * @cpu: The CPU whose memory mappings are to be obtained.
623 * @list: Where to write the memory mappings to.
624 * @errp: Pointer for reporting an #Error.
625 *
626 * Returns: %true on success, %false otherwise.
627 */
628 bool cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
629 Error **errp);
630
631 /**
632 * cpu_write_elf64_note:
633 * @f: pointer to a function that writes memory to a file
634 * @cpu: The CPU whose memory is to be dumped
635 * @cpuid: ID number of the CPU
636 * @opaque: pointer to the CPUState struct
637 */
638 int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
639 int cpuid, void *opaque);
640
641 /**
642 * cpu_write_elf64_qemunote:
643 * @f: pointer to a function that writes memory to a file
644 * @cpu: The CPU whose memory is to be dumped
645 * @cpuid: ID number of the CPU
646 * @opaque: pointer to the CPUState struct
647 */
648 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
649 void *opaque);
650
651 /**
652 * cpu_write_elf32_note:
653 * @f: pointer to a function that writes memory to a file
654 * @cpu: The CPU whose memory is to be dumped
655 * @cpuid: ID number of the CPU
656 * @opaque: pointer to the CPUState struct
657 */
658 int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
659 int cpuid, void *opaque);
660
661 /**
662 * cpu_write_elf32_qemunote:
663 * @f: pointer to a function that writes memory to a file
664 * @cpu: The CPU whose memory is to be dumped
665 * @cpuid: ID number of the CPU
666 * @opaque: pointer to the CPUState struct
667 */
668 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
669 void *opaque);
670
671 /**
672 * cpu_get_crash_info:
673 * @cpu: The CPU to get crash information for
674 *
675 * Gets the previously saved crash information.
676 * Caller is responsible for freeing the data.
677 */
678 GuestPanicInformation *cpu_get_crash_info(CPUState *cpu);
679
680 #endif /* !CONFIG_USER_ONLY */
681
682 /**
683 * CPUDumpFlags:
684 * @CPU_DUMP_CODE:
685 * @CPU_DUMP_FPU: dump FPU register state, not just integer
686 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
687 * @CPU_DUMP_VPU: dump VPU registers
688 */
689 enum CPUDumpFlags {
690 CPU_DUMP_CODE = 0x00010000,
691 CPU_DUMP_FPU = 0x00020000,
692 CPU_DUMP_CCOP = 0x00040000,
693 CPU_DUMP_VPU = 0x00080000,
694 };
695
696 /**
697 * cpu_dump_state:
698 * @cpu: The CPU whose state is to be dumped.
699 * @f: If non-null, dump to this stream, else to current print sink.
700 *
701 * Dumps CPU state.
702 */
703 void cpu_dump_state(CPUState *cpu, FILE *f, int flags);
704
705 #ifndef CONFIG_USER_ONLY
706 /**
707 * cpu_get_phys_page_attrs_debug:
708 * @cpu: The CPU to obtain the physical page address for.
709 * @addr: The virtual address.
710 * @attrs: Updated on return with the memory transaction attributes to use
711 * for this access.
712 *
713 * Obtains the physical page corresponding to a virtual one, together
714 * with the corresponding memory transaction attributes to use for the access.
715 * Use it only for debugging because no protection checks are done.
716 *
717 * Returns: Corresponding physical page address or -1 if no page found.
718 */
719 hwaddr cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr,
720 MemTxAttrs *attrs);
721
722 /**
723 * cpu_get_phys_page_debug:
724 * @cpu: The CPU to obtain the physical page address for.
725 * @addr: The virtual address.
726 *
727 * Obtains the physical page corresponding to a virtual one.
728 * Use it only for debugging because no protection checks are done.
729 *
730 * Returns: Corresponding physical page address or -1 if no page found.
731 */
732 hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr);
733
734 /** cpu_asidx_from_attrs:
735 * @cpu: CPU
736 * @attrs: memory transaction attributes
737 *
738 * Returns the address space index specifying the CPU AddressSpace
739 * to use for a memory access with the given transaction attributes.
740 */
741 int cpu_asidx_from_attrs(CPUState *cpu, MemTxAttrs attrs);
742
743 /**
744 * cpu_virtio_is_big_endian:
745 * @cpu: CPU
746
747 * Returns %true if a CPU which supports runtime configurable endianness
748 * is currently big-endian.
749 */
750 bool cpu_virtio_is_big_endian(CPUState *cpu);
751
752 /**
753 * cpu_has_work:
754 * @cpu: The vCPU to check.
755 *
756 * Checks whether the CPU has work to do.
757 *
758 * Returns: %true if the CPU has work, %false otherwise.
759 */
760 bool cpu_has_work(CPUState *cpu);
761
762 #endif /* CONFIG_USER_ONLY */
763
764 /**
765 * cpu_list_add:
766 * @cpu: The CPU to be added to the list of CPUs.
767 */
768 void cpu_list_add(CPUState *cpu);
769
770 /**
771 * cpu_list_remove:
772 * @cpu: The CPU to be removed from the list of CPUs.
773 */
774 void cpu_list_remove(CPUState *cpu);
775
776 /**
777 * cpu_reset:
778 * @cpu: The CPU whose state is to be reset.
779 */
780 void cpu_reset(CPUState *cpu);
781
782 /**
783 * cpu_class_by_name:
784 * @typename: The CPU base type.
785 * @cpu_model: The model string without any parameters.
786 *
787 * Looks up a concrete CPU #ObjectClass matching name @cpu_model.
788 *
789 * Returns: A concrete #CPUClass or %NULL if no matching class is found
790 * or if the matching class is abstract.
791 */
792 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
793
794 /**
795 * cpu_model_from_type:
796 * @typename: The CPU type name
797 *
798 * Extract the CPU model name from the CPU type name. The
799 * CPU type name is either the combination of the CPU model
800 * name and suffix, or same to the CPU model name.
801 *
802 * Returns: CPU model name or NULL if the CPU class doesn't exist
803 * The user should g_free() the string once no longer needed.
804 */
805 char *cpu_model_from_type(const char *typename);
806
807 /**
808 * cpu_create:
809 * @typename: The CPU type.
810 *
811 * Instantiates a CPU and realizes the CPU.
812 *
813 * Returns: A #CPUState or %NULL if an error occurred.
814 */
815 CPUState *cpu_create(const char *typename);
816
817 /**
818 * parse_cpu_option:
819 * @cpu_option: The -cpu option including optional parameters.
820 *
821 * processes optional parameters and registers them as global properties
822 *
823 * Returns: type of CPU to create or prints error and terminates process
824 * if an error occurred.
825 */
826 const char *parse_cpu_option(const char *cpu_option);
827
828 /**
829 * qemu_cpu_is_self:
830 * @cpu: The vCPU to check against.
831 *
832 * Checks whether the caller is executing on the vCPU thread.
833 *
834 * Returns: %true if called from @cpu's thread, %false otherwise.
835 */
836 bool qemu_cpu_is_self(CPUState *cpu);
837
838 /**
839 * qemu_cpu_kick:
840 * @cpu: The vCPU to kick.
841 *
842 * Kicks @cpu's thread.
843 */
844 void qemu_cpu_kick(CPUState *cpu);
845
846 /**
847 * cpu_is_stopped:
848 * @cpu: The CPU to check.
849 *
850 * Checks whether the CPU is stopped.
851 *
852 * Returns: %true if run state is not running or if artificially stopped;
853 * %false otherwise.
854 */
855 bool cpu_is_stopped(CPUState *cpu);
856
857 /**
858 * do_run_on_cpu:
859 * @cpu: The vCPU to run on.
860 * @func: The function to be executed.
861 * @data: Data to pass to the function.
862 * @mutex: Mutex to release while waiting for @func to run.
863 *
864 * Used internally in the implementation of run_on_cpu.
865 */
866 void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data,
867 QemuMutex *mutex);
868
869 /**
870 * run_on_cpu:
871 * @cpu: The vCPU to run on.
872 * @func: The function to be executed.
873 * @data: Data to pass to the function.
874 *
875 * Schedules the function @func for execution on the vCPU @cpu.
876 */
877 void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
878
879 /**
880 * async_run_on_cpu:
881 * @cpu: The vCPU to run on.
882 * @func: The function to be executed.
883 * @data: Data to pass to the function.
884 *
885 * Schedules the function @func for execution on the vCPU @cpu asynchronously.
886 */
887 void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
888
889 /**
890 * async_safe_run_on_cpu:
891 * @cpu: The vCPU to run on.
892 * @func: The function to be executed.
893 * @data: Data to pass to the function.
894 *
895 * Schedules the function @func for execution on the vCPU @cpu asynchronously,
896 * while all other vCPUs are sleeping.
897 *
898 * Unlike run_on_cpu and async_run_on_cpu, the function is run outside the
899 * BQL.
900 */
901 void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data);
902
903 /**
904 * cpu_in_exclusive_context()
905 * @cpu: The vCPU to check
906 *
907 * Returns true if @cpu is an exclusive context, for example running
908 * something which has previously been queued via async_safe_run_on_cpu().
909 */
cpu_in_exclusive_context(const CPUState * cpu)910 static inline bool cpu_in_exclusive_context(const CPUState *cpu)
911 {
912 return cpu->exclusive_context_count;
913 }
914
915 /**
916 * qemu_get_cpu:
917 * @index: The CPUState@cpu_index value of the CPU to obtain.
918 *
919 * Gets a CPU matching @index.
920 *
921 * Returns: The CPU or %NULL if there is no matching CPU.
922 */
923 CPUState *qemu_get_cpu(int index);
924
925 /**
926 * cpu_exists:
927 * @id: Guest-exposed CPU ID to lookup.
928 *
929 * Search for CPU with specified ID.
930 *
931 * Returns: %true - CPU is found, %false - CPU isn't found.
932 */
933 bool cpu_exists(int64_t id);
934
935 /**
936 * cpu_by_arch_id:
937 * @id: Guest-exposed CPU ID of the CPU to obtain.
938 *
939 * Get a CPU with matching @id.
940 *
941 * Returns: The CPU or %NULL if there is no matching CPU.
942 */
943 CPUState *cpu_by_arch_id(int64_t id);
944
945 /**
946 * cpu_interrupt:
947 * @cpu: The CPU to set an interrupt on.
948 * @mask: The interrupts to set.
949 *
950 * Invokes the interrupt handler.
951 */
952
953 void cpu_interrupt(CPUState *cpu, int mask);
954
955 /**
956 * cpu_set_pc:
957 * @cpu: The CPU to set the program counter for.
958 * @addr: Program counter value.
959 *
960 * Sets the program counter for a CPU.
961 */
cpu_set_pc(CPUState * cpu,vaddr addr)962 static inline void cpu_set_pc(CPUState *cpu, vaddr addr)
963 {
964 cpu->cc->set_pc(cpu, addr);
965 }
966
967 /**
968 * cpu_reset_interrupt:
969 * @cpu: The CPU to clear the interrupt on.
970 * @mask: The interrupt mask to clear.
971 *
972 * Resets interrupts on the vCPU @cpu.
973 */
974 void cpu_reset_interrupt(CPUState *cpu, int mask);
975
976 /**
977 * cpu_exit:
978 * @cpu: The CPU to exit.
979 *
980 * Requests the CPU @cpu to exit execution.
981 */
982 void cpu_exit(CPUState *cpu);
983
984 /**
985 * cpu_pause:
986 * @cpu: The CPU to pause.
987 *
988 * Pauses CPU, i.e. puts CPU into stopped state.
989 */
990 void cpu_pause(CPUState *cpu);
991
992 /**
993 * cpu_resume:
994 * @cpu: The CPU to resume.
995 *
996 * Resumes CPU, i.e. puts CPU into runnable state.
997 */
998 void cpu_resume(CPUState *cpu);
999
1000 /**
1001 * cpu_remove_sync:
1002 * @cpu: The CPU to remove.
1003 *
1004 * Requests the CPU to be removed and waits till it is removed.
1005 */
1006 void cpu_remove_sync(CPUState *cpu);
1007
1008 /**
1009 * free_queued_cpu_work() - free all items on CPU work queue
1010 * @cpu: The CPU which work queue to free.
1011 */
1012 void free_queued_cpu_work(CPUState *cpu);
1013
1014 /**
1015 * process_queued_cpu_work() - process all items on CPU work queue
1016 * @cpu: The CPU which work queue to process.
1017 */
1018 void process_queued_cpu_work(CPUState *cpu);
1019
1020 /**
1021 * cpu_exec_start:
1022 * @cpu: The CPU for the current thread.
1023 *
1024 * Record that a CPU has started execution and can be interrupted with
1025 * cpu_exit.
1026 */
1027 void cpu_exec_start(CPUState *cpu);
1028
1029 /**
1030 * cpu_exec_end:
1031 * @cpu: The CPU for the current thread.
1032 *
1033 * Record that a CPU has stopped execution and exclusive sections
1034 * can be executed without interrupting it.
1035 */
1036 void cpu_exec_end(CPUState *cpu);
1037
1038 /**
1039 * start_exclusive:
1040 *
1041 * Wait for a concurrent exclusive section to end, and then start
1042 * a section of work that is run while other CPUs are not running
1043 * between cpu_exec_start and cpu_exec_end. CPUs that are running
1044 * cpu_exec are exited immediately. CPUs that call cpu_exec_start
1045 * during the exclusive section go to sleep until this CPU calls
1046 * end_exclusive.
1047 */
1048 void start_exclusive(void);
1049
1050 /**
1051 * end_exclusive:
1052 *
1053 * Concludes an exclusive execution section started by start_exclusive.
1054 */
1055 void end_exclusive(void);
1056
1057 /**
1058 * qemu_init_vcpu:
1059 * @cpu: The vCPU to initialize.
1060 *
1061 * Initializes a vCPU.
1062 */
1063 void qemu_init_vcpu(CPUState *cpu);
1064
1065 #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
1066 #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
1067 #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
1068
1069 /**
1070 * cpu_single_step:
1071 * @cpu: CPU to the flags for.
1072 * @enabled: Flags to enable.
1073 *
1074 * Enables or disables single-stepping for @cpu.
1075 */
1076 void cpu_single_step(CPUState *cpu, int enabled);
1077
1078 /* Breakpoint/watchpoint flags */
1079 #define BP_MEM_READ 0x01
1080 #define BP_MEM_WRITE 0x02
1081 #define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE)
1082 #define BP_STOP_BEFORE_ACCESS 0x04
1083 /* 0x08 currently unused */
1084 #define BP_GDB 0x10
1085 #define BP_CPU 0x20
1086 #define BP_ANY (BP_GDB | BP_CPU)
1087 #define BP_HIT_SHIFT 6
1088 #define BP_WATCHPOINT_HIT_READ (BP_MEM_READ << BP_HIT_SHIFT)
1089 #define BP_WATCHPOINT_HIT_WRITE (BP_MEM_WRITE << BP_HIT_SHIFT)
1090 #define BP_WATCHPOINT_HIT (BP_MEM_ACCESS << BP_HIT_SHIFT)
1091
1092 int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
1093 CPUBreakpoint **breakpoint);
1094 int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags);
1095 void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint);
1096 void cpu_breakpoint_remove_all(CPUState *cpu, int mask);
1097
1098 /* Return true if PC matches an installed breakpoint. */
cpu_breakpoint_test(CPUState * cpu,vaddr pc,int mask)1099 static inline bool cpu_breakpoint_test(CPUState *cpu, vaddr pc, int mask)
1100 {
1101 CPUBreakpoint *bp;
1102
1103 if (unlikely(!QTAILQ_EMPTY(&cpu->breakpoints))) {
1104 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
1105 if (bp->pc == pc && (bp->flags & mask)) {
1106 return true;
1107 }
1108 }
1109 }
1110 return false;
1111 }
1112
1113 #if defined(CONFIG_USER_ONLY)
cpu_watchpoint_insert(CPUState * cpu,vaddr addr,vaddr len,int flags,CPUWatchpoint ** watchpoint)1114 static inline int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
1115 int flags, CPUWatchpoint **watchpoint)
1116 {
1117 return -ENOSYS;
1118 }
1119
cpu_watchpoint_remove(CPUState * cpu,vaddr addr,vaddr len,int flags)1120 static inline int cpu_watchpoint_remove(CPUState *cpu, vaddr addr,
1121 vaddr len, int flags)
1122 {
1123 return -ENOSYS;
1124 }
1125
cpu_watchpoint_remove_by_ref(CPUState * cpu,CPUWatchpoint * wp)1126 static inline void cpu_watchpoint_remove_by_ref(CPUState *cpu,
1127 CPUWatchpoint *wp)
1128 {
1129 }
1130
cpu_watchpoint_remove_all(CPUState * cpu,int mask)1131 static inline void cpu_watchpoint_remove_all(CPUState *cpu, int mask)
1132 {
1133 }
1134 #else
1135 int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
1136 int flags, CPUWatchpoint **watchpoint);
1137 int cpu_watchpoint_remove(CPUState *cpu, vaddr addr,
1138 vaddr len, int flags);
1139 void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint);
1140 void cpu_watchpoint_remove_all(CPUState *cpu, int mask);
1141 #endif
1142
1143 /**
1144 * cpu_get_address_space:
1145 * @cpu: CPU to get address space from
1146 * @asidx: index identifying which address space to get
1147 *
1148 * Return the requested address space of this CPU. @asidx
1149 * specifies which address space to read.
1150 */
1151 AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx);
1152
1153 G_NORETURN void cpu_abort(CPUState *cpu, const char *fmt, ...)
1154 G_GNUC_PRINTF(2, 3);
1155
1156 /* $(top_srcdir)/cpu.c */
1157 void cpu_class_init_props(DeviceClass *dc);
1158 void cpu_exec_class_post_init(CPUClass *cc);
1159 void cpu_exec_initfn(CPUState *cpu);
1160 void cpu_vmstate_register(CPUState *cpu);
1161 void cpu_vmstate_unregister(CPUState *cpu);
1162 bool cpu_exec_realizefn(CPUState *cpu, Error **errp);
1163 void cpu_exec_unrealizefn(CPUState *cpu);
1164 void cpu_exec_reset_hold(CPUState *cpu);
1165
1166 const char *target_name(void);
1167
1168 #ifdef COMPILING_PER_TARGET
1169
1170 #ifndef CONFIG_USER_ONLY
1171
1172 extern const VMStateDescription vmstate_cpu_common;
1173
1174 #define VMSTATE_CPU() { \
1175 .name = "parent_obj", \
1176 .size = sizeof(CPUState), \
1177 .vmsd = &vmstate_cpu_common, \
1178 .flags = VMS_STRUCT, \
1179 .offset = 0, \
1180 }
1181 #endif /* !CONFIG_USER_ONLY */
1182
1183 #endif /* COMPILING_PER_TARGET */
1184
1185 #define UNASSIGNED_CPU_INDEX -1
1186 #define UNASSIGNED_CLUSTER_INDEX -1
1187
1188 #endif
1189