Skip to content

Commit d306cbc

Browse files
committed
Simple Kernel with no input driver exec'ing hardcoded Calc
1 parent 953b12d commit d306cbc

File tree

18 files changed

+351
-67
lines changed

18 files changed

+351
-67
lines changed

Makefile

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
BL_SRC_DIR = src/bootloader
2+
KERNEL_SRC_DIR = src/kernel
23
SYSCALLS_SRC_DIR = src/lib/syscalls
34
LIB_SRC_DIR = src/lib
45
UTIL_SRC_DIR = src/lib/util
@@ -15,6 +16,11 @@ bt_stage2 = $(BUILD_DIR)/bt_stage2
1516
image_vmdk = $(BUILD_DIR)/image.vmdk
1617
app_entry_o = $(BUILD_DIR)/app_entry.o
1718

19+
# Kernel
20+
kernel_core = $(BUILD_DIR)/kernel_core
21+
kernel_core_c_o = $(BUILD_DIR)/kernel_core_c.o
22+
kernel_core_asm_o = $(BUILD_DIR)/kernel_core_asm.o
23+
1824
# Apps
1925
app_calc = $(BUILD_DIR)/calc
2026
app_ttt = $(BUILD_DIR)/ttt
@@ -32,25 +38,33 @@ all: images binaries
3238

3339
images: $(image_vmdk)
3440

35-
binaries: $(bt_stage1) $(bt_stage2)
41+
binaries: $(bt_stage1) $(bt_stage2) $(kernel_core)
3642

37-
$(image_vmdk): $(bt_stage1) $(bt_stage2) $(app_calc) $(app_ttt)
43+
$(image_vmdk): $(bt_stage1) $(bt_stage2) $(kernel_core) $(app_calc) $(app_ttt)
3844
dd bs=512 count=2 if=$(bt_stage1) of=$@
3945
/bin/echo -ne "\x55\xaa" | dd seek=510 bs=1 of=$@
40-
cat $(bt_stage2) >> $@
41-
cat $(app_calc) >> $@
42-
cat $(app_ttt) >> $@
4346
@echo "Stage 1 Size : " $$(stat -c %s $(bt_stage1))
47+
@echo "BT_STAGE2_SECTOR_START : "$$(( 1 + $$(stat -c %s $(image_vmdk)) / 512 ))
48+
cat $(bt_stage2) >> $@
4449
@echo "Stage 2 Size : " $$(stat -c %s $(bt_stage2))
45-
@echo "App Calc Size : " $$(stat -c %s $(app_calc))
46-
@echo "App TTT Size : " $$(stat -c %s $(app_ttt))
47-
@echo "Image Size : " $$(stat -c %s $@)
4850
@echo "Want BT_STAGE2_SECTOR_COUNT : 0x"$$(printf "%x\n" $$(( $$(stat -c %s $(bt_stage2)) / 512)) )
4951
@echo "Got BT_STAGE2_SECTOR_COUNT : 0x"$(BT_STAGE2_SECTOR_COUNT)
52+
53+
@echo "AppCalc Sector Start : "$$(( 1 + $$(stat -c %s $(image_vmdk)) / 512 ))
54+
cat $(app_calc) >> $@
5055
@echo "AppCalc Sector Count : "$$(( $$(stat -c %s $(app_calc)) / 512))
51-
@echo "AppCalc Sector Start : "$$(( 1 + $$(stat -c %s $(bt_stage1)) / 512 + $$(stat -c %s $(bt_stage2)) / 512))
56+
@echo "App Calc Size : " $$(stat -c %s $(app_calc))
57+
58+
@echo "AppTTT Sector Start : "$$(( 1 + $$(stat -c %s $(image_vmdk)) / 512 ))
59+
cat $(app_ttt) >> $@
5260
@echo "AppTTT Sector Count : "$$(( $$(stat -c %s $(app_ttt)) / 512))
53-
@echo "AppTTT Sector Start : "$$(( 1 + $$(stat -c %s $(bt_stage1)) / 512 + $$(stat -c %s $(bt_stage2)) / 512 + $$(stat -c %s $(app_calc)) / 512))
61+
@echo "App TTT Size : " $$(stat -c %s $(app_ttt))
62+
63+
@echo "Kernel Core Sector Start : "$$(( 1 + $$(stat -c %s $(image_vmdk)) / 512 ))
64+
cat $(kernel_core) >> $@
65+
@echo "Kernel Core Sector Count : "$$(( $$(stat -c %s $(kernel_core)) / 512))
66+
@echo "Kernel Core Size : " $$(stat -c %s $(kernel_core))
67+
@echo "Image Size : " $$(stat -c %s $@)
5468

5569
$(bt_stage1): $(BL_SRC_DIR)/stage1.asm $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(BL_SRC_DIR)/disk.asm
5670
nasm -o $@ -f bin -i $(BL_SRC_DIR)/ -D BT_STAGE2_SECTOR_COUNT=$(BT_STAGE2_SECTOR_COUNT) $<
@@ -63,18 +77,28 @@ $(bt_stage2): $(bt_stage2_asm_o) $(bt_stage2_c_o)
6377
$(bt_stage2_c_o): $(BL_SRC_DIR)/stage2.c $(SYSCALLS_SRC_DIR)/basic.h $(SYSCALLS_SRC_DIR)/io.h $(SYSCALLS_SRC_DIR)/time.h $(SYSCALLS_SRC_DIR)/color.h $(UTIL_SRC_DIR)/string.h $(APP_DIR)/dashboard.c
6478
gcc -m16 -fno-pie -c -Isrc -o $@ $<
6579

66-
$(bt_stage2_asm_o): $(BL_SRC_DIR)/stage2.asm $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(SYSCALLS_SRC_DIR)/io_syscall.asm $(SYSCALLS_SRC_DIR)/time_syscall.asm $(bt_stage2_o) $(SYSCALLS_SRC_DIR)/disk_syscall.asm
80+
$(bt_stage2_asm_o): $(BL_SRC_DIR)/stage2.asm $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(SYSCALLS_SRC_DIR)/io_interface_bios.asm $(SYSCALLS_SRC_DIR)/time_syscall.asm $(bt_stage2_o) $(SYSCALLS_SRC_DIR)/disk_syscall.asm
6781
nasm -o $@ -f elf32 -i $(BL_SRC_DIR)/ -i$(SYSCALLS_SRC_DIR)/ $<
6882

69-
$(app_entry_o): $(LIB_SRC_DIR)/app/entry.asm $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(SYSCALLS_SRC_DIR)/io_syscall.asm $(SYSCALLS_SRC_DIR)/time_syscall.asm
83+
$(kernel_core): $(kernel_core_asm_o) $(kernel_core_c_o)
84+
ld --oformat binary -m elf_i386 -Ttext 0xC000 --strip-all -o $@ $^
85+
truncate --size=%512 $@
86+
87+
$(kernel_core_asm_o): $(KERNEL_SRC_DIR)/core.asm
88+
nasm -o $@ -f elf32 -i $(KERNEL_SRC_DIR)/ -i$(SYSCALLS_SRC_DIR)/ $<
89+
90+
$(kernel_core_c_o): $(KERNEL_SRC_DIR)/core.c
91+
gcc -m16 -fno-pie -c -Isrc -o $@ $<
92+
93+
$(app_entry_o): $(LIB_SRC_DIR)/app/entry.asm $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(SYSCALLS_SRC_DIR)/io_interface_bios.asm $(SYSCALLS_SRC_DIR)/time_syscall.asm
7094
nasm -o $@ -f elf32 -i $(BL_SRC_DIR)/ -i$(SYSCALLS_SRC_DIR)/ $<
7195

72-
$(app_calc): $(app_entry_o) $(APP_DIR)/calc.c $(SYSCALLS_SRC_DIR)/basic.h $(SYSCALLS_SRC_DIR)/io.h $(SYSCALLS_SRC_DIR)/time.h $(SYSCALLS_SRC_DIR)/color.h $(UTIL_SRC_DIR)/string.h $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(SYSCALLS_SRC_DIR)/io_syscall.asm $(SYSCALLS_SRC_DIR)/time_syscall.asm
96+
$(app_calc): $(app_entry_o) $(APP_DIR)/calc.c $(SYSCALLS_SRC_DIR)/basic.h $(SYSCALLS_SRC_DIR)/io.h $(SYSCALLS_SRC_DIR)/time.h $(SYSCALLS_SRC_DIR)/color.h $(UTIL_SRC_DIR)/string.h $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(SYSCALLS_SRC_DIR)/io_interface_bios.asm $(SYSCALLS_SRC_DIR)/time_syscall.asm
7397
gcc -m16 -fno-pie -c -Isrc -o $(BUILD_DIR)/calc.o $(APP_DIR)/calc.c
74-
ld --oformat binary -m elf_i386 -Ttext 0xC000 --strip-all -o $@ $(app_entry_o) $(BUILD_DIR)/calc.o
98+
ld --oformat binary -m elf_i386 -Ttext 0x2000 --strip-all -o $@ $(app_entry_o) $(BUILD_DIR)/calc.o
7599
truncate --size=%512 $@
76100

77-
$(app_ttt): $(app_entry_o) $(APP_DIR)/tic_tac_toe.c $(SYSCALLS_SRC_DIR)/basic.h $(SYSCALLS_SRC_DIR)/io.h $(SYSCALLS_SRC_DIR)/time.h $(SYSCALLS_SRC_DIR)/color.h $(UTIL_SRC_DIR)/string.h $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(SYSCALLS_SRC_DIR)/io_syscall.asm $(SYSCALLS_SRC_DIR)/time_syscall.asm
101+
$(app_ttt): $(app_entry_o) $(APP_DIR)/tic_tac_toe.c $(SYSCALLS_SRC_DIR)/basic.h $(SYSCALLS_SRC_DIR)/io.h $(SYSCALLS_SRC_DIR)/time.h $(SYSCALLS_SRC_DIR)/color.h $(UTIL_SRC_DIR)/string.h $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(SYSCALLS_SRC_DIR)/io_interface_bios.asm $(SYSCALLS_SRC_DIR)/time_syscall.asm
78102
gcc -m16 -fno-pie -c -Isrc -o $(BUILD_DIR)/ttt.o $(APP_DIR)/tic_tac_toe.c
79103
ld --oformat binary -m elf_i386 -Ttext 0xC000 --strip-all -o $@ $(app_entry_o) $(BUILD_DIR)/ttt.o
80104
truncate --size=%512 $@
@@ -95,11 +119,23 @@ debug_stage2_asm: $(bt_stage2_asm_o)
95119
objdump -d -Maddr16,data16 $<
96120
xxd $<
97121

122+
debug_kernel: $(kernel_core)
123+
objdump -b binary -mi386 -Maddr16,data16 -D $<
124+
xxd $<
125+
126+
debug_kernel_c: $(kernel_core_c_o)
127+
objdump -d -Maddr16,data16 $<
128+
xxd $<
129+
130+
debug_kernel_asm: $(kernel_core_asm_o)
131+
objdump -d -Maddr16,data16 $<
132+
xxd $<
133+
98134
qemu: $(image_vmdk) images
99135
cpulimit -f -l 10 -- qemu-system-x86_64 -smp 1 -m 128M -hda $< -no-shutdown -no-reboot
100136

101137
qemu_debug: $(image_vmdk) images
102138
qemu-system-x86_64 -smp 1 -m 128M -hda $< -no-shutdown -no-reboot -d cpu,exec,in_asm
103139

104140
clean:
105-
rm -f $(image_vmdk) $(bt_stage1) $(bt_stage2) $(bt_stage2_c_o) $(bt_stage2_asm_o)
141+
rm -f $(image_vmdk) $(bt_stage1) $(bt_stage2) $(bt_stage2_c_o) $(bt_stage2_asm_o) $(kernel_core) $(kernel_core_c_o) $(kernel_core_asm_o) $(app_calc) $(app_ttt)

src/app/calc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ void print_int(int x) {
1313
}
1414
#else
1515
#include <lib/syscalls/io.h>
16+
#include <lib/syscalls/io_interface_protected.c>
1617
#include <lib/syscalls/time.h>
1718
#include <lib/util/string.h>
1819
#endif
@@ -106,7 +107,7 @@ int handle_expression(char str[]) {
106107
void console_init() {
107108
set_color_bg(C_BLACK);
108109
set_color_fg(C_WHITE);
109-
print_rectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
110+
print_rectangle(0, 0, WINDOW_WIDTH-1, WINDOW_HEIGHT-1);
110111
move_xy(0,0);
111112
}
112113

src/app/dashboard.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <app/calc.c>
2+
#include <lib/syscalls/io_interface_bios.c>
23
#include <lib/syscalls/io.h>
34
#include <lib/syscalls/time.h>
45
#include <lib/syscalls/color.h>
@@ -22,13 +23,13 @@ void print_applications(unsigned char x, unsigned char y, char *list, unsigned c
2223
void print_board() {
2324
set_color_bg(C_DARK_GRAY);
2425
set_color_fg(C_WHITE);
25-
print_rectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
26+
print_rectangle(0, 0, WINDOW_WIDTH-1, WINDOW_HEIGHT-1);
2627
move_xy(1,0);
2728
print_line("Fuzzy OS");
2829

2930
set_color_bg(C_LIGHT_GRAY);
3031
set_color_fg(C_BLACK);
31-
print_rectangle(1, 1, WINDOW_WIDTH-1, WINDOW_HEIGHT-1);
32+
print_rectangle(1, 1, WINDOW_WIDTH-2, WINDOW_HEIGHT-2);
3233
move_xy(3,3);
3334

3435
print_line(query_app_number);
@@ -50,7 +51,7 @@ int run_dashboard(int argc,char **argv) {
5051
int err = load_sectors(0xC000, 0x80, 52, 25);
5152
if(err) {
5253
set_color_bg(C_DARK_GRAY);
53-
move_xy(2,WINDOW_HEIGHT);
54+
move_xy(2,WINDOW_HEIGHT-1);
5455
print_line("Failed to load app in memory, Error: ");
5556
print_int(err);
5657
sleep(1000);
@@ -61,7 +62,7 @@ int run_dashboard(int argc,char **argv) {
6162

6263
print_board();
6364
set_color_bg(C_DARK_GRAY);
64-
move_xy(2,WINDOW_HEIGHT);
65+
move_xy(2,WINDOW_HEIGHT-1);
6566
print_line("Program Exited: ");
6667
print_int(return_status);
6768
sleep(1000);

src/app/tic_tac_toe.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define BUILD_FOR_FUZZY
33
#ifndef BUILD_FOR_FUZZY
44
#else
5+
#include <lib/syscalls/io_interface_protected.c>
56
#include <lib/syscalls/io.h>
67
#include <lib/syscalls/time.h>
78
#include <lib/util/string.h>
@@ -10,7 +11,7 @@
1011
void console_init() {
1112
set_color_bg(C_BLACK);
1213
set_color_fg(C_WHITE);
13-
print_rectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
14+
print_rectangle(0, 0, WINDOW_WIDTH-1, WINDOW_HEIGHT-1);
1415
move_xy(0,0);
1516
}
1617

src/bootloader/stage2.asm

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
; Fuzzy Bootloader Stage 2
22
%include "constants.asm"
33
%include "io.asm"
4-
%include "io_syscall.asm"
4+
%include "io_interface_bios.asm"
55
%include "time_syscall.asm"
66
%include "disk_syscall.asm"
77

88
[BITS 16]
99

1010
extern entry_stage
1111
global enter_protected_mode
12+
global label_exit
1213

1314
[SECTION .text]
1415
CLI
@@ -86,5 +87,5 @@ global enter_protected_mode
8687
mov al,'e'
8788
mov [ebx],ax
8889

89-
; TODO: Will improve this soon.
90-
hlt
90+
; Hardcoded Kernel Load Address
91+
jmp 0x08:0xC000

src/bootloader/stage2.c

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
#include <app/dashboard.c>
1+
#include <lib/syscalls/color.h>
2+
#include <lib/syscalls/disk.h>
3+
#include <lib/syscalls/io_interface_bios.c>
24
#include <lib/syscalls/io.h>
35
#include <lib/syscalls/time.h>
4-
#include <lib/syscalls/color.h>
56

67
char message_welcome[] = "C says 'Hello World'";
78
char message_dashboard[] = "Opening App 'Dashboard'";
9+
char message_kernel_loading[] = "Loading Kernel....";
10+
char message_calc_loading[] = "Loading Calc....";
811
char message_protected_mode[] = "Enabling Protected Mode...";
912

1013
#define GDT_TABLE_SIZE 3
@@ -42,6 +45,7 @@ void populate_gct_entry(struct GDTEntry *entry,
4245
}
4346

4447
extern void enter_protected_mode(int gdtr_address);
48+
extern void label_exit();
4549

4650
int populate_gdt_table() {
4751
// Assumption DS = 0
@@ -67,26 +71,52 @@ int populate_gdt_table() {
6771
gdtr.size = (sizeof(gdt_table));
6872

6973
// Print the table and table addresse.
70-
move_xy(6,14);
74+
move_xy(8,15);
7175
print_int((int)&gdtr);
72-
move_xy(6,15);
76+
move_xy(8,16);
7377
print_int(gdtr.base_address);
74-
move_xy(6,16);
78+
move_xy(8,17);
7579
print_int(gdtr.size);
7680
for(int i=0;i<GDT_TABLE_SIZE;i++) {
77-
move_xy(6,17+i);
81+
move_xy(8,18+i);
7882
print_memory_hex(8*i+(char*)&gdt_table, 8);
7983
}
8084
return (int)&gdtr;
8185
}
8286

87+
void load_kernel() {
88+
int err = load_sectors(0xC000, 0x80, DISK_KERNEL_SECTOR_START, DISK_KERNEL_SECTOR_COUNT);
89+
if(err) {
90+
print_line("Failed to load kernel in memory.");
91+
label_exit();
92+
} else {
93+
print_memory_hex((char*)0xC000, 16);
94+
}
95+
}
96+
97+
void load_calc() {
98+
int err = load_sectors(0x2000, 0x80, 27, 25);
99+
if(err) {
100+
print_line("Failed to load calc in memory.");
101+
label_exit();
102+
} else {
103+
print_memory_hex((char*)0x2000, 16);
104+
}
105+
}
106+
83107
void entry_stage() {
84108
move_xy(6, 11);
85109
set_color_bg(C_BLACK);
86110
set_color_fg(C_GREEN);
87111
print_line(message_welcome);
88112
set_color_fg(C_WHITE);
113+
move_xy(6, 12);
114+
print_line(message_kernel_loading);
115+
load_kernel();
89116
move_xy(6, 13);
117+
print_line(message_calc_loading);
118+
load_calc();
119+
move_xy(6, 14);
90120
print_line(message_protected_mode);
91121
int gdtr_address = populate_gdt_table();
92122
// Note: enter_protected_mode never returns.

src/kernel/core.asm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
%include "io_interface_protected.asm"
2+
3+
[BITS 16]
4+
5+
extern entry_core
6+
7+
[SECTION .text]
8+
jmp entry_core
9+
10+
PLUGIN_SYSCALLS_IO_PROTECTED
11+

src/kernel/core.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <lib/syscalls/io.h>
2+
#include <lib/syscalls/io_interface_protected.c>
3+
4+
char welcome_message[] = "Initializing Kernel...";
5+
6+
int call_main(unsigned short cs,unsigned short ip, int argc, char *argv[]) {
7+
return ((int (*) (int, char *[]))(unsigned int)ip)(argc, argv);
8+
}
9+
10+
void exec(int sector_index, int sector_count){
11+
int memory_address = 0x2000;
12+
int err = 0;
13+
// int err = load_sectors(memory_address, 0x80, 52, 25);
14+
if(err) {
15+
move_xy(3,4);
16+
print_line("Failed to load app in memory, Error: ");
17+
print_int(err);
18+
} else {
19+
call_main(0, memory_address, 0, 0);
20+
print_line("App Exited.");
21+
}
22+
}
23+
24+
void entry_core() {
25+
set_color_bg(C_BLACK);
26+
set_color_fg(C_WHITE);
27+
28+
move_xy(3,3);
29+
print_line(welcome_message);
30+
exec(0,0);
31+
while(1);
32+
}

src/lib/app/entry.asm

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
; Any app entry point
22
%include "constants.asm"
3-
%include "io.asm"
4-
%include "io_syscall.asm"
3+
%include "io_interface_protected.asm"
54
%include "time_syscall.asm"
65
%include "disk_syscall.asm"
76

@@ -12,6 +11,6 @@ extern main
1211
[SECTION .text]
1312
jmp main
1413

15-
PLUGIN_SYSCALLS_IO
14+
PLUGIN_SYSCALLS_IO_PROTECTED
1615
PLUGIN_SYSCALLS_TIME
1716
PLUGIN_SYSCALLS_DISK

src/lib/syscalls/basic.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#ifndef __LIB_SYSCALLS_BASIC
22
#define __LIB_SYSCALLS_BASIC
33

4-
#define WINDOW_HEIGHT 0x18
5-
#define WINDOW_WIDTH 0x4F
4+
#define WINDOW_HEIGHT 0x19
5+
#define WINDOW_WIDTH 0x50
66
#define CLOCKS_PER_MS 56230 // 5% of Host CPU
77

88
#define INT_MAX 0xFFFFFFFF

0 commit comments

Comments
 (0)