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