1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdint.h>
28 #include <stdbool.h>
29 #include <signal.h>
30 #include <stdarg.h>
31 #include <fcntl.h>
32 #include <sys/types.h>
33 #include <sys/sysmacros.h>
34 #include <sys/stat.h>
35 #include <sys/ioctl.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <sys/mman.h>
39 #include <dlfcn.h>
40 #include "drm-uapi/i915_drm.h"
41 #include <inttypes.h>
42
43 #include "intel_aub.h"
44 #include "aub_write.h"
45
46 #include "dev/gen_debug.h"
47 #include "dev/gen_device_info.h"
48 #include "util/macros.h"
49
50 static int close_init_helper(int fd);
51 static int ioctl_init_helper(int fd, unsigned long request, ...);
52 static int munmap_init_helper(void *addr, size_t length);
53
54 static int (*libc_close)(int fd) = close_init_helper;
55 static int (*libc_ioctl)(int fd, unsigned long request, ...) = ioctl_init_helper;
56 static int (*libc_munmap)(void *addr, size_t length) = munmap_init_helper;
57
58 static int drm_fd = -1;
59 static char *output_filename = NULL;
60 static FILE *output_file = NULL;
61 static int verbose = 0;
62 static bool device_override = false;
63 static bool capture_only = false;
64 static int64_t frame_id = -1;
65 static bool capture_finished = false;
66
67 #define MAX_FD_COUNT 64
68 #define MAX_BO_COUNT 64 * 1024
69
70 struct bo {
71 uint32_t size;
72 uint64_t offset;
73 void *map;
74 /* Whether the buffer has been positionned in the GTT already. */
75 bool gtt_mapped : 1;
76 /* Tracks userspace mmapping of the buffer */
77 bool user_mapped : 1;
78 /* Using the i915-gem mmapping ioctl & execbuffer ioctl, track whether a
79 * buffer has been updated.
80 */
81 bool dirty : 1;
82 };
83
84 static struct bo *bos;
85
86 #define DRM_MAJOR 226
87
88 /* We set bit 0 in the map pointer for userptr BOs so we know not to
89 * munmap them on DRM_IOCTL_GEM_CLOSE.
90 */
91 #define USERPTR_FLAG 1
92 #define IS_USERPTR(p) ((uintptr_t) (p) & USERPTR_FLAG)
93 #define GET_PTR(p) ( (void *) ((uintptr_t) p & ~(uintptr_t) 1) )
94
95 #define fail_if(cond, ...) _fail_if(cond, "intel_dump_gpu", __VA_ARGS__)
96
97 static struct bo *
get_bo(unsigned fd,uint32_t handle)98 get_bo(unsigned fd, uint32_t handle)
99 {
100 struct bo *bo;
101
102 fail_if(handle >= MAX_BO_COUNT, "bo handle too large\n");
103 fail_if(fd >= MAX_FD_COUNT, "bo fd too large\n");
104 bo = &bos[handle + fd * MAX_BO_COUNT];
105
106 return bo;
107 }
108
109 static inline uint32_t
align_u32(uint32_t v,uint32_t a)110 align_u32(uint32_t v, uint32_t a)
111 {
112 return (v + a - 1) & ~(a - 1);
113 }
114
115 static struct gen_device_info devinfo = {0};
116 static int device = 0;
117 static struct aub_file aub_file;
118
119 static void
ensure_device_info(int fd)120 ensure_device_info(int fd)
121 {
122 /* We can't do this at open time as we're not yet authenticated. */
123 if (device == 0) {
124 fail_if(!gen_get_device_info_from_fd(fd, &devinfo),
125 "failed to identify chipset.\n");
126 device = devinfo.chipset_id;
127 } else if (devinfo.gen == 0) {
128 fail_if(!gen_get_device_info_from_pci_id(device, &devinfo),
129 "failed to identify chipset.\n");
130 }
131 }
132
133 static void *
relocate_bo(int fd,struct bo * bo,const struct drm_i915_gem_execbuffer2 * execbuffer2,const struct drm_i915_gem_exec_object2 * obj)134 relocate_bo(int fd, struct bo *bo, const struct drm_i915_gem_execbuffer2 *execbuffer2,
135 const struct drm_i915_gem_exec_object2 *obj)
136 {
137 const struct drm_i915_gem_exec_object2 *exec_objects =
138 (struct drm_i915_gem_exec_object2 *) (uintptr_t) execbuffer2->buffers_ptr;
139 const struct drm_i915_gem_relocation_entry *relocs =
140 (const struct drm_i915_gem_relocation_entry *) (uintptr_t) obj->relocs_ptr;
141 void *relocated;
142 int handle;
143
144 relocated = malloc(bo->size);
145 fail_if(relocated == NULL, "out of memory\n");
146 memcpy(relocated, GET_PTR(bo->map), bo->size);
147 for (size_t i = 0; i < obj->relocation_count; i++) {
148 fail_if(relocs[i].offset >= bo->size, "reloc outside bo\n");
149
150 if (execbuffer2->flags & I915_EXEC_HANDLE_LUT)
151 handle = exec_objects[relocs[i].target_handle].handle;
152 else
153 handle = relocs[i].target_handle;
154
155 aub_write_reloc(&devinfo, ((char *)relocated) + relocs[i].offset,
156 get_bo(fd, handle)->offset + relocs[i].delta);
157 }
158
159 return relocated;
160 }
161
162 static int
gem_ioctl(int fd,unsigned long request,void * argp)163 gem_ioctl(int fd, unsigned long request, void *argp)
164 {
165 int ret;
166
167 do {
168 ret = libc_ioctl(fd, request, argp);
169 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
170
171 return ret;
172 }
173
174 static void *
gem_mmap(int fd,uint32_t handle,uint64_t offset,uint64_t size)175 gem_mmap(int fd, uint32_t handle, uint64_t offset, uint64_t size)
176 {
177 struct drm_i915_gem_mmap mmap = {
178 .handle = handle,
179 .offset = offset,
180 .size = size
181 };
182
183 if (gem_ioctl(fd, DRM_IOCTL_I915_GEM_MMAP, &mmap) == -1)
184 return MAP_FAILED;
185
186 return (void *)(uintptr_t) mmap.addr_ptr;
187 }
188
189 static enum drm_i915_gem_engine_class
engine_class_from_ring_flag(uint32_t ring_flag)190 engine_class_from_ring_flag(uint32_t ring_flag)
191 {
192 switch (ring_flag) {
193 case I915_EXEC_DEFAULT:
194 case I915_EXEC_RENDER:
195 return I915_ENGINE_CLASS_RENDER;
196 case I915_EXEC_BSD:
197 return I915_ENGINE_CLASS_VIDEO;
198 case I915_EXEC_BLT:
199 return I915_ENGINE_CLASS_COPY;
200 case I915_EXEC_VEBOX:
201 return I915_ENGINE_CLASS_VIDEO_ENHANCE;
202 default:
203 return I915_ENGINE_CLASS_INVALID;
204 }
205 }
206
207 static void
dump_execbuffer2(int fd,struct drm_i915_gem_execbuffer2 * execbuffer2)208 dump_execbuffer2(int fd, struct drm_i915_gem_execbuffer2 *execbuffer2)
209 {
210 struct drm_i915_gem_exec_object2 *exec_objects =
211 (struct drm_i915_gem_exec_object2 *) (uintptr_t) execbuffer2->buffers_ptr;
212 uint32_t ring_flag = execbuffer2->flags & I915_EXEC_RING_MASK;
213 uint32_t offset;
214 struct drm_i915_gem_exec_object2 *obj;
215 struct bo *bo, *batch_bo;
216 int batch_index;
217 void *data;
218
219 ensure_device_info(fd);
220
221 if (capture_finished)
222 return;
223
224 if (!aub_file.file) {
225 aub_file_init(&aub_file, output_file,
226 verbose == 2 ? stdout : NULL,
227 device, program_invocation_short_name);
228 aub_write_default_setup(&aub_file);
229
230 if (verbose)
231 printf("[running, output file %s, chipset id 0x%04x, gen %d]\n",
232 output_filename, device, devinfo.gen);
233 }
234
235 if (aub_use_execlists(&aub_file))
236 offset = 0x1000;
237 else
238 offset = aub_gtt_size(&aub_file);
239
240 for (uint32_t i = 0; i < execbuffer2->buffer_count; i++) {
241 obj = &exec_objects[i];
242 bo = get_bo(fd, obj->handle);
243
244 /* If bo->size == 0, this means they passed us an invalid
245 * buffer. The kernel will reject it and so should we.
246 */
247 if (bo->size == 0) {
248 if (verbose)
249 printf("BO #%d is invalid!\n", obj->handle);
250 return;
251 }
252
253 if (obj->flags & EXEC_OBJECT_PINNED) {
254 bo->offset = obj->offset;
255 } else {
256 if (obj->alignment != 0)
257 offset = align_u32(offset, obj->alignment);
258 bo->offset = offset;
259 offset = align_u32(offset + bo->size + 4095, 4096);
260 }
261
262 if (bo->map == NULL && bo->size > 0)
263 bo->map = gem_mmap(fd, obj->handle, 0, bo->size);
264 fail_if(bo->map == MAP_FAILED, "bo mmap failed\n");
265 }
266
267 uint64_t current_frame_id = 0;
268 if (frame_id >= 0) {
269 for (uint32_t i = 0; i < execbuffer2->buffer_count; i++) {
270 obj = &exec_objects[i];
271 bo = get_bo(fd, obj->handle);
272
273 /* Check against frame_id requirements. */
274 if (memcmp(bo->map, intel_debug_identifier(),
275 intel_debug_identifier_size()) == 0) {
276 const struct gen_debug_block_frame *frame_desc =
277 intel_debug_get_identifier_block(bo->map, bo->size,
278 GEN_DEBUG_BLOCK_TYPE_FRAME);
279
280 current_frame_id = frame_desc ? frame_desc->frame_id : 0;
281 break;
282 }
283 }
284 }
285
286 if (verbose)
287 printf("Dumping execbuffer2 (frame_id=%"PRIu64", buffers=%u):\n",
288 current_frame_id, execbuffer2->buffer_count);
289
290 /* Check whether we can stop right now. */
291 if (frame_id >= 0) {
292 if (current_frame_id < frame_id)
293 return;
294
295 if (current_frame_id > frame_id) {
296 aub_file_finish(&aub_file);
297 capture_finished = true;
298 return;
299 }
300 }
301
302
303 /* Map buffers into the PPGTT. */
304 for (uint32_t i = 0; i < execbuffer2->buffer_count; i++) {
305 obj = &exec_objects[i];
306 bo = get_bo(fd, obj->handle);
307
308 if (verbose) {
309 printf("BO #%d (%dB) @ 0x%" PRIx64 "\n",
310 obj->handle, bo->size, bo->offset);
311 }
312
313 if (aub_use_execlists(&aub_file) && !bo->gtt_mapped) {
314 aub_map_ppgtt(&aub_file, bo->offset, bo->size);
315 bo->gtt_mapped = true;
316 }
317 }
318
319 /* Write the buffer content into the Aub. */
320 batch_index = (execbuffer2->flags & I915_EXEC_BATCH_FIRST) ? 0 :
321 execbuffer2->buffer_count - 1;
322 batch_bo = get_bo(fd, exec_objects[batch_index].handle);
323 for (uint32_t i = 0; i < execbuffer2->buffer_count; i++) {
324 obj = &exec_objects[i];
325 bo = get_bo(fd, obj->handle);
326
327 if (obj->relocation_count > 0)
328 data = relocate_bo(fd, bo, execbuffer2, obj);
329 else
330 data = bo->map;
331
332 bool write = !capture_only || (obj->flags & EXEC_OBJECT_CAPTURE);
333
334 if (write && bo->dirty) {
335 if (bo == batch_bo) {
336 aub_write_trace_block(&aub_file, AUB_TRACE_TYPE_BATCH,
337 GET_PTR(data), bo->size, bo->offset);
338 } else {
339 aub_write_trace_block(&aub_file, AUB_TRACE_TYPE_NOTYPE,
340 GET_PTR(data), bo->size, bo->offset);
341 }
342
343 if (!bo->user_mapped)
344 bo->dirty = false;
345 }
346
347 if (data != bo->map)
348 free(data);
349 }
350
351 uint32_t ctx_id = execbuffer2->rsvd1;
352
353 aub_write_exec(&aub_file, ctx_id,
354 batch_bo->offset + execbuffer2->batch_start_offset,
355 offset, engine_class_from_ring_flag(ring_flag));
356
357 if (device_override &&
358 (execbuffer2->flags & I915_EXEC_FENCE_ARRAY) != 0) {
359 struct drm_i915_gem_exec_fence *fences =
360 (void*)(uintptr_t)execbuffer2->cliprects_ptr;
361 for (uint32_t i = 0; i < execbuffer2->num_cliprects; i++) {
362 if ((fences[i].flags & I915_EXEC_FENCE_SIGNAL) != 0) {
363 struct drm_syncobj_array arg = {
364 .handles = (uintptr_t)&fences[i].handle,
365 .count_handles = 1,
366 .pad = 0,
367 };
368 libc_ioctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &arg);
369 }
370 }
371 }
372 }
373
374 static void
add_new_bo(unsigned fd,int handle,uint64_t size,void * map)375 add_new_bo(unsigned fd, int handle, uint64_t size, void *map)
376 {
377 struct bo *bo = &bos[handle + fd * MAX_BO_COUNT];
378
379 fail_if(handle >= MAX_BO_COUNT, "bo handle out of range\n");
380 fail_if(fd >= MAX_FD_COUNT, "bo fd out of range\n");
381 fail_if(size == 0, "bo size is invalid\n");
382
383 bo->size = size;
384 bo->map = map;
385 bo->user_mapped = false;
386 bo->gtt_mapped = false;
387 }
388
389 static void
remove_bo(int fd,int handle)390 remove_bo(int fd, int handle)
391 {
392 struct bo *bo = get_bo(fd, handle);
393
394 if (bo->map && !IS_USERPTR(bo->map))
395 munmap(bo->map, bo->size);
396 memset(bo, 0, sizeof(*bo));
397 }
398
399 __attribute__ ((visibility ("default"))) int
close(int fd)400 close(int fd)
401 {
402 if (fd == drm_fd)
403 drm_fd = -1;
404
405 return libc_close(fd);
406 }
407
408 static int
get_pci_id(int fd,int * pci_id)409 get_pci_id(int fd, int *pci_id)
410 {
411 struct drm_i915_getparam gparam;
412
413 if (device_override) {
414 *pci_id = device;
415 return 0;
416 }
417
418 gparam.param = I915_PARAM_CHIPSET_ID;
419 gparam.value = pci_id;
420 return libc_ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gparam);
421 }
422
423 static void
maybe_init(int fd)424 maybe_init(int fd)
425 {
426 static bool initialized = false;
427 FILE *config;
428 char *key, *value;
429
430 if (initialized)
431 return;
432
433 initialized = true;
434
435 const char *config_path = getenv("INTEL_DUMP_GPU_CONFIG");
436 fail_if(config_path == NULL, "INTEL_DUMP_GPU_CONFIG is not set\n");
437
438 config = fopen(config_path, "r");
439 fail_if(config == NULL, "failed to open file %s\n", config_path);
440
441 while (fscanf(config, "%m[^=]=%m[^\n]\n", &key, &value) != EOF) {
442 if (!strcmp(key, "verbose")) {
443 if (!strcmp(value, "1")) {
444 verbose = 1;
445 } else if (!strcmp(value, "2")) {
446 verbose = 2;
447 }
448 } else if (!strcmp(key, "device")) {
449 fail_if(device != 0, "Device/Platform override specified multiple times.\n");
450 fail_if(sscanf(value, "%i", &device) != 1,
451 "failed to parse device id '%s'\n",
452 value);
453 device_override = true;
454 } else if (!strcmp(key, "platform")) {
455 fail_if(device != 0, "Device/Platform override specified multiple times.\n");
456 device = gen_device_name_to_pci_device_id(value);
457 fail_if(device == -1, "Unknown platform '%s'\n", value);
458 device_override = true;
459 } else if (!strcmp(key, "file")) {
460 output_filename = strdup(value);
461 output_file = fopen(output_filename, "w+");
462 fail_if(output_file == NULL,
463 "failed to open file '%s'\n",
464 output_filename);
465 } else if (!strcmp(key, "capture_only")) {
466 capture_only = atoi(value);
467 } else if (!strcmp(key, "frame")) {
468 frame_id = atol(value);
469 } else {
470 fprintf(stderr, "unknown option '%s'\n", key);
471 }
472
473 free(key);
474 free(value);
475 }
476 fclose(config);
477
478 bos = calloc(MAX_FD_COUNT * MAX_BO_COUNT, sizeof(bos[0]));
479 fail_if(bos == NULL, "out of memory\n");
480
481 int ret = get_pci_id(fd, &device);
482 assert(ret == 0);
483
484 aub_file_init(&aub_file, output_file,
485 verbose == 2 ? stdout : NULL,
486 device, program_invocation_short_name);
487 aub_write_default_setup(&aub_file);
488
489 if (verbose)
490 printf("[running, output file %s, chipset id 0x%04x, gen %d]\n",
491 output_filename, device, devinfo.gen);
492 }
493
494 __attribute__ ((visibility ("default"))) int
ioctl(int fd,unsigned long request,...)495 ioctl(int fd, unsigned long request, ...)
496 {
497 va_list args;
498 void *argp;
499 int ret;
500 struct stat buf;
501
502 va_start(args, request);
503 argp = va_arg(args, void *);
504 va_end(args);
505
506 if (_IOC_TYPE(request) == DRM_IOCTL_BASE &&
507 drm_fd != fd && fstat(fd, &buf) == 0 &&
508 (buf.st_mode & S_IFMT) == S_IFCHR && major(buf.st_rdev) == DRM_MAJOR) {
509 drm_fd = fd;
510 if (verbose)
511 printf("[intercept drm ioctl on fd %d]\n", fd);
512 }
513
514 if (fd == drm_fd) {
515 maybe_init(fd);
516
517 switch (request) {
518 case DRM_IOCTL_SYNCOBJ_WAIT:
519 case DRM_IOCTL_I915_GEM_WAIT: {
520 if (device_override)
521 return 0;
522 return libc_ioctl(fd, request, argp);
523 }
524
525 case DRM_IOCTL_I915_GET_RESET_STATS: {
526 if (device_override) {
527 struct drm_i915_reset_stats *stats = argp;
528
529 stats->reset_count = 0;
530 stats->batch_active = 0;
531 stats->batch_pending = 0;
532 return 0;
533 }
534 return libc_ioctl(fd, request, argp);
535 }
536
537 case DRM_IOCTL_I915_GETPARAM: {
538 struct drm_i915_getparam *getparam = argp;
539
540 ensure_device_info(fd);
541
542 if (getparam->param == I915_PARAM_CHIPSET_ID)
543 return get_pci_id(fd, getparam->value);
544
545 if (device_override) {
546 switch (getparam->param) {
547 case I915_PARAM_CS_TIMESTAMP_FREQUENCY:
548 *getparam->value = devinfo.timestamp_frequency;
549 return 0;
550
551 case I915_PARAM_HAS_WAIT_TIMEOUT:
552 case I915_PARAM_HAS_EXECBUF2:
553 case I915_PARAM_MMAP_VERSION:
554 case I915_PARAM_HAS_EXEC_ASYNC:
555 case I915_PARAM_HAS_EXEC_FENCE:
556 case I915_PARAM_HAS_EXEC_FENCE_ARRAY:
557 *getparam->value = 1;
558 return 0;
559
560 case I915_PARAM_HAS_EXEC_SOFTPIN:
561 *getparam->value = devinfo.gen >= 8 && !devinfo.is_cherryview;
562 return 0;
563
564 default:
565 return -1;
566 }
567 }
568
569 return libc_ioctl(fd, request, argp);
570 }
571
572 case DRM_IOCTL_I915_GEM_CONTEXT_GETPARAM: {
573 struct drm_i915_gem_context_param *getparam = argp;
574
575 ensure_device_info(fd);
576
577 if (device_override) {
578 switch (getparam->param) {
579 case I915_CONTEXT_PARAM_GTT_SIZE:
580 if (devinfo.is_elkhartlake)
581 getparam->value = 1ull << 36;
582 else if (devinfo.gen >= 8 && !devinfo.is_cherryview)
583 getparam->value = 1ull << 48;
584 else
585 getparam->value = 1ull << 31;
586 return 0;
587
588 default:
589 return -1;
590 }
591 }
592
593 return libc_ioctl(fd, request, argp);
594 }
595
596 case DRM_IOCTL_I915_GEM_EXECBUFFER: {
597 static bool once;
598 if (!once) {
599 fprintf(stderr,
600 "application uses DRM_IOCTL_I915_GEM_EXECBUFFER, not handled\n");
601 once = true;
602 }
603 return libc_ioctl(fd, request, argp);
604 }
605
606 case DRM_IOCTL_I915_GEM_EXECBUFFER2:
607 case DRM_IOCTL_I915_GEM_EXECBUFFER2_WR: {
608 dump_execbuffer2(fd, argp);
609 if (device_override)
610 return 0;
611
612 return libc_ioctl(fd, request, argp);
613 }
614
615 case DRM_IOCTL_I915_GEM_CONTEXT_CREATE: {
616 uint32_t *ctx_id = NULL;
617 struct drm_i915_gem_context_create *create = argp;
618 ret = 0;
619 if (!device_override) {
620 ret = libc_ioctl(fd, request, argp);
621 ctx_id = &create->ctx_id;
622 }
623
624 if (ret == 0)
625 create->ctx_id = aub_write_context_create(&aub_file, ctx_id);
626
627 return ret;
628 }
629
630 case DRM_IOCTL_I915_GEM_CONTEXT_CREATE_EXT: {
631 uint32_t *ctx_id = NULL;
632 struct drm_i915_gem_context_create_ext *create = argp;
633 ret = 0;
634 if (!device_override) {
635 ret = libc_ioctl(fd, request, argp);
636 ctx_id = &create->ctx_id;
637 }
638
639 if (ret == 0)
640 create->ctx_id = aub_write_context_create(&aub_file, ctx_id);
641
642 return ret;
643 }
644
645 case DRM_IOCTL_I915_GEM_CREATE: {
646 struct drm_i915_gem_create *create = argp;
647
648 ret = libc_ioctl(fd, request, argp);
649 if (ret == 0)
650 add_new_bo(fd, create->handle, create->size, NULL);
651
652 return ret;
653 }
654
655 case DRM_IOCTL_I915_GEM_USERPTR: {
656 struct drm_i915_gem_userptr *userptr = argp;
657
658 ret = libc_ioctl(fd, request, argp);
659 if (ret == 0)
660 add_new_bo(fd, userptr->handle, userptr->user_size,
661 (void *) (uintptr_t) (userptr->user_ptr | USERPTR_FLAG));
662
663 return ret;
664 }
665
666 case DRM_IOCTL_GEM_CLOSE: {
667 struct drm_gem_close *close = argp;
668
669 remove_bo(fd, close->handle);
670
671 return libc_ioctl(fd, request, argp);
672 }
673
674 case DRM_IOCTL_GEM_OPEN: {
675 struct drm_gem_open *open = argp;
676
677 ret = libc_ioctl(fd, request, argp);
678 if (ret == 0)
679 add_new_bo(fd, open->handle, open->size, NULL);
680
681 return ret;
682 }
683
684 case DRM_IOCTL_PRIME_FD_TO_HANDLE: {
685 struct drm_prime_handle *prime = argp;
686
687 ret = libc_ioctl(fd, request, argp);
688 if (ret == 0) {
689 off_t size;
690
691 size = lseek(prime->fd, 0, SEEK_END);
692 fail_if(size == -1, "failed to get prime bo size\n");
693 add_new_bo(fd, prime->handle, size, NULL);
694
695 }
696
697 return ret;
698 }
699
700 case DRM_IOCTL_I915_GEM_MMAP: {
701 ret = libc_ioctl(fd, request, argp);
702 if (ret == 0) {
703 struct drm_i915_gem_mmap *mmap = argp;
704 struct bo *bo = get_bo(fd, mmap->handle);
705 bo->user_mapped = true;
706 bo->dirty = true;
707 }
708 return ret;
709 }
710
711 default:
712 return libc_ioctl(fd, request, argp);
713 }
714 } else {
715 return libc_ioctl(fd, request, argp);
716 }
717 }
718
719 static void
init(void)720 init(void)
721 {
722 libc_close = dlsym(RTLD_NEXT, "close");
723 libc_ioctl = dlsym(RTLD_NEXT, "ioctl");
724 libc_munmap = dlsym(RTLD_NEXT, "munmap");
725 fail_if(libc_close == NULL || libc_ioctl == NULL,
726 "failed to get libc ioctl or close\n");
727 }
728
729 static int
close_init_helper(int fd)730 close_init_helper(int fd)
731 {
732 init();
733 return libc_close(fd);
734 }
735
736 static int
ioctl_init_helper(int fd,unsigned long request,...)737 ioctl_init_helper(int fd, unsigned long request, ...)
738 {
739 va_list args;
740 void *argp;
741
742 va_start(args, request);
743 argp = va_arg(args, void *);
744 va_end(args);
745
746 init();
747 return libc_ioctl(fd, request, argp);
748 }
749
750 static int
munmap_init_helper(void * addr,size_t length)751 munmap_init_helper(void *addr, size_t length)
752 {
753 init();
754 for (uint32_t i = 0; i < MAX_FD_COUNT * MAX_BO_COUNT; i++) {
755 struct bo *bo = &bos[i];
756 if (bo->map == addr) {
757 bo->user_mapped = false;
758 break;
759 }
760 }
761 return libc_munmap(addr, length);
762 }
763
764 static void __attribute__ ((destructor))
fini(void)765 fini(void)
766 {
767 if (devinfo.gen != 0) {
768 free(output_filename);
769 if (!capture_finished)
770 aub_file_finish(&aub_file);
771 free(bos);
772 }
773 }
774