From: Joel Sing Date: Tue, 22 Jul 2025 15:26:56 +0000 (+0000) Subject: runtime: identify virtual memory layout for riscv64 X-Git-Tag: go1.26rc1~1019 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=dae9e456ae6992b3c19a8c5039090ee9ca4d9b7d;p=gostls13.git 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 Reviewed-by: Meng Zhuo Reviewed-by: Jorropo Reviewed-by: Dmitri Shuralyov LUCI-TryBot-Result: Go LUCI --- diff --git a/src/runtime/malloc.go b/src/runtime/malloc.go index d21b2c49b5..29e41147f0 100644 --- a/src/runtime/malloc.go +++ b/src/runtime/malloc.go @@ -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