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>
25 #include "hw/qdev-core.h"
26 #include "exec/hwaddr.h"
27 #include "qemu/queue.h"
28 #include "qemu/thread.h"
30 #include "qemu/typedefs.h"
32 typedef int (*WriteCoreDumpFunction
)(const void *buf
, size_t size
,
37 * Type wide enough to contain any #target_ulong virtual address.
39 typedef uint64_t vaddr
;
40 #define VADDR_PRId PRId64
41 #define VADDR_PRIu PRIu64
42 #define VADDR_PRIo PRIo64
43 #define VADDR_PRIx PRIx64
44 #define VADDR_PRIX PRIX64
45 #define VADDR_MAX UINT64_MAX
49 * @section_id: QEMU-cpu
51 * @short_description: Base class for all CPUs
54 #define TYPE_CPU "cpu"
56 /* Since this macro is used a lot in hot code paths and in conjunction with
57 * FooCPU *foo_env_get_cpu(), we deviate from usual QOM practice by using
60 #define CPU(obj) ((CPUState *)(obj))
62 #define CPU_CLASS(class) OBJECT_CLASS_CHECK(CPUClass, (class), TYPE_CPU)
63 #define CPU_GET_CLASS(obj) OBJECT_GET_CLASS(CPUClass, (obj), TYPE_CPU)
65 typedef struct CPUState CPUState
;
67 typedef void (*CPUUnassignedAccess
)(CPUState
*cpu
, hwaddr addr
,
68 bool is_write
, bool is_exec
, int opaque
,
71 struct TranslationBlock
;
75 * @class_by_name: Callback to map -cpu command line model name to an
76 * instantiatable CPU type.
77 * @parse_features: Callback to parse command line arguments.
78 * @reset: Callback to reset the #CPUState to its initial state.
79 * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
80 * @has_work: Callback for checking if there is work to do.
81 * @do_interrupt: Callback for interrupt handling.
82 * @do_unassigned_access: Callback for unassigned access handling.
83 * @do_unaligned_access: Callback for unaligned access handling, if
84 * the target defines #ALIGNED_ONLY.
85 * @memory_rw_debug: Callback for GDB memory access.
86 * @dump_state: Callback for dumping state.
87 * @dump_statistics: Callback for dumping statistics.
88 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
89 * @get_paging_enabled: Callback for inquiring whether paging is enabled.
90 * @get_memory_mapping: Callback for obtaining the memory mappings.
91 * @set_pc: Callback for setting the Program Counter register.
92 * @synchronize_from_tb: Callback for synchronizing state from a TCG
94 * @handle_mmu_fault: Callback for handling an MMU fault.
95 * @get_phys_page_debug: Callback for obtaining a physical address.
96 * @gdb_read_register: Callback for letting GDB read a register.
97 * @gdb_write_register: Callback for letting GDB write a register.
98 * @vmsd: State description for migration.
99 * @gdb_num_core_regs: Number of core registers accessible to GDB.
100 * @gdb_core_xml_file: File name for core registers GDB XML description.
102 * Represents a CPU family or model.
104 typedef struct CPUClass
{
106 DeviceClass parent_class
;
109 ObjectClass
*(*class_by_name
)(const char *cpu_model
);
110 void (*parse_features
)(CPUState
*cpu
, char *str
, Error
**errp
);
112 void (*reset
)(CPUState
*cpu
);
113 int reset_dump_flags
;
114 bool (*has_work
)(CPUState
*cpu
);
115 void (*do_interrupt
)(CPUState
*cpu
);
116 CPUUnassignedAccess do_unassigned_access
;
117 void (*do_unaligned_access
)(CPUState
*cpu
, vaddr addr
,
118 int is_write
, int is_user
, uintptr_t retaddr
);
119 int (*memory_rw_debug
)(CPUState
*cpu
, vaddr addr
,
120 uint8_t *buf
, int len
, bool is_write
);
121 void (*dump_state
)(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
123 void (*dump_statistics
)(CPUState
*cpu
, FILE *f
,
124 fprintf_function cpu_fprintf
, int flags
);
125 int64_t (*get_arch_id
)(CPUState
*cpu
);
126 bool (*get_paging_enabled
)(const CPUState
*cpu
);
127 void (*get_memory_mapping
)(CPUState
*cpu
, MemoryMappingList
*list
,
129 void (*set_pc
)(CPUState
*cpu
, vaddr value
);
130 void (*synchronize_from_tb
)(CPUState
*cpu
, struct TranslationBlock
*tb
);
131 int (*handle_mmu_fault
)(CPUState
*cpu
, vaddr address
, int rw
,
133 hwaddr (*get_phys_page_debug
)(CPUState
*cpu
, vaddr addr
);
134 int (*gdb_read_register
)(CPUState
*cpu
, uint8_t *buf
, int reg
);
135 int (*gdb_write_register
)(CPUState
*cpu
, uint8_t *buf
, int reg
);
137 int (*write_elf64_note
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
138 int cpuid
, void *opaque
);
139 int (*write_elf64_qemunote
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
141 int (*write_elf32_note
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
142 int cpuid
, void *opaque
);
143 int (*write_elf32_qemunote
)(WriteCoreDumpFunction f
, CPUState
*cpu
,
146 const struct VMStateDescription
*vmsd
;
147 int gdb_num_core_regs
;
148 const char *gdb_core_xml_file
;
151 #ifdef HOST_WORDS_BIGENDIAN
152 typedef struct icount_decr_u16
{
157 typedef struct icount_decr_u16
{
163 typedef struct CPUBreakpoint
{
165 int flags
; /* BP_* */
166 QTAILQ_ENTRY(CPUBreakpoint
) entry
;
169 typedef struct CPUWatchpoint
{
172 int flags
; /* BP_* */
173 QTAILQ_ENTRY(CPUWatchpoint
) entry
;
179 #define TB_JMP_CACHE_BITS 12
180 #define TB_JMP_CACHE_SIZE (1 << TB_JMP_CACHE_BITS)
184 * @cpu_index: CPU index (informative).
185 * @nr_cores: Number of cores within this CPU package.
186 * @nr_threads: Number of threads within this CPU.
187 * @numa_node: NUMA node this CPU is belonging to.
188 * @host_tid: Host thread ID.
189 * @running: #true if CPU is currently running (usermode).
190 * @created: Indicates whether the CPU thread has been successfully created.
191 * @interrupt_request: Indicates a pending interrupt request.
192 * @halted: Nonzero if the CPU is in suspended state.
193 * @stop: Indicates a pending stop request.
194 * @stopped: Indicates the CPU has been artificially stopped.
195 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
196 * CPU and return to its top level loop.
197 * @singlestep_enabled: Flags for single-stepping.
198 * @icount_extra: Instructions until next timer event.
199 * @icount_decr: Number of cycles left, with interrupt flag in high bit.
200 * This allows a single read-compare-cbranch-write sequence to test
201 * for both decrementer underflow and exceptions.
202 * @can_do_io: Nonzero if memory-mapped IO is safe.
203 * @env_ptr: Pointer to subclass-specific CPUArchState field.
204 * @current_tb: Currently executing TB.
205 * @gdb_regs: Additional GDB registers.
206 * @gdb_num_regs: Number of total registers accessible to GDB.
207 * @gdb_num_g_regs: Number of registers in GDB 'g' packets.
208 * @next_cpu: Next CPU sharing TB cache.
209 * @opaque: User data.
210 * @mem_io_pc: Host Program Counter at which the memory was accessed.
211 * @mem_io_vaddr: Target virtual address at which the memory was accessed.
212 * @kvm_fd: vCPU file descriptor for KVM.
214 * State of one CPU core or thread.
218 DeviceState parent_obj
;
225 struct QemuThread
*thread
;
232 struct QemuCond
*halt_cond
;
233 struct qemu_work_item
*queued_work_first
, *queued_work_last
;
238 volatile sig_atomic_t exit_request
;
239 uint32_t interrupt_request
;
240 int singlestep_enabled
;
241 int64_t icount_extra
;
245 MemoryListener
*tcg_as_listener
;
247 void *env_ptr
; /* CPUArchState */
248 struct TranslationBlock
*current_tb
;
249 struct TranslationBlock
*tb_jmp_cache
[TB_JMP_CACHE_SIZE
];
250 struct GDBRegisterState
*gdb_regs
;
253 QTAILQ_ENTRY(CPUState
) node
;
255 /* ice debug support */
256 QTAILQ_HEAD(breakpoints_head
, CPUBreakpoint
) breakpoints
;
258 QTAILQ_HEAD(watchpoints_head
, CPUWatchpoint
) watchpoints
;
259 CPUWatchpoint
*watchpoint_hit
;
263 /* In order to avoid passing too many arguments to the MMIO helpers,
264 * we store some rarely used information in the CPU context.
271 struct KVMState
*kvm_state
;
272 struct kvm_run
*kvm_run
;
274 /* TODO Move common fields from CPUArchState here. */
275 int cpu_index
; /* used by alpha TCG */
276 uint32_t halted
; /* used by alpha, cris, ppc TCG */
282 int32_t exception_index
; /* used by m68k TCG */
284 /* Note that this is accessed at the start of every TB via a negative
285 offset from AREG0. Leave this field at the end so as to make the
286 (absolute value) offset as small as possible. This reduces code
287 size, especially for hosts without large memory offsets. */
288 volatile sig_atomic_t tcg_exit_req
;
291 QTAILQ_HEAD(CPUTailQ
, CPUState
);
292 extern struct CPUTailQ cpus
;
293 #define CPU_NEXT(cpu) QTAILQ_NEXT(cpu, node)
294 #define CPU_FOREACH(cpu) QTAILQ_FOREACH(cpu, &cpus, node)
295 #define CPU_FOREACH_SAFE(cpu, next_cpu) \
296 QTAILQ_FOREACH_SAFE(cpu, &cpus, node, next_cpu)
297 #define first_cpu QTAILQ_FIRST(&cpus)
299 DECLARE_TLS(CPUState
*, current_cpu
);
300 #define current_cpu tls_var(current_cpu)
303 * cpu_paging_enabled:
304 * @cpu: The CPU whose state is to be inspected.
306 * Returns: %true if paging is enabled, %false otherwise.
308 bool cpu_paging_enabled(const CPUState
*cpu
);
311 * cpu_get_memory_mapping:
312 * @cpu: The CPU whose memory mappings are to be obtained.
313 * @list: Where to write the memory mappings to.
314 * @errp: Pointer for reporting an #Error.
316 void cpu_get_memory_mapping(CPUState
*cpu
, MemoryMappingList
*list
,
320 * cpu_write_elf64_note:
321 * @f: pointer to a function that writes memory to a file
322 * @cpu: The CPU whose memory is to be dumped
323 * @cpuid: ID number of the CPU
324 * @opaque: pointer to the CPUState struct
326 int cpu_write_elf64_note(WriteCoreDumpFunction f
, CPUState
*cpu
,
327 int cpuid
, void *opaque
);
330 * cpu_write_elf64_qemunote:
331 * @f: pointer to a function that writes memory to a file
332 * @cpu: The CPU whose memory is to be dumped
333 * @cpuid: ID number of the CPU
334 * @opaque: pointer to the CPUState struct
336 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f
, CPUState
*cpu
,
340 * cpu_write_elf32_note:
341 * @f: pointer to a function that writes memory to a file
342 * @cpu: The CPU whose memory is to be dumped
343 * @cpuid: ID number of the CPU
344 * @opaque: pointer to the CPUState struct
346 int cpu_write_elf32_note(WriteCoreDumpFunction f
, CPUState
*cpu
,
347 int cpuid
, void *opaque
);
350 * cpu_write_elf32_qemunote:
351 * @f: pointer to a function that writes memory to a file
352 * @cpu: The CPU whose memory is to be dumped
353 * @cpuid: ID number of the CPU
354 * @opaque: pointer to the CPUState struct
356 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f
, CPUState
*cpu
,
362 * @CPU_DUMP_FPU: dump FPU register state, not just integer
363 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
366 CPU_DUMP_CODE
= 0x00010000,
367 CPU_DUMP_FPU
= 0x00020000,
368 CPU_DUMP_CCOP
= 0x00040000,
373 * @cpu: The CPU whose state is to be dumped.
374 * @f: File to dump to.
375 * @cpu_fprintf: Function to dump with.
376 * @flags: Flags what to dump.
380 void cpu_dump_state(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
384 * cpu_dump_statistics:
385 * @cpu: The CPU whose state is to be dumped.
386 * @f: File to dump to.
387 * @cpu_fprintf: Function to dump with.
388 * @flags: Flags what to dump.
390 * Dumps CPU statistics.
392 void cpu_dump_statistics(CPUState
*cpu
, FILE *f
, fprintf_function cpu_fprintf
,
395 #ifndef CONFIG_USER_ONLY
397 * cpu_get_phys_page_debug:
398 * @cpu: The CPU to obtain the physical page address for.
399 * @addr: The virtual address.
401 * Obtains the physical page corresponding to a virtual one.
402 * Use it only for debugging because no protection checks are done.
404 * Returns: Corresponding physical page address or -1 if no page found.
406 static inline hwaddr
cpu_get_phys_page_debug(CPUState
*cpu
, vaddr addr
)
408 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
410 return cc
->get_phys_page_debug(cpu
, addr
);
416 * @cpu: The CPU whose state is to be reset.
418 void cpu_reset(CPUState
*cpu
);
422 * @typename: The CPU base type.
423 * @cpu_model: The model string without any parameters.
425 * Looks up a CPU #ObjectClass matching name @cpu_model.
427 * Returns: A #CPUClass or %NULL if not matching class is found.
429 ObjectClass
*cpu_class_by_name(const char *typename
, const char *cpu_model
);
433 * @typename: The CPU base type.
434 * @cpu_model: The model string including optional parameters.
436 * Instantiates a CPU, processes optional parameters and realizes the CPU.
438 * Returns: A #CPUState or %NULL if an error occurred.
440 CPUState
*cpu_generic_init(const char *typename
, const char *cpu_model
);
444 * @cpu: The vCPU to check.
446 * Checks whether the CPU has work to do.
448 * Returns: %true if the CPU has work, %false otherwise.
450 static inline bool cpu_has_work(CPUState
*cpu
)
452 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
454 g_assert(cc
->has_work
);
455 return cc
->has_work(cpu
);
460 * @cpu: The vCPU to check against.
462 * Checks whether the caller is executing on the vCPU thread.
464 * Returns: %true if called from @cpu's thread, %false otherwise.
466 bool qemu_cpu_is_self(CPUState
*cpu
);
470 * @cpu: The vCPU to kick.
472 * Kicks @cpu's thread.
474 void qemu_cpu_kick(CPUState
*cpu
);
478 * @cpu: The CPU to check.
480 * Checks whether the CPU is stopped.
482 * Returns: %true if run state is not running or if artificially stopped;
485 bool cpu_is_stopped(CPUState
*cpu
);
489 * @cpu: The vCPU to run on.
490 * @func: The function to be executed.
491 * @data: Data to pass to the function.
493 * Schedules the function @func for execution on the vCPU @cpu.
495 void run_on_cpu(CPUState
*cpu
, void (*func
)(void *data
), void *data
);
499 * @cpu: The vCPU to run on.
500 * @func: The function to be executed.
501 * @data: Data to pass to the function.
503 * Schedules the function @func for execution on the vCPU @cpu asynchronously.
505 void async_run_on_cpu(CPUState
*cpu
, void (*func
)(void *data
), void *data
);
509 * @index: The CPUState@cpu_index value of the CPU to obtain.
511 * Gets a CPU matching @index.
513 * Returns: The CPU or %NULL if there is no matching CPU.
515 CPUState
*qemu_get_cpu(int index
);
519 * @id: Guest-exposed CPU ID to lookup.
521 * Search for CPU with specified ID.
523 * Returns: %true - CPU is found, %false - CPU isn't found.
525 bool cpu_exists(int64_t id
);
527 #ifndef CONFIG_USER_ONLY
529 typedef void (*CPUInterruptHandler
)(CPUState
*, int);
531 extern CPUInterruptHandler cpu_interrupt_handler
;
535 * @cpu: The CPU to set an interrupt on.
536 * @mask: The interupts to set.
538 * Invokes the interrupt handler.
540 static inline void cpu_interrupt(CPUState
*cpu
, int mask
)
542 cpu_interrupt_handler(cpu
, mask
);
545 #else /* USER_ONLY */
547 void cpu_interrupt(CPUState
*cpu
, int mask
);
549 #endif /* USER_ONLY */
551 #ifdef CONFIG_SOFTMMU
552 static inline void cpu_unassigned_access(CPUState
*cpu
, hwaddr addr
,
553 bool is_write
, bool is_exec
,
554 int opaque
, unsigned size
)
556 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
558 if (cc
->do_unassigned_access
) {
559 cc
->do_unassigned_access(cpu
, addr
, is_write
, is_exec
, opaque
, size
);
563 static inline void cpu_unaligned_access(CPUState
*cpu
, vaddr addr
,
564 int is_write
, int is_user
,
567 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
569 return cc
->do_unaligned_access(cpu
, addr
, is_write
, is_user
, retaddr
);
574 * cpu_reset_interrupt:
575 * @cpu: The CPU to clear the interrupt on.
576 * @mask: The interrupt mask to clear.
578 * Resets interrupts on the vCPU @cpu.
580 void cpu_reset_interrupt(CPUState
*cpu
, int mask
);
584 * @cpu: The CPU to exit.
586 * Requests the CPU @cpu to exit execution.
588 void cpu_exit(CPUState
*cpu
);
592 * @cpu: The CPU to resume.
594 * Resumes CPU, i.e. puts CPU into runnable state.
596 void cpu_resume(CPUState
*cpu
);
600 * @cpu: The vCPU to initialize.
602 * Initializes a vCPU.
604 void qemu_init_vcpu(CPUState
*cpu
);
606 #define SSTEP_ENABLE 0x1 /* Enable simulated HW single stepping */
607 #define SSTEP_NOIRQ 0x2 /* Do not use IRQ while single stepping */
608 #define SSTEP_NOTIMER 0x4 /* Do not Timers while single stepping */
612 * @cpu: CPU to the flags for.
613 * @enabled: Flags to enable.
615 * Enables or disables single-stepping for @cpu.
617 void cpu_single_step(CPUState
*cpu
, int enabled
);
619 /* Breakpoint/watchpoint flags */
620 #define BP_MEM_READ 0x01
621 #define BP_MEM_WRITE 0x02
622 #define BP_MEM_ACCESS (BP_MEM_READ | BP_MEM_WRITE)
623 #define BP_STOP_BEFORE_ACCESS 0x04
624 #define BP_WATCHPOINT_HIT 0x08
628 int cpu_breakpoint_insert(CPUState
*cpu
, vaddr pc
, int flags
,
629 CPUBreakpoint
**breakpoint
);
630 int cpu_breakpoint_remove(CPUState
*cpu
, vaddr pc
, int flags
);
631 void cpu_breakpoint_remove_by_ref(CPUState
*cpu
, CPUBreakpoint
*breakpoint
);
632 void cpu_breakpoint_remove_all(CPUState
*cpu
, int mask
);
634 int cpu_watchpoint_insert(CPUState
*cpu
, vaddr addr
, vaddr len
,
635 int flags
, CPUWatchpoint
**watchpoint
);
636 int cpu_watchpoint_remove(CPUState
*cpu
, vaddr addr
,
637 vaddr len
, int flags
);
638 void cpu_watchpoint_remove_by_ref(CPUState
*cpu
, CPUWatchpoint
*watchpoint
);
639 void cpu_watchpoint_remove_all(CPUState
*cpu
, int mask
);
641 void QEMU_NORETURN
cpu_abort(CPUState
*cpu
, const char *fmt
, ...)
644 #ifdef CONFIG_SOFTMMU
645 extern const struct VMStateDescription vmstate_cpu_common
;
647 #define vmstate_cpu_common vmstate_dummy
650 #define VMSTATE_CPU() { \
651 .name = "parent_obj", \
652 .size = sizeof(CPUState), \
653 .vmsd = &vmstate_cpu_common, \
654 .flags = VMS_STRUCT, \