github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/pkg/sentry/devices/accel/accel_mmap.go (about)

     1  // Copyright 2023 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 accel
    16  
    17  import (
    18  	"github.com/MerlinKodo/gvisor/pkg/context"
    19  	"github.com/MerlinKodo/gvisor/pkg/errors/linuxerr"
    20  	"github.com/MerlinKodo/gvisor/pkg/hostarch"
    21  	"github.com/MerlinKodo/gvisor/pkg/log"
    22  	"github.com/MerlinKodo/gvisor/pkg/safemem"
    23  	"github.com/MerlinKodo/gvisor/pkg/sentry/memmap"
    24  	"github.com/MerlinKodo/gvisor/pkg/sentry/vfs"
    25  )
    26  
    27  // ConfigureMMap implements vfs.FileDescriptionImpl.ConfigureMMap.
    28  func (fd *accelFD) ConfigureMMap(ctx context.Context, opts *memmap.MMapOpts) error {
    29  	return vfs.GenericConfigureMMap(&fd.vfsfd, fd, opts)
    30  }
    31  
    32  // AddMapping implements memmap.Mappable.AddMapping.
    33  func (fd *accelFD) AddMapping(ctx context.Context, ms memmap.MappingSpace, ar hostarch.AddrRange, offset uint64, writable bool) error {
    34  	return nil
    35  }
    36  
    37  // RemoveMapping implements memmap.Mappable.RemoveMapping.
    38  func (fd *accelFD) RemoveMapping(ctx context.Context, ms memmap.MappingSpace, ar hostarch.AddrRange, offset uint64, writable bool) {
    39  }
    40  
    41  // CopyMapping implements memmap.Mappable.CopyMapping.
    42  func (fd *accelFD) CopyMapping(ctx context.Context, ms memmap.MappingSpace, srcAR, dstAR hostarch.AddrRange, offset uint64, writable bool) error {
    43  	return nil
    44  }
    45  
    46  // Translate implements memmap.Mappable.Translate.
    47  func (fd *accelFD) Translate(ctx context.Context, required, optional memmap.MappableRange, at hostarch.AccessType) ([]memmap.Translation, error) {
    48  	return []memmap.Translation{
    49  		{
    50  			Source: optional,
    51  			File:   &fd.memmapFile,
    52  			Offset: optional.Start,
    53  			Perms:  at,
    54  		},
    55  	}, nil
    56  }
    57  
    58  // InvalidateUnsavable implements memmap.Mappable.InvalidateUnsavable.
    59  func (fd *accelFD) InvalidateUnsavable(ctx context.Context) error {
    60  	return nil
    61  }
    62  
    63  type accelFDMemmapFile struct {
    64  	fd *accelFD
    65  }
    66  
    67  // IncRef implements memmap.File.IncRef.
    68  func (mf *accelFDMemmapFile) IncRef(memmap.FileRange, uint32) {
    69  }
    70  
    71  // DecRef implements memmap.File.DecRef.
    72  func (mf *accelFDMemmapFile) DecRef(fr memmap.FileRange) {
    73  }
    74  
    75  // MapInternal implements memmap.File.MapInternal.
    76  func (mf *accelFDMemmapFile) MapInternal(fr memmap.FileRange, at hostarch.AccessType) (safemem.BlockSeq, error) {
    77  	log.Traceback("accel: rejecting accelFDMemmapFile.MapInternal")
    78  	return safemem.BlockSeq{}, linuxerr.EINVAL
    79  }
    80  
    81  // FD implements memmap.File.FD.
    82  func (mf *accelFDMemmapFile) FD() int {
    83  	return int(mf.fd.hostFD)
    84  }