//
-#define CHK_VMCS_READ(tag, val) {if (VMCS_READ(tag, val) != 0) return -1;}
-#define CHK_VMCS_WRITE(tag, val) {if (VMCS_WRITE(tag, val) != 0) return -1;}
-
-
-
-int CopyOutVMCSGuestStateArea(struct VMCSGuestStateArea *p) {
- CHK_VMCS_READ(GUEST_CR0, &(p->cr0));
- CHK_VMCS_READ(GUEST_CR3, &(p->cr3));
- CHK_VMCS_READ(GUEST_CR4, &(p->cr4));
- CHK_VMCS_READ(GUEST_DR7, &(p->dr7));
- CHK_VMCS_READ(GUEST_RSP, &(p->rsp));
- CHK_VMCS_READ(GUEST_RIP, &(p->rip));
- CHK_VMCS_READ(GUEST_RFLAGS, &(p->rflags));
- CHK_VMCS_READ(VMCS_GUEST_CS_SELECTOR, &(p->cs.selector));
- CHK_VMCS_READ(VMCS_GUEST_SS_SELECTOR, &(p->ss.selector));
- CHK_VMCS_READ(VMCS_GUEST_DS_SELECTOR, &(p->ds.selector));
- CHK_VMCS_READ(VMCS_GUEST_ES_SELECTOR, &(p->es.selector));
- CHK_VMCS_READ(VMCS_GUEST_FS_SELECTOR, &(p->fs.selector));
- CHK_VMCS_READ(VMCS_GUEST_GS_SELECTOR, &(p->gs.selector));
- CHK_VMCS_READ(VMCS_GUEST_LDTR_SELECTOR, &(p->ldtr.selector));
- CHK_VMCS_READ(VMCS_GUEST_TR_SELECTOR, &(p->tr.selector));
- CHK_VMCS_READ(GUEST_CS_BASE, &(p->cs.baseAddr));
- CHK_VMCS_READ(GUEST_SS_BASE, &(p->ss.baseAddr));
- CHK_VMCS_READ(GUEST_DS_BASE, &(p->ds.baseAddr));
- CHK_VMCS_READ(GUEST_ES_BASE, &(p->es.baseAddr));
- CHK_VMCS_READ(GUEST_FS_BASE, &(p->fs.baseAddr));
- CHK_VMCS_READ(GUEST_GS_BASE, &(p->gs.baseAddr));
- CHK_VMCS_READ(GUEST_LDTR_BASE, &(p->ldtr.baseAddr));
- CHK_VMCS_READ(GUEST_TR_BASE, &(p->tr.baseAddr));
- CHK_VMCS_READ(GUEST_CS_LIMIT, &(p->cs.limit));
- CHK_VMCS_READ(GUEST_SS_LIMIT, &(p->ss.limit));
- CHK_VMCS_READ(GUEST_DS_LIMIT, &(p->ds.limit));
- CHK_VMCS_READ(GUEST_ES_LIMIT, &(p->es.limit));
- CHK_VMCS_READ(GUEST_FS_LIMIT, &(p->fs.limit));
- CHK_VMCS_READ(GUEST_GS_LIMIT, &(p->gs.limit));
- CHK_VMCS_READ(GUEST_LDTR_LIMIT, &(p->ldtr.limit));
- CHK_VMCS_READ(GUEST_TR_LIMIT, &(p->tr.limit));
- CHK_VMCS_READ(GUEST_CS_ACCESS, &(p->cs.access));
- CHK_VMCS_READ(GUEST_SS_ACCESS, &(p->ss.access));
- CHK_VMCS_READ(GUEST_DS_ACCESS, &(p->ds.access));
- CHK_VMCS_READ(GUEST_ES_ACCESS, &(p->es.access));
- CHK_VMCS_READ(GUEST_FS_ACCESS, &(p->fs.access));
- CHK_VMCS_READ(GUEST_GS_ACCESS, &(p->gs.access));
- CHK_VMCS_READ(GUEST_LDTR_ACCESS, &(p->ldtr.access));
- CHK_VMCS_READ(GUEST_TR_ACCESS, &(p->tr.access));
- CHK_VMCS_READ(GUEST_GDTR_BASE, &(p->gdtr.baseAddr));
- CHK_VMCS_READ(GUEST_IDTR_BASE, &(p->idtr.baseAddr));
- CHK_VMCS_READ(GUEST_GDTR_LIMIT, &(p->gdtr.limit));
- CHK_VMCS_READ(GUEST_IDTR_LIMIT, &(p->idtr.limit));
- CHK_VMCS_READ(GUEST_IA32_DEBUGCTL, &(p->dbg_ctrl));
- CHK_VMCS_READ(GUEST_IA32_DEBUGCTL_HIGH, ((char *)&(p->dbg_ctrl)) + 4);
- CHK_VMCS_READ(GUEST_IA32_SYSENTER_CS, &(p->sysenter_cs));
- CHK_VMCS_READ(GUEST_IA32_SYSENTER_ESP, &(p->sysenter_esp));
- CHK_VMCS_READ(GUEST_IA32_SYSENTER_EIP, &(p->sysenter_eip));
- CHK_VMCS_READ(GUEST_SMBASE, &(p->smbase));
-
- CHK_VMCS_READ(GUEST_ACTIVITY_STATE, &(p->activity));
- CHK_VMCS_READ(GUEST_INT_STATE, &(p->interrupt_state));
- CHK_VMCS_READ(GUEST_PENDING_DEBUG_EXCS, &(p->pending_dbg_exceptions));
- CHK_VMCS_READ(VMCS_LINK_PTR, &(p->vmcs_link));
- CHK_VMCS_READ(VMCS_LINK_PTR_HIGH, ((char *)&(p->vmcs_link)) + 4);
- return 0;
+static inline void print_vmcs_field(uint_t vmcs_index) {
+ int len = v3_vmcs_get_field_len(vmcs_index);
+ addr_t val;
+
+ if (vmcs_read(vmcs_index, &val, len) != VMX_SUCCESS) {
+ PrintError("VMCS_READ error for index %x\n", vmcs_index);
+ return;
+ };
+
+ if (len == 2) {
+ PrintDebug("%s: %x\n", v3_vmcs_get_field_name(vmcs_index), (uint16_t)val);
+ } else if (len == 4) {
+ PrintDebug("%s: %x\n", v3_vmcs_get_field_name(vmcs_index), (uint32_t)val);
+ } else if (len == 8) {
+ PrintDebug("%s: %p\n", v3_vmcs_get_field_name(vmcs_index), (void *)(addr_t)val);
+ }
}
-int CopyInVMCSGuestStateArea(struct VMCSGuestStateArea *p) {
- CHK_VMCS_WRITE(GUEST_CR0, &(p->cr0));
- CHK_VMCS_WRITE(GUEST_CR3, &(p->cr3));
- CHK_VMCS_WRITE(GUEST_CR4, &(p->cr4));
- CHK_VMCS_WRITE(GUEST_DR7, &(p->dr7));
- CHK_VMCS_WRITE(GUEST_RSP, &(p->rsp));
- CHK_VMCS_WRITE(GUEST_RIP, &(p->rip));
- CHK_VMCS_WRITE(GUEST_RFLAGS, &(p->rflags));
- CHK_VMCS_WRITE(VMCS_GUEST_CS_SELECTOR, &(p->cs.selector));
- CHK_VMCS_WRITE(VMCS_GUEST_SS_SELECTOR, &(p->ss.selector));
- CHK_VMCS_WRITE(VMCS_GUEST_DS_SELECTOR, &(p->ds.selector));
- CHK_VMCS_WRITE(VMCS_GUEST_ES_SELECTOR, &(p->es.selector));
- CHK_VMCS_WRITE(VMCS_GUEST_FS_SELECTOR, &(p->fs.selector));
- CHK_VMCS_WRITE(VMCS_GUEST_GS_SELECTOR, &(p->gs.selector));
- CHK_VMCS_WRITE(VMCS_GUEST_LDTR_SELECTOR, &(p->ldtr.selector));
- CHK_VMCS_WRITE(VMCS_GUEST_TR_SELECTOR, &(p->tr.selector));
- CHK_VMCS_WRITE(GUEST_CS_BASE, &(p->cs.baseAddr));
- CHK_VMCS_WRITE(GUEST_SS_BASE, &(p->ss.baseAddr));
- CHK_VMCS_WRITE(GUEST_DS_BASE, &(p->ds.baseAddr));
- CHK_VMCS_WRITE(GUEST_ES_BASE, &(p->es.baseAddr));
- CHK_VMCS_WRITE(GUEST_FS_BASE, &(p->fs.baseAddr));
- CHK_VMCS_WRITE(GUEST_GS_BASE, &(p->gs.baseAddr));
- CHK_VMCS_WRITE(GUEST_LDTR_BASE, &(p->ldtr.baseAddr));
- CHK_VMCS_WRITE(GUEST_TR_BASE, &(p->tr.baseAddr));
- CHK_VMCS_WRITE(GUEST_CS_LIMIT, &(p->cs.limit));
- CHK_VMCS_WRITE(GUEST_SS_LIMIT, &(p->ss.limit));
- CHK_VMCS_WRITE(GUEST_DS_LIMIT, &(p->ds.limit));
- CHK_VMCS_WRITE(GUEST_ES_LIMIT, &(p->es.limit));
- CHK_VMCS_WRITE(GUEST_FS_LIMIT, &(p->fs.limit));
- CHK_VMCS_WRITE(GUEST_GS_LIMIT, &(p->gs.limit));
- CHK_VMCS_WRITE(GUEST_LDTR_LIMIT, &(p->ldtr.limit));
- CHK_VMCS_WRITE(GUEST_TR_LIMIT, &(p->tr.limit));
- CHK_VMCS_WRITE(GUEST_CS_ACCESS, &(p->cs.access));
- CHK_VMCS_WRITE(GUEST_SS_ACCESS, &(p->ss.access));
- CHK_VMCS_WRITE(GUEST_DS_ACCESS, &(p->ds.access));
- CHK_VMCS_WRITE(GUEST_ES_ACCESS, &(p->es.access));
- CHK_VMCS_WRITE(GUEST_FS_ACCESS, &(p->fs.access));
- CHK_VMCS_WRITE(GUEST_GS_ACCESS, &(p->gs.access));
- CHK_VMCS_WRITE(GUEST_LDTR_ACCESS, &(p->ldtr.access));
- CHK_VMCS_WRITE(GUEST_TR_ACCESS, &(p->tr.access));
- CHK_VMCS_WRITE(GUEST_GDTR_BASE, &(p->gdtr.baseAddr));
- CHK_VMCS_WRITE(GUEST_IDTR_BASE, &(p->idtr.baseAddr));
- CHK_VMCS_WRITE(GUEST_GDTR_LIMIT, &(p->gdtr.limit));
- CHK_VMCS_WRITE(GUEST_IDTR_LIMIT, &(p->idtr.limit));
- CHK_VMCS_WRITE(GUEST_IA32_DEBUGCTL, &(p->dbg_ctrl));
- CHK_VMCS_WRITE(GUEST_IA32_DEBUGCTL_HIGH, ((char *)&(p->dbg_ctrl)) + 4);
- CHK_VMCS_WRITE(GUEST_IA32_SYSENTER_CS, &(p->sysenter_cs));
- CHK_VMCS_WRITE(GUEST_IA32_SYSENTER_ESP, &(p->sysenter_esp));
- CHK_VMCS_WRITE(GUEST_IA32_SYSENTER_EIP, &(p->sysenter_eip));
- CHK_VMCS_WRITE(GUEST_SMBASE, &(p->smbase));
-
- CHK_VMCS_WRITE(GUEST_ACTIVITY_STATE, &(p->activity));
- CHK_VMCS_WRITE(GUEST_INT_STATE, &(p->interrupt_state));
- CHK_VMCS_WRITE(GUEST_PENDING_DEBUG_EXCS, &(p->pending_dbg_exceptions));
- CHK_VMCS_WRITE(VMCS_LINK_PTR, &(p->vmcs_link));
- CHK_VMCS_WRITE(VMCS_LINK_PTR_HIGH, ((char *)&(p->vmcs_link)) + 4);
- return 0;
+static inline void print_vmcs_segments() {
+ // see vm_guest.c
}
-int CopyOutVMCSHostStateArea(struct VMCSHostStateArea *p) {
- CHK_VMCS_READ(HOST_CR0, &(p->cr0));
- CHK_VMCS_READ(HOST_CR3, &(p->cr3));
- CHK_VMCS_READ(HOST_CR4, &(p->cr4));
- CHK_VMCS_READ(HOST_RSP, &(p->rsp));
- CHK_VMCS_READ(HOST_RIP, &(p->rip));
- CHK_VMCS_READ(VMCS_HOST_CS_SELECTOR, &(p->csSelector));
- CHK_VMCS_READ(VMCS_HOST_SS_SELECTOR, &(p->ssSelector));
- CHK_VMCS_READ(VMCS_HOST_DS_SELECTOR, &(p->dsSelector));
- CHK_VMCS_READ(VMCS_HOST_ES_SELECTOR, &(p->esSelector));
- CHK_VMCS_READ(VMCS_HOST_FS_SELECTOR, &(p->fsSelector));
- CHK_VMCS_READ(VMCS_HOST_GS_SELECTOR, &(p->gsSelector));
- CHK_VMCS_READ(VMCS_HOST_TR_SELECTOR, &(p->trSelector));
- CHK_VMCS_READ(HOST_FS_BASE, &(p->fsBaseAddr));
- CHK_VMCS_READ(HOST_GS_BASE, &(p->gsBaseAddr));
- CHK_VMCS_READ(HOST_TR_BASE, &(p->trBaseAddr));
- CHK_VMCS_READ(HOST_GDTR_BASE, &(p->gdtrBaseAddr));
- CHK_VMCS_READ(HOST_IDTR_BASE, &(p->idtrBaseAddr));
- CHK_VMCS_READ(HOST_IA32_SYSENTER_CS, &(p->sysenter_cs));
- CHK_VMCS_READ(HOST_IA32_SYSENTER_ESP, &(p->sysenter_esp));
- CHK_VMCS_READ(HOST_IA32_SYSENTER_EIP, &(p->sysenter_eip));
- return 0;
-}
-
-
-
-int CopyInVMCSHostStateArea(struct VMCSHostStateArea *p) {
- CHK_VMCS_WRITE(HOST_CR0, &(p->cr0));
- CHK_VMCS_WRITE(HOST_CR3, &(p->cr3));
- CHK_VMCS_WRITE(HOST_CR4, &(p->cr4));
- CHK_VMCS_WRITE(HOST_RSP, &(p->rsp));
- CHK_VMCS_WRITE(HOST_RIP, &(p->rip));
- CHK_VMCS_WRITE(VMCS_HOST_CS_SELECTOR, &(p->csSelector));
- CHK_VMCS_WRITE(VMCS_HOST_SS_SELECTOR, &(p->ssSelector));
- CHK_VMCS_WRITE(VMCS_HOST_DS_SELECTOR, &(p->dsSelector));
- CHK_VMCS_WRITE(VMCS_HOST_ES_SELECTOR, &(p->esSelector));
- CHK_VMCS_WRITE(VMCS_HOST_FS_SELECTOR, &(p->fsSelector));
- CHK_VMCS_WRITE(VMCS_HOST_GS_SELECTOR, &(p->gsSelector));
- CHK_VMCS_WRITE(VMCS_HOST_TR_SELECTOR, &(p->trSelector));
- CHK_VMCS_WRITE(HOST_FS_BASE, &(p->fsBaseAddr));
- CHK_VMCS_WRITE(HOST_GS_BASE, &(p->gsBaseAddr));
- CHK_VMCS_WRITE(HOST_TR_BASE, &(p->trBaseAddr));
- CHK_VMCS_WRITE(HOST_GDTR_BASE, &(p->gdtrBaseAddr));
- CHK_VMCS_WRITE(HOST_IDTR_BASE, &(p->idtrBaseAddr));
- CHK_VMCS_WRITE(HOST_IA32_SYSENTER_CS, &(p->sysenter_cs));
- CHK_VMCS_WRITE(HOST_IA32_SYSENTER_ESP, &(p->sysenter_esp));
- CHK_VMCS_WRITE(HOST_IA32_SYSENTER_EIP, &(p->sysenter_eip));
- return 0;
-}
-
-int CopyOutVMCSExitCtrlFields(struct VMCSExitCtrlFields *p)
-{
- CHK_VMCS_READ(VM_EXIT_CTRLS,&(p->exitCtrls));
- CHK_VMCS_READ(VM_EXIT_MSR_STORE_COUNT,&(p->msrStoreCount));
- CHK_VMCS_READ(VM_EXIT_MSR_STORE_ADDR,&(p->msrStoreAddr));
- CHK_VMCS_READ(VM_EXIT_MSR_LOAD_COUNT,&(p->msrLoadCount));
- CHK_VMCS_READ(VM_EXIT_MSR_LOAD_ADDR,&(p->msrLoadAddr));
- return 0;
-}
-
-int CopyInVMCSExitCtrlFields(struct VMCSExitCtrlFields *p)
-{
- CHK_VMCS_WRITE(VM_EXIT_CTRLS,&(p->exitCtrls));
- CHK_VMCS_WRITE(VM_EXIT_MSR_STORE_COUNT,&(p->msrStoreCount));
- CHK_VMCS_WRITE(VM_EXIT_MSR_STORE_ADDR,&(p->msrStoreAddr));
- CHK_VMCS_WRITE(VM_EXIT_MSR_LOAD_COUNT,&(p->msrLoadCount));
- CHK_VMCS_WRITE(VM_EXIT_MSR_LOAD_ADDR,&(p->msrLoadAddr));
- return 0;
-}
-
-
-int CopyOutVMCSEntryCtrlFields(struct VMCSEntryCtrlFields *p)
-{
- CHK_VMCS_READ(VM_ENTRY_CTRLS,&(p->entryCtrls));
- CHK_VMCS_READ(VM_ENTRY_MSR_LOAD_COUNT,&(p->msrLoadCount));
- CHK_VMCS_READ(VM_ENTRY_MSR_LOAD_ADDR,&(p->msrLoadAddr));
- CHK_VMCS_READ(VM_ENTRY_INT_INFO_FIELD,&(p->intInfo));
- CHK_VMCS_READ(VM_ENTRY_EXCEPTION_ERROR,&(p->exceptionErrorCode));
- CHK_VMCS_READ(VM_ENTRY_INSTR_LENGTH,&(p->instrLength));
- return 0;
-}
-int CopyInVMCSEntryCtrlFields(struct VMCSEntryCtrlFields *p)
-{
- CHK_VMCS_WRITE(VM_ENTRY_CTRLS,&(p->entryCtrls));
- CHK_VMCS_WRITE(VM_ENTRY_MSR_LOAD_COUNT,&(p->msrLoadCount));
- CHK_VMCS_WRITE(VM_ENTRY_MSR_LOAD_ADDR,&(p->msrLoadAddr));
- CHK_VMCS_WRITE(VM_ENTRY_INT_INFO_FIELD,&(p->intInfo));
- CHK_VMCS_WRITE(VM_ENTRY_EXCEPTION_ERROR,&(p->exceptionErrorCode));
- CHK_VMCS_WRITE(VM_ENTRY_INSTR_LENGTH,&(p->instrLength));
- return 0;
-}
+void print_debug_vmcs_load_guest() {
+ const int wordsize = sizeof(addr_t);
+ uint64_t temp;
+ vmcs_segment tmp_seg;
-int CopyOutVMCSExitInfoFields(struct VMCSExitInfoFields *p) {
- CHK_VMCS_READ(EXIT_REASON,&(p->reason));
- CHK_VMCS_READ(EXIT_QUALIFICATION,&(p->qualification));
- CHK_VMCS_READ(VM_EXIT_INT_INFO,&(p->intInfo));
- CHK_VMCS_READ(VM_EXIT_INT_ERROR,&(p->intErrorCode));
- CHK_VMCS_READ(IDT_VECTOR_INFO,&(p->idtVectorInfo));
- CHK_VMCS_READ(IDT_VECTOR_ERROR,&(p->idtVectorErrorCode));
- CHK_VMCS_READ(VM_EXIT_INSTR_LENGTH,&(p->instrLength));
- CHK_VMCS_READ(GUEST_LINEAR_ADDR,&(p->guestLinearAddr));
- CHK_VMCS_READ(VMX_INSTR_INFO,&(p->instrInfo));
- CHK_VMCS_READ(IO_RCX,&(p->ioRCX));
- CHK_VMCS_READ(IO_RSI,&(p->ioRSI));
- CHK_VMCS_READ(IO_RDI,&(p->ioRDI));
- CHK_VMCS_READ(IO_RIP,&(p->ioRIP));
- CHK_VMCS_READ(VM_INSTR_ERROR,&(p->instrErrorField));
- return 0;
-}
+ PrintDebug("\n====== Loading Guest State ======\n");
+ PRINT_VMREAD("Guest CR0: %x\n", GUEST_CR0, wordsize);
+ PRINT_VMREAD("Guest CR3: %x\n", GUEST_CR3, wordsize);
+ PRINT_VMREAD("Guest CR4: %x\n", GUEST_CR4, wordsize);
+ PRINT_VMREAD("Guest DR7: %x\n", GUEST_DR7, wordsize);
+ READ_VMCS_SEG(&tmp_seg,CS,wordsize);
+ print_vmcs_segment("CS", &tmp_seg);
+
+ READ_VMCS_SEG(&tmp_seg,SS,wordsize);
+ print_vmcs_segment("SS", &tmp_seg);
-int CopyOutVMCSExecCtrlFields(struct VMCSExecCtrlFields *p)
-{
- CHK_VMCS_READ(PIN_VM_EXEC_CTRLS,&(p->pinCtrls));
- CHK_VMCS_READ(PROC_VM_EXEC_CTRLS,&(p->procCtrls));
- CHK_VMCS_READ(EXCEPTION_BITMAP,&(p->execBitmap));
- CHK_VMCS_READ(PAGE_FAULT_ERROR_MASK,&(p->pageFaultErrorMask));
- CHK_VMCS_READ(PAGE_FAULT_ERROR_MATCH,&(p->pageFaultErrorMatch));
- CHK_VMCS_READ(IO_BITMAP_A_ADDR,&(p->ioBitmapA));
- CHK_VMCS_READ(IO_BITMAP_B_ADDR,&(p->ioBitmapB));
- CHK_VMCS_READ(TSC_OFFSET,&(p->tscOffset));
- CHK_VMCS_READ(CR0_GUEST_HOST_MASK,&(p->cr0GuestHostMask));
- CHK_VMCS_READ(CR0_READ_SHADOW,&(p->cr0ReadShadow));
- CHK_VMCS_READ(CR4_GUEST_HOST_MASK,&(p->cr4GuestHostMask));
- CHK_VMCS_READ(CR4_READ_SHADOW,&(p->cr4ReadShadow));
- CHK_VMCS_READ(CR3_TARGET_COUNT, &(p->cr3TargetCount));
- CHK_VMCS_READ(CR3_TARGET_VALUE_0, &(p->cr3TargetValue0));
- CHK_VMCS_READ(CR3_TARGET_VALUE_1, &(p->cr3TargetValue1));
- CHK_VMCS_READ(CR3_TARGET_VALUE_2, &(p->cr3TargetValue2));
- CHK_VMCS_READ(CR3_TARGET_VALUE_3, &(p->cr3TargetValue3));
- CHK_VMCS_READ(VIRT_APIC_PAGE_ADDR, &(p->virtApicPageAddr));
- CHK_VMCS_READ(TPR_THRESHOLD, &(p->tprThreshold));
- CHK_VMCS_READ(MSR_BITMAPS, &(p->MSRBitmapsBaseAddr));
- CHK_VMCS_READ(VMCS_EXEC_PTR,&(p->vmcsExecPtr));
- return 0;
-}
+ READ_VMCS_SEG(&tmp,DS,wordsize);
+ print_vmcs_segment("DS", &tmp_seg);
+ READ_VMCS_SEG(&tmp_seg,ES,wordsize);
+ print_vmcs_segment("ES", &tmp_seg);
-int CopyInVMCSExecCtrlFields(struct VMCSExecCtrlFields *p)
-{
- CHK_VMCS_WRITE(PIN_VM_EXEC_CTRLS,&(p->pinCtrls));
- CHK_VMCS_WRITE(PROC_VM_EXEC_CTRLS,&(p->procCtrls));
- CHK_VMCS_WRITE(EXCEPTION_BITMAP,&(p->execBitmap));
- CHK_VMCS_WRITE(PAGE_FAULT_ERROR_MASK,&(p->pageFaultErrorMask));
- CHK_VMCS_WRITE(PAGE_FAULT_ERROR_MATCH,&(p->pageFaultErrorMatch));
- CHK_VMCS_WRITE(IO_BITMAP_A_ADDR,&(p->ioBitmapA));
- CHK_VMCS_WRITE(IO_BITMAP_B_ADDR,&(p->ioBitmapB));
- CHK_VMCS_WRITE(TSC_OFFSET,&(p->tscOffset));
- CHK_VMCS_WRITE(CR0_GUEST_HOST_MASK,&(p->cr0GuestHostMask));
- CHK_VMCS_WRITE(CR0_READ_SHADOW,&(p->cr0ReadShadow));
- CHK_VMCS_WRITE(CR4_GUEST_HOST_MASK,&(p->cr4GuestHostMask));
- CHK_VMCS_WRITE(CR4_READ_SHADOW,&(p->cr4ReadShadow));
- CHK_VMCS_WRITE(CR3_TARGET_COUNT, &(p->cr3TargetCount));
- CHK_VMCS_WRITE(CR3_TARGET_VALUE_0, &(p->cr3TargetValue0));
- CHK_VMCS_WRITE(CR3_TARGET_VALUE_1, &(p->cr3TargetValue1));
- CHK_VMCS_WRITE(CR3_TARGET_VALUE_2, &(p->cr3TargetValue2));
- CHK_VMCS_WRITE(CR3_TARGET_VALUE_3, &(p->cr3TargetValue3));
- CHK_VMCS_WRITE(VIRT_APIC_PAGE_ADDR, &(p->virtApicPageAddr));
- CHK_VMCS_WRITE(TPR_THRESHOLD, &(p->tprThreshold));
- CHK_VMCS_WRITE(MSR_BITMAPS, &(p->MSRBitmapsBaseAddr));
- CHK_VMCS_WRITE(VMCS_EXEC_PTR,&(p->vmcsExecPtr));
- return 0;
-}
+ READ_VMCS_SEG(&tmp_seg,FS,wordsize);
+ print_vmcs_segment("FS", &tmp_seg);
+ READ_VMCS_SEG(&tmp_seg,GS,wordsize);
+ print_vmcs_segment("GS", &tmp_seg);
-int CopyOutVMCSData(struct VMCSData *p) {
- if (CopyOutVMCSGuestStateArea(&(p->guestStateArea)) != 0) {
- return -1;
- }
- if (CopyOutVMCSHostStateArea(&(p->hostStateArea)) != 0) {
- return -1;
- }
- if (CopyOutVMCSExecCtrlFields(&(p->execCtrlFields)) != 0) {
- return -1;
- }
- if (CopyOutVMCSExitCtrlFields(&(p->exitCtrlFields)) != 0) {
- return -1;
- }
- if (CopyOutVMCSEntryCtrlFields(&(p->entryCtrlFields)) != 0) {
- return -1;
- }
- if (CopyOutVMCSExitInfoFields(&(p->exitInfoFields)) != 0) {
- return -1;
- }
- return 0;
-}
+ READ_VMCS_SEG(&tmp_seg,TR,wordsize);
+ print_vmcs_segment("TR", &tmp_seg);
+ READ_VMCS_SEG(&tmp_seg,LDTR,wordsize);
+ print_vmcs_segment("LDTR", &tmp_seg);
+
+ PrintDebug("\n==GDTR==\n");
+ PRINT_VMREAD("GDTR Base: %x\n", GUEST_GDTR_BASE, wordsize);
+ PRINT_VMREAD("GDTR Limit: %x\n", GUEST_GDTR_LIMIT, 32);
+ PrintDebug("====\n");
-int CopyInVMCSData(struct VMCSData *p) {
- if (CopyInVMCSGuestStateArea(&(p->guestStateArea)) != 0) {
- return -1;
- }
- if (CopyInVMCSHostStateArea(&(p->hostStateArea)) != 0) {
- return -1;
- }
- if (CopyInVMCSExecCtrlFields(&(p->execCtrlFields)) != 0) {
- return -1;
- }
- if (CopyInVMCSExitCtrlFields(&(p->exitCtrlFields)) != 0) {
- return -1;
- }
- if (CopyInVMCSEntryCtrlFields(&(p->entryCtrlFields)) != 0) {
- return -1;
- }
- return 0;
-}
+ PrintDebug("\n==LDTR==\n");
+ PRINT_VMREAD("LDTR Base: %x\n", GUEST_LDTR_BASE, wordsize);
+ PRINT_VMREAD("LDTR Limit: %x\n", GUEST_LDTR_LIMIT, 32);
+ PrintDebug("=====\n");
+ PRINT_VMREAD("Guest RSP: %x\n", GUEST_RSP, wordsize);
+ PRINT_VMREAD("Guest RIP: %x\n", GUEST_RIP, wordsize);
+ PRINT_VMREAD("Guest RFLAGS: %x\n", GUEST_RFLAGS, wordsize);
+ PRINT_VMREAD("Guest Activity state: %x\n", GUEST_ACTIVITY_STATE, 32);
+ PRINT_VMREAD("Guest Interruptibility state: %x\n", GUEST_INT_STATE, 32);
+ PRINT_VMREAD("Guest pending debug: %x\n", GUEST_PENDING_DEBUG_EXCS, wordsize);
-void PrintTrace_VMX_Regs(struct VMXRegs * regs) {
- PrintTrace("==>VMX Register values:\n");
- PrintTrace("EAX: %x\n", regs->eax);
- PrintTrace("ECX: %x\n", regs->ecx);
- PrintTrace("EDX: %x\n", regs->edx);
- PrintTrace("EBX: %x\n", regs->ebx);
- PrintTrace("ESP: %x\n", regs->esp);
- PrintTrace("EBP: %x\n", regs->ebp);
- PrintTrace("ESI: %x\n", regs->esi);
- PrintTrace("EDI: %x\n", regs->edi);
- PrintTrace("\n");
+ PRINT_VMREAD("IA32_DEBUGCTL: %x\n", GUEST_IA32_DEBUGCTL, 64);
+ PRINT_VMREAD("IA32_SYSENTER_CS: %x\n", GUEST_IA32_SYSENTER_CS, 32);
+ PRINT_VMREAD("IA32_SYSTENTER_ESP: %x\n", GUEST_IA32_SYSENTER_ESP, wordsize);
+ PRINT_VMREAD("IA32_SYSTENTER_EIP: %x\n", GUEST_IA32_SYSENTER_EIP, wordsize);
+ PRINT_VMREAD("IA32_PERF_GLOBAL_CTRL: %x\n", GUEST_IA32_PERF_GLOBAL_CTRL, wordsize);
+ PRINT_VMREAD("VMCS Link Ptr: %x\n", VMCS_LINK_PTR, 64);
+ // TODO: Maybe add VMX preemption timer and PDTE (Intel 20-8 Vol. 3b)
}
-
-void PrintTrace_VMCSSegment(char * segname, struct VMCSSegment * seg, int abbr) {
- PrintTrace("Segment: %s\n", segname);
- if (abbr == 0) {
- PrintTrace("\tSelector: %x\n", (uint_t)seg->selector);
- PrintTrace("\tAccess: %x\n", *(uint_t*)&(seg->access));
- }
- PrintTrace("\tBase Addr: %x\n", (uint_t)seg->baseAddr);
- PrintTrace("\tLimit: %x\n", (uint_t)seg->limit);
-
+void print_debug_load_host() {
+ const int wordsize = sizeof(addr_t);
+ uint64_t temp;
+ vmcs_segment tmp_seg;
+
+ PrintDebug("\n====== Host State ========\n");
+ PRINT_VMREAD("Host CR0: %x\n", HOST_CR0, wordsize);
+ PRINT_VMREAD("Host CR3: %x\n", HOST_CR3, wordsize);
+ PRINT_VMREAD("Host CR4: %x\n", HOST_CR4, wordsize);
+ PRINT_VMREAD("Host RSP: %x\n", HOST_RSP, wordsize);
+ PRINT_VMREAD("Host RIP: %x\n", HOST_RIP, wordsize);
+ PRINT_VMREAD("IA32_SYSENTER_CS: %x\n", HOST_IA32_SYSENTER_CS, 32);
+ PRINT_VMREAD("IA32_SYSENTER_ESP: %x\n", HOST_IA32_SYSENTER_ESP, wordsize);
+ PRINT_VMREAD("IA32_SYSENTER_EIP: %x\n", HOST_IA32_SYSENTER_EIP, wordsize);
+
+ PRINT_VMREAD("Host CS Selector: %x\n", HOST_CS_SELECTOR, 16);
+ PRINT_VMREAD("Host SS Selector: %x\n", HOST_SS_SELECTOR, 16);
+ PRINT_VMREAD("Host DS Selector: %x\n", HOST_DS_SELECTOR, 16);
+ PRINT_VMREAD("Host ES Selector: %x\n", HOST_ES_SELECTOR, 16);
+ PRINT_VMREAD("Host FS Selector: %x\n", HOST_FS_SELECTOR, 16);
+ PRINT_VMREAD("Host GS Selector: %x\n", HOST_GS_SELECTOR, 16);
+ PRINT_VMREAD("Host TR Selector: %x\n", HOST_TR_SELECTOR, 16);
+
+ PRINT_VMREAD("Host FS Base: %x\n", HOST_FS_BASE, wordsize);
+ PRINT_VMREAD("Host GS Base: %x\n", HOST_GS_BASE, wordsize);
+ PRINT_VMREAD("Host TR Base: %x\n", HOST_TR_BASE, wordsize);
+ PRINT_VMREAD("Host GDTR Base: %x\n", HOST_GDTR_BASE, wordsize);
+ PRINT_VMREAD("Host IDTR Base: %x\n", HOSE_IDTR_BASE, wordsize);
}
-
-void PrintTrace_VMCSGuestStateArea(struct VMCSGuestStateArea * guestState) {
- PrintTrace("==>Guest State Area\n");
- PrintTrace("==>==> Guest Register State\n");
- PrintTrace("GUEST_CR0: %x\n",(uint_t) guestState->cr0);
- PrintTrace("GUEST_CR3: %x\n",(uint_t)guestState->cr3);
- PrintTrace("GUEST_CR4: %x\n",(uint_t)guestState->cr4);
- PrintTrace("GUEST_DR7: %x\n",(uint_t)guestState->dr7);
- PrintTrace("GUEST_RSP: %x\n",(uint_t)guestState->rsp);
- PrintTrace("GUEST_RIP: %x\n",(uint_t)guestState->rip);
- PrintTrace("GUEST_RFLAGS: %x\n",(uint_t)guestState->rflags);
-
- PrintTrace_VMCSSegment("Guest CS", &(guestState->cs), 0);
- PrintTrace_VMCSSegment("Guest SS", &(guestState->ss), 0);
- PrintTrace_VMCSSegment("Guest DS",&(guestState->ds), 0);
- PrintTrace_VMCSSegment("Guest ES", &(guestState->es), 0);
- PrintTrace_VMCSSegment("Guest FS", &(guestState->fs), 0);
- PrintTrace_VMCSSegment("Guest GS", &(guestState->gs), 0);
- PrintTrace_VMCSSegment("Guest LDTR", &(guestState->ldtr), 0);
- PrintTrace_VMCSSegment("Guest TR", &(guestState->tr), 0);
- PrintTrace_VMCSSegment("Guest GDTR", &(guestState->gdtr), 1);
- PrintTrace_VMCSSegment("Guest IDTR", &(guestState->idtr), 1);
-
-
- PrintTrace("GUEST_IA32_DEBUGCTL: %x\n",(uint_t)(guestState->dbg_ctrl & 0xffffffff));
- PrintTrace("GUEST_IA32_DEBUGCTL_HIGH: %x\n",(uint_t)(guestState->dbg_ctrl >> 32) & 0xffffffff);
- PrintTrace("GUEST_IA32_SYSENTER_CS: %x\n",guestState->sysenter_cs);
- PrintTrace("GUEST_IA32_SYSENTER_ESP: %x\n",(uint_t)guestState->sysenter_esp);
- PrintTrace("GUEST_IA32_SYSENTER_EIP: %x\n",(uint_t)guestState->sysenter_eip);
- PrintTrace("GUEST_SMBASE: %x\n", (uint_t)guestState->smbase);
-
- PrintTrace("==>==> Guest Non-Register State\n");
- PrintTrace("GUEST_ACTIVITY_STATE: %x\n", (uint_t)guestState->activity);
- PrintTrace("GUEST_INT_STATE: %x\n", (uint_t)guestState->interrupt_state);
- PrintTrace("GUEST_PENDING_DEBUG_EXCS: %x\n", (uint_t)guestState->pending_dbg_exceptions);
- PrintTrace("VMCS_LINK_PTR: %x\n", (uint_t)guestState->vmcs_link & 0xffffffff);
- PrintTrace("VMCS_LINK_PTR_HIGH: %x\n", (uint_t)(guestState->vmcs_link >> 32) & 0xffffffff);
+void print_vmcs_segment(char * name, vmcs_segment* seg)
+{
+ PrintDebug("\n==VMCS %s Segment==\n",name);
+ PrintDebug("\tSelector: %x\n", seg->selector);
+ PrintDebug("\tBase Address: %x\n", seg->baseAddr);
+ PrintDebug("\tLimit: %x\n", seg->limit);
+ PrintDebug("\tAccess: %x\n", seg->access);
}
-
void PrintTrace_VMCSHostStateArea(struct VMCSHostStateArea * hostState) {
PrintTrace("\n==> Host State Area\n");
PrintTrace("HOST_CR0: %x\n", (uint_t)hostState->cr0);
#include <palacios/vmx.h>
#include <palacios/vmcs.h>
#include <palacios/vmm.h>
+#include <palacios/vmx_lowlevel.h>
//
#include <palacios/vmm_util.h>
#include <palacios/vmm_string.h>
+#include <palacios/vmm_ctrl_regs.h>
#define ROMBIOS_DEBUG_PORT 0x403
-static struct VM theVM;
static uint_t GetLinearIP(struct VM * vm) {
if (vm->state == VM_VMXASSIST_V8086_BIOS || vm->state == VM_VMXASSIST_V8086) {
-extern uint_t VMCS_CLEAR();
-extern uint_t VMCS_LOAD();
-extern uint_t VMCS_STORE();
extern uint_t VMCS_LAUNCH();
-extern uint_t VMCS_RESUME();
extern uint_t Init_VMCS_HostState();
extern uint_t Init_VMCS_GuestState();
}
-static void V8086ModeSegmentRegisterFixup(struct VM *vm)
+static void setup_v8086_mode_for_boot(struct guest_info* vm_info)
{
- vm->vmcs.guestStateArea.cs.baseAddr = vm->vmcs.guestStateArea.cs.selector << 4;
- vm->vmcs.guestStateArea.es.baseAddr = vm->vmcs.guestStateArea.es.selector << 4;
- vm->vmcs.guestStateArea.ss.baseAddr = vm->vmcs.guestStateArea.ss.selector << 4;
- vm->vmcs.guestStateArea.ds.baseAddr = vm->vmcs.guestStateArea.ds.selector << 4;
- vm->vmcs.guestStateArea.fs.baseAddr = vm->vmcs.guestStateArea.fs.selector << 4;
- vm->vmcs.guestStateArea.gs.baseAddr = vm->vmcs.guestStateArea.gs.selector << 4;
-}
-
-static void SetupV8086ModeForBoot(struct VM *vm)
-{
- vm->state = VM_VMXASSIST_V8086_BIOS;
-
- // Put guest into V8086 mode on return
- vm->vmcs.guestStateArea.rflags |= EFLAGS_VM | EFLAGS_IOPL_HI | EFLAGS_IOPL_LO ;
-
- // We will start at f000:fff0 on return
- //
- // We want this to look as much as possible as a processor
- // reset
- vm->vmcs.guestStateArea.rip = 0xfff0; // note, 16 bit rip
- vm->vmcs.guestStateArea.cs.selector = 0xf000;
- vm->vmcs.guestStateArea.cs.limit = 0xffff;
- vm->vmcs.guestStateArea.cs.access.as_dword = 0xf3;
-
- vm->vmcs.guestStateArea.ss.selector = 0x0000;
- vm->vmcs.guestStateArea.ss.limit = 0xffff;
- vm->vmcs.guestStateArea.ss.access.as_dword = 0xf3;
-
- vm->vmcs.guestStateArea.ds.selector = 0x0000;
- vm->vmcs.guestStateArea.ds.limit = 0xffff;
- vm->vmcs.guestStateArea.ds.access.as_dword = 0xf3;
-
- vm->vmcs.guestStateArea.es.selector = 0x0000;
- vm->vmcs.guestStateArea.es.limit = 0xffff;
- vm->vmcs.guestStateArea.es.access.as_dword = 0xf3;
-
- vm->vmcs.guestStateArea.fs.selector = 0x0000;
- vm->vmcs.guestStateArea.fs.limit = 0xffff;
- vm->vmcs.guestStateArea.fs.access.as_dword = 0xf3;
-
- vm->vmcs.guestStateArea.gs.selector = 0x0000;
- vm->vmcs.guestStateArea.gs.limit = 0xffff;
- vm->vmcs.guestStateArea.gs.access.as_dword = 0xf3;
-
- V8086ModeSegmentRegisterFixup(vm);
-
- PrintTrace_VMCSData(&(vm->vmcs));
+ ((struct vmx_data*)vm_info->vmm_data)->state = VMXASSIST_V8086_BIOS;
+ ((struct rflags)info->ctrl_regs.rflags).vm = 1;
+ ((struct rflags)info->ctrl_regs.rflags).iopl = 3;
+
+
+ vm_info->rip = 0xfff0;
+
+ vm_info->segments.cs.selector = 0xf000;
+ vm_info->segments.cs.base = 0xf000<<4;
+ vm_info->segments.cs.limit = 0xffff;
+ vm_info->segments.cs.type = 3;
+ vm_info->segments.cs.system = 1;
+ vm_info->segments.cs.dpl = 3;
+ vm_info->segments.cs.present = 1;
+ vm_info->segments.cs.granularity = 0;
+
+ vm_info->segments.ss.selector = 0x0000;
+ vm_info->segments.ss.base = 0x0000<<4;
+ vm_info->segments.ss.limit = 0xffff;
+ vm_info->segments.ss.type = 3;
+ vm_info->segments.ss.system = 1;
+ vm_info->segments.ss.dpl = 3;
+ vm_info->segments.ss.present = 1;
+ vm_info->segments.ss.granularity = 0;
+
+ vm_info->segments.es.selector = 0x0000;
+ vm_info->segments.es.base = 0x0000<<4;
+ vm_info->segments.es.limit = 0xffff;
+ vm_info->segments.es.type = 3;
+ vm_info->segments.es.system = 1;
+ vm_info->segments.es.dpl = 3;
+ vm_info->segments.es.present = 1;
+ vm_info->segments.es.granularity = 0;
+
+ vm_info->segments.fs.selector = 0x0000;
+ vm_info->segments.fs.base = 0x0000<<4;
+ vm_info->segments.fs.limit = 0xffff;
+ vm_info->segments.fs.type = 3;
+ vm_info->segments.fs.system = 1;
+ vm_info->segments.fs.dpl = 3;
+ vm_info->segments.fs.present = 1;
+ vm_info->segments.fs.granularity = 0;
+
+ vm_info->segments.gs.selector = 0x0000;
+ vm_info->segments.gs.base = 0x0000<<4;
+ vm_info->segments.gs.limit = 0xffff;
+ vm_info->segments.gs.type = 3;
+ vm_info->segments.gs.system = 1;
+ vm_info->segments.gs.dpl = 3;
+ vm_info->segments.gs.present = 1;
+ vm_info->segments.gs.granularity = 0;
}
-
-
-
static void ConfigureExits(struct VM *vm)
{
-static vmcs_t * allocate_vmcs() {
+static vmcs_data* allocate_vmcs() {
reg_ex_t msr;
- vmcs_t * vmcs_page = (vmcs_t *)V3_VAddr(V3_AllocPages(1));
+ vmcs_data* vmcs_page = (vmcs_data*)V3_VAddr(V3_AllocPages(1));
- memset(vmcb_page, 0, 4096);
+ memset(vmcs_page, 0, 4096);
v3_get_msr(VMX_BASIC_MSR, &(msr.e_reg.high), &(msr.e_reg.low));
- *(uint32_t *)vmcs_page = ((struct vmx_basic_msr *)msr.val)->revision;
+ vmcs_page->revision = ((struct vmx_basic_msr)msr).revision;
return vmcs_page;
}
static int init_vmx_guest(struct guest_info * info, struct v3_vm_config * config_ptr) {
v3_pre_config_guest(info, config_ptr);
+ struct vmx_data* data;
+
+ PrintDebug("Allocating vmx_data\n");
+ data = (struct vmx_data*)V3_Malloc(sizeof(vmx_data));
PrintDebug("Allocating VMCS\n");
- info->vmm_data = (void *)allocate_vmcs();
+ data->vmcs = allocate_vmcs();
+
+ info->vmm_data = (void*)data;
PrintDebug("Initializing VMCS (addr=%p)\n", (void *)info->vmm_data);
init_vmcs_bios((vmcs_t *)(info->vmm_data), info);