Blame src/GLdispatch/vnd-glapi/entry_x86_tsd.c

Packit Service 9e77c8
/*
Packit Service 9e77c8
 * Mesa 3-D graphics library
Packit Service 9e77c8
 *
Packit Service 9e77c8
 * Copyright (C) 2010 LunarG Inc.
Packit Service 9e77c8
 *
Packit Service 9e77c8
 * Permission is hereby granted, free of charge, to any person obtaining a
Packit Service 9e77c8
 * copy of this software and associated documentation files (the "Software"),
Packit Service 9e77c8
 * to deal in the Software without restriction, including without limitation
Packit Service 9e77c8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
Packit Service 9e77c8
 * and/or sell copies of the Software, and to permit persons to whom the
Packit Service 9e77c8
 * Software is furnished to do so, subject to the following conditions:
Packit Service 9e77c8
 *
Packit Service 9e77c8
 * The above copyright notice and this permission notice shall be included
Packit Service 9e77c8
 * in all copies or substantial portions of the Software.
Packit Service 9e77c8
 *
Packit Service 9e77c8
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit Service 9e77c8
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit Service 9e77c8
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
Packit Service 9e77c8
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit Service 9e77c8
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit Service 9e77c8
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
Packit Service 9e77c8
 * DEALINGS IN THE SOFTWARE.
Packit Service 9e77c8
 *
Packit Service 9e77c8
 * Authors:
Packit Service 9e77c8
 *    Chia-I Wu <olv@lunarg.com>
Packit Service 9e77c8
 */
Packit Service 9e77c8
Packit Service 9e77c8
#include "entry.h"
Packit Service 9e77c8
#include "entry_common.h"
Packit Service 9e77c8
Packit Service 9e77c8
#include <assert.h>
Packit Service 9e77c8
#include <stdint.h>
Packit Service 9e77c8
#include <sys/mman.h>
Packit Service 9e77c8
#include <unistd.h>
Packit Service 9e77c8
#include <string.h>
Packit Service 9e77c8
Packit Service 9e77c8
#include "u_macros.h"
Packit Service 9e77c8
#include "glapi.h"
Packit Service 9e77c8
#include "glvnd/GLdispatchABI.h"
Packit Service 9e77c8
Packit Service 9e77c8
#define ENTRY_STUB_ALIGN 64
Packit Service 9e77c8
#if !defined(GLDISPATCH_PAGE_SIZE)
Packit Service 9e77c8
#define GLDISPATCH_PAGE_SIZE 4096
Packit Service 9e77c8
#endif
Packit Service 9e77c8
Packit Service 9e77c8
__asm__(".section wtext,\"ax\",@progbits\n");
Packit Service 9e77c8
__asm__(".balign " U_STRINGIFY(GLDISPATCH_PAGE_SIZE) "\n"
Packit Service 9e77c8
       ".globl public_entry_start\n"
Packit Service 9e77c8
       ".hidden public_entry_start\n"
Packit Service 9e77c8
        "public_entry_start:");
Packit Service 9e77c8
Packit Service 9e77c8
#define STUB_ASM_ENTRY(func)        \
Packit Service 9e77c8
    ".globl " func "\n"              \
Packit Service 9e77c8
    ".type " func ", @function\n"    \
Packit Service 9e77c8
    ".balign " U_STRINGIFY(ENTRY_STUB_ALIGN) "\n" \
Packit Service 9e77c8
    func ":\n"
Packit Service 9e77c8
Packit Service 9e77c8
#define STUB_ASM_CODE(slot)         \
Packit Service 9e77c8
    "push %ebx\n" \
Packit Service 9e77c8
    "call 1f\n" \
Packit Service 9e77c8
    "1:\n" \
Packit Service 9e77c8
    "popl %ebx\n" \
Packit Service 9e77c8
    "addl $_GLOBAL_OFFSET_TABLE_+[.-1b], %ebx\n" \
Packit Service 9e77c8
    "movl _glapi_Current@GOT(%ebx), %eax\n" \
Packit Service 9e77c8
    "mov (%eax), %eax\n" \
Packit Service 9e77c8
    "testl %eax, %eax\n" \
Packit Service 9e77c8
    "jne 1f\n" \
Packit Service 9e77c8
    "call _glapi_get_current@PLT\n" \
Packit Service 9e77c8
    "1:\n" \
Packit Service 9e77c8
    "pop %ebx\n" \
Packit Service 9e77c8
    "jmp *(4 * " slot ")(%eax)\n"
Packit Service 9e77c8
Packit Service 9e77c8
#define MAPI_TMP_STUB_ASM_GCC
Packit Service 9e77c8
#include "mapi_tmp.h"
Packit Service 9e77c8
Packit Service 9e77c8
Packit Service 9e77c8
__asm__(".balign " U_STRINGIFY(GLDISPATCH_PAGE_SIZE) "\n"
Packit Service 9e77c8
       ".globl public_entry_end\n"
Packit Service 9e77c8
       ".hidden public_entry_end\n"
Packit Service 9e77c8
        "public_entry_end:");
Packit Service 9e77c8
__asm__(".text\n");
Packit Service 9e77c8
Packit Service 9e77c8
const int entry_type = __GLDISPATCH_STUB_X86;
Packit Service 9e77c8
const int entry_stub_size = ENTRY_STUB_ALIGN;
Packit Service 9e77c8
Packit Service 9e77c8
// Note that the generated stubs are simpler than the assembly stubs above.
Packit Service 9e77c8
// For the generated stubs, we can patch in the addresses of _glapi_Current and
Packit Service 9e77c8
// _glapi_get_current, so we don't need to go through the GOT and PLT lookups.
Packit Service 9e77c8
static const unsigned char ENTRY_TEMPLATE[] =
Packit Service 9e77c8
{
Packit Service 9e77c8
    0xa1, 0x40, 0x30, 0x20, 0x10,       // <ENTRY>:    mov    _glapi_Current, %eax
Packit Service 9e77c8
    0x85, 0xc0,                         // <ENTRY+5>:  test   %eax, %eax
Packit Service 9e77c8
    0x74, 0x06,                         // <ENTRY+7>:  je     <ENTRY+15>
Packit Service 9e77c8
    0xff, 0xa0, 0x40, 0x30, 0x20, 0x10, // <ENTRY+9>:  jmp    *slot(%eax)
Packit Service 9e77c8
    0xe8, 0x40, 0x30, 0x20, 0x10,       // <ENTRY+15>: call   _glapi_get_current
Packit Service 9e77c8
    0xff, 0xa0, 0x40, 0x30, 0x20, 0x10, // <ENTRY+20>: jmp    *slot(%eax)
Packit Service 9e77c8
};
Packit Service 9e77c8
Packit Service 9e77c8
// These are the offsets in ENTRY_TEMPLATE of the values that we have to patch.
Packit Service 9e77c8
static const int TEMPLATE_OFFSET_CURRENT_TABLE = 1;
Packit Service 9e77c8
static const int TEMPLATE_OFFSET_CURRENT_TABLE_GET = 16;
Packit Service 9e77c8
static const int TEMPLATE_OFFSET_CURRENT_TABLE_GET_RELATIVE = 20;
Packit Service 9e77c8
static const int TEMPLATE_OFFSET_SLOT1 = 11;
Packit Service 9e77c8
static const int TEMPLATE_OFFSET_SLOT2 = 22;
Packit Service 9e77c8
Packit Service 9e77c8
void entry_generate_default_code(char *entry, int slot)
Packit Service 9e77c8
{
Packit Service 9e77c8
    char *writeEntry = u_execmem_get_writable(entry);
Packit Service 9e77c8
    uintptr_t getTableOffset;
Packit Service 9e77c8
Packit Service 9e77c8
    memcpy(writeEntry, ENTRY_TEMPLATE, sizeof(ENTRY_TEMPLATE));
Packit Service 9e77c8
Packit Service 9e77c8
    *((uint32_t *) (writeEntry + TEMPLATE_OFFSET_SLOT1)) = slot * sizeof(mapi_func);
Packit Service 9e77c8
    *((uint32_t *) (writeEntry + TEMPLATE_OFFSET_SLOT2)) = slot * sizeof(mapi_func);
Packit Service 9e77c8
    *((uintptr_t *) (writeEntry + TEMPLATE_OFFSET_CURRENT_TABLE)) = (uintptr_t) _glapi_Current;
Packit Service 9e77c8
Packit Service 9e77c8
    // Calculate the offset to patch for the CALL instruction to
Packit Service 9e77c8
    // _glapi_get_current.
Packit Service 9e77c8
    getTableOffset = (uintptr_t) _glapi_get_current;
Packit Service 9e77c8
    getTableOffset -= (((uintptr_t) entry) + TEMPLATE_OFFSET_CURRENT_TABLE_GET_RELATIVE);
Packit Service 9e77c8
    *((uintptr_t *) (writeEntry + TEMPLATE_OFFSET_CURRENT_TABLE_GET)) = getTableOffset;
Packit Service 9e77c8
}
Packit Service 9e77c8