Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2018 Intel Corporation
3 : : * Copyright (c) 2018 Friedt Professional Engineering Services, Inc
4 : : * Copyright (c) 2025 Tenstorrent AI ULC
5 : : *
6 : : * SPDX-License-Identifier: Apache-2.0
7 : : */
8 : :
9 : : #include <errno.h>
10 : : #include <stdbool.h>
11 : : #include <stddef.h>
12 : : #include <stdint.h>
13 : :
14 : : #include <zephyr/kernel.h>
15 : : #include <zephyr/internal/syscall_handler.h>
16 : : #include <zephyr/sys/clock.h>
17 : : #include <zephyr/sys/div64.h>
18 : : #include <zephyr/sys/timeutil.h>
19 : : #include <zephyr/toolchain.h>
20 : :
21 : : /*
22 : : * `k_uptime_get` returns a timestamp offset on an always increasing
23 : : * value from the system start. To support the `SYS_CLOCK_REALTIME`
24 : : * clock, this `rt_clock_offset` records the time that the system was
25 : : * started. This can either be set via 'sys_clock_settime', or could be
26 : : * set from a real time clock, if such hardware is present.
27 : : */
28 : : static struct timespec rt_clock_offset;
29 : : static struct k_spinlock rt_clock_offset_lock;
30 : :
31 : 0 : static bool is_valid_clock_id(int clock_id)
32 : : {
33 [ # # ]: 0 : switch (clock_id) {
34 : : case SYS_CLOCK_MONOTONIC:
35 : : case SYS_CLOCK_REALTIME:
36 : : return true;
37 : 0 : default:
38 : 0 : return false;
39 : : }
40 : : }
41 : :
42 : 0 : static void timespec_from_ticks(uint64_t ticks, struct timespec *ts)
43 : : {
44 [ # # ]: 0 : uint64_t elapsed_secs = sys_div64_u32_const(ticks, CONFIG_SYS_CLOCK_TICKS_PER_SEC);
45 [ # # ]: 0 : uint64_t nremainder = sys_mod64_u32_const(ticks, CONFIG_SYS_CLOCK_TICKS_PER_SEC);
46 : :
47 : 0 : *ts = (struct timespec){
48 : 0 : .tv_sec = (time_t)elapsed_secs,
49 : : /* For ns 32 bit conversion can be used since its smaller than 1sec. */
50 : 0 : .tv_nsec = (int32_t)k_ticks_to_ns_floor32(nremainder),
51 : : };
52 : 0 : }
53 : :
54 : 0 : int sys_clock_from_clockid(int clock_id)
55 : : {
56 : 0 : switch (clock_id) {
57 : : #if defined(CLOCK_REALTIME) || defined(_POSIX_C_SOURCE)
58 : : case (int)CLOCK_REALTIME:
59 : : return SYS_CLOCK_REALTIME;
60 : : #endif
61 : : #if defined(CLOCK_MONOTONIC) || defined(_POSIX_MONOTONIC_CLOCK)
62 : : case (int)CLOCK_MONOTONIC:
63 : : return SYS_CLOCK_MONOTONIC;
64 : : #endif
65 : : default:
66 : 0 : return -EINVAL;
67 : : }
68 : : }
69 : :
70 : 0 : int sys_clock_gettime(int clock_id, struct timespec *ts)
71 : : {
72 [ # # ]: 0 : if (!is_valid_clock_id(clock_id)) {
73 : : return -EINVAL;
74 : : }
75 : :
76 [ # # # ]: 0 : switch (clock_id) {
77 : 0 : case SYS_CLOCK_REALTIME: {
78 : 0 : struct timespec offset;
79 : :
80 : 0 : timespec_from_ticks(k_uptime_ticks(), ts);
81 : 0 : sys_clock_getrtoffset(&offset);
82 [ # # ]: 0 : if (unlikely(!timespec_add(ts, &offset))) {
83 : : /* Saturate rather than reporting an overflow in 292 billion years */
84 : 0 : *ts = (struct timespec){
85 : : .tv_sec = (time_t)INT64_MAX,
86 : : .tv_nsec = NSEC_PER_SEC - 1,
87 : : };
88 : : }
89 : 0 : } break;
90 : :
91 : 0 : case SYS_CLOCK_MONOTONIC:
92 : 0 : timespec_from_ticks(k_uptime_ticks(), ts);
93 : 0 : break;
94 : :
95 : 0 : default:
96 : 0 : CODE_UNREACHABLE;
97 : : return -EINVAL; /* Should never reach here */
98 : : }
99 : :
100 [ # # ]: 0 : __ASSERT_NO_MSG(timespec_is_valid(ts));
101 : :
102 : : return 0;
103 : : }
104 : :
105 : 0 : void z_impl_sys_clock_getrtoffset(struct timespec *tp)
106 : : {
107 [ # # ]: 0 : __ASSERT_NO_MSG(tp != NULL);
108 : :
109 [ # # ]: 0 : K_SPINLOCK(&rt_clock_offset_lock) {
110 : 0 : *tp = rt_clock_offset;
111 : : }
112 : :
113 [ # # ]: 0 : __ASSERT_NO_MSG(timespec_is_valid(tp));
114 : 0 : }
115 : :
116 : : #ifdef CONFIG_USERSPACE
117 : : void z_vrfy_sys_clock_getrtoffset(struct timespec *tp)
118 : : {
119 : : K_OOPS(K_SYSCALL_MEMORY_WRITE(tp, sizeof(*tp)));
120 : : return z_impl_sys_clock_getrtoffset(tp);
121 : : }
122 : : #include <zephyr/syscalls/sys_clock_getrtoffset_mrsh.c>
123 : : #endif /* CONFIG_USERSPACE */
124 : :
125 : 0 : int z_impl_sys_clock_settime(int clock_id, const struct timespec *tp)
126 : : {
127 : 0 : struct timespec offset;
128 : :
129 [ # # ]: 0 : if (clock_id != SYS_CLOCK_REALTIME) {
130 : : return -EINVAL;
131 : : }
132 : :
133 [ # # ]: 0 : if (!timespec_is_valid(tp)) {
134 : : return -EINVAL;
135 : : }
136 : :
137 : 0 : timespec_from_ticks(k_uptime_ticks(), &offset);
138 : 0 : (void)timespec_negate(&offset);
139 : 0 : (void)timespec_add(&offset, tp);
140 : :
141 [ # # ]: 0 : K_SPINLOCK(&rt_clock_offset_lock) {
142 : 0 : rt_clock_offset = offset;
143 : : }
144 : :
145 : 0 : return 0;
146 : : }
147 : :
148 : : #ifdef CONFIG_USERSPACE
149 : : int z_vrfy_sys_clock_settime(int clock_id, const struct timespec *ts)
150 : : {
151 : : struct timespec ts_copy;
152 : :
153 : : K_OOPS(k_usermode_from_copy(&ts_copy, ts, sizeof(ts_copy)));
154 : : return z_impl_sys_clock_settime(clock_id, &ts_copy);
155 : : }
156 : : #include <zephyr/syscalls/sys_clock_settime_mrsh.c>
157 : : #endif /* CONFIG_USERSPACE */
158 : :
159 : 0 : int z_impl_sys_clock_nanosleep(int clock_id, int flags, const struct timespec *rqtp,
160 : : struct timespec *rmtp)
161 : : {
162 : 0 : k_timepoint_t end;
163 : 0 : k_timeout_t timeout;
164 : 0 : struct timespec duration = {0, 0};
165 : 0 : const bool update_rmtp = rmtp != NULL;
166 : 0 : const bool abstime = (flags & SYS_TIMER_ABSTIME) != 0;
167 : :
168 [ # # ]: 0 : if (!is_valid_clock_id(clock_id)) {
169 : : return -EINVAL;
170 : : }
171 : :
172 [ # # # # ]: 0 : if ((rqtp->tv_sec < 0) || !timespec_is_valid(rqtp)) {
173 : : return -EINVAL;
174 : : }
175 : :
176 [ # # ]: 0 : if (abstime) {
177 : : /* convert absolute time to relative time duration */
178 : 0 : (void)sys_clock_gettime(clock_id, &duration);
179 : 0 : (void)timespec_negate(&duration);
180 : 0 : (void)timespec_add(&duration, rqtp);
181 : : } else {
182 : 0 : duration = *rqtp;
183 : : }
184 : :
185 : : /* sleep for relative time duration */
186 : 0 : if ((sizeof(rqtp->tv_sec) == sizeof(int64_t)) &&
187 : : unlikely(rqtp->tv_sec >= (time_t)(UINT64_MAX / NSEC_PER_SEC))) {
188 : : uint64_t ns = (uint64_t)k_sleep(K_SECONDS(duration.tv_sec - 1)) * NSEC_PER_MSEC;
189 : : struct timespec rem = {
190 : : .tv_sec = (time_t)sys_div64_u32_const(ns, NSEC_PER_SEC),
191 : : .tv_nsec = sys_mod64_u32_const(ns, NSEC_PER_SEC),
192 : : };
193 : :
194 : : duration.tv_sec = 1;
195 : : (void)timespec_add(&duration, &rem);
196 : : }
197 : :
198 : 0 : timeout = timespec_to_timeout(&duration, NULL);
199 : 0 : end = sys_timepoint_calc(timeout);
200 : 0 : do {
201 : 0 : (void)k_sleep(timeout);
202 : 0 : timeout = sys_timepoint_timeout(end);
203 [ # # ]: 0 : } while (!K_TIMEOUT_EQ(timeout, K_NO_WAIT));
204 : :
205 [ # # ]: 0 : if (update_rmtp) {
206 : 0 : *rmtp = (struct timespec){
207 : : .tv_sec = 0,
208 : : .tv_nsec = 0,
209 : : };
210 : : }
211 : :
212 : : return 0;
213 : : }
214 : :
215 : : #ifdef CONFIG_USERSPACE
216 : : int z_vrfy_sys_clock_nanosleep(int clock_id, int flags, const struct timespec *rqtp,
217 : : struct timespec *rmtp)
218 : : {
219 : : struct timespec rqtp_copy;
220 : :
221 : : K_OOPS(k_usermode_from_copy(&rqtp_copy, rqtp, sizeof(rqtp_copy)));
222 : : if (rmtp != NULL) {
223 : : K_OOPS(K_SYSCALL_MEMORY_WRITE(rmtp, sizeof(*rmtp)));
224 : : }
225 : : return z_impl_sys_clock_nanosleep(clock_id, flags, &rqtp_copy, rmtp);
226 : : }
227 : : #include <zephyr/syscalls/sys_clock_nanosleep_mrsh.c>
228 : : #endif /* CONFIG_USERSPACE */
229 : :
230 : : #ifdef CONFIG_ZTEST
231 : : #include <zephyr/ztest.h>
232 : 50 : static void reset_clock_offset(void)
233 : : {
234 [ + + ]: 100 : K_SPINLOCK(&rt_clock_offset_lock) {
235 : 50 : rt_clock_offset = (struct timespec){0};
236 : : }
237 : 50 : }
238 : :
239 : 50 : static void clock_offset_reset_rule_after(const struct ztest_unit_test *test, void *data)
240 : : {
241 : 50 : ARG_UNUSED(test);
242 : 50 : ARG_UNUSED(data);
243 : :
244 : 50 : reset_clock_offset();
245 : 50 : }
246 : :
247 : : ZTEST_RULE(clock_offset_reset_rule, NULL, clock_offset_reset_rule_after);
248 : : #endif /* CONFIG_ZTEST */
|