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