2022-12-03 12:07:44 +08:00
|
|
|
/*
|
2023-10-21 20:14:45 +08:00
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
2022-12-03 12:07:44 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
2023-10-21 20:14:45 +08:00
|
|
|
* 2023-10-18 Shell Add backtrace support
|
2022-12-03 12:07:44 +08:00
|
|
|
*/
|
2022-12-10 22:16:42 +08:00
|
|
|
|
2023-10-21 20:14:45 +08:00
|
|
|
#define DBG_TAG "hw.backtrace"
|
|
|
|
#define DBG_LVL DBG_INFO
|
|
|
|
#include <rtdbg.h>
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2023-10-21 20:14:45 +08:00
|
|
|
#include <rtthread.h>
|
|
|
|
#include <mm_aspace.h>
|
|
|
|
#include "riscv_mmu.h"
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2023-10-21 20:14:45 +08:00
|
|
|
#define WORD sizeof(rt_base_t)
|
|
|
|
#define ARCH_CONTEXT_FETCH(pctx, id) (*(((unsigned long *)pctx) + (id)))
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2023-10-21 20:14:45 +08:00
|
|
|
rt_inline rt_err_t _bt_kaddr(rt_ubase_t *fp, struct rt_hw_backtrace_frame *frame)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2023-10-21 20:14:45 +08:00
|
|
|
rt_err_t rc;
|
|
|
|
frame->fp = *(fp - 2);
|
|
|
|
frame->pc = *(fp - 1);
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2023-10-21 20:14:45 +08:00
|
|
|
if ((rt_ubase_t)fp == frame->fp)
|
|
|
|
{
|
|
|
|
rc = -RT_ERROR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rc = RT_EOK;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2023-10-21 20:14:45 +08:00
|
|
|
#ifdef RT_USING_SMART
|
|
|
|
#include <lwp_arch.h>
|
|
|
|
#include <lwp_user_mm.h>
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2023-10-21 20:14:45 +08:00
|
|
|
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)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2023-10-21 20:14:45 +08:00
|
|
|
rc = -RT_EFAULT;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2023-10-21 20:14:45 +08:00
|
|
|
else if (lwp_data_get(lwp, &frame->pc, fp - 1, WORD) != WORD)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2023-10-21 20:14:45 +08:00
|
|
|
rc = -RT_EFAULT;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2023-10-21 20:14:45 +08:00
|
|
|
else if ((rt_ubase_t)fp == frame->fp)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2023-10-21 20:14:45 +08:00
|
|
|
rc = -RT_ERROR;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-21 20:14:45 +08:00
|
|
|
frame->pc -= 0;
|
|
|
|
rc = RT_EOK;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2023-10-21 20:14:45 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#endif /* RT_USING_SMART */
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2023-10-21 20:14:45 +08:00
|
|
|
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))
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2023-10-21 20:14:45 +08:00
|
|
|
#ifdef RT_USING_SMART
|
|
|
|
if (thread->lwp)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2023-10-21 20:14:45 +08:00
|
|
|
void *lwp = thread->lwp;
|
|
|
|
void *this_lwp = lwp_self();
|
|
|
|
if (this_lwp == lwp && rt_hw_mmu_v2p(lwp, 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
|
|
|
|
if ((rt_kmem_v2p(fp) != ARCH_MAP_FAILED))
|
|
|
|
{
|
|
|
|
rc = _bt_kaddr(fp, frame);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rc = -RT_EINVAL;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
|
|
|
}
|
2023-10-21 20:14:45 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
rc = -RT_EFAULT;
|
|
|
|
}
|
|
|
|
return rc;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
|
|
|
|
2023-10-21 20:14:45 +08:00
|
|
|
rt_err_t rt_hw_backtrace_frame_get(rt_thread_t thread, struct rt_hw_backtrace_frame *frame)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2023-10-21 20:14:45 +08:00
|
|
|
rt_err_t rc;
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2023-10-21 20:14:45 +08:00
|
|
|
if (!thread || !frame)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2023-10-21 20:14:45 +08:00
|
|
|
rc = -RT_EINVAL;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-21 20:14:45 +08:00
|
|
|
frame->pc = ARCH_CONTEXT_FETCH(thread->sp, 13);
|
|
|
|
frame->fp = ARCH_CONTEXT_FETCH(thread->sp, 12);
|
|
|
|
rc = RT_EOK;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2023-10-21 20:14:45 +08:00
|
|
|
return rc;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|