阅读:1098回复:8
卸载驱动程序系统会不会保护一些资源?
当卸载驱动程序的时候,系统是怎么保证能够卸载的呢!或者是必须由自己来保证?
例如对于一些过滤驱动程序,对于每一个请求都设置一个自己的完成例程,那么如果一个请求下发到下层驱动程序了而还没有调用驱动程序设置的完成例程,那么这个时候调用卸载函数,如果没有保护的话,那么就很容易出错的啊!可是怎么才能保证他们不会出错呢! 如果正在调用卸载函数,可是这个时候又有请求发送到对应的处理函数,那么也会出错的啊! 系统怎么保证不会出现这种情况的呢! 例如 卸载函数A 请求处理函数B 请求完成函数C 在B里面有 1 IoCopyCurrentIrpStackLocationToNext(IRP); 2 IoSetCompletionRoutine(IRP,C,NULL,TRUE,TRUE,TRUE);); 3 status=IoCallDriver(nextdevobj,irp); 4 return status; 那么当运行3的时候把驱动程序卸载,那么系统是怎么保证不会出错的呢! 还有就是在4运行完成了,而请求还没有完成,还没有调用C的时候,却要把驱动程序卸载,系统又是怎么保证不会出错的呢! 当正在运行卸载函数A的时候,系统是否不会发送请求到B呢? |
|
沙发#
发布于:2002-05-27 08:42
这个需要由驱动程序自身来处理。
通常的做法是在驱动程序的设备扩展中保存当前的内核调用记数同时初始化一个内核事件。通过InterlockedIncrement,InterlockedDecrement安全的记数,当内核调用记数为0时将事件置为信号态。当设备收到IRP_MN_QUERY_STOP_DEVICE、IRP_MN_STOP_DEVICE、IRP_MN_QUERY_REMOVE_DEVICE、IRP_MN_REMOVE_DEVICE的请求时等待内核事件变为信号态才能执行。 如果你自己的驱动程序不做这样的处理,肯定会出现你所说的问题,因为这是你自己的驱动程序的健壮性问题,应该是你自己的“分内之事”。 |
|
|
板凳#
发布于:2002-05-27 11:27
就算使用内核事件也会有问题的啊!当卸载函数获得这个事件之后,有可能请求还会发送给请求处理函数的啊!
|
|
地板#
发布于:2002-05-27 17:19
你既然已经拥有这个内核事件,说明你正在处理IRP_MN_QUERY_STOP_DEVICE、IRP_MN_QUERY_REMOVE_DEVICE等请求,这时如果收到新的请求,你就应该不将它分发,直到你收到IRP_MN_CANCEL_REMOVE_DEVICE,IRP_MN_CANCEL_STOP_DEVICE;同样在处理IRP_MN_STOP_DEVICE后,你也应该不将它分发,直到你再收到系统的IRP_MJ_START_DEVICE请求。
而IRP_MN_REMOVE_DEVICE应该是你的驱动程序收到的最后一个请求啊。 具体的做法,你参考一下实际硬件的驱动程序,比如NTDDK\\SRC\\WDM\\USB\\ISOUSB的例子,或98DDk\\SRC\\USB\\ISOUSB。 写了这么多,给点分表示表示嘛。 :D :D :D |
|
|
地下室#
发布于:2002-05-27 19:57
哈哈!找到答案了!
Unload Routine Environment The operating system unloads a driver when the driver is being replaced, when all the devices the driver services have been removed, or when driver initialization fails. The PnP Manager calls a PnP driver\'s Unload routine if the driver has no more device objects after it handles an IRP_MN_REMOVE_DEVICE request. At the start of the unloading sequence, the I/O Manager or PnP Manager marks the driver object and its device objects as \"Unload Pending\". After a driver has been marked as \"Unload Pending\", no additional drivers can attach to that driver, nor can any additional references be made to the driver\'s device objects. The driver can complete outstanding IRPs, but the system will not send any new IRPs to the driver. The I/O Manager calls a driver\'s Unload routine when all of the following are true: No references remain to any of the device objects the driver has created. In other words, no files associated with the underlying device can be open, nor can any IRPs be outstanding for any of the driver\'s device objects. No other drivers remain attached to this driver. The driver has called IoUnregisterPlugPlayNotification to unregister all PnP notifications for which it previously registered. Note that the Unload routine is not called if a driver\'s DriverEntry routine returns a failure status. In this case, the I/O manager simply frees the memory space taken up by the driver. Neither the PnP Manager nor the I/O Manager calls Unload routines at system shutdown time. A driver that must perform shutdown processing should register a DispatchShutdown routine. 给你一半的分数吧!哈哈,要不你把元宝给我我就全给你了 |
|
5楼#
发布于:2002-05-28 08:21
你说的是操作系统需要做的工作,我说的是你自己的驱动程序需要做的工作。呵呵,连在一起就完整了。
给我分啊! |
|
|
6楼#
发布于:2002-05-28 17:46
我问的就是这些啦!对于驱动程序之间的资源访问,当然要在需要的时候进行保护啦!但是对于整个驱动程序的请求调度和驱动程序对象的访问就需要由系统来保护了,但是它是怎么保护的呢!驱动对象里面没有一些核心对象来保护啊!不可能通过一些标志来保护的吧!
|
|
7楼#
发布于:2002-05-28 17:53
那可能要深入IO管理了,可是我没有时间做实验。现在项目紧,哎。
|
|
|
8楼#
发布于:2002-05-28 18:09
////////////////////////////////////////////
我问的就是这些啦!对于驱动程序之间的资源访问,当然要在需要的时候进行保护啦!但是对于整个驱动程序的请求调度和驱动程序对象的访问就需要由系统来保护了,但是它是怎么保护的呢!驱动对象里面没有一些核心对象来保护啊!不可能通过一些标志来保护的吧! /////////////////////////////////////////// 可操作系统又由,谁保护??? 软件&硬件-》电路,脉冲,门。。。。。。。。 -》 榔头,焊枪,老虎钳,“老鼠药”。。。。。。。。。。。 :D :D :D guardee: 你真的想知道吗? 去写操作系统吧。。。。。。。。。。 想当年,linus 就是为此,而。。。。。。。。。\"生出\"linux.. :P :P :P |
|
|