2023-01-25 19:05:47 +02:00
|
|
|
.set PG_PRESENT, 1<<0
|
|
|
|
.set PG_READ_WRITE, 1<<1
|
|
|
|
.set PG_PAGE_SIZE, 1<<7
|
|
|
|
|
2023-12-09 17:33:58 +02:00
|
|
|
.set FB_WIDTH, 800
|
|
|
|
.set FB_HEIGHT, 600
|
|
|
|
.set FB_BPP, 32
|
|
|
|
|
2023-05-30 08:00:17 +03:00
|
|
|
#define KERNEL_OFFSET 0xFFFFFFFF80000000
|
|
|
|
#define V2P(vaddr) ((vaddr) - KERNEL_OFFSET)
|
|
|
|
|
2023-01-25 19:05:47 +02:00
|
|
|
.code32
|
|
|
|
|
2023-10-17 01:06:24 +03:00
|
|
|
# multiboot2 header
|
2023-01-25 19:05:47 +02:00
|
|
|
.section .multiboot, "aw"
|
2023-10-17 01:06:24 +03:00
|
|
|
.align 8
|
2023-12-09 17:33:58 +02:00
|
|
|
multiboot2_start:
|
2023-10-17 01:06:24 +03:00
|
|
|
.long 0xE85250D6
|
2023-01-25 19:05:47 +02:00
|
|
|
.long 0
|
2023-10-17 01:06:24 +03:00
|
|
|
.long multiboot2_end - multiboot2_start
|
|
|
|
.long -(0xE85250D6 + (multiboot2_end - multiboot2_start))
|
|
|
|
|
|
|
|
# framebuffer tag
|
|
|
|
.align 8
|
|
|
|
.short 5
|
|
|
|
.short 0
|
|
|
|
.long 20
|
2023-12-09 17:33:58 +02:00
|
|
|
.long FB_WIDTH
|
|
|
|
.long FB_HEIGHT
|
|
|
|
.long FB_BPP
|
2023-01-25 19:05:47 +02:00
|
|
|
|
2023-10-17 01:06:24 +03:00
|
|
|
# legacy start
|
|
|
|
.align 8
|
|
|
|
.short 3
|
|
|
|
.short 0
|
|
|
|
.long 12
|
|
|
|
.long V2P(_start)
|
|
|
|
|
|
|
|
.align 8
|
|
|
|
.short 0
|
|
|
|
.short 0
|
|
|
|
.long 8
|
|
|
|
multiboot2_end:
|
|
|
|
|
2023-12-09 17:33:58 +02:00
|
|
|
.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:
|
|
|
|
|
2023-01-25 19:05:47 +02:00
|
|
|
.section .bss, "aw", @nobits
|
2024-03-03 22:30:06 +02:00
|
|
|
boot_stack_bottom:
|
|
|
|
.skip 4096 * 4
|
|
|
|
boot_stack_top:
|
2023-09-09 22:52:03 +03:00
|
|
|
|
2023-01-25 19:05:47 +02:00
|
|
|
.global g_kernel_cmdline
|
|
|
|
g_kernel_cmdline:
|
|
|
|
.skip 4096
|
|
|
|
|
2023-11-17 18:54:59 +02:00
|
|
|
bootloader_magic:
|
2023-05-30 00:08:52 +03:00
|
|
|
.skip 8
|
2023-11-17 18:54:59 +02:00
|
|
|
bootloader_info:
|
2023-05-30 00:08:52 +03:00
|
|
|
.skip 8
|
2023-01-25 19:05:47 +02:00
|
|
|
|
2023-08-11 00:26:12 +03:00
|
|
|
.section .data
|
2023-03-01 21:21:08 +02:00
|
|
|
|
2023-05-31 00:42:24 +03:00
|
|
|
# Map first GiB to 0x00000000 and 0xFFFFFFFF80000000
|
2023-05-30 00:08:52 +03:00
|
|
|
.align 4096
|
|
|
|
boot_pml4:
|
2023-05-30 08:00:17 +03:00
|
|
|
.quad V2P(boot_pdpt_lo) + (PG_READ_WRITE | PG_PRESENT)
|
|
|
|
.rept 510
|
2023-05-30 00:08:52 +03:00
|
|
|
.quad 0
|
|
|
|
.endr
|
2023-05-30 08:00:17 +03:00
|
|
|
.quad V2P(boot_pdpt_hi) + (PG_READ_WRITE | PG_PRESENT)
|
|
|
|
boot_pdpt_lo:
|
2023-06-03 18:53:36 +03:00
|
|
|
.quad V2P(boot_pd) + (PG_READ_WRITE | PG_PRESENT)
|
2023-05-30 00:08:52 +03:00
|
|
|
.rept 511
|
|
|
|
.quad 0
|
|
|
|
.endr
|
2023-05-30 08:00:17 +03:00
|
|
|
boot_pdpt_hi:
|
|
|
|
.rept 510
|
|
|
|
.quad 0
|
2023-05-30 00:08:52 +03:00
|
|
|
.endr
|
2023-06-03 18:53:36 +03:00
|
|
|
.quad V2P(boot_pd) + (PG_READ_WRITE | PG_PRESENT)
|
2023-05-31 00:42:24 +03:00
|
|
|
.quad 0
|
2023-06-03 18:53:36 +03:00
|
|
|
boot_pd:
|
|
|
|
.set i, 0
|
|
|
|
.rept 512
|
|
|
|
.quad i + (PG_PAGE_SIZE | PG_READ_WRITE | PG_PRESENT)
|
|
|
|
.set i, i + 0x200000
|
|
|
|
.endr
|
2023-05-30 00:08:52 +03:00
|
|
|
|
2023-01-25 19:05:47 +02:00
|
|
|
boot_gdt:
|
2023-03-01 20:17:03 +02:00
|
|
|
.quad 0x0000000000000000 # null descriptor
|
|
|
|
.quad 0x00AF9A000000FFFF # kernel code
|
|
|
|
.quad 0x00AF92000000FFFF # kernel data
|
2023-01-25 19:05:47 +02:00
|
|
|
boot_gdtr:
|
|
|
|
.short . - boot_gdt - 1
|
2023-05-30 08:00:17 +03:00
|
|
|
.quad V2P(boot_gdt)
|
2023-01-25 19:05:47 +02:00
|
|
|
|
2024-03-03 02:17:24 +02:00
|
|
|
.global g_ap_startup_done
|
|
|
|
g_ap_startup_done:
|
|
|
|
.byte 0
|
|
|
|
.global g_ap_running_count
|
|
|
|
g_ap_running_count:
|
|
|
|
.byte 0
|
2024-03-03 22:30:06 +02:00
|
|
|
.global g_ap_stack_loaded
|
|
|
|
g_ap_stack_loaded:
|
|
|
|
.byte 0
|
2024-03-03 02:17:24 +02:00
|
|
|
|
2023-08-11 00:26:12 +03:00
|
|
|
.section .text
|
|
|
|
|
2023-01-25 19:05:47 +02:00
|
|
|
has_cpuid:
|
|
|
|
pushfl
|
|
|
|
pushfl
|
|
|
|
xorl $0x00200000, (%esp)
|
|
|
|
popfl
|
|
|
|
pushfl
|
|
|
|
popl %eax
|
|
|
|
xorl (%esp), %eax
|
|
|
|
popfl
|
|
|
|
testl $0x00200000, %eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
is_64_bit:
|
|
|
|
movl $0x80000000, %eax
|
|
|
|
cpuid
|
|
|
|
cmpl $0x80000001, %eax
|
|
|
|
jl .no_extension
|
|
|
|
movl $0x80000001, %eax
|
|
|
|
cpuid
|
|
|
|
testl $(1 << 29), %edx
|
|
|
|
ret
|
|
|
|
.no_extension:
|
|
|
|
cmpl %eax, %eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
check_requirements:
|
|
|
|
call has_cpuid
|
|
|
|
jz .exit
|
|
|
|
call is_64_bit
|
|
|
|
jz .exit
|
|
|
|
ret
|
|
|
|
.exit:
|
|
|
|
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
|
|
|
|
ret
|
|
|
|
|
|
|
|
initialize_paging:
|
|
|
|
# enable PAE
|
|
|
|
movl %cr4, %ecx
|
|
|
|
orl $0x20, %ecx
|
|
|
|
movl %ecx, %cr4
|
|
|
|
|
|
|
|
# set long mode enable bit
|
|
|
|
movl $0x100, %eax
|
|
|
|
movl $0x000, %edx
|
|
|
|
movl $0xC0000080, %ecx
|
|
|
|
wrmsr
|
|
|
|
|
|
|
|
# set address of paging structures
|
2023-05-30 08:00:17 +03:00
|
|
|
movl $V2P(boot_pml4), %ecx
|
2023-01-25 19:05:47 +02:00
|
|
|
movl %ecx, %cr3
|
|
|
|
|
|
|
|
# enable paging
|
|
|
|
movl %cr0, %ecx
|
|
|
|
orl $0x80000000, %ecx
|
|
|
|
movl %ecx, %cr0
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
.global _start
|
|
|
|
.type _start, @function
|
|
|
|
_start:
|
2024-03-03 22:30:06 +02:00
|
|
|
cli; cld
|
|
|
|
|
2023-01-25 19:05:47 +02:00
|
|
|
# Initialize stack and multiboot info
|
2023-11-17 18:54:59 +02:00
|
|
|
movl %eax, V2P(bootloader_magic)
|
|
|
|
movl %ebx, V2P(bootloader_info)
|
2023-01-25 19:05:47 +02:00
|
|
|
|
2024-03-03 22:30:06 +02:00
|
|
|
movl $V2P(boot_stack_top), %esp
|
2024-03-03 02:17:24 +02:00
|
|
|
|
2023-01-25 19:05:47 +02:00
|
|
|
call check_requirements
|
|
|
|
call enable_sse
|
|
|
|
|
|
|
|
call initialize_paging
|
|
|
|
|
|
|
|
# flush gdt and jump to 64 bit
|
2023-05-30 08:00:17 +03:00
|
|
|
lgdt V2P(boot_gdtr)
|
|
|
|
ljmpl $0x08, $V2P(long_mode)
|
2023-01-25 19:05:47 +02:00
|
|
|
|
|
|
|
.code64
|
|
|
|
long_mode:
|
2023-03-01 20:17:03 +02:00
|
|
|
movw $0x10, %ax
|
2023-01-25 19:05:47 +02:00
|
|
|
movw %ax, %ds
|
2024-03-03 02:17:24 +02:00
|
|
|
movw %ax, %ss
|
2023-01-25 19:05:47 +02:00
|
|
|
movw %ax, %es
|
2024-03-03 02:17:24 +02:00
|
|
|
|
|
|
|
# move stack pointer to higher half
|
|
|
|
movl %esp, %esp
|
|
|
|
addq $KERNEL_OFFSET, %rsp
|
2023-01-25 19:05:47 +02:00
|
|
|
|
2023-05-30 08:00:17 +03:00
|
|
|
# jump to higher half
|
|
|
|
movabsq $higher_half, %rcx
|
|
|
|
jmp *%rcx
|
|
|
|
|
|
|
|
higher_half:
|
2023-01-25 19:05:47 +02:00
|
|
|
# call global constuctors
|
|
|
|
call _init
|
|
|
|
|
2023-11-17 18:54:59 +02:00
|
|
|
# call to the kernel itself (clear rbp for stacktrace)
|
2023-01-25 19:05:47 +02:00
|
|
|
xorq %rbp, %rbp
|
2023-11-17 18:54:59 +02:00
|
|
|
|
|
|
|
movl V2P(bootloader_magic), %edi
|
|
|
|
movl V2P(bootloader_info), %esi
|
2023-01-25 19:05:47 +02:00
|
|
|
call kernel_main
|
|
|
|
|
|
|
|
# call global destructors
|
|
|
|
call _fini
|
|
|
|
|
|
|
|
system_halt:
|
|
|
|
xchgw %bx, %bx
|
|
|
|
cli
|
|
|
|
1: hlt
|
|
|
|
jmp 1b
|
2024-03-03 02:17:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
.section .ap_init, "ax"
|
|
|
|
|
|
|
|
.code16
|
|
|
|
.global ap_trampoline
|
|
|
|
ap_trampoline:
|
2024-03-03 22:30:06 +02:00
|
|
|
jmp 1f
|
|
|
|
.align 8
|
|
|
|
ap_stack_ptr:
|
|
|
|
.skip 4
|
|
|
|
1:
|
|
|
|
cli; cld
|
2024-03-03 02:17:24 +02:00
|
|
|
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
|
|
|
|
|
2024-03-03 22:30:06 +02:00
|
|
|
movl ap_stack_ptr, %esp
|
|
|
|
movb $1, V2P(g_ap_stack_loaded)
|
|
|
|
|
|
|
|
call V2P(enable_sse)
|
2024-03-03 02:17:24 +02:00
|
|
|
|
|
|
|
call V2P(initialize_paging)
|
2024-03-03 22:30:06 +02:00
|
|
|
|
|
|
|
# load boot gdt and enter long mode
|
2024-03-03 02:17:24 +02:00
|
|
|
lgdt V2P(boot_gdtr)
|
|
|
|
ljmpl $0x08, $ap_long_mode
|
|
|
|
|
|
|
|
.code64
|
|
|
|
ap_long_mode:
|
|
|
|
# move stack pointer to higher half
|
|
|
|
movl %esp, %esp
|
|
|
|
addq $KERNEL_OFFSET, %rsp
|
|
|
|
|
|
|
|
# jump to higher half
|
|
|
|
movabsq $ap_higher_half, %rcx
|
|
|
|
jmp *%rcx
|
|
|
|
|
|
|
|
ap_higher_half:
|
|
|
|
# clear rbp for stacktrace
|
|
|
|
xorq %rbp, %rbp
|
|
|
|
|
|
|
|
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
|
|
|
|
.quad ap_gdt
|