runtime: identify virtual memory layout for riscv64

Identify sv39, sv48 and sv57 based on the system stack address.
The current approach to memory allocation is less than ideal on
RISC-V hardware that is using sv39 mode. On sv39 we currently end
up doing around 85 mmap and 66 munmap, since we are trying to map
an unusable range. With this change we do 22 mmap and 0 munmap at
runtime initialisation.

This will also be necessary to support the race detector on sv39.

Updates #64345

Cq-Include-Trybots: luci.golang.try:gotip-linux-riscv64
Change-Id: I4f8ba6763b5ecfedfad5438e025d633820e8265c
Reviewed-on: https://go-review.googlesource.com/c/go/+/690495
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Meng Zhuo <mengzhuo1203@gmail.com>
Reviewed-by: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Joel Sing
2025-07-22 15:26:56 +00:00
parent 25c2d4109f
commit dae9e456ae

View File

@@ -580,6 +580,16 @@ func mallocinit() {
randHeapBasePrefix = byte(randHeapBase >> (randHeapAddrBits - 8))
}
var vmaSize int
if GOARCH == "riscv64" {
// Identify which memory layout is in use based on the system
// stack address, knowing that the bottom half of virtual memory
// is user space. This should result in 39, 48 or 57. It may be
// possible to use RISCV_HWPROBE_KEY_HIGHEST_VIRT_ADDRESS at some
// point in the future - for now use the system stack address.
vmaSize = sys.Len64(uint64(getg().m.g0.stack.hi)) + 1
}
for i := 0x7f; i >= 0; i-- {
var p uintptr
switch {
@@ -598,6 +608,8 @@ func mallocinit() {
p = uintptr(i)<<40 | uintptrMask&(0x0013<<28)
case GOARCH == "arm64":
p = uintptr(i)<<40 | uintptrMask&(0x0040<<32)
case GOARCH == "riscv64" && vmaSize == 39:
p = uintptr(i)<<32 | uintptrMask&(0x0013<<28)
case GOOS == "aix":
if i == 0 {
// We don't use addresses directly after 0x0A00000000000000