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