Skip to content

Commit 9e7be1d

Browse files
committed
* Initial commit
* Created a basic kernel 'arthur' * Implemented basic textmode driver * Implemented basic debug functions * Implemented functions for communicating through port I/O * Ported printf implementation
0 parents  commit 9e7be1d

18 files changed

Lines changed: 1489 additions & 0 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.elf
2+
**/*.o
3+
*.iso
4+
.vscode

Makefile

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
SYSROOT=~/sysroot/
2+
ROOTDIR=$(shell pwd)
3+
SRCDIR=$(ROOTDIR)/src
4+
INCDIR=$(SRCDIR)/include
5+
GRUBDIR=~/sysroot/grub/
6+
7+
CC=$(SYSROOT)/bin/i686-elf-gcc
8+
CFLAGS=
9+
KCFLAGS= -I$(INCDIR) -fno-builtin -fno-leading-underscore -ffreestanding -Wall -O2 -nostdlib $(CFLAGS)
10+
11+
LD=$(SYSROOT)/bin/i686-elf-ld
12+
LINKERSCRIPT=$(SRCDIR)/boot/link.ld
13+
LIBGCC_PATH=$(shell $(CC) -print-libgcc-file-name)
14+
LDFLAGS= -T$(LINKERSCRIPT)
15+
16+
AS=$(SYSROOT)/bin/i686-elf-as
17+
ASFLAGS=
18+
19+
NASM=nasm
20+
NASMFLAGS=-f elf32 -g -F dwarf -O2 -w+zeroing
21+
OBJCOPY=$(SYSROOT)/bin/i686-elf-objcopy
22+
OBJDUMP=$(SYSROOT)/bin/i686-elf-objdump
23+
24+
QEMU=qemu-system-i386
25+
QEMUFLAGS=-cdrom $(ISOFILE) -m 512M -boot d
26+
QEMUDFLAGS=-s -S -daemonize
27+
28+
KERNEL_NAME=arthur
29+
OS_NAME=SectorOS
30+
31+
KERNEL_IMAGE=$(KERNEL_NAME).elf
32+
ISOFILE=$(OS_NAME).iso
33+
34+
OBJECTS= $(SRCDIR)/boot/multiboot2.o \
35+
$(SRCDIR)/boot/boot.o \
36+
$(SRCDIR)/kernel/kernel.o \
37+
$(SRCDIR)/common/debug.o \
38+
$(SRCDIR)/common/printf.o \
39+
$(SRCDIR)/drivers/io/ports.o \
40+
$(SRCDIR)/drivers/video/textmode.o
41+
42+
kernel : $(KERNEL_IMAGE)
43+
iso : $(ISOFILE)
44+
45+
$(KERNEL_IMAGE) : $(OBJECTS)
46+
@echo "[LD] $@"
47+
@$(LD) $(LDFLAGS) -o $@ $(OBJECTS) $(LIBGCC_PATH)
48+
49+
$(ISOFILE) : $(KERNEL_IMAGE)
50+
@echo '[GRUB] $@'
51+
@mkdir -p $(OS_NAME)/boot/grub
52+
@cp $(ROOTDIR)/docs/grub.cfg $(OS_NAME)/boot/grub/grub.cfg
53+
@cp $(KERNEL_IMAGE) $(OS_NAME)/boot/
54+
@$(GRUBDIR)bin/grub-mkrescue -o $(ISOFILE) $(OS_NAME) --product-name=$(OS_NAME)
55+
@rm -rf $(OS_NAME)
56+
57+
%.o : %.c
58+
@echo '[CC] $@'
59+
@$(CC) $(KCFLAGS) -c -o $@ $<
60+
61+
%.o : %.s
62+
@echo '[AS] $@'
63+
@$(AS) $(ASFLAGS) -o $@ $<
64+
65+
%.o : %.asm
66+
@echo '[NASM] $@'
67+
@$(NASM) $(NASMFLAGS) -o $@ $<
68+
69+
run : $(ISOFILE)
70+
@echo '[QEMU] Running OS...'
71+
@$(QEMU) $(QEMUFLAGS) -serial stdio
72+
73+
PHONY: clean
74+
clean:
75+
@echo 'Cleaning the source directory...'
76+
@rm -f $(OBJECTS) $(KERNEL_IMAGE) $(ISOFILE)

docs/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SectorOS Project
2+
3+
SectorOS is a 32bit operating system.

docs/grub.cfg

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
set timeout=3
2+
set default=0
3+
set root=(cd)
4+
5+
menuentry "SectorOS" {
6+
multiboot2 /boot/arthur.elf
7+
boot
8+
}

src/boot/boot.asm

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
section .initial_stack, nobits
2+
align 4
3+
__KERNEL_STACK_BOTTOM:
4+
resb 104856
5+
__KERNEL_STACK_TOP:
6+
7+
8+
section .text
9+
global __KERNEL_BOOTSTRAP
10+
extern kernelMain
11+
__KERNEL_BOOTSTRAP:
12+
mov esp,__KERNEL_STACK_TOP
13+
push eax
14+
push ebx
15+
call kernelMain
16+
17+
__KERNEL_EXECUTION_STOP:
18+
cli
19+
hlt
20+
jmp __KERNEL_EXECUTION_STOP

src/boot/link.ld

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
ENTRY(__KERNEL_BOOTSTRAP)
2+
3+
SECTIONS
4+
{
5+
. = 2M;
6+
7+
.multiboot2 BLOCK(4K) : ALIGN(4K)
8+
{
9+
*(.multiboot2)
10+
}
11+
12+
.text BLOCK(4K) : ALIGN(4K)
13+
{
14+
*(.text)
15+
}
16+
17+
.rodata BLOCK(4K) : ALIGN(4K)
18+
{
19+
*(.rodata)
20+
}
21+
22+
.data BLOCK(4K) : ALIGN(4K)
23+
{
24+
*(.data)
25+
}
26+
27+
.bss BLOCK(4K) : ALIGN(4K)
28+
{
29+
*(.initial_stack)
30+
*(COMMON)
31+
*(.bss)
32+
}
33+
34+
__KERNEL_IMAGE_END = .;
35+
}

src/boot/multiboot2.s

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Multiboot 2 specification from https://gnu.org/software/grub/manual/multiboot2/multiboot.html
2+
3+
.set MULTIBOOT2_MAGIC, 0xE85250D6
4+
.set MULTIBOOT2_ARCH, 0
5+
6+
.section .multiboot2
7+
__MULTIBOOT2_HEADER_START:
8+
.long MULTIBOOT2_MAGIC
9+
.long MULTIBOOT2_ARCH
10+
.long __MULTIBOOT2_HEADER_END - __MULTIBOOT2_HEADER_START
11+
.long -(MULTIBOOT2_MAGIC+MULTIBOOT2_ARCH+(__MULTIBOOT2_HEADER_END-__MULTIBOOT2_HEADER_START))
12+
.align 8
13+
14+
__MULTIBOOT2_TAG_ALIGN_MODULE:
15+
.short 0x06
16+
.short 0x00
17+
.long 8
18+
.align 8
19+
20+
__MULTIBOOT2_TAG_END:
21+
.short 0
22+
.short 0
23+
.long 8
24+
.align 8
25+
26+
__MULTIBOOT2_HEADER_END:
27+
28+
// MULTIBOOT2 HEADER END
29+
30+

src/common/debug.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "debug.h"
2+
#include "printf.h"
3+
#include "textmode.h"
4+
5+
//TODO: Add support for register printing
6+
void kernel_panic(char* message)
7+
{
8+
textmode_chattr(15, 4);
9+
textmode_clear();
10+
11+
textmode_puts("\"KERNEL HAS POPPED\"\n\nAn unrecoverable error has been occurred in the kernel. The kernel will be halting...\n\nMessage: ");
12+
textmode_puts(message);
13+
textmode_puts("\n");
14+
15+
__KERNEL_ASM("cli; hlt");
16+
}
17+
18+
void kernel_assert(const char* file, int line, const char* statement, bool state)
19+
{
20+
if(state == true)
21+
return;
22+
23+
textmode_chattr(15, 4);
24+
textmode_clear();
25+
26+
printf("\"TACTICAL NUKE INCOMING...\"\n\nAn assertion has failed in the kernel. Kernel cannot verify its stability. Therefore, kernel will be halting...\n\nKernel: %s\nFile: %s:%d\nStatement: %s\n", __KERNEL_NAME" v"__KERNEL_VERSION, file, line, statement);
27+
28+
__KERNEL_ASM("cli; hlt");
29+
}

0 commit comments

Comments
 (0)