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