]> git.proxmox.com Git - qemu.git/blame - include/qom/cpu.h
Merge git://github.com/hw-claudio/qemu-aarch64-queue into tcg-next
[qemu.git] / include / qom / cpu.h
CommitLineData
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
30typedef 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
45typedef struct CPUState CPUState;
46
c658b94f
AF
47typedef 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 */
68typedef 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 99struct KVMState;
f7575c96 100struct 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 */
124struct 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
164extern CPUState *first_cpu;
165
4917cf44
AF
166DECLARE_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 */
175bool 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 */
183void 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 */
193int 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 */
203int 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 */
213int 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 */
223int 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 */
232enum 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 */
247void 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 */
259void 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 */
266void 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 */
277ObjectClass *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
291static 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
301static 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
323static 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 */
340bool 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 */
350bool 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 */
358void 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 */
369bool 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 */
379void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data);
380
d6b9e0d6
MT
381/**
382 * qemu_for_each_cpu:
383 * @func: The function to be executed.
384 * @data: Data to pass to the function.
385 *
386 * Executes @func for each CPU.
387 */
388void qemu_for_each_cpu(void (*func)(CPUState *cpu, void *data), void *data);
389
38d8f5c8
AF
390/**
391 * qemu_get_cpu:
392 * @index: The CPUState@cpu_index value of the CPU to obtain.
393 *
394 * Gets a CPU matching @index.
395 *
396 * Returns: The CPU or %NULL if there is no matching CPU.
397 */
398CPUState *qemu_get_cpu(int index);
399
69e5ff06
IM
400/**
401 * cpu_exists:
402 * @id: Guest-exposed CPU ID to lookup.
403 *
404 * Search for CPU with specified ID.
405 *
406 * Returns: %true - CPU is found, %false - CPU isn't found.
407 */
408bool cpu_exists(int64_t id);
409
c3affe56
AF
410#ifndef CONFIG_USER_ONLY
411
412typedef void (*CPUInterruptHandler)(CPUState *, int);
413
414extern CPUInterruptHandler cpu_interrupt_handler;
415
416/**
417 * cpu_interrupt:
418 * @cpu: The CPU to set an interrupt on.
419 * @mask: The interupts to set.
420 *
421 * Invokes the interrupt handler.
422 */
423static inline void cpu_interrupt(CPUState *cpu, int mask)
424{
425 cpu_interrupt_handler(cpu, mask);
426}
427
428#else /* USER_ONLY */
429
430void cpu_interrupt(CPUState *cpu, int mask);
431
432#endif /* USER_ONLY */
433
c658b94f
AF
434#ifndef CONFIG_USER_ONLY
435
436static inline void cpu_unassigned_access(CPUState *cpu, hwaddr addr,
437 bool is_write, bool is_exec,
438 int opaque, unsigned size)
439{
440 CPUClass *cc = CPU_GET_CLASS(cpu);
441
442 if (cc->do_unassigned_access) {
443 cc->do_unassigned_access(cpu, addr, is_write, is_exec, opaque, size);
444 }
445}
446
447#endif
448
d8ed887b
AF
449/**
450 * cpu_reset_interrupt:
451 * @cpu: The CPU to clear the interrupt on.
452 * @mask: The interrupt mask to clear.
453 *
454 * Resets interrupts on the vCPU @cpu.
455 */
456void cpu_reset_interrupt(CPUState *cpu, int mask);
457
60a3e17a
AF
458/**
459 * cpu_exit:
460 * @cpu: The CPU to exit.
461 *
462 * Requests the CPU @cpu to exit execution.
463 */
464void cpu_exit(CPUState *cpu);
465
2993683b
IM
466/**
467 * cpu_resume:
468 * @cpu: The CPU to resume.
469 *
470 * Resumes CPU, i.e. puts CPU into runnable state.
471 */
472void cpu_resume(CPUState *cpu);
dd83b06a 473
c643bed9
AF
474/**
475 * qemu_init_vcpu:
476 * @cpu: The vCPU to initialize.
477 *
478 * Initializes a vCPU.
479 */
480void qemu_init_vcpu(CPUState *cpu);
481
1a1562f5
AF
482#ifdef CONFIG_SOFTMMU
483extern const struct VMStateDescription vmstate_cpu_common;
484#else
485#define vmstate_cpu_common vmstate_dummy
486#endif
487
488#define VMSTATE_CPU() { \
489 .name = "parent_obj", \
490 .size = sizeof(CPUState), \
491 .vmsd = &vmstate_cpu_common, \
492 .flags = VMS_STRUCT, \
493 .offset = 0, \
494}
495
dd83b06a 496#endif