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"
27 typedef int (*WriteCoreDumpFunction
)(void *buf
, size_t size
, void *opaque
);
31 * @section_id: QEMU-cpu
33 * @short_description: Base class for all CPUs
36 #define TYPE_CPU "cpu"
38 #define CPU(obj) OBJECT_CHECK(CPUState, (obj), TYPE_CPU)
39 #define CPU_CLASS(class) OBJECT_CLASS_CHECK(CPUClass, (class), TYPE_CPU)
40 #define CPU_GET_CLASS(obj) OBJECT_GET_CLASS(CPUClass, (obj), TYPE_CPU)
42 typedef struct CPUState CPUState
;
46 * @class_by_name: Callback to map -cpu command line model name to an
47 * instantiatable CPU type.
48 * @reset: Callback to reset the #CPUState to its initial state.
49 * @do_interrupt: Callback for interrupt handling.
50 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
51 * @vmsd: State description for migration.
53 * Represents a CPU family or model.
55 typedef struct CPUClass
{
57 DeviceClass parent_class
;
60 ObjectClass
*(*class_by_name
)(const char *cpu_model
);
62 void (*reset
)(CPUState
*cpu
);
63 void (*do_interrupt
)(CPUState
*cpu
);
64 int64_t (*get_arch_id
)(CPUState
*cpu
);
66 const struct VMStateDescription
*vmsd
;
67 int (*write_elf64_note
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
68 int cpuid
, void *opaque
);
69 int (*write_elf64_qemunote
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
71 int (*write_elf32_note
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
72 int cpuid
, void *opaque
);
73 int (*write_elf32_qemunote
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
82 * @cpu_index: CPU index (informative).
83 * @nr_cores: Number of cores within this CPU package.
84 * @nr_threads: Number of threads within this CPU.
85 * @numa_node: NUMA node this CPU is belonging to.
86 * @host_tid: Host thread ID.
87 * @running: #true if CPU is currently running (usermode).
88 * @created: Indicates whether the CPU thread has been successfully created.
89 * @interrupt_request: Indicates a pending interrupt request.
90 * @halted: Nonzero if the CPU is in suspended state.
91 * @stop: Indicates a pending stop request.
92 * @stopped: Indicates the CPU has been artificially stopped.
93 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
94 * CPU and return to its top level loop.
95 * @env_ptr: Pointer to subclass-specific CPUArchState field.
96 * @current_tb: Currently executing TB.
97 * @kvm_fd: vCPU file descriptor for KVM.
99 * State of one CPU core or thread.
103 DeviceState parent_obj
;
110 struct QemuThread
*thread
;
117 struct QemuCond
*halt_cond
;
118 struct qemu_work_item
*queued_work_first
, *queued_work_last
;
123 volatile sig_atomic_t exit_request
;
124 volatile sig_atomic_t tcg_exit_req
;
125 uint32_t interrupt_request
;
127 void *env_ptr
; /* CPUArchState */
128 struct TranslationBlock
*current_tb
;
132 struct KVMState
*kvm_state
;
133 struct kvm_run
*kvm_run
;
135 /* TODO Move common fields from CPUArchState here. */
136 int cpu_index
; /* used by alpha TCG */
137 uint32_t halted
; /* used by alpha, cris, ppc TCG */
141 * cpu_write_elf64_note:
142 * @f: pointer to a function that writes memory to a file
143 * @cpu: The CPU whose memory is to be dumped
144 * @cpuid: ID number of the CPU
145 * @opaque: pointer to the CPUState struct
147 int cpu_write_elf64_note(WriteCoreDumpFunction f
, CPUState
*cpu
,
148 int cpuid
, void *opaque
);
151 * cpu_write_elf64_qemunote:
152 * @f: pointer to a function that writes memory to a file
153 * @cpu: The CPU whose memory is to be dumped
154 * @cpuid: ID number of the CPU
155 * @opaque: pointer to the CPUState struct
157 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f
, CPUState
*cpu
,
161 * cpu_write_elf32_note:
162 * @f: pointer to a function that writes memory to a file
163 * @cpu: The CPU whose memory is to be dumped
164 * @cpuid: ID number of the CPU
165 * @opaque: pointer to the CPUState struct
167 int cpu_write_elf32_note(WriteCoreDumpFunction f
, CPUState
*cpu
,
168 int cpuid
, void *opaque
);
171 * cpu_write_elf32_qemunote:
172 * @f: pointer to a function that writes memory to a file
173 * @cpu: The CPU whose memory is to be dumped
174 * @cpuid: ID number of the CPU
175 * @opaque: pointer to the CPUState struct
177 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f
, CPUState
*cpu
,
182 * @cpu: The CPU whose state is to be reset.
184 void cpu_reset(CPUState
*cpu
);
188 * @typename: The CPU base type.
189 * @cpu_model: The model string without any parameters.
191 * Looks up a CPU #ObjectClass matching name @cpu_model.
193 * Returns: A #CPUClass or %NULL if not matching class is found.
195 ObjectClass
*cpu_class_by_name(const char *typename
, const char *cpu_model
);
198 * cpu_class_set_vmsd:
200 * @value: Value to set. Unused for %CONFIG_USER_ONLY.
202 * Sets #VMStateDescription for @cc.
204 * The @value argument is intentionally discarded for the non-softmmu targets
205 * to avoid linker errors or excessive preprocessor usage. If this behavior
206 * is undesired, you should assign #CPUState.vmsd directly instead.
208 #ifndef CONFIG_USER_ONLY
209 static inline void cpu_class_set_vmsd(CPUClass
*cc
,
210 const struct VMStateDescription
*value
)
215 #define cpu_class_set_vmsd(cc, value) ((cc)->vmsd = NULL)
220 * @cpu: The vCPU to check.
222 * Checks whether the CPU has work to do.
224 * Returns: %true if the CPU has work, %false otherwise.
226 bool qemu_cpu_has_work(CPUState
*cpu
);
230 * @cpu: The vCPU to check against.
232 * Checks whether the caller is executing on the vCPU thread.
234 * Returns: %true if called from @cpu's thread, %false otherwise.
236 bool qemu_cpu_is_self(CPUState
*cpu
);
240 * @cpu: The vCPU to kick.
242 * Kicks @cpu's thread.
244 void qemu_cpu_kick(CPUState
*cpu
);
248 * @cpu: The CPU to check.
250 * Checks whether the CPU is stopped.
252 * Returns: %true if run state is not running or if artificially stopped;
255 bool cpu_is_stopped(CPUState
*cpu
);
259 * @cpu: The vCPU to run on.
260 * @func: The function to be executed.
261 * @data: Data to pass to the function.
263 * Schedules the function @func for execution on the vCPU @cpu.
265 void run_on_cpu(CPUState
*cpu
, void (*func
)(void *data
), void *data
);
269 * @func: The function to be executed.
270 * @data: Data to pass to the function.
272 * Executes @func for each CPU.
274 void qemu_for_each_cpu(void (*func
)(CPUState
*cpu
, void *data
), void *data
);
278 * @index: The CPUState@cpu_index value of the CPU to obtain.
280 * Gets a CPU matching @index.
282 * Returns: The CPU or %NULL if there is no matching CPU.
284 CPUState
*qemu_get_cpu(int index
);
288 * @id: Guest-exposed CPU ID to lookup.
290 * Search for CPU with specified ID.
292 * Returns: %true - CPU is found, %false - CPU isn't found.
294 bool cpu_exists(int64_t id
);
296 #ifndef CONFIG_USER_ONLY
298 typedef void (*CPUInterruptHandler
)(CPUState
*, int);
300 extern CPUInterruptHandler cpu_interrupt_handler
;
304 * @cpu: The CPU to set an interrupt on.
305 * @mask: The interupts to set.
307 * Invokes the interrupt handler.
309 static inline void cpu_interrupt(CPUState
*cpu
, int mask
)
311 cpu_interrupt_handler(cpu
, mask
);
314 #else /* USER_ONLY */
316 void cpu_interrupt(CPUState
*cpu
, int mask
);
318 #endif /* USER_ONLY */
321 * cpu_reset_interrupt:
322 * @cpu: The CPU to clear the interrupt on.
323 * @mask: The interrupt mask to clear.
325 * Resets interrupts on the vCPU @cpu.
327 void cpu_reset_interrupt(CPUState
*cpu
, int mask
);
331 * @cpu: The CPU to resume.
333 * Resumes CPU, i.e. puts CPU into runnable state.
335 void cpu_resume(CPUState
*cpu
);