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