diff --git a/libc/src/threads/linux/CMakeLists.txt b/libc/src/threads/linux/CMakeLists.txt --- a/libc/src/threads/linux/CMakeLists.txt +++ b/libc/src/threads/linux/CMakeLists.txt @@ -35,12 +35,11 @@ DEPENDS .threads_utils libc.include.errno + libc.include.sys_mman libc.include.sys_syscall libc.include.threads libc.src.__support.common libc.src.__support.OSUtil.osutil - libc.src.errno.errno - libc.src.sys.mman.mmap COMPILE_OPTIONS -O3 -fno-omit-frame-pointer # This allows us to sniff out the thread args from @@ -60,7 +59,6 @@ libc.src.__support.CPP.atomic libc.src.__support.common libc.src.__support.OSUtil.osutil - libc.src.sys.mman.munmap ) add_entrypoint_object( diff --git a/libc/src/threads/linux/thrd_create.cpp b/libc/src/threads/linux/thrd_create.cpp --- a/libc/src/threads/linux/thrd_create.cpp +++ b/libc/src/threads/linux/thrd_create.cpp @@ -8,21 +8,26 @@ #include "Futex.h" -#include "include/errno.h" // For E* error values. -#include "include/sys/mman.h" // For PROT_* and MAP_* definitions. -#include "include/sys/syscall.h" // For syscall numbers. -#include "include/threads.h" // For thrd_* type definitions. #include "src/__support/OSUtil/syscall.h" // For syscall function. #include "src/__support/architectures.h" #include "src/__support/common.h" -#include "src/errno/llvmlibc_errno.h" -#include "src/sys/mman/mmap.h" -#include "src/sys/mman/munmap.h" #include "src/threads/linux/Thread.h" #include "src/threads/thrd_create.h" +#include // For E* error values. #include // For CLONE_* flags. #include +#include // For PROT_* and MAP_* definitions. +#include // For syscall numbers. +#include // For thrd_* type definitions. + +#ifdef SYS_mmap2 +constexpr long MMAP_SYSCALL_NUMBER = SYS_mmap2; +#elif SYS_mmap +constexpr long MMAP_SYSCALL_NUMBER = SYS_mmap; +#else +#error "SYS_mmap or SYS_mmap2 not available on the target platform" +#endif namespace __llvm_libc { @@ -73,11 +78,21 @@ // TODO: Add the CLONE_SETTLS flag and setup the TLS area correctly when // making the clone syscall. - void *stack = __llvm_libc::mmap(nullptr, ThreadParams::DEFAULT_STACK_SIZE, - PROT_READ | PROT_WRITE, - MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); - if (stack == MAP_FAILED) - return llvmlibc_errno == ENOMEM ? thrd_nomem : thrd_error; + // Allocate thread stack. + long mmap_result = + __llvm_libc::syscall(MMAP_SYSCALL_NUMBER, + 0, // No special address + ThreadParams::DEFAULT_STACK_SIZE, + PROT_READ | PROT_WRITE, // Read and write stack + MAP_ANONYMOUS | MAP_PRIVATE, // Process private + -1, // Not backed by any file + 0 // No offset + ); + if (mmap_result < 0 && (uintptr_t(mmap_result) >= + UINTPTR_MAX - ThreadParams::DEFAULT_STACK_SIZE)) { + return -mmap_result == ENOMEM ? thrd_nomem : thrd_error; + } + void *stack = reinterpret_cast(mmap_result); thread->__stack = stack; thread->__stack_size = ThreadParams::DEFAULT_STACK_SIZE; @@ -126,7 +141,8 @@ if (clone_result == 0) { start_thread(); } else if (clone_result < 0) { - __llvm_libc::munmap(thread->__stack, thread->__stack_size); + __llvm_libc::syscall(SYS_munmap, mmap_result, + ThreadParams::DEFAULT_STACK_SIZE); int error_val = -clone_result; return error_val == ENOMEM ? thrd_nomem : thrd_error; } diff --git a/libc/src/threads/linux/thrd_join.cpp b/libc/src/threads/linux/thrd_join.cpp --- a/libc/src/threads/linux/thrd_join.cpp +++ b/libc/src/threads/linux/thrd_join.cpp @@ -8,16 +8,15 @@ #include "Futex.h" -#include "include/sys/syscall.h" // For syscall numbers. -#include "include/threads.h" // For thrd_* type definitions. #include "src/__support/CPP/atomic.h" #include "src/__support/OSUtil/syscall.h" // For syscall function. #include "src/__support/common.h" -#include "src/sys/mman/munmap.h" #include "src/threads/linux/Thread.h" #include "src/threads/thrd_join.h" #include // For futex operations. +#include // For syscall numbers. +#include // For thrd_* type definitions. namespace __llvm_libc { @@ -37,7 +36,8 @@ *retval = thread->__retval; - if (__llvm_libc::munmap(thread->__stack, thread->__stack_size) == -1) + if (__llvm_libc::syscall(SYS_munmap, reinterpret_cast(thread->__stack), + thread->__stack_size) == -1) return thrd_error; return thrd_success;