Commit graph

3231 commits

Author SHA1 Message Date
Al Semjonovs
911b3da139 kernel: Clean-up lingering code coverage exclusion flag
Remove LCOV_EXCL_STOP flag as LCOV_EXCL_START was removed in a previous
commit.  This causes a gcov compilation error.

Signed-off-by: Al Semjonovs <asemjonovs@google.com>
2025-09-12 08:21:21 +01:00
Marcin Szkudlinski
91d17f6931 kernel: add k_thread_absolute_deadline_set call
k_thread_absolute_deadline_set is simiar to existing
k_thread_deadline_set. Diffrence is that k_thread_deadline_set
takes a deadline as a time delta from the current time,
k_thread_absolute_deadline_set is expecting a timestamp
in the same units used by k_cycle_get_32().

This allows to calculate deadlines for several thread and
set them in deterministic way, using a common timestamp as
a "now" time base.

Signed-off-by: Marcin Szkudlinski <marcin.szkudlinski@intel.com>
2025-09-11 14:18:16 +01:00
Anas Nashif
f5d7081710 kernel: do not include ksched.h in subsys/soc code
Do not directly include and use APIs from ksched.h outside of the
kernel. For now do this using more suitable (ipi.h and
kernel_internal.h) internal APIs until more cleanup is done.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-09-09 11:45:06 +02:00
Anas Nashif
6b46c826aa arch: init: z_bss_zero -> arch_bss_zero
Do not use private API prefix and move to architecture interface as
those functions are primarily used across arches and can be defined by
the architecture.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-09-08 15:51:07 -04:00
Anas Nashif
d98184c8cb arch: boot: rename z_early_memcpy -> arch_early_memcpy
Do not use private API prefix and move to architecture interface as
those functions are primarily used across arches and can be defined by
the architecture.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-09-08 15:51:07 -04:00
Anas Nashif
06b179233e kernel: use cmake macro for adding kernel files
simplify cmake file and use macros for adding files that are part of the
kernel based on the configuration.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-09-08 15:51:07 -04:00
Anas Nashif
e39de0e257 device: move device syscalls to device.c
Move device model syscalls to device.c and decouple kernel header from
device related routines. Cleanup init to have only what is needed.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-09-08 15:51:07 -04:00
Anas Nashif
7aa3269a3f kernel: boot args kconfig cleanup
Cleanup kconfig of bootargs and put everything in one menuconfig.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-09-08 15:51:07 -04:00
Anas Nashif
c9269b9b85 kernel: init: move boot arg handling to own file
No reason for this to be part of already packed init.c.
Moved to own file and build only when BOOTARGS are enabled.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-09-08 15:51:07 -04:00
Anas Nashif
53a51b9287 kernel/arch: Move early init/boot code out of init/kernel headers
Cleanup init.c code and move early boot code into arch/ and make it
accessible outside of the boot process/kernel.

All of this code is not related to the 'kernel' and is mostly used
within the architecture boot / setup process.

The way it was done, some soc code was including kernel_internal.h
directly, which shouldn't be done.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-09-08 15:51:07 -04:00
Anas Nashif
cf6db903e1 kernel: move xip into arch/common
Not really a kernel feature, more for architecture, which is reflected
in how XIP is enabled and tested. Move it to architecture code to keep
which much of the 'implementation' and usage is.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-09-08 15:51:07 -04:00
Charles Hardin
81283c678a kernel: event api extensions to clear events and avoid phantom events
This is variation of the PR to handle phantom events and hopefully
this get merged into the PR to land.

See-also: https://github.com/zephyrproject-rtos/zephyr/pull/89624
Signed-off-by: Charles Hardin <ckhardin@gmail.com>
2025-09-05 16:50:28 -04:00
Anas Nashif
0c84cc5bc6 kernel: drop deprecated pipe API
This API was deprecated in 4.1, so drop it for the 4.3 release. Use new
PIPE API instead.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-09-05 17:13:05 +02:00
Loic Domaigne
6b61ec9d9b kernel: fix error propagation for device deferred initialization
This fix makes sure that do_device_init() returns a negative value if
the device's initialization failed. Previously, it mistakely returned
+errno instead of -errno.

This oversight happened during the refactoring of z_sys_init_run_level()
to support deferred initialization, from which most of do_device_init()
code derives. The rc value computed and stored in dev->state->init_res
is the POSITIVE value of the resulting errno. Returning rc therefore
breaks the convention of a negative value to signal failure.

Signed-off-by: Loic Domaigne <tech@domaigne.com>
2025-09-04 21:03:01 +02:00
Ederson de Souza
cdaca274a5 kernel: Automatically set up HW shadow stack for thread stacks
This patch modifies thread stack macros (such as K_KERNEL_STACK_DECLARE
or K_KERNEL_STACK_ARRAY_DECLARE) to also create a HW shadow stack (when
CONFIG_HW_SHADOW_STACK=y), as well as define a pairing between the
thread stack (or thread stack array) and the shadow stack (or shadow
stack array).

This pairing, which currently is simply an array of pairs (stack,
shadow_stack) is searched during thread setup to find the corresponding
shadow stack and attach it to the thread. If linear search on this array
proves to be a performance issue, the actual structure can be revisited.

To define the size of the shadow stack for a given stack, the stack size
is used. A new Kconfig, CONFIG_HW_SHADOW_STACK_PERCENTAGE_SIZE is used
to define how big the shadow stack is compared to the stack. Note that
this size is in *addition* to the stack size. To avoid some shadow
stacks becoming too small, CONFIG_HW_SHADOW_STACK_MIN_SIZE is used to
define a minimum size. Note that after this size is defined, platform
restrictions on the size of the shadow stack are still applied.

Signed-off-by: Ederson de Souza <ederson.desouza@intel.com>
2025-09-02 07:56:48 +02:00
Ederson de Souza
10d6ef0ea5 kernel: Option to allow shadow stack to be reused
It seems that, at least on tests, it's common to call k_thread_create()
on a thread multiple times. This trips a check for the CET shadow stack
- namely, set a shadow stack on a thread which already has a shadow
stack.

This patch adds a Kconfig option to allow that, iff the base address and
size of the new shadow stack are the same as before. This will trigger a
reset of the shadow stack, so it can be reused.

It may be the case that this behaviour (reusing threads) is more common
than only for tests, in which case it could make sense to change the
default - in this patch, is only true if ZTEST.

Even if being enabled by default becomes the reality, it would still
make sense to keep this option - more conscious apps could avoid the
need for the shadow stack reset code altogether.

Signed-off-by: Ederson de Souza <ederson.desouza@intel.com>
2025-09-02 07:56:48 +02:00
Ederson de Souza
016eaf2959 kernel: Add interface for hardware shadow stack usage
In order to allow kernel created threads (such as main and idle threads)
to make use of hardware shadow stack implementation, add an interface
for them.

This patch basically provides an infra that architectures need to
implement to provide hardware shadow stack.

Also, main and idle threads are updated to make use of this interface
(if hardware shadow stacks are enabled).

Signed-off-by: Ederson de Souza <ederson.desouza@intel.com>
2025-09-02 07:56:48 +02:00
Pieter De Gendt
294f7e52c5 linker: Split nocache memory sections into loadable and non-loadable
Commit f9168ae464 made all non-cached memory
loadable by default.

However as nocache memory is typically used for reserving larger buffers to
be shared between peripherals, this comes at fairly large cost towards ROM
usage.

This commit creates two distinct sections for both loadable and
non-loadable nocache memory sections.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
2025-09-01 23:28:28 +02:00
Peter Mitsis
0111cc9030 kernel: Execute function on another CPU
Adds infrastructure to allow the execution of a function on
a different CPU. The function to be executed on another CPU
runs within the IPI handler at ISR level (and is thus subject
to those constraints).

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-08-25 09:12:31 +02:00
Jamie McCrae
db8f991c77 kconfig: Use $(...) instead of ${...} for getting variables
Updates this to comply with the Zephyr Kconfig recommendations

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
2025-08-20 12:05:41 +02:00
Mahesh Mahadevan
ac7451e6af drivers: timer: Move MAX_WAIT to header file
Move MAX_WAIT define to the system timer header file
so system timer drivers can use this define.

Signed-off-by: Mahesh Mahadevan <mahesh.mahadevan@nxp.com>
2025-08-19 23:35:32 +02:00
Alexander Paschoaletto
8c03410af0 tracing: msgq: adding dedicated tracing for k_msgq_put_front
This commit adds the tracing macros and functions related
specifically to the k_msgq_put_front API.

Signed-off-by: Alexander Paschoaletto <axelpinheiro@gmail.com>
2025-07-25 08:16:58 -04:00
Alexander Paschoaletto
16986849d5 kernel: msgq: adding support to k_msgq_put_front
This commit introduces the k_msgq_put_front API for sending
messages to a queue in a LIFO scheme.

Signed-off-by: Alexander Paschoaletto <axelpinheiro@gmail.com>
2025-07-25 08:16:58 -04:00
Mathieu Choplain
5183fc5693 kernel: assert no spinlock is held on swap when !USE_SWITCH
The do_swap() routine used when CONFIG_USE_SWITCH=y asserts that caller
thread does not hold any spinlock when CONFIG_SPIN_VALIDATE is enabled.
However, there is no similar check in place when CONFIG_USE_SWITCH=n.

Copy this assertion in the USE_SWITCH=n implementation of z_swap_irqlock().

Signed-off-by: Mathieu Choplain <mathieu.choplain@st.com>
2025-07-24 02:47:21 -04:00
Peter Mitsis
e50851d476 arch: xtensa: Add support for lazy HiFi ctx switching
When lazy HiFi context switching is enabled, the system starts with
the HiFi coprocessor disabled. Should the thread use that coprocessor,
it will generate an exception which in turn will enable the coprocessor
and save/restore the HiFi registers as appropriate. When switching
to a new thread, the HiFi coprocessor is again disabled.

For simplicity, there are no restrictions as to which thread is allowed
to use the coprocessor.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-07-20 12:25:17 -04:00
Peter Mitsis
d397a91c62 kernel: Add arch_coprocessors_disable()
The intent of arch_coprocessors_disable() is to replace
arch_float_disable() in halt_thread() for the FPU will not
always be the only coprocessor that will need to be disabled.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-07-20 12:25:17 -04:00
Mirai SHINJO
e6ea5644b3 kernel: mmu: fix memory leak in virt_region_alloc()
The error path for an invalid destination address in virt_region_alloc()
frees the allocation using the originally requested 'size' instead of
the 'num_bits' that were actually allocated from the bitmap.
This leaks the virtual address space.

Pass 'num_bits' instead of 'size' to sys_bitarray_free() to ensure the
allocated region is correctly freed.

Signed-off-by: Mirai SHINJO <oss@mshinjo.com>
2025-07-19 15:47:35 -04:00
Nicolas Pitre
8140680e6b k_heap_aligned_alloc: validate the alignment argument
There is a special internal understanding between `z_alloc_helper()`
and `sys_heap_aligned_alloc()` for the meaning of non-power-of-two
alignment values. There was a time when `z_alloc_helper()` was expressed
in terms of `k_heap_aligned_alloc()` so the later had to accept special
alignment values from the former. This is no longer the case and
`k_heap_aligned_alloc()` should enforce proper alignment values now.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-07-19 13:45:56 -04:00
Anas Nashif
1a7ce3f45e Revert "init: Make entry init-function less and introduce service objects"
This reverts commit 175da6bdb0.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-07-16 16:39:36 -05:00
Anas Nashif
ce119f5d07 tracing: do not mark thread as switched_out in case of no reschedule
If z_get_next_switch_handle determines no reschdule is needed, do not
mark thread as switched_out in set_curent() where new and old thread are
the same.

See zephyrproject-rtos/zephyr#88596 for more details.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-07-08 18:34:11 -05:00
Daniel Leung
1984236c1d kernel: move z_sched_lock inside k_sched_lock
z_sched_lock() has exactly one user in tree which is
k_sched_lock(). So combine them to make it easier to
follow (or not, since there is no jumping to another
file anymore).

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-07-08 13:38:48 -05:00
Chaitanya Tata
4bd1d39c67 wifi: nrf70: Fix system work queue stack size override
Defaults cannot be overidden (even with configdefault extension), they
only work if the original symbol is defined after the override e.g.,
"drivers and then subsys/net" but for kernel symbols,the default value
overrides don't work due "kernel and drivers" order, the kernel defines
the original symbol with the default and then it cannot be overridden.

Move the kernel symbol override to the original definition to make it
affect. Else any sample that uses nRF70 but doesn't enable WPA
supplicant ends up with 1024 and crashes.

Signed-off-by: Chaitanya Tata <Chaitanya.Tata@nordicsemi.no>
2025-07-01 19:04:00 -05:00
Pisit Sawangvonganan
a163f9db61 kconfig: fix typo in (arch, boards, kernel, modules, samples, share)
Utilize a code spell-checking tool to scan for and correct spelling errors
in `Kconfig` files within the `arch`, `boards`, `kernel`, `modules`,
`samples`, and `share` directory.
Additionally, incorporates a fix recommended by the reviewer.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2025-07-01 10:58:54 -10:00
Tomasz Bursztyka
175da6bdb0 init: Make entry init-function less and introduce service objects
Since the addition of deinit operation in device, init and deinit have
been within each device, rendering their init entry's init function
useless.

In order to save ROM space, let's remove the init function from
init entry altogether, and introduce a new object called "service"
which owns an init function to go along with SYS_INIT/SYS_INIT_NAMED.

Signed-off-by: Tomasz Bursztyka <tobu@bang-olufsen.dk>
2025-06-27 14:13:58 +02:00
Duy Nguyen
8b0871c5e5 kernel: memslab: Prevent pointer underflow in free list init
The "p -= slab->info.block_size;" is causing the "p" pointer
to be underflow in RX architecture case, where the RAM address
start from 0x0, in some case p minus block size make it underflow
This change implementation uses an index-based reverse loop to
safely iterate over each block from last to first.

Signed-off-by: Duy Nguyen <duy.nguyen.xa@renesas.com>
2025-06-26 14:07:03 +02:00
Anas Nashif
72a8393aac kernel: kswap: removes unused arch_swap function declaration
Eliminates the external declaration of the `arch_swap` function.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-06-18 17:48:03 -04:00
Jordan Yates
117b452b50 kernel: add k_heap_array_get
Add `k_heap_array_get` as an alternative to `sys_heap_array_get`, which
only returns statically defined heaps (those defined with
`K_HEAP_DEFINE` or `K_HEAP_DEFINE_NOCACHE`), but doesn't depend on the
application guessing a value for `CONFIG_SYS_HEAP_ARRAY_SIZE`.

Signed-off-by: Jordan Yates <jordan@embeint.com>
2025-06-18 07:40:55 +02:00
Carles Cufi
61cdcb3383 kernel: Guard SYS_CLOCK_HW_CYCLES_PER_SEC to avoid spurious empty macro
If SYS_CLOCK_EXISTS is not enabled, then the SYS_CLOCK_HW_CYCLES_PER_SEC
still gets created, but with no value. This causes the code generation
in misc/generated/CMakeLists.txt to create an empty assembly macro:

`.equ  CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC,`

which then causes a build error.

Disable SYS_CLOCK_HW_CYCLES_PER_SEC entirely when SYS_CLOCK_EXISTS is
disabled to fix this.

This is a follow-up to 03f46db859.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2025-06-06 15:01:34 +01:00
Bjarki Arge Andreasen
8f9eae25c9 kernel: workq: introduce work timeout:
Introduce work timeout, which is an optional workqueue configuration
which enables monitoring for work items which take longer than
expected. This could be due to long running or deadlocked handlers.

Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
2025-06-06 08:42:53 +02:00
TOKITA Hiroshi
6fa82f7e84 kernel: mmu: Added a cast to avoid format warnings when logging
At runtime, `cbprintf` outputs a warning of the type check of the
print format.
Added a cast to avoid this warning to the first argument of
`device_map()` to `void*`.

Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@gmail.com>
2025-06-04 17:03:13 +02:00
Tomasz Bursztyka
c14ee16463 device: Provide de-init device function only if requested
This feature, fixing a specific corner case, is unilateraly growing
all struct device with a pointer that 99% of the time is not used.
Thus uselessly utilizing ROM.

Making the feature Kconfig controlled.

Signed-off-by: Tomasz Bursztyka <tobu@bang-olufsen.dk>
2025-06-03 17:08:50 +02:00
Bjarki Arge Andreasen
3d24070890 kernel: work: add k_work_queue_run()
Add the ability to block and process a work queue by invoking
`k_work_queue_run` from an existing thread. This can be particularly useful
for using the main thread to process work items, in the same vein as the
system work queue, but from a lower priority/preemptible thread.

Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
Co-authored-by: Peter Johanson <peter@peterjohanson.com>
2025-05-30 16:37:02 +02:00
Andriy Gelman
b78fb2c39d kernel/init: Corrected a typo
"bot region" should read "boot region".

Signed-off-by: Andriy Gelman <andriy.gelman@gmail.com>
2025-05-22 23:56:59 +02:00
Adrian Warecki
042bf2c3fa kernel: userspace: Fix memory leak in dynamic_object_create
If memory allocation for a dynamic object fails, an attempt is made to free
a null pointer instead of the allocated element of the dynamic object list.

Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
2025-05-20 00:26:30 +01:00
Daniel Mangum
e337b7b65d kernel: sched: default TIMESLICE_SIZE to 20
Sets the default for CONFIG_TIMESLICE_SIZE to 20 to approximate the
historical clamping behavior that was removed in 405611d.

Signed-off-by: Daniel Mangum <georgedanielmangum@gmail.com>
2025-05-16 22:55:49 +02:00
Anas Nashif
5fe84d5b69 arch: nios2: remove arch
Remove architecture and dependencies.
Remove altera HAL supporting nios2

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-05-15 20:01:05 -04:00
Alex Fabre
c8a37d009a kernel: priority_q: fix comparison of int of different signs
Clang 20.1.0 -Wsign-compare warning on int being compared with size_t
(unsigned int).

Signed-off-by: Alex Fabre <alex.fabre@rtone.fr>
2025-05-13 10:05:52 +02:00
Damian Krolik
222e0fcd06 debug: coredump: support dumping stack top only
Add Kconfig option to dump only a portion of stack from the
current stack pointer to the stack end. This is enough to
let gdb reconstruct the stack trace and can significantly
reduce the dump size. This is crucial if the core dump needs
to be sent over radio.

Additionally, add another option to set the limit for the
dumped stack portion.

Signed-off-by: Damian Krolik <damian.krolik@nordicsemi.no>
2025-05-12 19:19:25 +02:00
Björn Bergman
465f6ba73e userspace: weak defintions for generated k-object lookup functions
Instead of obscure linker-file hacks providing dummy definitions before
we have the proper generated k-object lookup functions, provide weak
definitions  that will be taken out by the real generated implementations
when they are available.

Signed-off-by: Björn Bergman <bjorn.bergman@iar.com>
2025-05-09 12:51:30 +02:00
Chris Friedt
36c44045a0 kernel: events: prevent k_event_init() from being called in an ISR
Most kernel objects should be initialized well before being
manipulated in ISR context.

Event objects are no exception. Initializing a k_event object in
ISR context would implicitly be racey and introduce an element of
non-determinism.

Assert that k_event_init() is not called from ISR context.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2025-05-02 01:16:46 +02:00