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 (signal_pending(prev))
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 && signal_pending(tsk)) {
627 ret = -EINTR;
628 atomic_inc(&sem->count);
629 break;
630 }
631
632 schedule();
633 tsk->state = task_state;
634 }
635
636 tsk->state = TASK_RUNNING;
637 remove_wait_queue(&sem->wait, &wait);
638 return ret;
639}
640
641void __down(struct semaphore * sem)
642{
643 __do_down(sem,TASK_UNINTERRUPTIBLE);
644}
645
646int __down_interruptible(struct semaphore * sem)
647{
648 return __do_down(sem,TASK_INTERRUPTIBLE);
649}
650
651
652static inline void __sleep_on(struct wait_queue **p, int state)
653{
654 unsigned long flags;
655 struct wait_queue wait = { current, NULL };
656
657 if (!p)
658 return;
659 current->state = state;
660 write_lock_irqsave(&waitqueue_lock, flags);
661 __add_wait_queue(p, &wait);
662 write_unlock(&waitqueue_lock);
663 schedule();
664 write_lock_irq(&waitqueue_lock);
665 __remove_wait_queue(p, &wait);
666 write_unlock_irqrestore(&waitqueue_lock, flags);
667}
668
669void interruptible_sleep_on(struct wait_queue **p)
670{
671 __sleep_on(p,TASK_INTERRUPTIBLE);
672}
673
674void sleep_on(struct wait_queue **p)
675{
676 __sleep_on(p,TASK_UNINTERRUPTIBLE);
677}
678
679static inline void cascade_timers(struct timer_vec *tv)
680{
681
682 struct timer_list *timer;
683 timer = tv->vec[tv->index];
684
685
686
687
688 while (timer) {
689 struct timer_list *tmp = timer;
690 timer = timer->next;
691 internal_add_timer(tmp);
692 }
693 tv->vec[tv->index] = NULL;
694 tv->index = (tv->index + 1) & TVN_MASK;
695}
696
697static inline void run_timer_list(void)
698{
699 spin_lock_irq(&timerlist_lock);
700 while ((long)(jiffies - timer_jiffies) >= 0) {
701 struct timer_list *timer;
702 if (!tv1.index) {
703 int n = 1;
704 do {
705 cascade_timers(tvecs[n]);
706 } while (tvecs[n]->index == 1 && ++n < NOOF_TVECS);
707 }
708 while ((timer = tv1.vec[tv1.index])) {
709 void (*fn)(unsigned long) = timer->function;
710 unsigned long data = timer->data;
711 detach_timer(timer);
712 timer->next = timer->prev = NULL;
713 spin_unlock_irq(&timerlist_lock);
714 fn(data);
715 spin_lock_irq(&timerlist_lock);
716 }
717 ++timer_jiffies;
718 tv1.index = (tv1.index + 1) & TVR_MASK;
719 }
720 spin_unlock_irq(&timerlist_lock);
721}
722
723
724static inline void run_old_timers(void)
725{
726 struct timer_struct *tp;
727 unsigned long mask;
728
729 for (mask = 1, tp = timer_table+0 ; mask ; tp++,mask += mask) {
730 if (mask > timer_active)
731 break;
732 if (!(mask & timer_active))
733 continue;
734 if (tp->expires > jiffies)
735 continue;
736 timer_active &= ~mask;
737 tp->fn();
738 sti();
739 }
740}
741
742spinlock_t tqueue_lock;
743
744void tqueue_bh(void)
745{
746 run_task_queue(&tq_timer);
747}
748
749void immediate_bh(void)
750{
751 run_task_queue(&tq_immediate);
752}
753
754unsigned long timer_active = 0;
755struct timer_struct timer_table[32];
756
757
758
759
760
761
762
763unsigned long avenrun[3] = { 0,0,0 };
764
765
766
767
768static unsigned long count_active_tasks(void)
769{
770 struct task_struct *p;
771 unsigned long nr = 0;
772
773 read_lock(&tasklist_lock);
774 for_each_task(p) {
775 if (p->pid &&
776 (p->state == TASK_RUNNING ||
777 p->state == TASK_UNINTERRUPTIBLE ||
778 p->state == TASK_SWAPPING))
779 nr += FIXED_1;
780 }
781 read_unlock(&tasklist_lock);
782 return nr;
783}
784
785static inline void calc_load(unsigned long ticks)
786{
787 unsigned long active_tasks;
788 static int count = LOAD_FREQ;
789
790 count -= ticks;
791 if (count < 0) {
792 count += LOAD_FREQ;
793 active_tasks = count_active_tasks();
794 CALC_LOAD(avenrun[0], EXP_1, active_tasks);
795 CALC_LOAD(avenrun[1], EXP_5, active_tasks);
796 CALC_LOAD(avenrun[2], EXP_15, active_tasks);
797 }
798}
799
800
801
802
803
804
805
806
807
808
809static void second_overflow(void)
810{
811 long ltemp;
812
813
814 time_maxerror += time_tolerance >> SHIFT_USEC;
815 if ( time_maxerror > MAXPHASE )
816 time_maxerror = MAXPHASE;
817
818
819
820
821
822
823
824
825
826
827 switch (time_state) {
828
829 case TIME_OK:
830 if (time_status & STA_INS)
831 time_state = TIME_INS;
832 else if (time_status & STA_DEL)
833 time_state = TIME_DEL;
834 break;
835
836 case TIME_INS:
837 if (xtime.tv_sec % 86400 == 0) {
838 xtime.tv_sec--;
839 time_state = TIME_OOP;
840 printk("Clock: inserting leap second 23:59:60 UTC\n");
841 }
842 break;
843
844 case TIME_DEL:
845 if ((xtime.tv_sec + 1) % 86400 == 0) {
846 xtime.tv_sec++;
847 time_state = TIME_WAIT;
848 printk("Clock: deleting leap second 23:59:59 UTC\n");
849 }
850 break;
851
852 case TIME_OOP:
853 time_state = TIME_WAIT;
854 break;
855
856 case TIME_WAIT:
857 if (!(time_status & (STA_INS | STA_DEL)))
858 time_state = TIME_OK;
859 }
860
861
862
863
864
865
866
867
868
869
870 if (time_offset < 0) {
871 ltemp = -time_offset;
872 if (!(time_status & STA_FLL))
873 ltemp >>= SHIFT_KG + time_constant;
874 if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE)
875 ltemp = (MAXPHASE / MINSEC) << SHIFT_UPDATE;
876 time_offset += ltemp;
877 time_adj = -ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
878 } else {
879 ltemp = time_offset;
880 if (!(time_status & STA_FLL))
881 ltemp >>= SHIFT_KG + time_constant;
882 if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE)
883 ltemp = (MAXPHASE / MINSEC) << SHIFT_UPDATE;
884 time_offset -= ltemp;
885 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
886 }
887
888
889
890
891
892
893
894
895 pps_valid++;
896 if (pps_valid == PPS_VALID) {
897 pps_jitter = MAXTIME;
898 pps_stabil = MAXFREQ;
899 time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
900 STA_PPSWANDER | STA_PPSERROR);
901 }
902 ltemp = time_freq + pps_freq;
903 if (ltemp < 0)
904 time_adj -= -ltemp >>
905 (SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE);
906 else
907 time_adj += ltemp >>
908 (SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE);
909
910#if HZ == 100
911
912 if (time_adj < 0)
913 time_adj -= -time_adj >> 2;
914 else
915 time_adj += time_adj >> 2;
916#endif
917}
918
919
920static void update_wall_time_one_tick(void)
921{
922
923
924
925
926 time_phase += time_adj;
927 if (time_phase <= -FINEUSEC) {
928 long ltemp = -time_phase >> SHIFT_SCALE;
929 time_phase += ltemp << SHIFT_SCALE;
930 xtime.tv_usec += tick + time_adjust_step - ltemp;
931 }
932 else if (time_phase >= FINEUSEC) {
933 long ltemp = time_phase >> SHIFT_SCALE;
934 time_phase -= ltemp << SHIFT_SCALE;
935 xtime.tv_usec += tick + time_adjust_step + ltemp;
936 } else
937 xtime.tv_usec += tick + time_adjust_step;
938
939 if (time_adjust) {
940
941
942
943
944
945
946
947
948
949 if (time_adjust > tickadj)
950 time_adjust_step = tickadj;
951 else if (time_adjust < -tickadj)
952 time_adjust_step = -tickadj;
953 else
954 time_adjust_step = time_adjust;
955
956
957 time_adjust -= time_adjust_step;
958 }
959 else
960 time_adjust_step = 0;
961}
962
963
964
965
966
967
968
969
970static void update_wall_time(unsigned long ticks)
971{
972 do {
973 ticks--;
974 update_wall_time_one_tick();
975 } while (ticks);
976
977 if (xtime.tv_usec >= 1000000) {
978 xtime.tv_usec -= 1000000;
979 xtime.tv_sec++;
980 second_overflow();
981 }
982}
983
984static inline void do_process_times(struct task_struct *p,
985 unsigned long user, unsigned long system)
986{
987 long psecs;
988
989 psecs = (p->times.tms_utime += user);
990 psecs += (p->times.tms_stime += system);
991 if (psecs / HZ > p->rlim[RLIMIT_CPU].rlim_cur) {
992
993 if (!(psecs % HZ))
994 send_sig(SIGXCPU, p, 1);
995
996 if (psecs / HZ > p->rlim[RLIMIT_CPU].rlim_max)
997 send_sig(SIGKILL, p, 1);
998 }
999}
1000
1001static inline void do_it_virt(struct task_struct * p, unsigned long ticks)
1002{
1003 unsigned long it_virt = p->it_virt_value;
1004
1005 if (it_virt) {
1006 if (it_virt <= ticks) {
1007 it_virt = ticks + p->it_virt_incr;
1008 send_sig(SIGVTALRM, p, 1);
1009 }
1010 p->it_virt_value = it_virt - ticks;
1011 }
1012}
1013
1014static inline void do_it_prof(struct task_struct * p, unsigned long ticks)
1015{
1016 unsigned long it_prof = p->it_prof_value;
1017
1018 if (it_prof) {
1019 if (it_prof <= ticks) {
1020 it_prof = ticks + p->it_prof_incr;
1021 send_sig(SIGPROF, p, 1);
1022 }
1023 p->it_prof_value = it_prof - ticks;
1024 }
1025}
1026
1027void update_one_process(struct task_struct *p,
1028 unsigned long ticks, unsigned long user, unsigned long system)
1029{
1030 do_process_times(p, user, system);
1031 do_it_virt(p, user);
1032 do_it_prof(p, ticks);
1033}
1034
1035static void update_process_times(unsigned long ticks, unsigned long system)
1036{
1037
1038
1039
1040#ifndef __SMP__
1041 struct task_struct * p = current;
1042 unsigned long user = ticks - system;
1043 if (p->pid) {
1044 p->counter -= ticks;
1045 if (p->counter < 0) {
1046 p->counter = 0;
1047 need_resched = 1;
1048 }
1049 if (p->priority < DEF_PRIORITY)
1050 kstat.cpu_nice += user;
1051 else
1052 kstat.cpu_user += user;
1053 kstat.cpu_system += system;
1054 }
1055 update_one_process(p, ticks, user, system);
1056#endif
1057}
1058
1059volatile unsigned long lost_ticks = 0;
1060static unsigned long lost_ticks_system = 0;
1061
1062static inline void update_times(void)
1063{
1064 unsigned long ticks;
1065 unsigned long flags;
1066
1067 save_flags(flags);
1068 cli();
1069
1070 ticks = lost_ticks;
1071 lost_ticks = 0;
1072
1073 if (ticks) {
1074 unsigned long system;
1075 system = xchg(&lost_ticks_system, 0);
1076
1077 calc_load(ticks);
1078 update_wall_time(ticks);
1079 restore_flags(flags);
1080
1081 update_process_times(ticks, system);
1082
1083 } else
1084 restore_flags(flags);
1085}
1086
1087static void timer_bh(void)
1088{
1089 update_times();
1090 run_old_timers();
1091 run_timer_list();
1092}
1093
1094void do_timer(struct pt_regs * regs)
1095{
1096 (*(unsigned long *)&jiffies)++;
1097 lost_ticks++;
1098 mark_bh(TIMER_BH);
1099 if (!user_mode(regs))
1100 lost_ticks_system++;
1101 if (tq_timer)
1102 mark_bh(TQUEUE_BH);
1103}
1104
1105#ifndef __alpha__
1106
1107
1108
1109
1110
1111asmlinkage unsigned int sys_alarm(unsigned int seconds)
1112{
1113 struct itimerval it_new, it_old;
1114 unsigned int oldalarm;
1115
1116 it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
1117 it_new.it_value.tv_sec = seconds;
1118 it_new.it_value.tv_usec = 0;
1119 _setitimer(ITIMER_REAL, &it_new, &it_old);
1120 oldalarm = it_old.it_value.tv_sec;
1121
1122
1123 if (it_old.it_value.tv_usec)
1124 oldalarm++;
1125 return oldalarm;
1126}
1127
1128
1129
1130
1131
1132
1133asmlinkage int sys_getpid(void)
1134{
1135
1136 return current->pid;
1137}
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162asmlinkage int sys_getppid(void)
1163{
1164 int pid;
1165 struct task_struct * me = current;
1166 struct task_struct * parent;
1167
1168 parent = me->p_opptr;
1169 for (;;) {
1170 pid = parent->pid;
1171#if __SMP__
1172{
1173 struct task_struct *old = parent;
1174 mb();
1175 parent = me->p_opptr;
1176 if (old != parent)
1177 continue;
1178}
1179#endif
1180 break;
1181 }
1182 return pid;
1183}
1184
1185asmlinkage int sys_getuid(void)
1186{
1187
1188 return current->uid;
1189}
1190
1191asmlinkage int sys_geteuid(void)
1192{
1193
1194 return current->euid;
1195}
1196
1197asmlinkage int sys_getgid(void)
1198{
1199
1200 return current->gid;
1201}
1202
1203asmlinkage int sys_getegid(void)
1204{
1205
1206 return current->egid;
1207}
1208
1209
1210
1211
1212
1213
1214
1215asmlinkage int sys_nice(int increment)
1216{
1217 unsigned long newprio;
1218 int increase = 0;
1219
1220
1221
1222
1223
1224
1225
1226 newprio = increment;
1227 if (increment < 0) {
1228 if (!suser())
1229 return -EPERM;
1230 newprio = -increment;
1231 increase = 1;
1232 }
1233
1234 if (newprio > 40)
1235 newprio = 40;
1236
1237
1238
1239
1240
1241
1242
1243 newprio = (newprio * DEF_PRIORITY + 10) / 20;
1244 increment = newprio;
1245 if (increase)
1246 increment = -increment;
1247
1248
1249
1250
1251
1252
1253
1254
1255 newprio = current->priority - increment;
1256 if ((signed) newprio < 1)
1257 newprio = 1;
1258 if (newprio > DEF_PRIORITY*2)
1259 newprio = DEF_PRIORITY*2;
1260 current->priority = newprio;
1261 return 0;
1262}
1263
1264#endif
1265
1266static inline struct task_struct *find_process_by_pid(pid_t pid)
1267{
1268 if (pid)
1269 return find_task_by_pid(pid);
1270 else
1271 return current;
1272}
1273
1274static int setscheduler(pid_t pid, int policy,
1275 struct sched_param *param)
1276{
1277 struct sched_param lp;
1278 struct task_struct *p;
1279
1280 if (!param || pid < 0)
1281 return -EINVAL;
1282
1283 if (copy_from_user(&lp, param, sizeof(struct sched_param)))
1284 return -EFAULT;
1285
1286 p = find_process_by_pid(pid);
1287 if (!p)
1288 return -ESRCH;
1289
1290 if (policy < 0)
1291 policy = p->policy;
1292 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
1293 policy != SCHED_OTHER)
1294 return -EINVAL;
1295
1296
1297
1298
1299
1300 if (lp.sched_priority < 0 || lp.sched_priority > 99)
1301 return -EINVAL;
1302 if ((policy == SCHED_OTHER) != (lp.sched_priority == 0))
1303 return -EINVAL;
1304
1305 if ((policy == SCHED_FIFO || policy == SCHED_RR) && !suser())
1306 return -EPERM;
1307 if ((current->euid != p->euid) && (current->euid != p->uid) &&
1308 !suser())
1309 return -EPERM;
1310
1311 p->policy = policy;
1312 p->rt_priority = lp.sched_priority;
1313 spin_lock(&scheduler_lock);
1314 spin_lock_irq(&runqueue_lock);
1315 if (p->next_run)
1316 move_last_runqueue(p);
1317 spin_unlock_irq(&runqueue_lock);
1318 spin_unlock(&scheduler_lock);
1319 need_resched = 1;
1320 return 0;
1321}
1322
1323asmlinkage int sys_sched_setscheduler(pid_t pid, int policy,
1324 struct sched_param *param)
1325{
1326 return setscheduler(pid, policy, param);
1327}
1328
1329asmlinkage int sys_sched_setparam(pid_t pid, struct sched_param *param)
1330{
1331 return setscheduler(pid, -1, param);
1332}
1333
1334asmlinkage int sys_sched_getscheduler(pid_t pid)
1335{
1336 struct task_struct *p;
1337
1338 if (pid < 0)
1339 return -EINVAL;
1340
1341 p = find_process_by_pid(pid);
1342 if (!p)
1343 return -ESRCH;
1344
1345 return p->policy;
1346}
1347
1348asmlinkage int sys_sched_getparam(pid_t pid, struct sched_param *param)
1349{
1350 struct task_struct *p;
1351 struct sched_param lp;
1352
1353 if (!param || pid < 0)
1354 return -EINVAL;
1355
1356 p = find_process_by_pid(pid);
1357 if (!p)
1358 return -ESRCH;
1359
1360 lp.sched_priority = p->rt_priority;
1361 return copy_to_user(param, &lp, sizeof(struct sched_param)) ? -EFAULT : 0;
1362}
1363
1364asmlinkage int sys_sched_yield(void)
1365{
1366
1367
1368
1369
1370 current->counter = 0;
1371 spin_lock(&scheduler_lock);
1372 spin_lock_irq(&runqueue_lock);
1373 move_last_runqueue(current);
1374 spin_unlock_irq(&runqueue_lock);
1375 spin_unlock(&scheduler_lock);
1376 need_resched = 1;
1377 return 0;
1378}
1379
1380asmlinkage int sys_sched_get_priority_max(int policy)
1381{
1382 int ret = -EINVAL;
1383
1384 switch (policy) {
1385 case SCHED_FIFO:
1386 case SCHED_RR:
1387 ret = 99;
1388 break;
1389 case SCHED_OTHER:
1390 ret = 0;
1391 break;
1392 }
1393 return ret;
1394}
1395
1396asmlinkage int sys_sched_get_priority_min(int policy)
1397{
1398 int ret = -EINVAL;
1399
1400 switch (policy) {
1401 case SCHED_FIFO:
1402 case SCHED_RR:
1403 ret = 1;
1404 break;
1405 case SCHED_OTHER:
1406 ret = 0;
1407 }
1408 return ret;
1409}
1410
1411asmlinkage int sys_sched_rr_get_interval(pid_t pid, struct timespec *interval)
1412{
1413 struct timespec t;
1414
1415 t.tv_sec = 0;
1416 t.tv_nsec = 150000;
1417 if (copy_to_user(interval, &t, sizeof(struct timespec)))
1418 return -EFAULT;
1419 return 0;
1420}
1421
1422
1423
1424
1425
1426static unsigned long timespectojiffies(struct timespec *value)
1427{
1428 unsigned long sec = (unsigned) value->tv_sec;
1429 long nsec = value->tv_nsec;
1430
1431 if (sec > (LONG_MAX / HZ))
1432 return LONG_MAX;
1433 nsec += 1000000000L / HZ - 1;
1434 nsec /= 1000000000L / HZ;
1435 return HZ * sec + nsec;
1436}
1437
1438static void jiffiestotimespec(unsigned long jiffies, struct timespec *value)
1439{
1440 value->tv_nsec = (jiffies % HZ) * (1000000000L / HZ);
1441 value->tv_sec = jiffies / HZ;
1442}
1443
1444asmlinkage int sys_nanosleep(struct timespec *rqtp, struct timespec *rmtp)
1445{
1446 struct timespec t;
1447 unsigned long expire;
1448
1449 if(copy_from_user(&t, rqtp, sizeof(struct timespec)))
1450 return -EFAULT;
1451
1452 if (t.tv_nsec >= 1000000000L || t.tv_nsec < 0 || t.tv_sec < 0)
1453 return -EINVAL;
1454
1455
1456 if (t.tv_sec == 0 && t.tv_nsec <= 2000000L &&
1457 current->policy != SCHED_OTHER)
1458 {
1459
1460
1461
1462
1463
1464
1465 udelay((t.tv_nsec + 999) / 1000);
1466 return 0;
1467 }
1468
1469 expire = timespectojiffies(&t) + (t.tv_sec || t.tv_nsec) + jiffies;
1470
1471 current->timeout = expire;
1472 current->state = TASK_INTERRUPTIBLE;
1473 schedule();
1474
1475 if (expire > jiffies) {
1476 if (rmtp) {
1477 jiffiestotimespec(expire - jiffies -
1478 (expire > jiffies + 1), &t);
1479 if (copy_to_user(rmtp, &t, sizeof(struct timespec)))
1480 return -EFAULT;
1481 }
1482 return -EINTR;
1483 }
1484 return 0;
1485}
1486
1487static void show_task(int nr,struct task_struct * p)
1488{
1489 unsigned long free = 0;
1490 static const char * stat_nam[] = { "R", "S", "D", "Z", "T", "W" };
1491
1492 printk("%-8s %3d ", p->comm, (p == current) ? -nr : nr);
1493 if (((unsigned) p->state) < sizeof(stat_nam)/sizeof(char *))
1494 printk(stat_nam[p->state]);
1495 else
1496 printk(" ");
1497#if ((~0UL) == 0xffffffff)
1498 if (p == current)
1499 printk(" current ");
1500 else
1501 printk(" %08lX ", thread_saved_pc(&p->tss));
1502#else
1503 if (p == current)
1504 printk(" current task ");
1505 else
1506 printk(" %016lx ", thread_saved_pc(&p->tss));
1507#endif
1508#if 0
1509 for (free = 1; free < PAGE_SIZE/sizeof(long) ; free++) {
1510 if (((unsigned long *)p->kernel_stack_page)[free])
1511 break;
1512 }
1513#endif
1514 printk("%5lu %5d %6d ", free*sizeof(long), p->pid, p->p_pptr->pid);
1515 if (p->p_cptr)
1516 printk("%5d ", p->p_cptr->pid);
1517 else
1518 printk(" ");
1519 if (p->p_ysptr)
1520 printk("%7d", p->p_ysptr->pid);
1521 else
1522 printk(" ");
1523 if (p->p_osptr)
1524 printk(" %5d\n", p->p_osptr->pid);
1525 else
1526 printk("\n");
1527}
1528
1529void show_state(void)
1530{
1531 struct task_struct *p;
1532
1533#if ((~0UL) == 0xffffffff)
1534 printk("\n"
1535 " free sibling\n");
1536 printk(" task PC stack pid father child younger older\n");
1537#else
1538 printk("\n"
1539 " free sibling\n");
1540 printk(" task PC stack pid father child younger older\n");
1541#endif
1542 read_lock(&tasklist_lock);
1543 for_each_task(p)
1544 show_task((p->tarray_ptr - &task[0]),p);
1545 read_unlock(&tasklist_lock);
1546}
1547
1548__initfunc(void sched_init(void))
1549{
1550
1551
1552
1553
1554 int cpu=hard_smp_processor_id();
1555 int nr = NR_TASKS;
1556
1557 init_task.processor=cpu;
1558
1559
1560 while(--nr > 0)
1561 add_free_taskslot(&task[nr]);
1562
1563 for(nr = 0; nr < PIDHASH_SZ; nr++)
1564 pidhash[nr] = NULL;
1565
1566 init_bh(TIMER_BH, timer_bh);
1567 init_bh(TQUEUE_BH, tqueue_bh);
1568 init_bh(IMMEDIATE_BH, immediate_bh);
1569}
1570