mouse.mouth
驱动牛犊
驱动牛犊
  • 注册日期2007-01-23
  • 最后登录2007-03-08
  • 粉丝0
  • 关注0
  • 积分120分
  • 威望13点
  • 贡献值0点
  • 好评度12点
  • 原创分0分
  • 专家分0分
阅读:2010回复:3

passthru中系统线程启动没有问题,但在驱动卸载时就成问题了,请教诸位有什么方法没有?

楼主#
更多 发布于:2007-01-23 10:50
我在passthru里面写了两个测试用的系统线程。线程的启动、关闭以及线程函数如下:
//全局变量
KEVENT                    RxThreadExiting;
KEVENT                    TxThreadExiting;
PVOID                    RxThreadObjPointer = NULL;
PVOID                    TxThreadObjPointer = NULL;
BOOLEAN                    RxExitNow = FALSE;
BOOLEAN                    TxExitNow = FALSE;

//线程例程
void TxThreadInit()
{
    HANDLE                    threadhandle;
    NTSTATUS                status;

    DBGPRINT( ( "==>TxThreadInit Routine\n" ) );
    TxExitNow = FALSE;

    KeInitializeEvent( &TxThreadExiting, SynchronizationEvent, FALSE );

    status = PsCreateSystemThread( &threadhandle,
                                THREAD_ALL_ACCESS,
                                NULL,
                                NULL,
                                NULL,
                                TxThreadFunc,
                                NULL );
    if( ! NT_SUCCESS( status ) )
    {
        DBGPRINT( ( "cannot create TX system thread\n" ) );
        return;
    }

    status = ObReferenceObjectByHandle( threadhandle,
                                        THREAD_ALL_ACCESS,
                                        NULL,
                                        KernelMode,
                                        &TxThreadObjPointer,
                                        NULL );
    if( ! NT_SUCCESS( status ) )
    {
        DBGPRINT( ( "cannot get TX system thread pointer\n" ) );
        return;
    }

    ZwClose( threadhandle );
}

void RxThreadInit()
{
    HANDLE                    threadhandle;
    NTSTATUS                status;

    DBGPRINT( ( "==>RxThreadInit Routine\n" ) );
    RxExitNow = FALSE;

    KeInitializeEvent( &RxThreadExiting, SynchronizationEvent, FALSE );

    status = PsCreateSystemThread( &threadhandle,
                                THREAD_ALL_ACCESS,
                                NULL,
                                NULL,
                                NULL,
                                RxThreadFunc,
                                NULL );
    if( ! NT_SUCCESS( status ) )
    {
        DBGPRINT( ( "cannot create RX system thread\n" ) );
        return;
    }

    status = ObReferenceObjectByHandle( threadhandle,
                                        THREAD_ALL_ACCESS,
                                        NULL,
                                        KernelMode,
                                        &RxThreadObjPointer,
                                        NULL );
    if( ! NT_SUCCESS( status ) )
    {
        DBGPRINT( ( "cannot get RX system thread pointer\n" ) );
        return;
    }

    ZwClose( threadhandle );
}

void TxThreadClose()
{
    DBGPRINT( ( "==>TxThreadClose Routine\n" ) );
    TxExitNow = TRUE;
    DBGPRINT( ( "have set tx exit flag to true\n" ) );
    KeWaitForSingleObject( &TxThreadExiting, Executive, KernelMode, FALSE, NULL );
    DBGPRINT( ( "<==TxThreadClose\n" ) );
}

void RxThreadClose()
{
    DBGPRINT( ( "==>RxThreadClose Routine\n" ) );
    RxExitNow = TRUE;
    DBGPRINT( ( "have set rx exit flag to true\n" ) );
    KeWaitForSingleObject( &RxThreadExiting, Executive, KernelMode, FALSE, NULL );
    DBGPRINT( ( "<==RxThreadClose\n" ) );
}

void TxThreadFunc( IN PVOID Context )
{
    LARGE_INTEGER                    intval;

    intval.QuadPart = -1i64 * 10i64 * 1000000i64;

    KeSetPriorityThread( KeGetCurrentThread(), LOW_REALTIME_PRIORITY );

    while( 1 )
    {
        KeDelayExecutionThread( KernelMode, FALSE, &intval );

        DbgPrint( "this is TX thread running\n" );

        if( TxExitNow )
        {
            DBGPRINT( ( "TX thread exit flag indicated\n" ) );
            break;
        }
    }

    if( TxThreadObjPointer )
    {
        DBGPRINT( ( "ObDereferenceObject the tx thread pointer\n" ) );
        ObDereferenceObject( &TxThreadObjPointer );
        DBGPRINT( ( "set the tx thread pointer to null\n" ) );
        TxThreadObjPointer = NULL;
    }

    DBGPRINT( ( "set tx thread exit event\n" ) );
    KeSetEvent( &TxThreadExiting, 0, FALSE );
    PsTerminateSystemThread( STATUS_SUCCESS );
}

void RxThreadFunc( IN PVOID Context )
{
    LARGE_INTEGER                    intval;
    
    intval.QuadPart = -1i64 * 10i64 * 1000000i64;

    KeSetPriorityThread( KeGetCurrentThread(), LOW_REALTIME_PRIORITY );

    while( 1 )
    {
        KeDelayExecutionThread( KernelMode, FALSE, &intval );

        DbgPrint( "this is RX thread running\n" );
        
        if( RxExitNow )
        {
            DBGPRINT( ( "RX thread exit flag indicated\n" ) );
            break;
        }
    }

    if( RxThreadObjPointer )
    {
        DBGPRINT( ( "ObDereferenceObject the rx thread pointer\n" ) );
        ObDereferenceObject( &RxThreadObjPointer );
        DBGPRINT( ( "set the rx thread pointer to null\n" ) );
        RxThreadObjPointer = NULL;
    }

    DBGPRINT( ( "set tx thread exit event\n" ) );
    KeSetEvent( &RxThreadExiting, 0, FALSE );
    PsTerminateSystemThread( STATUS_SUCCESS );
}
我在MPInitialize例程中用如下代码启动了这两个线程
if( TxThreadObjPointer == NULL && RxThreadObjPointer == NULL )
{
               TxThreadInit();
               RxThreadInit();
}
驱动卸载的时候进行卸载线程的工作,结果我把线程关闭放在MPHalt或者PtUnload的时候都会有蓝屏出现,说是irql_not_less_or_equal。用sofeice跟的时候发现兰屏停住的地方就在卸载过程中,但不在线程关闭的代码里面。
我卸载部分的代码如下
if( TxThreadObjPointer && RxThreadObjPointer )
{
               TxThreadClose();
               RxThreadClose();
}
请教一下,线程的卸载应该放在什么地方呢?应该怎么解决passthru下的线程卸载呢?另外,我如果不开线程,则驱动的安装和卸载都没有问题。
redf0x
驱动牛犊
驱动牛犊
  • 注册日期2007-01-06
  • 最后登录2010-03-12
  • 粉丝0
  • 关注0
  • 积分24分
  • 威望43点
  • 贡献值1点
  • 好评度2点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2007-01-23 15:34
Re:passthru中系统线程启动没有问题,但在驱动卸载时就成问题了,请教诸位有什么方法
不好意思,你的问题我解决不了,但是想问问怎么样用SoftICE在DriverEntry的地方设置断点呢
就是系统自动加载驱动(非自己写程序来启动驱动),那样断点设置了好像没用?
ProPlayboy
驱动大牛
驱动大牛
  • 注册日期2005-07-07
  • 最后登录2022-02-15
  • 粉丝0
  • 关注0
  • 积分1016分
  • 威望811点
  • 贡献值0点
  • 好评度719点
  • 原创分0分
  • 专家分0分
  • 社区居民
板凳#
发布于:2007-02-16 18:24
我现在不用Softice了...使用Windbg更爽!在Windbg中,命令 bu xxx!DriverEntry    
xxx是你的驱动名,不带.sys
人不靓仔心灵美,版头不正红花仔!
mouse.mouth
驱动牛犊
驱动牛犊
  • 注册日期2007-01-23
  • 最后登录2007-03-08
  • 粉丝0
  • 关注0
  • 积分120分
  • 威望13点
  • 贡献值0点
  • 好评度12点
  • 原创分0分
  • 专家分0分
地板#
发布于:2007-02-28 11:34
呵呵,楼上大哥,你说的windbg我很感兴趣,但不知哪里有关于windbg调试内核驱动的资料?另外请教你一下你用windbg调试内核驱动的时候需要用串口线和两台机器吗?能否像softice一样就在一台机器上呢?
游客

返回顶部