]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/proc.h
This commit was generated by cvs2svn to compensate for changes in r100490,
[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 #else
56 #include <sys/pcpu.h>
57 #endif
58 #include <sys/ucred.h>
59 #include <machine/proc.h>               /* Machine-dependent proc substruct. */
60 #include <vm/uma.h>
61
62 /*
63  * One structure allocated per session.
64  *
65  * List of locks
66  * (m)          locked by s_mtx mtx
67  * (e)          locked by proctree_lock sx
68  * (c)          const until freeing
69  */
70 struct session {
71         int             s_count;        /* (m)          Ref cnt; pgrps in session. */
72         struct  proc    *s_leader;      /* (m + e)      Session leader. */
73         struct  vnode   *s_ttyvp;       /* (m)          Vnode of controlling terminal. */
74         struct  tty     *s_ttyp;        /* (m)          Controlling terminal. */
75         pid_t           s_sid;          /* (c)          Session ID. */
76                                         /* (m)          Setlogin() name: */
77         char            s_login[roundup(MAXLOGNAME, sizeof(long))];
78         struct  mtx     s_mtx;          /*              Mutex to protect members */
79 };
80
81 /*
82  * One structure allocated per process group.
83  *
84  * List of locks
85  * (m)          locked by pg_mtx mtx
86  * (e)          locked by proctree_lock sx
87  * (c)          const until freeing
88  */
89 struct pgrp {
90         LIST_ENTRY(pgrp) pg_hash;       /* (e)          Hash chain. */
91         LIST_HEAD(, proc) pg_members;   /* (m + e)      Pointer to pgrp members. */
92         struct session  *pg_session;    /* (c)          Pointer to session. */
93         struct sigiolst pg_sigiolst;    /* (m)          List of sigio sources. */
94         pid_t           pg_id;          /* (c)          Pgrp id. */
95         int             pg_jobc;        /* (m)          # procs qualifying pgrp for job control */
96         struct  mtx     pg_mtx;         /*              Mutex to protect members */
97 };
98
99 struct procsig {
100         sigset_t ps_sigignore;  /* Signals being ignored. */
101         sigset_t ps_sigcatch;   /* Signals being caught by user. */
102         int      ps_flag;
103         struct   sigacts *ps_sigacts;   /* Signal actions, state. */
104         int      ps_refcnt;
105 };
106
107 #define PS_NOCLDWAIT    0x0001  /* No zombies if child dies */
108 #define PS_NOCLDSTOP    0x0002  /* No SIGCHLD when children stop. */
109 #define PS_CLDSIGIGN    0x0004  /* The SIGCHLD handler is SIG_IGN. */
110
111 /*
112  * pargs, used to hold a copy of the command line, if it had a sane length.
113  */
114 struct pargs {
115         u_int   ar_ref;         /* Reference count. */
116         u_int   ar_length;      /* Length. */
117         u_char  ar_args[];      /* Arguments. */
118 };
119
120 /*-
121  * Description of a process.
122  *
123  * This structure contains the information needed to manage a thread of
124  * control, known in UN*X as a process; it has references to substructures
125  * containing descriptions of things that the process uses, but may share
126  * with related processes.  The process structure and the substructures
127  * are always addressable except for those marked "(CPU)" below,
128  * which might be addressable only on a processor on which the process
129  * is running.
130  *
131  * Below is a key of locks used to protect each member of struct proc.  The
132  * lock is indicated by a reference to a specific character in parens in the
133  * associated comment.
134  *      * - not yet protected
135  *      a - only touched by curproc or parent during fork/wait
136  *      b - created at fork, never changes
137  *              (exception aiods switch vmspaces, but they are also
138  *              marked 'P_SYSTEM' so hopefully it will be left alone)
139  *      c - locked by proc mtx
140  *      d - locked by allproc_lock lock
141  *      e - locked by proctree_lock lock
142  *      f - session mtx
143  *      g - process group mtx
144  *      h - callout_lock mtx
145  *      i - by curproc or the master session mtx
146  *      j - locked by sched_lock mtx
147  *      k - only accessed by curthread
148  *      l - the attaching proc or attaching proc parent
149  *      m - Giant
150  *      n - not locked, lazy
151  *      o - ktrace lock
152  *      p - select lock (sellock)
153  *
154  * If the locking key specifies two identifiers (for example, p_pptr) then
155  * either lock is sufficient for read access, but both locks must be held
156  * for write access.
157  */
158 struct ithd;
159 struct nlminfo;
160 struct trapframe;
161
162 /*
163  * Here we define the four structures used for process information.
164  *
165  * The first is the thread. It might be though of as a "Kernel
166  * Schedulable Entity Context".
167  * This structure contains all the information as to where a thread of 
168  * execution is now, or was when it was suspended, why it was suspended,
169  * and anything else that will be needed to restart it when it is
170  * rescheduled. Always associated with a KSE when running, but can be
171  * reassigned to an equivalent KSE  when being restarted for
172  * load balancing. Each of these is associated with a kernel stack
173  * and a pcb.
174  * 
175  * It is important to remember that a particular thread structure only
176  * exists as long as the system call or kernel entrance (e.g. by pagefault)
177  * which it is currently executing. It should threfore NEVER be referenced
178  * by pointers in long lived structures that live longer than a single
179  * request. If several threads complete their work at the same time,
180  * they will all rewind their stacks to the uer boundary, report their
181  * completion state, and all but one will be freed. That last one will
182  * be kept to provide a kernel stack and pcb for the NEXT syscall or kernel
183  * entrance. (basically to save freeing and then re-allocating it) A process
184  * might keep a cache of threads available to allow it to quickly
185  * get one when it needs a new one. There would probably also be a system
186  * cache of free threads.
187  */
188 struct thread;
189
190 /* 
191  * The second structure is the Kernel Schedulable Entity. (KSE)
192  * As long as this is scheduled, it will continue to run any threads that
193  * are assigned to it or the KSEGRP (see later) until either it runs out
194  * of runnable threads or CPU.
195  * It runs on one CPU and is assigned a quantum of time. When a thread is
196  * blocked, The KSE continues to run and will search for another thread
197  * in a runnable state amongst those it has. It May decide to return to user
198  * mode with a new 'empty' thread if there are no runnable threads.
199  * threads are associated with a KSE for cache reasons, but a sheduled KSE with
200  * no runnable thread will try take a thread from a sibling KSE before
201  * surrendering its quantum. In some schemes it gets it's quantum from the KSEG
202  * and contributes to draining that quantum, along withthe other KSEs in
203  * the group. (undecided)
204  */
205 struct kse;
206
207 /*
208  * The KSEGRP is allocated resources across a number of CPUs.
209  * (Including a number of CPUxQUANTA. It parcels these QUANTA up among
210  * Its KSEs, each of which should be running in a different CPU.
211  * Priority and total available sheduled quanta are properties of a KSEGRP.
212  * Multiple KSEGRPs in a single process compete against each other
213  * for total quanta in the same way that a forked child competes against
214  * it's parent process.
215  */
216 struct ksegrp;
217
218 /*
219  * A process is the owner of all system resources allocated to a task
220  * except CPU quanta.
221  * All KSEGs under one process see, and have the same access to, these
222  * resources (e.g. files, memory, sockets, permissions kqueues).
223  * A process may compete for CPU cycles on the same basis as a
224  * forked process cluster by spawning several KSEGRPs. 
225  */
226 struct proc;
227
228 /***************
229  * In pictures:
230  With a single run queue used by all processors:
231
232  RUNQ: --->KSE---KSE--...               SLEEPQ:[]---THREAD---THREAD---THREAD
233            |   /                               []---THREAD
234            KSEG---THREAD--THREAD--THREAD       []
235                                                []---THREAD---THREAD
236
237   (processors run THREADs from the KSEG until they are exhausted or
238   the KSEG exhausts its quantum) 
239
240 With PER-CPU run queues:
241 KSEs on the separate run queues directly
242 They would be given priorities calculated from the KSEG.
243
244  *
245  *****************/
246
247 /*
248  * Kernel runnable context (thread).
249  * This is what is put to sleep and reactivated.
250  * The first KSE available in the correct group will run this thread.
251  * If several are available, use the one on the same CPU as last time.
252  * When waing to be run, threads are hung off the KSEGRP in priority order.
253  * with N runnable and queued KSEs in the KSEGRP, the first N threads
254  * are linked to them. Other threads are not yet assigned.
255  */
256 struct thread {
257         struct proc     *td_proc;       /* Associated process. */
258         struct ksegrp   *td_ksegrp;     /* Associated KSEG. */
259         TAILQ_ENTRY(thread) td_plist;   /* All threads in this proc */
260         TAILQ_ENTRY(thread) td_kglist;  /* All threads in this ksegrp */
261
262         /* The two queues below should someday be merged */
263         TAILQ_ENTRY(thread) td_slpq;    /* (j) Sleep queue. XXXKSE */ 
264         TAILQ_ENTRY(thread) td_blkq;    /* (j) Mutex queue. XXXKSE */ 
265         TAILQ_ENTRY(thread) td_runq;    /* (j) Run queue(s). XXXKSE */ 
266
267         TAILQ_HEAD(, selinfo) td_selq;  /* (p) List of selinfos. */
268
269 #define td_startzero td_flags
270         int             td_flags;       /* (j) TDF_* flags. */
271         struct kse      *td_last_kse;   /* Where it wants to be if possible. */
272         struct kse      *td_kse;        /* Current KSE if running. */
273         int             td_dupfd;       /* (k) Ret value from fdopen. XXX */
274         void            *td_wchan;      /* (j) Sleep address. */
275         const char      *td_wmesg;      /* (j) Reason for sleep. */
276         u_char          td_lastcpu;     /* (j) Last cpu we were on. */
277         u_char          td_inktr;       /* (k) Currently handling a KTR. */
278         u_char          td_inktrace;    /* (k) Currently handling a KTRACE. */
279         short           td_locks;       /* (k) DEBUG: lockmgr count of locks */
280         struct mtx      *td_blocked;    /* (j) Mutex process is blocked on. */
281         struct ithd     *td_ithd;       /* (b) For interrupt threads only. */
282         const char      *td_mtxname;    /* (j) Name of mutex blocked on. */
283         LIST_HEAD(, mtx) td_contested;  /* (j) Contested locks. */
284         struct lock_list_entry *td_sleeplocks; /* (k) Held sleep locks. */
285         int             td_intr_nesting_level; /* (k) Interrupt recursion. */
286         void            *td_mailbox;    /* the userland mailbox address */
287         struct ucred    *td_ucred;      /* (k) Reference to credentials. */
288         void            (*td_switchin)(void); /* (k) switchin special func */
289 #define td_endzero td_md
290
291 #define td_startcopy td_endzero
292         /* XXXKSE p_md is in the "on your own" section in old struct proc */
293         struct mdthread td_md;          /* (k) Any machine-dependent fields. */
294         register_t      td_retval[2];   /* (k) Syscall aux returns. */
295         u_char          td_base_pri;    /* (j) Thread base kernel priority. */
296         u_char          td_priority;    /* (j) Thread active priority. */
297 #define td_endcopy td_pcb
298
299         struct pcb      *td_pcb;        /* (k) Kernel VA of pcb and kstack. */
300         enum {
301                 TDS_NEW = 0x20,
302                 TDS_UNQUEUED,
303                 TDS_SLP,
304                 TDS_MTX,
305                 TDS_RUNQ,
306                 TDS_RUNNING,
307                 TDS_SUSPENDED,          /* would have liked to have run */
308                 TDS_IWAIT,
309                 TDS_SURPLUS
310         } td_state;
311         struct callout  td_slpcallout;  /* (h) Callout for sleep. */
312         struct trapframe *td_frame;     /* (k) */
313         struct vm_object *td_kstack_obj;/* (a) Kstack object. */
314         vm_offset_t     td_kstack;      /* Kernel VA of kstack. */
315         u_int           td_critnest;    /* (k) Critical section nest level. */
316 };
317 /* flags kept in td_flags */
318 #define TDF_UNBOUND     0x000001 /* may give away the kse, uses the kg runq */
319 #define TDF_INPANIC     0x000002 /* Caused a panic, let it drive crashdump */
320 #define TDF_SINTR       0x000008 /* Sleep is interruptible. */
321 #define TDF_TIMEOUT     0x000010 /* Timing out during sleep. */
322 #define TDF_SELECT      0x000040 /* Selecting; wakeup/waiting danger. */
323 #define TDF_CVWAITQ     0x000080 /* Thread is on a cv_waitq (not slpq). */
324 #define TDF_UPCALLING   0x000100 /* This thread is doing an upcall. */
325 #define TDF_INMSLEEP    0x000400 /* Don't recurse in msleep() */
326 #define TDF_TIMOFAIL    0x001000 /* Timeout from sleep after we were awake. */
327 #define TDF_DEADLKTREAT 0x800000 /* Lock aquisition - deadlock treatment. */
328
329 /*
330  * Traps for young players:
331  * The main thread flag that controls whether a thread acts as a threaded
332  * or unthreaded thread is the TDF_UNBOUND flag.
333  * UPCALLS run with the UNBOUND flags clear, after they are first scheduled.
334  * i.e. they bind themselves to whatever thread thay are first scheduled with.
335  * You may see BOUND threads in KSE processes but you should never see
336  * UNBOUND threads in non KSE processes.
337  */
338
339 /*
340  * The schedulable entity that can be given a context to run.
341  * A process may have several of these. Probably one per processor
342  * but posibly a few more. In this universe they are grouped
343  * with a KSEG that contains the priority and niceness
344  * for the group.
345  */
346 struct kse {
347         struct proc     *ke_proc;       /* Associated process. */
348         struct ksegrp   *ke_ksegrp;     /* Associated KSEG. */
349         TAILQ_ENTRY(kse) ke_kglist;     /* Queue of all KSEs in ke_ksegrp. */
350         TAILQ_ENTRY(kse) ke_kgrlist;    /* Queue of all KSEs in this state. */
351         TAILQ_ENTRY(kse) ke_procq;      /* (j) Run queue. */
352
353 #define ke_startzero ke_flags
354         int             ke_flags;       /* (j) KEF_* flags. */
355         struct thread   *ke_thread;     /* Active associated thread. */
356         struct thread   *ke_bound;      /* Thread bound to this KSE (*) */
357         /*u_int         ke_estcpu; */   /* (j) Time averaged val of cpticks. */
358         int             ke_cpticks;     /* (j) Ticks of cpu time. */
359         fixpt_t         ke_pctcpu;      /* (j) %cpu during p_swtime. */
360         u_int64_t       ke_uu;          /* (j) Previous user time in usec. */
361         u_int64_t       ke_su;          /* (j) Previous system time in usec. */
362         u_int64_t       ke_iu;          /* (j) Previous intr time in usec. */
363         u_int64_t       ke_uticks;      /* (j) Statclock hits in user mode. */
364         u_int64_t       ke_sticks;      /* (j) Statclock hits in system mode. */
365         u_int64_t       ke_iticks;      /* (j) Statclock hits in intr. */
366         u_char          ke_oncpu;       /* (j) Which cpu we are on. */
367         u_int           ke_slptime;     /* (j) Time since last idle. */
368         char            ke_rqindex;     /* (j) Run queue index. */
369         enum {
370                 KES_IDLE = 0x10,
371                 KES_ONRUNQ,
372                 KES_UNQUEUED, /* in transit */
373                 KES_THREAD      /* slaved to thread state */
374         } ke_state;     /* (j) S* process status. */
375         void            *ke_mailbox;    /* the userland mailbox address */
376         struct thread   *ke_tdspare;    /* spare thread for upcalls */
377 #define ke_endzero ke_dummy
378
379 #define ke_startcopy ke_endzero
380         u_char          ke_dummy;       /*   */
381 #define ke_endcopy ke_mdstorage
382
383         void            *ke_upcall;
384         void            *ke_stackbase;
385         u_long          ke_stacksize;
386         void            *ke_mdstorage;  /* where we store the pcb and frame */
387         struct pcb      *ke_pcb;        /* the pcb saved for the upcalls */
388         struct trapframe *ke_frame;     /* the upcall trapframe */
389         void    *mdkse;                 /* eventually you load from this in */
390                                         /* switch for our extension PCB x86 */
391 };
392 /* flags kept in ke_flags */
393 #define KEF_OWEUPC      0x00002 /* Owe process an addupc() call at next ast. */
394 #define KEF_IDLEKSE     0x00004 /* A 'Per CPU idle process'.. has one thread */
395 #define KEF_LOANED      0x00004 /* On loan from the bound thread to another */
396 #define KEF_ASTPENDING  0x00400 /* KSE has a pending ast. */
397 #define KEF_NEEDRESCHED 0x00800 /* Process needs to yield. */
398
399 /*
400  * (*) A bound KSE with a bound thread in a KSE process may be lent to
401  * Other threads, as long as those threads do not leave the kernel. 
402  * The other threads must be either exiting, or be unbound with a valid
403  * mailbox so that they can save their state there rather than going
404  * to user space. While this happens the real bound thread is still linked
405  * to the kse via the ke_bound field, and the KSE has its "KEF_LOANED
406  * flag set.
407  */
408
409 /*
410  * Kernel-scheduled entity group (KSEG).  The scheduler considers each KSEG to
411  * be an indivisible unit from a time-sharing perspective, though each KSEG may
412  * contain multiple KSEs.
413  */
414 struct ksegrp {
415         struct proc     *kg_proc;       /* Process that contains this KSEG. */
416         TAILQ_ENTRY(ksegrp) kg_ksegrp;  /* Queue of KSEGs in kg_proc. */
417         TAILQ_HEAD(, kse) kg_kseq;      /* (ke_kglist) All KSEs. */
418         TAILQ_HEAD(, kse) kg_iq;        /* (ke_kgrlist) Idle KSEs. */
419         TAILQ_HEAD(, thread) kg_threads;/* (td_kglist) All threads. */
420         TAILQ_HEAD(, thread) kg_runq;   /* (td_runq) waiting RUNNABLE threads */
421         TAILQ_HEAD(, thread) kg_slpq;   /* (td_runq) NONRUNNABLE threads. */
422
423 #define kg_startzero kg_estcpu
424         u_int           kg_estcpu;      /* Sum of the same field in KSEs. */
425         u_int           kg_slptime;     /* (j) How long completely blocked. */
426         struct thread   *kg_last_assigned; /* Last thread assigned to a KSE */
427         int             kg_numthreads;  /* Num threads in total */
428         int             kg_runnable;    /* Num runnable threads on queue. */
429         int             kg_kses;        /* Num KSEs in group. */
430         int             kg_runq_kses;   /* Num KSEs on runq. */
431         int             kg_idle_kses;   /* num KSEs idle */
432 #define kg_endzero kg_pri_class
433
434 #define kg_startcopy    kg_endzero
435         u_char          kg_pri_class;   /* (j) Scheduling class. */
436         u_char          kg_user_pri;    /* (j) User pri from estcpu and nice. */
437         char            kg_nice;        /* (j?/k?) Process "nice" value. */
438 /*      struct rtprio   kg_rtprio; */   /* (j) Realtime priority. */
439 #define kg_endcopy kg_dummy
440         int             kg_dummy;
441 };
442
443 /*
444  * The old fashionned process. May have multiple threads, KSEGRPs
445  * and KSEs. Starts off with a single embedded KSEGRP, KSE and THREAD.
446  */
447 struct proc {
448         LIST_ENTRY(proc) p_list;        /* (d) List of all processes. */
449         TAILQ_HEAD(, ksegrp) p_ksegrps; /* (kg_ksegrp) All KSEGs. */
450         TAILQ_HEAD(, thread) p_threads; /* (td_plist) Threads. (shortcut) */
451         TAILQ_HEAD(, thread) p_suspended; /* (td_runq) suspended threads */
452         struct ucred    *p_ucred;       /* (c) Process owner's identity. */
453         struct filedesc *p_fd;          /* (b) Ptr to open files structure. */
454                                         /* Accumulated stats for all KSEs? */
455         struct pstats   *p_stats;       /* (b) Accounting/statistics (CPU). */
456         struct plimit   *p_limit;       /* (m) Process limits. */
457         struct vm_object *p_upages_obj; /* (a) Upages object. */
458         struct procsig  *p_procsig;     /* (c) Signal actions, state (CPU). */
459  
460         struct ksegrp   p_ksegrp;
461         struct kse      p_kse;
462
463         /*
464          * The following don't make too much sense..
465          * See the td_ or ke_ versions of the same flags
466          */
467         int             p_flag;         /* (c) P_* flags. */
468         int             p_sflag;        /* (j) PS_* flags. */
469         enum {
470                 PRS_NEW = 0,    /* In creation */
471                 PRS_NORMAL,     /* KSEs can be run */
472                 PRS_WAIT,       /* Waiting on interrupt ? */
473                 PRS_ZOMBIE
474         } p_state;              /* (j) S* process status. */
475         pid_t           p_pid;          /* (b) Process identifier. */
476         LIST_ENTRY(proc) p_hash;        /* (d) Hash chain. */
477         LIST_ENTRY(proc) p_pglist;      /* (g + e) List of processes in pgrp. */
478         struct proc     *p_pptr;        /* (c + e) Pointer to parent process. */
479         LIST_ENTRY(proc) p_sibling;     /* (e) List of sibling processes. */
480         LIST_HEAD(, proc) p_children;   /* (e) Pointer to list of children. */
481         struct mtx      p_mtx;          /* (k) Lock for this struct. */
482
483 /* The following fields are all zeroed upon creation in fork. */
484 #define p_startzero     p_oppid
485         pid_t           p_oppid;        /* (c + e) Save ppid in ptrace. XXX */
486         struct vmspace  *p_vmspace;     /* (b) Address space. */
487         u_int           p_swtime;       /* (j) Time swapped in or out. */
488         struct itimerval p_realtimer;   /* (h?/k?) Alarm timer. */
489         struct bintime  p_runtime;      /* (j) Real time. */
490         int             p_traceflag;    /* (o) Kernel trace points. */
491         struct vnode    *p_tracep;      /* (c + o) Trace to vnode. */
492         sigset_t        p_siglist;      /* (c) Sigs arrived, not delivered. */
493         struct vnode    *p_textvp;      /* (b) Vnode of executable. */
494         char            p_lock;         /* (c) Proclock (prevent swap) count. */
495         struct klist p_klist;           /* (c) Knotes attached to this proc. */
496         struct sigiolst p_sigiolst;     /* (c) List of sigio sources. */
497         int             p_sigparent;    /* (c) Signal to parent on exit. */
498         sigset_t        p_oldsigmask;   /* (c) Saved mask from pre sigpause. */
499         int             p_sig;          /* (n) For core dump/debugger XXX. */
500         u_long          p_code;         /* (n) For core dump/debugger XXX. */
501         u_int           p_stops;        /* (c) Stop event bitmask. */
502         u_int           p_stype;        /* (c) Stop event type. */
503         char            p_step;         /* (c) Process is stopped. */
504         u_char          p_pfsflags;     /* (c) Procfs flags. */
505         struct nlminfo  *p_nlminfo;     /* (?) Only used by/for lockd. */
506         void            *p_aioinfo;     /* (c) ASYNC I/O info. */
507         int             p_numthreads;   /* (?) number of threads */
508         int             p_numksegrps;   /* (?) number of ksegrps */
509         struct thread   *p_singlethread;/* If single threading this is it */
510         int             p_suspcount;    /* # waiting threads in suspended mode*/
511 /* End area that is zeroed on creation. */
512 #define p_startcopy     p_sigmask
513
514 /* The following fields are all copied upon creation in fork. */
515 #define p_endzero       p_startcopy
516         sigset_t        p_sigmask;      /* (c) Current signal mask. */
517         stack_t         p_sigstk;       /* (c) Stack ptr and on-stack flag. */
518         int             p_magic;        /* (b) Magic number. */
519         char            p_comm[MAXCOMLEN + 1];  /* (b) Process name. */
520         struct pgrp     *p_pgrp;        /* (c + e) Pointer to process group. */
521         struct sysentvec *p_sysent;     /* (b) Syscall dispatch info. */
522         struct pargs    *p_args;        /* (c) Process arguments. */
523 /* End area that is copied on creation. */
524 #define p_endcopy       p_xstat
525
526         u_short         p_xstat;        /* (c) Exit status; also stop sig. */
527         struct mdproc   p_md;           /* (c) Any machine-dependent fields. */
528         struct callout  p_itcallout;    /* (h) Interval timer callout. */
529         struct user     *p_uarea;       /* (k) Kernel VA of u-area (CPU) */
530         u_short         p_acflag;       /* (c) Accounting flags. */
531         struct rusage   *p_ru;          /* (a) Exit information. XXX */
532         struct proc     *p_peers;       /* (c) */
533         struct proc     *p_leader;      /* (b) */
534         void            *p_emuldata;    /* (c) Emulator state data. */
535 };
536
537 #define p_rlimit        p_limit->pl_rlimit
538 #define p_sigacts       p_procsig->ps_sigacts
539 #define p_sigignore     p_procsig->ps_sigignore
540 #define p_sigcatch      p_procsig->ps_sigcatch
541 #define p_session       p_pgrp->pg_session
542 #define p_pgid          p_pgrp->pg_id
543
544 #define NOCPU   0xff            /* For p_oncpu when we aren't on a CPU. */
545
546 /* Status values (p_stat). */
547
548 /* These flags are kept in p_flag. */
549 #define P_ADVLOCK       0x00001 /* Process may hold a POSIX advisory lock. */
550 #define P_CONTROLT      0x00002 /* Has a controlling terminal. */
551 #define P_KTHREAD       0x00004 /* Kernel thread. (*)*/
552 #define P_NOLOAD        0x00008 /* Ignore during load avg calculations. */
553 #define P_PPWAIT        0x00010 /* Parent is waiting for child to exec/exit. */
554 #define P_SUGID         0x00100 /* Had set id privileges since last exec. */
555 #define P_SYSTEM        0x00200 /* System proc: no sigs, stats or swapping. */
556 #define P_WAITED        0x01000 /* Someone is waiting for us */
557 #define P_WEXIT         0x02000 /* Working on exiting. */
558 #define P_EXEC          0x04000 /* Process called exec. */
559 #define P_KSES          0x08000 /* Process is using KSEs. */
560 #define P_CONTINUED     0x10000 /* Proc has continued from a stopped state. */
561
562 /* flags that control how threads may be suspended for some reason */
563 #define P_STOPPED_SGNL  0x10000 /* Stopped due to SIGSTOP/SIGTSTP */
564 #define P_STOPPED_TRACE 0x20000 /* Stopped because of tracing */
565 #define P_STOPPED_SNGL  0x40000 /* Only one thread can continue (not to user) */
566 #define P_SINGLE_EXIT   0x00400 /* Threads suspending should exit, not wait */
567 #define P_TRACED        0x00800 /* Debugged process being traced. */
568 #define P_STOPPED       (P_STOPPED_SGNL|P_STOPPED_SNGL|P_STOPPED_TRACE)
569 #define P_SHOULDSTOP(p) ((p)->p_flag & P_STOPPED)
570
571 /* Should be moved to machine-dependent areas. */
572 #define P_UNUSED100000  0x100000
573 #define P_COWINPROGRESS 0x400000 /* Snapshot copy-on-write in progress. */
574
575 #define P_JAILED        0x1000000 /* Process is in jail. */
576 #define P_OLDMASK       0x2000000 /* Need to restore mask after suspend. */
577 #define P_ALTSTACK      0x4000000 /* Have alternate signal stack. */
578 #define P_INEXEC        0x8000000 /* Process is in execve(). */
579
580 /* These flags are kept in p_sflag and are protected with sched_lock. */
581 #define PS_INMEM        0x00001 /* Loaded into memory. */
582 #define PS_PROFIL       0x00004 /* Has started profiling. */
583 #define PS_ALRMPEND     0x00020 /* Pending SIGVTALRM needs to be posted. */
584 #define PS_PROFPEND     0x00040 /* Pending SIGPROF needs to be posted. */
585 #define PS_SWAPINREQ    0x00100 /* Swapin request due to wakeup. */
586 #define PS_SWAPPING     0x00200 /* Process is being swapped. */
587 #define PS_NEEDSIGCHK   0x02000 /* Process may need signal delivery. */
588
589 /* used only in legacy conversion code */
590 #define SIDL    1               /* Process being created by fork. */
591 #define SRUN    2               /* Currently runnable. */
592 #define SSLEEP  3               /* Sleeping on an address. */
593 #define SSTOP   4               /* Process debugging or suspension. */
594 #define SZOMB   5               /* Awaiting collection by parent. */
595 #define SWAIT   6               /* Waiting for interrupt. */
596 #define SMTX    7               /* Blocked on a mutex. */
597
598 #define P_MAGIC         0xbeefface
599
600 #ifdef _KERNEL
601
602 #ifdef MALLOC_DECLARE
603 MALLOC_DECLARE(M_PARGS);
604 MALLOC_DECLARE(M_PGRP);
605 MALLOC_DECLARE(M_SESSION);
606 MALLOC_DECLARE(M_SUBPROC);
607 MALLOC_DECLARE(M_ZOMBIE);
608 #endif
609
610 #define FOREACH_PROC_IN_SYSTEM(p)                                       \
611         LIST_FOREACH((p), &allproc, p_list)
612 #define FOREACH_KSEGRP_IN_PROC(p, kg)                                   \
613         TAILQ_FOREACH((kg), &(p)->p_ksegrps, kg_ksegrp)
614 #define FOREACH_THREAD_IN_GROUP(kg, td)                                 \
615         TAILQ_FOREACH((td), &(kg)->kg_threads, td_kglist)
616 #define FOREACH_KSE_IN_GROUP(kg, ke)                                    \
617         TAILQ_FOREACH((ke), &(kg)->kg_kseq, ke_kglist)
618 #define FOREACH_THREAD_IN_PROC(p, td)                                   \
619         TAILQ_FOREACH((td), &(p)->p_threads, td_plist)
620
621 /* XXXKSE the lines below should probably only be used in 1:1 code */
622 #define FIRST_THREAD_IN_PROC(p) TAILQ_FIRST(&p->p_threads)
623 #define FIRST_KSEGRP_IN_PROC(p) TAILQ_FIRST(&p->p_ksegrps)
624 #define FIRST_KSE_IN_KSEGRP(kg) TAILQ_FIRST(&kg->kg_kseq)
625 #define FIRST_KSE_IN_PROC(p) FIRST_KSE_IN_KSEGRP(FIRST_KSEGRP_IN_PROC(p))
626
627 static __inline int
628 sigonstack(size_t sp)
629 {
630         register struct thread *td = curthread;
631         struct proc *p = td->td_proc;
632
633         return ((p->p_flag & P_ALTSTACK) ?
634 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
635             ((p->p_sigstk.ss_size == 0) ? (p->p_sigstk.ss_flags & SS_ONSTACK) :
636                 ((sp - (size_t)p->p_sigstk.ss_sp) < p->p_sigstk.ss_size))
637 #else
638             ((sp - (size_t)p->p_sigstk.ss_sp) < p->p_sigstk.ss_size)
639 #endif
640             : 0);
641 }
642
643 /* Handy macro to determine if p1 can mangle p2. */
644 #define PRISON_CHECK(p1, p2) \
645         ((p1)->p_prison == NULL || (p1)->p_prison == (p2)->p_prison)
646
647 /*
648  * We use process IDs <= PID_MAX; PID_MAX + 1 must also fit in a pid_t,
649  * as it is used to represent "no process group".
650  */
651 #define PID_MAX         99999
652 #define NO_PID          100000
653
654 #define SESS_LEADER(p)  ((p)->p_session->s_leader == (p))
655 #define SESSHOLD(s)     ((s)->s_count++)
656 #define SESSRELE(s) {                                                   \
657         if (--(s)->s_count == 0)                                        \
658                 FREE(s, M_SESSION);                                     \
659 }
660
661 #define STOPEVENT(p, e, v) do {                                         \
662         PROC_LOCK(p);                                                   \
663         _STOPEVENT((p), (e), (v));                                      \
664         PROC_UNLOCK(p);                                                 \
665 } while (0)
666 #define _STOPEVENT(p, e, v) do {                                        \
667         PROC_LOCK_ASSERT(p, MA_OWNED);                                  \
668         if ((p)->p_stops & (e)) {                                       \
669                 stopevent((p), (e), (v));                               \
670         }                                                               \
671 } while (0)
672
673 /* Lock and unlock a process. */
674 #define PROC_LOCK(p)    mtx_lock(&(p)->p_mtx)
675 #define PROC_TRYLOCK(p) mtx_trylock(&(p)->p_mtx)
676 #define PROC_UNLOCK(p)  mtx_unlock(&(p)->p_mtx)
677 #define PROC_LOCKED(p)  mtx_owned(&(p)->p_mtx)
678 #define PROC_LOCK_ASSERT(p, type)       mtx_assert(&(p)->p_mtx, (type))
679
680 /* Lock and unlock a process group. */
681 #define PGRP_LOCK(pg)   mtx_lock(&(pg)->pg_mtx)
682 #define PGRP_UNLOCK(pg) mtx_unlock(&(pg)->pg_mtx)
683 #define PGRP_LOCKED(pg) mtx_owned(&(pg)->pg_mtx)
684 #define PGRP_LOCK_ASSERT(pg, type)      mtx_assert(&(pg)->pg_mtx, (type))
685
686 #define PGRP_LOCK_PGSIGNAL(pg)                                          \
687         do {                                                            \
688                 if ((pg) != NULL)                                       \
689                         PGRP_LOCK(pg);                                  \
690         } while (0);
691
692 #define PGRP_UNLOCK_PGSIGNAL(pg)                                        \
693         do {                                                            \
694                 if ((pg) != NULL)                                       \
695                         PGRP_UNLOCK(pg);                                \
696         } while (0);
697
698 /* Lock and unlock a session. */
699 #define SESS_LOCK(s)    mtx_lock(&(s)->s_mtx)
700 #define SESS_UNLOCK(s)  mtx_unlock(&(s)->s_mtx)
701 #define SESS_LOCKED(s)  mtx_owned(&(s)->s_mtx)
702 #define SESS_LOCK_ASSERT(s, type)       mtx_assert(&(s)->s_mtx, (type))
703
704 /* Hold process U-area in memory, normally for ptrace/procfs work. */
705 #define PHOLD(p) do {                                                   \
706         PROC_LOCK(p);                                                   \
707         _PHOLD(p);                                                      \
708         PROC_UNLOCK(p);                                                 \
709 } while (0)
710 #define _PHOLD(p) do {                                                  \
711         PROC_LOCK_ASSERT((p), MA_OWNED);                                \
712         if ((p)->p_lock++ == 0)                                         \
713                 faultin((p));                                           \
714 } while (0)
715
716 #define PRELE(p) do {                                                   \
717         PROC_LOCK((p));                                                 \
718         _PRELE((p));                                                    \
719         PROC_UNLOCK((p));                                               \
720 } while (0)
721 #define _PRELE(p) do {                                                  \
722         PROC_LOCK_ASSERT((p), MA_OWNED);                                \
723         (--(p)->p_lock);                                                \
724 } while (0)
725
726 /* Lock and unlock process arguments. */
727 #define PARGS_LOCK(p)           mtx_lock(&pargs_ref_lock)
728 #define PARGS_UNLOCK(p)         mtx_unlock(&pargs_ref_lock)
729
730 #define PIDHASH(pid)    (&pidhashtbl[(pid) & pidhash])
731 extern LIST_HEAD(pidhashhead, proc) *pidhashtbl;
732 extern u_long pidhash;
733
734 #define PGRPHASH(pgid)  (&pgrphashtbl[(pgid) & pgrphash])
735 extern LIST_HEAD(pgrphashhead, pgrp) *pgrphashtbl;
736 extern u_long pgrphash;
737
738 extern struct sx allproc_lock;
739 extern struct sx proctree_lock;
740 extern struct mtx pargs_ref_lock;
741 extern struct proc proc0;               /* Process slot for swapper. */
742 extern struct thread thread0;           /* Primary thread in proc0 */
743 extern int hogticks;                    /* Limit on kernel cpu hogs. */
744 extern int nprocs, maxproc;             /* Current and max number of procs. */
745 extern int maxprocperuid;               /* Max procs per uid. */
746 extern u_long ps_arg_cache_limit;
747 extern int ps_argsopen;
748 extern int ps_showallprocs;
749 extern int sched_quantum;               /* Scheduling quantum in ticks. */
750
751 LIST_HEAD(proclist, proc);
752 TAILQ_HEAD(procqueue, proc);
753 TAILQ_HEAD(threadqueue, thread);
754 extern struct proclist allproc;         /* List of all processes. */
755 extern struct proclist zombproc;        /* List of zombie processes. */
756 extern struct proc *initproc, *pageproc; /* Process slots for init, pager. */
757 extern struct proc *updateproc;         /* Process slot for syncer (sic). */
758
759 extern uma_zone_t proc_zone;
760
761 extern int lastpid;
762
763 /*
764  * XXX macros for scheduler.  Shouldn't be here, but currently needed for
765  * bounding the dubious p_estcpu inheritance in wait1().
766  * INVERSE_ESTCPU_WEIGHT is only suitable for statclock() frequencies in
767  * the range 100-256 Hz (approximately).
768  */
769 #define ESTCPULIM(e) \
770     min((e), INVERSE_ESTCPU_WEIGHT * (NICE_WEIGHT * (PRIO_MAX - PRIO_MIN) - \
771              RQ_PPQ) + INVERSE_ESTCPU_WEIGHT - 1)
772 #define INVERSE_ESTCPU_WEIGHT   8       /* 1 / (priorities per estcpu level). */
773 #define NICE_WEIGHT     1               /* Priorities per nice level. */
774
775 struct  proc *pfind(pid_t);     /* Find process by id. */
776 struct  pgrp *pgfind(pid_t);    /* Find process group by id. */
777 struct  proc *zpfind(pid_t);    /* Find zombie process by id. */
778
779 void    ast(struct trapframe *framep);
780 struct  thread *choosethread(void);
781 int     cr_cansignal(struct ucred *cred, struct proc *proc, int signum);
782 int     enterpgrp(struct proc *p, pid_t pgid, struct pgrp *pgrp, struct session *sess);
783 int     enterthispgrp(struct proc *p, struct pgrp *pgrp);
784 void    faultin(struct proc *p);
785 void    fixjobc(struct proc *p, struct pgrp *pgrp, int entering);
786 int     fork1(struct thread *, int, struct proc **);
787 void    fork_exit(void (*)(void *, struct trapframe *), void *,
788             struct trapframe *);
789 void    fork_return(struct thread *, struct trapframe *);
790 int     inferior(struct proc *p);
791 int     leavepgrp(struct proc *p);
792 void    mi_switch(void);
793 int     p_candebug(struct thread *td, struct proc *p);
794 int     p_cansee(struct thread *td, struct proc *p);
795 int     p_cansched(struct thread *td, struct proc *p);
796 int     p_cansignal(struct thread *td, struct proc *p, int signum);
797 struct  pargs *pargs_alloc(int len);
798 void    pargs_drop(struct pargs *pa);
799 void    pargs_free(struct pargs *pa);
800 void    pargs_hold(struct pargs *pa);
801 void    procinit(void);
802 void    threadinit(void);
803 void    proc_linkup(struct proc *p, struct ksegrp *kg,
804             struct kse *ke, struct thread *td);
805 void    proc_reparent(struct proc *child, struct proc *newparent);
806 void    remrunqueue(struct thread *);
807 void    resetpriority(struct ksegrp *);
808 int     roundrobin_interval(void);
809 void    schedclock(struct thread *);
810 int     securelevel_ge(struct ucred *cr, int level);
811 int     securelevel_gt(struct ucred *cr, int level);
812 void    setrunnable(struct thread *);
813 void    setrunqueue(struct thread *);
814 void    setsugid(struct proc *p);
815 void    sleepinit(void);
816 void    stopevent(struct proc *, u_int, u_int);
817 void    cpu_idle(void);
818 void    cpu_switch(void);
819 void    cpu_throw(void) __dead2;
820 void    unsleep(struct thread *);
821 void    updatepri(struct thread *);
822 void    userret(struct thread *, struct trapframe *, u_int);
823 void    maybe_resched(struct thread *);
824
825 void    cpu_exit(struct thread *);
826 void    cpu_sched_exit(struct thread *);
827 void    exit1(struct thread *, int) __dead2;
828 void    cpu_fork(struct thread *, struct proc *, struct thread *, int);
829 void    cpu_set_fork_handler(struct thread *, void (*)(void *), void *);
830 void    cpu_wait(struct proc *);
831 int     cpu_coredump(struct thread *, struct vnode *, struct ucred *);
832
833 /* New in KSE. */
834 struct  thread *thread_alloc(void);
835 void    thread_free(struct thread *td);
836 int     cpu_export_context(struct thread *td);
837 void    cpu_free_kse_mdstorage(struct kse *kse);
838 void    cpu_save_upcall(struct thread *td, struct kse *newkse);
839 void    cpu_set_args(struct thread *, struct kse *);
840 void    cpu_set_upcall(struct thread *td, void *pcb);
841 void    cpu_thread_exit(struct thread *);
842 void    cpu_thread_setup(struct thread *td);
843 void    kse_reassign(struct kse *ke);
844 void    kse_link(struct kse *ke, struct ksegrp *kg);
845 void    ksegrp_link(struct ksegrp *kg, struct proc *p);
846 int     kserunnable(void);
847 void    make_kse_runnable(struct kse *ke);
848 void    thread_exit(void) __dead2;
849 int     thread_export_context(struct thread *td);
850 void    thread_link(struct thread *td, struct ksegrp *kg);
851 void    thread_reap(void);
852 struct thread *thread_schedule_upcall(struct thread *td, struct kse *ke);
853 int     thread_single(int how);
854 #define SNGLE_NO_EXIT 0                 /* values for 'how' */
855 #define SNGLE_EXIT 1
856 void    thread_single_end(void);
857 void    thread_stash(struct thread *td);
858 int     thread_suspend_check(int how);
859 void    thread_unsuspend(struct proc *p);
860 int     thread_userret(struct proc *p, struct ksegrp *kg, struct kse *ke,
861             struct thread *td, struct trapframe *frame);
862
863 void    thread_sanity_check(struct thread *td);
864 #endif  /* _KERNEL */
865
866 #endif  /* !_SYS_PROC_H_ */