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/thread.h"
27 #include "qemu/typedefs.h"
29 typedef int (*WriteCoreDumpFunction
)(void *buf
, size_t size
, void *opaque
);
33 * @section_id: QEMU-cpu
35 * @short_description: Base class for all CPUs
38 #define TYPE_CPU "cpu"
40 #define CPU(obj) OBJECT_CHECK(CPUState, (obj), TYPE_CPU)
41 #define CPU_CLASS(class) OBJECT_CLASS_CHECK(CPUClass, (class), TYPE_CPU)
42 #define CPU_GET_CLASS(obj) OBJECT_GET_CLASS(CPUClass, (obj), TYPE_CPU)
44 typedef struct CPUState CPUState
;
46 typedef void (*CPUUnassignedAccess
)(CPUState
*cpu
, hwaddr addr
,
47 bool is_write
, bool is_exec
, int opaque
,
52 * @class_by_name: Callback to map -cpu command line model name to an
53 * instantiatable CPU type.
54 * @reset: Callback to reset the #CPUState to its initial state.
55 * @do_interrupt: Callback for interrupt handling.
56 * @do_unassigned_access: Callback for unassigned access handling.
57 * @dump_state: Callback for dumping state.
58 * @dump_statistics: Callback for dumping statistics.
59 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
60 * @get_paging_enabled: Callback for inquiring whether paging is enabled.
61 * @get_memory_mapping: Callback for obtaining the memory mappings.
62 * @vmsd: State description for migration.
64 * Represents a CPU family or model.
66 typedef struct CPUClass
{
68 DeviceClass parent_class
;
71 ObjectClass
*(*class_by_name
)(const char *cpu_model
);
73 void (*reset
)(CPUState
*cpu
);
74 void (*do_interrupt
)(CPUState
*cpu
);
75 CPUUnassignedAccess do_unassigned_access
;
76 void (*dump_state
)(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
78 void (*dump_statistics
)(CPUState
*cpu
, FILE *f
,
79 fprintf_function cpu_fprintf
, int flags
);
80 int64_t (*get_arch_id
)(CPUState
*cpu
);
81 bool (*get_paging_enabled
)(const CPUState
*cpu
);
82 void (*get_memory_mapping
)(CPUState
*cpu
, MemoryMappingList
*list
,
85 const struct VMStateDescription
*vmsd
;
86 int (*write_elf64_note
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
87 int cpuid
, void *opaque
);
88 int (*write_elf64_qemunote
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
90 int (*write_elf32_note
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
91 int cpuid
, void *opaque
);
92 int (*write_elf32_qemunote
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
101 * @cpu_index: CPU index (informative).
102 * @nr_cores: Number of cores within this CPU package.
103 * @nr_threads: Number of threads within this CPU.
104 * @numa_node: NUMA node this CPU is belonging to.
105 * @host_tid: Host thread ID.
106 * @running: #true if CPU is currently running (usermode).
107 * @created: Indicates whether the CPU thread has been successfully created.
108 * @interrupt_request: Indicates a pending interrupt request.
109 * @halted: Nonzero if the CPU is in suspended state.
110 * @stop: Indicates a pending stop request.
111 * @stopped: Indicates the CPU has been artificially stopped.
112 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
113 * CPU and return to its top level loop.
114 * @env_ptr: Pointer to subclass-specific CPUArchState field.
115 * @current_tb: Currently executing TB.
116 * @kvm_fd: vCPU file descriptor for KVM.
118 * State of one CPU core or thread.
122 DeviceState parent_obj
;
129 struct QemuThread
*thread
;
136 struct QemuCond
*halt_cond
;
137 struct qemu_work_item
*queued_work_first
, *queued_work_last
;
142 volatile sig_atomic_t exit_request
;
143 volatile sig_atomic_t tcg_exit_req
;
144 uint32_t interrupt_request
;
146 void *env_ptr
; /* CPUArchState */
147 struct TranslationBlock
*current_tb
;
151 struct KVMState
*kvm_state
;
152 struct kvm_run
*kvm_run
;
154 /* TODO Move common fields from CPUArchState here. */
155 int cpu_index
; /* used by alpha TCG */
156 uint32_t halted
; /* used by alpha, cris, ppc TCG */
160 * cpu_paging_enabled:
161 * @cpu: The CPU whose state is to be inspected.
163 * Returns: %true if paging is enabled, %false otherwise.
165 bool cpu_paging_enabled(const CPUState
*cpu
);
168 * cpu_get_memory_mapping:
169 * @cpu: The CPU whose memory mappings are to be obtained.
170 * @list: Where to write the memory mappings to.
171 * @errp: Pointer for reporting an #Error.
173 void cpu_get_memory_mapping(CPUState
*cpu
, MemoryMappingList
*list
,
177 * cpu_write_elf64_note:
178 * @f: pointer to a function that writes memory to a file
179 * @cpu: The CPU whose memory is to be dumped
180 * @cpuid: ID number of the CPU
181 * @opaque: pointer to the CPUState struct
183 int cpu_write_elf64_note(WriteCoreDumpFunction f
, CPUState
*cpu
,
184 int cpuid
, void *opaque
);
187 * cpu_write_elf64_qemunote:
188 * @f: pointer to a function that writes memory to a file
189 * @cpu: The CPU whose memory is to be dumped
190 * @cpuid: ID number of the CPU
191 * @opaque: pointer to the CPUState struct
193 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f
, CPUState
*cpu
,
197 * cpu_write_elf32_note:
198 * @f: pointer to a function that writes memory to a file
199 * @cpu: The CPU whose memory is to be dumped
200 * @cpuid: ID number of the CPU
201 * @opaque: pointer to the CPUState struct
203 int cpu_write_elf32_note(WriteCoreDumpFunction f
, CPUState
*cpu
,
204 int cpuid
, void *opaque
);
207 * cpu_write_elf32_qemunote:
208 * @f: pointer to a function that writes memory to a file
209 * @cpu: The CPU whose memory is to be dumped
210 * @cpuid: ID number of the CPU
211 * @opaque: pointer to the CPUState struct
213 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f
, CPUState
*cpu
,
219 * @CPU_DUMP_FPU: dump FPU register state, not just integer
220 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
223 CPU_DUMP_CODE
= 0x00010000,
224 CPU_DUMP_FPU
= 0x00020000,
225 CPU_DUMP_CCOP
= 0x00040000,
230 * @cpu: The CPU whose state is to be dumped.
231 * @f: File to dump to.
232 * @cpu_fprintf: Function to dump with.
233 * @flags: Flags what to dump.
237 void cpu_dump_state(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
241 * cpu_dump_statistics:
242 * @cpu: The CPU whose state is to be dumped.
243 * @f: File to dump to.
244 * @cpu_fprintf: Function to dump with.
245 * @flags: Flags what to dump.
247 * Dumps CPU statistics.
249 void cpu_dump_statistics(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
254 * @cpu: The CPU whose state is to be reset.
256 void cpu_reset(CPUState
*cpu
);
260 * @typename: The CPU base type.
261 * @cpu_model: The model string without any parameters.
263 * Looks up a CPU #ObjectClass matching name @cpu_model.
265 * Returns: A #CPUClass or %NULL if not matching class is found.
267 ObjectClass
*cpu_class_by_name(const char *typename
, const char *cpu_model
);
270 * cpu_class_set_vmsd:
272 * @value: Value to set. Unused for %CONFIG_USER_ONLY.
274 * Sets #VMStateDescription for @cc.
276 * The @value argument is intentionally discarded for the non-softmmu targets
277 * to avoid linker errors or excessive preprocessor usage. If this behavior
278 * is undesired, you should assign #CPUClass.vmsd directly instead.
280 #ifndef CONFIG_USER_ONLY
281 static inline void cpu_class_set_vmsd(CPUClass
*cc
,
282 const struct VMStateDescription
*value
)
287 #define cpu_class_set_vmsd(cc, value) ((cc)->vmsd = NULL)
290 #ifndef CONFIG_USER_ONLY
291 static inline void cpu_class_set_do_unassigned_access(CPUClass
*cc
,
292 CPUUnassignedAccess value
)
294 cc
->do_unassigned_access
= value
;
297 #define cpu_class_set_do_unassigned_access(cc, value) \
298 ((cc)->do_unassigned_access = NULL)
302 * device_class_set_vmsd:
304 * @value: Value to set. Unused for %CONFIG_USER_ONLY.
306 * Sets #VMStateDescription for @dc.
308 * The @value argument is intentionally discarded for the non-softmmu targets
309 * to avoid linker errors or excessive preprocessor usage. If this behavior
310 * is undesired, you should assign #DeviceClass.vmsd directly instead.
312 #ifndef CONFIG_USER_ONLY
313 static inline void device_class_set_vmsd(DeviceClass
*dc
,
314 const struct VMStateDescription
*value
)
319 #define device_class_set_vmsd(dc, value) ((dc)->vmsd = NULL)
324 * @cpu: The vCPU to check.
326 * Checks whether the CPU has work to do.
328 * Returns: %true if the CPU has work, %false otherwise.
330 bool qemu_cpu_has_work(CPUState
*cpu
);
334 * @cpu: The vCPU to check against.
336 * Checks whether the caller is executing on the vCPU thread.
338 * Returns: %true if called from @cpu's thread, %false otherwise.
340 bool qemu_cpu_is_self(CPUState
*cpu
);
344 * @cpu: The vCPU to kick.
346 * Kicks @cpu's thread.
348 void qemu_cpu_kick(CPUState
*cpu
);
352 * @cpu: The CPU to check.
354 * Checks whether the CPU is stopped.
356 * Returns: %true if run state is not running or if artificially stopped;
359 bool cpu_is_stopped(CPUState
*cpu
);
363 * @cpu: The vCPU to run on.
364 * @func: The function to be executed.
365 * @data: Data to pass to the function.
367 * Schedules the function @func for execution on the vCPU @cpu.
369 void run_on_cpu(CPUState
*cpu
, void (*func
)(void *data
), void *data
);
373 * @func: The function to be executed.
374 * @data: Data to pass to the function.
376 * Executes @func for each CPU.
378 void qemu_for_each_cpu(void (*func
)(CPUState
*cpu
, void *data
), void *data
);
382 * @index: The CPUState@cpu_index value of the CPU to obtain.
384 * Gets a CPU matching @index.
386 * Returns: The CPU or %NULL if there is no matching CPU.
388 CPUState
*qemu_get_cpu(int index
);
392 * @id: Guest-exposed CPU ID to lookup.
394 * Search for CPU with specified ID.
396 * Returns: %true - CPU is found, %false - CPU isn't found.
398 bool cpu_exists(int64_t id
);
400 #ifndef CONFIG_USER_ONLY
402 typedef void (*CPUInterruptHandler
)(CPUState
*, int);
404 extern CPUInterruptHandler cpu_interrupt_handler
;
408 * @cpu: The CPU to set an interrupt on.
409 * @mask: The interupts to set.
411 * Invokes the interrupt handler.
413 static inline void cpu_interrupt(CPUState
*cpu
, int mask
)
415 cpu_interrupt_handler(cpu
, mask
);
418 #else /* USER_ONLY */
420 void cpu_interrupt(CPUState
*cpu
, int mask
);
422 #endif /* USER_ONLY */
424 #ifndef CONFIG_USER_ONLY
426 static inline void cpu_unassigned_access(CPUState
*cpu
, hwaddr addr
,
427 bool is_write
, bool is_exec
,
428 int opaque
, unsigned size
)
430 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
432 if (cc
->do_unassigned_access
) {
433 cc
->do_unassigned_access(cpu
, addr
, is_write
, is_exec
, opaque
, size
);
440 * cpu_reset_interrupt:
441 * @cpu: The CPU to clear the interrupt on.
442 * @mask: The interrupt mask to clear.
444 * Resets interrupts on the vCPU @cpu.
446 void cpu_reset_interrupt(CPUState
*cpu
, int mask
);
450 * @cpu: The CPU to exit.
452 * Requests the CPU @cpu to exit execution.
454 void cpu_exit(CPUState
*cpu
);
458 * @cpu: The CPU to resume.
460 * Resumes CPU, i.e. puts CPU into runnable state.
462 void cpu_resume(CPUState
*cpu
);
466 * @cpu: The vCPU to initialize.
468 * Initializes a vCPU.
470 void qemu_init_vcpu(CPUState
*cpu
);
472 #ifdef CONFIG_SOFTMMU
473 extern const struct VMStateDescription vmstate_cpu_common
;
475 #define vmstate_cpu_common vmstate_dummy
478 #define VMSTATE_CPU() { \
479 .name = "parent_obj", \
480 .size = sizeof(CPUState), \
481 .vmsd = &vmstate_cpu_common, \
482 .flags = VMS_STRUCT, \