2 * Copyright © 2014 Intel Corporation
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:
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
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
24 * Chris Wilson <chris@chris-wilson.co.uk>
39 #include "ioctl_wrappers.h"
42 #define OBJECT_SIZE (1024*1024)
44 /* Testcase: checks that the kernel reports EFAULT when trying to use purged bo
50 static void sigtrap(int sig)
56 dontneed_before_mmap(void)
58 int fd = drm_open_any();
62 handle = gem_create(fd, OBJECT_SIZE);
63 gem_madvise(fd, handle, I915_MADV_DONTNEED);
64 ptr = gem_mmap(fd, handle, OBJECT_SIZE, PROT_READ | PROT_WRITE);
65 igt_assert(ptr == NULL);
66 igt_assert(errno == EFAULT);
71 dontneed_after_mmap(void)
73 int fd = drm_open_any();
77 handle = gem_create(fd, OBJECT_SIZE);
78 ptr = gem_mmap(fd, handle, OBJECT_SIZE, PROT_READ | PROT_WRITE);
79 igt_assert(ptr != NULL);
80 gem_madvise(fd, handle, I915_MADV_DONTNEED);
83 signal(SIGBUS, sigtrap);
84 switch (setjmp(jmp)) {
90 igt_assert(!"reached");
93 munmap(ptr, OBJECT_SIZE);
94 signal(SIGBUS, SIG_DFL);
98 dontneed_before_pwrite(void)
100 int fd = drm_open_any();
101 uint32_t buf[] = { MI_BATCH_BUFFER_END, 0 };
102 struct drm_i915_gem_pwrite gem_pwrite;
104 gem_pwrite.handle = gem_create(fd, OBJECT_SIZE);
105 gem_pwrite.offset = 0;
106 gem_pwrite.size = sizeof(buf);
107 gem_pwrite.data_ptr = (uintptr_t)buf;
108 gem_madvise(fd, gem_pwrite.handle, I915_MADV_DONTNEED);
110 igt_assert(drmIoctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &gem_pwrite));
111 igt_assert(errno == EFAULT);
113 gem_close(fd, gem_pwrite.handle);
118 dontneed_before_exec(void)
120 int fd = drm_open_any();
121 struct drm_i915_gem_execbuffer2 execbuf;
122 struct drm_i915_gem_exec_object2 exec;
123 uint32_t buf[] = { MI_BATCH_BUFFER_END, 0 };
125 memset(&execbuf, 0, sizeof(execbuf));
126 memset(&exec, 0, sizeof(exec));
128 exec.handle = gem_create(fd, OBJECT_SIZE);
129 gem_write(fd, exec.handle, 0, buf, sizeof(buf));
130 gem_madvise(fd, exec.handle, I915_MADV_DONTNEED);
132 execbuf.buffers_ptr = (uintptr_t)&exec;
133 execbuf.buffer_count = 1;
134 gem_execbuf(fd, &execbuf);
136 gem_close(fd, exec.handle);
142 igt_skip_on_simulation();
144 igt_subtest("dontneed-before-mmap")
145 dontneed_before_mmap();
147 igt_subtest("dontneed-after-mmap")
148 dontneed_after_mmap();
150 igt_subtest("dontneed-before-pwrite")
151 dontneed_before_pwrite();
153 igt_subtest("dontneed-before-exec")
154 dontneed_before_exec();