시스템을 초기화 하고 커널을 로딩한다.
; Stage2 의 진입 함수 ; ; Dummy GDT 를 준비한 후 Protected Mode 를 Toggle On 시킨다. ; 0xe820 service 를 이용해 MRD 를 읽어 둔다. ; ; Memory Range Descriptors ; ;
;
bits 16
SECTION .init
; This label is not for exporting, Just I want to get rid of warnings of ld.
GLOBAL entry
entry:
;
; Set the keyboard repeat rate to the max
mov ax, 0x0305
xor bx, bx
int 0x16
;
; Get the memory from the ACPI (E820h) services
mov dword [KERNEL_ARGUMENT], 0
xor ebx, ebx ; Continuation
mov es, ebx ; ES
mov edi, KERNEL_ARGUMENT + 4 ; DI (ES:DI) location of storing data
mov ecx, 20 ; Buffer size, 20 bytes
mov edx, 0x534d4150 ; Signature
e820_get_desc:
mov eax, 0x0000E820 ; function ID
clc ; Clear carry flag
int 0x15 ; Invoke interrupt 0x15
jc e820_error ; Check the carry flag
cmp eax, 0x534d4150 ; Check the signature
jne e820_error
cmp ecx, 20 ; Check the minimum size of descriptor
jl no_e820
add edi, ecx ; Move pointer to next
cmp edi, E820_BOUND ; Compare the boundary of memory for descriptors
jge e820_overflow
inc dword [KERNEL_ARGUMENT]
cmp ebx, 0 ; Check the continuation value
jne e820_get_desc
;
;
; Load global descriptor table
cli
lgdt [gdt_ptr]
; Change the CPU mode to protected
mov eax, cr0
or al, 1 ; Toggle PE bit
mov cr0, eax
;
;
; Now we got the code descripor and its selector,
; so we use it now through FAR jump
;
e820_error:
push e820_error_msg
call print
add sp, 2
e820_overflow:
push e820_overflow_msg
call print
add sp, 2
no_e820:
push no_e820_msg
call print
add sp, 2
print:
push bp
mov bp, sp
pusha
mov si, [bp+4] ; si : address of string
cld ; direction forward
print_loop:
lodsb ; next character - ds:si
cmp al, 0
je print_done
mov ah, 0x0E
mov bx, 0x0007
int 0x10
print_done:
popa
pop bp
ret
e820_error_msg db 'E820-Error',0x0
e820_overflow_msg db 'E820-Overflow',0x0
e820_read_msg db 'E820-Read', 0x0
no_e820_msg db 'No E820', 0x0
;
bits 32
SECTION .data
GLOBAL gdt_ptr
gdt_ptr equ $
dw end_gdt - begin_gdt - 1
dd begin_gdt
; GLOBAL begin_gdt
begin_gdt equ $
dd 0x00000000
dd 0x00000000 ;
NULL Descriptor
dw 0xFFFF
dw 0x0000
db 0x00
db 0x9A
db 0xCF
db 0x00
dw 0xFFFF
dw 0x0000
db 0x00
db 0x92
db 0xCF
db 0x00
dw 0xFFFF
dw 0x0000
db 0x00
db 0x92
db 0xCF
db 0x00
end_gdt equ $
; End of GDT
;
;
SECTION .text
GLOBAL entry32
entry32:
mov ds, eax
mov es, eax
mov fs, eax
mov gs, eax
mov ss, eax
mov esp, KTHREAD_MAIN_ESP ; I have to initialize the stack pointer
mov ebp, esp ; Base stack pointer
;
;
NC Kernel 을 로딩하기 위해 설계 아키텍쳐 독립적인 boot loader 의 인터페이스를 정의하고, 정의된 인터페이스에 맞추어 x86 용을 구현된다.