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 extern void SerialMemDump(unsigned char *start, int n);
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 int handle_cr0_write(struct guest_info * info) {
24 switch (info->cpu_mode) {
30 PrintDebug("Real Mode write to CR0 at linear guest pa 0x%x\n",get_addr_linear(info,info->rip,&(info->segments.cs)));
32 PrintV3Segments(&(info->segments));
34 // The real rip address is actually a combination of the rip + CS base
35 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
37 // I think we should inject a GPF into the guest
38 PrintDebug("Could not read instruction (ret=%d)\n", ret);
42 while (is_prefix_byte(instr[index])) {
43 switch(instr[index]) {
44 case PREFIX_CS_OVERRIDE:
45 case PREFIX_SS_OVERRIDE:
46 case PREFIX_DS_OVERRIDE:
47 case PREFIX_ES_OVERRIDE:
48 case PREFIX_FS_OVERRIDE:
49 case PREFIX_GS_OVERRIDE:
50 PrintDebug("Segment Override!!\n");
59 if ((instr[index] == cr_access_byte) &&
60 (instr[index + 1] == lmsw_byte) &&
61 (MODRM_REG(instr[index + 2]) == lmsw_reg_byte)) {
64 addr_t second_operand;
65 struct cr0_real *real_cr0;
66 struct cr0_real *new_cr0;
67 operand_type_t addr_type;
73 real_cr0 = (struct cr0_real*)&(info->ctrl_regs.cr0);
75 addr_type = decode_operands16(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG16);
78 if (addr_type == REG_OPERAND) {
79 new_cr0 = (struct cr0_real *)first_operand;
80 } else if (addr_type == MEM_OPERAND) {
83 if (guest_pa_to_host_va(info, first_operand + (info->segments.ds.base << 4), &host_addr) == -1) {
88 new_cr0 = (struct cr0_real *)host_addr;
91 // error... don't know what to do
95 if ((new_cr0->pe == 1) && (real_cr0->pe == 0)) {
96 info->cpu_mode = PROTECTED;
97 } else if ((new_cr0->pe == 0) && (real_cr0->pe == 1)) {
98 info->cpu_mode = REAL;
101 new_cr0_val = (*(char*)(new_cr0)) & 0x0f;
104 if (info->shdw_pg_mode == SHADOW_PAGING) {
105 struct cr0_real * shadow_cr0 = (struct cr0_real*)&(info->shdw_pg_state.guest_cr0);
107 PrintDebug("Old CR0=%x, Old Shadow CR0=%x\n", *real_cr0, *shadow_cr0);
108 /* struct cr0_real is only 4 bits wide,
109 * so we can overwrite the real_cr0 without worrying about the shadow fields
111 *(char*)real_cr0 &= 0xf0;
112 *(char*)real_cr0 |= new_cr0_val;
114 *(char*)shadow_cr0 &= 0xf0;
115 *(char*)shadow_cr0 |= new_cr0_val;
118 PrintDebug("New CR0=%x, New Shadow CR0=%x\n", *real_cr0, *shadow_cr0);
120 PrintDebug("Old CR0=%x\n", *real_cr0);
121 // for now we just pass through....
122 *(char*)real_cr0 &= 0xf0;
123 *(char*)real_cr0 |= new_cr0_val;
125 PrintDebug("New CR0=%x\n", *real_cr0);
131 } else if ((instr[index] == cr_access_byte) &&
132 (instr[index + 1] == clts_byte)) {
134 PrintDebug("CLTS unhandled in CR0 write\n");
137 } else if ((instr[index] == cr_access_byte) &&
138 (instr[index + 1] = mov_to_cr_byte)) {
139 addr_t first_operand;
140 addr_t second_operand;
141 struct cr0_32 *real_cr0;
142 struct cr0_32 *new_cr0;
143 operand_type_t addr_type;
148 real_cr0 = (struct cr0_32*)&(info->ctrl_regs.cr0);
150 addr_type = decode_operands16(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
152 if (addr_type != REG_OPERAND) {
153 PrintDebug("Moving to CR0 from non-register operand in CR0 write\n");
154 /* Mov to CR0 Can only be a 32 bit register */
159 new_cr0 = (struct cr0_32 *)first_operand;
161 if (new_cr0->pe == 1) {
162 PrintDebug("Entering Protected Mode\n");
163 info->cpu_mode = PROTECTED;
166 if (new_cr0->pe == 0) {
167 PrintDebug("Entering Real Mode\n");
168 info->cpu_mode = REAL;
173 if ((new_cr0->pg == 1) && (new_cr0->pe == 1)) {
174 PrintDebug("Paging is already turned on in switch to protected mode in CR0 write\n");
176 // Actually This appears to be Ok.
177 // Why shouldn't windows be allowed to switch to real mode whenever it wants to modify its page tables?
179 info->mem_mode = VIRTUAL_MEM;
183 } else if ((new_cr0->pg == 1) && (new_cr0->pe == 0)) {
184 PrintDebug("Will the madness Never End??\n");
189 if (info->shdw_pg_mode == SHADOW_PAGING) {
190 struct cr0_32 * shadow_cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
192 PrintDebug("Old CR0=%x, Old Shadow CR0=%x\n", *real_cr0, *shadow_cr0);
193 *real_cr0 = *new_cr0;
197 *shadow_cr0 = *new_cr0;
200 // Setup the page tables...???
201 if (info->mem_mode == VIRTUAL_MEM) {
202 // If we aren't in paged mode then we have to preserve the identity mapped CR3
203 info->ctrl_regs.cr3 = *(addr_t*)&(info->shdw_pg_state.shadow_cr3);
206 PrintDebug("New CR0=%x, New Shadow CR0=%x\n", *real_cr0, *shadow_cr0);
208 PrintDebug("Old CR0=%x\n", *real_cr0);
209 *real_cr0 = *new_cr0;
210 PrintDebug("New CR0=%x\n", *real_cr0);
216 PrintDebug("Unsupported Instruction\n");
217 // unsupported instruction, UD the guest
230 PrintDebug("Protected %s Mode write to CR0 at guest %s linear rip 0x%x\n",
231 info->mem_mode == VIRTUAL_MEM ? "Paged" : "",
232 info->mem_mode == VIRTUAL_MEM ? "virtual" : "",
233 get_addr_linear(info, info->rip, &(info->segments.cs)));
235 // OK, now we will read the instruction
236 // The only difference between PROTECTED and PROTECTED_PG is whether we read
237 // from guest_pa or guest_va
238 if (info->mem_mode == PHYSICAL_MEM) {
239 // The real rip address is actually a combination of the rip + CS base
240 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
242 ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
247 // I think we should inject a GPF into the guest
248 PrintDebug("Could not read instruction (ret=%d)\n", ret);
253 while (is_prefix_byte(instr[index])) {
254 switch(instr[index]) {
255 case PREFIX_CS_OVERRIDE:
256 case PREFIX_SS_OVERRIDE:
257 case PREFIX_DS_OVERRIDE:
258 case PREFIX_ES_OVERRIDE:
259 case PREFIX_FS_OVERRIDE:
260 case PREFIX_GS_OVERRIDE:
261 PrintDebug("Segment Override!!\n");
271 while (is_prefix_byte(instr[index])) {
276 struct cr0_32 * shadow_cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
278 struct cr0_32 * real_cr0 = (struct cr0_32*)&(info->ctrl_regs.cr0);
280 if ((instr[index] == cr_access_byte) &&
281 (instr[index + 1] == mov_to_cr_byte)) {
285 addr_t first_operand;
286 addr_t second_operand;
287 struct cr0_32 *new_cr0;
288 operand_type_t addr_type;
292 PrintDebug("MovToCR0 instr:\n");
293 PrintTraceMemDump(instr, 15);
294 PrintDebug("EAX=%x\n", *(uint_t*)&(info->vm_regs.rax));
296 addr_type = decode_operands32(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
298 if (addr_type != REG_OPERAND) {
299 PrintDebug("Non-register operand in write to CR0\n");
303 new_cr0 = (struct cr0_32 *)first_operand;
305 PrintDebug("first operand=%x\n", *(uint_t *)first_operand);
307 if (info->shdw_pg_mode == SHADOW_PAGING) {
308 struct cr0_32 * shadow_cr0 = (struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
310 if (new_cr0->pg == 1){
311 // This should be new_cr0->pg && !(old_cr->pg), right?
312 // and then a case for turning paging off?
314 struct cr3_32 * shadow_cr3 = (struct cr3_32 *)&(info->shdw_pg_state.shadow_cr3);
316 info->mem_mode = VIRTUAL_MEM;
318 *shadow_cr0 = *new_cr0;
319 *real_cr0 = *new_cr0;
324 // Activate Shadow Paging
326 PrintDebug("Turning on paging in the guest\n");
328 info->ctrl_regs.cr3 = *(addr_t*)shadow_cr3;
331 } else if (new_cr0->pe == 0) {
332 info->cpu_mode = REAL;
333 info->mem_mode = PHYSICAL_MEM;
334 PrintDebug("Entering Real Mode\n");
336 PrintV3CtrlRegs(&(info->ctrl_regs));
337 // reinstate the identity mapped paged tables
338 // But keep the shadow tables around to handle TLB issues.... UGH...
339 //info->shdw_pg_state.shadow_cr3 &= 0x00000fff;
340 //info->shdw_pg_state.shadow_cr3 |= ((addr_t)create_passthrough_pde32_pts(info) & ~0xfff);
342 //info->ctrl_regs.cr3 = info->shdw_pg_state.shadow_cr3;
343 info->ctrl_regs.cr3 = *(addr_t*)&(info->direct_map_pt);
345 *shadow_cr0 = *new_cr0;
346 *real_cr0 = *new_cr0;
351 PrintV3CtrlRegs(&(info->ctrl_regs));
357 *real_cr0 = *new_cr0;
361 } else if ((instr[index] == cr_access_byte) &&
362 (instr[index + 1] == lmsw_byte) &&
363 (MODRM_REG(instr[index + 2]) == lmsw_reg_byte)) {
364 addr_t first_operand;
365 addr_t second_operand;
366 struct cr0_real *real_cr0;
367 struct cr0_real *new_cr0;
368 operand_type_t addr_type;
369 char new_cr0_val = 0;
373 real_cr0 = (struct cr0_real*)&(info->ctrl_regs.cr0);
375 addr_type = decode_operands16(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG16);
377 if (addr_type == REG_OPERAND) {
378 new_cr0 = (struct cr0_real *)first_operand;
379 } else if (addr_type == MEM_OPERAND) {
381 // check segment descriptors....
383 /* TODO, TODO, TODO: Lookup segment overrides */
384 struct v3_segment *lmsw_segment = &(info->segments.ds);
386 if (info->mem_mode == PHYSICAL_MEM) {
387 if (guest_pa_to_host_va(info, get_addr_linear(info, first_operand, lmsw_segment), &host_addr) == -1) {
391 if (guest_va_to_host_va(info, get_addr_linear(info, first_operand, lmsw_segment), &host_addr) == -1) {
396 new_cr0 = (struct cr0_real *)host_addr;
402 if (new_cr0->pe == 0) {
403 // According to the intel manual this it is illegal to use
404 // lmsw to turn _off_ Protected mode
405 PrintDebug("Cannot switch to real mode with LMSW, unclear what to do\n");
409 new_cr0_val = (*(char *)(new_cr0)) & 0x0f;
412 if (info->shdw_pg_mode == SHADOW_PAGING) {
413 struct cr0_real * shadow_cr0 = (struct cr0_real*)&(info->shdw_pg_state.guest_cr0);
415 PrintDebug("Old CR0=%x, Old Shadow CR0=%x\n", *real_cr0, *shadow_cr0);
416 /* struct cr0_real is only 4 bits wide,
417 * so we can overwrite the real_cr0 without worrying about the shadow fields
419 *(char*)real_cr0 &= 0xf0;
420 *(char*)real_cr0 |= new_cr0_val;
422 *(char*)shadow_cr0 &= 0xf0;
423 *(char*)shadow_cr0 |= new_cr0_val;
426 PrintDebug("New CR0=%x, New Shadow CR0=%x\n", *real_cr0, *shadow_cr0);
428 PrintDebug("Old CR0=%x\n", *real_cr0);
429 // for now we just pass through....
430 *(char*)real_cr0 &= 0xf0;
431 *(char*)real_cr0 |= new_cr0_val;
433 PrintDebug("New CR0=%x\n", *real_cr0);
439 } else if ((instr[index] == 0x0f) &&
440 (instr[index + 1] == 0x06)) {
442 PrintDebug("CLTS instruction - clearing TS flag of real and shadow CR0\n");
451 PrintDebug("Unkown instruction: \n");
452 SerialMemDump(instr,15);
459 PrintDebug("Protected PAE Mode write to CR0 is UNIMPLEMENTED\n");
463 PrintDebug("Protected Long Mode write to CR0 is UNIMPLEMENTED\n");
468 PrintDebug("Unknown Mode write to CR0 (info->cpu_mode=0x%x\n)",info->cpu_mode);
479 int handle_cr0_read(struct guest_info * info) {
482 switch (info->cpu_mode) {
490 PrintDebug("Real Mode read from CR0 at linear guest pa 0x%x\n",get_addr_linear(info,info->rip,&(info->segments.cs)));
491 PrintV3Segments(&(info->segments));
493 // The real rip address is actually a combination of the rip + CS base
494 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
496 // I think we should inject a GPF into the guest
497 PrintDebug("Could not read Real Mode instruction (ret=%d)\n", ret);
502 while (is_prefix_byte(instr[index])) {
503 switch(instr[index]) {
504 case PREFIX_CS_OVERRIDE:
505 case PREFIX_SS_OVERRIDE:
506 case PREFIX_DS_OVERRIDE:
507 case PREFIX_ES_OVERRIDE:
508 case PREFIX_FS_OVERRIDE:
509 case PREFIX_GS_OVERRIDE:
510 PrintDebug("Segment Override!!\n");
520 while (is_prefix_byte(instr[index])) {
525 if ((instr[index] == cr_access_byte) &&
526 (instr[index + 1] == smsw_byte) &&
527 (MODRM_REG(instr[index + 2]) == smsw_reg_byte)) {
529 // SMSW (store machine status word)
531 addr_t first_operand;
532 addr_t second_operand;
533 struct cr0_real *cr0;
534 operand_type_t addr_type;
539 cr0 = (struct cr0_real*)&(info->ctrl_regs.cr0);
542 addr_type = decode_operands16(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG16);
544 if (addr_type == MEM_OPERAND) {
547 if (guest_pa_to_host_va(info, first_operand + (info->segments.ds.base << 4), &host_addr) == -1) {
549 PrintDebug("Could not convert guest physical address to host virtual address\n");
553 first_operand = host_addr;
559 cr0_val = *(char*)cr0 & 0x0f;
561 *(char *)first_operand &= 0xf0;
562 *(char *)first_operand |= cr0_val;
564 PrintDebug("index = %d, rip = %x\n", index, (ulong_t)(info->rip));
566 PrintDebug("new_rip = %x\n", (ulong_t)(info->rip));
569 } else if ((instr[index] == cr_access_byte) &&
570 (instr[index+1] == mov_from_cr_byte)) {
572 * This can only take a 32 bit register argument in anything less than 64 bit mode.
574 addr_t first_operand;
575 addr_t second_operand;
576 operand_type_t addr_type;
578 struct cr0_32 * real_cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
582 addr_type = decode_operands16(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
584 struct cr0_32 * virt_cr0 = (struct cr0_32 *)first_operand;
586 if (addr_type != REG_OPERAND) {
587 // invalid opcode to guest
588 PrintDebug("Invalid operand type in mov from CR0\n");
592 if (info->shdw_pg_mode == SHADOW_PAGING) {
593 *virt_cr0 = *(struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
595 *virt_cr0 = *real_cr0;
601 PrintDebug("Unknown read instr from CR0\n");
615 PrintDebug("Protected %s Mode read from CR0 at guest %s linear rip 0x%x\n",
616 info->mem_mode == VIRTUAL_MEM ? "Paged" : "",
617 info->mem_mode == VIRTUAL_MEM ? "virtual" : "",
618 get_addr_linear(info, info->rip, &(info->segments.cs)));
620 // We need to read the instruction, which is at CS:IP, but that
621 // linear address is guest physical without PG and guest virtual with PG
622 if (info->cpu_mode == PHYSICAL_MEM) {
623 // The real rip address is actually a combination of the rip + CS base
624 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
626 // The real rip address is actually a combination of the rip + CS base
627 ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
631 PrintDebug("Instr (15 bytes) at %x:\n", instr);
632 PrintTraceMemDump((char*)instr, 15);
635 // I think we should inject a GPF into the guest
636 PrintDebug("Could not read Protected %s mode instruction (ret=%d)\n",
637 info->cpu_mode == VIRTUAL_MEM ? "Paged" : "", ret);
642 while (is_prefix_byte(instr[index])) {
643 switch(instr[index]) {
644 case PREFIX_CS_OVERRIDE:
645 case PREFIX_SS_OVERRIDE:
646 case PREFIX_DS_OVERRIDE:
647 case PREFIX_ES_OVERRIDE:
648 case PREFIX_FS_OVERRIDE:
649 case PREFIX_GS_OVERRIDE:
650 PrintDebug("Segment Override!!\n");
661 while (is_prefix_byte(instr[index])) {
666 if ((instr[index] == cr_access_byte) &&
667 (instr[index+1] == mov_from_cr_byte)) {
669 // MOV from CR0 to register
671 addr_t first_operand;
672 addr_t second_operand;
673 operand_type_t addr_type;
674 struct cr0_32 * virt_cr0;
675 struct cr0_32 * real_cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
679 addr_type = decode_operands32(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
681 if (addr_type != REG_OPERAND) {
682 PrintDebug("Invalid operand type in mov from CR0\n");
686 virt_cr0 = (struct cr0_32 *)first_operand;
688 if (info->shdw_pg_mode == SHADOW_PAGING) {
689 *virt_cr0 = *(struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
691 if (info->mem_mode == PHYSICAL_MEM) {
692 virt_cr0->pg = 0; // clear the pg bit because guest doesn't think it's on
695 PrintDebug("real CR0: %x\n", *(uint_t*)real_cr0);
696 PrintDebug("returned CR0: %x\n", *(uint_t*)virt_cr0);
700 *virt_cr0 = *real_cr0;
706 PrintDebug("Unknown read instruction from CR0\n");
713 PrintDebug("Protected PAE Mode read to CR0 is UNIMPLEMENTED\n");
717 PrintDebug("Protected Long Mode read to CR0 is UNIMPLEMENTED\n");
723 PrintDebug("Unknown Mode read from CR0 (info->cpu_mode=0x%x)\n",info->cpu_mode);
736 int handle_cr3_write(struct guest_info * info) {
737 if (info->cpu_mode == REAL) {
738 // WHAT THE HELL DOES THIS EVEN MEAN?????
744 PrintDebug("Real Mode Write to CR3??? What the fuck???\n");
745 // We need to read the instruction, which is at CS:IP, but that
746 // linear address is guest physical without PG and guest virtual with PG
748 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
751 PrintDebug("Could not read instruction (ret=%d)\n", ret);
755 while (is_prefix_byte(instr[index])) {
756 switch(instr[index]) {
757 case PREFIX_CS_OVERRIDE:
758 case PREFIX_SS_OVERRIDE:
759 case PREFIX_DS_OVERRIDE:
760 case PREFIX_ES_OVERRIDE:
761 case PREFIX_FS_OVERRIDE:
762 case PREFIX_GS_OVERRIDE:
763 PrintDebug("Segment Override!!\n");
773 if ((instr[index] == cr_access_byte) &&
774 (instr[index + 1] == mov_to_cr_byte)) {
776 addr_t first_operand;
777 addr_t second_operand;
778 struct cr3_32 * new_cr3;
779 // struct cr3_32 * real_cr3;
780 operand_type_t addr_type;
784 addr_type = decode_operands16(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
786 if (addr_type != REG_OPERAND) {
787 /* Mov to CR3 can only be a 32 bit register */
791 new_cr3 = (struct cr3_32 *)first_operand;
793 if (info->shdw_pg_mode == SHADOW_PAGING) {
795 struct cr3_32 * shadow_cr3 = (struct cr3_32 *)&(info->shdw_pg_state.shadow_cr3);
796 struct cr3_32 * guest_cr3 = (struct cr3_32 *)&(info->shdw_pg_state.guest_cr3);
800 if (CR3_TO_PDE32(*(uint_t*)shadow_cr3) != 0) {
801 PrintDebug("Shadow Page Table\n");
802 PrintDebugPageTables((pde32_t *)CR3_TO_PDE32(*(uint_t*)shadow_cr3));
806 /* Delete the current Page Tables */
807 delete_page_tables_pde32((pde32_t *)CR3_TO_PDE32(*(uint_t*)shadow_cr3));
809 PrintDebug("Old Shadow CR3=%x; Old Guest CR3=%x\n",
810 *(uint_t*)shadow_cr3, *(uint_t*)guest_cr3);
813 *guest_cr3 = *new_cr3;
817 // Something like this
818 shadow_pt = create_new_shadow_pt32(info);
819 //shadow_pt = setup_shadow_pt32(info, CR3_TO_PDE32(*(addr_t *)new_cr3));
821 /* Copy Various flags */
822 *shadow_cr3 = *new_cr3;
827 guest_pa_to_host_va(info, ((*(uint_t*)guest_cr3) & 0xfffff000), &tmp_addr);
828 PrintDebug("Guest PD\n");
829 PrintPD32((pde32_t *)tmp_addr);
835 shadow_cr3->pdt_base_addr = PD32_BASE_ADDR(shadow_pt);
837 PrintDebug("New Shadow CR3=%x; New Guest CR3=%x\n",
838 *(uint_t*)shadow_cr3, *(uint_t*)guest_cr3);
847 PrintDebug("Unknown Instruction\n");
848 SerialMemDump(instr,15);
854 } else if (info->cpu_mode == PROTECTED) {
859 PrintDebug("Protected %s mode write to CR3 at %s 0x%x\n",
860 info->cpu_mode==PROTECTED ? "" : "Paged",
861 info->cpu_mode==PROTECTED ? "guest physical" : "guest virtual",
862 get_addr_linear(info,info->rip,&(info->segments.cs)));
864 // We need to read the instruction, which is at CS:IP, but that
865 // linear address is guest physical without PG and guest virtual with PG
866 if (info->mem_mode == PHYSICAL_MEM) {
867 // The real rip address is actually a combination of the rip + CS base
868 PrintDebug("Writing Guest CR3 Write (Physical Address)\n");
869 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
871 PrintDebug("Writing Guest CR3 Write (Virtual Address)\n");
872 // The real rip address is actually a combination of the rip + CS base
873 ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
877 PrintDebug("Could not read instruction (ret=%d)\n", ret);
881 while (is_prefix_byte(instr[index])) {
882 switch(instr[index]) {
883 case PREFIX_CS_OVERRIDE:
884 case PREFIX_SS_OVERRIDE:
885 case PREFIX_DS_OVERRIDE:
886 case PREFIX_ES_OVERRIDE:
887 case PREFIX_FS_OVERRIDE:
888 case PREFIX_GS_OVERRIDE:
889 PrintDebug("Segment Override!!\n");
899 while (is_prefix_byte(instr[index])) {
904 if ((instr[index] == cr_access_byte) &&
905 (instr[index + 1] == mov_to_cr_byte)) {
907 addr_t first_operand;
908 addr_t second_operand;
909 struct cr3_32 * new_cr3;
910 // struct cr3_32 * real_cr3;
911 operand_type_t addr_type;
915 addr_type = decode_operands32(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
917 if (addr_type != REG_OPERAND) {
918 /* Mov to CR3 can only be a 32 bit register */
922 new_cr3 = (struct cr3_32 *)first_operand;
924 if (info->shdw_pg_mode == SHADOW_PAGING) {
926 struct cr3_32 * shadow_cr3 = (struct cr3_32 *)&(info->shdw_pg_state.shadow_cr3);
927 struct cr3_32 * guest_cr3 = (struct cr3_32 *)&(info->shdw_pg_state.guest_cr3);
931 if (CR3_TO_PDE32(*(uint_t*)shadow_cr3) != 0) {
932 PrintDebug("Shadow Page Table\n");
933 PrintDebugPageTables((pde32_t *)CR3_TO_PDE32(*(uint_t*)shadow_cr3));
937 /* Delete the current Page Tables */
938 delete_page_tables_pde32((pde32_t *)CR3_TO_PDE32(*(uint_t*)shadow_cr3));
940 PrintDebug("Old Shadow CR3=%x; Old Guest CR3=%x\n",
941 *(uint_t*)shadow_cr3, *(uint_t*)guest_cr3);
944 *guest_cr3 = *new_cr3;
948 // Something like this
949 shadow_pt = create_new_shadow_pt32(info);
950 //shadow_pt = setup_shadow_pt32(info, CR3_TO_PDE32(*(addr_t *)new_cr3));
952 /* Copy Various flags */
953 *shadow_cr3 = *new_cr3;
958 guest_pa_to_host_va(info, ((*(uint_t*)guest_cr3) & 0xfffff000), &tmp_addr);
959 PrintDebug("Guest PD\n");
960 PrintPD32((pde32_t *)tmp_addr);
966 shadow_cr3->pdt_base_addr = PD32_BASE_ADDR(shadow_pt);
968 PrintDebug("New Shadow CR3=%x; New Guest CR3=%x\n",
969 *(uint_t*)shadow_cr3, *(uint_t*)guest_cr3);
973 if (info->mem_mode == VIRTUAL_MEM) {
974 // If we aren't in paged mode then we have to preserve the identity mapped CR3
975 info->ctrl_regs.cr3 = *(addr_t*)shadow_cr3;
982 PrintDebug("Unknown Instruction\n");
983 SerialMemDump(instr,15);
987 PrintDebug("Invalid operating Mode (0x%x)\n", info->cpu_mode);
997 int handle_cr3_read(struct guest_info * info) {
999 if (info->cpu_mode == REAL) {
1003 addr_t linear_addr = 0;
1005 linear_addr = get_addr_linear(info, info->rip, &(info->segments.cs));
1008 PrintDebug("RIP Linear: %x\n", linear_addr);
1009 PrintV3Segments(&(info->segments));
1011 ret = read_guest_pa_memory(info, linear_addr, 15, instr);
1014 PrintDebug("Could not read instruction (ret=%d)\n", ret);
1018 while (is_prefix_byte(instr[index])) {
1019 switch(instr[index]) {
1020 case PREFIX_CS_OVERRIDE:
1021 case PREFIX_SS_OVERRIDE:
1022 case PREFIX_DS_OVERRIDE:
1023 case PREFIX_ES_OVERRIDE:
1024 case PREFIX_FS_OVERRIDE:
1025 case PREFIX_GS_OVERRIDE:
1026 PrintDebug("Segment Override!!\n");
1036 if ((instr[index] == cr_access_byte) &&
1037 (instr[index + 1] == mov_from_cr_byte)) {
1038 addr_t first_operand;
1039 addr_t second_operand;
1040 struct cr3_32 * virt_cr3;
1041 struct cr3_32 * real_cr3 = (struct cr3_32 *)&(info->ctrl_regs.cr3);
1042 operand_type_t addr_type;
1046 addr_type = decode_operands16(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
1048 if (addr_type != REG_OPERAND) {
1049 /* Mov to CR3 can only be a 32 bit register */
1053 virt_cr3 = (struct cr3_32 *)first_operand;
1055 if (info->shdw_pg_mode == SHADOW_PAGING) {
1056 *virt_cr3 = *(struct cr3_32 *)&(info->shdw_pg_state.guest_cr3);
1058 *virt_cr3 = *real_cr3;
1063 PrintDebug("Unknown Instruction\n");
1064 SerialMemDump(instr,15);
1070 } else if (info->cpu_mode == PROTECTED) {
1077 // We need to read the instruction, which is at CS:IP, but that
1078 // linear address is guest physical without PG and guest virtual with PG
1079 if (info->cpu_mode == PHYSICAL_MEM) {
1080 // The real rip address is actually a combination of the rip + CS base
1081 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
1083 // The real rip address is actually a combination of the rip + CS base
1084 ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
1088 PrintDebug("Could not read instruction (ret=%d)\n", ret);
1092 while (is_prefix_byte(instr[index])) {
1093 switch(instr[index]) {
1094 case PREFIX_CS_OVERRIDE:
1095 case PREFIX_SS_OVERRIDE:
1096 case PREFIX_DS_OVERRIDE:
1097 case PREFIX_ES_OVERRIDE:
1098 case PREFIX_FS_OVERRIDE:
1099 case PREFIX_GS_OVERRIDE:
1100 PrintDebug("Segment Override!!\n");
1110 while (is_prefix_byte(instr[index])) {
1115 if ((instr[index] == cr_access_byte) &&
1116 (instr[index + 1] == mov_from_cr_byte)) {
1117 addr_t first_operand;
1118 addr_t second_operand;
1119 struct cr3_32 * virt_cr3;
1120 struct cr3_32 * real_cr3 = (struct cr3_32 *)&(info->ctrl_regs.cr3);
1121 operand_type_t addr_type;
1125 addr_type = decode_operands32(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
1127 if (addr_type != REG_OPERAND) {
1128 /* Mov to CR3 can only be a 32 bit register */
1132 virt_cr3 = (struct cr3_32 *)first_operand;
1134 if (info->shdw_pg_mode == SHADOW_PAGING) {
1135 *virt_cr3 = *(struct cr3_32 *)&(info->shdw_pg_state.guest_cr3);
1137 *virt_cr3 = *real_cr3;
1142 PrintDebug("Unknown Instruction\n");
1143 SerialMemDump(instr,15);
1147 PrintDebug("Invalid operating Mode (0x%x), control registers follow\n", info->cpu_mode);
1148 PrintV3CtrlRegs(&(info->ctrl_regs));