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