root/kernel/sched.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. math_state_restore
  2. math_emulate
  3. schedule
  4. sys_pause
  5. wake_up
  6. wake_up_interruptible
  7. __sleep_on
  8. interruptible_sleep_on
  9. sleep_on
  10. add_timer
  11. del_timer
  12. count_active_tasks
  13. calc_load
  14. second_overflow
  15. timer_bh
  16. do_timer
  17. sys_alarm
  18. sys_getpid
  19. sys_getppid
  20. sys_getuid
  21. sys_geteuid
  22. sys_getgid
  23. sys_getegid
  24. sys_nice
  25. show_task
  26. show_state
  27. sched_init

   1 /*
   2  *  linux/kernel/sched.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 /*
   8  * 'sched.c' is the main kernel file. It contains scheduling primitives
   9  * (sleep_on, wakeup, schedule etc) as well as a number of simple system
  10  * call functions (type getpid(), which just extracts a field from
  11  * current-task
  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  * kernel variables
  38  */
  39 long tick = 1000000 / HZ;               /* timer interrupt period */
  40 volatile struct timeval xtime;          /* The current time */
  41 int tickadj = 500/HZ;                   /* microsecs */
  42 
  43 /*
  44  * phase-lock loop variables
  45  */
  46 int time_status = TIME_BAD;     /* clock synchronization status */
  47 long time_offset = 0;           /* time adjustment (us) */
  48 long time_constant = 0;         /* pll time constant */
  49 long time_tolerance = MAXFREQ;  /* frequency tolerance (ppm) */
  50 long time_precision = 1;        /* clock precision (us) */
  51 long time_maxerror = 0x70000000;/* maximum error */
  52 long time_esterror = 0x70000000;/* estimated error */
  53 long time_phase = 0;            /* phase offset (scaled us) */
  54 long time_freq = 0;             /* frequency offset (scaled ppm) */
  55 long time_adj = 0;              /* tick adjust (scaled 1 / HZ) */
  56 long time_reftime = 0;          /* time at last adjustment (s) */
  57 
  58 long time_adjust = 0;
  59 
  60 int need_resched = 0;
  61 
  62 /*
  63  * Tell us the machine setup..
  64  */
  65 int hard_math = 0;              /* set by boot/head.S */
  66 int x86 = 0;                    /* set by boot/head.S to 3 or 4 */
  67 int ignore_irq13 = 0;           /* set if exception 16 works */
  68 int wp_works_ok = 0;            /* set if paging hardware honours WP */ 
  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  * int 0x80 entry points.. Moved away from the header file, as
 100  * iBCS2 may also want to use the '<linux/sys.h>' headers..
 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 /* So we don't have to do any more manual updating.... */
 132 int NR_syscalls = sizeof(sys_call_table)/sizeof(fn_ptr);
 133 
 134 #ifdef __cplusplus
 135 }
 136 #endif
 137 
 138 /*
 139  *  'math_state_restore()' saves the current math information in the
 140  * old math state array, and gets the new ones from the current task
 141  *
 142  * Careful.. There are problems with IBM-designed IRQ13 behaviour.
 143  * Don't touch unless you *really* know how it works.
 144  */
 145 asmlinkage void math_state_restore(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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 /* CONFIG_MATH_EMULATION */
 177 
 178 static unsigned long itimer_ticks = 0;
 179 static unsigned long itimer_next = ~0;
 180 static unsigned long lost_ticks = 0;
 181 
 182 /*
 183  *  'schedule()' is the scheduler function. It's a very simple and nice
 184  * scheduler: it's not perfect, but certainly works for most things.
 185  * The one thing you might take a look at is the signal-handler code here.
 186  *
 187  *   NOTE!!  Task 0 is the 'idle' task, which gets called when no other
 188  * tasks can run. It can not be killed, and it cannot sleep. The 'state'
 189  * information in task[0] is never used.
 190  *
 191  * The "confuse_gcc" goto is used only to get better assembly code..
 192  * Djikstra probably hates me.
 193  */
 194 asmlinkage void schedule(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 195 {
 196         int c;
 197         struct task_struct * p;
 198         struct task_struct * next;
 199         unsigned long ticks;
 200 
 201 /* check alarm, wake up any interruptible tasks that have got a signal */
 202 
 203         cli();
 204         ticks = itimer_ticks;
 205         itimer_ticks = 0;
 206         itimer_next = ~0;
 207         sti();
 208         need_resched = 0;
 209         p = &init_task;
 210         for (;;) {
 211                 if ((p = p->next_task) == &init_task)
 212                         goto confuse_gcc1;
 213                 if (ticks && p->it_real_value) {
 214                         if (p->it_real_value <= ticks) {
 215                                 send_sig(SIGALRM, p, 1);
 216                                 if (!p->it_real_incr) {
 217                                         p->it_real_value = 0;
 218                                         goto end_itimer;
 219                                 }
 220                                 do {
 221                                         p->it_real_value += p->it_real_incr;
 222                                 } while (p->it_real_value <= ticks);
 223                         }
 224                         p->it_real_value -= ticks;
 225                         if (p->it_real_value < itimer_next)
 226                                 itimer_next = p->it_real_value;
 227                 }
 228 end_itimer:
 229                 if (p->state != TASK_INTERRUPTIBLE)
 230                         continue;
 231                 if (p->signal & ~p->blocked) {
 232                         p->state = TASK_RUNNING;
 233                         continue;
 234                 }
 235                 if (p->timeout && p->timeout <= jiffies) {
 236                         p->timeout = 0;
 237                         p->state = TASK_RUNNING;
 238                 }
 239         }
 240 confuse_gcc1:
 241 
 242 /* this is the scheduler proper: */
 243 #if 0
 244         /* give processes that go to sleep a bit higher priority.. */
 245         /* This depends on the values for TASK_XXX */
 246         /* This gives smoother scheduling for some things, but */
 247         /* can be very unfair under some circumstances, so.. */
 248         if (TASK_UNINTERRUPTIBLE >= (unsigned) current->state &&
 249             current->counter < current->priority*2) {
 250                 ++current->counter;
 251         }
 252 #endif
 253         c = -1;
 254         next = p = &init_task;
 255         for (;;) {
 256                 if ((p = p->next_task) == &init_task)
 257                         goto confuse_gcc2;
 258                 if (p->state == TASK_RUNNING && p->counter > c)
 259                         c = p->counter, next = p;
 260         }
 261 confuse_gcc2:
 262         if (!c) {
 263                 for_each_task(p)
 264                         p->counter = (p->counter >> 1) + p->priority;
 265         }
 266         switch_to(next);
 267         /* Now maybe reload the debug registers */
 268         if(current->debugreg[7]){
 269                 loaddebug(0);
 270                 loaddebug(1);
 271                 loaddebug(2);
 272                 loaddebug(3);
 273                 loaddebug(6);
 274         };
 275 }
 276 
 277 asmlinkage int sys_pause(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 278 {
 279         current->state = TASK_INTERRUPTIBLE;
 280         schedule();
 281         return -ERESTARTNOHAND;
 282 }
 283 
 284 /*
 285  * wake_up doesn't wake up stopped processes - they have to be awakened
 286  * with signals or similar.
 287  *
 288  * Note that this doesn't need cli-sti pairs: interrupts may not change
 289  * the wait-queue structures directly, but only call wake_up() to wake
 290  * a process. The process itself must remove the queue once it has woken.
 291  */
 292 void wake_up(struct wait_queue **q)
     /* [previous][next][first][last][top][bottom][index][help] */
 293 {
 294         struct wait_queue *tmp;
 295         struct task_struct * p;
 296 
 297         if (!q || !(tmp = *q))
 298                 return;
 299         do {
 300                 if ((p = tmp->task) != NULL) {
 301                         if ((p->state == TASK_UNINTERRUPTIBLE) ||
 302                             (p->state == TASK_INTERRUPTIBLE)) {
 303                                 p->state = TASK_RUNNING;
 304                                 if (p->counter > current->counter)
 305                                         need_resched = 1;
 306                         }
 307                 }
 308                 if (!tmp->next) {
 309                         printk("wait_queue is bad (eip = %08lx)\n",((unsigned long *) q)[-1]);
 310                         printk("        q = %p\n",q);
 311                         printk("       *q = %p\n",*q);
 312                         printk("      tmp = %p\n",tmp);
 313                         break;
 314                 }
 315                 tmp = tmp->next;
 316         } while (tmp != *q);
 317 }
 318 
 319 void wake_up_interruptible(struct wait_queue **q)
     /* [previous][next][first][last][top][bottom][index][help] */
 320 {
 321         struct wait_queue *tmp;
 322         struct task_struct * p;
 323 
 324         if (!q || !(tmp = *q))
 325                 return;
 326         do {
 327                 if ((p = tmp->task) != NULL) {
 328                         if (p->state == TASK_INTERRUPTIBLE) {
 329                                 p->state = TASK_RUNNING;
 330                                 if (p->counter > current->counter)
 331                                         need_resched = 1;
 332                         }
 333                 }
 334                 if (!tmp->next) {
 335                         printk("wait_queue is bad (eip = %08lx)\n",((unsigned long *) q)[-1]);
 336                         printk("        q = %p\n",q);
 337                         printk("       *q = %p\n",*q);
 338                         printk("      tmp = %p\n",tmp);
 339                         break;
 340                 }
 341                 tmp = tmp->next;
 342         } while (tmp != *q);
 343 }
 344 
 345 static inline void __sleep_on(struct wait_queue **p, int state)
     /* [previous][next][first][last][top][bottom][index][help] */
 346 {
 347         unsigned long flags;
 348         struct wait_queue wait = { current, NULL };
 349 
 350         if (!p)
 351                 return;
 352         if (current == task[0])
 353                 panic("task[0] trying to sleep");
 354         current->state = state;
 355         add_wait_queue(p, &wait);
 356         save_flags(flags);
 357         sti();
 358         schedule();
 359         remove_wait_queue(p, &wait);
 360         restore_flags(flags);
 361 }
 362 
 363 void interruptible_sleep_on(struct wait_queue **p)
     /* [previous][next][first][last][top][bottom][index][help] */
 364 {
 365         __sleep_on(p,TASK_INTERRUPTIBLE);
 366 }
 367 
 368 void sleep_on(struct wait_queue **p)
     /* [previous][next][first][last][top][bottom][index][help] */
 369 {
 370         __sleep_on(p,TASK_UNINTERRUPTIBLE);
 371 }
 372 
 373 static struct timer_list * next_timer = NULL;
 374 
 375 void add_timer(struct timer_list * timer)
     /* [previous][next][first][last][top][bottom][index][help] */
 376 {
 377         unsigned long flags;
 378         struct timer_list ** p;
 379 
 380         if (!timer)
 381                 return;
 382         timer->next = NULL;
 383         p = &next_timer;
 384         save_flags(flags);
 385         cli();
 386         while (*p) {
 387                 if ((*p)->expires > timer->expires) {
 388                         (*p)->expires -= timer->expires;
 389                         timer->next = *p;
 390                         break;
 391                 }
 392                 timer->expires -= (*p)->expires;
 393                 p = &(*p)->next;
 394         }
 395         *p = timer;
 396         restore_flags(flags);
 397 }
 398 
 399 int del_timer(struct timer_list * timer)
     /* [previous][next][first][last][top][bottom][index][help] */
 400 {
 401         unsigned long flags;
 402         unsigned long expires = 0;
 403         struct timer_list **p;
 404 
 405         p = &next_timer;
 406         save_flags(flags);
 407         cli();
 408         while (*p) {
 409                 if (*p == timer) {
 410                         if ((*p = timer->next) != NULL)
 411                                 (*p)->expires += timer->expires;
 412                         timer->expires += expires;
 413                         restore_flags(flags);
 414                         return 1;
 415                 }
 416                 expires += (*p)->expires;
 417                 p = &(*p)->next;
 418         }
 419         restore_flags(flags);
 420         return 0;
 421 }
 422 
 423 unsigned long timer_active = 0;
 424 struct timer_struct timer_table[32];
 425 
 426 /*
 427  * Hmm.. Changed this, as the GNU make sources (load.c) seems to
 428  * imply that avenrun[] is the standard name for this kind of thing.
 429  * Nothing else seems to be standardized: the fractional size etc
 430  * all seem to differ on different machines.
 431  */
 432 unsigned long avenrun[3] = { 0,0,0 };
 433 
 434 /*
 435  * Nr of active tasks - counted in fixed-point numbers
 436  */
 437 static unsigned long count_active_tasks(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 438 {
 439         struct task_struct **p;
 440         unsigned long nr = 0;
 441 
 442         for(p = &LAST_TASK; p > &FIRST_TASK; --p)
 443                 if (*p && ((*p)->state == TASK_RUNNING ||
 444                            (*p)->state == TASK_UNINTERRUPTIBLE ||
 445                            (*p)->state == TASK_SWAPPING))
 446                         nr += FIXED_1;
 447         return nr;
 448 }
 449 
 450 static inline void calc_load(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 451 {
 452         unsigned long active_tasks; /* fixed-point */
 453         static int count = LOAD_FREQ;
 454 
 455         if (count-- > 0)
 456                 return;
 457         count = LOAD_FREQ;
 458         active_tasks = count_active_tasks();
 459         CALC_LOAD(avenrun[0], EXP_1, active_tasks);
 460         CALC_LOAD(avenrun[1], EXP_5, active_tasks);
 461         CALC_LOAD(avenrun[2], EXP_15, active_tasks);
 462 }
 463 
 464 /*
 465  * this routine handles the overflow of the microsecond field
 466  *
 467  * The tricky bits of code to handle the accurate clock support
 468  * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
 469  * They were originally developed for SUN and DEC kernels.
 470  * All the kudos should go to Dave for this stuff.
 471  *
 472  * These were ported to Linux by Philip Gladstone.
 473  */
 474 static void second_overflow(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 475 {
 476         long ltemp;
 477         /* last time the cmos clock got updated */
 478         static long last_rtc_update=0;
 479         extern int set_rtc_mmss(unsigned long);
 480 
 481         /* Bump the maxerror field */
 482         time_maxerror = (0x70000000-time_maxerror < time_tolerance) ?
 483           0x70000000 : (time_maxerror + time_tolerance);
 484 
 485         /* Run the PLL */
 486         if (time_offset < 0) {
 487                 ltemp = (-(time_offset+1) >> (SHIFT_KG + time_constant)) + 1;
 488                 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
 489                 time_offset += (time_adj * HZ) >> (SHIFT_SCALE - SHIFT_UPDATE);
 490                 time_adj = - time_adj;
 491         } else if (time_offset > 0) {
 492                 ltemp = ((time_offset-1) >> (SHIFT_KG + time_constant)) + 1;
 493                 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
 494                 time_offset -= (time_adj * HZ) >> (SHIFT_SCALE - SHIFT_UPDATE);
 495         } else {
 496                 time_adj = 0;
 497         }
 498 
 499         time_adj += (time_freq >> (SHIFT_KF + SHIFT_HZ - SHIFT_SCALE))
 500             + FINETUNE;
 501 
 502         /* Handle the leap second stuff */
 503         switch (time_status) {
 504                 case TIME_INS:
 505                 /* ugly divide should be replaced */
 506                 if (xtime.tv_sec % 86400 == 0) {
 507                         xtime.tv_sec--; /* !! */
 508                         time_status = TIME_OOP;
 509                         printk("Clock: inserting leap second 23:59:60 GMT\n");
 510                 }
 511                 break;
 512 
 513                 case TIME_DEL:
 514                 /* ugly divide should be replaced */
 515                 if (xtime.tv_sec % 86400 == 86399) {
 516                         xtime.tv_sec++;
 517                         time_status = TIME_OK;
 518                         printk("Clock: deleting leap second 23:59:59 GMT\n");
 519                 }
 520                 break;
 521 
 522                 case TIME_OOP:
 523                 time_status = TIME_OK;
 524                 break;
 525         }
 526         if (xtime.tv_sec > last_rtc_update + 660)
 527           if (set_rtc_mmss(xtime.tv_sec) == 0)
 528             last_rtc_update = xtime.tv_sec;
 529 }
 530 
 531 /*
 532  * disregard lost ticks for now.. We don't care enough.
 533  */
 534 static void timer_bh(void * unused)
     /* [previous][next][first][last][top][bottom][index][help] */
 535 {
 536         cli();
 537         while (next_timer && next_timer->expires == 0) {
 538                 void (*fn)(unsigned long) = next_timer->function;
 539                 unsigned long data = next_timer->data;
 540                 next_timer = next_timer->next;
 541                 sti();
 542                 fn(data);
 543                 cli();
 544         }
 545         sti();
 546 }
 547 
 548 /*
 549  * The int argument is really a (struct pt_regs *), in case the
 550  * interrupt wants to know from where it was called. The timer
 551  * irq uses this to decide if it should update the user or system
 552  * times.
 553  */
 554 static void do_timer(struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 555 {
 556         unsigned long mask;
 557         struct timer_struct *tp = timer_table+0;
 558 
 559         long ltemp;
 560 
 561         /* Advance the phase, once it gets to one microsecond, then
 562          * advance the tick more.
 563          */
 564         time_phase += time_adj;
 565         if (time_phase < -FINEUSEC) {
 566                 ltemp = -time_phase >> SHIFT_SCALE;
 567                 time_phase += ltemp << SHIFT_SCALE;
 568                 xtime.tv_usec += tick - ltemp;
 569         }
 570         else if (time_phase > FINEUSEC) {
 571                 ltemp = time_phase >> SHIFT_SCALE;
 572                 time_phase -= ltemp << SHIFT_SCALE;
 573                 xtime.tv_usec += tick + ltemp;
 574         } else
 575                 xtime.tv_usec += tick;
 576 
 577         if (time_adjust)
 578         {
 579             /* We are doing an adjtime thing. 
 580              */
 581 
 582             /* Limit the amount of the step for *next* tick to be
 583              * in the range -tickadj .. +tickadj
 584              */
 585              if (time_adjust > tickadj)
 586                ltemp = tickadj;
 587              else if (time_adjust < -tickadj)
 588                ltemp = -tickadj;
 589              else
 590                ltemp = time_adjust;
 591              
 592             /* Reduce the amount of time left by this step */
 593             time_adjust -= ltemp;
 594 
 595             /* Modify the value of the tick for next time.
 596              * Note that a positive delta means we want the clock
 597              * to run fast. This means that the tick should be bigger
 598              */
 599             tick = 1000000/HZ + ltemp;
 600         }
 601         else
 602             tick = 1000000/HZ;
 603 
 604         if (xtime.tv_usec >= 1000000) {
 605             xtime.tv_usec -= 1000000;
 606             xtime.tv_sec++;
 607             second_overflow();
 608         }
 609 
 610         jiffies++;
 611         calc_load();
 612         if ((VM_MASK & regs->eflags) || (3 & regs->cs)) {
 613                 current->utime++;
 614                 /* Update ITIMER_VIRT for current task if not in a system call */
 615                 if (current->it_virt_value && !(--current->it_virt_value)) {
 616                         current->it_virt_value = current->it_virt_incr;
 617                         send_sig(SIGVTALRM,current,1);
 618                 }
 619         } else {
 620                 current->stime++;
 621 #ifdef CONFIG_PROFILE
 622                 if (prof_buffer && current != task[0]) {
 623                         unsigned long eip = regs->eip;
 624                         eip >>= 2;
 625                         if (eip < prof_len)
 626                                 prof_buffer[eip]++;
 627                 }
 628 #endif
 629         }
 630         if (current == task[0] || (--current->counter)<=0) {
 631                 current->counter=0;
 632                 need_resched = 1;
 633         }
 634         /* Update ITIMER_PROF for the current task */
 635         if (current->it_prof_value && !(--current->it_prof_value)) {
 636                 current->it_prof_value = current->it_prof_incr;
 637                 send_sig(SIGPROF,current,1);
 638         }
 639         for (mask = 1 ; mask ; tp++,mask += mask) {
 640                 if (mask > timer_active)
 641                         break;
 642                 if (!(mask & timer_active))
 643                         continue;
 644                 if (tp->expires > jiffies)
 645                         continue;
 646                 timer_active &= ~mask;
 647                 tp->fn();
 648                 sti();
 649         }
 650         cli();
 651         itimer_ticks++;
 652         if (itimer_ticks > itimer_next)
 653                 need_resched = 1;
 654         if (next_timer) {
 655                 if (next_timer->expires) {
 656                         next_timer->expires--;
 657                         if (!next_timer->expires)
 658                                 mark_bh(TIMER_BH);
 659                 } else {
 660                         lost_ticks++;
 661                         mark_bh(TIMER_BH);
 662                 }
 663         }
 664         sti();
 665 }
 666 
 667 asmlinkage int sys_alarm(long seconds)
     /* [previous][next][first][last][top][bottom][index][help] */
 668 {
 669         struct itimerval it_new, it_old;
 670 
 671         it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
 672         it_new.it_value.tv_sec = seconds;
 673         it_new.it_value.tv_usec = 0;
 674         _setitimer(ITIMER_REAL, &it_new, &it_old);
 675         return(it_old.it_value.tv_sec + (it_old.it_value.tv_usec / 1000000));
 676 }
 677 
 678 asmlinkage int sys_getpid(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 679 {
 680         return current->pid;
 681 }
 682 
 683 asmlinkage int sys_getppid(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 684 {
 685         return current->p_opptr->pid;
 686 }
 687 
 688 asmlinkage int sys_getuid(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 689 {
 690         return current->uid;
 691 }
 692 
 693 asmlinkage int sys_geteuid(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 694 {
 695         return current->euid;
 696 }
 697 
 698 asmlinkage int sys_getgid(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 699 {
 700         return current->gid;
 701 }
 702 
 703 asmlinkage int sys_getegid(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 704 {
 705         return current->egid;
 706 }
 707 
 708 asmlinkage int sys_nice(long increment)
     /* [previous][next][first][last][top][bottom][index][help] */
 709 {
 710         int newprio;
 711 
 712         if (increment < 0 && !suser())
 713                 return -EPERM;
 714         newprio = current->priority - increment;
 715         if (newprio < 1)
 716                 newprio = 1;
 717         if (newprio > 35)
 718                 newprio = 35;
 719         current->priority = newprio;
 720         return 0;
 721 }
 722 
 723 static void show_task(int nr,struct task_struct * p)
     /* [previous][next][first][last][top][bottom][index][help] */
 724 {
 725         static char * stat_nam[] = { "R", "S", "D", "Z", "T", "W" };
 726 
 727         printk("%-8s %3d ", p->comm, (p == current) ? -nr : nr);
 728         if (((unsigned) p->state) < sizeof(stat_nam)/sizeof(char *))
 729                 printk(stat_nam[p->state]);
 730         else
 731                 printk(" ");
 732         /* this prints bogus values for the current process */
 733         printk(" %08lX ", ((unsigned long *)p->tss.esp)[2]);
 734         printk("%5lu %5d %6d ",
 735                 p->tss.esp - p->kernel_stack_page, p->pid, p->p_pptr->pid);
 736         if (p->p_cptr)
 737                 printk("%5d ", p->p_cptr->pid);
 738         else
 739                 printk("      ");
 740         if (p->p_ysptr)
 741                 printk("%7d", p->p_ysptr->pid);
 742         else
 743                 printk("       ");
 744         if (p->p_osptr)
 745                 printk(" %5d\n", p->p_osptr->pid);
 746         else
 747                 printk("\n");
 748 }
 749 
 750 void show_state(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 751 {
 752         int i;
 753 
 754         printk("                         free                        sibling\n");
 755         printk("  task             PC    stack   pid father child younger older\n");
 756         for (i=0 ; i<NR_TASKS ; i++)
 757                 if (task[i])
 758                         show_task(i,task[i]);
 759 }
 760 
 761 void sched_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 762 {
 763         int i;
 764         struct desc_struct * p;
 765 
 766         bh_base[TIMER_BH].routine = timer_bh;
 767         if (sizeof(struct sigaction) != 16)
 768                 panic("Struct sigaction MUST be 16 bytes");
 769         set_tss_desc(gdt+FIRST_TSS_ENTRY,&init_task.tss);
 770         set_ldt_desc(gdt+FIRST_LDT_ENTRY,&default_ldt,1);
 771         set_system_gate(0x80,&system_call);
 772         p = gdt+2+FIRST_TSS_ENTRY;
 773         for(i=1 ; i<NR_TASKS ; i++) {
 774                 task[i] = NULL;
 775                 p->a=p->b=0;
 776                 p++;
 777                 p->a=p->b=0;
 778                 p++;
 779         }
 780 /* Clear NT, so that we won't have troubles with that later on */
 781         __asm__("pushfl ; andl $0xffffbfff,(%esp) ; popfl");
 782         load_TR(0);
 783         load_ldt(0);
 784         outb_p(0x34,0x43);              /* binary, mode 2, LSB/MSB, ch 0 */
 785         outb_p(LATCH & 0xff , 0x40);    /* LSB */
 786         outb(LATCH >> 8 , 0x40);        /* MSB */
 787         if (request_irq(TIMER_IRQ,(void (*)(int)) do_timer)!=0)
 788                 panic("Could not allocate timer IRQ!");
 789 }

/* [previous][next][first][last][top][bottom][index][help] */