]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/proc.h
Move the stop event macros from pioctl.h to proc.h, and add an S_ALLSTOPS
[FreeBSD/FreeBSD.git] / sys / sys / proc.h
1 /*-
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.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
25  *
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
36  * SUCH DAMAGE.
37  *
38  *      @(#)proc.h      8.15 (Berkeley) 5/19/95
39  * $FreeBSD$
40  */
41
42 #ifndef _SYS_PROC_H_
43 #define _SYS_PROC_H_
44
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 */
51 #include <sys/runq.h>
52 #include <sys/signal.h>
53 #ifndef _KERNEL
54 #include <sys/time.h>                   /* For structs itimerval, timeval. */
55 #endif
56 #include <sys/ucred.h>
57 #include <machine/proc.h>               /* Machine-dependent proc substruct. */
58
59 /*
60  * One structure allocated per session.
61  */
62 struct 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))];
70 };
71
72 /*
73  * One structure allocated per process group.
74  */
75 struct pgrp {
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 */
82 };
83
84 struct procsig {
85         sigset_t ps_sigignore;  /* Signals being ignored. */
86         sigset_t ps_sigcatch;   /* Signals being caught by user. */
87         int      ps_flag;
88         struct   sigacts *ps_sigacts;   /* Signal actions, state. */
89         int      ps_refcnt;
90 };
91
92 #define PS_NOCLDWAIT    0x0001  /* No zombies if child dies */
93 #define PS_NOCLDSTOP    0x0002  /* No SIGCHLD when children stop. */
94
95 /*
96  * pargs, used to hold a copy of the command line, if it had a sane length.
97  */
98 struct pargs {
99         u_int   ar_ref;         /* Reference count. */
100         u_int   ar_length;      /* Length. */
101         u_char  ar_args[0];     /* Arguments. */
102 };
103
104 /*-
105  * Description of a process.
106  *
107  * This structure contains the information needed to manage a thread of
108  * control, known in UN*X as a process; it has references to substructures
109  * containing descriptions of things that the process uses, but may share
110  * with related processes.  The process structure and the substructures
111  * are always addressable except for those marked "(CPU)" below,
112  * which might be addressable only on a processor on which the process
113  * is running.
114  *
115  * Below is a key of locks used to protect each member of struct proc.  The
116  * lock is indicated by a reference to a specific character in parens in the
117  * associated comment.
118  *      * - not yet protected
119  *      a - only touched by curproc or parent during fork/wait
120  *      b - created at fork, never changes
121  *              (exception aiods switch vmspaces, but they are also
122  *              marked 'P_SYSTEM' so hopefully it will be left alone)
123  *      c - locked by proc mtx
124  *      d - locked by allproc_lock lock
125  *      e - locked by proctree_lock lock
126  *      f - session mtx
127  *      g - process group mtx
128  *      h - callout_lock mtx
129  *      i - by curproc or the master session mtx
130  *      j - locked by sched_lock mtx
131  *      k - only accessed by curthread
132  *      l - the attaching proc or attaching proc parent
133  *      m - Giant
134  *      n - not locked, lazy
135  *
136  * If the locking key specifies two identifiers (for example, p_pptr) then
137  * either lock is sufficient for read access, but both locks must be held
138  * for write access.
139  */
140 struct ithd;
141 struct nlminfo;
142 struct trapframe;
143
144 /*
145  * Here we define the four structures used for process information.
146  *
147  * The first is the thread. It might be though of as a "Kernel
148  * Schedulable Entity Context".
149  * This structure contains all the information as to where a thread of 
150  * execution is now, or was when it was suspended, why it was suspended,
151  * and anything else that will be needed to restart it when it is
152  * rescheduled. Always associated with a KSE when running, but can be
153  * reassigned to an equivalent KSE  when being restarted for
154  * load balancing. Each of these is associated with a kernel stack
155  * and a pcb.
156  * 
157  * It is important to remember that a particular thread structure only
158  * exists as long as the system call or kernel entrance (e.g. by pagefault)
159  * which it is currently executing. It should threfore NEVER be referenced
160  * by pointers in long lived structures that live longer than a single
161  * request. If several threads complete their work at the same time,
162  * they will all rewind their stacks to the uer boundary, report their
163  * completion state, and all but one will be freed. That last one will
164  * be kept to provide a kernel stack and pcb for the NEXT syscall or kernel
165  * entrance. (basically to save freeing and then re-allocating it) A process
166  * might keep a cache of threads available to allow it to quickly
167  * get one when it needs a new one. There would probably also be a system
168  * cache of free threads.
169  */
170 struct thread;
171
172 /* 
173  * The second structure is the Kernel Schedulable Entity. (KSE)
174  * As long as this is scheduled, it will continue to run any threads that
175  * are assigned to it or the KSEGRP (see later) until either it runs out
176  * of runnable threads or CPU.
177  * It runs on one CPU and is assigned a quantum of time. When a thread is
178  * blocked, The KSE continues to run and will search for another thread
179  * in a runnable state amongst those it has. It May decide to return to user
180  * mode with a new 'empty' thread if there are no runnable threads.
181  * threads are associated with a KSE for cache reasons, but a sheduled KSE with
182  * no runnable thread will try take a thread from a sibling KSE before
183  * surrendering its quantum. In some schemes it gets it's quantum from the KSEG
184  * and contributes to draining that quantum, along withthe other KSEs in
185  * the group. (undecided)
186  */
187 struct kse;
188
189 /*
190  * The KSEGRP is allocated resources across a number of CPUs.
191  * (Including a number of CPUxQUANTA. It parcels these QUANTA up among
192  * Its KSEs, each of which should be running in a different CPU.
193  * Priority and total available sheduled quanta are properties of a KSEGRP.
194  * Multiple KSEGRPs in a single process compete against each other
195  * for total quanta in the same way that a forked child competes against
196  * it's parent process.
197  */
198 struct ksegrp;
199
200 /*
201  * A process is the owner of all system resources allocated to a task
202  * except CPU quanta.
203  * All KSEGs under one process see, and have the same access to, these
204  * resources (e.g. files, memory, sockets, permissions kqueues).
205  * A process may compete for CPU cycles on the same basis as a
206  * forked process cluster by spawning several KSEGRPs. 
207  */
208 struct proc;
209
210 /***************
211  * In pictures:
212  With a single run queue used by all processors:
213
214  RUNQ: --->KSE---KSE--...               SLEEPQ:[]---THREAD---THREAD---THREAD
215            |   /                               []---THREAD
216            KSEG---THREAD--THREAD--THREAD       []
217                                                []---THREAD---THREAD
218
219   (processors run THREADs from the KSEG until they are exhausted or
220   the KSEG exhausts its quantum) 
221
222 With PER-CPU run queues:
223 KSEs on the separate run queues directly
224 They would be given priorities calculated from the KSEG.
225
226  *
227  *****************/
228
229 /*
230  * Kernel runnable context (thread).
231  * This is what is put to sleep and reactivated.
232  * The first KSE available in the correct group will run this thread.
233  * If several are available, use the one on the same CPU as last time.
234  */
235 struct thread {
236         struct  proc *td_proc;          /* Associated process. */
237         struct  ksegrp *td_ksegrp;      /* Associated KSEG. */
238         struct  kse *td_last_kse;       /* Where it wants to be if possible. */
239         struct  kse *td_kse;            /* Current KSE if running. */
240         TAILQ_ENTRY(thread) td_plist;   /* All threads in this proc */
241         TAILQ_ENTRY(thread) td_kglist;  /* All threads in this ksegrp */
242
243         /* The two queues below should someday be merged */
244         TAILQ_ENTRY(thread) td_slpq;    /* (j) Sleep queue. XXXKSE */ 
245         TAILQ_ENTRY(thread) td_blkq;    /* (j) Mutex queue. XXXKSE */ 
246         TAILQ_ENTRY(thread) td_runq;    /* (j) Run queue(s). XXXKSE */ 
247
248 #define td_startzero td_flags
249         int     td_flags;               /* (j) TDF_* flags. */
250         int     td_dupfd;               /* (k) Ret value from fdopen. XXX */
251         void    *td_wchan;              /* (j) Sleep address. */
252         const   char *td_wmesg;         /* (j) Reason for sleep. */
253         u_char  td_lastcpu;             /* (j) Last cpu we were on. */
254         short   td_locks;               /* (k) DEBUG: lockmgr count of locks */
255         struct  mtx *td_blocked;        /* (j) Mutex process is blocked on. */
256         struct  ithd *td_ithd;          /* (b) For interrupt threads only. */
257         const   char *td_mtxname;       /* (j) Name of mutex blocked on. */
258         LIST_HEAD(, mtx) td_contested;  /* (j) Contested locks. */
259         struct  lock_list_entry *td_sleeplocks; /* (k) Held sleep locks. */
260         int     td_intr_nesting_level;  /* (k) Interrupt recursion. */
261 #define td_endzero td_md
262
263 #define td_startcopy td_endzero
264         /* XXXKSE p_md is in the "on your own" section in old struct proc */
265         struct  mdthread td_md;         /* (k) Any machine-dependent fields. */
266         register_t      td_retval[2];   /* (k) Syscall aux returns. */
267 #define td_endcopy td_pcb
268
269         struct  pcb *td_pcb;            /* (k) Kernel VA of pcb and kstack. */
270         struct  callout td_slpcallout;  /* (h) Callout for sleep. */
271         struct  trapframe *td_frame;    /* (k) */
272         struct  vm_object *td_kstack_obj;/* (a) Kstack object. */
273         vm_offset_t     td_kstack;      /* Kernel VA of kstack. */
274 };
275
276 /*
277  * The schedulable entity that can be given a context to run.
278  * A process may have several of these. Probably one per processor
279  * but posibly a few more. In this universe they are grouped
280  * with a KSEG that contains the priority and niceness
281  * for the group.
282  */
283 struct kse {
284         struct  proc *ke_proc;          /* Associated process. */
285         struct  ksegrp *ke_ksegrp;      /* Associated KSEG. */
286         struct  thread *ke_thread;      /* Associated thread, if running. */
287         TAILQ_ENTRY(kse) ke_kglist;     /* Queue of all KSEs in ke_ksegrp. */
288         TAILQ_ENTRY(kse) ke_kgrlist;    /* Queue of all KSEs in this state. */
289         TAILQ_ENTRY(kse) ke_procq;      /* (j) Run queue. */
290         TAILQ_HEAD(, thread) ke_runq;   /* (td_runq) RUNNABLE bound to KSE. */
291
292 #define ke_startzero ke_flags
293         int     ke_flags;               /* (j) KEF_* flags. */
294         /*u_int ke_estcpu; */           /* (j) Time averaged val of cpticks. */
295         int     ke_cpticks;             /* (j) Ticks of cpu time. */
296         fixpt_t ke_pctcpu;              /* (j) %cpu during p_swtime. */
297         u_int64_t       ke_uu;          /* (j) Previous user time in usec. */
298         u_int64_t       ke_su;          /* (j) Previous system time in usec. */
299         u_int64_t       ke_iu;          /* (j) Previous intr time in usec. */
300         u_int64_t       ke_uticks;      /* (j) Statclock hits in user mode. */
301         u_int64_t       ke_sticks;      /* (j) Statclock hits in system mode. */
302         u_int64_t       ke_iticks;      /* (j) Statclock hits in intr. */
303         u_char  ke_oncpu;               /* (j) Which cpu we are on. */
304         u_int   ke_slptime;             /* (j) Time since last idle. */
305         char    ke_rqindex;             /* (j) Run queue index. */
306 #define ke_endzero ke_priority
307
308 #define ke_startcopy ke_endzero
309         u_char  ke_priority;            /* (j) Process priority. */
310         u_char  ke_usrpri;              /* (j) User pri from cpu & nice. */
311 #define ke_endcopy ke_end
312
313         int     ke_end;         /* dummy entry */
314 };
315
316 /*
317  * Kernel-scheduled entity group (KSEG).  The scheduler considers each KSEG to
318  * be an indivisible unit from a time-sharing perspective, though each KSEG may
319  * contain multiple KSEs.
320  */
321 struct ksegrp {
322         struct  proc *kg_proc;          /* Process that contains this KSEG. */
323         TAILQ_ENTRY(ksegrp) kg_ksegrp;  /* Queue of KSEGs in kg_proc. */
324         TAILQ_HEAD(, kse) kg_kseq;      /* (ke_kglist) All KSEs. */
325         TAILQ_HEAD(, kse) kg_rq;        /* (ke_kgrlist) Runnable KSEs. */
326         TAILQ_HEAD(, kse) kg_iq;        /* (ke_kgrlist) Idle KSEs. */
327         TAILQ_HEAD(, thread) kg_threads;/* (td_kglist) All threads. */
328         TAILQ_HEAD(, thread) kg_runq;   /* (td_runq) Unbound RUNNABLE threads */
329         TAILQ_HEAD(, thread) kg_slpq;   /* (td_runq) NONRUNNABLE threads. */
330
331 #define kg_startzero kg_estcpu
332         u_int   kg_slptime;             /* (j) How long completely blocked. */
333         u_int   kg_estcpu;              /* Sum of the same field in KSEs. */
334 #define kg_endzero kg_pri
335
336 #define kg_startcopy kg_endzero
337         struct  priority kg_pri;        /* (j) Process priority. */
338         char    kg_nice;                /* (j?/k?) Process "nice" value. */
339         struct  rtprio kg_rtprio;       /* (j) Realtime priority. */
340 #define kg_endcopy kg_runnable
341
342         int     kg_runnable;            /* Num runnable threads on queue. */
343         int     kg_runq_kses;           /* Num KSEs on runq. */
344         int     kg_kses;                /* Num KSEs in group. */
345 };
346
347 /*
348  * The old fashionned process. May have multiple threads, KSEGRPs
349  * and KSEs. Starts off with a single embedded KSEGRP, KSE and THREAD.
350  */
351 struct proc {
352         LIST_ENTRY(proc) p_list;        /* (d) List of all processes. */
353         TAILQ_HEAD(, ksegrp) p_ksegrps; /* (kg_ksegrp) All KSEGs. */
354         TAILQ_HEAD(, thread) p_threads; /* (td_plist) Threads. (shortcut) */
355         struct  ucred *p_ucred;         /* (c) Process owner's identity. */
356         struct  filedesc *p_fd;         /* (b) Ptr to open files structure. */
357                                         /* Accumulated stats for all KSEs? */
358         struct  pstats *p_stats;        /* (b) Accounting/statistics (CPU). */
359         struct  plimit *p_limit;        /* (m) Process limits. */
360         struct  vm_object *p_upages_obj; /* (a) Upages object. */
361         struct  procsig *p_procsig;     /* (c) Signal actions, state (CPU). */
362
363         struct  ksegrp p_ksegrp;
364         struct  kse p_kse;
365         struct  thread p_thread;
366
367         /*
368          * The following don't make too much sense..
369          * See the td_ or ke_ versions of the same flags
370          */
371         int     p_flag;                 /* (c) P_* flags. */
372         int     p_sflag;                /* (j) PS_* flags. */
373         int     p_stat;                 /* (j) S* process status. */
374
375         pid_t   p_pid;                  /* (b) Process identifier. */
376         LIST_ENTRY(proc) p_hash;        /* (d) Hash chain. */
377         LIST_ENTRY(proc) p_pglist;      /* (c) List of processes in pgrp. */
378         struct   proc *p_pptr;          /* (c + e) Pointer to parent process. */
379         LIST_ENTRY(proc) p_sibling;     /* (e) List of sibling processes. */
380         LIST_HEAD(, proc) p_children;   /* (e) Pointer to list of children. */
381
382 /* The following fields are all zeroed upon creation in fork. */
383 #define p_startzero     p_oppid
384         pid_t   p_oppid;                /* (c + e) Save ppid in ptrace. XXX */
385         struct  vmspace *p_vmspace;     /* (b) Address space. */
386         u_int   p_swtime;               /* (j) Time swapped in or out. */
387         struct  itimerval p_realtimer;  /* (h?/k?) Alarm timer. */
388         u_int64_t       p_runtime;      /* (j) Real time in microsec. */
389         int     p_traceflag;            /* (j?) Kernel trace points. */
390         struct  vnode *p_tracep;        /* (j?) Trace to vnode. */
391         sigset_t        p_siglist;      /* (c) Sigs arrived, not delivered. */
392         struct  vnode *p_textvp;        /* (b) Vnode of executable. */
393         struct  mtx p_mtx;              /* (k) Lock for this struct. */
394         char    p_lock;                 /* (c) Proclock (prevent swap) count. */
395         struct  klist p_klist;          /* (c) Knotes attached to this proc. */
396         struct  sigiolst p_sigiolst;    /* (c) List of sigio sources. */
397         int     p_sigparent;            /* (c) Signal to parent on exit. */
398         sigset_t        p_oldsigmask;   /* (c) Saved mask from pre sigpause. */
399         int     p_sig;                  /* (n) For core dump/debugger XXX. */
400         u_long  p_code;                 /* (n) For core dump/debugger XXX. */
401         u_int   p_stops;                /* (c) Stop event bitmask. */
402         u_int   p_stype;                /* (c) Stop event type. */
403         char    p_step;                 /* (c) Process is stopped. */
404         u_char  p_pfsflags;             /* (c) Procfs flags. */
405         struct  nlminfo *p_nlminfo;     /* (?) Only used by/for lockd. */
406         void    *p_aioinfo;             /* (c) ASYNC I/O info. */
407 /* End area that is zeroed on creation. */
408 #define p_startcopy     p_sigmask
409
410 /* The following fields are all copied upon creation in fork. */
411 #define p_endzero       p_startcopy
412         sigset_t        p_sigmask;      /* (c) Current signal mask. */
413         stack_t p_sigstk;               /* (c) Stack ptr and on-stack flag. */
414         int     p_magic;                /* (b) Magic number. */
415         char    p_comm[MAXCOMLEN + 1];  /* (b) Process name. */
416         struct  pgrp *p_pgrp;           /* (e?/c?) Pointer to process group. */
417         struct  sysentvec *p_sysent;    /* (b) Syscall dispatch info. */
418         struct  pargs *p_args;          /* (c) Process arguments. */
419 /* End area that is copied on creation. */
420 #define p_endcopy       p_xstat
421
422         u_short p_xstat;                /* (c) Exit status; also stop sig. */
423         struct  mdproc p_md;            /* (c) Any machine-dependent fields. */
424         struct  callout p_itcallout;    /* (h) Interval timer callout. */
425         struct  user *p_uarea;          /* (k) Kernel VA of u-area (CPU) */
426         u_short p_acflag;               /* (c) Accounting flags. */
427         struct  rusage *p_ru;           /* (a) Exit information. XXX */
428         struct  proc *p_peers;          /* (c) */
429         struct  proc *p_leader;         /* (c) */
430         void    *p_emuldata;            /* (c) Emulator state data. */
431 };
432
433 #define p_rlimit        p_limit->pl_rlimit
434 #define p_sigacts       p_procsig->ps_sigacts
435 #define p_sigignore     p_procsig->ps_sigignore
436 #define p_sigcatch      p_procsig->ps_sigcatch
437 #define p_session       p_pgrp->pg_session
438 #define p_pgid          p_pgrp->pg_id
439
440 #define NOCPU   0xff            /* For p_oncpu when we aren't on a CPU. */
441
442 /* Status values (p_stat). */
443 #define SIDL    1               /* Process being created by fork. */
444 #define SRUN    2               /* Currently runnable. */
445 #define SSLEEP  3               /* Sleeping on an address. */
446 #define SSTOP   4               /* Process debugging or suspension. */
447 #define SZOMB   5               /* Awaiting collection by parent. */
448 #define SWAIT   6               /* Waiting for interrupt. */
449 #define SMTX    7               /* Blocked on a mutex. */
450
451 /* Stop events */
452 #define S_EXEC          0x00001 /* stop on exec */
453 #define S_SIG           0x00002 /* stop on signal */
454 #define S_SCE           0x00004 /* stop on syscall entry */
455 #define S_SCX           0x00008 /* stop on syscall exit */
456 #define S_CORE          0x00010 /* stop on coredump */
457 #define S_EXIT          0x00020 /* stop on exit */
458 #define S_ALLSTOPS      0x0003f /* all of the above */
459
460 /* These flags are kept in p_flag. */
461 #define P_ADVLOCK       0x00001 /* Process may hold a POSIX advisory lock. */
462 #define P_CONTROLT      0x00002 /* Has a controlling terminal. */
463 #define P_KTHREAD       0x00004 /* Kernel thread. (*)*/
464 #define P_NOLOAD        0x00008 /* Ignore during load avg calculations. */
465 #define P_PPWAIT        0x00010 /* Parent is waiting for child to exec/exit. */
466 #define P_SUGID         0x00100 /* Had set id privileges since last exec. */
467 #define P_SYSTEM        0x00200 /* System proc: no sigs, stats or swapping. */
468 #define P_TRACED        0x00800 /* Debugged process being traced. */
469 #define P_WAITED        0x01000 /* Debugging process has waited for child. */
470 #define P_WEXIT         0x02000 /* Working on exiting. */
471 #define P_EXEC          0x04000 /* Process called exec. */
472 #define P_KSES          0x08000 /* Process is using KSEs. */
473
474 /* Should be moved to machine-dependent areas. */
475 #define P_BUFEXHAUST    0x100000 /* Dirty buffers flush is in progress. */
476 #define P_COWINPROGRESS 0x400000 /* Snapshot copy-on-write in progress. */
477
478 #define P_JAILED        0x1000000 /* Process is in jail. */
479 #define P_OLDMASK       0x2000000 /* Need to restore mask after suspend. */
480 #define P_ALTSTACK      0x4000000 /* Have alternate signal stack. */
481
482 /* These flags are kept in p_sflag and are protected with sched_lock. */
483 #define PS_INMEM        0x00001 /* Loaded into memory. */
484 #define PS_PROFIL       0x00004 /* Has started profiling. */
485 #define PS_ALRMPEND     0x00020 /* Pending SIGVTALRM needs to be posted. */
486 #define PS_PROFPEND     0x00040 /* Pending SIGPROF needs to be posted. */
487 #define PS_SWAPINREQ    0x00100 /* Swapin request due to wakeup. */
488 #define PS_SWAPPING     0x00200 /* Process is being swapped. */
489
490 /* flags kept in td_flags */
491 #define TDF_ONRUNQ      0x00001 /* This KE is on a run queue */
492 #define TDF_SINTR       0x00008 /* Sleep is interruptible. */
493 #define TDF_TIMEOUT     0x00010 /* Timing out during sleep. */
494 #define TDF_SELECT      0x00040 /* Selecting; wakeup/waiting danger. */
495 #define TDF_CVWAITQ     0x00080 /* Proces is on a cv_waitq (not slpq). */
496 #define TDF_TIMOFAIL    0x01000 /* Timeout from sleep after we were awake. */
497 #define TDF_DEADLKTREAT 0x800000 /* Lock aquisition - deadlock treatment. */
498
499 /* flags kept in ke_flags */
500 #define KEF_ONRUNQ      0x00001 /* This KE is on a run queue */
501 #define KEF_OWEUPC      0x00002 /* Owe process an addupc() call at next ast. */
502 #define KEF_ASTPENDING  0x00400 /* KSE has a pending ast. */
503 #define KEF_NEEDRESCHED 0x00800 /* Process needs to yield. */
504
505
506 #define P_MAGIC         0xbeefface
507
508 #ifdef _KERNEL
509
510 #ifdef MALLOC_DECLARE
511 MALLOC_DECLARE(M_PARGS);
512 MALLOC_DECLARE(M_SESSION);
513 MALLOC_DECLARE(M_SUBPROC);
514 MALLOC_DECLARE(M_ZOMBIE);
515 #endif
516
517 #define FOREACH_PROC_IN_SYSTEM(p)                                       \
518         LIST_FOREACH((p), &allproc, p_list)
519 #define FOREACH_KSEGRP_IN_PROC(p, kg)                                   \
520         TAILQ_FOREACH((kg), &(p)->p_ksegrps, kg_ksegrp)
521 #define FOREACH_THREAD_IN_GROUP(kg, td)                                 \
522         TAILQ_FOREACH((td), &(kg)->kg_threads, td_kglist)
523 #define FOREACH_KSE_IN_GROUP(kg, ke)                                    \
524         TAILQ_FOREACH((ke), &(kg)->kg_kseq, ke_kglist)
525 #define FOREACH_THREAD_IN_PROC(p, td)                                   \
526         TAILQ_FOREACH((td), &(p)->p_threads, td_plist)
527
528 static __inline int
529 sigonstack(size_t sp)
530 {
531         register struct thread *td = curthread;
532         struct proc *p = td->td_proc;
533
534         return ((p->p_flag & P_ALTSTACK) ?
535 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
536             ((p->p_sigstk.ss_size == 0) ? (p->p_sigstk.ss_flags & SS_ONSTACK) :
537                 ((sp - (size_t)p->p_sigstk.ss_sp) < p->p_sigstk.ss_size))
538 #else
539             ((sp - (size_t)p->p_sigstk.ss_sp) < p->p_sigstk.ss_size)
540 #endif
541             : 0);
542 }
543
544 /*
545  * Notify the current process (p) that it has a signal pending,
546  * process as soon as possible.
547  */
548 #define signotify(ke) do {                                              \
549         mtx_assert(&sched_lock, MA_OWNED);                              \
550         (ke)->ke_flags |= KEF_ASTPENDING;                               \
551 } while (0)
552
553 /* Handy macro to determine if p1 can mangle p2. */
554 #define PRISON_CHECK(p1, p2) \
555         ((p1)->p_prison == NULL || (p1)->p_prison == (p2)->p_prison)
556
557 /*
558  * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t,
559  * as it is used to represent "no process group".
560  */
561 #define PID_MAX         99999
562 #define NO_PID          100000
563
564 #define SESS_LEADER(p)  ((p)->p_session->s_leader == (p))
565 #define SESSHOLD(s)     ((s)->s_count++)
566 #define SESSRELE(s) {                                                   \
567         if (--(s)->s_count == 0)                                        \
568                 FREE(s, M_SESSION);                                     \
569 }
570
571 #define STOPEVENT(p, e, v) do {                                         \
572         PROC_LOCK(p);                                                   \
573         _STOPEVENT((p), (e), (v));                                      \
574         PROC_UNLOCK(p);                                                 \
575 } while (0)
576 #define _STOPEVENT(p, e, v) do {                                        \
577         PROC_LOCK_ASSERT(p, MA_OWNED);                                  \
578         if ((p)->p_stops & (e)) {                                       \
579                 stopevent((p), (e), (v));                               \
580         }                                                               \
581 } while (0)
582
583 /* Lock and unlock a process. */
584 #define PROC_LOCK(p)    mtx_lock(&(p)->p_mtx)
585 #define PROC_TRYLOCK(p) mtx_trylock(&(p)->p_mtx)
586 #define PROC_UNLOCK(p)  mtx_unlock(&(p)->p_mtx)
587 #define PROC_UNLOCK_NOSWITCH(p)                                         \
588         mtx_unlock_flags(&(p)->p_mtx, MTX_NOSWITCH)
589 #define PROC_LOCKED(p)  mtx_owned(&(p)->p_mtx)
590 #define PROC_LOCK_ASSERT(p, type)       mtx_assert(&(p)->p_mtx, (type))
591
592 /* Hold process U-area in memory, normally for ptrace/procfs work. */
593 #define PHOLD(p) do {                                                   \
594         PROC_LOCK(p);                                                   \
595         _PHOLD(p);                                                      \
596         PROC_UNLOCK(p);                                                 \
597 } while (0)
598 #define _PHOLD(p) do {                                                  \
599         PROC_LOCK_ASSERT((p), MA_OWNED);                                \
600         if ((p)->p_lock++ == 0)                                         \
601                 faultin((p));                                           \
602 } while (0)
603
604 #define PRELE(p) do {                                                   \
605         PROC_LOCK((p));                                                 \
606         _PRELE((p));                                                    \
607         PROC_UNLOCK((p));                                               \
608 } while (0)
609 #define _PRELE(p) do {                                                  \
610         PROC_LOCK_ASSERT((p), MA_OWNED);                                \
611         (--(p)->p_lock);                                                \
612 } while (0)
613
614 #define PIDHASH(pid)    (&pidhashtbl[(pid) & pidhash])
615 extern LIST_HEAD(pidhashhead, proc) *pidhashtbl;
616 extern u_long pidhash;
617
618 #define PGRPHASH(pgid)  (&pgrphashtbl[(pgid) & pgrphash])
619 extern LIST_HEAD(pgrphashhead, pgrp) *pgrphashtbl;
620 extern u_long pgrphash;
621
622 extern struct sx allproc_lock;
623 extern struct sx proctree_lock;
624 extern struct proc proc0;               /* Process slot for swapper. */
625 extern struct thread *thread0;          /* Primary thread in proc0 */
626 extern int hogticks;                    /* Limit on kernel cpu hogs. */
627 extern int nprocs, maxproc;             /* Current and max number of procs. */
628 extern int maxprocperuid;               /* Max procs per uid. */
629 extern u_long ps_arg_cache_limit;
630 extern int ps_argsopen;
631 extern int ps_showallprocs;
632 extern int sched_quantum;               /* Scheduling quantum in ticks. */
633
634 LIST_HEAD(proclist, proc);
635 TAILQ_HEAD(procqueue, proc);
636 TAILQ_HEAD(threadqueue, thread);
637 extern struct proclist allproc;         /* List of all processes. */
638 extern struct proclist zombproc;        /* List of zombie processes. */
639 extern struct proc *initproc, *pageproc; /* Process slots for init, pager. */
640 extern struct proc *updateproc;         /* Process slot for syncer (sic). */
641
642 extern struct vm_zone *proc_zone;
643
644 extern int lastpid;
645
646 /*
647  * XXX macros for scheduler.  Shouldn't be here, but currently needed for
648  * bounding the dubious p_estcpu inheritance in wait1().
649  * INVERSE_ESTCPU_WEIGHT is only suitable for statclock() frequencies in
650  * the range 100-256 Hz (approximately).
651  */
652 #define ESTCPULIM(e) \
653     min((e), INVERSE_ESTCPU_WEIGHT * (NICE_WEIGHT * (PRIO_MAX - PRIO_MIN) - \
654              RQ_PPQ) + INVERSE_ESTCPU_WEIGHT - 1)
655 #define INVERSE_ESTCPU_WEIGHT   8       /* 1 / (priorities per estcpu level). */
656 #define NICE_WEIGHT     1               /* Priorities per nice level. */
657
658 struct  proc *pfind __P((pid_t));       /* Find process by id. */
659 struct  pgrp *pgfind __P((pid_t));      /* Find process group by id. */
660 struct  proc *zpfind __P((pid_t));      /* Find zombie process by id. */
661
662 void    ast __P((struct trapframe *framep));
663 struct  thread *choosethread __P((void));
664 int     enterpgrp __P((struct proc *p, pid_t pgid, int mksess));
665 void    faultin __P((struct proc *p));
666 void    fixjobc __P((struct proc *p, struct pgrp *pgrp, int entering));
667 int     fork1 __P((struct thread *, int, struct proc **));
668 void    fork_exit __P((void (*)(void *, struct trapframe *), void *,
669             struct trapframe *));
670 void    fork_return __P((struct thread *, struct trapframe *));
671 int     inferior __P((struct proc *p));
672 int     leavepgrp __P((struct proc *p));
673 void    mi_switch __P((void));
674 int     p_candebug __P((struct proc *p1, struct proc *p2));
675 int     p_cansee __P((struct proc *p1, struct proc *p2));
676 int     p_cansched __P((struct proc *p1, struct proc *p2));
677 int     p_cansignal __P((struct proc *p1, struct proc *p2, int signum));
678 int     p_trespass __P((struct proc *p1, struct proc *p2));
679 void    procinit __P((void));
680 void    proc_linkup __P((struct proc *p));
681 void    proc_reparent __P((struct proc *child, struct proc *newparent));
682 int     procrunnable __P((void));
683 void    remrunqueue __P((struct thread *));
684 void    resetpriority __P((struct ksegrp *));
685 int     roundrobin_interval __P((void));
686 void    schedclock __P((struct thread *));
687 int     securelevel_ge __P((struct ucred *cr, int level));
688 int     securelevel_gt __P((struct ucred *cr, int level));
689 void    setrunnable __P((struct thread *));
690 void    setrunqueue __P((struct thread *));
691 void    setsugid __P((struct proc *p));
692 void    sleepinit __P((void));
693 void    stopevent __P((struct proc *, u_int, u_int));
694 void    cpu_idle __P((void));
695 void    cpu_switch __P((void));
696 void    cpu_throw __P((void)) __dead2;
697 void    unsleep __P((struct thread *));
698 void    updatepri __P((struct thread *));
699 void    userret __P((struct thread *, struct trapframe *, u_int));
700 void    maybe_resched __P((struct ksegrp *));
701
702 void    cpu_exit __P((struct thread *));
703 void    exit1 __P((struct thread *, int)) __dead2;
704 void    cpu_fork __P((struct thread *, struct proc *, int));
705 void    cpu_set_fork_handler __P((struct thread *, void (*)(void *), void *));
706 int     trace_req __P((struct proc *));
707 void    cpu_wait __P((struct proc *));
708 int     cpu_coredump __P((struct thread *, struct vnode *, struct ucred *));
709 #endif  /* _KERNEL */
710
711 #endif  /* !_SYS_PROC_H_ */