Skip to content

Commit

Permalink
[libc] Move the implementation of mmap and munmap into a linux specif…
Browse files Browse the repository at this point in the history
…ic area.

This allows us to get rid of the PAGE_SIZE macro and use EXEC_PAGESIZE
from linux/param.h.

Few other points about this change:
1. The linux syscall functions have been moved into a linux specific area
instead of src/unistd/syscall.h. The Linux syscall function from unistd.h
is a public vararg function. What we have currently are linux speciif internal
overloaded C++ functions. So, moving them to a Linux only area is more
meaningful.
2. The implementations of mmap and munmap are now in a 'linux' directory
within src/sys/mman. The idea here is that platform specific
implementations will live in a platform specific subdirectories like these.
Infrastructure common to a platform will live in the platform's config
directory. For example, the linux syscall implementations live in
config/linux.

Reviewers: abrachet

Tags: #libc-project

Differential Revision: https://reviews.llvm.org/D73302
  • Loading branch information
Siva Chandra Reddy committed Jan 24, 2020
1 parent 96f3ea0 commit ec62bf2
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 68 deletions.
10 changes: 10 additions & 0 deletions libc/config/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 0 additions & 6 deletions libc/config/linux/platfrom_defs.h.inc
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,3 @@
#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
File renamed without changes.
1 change: 1 addition & 0 deletions libc/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ add_gen_header(
GEN_HDR sys/mman.h
DEPENDS
libc_posix_types_h
llvm_libc_common_h
)

add_gen_header(
Expand Down
1 change: 0 additions & 1 deletion libc/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ add_subdirectory(math)
add_subdirectory(string)
# TODO: Add this target conditional to the target OS.
add_subdirectory(sys)
add_subdirectory(unistd)

add_subdirectory(__support)
30 changes: 3 additions & 27 deletions libc/src/sys/mman/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
25 changes: 25 additions & 0 deletions libc/src/sys/mman/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
add_entrypoint_object(
mmap
SRCS
mmap.cpp
HDRS
../mmap.h
DEPENDS
sys_mman_h
sys_syscall_h
linux_syscall_h
__errno_location
)

add_entrypoint_object(
munmap
SRCS
munmap.cpp
HDRS
../munmap.h
DEPENDS
sys_mman_h
sys_syscall_h
linux_syscall_h
__errno_location
)
17 changes: 12 additions & 5 deletions libc/src/sys/mman/mmap.cpp → libc/src/sys/mman/linux/mmap.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===-------------- 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.
Expand All @@ -7,10 +7,13 @@
//===----------------------------------------------------------------------===//

#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 {

Expand All @@ -22,8 +25,12 @@ void *LLVM_LIBC_ENTRYPOINT(mmap)(void *addr, size_t size, int prot, int flags,
// 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;
Expand All @@ -44,7 +51,7 @@ void *LLVM_LIBC_ENTRYPOINT(mmap)(void *addr, size_t size, int prot, int flags,
// 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===------------- 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.
Expand All @@ -7,10 +7,11 @@
//===----------------------------------------------------------------------===//

#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 {

Expand Down
9 changes: 0 additions & 9 deletions libc/src/unistd/CMakeLists.txt

This file was deleted.

2 changes: 1 addition & 1 deletion libc/test/config/linux/x86_64/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ add_libc_unittest(
SUITE libc_linux_tests
SRCS syscall_test.cpp
DEPENDS
syscall_impl_h
linux_syscall_h
support_common_h
)
2 changes: 1 addition & 1 deletion libc/test/config/linux/x86_64/syscall_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "src/unistd/syscall.h"
#include "config/linux/syscall.h"
#include "utils/UnitTest/Test.h"

#include <functional>
Expand Down
18 changes: 3 additions & 15 deletions libc/test/src/sys/mman/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
15 changes: 15 additions & 0 deletions libc/test/src/sys/mman/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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
)
File renamed without changes.

0 comments on commit ec62bf2

Please sign in to comment.