Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -422,16 +422,6 @@ else()
# list(APPEND mi_defines MI_WIN_INIT_USE_CRT_TLS=1)
endif()

# Check /proc/cpuinfo for an SV39 MMU and limit the virtual address bits.
# (this will skip the aligned hinting in that case. Issue #939, #949)
if (EXISTS /proc/cpuinfo)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps turn that into an explicit build time option? That way, people targeting an SV39 can at least explicitly request an appropriate number of bits.
(TBF, I have no idea how large that group may be.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... targeting an SV39 and an older kernel.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not a big fan of configure options like that, because people don't really look at it until things are broken. I prefer when things work out of the box, but I understand your concern about supporting older kernels. What about adding a fallback parsing /proc/cpuinfo at runtime for older kernels?

file(STRINGS /proc/cpuinfo mi_sv39_mmu REGEX "^mmu[ \t]+:[ \t]+sv39$")
if (mi_sv39_mmu)
MESSAGE( STATUS "Set virtual address bits to 39 (SV39 MMU detected)" )
list(APPEND mi_defines MI_DEFAULT_VIRTUAL_ADDRESS_BITS=39)
endif()
endif()

# On Haiku use `-DCMAKE_INSTALL_PREFIX` instead, issue #788
# if(CMAKE_SYSTEM_NAME MATCHES "Haiku")
# SET(CMAKE_INSTALL_LIBDIR ~/config/non-packaged/lib)
Expand Down
33 changes: 33 additions & 0 deletions src/prim/unix/prim.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ terms of the MIT license. A copy of the license can be found in the file
#else
#include <sys/mman.h>
#endif
#if defined(__riscv) || defined(_M_RISCV)
#if __has_include(<sys/hwprobe.h>)
#include <sys/hwprobe.h>
#elif __has_include(<asm/hwprobe.h>)
#include <asm/hwprobe.h>
#endif
#endif
#elif defined(__APPLE__)
#include <AvailabilityMacros.h>
#include <TargetConditionals.h>
Expand Down Expand Up @@ -187,6 +194,31 @@ static void unix_detect_physical_memory( size_t page_size, size_t* physical_memo
#endif
}

// Detect the virtual address bits (currently Linux/RISC-V only)
static size_t unix_detect_virtual_address_bits(void)
{
#ifdef RISCV_HWPROBE_KEY_HIGHEST_VIRT_ADDRESS
struct riscv_hwprobe probe = {
.key = RISCV_HWPROBE_KEY_HIGHEST_VIRT_ADDRESS,
};

// Prefer the GNU libc interface if available, as it can also use the VDSO
#if __has_include(<sys/hwprobe.h>)
if (__riscv_hwprobe(&probe, 1, 0, NULL, 0) == 0) {
#else
if (syscall(__NR_riscv_hwprobe, &probe, 1, 0, NULL, 0) == 0) {
#endif
// If a key is unknown to the kernel, its key field will be cleared to -1.
if (probe.key != -1) {
return MI_SIZE_BITS - mi_clz((uintptr_t)probe.value);
}
}
#endif

// default: MI_MAX_VABITS
return MI_MAX_VABITS;
}

void _mi_prim_mem_init( mi_os_mem_config_t* config )
{
long psize = sysconf(_SC_PAGESIZE);
Expand All @@ -199,6 +231,7 @@ void _mi_prim_mem_init( mi_os_mem_config_t* config )
config->has_overcommit = unix_detect_overcommit();
config->has_partial_free = true; // mmap can free in parts
config->has_virtual_reserve = true; // todo: check if this true for NetBSD? (for anonymous mmap with PROT_NONE)
config->virtual_address_bits = unix_detect_virtual_address_bits();

// disable transparent huge pages for this process?
#if (defined(__linux__) || defined(__ANDROID__)) && defined(PR_GET_THP_DISABLE)
Expand Down