1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include "qemu/osdep.h"
27#include "qemu-common.h"
28#include "sysemu/tcg.h"
29#include "sysemu/replay.h"
30#include "qemu/main-loop.h"
31#include "qemu/guest-random.h"
32#include "exec/exec-all.h"
33
34#include "tcg-accel-ops.h"
35#include "tcg-accel-ops-rr.h"
36#include "tcg-accel-ops-icount.h"
37
38
39void rr_kick_vcpu_thread(CPUState *unused)
40{
41 CPUState *cpu;
42
43 CPU_FOREACH(cpu) {
44 cpu_exit(cpu);
45 };
46}
47
48
49
50
51
52
53
54
55
56
57
58
59
60static QEMUTimer *rr_kick_vcpu_timer;
61static CPUState *rr_current_cpu;
62
63#define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10)
64
65static inline int64_t rr_next_kick_time(void)
66{
67 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
68}
69
70
71static void rr_kick_next_cpu(void)
72{
73 CPUState *cpu;
74 do {
75 cpu = qatomic_mb_read(&rr_current_cpu);
76 if (cpu) {
77 cpu_exit(cpu);
78 }
79 } while (cpu != qatomic_mb_read(&rr_current_cpu));
80}
81
82static void rr_kick_thread(void *opaque)
83{
84 timer_mod(rr_kick_vcpu_timer, rr_next_kick_time());
85 rr_kick_next_cpu();
86}
87
88static void rr_start_kick_timer(void)
89{
90 if (!rr_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
91 rr_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
92 rr_kick_thread, NULL);
93 }
94 if (rr_kick_vcpu_timer && !timer_pending(rr_kick_vcpu_timer)) {
95 timer_mod(rr_kick_vcpu_timer, rr_next_kick_time());
96 }
97}
98
99static void rr_stop_kick_timer(void)
100{
101 if (rr_kick_vcpu_timer && timer_pending(rr_kick_vcpu_timer)) {
102 timer_del(rr_kick_vcpu_timer);
103 }
104}
105
106static void rr_wait_io_event(void)
107{
108 CPUState *cpu;
109
110 while (all_cpu_threads_idle()) {
111 rr_stop_kick_timer();
112 qemu_cond_wait_iothread(first_cpu->halt_cond);
113 }
114
115 rr_start_kick_timer();
116
117 CPU_FOREACH(cpu) {
118 qemu_wait_io_event_common(cpu);
119 }
120}
121
122
123
124
125
126static void rr_deal_with_unplugged_cpus(void)
127{
128 CPUState *cpu;
129
130 CPU_FOREACH(cpu) {
131 if (cpu->unplug && !cpu_can_run(cpu)) {
132 tcg_cpus_destroy(cpu);
133 break;
134 }
135 }
136}
137
138
139
140
141
142
143
144
145
146static void *rr_cpu_thread_fn(void *arg)
147{
148 CPUState *cpu = arg;
149
150 assert(tcg_enabled());
151 rcu_register_thread();
152 tcg_register_thread();
153
154 qemu_mutex_lock_iothread();
155 qemu_thread_get_self(cpu->thread);
156
157 cpu->thread_id = qemu_get_thread_id();
158 cpu->can_do_io = 1;
159 cpu_thread_signal_created(cpu);
160 qemu_guest_random_seed_thread_part2(cpu->random_seed);
161
162
163 while (first_cpu->stopped) {
164 qemu_cond_wait_iothread(first_cpu->halt_cond);
165
166
167 CPU_FOREACH(cpu) {
168 current_cpu = cpu;
169 qemu_wait_io_event_common(cpu);
170 }
171 }
172
173 rr_start_kick_timer();
174
175 cpu = first_cpu;
176
177
178 cpu->exit_request = 1;
179
180 while (1) {
181 qemu_mutex_unlock_iothread();
182 replay_mutex_lock();
183 qemu_mutex_lock_iothread();
184
185 if (icount_enabled()) {
186
187 icount_account_warp_timer();
188
189
190
191
192 icount_handle_deadline();
193 }
194
195 replay_mutex_unlock();
196
197 if (!cpu) {
198 cpu = first_cpu;
199 }
200
201 while (cpu && cpu_work_list_empty(cpu) && !cpu->exit_request) {
202
203 qatomic_mb_set(&rr_current_cpu, cpu);
204 current_cpu = cpu;
205
206 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
207 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
208
209 if (cpu_can_run(cpu)) {
210 int r;
211
212 qemu_mutex_unlock_iothread();
213 if (icount_enabled()) {
214 icount_prepare_for_run(cpu);
215 }
216 r = tcg_cpus_exec(cpu);
217 if (icount_enabled()) {
218 icount_process_data(cpu);
219 }
220 qemu_mutex_lock_iothread();
221
222 if (r == EXCP_DEBUG) {
223 cpu_handle_guest_debug(cpu);
224 break;
225 } else if (r == EXCP_ATOMIC) {
226 qemu_mutex_unlock_iothread();
227 cpu_exec_step_atomic(cpu);
228 qemu_mutex_lock_iothread();
229 break;
230 }
231 } else if (cpu->stop) {
232 if (cpu->unplug) {
233 cpu = CPU_NEXT(cpu);
234 }
235 break;
236 }
237
238 cpu = CPU_NEXT(cpu);
239 }
240
241
242 qatomic_set(&rr_current_cpu, NULL);
243
244 if (cpu && cpu->exit_request) {
245 qatomic_mb_set(&cpu->exit_request, 0);
246 }
247
248 if (icount_enabled() && all_cpu_threads_idle()) {
249
250
251
252
253 qemu_notify_event();
254 }
255
256 rr_wait_io_event();
257 rr_deal_with_unplugged_cpus();
258 }
259
260 rcu_unregister_thread();
261 return NULL;
262}
263
264void rr_start_vcpu_thread(CPUState *cpu)
265{
266 char thread_name[VCPU_THREAD_NAME_SIZE];
267 static QemuCond *single_tcg_halt_cond;
268 static QemuThread *single_tcg_cpu_thread;
269
270 g_assert(tcg_enabled());
271 tcg_cpu_init_cflags(cpu, false);
272
273 if (!single_tcg_cpu_thread) {
274 cpu->thread = g_malloc0(sizeof(QemuThread));
275 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
276 qemu_cond_init(cpu->halt_cond);
277
278
279 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
280 qemu_thread_create(cpu->thread, thread_name,
281 rr_cpu_thread_fn,
282 cpu, QEMU_THREAD_JOINABLE);
283
284 single_tcg_halt_cond = cpu->halt_cond;
285 single_tcg_cpu_thread = cpu->thread;
286#ifdef _WIN32
287 cpu->hThread = qemu_thread_get_handle(cpu->thread);
288#endif
289 } else {
290
291 cpu->thread = single_tcg_cpu_thread;
292 cpu->halt_cond = single_tcg_halt_cond;
293 cpu->thread_id = first_cpu->thread_id;
294 cpu->can_do_io = 1;
295 cpu->created = true;
296 }
297}
298