1 /*
2 * Created: Fri Jan 8 09:01:26 1999 by faith@valinux.com
3 *
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * All Rights Reserved.
7 *
8 * Author Rickard E. (Rik) Faith <faith@valinux.com>
9 * Author Gareth Hughes <gareth@valinux.com>
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 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31 #include <drm/drmP.h>
32 #include <drm/drm_core.h>
33 #include "drm_legacy.h"
34 #include "drm_internal.h"
35 #include "drm_crtc_internal.h"
36
37 #include <linux/pci.h>
38 #include <linux/export.h>
39 #include <linux/nospec.h>
40
41 static int drm_version(struct drm_device *dev, void *data,
42 struct drm_file *file_priv);
43
44 /*
45 * Get the bus id.
46 *
47 * \param inode device inode.
48 * \param file_priv DRM file private.
49 * \param cmd command.
50 * \param arg user argument, pointing to a drm_unique structure.
51 * \return zero on success or a negative number on failure.
52 *
53 * Copies the bus id from drm_device::unique into user space.
54 */
drm_getunique(struct drm_device * dev,void * data,struct drm_file * file_priv)55 static int drm_getunique(struct drm_device *dev, void *data,
56 struct drm_file *file_priv)
57 {
58 struct drm_unique *u = data;
59 struct drm_master *master = file_priv->master;
60
61 if (u->unique_len >= master->unique_len) {
62 if (copy_to_user(u->unique, master->unique, master->unique_len))
63 return -EFAULT;
64 }
65 u->unique_len = master->unique_len;
66
67 return 0;
68 }
69
70 static void
drm_unset_busid(struct drm_device * dev,struct drm_master * master)71 drm_unset_busid(struct drm_device *dev,
72 struct drm_master *master)
73 {
74 kfree(master->unique);
75 master->unique = NULL;
76 master->unique_len = 0;
77 }
78
79 /*
80 * Set the bus id.
81 *
82 * \param inode device inode.
83 * \param file_priv DRM file private.
84 * \param cmd command.
85 * \param arg user argument, pointing to a drm_unique structure.
86 * \return zero on success or a negative number on failure.
87 *
88 * Copies the bus id from userspace into drm_device::unique, and verifies that
89 * it matches the device this DRM is attached to (EINVAL otherwise). Deprecated
90 * in interface version 1.1 and will return EBUSY when setversion has requested
91 * version 1.1 or greater. Also note that KMS is all version 1.1 and later and
92 * UMS was only ever supported on pci devices.
93 */
drm_setunique(struct drm_device * dev,void * data,struct drm_file * file_priv)94 static int drm_setunique(struct drm_device *dev, void *data,
95 struct drm_file *file_priv)
96 {
97 struct drm_unique *u = data;
98 struct drm_master *master = file_priv->master;
99 int ret;
100
101 if (master->unique_len || master->unique)
102 return -EBUSY;
103
104 if (!u->unique_len || u->unique_len > 1024)
105 return -EINVAL;
106
107 if (drm_core_check_feature(dev, DRIVER_MODESET))
108 return 0;
109
110 if (WARN_ON(!dev->pdev))
111 return -EINVAL;
112
113 ret = drm_pci_set_unique(dev, master, u);
114 if (ret)
115 goto err;
116
117 return 0;
118
119 err:
120 drm_unset_busid(dev, master);
121 return ret;
122 }
123
drm_set_busid(struct drm_device * dev,struct drm_file * file_priv)124 static int drm_set_busid(struct drm_device *dev, struct drm_file *file_priv)
125 {
126 struct drm_master *master = file_priv->master;
127 int ret;
128
129 if (master->unique != NULL)
130 drm_unset_busid(dev, master);
131
132 if (dev->driver->set_busid) {
133 ret = dev->driver->set_busid(dev, master);
134 if (ret) {
135 drm_unset_busid(dev, master);
136 return ret;
137 }
138 } else {
139 if (WARN(dev->unique == NULL,
140 "No drm_driver.set_busid() implementation provided by "
141 "%ps. Use drm_dev_set_unique() to set the unique "
142 "name explicitly.", dev->driver))
143 return -EINVAL;
144
145 master->unique = kstrdup(dev->unique, GFP_KERNEL);
146 if (master->unique)
147 master->unique_len = strlen(dev->unique);
148 }
149
150 return 0;
151 }
152
153 /*
154 * Get a mapping information.
155 *
156 * \param inode device inode.
157 * \param file_priv DRM file private.
158 * \param cmd command.
159 * \param arg user argument, pointing to a drm_map structure.
160 *
161 * \return zero on success or a negative number on failure.
162 *
163 * Searches for the mapping with the specified offset and copies its information
164 * into userspace
165 */
drm_getmap(struct drm_device * dev,void * data,struct drm_file * file_priv)166 static int drm_getmap(struct drm_device *dev, void *data,
167 struct drm_file *file_priv)
168 {
169 struct drm_map *map = data;
170 struct drm_map_list *r_list = NULL;
171 struct list_head *list;
172 int idx;
173 int i;
174
175 idx = map->offset;
176 if (idx < 0)
177 return -EINVAL;
178
179 i = 0;
180 mutex_lock(&dev->struct_mutex);
181 list_for_each(list, &dev->maplist) {
182 if (i == idx) {
183 r_list = list_entry(list, struct drm_map_list, head);
184 break;
185 }
186 i++;
187 }
188 if (!r_list || !r_list->map) {
189 mutex_unlock(&dev->struct_mutex);
190 return -EINVAL;
191 }
192
193 map->offset = r_list->map->offset;
194 map->size = r_list->map->size;
195 map->type = r_list->map->type;
196 map->flags = r_list->map->flags;
197 map->handle = (void *)(unsigned long) r_list->user_token;
198 map->mtrr = arch_phys_wc_index(r_list->map->mtrr);
199
200 mutex_unlock(&dev->struct_mutex);
201
202 return 0;
203 }
204
205 /*
206 * Get client information.
207 *
208 * \param inode device inode.
209 * \param file_priv DRM file private.
210 * \param cmd command.
211 * \param arg user argument, pointing to a drm_client structure.
212 *
213 * \return zero on success or a negative number on failure.
214 *
215 * Searches for the client with the specified index and copies its information
216 * into userspace
217 */
drm_getclient(struct drm_device * dev,void * data,struct drm_file * file_priv)218 static int drm_getclient(struct drm_device *dev, void *data,
219 struct drm_file *file_priv)
220 {
221 struct drm_client *client = data;
222
223 /*
224 * Hollowed-out getclient ioctl to keep some dead old drm tests/tools
225 * not breaking completely. Userspace tools stop enumerating one they
226 * get -EINVAL, hence this is the return value we need to hand back for
227 * no clients tracked.
228 *
229 * Unfortunately some clients (*cough* libva *cough*) use this in a fun
230 * attempt to figure out whether they're authenticated or not. Since
231 * that's the only thing they care about, give it to the directly
232 * instead of walking one giant list.
233 */
234 if (client->idx == 0) {
235 client->auth = file_priv->authenticated;
236 client->pid = pid_vnr(file_priv->pid);
237 client->uid = from_kuid_munged(current_user_ns(),
238 file_priv->uid);
239 client->magic = 0;
240 client->iocs = 0;
241
242 return 0;
243 } else {
244 return -EINVAL;
245 }
246 }
247
248 /*
249 * Get statistics information.
250 *
251 * \param inode device inode.
252 * \param file_priv DRM file private.
253 * \param cmd command.
254 * \param arg user argument, pointing to a drm_stats structure.
255 *
256 * \return zero on success or a negative number on failure.
257 */
drm_getstats(struct drm_device * dev,void * data,struct drm_file * file_priv)258 static int drm_getstats(struct drm_device *dev, void *data,
259 struct drm_file *file_priv)
260 {
261 struct drm_stats *stats = data;
262
263 /* Clear stats to prevent userspace from eating its stack garbage. */
264 memset(stats, 0, sizeof(*stats));
265
266 return 0;
267 }
268
269 /*
270 * Get device/driver capabilities
271 */
drm_getcap(struct drm_device * dev,void * data,struct drm_file * file_priv)272 static int drm_getcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
273 {
274 struct drm_get_cap *req = data;
275
276 req->value = 0;
277 switch (req->capability) {
278 case DRM_CAP_DUMB_BUFFER:
279 if (dev->driver->dumb_create)
280 req->value = 1;
281 break;
282 case DRM_CAP_VBLANK_HIGH_CRTC:
283 req->value = 1;
284 break;
285 case DRM_CAP_DUMB_PREFERRED_DEPTH:
286 req->value = dev->mode_config.preferred_depth;
287 break;
288 case DRM_CAP_DUMB_PREFER_SHADOW:
289 req->value = dev->mode_config.prefer_shadow;
290 break;
291 case DRM_CAP_PRIME:
292 req->value |= dev->driver->prime_fd_to_handle ? DRM_PRIME_CAP_IMPORT : 0;
293 req->value |= dev->driver->prime_handle_to_fd ? DRM_PRIME_CAP_EXPORT : 0;
294 break;
295 case DRM_CAP_TIMESTAMP_MONOTONIC:
296 req->value = drm_timestamp_monotonic;
297 break;
298 case DRM_CAP_ASYNC_PAGE_FLIP:
299 req->value = dev->mode_config.async_page_flip;
300 break;
301 case DRM_CAP_CURSOR_WIDTH:
302 if (dev->mode_config.cursor_width)
303 req->value = dev->mode_config.cursor_width;
304 else
305 req->value = 64;
306 break;
307 case DRM_CAP_CURSOR_HEIGHT:
308 if (dev->mode_config.cursor_height)
309 req->value = dev->mode_config.cursor_height;
310 else
311 req->value = 64;
312 break;
313 case DRM_CAP_ADDFB2_MODIFIERS:
314 req->value = dev->mode_config.allow_fb_modifiers;
315 break;
316 default:
317 return -EINVAL;
318 }
319 return 0;
320 }
321
322 /*
323 * Set device/driver capabilities
324 */
325 static int
drm_setclientcap(struct drm_device * dev,void * data,struct drm_file * file_priv)326 drm_setclientcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
327 {
328 struct drm_set_client_cap *req = data;
329
330 switch (req->capability) {
331 case DRM_CLIENT_CAP_STEREO_3D:
332 if (req->value > 1)
333 return -EINVAL;
334 file_priv->stereo_allowed = req->value;
335 break;
336 case DRM_CLIENT_CAP_UNIVERSAL_PLANES:
337 if (req->value > 1)
338 return -EINVAL;
339 file_priv->universal_planes = req->value;
340 break;
341 case DRM_CLIENT_CAP_ATOMIC:
342 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
343 return -EINVAL;
344 if (req->value > 1)
345 return -EINVAL;
346 file_priv->atomic = req->value;
347 file_priv->universal_planes = req->value;
348 break;
349 default:
350 return -EINVAL;
351 }
352
353 return 0;
354 }
355
356 /*
357 * Setversion ioctl.
358 *
359 * \param inode device inode.
360 * \param file_priv DRM file private.
361 * \param cmd command.
362 * \param arg user argument, pointing to a drm_lock structure.
363 * \return zero on success or negative number on failure.
364 *
365 * Sets the requested interface version
366 */
drm_setversion(struct drm_device * dev,void * data,struct drm_file * file_priv)367 static int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
368 {
369 struct drm_set_version *sv = data;
370 int if_version, retcode = 0;
371
372 if (sv->drm_di_major != -1) {
373 if (sv->drm_di_major != DRM_IF_MAJOR ||
374 sv->drm_di_minor < 0 || sv->drm_di_minor > DRM_IF_MINOR) {
375 retcode = -EINVAL;
376 goto done;
377 }
378 if_version = DRM_IF_VERSION(sv->drm_di_major,
379 sv->drm_di_minor);
380 dev->if_version = max(if_version, dev->if_version);
381 if (sv->drm_di_minor >= 1) {
382 /*
383 * Version 1.1 includes tying of DRM to specific device
384 * Version 1.4 has proper PCI domain support
385 */
386 retcode = drm_set_busid(dev, file_priv);
387 if (retcode)
388 goto done;
389 }
390 }
391
392 if (sv->drm_dd_major != -1) {
393 if (sv->drm_dd_major != dev->driver->major ||
394 sv->drm_dd_minor < 0 || sv->drm_dd_minor >
395 dev->driver->minor) {
396 retcode = -EINVAL;
397 goto done;
398 }
399 }
400
401 done:
402 sv->drm_di_major = DRM_IF_MAJOR;
403 sv->drm_di_minor = DRM_IF_MINOR;
404 sv->drm_dd_major = dev->driver->major;
405 sv->drm_dd_minor = dev->driver->minor;
406
407 return retcode;
408 }
409
410 /**
411 * drm_noop - DRM no-op ioctl implemntation
412 * @dev: DRM device for the ioctl
413 * @data: data pointer for the ioctl
414 * @file_priv: DRM file for the ioctl call
415 *
416 * This no-op implementation for drm ioctls is useful for deprecated
417 * functionality where we can't return a failure code because existing userspace
418 * checks the result of the ioctl, but doesn't care about the action.
419 *
420 * Always returns successfully with 0.
421 */
drm_noop(struct drm_device * dev,void * data,struct drm_file * file_priv)422 int drm_noop(struct drm_device *dev, void *data,
423 struct drm_file *file_priv)
424 {
425 DRM_DEBUG("\n");
426 return 0;
427 }
428 EXPORT_SYMBOL(drm_noop);
429
430 /**
431 * drm_invalid_op - DRM invalid ioctl implemntation
432 * @dev: DRM device for the ioctl
433 * @data: data pointer for the ioctl
434 * @file_priv: DRM file for the ioctl call
435 *
436 * This no-op implementation for drm ioctls is useful for deprecated
437 * functionality where we really don't want to allow userspace to call the ioctl
438 * any more. This is the case for old ums interfaces for drivers that
439 * transitioned to kms gradually and so kept the old legacy tables around. This
440 * only applies to radeon and i915 kms drivers, other drivers shouldn't need to
441 * use this function.
442 *
443 * Always fails with a return value of -EINVAL.
444 */
drm_invalid_op(struct drm_device * dev,void * data,struct drm_file * file_priv)445 int drm_invalid_op(struct drm_device *dev, void *data,
446 struct drm_file *file_priv)
447 {
448 return -EINVAL;
449 }
450 EXPORT_SYMBOL(drm_invalid_op);
451
452 /*
453 * Copy and IOCTL return string to user space
454 */
drm_copy_field(char __user * buf,size_t * buf_len,const char * value)455 static int drm_copy_field(char __user *buf, size_t *buf_len, const char *value)
456 {
457 int len;
458
459 /* don't overflow userbuf */
460 len = strlen(value);
461 if (len > *buf_len)
462 len = *buf_len;
463
464 /* let userspace know exact length of driver value (which could be
465 * larger than the userspace-supplied buffer) */
466 *buf_len = strlen(value);
467
468 /* finally, try filling in the userbuf */
469 if (len && buf)
470 if (copy_to_user(buf, value, len))
471 return -EFAULT;
472 return 0;
473 }
474
475 /*
476 * Get version information
477 *
478 * \param inode device inode.
479 * \param filp file pointer.
480 * \param cmd command.
481 * \param arg user argument, pointing to a drm_version structure.
482 * \return zero on success or negative number on failure.
483 *
484 * Fills in the version information in \p arg.
485 */
drm_version(struct drm_device * dev,void * data,struct drm_file * file_priv)486 static int drm_version(struct drm_device *dev, void *data,
487 struct drm_file *file_priv)
488 {
489 struct drm_version *version = data;
490 int err;
491
492 version->version_major = dev->driver->major;
493 version->version_minor = dev->driver->minor;
494 version->version_patchlevel = dev->driver->patchlevel;
495 err = drm_copy_field(version->name, &version->name_len,
496 dev->driver->name);
497 if (!err)
498 err = drm_copy_field(version->date, &version->date_len,
499 dev->driver->date);
500 if (!err)
501 err = drm_copy_field(version->desc, &version->desc_len,
502 dev->driver->desc);
503
504 return err;
505 }
506
507 /*
508 * drm_ioctl_permit - Check ioctl permissions against caller
509 *
510 * @flags: ioctl permission flags.
511 * @file_priv: Pointer to struct drm_file identifying the caller.
512 *
513 * Checks whether the caller is allowed to run an ioctl with the
514 * indicated permissions. If so, returns zero. Otherwise returns an
515 * error code suitable for ioctl return.
516 */
drm_ioctl_permit(u32 flags,struct drm_file * file_priv)517 int drm_ioctl_permit(u32 flags, struct drm_file *file_priv)
518 {
519 /* ROOT_ONLY is only for CAP_SYS_ADMIN */
520 if (unlikely((flags & DRM_ROOT_ONLY) && !capable(CAP_SYS_ADMIN)))
521 return -EACCES;
522
523 /* AUTH is only for authenticated or render client */
524 if (unlikely((flags & DRM_AUTH) && !drm_is_render_client(file_priv) &&
525 !file_priv->authenticated))
526 return -EACCES;
527
528 /* MASTER is only for master or control clients */
529 if (unlikely((flags & DRM_MASTER) && !file_priv->is_master &&
530 !drm_is_control_client(file_priv)))
531 return -EACCES;
532
533 /* Control clients must be explicitly allowed */
534 if (unlikely(!(flags & DRM_CONTROL_ALLOW) &&
535 drm_is_control_client(file_priv)))
536 return -EACCES;
537
538 /* Render clients must be explicitly allowed */
539 if (unlikely(!(flags & DRM_RENDER_ALLOW) &&
540 drm_is_render_client(file_priv)))
541 return -EACCES;
542
543 return 0;
544 }
545 EXPORT_SYMBOL(drm_ioctl_permit);
546
547 #define DRM_IOCTL_DEF(ioctl, _func, _flags) \
548 [DRM_IOCTL_NR(ioctl)] = { \
549 .cmd = ioctl, \
550 .func = _func, \
551 .flags = _flags, \
552 .name = #ioctl \
553 }
554
555 /* Ioctl table */
556 static const struct drm_ioctl_desc drm_ioctls[] = {
557 DRM_IOCTL_DEF(DRM_IOCTL_VERSION, drm_version,
558 DRM_UNLOCKED|DRM_RENDER_ALLOW|DRM_CONTROL_ALLOW),
559 DRM_IOCTL_DEF(DRM_IOCTL_GET_UNIQUE, drm_getunique, 0),
560 DRM_IOCTL_DEF(DRM_IOCTL_GET_MAGIC, drm_getmagic, 0),
561 DRM_IOCTL_DEF(DRM_IOCTL_IRQ_BUSID, drm_irq_by_busid, DRM_MASTER|DRM_ROOT_ONLY),
562 DRM_IOCTL_DEF(DRM_IOCTL_GET_MAP, drm_getmap, DRM_UNLOCKED),
563 DRM_IOCTL_DEF(DRM_IOCTL_GET_CLIENT, drm_getclient, DRM_UNLOCKED),
564 DRM_IOCTL_DEF(DRM_IOCTL_GET_STATS, drm_getstats, DRM_UNLOCKED),
565 DRM_IOCTL_DEF(DRM_IOCTL_GET_CAP, drm_getcap, DRM_UNLOCKED|DRM_RENDER_ALLOW),
566 DRM_IOCTL_DEF(DRM_IOCTL_SET_CLIENT_CAP, drm_setclientcap, 0),
567 DRM_IOCTL_DEF(DRM_IOCTL_SET_VERSION, drm_setversion, DRM_MASTER),
568
569 DRM_IOCTL_DEF(DRM_IOCTL_SET_UNIQUE, drm_setunique, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
570 DRM_IOCTL_DEF(DRM_IOCTL_BLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
571 DRM_IOCTL_DEF(DRM_IOCTL_UNBLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
572 DRM_IOCTL_DEF(DRM_IOCTL_AUTH_MAGIC, drm_authmagic, DRM_AUTH|DRM_MASTER),
573
574 DRM_IOCTL_DEF(DRM_IOCTL_ADD_MAP, drm_legacy_addmap_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
575 DRM_IOCTL_DEF(DRM_IOCTL_RM_MAP, drm_legacy_rmmap_ioctl, DRM_AUTH),
576
577 DRM_IOCTL_DEF(DRM_IOCTL_SET_SAREA_CTX, drm_legacy_setsareactx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
578 DRM_IOCTL_DEF(DRM_IOCTL_GET_SAREA_CTX, drm_legacy_getsareactx, DRM_AUTH),
579
580 DRM_IOCTL_DEF(DRM_IOCTL_SET_MASTER, drm_setmaster_ioctl, DRM_ROOT_ONLY),
581 DRM_IOCTL_DEF(DRM_IOCTL_DROP_MASTER, drm_dropmaster_ioctl, DRM_ROOT_ONLY),
582
583 DRM_IOCTL_DEF(DRM_IOCTL_ADD_CTX, drm_legacy_addctx, DRM_AUTH|DRM_ROOT_ONLY),
584 DRM_IOCTL_DEF(DRM_IOCTL_RM_CTX, drm_legacy_rmctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
585 DRM_IOCTL_DEF(DRM_IOCTL_MOD_CTX, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
586 DRM_IOCTL_DEF(DRM_IOCTL_GET_CTX, drm_legacy_getctx, DRM_AUTH),
587 DRM_IOCTL_DEF(DRM_IOCTL_SWITCH_CTX, drm_legacy_switchctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
588 DRM_IOCTL_DEF(DRM_IOCTL_NEW_CTX, drm_legacy_newctx, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
589 DRM_IOCTL_DEF(DRM_IOCTL_RES_CTX, drm_legacy_resctx, DRM_AUTH),
590
591 DRM_IOCTL_DEF(DRM_IOCTL_ADD_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
592 DRM_IOCTL_DEF(DRM_IOCTL_RM_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
593
594 DRM_IOCTL_DEF(DRM_IOCTL_LOCK, drm_legacy_lock, DRM_AUTH),
595 DRM_IOCTL_DEF(DRM_IOCTL_UNLOCK, drm_legacy_unlock, DRM_AUTH),
596
597 DRM_IOCTL_DEF(DRM_IOCTL_FINISH, drm_noop, DRM_AUTH),
598
599 DRM_IOCTL_DEF(DRM_IOCTL_ADD_BUFS, drm_legacy_addbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
600 DRM_IOCTL_DEF(DRM_IOCTL_MARK_BUFS, drm_legacy_markbufs, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
601 DRM_IOCTL_DEF(DRM_IOCTL_INFO_BUFS, drm_legacy_infobufs, DRM_AUTH),
602 DRM_IOCTL_DEF(DRM_IOCTL_MAP_BUFS, drm_legacy_mapbufs, DRM_AUTH),
603 DRM_IOCTL_DEF(DRM_IOCTL_FREE_BUFS, drm_legacy_freebufs, DRM_AUTH),
604 DRM_IOCTL_DEF(DRM_IOCTL_DMA, drm_legacy_dma_ioctl, DRM_AUTH),
605
606 DRM_IOCTL_DEF(DRM_IOCTL_CONTROL, drm_control, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
607
608 #if IS_ENABLED(CONFIG_AGP)
609 DRM_IOCTL_DEF(DRM_IOCTL_AGP_ACQUIRE, drm_agp_acquire_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
610 DRM_IOCTL_DEF(DRM_IOCTL_AGP_RELEASE, drm_agp_release_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
611 DRM_IOCTL_DEF(DRM_IOCTL_AGP_ENABLE, drm_agp_enable_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
612 DRM_IOCTL_DEF(DRM_IOCTL_AGP_INFO, drm_agp_info_ioctl, DRM_AUTH),
613 DRM_IOCTL_DEF(DRM_IOCTL_AGP_ALLOC, drm_agp_alloc_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
614 DRM_IOCTL_DEF(DRM_IOCTL_AGP_FREE, drm_agp_free_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
615 DRM_IOCTL_DEF(DRM_IOCTL_AGP_BIND, drm_agp_bind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
616 DRM_IOCTL_DEF(DRM_IOCTL_AGP_UNBIND, drm_agp_unbind_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
617 #endif
618
619 DRM_IOCTL_DEF(DRM_IOCTL_SG_ALLOC, drm_legacy_sg_alloc, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
620 DRM_IOCTL_DEF(DRM_IOCTL_SG_FREE, drm_legacy_sg_free, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
621
622 DRM_IOCTL_DEF(DRM_IOCTL_WAIT_VBLANK, drm_wait_vblank, DRM_UNLOCKED),
623
624 DRM_IOCTL_DEF(DRM_IOCTL_MODESET_CTL, drm_modeset_ctl, 0),
625
626 DRM_IOCTL_DEF(DRM_IOCTL_UPDATE_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
627
628 DRM_IOCTL_DEF(DRM_IOCTL_GEM_CLOSE, drm_gem_close_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
629 DRM_IOCTL_DEF(DRM_IOCTL_GEM_FLINK, drm_gem_flink_ioctl, DRM_AUTH|DRM_UNLOCKED),
630 DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH|DRM_UNLOCKED),
631
632 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETRESOURCES, drm_mode_getresources, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
633
634 DRM_IOCTL_DEF(DRM_IOCTL_PRIME_HANDLE_TO_FD, drm_prime_handle_to_fd_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
635 DRM_IOCTL_DEF(DRM_IOCTL_PRIME_FD_TO_HANDLE, drm_prime_fd_to_handle_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
636
637 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANERESOURCES, drm_mode_getplane_res, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
638 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCRTC, drm_mode_getcrtc, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
639 DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETCRTC, drm_mode_setcrtc, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
640 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANE, drm_mode_getplane, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
641 DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPLANE, drm_mode_setplane, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
642 DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR, drm_mode_cursor_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
643 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETGAMMA, drm_mode_gamma_get_ioctl, DRM_UNLOCKED),
644 DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETGAMMA, drm_mode_gamma_set_ioctl, DRM_MASTER|DRM_UNLOCKED),
645 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETENCODER, drm_mode_getencoder, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
646 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCONNECTOR, drm_mode_getconnector, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
647 DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATTACHMODE, drm_noop, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
648 DRM_IOCTL_DEF(DRM_IOCTL_MODE_DETACHMODE, drm_noop, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
649 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPERTY, drm_mode_getproperty_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
650 DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPROPERTY, drm_mode_connector_property_set_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
651 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPBLOB, drm_mode_getblob_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
652 DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETFB, drm_mode_getfb, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
653 DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB, drm_mode_addfb, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
654 DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB2, drm_mode_addfb2, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
655 DRM_IOCTL_DEF(DRM_IOCTL_MODE_RMFB, drm_mode_rmfb, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
656 DRM_IOCTL_DEF(DRM_IOCTL_MODE_PAGE_FLIP, drm_mode_page_flip_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
657 DRM_IOCTL_DEF(DRM_IOCTL_MODE_DIRTYFB, drm_mode_dirtyfb_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
658 DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATE_DUMB, drm_mode_create_dumb_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
659 DRM_IOCTL_DEF(DRM_IOCTL_MODE_MAP_DUMB, drm_mode_mmap_dumb_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
660 DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROY_DUMB, drm_mode_destroy_dumb_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
661 DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_GETPROPERTIES, drm_mode_obj_get_properties_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
662 DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_SETPROPERTY, drm_mode_obj_set_property_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
663 DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR2, drm_mode_cursor2_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
664 DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATOMIC, drm_mode_atomic_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
665 DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATEPROPBLOB, drm_mode_createblob_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
666 DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROYPROPBLOB, drm_mode_destroyblob_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
667 };
668
669 #define DRM_CORE_IOCTL_COUNT ARRAY_SIZE( drm_ioctls )
670
671 /**
672 * drm_ioctl - ioctl callback implementation for DRM drivers
673 * @filp: file this ioctl is called on
674 * @cmd: ioctl cmd number
675 * @arg: user argument
676 *
677 * Looks up the ioctl function in the ::ioctls table, checking for root
678 * previleges if so required, and dispatches to the respective function.
679 *
680 * Returns:
681 * Zero on success, negative error code on failure.
682 */
drm_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)683 long drm_ioctl(struct file *filp,
684 unsigned int cmd, unsigned long arg)
685 {
686 struct drm_file *file_priv = filp->private_data;
687 struct drm_device *dev;
688 const struct drm_ioctl_desc *ioctl = NULL;
689 drm_ioctl_t *func;
690 unsigned int nr = DRM_IOCTL_NR(cmd);
691 int retcode = -EINVAL;
692 char stack_kdata[128];
693 char *kdata = NULL;
694 unsigned int usize, asize, drv_size;
695 bool is_driver_ioctl;
696
697 dev = file_priv->minor->dev;
698
699 if (drm_device_is_unplugged(dev))
700 return -ENODEV;
701
702 is_driver_ioctl = nr >= DRM_COMMAND_BASE && nr < DRM_COMMAND_END;
703
704 if (is_driver_ioctl) {
705 /* driver ioctl */
706 unsigned int index = nr - DRM_COMMAND_BASE;
707
708 if (index >= dev->driver->num_ioctls)
709 goto err_i1;
710 index = array_index_nospec(index, dev->driver->num_ioctls);
711 ioctl = &dev->driver->ioctls[index];
712 } else {
713 /* core ioctl */
714 if (nr >= DRM_CORE_IOCTL_COUNT)
715 goto err_i1;
716 nr = array_index_nospec(nr, DRM_CORE_IOCTL_COUNT);
717 ioctl = &drm_ioctls[nr];
718 }
719
720 drv_size = _IOC_SIZE(ioctl->cmd);
721 usize = _IOC_SIZE(cmd);
722 asize = max(usize, drv_size);
723 cmd = ioctl->cmd;
724
725 DRM_DEBUG("pid=%d, dev=0x%lx, auth=%d, %s\n",
726 task_pid_nr(current),
727 (long)old_encode_dev(file_priv->minor->kdev->devt),
728 file_priv->authenticated, ioctl->name);
729
730 /* Do not trust userspace, use our own definition */
731 func = ioctl->func;
732
733 if (unlikely(!func)) {
734 DRM_DEBUG("no function\n");
735 retcode = -EINVAL;
736 goto err_i1;
737 }
738
739 retcode = drm_ioctl_permit(ioctl->flags, file_priv);
740 if (unlikely(retcode))
741 goto err_i1;
742
743 if (cmd & (IOC_IN | IOC_OUT)) {
744 if (asize <= sizeof(stack_kdata)) {
745 kdata = stack_kdata;
746 } else {
747 kdata = kmalloc(asize, GFP_KERNEL);
748 if (!kdata) {
749 retcode = -ENOMEM;
750 goto err_i1;
751 }
752 }
753 if (asize > usize)
754 memset(kdata + usize, 0, asize - usize);
755 }
756
757 if (cmd & IOC_IN) {
758 if (copy_from_user(kdata, (void __user *)arg,
759 usize) != 0) {
760 retcode = -EFAULT;
761 goto err_i1;
762 }
763 } else if (cmd & IOC_OUT) {
764 memset(kdata, 0, usize);
765 }
766
767 /* Enforce sane locking for kms driver ioctls. Core ioctls are
768 * too messy still. */
769 if ((drm_core_check_feature(dev, DRIVER_MODESET) && is_driver_ioctl) ||
770 (ioctl->flags & DRM_UNLOCKED))
771 retcode = func(dev, kdata, file_priv);
772 else {
773 mutex_lock(&drm_global_mutex);
774 retcode = func(dev, kdata, file_priv);
775 mutex_unlock(&drm_global_mutex);
776 }
777
778 if (cmd & IOC_OUT) {
779 if (copy_to_user((void __user *)arg, kdata,
780 usize) != 0)
781 retcode = -EFAULT;
782 }
783
784 err_i1:
785 if (!ioctl)
786 DRM_DEBUG("invalid ioctl: pid=%d, dev=0x%lx, auth=%d, cmd=0x%02x, nr=0x%02x\n",
787 task_pid_nr(current),
788 (long)old_encode_dev(file_priv->minor->kdev->devt),
789 file_priv->authenticated, cmd, nr);
790
791 if (kdata != stack_kdata)
792 kfree(kdata);
793 if (retcode)
794 DRM_DEBUG("ret = %d\n", retcode);
795 return retcode;
796 }
797 EXPORT_SYMBOL(drm_ioctl);
798
799 /**
800 * drm_ioctl_flags - Check for core ioctl and return ioctl permission flags
801 * @nr: ioctl number
802 * @flags: where to return the ioctl permission flags
803 *
804 * This ioctl is only used by the vmwgfx driver to augment the access checks
805 * done by the drm core and insofar a pretty decent layering violation. This
806 * shouldn't be used by any drivers.
807 *
808 * Returns:
809 * True if the @nr corresponds to a DRM core ioctl numer, false otherwise.
810 */
drm_ioctl_flags(unsigned int nr,unsigned int * flags)811 bool drm_ioctl_flags(unsigned int nr, unsigned int *flags)
812 {
813 if (nr >= DRM_COMMAND_BASE && nr < DRM_COMMAND_END)
814 return false;
815
816 if (nr >= DRM_CORE_IOCTL_COUNT)
817 return false;
818 nr = array_index_nospec(nr, DRM_CORE_IOCTL_COUNT);
819
820 *flags = drm_ioctls[nr].flags;
821 return true;
822 }
823 EXPORT_SYMBOL(drm_ioctl_flags);
824