Commit graph

1898 commits

Author SHA1 Message Date
Sayooj K Karun
cd7c78e92e lib: min_heap: Add min-heap data structure
Introduce a binary min-heap implementation as a generic data structure
for use in kernel and application code. A min-heap always maintains
the smallest element at the root, making insertion and removal of the
minimum element efficient (O(log n)).

The API allows both static and dynamic initialization, supports custom
comparators.

Signed-off-by: Sayooj K Karun <sayooj@aerlync.com>
2025-06-07 13:29:24 +01:00
Peter Mitsis
811302e6d2 kernel: sys_heap: Fix chunk size request validation
Updates the heap code to ensure that when converting the requested
number of bytes to chunks, we do not return a value that exceeds
the number of chunks in the heap.

Fixes #90306

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-06-06 10:12:19 +02:00
Anas Nashif
7bbb834deb Revert "posix: rwlock: Refactor locking using k_timepoint_t"
This reverts commit 833eaccfd1.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-05-30 08:27:19 -04:00
Anas Nashif
0a6e579987 Revert "posix: net: move static inline gethostname() to lib definition"
This reverts commit e60070d91b.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-05-30 08:27:19 -04:00
Anas Nashif
a649b31cfc Revert "posix: device_io: mark pwrite() buf as const"
This reverts commit c14f97f194.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-05-30 08:27:19 -04:00
Anas Nashif
da48a04572 Revert "posix: sysconf: match _SC* defines with newlib/picolib"
This reverts commit c5f1c43b8c.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-05-30 08:27:19 -04:00
Nicholas Lowell
c5f1c43b8c posix: sysconf: match _SC* defines with newlib/picolib
It's possible for newlib/picolib libc libraries to
internally call sysconf() which would execute zephyr's
implementation.  However, if the _SC* defines do not have
matching values, then the incorrect switch case executes.

This issue arises when using newlib/picolib libc that includes
sysconf implementation for ARM.  With current defaults, the
zephyr sysconf() overrides the original libc sysconf() so
we must ensure proper operation.

We will switch to the #define list just like newlib/picolib.
We can't currently use their unistd.h directly due to a domino
of declaration conflicts.
For the "small" macro implementation, we have to drop using
CONCAT to prevent pre-expansion of the new #defines

Signed-off-by: Nicholas Lowell <Nicholas.Lowell@lexmark.com>
2025-05-30 10:26:22 +02:00
Nicholas Lowell
c14f97f194 posix: device_io: mark pwrite() buf as const
match standard by having input void *buf parameter
for pwrite() marked as const, and avoid any potential
declaration conflicts

Signed-off-by: Nicholas Lowell <Nicholas.Lowell@lexmark.com>
2025-05-30 10:26:22 +02:00
Nicholas Lowell
e60070d91b posix: net: move static inline gethostname() to lib definition
static inline gethostname() in unistd.h can cause declaration collisions.
we should just move it to a normal function definition like the
rest of the network functions.

Signed-off-by: Nicholas Lowell <Nicholas.Lowell@lexmark.com>
2025-05-30 10:26:22 +02:00
Cla Mattia Galliard
833eaccfd1 posix: rwlock: Refactor locking using k_timepoint_t
Make use of k_timepoint_t ind the calculation of the remaining time for the
rwlock.

Signed-off-by: Cla Mattia Galliard <clamattia@gmail.com>
2025-05-30 10:26:02 +02:00
Valerio Setti
31e38c83b3 lib: uuid: do not support UUID_v5 on TF-M platforms
When TF-M is enabled, Mbed TLS's MD module (which is used to generate
v5 UUIDs) will dispacth hash operations to TF-M. Unfortunately TF-M
does not support SHA-1 (because it's a weak algorithm) so the
computation will fail.

Signed-off-by: Valerio Setti <vsetti@baylibre.com>
2025-05-27 10:29:34 +02:00
Keith Packard
0adac7cafc libc/minimal: Add stub 'sys/lock.h'
This file is explicitly included by the espressif hal module. It's an
internal file provided by picolibc and newlib. Provide a stub to let
code designed for those to work with the minimal C library.

Signed-off-by: Keith Packard <keithp@keithp.com>
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-05-23 17:41:40 -04:00
Chris Friedt
8a5c744213 posix: timers: use newly added timespec util functions
Use the newly added timespec util functions to manipulate and
compare timespec structures with overflow detection.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-05-22 23:57:17 +02:00
Benjamin Cabé
a3f658454a posix: clock: fix maybe-uninitialized warning in z_clock_gettime
Compiler gets confused and thinks base may be used uninitialized. This
shouldn't be possible, but to make the warning go away, initialize it.

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2025-05-14 15:18:57 +02:00
Chris Friedt
ae4a1dbf2c posix: implement the POSIX_CLOCK_SELECTION Option Group
Implement the POSIX_CLOCK_SELECTION Option Group.

This was mostly already done, but compiled / linked in the wrong places.

E.g. pthread_condattr_getclock() and pthread_condattr_setclock() were
in pthread.c and part of POSIX_THREADS_BASE. clock_nanosleep() was in
clock.c and part of POSIX_TIMERS.

This change builds them as part of clock_selection.c with
CONFIG_POSIX_CLOCK_SELECTION.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-05-13 22:23:21 -04:00
Chris Friedt
cfae041834 posix: implement the XSI_SINGLE_PROCESS Option Group
gettimeofday() was already implemented, but incorrectly lumped into
POSIX_TIMERS.

putenv() is really just a wrapper around setenv().

The only one left to implement was gethostid() which was relatively
trivial.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-05-13 22:23:21 -04:00
Chris Friedt
0b869efe6a posix: clock: move most implementations to clock_common.c
Move most implementations to clock_common.c in preparation for
moving gettimeofday() and clock_nanosleep() to different compilation
units.

We also take this as an opportunity to switch from using k_spinlock
to sys_sem.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-05-13 22:23:21 -04:00
Chris Friedt
73ee23e3ad posix: clock: create z_clock_gettime().. convenience functions.
To facilitate moving gettimeofday() and clock_nanosleep() to separate
compilation units, make z_clock_nanosleep(), z_clock_gettime(),
and z_clock_settime() convenience functions.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-05-13 22:23:21 -04:00
Chris Friedt
5c46fcb1e9 posix: env: move most implementations to env-common.c
Move most implementations to env-common.c in preparation for adding
putenv.c .

We also take this as an opportunity to switch from using k_spinlock
to sys_sem.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-05-13 22:23:21 -04:00
Chris Friedt
4137c489d8 posix: env: create z_getenv()..z_setenv() convenience functions
To facilitate adding putenv in a separate compilation unit,
make z_getenv(), z_getenv_r(), z_setenv(), and z_unsetenv()
convenience functions.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-05-13 22:23:21 -04:00
Alex Fabre
da2ecd3eb8 utils: bitarray: fix comparison of int of different signs
Clang 20.1.0 -Wsign-compare warning on int being compared with size_t
(unsigned int).

Signed-off-by: Alex Fabre <alex.fabre@rtone.fr>
2025-05-13 10:05:52 +02:00
Torsten Rasmussen
c79c4ef9a8 linker: move last section id constant to c-code
Move creation of last section id from ld linker script LONG() usage to
C code with last section attribute.

The use of `LONG()` works correctly with ld but lld emits a warning
because .last_section section is not allocated as there are no matching
input sections and discards the `LONG()` call, meaning the last section
identifier will not be present in the flash.
> ld.lld: warning: ignoring memory region assignment for
>                             non-allocatable section '.last_section'

Placing the last section id in `.last_section` in C code makes lld
allocate the memory for the id and thereby create the output section
with the correct output.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
2025-05-08 15:55:40 +02:00
Daniel Nicoletti
9bbccf39cb include: zephyr: sys: Fix uuid.h usage in C++ code
In uuid.h the function uuid_generate_v5 had a 'namespace'
parameter which is a reserved C++ keywork.
Renamed to 'ns'.

Signed-off-by: Daniel Nicoletti <dantti12@gmail.com>
2025-05-06 02:36:15 +02:00
Chris Friedt
76e1fc7713 tests: posix: move tv_to_ts to posix_clock.h
Move the private static inline function tv_to_ts() to posix_clock.h .

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-05-04 19:56:02 +02:00
Daniel Leung
d3dcf36897 libc: newlib: heap arean to support Xtensa MPU
This adds the bits to define HEAP_BASE when using Xtensa MPU.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-05-01 18:16:20 +02:00
Keith Packard
f075022bec libc/picolibc: Make lock typing match between Zephyr and picolibc
Picolibc's retargetable locking is based upon having the user own the lock
type (struct __lock, along with typedef struct __lock *_LOCK_T), and then
having the picolibc internal code only refer to this type via the _LOCK_T
pointer typedef, leaving the actual struct undeclared there.

Zephyr wants to use 'struct k_mutex' for this type; the initial picolibc
port handled this by trying to redefine the picolibc locking to use 'void
*' instead of 'struct __lock *' by including '#define _LOCK_T void
*'. Which 'works' as long as the Zephyr code doesn't actually include
picolibc's sys/lock.h.

A recent picolibc change to support POSIX stdio locking has picolibc's
stdio.h including sys/lock.h, which breaks Zephyr's hack.

To fix this, create a real 'struct __lock' type as

	struct __lock { struct k_mutex m; };

Define all of the required picolibc locking API with this real type,
referring to the mutex inside without needing any casts.

This required switching the definition of the C library global lock from
K_MUTEX_DEFINE to the open-coded version, STRUCT_SECTION_ITERABLE_ALTERNATE
so that it has the correct type and still lands in the same elf section.

The only mildly inappropriate code left is that lock are allocated using
k_object_alloc(K_OBJ_MUTEX), which "works" because the size of 'struct
__lock` will exactly match the size of 'struct k_mutex' because of C's
struct allocation rules.

Signed-off-by: Keith Packard <keithp@keithp.com>
2025-05-01 07:15:16 +02:00
Carles Cufi
7c15b08a7e lib: hash: murmur3: Account for unaligned 32-bit access
The code was casting a byte array to 32-bit words without accounting for
alignment. On some platforms (e.g. Arm Cortex-M with multiple load/store
instructions) this will fault. Fix it by using the UNALIGED_GET() macro
whenever the array is passed unaligned.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2025-04-29 17:54:51 +02:00
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