diff --git a/.gitignore b/.gitignore index 006bdc0..3c30d89 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ *.d *.elf compile_commands.json +bin diff --git a/Makefile b/Makefile index bfd0707..a42b01e 100644 --- a/Makefile +++ b/Makefile @@ -93,7 +93,7 @@ uprg: # Remove object files and the final executable. .PHONY: clean clean: - @rm -rf $(KERNEL) $(OBJ) $(HEADER_DEPS) + @rm -rf $(KERNEL) $(OBJ) $(HEADER_DEPS) bin @echo " CLEAN kernel" @make --no-print-directory -C usr.bin clean diff --git a/include/kernel/syscall.h b/include/kernel/syscall.h index ad05008..cd689c7 100644 --- a/include/kernel/syscall.h +++ b/include/kernel/syscall.h @@ -4,6 +4,7 @@ #define SYS_READ 0 #define SYS_WRITE 1 +#define SYS_OPEN 2 #define SYS_EXIT 60 #endif /* _SYSCALL_H_ */ diff --git a/sched/entry.S b/sched/entry.S index abb658c..a5535be 100644 --- a/sched/entry.S +++ b/sched/entry.S @@ -204,6 +204,10 @@ gate_syscall: movq 16(%rax), %r8 movq 56(%rax), %r9 call syscall + pushq %rax + call proc_current_regs + popq %rdi + movq %rdi, 0(%rax) call schedule /* void return_from_irq(struct procregs *regs, paddr_t cr3); */ diff --git a/sched/syscall.c b/sched/syscall.c index 2c1e809..684f1f4 100644 --- a/sched/syscall.c +++ b/sched/syscall.c @@ -7,18 +7,15 @@ #include -static struct rbtree *syscall_tree; +#define SYSCALL_MAX 0x100 typedef void (*syscall_t)(uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5); +syscall_t syscall_table[SYSCALL_MAX]; + void syscall(uint64_t syscall_no, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5) { - struct rbnode *node = rbt_search(syscall_tree, syscall_no); - if (node == NULL) { - return; - } - - syscall_t syscall = (void *)node->value; + syscall_t syscall = (void *)syscall_table[syscall_no]; syscall(arg1, arg2, arg3, arg4, arg5); } @@ -38,8 +35,18 @@ int sys_read(int fd, void *buf, size_t count) return -EBADF; struct file_descriptor *fdesc = (void *)fdesc_node->value; + void *tmp_buf = kzalloc(count, ALLOC_KERN); + if (tmp_buf == NULL) + return -ENOMEM; - return read(fdesc, buf, count); + int ret = read(fdesc, tmp_buf, count); + if (ret > 0) + memcpy(buf, tmp_buf, count); + else + ret = -1; + + kfree(tmp_buf); + return ret; } int sys_write(int fd, void *buf, size_t count) @@ -58,7 +65,38 @@ int sys_write(int fd, void *buf, size_t count) struct file_descriptor *fdesc = (void *)fdesc_node->value; - return write(fdesc, buf, count); + void *tmp_buf = kzalloc(count, ALLOC_KERN); + if (tmp_buf == NULL) + return -ENOMEM; + + memcpy(tmp_buf, buf, count); + int ret = write(fdesc, tmp_buf, count); + + if (ret < 0) + ret = -1; + kfree(tmp_buf); + return ret; +} + +int sys_open(const char *pathname, int flags) +{ + if ((uintptr_t)pathname >= hhdm_start) + return -EFAULT; + + struct proc *proc = proc_find(getpid()); + if (proc == NULL) + return -1; + + struct file_descriptor *fdesc = open(pathname, flags); + if (fdesc == NULL) + return -1; + + uint64_t fdno = rbt_next_key(&proc->fd_map); + + struct rbnode *node = rbt_insert(&proc->fd_map, fdno); + node->value = (uintptr_t)fdesc; + + return fdno; } void sys_exit(int status) @@ -79,13 +117,14 @@ void sys_exit(int status) static void syscall_insert(uint64_t syscall_no, syscall_t syscall) { - struct rbnode *node = rbt_insert(syscall_tree, syscall_no); - node->value = (uintptr_t)syscall; + syscall_table[syscall_no] = syscall; } void syscall_init() { - syscall_tree = kzalloc(sizeof(struct rbtree), ALLOC_KERN); + memset(syscall_table, 0, sizeof(syscall_table)); + syscall_insert(SYS_READ, (syscall_t)sys_read); syscall_insert(SYS_WRITE, (syscall_t)sys_write); + syscall_insert(SYS_OPEN, (syscall_t)sys_open); syscall_insert(SYS_EXIT, (syscall_t)sys_exit); } diff --git a/usr.bin/Makefile b/usr.bin/Makefile index 6893b4e..722f9de 100644 --- a/usr.bin/Makefile +++ b/usr.bin/Makefile @@ -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 diff --git a/usr.bin/ftest/Makefile b/usr.bin/ftest/Makefile new file mode 100644 index 0000000..ea4cbc9 --- /dev/null +++ b/usr.bin/ftest/Makefile @@ -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)" diff --git a/usr.bin/ftest/ftest.S b/usr.bin/ftest/ftest.S new file mode 100644 index 0000000..4a7bef4 --- /dev/null +++ b/usr.bin/ftest/ftest.S @@ -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 diff --git a/usr.bin/test1/Makefile b/usr.bin/test1/Makefile index 724bfb2..99e376f 100644 --- a/usr.bin/test1/Makefile +++ b/usr.bin/test1/Makefile @@ -36,3 +36,7 @@ $(OUT): $(OBJ) clean: @rm -rf $(OUT) $(OBJ) $(DEP) + +install: $(OUT) + @cp $(OUT) ../../bin + @echo " INSTALL $(CURDIR)/$(OUT)"