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 MIE/SIE 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 MIE/SIE, as mask.

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

Signed-off-by: Alvin Chang <alvinga@andestech.com>
  • Loading branch information
gagachang committed Oct 11, 2023
1 parent 1b0bd89 commit 3b5bf9d
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 @@ -32,9 +32,12 @@
#include <trace.h>
#include <util.h>

/* 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 @@ -43,6 +46,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 MIE/SIE 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 MIE/SIE. 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 3b5bf9d

Please sign in to comment.