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

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