Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_ioctl.c revision 1.53
      1  1.53      matt /*	$NetBSD: netbsd32_ioctl.c,v 1.53 2011/01/22 20:51:21 matt Exp $	*/
      2   1.1       mrg 
      3   1.1       mrg /*
      4  1.11       mrg  * Copyright (c) 1998, 2001 Matthew R. Green
      5   1.1       mrg  * All rights reserved.
      6   1.1       mrg  *
      7   1.1       mrg  * Redistribution and use in source and binary forms, with or without
      8   1.1       mrg  * modification, are permitted provided that the following conditions
      9   1.1       mrg  * are met:
     10   1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     11   1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     12   1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       mrg  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       mrg  *    documentation and/or other materials provided with the distribution.
     15   1.1       mrg  *
     16   1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1       mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1       mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1       mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1       mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21   1.1       mrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22   1.1       mrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23   1.1       mrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24   1.1       mrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1       mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1       mrg  * SUCH DAMAGE.
     27   1.1       mrg  */
     28   1.1       mrg 
     29   1.1       mrg /*
     30  1.14       mrg  * handle ioctl conversions from netbsd32 -> 64-bit kernel
     31   1.1       mrg  */
     32  1.13     lukem 
     33  1.13     lukem #include <sys/cdefs.h>
     34  1.53      matt __KERNEL_RCSID(0, "$NetBSD: netbsd32_ioctl.c,v 1.53 2011/01/22 20:51:21 matt Exp $");
     35   1.1       mrg 
     36   1.1       mrg #include <sys/param.h>
     37   1.1       mrg #include <sys/systm.h>
     38   1.7       eeh #include <sys/filedesc.h>
     39   1.7       eeh #include <sys/ioctl.h>
     40   1.7       eeh #include <sys/file.h>
     41   1.7       eeh #include <sys/proc.h>
     42   1.7       eeh #include <sys/socketvar.h>
     43   1.1       mrg #include <sys/audioio.h>
     44   1.1       mrg #include <sys/disklabel.h>
     45   1.1       mrg #include <sys/dkio.h>
     46   1.1       mrg #include <sys/sockio.h>
     47   1.1       mrg #include <sys/socket.h>
     48   1.1       mrg #include <sys/ttycom.h>
     49   1.1       mrg #include <sys/mount.h>
     50   1.1       mrg #include <sys/syscallargs.h>
     51  1.33     njoly #include <sys/ktrace.h>
     52  1.38        ad #include <sys/kmem.h>
     53  1.51     njoly #include <sys/envsys.h>
     54   1.1       mrg 
     55  1.10      fvdl #ifdef __sparc__
     56  1.12   thorpej #include <dev/sun/fbio.h>
     57   1.1       mrg #include <machine/openpromio.h>
     58  1.10      fvdl #endif
     59   1.1       mrg 
     60   1.1       mrg #include <net/if.h>
     61   1.1       mrg #include <net/route.h>
     62   1.1       mrg 
     63   1.1       mrg #include <netinet/in.h>
     64   1.1       mrg #include <netinet/in_var.h>
     65   1.1       mrg #include <netinet/igmp.h>
     66   1.1       mrg #include <netinet/igmp_var.h>
     67   1.1       mrg #include <netinet/ip_mroute.h>
     68   1.1       mrg 
     69  1.34  christos #include <compat/sys/sockio.h>
     70  1.34  christos 
     71   1.2       mrg #include <compat/netbsd32/netbsd32.h>
     72   1.2       mrg #include <compat/netbsd32/netbsd32_ioctl.h>
     73   1.2       mrg #include <compat/netbsd32/netbsd32_syscallargs.h>
     74   1.1       mrg 
     75  1.49       mrg #include <dev/vndvar.h>
     76  1.49       mrg 
     77   1.6       mrg /* prototypes for the converters */
     78  1.25     perry static inline void netbsd32_to_partinfo(struct netbsd32_partinfo *,
     79  1.22       mrg 					  struct partinfo *, u_long);
     80  1.17      fvdl #if 0
     81  1.25     perry static inline void netbsd32_to_format_op(struct netbsd32_format_op *,
     82  1.22       mrg 					   struct format_op *, u_long);
     83  1.17      fvdl #endif
     84  1.43  christos static inline void netbsd32_to_oifreq(struct netbsd32_oifreq *, struct oifreq *,
     85  1.43  christos 				       u_long cmd);
     86  1.25     perry static inline void netbsd32_to_ifreq(struct netbsd32_ifreq *, struct ifreq *,
     87  1.22       mrg 				       u_long cmd);
     88  1.25     perry static inline void netbsd32_to_ifconf(struct netbsd32_ifconf *,
     89  1.22       mrg 					struct ifconf *, u_long);
     90  1.25     perry static inline void netbsd32_to_ifmediareq(struct netbsd32_ifmediareq *,
     91  1.22       mrg 					    struct ifmediareq *, u_long);
     92  1.25     perry static inline void netbsd32_to_ifdrv(struct netbsd32_ifdrv *, struct ifdrv *,
     93  1.22       mrg 				       u_long);
     94  1.25     perry static inline void netbsd32_to_sioc_vif_req(struct netbsd32_sioc_vif_req *,
     95  1.22       mrg 					      struct sioc_vif_req *, u_long);
     96  1.25     perry static inline void netbsd32_to_sioc_sg_req(struct netbsd32_sioc_sg_req *,
     97  1.22       mrg 					     struct sioc_sg_req *, u_long);
     98  1.25     perry static inline void netbsd32_from_partinfo(struct partinfo *,
     99  1.22       mrg 					    struct netbsd32_partinfo *, u_long);
    100  1.17      fvdl #if 0
    101  1.25     perry static inline void netbsd32_from_format_op(struct format_op *,
    102  1.22       mrg 					     struct netbsd32_format_op *,
    103  1.22       mrg 					     u_long);
    104  1.22       mrg #endif
    105  1.25     perry static inline void netbsd32_from_ifreq(struct ifreq *,
    106  1.22       mrg                                          struct netbsd32_ifreq *, u_long);
    107  1.43  christos static inline void netbsd32_from_oifreq(struct oifreq *,
    108  1.43  christos                                          struct netbsd32_oifreq *, u_long);
    109  1.25     perry static inline void netbsd32_from_ifconf(struct ifconf *,
    110  1.22       mrg 					  struct netbsd32_ifconf *, u_long);
    111  1.25     perry static inline void netbsd32_from_ifmediareq(struct ifmediareq *,
    112  1.22       mrg 					      struct netbsd32_ifmediareq *,
    113  1.22       mrg 					      u_long);
    114  1.25     perry static inline void netbsd32_from_ifdrv(struct ifdrv *,
    115  1.22       mrg 					 struct netbsd32_ifdrv *, u_long);
    116  1.25     perry static inline void netbsd32_from_sioc_vif_req(struct sioc_vif_req *,
    117  1.22       mrg 						struct netbsd32_sioc_vif_req *,
    118  1.22       mrg 						u_long);
    119  1.25     perry static inline void netbsd32_from_sioc_sg_req(struct sioc_sg_req *,
    120  1.22       mrg 					       struct netbsd32_sioc_sg_req *,
    121  1.22       mrg 					       u_long);
    122   1.6       mrg 
    123   1.6       mrg /* convert to/from different structures */
    124   1.6       mrg 
    125  1.25     perry static inline void
    126  1.36       dsl netbsd32_to_partinfo(struct netbsd32_partinfo *s32p, struct partinfo *p, u_long cmd)
    127   1.1       mrg {
    128   1.1       mrg 
    129  1.15       scw 	p->disklab = (struct disklabel *)NETBSD32PTR64(s32p->disklab);
    130  1.15       scw 	p->part = (struct partition *)NETBSD32PTR64(s32p->part);
    131   1.1       mrg }
    132   1.1       mrg 
    133  1.17      fvdl #if 0
    134  1.25     perry static inline void
    135  1.36       dsl netbsd32_to_format_op(struct netbsd32_format_op *s32p, struct format_op *p, u_long cmd)
    136   1.1       mrg {
    137   1.1       mrg 
    138  1.15       scw 	p->df_buf = (char *)NETBSD32PTR64(s32p->df_buf);
    139   1.1       mrg 	p->df_count = s32p->df_count;
    140   1.1       mrg 	p->df_startblk = s32p->df_startblk;
    141   1.1       mrg 	memcpy(p->df_reg, s32p->df_reg, sizeof(s32p->df_reg));
    142   1.1       mrg }
    143  1.17      fvdl #endif
    144   1.1       mrg 
    145  1.25     perry static inline void
    146  1.36       dsl netbsd32_to_ifreq(struct netbsd32_ifreq *s32p, struct ifreq *p, u_long cmd)
    147   1.1       mrg {
    148   1.1       mrg 
    149  1.22       mrg 	memcpy(p, s32p, sizeof *s32p);
    150   1.1       mrg 	/*
    151   1.1       mrg 	 * XXX
    152   1.1       mrg 	 * struct ifreq says the same, but sometimes the ifr_data
    153   1.1       mrg 	 * union member needs to be converted to 64 bits... this
    154   1.1       mrg 	 * is very driver specific and so we ignore it for now..
    155   1.1       mrg 	 */
    156  1.22       mrg 	if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
    157  1.29  christos 		p->ifr_data = (void *)NETBSD32PTR64(s32p->ifr_data);
    158   1.1       mrg }
    159   1.1       mrg 
    160  1.25     perry static inline void
    161  1.43  christos netbsd32_to_oifreq(struct netbsd32_oifreq *s32p, struct oifreq *p, u_long cmd)
    162  1.43  christos {
    163  1.43  christos 
    164  1.43  christos 	memcpy(p, s32p, sizeof *s32p);
    165  1.43  christos 	/*
    166  1.43  christos 	 * XXX
    167  1.43  christos 	 * struct ifreq says the same, but sometimes the ifr_data
    168  1.43  christos 	 * union member needs to be converted to 64 bits... this
    169  1.43  christos 	 * is very driver specific and so we ignore it for now..
    170  1.43  christos 	 */
    171  1.43  christos 	if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
    172  1.43  christos 		p->ifr_data = (void *)NETBSD32PTR64(s32p->ifr_data);
    173  1.43  christos }
    174  1.43  christos 
    175  1.43  christos static inline void
    176  1.36       dsl netbsd32_to_ifconf(struct netbsd32_ifconf *s32p, struct ifconf *p, u_long cmd)
    177   1.1       mrg {
    178   1.1       mrg 
    179   1.1       mrg 	p->ifc_len = s32p->ifc_len;
    180   1.1       mrg 	/* ifc_buf & ifc_req are the same size so this works */
    181  1.29  christos 	p->ifc_buf = (void *)NETBSD32PTR64(s32p->ifc_buf);
    182   1.1       mrg }
    183   1.1       mrg 
    184  1.25     perry static inline void
    185  1.36       dsl netbsd32_to_ifmediareq(struct netbsd32_ifmediareq *s32p, struct ifmediareq *p, u_long cmd)
    186   1.1       mrg {
    187   1.1       mrg 
    188   1.1       mrg 	memcpy(p, s32p, sizeof *s32p);
    189  1.15       scw 	p->ifm_ulist = (int *)NETBSD32PTR64(s32p->ifm_ulist);
    190   1.1       mrg }
    191   1.1       mrg 
    192  1.25     perry static inline void
    193  1.36       dsl netbsd32_to_ifdrv(struct netbsd32_ifdrv *s32p, struct ifdrv *p, u_long cmd)
    194   1.1       mrg {
    195   1.1       mrg 
    196   1.1       mrg 	memcpy(p, s32p, sizeof *s32p);
    197  1.15       scw 	p->ifd_data = (void *)NETBSD32PTR64(s32p->ifd_data);
    198   1.1       mrg }
    199   1.1       mrg 
    200  1.25     perry static inline void
    201  1.36       dsl netbsd32_to_sioc_vif_req(struct netbsd32_sioc_vif_req *s32p, struct sioc_vif_req *p, u_long cmd)
    202   1.1       mrg {
    203   1.1       mrg 
    204   1.1       mrg 	p->vifi = s32p->vifi;
    205   1.1       mrg 	p->icount = (u_long)s32p->icount;
    206   1.1       mrg 	p->ocount = (u_long)s32p->ocount;
    207   1.1       mrg 	p->ibytes = (u_long)s32p->ibytes;
    208   1.1       mrg 	p->obytes = (u_long)s32p->obytes;
    209   1.1       mrg }
    210   1.1       mrg 
    211  1.25     perry static inline void
    212  1.36       dsl netbsd32_to_sioc_sg_req(struct netbsd32_sioc_sg_req *s32p, struct sioc_sg_req *p, u_long cmd)
    213   1.1       mrg {
    214   1.1       mrg 
    215   1.1       mrg 	p->src = s32p->src;
    216   1.1       mrg 	p->grp = s32p->grp;
    217   1.1       mrg 	p->pktcnt = (u_long)s32p->pktcnt;
    218   1.1       mrg 	p->bytecnt = (u_long)s32p->bytecnt;
    219   1.1       mrg 	p->wrong_if = (u_long)s32p->wrong_if;
    220   1.1       mrg }
    221   1.1       mrg 
    222  1.48       mrg static inline void
    223  1.48       mrg netbsd32_to_vnd_ioctl(struct netbsd32_vnd_ioctl *s32p, struct vnd_ioctl *p, u_long cmd)
    224  1.48       mrg {
    225  1.48       mrg 
    226  1.48       mrg 	p->vnd_file = (char *)NETBSD32PTR64(s32p->vnd_file);
    227  1.48       mrg 	p->vnd_flags = s32p->vnd_flags;
    228  1.48       mrg 	p->vnd_geom = s32p->vnd_geom;
    229  1.48       mrg 	p->vnd_osize = s32p->vnd_osize;
    230  1.48       mrg 	p->vnd_size = s32p->vnd_size;
    231  1.48       mrg }
    232  1.48       mrg 
    233  1.48       mrg static inline void
    234  1.48       mrg netbsd32_to_vnd_user(struct netbsd32_vnd_user *s32p, struct vnd_user *p, u_long cmd)
    235  1.48       mrg {
    236  1.48       mrg 
    237  1.48       mrg 	p->vnu_unit = s32p->vnu_unit;
    238  1.48       mrg 	p->vnu_dev = s32p->vnu_dev;
    239  1.48       mrg 	p->vnu_ino = s32p->vnu_ino;
    240  1.48       mrg }
    241  1.48       mrg 
    242  1.49       mrg static inline void
    243  1.49       mrg netbsd32_to_vnd_ioctl50(struct netbsd32_vnd_ioctl50 *s32p, struct vnd_ioctl50 *p, u_long cmd)
    244  1.49       mrg {
    245  1.49       mrg 
    246  1.49       mrg 	p->vnd_file = (char *)NETBSD32PTR64(s32p->vnd_file);
    247  1.49       mrg 	p->vnd_flags = s32p->vnd_flags;
    248  1.49       mrg 	p->vnd_geom = s32p->vnd_geom;
    249  1.49       mrg 	p->vnd_size = s32p->vnd_size;
    250  1.49       mrg }
    251  1.49       mrg 
    252  1.50       mrg static inline void
    253  1.51     njoly netbsd32_to_plistref(struct netbsd32_plistref *s32p, struct plistref *p, u_long cmd)
    254  1.51     njoly {
    255  1.51     njoly 
    256  1.51     njoly 	p->pref_plist = NETBSD32PTR64(s32p->pref_plist);
    257  1.51     njoly 	p->pref_len = s32p->pref_len;
    258  1.51     njoly }
    259  1.51     njoly 
    260  1.51     njoly static inline void
    261  1.50       mrg netbsd32_to_u_long(netbsd32_u_long *s32p, u_long *p, u_long cmd)
    262  1.50       mrg {
    263  1.50       mrg 
    264  1.50       mrg 	*p = (u_long)*s32p;
    265  1.50       mrg }
    266  1.50       mrg 
    267   1.1       mrg /*
    268  1.14       mrg  * handle ioctl conversions from 64-bit kernel -> netbsd32
    269   1.1       mrg  */
    270   1.1       mrg 
    271  1.25     perry static inline void
    272  1.36       dsl netbsd32_from_partinfo(struct partinfo *p, struct netbsd32_partinfo *s32p, u_long cmd)
    273   1.7       eeh {
    274   1.7       eeh 
    275  1.31       dsl 	NETBSD32PTR32(s32p->disklab, p->disklab);
    276  1.31       dsl 	NETBSD32PTR32(s32p->part, p->part);
    277   1.7       eeh }
    278   1.7       eeh 
    279  1.17      fvdl #if 0
    280  1.25     perry static inline void
    281  1.36       dsl netbsd32_from_format_op(struct format_op *p, struct netbsd32_format_op *s32p, u_long cmd)
    282   1.1       mrg {
    283   1.1       mrg 
    284   1.1       mrg /* filled in */
    285   1.1       mrg #if 0
    286   1.2       mrg 	s32p->df_buf = (netbsd32_charp)p->df_buf;
    287   1.1       mrg #endif
    288   1.1       mrg 	s32p->df_count = p->df_count;
    289   1.1       mrg 	s32p->df_startblk = p->df_startblk;
    290   1.1       mrg 	memcpy(s32p->df_reg, p->df_reg, sizeof(p->df_reg));
    291   1.1       mrg }
    292  1.17      fvdl #endif
    293   1.1       mrg 
    294  1.25     perry static inline void
    295  1.36       dsl netbsd32_from_ifreq(struct ifreq *p, struct netbsd32_ifreq *s32p, u_long cmd)
    296   1.1       mrg {
    297   1.1       mrg 
    298   1.1       mrg 	/*
    299   1.1       mrg 	 * XXX
    300   1.1       mrg 	 * struct ifreq says the same, but sometimes the ifr_data
    301   1.1       mrg 	 * union member needs to be converted to 64 bits... this
    302   1.1       mrg 	 * is very driver specific and so we ignore it for now..
    303   1.1       mrg 	 */
    304  1.46     njoly 	memcpy(s32p, p, sizeof *s32p);
    305  1.43  christos 	if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
    306  1.43  christos 		NETBSD32PTR32(s32p->ifr_data, p->ifr_data);
    307  1.43  christos }
    308  1.43  christos 
    309  1.43  christos static inline void
    310  1.43  christos netbsd32_from_oifreq(struct oifreq *p, struct netbsd32_oifreq *s32p, u_long cmd)
    311  1.43  christos {
    312  1.43  christos 
    313  1.43  christos 	/*
    314  1.43  christos 	 * XXX
    315  1.43  christos 	 * struct ifreq says the same, but sometimes the ifr_data
    316  1.43  christos 	 * union member needs to be converted to 64 bits... this
    317  1.43  christos 	 * is very driver specific and so we ignore it for now..
    318  1.43  christos 	 */
    319  1.46     njoly 	memcpy(s32p, p, sizeof *s32p);
    320  1.22       mrg 	if (cmd == SIOCGIFDATA || cmd == SIOCZIFDATA)
    321  1.31       dsl 		NETBSD32PTR32(s32p->ifr_data, p->ifr_data);
    322   1.1       mrg }
    323   1.1       mrg 
    324  1.25     perry static inline void
    325  1.36       dsl netbsd32_from_ifconf(struct ifconf *p, struct netbsd32_ifconf *s32p, u_long cmd)
    326   1.1       mrg {
    327   1.1       mrg 
    328   1.1       mrg 	s32p->ifc_len = p->ifc_len;
    329   1.1       mrg 	/* ifc_buf & ifc_req are the same size so this works */
    330  1.31       dsl 	NETBSD32PTR32(s32p->ifc_buf, p->ifc_buf);
    331   1.1       mrg }
    332   1.1       mrg 
    333  1.25     perry static inline void
    334  1.36       dsl netbsd32_from_ifmediareq(struct ifmediareq *p, struct netbsd32_ifmediareq *s32p, u_long cmd)
    335   1.1       mrg {
    336   1.1       mrg 
    337   1.1       mrg 	memcpy(s32p, p, sizeof *p);
    338   1.1       mrg /* filled in? */
    339   1.1       mrg #if 0
    340   1.2       mrg 	s32p->ifm_ulist = (netbsd32_intp_t)p->ifm_ulist;
    341   1.1       mrg #endif
    342   1.1       mrg }
    343   1.1       mrg 
    344  1.25     perry static inline void
    345  1.36       dsl netbsd32_from_ifdrv(struct ifdrv *p, struct netbsd32_ifdrv *s32p, u_long cmd)
    346   1.1       mrg {
    347   1.1       mrg 
    348   1.1       mrg 	memcpy(s32p, p, sizeof *p);
    349   1.1       mrg /* filled in? */
    350   1.1       mrg #if 0
    351   1.2       mrg 	s32p->ifm_data = (netbsd32_u_longp_t)p->ifm_data;
    352   1.1       mrg #endif
    353   1.1       mrg }
    354   1.1       mrg 
    355  1.25     perry static inline void
    356  1.36       dsl netbsd32_from_sioc_vif_req(struct sioc_vif_req *p, struct netbsd32_sioc_vif_req *s32p, u_long cmd)
    357   1.1       mrg {
    358   1.1       mrg 
    359   1.1       mrg 	s32p->vifi = p->vifi;
    360   1.2       mrg 	s32p->icount = (netbsd32_u_long)p->icount;
    361   1.2       mrg 	s32p->ocount = (netbsd32_u_long)p->ocount;
    362   1.2       mrg 	s32p->ibytes = (netbsd32_u_long)p->ibytes;
    363   1.2       mrg 	s32p->obytes = (netbsd32_u_long)p->obytes;
    364   1.1       mrg }
    365   1.1       mrg 
    366  1.25     perry static inline void
    367  1.36       dsl netbsd32_from_sioc_sg_req(struct sioc_sg_req *p, struct netbsd32_sioc_sg_req *s32p, u_long cmd)
    368   1.1       mrg {
    369   1.1       mrg 
    370   1.1       mrg 	s32p->src = p->src;
    371   1.1       mrg 	s32p->grp = p->grp;
    372   1.2       mrg 	s32p->pktcnt = (netbsd32_u_long)p->pktcnt;
    373   1.2       mrg 	s32p->bytecnt = (netbsd32_u_long)p->bytecnt;
    374   1.2       mrg 	s32p->wrong_if = (netbsd32_u_long)p->wrong_if;
    375   1.1       mrg }
    376   1.1       mrg 
    377  1.48       mrg static inline void
    378  1.48       mrg netbsd32_from_vnd_ioctl(struct vnd_ioctl *p, struct netbsd32_vnd_ioctl *s32p, u_long cmd)
    379  1.48       mrg {
    380  1.48       mrg 
    381  1.48       mrg 	s32p->vnd_flags = p->vnd_flags;
    382  1.48       mrg 	s32p->vnd_geom = p->vnd_geom;
    383  1.48       mrg 	s32p->vnd_osize = p->vnd_osize;
    384  1.48       mrg 	s32p->vnd_size = p->vnd_size;
    385  1.48       mrg }
    386  1.48       mrg 
    387  1.48       mrg static inline void
    388  1.48       mrg netbsd32_from_vnd_user(struct vnd_user *p, struct netbsd32_vnd_user *s32p, u_long cmd)
    389  1.48       mrg {
    390  1.48       mrg 
    391  1.48       mrg 	s32p->vnu_unit = p->vnu_unit;
    392  1.48       mrg 	s32p->vnu_dev = p->vnu_dev;
    393  1.48       mrg 	s32p->vnu_ino = p->vnu_ino;
    394  1.48       mrg }
    395  1.48       mrg 
    396  1.49       mrg static inline void
    397  1.49       mrg netbsd32_from_vnd_ioctl50(struct vnd_ioctl50 *p, struct netbsd32_vnd_ioctl50 *s32p, u_long cmd)
    398  1.49       mrg {
    399  1.49       mrg 
    400  1.49       mrg 	s32p->vnd_flags = p->vnd_flags;
    401  1.49       mrg 	s32p->vnd_geom = p->vnd_geom;
    402  1.49       mrg 	s32p->vnd_size = p->vnd_size;
    403  1.49       mrg }
    404  1.49       mrg 
    405  1.50       mrg static inline void
    406  1.51     njoly netbsd32_from_plistref(struct plistref *p, struct netbsd32_plistref *s32p, u_long cmd)
    407  1.51     njoly {
    408  1.51     njoly 
    409  1.51     njoly 	NETBSD32PTR32(s32p->pref_plist, p->pref_plist);
    410  1.51     njoly 	s32p->pref_len = p->pref_len;
    411  1.51     njoly }
    412  1.51     njoly 
    413  1.51     njoly static inline void
    414  1.50       mrg netbsd32_from_u_long(u_long *p, netbsd32_u_long *s32p, u_long cmd)
    415  1.50       mrg {
    416  1.50       mrg 
    417  1.50       mrg 	*s32p = (netbsd32_u_long)*p;
    418  1.50       mrg }
    419  1.50       mrg 
    420   1.1       mrg 
    421   1.1       mrg /*
    422   1.1       mrg  * main ioctl syscall.
    423   1.1       mrg  *
    424   1.1       mrg  * ok, here we are in the biggy.  we have to do fix ups depending
    425   1.1       mrg  * on the ioctl command before and afterwards.
    426   1.1       mrg  */
    427   1.1       mrg int
    428  1.37       dsl netbsd32_ioctl(struct lwp *l, const struct netbsd32_ioctl_args *uap, register_t *retval)
    429   1.1       mrg {
    430  1.37       dsl 	/* {
    431   1.1       mrg 		syscallarg(int) fd;
    432   1.2       mrg 		syscallarg(netbsd32_u_long) com;
    433   1.2       mrg 		syscallarg(netbsd32_voidp) data;
    434  1.37       dsl 	} */
    435  1.16   thorpej 	struct proc *p = l->l_proc;
    436   1.7       eeh 	struct file *fp;
    437   1.7       eeh 	struct filedesc *fdp;
    438   1.7       eeh 	u_long com;
    439   1.7       eeh 	int error = 0;
    440  1.52      matt 	size_t size;
    441  1.52      matt 	size_t alloc_size32, size32;
    442  1.30  christos 	void *data, *memp = NULL;
    443  1.30  christos 	void *data32, *memp32 = NULL;
    444  1.52      matt 	unsigned int fd;
    445  1.38        ad 	fdfile_t *ff;
    446   1.7       eeh 	int tmp;
    447   1.7       eeh #define STK_PARAMS	128
    448   1.7       eeh 	u_long stkbuf[STK_PARAMS/sizeof(u_long)];
    449   1.7       eeh 	u_long stkbuf32[STK_PARAMS/sizeof(u_long)];
    450   1.1       mrg 
    451   1.1       mrg 	/*
    452   1.1       mrg 	 * we need to translate some commands (_IOW) before calling sys_ioctl,
    453  1.23     perry 	 * some after (_IOR), and some both (_IOWR).
    454   1.1       mrg 	 */
    455   1.5       eeh #if 0
    456   1.5       eeh 	{
    457  1.52      matt 		const char * const dirs[8] = {
    458  1.52      matt 		    "NONE!", "VOID", "OUT", "VOID|OUT!", "IN", "VOID|IN!",
    459  1.52      matt 		    "INOUT", "VOID|IN|OUT!"
    460  1.52      matt 		};
    461  1.52      matt 
    462  1.52      matt 		printf("netbsd32_ioctl(%d, %x, %x): "
    463  1.52      matt 		    "%s group %c base %d len %d\n",
    464  1.52      matt 		    SCARG(uap, fd), SCARG(uap, com), SCARG(uap, data).i32,
    465  1.52      matt 		    dirs[((SCARG(uap, com) & IOC_DIRMASK)>>29)],
    466  1.52      matt 		    IOCGROUP(SCARG(uap, com)), IOCBASECMD(SCARG(uap, com)),
    467  1.52      matt 		    IOCPARM_LEN(SCARG(uap, com)));
    468   1.5       eeh 	}
    469   1.5       eeh #endif
    470   1.1       mrg 
    471  1.52      matt 	memp = NULL;
    472  1.52      matt 	memp32 = NULL;
    473  1.52      matt 	alloc_size32 = 0;
    474  1.52      matt 	size32 = 0;
    475  1.52      matt 	size = 0;
    476  1.52      matt 
    477   1.7       eeh 	fdp = p->p_fd;
    478  1.38        ad 	fd = SCARG(uap, fd);
    479  1.38        ad 	if ((fp = fd_getfile(fd)) == NULL)
    480   1.7       eeh 		return (EBADF);
    481   1.7       eeh 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
    482   1.7       eeh 		error = EBADF;
    483   1.7       eeh 		goto out;
    484   1.7       eeh 	}
    485   1.7       eeh 
    486  1.42        ad 	ff = fdp->fd_dt->dt_ff[SCARG(uap, fd)];
    487   1.7       eeh 	switch (com = SCARG(uap, com)) {
    488  1.41      matt 	case FIOCLEX:
    489  1.41      matt 		ff->ff_exclose = true;
    490  1.41      matt 		fdp->fd_exclose = true;
    491   1.7       eeh 		goto out;
    492   1.7       eeh 
    493  1.41      matt 	case FIONCLEX:
    494  1.41      matt 		ff->ff_exclose = false;
    495   1.7       eeh 		goto out;
    496   1.7       eeh 	}
    497   1.7       eeh 
    498   1.7       eeh 	/*
    499   1.7       eeh 	 * Interpret high order word to find amount of data to be
    500   1.7       eeh 	 * copied to/from the user's address space.
    501   1.7       eeh 	 */
    502   1.7       eeh 	size32 = IOCPARM_LEN(com);
    503  1.52      matt 	alloc_size32 = size32;
    504  1.52      matt 
    505  1.52      matt 	/*
    506  1.52      matt 	 * The disklabel is now padded to a multiple of 8 bytes however the old
    507  1.52      matt 	 * disklabel on 32bit platforms wasn't.  This leaves a difference in
    508  1.52      matt 	 * size of 4 bytes between the two but are otherwise identical.
    509  1.52      matt 	 * To deal with this, we allocate enough space for the new disklabel
    510  1.52      matt 	 * but only copyin/out the smaller amount.
    511  1.52      matt 	 */
    512  1.52      matt 	if (IOCGROUP(com) == 'd') {
    513  1.52      matt 		u_long ncom = com ^ (DIOCGDINFO ^ DIOCGDINFO32);
    514  1.52      matt 		switch (ncom) {
    515  1.52      matt 		case DIOCGDINFO:
    516  1.52      matt 		case DIOCWDINFO:
    517  1.52      matt 		case DIOCSDINFO:
    518  1.52      matt 		case DIOCGDEFLABEL:
    519  1.52      matt 			com = ncom;
    520  1.52      matt 			if (IOCPARM_LEN(DIOCGDINFO32) < IOCPARM_LEN(DIOCGDINFO))
    521  1.52      matt 				alloc_size32 = IOCPARM_LEN(DIOCGDINFO);
    522  1.52      matt 			break;
    523  1.52      matt 		}
    524  1.52      matt 	}
    525  1.52      matt 	if (alloc_size32 > IOCPARM_MAX) {
    526   1.7       eeh 		error = ENOTTY;
    527   1.7       eeh 		goto out;
    528   1.7       eeh 	}
    529  1.52      matt 	if (alloc_size32 > sizeof(stkbuf)) {
    530  1.52      matt 		memp32 = kmem_alloc(alloc_size32, KM_SLEEP);
    531   1.7       eeh 		data32 = memp32;
    532   1.7       eeh 	} else
    533  1.29  christos 		data32 = (void *)stkbuf32;
    534  1.52      matt 	if ((com >> IOCPARM_SHIFT) == 0)  {
    535  1.52      matt 		/* UNIX-style ioctl. */
    536  1.52      matt 		data32 = SCARG_P32(uap, data);
    537  1.52      matt 	} else {
    538  1.52      matt 		if (com&IOC_IN) {
    539  1.52      matt 			if (size32) {
    540  1.52      matt 				error = copyin(SCARG_P32(uap, data), data32,
    541  1.52      matt 				    size32);
    542  1.52      matt 				if (error) {
    543  1.52      matt 					goto out;
    544  1.52      matt 				}
    545  1.52      matt 				/*
    546  1.52      matt 				 * The data between size and alloc_size has
    547  1.52      matt 				 * not been overwritten.  It shouldn't matter
    548  1.52      matt 				 * but let's clear that anyway.
    549  1.52      matt 				 */
    550  1.52      matt 				if (__predict_false(size32 < alloc_size32)) {
    551  1.52      matt 					memset((char *)data32+size32, 0,
    552  1.52      matt 					    alloc_size32 - size32);
    553  1.52      matt 				}
    554  1.52      matt 				ktrgenio(fd, UIO_WRITE, SCARG_P32(uap, data),
    555  1.52      matt 				    size32, 0);
    556  1.52      matt 			} else
    557  1.52      matt 				*(void **)data32 = SCARG_P32(uap, data);
    558  1.52      matt 		} else if ((com&IOC_OUT) && size32) {
    559  1.52      matt 			/*
    560  1.52      matt 			 * Zero the buffer so the user always
    561  1.52      matt 			 * gets back something deterministic.
    562  1.52      matt 			 */
    563  1.52      matt 			memset(data32, 0, alloc_size32);
    564  1.52      matt 		} else if (com&IOC_VOID) {
    565  1.32       dsl 			*(void **)data32 = SCARG_P32(uap, data);
    566  1.52      matt 		}
    567  1.52      matt 	}
    568   1.7       eeh 
    569   1.1       mrg 	/*
    570   1.1       mrg 	 * convert various structures, pointers, and other objects that
    571   1.1       mrg 	 * change size from 32 bit -> 64 bit, for all ioctl commands.
    572   1.1       mrg 	 */
    573   1.1       mrg 	switch (SCARG(uap, com)) {
    574   1.7       eeh 	case FIONBIO:
    575  1.38        ad 		mutex_enter(&fp->f_lock);
    576   1.8       eeh 		if ((tmp = *(int *)data32) != 0)
    577   1.7       eeh 			fp->f_flag |= FNONBLOCK;
    578   1.7       eeh 		else
    579   1.7       eeh 			fp->f_flag &= ~FNONBLOCK;
    580  1.38        ad 		mutex_exit(&fp->f_lock);
    581  1.38        ad 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (void *)&tmp);
    582   1.7       eeh 		break;
    583   1.7       eeh 
    584   1.7       eeh 	case FIOASYNC:
    585  1.38        ad 		mutex_enter(&fp->f_lock);
    586   1.8       eeh 		if ((tmp = *(int *)data32) != 0)
    587   1.7       eeh 			fp->f_flag |= FASYNC;
    588   1.7       eeh 		else
    589   1.7       eeh 			fp->f_flag &= ~FASYNC;
    590  1.38        ad 		mutex_exit(&fp->f_lock);
    591  1.38        ad 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (void *)&tmp);
    592   1.1       mrg 		break;
    593   1.1       mrg 
    594  1.50       mrg 	case AUDIO_WSEEK32:
    595  1.50       mrg 		IOCTL_CONV_TO(AUDIO_WSEEK, u_long);
    596  1.50       mrg 
    597   1.7       eeh 	case DIOCGPART32:
    598   1.7       eeh 		IOCTL_STRUCT_CONV_TO(DIOCGPART, partinfo);
    599  1.17      fvdl #if 0	/* not implemented by anything */
    600   1.7       eeh 	case DIOCRFORMAT32:
    601   1.7       eeh 		IOCTL_STRUCT_CONV_TO(DIOCRFORMAT, format_op);
    602   1.7       eeh 	case DIOCWFORMAT32:
    603   1.7       eeh 		IOCTL_STRUCT_CONV_TO(DIOCWFORMAT, format_op);
    604  1.17      fvdl #endif
    605   1.1       mrg 
    606   1.1       mrg /*
    607   1.1       mrg  * only a few ifreq syscalls need conversion and those are
    608   1.1       mrg  * all driver specific... XXX
    609   1.1       mrg  */
    610   1.1       mrg #if 0
    611   1.7       eeh 	case SIOCGADDRROM3232:
    612   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCGADDRROM32, ifreq);
    613   1.7       eeh 	case SIOCGCHIPID32:
    614   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCGCHIPID, ifreq);
    615   1.7       eeh 	case SIOCSIFADDR32:
    616   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCSIFADDR, ifreq);
    617   1.7       eeh 	case OSIOCGIFADDR32:
    618   1.7       eeh 		IOCTL_STRUCT_CONV_TO(OSIOCGIFADDR, ifreq);
    619   1.7       eeh 	case SIOCGIFADDR32:
    620   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCGIFADDR, ifreq);
    621   1.7       eeh 	case SIOCSIFDSTADDR32:
    622   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCSIFDSTADDR, ifreq);
    623   1.7       eeh 	case OSIOCGIFDSTADDR32:
    624   1.7       eeh 		IOCTL_STRUCT_CONV_TO(OSIOCGIFDSTADDR, ifreq);
    625   1.7       eeh 	case SIOCGIFDSTADDR32:
    626   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCGIFDSTADDR, ifreq);
    627   1.7       eeh 	case OSIOCGIFBRDADDR32:
    628   1.7       eeh 		IOCTL_STRUCT_CONV_TO(OSIOCGIFBRDADDR, ifreq);
    629   1.7       eeh 	case SIOCGIFBRDADDR32:
    630   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCGIFBRDADDR, ifreq);
    631   1.7       eeh 	case SIOCSIFBRDADDR32:
    632   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCSIFBRDADDR, ifreq);
    633   1.7       eeh 	case OSIOCGIFNETMASK32:
    634   1.7       eeh 		IOCTL_STRUCT_CONV_TO(OSIOCGIFNETMASK, ifreq);
    635   1.7       eeh 	case SIOCGIFNETMASK32:
    636   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCGIFNETMASK, ifreq);
    637   1.7       eeh 	case SIOCSIFNETMASK32:
    638   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCSIFNETMASK, ifreq);
    639   1.7       eeh 	case SIOCGIFMETRIC32:
    640   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCGIFMETRIC, ifreq);
    641   1.7       eeh 	case SIOCSIFMETRIC32:
    642   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCSIFMETRIC, ifreq);
    643   1.7       eeh 	case SIOCDIFADDR32:
    644   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCDIFADDR, ifreq);
    645   1.7       eeh 	case SIOCADDMULTI32:
    646   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCADDMULTI, ifreq);
    647   1.7       eeh 	case SIOCDELMULTI32:
    648   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCDELMULTI, ifreq);
    649   1.7       eeh 	case SIOCSIFMEDIA32:
    650   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCSIFMEDIA, ifreq);
    651   1.7       eeh 	case SIOCSIFMTU32:
    652   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCSIFMTU, ifreq);
    653   1.7       eeh 	case SIOCGIFMTU32:
    654   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCGIFMTU, ifreq);
    655  1.18  christos 	case BIOCGETIF32:
    656  1.18  christos 		IOCTL_STRUCT_CONV_TO(BIOCGETIF, ifreq);
    657   1.7       eeh 	case BIOCSETIF32:
    658   1.7       eeh 		IOCTL_STRUCT_CONV_TO(BIOCSETIF, ifreq);
    659   1.7       eeh 	case SIOCPHASE132:
    660   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCPHASE1, ifreq);
    661   1.7       eeh 	case SIOCPHASE232:
    662   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCPHASE2, ifreq);
    663   1.1       mrg #endif
    664   1.1       mrg 
    665  1.40     njoly 	case OOSIOCGIFCONF32:
    666  1.40     njoly 		IOCTL_STRUCT_CONV_TO(OOSIOCGIFCONF, ifconf);
    667   1.7       eeh 	case OSIOCGIFCONF32:
    668   1.7       eeh 		IOCTL_STRUCT_CONV_TO(OSIOCGIFCONF, ifconf);
    669   1.7       eeh 	case SIOCGIFCONF32:
    670   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCGIFCONF, ifconf);
    671   1.7       eeh 
    672  1.22       mrg 	case SIOCGIFFLAGS32:
    673  1.22       mrg 		IOCTL_STRUCT_CONV_TO(SIOCGIFFLAGS, ifreq);
    674  1.22       mrg 	case SIOCSIFFLAGS32:
    675  1.22       mrg 		IOCTL_STRUCT_CONV_TO(SIOCSIFFLAGS, ifreq);
    676  1.22       mrg 
    677  1.43  christos 	case OSIOCGIFFLAGS32:
    678  1.43  christos 		IOCTL_STRUCT_CONV_TO(OSIOCGIFFLAGS, oifreq);
    679  1.43  christos 	case OSIOCSIFFLAGS32:
    680  1.43  christos 		IOCTL_STRUCT_CONV_TO(OSIOCSIFFLAGS, oifreq);
    681  1.43  christos 
    682   1.7       eeh 	case SIOCGIFMEDIA32:
    683   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCGIFMEDIA, ifmediareq);
    684   1.1       mrg 
    685   1.7       eeh 	case SIOCSDRVSPEC32:
    686   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCSDRVSPEC, ifdrv);
    687   1.1       mrg 
    688   1.7       eeh 	case SIOCGETVIFCNT32:
    689   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCGETVIFCNT, sioc_vif_req);
    690   1.1       mrg 
    691   1.7       eeh 	case SIOCGETSGCNT32:
    692   1.7       eeh 		IOCTL_STRUCT_CONV_TO(SIOCGETSGCNT, sioc_sg_req);
    693   1.1       mrg 
    694  1.48       mrg 	case VNDIOCSET32:
    695  1.48       mrg 		IOCTL_STRUCT_CONV_TO(VNDIOCSET, vnd_ioctl);
    696  1.48       mrg 
    697  1.48       mrg 	case VNDIOCCLR32:
    698  1.48       mrg 		IOCTL_STRUCT_CONV_TO(VNDIOCCLR, vnd_ioctl);
    699  1.48       mrg 
    700  1.48       mrg 	case VNDIOCGET32:
    701  1.48       mrg 		IOCTL_STRUCT_CONV_TO(VNDIOCGET, vnd_user);
    702  1.48       mrg 
    703  1.49       mrg 	case VNDIOCSET5032:
    704  1.49       mrg 		IOCTL_STRUCT_CONV_TO(VNDIOCSET50, vnd_ioctl50);
    705  1.49       mrg 
    706  1.49       mrg 	case VNDIOCCLR5032:
    707  1.49       mrg 		IOCTL_STRUCT_CONV_TO(VNDIOCCLR50, vnd_ioctl50);
    708  1.49       mrg 
    709  1.51     njoly 	case ENVSYS_GETDICTIONARY32:
    710  1.51     njoly 		IOCTL_STRUCT_CONV_TO(ENVSYS_GETDICTIONARY, plistref);
    711  1.51     njoly 	case ENVSYS_SETDICTIONARY32:
    712  1.51     njoly 		IOCTL_STRUCT_CONV_TO(ENVSYS_SETDICTIONARY, plistref);
    713  1.51     njoly 	case ENVSYS_REMOVEPROPS32:
    714  1.51     njoly 		IOCTL_STRUCT_CONV_TO(ENVSYS_REMOVEPROPS, plistref);
    715  1.51     njoly 
    716   1.7       eeh 	default:
    717  1.14       mrg #ifdef NETBSD32_MD_IOCTL
    718  1.24  christos 		error = netbsd32_md_ioctl(fp, com, data32, l);
    719  1.14       mrg #else
    720  1.38        ad 		error = (*fp->f_ops->fo_ioctl)(fp, com, data32);
    721  1.14       mrg #endif
    722   1.1       mrg 		break;
    723   1.1       mrg 	}
    724   1.1       mrg 
    725  1.26      fvdl 	if (error == EPASSTHROUGH)
    726  1.26      fvdl 		error = ENOTTY;
    727  1.26      fvdl 
    728   1.1       mrg 	/*
    729   1.7       eeh 	 * Copy any data to user, size was
    730   1.7       eeh 	 * already set and checked above.
    731   1.1       mrg 	 */
    732  1.33     njoly 	if (error == 0 && (com&IOC_OUT) && size32) {
    733  1.32       dsl 		error = copyout(data32, SCARG_P32(uap, data), size32);
    734  1.38        ad 		ktrgenio(fd, UIO_READ, SCARG_P32(uap, data),
    735  1.35        ad 		    size32, error);
    736  1.33     njoly 	}
    737   1.1       mrg 
    738  1.52      matt  out:
    739  1.47     rmind 	/* If we allocated data, free it here. */
    740   1.7       eeh 	if (memp32)
    741  1.52      matt 		kmem_free(memp32, alloc_size32);
    742   1.7       eeh 	if (memp)
    743  1.53      matt 		kmem_free(memp, size);
    744  1.38        ad 	fd_putfile(fd);
    745   1.7       eeh 	return (error);
    746   1.1       mrg }
    747