Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kernel/riscv64: Clean up the CSR interface a bit + drive-by stuff #25800

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Hendiadyoin1
Copy link
Contributor

Kernel: Add 2 missing includes

Kernel/riscv64: Simplify CSR struct decalarations a bit

The [[gnu::packed]] alignas(u64) where a no-op, and the SATP
comparator is just the default one

Kernel/riscv64: Make the CSR helpers a bit safer/saner and add some more

  • The CSR address is now mandated to be a compile time constant as a
    template argument, so we don't rely on inlining to make it a constant,
    which caused compilation failures in the past.
  • CSR::[set|clear]_bits now acts on enums which makes them a bit
    easier to use.
  • SSTATUS now provides a pre-shifted Bit enum instead of an Offset one
    making that safer/easier to use.
  • SIE reading/writing in now decoupled from the SCAUSE enum, removing
    some bit-fiddling in some places
  • Added the remaining SCAUSE/SIE values

Kernel: Remove redundant cast in page_round_down

It already takes a FlatPtr so no need to c-cast it to one...

The `[[gnu::packed]] alignas(u64)` where a no-op, and the SATP
comparator is just the default one
* The CSR address is now mandated to be a compile time constant as a
template argument, so we don't rely on inlining to make it a constant,
which caused compilation failures in the past.
* `CSR::[set|clear]_bits` now acts on enums which makes them a bit
  easier to use.
* SSTATUS now provides a pre-shifted Bit enum instead of an Offset one
  making that safer/easier to use.
* SIE reading/writing in now decoupled from the SCAUSE enum, removing
  some bit-fiddling in some places
* Added the remaining SCAUSE/SIE values
It already takes a `FlatPtr` so no need to c-cast it to one...
@github-actions github-actions bot added the 👀 pr-needs-review PR needs review from a maintainer or community member label Mar 16, 2025
Copy link
Member

@spholz spholz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for cleaning up the CSR helpers!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in commit message: s/where/were

Comment on lines +62 to +68
template<Address address, Enum E>
requires(IsSame<UnderlyingType<E>, u64>)
ALWAYS_INLINE void set_bits(E bit_mask)
{
asm volatile("csrs %0, %1" ::"i"(address), "Kr"(bit_mask));
}

ALWAYS_INLINE void clear_bits(Address address, FlatPtr bit_mask)
template<Address address, Enum E>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the empty lines between the functions.

ALWAYS_INLINE void set_bits(Address address, FlatPtr bit_mask)
template<Address address, Enum E>
requires(IsSame<UnderlyingType<E>, u64>)
ALWAYS_INLINE void set_bits(E bit_mask)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to use these functions without enums in the future, but I guess we can add an overload for that then.

// 10-12 Reserved
CounterOverflowInterrupt = SCAUSE_INTERRUPT_MASK | 13,
// 14-15 Reserved
// >= 16 Reserved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for platform use

@@ -356,6 +383,8 @@ struct AK::Formatter<Kernel::RISCV64::CSR::SCAUSE> : AK::Formatter<FormatString>
return builder.put_string("Supervisor timer interrupt"sv);
case Kernel::RISCV64::CSR::SCAUSE::SupervisorExternalInterrupt:
return builder.put_string("Supervisor external interrupt"sv);
case Kernel::RISCV64::CSR::SCAUSE::CounterOverflowInterrupt:
return builder.put_string("Counter overflow interrupt"sv);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the exact spelling from the RISC-V ISA manual, so "Counter-overflow interrupt".


ALWAYS_INLINE void set_bits(Address address, FlatPtr bit_mask)
template<Address address, Enum E>
requires(IsSame<UnderlyingType<E>, u64>)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We currently use FlatPtr as the CSR value type everywhere, not u64.


ALWAYS_INLINE void clear_bits(Address address, FlatPtr bit_mask)
template<Address address, Enum E>
requires(IsSame<UnderlyingType<E>, u64>)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
👀 pr-needs-review PR needs review from a maintainer or community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants