xref: /xnu-11417.101.15/bsd/sys/sem_internal.h (revision e3723e1f17661b24996789d8afc084c0c3303b26)
1*e3723e1fSApple OSS Distributions /*
2*e3723e1fSApple OSS Distributions  * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
3*e3723e1fSApple OSS Distributions  *
4*e3723e1fSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*e3723e1fSApple OSS Distributions  *
6*e3723e1fSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*e3723e1fSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*e3723e1fSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*e3723e1fSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*e3723e1fSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*e3723e1fSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*e3723e1fSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*e3723e1fSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*e3723e1fSApple OSS Distributions  *
15*e3723e1fSApple OSS Distributions  * Please obtain a copy of the License at
16*e3723e1fSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*e3723e1fSApple OSS Distributions  *
18*e3723e1fSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*e3723e1fSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*e3723e1fSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*e3723e1fSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*e3723e1fSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*e3723e1fSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*e3723e1fSApple OSS Distributions  * limitations under the License.
25*e3723e1fSApple OSS Distributions  *
26*e3723e1fSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*e3723e1fSApple OSS Distributions  */
28*e3723e1fSApple OSS Distributions /*
29*e3723e1fSApple OSS Distributions  * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
30*e3723e1fSApple OSS Distributions  * support for mandatory and extensible security protections.  This notice
31*e3723e1fSApple OSS Distributions  * is included in support of clause 2.2 (b) of the Apple Public License,
32*e3723e1fSApple OSS Distributions  * Version 2.0.
33*e3723e1fSApple OSS Distributions  */
34*e3723e1fSApple OSS Distributions /*	$NetBSD: sem.h,v 1.5 1994/06/29 06:45:15 cgd Exp $	*/
35*e3723e1fSApple OSS Distributions 
36*e3723e1fSApple OSS Distributions /*
37*e3723e1fSApple OSS Distributions  * SVID compatible sem_internal.h file
38*e3723e1fSApple OSS Distributions  *
39*e3723e1fSApple OSS Distributions  * Author:  Daniel Boulet
40*e3723e1fSApple OSS Distributions  */
41*e3723e1fSApple OSS Distributions /*
42*e3723e1fSApple OSS Distributions  * John Bellardo modified the implementation for Darwin. 12/2000
43*e3723e1fSApple OSS Distributions  */
44*e3723e1fSApple OSS Distributions 
45*e3723e1fSApple OSS Distributions #ifndef _SYS_SEM__INTERNALH_
46*e3723e1fSApple OSS Distributions #define _SYS_SEM__INTERNALH_
47*e3723e1fSApple OSS Distributions 
48*e3723e1fSApple OSS Distributions #include <sys/sem.h>
49*e3723e1fSApple OSS Distributions #include <sys/cdefs.h>
50*e3723e1fSApple OSS Distributions 
51*e3723e1fSApple OSS Distributions 
52*e3723e1fSApple OSS Distributions /*
53*e3723e1fSApple OSS Distributions  * This structure is variant for 64 bits because of sem_otime and sem_ctime.
54*e3723e1fSApple OSS Distributions  */
55*e3723e1fSApple OSS Distributions 
56*e3723e1fSApple OSS Distributions #if __DARWIN_ALIGN_NATURAL
57*e3723e1fSApple OSS Distributions #pragma options align=natural
58*e3723e1fSApple OSS Distributions #endif
59*e3723e1fSApple OSS Distributions 
60*e3723e1fSApple OSS Distributions struct user_semid_ds {
61*e3723e1fSApple OSS Distributions 	struct ipc_perm sem_perm;       /* [XSI] operation permission struct */
62*e3723e1fSApple OSS Distributions 	struct sem      *sem_base;      /* 32 bit base ptr for semaphore set */
63*e3723e1fSApple OSS Distributions 	unsigned short  sem_nsems;      /* [XSI] number of sems in set */
64*e3723e1fSApple OSS Distributions 	user_time_t     sem_otime;      /* [XSI] last operation time */
65*e3723e1fSApple OSS Distributions 	__int32_t       sem_pad1;       /* RESERVED: DO NOT USE! */
66*e3723e1fSApple OSS Distributions 	user_time_t     sem_ctime;      /* [XSI] last change time */
67*e3723e1fSApple OSS Distributions 	                                /* Times measured in secs since */
68*e3723e1fSApple OSS Distributions 	                                /* 00:00:00 GMT, Jan. 1, 1970 */
69*e3723e1fSApple OSS Distributions 	__int32_t       sem_pad2;       /* RESERVED: DO NOT USE! */
70*e3723e1fSApple OSS Distributions 	__int32_t       sem_pad3[4];    /* RESERVED: DO NOT USE! */
71*e3723e1fSApple OSS Distributions };
72*e3723e1fSApple OSS Distributions 
73*e3723e1fSApple OSS Distributions #pragma pack(4)
74*e3723e1fSApple OSS Distributions struct user64_semid_ds {
75*e3723e1fSApple OSS Distributions 	struct ipc_perm sem_perm;       /* [XSI] operation permission struct */
76*e3723e1fSApple OSS Distributions 	int32_t sem_base;       /* 32 bit base ptr for semaphore set */
77*e3723e1fSApple OSS Distributions 	unsigned short  sem_nsems;      /* [XSI] number of sems in set */
78*e3723e1fSApple OSS Distributions 	user64_time_t   sem_otime;      /* [XSI] last operation time */
79*e3723e1fSApple OSS Distributions 	int32_t sem_pad1;       /* RESERVED: DO NOT USE! */
80*e3723e1fSApple OSS Distributions 	user64_time_t   sem_ctime;      /* [XSI] last change time */
81*e3723e1fSApple OSS Distributions 	                                /* Times measured in secs since */
82*e3723e1fSApple OSS Distributions 	                                /* 00:00:00 GMT, Jan. 1, 1970 */
83*e3723e1fSApple OSS Distributions 	int32_t sem_pad2;       /* RESERVED: DO NOT USE! */
84*e3723e1fSApple OSS Distributions 	int32_t sem_pad3[4];    /* RESERVED: DO NOT USE! */
85*e3723e1fSApple OSS Distributions };
86*e3723e1fSApple OSS Distributions 
87*e3723e1fSApple OSS Distributions struct user32_semid_ds {
88*e3723e1fSApple OSS Distributions 	struct ipc_perm sem_perm;       /* [XSI] operation permission struct */
89*e3723e1fSApple OSS Distributions 	int32_t sem_base;       /* 32 bit base ptr for semaphore set */
90*e3723e1fSApple OSS Distributions 	unsigned short  sem_nsems;      /* [XSI] number of sems in set */
91*e3723e1fSApple OSS Distributions 	user32_time_t   sem_otime;      /* [XSI] last operation time */
92*e3723e1fSApple OSS Distributions 	int32_t sem_pad1;       /* RESERVED: DO NOT USE! */
93*e3723e1fSApple OSS Distributions 	user32_time_t   sem_ctime;      /* [XSI] last change time */
94*e3723e1fSApple OSS Distributions 	                                /* Times measured in secs since */
95*e3723e1fSApple OSS Distributions 	                                /* 00:00:00 GMT, Jan. 1, 1970 */
96*e3723e1fSApple OSS Distributions 	int32_t sem_pad2;       /* RESERVED: DO NOT USE! */
97*e3723e1fSApple OSS Distributions 	int32_t sem_pad3[4];    /* RESERVED: DO NOT USE! */
98*e3723e1fSApple OSS Distributions };
99*e3723e1fSApple OSS Distributions #pragma pack()
100*e3723e1fSApple OSS Distributions 
101*e3723e1fSApple OSS Distributions union user_semun {
102*e3723e1fSApple OSS Distributions 	user_addr_t     buf;            /* buffer for IPC_STAT & IPC_SET */
103*e3723e1fSApple OSS Distributions 	user_addr_t     array;          /* array for GETALL & SETALL */
104*e3723e1fSApple OSS Distributions };
105*e3723e1fSApple OSS Distributions typedef union user_semun user_semun_t;
106*e3723e1fSApple OSS Distributions 
107*e3723e1fSApple OSS Distributions #if __DARWIN_ALIGN_NATURAL
108*e3723e1fSApple OSS Distributions #pragma options align=reset
109*e3723e1fSApple OSS Distributions #endif
110*e3723e1fSApple OSS Distributions 
111*e3723e1fSApple OSS Distributions 
112*e3723e1fSApple OSS Distributions /*
113*e3723e1fSApple OSS Distributions  * Kernel implementation stuff
114*e3723e1fSApple OSS Distributions  */
115*e3723e1fSApple OSS Distributions #define SEMVMX  32767           /* semaphore maximum value */
116*e3723e1fSApple OSS Distributions #define SEMAEM  16384           /* adjust on exit max value */
117*e3723e1fSApple OSS Distributions 
118*e3723e1fSApple OSS Distributions /*
119*e3723e1fSApple OSS Distributions  * Configuration parameters.  SEMMNI, SEMMNS, and SEMMNU are hard limits.
120*e3723e1fSApple OSS Distributions  * The code dynamically allocates enough memory to satisfy the current
121*e3723e1fSApple OSS Distributions  * demand in even increments of SEMMNI_INC, SEMMNS_INC, and SEMMNU_INC.
122*e3723e1fSApple OSS Distributions  * The code will never allocate more than the hard limits.  The *_INC's
123*e3723e1fSApple OSS Distributions  * are defined in the kernel section of the header.
124*e3723e1fSApple OSS Distributions  */
125*e3723e1fSApple OSS Distributions /*
126*e3723e1fSApple OSS Distributions  * Configuration parameters
127*e3723e1fSApple OSS Distributions  */
128*e3723e1fSApple OSS Distributions #ifndef SEMMNS                  /* # of semaphores in system */
129*e3723e1fSApple OSS Distributions #define SEMMNS  (1048576/sizeof(struct sem))
130*e3723e1fSApple OSS Distributions #endif                          /* no more than 1M of semaphore data */
131*e3723e1fSApple OSS Distributions #ifndef SEMMNI                  /* # of semaphore identifiers */
132*e3723e1fSApple OSS Distributions #define SEMMNI  SEMMNS          /* max of 1 for each semaphore */
133*e3723e1fSApple OSS Distributions #endif
134*e3723e1fSApple OSS Distributions #ifndef SEMUME
135*e3723e1fSApple OSS Distributions #define SEMUME  10              /* max # of undo entries per process */
136*e3723e1fSApple OSS Distributions #endif
137*e3723e1fSApple OSS Distributions #ifndef SEMMNU                  /* # of undo structures in system */
138*e3723e1fSApple OSS Distributions #define SEMMNU  SEMMNS          /* 1 for each semaphore.  This is quite large */
139*e3723e1fSApple OSS Distributions #endif                          /* This should be max 1 for each process */
140*e3723e1fSApple OSS Distributions 
141*e3723e1fSApple OSS Distributions /* shouldn't need tuning */
142*e3723e1fSApple OSS Distributions #ifndef SEMMAP
143*e3723e1fSApple OSS Distributions #define SEMMAP  30              /* # of entries in semaphore map */
144*e3723e1fSApple OSS Distributions #endif
145*e3723e1fSApple OSS Distributions #ifndef SEMMSL
146*e3723e1fSApple OSS Distributions #define SEMMSL  SEMMNS          /* max # of semaphores per id */
147*e3723e1fSApple OSS Distributions #endif
148*e3723e1fSApple OSS Distributions #ifndef SEMOPM
149*e3723e1fSApple OSS Distributions #define SEMOPM  5               /* max # of operations per semop call */
150*e3723e1fSApple OSS Distributions #endif
151*e3723e1fSApple OSS Distributions 
152*e3723e1fSApple OSS Distributions 
153*e3723e1fSApple OSS Distributions /*
154*e3723e1fSApple OSS Distributions  * Undo structure (internal: one per process)
155*e3723e1fSApple OSS Distributions  */
156*e3723e1fSApple OSS Distributions struct sem_undo {
157*e3723e1fSApple OSS Distributions 	int     un_next_idx;    /* index of next active undo structure */
158*e3723e1fSApple OSS Distributions 	struct  proc *un_proc;          /* owner of this structure */
159*e3723e1fSApple OSS Distributions 	short   un_cnt;                 /* # of active entries */
160*e3723e1fSApple OSS Distributions 	struct undo {
161*e3723e1fSApple OSS Distributions 		short   une_adjval;     /* adjust on exit values */
162*e3723e1fSApple OSS Distributions 		short   une_num;        /* semaphore # */
163*e3723e1fSApple OSS Distributions 		int     une_id;         /* semid */
164*e3723e1fSApple OSS Distributions 		struct undo *une_next;  /* next undo entry */
165*e3723e1fSApple OSS Distributions 	} *un_ent;                      /* undo entries */
166*e3723e1fSApple OSS Distributions };
167*e3723e1fSApple OSS Distributions 
168*e3723e1fSApple OSS Distributions /*
169*e3723e1fSApple OSS Distributions  * semaphore info struct (internal; for administrative limits and ipcs)
170*e3723e1fSApple OSS Distributions  */
171*e3723e1fSApple OSS Distributions struct seminfo {
172*e3723e1fSApple OSS Distributions 	int     semmap,         /* # of entries in semaphore map */
173*e3723e1fSApple OSS Distributions 	    semmni,             /* # of semaphore identifiers */
174*e3723e1fSApple OSS Distributions 	    semmns,             /* # of semaphores in system */
175*e3723e1fSApple OSS Distributions 	    semmnu,             /* # of undo structures in system */
176*e3723e1fSApple OSS Distributions 	    semmsl,             /* max # of semaphores per id */
177*e3723e1fSApple OSS Distributions 	    semopm,             /* max # of operations per semop call */
178*e3723e1fSApple OSS Distributions 	    semume,             /* max # of undo entries per process */
179*e3723e1fSApple OSS Distributions 	    semusz,             /* size in bytes of undo structure */
180*e3723e1fSApple OSS Distributions 	    semvmx,             /* semaphore maximum value */
181*e3723e1fSApple OSS Distributions 	    semaem;             /* adjust on exit max value */
182*e3723e1fSApple OSS Distributions };
183*e3723e1fSApple OSS Distributions extern struct seminfo   seminfo;
184*e3723e1fSApple OSS Distributions 
185*e3723e1fSApple OSS Distributions /*
186*e3723e1fSApple OSS Distributions  * Kernel wrapper for the user-level structure
187*e3723e1fSApple OSS Distributions  */
188*e3723e1fSApple OSS Distributions struct semid_kernel {
189*e3723e1fSApple OSS Distributions 	struct  user_semid_ds u;
190*e3723e1fSApple OSS Distributions 	struct  label *label;   /* MAC framework label */
191*e3723e1fSApple OSS Distributions };
192*e3723e1fSApple OSS Distributions 
193*e3723e1fSApple OSS Distributions 
194*e3723e1fSApple OSS Distributions /* internal "mode" bits */
195*e3723e1fSApple OSS Distributions #define SEM_ALLOC       01000   /* semaphore is allocated */
196*e3723e1fSApple OSS Distributions #define SEM_DEST        02000   /* semaphore will be destroyed on last detach */
197*e3723e1fSApple OSS Distributions 
198*e3723e1fSApple OSS Distributions #define SEMMNI_INC      32      /* increment value for semaphore identifiers */
199*e3723e1fSApple OSS Distributions #define SEMMNS_INC      64      /* increment value for semaphores */
200*e3723e1fSApple OSS Distributions #define SEMMNU_INC      32      /* increment value for undo structures */
201*e3723e1fSApple OSS Distributions 
202*e3723e1fSApple OSS Distributions /*
203*e3723e1fSApple OSS Distributions  * Due to the way semaphore memory is allocated, we have to ensure that
204*e3723e1fSApple OSS Distributions  * SEMUSZ is properly aligned.
205*e3723e1fSApple OSS Distributions  *
206*e3723e1fSApple OSS Distributions  * We are not doing strange semaphore memory allocation anymore, so
207*e3723e1fSApple OSS Distributions  * these macros are no longer needed.
208*e3723e1fSApple OSS Distributions  */
209*e3723e1fSApple OSS Distributions 
210*e3723e1fSApple OSS Distributions /*
211*e3723e1fSApple OSS Distributions  * #define SEM_ALIGN(bytes) (((bytes) + (sizeof(long) - 1)) & ~(sizeof(long) - 1))
212*e3723e1fSApple OSS Distributions  */
213*e3723e1fSApple OSS Distributions 
214*e3723e1fSApple OSS Distributions /* actual size of an undo structure */
215*e3723e1fSApple OSS Distributions /*
216*e3723e1fSApple OSS Distributions  * #define SEMUSZ	SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME]))
217*e3723e1fSApple OSS Distributions  */
218*e3723e1fSApple OSS Distributions #define SEMUSZ          sizeof(struct sem_undo)
219*e3723e1fSApple OSS Distributions 
220*e3723e1fSApple OSS Distributions /*
221*e3723e1fSApple OSS Distributions  * Macro to find a particular sem_undo vector
222*e3723e1fSApple OSS Distributions  */
223*e3723e1fSApple OSS Distributions /* Until we can initialize seminfo.semusz to SEMUSZ, we hard code the size macro
224*e3723e1fSApple OSS Distributions  * in SEMU.  This should be fixed when (if) we implement dynamic pool sizes
225*e3723e1fSApple OSS Distributions  *
226*e3723e1fSApple OSS Distributions  * #define SEMU(ix)     ((struct sem_undo *)(((intptr_t)semu)+ix * seminfo.semusz))
227*e3723e1fSApple OSS Distributions  */
228*e3723e1fSApple OSS Distributions /*
229*e3723e1fSApple OSS Distributions  * This macro doesn't work because we are using a staticly allocated array
230*e3723e1fSApple OSS Distributions  * for semu now.
231*e3723e1fSApple OSS Distributions  * #define SEMU(ix)        ((struct sem_undo *)(((intptr_t)semu)+ix * SEMUSZ))
232*e3723e1fSApple OSS Distributions  */
233*e3723e1fSApple OSS Distributions #define SEMU(ix)        (&semu[ix])
234*e3723e1fSApple OSS Distributions 
235*e3723e1fSApple OSS Distributions 
236*e3723e1fSApple OSS Distributions /*
237*e3723e1fSApple OSS Distributions  * Process sem_undo vectors at proc exit.
238*e3723e1fSApple OSS Distributions  */
239*e3723e1fSApple OSS Distributions void    semexit(struct proc *p);
240*e3723e1fSApple OSS Distributions 
241*e3723e1fSApple OSS Distributions #endif /* !_SYS_SEM__INTERNALH_ */
242