1 /* Northwestern University */
2 /* (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> */
4 #include <palacios/vmm_mem.h>
5 #include <palacios/vmm.h>
6 #include <palacios/vmcb.h>
7 #include <palacios/vmm_decoder.h>
8 #include <palacios/vm_guest_mem.h>
9 #include <palacios/vmm_ctrl_regs.h>
13 /* Segmentation is a problem here...
15 * When we get a memory operand, presumably we use the default segment (which is?)
16 * unless an alternate segment was specfied in the prefix...
20 #ifndef DEBUG_CTRL_REGS
22 #define PrintDebug(fmt, args...)
26 // First Attempt = 494 lines
27 // current = 106 lines
28 int handle_cr0_write(struct guest_info * info) {
31 struct x86_instr dec_instr;
33 if (info->mem_mode == PHYSICAL_MEM) {
34 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
36 ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
39 /* The IFetch will already have faulted in the necessary bytes for the full instruction
41 // I think we should inject a GPF into the guest
42 PrintError("Could not read instruction (ret=%d)\n", ret);
47 if (v3_decode(info, (addr_t)instr, &dec_instr) == -1) {
48 PrintError("Could not decode instruction\n");
53 if (opcode_cmp(V3_OPCODE_LMSW, (const uchar_t *)(dec_instr.opcode)) == 0) {
54 struct cr0_real *real_cr0 = (struct cr0_real*)&(info->ctrl_regs.cr0);
55 struct cr0_real *new_cr0 = (struct cr0_real *)(dec_instr.src_operand.operand);
60 new_cr0_val = (*(char*)(new_cr0)) & 0x0f;
62 PrintDebug("OperandVal = %x\n", new_cr0_val);
64 PrintDebug("Old CR0=%x\n", *real_cr0);
65 *(uchar_t*)real_cr0 &= 0xf0;
66 *(uchar_t*)real_cr0 |= new_cr0_val;
67 PrintDebug("New CR0=%x\n", *real_cr0);
70 if (info->shdw_pg_mode == SHADOW_PAGING) {
71 struct cr0_real * shadow_cr0 = (struct cr0_real*)&(info->shdw_pg_state.guest_cr0);
73 PrintDebug(" Old Shadow CR0=%x\n", *shadow_cr0);
74 *(uchar_t*)shadow_cr0 &= 0xf0;
75 *(uchar_t*)shadow_cr0 |= new_cr0_val;
76 PrintDebug("New Shadow CR0=%x\n", *shadow_cr0);
78 } else if (opcode_cmp(V3_OPCODE_MOV2CR, (const uchar_t *)(dec_instr.opcode)) == 0) {
79 PrintDebug("MOV2CR0\n");
81 if (info->cpu_mode == LONG) {
85 struct cr0_32 *real_cr0 = (struct cr0_32*)&(info->ctrl_regs.cr0);
86 struct cr0_32 *new_cr0= (struct cr0_32 *)(dec_instr.src_operand.operand);
88 PrintDebug("OperandVal = %x, length=%d\n", *new_cr0, dec_instr.src_operand.size);
91 PrintDebug("Old CR0=%x\n", *real_cr0);
95 if (info->shdw_pg_mode == SHADOW_PAGING) {
96 struct cr0_32 * shadow_cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
98 PrintDebug("Old Shadow CR0=%x\n", *shadow_cr0);
102 *shadow_cr0 = *new_cr0;
105 if (get_mem_mode(info) == VIRTUAL_MEM) {
106 struct cr3_32 * shadow_cr3 = (struct cr3_32 *)&(info->shdw_pg_state.shadow_cr3);
108 info->ctrl_regs.cr3 = *(addr_t*)shadow_cr3;
110 info->ctrl_regs.cr3 = *(addr_t*)&(info->direct_map_pt);
114 PrintDebug("New Shadow CR0=%x\n",*shadow_cr0);
116 PrintDebug("New CR0=%x\n", *real_cr0);
119 } else if (opcode_cmp(V3_OPCODE_CLTS, (const uchar_t *)(dec_instr.opcode)) == 0) {
121 struct cr0_32 *real_cr0 = (struct cr0_32*)&(info->ctrl_regs.cr0);
125 if (info->shdw_pg_mode == SHADOW_PAGING) {
126 struct cr0_32 * shadow_cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
130 PrintError("Unhandled opcode in handle_cr0_write\n");
134 info->rip += dec_instr.instr_length;
140 // First attempt = 253 lines
141 // current = 51 lines
142 int handle_cr0_read(struct guest_info * info) {
145 struct x86_instr dec_instr;
147 if (info->mem_mode == PHYSICAL_MEM) {
148 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
150 ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
153 /* The IFetch will already have faulted in the necessary bytes for the full instruction
155 // I think we should inject a GPF into the guest
156 PrintError("Could not read instruction (ret=%d)\n", ret);
161 if (v3_decode(info, (addr_t)instr, &dec_instr) == -1) {
162 PrintError("Could not decode instruction\n");
166 if (opcode_cmp(V3_OPCODE_MOVCR2, (const uchar_t *)(dec_instr.opcode)) == 0) {
167 struct cr0_32 * virt_cr0 = (struct cr0_32 *)(dec_instr.dst_operand.operand);
168 struct cr0_32 * real_cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
170 PrintDebug("MOVCR2\n");
171 PrintDebug("CR0 at 0x%x\n", real_cr0);
173 if (info->shdw_pg_mode == SHADOW_PAGING) {
174 *virt_cr0 = *(struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
176 *virt_cr0 = *real_cr0;
179 PrintDebug("real CR0: %x\n", *(uint_t*)real_cr0);
180 PrintDebug("returned CR0: %x\n", *(uint_t*)virt_cr0);
181 } else if (opcode_cmp(V3_OPCODE_SMSW, (const uchar_t *)(dec_instr.opcode)) == 0) {
182 struct cr0_real *real_cr0= (struct cr0_real*)&(info->ctrl_regs.cr0);
183 struct cr0_real *virt_cr0 = (struct cr0_real *)(dec_instr.dst_operand.operand);
184 char cr0_val = *(char*)real_cr0 & 0x0f;
186 PrintDebug("SMSW\n");
188 PrintDebug("CR0 at 0x%x\n", real_cr0);
190 *(char *)virt_cr0 &= 0xf0;
191 *(char *)virt_cr0 |= cr0_val;
194 PrintError("Unhandled opcode in handle_cr0_read\n");
198 info->rip += dec_instr.instr_length;
205 // First Attempt = 256 lines
206 // current = 65 lines
207 int handle_cr3_write(struct guest_info * info) {
210 struct x86_instr dec_instr;
212 if (info->mem_mode == PHYSICAL_MEM) {
213 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
215 ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
218 /* The IFetch will already have faulted in the necessary bytes for the full instruction
220 // I think we should inject a GPF into the guest
221 PrintError("Could not read instruction (ret=%d)\n", ret);
226 if (v3_decode(info, (addr_t)instr, &dec_instr) == -1) {
227 PrintError("Could not decode instruction\n");
231 if (opcode_cmp(V3_OPCODE_MOV2CR, (const uchar_t *)(dec_instr.opcode)) == 0) {
233 PrintDebug("MOV2CR3\n");
235 PrintDebug("CR3 at 0x%x\n", &(info->ctrl_regs.cr3));
237 if (info->shdw_pg_mode == SHADOW_PAGING) {
238 struct cr3_32 * new_cr3 = (struct cr3_32 *)(dec_instr.src_operand.operand);
239 struct cr3_32 * guest_cr3 = (struct cr3_32 *)&(info->shdw_pg_state.guest_cr3);
240 struct cr3_32 * shadow_cr3 = (struct cr3_32 *)&(info->shdw_pg_state.shadow_cr3);
244 PrintDebug("Old Shadow CR3=%x; Old Guest CR3=%x\n",
245 *(uint_t*)shadow_cr3, *(uint_t*)guest_cr3);
248 cached = cache_page_tables32(info, CR3_TO_PDE32(*(addr_t *)new_cr3));
251 PrintError("CR3 Cache failed\n");
253 } else if (cached == 0) {
256 PrintDebug("New CR3 is different - flushing shadow page table\n");
258 delete_page_tables_pde32((pde32_t *)CR3_TO_PDE32(*(uint_t*)shadow_cr3));
260 shadow_pt = create_new_shadow_pt32();
262 shadow_cr3->pdt_base_addr = PD32_BASE_ADDR(shadow_pt);
264 PrintDebug("Reusing cached shadow Page table\n");
267 shadow_cr3->pwt = new_cr3->pwt;
268 shadow_cr3->pcd = new_cr3->pcd;
271 *guest_cr3 = *new_cr3;
273 PrintDebug("New Shadow CR3=%x; New Guest CR3=%x\n",
274 *(uint_t*)shadow_cr3, *(uint_t*)guest_cr3);
276 if (info->mem_mode == VIRTUAL_MEM) {
277 // If we aren't in paged mode then we have to preserve the identity mapped CR3
278 info->ctrl_regs.cr3 = *(addr_t*)shadow_cr3;
282 PrintError("Unhandled opcode in handle_cr3_write\n");
286 info->rip += dec_instr.instr_length;
293 // first attempt = 156 lines
294 // current = 36 lines
295 int handle_cr3_read(struct guest_info * info) {
298 struct x86_instr dec_instr;
300 if (info->mem_mode == PHYSICAL_MEM) {
301 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
303 ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
306 /* The IFetch will already have faulted in the necessary bytes for the full instruction
308 // I think we should inject a GPF into the guest
309 PrintError("Could not read instruction (ret=%d)\n", ret);
314 if (v3_decode(info, (addr_t)instr, &dec_instr) == -1) {
315 PrintError("Could not decode instruction\n");
319 if (opcode_cmp(V3_OPCODE_MOVCR2, (const uchar_t *)(dec_instr.opcode)) == 0) {
320 PrintDebug("MOVCR32\n");
321 struct cr3_32 * virt_cr3 = (struct cr3_32 *)(dec_instr.dst_operand.operand);
323 PrintDebug("CR3 at 0x%x\n", &(info->ctrl_regs.cr3));
325 if (info->shdw_pg_mode == SHADOW_PAGING) {
326 *virt_cr3 = *(struct cr3_32 *)&(info->shdw_pg_state.guest_cr3);
328 *virt_cr3 = *(struct cr3_32 *)&(info->ctrl_regs.cr3);
331 PrintError("Unhandled opcode in handle_cr3_read\n");
335 info->rip += dec_instr.instr_length;