lvm2

package module
v0.0.0-...-45c0841 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 24, 2022 License: Unlicense Imports: 9 Imported by: 0

README

lvm2

lemon VM 2

Documentation

Index

Constants

View Source
const (
	SYS_READ  = 0
	SYS_WRITE = 1
	SYS_OPEN  = 2
	SYS_CLOSE = 3
	SYS_EXIT  = 60

	SYS_ALLOCATE = 100
	SYS_FREE     = 101
)
View Source
const (
	REGISTER_R0    = 0
	REGISTER_R1    = 1
	REGISTER_R2    = 2
	REGISTER_R3    = 3
	REGISTER_R4    = 4
	REGISTER_R5    = 5
	REGISTER_R6    = 6
	REGISTER_R7    = 7
	REGISTER_R8    = 8
	REGISTER_R9    = 9
	REGISTER_R10   = 10
	REGISTER_R11   = 11
	REGISTER_R12   = 12
	REGISTER_R13   = 13
	REGISTER_R14   = 14
	REGISTER_R15   = 15
	REGISTER_R16   = 16
	REGISTER_R17   = 17
	REGISTER_R18   = 18
	REGISTER_R19   = 19
	REGISTER_R20   = 20
	REGISTER_R21   = 21
	REGISTER_R22   = 22
	REGISTER_R23   = 23
	REGISTER_R24   = 24
	REGISTER_R25   = 25
	REGISTER_R26   = 26
	REGISTER_R27   = 27
	REGISTER_R28   = 28
	REGISTER_R29   = 29
	REGISTER_R30   = 30
	REGISTER_R31   = 31
	REGISTER_SYS32 = 32
	REGISTER_SYS33 = 33
	REGISTER_SYS34 = 34
	REGISTER_SYS35 = 35
	REGISTER_SYS36 = 36
	REGISTER_SYS37 = 37
	REGISTER_SYS38 = 38
	REGISTER_SYS39 = 39
	REGISTER_SYS40 = 40
	REGISTER_SYS41 = 41
	REGISTER_SYS42 = 42
	REGISTER_SYS43 = 43
	REGISTER_SYS44 = 44
	REGISTER_SYS45 = 45
	REGISTER_SYS46 = 46
	REGISTER_SYS47 = 47
	REGISTER_SYS48 = 48
	REGISTER_SYS49 = 49
	REGISTER_SYS50 = 50
	REGISTER_SYS51 = 51
	REGISTER_SYS52 = 52
	REGISTER_SYS53 = 53
	REGISTER_SYS54 = 54
	REGISTER_SYS55 = 55
	REGISTER_SYS56 = 56
	REGISTER_SYS57 = 57
	REGISTER_SYS58 = 58
	REGISTER_SYS59 = 59
	REGISTER_SYS60 = 60
	REGISTER_SYS61 = 61
	REGISTER_SYS62 = 62
	REGISTER_SYS63 = 63
	REGISTER_PC    = 64
	REGISTER_SP    = 65
	REGISTER_SB    = 66
)
View Source
const BYTE_SIZE = 1
View Source
const HALF_WORD_SIZE = WORD_SIZE / 2
View Source
const InstructionBytecodeSize = 1 + 1 + 8*3
View Source
const PAGE_SIZE = 1 << 12 // 4KB
View Source
const WORD_SIZE = 8

Variables

View Source
var (
	ErrInvalidSize    = errors.New("Invalid Size")
	ErrNoMemory       = errors.New("No Memory")
	ErrInvalidAddress = errors.New("Invalid Address")
)
View Source
var ErrExited = errors.New("exited")
View Source
var ErrInvalidInstruction = fmt.Errorf("invalid instruction")
View Source
var ErrSegmentationFault = errors.New("Segmentation Fault")
View Source
var Registers = map[string]uint64{
	"PC":    REGISTER_PC,
	"SP":    REGISTER_SP,
	"SB":    REGISTER_SB,
	"R0":    REGISTER_R0,
	"R1":    REGISTER_R1,
	"R2":    REGISTER_R2,
	"R3":    REGISTER_R3,
	"R4":    REGISTER_R4,
	"R5":    REGISTER_R5,
	"R6":    REGISTER_R6,
	"R7":    REGISTER_R7,
	"R8":    REGISTER_R8,
	"R9":    REGISTER_R9,
	"R10":   REGISTER_R10,
	"R11":   REGISTER_R11,
	"R12":   REGISTER_R12,
	"R13":   REGISTER_R13,
	"R14":   REGISTER_R14,
	"R15":   REGISTER_R15,
	"R16":   REGISTER_R16,
	"R17":   REGISTER_R17,
	"R18":   REGISTER_R18,
	"R19":   REGISTER_R19,
	"R20":   REGISTER_R20,
	"R21":   REGISTER_R21,
	"R22":   REGISTER_R22,
	"R23":   REGISTER_R23,
	"R24":   REGISTER_R24,
	"R25":   REGISTER_R25,
	"R26":   REGISTER_R26,
	"R27":   REGISTER_R27,
	"R28":   REGISTER_R28,
	"R29":   REGISTER_R29,
	"R30":   REGISTER_R30,
	"R31":   REGISTER_R31,
	"SYS32": REGISTER_SYS32,
	"SYS33": REGISTER_SYS33,
	"SYS34": REGISTER_SYS34,
	"SYS35": REGISTER_SYS35,
	"SYS36": REGISTER_SYS36,
	"SYS37": REGISTER_SYS37,
	"SYS38": REGISTER_SYS38,
	"SYS39": REGISTER_SYS39,
	"SYS40": REGISTER_SYS40,
	"SYS41": REGISTER_SYS41,
	"SYS42": REGISTER_SYS42,
	"SYS43": REGISTER_SYS43,
	"SYS44": REGISTER_SYS44,
	"SYS45": REGISTER_SYS45,
	"SYS46": REGISTER_SYS46,
	"SYS47": REGISTER_SYS47,
	"SYS48": REGISTER_SYS48,
	"SYS49": REGISTER_SYS49,
	"SYS50": REGISTER_SYS50,
	"SYS51": REGISTER_SYS51,
	"SYS52": REGISTER_SYS52,
	"SYS53": REGISTER_SYS53,
	"SYS54": REGISTER_SYS54,
	"SYS55": REGISTER_SYS55,
	"SYS56": REGISTER_SYS56,
	"SYS57": REGISTER_SYS57,
	"SYS58": REGISTER_SYS58,
	"SYS59": REGISTER_SYS59,
	"SYS60": REGISTER_SYS60,
	"SYS61": REGISTER_SYS61,
	"SYS62": REGISTER_SYS62,
	"SYS63": REGISTER_SYS63,
}

Functions

This section is empty.

Types

type InstructionOpcode

type InstructionOpcode []byte

func New_InstructionOpcode

func New_InstructionOpcode(InstructionType uint8, OperandType uint8, Operand0 uint64, Operand1 uint64, Operand2 uint64) InstructionOpcode

func Serialize_InstructionOpcode

func Serialize_InstructionOpcode(dst InstructionOpcode, InstructionType uint8, OperandType uint8, Operand0 uint64, Operand1 uint64, Operand2 uint64) InstructionOpcode

func (InstructionOpcode) InstructionType

func (s InstructionOpcode) InstructionType() uint8

func (InstructionOpcode) Operand0

func (s InstructionOpcode) Operand0() uint64

func (InstructionOpcode) Operand1

func (s InstructionOpcode) Operand1() uint64

func (InstructionOpcode) Operand2

func (s InstructionOpcode) Operand2() uint64

func (InstructionOpcode) OperandType

func (s InstructionOpcode) OperandType() uint8

func (InstructionOpcode) String

func (s InstructionOpcode) String() string

func (InstructionOpcode) Vstruct_Validate

func (s InstructionOpcode) Vstruct_Validate() bool

type InstructionType

type InstructionType byte
const (
	InstructionType_NOP InstructionType = iota

	InstructionType_ADD // R0 = R1 + R2
	InstructionType_SUB // R0 = R1 - R2
	InstructionType_MUL // R0 = R1 * R2
	InstructionType_DIV // R0 = R1 / R2
	InstructionType_MOD // R0 = R1 % R2

	InstructionType_AND // R0 = R1 & R2
	InstructionType_OR  // R0 = R1 | R2
	InstructionType_XOR // R0 = R1 ^ R2
	InstructionType_NOT // R0 = ~R1

	InstructionType_SHL // R0 = R1 << R2
	InstructionType_SHR // R0 = R1 >> R2

	InstructionType_CMP // R0 = R1 - R2 (signed)
	InstructionType_JMP // PC = R0

	InstructionType_JG  // if R0 > 0; PC = R1
	InstructionType_JL  // if R0 < 0; PC = R1
	InstructionType_JE  // if R0 == 0; PC = R1
	InstructionType_JNE // if R0 != 0; PC = R1
	InstructionType_JGE // if R0 >= 0; PC = R1
	InstructionType_JLE // if R0 <= 0; PC = R1

	InstructionType_LOAD  // R0 = [MEM[R1 + R2]] (Load Register from Memory (WORD_SIZE))
	InstructionType_LOADH // R0 = [MEM[R1 + R2]] (Load Register from Memory (HALF_WORD_SIZE))
	InstructionType_LOADB // R0 = [MEM[R1 + R2]] (Load Register from Memory (BYTE_SIZE))

	InstructionType_STORE  // [MEM[R1 + R2]] = R0 (Store Register to Memory (WORD_SIZE))
	InstructionType_STOREH // [MEM[R1 + R2]] = R0 (Store Register to Memory (HALF_WORD_SIZE))
	InstructionType_STOREB // [MEM[R1 + R2]] = R0 (Store Register to Memory (BYTE_SIZE))

	InstructionType_MOV  // R0 = R1 (Move Register by WORD_SIZE)
	InstructionType_MOVH // R0 = R1 (Move Register by HALF_WORD_SIZE)
	InstructionType_MOVB // R0 = R1 (Move Register by BYTE_SIZE)

	InstructionType_PUSH // SP = SP - WORD_SIZE (stack.push(R0))
	InstructionType_POP  // SP = SP + WORD_SIZE (R0 = stack.pop())

	InstructionType_CALL // SP = SP - WORD_SIZE; [SP] = PC; PC = R0
	InstructionType_RET  // PC = [SP]; SP = SP + WORD_SIZE

	InstructionType_SYSCALL // R0 = syscall(R1, R2) (System Call) R0: errno, R1: syscall number, R2: register parameter
)

func (InstructionType) String

func (v InstructionType) String() string

type Memory

type Memory struct {
	Blocks []MemoryBlock

	MemoryHead uint64
	MaxAddress uint64

	Stack      MemoryBlock
	Cache      *MemoryBlock
	CacheIndex int
}

func NewMemory

func NewMemory() *Memory

func (*Memory) Allocate

func (m *Memory) Allocate(size uint64) uint64

func (*Memory) Free

func (m *Memory) Free(start uint64) error

func (*Memory) GetMemoryFunc

func (m *Memory) GetMemoryFunc(address uint64, size uint64, iterf func(addr uint64, b []byte) error) error

func (*Memory) LoadBlock

func (m *Memory) LoadBlock(address uint64) (MemoryBlock, error)

func (*Memory) LoadBlockIndex

func (m *Memory) LoadBlockIndex(address uint64) (MemoryBlock, int, error)

func (*Memory) ReadAt

func (m *Memory) ReadAt(address uint64, p []byte) (int, error)

func (*Memory) Reset

func (m *Memory) Reset()

func (*Memory) SetProgram

func (m *Memory) SetProgram(p []byte)

func (*Memory) WriteAt

func (m *Memory) WriteAt(address uint64, p []byte) (int, error)

type MemoryBlock

type MemoryBlock struct {
	Start uint64
	End   uint64

	Block []byte
}

type OpType

type OpType byte
const (
	OpTypeNone     OpType = 0b00
	OpTypeRegister OpType = 0b01
	OpTypeConstant OpType = 0b10
	OpTypeReserved OpType = 0b11
)

type SYSCALLFunc

type SYSCALLFunc func(vm *VM, R0, R1, R2 uint64) (errno uint64, err error)

type VM

type VM struct {
	Memory *Memory

	// # Registers
	//
	// ## 64-bit General Purpose Registers
	//
	// R0-R31
	//
	// ## SYSCALL registers
	//
	// SYS32-SYS63
	//
	// ## VM registers
	//
	// Program Counter (PC) (Register ID: 64)
	// Stack Pointer (SP)   (Register ID: 65)
	// Stack Base (SB)      (Register ID: 66)
	Registers [32 + 32 + 3]uint64

	// File Descriptor Table
	Files map[uint64]VMFile
	// File Descriptor Counter
	FileCounter uint64
}

func (*VM) Run

func (v *VM) Run() (uint64, error)

func (*VM) SetProgram

func (v *VM) SetProgram(p []byte)

func (*VM) SetProgramCounter

func (v *VM) SetProgramCounter(pc uint64)

type VMFile

type VMFile interface {
	Read(p []byte) (n int, err error)
	Write(p []byte) (n int, err error)
	Seek(offset int64, whence int) (int64, error)
	Close() error
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL