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