1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/mm.h>
34#include <linux/smp_lock.h>
35#include <linux/interrupt.h>
36#include <linux/slab.h>
37#include <linux/time.h>
38
39#include <asm/uaccess.h>
40#include <asm/semaphore.h>
41#include <linux/list.h>
42#include <linux/init.h>
43#include <linux/compiler.h>
44#include <linux/idr.h>
45#include <linux/posix-timers.h>
46#include <linux/wait.h>
47#include <linux/workqueue.h>
48
49#ifndef div_long_long_rem
50#include <asm/div64.h>
51
52#define div_long_long_rem(dividend,divisor,remainder) ({ \
53 u64 result = dividend; \
54 *remainder = do_div(result,divisor); \
55 result; })
56
57#endif
58#define CLOCK_REALTIME_RES TICK_NSEC
59
60static inline u64 mpy_l_X_l_ll(unsigned long mpy1,unsigned long mpy2)
61{
62 return (u64)mpy1 * mpy2;
63}
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85static kmem_cache_t *posix_timers_cache;
86static struct idr posix_timers_id;
87static spinlock_t idr_lock = SPIN_LOCK_UNLOCKED;
88
89
90
91
92
93#define TIMER_INACTIVE 1
94#define TIMER_RETRY 1
95
96#ifdef CONFIG_SMP
97# define timer_active(tmr) \
98 ((tmr)->it_timer.entry.prev != (void *)TIMER_INACTIVE)
99# define set_timer_inactive(tmr) \
100 do { \
101 (tmr)->it_timer.entry.prev = (void *)TIMER_INACTIVE; \
102 } while (0)
103#else
104# define timer_active(tmr) BARFY
105# define set_timer_inactive(tmr) do { } while (0)
106#endif
107
108
109
110
111#if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
112 ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
113#error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
114#endif
115
116
117#define REQUEUE_PENDING 1
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175static struct k_clock posix_clocks[MAX_CLOCKS];
176
177
178
179
180static struct k_clock_abs abs_list = {.list = LIST_HEAD_INIT(abs_list.list),
181 .lock = SPIN_LOCK_UNLOCKED};
182
183#define if_clock_do(clock_fun,alt_fun,parms) \
184 (!clock_fun) ? alt_fun parms : clock_fun parms
185
186#define p_timer_get(clock,a,b) \
187 if_clock_do((clock)->timer_get,do_timer_gettime, (a,b))
188
189#define p_nsleep(clock,a,b,c) \
190 if_clock_do((clock)->nsleep, do_nsleep, (a,b,c))
191
192#define p_timer_del(clock,a) \
193 if_clock_do((clock)->timer_del, do_timer_delete, (a))
194
195void register_posix_clock(int clock_id, struct k_clock *new_clock);
196static int do_posix_gettime(struct k_clock *clock, struct timespec *tp);
197static u64 do_posix_clock_monotonic_gettime_parts(
198 struct timespec *tp, struct timespec *mo);
199int do_posix_clock_monotonic_gettime(struct timespec *tp);
200int do_posix_clock_monotonic_settime(struct timespec *tp);
201static struct k_itimer *lock_timer(timer_t timer_id, unsigned long *flags);
202
203static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
204{
205 spin_unlock_irqrestore(&timr->it_lock, flags);
206}
207
208
209
210
211static __init int init_posix_timers(void)
212{
213 struct k_clock clock_realtime = {.res = CLOCK_REALTIME_RES,
214 .abs_struct = &abs_list
215 };
216 struct k_clock clock_monotonic = {.res = CLOCK_REALTIME_RES,
217 .abs_struct = NULL,
218 .clock_get = do_posix_clock_monotonic_gettime,
219 .clock_set = do_posix_clock_monotonic_settime
220 };
221
222 register_posix_clock(CLOCK_REALTIME, &clock_realtime);
223 register_posix_clock(CLOCK_MONOTONIC, &clock_monotonic);
224
225 posix_timers_cache = kmem_cache_create("posix_timers_cache",
226 sizeof (struct k_itimer), 0, 0, NULL, NULL);
227 idr_init(&posix_timers_id);
228 return 0;
229}
230
231__initcall(init_posix_timers);
232
233static void tstojiffie(struct timespec *tp, int res, u64 *jiff)
234{
235 long sec = tp->tv_sec;
236 long nsec = tp->tv_nsec + res - 1;
237
238 if (nsec > NSEC_PER_SEC) {
239 sec++;
240 nsec -= NSEC_PER_SEC;
241 }
242
243
244
245
246
247
248
249 *jiff = (mpy_l_X_l_ll(sec, SEC_CONVERSION) +
250 (mpy_l_X_l_ll(nsec, NSEC_CONVERSION) >>
251 (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
252}
253
254
255
256
257
258
259
260
261
262
263
264
265
266static long add_clockset_delta(struct k_itimer *timr,
267 struct timespec *new_wall_to)
268{
269 struct timespec delta;
270 int sign = 0;
271 u64 exp;
272
273 set_normalized_timespec(&delta,
274 new_wall_to->tv_sec -
275 timr->wall_to_prev.tv_sec,
276 new_wall_to->tv_nsec -
277 timr->wall_to_prev.tv_nsec);
278 if (likely(!(delta.tv_sec | delta.tv_nsec)))
279 return 0;
280 if (delta.tv_sec < 0) {
281 set_normalized_timespec(&delta,
282 -delta.tv_sec,
283 1 - delta.tv_nsec -
284 posix_clocks[timr->it_clock].res);
285 sign++;
286 }
287 tstojiffie(&delta, posix_clocks[timr->it_clock].res, &exp);
288 timr->wall_to_prev = *new_wall_to;
289 timr->it_timer.expires += (sign ? -exp : exp);
290 return 1;
291}
292
293static void remove_from_abslist(struct k_itimer *timr)
294{
295 if (!list_empty(&timr->abs_timer_entry)) {
296 spin_lock(&abs_list.lock);
297 list_del_init(&timr->abs_timer_entry);
298 spin_unlock(&abs_list.lock);
299 }
300}
301
302static void schedule_next_timer(struct k_itimer *timr)
303{
304 struct timespec new_wall_to;
305 struct now_struct now;
306 unsigned long seq;
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321 if (!timr->it_incr)
322 return;
323
324 do {
325 seq = read_seqbegin(&xtime_lock);
326 new_wall_to = wall_to_monotonic;
327 posix_get_now(&now);
328 } while (read_seqretry(&xtime_lock, seq));
329
330 if (!list_empty(&timr->abs_timer_entry)) {
331 spin_lock(&abs_list.lock);
332 add_clockset_delta(timr, &new_wall_to);
333
334 posix_bump_timer(timr, now);
335
336 spin_unlock(&abs_list.lock);
337 } else {
338 posix_bump_timer(timr, now);
339 }
340 timr->it_overrun_last = timr->it_overrun;
341 timr->it_overrun = -1;
342 ++timr->it_requeue_pending;
343 add_timer(&timr->it_timer);
344}
345
346
347
348
349
350
351
352
353
354
355
356
357void do_schedule_next_timer(struct siginfo *info)
358{
359 struct k_itimer *timr;
360 unsigned long flags;
361
362 timr = lock_timer(info->si_tid, &flags);
363
364 if (!timr || timr->it_requeue_pending != info->si_sys_private)
365 goto exit;
366
367 schedule_next_timer(timr);
368 info->si_overrun = timr->it_overrun_last;
369exit:
370 if (timr)
371 unlock_timer(timr, flags);
372}
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394static void timer_notify_task(struct k_itimer *timr)
395{
396 int ret;
397
398 memset(&timr->sigq->info, 0, sizeof(siginfo_t));
399
400
401
402
403
404
405
406
407
408
409 timr->sigq->info.si_signo = timr->it_sigev_signo;
410 timr->sigq->info.si_errno = 0;
411 timr->sigq->info.si_code = SI_TIMER;
412 timr->sigq->info.si_tid = timr->it_id;
413 timr->sigq->info.si_value = timr->it_sigev_value;
414 if (timr->it_incr)
415 timr->sigq->info.si_sys_private = ++timr->it_requeue_pending;
416 else {
417 remove_from_abslist(timr);
418 }
419
420 if (timr->it_sigev_notify & SIGEV_THREAD_ID) {
421 if (unlikely(timr->it_process->flags & PF_EXITING)) {
422 timr->it_sigev_notify = SIGEV_SIGNAL;
423 put_task_struct(timr->it_process);
424 timr->it_process = timr->it_process->group_leader;
425 goto group;
426 }
427 ret = send_sigqueue(timr->it_sigev_signo, timr->sigq,
428 timr->it_process);
429 }
430 else {
431 group:
432 ret = send_group_sigqueue(timr->it_sigev_signo, timr->sigq,
433 timr->it_process);
434 }
435 if (ret) {
436
437
438
439
440
441 schedule_next_timer(timr);
442 }
443}
444
445
446
447
448
449
450
451
452static void posix_timer_fn(unsigned long __data)
453{
454 struct k_itimer *timr = (struct k_itimer *) __data;
455 unsigned long flags;
456 unsigned long seq;
457 struct timespec delta, new_wall_to;
458 u64 exp = 0;
459 int do_notify = 1;
460
461 spin_lock_irqsave(&timr->it_lock, flags);
462 set_timer_inactive(timr);
463 if (!list_empty(&timr->abs_timer_entry)) {
464 spin_lock(&abs_list.lock);
465 do {
466 seq = read_seqbegin(&xtime_lock);
467 new_wall_to = wall_to_monotonic;
468 } while (read_seqretry(&xtime_lock, seq));
469 set_normalized_timespec(&delta,
470 new_wall_to.tv_sec -
471 timr->wall_to_prev.tv_sec,
472 new_wall_to.tv_nsec -
473 timr->wall_to_prev.tv_nsec);
474 if (likely((delta.tv_sec | delta.tv_nsec ) == 0)) {
475
476 } else if (delta.tv_sec < 0) {
477
478 } else {
479
480 tstojiffie(&delta,
481 posix_clocks[timr->it_clock].res,
482 &exp);
483 timr->wall_to_prev = new_wall_to;
484 timr->it_timer.expires += exp;
485 add_timer(&timr->it_timer);
486 do_notify = 0;
487 }
488 spin_unlock(&abs_list.lock);
489
490 }
491 if (do_notify)
492 timer_notify_task(timr);
493 unlock_timer(timr, flags);
494}
495
496
497static inline struct task_struct * good_sigevent(sigevent_t * event)
498{
499 struct task_struct *rtn = current->group_leader;
500
501 if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
502 (!(rtn = find_task_by_pid(event->sigev_notify_thread_id)) ||
503 rtn->tgid != current->tgid ||
504 (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL))
505 return NULL;
506
507 if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
508 ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
509 return NULL;
510
511 return rtn;
512}
513
514void register_posix_clock(int clock_id, struct k_clock *new_clock)
515{
516 if ((unsigned) clock_id >= MAX_CLOCKS) {
517 printk("POSIX clock register failed for clock_id %d\n",
518 clock_id);
519 return;
520 }
521 posix_clocks[clock_id] = *new_clock;
522}
523
524static struct k_itimer * alloc_posix_timer(void)
525{
526 struct k_itimer *tmr;
527 tmr = kmem_cache_alloc(posix_timers_cache, GFP_KERNEL);
528 if (!tmr)
529 return tmr;
530 memset(tmr, 0, sizeof (struct k_itimer));
531 INIT_LIST_HEAD(&tmr->abs_timer_entry);
532 if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
533 kmem_cache_free(posix_timers_cache, tmr);
534 tmr = NULL;
535 }
536 return tmr;
537}
538
539#define IT_ID_SET 1
540#define IT_ID_NOT_SET 0
541static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
542{
543 if (it_id_set) {
544 unsigned long flags;
545 spin_lock_irqsave(&idr_lock, flags);
546 idr_remove(&posix_timers_id, tmr->it_id);
547 spin_unlock_irqrestore(&idr_lock, flags);
548 }
549 sigqueue_free(tmr->sigq);
550 if (unlikely(tmr->it_process) &&
551 tmr->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
552 put_task_struct(tmr->it_process);
553 kmem_cache_free(posix_timers_cache, tmr);
554}
555
556
557
558asmlinkage long
559sys_timer_create(clockid_t which_clock,
560 struct sigevent __user *timer_event_spec,
561 timer_t __user * created_timer_id)
562{
563 int error = 0;
564 struct k_itimer *new_timer = NULL;
565 int new_timer_id;
566 struct task_struct *process = NULL;
567 unsigned long flags;
568 sigevent_t event;
569 int it_id_set = IT_ID_NOT_SET;
570
571 if ((unsigned) which_clock >= MAX_CLOCKS ||
572 !posix_clocks[which_clock].res)
573 return -EINVAL;
574
575 new_timer = alloc_posix_timer();
576 if (unlikely(!new_timer))
577 return -EAGAIN;
578
579 spin_lock_init(&new_timer->it_lock);
580 retry:
581 if (unlikely(!idr_pre_get(&posix_timers_id, GFP_KERNEL))) {
582 error = -EAGAIN;
583 goto out;
584 }
585 spin_lock_irq(&idr_lock);
586 error = idr_get_new(&posix_timers_id,
587 (void *) new_timer,
588 &new_timer_id);
589 spin_unlock_irq(&idr_lock);
590 if (error == -EAGAIN)
591 goto retry;
592 else if (error) {
593
594
595
596
597 error = -EAGAIN;
598 goto out;
599 }
600
601 it_id_set = IT_ID_SET;
602 new_timer->it_id = (timer_t) new_timer_id;
603 new_timer->it_clock = which_clock;
604 new_timer->it_incr = 0;
605 new_timer->it_overrun = -1;
606 init_timer(&new_timer->it_timer);
607 new_timer->it_timer.expires = 0;
608 new_timer->it_timer.data = (unsigned long) new_timer;
609 new_timer->it_timer.function = posix_timer_fn;
610 set_timer_inactive(new_timer);
611
612
613
614
615
616 if (copy_to_user(created_timer_id,
617 &new_timer_id, sizeof (new_timer_id))) {
618 error = -EFAULT;
619 goto out;
620 }
621 if (timer_event_spec) {
622 if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
623 error = -EFAULT;
624 goto out;
625 }
626 new_timer->it_sigev_notify = event.sigev_notify;
627 new_timer->it_sigev_signo = event.sigev_signo;
628 new_timer->it_sigev_value = event.sigev_value;
629
630 read_lock(&tasklist_lock);
631 if ((process = good_sigevent(&event))) {
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647 spin_lock_irqsave(&process->sighand->siglock, flags);
648 if (!(process->flags & PF_EXITING)) {
649 new_timer->it_process = process;
650 list_add(&new_timer->list,
651 &process->signal->posix_timers);
652 spin_unlock_irqrestore(&process->sighand->siglock, flags);
653 if (new_timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
654 get_task_struct(process);
655 } else {
656 spin_unlock_irqrestore(&process->sighand->siglock, flags);
657 process = NULL;
658 }
659 }
660 read_unlock(&tasklist_lock);
661 if (!process) {
662 error = -EINVAL;
663 goto out;
664 }
665 } else {
666 new_timer->it_sigev_notify = SIGEV_SIGNAL;
667 new_timer->it_sigev_signo = SIGALRM;
668 new_timer->it_sigev_value.sival_int = new_timer->it_id;
669 process = current->group_leader;
670 spin_lock_irqsave(&process->sighand->siglock, flags);
671 new_timer->it_process = process;
672 list_add(&new_timer->list, &process->signal->posix_timers);
673 spin_unlock_irqrestore(&process->sighand->siglock, flags);
674 }
675
676
677
678
679
680
681
682
683out:
684 if (error)
685 release_posix_timer(new_timer, it_id_set);
686
687 return error;
688}
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703static int good_timespec(const struct timespec *ts)
704{
705 if ((!ts) || (ts->tv_sec < 0) ||
706 ((unsigned) ts->tv_nsec >= NSEC_PER_SEC))
707 return 0;
708 return 1;
709}
710
711
712
713
714
715
716
717
718static struct k_itimer * lock_timer(timer_t timer_id, unsigned long *flags)
719{
720 struct k_itimer *timr;
721
722
723
724
725
726
727 spin_lock_irqsave(&idr_lock, *flags);
728 timr = (struct k_itimer *) idr_find(&posix_timers_id, (int) timer_id);
729 if (timr) {
730 spin_lock(&timr->it_lock);
731 spin_unlock(&idr_lock);
732
733 if ((timr->it_id != timer_id) || !(timr->it_process) ||
734 timr->it_process->tgid != current->tgid) {
735 unlock_timer(timr, *flags);
736 timr = NULL;
737 }
738 } else
739 spin_unlock_irqrestore(&idr_lock, *flags);
740
741 return timr;
742}
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760static void
761do_timer_gettime(struct k_itimer *timr, struct itimerspec *cur_setting)
762{
763 unsigned long expires;
764 struct now_struct now;
765
766 do
767 expires = timr->it_timer.expires;
768 while ((volatile long) (timr->it_timer.expires) != expires);
769
770 posix_get_now(&now);
771
772 if (expires &&
773 ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) &&
774 !timr->it_incr &&
775 posix_time_before(&timr->it_timer, &now))
776 timr->it_timer.expires = expires = 0;
777 if (expires) {
778 if (timr->it_requeue_pending & REQUEUE_PENDING ||
779 (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
780 posix_bump_timer(timr, now);
781 expires = timr->it_timer.expires;
782 }
783 else
784 if (!timer_pending(&timr->it_timer))
785 expires = 0;
786 if (expires)
787 expires -= now.jiffies;
788 }
789 jiffies_to_timespec(expires, &cur_setting->it_value);
790 jiffies_to_timespec(timr->it_incr, &cur_setting->it_interval);
791
792 if (cur_setting->it_value.tv_sec < 0) {
793 cur_setting->it_value.tv_nsec = 1;
794 cur_setting->it_value.tv_sec = 0;
795 }
796}
797
798
799asmlinkage long
800sys_timer_gettime(timer_t timer_id, struct itimerspec __user *setting)
801{
802 struct k_itimer *timr;
803 struct itimerspec cur_setting;
804 unsigned long flags;
805
806 timr = lock_timer(timer_id, &flags);
807 if (!timr)
808 return -EINVAL;
809
810 p_timer_get(&posix_clocks[timr->it_clock], timr, &cur_setting);
811
812 unlock_timer(timr, flags);
813
814 if (copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
815 return -EFAULT;
816
817 return 0;
818}
819
820
821
822
823
824
825
826
827
828
829asmlinkage long
830sys_timer_getoverrun(timer_t timer_id)
831{
832 struct k_itimer *timr;
833 int overrun;
834 long flags;
835
836 timr = lock_timer(timer_id, &flags);
837 if (!timr)
838 return -EINVAL;
839
840 overrun = timr->it_overrun_last;
841 unlock_timer(timr, flags);
842
843 return overrun;
844}
845
846
847
848
849
850
851
852
853
854
855static int adjust_abs_time(struct k_clock *clock, struct timespec *tp,
856 int abs, u64 *exp, struct timespec *wall_to)
857{
858 struct timespec now;
859 struct timespec oc = *tp;
860 u64 jiffies_64_f;
861 int rtn =0;
862
863 if (abs) {
864
865
866
867 if (!((clock - &posix_clocks[0]) & ~CLOCKS_MASK)) {
868 jiffies_64_f = do_posix_clock_monotonic_gettime_parts(
869 &now, wall_to);
870
871
872
873 if((clock - &posix_clocks[0]) & CLOCKS_MONO){
874 now.tv_sec += wall_to->tv_sec;
875 now.tv_nsec += wall_to->tv_nsec;
876 }
877 } else {
878
879
880
881 do_posix_gettime(clock, &now);
882 jiffies_64_f = get_jiffies_64();
883 }
884
885
886
887 oc.tv_sec -= now.tv_sec;
888 oc.tv_nsec -= now.tv_nsec;
889
890
891
892 while ((oc.tv_nsec - NSEC_PER_SEC) >= 0) {
893 oc.tv_nsec -= NSEC_PER_SEC;
894 oc.tv_sec++;
895 }
896 while ((oc.tv_nsec) < 0) {
897 oc.tv_nsec += NSEC_PER_SEC;
898 oc.tv_sec--;
899 }
900 }else{
901 jiffies_64_f = get_jiffies_64();
902 }
903
904
905
906 if (oc.tv_sec < 0)
907 oc.tv_sec = oc.tv_nsec = 0;
908 tstojiffie(&oc, clock->res, exp);
909
910
911
912
913
914 if (*exp > ((u64)MAX_JIFFY_OFFSET))
915
916
917
918
919
920
921
922
923 rtn = -EINVAL;
924
925
926
927 *exp += jiffies_64_f;
928 return rtn;
929}
930
931
932
933static inline int
934do_timer_settime(struct k_itimer *timr, int flags,
935 struct itimerspec *new_setting, struct itimerspec *old_setting)
936{
937 struct k_clock *clock = &posix_clocks[timr->it_clock];
938 u64 expire_64;
939
940 if (old_setting)
941 do_timer_gettime(timr, old_setting);
942
943
944 timr->it_incr = 0;
945
946
947
948
949#ifdef CONFIG_SMP
950 if (timer_active(timr) && !del_timer(&timr->it_timer))
951
952
953
954
955
956
957
958
959 return TIMER_RETRY;
960
961 set_timer_inactive(timr);
962#else
963 del_timer(&timr->it_timer);
964#endif
965 remove_from_abslist(timr);
966
967 timr->it_requeue_pending = (timr->it_requeue_pending + 2) &
968 ~REQUEUE_PENDING;
969 timr->it_overrun_last = 0;
970 timr->it_overrun = -1;
971
972
973
974 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec) {
975 timr->it_timer.expires = 0;
976 return 0;
977 }
978
979 if (adjust_abs_time(clock,
980 &new_setting->it_value, flags & TIMER_ABSTIME,
981 &expire_64, &(timr->wall_to_prev))) {
982 return -EINVAL;
983 }
984 timr->it_timer.expires = (unsigned long)expire_64;
985 tstojiffie(&new_setting->it_interval, clock->res, &expire_64);
986 timr->it_incr = (unsigned long)expire_64;
987
988
989
990
991
992 if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE))
993 add_timer(&timr->it_timer);
994
995 if (flags & TIMER_ABSTIME && clock->abs_struct) {
996 spin_lock(&clock->abs_struct->lock);
997 list_add_tail(&(timr->abs_timer_entry),
998 &(clock->abs_struct->list));
999 spin_unlock(&clock->abs_struct->lock);
1000 }
1001 return 0;
1002}
1003
1004
1005asmlinkage long
1006sys_timer_settime(timer_t timer_id, int flags,
1007 const struct itimerspec __user *new_setting,
1008 struct itimerspec __user *old_setting)
1009{
1010 struct k_itimer *timr;
1011 struct itimerspec new_spec, old_spec;
1012 int error = 0;
1013 long flag;
1014 struct itimerspec *rtn = old_setting ? &old_spec : NULL;
1015
1016 if (!new_setting)
1017 return -EINVAL;
1018
1019 if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
1020 return -EFAULT;
1021
1022 if ((!good_timespec(&new_spec.it_interval)) ||
1023 (!good_timespec(&new_spec.it_value)))
1024 return -EINVAL;
1025retry:
1026 timr = lock_timer(timer_id, &flag);
1027 if (!timr)
1028 return -EINVAL;
1029
1030 if (!posix_clocks[timr->it_clock].timer_set)
1031 error = do_timer_settime(timr, flags, &new_spec, rtn);
1032 else
1033 error = posix_clocks[timr->it_clock].timer_set(timr,
1034 flags,
1035 &new_spec, rtn);
1036 unlock_timer(timr, flag);
1037 if (error == TIMER_RETRY) {
1038 rtn = NULL;
1039 goto retry;
1040 }
1041
1042 if (old_setting && !error && copy_to_user(old_setting,
1043 &old_spec, sizeof (old_spec)))
1044 error = -EFAULT;
1045
1046 return error;
1047}
1048
1049static inline int do_timer_delete(struct k_itimer *timer)
1050{
1051 timer->it_incr = 0;
1052#ifdef CONFIG_SMP
1053 if (timer_active(timer) && !del_timer(&timer->it_timer))
1054
1055
1056
1057
1058
1059
1060
1061
1062 return TIMER_RETRY;
1063#else
1064 del_timer(&timer->it_timer);
1065#endif
1066 remove_from_abslist(timer);
1067
1068 return 0;
1069}
1070
1071
1072asmlinkage long
1073sys_timer_delete(timer_t timer_id)
1074{
1075 struct k_itimer *timer;
1076 long flags;
1077
1078#ifdef CONFIG_SMP
1079 int error;
1080retry_delete:
1081#endif
1082 timer = lock_timer(timer_id, &flags);
1083 if (!timer)
1084 return -EINVAL;
1085
1086#ifdef CONFIG_SMP
1087 error = p_timer_del(&posix_clocks[timer->it_clock], timer);
1088
1089 if (error == TIMER_RETRY) {
1090 unlock_timer(timer, flags);
1091 goto retry_delete;
1092 }
1093#else
1094 p_timer_del(&posix_clocks[timer->it_clock], timer);
1095#endif
1096 spin_lock(¤t->sighand->siglock);
1097 list_del(&timer->list);
1098 spin_unlock(¤t->sighand->siglock);
1099
1100
1101
1102
1103 if (timer->it_process) {
1104 if (timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
1105 put_task_struct(timer->it_process);
1106 timer->it_process = NULL;
1107 }
1108 unlock_timer(timer, flags);
1109 release_posix_timer(timer, IT_ID_SET);
1110 return 0;
1111}
1112
1113
1114
1115static inline void itimer_delete(struct k_itimer *timer)
1116{
1117 unsigned long flags;
1118
1119#ifdef CONFIG_SMP
1120 int error;
1121retry_delete:
1122#endif
1123 spin_lock_irqsave(&timer->it_lock, flags);
1124
1125#ifdef CONFIG_SMP
1126 error = p_timer_del(&posix_clocks[timer->it_clock], timer);
1127
1128 if (error == TIMER_RETRY) {
1129 unlock_timer(timer, flags);
1130 goto retry_delete;
1131 }
1132#else
1133 p_timer_del(&posix_clocks[timer->it_clock], timer);
1134#endif
1135 list_del(&timer->list);
1136
1137
1138
1139
1140 if (timer->it_process) {
1141 if (timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
1142 put_task_struct(timer->it_process);
1143 timer->it_process = NULL;
1144 }
1145 unlock_timer(timer, flags);
1146 release_posix_timer(timer, IT_ID_SET);
1147}
1148
1149
1150
1151
1152
1153void exit_itimers(struct signal_struct *sig)
1154{
1155 struct k_itimer *tmr;
1156
1157 while (!list_empty(&sig->posix_timers)) {
1158 tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
1159 itimer_delete(tmr);
1160 }
1161}
1162
1163
1164
1165
1166
1167
1168
1169
1170static int do_posix_gettime(struct k_clock *clock, struct timespec *tp)
1171{
1172 if (clock->clock_get)
1173 return clock->clock_get(tp);
1174
1175 getnstimeofday(tp);
1176 return 0;
1177}
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187static u64 do_posix_clock_monotonic_gettime_parts(
1188 struct timespec *tp, struct timespec *mo)
1189{
1190 u64 jiff;
1191 unsigned int seq;
1192
1193 do {
1194 seq = read_seqbegin(&xtime_lock);
1195 getnstimeofday(tp);
1196 *mo = wall_to_monotonic;
1197 jiff = jiffies_64;
1198
1199 } while(read_seqretry(&xtime_lock, seq));
1200
1201 return jiff;
1202}
1203
1204int do_posix_clock_monotonic_gettime(struct timespec *tp)
1205{
1206 struct timespec wall_to_mono;
1207
1208 do_posix_clock_monotonic_gettime_parts(tp, &wall_to_mono);
1209
1210 tp->tv_sec += wall_to_mono.tv_sec;
1211 tp->tv_nsec += wall_to_mono.tv_nsec;
1212
1213 if ((tp->tv_nsec - NSEC_PER_SEC) > 0) {
1214 tp->tv_nsec -= NSEC_PER_SEC;
1215 tp->tv_sec++;
1216 }
1217 return 0;
1218}
1219
1220int do_posix_clock_monotonic_settime(struct timespec *tp)
1221{
1222 return -EINVAL;
1223}
1224
1225asmlinkage long
1226sys_clock_settime(clockid_t which_clock, const struct timespec __user *tp)
1227{
1228 struct timespec new_tp;
1229
1230 if ((unsigned) which_clock >= MAX_CLOCKS ||
1231 !posix_clocks[which_clock].res)
1232 return -EINVAL;
1233 if (copy_from_user(&new_tp, tp, sizeof (*tp)))
1234 return -EFAULT;
1235 if (posix_clocks[which_clock].clock_set)
1236 return posix_clocks[which_clock].clock_set(&new_tp);
1237
1238 return do_sys_settimeofday(&new_tp, NULL);
1239}
1240
1241asmlinkage long
1242sys_clock_gettime(clockid_t which_clock, struct timespec __user *tp)
1243{
1244 struct timespec rtn_tp;
1245 int error = 0;
1246
1247 if ((unsigned) which_clock >= MAX_CLOCKS ||
1248 !posix_clocks[which_clock].res)
1249 return -EINVAL;
1250
1251 error = do_posix_gettime(&posix_clocks[which_clock], &rtn_tp);
1252
1253 if (!error && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp)))
1254 error = -EFAULT;
1255
1256 return error;
1257
1258}
1259
1260asmlinkage long
1261sys_clock_getres(clockid_t which_clock, struct timespec __user *tp)
1262{
1263 struct timespec rtn_tp;
1264
1265 if ((unsigned) which_clock >= MAX_CLOCKS ||
1266 !posix_clocks[which_clock].res)
1267 return -EINVAL;
1268
1269 rtn_tp.tv_sec = 0;
1270 rtn_tp.tv_nsec = posix_clocks[which_clock].res;
1271 if (tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp)))
1272 return -EFAULT;
1273
1274 return 0;
1275
1276}
1277
1278static void nanosleep_wake_up(unsigned long __data)
1279{
1280 struct task_struct *p = (struct task_struct *) __data;
1281
1282 wake_up_process(p);
1283}
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303static DECLARE_WAIT_QUEUE_HEAD(nanosleep_abs_wqueue);
1304static DECLARE_WORK(clock_was_set_work, (void(*)(void*))clock_was_set, NULL);
1305
1306static DECLARE_MUTEX(clock_was_set_lock);
1307
1308void clock_was_set(void)
1309{
1310 struct k_itimer *timr;
1311 struct timespec new_wall_to;
1312 LIST_HEAD(cws_list);
1313 unsigned long seq;
1314
1315
1316 if (unlikely(in_interrupt())) {
1317 schedule_work(&clock_was_set_work);
1318 return;
1319 }
1320 wake_up_all(&nanosleep_abs_wqueue);
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357 down(&clock_was_set_lock);
1358 spin_lock_irq(&abs_list.lock);
1359 list_splice_init(&abs_list.list, &cws_list);
1360 spin_unlock_irq(&abs_list.lock);
1361 do {
1362 do {
1363 seq = read_seqbegin(&xtime_lock);
1364 new_wall_to = wall_to_monotonic;
1365 } while (read_seqretry(&xtime_lock, seq));
1366
1367 spin_lock_irq(&abs_list.lock);
1368 if (list_empty(&cws_list)) {
1369 spin_unlock_irq(&abs_list.lock);
1370 break;
1371 }
1372 timr = list_entry(cws_list.next, struct k_itimer,
1373 abs_timer_entry);
1374
1375 list_del_init(&timr->abs_timer_entry);
1376 if (add_clockset_delta(timr, &new_wall_to) &&
1377 del_timer(&timr->it_timer))
1378 add_timer(&timr->it_timer);
1379 list_add(&timr->abs_timer_entry, &abs_list.list);
1380 spin_unlock_irq(&abs_list.lock);
1381 } while (1);
1382
1383 up(&clock_was_set_lock);
1384}
1385
1386long clock_nanosleep_restart(struct restart_block *restart_block);
1387
1388extern long do_clock_nanosleep(clockid_t which_clock, int flags,
1389 struct timespec *t);
1390
1391asmlinkage long
1392sys_clock_nanosleep(clockid_t which_clock, int flags,
1393 const struct timespec __user *rqtp,
1394 struct timespec __user *rmtp)
1395{
1396 struct timespec t;
1397 struct restart_block *restart_block =
1398 &(current_thread_info()->restart_block);
1399 int ret;
1400
1401 if ((unsigned) which_clock >= MAX_CLOCKS ||
1402 !posix_clocks[which_clock].res)
1403 return -EINVAL;
1404
1405 if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
1406 return -EFAULT;
1407
1408 if ((unsigned) t.tv_nsec >= NSEC_PER_SEC || t.tv_sec < 0)
1409 return -EINVAL;
1410
1411 ret = do_clock_nanosleep(which_clock, flags, &t);
1412
1413
1414
1415 restart_block->arg1 = (unsigned long)rmtp;
1416
1417 if ((ret == -ERESTART_RESTARTBLOCK) && rmtp &&
1418 copy_to_user(rmtp, &t, sizeof (t)))
1419 return -EFAULT;
1420 return ret;
1421}
1422
1423long
1424do_clock_nanosleep(clockid_t which_clock, int flags, struct timespec *tsave)
1425{
1426 struct timespec t, dum;
1427 struct timer_list new_timer;
1428 DECLARE_WAITQUEUE(abs_wqueue, current);
1429 u64 rq_time = (u64)0;
1430 s64 left;
1431 int abs;
1432 struct restart_block *restart_block =
1433 ¤t_thread_info()->restart_block;
1434
1435 abs_wqueue.flags = 0;
1436 init_timer(&new_timer);
1437 new_timer.expires = 0;
1438 new_timer.data = (unsigned long) current;
1439 new_timer.function = nanosleep_wake_up;
1440 abs = flags & TIMER_ABSTIME;
1441
1442 if (restart_block->fn == clock_nanosleep_restart) {
1443
1444
1445
1446
1447 restart_block->fn = do_no_restart_syscall;
1448
1449 rq_time = restart_block->arg3;
1450 rq_time = (rq_time << 32) + restart_block->arg2;
1451 if (!rq_time)
1452 return -EINTR;
1453 left = rq_time - get_jiffies_64();
1454 if (left <= (s64)0)
1455 return 0;
1456 }
1457
1458 if (abs && (posix_clocks[which_clock].clock_get !=
1459 posix_clocks[CLOCK_MONOTONIC].clock_get))
1460 add_wait_queue(&nanosleep_abs_wqueue, &abs_wqueue);
1461
1462 do {
1463 t = *tsave;
1464 if (abs || !rq_time) {
1465 adjust_abs_time(&posix_clocks[which_clock], &t, abs,
1466 &rq_time, &dum);
1467 rq_time += (t.tv_sec || t.tv_nsec);
1468 }
1469
1470 left = rq_time - get_jiffies_64();
1471 if (left >= (s64)MAX_JIFFY_OFFSET)
1472 left = (s64)MAX_JIFFY_OFFSET;
1473 if (left < (s64)0)
1474 break;
1475
1476 new_timer.expires = jiffies + left;
1477 __set_current_state(TASK_INTERRUPTIBLE);
1478 add_timer(&new_timer);
1479
1480 schedule();
1481
1482 del_timer_sync(&new_timer);
1483 left = rq_time - get_jiffies_64();
1484 } while (left > (s64)0 && !test_thread_flag(TIF_SIGPENDING));
1485
1486 if (abs_wqueue.task_list.next)
1487 finish_wait(&nanosleep_abs_wqueue, &abs_wqueue);
1488
1489 if (left > (s64)0) {
1490
1491
1492
1493
1494
1495 if (abs)
1496 return -ERESTARTNOHAND;
1497
1498 left *= TICK_NSEC;
1499 tsave->tv_sec = div_long_long_rem(left,
1500 NSEC_PER_SEC,
1501 &tsave->tv_nsec);
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511 restart_block->fn = clock_nanosleep_restart;
1512 restart_block->arg0 = which_clock;
1513
1514
1515
1516 restart_block->arg2 = rq_time & 0xffffffffLL;
1517 restart_block->arg3 = rq_time >> 32;
1518
1519 return -ERESTART_RESTARTBLOCK;
1520 }
1521
1522 return 0;
1523}
1524
1525
1526
1527long
1528clock_nanosleep_restart(struct restart_block *restart_block)
1529{
1530 struct timespec t;
1531 int ret = do_clock_nanosleep(restart_block->arg0, 0, &t);
1532
1533 if ((ret == -ERESTART_RESTARTBLOCK) && restart_block->arg1 &&
1534 copy_to_user((struct timespec __user *)(restart_block->arg1), &t,
1535 sizeof (t)))
1536 return -EFAULT;
1537 return ret;
1538}
1539