Skip to content

Commit

Permalink
Merge pull request #1590 from Sonicadvance1/termux_fixes
Browse files Browse the repository at this point in the history
Termux fixes
  • Loading branch information
Sonicadvance1 authored Mar 1, 2022
2 parents 2933a00 + 5137af5 commit b4e0565
Show file tree
Hide file tree
Showing 39 changed files with 378 additions and 249 deletions.
59 changes: 59 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,65 @@ if (NOT ENABLE_OFFLINE_TELEMETRY)
add_definitions(-DFEX_DISABLE_TELEMETRY=1)
endif()

# Check if the build target page size is 4096
include(CheckCSourceRuns)

check_c_source_runs(
"#include <unistd.h>
int main(int argc, char* argv[])
{
return getpagesize() == 4096 ? 0 : 1;
}"
PAGEFILE_RESULT
)

if (NOT ${PAGEFILE_RESULT})
message(FATAL_ERROR "Host PAGE_SIZE is not 4096. Can't build on this target")
endif()

include(CheckCXXSourceCompiles)
check_cxx_source_compiles(
"#include <sys/user.h>
int main() {
return PAGE_SIZE;
}
"
HAS_PAGESIZE)

check_cxx_source_compiles(
"#include <sys/user.h>
int main() {
return PAGE_SHIFT;
}
"
HAS_PAGESHIFT)

check_cxx_source_compiles(
"#include <sys/user.h>
int main() {
return PAGE_MASK;
}
"
HAS_PAGEMASK)

if (NOT HAS_PAGESIZE)
add_definitions(-DPAGE_SIZE=4096)
endif()

if (NOT HAS_PAGESHIFT)
add_definitions(-DPAGE_SHIFT=12)
endif()
if (NOT HAS_PAGEMASK)
add_definitions("-DPAGE_MASK=(~(PAGE_SIZE-1))")
endif()

if(DEFINED ENV{TERMUX_VERSION})
add_definitions(-DTERMUX_BUILD=1)
set(TERMUX_BUILD 1)
# Termux doesn't support Jemalloc due to bad interactions between emutls, jemalloc, and scudo
set(ENABLE_JEMALLOC FALSE)
endif()

if (ENABLE_STATIC_PIE)
if (_M_ARM_64 AND ENABLE_LLD)
message (FATAL_ERROR "Static linking does not currently work with AArch64+LLD. Use GNU ld for now.")
Expand Down
1 change: 1 addition & 0 deletions External/FEXCore/Source/Interface/Core/CPUID.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cstdint>
#include <unordered_map>
#include <utility>
#include <vector>

#include <FEXCore/Core/CPUID.h>
#include <FEXCore/Config/Config.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tags: ir|opts
#include <strings.h>
#include <unordered_map>
#include <unordered_set>
#include <sys/user.h>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -61,7 +62,7 @@ namespace {
};

static_assert(sizeof(RegisterNode) == 128 * 4);
constexpr size_t REGISTER_NODES_PER_PAGE = FEXCore::Core::PAGE_SIZE / sizeof(RegisterNode);
constexpr size_t REGISTER_NODES_PER_PAGE = PAGE_SIZE / sizeof(RegisterNode);

struct RegisterSet {
std::vector<RegisterClass> Classes;
Expand Down
1 change: 1 addition & 0 deletions External/FEXCore/Source/Utils/Allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <array>
#include <sys/mman.h>
#include <sys/user.h>
#ifdef ENABLE_JEMALLOC
#include <jemalloc/jemalloc.h>
#endif
Expand Down
18 changes: 8 additions & 10 deletions External/FEXCore/Source/Utils/Allocator/64BitAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
#include <sstream>
#include <sys/mman.h>
#include <sys/utsname.h>
#include <sys/user.h>
#include <type_traits>
#include <utility>

static constexpr uint64_t PAGE_SHIFT = 12;
static constexpr uint64_t PAGE_MASK = (1 << PAGE_SHIFT) - 1;

namespace Alloc::OSAllocator {

class OSAllocator_64Bit final : public Alloc::HostAllocator {
public:
OSAllocator_64Bit();
Expand All @@ -38,7 +37,6 @@ namespace Alloc::OSAllocator {
int Munmap(void *addr, size_t length) override;

private:
constexpr static uint64_t PAGE_SIZE = 4096;
// Upper bound is the maximum virtual address space of the host processor
uintptr_t UPPER_BOUND = (1ULL << 57);

Expand Down Expand Up @@ -107,8 +105,8 @@ namespace Alloc::OSAllocator {
static_assert(std::is_trivially_copyable<LiveVMARegion>::value, "Needs to be trivially copyable");
static_assert(offsetof(LiveVMARegion, UsedPages) == sizeof(LiveVMARegion), "FlexBitSet needs to be at the end");

using ReservedRegionListType = std::pmr::list<ReservedVMARegion*>;
using LiveRegionListType = std::pmr::list<LiveVMARegion*>;
using ReservedRegionListType = fex_pmr::list<ReservedVMARegion*>;
using LiveRegionListType = fex_pmr::list<LiveVMARegion*>;
ReservedRegionListType *ReservedRegions{};
LiveRegionListType *LiveRegions{};

Expand Down Expand Up @@ -162,13 +160,13 @@ void *OSAllocator_64Bit::Mmap(void *addr, size_t length, int prot, int flags, in

uint64_t Addr = reinterpret_cast<uint64_t>(addr);
// Addr must be page aligned
if (Addr & PAGE_MASK) {
if (Addr & ~PAGE_MASK) {
return reinterpret_cast<void*>(-EINVAL);
}

// If FD is provided then offset must also be page aligned
if (fd != -1 &&
offset & PAGE_MASK) {
offset & ~PAGE_MASK) {
return reinterpret_cast<void*>(-EINVAL);
}

Expand Down Expand Up @@ -430,11 +428,11 @@ int OSAllocator_64Bit::Munmap(void *addr, size_t length) {

uint64_t Addr = reinterpret_cast<uint64_t>(addr);

if (Addr & PAGE_MASK) {
if (Addr & ~PAGE_MASK) {
return -EINVAL;
}

if (length & PAGE_MASK) {
if (length & ~PAGE_MASK) {
return -EINVAL;
}

Expand Down
2 changes: 0 additions & 2 deletions External/FEXCore/Source/Utils/Allocator/HostAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include <memory>
#include <sys/types.h>

constexpr static uint64_t PAGE_SIZE = 4096;

namespace Alloc {
// HostAllocator is just a page pased slab allocator
// Similar to mmap and munmap only mapping at the page level
Expand Down
22 changes: 18 additions & 4 deletions External/FEXCore/Source/Utils/Allocator/IntrusiveArenaAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,26 @@

#include <bitset>
#include <cstddef>
#ifdef TERMUX_BUILD
#ifdef __has_include
#if __has_include(<memory_resource>)
#error Termux <experimental/memory_resource> workaround can be removed
#endif
#endif
#include <experimental/memory_resource>
#include <experimental/list>
namespace fex_pmr = std::experimental::pmr;
#else
#include <memory_resource>
namespace fex_pmr = std::pmr;
#endif
#include <sys/user.h>

#include <mutex>
#include <vector>

namespace Alloc {
class ForwardOnlyIntrusiveArenaAllocator final : public std::pmr::memory_resource {
class ForwardOnlyIntrusiveArenaAllocator final : public fex_pmr::memory_resource {
public:
ForwardOnlyIntrusiveArenaAllocator(void* Ptr, size_t _Size)
: Begin {reinterpret_cast<uintptr_t>(Ptr)}
Expand Down Expand Up @@ -54,7 +68,7 @@ namespace Alloc {
// Do nothing
}

bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {
bool do_is_equal(const fex_pmr::memory_resource& other) const noexcept override {
// Only if the allocator pointers are the same are they equal
if (this == &other) {
return true;
Expand All @@ -68,7 +82,7 @@ namespace Alloc {
size_t LastAllocation{};
};

class IntrusiveArenaAllocator final : public std::pmr::memory_resource {
class IntrusiveArenaAllocator final : public fex_pmr::memory_resource {
public:
IntrusiveArenaAllocator(void* Ptr, size_t _Size)
: Begin {reinterpret_cast<uintptr_t>(Ptr)}
Expand Down Expand Up @@ -167,7 +181,7 @@ namespace Alloc {
FreePages += FreedPages;
}

bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {
bool do_is_equal(const fex_pmr::memory_resource& other) const noexcept override {
// Only if the allocator pointers are the same are they equal
if (this == &other) {
return true;
Expand Down
7 changes: 0 additions & 7 deletions External/FEXCore/include/FEXCore/Core/CoreState.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,6 @@ namespace FEXCore::Core {

static_assert(std::is_standard_layout<CpuStateFrame>::value, "This needs to be standard layout");

#ifdef PAGE_SIZE
static_assert(PAGE_SIZE == 4096, "FEX only supports 4k pages");
#undef PAGE_SIZE
#endif

constexpr uint64_t PAGE_SIZE = 4096;

FEX_DEFAULT_VISIBILITY std::string_view const& GetFlagName(unsigned Flag);
FEX_DEFAULT_VISIBILITY std::string_view const& GetGRegName(unsigned Reg);
}
2 changes: 1 addition & 1 deletion External/imgui
3 changes: 3 additions & 0 deletions Source/Common/SocketLogging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
#include <FEXHeaderUtils/Syscalls.h>

#include <atomic>
#include <byteswap.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <poll.h>
#include <sstream>
#include <string>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include <vector>

// For older build environments
#ifndef POLLREMOVE
Expand Down
38 changes: 27 additions & 11 deletions Source/Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,34 @@ install(TARGETS FEXLoader
COMPONENT runtime
)

add_custom_target(FEXInterpreter ALL
COMMAND "ln" "-f" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/FEXLoader" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/FEXInterpreter"
DEPENDS FEXLoader
)
if(TERMUX_BUILD)
# Termux doesn't support hard links, just copy FEXLoader
add_custom_target(FEXInterpreter ALL
COMMAND "cp" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/FEXLoader" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/FEXInterpreter"
DEPENDS FEXLoader
)

install(
CODE "MESSAGE(\"-- Installing: ${CMAKE_INSTALL_PREFIX}/bin/FEXInterpreter\")"
CODE "
EXECUTE_PROCESS(COMMAND ln -f FEXLoader FEXInterpreter
WORKING_DIRECTORY ${CMAKE_INSTALL_PREFIX}/bin/
)"
)
install(
CODE "MESSAGE(\"-- Installing: ${CMAKE_INSTALL_PREFIX}/bin/FEXInterpreter\")"
CODE "
EXECUTE_PROCESS(COMMAND cp FEXLoader FEXInterpreter
WORKING_DIRECTORY ${CMAKE_INSTALL_PREFIX}/bin/
)"
)
else()
add_custom_target(FEXInterpreter ALL
COMMAND "ln" "-f" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/FEXLoader" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/FEXInterpreter"
DEPENDS FEXLoader
)

install(
CODE "MESSAGE(\"-- Installing: ${CMAKE_INSTALL_PREFIX}/bin/FEXInterpreter\")"
CODE "
EXECUTE_PROCESS(COMMAND ln -f FEXLoader FEXInterpreter
WORKING_DIRECTORY ${CMAKE_INSTALL_PREFIX}/bin/
)"
)
endif()

install(PROGRAMS "${PROJECT_SOURCE_DIR}/Scripts/FEXUpdateAOTIRCache.sh" DESTINATION bin RENAME FEXUpdateAOTIRCache)

Expand Down
8 changes: 1 addition & 7 deletions Source/Tests/HarnessHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <cstring>
#include <fstream>
#include <sys/mman.h>
#include <sys/user.h>
#include <vector>

#include <FEXCore/Core/CodeLoader.h>
Expand Down Expand Up @@ -357,14 +358,7 @@ namespace FEX::HarnessHelper {
ConfigStructBase BaseConfig;
};

#ifdef PAGE_SIZE
static_assert(PAGE_SIZE == 4096, "FEX only supports 4k pages");
#undef PAGE_SIZE
#endif

class HarnessCodeLoader final : public FEXCore::CodeLoader {

static constexpr uint32_t PAGE_SIZE = 4096;
public:

HarnessCodeLoader(std::string const &Filename, const char *ConfigFilename) {
Expand Down
1 change: 1 addition & 0 deletions Source/Tests/LinuxSyscalls/FileManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ desc: Rootfs overlay logic
#include <filesystem>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <syscall.h>
Expand Down
1 change: 1 addition & 0 deletions Source/Tests/LinuxSyscalls/FileManagement.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tags: LinuxSyscalls|common
#include <stddef.h>
#include <string>
#include <sys/stat.h>
#include <vector>

#include <unordered_map>
#include <unordered_set>
Expand Down
12 changes: 5 additions & 7 deletions Source/Tests/LinuxSyscalls/LinuxAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <map>
#include <linux/mman.h>
#include <unistd.h>
#include <sys/user.h>
#include <sys/mman.h>
#include <sys/shm.h>

Expand All @@ -18,9 +19,6 @@
namespace FEX::HLE {
class MemAllocator32Bit final : public FEX::HLE::MemAllocator {
private:
static constexpr uint64_t PAGE_SHIFT = 12;
static constexpr uint64_t PAGE_SIZE = 1 << PAGE_SHIFT;
static constexpr uint64_t PAGE_MASK = (1 << PAGE_SHIFT) - 1;
static constexpr uint64_t BASE_KEY = 16;
const uint64_t TOP_KEY = 0xFFFF'F000ULL >> PAGE_SHIFT;
const uint64_t TOP_KEY32BIT = 0x1F'F000ULL >> PAGE_SHIFT;
Expand Down Expand Up @@ -143,13 +141,13 @@ void *MemAllocator32Bit::mmap(void *addr, size_t length, int prot, int flags, in
(flags & MAP_FIXED_NOREPLACE));

// Both Addr and length must be page aligned
if (Addr & PAGE_MASK) {
if (Addr & ~PAGE_MASK) {
return reinterpret_cast<void*>(-EINVAL);
}

// If we do have an fd then offset must be page aligned
if (fd != -1 &&
offset & PAGE_MASK) {
offset & ~PAGE_MASK) {
return reinterpret_cast<void*>(-EINVAL);
}

Expand Down Expand Up @@ -275,11 +273,11 @@ int MemAllocator32Bit::munmap(void *addr, size_t length) {
uintptr_t PageEnd = PageAddr + PagesLength;

// Both Addr and length must be page aligned
if (Addr & PAGE_MASK) {
if (Addr & ~PAGE_MASK) {
return -EINVAL;
}

if (length & PAGE_MASK) {
if (length & ~PAGE_MASK) {
return -EINVAL;
}

Expand Down
Loading

0 comments on commit b4e0565

Please sign in to comment.