diff --git a/bootloader/bootloader.asm b/bootloader/bootloader.asm new file mode 100644 index 0000000..16834fe --- /dev/null +++ b/bootloader/bootloader.asm @@ -0,0 +1,15 @@ +org 0x7c00 + + +mov [BOOT_DISK], dl +mov bp, 0x7c00 +mov sp, bp +call boot + + + + +%include "bootloader/disk.asm" + +times (510-($-$$)) db 0 +dw 0xaa55 \ No newline at end of file diff --git a/bootloader/disk.asm b/bootloader/disk.asm new file mode 100644 index 0000000..a51c1a3 --- /dev/null +++ b/bootloader/disk.asm @@ -0,0 +1,16 @@ +PROGRAM_SPACE equ 0x7e00 + +boot: + mov ah, 0x02 + mov dl, [BOOT_DISK] + mov bx, PROGRAM_SPACE + mov al, 32 + mov ch, 0x00 + mov dh, 0x00 + mov cl, 0x02 + int 0x13 + jmp PROGRAM_SPACE + + +BOOT_DISK: + db 0 \ No newline at end of file diff --git a/bootloader/print.asm b/bootloader/print.asm new file mode 100644 index 0000000..5db7d70 --- /dev/null +++ b/bootloader/print.asm @@ -0,0 +1,24 @@ +[bits 16] +ClearScreen: + mov ah, 00h + mov al, 03h + int 10h + +PrintString: + push ax + push bx + + mov ah, 0x0e + .Loop: + cmp [bx], byte 0 + je .Exit + mov al, [bx] + int 0x10 + inc bx + jmp .Loop + .Exit: + pop ax + pop bx + ret + + diff --git a/build/x86_64/armenianOS-x86_64.flp b/build/x86_64/armenianOS-x86_64.flp new file mode 100644 index 0000000..d39c7b7 Binary files /dev/null and b/build/x86_64/armenianOS-x86_64.flp differ diff --git a/compile.bat b/compile.bat new file mode 100644 index 0000000..447293d --- /dev/null +++ b/compile.bat @@ -0,0 +1,13 @@ +nasm bootloader/bootloader.asm -f bin -o bootloader.bin +nasm kernel/kernel.asm -f elf64 -o init.o +wsl $WSLENV/x86_64-elf-gcc -ffreestanding -mno-red-zone -m64 -c "./kernel/kmain.cpp" -o "kernel.o" +wsl $WSLENV/x86_64-elf-ld -o kernel.tmp -Ttext 0x7e00 init.o kernel.o +wsl /usr/bin/objcopy -O binary kernel.tmp kernel.bin +copy /b bootloader.bin+kernel.bin armenianOS-x86_64.flp +move armenianOS-x86_64.flp ./build/x86_64/ +del kernel.bin +del bootloader.bin +del kernel.o +del init.o +del kernel.tmp +qemu-system-x86_64.exe build/x86_64/armenianOS-x86_64.flp \ No newline at end of file diff --git a/kernel/cpuid.asm b/kernel/cpuid.asm new file mode 100644 index 0000000..c691591 --- /dev/null +++ b/kernel/cpuid.asm @@ -0,0 +1,30 @@ +_detectCpuId: + pushfd + pop eax + + mov ecx,eax + + xor eax, 1 << 21 + push eax + popfd + pushfd + pop eax + push ecx + popfd + xor eax,ecx + jz NoCPUID + ret + +_detectLongMode: + mov eax,0x80000001 + cpuid + test edx, 1 << 29 + jz NoLongMode + ret + +NoLongMode: + hlt + +NoCPUID: + hlt + diff --git a/kernel/gdt.asm b/kernel/gdt.asm new file mode 100644 index 0000000..9fabf2f --- /dev/null +++ b/kernel/gdt.asm @@ -0,0 +1,34 @@ +gdt_nulldesc: + dd 0 + dd 0 + +gdt_codedesc: + dw 0xFFFF + dw 0x0000 + db 0x00 + db 10011010b + db 11001111b + db 0x00 +gdt_datasec: + dw 0xFFFF + dw 0x0000 + db 0x00 + db 10010010b + db 11001111b + db 0x00 + +gdt_end: + +gdt_descriptor: + gdt_size: + dw gdt_end - gdt_nulldesc - 1 + dd gdt_nulldesc + +codeseg equ gdt_codedesc - gdt_nulldesc +dataseg equ gdt_datasec - gdt_nulldesc + +[bits 32] +EditGDT: + mov [gdt_codedesc+6], byte 11001111b + mov [gdt_datasec+6], byte 10101111b + ret diff --git a/kernel/kernel.asm b/kernel/kernel.asm new file mode 100644 index 0000000..04d618c --- /dev/null +++ b/kernel/kernel.asm @@ -0,0 +1,51 @@ +[bits 16] +[extern _kmain] +_enterpm: + call ClearScreen + call EnableA20 + cli + lgdt [gdt_descriptor] + mov eax, cr0 + or eax, 1 + mov cr0, eax + jmp codeseg:_startpmode + +EnableA20: + in al, 0x92 + or al, 2 + out 0x92, al + ret + + +%include "bootloader/print.asm" +%include "kernel/gdt.asm" +%include "kernel/cpuid.asm" +%include "kernel/paging.asm" + + + +[bits 32] +_startpmode: + mov ax, dataseg + mov ds,ax + mov ss,ax + mov es,ax + mov fs,ax + mov gs,ax + call _kmain + call _detectCpuId + call _detectLongMode + call _setuppaging + call EditGDT + jmp codeseg:Start64Bit + + +[bits 64] +Start64Bit: + call _kmain + + + + + +times (2048-($-$$)) db 0 diff --git a/kernel/kmain.cpp b/kernel/kmain.cpp new file mode 100644 index 0000000..2a1ba7e --- /dev/null +++ b/kernel/kmain.cpp @@ -0,0 +1,10 @@ + + +extern "C" int _kmain(){ + *(char*)0xB8000 = 'H'; + *(char*)0xB8002 = 'e'; + *(char*)0xB8004 = 'l'; + *(char*)0xB8006 = 'l'; + *(char*)0xB8008 = 'o'; + while(true); +} \ No newline at end of file diff --git a/kernel/paging.asm b/kernel/paging.asm new file mode 100644 index 0000000..038ba93 --- /dev/null +++ b/kernel/paging.asm @@ -0,0 +1,32 @@ +PageTableEntry equ 0x1000 + +_setuppaging: + mov edi, PageTableEntry + mov cr3, edi + mov dword [edi], 0x2003 + add edi, 0x1000 + mov dword [edi], 0x3003 + add edi, 0x4003 + mov ebx, 0x00000003 + mov ecx, 512 + + .SetEntry: + mov dword [edi], ebx + add ebx, 0x1000 + add edi, 8 + loop .SetEntry + + mov eax, cr4 + or eax, 1 << 5 + mov cr4, eax + + + mov ecx, 0xC00000000 + rdmsr + or eax, 1 << 8 + wrmsr + + mov eax, cr0 + or eax, 1 << 31 + mov cr0, eax + ret \ No newline at end of file