blob: 7f36cbe066da00785a8ac2077ea412505a889a35 [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;
Al Viro9e926622017-05-24 19:10:32 -0400266 struct drm_client client;
267 int err;
Dave Airlie9a186642005-06-23 21:29:18 +1000268
Al Viro9e926622017-05-24 19:10:32 -0400269 if (copy_from_user(&c32, argp, sizeof(c32)))
Dave Airlie9a186642005-06-23 21:29:18 +1000270 return -EFAULT;
271
Al Viro9e926622017-05-24 19:10:32 -0400272 client.idx = c32.idx;
Dave Airlie9a186642005-06-23 21:29:18 +1000273
Al Viro9e926622017-05-24 19:10:32 -0400274 err = drm_ioctl_kernel(file, drm_getclient, &client, DRM_UNLOCKED);
Dave Airlie9a186642005-06-23 21:29:18 +1000275 if (err)
276 return err;
277
Al Viro9e926622017-05-24 19:10:32 -0400278 c32.idx = client.idx;
279 c32.auth = client.auth;
280 c32.pid = client.pid;
281 c32.uid = client.uid;
282 c32.magic = client.magic;
283 c32.iocs = client.iocs;
Dave Airlie9a186642005-06-23 21:29:18 +1000284
285 if (copy_to_user(argp, &c32, sizeof(c32)))
286 return -EFAULT;
287 return 0;
288}
289
290typedef struct drm_stats32 {
291 u32 count;
292 struct {
293 u32 value;
Dave Airlieff4135a2007-07-16 13:53:57 +1000294 enum drm_stat_type type;
Dave Airlie9a186642005-06-23 21:29:18 +1000295 } data[15];
296} drm_stats32_t;
297
298static int compat_drm_getstats(struct file *file, unsigned int cmd,
299 unsigned long arg)
300{
301 drm_stats32_t s32;
302 drm_stats32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000303 struct drm_stats __user *stats;
Dave Airlie9a186642005-06-23 21:29:18 +1000304 int i, err;
305
Heinrich Schuchardtec913f32016-08-21 19:56:19 +0200306 memset(&s32, 0, sizeof(drm_stats32_t));
Dave Airlie9a186642005-06-23 21:29:18 +1000307 stats = compat_alloc_user_space(sizeof(*stats));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300308 if (!stats)
Dave Airlie9a186642005-06-23 21:29:18 +1000309 return -EFAULT;
310
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000311 err = drm_ioctl(file, DRM_IOCTL_GET_STATS, (unsigned long)stats);
Dave Airlie9a186642005-06-23 21:29:18 +1000312 if (err)
313 return err;
314
315 if (__get_user(s32.count, &stats->count))
316 return -EFAULT;
317 for (i = 0; i < 15; ++i)
318 if (__get_user(s32.data[i].value, &stats->data[i].value)
319 || __get_user(s32.data[i].type, &stats->data[i].type))
320 return -EFAULT;
321
322 if (copy_to_user(argp, &s32, sizeof(s32)))
323 return -EFAULT;
324 return 0;
325}
326
327typedef struct drm_buf_desc32 {
Daniel Vetter26409812017-04-04 11:52:57 +0200328 int count; /* Number of buffers of this size */
329 int size; /* Size in bytes */
330 int low_mark; /* Low water mark */
331 int high_mark; /* High water mark */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000332 int flags;
Daniel Vetter26409812017-04-04 11:52:57 +0200333 u32 agp_start; /* Start address in the AGP aperture */
Dave Airlie9a186642005-06-23 21:29:18 +1000334} drm_buf_desc32_t;
335
336static int compat_drm_addbufs(struct file *file, unsigned int cmd,
337 unsigned long arg)
338{
339 drm_buf_desc32_t __user *argp = (void __user *)arg;
Al Viroff22ff92017-05-24 13:42:27 -0400340 drm_buf_desc32_t desc32;
341 struct drm_buf_desc desc;
Dave Airlie9a186642005-06-23 21:29:18 +1000342 int err;
Dave Airlie9a186642005-06-23 21:29:18 +1000343
Al Viroff22ff92017-05-24 13:42:27 -0400344 if (copy_from_user(&desc32, argp, sizeof(drm_buf_desc32_t)))
Dave Airlie9a186642005-06-23 21:29:18 +1000345 return -EFAULT;
346
Al Viroff22ff92017-05-24 13:42:27 -0400347 desc = (struct drm_buf_desc){
348 desc32.count, desc32.size, desc32.low_mark, desc32.high_mark,
349 desc32.flags, desc32.agp_start
350 };
Dave Airlie9a186642005-06-23 21:29:18 +1000351
Al Viroff22ff92017-05-24 13:42:27 -0400352 err = drm_ioctl_kernel(file, drm_legacy_addbufs, &desc,
353 DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
Dave Airlie9a186642005-06-23 21:29:18 +1000354 if (err)
355 return err;
356
Al Viroff22ff92017-05-24 13:42:27 -0400357 desc32 = (drm_buf_desc32_t){
358 desc.count, desc.size, desc.low_mark, desc.high_mark,
359 desc.flags, desc.agp_start
360 };
361 if (copy_to_user(argp, &desc32, sizeof(drm_buf_desc32_t)))
Dave Airlie9a186642005-06-23 21:29:18 +1000362 return -EFAULT;
363
364 return 0;
365}
366
367static int compat_drm_markbufs(struct file *file, unsigned int cmd,
368 unsigned long arg)
369{
370 drm_buf_desc32_t b32;
371 drm_buf_desc32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000372 struct drm_buf_desc __user *buf;
Dave Airlie9a186642005-06-23 21:29:18 +1000373
374 if (copy_from_user(&b32, argp, sizeof(b32)))
375 return -EFAULT;
376
377 buf = compat_alloc_user_space(sizeof(*buf));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300378 if (!buf)
Dave Airlie9a186642005-06-23 21:29:18 +1000379 return -EFAULT;
380
381 if (__put_user(b32.size, &buf->size)
382 || __put_user(b32.low_mark, &buf->low_mark)
383 || __put_user(b32.high_mark, &buf->high_mark))
384 return -EFAULT;
385
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000386 return drm_ioctl(file, DRM_IOCTL_MARK_BUFS, (unsigned long)buf);
Dave Airlie9a186642005-06-23 21:29:18 +1000387}
388
389typedef struct drm_buf_info32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000390 int count; /**< Entries in list */
391 u32 list;
Dave Airlie9a186642005-06-23 21:29:18 +1000392} drm_buf_info32_t;
393
Al Viro5c7640a2017-05-24 17:54:09 -0400394static int copy_one_buf32(void *data, int count, struct drm_buf_entry *from)
395{
396 drm_buf_info32_t *request = data;
397 drm_buf_desc32_t __user *to = compat_ptr(request->list);
398 drm_buf_desc32_t v = {.count = from->buf_count,
399 .size = from->buf_size,
400 .low_mark = from->low_mark,
401 .high_mark = from->high_mark};
402 return copy_to_user(to + count, &v, offsetof(drm_buf_desc32_t, flags));
403}
404
405static int drm_legacy_infobufs32(struct drm_device *dev, void *data,
406 struct drm_file *file_priv)
407{
408 drm_buf_info32_t *request = data;
409 return __drm_legacy_infobufs(dev, data, &request->count, copy_one_buf32);
410}
411
Dave Airlie9a186642005-06-23 21:29:18 +1000412static int compat_drm_infobufs(struct file *file, unsigned int cmd,
413 unsigned long arg)
414{
415 drm_buf_info32_t req32;
416 drm_buf_info32_t __user *argp = (void __user *)arg;
Al Viro5c7640a2017-05-24 17:54:09 -0400417 int err;
Dave Airlie9a186642005-06-23 21:29:18 +1000418
419 if (copy_from_user(&req32, argp, sizeof(req32)))
420 return -EFAULT;
421
Al Viro5c7640a2017-05-24 17:54:09 -0400422 if (req32.count < 0)
423 req32.count = 0;
Dave Airlie9a186642005-06-23 21:29:18 +1000424
Al Viro5c7640a2017-05-24 17:54:09 -0400425 err = drm_ioctl_kernel(file, drm_legacy_infobufs32, &req32, DRM_AUTH);
Dave Airlie9a186642005-06-23 21:29:18 +1000426 if (err)
427 return err;
428
Al Viro5c7640a2017-05-24 17:54:09 -0400429 if (put_user(req32.count, &argp->count))
Dave Airlie9a186642005-06-23 21:29:18 +1000430 return -EFAULT;
431
432 return 0;
433}
434
435typedef struct drm_buf_pub32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000436 int idx; /**< Index into the master buffer list */
437 int total; /**< Buffer size */
438 int used; /**< Amount of buffer in use (for DMA) */
439 u32 address; /**< Address of buffer */
Dave Airlie9a186642005-06-23 21:29:18 +1000440} drm_buf_pub32_t;
441
442typedef struct drm_buf_map32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000443 int count; /**< Length of the buffer list */
444 u32 virtual; /**< Mmap'd area in user-virtual */
445 u32 list; /**< Buffer information */
Dave Airlie9a186642005-06-23 21:29:18 +1000446} drm_buf_map32_t;
447
448static int compat_drm_mapbufs(struct file *file, unsigned int cmd,
449 unsigned long arg)
450{
451 drm_buf_map32_t __user *argp = (void __user *)arg;
452 drm_buf_map32_t req32;
453 drm_buf_pub32_t __user *list32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000454 struct drm_buf_map __user *request;
455 struct drm_buf_pub __user *list;
Dave Airlie9a186642005-06-23 21:29:18 +1000456 int i, err;
457 int count, actual;
458 size_t nbytes;
459 void __user *addr;
460
461 if (copy_from_user(&req32, argp, sizeof(req32)))
462 return -EFAULT;
463 count = req32.count;
464 list32 = (void __user *)(unsigned long)req32.list;
465
466 if (count < 0)
467 return -EINVAL;
Dave Airlieff4135a2007-07-16 13:53:57 +1000468 nbytes = sizeof(*request) + count * sizeof(struct drm_buf_pub);
Dave Airlie9a186642005-06-23 21:29:18 +1000469 request = compat_alloc_user_space(nbytes);
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300470 if (!request)
Dave Airlie9a186642005-06-23 21:29:18 +1000471 return -EFAULT;
Dave Airlieff4135a2007-07-16 13:53:57 +1000472 list = (struct drm_buf_pub *) (request + 1);
Dave Airlie9a186642005-06-23 21:29:18 +1000473
474 if (__put_user(count, &request->count)
475 || __put_user(list, &request->list))
476 return -EFAULT;
477
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000478 err = drm_ioctl(file, DRM_IOCTL_MAP_BUFS, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000479 if (err)
480 return err;
481
482 if (__get_user(actual, &request->count))
483 return -EFAULT;
484 if (count >= actual)
485 for (i = 0; i < actual; ++i)
486 if (__copy_in_user(&list32[i], &list[i],
Dave Airlieff4135a2007-07-16 13:53:57 +1000487 offsetof(struct drm_buf_pub, address))
Dave Airlie9a186642005-06-23 21:29:18 +1000488 || __get_user(addr, &list[i].address)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000489 || __put_user((unsigned long)addr,
Dave Airlie9a186642005-06-23 21:29:18 +1000490 &list32[i].address))
491 return -EFAULT;
492
493 if (__put_user(actual, &argp->count)
494 || __get_user(addr, &request->virtual)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000495 || __put_user((unsigned long)addr, &argp->virtual))
Dave Airlie9a186642005-06-23 21:29:18 +1000496 return -EFAULT;
497
498 return 0;
499}
500
501typedef struct drm_buf_free32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000502 int count;
503 u32 list;
Dave Airlie9a186642005-06-23 21:29:18 +1000504} drm_buf_free32_t;
505
506static int compat_drm_freebufs(struct file *file, unsigned int cmd,
507 unsigned long arg)
508{
509 drm_buf_free32_t req32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000510 struct drm_buf_free __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000511 drm_buf_free32_t __user *argp = (void __user *)arg;
512
513 if (copy_from_user(&req32, argp, sizeof(req32)))
514 return -EFAULT;
515
516 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300517 if (!request)
Dave Airlie9a186642005-06-23 21:29:18 +1000518 return -EFAULT;
519 if (__put_user(req32.count, &request->count)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000520 || __put_user((int __user *)(unsigned long)req32.list,
Dave Airlie9a186642005-06-23 21:29:18 +1000521 &request->list))
522 return -EFAULT;
523
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000524 return drm_ioctl(file, DRM_IOCTL_FREE_BUFS, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000525}
526
527typedef struct drm_ctx_priv_map32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000528 unsigned int ctx_id; /**< Context requesting private mapping */
529 u32 handle; /**< Handle of map */
Dave Airlie9a186642005-06-23 21:29:18 +1000530} drm_ctx_priv_map32_t;
531
532static int compat_drm_setsareactx(struct file *file, unsigned int cmd,
533 unsigned long arg)
534{
535 drm_ctx_priv_map32_t req32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000536 struct drm_ctx_priv_map __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000537 drm_ctx_priv_map32_t __user *argp = (void __user *)arg;
538
539 if (copy_from_user(&req32, argp, sizeof(req32)))
540 return -EFAULT;
541
542 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300543 if (!request)
Dave Airlie9a186642005-06-23 21:29:18 +1000544 return -EFAULT;
545 if (__put_user(req32.ctx_id, &request->ctx_id)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000546 || __put_user((void *)(unsigned long)req32.handle,
Dave Airlie9a186642005-06-23 21:29:18 +1000547 &request->handle))
548 return -EFAULT;
549
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000550 return drm_ioctl(file, DRM_IOCTL_SET_SAREA_CTX, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000551}
552
553static int compat_drm_getsareactx(struct file *file, unsigned int cmd,
554 unsigned long arg)
555{
Dave Airlieff4135a2007-07-16 13:53:57 +1000556 struct drm_ctx_priv_map __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000557 drm_ctx_priv_map32_t __user *argp = (void __user *)arg;
558 int err;
559 unsigned int ctx_id;
560 void *handle;
561
562 if (!access_ok(VERIFY_WRITE, argp, sizeof(*argp))
563 || __get_user(ctx_id, &argp->ctx_id))
564 return -EFAULT;
565
566 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300567 if (!request)
Dave Airlie9a186642005-06-23 21:29:18 +1000568 return -EFAULT;
569 if (__put_user(ctx_id, &request->ctx_id))
570 return -EFAULT;
571
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000572 err = drm_ioctl(file, DRM_IOCTL_GET_SAREA_CTX, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000573 if (err)
574 return err;
575
576 if (__get_user(handle, &request->handle)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000577 || __put_user((unsigned long)handle, &argp->handle))
Dave Airlie9a186642005-06-23 21:29:18 +1000578 return -EFAULT;
579
580 return 0;
581}
582
583typedef struct drm_ctx_res32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000584 int count;
585 u32 contexts;
Dave Airlie9a186642005-06-23 21:29:18 +1000586} drm_ctx_res32_t;
587
588static int compat_drm_resctx(struct file *file, unsigned int cmd,
589 unsigned long arg)
590{
591 drm_ctx_res32_t __user *argp = (void __user *)arg;
592 drm_ctx_res32_t res32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000593 struct drm_ctx_res __user *res;
Dave Airlie9a186642005-06-23 21:29:18 +1000594 int err;
595
596 if (copy_from_user(&res32, argp, sizeof(res32)))
597 return -EFAULT;
598
599 res = compat_alloc_user_space(sizeof(*res));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300600 if (!res)
Dave Airlie9a186642005-06-23 21:29:18 +1000601 return -EFAULT;
602 if (__put_user(res32.count, &res->count)
Dave Airlieff4135a2007-07-16 13:53:57 +1000603 || __put_user((struct drm_ctx __user *) (unsigned long)res32.contexts,
Dave Airlie9a186642005-06-23 21:29:18 +1000604 &res->contexts))
605 return -EFAULT;
606
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000607 err = drm_ioctl(file, DRM_IOCTL_RES_CTX, (unsigned long)res);
Dave Airlie9a186642005-06-23 21:29:18 +1000608 if (err)
609 return err;
610
611 if (__get_user(res32.count, &res->count)
612 || __put_user(res32.count, &argp->count))
613 return -EFAULT;
614
615 return 0;
616}
617
618typedef struct drm_dma32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000619 int context; /**< Context handle */
620 int send_count; /**< Number of buffers to send */
621 u32 send_indices; /**< List of handles to buffers */
622 u32 send_sizes; /**< Lengths of data to send */
Dave Airlieff4135a2007-07-16 13:53:57 +1000623 enum drm_dma_flags flags; /**< Flags */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000624 int request_count; /**< Number of buffers requested */
625 int request_size; /**< Desired size for buffers */
626 u32 request_indices; /**< Buffer information */
627 u32 request_sizes;
628 int granted_count; /**< Number of buffers granted */
Dave Airlie9a186642005-06-23 21:29:18 +1000629} drm_dma32_t;
630
631static int compat_drm_dma(struct file *file, unsigned int cmd,
632 unsigned long arg)
633{
634 drm_dma32_t d32;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000635 drm_dma32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000636 struct drm_dma __user *d;
Dave Airlie9a186642005-06-23 21:29:18 +1000637 int err;
638
639 if (copy_from_user(&d32, argp, sizeof(d32)))
640 return -EFAULT;
641
642 d = compat_alloc_user_space(sizeof(*d));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300643 if (!d)
Dave Airlie9a186642005-06-23 21:29:18 +1000644 return -EFAULT;
645
646 if (__put_user(d32.context, &d->context)
647 || __put_user(d32.send_count, &d->send_count)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000648 || __put_user((int __user *)(unsigned long)d32.send_indices,
Dave Airlie9a186642005-06-23 21:29:18 +1000649 &d->send_indices)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000650 || __put_user((int __user *)(unsigned long)d32.send_sizes,
Dave Airlie9a186642005-06-23 21:29:18 +1000651 &d->send_sizes)
652 || __put_user(d32.flags, &d->flags)
653 || __put_user(d32.request_count, &d->request_count)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000654 || __put_user((int __user *)(unsigned long)d32.request_indices,
Dave Airlie9a186642005-06-23 21:29:18 +1000655 &d->request_indices)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000656 || __put_user((int __user *)(unsigned long)d32.request_sizes,
Dave Airlie9a186642005-06-23 21:29:18 +1000657 &d->request_sizes))
658 return -EFAULT;
659
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000660 err = drm_ioctl(file, DRM_IOCTL_DMA, (unsigned long)d);
Dave Airlie9a186642005-06-23 21:29:18 +1000661 if (err)
662 return err;
663
664 if (__get_user(d32.request_size, &d->request_size)
665 || __get_user(d32.granted_count, &d->granted_count)
666 || __put_user(d32.request_size, &argp->request_size)
667 || __put_user(d32.granted_count, &argp->granted_count))
668 return -EFAULT;
669
670 return 0;
671}
672
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200673#if IS_ENABLED(CONFIG_AGP)
Dave Airlie9a186642005-06-23 21:29:18 +1000674typedef struct drm_agp_mode32 {
675 u32 mode; /**< AGP mode */
676} drm_agp_mode32_t;
677
678static int compat_drm_agp_enable(struct file *file, unsigned int cmd,
679 unsigned long arg)
680{
681 drm_agp_mode32_t __user *argp = (void __user *)arg;
682 drm_agp_mode32_t m32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000683 struct drm_agp_mode __user *mode;
Dave Airlie9a186642005-06-23 21:29:18 +1000684
685 if (get_user(m32.mode, &argp->mode))
686 return -EFAULT;
687
688 mode = compat_alloc_user_space(sizeof(*mode));
689 if (put_user(m32.mode, &mode->mode))
690 return -EFAULT;
691
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000692 return drm_ioctl(file, DRM_IOCTL_AGP_ENABLE, (unsigned long)mode);
Dave Airlie9a186642005-06-23 21:29:18 +1000693}
694
695typedef struct drm_agp_info32 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000696 int agp_version_major;
697 int agp_version_minor;
698 u32 mode;
699 u32 aperture_base; /* physical address */
700 u32 aperture_size; /* bytes */
701 u32 memory_allowed; /* bytes */
702 u32 memory_used;
Dave Airlie9a186642005-06-23 21:29:18 +1000703
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000704 /* PCI information */
Dave Airlie9a186642005-06-23 21:29:18 +1000705 unsigned short id_vendor;
706 unsigned short id_device;
707} drm_agp_info32_t;
708
709static int compat_drm_agp_info(struct file *file, unsigned int cmd,
710 unsigned long arg)
711{
712 drm_agp_info32_t __user *argp = (void __user *)arg;
713 drm_agp_info32_t i32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000714 struct drm_agp_info __user *info;
Dave Airlie9a186642005-06-23 21:29:18 +1000715 int err;
716
717 info = compat_alloc_user_space(sizeof(*info));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300718 if (!info)
Dave Airlie9a186642005-06-23 21:29:18 +1000719 return -EFAULT;
720
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000721 err = drm_ioctl(file, DRM_IOCTL_AGP_INFO, (unsigned long)info);
Dave Airlie9a186642005-06-23 21:29:18 +1000722 if (err)
723 return err;
724
725 if (__get_user(i32.agp_version_major, &info->agp_version_major)
726 || __get_user(i32.agp_version_minor, &info->agp_version_minor)
727 || __get_user(i32.mode, &info->mode)
728 || __get_user(i32.aperture_base, &info->aperture_base)
729 || __get_user(i32.aperture_size, &info->aperture_size)
730 || __get_user(i32.memory_allowed, &info->memory_allowed)
731 || __get_user(i32.memory_used, &info->memory_used)
732 || __get_user(i32.id_vendor, &info->id_vendor)
733 || __get_user(i32.id_device, &info->id_device))
734 return -EFAULT;
735
736 if (copy_to_user(argp, &i32, sizeof(i32)))
737 return -EFAULT;
738
739 return 0;
740}
741
742typedef struct drm_agp_buffer32 {
743 u32 size; /**< In bytes -- will round to page boundary */
744 u32 handle; /**< Used for binding / unbinding */
745 u32 type; /**< Type of memory to allocate */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000746 u32 physical; /**< Physical used by i810 */
Dave Airlie9a186642005-06-23 21:29:18 +1000747} drm_agp_buffer32_t;
748
749static int compat_drm_agp_alloc(struct file *file, unsigned int cmd,
750 unsigned long arg)
751{
752 drm_agp_buffer32_t __user *argp = (void __user *)arg;
753 drm_agp_buffer32_t req32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000754 struct drm_agp_buffer __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000755 int err;
756
757 if (copy_from_user(&req32, argp, sizeof(req32)))
758 return -EFAULT;
759
760 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300761 if (!request
Dave Airlie9a186642005-06-23 21:29:18 +1000762 || __put_user(req32.size, &request->size)
763 || __put_user(req32.type, &request->type))
764 return -EFAULT;
765
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000766 err = drm_ioctl(file, DRM_IOCTL_AGP_ALLOC, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000767 if (err)
768 return err;
769
770 if (__get_user(req32.handle, &request->handle)
771 || __get_user(req32.physical, &request->physical)
772 || copy_to_user(argp, &req32, sizeof(req32))) {
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000773 drm_ioctl(file, DRM_IOCTL_AGP_FREE, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000774 return -EFAULT;
775 }
776
777 return 0;
778}
779
780static int compat_drm_agp_free(struct file *file, unsigned int cmd,
781 unsigned long arg)
782{
783 drm_agp_buffer32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000784 struct drm_agp_buffer __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000785 u32 handle;
786
787 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300788 if (!request
Dave Airlie9a186642005-06-23 21:29:18 +1000789 || get_user(handle, &argp->handle)
790 || __put_user(handle, &request->handle))
791 return -EFAULT;
792
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000793 return drm_ioctl(file, DRM_IOCTL_AGP_FREE, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000794}
795
796typedef struct drm_agp_binding32 {
797 u32 handle; /**< From drm_agp_buffer */
798 u32 offset; /**< In bytes -- will round to page boundary */
799} drm_agp_binding32_t;
800
801static int compat_drm_agp_bind(struct file *file, unsigned int cmd,
802 unsigned long arg)
803{
804 drm_agp_binding32_t __user *argp = (void __user *)arg;
805 drm_agp_binding32_t req32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000806 struct drm_agp_binding __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000807
808 if (copy_from_user(&req32, argp, sizeof(req32)))
809 return -EFAULT;
810
811 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300812 if (!request
Dave Airlie9a186642005-06-23 21:29:18 +1000813 || __put_user(req32.handle, &request->handle)
814 || __put_user(req32.offset, &request->offset))
815 return -EFAULT;
816
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000817 return drm_ioctl(file, DRM_IOCTL_AGP_BIND, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000818}
819
820static int compat_drm_agp_unbind(struct file *file, unsigned int cmd,
821 unsigned long arg)
822{
823 drm_agp_binding32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000824 struct drm_agp_binding __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000825 u32 handle;
826
827 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300828 if (!request
Dave Airlie9a186642005-06-23 21:29:18 +1000829 || get_user(handle, &argp->handle)
830 || __put_user(handle, &request->handle))
831 return -EFAULT;
832
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000833 return drm_ioctl(file, DRM_IOCTL_AGP_UNBIND, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000834}
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200835#endif /* CONFIG_AGP */
Dave Airlie9a186642005-06-23 21:29:18 +1000836
837typedef struct drm_scatter_gather32 {
838 u32 size; /**< In bytes -- will round to page boundary */
839 u32 handle; /**< Used for mapping / unmapping */
840} drm_scatter_gather32_t;
841
842static int compat_drm_sg_alloc(struct file *file, unsigned int cmd,
843 unsigned long arg)
844{
845 drm_scatter_gather32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000846 struct drm_scatter_gather __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000847 int err;
848 unsigned long x;
849
850 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300851 if (!request || !access_ok(VERIFY_WRITE, argp, sizeof(*argp))
Dave Airlie9a186642005-06-23 21:29:18 +1000852 || __get_user(x, &argp->size)
853 || __put_user(x, &request->size))
854 return -EFAULT;
855
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000856 err = drm_ioctl(file, DRM_IOCTL_SG_ALLOC, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000857 if (err)
858 return err;
859
860 /* XXX not sure about the handle conversion here... */
861 if (__get_user(x, &request->handle)
862 || __put_user(x >> PAGE_SHIFT, &argp->handle))
863 return -EFAULT;
864
865 return 0;
866}
867
868static int compat_drm_sg_free(struct file *file, unsigned int cmd,
869 unsigned long arg)
870{
871 drm_scatter_gather32_t __user *argp = (void __user *)arg;
Dave Airlieff4135a2007-07-16 13:53:57 +1000872 struct drm_scatter_gather __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000873 unsigned long x;
874
875 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300876 if (!request || !access_ok(VERIFY_WRITE, argp, sizeof(*argp))
Dave Airlie9a186642005-06-23 21:29:18 +1000877 || __get_user(x, &argp->handle)
878 || __put_user(x << PAGE_SHIFT, &request->handle))
879 return -EFAULT;
880
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000881 return drm_ioctl(file, DRM_IOCTL_SG_FREE, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000882}
883
David Miller09e40d62009-02-18 01:35:21 -0800884#if defined(CONFIG_X86) || defined(CONFIG_IA64)
Eric Anholt49568872008-10-21 11:38:50 -0700885typedef struct drm_update_draw32 {
886 drm_drawable_t handle;
887 unsigned int type;
888 unsigned int num;
889 /* 64-bit version has a 32-bit pad here */
890 u64 data; /**< Pointer */
891} __attribute__((packed)) drm_update_draw32_t;
892
893static int compat_drm_update_draw(struct file *file, unsigned int cmd,
894 unsigned long arg)
895{
896 drm_update_draw32_t update32;
897 struct drm_update_draw __user *request;
898 int err;
899
900 if (copy_from_user(&update32, (void __user *)arg, sizeof(update32)))
901 return -EFAULT;
902
903 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300904 if (!request ||
Eric Anholt49568872008-10-21 11:38:50 -0700905 __put_user(update32.handle, &request->handle) ||
906 __put_user(update32.type, &request->type) ||
907 __put_user(update32.num, &request->num) ||
908 __put_user(update32.data, &request->data))
909 return -EFAULT;
910
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000911 err = drm_ioctl(file, DRM_IOCTL_UPDATE_DRAW, (unsigned long)request);
Eric Anholt49568872008-10-21 11:38:50 -0700912 return err;
913}
David Miller09e40d62009-02-18 01:35:21 -0800914#endif
Eric Anholt49568872008-10-21 11:38:50 -0700915
Dave Airlie9a186642005-06-23 21:29:18 +1000916struct drm_wait_vblank_request32 {
Dave Airlieff4135a2007-07-16 13:53:57 +1000917 enum drm_vblank_seq_type type;
Dave Airlie9a186642005-06-23 21:29:18 +1000918 unsigned int sequence;
919 u32 signal;
920};
921
922struct drm_wait_vblank_reply32 {
Dave Airlieff4135a2007-07-16 13:53:57 +1000923 enum drm_vblank_seq_type type;
Dave Airlie9a186642005-06-23 21:29:18 +1000924 unsigned int sequence;
925 s32 tval_sec;
926 s32 tval_usec;
927};
928
929typedef union drm_wait_vblank32 {
930 struct drm_wait_vblank_request32 request;
931 struct drm_wait_vblank_reply32 reply;
932} drm_wait_vblank32_t;
933
934static int compat_drm_wait_vblank(struct file *file, unsigned int cmd,
935 unsigned long arg)
936{
937 drm_wait_vblank32_t __user *argp = (void __user *)arg;
938 drm_wait_vblank32_t req32;
Dave Airlieff4135a2007-07-16 13:53:57 +1000939 union drm_wait_vblank __user *request;
Dave Airlie9a186642005-06-23 21:29:18 +1000940 int err;
941
942 if (copy_from_user(&req32, argp, sizeof(req32)))
943 return -EFAULT;
944
945 request = compat_alloc_user_space(sizeof(*request));
Jarkko Sakkinen1f96bee2015-07-03 14:17:29 +0300946 if (!request
Dave Airlie9a186642005-06-23 21:29:18 +1000947 || __put_user(req32.request.type, &request->request.type)
948 || __put_user(req32.request.sequence, &request->request.sequence)
949 || __put_user(req32.request.signal, &request->request.signal))
950 return -EFAULT;
951
Arnd Bergmanned8b6702009-12-16 22:17:09 +0000952 err = drm_ioctl(file, DRM_IOCTL_WAIT_VBLANK, (unsigned long)request);
Dave Airlie9a186642005-06-23 21:29:18 +1000953 if (err)
954 return err;
955
956 if (__get_user(req32.reply.type, &request->reply.type)
957 || __get_user(req32.reply.sequence, &request->reply.sequence)
958 || __get_user(req32.reply.tval_sec, &request->reply.tval_sec)
959 || __get_user(req32.reply.tval_usec, &request->reply.tval_usec))
960 return -EFAULT;
961
962 if (copy_to_user(argp, &req32, sizeof(req32)))
963 return -EFAULT;
964
965 return 0;
966}
967
Kristian H. Kristensena9885882016-09-13 14:20:45 -0700968#if defined(CONFIG_X86) || defined(CONFIG_IA64)
Tvrtko Ursulinc631d5f2015-07-14 11:13:08 +0100969typedef struct drm_mode_fb_cmd232 {
970 u32 fb_id;
971 u32 width;
972 u32 height;
973 u32 pixel_format;
974 u32 flags;
975 u32 handles[4];
976 u32 pitches[4];
977 u32 offsets[4];
978 u64 modifier[4];
979} __attribute__((packed)) drm_mode_fb_cmd232_t;
980
981static int compat_drm_mode_addfb2(struct file *file, unsigned int cmd,
982 unsigned long arg)
983{
984 struct drm_mode_fb_cmd232 __user *argp = (void __user *)arg;
985 struct drm_mode_fb_cmd232 req32;
986 struct drm_mode_fb_cmd2 __user *req64;
987 int i;
988 int err;
989
990 if (copy_from_user(&req32, argp, sizeof(req32)))
991 return -EFAULT;
992
993 req64 = compat_alloc_user_space(sizeof(*req64));
994
995 if (!access_ok(VERIFY_WRITE, req64, sizeof(*req64))
996 || __put_user(req32.width, &req64->width)
997 || __put_user(req32.height, &req64->height)
998 || __put_user(req32.pixel_format, &req64->pixel_format)
999 || __put_user(req32.flags, &req64->flags))
1000 return -EFAULT;
1001
1002 for (i = 0; i < 4; i++) {
1003 if (__put_user(req32.handles[i], &req64->handles[i]))
1004 return -EFAULT;
1005 if (__put_user(req32.pitches[i], &req64->pitches[i]))
1006 return -EFAULT;
1007 if (__put_user(req32.offsets[i], &req64->offsets[i]))
1008 return -EFAULT;
1009 if (__put_user(req32.modifier[i], &req64->modifier[i]))
1010 return -EFAULT;
1011 }
1012
1013 err = drm_ioctl(file, DRM_IOCTL_MODE_ADDFB2, (unsigned long)req64);
1014 if (err)
1015 return err;
1016
1017 if (__get_user(req32.fb_id, &req64->fb_id))
1018 return -EFAULT;
1019
1020 if (copy_to_user(argp, &req32, sizeof(req32)))
1021 return -EFAULT;
1022
1023 return 0;
1024}
Kristian H. Kristensena9885882016-09-13 14:20:45 -07001025#endif
Tvrtko Ursulinc631d5f2015-07-14 11:13:08 +01001026
Al Viro9f43e542017-05-24 13:59:22 -04001027static struct {
1028 drm_ioctl_compat_t *fn;
1029 char *name;
1030} drm_compat_ioctls[] = {
Al Viroff22ff92017-05-24 13:42:27 -04001031#define DRM_IOCTL32_DEF(n, f) [DRM_IOCTL_NR(n##32)] = {.fn = f, .name = #n}
Al Viro012c6742017-05-24 14:11:03 -04001032 DRM_IOCTL32_DEF(DRM_IOCTL_VERSION, compat_drm_version),
Al Viro17e3dad2017-05-24 14:15:20 -04001033 DRM_IOCTL32_DEF(DRM_IOCTL_GET_UNIQUE, compat_drm_getunique),
Al Viro8c4fe492017-05-24 14:26:39 -04001034 DRM_IOCTL32_DEF(DRM_IOCTL_GET_MAP, compat_drm_getmap),
Al Viro9e926622017-05-24 19:10:32 -04001035 DRM_IOCTL32_DEF(DRM_IOCTL_GET_CLIENT, compat_drm_getclient),
Al Viro9f43e542017-05-24 13:59:22 -04001036 [DRM_IOCTL_NR(DRM_IOCTL_GET_STATS32)].fn = compat_drm_getstats,
Al Viro56c275c2017-05-24 14:20:21 -04001037 DRM_IOCTL32_DEF(DRM_IOCTL_SET_UNIQUE, compat_drm_setunique),
Al Viro9f43e542017-05-24 13:59:22 -04001038 [DRM_IOCTL_NR(DRM_IOCTL_ADD_MAP32)].fn = compat_drm_addmap,
Al Viroff22ff92017-05-24 13:42:27 -04001039 DRM_IOCTL32_DEF(DRM_IOCTL_ADD_BUFS, compat_drm_addbufs),
Al Viro9f43e542017-05-24 13:59:22 -04001040 [DRM_IOCTL_NR(DRM_IOCTL_MARK_BUFS32)].fn = compat_drm_markbufs,
Al Viro5c7640a2017-05-24 17:54:09 -04001041 DRM_IOCTL32_DEF(DRM_IOCTL_INFO_BUFS, compat_drm_infobufs),
Al Viro9f43e542017-05-24 13:59:22 -04001042 [DRM_IOCTL_NR(DRM_IOCTL_MAP_BUFS32)].fn = compat_drm_mapbufs,
1043 [DRM_IOCTL_NR(DRM_IOCTL_FREE_BUFS32)].fn = compat_drm_freebufs,
1044 [DRM_IOCTL_NR(DRM_IOCTL_RM_MAP32)].fn = compat_drm_rmmap,
1045 [DRM_IOCTL_NR(DRM_IOCTL_SET_SAREA_CTX32)].fn = compat_drm_setsareactx,
1046 [DRM_IOCTL_NR(DRM_IOCTL_GET_SAREA_CTX32)].fn = compat_drm_getsareactx,
1047 [DRM_IOCTL_NR(DRM_IOCTL_RES_CTX32)].fn = compat_drm_resctx,
1048 [DRM_IOCTL_NR(DRM_IOCTL_DMA32)].fn = compat_drm_dma,
Daniel Vettera7fb8a22015-09-09 16:45:52 +02001049#if IS_ENABLED(CONFIG_AGP)
Al Viro9f43e542017-05-24 13:59:22 -04001050 [DRM_IOCTL_NR(DRM_IOCTL_AGP_ENABLE32)].fn = compat_drm_agp_enable,
1051 [DRM_IOCTL_NR(DRM_IOCTL_AGP_INFO32)].fn = compat_drm_agp_info,
1052 [DRM_IOCTL_NR(DRM_IOCTL_AGP_ALLOC32)].fn = compat_drm_agp_alloc,
1053 [DRM_IOCTL_NR(DRM_IOCTL_AGP_FREE32)].fn = compat_drm_agp_free,
1054 [DRM_IOCTL_NR(DRM_IOCTL_AGP_BIND32)].fn = compat_drm_agp_bind,
1055 [DRM_IOCTL_NR(DRM_IOCTL_AGP_UNBIND32)].fn = compat_drm_agp_unbind,
Dave Airlie9a186642005-06-23 21:29:18 +10001056#endif
Al Viro9f43e542017-05-24 13:59:22 -04001057 [DRM_IOCTL_NR(DRM_IOCTL_SG_ALLOC32)].fn = compat_drm_sg_alloc,
1058 [DRM_IOCTL_NR(DRM_IOCTL_SG_FREE32)].fn = compat_drm_sg_free,
David Miller09e40d62009-02-18 01:35:21 -08001059#if defined(CONFIG_X86) || defined(CONFIG_IA64)
Al Viro9f43e542017-05-24 13:59:22 -04001060 [DRM_IOCTL_NR(DRM_IOCTL_UPDATE_DRAW32)].fn = compat_drm_update_draw,
David Miller09e40d62009-02-18 01:35:21 -08001061#endif
Al Viro9f43e542017-05-24 13:59:22 -04001062 [DRM_IOCTL_NR(DRM_IOCTL_WAIT_VBLANK32)].fn = compat_drm_wait_vblank,
Kristian H. Kristensena9885882016-09-13 14:20:45 -07001063#if defined(CONFIG_X86) || defined(CONFIG_IA64)
Al Viro9f43e542017-05-24 13:59:22 -04001064 [DRM_IOCTL_NR(DRM_IOCTL_MODE_ADDFB232)].fn = compat_drm_mode_addfb2,
Kristian H. Kristensena9885882016-09-13 14:20:45 -07001065#endif
Dave Airlie9a186642005-06-23 21:29:18 +10001066};
1067
1068/**
Daniel Vetter26409812017-04-04 11:52:57 +02001069 * drm_compat_ioctl - 32bit IOCTL compatibility handler for DRM drivers
1070 * @filp: file this ioctl is called on
1071 * @cmd: ioctl cmd number
1072 * @arg: user argument
Dave Airlie9a186642005-06-23 21:29:18 +10001073 *
Daniel Vetter26409812017-04-04 11:52:57 +02001074 * Compatibility handler for 32 bit userspace running on 64 kernels. All actual
1075 * IOCTL handling is forwarded to drm_ioctl(), while marshalling structures as
1076 * appropriate. Note that this only handles DRM core IOCTLs, if the driver has
1077 * botched IOCTL itself, it must handle those by wrapping this function.
1078 *
1079 * Returns:
1080 * Zero on success, negative error code on failure.
Dave Airlie9a186642005-06-23 21:29:18 +10001081 */
1082long drm_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1083{
1084 unsigned int nr = DRM_IOCTL_NR(cmd);
1085 drm_ioctl_compat_t *fn;
1086 int ret;
1087
Ian Romanick7ffa05e2007-11-22 17:02:08 +10001088 /* Assume that ioctls without an explicit compat routine will just
1089 * work. This may not always be a good assumption, but it's better
1090 * than always failing.
1091 */
Dave Airlie3d774612006-08-07 20:07:43 +10001092 if (nr >= ARRAY_SIZE(drm_compat_ioctls))
Arnd Bergmanned8b6702009-12-16 22:17:09 +00001093 return drm_ioctl(filp, cmd, arg);
Dave Airlie9a186642005-06-23 21:29:18 +10001094
Al Viro9f43e542017-05-24 13:59:22 -04001095 fn = drm_compat_ioctls[nr].fn;
1096 if (!fn)
1097 return drm_ioctl(filp, cmd, arg);
Dave Airlie9a186642005-06-23 21:29:18 +10001098
Al Viro9f43e542017-05-24 13:59:22 -04001099 if (drm_compat_ioctls[nr].name) {
1100 struct drm_file *file_priv = filp->private_data;
1101 DRM_DEBUG("pid=%d, dev=0x%lx, auth=%d, %s\n",
1102 task_pid_nr(current),
1103 (long)old_encode_dev(file_priv->minor->kdev->devt),
1104 file_priv->authenticated,
1105 drm_compat_ioctls[nr].name);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001106 ret = (*fn) (filp, cmd, arg);
Al Viro9f43e542017-05-24 13:59:22 -04001107 if (ret)
1108 DRM_DEBUG("ret = %d\n", ret);
1109 } else {
1110 ret = (*fn) (filp, cmd, arg);
1111 }
Dave Airlie9a186642005-06-23 21:29:18 +10001112 return ret;
1113}
Dave Airlie9a186642005-06-23 21:29:18 +10001114EXPORT_SYMBOL(drm_compat_ioctl);