github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/runsc/boot/compat_amd64.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 boot
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"golang.org/x/sys/unix"
    21  	"github.com/nicocha30/gvisor-ligolo/pkg/abi"
    22  	"github.com/nicocha30/gvisor-ligolo/pkg/sentry/arch"
    23  	rpb "github.com/nicocha30/gvisor-ligolo/pkg/sentry/arch/registers_go_proto"
    24  	"github.com/nicocha30/gvisor-ligolo/pkg/sentry/strace"
    25  )
    26  
    27  const (
    28  	// reportLimit is the max number of events that should be reported per
    29  	// tracker.
    30  	reportLimit = 100
    31  	syscallLink = "https://gvisor.dev/c/linux/amd64"
    32  )
    33  
    34  // newRegs create a empty Registers instance.
    35  func newRegs() *rpb.Registers {
    36  	return &rpb.Registers{
    37  		Arch: &rpb.Registers_Amd64{
    38  			Amd64: &rpb.AMD64Registers{},
    39  		},
    40  	}
    41  }
    42  
    43  func argVal(argIdx int, regs *rpb.Registers) uint64 {
    44  	amd64Regs := regs.GetArch().(*rpb.Registers_Amd64).Amd64
    45  
    46  	switch argIdx {
    47  	case 0:
    48  		return amd64Regs.Rdi
    49  	case 1:
    50  		return amd64Regs.Rsi
    51  	case 2:
    52  		return amd64Regs.Rdx
    53  	case 3:
    54  		return amd64Regs.R10
    55  	case 4:
    56  		return amd64Regs.R8
    57  	case 5:
    58  		return amd64Regs.R9
    59  	}
    60  	panic(fmt.Sprintf("invalid syscall argument index %d", argIdx))
    61  }
    62  
    63  func setArgVal(argIdx int, argVal uint64, regs *rpb.Registers) {
    64  	amd64Regs := regs.GetArch().(*rpb.Registers_Amd64).Amd64
    65  
    66  	switch argIdx {
    67  	case 0:
    68  		amd64Regs.Rdi = argVal
    69  	case 1:
    70  		amd64Regs.Rsi = argVal
    71  	case 2:
    72  		amd64Regs.Rdx = argVal
    73  	case 3:
    74  		amd64Regs.R10 = argVal
    75  	case 4:
    76  		amd64Regs.R8 = argVal
    77  	case 5:
    78  		amd64Regs.R9 = argVal
    79  	default:
    80  		panic(fmt.Sprintf("invalid syscall argument index %d", argIdx))
    81  	}
    82  }
    83  
    84  func getSyscallNameMap() (strace.SyscallMap, bool) {
    85  	return strace.Lookup(abi.Linux, arch.AMD64)
    86  }
    87  
    88  func syscallNum(regs *rpb.Registers) uint64 {
    89  	amd64Regs := regs.GetArch().(*rpb.Registers_Amd64).Amd64
    90  	return amd64Regs.OrigRax
    91  }
    92  
    93  func newArchArgsTracker(sysnr uint64) syscallTracker {
    94  	switch sysnr {
    95  	case unix.SYS_ARCH_PRCTL:
    96  		// args: cmd, ...
    97  		return newArgsTracker(0)
    98  	}
    99  	return nil
   100  }