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 * @do_interrupt: Callback for interrupt handling.
57 * @do_unassigned_access: Callback for unassigned access handling.
58 * @dump_state: Callback for dumping state.
59 * @dump_statistics: Callback for dumping statistics.
60 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
61 * @get_paging_enabled: Callback for inquiring whether paging is enabled.
62 * @get_memory_mapping: Callback for obtaining the memory mappings.
63 * @vmsd: State description for migration.
65 * Represents a CPU family or model.
67 typedef struct CPUClass
{
69 DeviceClass parent_class
;
72 ObjectClass
*(*class_by_name
)(const char *cpu_model
);
74 void (*reset
)(CPUState
*cpu
);
75 void (*do_interrupt
)(CPUState
*cpu
);
76 CPUUnassignedAccess do_unassigned_access
;
77 void (*dump_state
)(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
79 void (*dump_statistics
)(CPUState
*cpu
, FILE *f
,
80 fprintf_function cpu_fprintf
, int flags
);
81 int64_t (*get_arch_id
)(CPUState
*cpu
);
82 bool (*get_paging_enabled
)(const CPUState
*cpu
);
83 void (*get_memory_mapping
)(CPUState
*cpu
, MemoryMappingList
*list
,
86 const struct VMStateDescription
*vmsd
;
87 int (*write_elf64_note
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
88 int cpuid
, void *opaque
);
89 int (*write_elf64_qemunote
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
91 int (*write_elf32_note
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
92 int cpuid
, void *opaque
);
93 int (*write_elf32_qemunote
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
102 * @cpu_index: CPU index (informative).
103 * @nr_cores: Number of cores within this CPU package.
104 * @nr_threads: Number of threads within this CPU.
105 * @numa_node: NUMA node this CPU is belonging to.
106 * @host_tid: Host thread ID.
107 * @running: #true if CPU is currently running (usermode).
108 * @created: Indicates whether the CPU thread has been successfully created.
109 * @interrupt_request: Indicates a pending interrupt request.
110 * @halted: Nonzero if the CPU is in suspended state.
111 * @stop: Indicates a pending stop request.
112 * @stopped: Indicates the CPU has been artificially stopped.
113 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
114 * CPU and return to its top level loop.
115 * @env_ptr: Pointer to subclass-specific CPUArchState field.
116 * @current_tb: Currently executing TB.
117 * @next_cpu: Next CPU sharing TB cache.
118 * @kvm_fd: vCPU file descriptor for KVM.
120 * State of one CPU core or thread.
124 DeviceState parent_obj
;
131 struct QemuThread
*thread
;
138 struct QemuCond
*halt_cond
;
139 struct qemu_work_item
*queued_work_first
, *queued_work_last
;
144 volatile sig_atomic_t exit_request
;
145 volatile sig_atomic_t tcg_exit_req
;
146 uint32_t interrupt_request
;
148 void *env_ptr
; /* CPUArchState */
149 struct TranslationBlock
*current_tb
;
154 struct KVMState
*kvm_state
;
155 struct kvm_run
*kvm_run
;
157 /* TODO Move common fields from CPUArchState here. */
158 int cpu_index
; /* used by alpha TCG */
159 uint32_t halted
; /* used by alpha, cris, ppc TCG */
162 extern CPUState
*first_cpu
;
164 DECLARE_TLS(CPUState
*, current_cpu
);
165 #define current_cpu tls_var(current_cpu)
168 * cpu_paging_enabled:
169 * @cpu: The CPU whose state is to be inspected.
171 * Returns: %true if paging is enabled, %false otherwise.
173 bool cpu_paging_enabled(const CPUState
*cpu
);
176 * cpu_get_memory_mapping:
177 * @cpu: The CPU whose memory mappings are to be obtained.
178 * @list: Where to write the memory mappings to.
179 * @errp: Pointer for reporting an #Error.
181 void cpu_get_memory_mapping(CPUState
*cpu
, MemoryMappingList
*list
,
185 * cpu_write_elf64_note:
186 * @f: pointer to a function that writes memory to a file
187 * @cpu: The CPU whose memory is to be dumped
188 * @cpuid: ID number of the CPU
189 * @opaque: pointer to the CPUState struct
191 int cpu_write_elf64_note(WriteCoreDumpFunction f
, CPUState
*cpu
,
192 int cpuid
, void *opaque
);
195 * cpu_write_elf64_qemunote:
196 * @f: pointer to a function that writes memory to a file
197 * @cpu: The CPU whose memory is to be dumped
198 * @cpuid: ID number of the CPU
199 * @opaque: pointer to the CPUState struct
201 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f
, CPUState
*cpu
,
205 * cpu_write_elf32_note:
206 * @f: pointer to a function that writes memory to a file
207 * @cpu: The CPU whose memory is to be dumped
208 * @cpuid: ID number of the CPU
209 * @opaque: pointer to the CPUState struct
211 int cpu_write_elf32_note(WriteCoreDumpFunction f
, CPUState
*cpu
,
212 int cpuid
, void *opaque
);
215 * cpu_write_elf32_qemunote:
216 * @f: pointer to a function that writes memory to a file
217 * @cpu: The CPU whose memory is to be dumped
218 * @cpuid: ID number of the CPU
219 * @opaque: pointer to the CPUState struct
221 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f
, CPUState
*cpu
,
227 * @CPU_DUMP_FPU: dump FPU register state, not just integer
228 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
231 CPU_DUMP_CODE
= 0x00010000,
232 CPU_DUMP_FPU
= 0x00020000,
233 CPU_DUMP_CCOP
= 0x00040000,
238 * @cpu: The CPU whose state is to be dumped.
239 * @f: File to dump to.
240 * @cpu_fprintf: Function to dump with.
241 * @flags: Flags what to dump.
245 void cpu_dump_state(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
249 * cpu_dump_statistics:
250 * @cpu: The CPU whose state is to be dumped.
251 * @f: File to dump to.
252 * @cpu_fprintf: Function to dump with.
253 * @flags: Flags what to dump.
255 * Dumps CPU statistics.
257 void cpu_dump_statistics(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
262 * @cpu: The CPU whose state is to be reset.
264 void cpu_reset(CPUState
*cpu
);
268 * @typename: The CPU base type.
269 * @cpu_model: The model string without any parameters.
271 * Looks up a CPU #ObjectClass matching name @cpu_model.
273 * Returns: A #CPUClass or %NULL if not matching class is found.
275 ObjectClass
*cpu_class_by_name(const char *typename
, const char *cpu_model
);
278 * cpu_class_set_vmsd:
280 * @value: Value to set. Unused for %CONFIG_USER_ONLY.
282 * Sets #VMStateDescription for @cc.
284 * The @value argument is intentionally discarded for the non-softmmu targets
285 * to avoid linker errors or excessive preprocessor usage. If this behavior
286 * is undesired, you should assign #CPUClass.vmsd directly instead.
288 #ifndef CONFIG_USER_ONLY
289 static inline void cpu_class_set_vmsd(CPUClass
*cc
,
290 const struct VMStateDescription
*value
)
295 #define cpu_class_set_vmsd(cc, value) ((cc)->vmsd = NULL)
298 #ifndef CONFIG_USER_ONLY
299 static inline void cpu_class_set_do_unassigned_access(CPUClass
*cc
,
300 CPUUnassignedAccess value
)
302 cc
->do_unassigned_access
= value
;
305 #define cpu_class_set_do_unassigned_access(cc, value) \
306 ((cc)->do_unassigned_access = NULL)
310 * device_class_set_vmsd:
312 * @value: Value to set. Unused for %CONFIG_USER_ONLY.
314 * Sets #VMStateDescription for @dc.
316 * The @value argument is intentionally discarded for the non-softmmu targets
317 * to avoid linker errors or excessive preprocessor usage. If this behavior
318 * is undesired, you should assign #DeviceClass.vmsd directly instead.
320 #ifndef CONFIG_USER_ONLY
321 static inline void device_class_set_vmsd(DeviceClass
*dc
,
322 const struct VMStateDescription
*value
)
327 #define device_class_set_vmsd(dc, value) ((dc)->vmsd = NULL)
332 * @cpu: The vCPU to check.
334 * Checks whether the CPU has work to do.
336 * Returns: %true if the CPU has work, %false otherwise.
338 bool qemu_cpu_has_work(CPUState
*cpu
);
342 * @cpu: The vCPU to check against.
344 * Checks whether the caller is executing on the vCPU thread.
346 * Returns: %true if called from @cpu's thread, %false otherwise.
348 bool qemu_cpu_is_self(CPUState
*cpu
);
352 * @cpu: The vCPU to kick.
354 * Kicks @cpu's thread.
356 void qemu_cpu_kick(CPUState
*cpu
);
360 * @cpu: The CPU to check.
362 * Checks whether the CPU is stopped.
364 * Returns: %true if run state is not running or if artificially stopped;
367 bool cpu_is_stopped(CPUState
*cpu
);
371 * @cpu: The vCPU to run on.
372 * @func: The function to be executed.
373 * @data: Data to pass to the function.
375 * Schedules the function @func for execution on the vCPU @cpu.
377 void run_on_cpu(CPUState
*cpu
, void (*func
)(void *data
), void *data
);
381 * @func: The function to be executed.
382 * @data: Data to pass to the function.
384 * Executes @func for each CPU.
386 void qemu_for_each_cpu(void (*func
)(CPUState
*cpu
, void *data
), void *data
);
390 * @index: The CPUState@cpu_index value of the CPU to obtain.
392 * Gets a CPU matching @index.
394 * Returns: The CPU or %NULL if there is no matching CPU.
396 CPUState
*qemu_get_cpu(int index
);
400 * @id: Guest-exposed CPU ID to lookup.
402 * Search for CPU with specified ID.
404 * Returns: %true - CPU is found, %false - CPU isn't found.
406 bool cpu_exists(int64_t id
);
408 #ifndef CONFIG_USER_ONLY
410 typedef void (*CPUInterruptHandler
)(CPUState
*, int);
412 extern CPUInterruptHandler cpu_interrupt_handler
;
416 * @cpu: The CPU to set an interrupt on.
417 * @mask: The interupts to set.
419 * Invokes the interrupt handler.
421 static inline void cpu_interrupt(CPUState
*cpu
, int mask
)
423 cpu_interrupt_handler(cpu
, mask
);
426 #else /* USER_ONLY */
428 void cpu_interrupt(CPUState
*cpu
, int mask
);
430 #endif /* USER_ONLY */
432 #ifndef CONFIG_USER_ONLY
434 static inline void cpu_unassigned_access(CPUState
*cpu
, hwaddr addr
,
435 bool is_write
, bool is_exec
,
436 int opaque
, unsigned size
)
438 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
440 if (cc
->do_unassigned_access
) {
441 cc
->do_unassigned_access(cpu
, addr
, is_write
, is_exec
, opaque
, size
);
448 * cpu_reset_interrupt:
449 * @cpu: The CPU to clear the interrupt on.
450 * @mask: The interrupt mask to clear.
452 * Resets interrupts on the vCPU @cpu.
454 void cpu_reset_interrupt(CPUState
*cpu
, int mask
);
458 * @cpu: The CPU to exit.
460 * Requests the CPU @cpu to exit execution.
462 void cpu_exit(CPUState
*cpu
);
466 * @cpu: The CPU to resume.
468 * Resumes CPU, i.e. puts CPU into runnable state.
470 void cpu_resume(CPUState
*cpu
);
474 * @cpu: The vCPU to initialize.
476 * Initializes a vCPU.
478 void qemu_init_vcpu(CPUState
*cpu
);
480 #ifdef CONFIG_SOFTMMU
481 extern const struct VMStateDescription vmstate_cpu_common
;
483 #define vmstate_cpu_common vmstate_dummy
486 #define VMSTATE_CPU() { \
487 .name = "parent_obj", \
488 .size = sizeof(CPUState), \
489 .vmsd = &vmstate_cpu_common, \
490 .flags = VMS_STRUCT, \