flat assembler
Message board for the users of flat assembler.

Index > DOS > Intel Virtualization (VMX) Utility in FASM

Author
Thread Post new topic Reply to topic
HyperVista



Joined: 18 Apr 2005
Posts: 691
Location: Virginia, USA
HyperVista
This is a simple program that determines if the CPU supports Intel's new virtualization extensions (VMX). The very first thing that needs to be done before launching a VM is determining if the CPU supports VMX operaitons. This utility accomplishes that. While the program merely prints a message indicating if the processor supports VMX, it could be used as part of a VMM or hypervisor installation program or a survey tool. Currently, Intel has over 15 CPU products out there that support VMX. Time to jump in and write some VMX code!

Code:
;*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
; Written and compiled with FASMW v1.67.3
; 2006-08-25 HyperVista
; Thanks to vid and locodelassembly for their help and guidance
;
; The purpose of this simple program is to determine
; if the box on which it is run supports Intel's VT-x
; virtualization.
;
; This program uses CPUID to determine if the CPU is Intel
; and if so, it determines if it supports Intel's VT-x
; extensions.  First, we use CPUID with 00H in EAX in order
; to retrieve the vendor identification string and then we
; test that string.  Next we use CPUID with 01H in EAX to
; retrieve the Extended Feature information returned in ECX.
; If Bit 5 in CL is set it indicates that the processor
; supports VT-x.
;
; While this program only prints a message indicating the
; processor does or doesn't support Intel's VT-x, this routine
; can be used to determine if VT-x is supported as a first
; step in a VMM or hypervisor installation program or as a
; portion of a survey tool.
;
;*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*

org 100h

jmp start

; Print Routine
print:
        mov ah,9h
        int 21h
        ret

print_wait:
        call print
        xor ax,ax   ; pause screen until user hits any key
        int 16h
        jmp exit

; Message Text
no_msg db 'Genuine Intel, but no VT-x support!  Press any key to continue....', 13, 10, '$'
yes_msg db 'Genuine Intel and VT-x IS supported!  Please press any key to continue....', 13, 10, '$'
not_intel db 'Non-Intel Processor.  VT-x in NOT supported.  Please press any key to continue....', 13, 10, '$'

start:
        ;  Check to see if the processor is Intel
        mov ax,00h
        CPUID
        cmp ebx,'Genu'    ; partial Intel signature placed in EBX
        jz Is_Intel       ; if not an Intel chip, we're done
        mov dx,not_intel
        call print_wait

        Is_Intel:
        ; Check to see if VT-x is supported
           mov ax,01h
           CPUID
           and cl,20h       ; ECX bit 5 bit mask test
           jnz VTX_Supported
           mov dx,no_msg
           call print_wait

       ; VTX supported - we only print message now
       ; This is where we will launch VMM or hypervisor later
       VTX_Supported:
            mov dx,yes_msg
            call print_wait

        ; Program End
        exit:
           mov ax,4C00h   ; hand back control to the OS
           int 21h    


Description:
Download
Filename: chk_vtx4.ASM
Filesize: 2.22 KB
Downloaded: 257 Time(s)



Last edited by HyperVista on 27 Aug 2006, 22:16; edited 8 times in total
Post 25 Aug 2006, 12:55
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Quote:
Code:
        mov dl,20h   ; Bit 5 bit mask 
        and cl,dl    



Can be:
Code:
and cl, 20h    


Quote:
Code:
        cmp ebx,756e6547h ;this value is partial Intel signature 
    


If you want it can be:
Code:
        cmp ebx, 'Genu' ;this value is partial Intel signature    


But with FASM only, other assemblers assembles "cmp ebx, 47656e75h" if you use 'Genu'.
Post 25 Aug 2006, 15:59
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u
Nice program HyperVista.
There's a Dex4u OS program, that detects "VME" as in Virtaul Mode Extension its call "cpuid.dex", i can send you the code, if it will help ?.

Also you above program runs fine on MiniDOS if you want to give people a bootable ver.
Post 26 Aug 2006, 21:56
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u
HyperVista wrote:
Are you coming to Krakow at the end of Sept?
HyperVista

I would really like to, so will do my best, but its only about 20% changes that i can make it Sad.
Post 27 Aug 2006, 19:46
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
now we should try to just enter/exit VMM root mode. From reading the intel manuals it should be just:

- set bit 13 of CR4
- set bit 0 and 2 of IA_FEATURE_CONTROL (MSR address 0000003Ah)
- allocate 4kb aligned physical memory
- initialize this region - no idea how
- vmxon
- vmxoff
- clear bit 13 of CR4

but i am a little confused about bit 0 of that MSR - what if it is already set, but bit 2 is not?

Is this everything, or did i miss something?
Post 06 Sep 2006, 08:13
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
HyperVista



Joined: 18 Apr 2005
Posts: 691
Location: Virginia, USA
HyperVista
vid - yes, that's basically it and while that's what the Intel manual says, there is quite a bit more to it than that Wink . i'll write more later tonight about the process. i'm at work now and all my hypervisor code is at home.

If bits 0 and 2 of the IA32_FEATURE_CONTROL are not set, BIOS needs to set them (some newer BIOS images provide a configuration setting to do this). So, part of the entering VMX routine involves checking these two bits before going forward (see the code provided below and you'll see these bits being checked to determine if they are set or cleared).


Last edited by HyperVista on 07 Sep 2006, 01:48; edited 1 time in total
Post 06 Sep 2006, 14:49
View user's profile Send private message Visit poster's website Reply with quote
HyperVista



Joined: 18 Apr 2005
Posts: 691
Location: Virginia, USA
HyperVista
vid - here's a bit of code that sets up VMX. as you can see, you have to set up the vmcs (virtual machine control structure - the 4kb alligned memory you mentioned), set bits in CR4, set bits in the proper MSR (wrmsr(..._MSR,..._LOCK | ..._VMXON) call VMXON.

This is how Xen 3.0 starts up VMX
Code:
int start_vmx(void)
{
    struct vmcs_struct *vmcs;
    u32 ecx;
    u32 eax, edx;
    u64 phys_vmcs;      /* debugging */

    /*
     * Xen does not fill x86_capability words except 0.
     */
    ecx = cpuid_ecx(1);
    boot_cpu_data.x86_capability[4] = ecx;

    if (!(test_bit(X86_FEATURE_VMXE, &boot_cpu_data.x86_capability)))
        return 0;

    rdmsr(IA32_FEATURE_CONTROL_MSR, eax, edx);

    if (eax & IA32_FEATURE_CONTROL_MSR_LOCK) {
        if ((eax & IA32_FEATURE_CONTROL_MSR_ENABLE_VMXON) == 0x0) {
            printk("VMX disabled by Feature Control MSR.\n");
            return 0;
        }
    }
    else {
        wrmsr(IA32_FEATURE_CONTROL_MSR,
              IA32_FEATURE_CONTROL_MSR_LOCK |
              IA32_FEATURE_CONTROL_MSR_ENABLE_VMXON, 0);
    }

    if (!check_vmx_controls(MONITOR_PIN_BASED_EXEC_CONTROLS,
                            MSR_IA32_VMX_PINBASED_CTLS_MSR))
        return 0;
    if (!check_vmx_controls(MONITOR_CPU_BASED_EXEC_CONTROLS,
                            MSR_IA32_VMX_PROCBASED_CTLS_MSR))
        return 0;
    if (!check_vmx_controls(MONITOR_VM_EXIT_CONTROLS,
                            MSR_IA32_VMX_EXIT_CTLS_MSR))
        return 0;
    if (!check_vmx_controls(MONITOR_VM_ENTRY_CONTROLS,
                            MSR_IA32_VMX_ENTRY_CTLS_MSR))
        return 0;

    set_in_cr4(X86_CR4_VMXE);   /* Enable VMXE */

    if (!(vmcs = alloc_vmcs())) {
        printk("Failed to allocate VMCS\n");
        return 0;
    }

    phys_vmcs = (u64) virt_to_maddr(vmcs);

    if (!(__vmxon(phys_vmcs))) {
        printk("VMXON is done\n");
    }

    vmx_save_init_msrs();

    /* Setup HVM interfaces */
    hvm_funcs.disable = stop_vmx;

    hvm_funcs.initialize_guest_resources = vmx_initialize_guest_resources;
    hvm_funcs.relinquish_guest_resources = vmx_relinquish_guest_resources;

    hvm_funcs.store_cpu_guest_regs = vmx_store_cpu_guest_regs;
    hvm_funcs.load_cpu_guest_regs = vmx_load_cpu_guest_regs;

    hvm_funcs.realmode = vmx_realmode;
    hvm_funcs.paging_enabled = vmx_paging_enabled;
    hvm_funcs.instruction_length = vmx_instruction_length;
    hvm_funcs.get_guest_ctrl_reg = vmx_get_ctrl_reg;

    hvm_funcs.init_ap_context = vmx_init_ap_context;

    hvm_enabled = 1;

    return 1;
}    
Post 06 Sep 2006, 17:09
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.