Commit graph

642 commits

Author SHA1 Message Date
Mark Holden
827a4f7387 coredump: xtensa: Add support for ARCH_SUPPORTS_COREDUMP_STACK_PTR
Select ARCH_SUPPORTS_COREDUMP_STACK_PTR on xtensa, and provide an
implementation for the arch_coredump_stack_ptr_get function.

Signed-off-by: Mark Holden <mholden@meta.com>
2025-09-09 14:53:00 +02: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
5e6e3a6de3 arch: mark z_prep_c as FUNC_NORETURN
z_prep_c does not return, mark it as such consistently across
architectures.  We had some arches do that, others not. This resolves a
few coding guideline violations in arch code.

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
Benjamin Cabé
0132ea07fb doc: fix spelling errors tree-wide
fix some spelling errors in code comments and Kconfig helps

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2025-09-03 17:04:13 +02:00
Guennadi Liakhovetski
414ede3057 xtensa: jump to GDB upon exception
If the GDB stub is enabled the exception handler will jump to the GDB
stub to allow remote GDB debugging.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2025-08-21 15:58:44 +02:00
Keith Packard
9051e2ea3e toolchain/gcc: Add "memory" clobber to asm traps before CODE_UNREACHABLE
GCC 14.3 will happily delete any code that appears before
__builtin_unreachable that isn't separated with an obvious branch. That
includes __asm__ statements, even those which generate traps.

The failure case that I debugged was on x86 in
z_check_stack_sentinel. There is a store to restore the sentinel to the
correct value just before the ARCH_EXCEPT, and that macro emits 'int $32'
followed by CODE_UNREACHABLE. Because the compiler didn't understand that
ARCH_EXCEPT was changing execution flow, it decided that the sentinel
restoring store 'couldn't' be reached and elided it.

I added the "memory" clobber to the asm statement in ARCH_EXCEPT before
CODE_UNREACHABLE to enforce that all pending store operations be performed
before the asm statement occurs. This ensures that they are not deleted by
the compiler.

I think this might be a GCC bug. The GCC documentation explicitly documents
that asm statements which change the flow of control should be followed by
__builtin_unreachable.

Signed-off-by: Keith Packard <keithp@keithp.com>
2025-08-18 22:01:08 +02:00
Peter Mitsis
85b8ae9b6a arch: xtensa: Fix arch_is_in_isr() race condition
Fixes a flaw in the the xtensa implementation of arch_is_in_isr()
that could manifest on SMP systems. If the reading of the current
CPU's nested interrupt count is not fully atomic on an SMP system,
then an ill-timed context switch could occur leaving the caller
reading the nested interrupt of a different CPU.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-08-02 13:19:21 +02:00
Gaetan Perrot
46341938eb arch: xtensa: core: thread: mark unused function argument
Use ARG_UNUSED() to mark unused function argument.

Signed-off-by: Gaetan Perrot <gaetan.perrot@spacecubics.com>
2025-08-01 07:54:11 -04:00
Guennadi Liakhovetski
9ce6d3599c xtensa: export syscall helpers
When building with user space and LLEXT support, syscall helpers need
to be exported.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2025-08-01 07:45:16 -04:00
Mike J. Chen
2dc6793261 soc: imxrt5xx: add fusion f1 dsp backtrace support
Add needed backtrace helpders routines and enable
backtrace for the Xtensa Fusion F1 DSP in the
IMXRT595S.

Signed-off-by: Mike J. Chen <mjchen@google.com>
2025-07-31 10:49:54 +01:00
Guennadi Liakhovetski
1dbca21fa2 gdb: xtensa: fix sparse warnings
Fix a sparse memory attribute mismatch.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2025-07-21 13:03:30 -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
Peter Mitsis
6505cf2e54 arch: xtensa: Carve space for saved HiFi regs
As the BSA can not be used when lazy HiFi context switching is
used, a more permanent and predictable location in which to store
the registers is required. To this end ...
  1. reserve some space in the arch-specific portion of the k_thread
     structure for those registers.
  2. clear that region when the thread is created.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-07-20 12:25:17 -04:00
Peter Mitsis
cd351208d5 arch: xtensa: Refine HiFi sharing Kconfigs
Splits HiFi sharing into two different models.
 1. XTENSA_EAGER_HIFI_SHARING - unconditional save/restore
    of the HiFi registers when context switching
 2. XTENSA_LAZY_HIFI_SHARING - on demand save/restore of
    of the HiFi registers. If a thread does not use the
    HiFi registers, they are neither saved nor restored.

To maintain backwards compatibility, the eager model is the
default model when XTENSA_HIFI_SHARING is enabled.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-07-20 12:25:17 -04:00
Anas Nashif
0a4e27c45f xtensa: use EXCEPTION_DUMP macro for fatal messages
instead of depending on logging for exceptions and fatal messages, use
macro and dump exceptions if printk is enable with no logging.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-07-19 15:49:28 -04:00
Anas Nashif
7f282f7928 xtensa: tracing: instrument thread switching
Add missing call to thread_switched_in for the purpose of tracing.

Fixes zephyrproject-rtos/zephyr#76057

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-07-08 18:34:11 -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
Daniel Leung
a9574c17ae xtensa: gdbstub: fix code stepping
The ICOUNTLEVEL register needs to be manipulated carefully
according to where we want to stop.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-06-30 15:19:59 -05:00
William Tambe
abeccfec28 xtensa: support for more than 32 interrupts
This change add support for using more than 32 interrupts.

Signed-off-by: William Tambe <williamt@cadence.com>
2025-06-27 08:59:56 -10:00
Guennadi Liakhovetski
b8a2eef2ef xtensa: gdbstub: update comments
Update a comment about GDB use of register index and "regno"
parameters. Only some GDB versions use "idx" instead of "regno."

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2025-06-23 12:32:44 -07:00
Noah Klayman
28e2b50aff xtensa: gdbstub: fix unavailable register reporting
To report unavailable registers to the GDB client, the GDB stub has
to return a string like "xx" in the value string.

Signed-off-by: Noah Klayman <noah.klayman@intel.com>
Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2025-06-23 12:32:44 -07:00
Guennadi Liakhovetski
1df49e9691 xtensa: gdbstub: (cosmetic) simplify a conditional
Move an assignment performed on both "if" branches as the last
operation to after the "if."

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2025-06-23 12:32:44 -07:00
Tahsin Mutlugun
784b3d6ea0 arch: xtensa: Add semihosting support
Add semihosting support for Xtensa architecture.

Existing semihosting instructions are based on ARM, so they are
converted to Xtensa codes before the semihosting call is invoked.

Return codes of read, write and seek calls had to be converted to match
semihosting API definitions.

Signed-off-by: Tahsin Mutlugun <Tahsin.Mutlugun@analog.com>
2025-06-19 09:36:42 +02:00
Peter Mitsis
2f2eaf7b6f arch: xtensa: Update arch_user_string_nlen()
When calling device_get_binding(NULL) from userspace, this eventually
funnels down to a call to arch_user_string_nlen() where it tried to
verify that the kernel has access to this address (0x0).  But since
this originates from userspace, we really want to know if this is
accessible from userspace, so using arch_buffer_validate() instead
of xtensa_mem_kernel_has_access() is preferable.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-06-19 00:03:00 +02:00
Peter Mitsis
dde9462666 arch: tweak xtensa_mem_kernel_has_access() API
Adds 'const' to address pointer as its memory contents
do not change.

Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
2025-06-19 00:03:00 +02:00
Daniel Leung
917bc51d2d xtensa: gdbstub: add arch_gdb_post_memory_write()
This adds arch_gdb_post_memory_write() to deal with caching
after GDB writing to memory.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-06-17 16:27:27 -05:00
Daniel Leung
733305293a xtensa: gdbstub: fix backtracing and stack unwinding
This fixes GDB backtracing by forcibly spilling all registers,
and faking values for WINDOWSTART and WINDOWBASE. This is
effectively telling GDB that only A0-A3 and AR0-AR3 contain
active data and other physical registers do not. GDB then must
rely on spilled values on stack. Otherwise, GDB will try to
look at all AR* registers for previous frame(s). Since we
do not save all AR* register values, there is nothing for GDB
to look at, and thus failing to unwind stack.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-06-17 16:27:27 -05:00
Daniel Leung
c7841ce472 xtensa: gdbstub: fix compiler warning about discarding const
Inside copy_to_ctx(), we are typecasting the stack pointer into
a 32-bit array pointer, and there was unbalanced const between
two sides. Since we should not be modifying anything inside
that array, add const to the 32-bit array pointer too. So now
the compiler will not complain about discarding the const
qualifier.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-06-17 16:27:27 -05:00
Daniel Leung
d00095c4a6 xtensa: fix num_high_regs calculation when dumping stack
The calculation of number of high registers is not entirely
correct. We need to get past the pointer to BSA in the stack
frame before reaching the high registers. The location address
difference between the BSA and start of high registers then
can be used to calculate how many high registers in the stack
frame. So correct the start location of high registers in
the calculation as it was incorrect before. Though the result
would be the same as further divisions would mask this error.
However, it is better to correct this for readability.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-06-17 16:27:27 -05:00
Guennadi Liakhovetski
53a179ac4e llext: make unresolved symbol errors fatal
With experience it becomes clear, that failing to resolve symbols
during the linking process is likely fatal for the module loading and
a simple warning isn't enough. Fail loading instead.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2025-06-17 16:09:50 +02:00
Daniel Leung
d31ee53b60 xtensa: allow flushing auto-refill DTLBs on page table swap
This adds a new kconfig and corresponding code to allow flushing
auto-refill data TLBs when page tables are swapped (e.g. during
context switching). This is mainly used to avoid multi-hit TLB
exception raised by certain memory access pattern. If memory is
only marked for user mode access but not inside a memory domain,
accessing that page in kernel mode would result in a TLB being
filled with kernel ASID. When going back into user mode, access
to the memory would result in another TLB being filled with
the user mode ASID. Now there are two entries on the same memory
page, and the multi-hit TLB exception will be raised if that
memory page is accessed. This type of access is better served
using memory partition and memory domain to share data. However,
this type of access is not prohibited but highly discouraged.
Wrapping the code in kconfig is simply because of the execution
penalty as there will be unnecessary TLB refilling being done.
So only enable this if necessary.

Fixes #88772

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-05-28 20:01:58 +02:00
Anas Nashif
2aacbcaab5 style: add missing curly braces in if/while/for statements.
Add missing curly braces in if/while/for statements.

This is a style guideline we have that was not enforced in CI. All
issues fixed here were detected by sonarqube SCA.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2025-05-17 14:10:33 +02:00
Guennadi Liakhovetski
0a919055e6 xtensa: gdbstub: fix stack calculation
Fix a logic error when calculating stack frame sizes.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2025-05-13 18:38:12 +01:00
Guennadi Liakhovetski
5769c4d565 xtensa: gdbstub: fix compilation
Building gdbstub for xtensa is failing currently with multiple
failures like

arch/xtensa/core/gdbstub.c:432:24: error: invalid operands to \
binary - (have 'int *' and 'const struct arch_esf *')
  432 |         if ((int *)bsa - stack > 4) {

Fix them by using appropriate pointer types.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2025-05-13 18:38:12 +01:00
Daniel Leung
b6be487394 xtensa: update HAL path for custom compilations
Xtensa arch layer has some custom compilation commands to
generate the interrupt dispatchers and the core-isa* files.
However, the include path to find core-isa.h does not work
for Espressif ESP32. So update the mechanism to use correct
path pointing to Espressif HAL when targeting ESP32 family
SoCs.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-05-09 08:23:09 +02:00
Daniel Leung
9cadc8cbec xtensa: userspace: use ADDX4 to calculate syscall table index
When looking for jump address in the syscall table, we need to
multiply the syscall ID by 4 before adding the address offset
of the beginning of the table. This is due to the jump address
being 32-bit (4 bytes). Instead of using two instructions to
shift the ID by 4 first and then the addition, we can use one
ADDX4 instruction to achieve the same result.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-04-17 00:57:19 +02:00
Daniel Leung
277fa9e8ac xtensa: userspace: swap page tables via assembly code
Since the necessary register values are now pre-computed and
stored in the memory domain struct, we can use them directly
in various assembly locations, thus replacing the function
call to xtensa_swap_update_page_tables().

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-04-17 00:57:19 +02:00
Daniel Leung
2abf7ce2f1 xtensa: userspace: prevent potential privilege escalation
When context switching and dealing with non-nested interrupts,
the context to be restored are saved in the thread stack.
When userspace is enabled, this means saving context into
the user stacks for user threads. This allows PS values to be
manipulated externally by setting PS.RING in the saved PS
value to 0, resulting in granting kernel access privilege when
the thread is restored. To prevent this, we store the PS value
into the thread struct instead, where user threads cannot
manipulate that. Note that nested interrupts and syscalls are
not using the user stack but the interrupt stack and thread
privileged stack respectively, where they are not accessible
under user mode.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-04-17 00:57:19 +02:00
Daniel Leung
d5c2e4b03c xtensa: userspace: workaround return PC calc with loops
When syscall assembly is executed, the EPC points to the syscall
instruction, and we have to manually advance it so we will
return to the instruction after syscall to continue execution.
However, with zero-overhead loops and the syscall instruction is
the last instruction, this simple addition does not work as it
would point past the loop and would have skipped the loop.
Because of this, syscall entrance would need to look at the loop
registers and set the PC back to the beginning of loop if we are
still looping. Assuming most of the syscalls are not inside
loops, the extra handling code consumes quite a few cycles.
To workaround this, simply adds a nop after syscall so we no
longer have to deal with loops at syscall entrance, and that
a nop is faster than all the code to manipulate loop registers.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-04-17 00:57:19 +02:00
Daniel Leung
bdb5723f28 xtensa: userspace: remove ununsed syscall helpers
Syscall helpers with 0, 1, 2, and 3 number of args are not
used so remove them.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-04-17 00:57:19 +02:00
Daniel Leung
b0693ee41b xtensa: userspace: handle privileged stack in arch_cohere_stacks
This extends arch_cohere_stacks() to handle privileged stacks of
user threads when userspace is enabled.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-04-17 00:57:19 +02:00
Daniel Leung
9ebe16b6e7 xtensa: no need to restore A0/A1 if not coherence in irq exit
Towards the end of interrupt handling, and before restoring
context, we would spill all register windows. This requires
A0 and A1 to be restored from the saved context so spilling
would work correct. However, when coherence is enabled,
window spilling has already been done earlier so there is
no need to spill the register windows again. So there is
no need to restore A0 and A1. They will be restored again
before returning from interrupt anyway.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-04-17 00:57:19 +02:00
Daniel Leung
0dce0bdc0b xtensa: userspace: pre-compute MMU registers at domain init
Instead of computing all the needed register values when
swapping page tables, we can pre-compute those values when
the memory domain is first initialized. Should save some
time during context switching.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-04-17 00:57:19 +02:00
Daniel Leung
36064939d3 xtensa: no need for flush register if threads are pin only
There is no need for ZSR_FLUSH when threads are pin only
(CONFIG_SCHED_CPU_MASK_PIN_ONLY=y), so there is no need to
reserve it.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-04-17 00:57:19 +02:00
Daniel Leung
50abe89970 xtensa: userspace: use ZSR_RFI_LEVEL for userspace enter
In xtensa_userspace_enter, we were hard-coding to use level 2
interrupt return mechanism to pivot to user mode and start
running the user thread. However, EPC2 and EPS2 may be used
for other purposes, and they could be used for interrupt
return if there are only two level interrupts. So change
the userspace enter to use ZSR_RFI_LEVEL, ZSR_EPC and ZSR_EPS
instead to be more explicit.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-04-17 00:57:19 +02:00
Daniel Leung
af5dfff3ae xtensa: userspace: use ZSR_RFI_LEVEL for syscall return
When returning from syscall, we cannot use RFE by using EPC1.
As there is no EPS1, we would need to write to PS before
returning. However, this creates a situation where interrupts
are being enabled (since PS is set), and any interrupts will
overwrite EPC1 before we return (which ensures chaos as we
would be returning to the wrong address). So utilize the same
mechanism as interrupt returning by use ZSR_EPS, ZSR_EPC and
ZSR_RFI_LEVEL.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-04-17 00:57:19 +02:00
Daniel Leung
514cd38431 xtensa: userspace: remove jump to _syscall_returned
There is no need to do a jump to _syscall_returned as it is
the next to be run anyway. Keep the label there so we can
set breakpoint if needed.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-04-17 00:57:19 +02:00
Daniel Leung
7f028e2f6e xtensa: userspace: align stack before calling syscall handler
We stuff the 7th argument into stack by moving the stack pointer
before calling syscall handler. The Xtensa ABI says stack must
be 16-byte aligned. So instead of moving stack pointer 4 bytes,
we move 16 bytes (assuming stack has been aligned so far).

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2025-04-17 00:57:19 +02:00