Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


real->protected mode switch should work now
[palacios.git] / palacios / src / geekos / svm_ctrl_regs.c
1 #include <geekos/svm_ctrl_regs.h>
2 #include <geekos/vmm_mem.h>
3 #include <geekos/vmm.h>
4 #include <geekos/vmcb.h>
5 #include <geekos/vmm_emulate.h>
6 #include <geekos/vm_guest_mem.h>
7 #include <geekos/vmm_ctrl_regs.h>
8
9
10 int handle_cr0_write(struct guest_info * info) {
11   //vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA((vmcb_t *)(info->vmm_data));
12   vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA((vmcb_t*)(info->vmm_data));
13   char instr[15];
14   
15   
16
17
18   if (info->cpu_mode == REAL) {
19     int index = 0;
20     int ret;
21
22     // The real rip address is actually a combination of the rip + CS base 
23     ret = read_guest_pa_memory(info, (addr_t)guest_state->rip, 15, instr);
24     if (ret != 0) {
25       // I think we should inject a GPF into the guest
26       PrintDebug("Could not read instruction (ret=%d)\n", ret);
27       return -1;
28     }
29
30     while (is_prefix_byte(instr[index])) {
31       PrintDebug("instr(%d): 0x%x\n", index, instr[index]);
32       index++; 
33     }
34     PrintDebug("instr(%d): 0x%x\n", index, instr[index]);
35     PrintDebug("instr(%d): 0x%x\n", index+1, instr[index + 1]);
36
37     if ((instr[index] == cr_access_byte) && 
38         (instr[index + 1] == lmsw_byte) && 
39         (MODRM_REG(instr[index + 2]) == lmsw_reg_byte)) {
40  
41       addr_t first_operand;
42       addr_t second_operand;
43       struct cr0_real *old_cr0;
44       struct cr0_real *new_cr0;
45      
46       // LMSW
47       // decode mod/RM
48       index += 2;
49  
50       old_cr0 = (struct cr0_real*)&(guest_state->cr0);
51
52       if (decode_operands16(&(info->vm_regs), instr + index, &first_operand, &second_operand, REG16) != 0) {
53         // error... don't know what to do
54         return -1;
55       }
56       
57       index += 3;
58
59       new_cr0 = (struct cr0_real *)first_operand;
60
61       if ((new_cr0->pe == 1) && (old_cr0->pe == 0)) {
62         info->cpu_mode = PROTECTED;
63       }
64       
65
66       if (info->page_mode == SHADOW_PAGING) {
67         struct cr0_real * virt_cr0 = (struct cr0_real*)&(info->shdw_pg_state.guest_cr0);
68
69         /* struct cr0_real is only 4 bits wide, 
70          * so we can overwrite the old_cr0 without worrying about the shadow fields
71          */
72         *old_cr0 = *new_cr0;
73         *virt_cr0 = *new_cr0;
74       } else {
75         // for now we just pass through....
76         *old_cr0 = *new_cr0;
77       }
78
79       PrintDebug("index = %d, rip = %x\n", index, (ulong_t)(info->rip));
80       info->rip += index;
81       PrintDebug("new_rip = %x\n", (ulong_t)(info->rip));
82     } else if ((instr[index] == cr_access_byte) && 
83                (instr[index + 1] == clts_byte)) {
84       // CLTS
85     } else {
86       // unsupported instruction, UD the guest
87       return -1;
88     }
89
90
91   } else if (info->cpu_mode == PROTECTED) {
92     PrintDebug("Protected Mode write to CR0\n");
93     while(1);
94   } else {
95     PrintDebug("Unknown Mode write to CR0\n");
96     while(1);
97   }
98   return 0;
99 }
100
101