2 * Copyright (c) 2013 Anish Gupta (akgupt3@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/cpuset.h>
34 #include <machine/segments.h>
35 #include <machine/specialreg.h>
36 #include <machine/vmm.h>
42 #include "svm_softc.h"
45 * The VMCB aka Virtual Machine Control Block is a 4KB aligned page
46 * in memory that describes the virtual machine.
49 * - instructions or events in the guest to intercept
50 * - control bits that modify execution environment of the guest
51 * - guest processor state (e.g. general purpose registers)
55 * Return VMCB segment area.
57 static struct vmcb_segment *
58 vmcb_segptr(struct vmcb *vmcb, int type)
60 struct vmcb_state *state;
61 struct vmcb_segment *seg;
90 case VM_REG_GUEST_GDTR:
94 case VM_REG_GUEST_IDTR:
98 case VM_REG_GUEST_LDTR:
102 case VM_REG_GUEST_TR:
115 * Read from segment selector, control and general purpose register of VMCB.
118 vmcb_read(struct svm_softc *sc, int vcpu, int ident, uint64_t *retval)
121 struct vmcb_state *state;
122 struct vmcb_segment *seg;
125 vmcb = svm_get_vmcb(sc, vcpu);
126 state = &vmcb->state;
130 case VM_REG_GUEST_CR0:
131 *retval = state->cr0;
134 case VM_REG_GUEST_CR2:
135 *retval = state->cr2;
138 case VM_REG_GUEST_CR3:
139 *retval = state->cr3;
142 case VM_REG_GUEST_CR4:
143 *retval = state->cr4;
146 case VM_REG_GUEST_DR7:
147 *retval = state->dr7;
150 case VM_REG_GUEST_EFER:
151 *retval = state->efer;
154 case VM_REG_GUEST_RAX:
155 *retval = state->rax;
158 case VM_REG_GUEST_RFLAGS:
159 *retval = state->rflags;
162 case VM_REG_GUEST_RIP:
163 *retval = state->rip;
166 case VM_REG_GUEST_RSP:
167 *retval = state->rsp;
170 case VM_REG_GUEST_CS:
171 case VM_REG_GUEST_DS:
172 case VM_REG_GUEST_ES:
173 case VM_REG_GUEST_FS:
174 case VM_REG_GUEST_GS:
175 case VM_REG_GUEST_SS:
176 case VM_REG_GUEST_LDTR:
177 case VM_REG_GUEST_TR:
178 seg = vmcb_segptr(vmcb, ident);
179 KASSERT(seg != NULL, ("%s: unable to get segment %d from VMCB",
181 *retval = seg->selector;
184 case VM_REG_GUEST_GDTR:
185 case VM_REG_GUEST_IDTR:
186 /* GDTR and IDTR don't have segment selectors */
198 * Write to segment selector, control and general purpose register of VMCB.
201 vmcb_write(struct svm_softc *sc, int vcpu, int ident, uint64_t val)
204 struct vmcb_state *state;
205 struct vmcb_segment *seg;
208 vmcb = svm_get_vmcb(sc, vcpu);
209 state = &vmcb->state;
214 case VM_REG_GUEST_CR0:
216 svm_set_dirty(sc, vcpu, VMCB_CACHE_CR);
219 case VM_REG_GUEST_CR2:
221 svm_set_dirty(sc, vcpu, VMCB_CACHE_CR2);
224 case VM_REG_GUEST_CR3:
226 svm_set_dirty(sc, vcpu, VMCB_CACHE_CR);
229 case VM_REG_GUEST_CR4:
231 svm_set_dirty(sc, vcpu, VMCB_CACHE_CR);
234 case VM_REG_GUEST_DR7:
238 case VM_REG_GUEST_EFER:
239 /* EFER_SVM must always be set when the guest is executing */
240 state->efer = val | EFER_SVM;
241 svm_set_dirty(sc, vcpu, VMCB_CACHE_CR);
244 case VM_REG_GUEST_RAX:
248 case VM_REG_GUEST_RFLAGS:
252 case VM_REG_GUEST_RIP:
256 case VM_REG_GUEST_RSP:
260 case VM_REG_GUEST_CS:
261 case VM_REG_GUEST_DS:
262 case VM_REG_GUEST_ES:
263 case VM_REG_GUEST_SS:
264 dirtyseg = 1; /* FALLTHROUGH */
265 case VM_REG_GUEST_FS:
266 case VM_REG_GUEST_GS:
267 case VM_REG_GUEST_LDTR:
268 case VM_REG_GUEST_TR:
269 seg = vmcb_segptr(vmcb, ident);
270 KASSERT(seg != NULL, ("%s: unable to get segment %d from VMCB",
274 svm_set_dirty(sc, vcpu, VMCB_CACHE_SEG);
277 case VM_REG_GUEST_GDTR:
278 case VM_REG_GUEST_IDTR:
279 /* GDTR and IDTR don't have segment selectors */
291 vmcb_seg(struct vmcb *vmcb, int ident, struct vmcb_segment *seg2)
293 struct vmcb_segment *seg;
295 seg = vmcb_segptr(vmcb, ident);
297 bcopy(seg, seg2, sizeof(struct vmcb_segment));
305 vmcb_setdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
308 struct svm_softc *sc;
309 struct vmcb_segment *seg;
313 vmcb = svm_get_vmcb(sc, vcpu);
315 seg = vmcb_segptr(vmcb, reg);
316 KASSERT(seg != NULL, ("%s: invalid segment descriptor %d",
319 seg->base = desc->base;
320 seg->limit = desc->limit;
321 if (reg != VM_REG_GUEST_GDTR && reg != VM_REG_GUEST_IDTR) {
323 * Map seg_desc access to VMCB attribute format.
325 * SVM uses the 'P' bit in the segment attributes to indicate a
326 * NULL segment so clear it if the segment is marked unusable.
328 attrib = ((desc->access & 0xF000) >> 4) | (desc->access & 0xFF);
329 if (SEG_DESC_UNUSABLE(desc->access)) {
332 seg->attrib = attrib;
335 VCPU_CTR4(sc->vm, vcpu, "Setting desc %d: base (%#lx), limit (%#x), "
336 "attrib (%#x)", reg, seg->base, seg->limit, seg->attrib);
339 case VM_REG_GUEST_CS:
340 case VM_REG_GUEST_DS:
341 case VM_REG_GUEST_ES:
342 case VM_REG_GUEST_SS:
343 svm_set_dirty(sc, vcpu, VMCB_CACHE_SEG);
344 case VM_REG_GUEST_GDTR:
345 case VM_REG_GUEST_IDTR:
346 svm_set_dirty(sc, vcpu, VMCB_CACHE_DT);
356 vmcb_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
359 struct svm_softc *sc;
360 struct vmcb_segment *seg;
363 vmcb = svm_get_vmcb(sc, vcpu);
364 seg = vmcb_segptr(vmcb, reg);
365 KASSERT(seg != NULL, ("%s: invalid segment descriptor %d",
368 desc->base = seg->base;
369 desc->limit = seg->limit;
372 if (reg != VM_REG_GUEST_GDTR && reg != VM_REG_GUEST_IDTR) {
373 /* Map seg_desc access to VMCB attribute format */
374 desc->access = ((seg->attrib & 0xF00) << 4) |
375 (seg->attrib & 0xFF);
378 * VT-x uses bit 16 to indicate a segment that has been loaded
379 * with a NULL selector (aka unusable). The 'desc->access'
380 * field is interpreted in the VT-x format by the
381 * processor-independent code.
383 * SVM uses the 'P' bit to convey the same information so
384 * convert it into the VT-x format. For more details refer to
385 * section "Segment State in the VMCB" in APMv2.
387 if (reg != VM_REG_GUEST_CS && reg != VM_REG_GUEST_TR) {
388 if ((desc->access & 0x80) == 0)
389 desc->access |= 0x10000; /* Unusable segment */