net/cadence_gem: Don't assert against 0 buffer address
[qemu.git] / include / qom / cpu.h
blob7739e000679f85dd0f7f2a15f4c3d4dace9fabb9
1 /*
2 * QEMU CPU model
4 * Copyright (c) 2012 SUSE LINUX Products GmbH
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.
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.
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>
20 #ifndef QEMU_CPU_H
21 #define QEMU_CPU_H
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"
31 typedef int (*WriteCoreDumpFunction)(void *buf, size_t size, void *opaque);
33 /**
34 * vaddr:
35 * Type wide enough to contain any #target_ulong virtual address.
37 typedef uint64_t vaddr;
38 #define VADDR_PRId PRId64
39 #define VADDR_PRIu PRIu64
40 #define VADDR_PRIo PRIo64
41 #define VADDR_PRIx PRIx64
42 #define VADDR_PRIX PRIX64
43 #define VADDR_MAX UINT64_MAX
45 /**
46 * SECTION:cpu
47 * @section_id: QEMU-cpu
48 * @title: CPU Class
49 * @short_description: Base class for all CPUs
52 #define TYPE_CPU "cpu"
54 #define CPU(obj) OBJECT_CHECK(CPUState, (obj), TYPE_CPU)
55 #define CPU_CLASS(class) OBJECT_CLASS_CHECK(CPUClass, (class), TYPE_CPU)
56 #define CPU_GET_CLASS(obj) OBJECT_GET_CLASS(CPUClass, (obj), TYPE_CPU)
58 typedef struct CPUState CPUState;
60 typedef void (*CPUUnassignedAccess)(CPUState *cpu, hwaddr addr,
61 bool is_write, bool is_exec, int opaque,
62 unsigned size);
64 struct TranslationBlock;
66 /**
67 * CPUClass:
68 * @class_by_name: Callback to map -cpu command line model name to an
69 * instantiatable CPU type.
70 * @reset: Callback to reset the #CPUState to its initial state.
71 * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
72 * @do_interrupt: Callback for interrupt handling.
73 * @do_unassigned_access: Callback for unassigned access handling.
74 * @memory_rw_debug: Callback for GDB memory access.
75 * @dump_state: Callback for dumping state.
76 * @dump_statistics: Callback for dumping statistics.
77 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
78 * @get_paging_enabled: Callback for inquiring whether paging is enabled.
79 * @get_memory_mapping: Callback for obtaining the memory mappings.
80 * @set_pc: Callback for setting the Program Counter register.
81 * @synchronize_from_tb: Callback for synchronizing state from a TCG
82 * #TranslationBlock.
83 * @get_phys_page_debug: Callback for obtaining a physical address.
84 * @gdb_read_register: Callback for letting GDB read a register.
85 * @gdb_write_register: Callback for letting GDB write a register.
86 * @vmsd: State description for migration.
87 * @gdb_num_core_regs: Number of core registers accessible to GDB.
88 * @gdb_core_xml_file: File name for core registers GDB XML description.
90 * Represents a CPU family or model.
92 typedef struct CPUClass {
93 /*< private >*/
94 DeviceClass parent_class;
95 /*< public >*/
97 ObjectClass *(*class_by_name)(const char *cpu_model);
99 void (*reset)(CPUState *cpu);
100 int reset_dump_flags;
101 void (*do_interrupt)(CPUState *cpu);
102 CPUUnassignedAccess do_unassigned_access;
103 int (*memory_rw_debug)(CPUState *cpu, vaddr addr,
104 uint8_t *buf, int len, bool is_write);
105 void (*dump_state)(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
106 int flags);
107 void (*dump_statistics)(CPUState *cpu, FILE *f,
108 fprintf_function cpu_fprintf, int flags);
109 int64_t (*get_arch_id)(CPUState *cpu);
110 bool (*get_paging_enabled)(const CPUState *cpu);
111 void (*get_memory_mapping)(CPUState *cpu, MemoryMappingList *list,
112 Error **errp);
113 void (*set_pc)(CPUState *cpu, vaddr value);
114 void (*synchronize_from_tb)(CPUState *cpu, struct TranslationBlock *tb);
115 hwaddr (*get_phys_page_debug)(CPUState *cpu, vaddr addr);
116 int (*gdb_read_register)(CPUState *cpu, uint8_t *buf, int reg);
117 int (*gdb_write_register)(CPUState *cpu, uint8_t *buf, int reg);
119 int (*write_elf64_note)(WriteCoreDumpFunction f, CPUState *cpu,
120 int cpuid, void *opaque);
121 int (*write_elf64_qemunote)(WriteCoreDumpFunction f, CPUState *cpu,
122 void *opaque);
123 int (*write_elf32_note)(WriteCoreDumpFunction f, CPUState *cpu,
124 int cpuid, void *opaque);
125 int (*write_elf32_qemunote)(WriteCoreDumpFunction f, CPUState *cpu,
126 void *opaque);
128 const struct VMStateDescription *vmsd;
129 int gdb_num_core_regs;
130 const char *gdb_core_xml_file;
131 } CPUClass;
133 struct KVMState;
134 struct kvm_run;
137 * CPUState:
138 * @cpu_index: CPU index (informative).
139 * @nr_cores: Number of cores within this CPU package.
140 * @nr_threads: Number of threads within this CPU.
141 * @numa_node: NUMA node this CPU is belonging to.
142 * @host_tid: Host thread ID.
143 * @running: #true if CPU is currently running (usermode).
144 * @created: Indicates whether the CPU thread has been successfully created.
145 * @interrupt_request: Indicates a pending interrupt request.
146 * @halted: Nonzero if the CPU is in suspended state.
147 * @stop: Indicates a pending stop request.
148 * @stopped: Indicates the CPU has been artificially stopped.
149 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
150 * CPU and return to its top level loop.
151 * @singlestep_enabled: Flags for single-stepping.
152 * @env_ptr: Pointer to subclass-specific CPUArchState field.
153 * @current_tb: Currently executing TB.
154 * @gdb_regs: Additional GDB registers.
155 * @gdb_num_regs: Number of total registers accessible to GDB.
156 * @gdb_num_g_regs: Number of registers in GDB 'g' packets.
157 * @next_cpu: Next CPU sharing TB cache.
158 * @kvm_fd: vCPU file descriptor for KVM.
160 * State of one CPU core or thread.
162 struct CPUState {
163 /*< private >*/
164 DeviceState parent_obj;
165 /*< public >*/
167 int nr_cores;
168 int nr_threads;
169 int numa_node;
171 struct QemuThread *thread;
172 #ifdef _WIN32
173 HANDLE hThread;
174 #endif
175 int thread_id;
176 uint32_t host_tid;
177 bool running;
178 struct QemuCond *halt_cond;
179 struct qemu_work_item *queued_work_first, *queued_work_last;
180 bool thread_kicked;
181 bool created;
182 bool stop;
183 bool stopped;
184 volatile sig_atomic_t exit_request;
185 volatile sig_atomic_t tcg_exit_req;
186 uint32_t interrupt_request;
187 int singlestep_enabled;
189 void *env_ptr; /* CPUArchState */
190 struct TranslationBlock *current_tb;
191 struct GDBRegisterState *gdb_regs;
192 int gdb_num_regs;
193 int gdb_num_g_regs;
194 QTAILQ_ENTRY(CPUState) node;
196 int kvm_fd;
197 bool kvm_vcpu_dirty;
198 struct KVMState *kvm_state;
199 struct kvm_run *kvm_run;
201 /* TODO Move common fields from CPUArchState here. */
202 int cpu_index; /* used by alpha TCG */
203 uint32_t halted; /* used by alpha, cris, ppc TCG */
206 QTAILQ_HEAD(CPUTailQ, CPUState);
207 extern struct CPUTailQ cpus;
208 #define CPU_NEXT(cpu) QTAILQ_NEXT(cpu, node)
209 #define CPU_FOREACH(cpu) QTAILQ_FOREACH(cpu, &cpus, node)
210 #define CPU_FOREACH_SAFE(cpu, next_cpu) \
211 QTAILQ_FOREACH_SAFE(cpu, &cpus, node, next_cpu)
212 #define first_cpu QTAILQ_FIRST(&cpus)
214 DECLARE_TLS(CPUState *, current_cpu);
215 #define current_cpu tls_var(current_cpu)
218 * cpu_paging_enabled:
219 * @cpu: The CPU whose state is to be inspected.
221 * Returns: %true if paging is enabled, %false otherwise.
223 bool cpu_paging_enabled(const CPUState *cpu);
226 * cpu_get_memory_mapping:
227 * @cpu: The CPU whose memory mappings are to be obtained.
228 * @list: Where to write the memory mappings to.
229 * @errp: Pointer for reporting an #Error.
231 void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
232 Error **errp);
235 * cpu_write_elf64_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
241 int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
242 int cpuid, void *opaque);
245 * cpu_write_elf64_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
251 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
252 void *opaque);
255 * cpu_write_elf32_note:
256 * @f: pointer to a function that writes memory to a file
257 * @cpu: The CPU whose memory is to be dumped
258 * @cpuid: ID number of the CPU
259 * @opaque: pointer to the CPUState struct
261 int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
262 int cpuid, void *opaque);
265 * cpu_write_elf32_qemunote:
266 * @f: pointer to a function that writes memory to a file
267 * @cpu: The CPU whose memory is to be dumped
268 * @cpuid: ID number of the CPU
269 * @opaque: pointer to the CPUState struct
271 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
272 void *opaque);
275 * CPUDumpFlags:
276 * @CPU_DUMP_CODE:
277 * @CPU_DUMP_FPU: dump FPU register state, not just integer
278 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
280 enum CPUDumpFlags {
281 CPU_DUMP_CODE = 0x00010000,
282 CPU_DUMP_FPU = 0x00020000,
283 CPU_DUMP_CCOP = 0x00040000,
287 * cpu_dump_state:
288 * @cpu: The CPU whose state is to be dumped.
289 * @f: File to dump to.
290 * @cpu_fprintf: Function to dump with.
291 * @flags: Flags what to dump.
293 * Dumps CPU state.
295 void cpu_dump_state(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
296 int flags);
299 * cpu_dump_statistics:
300 * @cpu: The CPU whose state is to be dumped.
301 * @f: File to dump to.
302 * @cpu_fprintf: Function to dump with.
303 * @flags: Flags what to dump.
305 * Dumps CPU statistics.
307 void cpu_dump_statistics(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
308 int flags);
310 #ifndef CONFIG_USER_ONLY
312 * cpu_get_phys_page_debug:
313 * @cpu: The CPU to obtain the physical page address for.
314 * @addr: The virtual address.
316 * Obtains the physical page corresponding to a virtual one.
317 * Use it only for debugging because no protection checks are done.
319 * Returns: Corresponding physical page address or -1 if no page found.
321 static inline hwaddr cpu_get_phys_page_debug(CPUState *cpu, vaddr addr)
323 CPUClass *cc = CPU_GET_CLASS(cpu);
325 return cc->get_phys_page_debug(cpu, addr);
327 #endif
330 * cpu_reset:
331 * @cpu: The CPU whose state is to be reset.
333 void cpu_reset(CPUState *cpu);
336 * cpu_class_by_name:
337 * @typename: The CPU base type.
338 * @cpu_model: The model string without any parameters.
340 * Looks up a CPU #ObjectClass matching name @cpu_model.
342 * Returns: A #CPUClass or %NULL if not matching class is found.
344 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
347 * qemu_cpu_has_work:
348 * @cpu: The vCPU to check.
350 * Checks whether the CPU has work to do.
352 * Returns: %true if the CPU has work, %false otherwise.
354 bool qemu_cpu_has_work(CPUState *cpu);
357 * qemu_cpu_is_self:
358 * @cpu: The vCPU to check against.
360 * Checks whether the caller is executing on the vCPU thread.
362 * Returns: %true if called from @cpu's thread, %false otherwise.
364 bool qemu_cpu_is_self(CPUState *cpu);
367 * qemu_cpu_kick:
368 * @cpu: The vCPU to kick.
370 * Kicks @cpu's thread.
372 void qemu_cpu_kick(CPUState *cpu);
375 * cpu_is_stopped:
376 * @cpu: The CPU to check.
378 * Checks whether the CPU is stopped.
380 * Returns: %true if run state is not running or if artificially stopped;
381 * %false otherwise.
383 bool cpu_is_stopped(CPUState *cpu);
386 * run_on_cpu:
387 * @cpu: The vCPU to run on.
388 * @func: The function to be executed.
389 * @data: Data to pass to the function.
391 * Schedules the function @func for execution on the vCPU @cpu.
393 void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data);
396 * async_run_on_cpu:
397 * @cpu: The vCPU to run on.
398 * @func: The function to be executed.
399 * @data: Data to pass to the function.
401 * Schedules the function @func for execution on the vCPU @cpu asynchronously.
403 void async_run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data);
406 * qemu_get_cpu:
407 * @index: The CPUState@cpu_index value of the CPU to obtain.
409 * Gets a CPU matching @index.
411 * Returns: The CPU or %NULL if there is no matching CPU.
413 CPUState *qemu_get_cpu(int index);
416 * cpu_exists:
417 * @id: Guest-exposed CPU ID to lookup.
419 * Search for CPU with specified ID.
421 * Returns: %true - CPU is found, %false - CPU isn't found.
423 bool cpu_exists(int64_t id);
425 #ifndef CONFIG_USER_ONLY
427 typedef void (*CPUInterruptHandler)(CPUState *, int);
429 extern CPUInterruptHandler cpu_interrupt_handler;
432 * cpu_interrupt:
433 * @cpu: The CPU to set an interrupt on.
434 * @mask: The interupts to set.
436 * Invokes the interrupt handler.
438 static inline void cpu_interrupt(CPUState *cpu, int mask)
440 cpu_interrupt_handler(cpu, mask);
443 #else /* USER_ONLY */
445 void cpu_interrupt(CPUState *cpu, int mask);
447 #endif /* USER_ONLY */
449 #ifndef CONFIG_USER_ONLY
451 static inline void cpu_unassigned_access(CPUState *cpu, hwaddr addr,
452 bool is_write, bool is_exec,
453 int opaque, unsigned size)
455 CPUClass *cc = CPU_GET_CLASS(cpu);
457 if (cc->do_unassigned_access) {
458 cc->do_unassigned_access(cpu, addr, is_write, is_exec, opaque, size);
462 #endif
465 * cpu_reset_interrupt:
466 * @cpu: The CPU to clear the interrupt on.
467 * @mask: The interrupt mask to clear.
469 * Resets interrupts on the vCPU @cpu.
471 void cpu_reset_interrupt(CPUState *cpu, int mask);
474 * cpu_exit:
475 * @cpu: The CPU to exit.
477 * Requests the CPU @cpu to exit execution.
479 void cpu_exit(CPUState *cpu);
482 * cpu_resume:
483 * @cpu: The CPU to resume.
485 * Resumes CPU, i.e. puts CPU into runnable state.
487 void cpu_resume(CPUState *cpu);
490 * qemu_init_vcpu:
491 * @cpu: The vCPU to initialize.
493 * Initializes a vCPU.
495 void qemu_init_vcpu(CPUState *cpu);
497 #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
498 #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
499 #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
502 * cpu_single_step:
503 * @cpu: CPU to the flags for.
504 * @enabled: Flags to enable.
506 * Enables or disables single-stepping for @cpu.
508 void cpu_single_step(CPUState *cpu, int enabled);
510 #ifdef CONFIG_SOFTMMU
511 extern const struct VMStateDescription vmstate_cpu_common;
512 #else
513 #define vmstate_cpu_common vmstate_dummy
514 #endif
516 #define VMSTATE_CPU() { \
517 .name = "parent_obj", \
518 .size = sizeof(CPUState), \
519 .vmsd = &vmstate_cpu_common, \
520 .flags = VMS_STRUCT, \
521 .offset = 0, \
524 #endif