1/*	$NetBSD: shm.h,v 1.15 1994/06/29 06:45:17 cgd Exp $	*/
  2
  3/*-
  4 * SPDX-License-Identifier: BSD-4-Clause
  5 *
  6 * Copyright (c) 1994 Adam Glass
  7 * All rights reserved.
  8 *
  9 * Redistribution and use in source and binary forms, with or without
 10 * modification, are permitted provided that the following conditions
 11 * are met:
 12 * 1. Redistributions of source code must retain the above copyright
 13 *    notice, this list of conditions and the following disclaimer.
 14 * 2. Redistributions in binary form must reproduce the above copyright
 15 *    notice, this list of conditions and the following disclaimer in the
 16 *    documentation and/or other materials provided with the distribution.
 17 * 3. All advertising materials mentioning features or use of this software
 18 *    must display the following acknowledgement:
 19 *      This product includes software developed by Adam Glass.
 20 * 4. The name of the author may not be used to endorse or promote products
 21 *    derived from this software without specific prior written permission
 22 *
 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 33 */
 34
 35/*
 36 * As defined+described in "X/Open System Interfaces and Headers"
 37 *                         Issue 4, p. XXX
 38 */
 39
 40#ifndef _SYS_SHM_H_
 41#define _SYS_SHM_H_
 42
 43#include <sys/cdefs.h>
 44#ifdef _WANT_SYSVSHM_INTERNALS
 45#define	_WANT_SYSVIPC_INTERNALS
 46#endif
 47#include <sys/ipc.h>
 48#include <sys/_types.h>
 49
 50#include <machine/param.h>
 51
 52#define SHM_RDONLY  010000  /* Attach read-only (else read-write) */
 53#define SHM_RND     020000  /* Round attach address to SHMLBA */
 54#define	SHM_REMAP   030000  /* Unmap before mapping */
 55#define SHMLBA      PAGE_SIZE /* Segment low boundary address multiple */
 56
 57/* "official" access mode definitions; somewhat braindead since you have
 58   to specify (SHM_* >> 3) for group and (SHM_* >> 6) for world permissions */
 59#define SHM_R       (IPC_R)
 60#define SHM_W       (IPC_W)
 61
 62/* predefine tbd *LOCK shmctl commands */
 63#define	SHM_LOCK	11
 64#define	SHM_UNLOCK	12
 65
 66/* ipcs shmctl commands for Linux compatibility */
 67#define	SHM_STAT	13
 68#define	SHM_INFO	14
 69
 70#ifndef _PID_T_DECLARED
 71typedef	__pid_t		pid_t;
 72#define	_PID_T_DECLARED
 73#endif
 74
 75#ifndef _TIME_T_DECLARED
 76typedef	__time_t	time_t;
 77#define	_TIME_T_DECLARED
 78#endif
 79
 80#ifndef _SIZE_T_DECLARED
 81typedef	__size_t	size_t;
 82#define	_SIZE_T_DECLARED
 83#endif
 84
 85#if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
 86    defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
 87struct shmid_ds_old {
 88	struct ipc_perm_old shm_perm;	/* operation permission structure */
 89	int             shm_segsz;	/* size of segment in bytes */
 90	pid_t           shm_lpid;   /* process ID of last shared memory op */
 91	pid_t           shm_cpid;	/* process ID of creator */
 92	short		shm_nattch;	/* number of current attaches */
 93	time_t          shm_atime;	/* time of last shmat() */
 94	time_t          shm_dtime;	/* time of last shmdt() */
 95	time_t          shm_ctime;	/* time of last change by shmctl() */
 96	void           *shm_internal;   /* sysv stupidity */
 97};
 98#endif
 99
100typedef unsigned int shmatt_t;
101
102struct shmid_ds {
103	struct ipc_perm shm_perm;	/* operation permission structure */
104	size_t          shm_segsz;	/* size of segment in bytes */
105	pid_t           shm_lpid;   /* process ID of last shared memory op */
106	pid_t           shm_cpid;	/* process ID of creator */
107	shmatt_t        shm_nattch;	/* number of current attaches */
108	time_t          shm_atime;	/* time of last shmat() */
109	time_t          shm_dtime;	/* time of last shmdt() */
110	time_t          shm_ctime;	/* time of last change by shmctl() */
111};
112
113#if defined(_KERNEL) || defined(_WANT_SYSVSHM_INTERNALS)
114/*
115 * System 5 style catch-all structure for shared memory constants that
116 * might be of interest to user programs.  Do we really want/need this?
117 */
118struct shminfo {
119	u_long	shmmax;		/* max shared memory segment size (bytes) */
120	u_long	shmmin;		/* max shared memory segment size (bytes) */
121	u_long	shmmni;		/* max number of shared memory identifiers */
122	u_long	shmseg;		/* max shared memory segments per process */
123	u_long	shmall;		/* max amount of shared memory (pages) */
124};
125
126struct vm_object;
127
128/* 
129 * Add a kernel wrapper to the shmid_ds struct so that private info (like the
130 * MAC label) can be added to it, without changing the user interface.
131 */
132struct shmid_kernel {
133	struct shmid_ds u;
134	struct vm_object *object;
135	struct label *label;	/* MAC label */
136	struct ucred *cred;	/* creator's credendials */
137};
138#endif
139
140struct shm_info {
141	int used_ids;
142	unsigned long shm_tot;
143	unsigned long shm_rss;
144	unsigned long shm_swp;
145	unsigned long swap_attempts;
146	unsigned long swap_successes;
147};
148
149#ifdef _KERNEL
150struct proc;
151struct vmspace;
152struct vm_object;
153
154extern struct shminfo	shminfo;
155
156#define	SHMSEG_FREE     	0x0200
157#define	SHMSEG_REMOVED  	0x0400
158#define	SHMSEG_ALLOCATED	0x0800
159
160void	shmexit(struct vmspace *);
161void	shmfork(struct proc *, struct proc *);
162void	shmobjinfo(struct vm_object *obj, key_t *key, unsigned short *seq);
163int	kern_get_shmsegs(struct thread *td, struct shmid_kernel **res,
164	    size_t *sz);
165
166#else /* !_KERNEL */
167
168#include <sys/cdefs.h>
169
170#ifndef _SIZE_T_DECLARED
171typedef __size_t        size_t;
172#define _SIZE_T_DECLARED
173#endif
174
175__BEGIN_DECLS
176#if __BSD_VISIBLE
177int shmsys(int, ...);
178#endif
179void *shmat(int, const void *, int);
180int shmget(key_t, size_t, int);
181int shmctl(int, int, struct shmid_ds *);
182int shmdt(const void *);
183__END_DECLS
184
185#endif /* _KERNEL */
186
187#endif /* !_SYS_SHM_H_ */