4 * Kernel internal timers, kernel timekeeping, basic process system calls
6 * Copyright (C) 1991, 1992 Linus Torvalds
8 * 1997-01-28 Modified by Finn Arne Gangstad to make timers scale better.
10 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
11 * "A Kernel Model for Precision Timekeeping" by Dave Mills
12 * 1998-12-24 Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
13 * serialize accesses to xtime/lost_ticks).
14 * Copyright (C) 1998 Andrea Arcangeli
15 * 1999-03-10 Improved NTP compatibility by Ulrich Windl
16 * 2002-05-31 Move sys_sysinfo here and make its locking sane, Robert Love
17 * 2000-10-05 Implemented scalable SMP per-CPU timer handling.
18 * Copyright (C) 2000, 2001, 2002 Ingo Molnar
19 * Designed by David S. Miller, Alexey Kuznetsov and Ingo Molnar
22 #include <linux/kernel_stat.h>
23 #include <linux/module.h>
24 #include <linux/interrupt.h>
25 #include <linux/percpu.h>
26 #include <linux/init.h>
28 #include <linux/swap.h>
29 #include <linux/notifier.h>
30 #include <linux/thread_info.h>
31 #include <linux/time.h>
32 #include <linux/jiffies.h>
33 #include <linux/cpu.h>
34 #include <linux/vserver/sched.h>
35 #include <linux/vserver/cvirt.h>
37 #include <asm/uaccess.h>
38 #include <asm/div64.h>
39 #include <asm/timex.h>
42 * per-CPU timer vector definitions:
46 #define TVN_SIZE (1 << TVN_BITS)
47 #define TVR_SIZE (1 << TVR_BITS)
48 #define TVN_MASK (TVN_SIZE - 1)
49 #define TVR_MASK (TVR_SIZE - 1)
51 typedef struct tvec_s {
52 struct list_head vec[TVN_SIZE];
55 typedef struct tvec_root_s {
56 struct list_head vec[TVR_SIZE];
59 struct tvec_t_base_s {
61 unsigned long timer_jiffies;
62 struct timer_list *running_timer;
68 } ____cacheline_aligned_in_smp;
70 typedef struct tvec_t_base_s tvec_base_t;
72 static inline void set_running_timer(tvec_base_t *base,
73 struct timer_list *timer)
76 base->running_timer = timer;
80 /* Fake initialization */
81 static DEFINE_PER_CPU(tvec_base_t, tvec_bases) = { SPIN_LOCK_UNLOCKED };
83 static void check_timer_failed(struct timer_list *timer)
85 static int whine_count;
86 if (whine_count < 16) {
88 printk("Uninitialised timer!\n");
89 printk("This is just a warning. Your computer is OK\n");
90 printk("function=0x%p, data=0x%lx\n",
91 timer->function, timer->data);
97 spin_lock_init(&timer->lock);
98 timer->magic = TIMER_MAGIC;
101 static inline void check_timer(struct timer_list *timer)
103 if (timer->magic != TIMER_MAGIC)
104 check_timer_failed(timer);
108 static void internal_add_timer(tvec_base_t *base, struct timer_list *timer)
110 unsigned long expires = timer->expires;
111 unsigned long idx = expires - base->timer_jiffies;
112 struct list_head *vec;
114 if (idx < TVR_SIZE) {
115 int i = expires & TVR_MASK;
116 vec = base->tv1.vec + i;
117 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
118 int i = (expires >> TVR_BITS) & TVN_MASK;
119 vec = base->tv2.vec + i;
120 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
121 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
122 vec = base->tv3.vec + i;
123 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
124 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
125 vec = base->tv4.vec + i;
126 } else if ((signed long) idx < 0) {
128 * Can happen if you add a timer with expires == jiffies,
129 * or you set a timer to go off in the past
131 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
134 /* If the timeout is larger than 0xffffffff on 64-bit
135 * architectures then we use the maximum timeout:
137 if (idx > 0xffffffffUL) {
139 expires = idx + base->timer_jiffies;
141 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
142 vec = base->tv5.vec + i;
147 list_add_tail(&timer->entry, vec);
150 int __mod_timer(struct timer_list *timer, unsigned long expires)
152 tvec_base_t *old_base, *new_base;
156 BUG_ON(!timer->function);
160 spin_lock_irqsave(&timer->lock, flags);
161 new_base = &__get_cpu_var(tvec_bases);
163 old_base = timer->base;
166 * Prevent deadlocks via ordering by old_base < new_base.
168 if (old_base && (new_base != old_base)) {
169 if (old_base < new_base) {
170 spin_lock(&new_base->lock);
171 spin_lock(&old_base->lock);
173 spin_lock(&old_base->lock);
174 spin_lock(&new_base->lock);
177 * The timer base might have been cancelled while we were
178 * trying to take the lock(s):
180 if (timer->base != old_base) {
181 spin_unlock(&new_base->lock);
182 spin_unlock(&old_base->lock);
186 spin_lock(&new_base->lock);
187 if (timer->base != old_base) {
188 spin_unlock(&new_base->lock);
194 * Delete the previous timeout (if there was any), and install
198 list_del(&timer->entry);
201 timer->expires = expires;
202 internal_add_timer(new_base, timer);
203 timer->base = new_base;
205 if (old_base && (new_base != old_base))
206 spin_unlock(&old_base->lock);
207 spin_unlock(&new_base->lock);
208 spin_unlock_irqrestore(&timer->lock, flags);
213 EXPORT_SYMBOL(__mod_timer);
216 * add_timer_on - start a timer on a particular CPU
217 * @timer: the timer to be added
218 * @cpu: the CPU to start it on
220 * This is not very scalable on SMP. Double adds are not possible.
222 void add_timer_on(struct timer_list *timer, int cpu)
224 tvec_base_t *base = &per_cpu(tvec_bases, cpu);
227 BUG_ON(timer_pending(timer) || !timer->function);
231 spin_lock_irqsave(&base->lock, flags);
232 internal_add_timer(base, timer);
234 spin_unlock_irqrestore(&base->lock, flags);
238 * mod_timer - modify a timer's timeout
239 * @timer: the timer to be modified
241 * mod_timer is a more efficient way to update the expire field of an
242 * active timer (if the timer is inactive it will be activated)
244 * mod_timer(timer, expires) is equivalent to:
246 * del_timer(timer); timer->expires = expires; add_timer(timer);
248 * Note that if there are multiple unserialized concurrent users of the
249 * same timer, then mod_timer() is the only safe way to modify the timeout,
250 * since add_timer() cannot modify an already running timer.
252 * The function returns whether it has modified a pending timer or not.
253 * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
254 * active timer returns 1.)
256 int mod_timer(struct timer_list *timer, unsigned long expires)
258 BUG_ON(!timer->function);
263 * This is a common optimization triggered by the
264 * networking code - if the timer is re-modified
265 * to be the same thing then just return:
267 if (timer->expires == expires && timer_pending(timer))
270 return __mod_timer(timer, expires);
273 EXPORT_SYMBOL(mod_timer);
276 * del_timer - deactive a timer.
277 * @timer: the timer to be deactivated
279 * del_timer() deactivates a timer - this works on both active and inactive
282 * The function returns whether it has deactivated a pending timer or not.
283 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
284 * active timer returns 1.)
286 int del_timer(struct timer_list *timer)
297 spin_lock_irqsave(&base->lock, flags);
298 if (base != timer->base) {
299 spin_unlock_irqrestore(&base->lock, flags);
302 list_del(&timer->entry);
304 spin_unlock_irqrestore(&base->lock, flags);
309 EXPORT_SYMBOL(del_timer);
313 * del_timer_sync - deactivate a timer and wait for the handler to finish.
314 * @timer: the timer to be deactivated
316 * This function only differs from del_timer() on SMP: besides deactivating
317 * the timer it also makes sure the handler has finished executing on other
320 * Synchronization rules: callers must prevent restarting of the timer,
321 * otherwise this function is meaningless. It must not be called from
322 * interrupt contexts. Upon exit the timer is not queued and the handler
323 * is not running on any CPU.
325 * The function returns whether it has deactivated a pending timer or not.
327 int del_timer_sync(struct timer_list *timer)
335 ret += del_timer(timer);
338 base = &per_cpu(tvec_bases, i);
339 if (base->running_timer == timer) {
340 while (base->running_timer == timer) {
342 preempt_check_resched();
348 if (timer_pending(timer))
354 EXPORT_SYMBOL(del_timer_sync);
357 static int cascade(tvec_base_t *base, tvec_t *tv, int index)
359 /* cascade all the timers from tv up one level */
360 struct list_head *head, *curr;
362 head = tv->vec + index;
365 * We are removing _all_ timers from the list, so we don't have to
366 * detach them individually, just clear the list afterwards.
368 while (curr != head) {
369 struct timer_list *tmp;
371 tmp = list_entry(curr, struct timer_list, entry);
372 BUG_ON(tmp->base != base);
374 internal_add_timer(base, tmp);
376 INIT_LIST_HEAD(head);
382 * __run_timers - run all expired timers (if any) on this CPU.
383 * @base: the timer vector to be processed.
385 * This function cascades all vectors and executes all expired timer
388 #define INDEX(N) (base->timer_jiffies >> (TVR_BITS + N * TVN_BITS)) & TVN_MASK
390 static inline void __run_timers(tvec_base_t *base)
392 struct timer_list *timer;
394 spin_lock_irq(&base->lock);
395 while (time_after_eq(jiffies, base->timer_jiffies)) {
396 struct list_head work_list = LIST_HEAD_INIT(work_list);
397 struct list_head *head = &work_list;
398 int index = base->timer_jiffies & TVR_MASK;
404 (!cascade(base, &base->tv2, INDEX(0))) &&
405 (!cascade(base, &base->tv3, INDEX(1))) &&
406 !cascade(base, &base->tv4, INDEX(2)))
407 cascade(base, &base->tv5, INDEX(3));
408 ++base->timer_jiffies;
409 list_splice_init(base->tv1.vec + index, &work_list);
411 if (!list_empty(head)) {
412 void (*fn)(unsigned long);
415 timer = list_entry(head->next,struct timer_list,entry);
416 fn = timer->function;
419 list_del(&timer->entry);
420 set_running_timer(base, timer);
423 spin_unlock_irq(&base->lock);
425 spin_lock_irq(&base->lock);
429 set_running_timer(base, NULL);
430 spin_unlock_irq(&base->lock);
433 #ifdef CONFIG_NO_IDLE_HZ
435 * Find out when the next timer event is due to happen. This
436 * is used on S/390 to stop all activity when a cpus is idle.
437 * This functions needs to be called disabled.
439 unsigned long next_timer_interrupt(void)
442 struct list_head *list;
443 struct timer_list *nte;
444 unsigned long expires;
448 base = &__get_cpu_var(tvec_bases);
449 spin_lock(&base->lock);
450 expires = base->timer_jiffies + (LONG_MAX >> 1);
453 /* Look for timer events in tv1. */
454 j = base->timer_jiffies & TVR_MASK;
456 list_for_each_entry(nte, base->tv1.vec + j, entry) {
457 expires = nte->expires;
458 if (j < (base->timer_jiffies & TVR_MASK))
459 list = base->tv2.vec + (INDEX(0));
462 j = (j + 1) & TVR_MASK;
463 } while (j != (base->timer_jiffies & TVR_MASK));
466 varray[0] = &base->tv2;
467 varray[1] = &base->tv3;
468 varray[2] = &base->tv4;
469 varray[3] = &base->tv5;
470 for (i = 0; i < 4; i++) {
473 if (list_empty(varray[i]->vec + j)) {
474 j = (j + 1) & TVN_MASK;
477 list_for_each_entry(nte, varray[i]->vec + j, entry)
478 if (time_before(nte->expires, expires))
479 expires = nte->expires;
480 if (j < (INDEX(i)) && i < 3)
481 list = varray[i + 1]->vec + (INDEX(i + 1));
483 } while (j != (INDEX(i)));
488 * The search wrapped. We need to look at the next list
489 * from next tv element that would cascade into tv element
490 * where we found the timer element.
492 list_for_each_entry(nte, list, entry) {
493 if (time_before(nte->expires, expires))
494 expires = nte->expires;
497 spin_unlock(&base->lock);
502 /******************************************************************/
505 * Timekeeping variables
507 unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */
508 unsigned long tick_nsec = TICK_NSEC; /* ACTHZ period (nsec) */
512 * wall_to_monotonic is what we need to add to xtime (or xtime corrected
513 * for sub jiffie times) to get to monotonic time. Monotonic is pegged at zero
514 * at zero at system boot time, so wall_to_monotonic will be negative,
515 * however, we will ALWAYS keep the tv_nsec part positive so we can use
516 * the usual normalization.
518 struct timespec xtime __attribute__ ((aligned (16)));
519 struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
521 EXPORT_SYMBOL(xtime);
523 /* Don't completely fail for HZ > 500. */
524 int tickadj = 500/HZ ? : 1; /* microsecs */
528 * phase-lock loop variables
530 /* TIME_ERROR prevents overwriting the CMOS clock */
531 int time_state = TIME_OK; /* clock synchronization status */
532 int time_status = STA_UNSYNC; /* clock status bits */
533 long time_offset; /* time adjustment (us) */
534 long time_constant = 2; /* pll time constant */
535 long time_tolerance = MAXFREQ; /* frequency tolerance (ppm) */
536 long time_precision = 1; /* clock precision (us) */
537 long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
538 long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
539 long time_phase; /* phase offset (scaled us) */
540 long time_freq = (((NSEC_PER_SEC + HZ/2) % HZ - HZ/2) << SHIFT_USEC) / NSEC_PER_USEC;
541 /* frequency offset (scaled ppm)*/
542 long time_adj; /* tick adjust (scaled 1 / HZ) */
543 long time_reftime; /* time at last adjustment (s) */
545 long time_next_adjust;
548 * this routine handles the overflow of the microsecond field
550 * The tricky bits of code to handle the accurate clock support
551 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
552 * They were originally developed for SUN and DEC kernels.
553 * All the kudos should go to Dave for this stuff.
556 static void second_overflow(void)
560 /* Bump the maxerror field */
561 time_maxerror += time_tolerance >> SHIFT_USEC;
562 if ( time_maxerror > NTP_PHASE_LIMIT ) {
563 time_maxerror = NTP_PHASE_LIMIT;
564 time_status |= STA_UNSYNC;
568 * Leap second processing. If in leap-insert state at
569 * the end of the day, the system clock is set back one
570 * second; if in leap-delete state, the system clock is
571 * set ahead one second. The microtime() routine or
572 * external clock driver will insure that reported time
573 * is always monotonic. The ugly divides should be
576 switch (time_state) {
579 if (time_status & STA_INS)
580 time_state = TIME_INS;
581 else if (time_status & STA_DEL)
582 time_state = TIME_DEL;
586 if (xtime.tv_sec % 86400 == 0) {
588 wall_to_monotonic.tv_sec++;
589 time_interpolator_update(-NSEC_PER_SEC);
590 time_state = TIME_OOP;
592 printk(KERN_NOTICE "Clock: inserting leap second 23:59:60 UTC\n");
597 if ((xtime.tv_sec + 1) % 86400 == 0) {
599 wall_to_monotonic.tv_sec--;
600 time_interpolator_update(NSEC_PER_SEC);
601 time_state = TIME_WAIT;
603 printk(KERN_NOTICE "Clock: deleting leap second 23:59:59 UTC\n");
608 time_state = TIME_WAIT;
612 if (!(time_status & (STA_INS | STA_DEL)))
613 time_state = TIME_OK;
617 * Compute the phase adjustment for the next second. In
618 * PLL mode, the offset is reduced by a fixed factor
619 * times the time constant. In FLL mode the offset is
620 * used directly. In either mode, the maximum phase
621 * adjustment for each second is clamped so as to spread
622 * the adjustment over not more than the number of
623 * seconds between updates.
625 if (time_offset < 0) {
626 ltemp = -time_offset;
627 if (!(time_status & STA_FLL))
628 ltemp >>= SHIFT_KG + time_constant;
629 if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE)
630 ltemp = (MAXPHASE / MINSEC) << SHIFT_UPDATE;
631 time_offset += ltemp;
632 time_adj = -ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
635 if (!(time_status & STA_FLL))
636 ltemp >>= SHIFT_KG + time_constant;
637 if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE)
638 ltemp = (MAXPHASE / MINSEC) << SHIFT_UPDATE;
639 time_offset -= ltemp;
640 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
644 * Compute the frequency estimate and additional phase
645 * adjustment due to frequency error for the next
646 * second. When the PPS signal is engaged, gnaw on the
647 * watchdog counter and update the frequency computed by
648 * the pll and the PPS signal.
651 if (pps_valid == PPS_VALID) { /* PPS signal lost */
652 pps_jitter = MAXTIME;
653 pps_stabil = MAXFREQ;
654 time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
655 STA_PPSWANDER | STA_PPSERROR);
657 ltemp = time_freq + pps_freq;
659 time_adj -= -ltemp >>
660 (SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE);
663 (SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE);
666 /* Compensate for (HZ==100) != (1 << SHIFT_HZ).
667 * Add 25% and 3.125% to get 128.125; => only 0.125% error (p. 14)
670 time_adj -= (-time_adj >> 2) + (-time_adj >> 5);
672 time_adj += (time_adj >> 2) + (time_adj >> 5);
675 /* Compensate for (HZ==1000) != (1 << SHIFT_HZ).
676 * Add 1.5625% and 0.78125% to get 1023.4375; => only 0.05% error (p. 14)
679 time_adj -= (-time_adj >> 6) + (-time_adj >> 7);
681 time_adj += (time_adj >> 6) + (time_adj >> 7);
685 /* in the NTP reference this is called "hardclock()" */
686 static void update_wall_time_one_tick(void)
688 long time_adjust_step, delta_nsec;
690 if ( (time_adjust_step = time_adjust) != 0 ) {
691 /* We are doing an adjtime thing.
693 * Prepare time_adjust_step to be within bounds.
694 * Note that a positive time_adjust means we want the clock
697 * Limit the amount of the step to be in the range
698 * -tickadj .. +tickadj
700 if (time_adjust > tickadj)
701 time_adjust_step = tickadj;
702 else if (time_adjust < -tickadj)
703 time_adjust_step = -tickadj;
705 /* Reduce by this step the amount of time left */
706 time_adjust -= time_adjust_step;
708 delta_nsec = tick_nsec + time_adjust_step * 1000;
710 * Advance the phase, once it gets to one microsecond, then
711 * advance the tick more.
713 time_phase += time_adj;
714 if (time_phase <= -FINENSEC) {
715 long ltemp = -time_phase >> (SHIFT_SCALE - 10);
716 time_phase += ltemp << (SHIFT_SCALE - 10);
719 else if (time_phase >= FINENSEC) {
720 long ltemp = time_phase >> (SHIFT_SCALE - 10);
721 time_phase -= ltemp << (SHIFT_SCALE - 10);
724 xtime.tv_nsec += delta_nsec;
725 time_interpolator_update(delta_nsec);
727 /* Changes by adjtime() do not take effect till next tick. */
728 if (time_next_adjust != 0) {
729 time_adjust = time_next_adjust;
730 time_next_adjust = 0;
735 * Using a loop looks inefficient, but "ticks" is
736 * usually just one (we shouldn't be losing ticks,
737 * we're doing this this way mainly for interrupt
738 * latency reasons, not because we think we'll
739 * have lots of lost timer ticks
741 static void update_wall_time(unsigned long ticks)
745 update_wall_time_one_tick();
748 if (xtime.tv_nsec >= 1000000000) {
749 xtime.tv_nsec -= 1000000000;
755 static inline void do_process_times(struct task_struct *p,
756 unsigned long user, unsigned long system)
760 psecs = (p->utime += user);
761 psecs += (p->stime += system);
762 if (psecs / HZ > p->rlim[RLIMIT_CPU].rlim_cur) {
763 /* Send SIGXCPU every second.. */
765 send_sig(SIGXCPU, p, 1);
766 /* and SIGKILL when we go over max.. */
767 if (psecs / HZ > p->rlim[RLIMIT_CPU].rlim_max)
768 send_sig(SIGKILL, p, 1);
772 static inline void do_it_virt(struct task_struct * p, unsigned long ticks)
774 unsigned long it_virt = p->it_virt_value;
779 it_virt = p->it_virt_incr;
780 send_sig(SIGVTALRM, p, 1);
782 p->it_virt_value = it_virt;
786 static inline void do_it_prof(struct task_struct *p)
788 unsigned long it_prof = p->it_prof_value;
791 if (--it_prof == 0) {
792 it_prof = p->it_prof_incr;
793 send_sig(SIGPROF, p, 1);
795 p->it_prof_value = it_prof;
799 void update_one_process(struct task_struct *p, unsigned long user,
800 unsigned long system, int cpu)
802 do_process_times(p, user, system);
808 * Called from the timer interrupt handler to charge one tick to the current
809 * process. user_tick is 1 if the tick is user time, 0 for system.
811 void update_process_times(int user_tick)
813 struct task_struct *p = current;
814 int cpu = smp_processor_id(), system = user_tick ^ 1;
816 update_one_process(p, user_tick, system, cpu);
818 scheduler_tick(user_tick, system);
822 * Nr of active tasks - counted in fixed-point numbers
824 static unsigned long count_active_tasks(void)
826 return (nr_running() + nr_uninterruptible()) * FIXED_1;
830 * Hmm.. Changed this, as the GNU make sources (load.c) seems to
831 * imply that avenrun[] is the standard name for this kind of thing.
832 * Nothing else seems to be standardized: the fractional size etc
833 * all seem to differ on different machines.
835 * Requires xtime_lock to access.
837 unsigned long avenrun[3];
840 * calc_load - given tick count, update the avenrun load estimates.
841 * This is called while holding a write_lock on xtime_lock.
843 static inline void calc_load(unsigned long ticks)
845 unsigned long active_tasks; /* fixed-point */
846 static int count = LOAD_FREQ;
851 active_tasks = count_active_tasks();
852 CALC_LOAD(avenrun[0], EXP_1, active_tasks);
853 CALC_LOAD(avenrun[1], EXP_5, active_tasks);
854 CALC_LOAD(avenrun[2], EXP_15, active_tasks);
858 /* jiffies at the most recent update of wall time */
859 unsigned long wall_jiffies = INITIAL_JIFFIES;
862 * This read-write spinlock protects us from races in SMP while
863 * playing with xtime and avenrun.
865 #ifndef ARCH_HAVE_XTIME_LOCK
866 seqlock_t xtime_lock __cacheline_aligned_in_smp = SEQLOCK_UNLOCKED;
868 EXPORT_SYMBOL(xtime_lock);
872 * This function runs timers and the timer-tq in bottom half context.
874 static void run_timer_softirq(struct softirq_action *h)
876 tvec_base_t *base = &__get_cpu_var(tvec_bases);
878 if (time_after_eq(jiffies, base->timer_jiffies))
883 * Called by the local, per-CPU timer interrupt on SMP.
885 void run_local_timers(void)
887 raise_softirq(TIMER_SOFTIRQ);
891 * Called by the timer interrupt. xtime_lock must already be taken
894 static inline void update_times(void)
898 ticks = jiffies - wall_jiffies;
900 wall_jiffies += ticks;
901 update_wall_time(ticks);
907 * The 64-bit jiffies value is not atomic - you MUST NOT read it
908 * without sampling the sequence number in xtime_lock.
909 * jiffies is defined in the linker script...
912 void do_timer(struct pt_regs *regs)
916 /* SMP process accounting uses the local APIC timer */
918 update_process_times(user_mode(regs));
923 #if !defined(__alpha__) && !defined(__ia64__)
926 * For backwards compatibility? This can be done in libc so Alpha
927 * and all newer ports shouldn't need it.
929 asmlinkage unsigned long sys_alarm(unsigned int seconds)
931 struct itimerval it_new, it_old;
932 unsigned int oldalarm;
934 it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
935 it_new.it_value.tv_sec = seconds;
936 it_new.it_value.tv_usec = 0;
937 do_setitimer(ITIMER_REAL, &it_new, &it_old);
938 oldalarm = it_old.it_value.tv_sec;
939 /* ehhh.. We can't return 0 if we have an alarm pending.. */
940 /* And we'd better return too much than too little anyway */
941 if ((!oldalarm && it_old.it_value.tv_usec) || it_old.it_value.tv_usec >= 500000)
951 * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
952 * should be moved into arch/i386 instead?
956 * sys_getpid - return the thread group id of the current process
958 * Note, despite the name, this returns the tgid not the pid. The tgid and
959 * the pid are identical unless CLONE_THREAD was specified on clone() in
960 * which case the tgid is the same in all threads of the same group.
962 * This is SMP safe as current->tgid does not change.
964 asmlinkage long sys_getpid(void)
966 return vx_map_tgid(current->vx_info, current->tgid);
970 * Accessing ->group_leader->real_parent is not SMP-safe, it could
971 * change from under us. However, rather than getting any lock
972 * we can use an optimistic algorithm: get the parent
973 * pid, and go back and check that the parent is still
974 * the same. If it has changed (which is extremely unlikely
975 * indeed), we just try again..
977 * NOTE! This depends on the fact that even if we _do_
978 * get an old value of "parent", we can happily dereference
979 * the pointer (it was and remains a dereferencable kernel pointer
980 * no matter what): we just can't necessarily trust the result
981 * until we know that the parent pointer is valid.
983 * NOTE2: ->group_leader never changes from under us.
985 asmlinkage long sys_getppid(void)
988 struct task_struct *me = current;
989 struct task_struct *parent;
991 parent = me->group_leader->real_parent;
996 struct task_struct *old = parent;
999 * Make sure we read the pid before re-reading the
1003 parent = me->group_leader->real_parent;
1010 return vx_map_tgid(current->vx_info, pid);
1013 asmlinkage long sys_getuid(void)
1015 /* Only we change this so SMP safe */
1016 return current->uid;
1019 asmlinkage long sys_geteuid(void)
1021 /* Only we change this so SMP safe */
1022 return current->euid;
1025 asmlinkage long sys_getgid(void)
1027 /* Only we change this so SMP safe */
1028 return current->gid;
1031 asmlinkage long sys_getegid(void)
1033 /* Only we change this so SMP safe */
1034 return current->egid;
1039 static void process_timeout(unsigned long __data)
1041 wake_up_process((task_t *)__data);
1045 * schedule_timeout - sleep until timeout
1046 * @timeout: timeout value in jiffies
1048 * Make the current task sleep until @timeout jiffies have
1049 * elapsed. The routine will return immediately unless
1050 * the current task state has been set (see set_current_state()).
1052 * You can set the task state as follows -
1054 * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
1055 * pass before the routine returns. The routine will return 0
1057 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
1058 * delivered to the current task. In this case the remaining time
1059 * in jiffies will be returned, or 0 if the timer expired in time
1061 * The current task state is guaranteed to be TASK_RUNNING when this
1064 * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
1065 * the CPU away without a bound on the timeout. In this case the return
1066 * value will be %MAX_SCHEDULE_TIMEOUT.
1068 * In all cases the return value is guaranteed to be non-negative.
1070 fastcall signed long __sched schedule_timeout(signed long timeout)
1072 struct timer_list timer;
1073 unsigned long expire;
1077 case MAX_SCHEDULE_TIMEOUT:
1079 * These two special cases are useful to be comfortable
1080 * in the caller. Nothing more. We could take
1081 * MAX_SCHEDULE_TIMEOUT from one of the negative value
1082 * but I' d like to return a valid offset (>=0) to allow
1083 * the caller to do everything it want with the retval.
1089 * Another bit of PARANOID. Note that the retval will be
1090 * 0 since no piece of kernel is supposed to do a check
1091 * for a negative retval of schedule_timeout() (since it
1092 * should never happens anyway). You just have the printk()
1093 * that will tell you if something is gone wrong and where.
1097 printk(KERN_ERR "schedule_timeout: wrong timeout "
1098 "value %lx from %p\n", timeout,
1099 __builtin_return_address(0));
1100 current->state = TASK_RUNNING;
1105 expire = timeout + jiffies;
1108 timer.expires = expire;
1109 timer.data = (unsigned long) current;
1110 timer.function = process_timeout;
1114 del_timer_sync(&timer);
1116 timeout = expire - jiffies;
1119 return timeout < 0 ? 0 : timeout;
1122 EXPORT_SYMBOL(schedule_timeout);
1124 /* Thread ID - the internal kernel "pid" */
1125 asmlinkage long sys_gettid(void)
1127 return current->pid;
1130 static long __sched nanosleep_restart(struct restart_block *restart)
1132 unsigned long expire = restart->arg0, now = jiffies;
1133 struct timespec __user *rmtp = (struct timespec __user *) restart->arg1;
1136 /* Did it expire while we handled signals? */
1137 if (!time_after(expire, now))
1140 current->state = TASK_INTERRUPTIBLE;
1141 expire = schedule_timeout(expire - now);
1146 jiffies_to_timespec(expire, &t);
1148 ret = -ERESTART_RESTARTBLOCK;
1149 if (rmtp && copy_to_user(rmtp, &t, sizeof(t)))
1151 /* The 'restart' block is already filled in */
1156 asmlinkage long sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
1159 unsigned long expire;
1162 if (copy_from_user(&t, rqtp, sizeof(t)))
1165 if ((t.tv_nsec >= 1000000000L) || (t.tv_nsec < 0) || (t.tv_sec < 0))
1168 expire = timespec_to_jiffies(&t) + (t.tv_sec || t.tv_nsec);
1169 current->state = TASK_INTERRUPTIBLE;
1170 expire = schedule_timeout(expire);
1174 struct restart_block *restart;
1175 jiffies_to_timespec(expire, &t);
1176 if (rmtp && copy_to_user(rmtp, &t, sizeof(t)))
1179 restart = ¤t_thread_info()->restart_block;
1180 restart->fn = nanosleep_restart;
1181 restart->arg0 = jiffies + expire;
1182 restart->arg1 = (unsigned long) rmtp;
1183 ret = -ERESTART_RESTARTBLOCK;
1189 * sys_sysinfo - fill in sysinfo struct
1191 asmlinkage long sys_sysinfo(struct sysinfo __user *info)
1194 unsigned long mem_total, sav_total;
1195 unsigned int mem_unit, bitcount;
1198 memset((char *)&val, 0, sizeof(struct sysinfo));
1202 seq = read_seqbegin(&xtime_lock);
1205 * This is annoying. The below is the same thing
1206 * posix_get_clock_monotonic() does, but it wants to
1207 * take the lock which we want to cover the loads stuff
1211 do_gettimeofday((struct timeval *)&tp);
1212 tp.tv_nsec *= NSEC_PER_USEC;
1213 tp.tv_sec += wall_to_monotonic.tv_sec;
1214 tp.tv_nsec += wall_to_monotonic.tv_nsec;
1215 if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
1216 tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
1219 if (vx_flags(VXF_VIRT_UPTIME, 0))
1220 vx_vsi_uptime(&tp, NULL);
1221 val.uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
1223 val.loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
1224 val.loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
1225 val.loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
1227 val.procs = nr_threads;
1228 } while (read_seqretry(&xtime_lock, seq));
1230 /* if (vx_flags(VXF_VIRT_CPU, 0))
1237 * If the sum of all the available memory (i.e. ram + swap)
1238 * is less than can be stored in a 32 bit unsigned long then
1239 * we can be binary compatible with 2.2.x kernels. If not,
1240 * well, in that case 2.2.x was broken anyways...
1242 * -Erik Andersen <andersee@debian.org>
1245 mem_total = val.totalram + val.totalswap;
1246 if (mem_total < val.totalram || mem_total < val.totalswap)
1249 mem_unit = val.mem_unit;
1250 while (mem_unit > 1) {
1253 sav_total = mem_total;
1255 if (mem_total < sav_total)
1260 * If mem_total did not overflow, multiply all memory values by
1261 * val.mem_unit and set it to 1. This leaves things compatible
1262 * with 2.2.x, and also retains compatibility with earlier 2.4.x
1267 val.totalram <<= bitcount;
1268 val.freeram <<= bitcount;
1269 val.sharedram <<= bitcount;
1270 val.bufferram <<= bitcount;
1271 val.totalswap <<= bitcount;
1272 val.freeswap <<= bitcount;
1273 val.totalhigh <<= bitcount;
1274 val.freehigh <<= bitcount;
1277 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
1283 static void __devinit init_timers_cpu(int cpu)
1288 base = &per_cpu(tvec_bases, cpu);
1289 spin_lock_init(&base->lock);
1290 for (j = 0; j < TVN_SIZE; j++) {
1291 INIT_LIST_HEAD(base->tv5.vec + j);
1292 INIT_LIST_HEAD(base->tv4.vec + j);
1293 INIT_LIST_HEAD(base->tv3.vec + j);
1294 INIT_LIST_HEAD(base->tv2.vec + j);
1296 for (j = 0; j < TVR_SIZE; j++)
1297 INIT_LIST_HEAD(base->tv1.vec + j);
1299 base->timer_jiffies = jiffies;
1302 #ifdef CONFIG_HOTPLUG_CPU
1303 static int migrate_timer_list(tvec_base_t *new_base, struct list_head *head)
1305 struct timer_list *timer;
1307 while (!list_empty(head)) {
1308 timer = list_entry(head->next, struct timer_list, entry);
1309 /* We're locking backwards from __mod_timer order here,
1311 if (!spin_trylock(&timer->lock))
1313 list_del(&timer->entry);
1314 internal_add_timer(new_base, timer);
1315 timer->base = new_base;
1316 spin_unlock(&timer->lock);
1321 static void __devinit migrate_timers(int cpu)
1323 tvec_base_t *old_base;
1324 tvec_base_t *new_base;
1327 BUG_ON(cpu_online(cpu));
1328 old_base = &per_cpu(tvec_bases, cpu);
1329 new_base = &get_cpu_var(tvec_bases);
1331 local_irq_disable();
1333 /* Prevent deadlocks via ordering by old_base < new_base. */
1334 if (old_base < new_base) {
1335 spin_lock(&new_base->lock);
1336 spin_lock(&old_base->lock);
1338 spin_lock(&old_base->lock);
1339 spin_lock(&new_base->lock);
1342 if (old_base->running_timer)
1344 for (i = 0; i < TVR_SIZE; i++)
1345 if (!migrate_timer_list(new_base, old_base->tv1.vec + i))
1347 for (i = 0; i < TVN_SIZE; i++)
1348 if (!migrate_timer_list(new_base, old_base->tv2.vec + i)
1349 || !migrate_timer_list(new_base, old_base->tv3.vec + i)
1350 || !migrate_timer_list(new_base, old_base->tv4.vec + i)
1351 || !migrate_timer_list(new_base, old_base->tv5.vec + i))
1353 spin_unlock(&old_base->lock);
1354 spin_unlock(&new_base->lock);
1356 put_cpu_var(tvec_bases);
1360 /* Avoid deadlock with __mod_timer, by backing off. */
1361 spin_unlock(&old_base->lock);
1362 spin_unlock(&new_base->lock);
1366 #endif /* CONFIG_HOTPLUG_CPU */
1368 static int __devinit timer_cpu_notify(struct notifier_block *self,
1369 unsigned long action, void *hcpu)
1371 long cpu = (long)hcpu;
1373 case CPU_UP_PREPARE:
1374 init_timers_cpu(cpu);
1376 #ifdef CONFIG_HOTPLUG_CPU
1378 migrate_timers(cpu);
1387 static struct notifier_block __devinitdata timers_nb = {
1388 .notifier_call = timer_cpu_notify,
1392 void __init init_timers(void)
1394 timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
1395 (void *)(long)smp_processor_id());
1396 register_cpu_notifier(&timers_nb);
1397 open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
1400 #ifdef CONFIG_TIME_INTERPOLATION
1401 volatile unsigned long last_nsec_offset;
1402 #ifndef __HAVE_ARCH_CMPXCHG
1403 spinlock_t last_nsec_offset_lock = SPIN_LOCK_UNLOCKED;
1406 struct time_interpolator *time_interpolator;
1407 static struct time_interpolator *time_interpolator_list;
1408 static spinlock_t time_interpolator_lock = SPIN_LOCK_UNLOCKED;
1411 is_better_time_interpolator(struct time_interpolator *new)
1413 if (!time_interpolator)
1415 return new->frequency > 2*time_interpolator->frequency ||
1416 (unsigned long)new->drift < (unsigned long)time_interpolator->drift;
1420 register_time_interpolator(struct time_interpolator *ti)
1422 spin_lock(&time_interpolator_lock);
1423 write_seqlock_irq(&xtime_lock);
1424 if (is_better_time_interpolator(ti))
1425 time_interpolator = ti;
1426 write_sequnlock_irq(&xtime_lock);
1428 ti->next = time_interpolator_list;
1429 time_interpolator_list = ti;
1430 spin_unlock(&time_interpolator_lock);
1434 unregister_time_interpolator(struct time_interpolator *ti)
1436 struct time_interpolator *curr, **prev;
1438 spin_lock(&time_interpolator_lock);
1439 prev = &time_interpolator_list;
1440 for (curr = *prev; curr; curr = curr->next) {
1448 write_seqlock_irq(&xtime_lock);
1449 if (ti == time_interpolator) {
1450 /* we lost the best time-interpolator: */
1451 time_interpolator = NULL;
1452 /* find the next-best interpolator */
1453 for (curr = time_interpolator_list; curr; curr = curr->next)
1454 if (is_better_time_interpolator(curr))
1455 time_interpolator = curr;
1457 write_sequnlock_irq(&xtime_lock);
1458 spin_unlock(&time_interpolator_lock);
1460 #endif /* CONFIG_TIME_INTERPOLATION */