2023-01-25 19:05:47 +02:00
|
|
|
# Declare constants for the multiboot header
|
|
|
|
.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
|
|
|
|
|
|
|
|
.set PG_PRESENT, 1<<0
|
|
|
|
.set PG_READ_WRITE, 1<<1
|
|
|
|
.set PG_PAGE_SIZE, 1<<7
|
|
|
|
|
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
|
|
|
|
|
|
|
|
# Multiboot header
|
|
|
|
.section .multiboot, "aw"
|
|
|
|
.align 4
|
|
|
|
.long MB_MAGIC
|
|
|
|
.long MB_FLAGS
|
|
|
|
.long MB_CHECKSUM
|
|
|
|
.skip 20
|
|
|
|
|
|
|
|
.long 0
|
|
|
|
.long 800
|
|
|
|
.long 600
|
|
|
|
.long 32
|
|
|
|
|
|
|
|
.section .bss, "aw", @nobits
|
|
|
|
# Create stack
|
2023-03-01 20:17:03 +02:00
|
|
|
.global g_boot_stack_bottom
|
|
|
|
g_boot_stack_bottom:
|
2023-01-25 19:05:47 +02:00
|
|
|
.skip 16384
|
2023-03-01 20:17:03 +02:00
|
|
|
.global g_boot_stack_top
|
|
|
|
g_boot_stack_top:
|
2023-01-25 19:05:47 +02:00
|
|
|
|
|
|
|
.global g_kernel_cmdline
|
|
|
|
g_kernel_cmdline:
|
|
|
|
.skip 4096
|
|
|
|
|
2023-05-30 00:08:52 +03:00
|
|
|
.global g_multiboot_info
|
|
|
|
g_multiboot_info:
|
|
|
|
.skip 8
|
|
|
|
.global g_multiboot_magic
|
|
|
|
g_multiboot_magic:
|
|
|
|
.skip 8
|
2023-01-25 19:05:47 +02:00
|
|
|
|
2023-03-07 01:57:09 +02:00
|
|
|
.section .text
|
2023-03-01 21:21:08 +02:00
|
|
|
|
2023-05-30 00:08:52 +03:00
|
|
|
# Identity map first GiB
|
|
|
|
.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:
|
|
|
|
.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
|
|
|
|
.endr
|
|
|
|
.quad V2P(boot_pd) + (PG_READ_WRITE | PG_PRESENT)
|
|
|
|
.quad 0
|
|
|
|
boot_pd: # 1 GiB
|
2023-05-30 00:08:52 +03:00
|
|
|
.set i, 0
|
|
|
|
.rept 512
|
|
|
|
.quad i + (PG_PAGE_SIZE | PG_READ_WRITE | PG_PRESENT)
|
|
|
|
.set i, i + 0x00200000
|
|
|
|
.endr
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
copy_kernel_commandline:
|
|
|
|
pushl %esi
|
|
|
|
pushl %edi
|
2023-05-30 08:00:17 +03:00
|
|
|
movl V2P(g_multiboot_info), %esi
|
2023-01-25 19:05:47 +02:00
|
|
|
addl $16, %esi
|
|
|
|
movl (%esi), %esi
|
|
|
|
movl $1024, %ecx
|
2023-05-30 08:00:17 +03:00
|
|
|
movl $V2P(g_kernel_cmdline), %edi
|
2023-01-25 19:05:47 +02:00
|
|
|
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
|
|
|
|
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:
|
|
|
|
# Initialize stack and multiboot info
|
2023-05-30 08:00:17 +03:00
|
|
|
movl $V2P(g_boot_stack_top), %esp
|
|
|
|
movl %eax, V2P(g_multiboot_magic)
|
|
|
|
movl %ebx, V2P(g_multiboot_info)
|
2023-01-25 19:05:47 +02:00
|
|
|
|
|
|
|
call copy_kernel_commandline
|
|
|
|
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, %ss
|
2023-03-01 20:17:03 +02:00
|
|
|
|
|
|
|
# clear segment registers (unused in x86_64?)
|
|
|
|
movw $0x00, %ax
|
2023-01-25 19:05:47 +02:00
|
|
|
movw %ax, %ds
|
|
|
|
movw %ax, %es
|
|
|
|
movw %ax, %fs
|
|
|
|
movw %ax, %gs
|
|
|
|
|
2023-05-30 08:00:17 +03:00
|
|
|
# jump to higher half
|
|
|
|
movq $g_boot_stack_top, %rsp
|
|
|
|
movabsq $higher_half, %rcx
|
|
|
|
jmp *%rcx
|
|
|
|
|
|
|
|
higher_half:
|
2023-05-30 22:21:12 +03:00
|
|
|
addq $KERNEL_OFFSET, g_multiboot_info
|
2023-05-30 08:00:17 +03:00
|
|
|
|
2023-01-25 19:05:47 +02:00
|
|
|
# call global constuctors
|
|
|
|
call _init
|
|
|
|
|
|
|
|
# call to the kernel itself (clear ebp for stacktrace)
|
|
|
|
xorq %rbp, %rbp
|
|
|
|
call kernel_main
|
|
|
|
|
|
|
|
# call global destructors
|
|
|
|
call _fini
|
|
|
|
|
|
|
|
system_halt:
|
|
|
|
xchgw %bx, %bx
|
|
|
|
cli
|
|
|
|
1: hlt
|
|
|
|
jmp 1b
|