1 /* $NetBSD: linux_ipccall.c,v 1.38 2026/09/20 13:43:51 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Frank van der Linden and Eric Haszlakiewicz. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: linux_ipccall.c,v 1.38 2026/09/20 13:43:51 riastradh Exp $"); 34 35 #if defined(_KERNEL_OPT) 36 #include "opt_sysv.h" 37 #endif 38 39 #include <sys/param.h> 40 #include <sys/shm.h> 41 #include <sys/sem.h> 42 #include <sys/msg.h> 43 #include <sys/proc.h> 44 #include <sys/systm.h> 45 46 /* real syscalls */ 47 #include <sys/mount.h> 48 #include <sys/syscallargs.h> 49 50 /* sys_ipc + args prototype */ 51 #include <compat/linux/common/linux_types.h> 52 #include <compat/linux/common/linux_signal.h> 53 54 #include <compat/linux/linux_syscallargs.h> 55 #include <compat/linux/linux_syscall.h> 56 57 /* general ipc defines */ 58 #include <compat/linux/common/linux_ipc.h> 59 60 /* prototypes for real/normal linux-emul syscalls */ 61 #include <compat/linux/common/linux_msg.h> 62 #include <compat/linux/common/linux_shm.h> 63 #include <compat/linux/common/linux_sem.h> 64 65 /* prototypes for sys_ipc stuff */ 66 #include <compat/linux/common/linux_ipccall.h> 67 68 /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */ 69 /* Not used on: aarch64, alpha */ 70 71 /* 72 * Stuff to deal with the SysV ipc/shm/semaphore interface in Linux. 73 * The main difference is, that Linux handles it all via one 74 * system call, which has the usual maximum amount of 5 arguments. 75 * This results in a kludge for calls that take 6 of them. 76 * 77 * The SYSV??? options have to be enabled to get the appropriate 78 * functions to work. 79 */ 80 81 int 82 linux_sys_ipc(struct lwp *l, const struct linux_sys_ipc_args *uap, register_t *retval) 83 { 84 /* { 85 syscallarg(int) what; 86 syscallarg(int) a1; 87 syscallarg(int) a2; 88 syscallarg(int) a3; 89 syscallarg(void *) ptr; 90 } */ 91 92 switch (SCARG(uap, what)) { 93 #ifdef SYSVSEM 94 case LINUX_SYS_SEMOP: 95 return linux_semop(l, uap, retval); 96 case LINUX_SYS_SEMGET: 97 return linux_semget(l, uap, retval); 98 case LINUX_SYS_SEMCTL: { 99 struct linux_sys_semctl_args bsa; 100 union linux_semun arg; 101 int error; 102 103 memset(&bsa, 0, sizeof(bsa)); 104 SCARG(&bsa, semid) = SCARG(uap, a1); 105 SCARG(&bsa, semnum) = SCARG(uap, a2); 106 SCARG(&bsa, cmd) = SCARG(uap, a3); 107 /* Convert from (union linux_semun *) to (union linux_semun) */ 108 if ((error = copyin(SCARG(uap, ptr), &arg, sizeof arg))) 109 return error; 110 SCARG(&bsa, arg) = arg; 111 112 return linux_sys_semctl(l, &bsa, retval); 113 } 114 #ifdef LINUX_SYS_semtimedop 115 case LINUX_SYS_SEMTIMEDOP: { 116 struct linux_sys_semtimedop_args ua; 117 118 ua.semid = SCARG(uap, a1); 119 ua.sops = (struct sembuf *)SCARG(uap, a2); 120 ua.nsops = (size_t)SCARG(uap, a3); 121 ua.timeout = (struct linux_timespec *)SCARG(uap, ptr); 122 123 return linux_sys_semtimedop(l, &ua, retval); 124 } 125 #endif 126 #endif 127 #ifdef SYSVMSG 128 case LINUX_SYS_MSGSND: 129 return linux_msgsnd(l, uap, retval); 130 case LINUX_SYS_MSGRCV: 131 return linux_msgrcv(l, uap, retval); 132 case LINUX_SYS_MSGGET: 133 return linux_msgget(l, uap, retval); 134 case LINUX_SYS_MSGCTL: { 135 struct linux_sys_msgctl_args bsa; 136 137 memset(&bsa, 0, sizeof(bsa)); 138 SCARG(&bsa, msqid) = SCARG(uap, a1); 139 SCARG(&bsa, cmd) = SCARG(uap, a2); 140 SCARG(&bsa, buf) = (struct linux_msqid_ds *)SCARG(uap, ptr); 141 142 return linux_sys_msgctl(l, &bsa, retval); 143 } 144 #endif 145 #ifdef SYSVSHM 146 case LINUX_SYS_SHMAT: { 147 struct linux_sys_shmat_args bsa; 148 149 memset(&bsa, 0, sizeof(bsa)); 150 SCARG(&bsa, shmid) = SCARG(uap, a1); 151 SCARG(&bsa, shmaddr) = (void *)SCARG(uap, ptr); 152 SCARG(&bsa, shmflg) = SCARG(uap, a2); 153 /* XXX passing pointer inside int here */ 154 SCARG(&bsa, raddr) = (u_long *)SCARG(uap, a3); 155 156 return linux_sys_shmat(l, &bsa, retval); 157 } 158 case LINUX_SYS_SHMDT: 159 return linux_shmdt(l, uap, retval); 160 case LINUX_SYS_SHMGET: 161 return linux_shmget(l, uap, retval); 162 case LINUX_SYS_SHMCTL: { 163 struct linux_sys_shmctl_args bsa; 164 165 memset(&bsa, 0, sizeof(bsa)); 166 SCARG(&bsa, shmid) = SCARG(uap, a1); 167 SCARG(&bsa, cmd) = SCARG(uap, a2); 168 SCARG(&bsa, buf) = (struct linux_shmid_ds *)SCARG(uap, ptr); 169 170 return linux_sys_shmctl(l, &bsa, retval); 171 } 172 #endif 173 default: 174 return ENOSYS; 175 } 176 } 177 178 #ifdef SYSVSEM 179 int 180 linux_semop(struct lwp *l, const struct linux_sys_ipc_args *uap, register_t *retval) 181 { 182 /* { 183 syscallarg(int) what; 184 syscallarg(int) a1; 185 syscallarg(int) a2; 186 syscallarg(int) a3; 187 syscallarg(void *) ptr; 188 } */ 189 struct sys_semop_args bsa; 190 191 memset(&bsa, 0, sizeof(bsa)); 192 SCARG(&bsa, semid) = SCARG(uap, a1); 193 SCARG(&bsa, sops) = (struct sembuf *)SCARG(uap, ptr); 194 SCARG(&bsa, nsops) = SCARG(uap, a2); 195 196 return sys_semop(l, &bsa, retval); 197 } 198 199 int 200 linux_semget(struct lwp *l, const struct linux_sys_ipc_args *uap, register_t *retval) 201 { 202 /* { 203 syscallarg(int) what; 204 syscallarg(int) a1; 205 syscallarg(int) a2; 206 syscallarg(int) a3; 207 syscallarg(void *) ptr; 208 } */ 209 struct sys_semget_args bsa; 210 211 memset(&bsa, 0, sizeof(bsa)); 212 SCARG(&bsa, key) = (key_t)SCARG(uap, a1); 213 SCARG(&bsa, nsems) = SCARG(uap, a2); 214 SCARG(&bsa, semflg) = SCARG(uap, a3); 215 216 return sys_semget(l, &bsa, retval); 217 } 218 219 #endif /* SYSVSEM */ 220 221 #ifdef SYSVMSG 222 223 int 224 linux_msgsnd(struct lwp *l, const struct linux_sys_ipc_args *uap, register_t *retval) 225 { 226 struct sys_msgsnd_args bma; 227 228 memset(&bma, 0, sizeof(bma)); 229 SCARG(&bma, msqid) = SCARG(uap, a1); 230 SCARG(&bma, msgp) = SCARG(uap, ptr); 231 SCARG(&bma, msgsz) = SCARG(uap, a2); 232 SCARG(&bma, msgflg) = SCARG(uap, a3); 233 234 return sys_msgsnd(l, &bma, retval); 235 } 236 237 int 238 linux_msgrcv(struct lwp *l, const struct linux_sys_ipc_args *uap, register_t *retval) 239 { 240 struct sys_msgrcv_args bma; 241 struct linux_msgrcv_msgarg kluge; 242 int error; 243 244 if ((error = copyin(SCARG(uap, ptr), &kluge, sizeof kluge))) 245 return error; 246 247 memset(&bma, 0, sizeof(bma)); 248 SCARG(&bma, msqid) = SCARG(uap, a1); 249 SCARG(&bma, msgp) = kluge.msg; 250 SCARG(&bma, msgsz) = SCARG(uap, a2); 251 SCARG(&bma, msgtyp) = kluge.type; 252 SCARG(&bma, msgflg) = SCARG(uap, a3); 253 254 return sys_msgrcv(l, &bma, retval); 255 } 256 257 int 258 linux_msgget(struct lwp *l, const struct linux_sys_ipc_args *uap, register_t *retval) 259 { 260 struct sys_msgget_args bma; 261 262 memset(&bma, 0, sizeof(bma)); 263 SCARG(&bma, key) = (key_t)SCARG(uap, a1); 264 SCARG(&bma, msgflg) = SCARG(uap, a2); 265 266 return sys_msgget(l, &bma, retval); 267 } 268 269 #endif /* SYSVMSG */ 270 271 #ifdef SYSVSHM 272 /* 273 * shmdt(): this could have been mapped directly, if it wasn't for 274 * the extra indirection by the linux_ipc system call. 275 */ 276 int 277 linux_shmdt(struct lwp *l, const struct linux_sys_ipc_args *uap, register_t *retval) 278 { 279 struct sys_shmdt_args bsa; 280 281 memset(&bsa, 0, sizeof(bsa)); 282 SCARG(&bsa, shmaddr) = SCARG(uap, ptr); 283 284 return sys_shmdt(l, &bsa, retval); 285 } 286 287 /* 288 * Same story as shmdt. 289 */ 290 int 291 linux_shmget(struct lwp *l, const struct linux_sys_ipc_args *uap, register_t *retval) 292 { 293 struct linux_sys_shmget_args bsa; 294 295 memset(&bsa, 0, sizeof(bsa)); 296 SCARG(&bsa, key) = SCARG(uap, a1); 297 SCARG(&bsa, size) = SCARG(uap, a2); 298 SCARG(&bsa, shmflg) = SCARG(uap, a3); 299 300 return linux_sys_shmget(l, &bsa, retval); 301 } 302 303 #endif /* SYSVSHM */ 304