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".
20 #include <palacios/vmm_shadow_paging.h>
21 #include <palacios/vmm_ctrl_regs.h>
23 #include <palacios/vm_guest.h>
24 #include <palacios/vm_guest_mem.h>
27 #ifndef V3_CONFIG_DEBUG_SHDW_PG_VTLB
29 #define PrintDebug(fmt, ...)
33 struct shadow_page_data {
37 struct list_head page_list_node;
41 struct vtlb_local_state {
43 struct list_head page_list;
48 static struct shadow_page_data * create_new_shadow_pt(struct guest_info * core);
51 #include "vmm_shdw_pg_tlb_32.h"
52 #include "vmm_shdw_pg_tlb_32pae.h"
53 #include "vmm_shdw_pg_tlb_64.h"
56 static struct shadow_page_data * create_new_shadow_pt(struct guest_info * core) {
57 struct v3_shdw_pg_state * state = &(core->shdw_pg_state);
58 struct vtlb_local_state * impl_state = (struct vtlb_local_state *)(state->local_impl_data);
59 v3_reg_t cur_cr3 = core->ctrl_regs.cr3;
60 struct shadow_page_data * page_tail = NULL;
63 if (!list_empty(&(impl_state->page_list))) {
64 page_tail = list_tail_entry(&(impl_state->page_list), struct shadow_page_data, page_list_node);
67 if (page_tail->cr3 != cur_cr3) {
68 PrintDebug("Reusing old shadow Page: %p (cur_CR3=%p)(page_cr3=%p) \n",
69 (void *)(addr_t)page_tail->page_pa,
70 (void *)(addr_t)cur_cr3,
71 (void *)(addr_t)(page_tail->cr3));
73 list_move(&(page_tail->page_list_node), &(impl_state->page_list));
75 memset(V3_VAddr((void *)(page_tail->page_pa)), 0, PAGE_SIZE_4KB);
84 page_tail = (struct shadow_page_data *)V3_Malloc(sizeof(struct shadow_page_data));
85 page_tail->page_pa = (addr_t)V3_AllocPages(1);
87 PrintDebug("Allocating new shadow Page: %p (cur_cr3=%p)\n",
88 (void *)(addr_t)page_tail->page_pa,
89 (void *)(addr_t)cur_cr3);
91 page_tail->cr3 = cur_cr3;
92 list_add(&(page_tail->page_list_node), &(impl_state->page_list));
94 shdw_page = (addr_t)V3_VAddr((void *)(page_tail->page_pa));
95 memset((void *)shdw_page, 0, PAGE_SIZE_4KB);
102 static int vtlb_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
104 V3_Print("VTLB initialization\n");
108 static int vtlb_deinit(struct v3_vm_info * vm) {
112 static int vtlb_local_init(struct guest_info * core) {
113 struct v3_shdw_pg_state * state = &(core->shdw_pg_state);
114 struct vtlb_local_state * vtlb_state = NULL;
116 V3_Print("VTLB local initialization\n");
118 vtlb_state = (struct vtlb_local_state *)V3_Malloc(sizeof(struct vtlb_local_state));
120 INIT_LIST_HEAD(&(vtlb_state->page_list));
122 state->local_impl_data = vtlb_state;
128 static int vtlb_local_deinit(struct guest_info * core) {
129 struct v3_shdw_pg_state * state = &(core->shdw_pg_state);
130 struct vtlb_local_state * vtlb_state = state->local_impl_data;
132 struct shadow_page_data * shdw_pg = NULL;
133 struct shadow_page_data * tmp = NULL;
136 list_for_each_entry_safe(shdw_pg, tmp, &(vtlb_state->page_list), page_list_node) {
137 list_del(&(shdw_pg->page_list_node));
138 V3_FreePages((void *)shdw_pg->page_pa, 1);
149 static int vtlb_activate_shdw_pt(struct guest_info * core) {
150 switch (v3_get_vm_cpu_mode(core)) {
153 return activate_shadow_pt_32(core);
155 return activate_shadow_pt_32pae(core);
159 return activate_shadow_pt_64(core);
161 PrintError("Invalid CPU mode: %s\n", v3_cpu_mode_to_str(v3_get_vm_cpu_mode(core)));
168 static int vtlb_invalidate_shdw_pt(struct guest_info * core) {
169 return vtlb_activate_shdw_pt(core);
173 static int vtlb_handle_pf(struct guest_info * core, addr_t fault_addr, pf_error_t error_code) {
175 switch (v3_get_vm_cpu_mode(core)) {
177 return handle_shadow_pagefault_32(core, fault_addr, error_code);
180 return handle_shadow_pagefault_32pae(core, fault_addr, error_code);
184 return handle_shadow_pagefault_64(core, fault_addr, error_code);
187 PrintError("Unhandled CPU Mode: %s\n", v3_cpu_mode_to_str(v3_get_vm_cpu_mode(core)));
193 static int vtlb_handle_invlpg(struct guest_info * core, addr_t vaddr) {
195 switch (v3_get_vm_cpu_mode(core)) {
197 return handle_shadow_invlpg_32(core, vaddr);
199 return handle_shadow_invlpg_32pae(core, vaddr);
203 return handle_shadow_invlpg_64(core, vaddr);
205 PrintError("Invalid CPU mode: %s\n", v3_cpu_mode_to_str(v3_get_vm_cpu_mode(core)));
210 static struct v3_shdw_pg_impl vtlb_impl = {
213 .deinit = vtlb_deinit,
214 .local_init = vtlb_local_init,
215 .local_deinit = vtlb_local_deinit,
216 .handle_pagefault = vtlb_handle_pf,
217 .handle_invlpg = vtlb_handle_invlpg,
218 .activate_shdw_pt = vtlb_activate_shdw_pt,
219 .invalidate_shdw_pt = vtlb_invalidate_shdw_pt
226 register_shdw_pg_impl(&vtlb_impl);