Finally, we got to write a test function to get the apic_id, and It feels to run correctly. Now, we're trying to implement another function, to transform the apic_id in the kernel cpuid.
I attach the current test function to get apic_id with its test file 2018-06-15 19:36 GMT+02:00 Almudena Garcia <liberamenso10...@gmail.com>: > Yes, I saw this. > > When I test It in Linux, I used a dual core computer. But the snippet > returns "8" number. > > Now I know the reason ;) > > 2018-06-15 19:25 GMT+02:00 Richard Braun <rbr...@sceen.net>: > >> On Fri, Jun 15, 2018 at 07:18:55PM +0200, Richard Braun wrote: >> > On Fri, Jun 15, 2018 at 06:27:21PM +0200, Almudena Garcia wrote: >> > > I'm trying to define the cpu_number() in multiprocessor. >> > > >> > > To do this, I tried to use CPUID assembly x86 instruction, to get the >> CPU >> > > SMP number. >> > > The function, in C, is this: >> > > >> > > static inline char smp_processor_id(void) { >> > > char apic_id = 0; >> > > asm("mov $1, %%eax\n\t" >> > > "cpuid\n\t" >> > > "mov %%bh, %0\n\t" : "=g" (apic_id)); >> > > return apic_id; >> > > } >> > > >> > > In Linux, after executing this in a test source, It returns '8' >> > > >> > > But, when I try to execute It in Hurd, It shows a segmentation fault. >> > > >> > > I attach the test source file >> > > >> > > >> > > Can you help me? >> >> Also note here that you're confusing the APIC ID, a device identifier at >> the hardware level, with the CPU ID, a processor identifier at the kernel >> level. On some machines, the APIC ID may be higher than or equal to the >> maximum number of processor installed. >> >> -- >> Richard Braun >> > >
#include <stdio.h> static inline int cpu_number(void) { long eax = 1, ebx = 0, ecx = 0, edx = 0; char apic_id = 0; int cpun = -1; asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (eax)); apic_id = (char) (ebx >> 24) & 0xff; printf("eax = %lx, ebx = %lx, ecx = %lx, edx = %lx\n", eax, ebx, ecx, edx); printf("apic_id = %lx\n", apic_id); return cpun; } int main (void) { printf("%d\n", cpu_number()); return 0; }