Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 469
Nathan Fellman Avatar asked Apr 26 '09 11:04

Nathan Fellman


1 Answers

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:
like image 75
Nathan Fellman Avatar answered Sep 17 '22 17:09

Nathan Fellman