2022-11-13 01:04:10 +02:00
|
|
|
# Declare constants for the multiboot header
|
2022-12-10 03:55:24 +02:00
|
|
|
.set ALIGN, 1<<0 # align loaded modules on page boundaries
|
|
|
|
.set MEMINFO, 1<<1 # provide memory map
|
|
|
|
.set VIDEOINFO, 1<<2 # provide video info
|
|
|
|
.set MB_FLAGS, ALIGN | MEMINFO | VIDEOINFO # this is the Multiboot 'flag' field
|
|
|
|
.set MB_MAGIC, 0x1BADB002 # 'magic number' lets bootloader find the header
|
|
|
|
.set MB_CHECKSUM, -(MB_MAGIC + MB_FLAGS) #checksum of above, to prove we are multiboot
|
2022-11-13 01:04:10 +02:00
|
|
|
|
|
|
|
# Multiboot header
|
2023-01-10 17:50:24 +02:00
|
|
|
.section .multiboot, "aw"
|
2022-11-12 21:04:47 +02:00
|
|
|
.align 4
|
|
|
|
.long MB_MAGIC
|
|
|
|
.long MB_FLAGS
|
|
|
|
.long MB_CHECKSUM
|
2022-12-19 11:35:24 +02:00
|
|
|
.skip 20
|
2022-12-16 00:46:10 +02:00
|
|
|
|
2022-12-10 03:55:24 +02:00
|
|
|
.long 0
|
2022-12-16 00:46:10 +02:00
|
|
|
.long 800
|
|
|
|
.long 600
|
|
|
|
.long 32
|
2022-11-13 01:04:10 +02:00
|
|
|
|
2023-01-10 17:50:24 +02:00
|
|
|
.section .bss, "aw", @nobits
|
|
|
|
# Create stack
|
2022-11-12 21:04:47 +02:00
|
|
|
stack_bottom:
|
2023-01-10 17:50:24 +02:00
|
|
|
.skip 16384
|
2022-11-12 21:04:47 +02:00
|
|
|
stack_top:
|
2023-01-10 17:50:24 +02:00
|
|
|
|
|
|
|
.global g_kernel_cmdline
|
|
|
|
g_kernel_cmdline:
|
|
|
|
.skip 4096
|
|
|
|
|
2023-01-22 00:47:25 +02:00
|
|
|
.global g_gdtr
|
|
|
|
g_gdtr:
|
|
|
|
.skip 2 + 4
|
|
|
|
.global g_gdt
|
|
|
|
g_gdt:
|
|
|
|
.skip 5 * 8
|
|
|
|
|
2023-01-10 17:50:24 +02:00
|
|
|
# Reserve memory for paging structures,
|
|
|
|
# we will identity map first 4 MiB
|
|
|
|
|
2023-01-21 22:16:01 +02:00
|
|
|
# 0 MiB -> 1 MiB: bootloader stuff
|
|
|
|
# 1 MiB -> 2 MiB: kernel
|
2023-01-10 17:50:24 +02:00
|
|
|
# 2 MiB -> 3 MiB: kmalloc
|
|
|
|
# 3 MiB -> 4 Mib: kmalloc_eternal
|
|
|
|
.align 32
|
|
|
|
boot_page_directory_pointer_table:
|
|
|
|
.skip 4 * 8
|
|
|
|
.align 4096
|
|
|
|
boot_page_directory1:
|
|
|
|
.skip 512 * 8
|
|
|
|
|
|
|
|
.section .text, "a"
|
|
|
|
|
|
|
|
.global g_multiboot_info
|
|
|
|
g_multiboot_info:
|
|
|
|
.skip 4
|
|
|
|
.global g_multiboot_magic
|
|
|
|
g_multiboot_magic:
|
|
|
|
.skip 4
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
has_pae:
|
|
|
|
movl $0, %eax
|
|
|
|
cpuid
|
2023-01-21 23:44:23 +02:00
|
|
|
testl $(1 << 6), %edx
|
|
|
|
ret
|
|
|
|
|
|
|
|
has_sse:
|
|
|
|
movl $1, %eax
|
|
|
|
cpuid
|
|
|
|
testl $(1 << 25), %edx
|
|
|
|
ret
|
|
|
|
|
|
|
|
check_requirements:
|
|
|
|
call has_cpuid
|
|
|
|
jz .exit
|
|
|
|
call has_pae
|
|
|
|
jz .exit
|
|
|
|
call has_sse
|
|
|
|
jz .exit
|
|
|
|
ret
|
2023-01-10 17:50:24 +02:00
|
|
|
.exit:
|
2023-01-21 23:44:23 +02:00
|
|
|
jmp system_halt
|
|
|
|
|
|
|
|
copy_kernel_commandline:
|
|
|
|
pushl %esi
|
|
|
|
pushl %edi
|
|
|
|
movl g_multiboot_info, %esi
|
|
|
|
addl $16, %esi
|
|
|
|
movl (%esi), %esi
|
|
|
|
movl $1024, %ecx
|
|
|
|
movl $g_kernel_cmdline, %edi
|
|
|
|
rep movsl
|
|
|
|
popl %edi
|
|
|
|
popl %esi
|
|
|
|
ret
|
|
|
|
|
|
|
|
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
|
|
|
# identity map first 4 MiB
|
|
|
|
movl $(0x00000000 + 0x83), boot_page_directory1 + 0
|
|
|
|
movl $(0x00200000 + 0x83), boot_page_directory1 + 8
|
|
|
|
movl $(boot_page_directory1 + 0x01), boot_page_directory_pointer_table
|
|
|
|
|
|
|
|
# enable PAE
|
|
|
|
movl %cr4, %ecx
|
|
|
|
orl $0x20, %ecx
|
|
|
|
movl %ecx, %cr4
|
|
|
|
|
|
|
|
# set address of paging structures
|
|
|
|
movl $boot_page_directory_pointer_table, %ecx
|
|
|
|
movl %ecx, %cr3
|
|
|
|
|
|
|
|
# enable paging
|
|
|
|
movl %cr0, %ecx
|
|
|
|
orl $0x80000000, %ecx
|
|
|
|
movl %ecx, %cr0
|
|
|
|
|
2023-01-22 00:47:25 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
initialize_gdt:
|
|
|
|
# setup gdt
|
|
|
|
movw $(5 * 8 - 1), g_gdtr
|
|
|
|
movl $(g_gdt), g_gdtr + 2
|
|
|
|
movl $(0x0000FFFF), g_gdt + 0x08
|
|
|
|
movl $(0x00CF9A00), g_gdt + 0x08 + 4
|
|
|
|
movl $(0x0000FFFF), g_gdt + 0x10
|
|
|
|
movl $(0x00CF9200), g_gdt + 0x10 + 4
|
|
|
|
movl $(0x0000FFFF), g_gdt + 0x18
|
|
|
|
movl $(0x00CFFA00), g_gdt + 0x18 + 4
|
|
|
|
movl $(0x0000FFFF), g_gdt + 0x20
|
|
|
|
movl $(0x00CFF200), g_gdt + 0x20 + 4
|
|
|
|
lgdt g_gdtr
|
|
|
|
|
|
|
|
# flush gdt
|
|
|
|
movw $0x10, %ax
|
|
|
|
movw %ax, %ds
|
|
|
|
movw %ax, %es
|
|
|
|
movw %ax, %fs
|
|
|
|
movw %ax, %gs
|
|
|
|
movw %ax, %ss
|
|
|
|
jmp $0x08, $flush
|
|
|
|
flush:
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
.global _start
|
|
|
|
.type _start, @function
|
|
|
|
_start:
|
|
|
|
# Initialize stack and multiboot info
|
|
|
|
movl $stack_top, %esp
|
|
|
|
movl %eax, g_multiboot_magic
|
|
|
|
movl %ebx, g_multiboot_info
|
|
|
|
|
|
|
|
call copy_kernel_commandline
|
|
|
|
call check_requirements
|
|
|
|
call enable_sse
|
|
|
|
|
|
|
|
call initialize_paging
|
|
|
|
call initialize_gdt
|
|
|
|
|
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
|
2022-11-12 21:04:47 +02:00
|
|
|
call kernel_main
|
2022-11-15 00:32:07 +02:00
|
|
|
|
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
|