Commit graph

1698 commits

Author SHA1 Message Date
Chris Friedt
4edb9017c2 posix: net: add kconfig option and inet_addr()
Add a Kconfig option for POSIX_NETWORKING and implement
inet_addr().

Signed-off-by: Chris Friedt <chrisfriedt@gmail.com>
2024-03-27 07:36:18 -04:00
Christopher Friedt
1847280fb4 posix: rwlock: implement pthread_rwlockattr_setpshared
Implement pthread_rwlockattr_setpshared() and
pthread_rwlockattr_getpshared().

Both functions are required by the _POSIX_READER_WRITER_LOCKS
Option as detailed in Section E.1 of IEEE-1003.1-2017.

The _POSIX_READER_WRITER_LOCKS Option is required for PSE51,
PSE52, PSE53, and PSE54 conformance, and is otherwise mandatory
for any POSIX conforming system as per Section A.2.1.3 of
IEEE-1003-1.2017.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-03-22 16:19:57 -04:00
Christopher Friedt
61c4139925 posix: rwlock: pthread_rwlockattr_init / destroy in lib
Move the implementation of the following functions into
the posix library rather than having themn as static
inline functions.

* pthread_rwlockattr_init()
* pthread_rwlockattr_destroy()

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-03-22 16:19:57 -04:00
Christopher Friedt
d36dd8097e posix: clock: implement clock_getres()
clock_getres() is required by the POSIX_TIMERS Option Group
as detailed in Section E.1 of IEEE-1003.1-2017.

The POSIX_TIMERS Option Group is required for PSE51, PSE52,
PSE53, and PSE54 conformance, and is otherwise mandatory for
any POSIX conforming system as per Section A.2.1.3 of
IEEE-1003-1.2017.

With this, we have complete support for the POSIX_TIMERS
Option Group.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-03-22 16:19:27 -04:00
Christopher Friedt
c9edda0fbf posix: unistd: support for confstr()
Support querying POSIX string configuration values (similar to
sysconf()).

confstr() is required by the POSIX_SINGLE_PROCESS Option
Group as detailed in Section E.1 of IEEE-1003.1-2017 and has
been part of the specification since POSIX-2.

The POSIX_SINGLE_PROCESS Option Group is required for PSE51,
PSE52, PSE53, and PSE54 conformance, and is otherwise mandatory
for any POSIX conforming system as per Section A.2.1.3 of
IEEE-1003-1.2017.

With this, we have complete support for the POSIX_SINGLE_PROCESS
Option Group.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-03-22 08:57:44 +01:00
Gaetan Perrot
669edc0d5e posix: Implement set and get scope APIs for pthread attr
Implement `pthread_attr_setscope()` and `pthread_attr_getscope()`
are required as part of _POSIX_THREAD_PRIORITY_SCHEDULING Option Group.

signed-off-by: Gaetan Perrot <gaetanperrotpro@gmail.com>
2024-03-18 10:15:53 +01:00
Matthias Alleman
b0f9f16f14 lib: posix: shell: Fix compiler warning of lib__posix__shell
This PR fixes the following compiler warning:
No SOURCES given to Zephyr library: lib__posix__shell

Signed-off-by: Matthias Alleman <matthias.alleman@basalte.be>
2024-03-15 06:30:31 -04:00
Christopher Friedt
cdc36d090c posix: sysconf: add a "small" implementation option
Previously, sysconf() was only available as a macro (i.e. the
"extra-small" option).

This has the advantage of being compile-time constant, and
optimized for both space and speed. One disadvantage is that
querying an `_SC_` value that was invalid or unsupported
would result in a compile error.

Provide a "small" implementation of sysconf() (via Kconfig
choice) as a normal addressable function.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-03-12 17:57:11 +00:00
Gaetan Perrot
6badcad883 posix: Implement getmsg and getpmsg
`getmsg()` and `getpmsg()` are required
as part of _XOPEN_STREAMS Option Group.

signed-off-by: Gaetan Perrot <gaetanperrotpro@gmail.com>
2024-03-11 14:25:26 +01:00
Jukka Rissanen
71fd9b2e07 posix: syslog: Fix uninitialized variable error
Seen this in compiler

lib/posix/options/syslog.c: In function 'setlogmask':
lib/posix/options/syslog.c:66:16: error: 'oldpri' may be used uninitialized
   66 |         return oldpri;
      |                ^~~~~~
lib/posix/options/syslog.c:59:13: note: 'oldpri' was declared here
   59 |         int oldpri;
      |             ^~~~~~
lib/posix/options/syslog.c: In function 'vsyslog':
lib/posix/options/syslog.c:83:33: error: 'mask' may be used uninitialized
   83 |         if ((BIT(level) & mask) == 0) {
      |             ~~~~~~~~~~~~~~~~~~~~^~~~
lib/posix/options/syslog.c:71:17: note: 'mask' was declared here
   71 |         uint8_t mask;
      |                 ^~~~

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2024-03-10 07:44:03 -04:00
Christopher Friedt
529023e6f8 posix: manually define _POSIX_C_SOURCE in lib/posix
This PR adds declarations for the application conformance
feature test macro _POSIX_C_SOURCE.

It needs to be defined to value greater than or equal to 200112L
by the appplication.

However, Zephyr currently does not have a simple and consistent
means of specifying this value for POSIX samples, tests,
applications, and other libraries.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-03-08 04:28:47 -05:00
Christopher Friedt
447fc24352 posix: shell: add an environment variable shell command
This service is used to get, set, and unset system
environment variables.

Note: shell parameter expansion is not supported
at this time.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-03-08 04:28:47 -05:00
Christopher Friedt
0f23818153 posix: env: support for environ, getenv(), setenv(), unsetenv()
Support getting and setting POSIX environment variables.

Additionally, the thread-safe BSD variant getenv_r() is
provided.

environ, getenv(), setenv(), and unsetenv() are required by
the POSIX_SINGLE_PROCESS Option Group as detailed in
Section E.1 of IEEE-1003.1-2017.

The POSIX_SINGLE_PROCESS Option Group is required for PSE51,
PSE52, PSE53, and PSE54 conformance, and is otherwise mandatory
for any POSIX conforming system as per Section A.2.1.3 of
IEEE-1003-1.2017.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-03-08 04:28:47 -05:00
Florian La Roche
0d5a670f4f lib/os/printk.c: use ARG_UNUSED() for char_out()
Use ARG_UNUSED() within the function char_out()
to handel unused param.

Signed-off-by: Florian La Roche <Florian.LaRoche@gmail.com>
2024-03-06 12:11:26 +01:00
Glenn Andrews
0569809c80 Lib: SMF: Add initial transition and smf_set_handled()
Brings SMF framework closer into alignment with accepted Hierarchical State
Machine operation by:
1. Allowing 'programming by difference' by having some child states handle
   events and prevent propagation up to the parent run actions while others
   propagate events up to a common handler in a parent state.
2. Optionally allow initial transitions within a parent state to determine
   the most nested child state to transition to.
3. Adding a test case for `CONFIG_SMF_INITIAL_TRANSITION` and
   `smf_set_handled()`
4. Updating documentation for the new API (and fixing some references)

There was discussion in https://github.com/zephyrproject-rtos/zephyr/issues/55344
about not making the initial transition a Kconfig option, but I'm not sure
of any way else of doing it without permanently adding a pointer to each
`smf_state` entry, which is a problem for resource-constrained devices.

This does not fix https://github.com/zephyrproject-rtos/zephyr/issues/66341
but documentation has been updated to warn users of the issue.

Signed-off-by: Glenn Andrews <glenn.andrews.42@gmail.com>
2024-03-04 22:16:40 +01:00
Radoslaw Koppel
118f1592ff buildsystem: Enable LTO also for the application
It turns out that currently LTO is enabled only for the kernel.
This commit updates it to enable it for the whole application
and adds additional LTO exclusions required for the standard
C libraries to build and link properly.

Signed-off-by: Radosław Koppel <radoslaw.koppel@nordicsemi.no>
2024-03-02 15:39:36 +01:00
Christopher Friedt
3593720080 posix: syslog: add support for syslog
This is just a simple wrapper around Zephyr's native log api.

Note: the standard LOG_ERR syslog priority does conflict with
Zephyr's LOG_ERR() macro. This will need to be worked-around on
a case-by-case basis.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-02-29 02:45:01 -05:00
Robert Lubos
0966be01fc libc: Move gmtime_r into common
gmtime_r() has been in the minimal libc for years, however it was not
added to expcetions due to an overlook. In order to do this however, it
has to be moved first to the common libc area, so that it's available
to any libc that may not implement it.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-02-28 00:54:42 +09:00
Christopher Friedt
ec27e8875d posix: mutexattr: improvements for pthread_mutexattr_t
Previously, pthread_mutexattr_gettype() and
pthread_mutexattr_settype() were non-conformant and also
less safe, as they would not check whether a pthread_mutexattr_t
had been initialized prior to manipulating them. Furthermore,
they would potentially dereference NULL pointers.

Additionally, move the pthread_mutexattr_init() and
pthread_mutexattr_destroy() functions to the library, and add
some level of checking to them so that they are more than simply
static inline / no-op calls.

Lastly, reduce the size of struct pthread_mutexattr to only
what is necessary (one byte should suffice).

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-02-27 06:11:03 -05:00
Christopher Friedt
1a3bb3fd5c posix: mqmeue: do not return NULL after pthread_exit()
pthread_exit() does not return and therefore it does not make
sense to return NULL after it in mq_notify_thread(), and that
would constitute dead code.

Rather than explicitly exiting the thread, simply return
gracefully from the thread function, and allow the pthread to
terminate in the usual way.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-02-26 08:12:29 -05:00
Gaetan Perrot
cd060f6cd3 posix: Implement fdetach and fattach
`fdetach()` and `fattach()` are required
as part of _XOPEN_STREAMS Option Group.

signed-off-by: Gaetan Perrot <gaetanperrotpro@gmail.com>
2024-02-26 11:36:04 +00:00
Christopher Friedt
f3709e20b8 posix: convert all error logs to debug logs
In Zephyr, things are often optimized for size first. That's how
we fit into such tight parking spaces.

This change gives more control to the user about whether the
POSIX API does any logging at all, simultaneously shrinking binary
size while improving speed.

No bytes / cycles left behind!

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-02-24 10:34:05 -05:00
Najumon B.A
89745fe472 lib: acpi: update shell and test app with the modifed resource struct
update shell and test app with the modifed resource struct such as
acpi_irq_resource and acpi_mmio_resource

Signed-off-by: Najumon B.A <najumon.ba@intel.com>
2024-02-15 12:28:55 +01:00
Najumon B.A
2139479722 lib: acpi: fix for build error when acpi not enabled
fix for acpi.h header file generates invalid C-code when
 CONFIG_ACPI=n

Signed-off-by: Najumon B.A <najumon.ba@intel.com>
2024-02-15 12:28:55 +01:00
Andrei Emeltchenko
80c8e9736b lib: acpi: Add asserts catching overflow
Add asserts catching sub-sctructure overflow.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2024-02-09 13:16:33 -06:00
Andrei Emeltchenko
d90060aad7 lib: acpi: Add more ASSERT() in ACPI table parsing
Add more assert() to DMAR parsing functions.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2024-02-09 13:16:33 -06:00
James Ogier
ffa49a8240 libc: newlib: Grant access to dynamic locks from all threads
Fixes: zephyrproject-rtos#67504

The following error is produced  when using the following configuration
```
CONFIG_USERSPACE=y
CONFIG_NEWLIB_LIBC=y
CONFIG_NEWLIB_LIBC_NANO=n
CONFIG_DYNAMIC_OBJECTS=y
CONFIG_HEAP_MEM_POOL_SIZE=256
CONFIG_THREAD_LOCAL_STORAGE=y
CONFIG_MAIN_STACK_SIZE=2048
```

```
os: thread 0x301a2950 (-1) does not have permission on k_mutex 0x301aaca4
os: permission bitmap
01 00 |..
os: syscall z_vrfy_k_mutex_lock failed check: access denied
os: r0/a1: 0x00000000 r1/a2: 0x00000000 r2/a3: 0x00000000
os: r3/a4: 0x00000000 r12/ip: 0x00000000 r14/lr: 0x00000000
os: xpsr: 0x00000000
os: s[ 0]: 0x00000000 s[ 1]: 0x00000000 s[ 2]: 0x00000000 s[ 3]: 0x00000000
os: s[ 4]: 0x00000000 s[ 5]: 0x00000000 s[ 6]: 0x00000000 s[ 7]: 0x00000000
os: s[ 8]: 0x00000000 s[ 9]: 0x00000000 s[10]: 0x00000000 s[11]: 0x00000000
os: s[12]: 0x00000000 s[13]: 0x00000000 s[14]: 0x00000000 s[15]: 0x00000000
os: fpscr: 0x00000000
os: Faulting instruction address (r15/pc): 0xee7fdb7d
os: >>> ZEPHYR FATAL ERROR 3: Kernel oops on CPU 0
os: Current thread: 0x301a2950
```

This bug caused by a global mutex used by _vfprintf_r()
which is initialized in __sinit() and located in z_malloc_partition
not being granted access to be used my multiple user threads
despite each user thread being granted read and write permission
to the z_malloc_partition.

Here is a sample main.c to reproduce the bug
```

extern struct k_mem_partition z_libc_partition;
extern struct k_mem_partition z_malloc_partition;

static k_tid_t tids[TEST_THDS];
static struct k_thread tcbs[TEST_THDS];
static struct k_mem_domain domains[TEST_THDS];
static K_THREAD_STACK_ARRAY_DEFINE(thd_stacks, TEST_THDS, 2048);

static int forbidden_global_data = 2;

void thread_worker(void* a, void* b, void* c) {
    (void)b;
    (void)c;
    printf("thd %d started\n", (int)(intptr_t)a);
    k_sleep(K_MSEC(1000));
    forbidden_global_data++; /* This should cause an MPU Fault */
}

int main(void) {
    forbidden_global_data = 1;
    struct k_mem_partition* share_parts[2] = {
        &z_libc_partition,
        &z_malloc_partition,
    };

    for (int i = 0l; i < TEST_THDS; i++) {
        tids[i] = k_thread_create(
			&tcbs[i],
			thd_stacks[i],
			K_THREAD_STACK_SIZEOF(thd_stacks[i]),
			thread_worker,
			(void*)(intptr_t)i,
			NULL,
			NULL,
			5,
			K_USER,
			K_FOREVER);
        k_mem_domain_init(&domains[i], 2, share_parts);
        k_mem_domain_add_thread(&domains[i], tids[i]);
        k_thread_start(tids[i]);
   }

    for (int i = 0; i < TEST_THDS; i++) {
        k_thread_join(tids[i], K_FOREVER);
    }

   return 0;
}
```

Signed-off-by: James Ogier <jogier@meta.com>
2024-02-07 15:22:38 +00:00
Christopher Friedt
ba639ed6a8 posix: semaphore: check return value of k_mutex_lock()
This fixes CID 340851.

This should never fail, but it's also something that can be
easily verified.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-02-06 17:51:56 -06:00
Daniel Leung
627d3b2cb6 lib: cbprintf: do not blindly skip tags
When CONFIG_LOG_USE_TAGGED_ARGUMENTS is enabled, and
CONFIG_CBPRINTF_COMPLETE is also enabled, we should not be
blindly skipping tags when processing the tagged package
for output.  The issue is that if there is a "%%" in
the format string, the specifier is considered invalid but
the code blindly skips ahead in the argument list as if
it is a valid specifier (think "%s"), which resulting in
the next valid specifier using incorrect argument in
the list. So fix it by skipping ahead if and only if
the specifier is not invalid.

Fixes #68271

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2024-02-06 09:53:15 +01:00
Christopher Friedt
4174ff119d posix: cmake: allow empty library
This change mitigates the following cmake warning

No SOURCES given to Zephyr library: lib__posix__options

without needing to have a separate interface library
(as we do not need private headers exposed).

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-02-05 15:35:09 +01:00
Keith Packard
e569a13d39 libc/newlib: Wrap <string.h> to define strnlen and strtok_r when needed
Newlib doesn't have Zephyr support, so we need to define these functions
when the application doesn't ask for the right level of POSIX support.

Signed-off-by: Keith Packard <keithp@keithp.com>
2024-02-02 19:54:33 +01:00
Gaetan Perrot
a897c1c7d9 posix: sched: Implement sched_rr_get_interval
Implement `sched_rr_get_interval()` POSIX APIs
as a part of PSE53 `_POSIX_PRIORITY_SCHEDULING` option group.
Functions is actually placeholders and just return `ENOSYS`
since Zephyr does not yet support processes or process scheduling.

signed-off-by: Gaetan Perrot <gaetanperrotpro@gmail.com>
2024-02-02 12:42:50 +01:00
Patrick Wildt
f61bbad625 spsc_pbuf: fix cache invalidation in spsc_pbuf_free
In case the read index points to the end of the packet buffer and thus
wraps around, we must not invalidate the cache, otherwise we draw
outside of the lines and purge data outside of our responsibility.

Signed-off-by: Patrick Wildt <pwildt@google.com>
2024-02-01 13:10:28 -06:00
Gaetan Perrot
9cd7564ab0 posix: pthread_testcancel zephyrproject-rtos#59946
Implement posix pthread_testcancel()

signed-off-by: Gaetan Perrot <gaetanperrotpro@gmail.com>
2024-02-01 15:14:28 +01:00
Christopher Friedt
e8b178411c posix: remove unneeded option to link with posix subsys
The CONFIG_APP_LINK_WITH_POSIX_SUBSYS option was originally
present so that internal POSIX implementation headers would be
on the include path.

There is no implicit need for any app or library to include
private POSIX headers. Instead, the standard POSIX API should
be used.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-02-01 05:26:24 -05:00
Christopher Friedt
855b8bc6ca posix: separate shell utilities and posix api implementation
Previously, the POSIX shell utilities were intermixed with the
POSIX API implementation.

The POSIX shell utilities only depend on the public POSIX API,
so it makes sense to keep them in a separate subdirectory.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-02-01 05:26:24 -05:00
Najumon B.A
9d6a0ceeab lib: acpi: add resource enumeration shell command
add resource enumeration and acpi method shell commands such as
retrieve mmio and interrupt resources.

Signed-off-by: Najumon B.A <najumon.ba@intel.com>
2024-01-31 15:03:06 +01:00
Najumon B.A
2f3fb49d76 lib: acpi: add device resource enum support
add device resource enumaration support such as irq and mmio.

Signed-off-by: Najumon B.A <najumon.ba@intel.com>
2024-01-31 15:03:06 +01:00
Gaetan Perrot
8a6c745e9f posix: sched: Implement set APIs for scheduling parameters
Implement `sched_setparam()` and `sched_setscheduler()` POSIX APIs
as a part of PSE53 `_POSIX_PRIORITY_SCHEDULING` option group.
Both functions are actually placeholders and just return `ENOSYS`
since Zephyr does not yet support processes or process scheduling.

signed-off-by: Gaetan Perrot <gaetanperrotpro@gmail.com>
2024-01-31 11:16:26 +01:00
Abhinav Srivastava
52485876f0 posix: Add tests for stropts
Add tests, Fix config issues, Re-add Timer

Signed-off-by: Abhinav Srivastava <atg271@gmail.com>
2024-01-31 09:05:50 +01:00
Abhinav Srivastava
93e9491dd9 posix: putmsg implementation and configurations
Add needed KConfig, CMakeList and Stropts.c

Signed-off-by: Abhinav Srivastava <atg271@gmail.com>
2024-01-31 09:05:50 +01:00
Adam Wojasinski
6f5626d1cf posix: Add basic sysconf() function implementation
The patch introduces basic implementation of sysconf() function.
It's based on macro - that means that every function call is resolved
at compile time - and is not fully complient with POSIX standard
(the errno value is not handled and passing invalid name argument
results in compilation error). Treat this commit as a starting point
for proper sysconf() implementation. The one introduced in the patch
could stay as a defult implementation.

sysconf() documentation:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html
https://man7.org/linux/man-pages/man3/sysconf.3.html

Fixes #56670

Signed-off-by: Adam Wojasinski <awojasinski@baylibre.com>
2024-01-30 18:01:18 +01:00
Christopher Friedt
b600e8a870 posix: timer: build timer.c using the correct kconfig option
Previously timer.c was only built with CONFIG_POSIX_CLOCK=y even
though it has had its own Kconfig symbol (CONFIG_TIMER) for a
very long time.

Make POSIX_CLOCK imply TIMER rather than control whether it is
built, and adjust Kconfig / CMake as necessary.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-29 10:28:10 +01:00
Christopher Friedt
e357ba835d posix: internal: make priority / policy transforms available
Two functions can be used with relative ease to convert between
Zephyr and POSIX priorities and policies. Namely,

uint32_t zephyr_to_posix_priority(int32_t z_prio, int *policy)
int32_t posix_to_zephyr_priority(uint32_t priority, int policy)

These are not necessarily public API, but they helped with the
POSIX Philosophers Sample, which is in a subsequent commit.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-28 13:01:36 -05:00
Christopher Friedt
fc26fd5a15 posix: pthread: initialize t and c pointers to NULL
* initialize posix_thread and __pthread_cleanup ptrs to NULL
* check ret is zero before finalizing a thread in pthread_cancel()

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-26 10:12:21 -05:00
Keith Packard
9dcbfa8bf2 libc/picolibc: Remove the global definition of _POSIX_C_SOURCE
This was necessary to get Picolibc to expose the whole Zephyr C library
API, but current versions of the SDK use a version of Picolibc with
built-in Zephyr support.

Signed-off-by: Keith Packard <keithp@keithp.com>
2024-01-26 07:48:55 -05:00
Christopher Friedt
23016f4589 posix: pthread: posix_thread_q_set() and posix_thread_q_get()
Create getter and setter functions for setting internal pthread
states to READY, RUN, or DONE queues.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-26 06:50:11 -05:00
Christopher Friedt
81f3777be1 posix: pthread: hold pool lock when calling to_posix_thread()
There may be race conditions when calling to_posix_thread()
from multiple threads.

Ensuing that the pthread pool lock is held when
to_posix_thread() is called will prevent those races.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-26 06:50:11 -05:00
Christopher Friedt
77135fcbc6 posix: pthread: embed posix_thread_attr inside of posix_thread
Rather than shadowing most of the fields from pthread_attr_t
(struct posix_thread_attr) inside of struct posix_thread, just
embed the structure itself.

Additionally, use pthread_attr_init() to allocate a thread
stack and pthread_attr_destroy() to destroy a thread stack,
dramatically simplifying pthread_create().

A fair bit of rework was needed already to mitigate bitrot
issues in the testsuite. More should probably be done later.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-26 06:50:11 -05:00
Christopher Friedt
dbcba2f46c posix: pthread: check canceltype before async cancel
The default pthread cancellation type is deferred.

Check that the canceltype is asynchronous (with
respect to cancellation points) before cancelling
a thread.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-26 06:50:11 -05:00
Christopher Friedt
301581a892 posix: pthread: wrapper to check attribute initialization
Add a small wrapper to check if a pthread_attr_t has been
properly initialized (i.e. ready to pass to
pthread_create()).

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-26 06:50:11 -05:00
Luca Burelli
5f2c6e58b0 ztest: export ztest_test_* symbols to llexts
Export the minimal set of ztest_test_* symbols to llexts so that the
zassert_* macros can be used by the extension code to affect test
execution. Calling these also requires vprintk() to be exported.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
2024-01-26 12:36:23 +01:00
Adam Wojasinski
c1643f9701 posix: Add implementation of mq_notify() function
The function was the last missing piece of the `_POSIX_MESSAGE_PASSING`
option group. Due to lack of signal subsystem in the Zephyr RTOS
the `sigev_notify` member of the `sigevent` structure that describes
the notification cannot be set to `SIGEV_SIGNAL` - this notification
type is not implemented, the function will return -1 and set `errno`
to `ENOSYS`.

`mq_notify` documentation:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/mq_notify.html

Fixes #66958

Signed-off-by: Adam Wojasinski <awojasinski@baylibre.com>
2024-01-25 19:47:37 +01:00
Christopher Friedt
57057b141c posix: add a top-level menu
Add a top-level menu to prevent POSIX API options from
cluttering the menuconfig view under
"Additional libraries".

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-25 05:54:06 -05:00
Christopher Friedt
3ff7c04f30 posix: timer: use async pthread cancellation
Previously, Zephyr's POSIX API did not differentiate between
deferred and asynchronous pthread cancellation. In fact all
pthread cancellation was asynchronous. According to the spec,
all pthreads should be created with deferred cancellation by
default.

Note: PTHREAD_CANCEL_ASYNCHRONOUS means cancel asynchronously
with respect to cancellation points (but synchronously with
respect to the thread that callse pthread_cancel(), which is
perhaps unintuitive).

The POSIX timer relied on this non-standard convention.

Oddly, this change prevents what would have otherwise been a
regression that would have been caused by fixing pthread
behaviour (in a separate commit).

We are effectively uncovering bugs which were probably always
present in the pthread.c and timer.c implementations going
back quite a few years.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-22 06:48:55 -05:00
Christopher Friedt
689dc4a45b posix: timer: support other clocks
There is no requirement that says e.g. CLOCK_REALTIME cannot be
used for timer_create(). In fact, the spec explicitly requires
it. It might not be ideal, but users should still be able to
use it.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-22 06:48:55 -05:00
Christopher Friedt
b0a83a9177 posix: rwlock: make pthread_rwlock_t a pooled ipc type
Like mutex, barrier, cond, spinlock, etc, make
pthread_rwlock_t a pooled ipc type.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-18 09:22:35 -05:00
Yong Cong Sin
7c42c01f10 posix: pthread: use is_posix_policy_prio_valid to check POSIX priority
Use the existing `is_posix_policy_prio_valid()` function to
verify the POSIX's priority in the conversion functions.

Changed the `priority` arg of `is_posix_policy_prio_valid` to
`int` since that is the output of `sched_get_priority_min` &
`sched_get_priority_max`.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-18 10:54:47 +01:00
Yong Cong Sin
c3cc2e4e6d posix: pthread: test the priority conversion functions
Made the conversion functions non-static and added ztests for
them to make sure that they work across the full range of
Zephyr priorities.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-18 10:54:47 +01:00
Yong Cong Sin
5ad7f4b314 posix: pthread: priority should be of int type
Changed the variable type of the priority in the args and the
return type of the conversion functions to `int`, as both
Zephyr's priority & POSIX's `sched_priority` has type `int`.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-18 10:54:47 +01:00
Yong Cong Sin
b2caec8601 posix: pthread: fix typos
- 'piority' should be 'priority'
- COOP should start from `-CONFIG_NUM_COOP_PRIORITIES`

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-18 10:54:47 +01:00
Yong Cong Sin
66fdd39ec5 posix: pthread: fix zephyr_to_posix_priority assert test
If `z_prio` is negative and we want to make sure that it is
within `[-CONFIG_NUM_COOP_PRIORITIES, -1]`, we should invert
its sign and make sure that it is `<=`
`CONFIG_NUM_COOP_PRIORITIES` instead of `<`.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-18 10:54:47 +01:00
Yong Cong Sin
45c554d082 posix: shell: introduce top level posix command
Added a top level `posix` shell command for other POSIX
commands. Currently only `uname` is supported.

New POSIX commands can be added by including the
`posix_shell.h` header and use the `POSIX_CMD_ADD` helper
macro.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-18 10:54:32 +01:00
Yong Cong Sin
635dabf07a posix: uname: shell: update help message with more information
Populate the help message with more information when an error
occurs.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-18 10:54:32 +01:00
Yong Cong Sin
48c16f9052 posix: uname: move uname shell from sample
Relocate the `uname` shell implementation from uname sample, so
that it can be reused by other application and the uname sample
only uses POSIX APIs.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-18 10:54:32 +01:00
Yong Cong Sin
98a631b5bc posix: pthread: allow oldstate & oldtype to be NULL
The POSIX standard doesn't specify if the argument to store
previous state/type in
`pthread_setcancelstate`/`pthread_setcancelstate` can be
`NULL`, but threading implementations in Linux & Apache NuttX
permit the arguments to be `NULL`.

This commit changes Zephyr's implementation to mimic that of
Linux & NuttX, so that user do not get caught off-guard by
NULL pointer dereferencing when porting code over from those
OSes.

Updated test accordingly.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-17 14:42:50 +01:00
Yong Cong Sin
73e20a8967 posix: clock: add ztest rule to reset clock base
Make sure that the POSIX clock base is reset after every
testsuite so that the initial time is the same for every test.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-17 05:41:37 -05:00
Christopher Friedt
a6dab1a85e posix: pthread: additional null checks on parameters
Instead of relying on a fault occurring when NULL
parameters are passed in, explicitly check and return
EINVAL.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-16 00:35:45 -05:00
Dmitrii Golovanov
68d1a52417 posix: sched: Implement get APIs for scheduling parameters
Initial implementation of `sched_getparam()` and `sched_getscheduler()`
POSIX APIs as a part of PSE53 `_POSIX_PRIORITY_SCHEDULING` option group.
Both functions are actually placeholders and just return `ENOSYS`
since Zephyr does not yet support processes or process scheduling.

Signed-off-by: Dmitrii Golovanov <dmitrii.golovanov@intel.com>
2024-01-15 09:57:44 +01:00
Dmitrii Golovanov
b184fc3a62 posix: sched: Add CONFIG_POSIX_PRIORITY_SCHEDULING
Add `CONFIG_POSIX_PRIORITY_SCHEDULING` Kconfig option to select
APIs from PSE53 `_POSIX_PRIORITY_SCHEDULING` option group.

Signed-off-by: Dmitrii Golovanov <dmitrii.golovanov@intel.com>
2024-01-15 09:57:44 +01:00
Yong Cong Sin
73da1e80f4 posix: signal: implement sigprocmask()
Implement `sigprocmask()` by simply redirecting call to the
`pthread_sigmask()` as they are identical.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-11 07:49:26 -05:00
Nicolas Pitre
cf74f22524 posix: semaphore: optimize named semaphore implementation a bit
- Regroup refcount decrement and semaphore destruction by making the
  linked state into a counted reference for it. This allows for
  simplifying the code and cleanly adding a few assertions in a common
  location.

- Remove redundant initialization to NULL on memory about to be freed
  and local pointer in nsem_cleanup().

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2024-01-10 07:18:19 -05:00
Yong Cong Sin
abb21d48d9 posix: semaphore: implement sem_open(), sem_unlink() & sem_close()
Implements `sem_open()`, `sem_unlink()` & `sem_close()`
functions and added tests for them.

Updated existing tests and POSIX docs.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-10 07:18:19 -05:00
Jai Arora
5c34726ab1 posix: patch to implement get_pid function
patch to implement get_pid function

Signed-off-by: Jai Arora <infolinesoni@gmail.com>
2024-01-08 12:44:45 +01:00
Jai Arora
a3573a9e30 posix: implement clock_getcpuclockid function
Implements clock_getcpuclockid function

Fixes #59954

Signed-off-by: Jai Arora <infolinesoni@gmail.com>
2024-01-08 12:44:45 +01:00
Alberto Escolar Piedras
b802e42c6f posix kconfig: Improve depends on host libC
Improve a depends on the host libC.
It is technically correct, but NATIVE_LIBC
is shorter and clearer than "ARCH_POSIX && EXTERNAL_LIBC"

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-01-05 11:38:57 -06:00
Alberto Escolar Piedras
c58710c602 lib/posix getopt: Fix include
Let's try to use the host unistd.h when building with
the host library only, instead of assuming
that the native boards are always built with it.
This fixes a build error when building for native boards
using minimal libc.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-01-05 11:38:57 -06:00
Christopher Friedt
95a22b1217 posix: clock: clock_gettime() should not be a syscall
We try to implement Zephyr's POSIX API as regular library
functions, so remove the __syscall annotation from
clock_gettime() and implement the syscall portion of it under
the hood.

This also adds a bit of a micro-optimization in that
we can do a lot of processing outside of the system call.

In fact, processing CLOCK_MONOTONIC likely does not
require any syscall other than k_uptime_ticks().

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-04 08:27:29 +09:00
Christopher Friedt
e0383a6f9d posix: clock: check for invalid ns in clock_settime()
The clock_settime() function should not accept an input timespec
with nanosecond values < 0 or >= NSEC_PER_SEC.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-04 08:27:29 +09:00
Pisit Sawangvonganan
b8184ca3b7 lib: crc: address absence of crc4, crc4_ti in crc_types array
Added CRC4 and CRC4_TI to the supported crc_types[] array.
On some SoCs, like ESP32-S3, missing values can cause hardfaults
due to attempts to access the zero address.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2024-01-03 10:20:14 +01:00
Martin Åberg
fdacddda08 posix: timer: Fix timer_obj alignment
This makes sure the heap posix_timer_slab provides objects aligned
compatible with the type timer_obj. It was previously set to align at 4
bytes. One example where this failed was on the SPARC which requires
access to int64_t to be 8-byte aligned.

In particular, struct timer_obj contains fields of type k_timer_t and
struct _timeout.

With this commit we now get the information on required alignment for
struct timer_obj from the compiler by using __alignof__().

Signed-off-by: Martin Åberg <martin.aberg@gaisler.com>
2024-01-02 10:09:00 +01:00
Christopher Friedt
10156f5f1d posix: mqueue: pop mode as int with va_arg()
There was some discussion about whether it was suitable to have
an architecture-specific workaround in mqueue.c after that
workaround was copied to a different source file in a PR.

The original issue was that newlib and picolibc declare mode_t
to be unsigned short instead of unsigned long when __svr4__
is not defined along with __sparc__. This is specifically
impactful, because va_arg() deals (mainly) with 32-bit and
64-bit values that are all naturally aligned to 4 bytes.

#if defined(__sparc__) && !defined(__sparc_v9__)
#ifdef __svr4__
typedef unsigned long __mode_t;
#else
typedef unsigned short __mode_t;
#endif

A uint16_t is naturally aligned to 2 bytes, so not only would
a 16-bit mode_t be corrupted, it would also generate a warning
with recent gcc versions which is promoted to error (rightfully
so) when run through CI.

mqueue.c:61:35: error: 'mode_t' {aka 'short unsigned int'} is
  promoted to 'int' when passed through '...' [-Werror]
   61 |                 mode = va_arg(va, mode_t);

Instead of using an architecture-specific workaround, simply
add a build assert that the size of mode_t is less than or
equal to the size of an int, and use an int to retrieve it
via va_arg().

Signed-off-by: Christopher Friedt <cfriedt@meta.com>

# Please enter the commit message for your changes. Lines starting
# with '#' will be kept; you may remove them yourself if you want to.
# An empty message aborts the commit.
#
# Date:      Fri Dec 29 10:06:44 2023 -0500
#
# On branch posix-mqueue-always-use-int-for-mode-t-va-arg
# Changes to be committed:
#	modified:   lib/posix/mqueue.c
#	modified:   tests/posix/common/testcase.yaml
#
2024-01-01 16:36:31 -05:00
Fabio Baltieri
e67b12e636 lib: posix: tag the fallthrough case as __fallthrough
Use the __fallthrough directive on the switch fallthrough case and drop
the comment instead. This informs the compiler that the fallthrough is
intentional and silence any possible warning about it.

Drop the not reached case as I think that that part can actually be
reached since there are breaks in some of the cases.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
2023-12-30 14:35:41 -05:00
Alberto Escolar Piedras
c05a483ba4 lib posix: Allow building the POSIX_API library without the host libC
In theory one may want to build the POSIX compability shim
with a different C library than one provided with Zephyr,
so let's not prevent it.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-12-28 09:58:23 -05:00
Alberto Escolar Piedras
23ad1177a2 Revert "lib: support linking to POSIX API with 3rd-party libc"
This reverts commit 6342aa3cc0.

This commit should never have been merged.
Apart from the fact that this change was rejected in previous
review,
this change is wrong, for 2 reasons:
1. The POSIX_API (POSIX compatibility shim) cannot be
built if the host libC is used.
2. The Zephyr libC CMake files were guarded so they
would not be dragged when CONFIG_EXTERNAL_LIBC was selected.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-12-28 09:58:23 -05:00
Christopher Friedt
ec69e64d4c posix: pthread_setspecific: fix for coverity issue cid 334906
* return EINVAL if pthread_self() is not a valid pthread

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-28 12:19:10 +00:00
Christopher Friedt
3458527a2c posix: pthread_getspecific: fix for coverity issue cid 334909
* remove unneeded line of code that duplicated the first part
  of the SYS_SLIST_FOR_EACH_NODE() expansion.
* return NULL if pthread_self() is not a valid pthread

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-28 12:19:10 +00:00
Krzysztof Chruściński
c48d61a4b4 lib: os: cbprintf: Fix size miscalculation in cbprintf_convert
When package contained RO string positions and flag indicates that
they shall not be kept, length was miscalculated which could lead
to failures (e.g. memory corruption).

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
2023-12-28 12:18:50 +00:00
Christopher Friedt
6342aa3cc0 lib: support linking to POSIX API with 3rd-party libc
Previously it was not possible to link to Zephyr's
POSIX API under lib/posix when building with
`CONFIG_EXTERNAL_LIBC=y`.

This small change allows that to work as expected.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-28 06:14:22 -05:00
Christopher Friedt
fcd139d7af posix: pthread: support stack sizes larger than 65k
A previous size optimization capped the pthread_attr_t stacksize
property at 65536. Some Zephyr users felt that was not large
enough for specific use cases.

Modify struct pthread_attr to support large stack sizes by
default with the flexibility to allow users to vary the number
of bits used for both stacksizes and guardsizes.

The default guardsize remains zero sinze Zephyr's stack
allocators already pad stacks with a guard area based on other
config parameters, and since Zephyr is already designed to
support both SW and HW stack protection at the kernel layer.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-23 22:29:41 -05:00
Anas Nashif
0b999c0943 lib: move utilities into own folder
Move various utilities out of lib into own folder for better assignement
and management in the maintainer file. lib/os has become another dumping
ground for everything and it the Kconfig and contents in that folder
became difficult to manage, configure and test.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-12-22 09:54:12 +01:00
Christopher Friedt
a7d2c5cb32 posix: implement pthread_getguardsize() pthread_setguardsize()
Implement pthread_getguardsize() and pthread_setguardsize().

pthread_getguardsize() and pthread_setguardsize() are required
by the POSIX_THREADS_EXT Option Group as detailed in Section
E.1 of IEEE-1003.1-2017. However, they were formerly part of
XSI_THREADS_EXT.

The XSI_THREADS_EXT Option Group was required for PSE51, PSE52,
PSE53, and PSE54 conformance.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-20 14:20:58 +00:00
Christopher Friedt
eb7e4bed98 posix: pthread_attr: use space in struct more efficiently
This change reduces the space occupied by struct pthread_attr
which is the internal type used for pthread_attr_t.

We cap the stack size at 16 bits (so up to 65536 bytes) and
since a stack size of 0 is invalid, we can encode the stack
size by simply subtracting 1 or adding 1 when setting or
getting.

The schedpolicy is capped at 2 bits and initialized,
cancellable, and detached are given 1 bit.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-20 14:20:58 +00:00
Johan Hedberg
7bb16c779b posix: mqueue: Remove custom default for HEAP_MEM_POOL_SIZE
Use the new HEAP_MEM_POOL_ADD_SIZE_ prefix to construct a minimum
requirement for posix message queue usage. This way we can remove the
"special case" default values from the HEAP_MEM_POOL_SIZE Kconfig
definition.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2023-12-20 11:01:42 +01:00
Johan Hedberg
0594d29f4e lib: acpi: Introduce HEAP_MEM_POOL_ADD_SIZE_ACPI option
All x86 boards have so far set a custom heap memory pool size because of
their dependency on ACPI. It makes more sense to introduce a new
ACPI-specific option, utilizing the recently added
HEAP_MEM_POOL_ADD_SIZE_ Kconfig option prefix, and adjust the default
value as necessary for each board.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2023-12-20 11:01:42 +01:00
Nikolay Agishev
04601aedad ARCMWDT: Add headers for POSIX compatibility
Add ENODATA errno code, wich is not presented in ARCMWDT headers
Add PATH_MAX define

Signed-off-by: Nikolay Agishev <agishev@synopsys.com>
2023-12-19 08:53:51 -06:00
Andrei Emeltchenko
24b58ecc51 lib: acpi: Add ASSERT() for wrong length
Add ASSERT() for length.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2023-12-19 11:04:19 +01:00
Andrei Emeltchenko
28ac21330d lib: acpi: Implement acpi_dmar_ioapic_get()
Implement get IOAPIC id from DMAR table.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2023-12-19 11:04:19 +01:00
Andrei Emeltchenko
4c3eda827a acpi: Add acpi_dmar_foreach helpers
Add function walking though all DMAR subtables, at the moment only
first subtable is taking into account, which causes bugs for some
boards.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2023-12-19 11:04:19 +01:00
Christopher Friedt
04c9903055 posix: pthread_once: simplify and reduce size of pthread_once_t
Since pthread_once() is both the initializer and executor of
pthread_once_t, it can have maximally two states. Since the
implementation in Zephyr previously aimed to maximize libc
compatibility, we opted to use the definition of pthread_once_t
from newlib, which is a structure with two ints.

It does not make sense to use 64 bits to manage 2 possible
states. The control for that should effectively be a bool.

We maintain compatibility with newlib by asserting (at build
time), that newlib's pthread_once_t is larger than Zephyr's
new struct pthread_once (which just contains a bool).

This allows us to delete the non-standard pthread_key.h
header file (finally).

Reuse the pthread_pool_lock in order to synchronize the related
init function (so that it is only called maximally once from any
thread). The spinlock is only used to test the state and the
init function is not called with the spinlock held.

The alternative was to use an atomic inside of
struct pthread_once. But again, that could be up to 64-bits with
Zephyr's atomics implementation.

Ideally we would use C11 generics or something to support atomics
on 8, 16, 32, and 64-bit primitives.

Lastly, also update declarations for C11 threads as they mostly
mirror our pthread implementation.

This needed to be done as a single commit in order to ensure
continuity of build.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-14 09:32:58 +01:00
Anas Nashif
246ec224e2 lib: heap: move heap stats to own file
heap stats are split out from heap_validate.c into own file.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-12-13 17:27:54 -05:00
Anas Nashif
d4c881da04 lib: mem_block: move to own folder
Move mem_block into own folder and seperate from lib/os and heap
configuration.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-12-13 17:27:54 -05:00
Anas Nashif
740d0c3b50 lib: heap: make multi-heap feature configurable
Make multi-heap configurable via Kconfig and adapt existing tests.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-12-13 17:27:54 -05:00
Anas Nashif
d398e9631b lib: heap: build heap_info conditionally
Only build when needed and adapt existing users to pull this in when
needed.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-12-13 17:27:54 -05:00
Anas Nashif
00680bd23a lib: heap: rename with files and use _
be consistent with other files in the same folder.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-12-13 17:27:54 -05:00
Anas Nashif
b9f2ac0270 lib: heap: make heap_print_info static
This should be declated static as it is being only called in this file.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-12-13 17:27:54 -05:00
Anas Nashif
d6d716b32a lib: heap: build heap utilities only on demand
Build the heap features only when needed and based on Kconfigs being
set.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-12-13 17:27:54 -05:00
Anas Nashif
4cd9c11692 lib: heap: split heap-validate.c
Split heap-validate.c into smaller chunks. We have been adding all kind
of new APIs under this file and building it unconditionally whether
those APIs are needed/used or not. Many of those APIs have nothing to do
with the validation part.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-12-13 17:27:54 -05:00
Anas Nashif
6cb588820f lib: os: move heap to own folder
consolidate all heap related files and implementation into one folder.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-12-13 17:27:54 -05:00
Florian La Roche
6621e780c9 lib: libc: minimal: proper cast to "(char *)" from "(const char *)"
The string "" is of type '(const char *)', so add a cast over to
'(char *)' to clean up source code.

Signed-off-by: Florian La Roche <Florian.LaRoche@gmail.com>
2023-12-13 20:12:45 +01:00
Christopher Friedt
c2721805f0 posix: pthread: implement pthread_sigmask()
pthread_sigmask() is required by the POSIX_THREADS_BASE
Option Group as detailed in Section E.1 of IEEE-1003.1-2017.

The POSIX_THREADS_BASE Option Group is required for PSE51,
PSE52, PSE53, and PSE54 conformance, and is otherwise mandatory
for any POSIX conforming system as per Section A.2.1.3 of
IEEE-1003-1.2017.

Currently, setting a pthread signal mask has no effect.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-04 20:49:35 -05:00
Christopher Friedt
73930c25f2 posix: signal: define max rt signals as zero when not enabled
When CONFIG_POSIX_SIGNAL=n, default CONFIG_POSIX_RTSIG_MAX to
0 so that the `sigset_t` type can be defined.

Also define RTSIG_MAX.

This allows native_sim and other platforms to build without
error.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-04 20:49:35 -05:00
Guennadi Liakhovetski
69cdc32892 llext: export some symbols
Export some symbols for loadable modules. Also add an
EXPORT_SYSCALL() helper macro for exporting system calls by their
official names.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-12-01 10:08:12 -05:00
Christopher Friedt
742de21eef posix: pthread: add logging to pthread key
To align with other supported POSIX features, add logging to
pthread key to provide better error reporting and diagnostics.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-30 11:24:09 -05:00
Christopher Friedt
5dc561f1b4 posix: key: remove unnecessary else / indent
Remove the unnecessary else clause and indentation in
pthread_setspecific().

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-30 11:24:09 -05:00
Christopher Friedt
fb695c42fb posix: pthread: implement pthread_cleanup_push() / pop()
pthread_cleanup_push() and pthread_cleanup_pop() are required
by the POSIX_THREADS_BASE Option Group as detailed in Section
E.1 of IEEE-1003.1-2017.

The POSIX_THREADS_BASE Option Group is required for PSE51,
PSE52, PSE53, and PSE54 conformance, and is otherwise mandatory
for any POSIX conforming system as per Section A.2.1.3 of
IEEE-1003-1.2017.

In this change, we require the addition of a dedicated
pthread_key_t that will not be available for applilcation usage.

Rather than including that as part of
CONFIG_MAX_PTHREAD_KEY_COUNT, we increase the storage by 1 in
order to be least invasive from the application perspective.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-29 10:08:44 +01:00
Christopher Friedt
5cd2e17424 posix: pthread: fix warning about uninitialized variable
Initialize the variable to zero. I don't think there is any way
out of this function without it being initialized, so IMHO it's
a false positive.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-27 19:57:42 -05:00
Christopher Friedt
e49ae776cc posix: pthread: implement pthread_getconcurrency()
Zephyr must support all functionality of the XSI_THREADS_EXT
subprofiling option group in order to claim it supports that
subprofiling option group.

The XSI_THREADS_EXT option group is critical to be able to
run POSIX threads with statically allocated thread stacks, which
has been a feature of the implementation since it was initially
added.

The pthread_getconcurrency() and pthread_setconcurrency()
functions are the only remaining, unimplemented functions of
the XSI_THREADS_EXT option group.

Implement pthread_getconcurrency() and pthread_setconcurrency()
via the more "posixly correct" interpretation of the
specification.

I.e. as the pthread_t:k_thread relationship is 1:1 and not M:N,
Zephyr does not support multiplexing of user threads on top of
schedulable kernel entities (i.e. "user threads" are directly
mapped to native threads, just like linuxthreads or NPTL are in
Linux).

For that reason, to be "posixly correct", we should save the
provided value via pthread_setconcurrency(), in the absense of
errors, and also return that same value back via
pthread_getconcurrency(), even though that serves zero purpose
in Zephyr for the foreseeable future.

Note: the specification also states
"an implementation can always ignore any calls to
pthread_setconcurrency() and return a constant for
pthread_getconcurrency()."

For that reason, the implementation may be revisited at a later
time when when considering optimizations and when there is a
better system in place for documenting deviations.

Any such optimization should be explicitly controlled via
Kconfig.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-27 08:26:31 -05:00
Christopher Friedt
1e7eb7a6da posix: pthread: support for pthread_setcanceltype()
pthread_setcanceltype() is required by the POSIX_THREADS_BASE
Option Group as detailed in Section E.1 of IEEE-1003.1-2017.

The POSIX_THREADS_BASE Option Group is required for PSE51,
PSE52, PSE53, and PSE54 conformance, and is otherwise mandatory
for any POSIX conforming system as per Section A.2.1.3 of
IEEE-1003-1.2017.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-26 05:55:08 -05:00
Christopher Friedt
87635dd34a posix: do not define _PTHREAD_CANCEL_POS
_PTHREAD_CANCEL_POS is an implementation detail and should not
be defined in the global scope. Furthermore, _PTHREAD_CANCEL_POS
uses a reserved identifier (underscore followed by capital
letter).

Adjust definitions so that the implementation detail is only
used in the implementation and not in the interface.

Additionally, modify naming so that the non-standard macro does
not use a reserved identifier.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-25 06:29:16 -05:00
Christopher Friedt
19ef279dc3 posix: define PTHREAD_CANCELED globally
Move the definition of PTHREAD_CANCELED from pthread.c to
pthread.h.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-25 06:29:16 -05:00
Christopher Friedt
ad5441bbdd posix: pthread: move pthread_equal() closer to pthread_self()
* "identity" functions grouped more closely
* posix_thread_pool_init() should be adjacent to the SYS_INIT()

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-25 06:29:16 -05:00
Christopher Friedt
a89fa32dac posix: pthread: ensure pthread_key_delete() removes correct key
Previously, `pthread_key_delete()` was only ever deleting key 0
rather than the key corresponding to the provided argument.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-24 10:47:28 -05:00
Christopher Friedt
a0c307c0a5 posix: pthread: implement pthread_atfork()
pthread_atfork() is required by the POSIX_THREADS_BASE
Option Group as detailed in Section E.1 of IEEE-1003.1-2017.

The POSIX_THREADS_BASE Option Group is required for PSE51,
PSE52, PSE53, and PSE54 conformance, and is otherwise
mandatory for any POSIX conforming system as per Section
A.2.1.3 of IEEE-1003-1.2017.

Since Zephyr does not yet support processes and (by extension)
fork(), this implementation includes a deviation and should be
categorized as producing undefined behaviour.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-23 00:59:32 -05:00
Flavio Ceolin
8679c58644 kernel: Option to not use tls to get current thread
Add a Kconfig option to tell whether or not using thread
local storage to store current thread.

The function using it can be called from ISR and using
TLS variables in this context may (should ???) not be
allowed

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2023-11-21 15:49:48 +01:00
Keith Packard
7a5fcb8c60 libc/picolibc: Support 'long long' and 'minimal' printf variants
Picolibc's 'minimal' printf mode reduces functionality and size even more
than the 'integer' mode. Use this where memory is at a premium and where
the application knows that it does not require exact printf semantics.

1.8.5 adds two more printf variants, 'long long' and 'minimal'. The 'long
long' variant is the same as the 'integer' variant but with long long
support enabled. The 'minimal' variant reduces functionality and size even
more than the 'integer' mode. Applications can use this where memory is at
a premium and where the application does not require exact printf
semantics.

With these two added variants, the SDK has enough options so that all of
the cbprintf modes can be supported with the pre-compiled bits:

 1. CBPRINTF_NANO - picolibc's 'minimal' variant
 2. CBPRINTF_REDUCED_INTEGRAL - picolibc's 'integer' variant
 3. CBPRINTF_FULL_INTEGRAL - picolibc's 'long long' variant
 4. CBPRINTF_FB_SUPPORT - picolibc's 'double' variant

This patch makes the cbprintf Kconfig values drive the default picolibc
variant, disables picolibc variants not capable of supporting the required
cbprintf level, but allows applications to select more functionality in
picolibc than cbprintf requires.

Note that this depends on the SDK including picolibc 1.8.5. Without that,
selecting the 'minimal' or 'long long' variant in Zephyr will end up with
the default variant from picolibc, which is the full version with floating
point support. When using the module things will work as specified.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-11-20 06:07:58 -05:00
Keith Packard
5347a834af libc/picolibc: Support picolibc's assert-verbose option
This option in picolibc switches the assert macro between a chatty version
and one which provides no information at all. This latter mode avoids
placing the associated strings in memory.

The Zephyr option is PICOLIBC_ASSERT_VERBOSE and it is disable by default.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-11-20 06:07:58 -05:00
Keith Packard
47e09806c8 lib/os: With CBPRINTF_NANO, picolibc long-long printf isn't required
CBPRINTF_FULL_INTEGRAL doesn't happen to explicitly conflict with
CBPRINTF_NANO, but when CBPRINTF_NANO is enabled, there's no long long I/O
support provided.

Allow picolibc long-long I/O support to also be elided when CBPRINTF_NANO
is enabled to save similar amounts of space.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-11-20 06:07:58 -05:00
Keith Packard
7d9c0b9db5 lib/os/cbprintf: Picolibc doesn't support several cbprintf options
* Picolibc doesn't provide the %a-only mode.

 * On advice from security experts, who report numerous vulnerabilities
   caused by %n in printf specifiers, picolibc never supports this
   feature.

 * Picolibc doesn't use cbprintf for C-library compatible functions,
   instead it provides aliases for the *printfcb functions using stdio
   names.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-11-20 09:22:32 +01:00
Henrik Brix Andersen
c0c8952739 shell: do not enable subsystem/driver shell modules by default
Do not enable subsystem/driver shell modules by default and stop abusing
CONFIG_SHELL_MINIMAL, which is internal to the shell subsystem, to decide
when to enable a driver shell.

The list of shell modules has grown considerably through the
years. Enabling CONFIG_SHELL for doing e.g. an interactive debug session
leads to a large number of shell modules also being enabled unless
explicitly disabled, which again leads to non-negligible increases in
RAM/ROM usage.

This commit attempts to establish a policy of subsystem/driver shell
modules being disabled by default, requiring the user/application to
explicitly enable only those needed.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-11-20 09:21:40 +01:00
Daniel Leung
c972ef1a0f kernel: mm: move kernel mm functions under kernel includes
This moves the k_* memory management functions from sys/ into
kernel/ includes, as there are kernel public APIs. The z_*
functions are further separated into the kernel internal
header directory.

Also made a quick change to doxygen to group sys_mem_* into
the OS Memory Management group so they will appear in doc.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2023-11-20 09:19:14 +01:00
Christopher Friedt
4c58c6b4c4 libc: common: support for C11 call_once()
Add C11 call_once() support to go with C11 threads
and mutexes.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-14 18:23:42 +09:00
Christopher Friedt
b9db7df628 libc: common: support for C11 thread-specific storage
Add C11 thread-specific storage (tss) support to go with C11 threads
and mutexes.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-14 18:23:42 +09:00
Christopher Friedt
7e539e2706 libc: common: support for C11 condition variables
Add C11 condition variable support to go with C11 threads
and mutexes.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-14 18:23:42 +09:00
Christopher Friedt
576ae7f677 lib: libc: common: add C11 mutex implementation
Add support for C11 mutexes to go with C11 threads.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-14 18:23:42 +09:00
Christopher Friedt
97668b8b09 libc: common: add support for iso c11 threads
This change capitalizes on newly added support for dynamic
thread stacks and the existing pthread support to provide
an implementation of the ISO C11 `<threads.h>` API.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-14 18:23:42 +09:00
Grant Ramsay
a3ff19a39e cmake: compiler: Add compiler property for no-builtin
Abstracts these flags for multiple toolchain support

Signed-off-by: Grant Ramsay <gramsay@enphaseenergy.com>
2023-11-13 10:21:41 +01:00
Grant Ramsay
679d82c484 libc: Add GCC fno-builtin-malloc flag to common stdlib compilation
This prevents the compiler from optimizing calloc into an
infinite recursive call.

For example a call to malloc + memset zero at GCC -O2 will be
replaced by a call to calloc. This causes infinite recursion
if the function being implemented *is* calloc.

fno-builtin-malloc forces the compiler to avoid this optimization.

Signed-off-by: Grant Ramsay <gramsay@enphaseenergy.com>
2023-11-13 10:21:41 +01:00
Keith Packard
f4e6e4b2e5 libc/picolibc: Don't force TLS when using picolibc module
The picolibc module can be built without thread local storage support if
desired. Allow that by using 'imply' instead of 'select'. However, when
using the toolchain picolibc, we assume that TLS will be enabled wherever
supported, so make sure we match by adding a 'select' for this case.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-11-07 09:42:19 +01:00
Johan Hedberg
6e8a1f5859 acpi: shell: Fix specifying string precision
The ACPI table signature is not null terminated, so a precision needs to
be provided in format strings. There was an attempt to do this, but it
was done in an incorrect way, which resulted in garbage characters
getting printed.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2023-11-06 12:32:31 +01:00
Johan Hedberg
584a1cf0fc acpi: Use ACPICA typedef names when available
For most types, ACPICA provides both a struct name as well as a typedef.
The struct names follow the exact same naming style as Zephyr's ACPI
API, which makes it impossible to distinguish which type is defined by
Zephyr and which comes from ACPICA. It's therefore better to use the
typedefs, since they follow a distinct style compared to the Zephyr API.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2023-11-06 12:32:31 +01:00
Johan Hedberg
31cafc3049 acpi: Remove redundant newline characters from logs
The log functions themselves automatically add newline characters, so no
need to do it when calling the log macros.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2023-11-06 12:32:31 +01:00
Johan Hedberg
fe382d4d30 acpi: shell: Remove unnecessary newlines
The ACPI shell command create way too much vertical empty space. Remove
the unnecesary newlines and use indentation to indicate grouping of
lines. At the same time, place case statments with variable declarations
behind {} since otherwise both the Zephyr compliance checker and some
other static analyzers get confused by the code in the branch.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2023-11-06 12:32:31 +01:00
Johan Hedberg
537cb1ffbe acpi: Remove unnecessary prs_buffer variable
This was never used for anything, since the ACPI API overwrites the
pointer when fetching a resource list.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2023-11-06 12:32:31 +01:00
Johan Hedberg
2ca5a3213f acpi: Don't mix ACPI_STATUS and "int" return types
ACPI_STATUS variables should not store values of any other error domain
(like negative POSIX error codes used for Zephyr's ACPI API).

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2023-11-06 12:32:31 +01:00
Johan Hedberg
4f2d03195b acpi: Move local pci_prt_table processing to the right place
The acpi_get_irq_table() function takes a pointer to a table that can
come from anywhere, i.e. it doesn't have to be the acpi.pci_prt.table
that acpi.c uses. Because of this, the correct place to iterate and
process the acpi.pci_prt_table is in the function that actually passes
acpi.pci_prt_table to the acpi_get_irq_table() function.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2023-11-06 12:32:31 +01:00
Johan Hedberg
149b00f70c acpi: Fix acpi_get_irq_routing_table() rt_size parameter meaning
The acpi_get_irq_routing_table() takes a pointer to an array of
ACPI_PCI_ROUTING_TABLE elements rather than a generic buffer pointer
(e.g. void *).

Because of the above, it makes sense to specify the array size as an
actual ARRAY_SIZE() value, since it makes no sense to accept buffers
which are not a multiple of sizeof(ACPI_PCI_ROUTING_TABLE) long.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2023-11-06 12:32:31 +01:00
Johan Hedberg
88120ae351 acpi: Fix status variable type
Use the appropriate ACPI_STATUS type for any status variable that stores
return values from ACPICA APIS.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2023-11-06 12:32:31 +01:00
Johan Hedberg
03cb76e59d acpi: Rename bus_ctx to simply acpi
ACPI is not treated as a bus in Zephyr. Rename the global context from
bus_ctx to simply acpi.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2023-11-06 12:32:31 +01:00
Johan Hedberg
5d8bbca05b acpi: Don't include PCI PRT code if not enabled in Kconfig
If the CONFIG_PCIE_PRT option is disabled it makes no sense to bloat the
ACPI build with PRT-related code or static tables. The diff looks a bit
larger since functions in acpi.c had to be shuffled around to be able to
be included in a single "#ifdef CONFIG_PCIE_PRT" block.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2023-11-06 12:32:31 +01:00
Johan Hedberg
3aa8b9ab83 acpi: Remove unused Kconfig variable
ACPI currently uses implicit (auto) initialization, i.e. it doesn't need
any init level or priority.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2023-11-06 12:32:31 +01:00
Johan Hedberg
e562784bac acpi: Remove unnecessary static array
The acpi_tables array is only needed for systems where dynamic memory
allocation is not available during the early ACPI init phase. In the
Zephyr case we can immediately start using k_malloc, so this is
unnecessary.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2023-11-06 12:32:31 +01:00
Anas Nashif
a08bfeb49c syscall: rename Z_OOPS -> K_OOPS
Rename internal API to not use z_/Z_.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-11-03 11:46:52 +01:00
Anas Nashif
9c4d881183 syscall: rename Z_SYSCALL_ to K_SYSCALL_
Rename internal API to not use z_/Z_.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-11-03 11:46:52 +01:00
Anas Nashif
c25d0804f0 syscall: rename z_object_find -> k_object_find
Rename internal API to not use z_/Z_.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-11-03 11:46:52 +01:00
Anas Nashif
43a7402baf syscall: rename z_object_recycle -> k_object_recycle
Rename z_object_recycle and do not use z_ for internal APIs.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-11-03 11:46:52 +01:00
Anas Nashif
df9428991a syscall: Z_SYSCALL_MEMORY_ARRAY -> K_SYSCALL_MEMORY_ARRAY
Rename macros and do not use Z_ for internal APIs.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-11-03 11:46:52 +01:00
Anas Nashif
4e396174ce kernel: move syscall_handler.h to internal include directory
Move the syscall_handler.h header, used internally only to a dedicated
internal folder that should not be used outside of Zephyr.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-11-03 11:46:52 +01:00
Anas Nashif
a6b490073e kernel: object: rename z_object -> k_object
Do not use z_ for internal structures and rename to k_object instead.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-11-03 11:46:52 +01:00
Johan Hedberg
ff0b803334 acpi: Fix ACPI PCI bus handle
The PRT bus name for most (especially older) platforms is _SB.PCI0. Only
newer platforms use something else (like _SB.PC00).

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2023-10-31 09:01:50 +01:00
Johan Hedberg
b95931859e acpi: Fix using correct buffer length for irq rt_table
We should use the given rt_size variable to indicate the size of
rt_table.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2023-10-31 09:01:50 +01:00
Johan Hedberg
ab46cef69f acpi: Fix ACPICA initialization routine order
The upstream ACPICA example initialization order does AcpiLoadTables()
before calling AcpiEnableSubsystem(), so use this order in Zephyr too.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2023-10-31 09:01:50 +01:00
Johan Hedberg
a0203d1b20 acpi: Fix trying to register a system memory handler
ACPICA itself already registers its own handler (which works perfectly
fine for our purposes). Furthermore, ACPICA will always fail trying to
register another handler, unless the previous one is explicitly cleared
(which is something the Zephyr code didn't do).

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2023-10-31 09:01:50 +01:00
Krzysztof Chruściński
0ca5fdc6e8 lib: os: cbprintf: Add option to enable pointer checking
Add CONFIG_CBPRINTF_CONVERT_CHECK_PTR which enables support for
checking if string candidate pointer is not %p. It is by default
disabled when logging strings are removed from the binary. Option
is added to save code.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
2023-10-31 09:01:38 +01:00
Tomasz Bursztyka
c294b7d270 lib/acpi: Fix the behavior to fit with how it used to be
z_acpi_get_cpu() used to retrieve the local apic on enabled CPU, where
n was about the n'th enabled CPU, not just the n'th local apic.
The system indeed keeps local apic info also about non-enabled CPU,
and we don't care about these as there is nothing to do about it.

This issue exists on up_squared board for instance, but it's a common
one anyway.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2023-10-27 17:36:31 +01:00
Jukka Rissanen
83c875adab hostap: Move the relevant config options away from hostap
Moving the Zephyr specific config options from
modules/hostap/Kconfig to corresponding Kconfig where the
option is specified.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-10-26 09:48:47 +02:00
Alberto Escolar Piedras
f8c202ec1c lib/posix clock: Add missing kconfig dependency
POSIX clock cannot be used with the host libC
when building with the POSIX architecture.
Let's ensure it via kconfig.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-10-25 09:58:07 +02:00
Keith Packard
1e40199c8d libc: Add REQUIRES_FLOAT_PRINTF to indirectly set printf support
Instead of making applications use C library specific settings to enable
floating point support in printf, provide this indirect symbol which then
detects which C library is in use and selects the correct configuration for
each.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-10-25 08:32:06 +02:00
Flavio Ceolin
564adad952 treewide: Add CODE_UNREACHABLE after k_thread_abort(current)
Compiler can't tell that k_thread_abort() won't return and issues a
warning unless we tell it that control never gets this far.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2023-10-24 09:04:42 +02:00
Patryk Duda
3ec58ff749 libc/newlib: Add support for LLVM toolchain
When Zephyr is compiled using LLVM toolchain, we don't need to link with
libgcc to resolve libc dependencies. With this patch, the trick will be
applied only when the GNU compiler is used. Otherwise, we will just link
libc, which works for LLVM.

Signed-off-by: Patryk Duda <pdk@semihalf.com>
2023-10-23 11:41:57 +02:00
Alberto Escolar Piedras
f97ac476f3 libC Kconfig: Do not default to picolibc for native_sim due to GETOPT
Defaulting to picolibc when selecting GETOPT is a bit nicer for users
in some cases, but not required.
But too many things require GETOPT (for ex. some SHELL configurations)
in combinations with native posix drivers which do not support
yet embedded libcs (for ex. the native USB driver)
(see https://github.com/zephyrproject-rtos/zephyr/issues/60096 )

Which leads to configurations in those cases which cannot be built.
Keep defaulting to the external libC in this case.

This reverts the getop part of this commit:
5f8057e262

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-10-23 10:25:22 +02:00
Alberto Escolar Piedras
5f8057e262 libC kconfig: default to PICOLIBC for NATIVE_LIBRARY if POSIX_API
When building with the POSIX_API we cannot use the host libC.
When using GETOPT it is similarly quite difficult for users
to use the host libC.
So to make it easier for users, let's just default to PICOBLIC
in those cases,
while we continue defaulting to the host library in other cases
so users can use the Linux APIs for whatever test functionality
they want.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-10-20 15:01:34 +02:00
Patryk Duda
ce438da14f llvm: Add support for LLVM libc++ C++ Standard Library
LLVM toolchain provides its own C++ standard library called libc++.
This patch adds new LLVM_LIBCXX config which should be used to indicate
that libc++ is used.

Information about library can be found at https://libcxx.llvm.org

Signed-off-by: Patryk Duda <pdk@semihalf.com>
2023-10-20 14:59:31 +02:00
Arunmani Alagarsamy
53f1af9bd7 timer: add support for sigev_thread flag in zephyr timer posix api
add support for the sigev_thread flag in zephyr timer posix api following
the behaviour described in the linux man page. with this enhancement,
a single thread is created to receive all notifications from timers

Signed-off-by: Arunmani Alagarsamy <arunmani@linumiz.com>
2023-10-20 14:52:07 +02:00
Christopher Friedt
7e8953ed7f posix: ensure that pooled ipc type is user-configurable
Previously it was not possible to n-select e.g.
CONFIG_PTHREAD_BARRIER because the Kconfig template for it
lacked a prompt. This made it impossible to disable some
unused POSIX features and unnecessarily increased code size
if unused code sections were not discarded by the linker.

Add a prompt so that each pooled IPC type is
user-configurable and can be disabled if unneeded.

Further, ensure that only the selected sources from lib/posix
are included in the cmake build.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-10-13 11:13:07 -04:00
Christopher Friedt
12ea06cac2 posix: fdtable: ensure stdin, stdout, and stderr are initialized
Ensure that stdin, stdout, and stderr are initialized statically.

Previously, the mutex and condition variable were uninitialized.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-10-13 04:48:47 -04:00
Keith Packard
e7126b5d84 libc/common: Place malloc data structures in libc partition
Leave the malloc partition so that it only contains the heap itself; this
lets the initialization code adjust the address range when configuring the
arena at startup.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-10-10 23:39:40 +03:00
Keith Packard
eb024b655f libc: Control Z_LIBC_PARTITION_EXISTS from Kconfig
Instead of adding every possible subsystem which places variables in the C
library memory partition in libc-hooks.h, place those conditions in the
related Kconfig files and simplify the libc-hooks.h to just looking at
CONFIG_NEED_LIBC_MEM_PARTITION.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-10-10 23:39:40 +03:00
Flavio Ceolin
e7bd10ae71 random: Rename random header
rand32.h does not make much sense, since the random subsystem
provides more APIs than just getting a random 32 bits value.

Rename it to random.h and get consistently with other
subsystems.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2023-10-10 14:23:50 +03:00
Al Semjonovs
955d85aa67 libc: picolibc: Fix picolibc to allow third party CPP
Picolibc dependencies limit ability to use third party minimal
implementations of CPP when enablng PICOLIBC_USE_MODULE.

Signed-off-by: Al Semjonovs <asemjonovs@google.com>
2023-10-09 15:05:39 +03:00
Wilfried Chauveau
d79ba86c04 lib: libc: armstdc: add missing retarget of fputc to _stdout_hook
Fixes #62677 where printf defaults to using armclang's semihosting backed
implementation of printf.

Signed-off-by: Wilfried Chauveau <wilfried.chauveau@arm.com>
2023-10-04 11:02:37 +02:00
Jaxson Han
d489ec087e lib: posix: mutex: to_posix_mutex should be spinlocked
to_posix_mutex allocates a mutex which will race to change the value of
the lock, thus to_posix_mutex should be performed under the spinlock to
prevent the racy issue.

Signed-off-by: Jaxson Han <jaxson.han@arm.com>
2023-10-03 22:30:38 -04:00
Flavio Ceolin
15aa3acaf6 kconfig: Remove MP_NUM_CPUS usage
Zephyr's code base uses MP_MAX_NUM_CPUS to
know how many cores exists in the target. It is
also expected that both symbols MP_MAX_NUM_CPUS
and MP_NUM_CPUS have the same value, so lets
just use MP_MAX_NUM_CPUS and simplify it.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2023-10-03 17:45:53 +01:00
Andrei Emeltchenko
06e8e9f5f5 posix: pthread: Reuse variable for return code
No need to have different variables for different function's return
codes.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2023-10-03 15:23:43 +01:00
Andrei Emeltchenko
0ee0380643 posix: pthread: Move logging out of spinlock area
Move LOG_ERR() outside of K_SPINLOCK().

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2023-10-03 15:23:43 +01:00
Andrei Emeltchenko
3d82fbe357 posix: pthread: Remove dead code warning
It is better to put LOG_ERR() before K_SPINLOCK_BREAK().

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2023-10-03 15:23:43 +01:00
Peter Mitsis
e6f1090553 kernel: Integrate object core statistics
Integrates object core statistics framework into the following
kernel objects:
  sys_mem_blocks, k_mem_slab
  threads, _cpu, z_kernel

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2023-09-30 08:04:14 +03:00
Peter Mitsis
6df8efe354 kernel: Integrate object cores into kernel
Integrates object cores into the following kernel structures
   sys_mem_blocks, k_mem_slab
   _cpu, z_kernel
   k_thread, k_timer
   k_condvar, k_event, k_mutex, k_sem
   k_mbox, k_msgq, k_pipe, k_fifo, k_lifo, k_stack

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2023-09-30 08:04:14 +03:00
Peter Mitsis
baea37aeb4 kernel: Re-factor sys_mem_blocks definition
Rearranges the sys_mem_blocks fields so that information that describes
how much of the memory block is used is co-located. This will allow
easier of its statistics into the object core statistics reporting
framework.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2023-09-30 08:04:14 +03:00
Tom Burdick
41e0a4a371 llext: Linkable loadable extensions
Adds the linkable loadable extensions (llext) subsystem which provides
functionality for reading, parsing, and linking ELF encoded executable
code into a managed extension to the running elf base image.

A loader interface, and default buffer loader implementation,
make available to the llext subsystem the elf data. A simple management
API provide the ability to load and unload extensions as needed. A shell
interface for extension loading and unloading makes it easy to try.

Adds initial support for armv7 thumb built elfs with very specific
compiler flags.

Signed-off-by: Tom Burdick <thomas.burdick@intel.com>
Co-authored-by: Chen Peng1 <peng1.chen@intel.com>
Co-authored-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2023-09-29 20:50:38 -04:00
Daniel Leung
0a50ff366e kernel: rename z_current_get() to k_sched_current_thread_query()
The original idea of z_current_get() was to be the counterpart
of k_current_get() when thread local variable for current has
not been initialized if TLS is enabled, otherwise they are
the same function. Now since z_current_get() is being used
outside of core kernel, rename it under kernel namespace so
other subsystem can conceptually use them too.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2023-09-28 16:15:46 +02:00
Keith Packard
8f5d0791bf sys/time_units.h: Convert time conversion to macros
The intent of this patch is to leave all of the semantics of the macros
unchanged, only replacing the easy-to-read static inline conversion
function with a pile of ?: operators.

Ick. This is not a cleanup. However, what it does enable is using constant
results while initializing global variables, which cannot be done with
either static inline functions or even statement expressions, even when
those generate constant results.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-09-28 16:15:27 +02:00
Anas Nashif
6d23a960db lib: os: build fdtable conditionally
Stop building fdtable by default, make it conditional and build it only
when needed.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-09-28 06:25:16 -04:00
Michal Morsisko
39aa2ad719 lib: crc: Add CRC4
Add two new functions: crc4 for generic calculations
of CRC4, and crc4_ti which use look-up table for
faster calculations of CRC4 algortihms that base
on 0x03 polynomial.

Signed-off-by: Michal Morsisko <morsisko@gmail.com>
2023-09-26 08:36:09 +02:00
Keith Packard
ae92df1e4a libc/common: Don't use malloc mutex when CONFIG_MULTITHREADING=n
When multithreading is disabled, the whole mutex infrastructure isn't
available. The common malloc code wasn't checking for this case which
caused build failures.

Signed-off-by: Keith Packard <keithp@keithp.com>
Tested-by: Detlev Zundel dzu@member.fsf.org
2023-09-25 18:42:21 -04:00
Christopher Friedt
b142e9dd5e posix: logging: use log module register instead of declare
Log modules should be registered with LOG_MODULE_REGISTER
rather than LOG_MODULE_DECLARE. It seems the latter works
on most platforms (at least with in minimal mode as configured
with ZTest).

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-09-19 16:11:58 +01:00
Christopher Friedt
d9841ca9fb posix: cond: add logging for condition variables
Add logging for POSIX condition variable operations.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-09-19 06:16:10 -04:00
Christopher Friedt
0958c46e23 posix: mutex: add logging for POSIX mutexes
Add logging for POSIX mutex operations.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-09-19 06:16:10 -04:00
Christopher Friedt
d2845abcb3 posix: pthread: add logging for POSIX threads
Add logging to POSIX threads.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-09-19 06:16:10 -04:00
Christopher Friedt
9cff600e25 posix: add logging to pooled resource templates
Support logging for all POSIX pooled resource types such as
pthread_t, pthread_mutex_t, and pthread_cond_t.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-09-19 06:16:10 -04:00