From bcf6906e72f7b2d074e9ff90ae16eb897565c9f8 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Wed, 15 Nov 2023 14:33:34 -0500 Subject: [PATCH] usr: add first userspace program --- .gitignore | 2 +- Makefile | 10 ++++++++-- usr.bin/Makefile | 21 +++++++++++++++++++++ usr.bin/test1/Makefile | 38 ++++++++++++++++++++++++++++++++++++++ usr.bin/test1/start.S | 15 +++++++++++++++ usr.bin/test1/test.c | 5 +++++ 6 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 usr.bin/Makefile create mode 100644 usr.bin/test1/Makefile create mode 100644 usr.bin/test1/start.S create mode 100644 usr.bin/test1/test.c diff --git a/.gitignore b/.gitignore index 5401bec..006bdc0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ .cache *.o *.d -kernel.elf +*.elf compile_commands.json diff --git a/Makefile b/Makefile index 390ab5e..bfd0707 100644 --- a/Makefile +++ b/Makefile @@ -66,7 +66,7 @@ override HEADER_DEPS := $(CFILES:.c=.d) $(ASFILES:.S=.d) # Default target. .PHONY: all -all: $(KERNEL) +all: $(KERNEL) uprg # Link rules for the final kernel executable. $(KERNEL): $(OBJ) linker.ld @@ -86,10 +86,16 @@ $(KERNEL): $(OBJ) linker.ld @$(CC) $(CFLAGS) -c $< -o $@ @echo " CC $@" +.PHONY: uprg +uprg: + @$(MAKE) --no-print-directory -C usr.bin + # Remove object files and the final executable. .PHONY: clean clean: - rm -rf $(KERNEL) $(OBJ) $(HEADER_DEPS) + @rm -rf $(KERNEL) $(OBJ) $(HEADER_DEPS) + @echo " CLEAN kernel" + @make --no-print-directory -C usr.bin clean .PHONY: distclean distclean: clean diff --git a/usr.bin/Makefile b/usr.bin/Makefile new file mode 100644 index 0000000..6893b4e --- /dev/null +++ b/usr.bin/Makefile @@ -0,0 +1,21 @@ +# SPDX-License-Identifier: GPL-2.0-only + +# usr.bin/Makefile +# +# Compile all user programs. + +UPRG := test1 +UPRGCLEAN := $(addsuffix clean,$(UPRG)) +UPRGBUILD := $(addsuffix build,$(UPRG)) + + +.PHONY: all +all: $(UPRGBUILD) + +%build: % + @$(MAKE) --no-print-directory -C $< + +clean: $(UPRGCLEAN) +%clean: % + @$(MAKE) -C $< clean + @echo " CLEAN usr.bin/$<" diff --git a/usr.bin/test1/Makefile b/usr.bin/test1/Makefile new file mode 100644 index 0000000..9bea1a8 --- /dev/null +++ b/usr.bin/test1/Makefile @@ -0,0 +1,38 @@ +# SPDX-License-Identifier: GPL-2.0-only + +# usr.bin/test1/Makefile +# +# test1 is the first test program, to be removed later. +# +# This Makefile also serves as a template for other user programs. + +CC := x86_64-elf-gcc +LD := x86_64-elf-ld + +OUT := test1.elf +CSRC := test.c +SSRC := start.S +OBJ := $(CSRC:.c=.o) $(SSRC:.S=.o) +DEP := $(OBJ:.o=.d) + +CFLAGS := -Wall -ffreestanding -mno-mmx -mno-sse2 -mno-sse -m64 -MMD +LDFLAGS := -nostdlib -static -m elf_x86_64 + +CURDIR := $(shell pwd | awk -F '/' '{print $$(NF-1),"/",$$NF}' | sed 's/ //g') + +$(OUT): $(OBJ) + @$(LD) $^ $(LDFLAGS) -o $@ + @echo " USR.LD $(CURDIR)/$@" + +-include $(DEP) + +%.o: %.S + @$(CC) $(CFLAGS) -c $< -o $@ + @echo " USR.CC $(CURDIR)/$@" + +%.o: %.c + @$(CC) $(CFLAGS) -c $< -o $@ + @echo " USR.CC $(CURDIR)/$@" + +clean: + @rm -rf $(OUT) $(OBJ) $(DEP) diff --git a/usr.bin/test1/start.S b/usr.bin/test1/start.S new file mode 100644 index 0000000..2954544 --- /dev/null +++ b/usr.bin/test1/start.S @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +/* test1/start.S + * + * Test the basic functionality of the kernel loader + */ + + sys_exit = 60 + +.global _start +_start: + call main + movq %rax, %rdi + movq sys_exit, %rax + int $0x80 diff --git a/usr.bin/test1/test.c b/usr.bin/test1/test.c new file mode 100644 index 0000000..c50c873 --- /dev/null +++ b/usr.bin/test1/test.c @@ -0,0 +1,5 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +int main() +{ + return 0x123456; +}