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/


Implementing KeGetCurrentProcessorNumberEx using cpuid instruction...

Prasad_Dabak-2Prasad_Dabak-2 Member - All Emails Posts: 18
Hello,

I see that KeGetCurrentProcessorNumberEx(NULL) returns KPRCB->Number field. So operating system has already setup the KPRCB and KeGetCurrentProcessorNumberEx is just an accessor to fetch KPRCB->Number.

Is it possible to implement the same function using cpuid instruction? I read cpuid documentation at various places, however, it's still unclear to me as to how to arrive at current logical CPU using the cpuid instruction. Any suggestions?

Thanks.
-Prasad

Comments

  • Slava_ImameevSlava_Imameev Member Posts: 480
    CPUID on x86 and x86-64 reports the current core capabilities, it has nothing to do with a core number perceived by Windows kernel. Nothing prevents you from developing your own algorithm for cores identification but you should not expect that these numbers will be the same as reported by KeGetCurrentProcessorNumberEx unless you disassemble the current Windows kernel/HAL.

    I would advise you to search Linux kernel for cpu_core_id if you want to know how real world kernel infer CPU core numbers for x86/amd64 architecture.
  • Doron_HolanDoron_Holan Member - All Emails Posts: 10,490
    What bigger problem are you trying to solve?



    On Tue, Aug 16, 2016 at 2:01 AM -0700, "[email protected]" > wrote:

    Hello,

    I see that KeGetCurrentProcessorNumberEx(NULL) returns KPRCB->Number field. So operating system has already setup the KPRCB and KeGetCurrentProcessorNumberEx is just an accessor to fetch KPRCB->Number.

    Is it possible to implement the same function using cpuid instruction? I read cpuid documentation at various places, however, it's still unclear to me as to how to arrive at current logical CPU using the cpuid instruction. Any suggestions?

    Thanks.
    -Prasad


    ---
    NTDEV is sponsored by OSR

    Visit the list online at:

    MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers!
    Details at

    To unsubscribe, visit the List Server section of OSR Online at
    d
  • Amritanshu_JohriAmritanshu_Johri Member Posts: 75
    @prasad I think you can use local apicid as a hint. It can be queried with
    cpuid as well. I am curious though :)

    On Tue, Aug 16, 2016 at 5:14 PM, wrote:

    > CPUID on x86 and x86-64 reports the current core capabilities, it has
    > nothing to do with a core number perceived by Windows kernel. Nothing
    > prevents you from developing your own algorithm for cores identification
    > but you should not expect that these numbers will be the same as reported
    > by KeGetCurrentProcessorNumberEx unless you disassemble the current Windows
    > kernel/HAL.
    >
    > I would advise you to search Linux kernel for cpu_core_id if you want to
    > know how real world kernel infer CPU core numbers for x86/amd64
    > architecture.
    >
    > ---
    > NTDEV is sponsored by OSR
    >
    > Visit the list online at: showlists.cfm?list=ntdev>
    >
    > MONTHLY seminars on crash dump analysis, WDF, Windows internals and
    > software drivers!
    > Details at
    >
    > To unsubscribe, visit the List Server section of OSR Online at <
    > http://www.osronline.com/page.cfm?name=ListServer&gt;
    >
  • Prasad_Dabak-2Prasad_Dabak-2 Member - All Emails Posts: 18
    Thanks Slava for the suggestion.

    Doron, I have a piece of a self contained library code (no external Windows kernel or any references). This code needs to to maintain a data structure on a per logical CPU basis. On the public interface of this code, I can pass the CPU number that is returned by KeGetCurrentProcesssNumberEx function. However, I was looking for something that is implicitly available. Hence my question.

    Thanks.
    -Prasad
  • Prasad_Dabak-2Prasad_Dabak-2 Member - All Emails Posts: 18
    Amritanshu, I guess you are referring to the one to returned in EBX register (24-31 bits). That value doesn't match. It returns me 2 on logical processor 1. In one of the reference I saw that it needs to masked with a value returned in 4 bits in another call to arrive at logical CPU ID. However, I am not sure about it and as Slava mentioned, it's probably just an OS representation.

    Thanks.
    -Prasad
  • Doron_HolanDoron_Holan Member - All Emails Posts: 10,490
    If you don't want to pass it in, have your portable library call MyPlatformProcNum() and then have each platform supply an implementation that calls the supported API and not assume an implementation detail

    Get Outlook for Android



    On Tue, Aug 16, 2016 at 7:01 AM -0700, "[email protected]" > wrote:

    Thanks Slava for the suggestion.

    Doron, I have a piece of a self contained library code (no external Windows kernel or any references). This code needs to to maintain a data structure on a per logical CPU basis. On the public interface of this code, I can pass the CPU number that is returned by KeGetCurrentProcesssNumberEx function. However, I was looking for something that is implicitly available. Hence my question.

    Thanks.
    -Prasad


    ---
    NTDEV is sponsored by OSR

    Visit the list online at:

    MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers!
    Details at

    To unsubscribe, visit the List Server section of OSR Online at
    d
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

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 30 Mar 2020 OSR Seminar Space
Developing Minifilters 15 Jun 2020 LIVE ONLINE
Writing WDF Drivers 22 June 2020 LIVE ONLINE
Internals & Software Drivers 28 Sept 2020 Dulles, VA