syscall: imeplement basic file IO syscalls

also fix a bug where the RAX register would be restored to its initial
state instead of carrying the syscall return value
This commit is contained in:
Michael Shaw 2023-11-27 02:47:57 -05:00
commit 0297f4a34c
9 changed files with 209 additions and 18 deletions

View file

@ -4,13 +4,12 @@
#
# Compile all user programs.
UPRG := test1
UPRG := test1 ftest
UPRGCLEAN := $(addsuffix clean,$(UPRG))
UPRGBUILD := $(addsuffix build,$(UPRG))
UPRGINSTALL := $(addsuffix install,$(UPRG))
.PHONY: all
all: $(UPRGBUILD)
all: $(UPRGBUILD) $(UPRGINSTALL)
%build: %
@$(MAKE) --no-print-directory -C $<
@ -19,3 +18,7 @@ clean: $(UPRGCLEAN)
%clean: %
@$(MAKE) -C $< clean
@echo " CLEAN usr.bin/$<"
%install: %
@mkdir -p ../bin
@$(MAKE) --no-print-directory -C $< install

36
usr.bin/ftest/Makefile Normal file
View file

@ -0,0 +1,36 @@
# SPDX-License-Identifier: GPL-2.0-only
CC := x86_64-elf-gcc
LD := x86_64-elf-ld
OUT := ftest.elf
CSRC :=
SSRC := ftest.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)"

103
usr.bin/ftest/ftest.S Normal file
View file

@ -0,0 +1,103 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/* ftest/ftest.S
*
* Test the read and write system calls.
*/
sys_read = 0
sys_write = 1
sys_open = 2
sys_exit = 60
.macro print text text_size
movl $sys_write, %eax
movl $1, %edi
movq $\text, %rsi
movl $\text_size, %edx
int $0x80
.endm
.text
.global _start
_start:
/* Open the file */
movl $sys_open, %eax
movq $filename, %rdi
int $0x80
cmpl $-1, %eax
je _error_open
/* Read the file */
movq %rax, %rdi
movl $sys_read, %eax
movq $buf, %rsi
movl $buf_size, %edx
int $0x80
cmpl $-1, %eax
je _error_read
/* Write the file */
movl $sys_write, %eax
movl $1, %edi
movq $buf, %rsi
movl $buf_size, %edx
int $0x80
cmpl $-1, %eax
je _error_write
print exit_message exit_message_size
movl $0, %edi
jmp _exit
_error_open:
pushq %rax
print error_open error_open_size
popq %rdi
jmp _exit
_error_read:
pushq %rax
print error_read error_read_size
popq %rdi
jmp _exit
_error_write:
pushq %rax
print error_write error_write_size
popq %rdi
jmp _exit
_exit:
/* Exit */
movl $sys_exit, %eax
int $0x80
.data
filename:
.asciz "test.txt"
error_open:
.asciz "Error opening file\n"
error_open_size = . - error_open
error_read:
.asciz "Error reading file\n"
error_read_size = . - error_read
error_write:
.asciz "Error writing file\n"
error_write_size = . - error_write
exit_message:
.asciz "Exiting successfully\n"
exit_message_size = . - exit_message
.bss
buf:
.skip 1024
buf_size = . - buf

View file

@ -36,3 +36,7 @@ $(OUT): $(OBJ)
clean:
@rm -rf $(OUT) $(OBJ) $(DEP)
install: $(OUT)
@cp $(OUT) ../../bin
@echo " INSTALL $(CURDIR)/$(OUT)"