This source file includes following definitions.
- add_to_runqueue
- del_from_runqueue
- wake_up_process
- process_timeout
- schedule
- sys_pause
- wake_up
- wake_up_interruptible
- __down
- __sleep_on
- interruptible_sleep_on
- sleep_on
- add_timer
- del_timer
- count_active_tasks
- calc_load
- second_overflow
- timer_bh
- tqueue_bh
- immediate_bh
- do_timer
- sys_alarm
- sys_getpid
- sys_getppid
- sys_getuid
- sys_geteuid
- sys_getgid
- sys_getegid
- sys_nice
- show_task
- show_state
- sched_init
1
2
3
4
5
6
7
8
9
10
11
12
13
14 #include <linux/config.h>
15 #include <linux/signal.h>
16 #include <linux/sched.h>
17 #include <linux/timer.h>
18 #include <linux/kernel.h>
19 #include <linux/kernel_stat.h>
20 #include <linux/fdreg.h>
21 #include <linux/errno.h>
22 #include <linux/time.h>
23 #include <linux/ptrace.h>
24 #include <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <linux/tqueue.h>
27 #include <linux/resource.h>
28 #include <linux/mm.h>
29
30 #include <asm/system.h>
31 #include <asm/io.h>
32 #include <asm/segment.h>
33 #include <asm/pgtable.h>
34
35 #define TIMER_IRQ 0
36
37 #include <linux/timex.h>
38
39
40
41
42 long tick = 1000000 / HZ;
43 volatile struct timeval xtime;
44 int tickadj = 500/HZ;
45
46 DECLARE_TASK_QUEUE(tq_timer);
47 DECLARE_TASK_QUEUE(tq_immediate);
48 DECLARE_TASK_QUEUE(tq_scheduler);
49
50
51
52
53 int time_status = TIME_BAD;
54 long time_offset = 0;
55 long time_constant = 0;
56 long time_tolerance = MAXFREQ;
57 long time_precision = 1;
58 long time_maxerror = 0x70000000;
59 long time_esterror = 0x70000000;
60 long time_phase = 0;
61 long time_freq = 0;
62 long time_adj = 0;
63 long time_reftime = 0;
64
65 long time_adjust = 0;
66 long time_adjust_step = 0;
67
68 int need_resched = 0;
69 unsigned long event = 0;
70
71 extern int _setitimer(int, struct itimerval *, struct itimerval *);
72 unsigned long * prof_buffer = NULL;
73 unsigned long prof_len = 0;
74
75 #define _S(nr) (1<<((nr)-1))
76
77 extern void mem_use(void);
78
79 extern int timer_interrupt(void);
80
81 static unsigned long init_kernel_stack[1024] = { STACK_MAGIC, };
82 unsigned long init_user_stack[1024] = { STACK_MAGIC, };
83 static struct vm_area_struct init_mmap = INIT_MMAP;
84 struct task_struct init_task = INIT_TASK;
85
86 unsigned long volatile jiffies=0;
87
88 struct task_struct *current = &init_task;
89 struct task_struct *last_task_used_math = NULL;
90
91 struct task_struct * task[NR_TASKS] = {&init_task, };
92
93 struct kernel_stat kstat = { 0 };
94
95 static inline void add_to_runqueue(struct task_struct * p)
96 {
97 #if 1
98 if (p->next_run || p->prev_run) {
99 printk("task already on run-queue\n");
100 return;
101 }
102 #endif
103 if (p->counter > current->counter + 3)
104 need_resched = 1;
105 nr_running++;
106 (p->next_run = init_task.next_run)->prev_run = p;
107 p->prev_run = &init_task;
108 init_task.next_run = p;
109 }
110
111 static inline void del_from_runqueue(struct task_struct * p)
112 {
113 struct task_struct *next = p->next_run;
114 struct task_struct *prev = p->prev_run;
115
116 #if 1
117 if (!next || !prev) {
118 printk("task not on run-queue\n");
119 return;
120 }
121 #endif
122 if (p == &init_task) {
123 static int nr = 0;
124 if (nr < 5) {
125 nr++;
126 printk("idle task may not sleep\n");
127 }
128 return;
129 }
130 nr_running--;
131 next->prev_run = prev;
132 prev->next_run = next;
133 p->next_run = NULL;
134 p->prev_run = NULL;
135 }
136
137
138
139
140
141
142
143
144
145 inline void wake_up_process(struct task_struct * p)
146 {
147 unsigned long flags;
148
149 save_flags(flags);
150 cli();
151 p->state = TASK_RUNNING;
152 if (!p->next_run)
153 add_to_runqueue(p);
154 restore_flags(flags);
155 }
156
157 static void process_timeout(unsigned long __data)
158 {
159 struct task_struct * p = (struct task_struct *) __data;
160
161 p->timeout = 0;
162 wake_up_process(p);
163 }
164
165
166
167
168
169
170
171
172
173
174
175 asmlinkage void schedule(void)
176 {
177 int c;
178 struct task_struct * p;
179 struct task_struct * next;
180 unsigned long timeout = 0;
181
182
183
184 if (intr_count) {
185 printk("Aiee: scheduling in interrupt\n");
186 return;
187 }
188 run_task_queue(&tq_scheduler);
189
190 need_resched = 0;
191 cli();
192 switch (current->state) {
193 case TASK_INTERRUPTIBLE:
194 if (current->signal & ~current->blocked)
195 goto makerunnable;
196 timeout = current->timeout;
197 if (timeout && (timeout <= jiffies)) {
198 current->timeout = 0;
199 timeout = 0;
200 makerunnable:
201 current->state = TASK_RUNNING;
202 break;
203 }
204 default:
205 del_from_runqueue(current);
206 case TASK_RUNNING:
207 }
208 p = init_task.next_run;
209 sti();
210
211
212
213
214
215
216
217 c = -1000;
218 next = &init_task;
219 while (p != &init_task) {
220 if (p->counter > c)
221 c = p->counter, next = p;
222 p = p->next_run;
223 }
224
225
226 if (!c) {
227 for_each_task(p)
228 p->counter = (p->counter >> 1) + p->priority;
229 }
230 if (current != next) {
231 struct timer_list timer;
232
233 kstat.context_swtch++;
234 if (timeout) {
235 init_timer(&timer);
236 timer.expires = timeout;
237 timer.data = (unsigned long) current;
238 timer.function = process_timeout;
239 add_timer(&timer);
240 }
241 switch_to(next);
242 if (timeout)
243 del_timer(&timer);
244 }
245 }
246
247 asmlinkage int sys_pause(void)
248 {
249 current->state = TASK_INTERRUPTIBLE;
250 schedule();
251 return -ERESTARTNOHAND;
252 }
253
254
255
256
257
258
259
260
261
262 void wake_up(struct wait_queue **q)
263 {
264 struct wait_queue *tmp;
265 struct task_struct * p;
266
267 if (!q || !(tmp = *q))
268 return;
269 do {
270 if ((p = tmp->task) != NULL) {
271 if ((p->state == TASK_UNINTERRUPTIBLE) ||
272 (p->state == TASK_INTERRUPTIBLE))
273 wake_up_process(p);
274 }
275 if (!tmp->next) {
276 printk("wait_queue is bad (eip = %p)\n",
277 __builtin_return_address(0));
278 printk(" q = %p\n",q);
279 printk(" *q = %p\n",*q);
280 printk(" tmp = %p\n",tmp);
281 break;
282 }
283 tmp = tmp->next;
284 } while (tmp != *q);
285 }
286
287 void wake_up_interruptible(struct wait_queue **q)
288 {
289 struct wait_queue *tmp;
290 struct task_struct * p;
291
292 if (!q || !(tmp = *q))
293 return;
294 do {
295 if ((p = tmp->task) != NULL) {
296 if (p->state == TASK_INTERRUPTIBLE)
297 wake_up_process(p);
298 }
299 if (!tmp->next) {
300 printk("wait_queue is bad (eip = %p)\n",
301 __builtin_return_address(0));
302 printk(" q = %p\n",q);
303 printk(" *q = %p\n",*q);
304 printk(" tmp = %p\n",tmp);
305 break;
306 }
307 tmp = tmp->next;
308 } while (tmp != *q);
309 }
310
311 void __down(struct semaphore * sem)
312 {
313 struct wait_queue wait = { current, NULL };
314 add_wait_queue(&sem->wait, &wait);
315 current->state = TASK_UNINTERRUPTIBLE;
316 while (sem->count <= 0) {
317 schedule();
318 current->state = TASK_UNINTERRUPTIBLE;
319 }
320 current->state = TASK_RUNNING;
321 remove_wait_queue(&sem->wait, &wait);
322 }
323
324 static inline void __sleep_on(struct wait_queue **p, int state)
325 {
326 unsigned long flags;
327 struct wait_queue wait = { current, NULL };
328
329 if (!p)
330 return;
331 if (current == task[0])
332 panic("task[0] trying to sleep");
333 current->state = state;
334 add_wait_queue(p, &wait);
335 save_flags(flags);
336 sti();
337 schedule();
338 remove_wait_queue(p, &wait);
339 restore_flags(flags);
340 }
341
342 void interruptible_sleep_on(struct wait_queue **p)
343 {
344 __sleep_on(p,TASK_INTERRUPTIBLE);
345 }
346
347 void sleep_on(struct wait_queue **p)
348 {
349 __sleep_on(p,TASK_UNINTERRUPTIBLE);
350 }
351
352
353
354
355
356 static struct timer_list timer_head = { &timer_head, &timer_head, ~0, 0, NULL };
357 #define SLOW_BUT_DEBUGGING_TIMERS 1
358
359 void add_timer(struct timer_list * timer)
360 {
361 unsigned long flags;
362 struct timer_list *p;
363
364 #if SLOW_BUT_DEBUGGING_TIMERS
365 if (timer->next || timer->prev) {
366 printk("add_timer() called with non-zero list from %p\n",
367 __builtin_return_address(0));
368 return;
369 }
370 #endif
371 p = &timer_head;
372 save_flags(flags);
373 cli();
374 do {
375 p = p->next;
376 } while (timer->expires > p->expires);
377 timer->next = p;
378 timer->prev = p->prev;
379 p->prev = timer;
380 timer->prev->next = timer;
381 restore_flags(flags);
382 }
383
384 int del_timer(struct timer_list * timer)
385 {
386 unsigned long flags;
387 #if SLOW_BUT_DEBUGGING_TIMERS
388 struct timer_list * p;
389
390 p = &timer_head;
391 save_flags(flags);
392 cli();
393 while ((p = p->next) != &timer_head) {
394 if (p == timer) {
395 timer->next->prev = timer->prev;
396 timer->prev->next = timer->next;
397 timer->next = timer->prev = NULL;
398 restore_flags(flags);
399 return 1;
400 }
401 }
402 if (timer->next || timer->prev)
403 printk("del_timer() called from %p with timer not initialized\n",
404 __builtin_return_address(0));
405 restore_flags(flags);
406 return 0;
407 #else
408 save_flags(flags);
409 cli();
410 if (timer->next) {
411 timer->next->prev = timer->prev;
412 timer->prev->next = timer->next;
413 timer->next = timer->prev = NULL;
414 restore_flags(flags);
415 return 1;
416 }
417 restore_flags(flags);
418 return 0;
419 #endif
420 }
421
422 unsigned long timer_active = 0;
423 struct timer_struct timer_table[32];
424
425
426
427
428
429
430
431 unsigned long avenrun[3] = { 0,0,0 };
432
433
434
435
436 static unsigned long count_active_tasks(void)
437 {
438 struct task_struct **p;
439 unsigned long nr = 0;
440
441 for(p = &LAST_TASK; p > &FIRST_TASK; --p)
442 if (*p && ((*p)->state == TASK_RUNNING ||
443 (*p)->state == TASK_UNINTERRUPTIBLE ||
444 (*p)->state == TASK_SWAPPING))
445 nr += FIXED_1;
446 return nr;
447 }
448
449 static inline void calc_load(void)
450 {
451 unsigned long active_tasks;
452 static int count = LOAD_FREQ;
453
454 if (count-- > 0)
455 return;
456 count = LOAD_FREQ;
457 active_tasks = count_active_tasks();
458 CALC_LOAD(avenrun[0], EXP_1, active_tasks);
459 CALC_LOAD(avenrun[1], EXP_5, active_tasks);
460 CALC_LOAD(avenrun[2], EXP_15, active_tasks);
461 }
462
463
464
465
466
467
468
469
470
471
472
473 static void second_overflow(void)
474 {
475 long ltemp;
476
477
478 time_maxerror = (0x70000000-time_maxerror < time_tolerance) ?
479 0x70000000 : (time_maxerror + time_tolerance);
480
481
482 if (time_offset < 0) {
483 ltemp = (-(time_offset+1) >> (SHIFT_KG + time_constant)) + 1;
484 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
485 time_offset += (time_adj * HZ) >> (SHIFT_SCALE - SHIFT_UPDATE);
486 time_adj = - time_adj;
487 } else if (time_offset > 0) {
488 ltemp = ((time_offset-1) >> (SHIFT_KG + time_constant)) + 1;
489 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
490 time_offset -= (time_adj * HZ) >> (SHIFT_SCALE - SHIFT_UPDATE);
491 } else {
492 time_adj = 0;
493 }
494
495 time_adj += (time_freq >> (SHIFT_KF + SHIFT_HZ - SHIFT_SCALE))
496 + FINETUNE;
497
498
499 switch (time_status) {
500 case TIME_INS:
501
502 if (xtime.tv_sec % 86400 == 0) {
503 xtime.tv_sec--;
504 time_status = TIME_OOP;
505 printk("Clock: inserting leap second 23:59:60 UTC\n");
506 }
507 break;
508
509 case TIME_DEL:
510
511 if (xtime.tv_sec % 86400 == 86399) {
512 xtime.tv_sec++;
513 time_status = TIME_OK;
514 printk("Clock: deleting leap second 23:59:59 UTC\n");
515 }
516 break;
517
518 case TIME_OOP:
519 time_status = TIME_OK;
520 break;
521 }
522 }
523
524
525
526
527 static void timer_bh(void * unused)
528 {
529 unsigned long mask;
530 struct timer_struct *tp;
531 struct timer_list * timer;
532
533 cli();
534 while ((timer = timer_head.next) != &timer_head && timer->expires < jiffies) {
535 void (*fn)(unsigned long) = timer->function;
536 unsigned long data = timer->data;
537 timer->next->prev = timer->prev;
538 timer->prev->next = timer->next;
539 timer->next = timer->prev = NULL;
540 sti();
541 fn(data);
542 cli();
543 }
544 sti();
545
546 for (mask = 1, tp = timer_table+0 ; mask ; tp++,mask += mask) {
547 if (mask > timer_active)
548 break;
549 if (!(mask & timer_active))
550 continue;
551 if (tp->expires > jiffies)
552 continue;
553 timer_active &= ~mask;
554 tp->fn();
555 sti();
556 }
557 }
558
559 void tqueue_bh(void * unused)
560 {
561 run_task_queue(&tq_timer);
562 }
563
564 void immediate_bh(void * unused)
565 {
566 run_task_queue(&tq_immediate);
567 }
568
569
570
571
572
573
574
575 static void do_timer(int irq, struct pt_regs * regs)
576 {
577 unsigned long mask;
578 struct timer_struct *tp;
579
580 static long last_rtc_update=0;
581 extern int set_rtc_mmss(unsigned long);
582
583 long ltemp, psecs;
584
585
586
587
588 time_phase += time_adj;
589 if (time_phase < -FINEUSEC) {
590 ltemp = -time_phase >> SHIFT_SCALE;
591 time_phase += ltemp << SHIFT_SCALE;
592 xtime.tv_usec += tick + time_adjust_step - ltemp;
593 }
594 else if (time_phase > FINEUSEC) {
595 ltemp = time_phase >> SHIFT_SCALE;
596 time_phase -= ltemp << SHIFT_SCALE;
597 xtime.tv_usec += tick + time_adjust_step + ltemp;
598 } else
599 xtime.tv_usec += tick + time_adjust_step;
600
601 if (time_adjust)
602 {
603
604
605
606
607
608
609
610
611
612 if (time_adjust > tickadj)
613 time_adjust_step = tickadj;
614 else if (time_adjust < -tickadj)
615 time_adjust_step = -tickadj;
616 else
617 time_adjust_step = time_adjust;
618
619
620 time_adjust -= time_adjust_step;
621 }
622 else
623 time_adjust_step = 0;
624
625 if (xtime.tv_usec >= 1000000) {
626 xtime.tv_usec -= 1000000;
627 xtime.tv_sec++;
628 second_overflow();
629 }
630
631
632
633
634
635 if (time_status != TIME_BAD && xtime.tv_sec > last_rtc_update + 660 &&
636 xtime.tv_usec > 500000 - (tick >> 1) &&
637 xtime.tv_usec < 500000 + (tick >> 1))
638 if (set_rtc_mmss(xtime.tv_sec) == 0)
639 last_rtc_update = xtime.tv_sec;
640 else
641 last_rtc_update = xtime.tv_sec - 600;
642
643 jiffies++;
644 calc_load();
645 if (user_mode(regs)) {
646 current->utime++;
647 if (current != task[0]) {
648 if (current->priority < 15)
649 kstat.cpu_nice++;
650 else
651 kstat.cpu_user++;
652 }
653
654 if (current->it_virt_value && !(--current->it_virt_value)) {
655 current->it_virt_value = current->it_virt_incr;
656 send_sig(SIGVTALRM,current,1);
657 }
658 } else {
659 current->stime++;
660 if(current != task[0])
661 kstat.cpu_system++;
662 #ifdef CONFIG_PROFILE
663 if (prof_buffer && current != task[0]) {
664 extern int _stext;
665 unsigned long eip = regs->eip - (unsigned long) &_stext;
666 eip >>= CONFIG_PROFILE_SHIFT;
667 if (eip < prof_len)
668 prof_buffer[eip]++;
669 }
670 #endif
671 }
672
673
674
675 if ((current->rlim[RLIMIT_CPU].rlim_max != RLIM_INFINITY) &&
676 (((current->stime + current->utime) / HZ) >= current->rlim[RLIMIT_CPU].rlim_max))
677 send_sig(SIGKILL, current, 1);
678 if ((current->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) &&
679 (((current->stime + current->utime) % HZ) == 0)) {
680 psecs = (current->stime + current->utime) / HZ;
681
682 if (psecs == current->rlim[RLIMIT_CPU].rlim_cur)
683 send_sig(SIGXCPU, current, 1);
684
685 else if ((psecs > current->rlim[RLIMIT_CPU].rlim_cur) &&
686 ((psecs - current->rlim[RLIMIT_CPU].rlim_cur) % 5) == 0)
687 send_sig(SIGXCPU, current, 1);
688 }
689
690 if (current != task[0] && 0 > --current->counter) {
691 current->counter = 0;
692 need_resched = 1;
693 }
694
695 if (current->it_prof_value && !(--current->it_prof_value)) {
696 current->it_prof_value = current->it_prof_incr;
697 send_sig(SIGPROF,current,1);
698 }
699 for (mask = 1, tp = timer_table+0 ; mask ; tp++,mask += mask) {
700 if (mask > timer_active)
701 break;
702 if (!(mask & timer_active))
703 continue;
704 if (tp->expires > jiffies)
705 continue;
706 mark_bh(TIMER_BH);
707 }
708 cli();
709 if (timer_head.next->expires < jiffies)
710 mark_bh(TIMER_BH);
711 if (tq_timer != &tq_last)
712 mark_bh(TQUEUE_BH);
713 sti();
714 }
715
716 asmlinkage unsigned int sys_alarm(unsigned int seconds)
717 {
718 struct itimerval it_new, it_old;
719
720 it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
721 it_new.it_value.tv_sec = seconds;
722 it_new.it_value.tv_usec = 0;
723 _setitimer(ITIMER_REAL, &it_new, &it_old);
724 return(it_old.it_value.tv_sec + (it_old.it_value.tv_usec / 1000000));
725 }
726
727 asmlinkage int sys_getpid(void)
728 {
729 return current->pid;
730 }
731
732 asmlinkage int sys_getppid(void)
733 {
734 return current->p_opptr->pid;
735 }
736
737 asmlinkage int sys_getuid(void)
738 {
739 return current->uid;
740 }
741
742 asmlinkage int sys_geteuid(void)
743 {
744 return current->euid;
745 }
746
747 asmlinkage int sys_getgid(void)
748 {
749 return current->gid;
750 }
751
752 asmlinkage int sys_getegid(void)
753 {
754 return current->egid;
755 }
756
757 asmlinkage int sys_nice(long increment)
758 {
759 int newprio;
760
761 if (increment < 0 && !suser())
762 return -EPERM;
763 newprio = current->priority - increment;
764 if (newprio < 1)
765 newprio = 1;
766 if (newprio > 35)
767 newprio = 35;
768 current->priority = newprio;
769 return 0;
770 }
771
772 static void show_task(int nr,struct task_struct * p)
773 {
774 unsigned long free;
775 static const char * stat_nam[] = { "R", "S", "D", "Z", "T", "W" };
776
777 printk("%-8s %3d ", p->comm, (p == current) ? -nr : nr);
778 if (((unsigned) p->state) < sizeof(stat_nam)/sizeof(char *))
779 printk(stat_nam[p->state]);
780 else
781 printk(" ");
782 #if ((~0UL) == 0xffffffff)
783 if (p == current)
784 printk(" current ");
785 else
786 printk(" %08lX ", thread_saved_pc(&p->tss));
787 #else
788 if (p == current)
789 printk(" current task ");
790 else
791 printk(" %016lx ", thread_saved_pc(&p->tss));
792 #endif
793 for (free = 1; free < PAGE_SIZE/sizeof(long) ; free++) {
794 if (((unsigned long *)p->kernel_stack_page)[free])
795 break;
796 }
797 printk("%5lu %5d %6d ", free*sizeof(long), p->pid, p->p_pptr->pid);
798 if (p->p_cptr)
799 printk("%5d ", p->p_cptr->pid);
800 else
801 printk(" ");
802 if (p->p_ysptr)
803 printk("%7d", p->p_ysptr->pid);
804 else
805 printk(" ");
806 if (p->p_osptr)
807 printk(" %5d\n", p->p_osptr->pid);
808 else
809 printk("\n");
810 }
811
812 void show_state(void)
813 {
814 int i;
815
816 #if ((~0UL) == 0xffffffff)
817 printk("\n"
818 " free sibling\n");
819 printk(" task PC stack pid father child younger older\n");
820 #else
821 printk("\n"
822 " free sibling\n");
823 printk(" task PC stack pid father child younger older\n");
824 #endif
825 for (i=0 ; i<NR_TASKS ; i++)
826 if (task[i])
827 show_task(i,task[i]);
828 }
829
830 void sched_init(void)
831 {
832 bh_base[TIMER_BH].routine = timer_bh;
833 bh_base[TQUEUE_BH].routine = tqueue_bh;
834 bh_base[IMMEDIATE_BH].routine = immediate_bh;
835 if (request_irq(TIMER_IRQ, do_timer, 0, "timer") != 0)
836 panic("Could not allocate timer IRQ!");
837 enable_bh(TIMER_BH);
838 enable_bh(TQUEUE_BH);
839 enable_bh(IMMEDIATE_BH);
840 }