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