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