/* head.S
*I don't mean to write the whole OS kernel.I just want to put what I have
*already learned into practice.But what will it be end up looking like.
*I don't know.ALA I have some free time,I will write something.Just like
*linus once said,"Just for fun".I don't care what this small ugly codes
*will be capable of,or does it worth something.I just write it for fun,
*and for practice.
* 10 / 3 / 2010 liangtao
* E-mail:liangtao90s@gmail.com
*/
#define __ASM__
#include
.global begtext, begdata, begbss, endtext, enddata, endbss
.text
begtext:
.data
begdata:
.bss
begbss:
.text
.global pg_dir
pg_dir:
_start_32:
cld
movw $__KERNEL_DS, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %fs
movw %ax, %gs
lss stack, %esp
lidt idtr
lgdt gdtr
#clear the bss
xorl %eax, %eax
movl $__bss_start, %edi
movl $_end, %ebx
clear_bss:
stosl
cmpl %ebx, %edi
jb clear_bss
# call setup_idt
# call setup_gdt
pushl $Oops
pushl $start_kernel
jmp enable_paging
# the NULL iterrupt handler is useless. cause we mask out all the interrupt
# and also the interrupt is not enabled until a appropriate time.
# we do not want any unexpected interrupt,EVEN they will.
/*
setup_idt:
lidt idtr
ret
setup_gdt:
lgdt gdtr
ret
*/
.org 0x1000
pg0:
.org 0x2000
pg1:
.org 0x3000
end_page:
enable_paging:
movl $pg0+7, pg_dir
movl $pg1+7, pg_dir + 4
movl $0x7, %eax
movl $pg0, %edi
fill_page_entry:
stosl
addl $0x1000, %eax
cmpl $end_page, %edi
jb fill_page_entry
movl $pg_dir, %eax
movl %eax, %cr3
movl %cr0, %eax
orl $0x80000000, %eax
movl %eax, %cr0
#after this, we have page directory, page table, idt table, gdt table.
#they are all set up.the protect mode is enabled,so we can jump.
#finally get rid of the low-level language...
ret
Oops:
jmp Oops
.align 8
stack:
.long init_stack + 4096
.word __KERNEL_DS
.align 8
.global sys_idt_table
sys_idt_table:
.fill 256, 8, 0
/*
gdt:
.long 0, 0 #NULL
.long 0, 0 #reserved
#kernel code segment
.word 0xffff #limit:0-15 bit
.word 0x0000 #base: 0-15 bit
.word 0x9a00 #base: 16-23 bit,type:1010b,s=1,dpl=0,p=1
.word 0x00cf #base: 24-31 bit,G=1,B=1,limit: 16-19
#kernel data segment
.word 0xffff
.word 0x0000
.word 0x9200
.word 0x00cf
.fill 252, 8, 0
*/
idtr:
.word 2048 - 1
.long sys_idt_table
gdtr:
.word GDT_LIMIT
.long sys_gdt_table
.text
endtext:
.data
enddata:
.bss
endbss:
阅读(2145) | 评论(0) | 转发(0) |