From 4c2f2fb867cfe64f217a246010a2e2722514dd90 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Tue, 17 Dec 2019 00:43:52 -0500 Subject: a good deal of process --- doc/design/memory_map.txt | 2 ++ src/kernel/files.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/kernel/files.h | 40 ++++++++++++++++++++++++++++++++++++++++ src/kernel/main.c | 11 +++++++++++ src/kernel/mem.c | 41 +++++++++++++++++++++++++++++++++++++++++ src/kernel/mem.h | 29 +++++++++++++++++++++++++++++ src/kernel/proc.c | 43 +++++++++++++++++++++++++++++++++++++++++++ src/kernel/proc.h | 34 ++++++++++++++++++++++++++++++++++ 8 files changed, 244 insertions(+) create mode 100644 src/kernel/files.c create mode 100644 src/kernel/files.h create mode 100644 src/kernel/mem.c create mode 100644 src/kernel/mem.h create mode 100644 src/kernel/proc.c create mode 100644 src/kernel/proc.h diff --git a/doc/design/memory_map.txt b/doc/design/memory_map.txt index d51ebcf..27da346 100644 --- a/doc/design/memory_map.txt +++ b/doc/design/memory_map.txt @@ -4,6 +4,8 @@ 0x00 : fat 0x04 : root directory 0x08 : kernel + 0x0c : file handle array + 0x10 : process array 0x0600 - 0x1fff : not assigned 0x2000 - 0x3fff : memory map for each word : 1 page diff --git a/src/kernel/files.c b/src/kernel/files.c new file mode 100644 index 0000000..c17edd4 --- /dev/null +++ b/src/kernel/files.c @@ -0,0 +1,44 @@ +/* +Copyright 2019 Benji Dial + +Permission to use, copy, modify, and/or distribute this +software for any purpose with or without fee is hereby +granted, provided that the above copyright notice and this +permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS +ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO +EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER +RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE +OF THIS SOFTWARE. +*/ + +#include "files.h" + +uint16_t open_file(uint8_t *name) { + //TODO +} + +void close_file(uint16_t handle) { + FILE_HANDLES[handle].first_sector = 0; +} + +void read_file(uint16_t handle, uint32_t length, void *buffer) { + //TODO +} + +void write_file(uint16_t handle, uint32_t length, void *buffer) { + //TODO +} + +void seek_file(uint16_t handle, int32_t by) { + //TODO +} + +uint32_t get_size(uint16_t handle) { + //TODO +} \ No newline at end of file diff --git a/src/kernel/files.h b/src/kernel/files.h new file mode 100644 index 0000000..31ad21f --- /dev/null +++ b/src/kernel/files.h @@ -0,0 +1,40 @@ +/* +Copyright 2019 Benji Dial + +Permission to use, copy, modify, and/or distribute this +software for any purpose with or without fee is hereby +granted, provided that the above copyright notice and this +permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS +ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO +EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER +RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE +OF THIS SOFTWARE. +*/ + +#include + +#ifndef FILES_H +#define FILES_H + +struct file_handle_info { + uint16_t first_sector; + uint16_t current_sector; + uint8_t *io_buffer; + uint32_t position; +}; + +#define FILE_HANDLES ((struct file_handle_info *)*(uint32_t *)0x0000050c) +uint16_t open_file(uint8_t *name); +void close_file(uint16_t handle); +void read_file(uint16_t handle, uint32_t length, void *buffer); +void write_file(uint16_t handle, uint32_t length, void *buffer); +void seek_file(uint16_t handle, int32_t by); +uint32_t get_size(uint16_t handle); + +#endif \ No newline at end of file diff --git a/src/kernel/main.c b/src/kernel/main.c index 079779c..d5f463c 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -18,8 +18,19 @@ OF THIS SOFTWARE. */ #include "vga.h" +#include "files.h" +#include "mem.h" +#include "proc.h" void main(void) { + *(uint32_t *)0x0000050c = (uint32_t)allocate_pages(16 * sizeof(struct file_handle_info), 2); + for (struct file_handle_info *i = FILE_HANDLES, *l = FILE_HANDLES + 65536; i < l; ++i) + i->first_sector = 0; + + *(uint32_t *)0x00000510 = (uint32_t)allocate_pages(16 * sizeof(struct proc_info), 2); + for (struct proc_info *i = FILE_HANDLES, *l = FILE_HANDLES + 65536; i < l; ++i) + i->memory_start = 0; + put_sz("Welcome to Portland version 0.0.8!\n"); while (1) put_sz("|\b/\b-\b\\\b"); diff --git a/src/kernel/mem.c b/src/kernel/mem.c new file mode 100644 index 0000000..f12fbf7 --- /dev/null +++ b/src/kernel/mem.c @@ -0,0 +1,41 @@ +/* +Copyright 2019 Benji Dial + +Permission to use, copy, modify, and/or distribute this +software for any purpose with or without fee is hereby +granted, provided that the above copyright notice and this +permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS +ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO +EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER +RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE +OF THIS SOFTWARE. +*/ + +#include "mem.h" + +void *allocate_pages(uint16_t n_pages, uint16_t proc_n) { + for (uint16_t *i = MMAP, *l = MMAP + 0x1000 - proc_n; ++i; i <= l) + outer_for: + if (!*i) { + for (uint16_t j = 1; j < n_pages; ++j) + if (i[j]) { + i += j + 1; + goto outer_for; + } + for (uint16_t j = 0; j < n_pages; ++j) + i[j] = proc_n; + return (void *)((i - MMAP) * 0x1000); + } + return (void *)0; +} + +void deallocate_pages(void *from, uint16_t n_pages) { + for (uint16_t *mb = MMAP + ((uint32_t)from / 0x1000), *l = mb + n_pages; mb < l; ++mb) + *mb = 0; +} \ No newline at end of file diff --git a/src/kernel/mem.h b/src/kernel/mem.h new file mode 100644 index 0000000..2bfb3bf --- /dev/null +++ b/src/kernel/mem.h @@ -0,0 +1,29 @@ +/* +Copyright 2019 Benji Dial + +Permission to use, copy, modify, and/or distribute this +software for any purpose with or without fee is hereby +granted, provided that the above copyright notice and this +permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS +ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO +EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER +RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE +OF THIS SOFTWARE. +*/ + +#include + +#ifndef MEM_H +#define MEM_H + +#define MMAP ((uint16_t *)0x00002000) +void *allocate_pages(uint16_t n_pages, uint16_t proc_n); +void deallocate_pages(void *from, uint16_t n_pages); + +#endif \ No newline at end of file diff --git a/src/kernel/proc.c b/src/kernel/proc.c new file mode 100644 index 0000000..dba6b92 --- /dev/null +++ b/src/kernel/proc.c @@ -0,0 +1,43 @@ +/* +Copyright 2019 Benji Dial + +Permission to use, copy, modify, and/or distribute this +software for any purpose with or without fee is hereby +granted, provided that the above copyright notice and this +permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS +ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO +EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER +RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE +OF THIS SOFTWARE. +*/ + +#include "proc.h" +#include "mem.h" +#include "files.h" + +uint16_t new_proc(uint8_t *file) { + uint16_t handle = open_file(file); + if (handle) + for (uint16_t id = 3; id; ++id) + if (!(PROCS[id].memory_start)) { + uint32_t size = get_size(id); + void *mem = PROCS[id].memory_start = allocate_pages(PROCS[id].n_pages = (size - 1) / 4096 + 1, id); + read_file(handle, size, mem); + close_file(handle); + //Process file header and make new process + return id; + } + close_file(handle); + return 0; +} + +void end_proc(uint16_t id) { + deallocate_pages(PROCS[id].memory_start, PROCS[id].n_pages); + PROCS[id].memory_start = 0; +} \ No newline at end of file diff --git a/src/kernel/proc.h b/src/kernel/proc.h new file mode 100644 index 0000000..893b84e --- /dev/null +++ b/src/kernel/proc.h @@ -0,0 +1,34 @@ +/* +Copyright 2019 Benji Dial + +Permission to use, copy, modify, and/or distribute this +software for any purpose with or without fee is hereby +granted, provided that the above copyright notice and this +permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS +ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO +EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER +RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE +OF THIS SOFTWARE. +*/ + +#include + +#ifndef PROC_H +#define PROC_H + +struct proc_info { + void *memory_start; + uint16_t n_pages; +}; + +#define PROCS ((struct proc_info *)*(uint32_t *)0x00000510) +uint16_t new_proc(uint8_t *file); +void end_proc(uint16_t id); + +#endif \ No newline at end of file -- cgit v1.2.3