Source file src/runtime/mpagealloc_32bit.go

     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
     6  
     7  // wasm is a treated as a 32-bit architecture for the purposes of the page
     8  // allocator, even though it has 64-bit pointers. This is because any wasm
     9  // pointer always has its top 32 bits as zero, so the effective heap address
    10  // space is only 2^32 bytes in size (see heapAddrBits).
    11  
    12  package runtime
    13  
    14  import (
    15  	"runtime/internal/atomic"
    16  	"unsafe"
    17  )
    18  
    19  const (
    20  	// The number of levels in the radix tree.
    21  	summaryLevels = 4
    22  
    23  	// Constants for testing.
    24  	pageAlloc32Bit = 1
    25  	pageAlloc64Bit = 0
    26  
    27  	// Number of bits needed to represent all indices into the L1 of the
    28  	// chunks map.
    29  	//
    30  	// See (*pageAlloc).chunks for more details. Update the documentation
    31  	// there should this number change.
    32  	pallocChunksL1Bits = 0
    33  )
    34  
    35  // See comment in mpagealloc_64bit.go.
    36  var levelBits = [summaryLevels]uint{
    37  	summaryL0Bits,
    38  	summaryLevelBits,
    39  	summaryLevelBits,
    40  	summaryLevelBits,
    41  }
    42  
    43  // See comment in mpagealloc_64bit.go.
    44  var levelShift = [summaryLevels]uint{
    45  	heapAddrBits - summaryL0Bits,
    46  	heapAddrBits - summaryL0Bits - 1*summaryLevelBits,
    47  	heapAddrBits - summaryL0Bits - 2*summaryLevelBits,
    48  	heapAddrBits - summaryL0Bits - 3*summaryLevelBits,
    49  }
    50  
    51  // See comment in mpagealloc_64bit.go.
    52  var levelLogPages = [summaryLevels]uint{
    53  	logPallocChunkPages + 3*summaryLevelBits,
    54  	logPallocChunkPages + 2*summaryLevelBits,
    55  	logPallocChunkPages + 1*summaryLevelBits,
    56  	logPallocChunkPages,
    57  }
    58  
    59  // scavengeIndexArray is the backing store for p.scav.index.chunks.
    60  // On 32-bit platforms, it's small enough to just be a global.
    61  var scavengeIndexArray [((1 << heapAddrBits) / pallocChunkBytes) / 8]atomic.Uint8
    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, totalSize)
    82  	p.summaryMappedReady += totalSize
    83  
    84  	// Iterate over the reservation and cut it up into slices.
    85  	//
    86  	// Maintain i as the byte offset from reservation where
    87  	// the new slice should start.
    88  	for l, shift := range levelShift {
    89  		entries := 1 << (heapAddrBits - shift)
    90  
    91  		// Put this reservation into a slice.
    92  		sl := notInHeapSlice{(*notInHeap)(reservation), 0, entries}
    93  		p.summary[l] = *(*[]pallocSum)(unsafe.Pointer(&sl))
    94  
    95  		reservation = add(reservation, uintptr(entries)*pallocSumBytes)
    96  	}
    97  
    98  	// Set up the scavenge index.
    99  	p.scav.index.chunks = scavengeIndexArray[:]
   100  }
   101  
   102  // See mpagealloc_64bit.go for details.
   103  func (p *pageAlloc) sysGrow(base, limit uintptr) {
   104  	if base%pallocChunkBytes != 0 || limit%pallocChunkBytes != 0 {
   105  		print("runtime: base = ", hex(base), ", limit = ", hex(limit), "\n")
   106  		throw("sysGrow bounds not aligned to pallocChunkBytes")
   107  	}
   108  
   109  	// Walk up the tree and update the summary slices.
   110  	for l := len(p.summary) - 1; l >= 0; l-- {
   111  		// Figure out what part of the summary array this new address space needs.
   112  		// Note that we need to align the ranges to the block width (1<<levelBits[l])
   113  		// at this level because the full block is needed to compute the summary for
   114  		// the next level.
   115  		lo, hi := addrsToSummaryRange(l, base, limit)
   116  		_, hi = blockAlignSummaryRange(l, lo, hi)
   117  		if hi > len(p.summary[l]) {
   118  			p.summary[l] = p.summary[l][:hi]
   119  		}
   120  	}
   121  }
   122  

View as plain text