Skip to content

Commit 9c664e4

Browse files
committed
Improve port robustness, portability, and CSR handling
- replaced constants with named macros - fixed TX_RESTORE logic to correctly clear/set bits and used read-modify-write patterns for mstatus. - made some cleanups, formatting to better readability Signed-off-by: Akif Ejaz <[email protected]>
1 parent 12dac1d commit 9c664e4

File tree

8 files changed

+660
-67
lines changed

8 files changed

+660
-67
lines changed

ports/risc-v64/gnu/inc/tx_port.h

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@
7171
#endif
7272
#define REGBYTES (1 << LOG_REGBYTES)
7373

74+
/* RISC-V mstatus bit definitions */
75+
#ifndef MSTATUS_MIE
76+
#define MSTATUS_MIE (1UL << 3) /* Machine interrupt enable */
77+
#define MSTATUS_MPIE (1UL << 7) /* Machine previous interrupt enable */
78+
#define MSTATUS_MPP_SHIFT 11
79+
#define MSTATUS_MPP_MASK (3UL << MSTATUS_MPP_SHIFT)
80+
#define MSTATUS_MPP_MACHINE (3UL << MSTATUS_MPP_SHIFT) /* MPP = Machine */
81+
#define MSTATUS_FS_INITIAL (1UL << 13) /* FP/FS state seed used by port */
82+
#endif
83+
7484
#else /*not __ASSEMBLER__ */
7585

7686
/* Include for memset. */
@@ -141,8 +151,13 @@ typedef unsigned short USHORT;
141151

142152
/* Define various constants for the ThreadX RISC-V port. */
143153

144-
#define TX_INT_DISABLE 0x00000000 /* Disable interrupts value */
145-
#define TX_INT_ENABLE 0x00000008 /* Enable interrupt value */
154+
/* This port assumes execution in Machine mode (M-mode). mstatus bits
155+
are manipulated directly for interrupt control. For S-mode ports,
156+
sstatus and SIE/SPIE semantics must be used instead. See the
157+
RISC-V Privileged Spec for details. */
158+
159+
#define TX_INT_DISABLE 0x00000000UL /* Disable interrupts value */
160+
#define TX_INT_ENABLE MSTATUS_MIE /* Enable interrupt value */
146161

147162

148163
/* Define the clock source for trace event entry time stamp. The following two item are port specific.
@@ -253,23 +268,24 @@ typedef unsigned short USHORT;
253268
is used to define a local function save area for the disable and restore
254269
macros. */
255270

256-
#ifdef TX_DISABLE_INLINE
271+
/* Expose helper used to perform an atomic read/modify/write of mstatus.
272+
The helper composes and returns the posture per ThreadX contract. */
273+
UINT _tx_thread_interrupt_control(UINT new_posture);
257274

258-
ULONG64 _tx_thread_interrupt_control(unsigned int new_posture);
275+
#ifdef TX_DISABLE_INLINE
259276

260-
#define TX_INTERRUPT_SAVE_AREA register ULONG64 interrupt_save;
277+
#define TX_INTERRUPT_SAVE_AREA register UINT interrupt_save;
261278

262279
#define TX_DISABLE interrupt_save = _tx_thread_interrupt_control(TX_INT_DISABLE);
263280
#define TX_RESTORE _tx_thread_interrupt_control(interrupt_save);
264281

265282
#else
266283

267-
#define TX_INTERRUPT_SAVE_AREA ULONG64 interrupt_save;
268-
/* Atomically read mstatus into interrupt_save and clear bit 3 of mstatus. */
269-
#define TX_DISABLE {__asm__ ("csrrci %0, mstatus, 0x08" : "=r" (interrupt_save) : );};
270-
/* We only care about mstatus.mie (bit 3), so mask interrupt_save and write to mstatus. */
271-
#define TX_RESTORE {register ULONG64 __tempmask = interrupt_save & 0x08; \
272-
__asm__ ("csrrs x0, mstatus, %0 \n\t" : : "r" (__tempmask) : );};
284+
/* Default inline macros delegate to the portable helper to ensure
285+
correct read/modify/write semantics across toolchains. */
286+
#define TX_INTERRUPT_SAVE_AREA UINT interrupt_save;
287+
#define TX_DISABLE interrupt_save = _tx_thread_interrupt_control(TX_INT_DISABLE);
288+
#define TX_RESTORE _tx_thread_interrupt_control(interrupt_save);
273289

274290
#endif
275291

0 commit comments

Comments
 (0)