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>
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>
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
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) \
58 load += n*(FIXED_1-exp); \
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
;
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
79 #define TASK_STOPPED 8
80 #define TASK_SWAPPING 16
90 * This is an additional bit set when we want to
91 * yield the CPU for one re-schedule..
93 #define SCHED_YIELD 0x10
100 #define NULL ((void *) 0)
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
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
{
130 struct file
** fd
; /* current fd array */
131 fd_set close_on_exec
;
135 #define INIT_FILES { \
146 struct dentry
* root
, * pwd
;
155 /* Maximum number of active map areas.. This is a random (large) number */
156 #define MAX_MAP_COUNT (65536)
159 struct vm_area_struct
*mmap
, *mmap_cache
;
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.
179 &init_mmap, NULL, swapper_pg_dir, \
189 struct signal_struct
{
191 struct k_sigaction action
[_NSIG
];
196 #define INIT_SIGNALS { \
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.
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 */
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
;
224 /* SMP and runqueue state */
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
;
233 struct linux_binfmt
*binfmt
;
234 int exit_code
, exit_signal
;
235 int pdeath_signal
; /* The signal sent when the parent dies */
237 unsigned long personality
;
244 /* boolean value for session group leader */
247 * pointers to (original) parent process, youngest child, younger sibling,
248 * older sibling, respectively. (p->father can be replaced with
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
;
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
;
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
;
281 gid_t groups
[NGROUPS
];
282 kernel_cap_t cap_effective
, cap_inheritable
, cap_permitted
;
283 struct user_struct
*user
;
285 struct rlimit rlim
[RLIM_NLIMITS
];
286 unsigned short used_math
;
288 /* file system info */
290 struct tty_struct
*tty
; /* NULL if no tty */
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
;
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)
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, \
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, \
365 /* rlimits */ INIT_RLIMITS, \
367 /* comm */ "swapper", \
368 /* fs info */ 0,NULL, \
369 /* ipc */ NULL, NULL, \
370 /* tss */ INIT_TSS, \
372 /* files */ &init_files, \
374 /* signals */ SPIN_LOCK_UNLOCKED, &init_signals, {{0}}, {{0}}, NULL, &init_task.sigqueue, 0, 0, \
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
;
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
);
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
;
423 p
->pidhash_pprev
= htable
;
426 extern __inline__
void unhash_pid(struct task_struct
*p
)
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
)
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
)
508 switch (_NSIG_WORDS
) {
510 for (i
= _NSIG_WORDS
, ready
= 0; --i
>= 0 ;)
511 ready
|= t
->signal
.sig
[i
] &~ t
->blocked
.sig
[i
];
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];
520 case 2: ready
= t
->signal
.sig
[1] &~ t
->blocked
.sig
[1];
521 ready
|= t
->signal
.sig
[0] &~ t
->blocked
.sig
[0];
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
*),
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
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
;
574 extern inline int fsuser(void)
576 if (!issecure(SECURE_NOROOT
) && current
->fsuid
== 0) {
577 current
->flags
|= PF_SUPERPRIV
;
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
)
592 if (cap_raised(current
->cap_effective
, cap
))
594 if (cap_is_fs_cap(cap
) ? current
->fsuid
== 0 : current
->euid
== 0)
597 current
->flags
|= PF_SUPERPRIV
;
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
);
636 extern rwlock_t waitqueue_lock
;
638 extern inline void add_wait_queue(struct wait_queue
** p
, struct wait_queue
* wait
)
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
) {
659 extern inline void remove_wait_queue(struct wait_queue
** p
, struct wait_queue
* wait
)
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; \
672 (p)->p_osptr->p_ysptr = (p)->p_ysptr; \
674 (p)->p_ysptr->p_osptr = (p)->p_osptr; \
676 (p)->p_pptr->p_cptr = (p)->p_osptr; \
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; \
690 #define for_each_task(p) \
691 for (p = &init_task ; (p = p->next_task) != &init_task ; )
693 #endif /* __KERNEL__ */