1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/signal.h>
20#include <linux/sched.h>
21#include <linux/timer.h>
22#include <linux/kernel.h>
23#include <linux/kernel_stat.h>
24#include <linux/fdreg.h>
25#include <linux/errno.h>
26#include <linux/time.h>
27#include <linux/ptrace.h>
28#include <linux/delay.h>
29#include <linux/interrupt.h>
30#include <linux/tqueue.h>
31#include <linux/resource.h>
32#include <linux/mm.h>
33#include <linux/smp.h>
34#include <linux/smp_lock.h>
35#include <linux/init.h>
36
37#include <asm/system.h>
38#include <asm/io.h>
39#include <asm/uaccess.h>
40#include <asm/pgtable.h>
41#include <asm/mmu_context.h>
42#include <asm/spinlock.h>
43
44#include <linux/timex.h>
45
46
47
48
49
50int securelevel = 0;
51
52long tick = (1000000 + HZ/2) / HZ;
53volatile struct timeval xtime __attribute__ ((aligned (8)));
54int tickadj = 500/HZ;
55
56DECLARE_TASK_QUEUE(tq_timer);
57DECLARE_TASK_QUEUE(tq_immediate);
58DECLARE_TASK_QUEUE(tq_scheduler);
59
60
61
62
63
64int time_state = TIME_ERROR;
65int time_status = STA_UNSYNC;
66long time_offset = 0;
67long time_constant = 2;
68long time_tolerance = MAXFREQ;
69long time_precision = 1;
70long time_maxerror = MAXPHASE;
71long time_esterror = MAXPHASE;
72long time_phase = 0;
73long time_freq = ((1000000 + HZ/2) % HZ - HZ/2) << SHIFT_USEC;
74long time_adj = 0;
75long time_reftime = 0;
76
77long time_adjust = 0;
78long time_adjust_step = 0;
79
80int need_resched = 0;
81unsigned long event = 0;
82
83extern int _setitimer(int, struct itimerval *, struct itimerval *);
84unsigned int * prof_buffer = NULL;
85unsigned long prof_len = 0;
86unsigned long prof_shift = 0;
87
88#define _S(nr) (1<<((nr)-1))
89
90extern void mem_use(void);
91
92unsigned long volatile jiffies=0;
93
94
95
96
97
98
99struct task_struct *last_task_used_math = NULL;
100
101struct task_struct * task[NR_TASKS] = {&init_task, };
102
103struct kernel_stat kstat = { 0 };
104
105static inline void add_to_runqueue(struct task_struct * p)
106{
107 if (p->counter > current->counter + 3)
108 need_resched = 1;
109 nr_running++;
110 (p->prev_run = init_task.prev_run)->next_run = p;
111 p->next_run = &init_task;
112 init_task.prev_run = p;
113}
114
115static inline void del_from_runqueue(struct task_struct * p)
116{
117 struct task_struct *next = p->next_run;
118 struct task_struct *prev = p->prev_run;
119
120 nr_running--;
121 next->prev_run = prev;
122 prev->next_run = next;
123 p->next_run = NULL;
124 p->prev_run = NULL;
125}
126
127static inline void move_last_runqueue(struct task_struct * p)
128{
129 struct task_struct *next = p->next_run;
130 struct task_struct *prev = p->prev_run;
131
132
133 next->prev_run = prev;
134 prev->next_run = next;
135
136 p->next_run = &init_task;
137 prev = init_task.prev_run;
138 init_task.prev_run = p;
139 p->prev_run = prev;
140 prev->next_run = p;
141}
142
143#ifdef __SMP__
144
145
146
147
148
149
150
151
152
153
154
155rwlock_t tasklist_lock = RW_LOCK_UNLOCKED;
156spinlock_t scheduler_lock = SPIN_LOCK_UNLOCKED;
157static spinlock_t runqueue_lock = SPIN_LOCK_UNLOCKED;
158#endif
159
160
161
162
163
164
165
166
167
168inline void wake_up_process(struct task_struct * p)
169{
170 unsigned long flags;
171
172 spin_lock_irqsave(&runqueue_lock, flags);
173 p->state = TASK_RUNNING;
174 if (!p->next_run)
175 add_to_runqueue(p);
176 spin_unlock_irqrestore(&runqueue_lock, flags);
177}
178
179static void process_timeout(unsigned long __data)
180{
181 struct task_struct * p = (struct task_struct *) __data;
182
183 p->timeout = 0;
184 wake_up_process(p);
185}
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200static inline int goodness(struct task_struct * p, struct task_struct * prev, int this_cpu)
201{
202 int weight;
203
204
205
206
207
208
209 if (p->policy != SCHED_OTHER)
210 return 1000 + p->rt_priority;
211
212
213
214
215
216
217
218
219 weight = p->counter;
220 if (weight) {
221
222#ifdef __SMP__
223
224
225 if (p->processor == this_cpu)
226 weight += PROC_CHANGE_PENALTY;
227#endif
228
229
230 if (p == prev)
231 weight += 1;
232 }
233
234 return weight;
235}
236
237
238
239
240#define TVN_BITS 6
241#define TVR_BITS 8
242#define TVN_SIZE (1 << TVN_BITS)
243#define TVR_SIZE (1 << TVR_BITS)
244#define TVN_MASK (TVN_SIZE - 1)
245#define TVR_MASK (TVR_SIZE - 1)
246
247struct timer_vec {
248 int index;
249 struct timer_list *vec[TVN_SIZE];
250};
251
252struct timer_vec_root {
253 int index;
254 struct timer_list *vec[TVR_SIZE];
255};
256
257static struct timer_vec tv5 = { 0 };
258static struct timer_vec tv4 = { 0 };
259static struct timer_vec tv3 = { 0 };
260static struct timer_vec tv2 = { 0 };
261static struct timer_vec_root tv1 = { 0 };
262
263static struct timer_vec * const tvecs[] = {
264 (struct timer_vec *)&tv1, &tv2, &tv3, &tv4, &tv5
265};
266
267#define NOOF_TVECS (sizeof(tvecs) / sizeof(tvecs[0]))
268
269static unsigned long timer_jiffies = 0;
270
271static inline void insert_timer(struct timer_list *timer,
272 struct timer_list **vec, int idx)
273{
274 if ((timer->next = vec[idx]))
275 vec[idx]->prev = timer;
276 vec[idx] = timer;
277 timer->prev = (struct timer_list *)&vec[idx];
278}
279
280static inline void internal_add_timer(struct timer_list *timer)
281{
282
283
284
285 unsigned long expires = timer->expires;
286 unsigned long idx = expires - timer_jiffies;
287
288 if (idx < TVR_SIZE) {
289 int i = expires & TVR_MASK;
290 insert_timer(timer, tv1.vec, i);
291 } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
292 int i = (expires >> TVR_BITS) & TVN_MASK;
293 insert_timer(timer, tv2.vec, i);
294 } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
295 int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
296 insert_timer(timer, tv3.vec, i);
297 } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
298 int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
299 insert_timer(timer, tv4.vec, i);
300 } else if (expires < timer_jiffies) {
301
302
303
304 insert_timer(timer, tv1.vec, tv1.index);
305 } else if (idx < 0xffffffffUL) {
306 int i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
307 insert_timer(timer, tv5.vec, i);
308 } else {
309
310 timer->next = timer->prev = timer;
311 }
312}
313
314static spinlock_t timerlist_lock = SPIN_LOCK_UNLOCKED;
315
316void add_timer(struct timer_list *timer)
317{
318 unsigned long flags;
319
320 spin_lock_irqsave(&timerlist_lock, flags);
321 internal_add_timer(timer);
322 spin_unlock_irqrestore(&timerlist_lock, flags);
323}
324
325static inline int detach_timer(struct timer_list *timer)
326{
327 int ret = 0;
328 struct timer_list *next, *prev;
329 next = timer->next;
330 prev = timer->prev;
331 if (next) {
332 next->prev = prev;
333 }
334 if (prev) {
335 ret = 1;
336 prev->next = next;
337 }
338 return ret;
339}
340
341
342int del_timer(struct timer_list * timer)
343{
344 int ret;
345 unsigned long flags;
346
347 spin_lock_irqsave(&timerlist_lock, flags);
348 ret = detach_timer(timer);
349 timer->next = timer->prev = 0;
350 spin_unlock_irqrestore(&timerlist_lock, flags);
351 return ret;
352}
353
354#ifdef __SMP__
355
356#define idle_task (task[cpu_number_map[this_cpu]])
357#define can_schedule(p) (!(p)->has_cpu)
358
359#else
360
361#define idle_task (&init_task)
362#define can_schedule(p) (1)
363
364#endif
365
366
367
368
369
370
371
372
373
374
375
376asmlinkage void schedule(void)
377{
378 int lock_depth;
379 struct task_struct * prev, * next;
380 unsigned long timeout;
381 int this_cpu;
382
383 need_resched = 0;
384 prev = current;
385 this_cpu = smp_processor_id();
386 if (local_irq_count[this_cpu])
387 goto scheduling_in_interrupt;
388 release_kernel_lock(prev, this_cpu, lock_depth);
389 if (bh_active & bh_mask)
390 do_bottom_half();
391
392 spin_lock(&scheduler_lock);
393 spin_lock_irq(&runqueue_lock);
394
395
396 if (!prev->counter && prev->policy == SCHED_RR) {
397 prev->counter = prev->priority;
398 move_last_runqueue(prev);
399 }
400 timeout = 0;
401 switch (prev->state) {
402 case TASK_INTERRUPTIBLE:
403 if (prev->signal & ~prev->blocked)
404 goto makerunnable;
405 timeout = prev->timeout;
406 if (timeout && (timeout <= jiffies)) {
407 prev->timeout = 0;
408 timeout = 0;
409 makerunnable:
410 prev->state = TASK_RUNNING;
411 break;
412 }
413 default:
414 del_from_runqueue(prev);
415 case TASK_RUNNING:
416 }
417 {
418 struct task_struct * p = init_task.next_run;
419
420
421
422
423
424
425
426
427
428
429 spin_unlock_irq(&runqueue_lock);
430#ifdef __SMP__
431 prev->has_cpu = 0;
432#endif
433
434
435
436
437
438
439
440 {
441 int c = -1000;
442 next = idle_task;
443 while (p != &init_task) {
444 if (can_schedule(p)) {
445 int weight = goodness(p, prev, this_cpu);
446 if (weight > c)
447 c = weight, next = p;
448 }
449 p = p->next_run;
450 }
451
452
453 if (!c) {
454 struct task_struct *p;
455 read_lock(&tasklist_lock);
456 for_each_task(p)
457 p->counter = (p->counter >> 1) + p->priority;
458 read_unlock(&tasklist_lock);
459 }
460 }
461 }
462
463#ifdef __SMP__
464 next->has_cpu = 1;
465 next->processor = this_cpu;
466#endif
467
468 if (prev != next) {
469 struct timer_list timer;
470
471 kstat.context_swtch++;
472 if (timeout) {
473 init_timer(&timer);
474 timer.expires = timeout;
475 timer.data = (unsigned long) prev;
476 timer.function = process_timeout;
477 add_timer(&timer);
478 }
479 get_mmu_context(next);
480 switch_to(prev,next);
481
482 if (timeout)
483 del_timer(&timer);
484 }
485 spin_unlock(&scheduler_lock);
486
487 reacquire_kernel_lock(prev, smp_processor_id(), lock_depth);
488 return;
489
490scheduling_in_interrupt:
491 printk("Scheduling in interrupt\n");
492 *(int *)0 = 0;
493}
494
495
496rwlock_t waitqueue_lock = RW_LOCK_UNLOCKED;
497
498
499
500
501
502
503
504
505
506void wake_up(struct wait_queue **q)
507{
508 struct wait_queue *next;
509
510 read_lock(&waitqueue_lock);
511 if (q && (next = *q)) {
512 struct wait_queue *head;
513
514 head = WAIT_QUEUE_HEAD(q);
515 while (next != head) {
516 struct task_struct *p = next->task;
517 next = next->next;
518 if ((p->state == TASK_UNINTERRUPTIBLE) ||
519 (p->state == TASK_INTERRUPTIBLE))
520 wake_up_process(p);
521 }
522 }
523 read_unlock(&waitqueue_lock);
524}
525
526void wake_up_interruptible(struct wait_queue **q)
527{
528 struct wait_queue *next;
529
530 read_lock(&waitqueue_lock);
531 if (q && (next = *q)) {
532 struct wait_queue *head;
533
534 head = WAIT_QUEUE_HEAD(q);
535 while (next != head) {
536 struct task_struct *p = next->task;
537 next = next->next;
538 if (p->state == TASK_INTERRUPTIBLE)
539 wake_up_process(p);
540 }
541 }
542 read_unlock(&waitqueue_lock);
543}
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575void __up(struct semaphore *sem)
576{
577 wake_one_more(sem);
578 wake_up(&sem->wait);
579}
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598static inline int __do_down(struct semaphore * sem, int task_state)
599{
600 struct task_struct *tsk = current;
601 struct wait_queue wait = { tsk, NULL };
602 int ret = 0;
603
604 tsk->state = task_state;
605 add_wait_queue(&sem->wait, &wait);
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622 for (;;) {
623 if (waking_non_zero(sem))
624 break;
625
626 if ( task_state == TASK_INTERRUPTIBLE
627 && (tsk->signal & ~tsk->blocked)
628 ) {
629 ret = -EINTR;
630 atomic_inc(&sem->count);
631 break;
632 }
633
634 schedule();
635 tsk->state = task_state;
636 }
637
638 tsk->state = TASK_RUNNING;
639 remove_wait_queue(&sem->wait, &wait);
640 return ret;
641}
642
643void __down(struct semaphore * sem)
644{
645 __do_down(sem,TASK_UNINTERRUPTIBLE);
646}
647
648int __down_interruptible(struct semaphore * sem)
649{
650 return __do_down(sem,TASK_INTERRUPTIBLE);
651}
652
653
654static inline void __sleep_on(struct wait_queue **p, int state)
655{
656 unsigned long flags;
657 struct wait_queue wait = { current, NULL };
658
659 if (!p)
660 return;
661 current->state = state;
662 write_lock_irqsave(&waitqueue_lock, flags);
663 __add_wait_queue(p, &wait);
664 write_unlock(&waitqueue_lock);
665 schedule();
666 write_lock_irq(&waitqueue_lock);
667 __remove_wait_queue(p, &wait);
668 write_unlock_irqrestore(&waitqueue_lock, flags);
669}
670
671void interruptible_sleep_on(struct wait_queue **p)
672{
673 __sleep_on(p,TASK_INTERRUPTIBLE);
674}
675
676void sleep_on(struct wait_queue **p)
677{
678 __sleep_on(p,TASK_UNINTERRUPTIBLE);
679}
680
681static inline void cascade_timers(struct timer_vec *tv)
682{
683
684 struct timer_list *timer;
685 timer = tv->vec[tv->index];
686
687
688
689
690 while (timer) {
691 struct timer_list *tmp = timer;
692 timer = timer->next;
693 internal_add_timer(tmp);
694 }
695 tv->vec[tv->index] = NULL;
696 tv->index = (tv->index + 1) & TVN_MASK;
697}
698
699static inline void run_timer_list(void)
700{
701 spin_lock_irq(&timerlist_lock);
702 while ((long)(jiffies - timer_jiffies) >= 0) {
703 struct timer_list *timer;
704 if (!tv1.index) {
705 int n = 1;
706 do {
707 cascade_timers(tvecs[n]);
708 } while (tvecs[n]->index == 1 && ++n < NOOF_TVECS);
709 }
710 while ((timer = tv1.vec[tv1.index])) {
711 void (*fn)(unsigned long) = timer->function;
712 unsigned long data = timer->data;
713 detach_timer(timer);
714 timer->next = timer->prev = NULL;
715 spin_unlock_irq(&timerlist_lock);
716 fn(data);
717 spin_lock_irq(&timerlist_lock);
718 }
719 ++timer_jiffies;
720 tv1.index = (tv1.index + 1) & TVR_MASK;
721 }
722 spin_unlock_irq(&timerlist_lock);
723}
724
725
726static inline void run_old_timers(void)
727{
728 struct timer_struct *tp;
729 unsigned long mask;
730
731 for (mask = 1, tp = timer_table+0 ; mask ; tp++,mask += mask) {
732 if (mask > timer_active)
733 break;
734 if (!(mask & timer_active))
735 continue;
736 if (tp->expires > jiffies)
737 continue;
738 timer_active &= ~mask;
739 tp->fn();
740 sti();
741 }
742}
743
744spinlock_t tqueue_lock;
745
746void tqueue_bh(void)
747{
748 run_task_queue(&tq_timer);
749}
750
751void immediate_bh(void)
752{
753 run_task_queue(&tq_immediate);
754}
755
756unsigned long timer_active = 0;
757struct timer_struct timer_table[32];
758
759
760
761
762
763
764
765unsigned long avenrun[3] = { 0,0,0 };
766
767
768
769
770static unsigned long count_active_tasks(void)
771{
772 struct task_struct *p;
773 unsigned long nr = 0;
774
775 read_lock(&tasklist_lock);
776 for_each_task(p) {
777 if (p->pid &&
778 (p->state == TASK_RUNNING ||
779 p->state == TASK_UNINTERRUPTIBLE ||
780 p->state == TASK_SWAPPING))
781 nr += FIXED_1;
782 }
783 read_unlock(&tasklist_lock);
784 return nr;
785}
786
787static inline void calc_load(unsigned long ticks)
788{
789 unsigned long active_tasks;
790 static int count = LOAD_FREQ;
791
792 count -= ticks;
793 if (count < 0) {
794 count += LOAD_FREQ;
795 active_tasks = count_active_tasks();
796 CALC_LOAD(avenrun[0], EXP_1, active_tasks);
797 CALC_LOAD(avenrun[1], EXP_5, active_tasks);
798 CALC_LOAD(avenrun[2], EXP_15, active_tasks);
799 }
800}
801
802
803
804
805
806
807
808
809
810
811static void second_overflow(void)
812{
813 long ltemp;
814
815
816 time_maxerror += time_tolerance >> SHIFT_USEC;
817 if ( time_maxerror > MAXPHASE )
818 time_maxerror = MAXPHASE;
819
820
821
822
823
824
825
826
827
828
829 switch (time_state) {
830
831 case TIME_OK:
832 if (time_status & STA_INS)
833 time_state = TIME_INS;
834 else if (time_status & STA_DEL)
835 time_state = TIME_DEL;
836 break;
837
838 case TIME_INS:
839 if (xtime.tv_sec % 86400 == 0) {
840 xtime.tv_sec--;
841 time_state = TIME_OOP;
842 printk("Clock: inserting leap second 23:59:60 UTC\n");
843 }
844 break;
845
846 case TIME_DEL:
847 if ((xtime.tv_sec + 1) % 86400 == 0) {
848 xtime.tv_sec++;
849 time_state = TIME_WAIT;
850 printk("Clock: deleting leap second 23:59:59 UTC\n");
851 }
852 break;
853
854 case TIME_OOP:
855 time_state = TIME_WAIT;
856 break;
857
858 case TIME_WAIT:
859 if (!(time_status & (STA_INS | STA_DEL)))
860 time_state = TIME_OK;
861 }
862
863
864
865
866
867
868
869
870
871
872 if (time_offset < 0) {
873 ltemp = -time_offset;
874 if (!(time_status & STA_FLL))
875 ltemp >>= SHIFT_KG + time_constant;
876 if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE)
877 ltemp = (MAXPHASE / MINSEC) << SHIFT_UPDATE;
878 time_offset += ltemp;
879 time_adj = -ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
880 } else {
881 ltemp = time_offset;
882 if (!(time_status & STA_FLL))
883 ltemp >>= SHIFT_KG + time_constant;
884 if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE)
885 ltemp = (MAXPHASE / MINSEC) << SHIFT_UPDATE;
886 time_offset -= ltemp;
887 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
888 }
889
890
891
892
893
894
895
896
897 pps_valid++;
898 if (pps_valid == PPS_VALID) {
899 pps_jitter = MAXTIME;
900 pps_stabil = MAXFREQ;
901 time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
902 STA_PPSWANDER | STA_PPSERROR);
903 }
904 ltemp = time_freq + pps_freq;
905 if (ltemp < 0)
906 time_adj -= -ltemp >>
907 (SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE);
908 else
909 time_adj += ltemp >>
910 (SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE);
911
912#if HZ == 100
913
914 if (time_adj < 0)
915 time_adj -= -time_adj >> 2;
916 else
917 time_adj += time_adj >> 2;
918#endif
919}
920
921
922static void update_wall_time_one_tick(void)
923{
924
925
926
927
928 time_phase += time_adj;
929 if (time_phase <= -FINEUSEC) {
930 long ltemp = -time_phase >> SHIFT_SCALE;
931 time_phase += ltemp << SHIFT_SCALE;
932 xtime.tv_usec += tick + time_adjust_step - ltemp;
933 }
934 else if (time_phase >= FINEUSEC) {
935 long ltemp = time_phase >> SHIFT_SCALE;
936 time_phase -= ltemp << SHIFT_SCALE;
937 xtime.tv_usec += tick + time_adjust_step + ltemp;
938 } else
939 xtime.tv_usec += tick + time_adjust_step;
940
941 if (time_adjust) {
942
943
944
945
946
947
948
949
950
951 if (time_adjust > tickadj)
952 time_adjust_step = tickadj;
953 else if (time_adjust < -tickadj)
954 time_adjust_step = -tickadj;
955 else
956 time_adjust_step = time_adjust;
957
958
959 time_adjust -= time_adjust_step;
960 }
961 else
962 time_adjust_step = 0;
963}
964
965
966
967
968
969
970
971
972static void update_wall_time(unsigned long ticks)
973{
974 do {
975 ticks--;
976 update_wall_time_one_tick();
977 } while (ticks);
978
979 if (xtime.tv_usec >= 1000000) {
980 xtime.tv_usec -= 1000000;
981 xtime.tv_sec++;
982 second_overflow();
983 }
984}
985
986static inline void do_process_times(struct task_struct *p,
987 unsigned long user, unsigned long system)
988{
989 long psecs;
990
991 psecs = (p->times.tms_utime += user);
992 psecs += (p->times.tms_stime += system);
993 if (psecs / HZ > p->rlim[RLIMIT_CPU].rlim_cur) {
994
995 if (!(psecs % HZ))
996 send_sig(SIGXCPU, p, 1);
997
998 if (psecs / HZ > p->rlim[RLIMIT_CPU].rlim_max)
999 send_sig(SIGKILL, p, 1);
1000 }
1001}
1002
1003static inline void do_it_virt(struct task_struct * p, unsigned long ticks)
1004{
1005 unsigned long it_virt = p->it_virt_value;
1006
1007 if (it_virt) {
1008 if (it_virt <= ticks) {
1009 it_virt = ticks + p->it_virt_incr;
1010 send_sig(SIGVTALRM, p, 1);
1011 }
1012 p->it_virt_value = it_virt - ticks;
1013 }
1014}
1015
1016static inline void do_it_prof(struct task_struct * p, unsigned long ticks)
1017{
1018 unsigned long it_prof = p->it_prof_value;
1019
1020 if (it_prof) {
1021 if (it_prof <= ticks) {
1022 it_prof = ticks + p->it_prof_incr;
1023 send_sig(SIGPROF, p, 1);
1024 }
1025 p->it_prof_value = it_prof - ticks;
1026 }
1027}
1028
1029void update_one_process(struct task_struct *p,
1030 unsigned long ticks, unsigned long user, unsigned long system)
1031{
1032 do_process_times(p, user, system);
1033 do_it_virt(p, user);
1034 do_it_prof(p, ticks);
1035}
1036
1037static void update_process_times(unsigned long ticks, unsigned long system)
1038{
1039
1040
1041
1042#ifndef __SMP__
1043 struct task_struct * p = current;
1044 unsigned long user = ticks - system;
1045 if (p->pid) {
1046 p->counter -= ticks;
1047 if (p->counter < 0) {
1048 p->counter = 0;
1049 need_resched = 1;
1050 }
1051 if (p->priority < DEF_PRIORITY)
1052 kstat.cpu_nice += user;
1053 else
1054 kstat.cpu_user += user;
1055 kstat.cpu_system += system;
1056 }
1057 update_one_process(p, ticks, user, system);
1058#endif
1059}
1060
1061volatile unsigned long lost_ticks = 0;
1062static unsigned long lost_ticks_system = 0;
1063
1064static inline void update_times(void)
1065{
1066 unsigned long ticks;
1067 unsigned long flags;
1068
1069 save_flags(flags);
1070 cli();
1071
1072 ticks = lost_ticks;
1073 lost_ticks = 0;
1074
1075 if (ticks) {
1076 unsigned long system;
1077 system = xchg(&lost_ticks_system, 0);
1078
1079 calc_load(ticks);
1080 update_wall_time(ticks);
1081 restore_flags(flags);
1082
1083 update_process_times(ticks, system);
1084
1085 } else
1086 restore_flags(flags);
1087}
1088
1089static void timer_bh(void)
1090{
1091 update_times();
1092 run_old_timers();
1093 run_timer_list();
1094}
1095
1096void do_timer(struct pt_regs * regs)
1097{
1098 (*(unsigned long *)&jiffies)++;
1099 lost_ticks++;
1100 mark_bh(TIMER_BH);
1101 if (!user_mode(regs))
1102 lost_ticks_system++;
1103 if (tq_timer)
1104 mark_bh(TQUEUE_BH);
1105}
1106
1107#ifndef __alpha__
1108
1109
1110
1111
1112
1113asmlinkage unsigned int sys_alarm(unsigned int seconds)
1114{
1115 struct itimerval it_new, it_old;
1116 unsigned int oldalarm;
1117
1118 it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
1119 it_new.it_value.tv_sec = seconds;
1120 it_new.it_value.tv_usec = 0;
1121 _setitimer(ITIMER_REAL, &it_new, &it_old);
1122 oldalarm = it_old.it_value.tv_sec;
1123
1124
1125 if (it_old.it_value.tv_usec)
1126 oldalarm++;
1127 return oldalarm;
1128}
1129
1130
1131
1132
1133
1134
1135asmlinkage int sys_getpid(void)
1136{
1137
1138 return current->pid;
1139}
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164asmlinkage int sys_getppid(void)
1165{
1166 int pid;
1167 struct task_struct * me = current;
1168 struct task_struct * parent;
1169
1170 parent = me->p_opptr;
1171 for (;;) {
1172 pid = parent->pid;
1173#if __SMP__
1174{
1175 struct task_struct *old = parent;
1176 mb();
1177 parent = me->p_opptr;
1178 if (old != parent)
1179 continue;
1180}
1181#endif
1182 break;
1183 }
1184 return pid;
1185}
1186
1187asmlinkage int sys_getuid(void)
1188{
1189
1190 return current->uid;
1191}
1192
1193asmlinkage int sys_geteuid(void)
1194{
1195
1196 return current->euid;
1197}
1198
1199asmlinkage int sys_getgid(void)
1200{
1201
1202 return current->gid;
1203}
1204
1205asmlinkage int sys_getegid(void)
1206{
1207
1208 return current->egid;
1209}
1210
1211
1212
1213
1214
1215
1216
1217asmlinkage int sys_nice(int increment)
1218{
1219 unsigned long newprio;
1220 int increase = 0;
1221
1222
1223
1224
1225
1226
1227
1228 newprio = increment;
1229 if (increment < 0) {
1230 if (!suser())
1231 return -EPERM;
1232 newprio = -increment;
1233 increase = 1;
1234 }
1235
1236 if (newprio > 40)
1237 newprio = 40;
1238
1239
1240
1241
1242
1243
1244
1245 newprio = (newprio * DEF_PRIORITY + 10) / 20;
1246 increment = newprio;
1247 if (increase)
1248 increment = -increment;
1249
1250
1251
1252
1253
1254
1255
1256
1257 newprio = current->priority - increment;
1258 if ((signed) newprio < 1)
1259 newprio = 1;
1260 if (newprio > DEF_PRIORITY*2)
1261 newprio = DEF_PRIORITY*2;
1262 current->priority = newprio;
1263 return 0;
1264}
1265
1266#endif
1267
1268static inline struct task_struct *find_process_by_pid(pid_t pid)
1269{
1270 if (pid)
1271 return find_task_by_pid(pid);
1272 else
1273 return current;
1274}
1275
1276static int setscheduler(pid_t pid, int policy,
1277 struct sched_param *param)
1278{
1279 struct sched_param lp;
1280 struct task_struct *p;
1281
1282 if (!param || pid < 0)
1283 return -EINVAL;
1284
1285 if (copy_from_user(&lp, param, sizeof(struct sched_param)))
1286 return -EFAULT;
1287
1288 p = find_process_by_pid(pid);
1289 if (!p)
1290 return -ESRCH;
1291
1292 if (policy < 0)
1293 policy = p->policy;
1294 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
1295 policy != SCHED_OTHER)
1296 return -EINVAL;
1297
1298
1299
1300
1301
1302 if (lp.sched_priority < 0 || lp.sched_priority > 99)
1303 return -EINVAL;
1304 if ((policy == SCHED_OTHER) != (lp.sched_priority == 0))
1305 return -EINVAL;
1306
1307 if ((policy == SCHED_FIFO || policy == SCHED_RR) && !suser())
1308 return -EPERM;
1309 if ((current->euid != p->euid) && (current->euid != p->uid) &&
1310 !suser())
1311 return -EPERM;
1312
1313 p->policy = policy;
1314 p->rt_priority = lp.sched_priority;
1315 spin_lock(&scheduler_lock);
1316 spin_lock_irq(&runqueue_lock);
1317 if (p->next_run)
1318 move_last_runqueue(p);
1319 spin_unlock_irq(&runqueue_lock);
1320 spin_unlock(&scheduler_lock);
1321 need_resched = 1;
1322 return 0;
1323}
1324
1325asmlinkage int sys_sched_setscheduler(pid_t pid, int policy,
1326 struct sched_param *param)
1327{
1328 return setscheduler(pid, policy, param);
1329}
1330
1331asmlinkage int sys_sched_setparam(pid_t pid, struct sched_param *param)
1332{
1333 return setscheduler(pid, -1, param);
1334}
1335
1336asmlinkage int sys_sched_getscheduler(pid_t pid)
1337{
1338 struct task_struct *p;
1339
1340 if (pid < 0)
1341 return -EINVAL;
1342
1343 p = find_process_by_pid(pid);
1344 if (!p)
1345 return -ESRCH;
1346
1347 return p->policy;
1348}
1349
1350asmlinkage int sys_sched_getparam(pid_t pid, struct sched_param *param)
1351{
1352 struct task_struct *p;
1353 struct sched_param lp;
1354
1355 if (!param || pid < 0)
1356 return -EINVAL;
1357
1358 p = find_process_by_pid(pid);
1359 if (!p)
1360 return -ESRCH;
1361
1362 lp.sched_priority = p->rt_priority;
1363 return copy_to_user(param, &lp, sizeof(struct sched_param)) ? -EFAULT : 0;
1364}
1365
1366asmlinkage int sys_sched_yield(void)
1367{
1368
1369
1370
1371
1372 current->counter = 0;
1373 spin_lock(&scheduler_lock);
1374 spin_lock_irq(&runqueue_lock);
1375 move_last_runqueue(current);
1376 spin_unlock_irq(&runqueue_lock);
1377 spin_unlock(&scheduler_lock);
1378 need_resched = 1;
1379 return 0;
1380}
1381
1382asmlinkage int sys_sched_get_priority_max(int policy)
1383{
1384 int ret = -EINVAL;
1385
1386 switch (policy) {
1387 case SCHED_FIFO:
1388 case SCHED_RR:
1389 ret = 99;
1390 break;
1391 case SCHED_OTHER:
1392 ret = 0;
1393 break;
1394 }
1395 return ret;
1396}
1397
1398asmlinkage int sys_sched_get_priority_min(int policy)
1399{
1400 int ret = -EINVAL;
1401
1402 switch (policy) {
1403 case SCHED_FIFO:
1404 case SCHED_RR:
1405 ret = 1;
1406 break;
1407 case SCHED_OTHER:
1408 ret = 0;
1409 }
1410 return ret;
1411}
1412
1413asmlinkage int sys_sched_rr_get_interval(pid_t pid, struct timespec *interval)
1414{
1415 struct timespec t;
1416
1417 t.tv_sec = 0;
1418 t.tv_nsec = 150000;
1419 if (copy_to_user(interval, &t, sizeof(struct timespec)))
1420 return -EFAULT;
1421 return 0;
1422}
1423
1424
1425
1426
1427
1428static unsigned long timespectojiffies(struct timespec *value)
1429{
1430 unsigned long sec = (unsigned) value->tv_sec;
1431 long nsec = value->tv_nsec;
1432
1433 if (sec > (LONG_MAX / HZ))
1434 return LONG_MAX;
1435 nsec += 1000000000L / HZ - 1;
1436 nsec /= 1000000000L / HZ;
1437 return HZ * sec + nsec;
1438}
1439
1440static void jiffiestotimespec(unsigned long jiffies, struct timespec *value)
1441{
1442 value->tv_nsec = (jiffies % HZ) * (1000000000L / HZ);
1443 value->tv_sec = jiffies / HZ;
1444}
1445
1446asmlinkage int sys_nanosleep(struct timespec *rqtp, struct timespec *rmtp)
1447{
1448 struct timespec t;
1449 unsigned long expire;
1450
1451 if(copy_from_user(&t, rqtp, sizeof(struct timespec)))
1452 return -EFAULT;
1453
1454 if (t.tv_nsec >= 1000000000L || t.tv_nsec < 0 || t.tv_sec < 0)
1455 return -EINVAL;
1456
1457
1458 if (t.tv_sec == 0 && t.tv_nsec <= 2000000L &&
1459 current->policy != SCHED_OTHER)
1460 {
1461
1462
1463
1464
1465
1466
1467 udelay((t.tv_nsec + 999) / 1000);
1468 return 0;
1469 }
1470
1471 expire = timespectojiffies(&t) + (t.tv_sec || t.tv_nsec) + jiffies;
1472
1473 current->timeout = expire;
1474 current->state = TASK_INTERRUPTIBLE;
1475 schedule();
1476
1477 if (expire > jiffies) {
1478 if (rmtp) {
1479 jiffiestotimespec(expire - jiffies -
1480 (expire > jiffies + 1), &t);
1481 if (copy_to_user(rmtp, &t, sizeof(struct timespec)))
1482 return -EFAULT;
1483 }
1484 return -EINTR;
1485 }
1486 return 0;
1487}
1488
1489static void show_task(int nr,struct task_struct * p)
1490{
1491 unsigned long free = 0;
1492 static const char * stat_nam[] = { "R", "S", "D", "Z", "T", "W" };
1493
1494 printk("%-8s %3d ", p->comm, (p == current) ? -nr : nr);
1495 if (((unsigned) p->state) < sizeof(stat_nam)/sizeof(char *))
1496 printk(stat_nam[p->state]);
1497 else
1498 printk(" ");
1499#if ((~0UL) == 0xffffffff)
1500 if (p == current)
1501 printk(" current ");
1502 else
1503 printk(" %08lX ", thread_saved_pc(&p->tss));
1504#else
1505 if (p == current)
1506 printk(" current task ");
1507 else
1508 printk(" %016lx ", thread_saved_pc(&p->tss));
1509#endif
1510#if 0
1511 for (free = 1; free < PAGE_SIZE/sizeof(long) ; free++) {
1512 if (((unsigned long *)p->kernel_stack_page)[free])
1513 break;
1514 }
1515#endif
1516 printk("%5lu %5d %6d ", free*sizeof(long), p->pid, p->p_pptr->pid);
1517 if (p->p_cptr)
1518 printk("%5d ", p->p_cptr->pid);
1519 else
1520 printk(" ");
1521 if (p->p_ysptr)
1522 printk("%7d", p->p_ysptr->pid);
1523 else
1524 printk(" ");
1525 if (p->p_osptr)
1526 printk(" %5d\n", p->p_osptr->pid);
1527 else
1528 printk("\n");
1529}
1530
1531void show_state(void)
1532{
1533 struct task_struct *p;
1534
1535#if ((~0UL) == 0xffffffff)
1536 printk("\n"
1537 " free sibling\n");
1538 printk(" task PC stack pid father child younger older\n");
1539#else
1540 printk("\n"
1541 " free sibling\n");
1542 printk(" task PC stack pid father child younger older\n");
1543#endif
1544 read_lock(&tasklist_lock);
1545 for_each_task(p)
1546 show_task((p->tarray_ptr - &task[0]),p);
1547 read_unlock(&tasklist_lock);
1548}
1549
1550__initfunc(void sched_init(void))
1551{
1552
1553
1554
1555
1556 int cpu=hard_smp_processor_id();
1557 int nr = NR_TASKS;
1558
1559 init_task.processor=cpu;
1560
1561
1562 while(--nr > 0)
1563 add_free_taskslot(&task[nr]);
1564
1565 for(nr = 0; nr < PIDHASH_SZ; nr++)
1566 pidhash[nr] = NULL;
1567
1568 init_bh(TIMER_BH, timer_bh);
1569 init_bh(TQUEUE_BH, tqueue_bh);
1570 init_bh(IMMEDIATE_BH, immediate_bh);
1571}
1572