diff --git a/usr.bin/make.sh b/usr.bin/make.sh index 9fff20a..5539f53 100755 --- a/usr.bin/make.sh +++ b/usr.bin/make.sh @@ -1,4 +1,4 @@ -usrprog=('ftest' 'test1' 'vfs') +usrprog=('ftest' 'test1' 'vfs' 'shtest') for prog in ${usrprog[@]}; do make -C $prog $1 --no-print-directory diff --git a/usr.bin/shtest/Makefile b/usr.bin/shtest/Makefile new file mode 100644 index 0000000..3476a09 --- /dev/null +++ b/usr.bin/shtest/Makefile @@ -0,0 +1,38 @@ +# SPDX-License-Identifier: GPL-2.0-only + +# shtest: simple userspace shell test + +CC := x86_64-elf-gcc +LD := x86_64-elf-ld + +OUT := shtest.elf +CSRC := shtest.c +SSRC := syscall.S +OBJ := $(CSRC:.c=.o) $(SSRC:.S=.o) +DEP := $(OBJ:.o=.d) + +CFLAGS := -Wall -ffreestanding -mno-mmx -mno-sse2 -mno-sse -m64 -MMD -g +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) + +install: $(OUT) + @cp $(OUT) ../../bin + @echo " INSTALL $(CURDIR)/$(OUT)" diff --git a/usr.bin/shtest/shtest.c b/usr.bin/shtest/shtest.c new file mode 100644 index 0000000..058e99f --- /dev/null +++ b/usr.bin/shtest/shtest.c @@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* shtest: simple userland shell test */ +#include +#include + +typedef int64_t ssize_t; + +int open(const char *pathname, int flags); +ssize_t read(int fd, void *buf, int count); +ssize_t write(int fd, void *buf, int count); +int close(int fd); +int exit(int status); + +void promptfd(int fd) +{ + write(fd, "shtest> ", 8); +} + +void _start() +{ + int fd = open("/dev/tty", 0); + if (fd < 0) + exit(1); + + promptfd(fd); + + char c; + while (read(fd, &c, 1) == 1) { + write(fd, &c, 1); + + if (c == '\n') { + promptfd(fd); + } + } + + close(fd); + exit(0); +} diff --git a/usr.bin/shtest/syscall.S b/usr.bin/shtest/syscall.S new file mode 100644 index 0000000..78231f7 --- /dev/null +++ b/usr.bin/shtest/syscall.S @@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* syscall handler for simple userland shell test */ + +#define __ASM__ + +.macro _sc_define name, num +.global \name +\name: + movq $\num, %rax + movq %rcx, %r10 + int $0x80 + ret +.endm + +_sc_define read, 0 +_sc_define write, 1 +_sc_define open, 2 +_sc_define close, 3 +_sc_define exit, 60