diff --git a/build.sh b/build.sh index 0273fab..148e9ad 100755 --- a/build.sh +++ b/build.sh @@ -24,3 +24,4 @@ mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/cat ::bin/cat mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/cp ::bin/cp mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/mkdir ::bin/mkdir mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/rm ::bin/rm +mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/wc ::bin/wc diff --git a/meson.build b/meson.build index 194d863..81688e0 100644 --- a/meson.build +++ b/meson.build @@ -427,3 +427,42 @@ custom_target( command: ['objcopy', '-O', 'binary', '@INPUT@', '@OUTPUT@'], build_by_default: true, ) + +wc_start = custom_target( + 'wc_start', + input: 'src/user/start.asm', + output: 'wc_start.o', + command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'], +) + +wc_syscall = custom_target( + 'wc_syscall', + input: 'src/user/syscall.asm', + output: 'wc_syscall.o', + command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'], +) + +wc_elf = executable( + 'wc.elf', + sources: [wc_start, wc_syscall, lib_sources, 'src/user/app/wc/wc.c'], + c_args: [ + '-ffreestanding', + '-nostdlib', + '-fno-stack-protector', + '-mcmodel=large', + '-DVERSION="' + meson.project_version() + '"', + ], + link_args: [ + '-T', meson.project_source_root() / 'src/user/linker.ld', + '-nostdlib', + ], + link_depends: 'src/user/linker.ld', +) + +custom_target( + 'wc', + input: wc_elf, + output: 'wc', + command: ['objcopy', '-O', 'binary', '@INPUT@', '@OUTPUT@'], + build_by_default: true, +) diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 465fc22..fcd48fb 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -79,10 +79,10 @@ void kernel_main() { fs_read(shell_node, 0, shell_node->size, shell_code); fs_close(shell_node); - process_t *terminal = process_create(kernel, terminal_code, terminal_node->size); + process_t *terminal = process_create(kernel, terminal_code, terminal_node->size, 0, 1); memory_free(terminal_code); - process_t *shell = process_create(kernel, shell_code, shell_node->size); + process_t *shell = process_create(kernel, shell_code, shell_node->size, 0, 1); memory_free(shell_code); uint64_t *terminal_stack = (uint64_t *)terminal->user_stack; diff --git a/src/kernel/path.c b/src/kernel/path.c index f426c14..8b3f38e 100644 --- a/src/kernel/path.c +++ b/src/kernel/path.c @@ -31,6 +31,9 @@ path_t *path_open(const path_t *base, const char *path, uint64_t flags) { } if (string_empty(path_components[i]) || string_equal(path_components[i], ".")) { + if (i == path_length - 1 && result->depth) { + // TODO Apply flags to current top of stack. + } continue; } else if (string_equal(path_components[i], "..")) { if (result->depth > 1) { diff --git a/src/kernel/process.c b/src/kernel/process.c index 930c5a9..f5cec1c 100644 --- a/src/kernel/process.c +++ b/src/kernel/process.c @@ -14,7 +14,7 @@ static uint64_t free_pid = 1; process_t *current_process; -process_t *process_create(const process_t *parent, const uint8_t *code, uint64_t size) { +process_t *process_create(const process_t *parent, const uint8_t *code, uint64_t size, uint64_t stdin, uint64_t stdout) { if (size >= USER_VIRTUAL_HEAP - USER_VIRTUAL_CODE) { return NUL; } @@ -29,6 +29,10 @@ process_t *process_create(const process_t *parent, const uint8_t *code, uint64_t return NUL; } + if (stdin >= MAX_FDS || !parent->fds[stdin] || stdout > MAX_FDS || !parent->fds[stdout]) { + return NUL; + } + process_t *proc = processes[free_pi] = memory_allocate(sizeof(process_t)); proc->parent = parent; @@ -71,8 +75,8 @@ process_t *process_create(const process_t *parent, const uint8_t *code, uint64_t proc->pid = free_pid++; proc->state = PROCESS_RUNNING; proc->cwd = path_open_again(parent->cwd); - proc->fds[0] = parent->fds[0]; - proc->fds[1] = parent->fds[1]; + proc->fds[0] = parent->fds[stdin]; + proc->fds[1] = parent->fds[stdout]; proc->free_fd = 2; proc->code = EXIT_CODE_OK; diff --git a/src/kernel/process.h b/src/kernel/process.h index 190f061..10e88db 100644 --- a/src/kernel/process.h +++ b/src/kernel/process.h @@ -36,7 +36,7 @@ extern process_t *current_process; extern void process_trampoline(); -process_t *process_create(const process_t *parent, const uint8_t *code, uint64_t size); +process_t *process_create(const process_t *parent, const uint8_t *code, uint64_t size, uint64_t stdin, uint64_t stdout); process_t *process_get(uint64_t pid); diff --git a/src/kernel/syscall.asm b/src/kernel/syscall.asm index 9b53e26..d571cab 100644 --- a/src/kernel/syscall.asm +++ b/src/kernel/syscall.asm @@ -28,6 +28,7 @@ syscall_entry: push r14 push r15 + mov r9, r8 ; arg5 mov r8, r10 ; arg4 mov rcx, rdx ; arg3 mov rdx, rsi ; arg2 diff --git a/src/kernel/syscall.c b/src/kernel/syscall.c index 35387dd..266fbe4 100644 --- a/src/kernel/syscall.c +++ b/src/kernel/syscall.c @@ -2,6 +2,7 @@ #include "src/kernel/fs.h" #include "src/kernel/gdt.h" #include "src/kernel/path.h" +#include "src/kernel/pipe.h" #include "src/kernel/process.h" #include "src/kernel/stream.h" #include "src/lib/layout.h" @@ -89,7 +90,7 @@ static uint64_t chdir(const char *path) { return current_process->cwd->depth; } -static uint64_t spawn(const char *path, uint64_t argc, const char **argv) { +static uint64_t spawn(const char *path, uint64_t argc, const char **argv, uint64_t stdin, uint64_t stdout) { fs_node_t *node = path_open_node(current_process->cwd, path, OPEN_FILE); if (!node) { return EXIT_CODE_NOT_FOUND; @@ -109,7 +110,7 @@ static uint64_t spawn(const char *path, uint64_t argc, const char **argv) { memory_copy(argv[i], sizes[i], blob + offsets[i]); } - process_t *child = process_create(current_process, bin, node->size); + process_t *child = process_create(current_process, bin, node->size, stdin, stdout); memory_free(bin); fs_close(node); @@ -192,6 +193,16 @@ static uint64_t remove(const char *path) { return 0; } +static uint64_t pipe(uint64_t *write_fd, uint64_t *read_fd) { + if (current_process->free_fd + 2 >= MAX_FDS) { + return (uint64_t)-1; + } + *write_fd = current_process->free_fd++; + *read_fd = current_process->free_fd++; + pipe_init(¤t_process->fds[*write_fd], ¤t_process->fds[*read_fd]); + return 0; +} + static void exit(exit_code_t code) { current_process->state = PROCESS_ZOMBIE; current_process->code = code; @@ -206,7 +217,7 @@ static void exit(exit_code_t code) { process_next(); } -uint64_t syscall_dispatch(uint64_t func, uint64_t arg1, uint64_t arg2, uint64_t arg3, __attribute__((unused)) uint64_t arg4) { +uint64_t syscall_dispatch(uint64_t func, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5) { switch (func) { case SYSCALL_READ: return read(arg1, arg2, (char *)arg3); @@ -217,7 +228,7 @@ uint64_t syscall_dispatch(uint64_t func, uint64_t arg1, uint64_t arg2, uint64_t case SYSCALL_CHDIR: return chdir((const char *)arg1); case SYSCALL_SPAWN: - return spawn((const char *)arg1, arg2, (const char **)arg3); + return spawn((const char *)arg1, arg2, (const char **)arg3, arg4, arg5); case SYSCALL_WAIT: return wait(arg1); case SYSCALL_OPEN: @@ -228,6 +239,8 @@ uint64_t syscall_dispatch(uint64_t func, uint64_t arg1, uint64_t arg2, uint64_t return truncate(arg1, arg2); case SYSCALL_REMOVE: return remove((char *)arg1); + case SYSCALL_PIPE: + return pipe((uint64_t *)arg1, (uint64_t *)arg2); case SYSCALL_EXIT: exit(arg1); return 0; diff --git a/src/kernel/syscall.h b/src/kernel/syscall.h index d702d5f..cb57ff5 100644 --- a/src/kernel/syscall.h +++ b/src/kernel/syscall.h @@ -4,4 +4,4 @@ void syscall_init(); -uint64_t syscall_dispatch(uint64_t func, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4); +uint64_t syscall_dispatch(uint64_t func, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5); diff --git a/src/lib/string.c b/src/lib/string.c index 79a4e93..a2d57de 100644 --- a/src/lib/string.c +++ b/src/lib/string.c @@ -33,6 +33,30 @@ uint8_t string_equal(const char *l, const char *r) { return *l == *r; } +uint64_t string_trim(char *string, char character, char **result) { + while (*string == character) { + string++; + } + *result = string; + + if (*string == '\0') { + return 0; + } + + char *end = string; + while (*end != '\0') { + end++; + } + + end--; + while (string < end && *end == character) { + *end = '\0'; + end--; + } + + return (uint64_t)(end - string + 1); +} + uint64_t string_split(char *string, char separator, uint64_t max, char **result) { uint64_t count = 1; *result = string; diff --git a/src/lib/string.h b/src/lib/string.h index 5c28377..ddd1de4 100644 --- a/src/lib/string.h +++ b/src/lib/string.h @@ -10,6 +10,8 @@ uint64_t string_length(const char *s); uint8_t string_equal(const char *l, const char *r); +uint64_t string_trim(char *string, char character, char **result); + uint64_t string_split(char *string, char separator, uint64_t max, char **result); uint64_t string_byte_to_hex(uint8_t value, uint64_t max, char *result); diff --git a/src/lib/syscall.h b/src/lib/syscall.h index 2bba0f0..b462e91 100644 --- a/src/lib/syscall.h +++ b/src/lib/syscall.h @@ -8,6 +8,7 @@ #define SYSCALL_CLOSE 7 #define SYSCALL_TRUNCATE 8 #define SYSCALL_REMOVE 9 +#define SYSCALL_PIPE 10 #define SYSCALL_EXIT 60 #define OPEN_CREATE 0b0001 diff --git a/src/user/app/cat/cat.c b/src/user/app/cat/cat.c index 7d9cfc5..407d848 100644 --- a/src/user/app/cat/cat.c +++ b/src/user/app/cat/cat.c @@ -7,33 +7,38 @@ #define PRINT_S(s) write(1, s, sizeof(s) - 1); #define PRINT_D(s) write(1, s, string_length(s)); -exit_code_t cat(const char *path) { - uint64_t fd = open(path, OPEN_FILE); - - if (fd == (uint64_t)-1) { - PRINT_S("cat: path does not exist\n"); - return EXIT_CODE_GENERAL_FAILURE; - } - +exit_code_t pass(uint64_t fd) { static char buffer[BLOCK_SIZE]; uint64_t bytes; while ((bytes = read(fd, BLOCK_SIZE, buffer))) { if (bytes == (uint64_t)-1) { PRINT_S("cat: could not read file\n"); - close(fd); return EXIT_CODE_GENERAL_FAILURE; } write(1, buffer, bytes); } + return EXIT_CODE_OK; +} + +exit_code_t cat(const char *path) { + uint64_t fd = open(path, OPEN_FILE); + + if (fd == (uint64_t)-1) { + PRINT_S("cat: path does not exist\n"); + return EXIT_CODE_GENERAL_FAILURE; + } + + exit_code_t code = pass(fd); + close(fd); - return 0; + return code; } exit_code_t main(uint64_t argc, const char **argv) { if (argc == 1) { - return cat("."); + return pass(0); } uint64_t code = EXIT_CODE_OK; diff --git a/src/user/app/shell/shell.c b/src/user/app/shell/shell.c index 37c4932..693cbd4 100644 --- a/src/user/app/shell/shell.c +++ b/src/user/app/shell/shell.c @@ -3,15 +3,15 @@ #include "src/lib/util.h" #include "src/user/syscall.h" -static void help(uint8_t argc, char **argv); -static void cd(uint8_t argc, char **argv); +static void help(uint8_t argc, char **argv, uint64_t stdin, uint64_t stdout); +static void cd(uint8_t argc, char **argv, uint64_t stdin, uint64_t stdout); #define PATH "/bin/" -#define PRINT_S(s) write(1, s, sizeof(s) - 1); -#define PRINT_D(s) write(1, s, string_length(s)); +#define PRINT_S(o, s) write(o, s, sizeof(s) - 1); +#define PRINT_D(o, s) write(o, s, string_length(s)); -typedef void (*app_t)(uint8_t argc, char **argv); +typedef void (*app_t)(uint8_t argc, char **argv, uint64_t stdin, uint64_t stdout); typedef struct { const char *name; @@ -23,27 +23,27 @@ static app_entry_t builtins[] = { {"cd", cd}, }; -static void help(uint8_t argc, __attribute__((unused)) char **argv) { +static void help(uint8_t argc, __attribute__((unused)) char **argv, __attribute__((unused)) uint64_t stdin, uint64_t stdout) { if (argc > 1) { - PRINT_S("help: expects no arguments"); + PRINT_S(stdout, "help: expects no arguments\n"); return; } - PRINT_S("Available commands:\n"); + PRINT_S(stdout, "Available commands:\n"); for (uint64_t i = 0; i < sizeof(builtins) / sizeof(app_entry_t); i++) { - PRINT_D(builtins[i].name); - PRINT_S("\n"); + PRINT_D(stdout, builtins[i].name); + PRINT_S(stdout, "\n"); } } -static void cd(uint8_t argc, char **argv) { +static void cd(uint8_t argc, char **argv, __attribute__((unused)) uint64_t stdin, uint64_t stdout) { if (argc != 2) { - PRINT_S("cd: requires a single path\n"); + PRINT_S(stdout, "cd: requires a single path\n"); return; } if (chdir(argv[1]) == (uint64_t)-1) { - PRINT_S("cd: path does not exist\n"); + PRINT_S(stdout, "cd: path does not exist\n"); } } @@ -53,16 +53,23 @@ static void print_prompt() { char *components[16]; uint8_t cl = string_split(path, '/', 16, components); - PRINT_S("[") - PRINT_D(pl == 1 ? "/" : components[cl - 1]); - PRINT_S("]$ "); + PRINT_S(1, "[") + PRINT_D(1, pl == 1 ? "/" : components[cl - 1]); + PRINT_S(1, "]$ "); } -static void execute(char *command) { - char *argv[16]; - uint8_t argc = (uint8_t)string_split(command, ' ', 16, argv); +static void free_fds(uint64_t *fds, uint8_t fds_count) { + for (uint8_t i = 0; i < fds_count; i++) { + close(fds[i]); + } +} - PRINT_S("\n"); +static void kill_pids(__attribute__((unused)) uint64_t *pids, __attribute__((unused)) uint8_t pid_count) { + // TODO. +} + +static void execute(char *command) { + PRINT_S(1, "\n"); if (string_equal(command, "^C")) { print_prompt(); @@ -71,34 +78,85 @@ static void execute(char *command) { if (string_equal(command, "^D")) { exit(EXIT_CODE_OK); + return; } - uint8_t i; - for (i = 0; i < sizeof(builtins) / sizeof(app_entry_t); i++) { - if (string_equal(argv[0], builtins[i].name)) { - builtins[i].app(argc, argv); - break; + char *subcommands[16]; + uint8_t subcommands_count = (uint8_t)string_split(command, '|', 16, subcommands); + + uint64_t fds[32]; + uint8_t fds_count = 0; + + uint64_t pids[16]; + uint8_t pids_count = 0; + + for (uint8_t i = 0; i < subcommands_count; i++) { + string_trim(subcommands[i], ' ', &subcommands[i]); + + char *argv[16]; + uint8_t argc = (uint8_t)string_split(subcommands[i], ' ', 16, argv); + + uint64_t stdin = 0; + uint64_t stdout = 1; + + if (i > 0) { + stdin = fds[fds_count - 1]; + } + if (i < subcommands_count - 1) { + if (pipe(&fds[fds_count], &fds[fds_count + 1]) == (uint64_t)-1) { + kill_pids(pids, pids_count); + free_fds(fds, fds_count); + print_prompt(); + return; + } + fds_count += 2; + stdout = fds[fds_count - 2]; + } + + uint8_t j; + for (j = 0; j < sizeof(builtins) / sizeof(app_entry_t); j++) { + if (string_equal(argv[0], builtins[j].name)) { + builtins[j].app(argc, argv, stdin, stdout); + pids[pids_count++] = 0; + break; + } + } + if (j == sizeof(builtins) / sizeof(app_entry_t)) { + uint64_t size = sizeof(PATH) + string_length(argv[0]) + 1; + char *path = memory_allocate(size); + memory_copy(PATH, sizeof(PATH), path); + memory_copy(argv[0], size - sizeof(PATH), path + sizeof(PATH) - 1); + uint64_t pid = spawn(path, argc, (const char **)argv, stdin, stdout); + memory_free(path); + + if (pid == (uint64_t)-1) { + kill_pids(pids, pids_count); + free_fds(fds, fds_count); + print_prompt(); + return; + } else { + pids[pids_count++] = pid; + } } } - if (i == sizeof(builtins) / sizeof(app_entry_t)) { - uint64_t size = sizeof(PATH) + string_length(argv[0]) + 1; - char *path = memory_allocate(size); - memory_copy(PATH, sizeof(PATH), path); - memory_copy(argv[0], size - sizeof(PATH), path + sizeof(PATH) - 1); - uint64_t pid = spawn(path, argc, (const char **)argv); - memory_free(path); - - if (pid == EXIT_CODE_NOT_FOUND) { - PRINT_S("shell: program not found\n"); - } else { - waitpid(pid); + + for (uint8_t i = 0; i < pids_count; i++) { + if (pids[i]) { + waitpid(pids[i]); + } + if (i > 0) { + close(fds[(i - 1) * 2 + 1]); + } + if (i < pids_count - 1) { + close(fds[i * 2]); } } + print_prompt(); } uint64_t main() { - PRINT_D("Welcome to FreywarOS v" VERSION "!\n\n"); + PRINT_D(1, "Welcome to FreywarOS v" VERSION "!\n\n"); print_prompt(); diff --git a/src/user/app/wc/wc.c b/src/user/app/wc/wc.c new file mode 100644 index 0000000..f437f81 --- /dev/null +++ b/src/user/app/wc/wc.c @@ -0,0 +1,59 @@ +#include "src/lib/string.h" +#include "src/lib/syscall.h" +#include "src/lib/util.h" +#include "src/user/syscall.h" + +#define BLOCK_SIZE 65536 + +#define PRINT_S(s) write(1, s, sizeof(s) - 1); +#define PRINT_D(s) write(1, s, string_length(s)); + +exit_code_t pass(uint64_t fd) { + static char buffer[BLOCK_SIZE]; + uint64_t bytes; + uint64_t total = 0; + while ((bytes = read(fd, BLOCK_SIZE, buffer))) { + if (bytes == (uint64_t)-1) { + PRINT_S("wc: could not read file\n"); + return EXIT_CODE_GENERAL_FAILURE; + } else { + total += bytes; + } + } + + uint64_t l = string_format("%d\n", BLOCK_SIZE, buffer, total); + write(1, buffer, l); + + return EXIT_CODE_OK; +} + +exit_code_t wc(const char *path) { + uint64_t fd = open(path, OPEN_FILE); + + if (fd == (uint64_t)-1) { + PRINT_S("wc: path does not exist\n"); + return EXIT_CODE_GENERAL_FAILURE; + } + + exit_code_t code = pass(fd); + + close(fd); + + return code; +} + +exit_code_t main(uint64_t argc, const char **argv) { + if (argc == 1) { + return pass(0); + } + + uint64_t code = EXIT_CODE_OK; + for (uint64_t i = 1; i < argc; i++) { + exit_code_t c = wc(argv[i]); + if (c != EXIT_CODE_OK) { + code = c; + } + } + + return code; +} diff --git a/src/user/syscall.asm b/src/user/syscall.asm index 407341e..af686ec 100644 --- a/src/user/syscall.asm +++ b/src/user/syscall.asm @@ -3,65 +3,83 @@ bits 64 global read read: mov rax, 0 + mov r10, rcx syscall ret global write write: mov rax, 1 + mov r10, rcx syscall ret global getcwd getcwd: mov rax, 2 + mov r10, rcx syscall ret global chdir chdir: mov rax, 3 + mov r10, rcx syscall ret global spawn spawn: mov rax, 4 + mov r10, rcx syscall ret global waitpid waitpid: mov rax, 5 + mov r10, rcx syscall ret global open open: mov rax, 6 + mov r10, rcx syscall ret global close close: mov rax, 7 + mov r10, rcx syscall ret global truncate truncate: mov rax, 8 + mov r10, rcx syscall ret global remove remove: mov rax, 9 + mov r10, rcx + syscall + ret + +global pipe +pipe: + mov rax, 10 + mov r10, rcx syscall ret global exit exit: mov rax, 60 + mov r10, rcx syscall ; never returns diff --git a/src/user/syscall.h b/src/user/syscall.h index 7b05f70..b96ea21 100644 --- a/src/user/syscall.h +++ b/src/user/syscall.h @@ -10,7 +10,7 @@ uint64_t getcwd(uint64_t max, void *to); uint64_t chdir(const char *path); -uint64_t spawn(const char *path, uint64_t argc, const char **argv); +uint64_t spawn(const char *path, uint64_t argc, const char **argv, uint64_t stdin, uint64_t stdout); exit_code_t waitpid(uint64_t pid); @@ -22,4 +22,6 @@ uint64_t truncate(uint64_t fd, uint64_t size); uint64_t remove(const char *path); +uint64_t pipe(uint64_t *write_fd, uint64_t *read_fd); + void exit(exit_code_t code);