-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3cfc6c7
commit 28cdd6d
Showing
15 changed files
with
516 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
kernel_source_files := $(shell find src/impl/kernel -name *.c) | ||
kernel_object_files := $(patsubst src/impl/kernel/%.c, build/kernel/%.o, $(kernel_source_files)) | ||
|
||
x86_64_c_source_files := $(shell find src/impl/x86_64 -name *.c) | ||
x86_64_c_object_files := $(patsubst src/impl/x86_64/%.c, build/x86_64/%.o, $(x86_64_c_source_files)) | ||
|
||
x86_64_asm_source_files := $(shell find src/impl/x86_64 -name *.asm) | ||
x86_64_asm_object_files := $(patsubst src/impl/x86_64/%.asm, build/x86_64/%.o, $(x86_64_asm_source_files)) | ||
|
||
x86_64_object_files := $(x86_64_c_object_files) $(x86_64_asm_object_files) | ||
|
||
$(kernel_object_files): build/kernel/%.o : src/impl/kernel/%.c | ||
mkdir -p $(dir $@) && \ | ||
x86_64-elf-gcc -c -I src/intf -ffreestanding $(patsubst build/kernel/%.o, src/impl/kernel/%.c, $@) -o $@ | ||
|
||
$(x86_64_c_object_files): build/x86_64/%.o : src/impl/x86_64/%.c | ||
mkdir -p $(dir $@) && \ | ||
x86_64-elf-gcc -c -I src/intf -ffreestanding $(patsubst build/x86_64/%.o, src/impl/x86_64/%.c, $@) -o $@ | ||
|
||
$(x86_64_asm_object_files): build/x86_64/%.o : src/impl/x86_64/%.asm | ||
mkdir -p $(dir $@) && \ | ||
nasm -f elf64 $(patsubst build/x86_64/%.o, src/impl/x86_64/%.asm, $@) -o $@ | ||
|
||
.PHONY: build | ||
build: $(kernel_object_files) $(x86_64_object_files) | ||
sudo mkinitramfs -o targets/x86_64/iso/boot/grub/initrd.img | ||
mkdir -p dist/x86_64 && \ | ||
x86_64-elf-ld -n -o dist/x86_64/kernel.bin -T targets/x86_64/linker.ld $(kernel_object_files) $(x86_64_object_files) && \ | ||
cp dist/x86_64/kernel.bin targets/x86_64/iso/boot/kernel.bin && \ | ||
dd if=/dev/zero of=dist/x86_64/kernel.iso bs=1M count=50 | ||
sudo mkfs.fat -F32 dist/x86_64/kernel.iso | ||
sudo fatlabel dist/x86_64/kernel.iso 9BAR-DEF0 | ||
grub-mkrescue /usr/lib/grub/i386-pc -o dist/x86_64/kernel.iso targets/x86_64/iso | ||
|
||
clean: | ||
rm -rf build | ||
rm -rf dist | ||
cd targets/x86_64/iso/boot/grub/ | ||
rm -f initrd.img |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#Os dev part 1 | ||
initrd: sudo mkinitramfs -o targets/x86_64/iso/boot/grub/initrd.img |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
make clean && make && make build | ||
qemu-system-x86_64 dist/x86_64/kernel.iso |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include "print.h" | ||
/*#include <stdarg.h> | ||
#include <stddef.h>*/ | ||
#include "types.h" | ||
|
||
void kernel_main() { | ||
|
||
int x = 6; | ||
int y = 3; | ||
int o = x * y; | ||
|
||
print_clear(); | ||
print_set_color(PRINT_COLOR_YELLOW, PRINT_COLOR_LIGHT_GREEN); | ||
print_str("Welcome to our 64-bit kernel!\n"); | ||
print_str("This is the FAT FILE SYSTEM\n"); | ||
// print_str(name); | ||
//printf("this is a %c\n", name); | ||
//print_int(ma); | ||
printf("output %d", o); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
section .multiboot_header | ||
header_start: | ||
; magic number | ||
dd 0xe85250d6 ; multiboot2 | ||
; architecture | ||
dd 0 ; protected mode i386 | ||
; header length | ||
dd header_end - header_start | ||
; checksum | ||
dd 0x100000000 - (0xe85250d6 + 0 + (header_end - header_start)) | ||
|
||
; end tag | ||
dw 0 | ||
dw 0 | ||
dd 8 | ||
header_end: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
global start | ||
extern long_mode_start | ||
|
||
section .text | ||
bits 32 | ||
start: | ||
mov esp, stack_top | ||
|
||
call check_multiboot | ||
call check_cpuid | ||
call check_long_mode | ||
|
||
call setup_page_tables | ||
call enable_paging | ||
|
||
lgdt [gdt64.pointer] | ||
jmp gdt64.code_segment:long_mode_start | ||
|
||
hlt | ||
|
||
check_multiboot: | ||
cmp eax, 0x36d76289 | ||
jne .no_multiboot | ||
ret | ||
.no_multiboot: | ||
mov al, "M" | ||
jmp error | ||
|
||
check_cpuid: | ||
pushfd | ||
pop eax | ||
mov ecx, eax | ||
xor eax, 1 << 21 | ||
push eax | ||
popfd | ||
pushfd | ||
pop eax | ||
push ecx | ||
popfd | ||
cmp eax, ecx | ||
je .no_cpuid | ||
ret | ||
.no_cpuid: | ||
mov al, "C" | ||
jmp error | ||
|
||
check_long_mode: | ||
mov eax, 0x80000000 | ||
cpuid | ||
cmp eax, 0x80000001 | ||
jb .no_long_mode | ||
|
||
mov eax, 0x80000001 | ||
cpuid | ||
test edx, 1 << 29 | ||
jz .no_long_mode | ||
ret | ||
.no_long_mode: | ||
mov al, "L" | ||
jmp error | ||
|
||
setup_page_tables: | ||
mov eax, page_table_l3 | ||
or eax, 0b11 ; present, writable | ||
mov [page_table_l4], eax | ||
mov eax, page_table_l2 | ||
or eax, 0b11 ; present, writable | ||
mov [page_table_l3], eax | ||
|
||
mov ecx, 0 ; counter | ||
.loop: | ||
|
||
mov eax, 0x200000 ; 2MiB | ||
mul ecx | ||
or eax, 0b10000011 ; present, writable, huge page | ||
mov [page_table_l2 + ecx * 8], eax | ||
|
||
inc ecx ; increment counter | ||
cmp ecx, 512 ; checks if the whole table is mapped | ||
jne .loop ; if not, continue | ||
|
||
ret | ||
|
||
enable_paging: | ||
; pass page table location to cpu | ||
mov eax, page_table_l4 | ||
mov cr3, eax | ||
|
||
; enable PAE | ||
mov eax, cr4 | ||
or eax, 1 << 5 | ||
mov cr4, eax | ||
|
||
; enable long mode | ||
mov ecx, 0xC0000080 | ||
rdmsr | ||
or eax, 1 << 8 | ||
wrmsr | ||
|
||
; enable paging | ||
mov eax, cr0 | ||
or eax, 1 << 31 | ||
mov cr0, eax | ||
|
||
ret | ||
|
||
error: | ||
; print "ERR: X" where X is the error code | ||
mov dword [0xb8000], 0x4f524f45 | ||
mov dword [0xb8004], 0x4f3a4f52 | ||
mov dword [0xb8008], 0x4f204f20 | ||
mov byte [0xb800a], al | ||
hlt | ||
|
||
section .bss | ||
align 4096 | ||
page_table_l4: | ||
resb 4096 | ||
page_table_l3: | ||
resb 4096 | ||
page_table_l2: | ||
resb 4096 | ||
stack_bottom: | ||
resb 4096 * 4 | ||
stack_top: | ||
|
||
section .rodata | ||
gdt64: | ||
dq 0 ; zero entry | ||
.code_segment: equ $ - gdt64 | ||
dq (1 << 43) | (1 << 44) | (1 << 47) | (1 << 53) ; code segment | ||
.pointer: | ||
dw $ - gdt64 - 1 ; length | ||
dq gdt64 ; address |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
global long_mode_start | ||
extern kernel_main | ||
|
||
section .text | ||
bits 64 | ||
long_mode_start: | ||
; load null into all data segment registers | ||
mov ax, 0 | ||
mov ss, ax | ||
mov ds, ax | ||
mov es, ax | ||
mov fs, ax | ||
mov gs, ax | ||
|
||
call kernel_main | ||
hlt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <stdint.h> | ||
|
||
// Read a byte from the specified port | ||
static inline uint8_t inportb(uint16_t port) { | ||
uint8_t result; | ||
__asm__ volatile ("inb %1, %0" : "=a"(result) : "dN"(port)); // we don't care asm code AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaaaaaaaaa | ||
return result; | ||
} | ||
|
||
// Write a byte to the specified port | ||
static inline void outportb(uint16_t port, uint8_t data) { | ||
__asm__ volatile ("outb %0, %1" : : "a"(data), "dN"(port)); | ||
} |
Oops, something went wrong.