Path: blob/21.2-virgl/src/mapi/entry_ppc64le_tls.h
4558 views
/*1* Mesa 3-D graphics library2*3* Copyright (C) 2017 Red Hat4*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* Ben Crocker <[email protected]>25*/2627#ifdef HAVE_FUNC_ATTRIBUTE_VISIBILITY28#define HIDDEN __attribute__((visibility("hidden")))29#else30#define HIDDEN31#endif3233// NOTE: These must be powers of two:34#define PPC64LE_ENTRY_SIZE 6435#define PPC64LE_PAGE_ALIGN 6553636#if ((PPC64LE_ENTRY_SIZE & (PPC64LE_ENTRY_SIZE - 1)) != 0)37#error PPC64LE_ENTRY_SIZE must be a power of two!38#endif39#if ((PPC64LE_PAGE_ALIGN & (PPC64LE_PAGE_ALIGN - 1)) != 0)40#error PPC64LE_PAGE_ALIGN must be a power of two!41#endif4243__asm__(".text\n"44".balign " U_STRINGIFY(PPC64LE_ENTRY_SIZE) "\n"45"ppc64le_entry_start:");4647#define STUB_ASM_ENTRY(func) \48".globl " func "\n" \49".type " func ", @function\n" \50".balign " U_STRINGIFY(PPC64LE_ENTRY_SIZE) "\n" \51func ":\n\t" \52" addis 2, 12, .TOC.-" func "@ha\n\t" \53" addi 2, 2, .TOC.-" func "@l\n\t" \54" .localentry " func ", .-" func "\n\t"5556#define STUB_ASM_CODE(slot) \57" addis 11, 2, " ENTRY_CURRENT_TABLE "@got@tprel@ha\n\t" \58" ld 11, " ENTRY_CURRENT_TABLE "@got@tprel@l(11)\n\t" \59" add 11, 11," ENTRY_CURRENT_TABLE "@tls\n\t" \60" ld 11, 0(11)\n\t" \61" ld 12, " slot "*8(11)\n\t" \62" mtctr 12\n\t" \63" bctr\n" \6465#define MAPI_TMP_STUB_ASM_GCC66#include "mapi_tmp.h"6768#ifndef MAPI_MODE_BRIDGE6970#include <string.h>71#include "u_execmem.h"7273void74entry_patch_public(void)75{76}7778extern char79ppc64le_entry_start[] HIDDEN;8081mapi_func82entry_get_public(int slot)83{84return (mapi_func) (ppc64le_entry_start + slot * PPC64LE_ENTRY_SIZE);85}8687__asm__(".text\n");8889__asm__("ppc64le_dispatch_tls:\n\t"90" addis 3, 2, " ENTRY_CURRENT_TABLE "@got@tprel@ha\n\t"91" ld 3, " ENTRY_CURRENT_TABLE "@got@tprel@l(3)\n\t"92" blr\n"93);9495extern uint64_t ppc64le_dispatch_tls();9697static const uint32_t code_templ[] = {98// This should be functionally the same code as would be generated from99// the STUB_ASM_CODE macro, but defined as a buffer.100// This is used to generate new dispatch stubs. Mesa will copy this101// data to the dispatch stub, and then it will patch the slot number and102// any addresses that it needs to.103// NOTE!!! NOTE!!! NOTE!!!104// This representation is correct for both little- and big-endian systems.105// However, more work needs to be done for big-endian Linux because it106// adheres to an older, AIX-compatible ABI that uses function descriptors.107// 1000:1080x7C0802A6, // <ENTRY+00>: mflr 01090xF8010010, // <ENTRY+04>: std 0, 16(1)1100xE96C0028, // <ENTRY+08>: ld 11, 9000f-1000b+0(12)1110x7D6B6A14, // <ENTRY+12>: add 11, 11, 131120xE96B0000, // <ENTRY+16>: ld 11, 0(11)1130xE80C0030, // <ENTRY+20>: ld 0, 9000f-1000b+8(12)1140x7D8B002A, // <ENTRY+24>: ldx 12, 11, 01150x7D8903A6, // <ENTRY+28>: mtctr 121160x4E800420, // <ENTRY+32>: bctr1170x60000000, // <ENTRY+36>: nop118// 9000:1190, 0, // <ENTRY+40>: .quad _glapi_tls_Dispatch1200, 0 // <ENTRY+48>: .quad <slot>*8121};122static const uint64_t TEMPLATE_OFFSET_TLS_ADDR = sizeof(code_templ) - 2*8;123static const uint64_t TEMPLATE_OFFSET_SLOT = sizeof(code_templ) - 1*8;124125void126entry_patch(mapi_func entry, int slot)127{128char *code = (char *) entry;129*((uint64_t *) (code + TEMPLATE_OFFSET_TLS_ADDR)) = ppc64le_dispatch_tls();130*((uint64_t *) (code + TEMPLATE_OFFSET_SLOT)) = slot * sizeof(mapi_func);131}132133mapi_func134entry_generate(int slot)135{136char *code;137mapi_func entry;138139code = u_execmem_alloc(sizeof(code_templ));140if (!code)141return NULL;142143memcpy(code, code_templ, sizeof(code_templ));144145entry = (mapi_func) code;146entry_patch(entry, slot);147148return entry;149}150151#endif /* MAPI_MODE_BRIDGE */152153154