1 /*	$NetBSD: netbsd32_compat_10_sysv.c,v 1.3 2021/01/19 03:20:13 simonb Exp $	*/
2 
3 /*
4  * Copyright (c) 1994 Adam Glass and Charles M. Hannum.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Adam Glass and Charles M.
17  *	Hannum.
18  * 4. The names of the authors may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_10_sysv.c,v 1.3 2021/01/19 03:20:13 simonb Exp $");
35 
36 #ifdef _KERNEL_OPT
37 #include "opt_sysv.h"
38 #include "opt_compat_netbsd.h"
39 #endif
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/module.h>
44 #include <sys/kernel.h>
45 #include <sys/proc.h>
46 #include <sys/sem.h>
47 
48 #include <sys/mount.h>
49 #include <sys/syscallargs.h>
50 #include <sys/syscallvar.h>
51 
52 #include <compat/netbsd32/netbsd32.h>
53 #include <compat/netbsd32/netbsd32_syscall.h>
54 #include <compat/netbsd32/netbsd32_syscallargs.h>
55 #include <compat/sys/shm.h>
56 
57 #if defined(COMPAT_10)
58 
59 #if defined(SYSVSEM)
60 int
compat_10_netbsd32_semsys(struct lwp * l,const struct compat_10_netbsd32_semsys_args * uap,register_t * retval)61 compat_10_netbsd32_semsys(struct lwp *l, const struct compat_10_netbsd32_semsys_args *uap, register_t *retval)
62 {
63 	/* {
64 		syscallarg(int) which;
65 		syscallarg(int) a2;
66 		syscallarg(int) a3;
67 		syscallarg(int) a4;
68 		syscallarg(int) a5;
69 	} */
70 	struct compat_50_netbsd32___semctl14_args /* {
71 		syscallarg(int) semid;
72 		syscallarg(int) semnum;
73 		syscallarg(int) cmd;
74 		syscallarg(union semun *) arg;
75 	} */ __semctl_args;
76 	struct sys_semget_args /* {
77 		syscallarg(key_t) key;
78 		syscallarg(int) nsems;
79 		syscallarg(int) semflg;
80 	} */ semget_args;
81 	struct sys_semop_args /* {
82 		syscallarg(int) semid;
83 		syscallarg(struct sembuf *) sops;
84 		syscallarg(u_int) nsops;
85 	} */ semop_args;
86 	struct sys_semconfig_args /* {
87 		syscallarg(int) flag;
88 	} */ semconfig_args;
89 	int a5;
90 
91 	switch (SCARG(uap, which)) {
92 	case 0:						/* __semctl() */
93 		SCARG(&__semctl_args, semid) = SCARG(uap, a2);
94 		SCARG(&__semctl_args, semnum) = SCARG(uap, a3);
95 		SCARG(&__semctl_args, cmd) = SCARG(uap, a4);
96 		a5 = SCARG(uap, a5);
97 		return do_netbsd32___semctl14(l, &__semctl_args, retval, &a5);
98 
99 	case 1:						/* semget() */
100 		SCARG(&semget_args, key) = SCARG(uap, a2);
101 		SCARG(&semget_args, nsems) = SCARG(uap, a3);
102 		SCARG(&semget_args, semflg) = SCARG(uap, a4);
103 		return sys_semget(l, &semget_args, retval);
104 
105 	case 2:						/* semop() */
106 		SCARG(&semop_args, semid) = SCARG(uap, a2);
107 		SCARG(&semop_args, sops) = NETBSD32IPTR64(SCARG(uap, a3));
108 		SCARG(&semop_args, nsops) = SCARG(uap, a4);
109 		return sys_semop(l, &semop_args, retval);
110 
111 	case 3:						/* semconfig() */
112 		SCARG(&semconfig_args, flag) = SCARG(uap, a2);
113 		return sys_semconfig(l, &semconfig_args, retval);
114 
115 	default:
116 		return EINVAL;
117 	}
118 }
119 #endif
120 
121 #if defined(SYSVSHM)
122 int
compat_10_netbsd32_shmsys(struct lwp * l,const struct compat_10_netbsd32_shmsys_args * uap,register_t * retval)123 compat_10_netbsd32_shmsys(struct lwp *l, const struct compat_10_netbsd32_shmsys_args *uap, register_t *retval)
124 {
125 	/* {
126 		syscallarg(int) which;
127 		syscallarg(int) a2;
128 		syscallarg(int) a3;
129 		syscallarg(int) a4;
130 	} */
131 	struct sys_shmat_args /* {
132 		syscallarg(int) shmid;
133 		syscallarg(void *) shmaddr;
134 		syscallarg(int) shmflg;
135 	} */ shmat_args;
136 	struct compat_14_sys_shmctl_args /* {
137 		syscallarg(int) shmid;
138 		syscallarg(int) cmd;
139 		syscallarg(struct shmid_ds50 *) buf;
140 	} */ shmctl_args;
141 	struct sys_shmdt_args /* {
142 		syscallarg(void *) shmaddr;
143 	} */ shmdt_args;
144 	struct sys_shmget_args /* {
145 		syscallarg(key_t) key;
146 		syscallarg(int) size;
147 		syscallarg(int) shmflg;
148 	} */ shmget_args;
149 
150 	switch (SCARG(uap, which)) {
151 	case 0:						/* shmat() */
152 		SCARG(&shmat_args, shmid) = SCARG(uap, a2);
153 		SCARG(&shmat_args, shmaddr) = NETBSD32IPTR64(SCARG(uap, a3));
154 		SCARG(&shmat_args, shmflg) = SCARG(uap, a4);
155 		return sys_shmat(l, &shmat_args, retval);
156 
157 	case 1:						/* shmctl() */
158 		SCARG(&shmctl_args, shmid) = SCARG(uap, a2);
159 		SCARG(&shmctl_args, cmd) = SCARG(uap, a3);
160 		SCARG(&shmctl_args, buf) = NETBSD32IPTR64(SCARG(uap, a4));
161 		return compat_14_sys_shmctl(l, &shmctl_args, retval);
162 
163 	case 2:						/* shmdt() */
164 		SCARG(&shmdt_args, shmaddr) = NETBSD32IPTR64(SCARG(uap, a2));
165 		return sys_shmdt(l, &shmdt_args, retval);
166 
167 	case 3:						/* shmget() */
168 		SCARG(&shmget_args, key) = SCARG(uap, a2);
169 		SCARG(&shmget_args, size) = SCARG(uap, a3);
170 		SCARG(&shmget_args, shmflg) = SCARG(uap, a4);
171 		return sys_shmget(l, &shmget_args, retval);
172 
173 	default:
174 		return EINVAL;
175 	}
176 }
177 #endif
178 
179 #if defined(SYSVMSG)
180 int
compat_10_netbsd32_msgsys(struct lwp * l,const struct compat_10_netbsd32_msgsys_args * uap,register_t * retval)181 compat_10_netbsd32_msgsys(struct lwp *l, const struct compat_10_netbsd32_msgsys_args *uap, register_t *retval)
182 {
183 	/* {
184 		syscallarg(int) which;
185 		syscallarg(int) a2;
186 		syscallarg(int) a3;
187 		syscallarg(int) a4;
188 		syscallarg(int) a5;
189 		syscallarg(int) a6;
190 	} */
191 	struct compat_14_sys_msgctl_args /* {
192 		syscallarg(int) msqid;
193 		syscallarg(int) cmd;
194 		syscallarg(struct msqid_ds *) buf;
195 	} */ msgctl_args;
196 	struct sys_msgget_args /* {
197 		syscallarg(key_t) key;
198 		syscallarg(int) msgflg;
199 	} */ msgget_args;
200 	struct sys_msgsnd_args /* {
201 		syscallarg(int) msqid;
202 		syscallarg(void *) msgp;
203 		syscallarg(size_t) msgsz;
204 		syscallarg(int) msgflg;
205 	} */ msgsnd_args;
206 	struct sys_msgrcv_args /* {
207 		syscallarg(int) msqid;
208 		syscallarg(void *) msgp;
209 		syscallarg(size_t) msgsz;
210 		syscallarg(long) msgtyp;
211 		syscallarg(int) msgflg;
212 	} */ msgrcv_args;
213 
214 	switch (SCARG(uap, which)) {
215 	case 0:					/* msgctl()*/
216 		SCARG(&msgctl_args, msqid) = SCARG(uap, a2);
217 		SCARG(&msgctl_args, cmd) = SCARG(uap, a3);
218 		SCARG(&msgctl_args, buf) = NETBSD32IPTR64(SCARG(uap, a4));
219 		return compat_14_sys_msgctl(l, &msgctl_args, retval);
220 
221 	case 1:					/* msgget() */
222 		SCARG(&msgget_args, key) = SCARG(uap, a2);
223 		SCARG(&msgget_args, msgflg) = SCARG(uap, a3);
224 		return sys_msgget(l, &msgget_args, retval);
225 
226 	case 2:					/* msgsnd() */
227 		SCARG(&msgsnd_args, msqid) = SCARG(uap, a2);
228 		SCARG(&msgsnd_args, msgp) = NETBSD32IPTR64(SCARG(uap, a3));
229 		SCARG(&msgsnd_args, msgsz) = SCARG(uap, a4);
230 		SCARG(&msgsnd_args, msgflg) = SCARG(uap, a5);
231 		return sys_msgsnd(l, &msgsnd_args, retval);
232 
233 	case 3:					/* msgrcv() */
234 		SCARG(&msgrcv_args, msqid) = SCARG(uap, a2);
235 		SCARG(&msgrcv_args, msgp) = NETBSD32IPTR64(SCARG(uap, a3));
236 		SCARG(&msgrcv_args, msgsz) = SCARG(uap, a4);
237 		SCARG(&msgrcv_args, msgtyp) = SCARG(uap, a5);
238 		SCARG(&msgrcv_args, msgflg) = SCARG(uap, a6);
239 		return sys_msgrcv(l, &msgrcv_args, retval);
240 
241 	default:
242 		return EINVAL;
243 	}
244 }
245 #endif
246 
247 #define _PKG_ENTRY(name)        \
248 	{ NETBSD32_SYS_ ## name, 0, (sy_call_t *)name }
249 
250 #define _PKG_ENTRY2(code, name) \
251 	{ NETBSD32_SYS_ ## code, 0, (sy_call_t *)name }
252 
253 static const struct syscall_package compat_sysvipc_10_syscalls[] = {
254 #if defined(SYSVSEM)
255 	_PKG_ENTRY2(compat_10_osemsys, compat_10_netbsd32_semsys),
256 #endif
257 #if defined(SYSVSHM)
258 	_PKG_ENTRY2(compat_10_oshmsys, compat_10_netbsd32_shmsys),
259 #endif
260 #if defined(SYSVMSG)
261 	_PKG_ENTRY2(compat_10_omsgsys, compat_10_netbsd32_msgsys),
262 #endif
263 	{ 0, 0, NULL }
264 };
265 
266 #define	REQ1    "sysv_ipc,compat_sysv_10,"
267 #define	REQ2    "compat_netbsd32,compat_netbsd32_sysvipc,"
268 #define	REQ3	"compat_netbsd32_sysvipc_14,compat_netbsd32_sysvipc_50"
269 
270 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_sysvipc_10, REQ1 REQ2 REQ3 );
271 
272 static int
compat_netbsd32_sysvipc_10_modcmd(modcmd_t cmd,void * arg)273 compat_netbsd32_sysvipc_10_modcmd(modcmd_t cmd, void *arg)
274 {
275 
276 	switch (cmd) {
277 	case MODULE_CMD_INIT:
278 		return syscall_establish(&emul_netbsd32,
279 		    compat_sysvipc_10_syscalls);
280 
281 	case MODULE_CMD_FINI:
282 		return syscall_disestablish(&emul_netbsd32,
283 		    compat_sysvipc_10_syscalls);
284 
285 	default:
286 		return ENOTTY;
287 	}
288 }
289 
290 #endif /* COMPAT_10 */
291