Black Lives Matter. Support the Equal Justice Initiative.

Source file src/runtime/lfstack_64bit.go

Documentation: runtime

     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 || mips64 || mips64le || ppc64 || ppc64le || riscv64 || s390x || wasm
     6  // +build amd64 arm64 mips64 mips64le ppc64 ppc64le riscv64 s390x wasm
     7  
     8  package runtime
     9  
    10  import "unsafe"
    11  
    12  const (
    13  	// addrBits is the number of bits needed to represent a virtual address.
    14  	//
    15  	// See heapAddrBits for a table of address space sizes on
    16  	// various architectures. 48 bits is enough for all
    17  	// architectures except s390x.
    18  	//
    19  	// On AMD64, virtual addresses are 48-bit (or 57-bit) numbers sign extended to 64.
    20  	// We shift the address left 16 to eliminate the sign extended part and make
    21  	// room in the bottom for the count.
    22  	//
    23  	// On s390x, virtual addresses are 64-bit. There's not much we
    24  	// can do about this, so we just hope that the kernel doesn't
    25  	// get to really high addresses and panic if it does.
    26  	addrBits = 48
    27  
    28  	// In addition to the 16 bits taken from the top, we can take 3 from the
    29  	// bottom, because node must be pointer-aligned, giving a total of 19 bits
    30  	// of count.
    31  	cntBits = 64 - addrBits + 3
    32  
    33  	// On AIX, 64-bit addresses are split into 36-bit segment number and 28-bit
    34  	// offset in segment.  Segment numbers in the range 0x0A0000000-0x0AFFFFFFF(LSA)
    35  	// are available for mmap.
    36  	// We assume all lfnode addresses are from memory allocated with mmap.
    37  	// We use one bit to distinguish between the two ranges.
    38  	aixAddrBits = 57
    39  	aixCntBits  = 64 - aixAddrBits + 3
    40  )
    41  
    42  func lfstackPack(node *lfnode, cnt uintptr) uint64 {
    43  	if GOARCH == "ppc64" && GOOS == "aix" {
    44  		return uint64(uintptr(unsafe.Pointer(node)))<<(64-aixAddrBits) | uint64(cnt&(1<<aixCntBits-1))
    45  	}
    46  	return uint64(uintptr(unsafe.Pointer(node)))<<(64-addrBits) | uint64(cnt&(1<<cntBits-1))
    47  }
    48  
    49  func lfstackUnpack(val uint64) *lfnode {
    50  	if GOARCH == "amd64" {
    51  		// amd64 systems can place the stack above the VA hole, so we need to sign extend
    52  		// val before unpacking.
    53  		return (*lfnode)(unsafe.Pointer(uintptr(int64(val) >> cntBits << 3)))
    54  	}
    55  	if GOARCH == "ppc64" && GOOS == "aix" {
    56  		return (*lfnode)(unsafe.Pointer(uintptr((val >> aixCntBits << 3) | 0xa<<56)))
    57  	}
    58  	return (*lfnode)(unsafe.Pointer(uintptr(val >> cntBits << 3)))
    59  }
    60  

View as plain text