]>
Commit | Line | Data |
---|---|---|
dd83b06a AF |
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 | ||
fcd7d003 | 23 | #include <signal.h> |
961f8395 | 24 | #include "hw/qdev-core.h" |
1de7afc9 | 25 | #include "qemu/thread.h" |
a23bbfda | 26 | #include "qemu/typedefs.h" |
dd83b06a | 27 | |
c72bf468 JF |
28 | typedef int (*WriteCoreDumpFunction)(void *buf, size_t size, void *opaque); |
29 | ||
dd83b06a AF |
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: | |
2b8c2754 AF |
47 | * @class_by_name: Callback to map -cpu command line model name to an |
48 | * instantiatable CPU type. | |
f5df5baf | 49 | * @reset: Callback to reset the #CPUState to its initial state. |
97a8ea5a | 50 | * @do_interrupt: Callback for interrupt handling. |
878096ee AF |
51 | * @dump_state: Callback for dumping state. |
52 | * @dump_statistics: Callback for dumping statistics. | |
997395d3 | 53 | * @get_arch_id: Callback for getting architecture-dependent CPU ID. |
444d5590 | 54 | * @get_paging_enabled: Callback for inquiring whether paging is enabled. |
a23bbfda | 55 | * @get_memory_mapping: Callback for obtaining the memory mappings. |
b170fce3 | 56 | * @vmsd: State description for migration. |
dd83b06a AF |
57 | * |
58 | * Represents a CPU family or model. | |
59 | */ | |
60 | typedef struct CPUClass { | |
61 | /*< private >*/ | |
961f8395 | 62 | DeviceClass parent_class; |
dd83b06a AF |
63 | /*< public >*/ |
64 | ||
2b8c2754 AF |
65 | ObjectClass *(*class_by_name)(const char *cpu_model); |
66 | ||
dd83b06a | 67 | void (*reset)(CPUState *cpu); |
97a8ea5a | 68 | void (*do_interrupt)(CPUState *cpu); |
878096ee AF |
69 | void (*dump_state)(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf, |
70 | int flags); | |
71 | void (*dump_statistics)(CPUState *cpu, FILE *f, | |
72 | fprintf_function cpu_fprintf, int flags); | |
997395d3 | 73 | int64_t (*get_arch_id)(CPUState *cpu); |
444d5590 | 74 | bool (*get_paging_enabled)(const CPUState *cpu); |
a23bbfda AF |
75 | void (*get_memory_mapping)(CPUState *cpu, MemoryMappingList *list, |
76 | Error **errp); | |
b170fce3 AF |
77 | |
78 | const struct VMStateDescription *vmsd; | |
c72bf468 JF |
79 | int (*write_elf64_note)(WriteCoreDumpFunction f, CPUState *cpu, |
80 | int cpuid, void *opaque); | |
81 | int (*write_elf64_qemunote)(WriteCoreDumpFunction f, CPUState *cpu, | |
82 | void *opaque); | |
83 | int (*write_elf32_note)(WriteCoreDumpFunction f, CPUState *cpu, | |
84 | int cpuid, void *opaque); | |
85 | int (*write_elf32_qemunote)(WriteCoreDumpFunction f, CPUState *cpu, | |
86 | void *opaque); | |
dd83b06a AF |
87 | } CPUClass; |
88 | ||
a60f24b5 | 89 | struct KVMState; |
f7575c96 | 90 | struct kvm_run; |
a60f24b5 | 91 | |
dd83b06a AF |
92 | /** |
93 | * CPUState: | |
55e5c285 | 94 | * @cpu_index: CPU index (informative). |
ce3960eb AF |
95 | * @nr_cores: Number of cores within this CPU package. |
96 | * @nr_threads: Number of threads within this CPU. | |
1b1ed8dc | 97 | * @numa_node: NUMA node this CPU is belonging to. |
0d34282f | 98 | * @host_tid: Host thread ID. |
0315c31c | 99 | * @running: #true if CPU is currently running (usermode). |
61a46217 | 100 | * @created: Indicates whether the CPU thread has been successfully created. |
259186a7 AF |
101 | * @interrupt_request: Indicates a pending interrupt request. |
102 | * @halted: Nonzero if the CPU is in suspended state. | |
4fdeee7c | 103 | * @stop: Indicates a pending stop request. |
f324e766 | 104 | * @stopped: Indicates the CPU has been artificially stopped. |
378df4b2 PM |
105 | * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this |
106 | * CPU and return to its top level loop. | |
c05efcb1 | 107 | * @env_ptr: Pointer to subclass-specific CPUArchState field. |
d77953b9 | 108 | * @current_tb: Currently executing TB. |
8737c51c | 109 | * @kvm_fd: vCPU file descriptor for KVM. |
dd83b06a AF |
110 | * |
111 | * State of one CPU core or thread. | |
112 | */ | |
113 | struct CPUState { | |
114 | /*< private >*/ | |
961f8395 | 115 | DeviceState parent_obj; |
dd83b06a AF |
116 | /*< public >*/ |
117 | ||
ce3960eb AF |
118 | int nr_cores; |
119 | int nr_threads; | |
1b1ed8dc | 120 | int numa_node; |
ce3960eb | 121 | |
814e612e | 122 | struct QemuThread *thread; |
bcba2a72 AF |
123 | #ifdef _WIN32 |
124 | HANDLE hThread; | |
125 | #endif | |
9f09e18a | 126 | int thread_id; |
0d34282f | 127 | uint32_t host_tid; |
0315c31c | 128 | bool running; |
f5c121b8 | 129 | struct QemuCond *halt_cond; |
c64ca814 | 130 | struct qemu_work_item *queued_work_first, *queued_work_last; |
216fc9a4 | 131 | bool thread_kicked; |
61a46217 | 132 | bool created; |
4fdeee7c | 133 | bool stop; |
f324e766 | 134 | bool stopped; |
fcd7d003 | 135 | volatile sig_atomic_t exit_request; |
378df4b2 | 136 | volatile sig_atomic_t tcg_exit_req; |
259186a7 | 137 | uint32_t interrupt_request; |
bcba2a72 | 138 | |
c05efcb1 | 139 | void *env_ptr; /* CPUArchState */ |
d77953b9 AF |
140 | struct TranslationBlock *current_tb; |
141 | ||
8737c51c | 142 | int kvm_fd; |
20d695a9 | 143 | bool kvm_vcpu_dirty; |
a60f24b5 | 144 | struct KVMState *kvm_state; |
f7575c96 | 145 | struct kvm_run *kvm_run; |
8737c51c | 146 | |
f5df5baf | 147 | /* TODO Move common fields from CPUArchState here. */ |
55e5c285 | 148 | int cpu_index; /* used by alpha TCG */ |
259186a7 | 149 | uint32_t halted; /* used by alpha, cris, ppc TCG */ |
dd83b06a AF |
150 | }; |
151 | ||
444d5590 AF |
152 | /** |
153 | * cpu_paging_enabled: | |
154 | * @cpu: The CPU whose state is to be inspected. | |
155 | * | |
156 | * Returns: %true if paging is enabled, %false otherwise. | |
157 | */ | |
158 | bool cpu_paging_enabled(const CPUState *cpu); | |
159 | ||
a23bbfda AF |
160 | /** |
161 | * cpu_get_memory_mapping: | |
162 | * @cpu: The CPU whose memory mappings are to be obtained. | |
163 | * @list: Where to write the memory mappings to. | |
164 | * @errp: Pointer for reporting an #Error. | |
165 | */ | |
166 | void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list, | |
167 | Error **errp); | |
168 | ||
c72bf468 JF |
169 | /** |
170 | * cpu_write_elf64_note: | |
171 | * @f: pointer to a function that writes memory to a file | |
172 | * @cpu: The CPU whose memory is to be dumped | |
173 | * @cpuid: ID number of the CPU | |
174 | * @opaque: pointer to the CPUState struct | |
175 | */ | |
176 | int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu, | |
177 | int cpuid, void *opaque); | |
178 | ||
179 | /** | |
180 | * cpu_write_elf64_qemunote: | |
181 | * @f: pointer to a function that writes memory to a file | |
182 | * @cpu: The CPU whose memory is to be dumped | |
183 | * @cpuid: ID number of the CPU | |
184 | * @opaque: pointer to the CPUState struct | |
185 | */ | |
186 | int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu, | |
187 | void *opaque); | |
188 | ||
189 | /** | |
190 | * cpu_write_elf32_note: | |
191 | * @f: pointer to a function that writes memory to a file | |
192 | * @cpu: The CPU whose memory is to be dumped | |
193 | * @cpuid: ID number of the CPU | |
194 | * @opaque: pointer to the CPUState struct | |
195 | */ | |
196 | int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu, | |
197 | int cpuid, void *opaque); | |
198 | ||
199 | /** | |
200 | * cpu_write_elf32_qemunote: | |
201 | * @f: pointer to a function that writes memory to a file | |
202 | * @cpu: The CPU whose memory is to be dumped | |
203 | * @cpuid: ID number of the CPU | |
204 | * @opaque: pointer to the CPUState struct | |
205 | */ | |
206 | int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu, | |
207 | void *opaque); | |
dd83b06a | 208 | |
878096ee AF |
209 | /** |
210 | * CPUDumpFlags: | |
211 | * @CPU_DUMP_CODE: | |
212 | * @CPU_DUMP_FPU: dump FPU register state, not just integer | |
213 | * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state | |
214 | */ | |
215 | enum CPUDumpFlags { | |
216 | CPU_DUMP_CODE = 0x00010000, | |
217 | CPU_DUMP_FPU = 0x00020000, | |
218 | CPU_DUMP_CCOP = 0x00040000, | |
219 | }; | |
220 | ||
221 | /** | |
222 | * cpu_dump_state: | |
223 | * @cpu: The CPU whose state is to be dumped. | |
224 | * @f: File to dump to. | |
225 | * @cpu_fprintf: Function to dump with. | |
226 | * @flags: Flags what to dump. | |
227 | * | |
228 | * Dumps CPU state. | |
229 | */ | |
230 | void cpu_dump_state(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf, | |
231 | int flags); | |
232 | ||
233 | /** | |
234 | * cpu_dump_statistics: | |
235 | * @cpu: The CPU whose state is to be dumped. | |
236 | * @f: File to dump to. | |
237 | * @cpu_fprintf: Function to dump with. | |
238 | * @flags: Flags what to dump. | |
239 | * | |
240 | * Dumps CPU statistics. | |
241 | */ | |
242 | void cpu_dump_statistics(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf, | |
243 | int flags); | |
244 | ||
dd83b06a AF |
245 | /** |
246 | * cpu_reset: | |
247 | * @cpu: The CPU whose state is to be reset. | |
248 | */ | |
249 | void cpu_reset(CPUState *cpu); | |
250 | ||
2b8c2754 AF |
251 | /** |
252 | * cpu_class_by_name: | |
253 | * @typename: The CPU base type. | |
254 | * @cpu_model: The model string without any parameters. | |
255 | * | |
256 | * Looks up a CPU #ObjectClass matching name @cpu_model. | |
257 | * | |
258 | * Returns: A #CPUClass or %NULL if not matching class is found. | |
259 | */ | |
260 | ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model); | |
261 | ||
ca91b15f AF |
262 | /** |
263 | * cpu_class_set_vmsd: | |
264 | * @cc: CPU class | |
265 | * @value: Value to set. Unused for %CONFIG_USER_ONLY. | |
266 | * | |
267 | * Sets #VMStateDescription for @cc. | |
268 | * | |
269 | * The @value argument is intentionally discarded for the non-softmmu targets | |
270 | * to avoid linker errors or excessive preprocessor usage. If this behavior | |
76d5f029 | 271 | * is undesired, you should assign #CPUClass.vmsd directly instead. |
ca91b15f AF |
272 | */ |
273 | #ifndef CONFIG_USER_ONLY | |
274 | static inline void cpu_class_set_vmsd(CPUClass *cc, | |
275 | const struct VMStateDescription *value) | |
276 | { | |
277 | cc->vmsd = value; | |
278 | } | |
279 | #else | |
280 | #define cpu_class_set_vmsd(cc, value) ((cc)->vmsd = NULL) | |
281 | #endif | |
282 | ||
19e3835c AF |
283 | /** |
284 | * device_class_set_vmsd: | |
285 | * @dc: Device class | |
286 | * @value: Value to set. Unused for %CONFIG_USER_ONLY. | |
287 | * | |
288 | * Sets #VMStateDescription for @dc. | |
289 | * | |
290 | * The @value argument is intentionally discarded for the non-softmmu targets | |
291 | * to avoid linker errors or excessive preprocessor usage. If this behavior | |
292 | * is undesired, you should assign #DeviceClass.vmsd directly instead. | |
293 | */ | |
294 | #ifndef CONFIG_USER_ONLY | |
295 | static inline void device_class_set_vmsd(DeviceClass *dc, | |
296 | const struct VMStateDescription *value) | |
297 | { | |
298 | dc->vmsd = value; | |
299 | } | |
300 | #else | |
301 | #define device_class_set_vmsd(dc, value) ((dc)->vmsd = NULL) | |
302 | #endif | |
303 | ||
3993c6bd AF |
304 | /** |
305 | * qemu_cpu_has_work: | |
306 | * @cpu: The vCPU to check. | |
307 | * | |
308 | * Checks whether the CPU has work to do. | |
309 | * | |
310 | * Returns: %true if the CPU has work, %false otherwise. | |
311 | */ | |
312 | bool qemu_cpu_has_work(CPUState *cpu); | |
313 | ||
60e82579 AF |
314 | /** |
315 | * qemu_cpu_is_self: | |
316 | * @cpu: The vCPU to check against. | |
317 | * | |
318 | * Checks whether the caller is executing on the vCPU thread. | |
319 | * | |
320 | * Returns: %true if called from @cpu's thread, %false otherwise. | |
321 | */ | |
322 | bool qemu_cpu_is_self(CPUState *cpu); | |
323 | ||
c08d7424 AF |
324 | /** |
325 | * qemu_cpu_kick: | |
326 | * @cpu: The vCPU to kick. | |
327 | * | |
328 | * Kicks @cpu's thread. | |
329 | */ | |
330 | void qemu_cpu_kick(CPUState *cpu); | |
331 | ||
2fa45344 AF |
332 | /** |
333 | * cpu_is_stopped: | |
334 | * @cpu: The CPU to check. | |
335 | * | |
336 | * Checks whether the CPU is stopped. | |
337 | * | |
338 | * Returns: %true if run state is not running or if artificially stopped; | |
339 | * %false otherwise. | |
340 | */ | |
341 | bool cpu_is_stopped(CPUState *cpu); | |
342 | ||
f100f0b3 AF |
343 | /** |
344 | * run_on_cpu: | |
345 | * @cpu: The vCPU to run on. | |
346 | * @func: The function to be executed. | |
347 | * @data: Data to pass to the function. | |
348 | * | |
349 | * Schedules the function @func for execution on the vCPU @cpu. | |
350 | */ | |
351 | void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data); | |
352 | ||
d6b9e0d6 MT |
353 | /** |
354 | * qemu_for_each_cpu: | |
355 | * @func: The function to be executed. | |
356 | * @data: Data to pass to the function. | |
357 | * | |
358 | * Executes @func for each CPU. | |
359 | */ | |
360 | void qemu_for_each_cpu(void (*func)(CPUState *cpu, void *data), void *data); | |
361 | ||
38d8f5c8 AF |
362 | /** |
363 | * qemu_get_cpu: | |
364 | * @index: The CPUState@cpu_index value of the CPU to obtain. | |
365 | * | |
366 | * Gets a CPU matching @index. | |
367 | * | |
368 | * Returns: The CPU or %NULL if there is no matching CPU. | |
369 | */ | |
370 | CPUState *qemu_get_cpu(int index); | |
371 | ||
69e5ff06 IM |
372 | /** |
373 | * cpu_exists: | |
374 | * @id: Guest-exposed CPU ID to lookup. | |
375 | * | |
376 | * Search for CPU with specified ID. | |
377 | * | |
378 | * Returns: %true - CPU is found, %false - CPU isn't found. | |
379 | */ | |
380 | bool cpu_exists(int64_t id); | |
381 | ||
c3affe56 AF |
382 | #ifndef CONFIG_USER_ONLY |
383 | ||
384 | typedef void (*CPUInterruptHandler)(CPUState *, int); | |
385 | ||
386 | extern CPUInterruptHandler cpu_interrupt_handler; | |
387 | ||
388 | /** | |
389 | * cpu_interrupt: | |
390 | * @cpu: The CPU to set an interrupt on. | |
391 | * @mask: The interupts to set. | |
392 | * | |
393 | * Invokes the interrupt handler. | |
394 | */ | |
395 | static inline void cpu_interrupt(CPUState *cpu, int mask) | |
396 | { | |
397 | cpu_interrupt_handler(cpu, mask); | |
398 | } | |
399 | ||
400 | #else /* USER_ONLY */ | |
401 | ||
402 | void cpu_interrupt(CPUState *cpu, int mask); | |
403 | ||
404 | #endif /* USER_ONLY */ | |
405 | ||
d8ed887b AF |
406 | /** |
407 | * cpu_reset_interrupt: | |
408 | * @cpu: The CPU to clear the interrupt on. | |
409 | * @mask: The interrupt mask to clear. | |
410 | * | |
411 | * Resets interrupts on the vCPU @cpu. | |
412 | */ | |
413 | void cpu_reset_interrupt(CPUState *cpu, int mask); | |
414 | ||
60a3e17a AF |
415 | /** |
416 | * cpu_exit: | |
417 | * @cpu: The CPU to exit. | |
418 | * | |
419 | * Requests the CPU @cpu to exit execution. | |
420 | */ | |
421 | void cpu_exit(CPUState *cpu); | |
422 | ||
2993683b IM |
423 | /** |
424 | * cpu_resume: | |
425 | * @cpu: The CPU to resume. | |
426 | * | |
427 | * Resumes CPU, i.e. puts CPU into runnable state. | |
428 | */ | |
429 | void cpu_resume(CPUState *cpu); | |
dd83b06a | 430 | |
c643bed9 AF |
431 | /** |
432 | * qemu_init_vcpu: | |
433 | * @cpu: The vCPU to initialize. | |
434 | * | |
435 | * Initializes a vCPU. | |
436 | */ | |
437 | void qemu_init_vcpu(CPUState *cpu); | |
438 | ||
1a1562f5 AF |
439 | #ifdef CONFIG_SOFTMMU |
440 | extern const struct VMStateDescription vmstate_cpu_common; | |
441 | #else | |
442 | #define vmstate_cpu_common vmstate_dummy | |
443 | #endif | |
444 | ||
445 | #define VMSTATE_CPU() { \ | |
446 | .name = "parent_obj", \ | |
447 | .size = sizeof(CPUState), \ | |
448 | .vmsd = &vmstate_cpu_common, \ | |
449 | .flags = VMS_STRUCT, \ | |
450 | .offset = 0, \ | |
451 | } | |
452 | ||
dd83b06a | 453 | #endif |