Page 1 of 1
Get Number Of Cpu's
Posted: Fri Oct 10, 2008 7:59 pm
by JoeTheProgrammer
Is there a way to do this in C using inline assembly, if so, how?
Thanks,
Joseph
Re: Get Number Of Cpu's
Posted: Fri Oct 10, 2008 9:14 pm
by 01000101
easily?
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.
Re: Get Number Of Cpu's
Posted: Sat Oct 11, 2008 10:46 am
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.
Re: Get Number Of Cpu's
Posted: Sat Oct 11, 2008 1:11 pm
by JoeTheProgrammer
Ok, thanks for the help everyone, I appreciate it.
Joseph
Re: Get Number Of Cpu's
Posted: Sat Oct 11, 2008 2:17 pm
by Ferrarius
The intel manuals have plenty of pages on CPUID as well,
Re: Get Number Of Cpu's
Posted: Mon Oct 20, 2008 5:01 pm
by gmoney
ckeck this code out via svn
http://sourceforge.net/projects/jeko i used the cpu code from it. real easy to implement
Re: Get Number Of Cpu's
Posted: Tue Oct 21, 2008 3:08 am
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.