]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - kernel/time/posix-timers.c
alarmtimer: Switch over to generic set/get/rearm routine
[mirror_ubuntu-artful-kernel.git] / kernel / time / posix-timers.c
CommitLineData
1da177e4 1/*
f30c2269 2 * linux/kernel/posix-timers.c
1da177e4
LT
3 *
4 *
5 * 2002-10-15 Posix Clocks & timers
6 * by George Anzinger george@mvista.com
7 *
8 * Copyright (C) 2002 2003 by MontaVista Software.
9 *
10 * 2004-06-01 Fix CLOCK_REALTIME clock/timer TIMER_ABSTIME bug.
11 * Copyright (C) 2004 Boris Hu
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or (at
16 * your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 *
27 * MontaVista Software | 1237 East Arques Avenue | Sunnyvale | CA 94085 | USA
28 */
29
30/* These are all the functions necessary to implement
31 * POSIX clocks & timers
32 */
33#include <linux/mm.h>
1da177e4
LT
34#include <linux/interrupt.h>
35#include <linux/slab.h>
36#include <linux/time.h>
97d1f15b 37#include <linux/mutex.h>
61855b6b 38#include <linux/sched/task.h>
1da177e4 39
7c0f6ba6 40#include <linux/uaccess.h>
1da177e4
LT
41#include <linux/list.h>
42#include <linux/init.h>
43#include <linux/compiler.h>
5ed67f05 44#include <linux/hash.h>
0606f422 45#include <linux/posix-clock.h>
1da177e4
LT
46#include <linux/posix-timers.h>
47#include <linux/syscalls.h>
48#include <linux/wait.h>
49#include <linux/workqueue.h>
9984de1a 50#include <linux/export.h>
5ed67f05 51#include <linux/hashtable.h>
1da177e4 52
8b094cd0 53#include "timekeeping.h"
bab0aae9 54#include "posix-timers.h"
8b094cd0 55
1da177e4 56/*
5ed67f05
PE
57 * Management arrays for POSIX timers. Timers are now kept in static hash table
58 * with 512 entries.
59 * Timer ids are allocated by local routine, which selects proper hash head by
60 * key, constructed from current->signal address and per signal struct counter.
61 * This keeps timer ids unique per process, but now they can intersect between
62 * processes.
1da177e4
LT
63 */
64
65/*
66 * Lets keep our timers in a slab cache :-)
67 */
e18b890b 68static struct kmem_cache *posix_timers_cache;
5ed67f05
PE
69
70static DEFINE_HASHTABLE(posix_timers_hashtable, 9);
71static DEFINE_SPINLOCK(hash_lock);
1da177e4 72
6631fa12
TG
73static const struct k_clock * const posix_clocks[];
74static const struct k_clock *clockid_to_kclock(const clockid_t id);
75
1da177e4
LT
76/*
77 * we assume that the new SIGEV_THREAD_ID shares no bits with the other
78 * SIGEV values. Here we put out an error if this assumption fails.
79 */
80#if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
81 ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
82#error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
83#endif
84
65da528d
TG
85/*
86 * parisc wants ENOTSUP instead of EOPNOTSUPP
87 */
88#ifndef ENOTSUP
89# define ENANOSLEEP_NOTSUP EOPNOTSUPP
90#else
91# define ENANOSLEEP_NOTSUP ENOTSUP
92#endif
1da177e4
LT
93
94/*
95 * The timer ID is turned into a timer address by idr_find().
96 * Verifying a valid ID consists of:
97 *
98 * a) checking that idr_find() returns other than -1.
99 * b) checking that the timer id matches the one in the timer itself.
100 * c) that the timer owner is in the callers thread group.
101 */
102
103/*
104 * CLOCKs: The POSIX standard calls for a couple of clocks and allows us
105 * to implement others. This structure defines the various
0061748d 106 * clocks.
1da177e4
LT
107 *
108 * RESOLUTION: Clock resolution is used to round up timer and interval
109 * times, NOT to report clock times, which are reported with as
110 * much resolution as the system can muster. In some cases this
111 * resolution may depend on the underlying clock hardware and
112 * may not be quantifiable until run time, and only then is the
113 * necessary code is written. The standard says we should say
114 * something about this issue in the documentation...
115 *
0061748d
RC
116 * FUNCTIONS: The CLOCKs structure defines possible functions to
117 * handle various clock functions.
1da177e4 118 *
0061748d
RC
119 * The standard POSIX timer management code assumes the
120 * following: 1.) The k_itimer struct (sched.h) is used for
121 * the timer. 2.) The list, it_lock, it_clock, it_id and
122 * it_pid fields are not modified by timer code.
1da177e4
LT
123 *
124 * Permissions: It is assumed that the clock_settime() function defined
125 * for each clock will take care of permission checks. Some
126 * clocks may be set able by any user (i.e. local process
127 * clocks) others not. Currently the only set able clock we
128 * have is CLOCK_REALTIME and its high res counter part, both of
129 * which we beg off on and pass to do_sys_settimeofday().
130 */
20f33a03
NK
131static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags);
132
133#define lock_timer(tid, flags) \
134({ struct k_itimer *__timr; \
135 __cond_lock(&__timr->it_lock, __timr = __lock_timer(tid, flags)); \
136 __timr; \
137})
1da177e4 138
5ed67f05
PE
139static int hash(struct signal_struct *sig, unsigned int nr)
140{
141 return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable));
142}
143
144static struct k_itimer *__posix_timers_find(struct hlist_head *head,
145 struct signal_struct *sig,
146 timer_t id)
147{
5ed67f05
PE
148 struct k_itimer *timer;
149
150 hlist_for_each_entry_rcu(timer, head, t_hash) {
151 if ((timer->it_signal == sig) && (timer->it_id == id))
152 return timer;
153 }
154 return NULL;
155}
156
157static struct k_itimer *posix_timer_by_id(timer_t id)
158{
159 struct signal_struct *sig = current->signal;
160 struct hlist_head *head = &posix_timers_hashtable[hash(sig, id)];
161
162 return __posix_timers_find(head, sig, id);
163}
164
165static int posix_timer_add(struct k_itimer *timer)
166{
167 struct signal_struct *sig = current->signal;
168 int first_free_id = sig->posix_timer_id;
169 struct hlist_head *head;
170 int ret = -ENOENT;
171
172 do {
173 spin_lock(&hash_lock);
174 head = &posix_timers_hashtable[hash(sig, sig->posix_timer_id)];
175 if (!__posix_timers_find(head, sig, sig->posix_timer_id)) {
176 hlist_add_head_rcu(&timer->t_hash, head);
177 ret = sig->posix_timer_id;
178 }
179 if (++sig->posix_timer_id < 0)
180 sig->posix_timer_id = 0;
181 if ((sig->posix_timer_id == first_free_id) && (ret == -ENOENT))
182 /* Loop over all possible ids completed */
183 ret = -EAGAIN;
184 spin_unlock(&hash_lock);
185 } while (ret == -ENOENT);
186 return ret;
187}
188
1da177e4
LT
189static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
190{
191 spin_unlock_irqrestore(&timr->it_lock, flags);
192}
193
42285777 194/* Get clock_realtime */
3c9c12f4 195static int posix_clock_realtime_get(clockid_t which_clock, struct timespec64 *tp)
42285777 196{
3c9c12f4 197 ktime_get_real_ts64(tp);
42285777
TG
198 return 0;
199}
200
26f9a479
TG
201/* Set clock_realtime */
202static int posix_clock_realtime_set(const clockid_t which_clock,
0fe6afe3 203 const struct timespec64 *tp)
26f9a479 204{
0fe6afe3 205 return do_sys_settimeofday64(tp, NULL);
26f9a479
TG
206}
207
f1f1d5eb
RC
208static int posix_clock_realtime_adj(const clockid_t which_clock,
209 struct timex *t)
210{
211 return do_adjtimex(t);
212}
213
becf8b5d
TG
214/*
215 * Get monotonic time for posix timers
216 */
3c9c12f4 217static int posix_ktime_get_ts(clockid_t which_clock, struct timespec64 *tp)
becf8b5d 218{
3c9c12f4 219 ktime_get_ts64(tp);
becf8b5d
TG
220 return 0;
221}
1da177e4 222
2d42244a 223/*
7fdd7f89 224 * Get monotonic-raw time for posix timers
2d42244a 225 */
3c9c12f4 226static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp)
2d42244a 227{
3c9c12f4 228 getrawmonotonic64(tp);
2d42244a
JS
229 return 0;
230}
231
da15cfda 232
3c9c12f4 233static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec64 *tp)
da15cfda 234{
3c9c12f4 235 *tp = current_kernel_time64();
da15cfda
JS
236 return 0;
237}
238
239static int posix_get_monotonic_coarse(clockid_t which_clock,
3c9c12f4 240 struct timespec64 *tp)
da15cfda 241{
3c9c12f4 242 *tp = get_monotonic_coarse64();
da15cfda
JS
243 return 0;
244}
245
d2e3e0ca 246static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 *tp)
da15cfda 247{
d2e3e0ca 248 *tp = ktime_to_timespec64(KTIME_LOW_RES);
da15cfda
JS
249 return 0;
250}
7fdd7f89 251
3c9c12f4 252static int posix_get_boottime(const clockid_t which_clock, struct timespec64 *tp)
7fdd7f89 253{
3c9c12f4 254 get_monotonic_boottime64(tp);
7fdd7f89
JS
255 return 0;
256}
257
3c9c12f4 258static int posix_get_tai(clockid_t which_clock, struct timespec64 *tp)
1ff3c967 259{
3c9c12f4 260 timekeeping_clocktai64(tp);
1ff3c967
JS
261 return 0;
262}
7fdd7f89 263
d2e3e0ca 264static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
056a3cac
TG
265{
266 tp->tv_sec = 0;
267 tp->tv_nsec = hrtimer_resolution;
268 return 0;
269}
270
1da177e4
LT
271/*
272 * Initialize everything, well, just everything in Posix clocks/timers ;)
273 */
274static __init int init_posix_timers(void)
275{
1da177e4 276 posix_timers_cache = kmem_cache_create("posix_timers_cache",
040b5c6f
AD
277 sizeof (struct k_itimer), 0, SLAB_PANIC,
278 NULL);
1da177e4
LT
279 return 0;
280}
1da177e4
LT
281__initcall(init_posix_timers);
282
f37fb0aa 283static void common_hrtimer_rearm(struct k_itimer *timr)
1da177e4 284{
44f21475
RZ
285 struct hrtimer *timer = &timr->it.real.timer;
286
80105cd0 287 if (!timr->it_interval)
1da177e4
LT
288 return;
289
4d672e7a
DL
290 timr->it_overrun += (unsigned int) hrtimer_forward(timer,
291 timer->base->get_time(),
80105cd0 292 timr->it_interval);
44f21475 293 hrtimer_restart(timer);
1da177e4
LT
294}
295
296/*
297 * This function is exported for use by the signal deliver code. It is
298 * called just prior to the info block being released and passes that
299 * block to us. It's function is to update the overrun entry AND to
300 * restart the timer. It should only be called if the timer is to be
301 * restarted (i.e. we have flagged this in the sys_private entry of the
302 * info block).
303 *
25985edc 304 * To protect against the timer going away while the interrupt is queued,
1da177e4
LT
305 * we require that the it_requeue_pending flag be set.
306 */
96fe3b07 307void posixtimer_rearm(struct siginfo *info)
1da177e4
LT
308{
309 struct k_itimer *timr;
310 unsigned long flags;
311
312 timr = lock_timer(info->si_tid, &flags);
af888d67
TG
313 if (!timr)
314 return;
1da177e4 315
af888d67 316 if (timr->it_requeue_pending == info->si_sys_private) {
f37fb0aa 317 timr->kclock->timer_rearm(timr);
1da177e4 318
21e55c1f 319 timr->it_active = 1;
af888d67
TG
320 timr->it_overrun_last = timr->it_overrun;
321 timr->it_overrun = -1;
322 ++timr->it_requeue_pending;
323
54da1174 324 info->si_overrun += timr->it_overrun_last;
becf8b5d
TG
325 }
326
af888d67 327 unlock_timer(timr, flags);
1da177e4
LT
328}
329
ba661292 330int posix_timer_event(struct k_itimer *timr, int si_private)
1da177e4 331{
27af4245
ON
332 struct task_struct *task;
333 int shared, ret = -1;
ba661292
ON
334 /*
335 * FIXME: if ->sigq is queued we can race with
96fe3b07 336 * dequeue_signal()->posixtimer_rearm().
ba661292
ON
337 *
338 * If dequeue_signal() sees the "right" value of
96fe3b07 339 * si_sys_private it calls posixtimer_rearm().
ba661292 340 * We re-queue ->sigq and drop ->it_lock().
96fe3b07 341 * posixtimer_rearm() locks the timer
ba661292
ON
342 * and re-schedules it while ->sigq is pending.
343 * Not really bad, but not that we want.
344 */
1da177e4 345 timr->sigq->info.si_sys_private = si_private;
1da177e4 346
27af4245
ON
347 rcu_read_lock();
348 task = pid_task(timr->it_pid, PIDTYPE_PID);
349 if (task) {
350 shared = !(timr->it_sigev_notify & SIGEV_THREAD_ID);
351 ret = send_sigqueue(timr->sigq, task, shared);
352 }
353 rcu_read_unlock();
4aa73611
ON
354 /* If we failed to send the signal the timer stops. */
355 return ret > 0;
1da177e4 356}
1da177e4
LT
357
358/*
359 * This function gets called when a POSIX.1b interval timer expires. It
360 * is used as a callback from the kernel internal timer. The
361 * run_timer_list code ALWAYS calls with interrupts on.
362
363 * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
364 */
c9cb2e3d 365static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer)
1da177e4 366{
05cfb614 367 struct k_itimer *timr;
1da177e4 368 unsigned long flags;
becf8b5d 369 int si_private = 0;
c9cb2e3d 370 enum hrtimer_restart ret = HRTIMER_NORESTART;
1da177e4 371
05cfb614 372 timr = container_of(timer, struct k_itimer, it.real.timer);
1da177e4 373 spin_lock_irqsave(&timr->it_lock, flags);
1da177e4 374
21e55c1f 375 timr->it_active = 0;
80105cd0 376 if (timr->it_interval != 0)
becf8b5d 377 si_private = ++timr->it_requeue_pending;
1da177e4 378
becf8b5d
TG
379 if (posix_timer_event(timr, si_private)) {
380 /*
381 * signal was not sent because of sig_ignor
382 * we will not get a call back to restart it AND
383 * it should be restarted.
384 */
80105cd0 385 if (timr->it_interval != 0) {
58229a18
TG
386 ktime_t now = hrtimer_cb_get_time(timer);
387
388 /*
389 * FIXME: What we really want, is to stop this
390 * timer completely and restart it in case the
391 * SIG_IGN is removed. This is a non trivial
392 * change which involves sighand locking
393 * (sigh !), which we don't want to do late in
394 * the release cycle.
395 *
396 * For now we just let timers with an interval
397 * less than a jiffie expire every jiffie to
398 * avoid softirq starvation in case of SIG_IGN
399 * and a very small interval, which would put
400 * the timer right back on the softirq pending
401 * list. By moving now ahead of time we trick
402 * hrtimer_forward() to expire the timer
403 * later, while we still maintain the overrun
404 * accuracy, but have some inconsistency in
405 * the timer_gettime() case. This is at least
406 * better than a starved softirq. A more
407 * complex fix which solves also another related
408 * inconsistency is already in the pipeline.
409 */
410#ifdef CONFIG_HIGH_RES_TIMERS
411 {
8b0e1953 412 ktime_t kj = NSEC_PER_SEC / HZ;
58229a18 413
80105cd0 414 if (timr->it_interval < kj)
58229a18
TG
415 now = ktime_add(now, kj);
416 }
417#endif
4d672e7a 418 timr->it_overrun += (unsigned int)
58229a18 419 hrtimer_forward(timer, now,
80105cd0 420 timr->it_interval);
becf8b5d 421 ret = HRTIMER_RESTART;
a0a0c28c 422 ++timr->it_requeue_pending;
21e55c1f 423 timr->it_active = 1;
1da177e4 424 }
1da177e4 425 }
1da177e4 426
becf8b5d
TG
427 unlock_timer(timr, flags);
428 return ret;
429}
1da177e4 430
27af4245 431static struct pid *good_sigevent(sigevent_t * event)
1da177e4
LT
432{
433 struct task_struct *rtn = current->group_leader;
434
435 if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
8dc86af0 436 (!(rtn = find_task_by_vpid(event->sigev_notify_thread_id)) ||
bac0abd6 437 !same_thread_group(rtn, current) ||
1da177e4
LT
438 (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL))
439 return NULL;
440
441 if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
442 ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
443 return NULL;
444
27af4245 445 return task_pid(rtn);
1da177e4
LT
446}
447
1da177e4
LT
448static struct k_itimer * alloc_posix_timer(void)
449{
450 struct k_itimer *tmr;
c3762229 451 tmr = kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL);
1da177e4
LT
452 if (!tmr)
453 return tmr;
1da177e4
LT
454 if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
455 kmem_cache_free(posix_timers_cache, tmr);
aa94fbd5 456 return NULL;
1da177e4 457 }
ba661292 458 memset(&tmr->sigq->info, 0, sizeof(siginfo_t));
1da177e4
LT
459 return tmr;
460}
461
8af08871
ED
462static void k_itimer_rcu_free(struct rcu_head *head)
463{
464 struct k_itimer *tmr = container_of(head, struct k_itimer, it.rcu);
465
466 kmem_cache_free(posix_timers_cache, tmr);
467}
468
1da177e4
LT
469#define IT_ID_SET 1
470#define IT_ID_NOT_SET 0
471static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
472{
473 if (it_id_set) {
474 unsigned long flags;
5ed67f05
PE
475 spin_lock_irqsave(&hash_lock, flags);
476 hlist_del_rcu(&tmr->t_hash);
477 spin_unlock_irqrestore(&hash_lock, flags);
1da177e4 478 }
89992102 479 put_pid(tmr->it_pid);
1da177e4 480 sigqueue_free(tmr->sigq);
8af08871 481 call_rcu(&tmr->it.rcu, k_itimer_rcu_free);
1da177e4
LT
482}
483
838394fb
TG
484static int common_timer_create(struct k_itimer *new_timer)
485{
486 hrtimer_init(&new_timer->it.real.timer, new_timer->it_clock, 0);
487 return 0;
488}
489
1da177e4
LT
490/* Create a POSIX.1b interval timer. */
491
362e9c07
HC
492SYSCALL_DEFINE3(timer_create, const clockid_t, which_clock,
493 struct sigevent __user *, timer_event_spec,
494 timer_t __user *, created_timer_id)
1da177e4 495{
d3ba5a9a 496 const struct k_clock *kc = clockid_to_kclock(which_clock);
2cd499e3 497 struct k_itimer *new_timer;
ef864c95 498 int error, new_timer_id;
1da177e4
LT
499 sigevent_t event;
500 int it_id_set = IT_ID_NOT_SET;
501
838394fb 502 if (!kc)
1da177e4 503 return -EINVAL;
838394fb
TG
504 if (!kc->timer_create)
505 return -EOPNOTSUPP;
1da177e4
LT
506
507 new_timer = alloc_posix_timer();
508 if (unlikely(!new_timer))
509 return -EAGAIN;
510
511 spin_lock_init(&new_timer->it_lock);
5ed67f05
PE
512 new_timer_id = posix_timer_add(new_timer);
513 if (new_timer_id < 0) {
514 error = new_timer_id;
1da177e4
LT
515 goto out;
516 }
517
518 it_id_set = IT_ID_SET;
519 new_timer->it_id = (timer_t) new_timer_id;
520 new_timer->it_clock = which_clock;
d97bb75d 521 new_timer->kclock = kc;
1da177e4 522 new_timer->it_overrun = -1;
1da177e4 523
1da177e4
LT
524 if (timer_event_spec) {
525 if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
526 error = -EFAULT;
527 goto out;
528 }
36b2f046 529 rcu_read_lock();
89992102 530 new_timer->it_pid = get_pid(good_sigevent(&event));
36b2f046 531 rcu_read_unlock();
89992102 532 if (!new_timer->it_pid) {
1da177e4
LT
533 error = -EINVAL;
534 goto out;
535 }
536 } else {
6891c450 537 memset(&event.sigev_value, 0, sizeof(event.sigev_value));
5a9fa730
ON
538 event.sigev_notify = SIGEV_SIGNAL;
539 event.sigev_signo = SIGALRM;
540 event.sigev_value.sival_int = new_timer->it_id;
89992102 541 new_timer->it_pid = get_pid(task_tgid(current));
1da177e4
LT
542 }
543
5a9fa730
ON
544 new_timer->it_sigev_notify = event.sigev_notify;
545 new_timer->sigq->info.si_signo = event.sigev_signo;
546 new_timer->sigq->info.si_value = event.sigev_value;
717835d9 547 new_timer->sigq->info.si_tid = new_timer->it_id;
5a9fa730 548 new_timer->sigq->info.si_code = SI_TIMER;
717835d9 549
2b08de00
AV
550 if (copy_to_user(created_timer_id,
551 &new_timer_id, sizeof (new_timer_id))) {
552 error = -EFAULT;
553 goto out;
554 }
555
838394fb 556 error = kc->timer_create(new_timer);
45e0fffc
AV
557 if (error)
558 goto out;
559
36b2f046 560 spin_lock_irq(&current->sighand->siglock);
27af4245 561 new_timer->it_signal = current->signal;
36b2f046
ON
562 list_add(&new_timer->list, &current->signal->posix_timers);
563 spin_unlock_irq(&current->sighand->siglock);
ef864c95
ON
564
565 return 0;
838394fb 566 /*
1da177e4
LT
567 * In the case of the timer belonging to another task, after
568 * the task is unlocked, the timer is owned by the other task
569 * and may cease to exist at any time. Don't use or modify
570 * new_timer after the unlock call.
571 */
1da177e4 572out:
ef864c95 573 release_posix_timer(new_timer, it_id_set);
1da177e4
LT
574 return error;
575}
576
1da177e4
LT
577/*
578 * Locking issues: We need to protect the result of the id look up until
579 * we get the timer locked down so it is not deleted under us. The
580 * removal is done under the idr spinlock so we use that here to bridge
581 * the find to the timer lock. To avoid a dead lock, the timer id MUST
582 * be release with out holding the timer lock.
583 */
20f33a03 584static struct k_itimer *__lock_timer(timer_t timer_id, unsigned long *flags)
1da177e4
LT
585{
586 struct k_itimer *timr;
8af08871 587
e182bb38
TH
588 /*
589 * timer_t could be any type >= int and we want to make sure any
590 * @timer_id outside positive int range fails lookup.
591 */
592 if ((unsigned long long)timer_id > INT_MAX)
593 return NULL;
594
8af08871 595 rcu_read_lock();
5ed67f05 596 timr = posix_timer_by_id(timer_id);
1da177e4 597 if (timr) {
8af08871 598 spin_lock_irqsave(&timr->it_lock, *flags);
89992102 599 if (timr->it_signal == current->signal) {
8af08871 600 rcu_read_unlock();
31d92845
ON
601 return timr;
602 }
8af08871 603 spin_unlock_irqrestore(&timr->it_lock, *flags);
31d92845 604 }
8af08871 605 rcu_read_unlock();
1da177e4 606
31d92845 607 return NULL;
1da177e4
LT
608}
609
91d57bae
TG
610static ktime_t common_hrtimer_remaining(struct k_itimer *timr, ktime_t now)
611{
612 struct hrtimer *timer = &timr->it.real.timer;
613
614 return __hrtimer_expires_remaining_adjusted(timer, now);
615}
616
617static int common_hrtimer_forward(struct k_itimer *timr, ktime_t now)
618{
619 struct hrtimer *timer = &timr->it.real.timer;
620
621 return (int)hrtimer_forward(timer, now, timr->it_interval);
622}
623
1da177e4
LT
624/*
625 * Get the time remaining on a POSIX.1b interval timer. This function
626 * is ALWAYS called with spin_lock_irq on the timer, thus it must not
627 * mess with irq.
628 *
629 * We have a couple of messes to clean up here. First there is the case
630 * of a timer that has a requeue pending. These timers should appear to
631 * be in the timer list with an expiry as if we were to requeue them
632 * now.
633 *
634 * The second issue is the SIGEV_NONE timer which may be active but is
635 * not really ever put in the timer list (to save system resources).
636 * This timer may be expired, and if so, we will do it here. Otherwise
637 * it is the same as a requeue pending timer WRT to what we should
638 * report.
639 */
f2c45807 640void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
1da177e4 641{
91d57bae 642 const struct k_clock *kc = timr->kclock;
3b98a532 643 ktime_t now, remaining, iv;
91d57bae
TG
644 struct timespec64 ts64;
645 bool sig_none;
1da177e4 646
91d57bae 647 sig_none = (timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE;
80105cd0 648 iv = timr->it_interval;
3b98a532 649
becf8b5d 650 /* interval timer ? */
91d57bae 651 if (iv) {
5f252b32 652 cur_setting->it_interval = ktime_to_timespec64(iv);
91d57bae
TG
653 } else if (!timr->it_active) {
654 /*
655 * SIGEV_NONE oneshot timers are never queued. Check them
656 * below.
657 */
658 if (!sig_none)
659 return;
660 }
3b98a532 661
91d57bae
TG
662 /*
663 * The timespec64 based conversion is suboptimal, but it's not
664 * worth to implement yet another callback.
665 */
666 kc->clock_get(timr->it_clock, &ts64);
667 now = timespec64_to_ktime(ts64);
3b98a532 668
becf8b5d 669 /*
91d57bae
TG
670 * When a requeue is pending or this is a SIGEV_NONE timer move the
671 * expiry time forward by intervals, so expiry is > now.
becf8b5d 672 */
91d57bae
TG
673 if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none))
674 timr->it_overrun += kc->timer_forward(timr, now);
3b98a532 675
91d57bae 676 remaining = kc->timer_remaining(timr, now);
becf8b5d 677 /* Return 0 only, when the timer is expired and not pending */
2456e855 678 if (remaining <= 0) {
3b98a532
RZ
679 /*
680 * A single shot SIGEV_NONE timer must return 0, when
681 * it is expired !
682 */
91d57bae 683 if (!sig_none)
3b98a532 684 cur_setting->it_value.tv_nsec = 1;
91d57bae 685 } else {
5f252b32 686 cur_setting->it_value = ktime_to_timespec64(remaining);
91d57bae 687 }
1da177e4
LT
688}
689
690/* Get the time remaining on a POSIX.1b interval timer. */
362e9c07
HC
691SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
692 struct itimerspec __user *, setting)
1da177e4 693{
5f252b32 694 struct itimerspec64 cur_setting64;
1da177e4 695 struct itimerspec cur_setting;
a7319fa2 696 struct k_itimer *timr;
d3ba5a9a 697 const struct k_clock *kc;
1da177e4 698 unsigned long flags;
a7319fa2 699 int ret = 0;
1da177e4
LT
700
701 timr = lock_timer(timer_id, &flags);
702 if (!timr)
703 return -EINVAL;
704
eabdec04 705 memset(&cur_setting64, 0, sizeof(cur_setting64));
d97bb75d 706 kc = timr->kclock;
a7319fa2
TG
707 if (WARN_ON_ONCE(!kc || !kc->timer_get))
708 ret = -EINVAL;
709 else
5f252b32 710 kc->timer_get(timr, &cur_setting64);
1da177e4
LT
711
712 unlock_timer(timr, flags);
713
5f252b32 714 cur_setting = itimerspec64_to_itimerspec(&cur_setting64);
a7319fa2 715 if (!ret && copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
1da177e4
LT
716 return -EFAULT;
717
a7319fa2 718 return ret;
1da177e4 719}
becf8b5d 720
1da177e4
LT
721/*
722 * Get the number of overruns of a POSIX.1b interval timer. This is to
723 * be the overrun of the timer last delivered. At the same time we are
724 * accumulating overruns on the next timer. The overrun is frozen when
725 * the signal is delivered, either at the notify time (if the info block
726 * is not queued) or at the actual delivery time (as we are informed by
96fe3b07 727 * the call back to posixtimer_rearm(). So all we need to do is
1da177e4
LT
728 * to pick up the frozen overrun.
729 */
362e9c07 730SYSCALL_DEFINE1(timer_getoverrun, timer_t, timer_id)
1da177e4
LT
731{
732 struct k_itimer *timr;
733 int overrun;
5ba25331 734 unsigned long flags;
1da177e4
LT
735
736 timr = lock_timer(timer_id, &flags);
737 if (!timr)
738 return -EINVAL;
739
740 overrun = timr->it_overrun_last;
741 unlock_timer(timr, flags);
742
743 return overrun;
744}
1da177e4 745
eae1c4ae
TG
746static void common_hrtimer_arm(struct k_itimer *timr, ktime_t expires,
747 bool absolute, bool sigev_none)
748{
749 struct hrtimer *timer = &timr->it.real.timer;
750 enum hrtimer_mode mode;
751
752 mode = absolute ? HRTIMER_MODE_ABS : HRTIMER_MODE_REL;
753 hrtimer_init(&timr->it.real.timer, timr->it_clock, mode);
754 timr->it.real.timer.function = posix_timer_fn;
755
756 if (!absolute)
757 expires = ktime_add_safe(expires, timer->base->get_time());
758 hrtimer_set_expires(timer, expires);
759
760 if (!sigev_none)
761 hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
762}
763
764static int common_hrtimer_try_to_cancel(struct k_itimer *timr)
765{
766 return hrtimer_try_to_cancel(&timr->it.real.timer);
767}
768
1da177e4 769/* Set a POSIX.1b interval timer. */
f2c45807
TG
770int common_timer_set(struct k_itimer *timr, int flags,
771 struct itimerspec64 *new_setting,
772 struct itimerspec64 *old_setting)
1da177e4 773{
eae1c4ae
TG
774 const struct k_clock *kc = timr->kclock;
775 bool sigev_none;
776 ktime_t expires;
1da177e4
LT
777
778 if (old_setting)
779 common_timer_get(timr, old_setting);
780
eae1c4ae 781 /* Prevent rearming by clearing the interval */
80105cd0 782 timr->it_interval = 0;
1da177e4 783 /*
eae1c4ae
TG
784 * Careful here. On SMP systems the timer expiry function could be
785 * active and spinning on timr->it_lock.
1da177e4 786 */
eae1c4ae 787 if (kc->timer_try_to_cancel(timr) < 0)
1da177e4 788 return TIMER_RETRY;
1da177e4 789
21e55c1f
TG
790 timr->it_active = 0;
791 timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
1da177e4
LT
792 ~REQUEUE_PENDING;
793 timr->it_overrun_last = 0;
1da177e4 794
eae1c4ae 795 /* Switch off the timer when it_value is zero */
becf8b5d
TG
796 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec)
797 return 0;
1da177e4 798
80105cd0 799 timr->it_interval = timespec64_to_ktime(new_setting->it_interval);
eae1c4ae
TG
800 expires = timespec64_to_ktime(new_setting->it_value);
801 sigev_none = (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE;
becf8b5d 802
eae1c4ae
TG
803 kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none);
804 timr->it_active = !sigev_none;
1da177e4
LT
805 return 0;
806}
807
808/* Set a POSIX.1b interval timer */
362e9c07
HC
809SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
810 const struct itimerspec __user *, new_setting,
811 struct itimerspec __user *, old_setting)
1da177e4 812{
5f252b32
DD
813 struct itimerspec64 new_spec64, old_spec64;
814 struct itimerspec64 *rtn = old_setting ? &old_spec64 : NULL;
1da177e4 815 struct itimerspec new_spec, old_spec;
5f252b32 816 struct k_itimer *timr;
5ba25331 817 unsigned long flag;
d3ba5a9a 818 const struct k_clock *kc;
5f252b32 819 int error = 0;
1da177e4
LT
820
821 if (!new_setting)
822 return -EINVAL;
823
824 if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
825 return -EFAULT;
5f252b32 826 new_spec64 = itimerspec_to_itimerspec64(&new_spec);
1da177e4 827
5f252b32
DD
828 if (!timespec64_valid(&new_spec64.it_interval) ||
829 !timespec64_valid(&new_spec64.it_value))
1da177e4
LT
830 return -EINVAL;
831retry:
832 timr = lock_timer(timer_id, &flag);
833 if (!timr)
834 return -EINVAL;
835
d97bb75d 836 kc = timr->kclock;
27722df1
TG
837 if (WARN_ON_ONCE(!kc || !kc->timer_set))
838 error = -EINVAL;
839 else
5f252b32 840 error = kc->timer_set(timr, flags, &new_spec64, rtn);
1da177e4
LT
841
842 unlock_timer(timr, flag);
843 if (error == TIMER_RETRY) {
844 rtn = NULL; // We already got the old time...
845 goto retry;
846 }
847
5f252b32 848 old_spec = itimerspec64_to_itimerspec(&old_spec64);
becf8b5d
TG
849 if (old_setting && !error &&
850 copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
1da177e4
LT
851 error = -EFAULT;
852
853 return error;
854}
855
f2c45807 856int common_timer_del(struct k_itimer *timer)
1da177e4 857{
eae1c4ae 858 const struct k_clock *kc = timer->kclock;
f972be33 859
eae1c4ae
TG
860 timer->it_interval = 0;
861 if (kc->timer_try_to_cancel(timer) < 0)
1da177e4 862 return TIMER_RETRY;
21e55c1f 863 timer->it_active = 0;
1da177e4
LT
864 return 0;
865}
866
867static inline int timer_delete_hook(struct k_itimer *timer)
868{
d97bb75d 869 const struct k_clock *kc = timer->kclock;
6761c670
TG
870
871 if (WARN_ON_ONCE(!kc || !kc->timer_del))
872 return -EINVAL;
873 return kc->timer_del(timer);
1da177e4
LT
874}
875
876/* Delete a POSIX.1b interval timer. */
362e9c07 877SYSCALL_DEFINE1(timer_delete, timer_t, timer_id)
1da177e4
LT
878{
879 struct k_itimer *timer;
5ba25331 880 unsigned long flags;
1da177e4 881
1da177e4 882retry_delete:
1da177e4
LT
883 timer = lock_timer(timer_id, &flags);
884 if (!timer)
885 return -EINVAL;
886
becf8b5d 887 if (timer_delete_hook(timer) == TIMER_RETRY) {
1da177e4
LT
888 unlock_timer(timer, flags);
889 goto retry_delete;
890 }
becf8b5d 891
1da177e4
LT
892 spin_lock(&current->sighand->siglock);
893 list_del(&timer->list);
894 spin_unlock(&current->sighand->siglock);
895 /*
896 * This keeps any tasks waiting on the spin lock from thinking
897 * they got something (see the lock code above).
898 */
89992102 899 timer->it_signal = NULL;
4b7a1304 900
1da177e4
LT
901 unlock_timer(timer, flags);
902 release_posix_timer(timer, IT_ID_SET);
903 return 0;
904}
becf8b5d 905
1da177e4
LT
906/*
907 * return timer owned by the process, used by exit_itimers
908 */
858119e1 909static void itimer_delete(struct k_itimer *timer)
1da177e4
LT
910{
911 unsigned long flags;
912
1da177e4 913retry_delete:
1da177e4
LT
914 spin_lock_irqsave(&timer->it_lock, flags);
915
becf8b5d 916 if (timer_delete_hook(timer) == TIMER_RETRY) {
1da177e4
LT
917 unlock_timer(timer, flags);
918 goto retry_delete;
919 }
1da177e4
LT
920 list_del(&timer->list);
921 /*
922 * This keeps any tasks waiting on the spin lock from thinking
923 * they got something (see the lock code above).
924 */
89992102 925 timer->it_signal = NULL;
4b7a1304 926
1da177e4
LT
927 unlock_timer(timer, flags);
928 release_posix_timer(timer, IT_ID_SET);
929}
930
931/*
25f407f0 932 * This is called by do_exit or de_thread, only when there are no more
1da177e4
LT
933 * references to the shared signal_struct.
934 */
935void exit_itimers(struct signal_struct *sig)
936{
937 struct k_itimer *tmr;
938
939 while (!list_empty(&sig->posix_timers)) {
940 tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
941 itimer_delete(tmr);
942 }
943}
944
362e9c07
HC
945SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
946 const struct timespec __user *, tp)
1da177e4 947{
d3ba5a9a 948 const struct k_clock *kc = clockid_to_kclock(which_clock);
0fe6afe3 949 struct timespec64 new_tp64;
1da177e4
LT
950 struct timespec new_tp;
951
26f9a479 952 if (!kc || !kc->clock_set)
1da177e4 953 return -EINVAL;
26f9a479 954
1da177e4
LT
955 if (copy_from_user(&new_tp, tp, sizeof (*tp)))
956 return -EFAULT;
0fe6afe3 957 new_tp64 = timespec_to_timespec64(new_tp);
1da177e4 958
0fe6afe3 959 return kc->clock_set(which_clock, &new_tp64);
1da177e4
LT
960}
961
362e9c07
HC
962SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
963 struct timespec __user *,tp)
1da177e4 964{
d3ba5a9a 965 const struct k_clock *kc = clockid_to_kclock(which_clock);
3c9c12f4 966 struct timespec64 kernel_tp64;
1da177e4
LT
967 struct timespec kernel_tp;
968 int error;
969
42285777 970 if (!kc)
1da177e4 971 return -EINVAL;
42285777 972
3c9c12f4
DD
973 error = kc->clock_get(which_clock, &kernel_tp64);
974 kernel_tp = timespec64_to_timespec(kernel_tp64);
42285777 975
1da177e4
LT
976 if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp)))
977 error = -EFAULT;
978
979 return error;
1da177e4
LT
980}
981
f1f1d5eb
RC
982SYSCALL_DEFINE2(clock_adjtime, const clockid_t, which_clock,
983 struct timex __user *, utx)
984{
d3ba5a9a 985 const struct k_clock *kc = clockid_to_kclock(which_clock);
f1f1d5eb
RC
986 struct timex ktx;
987 int err;
988
989 if (!kc)
990 return -EINVAL;
991 if (!kc->clock_adj)
992 return -EOPNOTSUPP;
993
994 if (copy_from_user(&ktx, utx, sizeof(ktx)))
995 return -EFAULT;
996
997 err = kc->clock_adj(which_clock, &ktx);
998
f0dbe81f 999 if (err >= 0 && copy_to_user(utx, &ktx, sizeof(ktx)))
f1f1d5eb
RC
1000 return -EFAULT;
1001
1002 return err;
1003}
1004
362e9c07
HC
1005SYSCALL_DEFINE2(clock_getres, const clockid_t, which_clock,
1006 struct timespec __user *, tp)
1da177e4 1007{
d3ba5a9a 1008 const struct k_clock *kc = clockid_to_kclock(which_clock);
d2e3e0ca 1009 struct timespec64 rtn_tp64;
1da177e4
LT
1010 struct timespec rtn_tp;
1011 int error;
1012
e5e542ee 1013 if (!kc)
1da177e4
LT
1014 return -EINVAL;
1015
d2e3e0ca
DD
1016 error = kc->clock_getres(which_clock, &rtn_tp64);
1017 rtn_tp = timespec64_to_timespec(rtn_tp64);
1da177e4 1018
e5e542ee 1019 if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp)))
1da177e4 1020 error = -EFAULT;
1da177e4
LT
1021
1022 return error;
1023}
1024
97735f25
TG
1025/*
1026 * nanosleep for monotonic and realtime clocks
1027 */
1028static int common_nsleep(const clockid_t which_clock, int flags,
ad196384 1029 struct timespec64 *tsave, struct timespec __user *rmtp)
97735f25 1030{
080344b9
ON
1031 return hrtimer_nanosleep(tsave, rmtp, flags & TIMER_ABSTIME ?
1032 HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
1033 which_clock);
97735f25 1034}
1da177e4 1035
362e9c07
HC
1036SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
1037 const struct timespec __user *, rqtp,
1038 struct timespec __user *, rmtp)
1da177e4 1039{
d3ba5a9a 1040 const struct k_clock *kc = clockid_to_kclock(which_clock);
ad196384 1041 struct timespec64 t64;
1da177e4 1042 struct timespec t;
1da177e4 1043
a5cd2880 1044 if (!kc)
1da177e4 1045 return -EINVAL;
a5cd2880
TG
1046 if (!kc->nsleep)
1047 return -ENANOSLEEP_NOTSUP;
1da177e4
LT
1048
1049 if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
1050 return -EFAULT;
1051
ad196384
DD
1052 t64 = timespec_to_timespec64(t);
1053 if (!timespec64_valid(&t64))
1da177e4
LT
1054 return -EINVAL;
1055
ad196384 1056 return kc->nsleep(which_clock, flags, &t64, rmtp);
1da177e4 1057}
1711ef38 1058
1711ef38
TA
1059/*
1060 * This will restart clock_nanosleep. This is required only by
1061 * compat_clock_nanosleep_restart for now.
1062 */
59bd5bc2 1063long clock_nanosleep_restart(struct restart_block *restart_block)
1711ef38 1064{
ab8177bc 1065 clockid_t which_clock = restart_block->nanosleep.clockid;
d3ba5a9a 1066 const struct k_clock *kc = clockid_to_kclock(which_clock);
59bd5bc2
TG
1067
1068 if (WARN_ON_ONCE(!kc || !kc->nsleep_restart))
1069 return -EINVAL;
1711ef38 1070
59bd5bc2 1071 return kc->nsleep_restart(restart_block);
1711ef38 1072}
6631fa12
TG
1073
1074static const struct k_clock clock_realtime = {
eae1c4ae
TG
1075 .clock_getres = posix_get_hrtimer_res,
1076 .clock_get = posix_clock_realtime_get,
1077 .clock_set = posix_clock_realtime_set,
1078 .clock_adj = posix_clock_realtime_adj,
1079 .nsleep = common_nsleep,
1080 .nsleep_restart = hrtimer_nanosleep_restart,
1081 .timer_create = common_timer_create,
1082 .timer_set = common_timer_set,
1083 .timer_get = common_timer_get,
1084 .timer_del = common_timer_del,
1085 .timer_rearm = common_hrtimer_rearm,
1086 .timer_forward = common_hrtimer_forward,
1087 .timer_remaining = common_hrtimer_remaining,
1088 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1089 .timer_arm = common_hrtimer_arm,
6631fa12
TG
1090};
1091
1092static const struct k_clock clock_monotonic = {
eae1c4ae
TG
1093 .clock_getres = posix_get_hrtimer_res,
1094 .clock_get = posix_ktime_get_ts,
1095 .nsleep = common_nsleep,
1096 .nsleep_restart = hrtimer_nanosleep_restart,
1097 .timer_create = common_timer_create,
1098 .timer_set = common_timer_set,
1099 .timer_get = common_timer_get,
1100 .timer_del = common_timer_del,
1101 .timer_rearm = common_hrtimer_rearm,
1102 .timer_forward = common_hrtimer_forward,
1103 .timer_remaining = common_hrtimer_remaining,
1104 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1105 .timer_arm = common_hrtimer_arm,
6631fa12
TG
1106};
1107
1108static const struct k_clock clock_monotonic_raw = {
eae1c4ae
TG
1109 .clock_getres = posix_get_hrtimer_res,
1110 .clock_get = posix_get_monotonic_raw,
6631fa12
TG
1111};
1112
1113static const struct k_clock clock_realtime_coarse = {
eae1c4ae
TG
1114 .clock_getres = posix_get_coarse_res,
1115 .clock_get = posix_get_realtime_coarse,
6631fa12
TG
1116};
1117
1118static const struct k_clock clock_monotonic_coarse = {
eae1c4ae
TG
1119 .clock_getres = posix_get_coarse_res,
1120 .clock_get = posix_get_monotonic_coarse,
6631fa12
TG
1121};
1122
1123static const struct k_clock clock_tai = {
eae1c4ae
TG
1124 .clock_getres = posix_get_hrtimer_res,
1125 .clock_get = posix_get_tai,
1126 .nsleep = common_nsleep,
1127 .nsleep_restart = hrtimer_nanosleep_restart,
1128 .timer_create = common_timer_create,
1129 .timer_set = common_timer_set,
1130 .timer_get = common_timer_get,
1131 .timer_del = common_timer_del,
1132 .timer_rearm = common_hrtimer_rearm,
1133 .timer_forward = common_hrtimer_forward,
1134 .timer_remaining = common_hrtimer_remaining,
1135 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1136 .timer_arm = common_hrtimer_arm,
6631fa12
TG
1137};
1138
1139static const struct k_clock clock_boottime = {
eae1c4ae
TG
1140 .clock_getres = posix_get_hrtimer_res,
1141 .clock_get = posix_get_boottime,
1142 .nsleep = common_nsleep,
1143 .nsleep_restart = hrtimer_nanosleep_restart,
1144 .timer_create = common_timer_create,
1145 .timer_set = common_timer_set,
1146 .timer_get = common_timer_get,
1147 .timer_del = common_timer_del,
1148 .timer_rearm = common_hrtimer_rearm,
1149 .timer_forward = common_hrtimer_forward,
1150 .timer_remaining = common_hrtimer_remaining,
1151 .timer_try_to_cancel = common_hrtimer_try_to_cancel,
1152 .timer_arm = common_hrtimer_arm,
6631fa12
TG
1153};
1154
1155static const struct k_clock * const posix_clocks[] = {
1156 [CLOCK_REALTIME] = &clock_realtime,
1157 [CLOCK_MONOTONIC] = &clock_monotonic,
1158 [CLOCK_PROCESS_CPUTIME_ID] = &clock_process,
1159 [CLOCK_THREAD_CPUTIME_ID] = &clock_thread,
1160 [CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw,
1161 [CLOCK_REALTIME_COARSE] = &clock_realtime_coarse,
1162 [CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse,
1163 [CLOCK_BOOTTIME] = &clock_boottime,
1164 [CLOCK_REALTIME_ALARM] = &alarm_clock,
1165 [CLOCK_BOOTTIME_ALARM] = &alarm_clock,
1166 [CLOCK_TAI] = &clock_tai,
1167};
1168
1169static const struct k_clock *clockid_to_kclock(const clockid_t id)
1170{
1171 if (id < 0)
1172 return (id & CLOCKFD_MASK) == CLOCKFD ?
1173 &clock_posix_dynamic : &clock_posix_cpu;
1174
1175 if (id >= ARRAY_SIZE(posix_clocks) || !posix_clocks[id])
1176 return NULL;
1177 return posix_clocks[id];
1178}