2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
21 #include <palacios/vmm.h>
22 #include <palacios/vmm_msr.h>
23 #include <palacios/vmm_mem.h>
24 #include <palacios/vmm_hypercall.h>
26 #define SYM_PAGE_MSR 0x535
28 #define SYM_CPUID_NUM 0x90000000
30 // A succesfull symcall returns via the RET_HCALL, with the return values in registers
31 // A symcall error returns via the ERR_HCALL with the error code in rbx
32 #define SYM_CALL_RET_HCALL 0x535
33 #define SYM_CALL_ERR_HCALL 0x536
36 /* Notes: We use a combination of SYSCALL and SYSENTER Semantics
37 * SYSCALL just sets an EIP, CS/SS seg, and GS seg via swapgs
38 * the RSP is loaded via the structure pointed to by GS
39 * This is safe because it assumes that system calls are guaranteed to be made with an empty kernel stack.
40 * We cannot make that assumption with a symcall, so we have to have our own stack area somewhere.
41 * SYSTENTER does not really use the GS base MSRs, but we do to map to 64 bit kernels
44 #define SYMCALL_RIP_MSR 0x536
45 #define SYMCALL_RSP_MSR 0x537
46 #define SYMCALL_CS_MSR 0x538
47 #define SYMCALL_GS_MSR 0x539
48 #define SYMCALL_FS_MSR 0x540
51 static int msr_read(uint_t msr, struct v3_msr * dst, void * priv_data) {
52 struct guest_info * info = (struct guest_info *)priv_data;
53 struct v3_sym_state * state = &(info->sym_state);
57 dst->value = state->guest_pg_addr;
60 dst->value = state->sym_call_rip;
63 dst->value = state->sym_call_rsp;
66 dst->value = state->sym_call_cs;
69 dst->value = state->sym_call_gs;
72 dst->value = state->sym_call_fs;
81 static int msr_write(uint_t msr, struct v3_msr src, void * priv_data) {
82 struct guest_info * info = (struct guest_info *)priv_data;
83 struct v3_sym_state * state = &(info->sym_state);
85 if (msr == SYM_PAGE_MSR) {
86 PrintDebug("Symbiotic MSR write for page %p\n", (void *)src.value);
88 if (state->active == 1) {
90 struct v3_shadow_region * old_reg = v3_get_shadow_region(info, (addr_t)state->guest_pg_addr);
92 if (old_reg == NULL) {
93 PrintError("Could not find previously active symbiotic page (%p)\n", (void *)state->guest_pg_addr);
97 v3_delete_shadow_region(info, old_reg);
100 state->guest_pg_addr = src.value;
101 state->guest_pg_addr &= ~0xfffLL;
106 v3_add_shadow_mem(info, (addr_t)state->guest_pg_addr,
107 (addr_t)(state->guest_pg_addr + PAGE_SIZE_4KB - 1),
111 } else if (msr == SYMCALL_RIP_MSR) {
112 state->sym_call_rip = src.value;
113 } else if (msr == SYMCALL_RSP_MSR) {
114 state->sym_call_rsp = src.value;
115 } else if (msr == SYMCALL_CS_MSR) {
116 state->sym_call_cs = src.value;
117 } else if (msr == SYMCALL_GS_MSR) {
118 state->sym_call_gs = src.value;
119 } else if (msr == SYMCALL_FS_MSR) {
120 state->sym_call_fs = src.value;
122 PrintError("Invalid Symbiotic MSR write (0x%x)\n", msr);
129 static int cpuid_fn(struct guest_info * info, uint32_t cpuid,
130 uint32_t * eax, uint32_t * ebx,
131 uint32_t * ecx, uint32_t * edx,
132 void * private_data) {
133 extern v3_cpu_arch_t v3_cpu_types[];
135 *eax = *(uint32_t *)"V3V";
137 if ((v3_cpu_types[info->cpu_id] == V3_SVM_CPU) ||
138 (v3_cpu_types[info->cpu_id] == V3_SVM_REV3_CPU)) {
139 *ebx = *(uint32_t *)"SVM";
140 } else if ((v3_cpu_types[info->cpu_id] == V3_VMX_CPU) ||
141 (v3_cpu_types[info->cpu_id] == V3_VMX_EPT_CPU)) {
142 *ebx = *(uint32_t *)"VMX";
150 static int sym_call_ret(struct guest_info * info, uint_t hcall_id, void * private_data);
151 static int sym_call_err(struct guest_info * info, uint_t hcall_id, void * private_data);
155 int v3_init_sym_iface(struct guest_info * info) {
156 struct v3_sym_state * state = &(info->sym_state);
157 memset(state, 0, sizeof(struct v3_sym_state));
159 state->sym_page_pa = (addr_t)V3_AllocPages(1);
160 state->sym_page = (struct v3_sym_interface *)V3_VAddr((void *)state->sym_page_pa);
161 memset(state->sym_page, 0, PAGE_SIZE_4KB);
164 memcpy(&(state->sym_page->magic), "V3V", 3);
166 v3_hook_msr(info, SYM_PAGE_MSR, msr_read, msr_write, info);
168 v3_hook_cpuid(info, SYM_CPUID_NUM, cpuid_fn, info);
170 v3_hook_msr(info, SYMCALL_RIP_MSR, msr_read, msr_write, info);
171 v3_hook_msr(info, SYMCALL_RSP_MSR, msr_read, msr_write, info);
172 v3_hook_msr(info, SYMCALL_CS_MSR, msr_read, msr_write, info);
173 v3_hook_msr(info, SYMCALL_GS_MSR, msr_read, msr_write, info);
174 v3_hook_msr(info, SYMCALL_FS_MSR, msr_read, msr_write, info);
176 v3_register_hypercall(info, SYM_CALL_RET_HCALL, sym_call_ret, NULL);
177 v3_register_hypercall(info, SYM_CALL_ERR_HCALL, sym_call_err, NULL);
182 int v3_sym_map_pci_passthrough(struct guest_info * info, uint_t bus, uint_t dev, uint_t fn) {
183 struct v3_sym_state * state = &(info->sym_state);
184 uint_t dev_index = (bus << 8) + (dev << 3) + fn;
185 uint_t major = dev_index / 8;
186 uint_t minor = dev_index % 8;
189 PrintError("Invalid PCI bus %d\n", bus);
193 PrintDebug("Setting passthrough pci map for index=%d\n", dev_index);
195 state->sym_page->pci_pt_map[major] |= 0x1 << minor;
197 PrintDebug("pt_map entry=%x\n", state->sym_page->pci_pt_map[major]);
199 PrintDebug("pt map vmm addr=%p\n", state->sym_page->pci_pt_map);
204 int v3_sym_unmap_pci_passthrough(struct guest_info * info, uint_t bus, uint_t dev, uint_t fn) {
205 struct v3_sym_state * state = &(info->sym_state);
206 uint_t dev_index = (bus << 8) + (dev << 3) + fn;
207 uint_t major = dev_index / 8;
208 uint_t minor = dev_index % 8;
211 PrintError("Invalid PCI bus %d\n", bus);
215 state->sym_page->pci_pt_map[major] &= ~(0x1 << minor);
221 static int sym_call_err(struct guest_info * info, uint_t hcall_id, void * private_data) {
222 struct v3_sym_state * state = (struct v3_sym_state *)&(info->sym_state);
224 PrintError("sym call error\n");
226 state->sym_call_errno = (int)info->vm_regs.rbx;
227 v3_print_guest_state(info);
228 v3_print_mem_map(info);
231 state->sym_call_error = 1;
232 state->sym_call_returned = 1;
237 static int sym_call_ret(struct guest_info * info, uint_t hcall_id, void * private_data) {
238 struct v3_sym_state * state = (struct v3_sym_state *)&(info->sym_state);
240 PrintError("Return from sym call\n");
241 v3_print_guest_state(info);
242 v3_print_mem_map(info);
244 state->sym_call_returned = 1;
249 static int execute_symcall(struct guest_info * info) {
251 while (info->sym_state.sym_call_returned == 0) {
252 if (v3_vm_enter(info) == -1) {
253 PrintError("Error in Sym call\n");
262 int v3_sym_call(struct guest_info * info,
263 uint64_t call_num, sym_arg_t * arg0,
264 sym_arg_t * arg1, sym_arg_t * arg2,
265 sym_arg_t * arg3, sym_arg_t * arg4) {
266 struct v3_sym_state * state = (struct v3_sym_state *)&(info->sym_state);
267 struct v3_sym_context * old_ctx = (struct v3_sym_context *)&(state->old_ctx);
268 struct v3_segment sym_cs;
269 struct v3_segment sym_ss;
270 uint64_t trash_args[5] = { [0 ... 4] = 0 };
272 PrintDebug("Making Sym call\n");
273 v3_print_guest_state(info);
275 if ((state->sym_page->sym_call_enabled == 0) ||
276 (state->sym_call_active == 1)) {
280 if (!arg0) arg0 = &trash_args[0];
281 if (!arg1) arg1 = &trash_args[1];
282 if (!arg2) arg2 = &trash_args[2];
283 if (!arg3) arg3 = &trash_args[3];
284 if (!arg4) arg4 = &trash_args[4];
286 // Save the old context
287 memcpy(&(old_ctx->vm_regs), &(info->vm_regs), sizeof(struct v3_gprs));
288 memcpy(&(old_ctx->cs), &(info->segments.cs), sizeof(struct v3_segment));
289 memcpy(&(old_ctx->ss), &(info->segments.ss), sizeof(struct v3_segment));
290 old_ctx->gs_base = info->segments.gs.base;
291 old_ctx->fs_base = info->segments.fs.base;
292 old_ctx->rip = info->rip;
293 old_ctx->cpl = info->cpl;
294 old_ctx->flags = info->ctrl_regs.rflags;
296 // Setup the sym call context
297 info->rip = state->sym_call_rip;
298 info->vm_regs.rsp = state->sym_call_rsp; // old contest rsp is saved in vm_regs
300 v3_translate_segment(info, state->sym_call_cs, &sym_cs);
301 memcpy(&(info->segments.cs), &sym_cs, sizeof(struct v3_segment));
303 v3_translate_segment(info, state->sym_call_cs + 8, &sym_ss);
304 memcpy(&(info->segments.ss), &sym_ss, sizeof(struct v3_segment));
306 info->segments.gs.base = state->sym_call_gs;
307 info->segments.fs.base = state->sym_call_fs;
310 info->vm_regs.rax = call_num;
311 info->vm_regs.rbx = *arg0;
312 info->vm_regs.rcx = *arg1;
313 info->vm_regs.rdx = *arg2;
314 info->vm_regs.rsi = *arg3;
315 info->vm_regs.rdi = *arg4;
317 // Mark sym call as active
318 state->sym_call_active = 1;
319 state->sym_call_returned = 0;
321 PrintDebug("Sym state\n");
322 v3_print_guest_state(info);
324 // Do the sym call entry
325 if (execute_symcall(info) == -1) {
326 PrintError("SYMCALL error\n");
331 state->sym_call_active = 0;
333 *arg0 = info->vm_regs.rbx;
334 *arg1 = info->vm_regs.rcx;
335 *arg2 = info->vm_regs.rdx;
336 *arg3 = info->vm_regs.rsi;
337 *arg4 = info->vm_regs.rdi;
339 // restore guest state
340 memcpy(&(info->vm_regs), &(old_ctx->vm_regs), sizeof(struct v3_gprs));
341 memcpy(&(info->segments.cs), &(old_ctx->cs), sizeof(struct v3_segment));
342 memcpy(&(info->segments.ss), &(old_ctx->ss), sizeof(struct v3_segment));
343 info->segments.gs.base = old_ctx->gs_base;
344 info->segments.fs.base = old_ctx->fs_base;
345 info->rip = old_ctx->rip;
346 info->cpl = old_ctx->cpl;
347 info->ctrl_regs.rflags = old_ctx->flags;
351 PrintDebug("restoring guest state\n");
352 v3_print_guest_state(info);