Skip to content

Commit

Permalink
Add macos.
Browse files Browse the repository at this point in the history
  • Loading branch information
fire committed Jun 29, 2024
1 parent 8764a1d commit 64ba08c
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 102 deletions.
12 changes: 12 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ add_library(riscv ${SOURCES})
if (COSMOPOLITAN)
target_compile_options(riscv PUBLIC -fexceptions)
endif()

if(APPLE)
find_library(SECURITY_FRAMEWORK Security)
find_library(FOUNDATION_FRAMEWORK Foundation)
mark_as_advanced(SECURITY_FRAMEWORK FOUNDATION_FRAMEWORK)

target_link_libraries(riscv PUBLIC
"${SECURITY_FRAMEWORK}"
"${FOUNDATION_FRAMEWORK}"
)
endif()

target_compile_features(riscv PUBLIC cxx_std_20)
target_include_directories(riscv PUBLIC .)
target_include_directories(riscv PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
Expand Down
29 changes: 28 additions & 1 deletion lib/libriscv/linux/syscalls_poll.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
#if defined(__APPLE__)
#include <time.h>
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
#else
#include <sys/time.h>
#endif

#include <poll.h>

int poll_with_timeout(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout_ts) {
#ifdef __linux__
return ppoll(fds, nfds, timeout_ts, NULL);
#elif __APPLE__
// On macOS, we don't have ppoll, so we need to use poll and then handle the timeout manually.
int timeout_ms;
if (timeout_ts != NULL) {
timeout_ms = timeout_ts->tv_sec * 1000 + timeout_ts->tv_nsec / 1000000;
} else {
timeout_ms = -1;
}
return poll(fds, nfds, timeout_ms);
#else
#error "Unknown compiler"
#endif
}

// int ppoll(struct pollfd *fds, nfds_t nfds,
// const struct timespec *timeout_ts, const sigset_t *sigmask);
template <int W>
Expand Down Expand Up @@ -30,7 +57,7 @@ static void syscall_ppoll(Machine<W>& machine)
linux_fds[i].fd = machine.fds().translate(fds[i].fd);
}

const int res = ppoll(linux_fds.data(), nfds, &ts, NULL);
const int res = poll_with_timeout(linux_fds.data(), nfds, &ts);
// The ppoll system call modifies TS
//clock_gettime(CLOCK_MONOTONIC, &ts);
//machine.copy_to_guest(g_ts, &ts, sizeof(ts));
Expand Down
Loading

0 comments on commit 64ba08c

Please sign in to comment.