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"
29 * @section_id: QEMU-cpu
31 * @short_description: Base class for all CPUs
34 #define TYPE_CPU "cpu"
36 #define CPU(obj) OBJECT_CHECK(CPUState, (obj), TYPE_CPU)
37 #define CPU_CLASS(class) OBJECT_CLASS_CHECK(CPUClass, (class), TYPE_CPU)
38 #define CPU_GET_CLASS(obj) OBJECT_GET_CLASS(CPUClass, (obj), TYPE_CPU)
40 typedef struct CPUState CPUState
;
44 * @class_by_name: Callback to map -cpu command line model name to an
45 * instantiatable CPU type.
46 * @reset: Callback to reset the #CPUState to its initial state.
47 * @do_interrupt: Callback for interrupt handling.
48 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
49 * @vmsd: State description for migration.
51 * Represents a CPU family or model.
53 typedef struct CPUClass
{
55 DeviceClass parent_class
;
58 ObjectClass
*(*class_by_name
)(const char *cpu_model
);
60 void (*reset
)(CPUState
*cpu
);
61 void (*do_interrupt
)(CPUState
*cpu
);
62 int64_t (*get_arch_id
)(CPUState
*cpu
);
64 const struct VMStateDescription
*vmsd
;
72 * @cpu_index: CPU index (informative).
73 * @nr_cores: Number of cores within this CPU package.
74 * @nr_threads: Number of threads within this CPU.
75 * @numa_node: NUMA node this CPU is belonging to.
76 * @host_tid: Host thread ID.
77 * @running: #true if CPU is currently running (usermode).
78 * @created: Indicates whether the CPU thread has been successfully created.
79 * @interrupt_request: Indicates a pending interrupt request.
80 * @halted: Nonzero if the CPU is in suspended state.
81 * @stop: Indicates a pending stop request.
82 * @stopped: Indicates the CPU has been artificially stopped.
83 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
84 * CPU and return to its top level loop.
85 * @env_ptr: Pointer to subclass-specific CPUArchState field.
86 * @current_tb: Currently executing TB.
87 * @kvm_fd: vCPU file descriptor for KVM.
89 * State of one CPU core or thread.
93 DeviceState parent_obj
;
100 struct QemuThread
*thread
;
107 struct QemuCond
*halt_cond
;
108 struct qemu_work_item
*queued_work_first
, *queued_work_last
;
113 volatile sig_atomic_t exit_request
;
114 volatile sig_atomic_t tcg_exit_req
;
115 uint32_t interrupt_request
;
117 void *env_ptr
; /* CPUArchState */
118 struct TranslationBlock
*current_tb
;
122 struct KVMState
*kvm_state
;
123 struct kvm_run
*kvm_run
;
125 /* TODO Move common fields from CPUArchState here. */
126 int cpu_index
; /* used by alpha TCG */
127 uint32_t halted
; /* used by alpha, cris, ppc TCG */
133 * @cpu: The CPU whose state is to be reset.
135 void cpu_reset(CPUState
*cpu
);
139 * @typename: The CPU base type.
140 * @cpu_model: The model string without any parameters.
142 * Looks up a CPU #ObjectClass matching name @cpu_model.
144 * Returns: A #CPUClass or %NULL if not matching class is found.
146 ObjectClass
*cpu_class_by_name(const char *typename
, const char *cpu_model
);
149 * cpu_class_set_vmsd:
151 * @value: Value to set. Unused for %CONFIG_USER_ONLY.
153 * Sets #VMStateDescription for @cc.
155 * The @value argument is intentionally discarded for the non-softmmu targets
156 * to avoid linker errors or excessive preprocessor usage. If this behavior
157 * is undesired, you should assign #CPUState.vmsd directly instead.
159 #ifndef CONFIG_USER_ONLY
160 static inline void cpu_class_set_vmsd(CPUClass
*cc
,
161 const struct VMStateDescription
*value
)
166 #define cpu_class_set_vmsd(cc, value) ((cc)->vmsd = NULL)
171 * @cpu: The vCPU to check.
173 * Checks whether the CPU has work to do.
175 * Returns: %true if the CPU has work, %false otherwise.
177 bool qemu_cpu_has_work(CPUState
*cpu
);
181 * @cpu: The vCPU to check against.
183 * Checks whether the caller is executing on the vCPU thread.
185 * Returns: %true if called from @cpu's thread, %false otherwise.
187 bool qemu_cpu_is_self(CPUState
*cpu
);
191 * @cpu: The vCPU to kick.
193 * Kicks @cpu's thread.
195 void qemu_cpu_kick(CPUState
*cpu
);
199 * @cpu: The CPU to check.
201 * Checks whether the CPU is stopped.
203 * Returns: %true if run state is not running or if artificially stopped;
206 bool cpu_is_stopped(CPUState
*cpu
);
210 * @cpu: The vCPU to run on.
211 * @func: The function to be executed.
212 * @data: Data to pass to the function.
214 * Schedules the function @func for execution on the vCPU @cpu.
216 void run_on_cpu(CPUState
*cpu
, void (*func
)(void *data
), void *data
);
220 * @func: The function to be executed.
221 * @data: Data to pass to the function.
223 * Executes @func for each CPU.
225 void qemu_for_each_cpu(void (*func
)(CPUState
*cpu
, void *data
), void *data
);
229 * @index: The CPUState@cpu_index value of the CPU to obtain.
231 * Gets a CPU matching @index.
233 * Returns: The CPU or %NULL if there is no matching CPU.
235 CPUState
*qemu_get_cpu(int index
);
239 * @id: Guest-exposed CPU ID to lookup.
241 * Search for CPU with specified ID.
243 * Returns: %true - CPU is found, %false - CPU isn't found.
245 bool cpu_exists(int64_t id
);
247 #ifndef CONFIG_USER_ONLY
249 typedef void (*CPUInterruptHandler
)(CPUState
*, int);
251 extern CPUInterruptHandler cpu_interrupt_handler
;
255 * @cpu: The CPU to set an interrupt on.
256 * @mask: The interupts to set.
258 * Invokes the interrupt handler.
260 static inline void cpu_interrupt(CPUState
*cpu
, int mask
)
262 cpu_interrupt_handler(cpu
, mask
);
265 #else /* USER_ONLY */
267 void cpu_interrupt(CPUState
*cpu
, int mask
);
269 #endif /* USER_ONLY */
272 * cpu_reset_interrupt:
273 * @cpu: The CPU to clear the interrupt on.
274 * @mask: The interrupt mask to clear.
276 * Resets interrupts on the vCPU @cpu.
278 void cpu_reset_interrupt(CPUState
*cpu
, int mask
);
282 * @cpu: The CPU to resume.
284 * Resumes CPU, i.e. puts CPU into runnable state.
286 void cpu_resume(CPUState
*cpu
);