Commit graph

1871 commits

Author SHA1 Message Date
Chris Friedt
f4259c384f posix: options: add xsi realtime option group
Add a POSIX Option Group called XSI_REALTIME (with Kconfig
option CONFIG_XSI_REALTIME).

When XSI_REALTIME is selected (or when required POSIX Options
are enabled), define _XOPEN_REALTIME to be something other
than -1 (_XOPEN_VERSION seemed appropriate).

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-02-03 09:05:09 +01:00
Chris Friedt
d08d63676b posix: options: split kconfig.xsi into separate option groups
Since XSI is composed of several distinct POSIX Option Groups
split Kconfig.xsi into separate files - one for each Option
Group.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-02-03 09:05:09 +01:00
Chris Friedt
0ac931b386 posix: options: profiles: uncomment options that should be selected
For the REALTIME_MINIMAL Application Environment Profile, uncomment
the POSIX_MEMLOCK, POSIX_MEMLOCK_RANGE, POSIX_MEMORY_PROTECTION,
POSIX_MAPPED_FILES, and POSIX_SHARED_MEMORY_OBJECTS options.

These should have been uncommented back when Kconfig options for
those features were added, so this can probably be called a
Kconfig bug.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-02-03 09:05:09 +01:00
Chris Friedt
8ab6e29cf1 posix: shm: ensure addend is less than INTPTR_MAX
Although it's quite unlikely that we will see shared memory
objects in practice that are greater than 2GiB, there is a
possibility of integer overflow when comparing intptr_t and
size_t, since the former is signed and the latter is unsigned.

Explicitly check to ensure that the addend is less than
INTPTR_MAX before subtraction.

This fixes CID 487734

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-01-30 20:27:59 +01:00
Chris Friedt
f81293d0dc posix: semaphore: explicitly ignore return of k_mutex_unlock()
The nsem_list_unlock() static inline function looks as though it
was designed to always ignore the return value of
k_mutex_unlock(), presumably for performance reasons.

A quick audit of the code looks as though the call should
always succeed.

Prepend (void) to the call to avoid the coverity defect in the
future.

CID 444386

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-01-30 20:27:48 +01:00
Krzysztof Chruściński
3e52109590 lib: os: mpsc_pbuf: Fix infinite loop during allocation
It was possible that allocation was continuously attempting to find
space. It could happen if allocation interrupted consuming a packet
(which is marked as busy) and there is not enough space to allocate
requested packet but there would be if busy packet was freed.
Algorithm in that case was continuously going through the buffer
in search for packets that can be dropped to find space for the
new packet.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
2025-01-29 17:55:56 +01:00
Pisit Sawangvonganan
e3a4a16594 lib: fix typo in multiple directories
Utilize a code spell-checking tool to scan for and correct spelling errors
in various files within the `lib` directory.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2025-01-28 00:06:18 +01:00
Joel Holdsworth
1b399d78fc picolibc: Replace hard coded -fno-lto flag
GCC versions before 4.5 do not have the -fno-lto flag and give an error if
it is used. The flag may also be unavailable on various other non-GCC
compilers.

Previously, the picolibc CMakeList.txt script assumed that the flag was
available, and hard-coded it as an addition to the compiler flags.

This patch improves compatibility by making use of the already existing
"prohibit_lto" CMake target compiler property, which is set to the
appropriate flag to disable LTO support, or unset if the compiler lacks
such an option.

Signed-off-by: Joel Holdsworth <jholdsworth@nvidia.com>
2025-01-27 13:24:52 +01:00
Nicolas Pitre
3075a7d906 ring_buffer: factorize almost identical code
Factorize almost identical code. May even improve performance due to
CPU cache locality.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-01-25 01:40:03 +01:00
Nicolas Pitre
83279d1524 ring_buffer: optimize the partial buffer loop
It is more efficient to break early when size of claimed area becomes zero.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-01-25 01:40:03 +01:00
Nicolas Pitre
67978f8ffc ring_buffer: delete redundant code
No need to clamp requested size as ring_buf_get_claim() does it already.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-01-25 01:40:03 +01:00
Geoffrey Hunter
fa795f4912 lib: smf: Fix handled bug causing events to not propagate.
When using the SMF for a project discovered that events would sometimes
 not propagate to parent states correctly. Could not create a minimum
reproducable test case for this, but it was found that these changes fixed
the bug. This commit creates a new function to reset internal state,
which is called on entry to smf_set_initial() and smf_set_state().

Closes #81300.

Signed-off-by: Geoffrey Hunter <gbmhunter@gmail.com>
2025-01-23 19:24:08 +01:00
Ivan Pankratov
257d9d45ba kernel: sys_heap: stats: save heap pointers to an array during init
To request heap statistics, a pointer to a heap structure is required.
This is straightforward for a user-defined heap. However, such a pointer
is not known for heaps created by other components or libraries, like
libc. Therefore, it is not possible to calculate the total heap memory.

The proposed solution is to use an array of pointers, which is filled in
on every sys_heap_init() call. One can then iterate through it to sum up
the total memory allocated for all heaps.

The array size is configurable. The default array size is zero,
which means the feature is disabled. Any other integer greater then zero
defines the array size and enables the feature.

A list of pointers instead of an array could be another approach,
but it requeres a heap, which is not always available.

Signed-off-by: Ivan Pankratov <ivan.pankratov@silabs.com>
2025-01-23 16:37:33 +01:00
Håvard Reierstad
e13e893dfc net: buf: revert disallowing blocking in syswq
Reverts the change made in d4d53010f00cadbb4f89c6d41391937646fc1740
(The changes was moved to another file in a restructuring)

The commit incorrectly assumed that no blocking would be allowed in
the syswq. This has caused issues observed among others
in #77241 and #80167

Signed-off-by: Håvard Reierstad <haavard.reierstad@nordicsemi.no>
2025-01-23 10:14:59 +01:00
Chris Friedt
dba7598517 posix: options: shm: use truncation flag that has been added
Originally, when the POSIX_SHARED_MEMORY_OBJECTS option was
added, the O_TRUNC flag was not consistently available.

Now that it is consistently available, ensure it is used
within the shm_open() function.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-01-23 00:12:58 +01:00
Maochen Wang
f36e2e0664 lib: net_buf: support counting max used buf
Support counting the max used net_buf when CONFIG_NET_BUF_POOL_USAGE
is defined.

Signed-off-by: Maochen Wang <maochen.wang@nxp.com>
2025-01-20 11:16:18 +01:00
Chris Friedt
8609a05236 posix: options: mlock: refine imply for MMU and DEMAND_PAGING
POSIX mlock() and munlock() require an MMU as well as
DEMAND_PAGING.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-01-11 18:31:02 +01:00
Daniel Leung
f81add2f65 posix: pin init functions and data for demand paging
Boot time initialization functions and data used there must be
available at boot. With demand paging, these may not exist in
memory when they are being used, resulting in page faults.
So pin these functions and data in linker sections to make
sure they are in memory at boot time.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-01-11 04:36:38 +01:00
Nicolas Pitre
46aa6717ff Revert "arch: deprecate _current"
Mostly a revert of commit b1def7145f ("arch: deprecate `_current`").

This commit was part of PR #80716 whose initial purpose was about providing
an architecture specific optimization for _current. The actual deprecation
was sneaked in later on without proper discussion.

The Zephyr core always used _current before and that was fine. It is quite
prevalent as well and the alternative is proving rather verbose.
Furthermore, as a concept, the "current thread" is not something that is
necessarily architecture specific. Therefore the primary abstraction
should not carry the arch_ prefix.

Hence this revert.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-01-10 07:49:08 +01:00
Chris Friedt
d58a5ce580 posix: options: mmap: disable xtensa support due to linker issue
The Xtensa linker scripts seem to be injecting syntax errors when
MMU is enabled. Disable the implication in Kconfig.mem for Xtensa
until linker issues are resolved.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-01-09 15:50:26 +01:00
Chris Friedt
cee9166dee posix: options: mlockall: include toolchain header
Include the toolchain header because ARG_UNUSED() is
not defined.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-01-09 15:49:57 +01:00
Chris Friedt
0acea8d5e6 posix: options: mprotect: include toolchain header
Include zephyr/toolchain.h to get access to ARG_UNUSED().

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-01-09 15:49:42 +01:00
Chris Friedt
587490d6db posix: options: mlock: include demand paging header
Fix a compile error because `k_mem_pin()` and `k_mem_unpin()`
are not defined.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-01-09 15:49:32 +01:00
Chris Friedt
2423219ea1 posix: include: dirent: rework the dirent.h header
Declare standard functions and split type definitions into
sys/dirent.h .

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-01-08 01:29:06 +01:00
Chris Friedt
fa841fe7bd posix: fs: declare _POSIX_C_SOURCE in a consistent way
Declare _POSIX_C_SOURCE in a consistent way for both the
posix/options library as well as the tests/posix/fs
testsuite.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-01-08 01:29:06 +01:00
Yong Cong Sin
1a578eb72c lib: os: mpsc_pbuf: do not wait when spinlock is held
Check if the spinlock is held before attempting to wait by
taking the semaphore, as that would cause a context switch which
isn't allowed and will trigger an assertion error when
`CONFIG_SPIN_VALIDATE` is enabled.

Logging in spinlock-held context when the log buffer is full can lead
to an infinite assertion error loop, as the logging subsys attempts to
allocate buffer when there's none available, it will try to wait for
one and thus triggers the assertion error, the error message will be
printed through the logging sybsys but there's no buffer available,
so it will try to wait for one and triggers another assertion error..
This loop just goes on and on forever, and nothing gets printed to
the terminal.

Added a test to validate the fix.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
Signed-off-by: Maxim Adelman <imax@meta.com>
2025-01-06 06:53:52 +01:00
Chris Friedt
68c0ff4e85 posix: fcntl.h: define constants in terms of zvfs constants
Define fcntl.h constants in terms of zvfs constants.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-01-05 09:58:54 +01:00
Chris Friedt
e08cc508e1 os: fdtable: set offset to zero with newly reserved fd's
Previously, if a file object is was re-used, it could
inherit the offset field of the previously closed file object,
making reading from the beginning of the file impossible
until the offset was manually zero'ed.

The offset should *always* be zero when a file is ready to be
used.

The issue really only presents itself when implementing a
vtable backend.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-01-05 09:58:19 +01:00
Chris Friedt
2a2806bb40 os: fdtable: restore errno if optional locking ioctl fails
The `zvfs_finalize_typed_fd()` function notifies some backends
via `ioctl()` with `ZFD_IOCTL_SET_LOCK`. However, support for
this method and functionality is optional.

In backends that do not support locking, this benign failure can
set `errno` to 95 (`EOPNOTSUPP`) in many circumstances where a
change in `errno` (indicating some kind of failure) is not
appropriate.

Prevent errno poisoning by backing-up and restoring `errno`.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-01-05 09:57:49 +01:00
Chris Friedt
ba8025fcd4 newlib: correct signature of _open() for xtensa
Previously, in libc-hooks.c, the signature of `_open()` was as
shown below.

```cpp
int _open(const char *name, int mode);
```

This conflicted with the signature of `_open()` from newlib,
which is

```cpp
int _open(const char *name, int flags, ...);
```

Moreover, the mode and flags field were reversed, but only for
the Xtensa architecture due to the `_open_r()` hook that is
present in `libc-hooks.c`.

This manifested itself via a call to `fopen(file, "w+")`, where
the expected flags should include `O_CREAT | O_TRUNC`, or
`0x200 | 0x400`. Instead, the unexpected flags passed to the
underlying `zvfs_open()` call were `0x1b6`.

This change corrects the function signature and order of the
arguments.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-01-05 09:56:54 +01:00
Chris Friedt
60928a55fc posix: options: remove default setting of toolchain option
Do not set TC_PROVIDES_POSIX_C_LANG_SUPPORT_R as the default in
lib/posix/options/Kconfig.c_lang_r . TC_PROVIDES options are
only intended to be set by C libraries that implement parts of
the POSIX standard.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-01-04 00:49:03 +01:00
Chris Friedt
6b103837a5 posix: options: file_system_r: include sys/util.h header for MIN
Oddly, even though CI passed when the file_system_r change was
merged, now CI has encountered a build error because MIN() was
not defined.

Include `<zephyr/sys/util.h>` to pull in the definition.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-01-02 19:53:29 +01:00
Chris Friedt
60ef84a48e posix: options: kconfig: fix typo informnation -> information
A copy-paste error propogated this typo to a few different
Kconfig files.

Correct 'informnation' to 'information'.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-01-02 19:47:36 +01:00
Chris Friedt
bc5aff3582 posix: options: fs: separate file_system_r to its own file
Move the functionality of POSIX_FILE_SYSTEM_R to its own
compilation unit and remove the unnecessary dependency on
POSIX_FILE_SYSTEM.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-01-02 09:18:57 +01:00
Chris Friedt
00a8818a71 posix: options: fs: move posix_fs_desc to fs_priv.h
Move struct posix_fs_desc to fs_priv.h

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-01-02 09:18:57 +01:00
Keith Packard
7962dcb288 picolibc: Disable LTO while building picolibc bits
The compiler requires that much of the C library be built without using LTO
so that various symbols are available for use by generated code, including
things like memset and memcpy.

Add -fno-lto when building both picolibc itself as well as the Zephyr
interface bits.

Closes: #81674

Signed-off-by: Keith Packard <keithp@keithp.com>
2024-12-20 00:49:56 +01:00
Robert Lubos
982402a99d posix: Move POSIX configs out of experimental
Many out of the POSIX subsystem configs are enabled automatically
when merely CONFIG_POSIX_API is enabled, which is a prerequisite for
many networking samples. This causes a massive experimental warning
printout when building with warnings enabled.

Since the new POSIX Kconfig configuration options are already present
in Zephyr for 2 release cycles and seem settled, I suggest we move
them out of experimental phase.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-12-19 17:37:40 +01:00
Cla Galliard
a71e0f2a29 posix: pthread: implement non-standard try-join and timed-join
These functions can be used to join pthreads in a non-standard way.
The function pthread_tryjoin will not block and simply test whether the
thread has exited already. The function pthread_timed_join will only block
until the specified time. The functions are wrappers for calling the
k_thread_join with timeout K_NO_WAIT and with a specific timeout as opposed
to calling it with K_FOREVER.

Signed-off-by: Cla Galliard <clamattia@gmail.com>
2024-12-15 19:24:33 +01:00
Fin Maaß
a9a475e525 lib: libc: newlib: Don't replace newlib libc time _r functions
Select TC_PROVIDES_POSIX_C_LANG_SUPPORT_R to keep Zephyr from
including the common libc implementation of the various _r APIs.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
2024-12-10 08:06:25 +01:00
Fin Maaß
c221496f14 lib: libc: select POSIX_C_LANG_SUPPORT_R
also select POSIX_C_LANG_SUPPORT_R, so we can check
that, to see if gmtime_r() is available.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
2024-12-10 08:06:25 +01:00
Fin Maaß
0418771902 lib: posix: set default of POSIX_C_LANG_SUPPORT_R
enable POSIX_C_LANG_SUPPORT_R by default if
the functions are already provided by the toolchain.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
2024-12-10 08:06:25 +01:00
Kai Vehmanen
4b49d7cb23 lib: os: p4wq: fix K_P4WQ_DELAYED_START mode
When the PRESTART thread state was removed, this changed the semantics
of k_thread_start() when thread was created with a K_FOREVER timeout,
suspended and then started with k_thread_start().

This sequence is used in p4wq to implement K_P4WQ_DELAYED_START
(which again is needed by K_P4WQ_USER_CPU_MASK).

With PRESTART removed, the following sequence:
  z_mark_thread_as_not_suspended(thread);
  k_thread_start(thread);

.. no longer starts the thread. As a result, p4wq users like SOF
multicore configurations, hit errors as p4wq threads never start.

Fix the implementation by removing the calls to change thread
suspended state explicitly, but rather rely on the new
k_thread_create() and k_thread_start() semantics.

Fixes: 7cdf40541b ("kernel/sched: Eliminate PRESTART thread state")
Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
2024-12-04 22:26:38 -05:00
James Roy
c875582443 lib: posix: Fix unchecked return value in rwlock
Fix 'sys_sem_xxx' functions unchecked return value
scanned by Coverity.

Signed-off-by: James Roy <rruuaanng@outlook.com>
2024-12-04 02:03:20 +01:00
Ilya Tagunov
bedfa5788d libc: arcmwdt: increase the number of preallocated locks
Increase the number of locks to match what the ARC MWDT libc requires
now. The library wants to have 2 locks per each available FILE entry,
and then some more. Also do not include an internal libc header, as
all that is needed from that header is a simple typedef.

Signed-off-by: Ilya Tagunov <Ilya.Tagunov@synopsys.com>
2024-12-03 02:37:16 +01:00
Ilya Tagunov
0afae55b4f libc: arcmwdt: replace _write if not provided by the POSIX subsystem
If CONFIG_POSIX_API is enabled, but CONFIG_POSIX_DEVICE_IO_ALIAS_WRITE
is not, the _write replacement should be provided to make stdio work.

Signed-off-by: Ilya Tagunov <Ilya.Tagunov@synopsys.com>
2024-12-03 02:37:16 +01:00
Ilya Tagunov
07b4d3a297 posix: clock: partially reformat __z_clock_nanosleep
Apply clang-format to the offending lines to make it happy.

Signed-off-by: Ilya Tagunov <Ilya.Tagunov@synopsys.com>
2024-11-28 15:41:32 +00:00
Ilya Tagunov
4c56eb6f65 posix: clock: fix potential int32_t overflow in __z_clock_nanosleep
As k_sleep returns int32_t, there is a possibility for integer overflow
during conversion from milliseconds to nanoseconds.

Signed-off-by: Ilya Tagunov <Ilya.Tagunov@synopsys.com>
2024-11-28 15:41:32 +00:00
Ilya Tagunov
bf525851e8 posix: clock: fix 32-bit time_t overflow in __z_clock_nanosleep
Nanosecond time calculation overflows if the libc has 32-bit time_t.
One such libc is the classic ARC MWDT one, but there might be others.

Signed-off-by: Ilya Tagunov <Ilya.Tagunov@synopsys.com>
2024-11-28 15:41:32 +00:00
Jakub Michalski
044b702b90 posix: fix pthread thread specific data cleanup
It was never deallocationg pthread_key_data nor it was removing the node
with it from list

Signed-off-by: Jakub Michalski <jmichalski@antmicro.com>
2024-11-25 08:31:08 +01:00
Yong Cong Sin
b1def7145f arch: deprecate _current
`_current` is now functionally equals to `arch_curr_thread()`, remove
its usage in-tree and deprecate it instead of removing it outright,
as it has been with us since forever.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
2024-11-23 20:12:24 -05:00