root/kernel/sched.c

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

DEFINITIONS

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

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

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