xref: /netbsd-src/sys/compat/netbsd32/netbsd32_conv.h (revision 53d1339bf7f9c7367b35a9e1ebe693f9b047a47b)
1 /*	$NetBSD: netbsd32_conv.h,v 1.45 2021/01/19 03:41:22 simonb 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 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/dirent.h>
36 #include <sys/ipc.h>
37 #include <sys/msg.h>
38 #define msg __msg /* Don't ask me! */
39 #include <sys/sem.h>
40 #include <sys/shm.h>
41 #include <sys/socket.h>
42 #include <sys/stat.h>
43 #include <sys/time.h>
44 #include <sys/timex.h>
45 #include <sys/event.h>
46 
47 #include <compat/sys/dirent.h>
48 
49 #include <prop/plistref.h>
50 
51 #include <nv.h>
52 
53 #include <compat/netbsd32/netbsd32.h>
54 
55 /* converters for structures that we need */
56 static __inline void
57 netbsd32_from_timeval50(const struct timeval *tv,
58     struct netbsd32_timeval50 *tv32)
59 {
60 
61 	tv32->tv_sec = (netbsd32_time50_t)tv->tv_sec;
62 	tv32->tv_usec = (netbsd32_long)tv->tv_usec;
63 }
64 
65 static __inline void
66 netbsd32_from_timeval(const struct timeval *tv,
67     struct netbsd32_timeval *tv32)
68 {
69 
70 	tv32->tv_sec = (netbsd32_time_t)tv->tv_sec;
71 	tv32->tv_usec = tv->tv_usec;
72 }
73 
74 static __inline void
75 netbsd32_to_timeval50(const struct netbsd32_timeval50 *tv32,
76     struct timeval *tv)
77 {
78 
79 	tv->tv_sec = (time_t)tv32->tv_sec;
80 	tv->tv_usec = tv32->tv_usec;
81 }
82 
83 static __inline void
84 netbsd32_to_timeval(const struct netbsd32_timeval *tv32,
85     struct timeval *tv)
86 {
87 
88 	tv->tv_sec = (time_t)tv32->tv_sec;
89 	tv->tv_usec = tv32->tv_usec;
90 }
91 
92 static __inline void
93 netbsd32_from_itimerval50(const struct itimerval *itv,
94     struct netbsd32_itimerval50 *itv32)
95 {
96 
97 	netbsd32_from_timeval50(&itv->it_interval,
98 			     &itv32->it_interval);
99 	netbsd32_from_timeval50(&itv->it_value,
100 			     &itv32->it_value);
101 }
102 
103 static __inline void
104 netbsd32_from_itimerval(const struct itimerval *itv,
105     struct netbsd32_itimerval *itv32)
106 {
107 
108 	netbsd32_from_timeval(&itv->it_interval,
109 			     &itv32->it_interval);
110 	netbsd32_from_timeval(&itv->it_value,
111 			     &itv32->it_value);
112 }
113 
114 static __inline void
115 netbsd32_to_itimerval50(const struct netbsd32_itimerval50 *itv32,
116     struct itimerval *itv)
117 {
118 
119 	netbsd32_to_timeval50(&itv32->it_interval, &itv->it_interval);
120 	netbsd32_to_timeval50(&itv32->it_value, &itv->it_value);
121 }
122 
123 static __inline void
124 netbsd32_to_itimerval(const struct netbsd32_itimerval *itv32,
125     struct itimerval *itv)
126 {
127 
128 	netbsd32_to_timeval(&itv32->it_interval, &itv->it_interval);
129 	netbsd32_to_timeval(&itv32->it_value, &itv->it_value);
130 }
131 
132 static __inline void
133 netbsd32_to_timespec50(const struct netbsd32_timespec50 *s32p,
134     struct timespec *p)
135 {
136 
137 	p->tv_sec = (time_t)s32p->tv_sec;
138 	p->tv_nsec = (long)s32p->tv_nsec;
139 }
140 
141 static __inline void
142 netbsd32_to_timespec(const struct netbsd32_timespec *s32p,
143     struct timespec *p)
144 {
145 
146 	p->tv_sec = (time_t)s32p->tv_sec;
147 	p->tv_nsec = (long)s32p->tv_nsec;
148 }
149 
150 static __inline void
151 netbsd32_from_timespec50(const struct timespec *p,
152     struct netbsd32_timespec50 *s32p)
153 {
154 
155 	s32p->tv_sec = (netbsd32_time50_t)p->tv_sec;
156 	s32p->tv_nsec = (netbsd32_long)p->tv_nsec;
157 }
158 
159 static __inline void
160 netbsd32_from_timespec(const struct timespec *p,
161     struct netbsd32_timespec *s32p)
162 {
163 
164 	s32p->tv_sec = (netbsd32_time_t)p->tv_sec;
165 	s32p->tv_nsec = (netbsd32_long)p->tv_nsec;
166 }
167 
168 static __inline void
169 netbsd32_from_rusage(const struct rusage *rup,
170     struct netbsd32_rusage *ru32p)
171 {
172 
173 	netbsd32_from_timeval(&rup->ru_utime, &ru32p->ru_utime);
174 	netbsd32_from_timeval(&rup->ru_stime, &ru32p->ru_stime);
175 #define C(var)	ru32p->var = (netbsd32_long)rup->var
176 	C(ru_maxrss);
177 	C(ru_ixrss);
178 	C(ru_idrss);
179 	C(ru_isrss);
180 	C(ru_minflt);
181 	C(ru_majflt);
182 	C(ru_nswap);
183 	C(ru_inblock);
184 	C(ru_oublock);
185 	C(ru_msgsnd);
186 	C(ru_msgrcv);
187 	C(ru_nsignals);
188 	C(ru_nvcsw);
189 	C(ru_nivcsw);
190 #undef C
191 }
192 
193 static __inline void
194 netbsd32_to_rusage(const struct netbsd32_rusage *ru32p,
195     struct rusage *rup)
196 {
197 
198 	netbsd32_to_timeval(&ru32p->ru_utime, &rup->ru_utime);
199 	netbsd32_to_timeval(&ru32p->ru_stime, &rup->ru_stime);
200 #define C(var)	rup->var = (long)ru32p->var
201 	C(ru_maxrss);
202 	C(ru_ixrss);
203 	C(ru_idrss);
204 	C(ru_isrss);
205 	C(ru_minflt);
206 	C(ru_majflt);
207 	C(ru_nswap);
208 	C(ru_inblock);
209 	C(ru_oublock);
210 	C(ru_msgsnd);
211 	C(ru_msgrcv);
212 	C(ru_nsignals);
213 	C(ru_nvcsw);
214 	C(ru_nivcsw);
215 #undef C
216 }
217 
218 static __inline void
219 netbsd32_from_rusage50(const struct rusage *rup,
220     struct netbsd32_rusage50 *ru32p)
221 {
222 
223 	netbsd32_from_timeval50(&rup->ru_utime, &ru32p->ru_utime);
224 	netbsd32_from_timeval50(&rup->ru_stime, &ru32p->ru_stime);
225 #define C(var)	ru32p->var = (netbsd32_long)rup->var
226 	C(ru_maxrss);
227 	C(ru_ixrss);
228 	C(ru_idrss);
229 	C(ru_isrss);
230 	C(ru_minflt);
231 	C(ru_majflt);
232 	C(ru_nswap);
233 	C(ru_inblock);
234 	C(ru_oublock);
235 	C(ru_msgsnd);
236 	C(ru_msgrcv);
237 	C(ru_nsignals);
238 	C(ru_nvcsw);
239 	C(ru_nivcsw);
240 #undef C
241 }
242 
243 static __inline int
244 netbsd32_to_iovecin(const struct netbsd32_iovec *iov32p, struct iovec *iovp,
245     int len)
246 {
247 	int i, error=0;
248 	uint32_t iov_base;
249 	uint32_t iov_len, total_iov_len;
250 
251 	/*
252 	 * We could allocate an iov32p, do a copyin, and translate
253 	 * each field and then free it all up, or we could copyin
254 	 * each field separately.  I'm doing the latter to reduce
255 	 * the number of MALLOC()s.
256 	 */
257 	total_iov_len = 0;
258 	for (i = 0; i < len; i++, iovp++, iov32p++) {
259 		if ((error = copyin(&iov32p->iov_base, &iov_base, sizeof(iov_base))))
260 		    return error;
261 		if ((error = copyin(&iov32p->iov_len, &iov_len, sizeof(iov_len))))
262 		    return error;
263 		iovp->iov_base = (void *)(u_long)iov_base;
264 		iovp->iov_len = (size_t)iov_len;
265 
266 		/*
267 		 * System calls return ssize_t because -1 is returned
268 		 * on error.  Therefore we must restrict the length to
269 		 * SSIZE_MAX (NETBSD32_SSIZE_MAX with compat32) to
270 		 * avoid garbage return values.
271 		 */
272 		total_iov_len += iov_len;
273 		if (iov_len > NETBSD32_SSIZE_MAX ||
274 		    total_iov_len > NETBSD32_SSIZE_MAX) {
275 			return EINVAL;
276 			break;
277 		}
278 	}
279 	return error;
280 }
281 
282 /* msg_iov must be done separately */
283 static __inline void
284 netbsd32_to_msghdr(const struct netbsd32_msghdr *mhp32, struct msghdr *mhp)
285 {
286 
287 	mhp->msg_name = NETBSD32PTR64(mhp32->msg_name);
288 	mhp->msg_namelen = mhp32->msg_namelen;
289 	mhp->msg_iovlen = (size_t)mhp32->msg_iovlen;
290 	mhp->msg_control = NETBSD32PTR64(mhp32->msg_control);
291 	mhp->msg_controllen = mhp32->msg_controllen;
292 	mhp->msg_flags = mhp32->msg_flags;
293 }
294 
295 /* msg_iov must be done separately */
296 static __inline void
297 netbsd32_from_msghdr(struct netbsd32_msghdr *mhp32, const struct msghdr *mhp)
298 {
299 
300 	NETBSD32PTR32(mhp32->msg_name, mhp->msg_name);
301 	mhp32->msg_namelen = mhp->msg_namelen;
302 	mhp32->msg_iovlen = mhp->msg_iovlen;
303 	NETBSD32PTR32(mhp32->msg_control, mhp->msg_control);
304 	mhp32->msg_controllen = mhp->msg_controllen;
305 	mhp32->msg_flags = mhp->msg_flags;
306 }
307 
308 static __inline void
309 netbsd32_to_mmsghdr(const struct netbsd32_mmsghdr *mmsg32,
310     struct mmsghdr *mmsg)
311 {
312     netbsd32_to_msghdr(&mmsg32->msg_hdr, &mmsg->msg_hdr);
313     mmsg->msg_len = mmsg32->msg_len;
314 }
315 
316 static __inline void
317 netbsd32_from_mmsghdr(struct netbsd32_mmsghdr *mmsg32,
318     const struct mmsghdr *mmsg)
319 {
320     netbsd32_from_msghdr(&mmsg32->msg_hdr, &mmsg->msg_hdr);
321     mmsg32->msg_len = mmsg->msg_len;
322 }
323 
324 static __inline void
325 netbsd32_from_statvfs90(const struct statvfs *sbp, struct netbsd32_statvfs90 *sb32p)
326 {
327 	sb32p->f_flag = sbp->f_flag;
328 	sb32p->f_bsize = (netbsd32_u_long)sbp->f_bsize;
329 	sb32p->f_frsize = (netbsd32_u_long)sbp->f_frsize;
330 	sb32p->f_iosize = (netbsd32_u_long)sbp->f_iosize;
331 	sb32p->f_blocks = sbp->f_blocks;
332 	sb32p->f_bfree = sbp->f_bfree;
333 	sb32p->f_bavail = sbp->f_bavail;
334 	sb32p->f_bresvd = sbp->f_bresvd;
335 	sb32p->f_files = sbp->f_files;
336 	sb32p->f_ffree = sbp->f_ffree;
337 	sb32p->f_favail = sbp->f_favail;
338 	sb32p->f_fresvd = sbp->f_fresvd;
339 	sb32p->f_syncreads = sbp->f_syncreads;
340 	sb32p->f_syncwrites = sbp->f_syncwrites;
341 	sb32p->f_asyncreads = sbp->f_asyncreads;
342 	sb32p->f_asyncwrites = sbp->f_asyncwrites;
343 	sb32p->f_fsidx = sbp->f_fsidx;
344 	sb32p->f_fsid = (netbsd32_u_long)sbp->f_fsid;
345 	sb32p->f_namemax = (netbsd32_u_long)sbp->f_namemax;
346 	sb32p->f_owner = sbp->f_owner;
347 	sb32p->f_spare[0] = 0;
348 	sb32p->f_spare[1] = 0;
349 	sb32p->f_spare[2] = 0;
350 	sb32p->f_spare[3] = 0;
351 #if 1
352 	/* May as well do the whole batch in one go */
353 	memcpy(sb32p->f_fstypename, sbp->f_fstypename,
354 	    sizeof(sb32p->f_fstypename) + sizeof(sb32p->f_mntonname) +
355 	    sizeof(sb32p->f_mntfromname));
356 #else
357 	/* If we want to be careful */
358 	memcpy(sb32p->f_fstypename, sbp->f_fstypename, sizeof(sb32p->f_fstypename));
359 	memcpy(sb32p->f_mntonname, sbp->f_mntonname, sizeof(sb32p->f_mntonname));
360 	memcpy(sb32p->f_mntfromname, sbp->f_mntfromname, sizeof(sb32p->f_mntfromname));
361 #endif
362 }
363 
364 static __inline void
365 netbsd32_from_statvfs(const struct statvfs *sbp, struct netbsd32_statvfs *sb32p)
366 {
367 	sb32p->f_flag = sbp->f_flag;
368 	sb32p->f_bsize = (netbsd32_u_long)sbp->f_bsize;
369 	sb32p->f_frsize = (netbsd32_u_long)sbp->f_frsize;
370 	sb32p->f_iosize = (netbsd32_u_long)sbp->f_iosize;
371 	sb32p->f_blocks = sbp->f_blocks;
372 	sb32p->f_bfree = sbp->f_bfree;
373 	sb32p->f_bavail = sbp->f_bavail;
374 	sb32p->f_bresvd = sbp->f_bresvd;
375 	sb32p->f_files = sbp->f_files;
376 	sb32p->f_ffree = sbp->f_ffree;
377 	sb32p->f_favail = sbp->f_favail;
378 	sb32p->f_fresvd = sbp->f_fresvd;
379 	sb32p->f_syncreads = sbp->f_syncreads;
380 	sb32p->f_syncwrites = sbp->f_syncwrites;
381 	sb32p->f_asyncreads = sbp->f_asyncreads;
382 	sb32p->f_asyncwrites = sbp->f_asyncwrites;
383 	sb32p->f_fsidx = sbp->f_fsidx;
384 	sb32p->f_fsid = (netbsd32_u_long)sbp->f_fsid;
385 	sb32p->f_namemax = (netbsd32_u_long)sbp->f_namemax;
386 	sb32p->f_owner = sbp->f_owner;
387 	sb32p->f_spare[0] = 0;
388 	sb32p->f_spare[1] = 0;
389 	sb32p->f_spare[2] = 0;
390 	sb32p->f_spare[3] = 0;
391 #if 1
392 	/* May as well do the whole batch in one go */
393 	memcpy(sb32p->f_fstypename, sbp->f_fstypename,
394 	    sizeof(sb32p->f_fstypename) + sizeof(sb32p->f_mntonname) +
395 	    sizeof(sb32p->f_mntfromname) + sizeof(sb32p->f_mntfromlabel));
396 #else
397 	/* If we want to be careful */
398 	memcpy(sb32p->f_fstypename, sbp->f_fstypename, sizeof(sb32p->f_fstypename));
399 	memcpy(sb32p->f_mntonname, sbp->f_mntonname, sizeof(sb32p->f_mntonname));
400 	memcpy(sb32p->f_mntfromname, sbp->f_mntfromname, sizeof(sb32p->f_mntfromname));
401 	memcpy(sb32p->f_mntfromlabel, sbp->f_mntfromlabel, sizeof(sb32p->f_mntfromlabel));
402 #endif
403 }
404 
405 static __inline void
406 netbsd32_from_timex(const struct timex *txp, struct netbsd32_timex *tx32p)
407 {
408 
409 	tx32p->modes = txp->modes;
410 	tx32p->offset = (netbsd32_long)txp->offset;
411 	tx32p->freq = (netbsd32_long)txp->freq;
412 	tx32p->maxerror = (netbsd32_long)txp->maxerror;
413 	tx32p->esterror = (netbsd32_long)txp->esterror;
414 	tx32p->status = txp->status;
415 	tx32p->constant = (netbsd32_long)txp->constant;
416 	tx32p->precision = (netbsd32_long)txp->precision;
417 	tx32p->tolerance = (netbsd32_long)txp->tolerance;
418 	tx32p->ppsfreq = (netbsd32_long)txp->ppsfreq;
419 	tx32p->jitter = (netbsd32_long)txp->jitter;
420 	tx32p->shift = txp->shift;
421 	tx32p->stabil = (netbsd32_long)txp->stabil;
422 	tx32p->jitcnt = (netbsd32_long)txp->jitcnt;
423 	tx32p->calcnt = (netbsd32_long)txp->calcnt;
424 	tx32p->errcnt = (netbsd32_long)txp->errcnt;
425 	tx32p->stbcnt = (netbsd32_long)txp->stbcnt;
426 }
427 
428 static __inline void
429 netbsd32_to_timex(const struct netbsd32_timex *tx32p, struct timex *txp)
430 {
431 
432 	txp->modes = tx32p->modes;
433 	txp->offset = (long)tx32p->offset;
434 	txp->freq = (long)tx32p->freq;
435 	txp->maxerror = (long)tx32p->maxerror;
436 	txp->esterror = (long)tx32p->esterror;
437 	txp->status = tx32p->status;
438 	txp->constant = (long)tx32p->constant;
439 	txp->precision = (long)tx32p->precision;
440 	txp->tolerance = (long)tx32p->tolerance;
441 	txp->ppsfreq = (long)tx32p->ppsfreq;
442 	txp->jitter = (long)tx32p->jitter;
443 	txp->shift = tx32p->shift;
444 	txp->stabil = (long)tx32p->stabil;
445 	txp->jitcnt = (long)tx32p->jitcnt;
446 	txp->calcnt = (long)tx32p->calcnt;
447 	txp->errcnt = (long)tx32p->errcnt;
448 	txp->stbcnt = (long)tx32p->stbcnt;
449 }
450 
451 static __inline void
452 netbsd32_from___stat13(const struct stat *sbp, struct netbsd32_stat13 *sb32p)
453 {
454 	memset(sb32p, 0, sizeof *sb32p);
455 	sb32p->st_dev = (uint32_t)sbp->st_dev;
456 	sb32p->st_ino = sbp->st_ino;
457 	sb32p->st_mode = sbp->st_mode;
458 	sb32p->st_nlink = sbp->st_nlink;
459 	sb32p->st_uid = sbp->st_uid;
460 	sb32p->st_gid = sbp->st_gid;
461 	sb32p->st_rdev = (uint32_t)sbp->st_rdev;
462 	sb32p->st_size = sbp->st_size;
463 	sb32p->st_atimespec.tv_sec = (int32_t)sbp->st_atimespec.tv_sec;
464 	sb32p->st_atimespec.tv_nsec = (netbsd32_long)sbp->st_atimespec.tv_nsec;
465 	sb32p->st_mtimespec.tv_sec = (int32_t)sbp->st_mtimespec.tv_sec;
466 	sb32p->st_mtimespec.tv_nsec = (netbsd32_long)sbp->st_mtimespec.tv_nsec;
467 	sb32p->st_ctimespec.tv_sec = (int32_t)sbp->st_ctimespec.tv_sec;
468 	sb32p->st_ctimespec.tv_nsec = (netbsd32_long)sbp->st_ctimespec.tv_nsec;
469 	sb32p->st_blksize = sbp->st_blksize;
470 	sb32p->st_blocks = sbp->st_blocks;
471 	sb32p->st_flags = sbp->st_flags;
472 	sb32p->st_gen = sbp->st_gen;
473 	sb32p->st_birthtimespec.tv_sec = (int32_t)sbp->st_birthtimespec.tv_sec;
474 	sb32p->st_birthtimespec.tv_nsec = (netbsd32_long)sbp->st_birthtimespec.tv_nsec;
475 }
476 
477 static __inline void
478 netbsd32_from___stat50(const struct stat *sbp, struct netbsd32_stat50 *sb32p)
479 {
480 	memset(sb32p, 0, sizeof *sb32p);
481 	sb32p->st_dev = (uint32_t)sbp->st_dev;
482 	sb32p->st_ino = sbp->st_ino;
483 	sb32p->st_mode = sbp->st_mode;
484 	sb32p->st_nlink = sbp->st_nlink;
485 	sb32p->st_uid = sbp->st_uid;
486 	sb32p->st_gid = sbp->st_gid;
487 	sb32p->st_rdev = (uint32_t)sbp->st_rdev;
488 	sb32p->st_size = sbp->st_size;
489 	sb32p->st_atimespec.tv_sec = (int32_t)sbp->st_atimespec.tv_sec;
490 	sb32p->st_atimespec.tv_nsec = (netbsd32_long)sbp->st_atimespec.tv_nsec;
491 	sb32p->st_mtimespec.tv_sec = (int32_t)sbp->st_mtimespec.tv_sec;
492 	sb32p->st_mtimespec.tv_nsec = (netbsd32_long)sbp->st_mtimespec.tv_nsec;
493 	sb32p->st_ctimespec.tv_sec = (int32_t)sbp->st_ctimespec.tv_sec;
494 	sb32p->st_ctimespec.tv_nsec = (netbsd32_long)sbp->st_ctimespec.tv_nsec;
495 	sb32p->st_birthtimespec.tv_sec = (int32_t)sbp->st_birthtimespec.tv_sec;
496 	sb32p->st_birthtimespec.tv_nsec = (netbsd32_long)sbp->st_birthtimespec.tv_nsec;
497 	sb32p->st_blksize = sbp->st_blksize;
498 	sb32p->st_blocks = sbp->st_blocks;
499 	sb32p->st_flags = sbp->st_flags;
500 	sb32p->st_gen = sbp->st_gen;
501 }
502 
503 static __inline void
504 netbsd32_from_stat(const struct stat *sbp, struct netbsd32_stat *sb32p)
505 {
506 	memset(sb32p, 0, sizeof *sb32p);
507 	sb32p->st_dev = sbp->st_dev;
508 	sb32p->st_ino = sbp->st_ino;
509 	sb32p->st_mode = sbp->st_mode;
510 	sb32p->st_nlink = sbp->st_nlink;
511 	sb32p->st_uid = sbp->st_uid;
512 	sb32p->st_gid = sbp->st_gid;
513 	sb32p->st_rdev = sbp->st_rdev;
514 	sb32p->st_size = sbp->st_size;
515 	sb32p->st_atimespec.tv_sec = (netbsd32_time_t)sbp->st_atimespec.tv_sec;
516 	sb32p->st_atimespec.tv_nsec = (netbsd32_long)sbp->st_atimespec.tv_nsec;
517 	sb32p->st_mtimespec.tv_sec = (netbsd32_time_t)sbp->st_mtimespec.tv_sec;
518 	sb32p->st_mtimespec.tv_nsec = (netbsd32_long)sbp->st_mtimespec.tv_nsec;
519 	sb32p->st_ctimespec.tv_sec = (netbsd32_time_t)sbp->st_ctimespec.tv_sec;
520 	sb32p->st_ctimespec.tv_nsec = (netbsd32_long)sbp->st_ctimespec.tv_nsec;
521 	sb32p->st_birthtimespec.tv_sec = (netbsd32_time_t)sbp->st_birthtimespec.tv_sec;
522 	sb32p->st_birthtimespec.tv_nsec = (netbsd32_long)sbp->st_birthtimespec.tv_nsec;
523 	sb32p->st_blksize = sbp->st_blksize;
524 	sb32p->st_blocks = sbp->st_blocks;
525 	sb32p->st_flags = sbp->st_flags;
526 	sb32p->st_gen = sbp->st_gen;
527 }
528 
529 static __inline void
530 netbsd32_to_ipc_perm(const struct netbsd32_ipc_perm *ip32p,
531     struct ipc_perm *ipp)
532 {
533 
534 	ipp->cuid = ip32p->cuid;
535 	ipp->cgid = ip32p->cgid;
536 	ipp->uid = ip32p->uid;
537 	ipp->gid = ip32p->gid;
538 	ipp->mode = ip32p->mode;
539 	ipp->_seq = ip32p->_seq;
540 	ipp->_key = (key_t)ip32p->_key;
541 }
542 
543 static __inline void
544 netbsd32_from_ipc_perm(const struct ipc_perm *ipp,
545     struct netbsd32_ipc_perm *ip32p)
546 {
547 
548 	memset(ip32p, 0, sizeof *ip32p);
549 	ip32p->cuid = ipp->cuid;
550 	ip32p->cgid = ipp->cgid;
551 	ip32p->uid = ipp->uid;
552 	ip32p->gid = ipp->gid;
553 	ip32p->mode = ipp->mode;
554 	ip32p->_seq = ipp->_seq;
555 	ip32p->_key = (netbsd32_key_t)ipp->_key;
556 }
557 
558 static __inline void
559 netbsd32_to_msg(const struct netbsd32_msg *m32p, struct msg *mp)
560 {
561 
562 	mp->msg_next = NETBSD32PTR64(m32p->msg_next);
563 	mp->msg_type = (long)m32p->msg_type;
564 	mp->msg_ts = m32p->msg_ts;
565 	mp->msg_spot = m32p->msg_spot;
566 }
567 
568 static __inline void
569 netbsd32_from_msg(const struct msg *mp, struct netbsd32_msg *m32p)
570 {
571 
572 	memset(m32p, 0, sizeof *m32p);
573 	NETBSD32PTR32(m32p->msg_next, mp->msg_next);
574 	m32p->msg_type = (netbsd32_long)mp->msg_type;
575 	m32p->msg_ts = mp->msg_ts;
576 	m32p->msg_spot = mp->msg_spot;
577 }
578 
579 static __inline void
580 netbsd32_to_msqid_ds50(const struct netbsd32_msqid_ds50 *ds32p,
581     struct msqid_ds *dsp)
582 {
583 
584 	netbsd32_to_ipc_perm(&ds32p->msg_perm, &dsp->msg_perm);
585 	dsp->_msg_cbytes = (u_long)ds32p->_msg_cbytes;
586 	dsp->msg_qnum = (u_long)ds32p->msg_qnum;
587 	dsp->msg_qbytes = (u_long)ds32p->msg_qbytes;
588 	dsp->msg_lspid = ds32p->msg_lspid;
589 	dsp->msg_lrpid = ds32p->msg_lrpid;
590 	dsp->msg_rtime = (time_t)ds32p->msg_rtime;
591 	dsp->msg_stime = (time_t)ds32p->msg_stime;
592 	dsp->msg_ctime = (time_t)ds32p->msg_ctime;
593 }
594 
595 static __inline void
596 netbsd32_to_msqid_ds(const struct netbsd32_msqid_ds *ds32p,
597     struct msqid_ds *dsp)
598 {
599 
600 	netbsd32_to_ipc_perm(&ds32p->msg_perm, &dsp->msg_perm);
601 	dsp->_msg_cbytes = (u_long)ds32p->_msg_cbytes;
602 	dsp->msg_qnum = (u_long)ds32p->msg_qnum;
603 	dsp->msg_qbytes = (u_long)ds32p->msg_qbytes;
604 	dsp->msg_lspid = ds32p->msg_lspid;
605 	dsp->msg_lrpid = ds32p->msg_lrpid;
606 	dsp->msg_rtime = (time_t)ds32p->msg_rtime;
607 	dsp->msg_stime = (time_t)ds32p->msg_stime;
608 	dsp->msg_ctime = (time_t)ds32p->msg_ctime;
609 }
610 
611 static __inline void
612 netbsd32_from_msqid_ds50(const struct msqid_ds *dsp,
613     struct netbsd32_msqid_ds50 *ds32p)
614 {
615 
616 	memset(ds32p, 0, sizeof *ds32p);
617 	netbsd32_from_ipc_perm(&dsp->msg_perm, &ds32p->msg_perm);
618 	ds32p->_msg_cbytes = (netbsd32_u_long)dsp->_msg_cbytes;
619 	ds32p->msg_qnum = (netbsd32_u_long)dsp->msg_qnum;
620 	ds32p->msg_qbytes = (netbsd32_u_long)dsp->msg_qbytes;
621 	ds32p->msg_lspid = dsp->msg_lspid;
622 	ds32p->msg_lrpid = dsp->msg_lrpid;
623 	ds32p->msg_rtime = (int32_t)dsp->msg_rtime;
624 	ds32p->msg_stime = (int32_t)dsp->msg_stime;
625 	ds32p->msg_ctime = (int32_t)dsp->msg_ctime;
626 }
627 
628 static __inline void
629 netbsd32_from_msqid_ds(const struct msqid_ds *dsp,
630     struct netbsd32_msqid_ds *ds32p)
631 {
632 
633 	memset(ds32p, 0, sizeof *ds32p);
634 	netbsd32_from_ipc_perm(&dsp->msg_perm, &ds32p->msg_perm);
635 	ds32p->_msg_cbytes = (netbsd32_u_long)dsp->_msg_cbytes;
636 	ds32p->msg_qnum = (netbsd32_u_long)dsp->msg_qnum;
637 	ds32p->msg_qbytes = (netbsd32_u_long)dsp->msg_qbytes;
638 	ds32p->msg_lspid = dsp->msg_lspid;
639 	ds32p->msg_lrpid = dsp->msg_lrpid;
640 	ds32p->msg_rtime = dsp->msg_rtime;
641 	ds32p->msg_stime = dsp->msg_stime;
642 	ds32p->msg_ctime = dsp->msg_ctime;
643 }
644 
645 static __inline void
646 netbsd32_to_shmid_ds50(const struct netbsd32_shmid_ds50 *ds32p,
647     struct shmid_ds *dsp)
648 {
649 
650 	netbsd32_to_ipc_perm(&ds32p->shm_perm, &dsp->shm_perm);
651 	dsp->shm_segsz = ds32p->shm_segsz;
652 	dsp->shm_lpid = ds32p->shm_lpid;
653 	dsp->shm_cpid = ds32p->shm_cpid;
654 	dsp->shm_nattch = ds32p->shm_nattch;
655 	dsp->shm_atime = (time_t)ds32p->shm_atime;
656 	dsp->shm_dtime = (time_t)ds32p->shm_dtime;
657 	dsp->shm_ctime = (time_t)ds32p->shm_ctime;
658 }
659 
660 static __inline void
661 netbsd32_to_shmid_ds(const struct netbsd32_shmid_ds *ds32p,
662     struct shmid_ds *dsp)
663 {
664 
665 	netbsd32_to_ipc_perm(&ds32p->shm_perm, &dsp->shm_perm);
666 	dsp->shm_segsz = ds32p->shm_segsz;
667 	dsp->shm_lpid = ds32p->shm_lpid;
668 	dsp->shm_cpid = ds32p->shm_cpid;
669 	dsp->shm_nattch = ds32p->shm_nattch;
670 	dsp->shm_atime = (long)ds32p->shm_atime;
671 	dsp->shm_dtime = (time_t)ds32p->shm_dtime;
672 	dsp->shm_ctime = (time_t)ds32p->shm_ctime;
673 }
674 
675 static __inline void
676 netbsd32_from_shmid_ds50(const struct shmid_ds *dsp,
677     struct netbsd32_shmid_ds50 *ds32p)
678 {
679 
680 	memset(ds32p, 0, sizeof *ds32p);
681 	netbsd32_from_ipc_perm(&dsp->shm_perm, &ds32p->shm_perm);
682 	ds32p->shm_segsz = dsp->shm_segsz;
683 	ds32p->shm_lpid = dsp->shm_lpid;
684 	ds32p->shm_cpid = dsp->shm_cpid;
685 	ds32p->shm_nattch = dsp->shm_nattch;
686 	ds32p->shm_atime = (int32_t)dsp->shm_atime;
687 	ds32p->shm_dtime = (int32_t)dsp->shm_dtime;
688 	ds32p->shm_ctime = (int32_t)dsp->shm_ctime;
689 }
690 
691 static __inline void
692 netbsd32_from_shmid_ds(const struct shmid_ds *dsp,
693     struct netbsd32_shmid_ds *ds32p)
694 {
695 
696 	memset(ds32p, 0, sizeof *ds32p);
697 	netbsd32_from_ipc_perm(&dsp->shm_perm, &ds32p->shm_perm);
698 	ds32p->shm_segsz = dsp->shm_segsz;
699 	ds32p->shm_lpid = dsp->shm_lpid;
700 	ds32p->shm_cpid = dsp->shm_cpid;
701 	ds32p->shm_nattch = dsp->shm_nattch;
702 	ds32p->shm_atime = (netbsd32_long)dsp->shm_atime;
703 	ds32p->shm_dtime = (netbsd32_long)dsp->shm_dtime;
704 	ds32p->shm_ctime = (netbsd32_long)dsp->shm_ctime;
705 }
706 
707 static __inline void
708 netbsd32_to_semid_ds50(const struct netbsd32_semid_ds50 *s32dsp,
709     struct semid_ds *dsp)
710 {
711 
712 	netbsd32_to_ipc_perm(&s32dsp->sem_perm, &dsp->sem_perm);
713 	dsp->sem_nsems = (time_t)s32dsp->sem_nsems;
714 	dsp->sem_otime = (time_t)s32dsp->sem_otime;
715 	dsp->sem_ctime = (time_t)s32dsp->sem_ctime;
716 }
717 
718 static __inline void
719 netbsd32_to_semid_ds(const struct netbsd32_semid_ds *s32dsp,
720     struct semid_ds *dsp)
721 {
722 
723 	netbsd32_to_ipc_perm(&s32dsp->sem_perm, &dsp->sem_perm);
724 	dsp->sem_nsems = s32dsp->sem_nsems;
725 	dsp->sem_otime = s32dsp->sem_otime;
726 	dsp->sem_ctime = s32dsp->sem_ctime;
727 }
728 
729 static __inline void
730 netbsd32_from_semid_ds50(const struct semid_ds *dsp,
731     struct netbsd32_semid_ds50 *s32dsp)
732 {
733 
734 	memset(s32dsp, 0, sizeof *s32dsp);
735 	netbsd32_from_ipc_perm(&dsp->sem_perm, &s32dsp->sem_perm);
736 	s32dsp->sem_nsems = (int32_t)dsp->sem_nsems;
737 	s32dsp->sem_otime = (int32_t)dsp->sem_otime;
738 	s32dsp->sem_ctime = (int32_t)dsp->sem_ctime;
739 }
740 
741 static __inline void
742 netbsd32_from_semid_ds(const struct semid_ds *dsp,
743     struct netbsd32_semid_ds *s32dsp)
744 {
745 
746 	memset(s32dsp, 0, sizeof *s32dsp);
747 	netbsd32_from_ipc_perm(&dsp->sem_perm, &s32dsp->sem_perm);
748 	s32dsp->sem_nsems = dsp->sem_nsems;
749 	s32dsp->sem_otime = dsp->sem_otime;
750 	s32dsp->sem_ctime = dsp->sem_ctime;
751 }
752 
753 static __inline void
754 netbsd32_from_loadavg(struct netbsd32_loadavg *av32,
755     const struct loadavg *av)
756 {
757 
758 	av32->ldavg[0] = av->ldavg[0];
759 	av32->ldavg[1] = av->ldavg[1];
760 	av32->ldavg[2] = av->ldavg[2];
761 	av32->fscale = (netbsd32_long)av->fscale;
762 }
763 
764 static __inline void
765 netbsd32_to_kevent(struct netbsd32_kevent *ke32, struct kevent *ke)
766 {
767 	ke->ident = ke32->ident;
768 	ke->filter = ke32->filter;
769 	ke->flags = ke32->flags;
770 	ke->fflags = ke32->fflags;
771 	ke->data = ke32->data;
772 	ke->udata = NETBSD32PTR64(ke32->udata);
773 }
774 
775 static __inline void
776 netbsd32_from_kevent(struct kevent *ke, struct netbsd32_kevent *ke32)
777 {
778 	ke32->ident = ke->ident;
779 	ke32->filter = ke->filter;
780 	ke32->flags = ke->flags;
781 	ke32->fflags = ke->fflags;
782 	ke32->data = ke->data;
783 	NETBSD32PTR32(ke32->udata, ke->udata);
784 }
785 
786 static __inline void
787 netbsd32_to_sigevent(const struct netbsd32_sigevent *ev32, struct sigevent *ev)
788 {
789 	ev->sigev_notify = ev32->sigev_notify;
790 	ev->sigev_signo = ev32->sigev_signo;
791 	/*
792 	 * XXX sival_ptr, sigev_notify_function and
793 	 *     sigev_notify_attributes are  currently unused
794 	 */
795 	ev->sigev_value.sival_int = ev32->sigev_value.sival_int;
796 	ev->sigev_notify_function = NETBSD32PTR64(ev32->sigev_notify_function);
797 	ev->sigev_notify_attributes = NETBSD32PTR64(ev32->sigev_notify_attributes);
798 }
799 
800 static __inline int
801 netbsd32_to_dirent12(char *buf, int nbytes)
802 {
803 	struct dirent *ndp, *nndp, *endp;
804 	struct dirent12 *odp;
805 
806 	odp = (struct dirent12 *)(void *)buf;
807 	ndp = (struct dirent *)(void *)buf;
808 	endp = (struct dirent *)(void *)&buf[nbytes];
809 
810 	/*
811 	 * In-place conversion. This works because odp
812 	 * is smaller than ndp, but it has to be done
813 	 * in the right sequence.
814 	 */
815 	for (; ndp < endp; ndp = nndp) {
816 		nndp = _DIRENT_NEXT(ndp);
817 		odp->d_fileno = (uint32_t)ndp->d_fileno;
818 		if (ndp->d_namlen >= sizeof(odp->d_name))
819 			odp->d_namlen = sizeof(odp->d_name) - 1;
820 		else
821 			odp->d_namlen = (uint8_t)ndp->d_namlen;
822 		odp->d_type = ndp->d_type;
823 		(void)memcpy(odp->d_name, ndp->d_name, (size_t)odp->d_namlen);
824 		odp->d_name[odp->d_namlen] = '\0';
825 		odp->d_reclen = _DIRENT_SIZE(odp);
826 		odp = _DIRENT_NEXT(odp);
827 	}
828 	return ((char *)(void *)odp) - buf;
829 }
830 
831 static __inline int
832 netbsd32_copyin_plistref(netbsd32_pointer_t n32p, struct plistref *p)
833 {
834 	struct netbsd32_plistref n32plist;
835 	int error;
836 
837 	error = copyin(NETBSD32PTR64(n32p), &n32plist,
838 	    sizeof(struct netbsd32_plistref));
839 	if (error)
840 		return error;
841 	p->pref_plist = NETBSD32PTR64(n32plist.pref_plist);
842 	p->pref_len = n32plist.pref_len;
843 	return 0;
844 }
845 
846 static __inline int
847 netbsd32_copyout_plistref(netbsd32_pointer_t n32p, struct plistref *p)
848 {
849 	struct netbsd32_plistref n32plist;
850 
851 	NETBSD32PTR32(n32plist.pref_plist, p->pref_plist);
852 	n32plist.pref_len = p->pref_len;
853 	return copyout(&n32plist, NETBSD32PTR64(n32p),
854 	    sizeof(struct netbsd32_plistref));
855 }
856 
857 static __inline int
858 netbsd32_copyin_nvlist_ref_t(netbsd32_pointer_t n32p, nvlist_ref_t *p)
859 {
860 	netbsd32_nvlist_ref_t n32nv;
861 	int error;
862 
863 	error = copyin(NETBSD32PTR64(n32p), &n32nv,
864 	    sizeof(netbsd32_nvlist_ref_t));
865 	if (error)
866 		return error;
867 	p->buf = NETBSD32PTR64(n32nv.buf);
868 	p->len = n32nv.len;
869 	p->flags = n32nv.flags;
870 	return 0;
871 }
872 
873 static __inline int
874 netbsd32_copyout_nvlist_ref_t(netbsd32_pointer_t n32p, nvlist_ref_t *p)
875 {
876 	netbsd32_nvlist_ref_t n32nv;
877 
878 	NETBSD32PTR32(n32nv.buf, p->buf);
879 	n32nv.len = p->len;
880 	n32nv.flags = p->flags;
881 	return copyout(&n32nv, NETBSD32PTR64(n32p),
882 	    sizeof(netbsd32_nvlist_ref_t));
883 }
884 
885 static __inline void
886 netbsd32_to_mq_attr(const struct netbsd32_mq_attr *a32,
887     struct mq_attr *attr)
888 {
889 	attr->mq_flags = a32->mq_flags;
890 	attr->mq_maxmsg = a32->mq_maxmsg;
891 	attr->mq_msgsize = a32->mq_msgsize;
892 	attr->mq_curmsgs = a32->mq_curmsgs;
893 }
894 
895 static __inline void
896 netbsd32_from_mq_attr(const struct mq_attr *attr,
897 	struct netbsd32_mq_attr *a32)
898 {
899 	a32->mq_flags = attr->mq_flags;
900 	a32->mq_maxmsg = attr->mq_maxmsg;
901 	a32->mq_msgsize = attr->mq_msgsize;
902 	a32->mq_curmsgs = attr->mq_curmsgs;
903 }
904 
905 #endif /* _COMPAT_NETBSD32_NETBSD32_CONV_H_ */
906