4 * Copyright (C) 1991, 1992 Linus Torvalds
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.
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
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>
33 #include <linux/smp.h>
34 #include <linux/smp_lock.h>
35 #include <linux/init.h>
37 #include <asm/system.h>
39 #include <asm/uaccess.h>
40 #include <asm/pgtable.h>
41 #include <asm/mmu_context.h>
42 #include <asm/spinlock.h>
44 #include <linux/timex.h>
50 unsigned securebits
= SECUREBITS_DEFAULT
; /* systemwide security settings */
52 long tick
= (1000000 + HZ
/2) / HZ
; /* timer interrupt period */
54 /* The current time */
55 volatile struct timeval xtime
__attribute__ ((aligned (16)));
57 /* Don't completely fail for HZ > 500. */
58 int tickadj
= 500/HZ
? : 1; /* microsecs */
60 DECLARE_TASK_QUEUE(tq_timer
);
61 DECLARE_TASK_QUEUE(tq_immediate
);
62 DECLARE_TASK_QUEUE(tq_scheduler
);
65 * phase-lock loop variables
67 /* TIME_ERROR prevents overwriting the CMOS clock */
68 int time_state
= TIME_ERROR
; /* clock synchronization status */
69 int time_status
= STA_UNSYNC
; /* clock status bits */
70 long time_offset
= 0; /* time adjustment (us) */
71 long time_constant
= 2; /* pll time constant */
72 long time_tolerance
= MAXFREQ
; /* frequency tolerance (ppm) */
73 long time_precision
= 1; /* clock precision (us) */
74 long time_maxerror
= MAXPHASE
; /* maximum error (us) */
75 long time_esterror
= MAXPHASE
; /* estimated error (us) */
76 long time_phase
= 0; /* phase offset (scaled us) */
77 long time_freq
= ((1000000 + HZ
/2) % HZ
- HZ
/2) << SHIFT_USEC
; /* frequency offset (scaled ppm) */
78 long time_adj
= 0; /* tick adjust (scaled 1 / HZ) */
79 long time_reftime
= 0; /* time at last adjustment (s) */
82 long time_adjust_step
= 0;
85 unsigned long event
= 0;
87 extern int do_setitimer(int, struct itimerval
*, struct itimerval
*);
88 unsigned int * prof_buffer
= NULL
;
89 unsigned long prof_len
= 0;
90 unsigned long prof_shift
= 0;
92 extern void mem_use(void);
94 unsigned long volatile jiffies
=0;
97 * Init task must be ok at boot for the ix86 as we will check its signals
98 * via the SMP irq return path.
101 struct task_struct
* task
[NR_TASKS
] = {&init_task
, };
103 struct kernel_stat kstat
= { 0 };
105 void scheduling_functions_start_here(void) { }
107 static inline void add_to_runqueue(struct task_struct
* p
)
109 if (p
->policy
!= SCHED_OTHER
|| p
->counter
> current
->counter
+ 3)
112 (p
->prev_run
= init_task
.prev_run
)->next_run
= p
;
113 p
->next_run
= &init_task
;
114 init_task
.prev_run
= p
;
117 static inline void del_from_runqueue(struct task_struct
* p
)
119 struct task_struct
*next
= p
->next_run
;
120 struct task_struct
*prev
= p
->prev_run
;
123 next
->prev_run
= prev
;
124 prev
->next_run
= next
;
129 static inline void move_last_runqueue(struct task_struct
* p
)
131 struct task_struct
*next
= p
->next_run
;
132 struct task_struct
*prev
= p
->prev_run
;
134 /* remove from list */
135 next
->prev_run
= prev
;
136 prev
->next_run
= next
;
137 /* add back to list */
138 p
->next_run
= &init_task
;
139 prev
= init_task
.prev_run
;
140 init_task
.prev_run
= p
;
145 static inline void move_first_runqueue(struct task_struct
* p
)
147 struct task_struct
*next
= p
->next_run
;
148 struct task_struct
*prev
= p
->prev_run
;
150 /* remove from list */
151 next
->prev_run
= prev
;
152 prev
->next_run
= next
;
153 /* add back to list */
154 p
->prev_run
= &init_task
;
155 next
= init_task
.next_run
;
156 init_task
.next_run
= p
;
162 * The tasklist_lock protects the linked list of processes.
164 * The scheduler lock is protecting against multiple entry
165 * into the scheduling code, and doesn't need to worry
166 * about interrupts (because interrupts cannot call the
169 * The run-queue lock locks the parts that actually access
170 * and change the run-queues, and have to be interrupt-safe.
172 spinlock_t scheduler_lock
= SPIN_LOCK_UNLOCKED
; /* should be acquired first */
173 spinlock_t runqueue_lock
= SPIN_LOCK_UNLOCKED
; /* second */
174 rwlock_t tasklist_lock
= RW_LOCK_UNLOCKED
; /* third */
177 * Wake up a process. Put it on the run-queue if it's not
178 * already there. The "current" process is always on the
179 * run-queue (except when the actual re-schedule is in
180 * progress), and as such you're allowed to do the simpler
181 * "current->state = TASK_RUNNING" to mark yourself runnable
182 * without the overhead of this.
184 inline void wake_up_process(struct task_struct
* p
)
188 spin_lock_irqsave(&runqueue_lock
, flags
);
189 p
->state
= TASK_RUNNING
;
192 spin_unlock_irqrestore(&runqueue_lock
, flags
);
195 static void process_timeout(unsigned long __data
)
197 struct task_struct
* p
= (struct task_struct
*) __data
;
204 * This is the function that decides how desirable a process is..
205 * You can weigh different processes against each other depending
206 * on what CPU they've run on lately etc to try to handle cache
207 * and TLB miss penalties.
210 * -1000: never select this
211 * 0: out of time, recalculate counters (but it might still be
213 * +ve: "goodness" value (the larger, the better)
214 * +1000: realtime process, select this.
216 static inline int goodness(struct task_struct
* p
, struct task_struct
* prev
, int this_cpu
)
218 int policy
= p
->policy
;
221 if (policy
& SCHED_YIELD
) {
222 p
->policy
= policy
& ~SCHED_YIELD
;
227 * Realtime process, select the first one on the
228 * runqueue (taking priorities within processes
231 if (policy
!= SCHED_OTHER
)
232 return 1000 + p
->rt_priority
;
235 * Give the process a first-approximation goodness value
236 * according to the number of clock-ticks it has left.
238 * Don't do any other calculations if the time slice is
245 /* Give a largish advantage to the same processor... */
246 /* (this is equivalent to penalizing other processors) */
247 if (p
->processor
== this_cpu
)
248 weight
+= PROC_CHANGE_PENALTY
;
251 /* .. and a slight advantage to the current thread */
252 if (p
->mm
== prev
->mm
)
254 weight
+= p
->priority
;
265 #define TVN_SIZE (1 << TVN_BITS)
266 #define TVR_SIZE (1 << TVR_BITS)
267 #define TVN_MASK (TVN_SIZE - 1)
268 #define TVR_MASK (TVR_SIZE - 1)
272 struct timer_list
*vec
[TVN_SIZE
];
275 struct timer_vec_root
{
277 struct timer_list
*vec
[TVR_SIZE
];
280 static struct timer_vec tv5
= { 0 };
281 static struct timer_vec tv4
= { 0 };
282 static struct timer_vec tv3
= { 0 };
283 static struct timer_vec tv2
= { 0 };
284 static struct timer_vec_root tv1
= { 0 };
286 static struct timer_vec
* const tvecs
[] = {
287 (struct timer_vec
*)&tv1
, &tv2
, &tv3
, &tv4
, &tv5
290 #define NOOF_TVECS (sizeof(tvecs) / sizeof(tvecs[0]))
292 static unsigned long timer_jiffies
= 0;
294 static inline void insert_timer(struct timer_list
*timer
,
295 struct timer_list
**vec
, int idx
)
297 if ((timer
->next
= vec
[idx
]))
298 vec
[idx
]->prev
= timer
;
300 timer
->prev
= (struct timer_list
*)&vec
[idx
];
303 static inline void internal_add_timer(struct timer_list
*timer
)
306 * must be cli-ed when calling this
308 unsigned long expires
= timer
->expires
;
309 unsigned long idx
= expires
- timer_jiffies
;
311 if (idx
< TVR_SIZE
) {
312 int i
= expires
& TVR_MASK
;
313 insert_timer(timer
, tv1
.vec
, i
);
314 } else if (idx
< 1 << (TVR_BITS
+ TVN_BITS
)) {
315 int i
= (expires
>> TVR_BITS
) & TVN_MASK
;
316 insert_timer(timer
, tv2
.vec
, i
);
317 } else if (idx
< 1 << (TVR_BITS
+ 2 * TVN_BITS
)) {
318 int i
= (expires
>> (TVR_BITS
+ TVN_BITS
)) & TVN_MASK
;
319 insert_timer(timer
, tv3
.vec
, i
);
320 } else if (idx
< 1 << (TVR_BITS
+ 3 * TVN_BITS
)) {
321 int i
= (expires
>> (TVR_BITS
+ 2 * TVN_BITS
)) & TVN_MASK
;
322 insert_timer(timer
, tv4
.vec
, i
);
323 } else if (expires
< timer_jiffies
) {
324 /* can happen if you add a timer with expires == jiffies,
325 * or you set a timer to go off in the past
327 insert_timer(timer
, tv1
.vec
, tv1
.index
);
328 } else if (idx
< 0xffffffffUL
) {
329 int i
= (expires
>> (TVR_BITS
+ 3 * TVN_BITS
)) & TVN_MASK
;
330 insert_timer(timer
, tv5
.vec
, i
);
332 /* Can only get here on architectures with 64-bit jiffies */
333 timer
->next
= timer
->prev
= timer
;
337 spinlock_t timerlist_lock
= SPIN_LOCK_UNLOCKED
;
339 void add_timer(struct timer_list
*timer
)
343 spin_lock_irqsave(&timerlist_lock
, flags
);
344 internal_add_timer(timer
);
345 spin_unlock_irqrestore(&timerlist_lock
, flags
);
348 static inline int detach_timer(struct timer_list
*timer
)
350 struct timer_list
*prev
= timer
->prev
;
352 struct timer_list
*next
= timer
->next
;
361 void mod_timer(struct timer_list
*timer
, unsigned long expires
)
365 spin_lock_irqsave(&timerlist_lock
, flags
);
366 timer
->expires
= expires
;
368 internal_add_timer(timer
);
369 spin_unlock_irqrestore(&timerlist_lock
, flags
);
372 int del_timer(struct timer_list
* timer
)
377 spin_lock_irqsave(&timerlist_lock
, flags
);
378 ret
= detach_timer(timer
);
379 timer
->next
= timer
->prev
= 0;
380 spin_unlock_irqrestore(&timerlist_lock
, flags
);
386 #define idle_task (task[cpu_number_map[this_cpu]])
387 #define can_schedule(p) (!(p)->has_cpu)
391 #define idle_task (&init_task)
392 #define can_schedule(p) (1)
397 * 'schedule()' is the scheduler function. It's a very simple and nice
398 * scheduler: it's not perfect, but certainly works for most things.
400 * The goto is "interesting".
402 * NOTE!! Task 0 is the 'idle' task, which gets called when no other
403 * tasks can run. It can not be killed, and it cannot sleep. The 'state'
404 * information in task[0] is never used.
406 asmlinkage
void schedule(void)
409 struct task_struct
* prev
, * next
;
410 unsigned long timeout
;
415 this_cpu
= smp_processor_id();
417 goto scheduling_in_interrupt
;
418 release_kernel_lock(prev
, this_cpu
, lock_depth
);
419 if (bh_active
& bh_mask
)
422 spin_lock(&scheduler_lock
);
423 spin_lock_irq(&runqueue_lock
);
425 /* move an exhausted RR process to be last.. */
426 if (!prev
->counter
&& prev
->policy
== SCHED_RR
) {
427 prev
->counter
= prev
->priority
;
428 move_last_runqueue(prev
);
431 switch (prev
->state
) {
432 case TASK_INTERRUPTIBLE
:
433 if (signal_pending(prev
))
435 timeout
= prev
->timeout
;
436 if (timeout
&& (timeout
<= jiffies
)) {
440 prev
->state
= TASK_RUNNING
;
444 del_from_runqueue(prev
);
448 struct task_struct
* p
= init_task
.next_run
;
451 * Note how we can enable interrupts here, even
452 * though interrupts can add processes to the run-
453 * queue. This is because any new processes will
454 * be added to the front of the queue, so "p" above
455 * is a safe starting point.
456 * run-queue deletion and re-ordering is protected by
459 spin_unlock_irq(&runqueue_lock
);
465 * Note! there may appear new tasks on the run-queue during this, as
466 * interrupts are enabled. However, they will be put on front of the
467 * list, so our list starting at "p" is essentially fixed.
469 /* this is the scheduler proper: */
473 while (p
!= &init_task
) {
474 if (can_schedule(p
)) {
475 int weight
= goodness(p
, prev
, this_cpu
);
477 c
= weight
, next
= p
;
482 /* Do we need to re-calculate counters? */
484 struct task_struct
*p
;
485 read_lock(&tasklist_lock
);
487 p
->counter
= (p
->counter
>> 1) + p
->priority
;
488 read_unlock(&tasklist_lock
);
495 next
->processor
= this_cpu
;
499 struct timer_list timer
;
501 kstat
.context_swtch
++;
504 timer
.expires
= timeout
;
505 timer
.data
= (unsigned long) prev
;
506 timer
.function
= process_timeout
;
509 get_mmu_context(next
);
510 switch_to(prev
,next
);
515 spin_unlock(&scheduler_lock
);
517 reacquire_kernel_lock(prev
, smp_processor_id(), lock_depth
);
520 scheduling_in_interrupt
:
521 printk("Scheduling in interrupt\n");
526 rwlock_t waitqueue_lock
= RW_LOCK_UNLOCKED
;
529 * wake_up doesn't wake up stopped processes - they have to be awakened
530 * with signals or similar.
532 * Note that we only need a read lock for the wait queue (and thus do not
533 * have to protect against interrupts), as the actual removal from the
534 * queue is handled by the process itself.
536 void __wake_up(struct wait_queue
**q
, unsigned int mode
)
538 struct wait_queue
*next
;
540 read_lock(&waitqueue_lock
);
541 if (q
&& (next
= *q
)) {
542 struct wait_queue
*head
;
544 head
= WAIT_QUEUE_HEAD(q
);
545 while (next
!= head
) {
546 struct task_struct
*p
= next
->task
;
552 read_unlock(&waitqueue_lock
);
556 * Semaphores are implemented using a two-way counter:
557 * The "count" variable is decremented for each process
558 * that tries to sleep, while the "waking" variable is
559 * incremented when the "up()" code goes to wake up waiting
562 * Notably, the inline "up()" and "down()" functions can
563 * efficiently test if they need to do any extra work (up
564 * needs to do something only if count was negative before
565 * the increment operation.
567 * waking_non_zero() (from asm/semaphore.h) must execute
570 * When __up() is called, the count was negative before
571 * incrementing it, and we need to wake up somebody.
573 * This routine adds one to the count of processes that need to
574 * wake up and exit. ALL waiting processes actually wake up but
575 * only the one that gets to the "waking" field first will gate
576 * through and acquire the semaphore. The others will go back
579 * Note that these functions are only called when there is
580 * contention on the lock, and as such all this is the
581 * "non-critical" part of the whole semaphore business. The
582 * critical part is the inline stuff in <asm/semaphore.h>
583 * where we want to avoid any extra jumps and calls.
585 void __up(struct semaphore
*sem
)
592 * Perform the "down" function. Return zero for semaphore acquired,
593 * return negative for signalled out of the function.
595 * If called from __down, the return is ignored and the wait loop is
596 * not interruptible. This means that a task waiting on a semaphore
597 * using "down()" cannot be killed until someone does an "up()" on
600 * If called from __down_interruptible, the return value gets checked
601 * upon return. If the return value is negative then the task continues
602 * with the negative value in the return register (it can be tested by
605 * Either form may be used in conjunction with "up()".
608 static inline int __do_down(struct semaphore
* sem
, int task_state
)
610 struct task_struct
*tsk
= current
;
611 struct wait_queue wait
= { tsk
, NULL
};
614 tsk
->state
= task_state
;
615 add_wait_queue(&sem
->wait
, &wait
);
618 * Ok, we're set up. sem->count is known to be less than zero
621 * We can let go the lock for purposes of waiting.
622 * We re-acquire it after awaking so as to protect
623 * all semaphore operations.
625 * If "up()" is called before we call waking_non_zero() then
626 * we will catch it right away. If it is called later then
627 * we will have to go through a wakeup cycle to catch it.
629 * Multiple waiters contend for the semaphore lock to see
630 * who gets to gate through and who has to wait some more.
633 if (waking_non_zero(sem
)) /* are we waking up? */
634 break; /* yes, exit loop */
636 if (task_state
== TASK_INTERRUPTIBLE
&& signal_pending(tsk
)) {
637 ret
= -EINTR
; /* interrupted */
638 atomic_inc(&sem
->count
); /* give up on down operation */
643 tsk
->state
= task_state
;
646 tsk
->state
= TASK_RUNNING
;
647 remove_wait_queue(&sem
->wait
, &wait
);
651 void __down(struct semaphore
* sem
)
653 __do_down(sem
,TASK_UNINTERRUPTIBLE
);
656 int __down_interruptible(struct semaphore
* sem
)
658 return __do_down(sem
,TASK_INTERRUPTIBLE
);
662 static void FASTCALL(__sleep_on(struct wait_queue
**p
, int state
));
663 static void __sleep_on(struct wait_queue
**p
, int state
)
666 struct wait_queue wait
;
668 current
->state
= state
;
670 write_lock_irqsave(&waitqueue_lock
, flags
);
671 __add_wait_queue(p
, &wait
);
672 write_unlock(&waitqueue_lock
);
674 write_lock_irq(&waitqueue_lock
);
675 __remove_wait_queue(p
, &wait
);
676 write_unlock_irqrestore(&waitqueue_lock
, flags
);
679 void interruptible_sleep_on(struct wait_queue
**p
)
681 __sleep_on(p
,TASK_INTERRUPTIBLE
);
684 void sleep_on(struct wait_queue
**p
)
686 __sleep_on(p
,TASK_UNINTERRUPTIBLE
);
689 void scheduling_functions_end_here(void) { }
691 static inline void cascade_timers(struct timer_vec
*tv
)
693 /* cascade all the timers from tv up one level */
694 struct timer_list
*timer
;
695 timer
= tv
->vec
[tv
->index
];
697 * We are removing _all_ timers from the list, so we don't have to
698 * detach them individually, just clear the list afterwards.
701 struct timer_list
*tmp
= timer
;
703 internal_add_timer(tmp
);
705 tv
->vec
[tv
->index
] = NULL
;
706 tv
->index
= (tv
->index
+ 1) & TVN_MASK
;
709 static inline void run_timer_list(void)
711 spin_lock_irq(&timerlist_lock
);
712 while ((long)(jiffies
- timer_jiffies
) >= 0) {
713 struct timer_list
*timer
;
717 cascade_timers(tvecs
[n
]);
718 } while (tvecs
[n
]->index
== 1 && ++n
< NOOF_TVECS
);
720 while ((timer
= tv1
.vec
[tv1
.index
])) {
721 void (*fn
)(unsigned long) = timer
->function
;
722 unsigned long data
= timer
->data
;
724 timer
->next
= timer
->prev
= NULL
;
725 spin_unlock_irq(&timerlist_lock
);
727 spin_lock_irq(&timerlist_lock
);
730 tv1
.index
= (tv1
.index
+ 1) & TVR_MASK
;
732 spin_unlock_irq(&timerlist_lock
);
736 static inline void run_old_timers(void)
738 struct timer_struct
*tp
;
741 for (mask
= 1, tp
= timer_table
+0 ; mask
; tp
++,mask
+= mask
) {
742 if (mask
> timer_active
)
744 if (!(mask
& timer_active
))
746 if (tp
->expires
> jiffies
)
748 timer_active
&= ~mask
;
754 spinlock_t tqueue_lock
;
758 run_task_queue(&tq_timer
);
761 void immediate_bh(void)
763 run_task_queue(&tq_immediate
);
766 unsigned long timer_active
= 0;
767 struct timer_struct timer_table
[32];
770 * Hmm.. Changed this, as the GNU make sources (load.c) seems to
771 * imply that avenrun[] is the standard name for this kind of thing.
772 * Nothing else seems to be standardized: the fractional size etc
773 * all seem to differ on different machines.
775 unsigned long avenrun
[3] = { 0,0,0 };
778 * Nr of active tasks - counted in fixed-point numbers
780 static unsigned long count_active_tasks(void)
782 struct task_struct
*p
;
783 unsigned long nr
= 0;
785 read_lock(&tasklist_lock
);
788 (p
->state
== TASK_RUNNING
||
789 p
->state
== TASK_UNINTERRUPTIBLE
||
790 p
->state
== TASK_SWAPPING
))
793 read_unlock(&tasklist_lock
);
797 static inline void calc_load(unsigned long ticks
)
799 unsigned long active_tasks
; /* fixed-point */
800 static int count
= LOAD_FREQ
;
805 active_tasks
= count_active_tasks();
806 CALC_LOAD(avenrun
[0], EXP_1
, active_tasks
);
807 CALC_LOAD(avenrun
[1], EXP_5
, active_tasks
);
808 CALC_LOAD(avenrun
[2], EXP_15
, active_tasks
);
813 * this routine handles the overflow of the microsecond field
815 * The tricky bits of code to handle the accurate clock support
816 * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
817 * They were originally developed for SUN and DEC kernels.
818 * All the kudos should go to Dave for this stuff.
821 static void second_overflow(void)
825 /* Bump the maxerror field */
826 time_maxerror
+= time_tolerance
>> SHIFT_USEC
;
827 if ( time_maxerror
> MAXPHASE
)
828 time_maxerror
= MAXPHASE
;
831 * Leap second processing. If in leap-insert state at
832 * the end of the day, the system clock is set back one
833 * second; if in leap-delete state, the system clock is
834 * set ahead one second. The microtime() routine or
835 * external clock driver will insure that reported time
836 * is always monotonic. The ugly divides should be
839 switch (time_state
) {
842 if (time_status
& STA_INS
)
843 time_state
= TIME_INS
;
844 else if (time_status
& STA_DEL
)
845 time_state
= TIME_DEL
;
849 if (xtime
.tv_sec
% 86400 == 0) {
851 time_state
= TIME_OOP
;
852 printk("Clock: inserting leap second 23:59:60 UTC\n");
857 if ((xtime
.tv_sec
+ 1) % 86400 == 0) {
859 time_state
= TIME_WAIT
;
860 printk("Clock: deleting leap second 23:59:59 UTC\n");
865 time_state
= TIME_WAIT
;
869 if (!(time_status
& (STA_INS
| STA_DEL
)))
870 time_state
= TIME_OK
;
874 * Compute the phase adjustment for the next second. In
875 * PLL mode, the offset is reduced by a fixed factor
876 * times the time constant. In FLL mode the offset is
877 * used directly. In either mode, the maximum phase
878 * adjustment for each second is clamped so as to spread
879 * the adjustment over not more than the number of
880 * seconds between updates.
882 if (time_offset
< 0) {
883 ltemp
= -time_offset
;
884 if (!(time_status
& STA_FLL
))
885 ltemp
>>= SHIFT_KG
+ time_constant
;
886 if (ltemp
> (MAXPHASE
/ MINSEC
) << SHIFT_UPDATE
)
887 ltemp
= (MAXPHASE
/ MINSEC
) << SHIFT_UPDATE
;
888 time_offset
+= ltemp
;
889 time_adj
= -ltemp
<< (SHIFT_SCALE
- SHIFT_HZ
- SHIFT_UPDATE
);
892 if (!(time_status
& STA_FLL
))
893 ltemp
>>= SHIFT_KG
+ time_constant
;
894 if (ltemp
> (MAXPHASE
/ MINSEC
) << SHIFT_UPDATE
)
895 ltemp
= (MAXPHASE
/ MINSEC
) << SHIFT_UPDATE
;
896 time_offset
-= ltemp
;
897 time_adj
= ltemp
<< (SHIFT_SCALE
- SHIFT_HZ
- SHIFT_UPDATE
);
901 * Compute the frequency estimate and additional phase
902 * adjustment due to frequency error for the next
903 * second. When the PPS signal is engaged, gnaw on the
904 * watchdog counter and update the frequency computed by
905 * the pll and the PPS signal.
908 if (pps_valid
== PPS_VALID
) {
909 pps_jitter
= MAXTIME
;
910 pps_stabil
= MAXFREQ
;
911 time_status
&= ~(STA_PPSSIGNAL
| STA_PPSJITTER
|
912 STA_PPSWANDER
| STA_PPSERROR
);
914 ltemp
= time_freq
+ pps_freq
;
916 time_adj
-= -ltemp
>>
917 (SHIFT_USEC
+ SHIFT_HZ
- SHIFT_SCALE
);
920 (SHIFT_USEC
+ SHIFT_HZ
- SHIFT_SCALE
);
923 /* compensate for (HZ==100) != 128. Add 25% to get 125; => only 3% error */
925 time_adj
-= -time_adj
>> 2;
927 time_adj
+= time_adj
>> 2;
931 /* in the NTP reference this is called "hardclock()" */
932 static void update_wall_time_one_tick(void)
935 * Advance the phase, once it gets to one microsecond, then
936 * advance the tick more.
938 time_phase
+= time_adj
;
939 if (time_phase
<= -FINEUSEC
) {
940 long ltemp
= -time_phase
>> SHIFT_SCALE
;
941 time_phase
+= ltemp
<< SHIFT_SCALE
;
942 xtime
.tv_usec
+= tick
+ time_adjust_step
- ltemp
;
944 else if (time_phase
>= FINEUSEC
) {
945 long ltemp
= time_phase
>> SHIFT_SCALE
;
946 time_phase
-= ltemp
<< SHIFT_SCALE
;
947 xtime
.tv_usec
+= tick
+ time_adjust_step
+ ltemp
;
949 xtime
.tv_usec
+= tick
+ time_adjust_step
;
952 /* We are doing an adjtime thing.
954 * Modify the value of the tick for next time.
955 * Note that a positive delta means we want the clock
956 * to run fast. This means that the tick should be bigger
958 * Limit the amount of the step for *next* tick to be
959 * in the range -tickadj .. +tickadj
961 if (time_adjust
> tickadj
)
962 time_adjust_step
= tickadj
;
963 else if (time_adjust
< -tickadj
)
964 time_adjust_step
= -tickadj
;
966 time_adjust_step
= time_adjust
;
968 /* Reduce by this step the amount of time left */
969 time_adjust
-= time_adjust_step
;
972 time_adjust_step
= 0;
976 * Using a loop looks inefficient, but "ticks" is
977 * usually just one (we shouldn't be losing ticks,
978 * we're doing this this way mainly for interrupt
979 * latency reasons, not because we think we'll
980 * have lots of lost timer ticks
982 static void update_wall_time(unsigned long ticks
)
986 update_wall_time_one_tick();
989 if (xtime
.tv_usec
>= 1000000) {
990 xtime
.tv_usec
-= 1000000;
996 static inline void do_process_times(struct task_struct
*p
,
997 unsigned long user
, unsigned long system
)
1001 psecs
= (p
->times
.tms_utime
+= user
);
1002 psecs
+= (p
->times
.tms_stime
+= system
);
1003 if (psecs
/ HZ
> p
->rlim
[RLIMIT_CPU
].rlim_cur
) {
1004 /* Send SIGXCPU every second.. */
1006 send_sig(SIGXCPU
, p
, 1);
1007 /* and SIGKILL when we go over max.. */
1008 if (psecs
/ HZ
> p
->rlim
[RLIMIT_CPU
].rlim_max
)
1009 send_sig(SIGKILL
, p
, 1);
1013 static inline void do_it_virt(struct task_struct
* p
, unsigned long ticks
)
1015 unsigned long it_virt
= p
->it_virt_value
;
1018 if (it_virt
<= ticks
) {
1019 it_virt
= ticks
+ p
->it_virt_incr
;
1020 send_sig(SIGVTALRM
, p
, 1);
1022 p
->it_virt_value
= it_virt
- ticks
;
1026 static inline void do_it_prof(struct task_struct
* p
, unsigned long ticks
)
1028 unsigned long it_prof
= p
->it_prof_value
;
1031 if (it_prof
<= ticks
) {
1032 it_prof
= ticks
+ p
->it_prof_incr
;
1033 send_sig(SIGPROF
, p
, 1);
1035 p
->it_prof_value
= it_prof
- ticks
;
1039 void update_one_process(struct task_struct
*p
,
1040 unsigned long ticks
, unsigned long user
, unsigned long system
, int cpu
)
1042 p
->per_cpu_utime
[cpu
] += user
;
1043 p
->per_cpu_stime
[cpu
] += system
;
1044 do_process_times(p
, user
, system
);
1045 do_it_virt(p
, user
);
1046 do_it_prof(p
, ticks
);
1049 static void update_process_times(unsigned long ticks
, unsigned long system
)
1052 * SMP does this on a per-CPU basis elsewhere
1055 struct task_struct
* p
= current
;
1056 unsigned long user
= ticks
- system
;
1058 p
->counter
-= ticks
;
1059 if (p
->counter
< 0) {
1063 if (p
->priority
< DEF_PRIORITY
)
1064 kstat
.cpu_nice
+= user
;
1066 kstat
.cpu_user
+= user
;
1067 kstat
.cpu_system
+= system
;
1069 update_one_process(p
, ticks
, user
, system
, 0);
1073 volatile unsigned long lost_ticks
= 0;
1074 static unsigned long lost_ticks_system
= 0;
1076 static inline void update_times(void)
1078 unsigned long ticks
;
1079 unsigned long flags
;
1088 unsigned long system
;
1089 system
= xchg(&lost_ticks_system
, 0);
1092 update_wall_time(ticks
);
1093 restore_flags(flags
);
1095 update_process_times(ticks
, system
);
1098 restore_flags(flags
);
1101 static void timer_bh(void)
1108 void do_timer(struct pt_regs
* regs
)
1110 (*(unsigned long *)&jiffies
)++;
1113 if (!user_mode(regs
))
1114 lost_ticks_system
++;
1122 * For backwards compatibility? This can be done in libc so Alpha
1123 * and all newer ports shouldn't need it.
1125 asmlinkage
unsigned int sys_alarm(unsigned int seconds
)
1127 struct itimerval it_new
, it_old
;
1128 unsigned int oldalarm
;
1130 it_new
.it_interval
.tv_sec
= it_new
.it_interval
.tv_usec
= 0;
1131 it_new
.it_value
.tv_sec
= seconds
;
1132 it_new
.it_value
.tv_usec
= 0;
1133 do_setitimer(ITIMER_REAL
, &it_new
, &it_old
);
1134 oldalarm
= it_old
.it_value
.tv_sec
;
1135 /* ehhh.. We can't return 0 if we have an alarm pending.. */
1136 /* And we'd better return too much than too little anyway */
1137 if (it_old
.it_value
.tv_usec
)
1143 * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
1144 * should be moved into arch/i386 instead?
1147 asmlinkage
int sys_getpid(void)
1149 /* This is SMP safe - current->pid doesn't change */
1150 return current
->pid
;
1154 * This is not strictly SMP safe: p_opptr could change
1155 * from under us. However, rather than getting any lock
1156 * we can use an optimistic algorithm: get the parent
1157 * pid, and go back and check that the parent is still
1158 * the same. If it has changed (which is extremely unlikely
1159 * indeed), we just try again..
1161 * NOTE! This depends on the fact that even if we _do_
1162 * get an old value of "parent", we can happily dereference
1163 * the pointer: we just can't necessarily trust the result
1164 * until we know that the parent pointer is valid.
1166 * The "mb()" macro is a memory barrier - a synchronizing
1167 * event. It also makes sure that gcc doesn't optimize
1168 * away the necessary memory references.. The barrier doesn't
1169 * have to have all that strong semantics: on x86 we don't
1170 * really require a synchronizing instruction, for example.
1171 * The barrier is more important for code generation than
1172 * for any real memory ordering semantics (even if there is
1173 * a small window for a race, using the old pointer is
1174 * harmless for a while).
1176 asmlinkage
int sys_getppid(void)
1179 struct task_struct
* me
= current
;
1180 struct task_struct
* parent
;
1182 parent
= me
->p_opptr
;
1187 struct task_struct
*old
= parent
;
1189 parent
= me
->p_opptr
;
1199 asmlinkage
int sys_getuid(void)
1201 /* Only we change this so SMP safe */
1202 return current
->uid
;
1205 asmlinkage
int sys_geteuid(void)
1207 /* Only we change this so SMP safe */
1208 return current
->euid
;
1211 asmlinkage
int sys_getgid(void)
1213 /* Only we change this so SMP safe */
1214 return current
->gid
;
1217 asmlinkage
int sys_getegid(void)
1219 /* Only we change this so SMP safe */
1220 return current
->egid
;
1224 * This has been replaced by sys_setpriority. Maybe it should be
1225 * moved into the arch dependent tree for those ports that require
1226 * it for backward compatibility?
1229 asmlinkage
int sys_nice(int increment
)
1231 unsigned long newprio
;
1235 * Setpriority might change our priority at the same moment.
1236 * We don't have to worry. Conceptually one call occurs first
1237 * and we have a single winner.
1240 newprio
= increment
;
1241 if (increment
< 0) {
1242 if (!capable(CAP_SYS_NICE
))
1244 newprio
= -increment
;
1251 * do a "normalization" of the priority (traditionally
1252 * Unix nice values are -20 to 20; Linux doesn't really
1253 * use that kind of thing, but uses the length of the
1254 * timeslice instead (default 150 ms). The rounding is
1255 * why we want to avoid negative values.
1257 newprio
= (newprio
* DEF_PRIORITY
+ 10) / 20;
1258 increment
= newprio
;
1260 increment
= -increment
;
1262 * Current->priority can change between this point
1263 * and the assignment. We are assigning not doing add/subs
1264 * so thats ok. Conceptually a process might just instantaneously
1265 * read the value we stomp over. I don't think that is an issue
1266 * unless posix makes it one. If so we can loop on changes
1267 * to current->priority.
1269 newprio
= current
->priority
- increment
;
1270 if ((signed) newprio
< 1)
1272 if (newprio
> DEF_PRIORITY
*2)
1273 newprio
= DEF_PRIORITY
*2;
1274 current
->priority
= newprio
;
1280 static inline struct task_struct
*find_process_by_pid(pid_t pid
)
1282 struct task_struct
*tsk
= current
;
1285 tsk
= find_task_by_pid(pid
);
1289 static int setscheduler(pid_t pid
, int policy
,
1290 struct sched_param
*param
)
1292 struct sched_param lp
;
1293 struct task_struct
*p
;
1297 if (!param
|| pid
< 0)
1301 if (copy_from_user(&lp
, param
, sizeof(struct sched_param
)))
1305 * We play safe to avoid deadlocks.
1307 spin_lock_irq(&scheduler_lock
);
1308 spin_lock(&runqueue_lock
);
1309 read_lock(&tasklist_lock
);
1311 p
= find_process_by_pid(pid
);
1321 if (policy
!= SCHED_FIFO
&& policy
!= SCHED_RR
&&
1322 policy
!= SCHED_OTHER
)
1327 * Valid priorities for SCHED_FIFO and SCHED_RR are 1..99, valid
1328 * priority for SCHED_OTHER is 0.
1331 if (lp
.sched_priority
< 0 || lp
.sched_priority
> 99)
1333 if ((policy
== SCHED_OTHER
) != (lp
.sched_priority
== 0))
1337 if ((policy
== SCHED_FIFO
|| policy
== SCHED_RR
) &&
1338 !capable(CAP_SYS_NICE
))
1340 if ((current
->euid
!= p
->euid
) && (current
->euid
!= p
->uid
) &&
1341 !capable(CAP_SYS_NICE
))
1346 p
->rt_priority
= lp
.sched_priority
;
1348 move_first_runqueue(p
);
1353 read_unlock(&tasklist_lock
);
1354 spin_unlock(&runqueue_lock
);
1355 spin_unlock_irq(&scheduler_lock
);
1361 asmlinkage
int sys_sched_setscheduler(pid_t pid
, int policy
,
1362 struct sched_param
*param
)
1364 return setscheduler(pid
, policy
, param
);
1367 asmlinkage
int sys_sched_setparam(pid_t pid
, struct sched_param
*param
)
1369 return setscheduler(pid
, -1, param
);
1372 asmlinkage
int sys_sched_getscheduler(pid_t pid
)
1374 struct task_struct
*p
;
1381 read_lock(&tasklist_lock
);
1384 p
= find_process_by_pid(pid
);
1391 read_unlock(&tasklist_lock
);
1397 asmlinkage
int sys_sched_getparam(pid_t pid
, struct sched_param
*param
)
1399 struct task_struct
*p
;
1400 struct sched_param lp
;
1404 if (!param
|| pid
< 0)
1407 read_lock(&tasklist_lock
);
1408 p
= find_process_by_pid(pid
);
1412 lp
.sched_priority
= p
->rt_priority
;
1413 read_unlock(&tasklist_lock
);
1416 * This one might sleep, we cannot do it with a spinlock held ...
1418 retval
= copy_to_user(param
, &lp
, sizeof(*param
)) ? -EFAULT
: 0;
1424 read_unlock(&tasklist_lock
);
1428 asmlinkage
int sys_sched_yield(void)
1430 spin_lock(&scheduler_lock
);
1431 spin_lock_irq(&runqueue_lock
);
1432 current
->policy
|= SCHED_YIELD
;
1433 move_last_runqueue(current
);
1434 spin_unlock_irq(&runqueue_lock
);
1435 spin_unlock(&scheduler_lock
);
1440 asmlinkage
int sys_sched_get_priority_max(int policy
)
1456 asmlinkage
int sys_sched_get_priority_min(int policy
)
1471 asmlinkage
int sys_sched_rr_get_interval(pid_t pid
, struct timespec
*interval
)
1477 if (copy_to_user(interval
, &t
, sizeof(struct timespec
)))
1482 asmlinkage
int sys_nanosleep(struct timespec
*rqtp
, struct timespec
*rmtp
)
1485 unsigned long expire
;
1487 if(copy_from_user(&t
, rqtp
, sizeof(struct timespec
)))
1490 if (t
.tv_nsec
>= 1000000000L || t
.tv_nsec
< 0 || t
.tv_sec
< 0)
1494 if (t
.tv_sec
== 0 && t
.tv_nsec
<= 2000000L &&
1495 current
->policy
!= SCHED_OTHER
)
1498 * Short delay requests up to 2 ms will be handled with
1499 * high precision by a busy wait for all real-time processes.
1501 * Its important on SMP not to do this holding locks.
1503 udelay((t
.tv_nsec
+ 999) / 1000);
1507 expire
= timespec_to_jiffies(&t
) + (t
.tv_sec
|| t
.tv_nsec
) + jiffies
;
1509 current
->timeout
= expire
;
1510 current
->state
= TASK_INTERRUPTIBLE
;
1513 if (expire
> jiffies
) {
1515 jiffies_to_timespec(expire
- jiffies
-
1516 (expire
> jiffies
+ 1), &t
);
1517 if (copy_to_user(rmtp
, &t
, sizeof(struct timespec
)))
1525 static void show_task(int nr
,struct task_struct
* p
)
1527 unsigned long free
= 0;
1528 static const char * stat_nam
[] = { "R", "S", "D", "Z", "T", "W" };
1530 printk("%-8s %3d ", p
->comm
, (p
== current
) ? -nr
: nr
);
1531 if (((unsigned) p
->state
) < sizeof(stat_nam
)/sizeof(char *))
1532 printk(stat_nam
[p
->state
]);
1535 #if (BITS_PER_LONG == 32)
1537 printk(" current ");
1539 printk(" %08lX ", thread_saved_pc(&p
->tss
));
1542 printk(" current task ");
1544 printk(" %016lx ", thread_saved_pc(&p
->tss
));
1547 for (free
= 1; free
< PAGE_SIZE
/sizeof(long) ; free
++) {
1548 if (((unsigned long *)p
->kernel_stack_page
)[free
])
1552 printk("%5lu %5d %6d ", free
*sizeof(long), p
->pid
, p
->p_pptr
->pid
);
1554 printk("%5d ", p
->p_cptr
->pid
);
1558 printk("%7d", p
->p_ysptr
->pid
);
1562 printk(" %5d\n", p
->p_osptr
->pid
);
1567 struct signal_queue
*q
;
1568 char s
[sizeof(sigset_t
)*2+1], b
[sizeof(sigset_t
)*2+1];
1570 render_sigset_t(&p
->signal
, s
);
1571 render_sigset_t(&p
->blocked
, b
);
1572 printk(" sig: %d %s %s :", signal_pending(p
), s
, b
);
1573 for (q
= p
->sigqueue
; q
; q
= q
->next
)
1574 printk(" %d", q
->info
.si_signo
);
1579 char * render_sigset_t(sigset_t
*set
, char *buffer
)
1584 if (sigismember(set
, i
+1)) x
|= 1;
1585 if (sigismember(set
, i
+2)) x
|= 2;
1586 if (sigismember(set
, i
+3)) x
|= 4;
1587 if (sigismember(set
, i
+4)) x
|= 8;
1588 *buffer
++ = (x
< 10 ? '0' : 'a' - 10) + x
;
1594 void show_state(void)
1596 struct task_struct
*p
;
1598 #if (BITS_PER_LONG == 32)
1601 printk(" task PC stack pid father child younger older\n");
1605 printk(" task PC stack pid father child younger older\n");
1607 read_lock(&tasklist_lock
);
1609 show_task((p
->tarray_ptr
- &task
[0]),p
);
1610 read_unlock(&tasklist_lock
);
1613 __initfunc(void sched_init(void))
1616 * We have to do a little magic to get the first
1617 * process right in SMP mode.
1619 int cpu
=hard_smp_processor_id();
1622 init_task
.processor
=cpu
;
1624 /* Init task array free list and pidhash table. */
1626 add_free_taskslot(&task
[nr
]);
1628 for(nr
= 0; nr
< PIDHASH_SZ
; nr
++)
1631 init_bh(TIMER_BH
, timer_bh
);
1632 init_bh(TQUEUE_BH
, tqueue_bh
);
1633 init_bh(IMMEDIATE_BH
, immediate_bh
);