drm_ioc32.c 11 KB

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