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"
28 #include "qemu/typedefs.h"
30 typedef int (*WriteCoreDumpFunction
)(void *buf
, size_t size
, void *opaque
);
34 * @section_id: QEMU-cpu
36 * @short_description: Base class for all CPUs
39 #define TYPE_CPU "cpu"
41 #define CPU(obj) OBJECT_CHECK(CPUState, (obj), TYPE_CPU)
42 #define CPU_CLASS(class) OBJECT_CLASS_CHECK(CPUClass, (class), TYPE_CPU)
43 #define CPU_GET_CLASS(obj) OBJECT_GET_CLASS(CPUClass, (obj), TYPE_CPU)
45 typedef struct CPUState CPUState
;
47 typedef void (*CPUUnassignedAccess
)(CPUState
*cpu
, hwaddr addr
,
48 bool is_write
, bool is_exec
, int opaque
,
53 * @class_by_name: Callback to map -cpu command line model name to an
54 * instantiatable CPU type.
55 * @reset: Callback to reset the #CPUState to its initial state.
56 * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
57 * @do_interrupt: Callback for interrupt handling.
58 * @do_unassigned_access: Callback for unassigned access handling.
59 * @dump_state: Callback for dumping state.
60 * @dump_statistics: Callback for dumping statistics.
61 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
62 * @get_paging_enabled: Callback for inquiring whether paging is enabled.
63 * @get_memory_mapping: Callback for obtaining the memory mappings.
64 * @vmsd: State description for migration.
66 * Represents a CPU family or model.
68 typedef struct CPUClass
{
70 DeviceClass parent_class
;
73 ObjectClass
*(*class_by_name
)(const char *cpu_model
);
75 void (*reset
)(CPUState
*cpu
);
77 void (*do_interrupt
)(CPUState
*cpu
);
78 CPUUnassignedAccess do_unassigned_access
;
79 void (*dump_state
)(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
81 void (*dump_statistics
)(CPUState
*cpu
, FILE *f
,
82 fprintf_function cpu_fprintf
, int flags
);
83 int64_t (*get_arch_id
)(CPUState
*cpu
);
84 bool (*get_paging_enabled
)(const CPUState
*cpu
);
85 void (*get_memory_mapping
)(CPUState
*cpu
, MemoryMappingList
*list
,
88 const struct VMStateDescription
*vmsd
;
89 int (*write_elf64_note
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
90 int cpuid
, void *opaque
);
91 int (*write_elf64_qemunote
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
93 int (*write_elf32_note
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
94 int cpuid
, void *opaque
);
95 int (*write_elf32_qemunote
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
104 * @cpu_index: CPU index (informative).
105 * @nr_cores: Number of cores within this CPU package.
106 * @nr_threads: Number of threads within this CPU.
107 * @numa_node: NUMA node this CPU is belonging to.
108 * @host_tid: Host thread ID.
109 * @running: #true if CPU is currently running (usermode).
110 * @created: Indicates whether the CPU thread has been successfully created.
111 * @interrupt_request: Indicates a pending interrupt request.
112 * @halted: Nonzero if the CPU is in suspended state.
113 * @stop: Indicates a pending stop request.
114 * @stopped: Indicates the CPU has been artificially stopped.
115 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
116 * CPU and return to its top level loop.
117 * @env_ptr: Pointer to subclass-specific CPUArchState field.
118 * @current_tb: Currently executing TB.
119 * @next_cpu: Next CPU sharing TB cache.
120 * @kvm_fd: vCPU file descriptor for KVM.
122 * State of one CPU core or thread.
126 DeviceState parent_obj
;
133 struct QemuThread
*thread
;
140 struct QemuCond
*halt_cond
;
141 struct qemu_work_item
*queued_work_first
, *queued_work_last
;
146 volatile sig_atomic_t exit_request
;
147 volatile sig_atomic_t tcg_exit_req
;
148 uint32_t interrupt_request
;
150 void *env_ptr
; /* CPUArchState */
151 struct TranslationBlock
*current_tb
;
156 struct KVMState
*kvm_state
;
157 struct kvm_run
*kvm_run
;
159 /* TODO Move common fields from CPUArchState here. */
160 int cpu_index
; /* used by alpha TCG */
161 uint32_t halted
; /* used by alpha, cris, ppc TCG */
164 extern CPUState
*first_cpu
;
166 DECLARE_TLS(CPUState
*, current_cpu
);
167 #define current_cpu tls_var(current_cpu)
170 * cpu_paging_enabled:
171 * @cpu: The CPU whose state is to be inspected.
173 * Returns: %true if paging is enabled, %false otherwise.
175 bool cpu_paging_enabled(const CPUState
*cpu
);
178 * cpu_get_memory_mapping:
179 * @cpu: The CPU whose memory mappings are to be obtained.
180 * @list: Where to write the memory mappings to.
181 * @errp: Pointer for reporting an #Error.
183 void cpu_get_memory_mapping(CPUState
*cpu
, MemoryMappingList
*list
,
187 * cpu_write_elf64_note:
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_note(WriteCoreDumpFunction f
, CPUState
*cpu
,
194 int cpuid
, void *opaque
);
197 * cpu_write_elf64_qemunote:
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_elf64_qemunote(WriteCoreDumpFunction f
, CPUState
*cpu
,
207 * cpu_write_elf32_note:
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_note(WriteCoreDumpFunction f
, CPUState
*cpu
,
214 int cpuid
, void *opaque
);
217 * cpu_write_elf32_qemunote:
218 * @f: pointer to a function that writes memory to a file
219 * @cpu: The CPU whose memory is to be dumped
220 * @cpuid: ID number of the CPU
221 * @opaque: pointer to the CPUState struct
223 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f
, CPUState
*cpu
,
229 * @CPU_DUMP_FPU: dump FPU register state, not just integer
230 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
233 CPU_DUMP_CODE
= 0x00010000,
234 CPU_DUMP_FPU
= 0x00020000,
235 CPU_DUMP_CCOP
= 0x00040000,
240 * @cpu: The CPU whose state is to be dumped.
241 * @f: File to dump to.
242 * @cpu_fprintf: Function to dump with.
243 * @flags: Flags what to dump.
247 void cpu_dump_state(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
251 * cpu_dump_statistics:
252 * @cpu: The CPU whose state is to be dumped.
253 * @f: File to dump to.
254 * @cpu_fprintf: Function to dump with.
255 * @flags: Flags what to dump.
257 * Dumps CPU statistics.
259 void cpu_dump_statistics(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
264 * @cpu: The CPU whose state is to be reset.
266 void cpu_reset(CPUState
*cpu
);
270 * @typename: The CPU base type.
271 * @cpu_model: The model string without any parameters.
273 * Looks up a CPU #ObjectClass matching name @cpu_model.
275 * Returns: A #CPUClass or %NULL if not matching class is found.
277 ObjectClass
*cpu_class_by_name(const char *typename
, const char *cpu_model
);
280 * cpu_class_set_vmsd:
282 * @value: Value to set. Unused for %CONFIG_USER_ONLY.
284 * Sets #VMStateDescription for @cc.
286 * The @value argument is intentionally discarded for the non-softmmu targets
287 * to avoid linker errors or excessive preprocessor usage. If this behavior
288 * is undesired, you should assign #CPUClass.vmsd directly instead.
290 #ifndef CONFIG_USER_ONLY
291 static inline void cpu_class_set_vmsd(CPUClass
*cc
,
292 const struct VMStateDescription
*value
)
297 #define cpu_class_set_vmsd(cc, value) ((cc)->vmsd = NULL)
300 #ifndef CONFIG_USER_ONLY
301 static inline void cpu_class_set_do_unassigned_access(CPUClass
*cc
,
302 CPUUnassignedAccess value
)
304 cc
->do_unassigned_access
= value
;
307 #define cpu_class_set_do_unassigned_access(cc, value) \
308 ((cc)->do_unassigned_access = NULL)
312 * device_class_set_vmsd:
314 * @value: Value to set. Unused for %CONFIG_USER_ONLY.
316 * Sets #VMStateDescription for @dc.
318 * The @value argument is intentionally discarded for the non-softmmu targets
319 * to avoid linker errors or excessive preprocessor usage. If this behavior
320 * is undesired, you should assign #DeviceClass.vmsd directly instead.
322 #ifndef CONFIG_USER_ONLY
323 static inline void device_class_set_vmsd(DeviceClass
*dc
,
324 const struct VMStateDescription
*value
)
329 #define device_class_set_vmsd(dc, value) ((dc)->vmsd = NULL)
334 * @cpu: The vCPU to check.
336 * Checks whether the CPU has work to do.
338 * Returns: %true if the CPU has work, %false otherwise.
340 bool qemu_cpu_has_work(CPUState
*cpu
);
344 * @cpu: The vCPU to check against.
346 * Checks whether the caller is executing on the vCPU thread.
348 * Returns: %true if called from @cpu's thread, %false otherwise.
350 bool qemu_cpu_is_self(CPUState
*cpu
);
354 * @cpu: The vCPU to kick.
356 * Kicks @cpu's thread.
358 void qemu_cpu_kick(CPUState
*cpu
);
362 * @cpu: The CPU to check.
364 * Checks whether the CPU is stopped.
366 * Returns: %true if run state is not running or if artificially stopped;
369 bool cpu_is_stopped(CPUState
*cpu
);
373 * @cpu: The vCPU to run on.
374 * @func: The function to be executed.
375 * @data: Data to pass to the function.
377 * Schedules the function @func for execution on the vCPU @cpu.
379 void run_on_cpu(CPUState
*cpu
, void (*func
)(void *data
), void *data
);
383 * @cpu: The vCPU to run on.
384 * @func: The function to be executed.
385 * @data: Data to pass to the function.
387 * Schedules the function @func for execution on the vCPU @cpu asynchronously.
389 void async_run_on_cpu(CPUState
*cpu
, void (*func
)(void *data
), void *data
);
393 * @func: The function to be executed.
394 * @data: Data to pass to the function.
396 * Executes @func for each CPU.
398 void qemu_for_each_cpu(void (*func
)(CPUState
*cpu
, void *data
), void *data
);
402 * @index: The CPUState@cpu_index value of the CPU to obtain.
404 * Gets a CPU matching @index.
406 * Returns: The CPU or %NULL if there is no matching CPU.
408 CPUState
*qemu_get_cpu(int index
);
412 * @id: Guest-exposed CPU ID to lookup.
414 * Search for CPU with specified ID.
416 * Returns: %true - CPU is found, %false - CPU isn't found.
418 bool cpu_exists(int64_t id
);
420 #ifndef CONFIG_USER_ONLY
422 typedef void (*CPUInterruptHandler
)(CPUState
*, int);
424 extern CPUInterruptHandler cpu_interrupt_handler
;
428 * @cpu: The CPU to set an interrupt on.
429 * @mask: The interupts to set.
431 * Invokes the interrupt handler.
433 static inline void cpu_interrupt(CPUState
*cpu
, int mask
)
435 cpu_interrupt_handler(cpu
, mask
);
438 #else /* USER_ONLY */
440 void cpu_interrupt(CPUState
*cpu
, int mask
);
442 #endif /* USER_ONLY */
444 #ifndef CONFIG_USER_ONLY
446 static inline void cpu_unassigned_access(CPUState
*cpu
, hwaddr addr
,
447 bool is_write
, bool is_exec
,
448 int opaque
, unsigned size
)
450 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
452 if (cc
->do_unassigned_access
) {
453 cc
->do_unassigned_access(cpu
, addr
, is_write
, is_exec
, opaque
, size
);
460 * cpu_reset_interrupt:
461 * @cpu: The CPU to clear the interrupt on.
462 * @mask: The interrupt mask to clear.
464 * Resets interrupts on the vCPU @cpu.
466 void cpu_reset_interrupt(CPUState
*cpu
, int mask
);
470 * @cpu: The CPU to exit.
472 * Requests the CPU @cpu to exit execution.
474 void cpu_exit(CPUState
*cpu
);
478 * @cpu: The CPU to resume.
480 * Resumes CPU, i.e. puts CPU into runnable state.
482 void cpu_resume(CPUState
*cpu
);
486 * @cpu: The vCPU to initialize.
488 * Initializes a vCPU.
490 void qemu_init_vcpu(CPUState
*cpu
);
492 #ifdef CONFIG_SOFTMMU
493 extern const struct VMStateDescription vmstate_cpu_common
;
495 #define vmstate_cpu_common vmstate_dummy
498 #define VMSTATE_CPU() { \
499 .name = "parent_obj", \
500 .size = sizeof(CPUState), \
501 .vmsd = &vmstate_cpu_common, \
502 .flags = VMS_STRUCT, \