Commit Graph

555 Commits

Author SHA1 Message Date
Keith Packard c759b8ac3d libc/picolibc: Split hooks into separate files
This splits the picolibc helper functions into separate files instead of
smashing them all together.

Signed-off-by: Keith Packard <keithp@keithp.com>
2024-11-16 13:50:16 -05:00
Jonathon Penix e020f31fdb cmake: libc: minimal: Avoid linking against other libc implementations
The minimal libc is currently built as a zephyr_library and will be
included in the final link line as such. However, the c_library property
will still be set as "-lc" (for most linkers) and will be added to the
link line. This effectively requires that a separate libc implementation
be available in the toolchain and makes it possible to accidentally pull
from the non-minimal libc.

This doesn't seem desirable, so try to prevent this by clearing the
c_library property if we are using the minimal libc.

Signed-off-by: Jonathon Penix <jpenix@quicinc.com>
2024-10-30 14:29:37 -05:00
Torsten Rasmussen 9d835fe464 cmake: move specs compiler and linker flags to toolchain properties
Moving specs argument to compiler and linker properties so that the
compiler and linker in use can decide how the flags are mapped / handled
for the compiler and linker in use.

This avoids specifying `--specs=spec.picolibc` for clang which prints a
warning about an unused argument.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
2024-10-04 16:34:35 +01:00
Torsten Rasmussen 2e3873adde cmake: improve Zephyr link phase
Zephyr is a bare metal build where standard libs are disabled.

This means that c and runtime libraries must manually be linked in.

This has generally been handled by using CMake's link libraries handling
but the issue with that is both de-duplication but also library link
order.

Standard libraries must be linked at last location to ensure symbols
are always available, however this is not optimal with
target_link_libraries() because this would ultimately require every
library to know the c library to link with, which is not desired.

Therefore, setup standard C and runtime library linking in linker
CMake files for toolchains where this is required.

This commit expands the principle introduced with toolchain abstraction,
see PR#24851.

This means that a toolchain implementation may specify standard C,
runtime, C++, etc libraries, as well as their link order.
Because a property approach is used, then Zephyr modules, such as the
Picolibc module can adjust such properties.

An optional `zephyr_linker_finalize()` macro is called at the end of
Zephyr's CMakeList process and can be used by the toolchain
implementation to define the final linker invocation.

This aligns the linker handling flow to the principle introduced in
PR#24851 and improves the flexibility and robustness of Zephyr build
system.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
2024-10-04 16:34:35 +01:00
Lars-Ove Karlsson 8fceb6421f libc: minimal: math: Removed undefined behavior in sqrt routines
Fixed a clang config warning.

Signed-off-by: Lars-Ove Karlsson <lars-ove.karlsson@iar.com>
2024-09-30 17:12:43 +01:00
Lars-Ove Karlsson 0a8e1ad972 libc: minimal: math: Removed undefined behavior in sqrt routines
The previous code used casts of address of int to float pointer.
This is undefined behavior in C and may cause a compiler to
optimize code incorrectly.

I have replaced it with a union of int and float types,
eliminating the cast problem.

Signed-off-by: Lars-Ove Karlsson <lars-ove.karlsson@iar.com>
2024-09-30 17:12:43 +01:00
Jamie McCrae 3a15e37b5a lib: libc: common: time: Add toolchain include
This file uses something that is defined in the toolchain files,
fix a build error by including that header

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
2024-09-26 13:31:03 -05:00
Keith Packard 8d176ea97d libc/picolibc: Don't replace picolibc time _r functions
Select TC_PROVIDES_POSIX_C_LANG_SUPPORT_R to keep Zephyr from
including the common libc implementation of the various _r APIs.

Signed-off-by: Keith Packard <keithp@keithp.com>
2024-09-16 20:17:35 +02:00
Keith Packard ac3df26f0a libc/common: Remove redundant Kconfig POSIX_THREAD_SAFE_FUNCTIONS uses
POSIX_THREAD_SAFE_FUNCTIONS already selects POSIX_C_LANG_SUPPORT_R and
POSIX_C_LANG_SUPPORT_R alread selects all of the COMMON_LIBC_*_R values.
That makes the 'default y if POSIX_THREAD_SAFE_FUNCTIONS' clauses in these
symbols redundant.

Signed-off-by: Keith Packard <keithp@keithp.com>
2024-09-16 20:17:35 +02:00
Keith Packard 7f412924d3 libc/common: Define _POSIX_C_SOURCE to gain access to POSIX functions
ctime.c and localtime_r_utc.c may use POSIX-only functions; to ensure those
are visible from a POSIX-conforming C library, define _POSIX_C_SOURCE in
these source files.

Signed-off-by: Keith Packard <keithp@keithp.com>
2024-09-16 20:17:35 +02:00
Alberto Escolar Piedras 6ce257cc76 lib/libc/Kconfig: Fix libC depends with NATIVE_APPlICATION
NATIVE_APPlICATION (old native_posix) does not support bulding with the
any other library than the host libC (EXTERNAL_LIBC).

MINIMAL_LIBC_SUPPORTED defaulted to y always, and instead the depends
was set in the MINIMAL_LIBC choice.
This lead to the right library selected, but an incorrectly set
MINIMAL_LIBC_SUPPORTED.
Many tests filter based on MINIMAL_LIBC_SUPPORTED, so they were
not filtered out appropriately.

Let's place the depends in the right place to avoid this problem.
Also remove a redundant depends on for PICOLIBC and NEWLIB_LIBC
which already have the dependency in their respective _SUPPORTED
option.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-09-16 13:51:50 +02:00
Pisit Sawangvonganan 6a64490c34 style: lib: comply with MISRA C:2012 Rule 15.6
Add missing braces to comply with MISRA C:2012 Rule 15.6 and
also following Zephyr's style guideline.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2024-09-11 07:40:35 -04:00
Torsten Rasmussen ce0ab067e6 cmake: cleanup newlib CMakeLists.txt file
Commit 282f77e732 removed the only place
defining LIBC_INCLUDE_DIR. Remove the corresponding use of
LIBC_INCLUDE_DIR from newlib/CMakeLists.txt as this setting is no longer
being defined anywhere.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
2024-09-04 21:26:59 +02:00
Chris Friedt 1593954083 posix: add stubs for signal.h functions that need process support
Since Zephyr itself does not currently support processes, but
conformant applications should still be able to link, add stubs
for the remaining POSIX functions in the POSIX_SIGNALS Option
Group.

The POSIX_SIGNALS Option Group is required for PSE51, PSE52,
PSE53, PSE54, and likely many other POSIX Subprofiles.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-08-23 10:35:11 -04:00
Karthikeyan Krishnasamy abf62699cb lib: libc: stdio: add support remove api
Implement remove api to zephyr's native c library

Signed-off-by: Karthikeyan Krishnasamy <karthikeyan@linumiz.com>
2024-08-22 14:26:00 -04:00
Yong Cong Sin fe94d4354a libc: common: implement multiple time functions
Implemented the following:
- `asctime_r()`
- `asctime()`
- `localtime()`
- `localtime_r()`
- `ctime()`
- `ctime_r()`

Specifically:
- the implementation of `localtime()` & `localtime_r()` simply
  wraps around the gmtime() & gmtime_r() functions, the
  results are always expressed as UTC.
- `ctime()` is equivalent to `asctime(localtime(clock))`, it
  inherits the limitation of `localtime()` as well, which only
  supports UTC results currently.

Added tests for these newly implemented functions.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
2024-08-21 16:59:15 -04:00
Dawid Niedzwiecki ff668a6bed libc: newlib: add config to use custom sbrk
Add a config to use the custom _sbrk function, defined by a user.

It is possible that an application doesn't want to use the entire
remaining RAM for the heap.

Signed-off-by: Dawid Niedzwiecki <dawidn@google.com>
2024-08-07 07:25:46 -04:00
Gerard Marull-Paretas 28a515982c lib: libc: errno: remove non-existing c_std_lib doxygen group
Some groups were being added to a non-existing group: c_std_lib

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
2024-07-18 09:23:33 -04:00
Yong Cong Sin 0155c6f87f lib/libc: minimal: remove deprecated headers
This headers have been deprecated for > 2 releases,
remove them.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-07-12 20:38:20 -04:00
Evgeniy Paltsev 89f3d220d4 tests: posix: headers: fix build error when minimal libc is used
The `tests/posix/headers/portability.posix.headers.without_posix_api`
test fails to build in case of toolchain which uses minimal libc by
default (for example ARC MWDT toolchain).

It can be easily fixed by providing sighal.h handler in minimal libc

Signed-off-by: Evgeniy Paltsev <PaltsevEvgeniy@gmail.com>
Signed-off-by: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
2024-07-12 05:49:28 -04:00
Yong Cong Sin f87539081d lib/libc: picolibc: select its source with Kconfig choice
Implement the selection of the picolibc source in a Kconfig
choice as they are mutually exclusive.

The following table represents the possible choices, where 'X'
means that `PICOLIBC_SUPPORTED=n` and is guarded in the parent
level as `PICOLIBC=n`, so we do not need to care.

Module  Toolchain    C++   |       Choice
  0         0         0    |          X
  0         0         1    |          X
  0         1         0    |    Toolchain only
  0         1         1    |    Toolchain only
  1         0         0    |      Module only
  1         0         1    |          X
  1         1         0    |   Toolchain/Module
  1         1         1    |    Toolchain only

The current implementation favors `PICOLIBC_USE_TOOLCHAIN` over
`PICOLIBC_USE_MODULE` whenever possible, that preference is
maintained in this implementation as well - the TOOLCHAIN
source will be the default choice when the toolchain supports
it or when the C++ is enabled. Otherwise, fallback to MODULE.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-07-09 17:29:12 -04:00
Yong Cong Sin 89bf4b2ad9 lib/libc: select PICOLIBC_SUPPORTED if toolchain has it or module exists
Currently, `PICOLIBC_SUPPORTED` could be enabled even when
neither the toolchain has picolibc nor the picolibc module is
present, this can be the case when Zephyr is built with
external toolchain (non-Zephyr SDK) + external build system
where PICOLIBC module doesn't exist.

Fix this by having `PICOLIBC_SUPPORTED` depends on either
toolchain OR module, while taking the libc++ into
consideration.

Here's the full logic table of when the PICOLIBC should
be marked as supported.

libc++  TOOLCHAIN       MODULE          PICOLIBC_SUPPORTED
  0         0             0                     0
  0         0             1                     1
  0         1             0                     1
  0         1             1                     1
  1         0             0                     0
  1         0             1                     0
  1         1             0                     1
  1         1             1                     1

Co-authored-by: Keith Packard <keithp@keithp.com>
Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-07-05 12:30:42 +02:00
Yong Cong Sin 7950638e53 lib/libc: picolibc: cleanup PICOLIBC_USE_TOOLCHAIN dependencies
Since `PICOLIBC_USE_TOOLCHAIN` is available only if
`PICOLIBC` is enabled, it doesn't need to depend on the latter
again.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-07-05 12:30:42 +02:00
Alberto Escolar Piedras 772ba464f7 Revert "posix: add stubs for signal.h functions that need process support"
This reverts commit b2243af32d.

PR #73978 introduced a regression.
Unfortunately this PR cannot be reverted without reverting also
Let's revert both PRs to stabilize main again towards the 3.7 release.

For more details on the issue see
https://github.com/zephyrproject-rtos/zephyr/issues/75205

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-07-03 15:03:05 -04:00
Nikolay Agishev 80fe531119 ARCMWDT: Fix issues with posix types
Fix posix pid_t type redifiniton (zephyr vs arcmwdtlib).
Add omitted in arcmwdtlib type sigevent.

This Fixes https://github.com/zephyrproject-rtos/zephyr/issues/75105

Signed-off-by: Nikolay Agishev <agishev@synopsys.com>
2024-07-01 18:14:34 +02:00
Chris Friedt b2243af32d posix: add stubs for signal.h functions that need process support
Since Zephyr itself does not currently support processes, but
conformant applications should still be able to link, add stubs
for the remaining POSIX functions in the POSIX_SIGNALS Option
Group.

The POSIX_SIGNALS Option Group is required for PSE51, PSE52,
PSE53, PSE54, and likely many other POSIX Subprofiles.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-27 15:16:50 -04:00
Maochen Wang e309f781ff libc: common: Fix init hang issue
In device init phase, it will call _mbedtls_init before malloc_prepare
as mbedtls has higher priority defined in SYS_INIT..
_mbedtls_init() will call psa_crypto_init() and malloc buffer,
but z_malloc_heap is not initialized, which will cause device hang.
Should call malloc_prepare() before _mbedtls_init to fix this issue,
so add new Kconfig to increase the priority of libc to deafult 30.

Signed-off-by: Maochen Wang <maochen.wang@nxp.com>
2024-06-26 13:07:02 -04:00
Lars-Ove Karlsson 49979fcace libc: minimal: Removed unnecessary casts
Remove two casts since the type was already the same.
Otherwise, the casts caused a warning with IAR tools.

Signed-off-by: Lars-Ove Karlsson <lars-ove.karlsson@iar.com>
2024-06-26 09:00:13 -04:00
Yong Cong Sin 19e4cb8f89 libc: cleanup deprecated heap-related Kconfigs
The following heap-related Kconfigs shared by the picolibc &
minimal libc have been deprecated for more than 2 releases,
remove them and update the Kconfigs accordingly:

- `CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE`
- `CONFIG_MINIMAL_LIBC_REALLOCARRAY`

Cleanup the handling to support the deprecated malloc arena
size configuration values.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-06-18 14:33:58 -04:00
Yong Cong Sin c22c98e510 libc: minimal: remove deprecated Kconfigs
The following Kconfigs have been deprecated for more than 2
releases, remove them:

- `CONFIG_SUPPORT_MINIMAL_LIBC`
- `CONFIG_MINIMAL_LIBC_MALLOC`
- `CONFIG_MINIMAL_LIBC_CALLOC`
- `CONFIG_MINIMAL_LIBC_REALLOCARRAY`

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-06-18 14:33:58 -04:00
Jonathon Penix d6041d62b3 cmake: kconfig: Add new abstraction for -Oz optimization level
Both Clang [1] and (recently) GCC [2] support this flag to enable
additional codesize optimizations beyond -Os, possibly at the expense of
performance.

This tradeoff is worthwhile for some (and, Clang's -Oz seems to be
closer to GCC's -Os currently), so add a new abstraction for this flag
so users can select it as appropriate.

[1] https://clang.llvm.org/docs/CommandGuide/clang.html#cmdoption-O0
[2] https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-Oz

Signed-off-by: Jonathon Penix <jpenix@quicinc.com>
2024-06-14 19:05:50 -04:00
Hess Nathan 5120744c8d lib: added blanks between operators
To adhere to general code style, I added some
blanks around operators

Signed-off-by: Hess Nathan <nhess@baumer.com>
2024-06-13 20:28:23 -04:00
Hess Nathan 974bad6242 coding guidelines: comply with MISRA Rule 12.1.
-added parentheses verifying lack of ambiguities

Signed-off-by: Hess Nathan <nhess@baumer.com>
2024-06-13 20:28:23 -04:00
frei tycho 77a3aec34e lib: added missing parenthesis
- added missing parenthesis around macro argument expansion

Signed-off-by: frei tycho <tfrei@baumer.com>
2024-06-13 20:21:08 -04:00
frei tycho 44782fd8af lib: change controlling expressions in if/while to Boolean
Use `do { ... } while (false)' instead of `do { ... } while (0)'.
Use comparisons with zero instead of implicitly testing integers.
Use comparisons with NULL instead of implicitly testing pointers.
Use comparisons with NUL instead of implicitly testing plain chars.
Use `bool' instead of `int' to represent Boolean values.
Use `while (true)' instead of `while (1)' to express infinite loops.

Signed-off-by: frei tycho <tfrei@baumer.com>
2024-06-11 20:03:34 +03:00
Chris Friedt b051bcee25 posix: move getpid() to Kconfig.procN and doc MULTI_PROCESS
Create a Kconfig option for POSIX_MULTI_PROCESS Option Group
and mark it as experimental.

Currently, Zephyr has two functions that belong to this
Option Group:
- sleep()
- getpid()

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-04 16:27:12 -05:00
Chris Friedt 3f29b17bd1 posix: deprecate POSIX_SIGNAL in favour of POSIX_SIGNALS
This change deprecates CONFIG_POSIX_SIGNAL in favour of
the plural CONFIG_POSIX_SIGNALS, which maps directly to the
name of the standard POSIX Option Group POSIX_SIGNALS.

Additionally, mark signals as experimental.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-04 16:27:12 -05:00
Chris Friedt 9f6590c7b7 posix: deprecate POSIX_FS for POSIX_FILE_SYSTEM
This change deprecates CONFIG_POSIX_FS in favour of
CONFIG_POSIX_FILE_SYSTEM, which maps directly to the name of the
standard POSIX Option Group.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-04 16:27:12 -05:00
Chris Friedt bc4374b5fe posix: deprecate POSIX_MAX_FDS and add POSIX_DEVICE_IO
The POSIX_MAX_FDS option does not correspond to any standard
POSIX option. It was used to define the size of the file
descriptor table, which is by no means exclusively used by
POSIX (also net, fs, ...).

POSIX_MAX_FDS is being deprecated in order to ensure that
Zephyr's POSIX Kconfig variables correspond to those defined in
the specification, as of IEEE 1003.1-2017. Namely,
POSIX_OPEN_MAX. CONFIG_POSIX_MAX_OPEN_FILES is being deprecated
for the same reason.

To mitigate any possible layering violations, that option is
not user selectable. It tracks the newly added
CONFIG_ZVFS_OPEN_MAX option, which is native to Zephyr.

With this deprecation, we introduce the following Kconfig
options that map directly to standard POSIX Option Groups by
simply removing "CONFIG_":

* CONFIG_POSIX_DEVICE_IO

Similarly, with this deprecation, we introduce the following
Kconfig options that map directly to standard POSIX Options by
simply removing "CONFIG":

* CONFIG_POSIX_OPEN_MAX

In order to maintain parity with the current feature set, we
introduce the following Kconfig options.

* CONFIG_POSIX_DEVICE_IO_ALIAS_CLOSE
* CONFIG_POSIX_DEVICE_IO_ALIAS_OPEN
* CONFIG_POSIX_DEVICE_IO_ALIAS_READ
* CONFIG_POSIX_DEVICE_IO_ALIAS_WRITE

Gate open(), close(), read(), and write() via the
CONFIG_POSIX_DEVICE_IO Kconfig option and move
implementations into device_io.c, to be conformant with the
spec.

Lastly, stage function names for upcoming ZVFS work, to be
completed as part of the LTSv3 Roadmap (e.g. zvfs_open(), ..).

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-04 16:27:12 -05:00
Chris Friedt 3f469665f4 posix: fd_mgmt: add fd_mgmt option group
With this change, we collect implementations of functions that are
part of the POSIX_FD_MGMT Option Group into one compilation unit
(fd_mgmt.c) and provide a matching Kconfig symbol that is
reflective of the standard.

Additionally, provide Kconfig options for aliases of those functions
since Newlib-based toolchains internally use an underscore-prefixed
alias for many POSIX functions (in this case _lseek).

Prefix Zephyr-native implementations with zvfs_ to prepare for the
up-coming changes in that area.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-04 16:27:12 -05:00
Chris Friedt 4a5c4e5f73 posix: timers: deprecate CONFIG_POSIX_CLOCK and TIMER
The POSIX_CLOCK option does not correspond to any standard
option. It was used to active features of several distinct
POSIX Options and Option Groups, which complicated API and
application configuration as a result.

POSIX_CLOCK is being deprecated in order to ensure that Zephyr's
POSIX Kconfig variables correspond to those defined in the
specification, as of IEEE 1003.1-2017.

Additionally, CONFIG_TIMER is being deprecated because it does
not match the corresponding POSIX Option (_POSIX_TIMERS).

With this deprecation, we introduce the following Kconfig
options that map directly to standard POSIX Option Groups by
simply removing "CONFIG_":

* CONFIG_POSIX_TIMERS

Similarly, we introduce the following Kconfig options that
map directly to standard POSIX Options by simply removing
"CONFIG":

* CONFIG_POSIX_CLOCK_SELECTION
* CONFIG_POSIX_CPUTIME
* CONFIG_POSIX_DELAYTIMER_MAX
* CONFIG_POSIX_MONOTONIC_CLOCK
* CONFIG_POSIX_TIMEOUTS
* CONFIG_POSIX_TIMER_MAX

In order to maintain parity with the current feature set, we
introduce the following Kconfig options that map directly to
standard POSIX Option Groups by simply removing "CONFIG_":

* CONFIG_POSIX_MULTI_PROCESS - sleep()

Similarly, in order to maintain parity with the current feature
set, we introduce the following additional Kconfig options that
map directly to standard POSIX Options by simply removing
"CONFIG":

* CONFIG_XSI_SINGLE_PROCESS - gettimeofday()

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-04 16:27:12 -05:00
Yong Cong Sin bbe5e1e6eb build: namespace the generated headers with `zephyr/`
Namespaced the generated headers with `zephyr` to prevent
potential conflict with other headers.

Introduce a temporary Kconfig `LEGACY_GENERATED_INCLUDE_PATH`
that is enabled by default. This allows the developers to
continue the use of the old include paths for the time being
until it is deprecated and eventually removed. The Kconfig will
generate a build-time warning message, similar to the
`CONFIG_TIMER_RANDOM_GENERATOR`.

Updated the includes path of in-tree sources accordingly.

Most of the changes here are scripted, check the PR for more
info.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-05-28 22:03:55 +02:00
Patryk Duda 759dcfe18f llvm: stdint: Don't define integer constant macros in llvm.h
Zephyr minimal libc implements integer constant macros using internal
headers provided by GCC, for example INT64_C(x) is just a __INT64_C(x)
which is implemented by the GCC.

Clang compiler doesn't provide these definitions, so we define them
(__INT64_C() and INT64_C()) in zephyr/toolchain/llvm.h, but it looks
like INT64_C() definition doesn't come from llvm.h, but from stdlib.h
despite of checking if __GNUC__ is defined.

This is because Clang sometimes pretends to be GNU compiler:
$ x86_64-pc-linux-gnu-clang -dM -E - < /dev/null | grep "GNU"
 #define __GNUC_MINOR__ 2
 #define __GNUC_PATCHLEVEL__ 1
 #define __GNUC_STDC_INLINE__ 1
 #define __GNUC__ 4

Let's keep the integer constant macros in stdlib.h and their
actual implementation in llvm.h. Also define these macros if __clang__
is defined, just in case.

Signed-off-by: Patryk Duda <patrykd@google.com>
2024-05-20 18:04:54 +02:00
David Schneider 012c9a2847 libc/picolibc: Support Picolibc for toolchains without bundled Picolibc
Allow Picolibc to get build from module, if the toolchain does not
include a bundled picolibc.
This should always be possible, except for native applications and
if libcpp is required.

Signed-off-by: David Schneider <david.schneider@chargepoint.com>
2024-05-09 10:28:35 +02:00
Patryk Duda 60d03c53e1 libc: minimal: Add 'noreturn' attribute to abort() and exit()
This aligns abort() and exit() definitions with other libc.

Without 'noreturn' attribute, compilers have to assume that we will
return from these functions which can lead to surprising errors like
'error: non-void function does not return a value'.

Signed-off-by: Patryk Duda <patrykd@google.com>
2024-05-06 17:32:17 +01:00
Alberto Escolar Piedras 60d5ce452e libC kconfig: Introduce a new option to avoid the native libC
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>
2024-04-24 09:53:36 +02:00
Christopher Friedt 0f23818153 posix: env: support for environ, getenv(), setenv(), unsetenv()
Support getting and setting POSIX environment variables.

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

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

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

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-03-08 04:28:47 -05:00
Radoslaw Koppel 118f1592ff buildsystem: Enable LTO also for the application
It turns out that currently LTO is enabled only for the kernel.
This commit updates it to enable it for the whole application
and adds additional LTO exclusions required for the standard
C libraries to build and link properly.

Signed-off-by: Radosław Koppel <radoslaw.koppel@nordicsemi.no>
2024-03-02 15:39:36 +01:00
Robert Lubos 0966be01fc libc: Move gmtime_r into common
gmtime_r() has been in the minimal libc for years, however it was not
added to expcetions due to an overlook. In order to do this however, it
has to be moved first to the common libc area, so that it's available
to any libc that may not implement it.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-02-28 00:54:42 +09:00
James Ogier ffa49a8240 libc: newlib: Grant access to dynamic locks from all threads
Fixes: zephyrproject-rtos#67504

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

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

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

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

extern struct k_mem_partition z_libc_partition;
extern struct k_mem_partition z_malloc_partition;

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

static int forbidden_global_data = 2;

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

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

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

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

   return 0;
}
```

Signed-off-by: James Ogier <jogier@meta.com>
2024-02-07 15:22:38 +00:00