1 #include <palacios/vmm_mem.h>
2 #include <palacios/vmm.h>
3 #include <palacios/vmcb.h>
4 #include <palacios/vmm_decoder.h>
5 #include <palacios/vm_guest_mem.h>
6 #include <palacios/vmm_ctrl_regs.h>
10 /* Segmentation is a problem here...
12 * When we get a memory operand, presumably we use the default segment (which is?)
13 * unless an alternate segment was specfied in the prefix...
17 #ifndef DEBUG_CTRL_REGS
19 #define PrintDebug(fmt, args...)
27 // First Attempt = 494 lines
28 // current = 106 lines
29 int handle_cr0_write(struct guest_info * info) {
32 struct x86_instr dec_instr;
34 if (info->mem_mode == PHYSICAL_MEM) {
35 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
37 ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
41 // I think we should inject a GPF into the guest
42 PrintError("Could not read instruction (ret=%d)\n", ret);
46 if (v3_decode(info, (addr_t)instr, &dec_instr) == -1) {
47 PrintError("Could not decode instruction\n");
52 if (opcode_cmp(V3_OPCODE_LMSW, (const uchar_t *)(dec_instr.opcode)) == 0) {
53 struct cr0_real *real_cr0 = (struct cr0_real*)&(info->ctrl_regs.cr0);
54 struct cr0_real *new_cr0 = (struct cr0_real *)(dec_instr.first_operand.operand);
59 new_cr0_val = (*(char*)(new_cr0)) & 0x0f;
61 PrintDebug("OperandVal = %x\n", new_cr0_val);
63 PrintDebug("Old CR0=%x\n", *real_cr0);
64 *(uchar_t*)real_cr0 &= 0xf0;
65 *(uchar_t*)real_cr0 |= new_cr0_val;
66 PrintDebug("New CR0=%x\n", *real_cr0);
69 if (info->shdw_pg_mode == SHADOW_PAGING) {
70 struct cr0_real * shadow_cr0 = (struct cr0_real*)&(info->shdw_pg_state.guest_cr0);
72 PrintDebug(" Old Shadow CR0=%x\n", *shadow_cr0);
73 *(uchar_t*)shadow_cr0 &= 0xf0;
74 *(uchar_t*)shadow_cr0 |= new_cr0_val;
75 PrintDebug("New Shadow CR0=%x\n", *shadow_cr0);
77 } else if (opcode_cmp(V3_OPCODE_MOV2CR, (const uchar_t *)(dec_instr.opcode)) == 0) {
78 PrintDebug("MOV2CR0\n");
80 if (info->cpu_mode == LONG) {
84 struct cr0_32 *real_cr0 = (struct cr0_32*)&(info->ctrl_regs.cr0);
85 struct cr0_32 *new_cr0= (struct cr0_32 *)(dec_instr.second_operand.operand);
87 PrintDebug("OperandVal = %x, length=%d\n", *new_cr0, dec_instr.first_operand.size);
90 PrintDebug("Old CR0=%x\n", *real_cr0);
94 if (info->shdw_pg_mode == SHADOW_PAGING) {
95 struct cr0_32 * shadow_cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
97 PrintDebug("Old Shadow CR0=%x\n", *shadow_cr0);
101 *shadow_cr0 = *new_cr0;
104 if (get_mem_mode(info) == VIRTUAL_MEM) {
105 struct cr3_32 * shadow_cr3 = (struct cr3_32 *)&(info->shdw_pg_state.shadow_cr3);
107 info->ctrl_regs.cr3 = *(addr_t*)shadow_cr3;
109 info->ctrl_regs.cr3 = *(addr_t*)&(info->direct_map_pt);
113 PrintDebug("New Shadow CR0=%x\n",*shadow_cr0);
115 PrintDebug("New CR0=%x\n", *real_cr0);
118 } else if (opcode_cmp(V3_OPCODE_CLTS, (const uchar_t *)(dec_instr.opcode)) == 0) {
120 struct cr0_32 *real_cr0 = (struct cr0_32*)&(info->ctrl_regs.cr0);
124 if (info->shdw_pg_mode == SHADOW_PAGING) {
125 struct cr0_32 * shadow_cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
129 PrintError("Unhandled opcode in handle_cr0_write\n");
133 info->rip += dec_instr.instr_length;
139 // First attempt = 253 lines
140 // current = 51 lines
141 int handle_cr0_read(struct guest_info * info) {
144 struct x86_instr dec_instr;
146 if (info->mem_mode == PHYSICAL_MEM) {
147 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
149 ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
153 // I think we should inject a GPF into the guest
154 PrintError("Could not read instruction (ret=%d)\n", ret);
158 if (v3_decode(info, (addr_t)instr, &dec_instr) == -1) {
159 PrintError("Could not decode instruction\n");
163 if (opcode_cmp(V3_OPCODE_MOVCR2, (const uchar_t *)(dec_instr.opcode)) == 0) {
164 struct cr0_32 * virt_cr0 = (struct cr0_32 *)(dec_instr.first_operand.operand);
165 struct cr0_32 * real_cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
167 PrintDebug("MOVCR2\n");
168 PrintDebug("CR0 at 0x%x\n", real_cr0);
170 if (info->shdw_pg_mode == SHADOW_PAGING) {
171 *virt_cr0 = *(struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
173 *virt_cr0 = *real_cr0;
176 PrintDebug("real CR0: %x\n", *(uint_t*)real_cr0);
177 PrintDebug("returned CR0: %x\n", *(uint_t*)virt_cr0);
178 } else if (opcode_cmp(V3_OPCODE_SMSW, (const uchar_t *)(dec_instr.opcode)) == 0) {
179 struct cr0_real *real_cr0= (struct cr0_real*)&(info->ctrl_regs.cr0);
180 struct cr0_real *virt_cr0 = (struct cr0_real *)(dec_instr.first_operand.operand);
181 char cr0_val = *(char*)real_cr0 & 0x0f;
183 PrintDebug("SMSW\n");
185 PrintDebug("CR0 at 0x%x\n", real_cr0);
187 *(char *)virt_cr0 &= 0xf0;
188 *(char *)virt_cr0 |= cr0_val;
191 PrintError("Unhandled opcode in handle_cr0_read\n");
195 info->rip += dec_instr.instr_length;
202 // First Attemp = 256 lines
203 // current = 65 lines
204 int handle_cr3_write(struct guest_info * info) {
207 struct x86_instr dec_instr;
209 if (info->mem_mode == PHYSICAL_MEM) {
210 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
212 ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
216 // I think we should inject a GPF into the guest
217 PrintError("Could not read instruction (ret=%d)\n", ret);
221 if (v3_decode(info, (addr_t)instr, &dec_instr) == -1) {
222 PrintError("Could not decode instruction\n");
226 if (opcode_cmp(V3_OPCODE_MOV2CR, (const uchar_t *)(dec_instr.opcode)) == 0) {
228 PrintDebug("MOV2CR3\n");
230 PrintDebug("CR3 at 0x%x\n", &(info->ctrl_regs.cr3));
232 if (info->shdw_pg_mode == SHADOW_PAGING) {
233 struct cr3_32 * new_cr3 = (struct cr3_32 *)(dec_instr.second_operand.operand);
234 struct cr3_32 * guest_cr3 = (struct cr3_32 *)&(info->shdw_pg_state.guest_cr3);
235 struct cr3_32 * shadow_cr3 = (struct cr3_32 *)&(info->shdw_pg_state.shadow_cr3);
237 PrintDebug("Old Shadow CR3=%x; Old Guest CR3=%x\n",
238 *(uint_t*)shadow_cr3, *(uint_t*)guest_cr3);
240 if (!CR3_32_SAME_BASE(new_cr3, guest_cr3)) {
244 PrintDebug("New CR3 is different - flushing shadow page table\n");
246 delete_page_tables_pde32((pde32_t *)CR3_TO_PDE32(*(uint_t*)shadow_cr3));
248 shadow_pt = create_new_shadow_pt32(info);
250 shadow_cr3->pdt_base_addr = PD32_BASE_ADDR(shadow_pt);
253 shadow_cr3->pwt = new_cr3->pwt;
254 shadow_cr3->pcd = new_cr3->pcd;
257 *guest_cr3 = *new_cr3;
259 PrintDebug("New Shadow CR3=%x; New Guest CR3=%x\n",
260 *(uint_t*)shadow_cr3, *(uint_t*)guest_cr3);
262 if (info->mem_mode == VIRTUAL_MEM) {
263 // If we aren't in paged mode then we have to preserve the identity mapped CR3
264 info->ctrl_regs.cr3 = *(addr_t*)shadow_cr3;
268 PrintError("Unhandled opcode in handle_cr3_write\n");
272 info->rip += dec_instr.instr_length;
279 // first attempt = 156 lines
280 // current = 36 lines
281 int handle_cr3_read(struct guest_info * info) {
284 struct x86_instr dec_instr;
286 if (info->mem_mode == PHYSICAL_MEM) {
287 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
289 ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
293 // I think we should inject a GPF into the guest
294 PrintError("Could not read instruction (ret=%d)\n", ret);
298 if (v3_decode(info, (addr_t)instr, &dec_instr) == -1) {
299 PrintError("Could not decode instruction\n");
303 if (opcode_cmp(V3_OPCODE_MOVCR2, (const uchar_t *)(dec_instr.opcode)) == 0) {
304 PrintDebug("MOVCR32\n");
305 struct cr3_32 * virt_cr3 = (struct cr3_32 *)(dec_instr.first_operand.operand);
307 PrintDebug("CR3 at 0x%x\n", &(info->ctrl_regs.cr3));
309 if (info->shdw_pg_mode == SHADOW_PAGING) {
310 *virt_cr3 = *(struct cr3_32 *)&(info->shdw_pg_state.guest_cr3);
312 *virt_cr3 = *(struct cr3_32 *)&(info->ctrl_regs.cr3);
315 PrintError("Unhandled opcode in handle_cr3_read\n");
320 info->rip += dec_instr.instr_length;