Let's introduce a new option so components which cannot
be used with the native libC can select it.
And at this point let's already have POSIX_API select it
instead of being used in the libC choice default.
Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
Support getting and setting POSIX environment variables.
Additionally, the thread-safe BSD variant getenv_r() is
provided.
environ, getenv(), setenv(), and unsetenv() are required by
the POSIX_SINGLE_PROCESS Option Group as detailed in
Section E.1 of IEEE-1003.1-2017.
The POSIX_SINGLE_PROCESS Option Group is required for PSE51,
PSE52, PSE53, and PSE54 conformance, and is otherwise mandatory
for any POSIX conforming system as per Section A.2.1.3 of
IEEE-1003-1.2017.
Signed-off-by: Christopher Friedt <cfriedt@meta.com>
It turns out that currently LTO is enabled only for the kernel.
This commit updates it to enable it for the whole application
and adds additional LTO exclusions required for the standard
C libraries to build and link properly.
Signed-off-by: Radosław Koppel <radoslaw.koppel@nordicsemi.no>
gmtime_r() has been in the minimal libc for years, however it was not
added to expcetions due to an overlook. In order to do this however, it
has to be moved first to the common libc area, so that it's available
to any libc that may not implement it.
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
Newlib doesn't have Zephyr support, so we need to define these functions
when the application doesn't ask for the right level of POSIX support.
Signed-off-by: Keith Packard <keithp@keithp.com>
This was necessary to get Picolibc to expose the whole Zephyr C library
API, but current versions of the SDK use a version of Picolibc with
built-in Zephyr support.
Signed-off-by: Keith Packard <keithp@keithp.com>
Since pthread_once() is both the initializer and executor of
pthread_once_t, it can have maximally two states. Since the
implementation in Zephyr previously aimed to maximize libc
compatibility, we opted to use the definition of pthread_once_t
from newlib, which is a structure with two ints.
It does not make sense to use 64 bits to manage 2 possible
states. The control for that should effectively be a bool.
We maintain compatibility with newlib by asserting (at build
time), that newlib's pthread_once_t is larger than Zephyr's
new struct pthread_once (which just contains a bool).
This allows us to delete the non-standard pthread_key.h
header file (finally).
Reuse the pthread_pool_lock in order to synchronize the related
init function (so that it is only called maximally once from any
thread). The spinlock is only used to test the state and the
init function is not called with the spinlock held.
The alternative was to use an atomic inside of
struct pthread_once. But again, that could be up to 64-bits with
Zephyr's atomics implementation.
Ideally we would use C11 generics or something to support atomics
on 8, 16, 32, and 64-bit primitives.
Lastly, also update declarations for C11 threads as they mostly
mirror our pthread implementation.
This needed to be done as a single commit in order to ensure
continuity of build.
Signed-off-by: Christopher Friedt <cfriedt@meta.com>
The string "" is of type '(const char *)', so add a cast over to
'(char *)' to clean up source code.
Signed-off-by: Florian La Roche <Florian.LaRoche@gmail.com>
Picolibc's 'minimal' printf mode reduces functionality and size even more
than the 'integer' mode. Use this where memory is at a premium and where
the application knows that it does not require exact printf semantics.
1.8.5 adds two more printf variants, 'long long' and 'minimal'. The 'long
long' variant is the same as the 'integer' variant but with long long
support enabled. The 'minimal' variant reduces functionality and size even
more than the 'integer' mode. Applications can use this where memory is at
a premium and where the application does not require exact printf
semantics.
With these two added variants, the SDK has enough options so that all of
the cbprintf modes can be supported with the pre-compiled bits:
1. CBPRINTF_NANO - picolibc's 'minimal' variant
2. CBPRINTF_REDUCED_INTEGRAL - picolibc's 'integer' variant
3. CBPRINTF_FULL_INTEGRAL - picolibc's 'long long' variant
4. CBPRINTF_FB_SUPPORT - picolibc's 'double' variant
This patch makes the cbprintf Kconfig values drive the default picolibc
variant, disables picolibc variants not capable of supporting the required
cbprintf level, but allows applications to select more functionality in
picolibc than cbprintf requires.
Note that this depends on the SDK including picolibc 1.8.5. Without that,
selecting the 'minimal' or 'long long' variant in Zephyr will end up with
the default variant from picolibc, which is the full version with floating
point support. When using the module things will work as specified.
Signed-off-by: Keith Packard <keithp@keithp.com>
This option in picolibc switches the assert macro between a chatty version
and one which provides no information at all. This latter mode avoids
placing the associated strings in memory.
The Zephyr option is PICOLIBC_ASSERT_VERBOSE and it is disable by default.
Signed-off-by: Keith Packard <keithp@keithp.com>
This moves the k_* memory management functions from sys/ into
kernel/ includes, as there are kernel public APIs. The z_*
functions are further separated into the kernel internal
header directory.
Also made a quick change to doxygen to group sys_mem_* into
the OS Memory Management group so they will appear in doc.
Signed-off-by: Daniel Leung <daniel.leung@intel.com>
This change capitalizes on newly added support for dynamic
thread stacks and the existing pthread support to provide
an implementation of the ISO C11 `<threads.h>` API.
Signed-off-by: Christopher Friedt <cfriedt@meta.com>
This prevents the compiler from optimizing calloc into an
infinite recursive call.
For example a call to malloc + memset zero at GCC -O2 will be
replaced by a call to calloc. This causes infinite recursion
if the function being implemented *is* calloc.
fno-builtin-malloc forces the compiler to avoid this optimization.
Signed-off-by: Grant Ramsay <gramsay@enphaseenergy.com>
The picolibc module can be built without thread local storage support if
desired. Allow that by using 'imply' instead of 'select'. However, when
using the toolchain picolibc, we assume that TLS will be enabled wherever
supported, so make sure we match by adding a 'select' for this case.
Signed-off-by: Keith Packard <keithp@keithp.com>
Move the syscall_handler.h header, used internally only to a dedicated
internal folder that should not be used outside of Zephyr.
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
Instead of making applications use C library specific settings to enable
floating point support in printf, provide this indirect symbol which then
detects which C library is in use and selects the correct configuration for
each.
Signed-off-by: Keith Packard <keithp@keithp.com>
When Zephyr is compiled using LLVM toolchain, we don't need to link with
libgcc to resolve libc dependencies. With this patch, the trick will be
applied only when the GNU compiler is used. Otherwise, we will just link
libc, which works for LLVM.
Signed-off-by: Patryk Duda <pdk@semihalf.com>
Defaulting to picolibc when selecting GETOPT is a bit nicer for users
in some cases, but not required.
But too many things require GETOPT (for ex. some SHELL configurations)
in combinations with native posix drivers which do not support
yet embedded libcs (for ex. the native USB driver)
(see https://github.com/zephyrproject-rtos/zephyr/issues/60096 )
Which leads to configurations in those cases which cannot be built.
Keep defaulting to the external libC in this case.
This reverts the getop part of this commit:
5f8057e262
Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
When building with the POSIX_API we cannot use the host libC.
When using GETOPT it is similarly quite difficult for users
to use the host libC.
So to make it easier for users, let's just default to PICOBLIC
in those cases,
while we continue defaulting to the host library in other cases
so users can use the Linux APIs for whatever test functionality
they want.
Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
Leave the malloc partition so that it only contains the heap itself; this
lets the initialization code adjust the address range when configuring the
arena at startup.
Signed-off-by: Keith Packard <keithp@keithp.com>
Instead of adding every possible subsystem which places variables in the C
library memory partition in libc-hooks.h, place those conditions in the
related Kconfig files and simplify the libc-hooks.h to just looking at
CONFIG_NEED_LIBC_MEM_PARTITION.
Signed-off-by: Keith Packard <keithp@keithp.com>
Picolibc dependencies limit ability to use third party minimal
implementations of CPP when enablng PICOLIBC_USE_MODULE.
Signed-off-by: Al Semjonovs <asemjonovs@google.com>
Fixes#62677 where printf defaults to using armclang's semihosting backed
implementation of printf.
Signed-off-by: Wilfried Chauveau <wilfried.chauveau@arm.com>
The original idea of z_current_get() was to be the counterpart
of k_current_get() when thread local variable for current has
not been initialized if TLS is enabled, otherwise they are
the same function. Now since z_current_get() is being used
outside of core kernel, rename it under kernel namespace so
other subsystem can conceptually use them too.
Signed-off-by: Daniel Leung <daniel.leung@intel.com>
When multithreading is disabled, the whole mutex infrastructure isn't
available. The common malloc code wasn't checking for this case which
caused build failures.
Signed-off-by: Keith Packard <keithp@keithp.com>
Tested-by: Detlev Zundel dzu@member.fsf.org
...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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>