1/*	$NetBSD: shm.h,v 1.55 2021/08/17 22:00:32 andvar Exp $	*/
  2
  3/*-
  4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
  5 * All rights reserved.
  6 *
  7 * This code is derived from software contributed to The NetBSD Foundation
  8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
  9 * NASA Ames Research Center.
 10 *
 11 * Redistribution and use in source and binary forms, with or without
 12 * modification, are permitted provided that the following conditions
 13 * are met:
 14 * 1. Redistributions of source code must retain the above copyright
 15 *    notice, this list of conditions and the following disclaimer.
 16 * 2. Redistributions in binary form must reproduce the above copyright
 17 *    notice, this list of conditions and the following disclaimer in the
 18 *    documentation and/or other materials provided with the distribution.
 19 *
 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30 * POSSIBILITY OF SUCH DAMAGE.
 31 */
 32
 33/*
 34 * Copyright (c) 1994 Adam Glass
 35 * All rights reserved.
 36 *
 37 * Redistribution and use in source and binary forms, with or without
 38 * modification, are permitted provided that the following conditions
 39 * are met:
 40 * 1. Redistributions of source code must retain the above copyright
 41 *    notice, this list of conditions and the following disclaimer.
 42 * 2. Redistributions in binary form must reproduce the above copyright
 43 *    notice, this list of conditions and the following disclaimer in the
 44 *    documentation and/or other materials provided with the distribution.
 45 * 3. All advertising materials mentioning features or use of this software
 46 *    must display the following acknowledgement:
 47 *      This product includes software developed by Adam Glass.
 48 * 4. The name of the author may not be used to endorse or promote products
 49 *    derived from this software without specific prior written permission
 50 *
 51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 54 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 55 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 60 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 61 */
 62
 63/*
 64 * As defined+described in "X/Open System Interfaces and Headers"
 65 *                         Issue 4, p. XXX
 66 */
 67
 68#ifndef _SYS_SHM_H_
 69#define _SYS_SHM_H_
 70
 71#include <sys/cdefs.h>
 72#include <sys/featuretest.h>
 73
 74#include <sys/ipc.h>
 75
 76#define	SHM_RDONLY	010000	/* Attach read-only (else read-write) */
 77#define	SHM_RND		020000	/* Round attach address to SHMLBA */
 78#ifdef _KERNEL
 79#define _SHM_RMLINGER	040000	/* Attach even if segment removed */
 80#endif
 81
 82/* Segment low boundary address multiple */
 83#if defined(_KERNEL) || defined(_STANDALONE) || defined(_MODULE)
 84#define	SHMLBA		PAGE_SIZE
 85#else
 86/*
 87 * SHMLBA uses libc's internal __sysconf() to retrieve the machine's
 88 * page size. The value of _SC_PAGESIZE is 28 -- we hard code it so we do not
 89 * need to include unistd.h
 90 */
 91__BEGIN_DECLS
 92long __sysconf(int);
 93__END_DECLS
 94#define	SHMLBA		(__sysconf(28))
 95#endif
 96
 97typedef unsigned int	shmatt_t;
 98
 99struct shmid_ds {
100	struct ipc_perm	shm_perm;	/* operation permission structure */
101	size_t		shm_segsz;	/* size of segment in bytes */
102	pid_t		shm_lpid;	/* process ID of last shm operation */
103	pid_t		shm_cpid;	/* process ID of creator */
104	shmatt_t	shm_nattch;	/* number of current attaches */
105	time_t		shm_atime;	/* time of last shmat() */
106	time_t		shm_dtime;	/* time of last shmdt() */
107	time_t		shm_ctime;	/* time of last change by shmctl() */
108
109	/*
110	 * These members are private and used only in the internal
111	 * implementation of this interface.
112	 */
113	void		*_shm_internal;
114};
115
116#if defined(_NETBSD_SOURCE)
117/*
118 * Some systems (e.g. HP-UX) take these as the second (cmd) arg to shmctl().
119 */
120#define	SHM_LOCK	3	/* Lock segment in memory. */
121#define	SHM_UNLOCK	4	/* Unlock a segment locked by SHM_LOCK. */
122#endif /* _NETBSD_SOURCE */
123
124#if defined(_NETBSD_SOURCE)
125/*
126 * Permission definitions used in shmflag arguments to shmat(2) and shmget(2).
127 * Provided for source compatibility only; do not use in new code!
128 */
129#define	SHM_R		IPC_R	/* S_IRUSR, R for owner */
130#define	SHM_W		IPC_W	/* S_IWUSR, W for owner */
131
132/*
133 * System 5 style catch-all structure for shared memory constants that
134 * might be of interest to user programs.  Do we really want/need this?
135 */
136struct shminfo {
137	uint64_t	shmmax;	/* max shared memory segment size (bytes) */
138	uint32_t	shmmin;	/* min shared memory segment size (bytes) */
139	uint32_t	shmmni;	/* max number of shared memory identifiers */
140	uint32_t	shmseg;	/* max shared memory segments per process */
141	uint32_t	shmall;	/* max amount of shared memory (pages) */
142};
143
144/* Warning: 64-bit structure padding is needed here */
145struct shmid_ds_sysctl {
146	struct		ipc_perm_sysctl shm_perm;
147	uint64_t	shm_segsz;
148	pid_t		shm_lpid;
149	pid_t		shm_cpid;
150	time_t		shm_atime;
151	time_t		shm_dtime;
152	time_t		shm_ctime;
153	uint32_t	shm_nattch;
154};
155struct shm_sysctl_info {
156	struct	shminfo shminfo;
157	struct	shmid_ds_sysctl shmids[1];
158};
159#endif /* _NETBSD_SOURCE */
160
161#ifdef _KERNEL
162extern struct shminfo shminfo;
163extern struct shmid_ds *shmsegs;
164extern int shm_nused;
165
166#define	SHMSEG_FREE		0x0200
167#define	SHMSEG_REMOVED		0x0400
168#define	SHMSEG_ALLOCATED	0x0800
169#define	SHMSEG_WANTED		0x1000
170#define	SHMSEG_RMLINGER		0x2000
171#define	SHMSEG_WIRED		0x4000
172
173struct vmspace;
174
175int	shminit(void);
176int	shmfini(void);
177void	shmfork(struct vmspace *, struct vmspace *);
178void	shmexit(struct vmspace *);
179int	shmctl1(struct lwp *, int, int, struct shmid_ds *);
180
181int	shm_find_segment_perm_by_index(int, struct ipc_perm *);
182
183extern void (*uvm_shmexit)(struct vmspace *);
184extern void (*uvm_shmfork)(struct vmspace *, struct vmspace *);
185
186#define SYSCTL_FILL_SHM(src, dst) do { \
187	SYSCTL_FILL_PERM((src).shm_perm, (dst).shm_perm); \
188	(dst).shm_segsz = (src).shm_segsz; \
189	(dst).shm_lpid = (src).shm_lpid; \
190	(dst).shm_cpid = (src).shm_cpid; \
191	(dst).shm_atime = (src).shm_atime; \
192	(dst).shm_dtime = (src).shm_dtime; \
193	(dst).shm_ctime = (src).shm_ctime; \
194	(dst).shm_nattch = (src).shm_nattch; \
195} while (/*CONSTCOND*/ 0)
196
197#else /* !_KERNEL */
198
199__BEGIN_DECLS
200void	*shmat(int, const void *, int);
201int	shmctl(int, int, struct shmid_ds *) __RENAME(__shmctl50);
202int	shmdt(const void *);
203int	shmget(key_t, size_t, int);
204__END_DECLS
205
206#endif /* !_KERNEL */
207
208#endif /* !_SYS_SHM_H_ */