summaryrefslogtreecommitdiff
path: root/libcpu/rh850/common/trap.c
blob: 5a6a3c0cf2f66db2aa98eefbfedb5fd882ce28e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * Copyright (c) 2026, Linumiz
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2026-11-06     Parthiban    first version
 */

#include <stdarg.h>
#include <rthw.h>
#include <rtthread.h>
#include "rh850_cpu.h"

static rt_err_t (*rh850_exception_hook)(void *context);

void rt_hw_exception_install(rt_err_t (*exception_handle)(void *context))
{
    rh850_exception_hook = exception_handle;
}

static void fault_print(const char *fmt, ...)
{
    char buf[96];
    va_list args;

    va_start(args, fmt);
    rt_vsnprintf(buf, sizeof(buf), fmt, args);
    va_end(args);
    rt_hw_console_output(buf);
}

/* While a switch from an interrupt is held back, "from" still runs */
static rt_thread_t running_thread(void)
{
    if (rt_thread_switch_interrupt_flag != 0)
    {
        return rt_container_of((rt_ubase_t *)rt_interrupt_from_thread,
                               struct rt_thread, sp);
    }

    return rt_thread_self();
}

static void __attribute__((noreturn)) fault_halt(void *context)
{
    if (rh850_exception_hook != RT_NULL)
    {
        rh850_exception_hook(context);
    }

    for (;;)
    {
        rh850_halt();
    }
}

void rh850_fault(rt_uint32_t fe, rt_uint32_t cause,
                 struct rt_hw_stack_frame *frame)
{
    rt_thread_t thread = running_thread();
    int n;

    fault_print("\n%s exception: cause 0x%08x, thread %s\n", fe ? "FE" : "EI",
                cause, thread ? thread->parent.name : "-");
    fault_print("pc  0x%08x psw   0x%08x sp 0x%08x\n", frame->eipc,
                frame->eipsw, (rt_ubase_t)(frame + 1));
    fault_print("fpsr 0x%08x fpepc 0x%08x\n", frame->fpsr, frame->fpepc);
    fault_print("r1  0x%08x r2  0x%08x\n", frame->r1, frame->r2);
    for (n = 4; n <= 31; n += 4)
    {
        fault_print("r%-2d 0x%08x r%-2d 0x%08x r%-2d 0x%08x r%-2d 0x%08x\n",
                    n, frame->FRAME_GPR(n), n + 1, frame->FRAME_GPR(n + 1),
                    n + 2, frame->FRAME_GPR(n + 2), n + 3,
                    frame->FRAME_GPR(n + 3));
    }

    fault_halt(frame);
}

void rh850_fpi_pending(rt_ubase_t from)
{
    rt_thread_t thread =
        rt_container_of((rt_ubase_t *)from, struct rt_thread, sp);

    fault_print("\nFPI pending at a thread switch: thread %s, fpepc 0x%08x\n",
                thread->parent.name, RH850_STSR(RH850_SR_FPEPC));

    fault_halt(RT_NULL);
}