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

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