|
@@ -1,99 +1,238 @@
|
|
.section .stage1
|
|
.section .stage1
|
|
|
|
+
|
|
|
|
+#include <kernel/mem/paging_asm.h>
|
|
|
|
+
|
|
|
|
+.code16
|
|
|
|
+
|
|
|
|
+.align 4
|
|
|
|
+.Lbios_idt_desc:
|
|
|
|
+ .word 0x03ff # size
|
|
|
|
+ .long 0x00000000 # base
|
|
|
|
+
|
|
|
|
+.align 4
|
|
|
|
+.Lnull_idt_desc:
|
|
|
|
+ .word 0 # size
|
|
|
|
+ .long 0 # base
|
|
|
|
+
|
|
|
|
+.Lhalt16:
|
|
|
|
+ hlt
|
|
|
|
+ jmp .Lhalt16
|
|
|
|
+
|
|
|
|
+# scratch %eax
|
|
|
|
+# return address should be of 2 bytes, and will be zero extended to 4 bytes
|
|
|
|
+go_32bit:
|
|
|
|
+ cli
|
|
|
|
+ lidt .Lnull_idt_desc
|
|
|
|
+
|
|
|
|
+ # set PE bit
|
|
|
|
+ mov %cr0, %eax
|
|
|
|
+ or $1, %eax
|
|
|
|
+ mov %eax, %cr0
|
|
|
|
+
|
|
|
|
+ ljmp $0x08, $.Lgo_32bit0
|
|
|
|
+
|
|
|
|
+.Lgo_16bit0:
|
|
|
|
+ mov $0x20, %ax
|
|
|
|
+ mov %ax, %ds
|
|
|
|
+ mov %ax, %ss
|
|
|
|
+
|
|
|
|
+ lidt .Lbios_idt_desc
|
|
|
|
+
|
|
|
|
+ mov %cr0, %eax
|
|
|
|
+ and $0xfffffffe, %eax
|
|
|
|
+ mov %eax, %cr0
|
|
|
|
+
|
|
|
|
+ ljmp $0x00, $.Lgo_16bit1
|
|
|
|
+.Lgo_16bit1:
|
|
|
|
+ xor %ax, %ax
|
|
|
|
+ mov %ax, %ds
|
|
|
|
+ mov %ax, %ss
|
|
|
|
+ mov %ax, %es
|
|
|
|
+
|
|
|
|
+ sti
|
|
|
|
+
|
|
|
|
+ pop %eax
|
|
|
|
+ push %ax
|
|
|
|
+ ret
|
|
|
|
+
|
|
|
|
+.code32
|
|
|
|
+# scratch %eax
|
|
|
|
+# return address should be of 4 bytes, and extra 2 bytes will be popped from the stack
|
|
|
|
+go_16bit:
|
|
|
|
+ cli
|
|
|
|
+ ljmp $0x18, $.Lgo_16bit0
|
|
|
|
+
|
|
|
|
+.Lgo_32bit0:
|
|
|
|
+ mov $0x10, %ax
|
|
|
|
+ mov %ax, %ds
|
|
|
|
+ mov %ax, %es
|
|
|
|
+ mov %ax, %ss
|
|
|
|
+
|
|
|
|
+ pop %ax
|
|
|
|
+ movzw %ax, %eax
|
|
|
|
+ push %eax
|
|
|
|
+ ret
|
|
|
|
+
|
|
|
|
+# build read disk packet on the stack and perform read operation
|
|
|
|
+#
|
|
|
|
+# read 32k to 0x2000 and then copy to destination
|
|
|
|
+#
|
|
|
|
+# %edi: lba start
|
|
|
|
+# %esi: destination
|
|
.code32
|
|
.code32
|
|
|
|
+read_disk:
|
|
|
|
+ push %ebp
|
|
|
|
+ mov %esp, %ebp
|
|
|
|
+
|
|
|
|
+ lea -24(%esp), %esp
|
|
|
|
+
|
|
|
|
+ mov $0x00400010, %eax # packet size 0, sector count 64
|
|
|
|
+ mov %eax, (%esp)
|
|
|
|
+
|
|
|
|
+ mov $0x02000000, %eax # destination address 0x0200:0x0000
|
|
|
|
+ mov %eax, 4(%esp)
|
|
|
|
+
|
|
|
|
+ mov %edi, 8(%esp) # lba low 4bytes
|
|
|
|
+
|
|
|
|
+ xor %eax, %eax
|
|
|
|
+ mov %eax, 12(%esp) # lba high 2bytes
|
|
|
|
+
|
|
|
|
+ mov %esi, %edi
|
|
|
|
+ mov %esp, %esi # packet address
|
|
|
|
+
|
|
|
|
+ call go_16bit
|
|
|
|
+.code16
|
|
|
|
+ mov $0x42, %ah
|
|
|
|
+ mov $0x80, %dl
|
|
|
|
+ int $0x13
|
|
|
|
+ jc .Lhalt16
|
|
|
|
+
|
|
|
|
+ call go_32bit
|
|
|
|
+.code32
|
|
|
|
+ # move data to destination
|
|
|
|
+ mov $0x2000, %esi
|
|
|
|
+ mov $8192, %ecx
|
|
|
|
+ rep movsl
|
|
|
|
+
|
|
|
|
+ mov %ebp, %esp
|
|
|
|
+ pop %ebp
|
|
|
|
+ ret
|
|
|
|
+
|
|
.globl start_32bit
|
|
.globl start_32bit
|
|
start_32bit:
|
|
start_32bit:
|
|
mov $0x10, %ax
|
|
mov $0x10, %ax
|
|
mov %ax, %ds
|
|
mov %ax, %ds
|
|
mov %ax, %es
|
|
mov %ax, %es
|
|
- mov %ax, %fs
|
|
|
|
- mov %ax, %gs
|
|
|
|
mov %ax, %ss
|
|
mov %ax, %ss
|
|
|
|
|
|
|
|
+ # read kimage into memory
|
|
|
|
+ lea -16(%esp), %esp
|
|
|
|
+ mov $KIMAGE_32K_COUNT, %ecx
|
|
|
|
+ mov $KERNEL_IMAGE_PADDR, 4(%esp) # destination address
|
|
|
|
+ mov $9, (%esp) # LBA
|
|
|
|
+
|
|
|
|
+.Lread_kimage:
|
|
|
|
+ mov (%esp), %edi
|
|
|
|
+ mov 4(%esp), %esi
|
|
|
|
+
|
|
|
|
+ mov %ecx, %ebx
|
|
|
|
+ call read_disk
|
|
|
|
+ mov %ebx, %ecx
|
|
|
|
+
|
|
|
|
+ add $0x8000, 4(%esp)
|
|
|
|
+ add $64, (%esp)
|
|
|
|
+
|
|
|
|
+ loop .Lread_kimage
|
|
|
|
+
|
|
|
|
+ lea 16(%esp), %esp
|
|
|
|
+
|
|
cld
|
|
cld
|
|
xor %eax, %eax
|
|
xor %eax, %eax
|
|
|
|
|
|
# clear paging structures
|
|
# clear paging structures
|
|
- mov $0x100000, %edi
|
|
|
|
- mov %edi, %ecx
|
|
|
|
|
|
+ mov $0x2000, %edi
|
|
|
|
+ mov $0x6000, %ecx
|
|
shr $2, %ecx # %ecx /= 4
|
|
shr $2, %ecx # %ecx /= 4
|
|
rep stosl
|
|
rep stosl
|
|
|
|
|
|
# set P, RW, G
|
|
# set P, RW, G
|
|
- mov $0x00000103, %ebx
|
|
|
|
- xor %edx, %edx
|
|
|
|
- mov $0x00101000, %esi
|
|
|
|
|
|
+ mov $(PA_P | PA_RW | PA_G), %ebx
|
|
|
|
+ xor %edx, %edx
|
|
|
|
+ mov $KERNEL_PDPT_PHYS_MAPPING, %esi
|
|
|
|
|
|
# PML4E 0x000
|
|
# PML4E 0x000
|
|
# we need the first 1GB identically mapped
|
|
# we need the first 1GB identically mapped
|
|
# so that we won't trigger a triple fault after
|
|
# so that we won't trigger a triple fault after
|
|
# enabling paging
|
|
# enabling paging
|
|
- lea -0x1000(%esi), %edi # %edi = 0x100000
|
|
|
|
|
|
+ mov $KERNEL_PML4, %edi
|
|
call fill_pxe
|
|
call fill_pxe
|
|
|
|
|
|
# PML4E 0xff0
|
|
# PML4E 0xff0
|
|
- mov $0x80000000, %edx
|
|
|
|
- lea 0xff0(%edi), %edi
|
|
|
|
- call fill_pxe
|
|
|
|
- xor %edx, %edx
|
|
|
|
|
|
+ mov $(PA_NXE >> 32), %edx
|
|
|
|
+ lea 0xff0(%edi), %edi
|
|
|
|
+ call fill_pxe
|
|
|
|
+ xor %edx, %edx
|
|
|
|
|
|
# setup PDPT for physical memory mapping
|
|
# setup PDPT for physical memory mapping
|
|
- mov %esi, %edi
|
|
|
|
|
|
+ mov $KERNEL_PDPT_PHYS_MAPPING, %edi
|
|
|
|
|
|
# set PS
|
|
# set PS
|
|
- or $0x00000080, %ebx
|
|
|
|
|
|
+ or $PA_PS, %ebx
|
|
mov $256, %ecx
|
|
mov $256, %ecx
|
|
xor %esi, %esi
|
|
xor %esi, %esi
|
|
-_fill_loop1:
|
|
|
|
|
|
+.Lfill1:
|
|
call fill_pxe
|
|
call fill_pxe
|
|
lea 8(%edi), %edi
|
|
lea 8(%edi), %edi
|
|
add $0x40000000, %esi # 1GB
|
|
add $0x40000000, %esi # 1GB
|
|
adc $0, %edx
|
|
adc $0, %edx
|
|
- loop _fill_loop1
|
|
|
|
|
|
+ loop .Lfill1
|
|
|
|
|
|
- mov $0x80000000, %edx
|
|
|
|
|
|
+ mov $(PA_NXE >> 32), %edx
|
|
|
|
|
|
# set PCD, PWT
|
|
# set PCD, PWT
|
|
- or $0x00000018, %ebx
|
|
|
|
|
|
+ or $(PA_PCD | PA_PWT), %ebx
|
|
mov $256, %ecx
|
|
mov $256, %ecx
|
|
xor %esi, %esi
|
|
xor %esi, %esi
|
|
-_fill_loop2:
|
|
|
|
|
|
+.Lfill2:
|
|
call fill_pxe
|
|
call fill_pxe
|
|
lea 8(%edi), %edi
|
|
lea 8(%edi), %edi
|
|
add $0x40000000, %esi # 1GB
|
|
add $0x40000000, %esi # 1GB
|
|
adc $0, %edx
|
|
adc $0, %edx
|
|
- loop _fill_loop2
|
|
|
|
|
|
+ loop .Lfill2
|
|
|
|
|
|
- xor %edx, %edx
|
|
|
|
|
|
+ xor %edx, %edx
|
|
|
|
|
|
# PML4E 0xff8
|
|
# PML4E 0xff8
|
|
- mov %edi, %esi # 0x102000
|
|
|
|
- mov $0x100ff8, %edi
|
|
|
|
|
|
+ mov $KERNEL_PDPT_KERNEL_SPACE, %esi
|
|
|
|
+ mov $KERNEL_PML4, %edi
|
|
|
|
+ lea 0xff8(%edi), %edi
|
|
# clear PCD, PWT, PS
|
|
# clear PCD, PWT, PS
|
|
- and $(~0x00000098), %ebx
|
|
|
|
|
|
+ and $(~(PA_PCD | PA_PWT | PA_PS)), %ebx
|
|
call fill_pxe
|
|
call fill_pxe
|
|
|
|
|
|
# PDPTE 0xff8
|
|
# PDPTE 0xff8
|
|
- lea 0xff8(%esi), %edi # 0x102ff8
|
|
|
|
- lea 0x1000(%esi), %esi # 0x103000
|
|
|
|
|
|
+ mov $KERNEL_PDPT_KERNEL_SPACE, %edi
|
|
|
|
+ lea 0xff8(%edi), %edi
|
|
|
|
+ mov $KERNEL_PD_KIMAGE, %esi
|
|
call fill_pxe
|
|
call fill_pxe
|
|
|
|
|
|
# PDE 0xff0
|
|
# PDE 0xff0
|
|
- lea 0xff0(%esi), %edi # 0x103ff0
|
|
|
|
- lea 0x1000(%esi), %esi # 0x104000
|
|
|
|
|
|
+ mov $KERNEL_PD_KIMAGE, %edi
|
|
|
|
+ lea 0xff0(%edi), %edi
|
|
|
|
+ mov $KERNEL_PT_KIMAGE, %esi # 0x104000
|
|
call fill_pxe
|
|
call fill_pxe
|
|
|
|
|
|
# fill PT (kernel image)
|
|
# fill PT (kernel image)
|
|
- mov %esi, %edi # 0x104000
|
|
|
|
- mov $0x2000, %esi
|
|
|
|
|
|
+ mov $KERNEL_PT_KIMAGE, %edi
|
|
|
|
+ mov $KERNEL_IMAGE_PADDR, %esi
|
|
|
|
|
|
-.extern KERNEL_PAGES
|
|
|
|
mov $KIMAGE_PAGES, %ecx
|
|
mov $KIMAGE_PAGES, %ecx
|
|
|
|
|
|
-_fill_loop3:
|
|
|
|
|
|
+.Lfill3:
|
|
call fill_pxe
|
|
call fill_pxe
|
|
lea 8(%edi), %edi
|
|
lea 8(%edi), %edi
|
|
- lea 0x1000(%esi), %esi
|
|
|
|
- loop _fill_loop3
|
|
|
|
|
|
+ lea 0x1000(%esi), %esi
|
|
|
|
+ loop .Lfill3
|
|
|
|
|
|
# set msr
|
|
# set msr
|
|
mov $0xc0000080, %ecx
|
|
mov $0xc0000080, %ecx
|
|
@@ -107,9 +246,7 @@ _fill_loop3:
|
|
mov %eax, %cr4
|
|
mov %eax, %cr4
|
|
|
|
|
|
# load new page table
|
|
# load new page table
|
|
- xor %eax, %eax
|
|
|
|
- inc %eax
|
|
|
|
- shl $20, %eax # %eax = 0x100000
|
|
|
|
|
|
+ mov $KERNEL_PML4, %eax
|
|
mov %eax, %cr3
|
|
mov %eax, %cr3
|
|
|
|
|
|
mov %cr0, %eax
|
|
mov %cr0, %eax
|
|
@@ -118,28 +255,28 @@ _fill_loop3:
|
|
mov %eax, %cr0
|
|
mov %eax, %cr0
|
|
|
|
|
|
# create gdt
|
|
# create gdt
|
|
- xor %eax, %eax # at 0x0000
|
|
|
|
- mov %eax, 0x00(%eax)
|
|
|
|
- mov %eax, 0x04(%eax) # null descriptor
|
|
|
|
- mov %eax, 0x08(%eax) # code segment lower
|
|
|
|
- mov %eax, 0x10(%eax) # data segment lower
|
|
|
|
- mov $0x00209a00, %ecx
|
|
|
|
- mov %ecx, 0x0c(%eax) # code segment higher
|
|
|
|
- mov $0x00009200, %ecx
|
|
|
|
- mov %ecx, 0x14(%eax) # data segment higher
|
|
|
|
|
|
+ xor %eax, %eax # at 0x0000
|
|
|
|
+ mov %eax, 0x00(%eax)
|
|
|
|
+ mov %eax, 0x04(%eax) # null descriptor
|
|
|
|
+ mov %eax, 0x08(%eax) # code segment lower
|
|
|
|
+ mov %eax, 0x10(%eax) # data segment lower
|
|
|
|
+ mov $0x00209a00, %ecx
|
|
|
|
+ mov %ecx, 0x0c(%eax) # code segment higher
|
|
|
|
+ mov $0x00009200, %ecx
|
|
|
|
+ mov %ecx, 0x14(%eax) # data segment higher
|
|
|
|
|
|
# gdt descriptor
|
|
# gdt descriptor
|
|
- push %eax
|
|
|
|
- push %eax
|
|
|
|
|
|
+ push %eax
|
|
|
|
+ push %eax
|
|
|
|
|
|
# pad with a word
|
|
# pad with a word
|
|
- mov $0x00170000, %eax
|
|
|
|
- push %eax
|
|
|
|
|
|
+ mov $0x00170000, %eax
|
|
|
|
+ push %eax
|
|
|
|
|
|
- lgdt 2(%esp)
|
|
|
|
- add $12, %esp
|
|
|
|
|
|
+ lgdt 2(%esp)
|
|
|
|
+ add $12, %esp
|
|
|
|
|
|
- ljmp $0x08, $_64bit_entry
|
|
|
|
|
|
+ ljmp $0x08, $.L64bit_entry
|
|
|
|
|
|
# %ebx: attribute low
|
|
# %ebx: attribute low
|
|
# %edx: attribute high
|
|
# %edx: attribute high
|
|
@@ -153,33 +290,33 @@ fill_pxe:
|
|
ret
|
|
ret
|
|
|
|
|
|
.code64
|
|
.code64
|
|
-_64bit_entry:
|
|
|
|
- jmp start_64bit
|
|
|
|
|
|
+.L64bit_entry:
|
|
|
|
+ jmp start_64bit
|
|
|
|
|
|
.section .text.kinit
|
|
.section .text.kinit
|
|
start_64bit:
|
|
start_64bit:
|
|
# set stack pointer and clear stack bottom
|
|
# set stack pointer and clear stack bottom
|
|
- movzw %sp, %rdi
|
|
|
|
- xor %rsp, %rsp
|
|
|
|
- inc %rsp
|
|
|
|
- neg %rsp
|
|
|
|
- shr $40, %rsp
|
|
|
|
- shl $40, %rsp
|
|
|
|
|
|
+ mov %rsp, %rdi
|
|
|
|
+ xor %rsp, %rsp
|
|
|
|
+ inc %rsp
|
|
|
|
+ neg %rsp
|
|
|
|
+ shr $40, %rsp
|
|
|
|
+ shl $40, %rsp
|
|
|
|
|
|
- add %rdi, %rsp
|
|
|
|
- mov %rsp, %rdi
|
|
|
|
|
|
+ add %rdi, %rsp
|
|
|
|
+ mov %rsp, %rdi
|
|
|
|
|
|
# make stack frame
|
|
# make stack frame
|
|
- lea -16(%rsp), %rsp
|
|
|
|
- mov %rsp, %rbp
|
|
|
|
|
|
+ lea -16(%rsp), %rsp
|
|
|
|
+ mov %rsp, %rbp
|
|
|
|
|
|
- xor %rax, %rax
|
|
|
|
- mov %rax, (%rsp)
|
|
|
|
- mov %rax, 8(%rsp)
|
|
|
|
|
|
+ xor %rax, %rax
|
|
|
|
+ mov %rax, (%rsp)
|
|
|
|
+ mov %rax, 8(%rsp)
|
|
|
|
|
|
call kernel_init
|
|
call kernel_init
|
|
|
|
|
|
-_64bit_hlt:
|
|
|
|
- cli
|
|
|
|
- hlt
|
|
|
|
- jmp _64bit_hlt
|
|
|
|
|
|
+.L64bit_hlt:
|
|
|
|
+ cli
|
|
|
|
+ hlt
|
|
|
|
+ jmp .L64bit_hlt
|