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 // We use one extra bit to placate systems which simulate amd64 binaries on 26 // an arm64 host. Allocated arm64 addresses could be as high as 1<<48-1, 27 // which would be invalid if we assumed 48-bit sign-extended addresses. 28 // See issue 69255. 29 // (Note that this does not help the other way around, simluating arm64 30 // on amd64, but we don't have that problem at the moment.) 31 // 32 // On s390x, virtual addresses are 64-bit. There's not much we 33 // can do about this, so we just hope that the kernel doesn't 34 // get to really high addresses and panic if it does. 35 defaultAddrBits = 48 + 1 36 37 // On AIX, 64-bit addresses are split into 36-bit segment number and 28-bit 38 // offset in segment. Segment numbers in the range 0x0A0000000-0x0AFFFFFFF(LSA) 39 // are available for mmap. 40 // We assume all tagged addresses are from memory allocated with mmap. 41 // We use one bit to distinguish between the two ranges. 42 aixAddrBits = 57 43 44 // Later versions of FreeBSD enable amd64's la57 by default. 45 freebsdAmd64AddrBits = 57 46 47 // riscv64 SV57 mode gives 56 bits of userspace VA. 48 // tagged pointer code supports it, 49 // but broader support for SV57 mode is incomplete, 50 // and there may be other issues (see #54104). 51 riscv64AddrBits = 56 52 53 addrBits = goos.IsAix*aixAddrBits + goarch.IsRiscv64*riscv64AddrBits + goos.IsFreebsd*goarch.IsAmd64*freebsdAmd64AddrBits + (1-goos.IsAix)*(1-goarch.IsRiscv64)*(1-goos.IsFreebsd*goarch.IsAmd64)*defaultAddrBits 54 55 // In addition to the 16 bits (or other, depending on arch/os) taken from the top, 56 // we can take 9 from the bottom, because we require pointers to be well-aligned 57 // (see tagptr.go:tagAlignBits). That gives us a total of 25 bits for the tag. 58 tagBits = 64 - addrBits + tagAlignBits 59 ) 60 61 // taggedPointerPack created a taggedPointer from a pointer and a tag. 62 // Tag bits that don't fit in the result are discarded. 63 func taggedPointerPack(ptr unsafe.Pointer, tag uintptr) taggedPointer { 64 t := taggedPointer(uint64(uintptr(ptr))<<(tagBits-tagAlignBits) | uint64(tag&(1<<tagBits-1))) 65 if t.pointer() != ptr || t.tag() != tag { 66 print("runtime: taggedPointerPack invalid packing: ptr=", ptr, " tag=", hex(tag), " packed=", hex(t), " -> ptr=", t.pointer(), " tag=", hex(t.tag()), "\n") 67 throw("taggedPointerPack") 68 } 69 return t 70 } 71 72 // Pointer returns the pointer from a taggedPointer. 73 func (tp taggedPointer) pointer() unsafe.Pointer { 74 if GOARCH == "amd64" { 75 // amd64 systems can place the stack above the VA hole, so we need to sign extend 76 // val before unpacking. 77 return unsafe.Pointer(uintptr(int64(tp) >> tagBits << tagAlignBits)) 78 } 79 if GOOS == "aix" { 80 return unsafe.Pointer(uintptr((tp >> tagBits << tagAlignBits) | 0xa<<56)) 81 } 82 return unsafe.Pointer(uintptr(tp >> tagBits << tagAlignBits)) 83 } 84 85 // Tag returns the tag from a taggedPointer. 86 func (tp taggedPointer) tag() uintptr { 87 return uintptr(tp & (1<<tagBits - 1)) 88 } 89