drm_ioctl.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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 <linux/export.h>
  31. #include <linux/nospec.h>
  32. #include <linux/pci.h>
  33. #include <linux/uaccess.h>
  34. #include <drm/drm_auth.h>
  35. #include <drm/drm_crtc.h>
  36. #include <drm/drm_drv.h>
  37. #include <drm/drm_file.h>
  38. #include <drm/drm_ioctl.h>
  39. #include <drm/drm_print.h>
  40. #include "drm_crtc_internal.h"
  41. #include "drm_internal.h"
  42. /**
  43. * DOC: getunique and setversion story
  44. *
  45. * BEWARE THE DRAGONS! MIND THE TRAPDOORS!
  46. *
  47. * In an attempt to warn anyone else who's trying to figure out what's going
  48. * on here, I'll try to summarize the story. First things first, let's clear up
  49. * the names, because the kernel internals, libdrm and the ioctls are all named
  50. * differently:
  51. *
  52. * - GET_UNIQUE ioctl, implemented by drm_getunique is wrapped up in libdrm
  53. * through the drmGetBusid function.
  54. * - The libdrm drmSetBusid function is backed by the SET_UNIQUE ioctl. All
  55. * that code is nerved in the kernel with drm_invalid_op().
  56. * - The internal set_busid kernel functions and driver callbacks are
  57. * exclusively use by the SET_VERSION ioctl, because only drm 1.0 (which is
  58. * nerved) allowed userspace to set the busid through the above ioctl.
  59. * - Other ioctls and functions involved are named consistently.
  60. *
  61. * For anyone wondering what's the difference between drm 1.1 and 1.4: Correctly
  62. * handling pci domains in the busid on ppc. Doing this correctly was only
  63. * implemented in libdrm in 2010, hence can't be nerved yet. No one knows what's
  64. * special with drm 1.2 and 1.3.
  65. *
  66. * Now the actual horror story of how device lookup in drm works. At large,
  67. * there's 2 different ways, either by busid, or by device driver name.
  68. *
  69. * Opening by busid is fairly simple:
  70. *
  71. * 1. First call SET_VERSION to make sure pci domains are handled properly. As a
  72. * side-effect this fills out the unique name in the master structure.
  73. * 2. Call GET_UNIQUE to read out the unique name from the master structure,
  74. * which matches the busid thanks to step 1. If it doesn't, proceed to try
  75. * the next device node.
  76. *
  77. * Opening by name is slightly different:
  78. *
  79. * 1. Directly call VERSION to get the version and to match against the driver
  80. * name returned by that ioctl. Note that SET_VERSION is not called, which
  81. * means the unique name for the master node just opening is _not_ filled
  82. * out. This despite that with current drm device nodes are always bound to
  83. * one device, and can't be runtime assigned like with drm 1.0.
  84. * 2. Match driver name. If it mismatches, proceed to the next device node.
  85. * 3. Call GET_UNIQUE, and check whether the unique name has length zero (by
  86. * checking that the first byte in the string is 0). If that's not the case
  87. * libdrm skips and proceeds to the next device node. Probably this is just
  88. * copypasta from drm 1.0 times where a set unique name meant that the driver
  89. * was in use already, but that's just conjecture.
  90. *
  91. * Long story short: To keep the open by name logic working, GET_UNIQUE must
  92. * _not_ return a unique string when SET_VERSION hasn't been called yet,
  93. * otherwise libdrm breaks. Even when that unique string can't ever change, and
  94. * is totally irrelevant for actually opening the device because runtime
  95. * assignable device instances were only support in drm 1.0, which is long dead.
  96. * But the libdrm code in drmOpenByName somehow survived, hence this can't be
  97. * broken.
  98. */
  99. /*
  100. * Get the bus id.
  101. *
  102. * \param inode device inode.
  103. * \param file_priv DRM file private.
  104. * \param cmd command.
  105. * \param arg user argument, pointing to a drm_unique structure.
  106. * \return zero on success or a negative number on failure.
  107. *
  108. * Copies the bus id from drm_device::unique into user space.
  109. */
  110. int drm_getunique(struct drm_device *dev, void *data,
  111. struct drm_file *file_priv)
  112. {
  113. struct drm_unique *u = data;
  114. struct drm_master *master;
  115. mutex_lock(&dev->master_mutex);
  116. master = file_priv->master;
  117. if (u->unique_len >= master->unique_len) {
  118. if (copy_to_user(u->unique, master->unique, master->unique_len)) {
  119. mutex_unlock(&dev->master_mutex);
  120. return -EFAULT;
  121. }
  122. }
  123. u->unique_len = master->unique_len;
  124. mutex_unlock(&dev->master_mutex);
  125. return 0;
  126. }
  127. static void
  128. drm_unset_busid(struct drm_device *dev,
  129. struct drm_master *master)
  130. {
  131. kfree(master->unique);
  132. master->unique = NULL;
  133. master->unique_len = 0;
  134. }
  135. static int drm_set_busid(struct drm_device *dev, struct drm_file *file_priv)
  136. {
  137. struct drm_master *master = file_priv->master;
  138. int ret;
  139. if (master->unique != NULL)
  140. drm_unset_busid(dev, master);
  141. if (dev->dev && dev_is_pci(dev->dev)) {
  142. ret = drm_pci_set_busid(dev, master);
  143. if (ret) {
  144. drm_unset_busid(dev, master);
  145. return ret;
  146. }
  147. } else {
  148. WARN_ON(!dev->unique);
  149. master->unique = kstrdup(dev->unique, GFP_KERNEL);
  150. if (master->unique)
  151. master->unique_len = strlen(dev->unique);
  152. }
  153. return 0;
  154. }
  155. /*
  156. * Get client information.
  157. *
  158. * \param inode device inode.
  159. * \param file_priv DRM file private.
  160. * \param cmd command.
  161. * \param arg user argument, pointing to a drm_client structure.
  162. *
  163. * \return zero on success or a negative number on failure.
  164. *
  165. * Searches for the client with the specified index and copies its information
  166. * into userspace
  167. */
  168. int drm_getclient(struct drm_device *dev, void *data,
  169. struct drm_file *file_priv)
  170. {
  171. struct drm_client *client = data;
  172. /*
  173. * Hollowed-out getclient ioctl to keep some dead old drm tests/tools
  174. * not breaking completely. Userspace tools stop enumerating one they
  175. * get -EINVAL, hence this is the return value we need to hand back for
  176. * no clients tracked.
  177. *
  178. * Unfortunately some clients (*cough* libva *cough*) use this in a fun
  179. * attempt to figure out whether they're authenticated or not. Since
  180. * that's the only thing they care about, give it to the directly
  181. * instead of walking one giant list.
  182. */
  183. if (client->idx == 0) {
  184. client->auth = file_priv->authenticated;
  185. client->pid = task_pid_vnr(current);
  186. client->uid = overflowuid;
  187. client->magic = 0;
  188. client->iocs = 0;
  189. return 0;
  190. } else {
  191. return -EINVAL;
  192. }
  193. }
  194. /*
  195. * Get statistics information.
  196. *
  197. * \param inode device inode.
  198. * \param file_priv DRM file private.
  199. * \param cmd command.
  200. * \param arg user argument, pointing to a drm_stats structure.
  201. *
  202. * \return zero on success or a negative number on failure.
  203. */
  204. static int drm_getstats(struct drm_device *dev, void *data,
  205. struct drm_file *file_priv)
  206. {
  207. struct drm_stats *stats = data;
  208. /* Clear stats to prevent userspace from eating its stack garbage. */
  209. memset(stats, 0, sizeof(*stats));
  210. return 0;
  211. }
  212. /*
  213. * Get device/driver capabilities
  214. */
  215. static int drm_getcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
  216. {
  217. struct drm_get_cap *req = data;
  218. struct drm_crtc *crtc;
  219. req->value = 0;
  220. /* Only some caps make sense with UMS/render-only drivers. */
  221. switch (req->capability) {
  222. case DRM_CAP_TIMESTAMP_MONOTONIC:
  223. req->value = 1;
  224. return 0;
  225. case DRM_CAP_PRIME:
  226. req->value = DRM_PRIME_CAP_IMPORT | DRM_PRIME_CAP_EXPORT;
  227. return 0;
  228. case DRM_CAP_SYNCOBJ:
  229. req->value = drm_core_check_feature(dev, DRIVER_SYNCOBJ);
  230. return 0;
  231. case DRM_CAP_SYNCOBJ_TIMELINE:
  232. req->value = drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE);
  233. return 0;
  234. }
  235. /* Other caps only work with KMS drivers */
  236. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  237. return -EOPNOTSUPP;
  238. switch (req->capability) {
  239. case DRM_CAP_DUMB_BUFFER:
  240. if (dev->driver->dumb_create)
  241. req->value = 1;
  242. break;
  243. case DRM_CAP_VBLANK_HIGH_CRTC:
  244. req->value = 1;
  245. break;
  246. case DRM_CAP_DUMB_PREFERRED_DEPTH:
  247. req->value = dev->mode_config.preferred_depth;
  248. break;
  249. case DRM_CAP_DUMB_PREFER_SHADOW:
  250. req->value = dev->mode_config.prefer_shadow;
  251. break;
  252. case DRM_CAP_ASYNC_PAGE_FLIP:
  253. req->value = dev->mode_config.async_page_flip;
  254. break;
  255. case DRM_CAP_PAGE_FLIP_TARGET:
  256. req->value = 1;
  257. drm_for_each_crtc(crtc, dev) {
  258. if (!crtc->funcs->page_flip_target)
  259. req->value = 0;
  260. }
  261. break;
  262. case DRM_CAP_CURSOR_WIDTH:
  263. if (dev->mode_config.cursor_width)
  264. req->value = dev->mode_config.cursor_width;
  265. else
  266. req->value = 64;
  267. break;
  268. case DRM_CAP_CURSOR_HEIGHT:
  269. if (dev->mode_config.cursor_height)
  270. req->value = dev->mode_config.cursor_height;
  271. else
  272. req->value = 64;
  273. break;
  274. case DRM_CAP_ADDFB2_MODIFIERS:
  275. req->value = !dev->mode_config.fb_modifiers_not_supported;
  276. break;
  277. case DRM_CAP_CRTC_IN_VBLANK_EVENT:
  278. req->value = 1;
  279. break;
  280. case DRM_CAP_ATOMIC_ASYNC_PAGE_FLIP:
  281. req->value = drm_core_check_feature(dev, DRIVER_ATOMIC) &&
  282. dev->mode_config.async_page_flip;
  283. break;
  284. default:
  285. return -EINVAL;
  286. }
  287. return 0;
  288. }
  289. /*
  290. * Set device/driver capabilities
  291. */
  292. static int
  293. drm_setclientcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
  294. {
  295. struct drm_set_client_cap *req = data;
  296. /* No render-only settable capabilities for now */
  297. /* Below caps that only works with KMS drivers */
  298. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  299. return -EOPNOTSUPP;
  300. switch (req->capability) {
  301. case DRM_CLIENT_CAP_STEREO_3D:
  302. if (req->value > 1)
  303. return -EINVAL;
  304. file_priv->stereo_allowed = req->value;
  305. break;
  306. case DRM_CLIENT_CAP_UNIVERSAL_PLANES:
  307. if (req->value > 1)
  308. return -EINVAL;
  309. file_priv->universal_planes = req->value;
  310. break;
  311. case DRM_CLIENT_CAP_ATOMIC:
  312. if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
  313. return -EOPNOTSUPP;
  314. /* The modesetting DDX has a totally broken idea of atomic. */
  315. if (current->comm[0] == 'X' && req->value == 1) {
  316. pr_info("broken atomic modeset userspace detected, disabling atomic\n");
  317. return -EOPNOTSUPP;
  318. }
  319. if (req->value > 2)
  320. return -EINVAL;
  321. file_priv->atomic = req->value;
  322. file_priv->universal_planes = req->value;
  323. /*
  324. * No atomic user-space blows up on aspect ratio mode bits.
  325. */
  326. file_priv->aspect_ratio_allowed = req->value;
  327. break;
  328. case DRM_CLIENT_CAP_ASPECT_RATIO:
  329. if (req->value > 1)
  330. return -EINVAL;
  331. file_priv->aspect_ratio_allowed = req->value;
  332. break;
  333. case DRM_CLIENT_CAP_WRITEBACK_CONNECTORS:
  334. if (!file_priv->atomic)
  335. return -EINVAL;
  336. if (req->value > 1)
  337. return -EINVAL;
  338. file_priv->writeback_connectors = req->value;
  339. break;
  340. case DRM_CLIENT_CAP_CURSOR_PLANE_HOTSPOT:
  341. if (!drm_core_check_feature(dev, DRIVER_CURSOR_HOTSPOT))
  342. return -EOPNOTSUPP;
  343. if (!file_priv->atomic)
  344. return -EINVAL;
  345. if (req->value > 1)
  346. return -EINVAL;
  347. file_priv->supports_virtualized_cursor_plane = req->value;
  348. break;
  349. case DRM_CLIENT_CAP_PLANE_COLOR_PIPELINE:
  350. if (!file_priv->atomic)
  351. return -EINVAL;
  352. if (req->value > 1)
  353. return -EINVAL;
  354. file_priv->plane_color_pipeline = req->value;
  355. break;
  356. default:
  357. return -EINVAL;
  358. }
  359. return 0;
  360. }
  361. /*
  362. * Setversion ioctl.
  363. *
  364. * \param inode device inode.
  365. * \param file_priv DRM file private.
  366. * \param cmd command.
  367. * \param arg user argument, pointing to a drm_lock structure.
  368. * \return zero on success or negative number on failure.
  369. *
  370. * Sets the requested interface version
  371. */
  372. static int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
  373. {
  374. struct drm_set_version *sv = data;
  375. int if_version, retcode = 0;
  376. mutex_lock(&dev->master_mutex);
  377. if (sv->drm_di_major != -1) {
  378. if (sv->drm_di_major != DRM_IF_MAJOR ||
  379. sv->drm_di_minor < 0 || sv->drm_di_minor > DRM_IF_MINOR) {
  380. retcode = -EINVAL;
  381. goto done;
  382. }
  383. if_version = DRM_IF_VERSION(sv->drm_di_major,
  384. sv->drm_di_minor);
  385. dev->if_version = max(if_version, dev->if_version);
  386. if (sv->drm_di_minor >= 1) {
  387. /*
  388. * Version 1.1 includes tying of DRM to specific device
  389. * Version 1.4 has proper PCI domain support
  390. */
  391. retcode = drm_set_busid(dev, file_priv);
  392. if (retcode)
  393. goto done;
  394. }
  395. }
  396. if (sv->drm_dd_major != -1) {
  397. if (sv->drm_dd_major != dev->driver->major ||
  398. sv->drm_dd_minor < 0 || sv->drm_dd_minor >
  399. dev->driver->minor) {
  400. retcode = -EINVAL;
  401. goto done;
  402. }
  403. }
  404. done:
  405. sv->drm_di_major = DRM_IF_MAJOR;
  406. sv->drm_di_minor = DRM_IF_MINOR;
  407. sv->drm_dd_major = dev->driver->major;
  408. sv->drm_dd_minor = dev->driver->minor;
  409. mutex_unlock(&dev->master_mutex);
  410. return retcode;
  411. }
  412. /**
  413. * drm_noop - DRM no-op ioctl implementation
  414. * @dev: DRM device for the ioctl
  415. * @data: data pointer for the ioctl
  416. * @file_priv: DRM file for the ioctl call
  417. *
  418. * This no-op implementation for drm ioctls is useful for deprecated
  419. * functionality where we can't return a failure code because existing userspace
  420. * checks the result of the ioctl, but doesn't care about the action.
  421. *
  422. * Always returns successfully with 0.
  423. */
  424. int drm_noop(struct drm_device *dev, void *data,
  425. struct drm_file *file_priv)
  426. {
  427. drm_dbg_core(dev, "\n");
  428. return 0;
  429. }
  430. EXPORT_SYMBOL(drm_noop);
  431. /**
  432. * drm_invalid_op - DRM invalid ioctl implementation
  433. * @dev: DRM device for the ioctl
  434. * @data: data pointer for the ioctl
  435. * @file_priv: DRM file for the ioctl call
  436. *
  437. * This no-op implementation for drm ioctls is useful for deprecated
  438. * functionality where we really don't want to allow userspace to call the ioctl
  439. * any more. This is the case for old ums interfaces for drivers that
  440. * transitioned to kms gradually and so kept the old legacy tables around. This
  441. * only applies to radeon and i915 kms drivers, other drivers shouldn't need to
  442. * use this function.
  443. *
  444. * Always fails with a return value of -EINVAL.
  445. */
  446. int drm_invalid_op(struct drm_device *dev, void *data,
  447. struct drm_file *file_priv)
  448. {
  449. return -EINVAL;
  450. }
  451. EXPORT_SYMBOL(drm_invalid_op);
  452. /*
  453. * Copy and IOCTL return string to user space
  454. */
  455. static int drm_copy_field(char __user *buf, size_t *buf_len, const char *value)
  456. {
  457. size_t len;
  458. /* don't attempt to copy a NULL pointer */
  459. if (WARN_ONCE(!value, "BUG: the value to copy was not set!")) {
  460. *buf_len = 0;
  461. return 0;
  462. }
  463. /* don't overflow userbuf */
  464. len = strlen(value);
  465. if (len > *buf_len)
  466. len = *buf_len;
  467. /* let userspace know exact length of driver value (which could be
  468. * larger than the userspace-supplied buffer) */
  469. *buf_len = strlen(value);
  470. /* finally, try filling in the userbuf */
  471. if (len && buf)
  472. if (copy_to_user(buf, value, len))
  473. return -EFAULT;
  474. return 0;
  475. }
  476. /*
  477. * Get version information
  478. *
  479. * \param inode device inode.
  480. * \param filp file pointer.
  481. * \param cmd command.
  482. * \param arg user argument, pointing to a drm_version structure.
  483. * \return zero on success or negative number on failure.
  484. *
  485. * Fills in the version information in \p arg.
  486. */
  487. int drm_version(struct drm_device *dev, void *data,
  488. struct drm_file *file_priv)
  489. {
  490. struct drm_version *version = data;
  491. int err;
  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. /* Driver date is deprecated. Userspace expects a non-empty string. */
  498. if (!err)
  499. err = drm_copy_field(version->date, &version->date_len, "0");
  500. if (!err)
  501. err = drm_copy_field(version->desc, &version->desc_len,
  502. dev->driver->desc);
  503. return err;
  504. }
  505. /*
  506. * Check if the passed string contains control char or spaces or
  507. * anything that would mess up a formatted output.
  508. */
  509. static int drm_validate_value_string(const char *value, size_t len)
  510. {
  511. int i;
  512. for (i = 0; i < len; i++) {
  513. if (!isascii(value[i]) || !isgraph(value[i]))
  514. return -EINVAL;
  515. }
  516. return 0;
  517. }
  518. static int drm_set_client_name(struct drm_device *dev, void *data,
  519. struct drm_file *file_priv)
  520. {
  521. struct drm_set_client_name *name = data;
  522. size_t len = name->name_len;
  523. void __user *user_ptr;
  524. char *new_name;
  525. if (len > DRM_CLIENT_NAME_MAX_LEN) {
  526. return -EINVAL;
  527. } else if (len) {
  528. user_ptr = u64_to_user_ptr(name->name);
  529. new_name = memdup_user_nul(user_ptr, len);
  530. if (IS_ERR(new_name))
  531. return PTR_ERR(new_name);
  532. if (strlen(new_name) != len ||
  533. drm_validate_value_string(new_name, len) < 0) {
  534. kfree(new_name);
  535. return -EINVAL;
  536. }
  537. } else {
  538. new_name = NULL;
  539. }
  540. mutex_lock(&file_priv->client_name_lock);
  541. kfree(file_priv->client_name);
  542. file_priv->client_name = new_name;
  543. mutex_unlock(&file_priv->client_name_lock);
  544. return 0;
  545. }
  546. static int drm_ioctl_permit(u32 flags, struct drm_file *file_priv)
  547. {
  548. /* ROOT_ONLY is only for CAP_SYS_ADMIN */
  549. if (unlikely((flags & DRM_ROOT_ONLY) && !capable(CAP_SYS_ADMIN)))
  550. return -EACCES;
  551. /* AUTH is only for authenticated or render client */
  552. if (unlikely((flags & DRM_AUTH) && !drm_is_render_client(file_priv) &&
  553. !file_priv->authenticated))
  554. return -EACCES;
  555. /* MASTER is only for master or control clients */
  556. if (unlikely((flags & DRM_MASTER) &&
  557. !drm_is_current_master(file_priv)))
  558. return -EACCES;
  559. /* Render clients must be explicitly allowed */
  560. if (unlikely(!(flags & DRM_RENDER_ALLOW) &&
  561. drm_is_render_client(file_priv)))
  562. return -EACCES;
  563. return 0;
  564. }
  565. #define DRM_IOCTL_DEF(ioctl, _func, _flags) \
  566. [DRM_IOCTL_NR(ioctl)] = { \
  567. .cmd = ioctl, \
  568. .func = _func, \
  569. .flags = _flags, \
  570. .name = #ioctl \
  571. }
  572. /* Ioctl table */
  573. static const struct drm_ioctl_desc drm_ioctls[] = {
  574. DRM_IOCTL_DEF(DRM_IOCTL_VERSION, drm_version, DRM_RENDER_ALLOW),
  575. DRM_IOCTL_DEF(DRM_IOCTL_GET_UNIQUE, drm_getunique, 0),
  576. DRM_IOCTL_DEF(DRM_IOCTL_GET_MAGIC, drm_getmagic, 0),
  577. DRM_IOCTL_DEF(DRM_IOCTL_GET_CLIENT, drm_getclient, 0),
  578. DRM_IOCTL_DEF(DRM_IOCTL_GET_STATS, drm_getstats, 0),
  579. DRM_IOCTL_DEF(DRM_IOCTL_GET_CAP, drm_getcap, DRM_RENDER_ALLOW),
  580. DRM_IOCTL_DEF(DRM_IOCTL_SET_CLIENT_CAP, drm_setclientcap, 0),
  581. DRM_IOCTL_DEF(DRM_IOCTL_SET_VERSION, drm_setversion, DRM_MASTER),
  582. DRM_IOCTL_DEF(DRM_IOCTL_SET_UNIQUE, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  583. DRM_IOCTL_DEF(DRM_IOCTL_BLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  584. DRM_IOCTL_DEF(DRM_IOCTL_UNBLOCK, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  585. DRM_IOCTL_DEF(DRM_IOCTL_AUTH_MAGIC, drm_authmagic, DRM_MASTER),
  586. DRM_IOCTL_DEF(DRM_IOCTL_SET_MASTER, drm_setmaster_ioctl, 0),
  587. DRM_IOCTL_DEF(DRM_IOCTL_DROP_MASTER, drm_dropmaster_ioctl, 0),
  588. DRM_IOCTL_DEF(DRM_IOCTL_ADD_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  589. DRM_IOCTL_DEF(DRM_IOCTL_RM_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  590. DRM_IOCTL_DEF(DRM_IOCTL_FINISH, drm_noop, DRM_AUTH),
  591. DRM_IOCTL_DEF(DRM_IOCTL_WAIT_VBLANK, drm_wait_vblank_ioctl, 0),
  592. DRM_IOCTL_DEF(DRM_IOCTL_UPDATE_DRAW, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  593. DRM_IOCTL_DEF(DRM_IOCTL_GEM_CLOSE, drm_gem_close_ioctl, DRM_RENDER_ALLOW),
  594. DRM_IOCTL_DEF(DRM_IOCTL_GEM_FLINK, drm_gem_flink_ioctl, DRM_AUTH),
  595. DRM_IOCTL_DEF(DRM_IOCTL_GEM_OPEN, drm_gem_open_ioctl, DRM_AUTH),
  596. DRM_IOCTL_DEF(DRM_IOCTL_GEM_CHANGE_HANDLE, drm_gem_change_handle_ioctl, DRM_RENDER_ALLOW),
  597. DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETRESOURCES, drm_mode_getresources, 0),
  598. DRM_IOCTL_DEF(DRM_IOCTL_PRIME_HANDLE_TO_FD, drm_prime_handle_to_fd_ioctl, DRM_RENDER_ALLOW),
  599. DRM_IOCTL_DEF(DRM_IOCTL_PRIME_FD_TO_HANDLE, drm_prime_fd_to_handle_ioctl, DRM_RENDER_ALLOW),
  600. DRM_IOCTL_DEF(DRM_IOCTL_SET_CLIENT_NAME, drm_set_client_name, DRM_RENDER_ALLOW),
  601. DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANERESOURCES, drm_mode_getplane_res, 0),
  602. DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCRTC, drm_mode_getcrtc, 0),
  603. DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETCRTC, drm_mode_setcrtc, DRM_MASTER),
  604. DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPLANE, drm_mode_getplane, 0),
  605. DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPLANE, drm_mode_setplane, DRM_MASTER),
  606. DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR, drm_mode_cursor_ioctl, DRM_MASTER),
  607. DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETGAMMA, drm_mode_gamma_get_ioctl, 0),
  608. DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETGAMMA, drm_mode_gamma_set_ioctl, DRM_MASTER),
  609. DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETENCODER, drm_mode_getencoder, 0),
  610. DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETCONNECTOR, drm_mode_getconnector, 0),
  611. DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATTACHMODE, drm_noop, DRM_MASTER),
  612. DRM_IOCTL_DEF(DRM_IOCTL_MODE_DETACHMODE, drm_noop, DRM_MASTER),
  613. DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPERTY, drm_mode_getproperty_ioctl, 0),
  614. DRM_IOCTL_DEF(DRM_IOCTL_MODE_SETPROPERTY, drm_connector_property_set_ioctl, DRM_MASTER),
  615. DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETPROPBLOB, drm_mode_getblob_ioctl, 0),
  616. DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETFB, drm_mode_getfb, 0),
  617. DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETFB2, drm_mode_getfb2_ioctl, 0),
  618. DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB, drm_mode_addfb_ioctl, 0),
  619. DRM_IOCTL_DEF(DRM_IOCTL_MODE_ADDFB2, drm_mode_addfb2_ioctl, 0),
  620. DRM_IOCTL_DEF(DRM_IOCTL_MODE_RMFB, drm_mode_rmfb_ioctl, 0),
  621. DRM_IOCTL_DEF(DRM_IOCTL_MODE_CLOSEFB, drm_mode_closefb_ioctl, 0),
  622. DRM_IOCTL_DEF(DRM_IOCTL_MODE_PAGE_FLIP, drm_mode_page_flip_ioctl, DRM_MASTER),
  623. DRM_IOCTL_DEF(DRM_IOCTL_MODE_DIRTYFB, drm_mode_dirtyfb_ioctl, DRM_MASTER),
  624. DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATE_DUMB, drm_mode_create_dumb_ioctl, 0),
  625. DRM_IOCTL_DEF(DRM_IOCTL_MODE_MAP_DUMB, drm_mode_mmap_dumb_ioctl, 0),
  626. DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROY_DUMB, drm_mode_destroy_dumb_ioctl, 0),
  627. DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_GETPROPERTIES, drm_mode_obj_get_properties_ioctl, 0),
  628. DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_SETPROPERTY, drm_mode_obj_set_property_ioctl, DRM_MASTER),
  629. DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR2, drm_mode_cursor2_ioctl, DRM_MASTER),
  630. DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATOMIC, drm_mode_atomic_ioctl, DRM_MASTER),
  631. DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATEPROPBLOB, drm_mode_createblob_ioctl, 0),
  632. DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROYPROPBLOB, drm_mode_destroyblob_ioctl, 0),
  633. DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_CREATE, drm_syncobj_create_ioctl,
  634. DRM_RENDER_ALLOW),
  635. DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_DESTROY, drm_syncobj_destroy_ioctl,
  636. DRM_RENDER_ALLOW),
  637. DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, drm_syncobj_handle_to_fd_ioctl,
  638. DRM_RENDER_ALLOW),
  639. DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, drm_syncobj_fd_to_handle_ioctl,
  640. DRM_RENDER_ALLOW),
  641. DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_TRANSFER, drm_syncobj_transfer_ioctl,
  642. DRM_RENDER_ALLOW),
  643. DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_WAIT, drm_syncobj_wait_ioctl,
  644. DRM_RENDER_ALLOW),
  645. DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_TIMELINE_WAIT, drm_syncobj_timeline_wait_ioctl,
  646. DRM_RENDER_ALLOW),
  647. DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_EVENTFD, drm_syncobj_eventfd_ioctl,
  648. DRM_RENDER_ALLOW),
  649. DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_RESET, drm_syncobj_reset_ioctl,
  650. DRM_RENDER_ALLOW),
  651. DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_SIGNAL, drm_syncobj_signal_ioctl,
  652. DRM_RENDER_ALLOW),
  653. DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_TIMELINE_SIGNAL, drm_syncobj_timeline_signal_ioctl,
  654. DRM_RENDER_ALLOW),
  655. DRM_IOCTL_DEF(DRM_IOCTL_SYNCOBJ_QUERY, drm_syncobj_query_ioctl,
  656. DRM_RENDER_ALLOW),
  657. DRM_IOCTL_DEF(DRM_IOCTL_CRTC_GET_SEQUENCE, drm_crtc_get_sequence_ioctl, 0),
  658. DRM_IOCTL_DEF(DRM_IOCTL_CRTC_QUEUE_SEQUENCE, drm_crtc_queue_sequence_ioctl, 0),
  659. DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATE_LEASE, drm_mode_create_lease_ioctl, DRM_MASTER),
  660. DRM_IOCTL_DEF(DRM_IOCTL_MODE_LIST_LESSEES, drm_mode_list_lessees_ioctl, DRM_MASTER),
  661. DRM_IOCTL_DEF(DRM_IOCTL_MODE_GET_LEASE, drm_mode_get_lease_ioctl, DRM_MASTER),
  662. DRM_IOCTL_DEF(DRM_IOCTL_MODE_REVOKE_LEASE, drm_mode_revoke_lease_ioctl, DRM_MASTER),
  663. };
  664. #define DRM_CORE_IOCTL_COUNT ARRAY_SIZE(drm_ioctls)
  665. /**
  666. * DOC: driver specific ioctls
  667. *
  668. * First things first, driver private IOCTLs should only be needed for drivers
  669. * supporting rendering. Kernel modesetting is all standardized, and extended
  670. * through properties. There are a few exceptions in some existing drivers,
  671. * which define IOCTL for use by the display DRM master, but they all predate
  672. * properties.
  673. *
  674. * Now if you do have a render driver you always have to support it through
  675. * driver private properties. There's a few steps needed to wire all the things
  676. * up.
  677. *
  678. * First you need to define the structure for your IOCTL in your driver private
  679. * UAPI header in ``include/uapi/drm/my_driver_drm.h``::
  680. *
  681. * struct my_driver_operation {
  682. * u32 some_thing;
  683. * u32 another_thing;
  684. * };
  685. *
  686. * Please make sure that you follow all the best practices from
  687. * ``Documentation/process/botching-up-ioctls.rst``. Note that drm_ioctl()
  688. * automatically zero-extends structures, hence make sure you can add more stuff
  689. * at the end, i.e. don't put a variable sized array there.
  690. *
  691. * Then you need to define your IOCTL number, using one of DRM_IO(), DRM_IOR(),
  692. * DRM_IOW() or DRM_IOWR(). It must start with the DRM_IOCTL\_ prefix::
  693. *
  694. * ##define DRM_IOCTL_MY_DRIVER_OPERATION \
  695. * DRM_IOW(DRM_COMMAND_BASE, struct my_driver_operation)
  696. *
  697. * DRM driver private IOCTL must be in the range from DRM_COMMAND_BASE to
  698. * DRM_COMMAND_END. Finally you need an array of &struct drm_ioctl_desc to wire
  699. * up the handlers and set the access rights::
  700. *
  701. * static const struct drm_ioctl_desc my_driver_ioctls[] = {
  702. * DRM_IOCTL_DEF_DRV(MY_DRIVER_OPERATION, my_driver_operation,
  703. * DRM_AUTH|DRM_RENDER_ALLOW),
  704. * };
  705. *
  706. * And then assign this to the &drm_driver.ioctls field in your driver
  707. * structure.
  708. *
  709. * See the separate chapter on :ref:`file operations<drm_driver_fops>` for how
  710. * the driver-specific IOCTLs are wired up.
  711. */
  712. long drm_ioctl_kernel(struct file *file, drm_ioctl_t *func, void *kdata,
  713. u32 flags)
  714. {
  715. struct drm_file *file_priv = file->private_data;
  716. struct drm_device *dev = file_priv->minor->dev;
  717. int ret;
  718. /* Update drm_file owner if fd was passed along. */
  719. drm_file_update_pid(file_priv);
  720. if (drm_dev_is_unplugged(dev))
  721. return -ENODEV;
  722. ret = drm_ioctl_permit(flags, file_priv);
  723. if (unlikely(ret))
  724. return ret;
  725. return func(dev, kdata, file_priv);
  726. }
  727. EXPORT_SYMBOL(drm_ioctl_kernel);
  728. /**
  729. * drm_ioctl - ioctl callback implementation for DRM drivers
  730. * @filp: file this ioctl is called on
  731. * @cmd: ioctl cmd number
  732. * @arg: user argument
  733. *
  734. * Looks up the ioctl function in the DRM core and the driver dispatch table,
  735. * stored in &drm_driver.ioctls. It checks for necessary permission by calling
  736. * drm_ioctl_permit(), and dispatches to the respective function.
  737. *
  738. * Returns:
  739. * Zero on success, negative error code on failure.
  740. */
  741. long drm_ioctl(struct file *filp,
  742. unsigned int cmd, unsigned long arg)
  743. {
  744. struct drm_file *file_priv = filp->private_data;
  745. struct drm_device *dev;
  746. const struct drm_ioctl_desc *ioctl = NULL;
  747. drm_ioctl_t *func;
  748. unsigned int nr = DRM_IOCTL_NR(cmd);
  749. int retcode = -EINVAL;
  750. char stack_kdata[128];
  751. char *kdata = NULL;
  752. unsigned int in_size, out_size, drv_size, ksize;
  753. bool is_driver_ioctl;
  754. dev = file_priv->minor->dev;
  755. if (drm_dev_is_unplugged(dev))
  756. return -ENODEV;
  757. if (DRM_IOCTL_TYPE(cmd) != DRM_IOCTL_BASE)
  758. return -ENOTTY;
  759. is_driver_ioctl = nr >= DRM_COMMAND_BASE && nr < DRM_COMMAND_END;
  760. if (is_driver_ioctl) {
  761. /* driver ioctl */
  762. unsigned int index = nr - DRM_COMMAND_BASE;
  763. if (index >= dev->driver->num_ioctls)
  764. goto err_i1;
  765. index = array_index_nospec(index, dev->driver->num_ioctls);
  766. ioctl = &dev->driver->ioctls[index];
  767. } else {
  768. /* core ioctl */
  769. if (nr >= DRM_CORE_IOCTL_COUNT)
  770. goto err_i1;
  771. nr = array_index_nospec(nr, DRM_CORE_IOCTL_COUNT);
  772. ioctl = &drm_ioctls[nr];
  773. }
  774. drv_size = _IOC_SIZE(ioctl->cmd);
  775. out_size = in_size = _IOC_SIZE(cmd);
  776. if ((cmd & ioctl->cmd & IOC_IN) == 0)
  777. in_size = 0;
  778. if ((cmd & ioctl->cmd & IOC_OUT) == 0)
  779. out_size = 0;
  780. ksize = max(max(in_size, out_size), drv_size);
  781. drm_dbg_core(dev, "comm=\"%s\" pid=%d, dev=0x%lx, auth=%d, %s\n",
  782. current->comm, task_pid_nr(current),
  783. (long)old_encode_dev(file_priv->minor->kdev->devt),
  784. file_priv->authenticated, ioctl->name);
  785. /* Do not trust userspace, use our own definition */
  786. func = ioctl->func;
  787. if (unlikely(!func)) {
  788. drm_dbg_core(dev, "no function\n");
  789. retcode = -EINVAL;
  790. goto err_i1;
  791. }
  792. if (ksize <= sizeof(stack_kdata)) {
  793. kdata = stack_kdata;
  794. } else {
  795. kdata = kmalloc(ksize, GFP_KERNEL);
  796. if (!kdata) {
  797. retcode = -ENOMEM;
  798. goto err_i1;
  799. }
  800. }
  801. if (copy_from_user(kdata, (void __user *)arg, in_size) != 0) {
  802. retcode = -EFAULT;
  803. goto err_i1;
  804. }
  805. if (ksize > in_size)
  806. memset(kdata + in_size, 0, ksize - in_size);
  807. retcode = drm_ioctl_kernel(filp, func, kdata, ioctl->flags);
  808. if (copy_to_user((void __user *)arg, kdata, out_size) != 0)
  809. retcode = -EFAULT;
  810. err_i1:
  811. if (!ioctl)
  812. drm_dbg_core(dev,
  813. "invalid ioctl: comm=\"%s\", pid=%d, dev=0x%lx, auth=%d, cmd=0x%02x, nr=0x%02x\n",
  814. current->comm, task_pid_nr(current),
  815. (long)old_encode_dev(file_priv->minor->kdev->devt),
  816. file_priv->authenticated, cmd, nr);
  817. if (kdata != stack_kdata)
  818. kfree(kdata);
  819. if (retcode)
  820. drm_dbg_core(dev, "comm=\"%s\", pid=%d, ret=%d\n",
  821. current->comm, task_pid_nr(current), retcode);
  822. return retcode;
  823. }
  824. EXPORT_SYMBOL(drm_ioctl);
  825. /**
  826. * drm_ioctl_flags - Check for core ioctl and return ioctl permission flags
  827. * @nr: ioctl number
  828. * @flags: where to return the ioctl permission flags
  829. *
  830. * This ioctl is only used by the vmwgfx driver to augment the access checks
  831. * done by the drm core and insofar a pretty decent layering violation. This
  832. * shouldn't be used by any drivers.
  833. *
  834. * Returns:
  835. * True if the @nr corresponds to a DRM core ioctl number, false otherwise.
  836. */
  837. bool drm_ioctl_flags(unsigned int nr, unsigned int *flags)
  838. {
  839. if (nr >= DRM_COMMAND_BASE && nr < DRM_COMMAND_END)
  840. return false;
  841. if (nr >= DRM_CORE_IOCTL_COUNT)
  842. return false;
  843. nr = array_index_nospec(nr, DRM_CORE_IOCTL_COUNT);
  844. *flags = drm_ioctls[nr].flags;
  845. return true;
  846. }
  847. EXPORT_SYMBOL(drm_ioctl_flags);