WIP Trampoline Changes

This commit is contained in:
TheBrokenRail 2024-06-04 18:29:13 -04:00
parent 804c9da179
commit f9e0f54620
20 changed files with 867 additions and 808 deletions

3
.gitmodules vendored
View File

@ -22,3 +22,6 @@
[submodule "dependencies/symbol-processor/src"] [submodule "dependencies/symbol-processor/src"]
path = dependencies/symbol-processor/src path = dependencies/symbol-processor/src
url = https://gitea.thebrokenrail.com/minecraft-pi-reborn/symbol-processor.git url = https://gitea.thebrokenrail.com/minecraft-pi-reborn/symbol-processor.git
[submodule "dependencies/trampoline/src"]
path = dependencies/trampoline/src
url = https://gitea.thebrokenrail.com/minecraft-pi-reborn/trampoline.git

View File

@ -27,7 +27,7 @@ if(NOT MCPI_HEADLESS_MODE)
if(BUILD_NATIVE_COMPONENTS AND NOT IS_ARM_TARGETING) if(BUILD_NATIVE_COMPONENTS AND NOT IS_ARM_TARGETING)
set(DEFAULT_USE_MEDIA_LAYER_TRAMPOLINE TRUE) set(DEFAULT_USE_MEDIA_LAYER_TRAMPOLINE TRUE)
endif() endif()
mcpi_option(USE_MEDIA_LAYER_TRAMPOLINE "Whether To Enable The Media Layer Trampoline (Requires QEMU)" BOOL "${DEFAULT_USE_MEDIA_LAYER_TRAMPOLINE}") mcpi_option(USE_MEDIA_LAYER_TRAMPOLINE "Whether To Enable The Media Layer Trampoline" BOOL "${DEFAULT_USE_MEDIA_LAYER_TRAMPOLINE}")
mcpi_option(USE_GLES1_COMPATIBILITY_LAYER "Whether To Enable The GLESv1_CM Compatibility Layer" BOOL TRUE) mcpi_option(USE_GLES1_COMPATIBILITY_LAYER "Whether To Enable The GLESv1_CM Compatibility Layer" BOOL TRUE)
else() else()
set(MCPI_USE_MEDIA_LAYER_TRAMPOLINE FALSE) set(MCPI_USE_MEDIA_LAYER_TRAMPOLINE FALSE)
@ -38,14 +38,10 @@ else()
set(BUILD_MEDIA_LAYER_CORE "${BUILD_ARM_COMPONENTS}") set(BUILD_MEDIA_LAYER_CORE "${BUILD_ARM_COMPONENTS}")
endif() endif()
# QEMU # Trampoline Host
if(BUILD_NATIVE_COMPONENTS) set(MCPI_USE_TRAMPOLINE_HOST FALSE)
include(CheckSymbolExists) if(BUILD_NATIVE_COMPONENTS AND (MCPI_USE_MEDIA_LAYER_TRAMPOLINE OR NOT IS_ARM_TARGETING))
check_symbol_exists("__ARM_ARCH" "" MCPI_IS_ARM32_OR_ARM64_TARGETING) set(MCPI_USE_TRAMPOLINE_HOST TRUE)
set(MCPI_USE_QEMU TRUE)
if(MCPI_IS_ARM32_OR_ARM64_TARGETING AND NOT MCPI_USE_MEDIA_LAYER_TRAMPOLINE)
set(MCPI_USE_QEMU FALSE)
endif()
endif() endif()
# Specify Variant Name # Specify Variant Name

View File

@ -16,10 +16,8 @@ endif()
if(BUILD_NATIVE_COMPONENTS OR (BUILD_MEDIA_LAYER_CORE AND NOT MCPI_HEADLESS_MODE)) if(BUILD_NATIVE_COMPONENTS OR (BUILD_MEDIA_LAYER_CORE AND NOT MCPI_HEADLESS_MODE))
add_subdirectory(LIEF) add_subdirectory(LIEF)
endif() endif()
# QEMU # Trampoline
if(BUILD_NATIVE_COMPONENTS AND MCPI_USE_QEMU) add_subdirectory(trampoline)
add_subdirectory(qemu)
endif()
# GLFW # GLFW
if(BUILD_MEDIA_LAYER_CORE AND NOT MCPI_HEADLESS_MODE) if(BUILD_MEDIA_LAYER_CORE AND NOT MCPI_HEADLESS_MODE)
add_subdirectory(glfw) add_subdirectory(glfw)

View File

@ -1,56 +0,0 @@
project(qemu)
## QEMU
# Version
set(QEMU_VERSION "9.0.0")
# Flatpak Support
set(QEMU_FLATPAK_PATCH "")
if(MCPI_IS_FLATPAK_BUILD)
set(QEMU_FLATPAK_PATCH "sed" "-i" "s/libdrm/libdrm-dis/g" "<SOURCE_DIR>/meson.build")
endif()
# Build
include(ExternalProject)
set(PKGCONFIG_ENV "")
if(DEFINED ENV{PKG_CONFIG_LIBDIR})
set(PKGCONFIG_ENV "PKG_CONFIG_LIBDIR=$ENV{PKG_CONFIG_LIBDIR}")
endif()
ExternalProject_Add(qemu
URL "${CMAKE_CURRENT_SOURCE_DIR}/../../archives/qemu-${QEMU_VERSION}.tar.xz"
# Configure Build
CONFIGURE_COMMAND
"${CMAKE_COMMAND}" "-E" "env"
${PKGCONFIG_ENV}
"CFLAGS=-s"
"CXXFLAGS=-s"
"<SOURCE_DIR>/configure"
"--prefix=${CMAKE_INSTALL_PREFIX}"
"--cross-prefix="
"--cc=${CMAKE_C_COMPILER}"
"--cxx=${CMAKE_CXX_COMPILER}"
"--extra-ldflags=-ldl -Wl,-rpath=$ORIGIN/../lib/native -Wl,--disable-new-dtags"
"--disable-debug-info"
"--target-list=arm-linux-user"
"--without-default-features"
USES_TERMINAL_CONFIGURE TRUE
# Build Command
BUILD_COMMAND "ninja" "qemu-arm"
BUILD_BYPRODUCTS "<BINARY_DIR>/qemu-arm"
USES_TERMINAL_BUILD TRUE
# Disable Install/Test Commands
INSTALL_COMMAND ""
TEST_COMMAND ""
# Patch Command
PATCH_COMMAND "patch" "-p1" "<" "${CMAKE_CURRENT_SOURCE_DIR}/trampoline.patch"
COMMAND ${QEMU_FLATPAK_PATCH}
)
# Install
ExternalProject_Get_property(qemu BINARY_DIR)
install(PROGRAMS "${BINARY_DIR}/qemu-arm" DESTINATION "${MCPI_BIN_DIR}")
# License
ExternalProject_Get_property(qemu SOURCE_DIR)
install(FILES "${SOURCE_DIR}/COPYING" DESTINATION "${MCPI_LEGAL_DIR}/qemu")

View File

@ -1,56 +0,0 @@
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -17,6 +17,7 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#define _ATFILE_SOURCE
+#include <dlfcn.h>
#include "qemu/osdep.h"
#include "qemu/cutils.h"
#include "qemu/path.h"
@@ -9070,6 +9071,17 @@ _syscall5(int, sys_move_mount, int, __from_dfd, const char *, __from_pathname,
int, __to_dfd, const char *, __to_pathname, unsigned int, flag)
#endif
+// g2h For Trampoline
+static CPUState *_trampoline_g2h_cpu = NULL;
+static void *_trampoline_g2h(uint32_t guest_addr) {
+ if (guest_addr == 0) {
+ return NULL;
+ }
+ return g2h(_trampoline_g2h_cpu, guest_addr);
+}
+// Trampoline Function
+typedef void (*_trampoline_t)(typeof(_trampoline_g2h) *g2h, uint32_t id, uint32_t *args);
+
/* This is an internal helper for do_syscall so that it is easier
* to have a single return point, so that actions, such as logging
* of syscall results, can be performed.
@@ -9095,6 +9107,27 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
void *p;
switch(num) {
+ case 0x1337: {
+ // Load Trampoline
+ static _trampoline_t _trampoline = NULL;
+ if (_trampoline == NULL) {
+ // Open Library
+ void *_trampoline_handle = dlopen("libmedia-layer-trampoline.so", RTLD_NOW);
+ // Load Function
+ if (_trampoline_handle != NULL) {
+ _trampoline = dlsym(_trampoline_handle, "trampoline");
+ }
+ }
+ if (_trampoline == NULL) {
+ // Failed To Load
+ qemu_log_mask(LOG_UNIMP, "Unable To Load Media Layer Trampoline: %s\n", dlerror());
+ return -TARGET_ENOSYS;
+ }
+ // Call Trampoline
+ _trampoline_g2h_cpu = cpu;
+ _trampoline(_trampoline_g2h, arg1, g2h(cpu, arg2));
+ return 0;
+ }
case TARGET_NR_exit:
/* In old applications this may be used to implement _exit(2).
However in threaded applications it is used for thread termination,

16
dependencies/trampoline/CMakeLists.txt vendored Normal file
View File

@ -0,0 +1,16 @@
project(trampoline)
## Trampoline Host
# QEMU
set(QEMU_VERSION "9.0.0")
set(TRAMPOLINE_QEMU_ARCHIVE "${CMAKE_CURRENT_SOURCE_DIR}/../../archives/qemu-${QEMU_VERSION}.tar.xz")
if(NOT MCPI_USE_TRAMPOLINE_HOST)
set(TRAMPOLINE_HEADERS_ONLY TRUE)
endif()
# Build
add_subdirectory(src)
# Install
install_trampoline("${MCPI_BIN_DIR}" "${MCPI_LEGAL_DIR}")

1
dependencies/trampoline/src vendored Submodule

@ -0,0 +1 @@
Subproject commit 2a1ee6a91bd539bb583f3214a9e1868464bdad44

View File

@ -20,6 +20,9 @@ if(NOT MCPI_SERVER_MODE)
) )
endif() endif()
target_link_libraries(launcher reborn-util LIB_LIEF) target_link_libraries(launcher reborn-util LIB_LIEF)
if(MCPI_USE_TRAMPOLINE_HOST)
target_link_libraries(launcher trampoline-headers)
endif()
# RPath # RPath
set_target_properties(launcher PROPERTIES INSTALL_RPATH "$ORIGIN/lib/native") set_target_properties(launcher PROPERTIES INSTALL_RPATH "$ORIGIN/lib/native")
target_link_options(launcher PRIVATE "LINKER:--disable-new-dtags") target_link_options(launcher PRIVATE "LINKER:--disable-new-dtags")

View File

@ -177,12 +177,14 @@ void bootstrap() {
// Arguments // Arguments
std::vector<std::string> args; std::vector<std::string> args;
// Non-ARM Systems Need QEMU // Use Trampoline Host If Needed
#ifdef MCPI_USE_TRAMPOLINE_HOST
args.push_back("trampoline");
#endif
// Fix QEMU Bug
#ifdef MCPI_USE_QEMU #ifdef MCPI_USE_QEMU
args.push_back(QEMU_BINARY);
// Fix Bug
args.push_back("-B"); args.push_back("-B");
args.push_back("0x40000"); // Arbitary Value (Aligns To 4k And 16k Page Sizes) args.push_back("0x40000"); // Arbitrary Value (Aligns To 4k And 16k Page Sizes)
#endif #endif
// Setup Linker // Setup Linker

View File

@ -34,14 +34,14 @@ if(BUILD_ARM_COMPONENTS)
install(TARGETS reborn-patch EXPORT sdk DESTINATION "${MCPI_SDK_LIB_DIR}") install(TARGETS reborn-patch EXPORT sdk DESTINATION "${MCPI_SDK_LIB_DIR}")
endif() endif()
# Fake LibPNG To Satisy Symbol Versioning Requirement # Fake LibPNG To Satisfy Symbol Versioning Requirement
if(BUILD_ARM_COMPONENTS) if(BUILD_ARM_COMPONENTS)
add_library(fake-libpng SHARED src/fake-libpng/empty.c) add_library(fake-libpng SHARED src/fake-libpng/empty.c)
set_target_properties(fake-libpng PROPERTIES set_target_properties(fake-libpng PROPERTIES
OUTPUT_NAME "png12" OUTPUT_NAME "png12"
SOVERSION 0 SOVERSION 0
LINK_OPTIONS "LINKER:--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/fake-libpng/empty.vers"
) )
target_link_options(fake-libpng PRIVATE "LINKER:--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/fake-libpng/empty.vers")
# Install # Install
install(TARGETS fake-libpng DESTINATION "${MCPI_LIB_DIR}") install(TARGETS fake-libpng DESTINATION "${MCPI_LIB_DIR}")
endif() endif()

View File

@ -13,6 +13,6 @@
#cmakedefine MCPI_VARIANT_NAME "@MCPI_VARIANT_NAME@" #cmakedefine MCPI_VARIANT_NAME "@MCPI_VARIANT_NAME@"
#cmakedefine MCPI_SDK_DIR "@MCPI_SDK_DIR@" #cmakedefine MCPI_SDK_DIR "@MCPI_SDK_DIR@"
#cmakedefine MCPI_SKIN_SERVER "@MCPI_SKIN_SERVER@" #cmakedefine MCPI_SKIN_SERVER "@MCPI_SKIN_SERVER@"
#cmakedefine MCPI_USE_QEMU #cmakedefine MCPI_USE_TRAMPOLINE_HOST
#cmakedefine MCPI_DISCORD_INVITE "@MCPI_DISCORD_INVITE@" #cmakedefine MCPI_DISCORD_INVITE "@MCPI_DISCORD_INVITE@"
#cmakedefine MCPI_DOCUMENTATION "@MCPI_DOCUMENTATION@" #cmakedefine MCPI_DOCUMENTATION "@MCPI_DOCUMENTATION@"

View File

@ -1,16 +1,16 @@
project(media-layer-trampoline) project(media-layer-trampoline)
# Configuration # Configuration
set(MEDIA_LAYER_TRAMPOLINE_SRC src/media-layer-core.c) # Media Layer Trampoline Source set(MEDIA_LAYER_TRAMPOLINE_SRC src/media-layer-core.cpp) # Media Layer Trampoline Source
if(NOT MCPI_HEADLESS_MODE) if(NOT MCPI_HEADLESS_MODE)
list(APPEND MEDIA_LAYER_TRAMPOLINE_SRC src/GLESv1_CM.c) list(APPEND MEDIA_LAYER_TRAMPOLINE_SRC src/GLESv1_CM.cpp)
endif() endif()
# Build # Build
if(BUILD_NATIVE_COMPONENTS) if(BUILD_NATIVE_COMPONENTS)
# Host Component # Host Component
add_library(media-layer-trampoline src/host/host.c ${MEDIA_LAYER_TRAMPOLINE_SRC}) add_library(media-layer-trampoline src/host/host.cpp ${MEDIA_LAYER_TRAMPOLINE_SRC})
target_link_libraries(media-layer-trampoline reborn-util media-layer-core) target_link_libraries(media-layer-trampoline reborn-util media-layer-core trampoline-headers)
if(NOT MCPI_HEADLESS_MODE) if(NOT MCPI_HEADLESS_MODE)
target_link_libraries(media-layer-trampoline GLESv1_CM) target_link_libraries(media-layer-trampoline GLESv1_CM)
endif() endif()
@ -19,8 +19,8 @@ if(BUILD_NATIVE_COMPONENTS)
install(TARGETS media-layer-trampoline DESTINATION "${MCPI_LIB_DIR}") install(TARGETS media-layer-trampoline DESTINATION "${MCPI_LIB_DIR}")
elseif(BUILD_ARM_COMPONENTS) elseif(BUILD_ARM_COMPONENTS)
# Guest Component # Guest Component
add_library(media-layer-core SHARED src/guest/guest.c ${MEDIA_LAYER_TRAMPOLINE_SRC} $<TARGET_OBJECTS:media-layer-extras>) add_library(media-layer-core SHARED src/guest/guest.cpp ${MEDIA_LAYER_TRAMPOLINE_SRC} $<TARGET_OBJECTS:media-layer-extras>)
target_link_libraries(media-layer-core media-layer-headers reborn-util) target_link_libraries(media-layer-core PUBLIC media-layer-headers PRIVATE reborn-util PRIVATE trampoline-headers)
target_compile_definitions(media-layer-core PRIVATE -DMEDIA_LAYER_TRAMPOLINE_GUEST) target_compile_definitions(media-layer-core PRIVATE -DMEDIA_LAYER_TRAMPOLINE_GUEST)
# Install # Install
if(MCPI_USE_MEDIA_LAYER_TRAMPOLINE) if(MCPI_USE_MEDIA_LAYER_TRAMPOLINE)

View File

@ -1,581 +0,0 @@
#include <stdint.h>
#include <GLES/gl.h>
#include <libreborn/libreborn.h>
#include "common/common.h"
CALL(11, glFogfv, void, (GLenum pname, const GLfloat *params))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(pname, (uint32_t) params);
#else
GLenum pname = next_int();
GLfloat *params = next_ptr();
// Run
func(pname, params);
#endif
}
// 'pointer' Is Only Supported As An Integer, Not As An Actual Pointer
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
#define CALL_GL_POINTER(unique_id, name) \
CALL(unique_id, name, void, (GLint size, GLenum type, GLsizei stride, const void *pointer)) \
trampoline(size, type, stride, (uint32_t) pointer); \
}
#else
#define CALL_GL_POINTER(unique_id, name) \
CALL(unique_id, name, unused, unused) \
GLint size = next_int(); \
GLenum type = next_int(); \
GLsizei stride = next_int(); \
const void *pointer = (const void *) (uint64_t) next_int(); \
/* Run */ \
func(size, type, stride, pointer); \
}
#endif
CALL_GL_POINTER(12, glVertexPointer)
CALL(13, glLineWidth, void, (GLfloat width))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(pun_to(uint32_t, width));
#else
GLfloat width = next_float();
// Run
func(width);
#endif
}
CALL(14, glBlendFunc, void, (GLenum sfactor, GLenum dfactor))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(sfactor, dfactor);
#else
GLenum sfactor = next_int();
GLenum dfactor = next_int();
// Run
func(sfactor, dfactor);
#endif
}
CALL(15, glDrawArrays, void, (GLenum mode, GLint first, GLsizei count))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(mode, first, count);
#else
GLenum mode = next_int();
GLint first = next_int();
GLsizei count = next_int();
// Run
func(mode, first, count);
#endif
}
CALL(16, glColor4f, void, (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(pun_to(uint32_t, red), pun_to(uint32_t, green), pun_to(uint32_t, blue), pun_to(uint32_t, alpha));
#else
GLfloat red = next_float();
GLfloat green = next_float();
GLfloat blue = next_float();
GLfloat alpha = next_float();
// Run
func(red, green, blue, alpha);
#endif
}
CALL(17, glClear, void, (GLbitfield mask))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(mask);
#else
GLbitfield mask = next_int();
// Run
func(mask);
#endif
}
CALL(18, glBufferData, void, (GLenum target, GLsizeiptr size, const void *data, GLenum usage))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(target, size, (uint32_t) data, usage);
#else
GLenum target = next_int();
GLsizeiptr size = next_int();
const void *data = next_ptr();
GLenum usage = next_int();
// Run
func(target, size, data, usage);
#endif
}
CALL(19, glFogx, void, (GLenum pname, GLfixed param))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(pname, param);
#else
GLenum pname = next_int();
GLfixed param = next_int();
// Run
func(pname, param);
#endif
}
CALL(20, glFogf, void, (GLenum pname, GLfloat param))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(pname, pun_to(uint32_t, param));
#else
GLenum pname = next_int();
GLfloat param = next_float();
// Run
func(pname, param);
#endif
}
CALL(21, glMatrixMode, void, (GLenum mode))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(mode);
#else
GLenum mode = next_int();
// Run
func(mode);
#endif
}
CALL_GL_POINTER(22, glColorPointer)
CALL(23, glScissor, void, (GLint x, GLint y, GLsizei width, GLsizei height))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(x, y, width, height);
#else
GLint x = next_int();
GLint y = next_int();
GLsizei width = next_int();
GLsizei height = next_int();
// Run
func(x, y, width, height);
#endif
}
CALL(24, glTexParameteri, void, (GLenum target, GLenum pname, GLint param))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(target, pname, param);
#else
GLenum target = next_int();
GLenum pname = next_int();
GLint param = next_int();
// Run
func(target, pname, param);
#endif
}
CALL(25, glTexImage2D, void, (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *pixels))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(target, level, internalformat, width, height, border, format, type, (uint32_t) pixels);
#else
GLenum target = next_int();
GLint level = next_int();
GLint internalformat = next_int();
GLsizei width = next_int();
GLsizei height = next_int();
GLint border = next_int();
GLenum format = next_int();
GLenum type = next_int();
const void *pixels = next_ptr();
// Run
func(target, level, internalformat, width, height, border, format, type, pixels);
#endif
}
CALL(26, glEnable, void, (GLenum cap))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(cap);
#else
GLenum cap = next_int();
// Run
func(cap);
#endif
}
CALL(27, glEnableClientState, void, (GLenum array))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(array);
#else
GLenum array = next_int();
// Run
func(array);
#endif
}
CALL(28, glPolygonOffset, void, (GLfloat factor, GLfloat units))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(pun_to(uint32_t, factor), pun_to(uint32_t, units));
#else
GLfloat factor = next_float();
GLfloat units = next_float();
// Run
func(factor, units);
#endif
}
CALL_GL_POINTER(41, glTexCoordPointer)
CALL(29, glDisableClientState, void, (GLenum array))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(array);
#else
GLenum array = next_int();
// Run
func(array);
#endif
}
CALL(30, glDepthRangef, void, (GLclampf near, GLclampf far))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(pun_to(uint32_t, near), pun_to(uint32_t, far));
#else
GLclampf near = next_float();
GLclampf far = next_float();
// Run
func(near, far);
#endif
}
CALL(31, glDepthFunc, void, (GLenum func))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(func);
#else
GLenum func2 = next_int();
// Run
func(func2);
#endif
}
CALL(32, glBindBuffer, void, (GLenum target, GLuint buffer))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(target, buffer);
#else
GLenum target = next_int();
GLenum buffer = next_int();
// Run
func(target, buffer);
#endif
}
CALL(33, glClearColor, void, (GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(pun_to(uint32_t, red), pun_to(uint32_t, green), pun_to(uint32_t, blue), pun_to(uint32_t, alpha));
#else
GLclampf red = next_float();
GLclampf green = next_float();
GLclampf blue = next_float();
GLclampf alpha = next_float();
// Run
func(red, green, blue, alpha);
#endif
}
CALL(34, glPopMatrix, void, ())
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline();
#else
// Run
func();
#endif
}
CALL(35, glLoadIdentity, void, ())
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline();
#else
// Run
func();
#endif
}
CALL(36, glScalef, void, (GLfloat x, GLfloat y, GLfloat z))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(pun_to(uint32_t, x), pun_to(uint32_t, y), pun_to(uint32_t, z));
#else
GLfloat x = next_float();
GLfloat y = next_float();
GLfloat z = next_float();
// Run
func(x, y, z);
#endif
}
CALL(37, glPushMatrix, void, ())
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline();
#else
// Run
func();
#endif
}
CALL(38, glDepthMask, void, (GLboolean flag))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(flag);
#else
GLboolean flag = next_int();
// Run
func(flag);
#endif
}
CALL(39, glHint, void, (GLenum target, GLenum mode))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(target, mode);
#else
GLenum target = next_int();
GLenum mode = next_int();
// Run
func(target, mode);
#endif
}
CALL(40, glMultMatrixf, void, (const GLfloat *m))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline((uint32_t) m);
#else
GLfloat *m = next_ptr();
// Run
func(m);
#endif
}
CALL(42, glDeleteBuffers, void, (GLsizei n, const GLuint *buffers))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(n, (uint32_t) buffers);
#else
GLsizei n = next_int();
GLuint *buffers = next_ptr();
// Run
func(n, buffers);
#endif
}
CALL(43, glColorMask, void, (GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(red, green, blue, alpha);
#else
GLboolean red = next_int();
GLboolean green = next_int();
GLboolean blue = next_int();
GLboolean alpha = next_int();
// Run
func(red, green, blue, alpha);
#endif
}
CALL(44, glTexSubImage2D, void, (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(target, level, xoffset, yoffset, width, height, format, type, (uint32_t) pixels);
#else
GLenum target = next_int();
GLint level = next_int();
GLint xoffset = next_int();
GLint yoffset = next_int();
GLsizei width = next_int();
GLsizei height = next_int();
GLenum format = next_int();
GLenum type = next_int();
const void *pixels = next_ptr();
// Run
func(target, level, xoffset, yoffset, width, height, format, type, pixels);
#endif
}
CALL(45, glGenTextures, void, (GLsizei n, GLuint *textures))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(n, (uint32_t) textures);
#else
GLsizei n = next_int();
GLuint *textures = next_ptr();
// Run
func(n, textures);
#endif
}
CALL(46, glDeleteTextures, void, (GLsizei n, const GLuint *textures))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(n, (uint32_t) textures);
#else
GLsizei n = next_int();
GLuint *textures = next_ptr();
// Run
func(n, textures);
#endif
}
CALL(47, glAlphaFunc, void, (GLenum func, GLclampf ref))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(func, pun_to(uint32_t, ref));
#else
GLenum func2 = next_int();
GLclampf ref = next_float();
// Run
func(func2, ref);
#endif
}
CALL(48, glGetFloatv, void, (GLenum pname, GLfloat *params))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(pname, (uint32_t) params);
#else
GLenum pname = next_int();
GLfloat *params = next_ptr();
// Run
func(pname, params);
#endif
}
CALL(49, glBindTexture, void, (GLenum target, GLuint texture))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(target, texture);
#else
GLenum target = next_int();
GLuint texture = next_int();
// Run
func(target, texture);
#endif
}
CALL(50, glTranslatef, void, (GLfloat x, GLfloat y, GLfloat z))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(pun_to(uint32_t, x), pun_to(uint32_t, y), pun_to(uint32_t, z));
#else
GLfloat x = next_float();
GLfloat y = next_float();
GLfloat z = next_float();
// Run
func(x, y, z);
#endif
}
CALL(51, glShadeModel, void, (GLenum mode))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(mode);
#else
GLenum mode = next_int();
// Run
func(mode);
#endif
}
CALL(52, glOrthof, void, (GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat near, GLfloat far))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(pun_to(uint32_t, left), pun_to(uint32_t, right), pun_to(uint32_t, bottom), pun_to(uint32_t, top), pun_to(uint32_t, near), pun_to(uint32_t, far));
#else
GLfloat left = next_float();
GLfloat right = next_float();
GLfloat bottom = next_float();
GLfloat top = next_float();
GLfloat near = next_float();
GLfloat far = next_float();
// Run
func(left, right, bottom, top, near, far);
#endif
}
CALL(53, glDisable, void, (GLenum cap))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(cap);
#else
GLenum cap = next_int();
// Run
func(cap);
#endif
}
CALL(54, glCullFace, void, (GLenum mode))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(mode);
#else
GLenum mode = next_int();
// Run
func(mode);
#endif
}
CALL(55, glRotatef, void, (GLfloat angle, GLfloat x, GLfloat y, GLfloat z))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(pun_to(uint32_t, angle), pun_to(uint32_t, x), pun_to(uint32_t, y), pun_to(uint32_t, z));
#else
GLfloat angle = next_float();
GLfloat x = next_float();
GLfloat y = next_float();
GLfloat z = next_float();
// Run
func(angle, x, y, z);
#endif
}
CALL(56, glViewport, void, (GLint x, GLint y, GLsizei width, GLsizei height))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(x, y, width, height);
#else
GLint x = next_int();
GLint y = next_int();
GLsizei width = next_int();
GLsizei height = next_int();
// Run
func(x, y, width, height);
#endif
}
CALL(57, glNormal3f, void, (GLfloat nx, GLfloat ny, GLfloat nz))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(pun_to(uint32_t, nx), pun_to(uint32_t, ny), pun_to(uint32_t, nz));
#else
GLfloat nx = next_float();
GLfloat ny = next_float();
GLfloat nz = next_float();
// Run
func(nx, ny, nz);
#endif
}
CALL(58, glIsEnabled, GLboolean, (GLenum cap))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
return trampoline(cap);
#else
GLenum cap = next_int();
// Run
ret(func(cap));
#endif
}
CALL(61, glGetIntegerv, void, (GLenum pname, GLint *params))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(pname, (uint32_t) params);
#else
GLenum pname = next_int();
GLint *params = next_ptr();
// Run
func(pname, params);
#endif
}
CALL(65, glReadPixels, void, (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void *data))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(x, y, width, height, format, type, (uint32_t) data);
#else
GLint x = next_int();
GLint y = next_int();
GLsizei width = next_int();
GLsizei height = next_int();
GLenum format = next_int();
GLenum type = next_int();
void *data = next_ptr();
// Run
func(x, y, width, height, format, type, data);
#endif
}
CALL(67, glGenBuffers, void, (GLsizei n, GLuint *buffers))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(n, (uint32_t) buffers);
#else
GLsizei n = next_int();
GLuint *buffers = next_ptr();
// Run
func(n, buffers);
#endif
}

View File

@ -0,0 +1,660 @@
#include <cstdint>
#include <GLES/gl.h>
#include <libreborn/libreborn.h>
#include "common/common.h"
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
static int get_glFogfv_params_length(GLenum pname) {
return pname == GL_FOG_COLOR ? 4 : 1;
}
#endif
CALL(11, glFogfv, void, (GLenum pname, const GLfloat *params))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(pname, copy_array(get_glFogfv_params_length(pname), params));
#else
GLenum pname = args.next<GLenum>();
const GLfloat *params = args.next_arr<GLfloat>();
func(pname, params);
return 0;
#endif
}
// 'pointer' Is Only Supported As An Integer, Not As An Actual Pointer
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
#define CALL_GL_POINTER(unique_id, name) \
CALL(unique_id, name, void, (GLint size, GLenum type, GLsizei stride, const void *pointer)) \
trampoline(size, type, stride, uint32_t(pointer)); \
}
#else
#define CALL_GL_POINTER(unique_id, name) \
CALL(unique_id, name, unused, unused) \
GLint size = args.next<GLint>(); \
GLenum type = args.next<GLenum>(); \
GLsizei stride = args.next<GLsizei>(); \
const void *pointer = (const void *) (uint64_t) args.next<uint32_t>(); \
func(size, type, stride, pointer); \
return 0; \
}
#endif
CALL_GL_POINTER(12, glVertexPointer)
CALL(13, glLineWidth, void, (GLfloat width))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(width);
#else
func(args.next<float>());
return 0;
#endif
}
CALL(14, glBlendFunc, void, (GLenum sfactor, GLenum dfactor))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(sfactor, dfactor);
#else
GLenum sfactor = args.next<GLenum>();
GLenum dfactor = args.next<GLenum>();
func(sfactor, dfactor);
return 0;
#endif
}
CALL(15, glDrawArrays, void, (GLenum mode, GLint first, GLsizei count))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(mode, first, count);
#else
GLenum mode = args.next<GLenum>();
GLint first = args.next<GLint>();
GLsizei count = args.next<GLsizei>();
func(mode, first, count);
return 0;
#endif
}
CALL(16, glColor4f, void, (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(red, green, blue, alpha);
#else
GLfloat red = args.next<float>();
GLfloat green = args.next<float>();
GLfloat blue = args.next<float>();
GLfloat alpha = args.next<float>();
func(red, green, blue, alpha);
return 0;
#endif
}
CALL(17, glClear, void, (GLbitfield mask))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(mask);
#else
func(args.next<GLbitfield>());
return 0;
#endif
}
CALL(18, glBufferData, void, (GLenum target, GLsizeiptr size, const void *data, GLenum usage))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(target, copy_array(size, (unsigned char *) data), usage);
#else
GLenum target = args.next<GLenum>();
uint32_t size;
const unsigned char *data = args.next_arr<unsigned char>(&size);
GLenum usage = args.next<GLenum>();
func(target, GLsizeiptr(size), data, usage);
return 0;
#endif
}
CALL(19, glFogx, void, (GLenum pname, GLfixed param))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(pname, param);
#else
GLenum pname = args.next<GLenum>();
GLfixed param = args.next<GLfixed>();
func(pname, param);
return 0;
#endif
}
CALL(20, glFogf, void, (GLenum pname, GLfloat param))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(pname, param);
#else
GLenum pname = args.next<GLenum>();
GLfloat param = args.next<GLfloat>();
func(pname, param);
return 0;
#endif
}
CALL(21, glMatrixMode, void, (GLenum mode))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(mode);
#else
func(args.next<GLenum>());
return 0;
#endif
}
CALL_GL_POINTER(22, glColorPointer)
CALL(23, glScissor, void, (GLint x, GLint y, GLsizei width, GLsizei height))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(x, y, width, height);
#else
GLint x = args.next<GLint>();
GLint y = args.next<GLint>();
GLsizei width = args.next<GLsizei>();
GLsizei height = args.next<GLsizei>();
func(x, y, width, height);
return 0;
#endif
}
CALL(24, glTexParameteri, void, (GLenum target, GLenum pname, GLint param))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(target, pname, param);
#else
GLenum target = args.next<GLenum>();
GLenum pname = args.next<GLenum>();
GLint param = args.next<GLint>();
func(target, pname, param);
return 0;
#endif
}
// Get Size (In Memory) Of Specified Texture
static int get_texture_size(const GLsizei width, const GLsizei height, const GLenum format, const GLenum type, bool is_upload) {
// Calculate Per-Pixel Multiplier
int multiplier;
if (type == GL_UNSIGNED_BYTE) {
switch (format) {
case GL_RGB: {
multiplier = 3;
break;
}
case GL_RGBA: {
multiplier = 4;
break;
}
default: {
ERR("Unsupported Texture Format: %u", format);
}
}
} else {
// GL_UNSIGNED_SHORT_*
multiplier = sizeof(unsigned short);
}
// Calculate Line Size
int line_size = width * multiplier;
{
// Handle Alignment
int alignment;
glGetIntegerv(is_upload ? GL_UNPACK_ALIGNMENT : GL_PACK_ALIGNMENT, &alignment);
// Round
int diff = line_size % alignment;
line_size = line_size + (alignment - diff);
}
// Return
return line_size * height;
}
CALL(25, glTexImage2D, void, (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *pixels))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(target, level, internalformat, width, height, border, format, type, copy_array(get_texture_size(width, height, format, type, true), (const unsigned char *) pixels));
#else
GLenum target = args.next<GLenum>();
GLint level = args.next<GLint>();
GLint internalformat = args.next<GLint>();
GLsizei width = args.next<GLsizei>();
GLsizei height = args.next<GLsizei>();
GLint border = args.next<GLint>();
GLenum format = args.next<GLenum>();
GLenum type = args.next<GLenum>();
const unsigned char *pixels = args.next_arr<unsigned char>();
func(target, level, internalformat, width, height, border, format, type, pixels);
return 0;
#endif
}
CALL(26, glEnable, void, (GLenum cap))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(cap);
#else
func(args.next<GLenum>());
return 0;
#endif
}
CALL(27, glEnableClientState, void, (GLenum array))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(array);
#else
func(args.next<GLenum>());
return 0;
#endif
}
CALL(28, glPolygonOffset, void, (GLfloat factor, GLfloat units))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(factor, units);
#else
GLfloat factor = args.next<GLfloat>();
GLfloat units = args.next<GLfloat>();
func(factor, units);
return 0;
#endif
}
CALL_GL_POINTER(41, glTexCoordPointer)
CALL(29, glDisableClientState, void, (GLenum array))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(array);
#else
func(args.next<GLenum>());
return 0;
#endif
}
CALL(30, glDepthRangef, void, (GLclampf near, GLclampf far))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(near, far);
#else
GLclampf near = args.next<GLclampf>();
GLclampf far = args.next<GLclampf>();
func(near, far);
return 0;
#endif
}
CALL(31, glDepthFunc, void, (GLenum func))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(func);
#else
func(args.next<GLenum>());
return 0;
#endif
}
CALL(32, glBindBuffer, void, (GLenum target, GLuint buffer))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(target, buffer);
#else
GLenum target = args.next<GLenum>();
GLenum buffer = args.next<GLenum>();
func(target, buffer);
return 0;
#endif
}
CALL(33, glClearColor, void, (GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(red, green, blue, alpha);
#else
GLclampf red = args.next<GLclampf>();
GLclampf green = args.next<GLclampf>();
GLclampf blue = args.next<GLclampf>();
GLclampf alpha = args.next<GLclampf>();
func(red, green, blue, alpha);
return 0;
#endif
}
CALL(34, glPopMatrix, void, ())
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline();
#else
func();
return 0;
#endif
}
CALL(35, glLoadIdentity, void, ())
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline();
#else
func();
return 0;
#endif
}
CALL(36, glScalef, void, (GLfloat x, GLfloat y, GLfloat z))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(x, y, z);
#else
GLfloat x = args.next<GLfloat>();
GLfloat y = args.next<GLfloat>();
GLfloat z = args.next<GLfloat>();
func(x, y, z);
return 0;
#endif
}
CALL(37, glPushMatrix, void, ())
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline();
#else
func();
return 0;
#endif
}
CALL(38, glDepthMask, void, (GLboolean flag))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(flag);
#else
func(args.next<GLboolean>());
return 0;
#endif
}
CALL(39, glHint, void, (GLenum target, GLenum mode))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(target, mode);
#else
GLenum target = args.next<GLenum>();
GLenum mode = args.next<GLenum>();
func(target, mode);
return 0;
#endif
}
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
static int get_glMultMatrixf_size() {
return 16;
}
#endif
CALL(40, glMultMatrixf, void, (const GLfloat *m))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(copy_array(get_glMultMatrixf_size(), m));
#else
func(args.next_arr<GLfloat>());
return 0;
#endif
}
CALL(42, glDeleteBuffers, void, (GLsizei n, const GLuint *buffers))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(copy_array(n, buffers));
#else
uint32_t n;
const GLuint *buffers = args.next_arr<GLuint>(&n);
func(GLsizei(n), buffers);
return 0;
#endif
}
CALL(43, glColorMask, void, (GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(red, green, blue, alpha);
#else
GLboolean red = args.next<GLboolean>();
GLboolean green = args.next<GLboolean>();
GLboolean blue = args.next<GLboolean>();
GLboolean alpha = args.next<GLboolean>();
func(red, green, blue, alpha);
return 0;
#endif
}
CALL(44, glTexSubImage2D, void, (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(target, level, xoffset, yoffset, width, height, format, type, copy_array(get_texture_size(width, height, format, type, true), (const unsigned char *) pixels));
#else
GLenum target = args.next<GLenum>();
GLint level = args.next<GLint>();
GLint xoffset = args.next<GLint>();
GLint yoffset = args.next<GLint>();
GLsizei width = args.next<GLsizei>();
GLsizei height = args.next<GLsizei>();
GLenum format = args.next<GLenum>();
GLenum type = args.next<GLenum>();
const unsigned char *pixels = args.next_arr<unsigned char>();
func(target, level, xoffset, yoffset, width, height, format, type, pixels);
return 0;
#endif
}
CALL(45, glGenTextures, void, (GLsizei n, GLuint *textures))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(n, uint32_t(textures));
#else
GLsizei n = args.next<GLsizei>();
GLuint *textures = new GLuint[n];
func(n, textures);
writer(args.next<uint32_t>(), textures, n * sizeof(GLuint));
delete[] textures;
return 0;
#endif
}
CALL(46, glDeleteTextures, void, (GLsizei n, const GLuint *textures))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(copy_array(n, textures));
#else
uint32_t n;
const GLuint *textures = args.next_arr<GLuint>(&n);
func(GLsizei(n), textures);
return 0;
#endif
}
CALL(47, glAlphaFunc, void, (GLenum func, GLclampf ref))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(func, ref);
#else
GLenum func2 = args.next<GLenum>();
GLclampf ref = args.next<GLclampf>();
func(func2, ref);
return 0;
#endif
}
#ifdef MEDIA_LAYER_TRAMPOLINE_HOST
static int get_glGetFloatv_params_size(GLenum pname) {
switch (pname) {
case GL_MODELVIEW_MATRIX:
case GL_PROJECTION_MATRIX: {
return 16;
}
case GL_ALIASED_LINE_WIDTH_RANGE:
case GL_SMOOTH_LINE_WIDTH_RANGE: {
return 2;
}
default: {
ERR("Unsupported glGetFloatv Property: %u", pname);
}
}
}
#endif
CALL(48, glGetFloatv, void, (GLenum pname, GLfloat *params))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(pname, uint32_t(params));
#else
GLenum pname = args.next<GLenum>();
int size = get_glGetFloatv_params_size(pname);
GLfloat *params = new GLfloat[size];
func(pname, params);
writer(args.next<uint32_t>(), params, size * sizeof(GLfloat));
delete[] params;
return 0;
#endif
}
CALL(49, glBindTexture, void, (GLenum target, GLuint texture))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(target, texture);
#else
GLenum target = args.next<GLenum>();
GLuint texture = args.next<GLuint>();
func(target, texture);
return 0;
#endif
}
CALL(50, glTranslatef, void, (GLfloat x, GLfloat y, GLfloat z))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(x, y, z);
#else
GLfloat x = args.next<GLfloat>();
GLfloat y = args.next<GLfloat>();
GLfloat z = args.next<GLfloat>();
func(x, y, z);
return 0;
#endif
}
CALL(51, glShadeModel, void, (GLenum mode))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(mode);
#else
func(args.next<GLenum>());
return 0;
#endif
}
CALL(52, glOrthof, void, (GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat near, GLfloat far))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(left, right, bottom, top, near, far);
#else
GLfloat left = args.next<GLfloat>();
GLfloat right = args.next<GLfloat>();
GLfloat bottom = args.next<GLfloat>();
GLfloat top = args.next<GLfloat>();
GLfloat near = args.next<GLfloat>();
GLfloat far = args.next<GLfloat>();
func(left, right, bottom, top, near, far);
return 0;
#endif
}
CALL(53, glDisable, void, (GLenum cap))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(cap);
#else
func(args.next<GLenum>());
return 0;
#endif
}
CALL(54, glCullFace, void, (GLenum mode))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(mode);
#else
func(args.next<GLenum>());
return 0;
#endif
}
CALL(55, glRotatef, void, (GLfloat angle, GLfloat x, GLfloat y, GLfloat z))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(angle, x, y, z);
#else
GLfloat angle = args.next<GLfloat>();
GLfloat x = args.next<GLfloat>();
GLfloat y = args.next<GLfloat>();
GLfloat z = args.next<GLfloat>();
func(angle, x, y, z);
return 0;
#endif
}
CALL(56, glViewport, void, (GLint x, GLint y, GLsizei width, GLsizei height))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(x, y, width, height);
#else
GLint x = args.next<GLint>();
GLint y = args.next<GLint>();
GLsizei width = args.next<GLsizei>();
GLsizei height = args.next<GLsizei>();
func(x, y, width, height);
return 0;
#endif
}
CALL(57, glNormal3f, void, (GLfloat nx, GLfloat ny, GLfloat nz))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(nx, ny, nz);
#else
GLfloat nx = args.next<GLfloat>();
GLfloat ny = args.next<GLfloat>();
GLfloat nz = args.next<GLfloat>();
func(nx, ny, nz);
return 0;
#endif
}
CALL(58, glIsEnabled, GLboolean, (GLenum cap))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
return trampoline(cap);
#else
return func(args.next<GLenum>());
#endif
}
#ifdef MEDIA_LAYER_TRAMPOLINE_HOST
static int get_glGetIntegerv_params_size(GLenum pname) {
switch (pname) {
case GL_TEXTURE_BINDING_2D:
case GL_PACK_ALIGNMENT:
case GL_UNPACK_ALIGNMENT: {
return 1;
}
case GL_VIEWPORT: {
return 4;
}
default: {
ERR("Unsupported glGetIntegerv Property: %u", pname);
}
}
}
#endif
CALL(61, glGetIntegerv, void, (GLenum pname, GLint *params))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(pname, uint32_t(params));
#else
GLenum pname = args.next<GLenum>();
int size = get_glGetIntegerv_params_size(pname);
GLint *params = new GLint[size];
func(pname, params);
writer(args.next<uint32_t>(), params, size * sizeof(GLint));
delete[] params;
return 0;
#endif
}
CALL(65, glReadPixels, void, (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void *data))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(x, y, width, height, format, type, uint32_t(data));
#else
GLint x = args.next<GLint>();
GLint y = args.next<GLint>();
GLsizei width = args.next<GLsizei>();
GLsizei height = args.next<GLsizei>();
GLenum format = args.next<GLenum>();
GLenum type = args.next<GLenum>();
int data_size = get_texture_size(width, height, format, type, false);
unsigned char *data = new unsigned char[data_size];
func(x, y, width, height, format, type, data);
writer(args.next<uint32_t>(), data, data_size);
delete[] data;
return 0;
#endif
}
CALL(67, glGenBuffers, void, (GLsizei n, GLuint *buffers))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(n, uint32_t(buffers));
#else
GLsizei n = args.next<GLsizei>();
GLuint *buffers = new GLuint[n];
func(n, buffers);
writer(args.next<uint32_t>(), buffers, n * sizeof(GLuint));
delete[] buffers;
return 0;
#endif
}

View File

@ -1,21 +1,21 @@
#pragma once #pragma once
#include <cstdint>
#include <type_traits>
#include <trampoline/types.h>
#if __BYTE_ORDER != __LITTLE_ENDIAN #if __BYTE_ORDER != __LITTLE_ENDIAN
#error "Only Little Endian Is Supported" #error "Only Little Endian Is Supported"
#endif #endif
static_assert(sizeof(int) == sizeof(int32_t));
#define block_pointer(T) static_assert(!std::is_pointer<T>::value, "Do Not Use Raw Pointers Here")
#if defined(MEDIA_LAYER_TRAMPOLINE_HOST) #if defined(MEDIA_LAYER_TRAMPOLINE_HOST)
#include "../host/host.h" #include "../host/host.h"
#elif defined(MEDIA_LAYER_TRAMPOLINE_GUEST) #elif defined(MEDIA_LAYER_TRAMPOLINE_GUEST)
#include "../guest/guest.h" #include "../guest/guest.h"
#else #else
#error "Invalid Configuration" #error "Invalid Configuration"
#endif #endif
//#define pun_to(type, x) (*(type *) &(x))
#define pun_to(type, x) \
({ \
union { typeof(x) a; type b; } _pun; \
_pun.a = x; \
_pun.b; \
})

View File

@ -3,13 +3,13 @@
#include "guest.h" #include "guest.h"
uint32_t _trampoline(uint32_t id, uint32_t *args) { uint32_t _raw_trampoline(const uint32_t id, const uint32_t length, const unsigned char *args) {
// Make Syscall // Make Syscall
long ret = syscall(0x1337 /* See trampoline.patch */, id, args); long ret = syscall(0x1337 /* See trampoline.patch */, id, length, args);
if (ret == -1) { if (ret == -1) {
// Error // Error
ERR("Trampoline Error: %s", strerror(errno)); ERR("Trampoline Error: %s", strerror(errno));
} }
// Return // Return
return args[0]; return *(uint32_t *) args;
} }

View File

@ -1,10 +1,65 @@
#pragma once #pragma once
#include <stdint.h> #include <cstring>
#include "../common/common.h"
// Trampoline Function // Trampoline Function
uint32_t _trampoline(uint32_t id, uint32_t *args); uint32_t _raw_trampoline(uint32_t id, uint32_t length, const unsigned char *args);
#define trampoline(...) _trampoline(_id, (uint32_t[]){__VA_ARGS__})
// Compile Trampoline Arguments
template <typename T>
void _handle_trampoline_arg(unsigned char *&out, const T arg) {
block_pointer(T);
*(T *) out = arg;
out += sizeof(T);
}
// Array Specialization
struct copy_array {
template <typename T>
copy_array(uint32_t length, T *arr) {
block_pointer(T);
if (arr == nullptr) {
length = 0;
}
this->size = length * sizeof(T);
this->data = arr;
}
copy_array(const char *str) {
this->size = str != nullptr ? (strlen(str) + 1) : 0;
this->data = str;
}
uint32_t size;
const void *data;
};
template <>
inline void _handle_trampoline_arg<copy_array>(unsigned char *&out, const copy_array arg) {
*(uint32_t *) out = arg.size;
out += sizeof(uint32_t);
if (arg.size > 0) {
memcpy(out, arg.data, arg.size);
out += arg.size;
}
}
// Variadic Templates
__attribute__((unused)) static void _add_to_trampoline_args(__attribute__((unused)) unsigned char *&out) {
}
template <typename T, typename... Args>
void _add_to_trampoline_args(unsigned char *&out, T first, Args... args) {
_handle_trampoline_arg(out, first);
_add_to_trampoline_args(out, args...);
}
// Main Trampoline Function
template <typename... Args>
unsigned int _trampoline(unsigned int id, Args... args) {
static unsigned char out[MAX_TRAMPOLINE_ARGS_SIZE];
unsigned char *end = out;
_add_to_trampoline_args(end, args...);
const uint32_t length = end - out;
return _raw_trampoline(id, length, out);
}
#define trampoline(...) _trampoline(_id, ##__VA_ARGS__)
// Macro // Macro
#define CALL(unique_id, name, return_type, args) \ #define CALL(unique_id, name, return_type, args) \

View File

@ -4,7 +4,7 @@
// Registration // Registration
static handler_t *handlers[256]; static handler_t *handlers[256];
void _add_handler(unsigned char id, handler_t *handler) { void _add_handler(const unsigned char id, handler_t *handler) {
if (handlers[id]) { if (handlers[id]) {
ERR("Conflicting Trampolines For ID: %i", (int) id); ERR("Conflicting Trampolines For ID: %i", (int) id);
} }
@ -12,6 +12,6 @@ void _add_handler(unsigned char id, handler_t *handler) {
} }
// Trampoline // Trampoline
void trampoline(g2h_t g2h, uint32_t id, uint32_t *args) { uint32_t trampoline(const trampoline_writer_t writer, const uint32_t id, const unsigned char *args) {
handlers[id](g2h, args); return handlers[id](writer, args);
} }

View File

@ -1,27 +1,49 @@
#pragma once #pragma once
#include <stdint.h> #include "../common/common.h"
// Trampoline Function // Trampoline Function
typedef void *(*g2h_t)(uint32_t guest_addr); extern "C" std::remove_pointer<trampoline_t>::type trampoline;
void trampoline(g2h_t g2h, uint32_t id, uint32_t *args); // See trampoline.patch
// Macro // Macro
typedef void handler_t(g2h_t g2h, uint32_t *args); typedef uint32_t handler_t(trampoline_writer_t writer, const unsigned char *args);
__attribute__((visibility("internal"))) void _add_handler(unsigned char id, handler_t *handler); __attribute__((visibility("internal"))) void _add_handler(unsigned char id, handler_t *handler);
#define CALL(unique_id, name, ignored1, ignored2) \ #define CALL(unique_id, name, ignored1, ignored2) \
static handler_t _run_##name; \ static handler_t _run_##name; \
__attribute__((constructor)) static void _init_##name() { \ __attribute__((constructor)) static void _init_##name() { \
_add_handler(unique_id, _run_##name); \ _add_handler(unique_id, _run_##name); \
} \ } \
static void _run_##name(__attribute__((unused)) g2h_t g2h, __attribute__((unused)) uint32_t *args) { \ static uint32_t _run_##name(__attribute__((unused)) trampoline_writer_t writer, const unsigned char *raw_args) { \
__attribute__((unused)) int _current_arg = 0; \ __attribute__((unused)) TrampolineArguments args(raw_args); \
static typeof(name) *func = name; static typeof(name) *func = name;
// Helper Macros // Arguemnts
#define next_int() args[_current_arg++] struct TrampolineArguments {
#define next_ptr() g2h(next_int()) TrampolineArguments(const unsigned char *args) {
#define next_float() pun_to(float, next_int()) this->raw_args = args;
#define ret(x) \ this->position = 0;
args[0] = x; \ }
return;
template <typename T>
T next() {
block_pointer(T);
T ret = *(const T *) raw_args;
raw_args += sizeof(T);
return ret;
}
template <typename T>
const T *next_arr(uint32_t *length = nullptr) {
block_pointer(T);
const uint32_t size = next<uint32_t>();
if (length != nullptr) {
*length = size / sizeof(T);
}
const T *ret = (const T *) raw_args;
raw_args += size;
return ret;
}
private:
const unsigned char *raw_args;
unsigned int position;
};

View File

@ -1,53 +1,53 @@
#include <stdint.h> #include <cstdint>
#include <SDL/SDL.h> #include <SDL/SDL.h>
#include <libreborn/libreborn.h>
#include <media-layer/core.h> #include <media-layer/core.h>
#include <media-layer/audio.h> #include <media-layer/audio.h>
#include "common/common.h" #include "common/common.h"
#include <libreborn/libreborn.h>
// SDL Functions // SDL Functions
CALL(0, SDL_Init, int, (uint32_t flags)) CALL(0, SDL_Init, int, (uint32_t flags))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST #ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
return trampoline(flags); return trampoline(flags);
#else #else
uint32_t flags = next_int(); uint32_t flags = args.next<uint32_t>();
// Run return func(flags);
ret(func(flags));
#endif #endif
} }
CALL(1, SDL_PollEvent, int, (SDL_Event *event)) CALL(1, SDL_PollEvent, int, (SDL_Event *event))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST #ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
return trampoline((uint32_t) event); return trampoline(uint32_t(event));
#else #else
SDL_Event *event = next_ptr(); SDL_Event event;
// Run const int ret = func(&event);
ret(func(event)); writer(args.next<uint32_t>(), &event, sizeof(SDL_Event));
return ret;
#endif #endif
} }
CALL(2, SDL_PushEvent, int, (SDL_Event *event)) CALL(2, SDL_PushEvent, int, (SDL_Event *event))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST #ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
return trampoline((uint32_t) event); return trampoline(*event);
#else #else
SDL_Event *event = next_ptr(); SDL_Event event = args.next<SDL_Event>();
// Run return func(&event);
ret(func(event));
#endif #endif
} }
CALL(3, SDL_WM_SetCaption, void, (const char *title, const char *icon)) CALL(3, SDL_WM_SetCaption, void, (const char *title, const char *icon))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST #ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline((uint32_t) title, (uint32_t) icon); trampoline(copy_array(title), copy_array(icon));
#else #else
char *title = next_ptr(); const char *title = args.next_arr<char>();
char *icon = next_ptr(); const char *icon = args.next_arr<char>();
// Run
func(title, icon); func(title, icon);
return 0;
#endif #endif
} }
@ -55,18 +55,16 @@ CALL(4, media_toggle_fullscreen, void, ())
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST #ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(); trampoline();
#else #else
// Run
func(); func();
return 0;
#endif #endif
} }
CALL(5, SDL_WM_GrabInput, SDL_GrabMode, (SDL_GrabMode mode)) CALL(5, SDL_WM_GrabInput, SDL_GrabMode, (SDL_GrabMode mode))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST #ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
return trampoline(mode); return (SDL_GrabMode) trampoline(mode);
#else #else
SDL_GrabMode mode = next_int(); return func(args.next<SDL_GrabMode>());
// Run
ret(func(mode));
#endif #endif
} }
@ -74,9 +72,7 @@ CALL(6, SDL_ShowCursor, int, (int32_t toggle))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST #ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
return trampoline(toggle); return trampoline(toggle);
#else #else
int mode = next_int(); return func(args.next<int32_t>());
// Run
ret(func(mode));
#endif #endif
} }
@ -84,8 +80,8 @@ CALL(8, media_swap_buffers, void, ())
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST #ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(); trampoline();
#else #else
// Run
func(); func();
return 0;
#endif #endif
} }
@ -93,50 +89,52 @@ CALL(9, media_cleanup, void, ())
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST #ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(); trampoline();
#else #else
// Run
func(); func();
return 0;
#endif #endif
} }
CALL(10, media_get_framebuffer_size, void, (int *width, int *height)) CALL(10, media_get_framebuffer_size, void, (int *width, int *height))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST #ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline((uint32_t) width, (uint32_t) height); trampoline(uint32_t(width), uint32_t(height));
#else #else
int *width = next_ptr(); int width;
int *height = next_ptr(); int height;
// Run func(&width, &height);
func(width, height); writer(args.next<uint32_t>(), &width, sizeof(int));
writer(args.next<uint32_t>(), &height, sizeof(int));
return 0;
#endif #endif
} }
CALL(59, media_audio_update, void, (float volume, float x, float y, float z, float yaw)) CALL(59, media_audio_update, void, (float volume, float x, float y, float z, float yaw))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST #ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(pun_to(uint32_t, volume), pun_to(uint32_t, x), pun_to(uint32_t, y), pun_to(uint32_t, z), pun_to(uint32_t, yaw)); trampoline(volume, x, y, z, yaw);
#else #else
float volume = next_float(); float volume = args.next<float>();
float x = next_float(); float x = args.next<float>();
float y = next_float(); float y = args.next<float>();
float z = next_float(); float z = args.next<float>();
float yaw = next_float(); float yaw = args.next<float>();
// Run
func(volume, x, y, z, yaw); func(volume, x, y, z, yaw);
return 0;
#endif #endif
} }
CALL(60, media_audio_play, void, (const char *source, const char *name, float x, float y, float z, float pitch, float volume, int is_ui)) CALL(60, media_audio_play, void, (const char *source, const char *name, float x, float y, float z, float pitch, float volume, int is_ui))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST #ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline((uint32_t) source, (uint32_t) name, pun_to(uint32_t, x), pun_to(uint32_t, y), pun_to(uint32_t, z), pun_to(uint32_t, pitch), pun_to(uint32_t, volume), is_ui); trampoline(copy_array(source), copy_array(name), x, y, z, pitch, volume, is_ui);
#else #else
char *source = next_ptr(); const char *source = args.next_arr<char>();
char *name = next_ptr(); const char *name = args.next_arr<char>();
float x = next_float(); float x = args.next<float>();
float y = next_float(); float y = args.next<float>();
float z = next_float(); float z = args.next<float>();
float pitch = next_float(); float pitch = args.next<float>();
float volume = next_float(); float volume = args.next<float>();
int is_ui = next_int(); int is_ui = args.next<int>();
// Run
func(source, name, x, y, z, pitch, volume, is_ui); func(source, name, x, y, z, pitch, volume, is_ui);
return 0;
#endif #endif
} }
@ -144,9 +142,8 @@ CALL(62, media_set_interactable, void, (int is_interactable))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST #ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(is_interactable); trampoline(is_interactable);
#else #else
int is_interactable = next_int(); func(args.next<int>());
// Run return 0;
func(is_interactable);
#endif #endif
} }
@ -154,8 +151,8 @@ CALL(63, media_disable_vsync, void, ())
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST #ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(); trampoline();
#else #else
// Run
func(); func();
return 0;
#endif #endif
} }
@ -163,9 +160,8 @@ CALL(64, media_set_raw_mouse_motion_enabled, void, (int enabled))
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST #ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(enabled); trampoline(enabled);
#else #else
int enabled = next_int(); func(args.next<int>());
// Run return 0;
func(enabled);
#endif #endif
} }
@ -173,8 +169,8 @@ CALL(66, media_force_egl, void, ())
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST #ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(); trampoline();
#else #else
// Run
func(); func();
return 0;
#endif #endif
} }
@ -182,7 +178,7 @@ CALL(68, media_ensure_loaded, void, ())
#ifdef MEDIA_LAYER_TRAMPOLINE_GUEST #ifdef MEDIA_LAYER_TRAMPOLINE_GUEST
trampoline(); trampoline();
#else #else
// Run
func(); func();
return 0;
#endif #endif
} }