1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/*
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Mesa 3-D graphics library
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Version:  7.9
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright (C) 2010 LunarG Inc.
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Permission is hereby granted, free of charge, to any person obtaining a
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * copy of this software and associated documentation files (the "Software"),
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * to deal in the Software without restriction, including without limitation
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * and/or sell copies of the Software, and to permit persons to whom the
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Software is furnished to do so, subject to the following conditions:
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The above copyright notice and this permission notice shall be included
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * in all copies or substantial portions of the Software.
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * DEALINGS IN THE SOFTWARE.
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Authors:
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *    Chia-I Wu <olv@lunarg.com>
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "u_macros.h"
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define X86_ENTRY_SIZE 32
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org__asm__(".text\n"
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org        ".balign 32\n"
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org        "x86_entry_start:");
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define STUB_ASM_ENTRY(func)        \
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ".globl " func "\n"              \
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ".type " func ", @function\n"    \
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ".balign 32\n"                   \
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   func ":"
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define STUB_ASM_CODE(slot)         \
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   "movl " ENTRY_CURRENT_TABLE ", %eax\n\t" \
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   "testl %eax, %eax\n\t"           \
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   "je 1f\n\t"                      \
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   "jmp *(4 * " slot ")(%eax)\n"    \
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   "1:\n\t"                         \
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   "call " ENTRY_CURRENT_TABLE_GET "\n\t" \
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   "jmp *(4 * " slot ")(%eax)"
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define MAPI_TMP_STUB_ASM_GCC
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "mapi_tmp.h"
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#ifndef MAPI_MODE_BRIDGE
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org__asm__(".balign 32\n"
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org        "x86_entry_end:");
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <string.h>
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "u_execmem.h"
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic const char x86_entry_start[];
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic const char x86_entry_end[];
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgentry_patch_public(void)
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgmapi_func
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgentry_get_public(int slot)
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return (mapi_func) (x86_entry_start + slot * X86_ENTRY_SIZE);
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgentry_patch(mapi_func entry, int slot)
79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   char *code = (char *) entry;
81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   *((unsigned long *) (code + 11)) = slot * sizeof(mapi_func);
83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   *((unsigned long *) (code + 22)) = slot * sizeof(mapi_func);
84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgmapi_func
87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgentry_generate(int slot)
88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const char *code_templ = x86_entry_end - X86_ENTRY_SIZE;
90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void *code;
91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   mapi_func entry;
92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   code = u_execmem_alloc(X86_ENTRY_SIZE);
94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (!code)
95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return NULL;
96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   memcpy(code, code_templ, X86_ENTRY_SIZE);
98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   entry = (mapi_func) code;
99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   entry_patch(entry, slot);
100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return entry;
102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif /* MAPI_MODE_BRIDGE */
105