101e04c3fSmrg/* 201e04c3fSmrg * Mesa 3-D graphics library 301e04c3fSmrg * 401e04c3fSmrg * Copyright (C) 2017 Red Hat 501e04c3fSmrg * 601e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a 701e04c3fSmrg * copy of this software and associated documentation files (the "Software"), 801e04c3fSmrg * to deal in the Software without restriction, including without limitation 901e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 1001e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the 1101e04c3fSmrg * Software is furnished to do so, subject to the following conditions: 1201e04c3fSmrg * 1301e04c3fSmrg * The above copyright notice and this permission notice shall be included 1401e04c3fSmrg * in all copies or substantial portions of the Software. 1501e04c3fSmrg * 1601e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1701e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1801e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1901e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 2001e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 2101e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 2201e04c3fSmrg * DEALINGS IN THE SOFTWARE. 2301e04c3fSmrg * 2401e04c3fSmrg * Authors: 2501e04c3fSmrg * Ben Crocker <bcrocker@redhat.com> 2601e04c3fSmrg */ 2701e04c3fSmrg 2801e04c3fSmrg#ifdef HAVE_FUNC_ATTRIBUTE_VISIBILITY 2901e04c3fSmrg#define HIDDEN __attribute__((visibility("hidden"))) 3001e04c3fSmrg#else 3101e04c3fSmrg#define HIDDEN 3201e04c3fSmrg#endif 3301e04c3fSmrg 3401e04c3fSmrg// NOTE: These must be powers of two: 3501e04c3fSmrg#define PPC64LE_ENTRY_SIZE 64 3601e04c3fSmrg#define PPC64LE_PAGE_ALIGN 65536 3701e04c3fSmrg#if ((PPC64LE_ENTRY_SIZE & (PPC64LE_ENTRY_SIZE - 1)) != 0) 3801e04c3fSmrg#error PPC64LE_ENTRY_SIZE must be a power of two! 3901e04c3fSmrg#endif 4001e04c3fSmrg#if ((PPC64LE_PAGE_ALIGN & (PPC64LE_PAGE_ALIGN - 1)) != 0) 4101e04c3fSmrg#error PPC64LE_PAGE_ALIGN must be a power of two! 4201e04c3fSmrg#endif 4301e04c3fSmrg 4401e04c3fSmrg__asm__(".text\n" 4501e04c3fSmrg ".balign " U_STRINGIFY(PPC64LE_ENTRY_SIZE) "\n" 4601e04c3fSmrg "ppc64le_entry_start:"); 4701e04c3fSmrg 4801e04c3fSmrg#define STUB_ASM_ENTRY(func) \ 4901e04c3fSmrg ".globl " func "\n" \ 5001e04c3fSmrg ".type " func ", @function\n" \ 5101e04c3fSmrg ".balign " U_STRINGIFY(PPC64LE_ENTRY_SIZE) "\n" \ 5201e04c3fSmrg func ":\n\t" \ 5301e04c3fSmrg " addis 2, 12, .TOC.-" func "@ha\n\t" \ 5401e04c3fSmrg " addi 2, 2, .TOC.-" func "@l\n\t" \ 5501e04c3fSmrg " .localentry " func ", .-" func "\n\t" 5601e04c3fSmrg 5701e04c3fSmrg#define STUB_ASM_CODE(slot) \ 5801e04c3fSmrg " addis 11, 2, " ENTRY_CURRENT_TABLE "@got@tprel@ha\n\t" \ 5901e04c3fSmrg " ld 11, " ENTRY_CURRENT_TABLE "@got@tprel@l(11)\n\t" \ 6001e04c3fSmrg " add 11, 11," ENTRY_CURRENT_TABLE "@tls\n\t" \ 6101e04c3fSmrg " ld 11, 0(11)\n\t" \ 6201e04c3fSmrg " ld 12, " slot "*8(11)\n\t" \ 6301e04c3fSmrg " mtctr 12\n\t" \ 6401e04c3fSmrg " bctr\n" \ 6501e04c3fSmrg 6601e04c3fSmrg#define MAPI_TMP_STUB_ASM_GCC 6701e04c3fSmrg#include "mapi_tmp.h" 6801e04c3fSmrg 6901e04c3fSmrg#ifndef MAPI_MODE_BRIDGE 7001e04c3fSmrg 7101e04c3fSmrg#include <string.h> 7201e04c3fSmrg#include "u_execmem.h" 7301e04c3fSmrg 7401e04c3fSmrgvoid 7501e04c3fSmrgentry_patch_public(void) 7601e04c3fSmrg{ 7701e04c3fSmrg} 7801e04c3fSmrg 7901e04c3fSmrgextern char 8001e04c3fSmrgppc64le_entry_start[] HIDDEN; 8101e04c3fSmrg 8201e04c3fSmrgmapi_func 8301e04c3fSmrgentry_get_public(int slot) 8401e04c3fSmrg{ 8501e04c3fSmrg return (mapi_func) (ppc64le_entry_start + slot * PPC64LE_ENTRY_SIZE); 8601e04c3fSmrg} 8701e04c3fSmrg 8801e04c3fSmrg__asm__(".text\n"); 8901e04c3fSmrg 9001e04c3fSmrg__asm__("ppc64le_dispatch_tls:\n\t" 9101e04c3fSmrg " addis 3, 2, " ENTRY_CURRENT_TABLE "@got@tprel@ha\n\t" 9201e04c3fSmrg " ld 3, " ENTRY_CURRENT_TABLE "@got@tprel@l(3)\n\t" 9301e04c3fSmrg " blr\n" 9401e04c3fSmrg ); 9501e04c3fSmrg 9601e04c3fSmrgextern uint64_t ppc64le_dispatch_tls(); 9701e04c3fSmrg 9801e04c3fSmrgstatic const uint32_t code_templ[] = { 9901e04c3fSmrg // This should be functionally the same code as would be generated from 10001e04c3fSmrg // the STUB_ASM_CODE macro, but defined as a buffer. 10101e04c3fSmrg // This is used to generate new dispatch stubs. Mesa will copy this 10201e04c3fSmrg // data to the dispatch stub, and then it will patch the slot number and 10301e04c3fSmrg // any addresses that it needs to. 10401e04c3fSmrg // NOTE!!! NOTE!!! NOTE!!! 10501e04c3fSmrg // This representation is correct for both little- and big-endian systems. 10601e04c3fSmrg // However, more work needs to be done for big-endian Linux because it 10701e04c3fSmrg // adheres to an older, AIX-compatible ABI that uses function descriptors. 10801e04c3fSmrg // 1000: 10901e04c3fSmrg 0x7C0802A6, // <ENTRY+00>: mflr 0 11001e04c3fSmrg 0xF8010010, // <ENTRY+04>: std 0, 16(1) 11101e04c3fSmrg 0xE96C0028, // <ENTRY+08>: ld 11, 9000f-1000b+0(12) 11201e04c3fSmrg 0x7D6B6A14, // <ENTRY+12>: add 11, 11, 13 11301e04c3fSmrg 0xE96B0000, // <ENTRY+16>: ld 11, 0(11) 11401e04c3fSmrg 0xE80C0030, // <ENTRY+20>: ld 0, 9000f-1000b+8(12) 11501e04c3fSmrg 0x7D8B002A, // <ENTRY+24>: ldx 12, 11, 0 11601e04c3fSmrg 0x7D8903A6, // <ENTRY+28>: mtctr 12 11701e04c3fSmrg 0x4E800420, // <ENTRY+32>: bctr 11801e04c3fSmrg 0x60000000, // <ENTRY+36>: nop 11901e04c3fSmrg // 9000: 12001e04c3fSmrg 0, 0, // <ENTRY+40>: .quad _glapi_tls_Dispatch 12101e04c3fSmrg 0, 0 // <ENTRY+48>: .quad <slot>*8 12201e04c3fSmrg}; 12301e04c3fSmrgstatic const uint64_t TEMPLATE_OFFSET_TLS_ADDR = sizeof(code_templ) - 2*8; 12401e04c3fSmrgstatic const uint64_t TEMPLATE_OFFSET_SLOT = sizeof(code_templ) - 1*8; 12501e04c3fSmrg 12601e04c3fSmrgvoid 12701e04c3fSmrgentry_patch(mapi_func entry, int slot) 12801e04c3fSmrg{ 12901e04c3fSmrg char *code = (char *) entry; 13001e04c3fSmrg *((uint64_t *) (code + TEMPLATE_OFFSET_TLS_ADDR)) = ppc64le_dispatch_tls(); 13101e04c3fSmrg *((uint64_t *) (code + TEMPLATE_OFFSET_SLOT)) = slot * sizeof(mapi_func); 13201e04c3fSmrg} 13301e04c3fSmrg 13401e04c3fSmrgmapi_func 13501e04c3fSmrgentry_generate(int slot) 13601e04c3fSmrg{ 13701e04c3fSmrg char *code; 13801e04c3fSmrg mapi_func entry; 13901e04c3fSmrg 14001e04c3fSmrg code = u_execmem_alloc(sizeof(code_templ)); 14101e04c3fSmrg if (!code) 14201e04c3fSmrg return NULL; 14301e04c3fSmrg 14401e04c3fSmrg memcpy(code, code_templ, sizeof(code_templ)); 14501e04c3fSmrg 14601e04c3fSmrg entry = (mapi_func) code; 14701e04c3fSmrg entry_patch(entry, slot); 14801e04c3fSmrg 14901e04c3fSmrg return entry; 15001e04c3fSmrg} 15101e04c3fSmrg 15201e04c3fSmrg#endif /* MAPI_MODE_BRIDGE */ 153