From 69c3c8a84724a00a387360fe89ce10131711bb87 Mon Sep 17 00:00:00 2001 From: Nathan Giddings Date: Fri, 11 Dec 2020 09:19:29 -0600 Subject: [PATCH] More work on Kernel class, syscalls --- src/allocator.cpp | 10 ++--- src/allocator.hpp | 2 +- src/elf.cpp | 10 ++--- src/elf.hpp | 2 +- src/interrupts.hpp | 2 +- src/kernel.cpp | 31 ++++++++++++++ src/kernel.hpp | 68 +++++++++++++++++++----------- src/kernelstate.cpp | 2 +- src/kernelstate.hpp | 2 +- src/map.hpp | 95 ++++++++++++++++++++++++++++++++++++++++++ src/memoryblock.hpp | 34 +++++++++++++++ src/memorymap.cpp | 2 +- src/memorymap.hpp | 2 +- src/mmap.hpp | 2 +- src/module.cpp | 2 +- src/module.hpp | 2 +- src/pageallocator.cpp | 42 +++++++++---------- src/pageallocator.hpp | 2 +- src/process.hpp | 13 ++++-- src/scheduler.cpp | 12 +++--- src/scheduler.hpp | 2 +- src/syscalls.cpp | 62 +++++++++++++++++++++++++++ src/syscalls.hpp | 23 ++++++++++ src/systeminfo.cpp | 2 +- src/systeminfo.hpp | 2 +- src/util.cpp | 23 +++++----- src/util.hpp | 2 + src/x86/initialize.cpp | 2 +- src/x86/interrupts.cpp | 8 ++-- src/x86/mmap.cpp | 16 +++---- src/x86/multiboot2.cpp | 2 +- src/x86/multiboot2.hpp | 2 +- src/x86/tty.cpp | 26 ++++++------ src/x86/tty.hpp | 2 +- 34 files changed, 390 insertions(+), 121 deletions(-) create mode 100644 src/kernel.cpp create mode 100644 src/map.hpp create mode 100644 src/memoryblock.hpp create mode 100644 src/syscalls.cpp create mode 100644 src/syscalls.hpp diff --git a/src/allocator.cpp b/src/allocator.cpp index 6ecef06..b7c5bbe 100644 --- a/src/allocator.cpp +++ b/src/allocator.cpp @@ -20,12 +20,12 @@ inline size_t ilog2(size_t n) return count - (isPowerOfTwo ? 1 : 0); } -kernel::Allocator::Allocator() +kernelns::Allocator::Allocator() { } -kernel::Allocator::Allocator(void* base, size_t heapSize, size_t blockSize) +kernelns::Allocator::Allocator(void* base, size_t heapSize, size_t blockSize) { this->base = (char*) base; this->heapSize = heapSize; @@ -54,7 +54,7 @@ kernel::Allocator::Allocator(void* base, size_t heapSize, size_t blockSize) } } -void* kernel::Allocator::allocate(size_t size) +void* kernelns::Allocator::allocate(size_t size) { size += blockSize - 1; size -= size % blockSize; @@ -68,7 +68,7 @@ void* kernel::Allocator::allocate(size_t size) return NULL; } -void kernel::Allocator::free(void* location) +void kernelns::Allocator::free(void* location) { size_t offset = (size_t) location - (size_t) base; size_t index = (offset / blockSize) + (1 << treeHeight); @@ -82,7 +82,7 @@ void kernel::Allocator::free(void* location) } } -size_t kernel::Allocator::findFreeBlock(size_t height) +size_t kernelns::Allocator::findFreeBlock(size_t height) { if(height > treeHeight) return 0; diff --git a/src/allocator.hpp b/src/allocator.hpp index bd9dfbf..d910ff8 100644 --- a/src/allocator.hpp +++ b/src/allocator.hpp @@ -3,7 +3,7 @@ #include -namespace kernel +namespace kernelns { class Allocator diff --git a/src/elf.cpp b/src/elf.cpp index e327acf..503da94 100644 --- a/src/elf.cpp +++ b/src/elf.cpp @@ -1,23 +1,23 @@ #include "elf.hpp" #include "util.hpp" -kernel::ELF::ELF() +kernelns::ELF::ELF() { this->m_fileLocation = (void*) NULL; } -kernel::ELF::ELF(void* location) +kernelns::ELF::ELF(void* location) { this->m_fileLocation = location; } -void* kernel::ELF::entry() +void* kernelns::ELF::entry() { Header* fileHeader = (Header*) m_fileLocation; return fileHeader->entry; } -int kernel::ELF::validate() +int kernelns::ELF::validate() { Header* fileHeader = (Header*) m_fileLocation; if(fileHeader->magic != 0x464c457f) @@ -29,7 +29,7 @@ int kernel::ELF::validate() return 0; } -int kernel::ELF::load() +int kernelns::ELF::load() { Header* fileHeader = (Header*) m_fileLocation; ProgramHeader* programHeader = (ProgramHeader*) ((size_t) m_fileLocation + fileHeader->phoffset); diff --git a/src/elf.hpp b/src/elf.hpp index 341296c..be986e6 100644 --- a/src/elf.hpp +++ b/src/elf.hpp @@ -3,7 +3,7 @@ #include "systypes.hpp" -namespace kernel +namespace kernelns { class ELF diff --git a/src/interrupts.hpp b/src/interrupts.hpp index d51d864..f8c99aa 100644 --- a/src/interrupts.hpp +++ b/src/interrupts.hpp @@ -1,7 +1,7 @@ #ifndef INTERRUPTS_H #define INTERRUPTS_H -namespace kernel +namespace kernelns { class Interrupts { diff --git a/src/kernel.cpp b/src/kernel.cpp new file mode 100644 index 0000000..98795ff --- /dev/null +++ b/src/kernel.cpp @@ -0,0 +1,31 @@ +#include "kernel.hpp" + +int Kernel::allocateRegion(void* page, size_t length, int flags) +{ + char* ptr = (char*) page; + for(int i = (int) length; i > 0; i -= mmgr.getPageSize()) + { + physaddr_t frame = pageAllocator.allocate(mmgr.getPageSize()); + if(frame != 0) + mmgr.mapPage(page, reserveFrame(), flags); + else + return -1; + ptr += mmgr.getPageSize(); + } + return 0; +} + +void Kernel::freeRegion(void* page, size_t length) +{ + char* ptr = (char*) page; + for(int i = (int) length; i > 0; i -= mmgr.getPageSize()) + { + pageAllocator.free(mmgr.unmapPage((void*) ptr), mmgr.getPageSize()); + ptr += mmgr.getPageSize(); + } +} + +int Kernel::mapRegion(void* page, physaddr_t frame, size_t length, int flags) +{ + +} \ No newline at end of file diff --git a/src/kernel.hpp b/src/kernel.hpp index f959661..107d9bf 100644 --- a/src/kernel.hpp +++ b/src/kernel.hpp @@ -1,17 +1,24 @@ #ifndef KERNEL_H #define KERNEL_H +#include "pageallocator.hpp" +#include "memorymanager.hpp" +#include "memoryblock.hpp" +#include "process.hpp" #include "systypes.hpp" +using namespace kernelns; + class Kernel { public: - Kernel(); - /** - * Maps a region of pages starting at the virtual address 'page', allocating - * each mapped frame and any necessary page tables. + * @brief Maps a region of pages starting at virtual address 'page' with + * length 'length'. Allocates each mapped frame and any necessary page + * tables. This method does not perform any checks before overwriting page + * tables; it is the responsibility of the caller to ensure that the + * operation is safe to perform. * * @param page The virtual address of the first page to map * @param length The size in bytes of the region to map @@ -22,9 +29,21 @@ public: int allocateRegion(void* page, size_t length, int flags); /** - * Maps a contiguous region of pages to a configuous region of frames, - * allocating new page tables as needed. All pages will share the same - * flags. + * @brief Unmaps and frees a region of mapped pages starting at virtual + * address 'page' with length 'length'. Does not free any page tables + * that are made redundant by this operation. It is the responsibility + * of the caller to ensure that all pages in the specified region are + * mapped and should be returned to the page allocator. + * + * @param page The virtual address of the first page to free + * @param length The size in bytes of the region to free + */ + void freeRegion(void* page, size_t length); + + /** + * @brief Maps a contiguous region of pages to a contiguous region of + * frames. Allocates new page tables as needed. All pages will share + * the same flags. * * @param page The virtual address of the first page to map * @param frame The physical address of the first frame to map to @@ -36,33 +55,32 @@ public: int mapRegion(void* page, physaddr_t frame, size_t length, int flags); /** - * Maps a single page to a single frame. Allocates a new page table if one - * does not exist for that frame. + * @brief Unmaps a region of pages; does not return them to the page + * allocator. * - * @param page the virtual address of the page to map - * @param frame the physical address of the frame to map to - * @param flags flags to apply to the entry - * - * @returns zero upon success, nonzero on failure + * @param page The virtual address of the first page to unmap + * @param length The size in bytes of the region to unmap */ - int mapPage(void* page, physaddr_t frame, int flags); + void unmapRegion(void* page, size_t length); - /** - * Reserve a single page frame. - * - * @returns the physical address of the reserved frame. - */ - physaddr_t reserveFrame(); + unsigned int createSharedBlock(size_t length, int flags); - /** - * Frees a single page frame. - */ - void freeFrame(physaddr_t frame); + void deleteSharedBlock(unsigned int id); + + const MemoryBlock& getSharedBlock(unsigned int id); + + const Process& getActiveProcess(); + + const Process& switchProcess(); private: + PageAllocator& pageAllocator; + MemoryManager& mmgr; }; +extern Kernel kernel; + #endif \ No newline at end of file diff --git a/src/kernelstate.cpp b/src/kernelstate.cpp index 561ba16..89e3301 100644 --- a/src/kernelstate.cpp +++ b/src/kernelstate.cpp @@ -1,6 +1,6 @@ #include "kernelstate.hpp" -using namespace kernel; +using namespace kernelns; BuddyAllocator State::pageAllocator; Allocator State::allocator; diff --git a/src/kernelstate.hpp b/src/kernelstate.hpp index 77a3cb8..ecb8b63 100644 --- a/src/kernelstate.hpp +++ b/src/kernelstate.hpp @@ -7,7 +7,7 @@ #include "scheduler.hpp" #include "systeminfo.hpp" -namespace kernel +namespace kernelns { class State diff --git a/src/map.hpp b/src/map.hpp new file mode 100644 index 0000000..e33ea38 --- /dev/null +++ b/src/map.hpp @@ -0,0 +1,95 @@ +#ifndef MAP_H +#define MAP_H + +template +class Map +{ +public: + + Map(); + + bool contains(const Key& key) const + { + if(m_tree == nullptr) + return false; + else if(m_tree->search(key) == nullptr) + return false; + return true; + } + + Value& get(const Key& key) + { + if(m_tree = nullptr) + return (Value&) *nullptr; + Node* node = m_tree->search(key); + if(node == nullptr) + return (Value&) *nullptr; + else + return node->m_value; + } + + void insert(const Value& value); + + void remove(const Key& key); + + unsigned int size() const; + +private: + + class Node + { + public: + + enum class Color + { + Black, + Red + } + + Key m_key; + + Value& m_value; + + Node *left, *right, *parent; + + Color color; + + Node(); + + Node* uncle() + { + if(parent != nullptr && parent->parent != nullptr) + { + if(parent == parent->parent->left) + return parent->parent->right; + return parent->parent->left; + } + return nullptr; + } + + Node* search(const Key& key) + { + if(key == m_key) + return this; + if(left != nullptr) + { + Node* lsearch = left->search(key); + if(lsearch != nullptr) + return lsearch; + } + if(right != nullptr) + { + Node* rsearch = right->search(key); + if(rsearch != nullptr) + return rsearch; + } + return nullptr; + } + + }; + + Node *m_tree; + +}; + +#endif \ No newline at end of file diff --git a/src/memoryblock.hpp b/src/memoryblock.hpp new file mode 100644 index 0000000..2df0625 --- /dev/null +++ b/src/memoryblock.hpp @@ -0,0 +1,34 @@ +#ifndef MEMORYBLOCK_H +#define MEMORYBLOCK_H + +#include "systypes.hpp" + +class MemoryBlock +{ +public: + + physaddr_t getLocation() const; + + physaddr_t getEnd() const; + + size_t getSize() const; + + int getAttributes() const; + + unsigned int getOwners() const; + + void decrementOwners(); + +private: + + physaddr_t m_location; + + size_t m_size; + + int m_attributes; + + unsigned int m_owners; + +}; + +#endif \ No newline at end of file diff --git a/src/memorymap.cpp b/src/memorymap.cpp index 43959df..d5e58e6 100644 --- a/src/memorymap.cpp +++ b/src/memorymap.cpp @@ -1,6 +1,6 @@ #include "memorymap.hpp" -using namespace kernel; +using namespace kernelns; MemoryMap::Region::Region() { diff --git a/src/memorymap.hpp b/src/memorymap.hpp index ddc2b7a..58aa1d0 100644 --- a/src/memorymap.hpp +++ b/src/memorymap.hpp @@ -3,7 +3,7 @@ #include "systypes.hpp" -namespace kernel +namespace kernelns { class MemoryMap diff --git a/src/mmap.hpp b/src/mmap.hpp index 83f243e..0e91955 100644 --- a/src/mmap.hpp +++ b/src/mmap.hpp @@ -7,7 +7,7 @@ #define MMAP_EXEC 0x02 #define MMAP_SHARED 0x04 -namespace kernel +namespace kernelns { int mmap(void* start, size_t length, int flags); diff --git a/src/module.cpp b/src/module.cpp index 7aae391..08ae855 100644 --- a/src/module.cpp +++ b/src/module.cpp @@ -1,7 +1,7 @@ #include "module.hpp" #include "util.hpp" -using namespace kernel; +using namespace kernelns; Module::Module() { diff --git a/src/module.hpp b/src/module.hpp index 2c4b586..2d3b024 100644 --- a/src/module.hpp +++ b/src/module.hpp @@ -5,7 +5,7 @@ #include "systypes.hpp" -namespace kernel +namespace kernelns { class Module diff --git a/src/pageallocator.cpp b/src/pageallocator.cpp index d31617c..08b6892 100755 --- a/src/pageallocator.cpp +++ b/src/pageallocator.cpp @@ -21,12 +21,12 @@ uint32_t ilog2(uint32_t n, bool roundUp) return count - (isPowerOfTwo ? 1 : (roundUp ? 0 : 1)); } -kernel::BuddyAllocator::BuddyAllocator() +kernelns::BuddyAllocator::BuddyAllocator() { } -kernel::BuddyAllocator::BuddyAllocator(const kernel::MemoryMap& memmap, +kernelns::BuddyAllocator::BuddyAllocator(const kernelns::MemoryMap& memmap, char* bitmap, size_t blockCount, size_t treeHeight) { @@ -44,7 +44,7 @@ kernel::BuddyAllocator::BuddyAllocator(const kernel::MemoryMap& memmap, physaddr_t location = 0x100000; for(size_t i = 0; i < memmap.size() && memmap[i].getSize() > 0; i++) { - if(memmap[i].getType() != kernel::MemoryMap::AVAILABLE) + if(memmap[i].getType() != kernelns::MemoryMap::AVAILABLE) continue; if(memmap[i].getLocation() > location) location = roundUp(memmap[i].getLocation(), 4096); @@ -58,7 +58,7 @@ kernel::BuddyAllocator::BuddyAllocator(const kernel::MemoryMap& memmap, } } -kernel::BuddyAllocator::BuddyAllocator(char* bitmap, size_t blockSize, +kernelns::BuddyAllocator::BuddyAllocator(char* bitmap, size_t blockSize, size_t blockCount, size_t treeHeight) { this->bitmap = bitmap; @@ -77,7 +77,7 @@ kernel::BuddyAllocator::BuddyAllocator(char* bitmap, size_t blockSize, } } -physaddr_t kernel::BuddyAllocator::allocate(size_t size) +physaddr_t kernelns::BuddyAllocator::allocate(size_t size) { size_t height = ilog2(roundUp(size, blockSize) / blockSize, true); if(height > treeHeight) // Requested block size is greater than maximum @@ -99,7 +99,7 @@ physaddr_t kernel::BuddyAllocator::allocate(size_t size) } } -void kernel::BuddyAllocator::free(physaddr_t location, size_t size) +void kernelns::BuddyAllocator::free(physaddr_t location, size_t size) { size_t height = ilog2(roundUp(size, blockSize) / blockSize, true); if(height <= treeHeight) @@ -113,7 +113,7 @@ void kernel::BuddyAllocator::free(physaddr_t location, size_t size) } } -size_t kernel::BuddyAllocator::freeBlocks() const +size_t kernelns::BuddyAllocator::freeBlocks() const { size_t count = 0; for(size_t j = 0; j < blockCount; j++) @@ -126,7 +126,7 @@ size_t kernel::BuddyAllocator::freeBlocks() const return count; } -size_t kernel::BuddyAllocator::maxAllocationSize() const +size_t kernelns::BuddyAllocator::maxAllocationSize() const { for(size_t i = treeHeight; i >= 0; i--) { @@ -141,17 +141,17 @@ size_t kernel::BuddyAllocator::maxAllocationSize() const return 0; } -size_t kernel::BuddyAllocator::getBlockSize() const +size_t kernelns::BuddyAllocator::getBlockSize() const { return blockSize; } -size_t kernel::BuddyAllocator::getMemorySize() const +size_t kernelns::BuddyAllocator::getMemorySize() const { return blockCount; } -size_t kernel::BuddyAllocator::findFreeBlock(size_t height) +size_t kernelns::BuddyAllocator::findFreeBlock(size_t height) { for(size_t i = 0; i < (blockCount >> height); i++) { @@ -171,7 +171,7 @@ size_t kernel::BuddyAllocator::findFreeBlock(size_t height) return INVALID; } -size_t kernel::BuddyAllocator::split(size_t height, size_t index) +size_t kernelns::BuddyAllocator::split(size_t height, size_t index) { if(height > 0 && isFree(height, index)) { @@ -186,7 +186,7 @@ size_t kernel::BuddyAllocator::split(size_t height, size_t index) } } -size_t kernel::BuddyAllocator::merge(size_t height, size_t index) +size_t kernelns::BuddyAllocator::merge(size_t height, size_t index) { if(isFree(height, index) && isFree(height, getBuddy(index)) && height < treeHeight) { @@ -209,34 +209,34 @@ size_t kernel::BuddyAllocator::merge(size_t height, size_t index) } } -size_t kernel::BuddyAllocator::getBuddy(size_t index) +size_t kernelns::BuddyAllocator::getBuddy(size_t index) { return index ^ 1; } -size_t kernel::BuddyAllocator::getParent(size_t index) +size_t kernelns::BuddyAllocator::getParent(size_t index) { return index / 2; } -size_t kernel::BuddyAllocator::getChild(size_t index) +size_t kernelns::BuddyAllocator::getChild(size_t index) { return index * 2; } -physaddr_t kernel::BuddyAllocator::nodeToAddress(size_t height, size_t index) +physaddr_t kernelns::BuddyAllocator::nodeToAddress(size_t height, size_t index) const { return index * (blockSize << height); } -size_t kernel::BuddyAllocator::addressToNode(size_t height, +size_t kernelns::BuddyAllocator::addressToNode(size_t height, physaddr_t location) const { return location / (blockSize << height); } -void kernel::BuddyAllocator::reserveNode(size_t height, size_t index) +void kernelns::BuddyAllocator::reserveNode(size_t height, size_t index) { size_t bit = (height == 0) ? 0 : ((blockCount * 2) - (blockCount >> (height - 1))); @@ -244,7 +244,7 @@ void kernel::BuddyAllocator::reserveNode(size_t height, size_t index) bitmap[bit / 8] |= 1 << (bit % 8); } -void kernel::BuddyAllocator::freeNode(size_t height, size_t index) +void kernelns::BuddyAllocator::freeNode(size_t height, size_t index) { size_t bit = (height == 0) ? 0 : ((blockCount * 2) - (blockCount >> (height - 1))); @@ -252,7 +252,7 @@ void kernel::BuddyAllocator::freeNode(size_t height, size_t index) bitmap[bit / 8] &= ~(1 << (bit % 8)); } -bool kernel::BuddyAllocator::isFree(size_t height, size_t index) const +bool kernelns::BuddyAllocator::isFree(size_t height, size_t index) const { size_t bit = (height == 0) ? 0 : ((blockCount * 2) - (blockCount >> (height - 1))); diff --git a/src/pageallocator.hpp b/src/pageallocator.hpp index 27e2acd..15b1ce5 100755 --- a/src/pageallocator.hpp +++ b/src/pageallocator.hpp @@ -5,7 +5,7 @@ #include "systypes.hpp" #include "memorymap.hpp" -namespace kernel +namespace kernelns { /** diff --git a/src/process.hpp b/src/process.hpp index 73e7a1c..ce363c7 100644 --- a/src/process.hpp +++ b/src/process.hpp @@ -2,20 +2,27 @@ #define PROCESS_H #include +#include "map.hpp" -namespace kernel +namespace kernelns { class Process { public: - Process(); - size_t priority; void* stack; + Process(); + + bool hasSharedBlock(unsigned int blockID) const; + +private: + + Map m_sharedBlocks; + }; } diff --git a/src/scheduler.cpp b/src/scheduler.cpp index c1c00c7..57089a1 100644 --- a/src/scheduler.cpp +++ b/src/scheduler.cpp @@ -1,17 +1,17 @@ #include "scheduler.hpp" -kernel::ProcessQueue::ProcessQueue() +kernelns::ProcessQueue::ProcessQueue() { } -kernel::ProcessQueue::ProcessQueue(Process** array) +kernelns::ProcessQueue::ProcessQueue(Process** array) { m_array = array; m_size = 0; } -kernel::Process* kernel::ProcessQueue::extractMin() +kernelns::Process* kernelns::ProcessQueue::extractMin() { if(m_size == 0) return NULL; @@ -22,7 +22,7 @@ kernel::Process* kernel::ProcessQueue::extractMin() return p; } -void kernel::ProcessQueue::insert(Process* n) +void kernelns::ProcessQueue::insert(Process* n) { size_t i = m_size; m_size++; @@ -33,7 +33,7 @@ void kernel::ProcessQueue::insert(Process* n) m_array[i] = n; } -void kernel::ProcessQueue::remove(Process* n) +void kernelns::ProcessQueue::remove(Process* n) { for(size_t i = 0; i < m_size; i++) { @@ -47,7 +47,7 @@ void kernel::ProcessQueue::remove(Process* n) } } -void kernel::ProcessQueue::heapify(size_t i) +void kernelns::ProcessQueue::heapify(size_t i) { if(i * 2 + 1 >= m_size) return; diff --git a/src/scheduler.hpp b/src/scheduler.hpp index 5e96496..c58ee32 100644 --- a/src/scheduler.hpp +++ b/src/scheduler.hpp @@ -5,7 +5,7 @@ #include "process.hpp" -namespace kernel +namespace kernelns { class ProcessQueue diff --git a/src/syscalls.cpp b/src/syscalls.cpp new file mode 100644 index 0000000..8255d15 --- /dev/null +++ b/src/syscalls.cpp @@ -0,0 +1,62 @@ +#include "syscalls.hpp" +#include "kernel.hpp" + +int mmap(void* location, size_t length, int flags) +{ + // TODO: check that the requested region does not overlap something important + return kernel.allocateRegion(location, length, flags); +} + +int munmap(void* location, size_t length) +{ + // TODO: check that the requested region does not overlap something important + kernel.freeRegion(location, length); + return 0; +} + +unsigned int createSharedBlock(void* location, size_t length, int flags) +{ + unsigned int blockID = kernel.createSharedBlock(length, flags); + if(blockID > 0) + { + const MemoryBlock& block = kernel.getSharedBlock(blockID); + kernel.mapRegion(location, block.getLocation(), length, flags); + // TODO: add block to current process + // TODO: perform safety checks + } + return blockID; +} + +int aquireSharedBlock(void* location, unsigned int id) +{ + const MemoryBlock& block = kernel.getSharedBlock(id); + kernel.mapRegion(location, block.getLocation(), block.getSize(), block.getAttributes()); + // TODO: (somehow) handle invalid ids -- possibly hard while using references + // TODO: add block to current process + // TODO: perform safety checks + return 0; +} + +int releaseSharedBlock(int id) +{ + // (0) Check that process actually posesses block + // (1) Get virtual address of block + // (2) Unmap block + // (3) Delete block if no one posesses it anymore + return 0; +} + +int querySharedBlock(void* info, int id) +{ + // TODO: define struct for block info +} + +int aquirePhysicalBlock(void* location, physaddr_t physicalAddress, size_t length) +{ + return 0; +} + +int releasePhysicalBlock(int id) +{ + return 0; +} \ No newline at end of file diff --git a/src/syscalls.hpp b/src/syscalls.hpp new file mode 100644 index 0000000..3971369 --- /dev/null +++ b/src/syscalls.hpp @@ -0,0 +1,23 @@ +#ifndef SYSCALLS_H +#define SYSCALLS_H + +#include +#include "systypes.hpp" + +int mmap(void* location, size_t length, int flags); + +int munmap(void* location, size_t length); + +unsigned int createSharedBlock(void* location, size_t length, int flags); + +int aquireSharedBlock(void* location, unsigned int id); + +int releaseSharedBlock(int id); + +int querySharedBlock(void* info, int id); + +int aquirePhysicalBlock(void* location, physaddr_t physicalAddress, size_t length); + +int releasePhysicalBlock(int id); + +#endif \ No newline at end of file diff --git a/src/systeminfo.cpp b/src/systeminfo.cpp index 1151de1..a7ceb84 100644 --- a/src/systeminfo.cpp +++ b/src/systeminfo.cpp @@ -1,7 +1,7 @@ #include "systeminfo.hpp" #include "util.hpp" -using namespace kernel; +using namespace kernelns; SystemInfo::SystemInfo() { diff --git a/src/systeminfo.hpp b/src/systeminfo.hpp index 81727cd..caff548 100644 --- a/src/systeminfo.hpp +++ b/src/systeminfo.hpp @@ -6,7 +6,7 @@ #include "systypes.hpp" #include "memorymap.hpp" -namespace kernel +namespace kernelns { class SystemInfo diff --git a/src/util.cpp b/src/util.cpp index 284251b..89c2958 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -17,17 +17,6 @@ void* memcpy(void* destination, const void* source, size_t num) return destination; } -/* - * There are four distinct cases here: - * 1. destination and source blocks do not overlap - * 2. destination and source are the same - * 3. destination and source do overlap; destination starts before source - * 4. destination and source do overlap; destination starts after source - * - * Memcpy results in expected behavior in all cases except for case 4. In that - * case, copying must be done backwards to avoid reading from bytes that have - * already been overwritten. - */ void* memmove(void* destination, const void* source, size_t num) { if(num > 0) @@ -123,7 +112,7 @@ char* strcpy(char* destination, const char* source) void* malloc(size_t size) { - return kernel::State::allocator.allocate(size); + return kernelns::State::allocator.allocate(size); } void* calloc(size_t count, size_t size) @@ -131,9 +120,17 @@ void* calloc(size_t count, size_t size) return memset(malloc(count * size), 0, count * size); } +void* realloc(void* ptr, size_t size) +{ + void* n = kernelns::State::allocator.allocate(size); + memmove(n, ptr, size); + free(ptr); + return n; +} + void free(void* p) { - kernel::State::allocator.free(p); + kernelns::State::allocator.free(p); } void __cxa_pure_virtual() diff --git a/src/util.hpp b/src/util.hpp index 608f8e0..d82bcf5 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -19,6 +19,8 @@ extern "C" void* malloc(size_t size); extern "C" void* calloc(size_t count, size_t size); +extern "C" void* realloc(void* ptr, size_t size); + extern "C" void free(void* p); extern "C" void __cxa_pure_virtual(); diff --git a/src/x86/initialize.cpp b/src/x86/initialize.cpp index 17dcdf0..57255fb 100644 --- a/src/x86/initialize.cpp +++ b/src/x86/initialize.cpp @@ -6,7 +6,7 @@ #include "../mmap.hpp" #include "../util.hpp" -using namespace kernel; +using namespace kernelns; extern int _kernelEnd; diff --git a/src/x86/interrupts.cpp b/src/x86/interrupts.cpp index 7845ef5..6eda16d 100644 --- a/src/x86/interrupts.cpp +++ b/src/x86/interrupts.cpp @@ -140,7 +140,7 @@ void syscallHandler(void* frame) } -kernel::Interrupts::Interrupts() +kernelns::Interrupts::Interrupts() { for(unsigned int i = 0; i <= MAX_SYSCALL_ID; i++) syscalls[i] = (void*) NULL; @@ -152,17 +152,17 @@ kernel::Interrupts::Interrupts() lidt(); } -void kernel::Interrupts::enable() +void kernelns::Interrupts::enable() { asm("sti"); } -void kernel::Interrupts::disable() +void kernelns::Interrupts::disable() { asm("cli"); } -void kernel::Interrupts::addSyscall(unsigned int id, void* function) +void kernelns::Interrupts::addSyscall(unsigned int id, void* function) { if(id <= MAX_SYSCALL_ID) syscalls[id] = function; diff --git a/src/x86/mmap.cpp b/src/x86/mmap.cpp index ea0e14f..3cddf3b 100644 --- a/src/x86/mmap.cpp +++ b/src/x86/mmap.cpp @@ -150,7 +150,7 @@ private: uint32_t physicalAddress : 20; }; -int kernel::mmap(void* start, size_t length, int flags) +int kernelns::mmap(void* start, size_t length, int flags) { if((size_t) start % 4096 != 0) return -1; @@ -184,7 +184,7 @@ int kernel::mmap(void* start, size_t length, int flags) return 0; } -int kernel::mmap(void* start, physaddr_t p_start, size_t length, int flags) +int kernelns::mmap(void* start, physaddr_t p_start, size_t length, int flags) { if((size_t) start % 4096 != 0 || p_start % 4096 != 0) return -1; @@ -198,7 +198,7 @@ int kernel::mmap(void* start, physaddr_t p_start, size_t length, int flags) return 0; } -int kernel::mapPage(void* start, physaddr_t p_start, int flags) +int kernelns::mapPage(void* start, physaddr_t p_start, int flags) { if((size_t) start % 4096 != 0 || p_start % 4096 != 0) return -1; @@ -228,7 +228,7 @@ int kernel::mapPage(void* start, physaddr_t p_start, int flags) return 0; } -int kernel::munmap(void* start, size_t length) +int kernelns::munmap(void* start, size_t length) { if((size_t) start % 4096 != 0) return -1; @@ -250,7 +250,7 @@ int kernel::munmap(void* start, size_t length) return 0; } -bool kernel::isMapped(void* addr) +bool kernelns::isMapped(void* addr) { PageTableEntry* pageDirectory = (PageTableEntry*) 0xFFFFF000; size_t tableIndex = (size_t) addr / 4096; @@ -263,14 +263,14 @@ bool kernel::isMapped(void* addr) return false; } -physaddr_t kernel::getPhysicalAddress(void* addr) +physaddr_t kernelns::getPhysicalAddress(void* addr) { PageTableEntry* pageTables = (PageTableEntry*) 0xFFC00000; size_t tableIndex = (size_t) addr / 4096; return pageTables[tableIndex].getPhysicalAddress() + ((size_t) addr & 0xFFF); } -int kernel::createAddressSpace(void* table) +int kernelns::createAddressSpace(void* table) { if(((size_t) table & 0xFFF) != 0) return -1; @@ -281,7 +281,7 @@ int kernel::createAddressSpace(void* table) return 0; } -int kernel::loadAddressSpace(physaddr_t table) +int kernelns::loadAddressSpace(physaddr_t table) { if((table & 0xFFF) != 0) return -1; diff --git a/src/x86/multiboot2.cpp b/src/x86/multiboot2.cpp index a191382..b1569f4 100644 --- a/src/x86/multiboot2.cpp +++ b/src/x86/multiboot2.cpp @@ -1,6 +1,6 @@ #include "multiboot2.hpp" -using namespace kernel; +using namespace kernelns; Multiboot2Info::Multiboot2Info(void* tableLocation) { diff --git a/src/x86/multiboot2.hpp b/src/x86/multiboot2.hpp index 9f9e944..6fcaf37 100644 --- a/src/x86/multiboot2.hpp +++ b/src/x86/multiboot2.hpp @@ -4,7 +4,7 @@ #include "../memorymap.hpp" #include "../module.hpp" -namespace kernel +namespace kernelns { class Multiboot2Info diff --git a/src/x86/tty.cpp b/src/x86/tty.cpp index b3132c5..8ee9929 100644 --- a/src/x86/tty.cpp +++ b/src/x86/tty.cpp @@ -1,7 +1,7 @@ #include #include "tty.hpp" -kernel::TTY::TTY(char* vga) +kernelns::TTY::TTY(char* vga) { this->vga = vga; this->cursor = 0; @@ -9,7 +9,7 @@ kernel::TTY::TTY(char* vga) this->base = 10; } -kernel::TTY& kernel::TTY::operator<<(kernel::TTY::Format fmt) +kernelns::TTY& kernelns::TTY::operator<<(kernelns::TTY::Format fmt) { switch(fmt) { @@ -25,42 +25,42 @@ kernel::TTY& kernel::TTY::operator<<(kernel::TTY::Format fmt) } } -kernel::TTY& kernel::TTY::operator<<(const char* str) +kernelns::TTY& kernelns::TTY::operator<<(const char* str) { return printString(str); } -kernel::TTY& kernel::TTY::operator<<(unsigned int n) +kernelns::TTY& kernelns::TTY::operator<<(unsigned int n) { return printNumber(n, base, width); } -kernel::TTY& kernel::TTY::operator<<(int n) +kernelns::TTY& kernelns::TTY::operator<<(int n) { return printNumber((unsigned int) n, base, width); } -kernel::TTY& kernel::TTY::operator<<(void* n) +kernelns::TTY& kernelns::TTY::operator<<(void* n) { return printNumber((unsigned int) n, 16, 8); } -kernel::TTY& kernel::TTY::operator<<(char c) +kernelns::TTY& kernelns::TTY::operator<<(char c) { return putChar(c); } -void kernel::TTY::setWidth(size_t width) +void kernelns::TTY::setWidth(size_t width) { this->width = width; } -size_t kernel::TTY::getWidth() +size_t kernelns::TTY::getWidth() { return width; } -void kernel::TTY::clear() +void kernelns::TTY::clear() { for(int i = 0; i < 80*25; i++) { @@ -69,7 +69,7 @@ void kernel::TTY::clear() cursor = 0; } -kernel::TTY& kernel::TTY::printNumber(unsigned int n, size_t base, +kernelns::TTY& kernelns::TTY::printNumber(unsigned int n, size_t base, size_t width) { const char* digits = "0123456789ABCDEF"; @@ -94,7 +94,7 @@ kernel::TTY& kernel::TTY::printNumber(unsigned int n, size_t base, return *this; } -kernel::TTY& kernel::TTY::printString(const char* str) +kernelns::TTY& kernelns::TTY::printString(const char* str) { while(*str) { @@ -104,7 +104,7 @@ kernel::TTY& kernel::TTY::printString(const char* str) return *this; } -kernel::TTY& kernel::TTY::putChar(char c) +kernelns::TTY& kernelns::TTY::putChar(char c) { switch(c) { diff --git a/src/x86/tty.hpp b/src/x86/tty.hpp index 47b5191..72bacf7 100644 --- a/src/x86/tty.hpp +++ b/src/x86/tty.hpp @@ -3,7 +3,7 @@ #include -namespace kernel +namespace kernelns { class TTY