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