]> git.proxmox.com Git - qemu.git/blob - include/qom/cpu.h
Merge remote-tracking branch 'cohuck/virtio-ccw-upstr' 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
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
40 typedef struct CPUState CPUState;
41
42 /**
43 * CPUClass:
44 * @class_by_name: Callback to map -cpu command line model name to an
45 * instantiatable CPU type.
46 * @reset: Callback to reset the #CPUState to its initial state.
47 * @do_interrupt: Callback for interrupt handling.
48 * @vmsd: State description for migration.
49 *
50 * Represents a CPU family or model.
51 */
52 typedef struct CPUClass {
53 /*< private >*/
54 DeviceClass parent_class;
55 /*< public >*/
56
57 ObjectClass *(*class_by_name)(const char *cpu_model);
58
59 void (*reset)(CPUState *cpu);
60 void (*do_interrupt)(CPUState *cpu);
61
62 const struct VMStateDescription *vmsd;
63 } CPUClass;
64
65 struct KVMState;
66 struct kvm_run;
67
68 /**
69 * CPUState:
70 * @cpu_index: CPU index (informative).
71 * @nr_cores: Number of cores within this CPU package.
72 * @nr_threads: Number of threads within this CPU.
73 * @numa_node: NUMA node this CPU is belonging to.
74 * @host_tid: Host thread ID.
75 * @running: #true if CPU is currently running (usermode).
76 * @created: Indicates whether the CPU thread has been successfully created.
77 * @interrupt_request: Indicates a pending interrupt request.
78 * @halted: Nonzero if the CPU is in suspended state.
79 * @stop: Indicates a pending stop request.
80 * @stopped: Indicates the CPU has been artificially stopped.
81 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
82 * CPU and return to its top level loop.
83 * @env_ptr: Pointer to subclass-specific CPUArchState field.
84 * @current_tb: Currently executing TB.
85 * @kvm_fd: vCPU file descriptor for KVM.
86 *
87 * State of one CPU core or thread.
88 */
89 struct CPUState {
90 /*< private >*/
91 DeviceState parent_obj;
92 /*< public >*/
93
94 int nr_cores;
95 int nr_threads;
96 int numa_node;
97
98 struct QemuThread *thread;
99 #ifdef _WIN32
100 HANDLE hThread;
101 #endif
102 int thread_id;
103 uint32_t host_tid;
104 bool running;
105 struct QemuCond *halt_cond;
106 struct qemu_work_item *queued_work_first, *queued_work_last;
107 bool thread_kicked;
108 bool created;
109 bool stop;
110 bool stopped;
111 volatile sig_atomic_t exit_request;
112 volatile sig_atomic_t tcg_exit_req;
113 uint32_t interrupt_request;
114
115 void *env_ptr; /* CPUArchState */
116 struct TranslationBlock *current_tb;
117
118 int kvm_fd;
119 bool kvm_vcpu_dirty;
120 struct KVMState *kvm_state;
121 struct kvm_run *kvm_run;
122
123 /* TODO Move common fields from CPUArchState here. */
124 int cpu_index; /* used by alpha TCG */
125 uint32_t halted; /* used by alpha, cris, ppc TCG */
126 };
127
128
129 /**
130 * cpu_reset:
131 * @cpu: The CPU whose state is to be reset.
132 */
133 void cpu_reset(CPUState *cpu);
134
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 */
144 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
145
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
158 static 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
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 */
175 bool qemu_cpu_has_work(CPUState *cpu);
176
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 */
185 bool qemu_cpu_is_self(CPUState *cpu);
186
187 /**
188 * qemu_cpu_kick:
189 * @cpu: The vCPU to kick.
190 *
191 * Kicks @cpu's thread.
192 */
193 void qemu_cpu_kick(CPUState *cpu);
194
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 */
204 bool cpu_is_stopped(CPUState *cpu);
205
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 */
214 void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data);
215
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 */
224 CPUState *qemu_get_cpu(int index);
225
226 #ifndef CONFIG_USER_ONLY
227
228 typedef void (*CPUInterruptHandler)(CPUState *, int);
229
230 extern 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 */
239 static inline void cpu_interrupt(CPUState *cpu, int mask)
240 {
241 cpu_interrupt_handler(cpu, mask);
242 }
243
244 #else /* USER_ONLY */
245
246 void cpu_interrupt(CPUState *cpu, int mask);
247
248 #endif /* USER_ONLY */
249
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 */
257 void cpu_reset_interrupt(CPUState *cpu, int mask);
258
259
260 #endif