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