1 #ifndef _LINUX_SCHED_H
2 #define _LINUX_SCHED_H
3 
4 #include <asm/param.h>	/* for HZ */
5 
6 extern unsigned long event;
7 
8 #include <linux/config.h>
9 #include <linux/binfmts.h>
10 #include <linux/threads.h>
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/times.h>
14 #include <linux/timex.h>
15 #include <linux/rbtree.h>
16 
17 #include <asm/system.h>
18 #include <asm/semaphore.h>
19 #include <asm/page.h>
20 #include <asm/ptrace.h>
21 #include <asm/mmu.h>
22 
23 #include <linux/smp.h>
24 #include <linux/tty.h>
25 #include <linux/sem.h>
26 #include <linux/signal.h>
27 #include <linux/securebits.h>
28 #include <linux/fs_struct.h>
29 
30 struct exec_domain;
31 
32 /*
33  * cloning flags:
34  */
35 #define CSIGNAL		0x000000ff	/* signal mask to be sent at exit */
36 #define CLONE_VM	0x00000100	/* set if VM shared between processes */
37 #define CLONE_FS	0x00000200	/* set if fs info shared between processes */
38 #define CLONE_FILES	0x00000400	/* set if open files shared between processes */
39 #define CLONE_SIGHAND	0x00000800	/* set if signal handlers and blocked signals shared */
40 #define CLONE_PID	0x00001000	/* set if pid shared */
41 #define CLONE_PTRACE	0x00002000	/* set if we want to let tracing continue on the child too */
42 #define CLONE_VFORK	0x00004000	/* set if the parent wants the child to wake it up on mm_release */
43 #define CLONE_PARENT	0x00008000	/* set if we want to have the same parent as the cloner */
44 #define CLONE_THREAD	0x00010000	/* Same thread group? */
45 #define CLONE_NEWNS	0x00020000	/* New namespace group? */
46 
47 #define CLONE_SIGNAL	(CLONE_SIGHAND | CLONE_THREAD)
48 
49 /*
50  * These are the constant used to fake the fixed-point load-average
51  * counting. Some notes:
52  *  - 11 bit fractions expand to 22 bits by the multiplies: this gives
53  *    a load-average precision of 10 bits integer + 11 bits fractional
54  *  - if you want to count load-averages more often, you need more
55  *    precision, or rounding will get you. With 2-second counting freq,
56  *    the EXP_n values would be 1981, 2034 and 2043 if still using only
57  *    11 bit fractions.
58  */
59 extern unsigned long avenrun[];		/* Load averages */
60 
61 #define FSHIFT		11		/* nr of bits of precision */
62 #define FIXED_1		(1<<FSHIFT)	/* 1.0 as fixed-point */
63 #define LOAD_FREQ	(5*HZ)		/* 5 sec intervals */
64 #define EXP_1		1884		/* 1/exp(5sec/1min) as fixed-point */
65 #define EXP_5		2014		/* 1/exp(5sec/5min) */
66 #define EXP_15		2037		/* 1/exp(5sec/15min) */
67 
68 #define CALC_LOAD(load,exp,n) \
69 	load *= exp; \
70 	load += n*(FIXED_1-exp); \
71 	load >>= FSHIFT;
72 
73 #define CT_TO_SECS(x)	((x) / HZ)
74 #define CT_TO_USECS(x)	(((x) % HZ) * 1000000/HZ)
75 
76 extern int nr_running, nr_threads;
77 extern int last_pid;
78 
79 #include <linux/fs.h>
80 #include <linux/time.h>
81 #include <linux/param.h>
82 #include <linux/resource.h>
83 #ifdef __KERNEL__
84 #include <linux/timer.h>
85 #endif
86 
87 #include <asm/processor.h>
88 
89 #define TASK_RUNNING		0
90 #define TASK_INTERRUPTIBLE	1
91 #define TASK_UNINTERRUPTIBLE	2
92 #define TASK_ZOMBIE		4
93 #define TASK_STOPPED		8
94 
95 #define __set_task_state(tsk, state_value)		\
96 	do { (tsk)->state = (state_value); } while (0)
97 #define set_task_state(tsk, state_value)		\
98 	set_mb((tsk)->state, (state_value))
99 
100 #define __set_current_state(state_value)			\
101 	do { current->state = (state_value); } while (0)
102 #define set_current_state(state_value)		\
103 	set_mb(current->state, (state_value))
104 
105 /*
106  * Scheduling policies
107  */
108 #define SCHED_OTHER		0
109 #define SCHED_FIFO		1
110 #define SCHED_RR		2
111 
112 /*
113  * This is an additional bit set when we want to
114  * yield the CPU for one re-schedule..
115  */
116 #define SCHED_YIELD		0x10
117 
118 struct sched_param {
119 	int sched_priority;
120 };
121 
122 struct completion;
123 
124 #ifdef __KERNEL__
125 
126 #include <linux/spinlock.h>
127 
128 /*
129  * This serializes "schedule()" and also protects
130  * the run-queue from deletions/modifications (but
131  * _adding_ to the beginning of the run-queue has
132  * a separate lock).
133  */
134 extern rwlock_t tasklist_lock;
135 extern spinlock_t runqueue_lock;
136 extern spinlock_t mmlist_lock;
137 
138 extern void sched_init(void);
139 extern void init_idle(void);
140 extern void show_state(void);
141 extern void cpu_init (void);
142 extern void trap_init(void);
143 extern void update_process_times(int user);
144 extern void update_one_process(struct task_struct *p, unsigned long user,
145 			       unsigned long system, int cpu);
146 
147 #define	MAX_SCHEDULE_TIMEOUT	LONG_MAX
148 extern signed long FASTCALL(schedule_timeout(signed long timeout));
149 asmlinkage void schedule(void);
150 
151 extern int schedule_task(struct tq_struct *task);
152 extern void flush_scheduled_tasks(void);
153 extern int start_context_thread(void);
154 extern int current_is_keventd(void);
155 
156 #if CONFIG_SMP
157 extern void set_cpus_allowed(struct task_struct *p, unsigned long new_mask);
158 #else
159 # define set_cpus_allowed(p, new_mask) do { } while (0)
160 #endif
161 
162 /*
163  * The default fd array needs to be at least BITS_PER_LONG,
164  * as this is the granularity returned by copy_fdset().
165  */
166 #define NR_OPEN_DEFAULT BITS_PER_LONG
167 
168 struct namespace;
169 /*
170  * Open file table structure
171  */
172 struct files_struct {
173 	atomic_t count;
174 	rwlock_t file_lock;	/* Protects all the below members.  Nests inside tsk->alloc_lock */
175 	int max_fds;
176 	int max_fdset;
177 	int next_fd;
178 	struct file ** fd;	/* current fd array */
179 	fd_set *close_on_exec;
180 	fd_set *open_fds;
181 	fd_set close_on_exec_init;
182 	fd_set open_fds_init;
183 	struct file * fd_array[NR_OPEN_DEFAULT];
184 };
185 
186 #define INIT_FILES \
187 { 							\
188 	count:		ATOMIC_INIT(1), 		\
189 	file_lock:	RW_LOCK_UNLOCKED, 		\
190 	max_fds:	NR_OPEN_DEFAULT, 		\
191 	max_fdset:	__FD_SETSIZE, 			\
192 	next_fd:	0, 				\
193 	fd:		&init_files.fd_array[0], 	\
194 	close_on_exec:	&init_files.close_on_exec_init, \
195 	open_fds:	&init_files.open_fds_init, 	\
196 	close_on_exec_init: { { 0, } }, 		\
197 	open_fds_init:	{ { 0, } }, 			\
198 	fd_array:	{ NULL, } 			\
199 }
200 
201 /* Maximum number of active map areas.. This is a random (large) number */
202 #define DEFAULT_MAX_MAP_COUNT	(65536)
203 
204 extern int max_map_count;
205 
206 struct mm_struct {
207 	struct vm_area_struct * mmap;		/* list of VMAs */
208 	rb_root_t mm_rb;
209 	struct vm_area_struct * mmap_cache;	/* last find_vma result */
210 	pgd_t * pgd;
211 	atomic_t mm_users;			/* How many users with user space? */
212 	atomic_t mm_count;			/* How many references to "struct mm_struct" (users count as 1) */
213 	int map_count;				/* number of VMAs */
214 	struct rw_semaphore mmap_sem;
215 	spinlock_t page_table_lock;		/* Protects task page tables and mm->rss */
216 
217 	struct list_head mmlist;		/* List of all active mm's.  These are globally strung
218 						 * together off init_mm.mmlist, and are protected
219 						 * by mmlist_lock
220 						 */
221 
222 	unsigned long start_code, end_code, start_data, end_data;
223 	unsigned long start_brk, brk, start_stack;
224 	unsigned long arg_start, arg_end, env_start, env_end;
225 	unsigned long rss, total_vm, locked_vm;
226 	unsigned long def_flags;
227 	unsigned long cpu_vm_mask;
228 	unsigned long swap_address;
229 
230 	unsigned dumpable:1;
231 
232 	/* Architecture-specific MM context */
233 	mm_context_t context;
234 };
235 
236 extern int mmlist_nr;
237 
238 #define INIT_MM(name) \
239 {			 				\
240 	mm_rb:		RB_ROOT,			\
241 	pgd:		swapper_pg_dir, 		\
242 	mm_users:	ATOMIC_INIT(2), 		\
243 	mm_count:	ATOMIC_INIT(1), 		\
244 	mmap_sem:	__RWSEM_INITIALIZER(name.mmap_sem), \
245 	page_table_lock: SPIN_LOCK_UNLOCKED, 		\
246 	mmlist:		LIST_HEAD_INIT(name.mmlist),	\
247 }
248 
249 struct signal_struct {
250 	atomic_t		count;
251 	struct k_sigaction	action[_NSIG];
252 	spinlock_t		siglock;
253 };
254 
255 
256 #define INIT_SIGNALS {	\
257 	count:		ATOMIC_INIT(1), 		\
258 	action:		{ {{0,}}, }, 			\
259 	siglock:	SPIN_LOCK_UNLOCKED 		\
260 }
261 
262 /*
263  * Some day this will be a full-fledged user tracking system..
264  */
265 struct user_struct {
266 	atomic_t __count;	/* reference count */
267 	atomic_t processes;	/* How many processes does this user have? */
268 	atomic_t files;		/* How many open files does this user have? */
269 
270 	/* Hash table maintenance information */
271 	struct user_struct *next, **pprev;
272 	uid_t uid;
273 };
274 
275 #define get_current_user() ({ 				\
276 	struct user_struct *__tmp_user = current->user;	\
277 	atomic_inc(&__tmp_user->__count);		\
278 	__tmp_user; })
279 
280 extern struct user_struct root_user;
281 #define INIT_USER (&root_user)
282 
283 struct task_struct {
284 	/*
285 	 * offsets of these are hardcoded elsewhere - touch with care
286 	 */
287 	volatile long state;	/* -1 unrunnable, 0 runnable, >0 stopped */
288 	unsigned long flags;	/* per process flags, defined below */
289 	int sigpending;
290 	mm_segment_t addr_limit;	/* thread address space:
291 					 	0-0xBFFFFFFF for user-thead
292 						0-0xFFFFFFFF for kernel-thread
293 					 */
294 	struct exec_domain *exec_domain;
295 	volatile long need_resched;
296 	unsigned long ptrace;
297 
298 	int lock_depth;		/* Lock depth */
299 
300 /*
301  * offset 32 begins here on 32-bit platforms. We keep
302  * all fields in a single cacheline that are needed for
303  * the goodness() loop in schedule().
304  */
305 	long counter;
306 	long nice;
307 	unsigned long policy;
308 	struct mm_struct *mm;
309 	int processor;
310 	/*
311 	 * cpus_runnable is ~0 if the process is not running on any
312 	 * CPU. It's (1 << cpu) if it's running on a CPU. This mask
313 	 * is updated under the runqueue lock.
314 	 *
315 	 * To determine whether a process might run on a CPU, this
316 	 * mask is AND-ed with cpus_allowed.
317 	 */
318 	unsigned long cpus_runnable, cpus_allowed;
319 	/*
320 	 * (only the 'next' pointer fits into the cacheline, but
321 	 * that's just fine.)
322 	 */
323 	struct list_head run_list;
324 	unsigned long sleep_time;
325 
326 	struct task_struct *next_task, *prev_task;
327 	struct mm_struct *active_mm;
328 	struct list_head local_pages;
329 	unsigned int allocation_order, nr_local_pages;
330 
331 /* task state */
332 	struct linux_binfmt *binfmt;
333 	int exit_code, exit_signal;
334 	int pdeath_signal;  /*  The signal sent when the parent dies  */
335 	/* ??? */
336 	unsigned long personality;
337 	int did_exec:1;
338 	unsigned task_dumpable:1;
339 	pid_t pid;
340 	pid_t pgrp;
341 	pid_t tty_old_pgrp;
342 	pid_t session;
343 	pid_t tgid;
344 	/* boolean value for session group leader */
345 	int leader;
346 	/*
347 	 * pointers to (original) parent process, youngest child, younger sibling,
348 	 * older sibling, respectively.  (p->father can be replaced with
349 	 * p->p_pptr->pid)
350 	 */
351 	struct task_struct *p_opptr, *p_pptr, *p_cptr, *p_ysptr, *p_osptr;
352 	struct list_head thread_group;
353 
354 	/* PID hash table linkage. */
355 	struct task_struct *pidhash_next;
356 	struct task_struct **pidhash_pprev;
357 
358 	wait_queue_head_t wait_chldexit;	/* for wait4() */
359 	struct completion *vfork_done;		/* for vfork() */
360 	unsigned long rt_priority;
361 	unsigned long it_real_value, it_prof_value, it_virt_value;
362 	unsigned long it_real_incr, it_prof_incr, it_virt_incr;
363 	struct timer_list real_timer;
364 	struct tms times;
365 	unsigned long start_time;
366 	long per_cpu_utime[NR_CPUS], per_cpu_stime[NR_CPUS];
367 /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
368 	unsigned long min_flt, maj_flt, nswap, cmin_flt, cmaj_flt, cnswap;
369 	int swappable:1;
370 /* process credentials */
371 	uid_t uid,euid,suid,fsuid;
372 	gid_t gid,egid,sgid,fsgid;
373 	int ngroups;
374 	gid_t	groups[NGROUPS];
375 	kernel_cap_t   cap_effective, cap_inheritable, cap_permitted;
376 	int keep_capabilities:1;
377 	struct user_struct *user;
378 /* limits */
379 	struct rlimit rlim[RLIM_NLIMITS];
380 	unsigned short used_math;
381 	char comm[16];
382 /* file system info */
383 	int link_count, total_link_count;
384 	struct tty_struct *tty; /* NULL if no tty */
385 	unsigned int locks; /* How many file locks are being held */
386 /* ipc stuff */
387 	struct sem_undo *semundo;
388 	struct sem_queue *semsleeping;
389 /* CPU-specific state of this task */
390 	struct thread_struct thread;
391 /* filesystem information */
392 	struct fs_struct *fs;
393 /* open file information */
394 	struct files_struct *files;
395 /* namespace */
396 	struct namespace *namespace;
397 /* signal handlers */
398 	spinlock_t sigmask_lock;	/* Protects signal and blocked */
399 	struct signal_struct *sig;
400 
401 	sigset_t blocked;
402 	struct sigpending pending;
403 
404 	unsigned long sas_ss_sp;
405 	size_t sas_ss_size;
406 	int (*notifier)(void *priv);
407 	void *notifier_data;
408 	sigset_t *notifier_mask;
409 
410 /* Thread group tracking */
411    	u32 parent_exec_id;
412    	u32 self_exec_id;
413 /* Protection of (de-)allocation: mm, files, fs, tty */
414 	spinlock_t alloc_lock;
415 
416 /* journalling filesystem info */
417 	void *journal_info;
418 
419 	struct list_head *scm_work_list;
420 };
421 
422 /*
423  * Per process flags
424  */
425 #define PF_ALIGNWARN	0x00000001	/* Print alignment warning msgs */
426 					/* Not implemented yet, only for 486*/
427 #define PF_STARTING	0x00000002	/* being created */
428 #define PF_EXITING	0x00000004	/* getting shut down */
429 #define PF_FORKNOEXEC	0x00000040	/* forked but didn't exec */
430 #define PF_SUPERPRIV	0x00000100	/* used super-user privileges */
431 #define PF_DUMPCORE	0x00000200	/* dumped core */
432 #define PF_SIGNALED	0x00000400	/* killed by a signal */
433 #define PF_MEMALLOC	0x00000800	/* Allocating memory */
434 #define PF_MEMDIE      0x00001000       /* Killed for out-of-memory */
435 #define PF_FREE_PAGES	0x00002000	/* per process page freeing */
436 #define PF_NOIO		0x00004000	/* avoid generating further I/O */
437 #define PF_FSTRANS	0x00008000	/* inside a filesystem transaction */
438 
439 #define PF_USEDFPU	0x00100000	/* task used FPU this quantum (SMP) */
440 
441 /*
442  * Ptrace flags
443  */
444 
445 #define PT_PTRACED	0x00000001
446 #define PT_TRACESYS	0x00000002
447 #define PT_DTRACE	0x00000004	/* delayed trace (used on m68k, i386) */
448 #define PT_TRACESYSGOOD	0x00000008
449 #define PT_PTRACE_CAP	0x00000010	/* ptracer can follow suid-exec */
450 
451 #define is_dumpable(tsk)    ((tsk)->task_dumpable && (tsk)->mm && (tsk)->mm->dumpable)
452 
453 /*
454  * Limit the stack by to some sane default: root can always
455  * increase this limit if needed..  8MB seems reasonable.
456  */
457 #define _STK_LIM	(8*1024*1024)
458 
459 #define DEF_COUNTER	(10*HZ/100)	/* 100 ms time slice */
460 #define MAX_COUNTER	(20*HZ/100)
461 #define DEF_NICE	(0)
462 
463 extern void yield(void);
464 
465 /*
466  * The default (Linux) execution domain.
467  */
468 extern struct exec_domain	default_exec_domain;
469 
470 /*
471  *  INIT_TASK is used to set up the first task table, touch at
472  * your own risk!. Base=0, limit=0x1fffff (=2MB)
473  */
474 #define INIT_TASK(tsk)	\
475 {									\
476     state:		0,						\
477     flags:		0,						\
478     sigpending:		0,						\
479     addr_limit:		KERNEL_DS,					\
480     exec_domain:	&default_exec_domain,				\
481     lock_depth:		-1,						\
482     counter:		DEF_COUNTER,					\
483     nice:		DEF_NICE,					\
484     policy:		SCHED_OTHER,					\
485     mm:			NULL,						\
486     active_mm:		&init_mm,					\
487     cpus_runnable:	~0UL,						\
488     cpus_allowed:	~0UL,						\
489     run_list:		LIST_HEAD_INIT(tsk.run_list),			\
490     next_task:		&tsk,						\
491     prev_task:		&tsk,						\
492     p_opptr:		&tsk,						\
493     p_pptr:		&tsk,						\
494     thread_group:	LIST_HEAD_INIT(tsk.thread_group),		\
495     wait_chldexit:	__WAIT_QUEUE_HEAD_INITIALIZER(tsk.wait_chldexit),\
496     real_timer:		{						\
497 	function:		it_real_fn				\
498     },									\
499     cap_effective:	CAP_INIT_EFF_SET,				\
500     cap_inheritable:	CAP_INIT_INH_SET,				\
501     cap_permitted:	CAP_FULL_SET,					\
502     keep_capabilities:	0,						\
503     rlim:		INIT_RLIMITS,					\
504     user:		INIT_USER,					\
505     comm:		"swapper",					\
506     thread:		INIT_THREAD,					\
507     fs:			&init_fs,					\
508     files:		&init_files,					\
509     sigmask_lock:	SPIN_LOCK_UNLOCKED,				\
510     sig:		&init_signals,					\
511     pending:		{ NULL, &tsk.pending.head, {{0}}},		\
512     blocked:		{{0}},						\
513     alloc_lock:		SPIN_LOCK_UNLOCKED,				\
514     journal_info:	NULL,						\
515 }
516 
517 
518 #ifndef INIT_TASK_SIZE
519 # define INIT_TASK_SIZE	2048*sizeof(long)
520 #endif
521 
522 union task_union {
523 	struct task_struct task;
524 	unsigned long stack[INIT_TASK_SIZE/sizeof(long)];
525 };
526 
527 extern union task_union init_task_union;
528 
529 extern struct   mm_struct init_mm;
530 extern struct task_struct *init_tasks[NR_CPUS];
531 
532 /* PID hashing. (shouldnt this be dynamic?) */
533 #define PIDHASH_SZ (4096 >> 2)
534 extern struct task_struct *pidhash[PIDHASH_SZ];
535 
536 #define pid_hashfn(x)	((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1))
537 
hash_pid(struct task_struct * p)538 static inline void hash_pid(struct task_struct *p)
539 {
540 	struct task_struct **htable = &pidhash[pid_hashfn(p->pid)];
541 
542 	if((p->pidhash_next = *htable) != NULL)
543 		(*htable)->pidhash_pprev = &p->pidhash_next;
544 	*htable = p;
545 	p->pidhash_pprev = htable;
546 }
547 
unhash_pid(struct task_struct * p)548 static inline void unhash_pid(struct task_struct *p)
549 {
550 	if(p->pidhash_next)
551 		p->pidhash_next->pidhash_pprev = p->pidhash_pprev;
552 	*p->pidhash_pprev = p->pidhash_next;
553 }
554 
find_task_by_pid(int pid)555 static inline struct task_struct *find_task_by_pid(int pid)
556 {
557 	struct task_struct *p, **htable = &pidhash[pid_hashfn(pid)];
558 
559 	for(p = *htable; p && p->pid != pid; p = p->pidhash_next)
560 		;
561 
562 	return p;
563 }
564 
565 #define task_has_cpu(tsk) ((tsk)->cpus_runnable != ~0UL)
566 
task_set_cpu(struct task_struct * tsk,unsigned int cpu)567 static inline void task_set_cpu(struct task_struct *tsk, unsigned int cpu)
568 {
569 	tsk->processor = cpu;
570 	tsk->cpus_runnable = 1UL << cpu;
571 }
572 
task_release_cpu(struct task_struct * tsk)573 static inline void task_release_cpu(struct task_struct *tsk)
574 {
575 	tsk->cpus_runnable = ~0UL;
576 }
577 
578 /* per-UID process charging. */
579 extern struct user_struct * alloc_uid(uid_t);
580 extern void free_uid(struct user_struct *);
581 extern void switch_uid(struct user_struct *);
582 
583 #include <asm/current.h>
584 
585 extern unsigned long volatile jiffies;
586 extern unsigned long itimer_ticks;
587 extern unsigned long itimer_next;
588 extern struct timeval xtime;
589 extern void do_timer(struct pt_regs *);
590 
591 extern unsigned int * prof_buffer;
592 extern unsigned long prof_len;
593 extern unsigned long prof_shift;
594 
595 #define CURRENT_TIME (xtime.tv_sec)
596 
597 extern void FASTCALL(__wake_up(wait_queue_head_t *q, unsigned int mode, int nr));
598 extern void FASTCALL(__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr));
599 extern void FASTCALL(sleep_on(wait_queue_head_t *q));
600 extern long FASTCALL(sleep_on_timeout(wait_queue_head_t *q,
601 				      signed long timeout));
602 extern void FASTCALL(interruptible_sleep_on(wait_queue_head_t *q));
603 extern long FASTCALL(interruptible_sleep_on_timeout(wait_queue_head_t *q,
604 						    signed long timeout));
605 extern int FASTCALL(wake_up_process(struct task_struct * tsk));
606 
607 #define wake_up(x)			__wake_up((x),TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 1)
608 #define wake_up_nr(x, nr)		__wake_up((x),TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, nr)
609 #define wake_up_all(x)			__wake_up((x),TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 0)
610 #define wake_up_sync(x)			__wake_up_sync((x),TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 1)
611 #define wake_up_sync_nr(x, nr)		__wake_up_sync((x),TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, nr)
612 #define wake_up_interruptible(x)	__wake_up((x),TASK_INTERRUPTIBLE, 1)
613 #define wake_up_interruptible_nr(x, nr)	__wake_up((x),TASK_INTERRUPTIBLE, nr)
614 #define wake_up_interruptible_all(x)	__wake_up((x),TASK_INTERRUPTIBLE, 0)
615 #define wake_up_interruptible_sync(x)	__wake_up_sync((x),TASK_INTERRUPTIBLE, 1)
616 #define wake_up_interruptible_sync_nr(x, nr) __wake_up_sync((x),TASK_INTERRUPTIBLE,  nr)
617 asmlinkage long sys_wait4(pid_t pid,unsigned int * stat_addr, int options, struct rusage * ru);
618 
619 extern int in_group_p(gid_t);
620 extern int in_egroup_p(gid_t);
621 
622 extern void proc_caches_init(void);
623 extern void flush_signals(struct task_struct *);
624 extern void flush_signal_handlers(struct task_struct *);
625 extern void sig_exit(int, int, struct siginfo *);
626 extern int dequeue_signal(sigset_t *, siginfo_t *);
627 extern void block_all_signals(int (*notifier)(void *priv), void *priv,
628 			      sigset_t *mask);
629 extern void unblock_all_signals(void);
630 extern int send_sig_info(int, struct siginfo *, struct task_struct *);
631 extern int force_sig_info(int, struct siginfo *, struct task_struct *);
632 extern int kill_pg_info(int, struct siginfo *, pid_t);
633 extern int kill_sl_info(int, struct siginfo *, pid_t);
634 extern int kill_proc_info(int, struct siginfo *, pid_t);
635 extern void notify_parent(struct task_struct *, int);
636 extern void do_notify_parent(struct task_struct *, int);
637 extern void force_sig(int, struct task_struct *);
638 extern int send_sig(int, struct task_struct *, int);
639 extern int kill_pg(pid_t, int, int);
640 extern int kill_sl(pid_t, int, int);
641 extern int kill_proc(pid_t, int, int);
642 extern int do_sigaction(int, const struct k_sigaction *, struct k_sigaction *);
643 extern int do_sigaltstack(const stack_t *, stack_t *, unsigned long);
644 
signal_pending(struct task_struct * p)645 static inline int signal_pending(struct task_struct *p)
646 {
647 	return (p->sigpending != 0);
648 }
649 
650 /*
651  * Re-calculate pending state from the set of locally pending
652  * signals, globally pending signals, and blocked signals.
653  */
has_pending_signals(sigset_t * signal,sigset_t * blocked)654 static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
655 {
656 	unsigned long ready;
657 	long i;
658 
659 	switch (_NSIG_WORDS) {
660 	default:
661 		for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
662 			ready |= signal->sig[i] &~ blocked->sig[i];
663 		break;
664 
665 	case 4: ready  = signal->sig[3] &~ blocked->sig[3];
666 		ready |= signal->sig[2] &~ blocked->sig[2];
667 		ready |= signal->sig[1] &~ blocked->sig[1];
668 		ready |= signal->sig[0] &~ blocked->sig[0];
669 		break;
670 
671 	case 2: ready  = signal->sig[1] &~ blocked->sig[1];
672 		ready |= signal->sig[0] &~ blocked->sig[0];
673 		break;
674 
675 	case 1: ready  = signal->sig[0] &~ blocked->sig[0];
676 	}
677 	return ready !=	0;
678 }
679 
680 /* Reevaluate whether the task has signals pending delivery.
681    This is required every time the blocked sigset_t changes.
682    All callers should have t->sigmask_lock.  */
683 
recalc_sigpending(struct task_struct * t)684 static inline void recalc_sigpending(struct task_struct *t)
685 {
686 	t->sigpending = has_pending_signals(&t->pending.signal, &t->blocked);
687 }
688 
689 /* True if we are on the alternate signal stack.  */
690 
on_sig_stack(unsigned long sp)691 static inline int on_sig_stack(unsigned long sp)
692 {
693 	return (sp - current->sas_ss_sp < current->sas_ss_size);
694 }
695 
sas_ss_flags(unsigned long sp)696 static inline int sas_ss_flags(unsigned long sp)
697 {
698 	return (current->sas_ss_size == 0 ? SS_DISABLE
699 		: on_sig_stack(sp) ? SS_ONSTACK : 0);
700 }
701 
702 extern int request_irq(unsigned int,
703 		       void (*handler)(int, void *, struct pt_regs *),
704 		       unsigned long, const char *, void *);
705 extern void free_irq(unsigned int, void *);
706 
707 /*
708  * This has now become a routine instead of a macro, it sets a flag if
709  * it returns true (to do BSD-style accounting where the process is flagged
710  * if it uses root privs). The implication of this is that you should do
711  * normal permissions checks first, and check suser() last.
712  *
713  * [Dec 1997 -- Chris Evans]
714  * For correctness, the above considerations need to be extended to
715  * fsuser(). This is done, along with moving fsuser() checks to be
716  * last.
717  *
718  * These will be removed, but in the mean time, when the SECURE_NOROOT
719  * flag is set, uids don't grant privilege.
720  */
suser(void)721 static inline int suser(void)
722 {
723 	if (!issecure(SECURE_NOROOT) && current->euid == 0) {
724 		current->flags |= PF_SUPERPRIV;
725 		return 1;
726 	}
727 	return 0;
728 }
729 
fsuser(void)730 static inline int fsuser(void)
731 {
732 	if (!issecure(SECURE_NOROOT) && current->fsuid == 0) {
733 		current->flags |= PF_SUPERPRIV;
734 		return 1;
735 	}
736 	return 0;
737 }
738 
739 /*
740  * capable() checks for a particular capability.
741  * New privilege checks should use this interface, rather than suser() or
742  * fsuser(). See include/linux/capability.h for defined capabilities.
743  */
744 
capable(int cap)745 static inline int capable(int cap)
746 {
747 #if 1 /* ok now */
748 	if (cap_raised(current->cap_effective, cap))
749 #else
750 	if (cap_is_fs_cap(cap) ? current->fsuid == 0 : current->euid == 0)
751 #endif
752 	{
753 		current->flags |= PF_SUPERPRIV;
754 		return 1;
755 	}
756 	return 0;
757 }
758 
759 /*
760  * Routines for handling mm_structs
761  */
762 extern struct mm_struct * mm_alloc(void);
763 
764 extern struct mm_struct * start_lazy_tlb(void);
765 extern void end_lazy_tlb(struct mm_struct *mm);
766 
767 /* mmdrop drops the mm and the page tables */
768 extern void FASTCALL(__mmdrop(struct mm_struct *));
mmdrop(struct mm_struct * mm)769 static inline void mmdrop(struct mm_struct * mm)
770 {
771 	if (atomic_dec_and_test(&mm->mm_count))
772 		__mmdrop(mm);
773 }
774 
775 /* mmput gets rid of the mappings and all user-space */
776 extern void mmput(struct mm_struct *);
777 /* Remove the current tasks stale references to the old mm_struct */
778 extern void mm_release(void);
779 
780 /*
781  * Routines for handling the fd arrays
782  */
783 extern struct file ** alloc_fd_array(int);
784 extern int expand_fd_array(struct files_struct *, int nr);
785 extern void free_fd_array(struct file **, int);
786 
787 extern fd_set *alloc_fdset(int);
788 extern int expand_fdset(struct files_struct *, int nr);
789 extern void free_fdset(fd_set *, int);
790 
791 extern int  copy_thread(int, unsigned long, unsigned long, unsigned long, struct task_struct *, struct pt_regs *);
792 extern void flush_thread(void);
793 extern void exit_thread(void);
794 
795 extern void exit_mm(struct task_struct *);
796 extern void exit_files(struct task_struct *);
797 extern void exit_sighand(struct task_struct *);
798 
799 extern void reparent_to_init(void);
800 extern void daemonize(void);
801 
802 extern int do_execve(char *, char **, char **, struct pt_regs *);
803 extern int do_fork(unsigned long, unsigned long, struct pt_regs *, unsigned long);
804 
805 extern void set_task_comm(struct task_struct *tsk, char *from);
806 extern void get_task_comm(char *to, struct task_struct *tsk);
807 
808 extern void FASTCALL(add_wait_queue(wait_queue_head_t *q, wait_queue_t * wait));
809 extern void FASTCALL(add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t * wait));
810 extern void FASTCALL(remove_wait_queue(wait_queue_head_t *q, wait_queue_t * wait));
811 
812 extern long kernel_thread(int (*fn)(void *), void * arg, unsigned long flags);
813 
814 #define __wait_event(wq, condition) 					\
815 do {									\
816 	wait_queue_t __wait;						\
817 	init_waitqueue_entry(&__wait, current);				\
818 									\
819 	add_wait_queue(&wq, &__wait);					\
820 	for (;;) {							\
821 		set_current_state(TASK_UNINTERRUPTIBLE);		\
822 		if (condition)						\
823 			break;						\
824 		schedule();						\
825 	}								\
826 	current->state = TASK_RUNNING;					\
827 	remove_wait_queue(&wq, &__wait);				\
828 } while (0)
829 
830 #define wait_event(wq, condition) 					\
831 do {									\
832 	if (condition)	 						\
833 		break;							\
834 	__wait_event(wq, condition);					\
835 } while (0)
836 
837 #define __wait_event_interruptible(wq, condition, ret)			\
838 do {									\
839 	wait_queue_t __wait;						\
840 	init_waitqueue_entry(&__wait, current);				\
841 									\
842 	add_wait_queue(&wq, &__wait);					\
843 	for (;;) {							\
844 		set_current_state(TASK_INTERRUPTIBLE);			\
845 		if (condition)						\
846 			break;						\
847 		if (!signal_pending(current)) {				\
848 			schedule();					\
849 			continue;					\
850 		}							\
851 		ret = -ERESTARTSYS;					\
852 		break;							\
853 	}								\
854 	current->state = TASK_RUNNING;					\
855 	remove_wait_queue(&wq, &__wait);				\
856 } while (0)
857 
858 #define wait_event_interruptible(wq, condition)				\
859 ({									\
860 	int __ret = 0;							\
861 	if (!(condition))						\
862 		__wait_event_interruptible(wq, condition, __ret);	\
863 	__ret;								\
864 })
865 
866 #define REMOVE_LINKS(p) do { \
867 	(p)->next_task->prev_task = (p)->prev_task; \
868 	(p)->prev_task->next_task = (p)->next_task; \
869 	if ((p)->p_osptr) \
870 		(p)->p_osptr->p_ysptr = (p)->p_ysptr; \
871 	if ((p)->p_ysptr) \
872 		(p)->p_ysptr->p_osptr = (p)->p_osptr; \
873 	else \
874 		(p)->p_pptr->p_cptr = (p)->p_osptr; \
875 	} while (0)
876 
877 #define SET_LINKS(p) do { \
878 	(p)->next_task = &init_task; \
879 	(p)->prev_task = init_task.prev_task; \
880 	init_task.prev_task->next_task = (p); \
881 	init_task.prev_task = (p); \
882 	(p)->p_ysptr = NULL; \
883 	if (((p)->p_osptr = (p)->p_pptr->p_cptr) != NULL) \
884 		(p)->p_osptr->p_ysptr = p; \
885 	(p)->p_pptr->p_cptr = p; \
886 	} while (0)
887 
888 #define for_each_task(p) \
889 	for (p = &init_task ; (p = p->next_task) != &init_task ; )
890 
891 #define for_each_thread(task) \
892 	for (task = next_thread(current) ; task != current ; task = next_thread(task))
893 
894 #define next_thread(p) \
895 	list_entry((p)->thread_group.next, struct task_struct, thread_group)
896 
897 #define thread_group_leader(p)	(p->pid == p->tgid)
898 
del_from_runqueue(struct task_struct * p)899 static inline void del_from_runqueue(struct task_struct * p)
900 {
901 	nr_running--;
902 	p->sleep_time = jiffies;
903 	list_del(&p->run_list);
904 	p->run_list.next = NULL;
905 }
906 
task_on_runqueue(struct task_struct * p)907 static inline int task_on_runqueue(struct task_struct *p)
908 {
909 	return (p->run_list.next != NULL);
910 }
911 
unhash_process(struct task_struct * p)912 static inline void unhash_process(struct task_struct *p)
913 {
914 	if (task_on_runqueue(p))
915 		out_of_line_bug();
916 	write_lock_irq(&tasklist_lock);
917 	nr_threads--;
918 	unhash_pid(p);
919 	REMOVE_LINKS(p);
920 	list_del(&p->thread_group);
921 	write_unlock_irq(&tasklist_lock);
922 }
923 
924 /* Protects ->fs, ->files, ->mm, and synchronises with wait4().  Nests inside tasklist_lock */
task_lock(struct task_struct * p)925 static inline void task_lock(struct task_struct *p)
926 {
927 	spin_lock(&p->alloc_lock);
928 }
929 
task_unlock(struct task_struct * p)930 static inline void task_unlock(struct task_struct *p)
931 {
932 	spin_unlock(&p->alloc_lock);
933 }
934 
935 /* write full pathname into buffer and return start of pathname */
d_path(struct dentry * dentry,struct vfsmount * vfsmnt,char * buf,int buflen)936 static inline char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
937 				char *buf, int buflen)
938 {
939 	char *res;
940 	struct vfsmount *rootmnt;
941 	struct dentry *root;
942 	read_lock(&current->fs->lock);
943 	rootmnt = mntget(current->fs->rootmnt);
944 	root = dget(current->fs->root);
945 	read_unlock(&current->fs->lock);
946 	spin_lock(&dcache_lock);
947 	res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
948 	spin_unlock(&dcache_lock);
949 	dput(root);
950 	mntput(rootmnt);
951 	return res;
952 }
953 
need_resched(void)954 static inline int need_resched(void)
955 {
956 	return (unlikely(current->need_resched));
957 }
958 
959 extern void __cond_resched(void);
cond_resched(void)960 static inline void cond_resched(void)
961 {
962 	if (need_resched())
963 		__cond_resched();
964 }
965 
966 #endif /* __KERNEL__ */
967 #endif
968