Debian Codegraph @

libgo/go/runtime/mpagealloc_32bit.go

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