Commit graph

1871 commits

Author SHA1 Message Date
Ederson de Souza
d83a649a67 lib/posix: Get uptime in ticks instead of cycles
__z_clock_nanosleep function was getting current time in cycles, via
k_cycle_get_32(), to perform its time calculations. However, when calling
k_sleep() to actually sleep, times are measured in ticks.

This causes a problem when there's a big skew between the uptime
measured in cycles vs uptime measured in ticks: in some platforms, the
system clock maybe up for a long time already when Zephyr starts
counting ticks, for instance, while downloading an image via PXE. In
this case, the calculations done inside __z_clock_nanosleep end up
measuring a much bigger current time than expected, thus sleeping too
much, basically all the time since system clock initialization.

This patch fixes that by avoiding the cycle trip: stick to ticks,
instead. They start counting from Zephyr initialization instead, which
is the expected uptime.

Fixes #69608

Signed-off-by: Ederson de Souza <ederson.desouza@intel.com>
2024-06-28 21:01:54 -04:00
Nikolay Agishev
5d92abeaec ARCMWDT: Fix cbprintf issue with omitted function prototype
MWDT requires function to be declared with argument types.
This PR provides explisitly type cast for out routine
inside z_cbvprintf_impl().

This PR also actual to LLVM-based compilers with strict
rules of compilation. It covers warnings generated with
additional flag -Wincompatible-function-pointer-types-strict.

This fixes https://github.com/zephyrproject-rtos/zephyr/issues/74562

Signed-off-by: Nikolay Agishev <agishev@synopsys.com>
2024-06-28 07:21:11 -04:00
Chris Friedt
b18cad15b9 posix: fd_mgmt: implement dup(), dup2(), fseeko(), and ftello()
Implement the remaining functions from the POSIX_FD_MGMT Option
Group that are part of POSIX, and add the
CONFIG_REQUIRES_FULL_LIBC dependency to CONFIG_POSIX_FD_MGMT, to
pull in the remaining C89 functions.

The POSIX_FD_MGMT Option Group is required for PSE52, PSE53, and
PSE54 Subprofiling Option Groups.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-27 15:17:56 -04: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
Chris Friedt
be086f174c posix: procN: add missing alias for getpid()
Newlib requires an alias for the getpid() function. There was
a Kconfig already present for doing so, but the actual alias
was missing.

So this is technically a bugfix.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-27 15:16:50 -04:00
Chris Friedt
b82b5b0734 posix: kconfig: remove select y from non-user-selectable help
Several help prompts for non-user-selectable Kconfig options
included the phrase "select 'y' here", which does not make
any sense in this situation.

Adjust the help sections to use more appropriate language.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-27 09:01:23 -04:00
Chris Friedt
499a633976 posix: device_io: use mode argument correctly in open()
Previously, we had only used the flags field and ignored mode
with the open() function.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-27 09:01:23 -04:00
Chris Friedt
48dff5562c posix: device_io: implement fileno()
Implement fileno() as required by the POSIX_DEVICE_IO Option
Group.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-27 09:01:23 -04:00
Chris Friedt
581a0f56e6 posix: device_io: implement fdopen()
Implement fdopen(), as required by the POSIX_DEVICE_IO Option
Group.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-27 09:01:23 -04:00
Chris Friedt
305ec62a6b posix: device_io: implement pselect()
Implement pselect() as it's required by POSIX_DEVICE_IO

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-27 09:01:23 -04:00
Chris Friedt
49ac1912b2 net: sockets: move select() implementation to zvfs
Move the implementation of zsock_select() to zvfs_select(). This
allows other types of file descriptors to also make use of
select() functionality even when the network subsystem is not
enabled.

Additionally, it partially removes a dependency cycle between
posix and networking by moving functionality into a mutual
dependency.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-27 09:01:23 -04:00
Chris Friedt
93973e2ead net: sockets: move poll implementation to zvfs
Move the implementation of zsock_poll to zvfs_poll. This allows
other types of file descriptors to also make use of poll()
functionality even when the network subsystem is not enabled.

Additionally, it partially removes a dependency cycle between
posix and networking by moving functionality into a mutual
dependency.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-27 09:01:23 -04:00
Chris Friedt
2d72966516 posix: device_io: implement pread() and pwrite()
Add pread() and pwrite() implementations, which are nearly
identical to read() and write() but differ in that they do not
update the file-descriptor offset and instead read from a
specific file offset.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-27 09:01:23 -04:00
Chris Friedt
86b92934cc posix: device_io: provide stdin, stdout, stderr variables
Create a compatibility Kconfig option for libc's that provide
their own stdin, stdout, stderr variables.

These are POSIX variables, so eventually we may want to
consider defaulting this option to 'y' and maybe providing
some zvfs abstraction for them.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-27 09:01:23 -04:00
Chris Friedt
6f62292d42 posix: device_io: require a full libc for c89 functions
The POSIX_DEVICE_IO Option Group requires a number of c89
functions mainly from stdio.h .

Namely,

clearerr(), fclose(), feof(), ferror(), fflush(), fetc(),
fgets(), fprintf(), fputc(), fputs(), fread(), freopen(),
fscanf(), fwrite(), getc(), getchar(), gets(), perror(),
printf(), putc(), putchar(), puts(), scanf(), setbuf(),
setvbuf(), ungetc(), vfprintf(), vfscanf(), vfprintf(), and
vscanf().

These should be provided by any conformant C library
(not by the POSIX API).

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-27 09:01:23 -04:00
Chris Friedt
a9a909c558 fdtable: read, write, close: only execute methods if non-NULL
Only invoke vtable methods read, write, and close if they are
non-NULL.

The close() vtable method is optional, so that should not return
an error if zvfs_close() is called and that method is
unimplemented.

Otherwise, if zvfs_read() or zvfs_write() are called and the
corresponding vtable method is unimplemented, fail setting
errno to EIO.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-27 09:01:23 -04:00
Emil Gydesen
4cf00a6125 zephyr: Add zero-len check for utf8_trunc
The function did not check if the provided string had a zero
length before starting to truncate, which meant that last_byte_p
could possible have pointed to the value before the string.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
2024-06-27 08:48:31 -04:00
Chris Friedt
fa9e93382c posix: allow for external implementation of option groups
Make it easier for external C libraries, toolchains, and
integrators to override Zephyr's implementation of functions and
sybmols on a per-Option-Group basis.

This change adds a number of non-user-configurable Kconfig
options that block internal Cmake rules from building Zephyr's
C sources corresponding to the particular option.

This is useful, for example, if a specific C library has a smaller,
or faster, or more secure  version of some symbols belonging to a
a particular option group.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-26 13:08:21 -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
Paul He
88d27ccf62 posix: pthread: correct log info of new stack address
When a new stack is allocated successfully in pthread_attr_setstacksize,
the new address should be printed not the original one.

Signed-off-by: Paul He <pawpawhe@gmail.com>
2024-06-26 13:06:28 -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
Chris Friedt
9ada52f060 fdtable: replace z_ prefix with zvfs_ for fdtable.h functions
For each of the fdtable.h functions listed below, convert the
z_ prefixed semi-private functions to use the zvfs_ prefix.

ZVFS stands for Zephyr Virtual File System and
is intended to be a common library used by the C library,
POSIX API, Networking, Filesystem, and other areas.

There are already a few functions in fdtable.h that use the
zvfs_ prefix, so this change is mostly about unifying them in
a way that uses a suitable prefix ("namespace") so that it can
be considered a public API.

- z_alloc_fd
- z_fdtable_call_ioctl
- z_finalize_fd
- z_finalize_typed_fd
- z_free_fd
- z_get_fd_obj
- z_get_fd_obj_and_vtable
- z_get_obj_lock_and_cond
- z_reserve_fd

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-26 12:33:02 +02:00
Alberto Escolar Piedras
6e977ae2d5 lib c/cpp: Move .ctor .init_array handling from C++ to kernel
* Move ctors and init_array from the CPP library
  to the kernel library, as this is common for both C
  and C++ and it is the kernel who is running it.
* Rename the hidden kconfig option CPP_STATIC_INIT_GNU
  STATIC_INIT_GNU instead.
* If STATIC_INIT_GNU is not selected verify there is
  constructors left behind.
* Rename common-rom-cpp.ld to common-rom-init.ld
* Rename z_cpp_init_static to z_init_static,
  and have the kernel always call it.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
Signed-off-by: Keith Packard <keithp@keithp.com>
2024-06-25 19:14:37 -04:00
Yong Cong Sin
9a8d1360cb posix: signal: use the provided argument in the macros
The `SIGNO_WORD_IDX` & `SIGNO_WORD_BIT` macros should have
used its own argument `_signo` instead of `signo`. It didn't
cause and error because the function's argument has `signo`.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-06-25 19:13:30 -04:00
Glenn Andrews
87db81e5cd Lib: SMF: fix stuck handled bit when transitioning
If both `smf_set_state()` and `smf_set_handled()` are called in
a run action, the `internal->handled` bit would not be reset.

This could cause an issue on the next run action by not
propagating events to parents.

Signed-off-by: Glenn Andrews <glenn.andrews.42@gmail.com>
2024-06-24 14:50:00 -04:00
Jordan Yates
07870934e3 everywhere: replace double words
Treewide search and replace on a range of double word combinations:
    * `the the`
    * `to to`
    * `if if`
    * `that that`
    * `on on`
    * `is is`
    * `from from`

Signed-off-by: Jordan Yates <jordan@embeint.com>
2024-06-22 05:40:22 -04:00
frei tycho
f17a67fee4 lib: avoided increments/decrements with side effects
- moved ++/-- before or after the value use

Signed-off-by: frei tycho <tfrei@baumer.com>
2024-06-21 08:48:15 -04:00
Yong Cong Sin
02a47cecb9 fdtable: lseek's returned offset should start from 0
An offset of `>= 0` is valid from a `lseek` call, fix it.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-06-18 19:56:13 -04:00
Yong Cong Sin
3ecef55179 posix: shm: offset should compare with the shm size
The read/write offset should have compared with the size of
the shm object, not the size of the buffer.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-06-18 19:56: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
Yong Cong Sin
67578faefe lib: cpp: remove deprecated Kconfigs
These Kconfigs should have been deprecated for more than 2
releases, remove them:

- `CONFIG_CPP_MAIN`
- `CONFIG_CPLUSPLUS`
- `CONFIG_LIB_CPLUSPLUS`
- `CONFIG_EXCEPTIONS`
- `CONFIG_RTTI`

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-06-18 14:33:58 -04:00
Hess Nathan
fe06ffb37f coding guidelines: comply with MISRA Rule 13.4
- avoid to use assignment expression value

Signed-off-by: Hess Nathan <nhess@baumer.com>
2024-06-15 05:17:26 -04:00
Jordan Yates
cc328a7fbf lib: os: reboot: dump coverage info on sys_reboot
If `sys_reboot` is called during testing, the standard dump when `main`
returns will never be executed. Failing to dump at this point means any
coverage information gathered will be lost upon reboot.

Signed-off-by: Jordan Yates <jordan@embeint.com>
2024-06-15 04:39:53 -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
Chris Friedt
2d4346ba53 posix: implement mprotect()
Provide a stub for mprotect() to satisfy the requirement for the
base definitions / system interfaces and subsequently PSE51,
PSE52, PSE52, etc.

Currently, Zephyr's virtual memory-management API does not seem
to support modifying memory protection bits after pages have
already been mapped.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-14 14:01:05 -04:00
Chris Friedt
64f336c251 posix: tie page size to mmu page size
Make POSIX_PAGE_SIZE_BITS not user-configurable and tie it to
CONFIG_MMU_PAGE_SIZE if there is an MMU. Otherwise, simply
default it to something small.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-14 14:01:05 -04:00
Chris Friedt
5346306261 posix: add support for shared memory objects
Implement shm_open() and shm_unlink() to complete support for
the _POSIX_SHARED_MEMORY_OBJECTS Option.

Since mmap() support is not yet implemented in Zephyr, I/O is
limited to read(), write(), ftruncate(), lseek(), close(), for now.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-14 14:01:05 -04:00
Chris Friedt
58c1c48df0 fdtable: per-fd offset support, add read/write_offs() methods
File offset actually varies on a per-file-descriptor basis,
and not with the resource that is abstracted behind the file
descriptor.

This is consistent with both the POSIX model and also the
ISO C/C++ model, so Zephyr should follow suit.

This is very work-around-y, but it's necessary to ensure
that shared memory objects, block devices, files and
directories all behave consistently.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-14 14:01:05 -04:00
Chris Friedt
2710903014 os: fdtable: add mode field for fd_entry
Add a mode field for struct fd_entry, as well as a new
initializer, z_finalize_typed_fd().

The constants ZVFS_MODE_* may be used to differentiate
between fifo, character device, message queues, directories,
semaphores, block devices, shared memory objects, regular files,
symbolic links, and sockets.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-14 14:01:05 -04:00
Chris Friedt
5e99e13b4c posix: add support for mlockall() and munlockall()
Add support for mlockall() and munlockall(). These two functions
comprise the _POSIX_MEMLOCK Option which is required by PSE51,
PSE52, PSE53, and PSE54.

Zephyr's on-demand paging API does not yet support pinning and
unpinning *all* virtual memory regions, so these functions are
expected to fail, setting errno to ENOSYS. Any other usage is
currently categorized as undefined behaviour.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-14 14:01:05 -04:00
Chris Friedt
d949c9c8b8 posix: add support for mlock() and munlock()
Add support for mlock() and munlock(). These two functions
comprise the _POSIX_MEMLOCK_RANGE Option which is
required by PSE51, PSE52, PSE53, and PSE54.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-14 14:01:05 -04:00
Chris Friedt
73394a8390 posix: add implementation of mmap(), msync(), and munmap()
Add stubs for mmap(), msync(), and munmap() as required by the
_POSIX_MAPPED_FILES Option and POSIX_MAPPED_FILES and
Option Group of IEEE 1003.1-2017.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-14 14:01:05 -04:00
Chris Friedt
156eaab70e posix: move ioctl() from fdtable to posix
The ioctl() call should be a part of the _XOPEN_STREAMS
Option in posix, so move it there.

Create a zephyr-native zvfs_ioctl() in the layer below.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-14 14:01:05 -04:00
Chris Friedt
50fc010f0f posix + fdtable: move zvfs_ftruncate() to fdtable.c
Move the zvfs_ftruncate() call from fs.c to fdtable.c, as file
types other than regular files can also be truncated.

Instead of hard-wiring zvfs_ftruncate() to fs_truncate(),
add a new ZVFS_IOCTL_TRUNCATE so that the operation can be
handled with other ioctl() operations.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-14 14:01:05 -04:00
Chris Friedt
1f9ca63b93 posix: fs: implement fstat() via zvfs_fstat()
Route the fstat() call (part of POSIX_FILE_SYSTEM) to
zvfs_fstat() so that other types of file descriptors can also
supply file status information.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-06-14 14:01:05 -04:00
TaiJu Wu
a2e97405d0 lib: check null pointer before taking lock for bitarray
In some function in bitarray.c, we should check the pointer
of bitarry before taking bitarray lock.

In this patch, we move the null-check assert before the use of
the struct to provide better validation than a crash.

Signed-off-by: TaiJu Wu <tjwu1217@gmail.com>
2024-06-14 19:18:28 +02: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
Hess Nathan
7fb4a748b5 coding guidelines: comply with MISRA Rule 21.15
- made explicit the copied data type

Signed-off-by: Hess Nathan <nhess@baumer.com>
2024-06-13 20:27:19 -04:00