Files
towos/kernel/arch/x86_64/boot.S
T

191 lines
2.4 KiB
ArmAsm

.section .multiboot
.align 8
multiboot_header:
.long 0xE85250D6
.long 0
.long multiboot_header_end - multiboot_header
.long -(0xE85250D6 + 0 + (multiboot_header_end - multiboot_header))
.short 0
.short 0
.long 8
multiboot_header_end:
.section .text
.code32
.global _start
.type _start, @function
.extern kmain
_start:
cli
cld
movl %eax, multiboot_magic
movl %ebx, multiboot_info
movl $stack_top, %esp
movl $pdpt_table, %eax
orl $0x3, %eax
movl %eax, pml4_table
movl $pd_table, %eax
orl $0x3, %eax
movl %eax, pdpt_table
xorl %ecx, %ecx
movl $pd_table, %edi
.fill_pd:
movl %ecx, %eax
shll $21, %eax
orl $0x83, %eax
movl %eax, (%edi)
addl $8, %edi
incl %ecx
cmpl $512, %ecx
jne .fill_pd
movl $pml4_table, %eax
movl %eax, %cr3
movl %cr4, %eax
orl $0x20, %eax
movl %eax, %cr4
movl $0xC0000080, %ecx
rdmsr
orl $0x100, %eax
wrmsr
movl %cr0, %eax
orl $0x80000001, %eax
movl %eax, %cr0
lgdt gdt_descriptor
ljmp $0x08, $long_mode_entry
.code64
enable_sse:
movq %cr0, %rax
andw $0xFFFB, %ax
orw $0x0002, %ax
movq %rax, %cr0
movq %cr4, %rax
orw $0x600, %ax
movq %rax, %cr4
ret
long_mode_entry:
movw $0x10, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %ss
movw %ax, %fs
movw %ax, %gs
movq $stack_top, %rsp
andq $-16, %rsp
xorq %rbp, %rbp
call run_global_constructors
call enable_sse
call kmain
.hang:
cli
hlt
jmp .hang
.extern __init_array_start
.extern __init_array_end
run_global_constructors:
pushq %rbp
movq %rsp, %rbp
movq $__init_array_start, %rax
.constructor_loop:
cmpq $__init_array_end, %rax
jae .constructors_done
movq (%rax), %rdx
call *%rdx
addq $8, %rax
jmp .constructor_loop
.constructors_done:
popq %rbp
ret
.align 8
gdt:
.quad 0x0000000000000000
.quad 0x00AF9A000000FFFF
.quad 0x00AF92000000FFFF
gdt_end:
gdt_descriptor:
.word gdt_end - gdt - 1
.long gdt
.section .bss
.align 8
.global multiboot_magic
multiboot_magic:
.quad 0
.global multiboot_info
multiboot_info:
.quad 0
.align 4096
pml4_table:
.skip 4096
.align 4096
pdpt_table:
.skip 4096
.align 4096
pd_table:
.skip 4096
.align 16
stack_bottom:
.skip 16384
stack_top: