xref: /onnv-gate/usr/src/uts/common/sys/ipc_impl.h (revision 13096:b02331b7b26d)
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
52677Sml93401  * Common Development and Distribution License (the "License").
62677Sml93401  * 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*13096SJordan.Vaughan@Sun.com  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #ifndef	_IPC_IMPL_H
260Sstevel@tonic-gate #define	_IPC_IMPL_H
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/ipc.h>
300Sstevel@tonic-gate #include <sys/mutex.h>
312677Sml93401 #include <sys/ipc_rctl.h>
320Sstevel@tonic-gate #include <sys/project.h>
332677Sml93401 #include <sys/zone.h>
340Sstevel@tonic-gate #include <sys/sysmacros.h>
350Sstevel@tonic-gate #include <sys/avl.h>
360Sstevel@tonic-gate #include <sys/id_space.h>
370Sstevel@tonic-gate #include <sys/cred.h>
380Sstevel@tonic-gate #include <sys/list.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #ifdef	__cplusplus
410Sstevel@tonic-gate extern "C" {
420Sstevel@tonic-gate #endif
430Sstevel@tonic-gate 
440Sstevel@tonic-gate typedef uint64_t ipc_time_t;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /* For xxxctl64 */
470Sstevel@tonic-gate #define	IPC_SET64	13	/* set options */
480Sstevel@tonic-gate #define	IPC_STAT64	14	/* get options */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * There are two versions of the userland ipc_perm structure:
520Sstevel@tonic-gate  *   ipc_perm     - the version used by user applications and by the kernel
530Sstevel@tonic-gate  *		    when the user and kernel data models match (in ipc.h)
540Sstevel@tonic-gate  *   ipc_perm32   - the 64-bit kernel's view of a 32-bit struct ipc_perm
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate #if	defined(_SYSCALL32)
570Sstevel@tonic-gate struct ipc_perm32 {
580Sstevel@tonic-gate 	uid32_t		uid;	/* owner's user id */
590Sstevel@tonic-gate 	gid32_t		gid;	/* owner's group id */
600Sstevel@tonic-gate 	uid32_t		cuid;	/* creator's user id */
610Sstevel@tonic-gate 	gid32_t		cgid;	/* creator's group id */
620Sstevel@tonic-gate 	mode32_t	mode;	/* access modes */
630Sstevel@tonic-gate 	uint32_t	seq;	/* slot usage sequence number */
640Sstevel@tonic-gate 	key32_t		key;	/* key */
650Sstevel@tonic-gate 	int32_t		pad[4];	/* reserve area */
660Sstevel@tonic-gate };
670Sstevel@tonic-gate #endif	/* _SYSCALL32 */
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * This is the ipc_perm equivalent used in the xxxid_ds64 structures.
710Sstevel@tonic-gate  * It, like the structures it is used in, is intended only for use in
720Sstevel@tonic-gate  * communication between the kernel and user programs, and has the same
730Sstevel@tonic-gate  * layout across all data models.
740Sstevel@tonic-gate  *
750Sstevel@tonic-gate  * The xxxid_ds64 structures rely on ipc_perm64 being a multiple of
760Sstevel@tonic-gate  * 8 bytes so subsequent fields are 64-bit aligned on x86.
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate typedef struct ipc_perm64 {
790Sstevel@tonic-gate 	uid_t	ipcx_uid;	/* owner's user id */
800Sstevel@tonic-gate 	gid_t	ipcx_gid;	/* owner's group id */
810Sstevel@tonic-gate 	uid_t	ipcx_cuid;	/* creator's user id */
820Sstevel@tonic-gate 	gid_t	ipcx_cgid;	/* creator's group id */
830Sstevel@tonic-gate 	mode_t	ipcx_mode;	/* access modes */
840Sstevel@tonic-gate 	key_t	ipcx_key;	/* key */
850Sstevel@tonic-gate 	projid_t ipcx_projid;	/* allocating project id */
860Sstevel@tonic-gate 	zoneid_t ipcx_zoneid;	/* creator's zone id */
870Sstevel@tonic-gate } ipc_perm64_t;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate  * These are versions of xxxid_ds which are intended only for use in
910Sstevel@tonic-gate  * communication between the kernel and user programs, and therefore
920Sstevel@tonic-gate  * have the same layout across all data models.  Omitted are all
930Sstevel@tonic-gate  * implementation-specific fields which would be of no use to user
940Sstevel@tonic-gate  * programs.
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate struct shmid_ds64 {
970Sstevel@tonic-gate 	ipc_perm64_t	shmx_perm;	/* operation permission struct */
980Sstevel@tonic-gate 	pid_t		shmx_lpid;	/* pid of last shmop */
990Sstevel@tonic-gate 	pid_t		shmx_cpid;	/* pid of creator */
1000Sstevel@tonic-gate 	uint64_t	shmx_segsz;	/* size of segment in bytes */
1010Sstevel@tonic-gate 	uint64_t	shmx_nattch;	/* # of attaches */
1020Sstevel@tonic-gate 	uint64_t	shmx_cnattch;	/* # of ISM attaches */
1030Sstevel@tonic-gate 	uint64_t	shmx_lkcnt;	/* lock count ??? */
1040Sstevel@tonic-gate 	ipc_time_t	shmx_atime;	/* last shmat time */
1050Sstevel@tonic-gate 	ipc_time_t	shmx_dtime;	/* last shmdt time */
1060Sstevel@tonic-gate 	ipc_time_t	shmx_ctime;	/* last change time */
1070Sstevel@tonic-gate };
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate struct semid_ds64 {
1100Sstevel@tonic-gate 	ipc_perm64_t	semx_perm;	/* operation permission struct */
1110Sstevel@tonic-gate 	ushort_t	semx_nsems;	/* # of semaphores in set */
1120Sstevel@tonic-gate 	ushort_t	_semx_pad[3];	/* pad to 8-byte multiple */
1130Sstevel@tonic-gate 	ipc_time_t	semx_otime;	/* last semop time */
1140Sstevel@tonic-gate 	ipc_time_t	semx_ctime;	/* last change time */
1150Sstevel@tonic-gate };
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate struct msqid_ds64 {
1180Sstevel@tonic-gate 	ipc_perm64_t	msgx_perm;	/* operation permission struct */
1190Sstevel@tonic-gate 	uint64_t	msgx_cbytes;	/* current # bytes on q */
1200Sstevel@tonic-gate 	uint64_t	msgx_qnum;	/* # of messages on q */
1210Sstevel@tonic-gate 	uint64_t	msgx_qbytes;	/* max # of bytes on q */
1220Sstevel@tonic-gate 	pid_t		msgx_lspid;	/* pid of last msgsnd */
1230Sstevel@tonic-gate 	pid_t		msgx_lrpid;	/* pid of last msgrcv */
1240Sstevel@tonic-gate 	ipc_time_t	msgx_stime;	/* last msgsnd time */
1250Sstevel@tonic-gate 	ipc_time_t	msgx_rtime;	/* last msgrcv time */
1260Sstevel@tonic-gate 	ipc_time_t	msgx_ctime;	/* last change time */
1270Sstevel@tonic-gate };
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate #ifdef _KERNEL
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate  * Implementation macros
1330Sstevel@tonic-gate  */
1340Sstevel@tonic-gate #define	IPC_FREE(x)	(((x)->ipc_mode & IPC_ALLOC) == 0)
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate #define	IPC_SEQ_BITS	7
1370Sstevel@tonic-gate #define	IPC_SEQ_MASK	((1 << IPC_SEQ_BITS) - 1)
1380Sstevel@tonic-gate #define	IPC_SEQ_SHIFT	(31 - IPC_SEQ_BITS)
1390Sstevel@tonic-gate #define	IPC_INDEX_MASK	((1 << IPC_SEQ_SHIFT) - 1)
1400Sstevel@tonic-gate #define	IPC_SEQ(x)	((unsigned int)(x) >> IPC_SEQ_SHIFT)
1410Sstevel@tonic-gate #define	IPC_INDEX(x)	((unsigned int)(x) & IPC_INDEX_MASK)
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate #define	IPC_IDS_MIN	(PAGESIZE / 64)		/* starting # of entries */
1440Sstevel@tonic-gate #define	IPC_IDS_MAX	(1 << IPC_SEQ_SHIFT)	/* maximum # of entries */
1450Sstevel@tonic-gate #define	IPC_ID_INVAL	UINT_MAX
1460Sstevel@tonic-gate 
1472677Sml93401 #define	IPC_PROJ_USAGE(p, s) \
1482677Sml93401 	(*(rctl_qty_t *)(((char *)&p->ipc_proj->kpj_data.kpd_ipc) + \
1492677Sml93401 	s->ipcs_rctlofs))
1502677Sml93401 #define	IPC_ZONE_USAGE(p, s) \
151*13096SJordan.Vaughan@Sun.com 	(*(rctl_qty_t *)(((char *)&p->ipc_zone_ref.zref_zone->zone_ipc) + \
1522677Sml93401 	s->ipcs_rctlofs))
1530Sstevel@tonic-gate #define	IPC_LOCKED(s, o) \
1540Sstevel@tonic-gate 	MUTEX_HELD(&s->ipcs_table[IPC_INDEX(o->ipc_id)].ipct_lock)
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate /*
1570Sstevel@tonic-gate  * The kernel's ipc_perm structure.
1580Sstevel@tonic-gate  */
1590Sstevel@tonic-gate typedef struct kipc_perm {
1600Sstevel@tonic-gate 	avl_node_t ipc_avl;	/* avl node if key is non-private */
1610Sstevel@tonic-gate 	list_node_t ipc_list;	/* list node in list of all ids */
1620Sstevel@tonic-gate 	uint_t	ipc_ref;	/* reference count		*/
1630Sstevel@tonic-gate 	uid_t	ipc_uid;	/* owner's user id		*/
1640Sstevel@tonic-gate 	gid_t	ipc_gid;	/* owner's group id		*/
1650Sstevel@tonic-gate 	uid_t	ipc_cuid;	/* creator's user id		*/
1660Sstevel@tonic-gate 	gid_t	ipc_cgid;	/* creator's group id		*/
1670Sstevel@tonic-gate 	mode_t	ipc_mode;	/* access modes			*/
1680Sstevel@tonic-gate 	key_t	ipc_key;	/* key				*/
1690Sstevel@tonic-gate 	kproject_t *ipc_proj;	/* creator's project		*/
1700Sstevel@tonic-gate 	uint_t	ipc_id;		/* id				*/
1710Sstevel@tonic-gate 	zoneid_t ipc_zoneid;	/* creator's zone id		*/
172*13096SJordan.Vaughan@Sun.com 	zone_ref_t ipc_zone_ref; /* reference to creator's zone */
1730Sstevel@tonic-gate } kipc_perm_t;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate typedef struct ipc_slot {
1760Sstevel@tonic-gate 	kmutex_t	ipct_lock;	/* bucket lock		*/
1770Sstevel@tonic-gate 	kipc_perm_t	*ipct_data;	/* data			*/
1780Sstevel@tonic-gate 	uint_t		ipct_seq;	/* sequence number	*/
1790Sstevel@tonic-gate 	struct ipc_slot	*ipct_chain;	/* for stale arrays	*/
1800Sstevel@tonic-gate 	char		ipct_pad[64 - sizeof (kmutex_t) - 3 * sizeof (void *)];
1810Sstevel@tonic-gate } ipc_slot_t;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate typedef void(ipc_func_t)(kipc_perm_t *);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate typedef struct ipc_service {
1860Sstevel@tonic-gate 	kmutex_t	ipcs_lock;	/* lock for (de)allocation, keys */
1870Sstevel@tonic-gate 	avl_tree_t	ipcs_keys;	/* objects sorted by key	*/
1880Sstevel@tonic-gate 	ipc_slot_t	*ipcs_table;	/* table of objects		*/
1890Sstevel@tonic-gate 	uint_t		ipcs_tabsz;	/* size of table		*/
1900Sstevel@tonic-gate 	uint_t		ipcs_count;	/* # of objects allocated	*/
1912677Sml93401 	rctl_hndl_t	ipcs_proj_rctl;	/* id limiting rctl handle	*/
1922677Sml93401 	rctl_hndl_t	ipcs_zone_rctl;	/* id limiting rctl handle	*/
1930Sstevel@tonic-gate 	size_t		ipcs_rctlofs;	/* offset in kproject_data_t	*/
1940Sstevel@tonic-gate 	id_space_t	*ipcs_ids;	/* id space for objects		*/
1950Sstevel@tonic-gate 	size_t		ipcs_ssize;	/* object size (for allocation)	*/
1960Sstevel@tonic-gate 	ipc_func_t	*ipcs_dtor;	/* object destructor		*/
1970Sstevel@tonic-gate 	ipc_func_t	*ipcs_rmid;	/* object removal		*/
1980Sstevel@tonic-gate 	list_t		ipcs_usedids;	/* list of allocated ids	*/
1990Sstevel@tonic-gate 	int		ipcs_atype;	/* audit type (see c2/audit.h)	*/
2000Sstevel@tonic-gate } ipc_service_t;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate int ipcperm_access(kipc_perm_t *, int, cred_t *);
2030Sstevel@tonic-gate int ipcperm_set(ipc_service_t *, struct cred *, kipc_perm_t *,
2040Sstevel@tonic-gate     struct ipc_perm *, model_t);
2050Sstevel@tonic-gate void ipcperm_stat(struct ipc_perm *, kipc_perm_t *, model_t);
2060Sstevel@tonic-gate int ipcperm_set64(ipc_service_t *, struct cred *, kipc_perm_t *,
2070Sstevel@tonic-gate     ipc_perm64_t *);
2080Sstevel@tonic-gate void ipcperm_stat64(ipc_perm64_t *, kipc_perm_t *);
2090Sstevel@tonic-gate 
2102677Sml93401 ipc_service_t *ipcs_create(const char *, rctl_hndl_t, rctl_hndl_t, size_t,
2112677Sml93401     ipc_func_t *, ipc_func_t *, int, size_t);
2120Sstevel@tonic-gate void ipcs_destroy(ipc_service_t *);
2130Sstevel@tonic-gate void ipcs_lock(ipc_service_t *);
2140Sstevel@tonic-gate void ipcs_unlock(ipc_service_t *);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate kmutex_t *ipc_lock(ipc_service_t *, int);
2170Sstevel@tonic-gate kmutex_t *ipc_relock(ipc_service_t *, int, kmutex_t *);
2180Sstevel@tonic-gate kmutex_t *ipc_lookup(ipc_service_t *, int, kipc_perm_t **);
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate void ipc_hold(ipc_service_t *, kipc_perm_t *);
2210Sstevel@tonic-gate void ipc_rele(ipc_service_t *, kipc_perm_t *);
2220Sstevel@tonic-gate void ipc_rele_locked(ipc_service_t *, kipc_perm_t *);
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate int ipc_get(ipc_service_t *, key_t, int, kipc_perm_t **, kmutex_t **);
2250Sstevel@tonic-gate int ipc_commit_begin(ipc_service_t *, key_t, int, kipc_perm_t *);
2260Sstevel@tonic-gate kmutex_t *ipc_commit_end(ipc_service_t *, kipc_perm_t *);
2270Sstevel@tonic-gate void ipc_cleanup(ipc_service_t *, kipc_perm_t *);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate int ipc_rmid(ipc_service_t *, int, cred_t *);
2300Sstevel@tonic-gate int ipc_ids(ipc_service_t *, int *, uint_t, uint_t *);
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate void ipc_remove_zone(ipc_service_t *, zoneid_t);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate #else	/* _KERNEL */
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate int msgctl64(int, int, struct msqid_ds64 *);
2370Sstevel@tonic-gate int semctl64(int, int, int, ...);
2380Sstevel@tonic-gate int shmctl64(int, int, struct shmid_ds64 *);
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate #endif	/* _KERNEL */
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate #ifdef	__cplusplus
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate #endif
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate #endif	/* _IPC_IMPL_H */
248