Skip to content

Commit 28cdd6d

Browse files
Add files via upload
1 parent 3cfc6c7 commit 28cdd6d

File tree

15 files changed

+516
-0
lines changed

15 files changed

+516
-0
lines changed

partt3/Makefile

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
sudo mkinitramfs -o targets/x86_64/iso/boot/grub/initrd.img
27+
mkdir -p dist/x86_64 && \
28+
x86_64-elf-ld -n -o dist/x86_64/kernel.bin -T targets/x86_64/linker.ld $(kernel_object_files) $(x86_64_object_files) && \
29+
cp dist/x86_64/kernel.bin targets/x86_64/iso/boot/kernel.bin && \
30+
dd if=/dev/zero of=dist/x86_64/kernel.iso bs=1M count=50
31+
sudo mkfs.fat -F32 dist/x86_64/kernel.iso
32+
sudo fatlabel dist/x86_64/kernel.iso 9BAR-DEF0
33+
grub-mkrescue /usr/lib/grub/i386-pc -o dist/x86_64/kernel.iso targets/x86_64/iso
34+
35+
clean:
36+
rm -rf build
37+
rm -rf dist
38+
cd targets/x86_64/iso/boot/grub/
39+
rm -f initrd.img

partt3/Readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#Os dev part 1
2+
initrd: sudo mkinitramfs -o targets/x86_64/iso/boot/grub/initrd.img

partt3/run.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
make clean && make && make build
2+
qemu-system-x86_64 dist/x86_64/kernel.iso

partt3/src/impl/kernel/main.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "print.h"
2+
/*#include <stdarg.h>
3+
#include <stddef.h>*/
4+
#include "types.h"
5+
6+
void kernel_main() {
7+
8+
int x = 6;
9+
int y = 3;
10+
int o = x * y;
11+
12+
print_clear();
13+
print_set_color(PRINT_COLOR_YELLOW, PRINT_COLOR_LIGHT_GREEN);
14+
print_str("Welcome to our 64-bit kernel!\n");
15+
print_str("This is the FAT FILE SYSTEM\n");
16+
// print_str(name);
17+
//printf("this is a %c\n", name);
18+
//print_int(ma);
19+
printf("output %d", o);
20+
}
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:

partt3/src/impl/x86_64/boot/main.asm

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

partt3/src/impl/x86_64/ports.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <stdint.h>
2+
3+
// Read a byte from the specified port
4+
static inline uint8_t inportb(uint16_t port) {
5+
uint8_t result;
6+
__asm__ volatile ("inb %1, %0" : "=a"(result) : "dN"(port)); // we don't care asm code AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaaaaaaaaa
7+
return result;
8+
}
9+
10+
// Write a byte to the specified port
11+
static inline void outportb(uint16_t port, uint8_t data) {
12+
__asm__ volatile ("outb %0, %1" : : "a"(data), "dN"(port));
13+
}

0 commit comments

Comments
 (0)