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