posix-timers.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  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 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 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 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
  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. COMPAT_SYSCALL_DEFINE2(clock_adjtime, clockid_t, which_clock,
  979. struct compat_timex __user *, utp)
  980. {
  981. const struct k_clock *kc = clockid_to_kclock(which_clock);
  982. struct timex ktx;
  983. int err;
  984. if (!kc)
  985. return -EINVAL;
  986. if (!kc->clock_adj)
  987. return -EOPNOTSUPP;
  988. err = compat_get_timex(&ktx, utp);
  989. if (err)
  990. return err;
  991. err = kc->clock_adj(which_clock, &ktx);
  992. if (err >= 0)
  993. err = compat_put_timex(utp, &ktx);
  994. return err;
  995. }
  996. COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock,
  997. struct compat_timespec __user *, tp)
  998. {
  999. const struct k_clock *kc = clockid_to_kclock(which_clock);
  1000. struct timespec64 ts;
  1001. int err;
  1002. if (!kc)
  1003. return -EINVAL;
  1004. err = kc->clock_getres(which_clock, &ts);
  1005. if (!err && tp && compat_put_timespec64(&ts, tp))
  1006. return -EFAULT;
  1007. return err;
  1008. }
  1009. #endif
  1010. /*
  1011. * nanosleep for monotonic and realtime clocks
  1012. */
  1013. static int common_nsleep(const clockid_t which_clock, int flags,
  1014. const struct timespec64 *rqtp)
  1015. {
  1016. return hrtimer_nanosleep(rqtp, flags & TIMER_ABSTIME ?
  1017. HRTIMER_MODE_ABS : HRTIMER_MODE_REL,
  1018. which_clock);
  1019. }
  1020. SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
  1021. const struct timespec __user *, rqtp,
  1022. struct timespec __user *, rmtp)
  1023. {
  1024. const struct k_clock *kc = clockid_to_kclock(which_clock);
  1025. struct timespec64 t;
  1026. if (!kc)
  1027. return -EINVAL;
  1028. if (!kc->nsleep)
  1029. return -ENANOSLEEP_NOTSUP;
  1030. if (get_timespec64(&t, rqtp))
  1031. return -EFAULT;
  1032. if (!timespec64_valid(&t))
  1033. return -EINVAL;
  1034. if (flags & TIMER_ABSTIME)
  1035. rmtp = NULL;
  1036. current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
  1037. current->restart_block.nanosleep.rmtp = rmtp;
  1038. return kc->nsleep(which_clock, flags, &t);
  1039. }
  1040. #ifdef CONFIG_COMPAT
  1041. COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
  1042. struct compat_timespec __user *, rqtp,
  1043. struct compat_timespec __user *, rmtp)
  1044. {
  1045. const struct k_clock *kc = clockid_to_kclock(which_clock);
  1046. struct timespec64 t;
  1047. if (!kc)
  1048. return -EINVAL;
  1049. if (!kc->nsleep)
  1050. return -ENANOSLEEP_NOTSUP;
  1051. if (compat_get_timespec64(&t, rqtp))
  1052. return -EFAULT;
  1053. if (!timespec64_valid(&t))
  1054. return -EINVAL;
  1055. if (flags & TIMER_ABSTIME)
  1056. rmtp = NULL;
  1057. current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
  1058. current->restart_block.nanosleep.compat_rmtp = rmtp;
  1059. return kc->nsleep(which_clock, flags, &t);
  1060. }
  1061. #endif
  1062. static const struct k_clock clock_realtime = {
  1063. .clock_getres = posix_get_hrtimer_res,
  1064. .clock_get = posix_clock_realtime_get,
  1065. .clock_set = posix_clock_realtime_set,
  1066. .clock_adj = posix_clock_realtime_adj,
  1067. .nsleep = common_nsleep,
  1068. .timer_create = common_timer_create,
  1069. .timer_set = common_timer_set,
  1070. .timer_get = common_timer_get,
  1071. .timer_del = common_timer_del,
  1072. .timer_rearm = common_hrtimer_rearm,
  1073. .timer_forward = common_hrtimer_forward,
  1074. .timer_remaining = common_hrtimer_remaining,
  1075. .timer_try_to_cancel = common_hrtimer_try_to_cancel,
  1076. .timer_arm = common_hrtimer_arm,
  1077. };
  1078. static const struct k_clock clock_monotonic = {
  1079. .clock_getres = posix_get_hrtimer_res,
  1080. .clock_get = posix_ktime_get_ts,
  1081. .nsleep = common_nsleep,
  1082. .timer_create = common_timer_create,
  1083. .timer_set = common_timer_set,
  1084. .timer_get = common_timer_get,
  1085. .timer_del = common_timer_del,
  1086. .timer_rearm = common_hrtimer_rearm,
  1087. .timer_forward = common_hrtimer_forward,
  1088. .timer_remaining = common_hrtimer_remaining,
  1089. .timer_try_to_cancel = common_hrtimer_try_to_cancel,
  1090. .timer_arm = common_hrtimer_arm,
  1091. };
  1092. static const struct k_clock clock_monotonic_raw = {
  1093. .clock_getres = posix_get_hrtimer_res,
  1094. .clock_get = posix_get_monotonic_raw,
  1095. };
  1096. static const struct k_clock clock_realtime_coarse = {
  1097. .clock_getres = posix_get_coarse_res,
  1098. .clock_get = posix_get_realtime_coarse,
  1099. };
  1100. static const struct k_clock clock_monotonic_coarse = {
  1101. .clock_getres = posix_get_coarse_res,
  1102. .clock_get = posix_get_monotonic_coarse,
  1103. };
  1104. static const struct k_clock clock_tai = {
  1105. .clock_getres = posix_get_hrtimer_res,
  1106. .clock_get = posix_get_tai,
  1107. .nsleep = common_nsleep,
  1108. .timer_create = common_timer_create,
  1109. .timer_set = common_timer_set,
  1110. .timer_get = common_timer_get,
  1111. .timer_del = common_timer_del,
  1112. .timer_rearm = common_hrtimer_rearm,
  1113. .timer_forward = common_hrtimer_forward,
  1114. .timer_remaining = common_hrtimer_remaining,
  1115. .timer_try_to_cancel = common_hrtimer_try_to_cancel,
  1116. .timer_arm = common_hrtimer_arm,
  1117. };
  1118. static const struct k_clock clock_boottime = {
  1119. .clock_getres = posix_get_hrtimer_res,
  1120. .clock_get = posix_get_boottime,
  1121. .nsleep = common_nsleep,
  1122. .timer_create = common_timer_create,
  1123. .timer_set = common_timer_set,
  1124. .timer_get = common_timer_get,
  1125. .timer_del = common_timer_del,
  1126. .timer_rearm = common_hrtimer_rearm,
  1127. .timer_forward = common_hrtimer_forward,
  1128. .timer_remaining = common_hrtimer_remaining,
  1129. .timer_try_to_cancel = common_hrtimer_try_to_cancel,
  1130. .timer_arm = common_hrtimer_arm,
  1131. };
  1132. static const struct k_clock * const posix_clocks[] = {
  1133. [CLOCK_REALTIME] = &clock_realtime,
  1134. [CLOCK_MONOTONIC] = &clock_monotonic,
  1135. [CLOCK_PROCESS_CPUTIME_ID] = &clock_process,
  1136. [CLOCK_THREAD_CPUTIME_ID] = &clock_thread,
  1137. [CLOCK_MONOTONIC_RAW] = &clock_monotonic_raw,
  1138. [CLOCK_REALTIME_COARSE] = &clock_realtime_coarse,
  1139. [CLOCK_MONOTONIC_COARSE] = &clock_monotonic_coarse,
  1140. [CLOCK_BOOTTIME] = &clock_boottime,
  1141. [CLOCK_REALTIME_ALARM] = &alarm_clock,
  1142. [CLOCK_BOOTTIME_ALARM] = &alarm_clock,
  1143. [CLOCK_TAI] = &clock_tai,
  1144. };
  1145. static const struct k_clock *clockid_to_kclock(const clockid_t id)
  1146. {
  1147. clockid_t idx = id;
  1148. if (id < 0) {
  1149. return (id & CLOCKFD_MASK) == CLOCKFD ?
  1150. &clock_posix_dynamic : &clock_posix_cpu;
  1151. }
  1152. if (id >= ARRAY_SIZE(posix_clocks))
  1153. return NULL;
  1154. return posix_clocks[array_index_nospec(idx, ARRAY_SIZE(posix_clocks))];
  1155. }