]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/freebsd32/freebsd32_ioctl.c
dts: Update our device tree sources file fomr Linux 4.13
[FreeBSD/FreeBSD.git] / sys / compat / freebsd32 / freebsd32_ioctl.c
1 /*-
2  * Copyright (c) 2008 David E. O'Brien
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include "opt_compat.h"
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/mdioctl.h>
44 #include <sys/memrange.h>
45 #include <sys/pciio.h>
46 #include <sys/proc.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>
53
54 #include <compat/freebsd32/freebsd32.h>
55 #include <compat/freebsd32/freebsd32_ioctl.h>
56 #include <compat/freebsd32/freebsd32_proto.h>
57
58 CTASSERT((sizeof(struct md_ioctl32)) == 436);
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);
64
65
66 static int
67 freebsd32_ioctl_md(struct thread *td, struct freebsd32_ioctl_args *uap,
68     struct file *fp)
69 {
70         struct md_ioctl mdv;
71         struct md_ioctl32 md32;
72         u_long com = 0;
73         int i, error;
74
75         if (uap->com & IOC_IN) {
76                 if ((error = copyin(uap->data, &md32, sizeof(md32)))) {
77                         return (error);
78                 }
79                 CP(md32, mdv, md_version);
80                 CP(md32, mdv, md_unit);
81                 CP(md32, mdv, md_type);
82                 PTRIN_CP(md32, mdv, md_file);
83                 CP(md32, mdv, md_mediasize);
84                 CP(md32, mdv, md_sectorsize);
85                 CP(md32, mdv, md_options);
86                 CP(md32, mdv, md_base);
87                 CP(md32, mdv, md_fwheads);
88                 CP(md32, mdv, md_fwsectors);
89                 PTRIN_CP(md32, mdv, md_label);
90         } else if (uap->com & IOC_OUT) {
91                 /*
92                  * Zero the buffer so the user always
93                  * gets back something deterministic.
94                  */
95                 bzero(&mdv, sizeof mdv);
96         }
97
98         switch (uap->com) {
99         case MDIOCATTACH_32:
100                 com = MDIOCATTACH;
101                 break;
102         case MDIOCDETACH_32:
103                 com = MDIOCDETACH;
104                 break;
105         case MDIOCQUERY_32:
106                 com = MDIOCQUERY;
107                 break;
108         case MDIOCLIST_32:
109                 com = MDIOCLIST;
110                 break;
111         default:
112                 panic("%s: unknown MDIOC %#x", __func__, uap->com);
113         }
114         error = fo_ioctl(fp, com, (caddr_t)&mdv, td->td_ucred, td);
115         if (error == 0 && (com & IOC_OUT)) {
116                 CP(mdv, md32, md_version);
117                 CP(mdv, md32, md_unit);
118                 CP(mdv, md32, md_type);
119                 PTROUT_CP(mdv, md32, md_file);
120                 CP(mdv, md32, md_mediasize);
121                 CP(mdv, md32, md_sectorsize);
122                 CP(mdv, md32, md_options);
123                 CP(mdv, md32, md_base);
124                 CP(mdv, md32, md_fwheads);
125                 CP(mdv, md32, md_fwsectors);
126                 PTROUT_CP(mdv, md32, md_label);
127                 if (com == MDIOCLIST) {
128                         /*
129                          * Use MDNPAD, and not MDNPAD32.  Padding is
130                          * allocated and used by compat32 ABI.
131                          */
132                         for (i = 0; i < MDNPAD; i++)
133                                 CP(mdv, md32, md_pad[i]);
134                 }
135                 error = copyout(&md32, uap->data, sizeof(md32));
136         }
137         return error;
138 }
139
140
141 static int
142 freebsd32_ioctl_ioc_read_toc(struct thread *td,
143     struct freebsd32_ioctl_args *uap, struct file *fp)
144 {
145         struct ioc_read_toc_entry toce;
146         struct ioc_read_toc_entry32 toce32;
147         int error;
148
149         if ((error = copyin(uap->data, &toce32, sizeof(toce32))))
150                 return (error);
151         CP(toce32, toce, address_format);
152         CP(toce32, toce, starting_track);
153         CP(toce32, toce, data_len);
154         PTRIN_CP(toce32, toce, data);
155
156         if ((error = fo_ioctl(fp, CDIOREADTOCENTRYS, (caddr_t)&toce,
157             td->td_ucred, td))) {
158                 CP(toce, toce32, address_format);
159                 CP(toce, toce32, starting_track);
160                 CP(toce, toce32, data_len);
161                 PTROUT_CP(toce, toce32, data);
162                 error = copyout(&toce32, uap->data, sizeof(toce32));
163         }
164         return error;
165 }
166
167 static int
168 freebsd32_ioctl_fiodgname(struct thread *td,
169     struct freebsd32_ioctl_args *uap, struct file *fp)
170 {
171         struct fiodgname_arg fgn;
172         struct fiodgname_arg32 fgn32;
173         int error;
174
175         if ((error = copyin(uap->data, &fgn32, sizeof fgn32)) != 0)
176                 return (error);
177         CP(fgn32, fgn, len);
178         PTRIN_CP(fgn32, fgn, buf);
179         error = fo_ioctl(fp, FIODGNAME, (caddr_t)&fgn, td->td_ucred, td);
180         return (error);
181 }
182
183 static int
184 freebsd32_ioctl_memrange(struct thread *td,
185     struct freebsd32_ioctl_args *uap, struct file *fp)
186 {
187         struct mem_range_op mro;
188         struct mem_range_op32 mro32;
189         int error;
190         u_long com;
191
192         if ((error = copyin(uap->data, &mro32, sizeof(mro32))) != 0)
193                 return (error);
194
195         PTRIN_CP(mro32, mro, mo_desc);
196         CP(mro32, mro, mo_arg[0]);
197         CP(mro32, mro, mo_arg[1]);
198
199         com = 0;
200         switch (uap->com) {
201         case MEMRANGE_GET32:
202                 com = MEMRANGE_GET;
203                 break;
204
205         case MEMRANGE_SET32:
206                 com = MEMRANGE_SET;
207                 break;
208
209         default:
210                 panic("%s: unknown MEMRANGE %#x", __func__, uap->com);
211         }
212
213         if ((error = fo_ioctl(fp, com, (caddr_t)&mro, td->td_ucred, td)) != 0)
214                 return (error);
215
216         if ( (com & IOC_OUT) ) {
217                 CP(mro, mro32, mo_arg[0]);
218                 CP(mro, mro32, mo_arg[1]);
219
220                 error = copyout(&mro32, uap->data, sizeof(mro32));
221         }
222
223         return (error);
224 }
225
226 static int
227 freebsd32_ioctl_pciocgetconf(struct thread *td,
228     struct freebsd32_ioctl_args *uap, struct file *fp)
229 {
230         struct pci_conf_io pci;
231         struct pci_conf_io32 pci32;
232         struct pci_match_conf32 pmc32;
233         struct pci_match_conf32 *pmc32p;
234         struct pci_match_conf pmc;
235         struct pci_match_conf *pmcp;
236         struct pci_conf32 pc32;
237         struct pci_conf32 *pc32p;
238         struct pci_conf pc;
239         struct pci_conf *pcp;
240         u_int32_t i;
241         u_int32_t npat_to_convert;
242         u_int32_t nmatch_to_convert;
243         vm_offset_t addr;
244         int error;
245
246         if ((error = copyin(uap->data, &pci32, sizeof(pci32))) != 0)
247                 return (error);
248
249         CP(pci32, pci, num_patterns);
250         CP(pci32, pci, offset);
251         CP(pci32, pci, generation);
252
253         npat_to_convert = pci32.pat_buf_len / sizeof(struct pci_match_conf32);
254         pci.pat_buf_len = npat_to_convert * sizeof(struct pci_match_conf);
255         pci.patterns = NULL;
256         nmatch_to_convert = pci32.match_buf_len / sizeof(struct pci_conf32);
257         pci.match_buf_len = nmatch_to_convert * sizeof(struct pci_conf);
258         pci.matches = NULL;
259
260         if ((error = copyout_map(td, &addr, pci.pat_buf_len)) != 0)
261                 goto cleanup;
262         pci.patterns = (struct pci_match_conf *)addr;
263         if ((error = copyout_map(td, &addr, pci.match_buf_len)) != 0)
264                 goto cleanup;
265         pci.matches = (struct pci_conf *)addr;
266
267         npat_to_convert = min(npat_to_convert, pci.num_patterns);
268
269         for (i = 0, pmc32p = (struct pci_match_conf32 *)PTRIN(pci32.patterns),
270              pmcp = pci.patterns;
271              i < npat_to_convert; i++, pmc32p++, pmcp++) {
272                 if ((error = copyin(pmc32p, &pmc32, sizeof(pmc32))) != 0)
273                         goto cleanup;
274                 CP(pmc32,pmc,pc_sel);
275                 strlcpy(pmc.pd_name, pmc32.pd_name, sizeof(pmc.pd_name));
276                 CP(pmc32,pmc,pd_unit);
277                 CP(pmc32,pmc,pc_vendor);
278                 CP(pmc32,pmc,pc_device);
279                 CP(pmc32,pmc,pc_class);
280                 CP(pmc32,pmc,flags);
281                 if ((error = copyout(&pmc, pmcp, sizeof(pmc))) != 0)
282                         goto cleanup;
283         }
284
285         if ((error = fo_ioctl(fp, PCIOCGETCONF, (caddr_t)&pci,
286                               td->td_ucred, td)) != 0)
287                 goto cleanup;
288
289         nmatch_to_convert = min(nmatch_to_convert, pci.num_matches);
290
291         for (i = 0, pcp = pci.matches,
292              pc32p = (struct pci_conf32 *)PTRIN(pci32.matches);
293              i < nmatch_to_convert; i++, pcp++, pc32p++) {
294                 if ((error = copyin(pcp, &pc, sizeof(pc))) != 0)
295                         goto cleanup;
296                 CP(pc,pc32,pc_sel);
297                 CP(pc,pc32,pc_hdr);
298                 CP(pc,pc32,pc_subvendor);
299                 CP(pc,pc32,pc_subdevice);
300                 CP(pc,pc32,pc_vendor);
301                 CP(pc,pc32,pc_device);
302                 CP(pc,pc32,pc_class);
303                 CP(pc,pc32,pc_subclass);
304                 CP(pc,pc32,pc_progif);
305                 CP(pc,pc32,pc_revid);
306                 strlcpy(pc32.pd_name, pc.pd_name, sizeof(pc32.pd_name));
307                 CP(pc,pc32,pd_unit);
308                 if ((error = copyout(&pc32, pc32p, sizeof(pc32))) != 0)
309                         goto cleanup;
310         }
311
312         CP(pci, pci32, num_matches);
313         CP(pci, pci32, offset);
314         CP(pci, pci32, generation);
315         CP(pci, pci32, status);
316
317         error = copyout(&pci32, uap->data, sizeof(pci32));
318
319 cleanup:
320         if (pci.patterns)
321                 copyout_unmap(td, (vm_offset_t)pci.patterns, pci.pat_buf_len);
322         if (pci.matches)
323                 copyout_unmap(td, (vm_offset_t)pci.matches, pci.match_buf_len);
324
325         return (error);
326 }
327
328 static int
329 freebsd32_ioctl_sg(struct thread *td,
330     struct freebsd32_ioctl_args *uap, struct file *fp)
331 {
332         struct sg_io_hdr io;
333         struct sg_io_hdr32 io32;
334         int error;
335
336         if ((error = copyin(uap->data, &io32, sizeof(io32))) != 0)
337                 return (error);
338
339         CP(io32, io, interface_id);
340         CP(io32, io, dxfer_direction);
341         CP(io32, io, cmd_len);
342         CP(io32, io, mx_sb_len);
343         CP(io32, io, iovec_count);
344         CP(io32, io, dxfer_len);
345         PTRIN_CP(io32, io, dxferp);
346         PTRIN_CP(io32, io, cmdp);
347         PTRIN_CP(io32, io, sbp);
348         CP(io32, io, timeout);
349         CP(io32, io, flags);
350         CP(io32, io, pack_id);
351         PTRIN_CP(io32, io, usr_ptr);
352         CP(io32, io, status);
353         CP(io32, io, masked_status);
354         CP(io32, io, msg_status);
355         CP(io32, io, sb_len_wr);
356         CP(io32, io, host_status);
357         CP(io32, io, driver_status);
358         CP(io32, io, resid);
359         CP(io32, io, duration);
360         CP(io32, io, info);
361
362         if ((error = fo_ioctl(fp, SG_IO, (caddr_t)&io, td->td_ucred, td)) != 0)
363                 return (error);
364
365         CP(io, io32, interface_id);
366         CP(io, io32, dxfer_direction);
367         CP(io, io32, cmd_len);
368         CP(io, io32, mx_sb_len);
369         CP(io, io32, iovec_count);
370         CP(io, io32, dxfer_len);
371         PTROUT_CP(io, io32, dxferp);
372         PTROUT_CP(io, io32, cmdp);
373         PTROUT_CP(io, io32, sbp);
374         CP(io, io32, timeout);
375         CP(io, io32, flags);
376         CP(io, io32, pack_id);
377         PTROUT_CP(io, io32, usr_ptr);
378         CP(io, io32, status);
379         CP(io, io32, masked_status);
380         CP(io, io32, msg_status);
381         CP(io, io32, sb_len_wr);
382         CP(io, io32, host_status);
383         CP(io, io32, driver_status);
384         CP(io, io32, resid);
385         CP(io, io32, duration);
386         CP(io, io32, info);
387
388         error = copyout(&io32, uap->data, sizeof(io32));
389
390         return (error);
391 }
392
393 int
394 freebsd32_ioctl(struct thread *td, struct freebsd32_ioctl_args *uap)
395 {
396         struct ioctl_args ap /*{
397                 int     fd;
398                 u_long  com;
399                 caddr_t data;
400         }*/ ;
401         struct file *fp;
402         cap_rights_t rights;
403         int error;
404
405         error = fget(td, uap->fd, cap_rights_init(&rights, CAP_IOCTL), &fp);
406         if (error != 0)
407                 return (error);
408         if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
409                 fdrop(fp, td);
410                 return (EBADF);
411         }
412
413         switch (uap->com) {
414         case MDIOCATTACH_32:    /* FALLTHROUGH */
415         case MDIOCDETACH_32:    /* FALLTHROUGH */
416         case MDIOCQUERY_32:     /* FALLTHROUGH */
417         case MDIOCLIST_32:
418                 error = freebsd32_ioctl_md(td, uap, fp);
419                 break;
420
421         case CDIOREADTOCENTRYS_32:
422                 error = freebsd32_ioctl_ioc_read_toc(td, uap, fp);
423                 break;
424
425         case FIODGNAME_32:
426                 error = freebsd32_ioctl_fiodgname(td, uap, fp);
427                 break;
428
429         case MEMRANGE_GET32:    /* FALLTHROUGH */
430         case MEMRANGE_SET32:
431                 error = freebsd32_ioctl_memrange(td, uap, fp);
432                 break;
433
434         case PCIOCGETCONF_32:
435                 error = freebsd32_ioctl_pciocgetconf(td, uap, fp);
436                 break;
437
438         case SG_IO_32:
439                 error = freebsd32_ioctl_sg(td, uap, fp);
440                 break;
441
442         default:
443                 fdrop(fp, td);
444                 ap.fd = uap->fd;
445                 ap.com = uap->com;
446                 PTRIN_CP(*uap, ap, data);
447                 return sys_ioctl(td, &ap);
448         }
449
450         fdrop(fp, td);
451         return error;
452 }