-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvm.go
73 lines (60 loc) · 1.48 KB
/
vm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package kvm
import (
"os"
"syscall"
"unsafe"
)
var osIoctl ioctler = &osIoctler{}
// VM is a KVM VM
type VM struct {
Fd uintptr
MMapSize int
}
// CreateVM creates a new VM
func CreateVM() (*VM, error) {
file, err := os.OpenFile("/dev/kvm", os.O_RDWR|syscall.O_CLOEXEC, 0)
if err != nil {
return nil, err
}
defer file.Close()
kvmfd := file.Fd()
vmfd, err := osIoctl.ioctl(kvmfd, ioctlKVMCreateVM, 0)
if err != nil {
return nil, err
}
mmapsize, err := osIoctl.ioctl(kvmfd, ioctlKVMGetVCPUMMAPSize, 0)
if err != nil {
return nil, err
}
vm := VM{
Fd: vmfd,
MMapSize: int(mmapsize),
}
return &vm, nil
}
const (
// UserMemoryFlagLogDirtyPages maps to KVM_MEM_LOG_DIRTY_PAGES
UserMemoryFlagLogDirtyPages = 1 << iota
// UserMemoryReadOnly maps to KVM_MEM_READONLY
UserMemoryReadOnly
)
type kvmUserspaceMemoryRegion struct {
Slot uint32
Flags uint32
GuestPhysAddr uint64
MemorySize uint64
UserspaceAddr uint64
}
// MapUserMemory maps memory to the specified slot with the given flags
func (vm *VM) MapUserMemory(slot uint32, flags uint32, guestAddress uint64, memory []byte) error {
userspaceAddr := unsafe.Pointer(&memory[0])
region := kvmUserspaceMemoryRegion{
Slot: slot,
Flags: flags,
GuestPhysAddr: guestAddress,
MemorySize: uint64(len(memory)),
UserspaceAddr: uint64(uintptr(userspaceAddr)),
}
_, err := osIoctl.ioctl(vm.Fd, ioctlKVMSetUserMemoryRegion, uintptr(unsafe.Pointer(®ion)))
return err
}