Import 2.2.0pre6
[davej-history.git] / include / linux / sched.h
blobebf339d0fe372ad2340a7ebd27048e0a9fa16904
1 #ifndef _LINUX_SCHED_H
2 #define _LINUX_SCHED_H
4 #include <asm/param.h> /* for HZ */
6 extern unsigned long event;
8 #include <linux/binfmts.h>
9 #include <linux/personality.h>
10 #include <linux/tasks.h>
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/times.h>
14 #include <linux/timex.h>
16 #include <asm/system.h>
17 #include <asm/semaphore.h>
18 #include <asm/page.h>
20 #include <linux/smp.h>
21 #include <linux/tty.h>
22 #include <linux/sem.h>
23 #include <linux/signal.h>
24 #include <linux/securebits.h>
27 * cloning flags:
29 #define CSIGNAL 0x000000ff /* signal mask to be sent at exit */
30 #define CLONE_VM 0x00000100 /* set if VM shared between processes */
31 #define CLONE_FS 0x00000200 /* set if fs info shared between processes */
32 #define CLONE_FILES 0x00000400 /* set if open files shared between processes */
33 #define CLONE_SIGHAND 0x00000800 /* set if signal handlers shared */
34 #define CLONE_PID 0x00001000 /* set if pid shared */
35 #define CLONE_PTRACE 0x00002000 /* set if we want to let tracing continue on the child too */
38 * These are the constant used to fake the fixed-point load-average
39 * counting. Some notes:
40 * - 11 bit fractions expand to 22 bits by the multiplies: this gives
41 * a load-average precision of 10 bits integer + 11 bits fractional
42 * - if you want to count load-averages more often, you need more
43 * precision, or rounding will get you. With 2-second counting freq,
44 * the EXP_n values would be 1981, 2034 and 2043 if still using only
45 * 11 bit fractions.
47 extern unsigned long avenrun[]; /* Load averages */
49 #define FSHIFT 11 /* nr of bits of precision */
50 #define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */
51 #define LOAD_FREQ (5*HZ) /* 5 sec intervals */
52 #define EXP_1 1884 /* 1/exp(5sec/1min) as fixed-point */
53 #define EXP_5 2014 /* 1/exp(5sec/5min) */
54 #define EXP_15 2037 /* 1/exp(5sec/15min) */
56 #define CALC_LOAD(load,exp,n) \
57 load *= exp; \
58 load += n*(FIXED_1-exp); \
59 load >>= FSHIFT;
61 #define CT_TO_SECS(x) ((x) / HZ)
62 #define CT_TO_USECS(x) (((x) % HZ) * 1000000/HZ)
64 extern int nr_running, nr_tasks;
65 extern int last_pid;
67 #include <linux/fs.h>
68 #include <linux/time.h>
69 #include <linux/param.h>
70 #include <linux/resource.h>
71 #include <linux/timer.h>
73 #include <asm/processor.h>
75 #define TASK_RUNNING 0
76 #define TASK_INTERRUPTIBLE 1
77 #define TASK_UNINTERRUPTIBLE 2
78 #define TASK_ZOMBIE 4
79 #define TASK_STOPPED 8
80 #define TASK_SWAPPING 16
83 * Scheduling policies
85 #define SCHED_OTHER 0
86 #define SCHED_FIFO 1
87 #define SCHED_RR 2
90 * This is an additional bit set when we want to
91 * yield the CPU for one re-schedule..
93 #define SCHED_YIELD 0x10
95 struct sched_param {
96 int sched_priority;
99 #ifndef NULL
100 #define NULL ((void *) 0)
101 #endif
103 #ifdef __KERNEL__
105 #include <asm/spinlock.h>
108 * This serializes "schedule()" and also protects
109 * the run-queue from deletions/modifications (but
110 * _adding_ to the beginning of the run-queue has
111 * a separate lock).
113 extern rwlock_t tasklist_lock;
114 extern spinlock_t scheduler_lock;
116 extern void sched_init(void);
117 extern void show_state(void);
118 extern void trap_init(void);
120 #define MAX_SCHEDULE_TIMEOUT LONG_MAX
121 extern signed long FASTCALL(schedule_timeout(signed long timeout));
122 asmlinkage void schedule(void);
125 * Open file table structure
127 struct files_struct {
128 atomic_t count;
129 int max_fds;
130 struct file ** fd; /* current fd array */
131 fd_set close_on_exec;
132 fd_set open_fds;
135 #define INIT_FILES { \
136 ATOMIC_INIT(1), \
137 NR_OPEN, \
138 &init_fd_array[0], \
139 { { 0, } }, \
140 { { 0, } } \
143 struct fs_struct {
144 atomic_t count;
145 int umask;
146 struct dentry * root, * pwd;
149 #define INIT_FS { \
150 ATOMIC_INIT(1), \
151 0022, \
152 NULL, NULL \
155 /* Maximum number of active map areas.. This is a random (large) number */
156 #define MAX_MAP_COUNT (65536)
158 struct mm_struct {
159 struct vm_area_struct *mmap, *mmap_cache;
160 pgd_t * pgd;
161 atomic_t count;
162 int map_count;
163 struct semaphore mmap_sem;
164 unsigned long context;
165 unsigned long start_code, end_code, start_data, end_data;
166 unsigned long start_brk, brk, start_stack;
167 unsigned long arg_start, arg_end, env_start, env_end;
168 unsigned long rss, total_vm, locked_vm;
169 unsigned long def_flags;
170 unsigned long cpu_vm_mask;
172 * This is an architecture-specific pointer: the portable
173 * part of Linux does not know about any segments.
175 void * segments;
178 #define INIT_MM { \
179 &init_mmap, NULL, swapper_pg_dir, \
180 ATOMIC_INIT(1), 1, \
181 MUTEX, \
182 0, \
183 0, 0, 0, 0, \
184 0, 0, 0, \
185 0, 0, 0, 0, \
186 0, 0, 0, \
187 0, 0, NULL }
189 struct signal_struct {
190 atomic_t count;
191 struct k_sigaction action[_NSIG];
192 spinlock_t siglock;
196 #define INIT_SIGNALS { \
197 ATOMIC_INIT(1), \
198 { {{0,}}, }, \
199 SPIN_LOCK_UNLOCKED }
202 * Some day this will be a full-fledged user tracking system..
203 * Right now it is only used to track how many processes a
204 * user has, but it has the potential to track memory usage etc.
206 struct user_struct;
208 struct task_struct {
209 /* these are hardcoded - don't touch */
210 volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
211 unsigned long flags; /* per process flags, defined below */
212 int sigpending;
213 mm_segment_t addr_limit; /* thread address space:
214 0-0xBFFFFFFF for user-thead
215 0-0xFFFFFFFF for kernel-thread
217 struct exec_domain *exec_domain;
218 long need_resched;
220 /* various fields */
221 long counter;
222 long priority;
223 cycles_t avg_slice;
224 /* SMP and runqueue state */
225 int has_cpu;
226 int processor;
227 int last_processor;
228 int lock_depth; /* Lock depth. We can context switch in and out of holding a syscall kernel lock... */
229 struct task_struct *next_task, *prev_task;
230 struct task_struct *next_run, *prev_run;
232 /* task state */
233 struct linux_binfmt *binfmt;
234 int exit_code, exit_signal;
235 int pdeath_signal; /* The signal sent when the parent dies */
236 /* ??? */
237 unsigned long personality;
238 int dumpable:1;
239 int did_exec:1;
240 pid_t pid;
241 pid_t pgrp;
242 pid_t tty_old_pgrp;
243 pid_t session;
244 /* boolean value for session group leader */
245 int leader;
247 * pointers to (original) parent process, youngest child, younger sibling,
248 * older sibling, respectively. (p->father can be replaced with
249 * p->p_pptr->pid)
251 struct task_struct *p_opptr, *p_pptr, *p_cptr, *p_ysptr, *p_osptr;
253 /* PID hash table linkage. */
254 struct task_struct *pidhash_next;
255 struct task_struct **pidhash_pprev;
257 /* Pointer to task[] array linkage. */
258 struct task_struct **tarray_ptr;
260 struct wait_queue *wait_chldexit, *vfork_sleep; /* for wait4()/vfork */
262 unsigned long policy, rt_priority;
263 unsigned long it_real_value, it_prof_value, it_virt_value;
264 unsigned long it_real_incr, it_prof_incr, it_virt_incr;
265 struct timer_list real_timer;
266 struct tms times;
267 unsigned long start_time;
268 long per_cpu_utime[NR_CPUS], per_cpu_stime[NR_CPUS];
269 /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
270 unsigned long min_flt, maj_flt, nswap, cmin_flt, cmaj_flt, cnswap;
271 int swappable:1;
272 int trashing_memory:1;
273 unsigned long swap_address;
274 unsigned long old_maj_flt; /* old value of maj_flt */
275 unsigned long dec_flt; /* page fault count of the last time */
276 unsigned long swap_cnt; /* number of pages to swap on next pass */
277 /* process credentials */
278 uid_t uid,euid,suid,fsuid;
279 gid_t gid,egid,sgid,fsgid;
280 int ngroups;
281 gid_t groups[NGROUPS];
282 kernel_cap_t cap_effective, cap_inheritable, cap_permitted;
283 struct user_struct *user;
284 /* limits */
285 struct rlimit rlim[RLIM_NLIMITS];
286 unsigned short used_math;
287 char comm[16];
288 /* file system info */
289 int link_count;
290 struct tty_struct *tty; /* NULL if no tty */
291 /* ipc stuff */
292 struct sem_undo *semundo;
293 struct sem_queue *semsleeping;
294 /* tss for this task */
295 struct thread_struct tss;
296 /* filesystem information */
297 struct fs_struct *fs;
298 /* open file information */
299 struct files_struct *files;
300 /* memory management info */
301 struct mm_struct *mm;
303 /* signal handlers */
304 spinlock_t sigmask_lock; /* Protects signal and blocked */
305 struct signal_struct *sig;
306 sigset_t signal, blocked;
307 struct signal_queue *sigqueue, **sigqueue_tail;
308 unsigned long sas_ss_sp;
309 size_t sas_ss_size;
313 * Per process flags
315 #define PF_ALIGNWARN 0x00000001 /* Print alignment warning msgs */
316 /* Not implemented yet, only for 486*/
317 #define PF_STARTING 0x00000002 /* being created */
318 #define PF_EXITING 0x00000004 /* getting shut down */
319 #define PF_PTRACED 0x00000010 /* set if ptrace (0) has been called */
320 #define PF_TRACESYS 0x00000020 /* tracing system calls */
321 #define PF_FORKNOEXEC 0x00000040 /* forked but didn't exec */
322 #define PF_SUPERPRIV 0x00000100 /* used super-user privileges */
323 #define PF_DUMPCORE 0x00000200 /* dumped core */
324 #define PF_SIGNALED 0x00000400 /* killed by a signal */
325 #define PF_MEMALLOC 0x00000800 /* Allocating memory */
327 #define PF_USEDFPU 0x00100000 /* task used FPU this quantum (SMP) */
328 #define PF_DTRACE 0x00200000 /* delayed trace (used on m68k, i386) */
331 * Limit the stack by to some sane default: root can always
332 * increase this limit if needed.. 8MB seems reasonable.
334 #define _STK_LIM (8*1024*1024)
336 #define DEF_PRIORITY (20*HZ/100) /* 200 ms time slices */
339 * INIT_TASK is used to set up the first task table, touch at
340 * your own risk!. Base=0, limit=0x1fffff (=2MB)
342 #define INIT_TASK \
343 /* state etc */ { 0,0,0,KERNEL_DS,&default_exec_domain,0, \
344 /* counter */ DEF_PRIORITY,DEF_PRIORITY,0, \
345 /* SMP */ 0,0,0,-1, \
346 /* schedlink */ &init_task,&init_task, &init_task, &init_task, \
347 /* binfmt */ NULL, \
348 /* ec,brk... */ 0,0,0,0,0,0, \
349 /* pid etc.. */ 0,0,0,0,0, \
350 /* proc links*/ &init_task,&init_task,NULL,NULL,NULL, \
351 /* pidhash */ NULL, NULL, \
352 /* tarray */ &task[0], \
353 /* chld wait */ NULL, NULL, \
354 /* timeout */ SCHED_OTHER,0,0,0,0,0,0,0, \
355 /* timer */ { NULL, NULL, 0, 0, it_real_fn }, \
356 /* utime */ {0,0,0,0},0, \
357 /* per CPU times */ {0, }, {0, }, \
358 /* flt */ 0,0,0,0,0,0, \
359 /* swp */ 0,0,0,0,0,0, \
360 /* process credentials */ \
361 /* uid etc */ 0,0,0,0,0,0,0,0, \
362 /* suppl grps*/ 0, {0,}, \
363 /* caps */ CAP_INIT_EFF_SET,CAP_INIT_INH_SET,CAP_FULL_SET, \
364 /* user */ NULL, \
365 /* rlimits */ INIT_RLIMITS, \
366 /* math */ 0, \
367 /* comm */ "swapper", \
368 /* fs info */ 0,NULL, \
369 /* ipc */ NULL, NULL, \
370 /* tss */ INIT_TSS, \
371 /* fs */ &init_fs, \
372 /* files */ &init_files, \
373 /* mm */ &init_mm, \
374 /* signals */ SPIN_LOCK_UNLOCKED, &init_signals, {{0}}, {{0}}, NULL, &init_task.sigqueue, 0, 0, \
377 union task_union {
378 struct task_struct task;
379 unsigned long stack[2048];
382 extern union task_union init_task_union;
384 extern struct mm_struct init_mm;
385 extern struct task_struct *task[NR_TASKS];
387 extern struct task_struct **tarray_freelist;
388 extern spinlock_t taskslot_lock;
390 extern __inline__ void add_free_taskslot(struct task_struct **t)
392 spin_lock(&taskslot_lock);
393 *t = (struct task_struct *) tarray_freelist;
394 tarray_freelist = t;
395 spin_unlock(&taskslot_lock);
398 extern __inline__ struct task_struct **get_free_taskslot(void)
400 struct task_struct **tslot;
402 spin_lock(&taskslot_lock);
403 if((tslot = tarray_freelist) != NULL)
404 tarray_freelist = (struct task_struct **) *tslot;
405 spin_unlock(&taskslot_lock);
407 return tslot;
410 /* PID hashing. */
411 #define PIDHASH_SZ (NR_TASKS >> 2)
412 extern struct task_struct *pidhash[PIDHASH_SZ];
414 #define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1))
416 extern __inline__ void hash_pid(struct task_struct *p)
418 struct task_struct **htable = &pidhash[pid_hashfn(p->pid)];
420 if((p->pidhash_next = *htable) != NULL)
421 (*htable)->pidhash_pprev = &p->pidhash_next;
422 *htable = p;
423 p->pidhash_pprev = htable;
426 extern __inline__ void unhash_pid(struct task_struct *p)
428 if(p->pidhash_next)
429 p->pidhash_next->pidhash_pprev = p->pidhash_pprev;
430 *p->pidhash_pprev = p->pidhash_next;
433 extern __inline__ struct task_struct *find_task_by_pid(int pid)
435 struct task_struct *p, **htable = &pidhash[pid_hashfn(pid)];
437 for(p = *htable; p && p->pid != pid; p = p->pidhash_next)
440 return p;
443 /* per-UID process charging. */
444 extern int alloc_uid(struct task_struct *p);
445 void free_uid(struct task_struct *p);
447 #include <asm/current.h>
449 extern unsigned long volatile jiffies;
450 extern unsigned long itimer_ticks;
451 extern unsigned long itimer_next;
452 extern struct timeval xtime;
453 extern void do_timer(struct pt_regs *);
455 extern unsigned int * prof_buffer;
456 extern unsigned long prof_len;
457 extern unsigned long prof_shift;
459 #define CURRENT_TIME (xtime.tv_sec)
461 extern void FASTCALL(__wake_up(struct wait_queue ** p, unsigned int mode));
462 extern void FASTCALL(sleep_on(struct wait_queue ** p));
463 extern long FASTCALL(sleep_on_timeout(struct wait_queue ** p,
464 signed long timeout));
465 extern void FASTCALL(interruptible_sleep_on(struct wait_queue ** p));
466 extern long FASTCALL(interruptible_sleep_on_timeout(struct wait_queue ** p,
467 signed long timeout));
468 extern void FASTCALL(wake_up_process(struct task_struct * tsk));
470 #define wake_up(x) __wake_up((x),TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE)
471 #define wake_up_interruptible(x) __wake_up((x),TASK_INTERRUPTIBLE)
473 extern int in_group_p(gid_t grp);
475 extern void flush_signals(struct task_struct *);
476 extern void flush_signal_handlers(struct task_struct *);
477 extern int dequeue_signal(sigset_t *block, siginfo_t *);
478 extern int send_sig_info(int, struct siginfo *info, struct task_struct *);
479 extern int force_sig_info(int, struct siginfo *info, struct task_struct *);
480 extern int kill_pg_info(int, struct siginfo *info, pid_t);
481 extern int kill_sl_info(int, struct siginfo *info, pid_t);
482 extern int kill_proc_info(int, struct siginfo *info, pid_t);
483 extern int kill_something_info(int, struct siginfo *info, int);
484 extern void notify_parent(struct task_struct * tsk, int);
485 extern void force_sig(int sig, struct task_struct * p);
486 extern int send_sig(int sig, struct task_struct * p, int priv);
487 extern int kill_pg(pid_t, int, int);
488 extern int kill_sl(pid_t, int, int);
489 extern int kill_proc(pid_t, int, int);
490 extern int do_sigaction(int sig, const struct k_sigaction *act,
491 struct k_sigaction *oact);
492 extern int do_sigaltstack(const stack_t *ss, stack_t *oss, unsigned long sp);
494 extern inline int signal_pending(struct task_struct *p)
496 return (p->sigpending != 0);
499 /* Reevaluate whether the task has signals pending delivery.
500 This is required every time the blocked sigset_t changes.
501 All callers should have t->sigmask_lock. */
503 static inline void recalc_sigpending(struct task_struct *t)
505 unsigned long ready;
506 long i;
508 switch (_NSIG_WORDS) {
509 default:
510 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
511 ready |= t->signal.sig[i] &~ t->blocked.sig[i];
512 break;
514 case 4: ready = t->signal.sig[3] &~ t->blocked.sig[3];
515 ready |= t->signal.sig[2] &~ t->blocked.sig[2];
516 ready |= t->signal.sig[1] &~ t->blocked.sig[1];
517 ready |= t->signal.sig[0] &~ t->blocked.sig[0];
518 break;
520 case 2: ready = t->signal.sig[1] &~ t->blocked.sig[1];
521 ready |= t->signal.sig[0] &~ t->blocked.sig[0];
522 break;
524 case 1: ready = t->signal.sig[0] &~ t->blocked.sig[0];
527 t->sigpending = (ready != 0);
530 /* True if we are on the alternate signal stack. */
532 static inline int on_sig_stack(unsigned long sp)
534 return (sp >= current->sas_ss_sp
535 && sp < current->sas_ss_sp + current->sas_ss_size);
538 static inline int sas_ss_flags(unsigned long sp)
540 return (current->sas_ss_size == 0 ? SS_DISABLE
541 : on_sig_stack(sp) ? SS_ONSTACK : 0);
544 extern int request_irq(unsigned int irq,
545 void (*handler)(int, void *, struct pt_regs *),
546 unsigned long flags,
547 const char *device,
548 void *dev_id);
549 extern void free_irq(unsigned int irq, void *dev_id);
552 * This has now become a routine instead of a macro, it sets a flag if
553 * it returns true (to do BSD-style accounting where the process is flagged
554 * if it uses root privs). The implication of this is that you should do
555 * normal permissions checks first, and check suser() last.
557 * [Dec 1997 -- Chris Evans]
558 * For correctness, the above considerations need to be extended to
559 * fsuser(). This is done, along with moving fsuser() checks to be
560 * last.
562 * These will be removed, but in the mean time, when the SECURE_NOROOT
563 * flag is set, uids don't grant privilege.
565 extern inline int suser(void)
567 if (!issecure(SECURE_NOROOT) && current->euid == 0) {
568 current->flags |= PF_SUPERPRIV;
569 return 1;
571 return 0;
574 extern inline int fsuser(void)
576 if (!issecure(SECURE_NOROOT) && current->fsuid == 0) {
577 current->flags |= PF_SUPERPRIV;
578 return 1;
580 return 0;
584 * capable() checks for a particular capability.
585 * New privilege checks should use this interface, rather than suser() or
586 * fsuser(). See include/linux/capability.h for defined capabilities.
589 extern inline int capable(int cap)
591 #if 1 /* ok now */
592 if (cap_raised(current->cap_effective, cap))
593 #else
594 if (cap_is_fs_cap(cap) ? current->fsuid == 0 : current->euid == 0)
595 #endif
597 current->flags |= PF_SUPERPRIV;
598 return 1;
600 return 0;
604 * Routines for handling mm_structs
606 extern struct mm_struct * mm_alloc(void);
607 static inline void mmget(struct mm_struct * mm)
609 atomic_inc(&mm->count);
611 extern void mmput(struct mm_struct *);
613 extern int copy_thread(int, unsigned long, unsigned long, struct task_struct *, struct pt_regs *);
614 extern void flush_thread(void);
615 extern void exit_thread(void);
617 extern void exit_mm(struct task_struct *);
618 extern void exit_fs(struct task_struct *);
619 extern void exit_files(struct task_struct *);
620 extern void exit_sighand(struct task_struct *);
622 extern int do_execve(char *, char **, char **, struct pt_regs *);
623 extern int do_fork(unsigned long, unsigned long, struct pt_regs *);
626 * The wait-queues are circular lists, and you have to be *very* sure
627 * to keep them correct. Use only these two functions to add/remove
628 * entries in the queues.
630 extern inline void __add_wait_queue(struct wait_queue ** p, struct wait_queue * wait)
632 wait->next = *p ? : WAIT_QUEUE_HEAD(p);
633 *p = wait;
636 extern rwlock_t waitqueue_lock;
638 extern inline void add_wait_queue(struct wait_queue ** p, struct wait_queue * wait)
640 unsigned long flags;
642 write_lock_irqsave(&waitqueue_lock, flags);
643 __add_wait_queue(p, wait);
644 write_unlock_irqrestore(&waitqueue_lock, flags);
647 extern inline void __remove_wait_queue(struct wait_queue ** p, struct wait_queue * wait)
649 struct wait_queue * next = wait->next;
650 struct wait_queue * head = next;
651 struct wait_queue * tmp;
653 while ((tmp = head->next) != wait) {
654 head = tmp;
656 head->next = next;
659 extern inline void remove_wait_queue(struct wait_queue ** p, struct wait_queue * wait)
661 unsigned long flags;
663 write_lock_irqsave(&waitqueue_lock, flags);
664 __remove_wait_queue(p, wait);
665 write_unlock_irqrestore(&waitqueue_lock, flags);
668 #define REMOVE_LINKS(p) do { \
669 (p)->next_task->prev_task = (p)->prev_task; \
670 (p)->prev_task->next_task = (p)->next_task; \
671 if ((p)->p_osptr) \
672 (p)->p_osptr->p_ysptr = (p)->p_ysptr; \
673 if ((p)->p_ysptr) \
674 (p)->p_ysptr->p_osptr = (p)->p_osptr; \
675 else \
676 (p)->p_pptr->p_cptr = (p)->p_osptr; \
677 } while (0)
679 #define SET_LINKS(p) do { \
680 (p)->next_task = &init_task; \
681 (p)->prev_task = init_task.prev_task; \
682 init_task.prev_task->next_task = (p); \
683 init_task.prev_task = (p); \
684 (p)->p_ysptr = NULL; \
685 if (((p)->p_osptr = (p)->p_pptr->p_cptr) != NULL) \
686 (p)->p_osptr->p_ysptr = p; \
687 (p)->p_pptr->p_cptr = p; \
688 } while (0)
690 #define for_each_task(p) \
691 for (p = &init_task ; (p = p->next_task) != &init_task ; )
693 #endif /* __KERNEL__ */
695 #endif