xref: /netbsd-src/sys/compat/netbsd32/netbsd32_ioctl.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: netbsd32_ioctl.c,v 1.1 1998/08/26 10:20:35 mrg Exp $	*/
2 
3 /*
4  * Copyright (c) 1998 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  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 /*
32  * handle ioctl conversions from sparc32 -> sparc64
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/audioio.h>
38 #include <sys/disklabel.h>
39 #include <sys/dkio.h>
40 #include <sys/malloc.h>
41 #include <sys/proc.h>
42 #include <sys/sockio.h>
43 #include <sys/socket.h>
44 #include <sys/ttycom.h>
45 #include <sys/mount.h>
46 #include <sys/syscallargs.h>
47 
48 #include <machine/fbio.h>
49 #include <machine/openpromio.h>
50 
51 #include <net/if.h>
52 #include <net/route.h>
53 
54 #include <netinet/in.h>
55 #include <netinet/in_var.h>
56 #include <netinet/igmp.h>
57 #include <netinet/igmp_var.h>
58 #include <netinet/ip_mroute.h>
59 
60 #include <compat/sparc32/sparc32.h>
61 #include <compat/sparc32/sparc32_ioctl.h>
62 #include <compat/sparc32/sparc32_syscallargs.h>
63 
64 void
65 sparc32_to_fbcmap(s32p, p)
66 	struct sparc32_fbcmap *s32p;
67 	struct fbcmap *p;
68 {
69 
70 	p->index = s32p->index;
71 	p->count = s32p->count;
72 	p->red = (u_char *)(u_long)s32p->red;
73 	p->green = (u_char *)(u_long)s32p->green;
74 	p->blue = (u_char *)(u_long)s32p->blue;
75 }
76 
77 void
78 sparc32_to_fbcursor(s32p, p)
79 	struct sparc32_fbcursor *s32p;
80 	struct fbcursor *p;
81 {
82 
83 	p->set = s32p->set;
84 	p->enable = s32p->enable;
85 	p->pos = s32p->pos;
86 	p->hot = s32p->hot;
87 	sparc32_to_fbcmap(&s32p->cmap, &p->cmap);
88 	p->size = s32p->size;
89 	p->image = (char *)(u_long)s32p->image;
90 	p->mask = (char *)(u_long)s32p->mask;
91 }
92 
93 void
94 sparc32_to_opiocdesc(s32p, p)
95 	struct sparc32_opiocdesc *s32p;
96 	struct opiocdesc *p;
97 {
98 
99 	p->op_nodeid = s32p->op_nodeid;
100 	p->op_namelen = s32p->op_namelen;
101 	p->op_name = (char *)(u_long)s32p->op_name;
102 	p->op_buflen = s32p->op_buflen;
103 	p->op_buf = (char *)(u_long)s32p->op_buf;
104 }
105 
106 void
107 sparc32_to_partinfo(s32p, p)
108 	struct sparc32_partinfo *s32p;
109 	struct partinfo *p;
110 {
111 
112 	p->disklab = (struct disklabel *)(u_long)s32p->disklab;
113 	p->part = (struct partition *)(u_long)s32p->part;
114 }
115 
116 void
117 sparc32_to_format_op(s32p, p)
118 	struct sparc32_format_op *s32p;
119 	struct format_op *p;
120 {
121 
122 	p->df_buf = (char *)(u_long)s32p->df_buf;
123 	p->df_count = s32p->df_count;
124 	p->df_startblk = s32p->df_startblk;
125 	memcpy(p->df_reg, s32p->df_reg, sizeof(s32p->df_reg));
126 }
127 
128 #if 0 /* XXX see below */
129 void
130 sparc32_to_ifreq(s32p, p, cmd)
131 	struct sparc32_ifreq *s32p;
132 	struct ifreq *p;
133 	u_long cmd;	/* XXX unused yet */
134 {
135 
136 	/*
137 	 * XXX
138 	 * struct ifreq says the same, but sometimes the ifr_data
139 	 * union member needs to be converted to 64 bits... this
140 	 * is very driver specific and so we ignore it for now..
141 	 */
142 	memcpy(p, s32p, sizeof *s32p);
143 }
144 #endif
145 
146 void
147 sparc32_to_ifconf(s32p, p)
148 	struct sparc32_ifconf *s32p;
149 	struct ifconf *p;
150 {
151 
152 	p->ifc_len = s32p->ifc_len;
153 	/* ifc_buf & ifc_req are the same size so this works */
154 	p->ifc_buf = (caddr_t)(u_long)s32p->ifc_buf;
155 }
156 
157 void
158 sparc32_to_ifmediareq(s32p, p)
159 	struct sparc32_ifmediareq *s32p;
160 	struct ifmediareq *p;
161 {
162 
163 	memcpy(p, s32p, sizeof *s32p);
164 	p->ifm_ulist = (int *)(u_long)s32p->ifm_ulist;
165 }
166 
167 void
168 sparc32_to_ifdrv(s32p, p)
169 	struct sparc32_ifdrv *s32p;
170 	struct ifdrv *p;
171 {
172 
173 	memcpy(p, s32p, sizeof *s32p);
174 	p->ifd_data = (void *)(u_long)s32p->ifd_data;
175 }
176 
177 void
178 sparc32_to_sioc_vif_req(s32p, p)
179 	struct sparc32_sioc_vif_req *s32p;
180 	struct sioc_vif_req *p;
181 {
182 
183 	p->vifi = s32p->vifi;
184 	p->icount = (u_long)s32p->icount;
185 	p->ocount = (u_long)s32p->ocount;
186 	p->ibytes = (u_long)s32p->ibytes;
187 	p->obytes = (u_long)s32p->obytes;
188 }
189 
190 void
191 sparc32_to_sioc_sg_req(s32p, p)
192 	struct sparc32_sioc_sg_req *s32p;
193 	struct sioc_sg_req *p;
194 {
195 
196 	p->src = s32p->src;
197 	p->grp = s32p->grp;
198 	p->pktcnt = (u_long)s32p->pktcnt;
199 	p->bytecnt = (u_long)s32p->bytecnt;
200 	p->wrong_if = (u_long)s32p->wrong_if;
201 }
202 
203 /*
204  * handle ioctl conversions from sparc64 -> sparc32
205  */
206 
207 void
208 sparc32_from_fbcmap(p, s32p)
209 	struct fbcmap *p;
210 	struct sparc32_fbcmap *s32p;
211 {
212 
213 	s32p->index = p->index;
214 	s32p->count = p->count;
215 /* filled in */
216 #if 0
217 	s32p->red = (sparc32_u_charp)p->red;
218 	s32p->green = (sparc32_u_charp)p->green;
219 	s32p->blue = (sparc32_u_charp)p->blue;
220 #endif
221 }
222 
223 void
224 sparc32_from_fbcursor(p, s32p)
225 	struct fbcursor *p;
226 	struct sparc32_fbcursor *s32p;
227 {
228 
229 	s32p->set = p->set;
230 	s32p->enable = p->enable;
231 	s32p->pos = p->pos;
232 	s32p->hot = p->hot;
233 	sparc32_from_fbcmap(&p->cmap, &s32p->cmap);
234 	s32p->size = p->size;
235 /* filled in */
236 #if 0
237 	s32p->image = (sparc32_charp)p->image;
238 	s32p->mask = (sparc32_charp)p->mask;
239 #endif
240 }
241 
242 void
243 sparc32_from_opiocdesc(p, s32p)
244 	struct opiocdesc *p;
245 	struct sparc32_opiocdesc *s32p;
246 {
247 
248 	s32p->op_nodeid = p->op_nodeid;
249 	s32p->op_namelen = p->op_namelen;
250 	s32p->op_name = (sparc32_charp)(u_long)p->op_name;
251 	s32p->op_buflen = p->op_buflen;
252 	s32p->op_buf = (sparc32_charp)(u_long)p->op_buf;
253 }
254 
255 void
256 sparc32_from_format_op(p, s32p)
257 	struct format_op *p;
258 	struct sparc32_format_op *s32p;
259 {
260 
261 /* filled in */
262 #if 0
263 	s32p->df_buf = (sparc32_charp)p->df_buf;
264 #endif
265 	s32p->df_count = p->df_count;
266 	s32p->df_startblk = p->df_startblk;
267 	memcpy(s32p->df_reg, p->df_reg, sizeof(p->df_reg));
268 }
269 
270 #if 0 /* XXX see below */
271 void
272 sparc32_from_ifreq(p, s32p, cmd)
273 	struct ifreq *p;
274 	struct sparc32_ifreq *s32p;
275 	u_long cmd;	/* XXX unused yet */
276 {
277 
278 	/*
279 	 * XXX
280 	 * struct ifreq says the same, but sometimes the ifr_data
281 	 * union member needs to be converted to 64 bits... this
282 	 * is very driver specific and so we ignore it for now..
283 	 */
284 	*s32p = *p;
285 }
286 #endif
287 
288 void
289 sparc32_from_ifconf(p, s32p)
290 	struct ifconf *p;
291 	struct sparc32_ifconf *s32p;
292 {
293 
294 	s32p->ifc_len = p->ifc_len;
295 	/* ifc_buf & ifc_req are the same size so this works */
296 	s32p->ifc_buf = (sparc32_caddr_t)(u_long)p->ifc_buf;
297 }
298 
299 void
300 sparc32_from_ifmediareq(p, s32p)
301 	struct ifmediareq *p;
302 	struct sparc32_ifmediareq *s32p;
303 {
304 
305 	memcpy(s32p, p, sizeof *p);
306 /* filled in? */
307 #if 0
308 	s32p->ifm_ulist = (sparc32_intp_t)p->ifm_ulist;
309 #endif
310 }
311 
312 void
313 sparc32_from_ifdrv(p, s32p)
314 	struct ifdrv *p;
315 	struct sparc32_ifdrv *s32p;
316 {
317 
318 	memcpy(s32p, p, sizeof *p);
319 /* filled in? */
320 #if 0
321 	s32p->ifm_data = (sparc32_u_longp_t)p->ifm_data;
322 #endif
323 }
324 
325 void
326 sparc32_from_sioc_vif_req(p, s32p)
327 	struct sioc_vif_req *p;
328 	struct sparc32_sioc_vif_req *s32p;
329 {
330 
331 	s32p->vifi = p->vifi;
332 	s32p->icount = (sparc32_u_long)p->icount;
333 	s32p->ocount = (sparc32_u_long)p->ocount;
334 	s32p->ibytes = (sparc32_u_long)p->ibytes;
335 	s32p->obytes = (sparc32_u_long)p->obytes;
336 }
337 
338 void
339 sparc32_from_sioc_sg_req(p, s32p)
340 	struct sioc_sg_req *p;
341 	struct sparc32_sioc_sg_req *s32p;
342 {
343 
344 	s32p->src = p->src;
345 	s32p->grp = p->grp;
346 	s32p->pktcnt = (sparc32_u_long)p->pktcnt;
347 	s32p->bytecnt = (sparc32_u_long)p->bytecnt;
348 	s32p->wrong_if = (sparc32_u_long)p->wrong_if;
349 }
350 
351 
352 /*
353  * main ioctl syscall.
354  *
355  * ok, here we are in the biggy.  we have to do fix ups depending
356  * on the ioctl command before and afterwards.
357  */
358 int
359 compat_sparc32_ioctl(p, v, retval)
360 	struct proc *p;
361 	void *v;
362 	register_t *retval;
363 {
364 	struct compat_sparc32_ioctl_args /* {
365 		syscallarg(int) fd;
366 		syscallarg(sparc32_u_long) com;
367 		syscallarg(sparc32_voidp) data;
368 	} */ *uap = v;
369 	struct sys_ioctl_args ua;
370 	void *data = NULL;
371 	int rv;
372 
373 	/*
374 	 * we need to translate some commands (_IOW) before calling sys_ioctl,
375 	 * some after (_IOR), and some both (_IOWR).
376 	 */
377 
378 /* we define some handy macros here... */
379 #define IOCTL_STRUCT_CONV_TO(type)	\
380 	data = malloc(sizeof(struct type), M_TEMP, M_WAITOK); \
381 	__CONCAT(sparc32_to_, type)((struct __CONCAT(sparc32_, type) *) \
382 	    (u_long)SCARG(uap, data), data)
383 
384 #define IOCTL_STRUCT_CONV_CMD_TO(type, cmd)	\
385 	data = malloc(sizeof(struct type), M_TEMP, M_WAITOK); \
386 	__CONCAT(sparc32_to_, type)((struct __CONCAT(sparc32_, type) *) \
387 	    (u_long)SCARG(uap, data), data, cmd)
388 
389 #define IOCTL_STRUCT_CONV_FROM(type)	\
390 	__CONCAT(sparc32_from_, type)(data, \
391 	    (struct __CONCAT(sparc32_, type) *) (u_long)SCARG(uap, data))
392 
393 #define IOCTL_STRUCT_CONV_CMD_FROM(type, cmd)	\
394 	__CONCAT(sparc32_from_, type)(data, \
395 	    (struct __CONCAT(sparc32_, type) *) (u_long)SCARG(uap, data), cmd)
396 
397 	/*
398 	 * convert various structures, pointers, and other objects that
399 	 * change size from 32 bit -> 64 bit, for all ioctl commands.
400 	 */
401 	switch (SCARG(uap, com)) {
402 	case FBIOPUTCMAP:
403 	case FBIOGETCMAP:
404 		IOCTL_STRUCT_CONV_TO(fbcmap);
405 		break;
406 
407 	case FBIOSCURSOR:
408 	case FBIOGCURSOR:
409 		IOCTL_STRUCT_CONV_TO(fbcursor);
410 		break;
411 
412 	case OPIOCGET:
413 	case OPIOCSET:
414 	case OPIOCNEXTPROP:
415 		IOCTL_STRUCT_CONV_TO(opiocdesc);
416 		break;
417 
418 	case DIOCGPART:
419 		IOCTL_STRUCT_CONV_TO(partinfo);
420 		break;
421 
422 	case DIOCRFORMAT:
423 	case DIOCWFORMAT:
424 		IOCTL_STRUCT_CONV_TO(format_op);
425 		break;
426 
427 /*
428  * only a few ifreq syscalls need conversion and those are
429  * all driver specific... XXX
430  */
431 #if 0
432 	case SIOCGADDRROM:
433 	case SIOCGCHIPID:
434 	case SIOCSIFADDR:
435 	case OSIOCGIFADDR:
436 	case SIOCGIFADDR:
437 	case SIOCSIFDSTADDR:
438 	case OSIOCGIFDSTADDR:
439 	case SIOCGIFDSTADDR:
440 	case SIOCSIFFLAGS:
441 	case SIOCGIFFLAGS:
442 	case OSIOCGIFBRDADDR:
443 	case SIOCGIFBRDADDR:
444 	case SIOCSIFBRDADDR:
445 	case OSIOCGIFNETMASK:
446 	case SIOCGIFNETMASK:
447 	case SIOCSIFNETMASK:
448 	case SIOCGIFMETRIC:
449 	case SIOCSIFMETRIC:
450 	case SIOCDIFADDR:
451 	case SIOCADDMULTI:
452 	case SIOCDELMULTI:
453 	case SIOCSIFMEDIA:
454 	case SIOCSIFMTU:
455 	case SIOCGIFMTU:
456 	case SIOCSIFASYNCMAP:
457 	case SIOCGIFASYNCMAP:
458 /*	case BIOCGETIF: READ ONLY */
459 	case BIOCSETIF:
460 	case SIOCPHASE1:
461 	case SIOCPHASE2:
462 		IOCTL_STRUCT_CONV_CMD_TO(ifreq, SCARG(uap, cmd));
463 		break;
464 #endif
465 
466 	case OSIOCGIFCONF:
467 	case SIOCGIFCONF:
468 		IOCTL_STRUCT_CONV_TO(ifconf);
469 		break;
470 
471 	case SIOCGIFMEDIA:
472 		IOCTL_STRUCT_CONV_TO(ifmediareq);
473 		break;
474 
475 	case SIOCSDRVSPEC:
476 		IOCTL_STRUCT_CONV_TO(ifdrv);
477 		break;
478 
479 	case SIOCGETVIFCNT:
480 		IOCTL_STRUCT_CONV_TO(sioc_vif_req);
481 		break;
482 
483 	case SIOCGETSGCNT:
484 		IOCTL_STRUCT_CONV_TO(sioc_sg_req);
485 		break;
486 
487 	}
488 
489 	/*
490 	 * if we malloced a new data segment, plug it into the
491 	 * syscall args, otherwise copy incoming one as a void
492 	 * pointer.  also copy the rest of the syscall args...
493 	 */
494 	if (data)
495 		SCARG(&ua, data) = data;
496 	else
497 		SPARC32TOP_UAP(data, void);
498 	SPARC32TO64_UAP(fd);
499 	SPARC32TOX_UAP(com, u_long);
500 
501 	/* call the real ioctl */
502 	rv = sys_ioctl(p, &ua, retval);
503 
504 	/*
505 	 * convert _back_ to 32 bit the results of the command.
506 	 */
507 	switch (SCARG(uap, com)) {
508 	case FBIOGETCMAP:
509 		IOCTL_STRUCT_CONV_FROM(fbcmap);
510 		break;
511 
512 	case FBIOGCURSOR:
513 		IOCTL_STRUCT_CONV_FROM(fbcursor);
514 		break;
515 
516 	case OPIOCGET:
517 	case OPIOCNEXTPROP:
518 		IOCTL_STRUCT_CONV_FROM(opiocdesc);
519 		break;
520 
521 	case DIOCRFORMAT:
522 	case DIOCWFORMAT:
523 		IOCTL_STRUCT_CONV_FROM(format_op);
524 		break;
525 
526 /*
527  * only a few ifreq syscalls need conversion and those are
528  * all driver specific... XXX
529  */
530 #if 0
531 	case SIOCGADDRROM:
532 	case SIOCGCHIPID:
533 	case SIOCSIFADDR:
534 	case OSIOCGIFADDR:
535 	case SIOCGIFADDR:
536 	case SIOCSIFDSTADDR:
537 	case OSIOCGIFDSTADDR:
538 	case SIOCGIFDSTADDR:
539 	case SIOCSIFFLAGS:
540 	case SIOCGIFFLAGS:
541 	case OSIOCGIFBRDADDR:
542 	case SIOCGIFBRDADDR:
543 	case SIOCSIFBRDADDR:
544 	case OSIOCGIFNETMASK:
545 	case SIOCGIFNETMASK:
546 	case SIOCSIFNETMASK:
547 	case SIOCGIFMETRIC:
548 	case SIOCSIFMETRIC:
549 	case SIOCDIFADDR:
550 	case SIOCADDMULTI:
551 	case SIOCDELMULTI:
552 	case SIOCSIFMEDIA:
553 	case SIOCSIFMTU:
554 	case SIOCGIFMTU:
555 	case SIOCSIFASYNCMAP:
556 	case SIOCGIFASYNCMAP:
557 /*	case BIOCGETIF: READ ONLY */
558 	case BIOCSETIF:
559 	case SIOCPHASE1:
560 	case SIOCPHASE2:
561 		IOCTL_STRUCT_CONV_CMD_FROM(ifreq, SCARG(uap, cmd));
562 		break;
563 #endif
564 
565 	case OSIOCGIFCONF:
566 	case SIOCGIFCONF:
567 		IOCTL_STRUCT_CONV_FROM(ifconf);
568 		break;
569 
570 	case SIOCGIFMEDIA:
571 		IOCTL_STRUCT_CONV_FROM(ifmediareq);
572 		break;
573 
574 	case SIOCSDRVSPEC:
575 		IOCTL_STRUCT_CONV_FROM(ifdrv);
576 		break;
577 
578 	case SIOCGETVIFCNT:
579 		IOCTL_STRUCT_CONV_FROM(sioc_vif_req);
580 		break;
581 
582 	case SIOCGETSGCNT:
583 		IOCTL_STRUCT_CONV_FROM(sioc_sg_req);
584 		break;
585 	}
586 
587 	/* if we malloced data, free it here */
588 	if (data)
589 		free(data, M_TEMP);
590 
591 	/* done! */
592 	return (rv);
593 }
594