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