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