Source file src/runtime/tagptr_64bit.go
1 // Copyright 2014 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build amd64 || arm64 || loong64 || mips64 || mips64le || ppc64 || ppc64le || riscv64 || s390x || wasm 6 7 package runtime 8 9 import ( 10 "internal/goarch" 11 "internal/goos" 12 "unsafe" 13 ) 14 15 const ( 16 // addrBits is the number of bits needed to represent a virtual address. 17 // 18 // See heapAddrBits for a table of address space sizes on 19 // various architectures. 48 bits is enough for all 20 // arch/os combos except s390x, aix, and riscv64. 21 // 22 // On AMD64, virtual addresses are 48-bit (or 57-bit) sign-extended. 23 // Other archs are 48-bit zero-extended. 24 // 25 // On s390x, virtual addresses are 64-bit. There's not much we 26 // can do about this, so we just hope that the kernel doesn't 27 // get to really high addresses and panic if it does. 28 defaultAddrBits = 48 29 30 // On AIX, 64-bit addresses are split into 36-bit segment number and 28-bit 31 // offset in segment. Segment numbers in the range 0x0A0000000-0x0AFFFFFFF(LSA) 32 // are available for mmap. 33 // We assume all tagged addresses are from memory allocated with mmap. 34 // We use one bit to distinguish between the two ranges. 35 aixAddrBits = 57 36 37 // Later versions of FreeBSD enable amd64's la57 by default. 38 freebsdAmd64AddrBits = 57 39 40 // riscv64 SV57 mode gives 56 bits of userspace VA. 41 // tagged pointer code supports it, 42 // but broader support for SV57 mode is incomplete, 43 // and there may be other issues (see #54104). 44 riscv64AddrBits = 56 45 46 addrBits = goos.IsAix*aixAddrBits + goarch.IsRiscv64*riscv64AddrBits + goos.IsFreebsd*goarch.IsAmd64*freebsdAmd64AddrBits + (1-goos.IsAix)*(1-goarch.IsRiscv64)*(1-goos.IsFreebsd*goarch.IsAmd64)*defaultAddrBits 47 48 // In addition to the 16 bits (or other, depending on arch/os) taken from the top, 49 // we can take 9 from the bottom, because we require pointers to be well-aligned 50 // (see tagptr.go:tagAlignBits). That gives us a total of 25 bits for the tag. 51 tagBits = 64 - addrBits + tagAlignBits 52 ) 53 54 // taggedPointerPack created a taggedPointer from a pointer and a tag. 55 // Tag bits that don't fit in the result are discarded. 56 func taggedPointerPack(ptr unsafe.Pointer, tag uintptr) taggedPointer { 57 t := taggedPointer(uint64(uintptr(ptr))<<(tagBits-tagAlignBits) | uint64(tag&(1<<tagBits-1))) 58 if t.pointer() != ptr || t.tag() != tag { 59 print("runtime: taggedPointerPack invalid packing: ptr=", ptr, " tag=", hex(tag), " packed=", hex(t), " -> ptr=", t.pointer(), " tag=", hex(t.tag()), "\n") 60 throw("taggedPointerPack") 61 } 62 return t 63 } 64 65 // Pointer returns the pointer from a taggedPointer. 66 func (tp taggedPointer) pointer() unsafe.Pointer { 67 if GOARCH == "amd64" { 68 // amd64 systems can place the stack above the VA hole, so we need to sign extend 69 // val before unpacking. 70 return unsafe.Pointer(uintptr(int64(tp) >> tagBits << tagAlignBits)) 71 } 72 if GOOS == "aix" { 73 return unsafe.Pointer(uintptr((tp >> tagBits << tagAlignBits) | 0xa<<56)) 74 } 75 return unsafe.Pointer(uintptr(tp >> tagBits << tagAlignBits)) 76 } 77 78 // Tag returns the tag from a taggedPointer. 79 func (tp taggedPointer) tag() uintptr { 80 return uintptr(tp & (1<<tagBits - 1)) 81 } 82