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