2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 Anish Gupta (akgupt3@gmail.com)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_bhyve_snapshot.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <machine/segments.h>
38 #include <machine/specialreg.h>
39 #include <machine/vmm.h>
40 #include <machine/vmm_snapshot.h>
46 #include "svm_softc.h"
49 * The VMCB aka Virtual Machine Control Block is a 4KB aligned page
50 * in memory that describes the virtual machine.
53 * - instructions or events in the guest to intercept
54 * - control bits that modify execution environment of the guest
55 * - guest processor state (e.g. general purpose registers)
59 * Return VMCB segment area.
61 static struct vmcb_segment *
62 vmcb_segptr(struct vmcb *vmcb, int type)
64 struct vmcb_state *state;
65 struct vmcb_segment *seg;
94 case VM_REG_GUEST_GDTR:
98 case VM_REG_GUEST_IDTR:
102 case VM_REG_GUEST_LDTR:
106 case VM_REG_GUEST_TR:
119 vmcb_access(struct svm_softc *softc, int vcpu, int write, int ident,
126 vmcb = svm_get_vmcb(softc, vcpu);
127 off = VMCB_ACCESS_OFFSET(ident);
128 bytes = VMCB_ACCESS_BYTES(ident);
130 if ((off + bytes) >= sizeof (struct vmcb))
143 memcpy(ptr + off, val, bytes);
145 memcpy(val, ptr + off, bytes);
148 VCPU_CTR1(softc->vm, vcpu,
149 "Invalid size %d for VMCB access: %d", bytes);
153 /* Invalidate all VMCB state cached by h/w. */
155 svm_set_dirty(softc, vcpu, 0xffffffff);
161 * Read from segment selector, control and general purpose register of VMCB.
164 vmcb_read(struct svm_softc *sc, int vcpu, int ident, uint64_t *retval)
167 struct vmcb_state *state;
168 struct vmcb_segment *seg;
171 vmcb = svm_get_vmcb(sc, vcpu);
172 state = &vmcb->state;
175 if (VMCB_ACCESS_OK(ident))
176 return (vmcb_access(sc, vcpu, 0, ident, retval));
179 case VM_REG_GUEST_CR0:
180 *retval = state->cr0;
183 case VM_REG_GUEST_CR2:
184 *retval = state->cr2;
187 case VM_REG_GUEST_CR3:
188 *retval = state->cr3;
191 case VM_REG_GUEST_CR4:
192 *retval = state->cr4;
195 case VM_REG_GUEST_DR6:
196 *retval = state->dr6;
199 case VM_REG_GUEST_DR7:
200 *retval = state->dr7;
203 case VM_REG_GUEST_EFER:
204 *retval = state->efer;
207 case VM_REG_GUEST_RAX:
208 *retval = state->rax;
211 case VM_REG_GUEST_RFLAGS:
212 *retval = state->rflags;
215 case VM_REG_GUEST_RIP:
216 *retval = state->rip;
219 case VM_REG_GUEST_RSP:
220 *retval = state->rsp;
223 case VM_REG_GUEST_CS:
224 case VM_REG_GUEST_DS:
225 case VM_REG_GUEST_ES:
226 case VM_REG_GUEST_FS:
227 case VM_REG_GUEST_GS:
228 case VM_REG_GUEST_SS:
229 case VM_REG_GUEST_LDTR:
230 case VM_REG_GUEST_TR:
231 seg = vmcb_segptr(vmcb, ident);
232 KASSERT(seg != NULL, ("%s: unable to get segment %d from VMCB",
234 *retval = seg->selector;
237 case VM_REG_GUEST_GDTR:
238 case VM_REG_GUEST_IDTR:
239 /* GDTR and IDTR don't have segment selectors */
251 * Write to segment selector, control and general purpose register of VMCB.
254 vmcb_write(struct svm_softc *sc, int vcpu, int ident, uint64_t val)
257 struct vmcb_state *state;
258 struct vmcb_segment *seg;
261 vmcb = svm_get_vmcb(sc, vcpu);
262 state = &vmcb->state;
266 if (VMCB_ACCESS_OK(ident))
267 return (vmcb_access(sc, vcpu, 1, ident, &val));
270 case VM_REG_GUEST_CR0:
272 svm_set_dirty(sc, vcpu, VMCB_CACHE_CR);
275 case VM_REG_GUEST_CR2:
277 svm_set_dirty(sc, vcpu, VMCB_CACHE_CR2);
280 case VM_REG_GUEST_CR3:
282 svm_set_dirty(sc, vcpu, VMCB_CACHE_CR);
285 case VM_REG_GUEST_CR4:
287 svm_set_dirty(sc, vcpu, VMCB_CACHE_CR);
290 case VM_REG_GUEST_DR6:
292 svm_set_dirty(sc, vcpu, VMCB_CACHE_DR);
295 case VM_REG_GUEST_DR7:
297 svm_set_dirty(sc, vcpu, VMCB_CACHE_DR);
300 case VM_REG_GUEST_EFER:
301 /* EFER_SVM must always be set when the guest is executing */
302 state->efer = val | EFER_SVM;
303 svm_set_dirty(sc, vcpu, VMCB_CACHE_CR);
306 case VM_REG_GUEST_RAX:
310 case VM_REG_GUEST_RFLAGS:
314 case VM_REG_GUEST_RIP:
318 case VM_REG_GUEST_RSP:
322 case VM_REG_GUEST_CS:
323 case VM_REG_GUEST_DS:
324 case VM_REG_GUEST_ES:
325 case VM_REG_GUEST_SS:
326 dirtyseg = 1; /* FALLTHROUGH */
327 case VM_REG_GUEST_FS:
328 case VM_REG_GUEST_GS:
329 case VM_REG_GUEST_LDTR:
330 case VM_REG_GUEST_TR:
331 seg = vmcb_segptr(vmcb, ident);
332 KASSERT(seg != NULL, ("%s: unable to get segment %d from VMCB",
336 svm_set_dirty(sc, vcpu, VMCB_CACHE_SEG);
339 case VM_REG_GUEST_GDTR:
340 case VM_REG_GUEST_IDTR:
341 /* GDTR and IDTR don't have segment selectors */
353 vmcb_seg(struct vmcb *vmcb, int ident, struct vmcb_segment *seg2)
355 struct vmcb_segment *seg;
357 seg = vmcb_segptr(vmcb, ident);
359 bcopy(seg, seg2, sizeof(struct vmcb_segment));
367 vmcb_setdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
370 struct svm_softc *sc;
371 struct vmcb_segment *seg;
375 vmcb = svm_get_vmcb(sc, vcpu);
377 seg = vmcb_segptr(vmcb, reg);
378 KASSERT(seg != NULL, ("%s: invalid segment descriptor %d",
381 seg->base = desc->base;
382 seg->limit = desc->limit;
383 if (reg != VM_REG_GUEST_GDTR && reg != VM_REG_GUEST_IDTR) {
385 * Map seg_desc access to VMCB attribute format.
387 * SVM uses the 'P' bit in the segment attributes to indicate a
388 * NULL segment so clear it if the segment is marked unusable.
390 attrib = ((desc->access & 0xF000) >> 4) | (desc->access & 0xFF);
391 if (SEG_DESC_UNUSABLE(desc->access)) {
394 seg->attrib = attrib;
397 VCPU_CTR4(sc->vm, vcpu, "Setting desc %d: base (%#lx), limit (%#x), "
398 "attrib (%#x)", reg, seg->base, seg->limit, seg->attrib);
401 case VM_REG_GUEST_CS:
402 case VM_REG_GUEST_DS:
403 case VM_REG_GUEST_ES:
404 case VM_REG_GUEST_SS:
405 svm_set_dirty(sc, vcpu, VMCB_CACHE_SEG);
407 case VM_REG_GUEST_GDTR:
408 case VM_REG_GUEST_IDTR:
409 svm_set_dirty(sc, vcpu, VMCB_CACHE_DT);
419 vmcb_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc)
422 struct svm_softc *sc;
423 struct vmcb_segment *seg;
426 vmcb = svm_get_vmcb(sc, vcpu);
427 seg = vmcb_segptr(vmcb, reg);
428 KASSERT(seg != NULL, ("%s: invalid segment descriptor %d",
431 desc->base = seg->base;
432 desc->limit = seg->limit;
435 if (reg != VM_REG_GUEST_GDTR && reg != VM_REG_GUEST_IDTR) {
436 /* Map seg_desc access to VMCB attribute format */
437 desc->access = ((seg->attrib & 0xF00) << 4) |
438 (seg->attrib & 0xFF);
441 * VT-x uses bit 16 to indicate a segment that has been loaded
442 * with a NULL selector (aka unusable). The 'desc->access'
443 * field is interpreted in the VT-x format by the
444 * processor-independent code.
446 * SVM uses the 'P' bit to convey the same information so
447 * convert it into the VT-x format. For more details refer to
448 * section "Segment State in the VMCB" in APMv2.
450 if (reg != VM_REG_GUEST_CS && reg != VM_REG_GUEST_TR) {
451 if ((desc->access & 0x80) == 0)
452 desc->access |= 0x10000; /* Unusable segment */
459 #ifdef BHYVE_SNAPSHOT
461 vmcb_getany(struct svm_softc *sc, int vcpu, int ident, uint64_t *val)
465 if (vcpu < 0 || vcpu >= VM_MAXCPU) {
470 if (ident >= VM_REG_LAST) {
475 error = vmcb_read(sc, vcpu, ident, val);
482 vmcb_setany(struct svm_softc *sc, int vcpu, int ident, uint64_t val)
486 if (vcpu < 0 || vcpu >= VM_MAXCPU) {
491 if (ident >= VM_REG_LAST) {
496 error = vmcb_write(sc, vcpu, ident, val);
503 vmcb_snapshot_desc(void *arg, int vcpu, int reg, struct vm_snapshot_meta *meta)
506 struct seg_desc desc;
508 if (meta->op == VM_SNAPSHOT_SAVE) {
509 ret = vmcb_getdesc(arg, vcpu, reg, &desc);
513 SNAPSHOT_VAR_OR_LEAVE(desc.base, meta, ret, done);
514 SNAPSHOT_VAR_OR_LEAVE(desc.limit, meta, ret, done);
515 SNAPSHOT_VAR_OR_LEAVE(desc.access, meta, ret, done);
516 } else if (meta->op == VM_SNAPSHOT_RESTORE) {
517 SNAPSHOT_VAR_OR_LEAVE(desc.base, meta, ret, done);
518 SNAPSHOT_VAR_OR_LEAVE(desc.limit, meta, ret, done);
519 SNAPSHOT_VAR_OR_LEAVE(desc.access, meta, ret, done);
521 ret = vmcb_setdesc(arg, vcpu, reg, &desc);
534 vmcb_snapshot_any(struct svm_softc *sc, int vcpu, int ident,
535 struct vm_snapshot_meta *meta)
540 if (meta->op == VM_SNAPSHOT_SAVE) {
541 ret = vmcb_getany(sc, vcpu, ident, &val);
545 SNAPSHOT_VAR_OR_LEAVE(val, meta, ret, done);
546 } else if (meta->op == VM_SNAPSHOT_RESTORE) {
547 SNAPSHOT_VAR_OR_LEAVE(val, meta, ret, done);
549 ret = vmcb_setany(sc, vcpu, ident, val);