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