Files
rt-thread/libcpu/risc-v/common/backtrace.c
T
GUIandseteiro 8d8dc96b67 [ARCH/RISC-V] Next RISC-V (#10320)
* [libcpu][risc-v] Refactor the RISC-V architecture port

Introduce an opt-in unified RISC-V common layer for RV32 and RV64 while keeping existing BSPs on the legacy implementation during migration.

- use ARCH_RISCV and ARCH_CPU_64BIT for architecture and XLEN selection
- unify context, trap, MMU, cache, timer, SBI, SMP and ISA handling
- add PLIC, CLIC, ACLINT, APLIC and IMSIC support
- map physical hart IDs to contiguous logical CPU IDs
- add T-Head vendor overrides while preserving legacy ports
- migrate qemu-virt64-riscv with RV32 and RV64 support
- update clock_time, MM, LWP, OFW, PCI and Rust integration

Existing BSPs continue using legacy_common or common64 unless they select RT_USING_RISCV_NEW_COMMON.

Signed-off-by: GuEe-GUI <2991707448@qq.com>

* [libcpu][risc-v] skip disabled CPU nodes in early FDT walk

Rbb666 noted that rt_hw_fdt_install_early() walked every /cpus child
without checking status, so disabled cores still joined hart registration
and MMU capability probing. Skip nodes that are not okay/ok via
rt_fdt_device_is_available() before reading reg or mmu-type.

Signed-off-by: seteiro <chenmoushun@163.com>

* [libcpu][risc-v] Fix per-CPU page table mapping

- reuse existing non-leaf page table entries when mapping per-CPU areas
- reject leaf entry conflicts instead of overwriting mappings

Signed-off-by: GuEe-GUI <2991707448@qq.com>

---------

Signed-off-by: GuEe-GUI <2991707448@qq.com>
Signed-off-by: seteiro <chenmoushun@163.com>
Co-authored-by: seteiro <chenmoushun@163.com>
2026-10-17 09:44:26 +08:00

159 lines
3.4 KiB
C

/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-10-18 Shell Add backtrace support
*/
#define DBG_TAG "hw.backtrace"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#include <stack.h>
#include <rtthread.h>
#ifdef ARCH_MM_MMU
#include <mmu.h>
#include <mm_aspace.h>
#endif
#define WORD sizeof(rt_base_t)
#define ARCH_CONTEXT_FETCH(pctx, id) (*(((unsigned long *)pctx) + (id)))
rt_inline rt_err_t _bt_kaddr(rt_ubase_t *fp, struct rt_hw_backtrace_frame *frame)
{
rt_err_t rc;
frame->fp = *(fp - 2);
frame->pc = *(fp - 1);
if ((rt_ubase_t)fp == frame->fp)
{
rc = -RT_ERROR;
}
else
{
rc = RT_EOK;
}
return rc;
}
#ifndef ARCH_MM_MMU
static rt_bool_t _bt_thread_stack_contains(rt_thread_t thread, rt_ubase_t *fp)
{
rt_ubase_t frame_start;
rt_ubase_t stack_start;
rt_ubase_t stack_end;
if (!thread)
{
thread = rt_thread_self();
}
if (!thread || (rt_ubase_t)fp < 2 * WORD)
{
return RT_FALSE;
}
frame_start = (rt_ubase_t)(fp - 2);
stack_start = (rt_ubase_t)thread->stack_addr;
stack_end = stack_start + thread->stack_size;
return frame_start >= stack_start &&
frame_start + 2 * WORD <= stack_end;
}
#endif
#ifdef RT_USING_SMART
#include <lwp_arch.h>
#include <lwp_user_mm.h>
rt_inline rt_err_t _bt_uaddr(rt_lwp_t lwp, rt_ubase_t *fp, struct rt_hw_backtrace_frame *frame)
{
rt_err_t rc;
if (lwp_data_get(lwp, &frame->fp, fp - 2, WORD) != WORD)
{
rc = -RT_EFAULT;
}
else if (lwp_data_get(lwp, &frame->pc, fp - 1, WORD) != WORD)
{
rc = -RT_EFAULT;
}
else if ((rt_ubase_t)fp == frame->fp)
{
rc = -RT_ERROR;
}
else
{
frame->pc -= 0;
rc = RT_EOK;
}
return rc;
}
#endif /* RT_USING_SMART */
rt_err_t rt_hw_backtrace_frame_unwind(rt_thread_t thread, struct rt_hw_backtrace_frame *frame)
{
rt_err_t rc = -RT_ERROR;
rt_ubase_t *fp = (rt_ubase_t *)frame->fp;
if (fp && !((long)fp & 0x7))
{
#ifdef RT_USING_SMART
if (thread && thread->lwp)
{
void *lwp = thread->lwp;
void *this_lwp = lwp_self();
if (this_lwp == lwp && rt_hw_mmu_v2p(((rt_lwp_t)lwp)->aspace, fp) != ARCH_MAP_FAILED)
{
rc = _bt_kaddr(fp, frame);
}
else if (lwp_user_accessible_ext(lwp, (void *)fp, WORD))
{
rc = _bt_uaddr(lwp, fp, frame);
}
else
{
rc = -RT_EFAULT;
}
}
else
#endif /* RT_USING_SMART */
#ifdef ARCH_MM_MMU
if (rt_kmem_v2p(fp) != ARCH_MAP_FAILED)
#else
if (_bt_thread_stack_contains(thread, fp))
#endif
{
rc = _bt_kaddr(fp, frame);
}
else
{
rc = -RT_EINVAL;
}
}
else
{
rc = -RT_EFAULT;
}
return rc;
}
rt_err_t rt_hw_backtrace_frame_get(rt_thread_t thread, struct rt_hw_backtrace_frame *frame)
{
rt_err_t rc;
if (!thread || !frame)
{
rc = -RT_EINVAL;
}
else
{
rt_hw_switch_frame_t sframe = thread->sp;
frame->pc = sframe->regs[RT_HW_SWITCH_CONTEXT_RA];
frame->fp = sframe->regs[RT_HW_SWITCH_CONTEXT_S0];
rc = RT_EOK;
}
return rc;
}