From a7ef49da0e07e0a1195794b527fa400c0235fcc7 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Tue, 29 Apr 2025 20:59:46 -0400 Subject: tarfs --- kernel/include/comus/fs/tar.h | 18 ++++++++++++++++++ kernel/include/comus/user.h | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 kernel/include/comus/fs/tar.h (limited to 'kernel/include') diff --git a/kernel/include/comus/fs/tar.h b/kernel/include/comus/fs/tar.h new file mode 100644 index 0000000..084c8bd --- /dev/null +++ b/kernel/include/comus/fs/tar.h @@ -0,0 +1,18 @@ +/** + * @file tar.h + * + * Tarball + */ + +#ifndef TAR_FS_H_ +#define TAR_FS_H_ + +#include + +/** + * Attempts to mount tar filesystem on disk + * @returns 0 on success + */ +int tar_mount(struct file_system *fs); + +#endif /* fs.h */ diff --git a/kernel/include/comus/user.h b/kernel/include/comus/user.h index f51ada5..a1a718b 100644 --- a/kernel/include/comus/user.h +++ b/kernel/include/comus/user.h @@ -15,7 +15,7 @@ /** * Load a user elf program from a file into a pcb */ -int user_load(struct pcb *pcb, struct disk *disk); +int user_load(struct pcb *pcb, struct file *file); /** * Clone a user process. Used for fork(). -- cgit v1.2.3-freya From 1a10a3725e7bea67e558715f6e9f78abcb415b3a Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Wed, 30 Apr 2025 21:07:46 -0400 Subject: finish syscall impls --- kernel/fs/tar.c | 15 ++-- kernel/include/comus/fs.h | 11 ++- kernel/include/comus/limits.h | 4 + kernel/include/comus/procs.h | 4 + kernel/include/comus/syscalls.h | 3 +- kernel/include/comus/user.h | 4 +- kernel/main.c | 5 +- kernel/procs.c | 3 +- kernel/syscall.c | 181 +++++++++++++++++++++++++++++++++++++++- kernel/user.c | 59 +++++++++++-- user/include/stdio.h | 6 +- user/include/sys/types.h | 15 ++++ user/include/unistd.h | 16 ++-- user/init.c | 75 +++++++++++++++++ user/lib/entry.S | 5 +- user/lib/fclose.c | 10 +++ user/lib/fseek.c | 16 ++++ user/lib/syscall.S | 3 + 18 files changed, 398 insertions(+), 37 deletions(-) create mode 100644 user/include/sys/types.h create mode 100644 user/init.c create mode 100644 user/lib/fclose.c create mode 100644 user/lib/fseek.c (limited to 'kernel/include') diff --git a/kernel/fs/tar.c b/kernel/fs/tar.c index 7f34107..86af81f 100644 --- a/kernel/fs/tar.c +++ b/kernel/fs/tar.c @@ -150,15 +150,15 @@ int tar_seek(struct file *in, long int off, int whence) switch (whence) { case SEEK_SET: file->offset = off; - return 0; + return file->offset; case SEEK_CUR: file->offset += off; - return 0; + return file->offset; case SEEK_END: file->offset = file->len + off; - return 0; + return file->offset; default: - return 1; + return -1; } } @@ -202,12 +202,17 @@ void tar_close(struct file *file) kfree(file); } -int tar_open(struct file_system *fs, const char *path, struct file **out) +int tar_open(struct file_system *fs, const char *path, int flags, + struct file **out) { struct tar_file *file; struct tar_hdr hdr; size_t sect; + // cannot create or write files + if (flags != O_RDONLY) + return 1; + if (tar_locate(fs, path, &hdr, §, NULL, false)) return 1; diff --git a/kernel/include/comus/fs.h b/kernel/include/comus/fs.h index 2f678b6..044330b 100644 --- a/kernel/include/comus/fs.h +++ b/kernel/include/comus/fs.h @@ -148,6 +148,15 @@ struct file { void (*close)(struct file *file); }; +/// open flags +enum { + O_CREATE = 0x01, + O_RDONLY = 0x02, + O_WRONLY = 0x04, + O_APPEND = 0x08, + O_RDWR = 0x10, +}; + /// file system vtable, used for opening /// and stating files. filesystem mount functions must /// set fs_name, fs_disk, open, and stat. @@ -200,7 +209,7 @@ struct file_system { /// filesystem name const char *fs_name; /// opens a file - int (*open)(struct file_system *fs, const char *fullpath, + int (*open)(struct file_system *fs, const char *fullpath, int flags, struct file **out); /// stats a file int (*stat)(struct file_system *fs, const char *fullpath, diff --git a/kernel/include/comus/limits.h b/kernel/include/comus/limits.h index 4cb348d..1ef13b4 100644 --- a/kernel/include/comus/limits.h +++ b/kernel/include/comus/limits.h @@ -12,6 +12,10 @@ /// max number of processes #define N_PROCS 256 +/// process limits +#define N_OPEN_FILES 64 +#define N_ARGS 64 + /// max nubmer of pci devices #define N_PCI_DEV 256 diff --git a/kernel/include/comus/procs.h b/kernel/include/comus/procs.h index 7b1a70a..3df31c3 100644 --- a/kernel/include/comus/procs.h +++ b/kernel/include/comus/procs.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -59,6 +60,9 @@ struct pcb { char *heap_start; size_t heap_len; + // open files + struct file *open_files[N_OPEN_FILES]; + // elf metadata Elf64_Ehdr elf_header; Elf64_Phdr elf_segments[N_ELF_SEGMENTS]; diff --git a/kernel/include/comus/syscalls.h b/kernel/include/comus/syscalls.h index f714184..8b671c2 100644 --- a/kernel/include/comus/syscalls.h +++ b/kernel/include/comus/syscalls.h @@ -30,9 +30,10 @@ #define SYS_poweroff 17 #define SYS_drm 18 #define SYS_ticks 19 +#define SYS_seek 20 // UPDATE THIS DEFINITION IF MORE SYSCALLS ARE ADDED! -#define N_SYSCALLS 20 +#define N_SYSCALLS 21 // interrupt vector entry for system calls #define VEC_SYSCALL 0x80 diff --git a/kernel/include/comus/user.h b/kernel/include/comus/user.h index a1a718b..1b35ac4 100644 --- a/kernel/include/comus/user.h +++ b/kernel/include/comus/user.h @@ -9,13 +9,15 @@ #ifndef USER_H_ #define USER_H_ +#include "comus/memory.h" #include #include /** * Load a user elf program from a file into a pcb */ -int user_load(struct pcb *pcb, struct file *file); +int user_load(struct pcb *pcb, struct file *file, const char **args, + mem_ctx_t args_ctx); /** * Clone a user process. Used for fork(). diff --git a/kernel/main.c b/kernel/main.c index a306e2b..72a6668 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -26,6 +26,7 @@ void load_init(void) { struct file_system *fs; struct file *file; + const char *init_vector[] = { NULL }; if (pcb_alloc(&init_pcb)) return; @@ -36,10 +37,10 @@ void load_init(void) return; // get init bin - if (fs->open(fs, "bin/apple", &file)) + if (fs->open(fs, "bin/init", O_RDONLY, &file)) return; - if (user_load(init_pcb, file)) { + if (user_load(init_pcb, file, init_vector, kernel_mem_ctx)) { file->close(file); return; } diff --git a/kernel/procs.c b/kernel/procs.c index 9bf7508..d471416 100644 --- a/kernel/procs.c +++ b/kernel/procs.c @@ -1,3 +1,4 @@ +#include "lib/kio.h" #include #include #include @@ -218,7 +219,7 @@ void pcb_zombify(struct pcb *victim) status = (int *)PCB_ARG2(parent); if (pid == 0 || pid == victim->pid) { - PCB_RET(parent) = zchild->pid; + PCB_RET(parent) = victim->pid; assert( pcb_queue_remove(syscall_queue[SYS_waitpid], parent) == SUCCESS, diff --git a/kernel/syscall.c b/kernel/syscall.c index 96d2fcf..44ebfa0 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -1,3 +1,5 @@ +#include "comus/fs.h" +#include "lib/kio.h" #include #include #include @@ -23,6 +25,16 @@ static struct pcb *pcb; #define stdout 1 #define stderr 2 +static struct file *get_file_ptr(int fd) +{ + // valid index? + if (fd < 3 || fd >= (N_OPEN_FILES + 3)) + return NULL; + + // will be NULL if not open + return pcb->open_files[fd - 3]; +} + __attribute__((noreturn)) static int sys_exit(void) { ARG1(int, status); @@ -36,6 +48,33 @@ __attribute__((noreturn)) static int sys_exit(void) static int sys_waitpid(void) { + ARG1(pid_t, pid); + ARG2(int *, status); + + struct pcb *child; + for (int i = 0; i < N_PROCS; i++) { + child = &ptable[i]; + if (child->state != PROC_STATE_ZOMBIE) + continue; + if (child->parent != pcb) + continue; + + // we found a child! + if (pid && pid != child->pid) + continue; + + // set status + mem_ctx_switch(pcb->memctx); + *status = child->exit_status; + mem_ctx_switch(kernel_mem_ctx); + + // clean up child process + pcb_cleanup(child); + + // return + return child->pid; + } + // arguments are read later // by procs.c pcb->state = PROC_STATE_BLOCKED; @@ -60,6 +99,125 @@ static int sys_fork(void) return 0; } +static int sys_exec(void) +{ + ARG1(const char *, in_filename); + ARG2(const char **, in_args); + + struct file_system *fs; + struct file *file; + char filename[N_FILE_NAME]; + struct pcb save; + + // save data + file = NULL; + save = *pcb; + + // read filename + mem_ctx_switch(pcb->memctx); + memcpy(filename, in_filename, strlen(in_filename) + 1); + mem_ctx_switch(kernel_mem_ctx); + + // get binary + fs = fs_get_root_file_system(); + if (fs == NULL) + goto fail; + if (fs->open(fs, filename, O_RDONLY, &file)) + goto fail; + + // load program + save = *pcb; + if (user_load(pcb, file, in_args, save.memctx)) + goto fail; + file->close(file); + mem_ctx_free(save.memctx); + schedule(pcb); + dispatch(); + +fail: + *pcb = save; + if (file) + file->close(file); + return 1; +} + +static int sys_open(void) +{ + ARG1(const char *, in_filename); + ARG2(int, flags); + + char filename[N_FILE_NAME]; + struct file_system *fs; + struct file **file; + int fd; + + // read filename + mem_ctx_switch(pcb->memctx); + memcpy(filename, in_filename, strlen(in_filename)); + mem_ctx_switch(kernel_mem_ctx); + + // get fd + for (fd = 3; fd < (N_OPEN_FILES + 3); fd++) { + if (pcb->open_files[fd - 3] == NULL) { + file = &pcb->open_files[fd - 3]; + break; + } + } + + // could not find fd + if (fd == (N_OPEN_FILES + 3)) + return -1; + + // open file + fs = fs_get_root_file_system(); + if (fs == NULL) + return -1; + if (fs->open(fs, filename, flags, file)) + return -1; + + // file opened + return fd; +} + +static int sys_close(void) +{ + ARG1(int, fd); + + struct file *file; + file = get_file_ptr(fd); + if (file == NULL) + return 1; + + file->close(file); + return 0; +} + +static int sys_read(void) +{ + ARG1(int, fd); + ARG2(void *, buffer); + ARG3(size_t, nbytes); + + struct file *file; + char *map_buf; + + map_buf = kmapuseraddr(pcb->memctx, buffer, nbytes); + if (map_buf == NULL) + return -1; + + file = get_file_ptr(fd); + if (file == NULL) + goto fail; + + nbytes = file->read(file, map_buf, nbytes); + kunmapaddr(map_buf); + return nbytes; + +fail: + kunmapaddr(map_buf); + return -1; +} + static int sys_write(void) { ARG1(int, fd); @@ -324,17 +482,34 @@ static int sys_ticks(void) return 0; } +static int sys_seek(void) +{ + RET(long int, ret); + ARG1(int, fd); + ARG2(long int, off); + ARG3(int, whence); + + struct file *file; + file = get_file_ptr(fd); + if (file == NULL) + return -1; + + *ret = file->seek(file, off, whence); + return 0; +} + static int (*syscall_tbl[N_SYSCALLS])(void) = { [SYS_exit] = sys_exit, [SYS_waitpid] = sys_waitpid, - [SYS_fork] = sys_fork, [SYS_exec] = NULL, - [SYS_open] = NULL, [SYS_close] = NULL, - [SYS_read] = NULL, [SYS_write] = sys_write, + [SYS_fork] = sys_fork, [SYS_exec] = sys_exec, + [SYS_open] = sys_open, [SYS_close] = sys_close, + [SYS_read] = sys_read, [SYS_write] = sys_write, [SYS_getpid] = sys_getpid, [SYS_getppid] = sys_getppid, [SYS_gettime] = sys_gettime, [SYS_getprio] = sys_getprio, [SYS_setprio] = sys_setprio, [SYS_kill] = sys_kill, [SYS_sleep] = sys_sleep, [SYS_brk] = sys_brk, [SYS_sbrk] = sys_sbrk, [SYS_poweroff] = sys_poweroff, [SYS_drm] = sys_drm, [SYS_ticks] = sys_ticks, + [SYS_seek] = sys_seek, }; void syscall_handler(void) diff --git a/kernel/user.c b/kernel/user.c index 4007383..9612710 100644 --- a/kernel/user.c +++ b/kernel/user.c @@ -57,7 +57,7 @@ static int user_load_segment(struct pcb *pcb, struct file *file, int idx) return 1; // seek to start of segment - if (file->seek(file, hdr.p_offset, SEEK_SET)) + if (file->seek(file, hdr.p_offset, SEEK_SET) < 0) return 1; // load data @@ -151,7 +151,7 @@ static int user_load_elf(struct pcb *pcb, struct file *file) { int ret = 0; - if (file->seek(file, 0, SEEK_SET)) + if (file->seek(file, 0, SEEK_SET) < 0) return 1; ret = file->read(file, (char *)&pcb->elf_header, sizeof(Elf64_Ehdr)); if (ret < 0) @@ -161,7 +161,7 @@ static int user_load_elf(struct pcb *pcb, struct file *file) return 1; pcb->n_elf_segments = pcb->elf_header.e_phnum; - if (file->seek(file, pcb->elf_header.e_phoff, SEEK_SET)) + if (file->seek(file, pcb->elf_header.e_phoff, SEEK_SET) < 0) return 1; ret = file->read(file, (char *)&pcb->elf_segments, sizeof(Elf64_Phdr) * pcb->elf_header.e_phnum); @@ -171,14 +171,56 @@ static int user_load_elf(struct pcb *pcb, struct file *file) return 0; } -static int user_setup_stack(struct pcb *pcb) +static int user_setup_stack(struct pcb *pcb, const char **args, + mem_ctx_t args_ctx) { - // allocate stack + /* args */ + int argbytes = 0; + int argc = 0; + + mem_ctx_switch(args_ctx); + + while (args[argc] != NULL) { + int n = strlen(args[argc]) + 1; + if ((argbytes + n) > USER_STACK_LEN) { + // oops - ignore this and any others + break; + } + argbytes += n; + argc++; + } + + // round to nearest multiple of 8 + argbytes = (argbytes + 7) & 0xfffffffffffffff8; + + // allocate arg strings on kernel stack + char argstrings[argbytes]; + char *argv[argc + 1]; + memset(argstrings, 0, sizeof(argstrings)); + memset(argv, 0, sizeof(argv)); + + // Next, duplicate the argument strings, and create pointers to + // each one in our argv. + char *tmp = argstrings; + for (int i = 0; i < argc; ++i) { + int nb = strlen(args[i]) + 1; + strcpy(tmp, args[i]); + argv[i] = tmp; + tmp += nb; + } + + // trailing NULL pointer + argv[argc] = NULL; + + mem_ctx_switch(kernel_mem_ctx); + + /* stack */ if (mem_alloc_pages_at(pcb->memctx, USER_STACK_LEN / PAGE_SIZE, (void *)(USER_STACK_TOP - USER_STACK_LEN), F_WRITEABLE | F_UNPRIVILEGED) == NULL) return 1; + /* regs */ memset(&pcb->regs, 0, sizeof(struct cpu_regs)); // pgdir @@ -189,7 +231,7 @@ static int user_setup_stack(struct pcb *pcb) pcb->regs.es = USER_DATA | RING3; pcb->regs.ds = USER_DATA | RING3; // registers - pcb->regs.rdi = 0; // argc + pcb->regs.rdi = argc; // argc pcb->regs.rsi = 0; // argv // intruction pointer pcb->regs.rip = pcb->elf_header.e_entry; @@ -205,7 +247,8 @@ static int user_setup_stack(struct pcb *pcb) return 0; } -int user_load(struct pcb *pcb, struct file *file) +int user_load(struct pcb *pcb, struct file *file, const char **args, + mem_ctx_t args_ctx) { // check inputs if (pcb == NULL || file == NULL) @@ -225,7 +268,7 @@ int user_load(struct pcb *pcb, struct file *file) goto fail; // setup process stack - if (user_setup_stack(pcb)) + if (user_setup_stack(pcb, args, args_ctx)) goto fail; // success diff --git a/user/include/stdio.h b/user/include/stdio.h index fe29c9d..bb57c6d 100644 --- a/user/include/stdio.h +++ b/user/include/stdio.h @@ -252,7 +252,7 @@ extern size_t fwrite(const void *restrict ptr, size_t size, size_t n, * @param stream - the stream to seek * @param off - the offset from whence * @param whence - where to seek from (SEEK_SET, SEEK_CUR, SEEK_END) - * @returns 0 on success, -1 on error setting errno + * @returns new offset on success, -1 on error */ extern int fseek(FILE *stream, long int off, int whence); @@ -260,9 +260,9 @@ extern int fseek(FILE *stream, long int off, int whence); * return the current position of stream * * @param stream - the stream to tell - * @return the position on success, -1 on error setting errno + * @returns new offset on success, -1 on error */ -extern long int ftell(FILE *stream); +extern long ftell(FILE *stream); /** * rewing to the begining of a stream diff --git a/user/include/sys/types.h b/user/include/sys/types.h new file mode 100644 index 0000000..f1b3266 --- /dev/null +++ b/user/include/sys/types.h @@ -0,0 +1,15 @@ +/** + * @file types.h + * + * @author Freya Murphy + * + * System types + */ + +#ifndef _TYPES_H +#define _TYPES_H + +typedef long int off_t; +typedef unsigned short pid_t; + +#endif /* types.h */ diff --git a/user/include/unistd.h b/user/include/unistd.h index cad3a81..4f582d2 100644 --- a/user/include/unistd.h +++ b/user/include/unistd.h @@ -11,11 +11,10 @@ #include #include +#include /* System Call Definitions */ -typedef unsigned short pid_t; - enum { S_SET = 0, S_CUR = 1, @@ -27,7 +26,7 @@ enum { O_RDONLY = 0x02, O_WRONLY = 0x04, O_APPEND = 0x08, - O_RDWR = O_RDONLY | O_WRONLY, + O_RDWR = 0x010, }; /** @@ -67,11 +66,9 @@ extern int fork(void); * * @param prog - program table index of the program to exec * @param args - the command-line argument vector - * - * Does not return if it succeeds; if it returns, something has - * gone wrong. + * @returns error code on failure */ -extern void exec(const char *filename, char **args); +extern int exec(const char *filename, const char **args); /** * open a stream with a given filename @@ -85,8 +82,9 @@ extern int open(const char *filename, int flags); * closes a stream with the given file descriptior * * @param fd - the file descriptior of the open stream + * @returns 0 on success, error code on invalid fd */ -extern void close(int fd); +extern int close(int fd); /** * read into a buffer from a stream @@ -116,7 +114,7 @@ extern int write(int fd, const void *buffer, size_t nbytes); * @param whence - whence to seek * @return 0 on success, or an error code */ -extern int seek(int fd, long int off, int whence); +extern off_t seek(int fd, off_t off, int whence); /** * gets the pid of the calling process diff --git a/user/init.c b/user/init.c new file mode 100644 index 0000000..18e70a2 --- /dev/null +++ b/user/init.c @@ -0,0 +1,75 @@ +#include +#include + +#define MAX_ARGS 4 + +struct proc { + pid_t pid; + const char *filename; + const char *args[MAX_ARGS]; +}; + +static struct proc spawn_table[] = { + // apple + { 0, "bin/apple", { NULL } }, + // end, + { 0, NULL, { NULL } }, +}; + +static int spawn(const char *filename, const char **args) +{ + int ret; + + // fork init + if ((ret = fork()) != 0) + return ret; + + // call exec + if ((ret = exec(filename, args))) + exit(ret); + + // should not happen! + exit(1); +} + +int main(void) +{ + struct proc *proc; + + // spawn our processes + for (proc = spawn_table; proc->filename != NULL; proc++) { + int pid; + pid = spawn(proc->filename, proc->args); + if (pid < 0) + fprintf(stderr, "init: cannot exec '%s': %d\n", proc->filename, + pid); + proc->pid = pid; + } + + // clean up dead on restart ours + while (1) { + int pid, status; + + pid = waitpid(0, &status); + if (pid < 0) + continue; + + printf("init: pid %d exited with %d\n", pid, status); + + // figure out if this is one of ours + for (proc = spawn_table; proc->filename != NULL; proc++) { + if (proc->pid == pid) { + proc->pid = 0; + pid = spawn(proc->filename, proc->args); + if (pid < 0) + fprintf(stderr, "init: cannot exec '%s': %d\n", + proc->filename, pid); + proc->pid = pid; + break; + } + } + } + + // very very bad! + return 1; +} diff --git a/user/lib/entry.S b/user/lib/entry.S index 40570b5..efaa652 100644 --- a/user/lib/entry.S +++ b/user/lib/entry.S @@ -5,7 +5,6 @@ .section .text .code64 _start: - call main - subq $16, %rsp # ??? - pushq %rax + call main + movq %rax, %rdi call exit diff --git a/user/lib/fclose.c b/user/lib/fclose.c new file mode 100644 index 0000000..be31421 --- /dev/null +++ b/user/lib/fclose.c @@ -0,0 +1,10 @@ +#include +#include + +void fclose(FILE *stream) +{ + int fd; + + fd = (uintptr_t)stream; + close(fd); +} diff --git a/user/lib/fseek.c b/user/lib/fseek.c new file mode 100644 index 0000000..a7a3377 --- /dev/null +++ b/user/lib/fseek.c @@ -0,0 +1,16 @@ +#include +#include + +int fseek(FILE *stream, long off, int whence) +{ + int fd; + fd = (uintptr_t)stream; + return seek(fd, off, whence); +} + +long ftell(FILE *stream) +{ + int fd; + fd = (uintptr_t)stream; + return seek(fd, 0, SEEK_CUR); +} diff --git a/user/lib/syscall.S b/user/lib/syscall.S index 2ba4dc0..9f7025e 100644 --- a/user/lib/syscall.S +++ b/user/lib/syscall.S @@ -13,6 +13,8 @@ SYSCALL exit SYS_exit SYSCALL waitpid SYS_waitpid SYSCALL fork SYS_fork SYSCALL exec SYS_exec +SYSCALL open SYS_open +SYSCALL close SYS_close SYSCALL read SYS_read SYSCALL write SYS_write SYSCALL getpid SYS_getpid @@ -27,3 +29,4 @@ SYSCALL sbrk SYS_sbrk SYSCALL poweroff SYS_poweroff SYSCALL drm SYS_drm SYSCALL ticks SYS_ticks +SYSCALL seek SYS_seek -- cgit v1.2.3-freya From 592c6992c7ea0c2e4cbea79fadb1c845f2fc050e Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Mon, 5 May 2025 09:54:48 -0400 Subject: pc spkr --- Makefile | 1 + kernel/cpu/idt.c | 9 +++--- kernel/drivers.c | 2 +- kernel/drivers/pit.c | 60 +++++++++++++++++++++++++++++-------- kernel/drivers/ps2.c | 15 ++++++++++ kernel/include/comus/drivers/pit.h | 14 +++++++-- kernel/include/comus/drivers/ps2.h | 13 ++++++++ kernel/include/comus/drivers/spkr.h | 29 ++++++++++++++++++ kernel/include/lib/klib.h | 5 ++++ kernel/lib/panic.c | 17 +++++++++-- 10 files changed, 144 insertions(+), 21 deletions(-) create mode 100644 kernel/include/comus/drivers/spkr.h (limited to 'kernel/include') diff --git a/Makefile b/Makefile index 3739534..2b78eb6 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,7 @@ GRUB ?= grub-mkrescue QEMUOPTS += -cdrom $(BIN)/$(ISO) \ -no-reboot \ -drive format=raw,file=user/bin/initrd.tar \ + -audiodev pa,id=speaker -machine pcspk-audiodev=speaker \ -serial mon:stdio \ -m 4G \ -name kern diff --git a/kernel/cpu/idt.c b/kernel/cpu/idt.c index 7cbbb65..50e15e0 100644 --- a/kernel/cpu/idt.c +++ b/kernel/cpu/idt.c @@ -1,3 +1,5 @@ +#include "comus/drivers/spkr.h" +#include "lib/klib.h" #include #include #include @@ -64,10 +66,11 @@ void idt_init(void) uint64_t isr = (uint64_t)isr_stub_table[vector]; // interrupts before 0x20 are for cpu exceptions + uint64_t gate_type = (isr < 0x20) ? GATE_64BIT_TRAP : GATE_64BIT_INT; entry->kernel_cs = 0x08; // offset of 1 into GDT entry->ist = 0; - entry->flags = PRESENT | RING0 | GATE_64BIT_INT; + entry->flags = PRESENT | RING0 | gate_type; entry->isr_low = isr & 0xffff; entry->isr_mid = (isr >> 16) & 0xffff; entry->isr_high = (isr >> 32) & 0xffffffff; @@ -148,9 +151,7 @@ __attribute__((noreturn)) void idt_exception_handler(uint64_t exception, log_backtrace_ex((void *)state->rip, (void *)state->rbp); - while (1) { - halt(); - } + fatal_loop(); } void isr_save(struct cpu_regs *regs) diff --git a/kernel/drivers.c b/kernel/drivers.c index 3d6ec10..572ab98 100644 --- a/kernel/drivers.c +++ b/kernel/drivers.c @@ -10,7 +10,7 @@ void drivers_init(void) { - pit_set_divider(1193); // 1ms + pit_set_freq(CHAN_TIMER, 1000); // 1ms uart_init(); ps2_init(); pci_init(); diff --git a/kernel/drivers/pit.c b/kernel/drivers/pit.c index 53a9263..0c7a207 100644 --- a/kernel/drivers/pit.c +++ b/kernel/drivers/pit.c @@ -1,29 +1,65 @@ +#include "lib/klib.h" #include #include +#include +#include -#define CHAN_0 0x40 -#define CHAN_1 0x41 -#define CHAN_2 0x42 #define CMD 0x43 +#define SPKR 0x61 + +#define BASE 1193180 volatile uint64_t ticks = 0; -uint16_t pit_read_divider(void) +uint32_t pit_read_freq(uint8_t chan) { - uint16_t count = 0; + uint16_t div = 0; cli(); outb(CMD, 0); // clear bits - count = inb(CHAN_0); // low byte - count |= inb(CHAN_0) << 8; // highbyte + div = inb(chan); // low byte + div |= inb(chan) << 8; // highbyte sti(); - return count; + return div * BASE; } -void pit_set_divider(uint16_t count) +void pit_set_freq(uint8_t chan, uint32_t hz) { - (void)count; + uint16_t div = BASE / hz; cli(); - outb(CHAN_0, count & 0xFF); // low byte - outb(CHAN_0, (count & 0xFF00) >> 8); // high byte + outb(CMD, 0xb6); + outb(chan, div & 0xFF); // low byte + outb(chan, (div & 0xFF00) >> 8); // high byte sti(); } + +void spkr_play_tone(uint32_t hz) +{ + uint8_t reg; + + if (hz == 0) { + spkr_quiet(); + return; + } + + // set spkr freq + pit_set_freq(CHAN_SPKR, hz); + + // enable spkr gate + reg = inb(SPKR); + if (reg != (reg | 0x3)) + outb(SPKR, reg | 0x3); +} + +void spkr_quiet(void) +{ + uint8_t reg; + reg = inb(SPKR) & 0xFC; + outb(SPKR, reg); +} + +void spkr_beep(void) +{ + spkr_play_tone(1000); + kspin_milliseconds(100); + spkr_quiet(); +} diff --git a/kernel/drivers/ps2.c b/kernel/drivers/ps2.c index e260f6b..f605c1d 100644 --- a/kernel/drivers/ps2.c +++ b/kernel/drivers/ps2.c @@ -266,3 +266,18 @@ int ps2_init(void) sti(); return 0; } + +int ps2_set_leds(uint8_t state) +{ + uint8_t result; + + if (!(has_mouse || has_kbd)) + return 1; + + ps2ctrl_out_cmd(0xED); + ps2ctrl_out_data(state); + if ((result = ps2ctrl_in()) != 0xFA) + return 1; + + return 0; +} diff --git a/kernel/include/comus/drivers/pit.h b/kernel/include/comus/drivers/pit.h index 77f0a14..22b8153 100644 --- a/kernel/include/comus/drivers/pit.h +++ b/kernel/include/comus/drivers/pit.h @@ -9,13 +9,23 @@ #ifndef PIT_H_ #define PIT_H_ +#define CHAN_TIMER 0x40 +#define CHAN_SPKR 0x42 + #include // how many time the pit has ticked // not accurate time, good for spinning though extern volatile uint64_t ticks; -uint16_t pit_read_divider(void); -void pit_set_divider(uint16_t count); +/** + * Read timer frequency + */ +uint32_t pit_read_freq(uint8_t chan); + +/** + * Set timer frequency + */ +void pit_set_freq(uint8_t chan, uint32_t hz); #endif diff --git a/kernel/include/comus/drivers/ps2.h b/kernel/include/comus/drivers/ps2.h index 7634e5f..c012ad4 100644 --- a/kernel/include/comus/drivers/ps2.h +++ b/kernel/include/comus/drivers/ps2.h @@ -9,6 +9,8 @@ #ifndef PS2_H_ #define PS2_H_ +#include + /** * Initalize the ps2 controller */ @@ -24,4 +26,15 @@ void ps2kb_recv(void); */ void ps2mouse_recv(void); +/** + * Set ps2 led state + * + * Bits + * ---- + * 0 - Scroll lock + * 1 - Num Lock + * 2 - Caps lock + */ +int ps2_set_leds(uint8_t state); + #endif /* ps2.h */ diff --git a/kernel/include/comus/drivers/spkr.h b/kernel/include/comus/drivers/spkr.h new file mode 100644 index 0000000..f9e427e --- /dev/null +++ b/kernel/include/comus/drivers/spkr.h @@ -0,0 +1,29 @@ +/** + * @file spkr.h + * + * @author Freya Murphy + * + * PC Speaker + */ + +#ifndef SPKR_H_ +#define SPKR_H_ + +#include + +/** + * Play a tone on the pc speaker continuously + */ +void spkr_play_tone(uint32_t hz); + +/** + * Shut up the pc speaker + */ +void spkr_quiet(void); + +/** + * Beep the pc speaker for a short period + */ +void spkr_beep(void); + +#endif /* spkr.h */ diff --git a/kernel/include/lib/klib.h b/kernel/include/lib/klib.h index b4d26ed..f549a09 100644 --- a/kernel/include/lib/klib.h +++ b/kernel/include/lib/klib.h @@ -263,6 +263,11 @@ enum log_level { __attribute__((noreturn, format(printf, 3, 4))) void __panic(unsigned int line, const char *file, const char *format, ...); +/** + * Loop kernel in fatal notifier (beep and blink caps lock key) + */ +__attribute__((noreturn)) void fatal_loop(void); + /** * Fill dst with a stack trace consisting of return addresses in order * from top to bottom diff --git a/kernel/lib/panic.c b/kernel/lib/panic.c index 3a6e029..35ea0b1 100644 --- a/kernel/lib/panic.c +++ b/kernel/lib/panic.c @@ -1,6 +1,8 @@ #include #include #include +#include +#include __attribute__((noreturn)) void __panic(unsigned int line, const char *file, const char *format, ...) @@ -16,6 +18,17 @@ __attribute__((noreturn)) void __panic(unsigned int line, const char *file, log_backtrace(); #endif - while (1) - halt(); + fatal_loop(); +} + +__attribute__((noreturn)) void fatal_loop(void) +{ + while(1) { + spkr_play_tone(1000); + ps2_set_leds(0x4); + kspin_milliseconds(200); + spkr_quiet(); + ps2_set_leds(0x0); + kspin_milliseconds(800); + } } -- cgit v1.2.3-freya From f485b63e72cc5bf29f134c3e033a21d4a52244d3 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Tue, 6 May 2025 12:33:57 -0400 Subject: update fs --- kernel/fs/tar.c | 4 ++-- kernel/include/comus/fs.h | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'kernel/include') diff --git a/kernel/fs/tar.c b/kernel/fs/tar.c index 061dd0c..8beebc6 100644 --- a/kernel/fs/tar.c +++ b/kernel/fs/tar.c @@ -113,7 +113,7 @@ static int tar_locate(struct file_system *fs, const char *path, return 1; } -int tar_read(struct file *in, char *buf, size_t len) +int tar_read(struct file *in, void *buf, size_t len) { struct tar_file *file = (struct tar_file *)in; size_t max_bytes = file->len - file->offset; @@ -135,7 +135,7 @@ int tar_read(struct file *in, char *buf, size_t len) return bytes; } -int tar_write(struct file *in, const char *buf, size_t len) +int tar_write(struct file *in, const void *buf, size_t len) { (void)in; (void)buf; diff --git a/kernel/include/comus/fs.h b/kernel/include/comus/fs.h index 044330b..b1d0e80 100644 --- a/kernel/include/comus/fs.h +++ b/kernel/include/comus/fs.h @@ -28,7 +28,7 @@ struct disk { /// internal disk device union { struct { - char *start; + uint8_t *start; size_t len; } rd; ide_device_t ide; @@ -137,9 +137,9 @@ struct file { /// file type enum file_type f_type; /// read from the file - int (*read)(struct file *file, char *buffer, size_t nbytes); + int (*read)(struct file *file, void *buffer, size_t nbytes); /// write into the file - int (*write)(struct file *file, const char *buffer, size_t nbytes); + int (*write)(struct file *file, const void *buffer, size_t nbytes); /// seeks the file int (*seek)(struct file *file, long int offset, int whence); /// get directory entry at index -- cgit v1.2.3-freya From 40bfcbb2cf8a39e58ec19b21956caaf4685c9b8b Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Tue, 6 May 2025 12:34:48 -0400 Subject: terrible hack to not overwrite mboot data --- kernel/include/comus/mboot.h | 10 ++++++++++ kernel/mboot/mboot.c | 19 +++++++++++++++++++ kernel/mboot/module.c | 16 +++++++++++++--- kernel/memory/physalloc.c | 24 ++++++++++++++++-------- 4 files changed, 58 insertions(+), 11 deletions(-) (limited to 'kernel/include') diff --git a/kernel/include/comus/mboot.h b/kernel/include/comus/mboot.h index aba3e4e..bed29f1 100644 --- a/kernel/include/comus/mboot.h +++ b/kernel/include/comus/mboot.h @@ -47,4 +47,14 @@ EFI_HANDLE mboot_get_efi_hdl(void); */ void *mboot_get_initrd(size_t *len); +/** + * Returns the physical pointer to the loaded init ram disk with size given by len + */ +void *mboot_get_initrd_phys(size_t *len); + +/** + * Gets the end of the mboot pointer + */ +void *mboot_end(void); + #endif /* mboot.h */ diff --git a/kernel/mboot/mboot.c b/kernel/mboot/mboot.c index e4547e7..8163f04 100644 --- a/kernel/mboot/mboot.c +++ b/kernel/mboot/mboot.c @@ -5,6 +5,8 @@ static volatile void *mboot = NULL; +extern char kernel_end[]; + void mboot_init(long magic, volatile void *ptr) { if (magic != MULTIBOOT2_BOOTLOADER_MAGIC) @@ -12,6 +14,23 @@ void mboot_init(long magic, volatile void *ptr) mboot = ptr; } +void *mboot_end(void) +{ + if (mboot == NULL) + return NULL; + + struct multiboot *info = (struct multiboot *)mboot; + uintptr_t mboot_end, initrd_end; + size_t initrd_len; + + mboot_end = (uintptr_t)info + info->total_size; + initrd_end = (uintptr_t)mboot_get_initrd_phys(&initrd_len); + if (initrd_end) + initrd_end += initrd_len; + + return (void *)MAX(mboot_end, initrd_end); +} + void *locate_mboot_table(uint32_t type) { if (mboot == NULL) diff --git a/kernel/mboot/module.c b/kernel/mboot/module.c index bf15eca..03c5147 100644 --- a/kernel/mboot/module.c +++ b/kernel/mboot/module.c @@ -1,4 +1,4 @@ -#include "comus/memory.h" +#include #include #include "mboot.h" @@ -16,7 +16,7 @@ struct multiboot_tag_module { static void *mapped_addr = NULL; size_t initrd_len; -void *mboot_get_initrd(size_t *len) +void *mboot_get_initrd_phys(size_t *len) { struct multiboot_tag_module *mod; void *tag, *phys; @@ -36,11 +36,21 @@ void *mboot_get_initrd(size_t *len) phys = (void *)(uintptr_t)mod->mod_start; initrd_len = mod->mod_end - mod->mod_start; + *len = initrd_len; + return phys; +} + +void *mboot_get_initrd(size_t *len) +{ + // get phys + void *phys = mboot_get_initrd_phys(len); + if (phys == NULL) + return NULL; + // map addr mapped_addr = kmapaddr(phys, NULL, initrd_len, F_PRESENT | F_WRITEABLE); if (mapped_addr == NULL) return NULL; - *len = initrd_len; return mapped_addr; } diff --git a/kernel/memory/physalloc.c b/kernel/memory/physalloc.c index 47522b8..7551c75 100644 --- a/kernel/memory/physalloc.c +++ b/kernel/memory/physalloc.c @@ -1,12 +1,14 @@ -#include "lib/kio.h" #include #include #include +#include +#include #include "physalloc.h" extern char kernel_start[]; extern char kernel_end[]; +static void *kernel_real_end = NULL; // between memory_start and kernel_start will be the bitmap static uintptr_t memory_start = 0; @@ -30,16 +32,17 @@ static int n_pages(const struct memory_segment *m) return (m->len + PAGE_SIZE - 1) / PAGE_SIZE; } -static void *page_at(int i) +static void *page_at(size_t i) { - int cur_page = 0; + size_t cur_page = 0; + const struct memory_segment *m = page_start; for (uint64_t idx = 0; idx < segment_count; idx++) { - const struct memory_segment *m = page_start; - int pages = n_pages(m); + size_t pages = n_pages(m); if (i - cur_page < pages) { return (void *)(m->addr + (PAGE_SIZE * (i - cur_page))); } cur_page += pages; + m++; } return NULL; } @@ -48,8 +51,8 @@ static long page_idx(void *page) { uintptr_t addr = (uintptr_t)page; int cur_page = 0; + const struct memory_segment *m = page_start; for (uint64_t idx = 0; idx < segment_count; idx++) { - const struct memory_segment *m = page_start; if (addr < m->addr) { return -1; } @@ -57,6 +60,7 @@ static long page_idx(void *page) return cur_page + ((addr - m->addr) / PAGE_SIZE); } cur_page += n_pages(m); + m++; } return -1; } @@ -211,7 +215,7 @@ static struct memory_segment clamp_segment(const struct memory_segment *segment) if (memory_start) start = memory_start; else - start = (uintptr_t)kernel_end; + start = (uintptr_t)kernel_real_end; if (segment->addr < start) { addr = start; @@ -243,6 +247,10 @@ void physalloc_init(struct memory_map *map) segment_count = 0; + kernel_real_end = mboot_end(); + if ((char *)kernel_real_end < kernel_end) + kernel_real_end = kernel_end; + for (uint32_t i = 0; i < map->entry_count; i++) { struct memory_segment *segment = &map->entries[i]; @@ -256,7 +264,7 @@ void physalloc_init(struct memory_map *map) long bitmap_pages = (page_count / 64 / PAGE_SIZE) + 1; long bitmap_size = bitmap_pages * PAGE_SIZE; - bitmap = (uint64_t *)page_align((uintptr_t)kernel_end); + bitmap = (uint64_t *)page_align((uintptr_t)kernel_real_end); long page_area_size = segment_count * sizeof(struct memory_segment); char *page_area_addr = (char *)bitmap + bitmap_size; -- cgit v1.2.3-freya From 9405e9870dcac253d66d5008260257e2230320c9 Mon Sep 17 00:00:00 2001 From: Galen Sagarin Date: Tue, 6 May 2025 13:30:06 -0400 Subject: tarfs.c, as well as an incomplete ramfs.c: --- kernel/fs/ramfs.c | 171 ++++++++++++++++ kernel/fs/tar.c | 459 +++++++++++++++++++++++-------------------- kernel/include/comus/ramfs.h | 40 ++++ kernel/include/comus/tar.h | 6 + 4 files changed, 459 insertions(+), 217 deletions(-) create mode 100644 kernel/fs/ramfs.c create mode 100644 kernel/include/comus/ramfs.h create mode 100644 kernel/include/comus/tar.h (limited to 'kernel/include') diff --git a/kernel/fs/ramfs.c b/kernel/fs/ramfs.c new file mode 100644 index 0000000..0ed4018 --- /dev/null +++ b/kernel/fs/ramfs.c @@ -0,0 +1,171 @@ +#include +#include +//#include +//#include +#include + +#define NOERROR 0 +#define ERROR 1 + +int ramfs_check_exists(const char *name) { + for(int i = 0; i < numberOfFiles; i++) { + if(memcmp(allTheFiles[i].name, name, strlen(allTheFiles[i].name)) == 0) { + // found it + return i; + } + } + // doesn't exist + return -1; +} +int ramfs_create(const char *name) { + if(ramfs_check_exists(name) != -1) { + // already exists + return ERROR; + } + struct file *newFile; + *newFile->name = name; + newFile->size = 0; + newFile->data = kalloc(MAX_FILE_SIZE); + newFile = &allTheFiles[numberOfFiles]; + +} + +int ramfs_delete(const char *name) { + int i = ramfs_check_exists(name); + if(i >= 0) { + kfree(allTheFiles[i].data); + for(int j = i; j < numberOfFiles; j++) { + allTheFiles[j] = allTheFiles[j+1]; + numberOfFiles -= 1; + } + return NOERROR; + + } + return ERROR; +} + +int ramfs_read(const char *name, char *buffer, size_t len) { + int i = ramfs_check_exists(name); + if(i >= 0) { + memcpy(buffer, allTheFiles[i].data, len); + return NOERROR; + } + return ERROR; +} + + +int ramfs_write(const char *name, char *buffer) { + int i = ramfs_check_exists(name); + if(i >= 0) { + memcpy(buffer) + } +} + +// here we return the index of the file as well. +/*int ramfs_find_file(root *r, const char *fullpath, const char *name, file *out, directory *outDirectory) { + directory *location = r->root; + if(ramfs_find_directory(r, fullpath, name, location) == NOERROR) { + for(int i = 0; i < location->file_count; i++) { + if(strcmp(location->files[i]->name, name) == 0) { + outDirectory = location; + out = location->files[i]; + return i; + } + } + + } + return -1; +} +int ramfs_find_directory(root *r, const char *fullpath, const char *name, directory *out) { + directory *location = r->root; + char *path = fullpath; + char *tempPath = strtok(tempPath, "/"); + while(tempPath != NULL) { + bool wasItFound = false; + + for(int i = 0; i < location->directory_count; i++) { + if(strcmp(location->directories[i]->name, tempPath) == 0) { + // yay we found it; + location = location->directories[i]; + wasItFound = true; + break; + + } + } + if(!wasItFound) { + return ERROR; + } + tempPath = strtok(NULL, "/"); + + } + out = location; + return NOERROR; + +} + +int ramfs_create_file(root *r, const char *fullpath, const char *name) { + // trace through the path + // struct ramfs_directory *location = r->root; + directory *location = r->root; + + if(ramfs_find_directory(r, fullpath, name, location) == NOERROR) { + if(location->file_count >= MAX_FILES) { + return ERROR; + } + file *newFile = malloc(sizeof(file)); + strcpy(newFile->name, name); + newFile->size = 0; + newFile->data = malloc(MAX_FILE_SIZE); + location->files[location->file_count] = newFile; + location->file_count += 1; + + return NOERROR; + + } + return ERROR; +} + +int ramfs_delete_file(root *r, const char *fullpath, const char *name) { + directory *location; + if(ramfs_find_directory(r, fullpath, name, location) == NOERROR) { + file *find_file; + int file_index = ramfs_find_file(r, fullpath, name, find_file, location) >= 0; + if(file_index >= 0) { + free(location->files[file_index]->data); + free(location->files[file_index]); + for(int i = file_index + 1; i < location->file_count; i++) { + location->files[i-1] = location->files[i]; + //memcpy(location->files[i-1], location->files[i], location->files[i]->size); + //free(location->files[i]); + } + location->file_count -= 1; + return NOERROR; + } + return ERROR; + + } + return ERROR; +} + +int ramfs_create_directory() { + +} + +int ramfs_delete_directory() { +} + +int ramfs_write() { + +} + + +root *ramfs_init() { + root *r = malloc(sizeof(root)); + r->root = malloc(sizeof(directory)); + strcpy(r->root->name, "/"); + r->root->file_count = 0; + r->root->directory_count = 0; + return r; +} + +*/ diff --git a/kernel/fs/tar.c b/kernel/fs/tar.c index 8beebc6..b844ed2 100644 --- a/kernel/fs/tar.c +++ b/kernel/fs/tar.c @@ -1,188 +1,222 @@ -#include "lib/kio.h" #include #include - -struct tar_hdr { - char name[100]; - char mode[8]; - char uid[8]; - char gid[8]; - char size[12]; - char mtime[12]; - char chksum[8]; - char typeflag; - char linkname[100]; - char magic[6]; - char version[2]; - char uname[32]; - char gname[32]; - char devmajor[8]; - char devminor[8]; - char prefix[155]; - char unused[12]; +#include + +// the placements of these values mimics their placement in standard UStar +struct tar_header { + char name[100]; // 0-99 + char mode[8]; // 100-107 + char ownerUID[8]; // 108-115 + char groupUID[8]; // 116-123 + char fileSize[12]; // 124-135 + char lastMod[12]; // 136-147 + char checksum[8]; // 148-155 + char type_flag; // 156 + char linked_name[100]; // 157-256 + char magic[6]; // 257-262 + char version[2]; // 263-264 + char username[32]; // 265-296 + char groupname[32]; // 297-328 + char majorNum[8]; // 329-336 + char minorNum[8]; // 337-344 + char prefix[155]; // 345-499 + char notUsed[12]; // 500-511 }; - -struct tar_file { - struct file file; - struct file_system *fs; - size_t len; - size_t offset; - size_t sect; -}; - -#define TAR_SECT_SIZE 512 - -#define TMAGIC "ustar" -#define TMAGLEN 6 -#define TVERSION "00" +#define TAR_SIZE 512 +#define TMAGIC "ustar" /* ustar and a null */ +#define TMAGLEN 6 +#define TVERSION "00" /* 00 and no null */ #define TVERSLEN 2 +#define ERROR_TAR 1 +#define NOERROR_TAR 0 -#define REGTYPE '0' -#define DIRTYPE '5' +#define REGTYPE '0' /* regular file */ +#define DIRTYPE '5' /* directory */ -static int read_tar_hdr(struct disk *disk, uint32_t sect, struct tar_hdr *hdr) -{ - if (disk_read(disk, sect * TAR_SECT_SIZE, TAR_SECT_SIZE, hdr) < - TAR_SECT_SIZE) - return 1; - - // check magic - if (memcmp(hdr->magic, TMAGIC, TMAGLEN) != 0) - return 1; - // check version - if (memcmp(hdr->version, TVERSION, TVERSLEN) != 0) - return 1; - - return 0; +struct tar_file { + struct file file; + struct file_system *fs; + size_t len; + size_t offset; + size_t sect; +}; +// read_tar_header reads what is in tar +int read_tar_header(struct disk *disk, uint32_t sect, struct tar_header *hdr) { + + if(disk_read(disk, sect * TAR_SIZE, TAR_SIZE, hdr) < TAR_SIZE) { + return ERROR_TAR; + } + if(memcmp(hdr->magic, TMAGIC, TMAGLEN) != 0 || memcmp(hdr->version, TVERSION, TVERSLEN) != 0) { + return ERROR_TAR; + } + return NOERROR_TAR; + } -static int tar_to_fs_type(char typeflag) -{ - switch (typeflag) { - case REGTYPE: - return F_REG; - case DIRTYPE: - return F_DIR; - default: - return -1; - } +/// @brief +/// @param f +/// @param buffer +/// @param len +/// @return +int tar_read(struct file *f, char *buffer, size_t len) { + struct tar_file *tf = (struct tar_file*) f; + long size = MIN((tf->len - tf->offset), len); + if(tf->file.f_type != F_REG || size < 1) { + return ERROR_TAR; + } + size = disk_read(tf->fs->fs_disk, tf->sect+1 * TAR_SIZE + tf->offset, size, buffer); + tf->offset += size; + return size; } - -static int tar_locate(struct file_system *fs, const char *path, - struct tar_hdr *out_hdr, size_t *out_sect, - size_t *in_sect, bool partial) -{ - struct tar_hdr hdr; - size_t sect = 0; - - if (in_sect != NULL) - sect = *in_sect; - - while (1) { - size_t filesize, sects; - int cmp; - - if (read_tar_hdr(fs->fs_disk, sect, &hdr)) - return 1; - - filesize = strtoull(hdr.size, NULL, 8); - sects = (filesize + TAR_SECT_SIZE - 1) / TAR_SECT_SIZE; - - if (partial) { - size_t len = MIN(strlen(path), strlen(hdr.name)); - cmp = memcmp(hdr.name, path, len); - } else { - cmp = memcmp(hdr.name, path, strlen(path) + 1); - } - - if (cmp) { - // not our file, goto next - sect += sects + 1; - continue; - } - - // we found our file! - *out_hdr = hdr; - *out_sect = sect; - if (in_sect != NULL) - *in_sect = sect + sects + 1; - return 0; - } - - return 1; +//static int read_tar() +// we are assuming that things are formatted correctly. +int find_file(struct file_system *fs, const char *filepath, size_t *sect, size_t *sect_return, struct tar_header *outHeader) { + //char *tempFilePath = filepath; + struct tar_header hdr; + size_t curr_sect; + if(sect == NULL) { + curr_sect = 0; + } else { + curr_sect = *sect; + } + while (1 == 1) { + if(read_tar_header(fs->fs_disk, curr_sect, &hdr) != 0) { + return ERROR_TAR; + } + if(memcmp(hdr.name, filepath, strlen(filepath) + 1) != 0) { + // didn't find it. + + curr_sect += ((strtoull(hdr.fileSize, NULL, 8) + TAR_SIZE - 1)/TAR_SIZE) + 1; + continue; + } else { + *outHeader = hdr; + *sect_return = curr_sect; + if(sect != NULL) { + sect += curr_sect; + } + return NOERROR_TAR; + } + + } + return ERROR_TAR; } +int find_file_redux(struct file_system *fs, const char *filepath, size_t *sect, size_t *sect_return, struct tar_header *outHeader) { + struct tar_header hdr; + size_t curr_sect; + if(sect == NULL) { + curr_sect = 0; + } else { + curr_sect = *sect; + } + while (1 == 1) { + if(read_tar_header(fs->fs_disk, curr_sect, &hdr) != 0) { + return ERROR_TAR; + } + if(memcmp(hdr.name, filepath, MIN(strlen(filepath), strlen(hdr.name))) != 0) { + // didn't find it. + + curr_sect += ((strtoull(hdr.fileSize, NULL, 8) + TAR_SIZE - 1)/TAR_SIZE) + 1; + continue; + } else { + *outHeader = hdr; + *sect_return = curr_sect; + if(sect != NULL) { + sect += curr_sect + ((strtoull(hdr.fileSize, NULL, 8) + TAR_SIZE - 1)/TAR_SIZE) + 1; + } + return NOERROR_TAR; + + } + + } + return ERROR_TAR; // it should never actually reach here. -int tar_read(struct file *in, void *buf, size_t len) -{ - struct tar_file *file = (struct tar_file *)in; - size_t max_bytes = file->len - file->offset; - long bytes = MIN(max_bytes, len); - - if (file->file.f_type != F_REG) - return 1; - - if (bytes < 1) - return 0; +} - bytes = disk_read(file->fs->fs_disk, - (file->sect + 1) * TAR_SECT_SIZE + file->offset, bytes, - buf); - if (bytes < 0) - return bytes; // return err code - file->offset += bytes; - return bytes; +void tar_close(struct file *f) { + kfree(f); } -int tar_write(struct file *in, const void *buf, size_t len) -{ - (void)in; - (void)buf; - (void)len; +int tar_seek(struct file *f, int offsetAdd, int theSeek) { + struct tar_file *tf = (struct tar_file*) f; + if(theSeek == SEEK_SET) { + tf->offset = offsetAdd; + } else if(theSeek == SEEK_CUR) { + tf->offset = tf->offset + offsetAdd; + } else if(theSeek ==SEEK_END) { + tf->offset = tf->len + offsetAdd; + } else { + return ERROR_TAR; + } + return NOERROR_TAR; + - // cannot write to tar balls - return -1; } -int tar_seek(struct file *in, long int off, int whence) -{ - struct tar_file *file = (struct tar_file *)in; - switch (whence) { - case SEEK_SET: - file->offset = off; - return file->offset; - case SEEK_CUR: - file->offset += off; - return file->offset; - case SEEK_END: - file->offset = file->len + off; - return file->offset; - default: - return -1; - } +int tar_write(struct file *f, const char *buffer, size_t len) { + // tar doesn't do write lol. This is just here so there is something to send to write + (void)f; + (void)buffer; + (void)len; + return ERROR_TAR; } -int tar_ents(struct file *in, struct dirent *ent, size_t entry) +/*int tar_ents(struct file *f, struct dirent *d, size_t dIndex) { + struct tar_header hdr; + struct tar_header dir; + struct tar_file *tf = (struct tar_file*) f; + size_t sect = 0; + size_t sect_off = 0; + size_t curr_index = 0; + if(read_tar_header(tf->fs->fs_disk, sect, &hdr) != 0) { + return ERROR_TAR; + } + while(1 == 1) { + if(find_file_redux(tf->fs, dir.name, §_off, §, &dir) != 0) { + return ERROR_TAR; + } + if(curr_index < dIndex) { + curr_index += 1; + } else if(curr_index > dIndex) { + // something went seriously wrong + return ERROR_TAR; + } else { + d->d_namelen = strlen(hdr.name); + dir.type_flag = DIRTYPE; + d->d_offset = dIndex; + memcpy(d->d_name, hdr.name, d->d_namelen + 1); + return NOERROR_TAR; + } + + + } + + +}*/ + + +int tar_ents(struct file *f, struct dirent *ent, size_t entry) { - struct tar_file *file; - struct tar_hdr dir, hdr; + struct tar_file *tf; + struct tar_header dir, hdr; size_t sect; size_t sect_off = 0; size_t idx = 0; - file = (struct tar_file *)in; + tf = (struct tar_file *)f; sect = 0; - if (file->file.f_type != F_DIR) + if (tf->file.f_type != F_DIR) return -1; - if (read_tar_hdr(file->fs->fs_disk, sect, &dir)) - return 1; + if (read_tar_header(tf->fs->fs_disk, sect, &dir)) { + return ERROR_TAR; + } while (1) { - if (tar_locate(file->fs, dir.name, &hdr, §, §_off, true)) - return 1; + if (find_file_redux(tf->fs, dir.name, §_off, §, &hdr)) + return ERROR_TAR; if (idx != entry) { idx++; @@ -192,77 +226,68 @@ int tar_ents(struct file *in, struct dirent *ent, size_t entry) ent->d_offset = entry; ent->d_namelen = strlen(hdr.name); memcpy(ent->d_name, hdr.name, ent->d_namelen + 1); - return 0; + return NOERROR_TAR; } - return 1; + return ERROR_TAR; } -void tar_close(struct file *file) -{ - kfree(file); +// opens up the tar file at fullpath, and puts it in out. +int tar_open(struct file_system *fs, const char *fullpath, struct file **out) { + struct tar_header hdr; + struct tar_file *newFile; + size_t sect_result; + find_file(fs, fullpath, NULL, §_result, &hdr); + newFile = kalloc(sizeof(struct tar_file)); + //newFile->file = idk; + newFile->file.f_type = F_REG; + newFile->fs = fs; + newFile->file.read = tar_read; + newFile->file.close = tar_close; + newFile->file.write = tar_write; // doesn't actually work; + newFile->file.ents = tar_ents; + newFile->file.seek = tar_seek; + newFile->offset = 0; + newFile->len = strtoull(hdr.fileSize, NULL, 8); + newFile->sect = sect_result; + + *out = (struct file *)newFile; + return NOERROR_TAR; + + + + } -int tar_open(struct file_system *fs, const char *path, int flags, - struct file **out) -{ - struct tar_file *file; - struct tar_hdr hdr; - size_t sect; - - // cannot create or write files - if (flags != O_RDONLY) - return 1; - - if (tar_locate(fs, path, &hdr, §, NULL, false)) - return 1; - - file = kalloc(sizeof(struct tar_file)); - if (file == NULL) - return 1; - - file->file.f_type = tar_to_fs_type(hdr.typeflag); - file->file.read = tar_read; - file->file.write = tar_write; - file->file.seek = tar_seek; - file->file.ents = tar_ents; - file->file.close = tar_close; - file->fs = fs; - file->len = strtoull(hdr.size, NULL, 8); - file->offset = 0; - file->sect = sect; - *out = (struct file *)file; - - return 0; +// gets stats at the file at fullpath, putting them in out. +int tar_stat(struct file_system *fs, const char *fullpath, struct stat *out) { + struct tar_header hdr; + size_t sect_result; + find_file(fs, fullpath, NULL, §_result, &hdr); + out->s_length = strtoull(hdr.fileSize, NULL, 8); + if(hdr.type_flag == REGTYPE) { + out->s_type = F_REG; + } else if(hdr.type_flag == DIRTYPE) { + out->s_type = F_DIR; + } else { + // wth + return ERROR_TAR; + } + return NOERROR_TAR; } -int tar_stat(struct file_system *fs, const char *path, struct stat *stat) -{ - struct tar_hdr hdr; - size_t sect; - - if (tar_locate(fs, path, &hdr, §, NULL, false)) - return 1; - - stat->s_length = strtoull(hdr.size, NULL, 8); - stat->s_type = tar_to_fs_type(hdr.typeflag); - - return 0; +// use tar for the filesystem. +int tar_mount(struct file_system *fs) { + struct tar_header hdr; + // reads into hdr + if(read_tar_header(fs->fs_disk, 0, &hdr) == 0) { + fs->fs_name = "tar"; + fs->open = tar_open; + fs->stat = tar_stat; + fs->fs_present = true; + return NOERROR_TAR; + } + return ERROR_TAR; } -int tar_mount(struct file_system *fs) -{ - struct tar_hdr hdr; - - // if first tar hdr is valid, assume valid tarball - if (read_tar_hdr(fs->fs_disk, 0, &hdr)) - return 1; - - fs->fs_present = true; - fs->fs_name = "tar"; - fs->open = tar_open; - fs->stat = tar_stat; - - INFO("loaded tarfs on disk %d", fs->fs_disk->d_id); - return 0; -} +// no need to unmount, since mount just overrides whatever it was using previously. \ No newline at end of file diff --git a/kernel/include/comus/ramfs.h b/kernel/include/comus/ramfs.h new file mode 100644 index 0000000..33644d6 --- /dev/null +++ b/kernel/include/comus/ramfs.h @@ -0,0 +1,40 @@ +#include +#ifndef RAMFS_H_ +#define RAMFS_H_ +#define MAX_FILES 32 +#define MAX_FILE_SIZE 4096 + +struct file { + char name[32]; + size_t size; + char *data; +} file; + +struct file allTheFiles[MAX_FILES]; +int numberOfFiles = 0; + +#endif +/* +typedef struct ramfs_file { + char name[32]; + int size; + char *data; +} file; + +typedef struct ramfs_directory { + char name[32]; + file *files[MAX_FILES]; + directory *directories[MAX_FILES]; + int file_count; + int directory_count; +} directory; + +typedef struct ramfs_root { + directory *root; +} root; + +//struct file allTheFiles[MAX_FILES]; +int numberOfFiles = 0; + + +#endif*/ \ No newline at end of file diff --git a/kernel/include/comus/tar.h b/kernel/include/comus/tar.h new file mode 100644 index 0000000..b08f668 --- /dev/null +++ b/kernel/include/comus/tar.h @@ -0,0 +1,6 @@ +#include +#ifndef TAR_FS_H_ +#define TAR_FS_H_ + +int tar_mount(struct file_system *fs); +#endif \ No newline at end of file -- cgit v1.2.3-freya From b9ba7d2f4da63442b93fa2f9d36978c19b45ba11 Mon Sep 17 00:00:00 2001 From: Galen Sagarin Date: Tue, 6 May 2025 13:41:58 -0400 Subject: Commenting out ramfs things because they aren't working --- kernel/fs/ramfs.c | 2 +- kernel/include/comus/ramfs.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'kernel/include') diff --git a/kernel/fs/ramfs.c b/kernel/fs/ramfs.c index 0ed4018..e9d5bc2 100644 --- a/kernel/fs/ramfs.c +++ b/kernel/fs/ramfs.c @@ -1,4 +1,4 @@ -#include +/*#include #include //#include //#include diff --git a/kernel/include/comus/ramfs.h b/kernel/include/comus/ramfs.h index 33644d6..d3cd703 100644 --- a/kernel/include/comus/ramfs.h +++ b/kernel/include/comus/ramfs.h @@ -1,4 +1,4 @@ -#include +/*#include #ifndef RAMFS_H_ #define RAMFS_H_ #define MAX_FILES 32 -- cgit v1.2.3-freya From 633d89e0cbfea56cb5dc9c2d7fb19857cbe6f49d Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Tue, 6 May 2025 14:06:27 -0400 Subject: increase identity map --- kernel/include/comus/limits.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'kernel/include') diff --git a/kernel/include/comus/limits.h b/kernel/include/comus/limits.h index 1ef13b4..a036fcb 100644 --- a/kernel/include/comus/limits.h +++ b/kernel/include/comus/limits.h @@ -7,7 +7,7 @@ */ /// number of pts to identity map the kernel (1pt = 2MB) -#define N_IDENT_PTS 4 // max 512 (1G) +#define N_IDENT_PTS 64 // max 512 (1G) /// max number of processes #define N_PROCS 256 -- cgit v1.2.3-freya