xref: /aosp_15_r20/external/mesa3d/src/egl/main/eglglobals.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2008 VMware, Inc.
4  * Copyright 2009-2010 Chia-I Wu <[email protected]>
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 #include <assert.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include "util/simple_mtx.h"
35 
36 #include "egldevice.h"
37 #include "egldisplay.h"
38 #include "eglglobals.h"
39 
40 #include "util/macros.h"
41 #include "util/os_misc.h"
42 
43 #ifdef HAVE_MINCORE
44 #include <unistd.h>
45 #include <sys/mman.h>
46 #endif
47 
48 static simple_mtx_t _eglGlobalMutex = SIMPLE_MTX_INITIALIZER;
49 
50 struct _egl_global _eglGlobal = {
51    .Mutex = &_eglGlobalMutex,
52    .DisplayList = NULL,
53    .DeviceList = &_eglSoftwareDevice,
54    .NumAtExitCalls = 2,
55    .AtExitCalls =
56       {
57          /* default AtExitCalls, called in reverse order */
58          _eglFiniDevice, /* always called last */
59          _eglFiniDisplay,
60       },
61 
62 #if USE_LIBGLVND
63    .ClientOnlyExtensionString =
64 #else
65    .ClientExtensionString =
66 #endif
67       "EGL_EXT_client_extensions"
68 #if !DETECT_OS_WINDOWS
69       " EGL_EXT_device_base"
70       " EGL_EXT_device_enumeration"
71       " EGL_EXT_device_query"
72 #endif
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 #if !DETECT_OS_WINDOWS
85       "EGL_EXT_platform_device"
86       " EGL_EXT_explicit_device"
87 #endif
88 #ifdef HAVE_WAYLAND_PLATFORM
89       " EGL_EXT_platform_wayland"
90       " EGL_KHR_platform_wayland"
91 #endif
92 #ifdef HAVE_X11_PLATFORM
93       " EGL_EXT_platform_x11"
94       " EGL_KHR_platform_x11"
95 #endif
96 #ifdef HAVE_XCB_PLATFORM
97       " EGL_EXT_platform_xcb"
98 #endif
99 #ifdef HAVE_DRM_PLATFORM
100       " EGL_MESA_platform_gbm"
101       " EGL_KHR_platform_gbm"
102 #endif
103       " EGL_MESA_platform_surfaceless"
104       "",
105 
106    .debugCallback = NULL,
107    .debugTypesEnabled = _EGL_DEBUG_BIT_CRITICAL | _EGL_DEBUG_BIT_ERROR,
108 };
109 
110 static void
_eglAtExit(void)111 _eglAtExit(void)
112 {
113    EGLint i;
114    for (i = _eglGlobal.NumAtExitCalls - 1; i >= 0; i--)
115       _eglGlobal.AtExitCalls[i]();
116 }
117 
118 void
_eglAddAtExitCall(void (* func)(void))119 _eglAddAtExitCall(void (*func)(void))
120 {
121    if (func) {
122       static EGLBoolean registered = EGL_FALSE;
123 
124       simple_mtx_lock(_eglGlobal.Mutex);
125 
126       if (!registered) {
127          atexit(_eglAtExit);
128          registered = EGL_TRUE;
129       }
130 
131       assert(_eglGlobal.NumAtExitCalls < ARRAY_SIZE(_eglGlobal.AtExitCalls));
132       _eglGlobal.AtExitCalls[_eglGlobal.NumAtExitCalls++] = func;
133 
134       simple_mtx_unlock(_eglGlobal.Mutex);
135    }
136 }
137 
138 EGLBoolean
_eglPointerIsDereferenceable(void * p)139 _eglPointerIsDereferenceable(void *p)
140 {
141    uintptr_t addr = (uintptr_t)p;
142    uint64_t page_size = 0;
143    os_get_page_size(&page_size);
144 #ifdef HAVE_MINCORE
145    unsigned char valid = 0;
146 
147    if (p == NULL)
148       return EGL_FALSE;
149 
150    /* align addr to page_size */
151    addr &= ~(page_size - 1);
152 
153    /* mincore expects &valid to be unsigned char* on Linux but char* on BSD:
154     * we cast pointers to void, to fix type mismatch warnings in all systems
155     */
156    if (mincore((void *)addr, page_size, (void *)&valid) < 0) {
157       return EGL_FALSE;
158    }
159 
160    /* mincore() returns 0 on success, and -1 on failure.  The last parameter
161     * is a vector of bytes with one entry for each page queried.  mincore
162     * returns page residency information in the first bit of each byte in the
163     * vector.
164     *
165     * Residency doesn't actually matter when determining whether a pointer is
166     * dereferenceable, so the output vector can be ignored.  What matters is
167     * whether mincore succeeds. See:
168     *
169     *   http://man7.org/linux/man-pages/man2/mincore.2.html
170     */
171    return EGL_TRUE;
172 #else
173    // Without mincore(), we just assume that the first page is unmapped.
174    return addr >= page_size;
175 #endif
176 }
177