Best or fastest way to determine which CPU is running (SMP)

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
Js2xxx
Member
Member
Posts: 48
Joined: Sat Dec 31, 2016 1:43 am
Libera.chat IRC: wrgq
Location: China

Re: Best or fastest way to determine which CPU is running (S

Post by Js2xxx »

I prefer rdtscp. IA32_TSC_AUX is only a model specific register. Reading it can be faster than memory access.

EDIT: And when booting, you can store APIC ID in IA32_TSC_AUX so that you can read it faster later.
Doing steadfastly, or doing nil.
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Best or fastest way to determine which CPU is running (S

Post by xenos »

samiam95124 wrote:Task register, to unique TSS, (which is usually per core in any case), then to a value in the TSS. The TSS span is set in the descriptor, which implies that you can make it longer than needed to store goodies in the TSS data, so voila, there is a place for a core number using a simple offset. You will have more than one thread per core, but in most implementations you don't use the hardware task switching but rather use the (fake) tss to bounce the stack pointer. Thus all threads use the same TSS, and thus all threads on the same core yield the same core number from the TSS.

Other idea: since you are not actually using the TSS to store registers, you can repurpose those fields so that you are not wasting the whole TSS per core.
If you are reading the task register, why not simply use the index part of the TSS selector itself as a core ID, since it is unique per core? This way you don't need another memory access to the TSS. You can use sequential selectors, and so your core ID function becomes something like

Code: Select all

(ReadTaskRegister() - FIRST_TSS) >> 3
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
Post Reply