↑ Back to main site.

EPOS

Experimental Protected-mode Operating System

entry.asm
; EPOS
; http://www.atanaslaskov.com/epos/
;
; FILE:         entry.asm
; DESCRIPTION:  Enter protected mode and call the kernel initialization.
;
; BSD LICENSE
; Copyright (c) 2006, Atanas Laskov
; All rights reserved.
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions are met:
;   1. Redistributions of source code must retain the above copyright notice,
;   this list of conditions and the following disclaimer.
;   2. Redistributions in binary form must reproduce the above copyright notice,
;   this list of conditions and the following disclaimer in the documentation
;   and/or other materials provided with the distribution.
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
; DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;
extern GDTR
extern GDT_START
extern GDT_END
extern SEL_GDT_LINEAR
extern SEL_GDT_CODE
extern SEL_GDT_DATA
extern SEL_GDT_LDT0
extern SEL_GDT_TSS
extern _IDTR
extern _IDT_START
extern _IDT_END
extern _LAST_BYTE

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Kernel signature
SECTION .text
global _start
_start:
dw 'KR'                 ;kernel signature
KRNL_SEC_SIZE equ 200   ;100k kernel space
global _KRN_SEC_SZ
_KRN_SEC_SZ: dw KRNL_SEC_SIZE ;sector count

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Kernel entry point

;------------------------------ Extract parameters
BITS 16

pop ax
mov [cs:KPR_ANCESTOR_END_OFS], ax
pop ax
mov [cs:KPR_ANCESTOR_END_SEG], ax
pop ax
mov [cs:KPR_ANCESTOR_START_OFS], ax
pop ax
mov [cs:KPR_ANCESTOR_START_SEG], ax
pop ax
mov [cs:KRN_BOOTUP_MDEIUM], ax


mov ax, ds
mov ds, ax

;------------------------------- 'Hello' :-)
mov ax, 0B800h
mov es, ax
mov [es:79*2], byte '0'

;------------------------------ Enter protected-mode
cli
lgdt [cs:GDTR]
lidt [cs:_IDTR]

; Clear NT
;
push dword 0
popfd

mov eax, cr0
or al, 1
mov cr0, eax

jmp SEL_GDT_CODE:ENTER_32

BITS 32
ENTER_32:

mov ax, SEL_GDT_DATA
mov ds, ax
mov es, ax
mov fs, ax

mov ax, SEL_GDT_LINEAR
mov gs, ax

mov ax, SEL_GDT_DATA
mov ss, ax
nop
mov esp, 0A0000h-600h-4 ; KRNL_SEC_SIZE*512 + 600h + 1024
mov ebp, 0A0000h-600h-4
nop

mov ax, SEL_GDT_LDT0
lldt ax

; Load task register
;
mov ax, SEL_GDT_TSS
ltr ax

;------------------------------- Kernel Initialization (already in PM)
mov [gs:0B8000h+78*2], byte '1'
mov al, [ds:_LAST_BYTE]
mov [gs:0B8000h+79*2], al

finit
fsetpm

extern _kinit
call _kinit

mov [gs:0B8000h+78*2], byte '.'
jmp $

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Kernel parameters
global KRN_BOOTUP_MDEIUM
global KPR_ANCESTOR_START_SEG
global KPR_ANCESTOR_START_OFS
global KPR_ANCESTOR_END_SEG
global KPR_ANCESTOR_END_OFS

KRN_BOOTUP_MDEIUM          dw 10
KPR_ANCESTOR_START_SEG     dw 10
KPR_ANCESTOR_START_OFS     dw 10
KPR_ANCESTOR_END_SEG       dw 10
KPR_ANCESTOR_END_OFS       dw 10