Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
559 views
in Technique[技术] by (71.8m points)

x86 - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...