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

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