xref: /netbsd-src/sys/compat/netbsd32/netbsd32_ioctl.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: netbsd32_ioctl.c,v 1.69 2014/01/24 12:16:10 bouyer 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.69 2014/01/24 12:16:10 bouyer Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/filedesc.h>
39 #include <sys/ioctl.h>
40 #include <sys/file.h>
41 #include <sys/proc.h>
42 #include <sys/socketvar.h>
43 #include <sys/audioio.h>
44 #include <sys/disklabel.h>
45 #include <sys/dkio.h>
46 #include <sys/ataio.h>
47 #include <sys/sockio.h>
48 #include <sys/socket.h>
49 #include <sys/ttycom.h>
50 #include <sys/mount.h>
51 #include <sys/syscallargs.h>
52 #include <sys/ktrace.h>
53 #include <sys/kmem.h>
54 #include <sys/envsys.h>
55 #include <sys/wdog.h>
56 #include <sys/clockctl.h>
57 
58 #ifdef __sparc__
59 #include <dev/sun/fbio.h>
60 #include <machine/openpromio.h>
61 #endif
62 
63 #include <net/if.h>
64 #include <net/route.h>
65 
66 #include <net/bpf.h>
67 #include <netinet/in.h>
68 #include <netinet/in_var.h>
69 #include <netinet/igmp.h>
70 #include <netinet/igmp_var.h>
71 #include <netinet/ip_mroute.h>
72 
73 #include <compat/sys/sockio.h>
74 
75 #include <compat/netbsd32/netbsd32.h>
76 #include <compat/netbsd32/netbsd32_ioctl.h>
77 #include <compat/netbsd32/netbsd32_syscallargs.h>
78 
79 #include <dev/vndvar.h>
80 
81 /* convert to/from different structures */
82 
83 static inline void
84 netbsd32_to_partinfo(struct netbsd32_partinfo *s32p, struct partinfo *p, u_long cmd)
85 {
86 
87 	p->disklab = (struct disklabel *)NETBSD32PTR64(s32p->disklab);
88 	p->part = (struct partition *)NETBSD32PTR64(s32p->part);
89 }
90 
91 #if 0
92 static inline void
93 netbsd32_to_format_op(struct netbsd32_format_op *s32p, struct format_op *p, u_long cmd)
94 {
95 
96 	p->df_buf = (char *)NETBSD32PTR64(s32p->df_buf);
97 	p->df_count = s32p->df_count;
98 	p->df_startblk = s32p->df_startblk;
99 	memcpy(p->df_reg, s32p->df_reg, sizeof(s32p->df_reg));
100 }
101 #endif
102 
103 static inline void
104 netbsd32_to_ifreq(struct netbsd32_ifreq *s32p, struct ifreq *p, u_long cmd)
105 {
106 
107 	memcpy(p, s32p, sizeof *s32p);
108 	/*
109 	 * XXX
110 	 * struct ifreq says the same, but sometimes the ifr_data
111 	 * union member needs to be converted to 64 bits... this
112 	 * is very driver specific and so we ignore it for now..
113 	 */
114 	if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
115 		p->ifr_data = (void *)NETBSD32PTR64(s32p->ifr_data);
116 }
117 
118 static inline void
119 netbsd32_to_oifreq(struct netbsd32_oifreq *s32p, struct oifreq *p, u_long cmd)
120 {
121 
122 	memcpy(p, s32p, sizeof *s32p);
123 	/*
124 	 * XXX
125 	 * struct ifreq says the same, but sometimes the ifr_data
126 	 * union member needs to be converted to 64 bits... this
127 	 * is very driver specific and so we ignore it for now..
128 	 */
129 	if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
130 		p->ifr_data = (void *)NETBSD32PTR64(s32p->ifr_data);
131 }
132 
133 static inline void
134 netbsd32_to_if_addrprefreq(const struct netbsd32_if_addrprefreq *ifap32,
135 	struct if_addrprefreq *ifap, u_long cmd)
136 {
137 	strlcpy(ifap->ifap_name, ifap32->ifap_name, sizeof(ifap->ifap_name));
138 	ifap->ifap_preference = ifap32->ifap_preference;
139 	memcpy(&ifap->ifap_addr, &ifap32->ifap_addr,
140 	    max(ifap32->ifap_addr.ss_len, _SS_MAXSIZE));
141 }
142 
143 static inline void
144 netbsd32_to_ifconf(struct netbsd32_ifconf *s32p, struct ifconf *p, u_long cmd)
145 {
146 
147 	p->ifc_len = s32p->ifc_len;
148 	/* ifc_buf & ifc_req are the same size so this works */
149 	p->ifc_buf = (void *)NETBSD32PTR64(s32p->ifc_buf);
150 }
151 
152 static inline void
153 netbsd32_to_ifmediareq(struct netbsd32_ifmediareq *s32p, struct ifmediareq *p, u_long cmd)
154 {
155 
156 	memcpy(p, s32p, sizeof *s32p);
157 	p->ifm_ulist = (int *)NETBSD32PTR64(s32p->ifm_ulist);
158 }
159 
160 static inline void
161 netbsd32_to_ifdrv(struct netbsd32_ifdrv *s32p, struct ifdrv *p, u_long cmd)
162 {
163 
164 	memcpy(p, s32p, sizeof *s32p);
165 	p->ifd_data = (void *)NETBSD32PTR64(s32p->ifd_data);
166 }
167 
168 static inline void
169 netbsd32_to_sioc_vif_req(struct netbsd32_sioc_vif_req *s32p, struct sioc_vif_req *p, u_long cmd)
170 {
171 
172 	p->vifi = s32p->vifi;
173 	p->icount = (u_long)s32p->icount;
174 	p->ocount = (u_long)s32p->ocount;
175 	p->ibytes = (u_long)s32p->ibytes;
176 	p->obytes = (u_long)s32p->obytes;
177 }
178 
179 static inline void
180 netbsd32_to_sioc_sg_req(struct netbsd32_sioc_sg_req *s32p, struct sioc_sg_req *p, u_long cmd)
181 {
182 
183 	p->src = s32p->src;
184 	p->grp = s32p->grp;
185 	p->pktcnt = (u_long)s32p->pktcnt;
186 	p->bytecnt = (u_long)s32p->bytecnt;
187 	p->wrong_if = (u_long)s32p->wrong_if;
188 }
189 
190 static inline void
191 netbsd32_to_atareq(struct netbsd32_atareq *s32p, struct atareq *p, u_long cmd)
192 {
193 	p->flags = (u_long)s32p->flags;
194 	p->command = s32p->command;
195 	p->features = s32p->features;
196 	p->sec_count = s32p->sec_count;
197 	p->sec_num = s32p->sec_num;
198 	p->head = s32p->head;
199 	p->cylinder = s32p->cylinder;
200 	p->databuf =  (char *)NETBSD32PTR64(s32p->databuf);
201 	p->datalen = (u_long)s32p->datalen;
202 	p->timeout = s32p->timeout;
203 	p->retsts = s32p->retsts;
204 	p->error = s32p->error;
205 }
206 
207 static inline void
208 netbsd32_to_vnd_ioctl(struct netbsd32_vnd_ioctl *s32p, struct vnd_ioctl *p, u_long cmd)
209 {
210 
211 	p->vnd_file = (char *)NETBSD32PTR64(s32p->vnd_file);
212 	p->vnd_flags = s32p->vnd_flags;
213 	p->vnd_geom = s32p->vnd_geom;
214 	p->vnd_osize = s32p->vnd_osize;
215 	p->vnd_size = s32p->vnd_size;
216 }
217 
218 static inline void
219 netbsd32_to_vnd_user(struct netbsd32_vnd_user *s32p, struct vnd_user *p, u_long cmd)
220 {
221 
222 	p->vnu_unit = s32p->vnu_unit;
223 	p->vnu_dev = s32p->vnu_dev;
224 	p->vnu_ino = s32p->vnu_ino;
225 }
226 
227 static inline void
228 netbsd32_to_vnd_ioctl50(struct netbsd32_vnd_ioctl50 *s32p, struct vnd_ioctl50 *p, u_long cmd)
229 {
230 
231 	p->vnd_file = (char *)NETBSD32PTR64(s32p->vnd_file);
232 	p->vnd_flags = s32p->vnd_flags;
233 	p->vnd_geom = s32p->vnd_geom;
234 	p->vnd_size = s32p->vnd_size;
235 }
236 
237 static inline void
238 netbsd32_to_plistref(struct netbsd32_plistref *s32p, struct plistref *p, u_long cmd)
239 {
240 
241 	p->pref_plist = NETBSD32PTR64(s32p->pref_plist);
242 	p->pref_len = s32p->pref_len;
243 }
244 
245 static inline void
246 netbsd32_to_u_long(netbsd32_u_long *s32p, u_long *p, u_long cmd)
247 {
248 
249 	*p = (u_long)*s32p;
250 }
251 
252 static inline void
253 netbsd32_to_wdog_conf(struct netbsd32_wdog_conf *s32p, struct wdog_conf *p, u_long cmd)
254 {
255 
256 	p->wc_names = (char *)NETBSD32PTR64(s32p->wc_names);
257 	p->wc_count = s32p->wc_count;
258 }
259 
260 static inline void
261 netbsd32_to_bpf_program(struct netbsd32_bpf_program *s32p, struct bpf_program *p, u_long cmd)
262 {
263 
264 	p->bf_insns = (void *)NETBSD32PTR64(s32p->bf_insns);
265 	p->bf_len = s32p->bf_len;
266 }
267 
268 static inline void
269 netbsd32_to_bpf_dltlist(struct netbsd32_bpf_dltlist *s32p, struct bpf_dltlist *p, u_long cmd)
270 {
271 
272 	p->bfl_list = (void *)NETBSD32PTR64(s32p->bfl_list);
273 	p->bfl_len = s32p->bfl_len;
274 }
275 
276 /* wsdisplay stuff */
277 static inline void
278 netbsd32_to_wsdisplay_addscreendata(struct netbsd32_wsdisplay_addscreendata *asd32,
279 					       struct wsdisplay_addscreendata *asd,
280 					       u_long cmd)
281 {
282 	asd->screentype = (char *)NETBSD32PTR64(asd32->screentype);
283 	asd->emul = (char *)NETBSD32PTR64(asd32->emul);
284 	asd->idx = asd32->idx;
285 }
286 
287 static inline void
288 netbsd32_to_ieee80211req(struct netbsd32_ieee80211req *ireq32,
289 			 struct ieee80211req *ireq, u_long cmd)
290 {
291 	strncpy(ireq->i_name, ireq32->i_name, IFNAMSIZ);
292 	ireq->i_type = ireq32->i_type;
293 	ireq->i_val = ireq32->i_val;
294 	ireq->i_len = ireq32->i_len;
295 	ireq->i_data = NETBSD32PTR64(ireq32->i_data);
296 }
297 
298 static inline void
299 netbsd32_to_ieee80211_nwkey(struct netbsd32_ieee80211_nwkey *nwk32,
300 					       struct ieee80211_nwkey *nwk,
301 					       u_long cmd)
302 {
303 	int i;
304 
305 	strncpy(nwk->i_name, nwk32->i_name, IFNAMSIZ);
306 	nwk->i_wepon = nwk32->i_wepon;
307 	nwk->i_defkid = nwk32->i_defkid;
308 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
309 		nwk->i_key[i].i_keylen = nwk32->i_key[i].i_keylen;
310 		nwk->i_key[i].i_keydat =
311 		    NETBSD32PTR64(nwk32->i_key[i].i_keydat);
312 	}
313 }
314 
315 static inline void
316 netbsd32_to_wsdisplay_cursor(struct netbsd32_wsdisplay_cursor *c32,
317 					       struct wsdisplay_cursor *c,
318 					       u_long cmd)
319 {
320 	c->which = c32->which;
321 	c->enable = c32->enable;
322 	c->pos.x = c32->pos.x;
323 	c->pos.y = c32->pos.y;
324 	c->hot.x = c32->hot.x;
325 	c->hot.y = c32->hot.y;
326 	c->size.x = c32->size.x;
327 	c->size.y = c32->size.y;
328 	c->cmap.index = c32->cmap.index;
329 	c->cmap.count = c32->cmap.count;
330 	c->cmap.red = NETBSD32PTR64(c32->cmap.red);
331 	c->cmap.green = NETBSD32PTR64(c32->cmap.green);
332 	c->cmap.blue = NETBSD32PTR64(c32->cmap.blue);
333 	c->image = NETBSD32PTR64(c32->image);
334 	c->mask = NETBSD32PTR64(c32->mask);
335 }
336 
337 static inline void
338 netbsd32_to_wsdisplay_cmap(struct netbsd32_wsdisplay_cmap *c32,
339 					       struct wsdisplay_cmap *c,
340 					       u_long cmd)
341 {
342 	c->index = c32->index;
343 	c->count = c32->count;
344 	c->red   = NETBSD32PTR64(c32->red);
345 	c->green = NETBSD32PTR64(c32->green);
346 	c->blue  = NETBSD32PTR64(c32->blue);
347 }
348 
349 static inline void
350 netbsd32_to_clockctl_settimeofday(
351     const struct netbsd32_clockctl_settimeofday *s32p,
352     struct clockctl_settimeofday *p,
353     u_long cmd)
354 {
355 
356 	p->tv = NETBSD32PTR64(s32p->tv);
357 	p->tzp = NETBSD32PTR64(s32p->tzp);
358 }
359 
360 static inline void
361 netbsd32_to_clockctl_adjtime(
362     const struct netbsd32_clockctl_adjtime *s32p,
363     struct clockctl_adjtime *p,
364     u_long cmd)
365 {
366 
367 	p->delta = NETBSD32PTR64(s32p->delta);
368 	p->olddelta = NETBSD32PTR64(s32p->olddelta);
369 }
370 
371 static inline void
372 netbsd32_to_clockctl_clock_settime(
373     const struct netbsd32_clockctl_clock_settime *s32p,
374     struct clockctl_clock_settime *p,
375     u_long cmd)
376 {
377 
378 	p->clock_id = s32p->clock_id;
379 	p->tp = NETBSD32PTR64(s32p->tp);
380 }
381 
382 static inline void
383 netbsd32_to_clockctl_ntp_adjtime(
384     const struct netbsd32_clockctl_ntp_adjtime *s32p,
385     struct clockctl_ntp_adjtime *p,
386     u_long cmd)
387 {
388 
389 	p->tp = NETBSD32PTR64(s32p->tp);
390 	p->retval = s32p->retval;
391 }
392 
393 /*
394  * handle ioctl conversions from 64-bit kernel -> netbsd32
395  */
396 
397 static inline void
398 netbsd32_from_partinfo(struct partinfo *p, struct netbsd32_partinfo *s32p, u_long cmd)
399 {
400 
401 	NETBSD32PTR32(s32p->disklab, p->disklab);
402 	NETBSD32PTR32(s32p->part, p->part);
403 }
404 
405 #if 0
406 static inline void
407 netbsd32_from_format_op(struct format_op *p, struct netbsd32_format_op *s32p, u_long cmd)
408 {
409 
410 /* filled in */
411 #if 0
412 	s32p->df_buf = (netbsd32_charp)p->df_buf;
413 #endif
414 	s32p->df_count = p->df_count;
415 	s32p->df_startblk = p->df_startblk;
416 	memcpy(s32p->df_reg, p->df_reg, sizeof(p->df_reg));
417 }
418 #endif
419 
420 static inline void
421 netbsd32_from_ifreq(struct ifreq *p, struct netbsd32_ifreq *s32p, u_long cmd)
422 {
423 
424 	/*
425 	 * XXX
426 	 * struct ifreq says the same, but sometimes the ifr_data
427 	 * union member needs to be converted to 64 bits... this
428 	 * is very driver specific and so we ignore it for now..
429 	 */
430 	memcpy(s32p, p, sizeof *s32p);
431 	if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
432 		NETBSD32PTR32(s32p->ifr_data, p->ifr_data);
433 }
434 
435 static inline void
436 netbsd32_from_oifreq(struct oifreq *p, struct netbsd32_oifreq *s32p, u_long cmd)
437 {
438 
439 	/*
440 	 * XXX
441 	 * struct ifreq says the same, but sometimes the ifr_data
442 	 * union member needs to be converted to 64 bits... this
443 	 * is very driver specific and so we ignore it for now..
444 	 */
445 	memcpy(s32p, p, sizeof *s32p);
446 	if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
447 		NETBSD32PTR32(s32p->ifr_data, p->ifr_data);
448 }
449 
450 static inline void
451 netbsd32_from_if_addrprefreq(const struct if_addrprefreq *ifap,
452 	struct netbsd32_if_addrprefreq *ifap32, u_long cmd)
453 {
454 	strlcpy(ifap32->ifap_name, ifap->ifap_name, sizeof(ifap32->ifap_name));
455 	ifap32->ifap_preference = ifap->ifap_preference;
456 	memcpy(&ifap32->ifap_addr, &ifap->ifap_addr,
457 	    max(ifap->ifap_addr.ss_len, _SS_MAXSIZE));
458 }
459 
460 static inline void
461 netbsd32_from_ifconf(struct ifconf *p, struct netbsd32_ifconf *s32p, u_long cmd)
462 {
463 
464 	s32p->ifc_len = p->ifc_len;
465 	/* ifc_buf & ifc_req are the same size so this works */
466 	NETBSD32PTR32(s32p->ifc_buf, p->ifc_buf);
467 }
468 
469 static inline void
470 netbsd32_from_ifmediareq(struct ifmediareq *p, struct netbsd32_ifmediareq *s32p, u_long cmd)
471 {
472 
473 	memcpy(s32p, p, sizeof *p);
474 /* filled in? */
475 #if 0
476 	s32p->ifm_ulist = (netbsd32_intp_t)p->ifm_ulist;
477 #endif
478 }
479 
480 static inline void
481 netbsd32_from_ifdrv(struct ifdrv *p, struct netbsd32_ifdrv *s32p, u_long cmd)
482 {
483 
484 	memcpy(s32p, p, sizeof *p);
485 /* filled in? */
486 #if 0
487 	s32p->ifm_data = (netbsd32_u_longp_t)p->ifm_data;
488 #endif
489 }
490 
491 static inline void
492 netbsd32_from_sioc_vif_req(struct sioc_vif_req *p, struct netbsd32_sioc_vif_req *s32p, u_long cmd)
493 {
494 
495 	s32p->vifi = p->vifi;
496 	s32p->icount = (netbsd32_u_long)p->icount;
497 	s32p->ocount = (netbsd32_u_long)p->ocount;
498 	s32p->ibytes = (netbsd32_u_long)p->ibytes;
499 	s32p->obytes = (netbsd32_u_long)p->obytes;
500 }
501 
502 static inline void
503 netbsd32_from_sioc_sg_req(struct sioc_sg_req *p, struct netbsd32_sioc_sg_req *s32p, u_long cmd)
504 {
505 
506 	s32p->src = p->src;
507 	s32p->grp = p->grp;
508 	s32p->pktcnt = (netbsd32_u_long)p->pktcnt;
509 	s32p->bytecnt = (netbsd32_u_long)p->bytecnt;
510 	s32p->wrong_if = (netbsd32_u_long)p->wrong_if;
511 }
512 
513 static inline void
514 netbsd32_from_atareq(struct atareq *p, struct netbsd32_atareq *s32p, u_long cmd)
515 {
516 	s32p->flags = (netbsd32_u_long)p->flags;
517 	s32p->command = p->command;
518 	s32p->features = p->features;
519 	s32p->sec_count = p->sec_count;
520 	s32p->sec_num = p->sec_num;
521 	s32p->head = p->head;
522 	s32p->cylinder = p->cylinder;
523 	NETBSD32PTR32(s32p->databuf, p->databuf);
524 	s32p->datalen = (netbsd32_u_long)p->datalen;
525 	s32p->timeout = p->timeout;
526 	s32p->retsts = p->retsts;
527 	s32p->error = p->error;
528 }
529 
530 static inline void
531 netbsd32_from_vnd_ioctl(struct vnd_ioctl *p, struct netbsd32_vnd_ioctl *s32p, u_long cmd)
532 {
533 
534 	s32p->vnd_flags = p->vnd_flags;
535 	s32p->vnd_geom = p->vnd_geom;
536 	s32p->vnd_osize = p->vnd_osize;
537 	s32p->vnd_size = p->vnd_size;
538 }
539 
540 static inline void
541 netbsd32_from_vnd_user(struct vnd_user *p, struct netbsd32_vnd_user *s32p, u_long cmd)
542 {
543 
544 	s32p->vnu_unit = p->vnu_unit;
545 	s32p->vnu_dev = p->vnu_dev;
546 	s32p->vnu_ino = p->vnu_ino;
547 }
548 
549 static inline void
550 netbsd32_from_vnd_ioctl50(struct vnd_ioctl50 *p, struct netbsd32_vnd_ioctl50 *s32p, u_long cmd)
551 {
552 
553 	s32p->vnd_flags = p->vnd_flags;
554 	s32p->vnd_geom = p->vnd_geom;
555 	s32p->vnd_size = p->vnd_size;
556 }
557 
558 static inline void
559 netbsd32_from_plistref(struct plistref *p, struct netbsd32_plistref *s32p, u_long cmd)
560 {
561 
562 	NETBSD32PTR32(s32p->pref_plist, p->pref_plist);
563 	s32p->pref_len = p->pref_len;
564 }
565 
566 static inline void
567 netbsd32_from_wdog_conf(struct wdog_conf *p, struct netbsd32_wdog_conf *s32p, u_long cmd)
568 {
569 
570 	NETBSD32PTR32(s32p->wc_names, p->wc_names);
571 	s32p->wc_count = p->wc_count;
572 }
573 
574 /* wsdisplay stuff */
575 static inline void
576 netbsd32_from_wsdisplay_addscreendata(struct wsdisplay_addscreendata *asd,
577 					struct netbsd32_wsdisplay_addscreendata *asd32,
578 					u_long cmd)
579 {
580 	NETBSD32PTR32(asd32->screentype, asd->screentype);
581 	NETBSD32PTR32(asd32->emul, asd->emul);
582 	asd32->idx = asd->idx;
583 }
584 
585 static inline void
586 netbsd32_from_wsdisplay_cursor(struct wsdisplay_cursor *c,
587 					       struct netbsd32_wsdisplay_cursor *c32,
588 					       u_long cmd)
589 {
590 	c32->which = c->which;
591 	c32->enable = c->enable;
592 	c32->pos.x = c->pos.x;
593 	c32->pos.y = c->pos.y;
594 	c32->hot.x = c->hot.x;
595 	c32->hot.y = c->hot.y;
596 	c32->size.x = c->size.x;
597 	c32->size.y = c->size.y;
598 	c32->cmap.index = c->cmap.index;
599 	c32->cmap.count = c->cmap.count;
600 	NETBSD32PTR32(c32->cmap.red, c->cmap.red);
601 	NETBSD32PTR32(c32->cmap.green, c->cmap.green);
602 	NETBSD32PTR32(c32->cmap.blue, c->cmap.blue);
603 	NETBSD32PTR32(c32->image, c->image);
604 	NETBSD32PTR32(c32->mask, c->mask);
605 }
606 
607 static inline void
608 netbsd32_from_wsdisplay_cmap(struct wsdisplay_cmap *c,
609 					   struct netbsd32_wsdisplay_cmap *c32,
610 					   u_long cmd)
611 {
612 	c32->index = c->index;
613 	c32->count = c->count;
614 	NETBSD32PTR32(c32->red, c->red);
615 	NETBSD32PTR32(c32->green, c->green);
616 	NETBSD32PTR32(c32->blue, c->blue);
617 }
618 
619 static inline void
620 netbsd32_from_ieee80211req(struct ieee80211req *ireq,
621 			   struct netbsd32_ieee80211req *ireq32, u_long cmd)
622 {
623 	strncpy(ireq32->i_name, ireq->i_name, IFNAMSIZ);
624 	ireq32->i_type = ireq->i_type;
625 	ireq32->i_val = ireq->i_val;
626 	ireq32->i_len = ireq->i_len;
627 	NETBSD32PTR32(ireq32->i_data, ireq->i_data);
628 }
629 
630 static inline void
631 netbsd32_from_ieee80211_nwkey(struct ieee80211_nwkey *nwk,
632 				struct netbsd32_ieee80211_nwkey *nwk32,
633 				u_long cmd)
634 {
635 	int i;
636 
637 	strncpy(nwk32->i_name, nwk->i_name, IFNAMSIZ);
638 	nwk32->i_wepon = nwk->i_wepon;
639 	nwk32->i_defkid = nwk->i_defkid;
640 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
641 		nwk32->i_key[i].i_keylen = nwk->i_key[i].i_keylen;
642 		NETBSD32PTR32(nwk32->i_key[i].i_keydat,
643 				nwk->i_key[i].i_keydat);
644 	}
645 }
646 
647 static inline void
648 netbsd32_from_bpf_program(struct bpf_program *p, struct netbsd32_bpf_program *s32p, u_long cmd)
649 {
650 
651 	NETBSD32PTR32(s32p->bf_insns, p->bf_insns);
652 	s32p->bf_len = p->bf_len;
653 }
654 
655 static inline void
656 netbsd32_from_bpf_dltlist(struct bpf_dltlist *p, struct netbsd32_bpf_dltlist *s32p, u_long cmd)
657 {
658 
659 	NETBSD32PTR32(s32p->bfl_list, p->bfl_list);
660 	s32p->bfl_len = p->bfl_len;
661 }
662 
663 static inline void
664 netbsd32_from_u_long(u_long *p, netbsd32_u_long *s32p, u_long cmd)
665 {
666 
667 	*s32p = (netbsd32_u_long)*p;
668 }
669 
670 static inline void
671 netbsd32_from_clockctl_settimeofday(
672     const struct clockctl_settimeofday *p,
673     struct netbsd32_clockctl_settimeofday *s32p,
674     u_long cmd)
675 {
676 
677 	NETBSD32PTR32(s32p->tv, p->tv);
678 	NETBSD32PTR32(s32p->tzp, p->tzp);
679 }
680 
681 static inline void
682 netbsd32_from_clockctl_adjtime(
683     const struct clockctl_adjtime *p,
684     struct netbsd32_clockctl_adjtime *s32p,
685     u_long cmd)
686 {
687 
688 	NETBSD32PTR32(s32p->delta, p->delta);
689 	NETBSD32PTR32(s32p->olddelta, p->olddelta);
690 }
691 
692 static inline void
693 netbsd32_from_clockctl_clock_settime(
694     const struct clockctl_clock_settime *p,
695     struct netbsd32_clockctl_clock_settime *s32p,
696     u_long cmd)
697 {
698 
699 	s32p->clock_id = p->clock_id;
700 	NETBSD32PTR32(s32p->tp, p->tp);
701 }
702 
703 static inline void
704 netbsd32_from_clockctl_ntp_adjtime(
705     const struct clockctl_ntp_adjtime *p,
706     struct netbsd32_clockctl_ntp_adjtime *s32p,
707     u_long cmd)
708 {
709 
710 	NETBSD32PTR32(s32p->tp, p->tp);
711 	s32p->retval = p->retval;
712 }
713 
714 /*
715  * main ioctl syscall.
716  *
717  * ok, here we are in the biggy.  we have to do fix ups depending
718  * on the ioctl command before and afterwards.
719  */
720 int
721 netbsd32_ioctl(struct lwp *l, const struct netbsd32_ioctl_args *uap, register_t *retval)
722 {
723 	/* {
724 		syscallarg(int) fd;
725 		syscallarg(netbsd32_u_long) com;
726 		syscallarg(netbsd32_voidp) data;
727 	} */
728 	struct proc *p = l->l_proc;
729 	struct file *fp;
730 	struct filedesc *fdp;
731 	u_long com;
732 	int error = 0;
733 	size_t size;
734 	size_t alloc_size32, size32;
735 	void *data, *memp = NULL;
736 	void *data32, *memp32 = NULL;
737 	unsigned int fd;
738 	fdfile_t *ff;
739 	int tmp;
740 #define STK_PARAMS	128
741 	uint64_t stkbuf[STK_PARAMS/sizeof(uint64_t)];
742 	uint64_t stkbuf32[STK_PARAMS/sizeof(uint64_t)];
743 
744 	/*
745 	 * we need to translate some commands (_IOW) before calling sys_ioctl,
746 	 * some after (_IOR), and some both (_IOWR).
747 	 */
748 #if 0
749 	{
750 		const char * const dirs[8] = {
751 		    "NONE!", "VOID", "OUT", "VOID|OUT!", "IN", "VOID|IN!",
752 		    "INOUT", "VOID|IN|OUT!"
753 		};
754 
755 		printf("netbsd32_ioctl(%d, %x, %x): "
756 		    "%s group %c base %d len %d\n",
757 		    SCARG(uap, fd), SCARG(uap, com), SCARG(uap, data).i32,
758 		    dirs[((SCARG(uap, com) & IOC_DIRMASK)>>29)],
759 		    IOCGROUP(SCARG(uap, com)), IOCBASECMD(SCARG(uap, com)),
760 		    IOCPARM_LEN(SCARG(uap, com)));
761 	}
762 #endif
763 
764 	memp = NULL;
765 	memp32 = NULL;
766 	alloc_size32 = 0;
767 	size32 = 0;
768 	size = 0;
769 
770 	fdp = p->p_fd;
771 	fd = SCARG(uap, fd);
772 	if ((fp = fd_getfile(fd)) == NULL)
773 		return (EBADF);
774 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
775 		error = EBADF;
776 		goto out;
777 	}
778 
779 	ff = fdp->fd_dt->dt_ff[SCARG(uap, fd)];
780 	switch (com = SCARG(uap, com)) {
781 	case FIOCLEX:
782 		ff->ff_exclose = true;
783 		fdp->fd_exclose = true;
784 		goto out;
785 
786 	case FIONCLEX:
787 		ff->ff_exclose = false;
788 		goto out;
789 	}
790 
791 	/*
792 	 * Interpret high order word to find amount of data to be
793 	 * copied to/from the user's address space.
794 	 */
795 	size32 = IOCPARM_LEN(com);
796 	alloc_size32 = size32;
797 
798 	/*
799 	 * The disklabel is now padded to a multiple of 8 bytes however the old
800 	 * disklabel on 32bit platforms wasn't.  This leaves a difference in
801 	 * size of 4 bytes between the two but are otherwise identical.
802 	 * To deal with this, we allocate enough space for the new disklabel
803 	 * but only copyin/out the smaller amount.
804 	 */
805 	if (IOCGROUP(com) == 'd') {
806 		u_long ncom = com ^ (DIOCGDINFO ^ DIOCGDINFO32);
807 		switch (ncom) {
808 		case DIOCGDINFO:
809 		case DIOCWDINFO:
810 		case DIOCSDINFO:
811 		case DIOCGDEFLABEL:
812 			com = ncom;
813 			if (IOCPARM_LEN(DIOCGDINFO32) < IOCPARM_LEN(DIOCGDINFO))
814 				alloc_size32 = IOCPARM_LEN(DIOCGDINFO);
815 			break;
816 		}
817 	}
818 	if (alloc_size32 > IOCPARM_MAX) {
819 		error = ENOTTY;
820 		goto out;
821 	}
822 	if (alloc_size32 > sizeof(stkbuf)) {
823 		memp32 = kmem_alloc(alloc_size32, KM_SLEEP);
824 		data32 = memp32;
825 	} else
826 		data32 = (void *)stkbuf32;
827 	if ((com >> IOCPARM_SHIFT) == 0)  {
828 		/* UNIX-style ioctl. */
829 		data32 = SCARG_P32(uap, data);
830 	} else {
831 		if (com&IOC_IN) {
832 			if (size32) {
833 				error = copyin(SCARG_P32(uap, data), data32,
834 				    size32);
835 				if (error) {
836 					goto out;
837 				}
838 				/*
839 				 * The data between size and alloc_size has
840 				 * not been overwritten.  It shouldn't matter
841 				 * but let's clear that anyway.
842 				 */
843 				if (__predict_false(size32 < alloc_size32)) {
844 					memset((char *)data32+size32, 0,
845 					    alloc_size32 - size32);
846 				}
847 				ktrgenio(fd, UIO_WRITE, SCARG_P32(uap, data),
848 				    size32, 0);
849 			} else
850 				*(void **)data32 = SCARG_P32(uap, data);
851 		} else if ((com&IOC_OUT) && size32) {
852 			/*
853 			 * Zero the buffer so the user always
854 			 * gets back something deterministic.
855 			 */
856 			memset(data32, 0, alloc_size32);
857 		} else if (com&IOC_VOID) {
858 			*(void **)data32 = SCARG_P32(uap, data);
859 		}
860 	}
861 
862 	/*
863 	 * convert various structures, pointers, and other objects that
864 	 * change size from 32 bit -> 64 bit, for all ioctl commands.
865 	 */
866 	switch (SCARG(uap, com)) {
867 	case FIONBIO:
868 		mutex_enter(&fp->f_lock);
869 		if ((tmp = *(int *)data32) != 0)
870 			fp->f_flag |= FNONBLOCK;
871 		else
872 			fp->f_flag &= ~FNONBLOCK;
873 		mutex_exit(&fp->f_lock);
874 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (void *)&tmp);
875 		break;
876 
877 	case FIOASYNC:
878 		mutex_enter(&fp->f_lock);
879 		if ((tmp = *(int *)data32) != 0)
880 			fp->f_flag |= FASYNC;
881 		else
882 			fp->f_flag &= ~FASYNC;
883 		mutex_exit(&fp->f_lock);
884 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (void *)&tmp);
885 		break;
886 
887 	case AUDIO_WSEEK32:
888 		IOCTL_CONV_TO(AUDIO_WSEEK, u_long);
889 
890 	case DIOCGPART32:
891 		IOCTL_STRUCT_CONV_TO(DIOCGPART, partinfo);
892 #if 0	/* not implemented by anything */
893 	case DIOCRFORMAT32:
894 		IOCTL_STRUCT_CONV_TO(DIOCRFORMAT, format_op);
895 	case DIOCWFORMAT32:
896 		IOCTL_STRUCT_CONV_TO(DIOCWFORMAT, format_op);
897 #endif
898 
899 	case ATAIOCCOMMAND32:
900 		IOCTL_STRUCT_CONV_TO(ATAIOCCOMMAND, atareq);
901 
902 /*
903  * only a few ifreq syscalls need conversion and those are
904  * all driver specific... XXX
905  */
906 #if 0
907 	case SIOCGADDRROM3232:
908 		IOCTL_STRUCT_CONV_TO(SIOCGADDRROM32, ifreq);
909 	case SIOCGCHIPID32:
910 		IOCTL_STRUCT_CONV_TO(SIOCGCHIPID, ifreq);
911 	case SIOCSIFADDR32:
912 		IOCTL_STRUCT_CONV_TO(SIOCSIFADDR, ifreq);
913 	case OSIOCGIFADDR32:
914 		IOCTL_STRUCT_CONV_TO(OSIOCGIFADDR, ifreq);
915 	case SIOCGIFADDR32:
916 		IOCTL_STRUCT_CONV_TO(SIOCGIFADDR, ifreq);
917 	case SIOCSIFDSTADDR32:
918 		IOCTL_STRUCT_CONV_TO(SIOCSIFDSTADDR, ifreq);
919 	case OSIOCGIFDSTADDR32:
920 		IOCTL_STRUCT_CONV_TO(OSIOCGIFDSTADDR, ifreq);
921 	case SIOCGIFDSTADDR32:
922 		IOCTL_STRUCT_CONV_TO(SIOCGIFDSTADDR, ifreq);
923 	case OSIOCGIFBRDADDR32:
924 		IOCTL_STRUCT_CONV_TO(OSIOCGIFBRDADDR, ifreq);
925 	case SIOCGIFBRDADDR32:
926 		IOCTL_STRUCT_CONV_TO(SIOCGIFBRDADDR, ifreq);
927 	case SIOCSIFBRDADDR32:
928 		IOCTL_STRUCT_CONV_TO(SIOCSIFBRDADDR, ifreq);
929 	case OSIOCGIFNETMASK32:
930 		IOCTL_STRUCT_CONV_TO(OSIOCGIFNETMASK, ifreq);
931 	case SIOCGIFNETMASK32:
932 		IOCTL_STRUCT_CONV_TO(SIOCGIFNETMASK, ifreq);
933 	case SIOCSIFNETMASK32:
934 		IOCTL_STRUCT_CONV_TO(SIOCSIFNETMASK, ifreq);
935 	case SIOCGIFMETRIC32:
936 		IOCTL_STRUCT_CONV_TO(SIOCGIFMETRIC, ifreq);
937 	case SIOCSIFMETRIC32:
938 		IOCTL_STRUCT_CONV_TO(SIOCSIFMETRIC, ifreq);
939 	case SIOCDIFADDR32:
940 		IOCTL_STRUCT_CONV_TO(SIOCDIFADDR, ifreq);
941 	case SIOCADDMULTI32:
942 		IOCTL_STRUCT_CONV_TO(SIOCADDMULTI, ifreq);
943 	case SIOCDELMULTI32:
944 		IOCTL_STRUCT_CONV_TO(SIOCDELMULTI, ifreq);
945 	case SIOCSIFMEDIA32:
946 		IOCTL_STRUCT_CONV_TO(SIOCSIFMEDIA, ifreq);
947 	case SIOCSIFMTU32:
948 		IOCTL_STRUCT_CONV_TO(SIOCSIFMTU, ifreq);
949 	case SIOCGIFMTU32:
950 		IOCTL_STRUCT_CONV_TO(SIOCGIFMTU, ifreq);
951 	case BIOCGETIF32:
952 		IOCTL_STRUCT_CONV_TO(BIOCGETIF, ifreq);
953 	case BIOCSETIF32:
954 		IOCTL_STRUCT_CONV_TO(BIOCSETIF, ifreq);
955 	case SIOCPHASE132:
956 		IOCTL_STRUCT_CONV_TO(SIOCPHASE1, ifreq);
957 	case SIOCPHASE232:
958 		IOCTL_STRUCT_CONV_TO(SIOCPHASE2, ifreq);
959 #endif
960 
961 	case OOSIOCGIFCONF32:
962 		IOCTL_STRUCT_CONV_TO(OOSIOCGIFCONF, ifconf);
963 	case OSIOCGIFCONF32:
964 		IOCTL_STRUCT_CONV_TO(OSIOCGIFCONF, ifconf);
965 	case SIOCGIFCONF32:
966 		IOCTL_STRUCT_CONV_TO(SIOCGIFCONF, ifconf);
967 
968 	case SIOCGIFFLAGS32:
969 		IOCTL_STRUCT_CONV_TO(SIOCGIFFLAGS, ifreq);
970 	case SIOCSIFFLAGS32:
971 		IOCTL_STRUCT_CONV_TO(SIOCSIFFLAGS, ifreq);
972 
973 	case SIOCGIFADDRPREF32:
974 		IOCTL_STRUCT_CONV_TO(SIOCGIFADDRPREF, if_addrprefreq);
975 	case SIOCSIFADDRPREF32:
976 		IOCTL_STRUCT_CONV_TO(SIOCSIFADDRPREF, if_addrprefreq);
977 
978 
979 	case OSIOCGIFFLAGS32:
980 		IOCTL_STRUCT_CONV_TO(OSIOCGIFFLAGS, oifreq);
981 	case OSIOCSIFFLAGS32:
982 		IOCTL_STRUCT_CONV_TO(OSIOCSIFFLAGS, oifreq);
983 
984 	case SIOCGIFMEDIA32:
985 		IOCTL_STRUCT_CONV_TO(SIOCGIFMEDIA, ifmediareq);
986 
987 	case SIOCSDRVSPEC32:
988 		IOCTL_STRUCT_CONV_TO(SIOCSDRVSPEC, ifdrv);
989 
990 	case SIOCGETVIFCNT32:
991 		IOCTL_STRUCT_CONV_TO(SIOCGETVIFCNT, sioc_vif_req);
992 
993 	case SIOCGETSGCNT32:
994 		IOCTL_STRUCT_CONV_TO(SIOCGETSGCNT, sioc_sg_req);
995 
996 	case VNDIOCSET32:
997 		IOCTL_STRUCT_CONV_TO(VNDIOCSET, vnd_ioctl);
998 
999 	case VNDIOCCLR32:
1000 		IOCTL_STRUCT_CONV_TO(VNDIOCCLR, vnd_ioctl);
1001 
1002 	case VNDIOCGET32:
1003 		IOCTL_STRUCT_CONV_TO(VNDIOCGET, vnd_user);
1004 
1005 	case VNDIOCSET5032:
1006 		IOCTL_STRUCT_CONV_TO(VNDIOCSET50, vnd_ioctl50);
1007 
1008 	case VNDIOCCLR5032:
1009 		IOCTL_STRUCT_CONV_TO(VNDIOCCLR50, vnd_ioctl50);
1010 
1011 	case ENVSYS_GETDICTIONARY32:
1012 		IOCTL_STRUCT_CONV_TO(ENVSYS_GETDICTIONARY, plistref);
1013 	case ENVSYS_SETDICTIONARY32:
1014 		IOCTL_STRUCT_CONV_TO(ENVSYS_SETDICTIONARY, plistref);
1015 	case ENVSYS_REMOVEPROPS32:
1016 		IOCTL_STRUCT_CONV_TO(ENVSYS_REMOVEPROPS, plistref);
1017 
1018 	case WDOGIOC_GWDOGS32:
1019 		IOCTL_STRUCT_CONV_TO(WDOGIOC_GWDOGS, wdog_conf);
1020 
1021 	case BIOCSETF32:
1022 		IOCTL_STRUCT_CONV_TO(BIOCSETF, bpf_program);
1023 	case BIOCSTCPF32:
1024 		IOCTL_STRUCT_CONV_TO(BIOCSTCPF, bpf_program);
1025 	case BIOCSUDPF32:
1026 		IOCTL_STRUCT_CONV_TO(BIOCSUDPF, bpf_program);
1027 	case BIOCGDLTLIST32:
1028 		IOCTL_STRUCT_CONV_TO(BIOCGDLTLIST, bpf_dltlist);
1029 
1030 	case WSDISPLAYIO_ADDSCREEN32:
1031 		IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_ADDSCREEN, wsdisplay_addscreendata);
1032 
1033 	case WSDISPLAYIO_GCURSOR32:
1034 		IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_GCURSOR, wsdisplay_cursor);
1035 	case WSDISPLAYIO_SCURSOR32:
1036 		IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_SCURSOR, wsdisplay_cursor);
1037 
1038 	case WSDISPLAYIO_GETCMAP32:
1039 		IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_GETCMAP, wsdisplay_cmap);
1040 	case WSDISPLAYIO_PUTCMAP32:
1041 		IOCTL_STRUCT_CONV_TO(WSDISPLAYIO_PUTCMAP, wsdisplay_cmap);
1042 
1043 	case SIOCS8021132:
1044 		IOCTL_STRUCT_CONV_TO(SIOCS80211, ieee80211req);
1045 	case SIOCG8021132:
1046 		IOCTL_STRUCT_CONV_TO(SIOCG80211, ieee80211req);
1047 	case SIOCS80211NWKEY32:
1048 		IOCTL_STRUCT_CONV_TO(SIOCS80211NWKEY, ieee80211_nwkey);
1049 	case SIOCG80211NWKEY32:
1050 		IOCTL_STRUCT_CONV_TO(SIOCG80211NWKEY, ieee80211_nwkey);
1051 
1052 	case POWER_EVENT_RECVDICT32:
1053 		IOCTL_STRUCT_CONV_TO(POWER_EVENT_RECVDICT, plistref);
1054 
1055 	case CLOCKCTL_SETTIMEOFDAY32:
1056 		IOCTL_STRUCT_CONV_TO(CLOCKCTL_SETTIMEOFDAY,
1057 		    clockctl_settimeofday);
1058 	case CLOCKCTL_ADJTIME32:
1059 		IOCTL_STRUCT_CONV_TO(CLOCKCTL_ADJTIME, clockctl_adjtime);
1060 	case CLOCKCTL_CLOCK_SETTIME32:
1061 		IOCTL_STRUCT_CONV_TO(CLOCKCTL_CLOCK_SETTIME,
1062 		    clockctl_clock_settime);
1063 	case CLOCKCTL_NTP_ADJTIME32:
1064 		IOCTL_STRUCT_CONV_TO(CLOCKCTL_NTP_ADJTIME,
1065 		    clockctl_ntp_adjtime);
1066 
1067 	default:
1068 #ifdef NETBSD32_MD_IOCTL
1069 		error = netbsd32_md_ioctl(fp, com, data32, l);
1070 #else
1071 		error = (*fp->f_ops->fo_ioctl)(fp, com, data32);
1072 #endif
1073 		break;
1074 	}
1075 
1076 	if (error == EPASSTHROUGH)
1077 		error = ENOTTY;
1078 
1079 	/*
1080 	 * Copy any data to user, size was
1081 	 * already set and checked above.
1082 	 */
1083 	if (error == 0 && (com&IOC_OUT) && size32) {
1084 		error = copyout(data32, SCARG_P32(uap, data), size32);
1085 		ktrgenio(fd, UIO_READ, SCARG_P32(uap, data),
1086 		    size32, error);
1087 	}
1088 
1089  out:
1090 	/* If we allocated data, free it here. */
1091 	if (memp32)
1092 		kmem_free(memp32, alloc_size32);
1093 	if (memp)
1094 		kmem_free(memp, size);
1095 	fd_putfile(fd);
1096 	return (error);
1097 }
1098