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 "sysemu/cpu-timers.h"
31#include "qemu/main-loop.h"
32#include "qemu/notify.h"
33#include "qemu/guest-random.h"
34#include "exec/exec-all.h"
35
36#include "tcg-accel-ops.h"
37#include "tcg-accel-ops-rr.h"
38#include "tcg-accel-ops-icount.h"
39
40
41void rr_kick_vcpu_thread(CPUState *unused)
42{
43 CPUState *cpu;
44
45 CPU_FOREACH(cpu) {
46 cpu_exit(cpu);
47 };
48}
49
50
51
52
53
54
55
56
57
58
59
60
61
62static QEMUTimer *rr_kick_vcpu_timer;
63static CPUState *rr_current_cpu;
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
138static void rr_force_rcu(Notifier *notify, void *data)
139{
140 rr_kick_next_cpu();
141}
142
143
144
145
146
147
148
149
150
151static void *rr_cpu_thread_fn(void *arg)
152{
153 Notifier force_rcu;
154 CPUState *cpu = arg;
155
156 assert(tcg_enabled());
157 rcu_register_thread();
158 force_rcu.notify = rr_force_rcu;
159 rcu_add_force_rcu_notifier(&force_rcu);
160 tcg_register_thread();
161
162 qemu_mutex_lock_iothread();
163 qemu_thread_get_self(cpu->thread);
164
165 cpu->thread_id = qemu_get_thread_id();
166 cpu->can_do_io = 1;
167 cpu_thread_signal_created(cpu);
168 qemu_guest_random_seed_thread_part2(cpu->random_seed);
169
170
171 while (first_cpu->stopped) {
172 qemu_cond_wait_iothread(first_cpu->halt_cond);
173
174
175 CPU_FOREACH(cpu) {
176 current_cpu = cpu;
177 qemu_wait_io_event_common(cpu);
178 }
179 }
180
181 rr_start_kick_timer();
182
183 cpu = first_cpu;
184
185
186 cpu->exit_request = 1;
187
188 while (1) {
189 qemu_mutex_unlock_iothread();
190 replay_mutex_lock();
191 qemu_mutex_lock_iothread();
192
193 if (icount_enabled()) {
194
195 icount_account_warp_timer();
196
197
198
199
200 icount_handle_deadline();
201 }
202
203 replay_mutex_unlock();
204
205 if (!cpu) {
206 cpu = first_cpu;
207 }
208
209 while (cpu && cpu_work_list_empty(cpu) && !cpu->exit_request) {
210
211 qatomic_mb_set(&rr_current_cpu, cpu);
212 current_cpu = cpu;
213
214 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
215 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
216
217 if (cpu_can_run(cpu)) {
218 int r;
219
220 qemu_mutex_unlock_iothread();
221 if (icount_enabled()) {
222 icount_prepare_for_run(cpu);
223 }
224 r = tcg_cpus_exec(cpu);
225 if (icount_enabled()) {
226 icount_process_data(cpu);
227 }
228 qemu_mutex_lock_iothread();
229
230 if (r == EXCP_DEBUG) {
231 cpu_handle_guest_debug(cpu);
232 break;
233 } else if (r == EXCP_ATOMIC) {
234 qemu_mutex_unlock_iothread();
235 cpu_exec_step_atomic(cpu);
236 qemu_mutex_lock_iothread();
237 break;
238 }
239 } else if (cpu->stop) {
240 if (cpu->unplug) {
241 cpu = CPU_NEXT(cpu);
242 }
243 break;
244 }
245
246 cpu = CPU_NEXT(cpu);
247 }
248
249
250 qatomic_set(&rr_current_cpu, NULL);
251
252 if (cpu && cpu->exit_request) {
253 qatomic_mb_set(&cpu->exit_request, 0);
254 }
255
256 if (icount_enabled() && all_cpu_threads_idle()) {
257
258
259
260
261 qemu_notify_event();
262 }
263
264 rr_wait_io_event();
265 rr_deal_with_unplugged_cpus();
266 }
267
268 rcu_remove_force_rcu_notifier(&force_rcu);
269 rcu_unregister_thread();
270 return NULL;
271}
272
273void rr_start_vcpu_thread(CPUState *cpu)
274{
275 char thread_name[VCPU_THREAD_NAME_SIZE];
276 static QemuCond *single_tcg_halt_cond;
277 static QemuThread *single_tcg_cpu_thread;
278
279 g_assert(tcg_enabled());
280 tcg_cpu_init_cflags(cpu, false);
281
282 if (!single_tcg_cpu_thread) {
283 cpu->thread = g_new0(QemuThread, 1);
284 cpu->halt_cond = g_new0(QemuCond, 1);
285 qemu_cond_init(cpu->halt_cond);
286
287
288 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
289 qemu_thread_create(cpu->thread, thread_name,
290 rr_cpu_thread_fn,
291 cpu, QEMU_THREAD_JOINABLE);
292
293 single_tcg_halt_cond = cpu->halt_cond;
294 single_tcg_cpu_thread = cpu->thread;
295#ifdef _WIN32
296 cpu->hThread = qemu_thread_get_handle(cpu->thread);
297#endif
298 } else {
299
300 cpu->thread = single_tcg_cpu_thread;
301 cpu->halt_cond = single_tcg_halt_cond;
302 cpu->thread_id = first_cpu->thread_id;
303 cpu->can_do_io = 1;
304 cpu->created = true;
305 }
306}
307