gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/mm/special_mappable.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package mm
    16  
    17  import (
    18  	"gvisor.dev/gvisor/pkg/context"
    19  	"gvisor.dev/gvisor/pkg/errors/linuxerr"
    20  	"gvisor.dev/gvisor/pkg/hostarch"
    21  	"gvisor.dev/gvisor/pkg/sentry/memmap"
    22  	"gvisor.dev/gvisor/pkg/sentry/pgalloc"
    23  )
    24  
    25  // SpecialMappable implements memmap.MappingIdentity and memmap.Mappable with
    26  // semantics similar to Linux's mm/mmap.c:_install_special_mapping(), except
    27  // that SpecialMappable takes ownership of the memory that it represents
    28  // (_install_special_mapping() does not.)
    29  //
    30  // +stateify savable
    31  type SpecialMappable struct {
    32  	SpecialMappableRefs
    33  
    34  	mf   *pgalloc.MemoryFile `state:"nosave"`
    35  	fr   memmap.FileRange
    36  	name string
    37  }
    38  
    39  // NewSpecialMappable returns a SpecialMappable that owns fr, which represents
    40  // offsets in mfp.MemoryFile() that contain the SpecialMappable's data. The
    41  // SpecialMappable will use the given name in /proc/[pid]/maps.
    42  //
    43  // Preconditions: fr.Length() != 0.
    44  func NewSpecialMappable(name string, mf *pgalloc.MemoryFile, fr memmap.FileRange) *SpecialMappable {
    45  	m := SpecialMappable{mf: mf, fr: fr, name: name}
    46  	m.InitRefs()
    47  	return &m
    48  }
    49  
    50  // DecRef implements refs.RefCounter.DecRef.
    51  func (m *SpecialMappable) DecRef(ctx context.Context) {
    52  	m.SpecialMappableRefs.DecRef(func() {
    53  		m.mf.DecRef(m.fr)
    54  	})
    55  }
    56  
    57  // MappedName implements memmap.MappingIdentity.MappedName.
    58  func (m *SpecialMappable) MappedName(ctx context.Context) string {
    59  	return m.name
    60  }
    61  
    62  // DeviceID implements memmap.MappingIdentity.DeviceID.
    63  func (m *SpecialMappable) DeviceID() uint64 {
    64  	return 0
    65  }
    66  
    67  // InodeID implements memmap.MappingIdentity.InodeID.
    68  func (m *SpecialMappable) InodeID() uint64 {
    69  	return 0
    70  }
    71  
    72  // Msync implements memmap.MappingIdentity.Msync.
    73  func (m *SpecialMappable) Msync(ctx context.Context, mr memmap.MappableRange) error {
    74  	// Linux: vm_file is NULL, causing msync to skip it entirely.
    75  	return nil
    76  }
    77  
    78  // AddMapping implements memmap.Mappable.AddMapping.
    79  func (*SpecialMappable) AddMapping(context.Context, memmap.MappingSpace, hostarch.AddrRange, uint64, bool) error {
    80  	return nil
    81  }
    82  
    83  // RemoveMapping implements memmap.Mappable.RemoveMapping.
    84  func (*SpecialMappable) RemoveMapping(context.Context, memmap.MappingSpace, hostarch.AddrRange, uint64, bool) {
    85  }
    86  
    87  // CopyMapping implements memmap.Mappable.CopyMapping.
    88  func (*SpecialMappable) CopyMapping(context.Context, memmap.MappingSpace, hostarch.AddrRange, hostarch.AddrRange, uint64, bool) error {
    89  	return nil
    90  }
    91  
    92  // Translate implements memmap.Mappable.Translate.
    93  func (m *SpecialMappable) Translate(ctx context.Context, required, optional memmap.MappableRange, at hostarch.AccessType) ([]memmap.Translation, error) {
    94  	var err error
    95  	if required.End > m.fr.Length() {
    96  		err = &memmap.BusError{linuxerr.EFAULT}
    97  	}
    98  	if source := optional.Intersect(memmap.MappableRange{0, m.fr.Length()}); source.Length() != 0 {
    99  		return []memmap.Translation{
   100  			{
   101  				Source: source,
   102  				File:   m.mf,
   103  				Offset: m.fr.Start + source.Start,
   104  				Perms:  hostarch.AnyAccess,
   105  			},
   106  		}, err
   107  	}
   108  	return nil, err
   109  }
   110  
   111  // InvalidateUnsavable implements memmap.Mappable.InvalidateUnsavable.
   112  func (m *SpecialMappable) InvalidateUnsavable(ctx context.Context) error {
   113  	// Since data is stored in pgalloc.MemoryFile, the contents of which are
   114  	// preserved across save/restore, we don't need to do anything.
   115  	return nil
   116  }
   117  
   118  // FileRange returns the offsets into m.mf that stores the SpecialMappable's
   119  // contents.
   120  func (m *SpecialMappable) FileRange() memmap.FileRange {
   121  	return m.fr
   122  }
   123  
   124  // Length returns the length of the SpecialMappable.
   125  func (m *SpecialMappable) Length() uint64 {
   126  	return m.fr.Length()
   127  }