posix-timers.c 36 KB

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