From 98845aa2647a72af2b142c40360fae9c081311a2 Mon Sep 17 00:00:00 2001 From: Fabien Poussin Date: Fri, 20 Nov 2026 16:43:25 +0100 Subject: [PATCH 1/3] 1.1.0: sys/mman.h for JIT compilers; no aborting __clear_cache Emulators with a dynamic recompiler (PCSX ReARMed's ARM dynarec) need anonymous mmap, mprotect with PROT_EXEC and __clear_cache. The host ABI already has the page calls and the instruction cache flush; the SDK's module runtime now implements the C functions over them. - libc/include/sys/mman.h: PROT_*, MAP_SHARED/PRIVATE/FIXED/ANONYMOUS, MAP_FAILED, mmap, munmap, mprotect. - compiler-rt's bare-metal __clear_cache aborts at its first use: it is removed from the builtins, so the SDK runtime's one is the only one and code linked without it fails at link time. --- VERSION | 2 +- docs/usage.md | 6 ++++-- libc/include/sys/mman.h | 38 ++++++++++++++++++++++++++++++++++++++ scripts/build-runtime.sh | 4 ++++ 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 libc/include/sys/mman.h diff --git a/VERSION b/VERSION index 3eefcb9..9084fa2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.0.0 +1.1.0 diff --git a/docs/usage.md b/docs/usage.md index baddb8a..200e858 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -15,9 +15,11 @@ Built by NBTFoundry from LLVM and picolibc sources; not an LLVM release. `-objcopy`, `-strip`, `-objdump`, `-readobj`, `-readelf`, `-size`, `-symbolizer`, `-addr2line`, `-cxxfilt`. Their only target is ARM. - `armv7a-nbt/include/`: the NBT C library headers (threads, semaphores, the - bridge's sockets, directories), implemented by the SDK's module runtime. + bridge's sockets, directories, anonymous and executable memory mappings), + implemented by the SDK's module runtime. - `armv7a-nbt/aligned/` and `armv7a-nbt/unaligned/`: picolibc, compiler-rt - builtins (with emulated TLS) and libc++, libc++abi and libunwind, built with + builtins (with emulated TLS; `__clear_cache` comes from the SDK's module + runtime) and libc++, libc++abi and libunwind, built with strict alignment or allowing unaligned accesses. Each `target.mk` holds the flags code linking that runtime must be compiled with. - `share/licenses/`: the licences of everything above. diff --git a/libc/include/sys/mman.h b/libc/include/sys/mman.h new file mode 100644 index 0000000..4be72ee --- /dev/null +++ b/libc/include/sys/mman.h @@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: MPL-2.0 */ +/* Memory mappings for NBT app modules, as JIT compilers use them: anonymous + * private mappings, protection changes (executable pages included) and, from + * the compiler, __clear_cache. The SDK's module runtime implements them over + * the host ABI (sdk/module/runtime/mman.c). There are no file mappings: mmap + * with a descriptor fails with ENODEV, MAP_FIXED with EINVAL, and an address + * hint is ignored. */ +#ifndef NBT_MODULE_SYS_MMAN_H +#define NBT_MODULE_SYS_MMAN_H + +#include /* off_t, size_t */ + +#ifdef __cplusplus +extern "C" { +#endif + +#define PROT_NONE 0x0 +#define PROT_READ 0x1 +#define PROT_WRITE 0x2 +#define PROT_EXEC 0x4 + +#define MAP_SHARED 0x01 +#define MAP_PRIVATE 0x02 +#define MAP_FIXED 0x10 +#define MAP_ANONYMOUS 0x20 +#define MAP_ANON MAP_ANONYMOUS + +#define MAP_FAILED ((void*)-1) + +void* mmap(void* addr, size_t len, int prot, int flags, int fd, off_t offset); +int munmap(void* addr, size_t len); +int mprotect(void* addr, size_t len, int prot); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/scripts/build-runtime.sh b/scripts/build-runtime.sh index 2e115b8..76a1092 100755 --- a/scripts/build-runtime.sh +++ b/scripts/build-runtime.sh @@ -108,6 +108,10 @@ EOF "$LLVM_BIN/clang" --target=$TRIPLE $FLAGS $DEFS $LIBC_INC -O2 -std=gnu11 -fvisibility=hidden -I"$SRC/compiler-rt/lib/builtins" \ -c "$SRC/compiler-rt/lib/builtins/emutls.c" -o "$W/emutls.o" "$LLVM_BIN/llvm-ar" rs "$V/builtins/libclang_rt.builtins.a" "$W/emutls.o" + # A bare-metal __clear_cache aborts: the module runtime has the real one + # (the host's instruction cache flush), so code linked without it fails to + # link instead of aborting when it first flushes. + "$LLVM_BIN/llvm-ar" d "$V/builtins/libclang_rt.builtins.a" clear_cache.c.obj # --- libc++, libc++abi, libunwind --- # Static, over picolibc (which libc++ treats as newlib) and the NBT From a772a6725dec07c9a81d30729a058843df37a9ab Mon Sep 17 00:00:00 2001 From: Fabien Poussin Date: Fri, 20 Nov 2026 17:04:59 +0100 Subject: [PATCH 2/3] pthread.h scheduling attributes, asm/hwcap.h Declarations portable code needs to compile against the NBT C library: pthread_attr_set/get of schedpolicy, schedparam and inheritsched (PTHREAD_INHERIT_SCHED, PTHREAD_EXPLICIT_SCHED), and Linux's ARM HWCAP/HWCAP2 bits for getauxval, which picolibc declares. The SDK's module runtime implements them: attributes unused, no optional CPU feature reported. --- docs/usage.md | 4 ++-- libc/include/asm/hwcap.h | 39 +++++++++++++++++++++++++++++++++++++++ libc/include/pthread.h | 12 ++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 libc/include/asm/hwcap.h diff --git a/docs/usage.md b/docs/usage.md index 200e858..c49eb95 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -15,8 +15,8 @@ Built by NBTFoundry from LLVM and picolibc sources; not an LLVM release. `-objcopy`, `-strip`, `-objdump`, `-readobj`, `-readelf`, `-size`, `-symbolizer`, `-addr2line`, `-cxxfilt`. Their only target is ARM. - `armv7a-nbt/include/`: the NBT C library headers (threads, semaphores, the - bridge's sockets, directories, anonymous and executable memory mappings), - implemented by the SDK's module runtime. + bridge's sockets, directories, anonymous and executable memory mappings, + ARM CPU feature bits), implemented by the SDK's module runtime. - `armv7a-nbt/aligned/` and `armv7a-nbt/unaligned/`: picolibc, compiler-rt builtins (with emulated TLS; `__clear_cache` comes from the SDK's module runtime) and libc++, libc++abi and libunwind, built with diff --git a/libc/include/asm/hwcap.h b/libc/include/asm/hwcap.h new file mode 100644 index 0000000..ba61975 --- /dev/null +++ b/libc/include/asm/hwcap.h @@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: MPL-2.0 */ +/* ARM CPU feature bits, as Linux numbers them for getauxval(AT_HWCAP) and + * getauxval(AT_HWCAP2) (), so portable feature probes compile. + * The SDK's module runtime reports none of these optional features: code + * takes its baseline paths (ARMv7-A, VFPv3-D16, no NEON). */ +#ifndef NBT_MODULE_ASM_HWCAP_H +#define NBT_MODULE_ASM_HWCAP_H + +#define HWCAP_SWP (1 << 0) +#define HWCAP_HALF (1 << 1) +#define HWCAP_THUMB (1 << 2) +#define HWCAP_26BIT (1 << 3) +#define HWCAP_FAST_MULT (1 << 4) +#define HWCAP_FPA (1 << 5) +#define HWCAP_VFP (1 << 6) +#define HWCAP_EDSP (1 << 7) +#define HWCAP_JAVA (1 << 8) +#define HWCAP_IWMMXT (1 << 9) +#define HWCAP_CRUNCH (1 << 10) +#define HWCAP_THUMBEE (1 << 11) +#define HWCAP_NEON (1 << 12) +#define HWCAP_VFPv3 (1 << 13) +#define HWCAP_VFPv3D16 (1 << 14) +#define HWCAP_TLS (1 << 15) +#define HWCAP_VFPv4 (1 << 16) +#define HWCAP_IDIVA (1 << 17) +#define HWCAP_IDIVT (1 << 18) +#define HWCAP_VFPD32 (1 << 19) +#define HWCAP_IDIV (HWCAP_IDIVA | HWCAP_IDIVT) +#define HWCAP_LPAE (1 << 20) +#define HWCAP_EVTSTRM (1 << 21) + +#define HWCAP2_AES (1 << 0) +#define HWCAP2_PMULL (1 << 1) +#define HWCAP2_SHA1 (1 << 2) +#define HWCAP2_SHA2 (1 << 3) +#define HWCAP2_CRC32 (1 << 4) + +#endif diff --git a/libc/include/pthread.h b/libc/include/pthread.h index e08e0b2..3add4a1 100644 --- a/libc/include/pthread.h +++ b/libc/include/pthread.h @@ -56,6 +56,12 @@ typedef struct { #define PTHREAD_CREATE_JOINABLE 0 #define PTHREAD_CREATE_DETACHED 1 +/* Threads take the application's scheduling: the policy and priority + * attributes are accepted and, as POSIX has it under PTHREAD_INHERIT_SCHED, + * unused; PTHREAD_EXPLICIT_SCHED is refused (ENOTSUP). */ +#define PTHREAD_INHERIT_SCHED 0 +#define PTHREAD_EXPLICIT_SCHED 1 + #define PTHREAD_MUTEX_INITIALIZER {0, PTHREAD_MUTEX_NORMAL, 0, 0} #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP {0, PTHREAD_MUTEX_RECURSIVE, 0, 0} #define PTHREAD_COND_INITIALIZER {0, 0} @@ -70,6 +76,12 @@ int pthread_attr_setstacksize(pthread_attr_t* attr, size_t size); int pthread_attr_getstacksize(const pthread_attr_t* attr, size_t* size); int pthread_attr_setdetachstate(pthread_attr_t* attr, int state); int pthread_attr_getdetachstate(const pthread_attr_t* attr, int* state); +int pthread_attr_setinheritsched(pthread_attr_t* attr, int inherit); +int pthread_attr_getinheritsched(const pthread_attr_t* attr, int* inherit); +int pthread_attr_setschedpolicy(pthread_attr_t* attr, int policy); +int pthread_attr_getschedpolicy(const pthread_attr_t* attr, int* policy); +int pthread_attr_setschedparam(pthread_attr_t* attr, const struct sched_param* param); +int pthread_attr_getschedparam(const pthread_attr_t* attr, struct sched_param* param); int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*fn)(void*), void* arg); int pthread_join(pthread_t thread, void** result); From 6cd2ee84b9c8e804b94fd96c1f51b510930069b0 Mon Sep 17 00:00:00 2001 From: Fabien Poussin Date: Fri, 20 Nov 2026 17:20:36 +0100 Subject: [PATCH 3/3] Test with the SDK at d6e8eb8 (exec-memory) The SDK's module runtime implements 1.1.0's new declarations (mmap, mprotect, __clear_cache, scheduling attributes, getauxval) and its probe checks them. Move to the SDK's main once the branch merges. --- tests/sdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/sdk b/tests/sdk index 663da19..d6e8eb8 160000 --- a/tests/sdk +++ b/tests/sdk @@ -1 +1 @@ -Subproject commit 663da19d082429408abdd8373e7e20fc91950758 +Subproject commit d6e8eb886ce2bbc849d80fa4b617170ac233409a