blob: 0be62f20e524ec1082cda0753cdef1ed539ba677 [file] [log] [blame]
Daniel Vetter26409812017-04-04 11:52:57 +02001/*
Dave Airlie9a186642005-06-23 21:29:18 +10002 * \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>
Christian Dietrichcafe8d82011-06-04 15:36:43 +000031#include <linux/ratelimit.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040032#include <linux/export.h>
Dave Airlie9a186642005-06-23 21:29:18 +100033
David Howells760285e2012-10-02 18:01:07 +010034#include <drm/drmP.h>
Al Viroff22ff92017-05-24 13:42:27 -040035#include "drm_legacy.h"
Al Viro012c6742017-05-24 14:11:03 -040036#include "drm_internal.h"
Dave Airlie9a186642005-06-23 21:29:18 +100037
38#define DRM_IOCTL_VERSION32 DRM_IOWR(0x00, drm_version32_t)
39#define DRM_IOCTL_GET_UNIQUE32 DRM_IOWR(0x01, drm_unique32_t)
40#define DRM_IOCTL_GET_MAP32 DRM_IOWR(0x04, drm_map32_t)
41#define DRM_IOCTL_GET_CLIENT32 DRM_IOWR(0x05, drm_client32_t)
42#define DRM_IOCTL_GET_STATS32 DRM_IOR( 0x06, drm_stats32_t)
43
44#define DRM_IOCTL_SET_UNIQUE32 DRM_IOW( 0x10, drm_unique32_t)
45#define DRM_IOCTL_ADD_MAP32 DRM_IOWR(0x15, drm_map32_t)
46#define DRM_IOCTL_ADD_BUFS32 DRM_IOWR(0x16, drm_buf_desc32_t)
47#define DRM_IOCTL_MARK_BUFS32 DRM_IOW( 0x17, drm_buf_desc32_t)
48#define DRM_IOCTL_INFO_BUFS32 DRM_IOWR(0x18, drm_buf_info32_t)
49#define DRM_IOCTL_MAP_BUFS32 DRM_IOWR(0x19, drm_buf_map32_t)
50#define DRM_IOCTL_FREE_BUFS32 DRM_IOW( 0x1a, drm_buf_free32_t)
51
52#define DRM_IOCTL_RM_MAP32 DRM_IOW( 0x1b, drm_map32_t)
53
54#define DRM_IOCTL_SET_SAREA_CTX32 DRM_IOW( 0x1c, drm_ctx_priv_map32_t)
55#define DRM_IOCTL_GET_SAREA_CTX32 DRM_IOWR(0x1d, drm_ctx_priv_map32_t)
56
57#define DRM_IOCTL_RES_CTX32 DRM_IOWR(0x26, drm_ctx_res32_t)
58#define DRM_IOCTL_DMA32 DRM_IOWR(0x29, drm_dma32_t)
59
60#define DRM_IOCTL_AGP_ENABLE32 DRM_IOW( 0x32, drm_agp_mode32_t)
61#define DRM_IOCTL_AGP_INFO32 DRM_IOR( 0x33, drm_agp_info32_t)
62#define DRM_IOCTL_AGP_ALLOC32 DRM_IOWR(0x34, drm_agp_buffer32_t)
63#define DRM_IOCTL_AGP_FREE32 DRM_IOW( 0x35, drm_agp_buffer32_t)
64#define DRM_IOCTL_AGP_BIND32 DRM_IOW( 0x36, drm_agp_binding32_t)
65#define DRM_IOCTL_AGP_UNBIND32 DRM_IOW( 0x37, drm_agp_binding32_t)
66
67#define DRM_IOCTL_SG_ALLOC32 DRM_IOW( 0x38, drm_scatter_gather32_t)
68#define DRM_IOCTL_SG_FREE32 DRM_IOW( 0x39, drm_scatter_gather32_t)
69
Eric Anholt49568872008-10-21 11:38:50 -070070#define DRM_IOCTL_UPDATE_DRAW32 DRM_IOW( 0x3f, drm_update_draw32_t)
71
Dave Airlie9a186642005-06-23 21:29:18 +100072#define DRM_IOCTL_WAIT_VBLANK32 DRM_IOWR(0x3a, drm_wait_vblank32_t)
73
Tvrtko Ursulinc631d5f2015-07-14 11:13:08 +010074#define DRM_IOCTL_MODE_ADDFB232 DRM_IOWR(0xb8, drm_mode_fb_cmd232_t)
75
Dave Airlie9a186642005-06-23 21:29:18 +100076typedef struct drm_version_32 {
Daniel Vetter26409812017-04-04 11:52:57 +020077 int version_major; /* Major version */
78 int version_minor; /* Minor version */
79 int version_patchlevel; /* Patch level */
80 u32 name_len; /* Length of name buffer */
81 u32 name; /* Name of driver */
82 u32 date_len; /* Length of date buffer */
83 u32 date; /* User-space buffer to hold date */
84 u32 desc_len; /* Length of desc buffer */
85 u32 desc; /* User-space buffer to hold desc */
Dave Airlie9a186642005-06-23 21:29:18 +100086} drm_version32_t;
87
88static int compat_drm_version(struct file *file, unsigned int cmd,
89 unsigned long arg)
90{
91 drm_version32_t v32;
Al Viro012c6742017-05-24 14:11:03 -040092 struct drm_version v;
Dave Airlie9a186642005-06-23 21:29:18 +100093 int err;
94
Dave Airlieb5e89ed2005-09-25 14:28:13 +100095 if (copy_from_user(&v32, (void __user *)arg, sizeof(v32)))
Dave Airlie9a186642005-06-23 21:29:18 +100096 return -EFAULT;
97
Al Viro012c6742017-05-24 14:11:03 -040098 v = (struct drm_version) {
99 .name_len = v32.name_len,
100 .name = compat_ptr(v32.name),
101 .date_len = v32.date_len,
102 .date = compat_ptr(v32.date),
103 .desc_len = v32.desc_len,
104 .desc = compat_ptr(v32.desc),
105 };
106 err = drm_ioctl_kernel(file, drm_version, &v,
107 DRM_UNLOCKED|DRM_RENDER_ALLOW|DRM_CONTROL_ALLOW);
Dave Airlie9a186642005-06-23 21:29:18 +1000108 if (err)
109 return err;
110
Al Viro012c6742017-05-24 14:11:03 -0400111 v32.version_major = v.version_major;
112 v32.version_minor = v.version_minor;
113 v32.version_patchlevel = v.version_patchlevel;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000114 if (copy_to_user((void __user *)arg, &v32, sizeof(v32)))
Dave Airlie9a186642005-06-23 21:29:18 +1000115 return -EFAULT;
116 return 0;
117}
118
119typedef struct drm_unique32 {
Daniel Vetter26409812017-04-04 11:52:57 +0200120 u32 unique_len; /* Length of unique */
121 u32 unique; /* Unique name for driver instantiation */
Dave Airlie9a186642005-06-23 21:29:18 +1000122} drm_unique32_t;
123
124static int compat_drm_getunique(struct file *file, unsigned int cmd,
125 unsigned long arg)
126{
127 drm_unique32_t uq32;
Al Viro17e3dad2017-05-24 14:15:20 -0400128 struct drm_unique uq;
Dave Airlie9a186642005-06-23 21:29:18 +1000129 int err;
130
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000131 if (copy_from_user(&uq32, (void __user *)arg, sizeof(uq32)))
Dave Airlie9a186642005-06-23 21:29:18 +1000132 return -EFAULT;
Al Viro17e3dad2017-05-24 14:15:20 -0400133 uq = (struct drm_unique){
134 .unique_len = uq32.unique_len,
135 .unique = compat_ptr(uq32.unique),
136 };
Dave Airlie9a186642005-06-23 21:29:18 +1000137
Al Viro17e3dad2017-05-24 14:15:20 -0400138 err = drm_ioctl_kernel(file, drm_getunique, &uq, DRM_UNLOCKED);
Dave Airlie9a186642005-06-23 21:29:18 +1000139 if (err)
140 return err;
141
Al Viro17e3dad2017-05-24 14:15:20 -0400142 uq32.unique_len = uq.unique_len;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000143 if (copy_to_user((void __user *)arg, &uq32, sizeof(uq32)))
Dave Airlie9a186642005-06-23 21:29:18 +1000144 return -EFAULT;
145 return 0;
146}
147
148static int compat_drm_setunique(struct file *file, unsigned int cmd,
149 unsigned long arg)
150{
Al Viro56c275c2017-05-24 14:20:21 -0400151 /* it's dead */
152 return -EINVAL;
Dave Airlie9a186642005-06-23 21:29:18 +1000153}
154
155typedef struct drm_map32 {
Daniel Vetter26409812017-04-04 11:52:57 +0200156 u32 offset; /* Requested physical address (0 for SAREA) */
157 u32 size; /* Requested physical size (bytes) */
158 enum drm_map_type type; /* Type of memory to map */
159 enum drm_map_flags flags; /* Flags */
160 u32 handle; /* User-space: "Handle" to pass to mmap() */
161 int mtrr; /* MTRR slot used */
Dave Airlie9a186642005-06-23 21:29:18 +1000162} drm_map32_t;
163
164static int compat_drm_getmap(struct file *file, unsigned int cmd,
165 unsigned long arg)
166{
167 drm_map32_t __user *argp = (void __user *)arg;
168 drm_map32_t m32;
Al Viro8c4fe492017-05-24 14:26:39 -0400169 struct drm_map map;
170 int err;
Dave Airlie9a186642005-06-23 21:29:18 +1000171
Al Viro8c4fe492017-05-24 14:26:39 -0400172 if (copy_from_user(&m32, argp, sizeof(m32)))
Dave Airlie9a186642005-06-23 21:29:18 +1000173 return -EFAULT;
174
Al Viro8c4fe492017-05-24 14:26:39 -0400175 map.offset = m32.offset;
176 err = drm_ioctl_kernel(file, drm_legacy_getmap_ioctl, &map, DRM_UNLOCKED);
Dave Airlie9a186642005-06-23 21:29:18 +1000177 if (err)
178 return err;
179
Al Viro8c4fe492017-05-24 14:26:39 -0400180 m32.offset = map.offset;
181 m32.size = map.size;
182 m32.type = map.type;
183 m32.flags = map.flags;
184 m32.handle = ptr_to_compat(map.handle);
185 m32.mtrr = map.mtrr;
Dave Airlie9a186642005-06-23 21:29:18 +1000186 if (copy_to_user(argp, &m32, sizeof(m32)))
187 return -EFAULT;
188 return 0;
189
190}
191
192static int compat_drm_addmap(struct file *file, unsigned int cmd,
193 unsigned long arg)
194{
195 drm_map32_t __user *argp = (void __user *)arg;
196 drm_map32_t m32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000197 struct drm_map __user *map;
Dave Airlie9a186642005-06-23 21:29:18 +1000198 int err;
199 void *handle;
200
201 if (copy_from_user(&m32, argp, sizeof(m32)))
202 return -EFAULT;
203
204 map = compat_alloc_user_space(sizeof(*map));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300205 if (!map)
Dave Airlie9a186642005-06-23 21:29:18 +1000206 return -EFAULT;
207 if (__put_user(m32.offset, &map->offset)
208 || __put_user(m32.size, &map->size)
209 || __put_user(m32.type, &map->type)
210 || __put_user(m32.flags, &map->flags))
211 return -EFAULT;
212
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000213 err = drm_ioctl(file, DRM_IOCTL_ADD_MAP, (unsigned long)map);
Dave Airlie9a186642005-06-23 21:29:18 +1000214 if (err)
215 return err;
216
217 if (__get_user(m32.offset, &map->offset)
218 || __get_user(m32.mtrr, &map->mtrr)
219 || __get_user(handle, &map->handle))
220 return -EFAULT;
221
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000222 m32.handle = (unsigned long)handle;
Christian Dietrichcafe8d82011-06-04 15:36:43 +0000223 if (m32.handle != (unsigned long)handle)
Joe Perches499447d2017-02-28 04:55:53 -0800224 pr_err_ratelimited("compat_drm_addmap truncated handle %p for type %d offset %x\n",
Christian Dietrichcafe8d82011-06-04 15:36:43 +0000225 handle, m32.type, m32.offset);
Dave Airlie9a186642005-06-23 21:29:18 +1000226
227 if (copy_to_user(argp, &m32, sizeof(m32)))
228 return -EFAULT;
229
230 return 0;
231}
232
233static int compat_drm_rmmap(struct file *file, unsigned int cmd,
234 unsigned long arg)
235{
236 drm_map32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000237 struct drm_map __user *map;
Dave Airlie9a186642005-06-23 21:29:18 +1000238 u32 handle;
239
240 if (get_user(handle, &argp->handle))
241 return -EFAULT;
242
243 map = compat_alloc_user_space(sizeof(*map));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300244 if (!map)
Dave Airlie9a186642005-06-23 21:29:18 +1000245 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000246 if (__put_user((void *)(unsigned long)handle, &map->handle))
Dave Airlie9a186642005-06-23 21:29:18 +1000247 return -EFAULT;
248
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000249 return drm_ioctl(file, DRM_IOCTL_RM_MAP, (unsigned long)map);
Dave Airlie9a186642005-06-23 21:29:18 +1000250}
251
252typedef struct drm_client32 {
Daniel Vetter26409812017-04-04 11:52:57 +0200253 int idx; /* Which client desired? */
254 int auth; /* Is client authenticated? */
255 u32 pid; /* Process ID */
256 u32 uid; /* User ID */
257 u32 magic; /* Magic */
258 u32 iocs; /* Ioctl count */
Dave Airlie9a186642005-06-23 21:29:18 +1000259} drm_client32_t;
260
261static int compat_drm_getclient(struct file *file, unsigned int cmd,
262 unsigned long arg)
263{
264 drm_client32_t c32;
265 drm_client32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000266 struct drm_client __user *client;
Dave Airlie9a186642005-06-23 21:29:18 +1000267 int idx, err;
268
269 if (get_user(idx, &argp->idx))
270 return -EFAULT;
271
272 client = compat_alloc_user_space(sizeof(*client));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300273 if (!client)
Dave Airlie9a186642005-06-23 21:29:18 +1000274 return -EFAULT;
275 if (__put_user(idx, &client->idx))
276 return -EFAULT;
277
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000278 err = drm_ioctl(file, DRM_IOCTL_GET_CLIENT, (unsigned long)client);
Dave Airlie9a186642005-06-23 21:29:18 +1000279 if (err)
280 return err;
281
Julia Lawall08bc3d42012-01-30 12:05:55 +0100282 if (__get_user(c32.idx, &client->idx)
283 || __get_user(c32.auth, &client->auth)
Dave Airlie9a186642005-06-23 21:29:18 +1000284 || __get_user(c32.pid, &client->pid)
285 || __get_user(c32.uid, &client->uid)
286 || __get_user(c32.magic, &client->magic)
287 || __get_user(c32.iocs, &client->iocs))
288 return -EFAULT;
289
290 if (copy_to_user(argp, &c32, sizeof(c32)))
291 return -EFAULT;
292 return 0;
293}
294
295typedef struct drm_stats32 {
296 u32 count;
297 struct {
298 u32 value;
Dave Airlieff4135a2007-07-16 13:53:57 +1000299 enum drm_stat_type type;
Dave Airlie9a186642005-06-23 21:29:18 +1000300 } data[15];
301} drm_stats32_t;
302
303static int compat_drm_getstats(struct file *file, unsigned int cmd,
304 unsigned long arg)
305{
306 drm_stats32_t s32;
307 drm_stats32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000308 struct drm_stats __user *stats;
Dave Airlie9a186642005-06-23 21:29:18 +1000309 int i, err;
310
Heinrich Schuchardtec913f32016-08-21 19:56:19 +0200311 memset(&s32, 0, sizeof(drm_stats32_t));
Dave Airlie9a186642005-06-23 21:29:18 +1000312 stats = compat_alloc_user_space(sizeof(*stats));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300313 if (!stats)
Dave Airlie9a186642005-06-23 21:29:18 +1000314 return -EFAULT;
315
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000316 err = drm_ioctl(file, DRM_IOCTL_GET_STATS, (unsigned long)stats);
Dave Airlie9a186642005-06-23 21:29:18 +1000317 if (err)
318 return err;
319
320 if (__get_user(s32.count, &stats->count))
321 return -EFAULT;
322 for (i = 0; i < 15; ++i)
323 if (__get_user(s32.data[i].value, &stats->data[i].value)
324 || __get_user(s32.data[i].type, &stats->data[i].type))
325 return -EFAULT;
326
327 if (copy_to_user(argp, &s32, sizeof(s32)))
328 return -EFAULT;
329 return 0;
330}
331
332typedef struct drm_buf_desc32 {
Daniel Vetter26409812017-04-04 11:52:57 +0200333 int count; /* Number of buffers of this size */
334 int size; /* Size in bytes */
335 int low_mark; /* Low water mark */
336 int high_mark; /* High water mark */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000337 int flags;
Daniel Vetter26409812017-04-04 11:52:57 +0200338 u32 agp_start; /* Start address in the AGP aperture */
Dave Airlie9a186642005-06-23 21:29:18 +1000339} drm_buf_desc32_t;
340
341static int compat_drm_addbufs(struct file *file, unsigned int cmd,
342 unsigned long arg)
343{
344 drm_buf_desc32_t __user *argp = (void __user *)arg;
Al Viroff22ff92017-05-24 13:42:27 -0400345 drm_buf_desc32_t desc32;
346 struct drm_buf_desc desc;
Dave Airlie9a186642005-06-23 21:29:18 +1000347 int err;
Dave Airlie9a186642005-06-23 21:29:18 +1000348
Al Viroff22ff92017-05-24 13:42:27 -0400349 if (copy_from_user(&desc32, argp, sizeof(drm_buf_desc32_t)))
Dave Airlie9a186642005-06-23 21:29:18 +1000350 return -EFAULT;
351
Al Viroff22ff92017-05-24 13:42:27 -0400352 desc = (struct drm_buf_desc){
353 desc32.count, desc32.size, desc32.low_mark, desc32.high_mark,
354 desc32.flags, desc32.agp_start
355 };
Dave Airlie9a186642005-06-23 21:29:18 +1000356
Al Viroff22ff92017-05-24 13:42:27 -0400357 err = drm_ioctl_kernel(file, drm_legacy_addbufs, &desc,
358 DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
Dave Airlie9a186642005-06-23 21:29:18 +1000359 if (err)
360 return err;
361
Al Viroff22ff92017-05-24 13:42:27 -0400362 desc32 = (drm_buf_desc32_t){
363 desc.count, desc.size, desc.low_mark, desc.high_mark,
364 desc.flags, desc.agp_start
365 };
366 if (copy_to_user(argp, &desc32, sizeof(drm_buf_desc32_t)))
Dave Airlie9a186642005-06-23 21:29:18 +1000367 return -EFAULT;
368
369 return 0;
370}
371
372static int compat_drm_markbufs(struct file *file, unsigned int cmd,
373 unsigned long arg)
374{
375 drm_buf_desc32_t b32;
376 drm_buf_desc32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000377 struct drm_buf_desc __user *buf;
Dave Airlie9a186642005-06-23 21:29:18 +1000378
379 if (copy_from_user(&b32, argp, sizeof(b32)))
380 return -EFAULT;
381
382 buf = compat_alloc_user_space(sizeof(*buf));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300383 if (!buf)
Dave Airlie9a186642005-06-23 21:29:18 +1000384 return -EFAULT;
385
386 if (__put_user(b32.size, &buf->size)
387 || __put_user(b32.low_mark, &buf->low_mark)
388 || __put_user(b32.high_mark, &buf->high_mark))
389 return -EFAULT;
390
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000391 return drm_ioctl(file, DRM_IOCTL_MARK_BUFS, (unsigned long)buf);
Dave Airlie9a186642005-06-23 21:29:18 +1000392}
393
394typedef struct drm_buf_info32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000395 int count; /**< Entries in list */
396 u32 list;
Dave Airlie9a186642005-06-23 21:29:18 +1000397} drm_buf_info32_t;
398
Al Viro5c7640a2017-05-24 17:54:09 -0400399static int copy_one_buf32(void *data, int count, struct drm_buf_entry *from)
400{
401 drm_buf_info32_t *request = data;
402 drm_buf_desc32_t __user *to = compat_ptr(request->list);
403 drm_buf_desc32_t v = {.count = from->buf_count,
404 .size = from->buf_size,
405 .low_mark = from->low_mark,
406 .high_mark = from->high_mark};
407 return copy_to_user(to + count, &v, offsetof(drm_buf_desc32_t, flags));
408}
409
410static int drm_legacy_infobufs32(struct drm_device *dev, void *data,
411 struct drm_file *file_priv)
412{
413 drm_buf_info32_t *request = data;
414 return __drm_legacy_infobufs(dev, data, &request->count, copy_one_buf32);
415}
416
Dave Airlie9a186642005-06-23 21:29:18 +1000417static int compat_drm_infobufs(struct file *file, unsigned int cmd,
418 unsigned long arg)
419{
420 drm_buf_info32_t req32;
421 drm_buf_info32_t __user *argp = (void __user *)arg;
Al Viro5c7640a2017-05-24 17:54:09 -0400422 int err;
Dave Airlie9a186642005-06-23 21:29:18 +1000423
424 if (copy_from_user(&req32, argp, sizeof(req32)))
425 return -EFAULT;
426
Al Viro5c7640a2017-05-24 17:54:09 -0400427 if (req32.count < 0)
428 req32.count = 0;
Dave Airlie9a186642005-06-23 21:29:18 +1000429
Al Viro5c7640a2017-05-24 17:54:09 -0400430 err = drm_ioctl_kernel(file, drm_legacy_infobufs32, &req32, DRM_AUTH);
Dave Airlie9a186642005-06-23 21:29:18 +1000431 if (err)
432 return err;
433
Al Viro5c7640a2017-05-24 17:54:09 -0400434 if (put_user(req32.count, &argp->count))
Dave Airlie9a186642005-06-23 21:29:18 +1000435 return -EFAULT;
436
437 return 0;
438}
439
440typedef struct drm_buf_pub32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000441 int idx; /**< Index into the master buffer list */
442 int total; /**< Buffer size */
443 int used; /**< Amount of buffer in use (for DMA) */
444 u32 address; /**< Address of buffer */
Dave Airlie9a186642005-06-23 21:29:18 +1000445} drm_buf_pub32_t;
446
447typedef struct drm_buf_map32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000448 int count; /**< Length of the buffer list */
449 u32 virtual; /**< Mmap'd area in user-virtual */
450 u32 list; /**< Buffer information */
Dave Airlie9a186642005-06-23 21:29:18 +1000451} drm_buf_map32_t;
452
453static int compat_drm_mapbufs(struct file *file, unsigned int cmd,
454 unsigned long arg)
455{
456 drm_buf_map32_t __user *argp = (void __user *)arg;
457 drm_buf_map32_t req32;
458 drm_buf_pub32_t __user *list32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000459 struct drm_buf_map __user *request;
460 struct drm_buf_pub __user *list;
Dave Airlie9a186642005-06-23 21:29:18 +1000461 int i, err;
462 int count, actual;
463 size_t nbytes;
464 void __user *addr;
465
466 if (copy_from_user(&req32, argp, sizeof(req32)))
467 return -EFAULT;
468 count = req32.count;
469 list32 = (void __user *)(unsigned long)req32.list;
470
471 if (count < 0)
472 return -EINVAL;
Dave Airlieff4135a2007-07-16 13:53:57 +1000473 nbytes = sizeof(*request) + count * sizeof(struct drm_buf_pub);
Dave Airlie9a186642005-06-23 21:29:18 +1000474 request = compat_alloc_user_space(nbytes);
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300475 if (!request)
Dave Airlie9a186642005-06-23 21:29:18 +1000476 return -EFAULT;
Dave Airlieff4135a2007-07-16 13:53:57 +1000477 list = (struct drm_buf_pub *) (request + 1);
Dave Airlie9a186642005-06-23 21:29:18 +1000478
479 if (__put_user(count, &request->count)
480 || __put_user(list, &request->list))
481 return -EFAULT;
482
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000483 err = drm_ioctl(file, DRM_IOCTL_MAP_BUFS, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000484 if (err)
485 return err;
486
487 if (__get_user(actual, &request->count))
488 return -EFAULT;
489 if (count >= actual)
490 for (i = 0; i < actual; ++i)
491 if (__copy_in_user(&list32[i], &list[i],
Dave Airlieff4135a2007-07-16 13:53:57 +1000492 offsetof(struct drm_buf_pub, address))
Dave Airlie9a186642005-06-23 21:29:18 +1000493 || __get_user(addr, &list[i].address)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000494 || __put_user((unsigned long)addr,
Dave Airlie9a186642005-06-23 21:29:18 +1000495 &list32[i].address))
496 return -EFAULT;
497
498 if (__put_user(actual, &argp->count)
499 || __get_user(addr, &request->virtual)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000500 || __put_user((unsigned long)addr, &argp->virtual))
Dave Airlie9a186642005-06-23 21:29:18 +1000501 return -EFAULT;
502
503 return 0;
504}
505
506typedef struct drm_buf_free32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000507 int count;
508 u32 list;
Dave Airlie9a186642005-06-23 21:29:18 +1000509} drm_buf_free32_t;
510
511static int compat_drm_freebufs(struct file *file, unsigned int cmd,
512 unsigned long arg)
513{
514 drm_buf_free32_t req32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000515 struct drm_buf_free __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000516 drm_buf_free32_t __user *argp = (void __user *)arg;
517
518 if (copy_from_user(&req32, argp, sizeof(req32)))
519 return -EFAULT;
520
521 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300522 if (!request)
Dave Airlie9a186642005-06-23 21:29:18 +1000523 return -EFAULT;
524 if (__put_user(req32.count, &request->count)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000525 || __put_user((int __user *)(unsigned long)req32.list,
Dave Airlie9a186642005-06-23 21:29:18 +1000526 &request->list))
527 return -EFAULT;
528
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000529 return drm_ioctl(file, DRM_IOCTL_FREE_BUFS, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000530}
531
532typedef struct drm_ctx_priv_map32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000533 unsigned int ctx_id; /**< Context requesting private mapping */
534 u32 handle; /**< Handle of map */
Dave Airlie9a186642005-06-23 21:29:18 +1000535} drm_ctx_priv_map32_t;
536
537static int compat_drm_setsareactx(struct file *file, unsigned int cmd,
538 unsigned long arg)
539{
540 drm_ctx_priv_map32_t req32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000541 struct drm_ctx_priv_map __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000542 drm_ctx_priv_map32_t __user *argp = (void __user *)arg;
543
544 if (copy_from_user(&req32, argp, sizeof(req32)))
545 return -EFAULT;
546
547 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300548 if (!request)
Dave Airlie9a186642005-06-23 21:29:18 +1000549 return -EFAULT;
550 if (__put_user(req32.ctx_id, &request->ctx_id)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000551 || __put_user((void *)(unsigned long)req32.handle,
Dave Airlie9a186642005-06-23 21:29:18 +1000552 &request->handle))
553 return -EFAULT;
554
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000555 return drm_ioctl(file, DRM_IOCTL_SET_SAREA_CTX, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000556}
557
558static int compat_drm_getsareactx(struct file *file, unsigned int cmd,
559 unsigned long arg)
560{
Dave Airlieff4135a2007-07-16 13:53:57 +1000561 struct drm_ctx_priv_map __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000562 drm_ctx_priv_map32_t __user *argp = (void __user *)arg;
563 int err;
564 unsigned int ctx_id;
565 void *handle;
566
567 if (!access_ok(VERIFY_WRITE, argp, sizeof(*argp))
568 || __get_user(ctx_id, &argp->ctx_id))
569 return -EFAULT;
570
571 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300572 if (!request)
Dave Airlie9a186642005-06-23 21:29:18 +1000573 return -EFAULT;
574 if (__put_user(ctx_id, &request->ctx_id))
575 return -EFAULT;
576
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000577 err = drm_ioctl(file, DRM_IOCTL_GET_SAREA_CTX, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000578 if (err)
579 return err;
580
581 if (__get_user(handle, &request->handle)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000582 || __put_user((unsigned long)handle, &argp->handle))
Dave Airlie9a186642005-06-23 21:29:18 +1000583 return -EFAULT;
584
585 return 0;
586}
587
588typedef struct drm_ctx_res32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000589 int count;
590 u32 contexts;
Dave Airlie9a186642005-06-23 21:29:18 +1000591} drm_ctx_res32_t;
592
593static int compat_drm_resctx(struct file *file, unsigned int cmd,
594 unsigned long arg)
595{
596 drm_ctx_res32_t __user *argp = (void __user *)arg;
597 drm_ctx_res32_t res32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000598 struct drm_ctx_res __user *res;
Dave Airlie9a186642005-06-23 21:29:18 +1000599 int err;
600
601 if (copy_from_user(&res32, argp, sizeof(res32)))
602 return -EFAULT;
603
604 res = compat_alloc_user_space(sizeof(*res));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300605 if (!res)
Dave Airlie9a186642005-06-23 21:29:18 +1000606 return -EFAULT;
607 if (__put_user(res32.count, &res->count)
Dave Airlieff4135a2007-07-16 13:53:57 +1000608 || __put_user((struct drm_ctx __user *) (unsigned long)res32.contexts,
Dave Airlie9a186642005-06-23 21:29:18 +1000609 &res->contexts))
610 return -EFAULT;
611
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000612 err = drm_ioctl(file, DRM_IOCTL_RES_CTX, (unsigned long)res);
Dave Airlie9a186642005-06-23 21:29:18 +1000613 if (err)
614 return err;
615
616 if (__get_user(res32.count, &res->count)
617 || __put_user(res32.count, &argp->count))
618 return -EFAULT;
619
620 return 0;
621}
622
623typedef struct drm_dma32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000624 int context; /**< Context handle */
625 int send_count; /**< Number of buffers to send */
626 u32 send_indices; /**< List of handles to buffers */
627 u32 send_sizes; /**< Lengths of data to send */
Dave Airlieff4135a2007-07-16 13:53:57 +1000628 enum drm_dma_flags flags; /**< Flags */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000629 int request_count; /**< Number of buffers requested */
630 int request_size; /**< Desired size for buffers */
631 u32 request_indices; /**< Buffer information */
632 u32 request_sizes;
633 int granted_count; /**< Number of buffers granted */
Dave Airlie9a186642005-06-23 21:29:18 +1000634} drm_dma32_t;
635
636static int compat_drm_dma(struct file *file, unsigned int cmd,
637 unsigned long arg)
638{
639 drm_dma32_t d32;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000640 drm_dma32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000641 struct drm_dma __user *d;
Dave Airlie9a186642005-06-23 21:29:18 +1000642 int err;
643
644 if (copy_from_user(&d32, argp, sizeof(d32)))
645 return -EFAULT;
646
647 d = compat_alloc_user_space(sizeof(*d));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300648 if (!d)
Dave Airlie9a186642005-06-23 21:29:18 +1000649 return -EFAULT;
650
651 if (__put_user(d32.context, &d->context)
652 || __put_user(d32.send_count, &d->send_count)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000653 || __put_user((int __user *)(unsigned long)d32.send_indices,
Dave Airlie9a186642005-06-23 21:29:18 +1000654 &d->send_indices)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000655 || __put_user((int __user *)(unsigned long)d32.send_sizes,
Dave Airlie9a186642005-06-23 21:29:18 +1000656 &d->send_sizes)
657 || __put_user(d32.flags, &d->flags)
658 || __put_user(d32.request_count, &d->request_count)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000659 || __put_user((int __user *)(unsigned long)d32.request_indices,
Dave Airlie9a186642005-06-23 21:29:18 +1000660 &d->request_indices)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000661 || __put_user((int __user *)(unsigned long)d32.request_sizes,
Dave Airlie9a186642005-06-23 21:29:18 +1000662 &d->request_sizes))
663 return -EFAULT;
664
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000665 err = drm_ioctl(file, DRM_IOCTL_DMA, (unsigned long)d);
Dave Airlie9a186642005-06-23 21:29:18 +1000666 if (err)
667 return err;
668
669 if (__get_user(d32.request_size, &d->request_size)
670 || __get_user(d32.granted_count, &d->granted_count)
671 || __put_user(d32.request_size, &argp->request_size)
672 || __put_user(d32.granted_count, &argp->granted_count))
673 return -EFAULT;
674
675 return 0;
676}
677
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200678#if IS_ENABLED(CONFIG_AGP)
Dave Airlie9a186642005-06-23 21:29:18 +1000679typedef struct drm_agp_mode32 {
680 u32 mode; /**< AGP mode */
681} drm_agp_mode32_t;
682
683static int compat_drm_agp_enable(struct file *file, unsigned int cmd,
684 unsigned long arg)
685{
686 drm_agp_mode32_t __user *argp = (void __user *)arg;
687 drm_agp_mode32_t m32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000688 struct drm_agp_mode __user *mode;
Dave Airlie9a186642005-06-23 21:29:18 +1000689
690 if (get_user(m32.mode, &argp->mode))
691 return -EFAULT;
692
693 mode = compat_alloc_user_space(sizeof(*mode));
694 if (put_user(m32.mode, &mode->mode))
695 return -EFAULT;
696
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000697 return drm_ioctl(file, DRM_IOCTL_AGP_ENABLE, (unsigned long)mode);
Dave Airlie9a186642005-06-23 21:29:18 +1000698}
699
700typedef struct drm_agp_info32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000701 int agp_version_major;
702 int agp_version_minor;
703 u32 mode;
704 u32 aperture_base; /* physical address */
705 u32 aperture_size; /* bytes */
706 u32 memory_allowed; /* bytes */
707 u32 memory_used;
Dave Airlie9a186642005-06-23 21:29:18 +1000708
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000709 /* PCI information */
Dave Airlie9a186642005-06-23 21:29:18 +1000710 unsigned short id_vendor;
711 unsigned short id_device;
712} drm_agp_info32_t;
713
714static int compat_drm_agp_info(struct file *file, unsigned int cmd,
715 unsigned long arg)
716{
717 drm_agp_info32_t __user *argp = (void __user *)arg;
718 drm_agp_info32_t i32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000719 struct drm_agp_info __user *info;
Dave Airlie9a186642005-06-23 21:29:18 +1000720 int err;
721
722 info = compat_alloc_user_space(sizeof(*info));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300723 if (!info)
Dave Airlie9a186642005-06-23 21:29:18 +1000724 return -EFAULT;
725
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000726 err = drm_ioctl(file, DRM_IOCTL_AGP_INFO, (unsigned long)info);
Dave Airlie9a186642005-06-23 21:29:18 +1000727 if (err)
728 return err;
729
730 if (__get_user(i32.agp_version_major, &info->agp_version_major)
731 || __get_user(i32.agp_version_minor, &info->agp_version_minor)
732 || __get_user(i32.mode, &info->mode)
733 || __get_user(i32.aperture_base, &info->aperture_base)
734 || __get_user(i32.aperture_size, &info->aperture_size)
735 || __get_user(i32.memory_allowed, &info->memory_allowed)
736 || __get_user(i32.memory_used, &info->memory_used)
737 || __get_user(i32.id_vendor, &info->id_vendor)
738 || __get_user(i32.id_device, &info->id_device))
739 return -EFAULT;
740
741 if (copy_to_user(argp, &i32, sizeof(i32)))
742 return -EFAULT;
743
744 return 0;
745}
746
747typedef struct drm_agp_buffer32 {
748 u32 size; /**< In bytes -- will round to page boundary */
749 u32 handle; /**< Used for binding / unbinding */
750 u32 type; /**< Type of memory to allocate */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000751 u32 physical; /**< Physical used by i810 */
Dave Airlie9a186642005-06-23 21:29:18 +1000752} drm_agp_buffer32_t;
753
754static int compat_drm_agp_alloc(struct file *file, unsigned int cmd,
755 unsigned long arg)
756{
757 drm_agp_buffer32_t __user *argp = (void __user *)arg;
758 drm_agp_buffer32_t req32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000759 struct drm_agp_buffer __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000760 int err;
761
762 if (copy_from_user(&req32, argp, sizeof(req32)))
763 return -EFAULT;
764
765 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300766 if (!request
Dave Airlie9a186642005-06-23 21:29:18 +1000767 || __put_user(req32.size, &request->size)
768 || __put_user(req32.type, &request->type))
769 return -EFAULT;
770
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000771 err = drm_ioctl(file, DRM_IOCTL_AGP_ALLOC, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000772 if (err)
773 return err;
774
775 if (__get_user(req32.handle, &request->handle)
776 || __get_user(req32.physical, &request->physical)
777 || copy_to_user(argp, &req32, sizeof(req32))) {
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000778 drm_ioctl(file, DRM_IOCTL_AGP_FREE, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000779 return -EFAULT;
780 }
781
782 return 0;
783}
784
785static int compat_drm_agp_free(struct file *file, unsigned int cmd,
786 unsigned long arg)
787{
788 drm_agp_buffer32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000789 struct drm_agp_buffer __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000790 u32 handle;
791
792 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300793 if (!request
Dave Airlie9a186642005-06-23 21:29:18 +1000794 || get_user(handle, &argp->handle)
795 || __put_user(handle, &request->handle))
796 return -EFAULT;
797
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000798 return drm_ioctl(file, DRM_IOCTL_AGP_FREE, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000799}
800
801typedef struct drm_agp_binding32 {
802 u32 handle; /**< From drm_agp_buffer */
803 u32 offset; /**< In bytes -- will round to page boundary */
804} drm_agp_binding32_t;
805
806static int compat_drm_agp_bind(struct file *file, unsigned int cmd,
807 unsigned long arg)
808{
809 drm_agp_binding32_t __user *argp = (void __user *)arg;
810 drm_agp_binding32_t req32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000811 struct drm_agp_binding __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000812
813 if (copy_from_user(&req32, argp, sizeof(req32)))
814 return -EFAULT;
815
816 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300817 if (!request
Dave Airlie9a186642005-06-23 21:29:18 +1000818 || __put_user(req32.handle, &request->handle)
819 || __put_user(req32.offset, &request->offset))
820 return -EFAULT;
821
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000822 return drm_ioctl(file, DRM_IOCTL_AGP_BIND, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000823}
824
825static int compat_drm_agp_unbind(struct file *file, unsigned int cmd,
826 unsigned long arg)
827{
828 drm_agp_binding32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000829 struct drm_agp_binding __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000830 u32 handle;
831
832 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300833 if (!request
Dave Airlie9a186642005-06-23 21:29:18 +1000834 || get_user(handle, &argp->handle)
835 || __put_user(handle, &request->handle))
836 return -EFAULT;
837
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000838 return drm_ioctl(file, DRM_IOCTL_AGP_UNBIND, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000839}
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200840#endif /* CONFIG_AGP */
Dave Airlie9a186642005-06-23 21:29:18 +1000841
842typedef struct drm_scatter_gather32 {
843 u32 size; /**< In bytes -- will round to page boundary */
844 u32 handle; /**< Used for mapping / unmapping */
845} drm_scatter_gather32_t;
846
847static int compat_drm_sg_alloc(struct file *file, unsigned int cmd,
848 unsigned long arg)
849{
850 drm_scatter_gather32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000851 struct drm_scatter_gather __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000852 int err;
853 unsigned long x;
854
855 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300856 if (!request || !access_ok(VERIFY_WRITE, argp, sizeof(*argp))
Dave Airlie9a186642005-06-23 21:29:18 +1000857 || __get_user(x, &argp->size)
858 || __put_user(x, &request->size))
859 return -EFAULT;
860
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000861 err = drm_ioctl(file, DRM_IOCTL_SG_ALLOC, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000862 if (err)
863 return err;
864
865 /* XXX not sure about the handle conversion here... */
866 if (__get_user(x, &request->handle)
867 || __put_user(x >> PAGE_SHIFT, &argp->handle))
868 return -EFAULT;
869
870 return 0;
871}
872
873static int compat_drm_sg_free(struct file *file, unsigned int cmd,
874 unsigned long arg)
875{
876 drm_scatter_gather32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000877 struct drm_scatter_gather __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000878 unsigned long x;
879
880 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300881 if (!request || !access_ok(VERIFY_WRITE, argp, sizeof(*argp))
Dave Airlie9a186642005-06-23 21:29:18 +1000882 || __get_user(x, &argp->handle)
883 || __put_user(x << PAGE_SHIFT, &request->handle))
884 return -EFAULT;
885
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000886 return drm_ioctl(file, DRM_IOCTL_SG_FREE, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000887}
888
David Miller09e40d62009-02-18 01:35:21 -0800889#if defined(CONFIG_X86) || defined(CONFIG_IA64)
Eric Anholt49568872008-10-21 11:38:50 -0700890typedef struct drm_update_draw32 {
891 drm_drawable_t handle;
892 unsigned int type;
893 unsigned int num;
894 /* 64-bit version has a 32-bit pad here */
895 u64 data; /**< Pointer */
896} __attribute__((packed)) drm_update_draw32_t;
897
898static int compat_drm_update_draw(struct file *file, unsigned int cmd,
899 unsigned long arg)
900{
901 drm_update_draw32_t update32;
902 struct drm_update_draw __user *request;
903 int err;
904
905 if (copy_from_user(&update32, (void __user *)arg, sizeof(update32)))
906 return -EFAULT;
907
908 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300909 if (!request ||
Eric Anholt49568872008-10-21 11:38:50 -0700910 __put_user(update32.handle, &request->handle) ||
911 __put_user(update32.type, &request->type) ||
912 __put_user(update32.num, &request->num) ||
913 __put_user(update32.data, &request->data))
914 return -EFAULT;
915
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000916 err = drm_ioctl(file, DRM_IOCTL_UPDATE_DRAW, (unsigned long)request);
Eric Anholt49568872008-10-21 11:38:50 -0700917 return err;
918}
David Miller09e40d62009-02-18 01:35:21 -0800919#endif
Eric Anholt49568872008-10-21 11:38:50 -0700920
Dave Airlie9a186642005-06-23 21:29:18 +1000921struct drm_wait_vblank_request32 {
Dave Airlieff4135a2007-07-16 13:53:57 +1000922 enum drm_vblank_seq_type type;
Dave Airlie9a186642005-06-23 21:29:18 +1000923 unsigned int sequence;
924 u32 signal;
925};
926
927struct drm_wait_vblank_reply32 {
Dave Airlieff4135a2007-07-16 13:53:57 +1000928 enum drm_vblank_seq_type type;
Dave Airlie9a186642005-06-23 21:29:18 +1000929 unsigned int sequence;
930 s32 tval_sec;
931 s32 tval_usec;
932};
933
934typedef union drm_wait_vblank32 {
935 struct drm_wait_vblank_request32 request;
936 struct drm_wait_vblank_reply32 reply;
937} drm_wait_vblank32_t;
938
939static int compat_drm_wait_vblank(struct file *file, unsigned int cmd,
940 unsigned long arg)
941{
942 drm_wait_vblank32_t __user *argp = (void __user *)arg;
943 drm_wait_vblank32_t req32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000944 union drm_wait_vblank __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000945 int err;
946
947 if (copy_from_user(&req32, argp, sizeof(req32)))
948 return -EFAULT;
949
950 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300951 if (!request
Dave Airlie9a186642005-06-23 21:29:18 +1000952 || __put_user(req32.request.type, &request->request.type)
953 || __put_user(req32.request.sequence, &request->request.sequence)
954 || __put_user(req32.request.signal, &request->request.signal))
955 return -EFAULT;
956
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000957 err = drm_ioctl(file, DRM_IOCTL_WAIT_VBLANK, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000958 if (err)
959 return err;
960
961 if (__get_user(req32.reply.type, &request->reply.type)
962 || __get_user(req32.reply.sequence, &request->reply.sequence)
963 || __get_user(req32.reply.tval_sec, &request->reply.tval_sec)
964 || __get_user(req32.reply.tval_usec, &request->reply.tval_usec))
965 return -EFAULT;
966
967 if (copy_to_user(argp, &req32, sizeof(req32)))
968 return -EFAULT;
969
970 return 0;
971}
972
Kristian H. Kristensena9885882016-09-13 14:20:45 -0700973#if defined(CONFIG_X86) || defined(CONFIG_IA64)
Tvrtko Ursulinc631d5f2015-07-14 11:13:08 +0100974typedef struct drm_mode_fb_cmd232 {
975 u32 fb_id;
976 u32 width;
977 u32 height;
978 u32 pixel_format;
979 u32 flags;
980 u32 handles[4];
981 u32 pitches[4];
982 u32 offsets[4];
983 u64 modifier[4];
984} __attribute__((packed)) drm_mode_fb_cmd232_t;
985
986static int compat_drm_mode_addfb2(struct file *file, unsigned int cmd,
987 unsigned long arg)
988{
989 struct drm_mode_fb_cmd232 __user *argp = (void __user *)arg;
990 struct drm_mode_fb_cmd232 req32;
991 struct drm_mode_fb_cmd2 __user *req64;
992 int i;
993 int err;
994
995 if (copy_from_user(&req32, argp, sizeof(req32)))
996 return -EFAULT;
997
998 req64 = compat_alloc_user_space(sizeof(*req64));
999
1000 if (!access_ok(VERIFY_WRITE, req64, sizeof(*req64))
1001 || __put_user(req32.width, &req64->width)
1002 || __put_user(req32.height, &req64->height)
1003 || __put_user(req32.pixel_format, &req64->pixel_format)
1004 || __put_user(req32.flags, &req64->flags))
1005 return -EFAULT;
1006
1007 for (i = 0; i < 4; i++) {
1008 if (__put_user(req32.handles[i], &req64->handles[i]))
1009 return -EFAULT;
1010 if (__put_user(req32.pitches[i], &req64->pitches[i]))
1011 return -EFAULT;
1012 if (__put_user(req32.offsets[i], &req64->offsets[i]))
1013 return -EFAULT;
1014 if (__put_user(req32.modifier[i], &req64->modifier[i]))
1015 return -EFAULT;
1016 }
1017
1018 err = drm_ioctl(file, DRM_IOCTL_MODE_ADDFB2, (unsigned long)req64);
1019 if (err)
1020 return err;
1021
1022 if (__get_user(req32.fb_id, &req64->fb_id))
1023 return -EFAULT;
1024
1025 if (copy_to_user(argp, &req32, sizeof(req32)))
1026 return -EFAULT;
1027
1028 return 0;
1029}
Kristian H. Kristensena9885882016-09-13 14:20:45 -07001030#endif
Tvrtko Ursulinc631d5f2015-07-14 11:13:08 +01001031
Al Viro9f43e542017-05-24 13:59:22 -04001032static struct {
1033 drm_ioctl_compat_t *fn;
1034 char *name;
1035} drm_compat_ioctls[] = {
Al Viroff22ff92017-05-24 13:42:27 -04001036#define DRM_IOCTL32_DEF(n, f) [DRM_IOCTL_NR(n##32)] = {.fn = f, .name = #n}
Al Viro012c6742017-05-24 14:11:03 -04001037 DRM_IOCTL32_DEF(DRM_IOCTL_VERSION, compat_drm_version),
Al Viro17e3dad2017-05-24 14:15:20 -04001038 DRM_IOCTL32_DEF(DRM_IOCTL_GET_UNIQUE, compat_drm_getunique),
Al Viro8c4fe492017-05-24 14:26:39 -04001039 DRM_IOCTL32_DEF(DRM_IOCTL_GET_MAP, compat_drm_getmap),
Al Viro9f43e542017-05-24 13:59:22 -04001040 [DRM_IOCTL_NR(DRM_IOCTL_GET_CLIENT32)].fn = compat_drm_getclient,
1041 [DRM_IOCTL_NR(DRM_IOCTL_GET_STATS32)].fn = compat_drm_getstats,
Al Viro56c275c2017-05-24 14:20:21 -04001042 DRM_IOCTL32_DEF(DRM_IOCTL_SET_UNIQUE, compat_drm_setunique),
Al Viro9f43e542017-05-24 13:59:22 -04001043 [DRM_IOCTL_NR(DRM_IOCTL_ADD_MAP32)].fn = compat_drm_addmap,
Al Viroff22ff92017-05-24 13:42:27 -04001044 DRM_IOCTL32_DEF(DRM_IOCTL_ADD_BUFS, compat_drm_addbufs),
Al Viro9f43e542017-05-24 13:59:22 -04001045 [DRM_IOCTL_NR(DRM_IOCTL_MARK_BUFS32)].fn = compat_drm_markbufs,
Al Viro5c7640a2017-05-24 17:54:09 -04001046 DRM_IOCTL32_DEF(DRM_IOCTL_INFO_BUFS, compat_drm_infobufs),
Al Viro9f43e542017-05-24 13:59:22 -04001047 [DRM_IOCTL_NR(DRM_IOCTL_MAP_BUFS32)].fn = compat_drm_mapbufs,
1048 [DRM_IOCTL_NR(DRM_IOCTL_FREE_BUFS32)].fn = compat_drm_freebufs,
1049 [DRM_IOCTL_NR(DRM_IOCTL_RM_MAP32)].fn = compat_drm_rmmap,
1050 [DRM_IOCTL_NR(DRM_IOCTL_SET_SAREA_CTX32)].fn = compat_drm_setsareactx,
1051 [DRM_IOCTL_NR(DRM_IOCTL_GET_SAREA_CTX32)].fn = compat_drm_getsareactx,
1052 [DRM_IOCTL_NR(DRM_IOCTL_RES_CTX32)].fn = compat_drm_resctx,
1053 [DRM_IOCTL_NR(DRM_IOCTL_DMA32)].fn = compat_drm_dma,
Daniel Vettera7fb8a22015-09-09 16:45:52 +02001054#if IS_ENABLED(CONFIG_AGP)
Al Viro9f43e542017-05-24 13:59:22 -04001055 [DRM_IOCTL_NR(DRM_IOCTL_AGP_ENABLE32)].fn = compat_drm_agp_enable,
1056 [DRM_IOCTL_NR(DRM_IOCTL_AGP_INFO32)].fn = compat_drm_agp_info,
1057 [DRM_IOCTL_NR(DRM_IOCTL_AGP_ALLOC32)].fn = compat_drm_agp_alloc,
1058 [DRM_IOCTL_NR(DRM_IOCTL_AGP_FREE32)].fn = compat_drm_agp_free,
1059 [DRM_IOCTL_NR(DRM_IOCTL_AGP_BIND32)].fn = compat_drm_agp_bind,
1060 [DRM_IOCTL_NR(DRM_IOCTL_AGP_UNBIND32)].fn = compat_drm_agp_unbind,
Dave Airlie9a186642005-06-23 21:29:18 +10001061#endif
Al Viro9f43e542017-05-24 13:59:22 -04001062 [DRM_IOCTL_NR(DRM_IOCTL_SG_ALLOC32)].fn = compat_drm_sg_alloc,
1063 [DRM_IOCTL_NR(DRM_IOCTL_SG_FREE32)].fn = compat_drm_sg_free,
David Miller09e40d62009-02-18 01:35:21 -08001064#if defined(CONFIG_X86) || defined(CONFIG_IA64)
Al Viro9f43e542017-05-24 13:59:22 -04001065 [DRM_IOCTL_NR(DRM_IOCTL_UPDATE_DRAW32)].fn = compat_drm_update_draw,
David Miller09e40d62009-02-18 01:35:21 -08001066#endif
Al Viro9f43e542017-05-24 13:59:22 -04001067 [DRM_IOCTL_NR(DRM_IOCTL_WAIT_VBLANK32)].fn = compat_drm_wait_vblank,
Kristian H. Kristensena9885882016-09-13 14:20:45 -07001068#if defined(CONFIG_X86) || defined(CONFIG_IA64)
Al Viro9f43e542017-05-24 13:59:22 -04001069 [DRM_IOCTL_NR(DRM_IOCTL_MODE_ADDFB232)].fn = compat_drm_mode_addfb2,
Kristian H. Kristensena9885882016-09-13 14:20:45 -07001070#endif
Dave Airlie9a186642005-06-23 21:29:18 +10001071};
1072
1073/**
Daniel Vetter26409812017-04-04 11:52:57 +02001074 * drm_compat_ioctl - 32bit IOCTL compatibility handler for DRM drivers
1075 * @filp: file this ioctl is called on
1076 * @cmd: ioctl cmd number
1077 * @arg: user argument
Dave Airlie9a186642005-06-23 21:29:18 +10001078 *
Daniel Vetter26409812017-04-04 11:52:57 +02001079 * Compatibility handler for 32 bit userspace running on 64 kernels. All actual
1080 * IOCTL handling is forwarded to drm_ioctl(), while marshalling structures as
1081 * appropriate. Note that this only handles DRM core IOCTLs, if the driver has
1082 * botched IOCTL itself, it must handle those by wrapping this function.
1083 *
1084 * Returns:
1085 * Zero on success, negative error code on failure.
Dave Airlie9a186642005-06-23 21:29:18 +10001086 */
1087long drm_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1088{
1089 unsigned int nr = DRM_IOCTL_NR(cmd);
1090 drm_ioctl_compat_t *fn;
1091 int ret;
1092
Ian Romanick7ffa05e2007-11-22 17:02:08 +10001093 /* Assume that ioctls without an explicit compat routine will just
1094 * work. This may not always be a good assumption, but it's better
1095 * than always failing.
1096 */
Dave Airlie3d774612006-08-07 20:07:43 +10001097 if (nr >= ARRAY_SIZE(drm_compat_ioctls))
Arnd Bergmanned8b6702009-12-16 22:17:09 +00001098 return drm_ioctl(filp, cmd, arg);
Dave Airlie9a186642005-06-23 21:29:18 +10001099
Al Viro9f43e542017-05-24 13:59:22 -04001100 fn = drm_compat_ioctls[nr].fn;
1101 if (!fn)
1102 return drm_ioctl(filp, cmd, arg);
Dave Airlie9a186642005-06-23 21:29:18 +10001103
Al Viro9f43e542017-05-24 13:59:22 -04001104 if (drm_compat_ioctls[nr].name) {
1105 struct drm_file *file_priv = filp->private_data;
1106 DRM_DEBUG("pid=%d, dev=0x%lx, auth=%d, %s\n",
1107 task_pid_nr(current),
1108 (long)old_encode_dev(file_priv->minor->kdev->devt),
1109 file_priv->authenticated,
1110 drm_compat_ioctls[nr].name);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001111 ret = (*fn) (filp, cmd, arg);
Al Viro9f43e542017-05-24 13:59:22 -04001112 if (ret)
1113 DRM_DEBUG("ret = %d\n", ret);
1114 } else {
1115 ret = (*fn) (filp, cmd, arg);
1116 }
Dave Airlie9a186642005-06-23 21:29:18 +10001117 return ret;
1118}
Dave Airlie9a186642005-06-23 21:29:18 +10001119EXPORT_SYMBOL(drm_compat_ioctl);