1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package pagetables provides a generic implementation of pagetables.
//
// The core functions must be safe to call from a nosplit context. Furthermore,
// this pagetables implementation goes to lengths to ensure that all functions
// are free from runtime allocation. Calls to NewPTEs/FreePTEs may be made
// during walks, but these can be cached elsewhere if required.
package pagetables
import (
"gvisor.dev/gvisor/pkg/hostarch"
)
// PageTables is a set of page tables.
type PageTables struct {
// Allocator is used to allocate nodes.
Allocator Allocator
// root is the pagetable root.
//
// For same archs such as amd64, the upper of the PTEs is cloned
// from and owned by upperSharedPageTables which are shared among
// many PageTables if upperSharedPageTables is not nil.
root *PTEs
// rootPhysical is the cached physical address of the root.
//
// This is saved only to prevent constant translation.
rootPhysical uintptr
// archPageTables includes architecture-specific features.
archPageTables
// upperSharedPageTables represents a read-only shared upper
// of the Pagetable. When it is not nil, the upper is not
// allowed to be modified.
upperSharedPageTables *PageTables
// upperStart is the start address of the upper portion that
// are shared from upperSharedPageTables
upperStart uintptr
// readOnlyShared indicates the Pagetables are read-only and
// own the ranges that are shared with other Pagetables.
readOnlyShared bool
}
// Init initializes a set of PageTables.
//
// +checkescape:hard,stack
//go:nosplit
func (p *PageTables) Init(allocator Allocator) {
p.Allocator = allocator
p.root = p.Allocator.NewPTEs()
p.rootPhysical = p.Allocator.PhysicalFor(p.root)
}
// NewWithUpper returns new PageTables.
//
// upperSharedPageTables are used for mapping the upper of addresses,
// starting at upperStart. These pageTables should not be touched (as
// invalidations may be incorrect) after they are passed as an
// upperSharedPageTables. Only when all dependent PageTables are gone
// may they be used. The intenteded use case is for kernel page tables,
// which are static and fixed.
//
// Precondition: upperStart must be between canonical ranges.
// Precondition: upperStart must be pgdSize aligned.
// precondition: upperSharedPageTables must be marked read-only shared.
func NewWithUpper(a Allocator, upperSharedPageTables *PageTables, upperStart uintptr) *PageTables {
p := new(PageTables)
p.Init(a)
if upperSharedPageTables != nil {
if !upperSharedPageTables.readOnlyShared {
panic("Only read-only shared pagetables can be used as upper")
}
p.upperSharedPageTables = upperSharedPageTables
p.upperStart = upperStart
}
p.InitArch(a)
return p
}
// New returns new PageTables.
func New(a Allocator) *PageTables {
return NewWithUpper(a, nil, 0)
}
// mapVisitor is used for map.
type mapVisitor struct {
target uintptr // Input.
physical uintptr // Input.
opts MapOpts // Input.
prev bool // Output.
}
// visit is used for map.
//
//go:nosplit
func (v *mapVisitor) visit(start uintptr, pte *PTE, align uintptr) bool {
p := v.physical + (start - uintptr(v.target))
if pte.Valid() && (pte.Address() != p || pte.Opts() != v.opts) {
v.prev = true
}
if p&align != 0 {
// We will install entries at a smaller granulaity if we don't
// install a valid entry here, however we must zap any existing
// entry to ensure this happens.
pte.Clear()
return true
}
pte.Set(p, v.opts)
return true
}
//go:nosplit
func (*mapVisitor) requiresAlloc() bool { return true }
//go:nosplit
func (*mapVisitor) requiresSplit() bool { return true }
// Map installs a mapping with the given physical address.
//
// True is returned iff there was a previous mapping in the range.
//
// Precondition: addr & length must be page-aligned, their sum must not overflow.
//
// +checkescape:hard,stack
//go:nosplit
func (p *PageTables) Map(addr hostarch.Addr, length uintptr, opts MapOpts, physical uintptr) bool {
if p.readOnlyShared {
panic("Should not modify read-only shared pagetables.")
}
if uintptr(addr)+length < uintptr(addr) {
panic("addr & length overflow")
}
if p.upperSharedPageTables != nil {
// ignore change to the read-only upper shared portion.
if uintptr(addr) >= p.upperStart {
return false
}
if uintptr(addr)+length > p.upperStart {
length = p.upperStart - uintptr(addr)
}
}
w := mapWalker{
pageTables: p,
visitor: mapVisitor{
target: uintptr(addr),
physical: physical,
opts: opts,
},
}
w.iterateRange(uintptr(addr), uintptr(addr)+length)
return w.visitor.prev
}
// unmapVisitor is used for unmap.
type unmapVisitor struct {
count int
}
//go:nosplit
func (*unmapVisitor) requiresAlloc() bool { return false }
//go:nosplit
func (*unmapVisitor) requiresSplit() bool { return true }
// visit unmaps the given entry.
//
//go:nosplit
func (v *unmapVisitor) visit(start uintptr, pte *PTE, align uintptr) bool {
pte.Clear()
v.count++
return true
}
// Unmap unmaps the given range.
//
// True is returned iff there was a previous mapping in the range.
//
// Precondition: addr & length must be page-aligned, their sum must not overflow.
//
// +checkescape:hard,stack
//go:nosplit
func (p *PageTables) Unmap(addr hostarch.Addr, length uintptr) bool {
if p.readOnlyShared {
panic("Should not modify read-only shared pagetables.")
}
if uintptr(addr)+length < uintptr(addr) {
panic("addr & length overflow")
}
if p.upperSharedPageTables != nil {
// ignore change to the read-only upper shared portion.
if uintptr(addr) >= p.upperStart {
return false
}
if uintptr(addr)+length > p.upperStart {
length = p.upperStart - uintptr(addr)
}
}
w := unmapWalker{
pageTables: p,
visitor: unmapVisitor{
count: 0,
},
}
w.iterateRange(uintptr(addr), uintptr(addr)+length)
return w.visitor.count > 0
}
// emptyVisitor is used for emptiness checks.
type emptyVisitor struct {
count int
}
//go:nosplit
func (*emptyVisitor) requiresAlloc() bool { return false }
//go:nosplit
func (*emptyVisitor) requiresSplit() bool { return false }
// visit unmaps the given entry.
//
//go:nosplit
func (v *emptyVisitor) visit(start uintptr, pte *PTE, align uintptr) bool {
v.count++
return true
}
// IsEmpty checks if the given range is empty.
//
// Precondition: addr & length must be page-aligned.
//
// +checkescape:hard,stack
//go:nosplit
func (p *PageTables) IsEmpty(addr hostarch.Addr, length uintptr) bool {
w := emptyWalker{
pageTables: p,
}
w.iterateRange(uintptr(addr), uintptr(addr)+length)
return w.visitor.count == 0
}
// lookupVisitor is used for lookup.
type lookupVisitor struct {
target uintptr // Input & Output.
findFirst bool // Input.
physical uintptr // Output.
size uintptr // Output.
opts MapOpts // Output.
}
// visit matches the given address.
//
//go:nosplit
func (v *lookupVisitor) visit(start uintptr, pte *PTE, align uintptr) bool {
if !pte.Valid() {
// If looking for the first, then we just keep iterating until
// we find a valid entry.
return v.findFirst
}
// Is this within the current range?
v.target = start
v.physical = pte.Address()
v.size = (align + 1)
v.opts = pte.Opts()
return false
}
//go:nosplit
func (*lookupVisitor) requiresAlloc() bool { return false }
//go:nosplit
func (*lookupVisitor) requiresSplit() bool { return false }
// Lookup returns the physical address for the given virtual address.
//
// If findFirst is true, then the next valid address after addr is returned.
// If findFirst is false, then only a mapping for addr will be returned.
//
// Note that if size is zero, then no matching entry was found.
//
// +checkescape:hard,stack
//go:nosplit
func (p *PageTables) Lookup(addr hostarch.Addr, findFirst bool) (virtual hostarch.Addr, physical, size uintptr, opts MapOpts) {
mask := uintptr(hostarch.PageSize - 1)
addr &^= hostarch.Addr(mask)
w := lookupWalker{
pageTables: p,
visitor: lookupVisitor{
target: uintptr(addr),
findFirst: findFirst,
},
}
end := ^hostarch.Addr(0) &^ hostarch.Addr(mask)
if !findFirst {
end = addr + 1
}
w.iterateRange(uintptr(addr), uintptr(end))
return hostarch.Addr(w.visitor.target), w.visitor.physical, w.visitor.size, w.visitor.opts
}
// MarkReadOnlyShared marks the pagetables read-only and can be shared.
//
// It is usually used on the pagetables that are used as the upper
func (p *PageTables) MarkReadOnlyShared() {
p.readOnlyShared = true
}
|