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