github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/pkg/abi/gasket/gasket.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 gasket describes the userspace interface for Gasket devices.
    16  package gasket
    17  
    18  import (
    19  	"fmt"
    20  
    21  	"github.com/ttpreport/gvisor-ligolo/pkg/abi/linux"
    22  )
    23  
    24  // Ioctl represents a gasket ioctl command.
    25  type Ioctl uint32
    26  
    27  // From https://github.com/tensorflow/tpu/blob/master/tools/driver/include/linux/google/gasket.h
    28  var (
    29  	GASKET_IOCTL_BASE                   = uint32(0xDC)
    30  	GASKET_IOCTL_RESET                  = Ioctl(linux.IOW(GASKET_IOCTL_BASE, 0, SizeOfUnsignedLong))
    31  	GASKET_IOCTL_SET_EVENTFD            = Ioctl(linux.IOW(GASKET_IOCTL_BASE, 1, SizeofGasketInterruptEventFd))
    32  	GASKET_IOCTL_CLEAR_EVENTFD          = Ioctl(linux.IOW(GASKET_IOCTL_BASE, 2, SizeOfUnsignedLong))
    33  	GASKET_IOCTL_NUMBER_PAGE_TABLES     = Ioctl(linux.IOR(GASKET_IOCTL_BASE, 4, SizeOfUnsignedLong))
    34  	GASKET_IOCTL_PAGE_TABLE_SIZE        = Ioctl(linux.IOWR(GASKET_IOCTL_BASE, 5, SizeofGasketPageTableIoctl))
    35  	GASKET_IOCTL_SIMPLE_PAGE_TABLE_SIZE = Ioctl(linux.IOWR(GASKET_IOCTL_BASE, 6, SizeofGasketPageTableIoctl))
    36  	GASKET_IOCTL_PARTITION_PAGE_TABLE   = Ioctl(linux.IOW(GASKET_IOCTL_BASE, 7, SizeofGasketPageTableIoctl))
    37  	GASKET_IOCTL_MAP_BUFFER             = Ioctl(linux.IOW(GASKET_IOCTL_BASE, 8, SizeofGasketPageTableIoctl))
    38  	GASKET_IOCTL_UNMAP_BUFFER           = Ioctl(linux.IOW(GASKET_IOCTL_BASE, 9, SizeofGasketPageTableIoctl))
    39  	GASKET_IOCTL_CLEAR_INTERRUPT_COUNTS = Ioctl(linux.IO(GASKET_IOCTL_BASE, 10))
    40  	GASKET_IOCTL_REGISTER_INTERRUPT     = Ioctl(linux.IOW(GASKET_IOCTL_BASE, 11, SizeofGasketInterruptMapping))
    41  	GASKET_IOCTL_UNREGISTER_INTERRUPT   = Ioctl(linux.IOW(GASKET_IOCTL_BASE, 12, SizeOfUnsignedLong))
    42  	GASKET_IOCTL_MAP_DMA_BUF            = Ioctl(linux.IOW(GASKET_IOCTL_BASE, 13, SizeofGasketPageTableDmaBufIoctl))
    43  )
    44  
    45  func (i Ioctl) String() string {
    46  	switch i {
    47  	case GASKET_IOCTL_RESET:
    48  		return "GASKET_IOCTL_RESET"
    49  	case GASKET_IOCTL_SET_EVENTFD:
    50  		return "GASKET_IOCTL_SET_EVENTFD"
    51  	case GASKET_IOCTL_CLEAR_EVENTFD:
    52  		return "GASKET_IOCTL_CLEAR_EVENTFD"
    53  	case GASKET_IOCTL_NUMBER_PAGE_TABLES:
    54  		return "GASKET_IOCTL_NUMBER_PAGE_TABLES"
    55  	case GASKET_IOCTL_PAGE_TABLE_SIZE:
    56  		return "GASKET_IOCTL_PAGE_TABLE_SIZE"
    57  	case GASKET_IOCTL_SIMPLE_PAGE_TABLE_SIZE:
    58  		return "GASKET_IOCTL_SIMPLE_PAGE_TABLE_SIZE"
    59  	case GASKET_IOCTL_PARTITION_PAGE_TABLE:
    60  		return "GASKET_IOCTL_PARTITION_PAGE_TABLE"
    61  	case GASKET_IOCTL_MAP_BUFFER:
    62  		return "GASKET_IOCTL_MAP_BUFFER"
    63  	case GASKET_IOCTL_UNMAP_BUFFER:
    64  		return "GASKET_IOCTL_UNMAP_BUFFER"
    65  	case GASKET_IOCTL_CLEAR_INTERRUPT_COUNTS:
    66  		return "GASKET_IOCTL_CLEAR_INTERRUPT_COUNTS"
    67  	case GASKET_IOCTL_REGISTER_INTERRUPT:
    68  		return "GASKET_IOCTL_REGISTER_INTERRUPT"
    69  	case GASKET_IOCTL_UNREGISTER_INTERRUPT:
    70  		return "GASKET_IOCTL_UNREGISTER_INTERRUPT"
    71  	case GASKET_IOCTL_MAP_DMA_BUF:
    72  		return "GASKET_IOCTL_MAP_DMA_BUF"
    73  	default:
    74  		return fmt.Sprintf("UNKNOWN GASKET COMMAND %d", uint32(i))
    75  	}
    76  }
    77  
    78  // GasketInterruptEventFd is the common structure for ioctls associating an
    79  // eventfd with a device interrupt, when using the Gasket interrupt module.
    80  //
    81  // +marshal
    82  type GasketInterruptEventFd struct {
    83  	Interrupt uint64
    84  	EventFD   uint64
    85  }
    86  
    87  // GasketPageTableIoctl is a common structure for ioctls mapping and unmapping
    88  // buffers when using the Gasket page_table module.
    89  //
    90  // +marshal
    91  type GasketPageTableIoctl struct {
    92  	PageTableIndex uint64
    93  	Size           uint64
    94  	HostAddress    uint64
    95  	DeviceAddress  uint64
    96  }
    97  
    98  // GasketInterruptMapping is a structure for ioctls associating an eventfd and
    99  // interrupt controlling bar register with a device interrupt, when using the
   100  // Gasket interrupt module.
   101  //
   102  // +marshal
   103  type GasketInterruptMapping struct {
   104  	Interrupt uint64
   105  	EventFD   uint64
   106  	BarIndex  uint64
   107  	RegOffset uint64
   108  }
   109  
   110  // GasketPageTableDmaBufIoctl is a structure for dma_buf mapping ioctl
   111  // parameters.
   112  //
   113  // +marshal
   114  type GasketPageTableDmaBufIoctl struct {
   115  	PageTableIndex uint64
   116  	DeviceAddress  uint64
   117  	DMABufID       int32 `marshal:"unaligned"` // Struct ends mid 64bit word.
   118  }
   119  
   120  // Ioctl parameter struct sizes.
   121  var (
   122  	SizeofGasketInterruptEventFd     = uint32((*GasketInterruptEventFd)(nil).SizeBytes())
   123  	SizeofGasketPageTableIoctl       = uint32((*GasketPageTableIoctl)(nil).SizeBytes())
   124  	SizeofGasketInterruptMapping     = uint32((*GasketInterruptMapping)(nil).SizeBytes())
   125  	SizeofGasketPageTableDmaBufIoctl = uint32((*GasketPageTableDmaBufIoctl)(nil).SizeBytes())
   126  	SizeOfUnsignedLong               = uint32(8)
   127  )