Skip to content

Commit 751b9ca

Browse files
committed
NMSIS/Core: add new __s_switch_mode API in core_feature_base.h
Signed-off-by: Huaqi Fang <578567190@qq.com>
1 parent e151221 commit 751b9ca

3 files changed

Lines changed: 32 additions & 2 deletions

File tree

NMSIS/Core/Include/core_feature_base.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,36 @@ __STATIC_INLINE void __switch_mode(uint8_t mode, uintptr_t stack, void(*entry_po
897897
__ASM volatile("mret");
898898
}
899899

900+
/**
901+
* \brief switch privilege from supervisor mode to others.
902+
* \details
903+
* Execute into \ref entry_point in \ref mode(user) with given stack
904+
* \param mode privilege mode
905+
* \param stack predefined stack, size should set enough
906+
* \param entry_point a function pointer to execute
907+
*/
908+
__STATIC_INLINE void __s_switch_mode(uint8_t mode, uintptr_t stack, void(*entry_point)(void))
909+
{
910+
unsigned long val = 0;
911+
912+
/* Set SPP to the requested privilege mode */
913+
val = __RV_CSR_READ(CSR_SSTATUS);
914+
val = __RV_INSERT_FIELD(val, SSTATUS_SPP, mode);
915+
916+
/* Set previous SIE disabled */
917+
val = __RV_INSERT_FIELD(val, SSTATUS_SPIE, 0);
918+
919+
__RV_CSR_WRITE(CSR_SSTATUS, val);
920+
921+
/* Set the entry point in SEPC */
922+
__RV_CSR_WRITE(CSR_SEPC, (unsigned long)entry_point);
923+
924+
/* Set the register file */
925+
__ASM volatile("mv sp, %0" ::"r"(stack));
926+
927+
__ASM volatile("sret");
928+
}
929+
900930
/**
901931
* \brief Enable IRQ Interrupts
902932
* \details Enables IRQ interrupts by setting the MIE-bit in the MSTATUS Register.

NMSIS/Core/Include/core_feature_pmp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ typedef struct PMP_CONFIG {
6464
/**
6565
* Size of memory region as power of 2, it has to be minimum 2 and maxium \ref __RISCV_XLEN according to the
6666
* hard-wired granularity 2^N bytes, if N = 12, then order has to be at least 12; if not, the order read out
67-
* is N though you configure less than N.
67+
* is N though you configure less than N. Generally, Nuclei CPU's pmp granularity is 2^12 = 4KBytes, you should refer to the databook.
6868
*/
6969
unsigned long order;
7070
/**

NMSIS/Core/Include/core_feature_spmp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ typedef struct SPMP_CONFIG {
7070
/**
7171
* Size of memory region as power of 2, it has to be minimum 2 and maxium \ref __RISCV_XLEN according to the
7272
* hardwired granularity 2^N bytes, if N = 12, then order has to be at least 12; if not, the order read out
73-
* is N though you configure less than N.
73+
* is N though you configure less than N. Generally, Nuclei CPU's smpu granularity is 2^12 = 4KBytes, you should refer to the databook.
7474
*/
7575
unsigned long order;
7676
/**

0 commit comments

Comments
 (0)