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