Page 1 of 1

Booting OS as a VMM

Posted: Fri Aug 06, 2010 12:03 pm
by MJ
HI,

Hoping someone can help. I am working on getting an OS booted in VMM mode on an intel processor. I am using OWC 17 as the compiler. So far I have paging enabled and working. (32 bit mode with segmentation enabled). I checked the cpuid for the processor I am using and it does support VMX. I allocate a region in memory equal to size shown in IA32_VMX_BASIC_MSR (bits44:32) = 800 so I set it up as 2048 = 2k region size. I set the VMCS revision Identifier as shown in bits 31:0 of this msr = 13 (on my test processor). It is a 4k aligned region. My vmxon address is 66c000 (looks ok). Control Registers:
CR0 = 8001003b
CR4 = 2200
IA32_FEATURE_CONTROL MSR = 0xd
IA32_EFER.LMA = 0x0
EFLAGS = 246
A20 = 0x0
Then when I call vmxon I get interrupt 13 a GP - seems to not like my memory reference to the vmxon region?

Watcom does not have support for the Intel VMX instruction set so had to add lib with opcode for vmxon

unsigned long long vmxon_region;


void __near vmxon(void);
#pragma aux vmxon = \
"nop" \
"nop" \
"push ebx" \
"mov ebx, offset vmxon_region" \
"nop" \
"nop" \
0xF3 0x0F 0xC7 0x33 \
"nop" \
"nop" \
"pop ebx" \
"nop" \
"nop";
I assign the 32 bit addres to vmxon_region (a 64 bit long long) in another function and then call vmxon. I am not real clear about how vmxon works does it take indirect or direct address? Tried both and haven't gotten anything but GP

Re: Booting OS as a VMM

Posted: Mon Aug 09, 2010 10:51 am
by MJ
Thanks, I saw a newsgroup that you needed to call vmxon from a 64 bit code segment. I'll give it a try!

Re: Booting OS as a VMM

Posted: Wed Aug 11, 2010 1:39 pm
by MJ
bump

Re: Booting OS as a VMM

Posted: Wed Aug 11, 2010 1:58 pm
by Combuster
bump
Where? :twisted:

Re: Booting OS as a VMM

Posted: Wed Aug 11, 2010 2:17 pm
by gerryg400
Are all your segments registers valid ?

Is your code segment readable ?

Re: Booting OS as a VMM

Posted: Wed Aug 11, 2010 2:52 pm
by Cognition
VMXON takes a memory address that should point to the physical address of the region, similar to how INVLPG works but with a physical address. Intel's SDM Volume 2B describes in depth the scenarios which can cause the VMXON instruction to GP fault. If I had to guess it's probably has to do with the setting of reserved bits in CR0/CR4, the settings of which can be determined from specific MSRs listed in Volume 3B of the SDM. This thread might be of some use to you.

Re: Booting OS as a VMM

Posted: Wed Aug 11, 2010 5:24 pm
by MJ
Yes all segment registers are valid
yes code segment is readable
I tried putting the vmxon region in both GS and DS and still get GP.

The MSR's related to setting the control registers:

IA32_VMX_CR0_FIXED0 = 0x80000021
IA32_VMX_CR0_FIXED0 = 0xFFFFFFFF
IA32_VMX_CR4_FIXED0 = 0x2000
IA32_VMX_CR4_FIXED1= 0x67FF
CR0 = 0x8001003B
CR4 = 0x2200

CR0 and CR4 seem to be correct as far as I can tell. I am still thinking that it may be how I am handling the address in the vmxon opcode? I am using an indirect address??

Re: Booting OS as a VMM

Posted: Wed Aug 11, 2010 6:14 pm
by gerryg400
I don't know whether the opcodes are correct, but my understanding is that you pass a register containing a 32 bit pointer to the 64 bit value in memory. I'm not certain you need the 2nd level of indirection. Did you try the opcode sequence ?

Code: Select all

0xF3 0x0F 0xC7 0xF1
EDIT: Actually, on 2nd thought, it's very unlikely that I'm right.

Re: Booting OS as a VMM

Posted: Thu Aug 12, 2010 3:27 pm
by MJ
ok tried F3 and get invalid opcode?

Re: Booting OS as a VMM

Posted: Thu Aug 12, 2010 5:40 pm
by MJ
I used F3 instead of F1 because I am using EBX. I assmume it does not matter which register you use.

Re: Booting OS as a VMM

Posted: Sun Aug 29, 2010 7:25 pm
by 01000101
Have you checked out this stub I made a while ago? I'm going to re-implement some old code of mine into a more basic OS soon to do some more testing. When I'm at my workstation I'll re-read your issue and see if I can find some differences and hopefully get around to adding information to that wiki article. I'd also be grateful if you did the same as you progress in your VMX journey. :-)

Re: Booting OS as a VMM

Posted: Wed Sep 01, 2010 7:45 pm
by 01000101
I dug up an old copy of my VMX code. You can find it (and earlier revisions or that plus its associated header) in the ArcticOS repository. http://quokforge.org/projects/arcticos/repository/revisions/145/entry/trunk/Core/vmx.c

Sorry I don't have much else to help with at this time, still juggling a few other things before I get to working on VMX stuff.