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