2 * Copyright (c) 2008 David E. O'Brien
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_compat.h"
35 #include <sys/param.h>
36 #include <sys/capsicum.h>
38 #include <sys/fcntl.h>
39 #include <sys/filio.h>
41 #include <sys/ioccom.h>
42 #include <sys/malloc.h>
43 #include <sys/mdioctl.h>
44 #include <sys/memrange.h>
45 #include <sys/pciio.h>
47 #include <sys/syscall.h>
48 #include <sys/syscallsubr.h>
49 #include <sys/sysctl.h>
50 #include <sys/sysent.h>
51 #include <sys/sysproto.h>
52 #include <sys/systm.h>
54 #include <compat/freebsd32/freebsd32.h>
55 #include <compat/freebsd32/freebsd32_ioctl.h>
56 #include <compat/freebsd32/freebsd32_proto.h>
58 /* Cannot get exact size in 64-bit due to alignment issue of entire struct. */
59 CTASSERT((sizeof(struct md_ioctl32)+4) == 436);
60 CTASSERT(sizeof(struct ioc_read_toc_entry32) == 8);
61 CTASSERT(sizeof(struct ioc_toc_header32) == 4);
62 CTASSERT(sizeof(struct mem_range_op32) == 12);
63 CTASSERT(sizeof(struct pci_conf_io32) == 36);
64 CTASSERT(sizeof(struct pci_match_conf32) == 44);
65 CTASSERT(sizeof(struct pci_conf32) == 44);
69 freebsd32_ioctl_md(struct thread *td, struct freebsd32_ioctl_args *uap,
73 struct md_ioctl32 md32;
77 if (uap->com & IOC_IN) {
78 if ((error = copyin(uap->data, &md32, sizeof(md32)))) {
81 CP(md32, mdv, md_version);
82 CP(md32, mdv, md_unit);
83 CP(md32, mdv, md_type);
84 PTRIN_CP(md32, mdv, md_file);
85 CP(md32, mdv, md_mediasize);
86 CP(md32, mdv, md_sectorsize);
87 CP(md32, mdv, md_options);
88 CP(md32, mdv, md_base);
89 CP(md32, mdv, md_fwheads);
90 CP(md32, mdv, md_fwsectors);
91 } else if (uap->com & IOC_OUT) {
93 * Zero the buffer so the user always
94 * gets back something deterministic.
96 bzero(&mdv, sizeof mdv);
113 panic("%s: unknown MDIOC %#x", __func__, uap->com);
115 error = fo_ioctl(fp, com, (caddr_t)&mdv, td->td_ucred, td);
116 if (error == 0 && (com & IOC_OUT)) {
117 CP(mdv, md32, md_version);
118 CP(mdv, md32, md_unit);
119 CP(mdv, md32, md_type);
120 PTROUT_CP(mdv, md32, md_file);
121 CP(mdv, md32, md_mediasize);
122 CP(mdv, md32, md_sectorsize);
123 CP(mdv, md32, md_options);
124 CP(mdv, md32, md_base);
125 CP(mdv, md32, md_fwheads);
126 CP(mdv, md32, md_fwsectors);
127 if (com == MDIOCLIST) {
129 * Use MDNPAD, and not MDNPAD32. Padding is
130 * allocated and used by compat32 ABI.
132 for (i = 0; i < MDNPAD; i++)
133 CP(mdv, md32, md_pad[i]);
135 error = copyout(&md32, uap->data, sizeof(md32));
142 freebsd32_ioctl_ioc_toc_header(struct thread *td,
143 struct freebsd32_ioctl_args *uap, struct file *fp)
145 struct ioc_toc_header toch;
146 struct ioc_toc_header32 toch32;
149 if ((error = copyin(uap->data, &toch32, sizeof(toch32))))
151 CP(toch32, toch, len);
152 CP(toch32, toch, starting_track);
153 CP(toch32, toch, ending_track);
154 error = fo_ioctl(fp, CDIOREADTOCHEADER, (caddr_t)&toch,
161 freebsd32_ioctl_ioc_read_toc(struct thread *td,
162 struct freebsd32_ioctl_args *uap, struct file *fp)
164 struct ioc_read_toc_entry toce;
165 struct ioc_read_toc_entry32 toce32;
168 if ((error = copyin(uap->data, &toce32, sizeof(toce32))))
170 CP(toce32, toce, address_format);
171 CP(toce32, toce, starting_track);
172 CP(toce32, toce, data_len);
173 PTRIN_CP(toce32, toce, data);
175 if ((error = fo_ioctl(fp, CDIOREADTOCENTRYS, (caddr_t)&toce,
176 td->td_ucred, td))) {
177 CP(toce, toce32, address_format);
178 CP(toce, toce32, starting_track);
179 CP(toce, toce32, data_len);
180 PTROUT_CP(toce, toce32, data);
181 error = copyout(&toce32, uap->data, sizeof(toce32));
187 freebsd32_ioctl_fiodgname(struct thread *td,
188 struct freebsd32_ioctl_args *uap, struct file *fp)
190 struct fiodgname_arg fgn;
191 struct fiodgname_arg32 fgn32;
194 if ((error = copyin(uap->data, &fgn32, sizeof fgn32)) != 0)
197 PTRIN_CP(fgn32, fgn, buf);
198 error = fo_ioctl(fp, FIODGNAME, (caddr_t)&fgn, td->td_ucred, td);
203 freebsd32_ioctl_memrange(struct thread *td,
204 struct freebsd32_ioctl_args *uap, struct file *fp)
206 struct mem_range_op mro;
207 struct mem_range_op32 mro32;
211 if ((error = copyin(uap->data, &mro32, sizeof(mro32))) != 0)
214 PTRIN_CP(mro32, mro, mo_desc);
215 CP(mro32, mro, mo_arg[0]);
216 CP(mro32, mro, mo_arg[1]);
229 panic("%s: unknown MEMRANGE %#x", __func__, uap->com);
232 if ((error = fo_ioctl(fp, com, (caddr_t)&mro, td->td_ucred, td)) != 0)
235 if ( (com & IOC_OUT) ) {
236 CP(mro, mro32, mo_arg[0]);
237 CP(mro, mro32, mo_arg[1]);
239 error = copyout(&mro32, uap->data, sizeof(mro32));
246 freebsd32_ioctl_pciocgetconf(struct thread *td,
247 struct freebsd32_ioctl_args *uap, struct file *fp)
249 struct pci_conf_io pci;
250 struct pci_conf_io32 pci32;
251 struct pci_match_conf32 pmc32;
252 struct pci_match_conf32 *pmc32p;
253 struct pci_match_conf pmc;
254 struct pci_match_conf *pmcp;
255 struct pci_conf32 pc32;
256 struct pci_conf32 *pc32p;
258 struct pci_conf *pcp;
260 u_int32_t npat_to_convert;
261 u_int32_t nmatch_to_convert;
265 memset(&pmc, 0, sizeof(pmc));
266 memset(&pc32, 0, sizeof(pc32));
267 if ((error = copyin(uap->data, &pci32, sizeof(pci32))) != 0)
270 CP(pci32, pci, num_patterns);
271 CP(pci32, pci, offset);
272 CP(pci32, pci, generation);
274 npat_to_convert = pci32.pat_buf_len / sizeof(struct pci_match_conf32);
275 pci.pat_buf_len = npat_to_convert * sizeof(struct pci_match_conf);
277 nmatch_to_convert = pci32.match_buf_len / sizeof(struct pci_conf32);
278 pci.match_buf_len = nmatch_to_convert * sizeof(struct pci_conf);
281 if ((error = copyout_map(td, &addr, pci.pat_buf_len)) != 0)
283 pci.patterns = (struct pci_match_conf *)addr;
284 if ((error = copyout_map(td, &addr, pci.match_buf_len)) != 0)
286 pci.matches = (struct pci_conf *)addr;
288 npat_to_convert = min(npat_to_convert, pci.num_patterns);
290 for (i = 0, pmc32p = (struct pci_match_conf32 *)PTRIN(pci32.patterns),
292 i < npat_to_convert; i++, pmc32p++, pmcp++) {
293 if ((error = copyin(pmc32p, &pmc32, sizeof(pmc32))) != 0)
295 CP(pmc32,pmc,pc_sel);
296 strlcpy(pmc.pd_name, pmc32.pd_name, sizeof(pmc.pd_name));
297 CP(pmc32,pmc,pd_unit);
298 CP(pmc32,pmc,pc_vendor);
299 CP(pmc32,pmc,pc_device);
300 CP(pmc32,pmc,pc_class);
302 if ((error = copyout(&pmc, pmcp, sizeof(pmc))) != 0)
306 if ((error = fo_ioctl(fp, PCIOCGETCONF, (caddr_t)&pci,
307 td->td_ucred, td)) != 0)
310 nmatch_to_convert = min(nmatch_to_convert, pci.num_matches);
312 for (i = 0, pcp = pci.matches,
313 pc32p = (struct pci_conf32 *)PTRIN(pci32.matches);
314 i < nmatch_to_convert; i++, pcp++, pc32p++) {
315 if ((error = copyin(pcp, &pc, sizeof(pc))) != 0)
319 CP(pc,pc32,pc_subvendor);
320 CP(pc,pc32,pc_subdevice);
321 CP(pc,pc32,pc_vendor);
322 CP(pc,pc32,pc_device);
323 CP(pc,pc32,pc_class);
324 CP(pc,pc32,pc_subclass);
325 CP(pc,pc32,pc_progif);
326 CP(pc,pc32,pc_revid);
327 strlcpy(pc32.pd_name, pc.pd_name, sizeof(pc32.pd_name));
329 if ((error = copyout(&pc32, pc32p, sizeof(pc32))) != 0)
333 CP(pci, pci32, num_matches);
334 CP(pci, pci32, offset);
335 CP(pci, pci32, generation);
336 CP(pci, pci32, status);
338 error = copyout(&pci32, uap->data, sizeof(pci32));
342 copyout_unmap(td, (vm_offset_t)pci.patterns, pci.pat_buf_len);
344 copyout_unmap(td, (vm_offset_t)pci.matches, pci.match_buf_len);
350 freebsd32_ioctl_sg(struct thread *td,
351 struct freebsd32_ioctl_args *uap, struct file *fp)
354 struct sg_io_hdr32 io32;
357 if ((error = copyin(uap->data, &io32, sizeof(io32))) != 0)
360 CP(io32, io, interface_id);
361 CP(io32, io, dxfer_direction);
362 CP(io32, io, cmd_len);
363 CP(io32, io, mx_sb_len);
364 CP(io32, io, iovec_count);
365 CP(io32, io, dxfer_len);
366 PTRIN_CP(io32, io, dxferp);
367 PTRIN_CP(io32, io, cmdp);
368 PTRIN_CP(io32, io, sbp);
369 CP(io32, io, timeout);
371 CP(io32, io, pack_id);
372 PTRIN_CP(io32, io, usr_ptr);
373 CP(io32, io, status);
374 CP(io32, io, masked_status);
375 CP(io32, io, msg_status);
376 CP(io32, io, sb_len_wr);
377 CP(io32, io, host_status);
378 CP(io32, io, driver_status);
380 CP(io32, io, duration);
383 if ((error = fo_ioctl(fp, SG_IO, (caddr_t)&io, td->td_ucred, td)) != 0)
386 CP(io, io32, interface_id);
387 CP(io, io32, dxfer_direction);
388 CP(io, io32, cmd_len);
389 CP(io, io32, mx_sb_len);
390 CP(io, io32, iovec_count);
391 CP(io, io32, dxfer_len);
392 PTROUT_CP(io, io32, dxferp);
393 PTROUT_CP(io, io32, cmdp);
394 PTROUT_CP(io, io32, sbp);
395 CP(io, io32, timeout);
397 CP(io, io32, pack_id);
398 PTROUT_CP(io, io32, usr_ptr);
399 CP(io, io32, status);
400 CP(io, io32, masked_status);
401 CP(io, io32, msg_status);
402 CP(io, io32, sb_len_wr);
403 CP(io, io32, host_status);
404 CP(io, io32, driver_status);
406 CP(io, io32, duration);
409 error = copyout(&io32, uap->data, sizeof(io32));
415 freebsd32_ioctl(struct thread *td, struct freebsd32_ioctl_args *uap)
417 struct ioctl_args ap /*{
426 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_IOCTL), &fp);
429 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
435 case MDIOCATTACH_32: /* FALLTHROUGH */
436 case MDIOCDETACH_32: /* FALLTHROUGH */
437 case MDIOCQUERY_32: /* FALLTHROUGH */
439 error = freebsd32_ioctl_md(td, uap, fp);
442 case CDIOREADTOCENTRYS_32:
443 error = freebsd32_ioctl_ioc_read_toc(td, uap, fp);
446 case CDIOREADTOCHEADER_32:
447 error = freebsd32_ioctl_ioc_toc_header(td, uap, fp);
451 error = freebsd32_ioctl_fiodgname(td, uap, fp);
454 case MEMRANGE_GET32: /* FALLTHROUGH */
456 error = freebsd32_ioctl_memrange(td, uap, fp);
459 case PCIOCGETCONF_32:
460 error = freebsd32_ioctl_pciocgetconf(td, uap, fp);
464 error = freebsd32_ioctl_sg(td, uap, fp);
471 PTRIN_CP(*uap, ap, data);
472 return sys_ioctl(td, &ap);