Path: blob/21.2-virgl/src/mapi/entry_x86-64_tls.h
4558 views
/*1* Mesa 3-D graphics library2*3* Copyright (C) 2010 LunarG Inc.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the "Software"),7* to deal in the Software without restriction, including without limitation8* the rights to use, copy, modify, merge, publish, distribute, sublicense,9* and/or sell copies of the Software, and to permit persons to whom the10* Software is furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice shall be included13* in all copies or substantial portions of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER21* DEALINGS IN THE SOFTWARE.22*23* Authors:24* Chia-I Wu <[email protected]>25*/2627#ifdef __CET__28#define ENDBR "endbr64\n\t"29#else30#define ENDBR31#endif3233#ifdef HAVE_FUNC_ATTRIBUTE_VISIBILITY34#define HIDDEN __attribute__((visibility("hidden")))35#else36#define HIDDEN37#endif3839__asm__(".text\n"40".balign 32\n"41"x86_64_entry_start:");4243#define STUB_ASM_ENTRY(func) \44".globl " func "\n" \45".type " func ", @function\n" \46".balign 32\n" \47func ":"4849#ifndef __ILP32__5051#define STUB_ASM_CODE(slot) \52ENDBR \53"movq " ENTRY_CURRENT_TABLE "@GOTTPOFF(%rip), %rax\n\t" \54"movq %fs:(%rax), %r11\n\t" \55"jmp *(8 * " slot ")(%r11)"5657#else5859#define STUB_ASM_CODE(slot) \60ENDBR \61"movq " ENTRY_CURRENT_TABLE "@GOTTPOFF(%rip), %rax\n\t" \62"movl %fs:(%rax), %r11d\n\t" \63"movl 4*" slot "(%r11d), %r11d\n\t" \64"jmp *%r11"6566#endif6768#define MAPI_TMP_STUB_ASM_GCC69#include "mapi_tmp.h"7071#ifndef MAPI_MODE_BRIDGE7273#include <string.h>74#include "u_execmem.h"7576void77entry_patch_public(void)78{79}8081extern char82x86_64_entry_start[] HIDDEN;8384mapi_func85entry_get_public(int slot)86{87return (mapi_func) (x86_64_entry_start + slot * 32);88}8990void91entry_patch(mapi_func entry, int slot)92{93char *code = (char *) entry;94int offset = 12;95#ifdef __ILP32__96offset = 13;97#endif98*((unsigned int *) (code + offset)) = slot * sizeof(mapi_func);99}100101mapi_func102entry_generate(int slot)103{104const char code_templ[] = {105#ifndef __ILP32__106/* movq %fs:0, %r11 */1070x64, 0x4c, 0x8b, 0x1c, 0x25, 0x00, 0x00, 0x00, 0x00,108/* jmp *0x1234(%r11) */1090x41, 0xff, 0xa3, 0x34, 0x12, 0x00, 0x00,110#else111/* movl %fs:0, %r11d */1120x64, 0x44, 0x8b, 0x1c, 0x25, 0x00, 0x00, 0x00, 0x00,113/* movl 0x1234(%r11d), %r11d */1140x67, 0x45, 0x8b, 0x9b, 0x34, 0x12, 0x00, 0x00,115/* jmp *%r11 */1160x41, 0xff, 0xe3,117#endif118};119unsigned long long addr;120char *code;121mapi_func entry;122123__asm__("movq " ENTRY_CURRENT_TABLE "@GOTTPOFF(%%rip), %0"124: "=r" (addr));125if ((addr >> 32) != 0xffffffff)126return NULL;127addr &= 0xffffffff;128129code = u_execmem_alloc(sizeof(code_templ));130if (!code)131return NULL;132133memcpy(code, code_templ, sizeof(code_templ));134135*((unsigned int *) (code + 5)) = addr;136entry = (mapi_func) code;137entry_patch(entry, slot);138139return entry;140}141142#endif /* MAPI_MODE_BRIDGE */143144145