Commit graph zephyr/arch/xtensa
Author SHA1 Message Date
Daniel Leung
d52060801c xtensa: mpu: xtensa_user_stack_perms to bail with kernel OOPS
If xtensa_user_stack_perms() fails to add stack space to the MPU
map, we must bail as the MPU map would not have the correct
permissions for the thread stack. However, ASSERT() cannot be
used at this point as we are running in privilege stack and yet
the thread has K_USER option set. Logging would go through
the syscall path (due to K_USER bit), and arch_buffer_validate()
on the runtime built assertion string would fail due to it being
in the privilege stack. So use arch_syscall_oops() here to bail
or else we would get cryptic error messages.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-06-23 09:11:38 -04:00
Daniel Leung
0ce82a1138 xtensa: remove xtensa_mem_kernel_has_access
xtensa_mem_kernel_has_access() is no longer being used so remove
it.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-06-23 09:11:38 -04:00
Daniel Leung
99379bbb1e xtensa: mpu: one signature for xtensa_mpu_map_write()
Having different signature for xtensa_mpu_map_write() based on
kconfig is error-prone. So fix that.

Rename xtensa_mpu_map_write() to xtensa_map_thread_map_write()
to reflect this change and to signify that it takes thread
pointer as argument.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-06-23 09:11:38 -04:00
Daniel Leung
55b7fd8f6c xtensa: mpu: no mpu write when setting user stack perms
The userspace code calls xtensa_mpu_map_write() anyway after
calling xtensa_user_stack_perms(), so there is no need to
write the MPU map whiling setting user stack permissions.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-06-23 09:11:38 -04:00
Daniel Leung
e442da3660 xtensa: mpu: rework consolidate_entries()
Rework consolidate_entries() to make it more robust. Now it only
checks a pair of entries at a time to see if one can be removed.
If so, remove one and restart the whole process. The previous
mechanism does not look back at previous entries (as index is
monotonically increasing) and we may miss some opportunities to
further compact the map.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-06-23 09:11:38 -04:00
Daniel Leung
bc8e63c5d9 xtensa: mpu: find_memory_type() goes through whole array
Similar to find_boot_permission(), find_memory_type() needs to
go through the whole xtensa_mpu_mem_type_ranges[] array to find
the memory type. This allows defining a big region earlier and
then gradually having smaller region for finer control.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-06-23 09:11:38 -04:00
Sefa Celik
b6c7c8fff5 xtensa: add dedicated NMI handler
The NMI is edge-triggered and not reflected in the INTERRUPT/INTENABLE
registers. Skip DEF_INT_C_HANDLER() for the NMI level and add a
dedicated xtensa_nmi_c() that directly invokes the registered ISR.
Update assembly vector dispatch to route the NMI level to the new
handler.

Signed-off-by: Sefa Celik <sefa.celik@analog.com>
2026-06-10 09:34:50 +02:00
Daniel Leung
e5a8c21885 xtensa: exc: give magic custom exccause names
We have two custom EXCCAUSE numbers to be used for raising
our own custom exceptions. Instead of having magic numbers
in the code, we give them names.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-06-08 21:30:12 -04:00
Sylvio Alves
65a542bd12 arch: xtensa: initialize secondary cpu interrupt stacks on smp
Initialize interrupt stacks for all secondary CPUs during z_prep_c()
when CONFIG_INIT_STACKS is enabled, preventing uninitialized stack
detection from reporting 100% usage on secondary CPU IRQ stacks.

Signed-off-by: Sylvio Alves <sylvio.alves@espressif.com>
2026-06-08 23:12:42 +02:00
Daniel Leung
bcfa5e42c4 xtensa: mpu: consolidate map if not enough free slots
If there are not enough free slots to add new regions, we first
try to consolidate entries in the map to see if we can free up
some slots.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-06-05 16:57:06 +02:00
Daniel Leung
064bde92a5 xtensa: mpu: consolidate_entries() to return if success
Change consolidate_entries() so that it can return true or false
on whether it is successful in consolidating MPU entries.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-06-05 16:57:06 +02:00
Daniel Leung
679d3fbc5b xtensa: mpu: fix adding to empty MPU map
When adding entries to an empty MPU map, we can simply insert
the necessary entries at the end of the MPU map. There is no
need to consolidate entries or chechking first enabled index.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-06-05 16:57:06 +02:00
Daniel Leung
8eb69ba1c6 xtensa: mpu: optimize arch_mem_domain_thread_add() a bit
There is no need to split the is_migration blocks into two.
They can be done in the same block. And since stack_end_addr
is only used inside the is_migration block, we only need to
calculate the address inside the block.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-06-05 16:57:06 +02:00
Daniel Leung
1ed96c9b49 xtensa: mpu: restore boot perms when removing thread from domain
This looks up the boot permissions in xtensa_mpu_ranges to be
programmed into MPU map when removing a thread from domain.
If the memory region is not described in xtensa_mpu_ranges, we
use the default permissions which only allows kernel read/write
access.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-06-05 16:57:06 +02:00
Daniel Leung
7433d1f286 xtensa: mpu: limit MPU entries sorting to enabled ones
When sorting the entries, we should only consider enabled ones
and skip the disabled one in front. This is more efficient.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-06-05 16:57:06 +02:00
Daniel Leung
b92d7325d9 xtensa: mpu: introduce memory type table
This introduces xtensa_mpu_mem_type_ranges[] to specify
the memory type for different memory regions. This is used to
populate the memory type bits in the MPU entries. The regions
here should correspond to hardware configuration. This is
being introduced as the memory mapping APIs do not have a way
to specify memory type, for example, device MMIO APIs.

This also changes the default memory type to 0x6 as this seems
to be the default in the background map.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-06-05 16:57:06 +02:00
Daniel Leung
58a5ec11ce xtensa: mpu: remove xtensa_soc_mpu_ranges[]
This removes xtensa_soc_mpu_ranges[]. SoC or board needing their
own memory regions should override xtensa_mpu_ranges[] in their
own layer.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-06-05 16:57:06 +02:00
Daniel Leung
acadac7f3e xtensa: mpu: extract the MPU region table to its own file
This refactors the default MPU region table into its own file.
Also declares it to be weak so SoC or board can override it if
needed. Because of this, CONFIG_XTENSA_MPU_ONLY_SOC_RANGES is
removed.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-06-05 16:57:06 +02:00
Daniel Leung
4167d79b71 xtensa: move MPU code into its own directory
This moves the MPU code into its own directory. This is in
preparation to separate out the default memory region table
in its own table.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-06-05 16:57:06 +02:00
Daniel Leung
52a570f308 xtensa: add CONFIG_XTENSA_HIFI5
This adds a kconfig for SoC to indicate that it uses HiFi5
instructions.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-06-03 08:11:13 -05:00
Anas Nashif
d0b389da9e kernel: remove redundant kernel_structs.h includes
kernel.h implies kernel_structs.h via kernel_includes.h, making
explicit inclusion of kernel_structs.h unnecessary whenever kernel.h
is already included in the same translation unit.

Remove the redundant includes across arch, boards, drivers, kernel,
lib, samples, subsys, and tests trees.

in include/zephyr/kernel_structs.h:
 *  2. kernel.h shall imply kernel_structs.h, such that it shall not be
 *    necessary to include kernel_structs.h explicitly when kernel.h is
 *    included.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-06-02 20:24:14 +02:00
Anas Nashif
046a430890 arch: fix various typos
Fix various typos under arch/

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-06-01 12:43:27 -04:00
Frank Li
4dadeb5069 arch/xtensa: Add CPU load support for Xtensa
Add cpu_load support for Xtensa.

Fix an issue in Xtensa tracing where sys_trace_idle_exit
was not called, causing idle time to be collected incorrectly.

Signed-off-by: Frank Li <lgl88911@163.com>
2026-05-31 13:41:30 +02:00
Lauren Murphy
16d69ce7d0 xtensa: unsupported unsigned load / store emulation
Adds Kconfig option
CONFIG_XTENSA_EMULATE_UNSUPPORTED_UNSIGNED_LOAD_STORE
to enable exception handler for unsupported
narrow and / or unaligned unsigned load / stores that
reads the triggering instruction and performs the
operation manually with supported word sized and
aligned accesses.

Signed-off-by: Lauren Murphy <lauren.murphy@intel.com>
Co-authored-by: Anthony Giardina <88748592+agiardin@users.noreply.github.com>
2026-05-21 17:00:57 -04:00
Daniel Leung
3b1bdaf548 xtensa: mpu: fix arch_buffer_validate() if overflow
If the (addr + size) overflows the memory address space,
the inner loop may not run to check for permission. Since
the default return value was 0 (meaning permitted), it
would incorrectly say memory access was okay. Fix this by
changing the default return value to -EINVAL. Only after
the loop of validating the whole input address range then
we set the return value to 0 to say memory access is
permitted. Also check for addition overflow.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-05-15 12:38:17 -05:00
Jamie McCrae
96d1142210 arch: Add support for dts RAM configuration
Allows using the chosen SRAM node for RAM configuration without
using Kconfig values

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
2026-05-11 08:45:38 +02:00
Daniel Leung
6ea8f79b6b xtensa: support L32EX/S32EX for atomic CAS operation
This adds support for using L32EX/S32EX for atomic CAS operation
in the architecture layer. This is an alternative to S32C1I if
the SoC supports it.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-04-27 07:23:53 +02:00
Tahsin Mutlugun
19fa644e34 arch: xtensa: semihost: Account for NULL terminator in path length
Include the terminating NULL character in the path length passed to the
semihosting open call, as required by some debuggers.

Signed-off-by: Tahsin Mutlugun <Tahsin.Mutlugun@analog.com>
2026-04-23 13:12:03 -04:00
Tahsin Mutlugun
a53ab458d5 arch: xtensa: semihost: Add QEMU target support
Add semihosting support for Xtensa QEMU targets. Although QEMU uses the
same simcall instruction, it relies on different flag values for file
open operations compared to Xtensa Instruction Set Simulator.

Prioritize QEMU-specific compatibility if both SIMULATOR_XTENSA and
QEMU_TARGET are enabled.

Signed-off-by: Tahsin Mutlugun <Tahsin.Mutlugun@analog.com>
2026-04-23 13:12:03 -04:00
Tahsin Mutlugun
9a8657193a arch: xtensa: semihost: Add simulator support
Add semihosting for Xtensa Instruction Set Simulator (ISS) via `simcall`
instruction.

Signed-off-by: Tahsin Mutlugun <Tahsin.Mutlugun@analog.com>
2026-04-23 13:12:03 -04:00
Guennadi Liakhovetski
33d43d0933 xtensa: ptables: fix dangling memory domains
When a memory domain is freed on Xtensa, it also has to be removed
from the global domain list. Leaving it on the list can cause
use-after-free exceptions.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2026-04-15 05:51:51 -04:00
Daniel Leung
bac294c90f xtensa: remove mem_manage.c
The custom memory range checks should be implemented in SoC or
board level as these checsk are SoC/board specific. So remove
it from the architecture level.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-04-14 22:22:23 -04:00
Guennadi Liakhovetski
78dcc5e7ce xtensa: (cosmetic) fix a Kconfig entry
select X if Y

in Kconfig entry for Y doesn't make sense. Remove it.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
2026-04-07 15:37:52 -04:00
Daniel Leung
04df4a4aa2 xtensa: correctly flush stack when creating new thread
On a cache incoherent system, we need to make sure the caching
of stack space is properly flushed to memory when creating new
threads. This is especially important if the thread starts
running on the CPU other than the one initializing the thread.
Without flushing, the other CPU would not have the up-to-date
data to correctly start the thread.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-04-03 14:51:56 -05:00
Jyri Sarha
ee0cc5a620 arch: xtensa: Add XTENSA_BACKTRACE_EXCEPTION_DUMP_HOOK Kconfig option
Add XTENSA_BACKTRACE_EXCEPTION_DUMP_HOOK Kconfig option for sending
backtrace through exception dump hook.

This commit also disables the printk backtrace dumping if Kconfig
option EXCEPTION_DUMP_HOOK_ONLY is set.

Signed-off-by: Jyri Sarha <jyri.sarha@linux.intel.com>
2026-03-20 18:20:27 +09:00
Jyri Sarha
9446b09ff2 arch: xtensa: Use exception dump hook helpers in exception dumping
The new exception dump hooks provides helper function for draining or
flushing the accumulated dump data. These helpers are for the backend
to deal intelligently with often excessive amount of data for limited
bandwidth interfaces.

These calls are placed specifically for SOF application, but AFAIK SOF
is the most widely used Zephyr application running on Xtensa.

The helpers do not have any effect if CONFIG_EXCEPTION_DUMP_HOOK is
not set.

Signed-off-by: Jyri Sarha <jyri.sarha@linux.intel.com>
2026-03-20 18:20:27 +09:00
Jyri Sarha
499cdcd51c arch: exception: Add hooks for delivering exception dumps
Add hooks for delivering exception dump prints over a specialized
interface. If CONFING_EXCEPTION_DUMP_HOOK=y then a client program can
set function pointers for printing, flushing, and draining exception
generated prints.

These hooks were implemented for SOF usage, but should be generic
enough to implement alternative exception reporting on any platform.

Signed-off-by: Jyri Sarha <jyri.sarha@linux.intel.com>
2026-03-20 18:20:27 +09:00
Daniel Leung
93a25f7b24 xtensa: set is_fatal_error before stack bound check
is_fatal_error is used to determine whether an exception is
a fatal one. In the default switch case for exception handling,
is_fatal_error needs to be set true. However, setting this
variable was done after stack bound check. So if stack bound
check fails, is_fatal_error is never set. So set the variable
earlier before the stack bound check.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-03-12 09:20:28 -05:00
Jisheng Zhang
9842b062bb cpuidle: optimize out weak stub function call for !TRACING
For !TRACING, most arch_cpu_idle and arch_cpu_atomic_idle implementation
relies on the fact that there's weak stub implementations in
subsys/tracing/tracing_none.c, this works, but the arch_cpu_idle sits in
hot code path, so we'd better to make it as efficient as possible.

Take the riscv implementation for example,
Before the patch:

80000a66 <arch_cpu_idle>:
80000a66:	1141                	addi	sp,sp,-16
80000a68:	c606                	sw	ra,12(sp)
80000a6a:	37c5                	jal	80000a4a <sys_trace_idle>
80000a6c:	10500073          	wfi
80000a70:	3ff1                	jal	80000a4c <sys_trace_idle_exit>
80000a72:	47a1                	li	a5,8
80000a74:	3007a073          	csrs	mstatus,a5
80000a78:	40b2                	lw	ra,12(sp)
80000a7a:	0141                	addi	sp,sp,16
80000a7c:	8082                	ret

NOTE: the sys_trace_idle and sys_trace_idle_exit are just stubs when
!TRACING

after the patch:
80000a62 <arch_cpu_idle>:
80000a62:	10500073          	wfi
80000a66:	47a1                	li	a5,8
80000a68:	3007a073          	csrs	mstatus,a5
80000a6c:	8082                	ret

Signed-off-by: Jisheng Zhang <jszhang@kernel.org>
2026-03-11 23:17:29 -04:00
Daniel Leung
383662a7ca xtensa: use __ASSERT_NO_MSG for assert with no message
Instead of using __ASSERT() with an empty string as message,
simply convert it to use __ASSERT_NO_MSG().

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-02-27 07:59:49 +01:00
Daniel Leung
3032b58f52 xtensa: mmu: support memory domain de-initialization
This supports de-initialization of memory domains to release
allocated page tables back to the pool.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-02-24 10:39:59 +01:00
Daniel Leung
6db2a0fd71 xtensa: mmu: clear L2 table when usage counter reaches zero
When a L2 table is no longer being used, we should set all PTEs
in the table to be illegal PTEs. This is simply a precautious
so that any stray references to the L2 table would not result
in incorrect permissions being applied.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-02-24 10:39:59 +01:00
William Tambe
f97b9ad9e9 xtensa: clear LCOUNT special register after saving it
When handling an ISR (which does not have a context from which to
restore its own value of LCOUNT), we must clear LCOUNT to prevents
incorrect zero-overhead execution if calling a function such as
memmove() which could be implemented using zero-overhead loop.

A function such as memmove() implemented using zero-overhead loop
assumes LCOUNT to have properly been setup before being called; but
an ISR calling memmove() in assembly, will likely not know that.

Signed-off-by: William Tambe <williamt@cadence.com>
2026-02-14 08:53:40 +01:00
Daniel Leung
ee54baf126 Revert "xtensa: move MMU init functions to mmu.c"
This reverts commit 8c02dde437.

For some unknown reasons, xt-clang emits two copies of
z_xt_init_pc if xtensa_mmu_init_paging() is in the same file
as xtensa_mmu_init() and xtensa_mmu_reinit(). So had to
revert the change.

Fixes #103055

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-02-04 13:50:54 +01:00
Daniel Leung
a5a9ff4c28 xtensa: handle instruction TLB multi-hit exception
When using LLEXT, instruction TLB multi-hit becomes a reality
as the same memory space can be occupied by different modules
with different permissions. The ITLB cache may still contain
entries of the unloaded module. So we need to manually
invalidate any cached ITLB corresponding to the exception
address so the TLB associated with the newly loaded module
can be used.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-02-03 09:38:03 +01:00
Daniel Leung
9b9e9ebde7 xtensa: mmu: data TLB multi-hit only handle exception address
There is no need to invalidate the whole auto-refilled data TLB
cache when DTLB multi-hit exception is raised. Now it only
invalidates the TLB entries corresponding to the one causing
the DTLB multi-hit. This allows other non-related TLB entries
to remain in the cache so they don't need to be reloaded.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-02-03 09:38:03 +01:00
Daniel Leung
599df95875 xtensa: return back to interrupted thread for certain exceptions
For both data TLB multi-hit and load/store ring error, we should
return to the interrupted thread immediately so that it can get
past the exception generated code. It is because both of these
exceptions are the result of having cached TLB entries not
aligning to the correct access pattern. So once we have handled
the exception, go back to the interrupted thread to continue
to minimize the chance of having another incompatible TLB being
cached.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-02-03 09:38:03 +01:00
Daniel Leung
27e4802621 xtensa: remove unnecessary setting of is_fatal_error during exc
There is no need for an extra switch block to manipulate
the value of is_fatal_error, which defaults to false, and is set
according to the actual exception above. So remove that.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-02-03 09:38:03 +01:00
Daniel Leung
8c02dde437 xtensa: move MMU init functions to mmu.c
Since we have split source files for page table related stuff
and MMU related stuff, move the MMU initialization functions
from the page table source file into MMU source file.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-01-24 08:48:54 -06:00
Daniel Leung
ed4cec2d22 xtensa: ptables: doxygen doc
This adds doxygen doc to the page table source file as
we are missing quite a bit of documentation there.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-01-24 08:48:54 -06:00