xref: /linux/drivers/gpu/drm/drm_ioc32.c (revision 1e525507)
1 /*
2  * \file drm_ioc32.c
3  *
4  * 32-bit ioctl compatibility routines for the DRM.
5  *
6  * \author Paul Mackerras <paulus@samba.org>
7  *
8  * Copyright (C) Paul Mackerras 2005.
9  * All Rights Reserved.
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice (including the next
19  * paragraph) shall be included in all copies or substantial portions of the
20  * Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
25  * THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28  * IN THE SOFTWARE.
29  */
30 #include <linux/compat.h>
31 #include <linux/ratelimit.h>
32 #include <linux/export.h>
33 
34 #include <drm/drm_device.h>
35 #include <drm/drm_file.h>
36 #include <drm/drm_print.h>
37 
38 #include "drm_crtc_internal.h"
39 #include "drm_internal.h"
40 
41 #define DRM_IOCTL_VERSION32		DRM_IOWR(0x00, drm_version32_t)
42 #define DRM_IOCTL_GET_UNIQUE32		DRM_IOWR(0x01, drm_unique32_t)
43 #define DRM_IOCTL_GET_MAP32		DRM_IOWR(0x04, drm_map32_t)
44 #define DRM_IOCTL_GET_CLIENT32		DRM_IOWR(0x05, drm_client32_t)
45 #define DRM_IOCTL_GET_STATS32		DRM_IOR( 0x06, drm_stats32_t)
46 
47 #define DRM_IOCTL_SET_UNIQUE32		DRM_IOW( 0x10, drm_unique32_t)
48 #define DRM_IOCTL_ADD_MAP32		DRM_IOWR(0x15, drm_map32_t)
49 #define DRM_IOCTL_ADD_BUFS32		DRM_IOWR(0x16, drm_buf_desc32_t)
50 #define DRM_IOCTL_MARK_BUFS32		DRM_IOW( 0x17, drm_buf_desc32_t)
51 #define DRM_IOCTL_INFO_BUFS32		DRM_IOWR(0x18, drm_buf_info32_t)
52 #define DRM_IOCTL_MAP_BUFS32		DRM_IOWR(0x19, drm_buf_map32_t)
53 #define DRM_IOCTL_FREE_BUFS32		DRM_IOW( 0x1a, drm_buf_free32_t)
54 
55 #define DRM_IOCTL_RM_MAP32		DRM_IOW( 0x1b, drm_map32_t)
56 
57 #define DRM_IOCTL_SET_SAREA_CTX32	DRM_IOW( 0x1c, drm_ctx_priv_map32_t)
58 #define DRM_IOCTL_GET_SAREA_CTX32	DRM_IOWR(0x1d, drm_ctx_priv_map32_t)
59 
60 #define DRM_IOCTL_RES_CTX32		DRM_IOWR(0x26, drm_ctx_res32_t)
61 #define DRM_IOCTL_DMA32			DRM_IOWR(0x29, drm_dma32_t)
62 
63 #define DRM_IOCTL_AGP_ENABLE32		DRM_IOW( 0x32, drm_agp_mode32_t)
64 #define DRM_IOCTL_AGP_INFO32		DRM_IOR( 0x33, drm_agp_info32_t)
65 #define DRM_IOCTL_AGP_ALLOC32		DRM_IOWR(0x34, drm_agp_buffer32_t)
66 #define DRM_IOCTL_AGP_FREE32		DRM_IOW( 0x35, drm_agp_buffer32_t)
67 #define DRM_IOCTL_AGP_BIND32		DRM_IOW( 0x36, drm_agp_binding32_t)
68 #define DRM_IOCTL_AGP_UNBIND32		DRM_IOW( 0x37, drm_agp_binding32_t)
69 
70 #define DRM_IOCTL_SG_ALLOC32		DRM_IOW( 0x38, drm_scatter_gather32_t)
71 #define DRM_IOCTL_SG_FREE32		DRM_IOW( 0x39, drm_scatter_gather32_t)
72 
73 #define DRM_IOCTL_UPDATE_DRAW32		DRM_IOW( 0x3f, drm_update_draw32_t)
74 
75 #define DRM_IOCTL_WAIT_VBLANK32		DRM_IOWR(0x3a, drm_wait_vblank32_t)
76 
77 #define DRM_IOCTL_MODE_ADDFB232		DRM_IOWR(0xb8, drm_mode_fb_cmd232_t)
78 
79 typedef struct drm_version_32 {
80 	int version_major;	  /* Major version */
81 	int version_minor;	  /* Minor version */
82 	int version_patchlevel;	   /* Patch level */
83 	u32 name_len;		  /* Length of name buffer */
84 	u32 name;		  /* Name of driver */
85 	u32 date_len;		  /* Length of date buffer */
86 	u32 date;		  /* User-space buffer to hold date */
87 	u32 desc_len;		  /* Length of desc buffer */
88 	u32 desc;		  /* User-space buffer to hold desc */
89 } drm_version32_t;
90 
91 static int compat_drm_version(struct file *file, unsigned int cmd,
92 			      unsigned long arg)
93 {
94 	drm_version32_t v32;
95 	struct drm_version v;
96 	int err;
97 
98 	if (copy_from_user(&v32, (void __user *)arg, sizeof(v32)))
99 		return -EFAULT;
100 
101 	memset(&v, 0, sizeof(v));
102 
103 	v = (struct drm_version) {
104 		.name_len = v32.name_len,
105 		.name = compat_ptr(v32.name),
106 		.date_len = v32.date_len,
107 		.date = compat_ptr(v32.date),
108 		.desc_len = v32.desc_len,
109 		.desc = compat_ptr(v32.desc),
110 	};
111 	err = drm_ioctl_kernel(file, drm_version, &v,
112 			       DRM_RENDER_ALLOW);
113 	if (err)
114 		return err;
115 
116 	v32.version_major = v.version_major;
117 	v32.version_minor = v.version_minor;
118 	v32.version_patchlevel = v.version_patchlevel;
119 	v32.name_len = v.name_len;
120 	v32.date_len = v.date_len;
121 	v32.desc_len = v.desc_len;
122 	if (copy_to_user((void __user *)arg, &v32, sizeof(v32)))
123 		return -EFAULT;
124 	return 0;
125 }
126 
127 typedef struct drm_unique32 {
128 	u32 unique_len;	/* Length of unique */
129 	u32 unique;	/* Unique name for driver instantiation */
130 } drm_unique32_t;
131 
132 static int compat_drm_getunique(struct file *file, unsigned int cmd,
133 				unsigned long arg)
134 {
135 	drm_unique32_t uq32;
136 	struct drm_unique uq;
137 	int err;
138 
139 	if (copy_from_user(&uq32, (void __user *)arg, sizeof(uq32)))
140 		return -EFAULT;
141 
142 	memset(&uq, 0, sizeof(uq));
143 
144 	uq = (struct drm_unique){
145 		.unique_len = uq32.unique_len,
146 		.unique = compat_ptr(uq32.unique),
147 	};
148 
149 	err = drm_ioctl_kernel(file, drm_getunique, &uq, 0);
150 	if (err)
151 		return err;
152 
153 	uq32.unique_len = uq.unique_len;
154 	if (copy_to_user((void __user *)arg, &uq32, sizeof(uq32)))
155 		return -EFAULT;
156 	return 0;
157 }
158 
159 static int compat_drm_setunique(struct file *file, unsigned int cmd,
160 				unsigned long arg)
161 {
162 	/* it's dead */
163 	return -EINVAL;
164 }
165 
166 typedef struct drm_client32 {
167 	int idx;	/* Which client desired? */
168 	int auth;	/* Is client authenticated? */
169 	u32 pid;	/* Process ID */
170 	u32 uid;	/* User ID */
171 	u32 magic;	/* Magic */
172 	u32 iocs;	/* Ioctl count */
173 } drm_client32_t;
174 
175 static int compat_drm_getclient(struct file *file, unsigned int cmd,
176 				unsigned long arg)
177 {
178 	drm_client32_t c32;
179 	drm_client32_t __user *argp = (void __user *)arg;
180 	struct drm_client client;
181 	int err;
182 
183 	if (copy_from_user(&c32, argp, sizeof(c32)))
184 		return -EFAULT;
185 
186 	memset(&client, 0, sizeof(client));
187 
188 	client.idx = c32.idx;
189 
190 	err = drm_ioctl_kernel(file, drm_getclient, &client, 0);
191 	if (err)
192 		return err;
193 
194 	c32.idx = client.idx;
195 	c32.auth = client.auth;
196 	c32.pid = client.pid;
197 	c32.uid = client.uid;
198 	c32.magic = client.magic;
199 	c32.iocs = client.iocs;
200 
201 	if (copy_to_user(argp, &c32, sizeof(c32)))
202 		return -EFAULT;
203 	return 0;
204 }
205 
206 typedef struct drm_stats32 {
207 	u32 count;
208 	struct {
209 		u32 value;
210 		enum drm_stat_type type;
211 	} data[15];
212 } drm_stats32_t;
213 
214 static int compat_drm_getstats(struct file *file, unsigned int cmd,
215 			       unsigned long arg)
216 {
217 	drm_stats32_t __user *argp = (void __user *)arg;
218 
219 	/* getstats is defunct, just clear */
220 	if (clear_user(argp, sizeof(drm_stats32_t)))
221 		return -EFAULT;
222 	return 0;
223 }
224 
225 #if defined(CONFIG_X86)
226 typedef struct drm_update_draw32 {
227 	drm_drawable_t handle;
228 	unsigned int type;
229 	unsigned int num;
230 	/* 64-bit version has a 32-bit pad here */
231 	u64 data;	/**< Pointer */
232 } __packed drm_update_draw32_t;
233 
234 static int compat_drm_update_draw(struct file *file, unsigned int cmd,
235 				  unsigned long arg)
236 {
237 	/* update_draw is defunct */
238 	return 0;
239 }
240 #endif
241 
242 struct drm_wait_vblank_request32 {
243 	enum drm_vblank_seq_type type;
244 	unsigned int sequence;
245 	u32 signal;
246 };
247 
248 struct drm_wait_vblank_reply32 {
249 	enum drm_vblank_seq_type type;
250 	unsigned int sequence;
251 	s32 tval_sec;
252 	s32 tval_usec;
253 };
254 
255 typedef union drm_wait_vblank32 {
256 	struct drm_wait_vblank_request32 request;
257 	struct drm_wait_vblank_reply32 reply;
258 } drm_wait_vblank32_t;
259 
260 static int compat_drm_wait_vblank(struct file *file, unsigned int cmd,
261 				  unsigned long arg)
262 {
263 	drm_wait_vblank32_t __user *argp = (void __user *)arg;
264 	drm_wait_vblank32_t req32;
265 	union drm_wait_vblank req;
266 	int err;
267 
268 	if (copy_from_user(&req32, argp, sizeof(req32)))
269 		return -EFAULT;
270 
271 	memset(&req, 0, sizeof(req));
272 
273 	req.request.type = req32.request.type;
274 	req.request.sequence = req32.request.sequence;
275 	req.request.signal = req32.request.signal;
276 	err = drm_ioctl_kernel(file, drm_wait_vblank_ioctl, &req, 0);
277 
278 	req32.reply.type = req.reply.type;
279 	req32.reply.sequence = req.reply.sequence;
280 	req32.reply.tval_sec = req.reply.tval_sec;
281 	req32.reply.tval_usec = req.reply.tval_usec;
282 	if (copy_to_user(argp, &req32, sizeof(req32)))
283 		return -EFAULT;
284 
285 	return err;
286 }
287 
288 #if defined(CONFIG_X86)
289 typedef struct drm_mode_fb_cmd232 {
290 	u32 fb_id;
291 	u32 width;
292 	u32 height;
293 	u32 pixel_format;
294 	u32 flags;
295 	u32 handles[4];
296 	u32 pitches[4];
297 	u32 offsets[4];
298 	u64 modifier[4];
299 } __packed drm_mode_fb_cmd232_t;
300 
301 static int compat_drm_mode_addfb2(struct file *file, unsigned int cmd,
302 				  unsigned long arg)
303 {
304 	struct drm_mode_fb_cmd232 __user *argp = (void __user *)arg;
305 	struct drm_mode_fb_cmd2 req64;
306 	int err;
307 
308 	memset(&req64, 0, sizeof(req64));
309 
310 	if (copy_from_user(&req64, argp,
311 			   offsetof(drm_mode_fb_cmd232_t, modifier)))
312 		return -EFAULT;
313 
314 	if (copy_from_user(&req64.modifier, &argp->modifier,
315 			   sizeof(req64.modifier)))
316 		return -EFAULT;
317 
318 	err = drm_ioctl_kernel(file, drm_mode_addfb2, &req64, 0);
319 	if (err)
320 		return err;
321 
322 	if (put_user(req64.fb_id, &argp->fb_id))
323 		return -EFAULT;
324 
325 	return 0;
326 }
327 #endif
328 
329 static struct {
330 	drm_ioctl_compat_t *fn;
331 	char *name;
332 } drm_compat_ioctls[] = {
333 #define DRM_IOCTL32_DEF(n, f) [DRM_IOCTL_NR(n##32)] = {.fn = f, .name = #n}
334 	DRM_IOCTL32_DEF(DRM_IOCTL_VERSION, compat_drm_version),
335 	DRM_IOCTL32_DEF(DRM_IOCTL_GET_UNIQUE, compat_drm_getunique),
336 	DRM_IOCTL32_DEF(DRM_IOCTL_GET_CLIENT, compat_drm_getclient),
337 	DRM_IOCTL32_DEF(DRM_IOCTL_GET_STATS, compat_drm_getstats),
338 	DRM_IOCTL32_DEF(DRM_IOCTL_SET_UNIQUE, compat_drm_setunique),
339 #if defined(CONFIG_X86)
340 	DRM_IOCTL32_DEF(DRM_IOCTL_UPDATE_DRAW, compat_drm_update_draw),
341 #endif
342 	DRM_IOCTL32_DEF(DRM_IOCTL_WAIT_VBLANK, compat_drm_wait_vblank),
343 #if defined(CONFIG_X86)
344 	DRM_IOCTL32_DEF(DRM_IOCTL_MODE_ADDFB2, compat_drm_mode_addfb2),
345 #endif
346 };
347 
348 /**
349  * drm_compat_ioctl - 32bit IOCTL compatibility handler for DRM drivers
350  * @filp: file this ioctl is called on
351  * @cmd: ioctl cmd number
352  * @arg: user argument
353  *
354  * Compatibility handler for 32 bit userspace running on 64 kernels. All actual
355  * IOCTL handling is forwarded to drm_ioctl(), while marshalling structures as
356  * appropriate. Note that this only handles DRM core IOCTLs, if the driver has
357  * botched IOCTL itself, it must handle those by wrapping this function.
358  *
359  * Returns:
360  * Zero on success, negative error code on failure.
361  */
362 long drm_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
363 {
364 	unsigned int nr = DRM_IOCTL_NR(cmd);
365 	struct drm_file *file_priv = filp->private_data;
366 	struct drm_device *dev = file_priv->minor->dev;
367 	drm_ioctl_compat_t *fn;
368 	int ret;
369 
370 	/* Assume that ioctls without an explicit compat routine will just
371 	 * work.  This may not always be a good assumption, but it's better
372 	 * than always failing.
373 	 */
374 	if (nr >= ARRAY_SIZE(drm_compat_ioctls))
375 		return drm_ioctl(filp, cmd, arg);
376 
377 	fn = drm_compat_ioctls[nr].fn;
378 	if (!fn)
379 		return drm_ioctl(filp, cmd, arg);
380 
381 	drm_dbg_core(dev, "comm=\"%s\", pid=%d, dev=0x%lx, auth=%d, %s\n",
382 		     current->comm, task_pid_nr(current),
383 		     (long)old_encode_dev(file_priv->minor->kdev->devt),
384 		     file_priv->authenticated,
385 		     drm_compat_ioctls[nr].name);
386 	ret = (*fn)(filp, cmd, arg);
387 	if (ret)
388 		drm_dbg_core(dev, "ret = %d\n", ret);
389 	return ret;
390 }
391 EXPORT_SYMBOL(drm_compat_ioctl);
392