1b8e80941Smrg/*
2b8e80941Smrg * Mesa 3-D graphics library
3b8e80941Smrg *
4b8e80941Smrg * Copyright (C) 2017 Red Hat
5b8e80941Smrg *
6b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
8b8e80941Smrg * to deal in the Software without restriction, including without limitation
9b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
11b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
12b8e80941Smrg *
13b8e80941Smrg * The above copyright notice and this permission notice shall be included
14b8e80941Smrg * in all copies or substantial portions of the Software.
15b8e80941Smrg *
16b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22b8e80941Smrg * DEALINGS IN THE SOFTWARE.
23b8e80941Smrg *
24b8e80941Smrg * Authors:
25b8e80941Smrg *    Ben Crocker <bcrocker@redhat.com>
26b8e80941Smrg */
27b8e80941Smrg
28b8e80941Smrg#ifdef HAVE_FUNC_ATTRIBUTE_VISIBILITY
29b8e80941Smrg#define HIDDEN __attribute__((visibility("hidden")))
30b8e80941Smrg#else
31b8e80941Smrg#define HIDDEN
32b8e80941Smrg#endif
33b8e80941Smrg
34b8e80941Smrg// NOTE: These must be powers of two:
35b8e80941Smrg#define PPC64LE_ENTRY_SIZE 64
36b8e80941Smrg#define PPC64LE_PAGE_ALIGN 65536
37b8e80941Smrg#if ((PPC64LE_ENTRY_SIZE & (PPC64LE_ENTRY_SIZE - 1)) != 0)
38b8e80941Smrg#error PPC64LE_ENTRY_SIZE must be a power of two!
39b8e80941Smrg#endif
40b8e80941Smrg#if ((PPC64LE_PAGE_ALIGN & (PPC64LE_PAGE_ALIGN - 1)) != 0)
41b8e80941Smrg#error PPC64LE_PAGE_ALIGN must be a power of two!
42b8e80941Smrg#endif
43b8e80941Smrg
44b8e80941Smrg__asm__(".text\n"
45b8e80941Smrg        ".balign " U_STRINGIFY(PPC64LE_ENTRY_SIZE) "\n"
46b8e80941Smrg        "ppc64le_entry_start:");
47b8e80941Smrg
48b8e80941Smrg#define STUB_ASM_ENTRY(func)                            \
49b8e80941Smrg   ".globl " func "\n"                                  \
50b8e80941Smrg   ".type " func ", @function\n"                        \
51b8e80941Smrg   ".balign " U_STRINGIFY(PPC64LE_ENTRY_SIZE) "\n"        \
52b8e80941Smrg   func ":\n\t"                                         \
53b8e80941Smrg   "  addis  2, 12, .TOC.-" func "@ha\n\t"              \
54b8e80941Smrg   "  addi   2, 2, .TOC.-" func "@l\n\t"                \
55b8e80941Smrg   "  .localentry  " func ", .-" func "\n\t"
56b8e80941Smrg
57b8e80941Smrg#define STUB_ASM_CODE(slot)                                     \
58b8e80941Smrg   "  addis  11, 2, " ENTRY_CURRENT_TABLE "@got@tprel@ha\n\t"   \
59b8e80941Smrg   "  ld     11, " ENTRY_CURRENT_TABLE "@got@tprel@l(11)\n\t"   \
60b8e80941Smrg   "  add    11, 11," ENTRY_CURRENT_TABLE "@tls\n\t"            \
61b8e80941Smrg   "  ld     11, 0(11)\n\t"                                     \
62b8e80941Smrg   "  ld     12, " slot "*8(11)\n\t"                            \
63b8e80941Smrg   "  mtctr  12\n\t"                                            \
64b8e80941Smrg   "  bctr\n"                                                   \
65b8e80941Smrg
66b8e80941Smrg#define MAPI_TMP_STUB_ASM_GCC
67b8e80941Smrg#include "mapi_tmp.h"
68b8e80941Smrg
69b8e80941Smrg#ifndef MAPI_MODE_BRIDGE
70b8e80941Smrg
71b8e80941Smrg#include <string.h>
72b8e80941Smrg#include "u_execmem.h"
73b8e80941Smrg
74b8e80941Smrgvoid
75b8e80941Smrgentry_patch_public(void)
76b8e80941Smrg{
77b8e80941Smrg}
78b8e80941Smrg
79b8e80941Smrgextern char
80b8e80941Smrgppc64le_entry_start[] HIDDEN;
81b8e80941Smrg
82b8e80941Smrgmapi_func
83b8e80941Smrgentry_get_public(int slot)
84b8e80941Smrg{
85b8e80941Smrg   return (mapi_func) (ppc64le_entry_start + slot * PPC64LE_ENTRY_SIZE);
86b8e80941Smrg}
87b8e80941Smrg
88b8e80941Smrg__asm__(".text\n");
89b8e80941Smrg
90b8e80941Smrg__asm__("ppc64le_dispatch_tls:\n\t"
91b8e80941Smrg        "  addis  3, 2, " ENTRY_CURRENT_TABLE "@got@tprel@ha\n\t"
92b8e80941Smrg        "  ld     3, " ENTRY_CURRENT_TABLE "@got@tprel@l(3)\n\t"
93b8e80941Smrg        "  blr\n"
94b8e80941Smrg        );
95b8e80941Smrg
96b8e80941Smrgextern uint64_t ppc64le_dispatch_tls();
97b8e80941Smrg
98b8e80941Smrgstatic const uint32_t code_templ[] = {
99b8e80941Smrg   // This should be functionally the same code as would be generated from
100b8e80941Smrg   // the STUB_ASM_CODE macro, but defined as a buffer.
101b8e80941Smrg   // This is used to generate new dispatch stubs. Mesa will copy this
102b8e80941Smrg   // data to the dispatch stub, and then it will patch the slot number and
103b8e80941Smrg   // any addresses that it needs to.
104b8e80941Smrg   // NOTE!!!  NOTE!!!  NOTE!!!
105b8e80941Smrg   // This representation is correct for both little- and big-endian systems.
106b8e80941Smrg   // However, more work needs to be done for big-endian Linux because it
107b8e80941Smrg   // adheres to an older, AIX-compatible ABI that uses function descriptors.
108b8e80941Smrg   // 1000:
109b8e80941Smrg   0x7C0802A6,    // <ENTRY+00>:   mflr   0
110b8e80941Smrg   0xF8010010,    // <ENTRY+04>:   std    0, 16(1)
111b8e80941Smrg   0xE96C0028,    // <ENTRY+08>:   ld     11, 9000f-1000b+0(12)
112b8e80941Smrg   0x7D6B6A14,    // <ENTRY+12>:   add    11, 11, 13
113b8e80941Smrg   0xE96B0000,    // <ENTRY+16>:   ld     11, 0(11)
114b8e80941Smrg   0xE80C0030,    // <ENTRY+20>:   ld     0, 9000f-1000b+8(12)
115b8e80941Smrg   0x7D8B002A,    // <ENTRY+24>:   ldx    12, 11, 0
116b8e80941Smrg   0x7D8903A6,    // <ENTRY+28>:   mtctr  12
117b8e80941Smrg   0x4E800420,    // <ENTRY+32>:   bctr
118b8e80941Smrg   0x60000000,    // <ENTRY+36>:   nop
119b8e80941Smrg   // 9000:
120b8e80941Smrg   0, 0,          // <ENTRY+40>:    .quad _glapi_tls_Dispatch
121b8e80941Smrg   0, 0           // <ENTRY+48>:    .quad <slot>*8
122b8e80941Smrg};
123b8e80941Smrgstatic const uint64_t TEMPLATE_OFFSET_TLS_ADDR = sizeof(code_templ) - 2*8;
124b8e80941Smrgstatic const uint64_t TEMPLATE_OFFSET_SLOT = sizeof(code_templ) - 1*8;
125b8e80941Smrg
126b8e80941Smrgvoid
127b8e80941Smrgentry_patch(mapi_func entry, int slot)
128b8e80941Smrg{
129b8e80941Smrg   char *code = (char *) entry;
130b8e80941Smrg   *((uint64_t *) (code + TEMPLATE_OFFSET_TLS_ADDR)) = ppc64le_dispatch_tls();
131b8e80941Smrg   *((uint64_t *) (code + TEMPLATE_OFFSET_SLOT)) = slot * sizeof(mapi_func);
132b8e80941Smrg}
133b8e80941Smrg
134b8e80941Smrgmapi_func
135b8e80941Smrgentry_generate(int slot)
136b8e80941Smrg{
137b8e80941Smrg   char *code;
138b8e80941Smrg   mapi_func entry;
139b8e80941Smrg
140b8e80941Smrg   code = u_execmem_alloc(sizeof(code_templ));
141b8e80941Smrg   if (!code)
142b8e80941Smrg      return NULL;
143b8e80941Smrg
144b8e80941Smrg   memcpy(code, code_templ, sizeof(code_templ));
145b8e80941Smrg
146b8e80941Smrg   entry = (mapi_func) code;
147b8e80941Smrg   entry_patch(entry, slot);
148b8e80941Smrg
149b8e80941Smrg   return entry;
150b8e80941Smrg}
151b8e80941Smrg
152b8e80941Smrg#endif /* MAPI_MODE_BRIDGE */
153