Kernel: Fix boot code for x86_32

Boot assembly now initializes processor and jumps to kernel
This commit is contained in:
Bananymous 2024-03-26 13:25:22 +02:00
parent 84ef2161a1
commit af050cc729
1 changed files with 52 additions and 20 deletions

View File

@ -53,6 +53,7 @@ bananboot_start:
bananboot_end: bananboot_end:
.section .bss, "aw", @nobits .section .bss, "aw", @nobits
.align 4096
boot_stack_bottom: boot_stack_bottom:
.skip 4096 * 4 .skip 4096 * 4
boot_stack_top: boot_stack_top:
@ -68,11 +69,23 @@ bananboot_end:
.section .data .section .data
# Map first GiB to 0x00000000 and 0xC0000000
.align 32
boot_pdpt:
.long V2P(boot_pd) + (PG_PRESENT)
.long 0
.quad 0
.quad 0
.long V2P(boot_pd) + (PG_PRESENT)
.long 0
.align 4096 .align 4096
boot_pml4:
boot_pdpt_lo:
boot_pdpt_hi:
boot_pd: boot_pd:
.set i, 0
.rept 512
.long i + (PG_PAGE_SIZE | PG_READ_WRITE | PG_PRESENT)
.long 0
.set i, i + 0x200000
.endr
boot_gdt: boot_gdt:
.quad 0x0000000000000000 # null descriptor .quad 0x0000000000000000 # null descriptor
@ -106,9 +119,25 @@ has_cpuid:
testl $0x00200000, %eax testl $0x00200000, %eax
ret ret
has_pae:
movl $0, %eax
cpuid
testl $(1 << 6), %edx
ret
has_sse:
movl $1, %eax
cpuid
testl $(1 << 25), %edx
ret
check_requirements: check_requirements:
call has_cpuid call has_cpuid
jz .exit jz .exit
call has_pae
jz .exit
call has_sse
jz .exit
ret ret
.exit: .exit:
jmp system_halt jmp system_halt
@ -126,16 +155,16 @@ enable_sse:
initialize_paging: initialize_paging:
# enable PAE # enable PAE
movl %cr4, %ecx movl %cr4, %ecx
orl $0x20, %ecx orl $(1 << 5), %ecx
movl %ecx, %cr4 movl %ecx, %cr4
# set address of paging structures # load page tables
movl $V2P(boot_pml4), %ecx movl $V2P(boot_pdpt), %ecx
movl %ecx, %cr3 movl %ecx, %cr3
# enable paging # enable paging
movl %cr0, %ecx movl %cr0, %ecx
orl $0x80000000, %ecx orl $(1 << 31), %ecx
movl %ecx, %cr0 movl %ecx, %cr0
ret ret
@ -145,30 +174,30 @@ initialize_paging:
_start: _start:
cli; cld cli; cld
# Initialize stack and multiboot info # save bootloader magic and info
movl %eax, V2P(bootloader_magic) movl %eax, V2P(bootloader_magic)
movl %ebx, V2P(bootloader_info) movl %ebx, V2P(bootloader_info)
# load boot stack
movl $V2P(boot_stack_top), %esp movl $V2P(boot_stack_top), %esp
call check_requirements # load boot GDT
call enable_sse
call initialize_paging
# flush gdt
lgdt V2P(boot_gdtr) lgdt V2P(boot_gdtr)
ljmpl $0x08, $V2P(gdt_flush) ljmpl $0x08, $V2P(gdt_flush)
gdt_flush: gdt_flush:
# set correct segment registers
movw $0x10, %ax movw $0x10, %ax
movw %ax, %ds movw %ax, %ds
movw %ax, %ss movw %ax, %ss
movw %ax, %es movw %ax, %es
# move stack pointer to higher half # do processor initialization
movl %esp, %esp call check_requirements
addl $KERNEL_OFFSET, %esp call enable_sse
call initialize_paging
# load higher half stack pointer
movl $boot_stack_top, %esp
# jump to higher half # jump to higher half
leal higher_half, %ecx leal higher_half, %ecx
@ -181,9 +210,11 @@ higher_half:
# call to the kernel itself (clear ebp for stacktrace) # call to the kernel itself (clear ebp for stacktrace)
xorl %ebp, %ebp xorl %ebp, %ebp
movl V2P(bootloader_magic), %edi subl $8, %esp
movl V2P(bootloader_info), %esi pushl bootloader_info
pushl bootloader_magic
call kernel_main call kernel_main
addl $16, %esp
# call global destructors # call global destructors
call _fini call _fini
@ -200,6 +231,7 @@ system_halt:
.code16 .code16
.global ap_trampoline .global ap_trampoline
ap_trampoline: ap_trampoline:
ud2
jmp 1f jmp 1f
.align 8 .align 8
ap_stack_ptr: ap_stack_ptr: