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