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