GLdispatch: Define dynamic dispatch stubs at compile time.

Instead of allocating pages of memory for the dynamic dispatch stubs, define
them at build time like the static stubs.

The dynamic stubs are identical to the static ones, so we can use the same code
to define both the static and dynamic stubs at compile time.

Removed entry_generate(). entry_generate_default_code is now only used to
restore entrypoints after patching.

Aside from simplifying things by not having a separate allocation for the
dynamic stubs, this should also allow the dispatch stubs to function properly
in both processes after a call to fork.
This commit is contained in:
Kyle Brenneman 2017-04-29 16:32:56 -06:00
parent 1613facce8
commit 86a9ed579b
17 changed files with 31 additions and 238 deletions

View file

@ -27,7 +27,6 @@ noinst_HEADERS = \
table.h \
u_compiler.h \
u_current.h \
u_execmem.h \
u_macros.h
noinst_LTLIBRARIES = libglapi.la
@ -36,8 +35,7 @@ libglapi_la_SOURCES = \
$(MAPI_GLDISPATCH_ENTRY_FILES) \
mapi_glapi.c \
stub.c \
table.c \
u_execmem.c
table.c
# Select the appropriate file for looking up the current dispatch table.
if GLDISPATCH_USE_TLS

View file

@ -53,19 +53,6 @@ entry_init_public(void);
mapi_func
entry_get_public(int index);
/**
* Generates an entrypoint for an extension function.
*
* This will allocate executable memory and generate an entrypoint function.
* This is used to dispatch any OpenGL functions that are not known at compile
* time.
*
* \param slot The slot in the dispatch table.
* \return A newly generated entrypoint function, or NULL on failure.
*/
mapi_func
entry_generate(int slot);
void
entry_generate_default_code(char *entry, int slot);

View file

@ -46,7 +46,7 @@
*/
/*
* u_execmem_alloc() allocates 128 bytes per stub.
* The size of each dispatch stub.
*/
#define ENTRY_STUB_ALIGN 128
#if !defined(GLDISPATCH_PAGE_SIZE)
@ -155,10 +155,7 @@ static const int TEMPLATE_OFFSET_SLOT = sizeof(ENTRY_TEMPLATE) - 8;
void entry_generate_default_code(char *entry, int slot)
{
char *writeEntry;
// Get the pointer to the writable mapping.
writeEntry = (char *) u_execmem_get_writable(entry);
char *writeEntry = (char *) entry;
memcpy(writeEntry, ENTRY_TEMPLATE, sizeof(ENTRY_TEMPLATE));

View file

@ -47,7 +47,7 @@
__asm__(".syntax unified\n\t");
/*
* u_execmem_alloc() allocates 64 bytes per stub.
* The size of each dispatch stub.
*/
#define ENTRY_STUB_ALIGN 128
#if !defined(GLDISPATCH_PAGE_SIZE)
@ -198,7 +198,7 @@ void entry_generate_default_code(char *entry, int slot)
assert((uintptr_t)entry & (uintptr_t)0x1);
// Get the pointer to the writable mapping.
writeEntry = (char *) u_execmem_get_writable(entry - 1);
writeEntry = (char *) (entry - 1);
memcpy(writeEntry, ENTRY_TEMPLATE, sizeof(ENTRY_TEMPLATE));
@ -227,22 +227,5 @@ void entry_get_patch_addresses(mapi_func entry, void **writePtr, const void **ex
// Get the actual beginning of the stub allocation
void *entryBase = (void *) (((uintptr_t) entry) - 1);
*execPtr = (const void *) entryBase;
*writePtr = u_execmem_get_writable(entryBase);
*writePtr = entryBase;
}
#if !defined(STATIC_DISPATCH_ONLY)
mapi_func entry_generate(int slot)
{
void *code = u_execmem_alloc(entry_stub_size);
if (!code) {
return NULL;
}
// Add 1 to the base address to force Thumb mode when jumping to the stub
code = (void *)((char *)code + 1);
entry_generate_default_code(code, slot);
return (mapi_func) code;
}
#endif // !defined(STATIC_DISPATCH_ONLY)

View file

@ -34,12 +34,6 @@
* Common code for the x86-64 TLS, x86-64 TSD, and ARMv7 entrypoint stubs.
*/
#if !defined(STATIC_DISPATCH_ONLY)
#include "u_execmem.h"
#else
#define u_execmem_get_writable(addr) ((void *) (addr))
#endif
#include "entry.h"
extern char public_entry_start[];

View file

@ -139,7 +139,7 @@ static const int TEMPLATE_OFFSET_SLOT = sizeof(ENTRY_TEMPLATE) - 8;
void entry_generate_default_code(char *entry, int slot)
{
char *writeEntry = u_execmem_get_writable(entry);
char *writeEntry = entry;
STATIC_ASSERT(ENTRY_STUB_ALIGN >= sizeof(ENTRY_TEMPLATE));

View file

@ -197,7 +197,7 @@ static const int TEMPLATE_OFFSET_SLOT = (sizeof(ENTRY_TEMPLATE) - 8);
*/
void entry_generate_default_code(char *entry, int slot)
{
char *writeEntry = u_execmem_get_writable(entry);
char *writeEntry = entry;
memcpy(writeEntry, ENTRY_TEMPLATE, sizeof(ENTRY_TEMPLATE));
*((uint32_t *) (writeEntry + TEMPLATE_OFFSET_SLOT)) = slot * sizeof(mapi_func);

View file

@ -30,6 +30,7 @@
#include <stdlib.h>
#include "glapi.h"
#include "utils_misc.h"
#include "glvnd/GLdispatchABI.h"
static INLINE const struct _glapi_table *
@ -66,8 +67,12 @@ entry_generate_default_code(char *entry, int slot)
mapi_func
entry_get_public(int index)
{
/* pubic_entries are defined by MAPI_TMP_PUBLIC_ENTRIES */
return public_entries[index];
/* pubic_entries are defined by MAPI_TMP_PUBLIC_ENTRIES */
if (index >= 0 && index < ARRAY_LEN(public_entries)) {
return public_entries[index];
} else {
return NULL;
}
}
int entry_patch_start(void)
@ -88,11 +93,3 @@ void entry_get_patch_addresses(mapi_func entry, void **writePtr, const void **ex
*writePtr = NULL;
*execPtr = NULL;
}
#if !defined(STATIC_DISPATCH_ONLY)
mapi_func
entry_generate(int slot)
{
return NULL;
}
#endif // !defined(STATIC_DISPATCH_ONLY)

View file

@ -63,19 +63,5 @@ mapi_func entry_get_public(int index)
void entry_get_patch_addresses(mapi_func entry, void **writePtr, const void **execPtr)
{
*execPtr = (const void *) entry;
*writePtr = u_execmem_get_writable(entry);
*writePtr = (void *) entry;
}
#if !defined(STATIC_DISPATCH_ONLY)
mapi_func entry_generate(int slot)
{
void *code = u_execmem_alloc(entry_stub_size);
if (!code) {
return NULL;
}
entry_generate_default_code(code, slot);
return (mapi_func) code;
}
#endif // !defined(STATIC_DISPATCH_ONLY)

View file

@ -118,7 +118,7 @@ static const unsigned int SLOT_OFFSET = 12;
void entry_generate_default_code(char *entry, int slot)
{
char *writeEntry = u_execmem_get_writable(entry);
char *writeEntry = entry;
uint64_t tls_addr;
STATIC_ASSERT(ENTRY_STUB_ALIGN >= sizeof(ENTRY_TEMPLATE));

View file

@ -64,6 +64,9 @@ __asm__(".balign " U_STRINGIFY(GLDISPATCH_PAGE_SIZE) "\n"
* We can't do that in general for the generated stubs since they're emitted
* into malloc()ed memory which may not be within 2GB of %rip, as explained in
* the comment in u_execmem.c.
*
* TODO: The dynamic stubs are no longer allocated, so we should be able to
* assume that they're within 2GB of %rip.
*/
#define STUB_ASM_CODE(slot) \
"movq _glapi_Current@GOTPCREL(%rip), %rax\n\t" \
@ -130,7 +133,7 @@ static const int TEMPLATE_OFFSET_SLOT = 45;
void entry_generate_default_code(char *entry, int slot)
{
char *writeEntry = u_execmem_get_writable(entry);
char *writeEntry = entry;
memcpy(writeEntry, ENTRY_TEMPLATE, sizeof(ENTRY_TEMPLATE));
*((uint32_t *) (writeEntry + TEMPLATE_OFFSET_SLOT)) = slot * sizeof(mapi_func);

View file

@ -95,7 +95,7 @@ static const int TEMPLATE_OFFSET_SLOT = 8;
void entry_generate_default_code(char *entry, int slot)
{
char *writeEntry = u_execmem_get_writable(entry);
char *writeEntry = entry;
memcpy(writeEntry, ENTRY_TEMPLATE, sizeof(ENTRY_TEMPLATE));
*((uint32_t *) (writeEntry + TEMPLATE_OFFSET_TLS_OFFSET)) = x86_current_tls();

View file

@ -105,7 +105,7 @@ static const int TEMPLATE_OFFSET_SLOT2 = 22;
void entry_generate_default_code(char *entry, int slot)
{
char *writeEntry = u_execmem_get_writable(entry);
char *writeEntry = entry;
uintptr_t getTableOffset;
memcpy(writeEntry, ENTRY_TEMPLATE, sizeof(ENTRY_TEMPLATE));

View file

@ -34,10 +34,6 @@
#include "stub.h"
#include "table.h"
#if !defined(STATIC_DISPATCH_ONLY)
#include "u_execmem.h"
#endif
#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
#define MAPI_LAST_SLOT (MAPI_TABLE_NUM_STATIC + MAPI_TABLE_NUM_DYNAMIC - 1)
@ -107,7 +103,6 @@ void stub_cleanup_dynamic(void)
}
num_dynamic_stubs = 0;
u_execmem_free();
}
/**
@ -137,7 +132,7 @@ stub_add_dynamic(const char *name)
/* Assign the next unused slot. */
stub->slot = MAPI_TABLE_NUM_STATIC + idx;
stub->addr = entry_generate(stub->slot);
stub->addr = entry_get_public(stub->slot);
if (!stub->addr) {
free(stub->nameBuffer);
stub->nameBuffer = NULL;

View file

@ -1,122 +0,0 @@
/*
* Mesa 3-D graphics library
*
* Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* \file glapi_execmem.c
*
* Function for allocating executable memory for dispatch stubs.
*
* Copied from main/execmem.c and simplified for dispatch stubs.
*/
#include <stdlib.h>
#include <stdint.h>
#include "u_compiler.h"
#include "u_execmem.h"
#include "utils_misc.h"
#include "glvnd_pthread.h"
#include "entry.h"
#include "table.h"
static glvnd_mutex_t exec_mutex = GLVND_MUTEX_INITIALIZER;
static unsigned int head = 0;
static unsigned char *exec_mem = NULL;
static unsigned char *write_mem = NULL;
/*
* Dispatch stubs are of fixed size and never freed. Thus, we do not need to
* overlay a heap, we just mmap a page and manage through an index.
*/
static int
init_map(void)
{
if (entry_stub_size == 0) {
return 0;
}
if (exec_mem == NULL) {
void *writePtr, *execPtr;
if (AllocExecPages(entry_stub_size * MAPI_TABLE_NUM_DYNAMIC, &writePtr, &execPtr) == 0) {
exec_mem = (unsigned char *) execPtr;
write_mem = (unsigned char *) writePtr;
head = 0;
}
}
return (exec_mem != NULL);
}
void u_execmem_free(void)
{
if (exec_mem != NULL) {
FreeExecPages(entry_stub_size * MAPI_TABLE_NUM_DYNAMIC, write_mem, exec_mem);
write_mem = NULL;
exec_mem = NULL;
}
}
void *
u_execmem_alloc(unsigned int size)
{
void *addr = NULL;
__glvndPthreadFuncs.mutex_lock(&exec_mutex);
if (!init_map())
goto bail;
/* free space check, assumes no integer overflow */
if (head + size > entry_stub_size * MAPI_TABLE_NUM_DYNAMIC)
goto bail;
/* allocation, assumes proper addr and size alignement */
addr = exec_mem + head;
head += size;
bail:
__glvndPthreadFuncs.mutex_unlock(&exec_mutex);
return addr;
}
void *u_execmem_get_writable(void *execPtr)
{
// If execPtr is within the executable mapping, then return the same offset
// in the writable mapping.
if (((uintptr_t) execPtr) >= ((uintptr_t) exec_mem))
{
uintptr_t offset = ((uintptr_t) execPtr) - ((uintptr_t) exec_mem);
if (offset < entry_stub_size * MAPI_TABLE_NUM_DYNAMIC)
{
return (void *) (((uintptr_t) write_mem) + offset);
}
}
return execPtr;
}

View file

@ -1,29 +0,0 @@
#ifndef _U_EXECMEM_H_
#define _U_EXECMEM_H_
/**
* Allocates \p size bytes of executable memory.
*
* The returned pointer may or may not be writable. Call
* \c u_execmem_get_writable to get a pointer to a writable mapping.
*/
void *u_execmem_alloc(unsigned int size);
/**
* Returns a writable mapping for a pointer returned by \c u_execmem_alloc.
*
* If \p execPtr is a pointer returned by \c u_execmem_alloc, then this
* function will return a writable mapping of the same memory.
*
* If \p execPtr was not returned by \c u_execmem_alloc, then it will be
* returned unmodified. Thus, it's safe to pass a pointer to a static
* or dynamic entrypoint.
*/
void *u_execmem_get_writable(void *execPtr);
/**
* Frees the memory allocated from u_execmem_alloc.
*/
void u_execmem_free(void);
#endif /* _U_EXECMEM_H_ */

View file

@ -53,7 +53,7 @@ def _main():
assert(all(functions[i].slot == i for i in range(len(functions))))
print(r"""
/* This file is automatically generated by mapi_abi.py. Do not modify. */
/* This file is automatically generated by gen_gldispatch_mapi.py. Do not modify. */
#ifndef _GLAPI_TMP_H_
#define _GLAPI_TMP_H_
@ -67,7 +67,7 @@ typedef void (APIENTRY *GLDEBUGPROCKHR)(GLenum source,GLenum type,GLuint id,GLe
print(generate_noop_array(functions))
print(generate_public_stubs(functions))
print(generate_public_entries(functions))
print(generate_stub_asm_gcc(functions))
print(generate_stub_asm_gcc(functions, (target == "gldispatch")))
def generate_defines(functions):
text = r"""
@ -162,7 +162,7 @@ GLAPI {f.rt} APIENTRY {f.name}({f.decArgs})
text += "#endif /* MAPI_TMP_PUBLIC_ENTRIES */\n"
return text
def generate_stub_asm_gcc(functions):
def generate_stub_asm_gcc(functions, includeDynamic):
text = "#ifdef MAPI_TMP_STUB_ASM_GCC\n"
text += "__asm__(\n"
@ -170,6 +170,10 @@ def generate_stub_asm_gcc(functions):
text += 'STUB_ASM_ENTRY("%s")"\\n"\n' % (func.name,)
text += '"\\t"STUB_ASM_CODE("%d")"\\n"\n\n' % (func.slot,)
if (includeDynamic):
for i in range(genCommon.MAPI_TABLE_NUM_DYNAMIC):
text += 'STUB_ASM_ENTRY("dynamic_%04d")"\\n"\n' % (i,)
text += '"\\t"STUB_ASM_CODE("%d")"\\n"\n\n' % (len(functions) + i)
text += ");\n"
text += "#undef MAPI_TMP_STUB_ASM_GCC\n"
text += "#endif /* MAPI_TMP_STUB_ASM_GCC */\n"