root/kernel/sched.c

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

DEFINITIONS

This source file includes following definitions.
  1. add_to_runqueue
  2. del_from_runqueue
  3. wake_up_process
  4. process_timeout
  5. schedule
  6. sys_pause
  7. wake_up
  8. wake_up_interruptible
  9. __down
  10. __sleep_on
  11. interruptible_sleep_on
  12. sleep_on
  13. add_timer
  14. del_timer
  15. count_active_tasks
  16. calc_load
  17. second_overflow
  18. timer_bh
  19. tqueue_bh
  20. immediate_bh
  21. do_timer
  22. sys_alarm
  23. sys_getpid
  24. sys_getppid
  25. sys_getuid
  26. sys_geteuid
  27. sys_getgid
  28. sys_getegid
  29. sys_nice
  30. show_task
  31. show_state
  32. 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/kernel_stat.h>
  20 #include <linux/fdreg.h>
  21 #include <linux/errno.h>
  22 #include <linux/time.h>
  23 #include <linux/ptrace.h>
  24 #include <linux/delay.h>
  25 #include <linux/interrupt.h>
  26 #include <linux/tqueue.h>
  27 #include <linux/resource.h>
  28 #include <linux/mm.h>
  29 
  30 #include <asm/system.h>
  31 #include <asm/io.h>
  32 #include <asm/segment.h>
  33 #include <asm/pgtable.h>
  34 
  35 #define TIMER_IRQ 0
  36 
  37 #include <linux/timex.h>
  38 
  39 /*
  40  * kernel variables
  41  */
  42 long tick = 1000000 / HZ;               /* timer interrupt period */
  43 volatile struct timeval xtime;          /* The current time */
  44 int tickadj = 500/HZ;                   /* microsecs */
  45 
  46 DECLARE_TASK_QUEUE(tq_timer);
  47 DECLARE_TASK_QUEUE(tq_immediate);
  48 DECLARE_TASK_QUEUE(tq_scheduler);
  49 
  50 /*
  51  * phase-lock loop variables
  52  */
  53 int time_status = TIME_BAD;     /* clock synchronization status */
  54 long time_offset = 0;           /* time adjustment (us) */
  55 long time_constant = 0;         /* pll time constant */
  56 long time_tolerance = MAXFREQ;  /* frequency tolerance (ppm) */
  57 long time_precision = 1;        /* clock precision (us) */
  58 long time_maxerror = 0x70000000;/* maximum error */
  59 long time_esterror = 0x70000000;/* estimated error */
  60 long time_phase = 0;            /* phase offset (scaled us) */
  61 long time_freq = 0;             /* frequency offset (scaled ppm) */
  62 long time_adj = 0;              /* tick adjust (scaled 1 / HZ) */
  63 long time_reftime = 0;          /* time at last adjustment (s) */
  64 
  65 long time_adjust = 0;
  66 long time_adjust_step = 0;
  67 
  68 int need_resched = 0;
  69 unsigned long event = 0;
  70 
  71 extern int _setitimer(int, struct itimerval *, struct itimerval *);
  72 unsigned long * prof_buffer = NULL;
  73 unsigned long prof_len = 0;
  74 
  75 #define _S(nr) (1<<((nr)-1))
  76 
  77 extern void mem_use(void);
  78 
  79 extern int timer_interrupt(void);
  80  
  81 static unsigned long init_kernel_stack[1024] = { STACK_MAGIC, };
  82 unsigned long init_user_stack[1024] = { STACK_MAGIC, };
  83 static struct vm_area_struct init_mmap = INIT_MMAP;
  84 struct task_struct init_task = INIT_TASK;
  85 
  86 unsigned long volatile jiffies=0;
  87 
  88 struct task_struct *current = &init_task;
  89 struct task_struct *last_task_used_math = NULL;
  90 
  91 struct task_struct * task[NR_TASKS] = {&init_task, };
  92 
  93 struct kernel_stat kstat = { 0 };
  94 
  95 static inline void add_to_runqueue(struct task_struct * p)
     /* [previous][next][first][last][top][bottom][index][help] */
  96 {
  97 #if 1   /* sanity tests */
  98         if (p->next_run || p->prev_run) {
  99                 printk("task already on run-queue\n");
 100                 return;
 101         }
 102 #endif
 103         if (p->counter > current->counter + 3)
 104                 need_resched = 1;
 105         nr_running++;
 106         (p->next_run = init_task.next_run)->prev_run = p;
 107         p->prev_run = &init_task;
 108         init_task.next_run = p;
 109 }
 110 
 111 static inline void del_from_runqueue(struct task_struct * p)
     /* [previous][next][first][last][top][bottom][index][help] */
 112 {
 113         struct task_struct *next = p->next_run;
 114         struct task_struct *prev = p->prev_run;
 115 
 116 #if 1   /* sanity tests */
 117         if (!next || !prev) {
 118                 printk("task not on run-queue\n");
 119                 return;
 120         }
 121 #endif
 122         if (p == &init_task) {
 123                 static int nr = 0;
 124                 if (nr < 5) {
 125                         nr++;
 126                         printk("idle task may not sleep\n");
 127                 }
 128                 return;
 129         }
 130         nr_running--;
 131         next->prev_run = prev;
 132         prev->next_run = next;
 133         p->next_run = NULL;
 134         p->prev_run = NULL;
 135 }
 136 
 137 /*
 138  * Wake up a process. Put it on the run-queue if it's not
 139  * already there.  The "current" process is always on the
 140  * run-queue (except when the actual re-schedule is in
 141  * progress), and as such you're allowed to do the simpler
 142  * "current->state = TASK_RUNNING" to mark yourself runnable
 143  * without the overhead of this.
 144  */
 145 inline void wake_up_process(struct task_struct * p)
     /* [previous][next][first][last][top][bottom][index][help] */
 146 {
 147         unsigned long flags;
 148 
 149         save_flags(flags);
 150         cli();
 151         p->state = TASK_RUNNING;
 152         if (!p->next_run)
 153                 add_to_runqueue(p);
 154         restore_flags(flags);
 155 }
 156 
 157 static void process_timeout(unsigned long __data)
     /* [previous][next][first][last][top][bottom][index][help] */
 158 {
 159         struct task_struct * p = (struct task_struct *) __data;
 160 
 161         p->timeout = 0;
 162         wake_up_process(p);
 163 }
 164 
 165 /*
 166  *  'schedule()' is the scheduler function. It's a very simple and nice
 167  * scheduler: it's not perfect, but certainly works for most things.
 168  *
 169  * The goto is "interesting".
 170  *
 171  *   NOTE!!  Task 0 is the 'idle' task, which gets called when no other
 172  * tasks can run. It can not be killed, and it cannot sleep. The 'state'
 173  * information in task[0] is never used.
 174  */
 175 asmlinkage void schedule(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 176 {
 177         int c;
 178         struct task_struct * p;
 179         struct task_struct * next;
 180         unsigned long timeout = 0;
 181 
 182 /* check alarm, wake up any interruptible tasks that have got a signal */
 183 
 184         if (intr_count) {
 185                 printk("Aiee: scheduling in interrupt\n");
 186                 return;
 187         }
 188         run_task_queue(&tq_scheduler);
 189 
 190         need_resched = 0;
 191         cli();
 192         switch (current->state) {
 193                 case TASK_INTERRUPTIBLE:
 194                         if (current->signal & ~current->blocked)
 195                                 goto makerunnable;
 196                         timeout = current->timeout;
 197                         if (timeout && (timeout <= jiffies)) {
 198                                 current->timeout = 0;
 199                                 timeout = 0;
 200                 makerunnable:
 201                                 current->state = TASK_RUNNING;
 202                                 break;
 203                         }
 204                 default:
 205                         del_from_runqueue(current);
 206                 case TASK_RUNNING:
 207         }
 208         p = init_task.next_run;
 209         sti();
 210 
 211 /*
 212  * Note! there may appear new tasks on the run-queue during this, as
 213  * interrupts are enabled. However, they will be put on front of the
 214  * list, so our list starting at "p" is essentially fixed.
 215  */
 216 /* this is the scheduler proper: */
 217         c = -1000;
 218         next = &init_task;
 219         while (p != &init_task) {
 220                 if (p->counter > c)
 221                         c = p->counter, next = p;
 222                 p = p->next_run;
 223         }
 224 
 225         /* if all runnable processes have "counter == 0", re-calculate counters */
 226         if (!c) {
 227                 for_each_task(p)
 228                         p->counter = (p->counter >> 1) + p->priority;
 229         }
 230         if (current != next) {
 231                 struct timer_list timer;
 232 
 233                 kstat.context_swtch++;
 234                 if (timeout) {
 235                         init_timer(&timer);
 236                         timer.expires = timeout;
 237                         timer.data = (unsigned long) current;
 238                         timer.function = process_timeout;
 239                         add_timer(&timer);
 240                 }
 241                 switch_to(next);
 242                 if (timeout)
 243                         del_timer(&timer);
 244         }
 245 }
 246 
 247 asmlinkage int sys_pause(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 248 {
 249         current->state = TASK_INTERRUPTIBLE;
 250         schedule();
 251         return -ERESTARTNOHAND;
 252 }
 253 
 254 /*
 255  * wake_up doesn't wake up stopped processes - they have to be awakened
 256  * with signals or similar.
 257  *
 258  * Note that this doesn't need cli-sti pairs: interrupts may not change
 259  * the wait-queue structures directly, but only call wake_up() to wake
 260  * a process. The process itself must remove the queue once it has woken.
 261  */
 262 void wake_up(struct wait_queue **q)
     /* [previous][next][first][last][top][bottom][index][help] */
 263 {
 264         struct wait_queue *tmp;
 265         struct task_struct * p;
 266 
 267         if (!q || !(tmp = *q))
 268                 return;
 269         do {
 270                 if ((p = tmp->task) != NULL) {
 271                         if ((p->state == TASK_UNINTERRUPTIBLE) ||
 272                             (p->state == TASK_INTERRUPTIBLE))
 273                                 wake_up_process(p);
 274                 }
 275                 if (!tmp->next) {
 276                         printk("wait_queue is bad (eip = %p)\n",
 277                                 __builtin_return_address(0));
 278                         printk("        q = %p\n",q);
 279                         printk("       *q = %p\n",*q);
 280                         printk("      tmp = %p\n",tmp);
 281                         break;
 282                 }
 283                 tmp = tmp->next;
 284         } while (tmp != *q);
 285 }
 286 
 287 void wake_up_interruptible(struct wait_queue **q)
     /* [previous][next][first][last][top][bottom][index][help] */
 288 {
 289         struct wait_queue *tmp;
 290         struct task_struct * p;
 291 
 292         if (!q || !(tmp = *q))
 293                 return;
 294         do {
 295                 if ((p = tmp->task) != NULL) {
 296                         if (p->state == TASK_INTERRUPTIBLE)
 297                                 wake_up_process(p);
 298                 }
 299                 if (!tmp->next) {
 300                         printk("wait_queue is bad (eip = %p)\n",
 301                                 __builtin_return_address(0));
 302                         printk("        q = %p\n",q);
 303                         printk("       *q = %p\n",*q);
 304                         printk("      tmp = %p\n",tmp);
 305                         break;
 306                 }
 307                 tmp = tmp->next;
 308         } while (tmp != *q);
 309 }
 310 
 311 void __down(struct semaphore * sem)
     /* [previous][next][first][last][top][bottom][index][help] */
 312 {
 313         struct wait_queue wait = { current, NULL };
 314         add_wait_queue(&sem->wait, &wait);
 315         current->state = TASK_UNINTERRUPTIBLE;
 316         while (sem->count <= 0) {
 317                 schedule();
 318                 current->state = TASK_UNINTERRUPTIBLE;
 319         }
 320         current->state = TASK_RUNNING;
 321         remove_wait_queue(&sem->wait, &wait);
 322 }
 323 
 324 static inline void __sleep_on(struct wait_queue **p, int state)
     /* [previous][next][first][last][top][bottom][index][help] */
 325 {
 326         unsigned long flags;
 327         struct wait_queue wait = { current, NULL };
 328 
 329         if (!p)
 330                 return;
 331         if (current == task[0])
 332                 panic("task[0] trying to sleep");
 333         current->state = state;
 334         add_wait_queue(p, &wait);
 335         save_flags(flags);
 336         sti();
 337         schedule();
 338         remove_wait_queue(p, &wait);
 339         restore_flags(flags);
 340 }
 341 
 342 void interruptible_sleep_on(struct wait_queue **p)
     /* [previous][next][first][last][top][bottom][index][help] */
 343 {
 344         __sleep_on(p,TASK_INTERRUPTIBLE);
 345 }
 346 
 347 void sleep_on(struct wait_queue **p)
     /* [previous][next][first][last][top][bottom][index][help] */
 348 {
 349         __sleep_on(p,TASK_UNINTERRUPTIBLE);
 350 }
 351 
 352 /*
 353  * The head for the timer-list has a "expires" field of MAX_UINT,
 354  * and the sorting routine counts on this..
 355  */
 356 static struct timer_list timer_head = { &timer_head, &timer_head, ~0, 0, NULL };
 357 #define SLOW_BUT_DEBUGGING_TIMERS 1
 358 
 359 void add_timer(struct timer_list * timer)
     /* [previous][next][first][last][top][bottom][index][help] */
 360 {
 361         unsigned long flags;
 362         struct timer_list *p;
 363 
 364 #if SLOW_BUT_DEBUGGING_TIMERS
 365         if (timer->next || timer->prev) {
 366                 printk("add_timer() called with non-zero list from %p\n",
 367                         __builtin_return_address(0));
 368                 return;
 369         }
 370 #endif
 371         p = &timer_head;
 372         save_flags(flags);
 373         cli();
 374         do {
 375                 p = p->next;
 376         } while (timer->expires > p->expires);
 377         timer->next = p;
 378         timer->prev = p->prev;
 379         p->prev = timer;
 380         timer->prev->next = timer;
 381         restore_flags(flags);
 382 }
 383 
 384 int del_timer(struct timer_list * timer)
     /* [previous][next][first][last][top][bottom][index][help] */
 385 {
 386         unsigned long flags;
 387 #if SLOW_BUT_DEBUGGING_TIMERS
 388         struct timer_list * p;
 389 
 390         p = &timer_head;
 391         save_flags(flags);
 392         cli();
 393         while ((p = p->next) != &timer_head) {
 394                 if (p == timer) {
 395                         timer->next->prev = timer->prev;
 396                         timer->prev->next = timer->next;
 397                         timer->next = timer->prev = NULL;
 398                         restore_flags(flags);
 399                         return 1;
 400                 }
 401         }
 402         if (timer->next || timer->prev)
 403                 printk("del_timer() called from %p with timer not initialized\n",
 404                         __builtin_return_address(0));
 405         restore_flags(flags);
 406         return 0;
 407 #else   
 408         save_flags(flags);
 409         cli();
 410         if (timer->next) {
 411                 timer->next->prev = timer->prev;
 412                 timer->prev->next = timer->next;
 413                 timer->next = timer->prev = NULL;
 414                 restore_flags(flags);
 415                 return 1;
 416         }
 417         restore_flags(flags);
 418         return 0;
 419 #endif
 420 }
 421 
 422 unsigned long timer_active = 0;
 423 struct timer_struct timer_table[32];
 424 
 425 /*
 426  * Hmm.. Changed this, as the GNU make sources (load.c) seems to
 427  * imply that avenrun[] is the standard name for this kind of thing.
 428  * Nothing else seems to be standardized: the fractional size etc
 429  * all seem to differ on different machines.
 430  */
 431 unsigned long avenrun[3] = { 0,0,0 };
 432 
 433 /*
 434  * Nr of active tasks - counted in fixed-point numbers
 435  */
 436 static unsigned long count_active_tasks(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 437 {
 438         struct task_struct **p;
 439         unsigned long nr = 0;
 440 
 441         for(p = &LAST_TASK; p > &FIRST_TASK; --p)
 442                 if (*p && ((*p)->state == TASK_RUNNING ||
 443                            (*p)->state == TASK_UNINTERRUPTIBLE ||
 444                            (*p)->state == TASK_SWAPPING))
 445                         nr += FIXED_1;
 446         return nr;
 447 }
 448 
 449 static inline void calc_load(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 450 {
 451         unsigned long active_tasks; /* fixed-point */
 452         static int count = LOAD_FREQ;
 453 
 454         if (count-- > 0)
 455                 return;
 456         count = LOAD_FREQ;
 457         active_tasks = count_active_tasks();
 458         CALC_LOAD(avenrun[0], EXP_1, active_tasks);
 459         CALC_LOAD(avenrun[1], EXP_5, active_tasks);
 460         CALC_LOAD(avenrun[2], EXP_15, active_tasks);
 461 }
 462 
 463 /*
 464  * this routine handles the overflow of the microsecond field
 465  *
 466  * The tricky bits of code to handle the accurate clock support
 467  * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
 468  * They were originally developed for SUN and DEC kernels.
 469  * All the kudos should go to Dave for this stuff.
 470  *
 471  * These were ported to Linux by Philip Gladstone.
 472  */
 473 static void second_overflow(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 474 {
 475         long ltemp;
 476 
 477         /* Bump the maxerror field */
 478         time_maxerror = (0x70000000-time_maxerror < time_tolerance) ?
 479           0x70000000 : (time_maxerror + time_tolerance);
 480 
 481         /* Run the PLL */
 482         if (time_offset < 0) {
 483                 ltemp = (-(time_offset+1) >> (SHIFT_KG + time_constant)) + 1;
 484                 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
 485                 time_offset += (time_adj * HZ) >> (SHIFT_SCALE - SHIFT_UPDATE);
 486                 time_adj = - time_adj;
 487         } else if (time_offset > 0) {
 488                 ltemp = ((time_offset-1) >> (SHIFT_KG + time_constant)) + 1;
 489                 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
 490                 time_offset -= (time_adj * HZ) >> (SHIFT_SCALE - SHIFT_UPDATE);
 491         } else {
 492                 time_adj = 0;
 493         }
 494 
 495         time_adj += (time_freq >> (SHIFT_KF + SHIFT_HZ - SHIFT_SCALE))
 496             + FINETUNE;
 497 
 498         /* Handle the leap second stuff */
 499         switch (time_status) {
 500                 case TIME_INS:
 501                 /* ugly divide should be replaced */
 502                 if (xtime.tv_sec % 86400 == 0) {
 503                         xtime.tv_sec--; /* !! */
 504                         time_status = TIME_OOP;
 505                         printk("Clock: inserting leap second 23:59:60 UTC\n");
 506                 }
 507                 break;
 508 
 509                 case TIME_DEL:
 510                 /* ugly divide should be replaced */
 511                 if (xtime.tv_sec % 86400 == 86399) {
 512                         xtime.tv_sec++;
 513                         time_status = TIME_OK;
 514                         printk("Clock: deleting leap second 23:59:59 UTC\n");
 515                 }
 516                 break;
 517 
 518                 case TIME_OOP:
 519                 time_status = TIME_OK;
 520                 break;
 521         }
 522 }
 523 
 524 /*
 525  * disregard lost ticks for now.. We don't care enough.
 526  */
 527 static void timer_bh(void * unused)
     /* [previous][next][first][last][top][bottom][index][help] */
 528 {
 529         unsigned long mask;
 530         struct timer_struct *tp;
 531         struct timer_list * timer;
 532 
 533         cli();
 534         while ((timer = timer_head.next) != &timer_head && timer->expires < jiffies) {
 535                 void (*fn)(unsigned long) = timer->function;
 536                 unsigned long data = timer->data;
 537                 timer->next->prev = timer->prev;
 538                 timer->prev->next = timer->next;
 539                 timer->next = timer->prev = NULL;
 540                 sti();
 541                 fn(data);
 542                 cli();
 543         }
 544         sti();
 545         
 546         for (mask = 1, tp = timer_table+0 ; mask ; tp++,mask += mask) {
 547                 if (mask > timer_active)
 548                         break;
 549                 if (!(mask & timer_active))
 550                         continue;
 551                 if (tp->expires > jiffies)
 552                         continue;
 553                 timer_active &= ~mask;
 554                 tp->fn();
 555                 sti();
 556         }
 557 }
 558 
 559 void tqueue_bh(void * unused)
     /* [previous][next][first][last][top][bottom][index][help] */
 560 {
 561         run_task_queue(&tq_timer);
 562 }
 563 
 564 void immediate_bh(void * unused)
     /* [previous][next][first][last][top][bottom][index][help] */
 565 {
 566         run_task_queue(&tq_immediate);
 567 }
 568 
 569 /*
 570  * The int argument is really a (struct pt_regs *), in case the
 571  * interrupt wants to know from where it was called. The timer
 572  * irq uses this to decide if it should update the user or system
 573  * times.
 574  */
 575 static void do_timer(int irq, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 576 {
 577         unsigned long mask;
 578         struct timer_struct *tp;
 579         /* last time the cmos clock got updated */
 580         static long last_rtc_update=0;
 581         extern int set_rtc_mmss(unsigned long);
 582 
 583         long ltemp, psecs;
 584 
 585         /* Advance the phase, once it gets to one microsecond, then
 586          * advance the tick more.
 587          */
 588         time_phase += time_adj;
 589         if (time_phase < -FINEUSEC) {
 590                 ltemp = -time_phase >> SHIFT_SCALE;
 591                 time_phase += ltemp << SHIFT_SCALE;
 592                 xtime.tv_usec += tick + time_adjust_step - ltemp;
 593         }
 594         else if (time_phase > FINEUSEC) {
 595                 ltemp = time_phase >> SHIFT_SCALE;
 596                 time_phase -= ltemp << SHIFT_SCALE;
 597                 xtime.tv_usec += tick + time_adjust_step + ltemp;
 598         } else
 599                 xtime.tv_usec += tick + time_adjust_step;
 600 
 601         if (time_adjust)
 602         {
 603             /* We are doing an adjtime thing. 
 604              *
 605              * Modify the value of the tick for next time.
 606              * Note that a positive delta means we want the clock
 607              * to run fast. This means that the tick should be bigger
 608              *
 609              * Limit the amount of the step for *next* tick to be
 610              * in the range -tickadj .. +tickadj
 611              */
 612              if (time_adjust > tickadj)
 613                time_adjust_step = tickadj;
 614              else if (time_adjust < -tickadj)
 615                time_adjust_step = -tickadj;
 616              else
 617                time_adjust_step = time_adjust;
 618              
 619             /* Reduce by this step the amount of time left  */
 620             time_adjust -= time_adjust_step;
 621         }
 622         else
 623             time_adjust_step = 0;
 624 
 625         if (xtime.tv_usec >= 1000000) {
 626             xtime.tv_usec -= 1000000;
 627             xtime.tv_sec++;
 628             second_overflow();
 629         }
 630 
 631         /* If we have an externally synchronized Linux clock, then update
 632          * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
 633          * called as close as possible to 500 ms before the new second starts.
 634          */
 635         if (time_status != TIME_BAD && xtime.tv_sec > last_rtc_update + 660 &&
 636             xtime.tv_usec > 500000 - (tick >> 1) &&
 637             xtime.tv_usec < 500000 + (tick >> 1))
 638           if (set_rtc_mmss(xtime.tv_sec) == 0)
 639             last_rtc_update = xtime.tv_sec;
 640           else
 641             last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
 642 
 643         jiffies++;
 644         calc_load();
 645         if (user_mode(regs)) {
 646                 current->utime++;
 647                 if (current != task[0]) {
 648                         if (current->priority < 15)
 649                                 kstat.cpu_nice++;
 650                         else
 651                                 kstat.cpu_user++;
 652                 }
 653                 /* Update ITIMER_VIRT for current task if not in a system call */
 654                 if (current->it_virt_value && !(--current->it_virt_value)) {
 655                         current->it_virt_value = current->it_virt_incr;
 656                         send_sig(SIGVTALRM,current,1);
 657                 }
 658         } else {
 659                 current->stime++;
 660                 if(current != task[0])
 661                         kstat.cpu_system++;
 662 #ifdef CONFIG_PROFILE
 663                 if (prof_buffer && current != task[0]) {
 664                         extern int _stext;
 665                         unsigned long eip = regs->eip - (unsigned long) &_stext;
 666                         eip >>= CONFIG_PROFILE_SHIFT;
 667                         if (eip < prof_len)
 668                                 prof_buffer[eip]++;
 669                 }
 670 #endif
 671         }
 672         /*
 673          * check the cpu time limit on the process.
 674          */
 675         if ((current->rlim[RLIMIT_CPU].rlim_max != RLIM_INFINITY) &&
 676             (((current->stime + current->utime) / HZ) >= current->rlim[RLIMIT_CPU].rlim_max))
 677                 send_sig(SIGKILL, current, 1);
 678         if ((current->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) &&
 679             (((current->stime + current->utime) % HZ) == 0)) {
 680                 psecs = (current->stime + current->utime) / HZ;
 681                 /* send when equal */
 682                 if (psecs == current->rlim[RLIMIT_CPU].rlim_cur)
 683                         send_sig(SIGXCPU, current, 1);
 684                 /* and every five seconds thereafter. */
 685                 else if ((psecs > current->rlim[RLIMIT_CPU].rlim_cur) &&
 686                         ((psecs - current->rlim[RLIMIT_CPU].rlim_cur) % 5) == 0)
 687                         send_sig(SIGXCPU, current, 1);
 688         }
 689 
 690         if (current != task[0] && 0 > --current->counter) {
 691                 current->counter = 0;
 692                 need_resched = 1;
 693         }
 694         /* Update ITIMER_PROF for the current task */
 695         if (current->it_prof_value && !(--current->it_prof_value)) {
 696                 current->it_prof_value = current->it_prof_incr;
 697                 send_sig(SIGPROF,current,1);
 698         }
 699         for (mask = 1, tp = timer_table+0 ; mask ; tp++,mask += mask) {
 700                 if (mask > timer_active)
 701                         break;
 702                 if (!(mask & timer_active))
 703                         continue;
 704                 if (tp->expires > jiffies)
 705                         continue;
 706                 mark_bh(TIMER_BH);
 707         }
 708         cli();
 709         if (timer_head.next->expires < jiffies)
 710                 mark_bh(TIMER_BH);
 711         if (tq_timer != &tq_last)
 712                 mark_bh(TQUEUE_BH);
 713         sti();
 714 }
 715 
 716 asmlinkage unsigned int sys_alarm(unsigned int seconds)
     /* [previous][next][first][last][top][bottom][index][help] */
 717 {
 718         struct itimerval it_new, it_old;
 719         unsigned int oldalarm;
 720 
 721         it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
 722         it_new.it_value.tv_sec = seconds;
 723         it_new.it_value.tv_usec = 0;
 724         _setitimer(ITIMER_REAL, &it_new, &it_old);
 725         oldalarm = it_old.it_value.tv_sec;
 726         /* ehhh.. We can't return 0 if we have an alarm pending.. */
 727         /* And we'd better return too much than too little anyway */
 728         if (it_old.it_value.tv_usec)
 729                 oldalarm++;
 730         return oldalarm;
 731 }
 732 
 733 asmlinkage int sys_getpid(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 734 {
 735         return current->pid;
 736 }
 737 
 738 asmlinkage int sys_getppid(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 739 {
 740         return current->p_opptr->pid;
 741 }
 742 
 743 asmlinkage int sys_getuid(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 744 {
 745         return current->uid;
 746 }
 747 
 748 asmlinkage int sys_geteuid(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 749 {
 750         return current->euid;
 751 }
 752 
 753 asmlinkage int sys_getgid(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 754 {
 755         return current->gid;
 756 }
 757 
 758 asmlinkage int sys_getegid(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 759 {
 760         return current->egid;
 761 }
 762 
 763 asmlinkage int sys_nice(long increment)
     /* [previous][next][first][last][top][bottom][index][help] */
 764 {
 765         int newprio;
 766 
 767         if (increment < 0 && !suser())
 768                 return -EPERM;
 769         newprio = current->priority - increment;
 770         if (newprio < 1)
 771                 newprio = 1;
 772         if (newprio > 35)
 773                 newprio = 35;
 774         current->priority = newprio;
 775         return 0;
 776 }
 777 
 778 static void show_task(int nr,struct task_struct * p)
     /* [previous][next][first][last][top][bottom][index][help] */
 779 {
 780         unsigned long free;
 781         static const char * stat_nam[] = { "R", "S", "D", "Z", "T", "W" };
 782 
 783         printk("%-8s %3d ", p->comm, (p == current) ? -nr : nr);
 784         if (((unsigned) p->state) < sizeof(stat_nam)/sizeof(char *))
 785                 printk(stat_nam[p->state]);
 786         else
 787                 printk(" ");
 788 #if ((~0UL) == 0xffffffff)
 789         if (p == current)
 790                 printk(" current  ");
 791         else
 792                 printk(" %08lX ", thread_saved_pc(&p->tss));
 793 #else
 794         if (p == current)
 795                 printk("   current task   ");
 796         else
 797                 printk(" %016lx ", thread_saved_pc(&p->tss));
 798 #endif
 799         for (free = 1; free < PAGE_SIZE/sizeof(long) ; free++) {
 800                 if (((unsigned long *)p->kernel_stack_page)[free])
 801                         break;
 802         }
 803         printk("%5lu %5d %6d ", free*sizeof(long), p->pid, p->p_pptr->pid);
 804         if (p->p_cptr)
 805                 printk("%5d ", p->p_cptr->pid);
 806         else
 807                 printk("      ");
 808         if (p->p_ysptr)
 809                 printk("%7d", p->p_ysptr->pid);
 810         else
 811                 printk("       ");
 812         if (p->p_osptr)
 813                 printk(" %5d\n", p->p_osptr->pid);
 814         else
 815                 printk("\n");
 816 }
 817 
 818 void show_state(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 819 {
 820         int i;
 821 
 822 #if ((~0UL) == 0xffffffff)
 823         printk("\n"
 824                "                         free                        sibling\n");
 825         printk("  task             PC    stack   pid father child younger older\n");
 826 #else
 827         printk("\n"
 828                "                                 free                        sibling\n");
 829         printk("  task                 PC        stack   pid father child younger older\n");
 830 #endif
 831         for (i=0 ; i<NR_TASKS ; i++)
 832                 if (task[i])
 833                         show_task(i,task[i]);
 834 }
 835 
 836 void sched_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 837 {
 838         bh_base[TIMER_BH].routine = timer_bh;
 839         bh_base[TQUEUE_BH].routine = tqueue_bh;
 840         bh_base[IMMEDIATE_BH].routine = immediate_bh;
 841         if (request_irq(TIMER_IRQ, do_timer, 0, "timer") != 0)
 842                 panic("Could not allocate timer IRQ!");
 843         enable_bh(TIMER_BH);
 844         enable_bh(TQUEUE_BH);
 845         enable_bh(IMMEDIATE_BH);
 846 }

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