]> git.proxmox.com Git - qemu.git/blob - include/qom/cpu.h
Merge remote-tracking branch 'mst/tags/for_anthony' 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/tls.h"
28 #include "qemu/typedefs.h"
29
30 typedef int (*WriteCoreDumpFunction)(void *buf, size_t size, void *opaque);
31
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
45 typedef struct CPUState CPUState;
46
47 typedef void (*CPUUnassignedAccess)(CPUState *cpu, hwaddr addr,
48 bool is_write, bool is_exec, int opaque,
49 unsigned size);
50
51 /**
52 * CPUClass:
53 * @class_by_name: Callback to map -cpu command line model name to an
54 * instantiatable CPU type.
55 * @reset: Callback to reset the #CPUState to its initial state.
56 * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
57 * @do_interrupt: Callback for interrupt handling.
58 * @do_unassigned_access: Callback for unassigned access handling.
59 * @dump_state: Callback for dumping state.
60 * @dump_statistics: Callback for dumping statistics.
61 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
62 * @get_paging_enabled: Callback for inquiring whether paging is enabled.
63 * @get_memory_mapping: Callback for obtaining the memory mappings.
64 * @vmsd: State description for migration.
65 *
66 * Represents a CPU family or model.
67 */
68 typedef struct CPUClass {
69 /*< private >*/
70 DeviceClass parent_class;
71 /*< public >*/
72
73 ObjectClass *(*class_by_name)(const char *cpu_model);
74
75 void (*reset)(CPUState *cpu);
76 int reset_dump_flags;
77 void (*do_interrupt)(CPUState *cpu);
78 CPUUnassignedAccess do_unassigned_access;
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);
83 int64_t (*get_arch_id)(CPUState *cpu);
84 bool (*get_paging_enabled)(const CPUState *cpu);
85 void (*get_memory_mapping)(CPUState *cpu, MemoryMappingList *list,
86 Error **errp);
87
88 const struct VMStateDescription *vmsd;
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);
97 } CPUClass;
98
99 struct KVMState;
100 struct kvm_run;
101
102 /**
103 * CPUState:
104 * @cpu_index: CPU index (informative).
105 * @nr_cores: Number of cores within this CPU package.
106 * @nr_threads: Number of threads within this CPU.
107 * @numa_node: NUMA node this CPU is belonging to.
108 * @host_tid: Host thread ID.
109 * @running: #true if CPU is currently running (usermode).
110 * @created: Indicates whether the CPU thread has been successfully created.
111 * @interrupt_request: Indicates a pending interrupt request.
112 * @halted: Nonzero if the CPU is in suspended state.
113 * @stop: Indicates a pending stop request.
114 * @stopped: Indicates the CPU has been artificially stopped.
115 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
116 * CPU and return to its top level loop.
117 * @env_ptr: Pointer to subclass-specific CPUArchState field.
118 * @current_tb: Currently executing TB.
119 * @next_cpu: Next CPU sharing TB cache.
120 * @kvm_fd: vCPU file descriptor for KVM.
121 *
122 * State of one CPU core or thread.
123 */
124 struct CPUState {
125 /*< private >*/
126 DeviceState parent_obj;
127 /*< public >*/
128
129 int nr_cores;
130 int nr_threads;
131 int numa_node;
132
133 struct QemuThread *thread;
134 #ifdef _WIN32
135 HANDLE hThread;
136 #endif
137 int thread_id;
138 uint32_t host_tid;
139 bool running;
140 struct QemuCond *halt_cond;
141 struct qemu_work_item *queued_work_first, *queued_work_last;
142 bool thread_kicked;
143 bool created;
144 bool stop;
145 bool stopped;
146 volatile sig_atomic_t exit_request;
147 volatile sig_atomic_t tcg_exit_req;
148 uint32_t interrupt_request;
149
150 void *env_ptr; /* CPUArchState */
151 struct TranslationBlock *current_tb;
152 CPUState *next_cpu;
153
154 int kvm_fd;
155 bool kvm_vcpu_dirty;
156 struct KVMState *kvm_state;
157 struct kvm_run *kvm_run;
158
159 /* TODO Move common fields from CPUArchState here. */
160 int cpu_index; /* used by alpha TCG */
161 uint32_t halted; /* used by alpha, cris, ppc TCG */
162 };
163
164 extern CPUState *first_cpu;
165
166 DECLARE_TLS(CPUState *, current_cpu);
167 #define current_cpu tls_var(current_cpu)
168
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 */
175 bool cpu_paging_enabled(const CPUState *cpu);
176
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 */
183 void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
184 Error **errp);
185
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 */
193 int 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 */
203 int 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 */
213 int 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 */
223 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
224 void *opaque);
225
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 */
232 enum 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 */
247 void 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 */
259 void cpu_dump_statistics(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
260 int flags);
261
262 /**
263 * cpu_reset:
264 * @cpu: The CPU whose state is to be reset.
265 */
266 void cpu_reset(CPUState *cpu);
267
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 */
277 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
278
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
288 * is undesired, you should assign #CPUClass.vmsd directly instead.
289 */
290 #ifndef CONFIG_USER_ONLY
291 static 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
300 #ifndef CONFIG_USER_ONLY
301 static 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
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
323 static 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
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 */
340 bool qemu_cpu_has_work(CPUState *cpu);
341
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 */
350 bool qemu_cpu_is_self(CPUState *cpu);
351
352 /**
353 * qemu_cpu_kick:
354 * @cpu: The vCPU to kick.
355 *
356 * Kicks @cpu's thread.
357 */
358 void qemu_cpu_kick(CPUState *cpu);
359
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 */
369 bool cpu_is_stopped(CPUState *cpu);
370
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 */
379 void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data);
380
381 /**
382 * async_run_on_cpu:
383 * @cpu: The vCPU to run on.
384 * @func: The function to be executed.
385 * @data: Data to pass to the function.
386 *
387 * Schedules the function @func for execution on the vCPU @cpu asynchronously.
388 */
389 void async_run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data);
390
391 /**
392 * qemu_for_each_cpu:
393 * @func: The function to be executed.
394 * @data: Data to pass to the function.
395 *
396 * Executes @func for each CPU.
397 */
398 void qemu_for_each_cpu(void (*func)(CPUState *cpu, void *data), void *data);
399
400 /**
401 * qemu_get_cpu:
402 * @index: The CPUState@cpu_index value of the CPU to obtain.
403 *
404 * Gets a CPU matching @index.
405 *
406 * Returns: The CPU or %NULL if there is no matching CPU.
407 */
408 CPUState *qemu_get_cpu(int index);
409
410 /**
411 * cpu_exists:
412 * @id: Guest-exposed CPU ID to lookup.
413 *
414 * Search for CPU with specified ID.
415 *
416 * Returns: %true - CPU is found, %false - CPU isn't found.
417 */
418 bool cpu_exists(int64_t id);
419
420 #ifndef CONFIG_USER_ONLY
421
422 typedef void (*CPUInterruptHandler)(CPUState *, int);
423
424 extern CPUInterruptHandler cpu_interrupt_handler;
425
426 /**
427 * cpu_interrupt:
428 * @cpu: The CPU to set an interrupt on.
429 * @mask: The interupts to set.
430 *
431 * Invokes the interrupt handler.
432 */
433 static inline void cpu_interrupt(CPUState *cpu, int mask)
434 {
435 cpu_interrupt_handler(cpu, mask);
436 }
437
438 #else /* USER_ONLY */
439
440 void cpu_interrupt(CPUState *cpu, int mask);
441
442 #endif /* USER_ONLY */
443
444 #ifndef CONFIG_USER_ONLY
445
446 static inline void cpu_unassigned_access(CPUState *cpu, hwaddr addr,
447 bool is_write, bool is_exec,
448 int opaque, unsigned size)
449 {
450 CPUClass *cc = CPU_GET_CLASS(cpu);
451
452 if (cc->do_unassigned_access) {
453 cc->do_unassigned_access(cpu, addr, is_write, is_exec, opaque, size);
454 }
455 }
456
457 #endif
458
459 /**
460 * cpu_reset_interrupt:
461 * @cpu: The CPU to clear the interrupt on.
462 * @mask: The interrupt mask to clear.
463 *
464 * Resets interrupts on the vCPU @cpu.
465 */
466 void cpu_reset_interrupt(CPUState *cpu, int mask);
467
468 /**
469 * cpu_exit:
470 * @cpu: The CPU to exit.
471 *
472 * Requests the CPU @cpu to exit execution.
473 */
474 void cpu_exit(CPUState *cpu);
475
476 /**
477 * cpu_resume:
478 * @cpu: The CPU to resume.
479 *
480 * Resumes CPU, i.e. puts CPU into runnable state.
481 */
482 void cpu_resume(CPUState *cpu);
483
484 /**
485 * qemu_init_vcpu:
486 * @cpu: The vCPU to initialize.
487 *
488 * Initializes a vCPU.
489 */
490 void qemu_init_vcpu(CPUState *cpu);
491
492 #ifdef CONFIG_SOFTMMU
493 extern const struct VMStateDescription vmstate_cpu_common;
494 #else
495 #define vmstate_cpu_common vmstate_dummy
496 #endif
497
498 #define VMSTATE_CPU() { \
499 .name = "parent_obj", \
500 .size = sizeof(CPUState), \
501 .vmsd = &vmstate_cpu_common, \
502 .flags = VMS_STRUCT, \
503 .offset = 0, \
504 }
505
506 #endif