]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: identify virtual memory layout for riscv64
authorJoel Sing <joel@sing.id.au>
Tue, 22 Jul 2025 15:26:56 +0000 (15:26 +0000)
committerJoel Sing <joel@sing.id.au>
Tue, 26 Aug 2025 08:38:57 +0000 (01:38 -0700)
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>

src/runtime/malloc.go

index d21b2c49b52271cc8986301d9a6977188a047091..29e41147f0781bed3a92f54300d93377c39ed94b 100644 (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