drm_ioctl.c 27 KB

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