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>
33 #include <machine/segments.h>
34 #include <machine/specialreg.h>
35 #include <machine/vmm.h>
41 #include "svm_softc.h"
44 * The VMCB aka Virtual Machine Control Block is a 4KB aligned page
45 * in memory that describes the virtual machine.
48 * - instructions or events in the guest to intercept
49 * - control bits that modify execution environment of the guest
50 * - guest processor state (e.g. general purpose registers)
54 * Return VMCB segment area.
56 static struct vmcb_segment *
57 vmcb_segptr(struct vmcb *vmcb, int type)
59 struct vmcb_state *state;
60 struct vmcb_segment *seg;
89 case VM_REG_GUEST_GDTR:
93 case VM_REG_GUEST_IDTR:
97 case VM_REG_GUEST_LDTR:
101 case VM_REG_GUEST_TR:
114 vmcb_access(struct svm_softc *softc, int vcpu, int write, int ident,
121 vmcb = svm_get_vmcb(softc, vcpu);
122 off = VMCB_ACCESS_OFFSET(ident);
123 bytes = VMCB_ACCESS_BYTES(ident);
125 if ((off + bytes) >= sizeof (struct vmcb))
138 memcpy(ptr + off, val, bytes);
140 memcpy(val, ptr + off, bytes);
143 VCPU_CTR1(softc->vm, vcpu,
144 "Invalid size %d for VMCB access: %d", bytes);
148 /* Invalidate all VMCB state cached by h/w. */
150 svm_set_dirty(softc, vcpu, 0xffffffff);
156 * Read from segment selector, control and general purpose register of VMCB.
159 vmcb_read(struct svm_softc *sc, int vcpu, int ident, uint64_t *retval)
162 struct vmcb_state *state;
163 struct vmcb_segment *seg;
166 vmcb = svm_get_vmcb(sc, vcpu);
167 state = &vmcb->state;
170 if (VMCB_ACCESS_OK(ident))
171 return (vmcb_access(sc, vcpu, 0, ident, retval));
174 case VM_REG_GUEST_CR0:
175 *retval = state->cr0;
178 case VM_REG_GUEST_CR2:
179 *retval = state->cr2;
182 case VM_REG_GUEST_CR3:
183 *retval = state->cr3;
186 case VM_REG_GUEST_CR4:
187 *retval = state->cr4;
190 case VM_REG_GUEST_DR7:
191 *retval = state->dr7;
194 case VM_REG_GUEST_EFER:
195 *retval = state->efer;
198 case VM_REG_GUEST_RAX:
199 *retval = state->rax;
202 case VM_REG_GUEST_RFLAGS:
203 *retval = state->rflags;
206 case VM_REG_GUEST_RIP:
207 *retval = state->rip;
210 case VM_REG_GUEST_RSP:
211 *retval = state->rsp;
214 case VM_REG_GUEST_CS:
215 case VM_REG_GUEST_DS:
216 case VM_REG_GUEST_ES:
217 case VM_REG_GUEST_FS:
218 case VM_REG_GUEST_GS:
219 case VM_REG_GUEST_SS:
220 case VM_REG_GUEST_LDTR:
221 case VM_REG_GUEST_TR:
222 seg = vmcb_segptr(vmcb, ident);
223 KASSERT(seg != NULL, ("%s: unable to get segment %d from VMCB",
225 *retval = seg->selector;
228 case VM_REG_GUEST_GDTR:
229 case VM_REG_GUEST_IDTR:
230 /* GDTR and IDTR don't have segment selectors */
242 * Write to segment selector, control and general purpose register of VMCB.
245 vmcb_write(struct svm_softc *sc, int vcpu, int ident, uint64_t val)
248 struct vmcb_state *state;
249 struct vmcb_segment *seg;
252 vmcb = svm_get_vmcb(sc, vcpu);
253 state = &vmcb->state;
257 if (VMCB_ACCESS_OK(ident))
258 return (vmcb_access(sc, vcpu, 1, ident, &val));
261 case VM_REG_GUEST_CR0:
263 svm_set_dirty(sc, vcpu, VMCB_CACHE_CR);
266 case VM_REG_GUEST_CR2:
268 svm_set_dirty(sc, vcpu, VMCB_CACHE_CR2);
271 case VM_REG_GUEST_CR3:
273 svm_set_dirty(sc, vcpu, VMCB_CACHE_CR);
276 case VM_REG_GUEST_CR4:
278 svm_set_dirty(sc, vcpu, VMCB_CACHE_CR);
281 case VM_REG_GUEST_DR7:
285 case VM_REG_GUEST_EFER:
286 /* EFER_SVM must always be set when the guest is executing */
287 state->efer = val | EFER_SVM;
288 svm_set_dirty(sc, vcpu, VMCB_CACHE_CR);
291 case VM_REG_GUEST_RAX:
295 case VM_REG_GUEST_RFLAGS:
299 case VM_REG_GUEST_RIP:
303 case VM_REG_GUEST_RSP:
307 case VM_REG_GUEST_CS:
308 case VM_REG_GUEST_DS:
309 case VM_REG_GUEST_ES:
310 case VM_REG_GUEST_SS:
311 dirtyseg = 1; /* FALLTHROUGH */
312 case VM_REG_GUEST_FS:
313 case VM_REG_GUEST_GS:
314 case VM_REG_GUEST_LDTR:
315 case VM_REG_GUEST_TR:
316 seg = vmcb_segptr(vmcb, ident);
317 KASSERT(seg != NULL, ("%s: unable to get segment %d from VMCB",
321 svm_set_dirty(sc, vcpu, VMCB_CACHE_SEG);
324 case VM_REG_GUEST_GDTR:
325 case VM_REG_GUEST_IDTR:
326 /* GDTR and IDTR don't have segment selectors */
338 vmcb_seg(struct vmcb *vmcb, int ident, struct vmcb_segment *seg2)
340 struct vmcb_segment *seg;
342 seg = vmcb_segptr(vmcb, ident);
344 bcopy(seg, seg2, sizeof(struct vmcb_segment));
352 vmcb_setdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
355 struct svm_softc *sc;
356 struct vmcb_segment *seg;
360 vmcb = svm_get_vmcb(sc, vcpu);
362 seg = vmcb_segptr(vmcb, reg);
363 KASSERT(seg != NULL, ("%s: invalid segment descriptor %d",
366 seg->base = desc->base;
367 seg->limit = desc->limit;
368 if (reg != VM_REG_GUEST_GDTR && reg != VM_REG_GUEST_IDTR) {
370 * Map seg_desc access to VMCB attribute format.
372 * SVM uses the 'P' bit in the segment attributes to indicate a
373 * NULL segment so clear it if the segment is marked unusable.
375 attrib = ((desc->access & 0xF000) >> 4) | (desc->access & 0xFF);
376 if (SEG_DESC_UNUSABLE(desc->access)) {
379 seg->attrib = attrib;
382 VCPU_CTR4(sc->vm, vcpu, "Setting desc %d: base (%#lx), limit (%#x), "
383 "attrib (%#x)", reg, seg->base, seg->limit, seg->attrib);
386 case VM_REG_GUEST_CS:
387 case VM_REG_GUEST_DS:
388 case VM_REG_GUEST_ES:
389 case VM_REG_GUEST_SS:
390 svm_set_dirty(sc, vcpu, VMCB_CACHE_SEG);
392 case VM_REG_GUEST_GDTR:
393 case VM_REG_GUEST_IDTR:
394 svm_set_dirty(sc, vcpu, VMCB_CACHE_DT);
404 vmcb_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
407 struct svm_softc *sc;
408 struct vmcb_segment *seg;
411 vmcb = svm_get_vmcb(sc, vcpu);
412 seg = vmcb_segptr(vmcb, reg);
413 KASSERT(seg != NULL, ("%s: invalid segment descriptor %d",
416 desc->base = seg->base;
417 desc->limit = seg->limit;
420 if (reg != VM_REG_GUEST_GDTR && reg != VM_REG_GUEST_IDTR) {
421 /* Map seg_desc access to VMCB attribute format */
422 desc->access = ((seg->attrib & 0xF00) << 4) |
423 (seg->attrib & 0xFF);
426 * VT-x uses bit 16 to indicate a segment that has been loaded
427 * with a NULL selector (aka unusable). The 'desc->access'
428 * field is interpreted in the VT-x format by the
429 * processor-independent code.
431 * SVM uses the 'P' bit to convey the same information so
432 * convert it into the VT-x format. For more details refer to
433 * section "Segment State in the VMCB" in APMv2.
435 if (reg != VM_REG_GUEST_CS && reg != VM_REG_GUEST_TR) {
436 if ((desc->access & 0x80) == 0)
437 desc->access |= 0x10000; /* Unusable segment */