1 /*	$NetBSD: netbsd32_conv.h,v 1.30 2015/06/22 10:35:00 mrg Exp $	*/
2 
3 /*
4  * Copyright (c) 1998, 2001 Matthew R. Green
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef _COMPAT_NETBSD32_NETBSD32_CONV_H_
30 #define _COMPAT_NETBSD32_NETBSD32_CONV_H_
31 
32 /*
33  * Though COMPAT_OLDSOCK is needed only for COMPAT_43, SunOS, Linux,
34  * HP-UX, FreeBSD, Ultrix, OSF1, we define it unconditionally so that
35  * this would be module-safe.
36  */
37 #define COMPAT_OLDSOCK /* used by <sys/socket.h> */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/dirent.h>
43 #include <sys/ipc.h>
44 #include <sys/msg.h>
45 #define msg __msg /* Don't ask me! */
46 #include <sys/sem.h>
47 #include <sys/shm.h>
48 #include <sys/socket.h>
49 #include <sys/stat.h>
50 #include <sys/time.h>
51 #include <sys/timex.h>
52 #include <sys/event.h>
53 
54 #include <compat/sys/dirent.h>
55 
56 #include <prop/plistref.h>
57 
58 #include <compat/netbsd32/netbsd32.h>
59 
60 /* converters for structures that we need */
61 static __inline void
netbsd32_from_timeval50(const struct timeval * tv,struct netbsd32_timeval50 * tv32)62 netbsd32_from_timeval50(const struct timeval *tv,
63     struct netbsd32_timeval50 *tv32)
64 {
65 
66 	tv32->tv_sec = (netbsd32_long)tv->tv_sec;
67 	tv32->tv_usec = (netbsd32_long)tv->tv_usec;
68 }
69 
70 static __inline void
netbsd32_from_timeval(const struct timeval * tv,struct netbsd32_timeval * tv32)71 netbsd32_from_timeval(const struct timeval *tv,
72     struct netbsd32_timeval *tv32)
73 {
74 
75 	tv32->tv_sec = (time_t)tv->tv_sec;
76 	tv32->tv_usec = (suseconds_t)tv->tv_usec;
77 }
78 
79 static __inline void
netbsd32_to_timeval50(const struct netbsd32_timeval50 * tv32,struct timeval * tv)80 netbsd32_to_timeval50(const struct netbsd32_timeval50 *tv32,
81     struct timeval *tv)
82 {
83 
84 	tv->tv_sec = (time_t)tv32->tv_sec;
85 	tv->tv_usec = (suseconds_t)tv32->tv_usec;
86 }
87 
88 static __inline void
netbsd32_to_timeval(const struct netbsd32_timeval * tv32,struct timeval * tv)89 netbsd32_to_timeval(const struct netbsd32_timeval *tv32,
90     struct timeval *tv)
91 {
92 
93 	tv->tv_sec = (time_t)tv32->tv_sec;
94 	tv->tv_usec = (suseconds_t)tv32->tv_usec;
95 }
96 
97 static __inline void
netbsd32_from_itimerval50(const struct itimerval * itv,struct netbsd32_itimerval50 * itv32)98 netbsd32_from_itimerval50(const struct itimerval *itv,
99     struct netbsd32_itimerval50 *itv32)
100 {
101 
102 	netbsd32_from_timeval50(&itv->it_interval,
103 			     &itv32->it_interval);
104 	netbsd32_from_timeval50(&itv->it_value,
105 			     &itv32->it_value);
106 }
107 
108 static __inline void
netbsd32_from_itimerval(const struct itimerval * itv,struct netbsd32_itimerval * itv32)109 netbsd32_from_itimerval(const struct itimerval *itv,
110     struct netbsd32_itimerval *itv32)
111 {
112 
113 	netbsd32_from_timeval(&itv->it_interval,
114 			     &itv32->it_interval);
115 	netbsd32_from_timeval(&itv->it_value,
116 			     &itv32->it_value);
117 }
118 
119 static __inline void
netbsd32_to_itimerval50(const struct netbsd32_itimerval50 * itv32,struct itimerval * itv)120 netbsd32_to_itimerval50(const struct netbsd32_itimerval50 *itv32,
121     struct itimerval *itv)
122 {
123 
124 	netbsd32_to_timeval50(&itv32->it_interval, &itv->it_interval);
125 	netbsd32_to_timeval50(&itv32->it_value, &itv->it_value);
126 }
127 
128 static __inline void
netbsd32_to_itimerval(const struct netbsd32_itimerval * itv32,struct itimerval * itv)129 netbsd32_to_itimerval(const struct netbsd32_itimerval *itv32,
130     struct itimerval *itv)
131 {
132 
133 	netbsd32_to_timeval(&itv32->it_interval, &itv->it_interval);
134 	netbsd32_to_timeval(&itv32->it_value, &itv->it_value);
135 }
136 
137 static __inline void
netbsd32_to_timespec50(const struct netbsd32_timespec50 * s32p,struct timespec * p)138 netbsd32_to_timespec50(const struct netbsd32_timespec50 *s32p,
139     struct timespec *p)
140 {
141 
142 	p->tv_sec = (time_t)s32p->tv_sec;
143 	p->tv_nsec = (long)s32p->tv_nsec;
144 }
145 
146 static __inline void
netbsd32_to_timespec(const struct netbsd32_timespec * s32p,struct timespec * p)147 netbsd32_to_timespec(const struct netbsd32_timespec *s32p,
148     struct timespec *p)
149 {
150 
151 	p->tv_sec = (time_t)s32p->tv_sec;
152 	p->tv_nsec = (long)s32p->tv_nsec;
153 }
154 
155 static __inline void
netbsd32_from_timespec50(const struct timespec * p,struct netbsd32_timespec50 * s32p)156 netbsd32_from_timespec50(const struct timespec *p,
157     struct netbsd32_timespec50 *s32p)
158 {
159 
160 	s32p->tv_sec = (netbsd32_long)p->tv_sec;
161 	s32p->tv_nsec = (netbsd32_long)p->tv_nsec;
162 }
163 
164 static __inline void
netbsd32_from_timespec(const struct timespec * p,struct netbsd32_timespec * s32p)165 netbsd32_from_timespec(const struct timespec *p,
166     struct netbsd32_timespec *s32p)
167 {
168 
169 	s32p->tv_sec = (netbsd32_long)p->tv_sec;
170 	s32p->tv_nsec = (netbsd32_long)p->tv_nsec;
171 }
172 
173 static __inline void
netbsd32_from_rusage(const struct rusage * rup,struct netbsd32_rusage * ru32p)174 netbsd32_from_rusage(const struct rusage *rup,
175     struct netbsd32_rusage *ru32p)
176 {
177 
178 	netbsd32_from_timeval(&rup->ru_utime, &ru32p->ru_utime);
179 	netbsd32_from_timeval(&rup->ru_stime, &ru32p->ru_stime);
180 #define C(var)	ru32p->var = (netbsd32_long)rup->var
181 	C(ru_maxrss);
182 	C(ru_ixrss);
183 	C(ru_idrss);
184 	C(ru_isrss);
185 	C(ru_minflt);
186 	C(ru_majflt);
187 	C(ru_nswap);
188 	C(ru_inblock);
189 	C(ru_oublock);
190 	C(ru_msgsnd);
191 	C(ru_msgrcv);
192 	C(ru_nsignals);
193 	C(ru_nvcsw);
194 	C(ru_nivcsw);
195 #undef C
196 }
197 
198 static __inline void
netbsd32_to_rusage(const struct netbsd32_rusage * ru32p,struct rusage * rup)199 netbsd32_to_rusage(const struct netbsd32_rusage *ru32p,
200     struct rusage *rup)
201 {
202 
203 	netbsd32_to_timeval(&ru32p->ru_utime, &rup->ru_utime);
204 	netbsd32_to_timeval(&ru32p->ru_stime, &rup->ru_stime);
205 #define C(var)	rup->var = (long)ru32p->var
206 	C(ru_maxrss);
207 	C(ru_ixrss);
208 	C(ru_idrss);
209 	C(ru_isrss);
210 	C(ru_minflt);
211 	C(ru_majflt);
212 	C(ru_nswap);
213 	C(ru_inblock);
214 	C(ru_oublock);
215 	C(ru_msgsnd);
216 	C(ru_msgrcv);
217 	C(ru_nsignals);
218 	C(ru_nvcsw);
219 	C(ru_nivcsw);
220 #undef C
221 }
222 
223 static __inline void
netbsd32_from_rusage50(const struct rusage * rup,struct netbsd32_rusage50 * ru32p)224 netbsd32_from_rusage50(const struct rusage *rup,
225     struct netbsd32_rusage50 *ru32p)
226 {
227 
228 	netbsd32_from_timeval50(&rup->ru_utime, &ru32p->ru_utime);
229 	netbsd32_from_timeval50(&rup->ru_stime, &ru32p->ru_stime);
230 #define C(var)	ru32p->var = (netbsd32_long)rup->var
231 	C(ru_maxrss);
232 	C(ru_ixrss);
233 	C(ru_idrss);
234 	C(ru_isrss);
235 	C(ru_minflt);
236 	C(ru_majflt);
237 	C(ru_nswap);
238 	C(ru_inblock);
239 	C(ru_oublock);
240 	C(ru_msgsnd);
241 	C(ru_msgrcv);
242 	C(ru_nsignals);
243 	C(ru_nvcsw);
244 	C(ru_nivcsw);
245 #undef C
246 }
247 
248 static __inline int
netbsd32_to_iovecin(const struct netbsd32_iovec * iov32p,struct iovec * iovp,int len)249 netbsd32_to_iovecin(const struct netbsd32_iovec *iov32p, struct iovec *iovp,
250     int len)
251 {
252 	int i, error=0;
253 	u_int32_t iov_base;
254 	u_int32_t iov_len;
255 	/*
256 	 * We could allocate an iov32p, do a copyin, and translate
257 	 * each field and then free it all up, or we could copyin
258 	 * each field separately.  I'm doing the latter to reduce
259 	 * the number of MALLOC()s.
260 	 */
261 	for (i = 0; i < len; i++, iovp++, iov32p++) {
262 		if ((error = copyin(&iov32p->iov_base, &iov_base, sizeof(iov_base))))
263 		    return (error);
264 		if ((error = copyin(&iov32p->iov_len, &iov_len, sizeof(iov_len))))
265 		    return (error);
266 		iovp->iov_base = (void *)(u_long)iov_base;
267 		iovp->iov_len = (size_t)iov_len;
268 	}
269 	return error;
270 }
271 
272 /* msg_iov must be done separately */
273 static __inline void
netbsd32_to_msghdr(const struct netbsd32_msghdr * mhp32,struct msghdr * mhp)274 netbsd32_to_msghdr(const struct netbsd32_msghdr *mhp32, struct msghdr *mhp)
275 {
276 
277 	mhp->msg_name = NETBSD32PTR64(mhp32->msg_name);
278 	mhp->msg_namelen = mhp32->msg_namelen;
279 	mhp->msg_iovlen = (size_t)mhp32->msg_iovlen;
280 	mhp->msg_control = NETBSD32PTR64(mhp32->msg_control);
281 	mhp->msg_controllen = mhp32->msg_controllen;
282 	mhp->msg_flags = mhp32->msg_flags;
283 }
284 
285 /* msg_iov must be done separately */
286 static __inline void
netbsd32_from_msghdr(struct netbsd32_msghdr * mhp32,const struct msghdr * mhp)287 netbsd32_from_msghdr(struct netbsd32_msghdr *mhp32, const struct msghdr *mhp)
288 {
289 
290 	mhp32->msg_name = mhp32->msg_name;
291 	mhp32->msg_namelen = mhp32->msg_namelen;
292 	mhp32->msg_iovlen = mhp32->msg_iovlen;
293 	mhp32->msg_control = mhp32->msg_control;
294 	mhp32->msg_controllen = mhp->msg_controllen;
295 	mhp32->msg_flags = mhp->msg_flags;
296 }
297 
298 static __inline void
netbsd32_from_statvfs(const struct statvfs * sbp,struct netbsd32_statvfs * sb32p)299 netbsd32_from_statvfs(const struct statvfs *sbp, struct netbsd32_statvfs *sb32p)
300 {
301 	sb32p->f_flag = sbp->f_flag;
302 	sb32p->f_bsize = (netbsd32_u_long)sbp->f_bsize;
303 	sb32p->f_frsize = (netbsd32_u_long)sbp->f_frsize;
304 	sb32p->f_iosize = (netbsd32_u_long)sbp->f_iosize;
305 	sb32p->f_blocks = sbp->f_blocks;
306 	sb32p->f_bfree = sbp->f_bfree;
307 	sb32p->f_bavail = sbp->f_bavail;
308 	sb32p->f_bresvd = sbp->f_bresvd;
309 	sb32p->f_files = sbp->f_files;
310 	sb32p->f_ffree = sbp->f_ffree;
311 	sb32p->f_favail = sbp->f_favail;
312 	sb32p->f_fresvd = sbp->f_fresvd;
313 	sb32p->f_syncreads = sbp->f_syncreads;
314 	sb32p->f_syncwrites = sbp->f_syncwrites;
315 	sb32p->f_asyncreads = sbp->f_asyncreads;
316 	sb32p->f_asyncwrites = sbp->f_asyncwrites;
317 	sb32p->f_fsidx = sbp->f_fsidx;
318 	sb32p->f_fsid = (netbsd32_u_long)sbp->f_fsid;
319 	sb32p->f_namemax = (netbsd32_u_long)sbp->f_namemax;
320 	sb32p->f_owner = sbp->f_owner;
321 	sb32p->f_spare[0] = 0;
322 	sb32p->f_spare[1] = 0;
323 	sb32p->f_spare[2] = 0;
324 	sb32p->f_spare[3] = 0;
325 #if 1
326 	/* May as well do the whole batch in one go */
327 	memcpy(sb32p->f_fstypename, sbp->f_fstypename,
328 	    sizeof(sb32p->f_fstypename) + sizeof(sb32p->f_mntonname) +
329 	    sizeof(sb32p->f_mntfromname));
330 #else
331 	/* If we want to be careful */
332 	memcpy(sb32p->f_fstypename, sbp->f_fstypename, sizeof(sb32p->f_fstypename));
333 	memcpy(sb32p->f_mntonname, sbp->f_mntonname, sizeof(sb32p->f_mntonname));
334 	memcpy(sb32p->f_mntfromname, sbp->f_mntfromname, sizeof(sb32p->f_mntfromname));
335 #endif
336 }
337 
338 static __inline void
netbsd32_from_timex(const struct timex * txp,struct netbsd32_timex * tx32p)339 netbsd32_from_timex(const struct timex *txp, struct netbsd32_timex *tx32p)
340 {
341 
342 	tx32p->modes = txp->modes;
343 	tx32p->offset = (netbsd32_long)txp->offset;
344 	tx32p->freq = (netbsd32_long)txp->freq;
345 	tx32p->maxerror = (netbsd32_long)txp->maxerror;
346 	tx32p->esterror = (netbsd32_long)txp->esterror;
347 	tx32p->status = txp->status;
348 	tx32p->constant = (netbsd32_long)txp->constant;
349 	tx32p->precision = (netbsd32_long)txp->precision;
350 	tx32p->tolerance = (netbsd32_long)txp->tolerance;
351 	tx32p->ppsfreq = (netbsd32_long)txp->ppsfreq;
352 	tx32p->jitter = (netbsd32_long)txp->jitter;
353 	tx32p->shift = txp->shift;
354 	tx32p->stabil = (netbsd32_long)txp->stabil;
355 	tx32p->jitcnt = (netbsd32_long)txp->jitcnt;
356 	tx32p->calcnt = (netbsd32_long)txp->calcnt;
357 	tx32p->errcnt = (netbsd32_long)txp->errcnt;
358 	tx32p->stbcnt = (netbsd32_long)txp->stbcnt;
359 }
360 
361 static __inline void
netbsd32_to_timex(const struct netbsd32_timex * tx32p,struct timex * txp)362 netbsd32_to_timex(const struct netbsd32_timex *tx32p, struct timex *txp)
363 {
364 
365 	txp->modes = tx32p->modes;
366 	txp->offset = (long)tx32p->offset;
367 	txp->freq = (long)tx32p->freq;
368 	txp->maxerror = (long)tx32p->maxerror;
369 	txp->esterror = (long)tx32p->esterror;
370 	txp->status = tx32p->status;
371 	txp->constant = (long)tx32p->constant;
372 	txp->precision = (long)tx32p->precision;
373 	txp->tolerance = (long)tx32p->tolerance;
374 	txp->ppsfreq = (long)tx32p->ppsfreq;
375 	txp->jitter = (long)tx32p->jitter;
376 	txp->shift = tx32p->shift;
377 	txp->stabil = (long)tx32p->stabil;
378 	txp->jitcnt = (long)tx32p->jitcnt;
379 	txp->calcnt = (long)tx32p->calcnt;
380 	txp->errcnt = (long)tx32p->errcnt;
381 	txp->stbcnt = (long)tx32p->stbcnt;
382 }
383 
384 static __inline void
netbsd32_from___stat13(const struct stat * sbp,struct netbsd32_stat13 * sb32p)385 netbsd32_from___stat13(const struct stat *sbp, struct netbsd32_stat13 *sb32p)
386 {
387 	memset(sb32p, 0, sizeof(*sb32p));
388 	sb32p->st_dev = (uint32_t)sbp->st_dev;
389 	sb32p->st_ino = sbp->st_ino;
390 	sb32p->st_mode = sbp->st_mode;
391 	sb32p->st_nlink = sbp->st_nlink;
392 	sb32p->st_uid = sbp->st_uid;
393 	sb32p->st_gid = sbp->st_gid;
394 	sb32p->st_rdev = (uint32_t)sbp->st_rdev;
395 	sb32p->st_size = sbp->st_size;
396 	sb32p->st_atimespec.tv_sec = (int32_t)sbp->st_atimespec.tv_sec;
397 	sb32p->st_atimespec.tv_nsec = (netbsd32_long)sbp->st_atimespec.tv_nsec;
398 	sb32p->st_mtimespec.tv_sec = (int32_t)sbp->st_mtimespec.tv_sec;
399 	sb32p->st_mtimespec.tv_nsec = (netbsd32_long)sbp->st_mtimespec.tv_nsec;
400 	sb32p->st_ctimespec.tv_sec = (int32_t)sbp->st_ctimespec.tv_sec;
401 	sb32p->st_ctimespec.tv_nsec = (netbsd32_long)sbp->st_ctimespec.tv_nsec;
402 	sb32p->st_blksize = sbp->st_blksize;
403 	sb32p->st_blocks = sbp->st_blocks;
404 	sb32p->st_flags = sbp->st_flags;
405 	sb32p->st_gen = sbp->st_gen;
406 	sb32p->st_birthtimespec.tv_sec = (int32_t)sbp->st_birthtimespec.tv_sec;
407 	sb32p->st_birthtimespec.tv_nsec = (netbsd32_long)sbp->st_birthtimespec.tv_nsec;
408 }
409 
410 static __inline void
netbsd32_from___stat50(const struct stat * sbp,struct netbsd32_stat50 * sb32p)411 netbsd32_from___stat50(const struct stat *sbp, struct netbsd32_stat50 *sb32p)
412 {
413 	memset(sb32p, 0, sizeof(*sb32p));
414 	sb32p->st_dev = (uint32_t)sbp->st_dev;
415 	sb32p->st_ino = sbp->st_ino;
416 	sb32p->st_mode = sbp->st_mode;
417 	sb32p->st_nlink = sbp->st_nlink;
418 	sb32p->st_uid = sbp->st_uid;
419 	sb32p->st_gid = sbp->st_gid;
420 	sb32p->st_rdev = (uint32_t)sbp->st_rdev;
421 	sb32p->st_size = sbp->st_size;
422 	sb32p->st_atimespec.tv_sec = (int32_t)sbp->st_atimespec.tv_sec;
423 	sb32p->st_atimespec.tv_nsec = (netbsd32_long)sbp->st_atimespec.tv_nsec;
424 	sb32p->st_mtimespec.tv_sec = (int32_t)sbp->st_mtimespec.tv_sec;
425 	sb32p->st_mtimespec.tv_nsec = (netbsd32_long)sbp->st_mtimespec.tv_nsec;
426 	sb32p->st_ctimespec.tv_sec = (int32_t)sbp->st_ctimespec.tv_sec;
427 	sb32p->st_ctimespec.tv_nsec = (netbsd32_long)sbp->st_ctimespec.tv_nsec;
428 	sb32p->st_birthtimespec.tv_sec = (int32_t)sbp->st_birthtimespec.tv_sec;
429 	sb32p->st_birthtimespec.tv_nsec = (netbsd32_long)sbp->st_birthtimespec.tv_nsec;
430 	sb32p->st_blksize = sbp->st_blksize;
431 	sb32p->st_blocks = sbp->st_blocks;
432 	sb32p->st_flags = sbp->st_flags;
433 	sb32p->st_gen = sbp->st_gen;
434 }
435 
436 static __inline void
netbsd32_from_stat(const struct stat * sbp,struct netbsd32_stat * sb32p)437 netbsd32_from_stat(const struct stat *sbp, struct netbsd32_stat *sb32p)
438 {
439 	memset(sb32p, 0, sizeof(*sb32p));
440 	sb32p->st_dev = sbp->st_dev;
441 	sb32p->st_ino = sbp->st_ino;
442 	sb32p->st_mode = sbp->st_mode;
443 	sb32p->st_nlink = sbp->st_nlink;
444 	sb32p->st_uid = sbp->st_uid;
445 	sb32p->st_gid = sbp->st_gid;
446 	sb32p->st_rdev = sbp->st_rdev;
447 	sb32p->st_size = sbp->st_size;
448 	sb32p->st_atimespec.tv_sec = (netbsd32_time_t)sbp->st_atimespec.tv_sec;
449 	sb32p->st_atimespec.tv_nsec = (netbsd32_long)sbp->st_atimespec.tv_nsec;
450 	sb32p->st_mtimespec.tv_sec = (netbsd32_time_t)sbp->st_mtimespec.tv_sec;
451 	sb32p->st_mtimespec.tv_nsec = (netbsd32_long)sbp->st_mtimespec.tv_nsec;
452 	sb32p->st_ctimespec.tv_sec = (netbsd32_time_t)sbp->st_ctimespec.tv_sec;
453 	sb32p->st_ctimespec.tv_nsec = (netbsd32_long)sbp->st_ctimespec.tv_nsec;
454 	sb32p->st_birthtimespec.tv_sec = (netbsd32_time_t)sbp->st_birthtimespec.tv_sec;
455 	sb32p->st_birthtimespec.tv_nsec = (netbsd32_long)sbp->st_birthtimespec.tv_nsec;
456 	sb32p->st_blksize = sbp->st_blksize;
457 	sb32p->st_blocks = sbp->st_blocks;
458 	sb32p->st_flags = sbp->st_flags;
459 	sb32p->st_gen = sbp->st_gen;
460 }
461 
462 static __inline void
netbsd32_to_ipc_perm(const struct netbsd32_ipc_perm * ip32p,struct ipc_perm * ipp)463 netbsd32_to_ipc_perm(const struct netbsd32_ipc_perm *ip32p,
464     struct ipc_perm *ipp)
465 {
466 
467 	ipp->cuid = ip32p->cuid;
468 	ipp->cgid = ip32p->cgid;
469 	ipp->uid = ip32p->uid;
470 	ipp->gid = ip32p->gid;
471 	ipp->mode = ip32p->mode;
472 	ipp->_seq = ip32p->_seq;
473 	ipp->_key = (key_t)ip32p->_key;
474 }
475 
476 static __inline void
netbsd32_from_ipc_perm(const struct ipc_perm * ipp,struct netbsd32_ipc_perm * ip32p)477 netbsd32_from_ipc_perm(const struct ipc_perm *ipp,
478     struct netbsd32_ipc_perm *ip32p)
479 {
480 
481 	ip32p->cuid = ipp->cuid;
482 	ip32p->cgid = ipp->cgid;
483 	ip32p->uid = ipp->uid;
484 	ip32p->gid = ipp->gid;
485 	ip32p->mode = ipp->mode;
486 	ip32p->_seq = ipp->_seq;
487 	ip32p->_key = (netbsd32_key_t)ipp->_key;
488 }
489 
490 static __inline void
netbsd32_to_msg(const struct netbsd32_msg * m32p,struct msg * mp)491 netbsd32_to_msg(const struct netbsd32_msg *m32p, struct msg *mp)
492 {
493 
494 	mp->msg_next = NETBSD32PTR64(m32p->msg_next);
495 	mp->msg_type = (long)m32p->msg_type;
496 	mp->msg_ts = m32p->msg_ts;
497 	mp->msg_spot = m32p->msg_spot;
498 }
499 
500 static __inline void
netbsd32_from_msg(const struct msg * mp,struct netbsd32_msg * m32p)501 netbsd32_from_msg(const struct msg *mp, struct netbsd32_msg *m32p)
502 {
503 
504 	NETBSD32PTR32(m32p->msg_next, mp->msg_next);
505 	m32p->msg_type = (netbsd32_long)mp->msg_type;
506 	m32p->msg_ts = mp->msg_ts;
507 	m32p->msg_spot = mp->msg_spot;
508 }
509 
510 static __inline void
netbsd32_to_msqid_ds50(const struct netbsd32_msqid_ds50 * ds32p,struct msqid_ds * dsp)511 netbsd32_to_msqid_ds50(const struct netbsd32_msqid_ds50 *ds32p,
512     struct msqid_ds *dsp)
513 {
514 
515 	netbsd32_to_ipc_perm(&ds32p->msg_perm, &dsp->msg_perm);
516 	dsp->_msg_cbytes = (u_long)ds32p->_msg_cbytes;
517 	dsp->msg_qnum = (u_long)ds32p->msg_qnum;
518 	dsp->msg_qbytes = (u_long)ds32p->msg_qbytes;
519 	dsp->msg_lspid = ds32p->msg_lspid;
520 	dsp->msg_lrpid = ds32p->msg_lrpid;
521 	dsp->msg_rtime = (time_t)ds32p->msg_rtime;
522 	dsp->msg_stime = (time_t)ds32p->msg_stime;
523 	dsp->msg_ctime = (time_t)ds32p->msg_ctime;
524 }
525 
526 static __inline void
netbsd32_to_msqid_ds(const struct netbsd32_msqid_ds * ds32p,struct msqid_ds * dsp)527 netbsd32_to_msqid_ds(const struct netbsd32_msqid_ds *ds32p,
528     struct msqid_ds *dsp)
529 {
530 
531 	netbsd32_to_ipc_perm(&ds32p->msg_perm, &dsp->msg_perm);
532 	dsp->_msg_cbytes = (u_long)ds32p->_msg_cbytes;
533 	dsp->msg_qnum = (u_long)ds32p->msg_qnum;
534 	dsp->msg_qbytes = (u_long)ds32p->msg_qbytes;
535 	dsp->msg_lspid = ds32p->msg_lspid;
536 	dsp->msg_lrpid = ds32p->msg_lrpid;
537 	dsp->msg_rtime = (time_t)ds32p->msg_rtime;
538 	dsp->msg_stime = (time_t)ds32p->msg_stime;
539 	dsp->msg_ctime = (time_t)ds32p->msg_ctime;
540 }
541 
542 static __inline void
netbsd32_from_msqid_ds50(const struct msqid_ds * dsp,struct netbsd32_msqid_ds50 * ds32p)543 netbsd32_from_msqid_ds50(const struct msqid_ds *dsp,
544     struct netbsd32_msqid_ds50 *ds32p)
545 {
546 
547 	netbsd32_from_ipc_perm(&dsp->msg_perm, &ds32p->msg_perm);
548 	ds32p->_msg_cbytes = (netbsd32_u_long)dsp->_msg_cbytes;
549 	ds32p->msg_qnum = (netbsd32_u_long)dsp->msg_qnum;
550 	ds32p->msg_qbytes = (netbsd32_u_long)dsp->msg_qbytes;
551 	ds32p->msg_lspid = dsp->msg_lspid;
552 	ds32p->msg_lrpid = dsp->msg_lrpid;
553 	ds32p->msg_rtime = (int32_t)dsp->msg_rtime;
554 	ds32p->msg_stime = (int32_t)dsp->msg_stime;
555 	ds32p->msg_ctime = (int32_t)dsp->msg_ctime;
556 }
557 
558 static __inline void
netbsd32_from_msqid_ds(const struct msqid_ds * dsp,struct netbsd32_msqid_ds * ds32p)559 netbsd32_from_msqid_ds(const struct msqid_ds *dsp,
560     struct netbsd32_msqid_ds *ds32p)
561 {
562 
563 	netbsd32_from_ipc_perm(&dsp->msg_perm, &ds32p->msg_perm);
564 	ds32p->_msg_cbytes = (netbsd32_u_long)dsp->_msg_cbytes;
565 	ds32p->msg_qnum = (netbsd32_u_long)dsp->msg_qnum;
566 	ds32p->msg_qbytes = (netbsd32_u_long)dsp->msg_qbytes;
567 	ds32p->msg_lspid = dsp->msg_lspid;
568 	ds32p->msg_lrpid = dsp->msg_lrpid;
569 	ds32p->msg_rtime = dsp->msg_rtime;
570 	ds32p->msg_stime = dsp->msg_stime;
571 	ds32p->msg_ctime = dsp->msg_ctime;
572 }
573 
574 static __inline void
netbsd32_to_shmid_ds50(const struct netbsd32_shmid_ds50 * ds32p,struct shmid_ds * dsp)575 netbsd32_to_shmid_ds50(const struct netbsd32_shmid_ds50 *ds32p,
576     struct shmid_ds *dsp)
577 {
578 
579 	netbsd32_to_ipc_perm(&ds32p->shm_perm, &dsp->shm_perm);
580 	dsp->shm_segsz = ds32p->shm_segsz;
581 	dsp->shm_lpid = ds32p->shm_lpid;
582 	dsp->shm_cpid = ds32p->shm_cpid;
583 	dsp->shm_nattch = ds32p->shm_nattch;
584 	dsp->shm_atime = (time_t)ds32p->shm_atime;
585 	dsp->shm_dtime = (time_t)ds32p->shm_dtime;
586 	dsp->shm_ctime = (time_t)ds32p->shm_ctime;
587 	dsp->_shm_internal = NETBSD32PTR64(ds32p->_shm_internal);
588 }
589 
590 static __inline void
netbsd32_to_shmid_ds(const struct netbsd32_shmid_ds * ds32p,struct shmid_ds * dsp)591 netbsd32_to_shmid_ds(const struct netbsd32_shmid_ds *ds32p,
592     struct shmid_ds *dsp)
593 {
594 
595 	netbsd32_to_ipc_perm(&ds32p->shm_perm, &dsp->shm_perm);
596 	dsp->shm_segsz = ds32p->shm_segsz;
597 	dsp->shm_lpid = ds32p->shm_lpid;
598 	dsp->shm_cpid = ds32p->shm_cpid;
599 	dsp->shm_nattch = ds32p->shm_nattch;
600 	dsp->shm_atime = (long)ds32p->shm_atime;
601 	dsp->shm_dtime = (time_t)ds32p->shm_dtime;
602 	dsp->shm_ctime = (time_t)ds32p->shm_ctime;
603 	dsp->_shm_internal = NETBSD32PTR64(ds32p->_shm_internal);
604 }
605 
606 static __inline void
netbsd32_from_shmid_ds50(const struct shmid_ds * dsp,struct netbsd32_shmid_ds50 * ds32p)607 netbsd32_from_shmid_ds50(const struct shmid_ds *dsp,
608     struct netbsd32_shmid_ds50 *ds32p)
609 {
610 
611 	netbsd32_from_ipc_perm(&dsp->shm_perm, &ds32p->shm_perm);
612 	ds32p->shm_segsz = dsp->shm_segsz;
613 	ds32p->shm_lpid = dsp->shm_lpid;
614 	ds32p->shm_cpid = dsp->shm_cpid;
615 	ds32p->shm_nattch = dsp->shm_nattch;
616 	ds32p->shm_atime = (int32_t)dsp->shm_atime;
617 	ds32p->shm_dtime = (int32_t)dsp->shm_dtime;
618 	ds32p->shm_ctime = (int32_t)dsp->shm_ctime;
619 	NETBSD32PTR32(ds32p->_shm_internal, dsp->_shm_internal);
620 }
621 
622 static __inline void
netbsd32_from_shmid_ds(const struct shmid_ds * dsp,struct netbsd32_shmid_ds * ds32p)623 netbsd32_from_shmid_ds(const struct shmid_ds *dsp,
624     struct netbsd32_shmid_ds *ds32p)
625 {
626 
627 	netbsd32_from_ipc_perm(&dsp->shm_perm, &ds32p->shm_perm);
628 	ds32p->shm_segsz = dsp->shm_segsz;
629 	ds32p->shm_lpid = dsp->shm_lpid;
630 	ds32p->shm_cpid = dsp->shm_cpid;
631 	ds32p->shm_nattch = dsp->shm_nattch;
632 	ds32p->shm_atime = (netbsd32_long)dsp->shm_atime;
633 	ds32p->shm_dtime = (netbsd32_long)dsp->shm_dtime;
634 	ds32p->shm_ctime = (netbsd32_long)dsp->shm_ctime;
635 	NETBSD32PTR32(ds32p->_shm_internal, dsp->_shm_internal);
636 }
637 
638 static __inline void
netbsd32_to_semid_ds50(const struct netbsd32_semid_ds50 * s32dsp,struct semid_ds * dsp)639 netbsd32_to_semid_ds50(const struct netbsd32_semid_ds50 *s32dsp,
640     struct semid_ds *dsp)
641 {
642 
643 	netbsd32_to_ipc_perm(&s32dsp->sem_perm, &dsp->sem_perm);
644 	dsp->_sem_base = NETBSD32PTR64(s32dsp->_sem_base);
645 	dsp->sem_nsems = (time_t)s32dsp->sem_nsems;
646 	dsp->sem_otime = (time_t)s32dsp->sem_otime;
647 	dsp->sem_ctime = (time_t)s32dsp->sem_ctime;
648 }
649 
650 static __inline void
netbsd32_to_semid_ds(const struct netbsd32_semid_ds * s32dsp,struct semid_ds * dsp)651 netbsd32_to_semid_ds(const struct netbsd32_semid_ds *s32dsp,
652     struct semid_ds *dsp)
653 {
654 
655 	netbsd32_to_ipc_perm(&s32dsp->sem_perm, &dsp->sem_perm);
656 	dsp->_sem_base = NETBSD32PTR64(s32dsp->_sem_base);
657 	dsp->sem_nsems = s32dsp->sem_nsems;
658 	dsp->sem_otime = s32dsp->sem_otime;
659 	dsp->sem_ctime = s32dsp->sem_ctime;
660 }
661 
662 static __inline void
netbsd32_from_semid_ds50(const struct semid_ds * dsp,struct netbsd32_semid_ds50 * s32dsp)663 netbsd32_from_semid_ds50(const struct semid_ds *dsp,
664     struct netbsd32_semid_ds50 *s32dsp)
665 {
666 
667 	netbsd32_from_ipc_perm(&dsp->sem_perm, &s32dsp->sem_perm);
668 	NETBSD32PTR32(s32dsp->_sem_base, dsp->_sem_base);
669 	s32dsp->sem_nsems = (int32_t)dsp->sem_nsems;
670 	s32dsp->sem_otime = (int32_t)dsp->sem_otime;
671 	s32dsp->sem_ctime = (int32_t)dsp->sem_ctime;
672 }
673 
674 static __inline void
netbsd32_from_semid_ds(const struct semid_ds * dsp,struct netbsd32_semid_ds * s32dsp)675 netbsd32_from_semid_ds(const struct semid_ds *dsp,
676     struct netbsd32_semid_ds *s32dsp)
677 {
678 
679 	netbsd32_from_ipc_perm(&dsp->sem_perm, &s32dsp->sem_perm);
680 	NETBSD32PTR32(s32dsp->_sem_base, dsp->_sem_base);
681 	s32dsp->sem_nsems = dsp->sem_nsems;
682 	s32dsp->sem_otime = dsp->sem_otime;
683 	s32dsp->sem_ctime = dsp->sem_ctime;
684 }
685 
686 static __inline void
netbsd32_from_loadavg(struct netbsd32_loadavg * av32,const struct loadavg * av)687 netbsd32_from_loadavg(struct netbsd32_loadavg *av32,
688     const struct loadavg *av)
689 {
690 
691 	av32->ldavg[0] = av->ldavg[0];
692 	av32->ldavg[1] = av->ldavg[1];
693 	av32->ldavg[2] = av->ldavg[2];
694 	av32->fscale = (netbsd32_long)av->fscale;
695 }
696 
697 static __inline void
netbsd32_to_kevent(struct netbsd32_kevent * ke32,struct kevent * ke)698 netbsd32_to_kevent(struct netbsd32_kevent *ke32, struct kevent *ke)
699 {
700 	ke->ident = ke32->ident;
701 	ke->filter = ke32->filter;
702 	ke->flags = ke32->flags;
703 	ke->fflags = ke32->fflags;
704 	ke->data = ke32->data;
705 	ke->udata = ke32->udata;
706 }
707 
708 static __inline void
netbsd32_from_kevent(struct kevent * ke,struct netbsd32_kevent * ke32)709 netbsd32_from_kevent(struct kevent *ke, struct netbsd32_kevent *ke32)
710 {
711 	ke32->ident = ke->ident;
712 	ke32->filter = ke->filter;
713 	ke32->flags = ke->flags;
714 	ke32->fflags = ke->fflags;
715 	ke32->data = ke->data;
716 	ke32->udata = ke->udata;
717 }
718 
719 static __inline void
netbsd32_to_sigevent(const struct netbsd32_sigevent * ev32,struct sigevent * ev)720 netbsd32_to_sigevent(const struct netbsd32_sigevent *ev32, struct sigevent *ev)
721 {
722 	ev->sigev_notify = ev32->sigev_notify;
723 	ev->sigev_signo = ev32->sigev_signo;
724 	/*
725 	 * XXX sival_ptr, sigev_notify_function and
726 	 *     sigev_notify_attributes are  currently unused
727 	 */
728 	ev->sigev_value.sival_int = ev32->sigev_value.sival_int;
729 	ev->sigev_notify_function = NETBSD32PTR64(ev32->sigev_notify_function);
730 	ev->sigev_notify_attributes = NETBSD32PTR64(ev32->sigev_notify_attributes);
731 }
732 
733 static __inline int
netbsd32_to_dirent12(char * buf,int nbytes)734 netbsd32_to_dirent12(char *buf, int nbytes)
735 {
736 	struct dirent *ndp, *nndp, *endp;
737 	struct dirent12 *odp;
738 
739 	odp = (struct dirent12 *)(void *)buf;
740 	ndp = (struct dirent *)(void *)buf;
741 	endp = (struct dirent *)(void *)&buf[nbytes];
742 
743 	/*
744 	 * In-place conversion. This works because odp
745 	 * is smaller than ndp, but it has to be done
746 	 * in the right sequence.
747 	 */
748 	for (; ndp < endp; ndp = nndp) {
749 		nndp = _DIRENT_NEXT(ndp);
750 		odp->d_fileno = (u_int32_t)ndp->d_fileno;
751 		if (ndp->d_namlen >= sizeof(odp->d_name))
752 			odp->d_namlen = sizeof(odp->d_name) - 1;
753 		else
754 			odp->d_namlen = (u_int8_t)ndp->d_namlen;
755 		odp->d_type = ndp->d_type;
756 		(void)memcpy(odp->d_name, ndp->d_name, (size_t)odp->d_namlen);
757 		odp->d_name[odp->d_namlen] = '\0';
758 		odp->d_reclen = _DIRENT_SIZE(odp);
759 		odp = _DIRENT_NEXT(odp);
760 	}
761 	return ((char *)(void *)odp) - buf;
762 }
763 
764 static inline int
netbsd32_copyin_plistref(netbsd32_pointer_t n32p,struct plistref * p)765 netbsd32_copyin_plistref(netbsd32_pointer_t n32p, struct plistref *p)
766 {
767 	struct netbsd32_plistref n32plist;
768 	int error;
769 
770 	error = copyin(NETBSD32PTR64(n32p), &n32plist,
771 	    sizeof(struct netbsd32_plistref));
772 	if (error)
773 		return error;
774 	p->pref_plist = NETBSD32PTR64(n32plist.pref_plist);
775 	p->pref_len = n32plist.pref_len;
776 	return 0;
777 }
778 
779 static inline int
netbsd32_copyout_plistref(netbsd32_pointer_t n32p,struct plistref * p)780 netbsd32_copyout_plistref(netbsd32_pointer_t n32p, struct plistref *p)
781 {
782 	struct netbsd32_plistref n32plist;
783 
784 	NETBSD32PTR32(n32plist.pref_plist, p->pref_plist);
785 	n32plist.pref_len = p->pref_len;
786 	return copyout(&n32plist, NETBSD32PTR64(n32p),
787 	    sizeof(struct netbsd32_plistref));
788 }
789 
790 static __inline void
netbsd32_to_mq_attr(const struct netbsd32_mq_attr * a32,struct mq_attr * attr)791 netbsd32_to_mq_attr(const struct netbsd32_mq_attr *a32,
792     struct mq_attr *attr)
793 {
794 	attr->mq_flags = a32->mq_flags;
795 	attr->mq_maxmsg = a32->mq_maxmsg;
796 	attr->mq_msgsize = a32->mq_msgsize;
797 	attr->mq_curmsgs = a32->mq_curmsgs;
798 }
799 
800 static __inline void
netbsd32_from_mq_attr(const struct mq_attr * attr,struct netbsd32_mq_attr * a32)801 netbsd32_from_mq_attr(const struct mq_attr *attr,
802 	struct netbsd32_mq_attr *a32)
803 {
804 	a32->mq_flags = attr->mq_flags;
805 	a32->mq_maxmsg = attr->mq_maxmsg;
806 	a32->mq_msgsize = attr->mq_msgsize;
807 	a32->mq_curmsgs = attr->mq_curmsgs;
808 }
809 
810 #endif /* _COMPAT_NETBSD32_NETBSD32_CONV_H_ */
811