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