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 * @vmsd: State description for migration.
50 * Represents a CPU family or model.
52 typedef struct CPUClass
{
54 DeviceClass parent_class
;
57 ObjectClass
*(*class_by_name
)(const char *cpu_model
);
59 void (*reset
)(CPUState
*cpu
);
60 void (*do_interrupt
)(CPUState
*cpu
);
62 const struct VMStateDescription
*vmsd
;
70 * @cpu_index: CPU index (informative).
71 * @nr_cores: Number of cores within this CPU package.
72 * @nr_threads: Number of threads within this CPU.
73 * @numa_node: NUMA node this CPU is belonging to.
74 * @host_tid: Host thread ID.
75 * @running: #true if CPU is currently running (usermode).
76 * @created: Indicates whether the CPU thread has been successfully created.
77 * @interrupt_request: Indicates a pending interrupt request.
78 * @halted: Nonzero if the CPU is in suspended state.
79 * @stop: Indicates a pending stop request.
80 * @stopped: Indicates the CPU has been artificially stopped.
81 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
82 * CPU and return to its top level loop.
83 * @env_ptr: Pointer to subclass-specific CPUArchState field.
84 * @current_tb: Currently executing TB.
85 * @kvm_fd: vCPU file descriptor for KVM.
87 * State of one CPU core or thread.
91 DeviceState parent_obj
;
98 struct QemuThread
*thread
;
105 struct QemuCond
*halt_cond
;
106 struct qemu_work_item
*queued_work_first
, *queued_work_last
;
111 volatile sig_atomic_t exit_request
;
112 volatile sig_atomic_t tcg_exit_req
;
113 uint32_t interrupt_request
;
115 void *env_ptr
; /* CPUArchState */
116 struct TranslationBlock
*current_tb
;
120 struct KVMState
*kvm_state
;
121 struct kvm_run
*kvm_run
;
123 /* TODO Move common fields from CPUArchState here. */
124 int cpu_index
; /* used by alpha TCG */
125 uint32_t halted
; /* used by alpha, cris, ppc TCG */
131 * @cpu: The CPU whose state is to be reset.
133 void cpu_reset(CPUState
*cpu
);
137 * @typename: The CPU base type.
138 * @cpu_model: The model string without any parameters.
140 * Looks up a CPU #ObjectClass matching name @cpu_model.
142 * Returns: A #CPUClass or %NULL if not matching class is found.
144 ObjectClass
*cpu_class_by_name(const char *typename
, const char *cpu_model
);
147 * cpu_class_set_vmsd:
149 * @value: Value to set. Unused for %CONFIG_USER_ONLY.
151 * Sets #VMStateDescription for @cc.
153 * The @value argument is intentionally discarded for the non-softmmu targets
154 * to avoid linker errors or excessive preprocessor usage. If this behavior
155 * is undesired, you should assign #CPUState.vmsd directly instead.
157 #ifndef CONFIG_USER_ONLY
158 static inline void cpu_class_set_vmsd(CPUClass
*cc
,
159 const struct VMStateDescription
*value
)
164 #define cpu_class_set_vmsd(cc, value) ((cc)->vmsd = NULL)
169 * @cpu: The vCPU to check.
171 * Checks whether the CPU has work to do.
173 * Returns: %true if the CPU has work, %false otherwise.
175 bool qemu_cpu_has_work(CPUState
*cpu
);
179 * @cpu: The vCPU to check against.
181 * Checks whether the caller is executing on the vCPU thread.
183 * Returns: %true if called from @cpu's thread, %false otherwise.
185 bool qemu_cpu_is_self(CPUState
*cpu
);
189 * @cpu: The vCPU to kick.
191 * Kicks @cpu's thread.
193 void qemu_cpu_kick(CPUState
*cpu
);
197 * @cpu: The CPU to check.
199 * Checks whether the CPU is stopped.
201 * Returns: %true if run state is not running or if artificially stopped;
204 bool cpu_is_stopped(CPUState
*cpu
);
208 * @cpu: The vCPU to run on.
209 * @func: The function to be executed.
210 * @data: Data to pass to the function.
212 * Schedules the function @func for execution on the vCPU @cpu.
214 void run_on_cpu(CPUState
*cpu
, void (*func
)(void *data
), void *data
);
218 * @index: The CPUState@cpu_index value of the CPU to obtain.
220 * Gets a CPU matching @index.
222 * Returns: The CPU or %NULL if there is no matching CPU.
224 CPUState
*qemu_get_cpu(int index
);
226 #ifndef CONFIG_USER_ONLY
228 typedef void (*CPUInterruptHandler
)(CPUState
*, int);
230 extern CPUInterruptHandler cpu_interrupt_handler
;
234 * @cpu: The CPU to set an interrupt on.
235 * @mask: The interupts to set.
237 * Invokes the interrupt handler.
239 static inline void cpu_interrupt(CPUState
*cpu
, int mask
)
241 cpu_interrupt_handler(cpu
, mask
);
244 #else /* USER_ONLY */
246 void cpu_interrupt(CPUState
*cpu
, int mask
);
248 #endif /* USER_ONLY */
251 * cpu_reset_interrupt:
252 * @cpu: The CPU to clear the interrupt on.
253 * @mask: The interrupt mask to clear.
255 * Resets interrupts on the vCPU @cpu.
257 void cpu_reset_interrupt(CPUState
*cpu
, int mask
);