2018-04-27 17:37:02 +00:00
|
|
|
// Copyright 2018 Google Inc.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
package pagetables
|
|
|
|
|
|
|
|
import (
|
|
|
|
"gvisor.googlesource.com/gvisor/pkg/sentry/usermem"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PageTables is a set of page tables.
|
|
|
|
type PageTables struct {
|
2018-06-07 04:47:39 +00:00
|
|
|
// Allocator is used to allocate nodes.
|
|
|
|
Allocator Allocator
|
|
|
|
|
2018-04-27 17:37:02 +00:00
|
|
|
// root is the pagetable root.
|
2018-06-07 04:47:39 +00:00
|
|
|
root *PTEs
|
2018-04-27 17:37:02 +00:00
|
|
|
|
2018-06-07 04:47:39 +00:00
|
|
|
// rootPhysical is the cached physical address of the root.
|
|
|
|
//
|
|
|
|
// This is saved only to prevent constant translation.
|
|
|
|
rootPhysical uintptr
|
2018-04-27 17:37:02 +00:00
|
|
|
|
|
|
|
// archPageTables includes architecture-specific features.
|
|
|
|
archPageTables
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns new PageTables.
|
2018-06-07 04:47:39 +00:00
|
|
|
func New(a Allocator, opts Opts) *PageTables {
|
|
|
|
p := &PageTables{Allocator: a}
|
|
|
|
p.root = p.Allocator.NewPTEs()
|
|
|
|
p.rootPhysical = p.Allocator.PhysicalFor(p.root)
|
2018-04-27 17:37:02 +00:00
|
|
|
p.init(opts)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new set of PageTables derived from the given one.
|
|
|
|
//
|
|
|
|
// This function should always be preferred to New if there are existing
|
|
|
|
// pagetables, as this function preserves architectural constraints relevant to
|
|
|
|
// managing multiple sets of pagetables.
|
2018-06-07 04:47:39 +00:00
|
|
|
func (p *PageTables) New(a Allocator) *PageTables {
|
|
|
|
np := &PageTables{Allocator: a}
|
|
|
|
np.root = np.Allocator.NewPTEs()
|
|
|
|
np.rootPhysical = p.Allocator.PhysicalFor(np.root)
|
2018-04-27 17:37:02 +00:00
|
|
|
np.initFrom(&p.archPageTables)
|
|
|
|
return np
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 aligned, their sum must not overflow.
|
2018-05-30 22:13:36 +00:00
|
|
|
func (p *PageTables) Map(addr usermem.Addr, length uintptr, opts MapOpts, physical uintptr) bool {
|
|
|
|
if !opts.AccessType.Any() {
|
2018-04-27 17:37:02 +00:00
|
|
|
return p.Unmap(addr, length)
|
|
|
|
}
|
|
|
|
prev := false
|
|
|
|
end, ok := addr.AddLength(uint64(length))
|
|
|
|
if !ok {
|
|
|
|
panic("pagetables.Map: overflow")
|
|
|
|
}
|
|
|
|
p.iterateRange(uintptr(addr), uintptr(end), true, func(s, e uintptr, pte *PTE, align uintptr) {
|
|
|
|
p := physical + (s - uintptr(addr))
|
2018-05-30 22:13:36 +00:00
|
|
|
prev = prev || (pte.Valid() && (p != pte.Address() || opts != pte.Opts()))
|
2018-04-27 17:37:02 +00:00
|
|
|
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
|
|
|
|
}
|
2018-05-30 22:13:36 +00:00
|
|
|
pte.Set(p, opts)
|
2018-04-27 17:37:02 +00:00
|
|
|
})
|
|
|
|
return prev
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmap unmaps the given range.
|
|
|
|
//
|
|
|
|
// True is returned iff there was a previous mapping in the range.
|
|
|
|
func (p *PageTables) Unmap(addr usermem.Addr, length uintptr) bool {
|
|
|
|
count := 0
|
|
|
|
p.iterateRange(uintptr(addr), uintptr(addr)+length, false, func(s, e uintptr, pte *PTE, align uintptr) {
|
|
|
|
pte.Clear()
|
|
|
|
count++
|
|
|
|
})
|
|
|
|
return count > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release releases this address space.
|
|
|
|
//
|
|
|
|
// This must be called to release the PCID.
|
|
|
|
func (p *PageTables) Release() {
|
|
|
|
// Clear all pages.
|
|
|
|
p.Unmap(0, ^uintptr(0))
|
|
|
|
p.release()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup returns the physical address for the given virtual address.
|
2018-05-30 22:13:36 +00:00
|
|
|
func (p *PageTables) Lookup(addr usermem.Addr) (physical uintptr, opts MapOpts) {
|
2018-04-27 17:37:02 +00:00
|
|
|
mask := uintptr(usermem.PageSize - 1)
|
|
|
|
off := uintptr(addr) & mask
|
|
|
|
addr = addr &^ usermem.Addr(mask)
|
|
|
|
p.iterateRange(uintptr(addr), uintptr(addr+usermem.PageSize), false, func(s, e uintptr, pte *PTE, align uintptr) {
|
|
|
|
if !pte.Valid() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
physical = pte.Address() + (s - uintptr(addr)) + off
|
2018-05-30 22:13:36 +00:00
|
|
|
opts = pte.Opts()
|
2018-04-27 17:37:02 +00:00
|
|
|
})
|
2018-05-30 22:13:36 +00:00
|
|
|
return
|
2018-04-27 17:37:02 +00:00
|
|
|
}
|