Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_ioctl.c revision 1.92.2.3
      1 /*	$NetBSD: netbsd32_ioctl.c,v 1.92.2.3 2020/04/13 08:04:16 martin 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 /*
     30  * handle ioctl conversions from netbsd32 -> 64-bit kernel
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: netbsd32_ioctl.c,v 1.92.2.3 2020/04/13 08:04:16 martin Exp $");
     35 
     36 #if defined(_KERNEL_OPT)
     37 #include "opt_ntp.h"
     38 #endif
     39 
     40 #include <sys/param.h>
     41 #include <sys/atomic.h>
     42 #include <sys/systm.h>
     43 #include <sys/filedesc.h>
     44 #include <sys/ioctl.h>
     45 #include <sys/file.h>
     46 #include <sys/proc.h>
     47 #include <sys/socketvar.h>
     48 #include <sys/audioio.h>
     49 #include <sys/disklabel.h>
     50 #include <sys/dkio.h>
     51 #include <sys/ataio.h>
     52 #include <sys/sockio.h>
     53 #include <sys/socket.h>
     54 #include <sys/ttycom.h>
     55 #include <sys/mount.h>
     56 #include <sys/syscallargs.h>
     57 #include <sys/ktrace.h>
     58 #include <sys/kmem.h>
     59 #include <sys/envsys.h>
     60 #include <sys/wdog.h>
     61 #include <sys/clockctl.h>
     62 #include <sys/exec_elf.h>
     63 #include <sys/ksyms.h>
     64 #include <sys/drvctlio.h>
     65 #include <sys/compat_stub.h>
     66 
     67 #include <sys/vnode.h>
     68 #include <sys/conf.h>
     69 #include <miscfs/specfs/specdev.h>
     70 
     71 #ifdef __sparc__
     72 #include <dev/sun/fbio.h>
     73 #include <machine/openpromio.h>
     74 #endif
     75 
     76 #include <net/if.h>
     77 #include <net/route.h>
     78 
     79 #include <net/if_pppoe.h>
     80 #include <net/if_sppp.h>
     81 
     82 #include <net/bpf.h>
     83 #include <netinet/in.h>
     84 #include <netinet/in_var.h>
     85 #include <netinet/igmp.h>
     86 #include <netinet/igmp_var.h>
     87 #include <netinet/ip_mroute.h>
     88 
     89 #include <compat/sys/sockio.h>
     90 
     91 #include <compat/netbsd32/netbsd32.h>
     92 #include <compat/netbsd32/netbsd32_ioctl.h>
     93 #include <compat/netbsd32/netbsd32_syscallargs.h>
     94 #include <compat/netbsd32/netbsd32_conv.h>
     95 
     96 #include <dev/vndvar.h>
     97 
     98 /* convert to/from different structures */
     99 
    100 #if 0
    101 static inline void
    102 netbsd32_to_format_op(struct netbsd32_format_op *s32p,
    103     struct format_op *p, u_long cmd)
    104 {
    105 
    106 	p->df_buf = (char *)NETBSD32PTR64(s32p->df_buf);
    107 	p->df_count = s32p->df_count;
    108 	p->df_startblk = s32p->df_startblk;
    109 	memcpy(p->df_reg, s32p->df_reg, sizeof(s32p->df_reg));
    110 }
    111 #endif
    112 
    113 static inline void
    114 netbsd32_to_ifreq(struct netbsd32_ifreq *s32p, struct ifreq *p, u_long cmd)
    115 {
    116 
    117 	memcpy(p, s32p, sizeof *s32p);
    118 	/*
    119 	 * XXX
    120 	 * struct ifreq says the same, but sometimes the ifr_data
    121 	 * union member needs to be converted to 64 bits... this
    122 	 * is very driver specific and so we ignore it for now..
    123 	 */
    124 	switch (cmd) {
    125 	case SIOCGIFDATA:
    126 	case SIOCZIFDATA:
    127 	case SIOCGIFGENERIC:
    128 	case SIOCSIFGENERIC:
    129 		p->ifr_data = (void *)NETBSD32PTR64(s32p->ifr_data);
    130 		break;
    131 	}
    132 }
    133 
    134 static inline void
    135 netbsd32_to_oifreq(struct netbsd32_oifreq *s32p, struct oifreq *p, u_long cmd)
    136 {
    137 
    138 	memcpy(p, s32p, sizeof *s32p);
    139 	/*
    140 	 * XXX
    141 	 * struct ifreq says the same, but sometimes the ifr_data
    142 	 * union member needs to be converted to 64 bits... this
    143 	 * is very driver specific and so we ignore it for now..
    144 	 */
    145 	if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
    146 		p->ifr_data = (void *)NETBSD32PTR64(s32p->ifr_data);
    147 }
    148 
    149 static inline void
    150 netbsd32_to_if_addrprefreq(const struct netbsd32_if_addrprefreq *ifap32,
    151     struct if_addrprefreq *ifap, u_long cmd)
    152 {
    153 
    154 	memcpy(ifap->ifap_name, ifap32->ifap_name, IFNAMSIZ);
    155 	ifap->ifap_preference = ifap32->ifap_preference;
    156 	memcpy(&ifap->ifap_addr, &ifap32->ifap_addr,
    157 	    uimin(ifap32->ifap_addr.ss_len, _SS_MAXSIZE));
    158 }
    159 
    160 static inline void
    161 netbsd32_to_ifconf(struct netbsd32_ifconf *s32p, struct ifconf *p, u_long cmd)
    162 {
    163 
    164 	p->ifc_len = s32p->ifc_len;
    165 	/* ifc_buf & ifc_req are the same size so this works */
    166 	p->ifc_buf = (void *)NETBSD32PTR64(s32p->ifc_buf);
    167 }
    168 
    169 static inline void
    170 netbsd32_to_ifmediareq(struct netbsd32_ifmediareq *s32p,
    171     struct ifmediareq *p, u_long cmd)
    172 {
    173 
    174 	memcpy(p, s32p, sizeof *s32p);
    175 	p->ifm_ulist = (int *)NETBSD32PTR64(s32p->ifm_ulist);
    176 }
    177 
    178 static inline void
    179 netbsd32_to_pppoediscparms(struct netbsd32_pppoediscparms *s32p,
    180     struct pppoediscparms *p, u_long cmd)
    181 {
    182 
    183 	memcpy(p->ifname, s32p->ifname, sizeof p->ifname);
    184 	memcpy(p->eth_ifname, s32p->eth_ifname, sizeof p->eth_ifname);
    185 	p->ac_name = (char *)NETBSD32PTR64(s32p->ac_name);
    186 	p->ac_name_len = s32p->ac_name_len;
    187 	p->service_name = (char *)NETBSD32PTR64(s32p->service_name);
    188 	p->service_name_len = s32p->service_name_len;
    189 }
    190 
    191 static inline void
    192 netbsd32_to_spppauthcfg(struct netbsd32_spppauthcfg *s32p,
    193     struct spppauthcfg *p, u_long cmd)
    194 {
    195 
    196 	memcpy(p->ifname, s32p->ifname, sizeof p->ifname);
    197 	p->hisauth = s32p->hisauth;
    198 	p->myauth = s32p->myauth;
    199 	p->myname_length = s32p->myname_length;
    200 	p->mysecret_length = s32p->mysecret_length;
    201 	p->hisname_length = s32p->hisname_length;
    202 	p->hissecret_length = s32p->hissecret_length;
    203 	p->myauthflags = s32p->myauthflags;
    204 	p->hisauthflags = s32p->hisauthflags;
    205 	p->myname = (char *)NETBSD32PTR64(s32p->myname);
    206 	p->mysecret = (char *)NETBSD32PTR64(s32p->mysecret);
    207 	p->hisname = (char *)NETBSD32PTR64(s32p->hisname);
    208 	p->hissecret = (char *)NETBSD32PTR64(s32p->hissecret);
    209 }
    210 
    211 static inline void
    212 netbsd32_to_ifdrv(struct netbsd32_ifdrv *s32p, struct ifdrv *p, u_long cmd)
    213 {
    214 
    215 	memcpy(p->ifd_name, s32p->ifd_name, sizeof p->ifd_name);
    216 	p->ifd_cmd = s32p->ifd_cmd;
    217 	p->ifd_len = s32p->ifd_len;
    218 	p->ifd_data = (void *)NETBSD32PTR64(s32p->ifd_data);
    219 }
    220 
    221 static inline void
    222 netbsd32_to_sioc_vif_req(struct netbsd32_sioc_vif_req *s32p,
    223     struct sioc_vif_req *p, u_long cmd)
    224 {
    225 
    226 	p->vifi = s32p->vifi;
    227 	p->icount = (u_long)s32p->icount;
    228 	p->ocount = (u_long)s32p->ocount;
    229 	p->ibytes = (u_long)s32p->ibytes;
    230 	p->obytes = (u_long)s32p->obytes;
    231 }
    232 
    233 static inline void
    234 netbsd32_to_sioc_sg_req(struct netbsd32_sioc_sg_req *s32p,
    235     struct sioc_sg_req *p, u_long cmd)
    236 {
    237 
    238 	p->src = s32p->src;
    239 	p->grp = s32p->grp;
    240 	p->pktcnt = (u_long)s32p->pktcnt;
    241 	p->bytecnt = (u_long)s32p->bytecnt;
    242 	p->wrong_if = (u_long)s32p->wrong_if;
    243 }
    244 
    245 static inline void
    246 netbsd32_to_atareq(struct netbsd32_atareq *s32p, struct atareq *p, u_long cmd)
    247 {
    248 
    249 	p->flags = (u_long)s32p->flags;
    250 	p->command = s32p->command;
    251 	p->features = s32p->features;
    252 	p->sec_count = s32p->sec_count;
    253 	p->sec_num = s32p->sec_num;
    254 	p->head = s32p->head;
    255 	p->cylinder = s32p->cylinder;
    256 	p->databuf =  (char *)NETBSD32PTR64(s32p->databuf);
    257 	p->datalen = (u_long)s32p->datalen;
    258 	p->timeout = s32p->timeout;
    259 	p->retsts = s32p->retsts;
    260 	p->error = s32p->error;
    261 }
    262 
    263 static inline void
    264 netbsd32_to_vnd_ioctl(struct netbsd32_vnd_ioctl *s32p,
    265     struct vnd_ioctl *p, u_long cmd)
    266 {
    267 
    268 	p->vnd_file = (char *)NETBSD32PTR64(s32p->vnd_file);
    269 	p->vnd_flags = s32p->vnd_flags;
    270 	p->vnd_geom = s32p->vnd_geom;
    271 	p->vnd_osize = s32p->vnd_osize;
    272 	p->vnd_size = s32p->vnd_size;
    273 }
    274 
    275 static inline void
    276 netbsd32_to_vnd_user(struct netbsd32_vnd_user *s32p,
    277     struct vnd_user *p, u_long cmd)
    278 {
    279 
    280 	p->vnu_unit = s32p->vnu_unit;
    281 	p->vnu_dev = s32p->vnu_dev;
    282 	p->vnu_ino = s32p->vnu_ino;
    283 }
    284 
    285 static inline void
    286 netbsd32_to_vnd_ioctl50(struct netbsd32_vnd_ioctl50 *s32p,
    287     struct vnd_ioctl50 *p, u_long cmd)
    288 {
    289 
    290 	p->vnd_file = (char *)NETBSD32PTR64(s32p->vnd_file);
    291 	p->vnd_flags = s32p->vnd_flags;
    292 	p->vnd_geom = s32p->vnd_geom;
    293 	p->vnd_size = s32p->vnd_size;
    294 }
    295 
    296 static inline void
    297 netbsd32_to_plistref(struct netbsd32_plistref *s32p,
    298     struct plistref *p, u_long cmd)
    299 {
    300 
    301 	p->pref_plist = NETBSD32PTR64(s32p->pref_plist);
    302 	p->pref_len = s32p->pref_len;
    303 }
    304 
    305 static inline void
    306 netbsd32_to_nvlist_ref_t(netbsd32_nvlist_ref_t *s32p,
    307     nvlist_ref_t *p, u_long cmd)
    308 {
    309 
    310 	p->buf = NETBSD32PTR64(s32p->buf);
    311 	p->len = s32p->len;
    312 	p->flags = s32p->flags;
    313 }
    314 
    315 static inline void
    316 netbsd32_to_u_long(netbsd32_u_long *s32p, u_long *p, u_long cmd)
    317 {
    318 
    319 	*p = (u_long)*s32p;
    320 }
    321 
    322 static inline void
    323 netbsd32_to_voidp(netbsd32_voidp *s32p, voidp *p, u_long cmd)
    324 {
    325 
    326 	*p = (void *)NETBSD32PTR64(*s32p);
    327 }
    328 
    329 static inline void
    330 netbsd32_to_wdog_conf(struct netbsd32_wdog_conf *s32p,
    331     struct wdog_conf *p, u_long cmd)
    332 {
    333 
    334 	p->wc_names = (char *)NETBSD32PTR64(s32p->wc_names);
    335 	p->wc_count = s32p->wc_count;
    336 }
    337 
    338 static inline void
    339 netbsd32_to_npf_ioctl_table(const struct netbsd32_npf_ioctl_table *s32p,
    340     struct npf_ioctl_table *p, u_long cmd)
    341 {
    342 
    343        p->nct_cmd = s32p->nct_cmd;
    344        p->nct_name = NETBSD32PTR64(s32p->nct_name);
    345        switch (s32p->nct_cmd) {
    346        case NPF_CMD_TABLE_LOOKUP:
    347        case NPF_CMD_TABLE_ADD:
    348        case NPF_CMD_TABLE_REMOVE:
    349                p->nct_data.ent = s32p->nct_data.ent;
    350                break;
    351        case NPF_CMD_TABLE_LIST:
    352                p->nct_data.buf.buf = NETBSD32PTR64(s32p->nct_data.buf.buf);
    353                p->nct_data.buf.len = s32p->nct_data.buf.len;
    354                break;
    355        }
    356 }
    357 
    358 static inline void
    359 netbsd32_to_bpf_program(struct netbsd32_bpf_program *s32p,
    360     struct bpf_program *p, u_long cmd)
    361 {
    362 
    363 	p->bf_insns = (void *)NETBSD32PTR64(s32p->bf_insns);
    364 	p->bf_len = s32p->bf_len;
    365 }
    366 
    367 static inline void
    368 netbsd32_to_bpf_dltlist(struct netbsd32_bpf_dltlist *s32p,
    369     struct bpf_dltlist *p, u_long cmd)
    370 {
    371 
    372 	p->bfl_list = (void *)NETBSD32PTR64(s32p->bfl_list);
    373 	p->bfl_len = s32p->bfl_len;
    374 }
    375 
    376 /* wsdisplay stuff */
    377 static inline void
    378 netbsd32_to_wsdisplay_addscreendata(
    379     struct netbsd32_wsdisplay_addscreendata *asd32,
    380     struct wsdisplay_addscreendata *asd, u_long cmd)
    381 {
    382 
    383 	asd->screentype = (char *)NETBSD32PTR64(asd32->screentype);
    384 	asd->emul = (char *)NETBSD32PTR64(asd32->emul);
    385 	asd->idx = asd32->idx;
    386 }
    387 
    388 static inline void
    389 netbsd32_to_ieee80211req(struct netbsd32_ieee80211req *ireq32,
    390     struct ieee80211req *ireq, u_long cmd)
    391 {
    392 
    393 	memcpy(ireq->i_name, ireq32->i_name, IFNAMSIZ);
    394 	ireq->i_type = ireq32->i_type;
    395 	ireq->i_val = ireq32->i_val;
    396 	ireq->i_len = ireq32->i_len;
    397 	ireq->i_data = NETBSD32PTR64(ireq32->i_data);
    398 }
    399 
    400 static inline void
    401 netbsd32_to_ieee80211_nwkey(struct netbsd32_ieee80211_nwkey *nwk32,
    402     struct ieee80211_nwkey *nwk, u_long cmd)
    403 {
    404 	int i;
    405 
    406 	memcpy(nwk->i_name, nwk32->i_name, IFNAMSIZ);
    407 	nwk->i_wepon = nwk32->i_wepon;
    408 	nwk->i_defkid = nwk32->i_defkid;
    409 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    410 		nwk->i_key[i].i_keylen = nwk32->i_key[i].i_keylen;
    411 		nwk->i_key[i].i_keydat =
    412 		    NETBSD32PTR64(nwk32->i_key[i].i_keydat);
    413 	}
    414 }
    415 
    416 static inline void
    417 netbsd32_to_wsdisplay_cursor(struct netbsd32_wsdisplay_cursor *c32,
    418     struct wsdisplay_cursor *c, u_long cmd)
    419 {
    420 
    421 	c->which = c32->which;
    422 	c->enable = c32->enable;
    423 	c->pos.x = c32->pos.x;
    424 	c->pos.y = c32->pos.y;
    425 	c->hot.x = c32->hot.x;
    426 	c->hot.y = c32->hot.y;
    427 	c->size.x = c32->size.x;
    428 	c->size.y = c32->size.y;
    429 	c->cmap.index = c32->cmap.index;
    430 	c->cmap.count = c32->cmap.count;
    431 	c->cmap.red = NETBSD32PTR64(c32->cmap.red);
    432 	c->cmap.green = NETBSD32PTR64(c32->cmap.green);
    433 	c->cmap.blue = NETBSD32PTR64(c32->cmap.blue);
    434 	c->image = NETBSD32PTR64(c32->image);
    435 	c->mask = NETBSD32PTR64(c32->mask);
    436 }
    437 
    438 static inline void
    439 netbsd32_to_wsdisplay_cmap(struct netbsd32_wsdisplay_cmap *c32,
    440     struct wsdisplay_cmap *c, u_long cmd)
    441 {
    442 
    443 	c->index = c32->index;
    444 	c->count = c32->count;
    445 	c->red   = NETBSD32PTR64(c32->red);
    446 	c->green = NETBSD32PTR64(c32->green);
    447 	c->blue  = NETBSD32PTR64(c32->blue);
    448 }
    449 
    450 static inline void
    451 netbsd32_to_wsdisplay_font(struct netbsd32_wsdisplay_font *f32,
    452     struct wsdisplay_font *f, u_long cmd)
    453 {
    454 
    455 	f->name = NETBSD32PTR64(f32->name);
    456 	f->firstchar = f32->firstchar;
    457 	f->numchars = f32->numchars;
    458 	f->encoding = f32->encoding;
    459 	f->fontwidth = f32->fontwidth;
    460 	f->fontheight = f32->fontheight;
    461 	f->stride = f32->stride;
    462 	f->bitorder = f32->bitorder;
    463 	f->byteorder = f32->byteorder;
    464 	f->data = NETBSD32PTR64(f32->data);
    465 }
    466 
    467 static inline void
    468 netbsd32_to_wsdisplay_usefontdata(struct netbsd32_wsdisplay_usefontdata *f32,
    469     struct wsdisplay_usefontdata *f, u_long cmd)
    470 {
    471 
    472 	f->name = NETBSD32PTR64(f32->name);
    473 }
    474 
    475 static inline void
    476 netbsd32_to_clockctl_settimeofday(
    477     const struct netbsd32_clockctl_settimeofday *s32p,
    478     struct clockctl_settimeofday *p, u_long cmd)
    479 {
    480 
    481 	p->tv = NETBSD32PTR64(s32p->tv);
    482 	p->tzp = NETBSD32PTR64(s32p->tzp);
    483 }
    484 
    485 static inline void
    486 netbsd32_to_clockctl_adjtime(
    487     const struct netbsd32_clockctl_adjtime *s32p,
    488     struct clockctl_adjtime *p, u_long cmd)
    489 {
    490 
    491 	p->delta = NETBSD32PTR64(s32p->delta);
    492 	p->olddelta = NETBSD32PTR64(s32p->olddelta);
    493 }
    494 
    495 static inline void
    496 netbsd32_to_clockctl_clock_settime(
    497     const struct netbsd32_clockctl_clock_settime *s32p,
    498     struct clockctl_clock_settime *p, u_long cmd)
    499 {
    500 
    501 	p->clock_id = s32p->clock_id;
    502 	p->tp = NETBSD32PTR64(s32p->tp);
    503 }
    504 
    505 #ifdef NTP
    506 static inline void
    507 netbsd32_to_clockctl_ntp_adjtime(
    508     const struct netbsd32_clockctl_ntp_adjtime *s32p,
    509     struct clockctl_ntp_adjtime *p, u_long cmd)
    510 {
    511 
    512 	p->tp = NETBSD32PTR64(s32p->tp);
    513 	p->retval = s32p->retval;
    514 }
    515 #endif
    516 
    517 static inline void
    518 netbsd32_to_ksyms_gsymbol(const struct netbsd32_ksyms_gsymbol *s32p,
    519     struct ksyms_gsymbol *p, u_long cmd)
    520 {
    521 
    522 	p->kg_name = NETBSD32PTR64(s32p->kg_name);
    523 }
    524 
    525 static inline void
    526 netbsd32_to_ksyms_gvalue(const struct netbsd32_ksyms_gvalue *s32p,
    527     struct ksyms_gvalue *p, u_long cmd)
    528 {
    529 
    530 	p->kv_name = NETBSD32PTR64(s32p->kv_name);
    531 }
    532 
    533 static inline void
    534 netbsd32_to_devlistargs(const struct netbsd32_devlistargs *s32p,
    535     struct devlistargs *p, u_long cmd)
    536 {
    537 
    538 	memcpy(p->l_devname, s32p->l_devname, sizeof(p->l_devname));
    539 	p->l_children = s32p->l_children;
    540 	p->l_childname = NETBSD32PTR64(s32p->l_childname);
    541 }
    542 
    543 static inline void
    544 netbsd32_to_devrescanargs(const struct netbsd32_devrescanargs *s32p,
    545     struct devrescanargs *p, u_long cmd)
    546 {
    547 
    548 	memcpy(p->busname, s32p->busname, sizeof(p->busname));
    549 	memcpy(p->ifattr, s32p->ifattr, sizeof(p->ifattr));
    550 	p->numlocators = s32p->numlocators;
    551 	p->locators = NETBSD32PTR64(s32p->locators);
    552 }
    553 
    554 static inline void
    555 netbsd32_to_disk_strategy(const struct netbsd32_disk_strategy *s32p,
    556     struct disk_strategy *p, u_long cmd)
    557 {
    558 
    559 	memcpy(p->dks_name, s32p->dks_name, sizeof(p->dks_name));
    560 	p->dks_param = NETBSD32PTR64(s32p->dks_param);
    561 	p->dks_paramlen = s32p->dks_paramlen;
    562 }
    563 
    564 static inline void
    565 netbsd32_to_dkwedge_list(const struct netbsd32_dkwedge_list *s32p,
    566     struct dkwedge_list *p, u_long cmd)
    567 {
    568 
    569 	p->dkwl_buf = NETBSD32PTR64(s32p->dkwl_buf);
    570 	p->dkwl_bufsize = s32p->dkwl_bufsize;
    571 	p->dkwl_nwedges = s32p->dkwl_nwedges;
    572 	p->dkwl_ncopied = s32p->dkwl_ncopied;
    573 }
    574 
    575 /*
    576  * handle ioctl conversions from 64-bit kernel -> netbsd32
    577  */
    578 
    579 #if 0
    580 static inline void
    581 netbsd32_from_format_op(struct format_op *p,
    582     struct netbsd32_format_op *s32p, u_long cmd)
    583 {
    584 
    585 /* filled in */
    586 #if 0
    587 	s32p->df_buf = (netbsd32_charp)p->df_buf;
    588 #endif
    589 	s32p->df_count = p->df_count;
    590 	s32p->df_startblk = p->df_startblk;
    591 	memcpy(s32p->df_reg, p->df_reg, sizeof(p->df_reg));
    592 }
    593 #endif
    594 
    595 static inline void
    596 netbsd32_from_ifreq(struct ifreq *p, struct netbsd32_ifreq *s32p, u_long cmd)
    597 {
    598 
    599 	/*
    600 	 * XXX
    601 	 * struct ifreq says the same, but sometimes the ifr_data
    602 	 * union member needs to be converted to 64 bits... this
    603 	 * is very driver specific and so we ignore it for now..
    604 	 */
    605 	memcpy(s32p, p, sizeof *s32p);
    606 	switch (cmd) {
    607 	case SIOCGIFDATA:
    608 	case SIOCZIFDATA:
    609 	case SIOCGIFGENERIC:
    610 	case SIOCSIFGENERIC:
    611 		NETBSD32PTR32(s32p->ifr_data, p->ifr_data);
    612 		break;
    613 	}
    614 }
    615 
    616 static inline void
    617 netbsd32_from_oifreq(struct oifreq *p,
    618     struct netbsd32_oifreq *s32p, u_long cmd)
    619 {
    620 
    621 	/*
    622 	 * XXX
    623 	 * struct ifreq says the same, but sometimes the ifr_data
    624 	 * union member needs to be converted to 64 bits... this
    625 	 * is very driver specific and so we ignore it for now..
    626 	 */
    627 	memcpy(s32p, p, sizeof *s32p);
    628 	if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
    629 		NETBSD32PTR32(s32p->ifr_data, p->ifr_data);
    630 }
    631 
    632 static inline void
    633 netbsd32_from_if_addrprefreq(const struct if_addrprefreq *ifap,
    634     struct netbsd32_if_addrprefreq *ifap32, u_long cmd)
    635 {
    636 
    637 	memcpy(ifap32->ifap_name, ifap->ifap_name, IFNAMSIZ);
    638 	ifap32->ifap_preference = ifap->ifap_preference;
    639 	memcpy(&ifap32->ifap_addr, &ifap->ifap_addr,
    640 	    uimin(ifap->ifap_addr.ss_len, _SS_MAXSIZE));
    641 }
    642 
    643 static inline void
    644 netbsd32_from_ifconf(struct ifconf *p,
    645     struct netbsd32_ifconf *s32p, u_long cmd)
    646 {
    647 
    648 	s32p->ifc_len = p->ifc_len;
    649 	/* ifc_buf & ifc_req are the same size so this works */
    650 	NETBSD32PTR32(s32p->ifc_buf, p->ifc_buf);
    651 }
    652 
    653 static inline void
    654 netbsd32_from_ifmediareq(struct ifmediareq *p,
    655     struct netbsd32_ifmediareq *s32p, u_long cmd)
    656 {
    657 
    658 	memcpy(s32p, p, sizeof *p);
    659 /* filled in? */
    660 #if 0
    661 	s32p->ifm_ulist = (netbsd32_intp_t)p->ifm_ulist;
    662 #endif
    663 }
    664 
    665 static inline void
    666 netbsd32_from_pppoediscparms(struct pppoediscparms *p,
    667     struct netbsd32_pppoediscparms *s32p, u_long cmd)
    668 {
    669 
    670 	memcpy(s32p->ifname, p->ifname, sizeof s32p->ifname);
    671 	memcpy(s32p->eth_ifname, p->eth_ifname, sizeof s32p->eth_ifname);
    672 	NETBSD32PTR32(s32p->ac_name, p->ac_name);
    673 	s32p->ac_name_len = p->ac_name_len;
    674 	NETBSD32PTR32(s32p->service_name, p->service_name);
    675 	s32p->service_name_len = p->service_name_len;
    676 }
    677 
    678 static inline void
    679 netbsd32_from_spppauthcfg(struct spppauthcfg *p,
    680     struct netbsd32_spppauthcfg *s32p, u_long cmd)
    681 {
    682 
    683 	memcpy(s32p->ifname, p->ifname, sizeof s32p->ifname);
    684 	s32p->hisauth = p->hisauth;
    685 	s32p->myauth = p->myauth;
    686 	s32p->myname_length = p->myname_length;
    687 	s32p->mysecret_length = p->mysecret_length;
    688 	s32p->hisname_length = p->hisname_length;
    689 	s32p->hissecret_length = p->hissecret_length;
    690 	s32p->myauthflags = p->myauthflags;
    691 	s32p->hisauthflags = p->hisauthflags;
    692 	NETBSD32PTR32(s32p->myname, p->myname);
    693 	NETBSD32PTR32(s32p->mysecret, p->mysecret);
    694 	NETBSD32PTR32(s32p->hisname, p->hisname);
    695 	NETBSD32PTR32(s32p->hissecret, p->hissecret);
    696 }
    697 
    698 static inline void
    699 netbsd32_from_ifdrv(struct ifdrv *p, struct netbsd32_ifdrv *s32p, u_long cmd)
    700 {
    701 
    702 	memcpy(s32p->ifd_name, p->ifd_name, sizeof s32p->ifd_name);
    703 	s32p->ifd_cmd = p->ifd_cmd;
    704 	s32p->ifd_len = p->ifd_len;
    705 	NETBSD32PTR32(s32p->ifd_data, p->ifd_data);
    706 }
    707 
    708 static inline void
    709 netbsd32_from_sioc_vif_req(struct sioc_vif_req *p,
    710     struct netbsd32_sioc_vif_req *s32p, u_long cmd)
    711 {
    712 
    713 	s32p->vifi = p->vifi;
    714 	s32p->icount = (netbsd32_u_long)p->icount;
    715 	s32p->ocount = (netbsd32_u_long)p->ocount;
    716 	s32p->ibytes = (netbsd32_u_long)p->ibytes;
    717 	s32p->obytes = (netbsd32_u_long)p->obytes;
    718 }
    719 
    720 static inline void
    721 netbsd32_from_sioc_sg_req(struct sioc_sg_req *p,
    722     struct netbsd32_sioc_sg_req *s32p, u_long cmd)
    723 {
    724 
    725 	s32p->src = p->src;
    726 	s32p->grp = p->grp;
    727 	s32p->pktcnt = (netbsd32_u_long)p->pktcnt;
    728 	s32p->bytecnt = (netbsd32_u_long)p->bytecnt;
    729 	s32p->wrong_if = (netbsd32_u_long)p->wrong_if;
    730 }
    731 
    732 static inline void
    733 netbsd32_from_atareq(struct atareq *p,
    734     struct netbsd32_atareq *s32p, u_long cmd)
    735 {
    736 
    737 	s32p->flags = (netbsd32_u_long)p->flags;
    738 	s32p->command = p->command;
    739 	s32p->features = p->features;
    740 	s32p->sec_count = p->sec_count;
    741 	s32p->sec_num = p->sec_num;
    742 	s32p->head = p->head;
    743 	s32p->cylinder = p->cylinder;
    744 	NETBSD32PTR32(s32p->databuf, p->databuf);
    745 	s32p->datalen = (netbsd32_u_long)p->datalen;
    746 	s32p->timeout = p->timeout;
    747 	s32p->retsts = p->retsts;
    748 	s32p->error = p->error;
    749 }
    750 
    751 static inline void
    752 netbsd32_from_vnd_ioctl(struct vnd_ioctl *p,
    753     struct netbsd32_vnd_ioctl *s32p, u_long cmd)
    754 {
    755 
    756 	s32p->vnd_flags = p->vnd_flags;
    757 	s32p->vnd_geom = p->vnd_geom;
    758 	s32p->vnd_osize = p->vnd_osize;
    759 	s32p->vnd_size = p->vnd_size;
    760 }
    761 
    762 static inline void
    763 netbsd32_from_vnd_user(struct vnd_user *p,
    764     struct netbsd32_vnd_user *s32p, u_long cmd)
    765 {
    766 
    767 	s32p->vnu_unit = p->vnu_unit;
    768 	s32p->vnu_dev = p->vnu_dev;
    769 	s32p->vnu_ino = p->vnu_ino;
    770 }
    771 
    772 static inline void
    773 netbsd32_from_vnd_ioctl50(struct vnd_ioctl50 *p,
    774     struct netbsd32_vnd_ioctl50 *s32p, u_long cmd)
    775 {
    776 
    777 	s32p->vnd_flags = p->vnd_flags;
    778 	s32p->vnd_geom = p->vnd_geom;
    779 	s32p->vnd_size = p->vnd_size;
    780 }
    781 
    782 static inline void
    783 netbsd32_from_plistref(struct plistref *p,
    784     struct netbsd32_plistref *s32p, u_long cmd)
    785 {
    786 
    787 	NETBSD32PTR32(s32p->pref_plist, p->pref_plist);
    788 	s32p->pref_len = p->pref_len;
    789 }
    790 
    791 static inline void
    792 netbsd32_from_nvlist_ref_t(nvlist_ref_t *p,
    793     netbsd32_nvlist_ref_t *s32p, u_long cmd)
    794 {
    795 
    796 	NETBSD32PTR32(s32p->buf, p->buf);
    797 	s32p->len = p->len;
    798 	s32p->flags = p->flags;
    799 }
    800 
    801 static inline void
    802 netbsd32_from_wdog_conf(struct wdog_conf *p,
    803     struct netbsd32_wdog_conf *s32p, u_long cmd)
    804 {
    805 
    806 	NETBSD32PTR32(s32p->wc_names, p->wc_names);
    807 	s32p->wc_count = p->wc_count;
    808 }
    809 
    810 /* wsdisplay stuff */
    811 static inline void
    812 netbsd32_from_wsdisplay_addscreendata(struct wsdisplay_addscreendata *asd,
    813     struct netbsd32_wsdisplay_addscreendata *asd32, u_long cmd)
    814 {
    815 
    816 	NETBSD32PTR32(asd32->screentype, asd->screentype);
    817 	NETBSD32PTR32(asd32->emul, asd->emul);
    818 	asd32->idx = asd->idx;
    819 }
    820 
    821 static inline void
    822 netbsd32_from_wsdisplay_cursor(struct wsdisplay_cursor *c,
    823     struct netbsd32_wsdisplay_cursor *c32, u_long cmd)
    824 {
    825 
    826 	c32->which = c->which;
    827 	c32->enable = c->enable;
    828 	c32->pos.x = c->pos.x;
    829 	c32->pos.y = c->pos.y;
    830 	c32->hot.x = c->hot.x;
    831 	c32->hot.y = c->hot.y;
    832 	c32->size.x = c->size.x;
    833 	c32->size.y = c->size.y;
    834 	c32->cmap.index = c->cmap.index;
    835 	c32->cmap.count = c->cmap.count;
    836 	NETBSD32PTR32(c32->cmap.red, c->cmap.red);
    837 	NETBSD32PTR32(c32->cmap.green, c->cmap.green);
    838 	NETBSD32PTR32(c32->cmap.blue, c->cmap.blue);
    839 	NETBSD32PTR32(c32->image, c->image);
    840 	NETBSD32PTR32(c32->mask, c->mask);
    841 }
    842 
    843 static inline void
    844 netbsd32_from_wsdisplay_cmap(struct wsdisplay_cmap *c,
    845     struct netbsd32_wsdisplay_cmap *c32, u_long cmd)
    846 {
    847 
    848 	c32->index = c->index;
    849 	c32->count = c->count;
    850 	NETBSD32PTR32(c32->red, c->red);
    851 	NETBSD32PTR32(c32->green, c->green);
    852 	NETBSD32PTR32(c32->blue, c->blue);
    853 }
    854 
    855 static inline void
    856 netbsd32_from_wsdisplay_font(struct wsdisplay_font *f,
    857     struct netbsd32_wsdisplay_font *f32, u_long cmd)
    858 {
    859 }
    860 
    861 static inline void
    862 netbsd32_from_wsdisplay_usefontdata(struct wsdisplay_usefontdata *f,
    863     struct netbsd32_wsdisplay_usefontdata *f32, u_long cmd)
    864 {
    865 }
    866 
    867 static inline void
    868 netbsd32_from_ieee80211req(struct ieee80211req *ireq,
    869     struct netbsd32_ieee80211req *ireq32, u_long cmd)
    870 {
    871 
    872 	memcpy(ireq32->i_name, ireq->i_name, IFNAMSIZ);
    873 	ireq32->i_type = ireq->i_type;
    874 	ireq32->i_val = ireq->i_val;
    875 	ireq32->i_len = ireq->i_len;
    876 	NETBSD32PTR32(ireq32->i_data, ireq->i_data);
    877 }
    878 
    879 static inline void
    880 netbsd32_from_ieee80211_nwkey(struct ieee80211_nwkey *nwk,
    881     struct netbsd32_ieee80211_nwkey *nwk32, u_long cmd)
    882 {
    883 	int i;
    884 
    885 	memcpy(nwk32->i_name, nwk->i_name, IFNAMSIZ);
    886 	nwk32->i_wepon = nwk->i_wepon;
    887 	nwk32->i_defkid = nwk->i_defkid;
    888 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    889 		nwk32->i_key[i].i_keylen = nwk->i_key[i].i_keylen;
    890 		NETBSD32PTR32(nwk32->i_key[i].i_keydat,
    891 				nwk->i_key[i].i_keydat);
    892 	}
    893 }
    894 
    895 static inline void
    896 netbsd32_from_bpf_program(struct bpf_program *p,
    897     struct netbsd32_bpf_program *s32p, u_long cmd)
    898 {
    899 
    900 	NETBSD32PTR32(s32p->bf_insns, p->bf_insns);
    901 	s32p->bf_len = p->bf_len;
    902 }
    903 
    904 static inline void
    905 netbsd32_from_bpf_dltlist(struct bpf_dltlist *p,
    906     struct netbsd32_bpf_dltlist *s32p, u_long cmd)
    907 {
    908 
    909 	NETBSD32PTR32(s32p->bfl_list, p->bfl_list);
    910 	s32p->bfl_len = p->bfl_len;
    911 }
    912 
    913 static inline void
    914 netbsd32_from_u_long(u_long *p, netbsd32_u_long *s32p, u_long cmd)
    915 {
    916 
    917 	*s32p = (netbsd32_u_long)*p;
    918 }
    919 
    920 static inline void
    921 netbsd32_from_voidp(voidp *p, netbsd32_voidp *s32p, u_long cmd)
    922 {
    923 
    924 	NETBSD32PTR32(*s32p, *p);
    925 }
    926 
    927 static inline void
    928 netbsd32_from_clockctl_settimeofday(const struct clockctl_settimeofday *p,
    929     struct netbsd32_clockctl_settimeofday *s32p, u_long cmd)
    930 {
    931 
    932 	NETBSD32PTR32(s32p->tv, p->tv);
    933 	NETBSD32PTR32(s32p->tzp, p->tzp);
    934 }
    935 
    936 static inline void
    937 netbsd32_from_clockctl_adjtime(const struct clockctl_adjtime *p,
    938     struct netbsd32_clockctl_adjtime *s32p, u_long cmd)
    939 {
    940 
    941 	NETBSD32PTR32(s32p->delta, p->delta);
    942 	NETBSD32PTR32(s32p->olddelta, p->olddelta);
    943 }
    944 
    945 static inline void
    946 netbsd32_from_clockctl_clock_settime(const struct clockctl_clock_settime *p,
    947     struct netbsd32_clockctl_clock_settime *s32p, u_long cmd)
    948 {
    949 
    950 	s32p->clock_id = p->clock_id;
    951 	NETBSD32PTR32(s32p->tp, p->tp);
    952 }
    953 
    954 #ifdef NTP
    955 static inline void
    956 netbsd32_from_clockctl_ntp_adjtime(const struct clockctl_ntp_adjtime *p,
    957     struct netbsd32_clockctl_ntp_adjtime *s32p, u_long cmd)
    958 {
    959 
    960 	NETBSD32PTR32(s32p->tp, p->tp);
    961 	s32p->retval = p->retval;
    962 }
    963 #endif
    964 
    965 static inline void
    966 netbsd32_from_ksyms_gsymbol( const struct ksyms_gsymbol *p,
    967     struct netbsd32_ksyms_gsymbol *s32p, u_long cmd)
    968 {
    969 
    970 	NETBSD32PTR32(s32p->kg_name, p->kg_name);
    971 	s32p->kg_sym = p->kg_sym;
    972 }
    973 
    974 static inline void
    975 netbsd32_from_ksyms_gvalue(
    976     const struct ksyms_gvalue *p,
    977     struct netbsd32_ksyms_gvalue *s32p, u_long cmd)
    978 {
    979 
    980 	NETBSD32PTR32(s32p->kv_name, p->kv_name);
    981 	s32p->kv_value = p->kv_value;
    982 }
    983 
    984 static inline void
    985 netbsd32_from_npf_ioctl_table(const struct npf_ioctl_table *p,
    986     struct netbsd32_npf_ioctl_table *s32p, u_long cmd)
    987 {
    988 
    989        s32p->nct_cmd = p->nct_cmd;
    990        NETBSD32PTR32(s32p->nct_name, p->nct_name);
    991        switch (p->nct_cmd) {
    992        case NPF_CMD_TABLE_LOOKUP:
    993        case NPF_CMD_TABLE_ADD:
    994        case NPF_CMD_TABLE_REMOVE:
    995                s32p->nct_data.ent = p->nct_data.ent;
    996                break;
    997        case NPF_CMD_TABLE_LIST:
    998                NETBSD32PTR32(s32p->nct_data.buf.buf, p->nct_data.buf.buf);
    999                s32p->nct_data.buf.len = p->nct_data.buf.len;
   1000                break;
   1001        }
   1002 }
   1003 
   1004 static inline void
   1005 netbsd32_from_devlistargs(const struct devlistargs *p,
   1006     struct netbsd32_devlistargs *s32p, u_long cmd)
   1007 {
   1008 
   1009 	memcpy(s32p->l_devname, p->l_devname, sizeof(s32p->l_devname));
   1010 	s32p->l_children = p->l_children;
   1011 	NETBSD32PTR32(s32p->l_childname, p->l_childname);
   1012 }
   1013 
   1014 static inline void
   1015 netbsd32_from_devrescanargs(const struct devrescanargs *p,
   1016     struct netbsd32_devrescanargs *s32p, u_long cmd)
   1017 {
   1018 
   1019 	memcpy(s32p->busname, p->busname, sizeof(s32p->busname));
   1020 	memcpy(s32p->ifattr, p->ifattr, sizeof(s32p->ifattr));
   1021 	s32p->numlocators = p->numlocators;
   1022 	NETBSD32PTR32(s32p->locators, p->locators);
   1023 }
   1024 
   1025 static inline void
   1026 netbsd32_from_disk_strategy(const struct disk_strategy *p,
   1027     struct netbsd32_disk_strategy *s32p, u_long cmd)
   1028 {
   1029 
   1030 	memcpy(s32p->dks_name, p->dks_name, sizeof(p->dks_name));
   1031 	NETBSD32PTR32(s32p->dks_param, p->dks_param);
   1032 	s32p->dks_paramlen = p->dks_paramlen;
   1033 }
   1034 
   1035 static inline void
   1036 netbsd32_from_dkwedge_list(const struct dkwedge_list *p,
   1037     struct netbsd32_dkwedge_list *s32p, u_long cmd)
   1038 {
   1039 
   1040 	NETBSD32PTR32(s32p->dkwl_buf, p->dkwl_buf);
   1041 	s32p->dkwl_bufsize = p->dkwl_bufsize;
   1042 	s32p->dkwl_nwedges = p->dkwl_nwedges;
   1043 	s32p->dkwl_ncopied = p->dkwl_ncopied;
   1044 }
   1045 
   1046 #ifdef NTP
   1047 static int
   1048 netbsd32_do_clockctl_ntp_adjtime(struct file *fp,
   1049     struct clockctl_ntp_adjtime *args)
   1050 {
   1051 	struct vnode *vp;
   1052 	struct specnode *sn;
   1053 	const char *name;
   1054 
   1055 	struct netbsd32_timex ntv32;
   1056 	struct timex ntv;
   1057 	int error;
   1058 
   1059 	/* Verify that the file descriptor is is to the clockctl device */
   1060 	if (fp->f_type != DTYPE_VNODE)
   1061 		return EINVAL;
   1062 
   1063 	vp = fp->f_vnode;
   1064 	if (vp->v_type != VCHR)
   1065 		return EINVAL;
   1066 
   1067 	sn = vp->v_specnode;
   1068 	name = cdevsw_getname(major(sn->sn_rdev));
   1069 	if (name == NULL || strcmp(name, "clockctl") != 0)
   1070 		return EINVAL;
   1071 
   1072 	if (vec_ntp_adjtime1 == NULL)
   1073 		return EINVAL;
   1074 
   1075 	error = copyin(args->tp, &ntv32, sizeof(ntv32));
   1076 	if (error)
   1077 		return error;
   1078 
   1079 	netbsd32_to_timex(&ntv32, &ntv);
   1080 	(*vec_ntp_adjtime1)(&ntv);
   1081 	netbsd32_from_timex(&ntv, &ntv32);
   1082 
   1083 	error = copyout(&ntv32, args->tp, sizeof(ntv32));
   1084 	if (error == 0)
   1085 		args->retval = ntp_timestatus();
   1086 
   1087 	return error;
   1088 }
   1089 #endif
   1090 
   1091 /*
   1092  * main ioctl syscall.
   1093  *
   1094  * ok, here we are in the biggy.  we have to do fix ups depending
   1095  * on the ioctl command before and afterwards.
   1096  */
   1097 int
   1098 netbsd32_ioctl(struct lwp *l,
   1099     const struct netbsd32_ioctl_args *uap, register_t *retval)
   1100 {
   1101 	/* {
   1102 		syscallarg(int) fd;
   1103 		syscallarg(netbsd32_u_long) com;
   1104 		syscallarg(netbsd32_voidp) data;
   1105 	} */
   1106 	struct proc *p = l->l_proc;
   1107 	struct file *fp;
   1108 	struct filedesc *fdp;
   1109 	u_long com;
   1110 	int error = 0;
   1111 	size_t size;
   1112 	size_t alloc_size32, size32;
   1113 	void *data, *memp = NULL;
   1114 	void *data32, *memp32 = NULL;
   1115 	unsigned int fd;
   1116 	fdfile_t *ff;
   1117 	int tmp;
   1118 #define STK_PARAMS	128
   1119 	uint64_t stkbuf[STK_PARAMS/sizeof(uint64_t)];
   1120 	uint64_t stkbuf32[STK_PARAMS/sizeof(uint64_t)];
   1121 
   1122 	/*
   1123 	 * we need to translate some commands (_IOW) before calling sys_ioctl,
   1124 	 * some after (_IOR), and some both (_IOWR).
   1125 	 */
   1126 #if 0
   1127 	{
   1128 		const char * const dirs[8] = {
   1129 		    "NONE!", "VOID", "OUT", "VOID|OUT!", "IN", "VOID|IN!",
   1130 		    "INOUT", "VOID|IN|OUT!"
   1131 		};
   1132 
   1133 		printf("netbsd32_ioctl(%d, %x, %x): "
   1134 		    "%s group %c base %d len %d\n",
   1135 		    SCARG(uap, fd), SCARG(uap, com), SCARG(uap, data).i32,
   1136 		    dirs[((SCARG(uap, com) & IOC_DIRMASK)>>29)],
   1137 		    IOCGROUP(SCARG(uap, com)), IOCBASECMD(SCARG(uap, com)),
   1138 		    IOCPARM_LEN(SCARG(uap, com)));
   1139 	}
   1140 #endif
   1141 
   1142 	memp = NULL;
   1143 	memp32 = NULL;
   1144 	alloc_size32 = 0;
   1145 	size32 = 0;
   1146 	size = 0;
   1147 
   1148 	fdp = p->p_fd;
   1149 	fd = SCARG(uap, fd);
   1150 	if ((fp = fd_getfile(fd)) == NULL)
   1151 		return EBADF;
   1152 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
   1153 		error = EBADF;
   1154 		goto out;
   1155 	}
   1156 
   1157 	ff = atomic_load_consume(&fdp->fd_dt)->dt_ff[SCARG(uap, fd)];
   1158 	switch (com = SCARG(uap, com)) {
   1159 	case FIOCLEX:
   1160 		ff->ff_exclose = true;
   1161 		fdp->fd_exclose = true;
   1162 		goto out;
   1163 
   1164 	case FIONCLEX:
   1165 		ff->ff_exclose = false;
   1166 		goto out;
   1167 	}
   1168 
   1169 	/*
   1170 	 * Interpret high order word to find amount of data to be
   1171 	 * copied to/from the user's address space.
   1172 	 */
   1173 	size32 = IOCPARM_LEN(com);
   1174 	alloc_size32 = size32;
   1175 
   1176 	/*
   1177 	 * The disklabel is now padded to a multiple of 8 bytes however the old
   1178 	 * disklabel on 32bit platforms wasn't.  This leaves a difference in
   1179 	 * size of 4 bytes between the two but are otherwise identical.
   1180 	 * To deal with this, we allocate enough space for the new disklabel
   1181 	 * but only copyin/out the smaller amount.
   1182 	 */
   1183 	if (IOCGROUP(com) == 'd') {
   1184 		u_long ncom = com ^ (DIOCGDINFO ^ DIOCGDINFO32);
   1185 		switch (ncom) {
   1186 		case DIOCGDINFO:
   1187 		case DIOCWDINFO:
   1188 		case DIOCSDINFO:
   1189 		case DIOCGDEFLABEL:
   1190 			com = ncom;
   1191 			if (IOCPARM_LEN(DIOCGDINFO32) < IOCPARM_LEN(DIOCGDINFO))
   1192 				alloc_size32 = IOCPARM_LEN(DIOCGDINFO);
   1193 			break;
   1194 		}
   1195 	}
   1196 	if (alloc_size32 > IOCPARM_MAX) {
   1197 		error = ENOTTY;
   1198 		goto out;
   1199 	}
   1200 	if (alloc_size32 > sizeof(stkbuf)) {
   1201 		memp32 = kmem_alloc(alloc_size32, KM_SLEEP);
   1202 		data32 = memp32;
   1203 	} else
   1204 		data32 = (void *)stkbuf32;
   1205 	if ((com >> IOCPARM_SHIFT) == 0)  {
   1206 		/* UNIX-style ioctl. */
   1207 		data32 = SCARG_P32(uap, data);
   1208 	} else {
   1209 		if (com&IOC_IN) {
   1210 			if (size32) {
   1211 				error = copyin(SCARG_P32(uap, data), data32,
   1212 				    size32);
   1213 				if (error) {
   1214 					goto out;
   1215 				}
   1216 				/*
   1217 				 * The data between size and alloc_size has
   1218 				 * not been overwritten.  It shouldn't matter
   1219 				 * but let's clear that anyway.
   1220 				 */
   1221 				if (__predict_false(size32 < alloc_size32)) {
   1222 					memset((char *)data32+size32, 0,
   1223 					    alloc_size32 - size32);
   1224 				}
   1225 				ktrgenio(fd, UIO_WRITE, SCARG_P32(uap, data),
   1226 				    size32, 0);
   1227 			} else
   1228 				*(void **)data32 = SCARG_P32(uap, data);
   1229 		} else if ((com&IOC_OUT) && size32) {
   1230 			/*
   1231 			 * Zero the buffer so the user always
   1232 			 * gets back something deterministic.
   1233 			 */
   1234 			memset(data32, 0, alloc_size32);
   1235 		} else if (com&IOC_VOID) {
   1236 			*(void **)data32 = SCARG_P32(uap, data);
   1237 		}
   1238 	}
   1239 
   1240 	/*
   1241 	 * convert various structures, pointers, and other objects that
   1242 	 * change size from 32 bit -> 64 bit, for all ioctl commands.
   1243 	 */
   1244 	switch (SCARG(uap, com)) {
   1245 	case FIONBIO:
   1246 		mutex_enter(&fp->f_lock);
   1247 		if ((tmp = *(int *)data32) != 0)
   1248 			fp->f_flag |= FNONBLOCK;
   1249 		else
   1250 			fp->f_flag &= ~FNONBLOCK;
   1251 		mutex_exit(&fp->f_lock);
   1252 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (void *)&tmp);
   1253 		break;
   1254 
   1255 	case FIOASYNC:
   1256 		mutex_enter(&fp->f_lock);
   1257 		if ((tmp = *(int *)data32) != 0)
   1258 			fp->f_flag |= FASYNC;
   1259 		else
   1260 			fp->f_flag &= ~FASYNC;
   1261 		mutex_exit(&fp->f_lock);
   1262 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (void *)&tmp);
   1263 		break;
   1264 
   1265 	case AUDIO_WSEEK32:
   1266 		IOCTL_CONV_TO(AUDIO_WSEEK, u_long);
   1267 
   1268 #if 0	/* not implemented by anything */
   1269 	case DIOCRFORMAT32:
   1270 		IOCTL_STRUCT_CONV_TO(DIOCRFORMAT, format_op);
   1271 	case DIOCWFORMAT32:
   1272 		IOCTL_STRUCT_CONV_TO(DIOCWFORMAT, format_op);
   1273 #endif
   1274 
   1275 	case ATAIOCCOMMAND32:
   1276 		IOCTL_STRUCT_CONV_TO(ATAIOCCOMMAND, atareq);
   1277 
   1278 	case SIOCIFGCLONERS32:
   1279 		{
   1280 			struct netbsd32_if_clonereq *req =
   1281 			    (struct netbsd32_if_clonereq *)data32;
   1282 			char *buf = NETBSD32PTR64(req->ifcr_buffer);
   1283 
   1284 			error = if_clone_list(req->ifcr_count,
   1285 			    buf, &req->ifcr_total);
   1286 			break;
   1287 		}
   1288 
   1289 /*
   1290  * only a few ifreq syscalls need conversion and those are
   1291  * all driver specific... XXX
   1292  */
   1293 #if 0
   1294 	case SIOCGADDRROM3232:
   1295 		IOCTL_STRUCT_CONV_TO(SIOCGADDRROM32, ifreq);
   1296 	case SIOCGCHIPID32:
   1297 		IOCTL_STRUCT_CONV_TO(SIOCGCHIPID, ifreq);
   1298 	case SIOCSIFADDR32:
   1299 		IOCTL_STRUCT_CONV_TO(SIOCSIFADDR, ifreq);
   1300 	case OSIOCGIFADDR32:
   1301 		IOCTL_STRUCT_CONV_TO(OSIOCGIFADDR, ifreq);
   1302 	case SIOCGIFADDR32:
   1303 		IOCTL_STRUCT_CONV_TO(SIOCGIFADDR, ifreq);
   1304 	case SIOCSIFDSTADDR32:
   1305 		IOCTL_STRUCT_CONV_TO(SIOCSIFDSTADDR, ifreq);
   1306 	case OSIOCGIFDSTADDR32:
   1307 		IOCTL_STRUCT_CONV_TO(OSIOCGIFDSTADDR, ifreq);
   1308 	case SIOCGIFDSTADDR32:
   1309 		IOCTL_STRUCT_CONV_TO(SIOCGIFDSTADDR, ifreq);
   1310 	case OSIOCGIFBRDADDR32:
   1311 		IOCTL_STRUCT_CONV_TO(OSIOCGIFBRDADDR, ifreq);
   1312 	case SIOCGIFBRDADDR32:
   1313 		IOCTL_STRUCT_CONV_TO(SIOCGIFBRDADDR, ifreq);
   1314 	case SIOCSIFBRDADDR32:
   1315 		IOCTL_STRUCT_CONV_TO(SIOCSIFBRDADDR, ifreq);
   1316 	case OSIOCGIFNETMASK32:
   1317 		IOCTL_STRUCT_CONV_TO(OSIOCGIFNETMASK, ifreq);
   1318 	case SIOCGIFNETMASK32:
   1319 		IOCTL_STRUCT_CONV_TO(SIOCGIFNETMASK, ifreq);
   1320 	case SIOCSIFNETMASK32:
   1321 		IOCTL_STRUCT_CONV_TO(SIOCSIFNETMASK, ifreq);
   1322 	case SIOCGIFMETRIC32:
   1323 		IOCTL_STRUCT_CONV_TO(SIOCGIFMETRIC, ifreq);
   1324 	case SIOCSIFMETRIC32:
   1325 		IOCTL_STRUCT_CONV_TO(SIOCSIFMETRIC, ifreq);
   1326 	case SIOCDIFADDR32:
   1327 		IOCTL_STRUCT_CONV_TO(SIOCDIFADDR, ifreq);
   1328 	case SIOCADDMULTI32:
   1329 		IOCTL_STRUCT_CONV_TO(SIOCADDMULTI, ifreq);
   1330 	case SIOCDELMULTI32:
   1331 		IOCTL_STRUCT_CONV_TO(SIOCDELMULTI, ifreq);
   1332 	case SIOCSIFMEDIA32:
   1333 		IOCTL_STRUCT_CONV_TO(SIOCSIFMEDIA, ifreq);
   1334 	case SIOCSIFMTU32:
   1335 		IOCTL_STRUCT_CONV_TO(SIOCSIFMTU, ifreq);
   1336 	case SIOCGIFMTU32:
   1337 		IOCTL_STRUCT_CONV_TO(SIOCGIFMTU, ifreq);
   1338 	case BIOCGETIF32:
   1339 		IOCTL_STRUCT_CONV_TO(BIOCGETIF, ifreq);
   1340 	case BIOCSETIF32:
   1341 		IOCTL_STRUCT_CONV_TO(BIOCSETIF, ifreq);
   1342 	case SIOCPHASE132:
   1343 		IOCTL_STRUCT_CONV_TO(SIOCPHASE1, ifreq);
   1344 	case SIOCPHASE232:
   1345 		IOCTL_STRUCT_CONV_TO(SIOCPHASE2, ifreq);
   1346 #endif
   1347 
   1348 	case OOSIOCGIFCONF32:
   1349 		IOCTL_STRUCT_CONV_TO(OOSIOCGIFCONF, ifconf);
   1350 	case OSIOCGIFCONF32:
   1351 		IOCTL_STRUCT_CONV_TO(OSIOCGIFCONF, ifconf);
   1352 	case SIOCGIFCONF32:
   1353 		IOCTL_STRUCT_CONV_TO(SIOCGIFCONF, ifconf);
   1354 
   1355 	case SIOCGIFFLAGS32:
   1356 		IOCTL_STRUCT_CONV_TO(SIOCGIFFLAGS, ifreq);
   1357 	case SIOCSIFFLAGS32:
   1358 		IOCTL_STRUCT_CONV_TO(SIOCSIFFLAGS, ifreq);
   1359 
   1360 	case SIOCGIFADDRPREF32:
   1361 		IOCTL_STRUCT_CONV_TO(SIOCGIFADDRPREF, if_addrprefreq);
   1362 	case SIOCSIFADDRPREF32:
   1363 		IOCTL_STRUCT_CONV_TO(SIOCSIFADDRPREF, if_addrprefreq);
   1364 
   1365 
   1366 	case OSIOCGIFFLAGS32:
   1367 		IOCTL_STRUCT_CONV_TO(OSIOCGIFFLAGS, oifreq);
   1368 	case OSIOCSIFFLAGS32:
   1369 		IOCTL_STRUCT_CONV_TO(OSIOCSIFFLAGS, oifreq);
   1370 
   1371 	case SIOCGIFMEDIA32_80:
   1372 		IOCTL_STRUCT_CONV_TO(SIOCGIFMEDIA_80, ifmediareq);
   1373 	case SIOCGIFMEDIA32:
   1374 		IOCTL_STRUCT_CONV_TO(SIOCGIFMEDIA, ifmediareq);
   1375 
   1376 	case SIOCGIFGENERIC32:
   1377 		IOCTL_STRUCT_CONV_TO(SIOCGIFGENERIC, ifreq);
   1378 	case SIOCSIFGENERIC32:
   1379 		IOCTL_STRUCT_CONV_TO(SIOCSIFGENERIC, ifreq);
   1380 
   1381 	case PPPOESETPARMS32:
   1382 		IOCTL_STRUCT_CONV_TO(PPPOESETPARMS, pppoediscparms);
   1383 	case PPPOEGETPARMS32:
   1384 		IOCTL_STRUCT_CONV_TO(PPPOEGETPARMS, pppoediscparms);
   1385 	case SPPPGETAUTHCFG32:
   1386 		IOCTL_STRUCT_CONV_TO(SPPPGETAUTHCFG, spppauthcfg);
   1387 	case SPPPSETAUTHCFG32:
   1388 		IOCTL_STRUCT_CONV_TO(SPPPSETAUTHCFG, spppauthcfg);
   1389 
   1390 	case SIOCSDRVSPEC32:
   1391 		IOCTL_STRUCT_CONV_TO(SIOCSDRVSPEC, ifdrv);
   1392 	case SIOCGDRVSPEC32:
   1393 		IOCTL_STRUCT_CONV_TO(SIOCGDRVSPEC, ifdrv);
   1394 
   1395 	case SIOCGETVIFCNT32:
   1396 		IOCTL_STRUCT_CONV_TO(SIOCGETVIFCNT, sioc_vif_req);
   1397 
   1398 	case SIOCGETSGCNT32:
   1399 		IOCTL_STRUCT_CONV_TO(SIOCGETSGCNT, sioc_sg_req);
   1400 
   1401 	case VNDIOCSET32:
   1402 		IOCTL_STRUCT_CONV_TO(VNDIOCSET, vnd_ioctl);
   1403 
   1404 	case VNDIOCCLR32:
   1405 		IOCTL_STRUCT_CONV_TO(VNDIOCCLR, vnd_ioctl);
   1406 
   1407 	case VNDIOCGET32:
   1408 		IOCTL_STRUCT_CONV_TO(VNDIOCGET, vnd_user);
   1409 
   1410 	case VNDIOCSET5032:
   1411 		IOCTL_STRUCT_CONV_TO(VNDIOCSET50, vnd_ioctl50);
   1412 
   1413 	case VNDIOCCLR5032:
   1414 		IOCTL_STRUCT_CONV_TO(VNDIOCCLR50, vnd_ioctl50);
   1415 
   1416 	case ENVSYS_GETDICTIONARY32:
   1417 		IOCTL_STRUCT_CONV_TO(ENVSYS_GETDICTIONARY, plistref);
   1418 	case ENVSYS_SETDICTIONARY32:
   1419 		IOCTL_STRUCT_CONV_TO(ENVSYS_SETDICTIONARY, plistref);
   1420 	case ENVSYS_REMOVEPROPS32:
   1421 		IOCTL_STRUCT_CONV_TO(ENVSYS_REMOVEPROPS, plistref);
   1422 
   1423 	case WDOGIOC_GWDOGS32:
   1424 		IOCTL_STRUCT_CONV_TO(WDOGIOC_GWDOGS, wdog_conf);
   1425 
   1426 	case BIOCSETF32:
   1427 		IOCTL_STRUCT_CONV_TO(BIOCSETF, bpf_program);
   1428 	case BIOCSTCPF32:
   1429 		IOCTL_STRUCT_CONV_TO(BIOCSTCPF, bpf_program);
   1430 	case BIOCSUDPF32:
   1431 		IOCTL_STRUCT_CONV_TO(BIOCSUDPF, bpf_program);
   1432 	case BIOCGDLTLIST32:
   1433 		IOCTL_STRUCT_CONV_TO(BIOCGDLTLIST, bpf_dltlist);
   1434 	case BIOCSRTIMEOUT32:
   1435 #define netbsd32_to_timeval(s32p, p, cmd) netbsd32_to_timeval(s32p, p)
   1436 #define netbsd32_from_timeval(p, s32p, cmd) netbsd32_from_timeval(p, s32p)
   1437 		IOCTL_STRUCT_CONV_TO(BIOCSRTIMEOUT, timeval);
   1438 #undef netbsd32_to_timeval
   1439 #undef netbsd32_from_timeval
   1440 
   1441 	case WSDISPLAYIO_ADDSCREEN32:
   1442 		IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_ADDSCREEN,
   1443 		    wsdisplay_addscreendata);
   1444 
   1445 	case WSDISPLAYIO_GCURSOR32:
   1446 		IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_GCURSOR, wsdisplay_cursor);
   1447 	case WSDISPLAYIO_SCURSOR32:
   1448 		IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_SCURSOR, wsdisplay_cursor);
   1449 
   1450 	case WSDISPLAYIO_GETCMAP32:
   1451 		IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_GETCMAP, wsdisplay_cmap);
   1452 	case WSDISPLAYIO_PUTCMAP32:
   1453 		IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_PUTCMAP, wsdisplay_cmap);
   1454 
   1455 	case WSDISPLAYIO_LDFONT32:
   1456 		IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_LDFONT, wsdisplay_font);
   1457 	case WSDISPLAYIO_SFONT32:
   1458 		IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_SFONT, wsdisplay_usefontdata);
   1459 
   1460 	case SIOCS8021132:
   1461 		IOCTL_STRUCT_CONV_TO(SIOCS80211, ieee80211req);
   1462 	case SIOCG8021132:
   1463 		IOCTL_STRUCT_CONV_TO(SIOCG80211, ieee80211req);
   1464 	case SIOCS80211NWKEY32:
   1465 		IOCTL_STRUCT_CONV_TO(SIOCS80211NWKEY, ieee80211_nwkey);
   1466 	case SIOCG80211NWKEY32:
   1467 		IOCTL_STRUCT_CONV_TO(SIOCG80211NWKEY, ieee80211_nwkey);
   1468 
   1469 	case POWER_EVENT_RECVDICT32:
   1470 		IOCTL_STRUCT_CONV_TO(POWER_EVENT_RECVDICT, plistref);
   1471 
   1472 	case CLOCKCTL_SETTIMEOFDAY32:
   1473 		IOCTL_STRUCT_CONV_TO(CLOCKCTL_SETTIMEOFDAY,
   1474 		    clockctl_settimeofday);
   1475 	case CLOCKCTL_ADJTIME32:
   1476 		IOCTL_STRUCT_CONV_TO(CLOCKCTL_ADJTIME, clockctl_adjtime);
   1477 	case CLOCKCTL_CLOCK_SETTIME32:
   1478 		IOCTL_STRUCT_CONV_TO(CLOCKCTL_CLOCK_SETTIME,
   1479 		    clockctl_clock_settime);
   1480 	case CLOCKCTL_NTP_ADJTIME32:
   1481 #ifdef NTP
   1482 		{
   1483 			size = IOCPARM_LEN(CLOCKCTL_NTP_ADJTIME);
   1484 			if (size > sizeof(stkbuf))
   1485 				data = memp = kmem_alloc(size, KM_SLEEP);
   1486 			else
   1487 				data = (void *)stkbuf;
   1488 
   1489 			netbsd32_to_clockctl_ntp_adjtime(
   1490 				(const struct netbsd32_clockctl_ntp_adjtime *)data32,
   1491 				(struct clockctl_ntp_adjtime *)data,
   1492 				CLOCKCTL_NTP_ADJTIME);
   1493 			error = netbsd32_do_clockctl_ntp_adjtime(fp,
   1494 				(struct clockctl_ntp_adjtime *)data);
   1495 			netbsd32_from_clockctl_ntp_adjtime(
   1496 				(const struct clockctl_ntp_adjtime *)data,
   1497 				(struct netbsd32_clockctl_ntp_adjtime *)data32,
   1498 				CLOCKCTL_NTP_ADJTIME);
   1499 
   1500 			break;
   1501 		}
   1502 #else
   1503 		error = ENOTTY;
   1504 		break;
   1505 #endif /* NTP */
   1506 
   1507 	case KIOCGSYMBOL32:
   1508 		IOCTL_STRUCT_CONV_TO(KIOCGSYMBOL, ksyms_gsymbol);
   1509 	case KIOCGVALUE32:
   1510 		IOCTL_STRUCT_CONV_TO(KIOCGVALUE, ksyms_gvalue);
   1511 
   1512         case IOC_NPF_LOAD32:
   1513                 IOCTL_CONV_TO(IOC_NPF_LOAD, nvlist_ref_t);
   1514         case IOC_NPF_TABLE32:
   1515                 IOCTL_STRUCT_CONV_TO(IOC_NPF_TABLE, npf_ioctl_table);
   1516         case IOC_NPF_STATS32:
   1517                 IOCTL_CONV_TO(IOC_NPF_STATS, voidp);
   1518         case IOC_NPF_SAVE32:
   1519                 IOCTL_CONV_TO(IOC_NPF_SAVE, nvlist_ref_t);
   1520         case IOC_NPF_RULE32:
   1521                 IOCTL_CONV_TO(IOC_NPF_RULE, nvlist_ref_t);
   1522         case IOC_NPF_CONN_LOOKUP32:
   1523                 IOCTL_CONV_TO(IOC_NPF_CONN_LOOKUP, nvlist_ref_t);
   1524 
   1525 	case DRVRESCANBUS32:
   1526 		IOCTL_STRUCT_CONV_TO(DRVRESCANBUS, devrescanargs);
   1527 	case DRVLISTDEV32:
   1528 		IOCTL_STRUCT_CONV_TO(DRVLISTDEV, devlistargs);
   1529 	case DRVCTLCOMMAND32:
   1530 		IOCTL_STRUCT_CONV_TO(DRVCTLCOMMAND, plistref);
   1531 	case DRVGETEVENT32:
   1532 		IOCTL_STRUCT_CONV_TO(DRVGETEVENT, plistref);
   1533 
   1534 	case DIOCGSTRATEGY32:
   1535 		IOCTL_STRUCT_CONV_TO(DIOCGSTRATEGY, disk_strategy);
   1536 	case DIOCSSTRATEGY32:
   1537 		IOCTL_STRUCT_CONV_TO(DIOCSSTRATEGY, disk_strategy);
   1538 	case DIOCLWEDGES32:
   1539 		IOCTL_STRUCT_CONV_TO(DIOCLWEDGES, dkwedge_list);
   1540 
   1541 	default:
   1542 #ifdef NETBSD32_DRMKMS
   1543 		if (IOCGROUP(com) == 'd') {
   1544 			error = netbsd32_drm_ioctl(fp, com, data32, l);
   1545 			break;
   1546 		}
   1547 #endif
   1548 #ifdef NETBSD32_MD_IOCTL
   1549 		error = netbsd32_md_ioctl(fp, com, data32, l);
   1550 #else
   1551 		error = (*fp->f_ops->fo_ioctl)(fp, com, data32);
   1552 #endif
   1553 		break;
   1554 	}
   1555 
   1556 	if (error == EPASSTHROUGH)
   1557 		error = ENOTTY;
   1558 
   1559 	/*
   1560 	 * Copy any data to user, size was
   1561 	 * already set and checked above.
   1562 	 */
   1563 	if (error == 0 && (com&IOC_OUT) && size32) {
   1564 		error = copyout(data32, SCARG_P32(uap, data), size32);
   1565 		ktrgenio(fd, UIO_READ, SCARG_P32(uap, data),
   1566 		    size32, error);
   1567 	}
   1568 
   1569  out:
   1570 	/* If we allocated data, free it here. */
   1571 	if (memp32)
   1572 		kmem_free(memp32, alloc_size32);
   1573 	if (memp)
   1574 		kmem_free(memp, size);
   1575 	fd_putfile(fd);
   1576 	return error;
   1577 }
   1578