1 #include <geekos/svm.h>
2 #include <geekos/vmm.h>
4 #include <geekos/vmcb.h>
5 #include <geekos/vmm_mem.h>
6 #include <geekos/vmm_paging.h>
7 #include <geekos/svm_handler.h>
9 #include <geekos/vmm_debug.h>
10 #include <geekos/vm_guest_mem.h>
13 /* TEMPORARY BECAUSE SVM IS WEIRD */
14 #include <geekos/tss.h>
17 extern struct vmm_os_hooks * os_hooks;
19 extern uint_t cpuid_ecx(uint_t op);
20 extern uint_t cpuid_edx(uint_t op);
21 extern void Get_MSR(uint_t MSR, uint_t * high_byte, uint_t * low_byte);
22 extern void Set_MSR(uint_t MSR, uint_t high_byte, uint_t low_byte);
23 extern uint_t launch_svm(vmcb_t * vmcb_addr);
24 extern void safe_svm_launch(vmcb_t * vmcb_addr, struct guest_gprs * gprs);
26 extern uint_t Get_CR3();
28 extern void GetGDTR(void * gdt);
29 extern void GetIDTR(void * idt);
31 extern void DisableInts();
33 /* Checks machine SVM capability */
34 /* Implemented from: AMD Arch Manual 3, sect 15.4 */
35 int is_svm_capable() {
36 uint_t ret = cpuid_ecx(CPUID_FEATURE_IDS);
37 uint_t vm_cr_low = 0, vm_cr_high = 0;
40 if ((ret & CPUID_FEATURE_IDS_ecx_svm_avail) == 0) {
41 PrintDebug("SVM Not Available\n");
45 Get_MSR(SVM_VM_CR_MSR, &vm_cr_high, &vm_cr_low);
47 if ((ret & CPUID_SVM_REV_AND_FEATURE_IDS_edx_np) == 1) {
48 PrintDebug("Nested Paging not supported\n");
51 if ((vm_cr_low & SVM_VM_CR_MSR_svmdis) == 0) {
55 ret = cpuid_edx(CPUID_SVM_REV_AND_FEATURE_IDS);
57 if ((ret & CPUID_SVM_REV_AND_FEATURE_IDS_edx_svml) == 0) {
58 PrintDebug("SVM BIOS Disabled, not unlockable\n");
60 PrintDebug("SVM is locked with a key\n");
68 void Init_SVM(struct vmm_ctrl_ops * vmm_ops) {
73 // Enable SVM on the CPU
74 Get_MSR(EFER_MSR, &(msr.e_reg.high), &(msr.e_reg.low));
75 msr.e_reg.low |= EFER_MSR_svm_enable;
76 Set_MSR(EFER_MSR, 0, msr.e_reg.low);
78 PrintDebug("SVM Enabled\n");
81 // Setup the host state save area
82 host_state = os_hooks->allocate_pages(4);
85 msr.e_reg.low = (uint_t)host_state;
88 PrintDebug("Host State being saved at %x\n", (uint_t)host_state);
89 Set_MSR(SVM_VM_HSAVE_PA_MSR, msr.e_reg.high, msr.e_reg.low);
93 // Setup the SVM specific vmm operations
94 vmm_ops->init_guest = &init_svm_guest;
95 vmm_ops->start_guest = &start_svm_guest;
102 int init_svm_guest(struct guest_info *info) {
104 PrintDebug("Allocating VMCB\n");
105 info->vmm_data = (void*)Allocate_VMCB();
108 //PrintDebug("Generating Guest nested page tables\n");
109 // info->page_tables = NULL;
110 //info->page_tables = generate_guest_page_tables_64(&(info->mem_layout), &(info->mem_list));
111 //info->page_tables = generate_guest_page_tables(&(info->mem_layout), &(info->mem_list));
112 //PrintDebugPageTables(info->page_tables);
116 PrintDebug("Initializing VMCB (addr=%x)\n", info->vmm_data);
117 Init_VMCB((vmcb_t*)(info->vmm_data), *info);
121 info->vm_regs.rdi = 0;
122 info->vm_regs.rsi = 0;
123 info->vm_regs.rbp = 0;
124 info->vm_regs.rsp = 0;
125 info->vm_regs.rbx = 0;
126 info->vm_regs.rdx = 0;
127 info->vm_regs.rcx = 0;
128 info->vm_regs.rax = 0;
134 // can we start a kernel thread here...
135 int start_svm_guest(struct guest_info *info) {
139 PrintDebug("Launching SVM VM (vmcb=%x)\n", info->vmm_data);
140 //PrintDebugVMCB((vmcb_t*)(info->vmm_data));
144 PrintDebug("SVM Launch Args (vmcb=%x), (info=%x), (vm_regs=%x)\n", info->vmm_data, &(info->vm_regs));
145 PrintDebug("Launching to RIP: %x\n", info->rip);
146 safe_svm_launch((vmcb_t*)(info->vmm_data), &(info->vm_regs));
147 //launch_svm((vmcb_t*)(info->vmm_data));
148 PrintDebug("SVM Returned\n");
150 if (handle_svm_exit(info) != 0) {
151 // handle exit code....
160 vmcb_t * Allocate_VMCB() {
161 vmcb_t * vmcb_page = (vmcb_t*)os_hooks->allocate_pages(1);
164 memset(vmcb_page, 0, 4096);
170 void Init_VMCB_Real(vmcb_t * vmcb, struct guest_info vm_info) {
171 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA(vmcb);
172 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA(vmcb);
176 guest_state->rsp = vm_info.vm_regs.rsp;
177 guest_state->rip = vm_info.rip;
183 guest_state->efer |= EFER_MSR_svm_enable;
184 guest_state->rflags = 0x00000002; // The reserved bit is always 1
185 ctrl_area->svm_instrs.instrs.VMRUN = 1;
186 // guest_state->cr0 = 0x00000001; // PE
187 ctrl_area->guest_ASID = 1;
188 guest_state->cr0 = 0x60000010;
191 ctrl_area->exceptions.ex_names.de = 1;
192 ctrl_area->exceptions.ex_names.df = 1;
193 ctrl_area->exceptions.ex_names.pf = 1;
194 ctrl_area->exceptions.ex_names.ts = 1;
195 ctrl_area->exceptions.ex_names.ss = 1;
196 ctrl_area->exceptions.ex_names.ac = 1;
197 ctrl_area->exceptions.ex_names.mc = 1;
198 ctrl_area->exceptions.ex_names.gp = 1;
199 ctrl_area->exceptions.ex_names.ud = 1;
200 ctrl_area->exceptions.ex_names.np = 1;
201 ctrl_area->exceptions.ex_names.of = 1;
202 ctrl_area->exceptions.ex_names.nmi = 1;
204 guest_state->cs.selector = 0xf000;
205 guest_state->cs.limit=0xffff;
206 guest_state->cs.base = 0xffff0000;
207 guest_state->cs.attrib.raw = 0x9a;
210 struct vmcb_selector *segregs [] = {&(guest_state->ss), &(guest_state->ds), &(guest_state->es), &(guest_state->fs), &(guest_state->gs), NULL};
211 for ( i = 0; segregs[i] != NULL; i++) {
212 struct vmcb_selector * seg = segregs[i];
214 seg->selector = 0x0000;
215 seg->base = 0xffff0000;
216 seg->attrib.raw = 0x9b;
223 EAX, EBX, ECX, ESI, EDI, EBP, ESP == 0x0
226 guest_state->gdtr.base = 0;
227 guest_state->gdtr.limit = 0xffff;
228 guest_state->gdtr.attrib.raw = 0x0;
230 guest_state->idtr.base = 0;
231 guest_state->idtr.limit = 0xffff;
232 guest_state->idtr.attrib.raw = 0x0;
234 guest_state->ldtr.base = 0;
235 guest_state->ldtr.limit = 0xffff;
236 guest_state->ldtr.attrib.raw = 0x82;
238 guest_state->tr.base = 0;
239 guest_state->tr.limit = 0xffff;
240 guest_state->tr.attrib.raw = 0x83;
245 if (vm_info.io_map.num_ports > 0) {
246 vmm_io_hook_t * iter;
247 addr_t io_port_bitmap;
249 io_port_bitmap = (addr_t)os_hooks->allocate_pages(3);
250 memset((uchar_t*)io_port_bitmap, 0, PAGE_SIZE * 3);
252 ctrl_area->IOPM_BASE_PA = io_port_bitmap;
254 //PrintDebug("Setting up IO Map at 0x%x\n", io_port_bitmap);
256 FOREACH_IO_HOOK(vm_info.io_map, iter) {
257 ushort_t port = iter->port;
258 uchar_t * bitmap = (uchar_t *)io_port_bitmap;
260 bitmap += (port / 8);
261 PrintDebug("Setting Bit in block %x\n", bitmap);
262 *bitmap |= 1 << (port % 8);
265 // memset((uchar_t*)io_port_bitmap, 0xff, PAGE_SIZE * 2);
266 //PrintDebugMemDump((uchar_t*)io_port_bitmap, PAGE_SIZE *2);
268 ctrl_area->instrs.instrs.IOIO_PROT = 1;
271 ctrl_area->instrs.instrs.INTR = 1;
273 // also determine if CPU supports nested paging
275 if (vm_info.page_mode == SHADOW_PAGING) {
276 PrintDebug("Creating initial shadow page table\n");
277 vm_info.shdw_pg_state.shadow_cr3.e_reg.low |= ((addr_t)create_passthrough_pde32_pts(&vm_info) & ~0xfff);
278 PrintDebug("Created\n");
280 guest_state->cr3 = vm_info.shdw_pg_state.shadow_cr3.r_reg;
282 ctrl_area->cr_reads.crs.cr3 = 1;
283 ctrl_area->cr_writes.crs.cr3 = 1;
284 ctrl_area->cr_reads.crs.cr0 = 1;
285 ctrl_area->cr_writes.crs.cr0 = 1;
287 ctrl_area->instrs.instrs.INVLPG = 1;
288 ctrl_area->instrs.instrs.INVLPGA = 1;
291 guest_state->g_pat = 0x7040600070406ULL;
293 guest_state->cr0 |= 0x80000000;
294 } else if (vm_info.page_mode == NESTED_PAGING) {
295 // Flush the TLB on entries/exits
296 //ctrl_area->TLB_CONTROL = 1;
298 // Enable Nested Paging
299 //ctrl_area->NP_ENABLE = 1;
301 //PrintDebug("NP_Enable at 0x%x\n", &(ctrl_area->NP_ENABLE));
303 // Set the Nested Page Table pointer
304 // ctrl_area->N_CR3 = ((addr_t)vm_info.page_tables);
305 // ctrl_area->N_CR3 = (addr_t)(vm_info.page_tables);
307 // ctrl_area->N_CR3 = Get_CR3();
308 // guest_state->cr3 |= (Get_CR3() & 0xfffff000);
310 // guest_state->g_pat = 0x7040600070406ULL;
316 void Init_VMCB(vmcb_t * vmcb, struct guest_info vm_info) {
317 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA(vmcb);
318 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA(vmcb);
322 guest_state->rsp = vm_info.vm_regs.rsp;
323 guest_state->rip = vm_info.rip;
326 //ctrl_area->instrs.instrs.CR0 = 1;
327 ctrl_area->cr_reads.crs.cr0 = 1;
328 ctrl_area->cr_writes.crs.cr0 = 1;
330 guest_state->efer |= EFER_MSR_svm_enable;
331 guest_state->rflags = 0x00000002; // The reserved bit is always 1
332 ctrl_area->svm_instrs.instrs.VMRUN = 1;
333 // guest_state->cr0 = 0x00000001; // PE
334 ctrl_area->guest_ASID = 1;
337 ctrl_area->exceptions.ex_names.de = 1;
338 ctrl_area->exceptions.ex_names.df = 1;
339 ctrl_area->exceptions.ex_names.pf = 1;
340 ctrl_area->exceptions.ex_names.ts = 1;
341 ctrl_area->exceptions.ex_names.ss = 1;
342 ctrl_area->exceptions.ex_names.ac = 1;
343 ctrl_area->exceptions.ex_names.mc = 1;
344 ctrl_area->exceptions.ex_names.gp = 1;
345 ctrl_area->exceptions.ex_names.ud = 1;
346 ctrl_area->exceptions.ex_names.np = 1;
347 ctrl_area->exceptions.ex_names.of = 1;
348 ctrl_area->exceptions.ex_names.nmi = 1;
350 guest_state->cs.selector = 0x0000;
351 guest_state->cs.limit=~0u;
352 guest_state->cs.base = guest_state->cs.selector<<4;
353 guest_state->cs.attrib.raw = 0xf3;
356 struct vmcb_selector *segregs [] = {&(guest_state->ss), &(guest_state->ds), &(guest_state->es), &(guest_state->fs), &(guest_state->gs), NULL};
357 for ( i = 0; segregs[i] != NULL; i++) {
358 struct vmcb_selector * seg = segregs[i];
360 seg->selector = 0x0000;
361 seg->base = seg->selector << 4;
362 seg->attrib.raw = 0xf3;
366 if (vm_info.io_map.num_ports > 0) {
367 vmm_io_hook_t * iter;
368 addr_t io_port_bitmap;
370 io_port_bitmap = (addr_t)os_hooks->allocate_pages(3);
371 memset((uchar_t*)io_port_bitmap, 0, PAGE_SIZE * 3);
373 ctrl_area->IOPM_BASE_PA = io_port_bitmap;
375 //PrintDebug("Setting up IO Map at 0x%x\n", io_port_bitmap);
377 FOREACH_IO_HOOK(vm_info.io_map, iter) {
378 ushort_t port = iter->port;
379 uchar_t * bitmap = (uchar_t *)io_port_bitmap;
381 bitmap += (port / 8);
382 PrintDebug("Setting Bit in block %x\n", bitmap);
383 *bitmap |= 1 << (port % 8);
387 //PrintDebugMemDump((uchar_t*)io_port_bitmap, PAGE_SIZE *2);
389 ctrl_area->instrs.instrs.IOIO_PROT = 1;
392 ctrl_area->instrs.instrs.INTR = 1;
396 if (vm_info.page_mode == SHADOW_PAGING) {
397 PrintDebug("Creating initial shadow page table\n");
398 vm_info.shdw_pg_state.shadow_cr3.e_reg.low |= ((addr_t)create_passthrough_pde32_pts(&vm_info) & ~0xfff);
399 PrintDebug("Created\n");
401 guest_state->cr3 = vm_info.shdw_pg_state.shadow_cr3.r_reg;
403 ctrl_area->cr_reads.crs.cr3 = 1;
404 ctrl_area->cr_writes.crs.cr3 = 1;
407 ctrl_area->instrs.instrs.INVLPG = 1;
408 ctrl_area->instrs.instrs.INVLPGA = 1;
410 guest_state->g_pat = 0x7040600070406ULL;
412 guest_state->cr0 |= 0x80000000;
413 } else if (vm_info.page_mode == NESTED_PAGING) {
414 // Flush the TLB on entries/exits
415 //ctrl_area->TLB_CONTROL = 1;
417 // Enable Nested Paging
418 //ctrl_area->NP_ENABLE = 1;
420 //PrintDebug("NP_Enable at 0x%x\n", &(ctrl_area->NP_ENABLE));
422 // Set the Nested Page Table pointer
423 // ctrl_area->N_CR3 = ((addr_t)vm_info.page_tables);
424 // ctrl_area->N_CR3 = (addr_t)(vm_info.page_tables);
426 // ctrl_area->N_CR3 = Get_CR3();
427 // guest_state->cr3 |= (Get_CR3() & 0xfffff000);
429 // guest_state->g_pat = 0x7040600070406ULL;
436 void Init_VMCB_pe(vmcb_t *vmcb, struct guest_info vm_info) {
437 vmcb_ctrl_t * ctrl_area = GET_VMCB_CTRL_AREA(vmcb);
438 vmcb_saved_state_t * guest_state = GET_VMCB_SAVE_STATE_AREA(vmcb);
442 guest_state->rsp = vm_info.vm_regs.rsp;
443 guest_state->rip = vm_info.rip;
446 /* I pretty much just gutted this from TVMM */
447 /* Note: That means its probably wrong */
449 // set the segment registers to mirror ours
450 guest_state->cs.selector = 1<<3;
451 guest_state->cs.attrib.fields.type = 0xa; // Code segment+read
452 guest_state->cs.attrib.fields.S = 1;
453 guest_state->cs.attrib.fields.P = 1;
454 guest_state->cs.attrib.fields.db = 1;
455 guest_state->cs.attrib.fields.G = 1;
456 guest_state->cs.limit = 0xfffff;
457 guest_state->cs.base = 0;
459 struct vmcb_selector *segregs [] = {&(guest_state->ss), &(guest_state->ds), &(guest_state->es), &(guest_state->fs), &(guest_state->gs), NULL};
460 for ( i = 0; segregs[i] != NULL; i++) {
461 struct vmcb_selector * seg = segregs[i];
463 seg->selector = 2<<3;
464 seg->attrib.fields.type = 0x2; // Data Segment+read/write
465 seg->attrib.fields.S = 1;
466 seg->attrib.fields.P = 1;
467 seg->attrib.fields.db = 1;
468 seg->attrib.fields.G = 1;
469 seg->limit = 0xfffff;
475 /* JRL THIS HAS TO GO */
477 guest_state->tr.selector = GetTR_Selector();
478 guest_state->tr.attrib.fields.type = 0x9;
479 guest_state->tr.attrib.fields.P = 1;
480 guest_state->tr.limit = GetTR_Limit();
481 guest_state->tr.base = GetTR_Base();// - 0x2000;
489 guest_state->efer |= EFER_MSR_svm_enable;
490 guest_state->rflags = 0x00000002; // The reserved bit is always 1
491 ctrl_area->svm_instrs.instrs.VMRUN = 1;
492 guest_state->cr0 = 0x00000001; // PE
493 ctrl_area->guest_ASID = 1;
496 // guest_state->cpl = 0;
502 ctrl_area->cr_writes.crs.cr4 = 1;
504 ctrl_area->exceptions.ex_names.de = 1;
505 ctrl_area->exceptions.ex_names.df = 1;
506 ctrl_area->exceptions.ex_names.pf = 1;
507 ctrl_area->exceptions.ex_names.ts = 1;
508 ctrl_area->exceptions.ex_names.ss = 1;
509 ctrl_area->exceptions.ex_names.ac = 1;
510 ctrl_area->exceptions.ex_names.mc = 1;
511 ctrl_area->exceptions.ex_names.gp = 1;
512 ctrl_area->exceptions.ex_names.ud = 1;
513 ctrl_area->exceptions.ex_names.np = 1;
514 ctrl_area->exceptions.ex_names.of = 1;
515 ctrl_area->exceptions.ex_names.nmi = 1;
519 ctrl_area->instrs.instrs.IOIO_PROT = 1;
520 ctrl_area->IOPM_BASE_PA = (uint_t)os_hooks->allocate_pages(3);
524 tmp_reg.r_reg = ctrl_area->IOPM_BASE_PA;
525 memset((void*)(tmp_reg.e_reg.low), 0xffffffff, PAGE_SIZE * 2);
528 ctrl_area->instrs.instrs.INTR = 1;
535 memset(gdt_buf, 0, 6);
536 memset(idt_buf, 0, 6);
539 uint_t gdt_base, idt_base;
540 ushort_t gdt_limit, idt_limit;
543 gdt_base = *(ulong_t*)((uchar_t*)gdt_buf + 2) & 0xffffffff;
544 gdt_limit = *(ushort_t*)(gdt_buf) & 0xffff;
545 PrintDebug("GDT: base: %x, limit: %x\n", gdt_base, gdt_limit);
548 idt_base = *(ulong_t*)(idt_buf + 2) & 0xffffffff;
549 idt_limit = *(ushort_t*)(idt_buf) & 0xffff;
550 PrintDebug("IDT: base: %x, limit: %x\n",idt_base, idt_limit);
553 // gdt_base -= 0x2000;
554 //idt_base -= 0x2000;
556 guest_state->gdtr.base = gdt_base;
557 guest_state->gdtr.limit = gdt_limit;
558 guest_state->idtr.base = idt_base;
559 guest_state->idtr.limit = idt_limit;
565 // also determine if CPU supports nested paging
567 if (vm_info.page_tables) {
569 // Flush the TLB on entries/exits
570 ctrl_area->TLB_CONTROL = 1;
572 // Enable Nested Paging
573 ctrl_area->NP_ENABLE = 1;
575 PrintDebug("NP_Enable at 0x%x\n", &(ctrl_area->NP_ENABLE));
577 // Set the Nested Page Table pointer
578 ctrl_area->N_CR3 |= ((addr_t)vm_info.page_tables & 0xfffff000);
581 // ctrl_area->N_CR3 = Get_CR3();
582 // guest_state->cr3 |= (Get_CR3() & 0xfffff000);
584 guest_state->g_pat = 0x7040600070406ULL;
586 PrintDebug("Set Nested CR3: lo: 0x%x hi: 0x%x\n", (uint_t)*(&(ctrl_area->N_CR3)), (uint_t)*((unsigned char *)&(ctrl_area->N_CR3) + 4));
587 PrintDebug("Set Guest CR3: lo: 0x%x hi: 0x%x\n", (uint_t)*(&(guest_state->cr3)), (uint_t)*((unsigned char *)&(guest_state->cr3) + 4));
589 // guest_state->cr0 |= 0x80000000;