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

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