diff --git a/init/init.c b/init/init.c index 2593356..5e19d17 100644 --- a/init/init.c +++ b/init/init.c @@ -7,6 +7,11 @@ int main(int argc, char **argv) (void) argc; (void) argv; + + FILE *fp = fopen("/dev/debug", "w"); + fprintf(fp, "Hello, filesystem!\n"); + + for(;;); return 0; } diff --git a/kernel/boot/kmain.c b/kernel/boot/kmain.c index 4b12a6b..d636122 100644 --- a/kernel/boot/kmain.c +++ b/kernel/boot/kmain.c @@ -30,24 +30,19 @@ int kmain(uint64_t multiboot_magic, void *multiboot_data) debug_info("Syscall enabled:%d\n", CPUID_FEATURE_SYSCALL); + + fs_mount(0, "/"); + fs_mount(&debug_file, "/dev/debug"); + + fs_write(&debug_file, "TESTING DEBUG FILE", 18, 0); + process_t *p1 = process_spawn(0); char *args[] = {"init", "Hello, arg", "5", 0}; char *env[] = {"OS=mittos64", 0}; + p1->fp[1].file = fs_namef("/dev/debug"); thread_t *th = exec_elf(p1, mboot_data.init, args, env); scheduler_insert(th); - procmm_print_map(p1->mmap); - fs_mount(0, "/"); - fs_mount(0, "/dev"); - fs_mount(0, "/home/user/mnt/photos"); - - - fs_namef("/"); - fs_namef("/usr/local/bin/python"); - fs_namef("/dev/tty0"); - fs_namef("/home/thomas"); - fs_namef("/home/user/mnt/photos/2016/june"); - asm("sti"); debug_info("BOOT COMPLETE\n"); schedule(); diff --git a/kernel/fs/debug_fs.c b/kernel/fs/debug_fs.c new file mode 100644 index 0000000..7572978 --- /dev/null +++ b/kernel/fs/debug_fs.c @@ -0,0 +1,20 @@ +#include +#include + +size_t debug_write(file_t *file, void *buffer, size_t nbyte, size_t offset) +{ + (void)file; + (void)offset; + debug_putsn(buffer, nbyte); + return nbyte; +} + +fs_driver_t debug_fs = { + .write = debug_write, +}; + +file_t debug_file = { + .refs = 1, + .type = FS_TTY, + .driver = &debug_fs, +}; diff --git a/kernel/include/process.h b/kernel/include/process.h index 2d5ed42..20aea52 100644 --- a/kernel/include/process.h +++ b/kernel/include/process.h @@ -7,6 +7,9 @@ typedef struct process_st process_t; #include #include #include +#include + +#define PROC_NUMFP 20 struct procmm_mmap_st; typedef struct process_st @@ -20,6 +23,11 @@ typedef struct process_st LIST(struct process_st, children); LIST(struct process_st, siblings); LIST(thread_t, threads); + struct { + file_t *file; + uint64_t flags; + uint64_t pos; + } fp[PROC_NUMFP]; } process_t; #define PROC_STATE_READY 1 diff --git a/kernel/include/syscall.h b/kernel/include/syscall.h index 8ef4f02..df0c2d1 100644 --- a/kernel/include/syscall.h +++ b/kernel/include/syscall.h @@ -40,8 +40,15 @@ typedef long (*syscall_handler_t)(long num, long, long, long, long, long, long); #define SYSCALL_REGISTER(name, num) syscall_handlers[num] = syscall_##name +SYSCALL_DECL(open); +SYSCALL_DECL(close); +SYSCALL_DECL(read); SYSCALL_DECL(write); +SYSCALL_DECL(isatty); +SYSCALL_DECL(seek); + SYSCALL_DECL(brk); + SYSCALL_DECL(fork); SYSCALL_DECL(exit); SYSCALL_DECL(wait); diff --git a/kernel/include/vfs.h b/kernel/include/vfs.h index 699e3ab..8f7e068 100644 --- a/kernel/include/vfs.h +++ b/kernel/include/vfs.h @@ -22,6 +22,7 @@ typedef struct dirent_st #define FS_FILE 0x1 #define FS_DIR 0x2 #define FS_PIPE 0x3 +#define FS_TTY 0x4 typedef struct fs_driver_st { @@ -56,3 +57,5 @@ void fs_mount(file_t *root, const char *path); void fs_umount(const char *path); file_t *fs_namef(const char *path); + +file_t debug_file; diff --git a/kernel/proc/process.c b/kernel/proc/process.c index 8c4239e..03e9cf1 100644 --- a/kernel/proc/process.c +++ b/kernel/proc/process.c @@ -24,6 +24,16 @@ process_t *process_spawn(process_t *parent) LIST_APPEND(parent->children, proc, siblings); proc->mmap = procmm_new_map(proc, parent->mmap); spin_unlock(&parent->lock); + for(int i = 0; i < PROC_NUMFP; i++) + { + if(parent->fp[i].file) + { + proc->fp[i].file = fs_get(parent->fp[i].file); + proc->fp[i].pos = parent->fp[i].pos; + proc->fp[i].flags = parent->fp[i].flags; + fs_open(proc->fp[i].file, proc->fp[i].flags); + } + } } else { proc->mmap = procmm_new_map(proc, 0); } @@ -76,6 +86,14 @@ void process_exit(process_t *proc, uint64_t status) c->parent = init_proc; LIST_APPEND(init_proc->children, c, siblings); } + for(int i = 0; i < PROC_NUMFP; i++) + { + if(proc->fp[i].file) + { + fs_close(proc->fp[i].file); + fs_put(proc->fp[i].file); + } + } proc->state = PROC_STATE_ZOMBIE; spin_unlock(&proc->lock); } diff --git a/kernel/syscall/sys_fs.c b/kernel/syscall/sys_fs.c index 00288b8..41a4fd0 100644 --- a/kernel/syscall/sys_fs.c +++ b/kernel/syscall/sys_fs.c @@ -4,17 +4,77 @@ #include #include #include +#include +#include + +SYSCALL_DEF(open) +{ + SYSCALL_INIT(char *, path, int, flags, int, mode); + process_t *p = get_current_process(); + for(int i = 0; i < PROC_NUMFP; i++) + { + if(!p->fp[i].file) + { + p->fp[i].file = fs_namef(path); + p->fp[i].flags = flags; + fs_open(p->fp[i].file, flags); + p->fp[i].pos = 0; + if(p->fp[i].file) + return i; + break; + } + } + return -1; +} + +SYSCALL_DEF(close) +{ + SYSCALL_INIT(int, fd); + process_t *p = get_current_process(); + int retval = fs_close(p->fp[fd].file); + fs_put(p->fp[fd].file); + p->fp[fd].file = 0; + return retval; +} + +SYSCALL_DEF(isatty) +{ + SYSCALL_INIT(int, fd); + process_t *p = get_current_process(); + if(p->fp[fd].file && ((p->fp[fd].file->type & FS_TTY) == FS_TTY)) + return 1; + return 0; +} + +SYSCALL_DEF(read) +{ + SYSCALL_INIT(int, fd, void *, buffer, size_t, nbyte); + process_t *p = get_current_process(); + int64_t bytes = fs_read(p->fp[fd].file, buffer, nbyte, p->fp[fd].pos); + p->fp[fd].pos += bytes; + return bytes; +} SYSCALL_DEF(write) { SYSCALL_INIT(int, fd, void *, buffer, size_t, nbyte); - - if(fd == 1) - { - debugn(buffer, nbyte); - return nbyte; - } - - return 0; + process_t *p = get_current_process(); + int64_t bytes = fs_write(p->fp[fd].file, buffer, nbyte, p->fp[fd].pos); + p->fp[fd].pos += bytes; + return bytes; +} + +SYSCALL_DEF(seek) +{ + SYSCALL_INIT(int, fd, long, offset, int, whence); + process_t *p = get_current_process(); + if(!p->fp[fd].file) + return -1; + if(whence == 0) + p->fp[fd].pos = offset; + else if(whence == 1) + p->fp[fd].pos += offset; + + return p->fp[fd].pos; } diff --git a/kernel/syscall/sys_proc.c b/kernel/syscall/sys_proc.c index 7499d67..aba572e 100644 --- a/kernel/syscall/sys_proc.c +++ b/kernel/syscall/sys_proc.c @@ -6,6 +6,7 @@ #include #include #include +#include SYSCALL_DEF(fork) { diff --git a/kernel/syscall/syscall.c b/kernel/syscall/syscall.c index b9b03ed..6058023 100644 --- a/kernel/syscall/syscall.c +++ b/kernel/syscall/syscall.c @@ -56,8 +56,15 @@ void syscall_init() memset(syscall_handlers, 0, 1024*sizeof(syscall_handler_t)); SYSCALL_REGISTER(debug, SYS_DEBUG); + SYSCALL_REGISTER(open, SYS_OPEN); + SYSCALL_REGISTER(close, SYS_CLOSE); + SYSCALL_REGISTER(read, SYS_READ); SYSCALL_REGISTER(write, SYS_WRITE); + SYSCALL_REGISTER(isatty, SYS_ISATTY); + SYSCALL_REGISTER(seek, SYS_SEEK); + SYSCALL_REGISTER(brk, SYS_BRK); + SYSCALL_REGISTER(fork, SYS_FORK); SYSCALL_REGISTER(exit, SYS_EXIT); SYSCALL_REGISTER(wait, SYS_WAIT); diff --git a/libc/file_io.c b/libc/file_io.c index 138598c..ff3aa57 100644 --- a/libc/file_io.c +++ b/libc/file_io.c @@ -4,18 +4,48 @@ #include #include -SYSCALL_DEF(ioctl) -{ - SYSCALL_INIT(int, fd, unsigned long, request); - if(fd == 1 && request == TIOCGWINSZ) - { - return 0; - } - kernel_debug("==> IOCTL - unsupported request:%lx\n", request); - return -1; +SYSCALL_DEF(open) +{ + SYSCALL_INIT(char *, path, int, flags, int, mode); + return kernel_syscall(SYS_OPEN, path, flags, mode); +} +SYSCALL_DEF(close) +{ + SYSCALL_INIT(int, fd); + return kernel_syscall(SYS_CLOSE, fd); +} +SYSCALL_DEF(read) +{ + SYSCALL_INIT(int, fd, void *, buffer, size_t, nbyte); + + return kernel_syscall(SYS_READ, fd, buffer, nbyte); +} +SYSCALL_DEF(write) +{ + SYSCALL_INIT(int, fd, void *, buffer, size_t, nbyte); + + return kernel_syscall(SYS_WRITE, fd, buffer, nbyte); +} +SYSCALL_DEF(lseek) +{ + SYSCALL_INIT(int, fd, long, offset, int, whence); + return kernel_syscall(SYS_SEEK, fd, offset, whence); } +SYSCALL_DEF(readv) +{ + SYSCALL_INIT(int, fd, const struct iovec *, iov, int, iovcnt); + + size_t len = 0; + + for(int i=0; i < iovcnt; i++) + { + len += read(fd, iov[i].iov_base, iov[i].iov_len); + } + + return (long)len; +} SYSCALL_DEF(writev) { SYSCALL_INIT(int, fd, const struct iovec *, iov, int, iovcnt); @@ -29,10 +59,13 @@ SYSCALL_DEF(writev) return (long)len; } - -SYSCALL_DEF(write) +SYSCALL_DEF(ioctl) { - SYSCALL_INIT(int, fd, void *, buffer, size_t, nbyte); + SYSCALL_INIT(int, fd, unsigned long, request); - return kernel_syscall(SYS_WRITE, fd, buffer, nbyte); + if(request == TIOCGWINSZ) + return !kernel_syscall(SYS_ISATTY, fd); + + kernel_debug("==> IOCTL - unsupported request:%lx\n", request); + return -1; } diff --git a/libc/syscall_num.h b/libc/syscall_num.h index 0ab8f51..c07bfe6 100644 --- a/libc/syscall_num.h +++ b/libc/syscall_num.h @@ -1,8 +1,16 @@ #pragma once -#define SYS_WRITE 0x001 -#define SYS_BRK 0x002 -#define SYS_FORK 0x003 -#define SYS_EXIT 0x004 -#define SYS_WAIT 0x005 -#define SYS_DEBUG 0x3FF +#define SYS_DEBUG 0x3FF + +#define SYS_OPEN 0x001 +#define SYS_CLOSE 0x002 +#define SYS_READ 0x003 +#define SYS_WRITE 0x004 +#define SYS_ISATTY 0x005 +#define SYS_SEEK 0x006 + +#define SYS_BRK 0x007 + +#define SYS_FORK 0x008 +#define SYS_EXIT 0x009 +#define SYS_WAIT 0x00A