xref: /minix/minix/include/minix/endpoint.h (revision 83133719)
1 
2 #ifndef _MINIX_ENDPOINT_H
3 #define _MINIX_ENDPOINT_H 1
4 
5 #ifdef _MINIX_SYSTEM
6 
7 #include <minix/sys_config.h>
8 #include <minix/com.h>
9 #include <limits.h>
10 #include <minix/type.h>
11 
12 /*
13  * Endpoints are split into two parts: a process slot and a generation number.
14  *
15  * The process slot number identifies the slot in various process tables.
16  * It is positive or zero for user processes, and negative for kernel tasks.
17  * Constants dictate that with the current endpoint layout, the maximum range
18  * of process slot numbers is [-MAX_NR_TASKS,MAX_NR_PROCS>.  The used part of
19  * the range is currently [-NR_TASKS,NR_PROCS> -- these two constants may be
20  * changed within the maximum range without changing the endpoint layout.
21  *
22  * The generation number is a per-slot number that gets increased by one every
23  * time a slot is reused for a new process.  The generation number minimizes
24  * the chance that the endpoint of a dead process can (accidentially) be used
25  * to communicate with a different, live process.  Preventing such accidents
26  * is essential for proper system service restartability support.
27  *
28  * The split between the two parts of the endpoint is such that when the
29  * generation number is zero, the endpoint number equals the process slot
30  * number, even for negative task numbers.  This is required for the endpoint
31  * numbers hardcoded in <minix/com.h>, and it makes endpoint numbers easy to
32  * read in general.
33  *
34  * There are three special endpoint numbers: ANY, NONE, and SELF.  These
35  * numbers are used to identify "any process", "no process at all", and
36  * "own process", respectively.  They fall outside the normal range of
37  * process slot numbers, and are always of generation zero.
38  */
39 
40 /*
41  * The following constant defines the split between the two parts of the
42  * endpoint numbers.  It can be adjusted to allow for either more processes
43  * or more per-process generation numbers.  Changing it will change the
44  * endpoint number layout, and thus break binary compatibility with existing
45  * processes.
46  */
47 #define _ENDPOINT_GENERATION_SHIFT	15
48 
49 /* Derived constants. */
50 #define _ENDPOINT_GENERATION_SIZE	(1 << _ENDPOINT_GENERATION_SHIFT)
51 /* INT_MAX is used here to prevent signedness issues with the macros below. */
52 #define _ENDPOINT_MAX_GENERATION	(INT_MAX/_ENDPOINT_GENERATION_SIZE-1)
53 #define _ENDPOINT_SLOT_TOP	(_ENDPOINT_GENERATION_SIZE-MAX_NR_TASKS)
54 
55 /* The special endpoint numbers, and the resulting maximum slot number. */
56 #define ANY		((endpoint_t) (_ENDPOINT_SLOT_TOP - 1))
57 #define NONE		((endpoint_t) (_ENDPOINT_SLOT_TOP - 2))
58 #define SELF		((endpoint_t) (_ENDPOINT_SLOT_TOP - 3))
59 #define MAX_NR_PROCS		      (_ENDPOINT_SLOT_TOP - 3)	/* (int)SELF */
60 
61 /* Sanity check. */
62 #if NR_PROCS > MAX_NR_PROCS
63 #error "NR_PROCS exceeds MAX_NR_PROCS, increase _ENDPOINT_GENERATION_SHIFT"
64 #endif
65 
66 /* Generation + Process slot number <-> endpoint. */
67 #define _ENDPOINT(g, p) \
68 	((endpoint_t)(((g) << _ENDPOINT_GENERATION_SHIFT) + (p)))
69 #define _ENDPOINT_G(e) (((e)+MAX_NR_TASKS) >> _ENDPOINT_GENERATION_SHIFT)
70 #define _ENDPOINT_P(e) \
71 	((((e)+MAX_NR_TASKS) & (_ENDPOINT_GENERATION_SIZE - 1)) - MAX_NR_TASKS)
72 
73 #endif /* _MINIX_SYSTEM */
74 
75 #endif
76