Skip to content

[libc] Enable setitimer and getitimer functions on riscv #139182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mikhailramalho
Copy link
Member

These functions don't have a _time64 variant, so we can't use time_t directly (since our time_t is an uint64_t). The workaround is to use longs when doing the syscall and write back when necessary.

These functions don't have a _time64 variant, so we can't use time_t
directly (since our time_t is an uint64_t). The workaround is to use
longs when doing the syscall and write back when necessary.
@llvmbot
Copy link
Member

llvmbot commented May 9, 2025

@llvm/pr-subscribers-libc

Author: Mikhail R. Gadelha (mikhailramalho)

Changes

These functions don't have a _time64 variant, so we can't use time_t directly (since our time_t is an uint64_t). The workaround is to use longs when doing the syscall and write back when necessary.


Full diff: https://github.com/llvm/llvm-project/pull/139182.diff

3 Files Affected:

  • (modified) libc/config/linux/riscv/entrypoints.txt (+2-2)
  • (modified) libc/src/sys/time/linux/getitimer.cpp (+16-2)
  • (modified) libc/src/sys/time/linux/setitimer.cpp (+24-2)
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 30d9d00dfefc9..effcd45de79bc 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -371,8 +371,8 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.uio.readv
 
     # sys/time.h entrypoints
-    # libc.src.sys.time.setitimer
-    # libc.src.sys.time.getitimer
+    libc.src.sys.time.setitimer
+    libc.src.sys.time.getitimer
 )
 
 if(LLVM_LIBC_INCLUDE_SCUDO)
diff --git a/libc/src/sys/time/linux/getitimer.cpp b/libc/src/sys/time/linux/getitimer.cpp
index bbdbaa57dfd30..180e95bef7ace 100644
--- a/libc/src/sys/time/linux/getitimer.cpp
+++ b/libc/src/sys/time/linux/getitimer.cpp
@@ -16,8 +16,22 @@
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, getitimer, (int which, struct itimerval *curr_value)) {
-  long ret =
-      LIBC_NAMESPACE::syscall_impl<long>(SYS_getitimer, which, curr_value);
+  long ret = 0;
+  if constexpr (sizeof(time_t) > sizeof(long)) {
+    // There is no SYS_getitimer_time64 call, so we can't use time_t directly.
+    long curr_value32[4];
+    ret = LIBC_NAMESPACE::syscall_impl<long>(SYS_getitimer, which, curr_value32);
+    if (!ret) {
+      curr_value->it_interval.tv_sec = curr_value32[0];
+      curr_value->it_interval.tv_usec = curr_value32[1];
+      curr_value->it_value.tv_sec = curr_value32[2];
+      curr_value->it_value.tv_usec = curr_value32[3];
+    }
+  } else {
+    ret =
+        LIBC_NAMESPACE::syscall_impl<long>(SYS_getitimer, which, curr_value);
+  }
+
   // On failure, return -1 and set errno.
   if (ret < 0) {
     libc_errno = static_cast<int>(-ret);
diff --git a/libc/src/sys/time/linux/setitimer.cpp b/libc/src/sys/time/linux/setitimer.cpp
index b50356004701d..def04a4740118 100644
--- a/libc/src/sys/time/linux/setitimer.cpp
+++ b/libc/src/sys/time/linux/setitimer.cpp
@@ -17,8 +17,30 @@ namespace LIBC_NAMESPACE_DECL {
 LLVM_LIBC_FUNCTION(int, setitimer,
                    (int which, const struct itimerval *new_value,
                     struct itimerval *old_value)) {
-  long ret = LIBC_NAMESPACE::syscall_impl<long>(SYS_setitimer, which, new_value,
-                                                old_value);
+  long ret = 0;
+  if constexpr (sizeof(time_t) > sizeof(long)) {
+    // There is no SYS_setitimer_time64 call, so we can't use time_t directly,
+    // and need to convert it to long first.
+    long new_value32[4] = {static_cast<long>(new_value->it_interval.tv_sec),
+                           new_value->it_interval.tv_usec,
+                           static_cast<long>(new_value->it_value.tv_sec),
+                           new_value->it_value.tv_usec};
+    long old_value32[4];
+
+    ret = LIBC_NAMESPACE::syscall_impl<long>(SYS_setitimer, which, new_value32,
+                                             old_value32);
+
+    if (!ret && old_value) {
+      old_value->it_interval.tv_sec = old_value32[0];
+      old_value->it_interval.tv_usec = old_value32[1];
+      old_value->it_value.tv_sec = old_value32[2];
+      old_value->it_value.tv_usec = old_value32[3];
+    }
+  } else {
+    ret = LIBC_NAMESPACE::syscall_impl<long>(SYS_setitimer, which, new_value,
+                                             old_value);
+  }
+
   // On failure, return -1 and set errno.
   if (ret < 0) {
     libc_errno = static_cast<int>(-ret);

long ret = 0;
if constexpr (sizeof(time_t) > sizeof(long)) {
// There is no SYS_getitimer_time64 call, so we can't use time_t directly.
long curr_value32[4];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created an array of long because it was less verbose than creating a local struct just for this, but please let me know if you have other suggestions.

Copy link

github-actions bot commented May 9, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Signed-off-by: Mikhail R. Gadelha <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants