diff --git a/libc/src/__support/OSUtil/darwin/CMakeLists.txt b/libc/src/__support/OSUtil/darwin/CMakeLists.txt new file mode 100644 index 0000000000000..a3e6b93c8e992 --- /dev/null +++ b/libc/src/__support/OSUtil/darwin/CMakeLists.txt @@ -0,0 +1,17 @@ +if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_ARCHITECTURE}) + return() +endif() + +add_subdirectory(${LIBC_TARGET_ARCHITECTURE}) + +add_header_library( + darwin_util + HDRS + io.h + quick_exit.h + syscall.h + DEPENDS + .${LIBC_TARGET_ARCHITECTURE}.darwin_util + libc.src.__support.common + libc.src.__support.CPP.string_view +) diff --git a/libc/src/__support/OSUtil/darwin/arm/CMakeLists.txt b/libc/src/__support/OSUtil/darwin/arm/CMakeLists.txt new file mode 100644 index 0000000000000..5ab95b01758c8 --- /dev/null +++ b/libc/src/__support/OSUtil/darwin/arm/CMakeLists.txt @@ -0,0 +1,7 @@ +add_header_library( + darwin_util + HDRS + syscall.h + DEPENDS + libc.src.__support.common +) diff --git a/libc/src/__support/OSUtil/darwin/arm/syscall.h b/libc/src/__support/OSUtil/darwin/arm/syscall.h new file mode 100644 index 0000000000000..5b7dc7a3056ef --- /dev/null +++ b/libc/src/__support/OSUtil/darwin/arm/syscall.h @@ -0,0 +1,112 @@ +//===------ inline implementation of Darwin arm64 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_DARWIN_ARM_SYSCALL_H +#define LLVM_LIBC_SRC_SUPPORT_OSUTIL_DARWIN_ARM_SYSCALL_H + +#include "src/__support/common.h" + +#define REGISTER_DECL_0 \ + register long x16 __asm__("x16") = number; \ + register long x0 __asm__("x0"); +#define REGISTER_DECL_1 \ + register long x16 __asm__("x16") = number; \ + register long x0 __asm__("x0") = arg1; +#define REGISTER_DECL_2 \ + REGISTER_DECL_1 \ + register long x1 __asm__("x1") = arg2; +#define REGISTER_DECL_3 \ + REGISTER_DECL_2 \ + register long x2 __asm__("x2") = arg3; +#define REGISTER_DECL_4 \ + REGISTER_DECL_3 \ + register long x3 __asm__("x3") = arg4; +#define REGISTER_DECL_5 \ + REGISTER_DECL_4 \ + register long x4 __asm__("x4") = arg5; +#define REGISTER_DECL_6 \ + REGISTER_DECL_5 \ + register long x5 __asm__("x5") = arg6; + +#define REGISTER_CONSTRAINT_0 "r"(x16) +#define REGISTER_CONSTRAINT_1 REGISTER_CONSTRAINT_0, "r"(x0) +#define REGISTER_CONSTRAINT_2 REGISTER_CONSTRAINT_1, "r"(x1) +#define REGISTER_CONSTRAINT_3 REGISTER_CONSTRAINT_2, "r"(x2) +#define REGISTER_CONSTRAINT_4 REGISTER_CONSTRAINT_3, "r"(x3) +#define REGISTER_CONSTRAINT_5 REGISTER_CONSTRAINT_4, "r"(x4) +#define REGISTER_CONSTRAINT_6 REGISTER_CONSTRAINT_5, "r"(x5) + +#define SYSCALL_INSTR(input_constraint) \ + LIBC_INLINE_ASM("svc 0x80" : "=r"(x0) : input_constraint : "memory", "cc") + +namespace __llvm_libc { + +LIBC_INLINE long syscall_impl(long number) { + REGISTER_DECL_0; + SYSCALL_INSTR(REGISTER_CONSTRAINT_0); + return x0; +} + +LIBC_INLINE long syscall_impl(long number, long arg1) { + REGISTER_DECL_1; + SYSCALL_INSTR(REGISTER_CONSTRAINT_1); + return x0; +} + +LIBC_INLINE long syscall_impl(long number, long arg1, long arg2) { + REGISTER_DECL_2; + SYSCALL_INSTR(REGISTER_CONSTRAINT_2); + return x0; +} + +LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3) { + REGISTER_DECL_3; + SYSCALL_INSTR(REGISTER_CONSTRAINT_3); + return x0; +} + +LIBC_INLINE long syscall_impl(long number, long arg1, long arg2, long arg3, + long arg4) { + REGISTER_DECL_4; + SYSCALL_INSTR(REGISTER_CONSTRAINT_4); + return x0; +} + +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 x0; +} + +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 x0; +} + +} // 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_DARWIN_ARM_SYSCALL_H diff --git a/libc/src/__support/OSUtil/darwin/io.h b/libc/src/__support/OSUtil/darwin/io.h new file mode 100644 index 0000000000000..164d02aacc01a --- /dev/null +++ b/libc/src/__support/OSUtil/darwin/io.h @@ -0,0 +1,24 @@ +//===------------- Darwin implementation of IO utils ------------*- 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_DARWIN_IO_H +#define LLVM_LIBC_SRC_SUPPORT_OSUTIL_DARWIN_IO_H + +#include "src/__support/CPP/string_view.h" +#include "syscall.h" // For internal syscall function. + +namespace __llvm_libc { + +LIBC_INLINE void write_to_stderr(cpp::string_view msg) { + __llvm_libc::syscall_impl(4 /*SYS_write*/, 2 /* stderr */, + reinterpret_cast(msg.data()), msg.size()); +} + +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_SUPPORT_OSUTIL_DARWIN_IO_H diff --git a/libc/src/__support/OSUtil/darwin/quick_exit.h b/libc/src/__support/OSUtil/darwin/quick_exit.h new file mode 100644 index 0000000000000..4fe5896cc812c --- /dev/null +++ b/libc/src/__support/OSUtil/darwin/quick_exit.h @@ -0,0 +1,26 @@ +//===--------- Darwin implementation of a quick exit function ---*- 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_DARWIN_QUICK_EXIT_H +#define LLVM_LIBC_SRC_SUPPORT_OSUTIL_DARWIN_QUICK_EXIT_H + +#include "syscall.h" // For internal syscall function. + +#include "src/__support/common.h" + +namespace __llvm_libc { + +LIBC_INLINE void quick_exit(int status) { + for (;;) { + __llvm_libc::syscall_impl(1 /* SYS_exit */, status); + } +} + +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_SUPPORT_OSUTIL_DARWIN_QUICK_EXIT_H diff --git a/libc/src/__support/OSUtil/darwin/syscall.h b/libc/src/__support/OSUtil/darwin/syscall.h new file mode 100644 index 0000000000000..9ce1b3cc8459b --- /dev/null +++ b/libc/src/__support/OSUtil/darwin/syscall.h @@ -0,0 +1,31 @@ +//===---------------------- Darwin 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_DARWIN_SYSCALL_H +#define LLVM_LIBC_SRC_SUPPORT_OSUTIL_DARWIN_SYSCALL_H + +#include "src/__support/common.h" +#include "src/__support/macros/properties/architectures.h" + +#ifdef LIBC_TARGET_ARCH_IS_ANY_ARM +#include "arm/syscall.h" +#else +#error "Unsupported architecture" +#endif + +namespace __llvm_libc { + +template +LIBC_INLINE long syscall_impl(long __number, Ts... ts) { + static_assert(sizeof...(Ts) <= 6, "Too many arguments for syscall"); + return syscall_impl(__number, (long)ts...); +} + +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_SUPPORT_OSUTIL_DARWIN_SYSCALL_H diff --git a/libc/src/__support/OSUtil/io.h b/libc/src/__support/OSUtil/io.h index 7727bd5d29ac8..9330ee57646de 100644 --- a/libc/src/__support/OSUtil/io.h +++ b/libc/src/__support/OSUtil/io.h @@ -13,6 +13,8 @@ #if defined(LIBC_TARGET_ARCH_IS_GPU) #include "gpu/io.h" +#elif defined(__APPLE__) +#include "darwin/io.h" #elif defined(__unix__) #include "linux/io.h" #elif defined(__Fuchsia__) diff --git a/libc/src/__support/OSUtil/quick_exit.h b/libc/src/__support/OSUtil/quick_exit.h index 310e376f49da0..ae0192d02c1bb 100644 --- a/libc/src/__support/OSUtil/quick_exit.h +++ b/libc/src/__support/OSUtil/quick_exit.h @@ -13,6 +13,8 @@ #if defined(LIBC_TARGET_ARCH_IS_GPU) #include "gpu/quick_exit.h" +#elif defined(__APPLE__) +#include "darwin/quick_exit.h" #elif defined(__unix__) #include "linux/quick_exit.h" #endif diff --git a/libc/src/__support/OSUtil/syscall.h b/libc/src/__support/OSUtil/syscall.h index b1137275a86f0..3601e7653123a 100644 --- a/libc/src/__support/OSUtil/syscall.h +++ b/libc/src/__support/OSUtil/syscall.h @@ -9,7 +9,9 @@ #ifndef LLVM_LIBC_SRC_SUPPORT_OSUTIL_SYSCALL_H #define LLVM_LIBC_SRC_SUPPORT_OSUTIL_SYSCALL_H -#ifdef __unix__ +#ifdef __APPLE__ +#include "darwin/syscall.h" +#elif defined(__unix__) #include "linux/syscall.h" #endif