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 #define SYM_CALL_RET_HCALL 0x535
33 static int msr_read(uint_t msr, struct v3_msr * dst, void * priv_data) {
34 struct guest_info * info = (struct guest_info *)priv_data;
35 struct v3_sym_state * state = &(info->sym_state);
37 dst->value = state->guest_pg_addr;
42 static int msr_write(uint_t msr, struct v3_msr src, void * priv_data) {
43 struct guest_info * info = (struct guest_info *)priv_data;
44 struct v3_sym_state * state = &(info->sym_state);
47 PrintDebug("Symbiotic MSR write for page %p\n", (void *)src.value);
49 if (state->active == 1) {
51 struct v3_shadow_region * old_reg = v3_get_shadow_region(info, (addr_t)state->guest_pg_addr);
53 if (old_reg == NULL) {
54 PrintError("Could not find previously active symbiotic page (%p)\n", (void *)state->guest_pg_addr);
58 v3_delete_shadow_region(info, old_reg);
61 state->guest_pg_addr = src.value;
62 state->guest_pg_addr &= ~0xfffLL;
67 v3_add_shadow_mem(info, (addr_t)state->guest_pg_addr,
68 (addr_t)(state->guest_pg_addr + PAGE_SIZE_4KB - 1),
74 static int cpuid_fn(struct guest_info * info, uint32_t cpuid,
75 uint32_t * eax, uint32_t * ebx,
76 uint32_t * ecx, uint32_t * edx,
77 void * private_data) {
79 memset(eax, 0, sizeof(uint32_t));
80 memcpy(eax, "V3V", 3);
86 static int sym_call_ret(struct guest_info * info, uint_t hcall_id, void * private_data);
90 int v3_init_sym_iface(struct guest_info * info) {
91 struct v3_sym_state * state = &(info->sym_state);
92 memset(state, 0, sizeof(struct v3_sym_state));
94 state->sym_page_pa = (addr_t)V3_AllocPages(1);
95 state->sym_page = (struct v3_sym_interface *)V3_VAddr((void *)state->sym_page_pa);
96 memset(state->sym_page, 0, PAGE_SIZE_4KB);
99 memcpy(&(state->sym_page->magic), "V3V", 3);
101 v3_hook_msr(info, SYM_PAGE_MSR, msr_read, msr_write, info);
103 v3_hook_cpuid(info, SYM_CPUID_NUM, cpuid_fn, info);
106 v3_register_hypercall(info, SYM_CALL_RET_HCALL, sym_call_ret, NULL);
111 int v3_sym_map_pci_passthrough(struct guest_info * info, uint_t bus, uint_t dev, uint_t fn) {
112 struct v3_sym_state * state = &(info->sym_state);
113 uint_t dev_index = (bus << 8) + (dev << 3) + fn;
114 uint_t major = dev_index / 8;
115 uint_t minor = dev_index % 8;
118 PrintError("Invalid PCI bus %d\n", bus);
122 PrintDebug("Setting passthrough pci map for index=%d\n", dev_index);
124 state->sym_page->pci_pt_map[major] |= 0x1 << minor;
126 PrintDebug("pt_map entry=%x\n", state->sym_page->pci_pt_map[major]);
128 PrintDebug("pt map vmm addr=%p\n", state->sym_page->pci_pt_map);
133 int v3_sym_unmap_pci_passthrough(struct guest_info * info, uint_t bus, uint_t dev, uint_t fn) {
134 struct v3_sym_state * state = &(info->sym_state);
135 uint_t dev_index = (bus << 8) + (dev << 3) + fn;
136 uint_t major = dev_index / 8;
137 uint_t minor = dev_index % 8;
140 PrintError("Invalid PCI bus %d\n", bus);
144 state->sym_page->pci_pt_map[major] &= ~(0x1 << minor);
151 static int sym_call_ret(struct guest_info * info, uint_t hcall_id, void * private_data) {
152 struct v3_sym_state * state = (struct v3_sym_state *)&(info->sym_state);
153 struct v3_sym_context * old_ctx = (struct v3_sym_context *)&(state->old_ctx);
156 PrintError("Return from sym call\n");
157 v3_print_guest_state(info);
158 v3_print_mem_map(info);
161 if (state->notifier != NULL) {
162 if (state->notifier(info, state->private_data) == -1) {
163 PrintError("Error in return from symcall.\n");
169 // restore guest state
170 memcpy(&(info->vm_regs), &(old_ctx->vm_regs), sizeof(struct v3_gprs));
171 memcpy(&(info->segments.cs), &(old_ctx->cs), sizeof(struct v3_segment));
172 memcpy(&(info->segments.ss), &(old_ctx->ss), sizeof(struct v3_segment));
173 info->segments.gs.base = old_ctx->gs_base;
174 info->segments.fs.base = old_ctx->fs_base;
175 info->rip = old_ctx->rip;
176 info->cpl = old_ctx->cpl;
179 PrintDebug("restoring guest state\n");
180 v3_print_guest_state(info);
183 state->call_active = 0;
190 int v3_sym_call(struct guest_info * info,
191 uint64_t arg0, uint64_t arg1,
192 uint64_t arg2, uint64_t arg3,
193 uint64_t arg4, uint64_t arg5,
194 int (*notifier)(struct guest_info * info, void * private_data),
195 void * private_data) {
196 struct v3_sym_state * state = (struct v3_sym_state *)&(info->sym_state);
199 PrintDebug("Making Sym call\n");
201 if ((state->sym_page->sym_call_enabled == 0) ||
202 (state->call_active == 1) ||
203 (state->call_pending == 1)) {
207 state->args[0] = arg0;
208 state->args[1] = arg1;
209 state->args[2] = arg2;
210 state->args[3] = arg3;
211 state->args[4] = arg4;
212 state->args[5] = arg5;
214 state->notifier = notifier;
215 state->private_data = private_data;
217 state->call_pending = 1;
224 int v3_activate_sym_call(struct guest_info * info) {
225 struct v3_sym_state * state = (struct v3_sym_state *)&(info->sym_state);
226 struct v3_sym_context * old_ctx = (struct v3_sym_context *)&(state->old_ctx);
227 struct v3_segment sym_cs;
228 struct v3_segment sym_ss;
231 if ((state->sym_page->sym_call_enabled == 0) ||
232 (state->call_pending == 0)) {
233 // Unable to make sym call or none pending
234 if (state->call_active == 1) {
235 PrintError("handled exit while in symcall\n");
241 PrintDebug("Activating Symbiotic call\n");
242 v3_print_guest_state(info);
245 // Save the old context
246 memcpy(&(old_ctx->vm_regs), &(info->vm_regs), sizeof(struct v3_gprs));
247 memcpy(&(old_ctx->cs), &(info->segments.cs), sizeof(struct v3_segment));
248 memcpy(&(old_ctx->ss), &(info->segments.ss), sizeof(struct v3_segment));
249 old_ctx->gs_base = info->segments.gs.base;
250 old_ctx->fs_base = info->segments.fs.base;
251 old_ctx->rip = info->rip;
252 old_ctx->cpl = info->cpl;
256 // Setup the sym call context
257 info->rip = state->sym_page->sym_call_rip;
258 info->vm_regs.rsp = state->sym_page->sym_call_rsp;
260 v3_translate_segment(info, state->sym_page->sym_call_cs, &sym_cs);
261 memcpy(&(info->segments.cs), &sym_cs, sizeof(struct v3_segment));
263 v3_translate_segment(info, state->sym_page->sym_call_cs + 8, &sym_ss);
264 memcpy(&(info->segments.ss), &sym_ss, sizeof(struct v3_segment));
266 info->segments.gs.base = state->sym_page->sym_call_gs;
267 info->segments.fs.base = 0;
270 info->vm_regs.rax = state->args[0];
271 info->vm_regs.rbx = state->args[1];
272 info->vm_regs.rcx = state->args[2];
273 info->vm_regs.rdx = state->args[3];
274 info->vm_regs.rsi = state->args[4];
275 info->vm_regs.rdi = state->args[5];
277 // Mark sym call as active
278 state->call_pending = 0;
279 state->call_active = 1;
282 PrintDebug("Sym state\n");
283 v3_print_guest_state(info);