用于查找多核系统中启用了多少个线程的汇编指令

Nat*_*man 5 x86 assembly multicore

我正在开发一个简单的系统,我需要在启动后确定启用了多少内核和线程,以便我可以向它们发送SIPI事件.我还希望每个线程都知道它是哪个线程.

例如,在启用了HT的单核配置中,我们有(例如,Intel Atom):

thread 0 --> core 0 thread 0
thread 1 --> core 0 thread 1
Run Code Online (Sandbox Code Playgroud)

虽然在没有HT的双核配置中我们有(例如,Core 2 Duo):

thread 0 --> core 0 thread 0
thread 1 --> core 1 thread 0
Run Code Online (Sandbox Code Playgroud)

确定这个的最佳方法是什么?

编辑: 我发现每个线程如何找到它所在的线程.我还没有找到如何确定有多少核心.

Nat*_*man 7

我研究了一下,并提出了这些事实. cpuidwith eax = 01h返回APIC ID in EBX[31:24]和HT enable in EDX[28].

这段代码应该做的工作:

    ; this code will put the thread id into ecx
    ; and the core id into ebx

    mov eax, 01h
    cpuid
    ; get APIC ID from EBX[31:24]
    shr ebx, 24
    and ebx, 0ffh; not really necessary but makes the code nice

    ; get HT enable bit from EDX[28]
    test edx, 010000000h
    jz ht_off

    ; HT is on
    ; bit 0 of EBX is the thread
    ; bits 7:1 are the core
    mov ecx, ebx
    and ecx, 01h
    shr ebx, 1

    jmp done

ht_off:
    ; the thread is always 0
    xor ecx, ecx

done:
Run Code Online (Sandbox Code Playgroud)