]> git.proxmox.com Git - qemu.git/blame - include/qom/cpu.h
cpu: Call cpu_synchronize_post_init() from DeviceClass::realize()
[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"
1de7afc9 25#include "qemu/thread.h"
dd83b06a
AF
26
27/**
28 * SECTION:cpu
29 * @section_id: QEMU-cpu
30 * @title: CPU Class
31 * @short_description: Base class for all CPUs
32 */
33
34#define TYPE_CPU "cpu"
35
36#define CPU(obj) OBJECT_CHECK(CPUState, (obj), TYPE_CPU)
37#define CPU_CLASS(class) OBJECT_CLASS_CHECK(CPUClass, (class), TYPE_CPU)
38#define CPU_GET_CLASS(obj) OBJECT_GET_CLASS(CPUClass, (obj), TYPE_CPU)
39
40typedef struct CPUState CPUState;
41
42/**
43 * CPUClass:
2b8c2754
AF
44 * @class_by_name: Callback to map -cpu command line model name to an
45 * instantiatable CPU type.
f5df5baf 46 * @reset: Callback to reset the #CPUState to its initial state.
97a8ea5a 47 * @do_interrupt: Callback for interrupt handling.
b170fce3 48 * @vmsd: State description for migration.
dd83b06a
AF
49 *
50 * Represents a CPU family or model.
51 */
52typedef struct CPUClass {
53 /*< private >*/
961f8395 54 DeviceClass parent_class;
dd83b06a
AF
55 /*< public >*/
56
2b8c2754
AF
57 ObjectClass *(*class_by_name)(const char *cpu_model);
58
dd83b06a 59 void (*reset)(CPUState *cpu);
97a8ea5a 60 void (*do_interrupt)(CPUState *cpu);
b170fce3
AF
61
62 const struct VMStateDescription *vmsd;
dd83b06a
AF
63} CPUClass;
64
a60f24b5 65struct KVMState;
f7575c96 66struct kvm_run;
a60f24b5 67
dd83b06a
AF
68/**
69 * CPUState:
55e5c285 70 * @cpu_index: CPU index (informative).
ce3960eb
AF
71 * @nr_cores: Number of cores within this CPU package.
72 * @nr_threads: Number of threads within this CPU.
1b1ed8dc 73 * @numa_node: NUMA node this CPU is belonging to.
0d34282f 74 * @host_tid: Host thread ID.
0315c31c 75 * @running: #true if CPU is currently running (usermode).
61a46217 76 * @created: Indicates whether the CPU thread has been successfully created.
259186a7
AF
77 * @interrupt_request: Indicates a pending interrupt request.
78 * @halted: Nonzero if the CPU is in suspended state.
4fdeee7c 79 * @stop: Indicates a pending stop request.
f324e766 80 * @stopped: Indicates the CPU has been artificially stopped.
378df4b2
PM
81 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
82 * CPU and return to its top level loop.
c05efcb1 83 * @env_ptr: Pointer to subclass-specific CPUArchState field.
d77953b9 84 * @current_tb: Currently executing TB.
8737c51c 85 * @kvm_fd: vCPU file descriptor for KVM.
dd83b06a
AF
86 *
87 * State of one CPU core or thread.
88 */
89struct CPUState {
90 /*< private >*/
961f8395 91 DeviceState parent_obj;
dd83b06a
AF
92 /*< public >*/
93
ce3960eb
AF
94 int nr_cores;
95 int nr_threads;
1b1ed8dc 96 int numa_node;
ce3960eb 97
814e612e 98 struct QemuThread *thread;
bcba2a72
AF
99#ifdef _WIN32
100 HANDLE hThread;
101#endif
9f09e18a 102 int thread_id;
0d34282f 103 uint32_t host_tid;
0315c31c 104 bool running;
f5c121b8 105 struct QemuCond *halt_cond;
c64ca814 106 struct qemu_work_item *queued_work_first, *queued_work_last;
216fc9a4 107 bool thread_kicked;
61a46217 108 bool created;
4fdeee7c 109 bool stop;
f324e766 110 bool stopped;
fcd7d003 111 volatile sig_atomic_t exit_request;
378df4b2 112 volatile sig_atomic_t tcg_exit_req;
259186a7 113 uint32_t interrupt_request;
bcba2a72 114
c05efcb1 115 void *env_ptr; /* CPUArchState */
d77953b9
AF
116 struct TranslationBlock *current_tb;
117
8737c51c 118 int kvm_fd;
20d695a9 119 bool kvm_vcpu_dirty;
a60f24b5 120 struct KVMState *kvm_state;
f7575c96 121 struct kvm_run *kvm_run;
8737c51c 122
f5df5baf 123 /* TODO Move common fields from CPUArchState here. */
55e5c285 124 int cpu_index; /* used by alpha TCG */
259186a7 125 uint32_t halted; /* used by alpha, cris, ppc TCG */
dd83b06a
AF
126};
127
128
129/**
130 * cpu_reset:
131 * @cpu: The CPU whose state is to be reset.
132 */
133void cpu_reset(CPUState *cpu);
134
2b8c2754
AF
135/**
136 * cpu_class_by_name:
137 * @typename: The CPU base type.
138 * @cpu_model: The model string without any parameters.
139 *
140 * Looks up a CPU #ObjectClass matching name @cpu_model.
141 *
142 * Returns: A #CPUClass or %NULL if not matching class is found.
143 */
144ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
145
ca91b15f
AF
146/**
147 * cpu_class_set_vmsd:
148 * @cc: CPU class
149 * @value: Value to set. Unused for %CONFIG_USER_ONLY.
150 *
151 * Sets #VMStateDescription for @cc.
152 *
153 * The @value argument is intentionally discarded for the non-softmmu targets
154 * to avoid linker errors or excessive preprocessor usage. If this behavior
155 * is undesired, you should assign #CPUState.vmsd directly instead.
156 */
157#ifndef CONFIG_USER_ONLY
158static inline void cpu_class_set_vmsd(CPUClass *cc,
159 const struct VMStateDescription *value)
160{
161 cc->vmsd = value;
162}
163#else
164#define cpu_class_set_vmsd(cc, value) ((cc)->vmsd = NULL)
165#endif
166
3993c6bd
AF
167/**
168 * qemu_cpu_has_work:
169 * @cpu: The vCPU to check.
170 *
171 * Checks whether the CPU has work to do.
172 *
173 * Returns: %true if the CPU has work, %false otherwise.
174 */
175bool qemu_cpu_has_work(CPUState *cpu);
176
60e82579
AF
177/**
178 * qemu_cpu_is_self:
179 * @cpu: The vCPU to check against.
180 *
181 * Checks whether the caller is executing on the vCPU thread.
182 *
183 * Returns: %true if called from @cpu's thread, %false otherwise.
184 */
185bool qemu_cpu_is_self(CPUState *cpu);
186
c08d7424
AF
187/**
188 * qemu_cpu_kick:
189 * @cpu: The vCPU to kick.
190 *
191 * Kicks @cpu's thread.
192 */
193void qemu_cpu_kick(CPUState *cpu);
194
2fa45344
AF
195/**
196 * cpu_is_stopped:
197 * @cpu: The CPU to check.
198 *
199 * Checks whether the CPU is stopped.
200 *
201 * Returns: %true if run state is not running or if artificially stopped;
202 * %false otherwise.
203 */
204bool cpu_is_stopped(CPUState *cpu);
205
f100f0b3
AF
206/**
207 * run_on_cpu:
208 * @cpu: The vCPU to run on.
209 * @func: The function to be executed.
210 * @data: Data to pass to the function.
211 *
212 * Schedules the function @func for execution on the vCPU @cpu.
213 */
214void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data);
215
38d8f5c8
AF
216/**
217 * qemu_get_cpu:
218 * @index: The CPUState@cpu_index value of the CPU to obtain.
219 *
220 * Gets a CPU matching @index.
221 *
222 * Returns: The CPU or %NULL if there is no matching CPU.
223 */
224CPUState *qemu_get_cpu(int index);
225
c3affe56
AF
226#ifndef CONFIG_USER_ONLY
227
228typedef void (*CPUInterruptHandler)(CPUState *, int);
229
230extern CPUInterruptHandler cpu_interrupt_handler;
231
232/**
233 * cpu_interrupt:
234 * @cpu: The CPU to set an interrupt on.
235 * @mask: The interupts to set.
236 *
237 * Invokes the interrupt handler.
238 */
239static inline void cpu_interrupt(CPUState *cpu, int mask)
240{
241 cpu_interrupt_handler(cpu, mask);
242}
243
244#else /* USER_ONLY */
245
246void cpu_interrupt(CPUState *cpu, int mask);
247
248#endif /* USER_ONLY */
249
d8ed887b
AF
250/**
251 * cpu_reset_interrupt:
252 * @cpu: The CPU to clear the interrupt on.
253 * @mask: The interrupt mask to clear.
254 *
255 * Resets interrupts on the vCPU @cpu.
256 */
257void cpu_reset_interrupt(CPUState *cpu, int mask);
258
dd83b06a
AF
259
260#endif