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 vmcb_access(struct svm_softc *softc, int vcpu, int write, int ident,
122 vmcb = svm_get_vmcb(softc, vcpu);
123 off = VMCB_ACCESS_OFFSET(ident);
124 bytes = VMCB_ACCESS_BYTES(ident);
126 if ((off + bytes) >= sizeof (struct vmcb))
139 memcpy(ptr + off, val, bytes);
141 memcpy(val, ptr + off, bytes);
144 VCPU_CTR1(softc->vm, vcpu,
145 "Invalid size %d for VMCB access: %d", bytes);
149 /* Invalidate all VMCB state cached by h/w. */
151 svm_set_dirty(softc, vcpu, 0xffffffff);
157 * Read from segment selector, control and general purpose register of VMCB.
160 vmcb_read(struct svm_softc *sc, int vcpu, int ident, uint64_t *retval)
163 struct vmcb_state *state;
164 struct vmcb_segment *seg;
167 vmcb = svm_get_vmcb(sc, vcpu);
168 state = &vmcb->state;
171 if (VMCB_ACCESS_OK(ident))
172 return (vmcb_access(sc, vcpu, 0, ident, retval));
175 case VM_REG_GUEST_CR0:
176 *retval = state->cr0;
179 case VM_REG_GUEST_CR2:
180 *retval = state->cr2;
183 case VM_REG_GUEST_CR3:
184 *retval = state->cr3;
187 case VM_REG_GUEST_CR4:
188 *retval = state->cr4;
191 case VM_REG_GUEST_DR7:
192 *retval = state->dr7;
195 case VM_REG_GUEST_EFER:
196 *retval = state->efer;
199 case VM_REG_GUEST_RAX:
200 *retval = state->rax;
203 case VM_REG_GUEST_RFLAGS:
204 *retval = state->rflags;
207 case VM_REG_GUEST_RIP:
208 *retval = state->rip;
211 case VM_REG_GUEST_RSP:
212 *retval = state->rsp;
215 case VM_REG_GUEST_CS:
216 case VM_REG_GUEST_DS:
217 case VM_REG_GUEST_ES:
218 case VM_REG_GUEST_FS:
219 case VM_REG_GUEST_GS:
220 case VM_REG_GUEST_SS:
221 case VM_REG_GUEST_LDTR:
222 case VM_REG_GUEST_TR:
223 seg = vmcb_segptr(vmcb, ident);
224 KASSERT(seg != NULL, ("%s: unable to get segment %d from VMCB",
226 *retval = seg->selector;
229 case VM_REG_GUEST_GDTR:
230 case VM_REG_GUEST_IDTR:
231 /* GDTR and IDTR don't have segment selectors */
243 * Write to segment selector, control and general purpose register of VMCB.
246 vmcb_write(struct svm_softc *sc, int vcpu, int ident, uint64_t val)
249 struct vmcb_state *state;
250 struct vmcb_segment *seg;
253 vmcb = svm_get_vmcb(sc, vcpu);
254 state = &vmcb->state;
258 if (VMCB_ACCESS_OK(ident))
259 return (vmcb_access(sc, vcpu, 1, ident, &val));
262 case VM_REG_GUEST_CR0:
264 svm_set_dirty(sc, vcpu, VMCB_CACHE_CR);
267 case VM_REG_GUEST_CR2:
269 svm_set_dirty(sc, vcpu, VMCB_CACHE_CR2);
272 case VM_REG_GUEST_CR3:
274 svm_set_dirty(sc, vcpu, VMCB_CACHE_CR);
277 case VM_REG_GUEST_CR4:
279 svm_set_dirty(sc, vcpu, VMCB_CACHE_CR);
282 case VM_REG_GUEST_DR7:
286 case VM_REG_GUEST_EFER:
287 /* EFER_SVM must always be set when the guest is executing */
288 state->efer = val | EFER_SVM;
289 svm_set_dirty(sc, vcpu, VMCB_CACHE_CR);
292 case VM_REG_GUEST_RAX:
296 case VM_REG_GUEST_RFLAGS:
300 case VM_REG_GUEST_RIP:
304 case VM_REG_GUEST_RSP:
308 case VM_REG_GUEST_CS:
309 case VM_REG_GUEST_DS:
310 case VM_REG_GUEST_ES:
311 case VM_REG_GUEST_SS:
312 dirtyseg = 1; /* FALLTHROUGH */
313 case VM_REG_GUEST_FS:
314 case VM_REG_GUEST_GS:
315 case VM_REG_GUEST_LDTR:
316 case VM_REG_GUEST_TR:
317 seg = vmcb_segptr(vmcb, ident);
318 KASSERT(seg != NULL, ("%s: unable to get segment %d from VMCB",
322 svm_set_dirty(sc, vcpu, VMCB_CACHE_SEG);
325 case VM_REG_GUEST_GDTR:
326 case VM_REG_GUEST_IDTR:
327 /* GDTR and IDTR don't have segment selectors */
339 vmcb_seg(struct vmcb *vmcb, int ident, struct vmcb_segment *seg2)
341 struct vmcb_segment *seg;
343 seg = vmcb_segptr(vmcb, ident);
345 bcopy(seg, seg2, sizeof(struct vmcb_segment));
353 vmcb_setdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
356 struct svm_softc *sc;
357 struct vmcb_segment *seg;
361 vmcb = svm_get_vmcb(sc, vcpu);
363 seg = vmcb_segptr(vmcb, reg);
364 KASSERT(seg != NULL, ("%s: invalid segment descriptor %d",
367 seg->base = desc->base;
368 seg->limit = desc->limit;
369 if (reg != VM_REG_GUEST_GDTR && reg != VM_REG_GUEST_IDTR) {
371 * Map seg_desc access to VMCB attribute format.
373 * SVM uses the 'P' bit in the segment attributes to indicate a
374 * NULL segment so clear it if the segment is marked unusable.
376 attrib = ((desc->access & 0xF000) >> 4) | (desc->access & 0xFF);
377 if (SEG_DESC_UNUSABLE(desc->access)) {
380 seg->attrib = attrib;
383 VCPU_CTR4(sc->vm, vcpu, "Setting desc %d: base (%#lx), limit (%#x), "
384 "attrib (%#x)", reg, seg->base, seg->limit, seg->attrib);
387 case VM_REG_GUEST_CS:
388 case VM_REG_GUEST_DS:
389 case VM_REG_GUEST_ES:
390 case VM_REG_GUEST_SS:
391 svm_set_dirty(sc, vcpu, VMCB_CACHE_SEG);
393 case VM_REG_GUEST_GDTR:
394 case VM_REG_GUEST_IDTR:
395 svm_set_dirty(sc, vcpu, VMCB_CACHE_DT);
405 vmcb_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
408 struct svm_softc *sc;
409 struct vmcb_segment *seg;
412 vmcb = svm_get_vmcb(sc, vcpu);
413 seg = vmcb_segptr(vmcb, reg);
414 KASSERT(seg != NULL, ("%s: invalid segment descriptor %d",
417 desc->base = seg->base;
418 desc->limit = seg->limit;
421 if (reg != VM_REG_GUEST_GDTR && reg != VM_REG_GUEST_IDTR) {
422 /* Map seg_desc access to VMCB attribute format */
423 desc->access = ((seg->attrib & 0xF00) << 4) |
424 (seg->attrib & 0xFF);
427 * VT-x uses bit 16 to indicate a segment that has been loaded
428 * with a NULL selector (aka unusable). The 'desc->access'
429 * field is interpreted in the VT-x format by the
430 * processor-independent code.
432 * SVM uses the 'P' bit to convey the same information so
433 * convert it into the VT-x format. For more details refer to
434 * section "Segment State in the VMCB" in APMv2.
436 if (reg != VM_REG_GUEST_CS && reg != VM_REG_GUEST_TR) {
437 if ((desc->access & 0x80) == 0)
438 desc->access |= 0x10000; /* Unusable segment */