Skip to content

RISC-V: detect virtual address space at runtime using hwprobe#1296

Open
aurel32 wants to merge 1 commit into
microsoft:mainfrom
aurel32:main
Open

RISC-V: detect virtual address space at runtime using hwprobe#1296
aurel32 wants to merge 1 commit into
microsoft:mainfrom
aurel32:main

Conversation

@aurel32
Copy link
Copy Markdown

@aurel32 aurel32 commented May 18, 2026

The virtual address space on RISC-V is currently detected at build time by parsing /proc/cpuinfo. This works when the binary runs on the same hardware it was built, however running such a binary on a system with a larger address space just causes a segmentation fault.

Replace the build time check with a runtime detection using the hwprobe interface (available since Linux 6.11, commit c9b8cd139c1d "riscv: hwprobe export highest virtual userspace address"). For older kernels, fallback to the default 48 bits. This is suboptimal on SV39 MMUs but still works.

The virtual address space on RISC-V is currently detected at build time
by parsing /proc/cpuinfo. This works when the binary runs on the same
hardware it was built, however running such a binary on a system with a
larger address space just causes a segmentation fault.

Replace the build time check with a runtime detection using the hwprobe
interface (available since Linux 6.11, commit c9b8cd139c1d "riscv:
hwprobe export highest virtual userspace address"). For older kernels,
fallback to the default 48 bits. This is suboptimal on SV39 MMUs but
still works.
@aurel32
Copy link
Copy Markdown
Author

aurel32 commented May 18, 2026

@microsoft-github-policy-service agree

Comment thread CMakeLists.txt

# Check /proc/cpuinfo for an SV39 MMU and limit the virtual address bits.
# (this will skip the aligned hinting in that case. Issue #939, #949)
if (EXISTS /proc/cpuinfo)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Perhaps turn that into an explicit build time option? That way, people targeting an SV39 can at least explicitly request an appropriate number of bits.
(TBF, I have no idea how large that group may be.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

... targeting an SV39 and an older kernel.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I am not a big fan of configure options like that, because people don't really look at it until things are broken. I prefer when things work out of the box, but I understand your concern about supporting older kernels. What about adding a fallback parsing /proc/cpuinfo at runtime for older kernels?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants