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>
26 struct shadow_page_data {
30 struct list_head page_list_node;
34 struct vtlb_local_state {
36 struct list_head page_list;
41 static struct shadow_page_data * create_new_shadow_pt(struct guest_info * core);
44 #include "vmm_shdw_pg_tlb_32.h"
45 #include "vmm_shdw_pg_tlb_32pae.h"
46 #include "vmm_shdw_pg_tlb_64.h"
49 static struct shadow_page_data * create_new_shadow_pt(struct guest_info * core) {
50 struct v3_shdw_pg_state * state = &(core->shdw_pg_state);
51 struct vtlb_local_state * impl_state = (struct vtlb_local_state *)(state->local_impl_data);
52 v3_reg_t cur_cr3 = core->ctrl_regs.cr3;
53 struct shadow_page_data * page_tail = NULL;
56 if (!list_empty(&(impl_state->page_list))) {
57 page_tail = list_tail_entry(&(impl_state->page_list), struct shadow_page_data, page_list_node);
60 if (page_tail->cr3 != cur_cr3) {
61 PrintDebug("Reusing old shadow Page: %p (cur_CR3=%p)(page_cr3=%p) \n",
62 (void *)(addr_t)page_tail->page_pa,
63 (void *)(addr_t)cur_cr3,
64 (void *)(addr_t)(page_tail->cr3));
66 list_move(&(page_tail->page_list_node), &(impl_state->page_list));
68 memset(V3_VAddr((void *)(page_tail->page_pa)), 0, PAGE_SIZE_4KB);
77 page_tail = (struct shadow_page_data *)V3_Malloc(sizeof(struct shadow_page_data));
78 page_tail->page_pa = (addr_t)V3_AllocPages(1);
80 PrintDebug("Allocating new shadow Page: %p (cur_cr3=%p)\n",
81 (void *)(addr_t)page_tail->page_pa,
82 (void *)(addr_t)cur_cr3);
84 page_tail->cr3 = cur_cr3;
85 list_add(&(page_tail->page_list_node), &(impl_state->page_list));
87 shdw_page = (addr_t)V3_VAddr((void *)(page_tail->page_pa));
88 memset((void *)shdw_page, 0, PAGE_SIZE_4KB);
95 static int vtlb_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
97 V3_Print("VTLB initialization\n");
101 static int vtlb_deinit(struct v3_vm_info * vm) {
105 static int vtlb_local_init(struct guest_info * core) {
106 struct v3_shdw_pg_state * state = &(core->shdw_pg_state);
107 struct vtlb_local_state * vtlb_state = NULL;
109 V3_Print("VTLB local initialization\n");
112 vtlb_state = (struct vtlb_local_state *)V3_Malloc(sizeof(struct vtlb_local_state));
114 INIT_LIST_HEAD(&(vtlb_state->page_list));
116 state->local_impl_data = vtlb_state;
122 static int vtlb_activate_shdw_pt(struct guest_info * core) {
123 switch (v3_get_vm_cpu_mode(core)) {
126 return activate_shadow_pt_32(core);
128 return activate_shadow_pt_32pae(core);
132 return activate_shadow_pt_64(core);
134 PrintError("Invalid CPU mode: %s\n", v3_cpu_mode_to_str(v3_get_vm_cpu_mode(core)));
141 static int vtlb_invalidate_shdw_pt(struct guest_info * core) {
142 return vtlb_activate_shdw_pt(core);
146 static int vtlb_handle_pf(struct guest_info * core, addr_t fault_addr, pf_error_t error_code) {
148 switch (v3_get_vm_cpu_mode(core)) {
150 return handle_shadow_pagefault_32(core, fault_addr, error_code);
153 return handle_shadow_pagefault_32pae(core, fault_addr, error_code);
157 return handle_shadow_pagefault_64(core, fault_addr, error_code);
160 PrintError("Unhandled CPU Mode: %s\n", v3_cpu_mode_to_str(v3_get_vm_cpu_mode(core)));
166 static int vtlb_handle_invlpg(struct guest_info * core, addr_t vaddr) {
168 switch (v3_get_vm_cpu_mode(core)) {
170 return handle_shadow_invlpg_32(core, vaddr);
172 return handle_shadow_invlpg_32pae(core, vaddr);
176 return handle_shadow_invlpg_64(core, vaddr);
178 PrintError("Invalid CPU mode: %s\n", v3_cpu_mode_to_str(v3_get_vm_cpu_mode(core)));
183 static struct v3_shdw_pg_impl vtlb_impl = {
186 .deinit = vtlb_deinit,
187 .local_init = vtlb_local_init,
188 .handle_pagefault = vtlb_handle_pf,
189 .handle_invlpg = vtlb_handle_invlpg,
190 .activate_shdw_pt = vtlb_activate_shdw_pt,
191 .invalidate_shdw_pt = vtlb_invalidate_shdw_pt
198 register_shdw_pg_impl(&vtlb_impl);