Commit graph

1871 commits

Author SHA1 Message Date
Benjamin Cabé
f412cc643d posix: fix typo in pthread_cond_init
Update the casting of the 'attr' parameter in pthread_cond_init to use
the correct variable name 'att'. Thanks clang for spotting the typo.

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2025-04-25 19:03:28 +02:00
Andre Heinemans
fce576bfcf lib: open-amp: fix OPENAMP_COPY_RSC_TABLE dependancy
The config item OPENAMP_COPY_RSC_TABLE is only applicable when
OPENAMP_RSC_TABLE is enabled

Signed-off-by: Andre Heinemans <andre.heinemans@nxp.com>
2025-04-25 15:55:43 +02:00
Chris Friedt
e71c12c68f posix: refactor timespec_to_timeoutms() to use tp_diff()
Use the tp_diff() macro as a means of converting an absolute timeout
with respect to a specific clock to a relative timeout, in ms.

Clamp the result between 0 and UINT32_MAX.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-04-24 22:58:43 +02:00
Chris Friedt
89c1f15a6e posix: move timespec functions to posix_clock.h
Move somewhat useful (but private and internal functions) that deal
with struct timespec to posix_clock.h until there is a better API
available for dealing with operations on struct timespec.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-04-24 22:58:43 +02:00
Chris Friedt
d40be58093 posix: doc: hide internal posix_clock.h functions from doxygen
Prevent doxygen from parsing internal functions declared in
posix_clock.h .

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-04-24 22:58:43 +02:00
Chris Friedt
615ae7e1b5 posix: always require clockid_t argument to timespec_to_timeoutms()
Always require the clockid_t argument to timespec_to_timeoutms() and
remove the unused variant that accepts no clockid_t parameter.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-04-24 22:58:43 +02:00
Chris Friedt
a10f96e153 posix + tests: use CLOCK_REALTIME where specified by POSIX
Use CLOCK_REALTIME for the default clock source throughout
the POSIX implementation and tests so that we are
consistent with the specification.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-04-24 22:58:43 +02:00
Chris Friedt
e75f285bd1 posix: add timespec_is_valid() private internal function
Add a common private function timespec_is_valid() that
can be used to check if a timespec object is valid, and
use that consistently in lib/posix/options.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-04-24 22:58:43 +02:00
Chris Friedt
bb9ec32cf6 posix: cond: use clock specified via pthread_condattr_t
Use the clock specified via pthread_condattr_t in
pthread_cond_timedwait().

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-04-24 22:58:43 +02:00
Chris Friedt
8b60aa75fa posix: cond: check whether condattr is initialized in attr fns
Check whenther a pthread_condattr_t has been initialized in
pthread_condattr_*() functions.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-04-24 22:58:43 +02:00
Chris Friedt
ce7ae220a2 posix: cond: use struct posix_cond and struct posix_condattr internally
Use struct posix_cond and struct posix_condattr internally.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-04-24 22:58:43 +02:00
Chris Friedt
f208e3b02a posix: posix_internal: add definitions for posix_condattr, posix_cond
Add definitions for struct posix_condattr and struct posix_cond, which
are internal variants of the external pthread_condattr_t and
pthread_cond_t types.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-04-24 22:58:43 +02:00
Chris Friedt
241469af9a posix: provide timespec_to_clock_timeoutms
Provide a private internal function timespec_to_clock_timeoutms() to
complement timespec_to_timeoutms(). This new variant accepts a clock_t
parameter that allows the caller to specify which clock to use.

The original timespec_to_timeoutms() then just becomes a static inline
wrapper around the original.

Note: timespec_to_clock_timeoutms() and timespec_to_timeoutms() might
have a limited lifespan, since it might make sense to create a
common timespec manipulation library.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-04-24 22:58:43 +02:00
Chris Friedt
fffbd76683 posix: declare timespec_to_timeoutms() in posix_clock.h
Provide a single declaration of timespec_to_timeoutms() (which is
a private function), in the private header file posix_clock.h .

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-04-24 22:58:43 +02:00
Chris Friedt
0449324257 posix: semaphores: use a default minimal heap-add for semaphores
The implementation of POSIX_SEMAPHORES historically used heap allocation
and has not yet been transitioned to a pool allocator.

However, since 590258b381, the default heap-add with CONFIG_POSIX_API
has been reduced from 1 kiB which causes tests/posix/semaphores to fail
due to NULL being returned from a call to k_calloc().

Create a minimal heap-add for the POSIX_SEMAPHORES Option Group.

This can be removed at a future date if semaphores are changed to use
a pooled allocator and fixed-size name, rather than heap allocation.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-04-22 14:03:10 +02:00
Jordan Yates
590258b381 posix: options: POSIX_THREADS require a stack
`pthread_setspecific` requires a stack in order to allocate the
`struct pthread_key_data` data structure. On 64 bit systems this data
structure is 32 bytes, resulting in 160 bytes usage for the default 5
supported threads.

Signed-off-by: Jordan Yates <jordan@embeint.com>
2025-04-18 12:36:58 +02:00
Jordan Yates
3da69a809c posix: options: don't imply POSIX_MESSAGE_PASSING
Don't `imply POSIX_MESSAGE_PASSING` when `POSIX_API=y` as this option
has a non-trivial RAM implication in `HEAP_MEM_POOL_ADD_SIZE_MQUEUE`.

The `mq_*` API is minimally used in-tree, with all users already
enabling the symbol directly.

Signed-off-by: Jordan Yates <jordan@embeint.com>
2025-04-18 12:36:58 +02:00
Daniel Hajjar
69713f4e6a posix: net: Fix undefined behavior
The signedness of the variable caused undefined behavior because the
sign bit is modified when it gets left-shifted.

This fixes that by changing it to an unsigned variable.

Signed-off-by: Daniel Hajjar <daniel.hajjar16@gmail.com>
2025-04-17 09:05:56 +02:00
Chris Friedt
82d564a354 posix: timers: correct pointer passed to k_mem_slab_free()
If it is not possible to create a timer in timer_create(),
then the timer_obj associated with the timer must be freed.

However, the address of the pointer was mistakenly being
passed to k_mem_slab_free() rather than simply the
the pointer.

This caused a crash in tests which can easily be avoided
by passing the pointer rather than the address of the
pointer.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-04-16 08:04:36 +02:00
JP Hutchins
fe6366b804 lib: crc: add CRC-32K/4.2
This adds the best HD=4 CRC32 polynomial. The discovery
is the result of research by Philip Koopman of Carnegie
Mellon University, and is well documented at
https://users.ece.cmu.edu/~koopman/crc/.

The user is given the option of trading 1024B of RAM to
improve the execution speed. The unit tests are parameterized
with this KConfig option.

Signed-off-by: JP Hutchins <jp@intercreate.io>
2025-04-14 09:49:02 +02:00
Simone Orru
bb39048cc2 uuid: Add UUID utilities
Add UUID generation and parsing utilities compliant
with RFC9562.

Signed-off-by: Simone Orru <simone.orru@secomind.com>
2025-04-14 09:47:26 +02:00
Christoph Winklhofer
a5e295c452 json: improve parsing and serializing of integers
Add support for parsing and serializing of following integer types:
'int8_t', 'uint8_t', 'int16_t', 'uint16_t' and 'uint32_t'.

The generic integer token JSON_TOK_INT and JSON_TOK_UINT, in combination
with the field size (set by JSON_OBJ_DESCR_PRIM) allows to parse different
integer types, for example:

struct foo {
  int64_t i64;
  uint32_t u32;
  int16_t i16;
  uint8_t u8;
};

struct json_obj_descr foo_descr[] = {
  JSON_OBJ_DESCR_PRIM(struct foo, i64, JSON_TOK_INT),
  JSON_OBJ_DESCR_PRIM(struct foo, u32, JSON_TOK_UINT),
  JSON_OBJ_DESCR_PRIM(struct foo, i16, JSON_TOK_INT),
  JSON_OBJ_DESCR_PRIM(struct foo, u8, JSON_TOK_UINT),
};

These tokens also support parsing and serializing enums:

enum unsigned_enum { UA=0, UB=1, UC=2 };
enum signed_enum { SA=-1, SB=0, SC=1 };

struct foo {
  enum unsigned_enum u;
  enum signed_enum s;
};

struct json_obj_descr foo_descr[] = {
  JSON_OBJ_DESCR_PRIM(struct foo, u, JSON_TOK_UINT),
  JSON_OBJ_DESCR_PRIM(struct foo, s, JSON_TOK_INT),
};

Signed-off-by: Christoph Winklhofer <cj.winklhofer@gmail.com>
2025-04-11 06:32:50 +02:00
Christoph Winklhofer
63d33e631c json: support parsing and serializing of strings with char arrays
Support parsing and serializing of struct fields that are defined as a
char array.

Use the token JSON_TOK_STRING_BUF to parse and serialize a string for a
char array, for example:

struct foo {
  const char *str;
  char str_buf[30];
};

struct json_obj_descr foo_descr[] = {
  JSON_OBJ_DESCR_PRIM(struct foo, str, JSON_TOK_STRING),
  JSON_OBJ_DESCR_PRIM(struct foo, str_buf, JSON_TOK_STRING_BUF),
};

The struct 'json_obj_descr' now has an additional union member 'field'
to store the size of the struct field, which is used with the token
'JSON_TOK_STRING_BUF' to determine the element size.

Fixes: #65200
Signed-off-by: Christoph Winklhofer <cj.winklhofer@gmail.com>
2025-04-11 06:32:50 +02:00
Christoph Winklhofer
fc37c02eb1 json: improve parsing and serializing of 'float' and 'double'
Up to now, the handling of type float was offloaded to the users of the
JSON utility, with the token JSON_TOK_FLOAT.

Improve handling of floating point types and support the types 'float'
and 'double' in a built-in way so that they can be directly parsed to
and serialized from variables (of type float or double).

The types are serialized in the shortest representation, either as a
decimal number or in scientific notation:
  * float (with JSON_TOK_FLOAT_FP): encoded with maximal 9 digits
  * double (with JSON_TOK_DOUBLE_FP): encoded with maximal 16 digits
  * NaN, Infinity, -Infinity: encoded and decoded as:
    {"nan_val":NaN,"inf_pos":Infinity,"inf_neg":-Infinity}

Enable the floating point functionality with the Kconfig option:
  JSON_LIBRARY_FP_SUPPORT=y

It requires a libc implementation with support for floating point
functions: strtof(), strtod(), isnan() and isinf().

Fixes: #59412
Signed-off-by: Christoph Winklhofer <cj.winklhofer@gmail.com>
2025-04-09 22:05:14 +02:00
Christoph Winklhofer
c7b7ec2faf json: Support floating point values NaN and Infinity for numbers
Add support to decode the special floating point values NaN, Infinity
and -Infinity. For example:
  {"nan_val":NaN,"inf_pos":Infinity,"inf_neg":-Infinity}

Note that this commit is a preparation for the built-in support of
floating point values and these are only accepted when compiled with
the flag -DCONFIG_JSON_LIBRARY_FP_SUPPORT.

Signed-off-by: Christoph Winklhofer <cj.winklhofer@gmail.com>
2025-04-09 22:05:14 +02:00
Christoph Winklhofer
f07f2da8dc json: support scientific notation for numbers
Add support to decode floating point numbers in scientific notation,
e.g. 3.40282347e+38. Only the lower-case specifier 'e' is allowed.

Signed-off-by: Christoph Winklhofer <cj.winklhofer@gmail.com>
2025-04-09 22:05:14 +02:00
Christoph Winklhofer
061e345972 json: Fix calculation of object size
The calculation of the object size may be incorrect when the size of
a field is smaller than the struct alignment. When such a struct is
used in an array field, the decoded object contains wrong values.

The alignment influences the object size. For example the following
struct has a calculated object size of 8 bytes, however due to
alignment the real size of the struct is 12 bytes:

struct test_bool {
  bool b1; /* offset 0, size 1 */
           /* 3-byte padding */
  int i1;  /* offset 4, size 4 */
  bool b2; /* offset 8, size 1 */
           /* 3-byte padding */
};

This commit changes the object size calculation and computes the size
with the offset and size of the last field in the struct (rounded up
by the struct alignment).

Fixes: #85121
Signed-off-by: Christoph Winklhofer <cj.winklhofer@gmail.com>
2025-04-08 11:44:51 +02:00
Nicolas Pitre
11021cdd4f kernel: sys_heap: decouple realloc from aligned_realloc
When sys_heap_realloc() is expressed in terms of sys_heap_aligned_realloc()
it invokes a longer aligned allocation code path with an extra runtime
overhead even though no alignment is necessary.

Let's reference and invoke the aligned allocation code path only when an
actual aligned allocation is requested. This opens the possibility for
the linker to garbage-collect the aligning code otherwise.

Improve realloc documentation while at it.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-04-01 22:13:04 +02:00
Nicolas Pitre
457fa60bfc kernel: mempool: decouple simple alloc from aligned_alloc
When k_malloc() is expressed in terms of k_aligned_alloc() it invokes a
longer aligned allocation code path with an extra runtime overhead even
though no alignment is necessary.

Let's reference and invoke the aligned allocation code path only when an
actual aligned allocation is requested. This opens the possibility for
the linker to garbage-collect the aligning code otherwise.

Also bypass k_heap_malloc() and friends given they're invoked with
K_NO_WAIT. Go directly to sys_heap_*() instead to cut some more unneeded
overhead.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-04-01 22:13:04 +02:00
Nicolas Pitre
9da06456f2 kernel: kheap: decouple simple alloc from aligned_alloc
When k_heap_alloc() is expressed in terms of k_heap_aligned_alloc()
it invokes a longer aligned allocation code path with an extra runtime
overhead even though no alignment is necessary.

Let's reference and invoke the aligned allocation code path only when an
actual aligned allocation is requested. This opens the possibility for
the linker to garbage-collect the aligning code otherwise.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-04-01 22:13:04 +02:00
Andre Heinemans
ad65b81024 lib: open-amp: optional copy of rsc_table to memory region
Option OPENAMP_COPY_RSC_TABLE is added for cases where the placement of the
.resource_table section is not controlled by the remote. This option lets
zephyr do the copying of the .resource_table section to a specified memory
region.

Signed-off-by: Andre Heinemans <andre.heinemans@nxp.com>
2025-03-24 12:17:53 +01:00
Henrik Brix Andersen
4d01efbf90 lib: net_buf: remove deprecated net_buf_put() and net_buf_get() functions
Remove the deprecated net_buf_put() and net_buf_get() functions.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2025-03-12 19:04:19 +01:00
Chris Friedt
7623a18c2e posix: remove deprecated kconfig options
Remove deprecated Kconfig options. These were given an extra
release cycle of soak time, but generally were OK to remove
after Zephyr v4.0.0 was released.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-03-12 19:03:52 +01:00
Chris Friedt
4c28582224 posix: threads: use non-deprecated kconfig
MAX_PTHREAD_COUNT was deprecated in favour of
POSIX_THREAD_THREADS_MAX prior to v3.7.0.

Use CONFIG_POSIX_THREAD_THREADS_MAX going forward.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-03-12 19:03:52 +01:00
Chris Friedt
33fb827603 posix: key: do not use deprecated kconfig
Use CONFIG_POSIX_THREAD_KEYS_MAX instead of
CONFIG_MAX_PTHREAD_KEY_COUNT.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-03-12 19:03:52 +01:00
Alberto Escolar Piedras
ca5b7506e8 lib/os/fdtable: Remove ifdef on NATIVE_POSIX
This board does not exist anymore, so this is just dead code at this
point.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2025-03-12 02:27:36 +01:00
Nicolas Pitre
e1eead3925 ring_buffer: shrink size of struct ring_buf
Make struct ring_buf 12 bytes smaller by default. This comes with a 32KB
buffer size limit which covers almost all cases. The previous limit of
2GB can be restored with CONFIG_RING_BUFFER_LARGE=y.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-03-11 08:59:05 +01:00
Nicolas Pitre
b97f11a752 ring_buffer: simplify code some more
Some variable renaming made the code clearer, which in turn allowed for
minor optimizations.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-03-11 08:59:05 +01:00
Nicolas Pitre
fa73b8b061 ring_buffer: update Kconfig help text
Make it more informative and useful.

Buffer sizes that are a power of 2 didn't provide any advantage since
commit 099850e916 ("ring_buffer: the great simplification").

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-03-11 08:59:05 +01:00
Helmut Lord
f31b4a67be lib: utils: add consistent overhead byte stuffing
Implementation uses netbufs.

Signed-off-by: Helmut Lord <kellyhlord@gmail.com>
2025-03-11 05:36:49 +01:00
Jari Tervonen
3b1e60da90 lib: posix: Fix NULL pointer to strcmp in posix mqueue.
When a queue has been unlinked but not removed the name pointer is set to
NULL. The queue is still in the list when adding new queues and the name
pointer is passed to strcmp.

Signed-off-by: Jari Tervonen <jari.tervonen@nordicsemi.no>
2025-03-08 03:37:48 +01:00
Benjamin Cabé
0c2c0c4328 libc: add comment to empty sys/cdefs.h file
This documents the reason to exist for this otherwise odd-looking empty
header file.

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2025-02-21 15:13:20 +00:00
Chris Friedt
bca2ef672e posix: mem: remove imply MMU from POSIX options
With MMU being converted to a non-user-configurable option, we can
rely on that exclusively to know whether arch_mem_map() and
arch_mem_unmap() are available and we can remove the workarounds
in POSIX at the Kconfig level.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-02-19 04:59:33 +01:00
Damian Krolik
73802412a8 lib: os: mpsc_pbuf: take spinlock in utilization getters
Two public functions do not take the spinlock even though
they access mutable, non-atomic members of the buffer:
- mpsc_pbuf_get_utilization()
- mpsc_pbuf_get_max_utilization()

Take the spinlock to avoid possible data races.

Signed-off-by: Damian Krolik <damian.krolik@nordicsemi.no>
2025-02-18 13:30:29 +01:00
Luis Ubieda
ec45b29ea3 p4wq: Add Kconfig to perform early init on threads
In order to make them functional for devices during init. Default
behavior is to keep late initialization, as before.

Signed-off-by: Luis Ubieda <luisf@croxel.com>
2025-02-18 05:32:58 +01:00
Robin Kastberg
9ab06ec667 toolchain: iar: Add experimental IAR support
This adds experimental support for the IAR toolchain.

Signed-off-by: Robin Kastberg <robin.kastberg@iar.com>
2025-02-14 19:12:44 +00:00
Keith Packard
dc0fd3af17 picolibc: Use common abort(), call from assert when !__ASSERT_ON
Switch to the common abort implementation so that we can use it
from the assert hooks to avoid undefined behavior.

Closes: 84824

Signed-off-by: Keith Packard <keithp@keithp.com>
2025-02-14 10:42:16 +01:00
Florian Weber
093b29fdb5 lib: os: p4wq: add done handler
Add an optional handler to the p4wq to give the submitting code
(e.g. rtio workq) a possibility execute code after the work was
succesfully executed.

Signed-off-by: Florian Weber <Florian.Weber@live.de>
2025-02-12 16:03:17 +01:00
Yuval Peress
a455440570 cpp: Add c++ version number
Several downstream libraries need to specify that they work with c++ 17+
or 20+ and there's no easy way to do this without constantly updating
the Kconfig file and supporting different Kconfig versions for different
Zephyr versions (since I can't add a dependency on a new c++ standard
and have it backwards compatible.

Signed-off-by: Yuval Peress <peress@google.com>
2025-02-10 15:57:26 +00:00
Marek Matej
65d2139dc6 lib: libc: malloc.c: fix calculation of sys heap
Change the condition so that ESP32 SoCs are using `_heap_sentry` to
get actual run-time heap size.

Signed-off-by: Marek Matej <marek.matej@espressif.com>
2025-02-05 17:49:54 +01:00