Skip to content

Commit 816f393

Browse files
Add files via upload
1 parent 742f49c commit 816f393

File tree

11 files changed

+362
-2
lines changed

11 files changed

+362
-2
lines changed

part2/Makefile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
kernel_source_files := $(shell find src/impl/kernel -name *.c)
2+
kernel_object_files := $(patsubst src/impl/kernel/%.c, build/kernel/%.o, $(kernel_source_files))
3+
4+
x86_64_c_source_files := $(shell find src/impl/x86_64 -name *.c)
5+
x86_64_c_object_files := $(patsubst src/impl/x86_64/%.c, build/x86_64/%.o, $(x86_64_c_source_files))
6+
7+
x86_64_asm_source_files := $(shell find src/impl/x86_64 -name *.asm)
8+
x86_64_asm_object_files := $(patsubst src/impl/x86_64/%.asm, build/x86_64/%.o, $(x86_64_asm_source_files))
9+
10+
x86_64_object_files := $(x86_64_c_object_files) $(x86_64_asm_object_files)
11+
12+
$(kernel_object_files): build/kernel/%.o : src/impl/kernel/%.c
13+
mkdir -p $(dir $@) && \
14+
x86_64-elf-gcc -c -I src/intf -ffreestanding $(patsubst build/kernel/%.o, src/impl/kernel/%.c, $@) -o $@
15+
16+
$(x86_64_c_object_files): build/x86_64/%.o : src/impl/x86_64/%.c
17+
mkdir -p $(dir $@) && \
18+
x86_64-elf-gcc -c -I src/intf -ffreestanding $(patsubst build/x86_64/%.o, src/impl/x86_64/%.c, $@) -o $@
19+
20+
$(x86_64_asm_object_files): build/x86_64/%.o : src/impl/x86_64/%.asm
21+
mkdir -p $(dir $@) && \
22+
nasm -f elf64 $(patsubst build/x86_64/%.o, src/impl/x86_64/%.asm, $@) -o $@
23+
24+
.PHONY: build
25+
build: $(kernel_object_files) $(x86_64_object_files)
26+
mkdir -p dist/x86_64 && \
27+
x86_64-elf-ld -n -o dist/x86_64/kernel.bin -T targets/x86_64/linker.ld $(kernel_object_files) $(x86_64_object_files) && \
28+
cp dist/x86_64/kernel.bin targets/x86_64/iso/boot/kernel.bin && \
29+
dd if=/dev/zero of=dist/x86_64/kernel.iso bs=1M count=10
30+
sudo mkfs.fat dist/x86_64/kernel.iso
31+
sudo fatlabel dist/x86_64/kernel.iso 9BAR-DEF0
32+
grub-mkrescue /usr/lib/grub/i386-pc -o dist/x86_64/kernel.iso targets/x86_64/iso
33+
34+
clean:
35+
rm -rf build
36+
rm -rf dist

part2/Readme.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1-
#part2
1+
#Os dev part 1
22

3-
. Improved filesystem
3+
Welcome to make your first os
4+
5+
Download this template and make the cross-compiler and add it to $PATH variable
6+
Then run ```make``` and ```make build-x86_64```
7+
8+
This episode includes :
9+
1. A file system
10+
2. VGA support(a little bit)
11+
12+
13+
That's all. Se ya'll next episode
14+

part2/src/impl/kernel/main.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "print.h"
2+
3+
void kernel_main() {
4+
// print_clear();
5+
print_set_color(PRINT_COLOR_YELLOW, PRINT_COLOR_LIGHT_GREEN);
6+
print_str("Welcome to our 64-bit kernel!\n");
7+
print_str("This is the FAT FILE SYSTEM");
8+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
section .multiboot_header
2+
header_start:
3+
; magic number
4+
dd 0xe85250d6 ; multiboot2
5+
; architecture
6+
dd 0 ; protected mode i386
7+
; header length
8+
dd header_end - header_start
9+
; checksum
10+
dd 0x100000000 - (0xe85250d6 + 0 + (header_end - header_start))
11+
12+
; end tag
13+
dw 0
14+
dw 0
15+
dd 8
16+
header_end:
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
global start
2+
extern long_mode_start
3+
4+
section .text
5+
bits 32
6+
start:
7+
mov esp, stack_top
8+
9+
call check_multiboot
10+
call check_cpuid
11+
call check_long_mode
12+
13+
call setup_page_tables
14+
call enable_paging
15+
16+
lgdt [gdt64.pointer]
17+
jmp gdt64.code_segment:long_mode_start
18+
19+
hlt
20+
21+
check_multiboot:
22+
cmp eax, 0x36d76289
23+
jne .no_multiboot
24+
ret
25+
.no_multiboot:
26+
mov al, "M"
27+
jmp error
28+
29+
check_cpuid:
30+
pushfd
31+
pop eax
32+
mov ecx, eax
33+
xor eax, 1 << 21
34+
push eax
35+
popfd
36+
pushfd
37+
pop eax
38+
push ecx
39+
popfd
40+
cmp eax, ecx
41+
je .no_cpuid
42+
ret
43+
.no_cpuid:
44+
mov al, "C"
45+
jmp error
46+
47+
check_long_mode:
48+
mov eax, 0x80000000
49+
cpuid
50+
cmp eax, 0x80000001
51+
jb .no_long_mode
52+
53+
mov eax, 0x80000001
54+
cpuid
55+
test edx, 1 << 29
56+
jz .no_long_mode
57+
58+
ret
59+
.no_long_mode:
60+
mov al, "L"
61+
jmp error
62+
63+
setup_page_tables:
64+
mov eax, page_table_l3
65+
or eax, 0b11 ; present, writable
66+
mov [page_table_l4], eax
67+
68+
mov eax, page_table_l2
69+
or eax, 0b11 ; present, writable
70+
mov [page_table_l3], eax
71+
72+
mov ecx, 0 ; counter
73+
.loop:
74+
75+
mov eax, 0x200000 ; 2MiB
76+
mul ecx
77+
or eax, 0b10000011 ; present, writable, huge page
78+
mov [page_table_l2 + ecx * 8], eax
79+
80+
inc ecx ; increment counter
81+
cmp ecx, 512 ; checks if the whole table is mapped
82+
jne .loop ; if not, continue
83+
84+
ret
85+
86+
enable_paging:
87+
; pass page table location to cpu
88+
mov eax, page_table_l4
89+
mov cr3, eax
90+
91+
; enable PAE
92+
mov eax, cr4
93+
or eax, 1 << 5
94+
mov cr4, eax
95+
96+
; enable long mode
97+
mov ecx, 0xC0000080
98+
rdmsr
99+
or eax, 1 << 8
100+
wrmsr
101+
102+
; enable paging
103+
mov eax, cr0
104+
or eax, 1 << 31
105+
mov cr0, eax
106+
107+
ret
108+
109+
error:
110+
; print "ERR: X" where X is the error code
111+
mov dword [0xb8000], 0x4f524f45
112+
mov dword [0xb8004], 0x4f3a4f52
113+
mov dword [0xb8008], 0x4f204f20
114+
mov byte [0xb800a], al
115+
hlt
116+
117+
section .bss
118+
align 4096
119+
page_table_l4:
120+
resb 4096
121+
page_table_l3:
122+
resb 4096
123+
page_table_l2:
124+
resb 4096
125+
stack_bottom:
126+
resb 4096 * 4
127+
stack_top:
128+
129+
section .rodata
130+
gdt64:
131+
dq 0 ; zero entry
132+
.code_segment: equ $ - gdt64
133+
dq (1 << 43) | (1 << 44) | (1 << 47) | (1 << 53) ; code segment
134+
.pointer:
135+
dw $ - gdt64 - 1 ; length
136+
dq gdt64 ; address
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
global long_mode_start
2+
extern kernel_main
3+
4+
section .text
5+
bits 64
6+
long_mode_start:
7+
; load null into all data segment registers
8+
mov ax, 0
9+
mov ss, ax
10+
mov ds, ax
11+
mov es, ax
12+
mov fs, ax
13+
mov gs, ax
14+
15+
call kernel_main
16+
hlt

part2/src/impl/x86_64/print.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include "print.h"
2+
3+
const static size_t NUM_COLS = 80;
4+
const static size_t NUM_ROWS = 25;
5+
6+
struct Char {
7+
uint8_t character;
8+
uint8_t color;
9+
};
10+
11+
struct Char* buffer = (struct Char*) 0xb8000;
12+
size_t col = 0;
13+
size_t row = 0;
14+
uint8_t color = PRINT_COLOR_WHITE | PRINT_COLOR_BLACK << 4;
15+
16+
void clear_row(size_t row) {
17+
struct Char empty = (struct Char) {
18+
character: ' ',
19+
color: color,
20+
};
21+
22+
for (size_t col = 0; col < NUM_COLS; col++) {
23+
buffer[col + NUM_COLS * row] = empty;
24+
}
25+
}
26+
27+
void print_clear() {
28+
for (size_t i = 0; i < NUM_ROWS; i++) {
29+
clear_row(i);
30+
}
31+
}
32+
33+
void print_newline() {
34+
col = 0;
35+
36+
if (row < NUM_ROWS - 1) {
37+
row++;
38+
return;
39+
}
40+
41+
for (size_t row = 1; row < NUM_ROWS; row++) {
42+
for (size_t col = 0; col < NUM_COLS; col++) {
43+
struct Char character = buffer[col + NUM_COLS * row];
44+
buffer[col + NUM_COLS * (row - 1)] = character;
45+
}
46+
}
47+
48+
clear_row(NUM_COLS - 1);
49+
}
50+
51+
void print_char(char character) {
52+
if (character == '\n') {
53+
print_newline();
54+
return;
55+
}
56+
57+
if (col > NUM_COLS) {
58+
print_newline();
59+
}
60+
61+
buffer[col + NUM_COLS * row] = (struct Char) {
62+
character: (uint8_t) character,
63+
color: color,
64+
};
65+
66+
col++;
67+
}
68+
69+
void print_str(char* str) {
70+
for (size_t i = 0; 1; i++) {
71+
char character = (uint8_t) str[i];
72+
73+
if (character == '\0') {
74+
return;
75+
}
76+
77+
print_char(character);
78+
}
79+
}
80+
81+
void print_set_color(uint8_t foreground, uint8_t background) {
82+
color = foreground + (background << 4);
83+
}

part2/src/intf/print.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#pragma once
2+
3+
#include <stdint.h>
4+
#include <stddef.h>
5+
6+
enum {
7+
PRINT_COLOR_BLACK = 0,
8+
PRINT_COLOR_BLUE = 1,
9+
PRINT_COLOR_GREEN = 2,
10+
PRINT_COLOR_CYAN = 3,
11+
PRINT_COLOR_RED = 4,
12+
PRINT_COLOR_MAGENTA = 5,
13+
PRINT_COLOR_BROWN = 6,
14+
PRINT_COLOR_LIGHT_GRAY = 7,
15+
PRINT_COLOR_DARK_GRAY = 8,
16+
PRINT_COLOR_LIGHT_BLUE = 9,
17+
PRINT_COLOR_LIGHT_GREEN = 10,
18+
PRINT_COLOR_LIGHT_CYAN = 11,
19+
PRINT_COLOR_LIGHT_RED = 12,
20+
PRINT_COLOR_PINK = 13,
21+
PRINT_COLOR_YELLOW = 14,
22+
PRINT_COLOR_WHITE = 15,
23+
};
24+
25+
void print_clear();
26+
void print_char(char character);
27+
void print_str(char* string);
28+
void print_set_color(uint8_t foreground, uint8_t background);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
set timeout=0
2+
set default=0
3+
4+
menuentry "MITHUOS" {
5+
multiboot2 /boot/kernel.bin
6+
boot
7+
initrd initrd.img
8+
insmod fat
9+
search --no-floppy --fs-uuid --set=root 9BAR-DEF0
10+
}
7.59 KB
Binary file not shown.

0 commit comments

Comments
 (0)