]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/freebsd32/freebsd32_ioctl.c
Merge ^/head r338988 through r339014.
[FreeBSD/FreeBSD.git] / sys / compat / freebsd32 / freebsd32_ioctl.c
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 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/capsicum.h>
37 #include <sys/cdio.h>
38 #include <sys/fcntl.h>
39 #include <sys/filio.h>
40 #include <sys/file.h>
41 #include <sys/ioccom.h>
42 #include <sys/malloc.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 #include <sys/uio.h>
53
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>
58
59 CTASSERT(sizeof(struct ioc_read_toc_entry32) == 8);
60 CTASSERT(sizeof(struct mem_range_op32) == 12);
61
62 static int
63 freebsd32_ioctl_ioc_read_toc(struct thread *td,
64     struct freebsd32_ioctl_args *uap, struct file *fp)
65 {
66         struct ioc_read_toc_entry toce;
67         struct ioc_read_toc_entry32 toce32;
68         int error;
69
70         if ((error = copyin(uap->data, &toce32, sizeof(toce32))))
71                 return (error);
72         CP(toce32, toce, address_format);
73         CP(toce32, toce, starting_track);
74         CP(toce32, toce, data_len);
75         PTRIN_CP(toce32, toce, data);
76
77         if ((error = fo_ioctl(fp, CDIOREADTOCENTRYS, (caddr_t)&toce,
78             td->td_ucred, td))) {
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));
84         }
85         return error;
86 }
87
88 static int
89 freebsd32_ioctl_fiodgname(struct thread *td,
90     struct freebsd32_ioctl_args *uap, struct file *fp)
91 {
92         struct fiodgname_arg fgn;
93         struct fiodgname_arg32 fgn32;
94         int error;
95
96         if ((error = copyin(uap->data, &fgn32, sizeof fgn32)) != 0)
97                 return (error);
98         CP(fgn32, fgn, len);
99         PTRIN_CP(fgn32, fgn, buf);
100         error = fo_ioctl(fp, FIODGNAME, (caddr_t)&fgn, td->td_ucred, td);
101         return (error);
102 }
103
104 static int
105 freebsd32_ioctl_memrange(struct thread *td,
106     struct freebsd32_ioctl_args *uap, struct file *fp)
107 {
108         struct mem_range_op mro;
109         struct mem_range_op32 mro32;
110         int error;
111         u_long com;
112
113         if ((error = copyin(uap->data, &mro32, sizeof(mro32))) != 0)
114                 return (error);
115
116         PTRIN_CP(mro32, mro, mo_desc);
117         CP(mro32, mro, mo_arg[0]);
118         CP(mro32, mro, mo_arg[1]);
119
120         com = 0;
121         switch (uap->com) {
122         case MEMRANGE_GET32:
123                 com = MEMRANGE_GET;
124                 break;
125
126         case MEMRANGE_SET32:
127                 com = MEMRANGE_SET;
128                 break;
129
130         default:
131                 panic("%s: unknown MEMRANGE %#x", __func__, uap->com);
132         }
133
134         if ((error = fo_ioctl(fp, com, (caddr_t)&mro, td->td_ucred, td)) != 0)
135                 return (error);
136
137         if ( (com & IOC_OUT) ) {
138                 CP(mro, mro32, mo_arg[0]);
139                 CP(mro, mro32, mo_arg[1]);
140
141                 error = copyout(&mro32, uap->data, sizeof(mro32));
142         }
143
144         return (error);
145 }
146
147 static int
148 freebsd32_ioctl_barmmap(struct thread *td,
149     struct freebsd32_ioctl_args *uap, struct file *fp)
150 {
151         struct pci_bar_mmap32 pbm32;
152         struct pci_bar_mmap pbm;
153         int error;
154
155         error = copyin(uap->data, &pbm32, sizeof(pbm32));
156         if (error != 0)
157                 return (error);
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);
165         if (error == 0) {
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;
171 #else
172                 pbm32.pbm_bar_length1 = pbm.pbm_bar_length >> 32;
173                 pbm32.pbm_bar_length2 = pbm.pbm_bar_length;
174 #endif
175                 CP(pbm, pbm32, pbm_bar_off);
176                 error = copyout(&pbm32, uap->data, sizeof(pbm32));
177         }
178         return (error);
179 }
180
181 static int
182 freebsd32_ioctl_sg(struct thread *td,
183     struct freebsd32_ioctl_args *uap, struct file *fp)
184 {
185         struct sg_io_hdr io;
186         struct sg_io_hdr32 io32;
187         int error;
188
189         if ((error = copyin(uap->data, &io32, sizeof(io32))) != 0)
190                 return (error);
191
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);
202         CP(io32, io, flags);
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);
211         CP(io32, io, resid);
212         CP(io32, io, duration);
213         CP(io32, io, info);
214
215         if ((error = fo_ioctl(fp, SG_IO, (caddr_t)&io, td->td_ucred, td)) != 0)
216                 return (error);
217
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);
228         CP(io, io32, flags);
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);
237         CP(io, io32, resid);
238         CP(io, io32, duration);
239         CP(io, io32, info);
240
241         error = copyout(&io32, uap->data, sizeof(io32));
242
243         return (error);
244 }
245
246 int
247 freebsd32_ioctl(struct thread *td, struct freebsd32_ioctl_args *uap)
248 {
249         struct ioctl_args ap /*{
250                 int     fd;
251                 u_long  com;
252                 caddr_t data;
253         }*/ ;
254         struct file *fp;
255         cap_rights_t rights;
256         int error;
257
258         error = fget(td, uap->fd, cap_rights_init(&rights, CAP_IOCTL), &fp);
259         if (error != 0)
260                 return (error);
261         if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
262                 fdrop(fp, td);
263                 return (EBADF);
264         }
265
266         switch (uap->com) {
267         case CDIOREADTOCENTRYS_32:
268                 error = freebsd32_ioctl_ioc_read_toc(td, uap, fp);
269                 break;
270
271         case FIODGNAME_32:
272                 error = freebsd32_ioctl_fiodgname(td, uap, fp);
273                 break;
274
275         case MEMRANGE_GET32:    /* FALLTHROUGH */
276         case MEMRANGE_SET32:
277                 error = freebsd32_ioctl_memrange(td, uap, fp);
278                 break;
279
280         case SG_IO_32:
281                 error = freebsd32_ioctl_sg(td, uap, fp);
282                 break;
283
284         case PCIOCBARMMAP_32:
285                 error = freebsd32_ioctl_barmmap(td, uap, fp);
286                 break;
287
288         default:
289                 fdrop(fp, td);
290                 ap.fd = uap->fd;
291                 ap.com = uap->com;
292                 PTRIN_CP(*uap, ap, data);
293                 return sys_ioctl(td, &ap);
294         }
295
296         fdrop(fp, td);
297         return (error);
298 }