gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/devices/tpuproxy/tpu_mmap.go (about)

     1  // Copyright 2024 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 tpuproxy
    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/log"
    22  	"gvisor.dev/gvisor/pkg/safemem"
    23  	"gvisor.dev/gvisor/pkg/sentry/memmap"
    24  	"gvisor.dev/gvisor/pkg/sentry/vfs"
    25  )
    26  
    27  // ConfigureMMap implements vfs.FileDescriptionImpl.ConfigureMMap.
    28  func (fd *tpuFD) 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 *tpuFD) 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 *tpuFD) 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 *tpuFD) 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 *tpuFD) 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 *tpuFD) InvalidateUnsavable(ctx context.Context) error {
    60  	return nil
    61  }
    62  
    63  type tpuFDMemmapFile struct {
    64  	memmap.NoBufferedIOFallback
    65  
    66  	fd *tpuFD
    67  }
    68  
    69  // IncRef implements memmap.File.IncRef.
    70  func (mf *tpuFDMemmapFile) IncRef(memmap.FileRange, uint32) {
    71  }
    72  
    73  // DecRef implements memmap.File.DecRef.
    74  func (mf *tpuFDMemmapFile) DecRef(fr memmap.FileRange) {
    75  }
    76  
    77  // MapInternal implements memmap.File.MapInternal.
    78  func (mf *tpuFDMemmapFile) MapInternal(fr memmap.FileRange, at hostarch.AccessType) (safemem.BlockSeq, error) {
    79  	log.Traceback("tpuproxy: rejecting tpuFdMemmapFile.MapInternal")
    80  	return safemem.BlockSeq{}, linuxerr.EINVAL
    81  }
    82  
    83  // FD implements memmap.File.FD.
    84  func (mf *tpuFDMemmapFile) FD() int {
    85  	return int(mf.fd.hostFD)
    86  }
    87  
    88  // ConfigureMMap implements vfs.FileDescriptionImpl.ConfigureMMap.
    89  func (fd *pciDeviceFD) ConfigureMMap(ctx context.Context, opts *memmap.MMapOpts) error {
    90  	return vfs.GenericConfigureMMap(&fd.vfsfd, fd, opts)
    91  }
    92  
    93  // AddMapping implements memmap.Mappable.AddMapping.
    94  func (fd *pciDeviceFD) AddMapping(ctx context.Context, ms memmap.MappingSpace, ar hostarch.AddrRange, offset uint64, writable bool) error {
    95  	return nil
    96  }
    97  
    98  // RemoveMapping implements memmap.Mappable.RemoveMapping.
    99  func (fd *pciDeviceFD) RemoveMapping(ctx context.Context, ms memmap.MappingSpace, ar hostarch.AddrRange, offset uint64, writable bool) {
   100  }
   101  
   102  // CopyMapping implements memmap.Mappable.CopyMapping.
   103  func (fd *pciDeviceFD) CopyMapping(ctx context.Context, ms memmap.MappingSpace, srcAR, dstAR hostarch.AddrRange, offset uint64, writable bool) error {
   104  	return nil
   105  }
   106  
   107  // Translate implements memmap.Mappable.Translate.
   108  func (fd *pciDeviceFD) Translate(ctx context.Context, required, optional memmap.MappableRange, at hostarch.AccessType) ([]memmap.Translation, error) {
   109  	return []memmap.Translation{
   110  		{
   111  			Source: optional,
   112  			File:   &fd.memmapFile,
   113  			Offset: optional.Start,
   114  			Perms:  at,
   115  		},
   116  	}, nil
   117  }
   118  
   119  // InvalidateUnsavable implements memmap.Mappable.InvalidateUnsavable.
   120  func (fd *pciDeviceFD) InvalidateUnsavable(ctx context.Context) error {
   121  	return nil
   122  }
   123  
   124  type pciDeviceFdMemmapFile struct {
   125  	memmap.NoBufferedIOFallback
   126  
   127  	fd *pciDeviceFD
   128  }
   129  
   130  // IncRef implements memmap.File.IncRef.
   131  func (mf *pciDeviceFdMemmapFile) IncRef(memmap.FileRange, uint32) {
   132  }
   133  
   134  // DecRef implements memmap.File.DecRef.
   135  func (mf *pciDeviceFdMemmapFile) DecRef(fr memmap.FileRange) {
   136  }
   137  
   138  // MapInternal implements memmap.File.MapInternal.
   139  func (mf *pciDeviceFdMemmapFile) MapInternal(fr memmap.FileRange, at hostarch.AccessType) (safemem.BlockSeq, error) {
   140  	log.Traceback("tpuproxy: rejecting pciDeviceFdMemmapFile.MapInternal")
   141  	return safemem.BlockSeq{}, linuxerr.EINVAL
   142  }
   143  
   144  // FD implements memmap.File.FD.
   145  func (mf *pciDeviceFdMemmapFile) FD() int {
   146  	return int(mf.fd.hostFD)
   147  }