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