Source file src/runtime/mpagealloc_64bit.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 amd64 || (!ios && arm64) || mips64 || mips64le || ppc64 || ppc64le || riscv64 || s390x 6 // +build amd64 !ios,arm64 mips64 mips64le ppc64 ppc64le riscv64 s390x 7 8 // See mpagealloc_32bit.go for why ios/arm64 is excluded here. 9 10 package runtime 11 12 import "unsafe" 13 14 const ( 15 // The number of levels in the radix tree. 16 summaryLevels = 5 17 18 // Constants for testing. 19 pageAlloc32Bit = 0 20 pageAlloc64Bit = 1 21 22 // Number of bits needed to represent all indices into the L1 of the 23 // chunks map. 24 // 25 // See (*pageAlloc).chunks for more details. Update the documentation 26 // there should this number change. 27 pallocChunksL1Bits = 13 28 ) 29 30 // levelBits is the number of bits in the radix for a given level in the super summary 31 // structure. 32 // 33 // The sum of all the entries of levelBits should equal heapAddrBits. 34 var levelBits = [summaryLevels]uint{ 35 summaryL0Bits, 36 summaryLevelBits, 37 summaryLevelBits, 38 summaryLevelBits, 39 summaryLevelBits, 40 } 41 42 // levelShift is the number of bits to shift to acquire the radix for a given level 43 // in the super summary structure. 44 // 45 // With levelShift, one can compute the index of the summary at level l related to a 46 // pointer p by doing: 47 // p >> levelShift[l] 48 var levelShift = [summaryLevels]uint{ 49 heapAddrBits - summaryL0Bits, 50 heapAddrBits - summaryL0Bits - 1*summaryLevelBits, 51 heapAddrBits - summaryL0Bits - 2*summaryLevelBits, 52 heapAddrBits - summaryL0Bits - 3*summaryLevelBits, 53 heapAddrBits - summaryL0Bits - 4*summaryLevelBits, 54 } 55 56 // levelLogPages is log2 the maximum number of runtime pages in the address space 57 // a summary in the given level represents. 58 // 59 // The leaf level always represents exactly log2 of 1 chunk's worth of pages. 60 var levelLogPages = [summaryLevels]uint{ 61 logPallocChunkPages + 4*summaryLevelBits, 62 logPallocChunkPages + 3*summaryLevelBits, 63 logPallocChunkPages + 2*summaryLevelBits, 64 logPallocChunkPages + 1*summaryLevelBits, 65 logPallocChunkPages, 66 } 67 68 // sysInit performs architecture-dependent initialization of fields 69 // in pageAlloc. pageAlloc should be uninitialized except for sysStat 70 // if any runtime statistic should be updated. 71 func (p *pageAlloc) sysInit() { 72 // Reserve memory for each level. This will get mapped in 73 // as R/W by setArenas. 74 for l, shift := range levelShift { 75 entries := 1 << (heapAddrBits - shift) 76 77 // Reserve b bytes of memory anywhere in the address space. 78 b := alignUp(uintptr(entries)*pallocSumBytes, physPageSize) 79 r := sysReserve(nil, b) 80 if r == nil { 81 throw("failed to reserve page summary memory") 82 } 83 84 // Put this reservation into a slice. 85 sl := notInHeapSlice{(*notInHeap)(r), 0, entries} 86 p.summary[l] = *(*[]pallocSum)(unsafe.Pointer(&sl)) 87 } 88 } 89 90 // sysGrow performs architecture-dependent operations on heap 91 // growth for the page allocator, such as mapping in new memory 92 // for summaries. It also updates the length of the slices in 93 // [.summary. 94 // 95 // base is the base of the newly-added heap memory and limit is 96 // the first address past the end of the newly-added heap memory. 97 // Both must be aligned to pallocChunkBytes. 98 // 99 // The caller must update p.start and p.end after calling sysGrow. 100 func (p *pageAlloc) sysGrow(base, limit uintptr) { 101 if base%pallocChunkBytes != 0 || limit%pallocChunkBytes != 0 { 102 print("runtime: base = ", hex(base), ", limit = ", hex(limit), "\n") 103 throw("sysGrow bounds not aligned to pallocChunkBytes") 104 } 105 106 // addrRangeToSummaryRange converts a range of addresses into a range 107 // of summary indices which must be mapped to support those addresses 108 // in the summary range. 109 addrRangeToSummaryRange := func(level int, r addrRange) (int, int) { 110 sumIdxBase, sumIdxLimit := addrsToSummaryRange(level, r.base.addr(), r.limit.addr()) 111 return blockAlignSummaryRange(level, sumIdxBase, sumIdxLimit) 112 } 113 114 // summaryRangeToSumAddrRange converts a range of indices in any 115 // level of p.summary into page-aligned addresses which cover that 116 // range of indices. 117 summaryRangeToSumAddrRange := func(level, sumIdxBase, sumIdxLimit int) addrRange { 118 baseOffset := alignDown(uintptr(sumIdxBase)*pallocSumBytes, physPageSize) 119 limitOffset := alignUp(uintptr(sumIdxLimit)*pallocSumBytes, physPageSize) 120 base := unsafe.Pointer(&p.summary[level][0]) 121 return addrRange{ 122 offAddr{uintptr(add(base, baseOffset))}, 123 offAddr{uintptr(add(base, limitOffset))}, 124 } 125 } 126 127 // addrRangeToSumAddrRange is a convienience function that converts 128 // an address range r to the address range of the given summary level 129 // that stores the summaries for r. 130 addrRangeToSumAddrRange := func(level int, r addrRange) addrRange { 131 sumIdxBase, sumIdxLimit := addrRangeToSummaryRange(level, r) 132 return summaryRangeToSumAddrRange(level, sumIdxBase, sumIdxLimit) 133 } 134 135 // Find the first inUse index which is strictly greater than base. 136 // 137 // Because this function will never be asked remap the same memory 138 // twice, this index is effectively the index at which we would insert 139 // this new growth, and base will never overlap/be contained within 140 // any existing range. 141 // 142 // This will be used to look at what memory in the summary array is already 143 // mapped before and after this new range. 144 inUseIndex := p.inUse.findSucc(base) 145 146 // Walk up the radix tree and map summaries in as needed. 147 for l := range p.summary { 148 // Figure out what part of the summary array this new address space needs. 149 needIdxBase, needIdxLimit := addrRangeToSummaryRange(l, makeAddrRange(base, limit)) 150 151 // Update the summary slices with a new upper-bound. This ensures 152 // we get tight bounds checks on at least the top bound. 153 // 154 // We must do this regardless of whether we map new memory. 155 if needIdxLimit > len(p.summary[l]) { 156 p.summary[l] = p.summary[l][:needIdxLimit] 157 } 158 159 // Compute the needed address range in the summary array for level l. 160 need := summaryRangeToSumAddrRange(l, needIdxBase, needIdxLimit) 161 162 // Prune need down to what needs to be newly mapped. Some parts of it may 163 // already be mapped by what inUse describes due to page alignment requirements 164 // for mapping. prune's invariants are guaranteed by the fact that this 165 // function will never be asked to remap the same memory twice. 166 if inUseIndex > 0 { 167 need = need.subtract(addrRangeToSumAddrRange(l, p.inUse.ranges[inUseIndex-1])) 168 } 169 if inUseIndex < len(p.inUse.ranges) { 170 need = need.subtract(addrRangeToSumAddrRange(l, p.inUse.ranges[inUseIndex])) 171 } 172 // It's possible that after our pruning above, there's nothing new to map. 173 if need.size() == 0 { 174 continue 175 } 176 177 // Map and commit need. 178 sysMap(unsafe.Pointer(need.base.addr()), need.size(), p.sysStat) 179 sysUsed(unsafe.Pointer(need.base.addr()), need.size()) 180 } 181 } 182