Skip to content

Commit 3094ffb

Browse files
committed
fix(zephyr): Introduce a os_nanosleep function:
- Declare it in the api_vm_extension for each platform - Increase the experimental functions doc - Create a os_nanosleep function in a new zephyr_sleep file - add it to the compilation process.
1 parent c8a7f00 commit 3094ffb

File tree

4 files changed

+204
-13
lines changed

4 files changed

+204
-13
lines changed

core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,7 +2135,7 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds,
21352135
// nanosleep(). This is incorrect, but good enough for now.
21362136
os_timespec ts;
21372137
convert_timestamp(in[0].u.u.clock.timeout, &ts);
2138-
// nanosleep(&ts, NULL);
2138+
os_nanosleep(&ts, NULL);
21392139
}
21402140
break;
21412141
case __WASI_CLOCK_REALTIME:
@@ -2163,7 +2163,7 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds,
21632163
// Relative sleeps can be done using nanosleep().
21642164
os_timespec ts;
21652165
convert_timestamp(in[0].u.u.clock.timeout, &ts);
2166-
// nanosleep(&ts, NULL);
2166+
os_nanosleep(&ts, NULL);
21672167
}
21682168
break;
21692169
default:

core/shared/platform/include/platform_api_extension.h

Lines changed: 95 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,28 +1669,112 @@ __wasi_errno_t
16691669
os_clock_time_get(__wasi_clockid_t clock_id, __wasi_timestamp_t precision,
16701670
__wasi_timestamp_t *time);
16711671

1672-
#ifdef __cplusplus
1673-
}
1674-
#endif
16751672

1676-
/* Experimental */
1673+
/****************************************************
1674+
* Section 5 *
1675+
* Experimental functions *
1676+
****************************************************/
16771677

1678-
/* Used in posix.c around L2259 and expect the return code
1679-
* of ioctl() directly.
1678+
/**
1679+
* NOTES:
1680+
* The bellow functions were defined after increasing the support for the
1681+
* Zephyr platform to get the full WASI libc running.
1682+
*
1683+
* If you don't need to support WASI libc, there is no need to implement these
1684+
* APIs.
1685+
*
1686+
* We could also move these definitions to the proper sections
1687+
* (File, Time, ...) but still keep them here until we get more feedback or
1688+
* observe edges cases.
1689+
*/
1690+
1691+
/**
1692+
* @brief Control device.
1693+
*
1694+
* The `ioctl` function was one of the POSIX function used without platform
1695+
* abastraction API in the `sandboxed-system-primitives` and particularly in the
1696+
* `wasmtime_ssp_poll_oneoff` function.
1697+
*
1698+
* @param handle A platform file handler.
1699+
* @param request A platform-dependent request code.
1700+
* @param argp Usually an untyped pointer to memory.
1701+
*
1702+
* @return
1703+
* __WASI_ESUCCESS On success
1704+
* __WASI_EBADF handle is not a valid file handler.
1705+
* __WASI_EFAULT argp references an inaccessible memory area.
1706+
* __WASI_EINVAL request or argp is not valid.
1707+
* __WASI_ENOTTY handle is not associated with a character special device.
1708+
* __WASI_ENOTTY The specified request does not apply to the kind of
1709+
* object that the file handler handle references.
1710+
*
1711+
* NOTE: We seem to only use/support the `FIONREAD` request code:
1712+
*
1713+
* FIONREAD Get the number of bytes in the input buffer.
16801714
*/
16811715
int
16821716
os_ioctl(os_file_handle handle, int request, ...);
16831717

1684-
/* Higher level API:
1685-
* __wasi_errno_t
1686-
* blocking_op_poll(wasm_exec_env_t exec_env, os_poll_file_handle *pfds,
1687-
* os_nfds_t nfds, int timeout_ms, int *retp)
1688-
* Already format the errno and expect the return code of poll() directly.
1718+
/**
1719+
* @brief Wait for some event on a file descriptor.
1720+
*
1721+
* For more context, the higher level API `blocking_op_poll`.
1722+
*
1723+
* __wasi_errno_t
1724+
* blocking_op_poll(wasm_exec_env_t exec_env, os_poll_file_handle *pfds,
1725+
* os_nfds_t nfds, int timeout_ms, int *retp)
1726+
*
1727+
* Format the error code (errno) and expect the return code of (POSIX) `poll()`
1728+
* directly hence the `int` return type.
1729+
*
1730+
* @param pfds A poll file descriptors array
1731+
* @param nfs The size of the poll file descriptors array
1732+
* @param timeout Specify the number of ms that poll is busy waiting.
1733+
*
16891734
*/
16901735
int
16911736
os_poll(os_poll_file_handle *pfds, os_nfds_t nfs, int timeout);
16921737

1738+
/**
1739+
* @brief Compare two platform's file handle/descriptor.
1740+
*
1741+
* This function should ALWAYS be used when comparaing file handlers because
1742+
* for some platforms (Windows, Zephyr, etc...) the `os_file_handle` type is
1743+
* a structure and not a primary type like for POSIX.
1744+
*
1745+
* @param handle1 First file handle or constant.
1746+
* @param handle2 Second file handle.
1747+
*
1748+
* @return
1749+
* true The file handlers are similar.
1750+
* false The file handlers don't match.
1751+
*/
16931752
bool
16941753
os_compare_file_handle(os_file_handle handle1, os_file_handle handle2);
16951754

1755+
1756+
/**
1757+
* @brief high-resolution sleep
1758+
*
1759+
* The `nanosleep` function was the last POSIX function used without platform
1760+
* abastraction API in the `sandboxed-system-primitives` and particularly in the
1761+
* `wasmtime_ssp_poll_oneoff` function.
1762+
*
1763+
* @param req time requiered to sleep.
1764+
* @param rem remaining time in the case that the function is interrupted.
1765+
*
1766+
* @return
1767+
* __WASI_ESUCCESS On success
1768+
* __WASI_EFAULT Problem with copying information.
1769+
* __WASI_EINTR The sleep has been interrupted.
1770+
* __WASI_EINVAL The req input is badly formed.
1771+
*/
1772+
__wasi_errno_t
1773+
os_nanosleep(os_timespec *req, os_timespec *rem);
1774+
1775+
1776+
#ifdef __cplusplus
1777+
}
1778+
#endif
1779+
16961780
#endif /* #ifndef PLATFORM_API_EXTENSION_H */

core/shared/platform/zephyr/shared_platform.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ if (NOT WAMR_BUILD_LIBC_WASI EQUAL 1)
1919
list(REMOVE_ITEM source_all ${PLATFORM_SHARED_DIR}/zephyr_socket.c)
2020
list(REMOVE_ITEM source_all ${PLATFORM_SHARED_DIR}/zephyr_file.c)
2121
list(REMOVE_ITEM source_all ${PLATFORM_SHARED_DIR}/zephyr_clock.c)
22+
list(REMOVE_ITEM source_all ${PLATFORM_SHARED_DIR}/zephyr_sleep.c)
2223
else()
2324
include (${CMAKE_CURRENT_LIST_DIR}/../common/libc-util/platform_common_libc_util.cmake)
2425
set(source_all ${source_all} ${PLATFORM_COMMON_LIBC_UTIL_SOURCE})
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright (C) 2024 Grenoble INP - ESISAR. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
#include "platform_api_extension.h"
7+
8+
// #include <zephyr/kernel.h>
9+
10+
/*
11+
* Assuming CONFIG_POSIX_API=n
12+
* Inspired zephyr/lib/posix/options/clock.c
13+
*/
14+
15+
__wasi_errno_t
16+
os_nanosleep(os_timespec *req, os_timespec *rem)
17+
{
18+
// __wasi_errno_t ret;
19+
20+
// if (req == NULL){
21+
// return __WASI_EINVAL;
22+
// }
23+
24+
// /*
25+
// * os_timespec is typedef'ed to struct timespec so it's one to one.
26+
// * Also sys_clock_nanosleep return either:
27+
// * * 0 on sucess
28+
// * * -EINVAL on failure
29+
// */
30+
// int rc = sys_clock_nanosleep(SYS_CLOCK_REALTIME, 0, req, rem);
31+
// if (0 > rc){
32+
// return __WASI_EINVAL;
33+
// }
34+
35+
return __WASI_ESUCCESS;
36+
}
37+
38+
/*
39+
* Don't exist in v3.7
40+
*
41+
* Inspired zephyr/lib/posix/options/clock.c on main
42+
*/
43+
44+
// int sys_clock_nanosleep(int clock_id, int flags, const struct timespec *rqtp,
45+
// struct timespec *rmtp)
46+
// {
47+
// k_timepoint_t end;
48+
// k_timeout_t timeout;
49+
// struct timespec duration;
50+
// const bool update_rmtp = rmtp != NULL;
51+
// const bool abstime = (flags & SYS_TIMER_ABSTIME) != 0;
52+
53+
54+
// /*
55+
// * Arguments checks
56+
// */
57+
// if((clock_id != SYS_CLOCK_MONOTONIC) &&
58+
// (clock_id != SYS_CLOCK_REALTIME)){
59+
// return __WASI_EINVAL;
60+
// }
61+
62+
// if((rqtp->tv_sec < 0) ||
63+
// (rqtp->tv_nsec < 0) ||
64+
// (rqtp->tv_nsec >= (long)NSEC_PER_SEC)){
65+
// return __WASI_EINVAL;
66+
// }
67+
68+
69+
// if (abstime) {
70+
// /* convert absolute time to relative time duration */
71+
// (void)sys_clock_gettime(clock_id, &duration);
72+
// (void)timespec_negate(&duration);
73+
// (void)timespec_add(&duration, rqtp);
74+
// } else {
75+
// duration = *rqtp;
76+
// }
77+
78+
// /* sleep for relative time duration */
79+
// if ((sizeof(rqtp->tv_sec) == sizeof(int64_t)) &&
80+
// unlikely(rqtp->tv_sec >= (time_t)(UINT64_MAX / NSEC_PER_SEC))) {
81+
// uint64_t ns = (uint64_t)k_sleep(K_SECONDS(duration.tv_sec - 1)) * NSEC_PER_MSEC;
82+
// struct timespec rem = {
83+
// .tv_sec = (time_t)(ns / NSEC_PER_SEC),
84+
// .tv_nsec = ns % NSEC_PER_MSEC,
85+
// };
86+
87+
// duration.tv_sec = 1;
88+
// (void)timespec_add(&duration, &rem);
89+
// }
90+
91+
// timeout = timespec_to_timeout(&duration, NULL);
92+
// end = sys_timepoint_calc(timeout);
93+
// do {
94+
// (void)k_sleep(timeout);
95+
// timeout = sys_timepoint_timeout(end);
96+
// } while (!K_TIMEOUT_EQ(timeout, K_NO_WAIT));
97+
98+
// if (update_rmtp) {
99+
// *rmtp = (struct timespec){
100+
// .tv_sec = 0,
101+
// .tv_nsec = 0,
102+
// };
103+
// }
104+
105+
// return 0;
106+
// }

0 commit comments

Comments
 (0)