xref: /freebsd/sys/compat/freebsd32/freebsd32_ioctl.c (revision 5b5a48c787b8a4655e9782bd8ac392a79aeae1a6)
1ae528485SDavid E. O'Brien /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4ae528485SDavid E. O'Brien  * Copyright (c) 2008 David E. O'Brien
5ae528485SDavid E. O'Brien  * All rights reserved.
6ae528485SDavid E. O'Brien  *
7ae528485SDavid E. O'Brien  * Redistribution and use in source and binary forms, with or without
8ae528485SDavid E. O'Brien  * modification, are permitted provided that the following conditions
9ae528485SDavid E. O'Brien  * are met:
10ae528485SDavid E. O'Brien  * 1. Redistributions of source code must retain the above copyright
11ae528485SDavid E. O'Brien  *    notice, this list of conditions and the following disclaimer.
12ae528485SDavid E. O'Brien  * 2. Redistributions in binary form must reproduce the above copyright
13ae528485SDavid E. O'Brien  *    notice, this list of conditions and the following disclaimer in the
14ae528485SDavid E. O'Brien  *    documentation and/or other materials provided with the distribution.
15ae528485SDavid E. O'Brien  * 3. Neither the name of the author nor the names of its contributors
16ae528485SDavid E. O'Brien  *    may be used to endorse or promote products derived from this software
17ae528485SDavid E. O'Brien  *    without specific prior written permission.
18ae528485SDavid E. O'Brien  *
19ae528485SDavid E. O'Brien  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20ae528485SDavid E. O'Brien  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21ae528485SDavid E. O'Brien  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22ae528485SDavid E. O'Brien  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23ae528485SDavid E. O'Brien  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24ae528485SDavid E. O'Brien  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25ae528485SDavid E. O'Brien  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26ae528485SDavid E. O'Brien  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27ae528485SDavid E. O'Brien  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28ae528485SDavid E. O'Brien  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29ae528485SDavid E. O'Brien  * SUCH DAMAGE.
30ae528485SDavid E. O'Brien  */
31ae528485SDavid E. O'Brien 
32ae528485SDavid E. O'Brien #include <sys/cdefs.h>
33ae528485SDavid E. O'Brien __FBSDID("$FreeBSD$");
34ae528485SDavid E. O'Brien 
35ae528485SDavid E. O'Brien #include <sys/param.h>
364a144410SRobert Watson #include <sys/capsicum.h>
37ae528485SDavid E. O'Brien #include <sys/cdio.h>
38ae528485SDavid E. O'Brien #include <sys/fcntl.h>
397eac36edSEd Schouten #include <sys/filio.h>
40ae528485SDavid E. O'Brien #include <sys/file.h>
41ae528485SDavid E. O'Brien #include <sys/ioccom.h>
42bfac1583SKonstantin Belousov #include <sys/malloc.h>
43bfac1583SKonstantin Belousov #include <sys/memrange.h>
444ee107ddSKonstantin Belousov #include <sys/pciio.h>
45ae528485SDavid E. O'Brien #include <sys/proc.h>
46ae528485SDavid E. O'Brien #include <sys/syscall.h>
47ae528485SDavid E. O'Brien #include <sys/syscallsubr.h>
48ae528485SDavid E. O'Brien #include <sys/sysctl.h>
49ae528485SDavid E. O'Brien #include <sys/sysent.h>
50ae528485SDavid E. O'Brien #include <sys/sysproto.h>
51ae528485SDavid E. O'Brien #include <sys/systm.h>
5234a77b97SBrooks Davis #include <sys/uio.h>
53ae528485SDavid E. O'Brien 
54ae528485SDavid E. O'Brien #include <compat/freebsd32/freebsd32.h>
55ae528485SDavid E. O'Brien #include <compat/freebsd32/freebsd32_ioctl.h>
5687842989SKonstantin Belousov #include <compat/freebsd32/freebsd32_misc.h>
57ae528485SDavid E. O'Brien #include <compat/freebsd32/freebsd32_proto.h>
58ae528485SDavid E. O'Brien 
59bfac1583SKonstantin Belousov CTASSERT(sizeof(struct mem_range_op32) == 12);
60ae528485SDavid E. O'Brien 
61c750e17cSDavid E. O'Brien static int
62bfac1583SKonstantin Belousov freebsd32_ioctl_memrange(struct thread *td,
63bfac1583SKonstantin Belousov     struct freebsd32_ioctl_args *uap, struct file *fp)
64bfac1583SKonstantin Belousov {
65bfac1583SKonstantin Belousov 	struct mem_range_op mro;
66bfac1583SKonstantin Belousov 	struct mem_range_op32 mro32;
67bfac1583SKonstantin Belousov 	int error;
68bfac1583SKonstantin Belousov 	u_long com;
69bfac1583SKonstantin Belousov 
70bfac1583SKonstantin Belousov 	if ((error = copyin(uap->data, &mro32, sizeof(mro32))) != 0)
71bfac1583SKonstantin Belousov 		return (error);
72bfac1583SKonstantin Belousov 
73bfac1583SKonstantin Belousov 	PTRIN_CP(mro32, mro, mo_desc);
74bfac1583SKonstantin Belousov 	CP(mro32, mro, mo_arg[0]);
75bfac1583SKonstantin Belousov 	CP(mro32, mro, mo_arg[1]);
76bfac1583SKonstantin Belousov 
77*5b5a48c7SBrooks Davis 	com = _IOC_NEWTYPE(uap->com, struct mem_range_op);
78bfac1583SKonstantin Belousov 
79bfac1583SKonstantin Belousov 	if ((error = fo_ioctl(fp, com, (caddr_t)&mro, td->td_ucred, td)) != 0)
80bfac1583SKonstantin Belousov 		return (error);
81bfac1583SKonstantin Belousov 
82bfac1583SKonstantin Belousov 	if ( (com & IOC_OUT) ) {
83bfac1583SKonstantin Belousov 		CP(mro, mro32, mo_arg[0]);
84bfac1583SKonstantin Belousov 		CP(mro, mro32, mo_arg[1]);
85bfac1583SKonstantin Belousov 
86bfac1583SKonstantin Belousov 		error = copyout(&mro32, uap->data, sizeof(mro32));
87bfac1583SKonstantin Belousov 	}
88bfac1583SKonstantin Belousov 
89bfac1583SKonstantin Belousov 	return (error);
90bfac1583SKonstantin Belousov }
91bfac1583SKonstantin Belousov 
9278272eedSKonstantin Belousov static int
9387842989SKonstantin Belousov freebsd32_ioctl_barmmap(struct thread *td,
9487842989SKonstantin Belousov     struct freebsd32_ioctl_args *uap, struct file *fp)
9587842989SKonstantin Belousov {
9687842989SKonstantin Belousov 	struct pci_bar_mmap32 pbm32;
9787842989SKonstantin Belousov 	struct pci_bar_mmap pbm;
9887842989SKonstantin Belousov 	int error;
9987842989SKonstantin Belousov 
10087842989SKonstantin Belousov 	error = copyin(uap->data, &pbm32, sizeof(pbm32));
10187842989SKonstantin Belousov 	if (error != 0)
10287842989SKonstantin Belousov 		return (error);
10387842989SKonstantin Belousov 	PTRIN_CP(pbm32, pbm, pbm_map_base);
10487842989SKonstantin Belousov 	CP(pbm32, pbm, pbm_sel);
10587842989SKonstantin Belousov 	CP(pbm32, pbm, pbm_reg);
10687842989SKonstantin Belousov 	CP(pbm32, pbm, pbm_flags);
10787842989SKonstantin Belousov 	CP(pbm32, pbm, pbm_memattr);
10887842989SKonstantin Belousov 	pbm.pbm_bar_length = PAIR32TO64(uint64_t, pbm32.pbm_bar_length);
10987842989SKonstantin Belousov 	error = fo_ioctl(fp, PCIOCBARMMAP, (caddr_t)&pbm, td->td_ucred, td);
11087842989SKonstantin Belousov 	if (error == 0) {
11187842989SKonstantin Belousov 		PTROUT_CP(pbm, pbm32, pbm_map_base);
11287842989SKonstantin Belousov 		CP(pbm, pbm32, pbm_map_length);
11387842989SKonstantin Belousov #if BYTE_ORDER == LITTLE_ENDIAN
11487842989SKonstantin Belousov 		pbm32.pbm_bar_length1 = pbm.pbm_bar_length;
11587842989SKonstantin Belousov 		pbm32.pbm_bar_length2 = pbm.pbm_bar_length >> 32;
11687842989SKonstantin Belousov #else
11787842989SKonstantin Belousov 		pbm32.pbm_bar_length1 = pbm.pbm_bar_length >> 32;
11887842989SKonstantin Belousov 		pbm32.pbm_bar_length2 = pbm.pbm_bar_length;
11987842989SKonstantin Belousov #endif
12087842989SKonstantin Belousov 		CP(pbm, pbm32, pbm_bar_off);
12187842989SKonstantin Belousov 		error = copyout(&pbm32, uap->data, sizeof(pbm32));
12287842989SKonstantin Belousov 	}
12387842989SKonstantin Belousov 	return (error);
12487842989SKonstantin Belousov }
12587842989SKonstantin Belousov 
12687842989SKonstantin Belousov static int
127fcaf473cSAlexander Motin freebsd32_ioctl_sg(struct thread *td,
128fcaf473cSAlexander Motin     struct freebsd32_ioctl_args *uap, struct file *fp)
129fcaf473cSAlexander Motin {
130fcaf473cSAlexander Motin 	struct sg_io_hdr io;
131fcaf473cSAlexander Motin 	struct sg_io_hdr32 io32;
132fcaf473cSAlexander Motin 	int error;
133fcaf473cSAlexander Motin 
134fcaf473cSAlexander Motin 	if ((error = copyin(uap->data, &io32, sizeof(io32))) != 0)
135fcaf473cSAlexander Motin 		return (error);
136fcaf473cSAlexander Motin 
137fcaf473cSAlexander Motin 	CP(io32, io, interface_id);
138fcaf473cSAlexander Motin 	CP(io32, io, dxfer_direction);
139fcaf473cSAlexander Motin 	CP(io32, io, cmd_len);
140fcaf473cSAlexander Motin 	CP(io32, io, mx_sb_len);
141fcaf473cSAlexander Motin 	CP(io32, io, iovec_count);
142fcaf473cSAlexander Motin 	CP(io32, io, dxfer_len);
143fcaf473cSAlexander Motin 	PTRIN_CP(io32, io, dxferp);
144fcaf473cSAlexander Motin 	PTRIN_CP(io32, io, cmdp);
145fcaf473cSAlexander Motin 	PTRIN_CP(io32, io, sbp);
146fcaf473cSAlexander Motin 	CP(io32, io, timeout);
147fcaf473cSAlexander Motin 	CP(io32, io, flags);
148fcaf473cSAlexander Motin 	CP(io32, io, pack_id);
149fcaf473cSAlexander Motin 	PTRIN_CP(io32, io, usr_ptr);
150fcaf473cSAlexander Motin 	CP(io32, io, status);
151fcaf473cSAlexander Motin 	CP(io32, io, masked_status);
152fcaf473cSAlexander Motin 	CP(io32, io, msg_status);
153fcaf473cSAlexander Motin 	CP(io32, io, sb_len_wr);
154fcaf473cSAlexander Motin 	CP(io32, io, host_status);
155fcaf473cSAlexander Motin 	CP(io32, io, driver_status);
156fcaf473cSAlexander Motin 	CP(io32, io, resid);
157fcaf473cSAlexander Motin 	CP(io32, io, duration);
158fcaf473cSAlexander Motin 	CP(io32, io, info);
159fcaf473cSAlexander Motin 
160fcaf473cSAlexander Motin 	if ((error = fo_ioctl(fp, SG_IO, (caddr_t)&io, td->td_ucred, td)) != 0)
161fcaf473cSAlexander Motin 		return (error);
162fcaf473cSAlexander Motin 
163fcaf473cSAlexander Motin 	CP(io, io32, interface_id);
164fcaf473cSAlexander Motin 	CP(io, io32, dxfer_direction);
165fcaf473cSAlexander Motin 	CP(io, io32, cmd_len);
166fcaf473cSAlexander Motin 	CP(io, io32, mx_sb_len);
167fcaf473cSAlexander Motin 	CP(io, io32, iovec_count);
168fcaf473cSAlexander Motin 	CP(io, io32, dxfer_len);
169fcaf473cSAlexander Motin 	PTROUT_CP(io, io32, dxferp);
170fcaf473cSAlexander Motin 	PTROUT_CP(io, io32, cmdp);
171fcaf473cSAlexander Motin 	PTROUT_CP(io, io32, sbp);
172fcaf473cSAlexander Motin 	CP(io, io32, timeout);
173fcaf473cSAlexander Motin 	CP(io, io32, flags);
174fcaf473cSAlexander Motin 	CP(io, io32, pack_id);
175fcaf473cSAlexander Motin 	PTROUT_CP(io, io32, usr_ptr);
176fcaf473cSAlexander Motin 	CP(io, io32, status);
177fcaf473cSAlexander Motin 	CP(io, io32, masked_status);
178fcaf473cSAlexander Motin 	CP(io, io32, msg_status);
179fcaf473cSAlexander Motin 	CP(io, io32, sb_len_wr);
180fcaf473cSAlexander Motin 	CP(io, io32, host_status);
181fcaf473cSAlexander Motin 	CP(io, io32, driver_status);
182fcaf473cSAlexander Motin 	CP(io, io32, resid);
183fcaf473cSAlexander Motin 	CP(io, io32, duration);
184fcaf473cSAlexander Motin 	CP(io, io32, info);
185fcaf473cSAlexander Motin 
186fcaf473cSAlexander Motin 	error = copyout(&io32, uap->data, sizeof(io32));
187fcaf473cSAlexander Motin 
188fcaf473cSAlexander Motin 	return (error);
189fcaf473cSAlexander Motin }
190fcaf473cSAlexander Motin 
191ae528485SDavid E. O'Brien int
192ae528485SDavid E. O'Brien freebsd32_ioctl(struct thread *td, struct freebsd32_ioctl_args *uap)
193ae528485SDavid E. O'Brien {
194ae528485SDavid E. O'Brien 	struct ioctl_args ap /*{
195ae528485SDavid E. O'Brien 		int	fd;
196ae528485SDavid E. O'Brien 		u_long	com;
197ae528485SDavid E. O'Brien 		caddr_t	data;
198ae528485SDavid E. O'Brien 	}*/ ;
199ae528485SDavid E. O'Brien 	struct file *fp;
2007008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
201ae528485SDavid E. O'Brien 	int error;
202ae528485SDavid E. O'Brien 
2036b3a9a0fSMateusz Guzik 	error = fget(td, uap->fd, cap_rights_init_one(&rights, CAP_IOCTL), &fp);
2047008be5bSPawel Jakub Dawidek 	if (error != 0)
205ae528485SDavid E. O'Brien 		return (error);
206ae528485SDavid E. O'Brien 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
207ae528485SDavid E. O'Brien 		fdrop(fp, td);
208ae528485SDavid E. O'Brien 		return (EBADF);
209ae528485SDavid E. O'Brien 	}
210ae528485SDavid E. O'Brien 
211ae528485SDavid E. O'Brien 	switch (uap->com) {
212bfac1583SKonstantin Belousov 	case MEMRANGE_GET32:	/* FALLTHROUGH */
213bfac1583SKonstantin Belousov 	case MEMRANGE_SET32:
214bfac1583SKonstantin Belousov 		error = freebsd32_ioctl_memrange(td, uap, fp);
215bfac1583SKonstantin Belousov 		break;
216bfac1583SKonstantin Belousov 
217fcaf473cSAlexander Motin 	case SG_IO_32:
218fcaf473cSAlexander Motin 		error = freebsd32_ioctl_sg(td, uap, fp);
219fcaf473cSAlexander Motin 		break;
220fcaf473cSAlexander Motin 
22187842989SKonstantin Belousov 	case PCIOCBARMMAP_32:
22287842989SKonstantin Belousov 		error = freebsd32_ioctl_barmmap(td, uap, fp);
22387842989SKonstantin Belousov 		break;
22487842989SKonstantin Belousov 
225ae528485SDavid E. O'Brien 	default:
226ae528485SDavid E. O'Brien 		fdrop(fp, td);
227ae528485SDavid E. O'Brien 		ap.fd = uap->fd;
228ae528485SDavid E. O'Brien 		ap.com = uap->com;
229ae528485SDavid E. O'Brien 		PTRIN_CP(*uap, ap, data);
2308451d0ddSKip Macy 		return sys_ioctl(td, &ap);
231ae528485SDavid E. O'Brien 	}
2321e67ebb1SKonstantin Belousov 
2331e67ebb1SKonstantin Belousov 	fdrop(fp, td);
23487842989SKonstantin Belousov 	return (error);
235ae528485SDavid E. O'Brien }
236