]> git.proxmox.com Git - qemu.git/blob - include/qom/cpu.h
Merge remote-tracking branch 'mjt/trivial-patches-next' into staging
[qemu.git] / include / qom / cpu.h
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
23 #include <signal.h>
24 #include "hw/qdev-core.h"
25 #include "qemu/thread.h"
26 #include "qemu/typedefs.h"
27
28 typedef int (*WriteCoreDumpFunction)(void *buf, size_t size, void *opaque);
29
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:
47 * @class_by_name: Callback to map -cpu command line model name to an
48 * instantiatable CPU type.
49 * @reset: Callback to reset the #CPUState to its initial state.
50 * @do_interrupt: Callback for interrupt handling.
51 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
52 * @get_paging_enabled: Callback for inquiring whether paging is enabled.
53 * @get_memory_mapping: Callback for obtaining the memory mappings.
54 * @vmsd: State description for migration.
55 *
56 * Represents a CPU family or model.
57 */
58 typedef struct CPUClass {
59 /*< private >*/
60 DeviceClass parent_class;
61 /*< public >*/
62
63 ObjectClass *(*class_by_name)(const char *cpu_model);
64
65 void (*reset)(CPUState *cpu);
66 void (*do_interrupt)(CPUState *cpu);
67 int64_t (*get_arch_id)(CPUState *cpu);
68 bool (*get_paging_enabled)(const CPUState *cpu);
69 void (*get_memory_mapping)(CPUState *cpu, MemoryMappingList *list,
70 Error **errp);
71
72 const struct VMStateDescription *vmsd;
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);
81 } CPUClass;
82
83 struct KVMState;
84 struct kvm_run;
85
86 /**
87 * CPUState:
88 * @cpu_index: CPU index (informative).
89 * @nr_cores: Number of cores within this CPU package.
90 * @nr_threads: Number of threads within this CPU.
91 * @numa_node: NUMA node this CPU is belonging to.
92 * @host_tid: Host thread ID.
93 * @running: #true if CPU is currently running (usermode).
94 * @created: Indicates whether the CPU thread has been successfully created.
95 * @interrupt_request: Indicates a pending interrupt request.
96 * @halted: Nonzero if the CPU is in suspended state.
97 * @stop: Indicates a pending stop request.
98 * @stopped: Indicates the CPU has been artificially stopped.
99 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
100 * CPU and return to its top level loop.
101 * @env_ptr: Pointer to subclass-specific CPUArchState field.
102 * @current_tb: Currently executing TB.
103 * @kvm_fd: vCPU file descriptor for KVM.
104 *
105 * State of one CPU core or thread.
106 */
107 struct CPUState {
108 /*< private >*/
109 DeviceState parent_obj;
110 /*< public >*/
111
112 int nr_cores;
113 int nr_threads;
114 int numa_node;
115
116 struct QemuThread *thread;
117 #ifdef _WIN32
118 HANDLE hThread;
119 #endif
120 int thread_id;
121 uint32_t host_tid;
122 bool running;
123 struct QemuCond *halt_cond;
124 struct qemu_work_item *queued_work_first, *queued_work_last;
125 bool thread_kicked;
126 bool created;
127 bool stop;
128 bool stopped;
129 volatile sig_atomic_t exit_request;
130 volatile sig_atomic_t tcg_exit_req;
131 uint32_t interrupt_request;
132
133 void *env_ptr; /* CPUArchState */
134 struct TranslationBlock *current_tb;
135
136 int kvm_fd;
137 bool kvm_vcpu_dirty;
138 struct KVMState *kvm_state;
139 struct kvm_run *kvm_run;
140
141 /* TODO Move common fields from CPUArchState here. */
142 int cpu_index; /* used by alpha TCG */
143 uint32_t halted; /* used by alpha, cris, ppc TCG */
144 };
145
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
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
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);
202
203 /**
204 * cpu_reset:
205 * @cpu: The CPU whose state is to be reset.
206 */
207 void cpu_reset(CPUState *cpu);
208
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
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
229 * is undesired, you should assign #CPUState.vmsd directly instead.
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
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
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
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
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
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
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
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
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
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
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
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);
359
360 #endif