posix-timers.c 36 KB

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