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