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 The mechanism of the WIN 95 device driver is endless, Win 98 not only supports WDM compatible with Windows NT 5.0 (Win32 Driver
Mode driver, but also supports virtual device drivers with Win 95-compatible virtual device Vxd (Virtual Device
Driver. Here is Windows based on Windows.
The basic principle and design method of the 9X platform, the virtual device driver VXD, and combines the development tool vtoolsd to give a design instance of the virtual device driver VXD for visual phone audio card.
1. Windows 9x virtual environment
Windows 9x as a complete 32-bit multitasking operating system, it is not like Window
3.X depends on MS-DOS, but in order to ensure the compatibility of the software, Windows
The 9X has supported the operation of the MS-DOS application in addition to supporting Win16 applications and Win32 applications. Windows 9x is via virtual machine VM (Virtual
Machine environment to ensure its compatible and multitasking characteristics.
Windows virtual machine (usually referred to as Windows
VM) means that the virtual environment for executing the application, includes two virtual machine environments that MS-DOS VM and SYSTEM VM. In every MS-DOS
You only run a MS-DOS process in the VM, and System VM can be all Windows applications and dynamic link library DLL (Dynamic Link)
Libraries) Provides an operating environment. Each virtual machine has a separate address space, register status, stack, local descriptor table, interrupt state, and priority. Although Win16, Win32 application is running in System
In the VM environment, the Win16 application share the same address space, while the Win32 application has its 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 in Virtual Machine Manager VMM (Virtual Machine
The monitor is running under the monitoring, and the 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 WIN
95 RING0 system core (application runs 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 above, VXD is Virtual Device
Driver's abbreviation, 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 flat storage mode (FLAT MEMORY
Model, but its code and data still use segmentation management, six types of segments, namely, mode initialization, protection mode initialization, pageable, non-page, static, and only debugging (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 completed, the "real mode initialization" segment and the "Protection Mode Initialization" are abandoned. Most of the code of VXD is in some of the other sections, the "Paging" segment allows the virtual storage manager (Virtual
Memory
MANAGER) Make paging management, most VXD code should be in the "pageable" 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. "Only debug" paragraph is only VMM in Soft-Ice
It will be loaded in the FOR WIN 95 and other debug environments.
VMM is a device descriptor block DDB via VXD (Device Descriptor)
Block) to identify. 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 ports) or only contact with one VM, VXD needs to generate its own supported data structure (Supporting
Data
Structures) 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), the written program code will be available in all Windows platforms (including Win 95, Win 98, and Windows)
Share on NT). Of course, Microsoft's DDK (Device Developer)
Kit) to develop VXD, but DDK cannot provide a rich C run library and C class library through the underlying technology details of VToolsd, but to make developers fully enjoy the object-oriented programming method, so only For this point, it is inconvenient to use DDK.
4. VXD program design instance
I am using VToolsd 2.03, VC during the design of the visual telephone audio card.
5.0 Develop a virtual device driver AudCard.vxd for the homemade PC / XT bus expansion card. This card applies for an interrupt every 20ms, and the interrupt is interrupted by the application dynamic load system AudCard.vxd response and processes. Interrupt service program ISR (Interrupt
After the service routine, call the function shell_postmessage.
) Send a custom message to the application window. After the application accepts the message, then through the function deviceiocontrol (
) Interpretation of the interface function on the interface function onw32deviceioControl (). The program ends can be dynamically uninstalled. The following figure shows in Win
95 VXD to the processing process of hardware interrupt.
Figure Win95 hardware interrupt processing process
When the interrupt occurs, the processor is converted to RING0-level protection mode. The Windows system is not like the interrupt descriptor table IDT (Interrupt) like DOS.
Descriptor
Table) directly points to the interrupt processing, but by the IDT entry points to the program in the VMM. The program will determine if it is an interrupt call. If so, the interrupt control is handed over to the virtual programmable interrupt controller VPICD (Virtual
Programmable Interrupt Controller
Device), 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 :: oversysdynamicDeviceinit ()
// initialization when dynamically loading
{
... // 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
}