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);
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);
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);
357 if (new_cr0->pg == 1) {
358 info->mem_mode = VIRTUAL_MEM;
359 } else if (new_cr0->pg == 0) {
360 info->cpu_mode = REAL;
361 info->mem_mode = PHYSICAL_MEM;
364 *real_cr0 = *new_cr0;
368 } else if ((instr[index] == cr_access_byte) &&
369 (instr[index + 1] == lmsw_byte) &&
370 (MODRM_REG(instr[index + 2]) == lmsw_reg_byte)) {
371 addr_t first_operand;
372 addr_t second_operand;
373 struct cr0_real *real_cr0;
374 struct cr0_real *new_cr0;
375 operand_type_t addr_type;
376 char new_cr0_val = 0;
380 real_cr0 = (struct cr0_real*)&(info->ctrl_regs.cr0);
382 addr_type = decode_operands16(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG16);
384 if (addr_type == REG_OPERAND) {
385 new_cr0 = (struct cr0_real *)first_operand;
386 } else if (addr_type == MEM_OPERAND) {
388 // check segment descriptors....
390 /* TODO, TODO, TODO: Lookup segment overrides */
391 struct v3_segment *lmsw_segment = &(info->segments.ds);
393 if (info->mem_mode == PHYSICAL_MEM) {
394 if (guest_pa_to_host_va(info, get_addr_linear(info, first_operand, lmsw_segment), &host_addr) == -1) {
398 if (guest_va_to_host_va(info, get_addr_linear(info, first_operand, lmsw_segment), &host_addr) == -1) {
403 new_cr0 = (struct cr0_real *)host_addr;
409 if (new_cr0->pe == 0) {
410 // According to the intel manual this it is illegal to use
411 // lmsw to turn _off_ Protected mode
412 PrintDebug("Cannot switch to real mode with LMSW, unclear what to do\n");
416 new_cr0_val = (*(char *)(new_cr0)) & 0x0f;
419 if (info->shdw_pg_mode == SHADOW_PAGING) {
420 struct cr0_real * shadow_cr0 = (struct cr0_real*)&(info->shdw_pg_state.guest_cr0);
422 PrintDebug("Old CR0=%x, Old Shadow CR0=%x\n", *real_cr0, *shadow_cr0);
423 /* struct cr0_real is only 4 bits wide,
424 * so we can overwrite the real_cr0 without worrying about the shadow fields
426 *(char*)real_cr0 &= 0xf0;
427 *(char*)real_cr0 |= new_cr0_val;
429 *(char*)shadow_cr0 &= 0xf0;
430 *(char*)shadow_cr0 |= new_cr0_val;
433 PrintDebug("New CR0=%x, New Shadow CR0=%x\n", *real_cr0, *shadow_cr0);
435 PrintDebug("Old CR0=%x\n", *real_cr0);
436 // for now we just pass through....
437 *(char*)real_cr0 &= 0xf0;
438 *(char*)real_cr0 |= new_cr0_val;
440 PrintDebug("New CR0=%x\n", *real_cr0);
446 } else if ((instr[index] == 0x0f) &&
447 (instr[index + 1] == 0x06)) {
449 PrintDebug("CLTS instruction - clearing TS flag of real and shadow CR0\n");
458 PrintDebug("Unkown instruction: \n");
459 SerialMemDump(instr,15);
466 PrintDebug("Protected PAE Mode write to CR0 is UNIMPLEMENTED\n");
470 PrintDebug("Protected Long Mode write to CR0 is UNIMPLEMENTED\n");
475 PrintDebug("Unknown Mode write to CR0 (info->cpu_mode=0x%x\n)",info->cpu_mode);
486 int handle_cr0_read(struct guest_info * info) {
489 switch (info->cpu_mode) {
497 PrintDebug("Real Mode read from CR0 at linear guest pa 0x%x\n",get_addr_linear(info,info->rip,&(info->segments.cs)));
498 PrintV3Segments(info);
500 // The real rip address is actually a combination of the rip + CS base
501 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
503 // I think we should inject a GPF into the guest
504 PrintDebug("Could not read Real Mode instruction (ret=%d)\n", ret);
509 while (is_prefix_byte(instr[index])) {
510 switch(instr[index]) {
511 case PREFIX_CS_OVERRIDE:
512 case PREFIX_SS_OVERRIDE:
513 case PREFIX_DS_OVERRIDE:
514 case PREFIX_ES_OVERRIDE:
515 case PREFIX_FS_OVERRIDE:
516 case PREFIX_GS_OVERRIDE:
517 PrintDebug("Segment Override!!\n");
527 while (is_prefix_byte(instr[index])) {
532 if ((instr[index] == cr_access_byte) &&
533 (instr[index + 1] == smsw_byte) &&
534 (MODRM_REG(instr[index + 2]) == smsw_reg_byte)) {
536 // SMSW (store machine status word)
538 addr_t first_operand;
539 addr_t second_operand;
540 struct cr0_real *cr0;
541 operand_type_t addr_type;
546 cr0 = (struct cr0_real*)&(info->ctrl_regs.cr0);
549 addr_type = decode_operands16(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG16);
551 if (addr_type == MEM_OPERAND) {
554 if (guest_pa_to_host_va(info, first_operand + (info->segments.ds.base << 4), &host_addr) == -1) {
556 PrintDebug("Could not convert guest physical address to host virtual address\n");
560 first_operand = host_addr;
566 cr0_val = *(char*)cr0 & 0x0f;
568 *(char *)first_operand &= 0xf0;
569 *(char *)first_operand |= cr0_val;
571 PrintDebug("index = %d, rip = %x\n", index, (ulong_t)(info->rip));
573 PrintDebug("new_rip = %x\n", (ulong_t)(info->rip));
576 } else if ((instr[index] == cr_access_byte) &&
577 (instr[index+1] == mov_from_cr_byte)) {
579 * This can only take a 32 bit register argument in anything less than 64 bit mode.
581 addr_t first_operand;
582 addr_t second_operand;
583 operand_type_t addr_type;
585 struct cr0_32 * real_cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
589 addr_type = decode_operands16(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
591 struct cr0_32 * virt_cr0 = (struct cr0_32 *)first_operand;
593 if (addr_type != REG_OPERAND) {
594 // invalid opcode to guest
595 PrintDebug("Invalid operand type in mov from CR0\n");
599 if (info->shdw_pg_mode == SHADOW_PAGING) {
600 *virt_cr0 = *(struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
602 *virt_cr0 = *real_cr0;
608 PrintDebug("Unknown read instr from CR0\n");
622 PrintDebug("Protected %s Mode read from CR0 at guest %s linear rip 0x%x\n",
623 info->mem_mode == VIRTUAL_MEM ? "Paged" : "",
624 info->mem_mode == VIRTUAL_MEM ? "virtual" : "",
625 get_addr_linear(info, info->rip, &(info->segments.cs)));
627 // We need to read the instruction, which is at CS:IP, but that
628 // linear address is guest physical without PG and guest virtual with PG
629 if (info->cpu_mode == PHYSICAL_MEM) {
630 // The real rip address is actually a combination of the rip + CS base
631 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
633 // The real rip address is actually a combination of the rip + CS base
634 ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
638 PrintDebug("Instr (15 bytes) at %x:\n", instr);
639 PrintTraceMemDump((char*)instr, 15);
642 // I think we should inject a GPF into the guest
643 PrintDebug("Could not read Protected %s mode instruction (ret=%d)\n",
644 info->cpu_mode == VIRTUAL_MEM ? "Paged" : "", ret);
649 while (is_prefix_byte(instr[index])) {
650 switch(instr[index]) {
651 case PREFIX_CS_OVERRIDE:
652 case PREFIX_SS_OVERRIDE:
653 case PREFIX_DS_OVERRIDE:
654 case PREFIX_ES_OVERRIDE:
655 case PREFIX_FS_OVERRIDE:
656 case PREFIX_GS_OVERRIDE:
657 PrintDebug("Segment Override!!\n");
668 while (is_prefix_byte(instr[index])) {
673 if ((instr[index] == cr_access_byte) &&
674 (instr[index+1] == mov_from_cr_byte)) {
676 // MOV from CR0 to register
678 addr_t first_operand;
679 addr_t second_operand;
680 operand_type_t addr_type;
681 struct cr0_32 * virt_cr0;
682 struct cr0_32 * real_cr0 = (struct cr0_32 *)&(info->ctrl_regs.cr0);
686 addr_type = decode_operands32(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
688 if (addr_type != REG_OPERAND) {
689 PrintDebug("Invalid operand type in mov from CR0\n");
693 virt_cr0 = (struct cr0_32 *)first_operand;
695 if (info->shdw_pg_mode == SHADOW_PAGING) {
696 *virt_cr0 = *(struct cr0_32 *)&(info->shdw_pg_state.guest_cr0);
698 if (info->mem_mode == PHYSICAL_MEM) {
699 virt_cr0->pg = 0; // clear the pg bit because guest doesn't think it's on
703 *virt_cr0 = *real_cr0;
706 PrintDebug("real CR0: %x\n", *(uint_t*)real_cr0);
707 PrintDebug("returned CR0: %x\n", *(uint_t*)virt_cr0);
712 PrintDebug("Unknown read instruction from CR0\n");
719 PrintDebug("Protected PAE Mode read to CR0 is UNIMPLEMENTED\n");
723 PrintDebug("Protected Long Mode read to CR0 is UNIMPLEMENTED\n");
729 PrintDebug("Unknown Mode read from CR0 (info->cpu_mode=0x%x)\n",info->cpu_mode);
742 int handle_cr3_write(struct guest_info * info) {
743 if (info->cpu_mode == REAL) {
744 // WHAT THE HELL DOES THIS EVEN MEAN?????
750 PrintDebug("Real Mode Write to CR3??? What the fuck???\n");
751 // We need to read the instruction, which is at CS:IP, but that
752 // linear address is guest physical without PG and guest virtual with PG
754 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
757 PrintDebug("Could not read instruction (ret=%d)\n", ret);
761 while (is_prefix_byte(instr[index])) {
762 switch(instr[index]) {
763 case PREFIX_CS_OVERRIDE:
764 case PREFIX_SS_OVERRIDE:
765 case PREFIX_DS_OVERRIDE:
766 case PREFIX_ES_OVERRIDE:
767 case PREFIX_FS_OVERRIDE:
768 case PREFIX_GS_OVERRIDE:
769 PrintDebug("Segment Override!!\n");
779 if ((instr[index] == cr_access_byte) &&
780 (instr[index + 1] == mov_to_cr_byte)) {
782 addr_t first_operand;
783 addr_t second_operand;
784 struct cr3_32 * new_cr3;
785 // struct cr3_32 * real_cr3;
786 operand_type_t addr_type;
790 addr_type = decode_operands16(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
792 if (addr_type != REG_OPERAND) {
793 /* Mov to CR3 can only be a 32 bit register */
797 new_cr3 = (struct cr3_32 *)first_operand;
799 if (info->shdw_pg_mode == SHADOW_PAGING) {
801 struct cr3_32 * shadow_cr3 = (struct cr3_32 *)&(info->shdw_pg_state.shadow_cr3);
802 struct cr3_32 * guest_cr3 = (struct cr3_32 *)&(info->shdw_pg_state.guest_cr3);
806 if (CR3_TO_PDE32(*(uint_t*)shadow_cr3) != 0) {
807 PrintDebug("Shadow Page Table\n");
808 PrintDebugPageTables((pde32_t *)CR3_TO_PDE32(*(uint_t*)shadow_cr3));
812 /* Delete the current Page Tables */
813 delete_page_tables_pde32((pde32_t *)CR3_TO_PDE32(*(uint_t*)shadow_cr3));
815 PrintDebug("Old Shadow CR3=%x; Old Guest CR3=%x\n",
816 *(uint_t*)shadow_cr3, *(uint_t*)guest_cr3);
819 *guest_cr3 = *new_cr3;
823 // Something like this
824 shadow_pt = create_new_shadow_pt32(info);
825 //shadow_pt = setup_shadow_pt32(info, CR3_TO_PDE32(*(addr_t *)new_cr3));
827 /* Copy Various flags */
828 *shadow_cr3 = *new_cr3;
833 guest_pa_to_host_va(info, ((*(uint_t*)guest_cr3) & 0xfffff000), &tmp_addr);
834 PrintDebug("Guest PD\n");
835 PrintPD32((pde32_t *)tmp_addr);
841 shadow_cr3->pdt_base_addr = PD32_BASE_ADDR(shadow_pt);
843 PrintDebug("New Shadow CR3=%x; New Guest CR3=%x\n",
844 *(uint_t*)shadow_cr3, *(uint_t*)guest_cr3);
853 PrintDebug("Unknown Instruction\n");
854 SerialMemDump(instr,15);
860 } else if (info->cpu_mode == PROTECTED) {
865 PrintDebug("Protected %s mode write to CR3 at %s 0x%x\n",
866 info->cpu_mode==PROTECTED ? "" : "Paged",
867 info->cpu_mode==PROTECTED ? "guest physical" : "guest virtual",
868 get_addr_linear(info,info->rip,&(info->segments.cs)));
870 // We need to read the instruction, which is at CS:IP, but that
871 // linear address is guest physical without PG and guest virtual with PG
872 if (info->mem_mode == PHYSICAL_MEM) {
873 // The real rip address is actually a combination of the rip + CS base
874 PrintDebug("Writing Guest CR3 Write (Physical Address)\n");
875 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
877 PrintDebug("Writing Guest CR3 Write (Virtual Address)\n");
878 // The real rip address is actually a combination of the rip + CS base
879 ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
883 PrintDebug("Could not read instruction (ret=%d)\n", ret);
887 while (is_prefix_byte(instr[index])) {
888 switch(instr[index]) {
889 case PREFIX_CS_OVERRIDE:
890 case PREFIX_SS_OVERRIDE:
891 case PREFIX_DS_OVERRIDE:
892 case PREFIX_ES_OVERRIDE:
893 case PREFIX_FS_OVERRIDE:
894 case PREFIX_GS_OVERRIDE:
895 PrintDebug("Segment Override!!\n");
905 while (is_prefix_byte(instr[index])) {
910 if ((instr[index] == cr_access_byte) &&
911 (instr[index + 1] == mov_to_cr_byte)) {
913 addr_t first_operand;
914 addr_t second_operand;
915 struct cr3_32 * new_cr3;
916 // struct cr3_32 * real_cr3;
917 operand_type_t addr_type;
921 addr_type = decode_operands32(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
923 if (addr_type != REG_OPERAND) {
924 /* Mov to CR3 can only be a 32 bit register */
928 new_cr3 = (struct cr3_32 *)first_operand;
930 if (info->shdw_pg_mode == SHADOW_PAGING) {
932 struct cr3_32 * shadow_cr3 = (struct cr3_32 *)&(info->shdw_pg_state.shadow_cr3);
933 struct cr3_32 * guest_cr3 = (struct cr3_32 *)&(info->shdw_pg_state.guest_cr3);
937 if (CR3_TO_PDE32(*(uint_t*)shadow_cr3) != 0) {
938 PrintDebug("Shadow Page Table\n");
939 PrintDebugPageTables((pde32_t *)CR3_TO_PDE32(*(uint_t*)shadow_cr3));
943 /* Delete the current Page Tables */
944 delete_page_tables_pde32((pde32_t *)CR3_TO_PDE32(*(uint_t*)shadow_cr3));
946 PrintDebug("Old Shadow CR3=%x; Old Guest CR3=%x\n",
947 *(uint_t*)shadow_cr3, *(uint_t*)guest_cr3);
950 *guest_cr3 = *new_cr3;
954 // Something like this
955 shadow_pt = create_new_shadow_pt32(info);
956 //shadow_pt = setup_shadow_pt32(info, CR3_TO_PDE32(*(addr_t *)new_cr3));
958 /* Copy Various flags */
959 *shadow_cr3 = *new_cr3;
964 guest_pa_to_host_va(info, ((*(uint_t*)guest_cr3) & 0xfffff000), &tmp_addr);
965 PrintDebug("Guest PD\n");
966 PrintPD32((pde32_t *)tmp_addr);
972 shadow_cr3->pdt_base_addr = PD32_BASE_ADDR(shadow_pt);
974 PrintDebug("New Shadow CR3=%x; New Guest CR3=%x\n",
975 *(uint_t*)shadow_cr3, *(uint_t*)guest_cr3);
979 if (info->mem_mode == VIRTUAL_MEM) {
980 // If we aren't in paged mode then we have to preserve the identity mapped CR3
981 info->ctrl_regs.cr3 = *(addr_t*)shadow_cr3;
988 PrintDebug("Unknown Instruction\n");
989 SerialMemDump(instr,15);
993 PrintDebug("Invalid operating Mode (0x%x)\n", info->cpu_mode);
1003 int handle_cr3_read(struct guest_info * info) {
1005 if (info->cpu_mode == REAL) {
1009 addr_t linear_addr = 0;
1011 linear_addr = get_addr_linear(info, info->rip, &(info->segments.cs));
1014 PrintDebug("RIP Linear: %x\n", linear_addr);
1015 PrintV3Segments(info);
1017 ret = read_guest_pa_memory(info, linear_addr, 15, instr);
1020 PrintDebug("Could not read instruction (ret=%d)\n", ret);
1024 while (is_prefix_byte(instr[index])) {
1025 switch(instr[index]) {
1026 case PREFIX_CS_OVERRIDE:
1027 case PREFIX_SS_OVERRIDE:
1028 case PREFIX_DS_OVERRIDE:
1029 case PREFIX_ES_OVERRIDE:
1030 case PREFIX_FS_OVERRIDE:
1031 case PREFIX_GS_OVERRIDE:
1032 PrintDebug("Segment Override!!\n");
1042 if ((instr[index] == cr_access_byte) &&
1043 (instr[index + 1] == mov_from_cr_byte)) {
1044 addr_t first_operand;
1045 addr_t second_operand;
1046 struct cr3_32 * virt_cr3;
1047 struct cr3_32 * real_cr3 = (struct cr3_32 *)&(info->ctrl_regs.cr3);
1048 operand_type_t addr_type;
1052 addr_type = decode_operands16(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
1054 if (addr_type != REG_OPERAND) {
1055 /* Mov to CR3 can only be a 32 bit register */
1059 virt_cr3 = (struct cr3_32 *)first_operand;
1061 if (info->shdw_pg_mode == SHADOW_PAGING) {
1062 *virt_cr3 = *(struct cr3_32 *)&(info->shdw_pg_state.guest_cr3);
1064 *virt_cr3 = *real_cr3;
1069 PrintDebug("Unknown Instruction\n");
1070 SerialMemDump(instr,15);
1076 } else if (info->cpu_mode == PROTECTED) {
1083 // We need to read the instruction, which is at CS:IP, but that
1084 // linear address is guest physical without PG and guest virtual with PG
1085 if (info->cpu_mode == PHYSICAL_MEM) {
1086 // The real rip address is actually a combination of the rip + CS base
1087 ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
1089 // The real rip address is actually a combination of the rip + CS base
1090 ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
1094 PrintDebug("Could not read instruction (ret=%d)\n", ret);
1098 while (is_prefix_byte(instr[index])) {
1099 switch(instr[index]) {
1100 case PREFIX_CS_OVERRIDE:
1101 case PREFIX_SS_OVERRIDE:
1102 case PREFIX_DS_OVERRIDE:
1103 case PREFIX_ES_OVERRIDE:
1104 case PREFIX_FS_OVERRIDE:
1105 case PREFIX_GS_OVERRIDE:
1106 PrintDebug("Segment Override!!\n");
1116 while (is_prefix_byte(instr[index])) {
1121 if ((instr[index] == cr_access_byte) &&
1122 (instr[index + 1] == mov_from_cr_byte)) {
1123 addr_t first_operand;
1124 addr_t second_operand;
1125 struct cr3_32 * virt_cr3;
1126 struct cr3_32 * real_cr3 = (struct cr3_32 *)&(info->ctrl_regs.cr3);
1127 operand_type_t addr_type;
1131 addr_type = decode_operands32(&(info->vm_regs), instr + index, &index, &first_operand, &second_operand, REG32);
1133 if (addr_type != REG_OPERAND) {
1134 /* Mov to CR3 can only be a 32 bit register */
1138 virt_cr3 = (struct cr3_32 *)first_operand;
1140 if (info->shdw_pg_mode == SHADOW_PAGING) {
1141 *virt_cr3 = *(struct cr3_32 *)&(info->shdw_pg_state.guest_cr3);
1143 *virt_cr3 = *real_cr3;
1148 PrintDebug("Unknown Instruction\n");
1149 SerialMemDump(instr,15);
1153 PrintDebug("Invalid operating Mode (0x%x), control registers follow\n", info->cpu_mode);
1154 PrintV3CtrlRegs(info);