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