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