posix-timers.c 36 KB

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