《Windows内核编程》基本数据结构

上传人:M****1 文档编号:570460777 上传时间:2024-08-04 格式:PDF 页数:15 大小:186.26KB
返回 下载 相关 举报
《Windows内核编程》基本数据结构_第1页
第1页 / 共15页
《Windows内核编程》基本数据结构_第2页
第2页 / 共15页
《Windows内核编程》基本数据结构_第3页
第3页 / 共15页
《Windows内核编程》基本数据结构_第4页
第4页 / 共15页
《Windows内核编程》基本数据结构_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《《Windows内核编程》基本数据结构》由会员分享,可在线阅读,更多相关《《Windows内核编程》基本数据结构(15页珍藏版)》请在金锄头文库上搜索。

1、Windows 内核编程 基本数据结构学习各种高级外挂制作技术,马上去百度搜索(魔鬼作坊) ,点击第一个站进入,快速成为做挂达人。驱动对象:驱动对象:每个驱动程序都会有唯一的驱动对象与之对应, 并且这个驱动对象是在驱动加载时被内核中的对象管理程序所创建的。驱动对象用 DRIVER_OBJECT 数据结构表示,它作为驱动的一个实例被内核加载,并且内核对一个驱动只加载一个实例。 确切地说,是由内核中的 I/O 管理器负责加载的,驱动程序需要在 DriverEntry 中初始化。驱动对象的结构定义如下(wdm.h) :typedef struct _DRIVER_OBJECT /结构的类型和大小CS

2、HORT Type;CSHORT Size;/每个驱动程序会有一个或多个设备对象,其中,每个设备对象都有一个指针指向下一个驱动对象/最后一个设备对象指向空。DeviceObject指向驱动对象的第一个设备对象。通过 DeviceObject,就/可以遍历驱动对象中的所有设备对象了。PDEVICE_OBJECT DeviceObject;ULONG Flags;/ The following section describes where the driver is loaded.The count/ field is used to count the number of times the

3、driver has had its/ registered reinitialization routine invoked.PVOID DriverStart;ULONG DriverSize;PVOID DriverSection;PDRIVER_EXTENSION DriverExtension;/ 记录驱动设备的名字,用 UNICODE 字符串记录,该字符串一般/Driver/驱动程序名称UNICODE_STRING DriverName;/设备的硬件数据库键名,也是 UNICODE 字符串记录。一般为/REGISTRY/MACHINE/HADRWARE/DESCRIPTION/SY

4、STEMPUNICODE_STRING HardwareDatabase;/文件驱动中用到的派遣函数PFAST_IO_DISPATCH FastIoDispatch;/ The following section describes the entry points to this particular/ driver.Note that the major function dispatch table must be the last/ field in the object so that it remains extensible.PDRIVER_INITIALIZE DriverIn

5、it;/记录 StartIO 例程的函数地址,用于串行化操作PDRIVER_STARTIO DriverStartIo;/指定驱动卸载时所用的回调函数地址PDRIVER_UNLOAD DriverUnload;/MajorFunction 域记录的是一个函数指针数组,也就是 MajorFunction 是一个数组,数组中的每个/成员记录着一个指针,每一个指针指向的是一个函数。这个函数是处理 IRP 的派遣函数PDRIVER_DISPATCH MajorFunctionIRP_MJ_MAXIMUM_FUNCTION + 1; DRIVER_OBJECT;typedef struct _DRIVE

6、R_OBJECT *PDRIVER_OBJECT;实际上如果写一个驱动程序,或者说编写一个内核模块,要在 Windows中加载,就必须填写上面的结构,来告诉 Windows程序提供的功能。注意内核模块并不生成一个进程,它只是写一组回调函数让Windows调用, 且这组回调函数必须符合Windows内核规定的格式。上面代码中的“快速 IO分发函数”FastIoDispatch和“普通分发函数”MajorFunction 就是这样一种回调函数。这些函数用来处理发送给这个内核模块的请求。Windows中很多组件都拥有自己的 DRIVER_OBJECT,例如:所有的硬件驱动程序、所有的类驱动(Disk

7、、Cdrom) 、文件系统(NTFS 和 FastFat,有各自的 DRIVER_OBJECT),以及许多其他的内核组件。我们可以使用一个软件 WinObj 来查看所有的内核对象。设备对象:设备对象:每个驱动程序会创建一个或多个设备对象,用 DEVICE_OBJECT 数据结构表示。每个设备对象都会有一个指针指向下一个设备对象, 因此就形成一个设备链。 设备链的第一个设备是由 DRIVER_OBJECT 结构体中指明的。设备对象是内核中的重要对象,其重要性不亚于 Windows GUI 编程中的窗口。窗口是唯一可以接收消息的对象,任何消息都是发送到一个窗口中的;而在内核编程中,大部分“消息”是

8、以请求 IRP 的方式传递的。而设备对象(DEVICE_OBJECT)是唯一可以接收请求的实体,任何一个请求 IRP 都是发送给某个设备对象的。设备对象的结构是 DEVICE_OBJECT,常常被简称为 DO。一个 DO 可以代表很多不同的东西,例如一个实际的硬盘、或实现一个类似管道的功能等等。我们总是在内核程序中生成一个 DO,而一个内核程序是用一个驱动对象表示的,因此,一个设备对象总是属于一个驱动对象。在 WDK 的 wdm.h 文件中 DO 的定义如下:typedef struct DECLSPEC_ALIGN(MEMORY_ALLOCATION_ALIGNMENT)_DEVICE_OB

9、JECT /结构的类型和大小CSHORT Type;USHORT Size;/引用计数LONG ReferenceCount;/指向驱动程序中的驱动对象,同属于一个驱动程序的驱动对象指向的是同一驱动对象struct _DRIVER_OBJECT *DriverObject;/下一个设备对象。/这里指的下一个设备对象是同属于一个驱动对象的设备, 也就是同一个驱动程序创建的若干设备/对象,每个设备对象根据 NextDevice 域形成链表,从而可以枚举每个设备对象struct _DEVICE_OBJECT *NextDevice;/指向下一个设备对象,这里指的是,如果有更高一层的驱动附加到这个驱动

10、的时候/AttachedDevice 指向的就是那个更高一层的驱动struct _DEVICE_OBJECT *AttachedDevice;/在使用 StartIO 例程的时候,此域指向的是当前 IRP 结构struct _IRP *CurrentIrp;PIO_TIMER Timer;/此域是一个32位的无符号整型,每一位有具体的含义/DO_BUFFERED_IO-读写操作使用缓冲方式(系统复制缓冲区)访问用户模式数据/DO_EXCLUSIVE-一次只允许一个线程打开设备句柄/DO_DIRECT_IO-读写操作使用直接方式(内存描述符表)访问用户模式数据/DO_DEVICE_INITIAL

11、IZING-设备对象正在初始化/DO_POWER_PAGABLE-必须在 PASSIVE_LEVEL 级上处理 IRP_MJ_PNP 请求/DO_POWER_INRUSH-设备上电期间需要大电流ULONG Flags;ULONG Characteristics;_volatile PVPB Vpb;/指向设备扩展对象,每个设备都会指定一个设备扩展对象,设备扩展对象记录的是设备自己/特殊定义的结构体,即程序员自己定义的结构体。另外,在驱动开发中,应该尽量避免全局变量的/使用,因为全局变量涉及不容易同步问题。解决的方法是:将全局变量存在设备扩展中PVOID DeviceExtension;/设备类

12、型,当制作虚拟设备时,应选择 FILE_DEVICE_UNKNOWN 类型的设备DEVICE_TYPE DeviceType;/IRP 栈大小。在多层驱动情况下,驱动与驱动之间会形成类似堆栈的结构,IRP 会依次从/最高层传递到最底层CCHAR StackSize;union LIST_ENTRY ListEntry;WAIT_CONTEXT_BLOCK Wcb; Queue;/设备在大容量传输时,需要内存对齐,以保证传输速度ULONG AlignmentRequirement;KDEVICE_QUEUE DeviceQueue;KDPC Dpc;/The following field is

13、 for exclusive use by the filesystem to keep/track of the number of Fsp threads currently using the deviceULONG ActiveThreadCount;PSECURITY_DESCRIPTOR SecurityDescriptor;KEVENT DeviceLock;USHORT SectorSize;USHORT Spare1;struct _DEVOBJ_EXTENSION*DeviceObjectExtension;PVOIDReserved; DEVICE_OBJECT;type

14、def struct _DEVICE_OBJECT *PDEVICE_OBJECT;一个驱动对象可以生成多个设备对象, 而 Windows向设备对象发送请求时,这些请求是被驱动对象的分发函数所捕获的, 即当 Windows内核向一个设备发送一个请求时,驱动对象的分发函数中的某一个会被调用,分发函数原型如下:/参数 device 是请求的目标设备;参数 irp 是请求的指针NTSTATUE ASCEDispatch(PDEVICE_OBJECT device, PIRP irp);附:设备扩展:设备对象记录“通用”设备的信息,而另外一些“特殊”信息记录在设备扩展里。各个设备扩展由程序员自己定义,

15、每个设备的设备扩展不尽相同。设备扩展是由程序员指定内容和大小,由 I/O 管理器创建的,并保存在非分页内存中。在驱动程序中,尽量避免使用全局函数,因为全局函数往往导致函数的不可重入性。重入性指的是在多线程程序中, 多个函数并行运行, 函数的运行结果不会根据函数的调用先后顺序而导致不同。解决的办法是,将全局变量以设备扩展的形式存储,并加以适当的同步保护措施。除此之外,在设备扩展中还会记录下列一些内容:设备对象的反向指针;设备状态或驱动环境变量;中断对象指针;控制器对象指针。由于设备扩展是驱动程序专用的,它的结构必须在驱动程序的头文件中定义。请求请求 IRPIRPIRPIRP:内核中大部分请求以

16、IRP 的形式发送。IRP 是一个内核数据结构,比较复杂,因为它要表示无数种实际请求。在 WDK 的 wdm.h 中可看到 IRP 的结构:/ I/O Request Packet (IRP) definitiontypedef struct DECLSPEC_ALIGN(MEMORY_ALLOCATION_ALIGNMENT) _IRP /结构的类型和大小CSHORT Type;USHORT Size;/ Define the common fields used to control theIRP./ Define a pointer to the Memory Descriptor Li

17、st (MDL) for this I/O/ request.This field is only used if the I/O is direct I/O.PMDL MdlAddress;/ Flags word - used to remember various flags.ULONG Flags;/ The following union is used for one of three purposes:/1. This IRP is an associatedIRP.The field is a pointer to a master/IRP./2. This is the ma

18、sterIRP.The field is the count of the number of/IRPs which must complete (associated IRPs) before the master can/complete./3. This operation is being buffered and the field is the address of/the system space buffer.union struct _IRP *MasterIrp;_volatile LONG IrpCount;PVOID SystemBuffer; AssociatedIr

19、p;/ Thread list entry - allows queueing the IRP to the thread pending I/O/ request packet list.LIST_ENTRY ThreadListEntry;/ I/O status - final status of operation.IO_STATUS_BLOCK IoStatus;/ Requestor mode - mode of the original requestor of this operation.KPROCESSOR_MODE RequestorMode;/ Pending retu

20、rned - TRUE if pending was initially returned as the/ status for this packet.BOOLEAN PendingReturned;/ Stack state information.CHAR StackCount;/IPR 栈空间大小CHAR CurrentLocation;/IRP 当前栈空间/ Cancel - packet has been canceled.BOOLEAN Cancel;/ Cancel Irql - Irql at which the cancel spinlock was acquired.KI

21、RQL CancelIrql;/ ApcEnvironment - Used to save the APC environment at the time thatthe/ packet was initialized.CCHAR ApcEnvironment;/ Allocation control flags.UCHAR AllocationFlags;/ User parameters.PIO_STATUS_BLOCK UserIosb;PKEVENT UserEvent;union struct union PIO_APC_ROUTINE UserApcRoutine;PVOID I

22、ssuingProcess;PVOID UserApcContext; AsynchronousParameters;LARGE_INTEGER AllocationSize; Overlay;/ CancelRoutine - Used to contain the address of a cancel routine supplied/ by a device driver when the IRP is in a cancelable state._volatile PDRIVER_CANCEL CancelRoutine;/ Note that the UserBuffer para

23、meter is outside of the stack so that I/O/ completion can copy data back into the users address space without/ having to know exactly which service was being invoked.The length/ of the copy is stored in the second half of the I/O status block. If/ the UserBuffer field is NULL, then no copy is perfor

24、med.PVOID UserBuffer;/ Kernel structures/ The following section contains kernel structures which the IRP needs/ in order to place various work information in kernel controller system/ queues.Because the size and alignment cannot be controlled, they are/ placed here at the end so they just hang off a

25、nd do not affect the/ alignment of other fields in theIRP.union struct union / DeviceQueueEntry - The device queue entry field is used to/ queue the IRP to the device driver device queue.KDEVICE_QUEUE_ENTRY DeviceQueueEntry;struct / The following are available to the driver to use in/ whatever manne

26、r is desired, while the driver owns the/ packet.PVOID DriverContext4;/ Thread - pointer to callers Thread Control Block.PETHREAD Thread;/ Auxiliary buffer - pointer to any auxiliary buffer that is/ required to pass information to a driver that is not contained/ in a normal buffer.PCHAR AuxiliaryBuff

27、er;/ The following unnamed structure must be exactly identical/ to the unnamed structure used in the minipacket header used/ for completion queue entries.struct / List entry - used to queue the packet to completion queue,among/ others.LIST_ENTRY ListEntry;union / Current stack location - contains a

28、pointer to the current/ IO_STACK_LOCATION structure in the IRP stack.This field/ should never be directly accessed by drivers.They should/ use the standard functions.struct _IO_STACK_LOCATION *CurrentStackLocation;/ Minipacket type.ULONG PacketType;/ Original file object - pointer to the original fi

29、le object/ that was used to open the file.This field is owned by the/ I/O system and should not be used by any other drivers.PFILE_OBJECT OriginalFileObject; Overlay;/ APC - This APC control block is used for the special kernel APC as/ well as for the callers APC, if one was specified in the origina

30、l/ argument list.If so, then the APC is reused for the normal APC for/ whatever mode the caller was in and the special routine that is/ invoked before the APC gets control simply deallocates theIRP.KAPC Apc;/ CompletionKey - This is the key that is used to distinguish/ individual I/O operations init

31、iated on a single file handle.PVOID CompletionKey;Tail; IRP;typedef IRP *PIRP;上面出现 IRP 栈空间,是因为一个 IRP 往往要传递 n 个设备才能得以完成,而在传递过程中会有一些“中间转换”,导致请求的参数变化。为了保存这些变换,我们给每次中转都留一个“栈空间”,用于保存中间参数。常见的请求:生成请求:主功能号为 IRP_MJ_CREATE查询请求:主功能号为 IRP_MJ_QUERY_INFORMATION设置请求:主功能号为 IRP_MJ_SET_INFORMATION控制请求: 主功能号为 IRP_MJ_DEVICE_CONTROL 或 IRP_MJ_INTERNAL_DEVICE_CONTROL关闭请求:主功能号为 IRP_MJ_CLOSE

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号