[PATCH] hrtimer: remove duplicate div_long_long_rem implementation
[safe/jmp/linux-2.6] / kernel / posix-timers.c
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
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  *
27  * MontaVista Software | 1237 East Arques Avenue | Sunnyvale | CA 94085 | USA
28  */
29
30 /* These are all the functions necessary to implement
31  * POSIX clocks & timers
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 #include <linux/calc64.h>
39
40 #include <asm/uaccess.h>
41 #include <asm/semaphore.h>
42 #include <linux/list.h>
43 #include <linux/init.h>
44 #include <linux/compiler.h>
45 #include <linux/idr.h>
46 #include <linux/posix-timers.h>
47 #include <linux/syscalls.h>
48 #include <linux/wait.h>
49 #include <linux/workqueue.h>
50 #include <linux/module.h>
51
52 #define CLOCK_REALTIME_RES TICK_NSEC  /* In nano seconds. */
53
54 static inline u64  mpy_l_X_l_ll(unsigned long mpy1,unsigned long mpy2)
55 {
56         return (u64)mpy1 * mpy2;
57 }
58 /*
59  * Management arrays for POSIX timers.   Timers are kept in slab memory
60  * Timer ids are allocated by an external routine that keeps track of the
61  * id and the timer.  The external interface is:
62  *
63  * void *idr_find(struct idr *idp, int id);           to find timer_id <id>
64  * int idr_get_new(struct idr *idp, void *ptr);       to get a new id and
65  *                                                    related it to <ptr>
66  * void idr_remove(struct idr *idp, int id);          to release <id>
67  * void idr_init(struct idr *idp);                    to initialize <idp>
68  *                                                    which we supply.
69  * The idr_get_new *may* call slab for more memory so it must not be
70  * called under a spin lock.  Likewise idr_remore may release memory
71  * (but it may be ok to do this under a lock...).
72  * idr_find is just a memory look up and is quite fast.  A -1 return
73  * indicates that the requested id does not exist.
74  */
75
76 /*
77  * Lets keep our timers in a slab cache :-)
78  */
79 static kmem_cache_t *posix_timers_cache;
80 static struct idr posix_timers_id;
81 static DEFINE_SPINLOCK(idr_lock);
82
83 /*
84  * we assume that the new SIGEV_THREAD_ID shares no bits with the other
85  * SIGEV values.  Here we put out an error if this assumption fails.
86  */
87 #if SIGEV_THREAD_ID != (SIGEV_THREAD_ID & \
88                        ~(SIGEV_SIGNAL | SIGEV_NONE | SIGEV_THREAD))
89 #error "SIGEV_THREAD_ID must not share bit with other SIGEV values!"
90 #endif
91
92
93 /*
94  * The timer ID is turned into a timer address by idr_find().
95  * Verifying a valid ID consists of:
96  *
97  * a) checking that idr_find() returns other than -1.
98  * b) checking that the timer id matches the one in the timer itself.
99  * c) that the timer owner is in the callers thread group.
100  */
101
102 /*
103  * CLOCKs: The POSIX standard calls for a couple of clocks and allows us
104  *          to implement others.  This structure defines the various
105  *          clocks and allows the possibility of adding others.  We
106  *          provide an interface to add clocks to the table and expect
107  *          the "arch" code to add at least one clock that is high
108  *          resolution.  Here we define the standard CLOCK_REALTIME as a
109  *          1/HZ resolution clock.
110  *
111  * RESOLUTION: Clock resolution is used to round up timer and interval
112  *          times, NOT to report clock times, which are reported with as
113  *          much resolution as the system can muster.  In some cases this
114  *          resolution may depend on the underlying clock hardware and
115  *          may not be quantifiable until run time, and only then is the
116  *          necessary code is written.  The standard says we should say
117  *          something about this issue in the documentation...
118  *
119  * FUNCTIONS: The CLOCKs structure defines possible functions to handle
120  *          various clock functions.  For clocks that use the standard
121  *          system timer code these entries should be NULL.  This will
122  *          allow dispatch without the overhead of indirect function
123  *          calls.  CLOCKS that depend on other sources (e.g. WWV or GPS)
124  *          must supply functions here, even if the function just returns
125  *          ENOSYS.  The standard POSIX timer management code assumes the
126  *          following: 1.) The k_itimer struct (sched.h) is used for the
127  *          timer.  2.) The list, it_lock, it_clock, it_id and it_process
128  *          fields are not modified by timer code.
129  *
130  *          At this time all functions EXCEPT clock_nanosleep can be
131  *          redirected by the CLOCKS structure.  Clock_nanosleep is in
132  *          there, but the code ignores it.
133  *
134  * Permissions: It is assumed that the clock_settime() function defined
135  *          for each clock will take care of permission checks.  Some
136  *          clocks may be set able by any user (i.e. local process
137  *          clocks) others not.  Currently the only set able clock we
138  *          have is CLOCK_REALTIME and its high res counter part, both of
139  *          which we beg off on and pass to do_sys_settimeofday().
140  */
141
142 static struct k_clock posix_clocks[MAX_CLOCKS];
143 /*
144  * We only have one real clock that can be set so we need only one abs list,
145  * even if we should want to have several clocks with differing resolutions.
146  */
147 static struct k_clock_abs abs_list = {.list = LIST_HEAD_INIT(abs_list.list),
148                                       .lock = SPIN_LOCK_UNLOCKED};
149
150 static void posix_timer_fn(unsigned long);
151 static u64 do_posix_clock_monotonic_gettime_parts(
152         struct timespec *tp, struct timespec *mo);
153 int do_posix_clock_monotonic_gettime(struct timespec *tp);
154 static int do_posix_clock_monotonic_get(clockid_t, struct timespec *tp);
155
156 static struct k_itimer *lock_timer(timer_t timer_id, unsigned long *flags);
157
158 static inline void unlock_timer(struct k_itimer *timr, unsigned long flags)
159 {
160         spin_unlock_irqrestore(&timr->it_lock, flags);
161 }
162
163 /*
164  * Call the k_clock hook function if non-null, or the default function.
165  */
166 #define CLOCK_DISPATCH(clock, call, arglist) \
167         ((clock) < 0 ? posix_cpu_##call arglist : \
168          (posix_clocks[clock].call != NULL \
169           ? (*posix_clocks[clock].call) arglist : common_##call arglist))
170
171 /*
172  * Default clock hook functions when the struct k_clock passed
173  * to register_posix_clock leaves a function pointer null.
174  *
175  * The function common_CALL is the default implementation for
176  * the function pointer CALL in struct k_clock.
177  */
178
179 static inline int common_clock_getres(clockid_t which_clock,
180                                       struct timespec *tp)
181 {
182         tp->tv_sec = 0;
183         tp->tv_nsec = posix_clocks[which_clock].res;
184         return 0;
185 }
186
187 static inline int common_clock_get(clockid_t which_clock, struct timespec *tp)
188 {
189         getnstimeofday(tp);
190         return 0;
191 }
192
193 static inline int common_clock_set(clockid_t which_clock, struct timespec *tp)
194 {
195         return do_sys_settimeofday(tp, NULL);
196 }
197
198 static inline int common_timer_create(struct k_itimer *new_timer)
199 {
200         INIT_LIST_HEAD(&new_timer->it.real.abs_timer_entry);
201         init_timer(&new_timer->it.real.timer);
202         new_timer->it.real.timer.data = (unsigned long) new_timer;
203         new_timer->it.real.timer.function = posix_timer_fn;
204         return 0;
205 }
206
207 /*
208  * These ones are defined below.
209  */
210 static int common_nsleep(clockid_t, int flags, struct timespec *t);
211 static void common_timer_get(struct k_itimer *, struct itimerspec *);
212 static int common_timer_set(struct k_itimer *, int,
213                             struct itimerspec *, struct itimerspec *);
214 static int common_timer_del(struct k_itimer *timer);
215
216 /*
217  * Return nonzero iff we know a priori this clockid_t value is bogus.
218  */
219 static inline int invalid_clockid(clockid_t which_clock)
220 {
221         if (which_clock < 0)    /* CPU clock, posix_cpu_* will check it */
222                 return 0;
223         if ((unsigned) which_clock >= MAX_CLOCKS)
224                 return 1;
225         if (posix_clocks[which_clock].clock_getres != NULL)
226                 return 0;
227 #ifndef CLOCK_DISPATCH_DIRECT
228         if (posix_clocks[which_clock].res != 0)
229                 return 0;
230 #endif
231         return 1;
232 }
233
234
235 /*
236  * Initialize everything, well, just everything in Posix clocks/timers ;)
237  */
238 static __init int init_posix_timers(void)
239 {
240         struct k_clock clock_realtime = {.res = CLOCK_REALTIME_RES,
241                                          .abs_struct = &abs_list
242         };
243         struct k_clock clock_monotonic = {.res = CLOCK_REALTIME_RES,
244                 .abs_struct = NULL,
245                 .clock_get = do_posix_clock_monotonic_get,
246                 .clock_set = do_posix_clock_nosettime
247         };
248
249         register_posix_clock(CLOCK_REALTIME, &clock_realtime);
250         register_posix_clock(CLOCK_MONOTONIC, &clock_monotonic);
251
252         posix_timers_cache = kmem_cache_create("posix_timers_cache",
253                                         sizeof (struct k_itimer), 0, 0, NULL, NULL);
254         idr_init(&posix_timers_id);
255         return 0;
256 }
257
258 __initcall(init_posix_timers);
259
260 static void tstojiffie(struct timespec *tp, int res, u64 *jiff)
261 {
262         long sec = tp->tv_sec;
263         long nsec = tp->tv_nsec + res - 1;
264
265         if (nsec >= NSEC_PER_SEC) {
266                 sec++;
267                 nsec -= NSEC_PER_SEC;
268         }
269
270         /*
271          * The scaling constants are defined in <linux/time.h>
272          * The difference between there and here is that we do the
273          * res rounding and compute a 64-bit result (well so does that
274          * but it then throws away the high bits).
275          */
276         *jiff =  (mpy_l_X_l_ll(sec, SEC_CONVERSION) +
277                   (mpy_l_X_l_ll(nsec, NSEC_CONVERSION) >> 
278                    (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
279 }
280
281 /*
282  * This function adjusts the timer as needed as a result of the clock
283  * being set.  It should only be called for absolute timers, and then
284  * under the abs_list lock.  It computes the time difference and sets
285  * the new jiffies value in the timer.  It also updates the timers
286  * reference wall_to_monotonic value.  It is complicated by the fact
287  * that tstojiffies() only handles positive times and it needs to work
288  * with both positive and negative times.  Also, for negative offsets,
289  * we need to defeat the res round up.
290  *
291  * Return is true if there is a new time, else false.
292  */
293 static long add_clockset_delta(struct k_itimer *timr,
294                                struct timespec *new_wall_to)
295 {
296         struct timespec delta;
297         int sign = 0;
298         u64 exp;
299
300         set_normalized_timespec(&delta,
301                                 new_wall_to->tv_sec -
302                                 timr->it.real.wall_to_prev.tv_sec,
303                                 new_wall_to->tv_nsec -
304                                 timr->it.real.wall_to_prev.tv_nsec);
305         if (likely(!(delta.tv_sec | delta.tv_nsec)))
306                 return 0;
307         if (delta.tv_sec < 0) {
308                 set_normalized_timespec(&delta,
309                                         -delta.tv_sec,
310                                         1 - delta.tv_nsec -
311                                         posix_clocks[timr->it_clock].res);
312                 sign++;
313         }
314         tstojiffie(&delta, posix_clocks[timr->it_clock].res, &exp);
315         timr->it.real.wall_to_prev = *new_wall_to;
316         timr->it.real.timer.expires += (sign ? -exp : exp);
317         return 1;
318 }
319
320 static void remove_from_abslist(struct k_itimer *timr)
321 {
322         if (!list_empty(&timr->it.real.abs_timer_entry)) {
323                 spin_lock(&abs_list.lock);
324                 list_del_init(&timr->it.real.abs_timer_entry);
325                 spin_unlock(&abs_list.lock);
326         }
327 }
328
329 static void schedule_next_timer(struct k_itimer *timr)
330 {
331         struct timespec new_wall_to;
332         struct now_struct now;
333         unsigned long seq;
334
335         /*
336          * Set up the timer for the next interval (if there is one).
337          * Note: this code uses the abs_timer_lock to protect
338          * it.real.wall_to_prev and must hold it until exp is set, not exactly
339          * obvious...
340
341          * This function is used for CLOCK_REALTIME* and
342          * CLOCK_MONOTONIC* timers.  If we ever want to handle other
343          * CLOCKs, the calling code (do_schedule_next_timer) would need
344          * to pull the "clock" info from the timer and dispatch the
345          * "other" CLOCKs "next timer" code (which, I suppose should
346          * also be added to the k_clock structure).
347          */
348         if (!timr->it.real.incr)
349                 return;
350
351         do {
352                 seq = read_seqbegin(&xtime_lock);
353                 new_wall_to =   wall_to_monotonic;
354                 posix_get_now(&now);
355         } while (read_seqretry(&xtime_lock, seq));
356
357         if (!list_empty(&timr->it.real.abs_timer_entry)) {
358                 spin_lock(&abs_list.lock);
359                 add_clockset_delta(timr, &new_wall_to);
360
361                 posix_bump_timer(timr, now);
362
363                 spin_unlock(&abs_list.lock);
364         } else {
365                 posix_bump_timer(timr, now);
366         }
367         timr->it_overrun_last = timr->it_overrun;
368         timr->it_overrun = -1;
369         ++timr->it_requeue_pending;
370         add_timer(&timr->it.real.timer);
371 }
372
373 /*
374  * This function is exported for use by the signal deliver code.  It is
375  * called just prior to the info block being released and passes that
376  * block to us.  It's function is to update the overrun entry AND to
377  * restart the timer.  It should only be called if the timer is to be
378  * restarted (i.e. we have flagged this in the sys_private entry of the
379  * info block).
380  *
381  * To protect aginst the timer going away while the interrupt is queued,
382  * we require that the it_requeue_pending flag be set.
383  */
384 void do_schedule_next_timer(struct siginfo *info)
385 {
386         struct k_itimer *timr;
387         unsigned long flags;
388
389         timr = lock_timer(info->si_tid, &flags);
390
391         if (!timr || timr->it_requeue_pending != info->si_sys_private)
392                 goto exit;
393
394         if (timr->it_clock < 0) /* CPU clock */
395                 posix_cpu_timer_schedule(timr);
396         else
397                 schedule_next_timer(timr);
398         info->si_overrun = timr->it_overrun_last;
399 exit:
400         if (timr)
401                 unlock_timer(timr, flags);
402 }
403
404 int posix_timer_event(struct k_itimer *timr,int si_private)
405 {
406         memset(&timr->sigq->info, 0, sizeof(siginfo_t));
407         timr->sigq->info.si_sys_private = si_private;
408         /*
409          * Send signal to the process that owns this timer.
410
411          * This code assumes that all the possible abs_lists share the
412          * same lock (there is only one list at this time). If this is
413          * not the case, the CLOCK info would need to be used to find
414          * the proper abs list lock.
415          */
416
417         timr->sigq->info.si_signo = timr->it_sigev_signo;
418         timr->sigq->info.si_errno = 0;
419         timr->sigq->info.si_code = SI_TIMER;
420         timr->sigq->info.si_tid = timr->it_id;
421         timr->sigq->info.si_value = timr->it_sigev_value;
422
423         if (timr->it_sigev_notify & SIGEV_THREAD_ID) {
424                 struct task_struct *leader;
425                 int ret = send_sigqueue(timr->it_sigev_signo, timr->sigq,
426                                         timr->it_process);
427
428                 if (likely(ret >= 0))
429                         return ret;
430
431                 timr->it_sigev_notify = SIGEV_SIGNAL;
432                 leader = timr->it_process->group_leader;
433                 put_task_struct(timr->it_process);
434                 timr->it_process = leader;
435         }
436
437         return send_group_sigqueue(timr->it_sigev_signo, timr->sigq,
438                                    timr->it_process);
439 }
440 EXPORT_SYMBOL_GPL(posix_timer_event);
441
442 /*
443  * This function gets called when a POSIX.1b interval timer expires.  It
444  * is used as a callback from the kernel internal timer.  The
445  * run_timer_list code ALWAYS calls with interrupts on.
446
447  * This code is for CLOCK_REALTIME* and CLOCK_MONOTONIC* timers.
448  */
449 static void posix_timer_fn(unsigned long __data)
450 {
451         struct k_itimer *timr = (struct k_itimer *) __data;
452         unsigned long flags;
453         unsigned long seq;
454         struct timespec delta, new_wall_to;
455         u64 exp = 0;
456         int do_notify = 1;
457
458         spin_lock_irqsave(&timr->it_lock, flags);
459         if (!list_empty(&timr->it.real.abs_timer_entry)) {
460                 spin_lock(&abs_list.lock);
461                 do {
462                         seq = read_seqbegin(&xtime_lock);
463                         new_wall_to =   wall_to_monotonic;
464                 } while (read_seqretry(&xtime_lock, seq));
465                 set_normalized_timespec(&delta,
466                                         new_wall_to.tv_sec -
467                                         timr->it.real.wall_to_prev.tv_sec,
468                                         new_wall_to.tv_nsec -
469                                         timr->it.real.wall_to_prev.tv_nsec);
470                 if (likely((delta.tv_sec | delta.tv_nsec ) == 0)) {
471                         /* do nothing, timer is on time */
472                 } else if (delta.tv_sec < 0) {
473                         /* do nothing, timer is already late */
474                 } else {
475                         /* timer is early due to a clock set */
476                         tstojiffie(&delta,
477                                    posix_clocks[timr->it_clock].res,
478                                    &exp);
479                         timr->it.real.wall_to_prev = new_wall_to;
480                         timr->it.real.timer.expires += exp;
481                         add_timer(&timr->it.real.timer);
482                         do_notify = 0;
483                 }
484                 spin_unlock(&abs_list.lock);
485
486         }
487         if (do_notify)  {
488                 int si_private=0;
489
490                 if (timr->it.real.incr)
491                         si_private = ++timr->it_requeue_pending;
492                 else {
493                         remove_from_abslist(timr);
494                 }
495
496                 if (posix_timer_event(timr, si_private))
497                         /*
498                          * signal was not sent because of sig_ignor
499                          * we will not get a call back to restart it AND
500                          * it should be restarted.
501                          */
502                         schedule_next_timer(timr);
503         }
504         unlock_timer(timr, flags); /* hold thru abs lock to keep irq off */
505 }
506
507
508 static inline struct task_struct * good_sigevent(sigevent_t * event)
509 {
510         struct task_struct *rtn = current->group_leader;
511
512         if ((event->sigev_notify & SIGEV_THREAD_ID ) &&
513                 (!(rtn = find_task_by_pid(event->sigev_notify_thread_id)) ||
514                  rtn->tgid != current->tgid ||
515                  (event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_SIGNAL))
516                 return NULL;
517
518         if (((event->sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) &&
519             ((event->sigev_signo <= 0) || (event->sigev_signo > SIGRTMAX)))
520                 return NULL;
521
522         return rtn;
523 }
524
525 void register_posix_clock(clockid_t clock_id, struct k_clock *new_clock)
526 {
527         if ((unsigned) clock_id >= MAX_CLOCKS) {
528                 printk("POSIX clock register failed for clock_id %d\n",
529                        clock_id);
530                 return;
531         }
532
533         posix_clocks[clock_id] = *new_clock;
534 }
535 EXPORT_SYMBOL_GPL(register_posix_clock);
536
537 static struct k_itimer * alloc_posix_timer(void)
538 {
539         struct k_itimer *tmr;
540         tmr = kmem_cache_alloc(posix_timers_cache, GFP_KERNEL);
541         if (!tmr)
542                 return tmr;
543         memset(tmr, 0, sizeof (struct k_itimer));
544         if (unlikely(!(tmr->sigq = sigqueue_alloc()))) {
545                 kmem_cache_free(posix_timers_cache, tmr);
546                 tmr = NULL;
547         }
548         return tmr;
549 }
550
551 #define IT_ID_SET       1
552 #define IT_ID_NOT_SET   0
553 static void release_posix_timer(struct k_itimer *tmr, int it_id_set)
554 {
555         if (it_id_set) {
556                 unsigned long flags;
557                 spin_lock_irqsave(&idr_lock, flags);
558                 idr_remove(&posix_timers_id, tmr->it_id);
559                 spin_unlock_irqrestore(&idr_lock, flags);
560         }
561         sigqueue_free(tmr->sigq);
562         if (unlikely(tmr->it_process) &&
563             tmr->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
564                 put_task_struct(tmr->it_process);
565         kmem_cache_free(posix_timers_cache, tmr);
566 }
567
568 /* Create a POSIX.1b interval timer. */
569
570 asmlinkage long
571 sys_timer_create(clockid_t which_clock,
572                  struct sigevent __user *timer_event_spec,
573                  timer_t __user * created_timer_id)
574 {
575         int error = 0;
576         struct k_itimer *new_timer = NULL;
577         int new_timer_id;
578         struct task_struct *process = NULL;
579         unsigned long flags;
580         sigevent_t event;
581         int it_id_set = IT_ID_NOT_SET;
582
583         if (invalid_clockid(which_clock))
584                 return -EINVAL;
585
586         new_timer = alloc_posix_timer();
587         if (unlikely(!new_timer))
588                 return -EAGAIN;
589
590         spin_lock_init(&new_timer->it_lock);
591  retry:
592         if (unlikely(!idr_pre_get(&posix_timers_id, GFP_KERNEL))) {
593                 error = -EAGAIN;
594                 goto out;
595         }
596         spin_lock_irq(&idr_lock);
597         error = idr_get_new(&posix_timers_id,
598                             (void *) new_timer,
599                             &new_timer_id);
600         spin_unlock_irq(&idr_lock);
601         if (error == -EAGAIN)
602                 goto retry;
603         else if (error) {
604                 /*
605                  * Wierd looking, but we return EAGAIN if the IDR is
606                  * full (proper POSIX return value for this)
607                  */
608                 error = -EAGAIN;
609                 goto out;
610         }
611
612         it_id_set = IT_ID_SET;
613         new_timer->it_id = (timer_t) new_timer_id;
614         new_timer->it_clock = which_clock;
615         new_timer->it_overrun = -1;
616         error = CLOCK_DISPATCH(which_clock, timer_create, (new_timer));
617         if (error)
618                 goto out;
619
620         /*
621          * return the timer_id now.  The next step is hard to
622          * back out if there is an error.
623          */
624         if (copy_to_user(created_timer_id,
625                          &new_timer_id, sizeof (new_timer_id))) {
626                 error = -EFAULT;
627                 goto out;
628         }
629         if (timer_event_spec) {
630                 if (copy_from_user(&event, timer_event_spec, sizeof (event))) {
631                         error = -EFAULT;
632                         goto out;
633                 }
634                 new_timer->it_sigev_notify = event.sigev_notify;
635                 new_timer->it_sigev_signo = event.sigev_signo;
636                 new_timer->it_sigev_value = event.sigev_value;
637
638                 read_lock(&tasklist_lock);
639                 if ((process = good_sigevent(&event))) {
640                         /*
641                          * We may be setting up this process for another
642                          * thread.  It may be exiting.  To catch this
643                          * case the we check the PF_EXITING flag.  If
644                          * the flag is not set, the siglock will catch
645                          * him before it is too late (in exit_itimers).
646                          *
647                          * The exec case is a bit more invloved but easy
648                          * to code.  If the process is in our thread
649                          * group (and it must be or we would not allow
650                          * it here) and is doing an exec, it will cause
651                          * us to be killed.  In this case it will wait
652                          * for us to die which means we can finish this
653                          * linkage with our last gasp. I.e. no code :)
654                          */
655                         spin_lock_irqsave(&process->sighand->siglock, flags);
656                         if (!(process->flags & PF_EXITING)) {
657                                 new_timer->it_process = process;
658                                 list_add(&new_timer->list,
659                                          &process->signal->posix_timers);
660                                 spin_unlock_irqrestore(&process->sighand->siglock, flags);
661                                 if (new_timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
662                                         get_task_struct(process);
663                         } else {
664                                 spin_unlock_irqrestore(&process->sighand->siglock, flags);
665                                 process = NULL;
666                         }
667                 }
668                 read_unlock(&tasklist_lock);
669                 if (!process) {
670                         error = -EINVAL;
671                         goto out;
672                 }
673         } else {
674                 new_timer->it_sigev_notify = SIGEV_SIGNAL;
675                 new_timer->it_sigev_signo = SIGALRM;
676                 new_timer->it_sigev_value.sival_int = new_timer->it_id;
677                 process = current->group_leader;
678                 spin_lock_irqsave(&process->sighand->siglock, flags);
679                 new_timer->it_process = process;
680                 list_add(&new_timer->list, &process->signal->posix_timers);
681                 spin_unlock_irqrestore(&process->sighand->siglock, flags);
682         }
683
684         /*
685          * In the case of the timer belonging to another task, after
686          * the task is unlocked, the timer is owned by the other task
687          * and may cease to exist at any time.  Don't use or modify
688          * new_timer after the unlock call.
689          */
690
691 out:
692         if (error)
693                 release_posix_timer(new_timer, it_id_set);
694
695         return error;
696 }
697
698 /*
699  * good_timespec
700  *
701  * This function checks the elements of a timespec structure.
702  *
703  * Arguments:
704  * ts        : Pointer to the timespec structure to check
705  *
706  * Return value:
707  * If a NULL pointer was passed in, or the tv_nsec field was less than 0
708  * or greater than NSEC_PER_SEC, or the tv_sec field was less than 0,
709  * this function returns 0. Otherwise it returns 1.
710  */
711 static int good_timespec(const struct timespec *ts)
712 {
713         if ((!ts) || (ts->tv_sec < 0) ||
714                         ((unsigned) ts->tv_nsec >= NSEC_PER_SEC))
715                 return 0;
716         return 1;
717 }
718
719 /*
720  * Locking issues: We need to protect the result of the id look up until
721  * we get the timer locked down so it is not deleted under us.  The
722  * removal is done under the idr spinlock so we use that here to bridge
723  * the find to the timer lock.  To avoid a dead lock, the timer id MUST
724  * be release with out holding the timer lock.
725  */
726 static struct k_itimer * lock_timer(timer_t timer_id, unsigned long *flags)
727 {
728         struct k_itimer *timr;
729         /*
730          * Watch out here.  We do a irqsave on the idr_lock and pass the
731          * flags part over to the timer lock.  Must not let interrupts in
732          * while we are moving the lock.
733          */
734
735         spin_lock_irqsave(&idr_lock, *flags);
736         timr = (struct k_itimer *) idr_find(&posix_timers_id, (int) timer_id);
737         if (timr) {
738                 spin_lock(&timr->it_lock);
739                 spin_unlock(&idr_lock);
740
741                 if ((timr->it_id != timer_id) || !(timr->it_process) ||
742                                 timr->it_process->tgid != current->tgid) {
743                         unlock_timer(timr, *flags);
744                         timr = NULL;
745                 }
746         } else
747                 spin_unlock_irqrestore(&idr_lock, *flags);
748
749         return timr;
750 }
751
752 /*
753  * Get the time remaining on a POSIX.1b interval timer.  This function
754  * is ALWAYS called with spin_lock_irq on the timer, thus it must not
755  * mess with irq.
756  *
757  * We have a couple of messes to clean up here.  First there is the case
758  * of a timer that has a requeue pending.  These timers should appear to
759  * be in the timer list with an expiry as if we were to requeue them
760  * now.
761  *
762  * The second issue is the SIGEV_NONE timer which may be active but is
763  * not really ever put in the timer list (to save system resources).
764  * This timer may be expired, and if so, we will do it here.  Otherwise
765  * it is the same as a requeue pending timer WRT to what we should
766  * report.
767  */
768 static void
769 common_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting)
770 {
771         unsigned long expires;
772         struct now_struct now;
773
774         do
775                 expires = timr->it.real.timer.expires;
776         while ((volatile long) (timr->it.real.timer.expires) != expires);
777
778         posix_get_now(&now);
779
780         if (expires &&
781             ((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) &&
782             !timr->it.real.incr &&
783             posix_time_before(&timr->it.real.timer, &now))
784                 timr->it.real.timer.expires = expires = 0;
785         if (expires) {
786                 if (timr->it_requeue_pending & REQUEUE_PENDING ||
787                     (timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
788                         posix_bump_timer(timr, now);
789                         expires = timr->it.real.timer.expires;
790                 }
791                 else
792                         if (!timer_pending(&timr->it.real.timer))
793                                 expires = 0;
794                 if (expires)
795                         expires -= now.jiffies;
796         }
797         jiffies_to_timespec(expires, &cur_setting->it_value);
798         jiffies_to_timespec(timr->it.real.incr, &cur_setting->it_interval);
799
800         if (cur_setting->it_value.tv_sec < 0) {
801                 cur_setting->it_value.tv_nsec = 1;
802                 cur_setting->it_value.tv_sec = 0;
803         }
804 }
805
806 /* Get the time remaining on a POSIX.1b interval timer. */
807 asmlinkage long
808 sys_timer_gettime(timer_t timer_id, struct itimerspec __user *setting)
809 {
810         struct k_itimer *timr;
811         struct itimerspec cur_setting;
812         unsigned long flags;
813
814         timr = lock_timer(timer_id, &flags);
815         if (!timr)
816                 return -EINVAL;
817
818         CLOCK_DISPATCH(timr->it_clock, timer_get, (timr, &cur_setting));
819
820         unlock_timer(timr, flags);
821
822         if (copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
823                 return -EFAULT;
824
825         return 0;
826 }
827 /*
828  * Get the number of overruns of a POSIX.1b interval timer.  This is to
829  * be the overrun of the timer last delivered.  At the same time we are
830  * accumulating overruns on the next timer.  The overrun is frozen when
831  * the signal is delivered, either at the notify time (if the info block
832  * is not queued) or at the actual delivery time (as we are informed by
833  * the call back to do_schedule_next_timer().  So all we need to do is
834  * to pick up the frozen overrun.
835  */
836
837 asmlinkage long
838 sys_timer_getoverrun(timer_t timer_id)
839 {
840         struct k_itimer *timr;
841         int overrun;
842         long flags;
843
844         timr = lock_timer(timer_id, &flags);
845         if (!timr)
846                 return -EINVAL;
847
848         overrun = timr->it_overrun_last;
849         unlock_timer(timr, flags);
850
851         return overrun;
852 }
853 /*
854  * Adjust for absolute time
855  *
856  * If absolute time is given and it is not CLOCK_MONOTONIC, we need to
857  * adjust for the offset between the timer clock (CLOCK_MONOTONIC) and
858  * what ever clock he is using.
859  *
860  * If it is relative time, we need to add the current (CLOCK_MONOTONIC)
861  * time to it to get the proper time for the timer.
862  */
863 static int adjust_abs_time(struct k_clock *clock, struct timespec *tp, 
864                            int abs, u64 *exp, struct timespec *wall_to)
865 {
866         struct timespec now;
867         struct timespec oc = *tp;
868         u64 jiffies_64_f;
869         int rtn =0;
870
871         if (abs) {
872                 /*
873                  * The mask pick up the 4 basic clocks 
874                  */
875                 if (!((clock - &posix_clocks[0]) & ~CLOCKS_MASK)) {
876                         jiffies_64_f = do_posix_clock_monotonic_gettime_parts(
877                                 &now,  wall_to);
878                         /*
879                          * If we are doing a MONOTONIC clock
880                          */
881                         if((clock - &posix_clocks[0]) & CLOCKS_MONO){
882                                 now.tv_sec += wall_to->tv_sec;
883                                 now.tv_nsec += wall_to->tv_nsec;
884                         }
885                 } else {
886                         /*
887                          * Not one of the basic clocks
888                          */
889                         clock->clock_get(clock - posix_clocks, &now);
890                         jiffies_64_f = get_jiffies_64();
891                 }
892                 /*
893                  * Take away now to get delta and normalize
894                  */
895                 set_normalized_timespec(&oc, oc.tv_sec - now.tv_sec,
896                                         oc.tv_nsec - now.tv_nsec);
897         }else{
898                 jiffies_64_f = get_jiffies_64();
899         }
900         /*
901          * Check if the requested time is prior to now (if so set now)
902          */
903         if (oc.tv_sec < 0)
904                 oc.tv_sec = oc.tv_nsec = 0;
905
906         if (oc.tv_sec | oc.tv_nsec)
907                 set_normalized_timespec(&oc, oc.tv_sec,
908                                         oc.tv_nsec + clock->res);
909         tstojiffie(&oc, clock->res, exp);
910
911         /*
912          * Check if the requested time is more than the timer code
913          * can handle (if so we error out but return the value too).
914          */
915         if (*exp > ((u64)MAX_JIFFY_OFFSET))
916                         /*
917                          * This is a considered response, not exactly in
918                          * line with the standard (in fact it is silent on
919                          * possible overflows).  We assume such a large 
920                          * value is ALMOST always a programming error and
921                          * try not to compound it by setting a really dumb
922                          * value.
923                          */
924                         rtn = -EINVAL;
925         /*
926          * return the actual jiffies expire time, full 64 bits
927          */
928         *exp += jiffies_64_f;
929         return rtn;
930 }
931
932 /* Set a POSIX.1b interval timer. */
933 /* timr->it_lock is taken. */
934 static inline int
935 common_timer_set(struct k_itimer *timr, int flags,
936                  struct itimerspec *new_setting, struct itimerspec *old_setting)
937 {
938         struct k_clock *clock = &posix_clocks[timr->it_clock];
939         u64 expire_64;
940
941         if (old_setting)
942                 common_timer_get(timr, old_setting);
943
944         /* disable the timer */
945         timr->it.real.incr = 0;
946         /*
947          * careful here.  If smp we could be in the "fire" routine which will
948          * be spinning as we hold the lock.  But this is ONLY an SMP issue.
949          */
950         if (try_to_del_timer_sync(&timr->it.real.timer) < 0) {
951 #ifdef CONFIG_SMP
952                 /*
953                  * It can only be active if on an other cpu.  Since
954                  * we have cleared the interval stuff above, it should
955                  * clear once we release the spin lock.  Of course once
956                  * we do that anything could happen, including the
957                  * complete melt down of the timer.  So return with
958                  * a "retry" exit status.
959                  */
960                 return TIMER_RETRY;
961 #endif
962         }
963
964         remove_from_abslist(timr);
965
966         timr->it_requeue_pending = (timr->it_requeue_pending + 2) & 
967                 ~REQUEUE_PENDING;
968         timr->it_overrun_last = 0;
969         timr->it_overrun = -1;
970         /*
971          *switch off the timer when it_value is zero
972          */
973         if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec) {
974                 timr->it.real.timer.expires = 0;
975                 return 0;
976         }
977
978         if (adjust_abs_time(clock,
979                             &new_setting->it_value, flags & TIMER_ABSTIME, 
980                             &expire_64, &(timr->it.real.wall_to_prev))) {
981                 return -EINVAL;
982         }
983         timr->it.real.timer.expires = (unsigned long)expire_64;
984         tstojiffie(&new_setting->it_interval, clock->res, &expire_64);
985         timr->it.real.incr = (unsigned long)expire_64;
986
987         /*
988          * We do not even queue SIGEV_NONE timers!  But we do put them
989          * in the abs list so we can do that right.
990          */
991         if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE))
992                 add_timer(&timr->it.real.timer);
993
994         if (flags & TIMER_ABSTIME && clock->abs_struct) {
995                 spin_lock(&clock->abs_struct->lock);
996                 list_add_tail(&(timr->it.real.abs_timer_entry),
997                               &(clock->abs_struct->list));
998                 spin_unlock(&clock->abs_struct->lock);
999         }
1000         return 0;
1001 }
1002
1003 /* Set a POSIX.1b interval timer */
1004 asmlinkage long
1005 sys_timer_settime(timer_t timer_id, int flags,
1006                   const struct itimerspec __user *new_setting,
1007                   struct itimerspec __user *old_setting)
1008 {
1009         struct k_itimer *timr;
1010         struct itimerspec new_spec, old_spec;
1011         int error = 0;
1012         long flag;
1013         struct itimerspec *rtn = old_setting ? &old_spec : NULL;
1014
1015         if (!new_setting)
1016                 return -EINVAL;
1017
1018         if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
1019                 return -EFAULT;
1020
1021         if ((!good_timespec(&new_spec.it_interval)) ||
1022             (!good_timespec(&new_spec.it_value)))
1023                 return -EINVAL;
1024 retry:
1025         timr = lock_timer(timer_id, &flag);
1026         if (!timr)
1027                 return -EINVAL;
1028
1029         error = CLOCK_DISPATCH(timr->it_clock, timer_set,
1030                                (timr, flags, &new_spec, rtn));
1031
1032         unlock_timer(timr, flag);
1033         if (error == TIMER_RETRY) {
1034                 rtn = NULL;     // We already got the old time...
1035                 goto retry;
1036         }
1037
1038         if (old_setting && !error && copy_to_user(old_setting,
1039                                                   &old_spec, sizeof (old_spec)))
1040                 error = -EFAULT;
1041
1042         return error;
1043 }
1044
1045 static inline int common_timer_del(struct k_itimer *timer)
1046 {
1047         timer->it.real.incr = 0;
1048
1049         if (try_to_del_timer_sync(&timer->it.real.timer) < 0) {
1050 #ifdef CONFIG_SMP
1051                 /*
1052                  * It can only be active if on an other cpu.  Since
1053                  * we have cleared the interval stuff above, it should
1054                  * clear once we release the spin lock.  Of course once
1055                  * we do that anything could happen, including the
1056                  * complete melt down of the timer.  So return with
1057                  * a "retry" exit status.
1058                  */
1059                 return TIMER_RETRY;
1060 #endif
1061         }
1062
1063         remove_from_abslist(timer);
1064
1065         return 0;
1066 }
1067
1068 static inline int timer_delete_hook(struct k_itimer *timer)
1069 {
1070         return CLOCK_DISPATCH(timer->it_clock, timer_del, (timer));
1071 }
1072
1073 /* Delete a POSIX.1b interval timer. */
1074 asmlinkage long
1075 sys_timer_delete(timer_t timer_id)
1076 {
1077         struct k_itimer *timer;
1078         long flags;
1079
1080 #ifdef CONFIG_SMP
1081         int error;
1082 retry_delete:
1083 #endif
1084         timer = lock_timer(timer_id, &flags);
1085         if (!timer)
1086                 return -EINVAL;
1087
1088 #ifdef CONFIG_SMP
1089         error = timer_delete_hook(timer);
1090
1091         if (error == TIMER_RETRY) {
1092                 unlock_timer(timer, flags);
1093                 goto retry_delete;
1094         }
1095 #else
1096         timer_delete_hook(timer);
1097 #endif
1098         spin_lock(&current->sighand->siglock);
1099         list_del(&timer->list);
1100         spin_unlock(&current->sighand->siglock);
1101         /*
1102          * This keeps any tasks waiting on the spin lock from thinking
1103          * they got something (see the lock code above).
1104          */
1105         if (timer->it_process) {
1106                 if (timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
1107                         put_task_struct(timer->it_process);
1108                 timer->it_process = NULL;
1109         }
1110         unlock_timer(timer, flags);
1111         release_posix_timer(timer, IT_ID_SET);
1112         return 0;
1113 }
1114 /*
1115  * return timer owned by the process, used by exit_itimers
1116  */
1117 static inline void itimer_delete(struct k_itimer *timer)
1118 {
1119         unsigned long flags;
1120
1121 #ifdef CONFIG_SMP
1122         int error;
1123 retry_delete:
1124 #endif
1125         spin_lock_irqsave(&timer->it_lock, flags);
1126
1127 #ifdef CONFIG_SMP
1128         error = timer_delete_hook(timer);
1129
1130         if (error == TIMER_RETRY) {
1131                 unlock_timer(timer, flags);
1132                 goto retry_delete;
1133         }
1134 #else
1135         timer_delete_hook(timer);
1136 #endif
1137         list_del(&timer->list);
1138         /*
1139          * This keeps any tasks waiting on the spin lock from thinking
1140          * they got something (see the lock code above).
1141          */
1142         if (timer->it_process) {
1143                 if (timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
1144                         put_task_struct(timer->it_process);
1145                 timer->it_process = NULL;
1146         }
1147         unlock_timer(timer, flags);
1148         release_posix_timer(timer, IT_ID_SET);
1149 }
1150
1151 /*
1152  * This is called by do_exit or de_thread, only when there are no more
1153  * references to the shared signal_struct.
1154  */
1155 void exit_itimers(struct signal_struct *sig)
1156 {
1157         struct k_itimer *tmr;
1158
1159         while (!list_empty(&sig->posix_timers)) {
1160                 tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
1161                 itimer_delete(tmr);
1162         }
1163 }
1164
1165 /*
1166  * And now for the "clock" calls
1167  *
1168  * These functions are called both from timer functions (with the timer
1169  * spin_lock_irq() held and from clock calls with no locking.   They must
1170  * use the save flags versions of locks.
1171  */
1172
1173 /*
1174  * We do ticks here to avoid the irq lock ( they take sooo long).
1175  * The seqlock is great here.  Since we a reader, we don't really care
1176  * if we are interrupted since we don't take lock that will stall us or
1177  * any other cpu. Voila, no irq lock is needed.
1178  *
1179  */
1180
1181 static u64 do_posix_clock_monotonic_gettime_parts(
1182         struct timespec *tp, struct timespec *mo)
1183 {
1184         u64 jiff;
1185         unsigned int seq;
1186
1187         do {
1188                 seq = read_seqbegin(&xtime_lock);
1189                 getnstimeofday(tp);
1190                 *mo = wall_to_monotonic;
1191                 jiff = jiffies_64;
1192
1193         } while(read_seqretry(&xtime_lock, seq));
1194
1195         return jiff;
1196 }
1197
1198 static int do_posix_clock_monotonic_get(clockid_t clock, struct timespec *tp)
1199 {
1200         struct timespec wall_to_mono;
1201
1202         do_posix_clock_monotonic_gettime_parts(tp, &wall_to_mono);
1203
1204         set_normalized_timespec(tp, tp->tv_sec + wall_to_mono.tv_sec,
1205                                 tp->tv_nsec + wall_to_mono.tv_nsec);
1206
1207         return 0;
1208 }
1209
1210 int do_posix_clock_monotonic_gettime(struct timespec *tp)
1211 {
1212         return do_posix_clock_monotonic_get(CLOCK_MONOTONIC, tp);
1213 }
1214
1215 int do_posix_clock_nosettime(clockid_t clockid, struct timespec *tp)
1216 {
1217         return -EINVAL;
1218 }
1219 EXPORT_SYMBOL_GPL(do_posix_clock_nosettime);
1220
1221 int do_posix_clock_notimer_create(struct k_itimer *timer)
1222 {
1223         return -EINVAL;
1224 }
1225 EXPORT_SYMBOL_GPL(do_posix_clock_notimer_create);
1226
1227 int do_posix_clock_nonanosleep(clockid_t clock, int flags, struct timespec *t)
1228 {
1229 #ifndef ENOTSUP
1230         return -EOPNOTSUPP;     /* aka ENOTSUP in userland for POSIX */
1231 #else  /*  parisc does define it separately.  */
1232         return -ENOTSUP;
1233 #endif
1234 }
1235 EXPORT_SYMBOL_GPL(do_posix_clock_nonanosleep);
1236
1237 asmlinkage long
1238 sys_clock_settime(clockid_t which_clock, const struct timespec __user *tp)
1239 {
1240         struct timespec new_tp;
1241
1242         if (invalid_clockid(which_clock))
1243                 return -EINVAL;
1244         if (copy_from_user(&new_tp, tp, sizeof (*tp)))
1245                 return -EFAULT;
1246
1247         return CLOCK_DISPATCH(which_clock, clock_set, (which_clock, &new_tp));
1248 }
1249
1250 asmlinkage long
1251 sys_clock_gettime(clockid_t which_clock, struct timespec __user *tp)
1252 {
1253         struct timespec kernel_tp;
1254         int error;
1255
1256         if (invalid_clockid(which_clock))
1257                 return -EINVAL;
1258         error = CLOCK_DISPATCH(which_clock, clock_get,
1259                                (which_clock, &kernel_tp));
1260         if (!error && copy_to_user(tp, &kernel_tp, sizeof (kernel_tp)))
1261                 error = -EFAULT;
1262
1263         return error;
1264
1265 }
1266
1267 asmlinkage long
1268 sys_clock_getres(clockid_t which_clock, struct timespec __user *tp)
1269 {
1270         struct timespec rtn_tp;
1271         int error;
1272
1273         if (invalid_clockid(which_clock))
1274                 return -EINVAL;
1275
1276         error = CLOCK_DISPATCH(which_clock, clock_getres,
1277                                (which_clock, &rtn_tp));
1278
1279         if (!error && tp && copy_to_user(tp, &rtn_tp, sizeof (rtn_tp))) {
1280                 error = -EFAULT;
1281         }
1282
1283         return error;
1284 }
1285
1286 /*
1287  * The standard says that an absolute nanosleep call MUST wake up at
1288  * the requested time in spite of clock settings.  Here is what we do:
1289  * For each nanosleep call that needs it (only absolute and not on
1290  * CLOCK_MONOTONIC* (as it can not be set)) we thread a little structure
1291  * into the "nanosleep_abs_list".  All we need is the task_struct pointer.
1292  * When ever the clock is set we just wake up all those tasks.   The rest
1293  * is done by the while loop in clock_nanosleep().
1294  *
1295  * On locking, clock_was_set() is called from update_wall_clock which
1296  * holds (or has held for it) a write_lock_irq( xtime_lock) and is
1297  * called from the timer bh code.  Thus we need the irq save locks.
1298  *
1299  * Also, on the call from update_wall_clock, that is done as part of a
1300  * softirq thing.  We don't want to delay the system that much (possibly
1301  * long list of timers to fix), so we defer that work to keventd.
1302  */
1303
1304 static DECLARE_WAIT_QUEUE_HEAD(nanosleep_abs_wqueue);
1305 static DECLARE_WORK(clock_was_set_work, (void(*)(void*))clock_was_set, NULL);
1306
1307 static DECLARE_MUTEX(clock_was_set_lock);
1308
1309 void clock_was_set(void)
1310 {
1311         struct k_itimer *timr;
1312         struct timespec new_wall_to;
1313         LIST_HEAD(cws_list);
1314         unsigned long seq;
1315
1316
1317         if (unlikely(in_interrupt())) {
1318                 schedule_work(&clock_was_set_work);
1319                 return;
1320         }
1321         wake_up_all(&nanosleep_abs_wqueue);
1322
1323         /*
1324          * Check if there exist TIMER_ABSTIME timers to correct.
1325          *
1326          * Notes on locking: This code is run in task context with irq
1327          * on.  We CAN be interrupted!  All other usage of the abs list
1328          * lock is under the timer lock which holds the irq lock as
1329          * well.  We REALLY don't want to scan the whole list with the
1330          * interrupt system off, AND we would like a sequence lock on
1331          * this code as well.  Since we assume that the clock will not
1332          * be set often, it seems ok to take and release the irq lock
1333          * for each timer.  In fact add_timer will do this, so this is
1334          * not an issue.  So we know when we are done, we will move the
1335          * whole list to a new location.  Then as we process each entry,
1336          * we will move it to the actual list again.  This way, when our
1337          * copy is empty, we are done.  We are not all that concerned
1338          * about preemption so we will use a semaphore lock to protect
1339          * aginst reentry.  This way we will not stall another
1340          * processor.  It is possible that this may delay some timers
1341          * that should have expired, given the new clock, but even this
1342          * will be minimal as we will always update to the current time,
1343          * even if it was set by a task that is waiting for entry to
1344          * this code.  Timers that expire too early will be caught by
1345          * the expire code and restarted.
1346
1347          * Absolute timers that repeat are left in the abs list while
1348          * waiting for the task to pick up the signal.  This means we
1349          * may find timers that are not in the "add_timer" list, but are
1350          * in the abs list.  We do the same thing for these, save
1351          * putting them back in the "add_timer" list.  (Note, these are
1352          * left in the abs list mainly to indicate that they are
1353          * ABSOLUTE timers, a fact that is used by the re-arm code, and
1354          * for which we have no other flag.)
1355
1356          */
1357
1358         down(&clock_was_set_lock);
1359         spin_lock_irq(&abs_list.lock);
1360         list_splice_init(&abs_list.list, &cws_list);
1361         spin_unlock_irq(&abs_list.lock);
1362         do {
1363                 do {
1364                         seq = read_seqbegin(&xtime_lock);
1365                         new_wall_to =   wall_to_monotonic;
1366                 } while (read_seqretry(&xtime_lock, seq));
1367
1368                 spin_lock_irq(&abs_list.lock);
1369                 if (list_empty(&cws_list)) {
1370                         spin_unlock_irq(&abs_list.lock);
1371                         break;
1372                 }
1373                 timr = list_entry(cws_list.next, struct k_itimer,
1374                                   it.real.abs_timer_entry);
1375
1376                 list_del_init(&timr->it.real.abs_timer_entry);
1377                 if (add_clockset_delta(timr, &new_wall_to) &&
1378                     del_timer(&timr->it.real.timer))  /* timer run yet? */
1379                         add_timer(&timr->it.real.timer);
1380                 list_add(&timr->it.real.abs_timer_entry, &abs_list.list);
1381                 spin_unlock_irq(&abs_list.lock);
1382         } while (1);
1383
1384         up(&clock_was_set_lock);
1385 }
1386
1387 long clock_nanosleep_restart(struct restart_block *restart_block);
1388
1389 asmlinkage long
1390 sys_clock_nanosleep(clockid_t which_clock, int flags,
1391                     const struct timespec __user *rqtp,
1392                     struct timespec __user *rmtp)
1393 {
1394         struct timespec t;
1395         struct restart_block *restart_block =
1396             &(current_thread_info()->restart_block);
1397         int ret;
1398
1399         if (invalid_clockid(which_clock))
1400                 return -EINVAL;
1401
1402         if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
1403                 return -EFAULT;
1404
1405         if ((unsigned) t.tv_nsec >= NSEC_PER_SEC || t.tv_sec < 0)
1406                 return -EINVAL;
1407
1408         /*
1409          * Do this here as nsleep function does not have the real address.
1410          */
1411         restart_block->arg1 = (unsigned long)rmtp;
1412
1413         ret = CLOCK_DISPATCH(which_clock, nsleep, (which_clock, flags, &t));
1414
1415         if ((ret == -ERESTART_RESTARTBLOCK) && rmtp &&
1416                                         copy_to_user(rmtp, &t, sizeof (t)))
1417                 return -EFAULT;
1418         return ret;
1419 }
1420
1421
1422 static int common_nsleep(clockid_t which_clock,
1423                          int flags, struct timespec *tsave)
1424 {
1425         struct timespec t, dum;
1426         DECLARE_WAITQUEUE(abs_wqueue, current);
1427         u64 rq_time = (u64)0;
1428         s64 left;
1429         int abs;
1430         struct restart_block *restart_block =
1431             &current_thread_info()->restart_block;
1432
1433         abs_wqueue.flags = 0;
1434         abs = flags & TIMER_ABSTIME;
1435
1436         if (restart_block->fn == clock_nanosleep_restart) {
1437                 /*
1438                  * Interrupted by a non-delivered signal, pick up remaining
1439                  * time and continue.  Remaining time is in arg2 & 3.
1440                  */
1441                 restart_block->fn = do_no_restart_syscall;
1442
1443                 rq_time = restart_block->arg3;
1444                 rq_time = (rq_time << 32) + restart_block->arg2;
1445                 if (!rq_time)
1446                         return -EINTR;
1447                 left = rq_time - get_jiffies_64();
1448                 if (left <= (s64)0)
1449                         return 0;       /* Already passed */
1450         }
1451
1452         if (abs && (posix_clocks[which_clock].clock_get !=
1453                             posix_clocks[CLOCK_MONOTONIC].clock_get))
1454                 add_wait_queue(&nanosleep_abs_wqueue, &abs_wqueue);
1455
1456         do {
1457                 t = *tsave;
1458                 if (abs || !rq_time) {
1459                         adjust_abs_time(&posix_clocks[which_clock], &t, abs,
1460                                         &rq_time, &dum);
1461                 }
1462
1463                 left = rq_time - get_jiffies_64();
1464                 if (left >= (s64)MAX_JIFFY_OFFSET)
1465                         left = (s64)MAX_JIFFY_OFFSET;
1466                 if (left < (s64)0)
1467                         break;
1468
1469                 schedule_timeout_interruptible(left);
1470
1471                 left = rq_time - get_jiffies_64();
1472         } while (left > (s64)0 && !test_thread_flag(TIF_SIGPENDING));
1473
1474         if (abs_wqueue.task_list.next)
1475                 finish_wait(&nanosleep_abs_wqueue, &abs_wqueue);
1476
1477         if (left > (s64)0) {
1478
1479                 /*
1480                  * Always restart abs calls from scratch to pick up any
1481                  * clock shifting that happened while we are away.
1482                  */
1483                 if (abs)
1484                         return -ERESTARTNOHAND;
1485
1486                 left *= TICK_NSEC;
1487                 tsave->tv_sec = div_long_long_rem(left, 
1488                                                   NSEC_PER_SEC, 
1489                                                   &tsave->tv_nsec);
1490                 /*
1491                  * Restart works by saving the time remaing in 
1492                  * arg2 & 3 (it is 64-bits of jiffies).  The other
1493                  * info we need is the clock_id (saved in arg0). 
1494                  * The sys_call interface needs the users 
1495                  * timespec return address which _it_ saves in arg1.
1496                  * Since we have cast the nanosleep call to a clock_nanosleep
1497                  * both can be restarted with the same code.
1498                  */
1499                 restart_block->fn = clock_nanosleep_restart;
1500                 restart_block->arg0 = which_clock;
1501                 /*
1502                  * Caller sets arg1
1503                  */
1504                 restart_block->arg2 = rq_time & 0xffffffffLL;
1505                 restart_block->arg3 = rq_time >> 32;
1506
1507                 return -ERESTART_RESTARTBLOCK;
1508         }
1509
1510         return 0;
1511 }
1512 /*
1513  * This will restart clock_nanosleep.
1514  */
1515 long
1516 clock_nanosleep_restart(struct restart_block *restart_block)
1517 {
1518         struct timespec t;
1519         int ret = common_nsleep(restart_block->arg0, 0, &t);
1520
1521         if ((ret == -ERESTART_RESTARTBLOCK) && restart_block->arg1 &&
1522             copy_to_user((struct timespec __user *)(restart_block->arg1), &t,
1523                          sizeof (t)))
1524                 return -EFAULT;
1525         return ret;
1526 }