Page MenuHomePhabricator

No OneTemporary

File Metadata

Created
Jan 24 2020, 3:50 PM
diff --git a/libc/config/linux/CMakeLists.txt b/libc/config/linux/CMakeLists.txt
index 7e2608e..86b178a 100644
--- a/libc/config/linux/CMakeLists.txt
+++ b/libc/config/linux/CMakeLists.txt
@@ -1 +1,11 @@
+add_gen_header(
+ linux_syscall_h
+ DEF_FILE syscall.h.def
+ GEN_HDR syscall.h
+ PARAMS
+ inline_syscalls=${LIBC_TARGET_MACHINE}/syscall.h.inc
+ DATA_FILES
+ ${LIBC_TARGET_MACHINE}/syscall.h.inc
+)
+
add_subdirectory(x86_64)
diff --git a/libc/config/linux/platfrom_defs.h.inc b/libc/config/linux/platfrom_defs.h.inc
index 19bf3f7..495d07c 100644
--- a/libc/config/linux/platfrom_defs.h.inc
+++ b/libc/config/linux/platfrom_defs.h.inc
@@ -1,19 +1,13 @@
//===----- Definition of platform specific macros for ELF paltforms -------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
%%begin()
#define ENTRYPOINT_SECTION_ATTRIBUTE(name) \
__attribute__((section(".llvm.libc.entrypoint."#name)))
#define LLVM_LIBC_ENTRYPOINT(name) ENTRYPOINT_SECTION_ATTRIBUTE(name) name
-
-// TODO: Get rid of the PAGE_SIZE macro. It is present only as an interim
-// measure until we can move the implementations of mmap and munmap to under
-// the config/linux directory. After that, the implementations can use
-// EXEC_PAGESIZE until page size can be read from the aux vector.
-#define PAGE_SIZE 4096
diff --git a/libc/src/unistd/syscall.h.def b/libc/config/linux/syscall.h.def
similarity index 100%
rename from libc/src/unistd/syscall.h.def
rename to libc/config/linux/syscall.h.def
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 7773be6..8558d75 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -1,69 +1,70 @@
add_header(
llvm_libc_common_h
HDR
__llvm-libc-common.h
)
add_header(
libc_posix_types_h
HDR
__posix-types.h
)
add_header(
ctype_h
HDR
ctype.h
DEPENDS
llvm_libc_common_h
)
add_header(
math_h
HDR
math.h
DEPENDS
llvm_libc_common_h
)
add_gen_header(
string_h
DEF_FILE string.h.def
GEN_HDR string.h
DEPENDS
llvm_libc_common_h
)
add_gen_header(
errno_h
DEF_FILE errno.h.def
PARAMS
platform_errno=../config/${LIBC_TARGET_OS}/errno.h.in
GEN_HDR errno.h
DATA_FILES
../config/${LIBC_TARGET_OS}/errno.h.in
)
# TODO: Not all platforms will have a include/sys directory. Add the sys
# directory and the targets for sys/*.h files conditional to the OS requiring
# them.
file(MAKE_DIRECTORY "sys")
add_gen_header(
sys_mman_h
DEF_FILE sys/mman.h.def
GEN_HDR sys/mman.h
DEPENDS
libc_posix_types_h
+ llvm_libc_common_h
)
add_gen_header(
sys_syscall_h
DEF_FILE sys/syscall.h.def
GEN_HDR sys/syscall.h
PARAMS
syscall_numbers=../config/${LIBC_TARGET_OS}/syscall_numbers.h.inc
DATA_FILES
../config/${LIBC_TARGET_OS}/syscall_numbers.h.inc
)
diff --git a/libc/src/CMakeLists.txt b/libc/src/CMakeLists.txt
index 28084cd..b416e83 100644
--- a/libc/src/CMakeLists.txt
+++ b/libc/src/CMakeLists.txt
@@ -1,8 +1,7 @@
add_subdirectory(errno)
add_subdirectory(math)
add_subdirectory(string)
# TODO: Add this target conditional to the target OS.
add_subdirectory(sys)
-add_subdirectory(unistd)
add_subdirectory(__support)
diff --git a/libc/src/sys/mman/CMakeLists.txt b/libc/src/sys/mman/CMakeLists.txt
index 9b8cc66..b4bbe81 100644
--- a/libc/src/sys/mman/CMakeLists.txt
+++ b/libc/src/sys/mman/CMakeLists.txt
@@ -1,27 +1,3 @@
-#TODO: The sources and target listed here should ideally live in config/linux.
-
-add_entrypoint_object(
- mmap
- SRCS
- mmap.cpp
- HDRS
- mmap.h
- DEPENDS
- sys_mman_h
- sys_syscall_h
- syscall_impl_h
- __errno_location
-)
-
-add_entrypoint_object(
- munmap
- SRCS
- munmap.cpp
- HDRS
- munmap.h
- DEPENDS
- sys_mman_h
- sys_syscall_h
- syscall_impl_h
- __errno_location
-)
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+ add_subdirectory(${LIBC_TARGET_OS})
+endif()
diff --git a/libc/src/sys/mman/CMakeLists.txt b/libc/src/sys/mman/linux/CMakeLists.txt
similarity index 63%
copy from libc/src/sys/mman/CMakeLists.txt
copy to libc/src/sys/mman/linux/CMakeLists.txt
index 9b8cc66..527da64 100644
--- a/libc/src/sys/mman/CMakeLists.txt
+++ b/libc/src/sys/mman/linux/CMakeLists.txt
@@ -1,27 +1,25 @@
-#TODO: The sources and target listed here should ideally live in config/linux.
-
add_entrypoint_object(
mmap
SRCS
mmap.cpp
HDRS
- mmap.h
+ ../mmap.h
DEPENDS
sys_mman_h
sys_syscall_h
- syscall_impl_h
+ linux_syscall_h
__errno_location
)
add_entrypoint_object(
munmap
SRCS
munmap.cpp
HDRS
- munmap.h
+ ../munmap.h
DEPENDS
sys_mman_h
sys_syscall_h
- syscall_impl_h
+ linux_syscall_h
__errno_location
)
diff --git a/libc/src/sys/mman/mmap.cpp b/libc/src/sys/mman/linux/mmap.cpp
similarity index 79%
rename from libc/src/sys/mman/mmap.cpp
rename to libc/src/sys/mman/linux/mmap.cpp
index 10ae0c7..616cd98 100644
--- a/libc/src/sys/mman/mmap.cpp
+++ b/libc/src/sys/mman/linux/mmap.cpp
@@ -1,55 +1,62 @@
-//===-------------- Implementation of the POSIX mmap function -------------===//
+//===---------- Linux implementation of the POSIX mmap function -----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "src/sys/mman/mmap.h"
-#include "include/sys/syscall.h" // For syscall numbers.
+
+#include "config/linux/syscall.h" // For internal syscall function.
+#include "include/sys/syscall.h" // For syscall numbers.
#include "src/__support/common.h"
#include "src/errno/llvmlibc_errno.h"
-#include "src/unistd/syscall.h" // For internal syscall function.
+
+#include <linux/param.h> // For EXEC_PAGESIZE.
namespace __llvm_libc {
// This function is currently linux only. It has to be refactored suitably if
// mmap is to be supported on non-linux operating systems also.
void *LLVM_LIBC_ENTRYPOINT(mmap)(void *addr, size_t size, int prot, int flags,
int fd, off_t offset) {
// A lot of POSIX standard prescribed validation of the parameters is not
// done in this function as modern linux versions do it in the syscall.
// TODO: Perform argument validation not done by the linux syscall.
+ // EXEC_PAGESIZE is used for the page size. While this is OK for x86_64, it
+ // might not be correct in general.
+ // TODO: Use pagesize read from the ELF aux vector instead of EXEC_PAGESIZE.
+
#ifdef SYS_mmap2
- offset /= PAGE_SIZE;
+ offset /= EXEC_PAGESIZE;
long syscall_number = SYS_mmap2;
#elif SYS_mmap
long syscall_number = SYS_mmap;
#else
#error "Target platform does not have SYS_mmap or SYS_mmap2 defined"
#endif
long ret_val =
__llvm_libc::syscall(syscall_number, reinterpret_cast<long>(addr), size,
prot, flags, fd, offset);
// The mmap/mmap2 syscalls return negative values on error. These negative
// values are actually the negative values of the error codes. So, fix them
// up in case an error code is detected.
//
// A point to keep in mind for the fix up is that a negative return value
// from the syscall can also be an error-free value returned by the syscall.
// However, since a valid return address cannot be within the last page, a
// return value corresponding to a location in the last page is an error
// value.
- if (ret_val < 0 && ret_val > -PAGE_SIZE) {
+ if (ret_val < 0 && ret_val > -EXEC_PAGESIZE) {
llvmlibc_errno = -ret_val;
return MAP_FAILED;
}
return reinterpret_cast<void *>(ret_val);
}
} // namespace __llvm_libc
diff --git a/libc/src/sys/mman/munmap.cpp b/libc/src/sys/mman/linux/munmap.cpp
similarity index 81%
rename from libc/src/sys/mman/munmap.cpp
rename to libc/src/sys/mman/linux/munmap.cpp
index d11f53a..1f112f8 100644
--- a/libc/src/sys/mman/munmap.cpp
+++ b/libc/src/sys/mman/linux/munmap.cpp
@@ -1,33 +1,34 @@
-//===------------- Implementation of the POSIX munmap function ------------===//
+//===---------- Linux implementation of the POSIX munmap function ---------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "src/sys/mman/munmap.h"
-#include "include/sys/syscall.h" // For syscall numbers.
+
+#include "config/linux/syscall.h" // For internal syscall function.
+#include "include/sys/syscall.h" // For syscall numbers.
#include "src/__support/common.h"
#include "src/errno/llvmlibc_errno.h"
-#include "src/unistd/syscall.h" // For internal syscall function.
namespace __llvm_libc {
// This function is currently linux only. It has to be refactored suitably if
// mmap is to be supported on non-linux operating systems also.
int LLVM_LIBC_ENTRYPOINT(munmap)(void *addr, size_t size) {
long ret_val =
__llvm_libc::syscall(SYS_munmap, reinterpret_cast<long>(addr), size);
// A negative return value indicates an error with the magnitude of the
// value being the error code.
if (ret_val < 0) {
llvmlibc_errno = -ret_val;
return -1;
}
return 0;
}
} // namespace __llvm_libc
diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
deleted file mode 100644
index ed23b70..0000000
--- a/libc/src/unistd/CMakeLists.txt
+++ /dev/null
@@ -1,9 +0,0 @@
-add_gen_header(
- syscall_impl_h
- DEF_FILE syscall.h.def
- GEN_HDR syscall.h
- PARAMS
- inline_syscalls=../../config/${LIBC_TARGET_OS}/${LIBC_TARGET_MACHINE}/syscall.h.inc
- DATA_FILES
- ../../config/${LIBC_TARGET_OS}/${LIBC_TARGET_MACHINE}/syscall.h.inc
-)
diff --git a/libc/test/config/linux/x86_64/CMakeLists.txt b/libc/test/config/linux/x86_64/CMakeLists.txt
index 9f9d212..23f82b2 100644
--- a/libc/test/config/linux/x86_64/CMakeLists.txt
+++ b/libc/test/config/linux/x86_64/CMakeLists.txt
@@ -1,8 +1,8 @@
add_libc_unittest(
libc_linux_x86_64_syscall_unittest
SUITE libc_linux_tests
SRCS syscall_test.cpp
DEPENDS
- syscall_impl_h
+ linux_syscall_h
support_common_h
)
diff --git a/libc/test/config/linux/x86_64/syscall_test.cpp b/libc/test/config/linux/x86_64/syscall_test.cpp
index 27628be..efab144 100644
--- a/libc/test/config/linux/x86_64/syscall_test.cpp
+++ b/libc/test/config/linux/x86_64/syscall_test.cpp
@@ -1,39 +1,39 @@
//===------------------ Unittests for x86_64 syscalls ---------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
-#include "src/unistd/syscall.h"
+#include "config/linux/syscall.h"
#include "utils/UnitTest/Test.h"
#include <functional>
TEST(X86_64_SyscallTest, APITest) {
// We only do a signature test here. Actual functionality tests are
// done by the unit tests of the syscall wrappers like mmap.
std::function<long(long)> f([](long n) { return __llvm_libc::syscall(n); });
std::function<long(long, long)> f1(
[](long n, long a1) { return __llvm_libc::syscall(n, a1); });
std::function<long(long, long, long)> f2(
[](long n, long a1, long a2) { return __llvm_libc::syscall(n, a1, a2); });
std::function<long(long, long, long, long)> f3(
[](long n, long a1, long a2, long a3) {
return __llvm_libc::syscall(n, a1, a2, a3);
});
std::function<long(long, long, long, long, long)> f4(
[](long n, long a1, long a2, long a3, long a4) {
return __llvm_libc::syscall(n, a1, a2, a3, a4);
});
std::function<long(long, long, long, long, long, long)> f5(
[](long n, long a1, long a2, long a3, long a4, long a5) {
return __llvm_libc::syscall(n, a1, a2, a3, a4, a5);
});
std::function<long(long, long, long, long, long, long, long)> f6(
[](long n, long a1, long a2, long a3, long a4, long a5, long a6) {
return __llvm_libc::syscall(n, a1, a2, a3, a4, a5, a6);
});
}
diff --git a/libc/test/src/sys/mman/CMakeLists.txt b/libc/test/src/sys/mman/CMakeLists.txt
index 3fcc8ff..b4bbe81 100644
--- a/libc/test/src/sys/mman/CMakeLists.txt
+++ b/libc/test/src/sys/mman/CMakeLists.txt
@@ -1,15 +1,3 @@
-add_libc_testsuite(libc_sys_mman_unittests)
-
-add_libc_unittest(
- mmap_test
- SUITE
- libc_sys_mman_unittests
- SRCS
- mmap_test.cpp
- DEPENDS
- errno_h
- sys_mman_h
- mmap
- munmap
- __errno_location
-)
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+ add_subdirectory(${LIBC_TARGET_OS})
+endif()
diff --git a/libc/test/src/sys/mman/CMakeLists.txt b/libc/test/src/sys/mman/linux/CMakeLists.txt
similarity index 100%
copy from libc/test/src/sys/mman/CMakeLists.txt
copy to libc/test/src/sys/mman/linux/CMakeLists.txt
diff --git a/libc/test/src/sys/mman/mmap_test.cpp b/libc/test/src/sys/mman/linux/mmap_test.cpp
similarity index 100%
rename from libc/test/src/sys/mman/mmap_test.cpp
rename to libc/test/src/sys/mman/linux/mmap_test.cpp

Event Timeline