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