Skip to content

Commit 53f30f3

Browse files
committed
OS/RTThread: initial changes to support RT-Thread running in S-Mode
Take care when running in S-Mode, macro SMODE_RTOS must be defined Signed-off-by: Huaqi Fang <[email protected]>
1 parent 06d28ce commit 53f30f3

File tree

6 files changed

+154
-54
lines changed

6 files changed

+154
-54
lines changed

NMSIS/Core/Include/riscv_encoding.h

+57
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,63 @@
10961096
#define DCAUSE_FAULT_STORE_PMP 0x1
10971097
#define DCAUSE_FAULT_STORE_INST 0x2
10981098

1099+
#ifdef SMODE_RTOS
1100+
#define CSR_XSTATUS CSR_SSTATUS
1101+
#define CSR_XTVEC CSR_STVEC
1102+
#define CSR_XCOUNTEREN CSR_SCOUNTEREN
1103+
#define CSR_XIE CSR_SIE
1104+
#define CSR_XIP CSR_SIP
1105+
#define CSR_XSCRATCH CSR_SSCRATCH
1106+
#define CSR_XEPC CSR_SEPC
1107+
#define CSR_XCAUSE CSR_SCAUSE
1108+
#define CSR_XTVAL CSR_STVAL
1109+
#define CSR_XENVCFG CSR_SENVCFG
1110+
#define CSR_XTVT CSR_STVT
1111+
#define CSR_XTVT2 CSR_STVT2
1112+
#define CSR_XSCRATCHCSWL CSR_SSCRATCHCSWL
1113+
#define CSR_XSCRATCHCSW CSR_SSCRATCHCSW
1114+
#define CSR_XDCAUSE CSR_SDCAUSE
1115+
#define CSR_JALXNXTI CSR_JALSNXTI
1116+
#define CSR_XINTSTATUS CSR_SINTSTATUS
1117+
#define CSR_XNXTI CSR_SNXTI
1118+
#define CSR_PUSHXEPC CSR_PUSHSEPC
1119+
#define CSR_PUSHXCAUSE CSR_PUSHSCAUSE
1120+
#define XRET sret
1121+
#define eclic_xsip_handler eclic_ssip_handler
1122+
#define eclic_xtip_handler eclic_stip_handler
1123+
#define XSTATUS_XIE SSTATUS_SIE
1124+
#define x_exc_entry exc_entry_s
1125+
#define x_irq_entry irq_entry_s
1126+
#else
1127+
#define CSR_XSTATUS CSR_MSTATUS
1128+
#define CSR_XTVEC CSR_MTVEC
1129+
#define CSR_XCOUNTEREN CSR_MCOUNTEREN
1130+
#define CSR_XIE CSR_MIE
1131+
#define CSR_XIP CSR_MIP
1132+
#define CSR_XSCRATCH CSR_MSCRATCH
1133+
#define CSR_XEPC CSR_MEPC
1134+
#define CSR_XCAUSE CSR_MCAUSE
1135+
#define CSR_XSUBM CSR_MSUBM
1136+
#define CSR_XTVAL CSR_MTVAL
1137+
#define CSR_XENVCFG CSR_MENVCFG
1138+
#define CSR_XTVT CSR_MTVT
1139+
#define CSR_XTVT2 CSR_MTVT2
1140+
#define CSR_XSCRATCHCSWL CSR_MSCRATCHCSWL
1141+
#define CSR_XSCRATCHCSW CSR_MSCRATCHCSW
1142+
#define CSR_XDCAUSE CSR_MDCAUSE
1143+
#define CSR_JALXNXTI CSR_JALMNXTI
1144+
#define CSR_XINTSTATUS CSR_MINTSTATUS
1145+
#define CSR_XNXTI CSR_MNXTI
1146+
#define CSR_PUSHXEPC CSR_PUSHMEPC
1147+
#define CSR_PUSHXCAUSE CSR_PUSHMCAUSE
1148+
#define XRET mret
1149+
#define eclic_xsip_handler eclic_msip_handler
1150+
#define eclic_xtip_handler eclic_mtip_handler
1151+
#define XSTATUS_XIE MSTATUS_MIE
1152+
#define x_exc_entry exc_entry
1153+
#define x_irq_entry irq_entry
1154+
#endif
1155+
10991156
/** @} */ /** End of Doxygen Group NMSIS_Core_CSR_Encoding **/
11001157

11011158
#ifdef __cplusplus

OS/RTThread/libcpu/risc-v/nuclei/cpuport.c

+50-9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515

1616
#include "cpuport.h"
1717

18+
#ifndef BOOT_HARTID
19+
#define EXECUTE_HARTID 0
20+
#else
21+
#define EXECUTE_HARTID BOOT_HARTID
22+
#endif
23+
24+
1825
#define SYSTICK_TICK_CONST (SOC_TIMER_FREQ / RT_TICK_PER_SECOND)
1926

2027
#ifndef configKERNEL_INTERRUPT_PRIORITY
@@ -26,7 +33,13 @@
2633
#define configMAX_SYSCALL_INTERRUPT_PRIORITY 255
2734
#endif
2835

29-
#define portINITIAL_MSTATUS ( MSTATUS_MPP | MSTATUS_MPIE | MSTATUS_FS_INITIAL | MSTATUS_VS_INITIAL)
36+
#ifdef SMODE_RTOS
37+
#define SysTick_Handler eclic_stip_handler
38+
#define portINITIAL_XSTATUS ( SSTATUS_SPP | SSTATUS_SPIE | MSTATUS_FS_INITIAL | MSTATUS_VS_INITIAL)
39+
#else
40+
#define SysTick_Handler eclic_mtip_handler
41+
#define portINITIAL_XSTATUS ( MSTATUS_MPP | MSTATUS_MPIE | MSTATUS_FS_INITIAL | MSTATUS_VS_INITIAL)
42+
#endif
3043

3144
volatile rt_ubase_t rt_interrupt_from_thread = 0;
3245
volatile rt_ubase_t rt_interrupt_to_thread = 0;
@@ -64,7 +77,7 @@ struct rt_hw_stack_frame {
6477
rt_ubase_t t5; /* x30 - t5 - temporary register 5 */
6578
rt_ubase_t t6; /* x31 - t6 - temporary register 6 */
6679
#endif
67-
rt_ubase_t mstatus; /* - machine status register */
80+
rt_ubase_t xstatus; /* - m/s status register */
6881
};
6982

7083
/**
@@ -100,7 +113,7 @@ rt_uint8_t* rt_hw_stack_init(void* tentry,
100113
frame->a0 = (rt_ubase_t)parameter;
101114
frame->epc = (rt_ubase_t)tentry;
102115

103-
frame->mstatus = portINITIAL_MSTATUS;
116+
frame->xstatus = portINITIAL_XSTATUS;
104117

105118
return stk;
106119
}
@@ -116,7 +129,15 @@ void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to)
116129

117130
rt_interrupt_to_thread = to;
118131
rt_thread_switch_interrupt_flag = 1;
119-
portYIELD();
132+
/* Set a software interrupt(SWI) request to request a context switch. */
133+
#ifdef SMODE_RTOS
134+
SysTimer_SetHartSWIRQ(EXECUTE_HARTID);
135+
#else
136+
SysTimer_SetSWIRQ();
137+
#endif
138+
/* Barriers are normally not required but do ensure the code is completely
139+
within the specified behaviour for the architecture. */
140+
__RWMB();
120141
}
121142

122143
void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to)
@@ -139,7 +160,11 @@ void rt_hw_cpu_shutdown()
139160
void xPortTaskSwitch(void)
140161
{
141162
/* Clear Software IRQ, A MUST */
163+
#ifdef SMODE_RTOS
164+
SysTimer_ClearHartSWIRQ(EXECUTE_HARTID);
165+
#else
142166
SysTimer_ClearSWIRQ();
167+
#endif
143168
rt_thread_switch_interrupt_flag = 0;
144169
// make from thread to be to thread
145170
// If there is another swi interrupt triggered by other harts
@@ -152,6 +177,18 @@ void vPortSetupTimerInterrupt(void)
152177
{
153178
uint64_t ticks = SYSTICK_TICK_CONST;
154179

180+
#ifdef SMODE_RTOS
181+
SysTick_HartConfig(ticks, EXECUTE_HARTID);
182+
ECLIC_DisableIRQ_S(SysTimer_IRQn);
183+
ECLIC_SetLevelIRQ_S(SysTimer_IRQn, configKERNEL_INTERRUPT_PRIORITY);
184+
ECLIC_SetShvIRQ_S(SysTimer_IRQn, ECLIC_NON_VECTOR_INTERRUPT);
185+
ECLIC_EnableIRQ_S(SysTimer_IRQn);
186+
187+
/* Set SWI interrupt level to lowest level/priority, SysTimerSW as Vector Interrupt */
188+
ECLIC_SetShvIRQ_S(SysTimerSW_IRQn, ECLIC_VECTOR_INTERRUPT);
189+
ECLIC_SetLevelIRQ_S(SysTimerSW_IRQn, configKERNEL_INTERRUPT_PRIORITY);
190+
ECLIC_EnableIRQ_S(SysTimerSW_IRQn);
191+
#else
155192
/* Make SWI and SysTick the lowest priority interrupts. */
156193
/* Stop and clear the SysTimer. SysTimer as Non-Vector Interrupt */
157194
SysTick_Config(ticks);
@@ -164,6 +201,8 @@ void vPortSetupTimerInterrupt(void)
164201
ECLIC_SetShvIRQ(SysTimerSW_IRQn, ECLIC_VECTOR_INTERRUPT);
165202
ECLIC_SetLevelIRQ(SysTimerSW_IRQn, configKERNEL_INTERRUPT_PRIORITY);
166203
ECLIC_EnableIRQ(SysTimerSW_IRQn);
204+
205+
#endif
167206
}
168207

169208

@@ -201,16 +240,18 @@ void rt_hw_board_init()
201240
rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
202241
#endif
203242

204-
__disable_irq();
243+
rt_hw_interrupt_disable();
205244
}
206245

207-
#define SysTick_Handler eclic_mtip_handler
208-
209246
/* This is the timer interrupt service routine. */
210247
void SysTick_Handler(void)
211248
{
212249
// Reload timer
250+
#ifdef SMODE_RTOS
251+
SysTick_HartReload(SYSTICK_TICK_CONST, EXECUTE_HARTID);
252+
#else
213253
SysTick_Reload(SYSTICK_TICK_CONST);
254+
#endif
214255

215256
/* enter interrupt */
216257
rt_interrupt_enter();
@@ -242,10 +283,10 @@ char rt_hw_console_getchar(void)
242283

243284
rt_base_t rt_hw_interrupt_disable(void)
244285
{
245-
return __RV_CSR_READ_CLEAR(CSR_MSTATUS, MSTATUS_MIE);
286+
return __RV_CSR_READ_CLEAR(CSR_XSTATUS, XSTATUS_XIE);
246287
}
247288

248289
void rt_hw_interrupt_enable(rt_base_t level)
249290
{
250-
__RV_CSR_WRITE(CSR_MSTATUS, level);
291+
__RV_CSR_WRITE(CSR_XSTATUS, level);
251292
}

OS/RTThread/libcpu/risc-v/nuclei/cpuport.h

-10
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@
1919
extern "C" {
2020
#endif
2121

22-
/* Scheduler utilities. */
23-
#define portYIELD() \
24-
{ \
25-
/* Set a software interrupt(SWI) request to request a context switch. */ \
26-
SysTimer_SetSWIRQ(); \
27-
/* Barriers are normally not required but do ensure the code is completely \
28-
within the specified behaviour for the architecture. */ \
29-
__RWMB(); \
30-
}
31-
3222

3323
#ifdef __cplusplus
3424
}

OS/RTThread/libcpu/risc-v/nuclei/gcc/context_gcc.S

+14-14
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ rt_hw_context_switch_to:
4343
The stack that was used by main()
4444
before the scheduler is started is
4545
no longer required after the scheduler is started.
46-
Interrupt stack pointer is stored in CSR_MSCRATCH */
46+
Interrupt stack pointer is stored in CSR_XSCRATCH */
4747
la t0, _sp
48-
csrw CSR_MSCRATCH, t0
48+
csrw CSR_XSCRATCH, t0
4949
LOAD sp, 0x0(a0) /* Read sp from first TCB member(a0) */
5050

5151
/* Pop PC from stack and set MEPC */
5252
LOAD t0, 0 * REGBYTES(sp)
53-
csrw CSR_MEPC, t0
53+
csrw CSR_XEPC, t0
5454
/* Pop mstatus from stack and set it */
5555
LOAD t0, (portRegNum - 1) * REGBYTES(sp)
56-
csrw CSR_MSTATUS, t0
56+
csrw CSR_XSTATUS, t0
5757
/* Interrupt still disable here */
5858
/* Restore Registers from Stack */
5959
LOAD x1, 1 * REGBYTES(sp) /* RA */
@@ -89,14 +89,14 @@ rt_hw_context_switch_to:
8989

9090
addi sp, sp, portCONTEXT_SIZE
9191

92-
mret
92+
XRET
9393

9494
.size rt_hw_context_switch_to, . - rt_hw_context_switch_to
9595

9696
.align 2
97-
.global eclic_msip_handler
98-
.type eclic_msip_handler, @function
99-
eclic_msip_handler:
97+
.global eclic_xsip_handler
98+
.type eclic_xsip_handler, @function
99+
eclic_xsip_handler:
100100
addi sp, sp, -portCONTEXT_SIZE
101101
STORE x1, 1 * REGBYTES(sp) /* RA */
102102
STORE x5, 2 * REGBYTES(sp)
@@ -129,7 +129,7 @@ eclic_msip_handler:
129129
STORE x31, 28 * REGBYTES(sp)
130130
#endif
131131
/* Push mstatus to stack */
132-
csrr t0, CSR_MSTATUS
132+
csrr t0, CSR_XSTATUS
133133
STORE t0, (portRegNum - 1) * REGBYTES(sp)
134134

135135
/* Push additional registers */
@@ -138,7 +138,7 @@ eclic_msip_handler:
138138
LOAD t0, rt_interrupt_from_thread
139139
STORE sp, 0(t0)
140140

141-
csrr t0, CSR_MEPC
141+
csrr t0, CSR_XEPC
142142
STORE t0, 0(sp)
143143

144144
jal xPortTaskSwitch
@@ -149,12 +149,12 @@ eclic_msip_handler:
149149

150150
/* Pop PC from stack and set MEPC */
151151
LOAD t0, 0 * REGBYTES(sp)
152-
csrw CSR_MEPC, t0
152+
csrw CSR_XEPC, t0
153153
/* Pop additional registers */
154154

155155
/* Pop mstatus from stack and set it */
156156
LOAD t0, (portRegNum - 1) * REGBYTES(sp)
157-
csrw CSR_MSTATUS, t0
157+
csrw CSR_XSTATUS, t0
158158
/* Interrupt still disable here */
159159
/* Restore Registers from Stack */
160160
LOAD x1, 1 * REGBYTES(sp) /* RA */
@@ -189,6 +189,6 @@ eclic_msip_handler:
189189
#endif
190190

191191
addi sp, sp, portCONTEXT_SIZE
192-
mret
192+
XRET
193193

194-
.size eclic_msip_handler, . - eclic_msip_handler
194+
.size eclic_xsip_handler, . - eclic_xsip_handler

0 commit comments

Comments
 (0)