linux-old/kernel/sched.c
<<
>>
Prefs
   1/*
   2 *  linux/kernel/sched.c
   3 *
   4 *  Copyright (C) 1991, 1992  Linus Torvalds
   5 *
   6 *  1996-04-21  Modified by Ulrich Windl to make NTP work
   7 *  1996-12-23  Modified by Dave Grothe to fix bugs in semaphores and
   8 *              make semaphores SMP safe
   9 *  1997-01-28  Modified by Finn Arne Gangstad to make timers scale better.
  10 */
  11
  12/*
  13 * 'sched.c' is the main kernel file. It contains scheduling primitives
  14 * (sleep_on, wakeup, schedule etc) as well as a number of simple system
  15 * call functions (type getpid()), which just extract a field from
  16 * current-task
  17 */
  18
  19#include <linux/signal.h>
  20#include <linux/sched.h>
  21#include <linux/timer.h>
  22#include <linux/kernel.h>
  23#include <linux/kernel_stat.h>
  24#include <linux/fdreg.h>
  25#include <linux/errno.h>
  26#include <linux/time.h>
  27#include <linux/ptrace.h>
  28#include <linux/delay.h>
  29#include <linux/interrupt.h>
  30#include <linux/tqueue.h>
  31#include <linux/resource.h>
  32#include <linux/mm.h>
  33#include <linux/smp.h>
  34#include <linux/smp_lock.h>
  35#include <linux/init.h>
  36
  37#include <asm/system.h>
  38#include <asm/io.h>
  39#include <asm/uaccess.h>
  40#include <asm/pgtable.h>
  41#include <asm/mmu_context.h>
  42#include <asm/spinlock.h>
  43
  44#include <linux/timex.h>
  45
  46/*
  47 * kernel variables
  48 */
  49
  50int securelevel = 0;                    /* system security level */
  51
  52long tick = (1000000 + HZ/2) / HZ;      /* timer interrupt period */
  53volatile struct timeval xtime __attribute__ ((aligned (8)));    /* The current time */
  54int tickadj = 500/HZ;                   /* microsecs */
  55
  56DECLARE_TASK_QUEUE(tq_timer);
  57DECLARE_TASK_QUEUE(tq_immediate);
  58DECLARE_TASK_QUEUE(tq_scheduler);
  59
  60/*
  61 * phase-lock loop variables
  62 */
  63/* TIME_ERROR prevents overwriting the CMOS clock */
  64int time_state = TIME_ERROR;    /* clock synchronization status */
  65int time_status = STA_UNSYNC;   /* clock status bits */
  66long time_offset = 0;           /* time adjustment (us) */
  67long time_constant = 2;         /* pll time constant */
  68long time_tolerance = MAXFREQ;  /* frequency tolerance (ppm) */
  69long time_precision = 1;        /* clock precision (us) */
  70long time_maxerror = MAXPHASE;  /* maximum error (us) */
  71long time_esterror = MAXPHASE;  /* estimated error (us) */
  72long time_phase = 0;            /* phase offset (scaled us) */
  73long time_freq = ((1000000 + HZ/2) % HZ - HZ/2) << SHIFT_USEC;  /* frequency offset (scaled ppm) */
  74long time_adj = 0;              /* tick adjust (scaled 1 / HZ) */
  75long time_reftime = 0;          /* time at last adjustment (s) */
  76
  77long time_adjust = 0;
  78long time_adjust_step = 0;
  79
  80int need_resched = 0;
  81unsigned long event = 0;
  82
  83extern int _setitimer(int, struct itimerval *, struct itimerval *);
  84unsigned int * prof_buffer = NULL;
  85unsigned long prof_len = 0;
  86unsigned long prof_shift = 0;
  87
  88#define _S(nr) (1<<((nr)-1))
  89
  90extern void mem_use(void);
  91
  92unsigned long volatile jiffies=0;
  93
  94/*
  95 *      Init task must be ok at boot for the ix86 as we will check its signals
  96 *      via the SMP irq return path.
  97 */
  98 
  99struct task_struct *last_task_used_math = NULL;
 100
 101struct task_struct * task[NR_TASKS] = {&init_task, };
 102
 103struct kernel_stat kstat = { 0 };
 104
 105static inline void add_to_runqueue(struct task_struct * p)
 106{
 107        if (p->counter > current->counter + 3)
 108                need_resched = 1;
 109        nr_running++;
 110        (p->prev_run = init_task.prev_run)->next_run = p;
 111        p->next_run = &init_task;
 112        init_task.prev_run = p;
 113}
 114
 115static inline void del_from_runqueue(struct task_struct * p)
 116{
 117        struct task_struct *next = p->next_run;
 118        struct task_struct *prev = p->prev_run;
 119
 120        nr_running--;
 121        next->prev_run = prev;
 122        prev->next_run = next;
 123        p->next_run = NULL;
 124        p->prev_run = NULL;
 125}
 126
 127static inline void move_last_runqueue(struct task_struct * p)
 128{
 129        struct task_struct *next = p->next_run;
 130        struct task_struct *prev = p->prev_run;
 131
 132        /* remove from list */
 133        next->prev_run = prev;
 134        prev->next_run = next;
 135        /* add back to list */
 136        p->next_run = &init_task;
 137        prev = init_task.prev_run;
 138        init_task.prev_run = p;
 139        p->prev_run = prev;
 140        prev->next_run = p;
 141}
 142
 143#ifdef __SMP__
 144/*
 145 * The tasklist_lock protects the linked list of processes.
 146 *
 147 * The scheduler lock is protecting against multiple entry
 148 * into the scheduling code, and doesn't need to worry
 149 * about interrupts (because interrupts cannot call the
 150 * scheduler).
 151 *
 152 * The run-queue lock locks the parts that actually access
 153 * and change the run-queues, and have to be interrupt-safe.
 154 */
 155rwlock_t tasklist_lock = RW_LOCK_UNLOCKED;
 156spinlock_t scheduler_lock = SPIN_LOCK_UNLOCKED;
 157static spinlock_t runqueue_lock = SPIN_LOCK_UNLOCKED;
 158#endif
 159
 160/*
 161 * Wake up a process. Put it on the run-queue if it's not
 162 * already there.  The "current" process is always on the
 163 * run-queue (except when the actual re-schedule is in
 164 * progress), and as such you're allowed to do the simpler
 165 * "current->state = TASK_RUNNING" to mark yourself runnable
 166 * without the overhead of this.
 167 */
 168inline void wake_up_process(struct task_struct * p)
 169{
 170        unsigned long flags;
 171
 172        spin_lock_irqsave(&runqueue_lock, flags);
 173        p->state = TASK_RUNNING;
 174        if (!p->next_run)
 175                add_to_runqueue(p);
 176        spin_unlock_irqrestore(&runqueue_lock, flags);
 177}
 178
 179static void process_timeout(unsigned long __data)
 180{
 181        struct task_struct * p = (struct task_struct *) __data;
 182
 183        p->timeout = 0;
 184        wake_up_process(p);
 185}
 186
 187/*
 188 * This is the function that decides how desirable a process is..
 189 * You can weigh different processes against each other depending
 190 * on what CPU they've run on lately etc to try to handle cache
 191 * and TLB miss penalties.
 192 *
 193 * Return values:
 194 *       -1000: never select this
 195 *           0: out of time, recalculate counters (but it might still be
 196 *              selected)
 197 *         +ve: "goodness" value (the larger, the better)
 198 *       +1000: realtime process, select this.
 199 */
 200static inline int goodness(struct task_struct * p, struct task_struct * prev, int this_cpu)
 201{
 202        int weight;
 203
 204        /*
 205         * Realtime process, select the first one on the
 206         * runqueue (taking priorities within processes
 207         * into account).
 208         */
 209        if (p->policy != SCHED_OTHER)
 210                return 1000 + p->rt_priority;
 211
 212        /*
 213         * Give the process a first-approximation goodness value
 214         * according to the number of clock-ticks it has left.
 215         *
 216         * Don't do any other calculations if the time slice is
 217         * over..
 218         */
 219        weight = p->counter;
 220        if (weight) {
 221                        
 222#ifdef __SMP__
 223                /* Give a largish advantage to the same processor...   */
 224                /* (this is equivalent to penalizing other processors) */
 225                if (p->processor == this_cpu)
 226                        weight += PROC_CHANGE_PENALTY;
 227#endif
 228
 229                /* .. and a slight advantage to the current process */
 230                if (p == prev)
 231                        weight += 1;
 232        }
 233
 234        return weight;
 235}
 236
 237/*
 238 * Event timer code
 239 */
 240#define TVN_BITS 6
 241#define TVR_BITS 8
 242#define TVN_SIZE (1 << TVN_BITS)
 243#define TVR_SIZE (1 << TVR_BITS)
 244#define TVN_MASK (TVN_SIZE - 1)
 245#define TVR_MASK (TVR_SIZE - 1)
 246
 247struct timer_vec {
 248        int index;
 249        struct timer_list *vec[TVN_SIZE];
 250};
 251
 252struct timer_vec_root {
 253        int index;
 254        struct timer_list *vec[TVR_SIZE];
 255};
 256
 257static struct timer_vec tv5 = { 0 };
 258static struct timer_vec tv4 = { 0 };
 259static struct timer_vec tv3 = { 0 };
 260static struct timer_vec tv2 = { 0 };
 261static struct timer_vec_root tv1 = { 0 };
 262
 263static struct timer_vec * const tvecs[] = {
 264        (struct timer_vec *)&tv1, &tv2, &tv3, &tv4, &tv5
 265};
 266
 267#define NOOF_TVECS (sizeof(tvecs) / sizeof(tvecs[0]))
 268
 269static unsigned long timer_jiffies = 0;
 270
 271static inline void insert_timer(struct timer_list *timer,
 272                                struct timer_list **vec, int idx)
 273{
 274        if ((timer->next = vec[idx]))
 275                vec[idx]->prev = timer;
 276        vec[idx] = timer;
 277        timer->prev = (struct timer_list *)&vec[idx];
 278}
 279
 280static inline void internal_add_timer(struct timer_list *timer)
 281{
 282        /*
 283         * must be cli-ed when calling this
 284         */
 285        unsigned long expires = timer->expires;
 286        unsigned long idx = expires - timer_jiffies;
 287
 288        if (idx < TVR_SIZE) {
 289                int i = expires & TVR_MASK;
 290                insert_timer(timer, tv1.vec, i);
 291        } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
 292                int i = (expires >> TVR_BITS) & TVN_MASK;
 293                insert_timer(timer, tv2.vec, i);
 294        } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
 295                int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
 296                insert_timer(timer, tv3.vec, i);
 297        } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
 298                int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
 299                insert_timer(timer, tv4.vec, i);
 300        } else if (expires < timer_jiffies) {
 301                /* can happen if you add a timer with expires == jiffies,
 302                 * or you set a timer to go off in the past
 303                 */
 304                insert_timer(timer, tv1.vec, tv1.index);
 305        } else if (idx < 0xffffffffUL) {
 306                int i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
 307                insert_timer(timer, tv5.vec, i);
 308        } else {
 309                /* Can only get here on architectures with 64-bit jiffies */
 310                timer->next = timer->prev = timer;
 311        }
 312}
 313
 314static spinlock_t timerlist_lock = SPIN_LOCK_UNLOCKED;
 315
 316void add_timer(struct timer_list *timer)
 317{
 318        unsigned long flags;
 319
 320        spin_lock_irqsave(&timerlist_lock, flags);
 321        internal_add_timer(timer);
 322        spin_unlock_irqrestore(&timerlist_lock, flags);
 323}
 324
 325static inline int detach_timer(struct timer_list *timer)
 326{
 327        int ret = 0;
 328        struct timer_list *next, *prev;
 329        next = timer->next;
 330        prev = timer->prev;
 331        if (next) {
 332                next->prev = prev;
 333        }
 334        if (prev) {
 335                ret = 1;
 336                prev->next = next;
 337        }
 338        return ret;
 339}
 340
 341
 342int del_timer(struct timer_list * timer)
 343{
 344        int ret;
 345        unsigned long flags;
 346
 347        spin_lock_irqsave(&timerlist_lock, flags);
 348        ret = detach_timer(timer);
 349        timer->next = timer->prev = 0;
 350        spin_unlock_irqrestore(&timerlist_lock, flags);
 351        return ret;
 352}
 353
 354#ifdef __SMP__
 355
 356#define idle_task (task[cpu_number_map[this_cpu]])
 357#define can_schedule(p) (!(p)->has_cpu)
 358
 359#else
 360
 361#define idle_task (&init_task)
 362#define can_schedule(p) (1)
 363
 364#endif
 365
 366/*
 367 *  'schedule()' is the scheduler function. It's a very simple and nice
 368 * scheduler: it's not perfect, but certainly works for most things.
 369 *
 370 * The goto is "interesting".
 371 *
 372 *   NOTE!!  Task 0 is the 'idle' task, which gets called when no other
 373 * tasks can run. It can not be killed, and it cannot sleep. The 'state'
 374 * information in task[0] is never used.
 375 */
 376asmlinkage void schedule(void)
 377{
 378        int lock_depth;
 379        struct task_struct * prev, * next;
 380        unsigned long timeout;
 381        int this_cpu;
 382
 383        need_resched = 0;
 384        prev = current;
 385        this_cpu = smp_processor_id();
 386        if (local_irq_count[this_cpu])
 387                goto scheduling_in_interrupt;
 388        release_kernel_lock(prev, this_cpu, lock_depth);
 389        if (bh_active & bh_mask)
 390                do_bottom_half();
 391
 392        spin_lock(&scheduler_lock);
 393        spin_lock_irq(&runqueue_lock);
 394
 395        /* move an exhausted RR process to be last.. */
 396        if (!prev->counter && prev->policy == SCHED_RR) {
 397                prev->counter = prev->priority;
 398                move_last_runqueue(prev);
 399        }
 400        timeout = 0;
 401        switch (prev->state) {
 402                case TASK_INTERRUPTIBLE:
 403                        if (signal_pending(prev))
 404                                goto makerunnable;
 405                        timeout = prev->timeout;
 406                        if (timeout && (timeout <= jiffies)) {
 407                                prev->timeout = 0;
 408                                timeout = 0;
 409                makerunnable:
 410                                prev->state = TASK_RUNNING;
 411                                break;
 412                        }
 413                default:
 414                        del_from_runqueue(prev);
 415                case TASK_RUNNING:
 416        }
 417        {
 418                struct task_struct * p = init_task.next_run;
 419                /*
 420                 * This is subtle.
 421                 * Note how we can enable interrupts here, even
 422                 * though interrupts can add processes to the run-
 423                 * queue. This is because any new processes will
 424                 * be added to the front of the queue, so "p" above
 425                 * is a safe starting point.
 426                 * run-queue deletion and re-ordering is protected by
 427                 * the scheduler lock
 428                 */
 429                spin_unlock_irq(&runqueue_lock);
 430#ifdef __SMP__
 431                prev->has_cpu = 0;
 432#endif
 433        
 434/*
 435 * Note! there may appear new tasks on the run-queue during this, as
 436 * interrupts are enabled. However, they will be put on front of the
 437 * list, so our list starting at "p" is essentially fixed.
 438 */
 439/* this is the scheduler proper: */
 440                {
 441                        int c = -1000;
 442                        next = idle_task;
 443                        while (p != &init_task) {
 444                                if (can_schedule(p)) {
 445                                        int weight = goodness(p, prev, this_cpu);
 446                                        if (weight > c)
 447                                                c = weight, next = p;
 448                                }
 449                                p = p->next_run;
 450                        }
 451
 452                        /* Do we need to re-calculate counters? */
 453                        if (!c) {
 454                                struct task_struct *p;
 455                                read_lock(&tasklist_lock);
 456                                for_each_task(p)
 457                                        p->counter = (p->counter >> 1) + p->priority;
 458                                read_unlock(&tasklist_lock);
 459                        }
 460                }
 461        }
 462
 463#ifdef __SMP__
 464        next->has_cpu = 1;
 465        next->processor = this_cpu;
 466#endif
 467
 468        if (prev != next) {
 469                struct timer_list timer;
 470
 471                kstat.context_swtch++;
 472                if (timeout) {
 473                        init_timer(&timer);
 474                        timer.expires = timeout;
 475                        timer.data = (unsigned long) prev;
 476                        timer.function = process_timeout;
 477                        add_timer(&timer);
 478                }
 479                get_mmu_context(next);
 480                switch_to(prev,next);
 481
 482                if (timeout)
 483                        del_timer(&timer);
 484        }
 485        spin_unlock(&scheduler_lock);
 486
 487        reacquire_kernel_lock(prev, smp_processor_id(), lock_depth);
 488        return;
 489
 490scheduling_in_interrupt:
 491        printk("Scheduling in interrupt\n");
 492        *(int *)0 = 0;
 493}
 494
 495
 496rwlock_t waitqueue_lock = RW_LOCK_UNLOCKED;
 497
 498/*
 499 * wake_up doesn't wake up stopped processes - they have to be awakened
 500 * with signals or similar.
 501 *
 502 * Note that we only need a read lock for the wait queue (and thus do not
 503 * have to protect against interrupts), as the actual removal from the
 504 * queue is handled by the process itself.
 505 */
 506void wake_up(struct wait_queue **q)
 507{
 508        struct wait_queue *next;
 509
 510        read_lock(&waitqueue_lock);
 511        if (q && (next = *q)) {
 512                struct wait_queue *head;
 513
 514                head = WAIT_QUEUE_HEAD(q);
 515                while (next != head) {
 516                        struct task_struct *p = next->task;
 517                        next = next->next;
 518                        if ((p->state == TASK_UNINTERRUPTIBLE) ||
 519                            (p->state == TASK_INTERRUPTIBLE))
 520                                wake_up_process(p);
 521                }
 522        }
 523        read_unlock(&waitqueue_lock);
 524}
 525
 526void wake_up_interruptible(struct wait_queue **q)
 527{
 528        struct wait_queue *next;
 529
 530        read_lock(&waitqueue_lock);
 531        if (q && (next = *q)) {
 532                struct wait_queue *head;
 533
 534                head = WAIT_QUEUE_HEAD(q);
 535                while (next != head) {
 536                        struct task_struct *p = next->task;
 537                        next = next->next;
 538                        if (p->state == TASK_INTERRUPTIBLE)
 539                                wake_up_process(p);
 540                }
 541        }
 542        read_unlock(&waitqueue_lock);
 543}
 544
 545/*
 546 * Semaphores are implemented using a two-way counter:
 547 * The "count" variable is decremented for each process
 548 * that tries to sleep, while the "waking" variable is
 549 * incremented when the "up()" code goes to wake up waiting
 550 * processes.
 551 *
 552 * Notably, the inline "up()" and "down()" functions can
 553 * efficiently test if they need to do any extra work (up
 554 * needs to do something only if count was negative before
 555 * the increment operation.
 556 *
 557 * waking_non_zero() (from asm/semaphore.h) must execute
 558 * atomically.
 559 *
 560 * When __up() is called, the count was negative before
 561 * incrementing it, and we need to wake up somebody.
 562 *
 563 * This routine adds one to the count of processes that need to
 564 * wake up and exit.  ALL waiting processes actually wake up but
 565 * only the one that gets to the "waking" field first will gate
 566 * through and acquire the semaphore.  The others will go back
 567 * to sleep.
 568 *
 569 * Note that these functions are only called when there is
 570 * contention on the lock, and as such all this is the
 571 * "non-critical" part of the whole semaphore business. The
 572 * critical part is the inline stuff in <asm/semaphore.h>
 573 * where we want to avoid any extra jumps and calls.
 574 */
 575void __up(struct semaphore *sem)
 576{
 577        wake_one_more(sem);
 578        wake_up(&sem->wait);
 579}
 580
 581/*
 582 * Perform the "down" function.  Return zero for semaphore acquired,
 583 * return negative for signalled out of the function.
 584 *
 585 * If called from __down, the return is ignored and the wait loop is
 586 * not interruptible.  This means that a task waiting on a semaphore
 587 * using "down()" cannot be killed until someone does an "up()" on
 588 * the semaphore.
 589 *
 590 * If called from __down_interruptible, the return value gets checked
 591 * upon return.  If the return value is negative then the task continues
 592 * with the negative value in the return register (it can be tested by
 593 * the caller).
 594 *
 595 * Either form may be used in conjunction with "up()".
 596 *
 597 */
 598static inline int __do_down(struct semaphore * sem, int task_state)
 599{
 600        struct task_struct *tsk = current;
 601        struct wait_queue wait = { tsk, NULL };
 602        int               ret = 0;
 603
 604        tsk->state = task_state;
 605        add_wait_queue(&sem->wait, &wait);
 606
 607        /*
 608         * Ok, we're set up.  sem->count is known to be less than zero
 609         * so we must wait.
 610         *
 611         * We can let go the lock for purposes of waiting.
 612         * We re-acquire it after awaking so as to protect
 613         * all semaphore operations.
 614         *
 615         * If "up()" is called before we call waking_non_zero() then
 616         * we will catch it right away.  If it is called later then
 617         * we will have to go through a wakeup cycle to catch it.
 618         *
 619         * Multiple waiters contend for the semaphore lock to see
 620         * who gets to gate through and who has to wait some more.
 621         */
 622        for (;;) {
 623                if (waking_non_zero(sem))       /* are we waking up?  */
 624                        break;                  /* yes, exit loop */
 625
 626                if (task_state == TASK_INTERRUPTIBLE && signal_pending(tsk)) {
 627                        ret = -EINTR;                   /* interrupted */
 628                        atomic_inc(&sem->count);        /* give up on down operation */
 629                        break;
 630                }
 631
 632                schedule();
 633                tsk->state = task_state;
 634        }
 635
 636        tsk->state = TASK_RUNNING;
 637        remove_wait_queue(&sem->wait, &wait);
 638        return ret;
 639}
 640
 641void __down(struct semaphore * sem)
 642{
 643        __do_down(sem,TASK_UNINTERRUPTIBLE);
 644}
 645
 646int __down_interruptible(struct semaphore * sem)
 647{
 648        return __do_down(sem,TASK_INTERRUPTIBLE);
 649}
 650
 651
 652static inline void __sleep_on(struct wait_queue **p, int state)
 653{
 654        unsigned long flags;
 655        struct wait_queue wait = { current, NULL };
 656
 657        if (!p)
 658                return;
 659        current->state = state;
 660        write_lock_irqsave(&waitqueue_lock, flags);
 661        __add_wait_queue(p, &wait);
 662        write_unlock(&waitqueue_lock);
 663        schedule();
 664        write_lock_irq(&waitqueue_lock);
 665        __remove_wait_queue(p, &wait);
 666        write_unlock_irqrestore(&waitqueue_lock, flags);
 667}
 668
 669void interruptible_sleep_on(struct wait_queue **p)
 670{
 671        __sleep_on(p,TASK_INTERRUPTIBLE);
 672}
 673
 674void sleep_on(struct wait_queue **p)
 675{
 676        __sleep_on(p,TASK_UNINTERRUPTIBLE);
 677}
 678
 679static inline void cascade_timers(struct timer_vec *tv)
 680{
 681        /* cascade all the timers from tv up one level */
 682        struct timer_list *timer;
 683        timer = tv->vec[tv->index];
 684        /*
 685         * We are removing _all_ timers from the list, so we don't  have to
 686         * detach them individually, just clear the list afterwards.
 687         */
 688        while (timer) {
 689                struct timer_list *tmp = timer;
 690                timer = timer->next;
 691                internal_add_timer(tmp);
 692        }
 693        tv->vec[tv->index] = NULL;
 694        tv->index = (tv->index + 1) & TVN_MASK;
 695}
 696
 697static inline void run_timer_list(void)
 698{
 699        spin_lock_irq(&timerlist_lock);
 700        while ((long)(jiffies - timer_jiffies) >= 0) {
 701                struct timer_list *timer;
 702                if (!tv1.index) {
 703                        int n = 1;
 704                        do {
 705                                cascade_timers(tvecs[n]);
 706                        } while (tvecs[n]->index == 1 && ++n < NOOF_TVECS);
 707                }
 708                while ((timer = tv1.vec[tv1.index])) {
 709                        void (*fn)(unsigned long) = timer->function;
 710                        unsigned long data = timer->data;
 711                        detach_timer(timer);
 712                        timer->next = timer->prev = NULL;
 713                        spin_unlock_irq(&timerlist_lock);
 714                        fn(data);
 715                        spin_lock_irq(&timerlist_lock);
 716                }
 717                ++timer_jiffies; 
 718                tv1.index = (tv1.index + 1) & TVR_MASK;
 719        }
 720        spin_unlock_irq(&timerlist_lock);
 721}
 722
 723
 724static inline void run_old_timers(void)
 725{
 726        struct timer_struct *tp;
 727        unsigned long mask;
 728
 729        for (mask = 1, tp = timer_table+0 ; mask ; tp++,mask += mask) {
 730                if (mask > timer_active)
 731                        break;
 732                if (!(mask & timer_active))
 733                        continue;
 734                if (tp->expires > jiffies)
 735                        continue;
 736                timer_active &= ~mask;
 737                tp->fn();
 738                sti();
 739        }
 740}
 741
 742spinlock_t tqueue_lock;
 743
 744void tqueue_bh(void)
 745{
 746        run_task_queue(&tq_timer);
 747}
 748
 749void immediate_bh(void)
 750{
 751        run_task_queue(&tq_immediate);
 752}
 753
 754unsigned long timer_active = 0;
 755struct timer_struct timer_table[32];
 756
 757/*
 758 * Hmm.. Changed this, as the GNU make sources (load.c) seems to
 759 * imply that avenrun[] is the standard name for this kind of thing.
 760 * Nothing else seems to be standardized: the fractional size etc
 761 * all seem to differ on different machines.
 762 */
 763unsigned long avenrun[3] = { 0,0,0 };
 764
 765/*
 766 * Nr of active tasks - counted in fixed-point numbers
 767 */
 768static unsigned long count_active_tasks(void)
 769{
 770        struct task_struct *p;
 771        unsigned long nr = 0;
 772
 773        read_lock(&tasklist_lock);
 774        for_each_task(p) {
 775                if (p->pid &&
 776                    (p->state == TASK_RUNNING ||
 777                     p->state == TASK_UNINTERRUPTIBLE ||
 778                     p->state == TASK_SWAPPING))
 779                        nr += FIXED_1;
 780        }
 781        read_unlock(&tasklist_lock);
 782        return nr;
 783}
 784
 785static inline void calc_load(unsigned long ticks)
 786{
 787        unsigned long active_tasks; /* fixed-point */
 788        static int count = LOAD_FREQ;
 789
 790        count -= ticks;
 791        if (count < 0) {
 792                count += LOAD_FREQ;
 793                active_tasks = count_active_tasks();
 794                CALC_LOAD(avenrun[0], EXP_1, active_tasks);
 795                CALC_LOAD(avenrun[1], EXP_5, active_tasks);
 796                CALC_LOAD(avenrun[2], EXP_15, active_tasks);
 797        }
 798}
 799
 800/*
 801 * this routine handles the overflow of the microsecond field
 802 *
 803 * The tricky bits of code to handle the accurate clock support
 804 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
 805 * They were originally developed for SUN and DEC kernels.
 806 * All the kudos should go to Dave for this stuff.
 807 *
 808 */
 809static void second_overflow(void)
 810{
 811    long ltemp;
 812
 813    /* Bump the maxerror field */
 814    time_maxerror += time_tolerance >> SHIFT_USEC;
 815    if ( time_maxerror > MAXPHASE )
 816        time_maxerror = MAXPHASE;
 817
 818    /*
 819     * Leap second processing. If in leap-insert state at
 820     * the end of the day, the system clock is set back one
 821     * second; if in leap-delete state, the system clock is
 822     * set ahead one second. The microtime() routine or
 823     * external clock driver will insure that reported time
 824     * is always monotonic. The ugly divides should be
 825     * replaced.
 826     */
 827    switch (time_state) {
 828
 829    case TIME_OK:
 830        if (time_status & STA_INS)
 831            time_state = TIME_INS;
 832        else if (time_status & STA_DEL)
 833            time_state = TIME_DEL;
 834        break;
 835
 836    case TIME_INS:
 837        if (xtime.tv_sec % 86400 == 0) {
 838            xtime.tv_sec--;
 839            time_state = TIME_OOP;
 840            printk("Clock: inserting leap second 23:59:60 UTC\n");
 841        }
 842        break;
 843
 844    case TIME_DEL:
 845        if ((xtime.tv_sec + 1) % 86400 == 0) {
 846            xtime.tv_sec++;
 847            time_state = TIME_WAIT;
 848            printk("Clock: deleting leap second 23:59:59 UTC\n");
 849        }
 850        break;
 851
 852    case TIME_OOP:
 853        time_state = TIME_WAIT;
 854        break;
 855
 856    case TIME_WAIT:
 857        if (!(time_status & (STA_INS | STA_DEL)))
 858            time_state = TIME_OK;
 859    }
 860
 861    /*
 862     * Compute the phase adjustment for the next second. In
 863     * PLL mode, the offset is reduced by a fixed factor
 864     * times the time constant. In FLL mode the offset is
 865     * used directly. In either mode, the maximum phase
 866     * adjustment for each second is clamped so as to spread
 867     * the adjustment over not more than the number of
 868     * seconds between updates.
 869     */
 870    if (time_offset < 0) {
 871        ltemp = -time_offset;
 872        if (!(time_status & STA_FLL))
 873            ltemp >>= SHIFT_KG + time_constant;
 874        if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE)
 875            ltemp = (MAXPHASE / MINSEC) << SHIFT_UPDATE;
 876        time_offset += ltemp;
 877        time_adj = -ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
 878    } else {
 879        ltemp = time_offset;
 880        if (!(time_status & STA_FLL))
 881            ltemp >>= SHIFT_KG + time_constant;
 882        if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE)
 883            ltemp = (MAXPHASE / MINSEC) << SHIFT_UPDATE;
 884        time_offset -= ltemp;
 885        time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
 886    }
 887
 888    /*
 889     * Compute the frequency estimate and additional phase
 890     * adjustment due to frequency error for the next
 891     * second. When the PPS signal is engaged, gnaw on the
 892     * watchdog counter and update the frequency computed by
 893     * the pll and the PPS signal.
 894     */
 895    pps_valid++;
 896    if (pps_valid == PPS_VALID) {
 897        pps_jitter = MAXTIME;
 898        pps_stabil = MAXFREQ;
 899        time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
 900                         STA_PPSWANDER | STA_PPSERROR);
 901    }
 902    ltemp = time_freq + pps_freq;
 903    if (ltemp < 0)
 904        time_adj -= -ltemp >>
 905            (SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE);
 906    else
 907        time_adj += ltemp >>
 908            (SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE);
 909
 910#if HZ == 100
 911    /* compensate for (HZ==100) != 128. Add 25% to get 125; => only 3% error */
 912    if (time_adj < 0)
 913        time_adj -= -time_adj >> 2;
 914    else
 915        time_adj += time_adj >> 2;
 916#endif
 917}
 918
 919/* in the NTP reference this is called "hardclock()" */
 920static void update_wall_time_one_tick(void)
 921{
 922        /*
 923         * Advance the phase, once it gets to one microsecond, then
 924         * advance the tick more.
 925         */
 926        time_phase += time_adj;
 927        if (time_phase <= -FINEUSEC) {
 928                long ltemp = -time_phase >> SHIFT_SCALE;
 929                time_phase += ltemp << SHIFT_SCALE;
 930                xtime.tv_usec += tick + time_adjust_step - ltemp;
 931        }
 932        else if (time_phase >= FINEUSEC) {
 933                long ltemp = time_phase >> SHIFT_SCALE;
 934                time_phase -= ltemp << SHIFT_SCALE;
 935                xtime.tv_usec += tick + time_adjust_step + ltemp;
 936        } else
 937                xtime.tv_usec += tick + time_adjust_step;
 938
 939        if (time_adjust) {
 940            /* We are doing an adjtime thing. 
 941             *
 942             * Modify the value of the tick for next time.
 943             * Note that a positive delta means we want the clock
 944             * to run fast. This means that the tick should be bigger
 945             *
 946             * Limit the amount of the step for *next* tick to be
 947             * in the range -tickadj .. +tickadj
 948             */
 949             if (time_adjust > tickadj)
 950                time_adjust_step = tickadj;
 951             else if (time_adjust < -tickadj)
 952                time_adjust_step = -tickadj;
 953             else
 954                time_adjust_step = time_adjust;
 955             
 956            /* Reduce by this step the amount of time left  */
 957            time_adjust -= time_adjust_step;
 958        }
 959        else
 960            time_adjust_step = 0;
 961}
 962
 963/*
 964 * Using a loop looks inefficient, but "ticks" is
 965 * usually just one (we shouldn't be losing ticks,
 966 * we're doing this this way mainly for interrupt
 967 * latency reasons, not because we think we'll
 968 * have lots of lost timer ticks
 969 */
 970static void update_wall_time(unsigned long ticks)
 971{
 972        do {
 973                ticks--;
 974                update_wall_time_one_tick();
 975        } while (ticks);
 976
 977        if (xtime.tv_usec >= 1000000) {
 978            xtime.tv_usec -= 1000000;
 979            xtime.tv_sec++;
 980            second_overflow();
 981        }
 982}
 983
 984static inline void do_process_times(struct task_struct *p,
 985        unsigned long user, unsigned long system)
 986{
 987        long psecs;
 988
 989        psecs = (p->times.tms_utime += user);
 990        psecs += (p->times.tms_stime += system);
 991        if (psecs / HZ > p->rlim[RLIMIT_CPU].rlim_cur) {
 992                /* Send SIGXCPU every second.. */
 993                if (!(psecs % HZ))
 994                        send_sig(SIGXCPU, p, 1);
 995                /* and SIGKILL when we go over max.. */
 996                if (psecs / HZ > p->rlim[RLIMIT_CPU].rlim_max)
 997                        send_sig(SIGKILL, p, 1);
 998        }
 999}
1000
1001static inline void do_it_virt(struct task_struct * p, unsigned long ticks)
1002{
1003        unsigned long it_virt = p->it_virt_value;
1004
1005        if (it_virt) {
1006                if (it_virt <= ticks) {
1007                        it_virt = ticks + p->it_virt_incr;
1008                        send_sig(SIGVTALRM, p, 1);
1009                }
1010                p->it_virt_value = it_virt - ticks;
1011        }
1012}
1013
1014static inline void do_it_prof(struct task_struct * p, unsigned long ticks)
1015{
1016        unsigned long it_prof = p->it_prof_value;
1017
1018        if (it_prof) {
1019                if (it_prof <= ticks) {
1020                        it_prof = ticks + p->it_prof_incr;
1021                        send_sig(SIGPROF, p, 1);
1022                }
1023                p->it_prof_value = it_prof - ticks;
1024        }
1025}
1026
1027void update_one_process(struct task_struct *p,
1028        unsigned long ticks, unsigned long user, unsigned long system)
1029{
1030        do_process_times(p, user, system);
1031        do_it_virt(p, user);
1032        do_it_prof(p, ticks);
1033}       
1034
1035static void update_process_times(unsigned long ticks, unsigned long system)
1036{
1037/*
1038 * SMP does this on a per-CPU basis elsewhere
1039 */
1040#ifndef  __SMP__
1041        struct task_struct * p = current;
1042        unsigned long user = ticks - system;
1043        if (p->pid) {
1044                p->counter -= ticks;
1045                if (p->counter < 0) {
1046                        p->counter = 0;
1047                        need_resched = 1;
1048                }
1049                if (p->priority < DEF_PRIORITY)
1050                        kstat.cpu_nice += user;
1051                else
1052                        kstat.cpu_user += user;
1053                kstat.cpu_system += system;
1054        }
1055        update_one_process(p, ticks, user, system);
1056#endif
1057}
1058
1059volatile unsigned long lost_ticks = 0;
1060static unsigned long lost_ticks_system = 0;
1061
1062static inline void update_times(void)
1063{
1064        unsigned long ticks;
1065        unsigned long flags;
1066
1067        save_flags(flags);
1068        cli();
1069
1070        ticks = lost_ticks;
1071        lost_ticks = 0;
1072
1073        if (ticks) {
1074                unsigned long system;
1075                system = xchg(&lost_ticks_system, 0);
1076
1077                calc_load(ticks);
1078                update_wall_time(ticks);
1079                restore_flags(flags);
1080                
1081                update_process_times(ticks, system);
1082
1083        } else
1084                restore_flags(flags);
1085}
1086
1087static void timer_bh(void)
1088{
1089        update_times();
1090        run_old_timers();
1091        run_timer_list();
1092}
1093
1094void do_timer(struct pt_regs * regs)
1095{
1096        (*(unsigned long *)&jiffies)++;
1097        lost_ticks++;
1098        mark_bh(TIMER_BH);
1099        if (!user_mode(regs))
1100                lost_ticks_system++;
1101        if (tq_timer)
1102                mark_bh(TQUEUE_BH);
1103}
1104
1105#ifndef __alpha__
1106
1107/*
1108 * For backwards compatibility?  This can be done in libc so Alpha
1109 * and all newer ports shouldn't need it.
1110 */
1111asmlinkage unsigned int sys_alarm(unsigned int seconds)
1112{
1113        struct itimerval it_new, it_old;
1114        unsigned int oldalarm;
1115
1116        it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
1117        it_new.it_value.tv_sec = seconds;
1118        it_new.it_value.tv_usec = 0;
1119        _setitimer(ITIMER_REAL, &it_new, &it_old);
1120        oldalarm = it_old.it_value.tv_sec;
1121        /* ehhh.. We can't return 0 if we have an alarm pending.. */
1122        /* And we'd better return too much than too little anyway */
1123        if (it_old.it_value.tv_usec)
1124                oldalarm++;
1125        return oldalarm;
1126}
1127
1128/*
1129 * The Alpha uses getxpid, getxuid, and getxgid instead.  Maybe this
1130 * should be moved into arch/i386 instead?
1131 */
1132 
1133asmlinkage int sys_getpid(void)
1134{
1135        /* This is SMP safe - current->pid doesnt change */
1136        return current->pid;
1137}
1138
1139/*
1140 * This is not strictly SMP safe: p_opptr could change
1141 * from under us. However, rather than getting any lock
1142 * we can use an optimistic algorithm: get the parent
1143 * pid, and go back and check that the parent is still
1144 * the same. If it has changed (which is extremely unlikely
1145 * indeed), we just try again..
1146 *
1147 * NOTE! This depends on the fact that even if we _do_
1148 * get an old value of "parent", we can happily dereference
1149 * the pointer: we just can't necessarily trust the result
1150 * until we know that the parent pointer is valid.
1151 *
1152 * The "mb()" macro is a memory barrier - a synchronizing
1153 * event. It also makes sure that gcc doesn't optimize
1154 * away the necessary memory references.. The barrier doesn't
1155 * have to have all that strong semantics: on x86 we don't
1156 * really require a synchronizing instruction, for example.
1157 * The barrier is more important for code generation than
1158 * for any real memory ordering semantics (even if there is
1159 * a small window for a race, using the old pointer is
1160 * harmless for a while).
1161 */
1162asmlinkage int sys_getppid(void)
1163{
1164        int pid;
1165        struct task_struct * me = current;
1166        struct task_struct * parent;
1167
1168        parent = me->p_opptr;
1169        for (;;) {
1170                pid = parent->pid;
1171#if __SMP__
1172{
1173                struct task_struct *old = parent;
1174                mb();
1175                parent = me->p_opptr;
1176                if (old != parent)
1177                        continue;
1178}
1179#endif
1180                break;
1181        }
1182        return pid;
1183}
1184
1185asmlinkage int sys_getuid(void)
1186{
1187        /* Only we change this so SMP safe */
1188        return current->uid;
1189}
1190
1191asmlinkage int sys_geteuid(void)
1192{
1193        /* Only we change this so SMP safe */
1194        return current->euid;
1195}
1196
1197asmlinkage int sys_getgid(void)
1198{
1199        /* Only we change this so SMP safe */
1200        return current->gid;
1201}
1202
1203asmlinkage int sys_getegid(void)
1204{
1205        /* Only we change this so SMP safe */
1206        return  current->egid;
1207}
1208
1209/*
1210 * This has been replaced by sys_setpriority.  Maybe it should be
1211 * moved into the arch dependent tree for those ports that require
1212 * it for backward compatibility?
1213 */
1214
1215asmlinkage int sys_nice(int increment)
1216{
1217        unsigned long newprio;
1218        int increase = 0;
1219
1220        /*
1221         *      Setpriority might change our priority at the same moment.
1222         *      We don't have to worry. Conceptually one call occurs first
1223         *      and we have a single winner.
1224         */
1225         
1226        newprio = increment;
1227        if (increment < 0) {
1228                if (!suser())
1229                        return -EPERM;
1230                newprio = -increment;
1231                increase = 1;
1232        }
1233
1234        if (newprio > 40)
1235                newprio = 40;
1236        /*
1237         * do a "normalization" of the priority (traditionally
1238         * unix nice values are -20..20, linux doesn't really
1239         * use that kind of thing, but uses the length of the
1240         * timeslice instead (default 150 msec). The rounding is
1241         * why we want to avoid negative values.
1242         */
1243        newprio = (newprio * DEF_PRIORITY + 10) / 20;
1244        increment = newprio;
1245        if (increase)
1246                increment = -increment;
1247        /*
1248         *      Current->priority can change between this point
1249         *      and the assignment. We are assigning not doing add/subs
1250         *      so thats ok. Conceptually a process might just instantaneously
1251         *      read the value we stomp over. I don't think that is an issue
1252         *      unless posix makes it one. If so we can loop on changes
1253         *      to current->priority.
1254         */
1255        newprio = current->priority - increment;
1256        if ((signed) newprio < 1)
1257                newprio = 1;
1258        if (newprio > DEF_PRIORITY*2)
1259                newprio = DEF_PRIORITY*2;
1260        current->priority = newprio;
1261        return 0;
1262}
1263
1264#endif
1265
1266static inline struct task_struct *find_process_by_pid(pid_t pid)
1267{
1268        if (pid)
1269                return find_task_by_pid(pid);
1270        else
1271                return current;
1272}
1273
1274static int setscheduler(pid_t pid, int policy, 
1275                        struct sched_param *param)
1276{
1277        struct sched_param lp;
1278        struct task_struct *p;
1279
1280        if (!param || pid < 0)
1281                return -EINVAL;
1282
1283        if (copy_from_user(&lp, param, sizeof(struct sched_param)))
1284                return -EFAULT;
1285
1286        p = find_process_by_pid(pid);
1287        if (!p)
1288                return -ESRCH;
1289                        
1290        if (policy < 0)
1291                policy = p->policy;
1292        else if (policy != SCHED_FIFO && policy != SCHED_RR &&
1293                 policy != SCHED_OTHER)
1294                return -EINVAL;
1295        
1296        /*
1297         * Valid priorities for SCHED_FIFO and SCHED_RR are 1..99, valid
1298         * priority for SCHED_OTHER is 0.
1299         */
1300        if (lp.sched_priority < 0 || lp.sched_priority > 99)
1301                return -EINVAL;
1302        if ((policy == SCHED_OTHER) != (lp.sched_priority == 0))
1303                return -EINVAL;
1304
1305        if ((policy == SCHED_FIFO || policy == SCHED_RR) && !suser())
1306                return -EPERM;
1307        if ((current->euid != p->euid) && (current->euid != p->uid) &&
1308            !suser())
1309                return -EPERM;
1310
1311        p->policy = policy;
1312        p->rt_priority = lp.sched_priority;
1313        spin_lock(&scheduler_lock);
1314        spin_lock_irq(&runqueue_lock);
1315        if (p->next_run)
1316                move_last_runqueue(p);
1317        spin_unlock_irq(&runqueue_lock);
1318        spin_unlock(&scheduler_lock);
1319        need_resched = 1;
1320        return 0;
1321}
1322
1323asmlinkage int sys_sched_setscheduler(pid_t pid, int policy, 
1324                                      struct sched_param *param)
1325{
1326        return setscheduler(pid, policy, param);
1327}
1328
1329asmlinkage int sys_sched_setparam(pid_t pid, struct sched_param *param)
1330{
1331        return setscheduler(pid, -1, param);
1332}
1333
1334asmlinkage int sys_sched_getscheduler(pid_t pid)
1335{
1336        struct task_struct *p;
1337
1338        if (pid < 0)
1339                return -EINVAL;
1340
1341        p = find_process_by_pid(pid);
1342        if (!p)
1343                return -ESRCH;
1344                        
1345        return p->policy;
1346}
1347
1348asmlinkage int sys_sched_getparam(pid_t pid, struct sched_param *param)
1349{
1350        struct task_struct *p;
1351        struct sched_param lp;
1352
1353        if (!param || pid < 0)
1354                return -EINVAL;
1355
1356        p = find_process_by_pid(pid);
1357        if (!p)
1358                return -ESRCH;
1359
1360        lp.sched_priority = p->rt_priority;
1361        return copy_to_user(param, &lp, sizeof(struct sched_param)) ? -EFAULT : 0;
1362}
1363
1364asmlinkage int sys_sched_yield(void)
1365{
1366        /*
1367         * This is not really right. We'd like to reschedule
1368         * just _once_ with this process having a zero count.
1369         */
1370        current->counter = 0;
1371        spin_lock(&scheduler_lock);
1372        spin_lock_irq(&runqueue_lock);
1373        move_last_runqueue(current);
1374        spin_unlock_irq(&runqueue_lock);
1375        spin_unlock(&scheduler_lock);
1376        need_resched = 1;
1377        return 0;
1378}
1379
1380asmlinkage int sys_sched_get_priority_max(int policy)
1381{
1382        int ret = -EINVAL;
1383
1384        switch (policy) {
1385        case SCHED_FIFO:
1386        case SCHED_RR:
1387                ret = 99;
1388                break;
1389        case SCHED_OTHER:
1390                ret = 0;
1391                break;
1392        }
1393        return ret;
1394}
1395
1396asmlinkage int sys_sched_get_priority_min(int policy)
1397{
1398        int ret = -EINVAL;
1399
1400        switch (policy) {
1401        case SCHED_FIFO:
1402        case SCHED_RR:
1403                ret = 1;
1404                break;
1405        case SCHED_OTHER:
1406                ret = 0;
1407        }
1408        return ret;
1409}
1410
1411asmlinkage int sys_sched_rr_get_interval(pid_t pid, struct timespec *interval)
1412{
1413        struct timespec t;
1414
1415        t.tv_sec = 0;
1416        t.tv_nsec = 150000;
1417        if (copy_to_user(interval, &t, sizeof(struct timespec)))
1418                return -EFAULT;
1419        return 0;
1420}
1421
1422/*
1423 * change timeval to jiffies, trying to avoid the 
1424 * most obvious overflows..
1425 */
1426static unsigned long timespectojiffies(struct timespec *value)
1427{
1428        unsigned long sec = (unsigned) value->tv_sec;
1429        long nsec = value->tv_nsec;
1430
1431        if (sec > (LONG_MAX / HZ))
1432                return LONG_MAX;
1433        nsec += 1000000000L / HZ - 1;
1434        nsec /= 1000000000L / HZ;
1435        return HZ * sec + nsec;
1436}
1437
1438static void jiffiestotimespec(unsigned long jiffies, struct timespec *value)
1439{
1440        value->tv_nsec = (jiffies % HZ) * (1000000000L / HZ);
1441        value->tv_sec = jiffies / HZ;
1442}
1443
1444asmlinkage int sys_nanosleep(struct timespec *rqtp, struct timespec *rmtp)
1445{
1446        struct timespec t;
1447        unsigned long expire;
1448
1449        if(copy_from_user(&t, rqtp, sizeof(struct timespec)))
1450                return -EFAULT;
1451
1452        if (t.tv_nsec >= 1000000000L || t.tv_nsec < 0 || t.tv_sec < 0)
1453                return -EINVAL;
1454
1455
1456        if (t.tv_sec == 0 && t.tv_nsec <= 2000000L &&
1457            current->policy != SCHED_OTHER)
1458        {
1459                /*
1460                 * Short delay requests up to 2 ms will be handled with
1461                 * high precision by a busy wait for all real-time processes.
1462                 *
1463                 * Its important on SMP not to do this holding locks.
1464                 */
1465                udelay((t.tv_nsec + 999) / 1000);
1466                return 0;
1467        }
1468
1469        expire = timespectojiffies(&t) + (t.tv_sec || t.tv_nsec) + jiffies;
1470
1471        current->timeout = expire;
1472        current->state = TASK_INTERRUPTIBLE;
1473        schedule();
1474
1475        if (expire > jiffies) {
1476                if (rmtp) {
1477                        jiffiestotimespec(expire - jiffies -
1478                                          (expire > jiffies + 1), &t);
1479                        if (copy_to_user(rmtp, &t, sizeof(struct timespec)))
1480                                return -EFAULT;
1481                }
1482                return -EINTR;
1483        }
1484        return 0;
1485}
1486
1487static void show_task(int nr,struct task_struct * p)
1488{
1489        unsigned long free = 0;
1490        static const char * stat_nam[] = { "R", "S", "D", "Z", "T", "W" };
1491
1492        printk("%-8s %3d ", p->comm, (p == current) ? -nr : nr);
1493        if (((unsigned) p->state) < sizeof(stat_nam)/sizeof(char *))
1494                printk(stat_nam[p->state]);
1495        else
1496                printk(" ");
1497#if ((~0UL) == 0xffffffff)
1498        if (p == current)
1499                printk(" current  ");
1500        else
1501                printk(" %08lX ", thread_saved_pc(&p->tss));
1502#else
1503        if (p == current)
1504                printk("   current task   ");
1505        else
1506                printk(" %016lx ", thread_saved_pc(&p->tss));
1507#endif
1508#if 0
1509        for (free = 1; free < PAGE_SIZE/sizeof(long) ; free++) {
1510                if (((unsigned long *)p->kernel_stack_page)[free])
1511                        break;
1512        }
1513#endif
1514        printk("%5lu %5d %6d ", free*sizeof(long), p->pid, p->p_pptr->pid);
1515        if (p->p_cptr)
1516                printk("%5d ", p->p_cptr->pid);
1517        else
1518                printk("      ");
1519        if (p->p_ysptr)
1520                printk("%7d", p->p_ysptr->pid);
1521        else
1522                printk("       ");
1523        if (p->p_osptr)
1524                printk(" %5d\n", p->p_osptr->pid);
1525        else
1526                printk("\n");
1527}
1528
1529void show_state(void)
1530{
1531        struct task_struct *p;
1532
1533#if ((~0UL) == 0xffffffff)
1534        printk("\n"
1535               "                         free                        sibling\n");
1536        printk("  task             PC    stack   pid father child younger older\n");
1537#else
1538        printk("\n"
1539               "                                 free                        sibling\n");
1540        printk("  task                 PC        stack   pid father child younger older\n");
1541#endif
1542        read_lock(&tasklist_lock);
1543        for_each_task(p)
1544                show_task((p->tarray_ptr - &task[0]),p);
1545        read_unlock(&tasklist_lock);
1546}
1547
1548__initfunc(void sched_init(void))
1549{
1550        /*
1551         *      We have to do a little magic to get the first
1552         *      process right in SMP mode.
1553         */
1554        int cpu=hard_smp_processor_id();
1555        int nr = NR_TASKS;
1556
1557        init_task.processor=cpu;
1558
1559        /* Init task array free list and pidhash table. */
1560        while(--nr > 0)
1561                add_free_taskslot(&task[nr]);
1562
1563        for(nr = 0; nr < PIDHASH_SZ; nr++)
1564                pidhash[nr] = NULL;
1565
1566        init_bh(TIMER_BH, timer_bh);
1567        init_bh(TQUEUE_BH, tqueue_bh);
1568        init_bh(IMMEDIATE_BH, immediate_bh);
1569}
1570
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.