usr: add first userspace program

This commit is contained in:
Michael Shaw 2023-11-15 14:33:34 -05:00
commit bcf6906e72
6 changed files with 88 additions and 3 deletions

21
usr.bin/Makefile Normal file
View file

@ -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/$<"

38
usr.bin/test1/Makefile Normal file
View file

@ -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)

15
usr.bin/test1/start.S Normal file
View file

@ -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

5
usr.bin/test1/test.c Normal file
View file

@ -0,0 +1,5 @@
/* SPDX-License-Identifier: GPL-2.0-only */
int main()
{
return 0x123456;
}