github.com/dylandreimerink/gobpfld@v0.6.1-0.20220205171531-e79c330ad608/emulator/inst_mov.go (about)

     1  package emulator
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/dylandreimerink/gobpfld/ebpf"
     7  )
     8  
     9  var _ Instruction = (*Mov32)(nil)
    10  
    11  type Mov32 struct {
    12  	ebpf.Mov32
    13  }
    14  
    15  func (i *Mov32) Clone() Instruction {
    16  	c := *i
    17  	return &c
    18  }
    19  
    20  func (i *Mov32) Execute(vm *VM) error {
    21  	// TODO is this correct, or should we preseve the upper 32 bits?
    22  	err := vm.Registers.Assign(i.Dest, newIMM(int64(i.Value)))
    23  	if err != nil {
    24  		return fmt.Errorf("assign %s: %w", i.Dest.String(), err)
    25  	}
    26  
    27  	return nil
    28  }
    29  
    30  var _ Instruction = (*Mov64)(nil)
    31  
    32  type Mov64 struct {
    33  	ebpf.Mov64
    34  }
    35  
    36  func (i *Mov64) Clone() Instruction {
    37  	c := *i
    38  	return &c
    39  }
    40  
    41  func (i *Mov64) Execute(vm *VM) error {
    42  	err := vm.Registers.Assign(i.Dest, newIMM(int64(i.Value)))
    43  	if err != nil {
    44  		return fmt.Errorf("assign %s: %w", i.Dest.String(), err)
    45  	}
    46  
    47  	return nil
    48  }
    49  
    50  var _ Instruction = (*Mov32Register)(nil)
    51  
    52  type Mov32Register struct {
    53  	ebpf.Mov32Register
    54  }
    55  
    56  func (i *Mov32Register) Clone() Instruction {
    57  	c := *i
    58  	return &c
    59  }
    60  
    61  func (i *Mov32Register) Execute(vm *VM) error {
    62  	dr, err := vm.Registers.Copy(i.Src)
    63  	if err != nil {
    64  		return fmt.Errorf("read %s: %w", i.Dest.String(), err)
    65  	}
    66  
    67  	// TODO is this correct, or should we preseve the upper 32 bits?
    68  	err = vm.Registers.Assign(i.Dest, dr)
    69  	if err != nil {
    70  		return fmt.Errorf("assign %s: %w", i.Dest.String(), err)
    71  	}
    72  
    73  	return nil
    74  }
    75  
    76  var _ Instruction = (*Mov64Register)(nil)
    77  
    78  type Mov64Register struct {
    79  	ebpf.Mov64Register
    80  }
    81  
    82  func (i *Mov64Register) Clone() Instruction {
    83  	c := *i
    84  	return &c
    85  }
    86  
    87  func (i *Mov64Register) Execute(vm *VM) error {
    88  	dr, err := vm.Registers.Copy(i.Src)
    89  	if err != nil {
    90  		return fmt.Errorf("read %s: %w", i.Dest.String(), err)
    91  	}
    92  
    93  	err = vm.Registers.Assign(i.Dest, dr)
    94  	if err != nil {
    95  		return fmt.Errorf("assign %s: %w", i.Dest.String(), err)
    96  	}
    97  
    98  	return nil
    99  }