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