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