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