[libc-commits] [libc] 9ca2fb8 - [libc] Add riscv64 syscall implementation.

Mikhail R. Gadelha via libc-commits libc-commits at lists.llvm.org
Tue Mar 7 16:49:11 PST 2023


Author: Siva Chandra
Date: 2023-03-07T21:47:58-03:00
New Revision: 9ca2fb82177ff666f0d2049bc6250c88ca103e50

URL: https://github.com/llvm/llvm-project/commit/9ca2fb82177ff666f0d2049bc6250c88ca103e50
DIFF: https://github.com/llvm/llvm-project/commit/9ca2fb82177ff666f0d2049bc6250c88ca103e50.diff

LOG: [libc] Add riscv64 syscall implementation.

All syscall wrapper functions which have unit tests have been enabled.

Reviewed By: kito-cheng

Differential Revision: https://reviews.llvm.org/D145452

Added: 
    libc/src/__support/OSUtil/linux/riscv64/CMakeLists.txt
    libc/src/__support/OSUtil/linux/riscv64/syscall.h

Modified: 
    libc/config/linux/riscv64/entrypoints.txt
    libc/src/__support/OSUtil/linux/syscall.h
    libc/src/__support/macros/properties/architectures.h

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/riscv64/entrypoints.txt b/libc/config/linux/riscv64/entrypoints.txt
index 183cf1b66a88d..de901a7a95500 100644
--- a/libc/config/linux/riscv64/entrypoints.txt
+++ b/libc/config/linux/riscv64/entrypoints.txt
@@ -20,6 +20,15 @@ set(TARGET_LIBC_ENTRYPOINTS
     # errno.h entrypoints
     libc.src.errno.errno
 
+    # fcntl.h entrypoints
+    libc.src.fcntl.creat
+    libc.src.fcntl.open
+    libc.src.fcntl.openat
+
+    # sched.h entrypoints
+    libc.src.sched.sched_getaffinity
+    libc.src.sched.sched_setaffinity
+
     # string.h entrypoints
     libc.src.string.bcmp
     libc.src.string.bcopy
@@ -41,6 +50,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.string.strcmp
     libc.src.string.strcpy
     libc.src.string.strcspn
+    libc.src.string.strdup
+    libc.src.string.strerror
+    libc.src.string.strerror_r
     libc.src.string.strlcat
     libc.src.string.strlcpy
     libc.src.string.strlen
@@ -48,9 +60,11 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.string.strncat
     libc.src.string.strncmp
     libc.src.string.strncpy
+    libc.src.string.strndup
     libc.src.string.strnlen
     libc.src.string.strpbrk
     libc.src.string.strrchr
+    libc.src.string.strsignal
     libc.src.string.strspn
     libc.src.string.strstr
     libc.src.string.strtok
@@ -82,6 +96,96 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdlib.strtoll
     libc.src.stdlib.strtoul
     libc.src.stdlib.strtoull
+
+    # stdlib.h external entrypoints
+    libc.src.stdlib.malloc
+    libc.src.stdlib.calloc
+    libc.src.stdlib.realloc
+    libc.src.stdlib.aligned_alloc
+    libc.src.stdlib.free
+
+    # stdio.h entrypoints
+    libc.src.stdio.remove
+
+    # sys/mman.h entrypoints
+    libc.src.sys.mman.madvise
+    libc.src.sys.mman.mmap
+    libc.src.sys.mman.mprotect
+    libc.src.sys.mman.munmap
+    libc.src.sys.mman.posix_madvise
+
+    # sys/random.h entrypoints
+    libc.src.sys.random.getrandom
+
+    # sys/resource.h entrypoints
+    libc.src.sys.resource.getrlimit
+    libc.src.sys.resource.setrlimit
+
+    # sys/sendfile entrypoints
+    libc.src.sys.sendfile.sendfile
+
+    # sys/stat.h entrypoints
+    libc.src.sys.stat.chmod
+    libc.src.sys.stat.fchmod
+    libc.src.sys.stat.fchmodat
+    libc.src.sys.stat.fstat
+    libc.src.sys.stat.lstat
+    libc.src.sys.stat.mkdir
+    libc.src.sys.stat.mkdirat
+    libc.src.sys.stat.stat
+
+    # sys/utsname.h entrypoints
+    libc.src.sys.utsname.uname
+
+    # sys/wait.h entrypoints
+    libc.src.sys.wait.wait
+    libc.src.sys.wait.wait4
+    libc.src.sys.wait.waitpid
+
+    # termios.h entrypoints
+    libc.src.termios.cfgetispeed
+    libc.src.termios.cfgetospeed
+    libc.src.termios.cfsetispeed
+    libc.src.termios.cfsetospeed
+    libc.src.termios.tcgetattr
+    libc.src.termios.tcgetsid
+    libc.src.termios.tcdrain
+    libc.src.termios.tcflow
+    libc.src.termios.tcflush
+    libc.src.termios.tcsendbreak
+    libc.src.termios.tcsetattr
+
+    # unistd.h entrypoints
+    libc.src.unistd.access
+    libc.src.unistd.chdir
+    libc.src.unistd.close
+    libc.src.unistd.dup
+    libc.src.unistd.dup2
+    libc.src.unistd.dup3
+    libc.src.unistd.fchdir
+    libc.src.unistd.fsync
+    libc.src.unistd.ftruncate
+    libc.src.unistd.getcwd
+    libc.src.unistd.geteuid
+    libc.src.unistd.getpid
+    libc.src.unistd.getppid
+    libc.src.unistd.getuid
+    libc.src.unistd.link
+    libc.src.unistd.linkat
+    libc.src.unistd.lseek
+    libc.src.unistd.pread
+    libc.src.unistd.pwrite
+    libc.src.unistd.read
+    libc.src.unistd.readlink
+    libc.src.unistd.readlinkat
+    libc.src.unistd.rmdir
+    libc.src.unistd.symlink
+    libc.src.unistd.symlinkat
+    libc.src.unistd.sysconf
+    libc.src.unistd.truncate
+    libc.src.unistd.unlink
+    libc.src.unistd.unlinkat
+    libc.src.unistd.write
 )
 
 set(TARGET_LIBM_ENTRYPOINTS

diff  --git a/libc/src/__support/OSUtil/linux/riscv64/CMakeLists.txt b/libc/src/__support/OSUtil/linux/riscv64/CMakeLists.txt
new file mode 100644
index 0000000000000..69540e3b45cbc
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/riscv64/CMakeLists.txt
@@ -0,0 +1,7 @@
+add_header_library(
+  linux_riscv64_util
+  HDRS
+    syscall.h
+  DEPENDS
+    libc.src.__support.common
+)

diff  --git a/libc/src/__support/OSUtil/linux/riscv64/syscall.h b/libc/src/__support/OSUtil/linux/riscv64/syscall.h
new file mode 100644
index 0000000000000..c9d2a39941a64
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/riscv64/syscall.h
@@ -0,0 +1,110 @@
+//===--------- inline implementation of riscv64 syscalls ----------* C++ *-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SUPPORT_OSUTIL_LINUX_RISCV64_SYSCALL_H
+#define LLVM_LIBC_SRC_SUPPORT_OSUTIL_LINUX_RISCV64_SYSCALL_H
+
+#include "src/__support/common.h"
+
+#define REGISTER_DECL_0                                                        \
+  register long a7 __asm__("a7") = number;                                     \
+  register long a0 __asm__("a0");
+#define REGISTER_DECL_1                                                        \
+  register long a7 __asm__("a7") = number;                                     \
+  register long a0 __asm__("a0") = arg1;
+#define REGISTER_DECL_2 REGISTER_DECL_1 register long a1 __asm__("a1") = arg2;
+#define REGISTER_DECL_3                                                        \
+  REGISTER_DECL_2                                                              \
+  register long a2 __asm__("a2") = arg3;
+#define REGISTER_DECL_4                                                        \
+  REGISTER_DECL_3                                                              \
+  register long a3 __asm__("a3") = arg4;
+#define REGISTER_DECL_5                                                        \
+  REGISTER_DECL_4                                                              \
+  register long a4 __asm__("a4") = arg5;
+#define REGISTER_DECL_6                                                        \
+  REGISTER_DECL_5                                                              \
+  register long a5 __asm__("a5") = arg6;
+
+#define REGISTER_CONSTRAINT_0 "r"(a7)
+#define REGISTER_CONSTRAINT_1 REGISTER_CONSTRAINT_0, "r"(a0)
+#define REGISTER_CONSTRAINT_2 REGISTER_CONSTRAINT_1, "r"(a1)
+#define REGISTER_CONSTRAINT_3 REGISTER_CONSTRAINT_2, "r"(a2)
+#define REGISTER_CONSTRAINT_4 REGISTER_CONSTRAINT_3, "r"(a3)
+#define REGISTER_CONSTRAINT_5 REGISTER_CONSTRAINT_4, "r"(a4)
+#define REGISTER_CONSTRAINT_6 REGISTER_CONSTRAINT_5, "r"(a5)
+
+#define SYSCALL_INSTR(input_constraint)                                        \
+  LIBC_INLINE_ASM("ecall\n\t" : "=r"(a0) : input_constraint : "memory")
+
+namespace __llvm_libc {
+
+LIBC_INLINE long syscall_impl(long number) {
+  REGISTER_DECL_0;
+  SYSCALL_INSTR(REGISTER_CONSTRAINT_0);
+  return a0;
+}
+
+LIBC_INLINE long syscall_impl(long number, long arg1) {
+  REGISTER_DECL_1;
+  SYSCALL_INSTR(REGISTER_CONSTRAINT_1);
+  return a0;
+}
+
+LIBC_INLINE long syscall_impl(long number, long arg1, long arg2) {
+  REGISTER_DECL_2;
+  SYSCALL_INSTR(REGISTER_CONSTRAINT_2);
+  return a0;
+}
+
+LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3) {
+  REGISTER_DECL_3;
+  SYSCALL_INSTR(REGISTER_CONSTRAINT_3);
+  return a0;
+}
+
+LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3,
+                              long arg4) {
+  REGISTER_DECL_4;
+  SYSCALL_INSTR(REGISTER_CONSTRAINT_4);
+  return a0;
+}
+
+LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3,
+                              long arg4, long arg5) {
+  REGISTER_DECL_5;
+  SYSCALL_INSTR(REGISTER_CONSTRAINT_5);
+  return a0;
+}
+
+LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3,
+                              long arg4, long arg5, long arg6) {
+  REGISTER_DECL_6;
+  SYSCALL_INSTR(REGISTER_CONSTRAINT_6);
+  return a0;
+}
+
+} // namespace __llvm_libc
+
+#undef REGISTER_DECL_0
+#undef REGISTER_DECL_1
+#undef REGISTER_DECL_2
+#undef REGISTER_DECL_3
+#undef REGISTER_DECL_4
+#undef REGISTER_DECL_5
+#undef REGISTER_DECL_6
+
+#undef REGISTER_CONSTRAINT_0
+#undef REGISTER_CONSTRAINT_1
+#undef REGISTER_CONSTRAINT_2
+#undef REGISTER_CONSTRAINT_3
+#undef REGISTER_CONSTRAINT_4
+#undef REGISTER_CONSTRAINT_5
+#undef REGISTER_CONSTRAINT_6
+
+#endif // LLVM_LIBC_SRC_SUPPORT_OSUTIL_LINUX_RISCV64_SYSCALL_H

diff  --git a/libc/src/__support/OSUtil/linux/syscall.h b/libc/src/__support/OSUtil/linux/syscall.h
index 864fa4b99824a..c0366cfd9e71b 100644
--- a/libc/src/__support/OSUtil/linux/syscall.h
+++ b/libc/src/__support/OSUtil/linux/syscall.h
@@ -18,6 +18,8 @@
 #include "aarch64/syscall.h"
 #elif defined(LIBC_TARGET_ARCH_IS_ARM)
 #include "arm/syscall.h"
+#elif defined(LIBC_TARGET_ARCH_IS_RISCV64)
+#include "riscv64/syscall.h"
 #endif
 
 namespace __llvm_libc {

diff  --git a/libc/src/__support/macros/properties/architectures.h b/libc/src/__support/macros/properties/architectures.h
index 238e5feef192b..1247fd6ef5cbc 100644
--- a/libc/src/__support/macros/properties/architectures.h
+++ b/libc/src/__support/macros/properties/architectures.h
@@ -45,6 +45,10 @@
 #define LIBC_TARGET_ARCH_IS_AARCH64
 #endif
 
+#if defined(__riscv) && (__riscv_xlen == 64)
+#define LIBC_TARGET_ARCH_IS_RISCV64
+#endif
+
 #if (defined(LIBC_TARGET_ARCH_IS_AARCH64) || defined(LIBC_TARGET_ARCH_IS_ARM))
 #define LIBC_TARGET_ARCH_IS_ANY_ARM
 #endif


        


More information about the libc-commits mailing list