Get Number Of Cpu's

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.
Post Reply
JoeTheProgrammer
Member
Member
Posts: 48
Joined: Mon Aug 13, 2007 2:30 pm

Get Number Of Cpu's

Post by JoeTheProgrammer »

Is there a way to do this in C using inline assembly, if so, how?

Thanks,
Joseph
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: Get Number Of Cpu's

Post by 01000101 »

easily? 8)

You can parse the SMP/xAPIC tables and that will tell you info about all processors (logical/physical) that are attached to the mobo.

Also, IIRC, there is a CPUID function that returns the logical processor count. Check the AMD CPUID specs, I think it gets talked about there.
User avatar
djsilence
Member
Member
Posts: 70
Joined: Wed Oct 01, 2008 11:18 am
Location: Ukraine, Kiev
Contact:

Re: Get Number Of Cpu's

Post by djsilence »

I've got this on one of this forum's pages:

Code: Select all

uint16 cpu_number;
asm { 
   STR AX
   MOV [cpu_number], AX
}
cpu_number = (cpu_number - index_of_first_tss) >> 3;
and this:

Code: Select all

#define MAX_CPU 12
unsigned int num_cpus = 1;
static unsigned int cpu_ids[MAX_CPU];
#define APIC_ID            ((unsigned int *) (apic + 0x020))

static volatile unsigned int apic_read (unsigned int *addr)

{

    return *addr;

}

unsigned ArchThisCpu(void)

{

     if (num_cpus == 1)

          return 0;

     else

          return cpu_ids[(apic_read(APIC_ID) & 0xffffffff) >> 24];

}
I think the second is more useable (maybe, just for me :) ). The forum's page is called "multiprocessor stuff".

Hope, it helps.
Daniel.
Don't think a ****, but in ukrainian schools English is TOO BAD!
JoeTheProgrammer
Member
Member
Posts: 48
Joined: Mon Aug 13, 2007 2:30 pm

Re: Get Number Of Cpu's

Post by JoeTheProgrammer »

Ok, thanks for the help everyone, I appreciate it.

Joseph
Ferrarius
Member
Member
Posts: 69
Joined: Sun Oct 28, 2007 4:10 pm

Re: Get Number Of Cpu's

Post by Ferrarius »

The intel manuals have plenty of pages on CPUID as well,
Modular Interface Kernel With a lot of bugs ;)
gmoney
Member
Member
Posts: 106
Joined: Mon Jan 03, 2005 12:00 am
Location: Texas, Usa

Re: Get Number Of Cpu's

Post by gmoney »

ckeck this code out via svn http://sourceforge.net/projects/jeko i used the cpu code from it. real easy to implement
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Get Number Of Cpu's

Post by Combuster »

The only correct way to go about this is by parsing the ACPI, or if that isn't present, MP tables.

Problems with the other versions:
The code snippets by djsilence is a way to get the number of the current running processor, of which the first assumes that all the processors have been started up by the OS already. Since you'll probably be starting off CPU #0 you know nothing about the presence of other ones.

CPUID can only tell you the processor count for the current die - i.e. it is accurate only if you have one multicore chip. If you have more physical processors then the processor will only tell you what's on itself, and miss the other processor. And the relevant CPUID page is completely missing on older processors even though they can just as well be SMP.

I haven't looked at Jeko's code, but I expect it to use the tables.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply