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