drm_ioctl.c 26 KB

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