Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
egely1337 authored Jul 29, 2022
1 parent c9c8caa commit 74aae83
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 0 deletions.
15 changes: 15 additions & 0 deletions bootloader/bootloader.asm
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions bootloader/disk.asm
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions bootloader/print.asm
Original file line number Diff line number Diff line change
@@ -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
Binary file added build/x86_64/armenianOS-x86_64.flp
Binary file not shown.
13 changes: 13 additions & 0 deletions compile.bat
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions kernel/cpuid.asm
Original file line number Diff line number Diff line change
@@ -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

34 changes: 34 additions & 0 deletions kernel/gdt.asm
Original file line number Diff line number Diff line change
@@ -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
51 changes: 51 additions & 0 deletions kernel/kernel.asm
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions kernel/kmain.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
32 changes: 32 additions & 0 deletions kernel/paging.asm
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 74aae83

Please sign in to comment.