]> git.proxmox.com Git - qemu.git/blob - include/qom/cpu.h
cpu: Add qemu_for_each_cpu()
[qemu.git] / include / qom / cpu.h
1 /*
2 * QEMU CPU model
3 *
4 * Copyright (c) 2012 SUSE LINUX Products GmbH
5 *
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.
10 *
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.
15 *
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>
19 */
20 #ifndef QEMU_CPU_H
21 #define QEMU_CPU_H
22
23 #include <signal.h>
24 #include "hw/qdev-core.h"
25 #include "qemu/thread.h"
26
27 /**
28 * SECTION:cpu
29 * @section_id: QEMU-cpu
30 * @title: CPU Class
31 * @short_description: Base class for all CPUs
32 */
33
34 #define TYPE_CPU "cpu"
35
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)
39
40 typedef struct CPUState CPUState;
41
42 /**
43 * CPUClass:
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.
50 *
51 * Represents a CPU family or model.
52 */
53 typedef struct CPUClass {
54 /*< private >*/
55 DeviceClass parent_class;
56 /*< public >*/
57
58 ObjectClass *(*class_by_name)(const char *cpu_model);
59
60 void (*reset)(CPUState *cpu);
61 void (*do_interrupt)(CPUState *cpu);
62 int64_t (*get_arch_id)(CPUState *cpu);
63
64 const struct VMStateDescription *vmsd;
65 } CPUClass;
66
67 struct KVMState;
68 struct kvm_run;
69
70 /**
71 * CPUState:
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.
88 *
89 * State of one CPU core or thread.
90 */
91 struct CPUState {
92 /*< private >*/
93 DeviceState parent_obj;
94 /*< public >*/
95
96 int nr_cores;
97 int nr_threads;
98 int numa_node;
99
100 struct QemuThread *thread;
101 #ifdef _WIN32
102 HANDLE hThread;
103 #endif
104 int thread_id;
105 uint32_t host_tid;
106 bool running;
107 struct QemuCond *halt_cond;
108 struct qemu_work_item *queued_work_first, *queued_work_last;
109 bool thread_kicked;
110 bool created;
111 bool stop;
112 bool stopped;
113 volatile sig_atomic_t exit_request;
114 volatile sig_atomic_t tcg_exit_req;
115 uint32_t interrupt_request;
116
117 void *env_ptr; /* CPUArchState */
118 struct TranslationBlock *current_tb;
119
120 int kvm_fd;
121 bool kvm_vcpu_dirty;
122 struct KVMState *kvm_state;
123 struct kvm_run *kvm_run;
124
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 */
128 };
129
130
131 /**
132 * cpu_reset:
133 * @cpu: The CPU whose state is to be reset.
134 */
135 void cpu_reset(CPUState *cpu);
136
137 /**
138 * cpu_class_by_name:
139 * @typename: The CPU base type.
140 * @cpu_model: The model string without any parameters.
141 *
142 * Looks up a CPU #ObjectClass matching name @cpu_model.
143 *
144 * Returns: A #CPUClass or %NULL if not matching class is found.
145 */
146 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
147
148 /**
149 * cpu_class_set_vmsd:
150 * @cc: CPU class
151 * @value: Value to set. Unused for %CONFIG_USER_ONLY.
152 *
153 * Sets #VMStateDescription for @cc.
154 *
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.
158 */
159 #ifndef CONFIG_USER_ONLY
160 static inline void cpu_class_set_vmsd(CPUClass *cc,
161 const struct VMStateDescription *value)
162 {
163 cc->vmsd = value;
164 }
165 #else
166 #define cpu_class_set_vmsd(cc, value) ((cc)->vmsd = NULL)
167 #endif
168
169 /**
170 * qemu_cpu_has_work:
171 * @cpu: The vCPU to check.
172 *
173 * Checks whether the CPU has work to do.
174 *
175 * Returns: %true if the CPU has work, %false otherwise.
176 */
177 bool qemu_cpu_has_work(CPUState *cpu);
178
179 /**
180 * qemu_cpu_is_self:
181 * @cpu: The vCPU to check against.
182 *
183 * Checks whether the caller is executing on the vCPU thread.
184 *
185 * Returns: %true if called from @cpu's thread, %false otherwise.
186 */
187 bool qemu_cpu_is_self(CPUState *cpu);
188
189 /**
190 * qemu_cpu_kick:
191 * @cpu: The vCPU to kick.
192 *
193 * Kicks @cpu's thread.
194 */
195 void qemu_cpu_kick(CPUState *cpu);
196
197 /**
198 * cpu_is_stopped:
199 * @cpu: The CPU to check.
200 *
201 * Checks whether the CPU is stopped.
202 *
203 * Returns: %true if run state is not running or if artificially stopped;
204 * %false otherwise.
205 */
206 bool cpu_is_stopped(CPUState *cpu);
207
208 /**
209 * run_on_cpu:
210 * @cpu: The vCPU to run on.
211 * @func: The function to be executed.
212 * @data: Data to pass to the function.
213 *
214 * Schedules the function @func for execution on the vCPU @cpu.
215 */
216 void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data);
217
218 /**
219 * qemu_for_each_cpu:
220 * @func: The function to be executed.
221 * @data: Data to pass to the function.
222 *
223 * Executes @func for each CPU.
224 */
225 void qemu_for_each_cpu(void (*func)(CPUState *cpu, void *data), void *data);
226
227 /**
228 * qemu_get_cpu:
229 * @index: The CPUState@cpu_index value of the CPU to obtain.
230 *
231 * Gets a CPU matching @index.
232 *
233 * Returns: The CPU or %NULL if there is no matching CPU.
234 */
235 CPUState *qemu_get_cpu(int index);
236
237 #ifndef CONFIG_USER_ONLY
238
239 typedef void (*CPUInterruptHandler)(CPUState *, int);
240
241 extern CPUInterruptHandler cpu_interrupt_handler;
242
243 /**
244 * cpu_interrupt:
245 * @cpu: The CPU to set an interrupt on.
246 * @mask: The interupts to set.
247 *
248 * Invokes the interrupt handler.
249 */
250 static inline void cpu_interrupt(CPUState *cpu, int mask)
251 {
252 cpu_interrupt_handler(cpu, mask);
253 }
254
255 #else /* USER_ONLY */
256
257 void cpu_interrupt(CPUState *cpu, int mask);
258
259 #endif /* USER_ONLY */
260
261 /**
262 * cpu_reset_interrupt:
263 * @cpu: The CPU to clear the interrupt on.
264 * @mask: The interrupt mask to clear.
265 *
266 * Resets interrupts on the vCPU @cpu.
267 */
268 void cpu_reset_interrupt(CPUState *cpu, int mask);
269
270 /**
271 * cpu_resume:
272 * @cpu: The CPU to resume.
273 *
274 * Resumes CPU, i.e. puts CPU into runnable state.
275 */
276 void cpu_resume(CPUState *cpu);
277
278 #endif