]> git.proxmox.com Git - mirror_qemu.git/blob - include/qom/cpu.h
cpu: Introduce cpu_class_set_vmsd()
[mirror_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 * @vmsd: State description for migration.
48 *
49 * Represents a CPU family or model.
50 */
51 typedef struct CPUClass {
52 /*< private >*/
53 DeviceClass parent_class;
54 /*< public >*/
55
56 ObjectClass *(*class_by_name)(const char *cpu_model);
57
58 void (*reset)(CPUState *cpu);
59
60 const struct VMStateDescription *vmsd;
61 } CPUClass;
62
63 struct KVMState;
64 struct kvm_run;
65
66 /**
67 * CPUState:
68 * @cpu_index: CPU index (informative).
69 * @nr_cores: Number of cores within this CPU package.
70 * @nr_threads: Number of threads within this CPU.
71 * @numa_node: NUMA node this CPU is belonging to.
72 * @host_tid: Host thread ID.
73 * @running: #true if CPU is currently running (usermode).
74 * @created: Indicates whether the CPU thread has been successfully created.
75 * @stop: Indicates a pending stop request.
76 * @stopped: Indicates the CPU has been artificially stopped.
77 * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
78 * CPU and return to its top level loop.
79 * @env_ptr: Pointer to subclass-specific CPUArchState field.
80 * @current_tb: Currently executing TB.
81 * @kvm_fd: vCPU file descriptor for KVM.
82 *
83 * State of one CPU core or thread.
84 */
85 struct CPUState {
86 /*< private >*/
87 DeviceState parent_obj;
88 /*< public >*/
89
90 int nr_cores;
91 int nr_threads;
92 int numa_node;
93
94 struct QemuThread *thread;
95 #ifdef _WIN32
96 HANDLE hThread;
97 #endif
98 int thread_id;
99 uint32_t host_tid;
100 bool running;
101 struct QemuCond *halt_cond;
102 struct qemu_work_item *queued_work_first, *queued_work_last;
103 bool thread_kicked;
104 bool created;
105 bool stop;
106 bool stopped;
107 volatile sig_atomic_t exit_request;
108 volatile sig_atomic_t tcg_exit_req;
109
110 void *env_ptr; /* CPUArchState */
111 struct TranslationBlock *current_tb;
112
113 int kvm_fd;
114 bool kvm_vcpu_dirty;
115 struct KVMState *kvm_state;
116 struct kvm_run *kvm_run;
117
118 /* TODO Move common fields from CPUArchState here. */
119 int cpu_index; /* used by alpha TCG */
120 };
121
122
123 /**
124 * cpu_reset:
125 * @cpu: The CPU whose state is to be reset.
126 */
127 void cpu_reset(CPUState *cpu);
128
129 /**
130 * cpu_class_by_name:
131 * @typename: The CPU base type.
132 * @cpu_model: The model string without any parameters.
133 *
134 * Looks up a CPU #ObjectClass matching name @cpu_model.
135 *
136 * Returns: A #CPUClass or %NULL if not matching class is found.
137 */
138 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
139
140 /**
141 * cpu_class_set_vmsd:
142 * @cc: CPU class
143 * @value: Value to set. Unused for %CONFIG_USER_ONLY.
144 *
145 * Sets #VMStateDescription for @cc.
146 *
147 * The @value argument is intentionally discarded for the non-softmmu targets
148 * to avoid linker errors or excessive preprocessor usage. If this behavior
149 * is undesired, you should assign #CPUState.vmsd directly instead.
150 */
151 #ifndef CONFIG_USER_ONLY
152 static inline void cpu_class_set_vmsd(CPUClass *cc,
153 const struct VMStateDescription *value)
154 {
155 cc->vmsd = value;
156 }
157 #else
158 #define cpu_class_set_vmsd(cc, value) ((cc)->vmsd = NULL)
159 #endif
160
161 /**
162 * qemu_cpu_has_work:
163 * @cpu: The vCPU to check.
164 *
165 * Checks whether the CPU has work to do.
166 *
167 * Returns: %true if the CPU has work, %false otherwise.
168 */
169 bool qemu_cpu_has_work(CPUState *cpu);
170
171 /**
172 * qemu_cpu_is_self:
173 * @cpu: The vCPU to check against.
174 *
175 * Checks whether the caller is executing on the vCPU thread.
176 *
177 * Returns: %true if called from @cpu's thread, %false otherwise.
178 */
179 bool qemu_cpu_is_self(CPUState *cpu);
180
181 /**
182 * qemu_cpu_kick:
183 * @cpu: The vCPU to kick.
184 *
185 * Kicks @cpu's thread.
186 */
187 void qemu_cpu_kick(CPUState *cpu);
188
189 /**
190 * cpu_is_stopped:
191 * @cpu: The CPU to check.
192 *
193 * Checks whether the CPU is stopped.
194 *
195 * Returns: %true if run state is not running or if artificially stopped;
196 * %false otherwise.
197 */
198 bool cpu_is_stopped(CPUState *cpu);
199
200 /**
201 * run_on_cpu:
202 * @cpu: The vCPU to run on.
203 * @func: The function to be executed.
204 * @data: Data to pass to the function.
205 *
206 * Schedules the function @func for execution on the vCPU @cpu.
207 */
208 void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data);
209
210 /**
211 * qemu_get_cpu:
212 * @index: The CPUState@cpu_index value of the CPU to obtain.
213 *
214 * Gets a CPU matching @index.
215 *
216 * Returns: The CPU or %NULL if there is no matching CPU.
217 */
218 CPUState *qemu_get_cpu(int index);
219
220
221 #endif