1 /*- 2 * Copyright (c) 2008 David E. O'Brien 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the author nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include "opt_compat.h" 34 35 #include <sys/param.h> 36 #include <sys/cdio.h> 37 #include <sys/fcntl.h> 38 #include <sys/filio.h> 39 #include <sys/file.h> 40 #include <sys/ioccom.h> 41 #include <sys/malloc.h> 42 #include <sys/mdioctl.h> 43 #include <sys/memrange.h> 44 #include <sys/pciio.h> 45 #include <sys/proc.h> 46 #include <sys/syscall.h> 47 #include <sys/syscallsubr.h> 48 #include <sys/sysctl.h> 49 #include <sys/sysent.h> 50 #include <sys/sysproto.h> 51 #include <sys/systm.h> 52 53 #include <compat/freebsd32/freebsd32.h> 54 #include <compat/freebsd32/freebsd32_ioctl.h> 55 #include <compat/freebsd32/freebsd32_proto.h> 56 57 /* Cannot get exact size in 64-bit due to alignment issue of entire struct. */ 58 CTASSERT((sizeof(struct md_ioctl32)+4) == 436); 59 CTASSERT(sizeof(struct ioc_read_toc_entry32) == 8); 60 CTASSERT(sizeof(struct ioc_toc_header32) == 4); 61 CTASSERT(sizeof(struct mem_range_op32) == 12); 62 CTASSERT(sizeof(struct pci_conf_io32) == 36); 63 CTASSERT(sizeof(struct pci_match_conf32) == 44); 64 CTASSERT(sizeof(struct pci_conf32) == 44); 65 66 67 static int 68 freebsd32_ioctl_md(struct thread *td, struct freebsd32_ioctl_args *uap, 69 struct file *fp) 70 { 71 struct md_ioctl mdv; 72 struct md_ioctl32 md32; 73 u_long com = 0; 74 int i, error; 75 76 if (uap->com & IOC_IN) { 77 if ((error = copyin(uap->data, &md32, sizeof(md32)))) { 78 return (error); 79 } 80 CP(md32, mdv, md_version); 81 CP(md32, mdv, md_unit); 82 CP(md32, mdv, md_type); 83 PTRIN_CP(md32, mdv, md_file); 84 CP(md32, mdv, md_mediasize); 85 CP(md32, mdv, md_sectorsize); 86 CP(md32, mdv, md_options); 87 CP(md32, mdv, md_base); 88 CP(md32, mdv, md_fwheads); 89 CP(md32, mdv, md_fwsectors); 90 } else if (uap->com & IOC_OUT) { 91 /* 92 * Zero the buffer so the user always 93 * gets back something deterministic. 94 */ 95 bzero(&mdv, sizeof mdv); 96 } 97 98 switch (uap->com) { 99 case MDIOCATTACH_32: 100 com = MDIOCATTACH; 101 break; 102 case MDIOCDETACH_32: 103 com = MDIOCDETACH; 104 break; 105 case MDIOCQUERY_32: 106 com = MDIOCQUERY; 107 break; 108 case MDIOCLIST_32: 109 com = MDIOCLIST; 110 break; 111 default: 112 panic("%s: unknown MDIOC %#x", __func__, uap->com); 113 } 114 error = fo_ioctl(fp, com, (caddr_t)&mdv, td->td_ucred, td); 115 if (error == 0 && (com & IOC_OUT)) { 116 CP(mdv, md32, md_version); 117 CP(mdv, md32, md_unit); 118 CP(mdv, md32, md_type); 119 PTROUT_CP(mdv, md32, md_file); 120 CP(mdv, md32, md_mediasize); 121 CP(mdv, md32, md_sectorsize); 122 CP(mdv, md32, md_options); 123 CP(mdv, md32, md_base); 124 CP(mdv, md32, md_fwheads); 125 CP(mdv, md32, md_fwsectors); 126 if (com == MDIOCLIST) { 127 /* 128 * Use MDNPAD, and not MDNPAD32. Padding is 129 * allocated and used by compat32 ABI. 130 */ 131 for (i = 0; i < MDNPAD; i++) 132 CP(mdv, md32, md_pad[i]); 133 } 134 error = copyout(&md32, uap->data, sizeof(md32)); 135 } 136 return error; 137 } 138 139 140 static int 141 freebsd32_ioctl_ioc_toc_header(struct thread *td, 142 struct freebsd32_ioctl_args *uap, struct file *fp) 143 { 144 struct ioc_toc_header toch; 145 struct ioc_toc_header32 toch32; 146 int error; 147 148 if ((error = copyin(uap->data, &toch32, sizeof(toch32)))) 149 return (error); 150 CP(toch32, toch, len); 151 CP(toch32, toch, starting_track); 152 CP(toch32, toch, ending_track); 153 error = fo_ioctl(fp, CDIOREADTOCHEADER, (caddr_t)&toch, 154 td->td_ucred, td); 155 return (error); 156 } 157 158 159 static int 160 freebsd32_ioctl_ioc_read_toc(struct thread *td, 161 struct freebsd32_ioctl_args *uap, struct file *fp) 162 { 163 struct ioc_read_toc_entry toce; 164 struct ioc_read_toc_entry32 toce32; 165 int error; 166 167 if ((error = copyin(uap->data, &toce32, sizeof(toce32)))) 168 return (error); 169 CP(toce32, toce, address_format); 170 CP(toce32, toce, starting_track); 171 CP(toce32, toce, data_len); 172 PTRIN_CP(toce32, toce, data); 173 174 if ((error = fo_ioctl(fp, CDIOREADTOCENTRYS, (caddr_t)&toce, 175 td->td_ucred, td))) { 176 CP(toce, toce32, address_format); 177 CP(toce, toce32, starting_track); 178 CP(toce, toce32, data_len); 179 PTROUT_CP(toce, toce32, data); 180 error = copyout(&toce32, uap->data, sizeof(toce32)); 181 } 182 return error; 183 } 184 185 static int 186 freebsd32_ioctl_fiodgname(struct thread *td, 187 struct freebsd32_ioctl_args *uap, struct file *fp) 188 { 189 struct fiodgname_arg fgn; 190 struct fiodgname_arg32 fgn32; 191 int error; 192 193 if ((error = copyin(uap->data, &fgn32, sizeof fgn32)) != 0) 194 return (error); 195 CP(fgn32, fgn, len); 196 PTRIN_CP(fgn32, fgn, buf); 197 error = fo_ioctl(fp, FIODGNAME, (caddr_t)&fgn, td->td_ucred, td); 198 return (error); 199 } 200 201 static int 202 freebsd32_ioctl_memrange(struct thread *td, 203 struct freebsd32_ioctl_args *uap, struct file *fp) 204 { 205 struct mem_range_op mro; 206 struct mem_range_op32 mro32; 207 int error; 208 u_long com; 209 210 if ((error = copyin(uap->data, &mro32, sizeof(mro32))) != 0) 211 return (error); 212 213 PTRIN_CP(mro32, mro, mo_desc); 214 CP(mro32, mro, mo_arg[0]); 215 CP(mro32, mro, mo_arg[1]); 216 217 com = 0; 218 switch (uap->com) { 219 case MEMRANGE_GET32: 220 com = MEMRANGE_GET; 221 break; 222 223 case MEMRANGE_SET32: 224 com = MEMRANGE_SET; 225 break; 226 227 default: 228 panic("%s: unknown MEMRANGE %#x", __func__, uap->com); 229 } 230 231 if ((error = fo_ioctl(fp, com, (caddr_t)&mro, td->td_ucred, td)) != 0) 232 return (error); 233 234 if ( (com & IOC_OUT) ) { 235 CP(mro, mro32, mo_arg[0]); 236 CP(mro, mro32, mo_arg[1]); 237 238 error = copyout(&mro32, uap->data, sizeof(mro32)); 239 } 240 241 return (error); 242 } 243 244 static int 245 freebsd32_ioctl_pciocgetconf(struct thread *td, 246 struct freebsd32_ioctl_args *uap, struct file *fp) 247 { 248 struct pci_conf_io pci; 249 struct pci_conf_io32 pci32; 250 struct pci_match_conf32 pmc32; 251 struct pci_match_conf32 *pmc32p; 252 struct pci_match_conf pmc; 253 struct pci_match_conf *pmcp; 254 struct pci_conf32 pc32; 255 struct pci_conf32 *pc32p; 256 struct pci_conf pc; 257 struct pci_conf *pcp; 258 u_int32_t i; 259 u_int32_t npat_to_convert; 260 u_int32_t nmatch_to_convert; 261 vm_offset_t addr; 262 int error; 263 264 if ((error = copyin(uap->data, &pci32, sizeof(pci32))) != 0) 265 return (error); 266 267 CP(pci32, pci, num_patterns); 268 CP(pci32, pci, offset); 269 CP(pci32, pci, generation); 270 271 npat_to_convert = pci32.pat_buf_len / sizeof(struct pci_match_conf32); 272 pci.pat_buf_len = npat_to_convert * sizeof(struct pci_match_conf); 273 pci.patterns = NULL; 274 nmatch_to_convert = pci32.match_buf_len / sizeof(struct pci_conf32); 275 pci.match_buf_len = nmatch_to_convert * sizeof(struct pci_conf); 276 pci.matches = NULL; 277 278 if ((error = copyout_map(td, &addr, pci.pat_buf_len)) != 0) 279 goto cleanup; 280 pci.patterns = (struct pci_match_conf *)addr; 281 if ((error = copyout_map(td, &addr, pci.match_buf_len)) != 0) 282 goto cleanup; 283 pci.matches = (struct pci_conf *)addr; 284 285 npat_to_convert = min(npat_to_convert, pci.num_patterns); 286 287 for (i = 0, pmc32p = (struct pci_match_conf32 *)PTRIN(pci32.patterns), 288 pmcp = pci.patterns; 289 i < npat_to_convert; i++, pmc32p++, pmcp++) { 290 if ((error = copyin(pmc32p, &pmc32, sizeof(pmc32))) != 0) 291 goto cleanup; 292 CP(pmc32,pmc,pc_sel); 293 strlcpy(pmc.pd_name, pmc32.pd_name, sizeof(pmc.pd_name)); 294 CP(pmc32,pmc,pd_unit); 295 CP(pmc32,pmc,pc_vendor); 296 CP(pmc32,pmc,pc_device); 297 CP(pmc32,pmc,pc_class); 298 CP(pmc32,pmc,flags); 299 if ((error = copyout(&pmc, pmcp, sizeof(pmc))) != 0) 300 goto cleanup; 301 } 302 303 if ((error = fo_ioctl(fp, PCIOCGETCONF, (caddr_t)&pci, 304 td->td_ucred, td)) != 0) 305 goto cleanup; 306 307 nmatch_to_convert = min(nmatch_to_convert, pci.num_matches); 308 309 for (i = 0, pcp = pci.matches, 310 pc32p = (struct pci_conf32 *)PTRIN(pci32.matches); 311 i < nmatch_to_convert; i++, pcp++, pc32p++) { 312 if ((error = copyin(pcp, &pc, sizeof(pc))) != 0) 313 goto cleanup; 314 CP(pc,pc32,pc_sel); 315 CP(pc,pc32,pc_hdr); 316 CP(pc,pc32,pc_subvendor); 317 CP(pc,pc32,pc_subdevice); 318 CP(pc,pc32,pc_vendor); 319 CP(pc,pc32,pc_device); 320 CP(pc,pc32,pc_class); 321 CP(pc,pc32,pc_subclass); 322 CP(pc,pc32,pc_progif); 323 CP(pc,pc32,pc_revid); 324 strlcpy(pc32.pd_name, pc.pd_name, sizeof(pc32.pd_name)); 325 CP(pc,pc32,pd_unit); 326 if ((error = copyout(&pc32, pc32p, sizeof(pc32))) != 0) 327 goto cleanup; 328 } 329 330 CP(pci, pci32, num_matches); 331 CP(pci, pci32, offset); 332 CP(pci, pci32, generation); 333 CP(pci, pci32, status); 334 335 error = copyout(&pci32, uap->data, sizeof(pci32)); 336 337 cleanup: 338 if (pci.patterns) 339 copyout_unmap(td, (vm_offset_t)pci.patterns, pci.pat_buf_len); 340 if (pci.matches) 341 copyout_unmap(td, (vm_offset_t)pci.matches, pci.match_buf_len); 342 343 return (error); 344 } 345 346 int 347 freebsd32_ioctl(struct thread *td, struct freebsd32_ioctl_args *uap) 348 { 349 struct ioctl_args ap /*{ 350 int fd; 351 u_long com; 352 caddr_t data; 353 }*/ ; 354 struct file *fp; 355 int error; 356 357 if ((error = fget(td, uap->fd, &fp)) != 0) 358 return (error); 359 if ((fp->f_flag & (FREAD | FWRITE)) == 0) { 360 fdrop(fp, td); 361 return (EBADF); 362 } 363 364 switch (uap->com) { 365 case MDIOCATTACH_32: /* FALLTHROUGH */ 366 case MDIOCDETACH_32: /* FALLTHROUGH */ 367 case MDIOCQUERY_32: /* FALLTHROUGH */ 368 case MDIOCLIST_32: 369 error = freebsd32_ioctl_md(td, uap, fp); 370 break; 371 372 case CDIOREADTOCENTRYS_32: 373 error = freebsd32_ioctl_ioc_read_toc(td, uap, fp); 374 break; 375 376 case CDIOREADTOCHEADER_32: 377 error = freebsd32_ioctl_ioc_toc_header(td, uap, fp); 378 break; 379 380 case FIODGNAME_32: 381 error = freebsd32_ioctl_fiodgname(td, uap, fp); 382 break; 383 384 case MEMRANGE_GET32: /* FALLTHROUGH */ 385 case MEMRANGE_SET32: 386 error = freebsd32_ioctl_memrange(td, uap, fp); 387 break; 388 389 case PCIOCGETCONF_32: 390 error = freebsd32_ioctl_pciocgetconf(td, uap, fp); 391 break; 392 393 default: 394 fdrop(fp, td); 395 ap.fd = uap->fd; 396 ap.com = uap->com; 397 PTRIN_CP(*uap, ap, data); 398 return ioctl(td, &ap); 399 } 400 401 fdrop(fp, td); 402 return error; 403 } 404