|
| 1 | +/* |
| 2 | + * Copyright (C) <2018> Intel Corporation |
| 3 | + * SPDX-License-Identifier: BSD-3-Clause |
| 4 | + */ |
| 5 | + |
| 6 | +#include <stdio.h> |
| 7 | +#include <stdbool.h> |
| 8 | +#include <unistd.h> |
| 9 | +#include <sys/timerfd.h> |
| 10 | + |
| 11 | +#include "vmmapi.h" |
| 12 | +#include "mevent.h" |
| 13 | +#include "timer.h" |
| 14 | + |
| 15 | +/* We can use timerfd and epoll mechanism to emulate kinds of timers like |
| 16 | + * PIT/RTC/WDT/PMTIMER/... in device model under Linux. |
| 17 | + * Compare with sigevent mechanism, timerfd has a advantage that it could |
| 18 | + * avoid race condition on resource accessing in the async sigev thread. |
| 19 | + * |
| 20 | + * Please note timerfd and epoll are all Linux specific. If the code need to be |
| 21 | + * ported to other OS, we can modify the api with POSIX timers and sigevent |
| 22 | + * mechanism. |
| 23 | + */ |
| 24 | + |
| 25 | +static void |
| 26 | +timer_handler(int fd __attribute__((unused)), |
| 27 | + enum ev_type t __attribute__((unused)), |
| 28 | + void *arg) |
| 29 | +{ |
| 30 | + struct acrn_timer *timer = arg; |
| 31 | + uint64_t buf; |
| 32 | + int32_t size; |
| 33 | + |
| 34 | + if (timer == NULL) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + /* Consume I/O event for default EPOLLLT type. |
| 39 | + * Here is a temporary solution, the processing could be moved to |
| 40 | + * mevent.c once EVF_TIMER is supported. |
| 41 | + */ |
| 42 | + size = read(timer->fd, &buf, sizeof(buf)); |
| 43 | + if (size < 1) { |
| 44 | + fprintf(stderr, "acrn_timer read timerfd error!"); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + if (timer->callback != NULL) { |
| 49 | + (*timer->callback)(timer->callback_param); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +int32_t |
| 54 | +acrn_timer_init(struct acrn_timer *timer, void (*cb)(void *), void *param) |
| 55 | +{ |
| 56 | + if ((timer == NULL) || (cb == NULL)) { |
| 57 | + return -1; |
| 58 | + } |
| 59 | + |
| 60 | + timer->fd = -1; |
| 61 | + if ((timer->clockid == CLOCK_REALTIME) || |
| 62 | + (timer->clockid == CLOCK_MONOTONIC)) { |
| 63 | + timer->fd = timerfd_create(timer->clockid, |
| 64 | + TFD_NONBLOCK | TFD_CLOEXEC); |
| 65 | + } else { |
| 66 | + perror("acrn_timer clockid is not supported.\n"); |
| 67 | + } |
| 68 | + |
| 69 | + if (timer->fd <= 0) { |
| 70 | + perror("acrn_timer create failed.\n"); |
| 71 | + return -1; |
| 72 | + } |
| 73 | + |
| 74 | + timer->mevp = mevent_add(timer->fd, EVF_READ, timer_handler, timer); |
| 75 | + if (timer->mevp == NULL) { |
| 76 | + close(timer->fd); |
| 77 | + perror("acrn_timer mevent add failed.\n"); |
| 78 | + return -1; |
| 79 | + } |
| 80 | + |
| 81 | + timer->callback = cb; |
| 82 | + timer->callback_param = param; |
| 83 | + |
| 84 | + return 0; |
| 85 | +} |
| 86 | + |
| 87 | +void |
| 88 | +acrn_timer_deinit(struct acrn_timer *timer) |
| 89 | +{ |
| 90 | + if (timer == NULL) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + if (timer->mevp != NULL) { |
| 95 | + mevent_delete_close(timer->mevp); |
| 96 | + timer->mevp = NULL; |
| 97 | + } |
| 98 | + |
| 99 | + timer->fd = -1; |
| 100 | + timer->callback = NULL; |
| 101 | + timer->callback_param = NULL; |
| 102 | +} |
| 103 | + |
| 104 | +int32_t |
| 105 | +acrn_timer_settime(struct acrn_timer *timer, struct itimerspec *new_value) |
| 106 | +{ |
| 107 | + if (timer == NULL) { |
| 108 | + return -1; |
| 109 | + } |
| 110 | + |
| 111 | + return timerfd_settime(timer->fd, 0, new_value, NULL); |
| 112 | +} |
| 113 | + |
| 114 | +int32_t |
| 115 | +acrn_timer_gettime(struct acrn_timer *timer, struct itimerspec *cur_value) |
| 116 | +{ |
| 117 | + if (timer == NULL) { |
| 118 | + return -1; |
| 119 | + } |
| 120 | + |
| 121 | + return timerfd_gettime(timer->fd, cur_value); |
| 122 | +} |
0 commit comments