Assembly instructions to find how many threads are enabled in a multi-core system

梦想的初衷 提交于 2019-11-27 22:40:33

问题


I'm working on a bare-bones system in which I need to determine sometime after boot how many cores and threads are enabled, so that I can send them SIPI events. I also want each thread to know which thread it is.

For instance, in a single-core configuration with HT enabled, we have (for instance, Intel Atom):

thread 0 --> core 0 thread 0
thread 1 --> core 0 thread 1

While in a dual-core configuration with no HT we have (for instance, Core 2 Duo):

thread 0 --> core 0 thread 0
thread 1 --> core 1 thread 0

What's the best way to determine this?

Edit: I found how each thread can find which thread it is. I still haven't found how to determine how many cores there are.


回答1:


I researched it a bit and came up with these facts. cpuid with eax = 01h returns the APIC ID in EBX[31:24] and HT enable in EDX[28].

This code should do the work:

    ; 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:


来源:https://stackoverflow.com/questions/790682/assembly-instructions-to-find-how-many-threads-are-enabled-in-a-multi-core-syste

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!