• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
41 #include "util/macros.h"
42 #include "util/os_misc.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 = 2,
58    .AtExitCalls = {
59       /* default AtExitCalls, called in reverse order */
60       _eglFiniDevice, /* always called last */
61       _eglFiniDisplay,
62    },
63 
64 #if USE_LIBGLVND
65    .ClientOnlyExtensionString =
66 #else
67    .ClientExtensionString =
68 #endif
69    "EGL_EXT_client_extensions"
70    " EGL_EXT_device_base"
71    " EGL_EXT_device_enumeration"
72    " EGL_EXT_device_query"
73    " EGL_EXT_platform_base"
74    " EGL_KHR_client_get_all_proc_addresses"
75    " EGL_KHR_debug"
76 
77 #if USE_LIBGLVND
78    ,
79    .PlatformExtensionString =
80 #else
81    " "
82 #endif
83 
84    "EGL_EXT_platform_device"
85 #ifdef HAVE_WAYLAND_PLATFORM
86    " EGL_EXT_platform_wayland"
87    " EGL_KHR_platform_wayland"
88 #endif
89 #ifdef HAVE_X11_PLATFORM
90    " EGL_EXT_platform_x11"
91    " EGL_KHR_platform_x11"
92 #endif
93 #ifdef HAVE_XCB_PLATFORM
94    " EGL_MESA_platform_xcb"
95 #endif
96 #ifdef HAVE_DRM_PLATFORM
97    " EGL_MESA_platform_gbm"
98    " EGL_KHR_platform_gbm"
99 #endif
100    " EGL_MESA_platform_surfaceless"
101    "",
102 
103    .debugCallback = NULL,
104    .debugTypesEnabled = _EGL_DEBUG_BIT_CRITICAL | _EGL_DEBUG_BIT_ERROR,
105 };
106 
107 
108 static void
_eglAtExit(void)109 _eglAtExit(void)
110 {
111    EGLint i;
112    for (i = _eglGlobal.NumAtExitCalls - 1; i >= 0; i--)
113       _eglGlobal.AtExitCalls[i]();
114 }
115 
116 
117 void
_eglAddAtExitCall(void (* func)(void))118 _eglAddAtExitCall(void (*func)(void))
119 {
120    if (func) {
121       static EGLBoolean registered = EGL_FALSE;
122 
123       mtx_lock(_eglGlobal.Mutex);
124 
125       if (!registered) {
126          atexit(_eglAtExit);
127          registered = EGL_TRUE;
128       }
129 
130       assert(_eglGlobal.NumAtExitCalls < ARRAY_SIZE(_eglGlobal.AtExitCalls));
131       _eglGlobal.AtExitCalls[_eglGlobal.NumAtExitCalls++] = func;
132 
133       mtx_unlock(_eglGlobal.Mutex);
134    }
135 }
136 
137 EGLBoolean
_eglPointerIsDereferencable(void * p)138 _eglPointerIsDereferencable(void *p)
139 {
140    uintptr_t addr = (uintptr_t) p;
141    uint64_t page_size = 0;
142    os_get_page_size(&page_size);
143 #ifdef HAVE_MINCORE
144    unsigned char valid = 0;
145 
146    if (p == NULL)
147       return EGL_FALSE;
148 
149    /* align addr to page_size */
150    addr &= ~(page_size - 1);
151 
152    /* mincore expects &valid to be unsigned char* on Linux but char* on BSD:
153     * we cast pointers to void, to fix type mismatch warnings in all systems
154     */
155    if (mincore((void *) addr, page_size, (void*)&valid) < 0) {
156       return EGL_FALSE;
157    }
158 
159    /* mincore() returns 0 on success, and -1 on failure.  The last parameter
160     * is a vector of bytes with one entry for each page queried.  mincore
161     * returns page residency information in the first bit of each byte in the
162     * vector.
163     *
164     * Residency doesn't actually matter when determining whether a pointer is
165     * dereferenceable, so the output vector can be ignored.  What matters is
166     * whether mincore succeeds. See:
167     *
168     *   http://man7.org/linux/man-pages/man2/mincore.2.html
169     */
170    return EGL_TRUE;
171 #else
172    // Without mincore(), we just assume that the first page is unmapped.
173    return addr >= page_size;
174 #endif
175 }
176