]> git.proxmox.com Git - qemu.git/blob - include/qom/cpu.h
cpu: Change cpu_exit() argument to CPUState
[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 #include "qemu/typedefs.h"
27
28 typedef int (*WriteCoreDumpFunction)(void *buf, size_t size, void *opaque);
29
30 /**
31 * SECTION:cpu
32 * @section_id: QEMU-cpu
33 * @title: CPU Class
34 * @short_description: Base class for all CPUs
35 */
36
37 #define TYPE_CPU "cpu"
38
39 #define CPU(obj) OBJECT_CHECK(CPUState, (obj), TYPE_CPU)
40 #define CPU_CLASS(class) OBJECT_CLASS_CHECK(CPUClass, (class), TYPE_CPU)
41 #define CPU_GET_CLASS(obj) OBJECT_GET_CLASS(CPUClass, (obj), TYPE_CPU)
42
43 typedef struct CPUState CPUState;
44
45 /**
46 * CPUClass:
47 * @class_by_name: Callback to map -cpu command line model name to an
48 * instantiatable CPU type.
49 * @reset: Callback to reset the #CPUState to its initial state.
50 * @do_interrupt: Callback for interrupt handling.
51 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
52 * @get_paging_enabled: Callback for inquiring whether paging is enabled.
53 * @get_memory_mapping: Callback for obtaining the memory mappings.
54 * @vmsd: State description for migration.
55 *
56 * Represents a CPU family or model.
57 */
58 typedef struct CPUClass {
59 /*< private >*/
60 DeviceClass parent_class;
61 /*< public >*/
62
63 ObjectClass *(*class_by_name)(const char *cpu_model);
64
65 void (*reset)(CPUState *cpu);
66 void (*do_interrupt)(CPUState *cpu);
67 int64_t (*get_arch_id)(CPUState *cpu);
68 bool (*get_paging_enabled)(const CPUState *cpu);
69 void (*get_memory_mapping)(CPUState *cpu, MemoryMappingList *list,
70 Error **errp);
71
72 const struct VMStateDescription *vmsd;
73 int (*write_elf64_note)(WriteCoreDumpFunction f, CPUState *cpu,
74 int cpuid, void *opaque);
75 int (*write_elf64_qemunote)(WriteCoreDumpFunction f, CPUState *cpu,
76 void *opaque);
77 int (*write_elf32_note)(WriteCoreDumpFunction f, CPUState *cpu,
78 int cpuid, void *opaque);
79 int (*write_elf32_qemunote)(WriteCoreDumpFunction f, CPUState *cpu,
80 void *opaque);
81 } CPUClass;
82
83 struct KVMState;
84 struct kvm_run;
85
86 /**
87 * CPUState:
88 * @cpu_index: CPU index (informative).
89 * @nr_cores: Number of cores within this CPU package.
90 * @nr_threads: Number of threads within this CPU.
91 * @numa_node: NUMA node this CPU is belonging to.
92 * @host_tid: Host thread ID.
93 * @running: #true if CPU is currently running (usermode).
94 * @created: Indicates whether the CPU thread has been successfully created.
95 * @interrupt_request: Indicates a pending interrupt request.
96 * @halted: Nonzero if the CPU is in suspended state.
97 * @stop: Indicates a pending stop request.
98 * @stopped: Indicates the CPU has been artificially stopped.
99 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
100 * CPU and return to its top level loop.
101 * @env_ptr: Pointer to subclass-specific CPUArchState field.
102 * @current_tb: Currently executing TB.
103 * @kvm_fd: vCPU file descriptor for KVM.
104 *
105 * State of one CPU core or thread.
106 */
107 struct CPUState {
108 /*< private >*/
109 DeviceState parent_obj;
110 /*< public >*/
111
112 int nr_cores;
113 int nr_threads;
114 int numa_node;
115
116 struct QemuThread *thread;
117 #ifdef _WIN32
118 HANDLE hThread;
119 #endif
120 int thread_id;
121 uint32_t host_tid;
122 bool running;
123 struct QemuCond *halt_cond;
124 struct qemu_work_item *queued_work_first, *queued_work_last;
125 bool thread_kicked;
126 bool created;
127 bool stop;
128 bool stopped;
129 volatile sig_atomic_t exit_request;
130 volatile sig_atomic_t tcg_exit_req;
131 uint32_t interrupt_request;
132
133 void *env_ptr; /* CPUArchState */
134 struct TranslationBlock *current_tb;
135
136 int kvm_fd;
137 bool kvm_vcpu_dirty;
138 struct KVMState *kvm_state;
139 struct kvm_run *kvm_run;
140
141 /* TODO Move common fields from CPUArchState here. */
142 int cpu_index; /* used by alpha TCG */
143 uint32_t halted; /* used by alpha, cris, ppc TCG */
144 };
145
146 /**
147 * cpu_paging_enabled:
148 * @cpu: The CPU whose state is to be inspected.
149 *
150 * Returns: %true if paging is enabled, %false otherwise.
151 */
152 bool cpu_paging_enabled(const CPUState *cpu);
153
154 /**
155 * cpu_get_memory_mapping:
156 * @cpu: The CPU whose memory mappings are to be obtained.
157 * @list: Where to write the memory mappings to.
158 * @errp: Pointer for reporting an #Error.
159 */
160 void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
161 Error **errp);
162
163 /**
164 * cpu_write_elf64_note:
165 * @f: pointer to a function that writes memory to a file
166 * @cpu: The CPU whose memory is to be dumped
167 * @cpuid: ID number of the CPU
168 * @opaque: pointer to the CPUState struct
169 */
170 int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
171 int cpuid, void *opaque);
172
173 /**
174 * cpu_write_elf64_qemunote:
175 * @f: pointer to a function that writes memory to a file
176 * @cpu: The CPU whose memory is to be dumped
177 * @cpuid: ID number of the CPU
178 * @opaque: pointer to the CPUState struct
179 */
180 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
181 void *opaque);
182
183 /**
184 * cpu_write_elf32_note:
185 * @f: pointer to a function that writes memory to a file
186 * @cpu: The CPU whose memory is to be dumped
187 * @cpuid: ID number of the CPU
188 * @opaque: pointer to the CPUState struct
189 */
190 int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
191 int cpuid, void *opaque);
192
193 /**
194 * cpu_write_elf32_qemunote:
195 * @f: pointer to a function that writes memory to a file
196 * @cpu: The CPU whose memory is to be dumped
197 * @cpuid: ID number of the CPU
198 * @opaque: pointer to the CPUState struct
199 */
200 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
201 void *opaque);
202
203 /**
204 * cpu_reset:
205 * @cpu: The CPU whose state is to be reset.
206 */
207 void cpu_reset(CPUState *cpu);
208
209 /**
210 * cpu_class_by_name:
211 * @typename: The CPU base type.
212 * @cpu_model: The model string without any parameters.
213 *
214 * Looks up a CPU #ObjectClass matching name @cpu_model.
215 *
216 * Returns: A #CPUClass or %NULL if not matching class is found.
217 */
218 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
219
220 /**
221 * cpu_class_set_vmsd:
222 * @cc: CPU class
223 * @value: Value to set. Unused for %CONFIG_USER_ONLY.
224 *
225 * Sets #VMStateDescription for @cc.
226 *
227 * The @value argument is intentionally discarded for the non-softmmu targets
228 * to avoid linker errors or excessive preprocessor usage. If this behavior
229 * is undesired, you should assign #CPUClass.vmsd directly instead.
230 */
231 #ifndef CONFIG_USER_ONLY
232 static inline void cpu_class_set_vmsd(CPUClass *cc,
233 const struct VMStateDescription *value)
234 {
235 cc->vmsd = value;
236 }
237 #else
238 #define cpu_class_set_vmsd(cc, value) ((cc)->vmsd = NULL)
239 #endif
240
241 /**
242 * device_class_set_vmsd:
243 * @dc: Device class
244 * @value: Value to set. Unused for %CONFIG_USER_ONLY.
245 *
246 * Sets #VMStateDescription for @dc.
247 *
248 * The @value argument is intentionally discarded for the non-softmmu targets
249 * to avoid linker errors or excessive preprocessor usage. If this behavior
250 * is undesired, you should assign #DeviceClass.vmsd directly instead.
251 */
252 #ifndef CONFIG_USER_ONLY
253 static inline void device_class_set_vmsd(DeviceClass *dc,
254 const struct VMStateDescription *value)
255 {
256 dc->vmsd = value;
257 }
258 #else
259 #define device_class_set_vmsd(dc, value) ((dc)->vmsd = NULL)
260 #endif
261
262 /**
263 * qemu_cpu_has_work:
264 * @cpu: The vCPU to check.
265 *
266 * Checks whether the CPU has work to do.
267 *
268 * Returns: %true if the CPU has work, %false otherwise.
269 */
270 bool qemu_cpu_has_work(CPUState *cpu);
271
272 /**
273 * qemu_cpu_is_self:
274 * @cpu: The vCPU to check against.
275 *
276 * Checks whether the caller is executing on the vCPU thread.
277 *
278 * Returns: %true if called from @cpu's thread, %false otherwise.
279 */
280 bool qemu_cpu_is_self(CPUState *cpu);
281
282 /**
283 * qemu_cpu_kick:
284 * @cpu: The vCPU to kick.
285 *
286 * Kicks @cpu's thread.
287 */
288 void qemu_cpu_kick(CPUState *cpu);
289
290 /**
291 * cpu_is_stopped:
292 * @cpu: The CPU to check.
293 *
294 * Checks whether the CPU is stopped.
295 *
296 * Returns: %true if run state is not running or if artificially stopped;
297 * %false otherwise.
298 */
299 bool cpu_is_stopped(CPUState *cpu);
300
301 /**
302 * run_on_cpu:
303 * @cpu: The vCPU to run on.
304 * @func: The function to be executed.
305 * @data: Data to pass to the function.
306 *
307 * Schedules the function @func for execution on the vCPU @cpu.
308 */
309 void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data);
310
311 /**
312 * qemu_for_each_cpu:
313 * @func: The function to be executed.
314 * @data: Data to pass to the function.
315 *
316 * Executes @func for each CPU.
317 */
318 void qemu_for_each_cpu(void (*func)(CPUState *cpu, void *data), void *data);
319
320 /**
321 * qemu_get_cpu:
322 * @index: The CPUState@cpu_index value of the CPU to obtain.
323 *
324 * Gets a CPU matching @index.
325 *
326 * Returns: The CPU or %NULL if there is no matching CPU.
327 */
328 CPUState *qemu_get_cpu(int index);
329
330 /**
331 * cpu_exists:
332 * @id: Guest-exposed CPU ID to lookup.
333 *
334 * Search for CPU with specified ID.
335 *
336 * Returns: %true - CPU is found, %false - CPU isn't found.
337 */
338 bool cpu_exists(int64_t id);
339
340 #ifndef CONFIG_USER_ONLY
341
342 typedef void (*CPUInterruptHandler)(CPUState *, int);
343
344 extern CPUInterruptHandler cpu_interrupt_handler;
345
346 /**
347 * cpu_interrupt:
348 * @cpu: The CPU to set an interrupt on.
349 * @mask: The interupts to set.
350 *
351 * Invokes the interrupt handler.
352 */
353 static inline void cpu_interrupt(CPUState *cpu, int mask)
354 {
355 cpu_interrupt_handler(cpu, mask);
356 }
357
358 #else /* USER_ONLY */
359
360 void cpu_interrupt(CPUState *cpu, int mask);
361
362 #endif /* USER_ONLY */
363
364 /**
365 * cpu_reset_interrupt:
366 * @cpu: The CPU to clear the interrupt on.
367 * @mask: The interrupt mask to clear.
368 *
369 * Resets interrupts on the vCPU @cpu.
370 */
371 void cpu_reset_interrupt(CPUState *cpu, int mask);
372
373 /**
374 * cpu_exit:
375 * @cpu: The CPU to exit.
376 *
377 * Requests the CPU @cpu to exit execution.
378 */
379 void cpu_exit(CPUState *cpu);
380
381 /**
382 * cpu_resume:
383 * @cpu: The CPU to resume.
384 *
385 * Resumes CPU, i.e. puts CPU into runnable state.
386 */
387 void cpu_resume(CPUState *cpu);
388
389 #ifdef CONFIG_SOFTMMU
390 extern const struct VMStateDescription vmstate_cpu_common;
391 #else
392 #define vmstate_cpu_common vmstate_dummy
393 #endif
394
395 #define VMSTATE_CPU() { \
396 .name = "parent_obj", \
397 .size = sizeof(CPUState), \
398 .vmsd = &vmstate_cpu_common, \
399 .flags = VMS_STRUCT, \
400 .offset = 0, \
401 }
402
403 #endif