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.
5 //go:build amd64 || arm64 || loong64 || mips64 || mips64le || ppc64 || ppc64le || riscv64 || s390x || wasm
16 // addrBits is the number of bits needed to represent a virtual address.
18 // See heapAddrBits for a table of address space sizes on
19 // various architectures. 48 bits is enough for all
20 // architectures except s390x.
22 // On AMD64, virtual addresses are 48-bit (or 57-bit) numbers sign extended to 64.
23 // We shift the address left 16 to eliminate the sign extended part and make
24 // room in the bottom for the count.
26 // On s390x, virtual addresses are 64-bit. There's not much we
27 // can do about this, so we just hope that the kernel doesn't
28 // get to really high addresses and panic if it does.
31 // In addition to the 16 bits taken from the top, we can take 3 from the
32 // bottom, because node must be pointer-aligned, giving a total of 19 bits
34 tagBits = 64 - addrBits + 3
36 // On AIX, 64-bit addresses are split into 36-bit segment number and 28-bit
37 // offset in segment. Segment numbers in the range 0x0A0000000-0x0AFFFFFFF(LSA)
38 // are available for mmap.
39 // We assume all tagged addresses are from memory allocated with mmap.
40 // We use one bit to distinguish between the two ranges.
42 aixTagBits = 64 - aixAddrBits + 3
44 // riscv64 SV57 mode gives 56 bits of userspace VA.
45 // tagged pointer code supports it,
46 // but broader support for SV57 mode is incomplete,
47 // and there may be other issues (see #54104).
49 riscv64TagBits = 64 - riscv64AddrBits + 3
52 // The number of bits stored in the numeric tag of a taggedPointer
53 const taggedPointerBits = (goos.IsAix * aixTagBits) + (goarch.IsRiscv64 * riscv64TagBits) + ((1 - goos.IsAix) * (1 - goarch.IsRiscv64) * tagBits)
55 // taggedPointerPack created a taggedPointer from a pointer and a tag.
56 // Tag bits that don't fit in the result are discarded.
57 func taggedPointerPack(ptr unsafe.Pointer, tag uintptr) taggedPointer {
59 if GOARCH != "ppc64" {
60 throw("check this code for aix on non-ppc64")
62 return taggedPointer(uint64(uintptr(ptr))<<(64-aixAddrBits) | uint64(tag&(1<<aixTagBits-1)))
64 if GOARCH == "riscv64" {
65 return taggedPointer(uint64(uintptr(ptr))<<(64-riscv64AddrBits) | uint64(tag&(1<<riscv64TagBits-1)))
67 return taggedPointer(uint64(uintptr(ptr))<<(64-addrBits) | uint64(tag&(1<<tagBits-1)))
70 // Pointer returns the pointer from a taggedPointer.
71 func (tp taggedPointer) pointer() unsafe.Pointer {
72 if GOARCH == "amd64" {
73 // amd64 systems can place the stack above the VA hole, so we need to sign extend
74 // val before unpacking.
75 return unsafe.Pointer(uintptr(int64(tp) >> tagBits << 3))
78 return unsafe.Pointer(uintptr((tp >> aixTagBits << 3) | 0xa<<56))
80 if GOARCH == "riscv64" {
81 return unsafe.Pointer(uintptr(tp >> riscv64TagBits << 3))
83 return unsafe.Pointer(uintptr(tp >> tagBits << 3))
86 // Tag returns the tag from a taggedPointer.
87 func (tp taggedPointer) tag() uintptr {
88 return uintptr(tp & (1<<taggedPointerBits - 1))