summaryrefslogtreecommitdiff
path: root/components/lwp/vdso/user/common/vdso_clock_gettime.c
blob: eaa7d468fc3d361cb5d691759ec0dc22ee369bbd (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * Copyright (c) 2006-2026 RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author         Notes
 * 2026-04-21     rcitach        init ver.
 */
#include "vdso_user_internal.h"

static int rt_vdso_resolve_clock_index(clockid_t clock, unsigned int *index)
{
    switch (clock)
    {
    case CLOCK_REALTIME:
    case CLOCK_REALTIME_COARSE:
        *index = RT_VDSO_CLOCK_REALTIME_INDEX;
        return 0;

    case CLOCK_MONOTONIC:
    case CLOCK_MONOTONIC_COARSE:
    case CLOCK_MONOTONIC_RAW:
    case CLOCK_BOOTTIME:
        *index = RT_VDSO_CLOCK_MONOTONIC_INDEX;
        return 0;

    default:
        if ((unsigned int)clock >= RT_VDSO_CLOCK_ID_MAX)
        {
            return -EINVAL;
        }
        return -ENOSYS;
    }
}

int rt_vdso_clock_gettime_impl(clockid_t clock, struct timespec *ts)
{
    const struct rt_vdso_data_page *data_page = rt_vdso_get_data_page();
    uint32_t seq;
    unsigned int index;
    uint64_t last;
    uint64_t freq;
    uint64_t now;
    uint64_t delta_ns;
    struct timespec base_time;
    int ret;

    ret = rt_vdso_resolve_clock_index(clock, &index);
    if (ret != 0)
    {
        return ret;
    }

    do
    {
        seq = rt_vdso_data_read_begin(data_page);
        if (index == RT_VDSO_CLOCK_REALTIME_INDEX &&
            !(data_page->flags & RT_VDSO_FLAG_REALTIME_VALID))
        {
            return -ENOSYS;
        }
        base_time = data_page->base_time[index];
        last = data_page->counter_last;
        freq = data_page->counter_freq;
        now = rt_vdso_arch_read_counter();
    } while (unlikely(rt_vdso_data_read_retry(data_page, seq)));

    if (freq == 0)
    {
        return -ENOSYS;
    }

    delta_ns = rt_vdso_counter_delta_to_ns(now, last, freq);
    rt_vdso_timespec_add_nanoseconds(&base_time, delta_ns);
    *ts = base_time;

    return 0;
}

int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
    __attribute__((alias("rt_vdso_clock_gettime_impl"), visibility("default")));

int __kernel_clock_gettime(clockid_t clock, struct timespec *ts)
    __attribute__((alias("rt_vdso_clock_gettime_impl"), visibility("default")));