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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>