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 "qemu/thread.h"
26 #include "qemu/typedefs.h"
28 typedef int (*WriteCoreDumpFunction
)(void *buf
, size_t size
, void *opaque
);
32 * @section_id: QEMU-cpu
34 * @short_description: Base class for all CPUs
37 #define TYPE_CPU "cpu"
39 #define CPU(obj) OBJECT_CHECK(CPUState, (obj), TYPE_CPU)
40 #define CPU_CLASS(class) OBJECT_CLASS_CHECK(CPUClass, (class), TYPE_CPU)
41 #define CPU_GET_CLASS(obj) OBJECT_GET_CLASS(CPUClass, (obj), TYPE_CPU)
43 typedef struct CPUState CPUState
;
47 * @class_by_name: Callback to map -cpu command line model name to an
48 * instantiatable CPU type.
49 * @reset: Callback to reset the #CPUState to its initial state.
50 * @do_interrupt: Callback for interrupt handling.
51 * @dump_state: Callback for dumping state.
52 * @dump_statistics: Callback for dumping statistics.
53 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
54 * @get_paging_enabled: Callback for inquiring whether paging is enabled.
55 * @get_memory_mapping: Callback for obtaining the memory mappings.
56 * @vmsd: State description for migration.
58 * Represents a CPU family or model.
60 typedef struct CPUClass
{
62 DeviceClass parent_class
;
65 ObjectClass
*(*class_by_name
)(const char *cpu_model
);
67 void (*reset
)(CPUState
*cpu
);
68 void (*do_interrupt
)(CPUState
*cpu
);
69 void (*dump_state
)(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
71 void (*dump_statistics
)(CPUState
*cpu
, FILE *f
,
72 fprintf_function cpu_fprintf
, int flags
);
73 int64_t (*get_arch_id
)(CPUState
*cpu
);
74 bool (*get_paging_enabled
)(const CPUState
*cpu
);
75 void (*get_memory_mapping
)(CPUState
*cpu
, MemoryMappingList
*list
,
78 const struct VMStateDescription
*vmsd
;
79 int (*write_elf64_note
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
80 int cpuid
, void *opaque
);
81 int (*write_elf64_qemunote
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
83 int (*write_elf32_note
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
84 int cpuid
, void *opaque
);
85 int (*write_elf32_qemunote
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
94 * @cpu_index: CPU index (informative).
95 * @nr_cores: Number of cores within this CPU package.
96 * @nr_threads: Number of threads within this CPU.
97 * @numa_node: NUMA node this CPU is belonging to.
98 * @host_tid: Host thread ID.
99 * @running: #true if CPU is currently running (usermode).
100 * @created: Indicates whether the CPU thread has been successfully created.
101 * @interrupt_request: Indicates a pending interrupt request.
102 * @halted: Nonzero if the CPU is in suspended state.
103 * @stop: Indicates a pending stop request.
104 * @stopped: Indicates the CPU has been artificially stopped.
105 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
106 * CPU and return to its top level loop.
107 * @env_ptr: Pointer to subclass-specific CPUArchState field.
108 * @current_tb: Currently executing TB.
109 * @kvm_fd: vCPU file descriptor for KVM.
111 * State of one CPU core or thread.
115 DeviceState parent_obj
;
122 struct QemuThread
*thread
;
129 struct QemuCond
*halt_cond
;
130 struct qemu_work_item
*queued_work_first
, *queued_work_last
;
135 volatile sig_atomic_t exit_request
;
136 volatile sig_atomic_t tcg_exit_req
;
137 uint32_t interrupt_request
;
139 void *env_ptr
; /* CPUArchState */
140 struct TranslationBlock
*current_tb
;
144 struct KVMState
*kvm_state
;
145 struct kvm_run
*kvm_run
;
147 /* TODO Move common fields from CPUArchState here. */
148 int cpu_index
; /* used by alpha TCG */
149 uint32_t halted
; /* used by alpha, cris, ppc TCG */
153 * cpu_paging_enabled:
154 * @cpu: The CPU whose state is to be inspected.
156 * Returns: %true if paging is enabled, %false otherwise.
158 bool cpu_paging_enabled(const CPUState
*cpu
);
161 * cpu_get_memory_mapping:
162 * @cpu: The CPU whose memory mappings are to be obtained.
163 * @list: Where to write the memory mappings to.
164 * @errp: Pointer for reporting an #Error.
166 void cpu_get_memory_mapping(CPUState
*cpu
, MemoryMappingList
*list
,
170 * cpu_write_elf64_note:
171 * @f: pointer to a function that writes memory to a file
172 * @cpu: The CPU whose memory is to be dumped
173 * @cpuid: ID number of the CPU
174 * @opaque: pointer to the CPUState struct
176 int cpu_write_elf64_note(WriteCoreDumpFunction f
, CPUState
*cpu
,
177 int cpuid
, void *opaque
);
180 * cpu_write_elf64_qemunote:
181 * @f: pointer to a function that writes memory to a file
182 * @cpu: The CPU whose memory is to be dumped
183 * @cpuid: ID number of the CPU
184 * @opaque: pointer to the CPUState struct
186 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f
, CPUState
*cpu
,
190 * cpu_write_elf32_note:
191 * @f: pointer to a function that writes memory to a file
192 * @cpu: The CPU whose memory is to be dumped
193 * @cpuid: ID number of the CPU
194 * @opaque: pointer to the CPUState struct
196 int cpu_write_elf32_note(WriteCoreDumpFunction f
, CPUState
*cpu
,
197 int cpuid
, void *opaque
);
200 * cpu_write_elf32_qemunote:
201 * @f: pointer to a function that writes memory to a file
202 * @cpu: The CPU whose memory is to be dumped
203 * @cpuid: ID number of the CPU
204 * @opaque: pointer to the CPUState struct
206 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f
, CPUState
*cpu
,
212 * @CPU_DUMP_FPU: dump FPU register state, not just integer
213 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
216 CPU_DUMP_CODE
= 0x00010000,
217 CPU_DUMP_FPU
= 0x00020000,
218 CPU_DUMP_CCOP
= 0x00040000,
223 * @cpu: The CPU whose state is to be dumped.
224 * @f: File to dump to.
225 * @cpu_fprintf: Function to dump with.
226 * @flags: Flags what to dump.
230 void cpu_dump_state(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
234 * cpu_dump_statistics:
235 * @cpu: The CPU whose state is to be dumped.
236 * @f: File to dump to.
237 * @cpu_fprintf: Function to dump with.
238 * @flags: Flags what to dump.
240 * Dumps CPU statistics.
242 void cpu_dump_statistics(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
247 * @cpu: The CPU whose state is to be reset.
249 void cpu_reset(CPUState
*cpu
);
253 * @typename: The CPU base type.
254 * @cpu_model: The model string without any parameters.
256 * Looks up a CPU #ObjectClass matching name @cpu_model.
258 * Returns: A #CPUClass or %NULL if not matching class is found.
260 ObjectClass
*cpu_class_by_name(const char *typename
, const char *cpu_model
);
263 * cpu_class_set_vmsd:
265 * @value: Value to set. Unused for %CONFIG_USER_ONLY.
267 * Sets #VMStateDescription for @cc.
269 * The @value argument is intentionally discarded for the non-softmmu targets
270 * to avoid linker errors or excessive preprocessor usage. If this behavior
271 * is undesired, you should assign #CPUClass.vmsd directly instead.
273 #ifndef CONFIG_USER_ONLY
274 static inline void cpu_class_set_vmsd(CPUClass
*cc
,
275 const struct VMStateDescription
*value
)
280 #define cpu_class_set_vmsd(cc, value) ((cc)->vmsd = NULL)
284 * device_class_set_vmsd:
286 * @value: Value to set. Unused for %CONFIG_USER_ONLY.
288 * Sets #VMStateDescription for @dc.
290 * The @value argument is intentionally discarded for the non-softmmu targets
291 * to avoid linker errors or excessive preprocessor usage. If this behavior
292 * is undesired, you should assign #DeviceClass.vmsd directly instead.
294 #ifndef CONFIG_USER_ONLY
295 static inline void device_class_set_vmsd(DeviceClass
*dc
,
296 const struct VMStateDescription
*value
)
301 #define device_class_set_vmsd(dc, value) ((dc)->vmsd = NULL)
306 * @cpu: The vCPU to check.
308 * Checks whether the CPU has work to do.
310 * Returns: %true if the CPU has work, %false otherwise.
312 bool qemu_cpu_has_work(CPUState
*cpu
);
316 * @cpu: The vCPU to check against.
318 * Checks whether the caller is executing on the vCPU thread.
320 * Returns: %true if called from @cpu's thread, %false otherwise.
322 bool qemu_cpu_is_self(CPUState
*cpu
);
326 * @cpu: The vCPU to kick.
328 * Kicks @cpu's thread.
330 void qemu_cpu_kick(CPUState
*cpu
);
334 * @cpu: The CPU to check.
336 * Checks whether the CPU is stopped.
338 * Returns: %true if run state is not running or if artificially stopped;
341 bool cpu_is_stopped(CPUState
*cpu
);
345 * @cpu: The vCPU to run on.
346 * @func: The function to be executed.
347 * @data: Data to pass to the function.
349 * Schedules the function @func for execution on the vCPU @cpu.
351 void run_on_cpu(CPUState
*cpu
, void (*func
)(void *data
), void *data
);
355 * @func: The function to be executed.
356 * @data: Data to pass to the function.
358 * Executes @func for each CPU.
360 void qemu_for_each_cpu(void (*func
)(CPUState
*cpu
, void *data
), void *data
);
364 * @index: The CPUState@cpu_index value of the CPU to obtain.
366 * Gets a CPU matching @index.
368 * Returns: The CPU or %NULL if there is no matching CPU.
370 CPUState
*qemu_get_cpu(int index
);
374 * @id: Guest-exposed CPU ID to lookup.
376 * Search for CPU with specified ID.
378 * Returns: %true - CPU is found, %false - CPU isn't found.
380 bool cpu_exists(int64_t id
);
382 #ifndef CONFIG_USER_ONLY
384 typedef void (*CPUInterruptHandler
)(CPUState
*, int);
386 extern CPUInterruptHandler cpu_interrupt_handler
;
390 * @cpu: The CPU to set an interrupt on.
391 * @mask: The interupts to set.
393 * Invokes the interrupt handler.
395 static inline void cpu_interrupt(CPUState
*cpu
, int mask
)
397 cpu_interrupt_handler(cpu
, mask
);
400 #else /* USER_ONLY */
402 void cpu_interrupt(CPUState
*cpu
, int mask
);
404 #endif /* USER_ONLY */
407 * cpu_reset_interrupt:
408 * @cpu: The CPU to clear the interrupt on.
409 * @mask: The interrupt mask to clear.
411 * Resets interrupts on the vCPU @cpu.
413 void cpu_reset_interrupt(CPUState
*cpu
, int mask
);
417 * @cpu: The CPU to exit.
419 * Requests the CPU @cpu to exit execution.
421 void cpu_exit(CPUState
*cpu
);
425 * @cpu: The CPU to resume.
427 * Resumes CPU, i.e. puts CPU into runnable state.
429 void cpu_resume(CPUState
*cpu
);
431 #ifdef CONFIG_SOFTMMU
432 extern const struct VMStateDescription vmstate_cpu_common
;
434 #define vmstate_cpu_common vmstate_dummy
437 #define VMSTATE_CPU() { \
438 .name = "parent_obj", \
439 .size = sizeof(CPUState), \
440 .vmsd = &vmstate_cpu_common, \
441 .flags = VMS_STRUCT, \