drm_ioc32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * \file drm_ioc32.c
  3. *
  4. * 32-bit ioctl compatibility routines for the DRM.
  5. *
  6. * \author Paul Mackerras <paulus@samba.org>
  7. *
  8. * Copyright (C) Paul Mackerras 2005.
  9. * All Rights Reserved.
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a
  12. * copy of this software and associated documentation files (the "Software"),
  13. * to deal in the Software without restriction, including without limitation
  14. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15. * and/or sell copies of the Software, and to permit persons to whom the
  16. * Software is furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice (including the next
  19. * paragraph) shall be included in all copies or substantial portions of the
  20. * Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  25. * THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  26. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  28. * IN THE SOFTWARE.
  29. */
  30. #include <linux/compat.h>
  31. #include <linux/nospec.h>
  32. #include <linux/ratelimit.h>
  33. #include <linux/export.h>
  34. #include <drm/drm_device.h>
  35. #include <drm/drm_file.h>
  36. #include <drm/drm_print.h>
  37. #include "drm_crtc_internal.h"
  38. #include "drm_internal.h"
  39. #define DRM_IOCTL_VERSION32 DRM_IOWR(0x00, drm_version32_t)
  40. #define DRM_IOCTL_GET_UNIQUE32 DRM_IOWR(0x01, drm_unique32_t)
  41. #define DRM_IOCTL_GET_MAP32 DRM_IOWR(0x04, drm_map32_t)
  42. #define DRM_IOCTL_GET_CLIENT32 DRM_IOWR(0x05, drm_client32_t)
  43. #define DRM_IOCTL_GET_STATS32 DRM_IOR( 0x06, drm_stats32_t)
  44. #define DRM_IOCTL_SET_UNIQUE32 DRM_IOW( 0x10, drm_unique32_t)
  45. #define DRM_IOCTL_ADD_MAP32 DRM_IOWR(0x15, drm_map32_t)
  46. #define DRM_IOCTL_ADD_BUFS32 DRM_IOWR(0x16, drm_buf_desc32_t)
  47. #define DRM_IOCTL_MARK_BUFS32 DRM_IOW( 0x17, drm_buf_desc32_t)
  48. #define DRM_IOCTL_INFO_BUFS32 DRM_IOWR(0x18, drm_buf_info32_t)
  49. #define DRM_IOCTL_MAP_BUFS32 DRM_IOWR(0x19, drm_buf_map32_t)
  50. #define DRM_IOCTL_FREE_BUFS32 DRM_IOW( 0x1a, drm_buf_free32_t)
  51. #define DRM_IOCTL_RM_MAP32 DRM_IOW( 0x1b, drm_map32_t)
  52. #define DRM_IOCTL_SET_SAREA_CTX32 DRM_IOW( 0x1c, drm_ctx_priv_map32_t)
  53. #define DRM_IOCTL_GET_SAREA_CTX32 DRM_IOWR(0x1d, drm_ctx_priv_map32_t)
  54. #define DRM_IOCTL_RES_CTX32 DRM_IOWR(0x26, drm_ctx_res32_t)
  55. #define DRM_IOCTL_DMA32 DRM_IOWR(0x29, drm_dma32_t)
  56. #define DRM_IOCTL_AGP_ENABLE32 DRM_IOW( 0x32, drm_agp_mode32_t)
  57. #define DRM_IOCTL_AGP_INFO32 DRM_IOR( 0x33, drm_agp_info32_t)
  58. #define DRM_IOCTL_AGP_ALLOC32 DRM_IOWR(0x34, drm_agp_buffer32_t)
  59. #define DRM_IOCTL_AGP_FREE32 DRM_IOW( 0x35, drm_agp_buffer32_t)
  60. #define DRM_IOCTL_AGP_BIND32 DRM_IOW( 0x36, drm_agp_binding32_t)
  61. #define DRM_IOCTL_AGP_UNBIND32 DRM_IOW( 0x37, drm_agp_binding32_t)
  62. #define DRM_IOCTL_SG_ALLOC32 DRM_IOW( 0x38, drm_scatter_gather32_t)
  63. #define DRM_IOCTL_SG_FREE32 DRM_IOW( 0x39, drm_scatter_gather32_t)
  64. #define DRM_IOCTL_UPDATE_DRAW32 DRM_IOW( 0x3f, drm_update_draw32_t)
  65. #define DRM_IOCTL_WAIT_VBLANK32 DRM_IOWR(0x3a, drm_wait_vblank32_t)
  66. #define DRM_IOCTL_MODE_ADDFB232 DRM_IOWR(0xb8, drm_mode_fb_cmd232_t)
  67. typedef struct drm_version_32 {
  68. int version_major; /* Major version */
  69. int version_minor; /* Minor version */
  70. int version_patchlevel; /* Patch level */
  71. u32 name_len; /* Length of name buffer */
  72. u32 name; /* Name of driver */
  73. u32 date_len; /* Length of date buffer */
  74. u32 date; /* User-space buffer to hold date */
  75. u32 desc_len; /* Length of desc buffer */
  76. u32 desc; /* User-space buffer to hold desc */
  77. } drm_version32_t;
  78. static int compat_drm_version(struct file *file, unsigned int cmd,
  79. unsigned long arg)
  80. {
  81. drm_version32_t v32;
  82. struct drm_version v;
  83. int err;
  84. if (copy_from_user(&v32, (void __user *)arg, sizeof(v32)))
  85. return -EFAULT;
  86. memset(&v, 0, sizeof(v));
  87. v = (struct drm_version) {
  88. .name_len = v32.name_len,
  89. .name = compat_ptr(v32.name),
  90. .date_len = v32.date_len,
  91. .date = compat_ptr(v32.date),
  92. .desc_len = v32.desc_len,
  93. .desc = compat_ptr(v32.desc),
  94. };
  95. err = drm_ioctl_kernel(file, drm_version, &v,
  96. DRM_RENDER_ALLOW);
  97. if (err)
  98. return err;
  99. v32.version_major = v.version_major;
  100. v32.version_minor = v.version_minor;
  101. v32.version_patchlevel = v.version_patchlevel;
  102. v32.name_len = v.name_len;
  103. v32.date_len = v.date_len;
  104. v32.desc_len = v.desc_len;
  105. if (copy_to_user((void __user *)arg, &v32, sizeof(v32)))
  106. return -EFAULT;
  107. return 0;
  108. }
  109. typedef struct drm_unique32 {
  110. u32 unique_len; /* Length of unique */
  111. u32 unique; /* Unique name for driver instantiation */
  112. } drm_unique32_t;
  113. static int compat_drm_getunique(struct file *file, unsigned int cmd,
  114. unsigned long arg)
  115. {
  116. drm_unique32_t uq32;
  117. struct drm_unique uq;
  118. int err;
  119. if (copy_from_user(&uq32, (void __user *)arg, sizeof(uq32)))
  120. return -EFAULT;
  121. memset(&uq, 0, sizeof(uq));
  122. uq = (struct drm_unique){
  123. .unique_len = uq32.unique_len,
  124. .unique = compat_ptr(uq32.unique),
  125. };
  126. err = drm_ioctl_kernel(file, drm_getunique, &uq, 0);
  127. if (err)
  128. return err;
  129. uq32.unique_len = uq.unique_len;
  130. if (copy_to_user((void __user *)arg, &uq32, sizeof(uq32)))
  131. return -EFAULT;
  132. return 0;
  133. }
  134. static int compat_drm_setunique(struct file *file, unsigned int cmd,
  135. unsigned long arg)
  136. {
  137. /* it's dead */
  138. return -EINVAL;
  139. }
  140. typedef struct drm_client32 {
  141. int idx; /* Which client desired? */
  142. int auth; /* Is client authenticated? */
  143. u32 pid; /* Process ID */
  144. u32 uid; /* User ID */
  145. u32 magic; /* Magic */
  146. u32 iocs; /* Ioctl count */
  147. } drm_client32_t;
  148. static int compat_drm_getclient(struct file *file, unsigned int cmd,
  149. unsigned long arg)
  150. {
  151. drm_client32_t c32;
  152. drm_client32_t __user *argp = (void __user *)arg;
  153. struct drm_client client;
  154. int err;
  155. if (copy_from_user(&c32, argp, sizeof(c32)))
  156. return -EFAULT;
  157. memset(&client, 0, sizeof(client));
  158. client.idx = c32.idx;
  159. err = drm_ioctl_kernel(file, drm_getclient, &client, 0);
  160. if (err)
  161. return err;
  162. c32.idx = client.idx;
  163. c32.auth = client.auth;
  164. c32.pid = client.pid;
  165. c32.uid = client.uid;
  166. c32.magic = client.magic;
  167. c32.iocs = client.iocs;
  168. if (copy_to_user(argp, &c32, sizeof(c32)))
  169. return -EFAULT;
  170. return 0;
  171. }
  172. typedef struct drm_stats32 {
  173. u32 count;
  174. struct {
  175. u32 value;
  176. enum drm_stat_type type;
  177. } data[15];
  178. } drm_stats32_t;
  179. static int compat_drm_getstats(struct file *file, unsigned int cmd,
  180. unsigned long arg)
  181. {
  182. drm_stats32_t __user *argp = (void __user *)arg;
  183. /* getstats is defunct, just clear */
  184. if (clear_user(argp, sizeof(drm_stats32_t)))
  185. return -EFAULT;
  186. return 0;
  187. }
  188. #if defined(CONFIG_X86)
  189. typedef struct drm_update_draw32 {
  190. drm_drawable_t handle;
  191. unsigned int type;
  192. unsigned int num;
  193. /* 64-bit version has a 32-bit pad here */
  194. u64 data; /**< Pointer */
  195. } __packed drm_update_draw32_t;
  196. static int compat_drm_update_draw(struct file *file, unsigned int cmd,
  197. unsigned long arg)
  198. {
  199. /* update_draw is defunct */
  200. return 0;
  201. }
  202. #endif
  203. struct drm_wait_vblank_request32 {
  204. enum drm_vblank_seq_type type;
  205. unsigned int sequence;
  206. u32 signal;
  207. };
  208. struct drm_wait_vblank_reply32 {
  209. enum drm_vblank_seq_type type;
  210. unsigned int sequence;
  211. s32 tval_sec;
  212. s32 tval_usec;
  213. };
  214. typedef union drm_wait_vblank32 {
  215. struct drm_wait_vblank_request32 request;
  216. struct drm_wait_vblank_reply32 reply;
  217. } drm_wait_vblank32_t;
  218. static int compat_drm_wait_vblank(struct file *file, unsigned int cmd,
  219. unsigned long arg)
  220. {
  221. drm_wait_vblank32_t __user *argp = (void __user *)arg;
  222. drm_wait_vblank32_t req32;
  223. union drm_wait_vblank req;
  224. int err;
  225. if (copy_from_user(&req32, argp, sizeof(req32)))
  226. return -EFAULT;
  227. memset(&req, 0, sizeof(req));
  228. req.request.type = req32.request.type;
  229. req.request.sequence = req32.request.sequence;
  230. req.request.signal = req32.request.signal;
  231. err = drm_ioctl_kernel(file, drm_wait_vblank_ioctl, &req, 0);
  232. req32.reply.type = req.reply.type;
  233. req32.reply.sequence = req.reply.sequence;
  234. req32.reply.tval_sec = req.reply.tval_sec;
  235. req32.reply.tval_usec = req.reply.tval_usec;
  236. if (copy_to_user(argp, &req32, sizeof(req32)))
  237. return -EFAULT;
  238. return err;
  239. }
  240. #if defined(CONFIG_X86)
  241. typedef struct drm_mode_fb_cmd232 {
  242. u32 fb_id;
  243. u32 width;
  244. u32 height;
  245. u32 pixel_format;
  246. u32 flags;
  247. u32 handles[4];
  248. u32 pitches[4];
  249. u32 offsets[4];
  250. u64 modifier[4];
  251. } __packed drm_mode_fb_cmd232_t;
  252. static int compat_drm_mode_addfb2(struct file *file, unsigned int cmd,
  253. unsigned long arg)
  254. {
  255. struct drm_mode_fb_cmd232 __user *argp = (void __user *)arg;
  256. struct drm_mode_fb_cmd2 req64;
  257. int err;
  258. memset(&req64, 0, sizeof(req64));
  259. if (copy_from_user(&req64, argp,
  260. offsetof(drm_mode_fb_cmd232_t, modifier)))
  261. return -EFAULT;
  262. if (copy_from_user(&req64.modifier, &argp->modifier,
  263. sizeof(req64.modifier)))
  264. return -EFAULT;
  265. err = drm_ioctl_kernel(file, drm_mode_addfb2, &req64, 0);
  266. if (err)
  267. return err;
  268. if (put_user(req64.fb_id, &argp->fb_id))
  269. return -EFAULT;
  270. return 0;
  271. }
  272. #endif
  273. static struct {
  274. drm_ioctl_compat_t *fn;
  275. char *name;
  276. } drm_compat_ioctls[] = {
  277. #define DRM_IOCTL32_DEF(n, f) [DRM_IOCTL_NR(n##32)] = {.fn = f, .name = #n}
  278. DRM_IOCTL32_DEF(DRM_IOCTL_VERSION, compat_drm_version),
  279. DRM_IOCTL32_DEF(DRM_IOCTL_GET_UNIQUE, compat_drm_getunique),
  280. DRM_IOCTL32_DEF(DRM_IOCTL_GET_CLIENT, compat_drm_getclient),
  281. DRM_IOCTL32_DEF(DRM_IOCTL_GET_STATS, compat_drm_getstats),
  282. DRM_IOCTL32_DEF(DRM_IOCTL_SET_UNIQUE, compat_drm_setunique),
  283. #if defined(CONFIG_X86)
  284. DRM_IOCTL32_DEF(DRM_IOCTL_UPDATE_DRAW, compat_drm_update_draw),
  285. #endif
  286. DRM_IOCTL32_DEF(DRM_IOCTL_WAIT_VBLANK, compat_drm_wait_vblank),
  287. #if defined(CONFIG_X86)
  288. DRM_IOCTL32_DEF(DRM_IOCTL_MODE_ADDFB2, compat_drm_mode_addfb2),
  289. #endif
  290. };
  291. /**
  292. * drm_compat_ioctl - 32bit IOCTL compatibility handler for DRM drivers
  293. * @filp: file this ioctl is called on
  294. * @cmd: ioctl cmd number
  295. * @arg: user argument
  296. *
  297. * Compatibility handler for 32 bit userspace running on 64 kernels. All actual
  298. * IOCTL handling is forwarded to drm_ioctl(), while marshalling structures as
  299. * appropriate. Note that this only handles DRM core IOCTLs, if the driver has
  300. * botched IOCTL itself, it must handle those by wrapping this function.
  301. *
  302. * Returns:
  303. * Zero on success, negative error code on failure.
  304. */
  305. long drm_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  306. {
  307. unsigned int nr = DRM_IOCTL_NR(cmd);
  308. struct drm_file *file_priv = filp->private_data;
  309. struct drm_device *dev = file_priv->minor->dev;
  310. drm_ioctl_compat_t *fn;
  311. int ret;
  312. /* Assume that ioctls without an explicit compat routine will just
  313. * work. This may not always be a good assumption, but it's better
  314. * than always failing.
  315. */
  316. if (nr >= ARRAY_SIZE(drm_compat_ioctls))
  317. return drm_ioctl(filp, cmd, arg);
  318. nr = array_index_nospec(nr, ARRAY_SIZE(drm_compat_ioctls));
  319. fn = drm_compat_ioctls[nr].fn;
  320. if (!fn)
  321. return drm_ioctl(filp, cmd, arg);
  322. drm_dbg_core(dev, "comm=\"%s\", pid=%d, dev=0x%lx, auth=%d, %s\n",
  323. current->comm, task_pid_nr(current),
  324. (long)old_encode_dev(file_priv->minor->kdev->devt),
  325. file_priv->authenticated,
  326. drm_compat_ioctls[nr].name);
  327. ret = (*fn)(filp, cmd, arg);
  328. if (ret)
  329. drm_dbg_core(dev, "ret = %d\n", ret);
  330. return ret;
  331. }
  332. EXPORT_SYMBOL(drm_compat_ioctl);