Commit graph

1698 commits

Author SHA1 Message Date
Christopher Friedt
1731010869 posix: pthread: do not assert in pthread_exit() on k_thread
If `pthread_exit()` is called from a `k_thread`, then we would
previously trigger an assertion. The problem with that, is that
is POSIX is acting as a compatibility layer.

Given that it is a reasonable expectation to have the calling
thread exit or abort when calling `pthread_exit()`, lets do just
that.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-09-16 18:56:33 -04:00
Daniel Leung
b21d35d357 libc: picolibc: disable prompt for PICOLIBC_USE_MODULE...
...when toolchain does not have native picolibc support.
Without native support, picolibc needs to be built from
the module. Disabling the prompt means this kconfig
takes on whatever default value specified, which is
to build source from module when there is no native
support.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2023-09-13 17:36:46 -04:00
Daniel Leung
01dd2f0897 libc: picolibc: extend support beyond Zephyr SDK
The picolibc kconfigs were tied to ZEPHYR_TOOLCHAIN_VARIANT
and it only supported the Zephyr SDK, which is the only
toolchain having picolibc built-in at the moment. This
commit generalizes that to use TOOLCHAIN_HAS_PICOLIBC
cmake variable the same way as newlib: TOOLCHAIN_HAS_NEWLIB.
This provides the ability for other toolchains to declare
their support for picolibc.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2023-09-13 17:36:46 -04:00
Anas Nashif
8634c3b444 kernel: move wait_q.h header to be internal
This header does not expose any public APIs, so move it under
kernel/include and change files including it.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-09-12 12:55:36 -04:00
Anas Nashif
1fd3171f7b arch: remove wait_q.h include
This include is not used directly in those files, so remove it.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-09-12 12:55:36 -04:00
Najumon B.A
e5ec893c00 lib: acpi: add support for MAD table and DMAR table
add support for retrieve MAD and DMAR table information. Provided
two new interface namely acpi_dmar_entry_get() and acpi_drhd_get()
for retrieve DMA Remapping Reporting and DMA-remapping hardware
unit definition (DRDH).

Signed-off-by: Najumon B.A <najumon.ba@intel.com>
2023-09-07 09:42:38 +02:00
Keith Packard
f0daf904bb libc: Default to picolibc where supported
This switches the default C library to picolibc for all targets which
support it.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-09-04 15:29:13 -04:00
Carles Cufi
8c748fd005 kernel: Modify the signature of k_mem_slab_free()
Modify the signature of the k_mem_slab_free() function with a new one,
replacing the old void **mem with void *mem as a parameter.

The following function:
void k_mem_slab_free(struct k_mem_slab *slab, void **mem);

has the wrong signature. mem is only used as a regular pointer, so there
is no need to use a double-pointer. The correct signature should be:
void k_mem_slab_free(struct k_mem_slab *slab, void *mem);

The issue with the current signature, although functional, is that it is
extremely confusing. I myself, a veteran Zephyr developer, was confused
by this parameter when looking at it recently.

All in-tree uses of the function have been adapted.

Fixes #61888.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2023-09-03 18:20:59 -04:00
Tom Finet
d09a1c39f9 posix: implement clock_nanosleep
Implements the posix clock_nanosleep function, where both relative
and absolute sleeps are made as absolute sleeps.
The nanosleep() function is a special case of clock_nanosleep(),
and so has been refactored to simply call it.

Signed-off-by: Tom Finet <tom.codeninja@gmail.com>
2023-08-30 13:02:58 -07:00
Mateusz Marszalek
61219dacc6 POSIX: Implement pthread_condattr functions
Added:
pthread_condattr_init
pthread_condattr_destroy
pthread_condattr_getclock
pthread_condattr_setclock

Signed-off-by: Mateusz Marszalek <matti.marszalek@gmail.com>
2023-08-30 13:01:03 -07:00
ferar alashkar
11e2c58928 lib: os: hex: clarify controlling expression
add explicit boolean type to 'if' statement controlling expression, thus
improving code readability and maintainability, complying with required
[misra-c2012-14.4] rule which states; The controlling expression of an
if statement and the controlling expression of an iteration-statement
shall have essentially boolean type.

Found as a coding guideline violation (Rule 14.4) by static code
scanning tool.

Note: Tested on STM32L5 Nucleo-144 board (stm32l552xx).

Signed-off-by: ferar alashkar <ferar.alashkar@gmail.com>
2023-08-23 16:06:05 +01:00
Christopher Friedt
9f1e4fa86c posix: signal: type corrections for sigval, sigevent, notify attr
* `struct sigevent` is not type-defined
* `union sigval` is not type-defined
* `struct sigevent` must include `sigev_notify_attributes`

For more information, see https://bit.ly/3YfnELI

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-08-23 10:06:00 +02:00
Daniel Leung
e38fc6de8a cmake: enable -Wshadow partially for in-tree code
This enables -Wshadow to warn about shadow variables on
in tree code under arch/, boards/, drivers/, kernel/,
lib/, soc/, and subsys/.

Note that this does not enable it globally because
out-of-tree modules will probably take some time to fix
(or not at all depending on the project), and it would be
great to avoid introduction of any new shadow variables
in the meantime.

Also note that this tries to be done in a minimally
invasive way so it is easy to revert when we enable
-Wshadow globally. Source files under modules/, samples/
and tests/ are currently excluded because there does not
seem to be a trivial way to add -Wshadow there without
going through all CMakeLists.txt to add the option
(as there are 1000+ files to change).

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2023-08-22 11:39:58 +02:00
Anas Nashif
e6885a4515 lib: crc: move from lib/os to lib/crc
Move crc implementation to own directory and reduce clutter in lib/os.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-08-18 08:52:16 +03:00
Anas Nashif
234032cac7 os: disable CRC by default
Users of CRC shall enable the the kconfig and not rely on it being
enabled by default.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-08-18 08:52:16 +03:00
Keith Packard
101cdcd86e libc/common: Add memalign
Memalign is another name for the posix aligned_alloc function, although it
has weaker restrictions on the relationship between the alignment and size.

memalign() is used internally by the libstdc++ when built for 'newlib'
targets (which includes picolibc) instead of aligned_alloc() due to a bug
in gcc, so we need to provide an implementation of this when using that
library, even though it's not part of the Zephyr C library API.

When a fix for the libstdc++ is merged upstream and can be consider a
reasonable dependency for Zephyr, this work-around can be removed.

Closes: #57899

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-08-10 11:35:17 -04:00
Daniel Leung
c967a9a8c8 acpi: rename shadow variables
Renames	shadow variables found by -Wshadow.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2023-08-10 08:14:12 +00:00
Flavio Ceolin
d16c5b9048 kernel: canaries: Allow using TLS to store it
Add new option to use thread local storage for stack
canaries. This makes harder to find the canaries location
and value. This is made optional because there is
a performance and size penalty when using it.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2023-08-08 19:08:04 -04:00
Nicolas Pitre
4755ebee6a lib/cbprintf: CONFIG_CBPRINTF_LIBC_SUBSTS should be optional
When aiming for the smallest build result, it is desirable to
use CONFIG_CBPRINTF_NANO together with CONFIG_MINIMAL_LIBC. However
that doesn't mean we need all the functions enabled by having
CONFIG_CBPRINTF_LIBC_SUBSTS=y which increases the binary size even if
those functions are not used and not linked in.

When CONFIG_CBPRINTF_LIBC_SUBSTS=n such functions are not defined
anyway so z_cbvprintf_impl() won't be invoked with missing
functionalities.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2023-08-07 11:26:40 +02:00
Yong Cong Sin
43123e77c0 posix: signal: extend strsignal buf to cover entire INT range
extends the char buffer in the strsignal function to cover the
entire range of `int`

Had to use `-INT_MAX` as the compiler resolves

```
STRINGIFY(INT_MIN)
```

to:

```
(-2147483647 - 1)
```

instead of:

```
-2147483648
```

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2023-08-04 15:54:18 -04:00
Gerard Marull-Paretas
90b247b90b lib: os: add support for system power off
Add a new API to perform an immediate system power off:
`sys_poweroff()`.

Until now, this functionality has been implemented via the system power
management module, but in a clunky fashion. The way system PM works is
by defining some idle states in devicetree, that, given some properties
(e.g. minimal residency, exit latency, etc.) are automatically selected
when system goes to idle based on the expected next wake-up. However,
system off is a power state that one typically wants to control manually
from the application because it implies state loss, and in most cases,
configuring some sort of wake-up source. So in general, it is not
desired to let the system enter this state automatically. This led to
the following stuff in-tree:

from `boards/arm/mimxrt595_evk/mimxrt595_evk_cm33.dts`:

```c
/*
 * Deep power-down mode is supported in this SoC through
 *  'PM_STATE_SOFT_OFF' state. There is no entry for this in device tree,
 *  user can call pm_state_force to enter this state.
 */
```

That is, state not being defined in devicetree so that PM subsystem
doesn't pick it automatically, but still implemented in in the PM hooks:

from `soc/arm/nxp_imx/rt5xx/power.c`, `pm_state_set()`:
```c
case PM_STATE_SOFT_OFF:
	set_deepsleep_pin_config();
	POWER_EnterDeepPowerDown(EXCLUDE_FROM_DEEP_POWERDOWN);
	break;
```

And to actually make use of this state, users had to do this kind of
abominations:

```c
pm_state_force(0u, &(struct pm_state_info){ PM_STATE_SOFT_OFF, 0, 0 });

/* Now we need to go sleep. This will let the idle thread runs and
 * the pm subsystem will use the forced state. To confirm that the
 * forced state is used, lets set the same timeout used previously.
 */
k_sleep(K_SECONDS(SLEEP_S));

printk("ERROR: System off failed\n");
while (true) {
	/* spin to avoid fall-off behavior */
}
```

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
2023-08-04 16:59:36 +02:00
Patryk Duda
4b94fc3da2 llvm: Add support for selecting runtime library
This patch adds Kconfig options to select either GNU libgcc or LLVM
compiler-rt. The 'rtlib' flag is provided in a config file, so this
patch introduces 'clang_libgcc.cfg' and 'clang_compiler_rt.cfg' which
enable appropriate library. The file is selected by concatenating
the 'clang_' prefix with library name.

Signed-off-by: Patryk Duda <pdk@semihalf.com>
2023-08-03 19:35:11 +02:00
Christopher Friedt
f9ecc9a658 posix: mutex: return ETIMEDOUT from pthread_mutex_timedlock()
The normative spec for `pthread_mutex_timedlock()` says that
it should return `ETIMEDOUT` when a timeout occurs. However,
currently it returns `EAGAIN`, which reflects what is returned
by `k_mutex_lock()`.

Inspect and update the return value to account for this slight
difference.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-08-02 12:04:34 -04:00
Keith Packard
d47025bd84 libc/picolibc: Remove unused read_stdin/write_stdout hooks
Picolibc doesn't need these two syscall implementations as it uses
zephyr_fputc instead. Make sure that zephyr_putc is declared correctly.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-08-02 10:35:18 +02:00
Christopher Friedt
73bf557a95 posix: pthread: thread return value not set by pthread_join()
Ensure that the thread return value is set by `pthread_join()`
when `status` is non-NULL.

Additionally, we have an opportunity to synchronously clean
up thread stacks in `pthread_join()`, which is preferable.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-07-30 19:17:49 -04:00
Christopher Friedt
52f00882d1 posix: pthread: report appropriate return value instead of 0
Discovered this while implementing c11 threads, but there
was a regression recently that made it so that `pthread_join()`
would report success when attempting to join a thread that had
been detached with `pthread_detach()`.

Technically now that is undefined behaviour, but historically,
we have reported `EINVAL`, which was the older specified
return value.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-07-30 12:51:12 -04:00
ferar alashkar
595bcda87c lib: os: dec: add misra-c2012 compliance changes
1. change explicit type cast of essential character type, complying with
required [misra-c2012-10.2] rule which states; Expressions of
essentially character type shall not be used inappropriately in addition
and subtraction operations, and

2. add explicit boolean type to 'if' statement controlling expression,
consolidating it with 'buflen' type, thus improving code readability and
maintainability , complying with required [misra-c2012-14.4] rule which
states; ; The controlling expression of an if statement and the
controlling expression of an iteration-statement shall have essentially
boolean type, and

3. add enclosing parentheses enforcing and clarifying precedence of
operators, improving code readability and maintainability, complying
with *advisory* [misra-c2012-12.1] rule which states; The precedence of
operators within expressions should be made explicit.

Found as a coding guideline violation (Rules 10.2, 14.4), and coding
guideline recommendation (Rule 12.1) by static code scanning tool.

Note: Tested on STM32L5 Nucleo-144 board (stm32l552xx).

Signed-off-by: ferar alashkar <ferar.alashkar@gmail.com>
2023-07-28 11:15:44 +02:00
ferar alashkar
dddc034923 lib: os: hex: correct explicit cast type
change explicit type cast of essential character type, complying with
required [misra-c2012-10.2] rule which states; Expressions of
essentially character type shall not be used inappropriately in addition
and subtraction operations.

Found as a coding guideline violation (Rule 10.2) by static code
scanning tool.

Note: Tested on STM32L5 Nucleo-144 board (stm32l552xx).

Signed-off-by: ferar alashkar <ferar.alashkar@gmail.com>
2023-07-28 11:15:19 +02:00
ferar alashkar
bba6a1d69e lib: os: hex: add explicit unsigned suffices
add explicit unsigned suffices to various immediate numbers, matching
them to size_t, complying with required [misra-c2012-10.4] rule which
states; Both operands of an operator in which the usual arithmetic
conversions are performed shall have the same essential type category.

Found as a coding guideline violation (Rule 10.4) by static code
scanning tool.

Note: Tested on STM32L5 Nucleo-144 board (stm32l552xx).

Signed-off-by: ferar alashkar <ferar.alashkar@gmail.com>
2023-07-28 11:15:12 +02:00
Damian Krolik
74859013f1 lib: cpp: name the choice group for selecting C++ standard
Add a name to the choice group for selecting the C++
standard to be able to override the default standard in
Kconfig.* files.

Signed-off-by: Damian Krolik <damian.krolik@nordicsemi.no>
2023-07-27 22:33:14 -04:00
Gerard Marull-Paretas
b51dd4ade0 lib: hash: use new c++ Kconfig symbols
SYS_HASH_MAP_CXX was using deprecated C++ symbols.

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
2023-07-26 14:53:51 +02:00
Carlo Caione
561a80d109 shared_multi_heap: Use a data struct
Embed all the helper structs in one single data struct for easy access
indexed on the memory attr.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2023-07-26 07:56:39 -04:00
Carlo Caione
5d6d93c3b5 shared_multi_heap: Rename heap counter
We are calling the heap counter `attr_cnt` and that is misleading.
Rename it.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2023-07-26 07:56:39 -04:00
Carlo Caione
b7e252c2db shared_multi_heap: Use proper enum instead of int
We have an enum for the memory attr, use that instead of a generic
unsigned int.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2023-07-26 07:56:39 -04:00
Christopher Friedt
115efa2e35 pthread: facilitate dynamically allocated thread stacks
This change allows users to call pthread_create() with
the pthread_attr_t argument equal to NULL.

If Zephyr is configured with `CONFIG_DYNAMIC_THREAD`, then a
suitable thread stack will be allocated via
k_thread_stack_alloc(). The allocated thread stack is
automatically freed via k_thread_stack_free().

This makes the Zephyr implementation of pthread_create()
compliant with the normative spec.

Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
2023-07-24 12:59:43 -04:00
Christopher Friedt
748683fae6 posix: eventfd: remove redundant conditional
Since the argument is a 32-bit unsigned int, all possible
values satisfy the condition that intval < UINT64_MAX - 1.

Remove the redundant conditional.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-07-21 06:33:19 -04:00
Daniel Leung
4c80949ecf libc: remove no longer valid kconfig comment
After commit 9a0aebc5fd,
the exclusion of qemu_x86_tiny is no longer and the "depends on"
option was removed. However, the comment about that remained.
Remove the comment as it is no longer valid.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2023-07-20 12:58:36 -04:00
Jai Arora
82f5db37d2 posix: Changes pthread_equal to regular type function
Patch changes pthread_equal function from static inline to regular
function type

Fix for issue#59942

Signed-off-by: Jai Arora <infolinesoni@gmail.com>
2023-07-20 16:24:16 +02:00
Yong Cong Sin
43c549305b posix: signal: check RTSIG limit
Use build assert to make sure that the realtime signal
constants are configured properly in the Kconfig.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2023-07-18 15:35:42 -04:00
Yong Cong Sin
335acf817d posix: signal: implement strsignal
Implementation and ztest for strsignal.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2023-07-18 15:35:42 -04:00
Yong Cong Sin
ff4b81e5e5 posix: signal: implement sigismember
Implementation and ztest for sigismember.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2023-07-18 15:35:42 -04:00
Yong Cong Sin
88cd494430 posix: signal: implement sigdelset
Implementation and ztest for sigdelset.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2023-07-18 15:35:42 -04:00
Yong Cong Sin
8011449542 posix: signal: implement sigaddset
Implementation and ztest for sigaddset.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2023-07-18 15:35:42 -04:00
Yong Cong Sin
b9720e27c0 posix: signal: implement sigfillset
Implementation and ztest for sigfillset.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2023-07-18 15:35:42 -04:00
Yong Cong Sin
6910eb4a9b posix: signal: implement sigemptyset
Implementation and ztest for sigemptyset.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2023-07-18 15:35:42 -04:00
Yong Cong Sin
e3c49b9573 posix: signal: add initial header
Initial header for the signal APIs.
APIs to be implemented in later commit.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2023-07-18 15:35:42 -04:00
Markus Fuchs
c0d7218ef2 json: Skip unknown fields on parsing
Skip child objects and arrays that are not specified in the given object
descriptor when parsing a JSON input string.
This patch adds support for extra child arrays which previously were not
supported by the parser as opposed to additional child objects.

Fixes #47988

Signed-off-by: Markus Fuchs <markus.fuchs@ch.sauter-bc.com>
2023-07-18 11:03:21 +00:00
Lawrence King
7dae27a90d libc: minimal: math sqrt: sqrtf: fix numeric accuracy of sqrt and sqrtf.
Changed initial guess from a simple x/3 to dividing the exponent by 2.
This makes large or small numbers like 10e10 and 01e-10 converge in a few
loops.

Added a loop counter to ensure that the algorithm breaks out of the loop in
the case that the algorithm doesn't converge (toggling between two
numbers).

Added test cases for sqrt and sqrtf in libc. Tested with a range of numbers
between 10e10 and 10e-10. Verify good accuracy in test case.

Closes: #55962

Signed-off-by: Lawrence King <lawrencek52@gmail.com>
2023-07-14 12:42:41 -04:00
Jaxson Han
0df7bd26ed lib: posix: pthread_rwlock: Fix a racy issue
Multiple reader threads unlocking the read lock simultaneously might
cause the program hang because it's possible that no thread is
identified as the last one to active the writer thread.

To fix the issue, swap the k_sem_give sequence.

Signed-off-by: Jaxson Han <jaxson.han@arm.com>
2023-07-12 09:25:26 +02:00
Keith Packard
021923b58a libc/picolibc: Use mutexes instead of semaphores for non-recursive locks
Picolibc has both recursive and non-recursive mutex uses. The bulk of the
library locking uses the global libc lock, which is a recursive mutex as
that greatly simplifies the implementation.

The only use of non-recursive mutexes is in the stdio code when dealing
with file system I/O via fopen.

Using mutexes for both APIs is valid; the assumption picolibc makes is that
the non-recursive mutexes are somehow cheaper or faster and should be
preferred. However, in Zephyr, recursive mutexes are the default and the
non-recursive locks for picolibc were implemented using semaphores.

Switch the non-recursive picolibc locks to just invoking the existing
recursive functions using mutexes. This avoids pulling in another lock
implementation, saving a bit of space.

This also lets the kernel.memory_protection.mem_map test work on
qemu_x86_tiny where the amount of memory available is 320kB and that is
nearly filled by this test case, leaving too little space for allocating
pages in the k_mem_map_unmap test.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-07-10 15:11:15 +02:00
Harshil Bhatt
8b2a41904d posix: implement timer_getoverrun()
Address missing POSIX function and tests #59956

Signed-off-by: Harshil Bhatt <harshilbhatt2001@gmail.com>
2023-07-10 09:30:33 +02:00
Yong Cong Sin
4580708f54 posix: implement uname
Add implementation for posix uname.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2023-07-09 16:57:16 -04:00
Harshil Bhatt
78c8176c4d posix: Implement pthread_barrieratter functions
Added pthread_barrieratter_init() #59936,
pthread_barrieratter_destroy() #59935,
pthread_barrieratter_getpshared() #59937 and
pthread_barrieratter_setpshared() #59939.

Signed-off-by: Harshil Bhatt <harshilbhatt2001@gmail.com>
2023-07-09 10:40:57 -04:00
Christopher Friedt
694cd58647 posix: pthread: fixes for coverity 321140 and 321092
The `pthread_once_lock` `k_mutex` is statically initialized and
only visible within file scope. Coverity identified it as unsafe
because the return values of `pthread_mutex_lock()` and
`pthread_mutex_unlock()` were unchecked. However, if those
functions were to fail here, it would be indicative that
something far worse has happened.

In any case, we add assertions that these functions
succeed rather than silently ignoring with `(void)`, which
ensures that we have coverage when assertions are enabled,
in test, while removing unneeded code with assertions disable,
in production.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-07-09 08:20:32 -04:00
Christopher Friedt
1434296915 posix: spinlock: additional kconfig for k_spinlock being size 0
The `struct k_spinlock` size is zero bytes under certain
circumstances. This is a bit of a problem, because it breaks a
number of assumptions about things in C.

That should be fixed when #59922 is addressed.

This change is just a hotfix to correct the specific condition
where we will need workarounds in other source files.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-07-06 16:45:26 +00:00
Christopher Friedt
13dcf2e4ed posix: split supported features into separate Kconfig files
This change is setting up for switching over to proper POSIX
option requirements, feature test macros, and a dependency
structure that is reflective of the standard.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-07-06 11:47:07 -04:00
Christopher Friedt
4fcf197c50 posix: rename some files for brevity
Most of the posix source files can be easily identified by a
short name. I.e. most of the `pthread_` prefixed files do not
need the `pthread_` prefix.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-07-06 11:47:07 -04:00
Christopher Friedt
e11856fbfe posix: add Meta copyright as appropriate
A significant enough portion of these files has been
changed to justify adding Meta copyright as well as
that of the original author.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-07-06 11:47:07 -04:00
Keith Packard
4e7930b099 libc/picolibc: Remove -T /dev/null linker arguments
Now that the gcc compiler driver uses the -T flag instead of -Wl,-T, we can
remove the hack here that kept the picolibc specs file from inserting the
picolibc linker script.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-07-06 11:43:09 +02:00
Christopher Friedt
452205ff61 posix: pthread: implement pthread spinlocks
pthread_spinlock_t support is part of the base threading
requirement.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-07-05 21:06:55 -04:00
Alberto Escolar Piedras
7e37482a77 libC: PICOLIBC_SUPPORTED: Enable for NATIVE_LIBRARY
It is possible to build with the PICOLIBC_MODULE
with the POSIX arch targets which use the native
simulator as runner.
Update filtering accordingly.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-07-05 07:01:19 -04:00
Alberto Escolar Piedras
2e6396cf20 libC: STDOUT_CONSOLE narrow filtering by type of native build
It is possible to build with STDOUT_CONSOLE with
the embedded C libraries with the POSIX arch.
Narrow down the filtering accordingly.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-07-05 07:01:19 -04:00
Alberto Escolar Piedras
56dc20eb1d Kconfig: Tidy up dependants of NATIVE_APPLICATION
So they depend or select on the right NATIVE_BUILD
instead of NATIVE_APPLICATION.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-07-05 07:01:19 -04:00
Najumon Ba
f25dfcf88c lib: acpi: added acpi support using acpica lib
Add ACPI support for Zephyr using acpica open source
project. ACPI subsystem use to discover and configure
hardware components, perform power management (e.g. putting
unused hardware components to sleep), auto configuration (e.g.
Plug and Play and hot swapping) etc.

Signed-off-by: Najumon Ba <najumon.ba@intel.com>
2023-06-30 17:53:01 +03:00
Markus Fuchs
8757c71bd0 json: Fix 64-bit support
This patch fixes encoding arrays of objects on 64-bit targets.

Fixes #36696

Signed-off-by: Markus Fuchs <markus.fuchs@ch.sauter-bc.com>
2023-06-29 21:03:48 +02:00
Markus Fuchs
ae6aa6165c json: Fix multidimensional array support
This patch fixes support for encoding and decoding multidimensional arrays
as described by the JSON_OBJ_DESCR_ARRAY_ARRAY() macro.

Currently, the JSON array encoding and decoding functions, arr_encode()
and arr_parse(), expect array elements to be of object or primitive type.
However, arrays may be nested and so an array's elements may also be
arrays.

In order to support nested arrays, two special cases must be considered:

1. The array of objects/arrays sub-descriptor is described by two
`json_obj_descr` structs and so two instead of one `json_obj_descr`
structs must be skipped when iterating over the JSON descriptor to get to
an array's elements.
2. The implicit array item count field has to be considered for the
parent itself and all its child array items when calculating an element's
size.

Fixes #50801

Signed-off-by: Markus Fuchs <markus.fuchs@ch.sauter-bc.com>
2023-06-29 21:03:48 +02:00
Vincent van Beveren
2ae521a5f5 posix: fs: Fixes stat command to return file information
Fixes #58911. Previously the stat command returned
information on the filesystem, but not the file itself.
Because block size is still set this function is
backwards compatible with the previous faulty
behavior.

Signed-off-by: Vincent van Beveren <v.van.beveren@nikhef.nl>
2023-06-23 17:04:34 -04:00
Alberto Escolar Piedras
3454d8f727 lib POSIX: Enable selectively for POSIX ARCH
The POSIX API compatibility shim can be used for some
of the POSIX ARCH targets.
Narrow the Kconfig filtering accordingly.

Note that the recommended configuration when building
with the native simulator is still to use an embedded
C library. Using the host C library will in some cases
cause undesired behaviour.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-06-22 19:46:43 -04:00
Alberto Escolar Piedras
0517c85dcc libC COMMON_LIBC_MALLOC_ARENA_SIZE: provide default for POSIX ARCH
Provide a sensible default for the POSIX architecture,
as now it is possible to build with it.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-06-22 17:16:17 -04:00
Alberto Escolar Piedras
590e8efd4b libC pico hooks: Fix for POSIX arch
The spin loop in _exit() needs a Z_SPIN_DELAY() for the
POSIX architecture, so it does not hang the whole
executable on that infinite loop but only the thread
that exit'ed.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-06-22 17:16:17 -04:00
Alberto Escolar Piedras
8eff5b6c01 libC minimal: Fix for POSIX arch
The spin loop in _exit() needs a Z_SPIN_DELAY() for the
posix architecture, so it does not hang the whole
executable on that infinite loop but only the thread
that exit'ed.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-06-22 17:16:17 -04:00
Marcin Niestroj
44d61bde84 posix: eventfd: fix waking up poll()
Fix a regression introduced by commit e6eb0a705b ("posix: eventfd: revise
locking, signaling, and allocation"), which was a complete rewrite stating
that:

  The `wait_q` and `k_poll_signal` entries were removed from
  `struct eventfd` as they were unnecessary.

In fact, `k_poll_signal` (both `read_sig` and `write_sig`) were used to
wake-up blocking `poll()` invocation in another thread. This is no longer
the case now, i.e. `poll(..., POLLIN)` does not return after calling
`eventfd_write()` on the observed (polled) FD.

Fix this regression by bringing back `read_sig` and `write_sig` to very
similar state as it was before.

Fixes: e6eb0a705b ("posix: eventfd: revise locking, signaling, and
  allocation")
Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
2023-06-22 16:09:42 -04:00
Daniel Leung
1d4d718a9b lib: syscalls: use zephyr_syscall_header
This adds a few line use zephyr_syscall_header() to include
headers containing syscall function prototypes.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2023-06-17 07:57:45 -04:00
Nikolay Agishev
6e940c513c ARC: Fix portability.posix.common.arcmwdtlib test
portability.posix.common.arcmwdtlib test fails with ARCMWDT libc.
This path fixes the test.

STDIN_FILENO and others macroses are used in libc-hooks.c only. So they
defined localy.

Signed-off-by: Nikolay Agishev <agishev@synopsys.com>
2023-06-09 14:17:50 -04:00
Christopher Friedt
89cf4cea56 posix: pthread: mitigate include order sensitivity
Previously, the `posix_internal.h` header needed to be exposed
to the application because we had non-trivial details for
most posix types (pthread, mutex, cond, ...). Since most of
those have been simplified to a typedef'ed integer, we
no longer need to expose that header to the applicaiton.

Additionally, it means that we can adopt normalized
header order in posix.

Additionally, keep more implementation details hidden
and prefer the static keyword on internal symbols where
possible.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-06-09 12:27:04 -04:00
Christopher Friedt
e1f8ea1ad7 posix: pthread: add option for pthread_create() barrier
To enable testing, introduce `CONFIG_PTHREAD_CREATE_BARRIER`.

Some observations were made that running several Qemu SMP targets
concurrently could lead to synchronization problems. On such
targets, it was found that the synchronization issues were
mitigated by introducing a `pthread_barrier_t` shared between
`pthread_create()` and the spawned thread.

It is suggested to enable the option when running many
SMP tests concurrently in several parallel Qemu processes,
e.g. with `twister`.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-06-09 12:27:04 -04:00
Christopher Friedt
85e18746b8 posix: pthread: rework pthreads to use zephyr api
Previously, pthreads suffered from some race conditions.
This was almost inevitable given that it was maintained in
parallel to Zephyr's threading and synchronization API.

The unfortunate side-effect of with that is that it did not
receive the reliability and other improvements that
`k_thread`s did.

Here, we perform a significant update of pthread code so
that it depends directly on public Zephyr API. With that,
we reuse as many concepts as possible and pthreads benefits for
free from any improvement made to Zephyr's threading and
synchronization APIs.

Included with this change, we
* implement state with `ready_q`, `run_q`, and `done_q`
* use `pthread_barrier_wait()` to sync `pthread_create()`
* synchronize internal state with a spinlock

These pthreads are considerably more reliable than
before.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-06-09 12:27:04 -04:00
Christopher Friedt
08ba17683b posix: cond: use k_condvar instead of posix_cond
The internal representation of `pthread_cond_t`,
`struct posix_cond`, is an identical clone of `struct k_condvar`
but without the benefit of being able to use all of the
existing `k_condvar_*()` suite of functions.

The first step in the right direction was switching
the external representation of `pthread_cond_t` to a simple
`int`. Let's take the next step in the right direction, which
is getting rid of `struct posix_cond`.

For now, let's keep this change as a simple type
substitution. Eventually, we should be able to fully switch
to Zephyr API internally.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-06-09 12:27:04 -04:00
Christopher Friedt
0d7ef2297b posix: mutex: use k_mutex instead of posix_mutex
The internal representation of `pthread_mutex_t`,
`struct posix_mutex`, is basically a clone of `struct k_mutex`
but without the benefit of being able to use all of the
existing `k_mutex_*()` suite of functions.

The first step in the right direction was switching
the external representation of `pthread_mutex_t` to a simple
`int`. Let's take the next step in the right direction, which
is getting rid of `struct posix_mutex`.

The only significant difference between `struct k_mutex` and
`struct posix_mutex` is that the latter needs a `type` field.

Since there were a fixed number of `struct posix_mutex`, we
can just externalize the `type` field and reuse
`struct k_mutex` as-is.

For now, let's keep this change as a simple type
substitution. Eventually, we should be able to fully switch
to Zephyr API internally.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-06-09 12:27:04 -04:00
Christopher Friedt
f35fb33b94 posix: pthread: reimplement pthread_barrier using zephyr objects
Previously pthread_barrier_t was implemented in terms of wait
queues and internal scheduler functions.

This introduced some obstacles and inconsistency. In order
to be more consistent, rely only on Zephyr's public API and
reuse as many concepts as possible.

Deprecate `PTHREAD_BARRIER_DEFINE()` since it's non-standard.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-06-09 12:27:04 -04:00
Christopher Friedt
3e27c7f4a7 posix: eventfd: deprecate non-public EFD macros
Deprecate `EFD_IN_USE` and `EFD_FLAGS_SET` as they are not
specified as part of any public `eventfd()` API.

While those are being deprecated, use `_INTERNAL` variants.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-06-02 18:45:46 -04:00
Christopher Friedt
e6eb0a705b posix: eventfd: revise locking, signaling, and allocation
TL;DR - a complete rewrite.

Previously, the prototypical `eventfd()` usage (one thread
performing a blocking `read()`, followed by another thread
performing a `write()`) would deadlock Zephyr. This shortcoming
has existed in Zephyr's `eventfd()` implementation from the
start and the suggested workaround was to use `poll()`.

However, that is not sufficient for integrating 3rd-party
libraries that may rely on proper `eventfd()` blocking
operations such as `eventfd_read()` and `eventfd_write()`.

The culprit was the per-fdtable-entry `struct k_mutex`.

Here we perform a minor revision of the locking strategy
and employ `k_condvar_broadcast()` and `k_condvar_wait()`
to signal and wait on the holder of a given `struct k_mutex`.

It is important to note, however, that the primary means of
synchronizing the eventfd state is actually the eventfd
spinlock. The fdtable mutex and condition variable are mainly
used for the purposes of blocking io (r,w,close) and are not
used in the code path of non-blocking reads.

The `wait_q` and `k_poll_signal` entries were removed from
`struct eventfd` as they were unnecessary.

Additionally, switch to using a bitarray because it is
possibly faster than linear search for allocating and
deallocating eventfd resources.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-06-02 18:45:46 -04:00
Christopher Friedt
90343a1f6d posix: eventfd: un-inline eventfd_read() and eventfd_write()
There is no major benefit to haveing `eventfd_read()` and
`eventfd_write()` as `static inline` functions and it is
arguably much easier to solve the deadlock bug when they
are not `inline`.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-06-02 18:45:46 -04:00
Christopher Friedt
0014dd05f0 lib: os: add k_condvar for each fdtable entry
Complement the `struct k_mutex` in each fdtable entry
with a `struct k_condvar`. The reasoning for this should be
self-evident.

For a bit of history, `fdtable` was introduced in
commit 06eb489c45 ("kernel: add condition variables")
which predates `struct k_condvar`, introduced in
commit f484bbaa26 ("lib: posix: Implement generic file descriptor table")
by almost 2 years.

Additionally, provide a new accessor function,
`z_get_obj_lock_and_cond()`, that (optionally) gets the mutex
and condition variable associated with the provided object and
vtable.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-06-02 18:45:46 -04:00
Krzysztof Chruściński
44988b95c4 lib: os: cbprintf_nano: Fix Coverity issue 316025
Coverity does not like that we are passing a pointer to a location
just beyond fixed array. Inside the function access is done through
negative indexes so there was no memory corruption but to satisfy
Coverity pointer to the last element of the array is passed and
we start from index 0 instead of -1.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
2023-06-02 15:07:34 -04:00
Nicolas Pitre
577a7f2fac cbprintf: correct a Kconfig option help text
People interested in those options may be truly concerned by binary
sizes. Let's provide complete information.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2023-06-01 11:10:51 +02:00
Patryk Duda
7abe39c68a libc: minimal: Add 'break' to suppress fall-through error in atoi.c
Compiling using clang fails with following error:

error: unannotated fall-through between switch labels
[-Werror,-Wimplicit-fallthrough]

Actually, missing 'break' statement is not an error here because
the next label is 'default' which contains 'break' only. For the same
reason we can add it to silence the compiler.

Signed-off-by: Patryk Duda <pdk@semihalf.com>
2023-05-31 14:41:25 -04:00
Keith Packard
b97a4a52a8 libc/common: Increase USERSPACE + POT MPU default arena to 2048 bytes
This matches the size that would be used with the newlib_nano configuration
and allows several tests to complete which would otherwise fail due to
insufficient heap space.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-31 10:02:53 -04:00
Keith Packard
3103f94c6a libc/common: Require at least double alignment for common malloc arena
arm, arc and riscv all have special cases for malloc arena alignment that
might be smaller than the minimum required for a C allocator. In
particular, the riscv value might actually be zero, which turns out to be
an invalid alignment value.

Make sure all of these have alignment that meets the C language
requirements for allocation alignment.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-31 10:02:53 -04:00
Keith Packard
6ccdf47f60 lib/cpp: Remove CONFIG_CPP_MAIN
With Zephyr now always using `int main(void)`, there's no longer any need
for this definition. The last remaining use which gated the declaration of
_posix_zephyr_main isn't necessary as adding that declaration
unconditionally is harmless.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-31 06:16:35 -04:00
Carlo Caione
fecb03ecf8 bitarray: Fix mismatch checking when matching regions
We are currently reporting the wrong mismatching bits in in-between
bundles. Fix this and extend the test to cover the wrong case.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2023-05-25 16:31:23 -04:00
Keith Packard
2200861b96 lib/libc/picolibc: Use SDK picolibc by default
When using the Zephyr SDK toolchain, prefer the pre-built version of
picolibc over using the picolibc module. This will reduce the time it takes
to build applications.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-25 13:33:04 +00:00
Gerard Marull-Paretas
dacb3dbfeb iterable_sections: move to specific header
Until now iterable sections APIs have been part of the toolchain
(common) headers. They are not strictly related to a toolchain, they
just rely on linker providing support for sections. Most files relied on
indirect includes to access the API, now, it is included as needed.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-05-22 10:42:30 +02:00
Miika Karanki
d8166e813b json: add json_calc_encoded_arr_len
Analog to json_obj_encode vs. json_calc_encoded_len which
calculates the object len using json_obj_encode, introduce
json_calc_encoded_arr_len which calculates the length using
json_arr_encode. That is needed when the object to be encoded
is array on the root level.

Signed-off-by: Miika Karanki <miika.karanki@vaisala.com>
2023-05-19 19:52:04 +00:00
Alberto Escolar Piedras
b0668cc586 lib: cpp: Do not define __cxa_atexit() for the posix arch
For the POSIX architecture we do not define an
empty __cxa_atexit() as it otherwise would
override its host libC counterpart. And this would
both disable the atexit() hooks, and prevent possible
test code global destructors from being registered.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-05-12 18:31:44 -04:00
Lucian Zala
2fae5e86b8 libc: minimal: strtoll: Remove typo in SPDX-License-Identifier
BSD-3-Clause-UC is not a valid license name and spdx validator
generates a Warning! when detected. Most probably a typo from
old version of strtoll.c licenced as BSD-4-Clause-UC.

see commit: 570ed08

Signed-off-by: Lucian Zala <zala.lucian@gmail.com>
2023-05-11 06:04:30 -04:00
Keith Packard
5279acba9d libc/common: Make sure malloc arena is PoT aligned on Risc-V
The malloc arena needs to be aligned to a suitable size on Risc-V, and the
Z_RISCV_STACK_GUARD_SIZE variable is a helpful proxy for what size that
is. However, that is not always a power of two, so round it up to make the
linker capable of performing the necessary allocation.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-11 01:25:01 +09:00
Keith Packard
69f4f39aef libc/common: Always build aligned_alloc
This function may be used by system libraries (like libstdc++), so instead
of gating compile on target language, always build this function and gate
API visibility in header files instead.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-11 01:25:01 +09:00
Keith Packard
f74455127d libc/common: Refine check for xtensa _heap_sentry
Not all xtensa targets define the top of usable RAM via the _heap_sentry
address; it looks like the list is limited to esp32, esp32s2, esp32s3 and
intel parts. The first three all define HAS_ESPRESSIF_HAL, so key the test
using that or SOC_FAMILY_INTEL_ADSP.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-11 01:25:01 +09:00
Keith Packard
706d9f6db2 libc/common: Use static initializer for z_malloc_heap_mutex
Instead of explicitly initializing the mutex at runtime, use
SYS_MUTEX_DEFINE to initialize it at build time instead.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-11 01:25:01 +09:00
Kristoffer Rist Skøien
5974617db8 lib: getopt: Fix compiler warning.
current_dash may be used uninitialized

Signed-off-by: Kristoffer Rist Skøien <kristoffer.skoien@nordicsemi.no>
2023-05-09 11:03:44 -04:00
Kumar Gala
f215e4494c posix: eventfd: depends on polling
Have eventfd Kconfig select POLL is the code utilizes the polling
API.  We get a link error for tests/lib/fdtable/libraries.os.fdtable
when building on arm-clang without this.

Signed-off-by: Kumar Gala <kumar.gala@intel.com>
2023-05-09 23:32:57 +09:00
Keith Packard
9b90f2a351 libc/picolibc: Switch to common malloc implementation
Enable the common malloc implementation when using picolibc. Support
existing Picolibc configurations by respecting existing PICOLIBC_HEAP_SIZE
settings.

When PICOLIBC_HEAP_SIZE is set to a value other than -2, then
*always* set COMMON_LIBC_MALLOC_ARENA_SIZE to that value.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-09 01:29:16 +09:00
Keith Packard
0a44b419ab libc/common: Allocate default malloc arena for non-minimal-libc uses
When using the common malloc implementation on systems not using the
minimal C library, allocate a reasonable default malloc heap according to
the following rules (adopted from the Picolibc heap size defaults):

* When an MMU is available, allocate 16kB.
* When USERSPACE is enabled for a device with an MPU require PoT alignment,
  allocate 1024 bytes
* Otherwise, use all available memory.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-09 01:29:16 +09:00
Keith Packard
420bf1e444 libc/common: Change how legacy malloc setting works
Change the default value of MINIMAL_LIBC_MALLOC_ARENA_SIZE from 0 to an
otherwise invalid value of -2. This allows the common malloc code to
distinguish between an application which explicitly sets the malloc arena
to zero bytes from an application which does not set the variable at all.

When MINIMAL_LIBC_MALLOC_ARENA_SIZE is set to a value other than -2, then
*always* set COMMON_LIBC_MALLOC_ARENA_SIZE to that value. Emit a compile
warning to let the developer know that their application is using a
deprecated Kconfig variable.

This means that MINIMAL_LIBC_MALLOC_ARENA_SIZE now always overrides
COMMON_LIBC_MALLOC_ARENA_SIZE, but an application setting both will already
be getting a configuration warning for setting the DEPRECATED value
anyways, which should serve as an adequate indication to the user.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-09 01:29:16 +09:00
Keith Packard
32a437cbc8 libc/common: Handle MMU/MPU/USERSPACE and alignment requirements in malloc
When CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE is set to -1, make the malloc
arena use all remaining RAM. When an MMU exists, allocate the arena at
startup using k_mem_map. Otherwise, compute the available memory
automatically and use that.

When an MPU is being used to manage the malloc arena, make sure the heap
respects any MPU alignment requirements. Otherwise, align the heap to
sizeof(double).

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-09 01:29:16 +09:00
Kumar Gala
161e4a05a2 libc: Move abort into common
Move the abort implementation into common so its shared among the
libc.  As part of this start using the common abort on newlib.

Signed-off-by: Kumar Gala <kumar.gala@intel.com>
2023-05-08 09:59:27 +02:00
Kumar Gala
ca4dc50ec1 libc: Move strnlen into common
Move the strnlen implementation into common so its available to any
libc that may not implement strnlen.

Signed-off-by: Kumar Gala <kumar.gala@intel.com>
2023-05-08 09:59:27 +02:00
Kumar Gala
e268f8eb4f lib: cpp: Introduce FULL_LIBCPP_SUPPORTED similar to C version
Introduce Kconfig symbol FULL_LIBCPP_SUPPORTED that is similar to the
C version FULL_LIBC_SUPPORTED.  This way we can utilize the same
pattern in tests and samples to filter for when a full libc++ is
needed.

Signed-off-by: Kumar Gala <kumar.gala@intel.com>
2023-05-03 10:01:05 +02:00
Chris Friedt
b9828a74ab posix: sched: add support for SCHED_OTHER
The `SCHED_OTHER` scheduling priority is mandatory as part of
POSIX. It must be numerically distinct from `SCHED_FIFO`,
`SCHED_RR`, and `SCHED_SPORADIC`, but is implementation-
defined and may behave identically to `SCHED_FIFO` or
`SCHED_RR`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-04-29 13:14:40 -07:00
Chris Friedt
2b2cbf8107 posix: sched: ensure min and max priority are schedulable
Previously, there was an off-by-one error for SCHED_RR.

Fixes #56729

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-04-29 13:14:40 -07:00
Keith Packard
e0b540d432 lib/libc: Mark picolibc as providing full C library support
Select FULL_LIBC_SUPPORTED when picolibc is available.

Add picolibc as a secondary default C library when REQUIRES_FULL_LIBC is
selected. This is necessary as tests gated on FULL_LIBC_SUPPORTED need to
be sure that a full C library will be selected -- if only picolibc is
available, those tests will need to select that.

This should permit use of a picolibc-only crosstool-ng toolchain in
testing.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-04-27 20:18:11 +09:00
Keith Packard
214a3c5fa8 lib/libc: Add FULL_LIBC_SUPPORTED helper Kconfig symbol
This symbol is selected when the target has any full libc available. This
allows tests to filter on this condition. It doesn't depend on whether the
application actually selects that library, only whether requesting a full C
library would work.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-04-27 20:18:11 +09:00
Keith Packard
3ffde85895 lib/libc: newlib use depends upon newlib being supported
Before allowing newlib to be selected as the C library, ensure that it is
available for the target.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-04-27 20:18:11 +09:00
Keith Packard
45733aa925 lib/libc: Add NEWLIB_LIBC_SUPPORTED
This reflects whether newlib is available in the environment. This
symbol should be used in place of TOOLCHAIN_HAS_NEWLIB.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-04-27 20:18:11 +09:00
Keith Packard
02262d7367 lib/libc: Replace SUPPORT_MINIMAL_LIBC with MINIMAL_LIBC_SUPPORTED
Clean up libc-related symbols to use a common pattern.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-04-27 20:18:11 +09:00
Keith Packard
2d1efd5b41 lib/libc: Allow REQUIRES_FULL_LIBC to be selected by applications
Add a prompt to the Kconfig symbol so that applications can select this in
their configuration to guide C library selection away from the minimal C
library.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-04-27 20:18:11 +09:00
Keith Packard
6a698f482a lib/libc: Move helper symbols above C library menu
Restructure the file so that the only elements within 'menu "C Library"'
are the library choices.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-04-27 20:18:11 +09:00
Keith Packard
a9c62caab7 lib/libc: Move malloc from minimal to common library
This will (eventually) permit use of a common malloc implementation with
other C libraries, reducing the amount of Zephyr-specific code required
to support each C library.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-04-27 20:17:24 +09:00
Keith Packard
52e57ef17f lib/libc: Allow common library to be empty
This allows the C library common library to be empty if no pieces are
needed, eliminating cmake warnings.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-04-27 20:17:24 +09:00
Keith Packard
dc3f1d7d62 lib/cpp: Allow REQUIRES_FULL_LIBCPP to be selected by applications
Add a prompt to the Kconfig symbol so that applications can select this in
to guide C++ standard library selection towards configurations which
provide a complete implementation.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-04-25 06:20:49 -04:00
Kumar Gala
48cd295a7f armstdc: add limits.h header
Add limits.h header that defines PATH_MAX as this is not defined
by the toolchain headers.

The value of 256 for PATH_MAX is based on how it is set in
lib/libc/minimal/include/limits.h.

Signed-off-by: Kumar Gala <kumar.gala@intel.com>
2023-04-25 12:18:55 +02:00
Keith Packard
25b2db74e0 libc/minimal: Add -fno-builtin option
This ensures that the compiler won't end up recognizing patterns in code
that map to known built-in functions, like noticing that the memcpy
implementation looks a lot like a memory copy operation that could be
replaced by a call to ... memcpy.

When built with -ffreestanding, this has no effect as that option always
enables -fno-builtin.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-04-25 06:18:26 -04:00
Keith Packard
bc2075e31b libc/minimal: Use CODE_UNREACHABLE in abort()
This allows this function to build without -ffreestanding for testing.

Signed-off-by: Keith Packard <keithp@keithp.com>

fixup
2023-04-25 06:18:26 -04:00
Keith Packard
27b7ace9fd libc/minimal: Create "real" functions for putc and putchar
When gcc is building without -fno-builtin, it will optimize calls like
printf("\n") into a call to putchar('\n'), but it won't use a static inline
in that case, instead insisting on a real function.

To make this a bit easier, adopt the usual C library practice of making
putc and putchar macros instead of static inline functions. There's no loss
of typechecking as the parameters are directly passed to underlying
functions with the same parameter types.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-04-25 06:18:26 -04:00
Andrei Emeltchenko
77e5426c5f lib: bitarray: Remove redundant code
Remove statement probably left after rebase. ret should be 0 or
error codes, described in docs, and it is rewritten 4 lines below.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2023-04-21 09:39:49 +02:00
Carlo Caione
0bd95509f6 open-amp: Fix compilation with cache enabled
In d540cf8877 I tried to optionally enable the cache management
functions in Open-AMP introducing a new CONFIG_OPENAMP_WITH_DCACHE
symbol.

This is not working. Introduce a proper fix to have this actually
working correctly as intended.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2023-04-18 12:12:56 +02:00
Keith Packard
b5caccb2ba lib/picolibc: Use zephyr_libc_link_libraries to find toolchain lib
When using the toolchain C library, that must be added to the link command
after all other libraries and modules in the system to resolve undefined
symbols.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-04-17 11:13:17 +02:00
Keith Packard
6c5d806bcb toolchain: Add COMPILER_FREESTANDING to control use of -ffreestanding
Add an explicit compiler configuration, COMPILER_FREESTANDING, which
controls whether the compiler should operate in freestanding or hosted mode
(according to the C and C++ language specifications.

This depends on having a C library which conforms with the language
specification, and the minimal C library does not. Have the minimal C
library select COMPILER_FREESTANDING to continue using freestanding mode
with that library.

For other C libraries, leave this disabled by default while allowing users
to enable it if they want to go back to the previous configuration.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-04-14 21:36:31 +09:00
Gerard Marull-Paretas
667eeb11fb shell: fix MISRA 5.7 violations on struct shell
MISRA Rule 5.7 requires uniqueness of tag identifiers. Shell is
frequently problematic because many code uses `const struct shell
*shell`. This causes CI noise every time one of these shell files is
edited, so let's update all of them with `const struct shell *sh`
instead.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-04-14 12:21:08 +02:00
Gerard Marull-Paretas
a5fd0d184a init: remove the need for a dummy device pointer in SYS_INIT functions
The init infrastructure, found in `init.h`, is currently used by:

- `SYS_INIT`: to call functions before `main`
- `DEVICE_*`: to initialize devices

They are all sorted according to an initialization level + a priority.
`SYS_INIT` calls are really orthogonal to devices, however, the required
function signature requires a `const struct device *dev` as a first
argument. The only reason for that is because the same init machinery is
used by devices, so we have something like:

```c
struct init_entry {
	int (*init)(const struct device *dev);
	/* only set by DEVICE_*, otherwise NULL */
	const struct device *dev;
}
```

As a result, we end up with such weird/ugly pattern:

```c
static int my_init(const struct device *dev)
{
	/* always NULL! add ARG_UNUSED to avoid compiler warning */
	ARG_UNUSED(dev);
	...
}
```

This is really a result of poor internals isolation. This patch proposes
a to make init entries more flexible so that they can accept sytem
initialization calls like this:

```c
static int my_init(void)
{
	...
}
```

This is achieved using a union:

```c
union init_function {
	/* for SYS_INIT, used when init_entry.dev == NULL */
	int (*sys)(void);
	/* for DEVICE*, used when init_entry.dev != NULL */
	int (*dev)(const struct device *dev);
};

struct init_entry {
	/* stores init function (either for SYS_INIT or DEVICE*)
	union init_function init_fn;
	/* stores device pointer for DEVICE*, NULL for SYS_INIT. Allows
	 * to know which union entry to call.
	 */
	const struct device *dev;
}
```

This solution **does not increase ROM usage**, and allows to offer clean
public APIs for both SYS_INIT and DEVICE*. Note that however, init
machinery keeps a coupling with devices.

**NOTE**: This is a breaking change! All `SYS_INIT` functions will need
to be converted to the new signature. See the script offered in the
following commit.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

init: convert SYS_INIT functions to the new signature

Conversion scripted using scripts/utils/migrate_sys_init.py.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

manifest: update projects for SYS_INIT changes

Update modules with updated SYS_INIT calls:

- hal_ti
- lvgl
- sof
- TraceRecorderSource

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

tests: devicetree: devices: adjust test

Adjust test according to the recently introduced SYS_INIT
infrastructure.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

tests: kernel: threads: adjust SYS_INIT call

Adjust to the new signature: int (*init_fn)(void);

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-04-12 14:28:07 +00:00
Gerard Marull-Paretas
0ebe14beb4 sys: util: migrate all files to DIV_ROUND_UP
ceiling_fraction is deprecated, use DIV_ROUND_UP.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-04-11 12:00:37 +02:00
Kumar Gala
af79019146 armstdc: add string.h & strings.h header
Add string.h header that declerates prototype for strnlen()
as this is not provided by the toolchain headers.

Add strings.h to allow anything that expects strncasecmp() to
be defined there to build correctly.  The arm toolchain actually
defines strncasecmp() in <string.h> so we just have <strings.h>
include <string.h>.

Signed-off-by: Kumar Gala <kumar.gala@intel.com>
2023-04-10 16:32:10 -05:00
Kumar Gala
20b21091cc armstdc: Fixup headers as needed by posix
* Move 'struct _timespec' into sys/_timespec.h as expected by
  <posix/time.h>.

* Introduce 'struct timeval' in sys/_timeval.h as expected by
  <posix/time.h>.

* Add mode_t to <sys/types.h> as expected by <posix/stat.h>

Signed-off-by: Kumar Gala <kumar.gala@intel.com>
2023-04-10 16:32:10 -05:00
Kumar Gala
82538a2d28 armclang: add empty sys/cdefs.h
Various headers include <sys/cdefs.h> like zephyr/posix/sys/stat.h.
When building with the arm-clang toolchain we utilize armstdc
so we need an empty cdefs.h similar to what minimal libc has so
things will build.

Signed-off-by: Kumar Gala <kumar.gala@intel.com>
2023-04-10 16:32:10 -05:00
Kumar Gala
0e43b8861b libc: fix armclang compiler warnings with is*() functions
We get compile warnings of the form:

error: converting the result of
'<<' to a boolean; did you mean
'((__aeabi_ctype_table_ + 1)[(byte)] << 28) != 0'?
 [-Werror,-Wint-in-bool-context]
                if (!isprint(byte)) {
                     ^

Since isprint (and the other is* functions) return an int, change check
to an explicit test against the return value.

Signed-off-by: Kumar Gala <kumar.gala@intel.com>
2023-04-04 13:47:34 +02:00
Kumar Gala
62ffafcb3d lib: os: fix armclang compiler warnings with is*() functions
We get compile warnings of the form:

error: converting the result of
'<<' to a boolean; did you mean
'((__aeabi_ctype_table_ + 1)[(byte)] << 28) != 0'?
 [-Werror,-Wint-in-bool-context]
                if (!isprint(byte)) {
                     ^

Since isprint (and the other is* functions) return an int, change check
to an explicit test against the return value.

Signed-off-by: Kumar Gala <kumar.gala@intel.com>
2023-03-31 09:19:02 +02:00
Kumar Gala
672aeace88 libc: share time() between minimal libc and armclang libc
Introduce a place to share implementations of libc functions that
are needed by different libc versions.  Place time() in this common
location so it can be shared when building for either minimal libc or
armclang libc.

Signed-off-by: Kumar Gala <kumar.gala@intel.com>
2023-03-27 09:49:40 +02:00
Chris Friedt
5e9d3c7522 posix: fnmatch: correct (many) compliance issues
* add opening and closing braces around several blocks
* convert dos line endings to unix
* move assignments out of conditionals
* ensure adequate spacing after control blocks

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-03-03 17:40:14 +09:00
Chris Friedt
f923441146 lib: posix: move fnmatch from lib/util to lib/posix
The `fnmatch()` function is specified by POSIX and should be
a part of `lib/posix`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-03-03 17:40:14 +09:00
Joshua Lilly
69966dd452 lib: hashmap: Move hash table files lib/hashmap
Moves the hashmap related files into their own directory

Signed-off-by: Joshua Lilly <jgl@fb.com>
2023-03-02 12:03:07 -05:00
Andy Ross
7df2ca6e86 minimal libcpp: Un-11-ify cstddef
Don't expose C++11 APIs unless we're building for that platform

Signed-off-by: Andy Ross <andyross@google.com>

squashme cdecl
2023-03-01 19:42:32 -05:00
Chris Friedt
0bda7b30df lib: os: add hashmap support
* Add a flexible Hashmap API
* Add a Separate-Chaining Hashmap Implementation
* Add a Open-Addressing Linear Probe Hashmap Implementation
* Add a C-Wrapper for `std::unordered_map` for benchmarking

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-02-22 19:14:05 +01:00
Chris Friedt
b75df60f0b lib: os: add hash function specification and implementation
Hash functions are necessary for Hashmaps (a.k.a. Hash Tables).

* Create a flexible hash function interface
* Implementation 1. Murmur3
* Implementation 2. djb2
* Kconfig option for system-wide 32-bit hash

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-02-22 19:14:05 +01:00
Zachary J. Fields
8dbe58907d os: bitarray.c: Address -Wextra warnings
Changed incrementing `for` loop counters to `size_t` from `int` to
eliminate warning, "warning: comparison of integer expressions of
different signedness: 'uint32_t' {aka 'unsigned int'} and 'int'
[-Wsign-compare]"

Signed-off-by: Zachary J. Fields <zachary_fields@yahoo.com>
2023-02-21 15:07:20 +01:00
Zachary J. Fields
02478e1b16 os: cbprintf: Address -Wextra warnings
Changed incrementing `for` loop counters to `size_t` from `int` to
eliminate warning, "warning: comparison of integer expressions of
different signedness: 'uint32_t' {aka 'unsigned int'} and 'int'
[-Wsign-compare]"

Signed-off-by: Zachary J. Fields <zachary_fields@yahoo.com>
2023-02-20 09:50:12 +01:00
Evgeniy Paltsev
e0de642d0a ARC: qemu: disable test where we trigger ARC QEMU bug #54720
Disable tests/kernel/mem_protect/syscalls for qemu_arc_em where
we trigger ARC QEMU bug which cause illegal instruction exception
on perfectly valid ARC code.

Signed-off-by: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
Signed-off-by: Evgeniy Paltsev <PaltsevEvgeniy@gmail.com>
2023-02-17 08:50:37 +09:00
Andy Ross
668bb3cb22 lib/os/heap: Compiler bug workaround on ARC
This is a workaround for a compiler bug on (at least) GCC 12.1.0 in
Zephyr SDK 0.15.1.  The optimizer generates this function with a last
instruction that is an unconditional branch (a tail call into the
chunk_set() handling).  But that means that the NEXT instruction gets
decoded as part of the branch delay slot, but that instruction isn't
part of this function!  Some instructions aren't legal in branch delay
slots.  One of those is ENTER_S, which is a very common entry
instruction for whatever function the linker places after us.  It
seems like the compiler doesn't understand this problem.  Stuff a NOP
in to guarantee the code is legal.

Comment above is duplicated in the code.  The workaround is
straightforward once the issue is understood, but the path to get here
was hilariously weird.

Fixes #54720

Signed-off-by: Andy Ross <andyross@google.com>
2023-02-10 14:32:26 -05:00
Anas Nashif
0e0f878ddc picolibc: picolibc support still not complete with many toolchains
Clang support still work in progress in zephyr. So for now enable only
on gcc based toolchains.

We should enable this back for more toolchains once we have full clang
support and are able to verify.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-02-07 18:16:10 +01:00
Keith Packard
9a0aebc5fd lib/libc: Enable picolibc on qemu_x86_tiny
With all of the test suite issues on this board fixed, qemu_x86_tiny should
now be usable with picolibc.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-01-30 23:46:55 +00:00
Keith Packard
67ca25467b libc: Add remaining picolibc-supported targets
Picolibc now supports all of the Zephyr SDK target architectures for C.

qemu_x86_tiny needs fixes to get the libc partition linked
into the right spot. See issue #54148

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-01-27 06:27:43 -05:00
Björn Stenberg
0321ac8dbf json: Increase max number of descriptor elements from 30 to 62
The limiting factor is the output bitmask that says which elements have
been filled in by the parser. This patch changes the bitmask type from int
to int64_t.

Signed-off-by: Björn Stenberg <bjorn@haxx.se>
2023-01-26 09:51:54 +00:00
Henrik Brix Andersen
277f6469f1 lib: os: reboot: include zephyr/cache.h
Include <zephyr/cache.h> in order to use the Zephyr cache APIs.

Fixes: b94ab6e9f1

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
2023-01-24 14:35:49 +00:00
Chris Friedt
ed11614a87 posix: mqueue: do not typedef mq_attr
The specification actually requires this to be a plain
`struct mq_attr`, without the typedef.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-23 09:57:31 -08:00
Chris Friedt
3aff1ff0c2 posix: fs: avoid adding open() alias with newlib and picolibc
Newlib and PicoLibc both already alias `open` to `_open`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-23 09:57:31 -08:00
Chris Friedt
e431cfbbe5 posix: eventfd: select eventfd by default with CONFIG_POSIX_API
Although the eventfd API is not (yet) a part of POSIX,
it's pretty well ubiquitous on POSIX systems now.

Enable it by default when `CONFIG_POSIX_API=y`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-23 09:57:31 -08:00
Chris Friedt
bf5d47a5ce posix: getopt: select getopt by default with CONFIG_POSIX_API
The `getopt()` function is part of POSIX and should be
available when applications choose to enable general POSIX
API support.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-23 09:57:31 -08:00
Chris Friedt
e93f3c9b6d posix: rename posix_sched.h to sched.h
Rename the `posix_sched.h` to conform to the spec.

IEEE Std 1003.1

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-23 09:57:31 -08:00
Chris Friedt
43110ddb71 lib: posix: standard include paths with CONFIG_POSIX_API
With the `<zephyr/posix/...> prefix, it became
exponentially more difficult to integrate 3rd-party
libraries that depend on the POSIX API.

Standard POSIX headers should be available in standard
include paths - and that should most certainly the case
when `CONFIG_POSIX_API=y`.

With this change:

* When `CONFIG_POSIX_API=y`
  - applications have explicitly chosen to use
    POSIX APIs.
  - all standard POSIX includes are in the default
    include path.
* When `CONFIG_POSIX_API=n`
  - applications *may* include POSIX headers
    explicitly with the namespaced prefix
  - e.g. `#include <zephyr/posix/unistd.h>`
  - individual Kconfig options can be used to
    enable POSIX features selectively, such as
    `getopt` or `eventfd`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-23 09:57:31 -08:00
Joakim Andersson
22a8af5e63 drivers: timers: Refactor sys_clock_disable not available
Refactor sys_clock_disable not implemented behavior.
This follows the coding guidelines
Rule A.1: Conditional Compilation:
Do not conditionally compile function declarations in header files.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
2023-01-20 20:02:20 +01:00
Keith Packard
13dfbaebd1 libc/picolibc: Clean up Picolibc Kconfig for C++
libstdc++ is supported with Picolibc only when the toolchain version of
Picolibc is use -- libstdc++ must be built using a specific Picolibc build
and libstdc++ is included with the toolchain.

Ideally, we'd allow the use of the Picolibc module whenever we weren't
using the GNU libstdc++, including when using the minimal libc++. However,
the obvious dependency settings create a loop:

config PICOLIBC
    depends on PICOLIBC_SUPPORTED

config PICOLIBC_SUPPORTED
    depends on !(GLIBCXX_LIBCPP && "$(ZEPHYR_TOOCHAIN_VARIANT" = "zephyr")

config GLIBCXX_LIBCPP
    depends on NEWLIB_LIBC || PICOLIBC

To break this loop, we replace GLIBCXX_LIBCPP in the second block with
CPP:

config PICOLIBC_SUPPORTED
    depends on !(CPP && "$(ZEPHYR_TOOCHAIN_VARIANT" = "zephyr")

This means that picolibc cannot be used with any C++ apps when using the
Zephyr SDK, even when not using the GNU libstdc++. However, Zephyr SDK 0.16
will come with an additional Kconfig file that includes:

config PICOLIBC_SUPPORTED
    def_bool y
    depends on "$(ZEPHYR_TOOLCHAIN_VARIANT)" = "zephyr"

This will override the Kconfig bits included in Zephyr and allow use of the
Picolibc module with C++ code, including using the minimal libc++ bits.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-01-20 09:03:25 +01:00
Armin Brauns
b94ab6e9f1 lib: os: disable caches before reboot
This ensures that all pending writes are committed. This is relevant for
flags in .noinit SRAM that are read back after reset.

Signed-off-by: Armin Brauns <armin.brauns@embedded-solutions.at>
2023-01-18 16:21:25 +01:00
Stephanos Ioannidis
669a0f5bec lib: cpp: Rework C++ standard library configurations
This commit reworks the C++ standard library configurations such that:

* the separation between the Zephyr minimal C++ library and the fully
  featured C++ standard libraries, such as GNU libstdc++, is clear.
  This is done by deprecating the Kconfig `CONFIG_LIB_CPLUSPLUS`
  symbol, which implies that the minimal C++ library is selected when
  set to `n`, and introducing the `CONFIG_MINIMAL_LIBCPP` symbol.

* the type of the selected C++ standard library is clear. This is done
  by introducing a Kconfig choice, `LIBCPP_IMPLEMENTATION`, for the C++
  standard library type and providing the choice symbols for each
  library type supported, such as `CONFIG_MINIMAL_LIBCPP` and
  `CONFIG_GLIBCXX_LIBCPP`.

Signed-off-by: Stephanos Ioannidis <stephanos.ioannidis@nordicsemi.no>
2023-01-13 17:42:55 -05:00
Stephanos Ioannidis
45ba8d5787 lib: cpp: Rename CONFIG_RTTI to CONFIG_CPP_RTTI
The Kconfig `CONFIG_RTTI` symbol enables the C++ Run-Time Type
Information (RTTI) support and is a feature of the standard C++
library; therefore, it should be prefixed with `CPP_` as with the rest
of the C++-specific symbols.

This commit renames the Kconfig `CONFIG_RTTI` symbol to
`CONFIG_CPP_RTTI` in order to better align with the other existing C++
Kconfig symbols, and deprecates the old `CONFIG_RTTI` symbol.

Signed-off-by: Stephanos Ioannidis <stephanos.ioannidis@nordicsemi.no>
2023-01-13 17:42:55 -05:00
Stephanos Ioannidis
26ede92f84 lib: cpp: Rename CONFIG_EXCEPTIONS to CONFIG_CPP_EXCEPTIONS
`CONFIG_EXCEPTIONS` is ambiguous in that the word "exception" may be
used to refer to many different types of exceptions, from the hardware
generated faults and interrupts (aka. exceptions) to the C++
exceptions.

This commit renames the Kconfig `CONFIG_EXCEPTIONS` symbol to
`CONFIG_CPP_EXCEPTIONS` in order to clarify that the symbol refers to
the C++ exceptions and eliminate any ambiguity, and deprecates the old
`CONFIG_EXCEPTIONS` symbol.

Signed-off-by: Stephanos Ioannidis <stephanos.ioannidis@nordicsemi.no>
2023-01-13 17:42:55 -05:00
Stephanos Ioannidis
4a64bfe351 treewide: Use CONFIG_CPP instead of CONFIG_CPLUSPLUS
This commit updates all in-tree code to use `CONFIG_CPP` instead of
`CONFIG_CPLUSPLUS`, which is now deprecated.

Signed-off-by: Stephanos Ioannidis <stephanos.ioannidis@nordicsemi.no>
2023-01-13 17:42:55 -05:00
Stephanos Ioannidis
05aea1ce58 lib: cpp: Rename CONFIG_CPLUSPLUS to CONFIG_CPP
There is no reason for the top-level C++ Kconfig symbol to be called
`CPLUSPLUS` when the convention used throughout the Zephyr codebase is
to use "cpp" to refer to C++ and the rest of the C++-related Kconfig
symbols are prefixed with `CPP_`.

This commit renames the Kconfig `CPLUSPLUS` symbol to `CPP` in order to
better align with the de-facto standard naming convention used throughout
the Zephyr codebase, and deprecates the old `CPLUSPLUS` symbol.

Signed-off-by: Stephanos Ioannidis <stephanos.ioannidis@nordicsemi.no>
2023-01-13 17:42:55 -05:00
Stephanos Ioannidis
96234b15df lib: cpp: Relocate C++ minimal library components to lib/cpp/minimal
This commit relocates the "C++ minimal library" components, that
implement a very limited subset of the standard C++ library, to a
dedicated directory, `lib/cpp/minimal`, in order to provide a clear
separation among the different C++ library components.

After this refactoring, the top `lib/cpp` directory should only contain
the sub-directories for each C++ library (i.e. `abi`, `minimal`). In
the future, a C++ library-specific shim layer implementation may be
added as sub-directories under `lib/cpp` as well (e.g. `libstdc++`
sub-directory containing the shim layer implementation for the GCC
libstdc++ library) -- this is similar to how the libc directories are
structured under `lib/libc`.

Signed-off-by: Stephanos Ioannidis <stephanos.ioannidis@nordicsemi.no>
2023-01-13 17:42:55 -05:00
Stephanos Ioannidis
03879b9e2b lib: cpp: Relocate C++ ABI library components to lib/cpp/abi
This commit relocates the "C++ ABI library" components such as
global constructor/destructor and initialiser handlers to a dedicated
directory, `lib/cpp/abi`, in order to provide a clear separation
between the C++ ABI/runtime library and the standard C++ library
components.

Note that the Zephyr C++ ABI library currently implements the GNU/GCC
C++ ABI, which is the de-facto standard ABI used by many compilers
including Clang -- it may be necessary to sub-divide the `lib/cpp/abi`
into `lib/cpp/abi/gnu` and `lib/cpp/abi/someotherabi` in the future
when adding the support for a C++ compiler that expects an ABI vastly
different from the GNU C++ ABI.

Signed-off-by: Stephanos Ioannidis <stephanos.ioannidis@nordicsemi.no>
2023-01-13 17:42:55 -05:00
Stephanos Ioannidis
feaab27c1b lib: cpp: Relocate subsys/cpp to lib/cpp
This commit moves the files under `subsys/cpp` directory to the
`lib/cpp` directory because the C++ ABI runtime library and the
standard C++ library components are not a "subsystem" (aka. API) in
conventional sense and is better described as a "library."

Classifying the C++ ABI runtime library and the standard C++ library as
"libraries" instead of "subsystems" also better aligns with how the
existing C standard library implementation (`lib/libc`) is handled.

Signed-off-by: Stephanos Ioannidis <stephanos.ioannidis@nordicsemi.no>
2023-01-13 17:42:55 -05:00
HaiLong Yang
1c2c254f7c lib: os: Fix note on fdtable.c
After fcntl.h moved to posix, there have a compiler note
on fdtable.c. As suggested in fcntl.h, instead with
zephyr/posix/fcntl.h.

Signed-off-by: HaiLong Yang <hailong.yang@brainco.cn>
2023-01-11 10:54:42 +01:00
Chris Friedt
addbec9591 libc: minimal: stdio.h: define SEEK_SET, SEEK_CUR, SEEK_END
The `SEEK_SET`, `SEEK_CUR`, and `SEEK_END` constants are defined
in `<stdio.h>`, not in `<sys/stat.h>`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-10 09:02:21 +09:00
Chris Friedt
0697c7b3fe posix: headers: harmonize remaining types with newlib
The remaining types that needed to be harmonized between
Newlib and Zephyr's POSIX definitions are:

* `struct sched_param`
  - don't re-define if using minimal libc
* `pthread_attr_t`
  - convert to `struct pthread_attr`
  - define type if using minimal libc
  - assert acceptible object size
* `pthread_mutexattr_t`
  - convert to `struct pthread_mutexattr`
  - define type if using minimal libc
  - assert acceptible object size
* `pthred_condattr_t`
  - convert to `struct pthread_condattr`
  - define type if using minimal libc
  - assert acceptible object size
* `pthread_once_t`
  - adopt newlib definition
  - define type if using minimal libc
Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-10 09:02:21 +09:00
Chris Friedt
ecb82784b7 posix: newlib compatible PTHREAD_CREATE_DETACHED and JOINABLE
Define `PTHREAD_CREATE_DETACHED` and
`PTHREAD_CREATE_JOINABLE` to be compatible with the Newlib
definitions.

This is a temporary workaround for #51211 until Newlib
headers are pulled in.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-10 09:02:21 +09:00
Chris Friedt
8659e2f69e libc: minimal: include: move fcntl.h to posix
The `fcntl.h` header has never been a part of ISO C so move it to
`include/zephyr/posix`.

To ensure a smooth migration, a header was left in
`lib/libc/minimal/include` that prints a deprecation warning.

Users should either include `<zephyr/posix/fcntl.h>` or switch to
`CONFIG_POSIX_API=y`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-10 09:02:21 +09:00
Chris Friedt
6f4e96bc24 libc: minimal: include: move sys/stat.h to posix
The `sys/stat.h` header has never been a part of ISO C so move it
to `zephyr/include/posix/sys/`.

To ensure a smooth migration, leave a stub header in
`lib/libc/minimal/include/sys/` that prints a deprecation warning
suggesting developers either include `<zephyr/posix/sys/stat.h>`
or use `CONFIG_POSIX_API=y`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-10 09:02:21 +09:00
Chris Friedt
44b72bf801 posix: clock: fix seconds calculation
The previous method used to calculate seconds in `clock_gettime()`
seemed to have an inaccuracy that grew with time causing the
seconds to be off by an order of magnitude when ticks would roll
over.

This change fixes the method used to calculate seconds.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-04 21:12:58 +01:00
Krzysztof Chruscinski
accaebb708 lib: os: mpsc_pbuf: Fix concurrency issues
Fixed issues which were leading to failures when producing
and consuming is preempted at various stages.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-12-29 10:33:29 +01:00
Krzysztof Chruscinski
235ee63233 lib: os: mpsc_pbuf: Use flag for buffer full indication
Use flag instead of word in the buffer. Using this method allows
to dedicate full buffer capacity for data.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-12-29 10:33:29 +01:00
Krzysztof Chruscinski
e46efdcd94 lib: os: mpsc_pbuf: Improve debugging
Improved debugging messages.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-12-29 10:33:29 +01:00
Nicholas Lowell
51f83185b0 posix: Kconfig for timer_create wait time
should be able to configure the time spent waiting
for available resources when calling timer_create()
to not cause a hiccup in applications that require
faster response times than the original hard-coded 100 ms.

Signed-off-by: Nicholas Lowell <nlowell@lexmark.com>
2022-12-27 15:34:13 +01:00
Carlo Caione
d540cf8877 openamp: Add new Kconfig option to enable dcache
Currently OpenAMP is unconditionally compiled with the options to use
the vrings cache operations disabled.

Add a new CONFIG_OPENAMP_WITH_DCACHE Kconfig option to enable the
support for d-cache operations in OpenAMP when needed.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2022-12-12 18:41:01 +01:00
Stephanos Ioannidis
997f5edf1a lib: libc: picolibc: Clean up Kconfig configurations
This commit updates the Picolibc configurations to remove any
unnecessary defaults and dependencies and conform to the de-facto
standard convention across the Zephyr repository.

Signed-off-by: Stephanos Ioannidis <stephanos.ioannidis@nordicsemi.no>
2022-12-08 15:09:55 -05:00
Stephanos Ioannidis
c9c40d9465 lib: libc: Move newlib libc configs to lib/libc/newlib/Kconfig
This commit relocates the newlib libc configurations under the top-
level libc Kconfig to a new Kconfig under `lib/libc/newlib` for
improved organisation of the libc configurations.

Signed-off-by: Stephanos Ioannidis <stephanos.ioannidis@nordicsemi.no>
2022-12-08 15:09:55 -05:00
Stephanos Ioannidis
5efc0d51c8 lib: libc: Move minimal libc configs to lib/libc/minimal/Kconfig
This commit relocates the minimal libc configurations under the top-
level libc Kconfig to a new Kconfig under `lib/libc/minimal` for
improved organisation of the libc configurations.

Signed-off-by: Stephanos Ioannidis <stephanos.ioannidis@nordicsemi.no>
2022-12-08 15:09:55 -05:00
Stephanos Ioannidis
c3db8dc9e7 lib: libc: Clean up CMake script to use add_subdirectory_ifdef
This commit cleans up the top `CMakeLists.txt` for the libc directory
to use `add_subdirectory_ifdef` in order to make the code more
readable.

Signed-off-by: Stephanos Ioannidis <stephanos.ioannidis@nordicsemi.no>
2022-12-08 15:09:55 -05:00
Radoslaw Koppel
5a55d53242 spsc_pbuf: Fix cache wb in spsc_pbuf_free
This commit properly writes back cached read pointer in spsc buffer
when freeing the memory.

Signed-off-by: Radoslaw Koppel <radoslaw.koppel@nordicsemi.no>
2022-12-07 10:21:33 +00:00
Stephanos Ioannidis
82a902e5ea lib: newlib: Define _ANSI_SOURCE
This commit updates the Newlib integration to define `_ANSI_SOURCE`
in order to prevent Newlib from defining POSIX primitives in its
headers when GNU dialect is used (`-std=gnu*`).

Newlib `features.h` defines `_DEFAULT_SOURCE` when `__STRICT_ANSI__`
is not defined by GCC (i.e. when `-std=gnu*`), which results in the
Newlib headers defining POSIX primitives that are in conflict with the
POSIX primitives defined by Zephyr.

Newlib must not define POSIX primitives unless the feature test macros
such as `_POSIX_SOURCE`, `_GNU_SOURCE` and `_DEFAULT_SOURCE` are
explicitly defined.

Note that `-std=gnu` does not imply `_GNU_SOURCE` or `_DEFAULT_SOURCE`
because it is only supposed to instruct the compiler to use the GNU C
language dialect (i.e. GNU C language extensions).

Refer to the GitHub issue #52739 for more details.

Signed-off-by: Stephanos Ioannidis <stephanos.ioannidis@nordicsemi.no>
2022-12-05 11:02:36 +01:00
Chris Friedt
ff3aaa6ef3 posix: getopt: move declarations to unistd.h
Declarations for `getopt()` should be in `<unistd.h>`
according to the spec. The extended versions `getopt_long()`
and `getopt_long_only()` are declared in `<getopt.h>`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-12-04 14:51:52 +01:00
Chris Friedt
4d906d944b Revert "posix: getopt: move declarations to unistd.h"
This seems to have caused build failures in spite of CI being
green in PR 52653.

This reverts commit fc92eb1b37.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-12-02 14:09:37 +09:00
Chris Friedt
fc92eb1b37 posix: getopt: move declarations to unistd.h
Declarations for `getopt()` should be in `<unistd.h>`
according to the spec. The extended versions `getopt_long()`
and `getopt_long_only()` are declared in `<getopt.h>`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-12-01 15:52:47 -05:00
Carlo Caione
cc427b4bb0 cache: Fix libraries and drivers
Fix the usage to be compliant to the new cache API.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2022-12-01 13:40:56 -05:00
Chris Friedt
2e36d1f3eb lib: os: add CRC shell command for integrity verification
There are several Zephyr shell commands that are used for
transferring data over a possibly unreliable connection such as a
UART into either memory or flash. For example, `flash load` and
`devmem load`.

Make the CRC functions available so that they can be used to
verify the integrity of data transferred over possibly
unreliable connections.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-29 09:32:28 +01:00
Chris Friedt
7b95428fa0 lib: posix: update usleep() to follow the POSIX spec
The original implementation of `usleep()` was not compliant
to the POSIX spec in 3 ways.
- calling thread may not be suspended (because `k_busy_wait()`
  was previously used for short durations)
- if `usecs` > 1000000, previously we did not return -1 or set
  `errno` to `EINVAL`
- if interrupted, previously we did not return -1 or set
  `errno` to `EINTR`

This change addresses those issues to make `usleep()` more
POSIX-compliant.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-24 19:31:33 -05:00
Chris Friedt
dcfcc6454b lib: posix: sleep() should report unslept time in seconds
In the case that `sleep()` is interrupted, the POSIX spec requires
it to return the number of "unslept" seconds (i.e. the number of
seconds requested minus the number of seconds actually slept).

Since `k_sleep()` already returns the amount of "unslept" time
in ms, we can simply use that.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-24 19:31:33 -05:00
Chris Friedt
f1ececc682 posix: key: abstract pthread_key_t as uint32_t
Consistent with the change of `pthread_t`, `pthread_mutex_t`,
and `pthread_cond_t` to `uint32_t`, we can now also abstract
`pthread_key_t` as `uint32_t` and separate the implementation
detail, hidden from POSIX API consumers.

This change introduces `CONFIG_MAX_PTHREAD_KEY_COUNT`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-24 04:19:01 +09:00
Chris Friedt
96b9d7cb6d lib: posix: pthread_key: use spinlock instead of semaphore
None of the operations that `pthread_key_sem` protected were
blocking, so simply make it a spinlock.

Also made the lock static.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-24 04:19:01 +09:00
Chris Friedt
187df899da lib: posix: pthread: rename pthread_key_lock to pthread_once_lock
Since this lock is only used in `pthread_once()` it makes sense
to name it accordingly.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-24 04:19:01 +09:00
Anas Nashif
cffe98d9de crc: Make the build of crc function dependent on a Kconfig
Add CONFIG_CRC for building CRC related routines.
CRC routines are now being built for each application, whether used or
not and are add in the build system unconditionally.

Keep CONFIG_CRC enabled by default for now and until all users have
converted to use the new option.

Partial fix for #50654

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2022-11-23 13:30:00 +01:00
Chris Friedt
3a56d276e9 posix: cond: abstract pthread_cond_t as uint32_t
Consistent with the change of `pthread_t` from
`struct posix_thread` to `uint32_t`, we can now also abstract
`pthread_cond_t` as `uint32_t` and separate `struct posix_cond`
as an implementation detail, hidden from POSIX API consumers.

This change deprecates `PTHREAD_COND_DEFINE()` in favour of the
(standardized) `PTHREAD_COND_INITIALIZER`.

This change introduces `CONFIG_MAX_PTHREAD_COND_COUNT`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-19 14:16:38 +09:00
Chris Friedt
afae448ff2 posix: pthread: take care with pthread cond resources
Previously, `pthread_cond_init()` could not actually fail, and
destroying condition variables was a no-op, and it was missing
in `pthread_exit()`.

However, with the change of `pthread_cond_t` to `uint32_t`, and
since those are embedded inside of `struct posix_thread` for the
time being, the pthread code needs to keep track that it is
relinquishes used condition variables when a thread completes.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-19 14:16:38 +09:00
Chris Friedt
1777a33558 lib: posix: internal: use a more generic INIT mask and inlines
Previously `PTHREAD_MUTEX_MASK_INIT` was used to mark a
`pthread_mutex_t` as initialized.

The same needs to be done for `pthread_cond_t` and likely others.

Rather than copy-pasting that and a number of inlines that
duplicate the same functionality, simply make it more generic.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-19 14:16:38 +09:00
Chris Friedt
b0b4c9c3f1 posix: mutex: abstract pthread_mutex_t as uint32_t
Consistent with the change of `pthread_t` from
`struct posix_thread` to `uint32_t`, we can now also abstract
`pthread_mutex_t` as `uint32_t` and separate `struct posix_mutex`
as an implementation detail, hidden from POSIX API consumers.

This change deprecates `PTHREAD_MUTEX_DEFINE()` in favour of the
(standardized) `PTHREAD_MUTEX_INITIALIZER`.

This change introduces `CONFIG_MAX_PTHREAD_MUTEX_COUNT`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-15 05:56:31 +09:00
Chris Friedt
6042acc1a9 posix: pthread: take care with pthread mutex resources
Previously, `pthread_mutex_init()` could not actually fail, and
destroying mutexes was a no-op, so it was missing in a couple of
places.

However, with the change of `pthread_mutex_t` to `uint32_t`, and
since those are embedded inside of `struct posix_thread` for the
time being, the pthread code needs to keep track that it is
relinquishes used mutex resources when a thread completes.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-15 05:56:31 +09:00
Chris Friedt
39b8b3ac8a posix: pthread_create: use spinlock for pthread_pool_lock
The `pthread_create()` function is not a cancellation point and
iterating over / mutating `posix_thread_pool` is not a blocking
operation, so use a spinlock for the internal `pthread_pool_lock`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-08 16:10:26 -05:00
Chris Friedt
660a7db3f2 posix: pthread: use spinlock instead of mutex for cancel lock
The cancel lock will not block so a mutex is overkill.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-08 13:30:02 -05:00
Chris Friedt
4bae4f426f include: posix: move pthread impl detail to posix_internal.h
The `struct pthread` and `enum pthread_state` are actually
implementation details specific to Zephyr.

Let's limit the scope where that level of detail is visible.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-07 09:09:11 -05:00
Chris Friedt
2812f61e4e posix: pthread_once: use k_mutex for pthread_key_lock
In the interest of reducing any layering concerns,
avoid using POSIX locking primitives where necessary.

Note: it is not safe to use a spinlock here, as the
callback function to `pthread_once()` may itself be
a cancellation point.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-07 09:04:01 -05:00
Chris Friedt
ad5c3a1ae3 posix: pthread: remove duplicate assignment
The `thread` pointer variable is already assigned within the loop
above.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-07 10:16:09 +00:00
Keith Packard
d8d81c4a09 libc/picolibc: Remove unused POSIX-ish hooks
While reviewing the dependency between Picolibc POSIX APIs and Zephyr, I
found that the picolibc libc-hooks code copied a bunch of functions from
the newlib version which weren't needed. This required replacing a few
calls to the (now removed) '_write' hook with printk instead.

Signed-off-by: Keith Packard <keithp@keithp.com>
2022-11-02 06:49:32 -04:00
Chris Friedt
ce054404a2 posix: use uint32_t instead of void* for pthread_t
Several other widely-used pthread implementations
abstract `pthread_t` as `uint32_t`. The benefit
there is that we avoid passing around a pointer to
an internal structure (implementation detail).

Additionally, this removes the alias from `k_tid_t`
to `pthread_t` inside of `struct pthread_mutex`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-02 15:39:44 +09:00
Kumar Gala
9be0914885 lib: os: p4wq: Convert CONFIG_MP_NUM_CPUS handling
Move runtime checks to use arch_num_cpus().  This is to allow
runtime determination of the number of CPUs in the future.

Signed-off-by: Kumar Gala <kumar.gala@intel.com>
2022-10-31 17:08:34 +01:00
Chris Friedt
f5cfeae152 posix: pthread: remove duplicate assignment in pthread_exit
The `self->retval` field was assigned twice.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-10-31 16:58:47 +01:00
Keith Packard
25c378461e libc/picolibc: Set __LINUX_ERRNO_EXTENSIONS__ for picolibc
This makes sure extra errno values from Linux that Zephyr uses are
available.

Signed-off-by: Keith Packard <keithp@keithp.com>
2022-10-31 17:17:42 +09:00
Daniel Leung
cf23b312c3 lib: picolib: put stdio variables into libc partition
This adds the necessary modifier to the stdin/stdout/stderr
variables in picolib, and putting into the z_libc_partition.
This allows userspace applications to utilize these variables
for console I/O.

Fixes #51343

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2022-10-20 09:59:42 +02:00
Daniel Leung
6ee7294ac2 lib: picolib: move static to be the first modifier
Compliance check complains about static not being the first
modifier. So move them so there are no more complains
in the future.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2022-10-20 09:59:42 +02:00
Stephanos Ioannidis
28b22b276a lib: libc: newlib: Make newlib nano variant optional
The newlib nano variant is currently enabled by default when
`CONFIG_NEWLIB_LIBC=y` and the selected toolchain-architecture
combination includes the newlib nano variant support, even if
`CONFIG_NEWLIB_LIBC_NANO` is not selected by the user.

When `CONFIG_NEWLIB_LIBC=y`, this results in the newlib nano variant
being selected for some architectures (e.g. ARC, ARM and RISC-V), while
the full variant is selected for the rest of the architectures.

The above behaviour is problematic because there exist functional
differences between the newlib full and nano variants (e.g. C99 format
modifiers such as `hh`, `ll`, `z`, `j` and `t` are not available in the
newlib nano variant), and this effectively leads to different level of
C standard support across different architectures when
`CONFIG_NEWLIB_LIBC=y`.

This commit fixes this problem by making the `CONFIG_NEWLIB_LIBC_NANO`
not `default y` and requiring its user to explicitly set this symbol to
`y` when they want to use the newlib nano variant.

Signed-off-by: Stephanos Ioannidis <stephanos.ioannidis@nordicsemi.no>
2022-10-19 16:02:51 +02:00
Anas Nashif
a5bd666f4e lib: notify: build sys-notify conditionally.
Add a new Kconfig and build this code conditionally, so we do not end up
with this file being built for each zephyr app.

Partial fix for #50654

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2022-10-03 10:18:15 +02:00
Anas Nashif
a81b322828 lib: onoff: add a config for on-off and build conditionally
Do not build this service unconditionally.

Partial fix of #50654

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2022-10-03 10:18:15 +02:00
Piotr Pryga
e2e06a74c3 libc: minimal: Add C11 aligned_alloc
Extend capabilities of a minimal libc to support C11 capability
to allocate memory with requested alignment.

Signed-off-by: Piotr Pryga <piotr.pryga@nordicsemi.no>
2022-10-03 10:13:25 +02:00
Stephanos Ioannidis
175cc385c4 lib: picolibc: Use smaller default heap size
The picolibc heap size configuration (`CONFIG_PICOLIBC_HEAP_SIZE`) is
used to set the statically allocated malloc heap size when userspace is
enabled.

The current default heap size of 1048576 bytes (MMU) and 65536 bytes
(MPU) is too large for most platforms that Zephyr supports and may
result in the picolibc tests being filtered out due to the increased
memory footprint of the compiled image (i.e. SRAM overflow).

This commit updates the default picolibc heap size to a more reasonable
16384 bytes for MMU platforms and 1024 bytes for MPU platforms.

Signed-off-by: Stephanos Ioannidis <stephanos.ioannidis@nordicsemi.no>
2022-09-30 13:52:24 +00:00
Chris Friedt
0217c3952a posix: conditionally compile perror pthread_common and nanosleep
Previously, these files were compiled unconditionally.

Partial fix for #50654

Signed-off-by: Chris Friedt <cfriedt@fb.com>
2022-09-28 14:06:55 +00:00
Jiafei Pan
d486bd3cfe lib: libc: newlib: make sure retargetable locking is enabled in toolchain
Add build assert to make sure _RETARGETABLE_LOCKING is enabled in
toolchain, When _RETARGETABLE_LOCKING is enabled, "_LOCK_T" is "__lock"
pointer type, otherwise "_LOCK_T" is "int" type, so there will be the
following compile warnings when toolchain doesn't enable
_RETARGETABLE_LOCKING:

zephyr/lib/libc/newlib/libc-hooks.c:416:13: warning: cast to pointer from
integer of different size [-Wint-to-pointer-cast]
  416 |  k_sem_take((struct k_sem *)lock, K_FOREVER);
      |             ^
zephyr/lib/libc/newlib/libc-hooks.c: In function '__retarget_lock_acquire
_recursive':
zephyr/lib/libc/newlib/libc-hooks.c:423:15: warning: cast to pointer from
integer of different size [-Wint-to-pointer-cast]
  423 |  k_mutex_lock((struct k_mutex *)lock, K_FOREVER);
      |               ^
...

Signed-off-by: Jiafei Pan <Jiafei.Pan@nxp.com>
2022-09-21 08:59:24 +00:00
Seppo Takalo
c93b4cf307 libc: newlib: Call gettimeofday() also when CONFIG_POSIX_CLOCK
When CONFIG_POSIX_CLOCK is enabled, we should have implementation
of gettimeofday() and therefore time(NULL) should return correct
time, instead of -1.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
2022-09-20 08:19:23 +00:00
Krzysztof Chruscinski
a7224830ce lib: os: cbprintf: Mechanism for detecting %p in static package
Static packaging is using only argument types to build a package. There
is one case where analysing argument type only is not enough to
determine package content. That is %p with (unsigned) char pointer vs
%s. In case of %s a string might need to be appended to the package
and in case of %p it must be avoided. Format string analysis is required
to distinguish those two cases.
In order to speed up the runtime inspection, additional information is
added to a static package. That is index of the string argument (where
first argument has index 0). This information allows quick format string
inspection where nth format specifier is found and checked if it is a
pointer format specifier.
Inspection algorithm is added to cbprintf_package_convert() and if %p
is found then data for that argument is discarded. Additionally, log
warning is printed with suggestion to cast pointer argument to void *
to avoid confusion. It is desired to get rid of this ambiguity because
there are going to be logging configurations where strings are stripped
from a binary and runtime inspection cannot be performed.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-09-19 10:14:23 +00:00
Gerard Marull-Paretas
79e6b0e0f6 includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.

The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.

NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-09-05 16:31:47 +02:00
Christopher Friedt
a5ee862ff0 posix: clock: make tz non-const in gettimeofday
This `tz` field is not const in the spec.

Signed-off-by: Christopher Friedt <cfriedt@fb.com>
2022-09-03 04:00:37 -04:00
Krzysztof Chruscinski
bb74b4f028 lib: os: cbprintf: Renamed flags used for conversion function
At some point, package copy function was extended and renamed
to cbprintf_package_convert. However, flags used by this
function were not renamed and used contained COPY idiom.
Deprecating flags with COPY and replacing them with flags
with CONVERT idiom to match function which is utilizing them.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-08-26 12:52:08 +02:00
Krzysztof Chruscinski
7303ac135d lib: os: spsc_pbuf: Minor code cleanup
Minor cleanup in allocation function. Using define instead of
sizeof(uint32_t) to better explain the purpose. Adding few
comments.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-08-24 17:48:50 +00:00
Krzysztof Chruscinski
6928c4a546 lib: os: spsc_pbuf: Fix race condition
Allocator is adding padding in 2 steps. First padding mark is written
to the buffer and then write index is reset to 0. Consumer may interrupt
this operation (another thread or core) and fail since it was only checking
padding marker and that alone was enough to consume the padding. If that
happen before write index got updated, buffer reading become corrupted.
Fixing it by adding write index check when padding is found.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-08-24 17:48:50 +00:00
Krzysztof Chruscinski
4b5ff413f5 lib: os: spsc_pbuf: Fix miscalculation in the allocation
Wrong value was used for free space calculation. Updating test which
previously was hiding this bug.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-08-24 17:48:50 +00:00
Andrei Emeltchenko
6f831d542a posix: getopt: Set variable only if used
Only assign dash_prefix when PRINT_ERROR true.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2022-08-19 12:07:45 +02:00
Andy Ross
02b23f3733 arch/posix: Add MemorySanitizer support
Wire this up the same way ASAN works.  Right now it's support only by
recent clang versions (not gcc), and only in 64 bit mode.  But it's
capable of detecting uninitialized data reads, which ASAN is not.

This support is wired into the sys_heap (and thus k_heap/k_malloc)
layers, allowing detection of heap misuse like use-after-free.  Note
that there is one false negative lurking: due to complexity, in the
case where a sys_heap_realloc() call is able to shrink memory in
place, the now-unused suffix is not marked uninitialized immediately,
making it impossible to detect use-after-free of those particular
bytes.  But the system will recover cleanly the next time the memory
gets allocated.

Also no attempt was made to integrate this handling into the newlib or
picolibc allocators, though that should hopefully be possible via
similar means.

Signed-off-by: Andy Ross <andyross@google.com>
2022-08-19 08:30:01 +02:00
Stephanos Ioannidis
e918783af1 lib: libc: minimal: Define off_t as intptr_t
The `off_t` type, which is specified by the POSIX standard as a signed
integer type representing file sizes, was defined as `long` or `int`
depending on the target architecture without a clear explanation on why
it was defined as such.

While the POSIX standard does not specify the size requirement of the
`off_t` type, it generally corresponds to the size of a pointer in
practice, mainly because the optimal file handling size is closely tied
to the native pointer size.

For this reason, this commit removes the per-architecture `off_t`
definition and defines it as `intptr_t` such that its size always
matches the native pointer size.

Note that the toolchain-defined `__INTPTR_TYPE__` macro is used instead
of the `intptr_t` typedef as per the common convention used in the C
standard library headers.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-08-19 02:47:50 +09:00
Daniel Leung
7dc20978e3 lib: os: cbprintf: fix typo exteral -> external
The cbprintf formatter cbvprintf_exteral_formatter_func has a typo
in it with a missing 'n'. So add it.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2022-08-17 08:08:46 +00:00
Evgeniy Paltsev
a8a4fec2b1 teslibct: don't allow picolib tests for ARC MWDT toolchain
Currently picolib isn't compatible with ARC MWDT toolchain,
so don't try to build picolib tests in case of ARC MWDT toolchain
usage.

Signed-off-by: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
Signed-off-by: Evgeniy Paltsev <PaltsevEvgeniy@gmail.com>
2022-08-16 08:23:22 +00:00
Keith Packard
8865d4d7db picolibc: Don't select TLS without toolchain support
If the architecture has TLS support, but the toolchain doesn't, then
don't enable Zephyr TLS support when selecting picolibc.

Closes: #47275.

Signed-off-by: Keith Packard <keithp@keithp.com>
2022-08-11 12:20:20 +02:00
Keith Packard
f0216a2619 libc/picolibc: Place malloc heap in noinit section if possible
When the heap is of a fixed size and there isn't a special malloc partition
in use, place the heap in uninitialized memory so that the application
doesn't spend time at startup erasing it. Picolibc malloc always clears
memory before returning it to applications, so this change will not be
visible to applications.

Signed-off-by: Keith Packard <keithp@keithp.com>
2022-08-11 12:20:05 +02:00
Enjia Mai
e696de1f31 lib: os: disable PRINTK_SYNC while using EFI console as log backend
There is a spinlock used in the EFI console code for printing
one line string. It already made the log output being in order
under multi-cores, so it doesn't need a printk sync spinlock
again. Disable it while using EFI console as a log backend.

Fixes #47512.

Signed-off-by: Enjia Mai <enjia.mai@intel.com>
2022-08-11 12:17:24 +02:00
Krzysztof Chruscinski
09b41829a8 lib: os: spsc_pbuf: Fix free space calculation
Fixing bug in free space calculation which was assuming 1 byte
padding and not 32 bit word padding. Bug could result in the
data corruption in certain scenario.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-08-02 08:13:31 -04:00
Flavio Ceolin
dfa71ca332 lib: os: Fix warning on XCC
xcc compiler complains about how fdtable variable is initialized:
"""
warning: missing braces around initialize
"""

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2022-07-25 17:07:11 -04:00
Krzysztof Chruscinski
b6dcdb17f9 lib: os: spsc_pbuf: Improve data cache configuration
Use CONFIG_DCACHE_LINE_SIZE to determine distance between
fields modified by different cores.
Add option which specifies what is the data cache line
of the remote core. Maximum from local and remote cache
line sizes is used as distance and alignement.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-07-18 12:21:16 -04:00
Tomislav Milkovic
0fe2c1fe90 everywhere: Fix legacy include paths
Any project with Kconfig option CONFIG_LEGACY_INCLUDE_PATH set to n
couldn't be built because some files were missing zephyr/ prefix in
includes
Re-run the migrate_includes.py script to fix all legacy include paths

Signed-off-by: Tomislav Milkovic <milkovic@byte-lab.com>
2022-07-18 16:16:47 +00:00
Marcin Szkudlinski
e4d11fe177 lib: mem_blocks: add a usage test for a block
add a sys_mem_blocks_is_region_free procedure to test
if the block in question is free or taken

Signed-off-by: Marcin Szkudlinski <marcin.szkudlinski@intel.com>
Signed-off-by: Tomasz Leman <tomasz.m.leman@intel.com>
2022-07-14 10:44:18 +02:00
Peter Mitsis
3b7818b2d2 lib/os: use generic mem stats structure for heap
Since the retrieved heap memory statistics are identical to those of
the retrieved mem_block statistics, it makes sense to use a single
generic-named structure for both instead of two identical structures
with different names.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2022-07-12 13:59:26 +00:00
Peter Mitsis
d061366f54 lib/os: add statistics tracking to mem_blocks
Both the current and maximum number of allocations in a given memory
blocked are tracked (and can be queried) when the
CONFIG_SYS_MEM_BLOCKS_RUNTIME_STATS Kconfig option is selected.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2022-07-12 13:59:26 +00:00
Christopher Friedt
da0398d198 posix: pthread: consider PTHREAD_EXITED state in pthread_create
If a thread is joined using `pthread_join()`, then the
internal state would be set to `PTHREAD_EXITED`.

Previously, `pthread_create()` would only consider pthreads
with internal state `PTHREAD_TERMINATED` as candidates for new
threads. However, that causes a descriptor leak.

We should be able to reuse a single thread an infinite number
of times.

Here, we also consider threads with internal state
`PTHREAD_EXITED` as candiates in `pthread_create()`.

Fixes #47609

Signed-off-by: Christopher Friedt <cfriedt@fb.com>
2022-07-12 10:16:02 +02:00
Anas Nashif
4f65bf6412 scripts: move gen_strerror_table.py to scripts/build
Move scripts needed by the build system and not designed to be run
individually or standalone into the build subfolder.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2022-07-12 10:03:45 +02:00
Alexander Dengg
21aa4a9a86 lib: open-amp: support resource table on C++ applications
In C++ applications, methods which are meant to be implemented in C
should be wrapped in a `extern "C"` scope at the point of declaration.
This enables the correct symbol table mangling, which fixes current
linker errors in C++.

Signed-off-by: Alexander Dengg <dornbirndevelops@gmail.com>
2022-07-06 10:56:01 -05:00
Anas Nashif
65337bc7eb lib: add mising braces to single line if statements
Following zephyr's style guideline, all if statements, including single
line statements shall have braces.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2022-07-06 11:00:45 -04:00
Stephanos Ioannidis
b8d4a31abd lib: libc: picolibc: Initialise libc heap during POST_KERNEL phase
This commit changes the invocation of the picolibc malloc heap
initialisation function such that it is executed during the POST_KERNEL
phase instead of the APPLICATION phase.

This is necessary in order to ensure that the application
initialisation functions (i.e. the functions called during the
APPLICATIION phase) can make use of the libc heap.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-07-06 10:46:39 +02:00
Stephanos Ioannidis
43e1c28a25 lib: libc: newlib: Initialise libc heap during POST_KERNEL phase
This commit changes the invocation of the newlib malloc heap
initialisation function such that it is executed during the POST_KERNEL
phase instead of the APPLICATION phase.

This is necessary in order to ensure that the application
initialisation functions (i.e. the functions called during the
APPLICATIION phase) can make use of the libc heap.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-07-06 10:46:39 +02:00
Stephanos Ioannidis
db0748c462 lib: libc: minimal: Initialise libc heap during POST_KERNEL phase
This commit changes the invocation of the minimal libc malloc
initialisation function such that it is executed during the POST_KERNEL
phase instead of the APPLICATION phase.

This is necessary in order to ensure that the application
initialisation functions (i.e. the functions called during the
APPLICATIION phase) can make use of the libc heap.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-07-06 10:46:39 +02:00
Christopher Friedt
b391993d1b lib: posix: add perror() implementation
Add a trivial implementation of `perror()`.

Fixes #46100

Signed-off-by: Christopher Friedt <cfriedt@fb.com>
2022-07-04 22:53:36 +02:00
Christopher Friedt
4cc443705d libc: minimal: add strerror and strerror_r function
Add simple strerror() and strerror_r() implementations.

Fixes #46099

Signed-off-by: Christopher Friedt <cfriedt@fb.com>
2022-07-04 22:53:36 +02:00
Emil Gydesen
de4b564754 zephyr: Fix n=0 for utf8_lcpy
The function used an assert if n was 0. Instead
of using an assert, the function will now just
not do anything. The documentation has also
been updated to reflect this.

The reasoning for this is that the strlcpy function
this (sort of) implements for utf8 works the same way.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
2022-07-04 15:49:22 +02:00
Abramo Bagnara
8521b43546 coding guidelines: comply with MISRA C:2012 Rule 21.13
MISRA C:2012 Rule 21.13 (Any value passed to a function in <ctype.h>
shall be representable as an unsigned char or be the value EOF).

Functions in <ctype.h> have undefined behavior if they are called with
any other value. Callers affected by this change are not prepared to
handle EOF anyway. The addition of these casts avoids the issue
and does not result in any performance penalty.

Signed-off-by: Abramo Bagnara <abramo.bagnara@bugseng.com>
Signed-off-by: Simon Hein <SHein@baumer.com>
2022-06-30 17:34:28 -04:00
Abramo Bagnara
795500bbe3 coding guidelines: comply with MISRA C:2012 Rule 9.3
MISRA C:2012 Rule 9.3 (Arrays shall not be partially initialized.)

Systematically use `{0}' to specify full 0 initialization
(not `{}', not `{0U}').

Signed-off-by: Abramo Bagnara <abramo.bagnara@bugseng.com>
Signed-off-by: Simon Hein <SHein@baumer.com>
2022-06-30 17:34:05 -04:00
Keith Packard
bc234fb1af libc/picolibc: Rework malloc arena setup
Picolibc inherited its malloc arena configuration from newlib instead of
from minimal libc. This ended up making it a bit too fragile to run the
full set of zephyr tests. In particular:

 * Z_MALLOC_PARTITION_EXISTS would get set when not used

 * Setting an arena size depended on a bunch of other values, including
   whether the system had an MMU or MPU, and whether the MPU required
   power-of-two alignment or not.

This patch cleans things up so that there is a single heap size specifier,
PICOLIBC_HEAP_SIZE.

 * If PICOLIBC_HEAP_SIZE is positive, this sets the size of the heap. On
   MMU systems, picolibc will only use the remaining memory if that's
   smaller.

 * If PICOLIBC_HEAP_SIZE is zero, then there is no heap available and
   malloc will always fail. This also disables Z_MALLOC_PARTITION_EXISTS.

 * If PICOLIBC_HEAP_SIZE is negative, then picolibc uses all remaining
   memory for the malloc heap.

The defaults are designed to allow tests to work without requiring
additional settings.

 * For MMU enabled systems, the default value is 1048576. It would be nice
   to have this use 'all available memory', but that's difficult to manage
   as the API which returns free memory (k_mem_free_get) doesn't take into
   account the amount of free virtual address space.

 * For MPU enabled systems which require power-of-two aligned MPU regions,
   the default value is 64kB.

 * For other systems, the default value is -1, indicating that all
   available memory be used for the malloc arena.

Signed-off-by: Keith Packard <keithp@keithp.com>
2022-06-30 10:33:24 +02:00
Krzysztof Chruscinski
715ae32501 lib: os: spsc_pbuf: Add option to get maximum utilization
Add option to track maximum utilization of the packet buffer.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-06-30 10:30:49 +02:00
Krzysztof Chruscinski
fa055f743f lib: os: spsc_pbuf: Extend API with zero copy model
Added functions which allow to use zero copy model for handling
data within the packet buffer.

Additionally, added handling of cache by adding option to keep rd_idx
in different cache line than wr_idx and data.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-06-30 10:30:49 +02:00
Keith Packard
866f1cb9b0 lib/cbprintf: Disable tagged arguments with picolibc
Picolibc doesn't have the non-standard printf support required for tagged
arguments in cbprintf. Disable this and use the format string parsing code
instead.

Signed-off-by: Keith Packard <keithp@keithp.com>
2022-06-29 10:29:13 +02:00
Keith Packard
2d20faab47 lib/os: Replace cbvprintf and printfcb family when using picolibc
Picolibc already provides the functionality offered by cbprintf, so
there's no reason to use the larger and less functional version included
in zephyr.

Signed-off-by: Keith Packard <keithp@keithp.com>
2022-06-27 12:47:25 +02:00
Keith Packard
d90fa562b2 lib/printk: Use picolibc's vfprintf instead of cbvprintf
When using picolibc, change vprintk to use picolibc's vfprintf instead of
cbvprintf.

Building samples/hello-world for qemu-cortex-m3.

old:

Memory region         Used Size  Region Size  %age Used
           FLASH:        9056 B       256 KB      3.45%
            SRAM:        3960 B        64 KB      6.04%
        IDT_LIST:          0 GB         2 KB      0.00%

new:

Memory region         Used Size  Region Size  %age Used
           FLASH:        7900 B       256 KB      3.01%
            SRAM:        3960 B        64 KB      6.04%
        IDT_LIST:          0 GB         2 KB      0.00%

Signed-off-by: Keith Packard <keithp@keithp.com>
2022-06-24 20:30:03 +02:00
Keith Packard
d465dbba6d lib/printk: Removed unused 'count' field in printk context
There's no use of the 'count' field of the context, so remove it. Because
that's the only member of struct out_context, remove the whole struct.

Signed-off-by: Keith Packard <keithp@keithp.com>
2022-06-24 20:30:03 +02:00
Keith Packard
ecee708e4b lib/printk: Use picolibc's snprintf/vsnprintf for snprintk/vsnprintk
Replace wrapper functions that use cbprintf with direct calls to picolibc
stdio functions.

Signed-off-by: Keith Packard <keithp@keithp.com>
2022-06-24 20:30:03 +02:00
Christopher Friedt
9d433c89a2 lib: posix: semaphore: use consistent timebase in sem_timedwait
In the Zephyr implementation, `sem_timedwait()` uses a
potentially wildly different timebase for comparison via
`k_uptime_get()` (uptime in ms).

The standard specifies `CLOCK_REALTIME`. However, the real-time
clock can be modified to an arbitrary value via clock_settime()
and there is no guarantee that it will always reflect uptime.

This change ensures that `sem_timedwait()` uses a more
consistent timebase for comparison.

Fixes #46807

Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
2022-06-24 20:12:05 +02:00
Andrei Emeltchenko
edc98394b1 lib: os: Remove unneeded assignment
Remove assignment since it is duplicated at the end of do-while
loop.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2022-06-23 15:43:59 -05:00
Keith Packard
0984aedf68 libc/picolibc: When !TLS, use zephyr errno
For targets without thread local storage, we need to use the builtin
per-thread errno support provided by Zephyr as the multi-thread errno
support provided in picolibc relies on TLS.

Signed-off-by: Keith Packard <keithp@keithp.com>
2022-06-23 09:16:32 +02:00
Abramo Bagnara
d1d5acd2cd coding guidelines: comply with MISRA C:2012 Rule 8.2
MISRA C:2012 Rule 8.2 (Function types shall be in prototype form with
named parameters.)

Added missing parameter names.

Signed-off-by: Abramo Bagnara <abramo.bagnara@bugseng.com>
2022-06-22 17:17:39 -04:00
Keith Packard
d0c75f3b96 lib/libc: Add picolibc support (aarch32, aarch64 and RISC-V) [v21]
Picolibc is a fork of newlib designed and tested on embedded systems. It
offers a smaller memory footprint (both ROM and RAM), and native TLS
support, which uses the Zephyr TLS support.

By default, the full printf version is included in the executable, which
includes exact floating point and long long input and output. A
configuration option has been added to switch to the integer-only
version (which also omits long long support).

Here are some size comparisons using qemu-cortex-m3 and this application
(parameters passed to printf to avoid GCC optimizing it into puts):

void main(void)
{
    printf("Hello World! %s %d\n", CONFIG_BOARD, 12);
}

                       FLASH    SRAM
    minimal             8696    3952
    picolibc int        7600    3960
    picolibc float     12304    3960
    newlib-nano int    11696    4128
    newlib-nano float  30516    4496
    newlib             34800    6112

---

v2:
	Include picolibc-tls.ld

v3:
	Document usage in guides/c_library.rst and
	getting_started/toolchain_other_x_compilers.rst

v4:
	Lost the lib/libc/picolibc directory somehow!

v5:
	Add PICOLIBC_ALIGNED_HEAP_SIZE configuration option.
	Delete PICOLIBC_SEMIHOST option support code

v6:
	Don't allocate static RAM for TLS values; TLS
	values only need to be allocated for each thread.

v7:
	Use arm coprocessor for TLS pointer storage where supported for
	compatibility with the -mtp=cp15 compiler option (or when the
	target cpu type selects this option)

	Add a bunch of tests

	Round TLS segment up to stack alignment so that overall stack
	remains correctly aligned

	Add aarch64 support

	Rebase to upstream head

v8:
	Share NEWLIB, NEWLIB_NANO and PICOLIBC library configuration
	variables in a single LIBC_PARTITIONS variable instead of
	having separate PICOLIBC_PART and NEWLIB_PART variables.

v9:
	Update docs to reference pending sdk-ng support for picolibc

v10:
	Support memory protection by creating a partition for
	picolibc shared data and any pre-defined picolibc heap.

v11:
	Fix formatting in arch/arm/core/aarch64/switch.S

v12:
	Remove TLS support from this patch now that TLS is upstream
	Require THREAD_LOCAL_STORAGE when using PICOLIBC for architectures
	that support it.

v13:
	Merge errno changes as they're only needed for picolibc.
	Adapt cmake changes suggested by Torsten Tejlmand Rasmussen

v14:
	Update to picolibc 1.7 and newer (new stdin/stdout/stderr ABI)

v15:
	Respond to comments from dcpleung:
	* switch kernel/errno to use CONFIG_LIBC_ERRNO instead of
          CONFIG_PICOLIBC
	* Add comment to test/lib/sprintf as to why the %n test
	  was disabled for picolibc.

v16:
	Switch picolibc to a module built with Zephyr. This eliminates
	toolchain dependencies and allows compiler settings for Zephyr
	to also be applied to picolibc.

v17:
	Provide Zephyr-specific 'abort' implementation.
	Support systems with MMU

v18:
	Allow use of toolchain picolibc version.

v19:
	Use zephyr/ for zephyr headers

v20:
	Add locking
	Use explicit commit for picolibc module

v21:
	Create PICOLIBC_SUPPORTED config param. Set on arc, arm, arm64,
	mips and riscv architectures.

Signed-off-by: Keith Packard <keithp@keithp.com>
2022-06-22 13:15:55 +02:00
Kai Vehmanen
7f6b762a73 lib/os: winstream: properly include string.h for memcpy
If system memcpy() is used, assert.h must be included. Fixes
a build warning on undeclared use of memcpy.

Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
2022-06-21 14:02:13 -04:00
Abramo Bagnara
ada9ca4c93 coding guidelines: comply with MISRA C:2012 Rule 7.2
MISRA C:2012 Rule 7.2 (A `u' or `U' suffix shall be applied to all
integer constants that are represented in an unsigned type)

Added missing `U' suffixes in constants that are involved in the
analyzed build, plus a few more not to introduce inconsistencies
with respect to nearby constants that are either unused in the
build (but implicitly unsigned) or are used and are immediately
converted to unsigned.

Signed-off-by: Abramo Bagnara <abramo.bagnara@bugseng.com>
2022-06-14 13:36:14 -04:00
Keith Packard
11267a30a1 lib/os/cbprintf: Use signed char for %hhd format
The cast to narrow for %hhd support must be 'signed char' instead of 'char'
to support targets where 'char' is unsigned, as on riscv.

Signed-off-by: Keith Packard <keithp@keithp.com>
2022-06-09 11:32:58 +02:00
Krzysztof Chruscinski
0829610bbc lib: os: spsc_pbuf: Add option to use cache
Add flags option to init call and a flag to use cache.
Add Kconfig choice to pick how to approach cache. Cache can be
enforced in all spsc_pbuf instances, disable in all, or runtime selected
based on configuration flag. Option is added to allow memory footprint
savings.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-06-07 19:04:35 +02:00
Krzysztof Chruscinski
2f189e39a5 lib: os: Rename icmsg_buf to spsc_pbuf
Move icmsg_buf to lib/os and rename to spsc_pbuf (Single Producer
Single Consumer Packet Buffer). It is a generic module and initially
was created as internal module for ipc service.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-06-07 19:04:35 +02:00
Daniel Leung
aff6e8b2f7 lib: os: cbprint: enable tagged arguments for packaging
This adds some bits to support tagged arguments to be used for
packaging. If enabled, the packaging function no longer looks at
the format strings to determine the types of arguments, but
instead, each argument is tagged with a type by preceding it
with another argument as type (integer). This allows the format
strings to be removed from the final binary to conserve space.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2022-06-08 00:15:55 +09:00
Andrei Emeltchenko
29128bfa02 lib: mpsc_pbuf: Fix compilation unused-but-set-variable warning
Use ARG_UNUSED() to fix compilation warnings.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2022-06-06 22:47:11 +02:00
Andrei Emeltchenko
3cf5c3c33c lib: ring_buf: Make code consistent
Make code easier to read by using the same name for error code.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2022-06-06 22:46:43 +02:00
Andrei Emeltchenko
e63eedce51 lib: ring_buf: Fix unused-but-set compilation warnings
For the error codes used only in __ASSERT() statements fix compilation
warnings like:

...
warning: variable 'err' set but not used [-Wunused-but-set-variable]
        int err;
            ^
...

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2022-06-06 22:46:43 +02:00
Andrei Emeltchenko
14b6f491cb lib: bitarray: Remove dead statement
Remove dead code since we go out of the loop after break statement.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2022-06-06 22:46:37 +02:00
Juha Heiskanen
ec1c85b385 lib: os: Extend Json library API and type update
New Types:
* Added support for Generic Numeric type (float, 64-bit)
* Added support for Opaque string type.
* Added support parse Array data in object for seprate array parsing
New API for Json Array parsing Object 1 by 1:
* json_arr_separate_object_parse_init() init array parse
* json_arr_separate_parse_object() Parsing 1 Json Object
Rename token and lexer structures and publish those.

Signed-off-by: Juha Heiskanen <juha.heiskanen@nordicsemi.no>
2022-06-06 12:06:43 +02:00
Andrei Emeltchenko
c62a380843 lib: os: Fix unused variable set warning
Fixes following warning during compilation with llvm:

...
lib/os/mpsc_pbuf.c:428:8: warning: variable 'wrap' set but not used
  [-Wunused-but-set-variable]
        bool wrap;
             ^
1 warning generated.
...

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2022-06-05 14:49:45 +02:00
Andrei Emeltchenko
0c934e22d1 lib: rb: Remove unneeded statement
Remove unneeded statement since it is done in the beginning of the
loop.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2022-06-05 14:49:37 +02:00
Andrei Emeltchenko
3074b5808e libc: sqrtf: Remove dead assignment
Remove dead assignment from sqrtf().

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2022-06-05 14:49:31 +02:00
Stephanos Ioannidis
eb710039c0 libc: minimal: Fix gmtime() userspace support
The gmtime() function returns a global result variable, and this
variable must be placed in the `z_libc_partition` when userspace is
enabled.

Since gmtime() makes use of a global variable and this results in a
footprint increase, this commit makes the time functions optional by
introducing `CONFIG_MINIMAL_LIBC_TIME` Kconfig and making them only
available when this option is enabled.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-06-01 11:03:38 +02:00
Stephanos Ioannidis
2bcb713371 libc: Define Z_LIBC_DATA macro globally
This commit globally defines the `Z_LIBC_DATA` macro, which is used to
place variables into the libc memory partition, so that it can be
re-used.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-06-01 11:03:38 +02:00
Stephanos Ioannidis
4735e10630 libc: minimal: Introduce CONFIG_MINIMAL_LIBC_NON_REENTRANT_FUNCTIONS
This commit introduces a new configuration called
`CONFIG_MINIMAL_LIBC_NON_REENTRANT_FUNCTIONS`, which enables the
traditional non-reentrant (i.e. not thread-safe) version of the C
standard library functions such as rand() and gmtime() when the
respective configs are enabled.

The non-reentrant functions make use of the globals and require an
additional memory partition (MPU region), which is scarce on low-end
devices, when CONFIG_USERSPACE=y.

The purpose of this option is to classify the MPU resource intensive
functions as a separate category and only enable them when there is a
demand for such.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-06-01 11:03:38 +02:00
Stephanos Ioannidis
b290926a99 libc: minimal: Add reentrant rand_r function
This commit adds the `rand_r` function, which is a reentrant (i.e.
thread-safe) version of the `rand` function, such that a thread-safe
variant is always available.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-06-01 11:03:38 +02:00
Stephanos Ioannidis
d755050a14 libc: minimal: Add PRIxMAX macros for [u]intmax_t
This commit adds the missing `PRIxMAX` macros for the C99 `intmax_t`
and `uintmax_t` types:

  PRIdMAX, PRIiMAX, PRIoMAX, PRIuMAX, PRIxMAX, PRIXMAX

Note that the `PRIxMAX` macros specify the `ll` size modifier because
the type of the `intmax_t` for the minimal libc is defined as that of
the `int64_t`, which is always overridden to `long long int` by
`zephyr_stdint.h`; for more details, refer to the GitHub PR #29876,
which deliberately introduced this scheme.

In the future, this scheme will need to be reworked such that the
minimal libc `stdint.h` defines `intmax_t` as `__INTMAX_TYPE__`, and
the `inttypes.h` resolves the corresponding format specifier.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-05-26 17:38:50 +02:00
Stephanos Ioannidis
c3f80aa844 libc: minimal: Add PRIx{FAST,LEAST}N macros
This commit adds the missing `PRIx{FAST,LEAST}N` C99 integer type
format macros that correspond to the C99 integer types overridden in
the `zephyr_stdint.h` header:

  PRIdFAST8, PRIdFAST16, PRIdFAST32, PRIdFAST64
  PRIdLEAST8, PRIdLEAST16, PRIdLEAST32, PRIdLEAST64

  PRIiFAST8, PRIiFAST16, PRIiFAST32, PRIiFAST64
  PRIiLEAST8, PRIiLEAST16, PRIiLEAST32, PRIiLEAST64

  PRIoFAST8, PRIoFAST16, PRIoFAST32, PRIoFAST64
  PRIoLEAST8, PRIoLEAST16, PRIoLEAST32, PRIoLEAST64

  PRIuFAST8, PRIuFAST16, PRIuFAST32, PRIuFAST64
  PRIuLEAST8, PRIuLEAST16, PRIuLEAST32, PRIuLEAST64

  PRIxFAST8, PRIxFAST16, PRIxFAST32, PRIxFAST64
  PRIxLEAST8, PRIxLEAST16, PRIxLEAST32, PRIxLEAST64

  PRIXFAST8, PRIXFAST16, PRIXFAST32, PRIXFAST64
  PRIXLEAST8, PRIXLEAST16, PRIXLEAST32, PRIXLEAST64

Note that these macros will eventually need to be defined according to
the toolchain-specified types when the `zephyr_stdint.h` hack is
removed in the future; refer to the the GitHub issue #46032 for more
details.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-05-26 17:38:50 +02:00
Stephanos Ioannidis
c2db0c5a86 libc: minimal: Add strstr implementation
This commit adds the strstr function implementation that is licensed
BSD-3-Clause, which is an OSI-approved license, with the modifications
necessary for adoption into the Zephyr minimal C library.

Note that this implementation is based on the size optimised version of
the newlib strcasestr function.

Origin: Newlib
License: BSD 3-Clause
URL: git://sourceware.org/git/newlib-cygwin.git
Commit: 9087163804df8af6dc2ec1f675a2341c25f7795f
Purpose: strstr function support in the minimal C library

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-05-25 14:52:29 +02:00
Stephanos Ioannidis
8c086d5791 libc: minimal: Remove incompatibly licensed strstr function
This commit removes the strstr function implementation that is
licensed BSD-4-Clause-UC, which is not an OSI-approved license.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-05-25 14:52:29 +02:00
Stephanos Ioannidis
c96f15ecf3 libc: minimal: Add strtoull implementation
This commit adds the strtoull function implementation that is licensed
BSD-3-Clause, which is an OSI-approved license, with the modifications
necessary for adoption into the Zephyr minimal C library.

Origin: Newlib
License: BSD 3-Clause
URL: git://sourceware.org/git/newlib-cygwin.git
Commit: 9042d0ce65533a26fc3264206db5828d5692332c
Purpose: strtoull function support in the minimal C library

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-05-25 14:52:29 +02:00
Stephanos Ioannidis
c6b75cd4cd libc: minimal: Remove incompatibly licensed strtoull function
This commit removes the strtoull function implementation that is
licensed BSD-4-Clause-UC, which is not an OSI-approved license.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-05-25 14:52:29 +02:00
Stephanos Ioannidis
25b903cec1 libc: minimal: Add strtoll implementation
This commit adds the strtoll function implementation that is licensed
BSD-3-Clause, which is an OSI-approved license, with the modifications
necessary for adoption into the Zephyr minimal C library.

Origin: Newlib
License: BSD 3-Clause
URL: git://sourceware.org/git/newlib-cygwin.git
Commit: 9042d0ce65533a26fc3264206db5828d5692332c
Purpose: strtoll function support in the minimal C library

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-05-25 14:52:29 +02:00
Stephanos Ioannidis
570ed08221 libc: minimal: Remove incompatibly licensed strtoll function
This commit removes the strtoll function implementation that is
licensed BSD-4-Clause-UC, which is not an OSI-approved license.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-05-25 14:52:29 +02:00
Stephanos Ioannidis
4c78607618 libc: minimal: Add strtoul implementation
This commit adds the strtoul function implementation that is licensed
BSD-3-Clause, which is an OSI-approved license, with the modifications
necessary for adoption into the Zephyr minimal C library.

Origin: Newlib
License: BSD 3-Clause
URL: git://sourceware.org/git/newlib-cygwin.git
Commit: 9042d0ce65533a26fc3264206db5828d5692332c
Purpose: strtoul function support in the minimal C library

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-05-25 14:52:29 +02:00
Stephanos Ioannidis
b6b1edbf91 libc: minimal: Remove incompatibly licensed strtoul function
This commit removes the strtoul function implementation that is
licensed BSD-4-Clause-UC, which is not an OSI-approved license.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-05-25 14:52:29 +02:00
Stephanos Ioannidis
f1563751b3 libc: minimal: Add strtol implementation
This commit adds the strtol function implementation that is licensed
BSD-3-Clause, which is an OSI-approved license, with the modifications
necessary for adoption into the Zephyr minimal C library.

Origin: Newlib
License: BSD 3-Clause
URL: git://sourceware.org/git/newlib-cygwin.git
Commit: 9042d0ce65533a26fc3264206db5828d5692332c
Purpose: strtol function support in the minimal C library

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-05-25 14:52:29 +02:00
Stephanos Ioannidis
35caf38ce7 libc: minimal: Remove incompatibly licensed strtol function
This commit removes the strtol function implementation that is
licensed BSD-4-Clause-UC, which is not an OSI-approved license.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-05-25 14:52:29 +02:00
Tomislav Milkovic
3df84e6d55 lib: smf: fix include
Change include to angle brackets
Add zephyr/ prefix to include

Signed-off-by: Tomislav Milkovic <milkovic@byte-lab.com>
2022-05-25 11:14:18 +09:00
Anas Nashif
e5471a253e p4wq: register log module with default log level
Set default log level while registering p4wq as a log module.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2022-05-17 20:39:25 -04:00
Keith Packard
05946ed9b2 lib/libc/minimal: Move sqrt/sqrtf from samples
The lmp90100_evb sample included an implementation of double sqrt, and the
on_off_level_lighting_vnd_app sample included an implementation of float
sqrtf. Move that code into minimal libc instead of requiring applications
to hand-roll their own version.

Signed-off-by: Keith Packard <keithp@keithp.com>
2022-05-14 08:49:36 +09:00
Rico Ganahl
5cf1193902 lib/posix/fs: fix end-of-dir in readdir
POSIX readdir should return NULL if end of dir is reached and
leave errno untouched.

Signed-off-by: Rico Ganahl <rico.ganahl@bytesatwork.ch>
2022-05-13 11:10:04 -07:00
Matt Campbell
2d937f0683 lib: os: mpsc_pbuf: null check notify_drop callback
Allow the user of a mpsc_pbuf to not use the notify_drop callback by
setting it to NULL.

signed-off-by: Matt Campbell <matt@silvertree.io>
2022-05-10 18:32:26 +02:00
Gerard Marull-Paretas
cbd31d720b lib: migrate includes to <zephyr/...>
In order to bring consistency in-tree, migrate all lib code to the new
prefix <zephyr/...>. Note that the conversion has been scripted, refer
to zephyrproject-rtos#45388 for more details.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-05-06 19:58:09 +02:00
Krzysztof Chruscinski
4b4c5bfcee lib: os: cbprintf: Fix variable assignment
When in_len is 0 then length is calculated from the package and
assignment was missing.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-05-05 14:29:26 -05:00
Krzysztof Chruscinski
07322b85c9 lib: os: cbprintf: Add function for converting package
Extend package copying functionality by adding function for converting
a package. Function gets callback+context pair and converted package
is part by part passed to that callback. Contrary to typical sprintf
callback which works on chars, callback works with buffers.

Existing cbprintf_package_copy function is implemented as static
inline and uses new cbprintf_package_convert API.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-05-04 18:56:55 -04:00
Guennadi Liakhovetski
462bd8abc2 sparse: fix all errors
sparse complains about cbprintf incompatible callback type and
incorrect size of struct __va_list. Add exceptions to silence those
errors.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2022-04-28 08:54:21 -04:00
Carlo Caione
1dcea253d2 shared_multi_heap: Rework framework
Entirely rework the shared_multi_heap framework. Refer to the
documentation for more information.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2022-04-21 13:15:26 +02:00
Flavio Ceolin
bb98bd6d11 libc: newlib: Fix declare a type conflict
Fix a variable declaration type conflict:

libc-hooks.c:92:16: error: conflicting types for '_heap_sentry'
   92 |   extern void *_heap_sentry;
soc.h:78:13: note: previous declaration of '_heap_sentry' was here

Fixes #44926

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2022-04-18 14:42:19 -05:00
Rico Ganahl
f2affbd973 os: lib: bin2hex: fix memory overwrite
Destination buffer size could be too small by one,
but null termination is still written. This could cause an
overwrite in contiguous memory without notice.

Signed-off-by: Rico Ganahl <rico.ganahl@bytesatwork.ch>
2022-04-13 13:43:53 -07:00
Damian Krolik
5f5410a0cc sys: heap: support maximum allocated bytes statistic
Besides the current allocated/free bytes, keep track of
the maximum allocated bytes to help determine the heap
size requirements. Also, provide a function to reset
the statistic.

Signed-off-by: Damian Krolik <damian.krolik@nordicsemi.no>
2022-04-13 13:27:28 -07:00
Christopher Friedt
7c583bbf8f lib: posix: support for pthread_attr_setstacksize
Support pthread_attr_setstacksize(3).

See https://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_attr_getstacksize.html

Fixes #44722

Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
2022-04-12 22:02:04 +09:00
Yuval Peress
53ef68d459 include: Prefix includes to use a scope
Move include paths and add new target_include_directories to support
backwards compatibility:
* /include -> /include/zephyr
  example: <irq.h> -> <zephyr/irq.h>

Issue #41543

Signed-off-by: Yuval Peress <peress@google.com>
2022-04-08 19:03:32 +02:00
Marcin Szkudlinski
68a5059ff3 os/lib: change all NULL checking in mem_blocks.c to asserts
Some problems, as passing NULL when a context in expected,
always reflect to bugs. Such problems cannot be handled
in runtime. So the desired action is system panic.

Signed-off-by: Marcin Szkudlinski <marcin.szkudlinski@intel.com>
2022-04-05 14:29:33 -04:00
Marcin Szkudlinski
05e16411fb os/lib: change all NULL checking in bitarray.c to asserts
Some problems, as passing NULL when a context in expected,
always reflect to bugs. Such problems cannot be handled
in runtime. So the desired action is system panic.

Signed-off-by: Marcin Szkudlinski <marcin.szkudlinski@intel.com>
2022-04-05 14:29:33 -04:00
Marcin Szkudlinski
033dbae787 lib/mem_blocks: add allocation for contiguous memory blocks
Added allocation and freeing of a contiguous memory blocks

Signed-off-by: Marcin Szkudlinski <marcin.szkudlinski@intel.com>
2022-04-05 14:29:33 -04:00
Marcin Szkudlinski
297321a9e1 lib/os: sys_mem_blocks_get added
memory block should allow not only allocate memory but also
getting an arbitrary chosen part of memory.
Mixing of allocate and get, however possible, may be dangerous
because alloc may get any of memory. So be careful.

Signed-off-by: Marcin Szkudlinski <marcin.szkudlinski@intel.com>
2022-04-05 14:29:33 -04:00
Marcin Szkudlinski
e25a7c4611 lib/mem_blocks: change internal alloc_one to alloc_blocks
As preparation for memory blocks to support continuous memory blocks
modify bitmap operation wrappers to support multiple bits alloc/free

Signed-off-by: Marcin Szkudlinski <marcin.szkudlinski@intel.com>
2022-04-05 14:29:33 -04:00
Marcin Szkudlinski
6c6a16d3b9 lib: bitarray: sys_bitarray_test_and_set_region added
a method to check and set/clear a chosen region in a bitmap
if not previously set/cleared in a single atomic operation.
Useful for keeping track of resources usage, like memory banks

Signed-off-by: Marcin Szkudlinski <marcin.szkudlinski@intel.com>
2022-04-05 14:29:33 -04:00
Jaxson Han
0e1ff84fe7 posix: Fix pthread_once has incorrect behavior
As described in
https://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_once.html.
The behavior of pthread_once() is undefined if once_control has
automatic storage duration or is not initialized by PTHREAD_ONCE_INIT

However, in Zephyr, the implementation is incorrect. If the init value
is PTHREAD_ONCE_INIT, the program will never run the init_func.

Signed-off-by: Jaxson Han <jaxson.han@arm.com>
2022-04-05 10:38:30 -07:00
Krzysztof Chruscinski
35593ce1e9 lib: posix: clock: Prevent early overflows
Algorithm was converting uptime to nanoseconds which can easily
lead to overflows. Changed algorithm to use milliseconds and
nanoseconds for remainder only.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-04-01 09:37:41 +02:00
Krzysztof Chruscinski
a33f7de86a lib: os: cbprintf: Add CBPRINTF_PACKAGE_COPY_KEEP_RO_STR flag
Add flag to copy function which indicates that read-only
string locations shall be kept in the output package.

Updated cbprintf_package test to pass.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-03-29 23:03:35 -04:00
Krzysztof Chruscinski
6d98a1cc7e debug: Revert ASSERT options dependency
f4df23c9 added dependency on ASSERT to some options prefixed
with ASSERT_ assuming that they are no used elsewhere. Turned
out that there are subsystem specific assert macros (e.g. BT_ASSERT)
which relies on those options. Removing the dependency.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-03-29 12:29:55 -07:00
Gerhard Jörges
4fd24a4341 libc: minimal: Add strtoll() and strtoull()
- strtoll() and strtoull() are copies of strtol() and strtoul() with
  types changed to long long instead of long.
- added tests
- added documentation
- removed stubs from civetweb sample

Signed-off-by: Gerhard Jörges <joerges@metratec.com>
2022-03-24 11:03:06 +01:00
Matthias Fend
fc4055852e lib: open-amp: add support for an empty resource table
This allows a resource table to be included even if neither virtIO nor the
RAM console are used.

Signed-off-by: Matthias Fend <matthias.fend@emfend.at>
2022-03-24 10:41:39 +01:00
Nazar Kazakov
f483b1bc4c everywhere: fix typos
Fix a lot of typos

Signed-off-by: Nazar Kazakov <nazar.kazakov.work@gmail.com>
2022-03-18 13:24:08 -04:00
Krzysztof Chruscinski
786647bdcf lib: os: assert: Avoid including printk.h in __assert.h
Create wrapper for printk to avoid including printk.h in __assert.h.
__assert.h is used everywhere thus should not have dependency to
printk.h.

Cleanup assert Kconfig options.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-03-16 10:27:20 +01:00
Nazar Kazakov
9713f0d47c everywhere: fix typos
Fix a lot of typos

Signed-off-by: Nazar Kazakov <nazar.kazakov.work@gmail.com>
2022-03-14 20:22:24 -04:00
Krzysztof Chruscinski
c917e6c2ce lib: os: cbprintf: Extend API to support new packaging modes
Added new flags to packaging API:
- CBPRINTF_PACKAGE_ADD_RO_STR_IDXS - when set, read-only string
  locations are appended to the package
- CBPRINTF_PACKAGE_ADD_RW_STR_IDXS - when set, read-write string
  locations are appended to the package (instead of appending actual
  strings)
- CBPRINTF_PACKAGE_FIRST_RO_STR_CNT(n) - indicate that n first strings
  are known to be read only. Ignored in runtime packaging.

Add function for copying packages with optional appending strings.

Changed CBPRINTF_MUST_RUNTIME_PACKAGE to use same flags as packaging.

Aligned logging and test to those changes.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-03-14 11:16:14 +01:00
Marcin Szkudlinski
2597b787d8 lib/os: Add sys_multi_heap_get_heap function for multi_heap
Sometimes there's a need to perform special operations on a heap
for a specified block. I.e. release virtual memory mapping,
power off memory bank etc.
The added procedure gets a pointer to a proper heap with metadata
for a given address.

Signed-off-by: Marcin Szkudlinski <marcin.szkudlinski@intel.com>
2022-03-11 13:56:05 -05:00
Marcin Szkudlinski
2a0ee8c920 lib/os: Add metadata to heap in multi_heap
When operating on different kinds of heaps sometimes there's a need to
perform special operations on heap, poweroff memory bank when releasing
memory etc. Therefore some additional data may be required.
Metadata is a point to keep such data.

Signed-off-by: Marcin Szkudlinski <marcin.szkudlinski@intel.com>
2022-03-11 13:56:05 -05:00
Gerard Marull-Paretas
dffaf5375c kconfig: tweak Kconfig prompts
Tweak some Kconfig prompts after the removal of "Enable...".

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-03-09 15:35:54 +01:00
Gerard Marull-Paretas
95fb0ded6b kconfig: remove Enable from boolean prompts
According to Kconfig guidelines, boolean prompts must not start with
"Enable...". The following command has been used to automate the changes
in this patch:

sed -i "s/bool \"[Ee]nables\? \(\w\)/bool \"\U\1/g" **/Kconfig*

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-03-09 15:35:54 +01:00
Carles Cufi
66c9a8e0f9 lib: libc: minimal: Add macros for fast and least min/max values
The minimal C library already supports the fast and least types via
typedefs, but the corresponding min and max macros were missing. Add
those so that we are compatible with software using them.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2022-03-09 13:40:58 +01:00
Danny Oerndrup
845a200c1b libc: minimal: qsort remove callback cast in call of qsort_r
Remove the cast of the two parameter compare function used by qsort, to
the three parameter callback function used by qsort_r, in order to
ensure compatibility with other toolchains, even those off-tree.

Fixes #42870

Signed-off-by: Danny Oerndrup <daor@demant.com>
2022-03-09 06:11:40 -05:00
Daniel Leung
dc22617478 linker: add a utility func to check if an addr is in RO section
This adds a utility function to check if an address is within
read only section. This is extracted from logging subsys so
use the new func in logging. The one is cbprintf_packaged is
also replaced.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2022-02-28 10:53:02 +01:00
Nicolas Pitre
eee5b8e563 ring_buffer: make finish methods final
Make it possible to "finish" with fewer bytes than what was "claimed".

This was possible before on the get side, but the put side was
cummulative wrt finish. The revamp made it cummulative on both sides.
Turns out that existing users rely on the opposite behavior which is
more logical and useful. So make both sides that way.

Adjust documentation, test case and users accordingly.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2022-02-28 10:52:41 +01:00
Nicolas Pitre
81dbc9dbbf ring_buffer: make the normal "raw" byte mode first
The item mode is really a specialization of the byte mode. And in-tree
usage shows the byte mode is prominent. It feels more natural if the
byte mode is presented first with the item mode second. Swap the code
and documentation order accordingly. No code change.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2022-02-24 14:49:00 -08:00
Nicolas Pitre
099850e916 ring_buffer: the great simplification
This code is rather hairy. When I look at it I don't like the way it
stares back at me.

First, the rewind business looks fishy. It has to die.

And we don't have to rely on modulus either. Not even for non-power-of-2
buffers. Let's kill that distinction too and make all sizes always
"high performance".

The code is now entirely relying only on simple ALU operations (add,
sub and compare).

The key assumption: 32-bit values do wrap around after max range has
been reached. No saturation. All architectures supported by Zephyr
do that.

Some stats:

lib/os/ring_buffer.c: 62 insertions(+), 124 deletions(-)

ring_buffer.c.obj       before   after    diff
----------------------------------------------
frdm_k64f                 1224    1136     -88
m2gl025_miv               2485    2079    -406
mps2_an385                1228    1132     -96
mps2_an521                1228    1132     -96
native_posix              1546    1496     -50
native_posix_64           1598    1595      -3
nsim_hs_mpuv6             1252    1192     -60
nsim_hs_smp               1252    1192     -60
nsim_sem                  1252    1192     -60
qemu_arc_em               1324    1192    -132
qemu_arc_hs6x             1824    1620    -204
qemu_arc_hs               1252    1192     -60
qemu_cortex_a53_smp       2154    1888    -266
qemu_cortex_a53           2154    1888    -266
qemu_cortex_a9            1938    1792    -146

Before (qemu_cortex_a53):
START - test_ringbuffer_performance
1 byte put-get, avg cycles: 52
4 byte put-get, avg cycles: 47
1 byte put claim-finish, avg cycles: 39
5 byte put claim-finish, avg cycles: 41
5 byte get claim-finish, avg cycles: 52
 PASS - test_ringbuffer_performance in 0.8 seconds

After (qemu_cortex_a53):
START - test_ringbuffer_performance
1 byte put-get, avg cycles: 34
4 byte put-get, avg cycles: 41
1 byte put claim-finish, avg cycles: 27
5 byte put claim-finish, avg cycles: 29
5 byte get claim-finish, avg cycles: 29
 PASS - test_ringbuffer_performance in 0.4 seconds

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2022-02-24 14:49:00 -08:00
Nicolas Pitre
c2543320d8 ring_buffer: rationalize the item-based put and get methods
Conceptually, ring_buf_item_put() and ring_buf_item_get() are specialized
versions of ring_buf_put() and ring_buf_get(). Make it so to rationalize
the code to open the way for more optimizations.

This means we need specialized wrappers on top of ring_buf_init()
accordingly, given that the core machinery is now common and byte based.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2022-02-24 14:49:00 -08:00
Daniel Leung
660d30e870 sys: bitarray: remove set but unused variable
The mismatch_mask in match_region() is set but never actually
being used. So remove it as Clang complains.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2022-02-24 08:38:38 -06:00
Bartosz Golaszewski
6c190292c0 lvgl: move the lvgl glue out of the zephyr tree
Start using the upstream Kconfig from LVGL and move the glue code out
of the zephyr tree and put it under lvgl/zephyr/ in modules.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@huawei.com>
2022-02-24 11:51:33 +01:00
Bartosz Golaszewski
1244a89a21 lvgl: bump version to v8.1.0
This updates the lvgl in-tree glue code to work with version v8.1.0 and
bumps the west manifest accordingly.

The following are the most significant changes:
- The logging callback has changes in lvgl and no longer provides the
  caller with an integer log level code. We now need to parse the log
  string's prefix to determine the level.
- Several Kconfig options (mostly for default values of various settings)
  have been removed because these values are no longer configurable in
  lvgl.
- The library no longer performs a deep copy of the display and input
  device driver structs, so these must no longer be allocated on the
  stack in the init func.

Other than that it's mostly about renaming of various structures and
functions and adjusting the calls if function's signatures have changed.

This patch allows all in-tree users to work correctly but it's likely
it doesn't support all new widgets and layouts added in lvgl v8. These
however can be added gradually once this is upstream.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@huawei.com>
2022-02-24 11:51:33 +01:00
Bartosz Golaszewski
1e80d5a954 lvgl: provide lvgl_realloc()
In order to support lvgl v8, we need to provide a realloc() implementation
in our custom sys_heap allocator. This uses sys_heap_realloc() internally
and exposes traditional realloc() semantics.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@huawei.com>
2022-02-24 11:51:33 +01:00
Bartosz Golaszewski
b7dbe1a51e lvgl: add locking for the sys heap
The sys_heap layer doesn't provide any locking mechanism. Add a spin_lock
around the calls to sys_heap functions.

Suggested-by: Nicolas Pitre <npitre@baylibre.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@huawei.com>
2022-02-24 11:51:33 +01:00
Bartosz Golaszewski
b2f1bbed88 lvgl: mem: align the memory used for the private sys heap
Improve efficiency by introducing the 8-byte alignment of the private
heap.

Suggested-by: Nicolas Pitre <npitre@baylibre.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@huawei.com>
2022-02-24 11:51:33 +01:00
Bartosz Golaszewski
595131c2d0 lvgl: kill LVGL_MEM_POOL_HEAP_KERNEL and LVGL_MEM_POOL_KERNEL
Current lvgl code allows to use the kernel heap for dynamic memory
allocation. The k_heap API doesn't however provide k_realloc() which
will be needed in order to update lvgl to v8. Nico suggested there's
no good reason for lvgl to use k_heap and it should stick to either
the libc's allocator or depend on its own private sys_heap.

The alternative would be to extend the k_heap API to provide k_realloc()
but this may be tricky for several reasons and for now there would
be a single user anyway.

This removes the choice of using k_heap for lvgl and renames the user
pool to SYS_HEAP in Kconfig and makes it the default option.

The prj.conf for the lvgl sample is modifed to specify the number of
memory pool blocks instead of the total size as the default block
size is 2048 and it results in the same size of memory.

Suggested-by: Nicolas Pitre <npitre@baylibre.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@huawei.com>
2022-02-24 11:51:33 +01:00
Bartosz Golaszewski
e467088faa lvgl: take opacity into account
The callback used for the 32-bit color depth has a slight problem, it
doesn't take opacity into account. Correctly mix the colors by using
lv_color_mix().

Suggested-by: Gabor Kiss-Vamosi <kisvegabor@gmail.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@huawei.com>
2022-02-24 11:51:33 +01:00
Krzysztof Chruscinski
5c8cc1aa8f lib: os: mpsc_pbuf: Add usage tracking
Add API to fetch current buffer usage. Add option to track
maximum buffer usage and API to fetch that value.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-02-21 20:41:17 -05:00
Daniel Nejezchleb
b68bdf20cf lib/os: fdtable: add locking to posix api
Added locking to posix read(), write(), close()
for additional protection.

In read() missing lock would create uneven calls to locking
mechanism in sockets.c after k_condvar_wait().
That results in socket lock not ever being unlocked

Signed-off-by: Daniel Nejezchleb <dnejezchleb@hwg.cz>
2022-02-10 10:59:03 +01:00
Christopher Friedt
07c00fff25 pthread: cond: fix pthread_cond_wait always returning ETIMEDOUT
It was noted that `pthread_cond_wait()` would always return
ETIMEDOUT, even when successful (and no timeout should ever
occur with `K_FOREVER`).

The z_sched_wake() / z_sched_wake_all() / z_sched_wait() API
are used here with a swap return value of 0 to indicate
success.

Fixes #41284

Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
2022-02-09 22:16:45 -05:00
Krzysztof Chruscinski
c2666c3138 lib: os: cbprintf: Use rodata section on sparc
Use rodata region markers to detect if string is read only on
sparc. It was previously disabled but now can be used.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-02-08 09:41:37 -05:00
Carles Cufi
7f6524949a lib: os: crc: Rework the crc16() implementation
As described in #42403, there was an issue with the existing crc16_ansi()
implementation, since it was not calculating the CRC-16-ANSI (aka
CRC-16-MODBUS). This is because the  existing crc16() function only
supported non-reflected input and output (and the CRC-16-ANSI requires
reflection on both) and also it did not seem to support correctly inial
seeds different from 0x0000 (and, again, the CRC-16-ANSI requires 0xffff
as an initial seed).

This commit replaces the existing crc16() with a functional pair,
crc16() and crc16_reflect(), that also work with any poly, any initial seed
and allow to select whether reflection is performed.
It also adapts crc16_ansi() so that it actually returns the correct CRC.

Fixes #42403.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2022-02-04 12:33:22 -05:00
Daniel Leung
e2f109456c lib/os: cbprintf_packaged: add ability to use external formatter
This adds to the cbprintf_packaged library to allow external
formatters to be used by the way of callback. This will allow
logging backends to use their own formatter for output if
needed.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2022-02-04 11:14:55 +01:00
Krzysztof Chruscinski
a40ca6fd1c logging: printk: Fix LOG_PRINTK for v2
Fixed a dependency from printk.h to logging headers which in
certain configurations could lead to circular dependencies.
Cleaned up printk.c to call z_log_vprintk from vprintk.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-01-27 10:02:21 +01:00
Krzysztof Chruscinski
09fef8e7d2 lib: os: printk: Minor refactoring
Refactoring to remove code redundancy caused by splitted
handling based on USERSPACE enabled.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-01-27 10:02:21 +01:00
Binu Jacob
5fa5658ef7 libc: newlibc: Fix recursive gettimeofday() calls on non-Posix systems
Calling gettimeofday() from _gettimeofday() in a non-Posix build
environment can result in a recursive call loop, causing a stack
overflow. Modify _gettimeofday() to return -1 for non-posix systems
(the previous behaviour that was added in #22508).

Fixes #41095

Signed-off-by: Binu Jacob <bjj@planetinnovation.com.au>
2022-01-21 15:27:45 -05:00
Nicolas Pitre
019a1e13f4 cbprintf_packaged: code cleanups
- Abstract buffer offset computation for better code clarity.

- Rework the logic around rw/ro strings to simplify the logic and
  to guard against overflows even when only computing the needed buffer
  size.

- Use modulus to simplify alignment tests (generated assembly is
  the same).

- Avoid CBPRINTF_ prefixes for local macro names

- Better pointer types to reduce cast usage.

- Add more comments.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2022-01-20 08:21:33 -05:00
Antony Pavlov
0369998e61 arch: add MIPS architecture support
MIPS (Microprocessor without Interlocked Pipelined Stages) is a
instruction set architecture (ISA) developed by MIPS Computer
Systems, now MIPS Technologies.

This commit provides MIPS architecture support to Zephyr. It is
compatible with the MIPS32 Release 1 specification.

Signed-off-by: Antony Pavlov <antonynpavlov@gmail.com>
2022-01-19 13:48:21 -05:00
Andy Ross
528bef2d22 lib/os: Add sys_winstream lockless shared memory byte stream IPC
It's not uncommon to have Zephyr running in environments where it
shares a memory bus with a foreign/non-Zephyr system (both the older
Intel Quark and cAVS audio DSP systems share this property).  In those
circumstances, it would be nice to have a utility that allows an
arbitrary-sized chunk of that memory to be used as a unidirectional
buffered byte stream without requiring complicated driver support.
sys_winstream is one such abstraction.

This code is lockless, it makes no synchronization demands of the OS
or hardware beyond memory ordering[1].  It implements a simple
file/socket-style read/write API.  It produces small code and is high
performance (e.g. a read or write on Xtensa is about 60 cycles plus
one per byte copied).  It's bidirectional, with no internal Zephyr
dependencies (allowing it to be easily ported to the foreign system).
And it's quite a bit simpler (especially for the reader) than the
older cAVS trace protocol it's designed to replace.

[1] Which means that right now it won't work reliably on arm64 until
we add a memory barrier framework to Zephyr!  See notes in the code;
the locations for the barriers are present, but there's no utility to
call.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2022-01-13 14:01:23 -05:00
Daniel Leung
3feec54acf lib: os: remove @return doc for void functions
For functions returning nothing, there is no need to document
with @return, as Doxgen complains about "documented empty
return type of ...".

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2022-01-12 16:02:16 -05:00
Krzysztof Chruscinski
4433953e34 lib: os: ring_buffer: Fix issues in rewinding
Two issues were found:
- subtracting rewinding value from head could result in negative value
- calling ring_buf_put_claim after tail got rewinded but before head
got rewinded resulted in error.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-01-11 18:19:44 -05:00
Daniel Leung
f3856d0b47 lib: os: mem_blocks: add alloc/free event notifications
This adds event notification for mem_blocks using the new
heap listener interface.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2022-01-11 16:10:53 -05:00
Daniel Leung
d8f97caac2 lib: os: mem_blocks: add memory blocks allocator groups
This adds similar ability of sys_multi_heap to the memory blocks
allocator, where a choice function can be used to select
which allocator (of a group) is used for memory block allocation.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2022-01-11 16:10:53 -05:00
Daniel Leung
8308f3f4d5 lib: os: introduce memory blocks allocator
This introduces yet another memory blocks allocator where:

() All memory blocks have a single fixed size.

() Multiple blocks can be allocated or freed at the same time.

() A group of blocks allocated together may not be contiguous.
   This is useful for operations such as scatter-gather DMA
   transfers.

() Bookkeeping of allocated blocks is done outside of
   the associated buffer (unlike memory slab). This allows
   the buffer to reside in memory regions where these can be
   powered down to conserve energy.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2022-01-11 16:10:53 -05:00
Daniel Leung
5604b98f21 lib: os: group heap related kconfigs under a menu
This groups the heap related kconfigs under a menu
so they are under a group (!).

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2022-01-11 16:10:53 -05:00
Emil Gydesen
43b827c562 zephyr: Add UTF8 kconfig to conditionally compile utf8.c
Add the kconfig option so that the utf8.c file can be
conditionally compile, and only for the applications
that need it.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
2022-01-11 11:50:15 +01:00
Emil Gydesen
baf48fbe2e zephyr: Add UTF-8 truncating strlcpy variant
Add a function to copy a UTF-8 encoded string that
ensure correct truncation of the string if the source
is larger than the destination, as well as ensuring that
the resulting destination string is NULL-terminated.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
2022-01-11 11:50:15 +01:00
Emil Gydesen
5cb72d2193 zephyr: Add function to properly truncate UTF-8 strings
Add a function that can properly truncate UTF-8 strings
without leaving unterminated started characters,
as UTF-8 characters can be 1-4 bytes long.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
2022-01-11 11:50:15 +01:00
Daniel Leung
bbe03935f6 lib: os/heap: fix bytes freed calculation for heap listener
This fixes an issue of how the number of bytes freed is calculated
for the heap listener.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2022-01-10 21:27:28 -05:00
Daniel Leung
f6e2705719 sys: heap: add support for heap listener
This adds notification in the heap code to emit events for
heap listeners.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2022-01-10 10:28:04 -05:00
Daniel Leung
071db26f6c lib: os: CONFIG_HEAP_LISTENER to be hidden
This changes CONFIG_HEAP_LISTENER to be a hidden kconfig so that
the actual heap implementation can select it to enable
notifications. Each heap implementations will have their own
kconfigs to enable heap listener functionality so that app
can be built to only listen to certain heap implementations.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2022-01-10 10:28:04 -05:00
Daniel Leung
ae415272c1 sys: heap_listener: extend to cover more events
This extends the heap_listener to cover more events,
specifically, allocation, free and realloc.

Note that typedef are used so the callback can be
documented.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2022-01-10 10:28:04 -05:00
Daniel Leung
39d17a180a sys: heap_listener: resize_cb to also take heap ID
Add a parameter to the resize callback to also take the heap ID.
This allows a single callback to be used for multiple heaps if
so desired.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2022-01-10 10:28:04 -05:00
Eduardo Montoya
b2ca577256 libc: add stddef.h to the minimal libc
It is required for using `size_t`.

Signed-off-by: Eduardo Montoya <eduardo.montoya@nordicsemi.no>
2022-01-07 12:48:27 -05:00
Krzysztof Chruscinski
bd8cc594d9 lib: os: mpsc_pbuf: Add const to mpsc_pbuf_free argument
Added const qualifier to argument in a function.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-01-07 18:47:19 +01:00
Jakub Rzeszutko
d4559f53fa lib: getopt: rework and extend getopt library
Getopt has been rework in this way that calling it does not require
extra state parameter and its execution is thread safe.
Global parameters describing the state of the getopt function have been
made available to ensure full API compatibility in using this library.
However, referencing these global variables directly is not thread
safe. In order to get the state of the getopt function for the thread
that is currently using it, call: getopt_state_get();

Extended the library with getopt_long and getopt_long_only functions.

Moved getopt libary from utils to posix.

Signed-off-by: Jakub Rzeszutko <jakub.rzeszutko@nordicsemi.no>
2022-01-06 21:26:59 +01:00
Patric Karlström
20b3d180d1 posix: Make clock_settime/gettime REALTIME thread-safe
Fixes #23419

Signed-off-by: Patric Karlström <pakar@imperialnet.org>
2021-12-21 11:49:51 +01:00
Damian Krolik
3aedda9852 lib: os: add heap event listener
* add generic heap event listener module that can be used
  for notifying an application of heap-related events
* use the listener module in newlib libc hooks
* add a unit test

Signed-off-by: Damian Krolik <damian.krolik@nordicsemi.no>
2021-12-18 07:49:15 -05:00
Nicolas Pitre
40a94d2451 lib/os/heap: use BIT() and BIT_MASK() on bit fields
This is a clean way to make MISRA happy.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-12-13 17:16:07 -05:00
Jeremy Bettis
1e0a36c655 build: Remove unused functions
Removed unused functions, or moved inside #ifdefs.

This allows using -Werror=unused-function on the clang compiler. Tested
by building the ChromeOS EC on all supported platforms with
-Werror=unused-functions.

Signed-off-by: Jeremy Bettis <jbettis@google.com>
2021-12-13 15:49:08 -05:00
Johann Fischer
5228211060 lib: lvgl: obtain LVGL_DISPLAY_DEV_NAME from devicetree
It is not possible completely remove options like
LVGL_DISPLAY_DEV_NAME without heavy reworking of LVGL support
because lvgl_init needs the name of the device, and
LVGL_*_RES_MAX options to define internal buffers.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2021-12-10 12:47:30 +01:00
Gerard Marull-Paretas
78dc8ce338 drivers: timer: improve sys_timer_disable usage
- Remove the weak symbol definition
- Notify about the capability of disabling via a selected Kconfig option
  (CONFIG_SYSTEM_TIMER_HAS_DISABLE_SUPPORT)
- Provide a dummy inline function when the functionality is not
  available

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2021-12-04 07:34:53 -05:00
Ramiro Merello
b1c350dbfe json: Fix rule 5.7 violations (Tag name should be unique)
Fixed 5.7 rules errors due to:
- struct token token updated to struct token tok
- struct lexer lexer updated to struct lexer lex

Signed-off-by: Ramiro Merello <rmerello@itba.edu.ar>
2021-12-01 12:21:19 -05:00
Markus Fuchs
c6bc548b73 json: Add top-level array decoding support
The library supports encoding JSON objects and arrays as well as
parsing JSON objects. Introduce a new function json_arr_parse() adding
support for parsing top-level JSON arrays.

Signed-off-by: Markus Fuchs <markus.fuchs@ch.sauter-bc.com>
2021-12-01 12:21:19 -05:00
Nicolas Pitre
5aeb809374 lib/os/heap-validate: code cleanup
Remove code duplication, use common code idiom, etc.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-11-15 11:03:57 -05:00
Nicolas Pitre
f13387d8d2 lib/os/heap: fix a type
Rationale in commit b1eefc0c26 ("lib/os/heap: straighten up our type
usage").

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-11-15 11:03:57 -05:00
Chen Peng1
28f834c914 sys_heap: add check for sys_heap_runtime_stats_get API
add operations to check sys_heap_runtime_stats_get API
in existing sys_heap_validate function.

Signed-off-by: Chen Peng1 <peng1.chen@intel.com>
2021-11-11 16:21:43 -05:00
Chen Peng1
a71cd8790f heap: add functions to get heap runtime statistics
add functions to get the sys_heap runtime statistics,
include total free bytes, total allocated bytes.

Signed-off-by: Chen Peng1 <peng1.chen@intel.com>
2021-11-11 16:21:43 -05:00
Christopher Friedt
bd83df1552 libc: minimal: add qsort to the minimal libc
This change implements qsort() for the minimal libc via Heapsort.

Heapsort time complexity is O(n log(n)) in the best, average,
and worst cases. It is O(1) in space complexity (i.e. sorts
in-place) and is iterative rather than recursive. Heapsort is
not stable (i.e. does not preserve order of identical elements).

On cortex-m0, this implementation occupies ~240 bytes.

Fixes #28896

Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
2021-11-10 07:00:36 -05:00
Bradley Bolen
6336cb26d8 libc: minimal: Use new ZRESTRICT macro
This lets the toolchain header files determine how to use "restrict"
instead of having that decision down in the minimal libc library.

Signed-off-by: Bradley Bolen <bbolen@lexmark.com>
2021-11-05 13:29:31 +01:00
Sam Hurst
c4cdbc6062 lib: smf: Remove unused variables
In function smf_execute_ancestor_exit_actions, variables
"tmp_state" and "target_parent" are set but not used.

Twister passed:
twister -T tests/lib/smf/

Signed-off-by: Sam Hurst <sbh1187@gmail.com>
2021-10-26 17:53:32 -04:00
Nicolas Pitre
46db2b491d lib/os/heap: option for optimizing for one heap size on 32-bit systems
The "small" heap is is way sufficient for most 32-bit systems.

Let's provide the option to have only one type of heap allowing for
smaller and faster heap code due to not having a bunch of runtime
conditionals based on the heap size.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2021-10-17 07:34:51 -04:00
Carlo Caione
43cb00df08 multi_heap: Introduce shared multi-heap memory pool manager
The shared multi-heap memory pool manager uses the multi-heap allocator
to manage a set of reserved memory regions with different capabilities /
attributes (cacheable, non-cacheable, etc...) defined in the DT.

The user can request allocation from the shared pool specifying the
capability / attribute of interest for the memory (cacheable /
non-cacheable memory, etc...)

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2021-10-12 07:44:46 -04:00
Andy Ross
cf0c5e2a1c lib/os: Add sys_heap_usable_size()
Add a simple internal block size predicate to expose the internal
memory region reserved for an allocation.  The immediate use case is
cache-incoherent systems wanting to do an invalidate of freed memory,
but it might be useful for apps doing e.g. string processing to better
optimize size changes, etc...

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2021-10-06 20:20:31 -04:00
Sam Hurst
cb4785542e lib: smf: Add State Machine Framework
Add an application agnostic State Machine Framework library to
Zephyr that provides an easy way for developers to integrate
state machines into their application.

Twister passed:
twister -T tests/lib/smf/

Signed-off-by: Sam Hurst <sbh1187@gmail.com>
2021-10-04 20:33:11 -04:00
Andy Ross
85e96ff3ca lib/os: Add sys_multi_heap utility
This is a simple wrapper allowing multiple sys_heap regions to be
unified under a single allocation API.  Sometimes apps need the
ability to share multiple discontiguous regions in a single "heap", or
to have memory of different "types" be allocated heuristically based
on usage (e.g. cacheability, latency, power...).  This allows a
user-specified function to select the underlying memory to use for
each application.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
2021-10-01 20:38:35 -04:00