lib/igt.cocci: Add s/assert/igt_assert/
[platform/upstream/intel-gpu-tools.git] / tests / gem_madvise.c
1 /*
2  * Copyright © 2014 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  * Authors:
24  *    Chris Wilson <chris@chris-wilson.co.uk>
25  *
26  */
27
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <inttypes.h>
34 #include <errno.h>
35 #include <setjmp.h>
36 #include <signal.h>
37
38 #include "drm.h"
39 #include "ioctl_wrappers.h"
40 #include "drmtest.h"
41
42 #define OBJECT_SIZE (1024*1024)
43
44 /* Testcase: checks that the kernel reports EFAULT when trying to use purged bo
45  *
46  */
47
48 static jmp_buf jmp;
49
50 static void sigtrap(int sig)
51 {
52         longjmp(jmp, sig);
53 }
54
55 static void
56 dontneed_before_mmap(void)
57 {
58         int fd = drm_open_any();
59         uint32_t handle;
60         char *ptr;
61
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);
67         close(fd);
68 }
69
70 static void
71 dontneed_after_mmap(void)
72 {
73         int fd = drm_open_any();
74         uint32_t handle;
75         char *ptr;
76
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);
81         close(fd);
82
83         signal(SIGBUS, sigtrap);
84         switch (setjmp(jmp)) {
85         case SIGBUS:
86                 break;
87         case 0:
88                 *ptr = 0;
89         default:
90                 igt_assert(!"reached");
91                 break;
92         }
93         munmap(ptr, OBJECT_SIZE);
94         signal(SIGBUS, SIG_DFL);
95 }
96
97 static void
98 dontneed_before_pwrite(void)
99 {
100         int fd = drm_open_any();
101         uint32_t buf[] = { MI_BATCH_BUFFER_END, 0 };
102         struct drm_i915_gem_pwrite gem_pwrite;
103
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);
109
110         igt_assert(drmIoctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &gem_pwrite));
111         igt_assert(errno == EFAULT);
112
113         gem_close(fd, gem_pwrite.handle);
114         close(fd);
115 }
116
117 static void
118 dontneed_before_exec(void)
119 {
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 };
124
125         memset(&execbuf, 0, sizeof(execbuf));
126         memset(&exec, 0, sizeof(exec));
127
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);
131
132         execbuf.buffers_ptr = (uintptr_t)&exec;
133         execbuf.buffer_count = 1;
134         gem_execbuf(fd, &execbuf);
135
136         gem_close(fd, exec.handle);
137         close(fd);
138 }
139
140 igt_main
141 {
142         igt_skip_on_simulation();
143
144         igt_subtest("dontneed-before-mmap")
145                 dontneed_before_mmap();
146
147         igt_subtest("dontneed-after-mmap")
148                 dontneed_after_mmap();
149
150         igt_subtest("dontneed-before-pwrite")
151                 dontneed_before_pwrite();
152
153         igt_subtest("dontneed-before-exec")
154                 dontneed_before_exec();
155 }