2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2008 David E. O'Brien
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the author nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
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/memrange.h>
44 #include <sys/pciio.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>
54 #include <compat/freebsd32/freebsd32.h>
55 #include <compat/freebsd32/freebsd32_ioctl.h>
56 #include <compat/freebsd32/freebsd32_misc.h>
57 #include <compat/freebsd32/freebsd32_proto.h>
59 CTASSERT(sizeof(struct ioc_read_toc_entry32) == 8);
60 CTASSERT(sizeof(struct mem_range_op32) == 12);
61 CTASSERT(sizeof(struct pci_conf_io32) == 36);
62 CTASSERT(sizeof(struct pci_match_conf32) == 44);
63 CTASSERT(sizeof(struct pci_conf32) == 44);
66 freebsd32_ioctl_ioc_read_toc(struct thread *td,
67 struct freebsd32_ioctl_args *uap, struct file *fp)
69 struct ioc_read_toc_entry toce;
70 struct ioc_read_toc_entry32 toce32;
73 if ((error = copyin(uap->data, &toce32, sizeof(toce32))))
75 CP(toce32, toce, address_format);
76 CP(toce32, toce, starting_track);
77 CP(toce32, toce, data_len);
78 PTRIN_CP(toce32, toce, data);
80 if ((error = fo_ioctl(fp, CDIOREADTOCENTRYS, (caddr_t)&toce,
82 CP(toce, toce32, address_format);
83 CP(toce, toce32, starting_track);
84 CP(toce, toce32, data_len);
85 PTROUT_CP(toce, toce32, data);
86 error = copyout(&toce32, uap->data, sizeof(toce32));
92 freebsd32_ioctl_fiodgname(struct thread *td,
93 struct freebsd32_ioctl_args *uap, struct file *fp)
95 struct fiodgname_arg fgn;
96 struct fiodgname_arg32 fgn32;
99 if ((error = copyin(uap->data, &fgn32, sizeof fgn32)) != 0)
102 PTRIN_CP(fgn32, fgn, buf);
103 error = fo_ioctl(fp, FIODGNAME, (caddr_t)&fgn, td->td_ucred, td);
108 freebsd32_ioctl_memrange(struct thread *td,
109 struct freebsd32_ioctl_args *uap, struct file *fp)
111 struct mem_range_op mro;
112 struct mem_range_op32 mro32;
116 if ((error = copyin(uap->data, &mro32, sizeof(mro32))) != 0)
119 PTRIN_CP(mro32, mro, mo_desc);
120 CP(mro32, mro, mo_arg[0]);
121 CP(mro32, mro, mo_arg[1]);
134 panic("%s: unknown MEMRANGE %#x", __func__, uap->com);
137 if ((error = fo_ioctl(fp, com, (caddr_t)&mro, td->td_ucred, td)) != 0)
140 if ( (com & IOC_OUT) ) {
141 CP(mro, mro32, mo_arg[0]);
142 CP(mro, mro32, mo_arg[1]);
144 error = copyout(&mro32, uap->data, sizeof(mro32));
151 freebsd32_ioctl_pciocgetconf(struct thread *td,
152 struct freebsd32_ioctl_args *uap, struct file *fp)
154 struct pci_conf_io pci;
155 struct pci_conf_io32 pci32;
156 struct pci_match_conf32 pmc32;
157 struct pci_match_conf32 *pmc32p;
158 struct pci_match_conf pmc;
159 struct pci_match_conf *pmcp;
160 struct pci_conf32 pc32;
161 struct pci_conf32 *pc32p;
163 struct pci_conf *pcp;
165 u_int32_t npat_to_convert;
166 u_int32_t nmatch_to_convert;
170 if ((error = copyin(uap->data, &pci32, sizeof(pci32))) != 0)
173 CP(pci32, pci, num_patterns);
174 CP(pci32, pci, offset);
175 CP(pci32, pci, generation);
177 npat_to_convert = pci32.pat_buf_len / sizeof(struct pci_match_conf32);
178 pci.pat_buf_len = npat_to_convert * sizeof(struct pci_match_conf);
180 nmatch_to_convert = pci32.match_buf_len / sizeof(struct pci_conf32);
181 pci.match_buf_len = nmatch_to_convert * sizeof(struct pci_conf);
184 if ((error = copyout_map(td, &addr, pci.pat_buf_len)) != 0)
186 pci.patterns = (struct pci_match_conf *)addr;
187 if ((error = copyout_map(td, &addr, pci.match_buf_len)) != 0)
189 pci.matches = (struct pci_conf *)addr;
191 npat_to_convert = min(npat_to_convert, pci.num_patterns);
193 for (i = 0, pmc32p = (struct pci_match_conf32 *)PTRIN(pci32.patterns),
195 i < npat_to_convert; i++, pmc32p++, pmcp++) {
196 if ((error = copyin(pmc32p, &pmc32, sizeof(pmc32))) != 0)
198 CP(pmc32,pmc,pc_sel);
199 strlcpy(pmc.pd_name, pmc32.pd_name, sizeof(pmc.pd_name));
200 CP(pmc32,pmc,pd_unit);
201 CP(pmc32,pmc,pc_vendor);
202 CP(pmc32,pmc,pc_device);
203 CP(pmc32,pmc,pc_class);
205 if ((error = copyout(&pmc, pmcp, sizeof(pmc))) != 0)
209 if ((error = fo_ioctl(fp, PCIOCGETCONF, (caddr_t)&pci,
210 td->td_ucred, td)) != 0)
213 nmatch_to_convert = min(nmatch_to_convert, pci.num_matches);
215 for (i = 0, pcp = pci.matches,
216 pc32p = (struct pci_conf32 *)PTRIN(pci32.matches);
217 i < nmatch_to_convert; i++, pcp++, pc32p++) {
218 if ((error = copyin(pcp, &pc, sizeof(pc))) != 0)
222 CP(pc,pc32,pc_subvendor);
223 CP(pc,pc32,pc_subdevice);
224 CP(pc,pc32,pc_vendor);
225 CP(pc,pc32,pc_device);
226 CP(pc,pc32,pc_class);
227 CP(pc,pc32,pc_subclass);
228 CP(pc,pc32,pc_progif);
229 CP(pc,pc32,pc_revid);
230 strlcpy(pc32.pd_name, pc.pd_name, sizeof(pc32.pd_name));
232 if ((error = copyout(&pc32, pc32p, sizeof(pc32))) != 0)
236 CP(pci, pci32, num_matches);
237 CP(pci, pci32, offset);
238 CP(pci, pci32, generation);
239 CP(pci, pci32, status);
241 error = copyout(&pci32, uap->data, sizeof(pci32));
245 copyout_unmap(td, (vm_offset_t)pci.patterns, pci.pat_buf_len);
247 copyout_unmap(td, (vm_offset_t)pci.matches, pci.match_buf_len);
253 freebsd32_ioctl_barmmap(struct thread *td,
254 struct freebsd32_ioctl_args *uap, struct file *fp)
256 struct pci_bar_mmap32 pbm32;
257 struct pci_bar_mmap pbm;
260 error = copyin(uap->data, &pbm32, sizeof(pbm32));
263 PTRIN_CP(pbm32, pbm, pbm_map_base);
264 CP(pbm32, pbm, pbm_sel);
265 CP(pbm32, pbm, pbm_reg);
266 CP(pbm32, pbm, pbm_flags);
267 CP(pbm32, pbm, pbm_memattr);
268 pbm.pbm_bar_length = PAIR32TO64(uint64_t, pbm32.pbm_bar_length);
269 error = fo_ioctl(fp, PCIOCBARMMAP, (caddr_t)&pbm, td->td_ucred, td);
271 PTROUT_CP(pbm, pbm32, pbm_map_base);
272 CP(pbm, pbm32, pbm_map_length);
273 #if BYTE_ORDER == LITTLE_ENDIAN
274 pbm32.pbm_bar_length1 = pbm.pbm_bar_length;
275 pbm32.pbm_bar_length2 = pbm.pbm_bar_length >> 32;
277 pbm32.pbm_bar_length1 = pbm.pbm_bar_length >> 32;
278 pbm32.pbm_bar_length2 = pbm.pbm_bar_length;
280 CP(pbm, pbm32, pbm_bar_off);
281 error = copyout(&pbm32, uap->data, sizeof(pbm32));
287 freebsd32_ioctl_sg(struct thread *td,
288 struct freebsd32_ioctl_args *uap, struct file *fp)
291 struct sg_io_hdr32 io32;
294 if ((error = copyin(uap->data, &io32, sizeof(io32))) != 0)
297 CP(io32, io, interface_id);
298 CP(io32, io, dxfer_direction);
299 CP(io32, io, cmd_len);
300 CP(io32, io, mx_sb_len);
301 CP(io32, io, iovec_count);
302 CP(io32, io, dxfer_len);
303 PTRIN_CP(io32, io, dxferp);
304 PTRIN_CP(io32, io, cmdp);
305 PTRIN_CP(io32, io, sbp);
306 CP(io32, io, timeout);
308 CP(io32, io, pack_id);
309 PTRIN_CP(io32, io, usr_ptr);
310 CP(io32, io, status);
311 CP(io32, io, masked_status);
312 CP(io32, io, msg_status);
313 CP(io32, io, sb_len_wr);
314 CP(io32, io, host_status);
315 CP(io32, io, driver_status);
317 CP(io32, io, duration);
320 if ((error = fo_ioctl(fp, SG_IO, (caddr_t)&io, td->td_ucred, td)) != 0)
323 CP(io, io32, interface_id);
324 CP(io, io32, dxfer_direction);
325 CP(io, io32, cmd_len);
326 CP(io, io32, mx_sb_len);
327 CP(io, io32, iovec_count);
328 CP(io, io32, dxfer_len);
329 PTROUT_CP(io, io32, dxferp);
330 PTROUT_CP(io, io32, cmdp);
331 PTROUT_CP(io, io32, sbp);
332 CP(io, io32, timeout);
334 CP(io, io32, pack_id);
335 PTROUT_CP(io, io32, usr_ptr);
336 CP(io, io32, status);
337 CP(io, io32, masked_status);
338 CP(io, io32, msg_status);
339 CP(io, io32, sb_len_wr);
340 CP(io, io32, host_status);
341 CP(io, io32, driver_status);
343 CP(io, io32, duration);
346 error = copyout(&io32, uap->data, sizeof(io32));
352 freebsd32_ioctl(struct thread *td, struct freebsd32_ioctl_args *uap)
354 struct ioctl_args ap /*{
363 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_IOCTL), &fp);
366 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
372 case CDIOREADTOCENTRYS_32:
373 error = freebsd32_ioctl_ioc_read_toc(td, uap, fp);
377 error = freebsd32_ioctl_fiodgname(td, uap, fp);
380 case MEMRANGE_GET32: /* FALLTHROUGH */
382 error = freebsd32_ioctl_memrange(td, uap, fp);
385 case PCIOCGETCONF_32:
386 error = freebsd32_ioctl_pciocgetconf(td, uap, fp);
390 error = freebsd32_ioctl_sg(td, uap, fp);
393 case PCIOCBARMMAP_32:
394 error = freebsd32_ioctl_barmmap(td, uap, fp);
401 PTRIN_CP(*uap, ap, data);
402 return sys_ioctl(td, &ap);