]> git.proxmox.com Git - qemu.git/blame - include/qom/cpu.h
target-unicore32: Implement CPUClass::set_pc()
[qemu.git] / include / qom / cpu.h
CommitLineData
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"
c658b94f 25#include "exec/hwaddr.h"
1de7afc9 26#include "qemu/thread.h"
4917cf44 27#include "qemu/tls.h"
a23bbfda 28#include "qemu/typedefs.h"
dd83b06a 29
c72bf468
JF
30typedef int (*WriteCoreDumpFunction)(void *buf, size_t size, void *opaque);
31
577f42c0
AF
32/**
33 * vaddr:
34 * Type wide enough to contain any #target_ulong virtual address.
35 */
36typedef uint64_t vaddr;
37#define VADDR_PRId PRId64
38#define VADDR_PRIu PRIu64
39#define VADDR_PRIo PRIo64
40#define VADDR_PRIx PRIx64
41#define VADDR_PRIX PRIX64
42#define VADDR_MAX UINT64_MAX
43
dd83b06a
AF
44/**
45 * SECTION:cpu
46 * @section_id: QEMU-cpu
47 * @title: CPU Class
48 * @short_description: Base class for all CPUs
49 */
50
51#define TYPE_CPU "cpu"
52
53#define CPU(obj) OBJECT_CHECK(CPUState, (obj), TYPE_CPU)
54#define CPU_CLASS(class) OBJECT_CLASS_CHECK(CPUClass, (class), TYPE_CPU)
55#define CPU_GET_CLASS(obj) OBJECT_GET_CLASS(CPUClass, (obj), TYPE_CPU)
56
57typedef struct CPUState CPUState;
58
c658b94f
AF
59typedef void (*CPUUnassignedAccess)(CPUState *cpu, hwaddr addr,
60 bool is_write, bool is_exec, int opaque,
61 unsigned size);
62
dd83b06a
AF
63/**
64 * CPUClass:
2b8c2754
AF
65 * @class_by_name: Callback to map -cpu command line model name to an
66 * instantiatable CPU type.
f5df5baf 67 * @reset: Callback to reset the #CPUState to its initial state.
91b1df8c 68 * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
97a8ea5a 69 * @do_interrupt: Callback for interrupt handling.
c658b94f 70 * @do_unassigned_access: Callback for unassigned access handling.
878096ee
AF
71 * @dump_state: Callback for dumping state.
72 * @dump_statistics: Callback for dumping statistics.
997395d3 73 * @get_arch_id: Callback for getting architecture-dependent CPU ID.
444d5590 74 * @get_paging_enabled: Callback for inquiring whether paging is enabled.
a23bbfda 75 * @get_memory_mapping: Callback for obtaining the memory mappings.
f45748f1 76 * @set_pc: Callback for setting the Program Counter register.
b170fce3 77 * @vmsd: State description for migration.
dd83b06a
AF
78 *
79 * Represents a CPU family or model.
80 */
81typedef struct CPUClass {
82 /*< private >*/
961f8395 83 DeviceClass parent_class;
dd83b06a
AF
84 /*< public >*/
85
2b8c2754
AF
86 ObjectClass *(*class_by_name)(const char *cpu_model);
87
dd83b06a 88 void (*reset)(CPUState *cpu);
91b1df8c 89 int reset_dump_flags;
97a8ea5a 90 void (*do_interrupt)(CPUState *cpu);
c658b94f 91 CPUUnassignedAccess do_unassigned_access;
878096ee
AF
92 void (*dump_state)(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
93 int flags);
94 void (*dump_statistics)(CPUState *cpu, FILE *f,
95 fprintf_function cpu_fprintf, int flags);
997395d3 96 int64_t (*get_arch_id)(CPUState *cpu);
444d5590 97 bool (*get_paging_enabled)(const CPUState *cpu);
a23bbfda
AF
98 void (*get_memory_mapping)(CPUState *cpu, MemoryMappingList *list,
99 Error **errp);
f45748f1 100 void (*set_pc)(CPUState *cpu, vaddr value);
b170fce3
AF
101
102 const struct VMStateDescription *vmsd;
c72bf468
JF
103 int (*write_elf64_note)(WriteCoreDumpFunction f, CPUState *cpu,
104 int cpuid, void *opaque);
105 int (*write_elf64_qemunote)(WriteCoreDumpFunction f, CPUState *cpu,
106 void *opaque);
107 int (*write_elf32_note)(WriteCoreDumpFunction f, CPUState *cpu,
108 int cpuid, void *opaque);
109 int (*write_elf32_qemunote)(WriteCoreDumpFunction f, CPUState *cpu,
110 void *opaque);
dd83b06a
AF
111} CPUClass;
112
a60f24b5 113struct KVMState;
f7575c96 114struct kvm_run;
a60f24b5 115
dd83b06a
AF
116/**
117 * CPUState:
55e5c285 118 * @cpu_index: CPU index (informative).
ce3960eb
AF
119 * @nr_cores: Number of cores within this CPU package.
120 * @nr_threads: Number of threads within this CPU.
1b1ed8dc 121 * @numa_node: NUMA node this CPU is belonging to.
0d34282f 122 * @host_tid: Host thread ID.
0315c31c 123 * @running: #true if CPU is currently running (usermode).
61a46217 124 * @created: Indicates whether the CPU thread has been successfully created.
259186a7
AF
125 * @interrupt_request: Indicates a pending interrupt request.
126 * @halted: Nonzero if the CPU is in suspended state.
4fdeee7c 127 * @stop: Indicates a pending stop request.
f324e766 128 * @stopped: Indicates the CPU has been artificially stopped.
378df4b2
PM
129 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
130 * CPU and return to its top level loop.
c05efcb1 131 * @env_ptr: Pointer to subclass-specific CPUArchState field.
d77953b9 132 * @current_tb: Currently executing TB.
182735ef 133 * @next_cpu: Next CPU sharing TB cache.
8737c51c 134 * @kvm_fd: vCPU file descriptor for KVM.
dd83b06a
AF
135 *
136 * State of one CPU core or thread.
137 */
138struct CPUState {
139 /*< private >*/
961f8395 140 DeviceState parent_obj;
dd83b06a
AF
141 /*< public >*/
142
ce3960eb
AF
143 int nr_cores;
144 int nr_threads;
1b1ed8dc 145 int numa_node;
ce3960eb 146
814e612e 147 struct QemuThread *thread;
bcba2a72
AF
148#ifdef _WIN32
149 HANDLE hThread;
150#endif
9f09e18a 151 int thread_id;
0d34282f 152 uint32_t host_tid;
0315c31c 153 bool running;
f5c121b8 154 struct QemuCond *halt_cond;
c64ca814 155 struct qemu_work_item *queued_work_first, *queued_work_last;
216fc9a4 156 bool thread_kicked;
61a46217 157 bool created;
4fdeee7c 158 bool stop;
f324e766 159 bool stopped;
fcd7d003 160 volatile sig_atomic_t exit_request;
378df4b2 161 volatile sig_atomic_t tcg_exit_req;
259186a7 162 uint32_t interrupt_request;
bcba2a72 163
c05efcb1 164 void *env_ptr; /* CPUArchState */
d77953b9 165 struct TranslationBlock *current_tb;
182735ef 166 CPUState *next_cpu;
d77953b9 167
8737c51c 168 int kvm_fd;
20d695a9 169 bool kvm_vcpu_dirty;
a60f24b5 170 struct KVMState *kvm_state;
f7575c96 171 struct kvm_run *kvm_run;
8737c51c 172
f5df5baf 173 /* TODO Move common fields from CPUArchState here. */
55e5c285 174 int cpu_index; /* used by alpha TCG */
259186a7 175 uint32_t halted; /* used by alpha, cris, ppc TCG */
dd83b06a
AF
176};
177
182735ef
AF
178extern CPUState *first_cpu;
179
4917cf44
AF
180DECLARE_TLS(CPUState *, current_cpu);
181#define current_cpu tls_var(current_cpu)
182
444d5590
AF
183/**
184 * cpu_paging_enabled:
185 * @cpu: The CPU whose state is to be inspected.
186 *
187 * Returns: %true if paging is enabled, %false otherwise.
188 */
189bool cpu_paging_enabled(const CPUState *cpu);
190
a23bbfda
AF
191/**
192 * cpu_get_memory_mapping:
193 * @cpu: The CPU whose memory mappings are to be obtained.
194 * @list: Where to write the memory mappings to.
195 * @errp: Pointer for reporting an #Error.
196 */
197void cpu_get_memory_mapping(CPUState *cpu, MemoryMappingList *list,
198 Error **errp);
199
c72bf468
JF
200/**
201 * cpu_write_elf64_note:
202 * @f: pointer to a function that writes memory to a file
203 * @cpu: The CPU whose memory is to be dumped
204 * @cpuid: ID number of the CPU
205 * @opaque: pointer to the CPUState struct
206 */
207int cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cpu,
208 int cpuid, void *opaque);
209
210/**
211 * cpu_write_elf64_qemunote:
212 * @f: pointer to a function that writes memory to a file
213 * @cpu: The CPU whose memory is to be dumped
214 * @cpuid: ID number of the CPU
215 * @opaque: pointer to the CPUState struct
216 */
217int cpu_write_elf64_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
218 void *opaque);
219
220/**
221 * cpu_write_elf32_note:
222 * @f: pointer to a function that writes memory to a file
223 * @cpu: The CPU whose memory is to be dumped
224 * @cpuid: ID number of the CPU
225 * @opaque: pointer to the CPUState struct
226 */
227int cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cpu,
228 int cpuid, void *opaque);
229
230/**
231 * cpu_write_elf32_qemunote:
232 * @f: pointer to a function that writes memory to a file
233 * @cpu: The CPU whose memory is to be dumped
234 * @cpuid: ID number of the CPU
235 * @opaque: pointer to the CPUState struct
236 */
237int cpu_write_elf32_qemunote(WriteCoreDumpFunction f, CPUState *cpu,
238 void *opaque);
dd83b06a 239
878096ee
AF
240/**
241 * CPUDumpFlags:
242 * @CPU_DUMP_CODE:
243 * @CPU_DUMP_FPU: dump FPU register state, not just integer
244 * @CPU_DUMP_CCOP: dump info about TCG QEMU's condition code optimization state
245 */
246enum CPUDumpFlags {
247 CPU_DUMP_CODE = 0x00010000,
248 CPU_DUMP_FPU = 0x00020000,
249 CPU_DUMP_CCOP = 0x00040000,
250};
251
252/**
253 * cpu_dump_state:
254 * @cpu: The CPU whose state is to be dumped.
255 * @f: File to dump to.
256 * @cpu_fprintf: Function to dump with.
257 * @flags: Flags what to dump.
258 *
259 * Dumps CPU state.
260 */
261void cpu_dump_state(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
262 int flags);
263
264/**
265 * cpu_dump_statistics:
266 * @cpu: The CPU whose state is to be dumped.
267 * @f: File to dump to.
268 * @cpu_fprintf: Function to dump with.
269 * @flags: Flags what to dump.
270 *
271 * Dumps CPU statistics.
272 */
273void cpu_dump_statistics(CPUState *cpu, FILE *f, fprintf_function cpu_fprintf,
274 int flags);
275
dd83b06a
AF
276/**
277 * cpu_reset:
278 * @cpu: The CPU whose state is to be reset.
279 */
280void cpu_reset(CPUState *cpu);
281
2b8c2754
AF
282/**
283 * cpu_class_by_name:
284 * @typename: The CPU base type.
285 * @cpu_model: The model string without any parameters.
286 *
287 * Looks up a CPU #ObjectClass matching name @cpu_model.
288 *
289 * Returns: A #CPUClass or %NULL if not matching class is found.
290 */
291ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
292
ca91b15f
AF
293/**
294 * cpu_class_set_vmsd:
295 * @cc: CPU class
296 * @value: Value to set. Unused for %CONFIG_USER_ONLY.
297 *
298 * Sets #VMStateDescription for @cc.
299 *
300 * The @value argument is intentionally discarded for the non-softmmu targets
301 * to avoid linker errors or excessive preprocessor usage. If this behavior
76d5f029 302 * is undesired, you should assign #CPUClass.vmsd directly instead.
ca91b15f
AF
303 */
304#ifndef CONFIG_USER_ONLY
305static inline void cpu_class_set_vmsd(CPUClass *cc,
306 const struct VMStateDescription *value)
307{
308 cc->vmsd = value;
309}
310#else
311#define cpu_class_set_vmsd(cc, value) ((cc)->vmsd = NULL)
312#endif
313
c658b94f
AF
314#ifndef CONFIG_USER_ONLY
315static inline void cpu_class_set_do_unassigned_access(CPUClass *cc,
316 CPUUnassignedAccess value)
317{
318 cc->do_unassigned_access = value;
319}
320#else
321#define cpu_class_set_do_unassigned_access(cc, value) \
322 ((cc)->do_unassigned_access = NULL)
323#endif
324
19e3835c
AF
325/**
326 * device_class_set_vmsd:
327 * @dc: Device class
328 * @value: Value to set. Unused for %CONFIG_USER_ONLY.
329 *
330 * Sets #VMStateDescription for @dc.
331 *
332 * The @value argument is intentionally discarded for the non-softmmu targets
333 * to avoid linker errors or excessive preprocessor usage. If this behavior
334 * is undesired, you should assign #DeviceClass.vmsd directly instead.
335 */
336#ifndef CONFIG_USER_ONLY
337static inline void device_class_set_vmsd(DeviceClass *dc,
338 const struct VMStateDescription *value)
339{
340 dc->vmsd = value;
341}
342#else
343#define device_class_set_vmsd(dc, value) ((dc)->vmsd = NULL)
344#endif
345
3993c6bd
AF
346/**
347 * qemu_cpu_has_work:
348 * @cpu: The vCPU to check.
349 *
350 * Checks whether the CPU has work to do.
351 *
352 * Returns: %true if the CPU has work, %false otherwise.
353 */
354bool qemu_cpu_has_work(CPUState *cpu);
355
60e82579
AF
356/**
357 * qemu_cpu_is_self:
358 * @cpu: The vCPU to check against.
359 *
360 * Checks whether the caller is executing on the vCPU thread.
361 *
362 * Returns: %true if called from @cpu's thread, %false otherwise.
363 */
364bool qemu_cpu_is_self(CPUState *cpu);
365
c08d7424
AF
366/**
367 * qemu_cpu_kick:
368 * @cpu: The vCPU to kick.
369 *
370 * Kicks @cpu's thread.
371 */
372void qemu_cpu_kick(CPUState *cpu);
373
2fa45344
AF
374/**
375 * cpu_is_stopped:
376 * @cpu: The CPU to check.
377 *
378 * Checks whether the CPU is stopped.
379 *
380 * Returns: %true if run state is not running or if artificially stopped;
381 * %false otherwise.
382 */
383bool cpu_is_stopped(CPUState *cpu);
384
f100f0b3
AF
385/**
386 * run_on_cpu:
387 * @cpu: The vCPU to run on.
388 * @func: The function to be executed.
389 * @data: Data to pass to the function.
390 *
391 * Schedules the function @func for execution on the vCPU @cpu.
392 */
393void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data);
394
3c02270d
CV
395/**
396 * async_run_on_cpu:
397 * @cpu: The vCPU to run on.
398 * @func: The function to be executed.
399 * @data: Data to pass to the function.
400 *
401 * Schedules the function @func for execution on the vCPU @cpu asynchronously.
402 */
403void async_run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data);
404
d6b9e0d6
MT
405/**
406 * qemu_for_each_cpu:
407 * @func: The function to be executed.
408 * @data: Data to pass to the function.
409 *
410 * Executes @func for each CPU.
411 */
412void qemu_for_each_cpu(void (*func)(CPUState *cpu, void *data), void *data);
413
38d8f5c8
AF
414/**
415 * qemu_get_cpu:
416 * @index: The CPUState@cpu_index value of the CPU to obtain.
417 *
418 * Gets a CPU matching @index.
419 *
420 * Returns: The CPU or %NULL if there is no matching CPU.
421 */
422CPUState *qemu_get_cpu(int index);
423
69e5ff06
IM
424/**
425 * cpu_exists:
426 * @id: Guest-exposed CPU ID to lookup.
427 *
428 * Search for CPU with specified ID.
429 *
430 * Returns: %true - CPU is found, %false - CPU isn't found.
431 */
432bool cpu_exists(int64_t id);
433
c3affe56
AF
434#ifndef CONFIG_USER_ONLY
435
436typedef void (*CPUInterruptHandler)(CPUState *, int);
437
438extern CPUInterruptHandler cpu_interrupt_handler;
439
440/**
441 * cpu_interrupt:
442 * @cpu: The CPU to set an interrupt on.
443 * @mask: The interupts to set.
444 *
445 * Invokes the interrupt handler.
446 */
447static inline void cpu_interrupt(CPUState *cpu, int mask)
448{
449 cpu_interrupt_handler(cpu, mask);
450}
451
452#else /* USER_ONLY */
453
454void cpu_interrupt(CPUState *cpu, int mask);
455
456#endif /* USER_ONLY */
457
c658b94f
AF
458#ifndef CONFIG_USER_ONLY
459
460static inline void cpu_unassigned_access(CPUState *cpu, hwaddr addr,
461 bool is_write, bool is_exec,
462 int opaque, unsigned size)
463{
464 CPUClass *cc = CPU_GET_CLASS(cpu);
465
466 if (cc->do_unassigned_access) {
467 cc->do_unassigned_access(cpu, addr, is_write, is_exec, opaque, size);
468 }
469}
470
471#endif
472
d8ed887b
AF
473/**
474 * cpu_reset_interrupt:
475 * @cpu: The CPU to clear the interrupt on.
476 * @mask: The interrupt mask to clear.
477 *
478 * Resets interrupts on the vCPU @cpu.
479 */
480void cpu_reset_interrupt(CPUState *cpu, int mask);
481
60a3e17a
AF
482/**
483 * cpu_exit:
484 * @cpu: The CPU to exit.
485 *
486 * Requests the CPU @cpu to exit execution.
487 */
488void cpu_exit(CPUState *cpu);
489
2993683b
IM
490/**
491 * cpu_resume:
492 * @cpu: The CPU to resume.
493 *
494 * Resumes CPU, i.e. puts CPU into runnable state.
495 */
496void cpu_resume(CPUState *cpu);
dd83b06a 497
c643bed9
AF
498/**
499 * qemu_init_vcpu:
500 * @cpu: The vCPU to initialize.
501 *
502 * Initializes a vCPU.
503 */
504void qemu_init_vcpu(CPUState *cpu);
505
1a1562f5
AF
506#ifdef CONFIG_SOFTMMU
507extern const struct VMStateDescription vmstate_cpu_common;
508#else
509#define vmstate_cpu_common vmstate_dummy
510#endif
511
512#define VMSTATE_CPU() { \
513 .name = "parent_obj", \
514 .size = sizeof(CPUState), \
515 .vmsd = &vmstate_cpu_common, \
516 .flags = VMS_STRUCT, \
517 .offset = 0, \
518}
519
dd83b06a 520#endif