diff options
author | Abel Romero Pérez <romeroperezabel@gmail.com> | 2018-06-28 02:09:24 -0400 |
---|---|---|
committer | Abel Romero Pérez <romeroperezabel@gmail.com> | 2018-06-28 02:09:24 -0400 |
commit | 371df36e565f4408737948ccc3d25acf2e1ccb57 (patch) | |
tree | 81383a8771d3aea315f8ce7d7320de632ad94318 /cpu_number.c | |
parent | 0d490ef21c156907f3f26a6cdc00842f462a877a (diff) |
moved cpu_number() to cpu_number.c, created CPU_NUMBER ASM macro
Diffstat (limited to 'cpu_number.c')
-rw-r--r-- | cpu_number.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/cpu_number.c b/cpu_number.c new file mode 100644 index 00000000..0b2e2f1c --- /dev/null +++ b/cpu_number.c @@ -0,0 +1,28 @@ +int apic2kernel[255]; +int cpu_number_start = 0, cpu_number_counter = 0; + +int cpu_number(void) { + int eax = 1, ebx = 0, ecx = 0, edx = 0; + unsigned int i = 0; + int apic_id = 0; + + if (!cpu_number_start) { + for (i = 0; i < 255; i++) { + apic2kernel[i] = -1; + } + cpu_number_start = 1; + } + + asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (eax)); + apic_id = (char) (ebx >> 24) & 0xff; + //printf("apic_id = %d\n", apic_id); + + if (apic2kernel[apic_id] != -1) { + return apic2kernel[apic_id]; + } else { + apic2kernel[apic_id] = cpu_number_counter; + cpu_number_counter++; + } + + return apic2kernel[apic_id]; +} |