2024-03-26 00:09:05 +02:00
|
|
|
.set PG_PRESENT, 1<<0
|
|
|
|
.set PG_READ_WRITE, 1<<1
|
|
|
|
.set PG_PAGE_SIZE, 1<<7
|
|
|
|
|
|
|
|
.set FB_WIDTH, 800
|
|
|
|
.set FB_HEIGHT, 600
|
|
|
|
.set FB_BPP, 32
|
|
|
|
|
|
|
|
#define KERNEL_OFFSET 0xC0000000
|
|
|
|
#define V2P(vaddr) ((vaddr) - KERNEL_OFFSET)
|
2022-12-16 00:46:10 +02:00
|
|
|
|
2024-03-26 00:09:05 +02:00
|
|
|
.code32
|
|
|
|
|
|
|
|
# multiboot2 header
|
|
|
|
.section .multiboot, "aw"
|
|
|
|
.align 8
|
|
|
|
multiboot2_start:
|
|
|
|
.long 0xE85250D6
|
2022-12-10 03:55:24 +02:00
|
|
|
.long 0
|
2024-03-26 00:09:05 +02:00
|
|
|
.long multiboot2_end - multiboot2_start
|
|
|
|
.long -(0xE85250D6 + (multiboot2_end - multiboot2_start))
|
|
|
|
|
|
|
|
# framebuffer tag
|
|
|
|
.align 8
|
|
|
|
.short 5
|
|
|
|
.short 0
|
|
|
|
.long 20
|
|
|
|
.long FB_WIDTH
|
|
|
|
.long FB_HEIGHT
|
|
|
|
.long FB_BPP
|
|
|
|
|
|
|
|
# legacy start
|
|
|
|
.align 8
|
|
|
|
.short 3
|
|
|
|
.short 0
|
|
|
|
.long 12
|
|
|
|
.long V2P(_start)
|
|
|
|
|
|
|
|
.align 8
|
|
|
|
.short 0
|
|
|
|
.short 0
|
|
|
|
.long 8
|
|
|
|
multiboot2_end:
|
|
|
|
|
|
|
|
.section .bananboot, "aw"
|
|
|
|
.align 8
|
|
|
|
bananboot_start:
|
|
|
|
.long 0xBABAB007
|
|
|
|
.long -(0xBABAB007 + FB_WIDTH + FB_HEIGHT + FB_BPP)
|
|
|
|
.long FB_WIDTH
|
|
|
|
.long FB_HEIGHT
|
|
|
|
.long FB_BPP
|
|
|
|
bananboot_end:
|
2022-11-13 01:04:10 +02:00
|
|
|
|
2023-01-10 17:50:24 +02:00
|
|
|
.section .bss, "aw", @nobits
|
2024-03-26 13:25:22 +02:00
|
|
|
.align 4096
|
2024-03-26 00:09:05 +02:00
|
|
|
boot_stack_bottom:
|
|
|
|
.skip 4096 * 4
|
|
|
|
boot_stack_top:
|
2023-03-01 21:21:08 +02:00
|
|
|
|
2024-03-26 00:09:05 +02:00
|
|
|
.global g_kernel_cmdline
|
|
|
|
g_kernel_cmdline:
|
|
|
|
.skip 4096
|
2023-01-10 17:50:24 +02:00
|
|
|
|
2024-03-26 00:09:05 +02:00
|
|
|
bootloader_magic:
|
|
|
|
.skip 8
|
|
|
|
bootloader_info:
|
|
|
|
.skip 8
|
2023-01-10 17:50:24 +02:00
|
|
|
|
2024-03-26 00:09:05 +02:00
|
|
|
.section .data
|
|
|
|
|
2024-03-26 13:25:22 +02:00
|
|
|
# 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
|
2024-03-26 00:09:05 +02:00
|
|
|
.align 4096
|
|
|
|
boot_pd:
|
2024-03-26 13:25:22 +02:00
|
|
|
.set i, 0
|
|
|
|
.rept 512
|
|
|
|
.long i + (PG_PAGE_SIZE | PG_READ_WRITE | PG_PRESENT)
|
|
|
|
.long 0
|
|
|
|
.set i, i + 0x200000
|
|
|
|
.endr
|
2023-03-01 21:21:08 +02:00
|
|
|
|
|
|
|
boot_gdt:
|
2024-03-26 00:09:05 +02:00
|
|
|
.quad 0x0000000000000000 # null descriptor
|
2023-03-01 21:21:08 +02:00
|
|
|
.quad 0x00CF9A000000FFFF # kernel code
|
|
|
|
.quad 0x00CF92000000FFFF # kernel data
|
|
|
|
boot_gdtr:
|
|
|
|
.short . - boot_gdt - 1
|
2024-03-26 00:09:05 +02:00
|
|
|
.long V2P(boot_gdt)
|
|
|
|
|
|
|
|
.global g_ap_startup_done
|
|
|
|
g_ap_startup_done:
|
|
|
|
.byte 0
|
|
|
|
.global g_ap_running_count
|
|
|
|
g_ap_running_count:
|
|
|
|
.byte 0
|
|
|
|
.global g_ap_stack_loaded
|
|
|
|
g_ap_stack_loaded:
|
|
|
|
.byte 0
|
|
|
|
|
|
|
|
.section .text
|
2023-03-01 21:21:08 +02:00
|
|
|
|
2023-01-10 17:50:24 +02:00
|
|
|
has_cpuid:
|
|
|
|
pushfl
|
|
|
|
pushfl
|
|
|
|
xorl $0x00200000, (%esp)
|
|
|
|
popfl
|
|
|
|
pushfl
|
|
|
|
popl %eax
|
|
|
|
xorl (%esp), %eax
|
|
|
|
popfl
|
2023-01-21 23:44:23 +02:00
|
|
|
testl $0x00200000, %eax
|
2023-01-10 17:50:24 +02:00
|
|
|
ret
|
|
|
|
|
2024-03-26 13:25:22 +02:00
|
|
|
has_pae:
|
|
|
|
movl $0, %eax
|
|
|
|
cpuid
|
|
|
|
testl $(1 << 6), %edx
|
|
|
|
ret
|
|
|
|
|
|
|
|
has_sse:
|
|
|
|
movl $1, %eax
|
|
|
|
cpuid
|
|
|
|
testl $(1 << 25), %edx
|
|
|
|
ret
|
|
|
|
|
2023-01-21 23:44:23 +02:00
|
|
|
check_requirements:
|
|
|
|
call has_cpuid
|
|
|
|
jz .exit
|
2024-03-26 13:25:22 +02:00
|
|
|
call has_pae
|
|
|
|
jz .exit
|
|
|
|
call has_sse
|
|
|
|
jz .exit
|
2023-01-21 23:44:23 +02:00
|
|
|
ret
|
2023-01-10 17:50:24 +02:00
|
|
|
.exit:
|
2023-01-21 23:44:23 +02:00
|
|
|
jmp system_halt
|
|
|
|
|
|
|
|
enable_sse:
|
|
|
|
movl %cr0, %eax
|
|
|
|
andw $0xFFFB, %ax
|
|
|
|
orw $0x0002, %ax
|
|
|
|
movl %eax, %cr0
|
|
|
|
movl %cr4, %eax
|
|
|
|
orw $0x0600, %ax
|
|
|
|
movl %eax, %cr4
|
2023-01-10 17:50:24 +02:00
|
|
|
ret
|
2022-11-13 01:04:10 +02:00
|
|
|
|
2023-01-22 00:47:25 +02:00
|
|
|
initialize_paging:
|
2023-01-10 17:50:24 +02:00
|
|
|
# enable PAE
|
|
|
|
movl %cr4, %ecx
|
2024-03-26 13:25:22 +02:00
|
|
|
orl $(1 << 5), %ecx
|
2023-01-10 17:50:24 +02:00
|
|
|
movl %ecx, %cr4
|
|
|
|
|
2024-03-26 13:25:22 +02:00
|
|
|
# load page tables
|
|
|
|
movl $V2P(boot_pdpt), %ecx
|
2023-01-10 17:50:24 +02:00
|
|
|
movl %ecx, %cr3
|
|
|
|
|
|
|
|
# enable paging
|
|
|
|
movl %cr0, %ecx
|
2024-03-26 13:25:22 +02:00
|
|
|
orl $(1 << 31), %ecx
|
2023-01-10 17:50:24 +02:00
|
|
|
movl %ecx, %cr0
|
|
|
|
|
2023-01-22 00:47:25 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.global _start
|
|
|
|
.type _start, @function
|
|
|
|
_start:
|
2024-03-26 00:09:05 +02:00
|
|
|
cli; cld
|
|
|
|
|
2024-03-26 13:25:22 +02:00
|
|
|
# save bootloader magic and info
|
2024-03-26 00:09:05 +02:00
|
|
|
movl %eax, V2P(bootloader_magic)
|
|
|
|
movl %ebx, V2P(bootloader_info)
|
|
|
|
|
2024-03-26 13:25:22 +02:00
|
|
|
# load boot stack
|
2024-03-26 00:09:05 +02:00
|
|
|
movl $V2P(boot_stack_top), %esp
|
2023-01-22 00:47:25 +02:00
|
|
|
|
2024-03-26 13:25:22 +02:00
|
|
|
# load boot GDT
|
2024-03-26 00:09:05 +02:00
|
|
|
lgdt V2P(boot_gdtr)
|
|
|
|
ljmpl $0x08, $V2P(gdt_flush)
|
|
|
|
gdt_flush:
|
2024-03-26 13:25:22 +02:00
|
|
|
# set correct segment registers
|
2024-03-26 00:09:05 +02:00
|
|
|
movw $0x10, %ax
|
|
|
|
movw %ax, %ds
|
|
|
|
movw %ax, %ss
|
|
|
|
movw %ax, %es
|
|
|
|
|
2024-03-26 13:25:22 +02:00
|
|
|
# do processor initialization
|
|
|
|
call check_requirements
|
|
|
|
call enable_sse
|
|
|
|
call initialize_paging
|
|
|
|
|
|
|
|
# load higher half stack pointer
|
|
|
|
movl $boot_stack_top, %esp
|
2024-03-26 00:09:05 +02:00
|
|
|
|
|
|
|
# jump to higher half
|
|
|
|
leal higher_half, %ecx
|
|
|
|
jmp *%ecx
|
|
|
|
|
|
|
|
higher_half:
|
|
|
|
# call global constuctors
|
2022-11-15 00:32:07 +02:00
|
|
|
call _init
|
2022-11-13 01:04:10 +02:00
|
|
|
|
2023-01-21 22:16:01 +02:00
|
|
|
# call to the kernel itself (clear ebp for stacktrace)
|
2023-01-16 21:50:50 +02:00
|
|
|
xorl %ebp, %ebp
|
2024-03-26 00:09:05 +02:00
|
|
|
|
2024-03-26 13:25:22 +02:00
|
|
|
subl $8, %esp
|
|
|
|
pushl bootloader_info
|
|
|
|
pushl bootloader_magic
|
2022-11-12 21:04:47 +02:00
|
|
|
call kernel_main
|
2024-03-26 13:25:22 +02:00
|
|
|
addl $16, %esp
|
2022-11-15 00:32:07 +02:00
|
|
|
|
2024-03-26 00:09:05 +02:00
|
|
|
# call global destructors
|
2023-01-21 22:16:01 +02:00
|
|
|
call _fini
|
|
|
|
|
2023-01-10 17:50:24 +02:00
|
|
|
system_halt:
|
2023-01-10 19:11:27 +02:00
|
|
|
xchgw %bx, %bx
|
2022-11-12 21:04:47 +02:00
|
|
|
cli
|
2023-01-10 17:50:24 +02:00
|
|
|
1: hlt
|
2022-11-12 21:04:47 +02:00
|
|
|
jmp 1b
|
2024-03-26 00:09:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
.section .ap_init, "ax"
|
|
|
|
|
|
|
|
.code16
|
|
|
|
.global ap_trampoline
|
|
|
|
ap_trampoline:
|
2024-03-26 13:25:22 +02:00
|
|
|
ud2
|
2024-03-26 00:09:05 +02:00
|
|
|
jmp 1f
|
|
|
|
.align 8
|
|
|
|
ap_stack_ptr:
|
|
|
|
.skip 4
|
|
|
|
1:
|
|
|
|
cli; cld
|
|
|
|
ljmpl $0x00, $ap_cs_clear
|
|
|
|
|
|
|
|
ap_cs_clear:
|
|
|
|
xorw %ax, %ax
|
|
|
|
movw %ax, %ds
|
|
|
|
|
|
|
|
# load ap gdt and enter protected mode
|
|
|
|
lgdt ap_gdtr
|
|
|
|
movl %cr0, %eax
|
|
|
|
orb $1, %al
|
|
|
|
movl %eax, %cr0
|
|
|
|
ljmpl $0x08, $ap_protected_mode
|
|
|
|
|
|
|
|
.code32
|
|
|
|
ap_protected_mode:
|
|
|
|
movw $0x10, %ax
|
|
|
|
movw %ax, %ds
|
|
|
|
movw %ax, %ss
|
|
|
|
movw %ax, %es
|
|
|
|
|
|
|
|
movl ap_stack_ptr, %esp
|
|
|
|
movb $1, V2P(g_ap_stack_loaded)
|
|
|
|
|
|
|
|
call V2P(enable_sse)
|
|
|
|
|
|
|
|
call V2P(initialize_paging)
|
|
|
|
|
|
|
|
# load boot gdt and enter long mode
|
|
|
|
lgdt V2P(boot_gdtr)
|
|
|
|
ljmpl $0x08, $ap_flush_gdt
|
|
|
|
|
|
|
|
ap_flush_gdt:
|
|
|
|
# move stack pointer to higher half
|
|
|
|
movl %esp, %esp
|
|
|
|
addl $KERNEL_OFFSET, %esp
|
|
|
|
|
|
|
|
# jump to higher half
|
|
|
|
leal ap_higher_half, %ecx
|
|
|
|
jmp *%ecx
|
|
|
|
|
|
|
|
ap_higher_half:
|
|
|
|
# clear rbp for stacktrace
|
|
|
|
xorl %ebp, %ebp
|
|
|
|
|
|
|
|
1: pause
|
|
|
|
cmpb $0, g_ap_startup_done
|
|
|
|
jz 1b
|
|
|
|
|
|
|
|
lock incb g_ap_running_count
|
|
|
|
|
|
|
|
call ap_main
|
|
|
|
jmp system_halt
|
|
|
|
|
|
|
|
ap_gdt:
|
|
|
|
.quad 0x0000000000000000 # null descriptor
|
|
|
|
.quad 0x00CF9A000000FFFF # 32 bit code
|
|
|
|
.quad 0x00CF92000000FFFF # 32 bit data
|
|
|
|
ap_gdtr:
|
|
|
|
.short . - ap_gdt - 1
|
|
|
|
.long ap_gdt
|