blob: 729a4d3398b78a4b1f1bf34830035e63eeadf32b [file] [log] [blame]
Chris Wilson5c9c8842014-02-04 14:14:31 +00001/*
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
Thomas Wood804e11f2015-08-17 17:57:43 +010028#include "igt.h"
Chris Wilson5c9c8842014-02-04 14:14:31 +000029#include <unistd.h>
30#include <stdlib.h>
31#include <stdio.h>
32#include <string.h>
33#include <fcntl.h>
34#include <inttypes.h>
35#include <errno.h>
36#include <setjmp.h>
37#include <signal.h>
38
39#include "drm.h"
Chris Wilson5c9c8842014-02-04 14:14:31 +000040
Thomas Woodb2ac2642014-11-28 11:02:44 +000041IGT_TEST_DESCRIPTION("Checks that the kernel reports EFAULT when trying to use"
42 " purged bo.");
43
Chris Wilson5c9c8842014-02-04 14:14:31 +000044#define OBJECT_SIZE (1024*1024)
45
46/* Testcase: checks that the kernel reports EFAULT when trying to use purged bo
47 *
48 */
49
50static jmp_buf jmp;
51
Thomas Wood36b81432015-11-02 10:02:08 +000052static void __attribute__((noreturn)) sigtrap(int sig)
Chris Wilson5c9c8842014-02-04 14:14:31 +000053{
54 longjmp(jmp, sig);
55}
56
57static void
58dontneed_before_mmap(void)
59{
Micah Fedkec81d2932015-07-22 21:54:02 +000060 int fd = drm_open_driver(DRIVER_INTEL);
Chris Wilson5c9c8842014-02-04 14:14:31 +000061 uint32_t handle;
62 char *ptr;
63
64 handle = gem_create(fd, OBJECT_SIZE);
65 gem_madvise(fd, handle, I915_MADV_DONTNEED);
Chris Wilsonb9e2db92016-08-05 09:37:00 +010066 ptr = gem_mmap__gtt(fd, handle, OBJECT_SIZE, PROT_READ | PROT_WRITE);
Chris Wilson5c9c8842014-02-04 14:14:31 +000067 close(fd);
Chris Wilsonb9e2db92016-08-05 09:37:00 +010068
69 signal(SIGSEGV, sigtrap);
70 signal(SIGBUS, sigtrap);
71 switch (setjmp(jmp)) {
72 case SIGBUS:
73 break;
74 case 0:
75 *ptr = 0;
76 default:
77 igt_assert(!"reached");
78 break;
79 }
80 munmap(ptr, OBJECT_SIZE);
81 signal(SIGBUS, SIG_DFL);
82 signal(SIGSEGV, SIG_DFL);
Chris Wilson5c9c8842014-02-04 14:14:31 +000083}
84
85static void
86dontneed_after_mmap(void)
87{
Micah Fedkec81d2932015-07-22 21:54:02 +000088 int fd = drm_open_driver(DRIVER_INTEL);
Chris Wilson5c9c8842014-02-04 14:14:31 +000089 uint32_t handle;
90 char *ptr;
91
92 handle = gem_create(fd, OBJECT_SIZE);
Chris Wilsonb9e2db92016-08-05 09:37:00 +010093 ptr = gem_mmap__gtt(fd, handle, OBJECT_SIZE, PROT_READ | PROT_WRITE);
Ville Syrjälä91d295c2015-10-09 17:00:29 +030094 igt_assert(ptr);
Chris Wilson5c9c8842014-02-04 14:14:31 +000095 gem_madvise(fd, handle, I915_MADV_DONTNEED);
96 close(fd);
97
98 signal(SIGBUS, sigtrap);
99 switch (setjmp(jmp)) {
100 case SIGBUS:
101 break;
102 case 0:
103 *ptr = 0;
104 default:
105 igt_assert(!"reached");
106 break;
107 }
108 munmap(ptr, OBJECT_SIZE);
109 signal(SIGBUS, SIG_DFL);
110}
111
112static void
113dontneed_before_pwrite(void)
114{
Micah Fedkec81d2932015-07-22 21:54:02 +0000115 int fd = drm_open_driver(DRIVER_INTEL);
Chris Wilson8fcca192017-03-08 12:22:58 +0000116 uint32_t bbe = MI_BATCH_BUFFER_END;
117 uint32_t handle;
Chris Wilson5c9c8842014-02-04 14:14:31 +0000118
Chris Wilson8fcca192017-03-08 12:22:58 +0000119 handle = gem_create(fd, OBJECT_SIZE);
120 gem_madvise(fd, handle, I915_MADV_DONTNEED);
Chris Wilson5c9c8842014-02-04 14:14:31 +0000121
Chris Wilson8fcca192017-03-08 12:22:58 +0000122 igt_assert_eq(__gem_write(fd, handle, 0, &bbe, sizeof(bbe)), -EFAULT);
Chris Wilson5c9c8842014-02-04 14:14:31 +0000123
Chris Wilson5c9c8842014-02-04 14:14:31 +0000124 close(fd);
125}
126
127static void
128dontneed_before_exec(void)
129{
Micah Fedkec81d2932015-07-22 21:54:02 +0000130 int fd = drm_open_driver(DRIVER_INTEL);
Chris Wilson5c9c8842014-02-04 14:14:31 +0000131 struct drm_i915_gem_execbuffer2 execbuf;
132 struct drm_i915_gem_exec_object2 exec;
133 uint32_t buf[] = { MI_BATCH_BUFFER_END, 0 };
134
135 memset(&execbuf, 0, sizeof(execbuf));
136 memset(&exec, 0, sizeof(exec));
137
138 exec.handle = gem_create(fd, OBJECT_SIZE);
139 gem_write(fd, exec.handle, 0, buf, sizeof(buf));
140 gem_madvise(fd, exec.handle, I915_MADV_DONTNEED);
141
Chris Wilson4de67b22017-01-02 11:05:21 +0000142 execbuf.buffers_ptr = to_user_pointer(&exec);
Chris Wilson5c9c8842014-02-04 14:14:31 +0000143 execbuf.buffer_count = 1;
Brad Volkinbf31ed82014-11-03 11:19:00 -0800144 execbuf.batch_len = sizeof(buf);
Chris Wilson8fcca192017-03-08 12:22:58 +0000145 igt_assert_eq(__gem_execbuf(fd, &execbuf), -EFAULT);
Chris Wilson5c9c8842014-02-04 14:14:31 +0000146
Chris Wilson5c9c8842014-02-04 14:14:31 +0000147 close(fd);
148}
149
Daniel Vetter36be04b2014-02-11 23:52:43 +0100150igt_main
Chris Wilson5c9c8842014-02-04 14:14:31 +0000151{
152 igt_skip_on_simulation();
153
154 igt_subtest("dontneed-before-mmap")
155 dontneed_before_mmap();
156
157 igt_subtest("dontneed-after-mmap")
158 dontneed_after_mmap();
159
160 igt_subtest("dontneed-before-pwrite")
161 dontneed_before_pwrite();
162
163 igt_subtest("dontneed-before-exec")
164 dontneed_before_exec();
165}