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