Commit graph

3196 commits

Author SHA1 Message Date
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
Julian Achatzi
f9168ae464 arch: common: Make nocache region loadable
The `nocache` is not loadable, thus data stored therein cannot be
initialized by the startup code. This might be needed in special
cases. E.g. One might have a buffer which one wants to DMA into,
and which is a member of a struct. Other members of the struct one
may want to have initialized by the startup code.
The buffer thus should be placed in the `nocache` region, but for
the other members of the buffer to be initialized by the startup
code, the `nocache` region needs to be loadable.

Fix it by making the `nocache` region loadable. Adding a KConfig
symbol to do this optionally was considered, but deemed unnecessary
during the PR.

Signed-off-by: Julian Achatzi <mail@achatzi.pro>
2025-04-21 12:42:19 +02:00
Nicolas Pitre
f087aa264e kernel/pipe: fix poll support
Two issues:

- is_condition_met() was missing proper code for The
  K_POLL_TYPE_PIPE_DATA_AVAILABLE case

- z_handle_obj_poll_events() was misplaced in z_impl_k_pipe_write()

Note: I added support for the deprecated pipe implementation to
      is_condition_met() but that is untested.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-04-21 07:45:35 +02:00
Peter van der Perk
6b369a322d kernel: init: bss clear nocache memory as well
If something is tagged as nocache it didn't got cleared, which could
lead some weird behaviour where bss memory is non-zero.

Signed-off-by: Peter van der Perk <peter.vanderperk@nxp.com>
2025-04-17 17:24:00 +02:00
Keith Packard
9398174340 kernel: Unify .ctors and .init_array handling
Handle both of these sections in a single chunk of code instead of
separately. We don't need to use the legacy .ctors ABI as both
the constructors array and startup logic are managed within a single
link result.

This can now also be used with ARC MWDT which had been using the .ctors
sections but with .init_array semantics. For ARC MWDT, we now always
discard .dtors and .fini sections as Zephyr will never cause global
destructors to execute. Stop discarding .eh_frame sections so that
exception handling works as expected.

When building a NATIVE_APPLICATION, we ask the native C library to run all
of the constructors to ensure any non-Zephyr constructors are run before
main is invoked. It might be "nice" to split the constructors so that the
Zephyr constructors were executed by the Zephyr code while the non-Zephyr
ones were executed by the native C library. I think that could be done if
we knew the pathnames of either the Zephyr or non-Zephyr files. That might
make a good future enhancement.

Signed-off-by: Keith Packard <keithp@keithp.com>
2025-04-16 20:36:27 +02:00
Peter Mitsis
4bc5498399 kconfig: Remove stale references to _Swap()
_Swap() is obsolete and has since been replaced with z_swap().

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-04-16 01:10:15 +02:00
Krzysztof Chruściński
66daaf6ba3 kernel: sched: sleep: Use value returned by z_add_timeout
z_tick_sleep function needs to calculate expected wakeup tick. It
required reading current system clock tick. It can be costly since
it is a register access. When timeout is added this value is
calculated and z_add_timeout returns it. It can be used instead to
optimize sleep performance.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
2025-04-15 19:09:33 +02:00
Krzysztof Chruściński
5f55fed4bb kernel: timeout: z_add_timeout to return expiration tick
Add return value to z_add_timeout. It returns system tick when timeout
will expire.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
2025-04-15 19:09:33 +02:00
Krzysztof Chruściński
6d35969a55 kernel: sched: Optimize sleeping function
Accessing system timer registers can be costly and it shall be avoided
if possible. When thread is waken up in z_tick_sleep it may be because
timeout expired or because thread was waken up before sleeping period
passed.

Add function to detect if timeout is aborted (before it was expired).
Use it in the sleep function and avoid reading system ticks if timeout
was not aborted.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
2025-04-15 19:09:33 +02:00
Pisit Sawangvonganan
cc0684351a kernel: system_work_q: make k_work_queue_config cfg as static const
Make `k_work_queue_config cfg` as `static const` to enable
compile-time instantiation instead of runtime allocation.
This modification saves substantial flash memory but has system-wide
effects that should be considered.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2025-04-04 21:15:40 +02:00
Krzysztof Chruściński
e7d590568c kernel: timeout: Reduce number of sys_clock_elapsed calls
sys_clock_elapsed requires access to system clock register interface
which is often slow. When new relative timeout is added sys_clock_elapsed()
is called once to calculate delta ticks and then if that triggers setting
new timeout sys_clock_elapsed() is called again. This call is redundant
since everything happens under spin lock so it is better to reuse value
returned by the first call.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
2025-04-04 07:43:01 +02:00
Anas Nashif
a5413499f1 kernel: thread_monitor: reafactor and remove duplicate code
Refactor code and remove duplication, same code almost being repeated 4
times.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-04-04 07:42:20 +02:00
Nicolas Pitre
457fa60bfc kernel: mempool: decouple simple alloc from aligned_alloc
When k_malloc() is expressed in terms of k_aligned_alloc() it invokes a
longer aligned allocation code path with an extra runtime overhead even
though no alignment is necessary.

Let's reference and invoke the aligned allocation code path only when an
actual aligned allocation is requested. This opens the possibility for
the linker to garbage-collect the aligning code otherwise.

Also bypass k_heap_malloc() and friends given they're invoked with
K_NO_WAIT. Go directly to sys_heap_*() instead to cut some more unneeded
overhead.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-04-01 22:13:04 +02:00
Nicolas Pitre
7c9f464b70 kernel: kheap: workaround for Percepio's TraceRecorder kludge
In .../modules/debug/percepio/TraceRecorder/kernelports/Zephyr/include/\
tracing_tracerecorder.h there is a concealed non-parameterized direct
reference to a local variable that is no longer in scope. Provide a dummy
stub for compilation to succeed until that module's layering violation is
fixed, after which this could be reverted.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-04-01 22:13:04 +02:00
Nicolas Pitre
9da06456f2 kernel: kheap: decouple simple alloc from aligned_alloc
When k_heap_alloc() is expressed in terms of k_heap_aligned_alloc()
it invokes a longer aligned allocation code path with an extra runtime
overhead even though no alignment is necessary.

Let's reference and invoke the aligned allocation code path only when an
actual aligned allocation is requested. This opens the possibility for
the linker to garbage-collect the aligning code otherwise.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-04-01 22:13:04 +02:00
Nicolas Pitre
31244724a2 kernel: k_aligned_alloc: simplify assertion
Let's avoid division and modulus operations as they're costly... and even
more so when they're unnecessary as in this case the main constraint is
about the alignment being a power of 2 which is a very small subset of
sizeof(void *) multiples.

Then move the assertion to common code for wider coverage.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2025-04-01 22:13:04 +02:00
Anisetti Avinash Krishna
139211772c include: zephyr: sys: time_units: Make z_clock_hw_cycles_per_sec unsigned
Convert z_clock_hw_cycles_per_sec to unsigned int to increase
supported frequency range.

Signed-off-by: Anisetti Avinash Krishna <anisetti.avinash.krishna@intel.com>
2025-03-28 12:21:07 +01:00
Josh DeWitt
c05cfbf15e kernel/sched: Re-sort waitq on priority change
k_thread_priority_set() on a pended thread wasn't re-inserting into the
waitq, causing the incorrect thread to run based on priority. When using
the scalable waitq config, this can also break assumptions of the tree
and leave the owner of a waitq still being in the waitq tree, cycles in
the tree, or a crash.

Remove and re-add a thread to a waitq to ensure the waitq remains in
order and the tree's assumptions are not violated.

To illustrate the issue, consider 4 threads in decreasing priority
order: A, B, C, and D along with two mutexes, m0 and m1. This is
implemented in the new complex_inversion mutex_api test.
1. D locks m1
2. C locks m0
3. C pends on m1
4. B pends on m1
5. A pends on m0, boosts C's priority, now tree on m1 is not sorted
6. D unlocks m1, left-most thread on tree is B. When removing B from
   tree it cannot be found because it searches to the right of C due to
   C's boosted priority when the node is actually on the left. rb_remove
   silently fails.
7. B unlocks m1, left-most thread on tree is still B and it tries to
   unpend itself, resulting in a NULL pointer dereference on
   B->base.pended_on.

Signed-off-by: Josh DeWitt <josh.dewitt@garmin.com>
2025-03-24 07:58:36 +01:00
Peter Mitsis
701aab92e2 kernel: Add Z_IS_TIMEOUT_RELATIVE() macro
Introduces the Z_IS_TIMEOUT_RELATIVE() macro to help ensure that
checking for relative/absolute timeouts is consistent. Using this
macro also helps ensure that we get the correct behavior when using
32-bit timeouts (CONFIG_TIMEOUT_64BIT=n).

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-03-17 02:21:02 +01:00
Anas Nashif
f29ae72d79 kernel: rename 'dumb' scheduler and simply call it 'simple'
Improve naming of the scheduler and call it what it is: simple. Using
'dumb' for the default scheduler algorithm in Zephyr is a bad idea.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-03-15 00:34:58 +01:00
Peter Mitsis
c6bc09223e kernel: Move current_fp field out of z_kernel
The current_fp field in the z_kernel structure is only used
by 32-bit x86 (which does not support SMP). As such, it should
reside in the arch specific of section of _kernel.cpus[0].

This also changes the name of 'current_fp' to 'fpu_owner' to
be more consistent with other architectures.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-03-14 05:47:10 +01:00
William Tambe
15fa9a8ed8 arch: x86: remove arch specific dependencies from DYNAMIC_BOOTARGS
CONFIG_DYNAMIC_BOOTARGS currently depends on CONFIG_MULTIBOOT_INFO
and CONFIG_BUILD_OUTPUT_EFI which are x86 specific.

This change removes those dependencies so that another architecture
can use CONFIG_DYNAMIC_BOOTARGS without the x86 specifc features.

Signed-off-by: William Tambe <williamt@cadence.com>
2025-03-13 07:22:53 +01:00
Henrik Brix Andersen
110cd6b806 kernel: fix compilation with CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME
Fix compilation with CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME=y and
CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC unset at the board/SoC level.

Signed-off-by: Henrik Brix Andersen <henrik@brixandersen.dk>
2025-03-11 18:53:40 +01:00
Gerard Marull-Paretas
0d4b957b11 device: allow initializing any device
Remove restrictions from device_init by allowing to perform device
initialization if the device state flags it being not initialized.
This makes the API usable in contexts where device_deinit has been
called before.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2025-03-10 21:32:40 +01:00
Gerard Marull-Paretas
8e59d4cd9d device: add new device_deinit API
Add a new API to de-initialize a device. When a device is
de-initialized, it will release any resources it has acquired
(e.g. pins, memory, clocks, DMA channels, etc.) and its status
will be left as in its reset state.

It is the responsability of the caller to ensure that the device is
ready to be de-initialized.

For now, deinit call always initializes to NULL. New macros will be
introduced to not break existing device APIs.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2025-03-10 21:32:40 +01:00
Gerard Marull-Paretas
766bfe7b2e device: introduce struct device_ops
Instead of passing a single init function, create
struct device_ops with the init function inside. This allows to easily
extend device's capabilities in the future without too much breakage,
e.g. to add a de-init call.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2025-03-10 21:32:40 +01:00
Gerard Marull-Paretas
f44a30109c device: introduce device flags
Introduce a new field to store device flags. Only device deferred init
flag has been added, replacing usage of linker hackery to know wether a
device requires initialization at boot time or not. This change will be
helpful in the near future as devices will become reference counted, so we
will need to know wether they have been initialized or not.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2025-03-10 21:32:40 +01:00
Gerard Marull-Paretas
3f6add69da init: drop device union from struct init_entry
Such union is rather redundant, considering a simple const cast can be
done when initializing the init entry. Note that the init_entry does not
need to be touched now that struct device stores the init call. It is
merely an init entry sorted by linker scripts, so we can intertwine
devices and SYS_INIT.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2025-03-10 21:32:40 +01:00
Gerard Marull-Paretas
ed3377ae78 init: drop init_fn union
Device init function is no longer taken from `struct init_entry`, so
there's no need to keep such union.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2025-03-10 21:32:40 +01:00
Gerard Marull-Paretas
a0a907c90e device: store init function in struct device
Preparation work for device init/de-init functionality.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2025-03-10 21:32:40 +01:00
Gerard Marull-Paretas
9b5083bb89 init: drop anonymous union
It just complicates things. It is not C99 strandard, and since C11 is
not mandatory, it is better to play safe here.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2025-03-10 21:32:40 +01:00
Peter Mitsis
ec4df64dc7 kernel: make order_key field in thread conditional
The 'order_key' field in the thread structure '_thread_base' is only
required when CONFIG_SCHED_SCALABLE and/or CONFIG_WAITQ_SCALABLE are
enabled (neither of which is a default setting). Making the existence
of this field conditional slightly reduces the size of the k_thread
structure when neither of those Kconfig options are selected.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-03-08 03:38:37 +01:00
Andy Ross
8b4ed6655a kernel: Clamp k_sleep() return value on overflow
k_sleep() returns a 32 bit count of milliseconds, as that was its
historical API.  But it now accepts a potentially 64 bit tick count as
an argument, leading to situations where an early wakeup will produce
sleep times that aren't representable.  Clamp this instead of
truncating to an arbitrary value.

Naive code will likely do the right thing with the large return (just
sleeping an extra round), and sophisticated apps can detect INT_MAX to
enable more elaborate retry logic.

(Also fixes a somewhat unfortunate puncutation error in the docs that
implied that it returns zero on early wakeup!)

Fixes: #84669

Signed-off-by: Andy Ross <andyross@google.com>
2025-03-07 20:20:25 +01:00
Robin Kastberg
ddd1063715 soc: stm32: handle CCM in LINKER_GENERATOR
Currently, the soc/stm32/ccm.ld is not handled in
CMAKE_LINKER_GENERATOR.

This commit adds support, making STM32 supportable by
alternative linkers such as AC6 and IAR.

This commit also renames a variable to match all other
LOADADDR symbols.

Signed-off-by: Robin Kastberg <robin.kastberg@iar.com>
2025-03-07 19:54:24 +01:00
Andy Ross
ddee1ab4cf kernel/sched: Correct locking in essential thread panic
Calling a (handled/ignored) panic with the scheduler lock held
produces spinlock errors in some circumstances, depending on whether
or not the swap gets reached before another context switch.  Release
the lock around the call, we don't touch any scheduler state on the
path to z_swap(), so this is safe.

Signed-off-by: Andy Ross <andyross@google.com>
2025-02-26 10:10:29 +00:00
Anas Nashif
e43fa18e4d arch: mmu: do not make MMU user configurable
MMU option is selected by the MMU implementation, i.e. X86_MMU or
ARM_MMU selects MMU.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-02-19 04:59:19 +01:00