Skip to content

Commit

Permalink
core: riscv: Fix logic of thread_{get/set}_exceptions()
Browse files Browse the repository at this point in the history
In ARM, the bits in DAIF register are used to mask the interrupts. While
in RISC-V, the bits in CSR XIE are used to enable(unmask) corresponding
interrupt sources.

To not modify the function of thread_get_exceptions(), we invert the
bits after reading the value of CSR XIE, as mask.

To not modify the function of thread_set_exceptions(), we invert the
bits in given "exceptions" before writing "exceptions" into CSR
XIE. Therefore, the intended masked exception bits will be cleared
when we write the final value into CSR XIE to mask those interrupts.

Signed-off-by: Alvin Chang <alvinga@andestech.com>
Acked-by: Etienne Carriere <etienne.carriere@foss.st.com>
Acked-by: Jens Wiklander <jens.wiklander@linaro.org>
  • Loading branch information
gagachang committed Oct 13, 2023
1 parent 470aadc commit 7f916ef
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion core/arch/riscv/kernel/thread_arch.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ void __noreturn __panic_at_abi_return(void)
panic();
}

/* This function returns current masked exception bits. */
uint32_t __nostackcheck thread_get_exceptions(void)
{
return read_csr(CSR_XIE) & THREAD_EXCP_ALL;
uint32_t xie = read_csr(CSR_XIE) & THREAD_EXCP_ALL;

return xie ^ THREAD_EXCP_ALL;
}

void __nostackcheck thread_set_exceptions(uint32_t exceptions)
Expand All @@ -52,6 +55,18 @@ void __nostackcheck thread_set_exceptions(uint32_t exceptions)
if (!(exceptions & THREAD_EXCP_FOREIGN_INTR))
assert_have_no_spinlock();

/*
* In ARM, the bits in DAIF register are used to mask the exceptions.
* While in RISC-V, the bits in CSR XIE are used to enable(unmask)
* corresponding interrupt sources. To not modify the function of
* thread_set_exceptions(), we should "invert" the bits in "exceptions".
* The corresponding bits in "exceptions" will be inverted so they will
* be cleared when we write the final value into CSR XIE. So that we
* can mask those exceptions.
*/
exceptions &= THREAD_EXCP_ALL;
exceptions ^= THREAD_EXCP_ALL;

barrier();
write_csr(CSR_XIE, exceptions);
barrier();
Expand Down

0 comments on commit 7f916ef

Please sign in to comment.