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