Black Lives Matter. Support the Equal Justice Initiative.

Source file src/runtime/mpagealloc_32bit.go

Documentation: runtime

     1  // Copyright 2019 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 386 || arm || mips || mipsle || wasm || (ios && arm64)
     6  // +build 386 arm mips mipsle wasm ios,arm64
     7  
     8  // wasm is a treated as a 32-bit architecture for the purposes of the page
     9  // allocator, even though it has 64-bit pointers. This is because any wasm
    10  // pointer always has its top 32 bits as zero, so the effective heap address
    11  // space is only 2^32 bytes in size (see heapAddrBits).
    12  
    13  // ios/arm64 is treated as a 32-bit architecture for the purposes of the
    14  // page allocator, even though it has 64-bit pointers and a 33-bit address
    15  // space (see heapAddrBits). The 33 bit address space cannot be rounded up
    16  // to 64 bits because there are too many summary levels to fit in just 33
    17  // bits.
    18  
    19  package runtime
    20  
    21  import "unsafe"
    22  
    23  const (
    24  	// The number of levels in the radix tree.
    25  	summaryLevels = 4
    26  
    27  	// Constants for testing.
    28  	pageAlloc32Bit = 1
    29  	pageAlloc64Bit = 0
    30  
    31  	// Number of bits needed to represent all indices into the L1 of the
    32  	// chunks map.
    33  	//
    34  	// See (*pageAlloc).chunks for more details. Update the documentation
    35  	// there should this number change.
    36  	pallocChunksL1Bits = 0
    37  )
    38  
    39  // See comment in mpagealloc_64bit.go.
    40  var levelBits = [summaryLevels]uint{
    41  	summaryL0Bits,
    42  	summaryLevelBits,
    43  	summaryLevelBits,
    44  	summaryLevelBits,
    45  }
    46  
    47  // See comment in mpagealloc_64bit.go.
    48  var levelShift = [summaryLevels]uint{
    49  	heapAddrBits - summaryL0Bits,
    50  	heapAddrBits - summaryL0Bits - 1*summaryLevelBits,
    51  	heapAddrBits - summaryL0Bits - 2*summaryLevelBits,
    52  	heapAddrBits - summaryL0Bits - 3*summaryLevelBits,
    53  }
    54  
    55  // See comment in mpagealloc_64bit.go.
    56  var levelLogPages = [summaryLevels]uint{
    57  	logPallocChunkPages + 3*summaryLevelBits,
    58  	logPallocChunkPages + 2*summaryLevelBits,
    59  	logPallocChunkPages + 1*summaryLevelBits,
    60  	logPallocChunkPages,
    61  }
    62  
    63  // See mpagealloc_64bit.go for details.
    64  func (p *pageAlloc) sysInit() {
    65  	// Calculate how much memory all our entries will take up.
    66  	//
    67  	// This should be around 12 KiB or less.
    68  	totalSize := uintptr(0)
    69  	for l := 0; l < summaryLevels; l++ {
    70  		totalSize += (uintptr(1) << (heapAddrBits - levelShift[l])) * pallocSumBytes
    71  	}
    72  	totalSize = alignUp(totalSize, physPageSize)
    73  
    74  	// Reserve memory for all levels in one go. There shouldn't be much for 32-bit.
    75  	reservation := sysReserve(nil, totalSize)
    76  	if reservation == nil {
    77  		throw("failed to reserve page summary memory")
    78  	}
    79  	// There isn't much. Just map it and mark it as used immediately.
    80  	sysMap(reservation, totalSize, p.sysStat)
    81  	sysUsed(reservation, totalSize)
    82  
    83  	// Iterate over the reservation and cut it up into slices.
    84  	//
    85  	// Maintain i as the byte offset from reservation where
    86  	// the new slice should start.
    87  	for l, shift := range levelShift {
    88  		entries := 1 << (heapAddrBits - shift)
    89  
    90  		// Put this reservation into a slice.
    91  		sl := notInHeapSlice{(*notInHeap)(reservation), 0, entries}
    92  		p.summary[l] = *(*[]pallocSum)(unsafe.Pointer(&sl))
    93  
    94  		reservation = add(reservation, uintptr(entries)*pallocSumBytes)
    95  	}
    96  }
    97  
    98  // See mpagealloc_64bit.go for details.
    99  func (p *pageAlloc) sysGrow(base, limit uintptr) {
   100  	if base%pallocChunkBytes != 0 || limit%pallocChunkBytes != 0 {
   101  		print("runtime: base = ", hex(base), ", limit = ", hex(limit), "\n")
   102  		throw("sysGrow bounds not aligned to pallocChunkBytes")
   103  	}
   104  
   105  	// Walk up the tree and update the summary slices.
   106  	for l := len(p.summary) - 1; l >= 0; l-- {
   107  		// Figure out what part of the summary array this new address space needs.
   108  		// Note that we need to align the ranges to the block width (1<<levelBits[l])
   109  		// at this level because the full block is needed to compute the summary for
   110  		// the next level.
   111  		lo, hi := addrsToSummaryRange(l, base, limit)
   112  		_, hi = blockAlignSummaryRange(l, lo, hi)
   113  		if hi > len(p.summary[l]) {
   114  			p.summary[l] = p.summary[l][:hi]
   115  		}
   116  	}
   117  }
   118  

View as plain text