cpu: Turn cpu_paging_enabled() into a CPUState hook
[qemu.git] / include / qom / cpu.h
blob1f7024099116dfec35aaa65d8df0173ca9a59d7b
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 "qemu/thread.h"
27 typedef int (*WriteCoreDumpFunction)(void *buf, size_t size, void *opaque);
29 /**
30 * SECTION:cpu
31 * @section_id: QEMU-cpu
32 * @title: CPU Class
33 * @short_description: Base class for all CPUs
36 #define TYPE_CPU "cpu"
38 #define CPU(obj) OBJECT_CHECK(CPUState, (obj), TYPE_CPU)
39 #define CPU_CLASS(class) OBJECT_CLASS_CHECK(CPUClass, (class), TYPE_CPU)
40 #define CPU_GET_CLASS(obj) OBJECT_GET_CLASS(CPUClass, (obj), TYPE_CPU)
42 typedef struct CPUState CPUState;
44 /**
45 * CPUClass:
46 * @class_by_name: Callback to map -cpu command line model name to an
47 * instantiatable CPU type.
48 * @reset: Callback to reset the #CPUState to its initial state.
49 * @do_interrupt: Callback for interrupt handling.
50 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
51 * @get_paging_enabled: Callback for inquiring whether paging is enabled.
52 * @vmsd: State description for migration.
54 * Represents a CPU family or model.
56 typedef struct CPUClass {
57 /*< private >*/
58 DeviceClass parent_class;
59 /*< public >*/
61 ObjectClass *(*class_by_name)(const char *cpu_model);
63 void (*reset)(CPUState *cpu);
64 void (*do_interrupt)(CPUState *cpu);
65 int64_t (*get_arch_id)(CPUState *cpu);
66 bool (*get_paging_enabled)(const CPUState *cpu);
68 const struct VMStateDescription *vmsd;
69 int (*write_elf64_note)(WriteCoreDumpFunction f, CPUState *cpu,
70 int cpuid, void *opaque);
71 int (*write_elf64_qemunote)(WriteCoreDumpFunction f, CPUState *cpu,
72 void *opaque);
73 int (*write_elf32_note)(WriteCoreDumpFunction f, CPUState *cpu,
74 int cpuid, void *opaque);
75 int (*write_elf32_qemunote)(WriteCoreDumpFunction f, CPUState *cpu,
76 void *opaque);
77 } CPUClass;
79 struct KVMState;
80 struct kvm_run;
82 /**
83 * CPUState:
84 * @cpu_index: CPU index (informative).
85 * @nr_cores: Number of cores within this CPU package.
86 * @nr_threads: Number of threads within this CPU.
87 * @numa_node: NUMA node this CPU is belonging to.
88 * @host_tid: Host thread ID.
89 * @running: #true if CPU is currently running (usermode).
90 * @created: Indicates whether the CPU thread has been successfully created.
91 * @interrupt_request: Indicates a pending interrupt request.
92 * @halted: Nonzero if the CPU is in suspended state.
93 * @stop: Indicates a pending stop request.
94 * @stopped: Indicates the CPU has been artificially stopped.
95 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
96 * CPU and return to its top level loop.
97 * @env_ptr: Pointer to subclass-specific CPUArchState field.
98 * @current_tb: Currently executing TB.
99 * @kvm_fd: vCPU file descriptor for KVM.
101 * State of one CPU core or thread.
103 struct CPUState {
104 /*< private >*/
105 DeviceState parent_obj;
106 /*< public >*/
108 int nr_cores;
109 int nr_threads;
110 int numa_node;
112 struct QemuThread *thread;
113 #ifdef _WIN32
114 HANDLE hThread;
115 #endif
116 int thread_id;
117 uint32_t host_tid;
118 bool running;
119 struct QemuCond *halt_cond;
120 struct qemu_work_item *queued_work_first, *queued_work_last;
121 bool thread_kicked;
122 bool created;
123 bool stop;
124 bool stopped;
125 volatile sig_atomic_t exit_request;
126 volatile sig_atomic_t tcg_exit_req;
127 uint32_t interrupt_request;
129 void *env_ptr; /* CPUArchState */
130 struct TranslationBlock *current_tb;
132 int kvm_fd;
133 bool kvm_vcpu_dirty;
134 struct KVMState *kvm_state;
135 struct kvm_run *kvm_run;
137 /* TODO Move common fields from CPUArchState here. */
138 int cpu_index; /* used by alpha TCG */
139 uint32_t halted; /* used by alpha, cris, ppc TCG */
143 * cpu_paging_enabled:
144 * @cpu: The CPU whose state is to be inspected.
146 * Returns: %true if paging is enabled, %false otherwise.
148 bool cpu_paging_enabled(const CPUState *cpu);
151 * cpu_write_elf64_note:
152 * @f: pointer to a function that writes memory to a file
153 * @cpu: The CPU whose memory is to be dumped
154 * @cpuid: ID number of the CPU
155 * @opaque: pointer to the CPUState struct
157 int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
158 int cpuid, void *opaque);
161 * cpu_write_elf64_qemunote:
162 * @f: pointer to a function that writes memory to a file
163 * @cpu: The CPU whose memory is to be dumped
164 * @cpuid: ID number of the CPU
165 * @opaque: pointer to the CPUState struct
167 int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
168 void *opaque);
171 * cpu_write_elf32_note:
172 * @f: pointer to a function that writes memory to a file
173 * @cpu: The CPU whose memory is to be dumped
174 * @cpuid: ID number of the CPU
175 * @opaque: pointer to the CPUState struct
177 int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
178 int cpuid, void *opaque);
181 * cpu_write_elf32_qemunote:
182 * @f: pointer to a function that writes memory to a file
183 * @cpu: The CPU whose memory is to be dumped
184 * @cpuid: ID number of the CPU
185 * @opaque: pointer to the CPUState struct
187 int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
188 void *opaque);
191 * cpu_reset:
192 * @cpu: The CPU whose state is to be reset.
194 void cpu_reset(CPUState *cpu);
197 * cpu_class_by_name:
198 * @typename: The CPU base type.
199 * @cpu_model: The model string without any parameters.
201 * Looks up a CPU #ObjectClass matching name @cpu_model.
203 * Returns: A #CPUClass or %NULL if not matching class is found.
205 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
208 * cpu_class_set_vmsd:
209 * @cc: CPU class
210 * @value: Value to set. Unused for %CONFIG_USER_ONLY.
212 * Sets #VMStateDescription for @cc.
214 * The @value argument is intentionally discarded for the non-softmmu targets
215 * to avoid linker errors or excessive preprocessor usage. If this behavior
216 * is undesired, you should assign #CPUState.vmsd directly instead.
218 #ifndef CONFIG_USER_ONLY
219 static inline void cpu_class_set_vmsd(CPUClass *cc,
220 const struct VMStateDescription *value)
222 cc->vmsd = value;
224 #else
225 #define cpu_class_set_vmsd(cc, value) ((cc)->vmsd = NULL)
226 #endif
229 * qemu_cpu_has_work:
230 * @cpu: The vCPU to check.
232 * Checks whether the CPU has work to do.
234 * Returns: %true if the CPU has work, %false otherwise.
236 bool qemu_cpu_has_work(CPUState *cpu);
239 * qemu_cpu_is_self:
240 * @cpu: The vCPU to check against.
242 * Checks whether the caller is executing on the vCPU thread.
244 * Returns: %true if called from @cpu's thread, %false otherwise.
246 bool qemu_cpu_is_self(CPUState *cpu);
249 * qemu_cpu_kick:
250 * @cpu: The vCPU to kick.
252 * Kicks @cpu's thread.
254 void qemu_cpu_kick(CPUState *cpu);
257 * cpu_is_stopped:
258 * @cpu: The CPU to check.
260 * Checks whether the CPU is stopped.
262 * Returns: %true if run state is not running or if artificially stopped;
263 * %false otherwise.
265 bool cpu_is_stopped(CPUState *cpu);
268 * run_on_cpu:
269 * @cpu: The vCPU to run on.
270 * @func: The function to be executed.
271 * @data: Data to pass to the function.
273 * Schedules the function @func for execution on the vCPU @cpu.
275 void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data);
278 * qemu_for_each_cpu:
279 * @func: The function to be executed.
280 * @data: Data to pass to the function.
282 * Executes @func for each CPU.
284 void qemu_for_each_cpu(void (*func)(CPUState *cpu, void *data), void *data);
287 * qemu_get_cpu:
288 * @index: The CPUState@cpu_index value of the CPU to obtain.
290 * Gets a CPU matching @index.
292 * Returns: The CPU or %NULL if there is no matching CPU.
294 CPUState *qemu_get_cpu(int index);
297 * cpu_exists:
298 * @id: Guest-exposed CPU ID to lookup.
300 * Search for CPU with specified ID.
302 * Returns: %true - CPU is found, %false - CPU isn't found.
304 bool cpu_exists(int64_t id);
306 #ifndef CONFIG_USER_ONLY
308 typedef void (*CPUInterruptHandler)(CPUState *, int);
310 extern CPUInterruptHandler cpu_interrupt_handler;
313 * cpu_interrupt:
314 * @cpu: The CPU to set an interrupt on.
315 * @mask: The interupts to set.
317 * Invokes the interrupt handler.
319 static inline void cpu_interrupt(CPUState *cpu, int mask)
321 cpu_interrupt_handler(cpu, mask);
324 #else /* USER_ONLY */
326 void cpu_interrupt(CPUState *cpu, int mask);
328 #endif /* USER_ONLY */
331 * cpu_reset_interrupt:
332 * @cpu: The CPU to clear the interrupt on.
333 * @mask: The interrupt mask to clear.
335 * Resets interrupts on the vCPU @cpu.
337 void cpu_reset_interrupt(CPUState *cpu, int mask);
340 * cpu_resume:
341 * @cpu: The CPU to resume.
343 * Resumes CPU, i.e. puts CPU into runnable state.
345 void cpu_resume(CPUState *cpu);
347 #endif