From f9bc3d6248f5d3192bbe863db0468b6b5202caf1 Mon Sep 17 00:00:00 2001 From: Abel Romero PĂ©rez Date: Thu, 28 Jun 2018 00:04:07 -0400 Subject: initial commit --- .gitignore | 3 +++ i386/i386/cpu_number.S | 4 ++++ kern/cpu_number.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 i386/i386/cpu_number.S create mode 100644 kern/cpu_number.c diff --git a/.gitignore b/.gitignore index 92fc6e7f..1a75fccc 100644 --- a/.gitignore +++ b/.gitignore @@ -50,3 +50,6 @@ build-aux/ /doc/mach.toc /doc/mach.tp /doc/mach.vr +/build +Makefile.am.orig +Makefile.am.rej diff --git a/i386/i386/cpu_number.S b/i386/i386/cpu_number.S new file mode 100644 index 00000000..95036b8b --- /dev/null +++ b/i386/i386/cpu_number.S @@ -0,0 +1,4 @@ +.macro CPU_NUMBER reg + call cpu_number + movl %eax, \reg +.endm diff --git a/kern/cpu_number.c b/kern/cpu_number.c new file mode 100644 index 00000000..324ed752 --- /dev/null +++ b/kern/cpu_number.c @@ -0,0 +1,28 @@ +int apic2kernel[255]; +int cpu_number_start = 0, cpu_number_counter = 0; + +static 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]; +} -- cgit v1.2.3