]> git.proxmox.com Git - mirror_qemu.git/blob - include/qom/cpu.h
cpu: Introduce CPUClass::parse_features() hook
[mirror_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/queue.h"
27 #include "qemu/thread.h"
28 #include "qemu/tls.h"
29 #include "qemu/typedefs.h"
30
31 typedef int (*WriteCoreDumpFunction)(const void *buf, size_t size,
32 void *opaque);
33
34 /**
35 * vaddr:
36 * Type wide enough to contain any #target_ulong virtual address.
37 */
38 typedef uint64_t vaddr;
39 #define VADDR_PRId PRId64
40 #define VADDR_PRIu PRIu64
41 #define VADDR_PRIo PRIo64
42 #define VADDR_PRIx PRIx64
43 #define VADDR_PRIX PRIX64
44 #define VADDR_MAX UINT64_MAX
45
46 /**
47 * SECTION:cpu
48 * @section_id: QEMU-cpu
49 * @title: CPU Class
50 * @short_description: Base class for all CPUs
51 */
52
53 #define TYPE_CPU "cpu"
54
55 #define CPU(obj) OBJECT_CHECK(CPUState, (obj), TYPE_CPU)
56 #define CPU_CLASS(class) OBJECT_CLASS_CHECK(CPUClass, (class), TYPE_CPU)
57 #define CPU_GET_CLASS(obj) OBJECT_GET_CLASS(CPUClass, (obj), TYPE_CPU)
58
59 typedef struct CPUState CPUState;
60
61 typedef void (*CPUUnassignedAccess)(CPUState *cpu, hwaddr addr,
62 bool is_write, bool is_exec, int opaque,
63 unsigned size);
64
65 struct TranslationBlock;
66
67 /**
68 * CPUClass:
69 * @class_by_name: Callback to map -cpu command line model name to an
70 * instantiatable CPU type.
71 * @parse_features: Callback to parse command line arguments.
72 * @reset: Callback to reset the #CPUState to its initial state.
73 * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
74 * @has_work: Callback for checking if there is work to do.
75 * @do_interrupt: Callback for interrupt handling.
76 * @do_unassigned_access: Callback for unassigned access handling.
77 * @memory_rw_debug: Callback for GDB memory access.
78 * @dump_state: Callback for dumping state.
79 * @dump_statistics: Callback for dumping statistics.
80 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
81 * @get_paging_enabled: Callback for inquiring whether paging is enabled.
82 * @get_memory_mapping: Callback for obtaining the memory mappings.
83 * @set_pc: Callback for setting the Program Counter register.
84 * @synchronize_from_tb: Callback for synchronizing state from a TCG
85 * #TranslationBlock.
86 * @get_phys_page_debug: Callback for obtaining a physical address.
87 * @gdb_read_register: Callback for letting GDB read a register.
88 * @gdb_write_register: Callback for letting GDB write a register.
89 * @vmsd: State description for migration.
90 * @gdb_num_core_regs: Number of core registers accessible to GDB.
91 * @gdb_core_xml_file: File name for core registers GDB XML description.
92 *
93 * Represents a CPU family or model.
94 */
95 typedef struct CPUClass {
96 /*< private >*/
97 DeviceClass parent_class;
98 /*< public >*/
99
100 ObjectClass *(*class_by_name)(const char *cpu_model);
101 void (*parse_features)(CPUState *cpu, char *str, Error **errp);
102
103 void (*reset)(CPUState *cpu);
104 int reset_dump_flags;
105 bool (*has_work)(CPUState *cpu);
106 void (*do_interrupt)(CPUState *cpu);
107 CPUUnassignedAccess do_unassigned_access;
108 int (*memory_rw_debug)(CPUState *cpu, vaddr addr,
109 uint8_t *buf, int len, bool is_write);
110 void (*dump_state)(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
111 int flags);
112 void (*dump_statistics)(CPUState *cpu, FILE *f,
113 fprintf_function cpu_fprintf, int flags);
114 int64_t (*get_arch_id)(CPUState *cpu);
115 bool (*get_paging_enabled)(const CPUState *cpu);
116 void (*get_memory_mapping)(CPUState *cpu, MemoryMappingList *list,
117 Error **errp);
118 void (*set_pc)(CPUState *cpu, vaddr value);
119 void (*synchronize_from_tb)(CPUState *cpu, struct TranslationBlock *tb);
120 hwaddr (*get_phys_page_debug)(CPUState *cpu, vaddr addr);
121 int (*gdb_read_register)(CPUState *cpu, uint8_t *buf, int reg);
122 int (*gdb_write_register)(CPUState *cpu, uint8_t *buf, int reg);
123
124 int (*write_elf64_note)(WriteCoreDumpFunction f, CPUState *cpu,
125 int cpuid, void *opaque);
126 int (*write_elf64_qemunote)(WriteCoreDumpFunction f, CPUState *cpu,
127 void *opaque);
128 int (*write_elf32_note)(WriteCoreDumpFunction f, CPUState *cpu,
129 int cpuid, void *opaque);
130 int (*write_elf32_qemunote)(WriteCoreDumpFunction f, CPUState *cpu,
131 void *opaque);
132
133 const struct VMStateDescription *vmsd;
134 int gdb_num_core_regs;
135 const char *gdb_core_xml_file;
136 } CPUClass;
137
138 struct KVMState;
139 struct kvm_run;
140
141 /**
142 * CPUState:
143 * @cpu_index: CPU index (informative).
144 * @nr_cores: Number of cores within this CPU package.
145 * @nr_threads: Number of threads within this CPU.
146 * @numa_node: NUMA node this CPU is belonging to.
147 * @host_tid: Host thread ID.
148 * @running: #true if CPU is currently running (usermode).
149 * @created: Indicates whether the CPU thread has been successfully created.
150 * @interrupt_request: Indicates a pending interrupt request.
151 * @halted: Nonzero if the CPU is in suspended state.
152 * @stop: Indicates a pending stop request.
153 * @stopped: Indicates the CPU has been artificially stopped.
154 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
155 * CPU and return to its top level loop.
156 * @singlestep_enabled: Flags for single-stepping.
157 * @env_ptr: Pointer to subclass-specific CPUArchState field.
158 * @current_tb: Currently executing TB.
159 * @gdb_regs: Additional GDB registers.
160 * @gdb_num_regs: Number of total registers accessible to GDB.
161 * @gdb_num_g_regs: Number of registers in GDB 'g' packets.
162 * @next_cpu: Next CPU sharing TB cache.
163 * @kvm_fd: vCPU file descriptor for KVM.
164 *
165 * State of one CPU core or thread.
166 */
167 struct CPUState {
168 /*< private >*/
169 DeviceState parent_obj;
170 /*< public >*/
171
172 int nr_cores;
173 int nr_threads;
174 int numa_node;
175
176 struct QemuThread *thread;
177 #ifdef _WIN32
178 HANDLE hThread;
179 #endif
180 int thread_id;
181 uint32_t host_tid;
182 bool running;
183 struct QemuCond *halt_cond;
184 struct qemu_work_item *queued_work_first, *queued_work_last;
185 bool thread_kicked;
186 bool created;
187 bool stop;
188 bool stopped;
189 volatile sig_atomic_t exit_request;
190 volatile sig_atomic_t tcg_exit_req;
191 uint32_t interrupt_request;
192 int singlestep_enabled;
193
194 AddressSpace *as;
195 MemoryListener *tcg_as_listener;
196
197 void *env_ptr; /* CPUArchState */
198 struct TranslationBlock *current_tb;
199 struct GDBRegisterState *gdb_regs;
200 int gdb_num_regs;
201 int gdb_num_g_regs;
202 QTAILQ_ENTRY(CPUState) node;
203
204 int kvm_fd;
205 bool kvm_vcpu_dirty;
206 struct KVMState *kvm_state;
207 struct kvm_run *kvm_run;
208
209 /* TODO Move common fields from CPUArchState here. */
210 int cpu_index; /* used by alpha TCG */
211 uint32_t halted; /* used by alpha, cris, ppc TCG */
212 };
213
214 QTAILQ_HEAD(CPUTailQ, CPUState);
215 extern struct CPUTailQ cpus;
216 #define CPU_NEXT(cpu) QTAILQ_NEXT(cpu, node)
217 #define CPU_FOREACH(cpu) QTAILQ_FOREACH(cpu, &cpus, node)
218 #define CPU_FOREACH_SAFE(cpu, next_cpu) \
219 QTAILQ_FOREACH_SAFE(cpu, &cpus, node, next_cpu)
220 #define first_cpu QTAILQ_FIRST(&cpus)
221
222 DECLARE_TLS(CPUState *, current_cpu);
223 #define current_cpu tls_var(current_cpu)
224
225 /**
226 * cpu_paging_enabled:
227 * @cpu: The CPU whose state is to be inspected.
228 *
229 * Returns: %true if paging is enabled, %false otherwise.
230 */
231 bool cpu_paging_enabled(const CPUState *cpu);
232
233 /**
234 * cpu_get_memory_mapping:
235 * @cpu: The CPU whose memory mappings are to be obtained.
236 * @list: Where to write the memory mappings to.
237 * @errp: Pointer for reporting an #Error.
238 */
239 void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
240 Error **errp);
241
242 /**
243 * cpu_write_elf64_note:
244 * @f: pointer to a function that writes memory to a file
245 * @cpu: The CPU whose memory is to be dumped
246 * @cpuid: ID number of the CPU
247 * @opaque: pointer to the CPUState struct
248 */
249 int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
250 int cpuid, void *opaque);
251
252 /**
253 * cpu_write_elf64_qemunote:
254 * @f: pointer to a function that writes memory to a file
255 * @cpu: The CPU whose memory is to be dumped
256 * @cpuid: ID number of the CPU
257 * @opaque: pointer to the CPUState struct
258 */
259 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
260 void *opaque);
261
262 /**
263 * cpu_write_elf32_note:
264 * @f: pointer to a function that writes memory to a file
265 * @cpu: The CPU whose memory is to be dumped
266 * @cpuid: ID number of the CPU
267 * @opaque: pointer to the CPUState struct
268 */
269 int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
270 int cpuid, void *opaque);
271
272 /**
273 * cpu_write_elf32_qemunote:
274 * @f: pointer to a function that writes memory to a file
275 * @cpu: The CPU whose memory is to be dumped
276 * @cpuid: ID number of the CPU
277 * @opaque: pointer to the CPUState struct
278 */
279 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
280 void *opaque);
281
282 /**
283 * CPUDumpFlags:
284 * @CPU_DUMP_CODE:
285 * @CPU_DUMP_FPU: dump FPU register state, not just integer
286 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
287 */
288 enum CPUDumpFlags {
289 CPU_DUMP_CODE = 0x00010000,
290 CPU_DUMP_FPU = 0x00020000,
291 CPU_DUMP_CCOP = 0x00040000,
292 };
293
294 /**
295 * cpu_dump_state:
296 * @cpu: The CPU whose state is to be dumped.
297 * @f: File to dump to.
298 * @cpu_fprintf: Function to dump with.
299 * @flags: Flags what to dump.
300 *
301 * Dumps CPU state.
302 */
303 void cpu_dump_state(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
304 int flags);
305
306 /**
307 * cpu_dump_statistics:
308 * @cpu: The CPU whose state is to be dumped.
309 * @f: File to dump to.
310 * @cpu_fprintf: Function to dump with.
311 * @flags: Flags what to dump.
312 *
313 * Dumps CPU statistics.
314 */
315 void cpu_dump_statistics(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
316 int flags);
317
318 #ifndef CONFIG_USER_ONLY
319 /**
320 * cpu_get_phys_page_debug:
321 * @cpu: The CPU to obtain the physical page address for.
322 * @addr: The virtual address.
323 *
324 * Obtains the physical page corresponding to a virtual one.
325 * Use it only for debugging because no protection checks are done.
326 *
327 * Returns: Corresponding physical page address or -1 if no page found.
328 */
329 static inline hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr)
330 {
331 CPUClass *cc = CPU_GET_CLASS(cpu);
332
333 return cc->get_phys_page_debug(cpu, addr);
334 }
335 #endif
336
337 /**
338 * cpu_reset:
339 * @cpu: The CPU whose state is to be reset.
340 */
341 void cpu_reset(CPUState *cpu);
342
343 /**
344 * cpu_class_by_name:
345 * @typename: The CPU base type.
346 * @cpu_model: The model string without any parameters.
347 *
348 * Looks up a CPU #ObjectClass matching name @cpu_model.
349 *
350 * Returns: A #CPUClass or %NULL if not matching class is found.
351 */
352 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
353
354 /**
355 * cpu_has_work:
356 * @cpu: The vCPU to check.
357 *
358 * Checks whether the CPU has work to do.
359 *
360 * Returns: %true if the CPU has work, %false otherwise.
361 */
362 static inline bool cpu_has_work(CPUState *cpu)
363 {
364 CPUClass *cc = CPU_GET_CLASS(cpu);
365
366 g_assert(cc->has_work);
367 return cc->has_work(cpu);
368 }
369
370 /**
371 * qemu_cpu_is_self:
372 * @cpu: The vCPU to check against.
373 *
374 * Checks whether the caller is executing on the vCPU thread.
375 *
376 * Returns: %true if called from @cpu's thread, %false otherwise.
377 */
378 bool qemu_cpu_is_self(CPUState *cpu);
379
380 /**
381 * qemu_cpu_kick:
382 * @cpu: The vCPU to kick.
383 *
384 * Kicks @cpu's thread.
385 */
386 void qemu_cpu_kick(CPUState *cpu);
387
388 /**
389 * cpu_is_stopped:
390 * @cpu: The CPU to check.
391 *
392 * Checks whether the CPU is stopped.
393 *
394 * Returns: %true if run state is not running or if artificially stopped;
395 * %false otherwise.
396 */
397 bool cpu_is_stopped(CPUState *cpu);
398
399 /**
400 * run_on_cpu:
401 * @cpu: The vCPU to run on.
402 * @func: The function to be executed.
403 * @data: Data to pass to the function.
404 *
405 * Schedules the function @func for execution on the vCPU @cpu.
406 */
407 void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data);
408
409 /**
410 * async_run_on_cpu:
411 * @cpu: The vCPU to run on.
412 * @func: The function to be executed.
413 * @data: Data to pass to the function.
414 *
415 * Schedules the function @func for execution on the vCPU @cpu asynchronously.
416 */
417 void async_run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data);
418
419 /**
420 * qemu_get_cpu:
421 * @index: The CPUState@cpu_index value of the CPU to obtain.
422 *
423 * Gets a CPU matching @index.
424 *
425 * Returns: The CPU or %NULL if there is no matching CPU.
426 */
427 CPUState *qemu_get_cpu(int index);
428
429 /**
430 * cpu_exists:
431 * @id: Guest-exposed CPU ID to lookup.
432 *
433 * Search for CPU with specified ID.
434 *
435 * Returns: %true - CPU is found, %false - CPU isn't found.
436 */
437 bool cpu_exists(int64_t id);
438
439 #ifndef CONFIG_USER_ONLY
440
441 typedef void (*CPUInterruptHandler)(CPUState *, int);
442
443 extern CPUInterruptHandler cpu_interrupt_handler;
444
445 /**
446 * cpu_interrupt:
447 * @cpu: The CPU to set an interrupt on.
448 * @mask: The interupts to set.
449 *
450 * Invokes the interrupt handler.
451 */
452 static inline void cpu_interrupt(CPUState *cpu, int mask)
453 {
454 cpu_interrupt_handler(cpu, mask);
455 }
456
457 #else /* USER_ONLY */
458
459 void cpu_interrupt(CPUState *cpu, int mask);
460
461 #endif /* USER_ONLY */
462
463 #ifndef CONFIG_USER_ONLY
464
465 static inline void cpu_unassigned_access(CPUState *cpu, hwaddr addr,
466 bool is_write, bool is_exec,
467 int opaque, unsigned size)
468 {
469 CPUClass *cc = CPU_GET_CLASS(cpu);
470
471 if (cc->do_unassigned_access) {
472 cc->do_unassigned_access(cpu, addr, is_write, is_exec, opaque, size);
473 }
474 }
475
476 #endif
477
478 /**
479 * cpu_reset_interrupt:
480 * @cpu: The CPU to clear the interrupt on.
481 * @mask: The interrupt mask to clear.
482 *
483 * Resets interrupts on the vCPU @cpu.
484 */
485 void cpu_reset_interrupt(CPUState *cpu, int mask);
486
487 /**
488 * cpu_exit:
489 * @cpu: The CPU to exit.
490 *
491 * Requests the CPU @cpu to exit execution.
492 */
493 void cpu_exit(CPUState *cpu);
494
495 /**
496 * cpu_resume:
497 * @cpu: The CPU to resume.
498 *
499 * Resumes CPU, i.e. puts CPU into runnable state.
500 */
501 void cpu_resume(CPUState *cpu);
502
503 /**
504 * qemu_init_vcpu:
505 * @cpu: The vCPU to initialize.
506 *
507 * Initializes a vCPU.
508 */
509 void qemu_init_vcpu(CPUState *cpu);
510
511 #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
512 #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
513 #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
514
515 /**
516 * cpu_single_step:
517 * @cpu: CPU to the flags for.
518 * @enabled: Flags to enable.
519 *
520 * Enables or disables single-stepping for @cpu.
521 */
522 void cpu_single_step(CPUState *cpu, int enabled);
523
524 #ifdef CONFIG_SOFTMMU
525 extern const struct VMStateDescription vmstate_cpu_common;
526 #else
527 #define vmstate_cpu_common vmstate_dummy
528 #endif
529
530 #define VMSTATE_CPU() { \
531 .name = "parent_obj", \
532 .size = sizeof(CPUState), \
533 .vmsd = &vmstate_cpu_common, \
534 .flags = VMS_STRUCT, \
535 .offset = 0, \
536 }
537
538 #endif