Windows System Software -- Consulting, Training, Development -- Unique Expertise, Guaranteed Results

Home NTDEV

Before Posting...

Please check out the Community Guidelines in the Announcements and Administration Category.

More Info on Driver Writing and Debugging


The free OSR Learning Library has more than 50 articles on a wide variety of topics about writing and debugging device drivers and Minifilters. From introductory level to advanced. All the articles have been recently reviewed and updated, and are written using the clear and definitive style you've come to expect from OSR over the years.


Check out The OSR Learning Library at: https://www.osr.com/osr-learning-library/


delay faster then KeDelayExecutionThread

daniel02daniel02 Member Posts: 15

is there way to make delay function that faster then KeDelayExecutionThread?

thats what im using right now

waitTime.QuadPart = -10000000; // 1 second
KeDelayExecutionThread(KernelMode, FALSE, &waitTime);

KeStallExecutionProcessor slow in my end

im thinking loop with rdtsc but i dont know how to convert it to x64 reading it through masm x64 its causing bsod

__declspec(naked) __forceinline void __fastcall Delay(unsigned int TicksC)
{
__asm
{
rdtsc
mov esi, eax
_loop:
rdtsc
sub eax, esi
cmp eax, ecx
jbe _loop
ret
}
}

Comments

  • Shane_CorbinShane_Corbin Member Posts: 307

    @daniel02 said:
    is there way to make delay function that faster then KeDelayExecutionThread?

    Can you elaborate on what your intentions are? What do you mean by "faster"?

  • daniel02daniel02 Member Posts: 15

    @Shane_Corbin said:

    @daniel02 said:
    is there way to make delay function that faster then KeDelayExecutionThread?

    Can you elaborate on what your intentions are? What do you mean by "faster"?

    im reading the rdtsc as main system clock so i need very good sleep function that have extreme nanoseconds KeDelayExecutionThread is the fastest on kernel ?

  • Shane_CorbinShane_Corbin Member Posts: 307

    @daniel02 said:
    im reading the rdtsc as main system clock so i need very good sleep function that have extreme nanoseconds KeDelayExecutionThread is the fastest on kernel ?

    It's still unclear what you're trying to do.

    rdtsc isn't a system clock. It's an instruction that returns the processor time stamp as a number of clock cycles since the last reset.

    Are you trying to sleep for exactly one second so you can see how many tick counts there are for the processor in one second?

    What's an extreme nanosecond?

    When you ask "is KeDelayExecutionThread() the fastest on kernel" are you wanting to know if it's return from delay is deterministic? It's hard to offer a recommendation without knowing more about what you're trying to do. I'm assuming you've found the documentation for KeDelayExecutionThread which clearly states accuracy of expiration times are limited by the granularity of the system clock.

  • daniel02daniel02 Member Posts: 15

    @Shane_Corbin said:

    @daniel02 said:
    im reading the rdtsc as main system clock so i need very good sleep function that have extreme nanoseconds KeDelayExecutionThread is the fastest on kernel ?

    It's still unclear what you're trying to do.

    rdtsc isn't a system clock. It's an instruction that returns the processor time stamp as a number of clock cycles since the last reset.

    Are you trying to sleep for exactly one second so you can see how many tick counts there are for the processor in one second?

    What's an extreme nanosecond?

    When you ask "is KeDelayExecutionThread() the fastest on kernel" are you wanting to know if it's return from delay is deterministic? It's hard to offer a recommendation without knowing more about what you're trying to do. I'm assuming you've found the documentation for KeDelayExecutionThread which clearly states accuracy of expiration times are limited by the granularity of the system clock.

    The behavior of the RDTSC instruction is implementation dependent. The TSC counts at a constant rate, but may be affected by power management events (such as frequency changes), depending on the processor implementation. If CPUID Fn8000_0007_EDX[TscInvariant] = 1, then the TSC rate is ensured to be invariant across all P-States, C-States, and stop-grant transitions (such as STPCLK Throttling); therefore, the TSC is suitable for use as a source of time. 
    

    so rdtsc can be used as source of time

    i found out KeDelayExecutionThread return to 100ns its still not fast enough then rdtsc loop i will try to found a way thanks for help !

  • Shane_CorbinShane_Corbin Member Posts: 307

    Best of luck. You may find the following information about high resolution time stamps helpful in your quest.
    https://learn.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps#direct-tsc-usage

  • daniel02daniel02 Member Posts: 15

    @Shane_Corbin said:
    Best of luck. You may find the following information about high resolution time stamps helpful in your quest.
    https://learn.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps#direct-tsc-usage

    thank you very much ZwYieldExecution fixed it

  • MBond2MBond2 Member Posts: 629

    For the sake of clarity, ZwYieldExecution is not a function that delays thread execution in the same way as KeDelayExecutionThread or KeStallExecutionProcessor. KeDelayExecutionThread is like UM Sleep and interacts with the scheduler to make this thread non-runnable for at least the time requested. KeStallExecutionProcessor runs a busy loop for approximately the time requested using a hardware specific algorithm.

    ZwYieldExecution interacts with the scheduler and might initiate a context switch if there is another thread ready to run. This is like the UM Sleep(0).

    Possibly the OP is avoiding a crash by using ZwYieldExecution to avoid a watchdog timer, but it is hard to know based on the lack of detail

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. Sign in or register to get started.

Upcoming OSR Seminars
OSR has suspended in-person seminars due to the Covid-19 outbreak. But, don't miss your training! Attend via the internet instead!
Kernel Debugging 13-17 May 2024 Live, Online
Developing Minifilters 1-5 Apr 2024 Live, Online
Internals & Software Drivers 11-15 Mar 2024 Live, Online
Writing WDF Drivers 26 Feb - 1 Mar 2024 Live, Online