Commit graph

3183 commits

Author SHA1 Message Date
Chris Friedt
95896bf17c kernel: dynamic: simplify dynamic stack allocation
Merge the functions z_thread_stack_alloc_dyn() and
stack_alloc_dyn(), simplifying the flow for dynamic thread stack
allocation.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-12-11 21:33:00 +01:00
Akaiwa Wataru
feefb7dd35 kernel/sched: correct k_sleep() return value calculation
Fix the issue: https://github.com/zephyrproject-rtos/zephyr/issues/79863

The expected_wakeup_ticks and sys_clock_tick_get_32() are uint32_t values,
and may wrap around individually.
If the expected_wakeup_ticks has a wraparound and sys_clock_tick_get_32()
doesn't, so expected_wakeup_ticks < sys_clock_tick_get_32(), the API return
value will be corrupted.

The API return value, that is the remaining time, should be calculated in
32bit-unsigned-integer manner, and any wraparound will be treated properly.

Signed-off-by: Akaiwa Wataru <akaiwa@sonas.co.jp>
2024-12-03 02:37:03 +01:00
Andy Ross
7cdf40541b kernel/sched: Eliminate PRESTART thread state
Traditionally threads have been initialized with a PRESTART flag set,
which gets cleared when the thread runs for the first time via either
its timeout or the k_thread_start() API.

But if you think about it, this is no different, semantically, than
SUSPENDED: the thread is prevented from running until the flag is
cleared.

So unify the two.  Start threads in the SUSPENDED state, point
everyone looking at the PRESTART bit to the SUSPENDED flag, and make
k_thread_start() be a synonym for k_thread_resume().

There is some mild code size savings from the eliminated duplication,
but the real win here is that we make space in the thread flags byte,
which had run out.

Signed-off-by: Andy Ross <andyross@google.com>
2024-11-27 10:38:05 -05:00
Andy Ross
6877b6d8e5 kernel/thread: Fix assumptions in k_thread_state_str()
This table just blindly assumed that the values of _THREAD_xxx state
bits wouldn't change.  That's dangerous.  (That's right, I tried to
change them and broke it.)

Signed-off-by: Andy Ross <andyross@google.com>
2024-11-27 10:38:05 -05:00
James Roy
ef8f037e78 device: Optimize device_get_binding search efficiency
Optimize 'device_get_binding' function search so that
when a matching device is found, it returns NULL if
it is not ready.

Signed-off-by: James Roy <rruuaanng@outlook.com>
2024-11-27 08:18:30 +01:00
Anas Nashif
f90ce01d4a kernel: sched: use arch_current_thread instead of _current
_current is deprecated, use arch_current_thread()
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2024-11-25 19:06:57 -05:00
Andy Ross
b3ff9ae82b kernel/sched: Optimize handling for suspend(_current)
k_thread_suspend() is an async API intended to stop any thread in any
state from any context.  Some apps just want to use it to "suspend
myself", which is a much (!) simpler operation.  Detect that specific
usage as a performance case.

Signed-off-by: Andy Ross <andyross@google.com>
2024-11-26 00:12:28 +01:00
Yong Cong Sin
b1def7145f arch: deprecate _current
`_current` is now functionally equals to `arch_curr_thread()`, remove
its usage in-tree and deprecate it instead of removing it outright,
as it has been with us since forever.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
2024-11-23 20:12:24 -05:00
Yong Cong Sin
d26c712258 arch: add new interfaces to set/get the current thread of current CPU
Add the following arch-specific APIs:
- arch_curr_thread()
- arch_set_curr_thread()

which allow SMP architectures to implement a faster "get current
thread pointer" than the default provided by the kernel. The 'set'
function is required for the 'get' to work, more on that later.

When `CONFIG_ARCH_HAS_CUSTOM_CURRENT_IMPL` is selected, calls to
`_current` & `k_sched_current_thread_query()` will be redirected to
`arch_curr_thread()`, which ideally should translate into a single
instruction read, avoiding the current
"lock > read CPU > read current thread > unlock" path in SMP
architectures and thus greatly improves the read performance.

However, since the kernel relies on a copy of the "current thread"s on
every CPU for certain operations (i.e. to compare the priority of the
currently scheduled thread on another CPU to determine if IPI should be
sent), we can't eliminate the copy of "current thread" (`current`) from
the `struct _cpu` and therefore the kernel now has to invoke
`arch_set_curr_thread()` in addition to what it has been doing. This
means that it will take slightly longer (most likely one instruction
write) to change the current thread pointer on the current
CPU.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
2024-11-23 20:12:24 -05:00
Volodymyr Fialko
9eee2eaee6 riscv: pmp: enable stackguard without multithreading
Without multithreading only two stacks present: ISR and main.
As any stack they also could overflow, so it make sense to add stack
guard for them also.

Remove stack guard dependency on multithreading and mark
`Z_RISCV_STACK_GUARD_SIZE` bytes at the beginning of stack as read-only
region with PMP entry.

Signed-off-by: Volodymyr Fialko <vfialko@marvell.com>
2024-11-20 08:25:49 -05:00
Lingao Meng
6d6d4565d3 kernel: workq: Fix function format to avoid CI Warning
formating

Signed-off-by: Lingao Meng <menglingao@xiaomi.com>
2024-11-20 10:15:20 +00:00
Lingao Meng
836fa88cbd kernel: work: Add missing ASSERT for args
Add missing ASSERT for dwork & queue.

Signed-off-by: Lingao Meng <menglingao@xiaomi.com>
2024-11-20 10:15:20 +00:00
Lingao Meng
2a3b28a3f5 kernel: work: Remove meanless foreach for work
Since call this function both in
```C
	k_spinlock_key_t key = k_spin_lock(&lock);

	bool need_flush = work_flush_locked(work, flusher);

	k_spin_unlock(&lock, key);
```

So, there are no flag_get change.

Signed-off-by: Lingao Meng <menglingao@xiaomi.com>
2024-11-20 10:15:20 +00:00
Robin Kastberg
6532c2f0bb kernel: make z_is_idle_thread_entry take code ptr
This is causing errors on IAR toolchain.

Signed-off-by: Robin Kastberg <robin.kastberg@iar.com>
2024-11-18 14:02:56 -05:00
Daniel Leung
01f8e0fa2d demand_paging: eviction: add kconfig CONFIG_EVICTION_TRACKING
This adds a new kconfig for eviction algorithm which needs page
tracking. When enabled, k_mem_paging_eviction_add()/_remove()
and k_mem_paging_eviction_accessed() must be implemented.
If an algorithm does not do page tracking, there is no need to
implement these functions, and no need for the kernel MMU code
to call into empty functions. This should save a few function
calls and some CPU cycles.

Note that arm64 unconditionally calls those functions so
forces CONFIG_EVICTION_TRACKING to be enabled there.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2024-11-18 13:16:44 -05:00
Corey Wharton
76bceb9ed2 kernel: mem_slab: always validate memory address on free
Allowing an invalid address to be "freed" when asserts are disabled
is dangerous and can lead to a very hard class of bugs (and potential
security issues) to troubleshoot. This change always validates the
address before adding it to the free list and calls k_panic() if
asserts are not enabled.

Signed-off-by: Corey Wharton <xodus7@cwharton.com>
2024-11-16 15:54:56 -05:00
Chris Friedt
9504034733 sys: util: use BITS_PER_BYTE macro instead of the magic number 8
Obviously, everyone knows that there are 8 bits per byte, so
there isn't a lot of magic happening, per se, but it's also
helpful to clearly denote where the magic number 8 is referring
to the number of bits in a byte.

Occasionally, 8 will refer to a field size or offset in a
structure, MMR, or word. Occasionally, the number 8 will refer
to the number of bytes in a 64-bit value (which should probably
be replaced with `sizeof(uint64_t)`).

For converting bits to bytes, or vice-versa, let's use
`BITS_PER_BYTE` for clarity (or other appropriate `BITS_PER_*`
macros).

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-11-16 15:22:35 -05:00
Tom Burdick
2b5012a5d9 kernel: Move run queue initialization
Move the initialization of the priority q for running out of sched.c to
remove one more ifdef from sched.c. No change in functionality but
better matches the rest of sched.c and priority_q.h such that the
ifdefry needed is done in in priority_q.h.

Signed-off-by: Tom Burdick <thomas.burdick@intel.com>
2024-11-16 15:20:15 -05:00
Mikhail Kushnerov
a995d9d76f kernel: fix k_sleep in no multi-threading mode
Fix k_sleep implementation for no multi-threading mode.

Absolute value of timeout expiration was fed to the k_busy_wait()
function instead of delta value. That caused bug like incrementing of
sleep time in geometric progression (while actual function argument is
constant) during program running.

Signed-off-by: Mikhail Kushnerov <m.kushnerov@yadro.com>
2024-11-16 14:07:41 -05:00
Yong Cong Sin
de347a4e07 init: support per-core init hook
Allow SoC to implement their custom per-core initialization function by
selecting `CONFIG_SOC_PER_CORE_INIT_HOOK` and implement
`soc_per_core_init_hook()`.

Signed-off-by: Maxim Adelman <imax@meta.com>
Signed-off-by: Yong Cong Sin <ycsin@meta.com>
Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
2024-11-16 14:04:25 -05:00
Daniel DeGrasse
6023d6a142 arch: common: fix copy for ramfunc region during XIP init
ramfunc region is copied into RAM from FLASH region during XIP init. We
copy from the loadaddr of the region, and were previously loading to the
symbol __ramfunc_start. This is incorrect when using an MPU with
alignment requirements, as the __ramfunc_start symbol may have padding
placed before it in the region. The __ramfunc_start symbol still needs
to be aligned in order to be used by the MPU though, so define a new
symbol __ramfunc_region_start, and use that symbol when copying the
__ramfunc region from FLASH to RAM.

Fixes #75296

Signed-off-by: Daniel DeGrasse <daniel.degrasse@nxp.com>
2024-11-06 10:19:08 -08:00
Anas Nashif
6261e2a673 Revert "kernel: banner: Expose tainted builds"
This reverts commit 6d4031f96c.

Those makes majority of builds og platforms with blobs tainted although
the blob were not used or compiled in. So it is very misleading.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2024-10-29 14:17:21 -05:00
Peter Mitsis
cedd36106b kernel: Begin abstracting out _sched_spinlock
In a uniprocessor system, _sched_spinlock may not need to be
held in all the same cases that it does in a multiprocessor
system. Removing those unnecessary usages can lead to better
performance on UP systems. In the case of uncontested taking
and giving of a semaphore, this can be as much as a +14%
performance gain.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2024-10-21 18:38:00 -05:00
Peter Mitsis
f6a76c32b7 kernel: inline z_unpend_first_thread()
Inlining z_unpend_first_thread() has been observed to give a
+8% and +16% performance boost to the thread_metric benchmark's
message processing and synchronization tests respectively.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2024-10-21 18:38:00 -05:00
Peter Mitsis
0bf44f2352 kernel: Re-order should_preempt() checks
Re-orders the checks in should_preempt() tests so that the
z_is_thread_timeout_active() check is done last.

This change has been observed to give a +7% performance boost on
the thread_metric benchmark's preemptive scheduling test.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2024-10-21 18:38:00 -05:00
Reto Schneider
6d4031f96c kernel: banner: Expose tainted builds
Due to the (potentially) hard to understand effects of blobs, it seems
prudent to make their presence more noticeable.

With this change, whenever blobs are present in the Zephyr work space,
the hello world sample output looks like this:

> *** Booting Zephyr OS build (tainted) v3.7.0-4569-gd4f8765ef20e ***
> Hello World! esp32c3_devkitm/esp32c3

Before, it looked like this:

> *** Booting Zephyr OS build v3.7.0-4568-g69c47471d187 ***
> Hello World! esp32c3_devkitm/esp32c3

Signed-off-by: Reto Schneider <reto.schneider@husqvarnagroup.com>
2024-10-21 15:58:27 +02:00
Peter Mitsis
c70a619a2f kernel: Remove unused z_ready_thread_locked()
Removing the routine z_ready_thread_locked() as it is not
used anywhere. It was a leftover artefact from development
that previously escaped cleanup.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2024-10-15 19:08:30 -04:00
Peter Mitsis
cc415bc139 kernel: Apply 'unlikely' attribute
Applies the 'unlikely' attribute to various kernel objects that
use z_unpend_first_thread() to optimize for the non-blocking path.

This boosts the thread_metric synchronization benchmark numbers
on the frdm_k64f board by about 10%.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2024-10-15 04:06:32 -04:00
Anas Nashif
121cb49a46 kernel: sched: inline update_cache
This improves context switching by 7% when measured using the
thread_metric benchmark.

Before:
**** Thread-Metric Preemptive Scheduling Test **** Relative Time: 120
Time Period Total:  5451879

After:
**** Thread-Metric Preemptive Scheduling Test **** Relative Time: 30
Time Period Total:  5853535

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2024-10-10 20:21:04 -04:00
Peter Mitsis
318b49570a tests: scheduler queue benchmarks
Implements a set of tests designed to show how the performance of the
three scheduler queue implementations (DUMB, SCALABLE and MULTIQ)
varies with respect to the number of threads in the ready queue.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2024-10-07 20:16:20 -04:00
Chris Friedt
b6aed5c505 kernel: dynamic: use 4k stack size for x86
x86 architectures require a dynamic stack size that is a multiple
of 4096 bytes due to mmu restrictions.

For example, this test would previously fail when using the
default dynamic stack size of 1024 bytes for 32-bit
platforms.

```
west build -p auto -b qemu_x86/atom/nopae -t run \
  tests/posix/common/ -- -DCONFIG_USERSPACE=y
```

It would pass with an additional argument
```
west build -p auto -b qemu_x86/atom/nopae -t run \
  tests/posix/common/ -- -DCONFIG_USERSPACE=y \
  -DCONFIG_DYNAMIC_THREAD_STACK_SIZE=4096
```

Add a special default for x86 when using dynamic thread stacks.

The x86 default removes the need for `boards/qemu_x86*.conf`,
with the exception of `qemu_x86_tiny`.

qemu_x86_tiny did not have sufficient memory (or configuration)
to run the non-userspace tests, so bump up the available ram
from 256k to 512k for this test and clone the .conf from the
demand paging tests.

Eventually, the common posix test should be split into more
concise functional categories.

Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
2024-10-05 14:13:18 -04:00
Yong Cong Sin
52a202309b zephyr: bulk update to DT_NODE_HAS_STATUS_OKAY
Change instances of:

DT_NODE_HAS_STATUS(<node_id>, okay)

to

DT_NODE_HAS_STATUS_OKAY(<node_id>)

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
2024-10-03 17:06:52 +01:00
Daniel Flodin
746c59c82a arch: kernel: lib: toolchain: Standardize TLS keyword
Up until now, the `__thread` keyword has been used for declaring
variables as Thread local storage. However, `__thread` is a GNU
specific keyword which thus limits compatibility with other
toolchains (for instance IAR).

This PR intoduces a new macro `Z_THREAD_LOCAL` which expands to the
corresponding C11, C23 or C++11 standard keyword based on the standard
that is specified during compilation, else it uses the old `__thread`
keyword.

Signed-off-by: Daniel Flodin <daniel.flodin@iar.com>
2024-09-23 10:01:48 +02:00
Daniel Leung
f5163d8940 arch: add interface to dump privileged stack in coredump
This adds a interface to allow coredump to dump privileged
stack which is defined in architecture specific way.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2024-09-21 11:29:39 +02:00
Anas Nashif
e9474302da init: enhance kconfig docs for custom hooks
Improve docs for custom soc/board hooks.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2024-09-20 13:15:31 +02:00
Pisit Sawangvonganan
5c8a2c0dbf style: kernel: remove unnecessary return statements
For code clarity, remove unnecessary `return` statements
in functions with a void return type they don't affect control flow.

Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
2024-09-20 11:06:55 +02:00
Peter Mitsis
eda5814dbc kernel: Simplify k_thread_cpu_pin()
Simplifies the k_thread_cpu_pin() implementation to leverage the
existing cpu_mask_mod() infrastructure.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2024-09-20 09:02:38 +02:00
Yong Cong Sin
b55f3c1c4f kernel: remove CONFIG_MP_NUM_CPUS
`CONFIG_MP_NUM_CPUS` has been deprecated for more than 2
releases, it's time to remove it.

Updated all usage of `CONFIG_MP_NUM_CPUS` to
`CONFIG_MP_MAX_NUM_CPUS`

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
2024-09-19 18:28:37 +01:00
Anas Nashif
7e225efab7 arch: initialize irq_offload during boot, do not use SYS_INIT
Do not use SYS_INIT for initializing irq_offload when enabled, instead
using a new interface that is called during the boot process for all
architectures.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2024-09-17 20:05:22 -04:00
Anas Nashif
ca09a4b91c doc: kernel/arch: fix some wrong doxygen references
Remove non-existing references and document parameters.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2024-09-17 05:24:09 -04:00
Yong Cong Sin
035c822253 arch: riscv: fill all IRQ stacks with 0xAA
Fill the memory of all CPU's IRQ stack with 0xAA on init, so
that `z_stack_space_get` can calculate the remaining space
correctly.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
Signed-off-by: Yong Cong Sin <ycsin@meta.com>
Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
2024-09-13 09:17:34 +02:00
Pisit Sawangvonganan
1e58b53d2a style: kernel: 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
Nicolas Pitre
1e4fd23e58 kernel: mmu: install demand mappings for the on-demand linker sections
This sets initial unpaged mappings for __ondemand_func code and
__ondemand_rodata variables. To achieve this, we have to augment the
backing store API.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2024-09-10 17:17:30 -04:00
Nicolas Pitre
6b3fff3a2f kernel: mmu: make demand paging work on SMP
This is the minimum for demand paging to work on SMP systems.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2024-09-10 11:44:16 +02:00
Anas Nashif
e260d03686 init: introduce soc and board hooks
Introduce soc and board hooks to replace arch specific code
and replace usages of SYS_INIT for platform initialization.

include/zephyr/platform/hooks.h introduces the hooks to be implemented
by boards and SoCs.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2024-09-09 10:07:33 +02:00
Nicolas Pitre
c692136f21 mmu: introduce k_mem_update_flags()
It is sometimes necessary to modify/update memory permissions on some
pages, especially with LLEXT where some allocated segments have to be
executable.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2024-09-06 11:25:54 -04:00
Jakub Michalski
f568e2d3ca zefi: add bootargs support
Add bootargs support to zefi. This implements
get_bootargs() when both efi and bootargs are
selected in config.

Signed-off-by: Jakub Michalski <jmichalski@internships.antmicro.com>
Signed-off-by: Filip Kokosinski <fkokosinski@antmicro.com>
2024-09-05 12:30:39 -05:00
Jakub Michalski
0cf726b8ef arch/x86: multiboot: add bootargs support
Add bootargs support for multiboot. This
implements get_bootargs() when multiboot and
bootargs are selected in config.

Signed-off-by: Jakub Michalski <jmichalski@internships.antmicro.com>
Signed-off-by: Filip Kokosinski <fkokosinski@antmicro.com>
2024-09-05 12:30:39 -05:00
Jakub Michalski
49fc106c60 kernel: add bootargs support
Add support for passing args to main(). The
content of bootargs is taken from get_bootargs()
which should be implemented for each loader and
then its split into args and passed to main.

Signed-off-by: Jakub Michalski <jmichalski@internships.antmicro.com>
Signed-off-by: Filip Kokosinski <fkokosinski@antmicro.com>
2024-09-05 12:30:39 -05:00
Rick Tsao
e9ff0dc35e kernel: userspace: Fix dynamic thread stack allocation for RISC-V
RISC-V use the same logic for dynamic thread stacks allocation as ARM.
This fixup reuses ARM code.

Signed-off-by: Rick Tsao <rick592@andestech.com>
2024-09-04 21:28:36 +02:00