xref: /onnv-gate/usr/src/uts/common/sys/ipc_impl.h (revision 2677:212d61b14a8b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2677Sml93401  * Common Development and Distribution License (the "License").
6*2677Sml93401  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2677Sml93401  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #ifndef	_IPC_IMPL_H
270Sstevel@tonic-gate #define	_IPC_IMPL_H
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/ipc.h>
330Sstevel@tonic-gate #include <sys/mutex.h>
34*2677Sml93401 #include <sys/ipc_rctl.h>
350Sstevel@tonic-gate #include <sys/project.h>
36*2677Sml93401 #include <sys/zone.h>
370Sstevel@tonic-gate #include <sys/sysmacros.h>
380Sstevel@tonic-gate #include <sys/avl.h>
390Sstevel@tonic-gate #include <sys/id_space.h>
400Sstevel@tonic-gate #include <sys/cred.h>
410Sstevel@tonic-gate #include <sys/list.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #ifdef	__cplusplus
440Sstevel@tonic-gate extern "C" {
450Sstevel@tonic-gate #endif
460Sstevel@tonic-gate 
470Sstevel@tonic-gate typedef uint64_t ipc_time_t;
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /* For xxxctl64 */
500Sstevel@tonic-gate #define	IPC_SET64	13	/* set options */
510Sstevel@tonic-gate #define	IPC_STAT64	14	/* get options */
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate  * There are two versions of the userland ipc_perm structure:
550Sstevel@tonic-gate  *   ipc_perm     - the version used by user applications and by the kernel
560Sstevel@tonic-gate  *		    when the user and kernel data models match (in ipc.h)
570Sstevel@tonic-gate  *   ipc_perm32   - the 64-bit kernel's view of a 32-bit struct ipc_perm
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate #if	defined(_SYSCALL32)
600Sstevel@tonic-gate struct ipc_perm32 {
610Sstevel@tonic-gate 	uid32_t		uid;	/* owner's user id */
620Sstevel@tonic-gate 	gid32_t		gid;	/* owner's group id */
630Sstevel@tonic-gate 	uid32_t		cuid;	/* creator's user id */
640Sstevel@tonic-gate 	gid32_t		cgid;	/* creator's group id */
650Sstevel@tonic-gate 	mode32_t	mode;	/* access modes */
660Sstevel@tonic-gate 	uint32_t	seq;	/* slot usage sequence number */
670Sstevel@tonic-gate 	key32_t		key;	/* key */
680Sstevel@tonic-gate 	int32_t		pad[4];	/* reserve area */
690Sstevel@tonic-gate };
700Sstevel@tonic-gate #endif	/* _SYSCALL32 */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  * This is the ipc_perm equivalent used in the xxxid_ds64 structures.
740Sstevel@tonic-gate  * It, like the structures it is used in, is intended only for use in
750Sstevel@tonic-gate  * communication between the kernel and user programs, and has the same
760Sstevel@tonic-gate  * layout across all data models.
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  * The xxxid_ds64 structures rely on ipc_perm64 being a multiple of
790Sstevel@tonic-gate  * 8 bytes so subsequent fields are 64-bit aligned on x86.
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate typedef struct ipc_perm64 {
820Sstevel@tonic-gate 	uid_t	ipcx_uid;	/* owner's user id */
830Sstevel@tonic-gate 	gid_t	ipcx_gid;	/* owner's group id */
840Sstevel@tonic-gate 	uid_t	ipcx_cuid;	/* creator's user id */
850Sstevel@tonic-gate 	gid_t	ipcx_cgid;	/* creator's group id */
860Sstevel@tonic-gate 	mode_t	ipcx_mode;	/* access modes */
870Sstevel@tonic-gate 	key_t	ipcx_key;	/* key */
880Sstevel@tonic-gate 	projid_t ipcx_projid;	/* allocating project id */
890Sstevel@tonic-gate 	zoneid_t ipcx_zoneid;	/* creator's zone id */
900Sstevel@tonic-gate } ipc_perm64_t;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate  * These are versions of xxxid_ds which are intended only for use in
940Sstevel@tonic-gate  * communication between the kernel and user programs, and therefore
950Sstevel@tonic-gate  * have the same layout across all data models.  Omitted are all
960Sstevel@tonic-gate  * implementation-specific fields which would be of no use to user
970Sstevel@tonic-gate  * programs.
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate struct shmid_ds64 {
1000Sstevel@tonic-gate 	ipc_perm64_t	shmx_perm;	/* operation permission struct */
1010Sstevel@tonic-gate 	pid_t		shmx_lpid;	/* pid of last shmop */
1020Sstevel@tonic-gate 	pid_t		shmx_cpid;	/* pid of creator */
1030Sstevel@tonic-gate 	uint64_t	shmx_segsz;	/* size of segment in bytes */
1040Sstevel@tonic-gate 	uint64_t	shmx_nattch;	/* # of attaches */
1050Sstevel@tonic-gate 	uint64_t	shmx_cnattch;	/* # of ISM attaches */
1060Sstevel@tonic-gate 	uint64_t	shmx_lkcnt;	/* lock count ??? */
1070Sstevel@tonic-gate 	ipc_time_t	shmx_atime;	/* last shmat time */
1080Sstevel@tonic-gate 	ipc_time_t	shmx_dtime;	/* last shmdt time */
1090Sstevel@tonic-gate 	ipc_time_t	shmx_ctime;	/* last change time */
1100Sstevel@tonic-gate };
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate struct semid_ds64 {
1130Sstevel@tonic-gate 	ipc_perm64_t	semx_perm;	/* operation permission struct */
1140Sstevel@tonic-gate 	ushort_t	semx_nsems;	/* # of semaphores in set */
1150Sstevel@tonic-gate 	ushort_t	_semx_pad[3];	/* pad to 8-byte multiple */
1160Sstevel@tonic-gate 	ipc_time_t	semx_otime;	/* last semop time */
1170Sstevel@tonic-gate 	ipc_time_t	semx_ctime;	/* last change time */
1180Sstevel@tonic-gate };
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate struct msqid_ds64 {
1210Sstevel@tonic-gate 	ipc_perm64_t	msgx_perm;	/* operation permission struct */
1220Sstevel@tonic-gate 	uint64_t	msgx_cbytes;	/* current # bytes on q */
1230Sstevel@tonic-gate 	uint64_t	msgx_qnum;	/* # of messages on q */
1240Sstevel@tonic-gate 	uint64_t	msgx_qbytes;	/* max # of bytes on q */
1250Sstevel@tonic-gate 	pid_t		msgx_lspid;	/* pid of last msgsnd */
1260Sstevel@tonic-gate 	pid_t		msgx_lrpid;	/* pid of last msgrcv */
1270Sstevel@tonic-gate 	ipc_time_t	msgx_stime;	/* last msgsnd time */
1280Sstevel@tonic-gate 	ipc_time_t	msgx_rtime;	/* last msgrcv time */
1290Sstevel@tonic-gate 	ipc_time_t	msgx_ctime;	/* last change time */
1300Sstevel@tonic-gate };
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate #ifdef _KERNEL
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate  * Implementation macros
1360Sstevel@tonic-gate  */
1370Sstevel@tonic-gate #define	IPC_FREE(x)	(((x)->ipc_mode & IPC_ALLOC) == 0)
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate #define	IPC_SEQ_BITS	7
1400Sstevel@tonic-gate #define	IPC_SEQ_MASK	((1 << IPC_SEQ_BITS) - 1)
1410Sstevel@tonic-gate #define	IPC_SEQ_SHIFT	(31 - IPC_SEQ_BITS)
1420Sstevel@tonic-gate #define	IPC_INDEX_MASK	((1 << IPC_SEQ_SHIFT) - 1)
1430Sstevel@tonic-gate #define	IPC_SEQ(x)	((unsigned int)(x) >> IPC_SEQ_SHIFT)
1440Sstevel@tonic-gate #define	IPC_INDEX(x)	((unsigned int)(x) & IPC_INDEX_MASK)
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate #define	IPC_IDS_MIN	(PAGESIZE / 64)		/* starting # of entries */
1470Sstevel@tonic-gate #define	IPC_IDS_MAX	(1 << IPC_SEQ_SHIFT)	/* maximum # of entries */
1480Sstevel@tonic-gate #define	IPC_ID_INVAL	UINT_MAX
1490Sstevel@tonic-gate 
150*2677Sml93401 #define	IPC_PROJ_USAGE(p, s) \
151*2677Sml93401 	(*(rctl_qty_t *)(((char *)&p->ipc_proj->kpj_data.kpd_ipc) + \
152*2677Sml93401 	s->ipcs_rctlofs))
153*2677Sml93401 #define	IPC_ZONE_USAGE(p, s) \
154*2677Sml93401 	(*(rctl_qty_t *)(((char *)&p->ipc_zone->zone_ipc) + \
155*2677Sml93401 	s->ipcs_rctlofs))
1560Sstevel@tonic-gate #define	IPC_LOCKED(s, o) \
1570Sstevel@tonic-gate 	MUTEX_HELD(&s->ipcs_table[IPC_INDEX(o->ipc_id)].ipct_lock)
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate /*
1600Sstevel@tonic-gate  * The kernel's ipc_perm structure.
1610Sstevel@tonic-gate  */
1620Sstevel@tonic-gate typedef struct kipc_perm {
1630Sstevel@tonic-gate 	avl_node_t ipc_avl;	/* avl node if key is non-private */
1640Sstevel@tonic-gate 	list_node_t ipc_list;	/* list node in list of all ids */
1650Sstevel@tonic-gate 	uint_t	ipc_ref;	/* reference count		*/
1660Sstevel@tonic-gate 	uid_t	ipc_uid;	/* owner's user id		*/
1670Sstevel@tonic-gate 	gid_t	ipc_gid;	/* owner's group id		*/
1680Sstevel@tonic-gate 	uid_t	ipc_cuid;	/* creator's user id		*/
1690Sstevel@tonic-gate 	gid_t	ipc_cgid;	/* creator's group id		*/
1700Sstevel@tonic-gate 	mode_t	ipc_mode;	/* access modes			*/
1710Sstevel@tonic-gate 	key_t	ipc_key;	/* key				*/
1720Sstevel@tonic-gate 	kproject_t *ipc_proj;	/* creator's project		*/
1730Sstevel@tonic-gate 	uint_t	ipc_id;		/* id				*/
1740Sstevel@tonic-gate 	zoneid_t ipc_zoneid;	/* creator's zone id		*/
175*2677Sml93401 	zone_t	*ipc_zone;	/* creator's zone		*/
1760Sstevel@tonic-gate } kipc_perm_t;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate typedef struct ipc_slot {
1790Sstevel@tonic-gate 	kmutex_t	ipct_lock;	/* bucket lock		*/
1800Sstevel@tonic-gate 	kipc_perm_t	*ipct_data;	/* data			*/
1810Sstevel@tonic-gate 	uint_t		ipct_seq;	/* sequence number	*/
1820Sstevel@tonic-gate 	struct ipc_slot	*ipct_chain;	/* for stale arrays	*/
1830Sstevel@tonic-gate 	char		ipct_pad[64 - sizeof (kmutex_t) - 3 * sizeof (void *)];
1840Sstevel@tonic-gate } ipc_slot_t;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate typedef void(ipc_func_t)(kipc_perm_t *);
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate typedef struct ipc_service {
1890Sstevel@tonic-gate 	kmutex_t	ipcs_lock;	/* lock for (de)allocation, keys */
1900Sstevel@tonic-gate 	avl_tree_t	ipcs_keys;	/* objects sorted by key	*/
1910Sstevel@tonic-gate 	ipc_slot_t	*ipcs_table;	/* table of objects		*/
1920Sstevel@tonic-gate 	uint_t		ipcs_tabsz;	/* size of table		*/
1930Sstevel@tonic-gate 	uint_t		ipcs_count;	/* # of objects allocated	*/
194*2677Sml93401 	rctl_hndl_t	ipcs_proj_rctl;	/* id limiting rctl handle	*/
195*2677Sml93401 	rctl_hndl_t	ipcs_zone_rctl;	/* id limiting rctl handle	*/
1960Sstevel@tonic-gate 	size_t		ipcs_rctlofs;	/* offset in kproject_data_t	*/
1970Sstevel@tonic-gate 	id_space_t	*ipcs_ids;	/* id space for objects		*/
1980Sstevel@tonic-gate 	size_t		ipcs_ssize;	/* object size (for allocation)	*/
1990Sstevel@tonic-gate 	ipc_func_t	*ipcs_dtor;	/* object destructor		*/
2000Sstevel@tonic-gate 	ipc_func_t	*ipcs_rmid;	/* object removal		*/
2010Sstevel@tonic-gate 	list_t		ipcs_usedids;	/* list of allocated ids	*/
2020Sstevel@tonic-gate 	int		ipcs_atype;	/* audit type (see c2/audit.h)	*/
2030Sstevel@tonic-gate } ipc_service_t;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate int ipcperm_access(kipc_perm_t *, int, cred_t *);
2060Sstevel@tonic-gate int ipcperm_set(ipc_service_t *, struct cred *, kipc_perm_t *,
2070Sstevel@tonic-gate     struct ipc_perm *, model_t);
2080Sstevel@tonic-gate void ipcperm_stat(struct ipc_perm *, kipc_perm_t *, model_t);
2090Sstevel@tonic-gate int ipcperm_set64(ipc_service_t *, struct cred *, kipc_perm_t *,
2100Sstevel@tonic-gate     ipc_perm64_t *);
2110Sstevel@tonic-gate void ipcperm_stat64(ipc_perm64_t *, kipc_perm_t *);
2120Sstevel@tonic-gate 
213*2677Sml93401 ipc_service_t *ipcs_create(const char *, rctl_hndl_t, rctl_hndl_t, size_t,
214*2677Sml93401     ipc_func_t *, ipc_func_t *, int, size_t);
2150Sstevel@tonic-gate void ipcs_destroy(ipc_service_t *);
2160Sstevel@tonic-gate void ipcs_lock(ipc_service_t *);
2170Sstevel@tonic-gate void ipcs_unlock(ipc_service_t *);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate kmutex_t *ipc_lock(ipc_service_t *, int);
2200Sstevel@tonic-gate kmutex_t *ipc_relock(ipc_service_t *, int, kmutex_t *);
2210Sstevel@tonic-gate kmutex_t *ipc_lookup(ipc_service_t *, int, kipc_perm_t **);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate void ipc_hold(ipc_service_t *, kipc_perm_t *);
2240Sstevel@tonic-gate void ipc_rele(ipc_service_t *, kipc_perm_t *);
2250Sstevel@tonic-gate void ipc_rele_locked(ipc_service_t *, kipc_perm_t *);
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate int ipc_get(ipc_service_t *, key_t, int, kipc_perm_t **, kmutex_t **);
2280Sstevel@tonic-gate int ipc_commit_begin(ipc_service_t *, key_t, int, kipc_perm_t *);
2290Sstevel@tonic-gate kmutex_t *ipc_commit_end(ipc_service_t *, kipc_perm_t *);
2300Sstevel@tonic-gate void ipc_cleanup(ipc_service_t *, kipc_perm_t *);
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate int ipc_rmid(ipc_service_t *, int, cred_t *);
2330Sstevel@tonic-gate int ipc_ids(ipc_service_t *, int *, uint_t, uint_t *);
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate void ipc_remove_zone(ipc_service_t *, zoneid_t);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate #else	/* _KERNEL */
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate int msgctl64(int, int, struct msqid_ds64 *);
2400Sstevel@tonic-gate int semctl64(int, int, int, ...);
2410Sstevel@tonic-gate int shmctl64(int, int, struct shmid_ds64 *);
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate #endif	/* _KERNEL */
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate #ifdef	__cplusplus
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate #endif
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate #endif	/* _IPC_IMPL_H */
251