]>
Commit | Line | Data |
---|---|---|
dd83b06a AF |
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 | ||
fcd7d003 | 23 | #include <signal.h> |
961f8395 | 24 | #include "hw/qdev-core.h" |
1de7afc9 | 25 | #include "qemu/thread.h" |
a23bbfda | 26 | #include "qemu/typedefs.h" |
dd83b06a | 27 | |
c72bf468 JF |
28 | typedef int (*WriteCoreDumpFunction)(void *buf, size_t size, void *opaque); |
29 | ||
dd83b06a AF |
30 | /** |
31 | * SECTION:cpu | |
32 | * @section_id: QEMU-cpu | |
33 | * @title: CPU Class | |
34 | * @short_description: Base class for all CPUs | |
35 | */ | |
36 | ||
37 | #define TYPE_CPU "cpu" | |
38 | ||
39 | #define CPU(obj) OBJECT_CHECK(CPUState, (obj), TYPE_CPU) | |
40 | #define CPU_CLASS(class) OBJECT_CLASS_CHECK(CPUClass, (class), TYPE_CPU) | |
41 | #define CPU_GET_CLASS(obj) OBJECT_GET_CLASS(CPUClass, (obj), TYPE_CPU) | |
42 | ||
43 | typedef struct CPUState CPUState; | |
44 | ||
45 | /** | |
46 | * CPUClass: | |
2b8c2754 AF |
47 | * @class_by_name: Callback to map -cpu command line model name to an |
48 | * instantiatable CPU type. | |
f5df5baf | 49 | * @reset: Callback to reset the #CPUState to its initial state. |
97a8ea5a | 50 | * @do_interrupt: Callback for interrupt handling. |
997395d3 | 51 | * @get_arch_id: Callback for getting architecture-dependent CPU ID. |
444d5590 | 52 | * @get_paging_enabled: Callback for inquiring whether paging is enabled. |
a23bbfda | 53 | * @get_memory_mapping: Callback for obtaining the memory mappings. |
b170fce3 | 54 | * @vmsd: State description for migration. |
dd83b06a AF |
55 | * |
56 | * Represents a CPU family or model. | |
57 | */ | |
58 | typedef struct CPUClass { | |
59 | /*< private >*/ | |
961f8395 | 60 | DeviceClass parent_class; |
dd83b06a AF |
61 | /*< public >*/ |
62 | ||
2b8c2754 AF |
63 | ObjectClass *(*class_by_name)(const char *cpu_model); |
64 | ||
dd83b06a | 65 | void (*reset)(CPUState *cpu); |
97a8ea5a | 66 | void (*do_interrupt)(CPUState *cpu); |
997395d3 | 67 | int64_t (*get_arch_id)(CPUState *cpu); |
444d5590 | 68 | bool (*get_paging_enabled)(const CPUState *cpu); |
a23bbfda AF |
69 | void (*get_memory_mapping)(CPUState *cpu, MemoryMappingList *list, |
70 | Error **errp); | |
b170fce3 AF |
71 | |
72 | const struct VMStateDescription *vmsd; | |
c72bf468 JF |
73 | int (*write_elf64_note)(WriteCoreDumpFunction f, CPUState *cpu, |
74 | int cpuid, void *opaque); | |
75 | int (*write_elf64_qemunote)(WriteCoreDumpFunction f, CPUState *cpu, | |
76 | void *opaque); | |
77 | int (*write_elf32_note)(WriteCoreDumpFunction f, CPUState *cpu, | |
78 | int cpuid, void *opaque); | |
79 | int (*write_elf32_qemunote)(WriteCoreDumpFunction f, CPUState *cpu, | |
80 | void *opaque); | |
dd83b06a AF |
81 | } CPUClass; |
82 | ||
a60f24b5 | 83 | struct KVMState; |
f7575c96 | 84 | struct kvm_run; |
a60f24b5 | 85 | |
dd83b06a AF |
86 | /** |
87 | * CPUState: | |
55e5c285 | 88 | * @cpu_index: CPU index (informative). |
ce3960eb AF |
89 | * @nr_cores: Number of cores within this CPU package. |
90 | * @nr_threads: Number of threads within this CPU. | |
1b1ed8dc | 91 | * @numa_node: NUMA node this CPU is belonging to. |
0d34282f | 92 | * @host_tid: Host thread ID. |
0315c31c | 93 | * @running: #true if CPU is currently running (usermode). |
61a46217 | 94 | * @created: Indicates whether the CPU thread has been successfully created. |
259186a7 AF |
95 | * @interrupt_request: Indicates a pending interrupt request. |
96 | * @halted: Nonzero if the CPU is in suspended state. | |
4fdeee7c | 97 | * @stop: Indicates a pending stop request. |
f324e766 | 98 | * @stopped: Indicates the CPU has been artificially stopped. |
378df4b2 PM |
99 | * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this |
100 | * CPU and return to its top level loop. | |
c05efcb1 | 101 | * @env_ptr: Pointer to subclass-specific CPUArchState field. |
d77953b9 | 102 | * @current_tb: Currently executing TB. |
8737c51c | 103 | * @kvm_fd: vCPU file descriptor for KVM. |
dd83b06a AF |
104 | * |
105 | * State of one CPU core or thread. | |
106 | */ | |
107 | struct CPUState { | |
108 | /*< private >*/ | |
961f8395 | 109 | DeviceState parent_obj; |
dd83b06a AF |
110 | /*< public >*/ |
111 | ||
ce3960eb AF |
112 | int nr_cores; |
113 | int nr_threads; | |
1b1ed8dc | 114 | int numa_node; |
ce3960eb | 115 | |
814e612e | 116 | struct QemuThread *thread; |
bcba2a72 AF |
117 | #ifdef _WIN32 |
118 | HANDLE hThread; | |
119 | #endif | |
9f09e18a | 120 | int thread_id; |
0d34282f | 121 | uint32_t host_tid; |
0315c31c | 122 | bool running; |
f5c121b8 | 123 | struct QemuCond *halt_cond; |
c64ca814 | 124 | struct qemu_work_item *queued_work_first, *queued_work_last; |
216fc9a4 | 125 | bool thread_kicked; |
61a46217 | 126 | bool created; |
4fdeee7c | 127 | bool stop; |
f324e766 | 128 | bool stopped; |
fcd7d003 | 129 | volatile sig_atomic_t exit_request; |
378df4b2 | 130 | volatile sig_atomic_t tcg_exit_req; |
259186a7 | 131 | uint32_t interrupt_request; |
bcba2a72 | 132 | |
c05efcb1 | 133 | void *env_ptr; /* CPUArchState */ |
d77953b9 AF |
134 | struct TranslationBlock *current_tb; |
135 | ||
8737c51c | 136 | int kvm_fd; |
20d695a9 | 137 | bool kvm_vcpu_dirty; |
a60f24b5 | 138 | struct KVMState *kvm_state; |
f7575c96 | 139 | struct kvm_run *kvm_run; |
8737c51c | 140 | |
f5df5baf | 141 | /* TODO Move common fields from CPUArchState here. */ |
55e5c285 | 142 | int cpu_index; /* used by alpha TCG */ |
259186a7 | 143 | uint32_t halted; /* used by alpha, cris, ppc TCG */ |
dd83b06a AF |
144 | }; |
145 | ||
444d5590 AF |
146 | /** |
147 | * cpu_paging_enabled: | |
148 | * @cpu: The CPU whose state is to be inspected. | |
149 | * | |
150 | * Returns: %true if paging is enabled, %false otherwise. | |
151 | */ | |
152 | bool cpu_paging_enabled(const CPUState *cpu); | |
153 | ||
a23bbfda AF |
154 | /** |
155 | * cpu_get_memory_mapping: | |
156 | * @cpu: The CPU whose memory mappings are to be obtained. | |
157 | * @list: Where to write the memory mappings to. | |
158 | * @errp: Pointer for reporting an #Error. | |
159 | */ | |
160 | void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list, | |
161 | Error **errp); | |
162 | ||
c72bf468 JF |
163 | /** |
164 | * cpu_write_elf64_note: | |
165 | * @f: pointer to a function that writes memory to a file | |
166 | * @cpu: The CPU whose memory is to be dumped | |
167 | * @cpuid: ID number of the CPU | |
168 | * @opaque: pointer to the CPUState struct | |
169 | */ | |
170 | int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu, | |
171 | int cpuid, void *opaque); | |
172 | ||
173 | /** | |
174 | * cpu_write_elf64_qemunote: | |
175 | * @f: pointer to a function that writes memory to a file | |
176 | * @cpu: The CPU whose memory is to be dumped | |
177 | * @cpuid: ID number of the CPU | |
178 | * @opaque: pointer to the CPUState struct | |
179 | */ | |
180 | int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu, | |
181 | void *opaque); | |
182 | ||
183 | /** | |
184 | * cpu_write_elf32_note: | |
185 | * @f: pointer to a function that writes memory to a file | |
186 | * @cpu: The CPU whose memory is to be dumped | |
187 | * @cpuid: ID number of the CPU | |
188 | * @opaque: pointer to the CPUState struct | |
189 | */ | |
190 | int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu, | |
191 | int cpuid, void *opaque); | |
192 | ||
193 | /** | |
194 | * cpu_write_elf32_qemunote: | |
195 | * @f: pointer to a function that writes memory to a file | |
196 | * @cpu: The CPU whose memory is to be dumped | |
197 | * @cpuid: ID number of the CPU | |
198 | * @opaque: pointer to the CPUState struct | |
199 | */ | |
200 | int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu, | |
201 | void *opaque); | |
dd83b06a AF |
202 | |
203 | /** | |
204 | * cpu_reset: | |
205 | * @cpu: The CPU whose state is to be reset. | |
206 | */ | |
207 | void cpu_reset(CPUState *cpu); | |
208 | ||
2b8c2754 AF |
209 | /** |
210 | * cpu_class_by_name: | |
211 | * @typename: The CPU base type. | |
212 | * @cpu_model: The model string without any parameters. | |
213 | * | |
214 | * Looks up a CPU #ObjectClass matching name @cpu_model. | |
215 | * | |
216 | * Returns: A #CPUClass or %NULL if not matching class is found. | |
217 | */ | |
218 | ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model); | |
219 | ||
ca91b15f AF |
220 | /** |
221 | * cpu_class_set_vmsd: | |
222 | * @cc: CPU class | |
223 | * @value: Value to set. Unused for %CONFIG_USER_ONLY. | |
224 | * | |
225 | * Sets #VMStateDescription for @cc. | |
226 | * | |
227 | * The @value argument is intentionally discarded for the non-softmmu targets | |
228 | * to avoid linker errors or excessive preprocessor usage. If this behavior | |
76d5f029 | 229 | * is undesired, you should assign #CPUClass.vmsd directly instead. |
ca91b15f AF |
230 | */ |
231 | #ifndef CONFIG_USER_ONLY | |
232 | static inline void cpu_class_set_vmsd(CPUClass *cc, | |
233 | const struct VMStateDescription *value) | |
234 | { | |
235 | cc->vmsd = value; | |
236 | } | |
237 | #else | |
238 | #define cpu_class_set_vmsd(cc, value) ((cc)->vmsd = NULL) | |
239 | #endif | |
240 | ||
3993c6bd AF |
241 | /** |
242 | * qemu_cpu_has_work: | |
243 | * @cpu: The vCPU to check. | |
244 | * | |
245 | * Checks whether the CPU has work to do. | |
246 | * | |
247 | * Returns: %true if the CPU has work, %false otherwise. | |
248 | */ | |
249 | bool qemu_cpu_has_work(CPUState *cpu); | |
250 | ||
60e82579 AF |
251 | /** |
252 | * qemu_cpu_is_self: | |
253 | * @cpu: The vCPU to check against. | |
254 | * | |
255 | * Checks whether the caller is executing on the vCPU thread. | |
256 | * | |
257 | * Returns: %true if called from @cpu's thread, %false otherwise. | |
258 | */ | |
259 | bool qemu_cpu_is_self(CPUState *cpu); | |
260 | ||
c08d7424 AF |
261 | /** |
262 | * qemu_cpu_kick: | |
263 | * @cpu: The vCPU to kick. | |
264 | * | |
265 | * Kicks @cpu's thread. | |
266 | */ | |
267 | void qemu_cpu_kick(CPUState *cpu); | |
268 | ||
2fa45344 AF |
269 | /** |
270 | * cpu_is_stopped: | |
271 | * @cpu: The CPU to check. | |
272 | * | |
273 | * Checks whether the CPU is stopped. | |
274 | * | |
275 | * Returns: %true if run state is not running or if artificially stopped; | |
276 | * %false otherwise. | |
277 | */ | |
278 | bool cpu_is_stopped(CPUState *cpu); | |
279 | ||
f100f0b3 AF |
280 | /** |
281 | * run_on_cpu: | |
282 | * @cpu: The vCPU to run on. | |
283 | * @func: The function to be executed. | |
284 | * @data: Data to pass to the function. | |
285 | * | |
286 | * Schedules the function @func for execution on the vCPU @cpu. | |
287 | */ | |
288 | void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data); | |
289 | ||
d6b9e0d6 MT |
290 | /** |
291 | * qemu_for_each_cpu: | |
292 | * @func: The function to be executed. | |
293 | * @data: Data to pass to the function. | |
294 | * | |
295 | * Executes @func for each CPU. | |
296 | */ | |
297 | void qemu_for_each_cpu(void (*func)(CPUState *cpu, void *data), void *data); | |
298 | ||
38d8f5c8 AF |
299 | /** |
300 | * qemu_get_cpu: | |
301 | * @index: The CPUState@cpu_index value of the CPU to obtain. | |
302 | * | |
303 | * Gets a CPU matching @index. | |
304 | * | |
305 | * Returns: The CPU or %NULL if there is no matching CPU. | |
306 | */ | |
307 | CPUState *qemu_get_cpu(int index); | |
308 | ||
69e5ff06 IM |
309 | /** |
310 | * cpu_exists: | |
311 | * @id: Guest-exposed CPU ID to lookup. | |
312 | * | |
313 | * Search for CPU with specified ID. | |
314 | * | |
315 | * Returns: %true - CPU is found, %false - CPU isn't found. | |
316 | */ | |
317 | bool cpu_exists(int64_t id); | |
318 | ||
c3affe56 AF |
319 | #ifndef CONFIG_USER_ONLY |
320 | ||
321 | typedef void (*CPUInterruptHandler)(CPUState *, int); | |
322 | ||
323 | extern CPUInterruptHandler cpu_interrupt_handler; | |
324 | ||
325 | /** | |
326 | * cpu_interrupt: | |
327 | * @cpu: The CPU to set an interrupt on. | |
328 | * @mask: The interupts to set. | |
329 | * | |
330 | * Invokes the interrupt handler. | |
331 | */ | |
332 | static inline void cpu_interrupt(CPUState *cpu, int mask) | |
333 | { | |
334 | cpu_interrupt_handler(cpu, mask); | |
335 | } | |
336 | ||
337 | #else /* USER_ONLY */ | |
338 | ||
339 | void cpu_interrupt(CPUState *cpu, int mask); | |
340 | ||
341 | #endif /* USER_ONLY */ | |
342 | ||
d8ed887b AF |
343 | /** |
344 | * cpu_reset_interrupt: | |
345 | * @cpu: The CPU to clear the interrupt on. | |
346 | * @mask: The interrupt mask to clear. | |
347 | * | |
348 | * Resets interrupts on the vCPU @cpu. | |
349 | */ | |
350 | void cpu_reset_interrupt(CPUState *cpu, int mask); | |
351 | ||
2993683b IM |
352 | /** |
353 | * cpu_resume: | |
354 | * @cpu: The CPU to resume. | |
355 | * | |
356 | * Resumes CPU, i.e. puts CPU into runnable state. | |
357 | */ | |
358 | void cpu_resume(CPUState *cpu); | |
dd83b06a AF |
359 | |
360 | #endif |