]> git.proxmox.com Git - qemu.git/blame - include/qom/cpu.h
cpu: Introduce device_class_set_vmsd() helper
[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"
1de7afc9 25#include "qemu/thread.h"
a23bbfda 26#include "qemu/typedefs.h"
dd83b06a 27
c72bf468
JF
28typedef int (*WriteCoreDumpFunction)(void *buf, size_t size, void *opaque);
29
dd83b06a
AF
30/**
31 * SECTION:cpu
32 * @section_id: QEMU-cpu
33 * @title: CPU Class
34 * @short_description: Base class for all CPUs
35 */
36
37#define TYPE_CPU "cpu"
38
39#define CPU(obj) OBJECT_CHECK(CPUState, (obj), TYPE_CPU)
40#define CPU_CLASS(class) OBJECT_CLASS_CHECK(CPUClass, (class), TYPE_CPU)
41#define CPU_GET_CLASS(obj) OBJECT_GET_CLASS(CPUClass, (obj), TYPE_CPU)
42
43typedef struct CPUState CPUState;
44
45/**
46 * CPUClass:
2b8c2754
AF
47 * @class_by_name: Callback to map -cpu command line model name to an
48 * instantiatable CPU type.
f5df5baf 49 * @reset: Callback to reset the #CPUState to its initial state.
97a8ea5a 50 * @do_interrupt: Callback for interrupt handling.
997395d3 51 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
444d5590 52 * @get_paging_enabled: Callback for inquiring whether paging is enabled.
a23bbfda 53 * @get_memory_mapping: Callback for obtaining the memory mappings.
b170fce3 54 * @vmsd: State description for migration.
dd83b06a
AF
55 *
56 * Represents a CPU family or model.
57 */
58typedef struct CPUClass {
59 /*< private >*/
961f8395 60 DeviceClass parent_class;
dd83b06a
AF
61 /*< public >*/
62
2b8c2754
AF
63 ObjectClass *(*class_by_name)(const char *cpu_model);
64
dd83b06a 65 void (*reset)(CPUState *cpu);
97a8ea5a 66 void (*do_interrupt)(CPUState *cpu);
997395d3 67 int64_t (*get_arch_id)(CPUState *cpu);
444d5590 68 bool (*get_paging_enabled)(const CPUState *cpu);
a23bbfda
AF
69 void (*get_memory_mapping)(CPUState *cpu, MemoryMappingList *list,
70 Error **errp);
b170fce3
AF
71
72 const struct VMStateDescription *vmsd;
c72bf468
JF
73 int (*write_elf64_note)(WriteCoreDumpFunction f, CPUState *cpu,
74 int cpuid, void *opaque);
75 int (*write_elf64_qemunote)(WriteCoreDumpFunction f, CPUState *cpu,
76 void *opaque);
77 int (*write_elf32_note)(WriteCoreDumpFunction f, CPUState *cpu,
78 int cpuid, void *opaque);
79 int (*write_elf32_qemunote)(WriteCoreDumpFunction f, CPUState *cpu,
80 void *opaque);
dd83b06a
AF
81} CPUClass;
82
a60f24b5 83struct KVMState;
f7575c96 84struct kvm_run;
a60f24b5 85
dd83b06a
AF
86/**
87 * CPUState:
55e5c285 88 * @cpu_index: CPU index (informative).
ce3960eb
AF
89 * @nr_cores: Number of cores within this CPU package.
90 * @nr_threads: Number of threads within this CPU.
1b1ed8dc 91 * @numa_node: NUMA node this CPU is belonging to.
0d34282f 92 * @host_tid: Host thread ID.
0315c31c 93 * @running: #true if CPU is currently running (usermode).
61a46217 94 * @created: Indicates whether the CPU thread has been successfully created.
259186a7
AF
95 * @interrupt_request: Indicates a pending interrupt request.
96 * @halted: Nonzero if the CPU is in suspended state.
4fdeee7c 97 * @stop: Indicates a pending stop request.
f324e766 98 * @stopped: Indicates the CPU has been artificially stopped.
378df4b2
PM
99 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
100 * CPU and return to its top level loop.
c05efcb1 101 * @env_ptr: Pointer to subclass-specific CPUArchState field.
d77953b9 102 * @current_tb: Currently executing TB.
8737c51c 103 * @kvm_fd: vCPU file descriptor for KVM.
dd83b06a
AF
104 *
105 * State of one CPU core or thread.
106 */
107struct CPUState {
108 /*< private >*/
961f8395 109 DeviceState parent_obj;
dd83b06a
AF
110 /*< public >*/
111
ce3960eb
AF
112 int nr_cores;
113 int nr_threads;
1b1ed8dc 114 int numa_node;
ce3960eb 115
814e612e 116 struct QemuThread *thread;
bcba2a72
AF
117#ifdef _WIN32
118 HANDLE hThread;
119#endif
9f09e18a 120 int thread_id;
0d34282f 121 uint32_t host_tid;
0315c31c 122 bool running;
f5c121b8 123 struct QemuCond *halt_cond;
c64ca814 124 struct qemu_work_item *queued_work_first, *queued_work_last;
216fc9a4 125 bool thread_kicked;
61a46217 126 bool created;
4fdeee7c 127 bool stop;
f324e766 128 bool stopped;
fcd7d003 129 volatile sig_atomic_t exit_request;
378df4b2 130 volatile sig_atomic_t tcg_exit_req;
259186a7 131 uint32_t interrupt_request;
bcba2a72 132
c05efcb1 133 void *env_ptr; /* CPUArchState */
d77953b9
AF
134 struct TranslationBlock *current_tb;
135
8737c51c 136 int kvm_fd;
20d695a9 137 bool kvm_vcpu_dirty;
a60f24b5 138 struct KVMState *kvm_state;
f7575c96 139 struct kvm_run *kvm_run;
8737c51c 140
f5df5baf 141 /* TODO Move common fields from CPUArchState here. */
55e5c285 142 int cpu_index; /* used by alpha TCG */
259186a7 143 uint32_t halted; /* used by alpha, cris, ppc TCG */
dd83b06a
AF
144};
145
444d5590
AF
146/**
147 * cpu_paging_enabled:
148 * @cpu: The CPU whose state is to be inspected.
149 *
150 * Returns: %true if paging is enabled, %false otherwise.
151 */
152bool cpu_paging_enabled(const CPUState *cpu);
153
a23bbfda
AF
154/**
155 * cpu_get_memory_mapping:
156 * @cpu: The CPU whose memory mappings are to be obtained.
157 * @list: Where to write the memory mappings to.
158 * @errp: Pointer for reporting an #Error.
159 */
160void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
161 Error **errp);
162
c72bf468
JF
163/**
164 * cpu_write_elf64_note:
165 * @f: pointer to a function that writes memory to a file
166 * @cpu: The CPU whose memory is to be dumped
167 * @cpuid: ID number of the CPU
168 * @opaque: pointer to the CPUState struct
169 */
170int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
171 int cpuid, void *opaque);
172
173/**
174 * cpu_write_elf64_qemunote:
175 * @f: pointer to a function that writes memory to a file
176 * @cpu: The CPU whose memory is to be dumped
177 * @cpuid: ID number of the CPU
178 * @opaque: pointer to the CPUState struct
179 */
180int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
181 void *opaque);
182
183/**
184 * cpu_write_elf32_note:
185 * @f: pointer to a function that writes memory to a file
186 * @cpu: The CPU whose memory is to be dumped
187 * @cpuid: ID number of the CPU
188 * @opaque: pointer to the CPUState struct
189 */
190int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
191 int cpuid, void *opaque);
192
193/**
194 * cpu_write_elf32_qemunote:
195 * @f: pointer to a function that writes memory to a file
196 * @cpu: The CPU whose memory is to be dumped
197 * @cpuid: ID number of the CPU
198 * @opaque: pointer to the CPUState struct
199 */
200int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
201 void *opaque);
dd83b06a
AF
202
203/**
204 * cpu_reset:
205 * @cpu: The CPU whose state is to be reset.
206 */
207void cpu_reset(CPUState *cpu);
208
2b8c2754
AF
209/**
210 * cpu_class_by_name:
211 * @typename: The CPU base type.
212 * @cpu_model: The model string without any parameters.
213 *
214 * Looks up a CPU #ObjectClass matching name @cpu_model.
215 *
216 * Returns: A #CPUClass or %NULL if not matching class is found.
217 */
218ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
219
ca91b15f
AF
220/**
221 * cpu_class_set_vmsd:
222 * @cc: CPU class
223 * @value: Value to set. Unused for %CONFIG_USER_ONLY.
224 *
225 * Sets #VMStateDescription for @cc.
226 *
227 * The @value argument is intentionally discarded for the non-softmmu targets
228 * to avoid linker errors or excessive preprocessor usage. If this behavior
76d5f029 229 * is undesired, you should assign #CPUClass.vmsd directly instead.
ca91b15f
AF
230 */
231#ifndef CONFIG_USER_ONLY
232static inline void cpu_class_set_vmsd(CPUClass *cc,
233 const struct VMStateDescription *value)
234{
235 cc->vmsd = value;
236}
237#else
238#define cpu_class_set_vmsd(cc, value) ((cc)->vmsd = NULL)
239#endif
240
19e3835c
AF
241/**
242 * device_class_set_vmsd:
243 * @dc: Device class
244 * @value: Value to set. Unused for %CONFIG_USER_ONLY.
245 *
246 * Sets #VMStateDescription for @dc.
247 *
248 * The @value argument is intentionally discarded for the non-softmmu targets
249 * to avoid linker errors or excessive preprocessor usage. If this behavior
250 * is undesired, you should assign #DeviceClass.vmsd directly instead.
251 */
252#ifndef CONFIG_USER_ONLY
253static inline void device_class_set_vmsd(DeviceClass *dc,
254 const struct VMStateDescription *value)
255{
256 dc->vmsd = value;
257}
258#else
259#define device_class_set_vmsd(dc, value) ((dc)->vmsd = NULL)
260#endif
261
3993c6bd
AF
262/**
263 * qemu_cpu_has_work:
264 * @cpu: The vCPU to check.
265 *
266 * Checks whether the CPU has work to do.
267 *
268 * Returns: %true if the CPU has work, %false otherwise.
269 */
270bool qemu_cpu_has_work(CPUState *cpu);
271
60e82579
AF
272/**
273 * qemu_cpu_is_self:
274 * @cpu: The vCPU to check against.
275 *
276 * Checks whether the caller is executing on the vCPU thread.
277 *
278 * Returns: %true if called from @cpu's thread, %false otherwise.
279 */
280bool qemu_cpu_is_self(CPUState *cpu);
281
c08d7424
AF
282/**
283 * qemu_cpu_kick:
284 * @cpu: The vCPU to kick.
285 *
286 * Kicks @cpu's thread.
287 */
288void qemu_cpu_kick(CPUState *cpu);
289
2fa45344
AF
290/**
291 * cpu_is_stopped:
292 * @cpu: The CPU to check.
293 *
294 * Checks whether the CPU is stopped.
295 *
296 * Returns: %true if run state is not running or if artificially stopped;
297 * %false otherwise.
298 */
299bool cpu_is_stopped(CPUState *cpu);
300
f100f0b3
AF
301/**
302 * run_on_cpu:
303 * @cpu: The vCPU to run on.
304 * @func: The function to be executed.
305 * @data: Data to pass to the function.
306 *
307 * Schedules the function @func for execution on the vCPU @cpu.
308 */
309void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data);
310
d6b9e0d6
MT
311/**
312 * qemu_for_each_cpu:
313 * @func: The function to be executed.
314 * @data: Data to pass to the function.
315 *
316 * Executes @func for each CPU.
317 */
318void qemu_for_each_cpu(void (*func)(CPUState *cpu, void *data), void *data);
319
38d8f5c8
AF
320/**
321 * qemu_get_cpu:
322 * @index: The CPUState@cpu_index value of the CPU to obtain.
323 *
324 * Gets a CPU matching @index.
325 *
326 * Returns: The CPU or %NULL if there is no matching CPU.
327 */
328CPUState *qemu_get_cpu(int index);
329
69e5ff06
IM
330/**
331 * cpu_exists:
332 * @id: Guest-exposed CPU ID to lookup.
333 *
334 * Search for CPU with specified ID.
335 *
336 * Returns: %true - CPU is found, %false - CPU isn't found.
337 */
338bool cpu_exists(int64_t id);
339
c3affe56
AF
340#ifndef CONFIG_USER_ONLY
341
342typedef void (*CPUInterruptHandler)(CPUState *, int);
343
344extern CPUInterruptHandler cpu_interrupt_handler;
345
346/**
347 * cpu_interrupt:
348 * @cpu: The CPU to set an interrupt on.
349 * @mask: The interupts to set.
350 *
351 * Invokes the interrupt handler.
352 */
353static inline void cpu_interrupt(CPUState *cpu, int mask)
354{
355 cpu_interrupt_handler(cpu, mask);
356}
357
358#else /* USER_ONLY */
359
360void cpu_interrupt(CPUState *cpu, int mask);
361
362#endif /* USER_ONLY */
363
d8ed887b
AF
364/**
365 * cpu_reset_interrupt:
366 * @cpu: The CPU to clear the interrupt on.
367 * @mask: The interrupt mask to clear.
368 *
369 * Resets interrupts on the vCPU @cpu.
370 */
371void cpu_reset_interrupt(CPUState *cpu, int mask);
372
2993683b
IM
373/**
374 * cpu_resume:
375 * @cpu: The CPU to resume.
376 *
377 * Resumes CPU, i.e. puts CPU into runnable state.
378 */
379void cpu_resume(CPUState *cpu);
dd83b06a
AF
380
381#endif