4 * Copyright (c) 2012 SUSE LINUX Products GmbH
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.
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.
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>
24 #include "hw/qdev-core.h"
25 #include "exec/hwaddr.h"
26 #include "qemu/queue.h"
27 #include "qemu/thread.h"
29 #include "qemu/typedefs.h"
31 typedef int (*WriteCoreDumpFunction
)(const void *buf
, size_t size
,
36 * Type wide enough to contain any #target_ulong virtual address.
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
48 * @section_id: QEMU-cpu
50 * @short_description: Base class for all CPUs
53 #define TYPE_CPU "cpu"
55 #define CPU(obj) OBJECT_CHECK(CPUState, (obj), TYPE_CPU)
56 #define CPU_CLASS(class) OBJECT_CLASS_CHECK(CPUClass, (class), TYPE_CPU)
57 #define CPU_GET_CLASS(obj) OBJECT_GET_CLASS(CPUClass, (obj), TYPE_CPU)
59 typedef struct CPUState CPUState
;
61 typedef void (*CPUUnassignedAccess
)(CPUState
*cpu
, hwaddr addr
,
62 bool is_write
, bool is_exec
, int opaque
,
65 struct TranslationBlock
;
69 * @class_by_name: Callback to map -cpu command line model name to an
70 * instantiatable CPU type.
71 * @parse_features: Callback to parse command line arguments.
72 * @reset: Callback to reset the #CPUState to its initial state.
73 * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
74 * @has_work: Callback for checking if there is work to do.
75 * @do_interrupt: Callback for interrupt handling.
76 * @do_unassigned_access: Callback for unassigned access handling.
77 * @memory_rw_debug: Callback for GDB memory access.
78 * @dump_state: Callback for dumping state.
79 * @dump_statistics: Callback for dumping statistics.
80 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
81 * @get_paging_enabled: Callback for inquiring whether paging is enabled.
82 * @get_memory_mapping: Callback for obtaining the memory mappings.
83 * @set_pc: Callback for setting the Program Counter register.
84 * @synchronize_from_tb: Callback for synchronizing state from a TCG
86 * @handle_mmu_fault: Callback for handling an MMU fault.
87 * @get_phys_page_debug: Callback for obtaining a physical address.
88 * @gdb_read_register: Callback for letting GDB read a register.
89 * @gdb_write_register: Callback for letting GDB write a register.
90 * @vmsd: State description for migration.
91 * @gdb_num_core_regs: Number of core registers accessible to GDB.
92 * @gdb_core_xml_file: File name for core registers GDB XML description.
94 * Represents a CPU family or model.
96 typedef struct CPUClass
{
98 DeviceClass parent_class
;
101 ObjectClass
*(*class_by_name
)(const char *cpu_model
);
102 void (*parse_features
)(CPUState
*cpu
, char *str
, Error
**errp
);
104 void (*reset
)(CPUState
*cpu
);
105 int reset_dump_flags
;
106 bool (*has_work
)(CPUState
*cpu
);
107 void (*do_interrupt
)(CPUState
*cpu
);
108 CPUUnassignedAccess do_unassigned_access
;
109 int (*memory_rw_debug
)(CPUState
*cpu
, vaddr addr
,
110 uint8_t *buf
, int len
, bool is_write
);
111 void (*dump_state
)(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
113 void (*dump_statistics
)(CPUState
*cpu
, FILE *f
,
114 fprintf_function cpu_fprintf
, int flags
);
115 int64_t (*get_arch_id
)(CPUState
*cpu
);
116 bool (*get_paging_enabled
)(const CPUState
*cpu
);
117 void (*get_memory_mapping
)(CPUState
*cpu
, MemoryMappingList
*list
,
119 void (*set_pc
)(CPUState
*cpu
, vaddr value
);
120 void (*synchronize_from_tb
)(CPUState
*cpu
, struct TranslationBlock
*tb
);
121 int (*handle_mmu_fault
)(CPUState
*cpu
, vaddr address
, int rw
,
123 hwaddr (*get_phys_page_debug
)(CPUState
*cpu
, vaddr addr
);
124 int (*gdb_read_register
)(CPUState
*cpu
, uint8_t *buf
, int reg
);
125 int (*gdb_write_register
)(CPUState
*cpu
, uint8_t *buf
, int reg
);
127 int (*write_elf64_note
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
128 int cpuid
, void *opaque
);
129 int (*write_elf64_qemunote
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
131 int (*write_elf32_note
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
132 int cpuid
, void *opaque
);
133 int (*write_elf32_qemunote
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
136 const struct VMStateDescription
*vmsd
;
137 int gdb_num_core_regs
;
138 const char *gdb_core_xml_file
;
146 * @cpu_index: CPU index (informative).
147 * @nr_cores: Number of cores within this CPU package.
148 * @nr_threads: Number of threads within this CPU.
149 * @numa_node: NUMA node this CPU is belonging to.
150 * @host_tid: Host thread ID.
151 * @running: #true if CPU is currently running (usermode).
152 * @created: Indicates whether the CPU thread has been successfully created.
153 * @interrupt_request: Indicates a pending interrupt request.
154 * @halted: Nonzero if the CPU is in suspended state.
155 * @stop: Indicates a pending stop request.
156 * @stopped: Indicates the CPU has been artificially stopped.
157 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
158 * CPU and return to its top level loop.
159 * @singlestep_enabled: Flags for single-stepping.
160 * @can_do_io: Nonzero if memory-mapped IO is safe.
161 * @env_ptr: Pointer to subclass-specific CPUArchState field.
162 * @current_tb: Currently executing TB.
163 * @gdb_regs: Additional GDB registers.
164 * @gdb_num_regs: Number of total registers accessible to GDB.
165 * @gdb_num_g_regs: Number of registers in GDB 'g' packets.
166 * @next_cpu: Next CPU sharing TB cache.
167 * @mem_io_pc: Host Program Counter at which the memory was accessed.
168 * @mem_io_vaddr: Target virtual address at which the memory was accessed.
169 * @kvm_fd: vCPU file descriptor for KVM.
171 * State of one CPU core or thread.
175 DeviceState parent_obj
;
182 struct QemuThread
*thread
;
189 struct QemuCond
*halt_cond
;
190 struct qemu_work_item
*queued_work_first
, *queued_work_last
;
195 volatile sig_atomic_t exit_request
;
196 volatile sig_atomic_t tcg_exit_req
;
197 uint32_t interrupt_request
;
198 int singlestep_enabled
;
201 MemoryListener
*tcg_as_listener
;
203 void *env_ptr
; /* CPUArchState */
204 struct TranslationBlock
*current_tb
;
205 struct GDBRegisterState
*gdb_regs
;
208 QTAILQ_ENTRY(CPUState
) node
;
210 /* In order to avoid passing too many arguments to the MMIO helpers,
211 * we store some rarely used information in the CPU context.
218 struct KVMState
*kvm_state
;
219 struct kvm_run
*kvm_run
;
221 /* TODO Move common fields from CPUArchState here. */
222 int cpu_index
; /* used by alpha TCG */
223 uint32_t halted
; /* used by alpha, cris, ppc TCG */
227 QTAILQ_HEAD(CPUTailQ
, CPUState
);
228 extern struct CPUTailQ cpus
;
229 #define CPU_NEXT(cpu) QTAILQ_NEXT(cpu, node)
230 #define CPU_FOREACH(cpu) QTAILQ_FOREACH(cpu, &cpus, node)
231 #define CPU_FOREACH_SAFE(cpu, next_cpu) \
232 QTAILQ_FOREACH_SAFE(cpu, &cpus, node, next_cpu)
233 #define first_cpu QTAILQ_FIRST(&cpus)
235 DECLARE_TLS(CPUState
*, current_cpu
);
236 #define current_cpu tls_var(current_cpu)
239 * cpu_paging_enabled:
240 * @cpu: The CPU whose state is to be inspected.
242 * Returns: %true if paging is enabled, %false otherwise.
244 bool cpu_paging_enabled(const CPUState
*cpu
);
247 * cpu_get_memory_mapping:
248 * @cpu: The CPU whose memory mappings are to be obtained.
249 * @list: Where to write the memory mappings to.
250 * @errp: Pointer for reporting an #Error.
252 void cpu_get_memory_mapping(CPUState
*cpu
, MemoryMappingList
*list
,
256 * cpu_write_elf64_note:
257 * @f: pointer to a function that writes memory to a file
258 * @cpu: The CPU whose memory is to be dumped
259 * @cpuid: ID number of the CPU
260 * @opaque: pointer to the CPUState struct
262 int cpu_write_elf64_note(WriteCoreDumpFunction f
, CPUState
*cpu
,
263 int cpuid
, void *opaque
);
266 * cpu_write_elf64_qemunote:
267 * @f: pointer to a function that writes memory to a file
268 * @cpu: The CPU whose memory is to be dumped
269 * @cpuid: ID number of the CPU
270 * @opaque: pointer to the CPUState struct
272 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f
, CPUState
*cpu
,
276 * cpu_write_elf32_note:
277 * @f: pointer to a function that writes memory to a file
278 * @cpu: The CPU whose memory is to be dumped
279 * @cpuid: ID number of the CPU
280 * @opaque: pointer to the CPUState struct
282 int cpu_write_elf32_note(WriteCoreDumpFunction f
, CPUState
*cpu
,
283 int cpuid
, void *opaque
);
286 * cpu_write_elf32_qemunote:
287 * @f: pointer to a function that writes memory to a file
288 * @cpu: The CPU whose memory is to be dumped
289 * @cpuid: ID number of the CPU
290 * @opaque: pointer to the CPUState struct
292 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f
, CPUState
*cpu
,
298 * @CPU_DUMP_FPU: dump FPU register state, not just integer
299 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
302 CPU_DUMP_CODE
= 0x00010000,
303 CPU_DUMP_FPU
= 0x00020000,
304 CPU_DUMP_CCOP
= 0x00040000,
309 * @cpu: The CPU whose state is to be dumped.
310 * @f: File to dump to.
311 * @cpu_fprintf: Function to dump with.
312 * @flags: Flags what to dump.
316 void cpu_dump_state(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
320 * cpu_dump_statistics:
321 * @cpu: The CPU whose state is to be dumped.
322 * @f: File to dump to.
323 * @cpu_fprintf: Function to dump with.
324 * @flags: Flags what to dump.
326 * Dumps CPU statistics.
328 void cpu_dump_statistics(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
331 #ifndef CONFIG_USER_ONLY
333 * cpu_get_phys_page_debug:
334 * @cpu: The CPU to obtain the physical page address for.
335 * @addr: The virtual address.
337 * Obtains the physical page corresponding to a virtual one.
338 * Use it only for debugging because no protection checks are done.
340 * Returns: Corresponding physical page address or -1 if no page found.
342 static inline hwaddr
cpu_get_phys_page_debug(CPUState
*cpu
, vaddr addr
)
344 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
346 return cc
->get_phys_page_debug(cpu
, addr
);
352 * @cpu: The CPU whose state is to be reset.
354 void cpu_reset(CPUState
*cpu
);
358 * @typename: The CPU base type.
359 * @cpu_model: The model string without any parameters.
361 * Looks up a CPU #ObjectClass matching name @cpu_model.
363 * Returns: A #CPUClass or %NULL if not matching class is found.
365 ObjectClass
*cpu_class_by_name(const char *typename
, const char *cpu_model
);
369 * @typename: The CPU base type.
370 * @cpu_model: The model string including optional parameters.
372 * Instantiates a CPU, processes optional parameters and realizes the CPU.
374 * Returns: A #CPUState or %NULL if an error occurred.
376 CPUState
*cpu_generic_init(const char *typename
, const char *cpu_model
);
380 * @cpu: The vCPU to check.
382 * Checks whether the CPU has work to do.
384 * Returns: %true if the CPU has work, %false otherwise.
386 static inline bool cpu_has_work(CPUState
*cpu
)
388 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
390 g_assert(cc
->has_work
);
391 return cc
->has_work(cpu
);
396 * @cpu: The vCPU to check against.
398 * Checks whether the caller is executing on the vCPU thread.
400 * Returns: %true if called from @cpu's thread, %false otherwise.
402 bool qemu_cpu_is_self(CPUState
*cpu
);
406 * @cpu: The vCPU to kick.
408 * Kicks @cpu's thread.
410 void qemu_cpu_kick(CPUState
*cpu
);
414 * @cpu: The CPU to check.
416 * Checks whether the CPU is stopped.
418 * Returns: %true if run state is not running or if artificially stopped;
421 bool cpu_is_stopped(CPUState
*cpu
);
425 * @cpu: The vCPU to run on.
426 * @func: The function to be executed.
427 * @data: Data to pass to the function.
429 * Schedules the function @func for execution on the vCPU @cpu.
431 void run_on_cpu(CPUState
*cpu
, void (*func
)(void *data
), void *data
);
435 * @cpu: The vCPU to run on.
436 * @func: The function to be executed.
437 * @data: Data to pass to the function.
439 * Schedules the function @func for execution on the vCPU @cpu asynchronously.
441 void async_run_on_cpu(CPUState
*cpu
, void (*func
)(void *data
), void *data
);
445 * @index: The CPUState@cpu_index value of the CPU to obtain.
447 * Gets a CPU matching @index.
449 * Returns: The CPU or %NULL if there is no matching CPU.
451 CPUState
*qemu_get_cpu(int index
);
455 * @id: Guest-exposed CPU ID to lookup.
457 * Search for CPU with specified ID.
459 * Returns: %true - CPU is found, %false - CPU isn't found.
461 bool cpu_exists(int64_t id
);
463 #ifndef CONFIG_USER_ONLY
465 typedef void (*CPUInterruptHandler
)(CPUState
*, int);
467 extern CPUInterruptHandler cpu_interrupt_handler
;
471 * @cpu: The CPU to set an interrupt on.
472 * @mask: The interupts to set.
474 * Invokes the interrupt handler.
476 static inline void cpu_interrupt(CPUState
*cpu
, int mask
)
478 cpu_interrupt_handler(cpu
, mask
);
481 #else /* USER_ONLY */
483 void cpu_interrupt(CPUState
*cpu
, int mask
);
485 #endif /* USER_ONLY */
487 #ifndef CONFIG_USER_ONLY
489 static inline void cpu_unassigned_access(CPUState
*cpu
, hwaddr addr
,
490 bool is_write
, bool is_exec
,
491 int opaque
, unsigned size
)
493 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
495 if (cc
->do_unassigned_access
) {
496 cc
->do_unassigned_access(cpu
, addr
, is_write
, is_exec
, opaque
, size
);
503 * cpu_reset_interrupt:
504 * @cpu: The CPU to clear the interrupt on.
505 * @mask: The interrupt mask to clear.
507 * Resets interrupts on the vCPU @cpu.
509 void cpu_reset_interrupt(CPUState
*cpu
, int mask
);
513 * @cpu: The CPU to exit.
515 * Requests the CPU @cpu to exit execution.
517 void cpu_exit(CPUState
*cpu
);
521 * @cpu: The CPU to resume.
523 * Resumes CPU, i.e. puts CPU into runnable state.
525 void cpu_resume(CPUState
*cpu
);
529 * @cpu: The vCPU to initialize.
531 * Initializes a vCPU.
533 void qemu_init_vcpu(CPUState
*cpu
);
535 #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
536 #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
537 #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
541 * @cpu: CPU to the flags for.
542 * @enabled: Flags to enable.
544 * Enables or disables single-stepping for @cpu.
546 void cpu_single_step(CPUState
*cpu
, int enabled
);
548 #ifdef CONFIG_SOFTMMU
549 extern const struct VMStateDescription vmstate_cpu_common
;
551 #define vmstate_cpu_common vmstate_dummy
554 #define VMSTATE_CPU() { \
555 .name = "parent_obj", \
556 .size = sizeof(CPUState), \
557 .vmsd = &vmstate_cpu_common, \
558 .flags = VMS_STRUCT, \