Skip to content

Commit

Permalink
Execute an 'init' program (elf) loaded as a grub module
Browse files Browse the repository at this point in the history
  • Loading branch information
willdurand committed Dec 17, 2019
1 parent 14652fe commit 4bf033c
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,4 +1,5 @@
/build/
/init/init
*.o
*.a
*.bin
Expand Down
9 changes: 8 additions & 1 deletion Makefile
Expand Up @@ -67,9 +67,10 @@ iso: ## build the image of the OS (.iso)
iso: $(ISO)
.PHONY: iso

$(ISO): $(KERNEL)
$(ISO): $(KERNEL) init
mkdir -p $(GRUB_DIR)
cp -R grub/* $(GRUB_DIR)
cp init/init $(KERNEL_DIR)
grub-mkrescue -o $@ $(ISO_DIR)

run: ## run the OS
Expand Down Expand Up @@ -100,6 +101,12 @@ gdb: $(ISO)
qemu-system-x86_64 -s -S -cdrom $< -serial file:/tmp/serial.log
.PHONY: gdb

# TODO: there must be a better way to compile different modules from a
# top-level makefile.
init: $(LIBC)
cd init && make clean && make
.PHONY: init

help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
.PHONY: help
1 change: 1 addition & 0 deletions grub/grub.cfg
Expand Up @@ -3,5 +3,6 @@ set default=0

menuentry "willOS" {
multiboot2 /boot/kernel.bin
module2 /boot/init
boot
}
26 changes: 26 additions & 0 deletions init/Makefile
@@ -0,0 +1,26 @@
CC ?= gcc
LD ?= ld
AR ?= ar
NASM ?= nasm

ifeq ($(shell uname -s),Darwin)
CC = x86_64-pc-elf-gcc
LD = x86_64-pc-elf-ld
AR = x86_64-pc-elf-ar
endif

TARGET = init
ROOT_DIR = ..
BUILD_DIR = .
CFLAGS = -Wl,-emain -Wall -pedantic -std=c11 -O2 -ffreestanding -nostdlib -I $(ROOT_DIR)/src/include
LIBS = $(ROOT_DIR)/build/libc-willOS.a

%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@

$(TARGET): $(TARGET).o
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)

clean:
rm -rf $(TARGET) *.o
.PHONY: clean
7 changes: 7 additions & 0 deletions init/init.c
@@ -0,0 +1,7 @@
#include <sys/syscall.h>

int main() {
test("I'm the init program");

return 0;
}
4 changes: 3 additions & 1 deletion src/core/syscall.c
Expand Up @@ -32,7 +32,9 @@ void syscall_handler(registers_t* registers) {
}

void syscall_test(registers_t* registers) {
printf(" (syscall_test) hello, %s!\n", registers->rbx);
// TODO: remove offset (0x100000) when we have a separate memory area for a
// process. See the `elf.c` file for more information.
printf(" (syscall_test) hello, %s!\n", (registers->rbx + 0x100000));
}

void syscall_print_registers(registers_t* registers) {
Expand Down
17 changes: 17 additions & 0 deletions src/kernel/kmain.c
@@ -1,4 +1,5 @@
#include "kmain.h"
#include <core/elf.h>
#include <core/debug.h>
#include <core/isr.h>
#include <core/syscall.h>
Expand Down Expand Up @@ -93,6 +94,22 @@ void kmain(uint64_t addr) {
keyboard_init();
print_ok();

multiboot_tag_module_t* module = find_multiboot_tag(mbi, MULTIBOOT_TAG_TYPE_MODULE);

print_step("loading grub module (elf)");
elf_header_t* elf = elf_load((uint64_t*)module->mod_start);

if (elf) {
DEBUG("loaded elf entry=%p", elf->entry);
print_ok();

printf("\n");

typedef int callable(void);
callable* c = (callable*)(elf->entry);
int res = c();
}

// kshell
printf("\n");
kshell_print_prompt();
Expand Down

0 comments on commit 4bf033c

Please sign in to comment.