Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
.PHONY: all kernel clean format
.PHONY: all kernel clean format user initramfs

all: kernel
all: kernel user initramfs

kernel:
@echo "Building Kernel..."
$(MAKE) -C kernel

user: user_bin

user_bin:
@echo "Building user binaries..."
$(MAKE) -C user bin

initramfs: user
( cd build/initramfs && find . -print | cpio -o -H newc -v ) > build/initramfs.cpio

format:
@echo "Formatting Kernel..."
$(MAKE) -C kernel format
@echo "Formatting User..."
$(MAKE) -C user format

clean:
@echo "Cleaning Kernel..."
$(MAKE) -C kernel clean
@echo "Cleaning User..."
$(MAKE) -C user clean
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ mkdir -p iso_root/boot/limine
# Copy the relevant files over.
cp -v limine.conf limine/limine-bios.sys limine/limine-bios-cd.bin limine/limine-uefi-cd.bin iso_root/boot/limine/
cp -v build/kernel/entry.elf iso_root/boot/
cp -v build/initramfs.cpio iso_root/boot/

# Create the EFI boot tree and copy Limine's EFI executables over.
mkdir -p iso_root/EFI/BOOT
Expand Down
18 changes: 15 additions & 3 deletions kernel/src/kernel/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern volatile struct limine_framebuffer_request framebuffer_request;
extern volatile struct limine_bootloader_info_request bootinfo_req;
extern volatile struct limine_boot_time_request boottime_req;
extern volatile struct limine_memmap_request memmap_req;
extern volatile struct limine_module_request mod_req;
extern volatile struct limine_hhdm_request hhdm_req;

uint64_t hhdm_base = 0;
Expand Down Expand Up @@ -133,14 +134,25 @@ void _start (void) {
__asm__ volatile ("mov %%cr3, %0" : "=r"(cr3));
cr3 = cr3 & 0xFFFFFFFFFF000;

printf ("CR3: %lx", cr3);
printf ("CR3: %lx\n", cr3);

if (mod_req.response == NULL || mod_req.response->module_count < 1) {
printf ("Error: no modules loaded.\n");
hcf ();
}

struct limine_file* initramfs = mod_req.response->modules[0];
void* initramfs_addr = initramfs->address;
uint64_t initramfs_size = initramfs->size;

printf ("\nInitramfs at 0x%llx, size %ld bytes\n", initramfs_addr, initramfs_size);

printf ("\nJumping to user land!\n");

/*
* TODO: bunch of stuff
* - [ ] set up user mode code to actually compile
* - [ ] set up limine loading it as a module
* - [x] set up user mode code to actually compile
* - [x] set up limine loading it as a module
* - [ ] set up a vfs
* - [ ] set up elf-loading
* - [ ] set up cpio reading and ramfs driver
Expand Down
4 changes: 4 additions & 0 deletions kernel/src/kernel/limine_requests.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ __attribute__ ((used,
section (".limine_requests"))) volatile struct limine_hhdm_request hhdm_req = {
.id = LIMINE_HHDM_REQUEST, .revision = 0};

__attribute__ ((used,
section (".limine_requests"))) volatile struct limine_module_request mod_req = {
.id = LIMINE_MODULE_REQUEST, .revision = 0};

__attribute__ ((used,
section (".limine_requests_start"))) static volatile LIMINE_REQUESTS_START_MARKER;

Expand Down
1 change: 1 addition & 0 deletions limine.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ timeout: 2

protocol: limine
kernel_path: boot():/boot/entry.elf
module_path: boot():/boot/initramfs.cpio
17 changes: 17 additions & 0 deletions user/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.PHONY: all hello

all: hello

bin: hello

hello:
@echo "Building hello..."
$(MAKE) -C hello

format:
@echo "Formatting hello..."
$(MAKE) -C hello format

clean:
@echo "Cleaning hello..."
$(MAKE) -C hello clean
100 changes: 100 additions & 0 deletions user/hello/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# This makefile is quite similar to the one used by the kernel, with
# the exception of some kernel-only flags that have been omitted here

override MAKEFLAGS += -rR
override TARGET := hello

define DEFAULT_VAR =
ifeq ($(origin $1),default)
override $(1) := $(2)
endif
ifeq ($(origin $1),undefined)
override $(1) := $(2)
endif
endef

override DEFAULT_CC := x86_64-elf-gcc
$(eval $(call DEFAULT_VAR,CC,$(DEFAULT_CC)))

override DEFAULT_LD := x86_64-elf-ld
$(eval $(call DEFAULT_VAR,LD,$(DEFAULT_LD)))

override DEFAULT_CFLAGS := -g -O2 -pipe
$(eval $(call DEFAULT_VAR,CFLAGS,$(DEFAULT_CFLAGS)))

# User controllable C preprocessor flags. We set none by default.
override DEFAULT_CPPFLAGS :=
$(eval $(call DEFAULT_VAR,CPPFLAGS,$(DEFAULT_CPPFLAGS)))

# User controllable linker flags. We set none by default.
override DEFAULT_LDFLAGS :=
$(eval $(call DEFAULT_VAR,LDFLAGS,$(DEFAULT_LDFLAGS)))

# Internal C flags that should not be changed by the user.
override CFLAGS += \
-Wall \
-Wextra \
-std=gnu11 \
-ffreestanding \
-fno-stack-protector \
-fno-stack-check \
-fno-lto \
-fno-PIE \
-fno-PIC \
-m64 \
-march=x86-64 \
-mabi=sysv

# Internal C preprocessor flags that should not be changed by the user.
override CPPFLAGS := \
-I include \
-I. \
$(CPPFLAGS) \
-MMD \
-MP

# Internal linker flags that should not be changed by the user.
override LDFLAGS += \
-nostdlib \
-static \
-m elf_x86_64 \
-z max-page-size=0x1000

ifeq ($(shell $(LD) --help 2>&1 | grep 'no-pie' >/dev/null 2>&1; echo $$?),0)
override LDFLAGS += -no-pie
endif

override CFILES := $(shell find -L . -type f -name '*.c')
override ASFILES := $(shell find -L . -type f -name '*.s')

override BUILD_DIR := ../../build/user/bin
override INITRAMFS_DIR := ../../build/initramfs/bin

override OBJ := $(patsubst %.c,$(BUILD_DIR)/%.c.o,$(CFILES)) $(patsubst %.s,$(BUILD_DIR)/%.s.o,$(ASFILES))
override HEADER_DEPS := $(patsubst %.c,$(BUILD_DIR)/%.c.d,$(CFILES)) $(patsubst %.s,$(BUILD_DIR)/%.s.d,$(ASFILES))

.PHONY: all
all: $(BUILD_DIR)/$(TARGET)

$(BUILD_DIR)/$(TARGET): $(OBJ)
$(LD) $(OBJ) $(LDFLAGS) -o $@
@mkdir -p $(INITRAMFS_DIR)
@cp $(BUILD_DIR)/$(TARGET) $(INITRAMFS_DIR)

-include $(HEADER_DEPS)

$(BUILD_DIR)/%.c.o: %.c
@mkdir -p $(@D)
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@

$(BUILD_DIR)/%.s.o: %.s
@mkdir -p $(@D)
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@

.PHONY: clean
clean:
rm -rf $(BUILD_DIR)

.PHONY: format
format:
find -type f \( -name '*.c' -o -name '*.h' \) ! -name 'limine.h' -exec clang-format -i {} +
20 changes: 20 additions & 0 deletions user/hello/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

static inline long syscall3 (long num, long arg1, long arg2, long arg3) {
long ret;
__asm__ volatile ("int $0x80"
: "=a"(ret)
: "a"(num), "b"(arg1), "c"(arg2), "d"(arg3)
: "memory");
return ret;
}

void _start (void) {
const char* msg = "Hello world!\n";

syscall3 (4, 1, (long)msg, 13);
syscall3 (1, 0, 0, 0);

while (1) {
__asm__ volatile ("pause");
}
}
Loading