]>
Commit | Line | Data |
---|---|---|
87ecb68b PB |
1 | #ifndef QEMU_TIMER_H |
2 | #define QEMU_TIMER_H | |
3 | ||
89a603a0 | 4 | #include "qemu/bitops.h" |
1de7afc9 | 5 | #include "qemu/notify.h" |
49caffe0 | 6 | #include "qemu/host-utils.h" |
29e922b6 | 7 | |
13566fe3 | 8 | #define NANOSECONDS_PER_SECOND 1000000000LL |
471fae3c | 9 | |
87ecb68b PB |
10 | /* timers */ |
11 | ||
0ce1b948 PB |
12 | #define SCALE_MS 1000000 |
13 | #define SCALE_US 1000 | |
14 | #define SCALE_NS 1 | |
15 | ||
ff83c66e AB |
16 | /** |
17 | * QEMUClockType: | |
18 | * | |
19 | * The following clock types are available: | |
20 | * | |
21 | * @QEMU_CLOCK_REALTIME: Real time clock | |
22 | * | |
23 | * The real time clock should be used only for stuff which does not | |
490ab15a C |
24 | * change the virtual machine state, as it runs even if the virtual |
25 | * machine is stopped. | |
ff83c66e | 26 | * |
ff83c66e AB |
27 | * @QEMU_CLOCK_VIRTUAL: virtual clock |
28 | * | |
490ab15a C |
29 | * The virtual clock only runs during the emulation. It stops |
30 | * when the virtual machine is stopped. | |
ff83c66e | 31 | * |
ff83c66e AB |
32 | * @QEMU_CLOCK_HOST: host clock |
33 | * | |
490ab15a | 34 | * The host clock should be used for device models that emulate accurate |
ff83c66e AB |
35 | * real time sources. It will continue to run when the virtual machine |
36 | * is suspended, and it will reflect system time changes the host may | |
490ab15a | 37 | * undergo (e.g. due to NTP). |
4e7fa73e PD |
38 | * |
39 | * @QEMU_CLOCK_VIRTUAL_RT: realtime clock used for icount warp | |
40 | * | |
41 | * Outside icount mode, this clock is the same as @QEMU_CLOCK_VIRTUAL. | |
42 | * In icount mode, this clock counts nanoseconds while the virtual | |
bf2a7ddb PD |
43 | * machine is running. It is used to increase @QEMU_CLOCK_VIRTUAL |
44 | * while the CPUs are sleeping and thus not executing instructions. | |
ff83c66e AB |
45 | */ |
46 | ||
47 | typedef enum { | |
48 | QEMU_CLOCK_REALTIME = 0, | |
49 | QEMU_CLOCK_VIRTUAL = 1, | |
50 | QEMU_CLOCK_HOST = 2, | |
4e7fa73e | 51 | QEMU_CLOCK_VIRTUAL_RT = 3, |
ff83c66e AB |
52 | QEMU_CLOCK_MAX |
53 | } QEMUClockType; | |
58ac56b9 | 54 | |
89a603a0 AP |
55 | /** |
56 | * QEMU Timer attributes: | |
57 | * | |
58 | * An individual timer may be given one or multiple attributes when initialized. | |
59 | * Each attribute corresponds to one bit. Attributes modify the processing | |
60 | * of timers when they fire. | |
61 | * | |
e81f8679 AP |
62 | * The following attributes are available: |
63 | * | |
64 | * QEMU_TIMER_ATTR_EXTERNAL: drives external subsystem | |
dcb15780 | 65 | * QEMU_TIMER_ATTR_ALL: mask for all existing attributes |
e81f8679 AP |
66 | * |
67 | * Timers with this attribute do not recorded in rr mode, therefore it could be | |
68 | * used for the subsystems that operate outside the guest core. Applicable only | |
69 | * with virtual clock type. | |
89a603a0 AP |
70 | */ |
71 | ||
dcb15780 PD |
72 | #define QEMU_TIMER_ATTR_EXTERNAL ((int)BIT(0)) |
73 | #define QEMU_TIMER_ATTR_ALL 0xffffffff | |
e81f8679 | 74 | |
ff83c66e | 75 | typedef struct QEMUTimerList QEMUTimerList; |
754d6a54 AB |
76 | |
77 | struct QEMUTimerListGroup { | |
78 | QEMUTimerList *tl[QEMU_CLOCK_MAX]; | |
79 | }; | |
80 | ||
87ecb68b | 81 | typedef void QEMUTimerCB(void *opaque); |
3f53bc61 | 82 | typedef void QEMUTimerListNotifyCB(void *opaque, QEMUClockType type); |
87ecb68b | 83 | |
ff83c66e AB |
84 | struct QEMUTimer { |
85 | int64_t expire_time; /* in nanoseconds */ | |
86 | QEMUTimerList *timer_list; | |
87 | QEMUTimerCB *cb; | |
88 | void *opaque; | |
89 | QEMUTimer *next; | |
89a603a0 | 90 | int attributes; |
ff83c66e AB |
91 | int scale; |
92 | }; | |
93 | ||
754d6a54 | 94 | extern QEMUTimerListGroup main_loop_tlg; |
87ecb68b | 95 | |
b4049b74 | 96 | /* |
54904d2a AB |
97 | * qemu_clock_get_ns; |
98 | * @type: the clock type | |
99 | * | |
100 | * Get the nanosecond value of a clock with | |
101 | * type @type | |
102 | * | |
103 | * Returns: the clock value in nanoseconds | |
104 | */ | |
40daca54 | 105 | int64_t qemu_clock_get_ns(QEMUClockType type); |
54904d2a | 106 | |
55a197da AB |
107 | /** |
108 | * qemu_clock_get_ms; | |
109 | * @type: the clock type | |
110 | * | |
111 | * Get the millisecond value of a clock with | |
112 | * type @type | |
113 | * | |
114 | * Returns: the clock value in milliseconds | |
115 | */ | |
116 | static inline int64_t qemu_clock_get_ms(QEMUClockType type) | |
117 | { | |
118 | return qemu_clock_get_ns(type) / SCALE_MS; | |
119 | } | |
120 | ||
121 | /** | |
122 | * qemu_clock_get_us; | |
123 | * @type: the clock type | |
124 | * | |
125 | * Get the microsecond value of a clock with | |
126 | * type @type | |
127 | * | |
128 | * Returns: the clock value in microseconds | |
129 | */ | |
130 | static inline int64_t qemu_clock_get_us(QEMUClockType type) | |
131 | { | |
132 | return qemu_clock_get_ns(type) / SCALE_US; | |
133 | } | |
134 | ||
54904d2a AB |
135 | /** |
136 | * qemu_clock_has_timers: | |
40daca54 | 137 | * @type: the clock type |
54904d2a AB |
138 | * |
139 | * Determines whether a clock's default timer list | |
140 | * has timers attached | |
141 | * | |
978f2205 SH |
142 | * Note that this function should not be used when other threads also access |
143 | * the timer list. The return value may be outdated by the time it is acted | |
144 | * upon. | |
145 | * | |
54904d2a AB |
146 | * Returns: true if the clock's default timer list |
147 | * has timers attached | |
148 | */ | |
40daca54 | 149 | bool qemu_clock_has_timers(QEMUClockType type); |
54904d2a AB |
150 | |
151 | /** | |
152 | * qemu_clock_expired: | |
40daca54 | 153 | * @type: the clock type |
54904d2a AB |
154 | * |
155 | * Determines whether a clock's default timer list | |
45241cf9 | 156 | * has an expired timer. |
54904d2a AB |
157 | * |
158 | * Returns: true if the clock's default timer list has | |
159 | * an expired timer | |
160 | */ | |
40daca54 | 161 | bool qemu_clock_expired(QEMUClockType type); |
02a03a9f | 162 | |
ff83c66e AB |
163 | /** |
164 | * qemu_clock_use_for_deadline: | |
40daca54 | 165 | * @type: the clock type |
ff83c66e AB |
166 | * |
167 | * Determine whether a clock should be used for deadline | |
168 | * calculations. Some clocks, for instance vm_clock with | |
740b1759 CF |
169 | * icount_enabled() set, do not count in nanoseconds. |
170 | * Such clocks are not used for deadline calculations, and are presumed | |
ff83c66e AB |
171 | * to interrupt any poll using qemu_notify/aio_notify |
172 | * etc. | |
173 | * | |
174 | * Returns: true if the clock runs in nanoseconds and | |
175 | * should be used for a deadline. | |
176 | */ | |
40daca54 | 177 | bool qemu_clock_use_for_deadline(QEMUClockType type); |
ff83c66e | 178 | |
ac70aafc | 179 | /** |
40daca54 AB |
180 | * qemu_clock_deadline_ns_all: |
181 | * @type: the clock type | |
dcb15780 PD |
182 | * @attr_mask: mask for the timer attributes that are included |
183 | * in deadline calculation | |
ac70aafc AB |
184 | * |
185 | * Calculate the deadline across all timer lists associated | |
186 | * with a clock (as opposed to just the default one) | |
187 | * in nanoseconds, or -1 if no timer is set to expire. | |
188 | * | |
189 | * Returns: time until expiry in nanoseconds or -1 | |
190 | */ | |
dcb15780 | 191 | int64_t qemu_clock_deadline_ns_all(QEMUClockType type, int attr_mask); |
ac70aafc | 192 | |
ff83c66e AB |
193 | /** |
194 | * qemu_clock_get_main_loop_timerlist: | |
40daca54 | 195 | * @type: the clock type |
ff83c66e | 196 | * |
083b96e2 | 197 | * Return the default timer list associated with a clock. |
ff83c66e AB |
198 | * |
199 | * Returns: the default timer list | |
200 | */ | |
40daca54 | 201 | QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type); |
ff83c66e | 202 | |
b1bbfe72 AB |
203 | /** |
204 | * qemu_clock_nofify: | |
40daca54 | 205 | * @type: the clock type |
b1bbfe72 AB |
206 | * |
207 | * Call the notifier callback connected with the default timer | |
208 | * list linked to the clock, or qemu_notify() if none. | |
209 | */ | |
40daca54 AB |
210 | void qemu_clock_notify(QEMUClockType type); |
211 | ||
212 | /** | |
213 | * qemu_clock_enable: | |
214 | * @type: the clock type | |
215 | * @enabled: true to enable, false to disable | |
216 | * | |
217 | * Enable or disable a clock | |
3c053411 LPF |
218 | * Disabling the clock will wait for related timerlists to stop |
219 | * executing qemu_run_timers. Thus, this functions should not | |
220 | * be used from the callback of a timer that is based on @clock. | |
221 | * Doing so would cause a deadlock. | |
222 | * | |
223 | * Caller should hold BQL. | |
40daca54 AB |
224 | */ |
225 | void qemu_clock_enable(QEMUClockType type, bool enabled); | |
226 | ||
40daca54 AB |
227 | /** |
228 | * qemu_clock_run_timers: | |
229 | * @type: clock on which to operate | |
230 | * | |
231 | * Run all the timers associated with the default timer list | |
232 | * of a clock. | |
233 | * | |
234 | * Returns: true if any timer ran. | |
235 | */ | |
236 | bool qemu_clock_run_timers(QEMUClockType type); | |
237 | ||
238 | /** | |
239 | * qemu_clock_run_all_timers: | |
240 | * | |
241 | * Run all the timers associated with the default timer list | |
242 | * of every clock. | |
243 | * | |
244 | * Returns: true if any timer ran. | |
245 | */ | |
246 | bool qemu_clock_run_all_timers(void); | |
247 | ||
4b930d26 | 248 | |
40daca54 AB |
249 | /* |
250 | * QEMUTimerList | |
251 | */ | |
b1bbfe72 | 252 | |
ff83c66e AB |
253 | /** |
254 | * timerlist_new: | |
255 | * @type: the clock type to associate with the timerlist | |
d5541d86 AB |
256 | * @cb: the callback to call on notification |
257 | * @opaque: the opaque pointer to pass to the callback | |
ff83c66e AB |
258 | * |
259 | * Create a new timerlist associated with the clock of | |
260 | * type @type. | |
261 | * | |
262 | * Returns: a pointer to the QEMUTimerList created | |
263 | */ | |
d5541d86 AB |
264 | QEMUTimerList *timerlist_new(QEMUClockType type, |
265 | QEMUTimerListNotifyCB *cb, void *opaque); | |
ff83c66e AB |
266 | |
267 | /** | |
268 | * timerlist_free: | |
269 | * @timer_list: the timer list to free | |
270 | * | |
271 | * Frees a timer_list. It must have no active timers. | |
272 | */ | |
273 | void timerlist_free(QEMUTimerList *timer_list); | |
274 | ||
275 | /** | |
276 | * timerlist_has_timers: | |
277 | * @timer_list: the timer list to operate on | |
278 | * | |
279 | * Determine whether a timer list has active timers | |
280 | * | |
978f2205 SH |
281 | * Note that this function should not be used when other threads also access |
282 | * the timer list. The return value may be outdated by the time it is acted | |
283 | * upon. | |
284 | * | |
ff83c66e AB |
285 | * Returns: true if the timer list has timers. |
286 | */ | |
287 | bool timerlist_has_timers(QEMUTimerList *timer_list); | |
288 | ||
289 | /** | |
290 | * timerlist_expired: | |
291 | * @timer_list: the timer list to operate on | |
292 | * | |
293 | * Determine whether a timer list has any timers which | |
294 | * are expired. | |
295 | * | |
296 | * Returns: true if the timer list has timers which | |
297 | * have expired. | |
298 | */ | |
299 | bool timerlist_expired(QEMUTimerList *timer_list); | |
ff83c66e AB |
300 | |
301 | /** | |
302 | * timerlist_deadline_ns: | |
303 | * @timer_list: the timer list to operate on | |
304 | * | |
305 | * Determine the deadline for a timer_list, i.e. | |
306 | * the number of nanoseconds until the first timer | |
307 | * expires. Return -1 if there are no timers. | |
308 | * | |
309 | * Returns: the number of nanoseconds until the earliest | |
310 | * timer expires -1 if none | |
311 | */ | |
312 | int64_t timerlist_deadline_ns(QEMUTimerList *timer_list); | |
313 | ||
314 | /** | |
40daca54 | 315 | * timerlist_get_clock: |
ff83c66e AB |
316 | * @timer_list: the timer list to operate on |
317 | * | |
40daca54 | 318 | * Determine the clock type associated with a timer list. |
ff83c66e | 319 | * |
40daca54 AB |
320 | * Returns: the clock type associated with the |
321 | * timer list. | |
ff83c66e | 322 | */ |
40daca54 | 323 | QEMUClockType timerlist_get_clock(QEMUTimerList *timer_list); |
ff83c66e AB |
324 | |
325 | /** | |
326 | * timerlist_run_timers: | |
327 | * @timer_list: the timer list to use | |
328 | * | |
329 | * Call all expired timers associated with the timer list. | |
330 | * | |
331 | * Returns: true if any timer expired | |
332 | */ | |
333 | bool timerlist_run_timers(QEMUTimerList *timer_list); | |
334 | ||
d5541d86 AB |
335 | /** |
336 | * timerlist_notify: | |
337 | * @timer_list: the timer list to use | |
338 | * | |
339 | * call the notifier callback associated with the timer list. | |
340 | */ | |
341 | void timerlist_notify(QEMUTimerList *timer_list); | |
342 | ||
40daca54 AB |
343 | /* |
344 | * QEMUTimerListGroup | |
345 | */ | |
346 | ||
754d6a54 AB |
347 | /** |
348 | * timerlistgroup_init: | |
349 | * @tlg: the timer list group | |
d5541d86 AB |
350 | * @cb: the callback to call when a notify is required |
351 | * @opaque: the opaque pointer to be passed to the callback. | |
754d6a54 AB |
352 | * |
353 | * Initialise a timer list group. This must already be | |
d5541d86 AB |
354 | * allocated in memory and zeroed. The notifier callback is |
355 | * called whenever a clock in the timer list group is | |
356 | * reenabled or whenever a timer associated with any timer | |
357 | * list is modified. If @cb is specified as null, qemu_notify() | |
358 | * is used instead. | |
754d6a54 | 359 | */ |
d5541d86 AB |
360 | void timerlistgroup_init(QEMUTimerListGroup *tlg, |
361 | QEMUTimerListNotifyCB *cb, void *opaque); | |
754d6a54 AB |
362 | |
363 | /** | |
364 | * timerlistgroup_deinit: | |
365 | * @tlg: the timer list group | |
366 | * | |
367 | * Deinitialise a timer list group. This must already be | |
368 | * initialised. Note the memory is not freed. | |
369 | */ | |
370 | void timerlistgroup_deinit(QEMUTimerListGroup *tlg); | |
371 | ||
372 | /** | |
373 | * timerlistgroup_run_timers: | |
374 | * @tlg: the timer list group | |
375 | * | |
376 | * Run the timers associated with a timer list group. | |
377 | * This will run timers on multiple clocks. | |
378 | * | |
379 | * Returns: true if any timer callback ran | |
380 | */ | |
381 | bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg); | |
382 | ||
383 | /** | |
54904d2a | 384 | * timerlistgroup_deadline_ns: |
754d6a54 AB |
385 | * @tlg: the timer list group |
386 | * | |
387 | * Determine the deadline of the soonest timer to | |
388 | * expire associated with any timer list linked to | |
389 | * the timer list group. Only clocks suitable for | |
390 | * deadline calculation are included. | |
391 | * | |
392 | * Returns: the deadline in nanoseconds or -1 if no | |
393 | * timers are to expire. | |
394 | */ | |
395 | int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg); | |
396 | ||
40daca54 AB |
397 | /* |
398 | * QEMUTimer | |
54904d2a | 399 | */ |
ff83c66e AB |
400 | |
401 | /** | |
89a603a0 | 402 | * timer_init_full: |
ff83c66e | 403 | * @ts: the timer to be initialised |
89a603a0 AP |
404 | * @timer_list_group: (optional) the timer list group to attach the timer to |
405 | * @type: the clock type to use | |
dc9fc1ca | 406 | * @scale: the scale value for the timer |
89a603a0 | 407 | * @attributes: 0, or one or more OR'ed QEMU_TIMER_ATTR_<id> values |
ff83c66e AB |
408 | * @cb: the callback to be called when the timer expires |
409 | * @opaque: the opaque pointer to be passed to the callback | |
410 | * | |
89a603a0 AP |
411 | * Initialise a timer with the given scale and attributes, |
412 | * and associate it with timer list for given clock @type in @timer_list_group | |
413 | * (or default timer list group, if NULL). | |
ff83c66e AB |
414 | * The caller is responsible for allocating the memory. |
415 | * | |
416 | * You need not call an explicit deinit call. Simply make | |
417 | * sure it is not on a list with timer_del. | |
418 | */ | |
89a603a0 AP |
419 | void timer_init_full(QEMUTimer *ts, |
420 | QEMUTimerListGroup *timer_list_group, QEMUClockType type, | |
421 | int scale, int attributes, | |
422 | QEMUTimerCB *cb, void *opaque); | |
ff83c66e | 423 | |
65a81af8 PB |
424 | /** |
425 | * timer_init: | |
04ecbb78 | 426 | * @ts: the timer to be initialised |
65a81af8 PB |
427 | * @type: the clock to associate with the timer |
428 | * @scale: the scale value for the timer | |
429 | * @cb: the callback to call when the timer expires | |
430 | * @opaque: the opaque pointer to pass to the callback | |
431 | * | |
432 | * Initialize a timer with the given scale on the default timer list | |
433 | * associated with the clock. | |
89a603a0 | 434 | * See timer_init_full for details. |
65a81af8 PB |
435 | */ |
436 | static inline void timer_init(QEMUTimer *ts, QEMUClockType type, int scale, | |
437 | QEMUTimerCB *cb, void *opaque) | |
438 | { | |
89a603a0 | 439 | timer_init_full(ts, NULL, type, scale, 0, cb, opaque); |
65a81af8 PB |
440 | } |
441 | ||
442 | /** | |
443 | * timer_init_ns: | |
04ecbb78 | 444 | * @ts: the timer to be initialised |
65a81af8 PB |
445 | * @type: the clock to associate with the timer |
446 | * @cb: the callback to call when the timer expires | |
447 | * @opaque: the opaque pointer to pass to the callback | |
448 | * | |
449 | * Initialize a timer with nanosecond scale on the default timer list | |
450 | * associated with the clock. | |
89a603a0 | 451 | * See timer_init_full for details. |
65a81af8 PB |
452 | */ |
453 | static inline void timer_init_ns(QEMUTimer *ts, QEMUClockType type, | |
454 | QEMUTimerCB *cb, void *opaque) | |
455 | { | |
456 | timer_init(ts, type, SCALE_NS, cb, opaque); | |
457 | } | |
458 | ||
459 | /** | |
460 | * timer_init_us: | |
04ecbb78 | 461 | * @ts: the timer to be initialised |
65a81af8 PB |
462 | * @type: the clock to associate with the timer |
463 | * @cb: the callback to call when the timer expires | |
464 | * @opaque: the opaque pointer to pass to the callback | |
465 | * | |
466 | * Initialize a timer with microsecond scale on the default timer list | |
467 | * associated with the clock. | |
89a603a0 | 468 | * See timer_init_full for details. |
65a81af8 PB |
469 | */ |
470 | static inline void timer_init_us(QEMUTimer *ts, QEMUClockType type, | |
471 | QEMUTimerCB *cb, void *opaque) | |
472 | { | |
473 | timer_init(ts, type, SCALE_US, cb, opaque); | |
474 | } | |
475 | ||
476 | /** | |
477 | * timer_init_ms: | |
04ecbb78 | 478 | * @ts: the timer to be initialised |
65a81af8 PB |
479 | * @type: the clock to associate with the timer |
480 | * @cb: the callback to call when the timer expires | |
481 | * @opaque: the opaque pointer to pass to the callback | |
482 | * | |
483 | * Initialize a timer with millisecond scale on the default timer list | |
484 | * associated with the clock. | |
89a603a0 | 485 | * See timer_init_full for details. |
65a81af8 PB |
486 | */ |
487 | static inline void timer_init_ms(QEMUTimer *ts, QEMUClockType type, | |
488 | QEMUTimerCB *cb, void *opaque) | |
489 | { | |
490 | timer_init(ts, type, SCALE_MS, cb, opaque); | |
491 | } | |
492 | ||
ff83c66e | 493 | /** |
89a603a0 AP |
494 | * timer_new_full: |
495 | * @timer_list_group: (optional) the timer list group to attach the timer to | |
496 | * @type: the clock type to use | |
dc9fc1ca | 497 | * @scale: the scale value for the timer |
89a603a0 | 498 | * @attributes: 0, or one or more OR'ed QEMU_TIMER_ATTR_<id> values |
ff83c66e AB |
499 | * @cb: the callback to be called when the timer expires |
500 | * @opaque: the opaque pointer to be passed to the callback | |
501 | * | |
89a603a0 AP |
502 | * Create a new timer with the given scale and attributes, |
503 | * and associate it with timer list for given clock @type in @timer_list_group | |
504 | * (or default timer list group, if NULL). | |
ff83c66e AB |
505 | * The memory is allocated by the function. |
506 | * | |
507 | * This is not the preferred interface unless you know you | |
89a603a0 AP |
508 | * are going to call timer_free. Use timer_init or timer_init_full instead. |
509 | * | |
510 | * The default timer list has one special feature: in icount mode, | |
511 | * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread. This is | |
512 | * not true of other timer lists, which are typically associated | |
513 | * with an AioContext---each of them runs its timer callbacks in its own | |
514 | * AioContext thread. | |
ff83c66e AB |
515 | * |
516 | * Returns: a pointer to the timer | |
517 | */ | |
89a603a0 AP |
518 | static inline QEMUTimer *timer_new_full(QEMUTimerListGroup *timer_list_group, |
519 | QEMUClockType type, | |
520 | int scale, int attributes, | |
521 | QEMUTimerCB *cb, void *opaque) | |
ff83c66e AB |
522 | { |
523 | QEMUTimer *ts = g_malloc0(sizeof(QEMUTimer)); | |
89a603a0 | 524 | timer_init_full(ts, timer_list_group, type, scale, attributes, cb, opaque); |
ff83c66e AB |
525 | return ts; |
526 | } | |
527 | ||
a3a726ae AB |
528 | /** |
529 | * timer_new: | |
530 | * @type: the clock type to use | |
dc9fc1ca | 531 | * @scale: the scale value for the timer |
a3a726ae AB |
532 | * @cb: the callback to be called when the timer expires |
533 | * @opaque: the opaque pointer to be passed to the callback | |
534 | * | |
89a603a0 AP |
535 | * Create a new timer with the given scale, |
536 | * and associate it with the default timer list for the clock type @type. | |
537 | * See timer_new_full for details. | |
6b8f0187 | 538 | * |
a3a726ae AB |
539 | * Returns: a pointer to the timer |
540 | */ | |
541 | static inline QEMUTimer *timer_new(QEMUClockType type, int scale, | |
542 | QEMUTimerCB *cb, void *opaque) | |
543 | { | |
89a603a0 | 544 | return timer_new_full(NULL, type, scale, 0, cb, opaque); |
a3a726ae AB |
545 | } |
546 | ||
54904d2a | 547 | /** |
40daca54 | 548 | * timer_new_ns: |
04ecbb78 C |
549 | * @type: the clock type to associate with the timer |
550 | * @cb: the callback to call when the timer expires | |
40daca54 | 551 | * @opaque: the opaque pointer to pass to the callback |
54904d2a | 552 | * |
40daca54 AB |
553 | * Create a new timer with nanosecond scale on the default timer list |
554 | * associated with the clock. | |
89a603a0 | 555 | * See timer_new_full for details. |
6b8f0187 | 556 | * |
40daca54 | 557 | * Returns: a pointer to the newly created timer |
ff83c66e | 558 | */ |
40daca54 AB |
559 | static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb, |
560 | void *opaque) | |
ff83c66e | 561 | { |
40daca54 | 562 | return timer_new(type, SCALE_NS, cb, opaque); |
ff83c66e AB |
563 | } |
564 | ||
54904d2a | 565 | /** |
40daca54 | 566 | * timer_new_us: |
04ecbb78 C |
567 | * @type: the clock type to associate with the timer |
568 | * @cb: the callback to call when the timer expires | |
40daca54 | 569 | * @opaque: the opaque pointer to pass to the callback |
54904d2a | 570 | * |
40daca54 AB |
571 | * Create a new timer with microsecond scale on the default timer list |
572 | * associated with the clock. | |
89a603a0 | 573 | * See timer_new_full for details. |
54904d2a | 574 | * |
40daca54 | 575 | * Returns: a pointer to the newly created timer |
54904d2a | 576 | */ |
40daca54 AB |
577 | static inline QEMUTimer *timer_new_us(QEMUClockType type, QEMUTimerCB *cb, |
578 | void *opaque) | |
579 | { | |
580 | return timer_new(type, SCALE_US, cb, opaque); | |
581 | } | |
54904d2a | 582 | |
ff83c66e | 583 | /** |
40daca54 | 584 | * timer_new_ms: |
04ecbb78 C |
585 | * @type: the clock type to associate with the timer |
586 | * @cb: the callback to call when the timer expires | |
40daca54 | 587 | * @opaque: the opaque pointer to pass to the callback |
ff83c66e | 588 | * |
40daca54 AB |
589 | * Create a new timer with millisecond scale on the default timer list |
590 | * associated with the clock. | |
89a603a0 | 591 | * See timer_new_full for details. |
40daca54 AB |
592 | * |
593 | * Returns: a pointer to the newly created timer | |
ff83c66e | 594 | */ |
40daca54 AB |
595 | static inline QEMUTimer *timer_new_ms(QEMUClockType type, QEMUTimerCB *cb, |
596 | void *opaque) | |
ff83c66e | 597 | { |
40daca54 | 598 | return timer_new(type, SCALE_MS, cb, opaque); |
ff83c66e AB |
599 | } |
600 | ||
cd1bd53a PB |
601 | /** |
602 | * timer_deinit: | |
603 | * @ts: the timer to be de-initialised | |
604 | * | |
605 | * Deassociate the timer from any timerlist. You should | |
606 | * call timer_del before. After this call, any further | |
607 | * timer_del call cannot cause dangling pointer accesses | |
608 | * even if the previously used timerlist is freed. | |
609 | */ | |
610 | void timer_deinit(QEMUTimer *ts); | |
611 | ||
54904d2a | 612 | /** |
40daca54 AB |
613 | * timer_free: |
614 | * @ts: the timer | |
54904d2a | 615 | * |
40daca54 | 616 | * Free a timer (it must not be on the active list) |
54904d2a | 617 | */ |
e703dcba MAL |
618 | static inline void timer_free(QEMUTimer *ts) |
619 | { | |
620 | g_free(ts); | |
621 | } | |
54904d2a | 622 | |
ff83c66e | 623 | /** |
40daca54 AB |
624 | * timer_del: |
625 | * @ts: the timer | |
ff83c66e | 626 | * |
40daca54 | 627 | * Delete a timer from the active list. |
978f2205 SH |
628 | * |
629 | * This function is thread-safe but the timer and its timer list must not be | |
630 | * freed while this function is running. | |
ff83c66e | 631 | */ |
40daca54 | 632 | void timer_del(QEMUTimer *ts); |
ff83c66e | 633 | |
54904d2a | 634 | /** |
40daca54 AB |
635 | * timer_mod_ns: |
636 | * @ts: the timer | |
637 | * @expire_time: the expiry time in nanoseconds | |
54904d2a | 638 | * |
40daca54 | 639 | * Modify a timer to expire at @expire_time |
978f2205 SH |
640 | * |
641 | * This function is thread-safe but the timer and its timer list must not be | |
642 | * freed while this function is running. | |
54904d2a | 643 | */ |
40daca54 | 644 | void timer_mod_ns(QEMUTimer *ts, int64_t expire_time); |
54904d2a | 645 | |
add40e97 PB |
646 | /** |
647 | * timer_mod_anticipate_ns: | |
648 | * @ts: the timer | |
649 | * @expire_time: the expiry time in nanoseconds | |
650 | * | |
651 | * Modify a timer to expire at @expire_time or the current time, | |
652 | * whichever comes earlier. | |
653 | * | |
654 | * This function is thread-safe but the timer and its timer list must not be | |
655 | * freed while this function is running. | |
656 | */ | |
657 | void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time); | |
658 | ||
ff83c66e AB |
659 | /** |
660 | * timer_mod: | |
40daca54 AB |
661 | * @ts: the timer |
662 | * @expire_time: the expire time in the units associated with the timer | |
ff83c66e | 663 | * |
40daca54 AB |
664 | * Modify a timer to expiry at @expire_time, taking into |
665 | * account the scale associated with the timer. | |
978f2205 SH |
666 | * |
667 | * This function is thread-safe but the timer and its timer list must not be | |
668 | * freed while this function is running. | |
ff83c66e | 669 | */ |
40daca54 | 670 | void timer_mod(QEMUTimer *ts, int64_t expire_timer); |
ff83c66e | 671 | |
add40e97 PB |
672 | /** |
673 | * timer_mod_anticipate: | |
674 | * @ts: the timer | |
420bd566 | 675 | * @expire_time: the expire time in the units associated with the timer |
add40e97 PB |
676 | * |
677 | * Modify a timer to expire at @expire_time or the current time, whichever | |
678 | * comes earlier, taking into account the scale associated with the timer. | |
679 | * | |
680 | * This function is thread-safe but the timer and its timer list must not be | |
681 | * freed while this function is running. | |
682 | */ | |
683 | void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time); | |
684 | ||
54904d2a AB |
685 | /** |
686 | * timer_pending: | |
40daca54 | 687 | * @ts: the timer |
54904d2a AB |
688 | * |
689 | * Determines whether a timer is pending (i.e. is on the | |
690 | * active list of timers, whether or not it has not yet expired). | |
691 | * | |
692 | * Returns: true if the timer is pending | |
693 | */ | |
694 | bool timer_pending(QEMUTimer *ts); | |
695 | ||
696 | /** | |
697 | * timer_expired: | |
40daca54 | 698 | * @ts: the timer |
04ecbb78 | 699 | * @current_time: the current time |
54904d2a AB |
700 | * |
701 | * Determines whether a timer has expired. | |
702 | * | |
703 | * Returns: true if the timer has expired | |
704 | */ | |
705 | bool timer_expired(QEMUTimer *timer_head, int64_t current_time); | |
706 | ||
707 | /** | |
708 | * timer_expire_time_ns: | |
40daca54 | 709 | * @ts: the timer |
54904d2a | 710 | * |
40daca54 | 711 | * Determine the expiry time of a timer |
54904d2a | 712 | * |
40daca54 | 713 | * Returns: the expiry time in nanoseconds |
54904d2a AB |
714 | */ |
715 | uint64_t timer_expire_time_ns(QEMUTimer *ts); | |
716 | ||
f9a976b7 | 717 | /** |
40daca54 AB |
718 | * timer_get: |
719 | * @f: the file | |
720 | * @ts: the timer | |
f9a976b7 | 721 | * |
40daca54 | 722 | * Read a timer @ts from a file @f |
f9a976b7 | 723 | */ |
40daca54 | 724 | void timer_get(QEMUFile *f, QEMUTimer *ts); |
f9a976b7 AB |
725 | |
726 | /** | |
40daca54 AB |
727 | * timer_put: |
728 | * @f: the file | |
729 | * @ts: the timer | |
730 | */ | |
731 | void timer_put(QEMUFile *f, QEMUTimer *ts); | |
732 | ||
733 | /* | |
734 | * General utility functions | |
735 | */ | |
736 | ||
737 | /** | |
738 | * qemu_timeout_ns_to_ms: | |
739 | * @ns: nanosecond timeout value | |
f9a976b7 | 740 | * |
40daca54 AB |
741 | * Convert a nanosecond timeout value (or -1) to |
742 | * a millisecond value (or -1), always rounding up. | |
f9a976b7 | 743 | * |
40daca54 | 744 | * Returns: millisecond timeout value |
f9a976b7 | 745 | */ |
40daca54 | 746 | int qemu_timeout_ns_to_ms(int64_t ns); |
f9a976b7 | 747 | |
54904d2a | 748 | /** |
40daca54 AB |
749 | * qemu_poll_ns: |
750 | * @fds: Array of file descriptors | |
751 | * @nfds: number of file descriptors | |
752 | * @timeout: timeout in nanoseconds | |
54904d2a | 753 | * |
40daca54 AB |
754 | * Perform a poll like g_poll but with a timeout in nanoseconds. |
755 | * See g_poll documentation for further details. | |
756 | * | |
757 | * Returns: number of fds ready | |
54904d2a | 758 | */ |
40daca54 | 759 | int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout); |
70c3b557 | 760 | |
02a03a9f AB |
761 | /** |
762 | * qemu_soonest_timeout: | |
763 | * @timeout1: first timeout in nanoseconds (or -1 for infinite) | |
764 | * @timeout2: second timeout in nanoseconds (or -1 for infinite) | |
765 | * | |
766 | * Calculates the soonest of two timeout values. -1 means infinite, which | |
767 | * is later than any other value. | |
768 | * | |
769 | * Returns: soonest timeout value in nanoseconds (or -1 for infinite) | |
770 | */ | |
771 | static inline int64_t qemu_soonest_timeout(int64_t timeout1, int64_t timeout2) | |
772 | { | |
773 | /* we can abuse the fact that -1 (which means infinite) is a maximal | |
774 | * value when cast to unsigned. As this is disgusting, it's kept in | |
775 | * one inline function. | |
776 | */ | |
777 | return ((uint64_t) timeout1 < (uint64_t) timeout2) ? timeout1 : timeout2; | |
778 | } | |
779 | ||
ff83c66e | 780 | /** |
40daca54 | 781 | * initclocks: |
ff83c66e | 782 | * |
40daca54 AB |
783 | * Initialise the clock & timer infrastructure |
784 | */ | |
3f53bc61 | 785 | void init_clocks(QEMUTimerListNotifyCB *notify_cb); |
40daca54 | 786 | |
fb1a3a05 PD |
787 | static inline int64_t get_max_clock_jump(void) |
788 | { | |
789 | /* This should be small enough to prevent excessive interrupts from being | |
790 | * generated by the RTC on clock jumps, but large enough to avoid frequent | |
791 | * unnecessary resets in idle VMs. | |
792 | */ | |
73bcb24d | 793 | return 60 * NANOSECONDS_PER_SECOND; |
fb1a3a05 PD |
794 | } |
795 | ||
40daca54 AB |
796 | /* |
797 | * Low level clock functions | |
798 | */ | |
87ecb68b | 799 | |
3224e878 | 800 | /* get host real time in nanosecond */ |
c57c846a BS |
801 | static inline int64_t get_clock_realtime(void) |
802 | { | |
803 | struct timeval tv; | |
804 | ||
805 | gettimeofday(&tv, NULL); | |
806 | return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); | |
807 | } | |
808 | ||
809 | /* Warning: don't insert tracepoints into these functions, they are | |
810 | also used by simpletrace backend and tracepoints would cause | |
811 | an infinite recursion! */ | |
812 | #ifdef _WIN32 | |
813 | extern int64_t clock_freq; | |
814 | ||
815 | static inline int64_t get_clock(void) | |
816 | { | |
817 | LARGE_INTEGER ti; | |
818 | QueryPerformanceCounter(&ti); | |
73bcb24d | 819 | return muldiv64(ti.QuadPart, NANOSECONDS_PER_SECOND, clock_freq); |
c57c846a BS |
820 | } |
821 | ||
822 | #else | |
823 | ||
824 | extern int use_rt_clock; | |
825 | ||
826 | static inline int64_t get_clock(void) | |
827 | { | |
c57c846a BS |
828 | if (use_rt_clock) { |
829 | struct timespec ts; | |
830 | clock_gettime(CLOCK_MONOTONIC, &ts); | |
831 | return ts.tv_sec * 1000000000LL + ts.tv_nsec; | |
a284f798 | 832 | } else { |
c57c846a BS |
833 | /* XXX: using gettimeofday leads to problems if the date |
834 | changes, so it should be avoided. */ | |
835 | return get_clock_realtime(); | |
836 | } | |
837 | } | |
838 | #endif | |
db1a4972 | 839 | |
29e922b6 BS |
840 | /*******************************************/ |
841 | /* host CPU ticks (if available) */ | |
842 | ||
843 | #if defined(_ARCH_PPC) | |
844 | ||
4a7428c5 | 845 | static inline int64_t cpu_get_host_ticks(void) |
29e922b6 BS |
846 | { |
847 | int64_t retval; | |
848 | #ifdef _ARCH_PPC64 | |
849 | /* This reads timebase in one 64bit go and includes Cell workaround from: | |
850 | http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html | |
851 | */ | |
852 | __asm__ __volatile__ ("mftb %0\n\t" | |
853 | "cmpwi %0,0\n\t" | |
854 | "beq- $-8" | |
855 | : "=r" (retval)); | |
856 | #else | |
857 | /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */ | |
858 | unsigned long junk; | |
4a9590f3 AG |
859 | __asm__ __volatile__ ("mfspr %1,269\n\t" /* mftbu */ |
860 | "mfspr %L0,268\n\t" /* mftb */ | |
861 | "mfspr %0,269\n\t" /* mftbu */ | |
29e922b6 BS |
862 | "cmpw %0,%1\n\t" |
863 | "bne $-16" | |
864 | : "=r" (retval), "=r" (junk)); | |
865 | #endif | |
866 | return retval; | |
867 | } | |
868 | ||
869 | #elif defined(__i386__) | |
870 | ||
4a7428c5 | 871 | static inline int64_t cpu_get_host_ticks(void) |
29e922b6 BS |
872 | { |
873 | int64_t val; | |
874 | asm volatile ("rdtsc" : "=A" (val)); | |
875 | return val; | |
876 | } | |
877 | ||
878 | #elif defined(__x86_64__) | |
879 | ||
4a7428c5 | 880 | static inline int64_t cpu_get_host_ticks(void) |
29e922b6 BS |
881 | { |
882 | uint32_t low,high; | |
883 | int64_t val; | |
884 | asm volatile("rdtsc" : "=a" (low), "=d" (high)); | |
885 | val = high; | |
886 | val <<= 32; | |
887 | val |= low; | |
888 | return val; | |
889 | } | |
890 | ||
891 | #elif defined(__hppa__) | |
892 | ||
4a7428c5 | 893 | static inline int64_t cpu_get_host_ticks(void) |
29e922b6 BS |
894 | { |
895 | int val; | |
896 | asm volatile ("mfctl %%cr16, %0" : "=r"(val)); | |
897 | return val; | |
898 | } | |
899 | ||
29e922b6 BS |
900 | #elif defined(__s390__) |
901 | ||
4a7428c5 | 902 | static inline int64_t cpu_get_host_ticks(void) |
29e922b6 BS |
903 | { |
904 | int64_t val; | |
905 | asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc"); | |
906 | return val; | |
907 | } | |
908 | ||
9b9c37c3 | 909 | #elif defined(__sparc__) |
29e922b6 | 910 | |
4a7428c5 | 911 | static inline int64_t cpu_get_host_ticks (void) |
29e922b6 BS |
912 | { |
913 | #if defined(_LP64) | |
914 | uint64_t rval; | |
915 | asm volatile("rd %%tick,%0" : "=r"(rval)); | |
916 | return rval; | |
917 | #else | |
9b9c37c3 RH |
918 | /* We need an %o or %g register for this. For recent enough gcc |
919 | there is an "h" constraint for that. Don't bother with that. */ | |
29e922b6 BS |
920 | union { |
921 | uint64_t i64; | |
922 | struct { | |
923 | uint32_t high; | |
924 | uint32_t low; | |
925 | } i32; | |
926 | } rval; | |
9b9c37c3 RH |
927 | asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1" |
928 | : "=r"(rval.i32.high), "=r"(rval.i32.low) : : "g1"); | |
29e922b6 BS |
929 | return rval.i64; |
930 | #endif | |
931 | } | |
932 | ||
933 | #elif defined(__mips__) && \ | |
934 | ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__)) | |
935 | /* | |
936 | * binutils wants to use rdhwr only on mips32r2 | |
937 | * but as linux kernel emulate it, it's fine | |
938 | * to use it. | |
939 | * | |
940 | */ | |
941 | #define MIPS_RDHWR(rd, value) { \ | |
942 | __asm__ __volatile__ (".set push\n\t" \ | |
943 | ".set mips32r2\n\t" \ | |
944 | "rdhwr %0, "rd"\n\t" \ | |
945 | ".set pop" \ | |
946 | : "=r" (value)); \ | |
947 | } | |
948 | ||
4a7428c5 | 949 | static inline int64_t cpu_get_host_ticks(void) |
29e922b6 BS |
950 | { |
951 | /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */ | |
952 | uint32_t count; | |
953 | static uint32_t cyc_per_count = 0; | |
954 | ||
955 | if (!cyc_per_count) { | |
956 | MIPS_RDHWR("$3", cyc_per_count); | |
957 | } | |
958 | ||
959 | MIPS_RDHWR("$2", count); | |
960 | return (int64_t)(count * cyc_per_count); | |
961 | } | |
962 | ||
14a6063a RH |
963 | #elif defined(__alpha__) |
964 | ||
4a7428c5 | 965 | static inline int64_t cpu_get_host_ticks(void) |
14a6063a RH |
966 | { |
967 | uint64_t cc; | |
968 | uint32_t cur, ofs; | |
969 | ||
970 | asm volatile("rpcc %0" : "=r"(cc)); | |
971 | cur = cc; | |
972 | ofs = cc >> 32; | |
973 | return cur - ofs; | |
974 | } | |
975 | ||
29e922b6 BS |
976 | #else |
977 | /* The host CPU doesn't have an easily accessible cycle counter. | |
978 | Just return a monotonically increasing value. This will be | |
979 | totally wrong, but hopefully better than nothing. */ | |
d1bb099f | 980 | static inline int64_t cpu_get_host_ticks(void) |
29e922b6 | 981 | { |
d1bb099f | 982 | return get_clock(); |
29e922b6 BS |
983 | } |
984 | #endif | |
985 | ||
2d8ebcf9 RH |
986 | #ifdef CONFIG_PROFILER |
987 | static inline int64_t profile_getclock(void) | |
988 | { | |
89d5cbdd | 989 | return get_clock(); |
2d8ebcf9 RH |
990 | } |
991 | ||
2d8ebcf9 RH |
992 | extern int64_t dev_time; |
993 | #endif | |
994 | ||
87ecb68b | 995 | #endif |