^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2012-2014 Cisco Systems
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2000 - 2007 Jeff Dike (jdike{addtoit,linux.intel}.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <sys/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <kern_util.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <os.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static timer_t event_high_res_timer = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static inline long long timeval_to_ns(const struct timeval *tv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) return ((long long) tv->tv_sec * UM_NSEC_PER_SEC) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) tv->tv_usec * UM_NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static inline long long timespec_to_ns(const struct timespec *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return ((long long) ts->tv_sec * UM_NSEC_PER_SEC) + ts->tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) long long os_persistent_clock_emulation(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct timespec realtime_tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) clock_gettime(CLOCK_REALTIME, &realtime_tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return timespec_to_ns(&realtime_tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * os_timer_create() - create an new posix (interval) timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int os_timer_create(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) timer_t *t = &event_high_res_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (timer_create(CLOCK_MONOTONIC, NULL, t) == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int os_timer_set_interval(unsigned long long nsecs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct itimerspec its;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) its.it_value.tv_sec = nsecs / UM_NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) its.it_value.tv_nsec = nsecs % UM_NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) its.it_interval.tv_sec = nsecs / UM_NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) its.it_interval.tv_nsec = nsecs % UM_NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (timer_settime(event_high_res_timer, 0, &its, NULL) == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int os_timer_one_shot(unsigned long long nsecs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct itimerspec its = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) .it_value.tv_sec = nsecs / UM_NSEC_PER_SEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) .it_value.tv_nsec = nsecs % UM_NSEC_PER_SEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) .it_interval.tv_sec = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) .it_interval.tv_nsec = 0, // we cheat here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) timer_settime(event_high_res_timer, 0, &its, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * os_timer_disable() - disable the posix (interval) timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) void os_timer_disable(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct itimerspec its;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) memset(&its, 0, sizeof(struct itimerspec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) timer_settime(event_high_res_timer, 0, &its, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) long long os_nsecs(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct timespec ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) clock_gettime(CLOCK_MONOTONIC,&ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return timespec_to_ns(&ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * os_idle_sleep() - sleep for a given time of nsecs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * @nsecs: nanoseconds to sleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) void os_idle_sleep(unsigned long long nsecs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct timespec ts = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) .tv_sec = nsecs / UM_NSEC_PER_SEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) .tv_nsec = nsecs % UM_NSEC_PER_SEC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * Relay the signal if clock_nanosleep is interrupted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) deliver_alarm();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }