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);
63 freebsd32_ioctl_ioc_read_toc(struct thread *td,
64 struct freebsd32_ioctl_args *uap, struct file *fp)
66 struct ioc_read_toc_entry toce;
67 struct ioc_read_toc_entry32 toce32;
70 if ((error = copyin(uap->data, &toce32, sizeof(toce32))))
72 CP(toce32, toce, address_format);
73 CP(toce32, toce, starting_track);
74 CP(toce32, toce, data_len);
75 PTRIN_CP(toce32, toce, data);
77 if ((error = fo_ioctl(fp, CDIOREADTOCENTRYS, (caddr_t)&toce,
79 CP(toce, toce32, address_format);
80 CP(toce, toce32, starting_track);
81 CP(toce, toce32, data_len);
82 PTROUT_CP(toce, toce32, data);
83 error = copyout(&toce32, uap->data, sizeof(toce32));
89 freebsd32_ioctl_fiodgname(struct thread *td,
90 struct freebsd32_ioctl_args *uap, struct file *fp)
92 struct fiodgname_arg fgn;
93 struct fiodgname_arg32 fgn32;
96 if ((error = copyin(uap->data, &fgn32, sizeof fgn32)) != 0)
99 PTRIN_CP(fgn32, fgn, buf);
100 error = fo_ioctl(fp, FIODGNAME, (caddr_t)&fgn, td->td_ucred, td);
105 freebsd32_ioctl_memrange(struct thread *td,
106 struct freebsd32_ioctl_args *uap, struct file *fp)
108 struct mem_range_op mro;
109 struct mem_range_op32 mro32;
113 if ((error = copyin(uap->data, &mro32, sizeof(mro32))) != 0)
116 PTRIN_CP(mro32, mro, mo_desc);
117 CP(mro32, mro, mo_arg[0]);
118 CP(mro32, mro, mo_arg[1]);
131 panic("%s: unknown MEMRANGE %#x", __func__, uap->com);
134 if ((error = fo_ioctl(fp, com, (caddr_t)&mro, td->td_ucred, td)) != 0)
137 if ( (com & IOC_OUT) ) {
138 CP(mro, mro32, mo_arg[0]);
139 CP(mro, mro32, mo_arg[1]);
141 error = copyout(&mro32, uap->data, sizeof(mro32));
148 freebsd32_ioctl_barmmap(struct thread *td,
149 struct freebsd32_ioctl_args *uap, struct file *fp)
151 struct pci_bar_mmap32 pbm32;
152 struct pci_bar_mmap pbm;
155 error = copyin(uap->data, &pbm32, sizeof(pbm32));
158 PTRIN_CP(pbm32, pbm, pbm_map_base);
159 CP(pbm32, pbm, pbm_sel);
160 CP(pbm32, pbm, pbm_reg);
161 CP(pbm32, pbm, pbm_flags);
162 CP(pbm32, pbm, pbm_memattr);
163 pbm.pbm_bar_length = PAIR32TO64(uint64_t, pbm32.pbm_bar_length);
164 error = fo_ioctl(fp, PCIOCBARMMAP, (caddr_t)&pbm, td->td_ucred, td);
166 PTROUT_CP(pbm, pbm32, pbm_map_base);
167 CP(pbm, pbm32, pbm_map_length);
168 #if BYTE_ORDER == LITTLE_ENDIAN
169 pbm32.pbm_bar_length1 = pbm.pbm_bar_length;
170 pbm32.pbm_bar_length2 = pbm.pbm_bar_length >> 32;
172 pbm32.pbm_bar_length1 = pbm.pbm_bar_length >> 32;
173 pbm32.pbm_bar_length2 = pbm.pbm_bar_length;
175 CP(pbm, pbm32, pbm_bar_off);
176 error = copyout(&pbm32, uap->data, sizeof(pbm32));
182 freebsd32_ioctl_sg(struct thread *td,
183 struct freebsd32_ioctl_args *uap, struct file *fp)
186 struct sg_io_hdr32 io32;
189 if ((error = copyin(uap->data, &io32, sizeof(io32))) != 0)
192 CP(io32, io, interface_id);
193 CP(io32, io, dxfer_direction);
194 CP(io32, io, cmd_len);
195 CP(io32, io, mx_sb_len);
196 CP(io32, io, iovec_count);
197 CP(io32, io, dxfer_len);
198 PTRIN_CP(io32, io, dxferp);
199 PTRIN_CP(io32, io, cmdp);
200 PTRIN_CP(io32, io, sbp);
201 CP(io32, io, timeout);
203 CP(io32, io, pack_id);
204 PTRIN_CP(io32, io, usr_ptr);
205 CP(io32, io, status);
206 CP(io32, io, masked_status);
207 CP(io32, io, msg_status);
208 CP(io32, io, sb_len_wr);
209 CP(io32, io, host_status);
210 CP(io32, io, driver_status);
212 CP(io32, io, duration);
215 if ((error = fo_ioctl(fp, SG_IO, (caddr_t)&io, td->td_ucred, td)) != 0)
218 CP(io, io32, interface_id);
219 CP(io, io32, dxfer_direction);
220 CP(io, io32, cmd_len);
221 CP(io, io32, mx_sb_len);
222 CP(io, io32, iovec_count);
223 CP(io, io32, dxfer_len);
224 PTROUT_CP(io, io32, dxferp);
225 PTROUT_CP(io, io32, cmdp);
226 PTROUT_CP(io, io32, sbp);
227 CP(io, io32, timeout);
229 CP(io, io32, pack_id);
230 PTROUT_CP(io, io32, usr_ptr);
231 CP(io, io32, status);
232 CP(io, io32, masked_status);
233 CP(io, io32, msg_status);
234 CP(io, io32, sb_len_wr);
235 CP(io, io32, host_status);
236 CP(io, io32, driver_status);
238 CP(io, io32, duration);
241 error = copyout(&io32, uap->data, sizeof(io32));
247 freebsd32_ioctl(struct thread *td, struct freebsd32_ioctl_args *uap)
249 struct ioctl_args ap /*{
258 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_IOCTL), &fp);
261 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
267 case CDIOREADTOCENTRYS_32:
268 error = freebsd32_ioctl_ioc_read_toc(td, uap, fp);
272 error = freebsd32_ioctl_fiodgname(td, uap, fp);
275 case MEMRANGE_GET32: /* FALLTHROUGH */
277 error = freebsd32_ioctl_memrange(td, uap, fp);
281 error = freebsd32_ioctl_sg(td, uap, fp);
284 case PCIOCBARMMAP_32:
285 error = freebsd32_ioctl_barmmap(td, uap, fp);
292 PTRIN_CP(*uap, ap, data);
293 return sys_ioctl(td, &ap);