Skip to content

Commit ec62bf2

Browse files
author
Siva Chandra Reddy
committed
[libc] Move the implementation of mmap and munmap into a linux specific 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
1 parent 96f3ea0 commit ec62bf2

File tree

15 files changed

+75
-68
lines changed

15 files changed

+75
-68
lines changed

libc/config/linux/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1+
add_gen_header(
2+
linux_syscall_h
3+
DEF_FILE syscall.h.def
4+
GEN_HDR syscall.h
5+
PARAMS
6+
inline_syscalls=${LIBC_TARGET_MACHINE}/syscall.h.inc
7+
DATA_FILES
8+
${LIBC_TARGET_MACHINE}/syscall.h.inc
9+
)
10+
111
add_subdirectory(x86_64)

libc/config/linux/platfrom_defs.h.inc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,3 @@
1111
#define ENTRYPOINT_SECTION_ATTRIBUTE(name) \
1212
__attribute__((section(".llvm.libc.entrypoint."#name)))
1313
#define LLVM_LIBC_ENTRYPOINT(name) ENTRYPOINT_SECTION_ATTRIBUTE(name) name
14-
15-
// TODO: Get rid of the PAGE_SIZE macro. It is present only as an interim
16-
// measure until we can move the implementations of mmap and munmap to under
17-
// the config/linux directory. After that, the implementations can use
18-
// EXEC_PAGESIZE until page size can be read from the aux vector.
19-
#define PAGE_SIZE 4096
File renamed without changes.

libc/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ add_gen_header(
5656
GEN_HDR sys/mman.h
5757
DEPENDS
5858
libc_posix_types_h
59+
llvm_libc_common_h
5960
)
6061

6162
add_gen_header(

libc/src/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ add_subdirectory(math)
33
add_subdirectory(string)
44
# TODO: Add this target conditional to the target OS.
55
add_subdirectory(sys)
6-
add_subdirectory(unistd)
76

87
add_subdirectory(__support)

libc/src/sys/mman/CMakeLists.txt

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,3 @@
1-
#TODO: The sources and target listed here should ideally live in config/linux.
2-
3-
add_entrypoint_object(
4-
mmap
5-
SRCS
6-
mmap.cpp
7-
HDRS
8-
mmap.h
9-
DEPENDS
10-
sys_mman_h
11-
sys_syscall_h
12-
syscall_impl_h
13-
__errno_location
14-
)
15-
16-
add_entrypoint_object(
17-
munmap
18-
SRCS
19-
munmap.cpp
20-
HDRS
21-
munmap.h
22-
DEPENDS
23-
sys_mman_h
24-
sys_syscall_h
25-
syscall_impl_h
26-
__errno_location
27-
)
1+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
2+
add_subdirectory(${LIBC_TARGET_OS})
3+
endif()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
add_entrypoint_object(
2+
mmap
3+
SRCS
4+
mmap.cpp
5+
HDRS
6+
../mmap.h
7+
DEPENDS
8+
sys_mman_h
9+
sys_syscall_h
10+
linux_syscall_h
11+
__errno_location
12+
)
13+
14+
add_entrypoint_object(
15+
munmap
16+
SRCS
17+
munmap.cpp
18+
HDRS
19+
../munmap.h
20+
DEPENDS
21+
sys_mman_h
22+
sys_syscall_h
23+
linux_syscall_h
24+
__errno_location
25+
)

libc/src/sys/mman/mmap.cpp renamed to libc/src/sys/mman/linux/mmap.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===-------------- Implementation of the POSIX mmap function -------------===//
1+
//===---------- Linux implementation of the POSIX mmap function -----------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -7,10 +7,13 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/sys/mman/mmap.h"
10-
#include "include/sys/syscall.h" // For syscall numbers.
10+
11+
#include "config/linux/syscall.h" // For internal syscall function.
12+
#include "include/sys/syscall.h" // For syscall numbers.
1113
#include "src/__support/common.h"
1214
#include "src/errno/llvmlibc_errno.h"
13-
#include "src/unistd/syscall.h" // For internal syscall function.
15+
16+
#include <linux/param.h> // For EXEC_PAGESIZE.
1417

1518
namespace __llvm_libc {
1619

@@ -22,8 +25,12 @@ void *LLVM_LIBC_ENTRYPOINT(mmap)(void *addr, size_t size, int prot, int flags,
2225
// done in this function as modern linux versions do it in the syscall.
2326
// TODO: Perform argument validation not done by the linux syscall.
2427

28+
// EXEC_PAGESIZE is used for the page size. While this is OK for x86_64, it
29+
// might not be correct in general.
30+
// TODO: Use pagesize read from the ELF aux vector instead of EXEC_PAGESIZE.
31+
2532
#ifdef SYS_mmap2
26-
offset /= PAGE_SIZE;
33+
offset /= EXEC_PAGESIZE;
2734
long syscall_number = SYS_mmap2;
2835
#elif SYS_mmap
2936
long syscall_number = SYS_mmap;
@@ -44,7 +51,7 @@ void *LLVM_LIBC_ENTRYPOINT(mmap)(void *addr, size_t size, int prot, int flags,
4451
// However, since a valid return address cannot be within the last page, a
4552
// return value corresponding to a location in the last page is an error
4653
// value.
47-
if (ret_val < 0 && ret_val > -PAGE_SIZE) {
54+
if (ret_val < 0 && ret_val > -EXEC_PAGESIZE) {
4855
llvmlibc_errno = -ret_val;
4956
return MAP_FAILED;
5057
}

libc/src/sys/mman/munmap.cpp renamed to libc/src/sys/mman/linux/munmap.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===------------- Implementation of the POSIX munmap function ------------===//
1+
//===---------- Linux implementation of the POSIX munmap function ---------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -7,10 +7,11 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/sys/mman/munmap.h"
10-
#include "include/sys/syscall.h" // For syscall numbers.
10+
11+
#include "config/linux/syscall.h" // For internal syscall function.
12+
#include "include/sys/syscall.h" // For syscall numbers.
1113
#include "src/__support/common.h"
1214
#include "src/errno/llvmlibc_errno.h"
13-
#include "src/unistd/syscall.h" // For internal syscall function.
1415

1516
namespace __llvm_libc {
1617

libc/src/unistd/CMakeLists.txt

Lines changed: 0 additions & 9 deletions
This file was deleted.

libc/test/config/linux/x86_64/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ add_libc_unittest(
33
SUITE libc_linux_tests
44
SRCS syscall_test.cpp
55
DEPENDS
6-
syscall_impl_h
6+
linux_syscall_h
77
support_common_h
88
)

libc/test/config/linux/x86_64/syscall_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "src/unistd/syscall.h"
9+
#include "config/linux/syscall.h"
1010
#include "utils/UnitTest/Test.h"
1111

1212
#include <functional>

libc/test/src/sys/mman/CMakeLists.txt

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
add_libc_testsuite(libc_sys_mman_unittests)
2-
3-
add_libc_unittest(
4-
mmap_test
5-
SUITE
6-
libc_sys_mman_unittests
7-
SRCS
8-
mmap_test.cpp
9-
DEPENDS
10-
errno_h
11-
sys_mman_h
12-
mmap
13-
munmap
14-
__errno_location
15-
)
1+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
2+
add_subdirectory(${LIBC_TARGET_OS})
3+
endif()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
add_libc_testsuite(libc_sys_mman_unittests)
2+
3+
add_libc_unittest(
4+
mmap_test
5+
SUITE
6+
libc_sys_mman_unittests
7+
SRCS
8+
mmap_test.cpp
9+
DEPENDS
10+
errno_h
11+
sys_mman_h
12+
mmap
13+
munmap
14+
__errno_location
15+
)

0 commit comments

Comments
 (0)