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#include "qemu/osdep.h"
26#include "qemu/cutils.h"
27#include "migration/vmstate.h"
28#include "qapi/error.h"
29#include "qemu/error-report.h"
30#include "exec/exec-all.h"
31#include "sysemu/cpus.h"
32#include "sysemu/qtest.h"
33#include "qemu/main-loop.h"
34#include "qemu/option.h"
35#include "qemu/seqlock.h"
36#include "sysemu/replay.h"
37#include "sysemu/runstate.h"
38#include "hw/core/cpu.h"
39#include "sysemu/cpu-timers.h"
40#include "sysemu/cpu-throttle.h"
41#include "timers-state.h"
42
43
44
45
46
47
48
49static bool icount_sleep = true;
50
51#define MAX_ICOUNT_SHIFT 10
52
53
54
55
56
57
58int use_icount;
59
60static void icount_enable_precise(void)
61{
62 use_icount = 1;
63}
64
65static void icount_enable_adaptive(void)
66{
67 use_icount = 2;
68}
69
70
71
72
73
74
75static int64_t icount_get_executed(CPUState *cpu)
76{
77 return (cpu->icount_budget -
78 (cpu_neg(cpu)->icount_decr.u16.low + cpu->icount_extra));
79}
80
81
82
83
84
85
86static void icount_update_locked(CPUState *cpu)
87{
88 int64_t executed = icount_get_executed(cpu);
89 cpu->icount_budget -= executed;
90
91 qatomic_set_i64(&timers_state.qemu_icount,
92 timers_state.qemu_icount + executed);
93}
94
95
96
97
98
99
100void icount_update(CPUState *cpu)
101{
102 seqlock_write_lock(&timers_state.vm_clock_seqlock,
103 &timers_state.vm_clock_lock);
104 icount_update_locked(cpu);
105 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
106 &timers_state.vm_clock_lock);
107}
108
109static int64_t icount_get_raw_locked(void)
110{
111 CPUState *cpu = current_cpu;
112
113 if (cpu && cpu->running) {
114 if (!cpu->can_do_io) {
115 error_report("Bad icount read");
116 exit(1);
117 }
118
119 icount_update_locked(cpu);
120 }
121
122 return qatomic_read_i64(&timers_state.qemu_icount);
123}
124
125static int64_t icount_get_locked(void)
126{
127 int64_t icount = icount_get_raw_locked();
128 return qatomic_read_i64(&timers_state.qemu_icount_bias) +
129 icount_to_ns(icount);
130}
131
132int64_t icount_get_raw(void)
133{
134 int64_t icount;
135 unsigned start;
136
137 do {
138 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
139 icount = icount_get_raw_locked();
140 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
141
142 return icount;
143}
144
145
146int64_t icount_get(void)
147{
148 int64_t icount;
149 unsigned start;
150
151 do {
152 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
153 icount = icount_get_locked();
154 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
155
156 return icount;
157}
158
159int64_t icount_to_ns(int64_t icount)
160{
161 return icount << qatomic_read(&timers_state.icount_time_shift);
162}
163
164
165
166
167
168
169
170#define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
171
172static void icount_adjust(void)
173{
174 int64_t cur_time;
175 int64_t cur_icount;
176 int64_t delta;
177
178
179 if (!runstate_is_running()) {
180 return;
181 }
182
183 seqlock_write_lock(&timers_state.vm_clock_seqlock,
184 &timers_state.vm_clock_lock);
185 cur_time = REPLAY_CLOCK_LOCKED(REPLAY_CLOCK_VIRTUAL_RT,
186 cpu_get_clock_locked());
187 cur_icount = icount_get_locked();
188
189 delta = cur_icount - cur_time;
190
191 if (delta > 0
192 && timers_state.last_delta + ICOUNT_WOBBLE < delta * 2
193 && timers_state.icount_time_shift > 0) {
194
195 qatomic_set(&timers_state.icount_time_shift,
196 timers_state.icount_time_shift - 1);
197 }
198 if (delta < 0
199 && timers_state.last_delta - ICOUNT_WOBBLE > delta * 2
200 && timers_state.icount_time_shift < MAX_ICOUNT_SHIFT) {
201
202 qatomic_set(&timers_state.icount_time_shift,
203 timers_state.icount_time_shift + 1);
204 }
205 timers_state.last_delta = delta;
206 qatomic_set_i64(&timers_state.qemu_icount_bias,
207 cur_icount - (timers_state.qemu_icount
208 << timers_state.icount_time_shift));
209 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
210 &timers_state.vm_clock_lock);
211}
212
213static void icount_adjust_rt(void *opaque)
214{
215 timer_mod(timers_state.icount_rt_timer,
216 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
217 icount_adjust();
218}
219
220static void icount_adjust_vm(void *opaque)
221{
222 timer_mod(timers_state.icount_vm_timer,
223 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
224 NANOSECONDS_PER_SECOND / 10);
225 icount_adjust();
226}
227
228int64_t icount_round(int64_t count)
229{
230 int shift = qatomic_read(&timers_state.icount_time_shift);
231 return (count + (1 << shift) - 1) >> shift;
232}
233
234static void icount_warp_rt(void)
235{
236 unsigned seq;
237 int64_t warp_start;
238
239
240
241
242
243 do {
244 seq = seqlock_read_begin(&timers_state.vm_clock_seqlock);
245 warp_start = timers_state.vm_clock_warp_start;
246 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq));
247
248 if (warp_start == -1) {
249 return;
250 }
251
252 seqlock_write_lock(&timers_state.vm_clock_seqlock,
253 &timers_state.vm_clock_lock);
254 if (runstate_is_running()) {
255 int64_t clock = REPLAY_CLOCK_LOCKED(REPLAY_CLOCK_VIRTUAL_RT,
256 cpu_get_clock_locked());
257 int64_t warp_delta;
258
259 warp_delta = clock - timers_state.vm_clock_warp_start;
260 if (icount_enabled() == 2) {
261
262
263
264
265 int64_t cur_icount = icount_get_locked();
266 int64_t delta = clock - cur_icount;
267 warp_delta = MIN(warp_delta, delta);
268 }
269 qatomic_set_i64(&timers_state.qemu_icount_bias,
270 timers_state.qemu_icount_bias + warp_delta);
271 }
272 timers_state.vm_clock_warp_start = -1;
273 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
274 &timers_state.vm_clock_lock);
275
276 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
277 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
278 }
279}
280
281static void icount_timer_cb(void *opaque)
282{
283
284
285
286
287 icount_warp_rt();
288}
289
290void icount_start_warp_timer(void)
291{
292 int64_t clock;
293 int64_t deadline;
294
295 assert(icount_enabled());
296
297
298
299
300
301 if (!runstate_is_running()) {
302 return;
303 }
304
305 if (replay_mode != REPLAY_MODE_PLAY) {
306 if (!all_cpu_threads_idle()) {
307 return;
308 }
309
310 if (qtest_enabled()) {
311
312 return;
313 }
314
315 replay_checkpoint(CHECKPOINT_CLOCK_WARP_START);
316 } else {
317
318 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) {
319
320
321
322
323
324
325 if (replay_has_event()) {
326 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
327 }
328 return;
329 }
330 }
331
332
333 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
334 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
335 ~QEMU_TIMER_ATTR_EXTERNAL);
336 if (deadline < 0) {
337 static bool notified;
338 if (!icount_sleep && !notified) {
339 warn_report("icount sleep disabled and no active timers");
340 notified = true;
341 }
342 return;
343 }
344
345 if (deadline > 0) {
346
347
348
349
350
351
352
353 if (!icount_sleep) {
354
355
356
357
358
359
360
361 seqlock_write_lock(&timers_state.vm_clock_seqlock,
362 &timers_state.vm_clock_lock);
363 qatomic_set_i64(&timers_state.qemu_icount_bias,
364 timers_state.qemu_icount_bias + deadline);
365 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
366 &timers_state.vm_clock_lock);
367 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
368 } else {
369
370
371
372
373
374
375
376
377 seqlock_write_lock(&timers_state.vm_clock_seqlock,
378 &timers_state.vm_clock_lock);
379 if (timers_state.vm_clock_warp_start == -1
380 || timers_state.vm_clock_warp_start > clock) {
381 timers_state.vm_clock_warp_start = clock;
382 }
383 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
384 &timers_state.vm_clock_lock);
385 timer_mod_anticipate(timers_state.icount_warp_timer,
386 clock + deadline);
387 }
388 } else if (deadline == 0) {
389 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
390 }
391}
392
393void icount_account_warp_timer(void)
394{
395 if (!icount_sleep) {
396 return;
397 }
398
399
400
401
402
403 if (!runstate_is_running()) {
404 return;
405 }
406
407 replay_async_events();
408
409
410 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) {
411 return;
412 }
413
414 timer_del(timers_state.icount_warp_timer);
415 icount_warp_rt();
416}
417
418void icount_configure(QemuOpts *opts, Error **errp)
419{
420 const char *option = qemu_opt_get(opts, "shift");
421 bool sleep = qemu_opt_get_bool(opts, "sleep", true);
422 bool align = qemu_opt_get_bool(opts, "align", false);
423 long time_shift = -1;
424
425 if (!option) {
426 if (qemu_opt_get(opts, "align") != NULL) {
427 error_setg(errp, "Please specify shift option when using align");
428 }
429 return;
430 }
431
432 if (align && !sleep) {
433 error_setg(errp, "align=on and sleep=off are incompatible");
434 return;
435 }
436
437 if (strcmp(option, "auto") != 0) {
438 if (qemu_strtol(option, NULL, 0, &time_shift) < 0
439 || time_shift < 0 || time_shift > MAX_ICOUNT_SHIFT) {
440 error_setg(errp, "icount: Invalid shift value");
441 return;
442 }
443 } else if (icount_align_option) {
444 error_setg(errp, "shift=auto and align=on are incompatible");
445 return;
446 } else if (!icount_sleep) {
447 error_setg(errp, "shift=auto and sleep=off are incompatible");
448 return;
449 }
450
451 icount_sleep = sleep;
452 if (icount_sleep) {
453 timers_state.icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
454 icount_timer_cb, NULL);
455 }
456
457 icount_align_option = align;
458
459 if (time_shift >= 0) {
460 timers_state.icount_time_shift = time_shift;
461 icount_enable_precise();
462 return;
463 }
464
465 icount_enable_adaptive();
466
467
468
469
470
471 timers_state.icount_time_shift = 3;
472
473
474
475
476
477
478
479
480 timers_state.vm_clock_warp_start = -1;
481 timers_state.icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
482 icount_adjust_rt, NULL);
483 timer_mod(timers_state.icount_rt_timer,
484 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
485 timers_state.icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
486 icount_adjust_vm, NULL);
487 timer_mod(timers_state.icount_vm_timer,
488 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
489 NANOSECONDS_PER_SECOND / 10);
490}
491
492void icount_notify_exit(void)
493{
494 if (icount_enabled() && current_cpu) {
495 qemu_cpu_kick(current_cpu);
496 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
497 }
498}
499