1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2008 David E. O'Brien
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/capsicum.h>
35 #include <sys/cdio.h>
36 #include <sys/fcntl.h>
37 #include <sys/filio.h>
38 #include <sys/file.h>
39 #include <sys/ioccom.h>
40 #include <sys/malloc.h>
41 #include <sys/memrange.h>
42 #include <sys/pciio.h>
43 #include <sys/proc.h>
44 #include <sys/syscall.h>
45 #include <sys/syscallsubr.h>
46 #include <sys/sysctl.h>
47 #include <sys/sysproto.h>
48 #include <sys/systm.h>
49 #include <sys/uio.h>
50
51 #include <compat/freebsd32/freebsd32.h>
52 #include <compat/freebsd32/freebsd32_ioctl.h>
53 #include <compat/freebsd32/freebsd32_misc.h>
54 #include <compat/freebsd32/freebsd32_proto.h>
55
56 CTASSERT(sizeof(struct mem_range_op32) == 12);
57
58 static int
freebsd32_ioctl_memrange(struct thread * td,struct freebsd32_ioctl_args * uap,struct file * fp)59 freebsd32_ioctl_memrange(struct thread *td,
60 struct freebsd32_ioctl_args *uap, struct file *fp)
61 {
62 struct mem_range_op mro;
63 struct mem_range_op32 mro32;
64 int error;
65 u_long com;
66
67 if ((error = copyin(uap->data, &mro32, sizeof(mro32))) != 0)
68 return (error);
69
70 PTRIN_CP(mro32, mro, mo_desc);
71 CP(mro32, mro, mo_arg[0]);
72 CP(mro32, mro, mo_arg[1]);
73
74 com = 0;
75 switch (uap->com) {
76 case MEMRANGE_GET32:
77 com = MEMRANGE_GET;
78 break;
79
80 case MEMRANGE_SET32:
81 com = MEMRANGE_SET;
82 break;
83
84 default:
85 panic("%s: unknown MEMRANGE %#x", __func__, uap->com);
86 }
87
88 if ((error = fo_ioctl(fp, com, (caddr_t)&mro, td->td_ucred, td)) != 0)
89 return (error);
90
91 if ( (com & IOC_OUT) ) {
92 CP(mro, mro32, mo_arg[0]);
93 CP(mro, mro32, mo_arg[1]);
94
95 error = copyout(&mro32, uap->data, sizeof(mro32));
96 }
97
98 return (error);
99 }
100
101 static int
freebsd32_ioctl_barmmap(struct thread * td,struct freebsd32_ioctl_args * uap,struct file * fp)102 freebsd32_ioctl_barmmap(struct thread *td,
103 struct freebsd32_ioctl_args *uap, struct file *fp)
104 {
105 struct pci_bar_mmap32 pbm32;
106 struct pci_bar_mmap pbm;
107 int error;
108
109 error = copyin(uap->data, &pbm32, sizeof(pbm32));
110 if (error != 0)
111 return (error);
112 PTRIN_CP(pbm32, pbm, pbm_map_base);
113 CP(pbm32, pbm, pbm_sel);
114 CP(pbm32, pbm, pbm_reg);
115 CP(pbm32, pbm, pbm_flags);
116 CP(pbm32, pbm, pbm_memattr);
117 pbm.pbm_bar_length = PAIR32TO64(uint64_t, pbm32.pbm_bar_length);
118 error = fo_ioctl(fp, PCIOCBARMMAP, (caddr_t)&pbm, td->td_ucred, td);
119 if (error == 0) {
120 PTROUT_CP(pbm, pbm32, pbm_map_base);
121 CP(pbm, pbm32, pbm_map_length);
122 #if BYTE_ORDER == LITTLE_ENDIAN
123 pbm32.pbm_bar_length1 = pbm.pbm_bar_length;
124 pbm32.pbm_bar_length2 = pbm.pbm_bar_length >> 32;
125 #else
126 pbm32.pbm_bar_length1 = pbm.pbm_bar_length >> 32;
127 pbm32.pbm_bar_length2 = pbm.pbm_bar_length;
128 #endif
129 CP(pbm, pbm32, pbm_bar_off);
130 error = copyout(&pbm32, uap->data, sizeof(pbm32));
131 }
132 return (error);
133 }
134
135 static int
freebsd32_ioctl_sg(struct thread * td,struct freebsd32_ioctl_args * uap,struct file * fp)136 freebsd32_ioctl_sg(struct thread *td,
137 struct freebsd32_ioctl_args *uap, struct file *fp)
138 {
139 struct sg_io_hdr io;
140 struct sg_io_hdr32 io32;
141 int error;
142
143 if ((error = copyin(uap->data, &io32, sizeof(io32))) != 0)
144 return (error);
145
146 CP(io32, io, interface_id);
147 CP(io32, io, dxfer_direction);
148 CP(io32, io, cmd_len);
149 CP(io32, io, mx_sb_len);
150 CP(io32, io, iovec_count);
151 CP(io32, io, dxfer_len);
152 PTRIN_CP(io32, io, dxferp);
153 PTRIN_CP(io32, io, cmdp);
154 PTRIN_CP(io32, io, sbp);
155 CP(io32, io, timeout);
156 CP(io32, io, flags);
157 CP(io32, io, pack_id);
158 PTRIN_CP(io32, io, usr_ptr);
159 CP(io32, io, status);
160 CP(io32, io, masked_status);
161 CP(io32, io, msg_status);
162 CP(io32, io, sb_len_wr);
163 CP(io32, io, host_status);
164 CP(io32, io, driver_status);
165 CP(io32, io, resid);
166 CP(io32, io, duration);
167 CP(io32, io, info);
168
169 if ((error = fo_ioctl(fp, SG_IO, (caddr_t)&io, td->td_ucred, td)) != 0)
170 return (error);
171
172 CP(io, io32, interface_id);
173 CP(io, io32, dxfer_direction);
174 CP(io, io32, cmd_len);
175 CP(io, io32, mx_sb_len);
176 CP(io, io32, iovec_count);
177 CP(io, io32, dxfer_len);
178 PTROUT_CP(io, io32, dxferp);
179 PTROUT_CP(io, io32, cmdp);
180 PTROUT_CP(io, io32, sbp);
181 CP(io, io32, timeout);
182 CP(io, io32, flags);
183 CP(io, io32, pack_id);
184 PTROUT_CP(io, io32, usr_ptr);
185 CP(io, io32, status);
186 CP(io, io32, masked_status);
187 CP(io, io32, msg_status);
188 CP(io, io32, sb_len_wr);
189 CP(io, io32, host_status);
190 CP(io, io32, driver_status);
191 CP(io, io32, resid);
192 CP(io, io32, duration);
193 CP(io, io32, info);
194
195 error = copyout(&io32, uap->data, sizeof(io32));
196
197 return (error);
198 }
199
200 int
freebsd32_ioctl(struct thread * td,struct freebsd32_ioctl_args * uap)201 freebsd32_ioctl(struct thread *td, struct freebsd32_ioctl_args *uap)
202 {
203 struct ioctl_args ap /*{
204 int fd;
205 u_long com;
206 caddr_t data;
207 }*/ ;
208 struct file *fp;
209 cap_rights_t rights;
210 int error;
211
212 error = fget(td, uap->fd, cap_rights_init_one(&rights, CAP_IOCTL), &fp);
213 if (error != 0)
214 return (error);
215 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
216 fdrop(fp, td);
217 return (EBADF);
218 }
219
220 switch (uap->com) {
221 case MEMRANGE_GET32: /* FALLTHROUGH */
222 case MEMRANGE_SET32:
223 error = freebsd32_ioctl_memrange(td, uap, fp);
224 break;
225
226 case SG_IO_32:
227 error = freebsd32_ioctl_sg(td, uap, fp);
228 break;
229
230 case PCIOCBARMMAP_32:
231 error = freebsd32_ioctl_barmmap(td, uap, fp);
232 break;
233
234 default:
235 fdrop(fp, td);
236 ap.fd = uap->fd;
237 ap.com = uap->com;
238 PTRIN_CP(*uap, ap, data);
239 return sys_ioctl(td, &ap);
240 }
241
242 fdrop(fp, td);
243 return (error);
244 }
245