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 * The VMCB aka Virtual Machine Control Block is a 4KB aligned page
43 * in memory that describes the virtual machine.
46 * - instructions or events in the guest to intercept
47 * - control bits that modify execution environment of the guest
48 * - guest processor state (e.g. general purpose registers)
52 * Read from segment selector, control and general purpose register of VMCB.
55 vmcb_read(struct vmcb *vmcb, int ident, uint64_t *retval)
57 struct vmcb_state *state;
58 struct vmcb_segment *seg;
65 case VM_REG_GUEST_CR0:
69 case VM_REG_GUEST_CR2:
73 case VM_REG_GUEST_CR3:
77 case VM_REG_GUEST_CR4:
81 case VM_REG_GUEST_DR7:
85 case VM_REG_GUEST_EFER:
86 *retval = state->efer;
89 case VM_REG_GUEST_RAX:
93 case VM_REG_GUEST_RFLAGS:
94 *retval = state->rflags;
97 case VM_REG_GUEST_RIP:
101 case VM_REG_GUEST_RSP:
102 *retval = state->rsp;
105 case VM_REG_GUEST_CS:
106 case VM_REG_GUEST_DS:
107 case VM_REG_GUEST_ES:
108 case VM_REG_GUEST_FS:
109 case VM_REG_GUEST_GS:
110 case VM_REG_GUEST_SS:
111 case VM_REG_GUEST_GDTR:
112 case VM_REG_GUEST_IDTR:
113 case VM_REG_GUEST_LDTR:
114 case VM_REG_GUEST_TR:
115 seg = vmcb_seg(vmcb, ident);
117 ERR("Invalid seg type %d\n", ident);
122 *retval = seg->selector;
134 * Write to segment selector, control and general purpose register of VMCB.
137 vmcb_write(struct vmcb *vmcb, int ident, uint64_t val)
139 struct vmcb_state *state;
140 struct vmcb_segment *seg;
143 state = &vmcb->state;
147 case VM_REG_GUEST_CR0:
151 case VM_REG_GUEST_CR2:
155 case VM_REG_GUEST_CR3:
159 case VM_REG_GUEST_CR4:
163 case VM_REG_GUEST_DR7:
167 case VM_REG_GUEST_EFER:
168 /* EFER_SVM must always be set when the guest is executing */
169 state->efer = val | EFER_SVM;
172 case VM_REG_GUEST_RAX:
176 case VM_REG_GUEST_RFLAGS:
180 case VM_REG_GUEST_RIP:
184 case VM_REG_GUEST_RSP:
188 case VM_REG_GUEST_CS:
189 case VM_REG_GUEST_DS:
190 case VM_REG_GUEST_ES:
191 case VM_REG_GUEST_FS:
192 case VM_REG_GUEST_GS:
193 case VM_REG_GUEST_SS:
194 case VM_REG_GUEST_GDTR:
195 case VM_REG_GUEST_IDTR:
196 case VM_REG_GUEST_LDTR:
197 case VM_REG_GUEST_TR:
198 seg = vmcb_seg(vmcb, ident);
200 ERR("Invalid segment type %d\n", ident);
216 * Return VMCB segment area.
218 struct vmcb_segment *
219 vmcb_seg(struct vmcb *vmcb, int type)
221 struct vmcb_state *state;
222 struct vmcb_segment *seg;
224 state = &vmcb->state;
227 case VM_REG_GUEST_CS:
231 case VM_REG_GUEST_DS:
235 case VM_REG_GUEST_ES:
239 case VM_REG_GUEST_FS:
243 case VM_REG_GUEST_GS:
247 case VM_REG_GUEST_SS:
251 case VM_REG_GUEST_GDTR:
255 case VM_REG_GUEST_IDTR:
259 case VM_REG_GUEST_LDTR:
263 case VM_REG_GUEST_TR: