summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/platform/kvm/kvm_amd64.go
blob: c5a6f9c7de6c1bde977df823c67065798c6af17c (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build amd64

package kvm

import (
	"gvisor.dev/gvisor/pkg/cpuid"
	"gvisor.dev/gvisor/pkg/sentry/platform/ring0"
)

// userMemoryRegion is a region of physical memory.
//
// This mirrors kvm_memory_region.
type userMemoryRegion struct {
	slot          uint32
	flags         uint32
	guestPhysAddr uint64
	memorySize    uint64
	userspaceAddr uint64
}

// userRegs represents KVM user registers.
//
// This mirrors kvm_regs.
type userRegs struct {
	RAX    uint64
	RBX    uint64
	RCX    uint64
	RDX    uint64
	RSI    uint64
	RDI    uint64
	RSP    uint64
	RBP    uint64
	R8     uint64
	R9     uint64
	R10    uint64
	R11    uint64
	R12    uint64
	R13    uint64
	R14    uint64
	R15    uint64
	RIP    uint64
	RFLAGS uint64
}

// systemRegs represents KVM system registers.
//
// This mirrors kvm_sregs.
type systemRegs struct {
	CS              segment
	DS              segment
	ES              segment
	FS              segment
	GS              segment
	SS              segment
	TR              segment
	LDT             segment
	GDT             descriptor
	IDT             descriptor
	CR0             uint64
	CR2             uint64
	CR3             uint64
	CR4             uint64
	CR8             uint64
	EFER            uint64
	apicBase        uint64
	interruptBitmap [(_KVM_NR_INTERRUPTS + 63) / 64]uint64
}

// segment is the expanded form of a segment register.
//
// This mirrors kvm_segment.
type segment struct {
	base     uint64
	limit    uint32
	selector uint16
	typ      uint8
	present  uint8
	DPL      uint8
	DB       uint8
	S        uint8
	L        uint8
	G        uint8
	AVL      uint8
	unusable uint8
	_        uint8
}

// Clear clears the segment and marks it unusable.
func (s *segment) Clear() {
	*s = segment{unusable: 1}
}

// selector is a segment selector.
type selector uint16

// tobool is a simple helper.
func tobool(x ring0.SegmentDescriptorFlags) uint8 {
	if x != 0 {
		return 1
	}
	return 0
}

// Load loads the segment described by d into the segment s.
//
// The argument sel is recorded as the segment selector index.
func (s *segment) Load(d *ring0.SegmentDescriptor, sel ring0.Selector) {
	flag := d.Flags()
	if flag&ring0.SegmentDescriptorPresent == 0 {
		s.Clear()
		return
	}
	s.base = uint64(d.Base())
	s.limit = d.Limit()
	s.typ = uint8((flag>>8)&0xF) | 1
	s.S = tobool(flag & ring0.SegmentDescriptorSystem)
	s.DPL = uint8(d.DPL())
	s.present = tobool(flag & ring0.SegmentDescriptorPresent)
	s.AVL = tobool(flag & ring0.SegmentDescriptorAVL)
	s.L = tobool(flag & ring0.SegmentDescriptorLong)
	s.DB = tobool(flag & ring0.SegmentDescriptorDB)
	s.G = tobool(flag & ring0.SegmentDescriptorG)
	if s.L != 0 {
		s.limit = 0xffffffff
	}
	s.unusable = 0
	s.selector = uint16(sel)
}

// descriptor describes a region of physical memory.
//
// It corresponds to the pseudo-descriptor used in the x86 LGDT and LIDT
// instructions, and mirrors kvm_dtable.
type descriptor struct {
	base  uint64
	limit uint16
	_     [3]uint16
}

// modelControlRegister is an MSR entry.
//
// This mirrors kvm_msr_entry.
type modelControlRegister struct {
	index uint32
	_     uint32
	data  uint64
}

// modelControlRegisers is a collection of MSRs.
//
// This mirrors kvm_msrs.
type modelControlRegisters struct {
	nmsrs   uint32
	_       uint32
	entries [16]modelControlRegister
}

// runData is the run structure. This may be mapped for synchronous register
// access (although that doesn't appear to be supported by my kernel at least).
//
// This mirrors kvm_run.
type runData struct {
	requestInterruptWindow uint8
	_                      [7]uint8

	exitReason                 uint32
	readyForInterruptInjection uint8
	ifFlag                     uint8
	_                          [2]uint8

	cr8      uint64
	apicBase uint64

	// This is the union data for exits. Interpretation depends entirely on
	// the exitReason above (see vCPU code for more information).
	data [32]uint64
}

// cpuidEntry is a single CPUID entry.
//
// This mirrors kvm_cpuid_entry2.
type cpuidEntry struct {
	function uint32
	index    uint32
	flags    uint32
	eax      uint32
	ebx      uint32
	ecx      uint32
	edx      uint32
	_        [3]uint32
}

// cpuidEntries is a collection of CPUID entries.
//
// This mirrors kvm_cpuid2.
type cpuidEntries struct {
	nr      uint32
	_       uint32
	entries [_KVM_NR_CPUID_ENTRIES]cpuidEntry
}

// updateGlobalOnce does global initialization. It has to be called only once.
func updateGlobalOnce(fd int) error {
	physicalInit()
	err := updateSystemValues(int(fd))
	ring0.Init(cpuid.HostFeatureSet())
	return err
}