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

Sept/Oct 2019 Issue of The NT Insider available


Download PDF here: http://insider.osr.com/2019/ntinsider_2019_01.pdf

It’s a particularly BIG issue, too: 40 pages of technical goodness, ranging from WDF to Minifilters. Check it out.
Before Posting...
Please check out the Community Guidelines in the Announcements and Administration Category.

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,444
    What bigger problem are you trying to solve?



    On Tue, Aug 16, 2016 at 2:01 AM -0700, "xxxxx@icloud.com" > 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: 73
    @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>;
    >
  • 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,444
    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, "xxxxx@icloud.com" > 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
Writing WDF Drivers 21 Oct 2019 OSR Seminar Space & ONLINE
Internals & Software Drivers 18 Nov 2019 Dulles, VA
Kernel Debugging 30 Mar 2020 OSR Seminar Space
Developing Minifilters 27 Apr 2020 OSR Seminar Space & ONLINE