Skip to content
Closed
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)
Comment thread
aurel32 marked this conversation as resolved.
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
54 changes: 54 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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We cannot use extensions like __has_include generally as it requires C23 (and mimalloc assumes C11). I think we should use the cmake CHECK_INCLUDE_FILES and pass an extra define MI_HAS_ASM_HWPROBE_H such we can test in the sources for it.

(mmm, but maybe riscV is so recent that we can assume that all C compilers on riscV support __has_include ? ... maybe we can make an exception in that case as __has_include is quite clean?)

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 have seen it used in an other place so I assumed it was fine, but I realized now that it was guarded by defined(__APPLE__). I'll fix that as you suggested

#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,52 @@ 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)
{
#if defined(__riscv) || defined(_M_RISCV)
#ifdef RISCV_HWPROBE_KEY_HIGHEST_VIRT_ADDRESS
struct riscv_hwprobe probe = {
.key = RISCV_HWPROBE_KEY_HIGHEST_VIRT_ADDRESS,
Comment thread
aurel32 marked this conversation as resolved.
};

// 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

// Fallback to checking /proc/cpuinfo for older kernels
int fd = mi_prim_open("/proc/cpuinfo", O_RDONLY);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

use const int fd

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.

ok, i'll fix that

if (fd >= 0) {
char buf[4096];

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

That is a large buffer on the stack; maybe 1024 bytes is enough to find if svXX is there?

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.

That works fine for now on a recent CPU, but that might be tight if more extensions are added. Here is for instance the first 1024 bytes on a SpacemiT K3:

processor       : 0
hart            : 0
isa             : rv64imafdcvh_zicbom_zicbop_zicboz_ziccrse_zicntr_zicond_zicsr_zifencei_zihintntl_zihintpause_zihpm_zimop_zaamo_zalrsc_zawrs_zfa_zfbfmin_zfh_zfhmin_zca_zcb_zcd_zcmop_zba_zbb_zbc_zbs_zkt_zvbb_zvbc_zve32f_zve32x_zve64d_zve64f_zve64x_zvfbfmin_zvfbfwma_zvfh_zvfhmin_zvkb_zvkg_zvkned_zvknha_zvknhb_zvksed_zvksh_zvkt_smaia_smstateen_ssaia_sscofpmf_ssnpm_sstc_svade_svinval_svnapot_svpbmt
mmu             : sv39
uarch           : spacemit,x100
mvendorid       : 0x710
marchid         : 0x8000000058000002
mimpid          : 0x33d8a600
hart isa        : rv64imafdcvh_zicbom_zicbop_zicboz_ziccrse_zicntr_zicond_zicsr_zifencei_zihintntl_zihintpause_zihpm_zimop_zaamo_zalrsc_zawrs_zfa_zfbfmin_zfh_zfhmin_zca_zcb_zcd_zcmop_zba_zbb_zbc_zbs_zkt_zvbb_zvbc_zve32f_zve32x_zve64d_zve64f_zve64x_zvfbfmin_zvfbfwma_zvfh_zvfhmin_zvkb_zvkg_zvkned_zvknha_zvknhb_zvksed_zvksh_zvkt_smaia_smstateen_ssaia_sscofpmf_ssnpm_sstc_svade_svinval_svnapot_svpbmt

processor       : 1
hart            : 1

Would that be acceptable to go down to 2048 bytes instead?

ssize_t nread = mi_prim_read(fd, &buf, sizeof(buf));
mi_prim_close(fd);
if (nread >= 1) {
if (_mi_strnstr(buf, nread, "sv39")) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe we should also test if nread <= sizeof(buf) just in case.

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.

Thanks, i'll fix that

return 39;
}
if (_mi_strnstr(buf, nread, "sv48")) {
return 48;
}
if (_mi_strnstr(buf, nread, "sv57")) {
return 57;
}
}
}
#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 +252,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