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