Architectural Support: x86_64 (#37)
[lunaix-os.git] / lunaix-os / arch / x86 / boot / x86_64 / prologue64.S
diff --git a/lunaix-os/arch/x86/boot/x86_64/prologue64.S b/lunaix-os/arch/x86/boot/x86_64/prologue64.S
new file mode 100644 (file)
index 0000000..06b2b94
--- /dev/null
@@ -0,0 +1,73 @@
+/* 高半核入口点 - 0xC0000000 */
+
+#define __ASM__
+#include <sys/abi.h>
+#include <sys/failsafe.h>
+
+.section .bss.kstack
+    .global __kinit_stack_end
+    
+    .align 16
+    __kinit_stack_end:
+        .skip 2048, 0
+    __kinit_stack_top:
+
+
+.section .text
+    .global hhk_entry_
+    hhk_entry_:
+
+        movq $__kinit_stack_top, %rsp
+        andq $stack_alignment, %rsp
+
+        movq $__kinit_stack_end, %rax
+        movl $STACK_SANITY, (%rax)
+        movl $STACK_SANITY, 4(%rax)
+        movl $STACK_SANITY, 8(%rax)
+        movl $STACK_SANITY, 12(%rax)
+
+        andq $stack_alignment, %rsp
+        subq $16, %rsp
+
+        # replace the temporary gdt
+        call _init_gdt
+
+        movq $_gdt, 2(%rsp)
+        movw _gdt_limit, %ax
+        movw %ax, (%rsp)
+        lgdt (%rsp)
+        
+        # do a far jump to switch cs
+        pushq $KCODE_SEG
+        pushq $_after_gdt
+        retfq
+
+    _after_gdt:
+
+        # initialize segment registers
+        # we will not touch them again in x86_64
+        movw $KDATA_SEG, %cx
+        movw %cx, %es
+        movw %cx, %ds
+        movw %cx, %ss
+
+        # perform arch-specific initialization before diving into kernel
+        call arch_preinit
+
+        # 加载 IDT
+        movq $_idt, 2(%rsp)
+        movw _idt_limit, %ax
+        movw %ax, (%rsp)
+        lidt (%rsp)
+
+        /* 加载TSS段选择器 */
+        movw $TSS_SEG, %ax
+        ltr %ax
+
+        xorq %rbp, %rbp
+        movq $bhctx_buffer, %rdi       # mb_parser.c
+        call kernel_bootstrap
+
+    1:
+        hlt
+        jmp 1b
\ No newline at end of file