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>
19 #include <asm/ptrace.h>
21 #include <linux/smp.h>
22 #include <linux/tty.h>
23 #include <linux/sem.h>
24 #include <linux/signal.h>
25 #include <linux/securebits.h>
30 #define CSIGNAL 0x000000ff /* signal mask to be sent at exit */
31 #define CLONE_VM 0x00000100 /* set if VM shared between processes */
32 #define CLONE_FS 0x00000200 /* set if fs info shared between processes */
33 #define CLONE_FILES 0x00000400 /* set if open files shared between processes */
34 #define CLONE_SIGHAND 0x00000800 /* set if signal handlers shared */
35 #define CLONE_PID 0x00001000 /* set if pid shared */
36 #define CLONE_PTRACE 0x00002000 /* set if we want to let tracing continue on the child too */
37 #define CLONE_VFORK 0x00004000 /* set if the parent wants the child to wake it up on mm_release */
40 * These are the constant used to fake the fixed-point load-average
41 * counting. Some notes:
42 * - 11 bit fractions expand to 22 bits by the multiplies: this gives
43 * a load-average precision of 10 bits integer + 11 bits fractional
44 * - if you want to count load-averages more often, you need more
45 * precision, or rounding will get you. With 2-second counting freq,
46 * the EXP_n values would be 1981, 2034 and 2043 if still using only
49 extern unsigned long avenrun
[]; /* Load averages */
51 #define FSHIFT 11 /* nr of bits of precision */
52 #define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */
53 #define LOAD_FREQ (5*HZ) /* 5 sec intervals */
54 #define EXP_1 1884 /* 1/exp(5sec/1min) as fixed-point */
55 #define EXP_5 2014 /* 1/exp(5sec/5min) */
56 #define EXP_15 2037 /* 1/exp(5sec/15min) */
58 #define CALC_LOAD(load,exp,n) \
60 load += n*(FIXED_1-exp); \
63 #define CT_TO_SECS(x) ((x) / HZ)
64 #define CT_TO_USECS(x) (((x) % HZ) * 1000000/HZ)
66 extern int nr_running
, nr_tasks
;
70 #include <linux/time.h>
71 #include <linux/param.h>
72 #include <linux/resource.h>
73 #include <linux/timer.h>
75 #include <asm/processor.h>
77 #define TASK_RUNNING 0
78 #define TASK_INTERRUPTIBLE 1
79 #define TASK_UNINTERRUPTIBLE 2
81 #define TASK_STOPPED 8
82 #define TASK_SWAPPING 16
83 #define TASK_EXCLUSIVE 32
93 * This is an additional bit set when we want to
94 * yield the CPU for one re-schedule..
96 #define SCHED_YIELD 0x10
103 #define NULL ((void *) 0)
108 #include <asm/spinlock.h>
111 * This serializes "schedule()" and also protects
112 * the run-queue from deletions/modifications (but
113 * _adding_ to the beginning of the run-queue has
116 extern rwlock_t tasklist_lock
;
117 extern spinlock_t runqueue_lock
;
119 extern void sched_init(void);
120 extern void init_idle(void);
121 extern void show_state(void);
122 extern void trap_init(void);
124 #define MAX_SCHEDULE_TIMEOUT LONG_MAX
125 extern signed long FASTCALL(schedule_timeout(signed long timeout
));
126 asmlinkage
void schedule(void);
129 * Open file table structure
131 struct files_struct
{
135 struct file
** fd
; /* current fd array */
136 fd_set close_on_exec
;
140 #define INIT_FILES { \
152 struct dentry
* root
, * pwd
;
161 /* Maximum number of active map areas.. This is a random (large) number */
162 #define MAX_MAP_COUNT (65536)
164 /* Number of map areas at which the AVL tree is activated. This is arbitrary. */
165 #define AVL_MIN_MAP_COUNT 32
168 struct vm_area_struct
* mmap
; /* list of VMAs */
169 struct vm_area_struct
* mmap_avl
; /* tree of VMAs */
170 struct vm_area_struct
* mmap_cache
; /* last find_vma result */
173 int map_count
; /* number of VMAs */
174 struct semaphore mmap_sem
;
175 rwlock_t page_table_lock
;
176 unsigned long context
;
177 unsigned long min_flt
, maj_flt
, nswap
, cmin_flt
, cmaj_flt
, cnswap
;
179 unsigned long start_code
, end_code
, start_data
, end_data
;
180 unsigned long start_brk
, brk
, start_stack
;
181 unsigned long arg_start
, arg_end
, env_start
, env_end
;
182 unsigned long rss
, total_vm
, locked_vm
;
183 unsigned long def_flags
;
184 unsigned long cpu_vm_mask
;
185 unsigned long swap_cnt
; /* number of pages to swap on next pass */
186 unsigned long swap_address
;
188 * This is an architecture-specific pointer: the portable
189 * part of Linux does not know about any segments.
194 #define INIT_MM(name) { \
195 &init_mmap, NULL, NULL, \
198 __MUTEX_INITIALIZER(name.mmap_sem), \
209 struct signal_struct
{
211 struct k_sigaction action
[_NSIG
];
216 #define INIT_SIGNALS { \
222 * Some day this will be a full-fledged user tracking system..
223 * Right now it is only used to track how many processes a
224 * user has, but it has the potential to track memory usage etc.
229 /* these are hardcoded - don't touch */
230 volatile long state
; /* -1 unrunnable, 0 runnable, >0 stopped */
231 unsigned long flags
; /* per process flags, defined below */
233 mm_segment_t addr_limit
; /* thread address space:
234 0-0xBFFFFFFF for user-thead
235 0-0xFFFFFFFF for kernel-thread
237 struct exec_domain
*exec_domain
;
244 /* SMP and runqueue state */
248 int lock_depth
; /* Lock depth. We can context switch in and out of holding a syscall kernel lock... */
249 struct task_struct
*next_task
, *prev_task
;
250 struct task_struct
*next_run
, *prev_run
;
253 struct linux_binfmt
*binfmt
;
254 int exit_code
, exit_signal
;
255 int pdeath_signal
; /* The signal sent when the parent dies */
257 unsigned long personality
;
264 /* boolean value for session group leader */
267 * pointers to (original) parent process, youngest child, younger sibling,
268 * older sibling, respectively. (p->father can be replaced with
271 struct task_struct
*p_opptr
, *p_pptr
, *p_cptr
, *p_ysptr
, *p_osptr
;
273 /* PID hash table linkage. */
274 struct task_struct
*pidhash_next
;
275 struct task_struct
**pidhash_pprev
;
277 /* Pointer to task[] array linkage. */
278 struct task_struct
**tarray_ptr
;
280 wait_queue_head_t wait_chldexit
; /* for wait4() */
281 struct semaphore
*vfork_sem
; /* for vfork() */
282 unsigned long policy
, rt_priority
;
283 unsigned long it_real_value
, it_prof_value
, it_virt_value
;
284 unsigned long it_real_incr
, it_prof_incr
, it_virt_incr
;
285 struct timer_list real_timer
;
287 unsigned long start_time
;
288 long per_cpu_utime
[NR_CPUS
], per_cpu_stime
[NR_CPUS
];
289 /* process credentials */
290 uid_t uid
,euid
,suid
,fsuid
;
291 gid_t gid
,egid
,sgid
,fsgid
;
293 gid_t groups
[NGROUPS
];
294 kernel_cap_t cap_effective
, cap_inheritable
, cap_permitted
;
295 struct user_struct
*user
;
297 struct rlimit rlim
[RLIM_NLIMITS
];
298 unsigned short used_math
;
300 /* file system info */
302 struct tty_struct
*tty
; /* NULL if no tty */
304 struct sem_undo
*semundo
;
305 struct sem_queue
*semsleeping
;
306 /* tss for this task */
307 struct thread_struct tss
;
308 /* filesystem information */
309 struct fs_struct
*fs
;
310 /* open file information */
311 struct files_struct
*files
;
312 /* memory management info */
313 struct mm_struct
*mm
;
315 /* signal handlers */
316 spinlock_t sigmask_lock
; /* Protects signal and blocked */
317 struct signal_struct
*sig
;
318 sigset_t signal
, blocked
;
319 struct signal_queue
*sigqueue
, **sigqueue_tail
;
320 unsigned long sas_ss_sp
;
327 #define PF_ALIGNWARN 0x00000001 /* Print alignment warning msgs */
328 /* Not implemented yet, only for 486*/
329 #define PF_STARTING 0x00000002 /* being created */
330 #define PF_EXITING 0x00000004 /* getting shut down */
331 #define PF_PTRACED 0x00000010 /* set if ptrace (0) has been called */
332 #define PF_TRACESYS 0x00000020 /* tracing system calls */
333 #define PF_FORKNOEXEC 0x00000040 /* forked but didn't exec */
334 #define PF_SUPERPRIV 0x00000100 /* used super-user privileges */
335 #define PF_DUMPCORE 0x00000200 /* dumped core */
336 #define PF_SIGNALED 0x00000400 /* killed by a signal */
337 #define PF_MEMALLOC 0x00000800 /* Allocating memory */
338 #define PF_VFORK 0x00001000 /* Wake up parent in mm_release */
340 #define PF_USEDFPU 0x00100000 /* task used FPU this quantum (SMP) */
341 #define PF_DTRACE 0x00200000 /* delayed trace (used on m68k, i386) */
344 * Limit the stack by to some sane default: root can always
345 * increase this limit if needed.. 8MB seems reasonable.
347 #define _STK_LIM (8*1024*1024)
349 #define DEF_PRIORITY (20*HZ/100) /* 200 ms time slices */
352 * INIT_TASK is used to set up the first task table, touch at
353 * your own risk!. Base=0, limit=0x1fffff (=2MB)
355 #define INIT_TASK(name) \
356 /* state etc */ { 0,0,0,KERNEL_DS,&default_exec_domain,0, \
357 /* counter */ DEF_PRIORITY,DEF_PRIORITY,0, \
358 /* SMP */ 0,0,0,-1, \
359 /* schedlink */ &init_task,&init_task, &init_task, &init_task, \
361 /* ec,brk... */ 0,0,0,0,0,0, \
362 /* pid etc.. */ 0,0,0,0,0, \
363 /* proc links*/ &init_task,&init_task,NULL,NULL,NULL, \
364 /* pidhash */ NULL, NULL, \
365 /* tarray */ &task[0], \
366 /* chld wait */ __WAIT_QUEUE_HEAD_INITIALIZER(name.wait_chldexit), NULL, \
367 /* timeout */ SCHED_OTHER,0,0,0,0,0,0,0, \
368 /* timer */ { NULL, NULL, 0, 0, it_real_fn }, \
369 /* utime */ {0,0,0,0},0, \
370 /* per CPU times */ {0, }, {0, }, \
371 /* process credentials */ \
372 /* uid etc */ 0,0,0,0,0,0,0,0, \
373 /* suppl grps*/ 0, {0,}, \
374 /* caps */ CAP_INIT_EFF_SET,CAP_INIT_INH_SET,CAP_FULL_SET, \
376 /* rlimits */ INIT_RLIMITS, \
378 /* comm */ "swapper", \
379 /* fs info */ 0,NULL, \
380 /* ipc */ NULL, NULL, \
381 /* tss */ INIT_TSS, \
383 /* files */ &init_files, \
385 /* signals */ SPIN_LOCK_UNLOCKED, &init_signals, {{0}}, {{0}}, NULL, &init_task.sigqueue, 0, 0, \
388 #ifndef INIT_TASK_SIZE
389 # define INIT_TASK_SIZE 2048*sizeof(long)
393 struct task_struct task
;
394 unsigned long stack
[INIT_TASK_SIZE
/sizeof(long)];
397 extern union task_union init_task_union
;
399 extern struct mm_struct init_mm
;
400 extern struct task_struct
*task
[NR_TASKS
];
402 extern struct task_struct
**tarray_freelist
;
403 extern spinlock_t taskslot_lock
;
405 extern __inline__
void add_free_taskslot(struct task_struct
**t
)
407 spin_lock(&taskslot_lock
);
408 *t
= (struct task_struct
*) tarray_freelist
;
410 spin_unlock(&taskslot_lock
);
413 extern __inline__
struct task_struct
**get_free_taskslot(void)
415 struct task_struct
**tslot
;
417 spin_lock(&taskslot_lock
);
418 if((tslot
= tarray_freelist
) != NULL
)
419 tarray_freelist
= (struct task_struct
**) *tslot
;
420 spin_unlock(&taskslot_lock
);
426 #define PIDHASH_SZ (NR_TASKS >> 2)
427 extern struct task_struct
*pidhash
[PIDHASH_SZ
];
429 #define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1))
431 extern __inline__
void hash_pid(struct task_struct
*p
)
433 struct task_struct
**htable
= &pidhash
[pid_hashfn(p
->pid
)];
435 if((p
->pidhash_next
= *htable
) != NULL
)
436 (*htable
)->pidhash_pprev
= &p
->pidhash_next
;
438 p
->pidhash_pprev
= htable
;
441 extern __inline__
void unhash_pid(struct task_struct
*p
)
444 p
->pidhash_next
->pidhash_pprev
= p
->pidhash_pprev
;
445 *p
->pidhash_pprev
= p
->pidhash_next
;
448 extern __inline__
struct task_struct
*find_task_by_pid(int pid
)
450 struct task_struct
*p
, **htable
= &pidhash
[pid_hashfn(pid
)];
452 for(p
= *htable
; p
&& p
->pid
!= pid
; p
= p
->pidhash_next
)
458 /* per-UID process charging. */
459 extern int alloc_uid(struct task_struct
*);
460 void free_uid(struct task_struct
*);
462 #include <asm/current.h>
464 extern unsigned long volatile jiffies
;
465 extern unsigned long itimer_ticks
;
466 extern unsigned long itimer_next
;
467 extern struct timeval xtime
;
468 extern void do_timer(struct pt_regs
*);
470 extern unsigned int * prof_buffer
;
471 extern unsigned long prof_len
;
472 extern unsigned long prof_shift
;
474 #define CURRENT_TIME (xtime.tv_sec)
476 extern void FASTCALL(__wake_up(wait_queue_head_t
*q
, unsigned int mode
));
477 extern void FASTCALL(sleep_on(wait_queue_head_t
*q
));
478 extern long FASTCALL(sleep_on_timeout(wait_queue_head_t
*q
,
479 signed long timeout
));
480 extern void FASTCALL(interruptible_sleep_on(wait_queue_head_t
*q
));
481 extern long FASTCALL(interruptible_sleep_on_timeout(wait_queue_head_t
*q
,
482 signed long timeout
));
483 extern void FASTCALL(wake_up_process(struct task_struct
* tsk
));
485 #define wake_up(x) __wake_up((x),TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE)
486 #define wake_up_interruptible(x) __wake_up((x),TASK_INTERRUPTIBLE)
488 extern int in_group_p(gid_t
);
490 extern void flush_signals(struct task_struct
*);
491 extern void flush_signal_handlers(struct task_struct
*);
492 extern int dequeue_signal(sigset_t
*, siginfo_t
*);
493 extern int send_sig_info(int, struct siginfo
*, struct task_struct
*);
494 extern int force_sig_info(int, struct siginfo
*, struct task_struct
*);
495 extern int kill_pg_info(int, struct siginfo
*, pid_t
);
496 extern int kill_sl_info(int, struct siginfo
*, pid_t
);
497 extern int kill_proc_info(int, struct siginfo
*, pid_t
);
498 extern int kill_something_info(int, struct siginfo
*, int);
499 extern void notify_parent(struct task_struct
*, int);
500 extern void force_sig(int, struct task_struct
*);
501 extern int send_sig(int, struct task_struct
*, int);
502 extern int kill_pg(pid_t
, int, int);
503 extern int kill_sl(pid_t
, int, int);
504 extern int kill_proc(pid_t
, int, int);
505 extern int do_sigaction(int, const struct k_sigaction
*, struct k_sigaction
*);
506 extern int do_sigaltstack(const stack_t
*, stack_t
*, unsigned long);
508 extern inline int signal_pending(struct task_struct
*p
)
510 return (p
->sigpending
!= 0);
513 /* Reevaluate whether the task has signals pending delivery.
514 This is required every time the blocked sigset_t changes.
515 All callers should have t->sigmask_lock. */
517 static inline void recalc_sigpending(struct task_struct
*t
)
522 switch (_NSIG_WORDS
) {
524 for (i
= _NSIG_WORDS
, ready
= 0; --i
>= 0 ;)
525 ready
|= t
->signal
.sig
[i
] &~ t
->blocked
.sig
[i
];
528 case 4: ready
= t
->signal
.sig
[3] &~ t
->blocked
.sig
[3];
529 ready
|= t
->signal
.sig
[2] &~ t
->blocked
.sig
[2];
530 ready
|= t
->signal
.sig
[1] &~ t
->blocked
.sig
[1];
531 ready
|= t
->signal
.sig
[0] &~ t
->blocked
.sig
[0];
534 case 2: ready
= t
->signal
.sig
[1] &~ t
->blocked
.sig
[1];
535 ready
|= t
->signal
.sig
[0] &~ t
->blocked
.sig
[0];
538 case 1: ready
= t
->signal
.sig
[0] &~ t
->blocked
.sig
[0];
541 t
->sigpending
= (ready
!= 0);
544 /* True if we are on the alternate signal stack. */
546 static inline int on_sig_stack(unsigned long sp
)
548 return (sp
- current
->sas_ss_sp
< current
->sas_ss_size
);
551 static inline int sas_ss_flags(unsigned long sp
)
553 return (current
->sas_ss_size
== 0 ? SS_DISABLE
554 : on_sig_stack(sp
) ? SS_ONSTACK
: 0);
557 extern int request_irq(unsigned int,
558 void (*handler
)(int, void *, struct pt_regs
*),
559 unsigned long, const char *, void *);
560 extern void free_irq(unsigned int, void *);
563 * This has now become a routine instead of a macro, it sets a flag if
564 * it returns true (to do BSD-style accounting where the process is flagged
565 * if it uses root privs). The implication of this is that you should do
566 * normal permissions checks first, and check suser() last.
568 * [Dec 1997 -- Chris Evans]
569 * For correctness, the above considerations need to be extended to
570 * fsuser(). This is done, along with moving fsuser() checks to be
573 * These will be removed, but in the mean time, when the SECURE_NOROOT
574 * flag is set, uids don't grant privilege.
576 extern inline int suser(void)
578 if (!issecure(SECURE_NOROOT
) && current
->euid
== 0) {
579 current
->flags
|= PF_SUPERPRIV
;
585 extern inline int fsuser(void)
587 if (!issecure(SECURE_NOROOT
) && current
->fsuid
== 0) {
588 current
->flags
|= PF_SUPERPRIV
;
595 * capable() checks for a particular capability.
596 * New privilege checks should use this interface, rather than suser() or
597 * fsuser(). See include/linux/capability.h for defined capabilities.
600 extern inline int capable(int cap
)
603 if (cap_raised(current
->cap_effective
, cap
))
605 if (cap_is_fs_cap(cap
) ? current
->fsuid
== 0 : current
->euid
== 0)
608 current
->flags
|= PF_SUPERPRIV
;
615 * Routines for handling mm_structs
617 extern struct mm_struct
* mm_alloc(void);
618 static inline void mmget(struct mm_struct
* mm
)
620 atomic_inc(&mm
->count
);
622 extern void mmput(struct mm_struct
*);
623 /* Remove the current tasks stale references to the old mm_struct */
624 extern void mm_release(void);
626 extern int copy_thread(int, unsigned long, unsigned long, struct task_struct
*, struct pt_regs
*);
627 extern void flush_thread(void);
628 extern void exit_thread(void);
630 extern void exit_mm(struct task_struct
*);
631 extern void exit_fs(struct task_struct
*);
632 extern void exit_files(struct task_struct
*);
633 extern void exit_sighand(struct task_struct
*);
635 extern int do_execve(char *, char **, char **, struct pt_regs
*);
636 extern int do_fork(unsigned long, unsigned long, struct pt_regs
*);
638 extern inline void add_wait_queue(wait_queue_head_t
*q
, wait_queue_t
* wait
)
642 wq_write_lock_irqsave(&q
->lock
, flags
);
643 __add_wait_queue(q
, wait
);
644 wq_write_unlock_irqrestore(&q
->lock
, flags
);
647 extern inline void add_wait_queue_exclusive(wait_queue_head_t
*q
,
652 wq_write_lock_irqsave(&q
->lock
, flags
);
653 __add_wait_queue_tail(q
, wait
);
654 wq_write_unlock_irqrestore(&q
->lock
, flags
);
657 extern inline void remove_wait_queue(wait_queue_head_t
*q
, wait_queue_t
* wait
)
661 wq_write_lock_irqsave(&q
->lock
, flags
);
662 __remove_wait_queue(q
, wait
);
663 wq_write_unlock_irqrestore(&q
->lock
, flags
);
666 #define __wait_event(wq, condition) \
668 wait_queue_t __wait; \
669 init_waitqueue_entry(&__wait, current); \
671 add_wait_queue(&wq, &__wait); \
673 current->state = TASK_UNINTERRUPTIBLE; \
678 current->state = TASK_RUNNING; \
679 remove_wait_queue(&wq, &__wait); \
682 #define wait_event(wq, condition) \
686 __wait_event(wq, condition); \
689 #define __wait_event_interruptible(wq, condition, ret) \
691 wait_queue_t __wait; \
692 init_waitqueue_entry(&__wait, current); \
694 add_wait_queue(&wq, &__wait); \
696 current->state = TASK_INTERRUPTIBLE; \
699 if (!signal_pending(current)) { \
703 ret = -ERESTARTSYS; \
706 current->state = TASK_RUNNING; \
707 remove_wait_queue(&wq, &__wait); \
710 #define wait_event_interruptible(wq, condition) \
714 __wait_event_interruptible(wq, condition, __ret); \
718 #define REMOVE_LINKS(p) do { \
719 (p)->next_task->prev_task = (p)->prev_task; \
720 (p)->prev_task->next_task = (p)->next_task; \
722 (p)->p_osptr->p_ysptr = (p)->p_ysptr; \
724 (p)->p_ysptr->p_osptr = (p)->p_osptr; \
726 (p)->p_pptr->p_cptr = (p)->p_osptr; \
729 #define SET_LINKS(p) do { \
730 (p)->next_task = &init_task; \
731 (p)->prev_task = init_task.prev_task; \
732 init_task.prev_task->next_task = (p); \
733 init_task.prev_task = (p); \
734 (p)->p_ysptr = NULL; \
735 if (((p)->p_osptr = (p)->p_pptr->p_cptr) != NULL) \
736 (p)->p_osptr->p_ysptr = p; \
737 (p)->p_pptr->p_cptr = p; \
740 #define for_each_task(p) \
741 for (p = &init_task ; (p = p->next_task) != &init_task ; )
743 #endif /* __KERNEL__ */