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