root/kernel/sched.c

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

DEFINITIONS

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

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