Design and implementation of virtual equipment drivers

zhaozj2021-02-08  236

Design and implementation of virtual equipment drivers

Since Windows takes a shielded policy for the system underlying operation, the system has become more secure, but this has brought a small difficult difficulty to many hardware or system software developers because as long as the application involves To the end of the floor, developers have to go deep into the Windows kernel to write a system-level virtual device driver. Win 98 is not the same, WIN 98 does not only support WINDM (Win32 Driver Mode)-compatible WDM (Win32 Driver Mode) mode driver, but also supports virtual device driver Vxd (Virtual Device Driver) with Win 95-compatible virtual device drivers ). The basic principle and design method of the virtual environment based on the Windows 9x platform, the virtual device driver VXD is introduced, and a design instance of a virtual device driver VXD for a visual phone audio card is given in conjunction with the development tool vtoolsd.

1. Windows 9x virtual environment

Windows 9x as a complete 32-bit multitasking operating system, it does not depend on MS-DOS as Window 3.x, but in order to ensure software compatibility, Windows 9X has supported Win16 applications and Win32 applications. Support to support the MS-DOS application. Windows 9x is made through a virtual machine VM (Virtual Machine) ensures its compatible and multitasking features.

The so-called Windows virtual machine (usually referred to as Windows VM) is a virtual environment that performs an application, which includes two virtual machine environments that MS-DOS VM and System VM. You only run a MS-DOS process in each MS-DOS VM, while System VM provides a running environment for all Windows applications and dynamic link libraries. Each virtual machine has a separate address space, register status, stack, local descriptor table, interrupt state, and priority. Although Win16, Win32 applications run in the System VM environment, but Win16 applications share the same address space, while Win32 applications have their own independent address space.

When writing an application, programmers often ignore differences between virtual environments and real environments, which generally believe that virtual environments are real environments. However, when writing a virtual device driver VXD, it cannot do this because the Work of VXD is to provide the application code to the environment of the hardware interface, and manage the status of virtual devices for each customer virtual machine, transparent arbitration multiple applications. The program is also accessed at the same time. This is the concept of so-called virtualization.

VXD runs under the monitoring of virtual machine managers VMM (Virtual Machine Manager, and VMM is actually a special VXD. VMM performs work related to system resources, providing virtual machine environments (generated, scheduling, uninstalling VM), responsible for scheduling multithreading, pretty time, and managing virtual memory. Vxd and VMM run outside of any other virtual machine, VXD is in fact the software of the virtual machine.

Like most operating systems, Windows also uses a layer-based architecture. VMM and VXDS constitute the core of Win 95 RING0 system (application running in Ring3, Ring1, Ring2 unused), has the highest priority of the system. Windows also provides some drivers that are "DRV" as the suffix name, mainly referring to the communication program of the serial port and the printer program of the parallel port. These programs are different from VXD, which are running in RING3. Figure 1 can better understand the virtual environment of Windows.

Figure

2. In-depth understanding VMM and VXD

As mentioned earlier, VXD is an abbreviation of Virtual Device Driver, but someone understands it as a virtual driver. In fact, VXD does not only refer to the device drivers of those virtualized specific hardware. For example, some VXDs can virtualize the device, and some VXDs are not virtualized as the device driver, and some VXDs and devices have nothing to do, which only provides services to other VXDs or applications. VXD can be static with VMM, and can be dynamically loaded or uninstalled as needed. It is because of the close collaboration between VXD and VMM, the VXD has the capacity does not have to have applications, such as accessible hardware devices without restrictions, arbitrarily viewing operating system data structures (such as descriptors, page tables, etc. ), Access any memory area, capture software interrupt, capture I / O port operations and memory access, or even interception of hardware interrupts.

Although VXD uses 32-bit plane storage mode (Flat Memory Model), its code and data still uses segmentation management, six types of segments, namely, mode initialization, protection mode initialization, pageable, non-page, static and only Debug Only, each type has a code segment and data segment, so VXD has 12 segments. The real mode code segment and data segment are 16 bits (segment mode), and other segments are 32 bits (planar mode). The "Real Mode Initialization" section contains the code to be executed before the initial phase VMM of the Windows initialization process. Static loaded VXD At this time, you can view the real-mode environment before Windows startup, decide whether to continue loading, and notify VMM. After the loading is completed, the VMM enters the protection mode and performs the protection mode initialization code, and the results will be notified to the VMM. After the initialization is complete, the "real mode initialization" segment is abandoned. Most of the code of VXDs are in some of the other sections, and the "Split" segment allows the Virtual Memory Manager to be managed, and most VXD code should be in the "Paging" segment. The contents of the "Inable" segment mainly include: VXD's primary invested point, hardware interrupt processing function, the data accessed, and asynchronous services that can be called by another VXD interrupt process. The "static" segment is only used to dynamically load VXD, and the static code segment and data segments are kept in memory when VXD is uninstalled. The "only debug" segment is just the VMM to load it in debug environments such as Soft-Ice for Win 95.

The VMM is identified by the device descriptor block DDB (Device Descriptor Block) via VXD. DDB provides VXD's primary invested points to VMM, and also provides entry points to applications and other VXDs. VMM uses this primary entry point to notify VXD, then VXD respond to these events through the appropriate work. Since VXD is not just a physical device (such as multiple serial port) or only contact with one VM, VXD needs to generate its own supported data structure to save each device, each VM configuration and status information. VXD saves device information with one or more device context structures, such as I / O port base sites, interrupt vectors, etc., VXD saves their status information of each of them in the VMM's VM control block.

The services provided by VMM include: event service, memory management service, compatible execution and protection mode execution, login table service, scheduling service, synchronous service, debugging service, I / O capture service, handling error and interrupt service, VM interrupt And callback services, configuration management program services, and other miscellaneous services.

The above includes only a small part of the VXD design, and the developer as VXD must master more knowledge. The first is the knowledge of the operating system, such as address space, executing context, resource lock, interprocess communication, and asynchronous event processing; Second, there should be more in-depth understanding of Intel processors, including registers, machine instructions, Protection mechanism, paging mechanism, and virtual 8086 mode; Final, you must be familiar with all kinds of services and interfaces provided by VMM, familiar with Windows other system VXDs. 3. Development Tool VToolsd Introduction

VToolsd is a tool software that is designed to develop VXD programs. It includes VXD frame code generator QuickVXD, C runprub, VMM / VXD service library, VXD C class library, VxDload, and VxDView and other utility and a large number of C, C routine. Compiled by VC , BC Compile the generated VXD program to be run out of the VToolsd environment.

Using QuickVXD, it can generate VXD framework, which is generated by three files named h, CPP, and MAK. The source file contains basic components that run VXD, which contain function frameworks such as control message processing, API entry points, and VXD services, and also define the flag, set the compilation parameters, declare the class, and then in the C environment, generate Add your own code in each processing function body, and finally use the compiler NMAKE to generate a standard VXD program.

Since VXD is running in RING0, the debugger is quite difficult. The debugging tool I use is Soft-Ice for Win 95.

At present, VToolsd's latest version is 3.0, which supports the device access architecture DAA (Device Access Architecture), which can be shared on all Windows platforms, including Win 95, WIN 98, and Windows NT. Of course, you can also use Microsoft's DDK (Device Developer Kit) to develop VXD, but DDK cannot provide rich C runns and C class libraries like vToolsd, which will provide a rich C running and C class libraries in the underlying technical details of VXD, but let developers fully enjoy The object-oriented programming method is convenient and fast, so it is inconvenient to use DDK only on this point.

4. VXD program design instance

I developed the virtual device driver Audcard.vxd with VToolsd 2.03, VC 5.0, using VToolsD 2.03, VC 5.0. VTOOLSD 2.03. This card applies for an interrupt every 20ms, and the interrupt is interrupted by the application dynamic load system AudCard.vxd response and processes. After the interrupt service program isr (Interrupt Service Routine), the call function shell_postMessage () sends a custom message to the application window. After the application accepts the message, the buffer data is switched through the function deviceiocontrol () interface function on the VXD. The program ends can be dynamically uninstalled. The figure below shows the processing procedure of the hardware interrupt in WIN 95.

Figure Win95 hardware interrupt processing process

When the interrupt occurs, the processor is converted to RING0-level protection mode. The Windows system does not directly point to the interrupt process by the interrupt descriptor table IDT (Interrupt Descriptor Table) as DOS, but is directed to the program in the VMM by the IDT entry. The program will determine whether it is an interrupt call. If so, the interrupt control is given to the virtual programmable interrupt controller Vpic (Virtual ProgramMable Interrupt Controller Device), and VPICD is actually an important VXD. Vpic is then handed over to another VXD (such as AudCard.vxd) that is interrupted. The VXD program is to register the interrupt by calling VPICD service vpicd_virtualize_irq. Virtual equipment driver Audcard.vxd part of the source code Audcard.h and audCard.cpp Online, URL: www.pc.computing.com.cn. This application uses the following functions: createfile () dynamically loads VXD, CloseHandle () and dynamically uninstalling VXD, PretranslateMessage () intercept message, DeviceIOControl () and VXD intermittent buffer data. The virtual device driver Audcard.vxd works fine after debugging, and no data or crash occurred.

Here is some source code audCard.h and audCard.cpp in the virtual device driver Audcard.vxd, limited to space, and audCard.mak generated automatically by QuickVxd is not listed.

1AudCard.h

//Audcard.h - incrude file for vxd audcard

#include

#define device_class audcarddevice

#define audCard_DeviceID undefined_device_id

#define audCard_init_order undefined_init_order # define audCard_major

#define audCard_minor 0

#define my_irq 5 // Define No. 5 Interrupt

Class Myhwint: Public VhardwareInt

{

PUBLIC:

MyHwint (): VhardwareInt (my_irq, 0,0,0) {}

Virtual void OnhardwareInt (VMHandle);

}

Class AudcardDevice: Public VDEvice

{

PUBLIC:

Virtual bool onsysdynamicDeviceinit ();

Virtual bool onsysdyNamicDeviceExit ();

Virtual DWORD ONW32DEVICEIOCONTROL (PiocTlparams PDIOCPARAMS);

Myhwint * PMYIRQ;

}

Class Audcardvm: Public VvirtualMachine

{

PUBLIC:

Audcardvm (VMHANDLE HVM);

}

Class Audcardthread: Public vthread

{

PUBLIC:

Audcardthread (threadhandle hthread);

}

2AudCard.cpp

//Audcard.cpp - main module for vxd Audcard

#define device_main

#include "audCard.h"

Declare_virtual_device (audCard)

#define WM_USER_POSTVXD 0X1000

// Customize the message

#undef device_main

Audcardvm :: AudCardvm (VMHANDLE HVM): vvirtualmachine (hvm) {} AudcardthRead :: Audcardthread (threadhandle hthread): vthread (hthread) {}

Bool AudcardDevice :: OnSysDynamicDeviceInit () // Dynamically initialized

{

... // Hardware initialization

PMYIRQ = new myhwint ();

IF (PMYIRQ && PMYIRQ-> Hook ()) // Mount Interrupt

{

PMYIRQ-> PhysicalUnmask (); // Allow interrupt

Return True;

}

Else Return False;

}

Bool AudcardDevice :: oversdynamicDeviceexit ()

// Dynamic uninstall process

{

Delete PMYIRQ;

Return True;

}

DWord AudcardDevice :: OnW32DeviceioControl (PiocTlparams PDIOCPARAMS)

/ / Interface function with Win32 app

{

......

}

Void myhwint :: onhardwareint (vmhandle hvm)

{

... // Interrupt Processing

Shell_PostMessage (AppWnd, WM_USER_POSTVXD, 0, 0, 0, NULL);

/ / Send a message to the application window

Sendphysicalaleoi (); // Notify VPICD interrupt end

}

转载请注明原文地址:https://www.9cbs.com/read-1285.html

New Post(0)