Skip to content

Commit

Permalink
Prepare for adding multiple architecures in OS
Browse files Browse the repository at this point in the history
* Need place to hold arch specific stuff
* Abstraction layer for multiple platforms

Signed-off-by: Mahavir Jain <[email protected]>
  • Loading branch information
mahavirj authored and Mahavir Jain committed Apr 1, 2017
1 parent dc8312e commit bdc60a2
Show file tree
Hide file tree
Showing 43 changed files with 244 additions and 159 deletions.
135 changes: 4 additions & 131 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,140 +1,17 @@

# Compiler GCC
CC := gcc
AS := nasm
OBJDUMP := objdump
OBJCOPY := objcopy
AR := ar

# Tiny OS version
VERSION := 0.2

# Architecture
ARCH ?= x86

# Verbose build pass verbose=1
ifeq ($(verbose),1)
V :=
else
V := @
endif

# Build artifacts
objdir := bin
os_image := $(objdir)/os.iso
kernel := $(objdir)/kernel.elf

kernel_src_dir := \
kernel \
drivers \
stdlib \

boot_src_dir := boot

app_src_dir := app

app_obj_dir := $(objdir)/$(app_src_dir)

c_srcs := kernel/cpio_parser.c \
kernel/gdt.c \
kernel/idt.c \
kernel/isr.c \
kernel/keyboard.c \
kernel/main.c \
kernel/mem.c \
kernel/sync.c \
kernel/syscall.c \
kernel/task.c \
kernel/timer.c \
kernel/vm.c \
kernel/wait_queue.c \
drivers/vga.c \
stdlib/stdlib.c \
stdlib/printk.c \

asm_srcs := boot/boot.s \
boot/helper.s \
boot/isr.s \
boot/swtch.s \
boot/sync.s \

c_objs := $(c_srcs:%.c=$(objdir)/%.o)
asm_objs := $(asm_srcs:%.s=$(objdir)/%.o)

app_lib_srcs := stdlib/crt.c \
stdlib/stdlib.c \
stdlib/printf.c \
stdlib/malloc.c \

app_asm_srcs := boot/syscall.s \

app_lib_objs := $(app_lib_srcs:%.c=$(objdir)/%.o)
app_asm_objs := $(app_asm_srcs:%.s=$(objdir)/%.o)

app_lib := $(objdir)/lib_helper.a

CFLAGS := -g -O2 -m32 -ffreestanding -Wall -Wextra -DVERSION=\"$(VERSION)\"
CFLAGS += -Iinclude/kernel \
-Iinclude/drivers \

LDSCRIPT = -T ldscript/linker.ld
APP_LDSCRIPT = -T app/linker.ld
LDFLAGS = -nostdlib -Wl,--build-id=none
APP_CFLAGS := -g -O2 -m32 -static -fno-pic -Wall -Wextra -ffreestanding -I app/
APP_LDFLAGS := -nostdlib -Ttext 0x100000 -Wl,--build-id=none
ASFLAGS = -f elf

define make-repo
for dir in $(kernel_src_dir) $(boot_src_dir) $(app_src_dir); \
do \
mkdir -p $(objdir)/$$dir; \
done
endef

all: pre-build $(kernel) $(os_image)

ramfs.obj: $(app_lib) $(app_obj_dir)/init $(app_obj_dir)/shell $(app_obj_dir)/forktest $(app_obj_dir)/memtest
$(V)cd $(app_obj_dir) && find . | cpio -o -H newc > ../ramfs.cpio
$(V)cd $(objdir) && $(OBJCOPY) -I binary -O elf32-i386 -B i386 ramfs.cpio $@

$(app_lib): $(app_lib_objs) $(app_asm_objs)
$(V)$(AR) cru $@ $^

$(app_obj_dir)/forktest: $(app_src_dir)/forktest.c $(app_lib)
@echo " APP $<"
$(V)$(CC) $(APP_CFLAGS) $(APP_LDFLAGS) $^ -o $@

$(app_obj_dir)/memtest: $(app_src_dir)/memtest.c $(app_lib)
@echo " APP $<"
$(V)$(CC) $(APP_CFLAGS) $(APP_LDFLAGS) $^ -o $@

$(app_obj_dir)/init: $(app_src_dir)/init.c $(app_lib)
@echo " APP $<"
$(V)$(CC) $(APP_CFLAGS) $(APP_LDFLAGS) $^ -o $@

$(app_obj_dir)/shell: $(app_src_dir)/shell.c $(app_lib)
@echo " APP $<"
$(V)$(CC) $(APP_CFLAGS) $(APP_LDFLAGS) $^ -o $@

pre-build:
@mkdir -p $(objdir)
@$(call make-repo)

$(kernel): ldscript/linker.ld $(asm_objs) $(c_objs) ramfs.obj
@echo " LD $@"
$(V)$(CC) $(CFLAGS) $(LDFLAGS) $(LDSCRIPT) $(asm_objs) $(c_objs) $(objdir)/ramfs.obj -o $@
$(V)$(OBJDUMP) -t $@ | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $(objdir)/kernel.sym

$(os_image): $(kernel)
$(V)cp $< iso/boot/
$(V)genisoimage -R -b boot/grub/grub -no-emul-boot -boot-load-size 4 \
-A os -input-charset utf8 -quiet -boot-info-table -o $@ iso

run: all
bochs -qf tools/bochsrc.txt -rc tools/bochsrc.debug

qemu: all
qemu-system-i386 -cdrom bin/os.iso -m 32 -monitor stdio

qemu_gdb: all
qemu-system-i386 -cdrom bin/os.iso -m 32 -s -S -monitor stdio
include build/rules-$(ARCH).mk

$(objdir)/%.o: %.c
@echo " CC $<"
Expand All @@ -144,8 +21,4 @@ $(objdir)/%.o: %.s
@echo " ASM $<"
$(V)$(AS) $(ASFLAGS) $< -o $@

clean:
@rm -rf bin/
@rm -f iso/boot/$(kernel)

.PHONY: all pre-build clean run
24 changes: 24 additions & 0 deletions arch/x86/arch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <vga.h>
#include <gdt.h>
#include <idt.h>
#include <helper.h>
#include <timer.h>
#include <keyboard.h>
#include <mem.h>
#include <vm.h>
#include <task.h>
#include <sync.h>

extern unsigned end;
void arch_init()
{
mem_init(&end, PHYS_RAM - (int) V2P(&end));
printk("Initialized memory allocator\n");
init_paging();
printk("Initialized kernel paging\n");
init_gdt();
init_idt();
printk("Initialized descriptors\n");
init_keyboard();
init_timer(100);
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions drivers/vga.c → arch/x86/drivers/vga.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,8 @@ void init_vga()
fb = (uint16_t *) (KERNBASE + 0xB8000);
cls();
}

void console_init()
{
init_vga();
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions kernel/vm.c → arch/x86/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,11 @@ void init_paging()
irq_install_handler(14, page_fault);
switch_pgdir(V2P(init_pd));
}

__attribute__((aligned(PGSIZE))) pd_t entrypgdir = {
/* Identiy mapping for first 4M memory */
.pdes[0] = (pde_t *) ((0) | PTE_P | PTE_W | PTE_PS),
/* Higher address mapping for 4M memory */
.pdes[KERNBASE >> PDXSHIFT] =
(pde_t *) ((0) | PTE_P | PTE_W | PTE_PS),
};
49 changes: 49 additions & 0 deletions build/rules-arm.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

# Compiler GCC
CC := arm-none-eabi-gcc
CFLAGS := -g -mcpu=cortex-m3 -mthumb -O2 -ffreestanding -Iarch/arm/ -I include/
LDFILE := arch/arm/ldscript/linker.ld
LDFLAGS := -T $(LDFILE) -nostdlib

# Build artifacts
objdir := bin
kernel := $(objdir)/kernel.elf

kernel_src_dir := \
arch/arm \
stdlib \

c_srcs := \
arch/arm/startup.c \
arch/arm/main.c \
stdlib/stdlib.c \
stdlib/printf.c \
stdlib/malloc.c \

c_objs := $(c_srcs:%.c=$(objdir)/%.o)

define make-repo
for dir in $(kernel_src_dir); \
do \
mkdir -p $(objdir)/$$dir; \
done
endef

all: pre-build $(kernel)

pre-build:
@mkdir -p $(objdir)
@$(call make-repo)

$(kernel): $(LDFILE) $(c_objs)
@echo " LD $@"
$(V)$(CC) $(CFLAGS) $(LDFLAGS) $(c_objs) -o $@

qemu: all
qemu-system-arm -M lm3s6965evb -kernel $(kernel) -serial stdio

qemu_gdb: all
qemu-system-arm -M lm3s6965evb -kernel $(kernel) -serial stdio -s -S

clean:
@rm -rf bin/
136 changes: 136 additions & 0 deletions build/rules-x86.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@

# Compiler GCC
CC := gcc
AS := nasm
OBJDUMP := objdump
OBJCOPY := objcopy
AR := ar

# Build artifacts
objdir := bin
os_image := $(objdir)/os.iso
kernel := $(objdir)/kernel.elf

kernel_src_dir := \
arch/x86/ \
arch/x86/drivers/ \
kernel/ \
stdlib/ \

boot_src_dir := arch/x86/boot

app_src_dir := app

app_obj_dir := $(objdir)/$(app_src_dir)

c_srcs := \
arch/x86/arch.c \
arch/x86/cpio_parser.c \
arch/x86/gdt.c \
arch/x86/idt.c \
arch/x86/isr.c \
arch/x86/keyboard.c \
arch/x86/mem.c \
arch/x86/sync.c \
arch/x86/syscall.c \
arch/x86/task.c \
arch/x86/timer.c \
arch/x86/vm.c \
arch/x86/drivers/vga.c \
kernel/main.c \
stdlib/stdlib.c \
stdlib/printk.c \
stdlib/wait_queue.c \

asm_srcs := arch/x86/boot/boot.s \
arch/x86/boot/helper.s \
arch/x86/boot/isr.s \
arch/x86/boot/swtch.s \
arch/x86/boot/sync.s \

c_objs := $(c_srcs:%.c=$(objdir)/%.o)
asm_objs := $(asm_srcs:%.s=$(objdir)/%.o)

app_lib_srcs := stdlib/crt.c \
stdlib/stdlib.c \
stdlib/printf.c \
stdlib/malloc.c \

app_asm_srcs := arch/x86/boot/syscall.s \

app_lib_objs := $(app_lib_srcs:%.c=$(objdir)/%.o)
app_asm_objs := $(app_asm_srcs:%.s=$(objdir)/%.o)

app_lib := $(objdir)/lib_helper.a

CFLAGS := -g -O2 -m32 -ffreestanding -Wall -Wextra -DVERSION=\"$(VERSION)\"
CFLAGS += -Iinclude/ \
-Iarch/x86/include \
-Iarch/x86/include/drivers \

LDFILE = arch/x86/ldscript/linker.ld
LDSCRIPT = -T $(LDFILE)
APP_LDSCRIPT = -T app/linker.ld
LDFLAGS = -nostdlib -Wl,--build-id=none
APP_CFLAGS := -g -O2 -m32 -static -fno-pic -Wall -Wextra -ffreestanding -I app/
APP_LDFLAGS := -nostdlib -Ttext 0x100000 -Wl,--build-id=none
ASFLAGS = -f elf

define make-repo
for dir in $(kernel_src_dir) $(boot_src_dir) $(app_src_dir); \
do \
mkdir -p $(objdir)/$$dir; \
done
endef

all: pre-build $(kernel) $(os_image)

ramfs.obj: $(app_lib) $(app_obj_dir)/init $(app_obj_dir)/shell $(app_obj_dir)/forktest $(app_obj_dir)/memtest
$(V)cd $(app_obj_dir) && find . | cpio -o -H newc > ../ramfs.cpio
$(V)cd $(objdir) && $(OBJCOPY) -I binary -O elf32-i386 -B i386 ramfs.cpio $@

$(app_lib): $(app_lib_objs) $(app_asm_objs)
$(V)$(AR) cru $@ $^

$(app_obj_dir)/forktest: $(app_src_dir)/forktest.c $(app_lib)
@echo " APP $<"
$(V)$(CC) $(APP_CFLAGS) $(APP_LDFLAGS) $^ -o $@

$(app_obj_dir)/memtest: $(app_src_dir)/memtest.c $(app_lib)
@echo " APP $<"
$(V)$(CC) $(APP_CFLAGS) $(APP_LDFLAGS) $^ -o $@

$(app_obj_dir)/init: $(app_src_dir)/init.c $(app_lib)
@echo " APP $<"
$(V)$(CC) $(APP_CFLAGS) $(APP_LDFLAGS) $^ -o $@

$(app_obj_dir)/shell: $(app_src_dir)/shell.c $(app_lib)
@echo " APP $<"
$(V)$(CC) $(APP_CFLAGS) $(APP_LDFLAGS) $^ -o $@

pre-build:
@mkdir -p $(objdir)
@$(call make-repo)

$(kernel): $(LDFILE) $(asm_objs) $(c_objs) ramfs.obj
@echo " LD $@"
$(V)$(CC) $(CFLAGS) $(LDFLAGS) $(LDSCRIPT) $(asm_objs) $(c_objs) $(objdir)/ramfs.obj -o $@
$(V)$(OBJDUMP) -t $@ | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $(objdir)/kernel.sym

$(os_image): $(kernel)
$(V)cp $< iso/boot/
$(V)genisoimage -R -b boot/grub/grub -no-emul-boot -boot-load-size 4 \
-A os -input-charset utf8 -quiet -boot-info-table -o $@ iso

run: all
bochs -qf tools/bochsrc.txt -rc tools/bochsrc.debug

qemu: all
qemu-system-i386 -cdrom bin/os.iso -m 32 -monitor stdio

qemu_gdb: all
qemu-system-i386 -cdrom bin/os.iso -m 32 -s -S -monitor stdio

clean:
@rm -rf bin/
@rm -f iso/boot/$(kernel)
6 changes: 6 additions & 0 deletions include/arch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __ARCH_H__
#define __ARCH_H__

void arch_init(void);

#endif /* __ARCH_H__ */
Loading

0 comments on commit bdc60a2

Please sign in to comment.