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