2

So in risc-v, for a virtual memory system, I imagine it's up to the kernel to decide if 0 is a valid memory address or not? But for machine mode, or supervisor mode, is memory address 0 valid to access?

By extent, should I check if the pointer to the device tree provided to the kernel is NULL?

8
  • 1
    If you're using virtual memory for user-space, normally kernel / supervisor mode will also use virtual memory. So you'd just make sure the PTE (page table entry) for the zero page marks it as invalid for both user and supervisor. Without paging, you'd need something else to mark memory regions as unusable to make sure null derefs fault noisly instead of silently succeeding. (Or even allow zero-page addresses to be valid if the alternative is leaving some RAM unused.) Commented Mar 15 at 22:29
  • @PeterCordes are you sure that if user-space uses virtual memory, so does the supervisor mode? From what I've learnt, this isn't true on risc-v architecture. Commented Mar 17 at 16:55
  • 1
    @CocytusDEDI on RISC-V machines, supervisor mode always uses virtual memory. Commented Mar 20 at 10:46
  • 1
    @CocytusDEDI: Almost certainly it does what OSes are designed around, which is having the kernel reserve part of virtual address-space for itself. (Typically the high half). And supporting a bit in the page table entries that marks an entry as being supervisor-only or also valid for user-space. All user-space page directories can point to the same tree of kernel mappings. (In some ISAs such as x86, there's a PTE bit that allows TLBs to keep an entry cached across changes to the top-level page-table pointer. Ideal for this use-case where the same kernel mappings are part of all userspace.) Commented Mar 22 at 13:54
  • 1
    I was talking about how paging works when it's enabled. If any part of address-space is virtual, the whole address-space is virtual. x86 is the same way, there's a control-register bit that enables paging (in 32-bit legacy protected mode; 64-bit mode requires paging be enabled.) It makes sense to have paging be optional especially for embedded use-cases which might not want it, and it makes booting simpler if it can leave setup of initial page-tables to software instead of requiring the initial power-on state to have a valid mapping or TLB entry. Commented Apr 22 at 17:03

1 Answer 1

1

By extent, should I check if the pointer to the device tree provided to the kernel is NULL?

I don't think the RISC-V specification per se specifies which addresses might be valid to access when the kernel boots. This information must be hardcoded into the kernel, or detected by probing the hardware or BIOS somehow, or provided by the device tree itself. In that last case it is impossible to sanitize the device tree address, so don't. In the other cases I don't think it's worth the effort; I would simply allow whatever happens when you access invalid memory to happen.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.