posix-timers.c 36 KB

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