xref: /netbsd/sys/sys/proc.h (revision c4a72b64)
1 /*	$NetBSD: proc.h,v 1.148 2002/11/07 00:22:28 manu Exp $	*/
2 
3 /*-
4  * Copyright (c) 1986, 1989, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)proc.h	8.15 (Berkeley) 5/19/95
41  */
42 
43 #ifndef _SYS_PROC_H_
44 #define	_SYS_PROC_H_
45 
46 #if defined(_KERNEL_OPT)
47 #include "opt_multiprocessor.h"
48 #include "opt_kstack.h"
49 #endif
50 
51 #if defined(_KERNEL)
52 #include <machine/cpu.h>		/* curcpu() and cpu_info */
53 #endif
54 #include <machine/proc.h>		/* Machine-dependent proc substruct */
55 #include <sys/lock.h>
56 #include <sys/queue.h>
57 #include <sys/callout.h>
58 #include <sys/signalvar.h>
59 #include <sys/event.h>
60 
61 /*
62  * One structure allocated per session.
63  */
64 struct session {
65 	int		s_count;	/* Ref cnt; pgrps in session */
66 	struct proc	*s_leader;	/* Session leader */
67 	struct vnode	*s_ttyvp;	/* Vnode of controlling terminal */
68 	struct tty	*s_ttyp;	/* Controlling terminal */
69 	char		s_login[MAXLOGNAME]; /* Setlogin() name */
70 	pid_t		s_sid;		/* Session ID (pid of leader) */
71 };
72 
73 /*
74  * One structure allocated per process group.
75  */
76 struct pgrp {
77 	LIST_ENTRY(pgrp) pg_hash;	/* Hash chain */
78 	LIST_HEAD(, proc) pg_members;	/* Pointer to pgrp members */
79 	struct session	*pg_session;	/* Pointer to session */
80 	pid_t		pg_id;		/* Pgrp id */
81 	int		pg_jobc;	/*
82 					 * Number of processes qualifying
83 					 * pgrp for job control
84 					 */
85 };
86 
87 /*
88  * One structure allocated per emulation.
89  */
90 struct exec_package;
91 struct ps_strings;
92 struct ras;
93 
94 struct emul {
95 	const char	*e_name;	/* Symbolic name */
96 	const char	*e_path;	/* Extra emulation path (NULL if none)*/
97 #ifndef __HAVE_MINIMAL_EMUL
98 	int		e_flags;	/* Miscellaneous flags, see above */
99 					/* Syscall handling function */
100 	const int	*e_errno;	/* Errno array */
101 	int		e_nosys;	/* Offset of the nosys() syscall */
102 	int		e_nsysent;	/* Number of system call entries */
103 #endif
104 	const struct sysent *e_sysent;	/* System call array */
105 	const char * const *e_syscallnames; /* System call name array */
106 					/* Signal sending function */
107 	void		(*e_sendsig) __P((int, sigset_t *, u_long));
108 	void		(*e_trapsignal) __P((struct proc *, int, u_long));
109 	char		*e_sigcode;	/* Start of sigcode */
110 	char		*e_esigcode;	/* End of sigcode */
111 					/* Set registers before execution */
112 	void		(*e_setregs) __P((struct proc *, struct exec_package *,
113 				  u_long));
114 
115 					/* Per-process hooks */
116 	void		(*e_proc_exec) __P((struct proc *,
117 					    struct exec_package *));
118 	void		(*e_proc_fork) __P((struct proc *p,
119 					    struct proc *parent));
120 	void		(*e_proc_exit) __P((struct proc *));
121 
122 #ifdef __HAVE_SYSCALL_INTERN
123 	void		(*e_syscall_intern) __P((struct proc *));
124 #else
125 	void		(*e_syscall) __P((void));
126 #endif
127 					/* Emulation specific sysctl */
128 	int		(*e_sysctl) __P((int *, u_int , void *, size_t *,
129 				void *, size_t, struct proc *p));
130 	int		(*e_fault) __P((struct proc *, vaddr_t, int, int));
131 };
132 
133 /*
134  * Emulation miscelaneous flags
135  */
136 #define	EMUL_HAS_SYS___syscall	0x001	/* Has SYS___syscall */
137 
138 /*
139  * Description of a process.
140  *
141  * This structure contains the information needed to manage a thread of
142  * control, known in UN*X as a process; it has references to substructures
143  * containing descriptions of things that the process uses, but may share
144  * with related processes.  The process structure and the substructures
145  * are always addressible except for those marked "(PROC ONLY)" below,
146  * which might be addressible only on a processor on which the process
147  * is running.
148  */
149 struct proc {
150 	struct proc	*p_forw;	/* Doubly-linked run/sleep queue */
151 	struct proc	*p_back;
152 	LIST_ENTRY(proc) p_list;	/* List of all processes */
153 
154 	/* Substructures: */
155 	struct pcred	*p_cred;	/* Process owner's identity */
156 	struct filedesc	*p_fd;		/* Ptr to open files structure */
157 	struct cwdinfo	*p_cwdi;	/* cdir/rdir/cmask info */
158 	struct pstats	*p_stats;	/* Accounting/statistics (PROC ONLY) */
159 	struct plimit	*p_limit;	/* Process limits */
160 	struct vmspace	*p_vmspace;	/* Address space */
161 	struct sigacts	*p_sigacts;	/* Process sigactions (state is below)*/
162 
163 #define	p_ucred		p_cred->pc_ucred
164 #define	p_rlimit	p_limit->pl_rlimit
165 
166 	int		p_exitsig;	/* Signal to sent to parent on exit */
167 	int		p_flag;		/* P_* flags */
168 	struct cpu_info	* __volatile p_cpu;
169 					/* CPU we're running on if SONPROC */
170 	char		p_stat;		/* S* process status */
171 	char		p_pad1[3];
172 
173 	pid_t		p_pid;		/* Process identifier */
174 	LIST_ENTRY(proc) p_hash;	/* Hash chain */
175 	LIST_ENTRY(proc) p_pglist;	/* List of processes in pgrp */
176 	struct proc	*p_pptr;	/* Pointer to parent process */
177 	LIST_ENTRY(proc) p_sibling;	/* List of sibling processes */
178 	LIST_HEAD(, proc) p_children;	/* Pointer to list of children */
179 
180 	LIST_HEAD(, ras) p_raslist;	/* Pointer to RAS queue */
181 	u_int p_nras;			/* number of RASs */
182 	struct simplelock p_raslock;	/* Lock for RAS queue */
183 
184 /*
185  * The following fields are all zeroed upon creation in fork.
186  */
187 #define	p_startzero	p_opptr
188 
189 	struct proc	*p_opptr;	/* Save parent during ptrace. */
190 	int		p_dupfd;	/* Sideways return value from filedescopen. XXX */
191 
192 	/* Scheduling */
193 	u_int		p_estcpu;	/* Time averaged value of p_cpticks. XXX belongs in p_startcopy section */
194 	int		p_cpticks;	/* Ticks of cpu time */
195 	fixpt_t		p_pctcpu;	/* %cpu for this proc during p_swtime */
196 	void		*p_wchan;	/* Sleep address */
197 	struct callout	p_tsleep_ch;	/* Callout for tsleep */
198 	const char	*p_wmesg;	/* Reason for sleep */
199 	u_int		p_swtime;	/* Time swapped in or out */
200 	u_int		p_slptime;	/* Time since last blocked */
201 
202 	struct callout	p_realit_ch;	/* Real time callout */
203 	struct itimerval p_realtimer;	/* Alarm timer */
204 	struct timeval	p_rtime;	/* Real time */
205 	u_quad_t	p_uticks;	/* Statclock hits in user mode */
206 	u_quad_t	p_sticks;	/* Statclock hits in system mode */
207 	u_quad_t	p_iticks;	/* Statclock hits processing intr */
208 
209 	int		p_traceflag;	/* Kernel trace points */
210 	struct file	*p_tracep;	/* Trace to file */
211 	void		*p_systrace;	/* Back pointer to systrace */
212 
213 	struct vnode	*p_textvp;	/* Vnode of executable */
214 
215 	int		p_locks;	/* DEBUG: lockmgr count of held locks */
216 
217 	int		p_holdcnt;	/* If non-zero, don't swap */
218 	const struct emul *p_emul;	/* Emulation information */
219 	void		*p_emuldata;	/*
220 					 * Per-process emulation data, or NULL.
221 					 * Malloc type M_EMULDATA
222 					 */
223 	const struct execsw *p_execsw;	/* Exec package information */
224 	struct klist	p_klist;	/* Knotes attached to this process */
225 
226 /*
227  * End area that is zeroed on creation
228  */
229 #define	p_endzero	p_startcopy
230 
231 /*
232  * The following fields are all copied upon creation in fork.
233  */
234 #define	p_startcopy	p_sigctx.ps_startcopy
235 
236 	struct sigctx	p_sigctx;	/* Signal state */
237 
238 	u_char		p_priority;	/* Process priority */
239 	u_char		p_usrpri;	/* User-priority based on p_cpu and p_nice */
240 	u_char		p_nice;		/* Process "nice" value */
241 	char		p_comm[MAXCOMLEN+1];	/* basename of last exec file */
242 
243 	struct pgrp	*p_pgrp;	/* Pointer to process group */
244 	void		*p_ctxlink;	/* uc_link {get,set}context */
245 
246 	struct ps_strings *p_psstr;	/* Address of process's ps_strings */
247 	size_t		p_psargv;	/* Offset of ps_argvstr in above */
248 	size_t		p_psnargv;	/* Offset of ps_nargvstr in above */
249 	size_t		p_psenv;	/* Offset of ps_envstr in above */
250 	size_t		p_psnenv;	/* Offset of ps_nenvstr in above */
251 
252 /*
253  * End area that is copied on creation
254  */
255 #define	p_endcopy	p_thread
256 
257 	void		*p_thread;	/* Id for this "thread"; Mach glue. XXX */
258 	struct user	*p_addr;	/* Kernel virtual addr of u-area (PROC ONLY) */
259 	struct mdproc	p_md;		/* Any machine-dependent fields */
260 
261 	u_short		p_xstat;	/* Exit status for wait; also stop signal */
262 	u_short		p_acflag;	/* Accounting flags */
263 	struct rusage	*p_ru;		/* Exit information. XXX */
264 };
265 
266 #define	p_session	p_pgrp->pg_session
267 #define	p_pgid		p_pgrp->pg_id
268 
269 /*
270  * Status values.
271  *
272  * A note about SRUN and SONPROC: SRUN indicates that a process is
273  * runnable but *not* yet running, i.e. is on a run queue.  SONPROC
274  * indicates that the process is actually executing on a CPU, i.e.
275  * it is no longer on a run queue.
276  */
277 #define	SIDL		1		/* Process being created by fork */
278 #define	SRUN		2		/* Currently runnable */
279 #define	SSLEEP		3		/* Sleeping on an address */
280 #define	SSTOP		4		/* Process debugging or suspension */
281 #define	SZOMB		5		/* Awaiting collection by parent */
282 #define	SDEAD		6		/* Process is almost a zombie */
283 #define	SONPROC		7		/* Process is currently on a CPU */
284 
285 #define	P_ZOMBIE(p)	((p)->p_stat == SZOMB || (p)->p_stat == SDEAD)
286 
287 /* These flags are kept in p_flag. */
288 #define	P_ADVLOCK	0x000001 /* Process may hold a POSIX advisory lock */
289 #define	P_CONTROLT	0x000002 /* Has a controlling terminal */
290 #define	P_INMEM		0x000004 /* Loaded into memory */
291 #define	P_NOCLDSTOP	0x000008 /* No SIGCHLD when children stop */
292 #define	P_PPWAIT	0x000010 /* Parent is waiting for child to exec/exit */
293 #define	P_PROFIL	0x000020 /* Has started profiling */
294 #define	P_SELECT	0x000040 /* Selecting; wakeup/waiting danger */
295 #define	P_SINTR		0x000080 /* Sleep is interruptible */
296 #define	P_SUGID		0x000100 /* Had set id privileges since last exec */
297 #define	P_SYSTEM	0x000200 /* System proc: no sigs, stats or swapping */
298 #define	P_TIMEOUT	0x000400 /* Timing out during sleep */
299 #define	P_TRACED	0x000800 /* Debugged process being traced */
300 #define	P_WAITED	0x001000 /* Debugging process has waited for child */
301 #define	P_WEXIT		0x002000 /* Working on exiting */
302 #define	P_EXEC		0x004000 /* Process called exec */
303 #define	P_OWEUPC	0x008000 /* Owe process an addupc() call at next ast */
304 #define	P_FSTRACE	0x010000 /* Debugger process being traced by procfs */
305 #define	P_NOCLDWAIT	0x020000 /* No zombies if child dies */
306 #define	P_32		0x040000 /* 32-bit process (used on 64-bit kernels) */
307 #define	P_BIGLOCK	0x080000 /* Process needs kernel "big lock" to run */
308 #define	P_INEXEC	0x100000 /* Process is exec'ing and cannot be traced */
309 #define	P_SYSTRACE	0x200000 /* Process system call tracing active */
310 #define	P_CHTRACED	0x400000 /* Child has been traced & reparented */
311 #define	P_STOPFORK	0x800000 /* Child will be stopped on fork(2) */
312 #define	P_STOPEXEC	0x1000000 /* Will be stopped on exec(2) */
313 
314 /*
315  * Macro to compute the exit signal to be delivered.
316  */
317 #define	P_EXITSIG(p)	(((p)->p_flag & (P_TRACED|P_FSTRACE)) ? SIGCHLD : \
318 			 p->p_exitsig)
319 
320 /*
321  * MOVE TO ucred.h?
322  *
323  * Shareable process credentials (always resident).  This includes a reference
324  * to the current user credentials as well as real and saved ids that may be
325  * used to change ids.
326  */
327 struct pcred {
328 	struct ucred	*pc_ucred;	/* Current credentials */
329 	uid_t		p_ruid;		/* Real user id */
330 	uid_t		p_svuid;	/* Saved effective user id */
331 	gid_t		p_rgid;		/* Real group id */
332 	gid_t		p_svgid;	/* Saved effective group id */
333 	int		p_refcnt;	/* Number of references */
334 };
335 
336 LIST_HEAD(proclist, proc);		/* A list of processes */
337 
338 /*
339  * This structure associates a proclist with its lock.
340  */
341 struct proclist_desc {
342 	struct proclist	*pd_list;	/* The list */
343 	/*
344 	 * XXX Add a pointer to the proclist's lock eventually.
345 	 */
346 };
347 
348 #ifdef _KERNEL
349 /*
350  * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t,
351  * as it is used to represent "no process group".
352  */
353 #define	PID_MAX		30000
354 #define	NO_PID		30001
355 
356 #define	SESS_LEADER(p)	((p)->p_session->s_leader == (p))
357 #define	SESSHOLD(s)	((s)->s_count++)
358 #define	SESSRELE(s)							\
359 do {									\
360 	if (--(s)->s_count == 0)					\
361 		FREE(s, M_SESSION);					\
362 } while (/* CONSTCOND */ 0)
363 
364 #define	PHOLD(p)							\
365 do {									\
366 	if ((p)->p_holdcnt++ == 0 && ((p)->p_flag & P_INMEM) == 0)	\
367 		uvm_swapin(p);						\
368 } while (/* CONSTCOND */ 0)
369 #define	PRELE(p)	(--(p)->p_holdcnt)
370 
371 /*
372  * Flags passed to fork1().
373  */
374 #define	FORK_PPWAIT	0x01		/* Block parent until child exit */
375 #define	FORK_SHAREVM	0x02		/* Share vmspace with parent */
376 #define	FORK_SHARECWD	0x04		/* Share cdir/rdir/cmask */
377 #define	FORK_SHAREFILES	0x08		/* Share file descriptors */
378 #define	FORK_SHARESIGS	0x10		/* Share signal actions */
379 #define	FORK_NOWAIT	0x20		/* Make init the parent of the child */
380 #define	FORK_CLEANFILES	0x40		/* Start with a clean descriptor set */
381 
382 #define	PIDHASH(pid)	(&pidhashtbl[(pid) & pidhash])
383 extern LIST_HEAD(pidhashhead, proc) *pidhashtbl;
384 extern u_long		pidhash;
385 
386 #define	PGRPHASH(pgid)	(&pgrphashtbl[(pgid) & pgrphash])
387 extern LIST_HEAD(pgrphashhead, pgrp) *pgrphashtbl;
388 extern u_long		pgrphash;
389 
390 /*
391  * Allow machine-dependent code to override curproc in <machine/cpu.h> for
392  * its own convenience.  Otherwise, we declare it as appropriate.
393  */
394 #if !defined(curproc)
395 #if defined(MULTIPROCESSOR)
396 #define	curproc		curcpu()->ci_curproc	/* Current running proc */
397 #else
398 extern struct proc	*curproc;		/* Current running proc */
399 #endif /* MULTIPROCESSOR */
400 #endif /* ! curproc */
401 
402 extern struct proc	proc0;		/* Process slot for swapper */
403 extern int		nprocs, maxproc; /* Current and max number of procs */
404 
405 /* Process list lock; see kern_proc.c for locking protocol details */
406 extern struct lock	proclist_lock;
407 
408 extern struct proclist	allproc;	/* List of all processes */
409 extern struct proclist	zombproc;	/* List of zombie processes */
410 
411 extern struct proclist deadproc;	/* List of dead processes */
412 extern struct simplelock deadproc_slock;
413 
414 extern struct proc	*initproc;	/* Process slots for init, pager */
415 
416 extern const struct proclist_desc proclists[];
417 
418 extern struct pool	proc_pool;	/* Memory pool for procs */
419 extern struct pool	pcred_pool;	/* Memory pool for pcreds */
420 extern struct pool	plimit_pool;	/* Memory pool for plimits */
421 extern struct pool	rusage_pool;	/* Memory pool for rusages */
422 
423 struct proc *pfind(pid_t);		/* Find process by id */
424 struct pgrp *pgfind(pid_t);		/* Find process group by id */
425 
426 struct simplelock;
427 
428 int	chgproccnt(uid_t uid, int diff);
429 int	enterpgrp(struct proc *p, pid_t pgid, int mksess);
430 void	fixjobc(struct proc *p, struct pgrp *pgrp, int entering);
431 int	inferior(struct proc *p, struct proc *q);
432 int	leavepgrp(struct proc *p);
433 void	yield(void);
434 void	preempt(struct proc *);
435 void	mi_switch(struct proc *, struct proc *);
436 struct proc *chooseproc(void);
437 void	pgdelete(struct pgrp *pgrp);
438 void	procinit(void);
439 #ifndef remrunqueue
440 void	remrunqueue(struct proc *);
441 #endif
442 #ifndef setrunqueue
443 void	setrunqueue(struct proc *);
444 #endif
445 #ifndef nextrunqueue
446 struct proc *nextrunqueue(void);
447 #endif
448 void	resetpriority(struct proc *);
449 void	setrunnable(struct proc *);
450 void	suspendsched(void);
451 int	ltsleep(void *chan, int pri, const char *wmesg, int timo,
452 	    __volatile struct simplelock *);
453 void	unsleep(struct proc *);
454 void	wakeup(void *chan);
455 void	wakeup_one(void *chan);
456 void	reaper(void *);
457 void	exit1(struct proc *, int);
458 void	exit2(struct proc *);
459 int	fork1(struct proc *, int, int, void *, size_t,
460 	    void (*)(void *), void *, register_t *, struct proc **);
461 void	rqinit(void);
462 int	groupmember(gid_t, struct ucred *);
463 #ifndef cpu_switch
464 void	cpu_switch(struct proc *, struct proc *);
465 #endif
466 #ifndef cpu_idle
467 void	cpu_idle(void);
468 #endif
469 void	cpu_exit(struct proc *);
470 void	cpu_fork(struct proc *, struct proc *, void *, size_t,
471 	    void (*)(void *), void *);
472 
473 		/*
474 		 * XXX: use __P() to allow ports to have as a #define.
475 		 * XXX: we need a better way to solve this.
476 		 */
477 void	cpu_wait __P((struct proc *));
478 
479 void	child_return(void *);
480 
481 int	proc_isunder(struct proc *, struct proc*);
482 
483 void	proclist_lock_read(void);
484 void	proclist_unlock_read(void);
485 int	proclist_lock_write(void);
486 void	proclist_unlock_write(int);
487 void	p_sugid(struct proc*);
488 
489 /* Compatibility with old, non-interlocked tsleep call */
490 #define	tsleep(chan, pri, wmesg, timo)					\
491 	ltsleep(chan, pri, wmesg, timo, NULL)
492 
493 #if defined(MULTIPROCESSOR)
494 void	proc_trampoline_mp(void);	/* XXX */
495 #endif
496 
497 #ifdef KSTACK_CHECK_MAGIC
498 void kstack_setup_magic(const struct proc *);
499 void kstack_check_magic(const struct proc *);
500 #endif
501 
502 /*
503  * kernel stack paramaters
504  * XXX require sizeof(struct user)
505  */
506 /* the lowest address of kernel stack */
507 #ifndef KSTACK_LOWEST_ADDR
508 #define	KSTACK_LOWEST_ADDR(p)	((caddr_t)ALIGN((p)->p_addr + 1))
509 #endif
510 /* size of kernel stack */
511 #ifndef KSTACK_SIZE
512 #define	KSTACK_SIZE	(USPACE - ALIGN(sizeof(struct user)))
513 #endif
514 
515 #endif	/* _KERNEL */
516 #endif	/* !_SYS_PROC_H_ */
517