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