Add file descriptors and basic `cat`

master
Freywar Ulvnaudgari 1 month ago
parent 968bb02aa8
commit bcf2e5bac6
  1. 1
      build.sh
  2. 39
      meson.build
  3. 12
      src/kernel/path.c
  4. 2
      src/kernel/path.h
  5. 1
      src/kernel/process.c
  6. 9
      src/kernel/process.h
  7. 61
      src/kernel/syscall.c
  8. 2
      src/kernel/syscall.h
  9. 45
      src/user/app/cat/cat.c
  10. 16
      src/user/app/ls/ls.c
  11. 32
      src/user/app/terminal/terminal.c
  12. 12
      src/user/syscall.asm
  13. 4
      src/user/syscall.h

@ -19,3 +19,4 @@ mmd -i build/os.img@@"$((partition_offset * sector_size))" ::bin
mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/terminal ::bin/terminal mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/terminal ::bin/terminal
mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/echo ::bin/echo mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/echo ::bin/echo
mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/ls ::bin/ls mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/ls ::bin/ls
mcopy -i build/os.img@@"$((partition_offset * sector_size))" build/cat ::bin/cat

@ -223,3 +223,42 @@ custom_target(
command: ['objcopy', '-O', 'binary', '@INPUT@', '@OUTPUT@'], command: ['objcopy', '-O', 'binary', '@INPUT@', '@OUTPUT@'],
build_by_default: true, build_by_default: true,
) )
cat_start = custom_target(
'cat_start',
input: 'src/user/start.asm',
output: 'cat_start.o',
command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'],
)
cat_syscall = custom_target(
'cat_syscall',
input: 'src/user/syscall.asm',
output: 'cat_syscall.o',
command: [nasm, '-f', 'elf64', '-o', '@OUTPUT@', '@INPUT@'],
)
cat_elf = executable(
'cat.elf',
sources: [cat_start, cat_syscall, lib_sources, 'src/user/app/cat/cat.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(
'cat',
input: cat_elf,
output: 'cat',
command: ['objcopy', '-O', 'binary', '@INPUT@', '@OUTPUT@'],
build_by_default: true,
)

@ -44,6 +44,16 @@ path_t *path_open(const fs_node_t *root, const path_t *source, const char *path)
return result; return result;
} }
fs_node_t *path_open_node(const fs_node_t *root, const path_t *source, const char *path) {
path_t *p = path_open(root, source, path);
if (!p) {
return NUL;
}
fs_node_t *n = fs_open_again(p->depth ? p->stack[p->depth - 1] : root);
path_close(p);
return n;
}
fs_node_t *path_open_file(const fs_node_t *root, const path_t *source, const char *path) { fs_node_t *path_open_file(const fs_node_t *root, const path_t *source, const char *path) {
path_t *p = path_open(root, source, path); path_t *p = path_open(root, source, path);
if (!p) { if (!p) {
@ -65,7 +75,7 @@ fs_node_t *path_open_directory(const fs_node_t *root, const path_t *source, cons
} }
if (!p->depth) { if (!p->depth) {
path_close(p); path_close(p);
return (fs_node_t *)root; return fs_open_again(root);
} }
if (!p->stack[p->depth - 1]->is_dir) { if (!p->stack[p->depth - 1]->is_dir) {
path_close(p); path_close(p);

@ -12,6 +12,8 @@ typedef struct {
path_t *path_open(const fs_node_t *root, const path_t *source, const char *path); path_t *path_open(const fs_node_t *root, const path_t *source, const char *path);
fs_node_t *path_open_node(const fs_node_t *root, const path_t *source, const char *path);
fs_node_t *path_open_file(const fs_node_t *root, const path_t *source, const char *path); fs_node_t *path_open_file(const fs_node_t *root, const path_t *source, const char *path);
fs_node_t *path_open_directory(const fs_node_t *root, const path_t *source, const char *path); fs_node_t *path_open_directory(const fs_node_t *root, const path_t *source, const char *path);

@ -59,6 +59,7 @@ process_t *process_create(const process_t *parent, const uint8_t *code, uint64_t
proc->root = parent->root; proc->root = parent->root;
proc->stdin = parent->stdin; proc->stdin = parent->stdin;
proc->stdout = parent->stdout; proc->stdout = parent->stdout;
proc->free_fd = 2;
return proc; return proc;
} }

@ -6,6 +6,13 @@
#define USER_RFLAGS 0x202 #define USER_RFLAGS 0x202
#define MAX_FDS 16
typedef struct {
fs_node_t *node;
uint64_t offset;
} fd_entry_t;
typedef struct process { typedef struct process {
const struct process *parent; const struct process *parent;
uint64_t *pml4; uint64_t *pml4;
@ -17,6 +24,8 @@ typedef struct process {
const path_t *cwd; const path_t *cwd;
const stream_t *stdin; const stream_t *stdin;
const stream_t *stdout; const stream_t *stdout;
fd_entry_t fds[MAX_FDS];
uint64_t free_fd;
} process_t; } process_t;
typedef void (*app_t)(process_t *proc, uint8_t argc, char **argv); typedef void (*app_t)(process_t *proc, uint8_t argc, char **argv);

@ -7,6 +7,7 @@
#include "src/lib/layout.h" #include "src/lib/layout.h"
#include "src/lib/memory.h" #include "src/lib/memory.h"
#include "src/lib/string.h" #include "src/lib/string.h"
#include "src/lib/util.h"
#define MSR_EFER 0xC0000080 #define MSR_EFER 0xC0000080
#define MSR_STAR 0xC0000081 #define MSR_STAR 0xC0000081
@ -33,13 +34,37 @@ void syscall_init() {
} }
static uint64_t read(uint64_t fd, uint64_t max, char *to) { static uint64_t read(uint64_t fd, uint64_t max, char *to) {
(void)fd; switch (fd) {
return current_process->stdin->read(current_process->stdin, max, to); case 0:
return current_process->stdin->read(current_process->stdin, max, to);
case 1:
return current_process->stdout->read(current_process->stdout, max, to);
default:
if (!current_process->fds[fd].node) {
return (uint64_t)-1;
}
if (current_process->fds[fd].offset >= current_process->fds[fd].node->size) {
return 0;
}
uint64_t remaining = current_process->fds[fd].node->size - current_process->fds[fd].offset;
uint64_t to_read = remaining > max ? max : remaining;
fs_read(current_process->fds[fd].node, current_process->fds[fd].offset, to_read, to);
current_process->fds[fd].offset += to_read;
return to_read;
}
return (uint64_t)-1;
} }
static uint64_t write(uint64_t fd, const char *from, uint64_t bytes) { static uint64_t write(uint64_t fd, const char *from, uint64_t bytes) {
(void)fd; switch (fd) {
return current_process->stdout->write(current_process->stdout, from, bytes); case 0:
return current_process->stdin->write(current_process->stdin, from, bytes);
case 1:
return current_process->stdout->write(current_process->stdout, from, bytes);
default:
return (uint64_t)-1;
}
return (uint64_t)-1;
} }
static uint64_t getcwd(uint64_t max, char *to) { static uint64_t getcwd(uint64_t max, char *to) {
@ -170,6 +195,30 @@ static uint64_t readdir(const char *path, uint64_t index, uint64_t max, char *to
return size; return size;
} }
static uint64_t open(const char *path) {
if (current_process->free_fd >= MAX_FDS) {
return (uint64_t)-1;
}
fs_node_t *node = path_open_node(current_process->root, current_process->cwd, path);
if (!node) {
return (uint64_t)-1;
}
current_process->fds[current_process->free_fd].node = node;
current_process->fds[current_process->free_fd].offset++;
current_process->free_fd++;
return current_process->free_fd - 1;
}
static uint64_t close(uint64_t fd) {
if (fd >= MAX_FDS || !current_process->fds[fd].node) {
return (uint64_t)-1;
}
fs_close(current_process->fds[fd].node);
current_process->fds[fd].node = NUL;
current_process->fds[fd].offset = 0;
return 0;
}
static void exit() { static void exit() {
process_switch_to(&current_process->kernel_rsp, current_process->parent->kernel_rsp, VIRT_TO_PHYS(current_process->parent->pml4)); process_switch_to(&current_process->kernel_rsp, current_process->parent->kernel_rsp, VIRT_TO_PHYS(current_process->parent->pml4));
} }
@ -188,6 +237,10 @@ uint64_t syscall_dispatch(uint64_t func, uint64_t arg1, uint64_t arg2, uint64_t
return spawn((const char *)arg1, arg2, (const char **)arg3); return spawn((const char *)arg1, arg2, (const char **)arg3);
case SYSCALL_READDIR: case SYSCALL_READDIR:
return readdir((const char *)arg1, arg2, arg3, (char *)arg4); return readdir((const char *)arg1, arg2, arg3, (char *)arg4);
case SYSCALL_OPEN:
return open((const char *)arg1);
case SYSCALL_CLOSE:
return close(arg1);
case SYSCALL_EXIT: case SYSCALL_EXIT:
exit(); exit();
return 0; return 0;

@ -8,6 +8,8 @@
#define SYSCALL_CHDIR 3 #define SYSCALL_CHDIR 3
#define SYSCALL_SPAWN 4 #define SYSCALL_SPAWN 4
#define SYSCALL_READDIR 5 #define SYSCALL_READDIR 5
#define SYSCALL_OPEN 6
#define SYSCALL_CLOSE 7
#define SYSCALL_EXIT 60 #define SYSCALL_EXIT 60
void syscall_init(); void syscall_init();

@ -0,0 +1,45 @@
#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));
uint64_t cat(const char *path) {
uint64_t fd = open(path);
if (fd == (uint64_t)-1) {
PRINT_S("cat: path does not exist\n");
return (uint64_t)-1;
}
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 (uint64_t)-1;
}
write(1, buffer, bytes);
}
close(fd);
return 0;
}
uint64_t main(uint64_t argc, const char **argv) {
if (argc == 1) {
return cat(".");
}
uint64_t code = 0;
for (uint64_t i = 1; i < argc; i++) {
if (cat(argv[i]) == (uint64_t)-1) {
code = (uint64_t)-1;
}
}
return code;
}

@ -1,8 +1,8 @@
#include "src/lib/string.h" #include "src/lib/string.h"
#include "src/user/syscall.h" #include "src/user/syscall.h"
#define WRITE_S(s) write(1, s, sizeof(s) - 1); #define PRINT_S(s) write(1, s, sizeof(s) - 1);
#define WRITE_D(s) write(1, s, string_length(s)); #define PRINT_D(s) write(1, s, string_length(s));
uint64_t ls(const char *path) { uint64_t ls(const char *path) {
uint64_t i = 0; uint64_t i = 0;
@ -13,11 +13,11 @@ uint64_t ls(const char *path) {
return 0; return 0;
} }
if (s == (uint64_t)-1) { if (s == (uint64_t)-1) {
WRITE_S("ls: path does not exist\n"); PRINT_S("ls: path does not exist\n");
return (uint64_t)-1; return (uint64_t)-1;
} }
WRITE_D(out); PRINT_D(out);
WRITE_S("\n"); PRINT_S("\n");
i++; i++;
} }
} }
@ -30,9 +30,9 @@ uint64_t main(uint64_t argc, const char **argv) {
uint64_t code = 0; uint64_t code = 0;
for (uint64_t i = 1; i < argc; i++) { for (uint64_t i = 1; i < argc; i++) {
if (argc > 2) { if (argc > 2) {
WRITE_S("\n"); PRINT_S("\n");
WRITE_D(argv[i]); PRINT_D(argv[i]);
WRITE_S(":\n"); PRINT_S(":\n");
} }
if (ls(argv[i]) == (uint64_t)-1) { if (ls(argv[i]) == (uint64_t)-1) {
code = (uint64_t)-1; code = (uint64_t)-1;

@ -8,8 +8,8 @@ static void cd(uint8_t argc, char **argv);
#define PATH "/bin/" #define PATH "/bin/"
#define WRITE_S(s) write(1, s, sizeof(s) - 1); #define PRINT_S(s) write(1, s, sizeof(s) - 1);
#define WRITE_D(s) write(1, s, string_length(s)); #define PRINT_D(s) write(1, s, string_length(s));
#define PROMPT_LENGTH 255 #define PROMPT_LENGTH 255
@ -34,25 +34,25 @@ static app_entry_t builtins[] = {
static void help(uint8_t argc, __attribute__((unused)) char **argv) { static void help(uint8_t argc, __attribute__((unused)) char **argv) {
if (argc) { if (argc) {
WRITE_S("help: expects no arguments"); PRINT_S("help: expects no arguments");
return; return;
} }
WRITE_S("Available commands:\n"); PRINT_S("Available commands:\n");
for (uint64_t i = 0; i < sizeof(builtins) / sizeof(app_entry_t); i++) { for (uint64_t i = 0; i < sizeof(builtins) / sizeof(app_entry_t); i++) {
WRITE_D(builtins[i].name); PRINT_D(builtins[i].name);
WRITE_S("\n"); PRINT_S("\n");
} }
} }
static void cd(uint8_t argc, char **argv) { static void cd(uint8_t argc, char **argv) {
if (argc != 2) { if (argc != 2) {
WRITE_S("cd: requires a single path\n"); PRINT_S("cd: requires a single path\n");
return; return;
} }
if (chdir(argv[1]) == (uint64_t)-1) { if (chdir(argv[1]) == (uint64_t)-1) {
WRITE_S("cd: path does not exist\n"); PRINT_S("cd: path does not exist\n");
} }
} }
@ -62,9 +62,9 @@ static void print_prompt() {
char *components[16]; char *components[16];
uint8_t cl = string_split(path, '/', 16, components); uint8_t cl = string_split(path, '/', 16, components);
WRITE_S("[") PRINT_S("[")
WRITE_D(pl == 1 ? "/" : components[cl - 1]); PRINT_D(pl == 1 ? "/" : components[cl - 1]);
WRITE_S("]$ "); PRINT_S("]$ ");
} }
static void on_home_pressed() { static void on_home_pressed() {
@ -109,7 +109,7 @@ static void on_enter_pressed() {
char *argv[16]; char *argv[16];
uint8_t argc = (uint8_t)string_split(prompt, ' ', 16, argv); uint8_t argc = (uint8_t)string_split(prompt, ' ', 16, argv);
WRITE_S("\n"); PRINT_S("\n");
uint8_t i; uint8_t i;
for (i = 0; i < sizeof(builtins) / sizeof(app_entry_t); i++) { for (i = 0; i < sizeof(builtins) / sizeof(app_entry_t); i++) {
@ -124,7 +124,7 @@ static void on_enter_pressed() {
memory_copy(PATH, sizeof(PATH), path); memory_copy(PATH, sizeof(PATH), path);
memory_copy(argv[0], size - sizeof(PATH), path + sizeof(PATH) - 1); memory_copy(argv[0], size - sizeof(PATH), path + sizeof(PATH) - 1);
if (spawn(path, argc, (const char **)argv) == (uint64_t)-1) { if (spawn(path, argc, (const char **)argv) == (uint64_t)-1) {
WRITE_S("terminal: program not found\n"); PRINT_S("terminal: program not found\n");
} }
memory_free(path); memory_free(path);
} }
@ -133,13 +133,13 @@ static void on_enter_pressed() {
} }
static void on_cancel_pressed() { static void on_cancel_pressed() {
WRITE_S("^C\n"); PRINT_S("^C\n");
prompt_length = prompt_offset = 0; prompt_length = prompt_offset = 0;
print_prompt(); print_prompt();
} }
static void on_disconnect_pressed() { static void on_disconnect_pressed() {
WRITE_S("^D\n"); PRINT_S("^D\n");
exit(); exit();
} }
@ -269,7 +269,7 @@ uint64_t main() {
memory_set('\b', PROMPT_LENGTH, (char *)bs); memory_set('\b', PROMPT_LENGTH, (char *)bs);
memory_set(' ', PROMPT_LENGTH, (char *)ws); memory_set(' ', PROMPT_LENGTH, (char *)ws);
WRITE_D("Welcome to FreywarOS v" VERSION "!\n\n"); PRINT_D("Welcome to FreywarOS v" VERSION "!\n\n");
print_prompt(); print_prompt();

@ -37,6 +37,18 @@ readdir:
syscall syscall
ret ret
global open
open:
mov rax, 6
syscall
ret
global close
close:
mov rax, 7
syscall
ret
global exit global exit
exit: exit:
mov rax, 60 mov rax, 60

@ -14,4 +14,8 @@ uint64_t spawn(const char *path, uint64_t argc, const char **argv);
uint64_t readdir(const char *path, uint64_t index, uint64_t max, char *to); uint64_t readdir(const char *path, uint64_t index, uint64_t max, char *to);
uint64_t open(const char *path);
uint64_t close(uint64_t fd);
void exit(); void exit();

Loading…
Cancel
Save