2 * Copyright (c) 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)proc.h 8.15 (Berkeley) 5/19/95
45 #include <sys/callout.h> /* For struct callout. */
46 #include <sys/event.h> /* For struct klist. */
47 #include <sys/filedesc.h>
48 #include <sys/queue.h>
49 #include <sys/priority.h>
50 #include <sys/rtprio.h> /* XXX */
52 #include <sys/signal.h>
54 #include <sys/time.h> /* For structs itimerval, timeval. */
56 #include <sys/ucred.h>
57 #include <machine/proc.h> /* Machine-dependent proc substruct. */
60 * One structure allocated per session.
63 int s_count; /* Ref cnt; pgrps in session. */
64 struct proc *s_leader; /* Session leader. */
65 struct vnode *s_ttyvp; /* Vnode of controlling terminal. */
66 struct tty *s_ttyp; /* Controlling terminal. */
67 pid_t s_sid; /* Session ID. */
68 /* Setlogin() name: */
69 char s_login[roundup(MAXLOGNAME, sizeof(long))];
73 * One structure allocated per process group.
76 LIST_ENTRY(pgrp) pg_hash; /* Hash chain. */
77 LIST_HEAD(, proc) pg_members; /* Pointer to pgrp members. */
78 struct session *pg_session; /* Pointer to session. */
79 struct sigiolst pg_sigiolst; /* List of sigio sources. */
80 pid_t pg_id; /* Pgrp id. */
81 int pg_jobc; /* # procs qualifying pgrp for job control */
85 sigset_t ps_sigignore; /* Signals being ignored. */
86 sigset_t ps_sigcatch; /* Signals being caught by user. */
88 struct sigacts *ps_sigacts; /* Signal actions, state. */
92 #define PS_NOCLDWAIT 0x0001 /* No zombies if child dies */
93 #define PS_NOCLDSTOP 0x0002 /* No SIGCHLD when children stop. */
96 * pasleep structure, used by asleep() syscall to hold requested priority
97 * and timeout values for await().
100 int as_priority; /* Async priority. */
101 int as_timo; /* Async timeout. */
105 * pargs, used to hold a copy of the command line, if it had a sane length.
108 u_int ar_ref; /* Reference count. */
109 u_int ar_length; /* Length. */
110 u_char ar_args[0]; /* Arguments. */
114 * Description of a process.
116 * This structure contains the information needed to manage a thread of
117 * control, known in UN*X as a process; it has references to substructures
118 * containing descriptions of things that the process uses, but may share
119 * with related processes. The process structure and the substructures
120 * are always addressable except for those marked "(CPU)" below,
121 * which might be addressable only on a processor on which the process
124 * Below is a key of locks used to protect each member of struct proc. The
125 * lock is indicated by a reference to a specific character in parens in the
126 * associated comment.
127 * * - not yet protected
128 * a - only touched by curproc or parent during fork/wait
129 * b - created at fork, never changes
130 * (exception aiods switch vmspaces, but they are also
131 * marked 'P_SYSTEM' so hopefully it will be left alone)
132 * c - locked by proc mtx
133 * d - locked by allproc_lock lock
134 * e - locked by proctree_lock lock
136 * g - process group mtx
137 * h - callout_lock mtx
138 * i - by curproc or the master session mtx
139 * j - locked by sched_lock mtx
140 * k - either by curproc or a lock which prevents the lock from
141 * going away, such as (d,e)
142 * l - the attaching proc or attaching proc parent
144 * n - not locked, lazy
146 * If the locking key specifies two identifiers (for example, p_pptr) then
147 * either lock is sufficient for read access, but both locks must be held
155 TAILQ_ENTRY(proc) p_procq; /* (j) Run/mutex queue. */
156 TAILQ_ENTRY(proc) p_slpq; /* (j) Sleep queue. */
157 LIST_ENTRY(proc) p_list; /* (d) List of all processes. */
160 struct ucred *p_ucred; /* (c + k) Process owner's identity. */
161 struct filedesc *p_fd; /* (b) Ptr to open files structure. */
162 struct pstats *p_stats; /* (b) Accounting/statistics (CPU). */
163 struct plimit *p_limit; /* (m) Process limits. */
164 struct vm_object *p_upages_obj;/* (a) Upages object. */
165 struct procsig *p_procsig; /* (c) Signal actions, state (CPU). */
166 #define p_sigacts p_procsig->ps_sigacts
167 #define p_sigignore p_procsig->ps_sigignore
168 #define p_sigcatch p_procsig->ps_sigcatch
170 #define p_rlimit p_limit->pl_rlimit
172 int p_flag; /* (c) P_* flags. */
173 int p_sflag; /* (j) PS_* flags. */
174 int p_stat; /* (j) S* process status. */
176 pid_t p_pid; /* (b) Process identifier. */
177 LIST_ENTRY(proc) p_hash; /* (d) Hash chain. */
178 LIST_ENTRY(proc) p_pglist; /* (c) List of processes in pgrp. */
179 struct proc *p_pptr; /* (c + e) Pointer to parent process. */
180 LIST_ENTRY(proc) p_sibling; /* (e) List of sibling processes. */
181 LIST_HEAD(, proc) p_children; /* (e) Pointer to list of children. */
183 /* The following fields are all zeroed upon creation in fork. */
184 #define p_startzero p_oppid
186 pid_t p_oppid; /* (c + e) Save parent pid during ptrace. XXX */
187 int p_dupfd; /* (c) Sideways ret value from fdopen. XXX */
188 struct vmspace *p_vmspace; /* (b) Address space. */
191 u_int p_estcpu; /* (j) Time averaged value of p_cpticks. */
192 int p_cpticks; /* (j) Ticks of cpu time. */
193 fixpt_t p_pctcpu; /* (j) %cpu during p_swtime. */
194 struct callout p_slpcallout; /* (h) Callout for sleep. */
195 void *p_wchan; /* (j) Sleep address. */
196 const char *p_wmesg; /* (j) Reason for sleep. */
197 u_int p_swtime; /* (j) Time swapped in or out. */
198 u_int p_slptime; /* (j) Time since last blocked. */
200 struct callout p_itcallout; /* (h) Interval timer callout. */
201 struct itimerval p_realtimer; /* (h?/k?) Alarm timer. */
202 u_int64_t p_runtime; /* (j) Real time in microsec. */
203 u_int64_t p_uu; /* (j) Previous user time in microsec. */
204 u_int64_t p_su; /* (j) Previous system time in microsec. */
205 u_int64_t p_iu; /* (j) Previous interrupt time in microsec. */
206 u_int64_t p_uticks; /* (j) Statclock hits in user mode. */
207 u_int64_t p_sticks; /* (j) Statclock hits in system mode. */
208 u_int64_t p_iticks; /* (j) Statclock hits processing intr. */
210 int p_traceflag; /* (j?) Kernel trace points. */
211 struct vnode *p_tracep; /* (j?) Trace to vnode. */
213 sigset_t p_siglist; /* (c) Signals arrived but not delivered. */
215 struct vnode *p_textvp; /* (b) Vnode of executable. */
217 struct mtx p_mtx; /* (k) Lock for this struct. */
218 u_int p_spinlocks; /* (k) Count of held spin locks. */
219 char p_lock; /* (c) Process lock (prevent swap) count. */
220 u_char p_oncpu; /* (j) Which cpu we are on. */
221 u_char p_lastcpu; /* (j) Last cpu we were on. */
222 char p_rqindex; /* (j) Run queue index. */
224 short p_locks; /* (*) DEBUG: lockmgr count of held locks */
225 u_int p_stops; /* (c) Procfs event bitmask. */
226 u_int p_stype; /* (c) Procfs stop event type. */
227 char p_step; /* (c) Procfs stop *once* flag. */
228 u_char p_pfsflags; /* (c) Procfs flags. */
229 char p_pad3[2]; /* Alignment. */
230 register_t p_retval[2]; /* (k) Syscall aux returns. */
231 struct sigiolst p_sigiolst; /* (c) List of sigio sources. */
232 int p_sigparent; /* (c) Signal to parent on exit. */
233 sigset_t p_oldsigmask; /* (c) Saved mask from before sigpause. */
234 int p_sig; /* (n) For core dump/debugger XXX. */
235 u_long p_code; /* (n) For core dump/debugger XXX. */
236 struct klist p_klist; /* (c) Knotes attached to this process. */
237 struct lock_list_entry *p_sleeplocks; /* (k) Held sleep locks. */
238 struct mtx *p_blocked; /* (j) Mutex process is blocked on. */
239 const char *p_mtxname; /* (j) Name of mutex blocked on. */
240 LIST_HEAD(, mtx) p_contested; /* (j) Contested locks. */
242 struct nlminfo *p_nlminfo; /* (?) only used by/for lockd */
243 void *p_aioinfo; /* (c) ASYNC I/O info. */
244 struct ithd *p_ithd; /* (b) For interrupt threads only. */
245 int p_intr_nesting_level; /* (k) Interrupt recursion. */
247 /* End area that is zeroed on creation. */
248 #define p_endzero p_startcopy
250 /* The following fields are all copied upon creation in fork. */
251 #define p_startcopy p_sigmask
253 sigset_t p_sigmask; /* (c) Current signal mask. */
254 stack_t p_sigstk; /* (c) Stack pointer and on-stack flag. */
256 int p_magic; /* (b) Magic number. */
257 struct priority p_pri; /* (j) Process priority. */
258 char p_nice; /* (j?/k?) Process "nice" value. */
259 char p_comm[MAXCOMLEN + 1]; /* (b) Process name. */
261 struct pgrp *p_pgrp; /* (e?/c?) Pointer to process group. */
262 struct sysentvec *p_sysent; /* (b) System call dispatch information. */
263 struct pargs *p_args; /* (c + k) Process arguments. */
265 /* End area that is copied on creation. */
266 #define p_endcopy p_addr
268 struct user *p_addr; /* (k) Kernel virtual addr of u-area (CPU). */
269 struct mdproc p_md; /* (k) Any machine-dependent fields. */
271 u_short p_xstat; /* (c) Exit status for wait; also stop sig. */
272 u_short p_acflag; /* (c) Accounting flags. */
273 struct rusage *p_ru; /* (a) Exit information. XXX */
275 struct proc *p_peers; /* (c) */
276 struct proc *p_leader; /* (c) */
277 struct pasleep p_asleep; /* (k) Used by asleep()/await(). */
278 void *p_emuldata; /* (c) Emulator state data. */
279 struct trapframe *p_frame; /* (k) */
282 #define p_session p_pgrp->pg_session
283 #define p_pgid p_pgrp->pg_id
285 #define NOCPU 0xff /* For p_oncpu when we aren't on a CPU. */
287 /* Status values (p_stat). */
288 #define SIDL 1 /* Process being created by fork. */
289 #define SRUN 2 /* Currently runnable. */
290 #define SSLEEP 3 /* Sleeping on an address. */
291 #define SSTOP 4 /* Process debugging or suspension. */
292 #define SZOMB 5 /* Awaiting collection by parent. */
293 #define SWAIT 6 /* Waiting for interrupt. */
294 #define SMTX 7 /* Blocked on a mutex. */
296 /* These flags are kept in p_flag. */
297 #define P_ADVLOCK 0x00001 /* Process may hold a POSIX advisory lock. */
298 #define P_CONTROLT 0x00002 /* Has a controlling terminal. */
299 #define P_KTHREAD 0x00004 /* Kernel thread. */
300 #define P_NOLOAD 0x00008 /* Ignore during load avg calculations. */
301 #define P_PPWAIT 0x00010 /* Parent is waiting for child to exec/exit. */
302 #define P_SELECT 0x00040 /* Selecting; wakeup/waiting danger. */
303 #define P_SUGID 0x00100 /* Had set id privileges since last exec. */
304 #define P_SYSTEM 0x00200 /* System proc: no sigs, stats or swapping. */
305 #define P_TRACED 0x00800 /* Debugged process being traced. */
306 #define P_WAITED 0x01000 /* Debugging process has waited for child. */
307 #define P_WEXIT 0x02000 /* Working on exiting. */
308 #define P_EXEC 0x04000 /* Process called exec. */
310 /* Should be moved to machine-dependent areas. */
312 #define P_BUFEXHAUST 0x100000 /* Dirty buffers flush is in progress. */
313 #define P_COWINPROGRESS 0x400000 /* Snapshot copy-on-write in progress. */
315 #define P_DEADLKTREAT 0x800000 /* Lock aquisition - deadlock treatment. */
317 #define P_JAILED 0x1000000 /* Process is in jail. */
318 #define P_OLDMASK 0x2000000 /* Need to restore mask after suspend. */
319 #define P_ALTSTACK 0x4000000 /* Have alternate signal stack. */
321 /* These flags are kept in p_sflag and are protected with sched_lock. */
322 #define PS_INMEM 0x00001 /* Loaded into memory. */
323 #define PS_OWEUPC 0x00002 /* Owe process an addupc() call at next ast. */
324 #define PS_PROFIL 0x00004 /* Has started profiling. */
325 #define PS_SINTR 0x00008 /* Sleep is interruptible. */
326 #define PS_TIMEOUT 0x00010 /* Timing out during sleep. */
327 #define PS_ALRMPEND 0x00020 /* Pending SIGVTALRM needs to be posted. */
328 #define PS_PROFPEND 0x00040 /* Pending SIGPROF needs to be posted. */
329 #define PS_CVWAITQ 0x00080 /* Proces is on a cv_waitq (not slpq). */
330 #define PS_SWAPINREQ 0x00100 /* Swapin request due to wakeup. */
331 #define PS_SWAPPING 0x00200 /* Process is being swapped. */
332 #define PS_ASTPENDING 0x00400 /* Process has a pending ast. */
333 #define PS_NEEDRESCHED 0x00800 /* Process needs to yield. */
335 #define P_MAGIC 0xbeefface
338 #define P_CAN_SCHED 3
339 #define P_CAN_DEBUG 4
343 #ifdef MALLOC_DECLARE
344 MALLOC_DECLARE(M_PARGS);
345 MALLOC_DECLARE(M_SESSION);
346 MALLOC_DECLARE(M_SUBPROC);
347 MALLOC_DECLARE(M_ZOMBIE);
351 sigonstack(size_t sp)
353 register struct proc *p = curproc;
355 return ((p->p_flag & P_ALTSTACK) ?
356 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
357 ((p->p_sigstk.ss_size == 0) ? (p->p_sigstk.ss_flags & SS_ONSTACK) :
358 ((sp - (size_t)p->p_sigstk.ss_sp) < p->p_sigstk.ss_size))
360 ((sp - (size_t)p->p_sigstk.ss_sp) < p->p_sigstk.ss_size)
366 * Preempt the current process if in interrupt from user mode,
367 * or after the current trap/syscall if in system mode.
369 #define need_resched(p) do { \
370 mtx_assert(&sched_lock, MA_OWNED); \
371 (p)->p_sflag |= PS_NEEDRESCHED; \
374 #define resched_wanted(p) ((p)->p_sflag & PS_NEEDRESCHED)
376 #define clear_resched(p) do { \
377 mtx_assert(&sched_lock, MA_OWNED); \
378 (p)->p_sflag &= ~PS_NEEDRESCHED; \
382 * Schedule an Asynchronous System Trap (AST) on return to user mode.
384 #define aston(p) do { \
385 mtx_assert(&sched_lock, MA_OWNED); \
386 (p)->p_sflag |= PS_ASTPENDING; \
389 #define astpending(p) ((p)->p_sflag & PS_ASTPENDING)
391 #define astoff(p) do { \
392 mtx_assert(&sched_lock, MA_OWNED); \
393 (p)->p_sflag &= ~PS_ASTPENDING; \
397 * Notify the current process (p) that it has a signal pending,
398 * process as soon as possible.
400 #define signotify(p) aston(p)
402 /* Handy macro to determine if p1 can mangle p2. */
403 #define PRISON_CHECK(p1, p2) \
404 ((p1)->p_prison == NULL || (p1)->p_prison == (p2)->p_prison)
407 * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t,
408 * as it is used to represent "no process group".
410 #define PID_MAX 99999
411 #define NO_PID 100000
413 #define SESS_LEADER(p) ((p)->p_session->s_leader == (p))
414 #define SESSHOLD(s) ((s)->s_count++)
415 #define SESSRELE(s) { \
416 if (--(s)->s_count == 0) \
417 FREE(s, M_SESSION); \
420 #define STOPEVENT(p, e, v) do { \
422 _STOPEVENT((p), (e), (v)); \
425 #define _STOPEVENT(p, e, v) do { \
426 PROC_LOCK_ASSERT(p, MA_OWNED); \
427 if ((p)->p_stops & (e)) { \
428 stopevent((p), (e), (v)); \
432 /* Lock and unlock a process. */
433 #define PROC_LOCK(p) mtx_lock(&(p)->p_mtx)
434 #define PROC_TRYLOCK(p) mtx_trylock(&(p)->p_mtx)
435 #define PROC_UNLOCK(p) mtx_unlock(&(p)->p_mtx)
436 #define PROC_UNLOCK_NOSWITCH(p) \
437 mtx_unlock_flags(&(p)->p_mtx, MTX_NOSWITCH)
438 #define PROC_LOCKED(p) mtx_owned(&(p)->p_mtx)
439 #define PROC_LOCK_ASSERT(p, type) mtx_assert(&(p)->p_mtx, (type))
441 /* Hold process U-area in memory, normally for ptrace/procfs work. */
442 #define PHOLD(p) do { \
447 #define _PHOLD(p) do { \
448 PROC_LOCK_ASSERT((p), MA_OWNED); \
449 if ((p)->p_lock++ == 0) \
453 #define PRELE(p) do { \
458 #define _PRELE(p) do { \
459 PROC_LOCK_ASSERT((p), MA_OWNED); \
463 #define PIDHASH(pid) (&pidhashtbl[(pid) & pidhash])
464 extern LIST_HEAD(pidhashhead, proc) *pidhashtbl;
465 extern u_long pidhash;
467 #define PGRPHASH(pgid) (&pgrphashtbl[(pgid) & pgrphash])
468 extern LIST_HEAD(pgrphashhead, pgrp) *pgrphashtbl;
469 extern u_long pgrphash;
471 extern struct sx allproc_lock;
472 extern struct sx proctree_lock;
473 extern struct proc proc0; /* Process slot for swapper. */
474 extern int hogticks; /* Limit on kernel cpu hogs. */
475 extern int nprocs, maxproc; /* Current and max number of procs. */
476 extern int maxprocperuid; /* Max procs per uid. */
477 extern u_long ps_arg_cache_limit;
478 extern int ps_argsopen;
479 extern int ps_showallprocs;
480 extern int sched_quantum; /* Scheduling quantum in ticks. */
482 LIST_HEAD(proclist, proc);
483 TAILQ_HEAD(procqueue, proc);
484 extern struct proclist allproc; /* List of all processes. */
485 extern struct proclist zombproc; /* List of zombie processes. */
486 extern struct proc *initproc, *pageproc; /* Process slots for init, pager. */
487 extern struct proc *updateproc; /* Process slot for syncer (sic). */
489 extern struct vm_zone *proc_zone;
494 * XXX macros for scheduler. Shouldn't be here, but currently needed for
495 * bounding the dubious p_estcpu inheritance in wait1().
496 * INVERSE_ESTCPU_WEIGHT is only suitable for statclock() frequencies in
497 * the range 100-256 Hz (approximately).
499 #define ESTCPULIM(e) \
500 min((e), INVERSE_ESTCPU_WEIGHT * (NICE_WEIGHT * (PRIO_MAX - PRIO_MIN) - \
501 RQ_PPQ) + INVERSE_ESTCPU_WEIGHT - 1)
502 #define INVERSE_ESTCPU_WEIGHT 8 /* 1 / (priorities per estcpu level). */
503 #define NICE_WEIGHT 1 /* Priorities per nice level. */
505 struct proc *pfind __P((pid_t)); /* Find process by id. */
506 struct pgrp *pgfind __P((pid_t)); /* Find process group by id. */
507 struct proc *zpfind __P((pid_t)); /* Find zombie process by id. */
509 struct proc *chooseproc __P((void));
510 int enterpgrp __P((struct proc *p, pid_t pgid, int mksess));
511 void faultin __P((struct proc *p));
512 void fixjobc __P((struct proc *p, struct pgrp *pgrp, int entering));
513 int fork1 __P((struct proc *, int, struct proc **));
514 void fork_exit __P((void (*)(void *, struct trapframe *), void *,
515 struct trapframe *));
516 void fork_return __P((struct proc *, struct trapframe *));
517 int inferior __P((struct proc *p));
518 int leavepgrp __P((struct proc *p));
519 void mi_switch __P((void));
520 int p_can __P((struct proc *p1, struct proc *p2, int operation,
522 int p_cansignal __P((struct proc *p1, struct proc *p2, int signum));
523 int p_trespass __P((struct proc *p1, struct proc *p2));
524 void procinit __P((void));
525 void proc_reparent __P((struct proc *child, struct proc *newparent));
526 int procrunnable __P((void));
527 void remrunqueue __P((struct proc *));
528 void resetpriority __P((struct proc *));
529 int roundrobin_interval __P((void));
530 void schedclock __P((struct proc *));
531 void setrunnable __P((struct proc *));
532 void setrunqueue __P((struct proc *));
533 void setsugid __P((struct proc *p));
534 void sleepinit __P((void));
535 void stopevent __P((struct proc *, u_int, u_int));
536 void cpu_idle __P((void));
537 void cpu_switch __P((void));
538 void cpu_throw __P((void)) __dead2;
539 void unsleep __P((struct proc *));
540 void updatepri __P((struct proc *));
541 void userret __P((struct proc *, struct trapframe *, u_quad_t));
542 void maybe_resched __P((struct proc *));
544 void cpu_exit __P((struct proc *)) __dead2;
545 void exit1 __P((struct proc *, int)) __dead2;
546 void cpu_fork __P((struct proc *, struct proc *, int));
547 void cpu_set_fork_handler __P((struct proc *, void (*)(void *), void *));
548 int trace_req __P((struct proc *));
549 void cpu_wait __P((struct proc *));
550 int cpu_coredump __P((struct proc *, struct vnode *, struct ucred *));
553 #endif /* !_SYS_PROC_H_ */