summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien Zammit <damien@zamaudio.com>2022-10-01 10:35:20 +1000
committerDamien Zammit <damien@zamaudio.com>2022-10-01 11:09:25 +1000
commit1195a87bf344f0c990df1749bc79b3ba0e2fc02e (patch)
tree9a50c1203d41742cfea38752152214fd90c556d9
parente45542fbd6cbc0a815b0808f16cf4a8e87410f64 (diff)
Add cpuboot and cpu_number for i386
-rw-r--r--i386/i386/cpu_number.c34
-rw-r--r--i386/i386/cpuboot.S162
2 files changed, 196 insertions, 0 deletions
diff --git a/i386/i386/cpu_number.c b/i386/i386/cpu_number.c
new file mode 100644
index 00000000..8671788e
--- /dev/null
+++ b/i386/i386/cpu_number.c
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2022 Free Software Foundation, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <i386/cpu_number.h>
+#include <i386/apic.h>
+#include <i386/smp.h>
+
+int cpu_number(){
+ int apic_id = apic_get_current_cpu();
+ int num_cpus = smp_get_numcpus();
+
+ //printf("num_cpus: %d\n", num_cpus);
+
+ int kernel_id = 0;
+ while(apic_id != apic_get_cpu_apic_id(kernel_id) && kernel_id < num_cpus) kernel_id++;
+
+ if(kernel_id >= num_cpus) return 0;
+
+ return kernel_id;
+}
diff --git a/i386/i386/cpuboot.S b/i386/i386/cpuboot.S
new file mode 100644
index 00000000..a2a6adee
--- /dev/null
+++ b/i386/i386/cpuboot.S
@@ -0,0 +1,162 @@
+/*
+ * Copyright (c) 2022 Free Software Foundation, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <mach/machine/asm.h>
+#include <i386/i386asm.h>
+#include <i386/proc_reg.h>
+
+#define STACK_SIZE (4096 * 2)
+
+ .section .boot, "awx"
+
+ .align 0x10
+
+ .text
+
+idt_descr:
+ .word 0
+ .long 0
+
+.align 16
+ .word 0
+boot_gdt_descr:
+ .word 3*8+7
+ .long boot_gdt - KERNELBASE
+.align 16
+boot_gdt:
+ /* NULL segment */
+ .quad 0
+ /* boot CS = 8 . Code Segment*/
+ .word 0xffff /* Segment limit first 0-15 bits*/
+ .word (-KERNELBASE) & 0xffff /*Base first 0-15 bits*/
+ .byte ((-KERNELBASE) >> 16) & 0xff /*Base 16-23 bits */
+ .byte 0x9a /*Access byte */
+ .byte 0xcf /* High 4 bits */
+ .byte ((-KERNELBASE) >> 24) & 0xff /*Base 24-31 bits */
+ /* boot DS = 8 . Data segment*/
+ .word 0xffff /*Segment limit */
+ .word (-KERNELBASE) & 0xffff /*Base first 0-15 bits*/
+ .byte ((-KERNELBASE) >> 16) & 0xff
+ .byte 0x92 /*Access byte*/
+ .byte 0xcf /* High 4 bits */
+ .byte ((-KERNELBASE) >> 24) & 0xff /*Base 24-31 bits */
+
+ .bss
+ .p2align 4
+
+
+#define M(addr) (addr - apboot + 0x7000)
+
+ .text
+ .globl apboot, apbootend, stack_ptr
+ .align 0x10
+ .code16
+
+apboot:
+_apboot:
+ cli
+ xor %ax, %ax
+ mov %ax, %ds
+ mov %ax, %es
+ mov %ax, %ss
+
+ lgdt M(gdt_descr_tmp)
+
+ mov %cr0, %eax
+ or $0x1, %eax
+ mov %eax, %cr0
+
+ ljmp $0x8, $M(0f)
+
+0:
+ .code32
+
+ mov $0x10, %eax
+ mov %ax, %ds
+ mov %ax, %es
+ mov %ax, %ss
+
+ /* use segmentation to offset ourself. */
+ lidt idt_descr - KERNELBASE
+ lgdt boot_gdt_descr - KERNELBASE
+ ljmp $KERNEL_CS, $0f
+
+0:
+ movw $KERNEL_DS,%ax
+ movw %ax,%ds
+ movw %ax,%es
+ movw %ax,%ss
+
+ /* Load cpu stack */
+ movl (stack_ptr), %esp
+ addl $STACK_SIZE, %esp
+
+ /* Reset EFLAGS to a known state. */
+ pushl $0
+ popf
+
+ /* Enable paging in the cpu */
+ //call paging_setup
+
+ /*lock interrupts*/
+ cli
+
+ /* Finish the cpu configuration */
+ call cpu_ap_main
+
+ /*if the processor is not added to the kernel, stop it*/
+/* sti
+ nop
+halt:
+ hlt*/
+
+ /*if the processor continues, returns to halt*/
+ //jmp halt
+
+stack_ptr:
+ .long 0x0
+
+ .align 0x10
+gdt_descr_tmp:
+ .short 0x8*3 - 1
+ .long M(gdt_tmp)
+
+ .align 0x10
+
+gdt_tmp:
+ /* 0 */
+ .quad 0
+ /* boot CS = 0x8 */
+ .word 0xffff
+ .word 0x0000
+ .byte 0x00
+ .byte 0x9a
+ .byte 0xcf
+ .byte 0x00
+ /* boot DS = 0x10 */
+ .word 0xffff
+ .word 0x0000
+ .byte 0x00
+ .byte 0x92
+ .byte 0xcf
+ .byte 0x00
+
+
+_apbootend:
+apbootend:
+//nop
+//hlt