1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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>
27#include <linux/mm.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
35#include <asm/uaccess.h>
36#include <asm/unistd.h>
37#include <asm/div64.h>
38#include <asm/timex.h>
39#include <asm/io.h>
40
41#ifdef CONFIG_TIME_INTERPOLATION
42static void time_interpolator_update(long delta_nsec);
43#else
44#define time_interpolator_update(x)
45#endif
46
47
48
49
50#define TVN_BITS 6
51#define TVR_BITS 8
52#define TVN_SIZE (1 << TVN_BITS)
53#define TVR_SIZE (1 << TVR_BITS)
54#define TVN_MASK (TVN_SIZE - 1)
55#define TVR_MASK (TVR_SIZE - 1)
56
57typedef struct tvec_s {
58 struct list_head vec[TVN_SIZE];
59} tvec_t;
60
61typedef struct tvec_root_s {
62 struct list_head vec[TVR_SIZE];
63} tvec_root_t;
64
65struct tvec_t_base_s {
66 spinlock_t lock;
67 unsigned long timer_jiffies;
68 struct timer_list *running_timer;
69 tvec_root_t tv1;
70 tvec_t tv2;
71 tvec_t tv3;
72 tvec_t tv4;
73 tvec_t tv5;
74} ____cacheline_aligned_in_smp;
75
76typedef struct tvec_t_base_s tvec_base_t;
77
78static inline void set_running_timer(tvec_base_t *base,
79 struct timer_list *timer)
80{
81#ifdef CONFIG_SMP
82 base->running_timer = timer;
83#endif
84}
85
86
87static DEFINE_PER_CPU(tvec_base_t, tvec_bases) = { SPIN_LOCK_UNLOCKED };
88
89static void check_timer_failed(struct timer_list *timer)
90{
91 static int whine_count;
92 if (whine_count < 16) {
93 whine_count++;
94 printk("Uninitialised timer!\n");
95 printk("This is just a warning. Your computer is OK\n");
96 printk("function=0x%p, data=0x%lx\n",
97 timer->function, timer->data);
98 dump_stack();
99 }
100
101
102
103 spin_lock_init(&timer->lock);
104 timer->magic = TIMER_MAGIC;
105}
106
107static inline void check_timer(struct timer_list *timer)
108{
109 if (timer->magic != TIMER_MAGIC)
110 check_timer_failed(timer);
111}
112
113
114static void internal_add_timer(tvec_base_t *base, struct timer_list *timer)
115{
116 unsigned long expires = timer->expires;
117 unsigned long idx = expires - base->timer_jiffies;
118 struct list_head *vec;
119
120 if (idx < TVR_SIZE) {
121 int i = expires & TVR_MASK;
122 vec = base->tv1.vec + i;
123 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
124 int i = (expires >> TVR_BITS) & TVN_MASK;
125 vec = base->tv2.vec + i;
126 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
127 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
128 vec = base->tv3.vec + i;
129 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
130 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
131 vec = base->tv4.vec + i;
132 } else if ((signed long) idx < 0) {
133
134
135
136
137 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
138 } else {
139 int i;
140
141
142
143 if (idx > 0xffffffffUL) {
144 idx = 0xffffffffUL;
145 expires = idx + base->timer_jiffies;
146 }
147 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
148 vec = base->tv5.vec + i;
149 }
150
151
152
153 list_add_tail(&timer->entry, vec);
154}
155
156int __mod_timer(struct timer_list *timer, unsigned long expires)
157{
158 tvec_base_t *old_base, *new_base;
159 unsigned long flags;
160 int ret = 0;
161
162 BUG_ON(!timer->function);
163
164 check_timer(timer);
165
166 spin_lock_irqsave(&timer->lock, flags);
167 new_base = &__get_cpu_var(tvec_bases);
168repeat:
169 old_base = timer->base;
170
171
172
173
174 if (old_base && (new_base != old_base)) {
175 if (old_base < new_base) {
176 spin_lock(&new_base->lock);
177 spin_lock(&old_base->lock);
178 } else {
179 spin_lock(&old_base->lock);
180 spin_lock(&new_base->lock);
181 }
182
183
184
185
186 if (timer->base != old_base) {
187 spin_unlock(&new_base->lock);
188 spin_unlock(&old_base->lock);
189 goto repeat;
190 }
191 } else {
192 spin_lock(&new_base->lock);
193 if (timer->base != old_base) {
194 spin_unlock(&new_base->lock);
195 goto repeat;
196 }
197 }
198
199
200
201
202
203 if (old_base) {
204 list_del(&timer->entry);
205 ret = 1;
206 }
207 timer->expires = expires;
208 internal_add_timer(new_base, timer);
209 timer->base = new_base;
210
211 if (old_base && (new_base != old_base))
212 spin_unlock(&old_base->lock);
213 spin_unlock(&new_base->lock);
214 spin_unlock_irqrestore(&timer->lock, flags);
215
216 return ret;
217}
218
219EXPORT_SYMBOL(__mod_timer);
220
221
222
223
224
225
226
227
228void add_timer_on(struct timer_list *timer, int cpu)
229{
230 tvec_base_t *base = &per_cpu(tvec_bases, cpu);
231 unsigned long flags;
232
233 BUG_ON(timer_pending(timer) || !timer->function);
234
235 check_timer(timer);
236
237 spin_lock_irqsave(&base->lock, flags);
238 internal_add_timer(base, timer);
239 timer->base = base;
240 spin_unlock_irqrestore(&base->lock, flags);
241}
242
243EXPORT_SYMBOL(add_timer_on);
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264int mod_timer(struct timer_list *timer, unsigned long expires)
265{
266 BUG_ON(!timer->function);
267
268 check_timer(timer);
269
270
271
272
273
274
275 if (timer->expires == expires && timer_pending(timer))
276 return 1;
277
278 return __mod_timer(timer, expires);
279}
280
281EXPORT_SYMBOL(mod_timer);
282
283
284
285
286
287
288
289
290
291
292
293
294int del_timer(struct timer_list *timer)
295{
296 unsigned long flags;
297 tvec_base_t *base;
298
299 check_timer(timer);
300
301repeat:
302 base = timer->base;
303 if (!base)
304 return 0;
305 spin_lock_irqsave(&base->lock, flags);
306 if (base != timer->base) {
307 spin_unlock_irqrestore(&base->lock, flags);
308 goto repeat;
309 }
310 list_del(&timer->entry);
311 timer->base = NULL;
312 spin_unlock_irqrestore(&base->lock, flags);
313
314 return 1;
315}
316
317EXPORT_SYMBOL(del_timer);
318
319#ifdef CONFIG_SMP
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341int del_timer_sync(struct timer_list *timer)
342{
343 tvec_base_t *base;
344 int i, ret = 0;
345
346 check_timer(timer);
347
348del_again:
349 ret += del_timer(timer);
350
351 for_each_online_cpu(i) {
352 base = &per_cpu(tvec_bases, i);
353 if (base->running_timer == timer) {
354 while (base->running_timer == timer) {
355 cpu_relax();
356 preempt_check_resched();
357 }
358 break;
359 }
360 }
361 smp_rmb();
362 if (timer_pending(timer))
363 goto del_again;
364
365 return ret;
366}
367EXPORT_SYMBOL(del_timer_sync);
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385int del_singleshot_timer_sync(struct timer_list *timer)
386{
387 int ret = del_timer(timer);
388
389 if (!ret) {
390 ret = del_timer_sync(timer);
391 BUG_ON(ret);
392 }
393
394 return ret;
395}
396EXPORT_SYMBOL(del_singleshot_timer_sync);
397#endif
398
399static int cascade(tvec_base_t *base, tvec_t *tv, int index)
400{
401
402 struct list_head *head, *curr;
403
404 head = tv->vec + index;
405 curr = head->next;
406
407
408
409
410 while (curr != head) {
411 struct timer_list *tmp;
412
413 tmp = list_entry(curr, struct timer_list, entry);
414 BUG_ON(tmp->base != base);
415 curr = curr->next;
416 internal_add_timer(base, tmp);
417 }
418 INIT_LIST_HEAD(head);
419
420 return index;
421}
422
423
424
425
426
427
428
429
430#define INDEX(N) (base->timer_jiffies >> (TVR_BITS + N * TVN_BITS)) & TVN_MASK
431
432static inline void __run_timers(tvec_base_t *base)
433{
434 struct timer_list *timer;
435
436 spin_lock_irq(&base->lock);
437 while (time_after_eq(jiffies, base->timer_jiffies)) {
438 struct list_head work_list = LIST_HEAD_INIT(work_list);
439 struct list_head *head = &work_list;
440 int index = base->timer_jiffies & TVR_MASK;
441
442
443
444
445 if (!index &&
446 (!cascade(base, &base->tv2, INDEX(0))) &&
447 (!cascade(base, &base->tv3, INDEX(1))) &&
448 !cascade(base, &base->tv4, INDEX(2)))
449 cascade(base, &base->tv5, INDEX(3));
450 ++base->timer_jiffies;
451 list_splice_init(base->tv1.vec + index, &work_list);
452repeat:
453 if (!list_empty(head)) {
454 void (*fn)(unsigned long);
455 unsigned long data;
456
457 timer = list_entry(head->next,struct timer_list,entry);
458 fn = timer->function;
459 data = timer->data;
460
461 list_del(&timer->entry);
462 set_running_timer(base, timer);
463 smp_wmb();
464 timer->base = NULL;
465 spin_unlock_irq(&base->lock);
466 fn(data);
467 spin_lock_irq(&base->lock);
468 goto repeat;
469 }
470 }
471 set_running_timer(base, NULL);
472 spin_unlock_irq(&base->lock);
473}
474
475#ifdef CONFIG_NO_IDLE_HZ
476
477
478
479
480
481unsigned long next_timer_interrupt(void)
482{
483 tvec_base_t *base;
484 struct list_head *list;
485 struct timer_list *nte;
486 unsigned long expires;
487 tvec_t *varray[4];
488 int i, j;
489
490 base = &__get_cpu_var(tvec_bases);
491 spin_lock(&base->lock);
492 expires = base->timer_jiffies + (LONG_MAX >> 1);
493 list = 0;
494
495
496 j = base->timer_jiffies & TVR_MASK;
497 do {
498 list_for_each_entry(nte, base->tv1.vec + j, entry) {
499 expires = nte->expires;
500 if (j < (base->timer_jiffies & TVR_MASK))
501 list = base->tv2.vec + (INDEX(0));
502 goto found;
503 }
504 j = (j + 1) & TVR_MASK;
505 } while (j != (base->timer_jiffies & TVR_MASK));
506
507
508 varray[0] = &base->tv2;
509 varray[1] = &base->tv3;
510 varray[2] = &base->tv4;
511 varray[3] = &base->tv5;
512 for (i = 0; i < 4; i++) {
513 j = INDEX(i);
514 do {
515 if (list_empty(varray[i]->vec + j)) {
516 j = (j + 1) & TVN_MASK;
517 continue;
518 }
519 list_for_each_entry(nte, varray[i]->vec + j, entry)
520 if (time_before(nte->expires, expires))
521 expires = nte->expires;
522 if (j < (INDEX(i)) && i < 3)
523 list = varray[i + 1]->vec + (INDEX(i + 1));
524 goto found;
525 } while (j != (INDEX(i)));
526 }
527found:
528 if (list) {
529
530
531
532
533
534 list_for_each_entry(nte, list, entry) {
535 if (time_before(nte->expires, expires))
536 expires = nte->expires;
537 }
538 }
539 spin_unlock(&base->lock);
540 return expires;
541}
542#endif
543
544
545
546
547
548
549unsigned long tick_usec = TICK_USEC;
550unsigned long tick_nsec = TICK_NSEC;
551
552
553
554
555
556
557
558
559
560struct timespec xtime __attribute__ ((aligned (16)));
561struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
562
563EXPORT_SYMBOL(xtime);
564
565
566int tickadj = 500/HZ ? : 1;
567
568
569
570
571
572
573int time_state = TIME_OK;
574int time_status = STA_UNSYNC;
575long time_offset;
576long time_constant = 2;
577long time_tolerance = MAXFREQ;
578long time_precision = 1;
579long time_maxerror = NTP_PHASE_LIMIT;
580long time_esterror = NTP_PHASE_LIMIT;
581long time_phase;
582long time_freq = (((NSEC_PER_SEC + HZ/2) % HZ - HZ/2) << SHIFT_USEC) / NSEC_PER_USEC;
583
584long time_adj;
585long time_reftime;
586long time_adjust;
587long time_next_adjust;
588
589
590
591
592
593
594
595
596
597
598static void second_overflow(void)
599{
600 long ltemp;
601
602
603 time_maxerror += time_tolerance >> SHIFT_USEC;
604 if ( time_maxerror > NTP_PHASE_LIMIT ) {
605 time_maxerror = NTP_PHASE_LIMIT;
606 time_status |= STA_UNSYNC;
607 }
608
609
610
611
612
613
614
615
616
617
618 switch (time_state) {
619
620 case TIME_OK:
621 if (time_status & STA_INS)
622 time_state = TIME_INS;
623 else if (time_status & STA_DEL)
624 time_state = TIME_DEL;
625 break;
626
627 case TIME_INS:
628 if (xtime.tv_sec % 86400 == 0) {
629 xtime.tv_sec--;
630 wall_to_monotonic.tv_sec++;
631
632
633
634 time_interpolator_update(-NSEC_PER_SEC);
635 time_state = TIME_OOP;
636 clock_was_set();
637 printk(KERN_NOTICE "Clock: inserting leap second 23:59:60 UTC\n");
638 }
639 break;
640
641 case TIME_DEL:
642 if ((xtime.tv_sec + 1) % 86400 == 0) {
643 xtime.tv_sec++;
644 wall_to_monotonic.tv_sec--;
645
646 time_interpolator_update(NSEC_PER_SEC);
647 time_state = TIME_WAIT;
648 clock_was_set();
649 printk(KERN_NOTICE "Clock: deleting leap second 23:59:59 UTC\n");
650 }
651 break;
652
653 case TIME_OOP:
654 time_state = TIME_WAIT;
655 break;
656
657 case TIME_WAIT:
658 if (!(time_status & (STA_INS | STA_DEL)))
659 time_state = TIME_OK;
660 }
661
662
663
664
665
666
667
668
669
670
671 if (time_offset < 0) {
672 ltemp = -time_offset;
673 if (!(time_status & STA_FLL))
674 ltemp >>= SHIFT_KG + time_constant;
675 if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE)
676 ltemp = (MAXPHASE / MINSEC) << SHIFT_UPDATE;
677 time_offset += ltemp;
678 time_adj = -ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
679 } else {
680 ltemp = time_offset;
681 if (!(time_status & STA_FLL))
682 ltemp >>= SHIFT_KG + time_constant;
683 if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE)
684 ltemp = (MAXPHASE / MINSEC) << SHIFT_UPDATE;
685 time_offset -= ltemp;
686 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
687 }
688
689
690
691
692
693
694
695
696 pps_valid++;
697 if (pps_valid == PPS_VALID) {
698 pps_jitter = MAXTIME;
699 pps_stabil = MAXFREQ;
700 time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
701 STA_PPSWANDER | STA_PPSERROR);
702 }
703 ltemp = time_freq + pps_freq;
704 if (ltemp < 0)
705 time_adj -= -ltemp >>
706 (SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE);
707 else
708 time_adj += ltemp >>
709 (SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE);
710
711#if HZ == 100
712
713
714
715 if (time_adj < 0)
716 time_adj -= (-time_adj >> 2) + (-time_adj >> 5);
717 else
718 time_adj += (time_adj >> 2) + (time_adj >> 5);
719#endif
720#if HZ == 1000
721
722
723
724 if (time_adj < 0)
725 time_adj -= (-time_adj >> 6) + (-time_adj >> 7);
726 else
727 time_adj += (time_adj >> 6) + (time_adj >> 7);
728#endif
729}
730
731
732static void update_wall_time_one_tick(void)
733{
734 long time_adjust_step, delta_nsec;
735
736 if ( (time_adjust_step = time_adjust) != 0 ) {
737
738
739
740
741
742
743
744
745
746 if (time_adjust > tickadj)
747 time_adjust_step = tickadj;
748 else if (time_adjust < -tickadj)
749 time_adjust_step = -tickadj;
750
751
752 time_adjust -= time_adjust_step;
753 }
754 delta_nsec = tick_nsec + time_adjust_step * 1000;
755
756
757
758
759 time_phase += time_adj;
760 if (time_phase <= -FINENSEC) {
761 long ltemp = -time_phase >> (SHIFT_SCALE - 10);
762 time_phase += ltemp << (SHIFT_SCALE - 10);
763 delta_nsec -= ltemp;
764 }
765 else if (time_phase >= FINENSEC) {
766 long ltemp = time_phase >> (SHIFT_SCALE - 10);
767 time_phase -= ltemp << (SHIFT_SCALE - 10);
768 delta_nsec += ltemp;
769 }
770 xtime.tv_nsec += delta_nsec;
771 time_interpolator_update(delta_nsec);
772
773
774 if (time_next_adjust != 0) {
775 time_adjust = time_next_adjust;
776 time_next_adjust = 0;
777 }
778}
779
780
781
782
783
784
785
786
787static void update_wall_time(unsigned long ticks)
788{
789 do {
790 ticks--;
791 update_wall_time_one_tick();
792 } while (ticks);
793
794 if (xtime.tv_nsec >= 1000000000) {
795 xtime.tv_nsec -= 1000000000;
796 xtime.tv_sec++;
797 second_overflow();
798 }
799}
800
801static inline void do_process_times(struct task_struct *p,
802 unsigned long user, unsigned long system)
803{
804 unsigned long psecs;
805
806 psecs = (p->utime += user);
807 psecs += (p->stime += system);
808 if (psecs / HZ >= p->rlim[RLIMIT_CPU].rlim_cur) {
809
810 if (!(psecs % HZ))
811 send_sig(SIGXCPU, p, 1);
812
813 if (psecs / HZ >= p->rlim[RLIMIT_CPU].rlim_max)
814 send_sig(SIGKILL, p, 1);
815 }
816}
817
818static inline void do_it_virt(struct task_struct * p, unsigned long ticks)
819{
820 unsigned long it_virt = p->it_virt_value;
821
822 if (it_virt) {
823 it_virt -= ticks;
824 if (!it_virt) {
825 it_virt = p->it_virt_incr;
826 send_sig(SIGVTALRM, p, 1);
827 }
828 p->it_virt_value = it_virt;
829 }
830}
831
832static inline void do_it_prof(struct task_struct *p)
833{
834 unsigned long it_prof = p->it_prof_value;
835
836 if (it_prof) {
837 if (--it_prof == 0) {
838 it_prof = p->it_prof_incr;
839 send_sig(SIGPROF, p, 1);
840 }
841 p->it_prof_value = it_prof;
842 }
843}
844
845static void update_one_process(struct task_struct *p, unsigned long user,
846 unsigned long system, int cpu)
847{
848 do_process_times(p, user, system);
849 do_it_virt(p, user);
850 do_it_prof(p);
851}
852
853
854
855
856
857void update_process_times(int user_tick)
858{
859 struct task_struct *p = current;
860 int cpu = smp_processor_id(), system = user_tick ^ 1;
861
862 update_one_process(p, user_tick, system, cpu);
863 run_local_timers();
864 scheduler_tick(user_tick, system);
865}
866
867
868
869
870static unsigned long count_active_tasks(void)
871{
872 return (nr_running() + nr_uninterruptible()) * FIXED_1;
873}
874
875
876
877
878
879
880
881
882
883unsigned long avenrun[3];
884
885
886
887
888
889static inline void calc_load(unsigned long ticks)
890{
891 unsigned long active_tasks;
892 static int count = LOAD_FREQ;
893
894 count -= ticks;
895 if (count < 0) {
896 count += LOAD_FREQ;
897 active_tasks = count_active_tasks();
898 CALC_LOAD(avenrun[0], EXP_1, active_tasks);
899 CALC_LOAD(avenrun[1], EXP_5, active_tasks);
900 CALC_LOAD(avenrun[2], EXP_15, active_tasks);
901 }
902}
903
904
905unsigned long wall_jiffies = INITIAL_JIFFIES;
906
907
908
909
910
911#ifndef ARCH_HAVE_XTIME_LOCK
912seqlock_t xtime_lock __cacheline_aligned_in_smp = SEQLOCK_UNLOCKED;
913
914EXPORT_SYMBOL(xtime_lock);
915#endif
916
917
918
919
920static void run_timer_softirq(struct softirq_action *h)
921{
922 tvec_base_t *base = &__get_cpu_var(tvec_bases);
923
924 if (time_after_eq(jiffies, base->timer_jiffies))
925 __run_timers(base);
926}
927
928
929
930
931void run_local_timers(void)
932{
933 raise_softirq(TIMER_SOFTIRQ);
934}
935
936
937
938
939
940static inline void update_times(void)
941{
942 unsigned long ticks;
943
944 ticks = jiffies - wall_jiffies;
945 if (ticks) {
946 wall_jiffies += ticks;
947 update_wall_time(ticks);
948 }
949 calc_load(ticks);
950}
951
952
953
954
955
956
957
958void do_timer(struct pt_regs *regs)
959{
960 jiffies_64++;
961#ifndef CONFIG_SMP
962
963
964 update_process_times(user_mode(regs));
965#endif
966 update_times();
967}
968
969#ifdef __ARCH_WANT_SYS_ALARM
970
971
972
973
974
975asmlinkage unsigned long sys_alarm(unsigned int seconds)
976{
977 struct itimerval it_new, it_old;
978 unsigned int oldalarm;
979
980 it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
981 it_new.it_value.tv_sec = seconds;
982 it_new.it_value.tv_usec = 0;
983 do_setitimer(ITIMER_REAL, &it_new, &it_old);
984 oldalarm = it_old.it_value.tv_sec;
985
986
987 if ((!oldalarm && it_old.it_value.tv_usec) || it_old.it_value.tv_usec >= 500000)
988 oldalarm++;
989 return oldalarm;
990}
991
992#endif
993
994#ifndef __alpha__
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010asmlinkage long sys_getpid(void)
1011{
1012 return current->tgid;
1013}
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031asmlinkage long sys_getppid(void)
1032{
1033 int pid;
1034 struct task_struct *me = current;
1035 struct task_struct *parent;
1036
1037 parent = me->group_leader->real_parent;
1038 for (;;) {
1039 pid = parent->tgid;
1040#ifdef CONFIG_SMP
1041{
1042 struct task_struct *old = parent;
1043
1044
1045
1046
1047
1048 rmb();
1049 parent = me->group_leader->real_parent;
1050 if (old != parent)
1051 continue;
1052}
1053#endif
1054 break;
1055 }
1056 return pid;
1057}
1058
1059asmlinkage long sys_getuid(void)
1060{
1061
1062 return current->uid;
1063}
1064
1065asmlinkage long sys_geteuid(void)
1066{
1067
1068 return current->euid;
1069}
1070
1071asmlinkage long sys_getgid(void)
1072{
1073
1074 return current->gid;
1075}
1076
1077asmlinkage long sys_getegid(void)
1078{
1079
1080 return current->egid;
1081}
1082
1083#endif
1084
1085static void process_timeout(unsigned long __data)
1086{
1087 wake_up_process((task_t *)__data);
1088}
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116fastcall signed long __sched schedule_timeout(signed long timeout)
1117{
1118 struct timer_list timer;
1119 unsigned long expire;
1120
1121 switch (timeout)
1122 {
1123 case MAX_SCHEDULE_TIMEOUT:
1124
1125
1126
1127
1128
1129
1130
1131 schedule();
1132 goto out;
1133 default:
1134
1135
1136
1137
1138
1139
1140
1141 if (timeout < 0)
1142 {
1143 printk(KERN_ERR "schedule_timeout: wrong timeout "
1144 "value %lx from %p\n", timeout,
1145 __builtin_return_address(0));
1146 current->state = TASK_RUNNING;
1147 goto out;
1148 }
1149 }
1150
1151 expire = timeout + jiffies;
1152
1153 init_timer(&timer);
1154 timer.expires = expire;
1155 timer.data = (unsigned long) current;
1156 timer.function = process_timeout;
1157
1158 add_timer(&timer);
1159 schedule();
1160 del_singleshot_timer_sync(&timer);
1161
1162 timeout = expire - jiffies;
1163
1164 out:
1165 return timeout < 0 ? 0 : timeout;
1166}
1167
1168EXPORT_SYMBOL(schedule_timeout);
1169
1170
1171asmlinkage long sys_gettid(void)
1172{
1173 return current->pid;
1174}
1175
1176static long __sched nanosleep_restart(struct restart_block *restart)
1177{
1178 unsigned long expire = restart->arg0, now = jiffies;
1179 struct timespec __user *rmtp = (struct timespec __user *) restart->arg1;
1180 long ret;
1181
1182
1183 if (!time_after(expire, now))
1184 return 0;
1185
1186 current->state = TASK_INTERRUPTIBLE;
1187 expire = schedule_timeout(expire - now);
1188
1189 ret = 0;
1190 if (expire) {
1191 struct timespec t;
1192 jiffies_to_timespec(expire, &t);
1193
1194 ret = -ERESTART_RESTARTBLOCK;
1195 if (rmtp && copy_to_user(rmtp, &t, sizeof(t)))
1196 ret = -EFAULT;
1197
1198 }
1199 return ret;
1200}
1201
1202asmlinkage long sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
1203{
1204 struct timespec t;
1205 unsigned long expire;
1206 long ret;
1207
1208 if (copy_from_user(&t, rqtp, sizeof(t)))
1209 return -EFAULT;
1210
1211 if ((t.tv_nsec >= 1000000000L) || (t.tv_nsec < 0) || (t.tv_sec < 0))
1212 return -EINVAL;
1213
1214 expire = timespec_to_jiffies(&t) + (t.tv_sec || t.tv_nsec);
1215 current->state = TASK_INTERRUPTIBLE;
1216 expire = schedule_timeout(expire);
1217
1218 ret = 0;
1219 if (expire) {
1220 struct restart_block *restart;
1221 jiffies_to_timespec(expire, &t);
1222 if (rmtp && copy_to_user(rmtp, &t, sizeof(t)))
1223 return -EFAULT;
1224
1225 restart = ¤t_thread_info()->restart_block;
1226 restart->fn = nanosleep_restart;
1227 restart->arg0 = jiffies + expire;
1228 restart->arg1 = (unsigned long) rmtp;
1229 ret = -ERESTART_RESTARTBLOCK;
1230 }
1231 return ret;
1232}
1233
1234
1235
1236
1237asmlinkage long sys_sysinfo(struct sysinfo __user *info)
1238{
1239 struct sysinfo val;
1240 unsigned long mem_total, sav_total;
1241 unsigned int mem_unit, bitcount;
1242 unsigned long seq;
1243
1244 memset((char *)&val, 0, sizeof(struct sysinfo));
1245
1246 do {
1247 struct timespec tp;
1248 seq = read_seqbegin(&xtime_lock);
1249
1250
1251
1252
1253
1254
1255
1256
1257 getnstimeofday(&tp);
1258 tp.tv_sec += wall_to_monotonic.tv_sec;
1259 tp.tv_nsec += wall_to_monotonic.tv_nsec;
1260 if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
1261 tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
1262 tp.tv_sec++;
1263 }
1264 val.uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
1265
1266 val.loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
1267 val.loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
1268 val.loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
1269
1270 val.procs = nr_threads;
1271 } while (read_seqretry(&xtime_lock, seq));
1272
1273 si_meminfo(&val);
1274 si_swapinfo(&val);
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285 mem_total = val.totalram + val.totalswap;
1286 if (mem_total < val.totalram || mem_total < val.totalswap)
1287 goto out;
1288 bitcount = 0;
1289 mem_unit = val.mem_unit;
1290 while (mem_unit > 1) {
1291 bitcount++;
1292 mem_unit >>= 1;
1293 sav_total = mem_total;
1294 mem_total <<= 1;
1295 if (mem_total < sav_total)
1296 goto out;
1297 }
1298
1299
1300
1301
1302
1303
1304
1305
1306 val.mem_unit = 1;
1307 val.totalram <<= bitcount;
1308 val.freeram <<= bitcount;
1309 val.sharedram <<= bitcount;
1310 val.bufferram <<= bitcount;
1311 val.totalswap <<= bitcount;
1312 val.freeswap <<= bitcount;
1313 val.totalhigh <<= bitcount;
1314 val.freehigh <<= bitcount;
1315
1316 out:
1317 if (copy_to_user(info, &val, sizeof(struct sysinfo)))
1318 return -EFAULT;
1319
1320 return 0;
1321}
1322
1323static void __devinit init_timers_cpu(int cpu)
1324{
1325 int j;
1326 tvec_base_t *base;
1327
1328 base = &per_cpu(tvec_bases, cpu);
1329 spin_lock_init(&base->lock);
1330 for (j = 0; j < TVN_SIZE; j++) {
1331 INIT_LIST_HEAD(base->tv5.vec + j);
1332 INIT_LIST_HEAD(base->tv4.vec + j);
1333 INIT_LIST_HEAD(base->tv3.vec + j);
1334 INIT_LIST_HEAD(base->tv2.vec + j);
1335 }
1336 for (j = 0; j < TVR_SIZE; j++)
1337 INIT_LIST_HEAD(base->tv1.vec + j);
1338
1339 base->timer_jiffies = jiffies;
1340}
1341
1342#ifdef CONFIG_HOTPLUG_CPU
1343static int migrate_timer_list(tvec_base_t *new_base, struct list_head *head)
1344{
1345 struct timer_list *timer;
1346
1347 while (!list_empty(head)) {
1348 timer = list_entry(head->next, struct timer_list, entry);
1349
1350
1351 if (!spin_trylock(&timer->lock))
1352 return 0;
1353 list_del(&timer->entry);
1354 internal_add_timer(new_base, timer);
1355 timer->base = new_base;
1356 spin_unlock(&timer->lock);
1357 }
1358 return 1;
1359}
1360
1361static void __devinit migrate_timers(int cpu)
1362{
1363 tvec_base_t *old_base;
1364 tvec_base_t *new_base;
1365 int i;
1366
1367 BUG_ON(cpu_online(cpu));
1368 old_base = &per_cpu(tvec_bases, cpu);
1369 new_base = &get_cpu_var(tvec_bases);
1370
1371 local_irq_disable();
1372again:
1373
1374 if (old_base < new_base) {
1375 spin_lock(&new_base->lock);
1376 spin_lock(&old_base->lock);
1377 } else {
1378 spin_lock(&old_base->lock);
1379 spin_lock(&new_base->lock);
1380 }
1381
1382 if (old_base->running_timer)
1383 BUG();
1384 for (i = 0; i < TVR_SIZE; i++)
1385 if (!migrate_timer_list(new_base, old_base->tv1.vec + i))
1386 goto unlock_again;
1387 for (i = 0; i < TVN_SIZE; i++)
1388 if (!migrate_timer_list(new_base, old_base->tv2.vec + i)
1389 || !migrate_timer_list(new_base, old_base->tv3.vec + i)
1390 || !migrate_timer_list(new_base, old_base->tv4.vec + i)
1391 || !migrate_timer_list(new_base, old_base->tv5.vec + i))
1392 goto unlock_again;
1393 spin_unlock(&old_base->lock);
1394 spin_unlock(&new_base->lock);
1395 local_irq_enable();
1396 put_cpu_var(tvec_bases);
1397 return;
1398
1399unlock_again:
1400
1401 spin_unlock(&old_base->lock);
1402 spin_unlock(&new_base->lock);
1403 cpu_relax();
1404 goto again;
1405}
1406#endif
1407
1408static int __devinit timer_cpu_notify(struct notifier_block *self,
1409 unsigned long action, void *hcpu)
1410{
1411 long cpu = (long)hcpu;
1412 switch(action) {
1413 case CPU_UP_PREPARE:
1414 init_timers_cpu(cpu);
1415 break;
1416#ifdef CONFIG_HOTPLUG_CPU
1417 case CPU_DEAD:
1418 migrate_timers(cpu);
1419 break;
1420#endif
1421 default:
1422 break;
1423 }
1424 return NOTIFY_OK;
1425}
1426
1427static struct notifier_block __devinitdata timers_nb = {
1428 .notifier_call = timer_cpu_notify,
1429};
1430
1431
1432void __init init_timers(void)
1433{
1434 timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
1435 (void *)(long)smp_processor_id());
1436 register_cpu_notifier(&timers_nb);
1437 open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
1438}
1439
1440#ifdef CONFIG_TIME_INTERPOLATION
1441
1442struct time_interpolator *time_interpolator;
1443static struct time_interpolator *time_interpolator_list;
1444static spinlock_t time_interpolator_lock = SPIN_LOCK_UNLOCKED;
1445
1446static inline unsigned long time_interpolator_get_cycles(unsigned int src)
1447{
1448 unsigned long (*x)(void);
1449
1450 switch (src)
1451 {
1452 case TIME_SOURCE_FUNCTION:
1453 x = time_interpolator->addr;
1454 return x();
1455
1456 case TIME_SOURCE_MMIO64 :
1457 return readq(time_interpolator->addr);
1458
1459 case TIME_SOURCE_MMIO32 :
1460 return readl(time_interpolator->addr);
1461 default: return get_cycles();
1462 }
1463}
1464
1465static inline unsigned long time_interpolator_get_counter(void)
1466{
1467 unsigned int src = time_interpolator->source;
1468
1469 if (time_interpolator->jitter)
1470 {
1471 unsigned long lcycle;
1472 unsigned long now;
1473
1474 do {
1475 lcycle = time_interpolator->last_cycle;
1476 now = time_interpolator_get_cycles(src);
1477 if (lcycle && time_after(lcycle, now)) return lcycle;
1478
1479
1480
1481 } while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle));
1482 return now;
1483 }
1484 else
1485 return time_interpolator_get_cycles(src);
1486}
1487
1488void time_interpolator_reset(void)
1489{
1490 time_interpolator->offset = 0;
1491 time_interpolator->last_counter = time_interpolator_get_counter();
1492}
1493
1494unsigned long time_interpolator_resolution(void)
1495{
1496 if (time_interpolator->frequency < NSEC_PER_SEC)
1497 return NSEC_PER_SEC / time_interpolator->frequency;
1498 else
1499 return 1;
1500}
1501
1502#define GET_TI_NSECS(count,i) ((((count) - i->last_counter) * i->nsec_per_cyc) >> i->shift)
1503
1504unsigned long time_interpolator_get_offset(void)
1505{
1506 return time_interpolator->offset +
1507 GET_TI_NSECS(time_interpolator_get_counter(), time_interpolator);
1508}
1509
1510static void time_interpolator_update(long delta_nsec)
1511{
1512 unsigned long counter = time_interpolator_get_counter();
1513 unsigned long offset = time_interpolator->offset + GET_TI_NSECS(counter, time_interpolator);
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523 if (delta_nsec < 0 || (unsigned long) delta_nsec < offset)
1524 time_interpolator->offset = offset - delta_nsec;
1525 else {
1526 time_interpolator->skips++;
1527 time_interpolator->ns_skipped += delta_nsec - offset;
1528 time_interpolator->offset = 0;
1529 }
1530 time_interpolator->last_counter = counter;
1531
1532
1533
1534
1535
1536 if (jiffies % INTERPOLATOR_ADJUST == 0)
1537 {
1538 if (time_interpolator->skips == 0 && time_interpolator->offset > TICK_NSEC)
1539 time_interpolator->nsec_per_cyc--;
1540 if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0)
1541 time_interpolator->nsec_per_cyc++;
1542 time_interpolator->skips = 0;
1543 time_interpolator->ns_skipped = 0;
1544 }
1545}
1546
1547static inline int
1548is_better_time_interpolator(struct time_interpolator *new)
1549{
1550 if (!time_interpolator)
1551 return 1;
1552 return new->frequency > 2*time_interpolator->frequency ||
1553 (unsigned long)new->drift < (unsigned long)time_interpolator->drift;
1554}
1555
1556void
1557register_time_interpolator(struct time_interpolator *ti)
1558{
1559 unsigned long flags;
1560
1561 ti->nsec_per_cyc = (NSEC_PER_SEC << ti->shift) / ti->frequency;
1562 spin_lock(&time_interpolator_lock);
1563 write_seqlock_irqsave(&xtime_lock, flags);
1564 if (is_better_time_interpolator(ti)) {
1565 time_interpolator = ti;
1566 time_interpolator_reset();
1567 }
1568 write_sequnlock_irqrestore(&xtime_lock, flags);
1569
1570 ti->next = time_interpolator_list;
1571 time_interpolator_list = ti;
1572 spin_unlock(&time_interpolator_lock);
1573}
1574
1575void
1576unregister_time_interpolator(struct time_interpolator *ti)
1577{
1578 struct time_interpolator *curr, **prev;
1579 unsigned long flags;
1580
1581 spin_lock(&time_interpolator_lock);
1582 prev = &time_interpolator_list;
1583 for (curr = *prev; curr; curr = curr->next) {
1584 if (curr == ti) {
1585 *prev = curr->next;
1586 break;
1587 }
1588 prev = &curr->next;
1589 }
1590
1591 write_seqlock_irqsave(&xtime_lock, flags);
1592 if (ti == time_interpolator) {
1593
1594 time_interpolator = NULL;
1595
1596 for (curr = time_interpolator_list; curr; curr = curr->next)
1597 if (is_better_time_interpolator(curr))
1598 time_interpolator = curr;
1599 time_interpolator_reset();
1600 }
1601 write_sequnlock_irqrestore(&xtime_lock, flags);
1602 spin_unlock(&time_interpolator_lock);
1603}
1604#endif
1605
1606
1607
1608
1609
1610void msleep(unsigned int msecs)
1611{
1612 unsigned long timeout = msecs_to_jiffies(msecs);
1613
1614 while (timeout) {
1615 set_current_state(TASK_UNINTERRUPTIBLE);
1616 timeout = schedule_timeout(timeout);
1617 }
1618}
1619
1620EXPORT_SYMBOL(msleep);
1621
1622
1623
1624
1625
1626unsigned long msleep_interruptible(unsigned int msecs)
1627{
1628 unsigned long timeout = msecs_to_jiffies(msecs);
1629
1630 while (timeout && !signal_pending(current)) {
1631 set_current_state(TASK_INTERRUPTIBLE);
1632 timeout = schedule_timeout(timeout);
1633 }
1634 return jiffies_to_msecs(timeout);
1635}
1636
1637EXPORT_SYMBOL(msleep_interruptible);
1638