egl: drop confusing mincore() error message
[mesa.git] / src / egl / main / eglglobals.c
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * Copyright 2009-2010 Chia-I Wu <olvaffe@gmail.com>
5 * Copyright 2010-2011 LunarG, Inc.
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 *
28 **************************************************************************/
29
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <assert.h>
35 #include "c11/threads.h"
36
37 #include "eglglobals.h"
38 #include "egldevice.h"
39 #include "egldisplay.h"
40 #include "egldriver.h"
41
42 #include "util/macros.h"
43
44 #ifdef HAVE_MINCORE
45 #include <unistd.h>
46 #include <sys/mman.h>
47 #endif
48
49
50 static mtx_t _eglGlobalMutex = _MTX_INITIALIZER_NP;
51
52 struct _egl_global _eglGlobal =
53 {
54 .Mutex = &_eglGlobalMutex,
55 .DisplayList = NULL,
56 .DeviceList = &_eglSoftwareDevice,
57 .NumAtExitCalls = 3,
58 .AtExitCalls = {
59 /* default AtExitCalls, called in reverse order */
60 _eglFiniDevice, /* always called last */
61 _eglUnloadDrivers,
62 _eglFiniDisplay,
63 },
64
65 .ClientOnlyExtensionString =
66 "EGL_EXT_client_extensions"
67 " EGL_EXT_device_base"
68 " EGL_EXT_device_enumeration"
69 " EGL_EXT_device_query"
70 " EGL_EXT_platform_base"
71 " EGL_KHR_client_get_all_proc_addresses"
72 " EGL_KHR_debug",
73
74 .PlatformExtensionString =
75 #ifdef HAVE_WAYLAND_PLATFORM
76 " EGL_EXT_platform_wayland"
77 #endif
78 #ifdef HAVE_X11_PLATFORM
79 " EGL_EXT_platform_x11"
80 #endif
81 #ifdef HAVE_DRM_PLATFORM
82 " EGL_MESA_platform_gbm"
83 #endif
84 #ifdef HAVE_SURFACELESS_PLATFORM
85 " EGL_MESA_platform_surfaceless"
86 #endif
87 " EGL_EXT_platform_device"
88 "",
89
90 .ClientExtensionString = NULL,
91
92 .debugCallback = NULL,
93 .debugTypesEnabled = _EGL_DEBUG_BIT_CRITICAL | _EGL_DEBUG_BIT_ERROR,
94 };
95
96
97 static void
98 _eglAtExit(void)
99 {
100 EGLint i;
101 for (i = _eglGlobal.NumAtExitCalls - 1; i >= 0; i--)
102 _eglGlobal.AtExitCalls[i]();
103 }
104
105
106 void
107 _eglAddAtExitCall(void (*func)(void))
108 {
109 if (func) {
110 static EGLBoolean registered = EGL_FALSE;
111
112 mtx_lock(_eglGlobal.Mutex);
113
114 if (!registered) {
115 atexit(_eglAtExit);
116 registered = EGL_TRUE;
117 }
118
119 assert(_eglGlobal.NumAtExitCalls < ARRAY_SIZE(_eglGlobal.AtExitCalls));
120 _eglGlobal.AtExitCalls[_eglGlobal.NumAtExitCalls++] = func;
121
122 mtx_unlock(_eglGlobal.Mutex);
123 }
124 }
125
126 const char *
127 _eglGetClientExtensionString(void)
128 {
129 const char *ret;
130
131 mtx_lock(_eglGlobal.Mutex);
132
133 if (_eglGlobal.ClientExtensionString == NULL) {
134 size_t clientLen = strlen(_eglGlobal.ClientOnlyExtensionString);
135 size_t platformLen = strlen(_eglGlobal.PlatformExtensionString);
136
137 _eglGlobal.ClientExtensionString = (char *) malloc(clientLen + platformLen + 1);
138 if (_eglGlobal.ClientExtensionString != NULL) {
139 char *ptr = _eglGlobal.ClientExtensionString;
140
141 memcpy(ptr, _eglGlobal.ClientOnlyExtensionString, clientLen);
142 ptr += clientLen;
143
144 if (platformLen > 0) {
145 // Note that if PlatformExtensionString is not empty, then it will
146 // already have a leading space.
147 assert(_eglGlobal.PlatformExtensionString[0] == ' ');
148 memcpy(ptr, _eglGlobal.PlatformExtensionString, platformLen);
149 ptr += platformLen;
150 }
151 *ptr = '\0';
152 }
153 }
154 ret = _eglGlobal.ClientExtensionString;
155
156 mtx_unlock(_eglGlobal.Mutex);
157 return ret;
158 }
159
160 EGLBoolean
161 _eglPointerIsDereferencable(void *p)
162 {
163 uintptr_t addr = (uintptr_t) p;
164 const long page_size = getpagesize();
165 #ifdef HAVE_MINCORE
166 unsigned char valid = 0;
167
168 if (p == NULL)
169 return EGL_FALSE;
170
171 /* align addr to page_size */
172 addr &= ~(page_size - 1);
173
174 if (mincore((void *) addr, page_size, &valid) < 0) {
175 return EGL_FALSE;
176 }
177
178 /* mincore() returns 0 on success, and -1 on failure. The last parameter
179 * is a vector of bytes with one entry for each page queried. mincore
180 * returns page residency information in the first bit of each byte in the
181 * vector.
182 *
183 * Residency doesn't actually matter when determining whether a pointer is
184 * dereferenceable, so the output vector can be ignored. What matters is
185 * whether mincore succeeds. See:
186 *
187 * http://man7.org/linux/man-pages/man2/mincore.2.html
188 */
189 return EGL_TRUE;
190 #else
191 // Without mincore(), we just assume that the first page is unmapped.
192 return addr >= page_size;
193 #endif
194 }