最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

pointers - Does risc-v require memory address 0 to be invalid? - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question asked Mar 15 at 17:05 CocytusDEDICocytusDEDI 3001 silver badge10 bronze badges 6
  • 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.) – Peter Cordes 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. – CocytusDEDI Commented Mar 17 at 16:55
  • That's how it works on most architectures, and for example Linux would want it to work that way (otherwise it would have to manually translate user-space pointers passed to system calls like read and stat). But I could easily imagine it being optional on RISC-V. – Peter Cordes Commented Mar 17 at 17:11
  • 1 @CocytusDEDI on RISC-V machines, supervisor mode always uses virtual memory. – droptop 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.) – Peter Cordes Commented Mar 22 at 13:54
 |  Show 1 more comment

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论